csi_dsp_core.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*
  2. * Copyright (c) 2021 Alibaba Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include "../include/xrp_api.h"
  23. #include "../include/dsp_ps_ns.h"
  24. #include "../include/csi_dsp_api.h"
  25. #include "csi_dsp_core.h"
  26. #include "dsp_common.h"
  27. // #include "../xrp-host/xrp_host_common.h"
  28. #define DEV_ID 0
  29. #define DSP_INVALID_ITEM 0xdead
  30. int csi_dsp_cmd_send(const struct xrp_queue *queue,int cmd_type,void * cmd_payload,
  31. size_t payload_size,void *resp, size_t resp_size,struct xrp_buffer_group *buffer_group)
  32. {
  33. enum xrp_status status;
  34. size_t cmd_size= payload_size+4;
  35. s_cmd_t *cmd=(s_cmd_t *)malloc(cmd_size);
  36. DSP_PRINT(DEBUG,"dsp common cmd send %d\n",cmd_type);
  37. if(!cmd)
  38. {
  39. DSP_PRINT(WARNING,"malloc fail\n",__func__);
  40. return -1;
  41. }
  42. cmd->cmd=cmd_type;
  43. if(payload_size>0)
  44. {
  45. memcpy(cmd->data,cmd_payload,payload_size);
  46. }
  47. xrp_run_command_sync(queue,cmd,cmd_size,resp,resp_size,buffer_group,&status);
  48. if(status!=XRP_STATUS_SUCCESS)
  49. {
  50. DSP_PRINT(WARNING,"cmd fail\n",__func__);
  51. free(cmd);
  52. return -1;
  53. }
  54. free(cmd);
  55. return 0;
  56. }
  57. int csi_dsp_delete_instance(void* dsp)
  58. {
  59. if(!dsp)
  60. return 0;
  61. // printf("%s,entry\n",__FUNCTION__);
  62. struct csi_dsp_instance *instance = (struct csi_dsp_instance *)dsp;
  63. csi_dsp_disable_heartbeat_check();
  64. xrp_release_queue(instance->comm_queue);
  65. xrp_release_device(instance->device);
  66. free(dsp);
  67. DSP_PRINT(INFO,"exit\n");
  68. return 0;
  69. }
  70. void* csi_dsp_create_instance(int dsp_id)
  71. {
  72. enum xrp_status status;
  73. struct xrp_device *device;
  74. struct csi_dsp_instance *instance = NULL;
  75. struct xrp_queue * queue;
  76. unsigned char ns_id[]=XRP_PS_NSID_COMMON_CMD;
  77. dsp_InitEnv();
  78. instance=malloc(sizeof(*instance));
  79. if(!instance)
  80. {
  81. DSP_PRINT(ERROR,"malloc fail\n");
  82. return NULL;
  83. }
  84. device = xrp_open_device(dsp_id, &status);
  85. if(status!=XRP_STATUS_SUCCESS)
  86. {
  87. free(instance);
  88. DSP_PRINT(ERROR,"open device\n");
  89. return NULL;
  90. }
  91. instance->device=device;
  92. /* unsigned char XRP_NSID[] = XRP_PS_NSID_INITIALIZER;
  93. create a comon queue to handler the common message
  94. */
  95. queue = xrp_create_ns_queue(device, ns_id, &status);
  96. if(status!=XRP_STATUS_SUCCESS)
  97. {
  98. xrp_release_device(device);
  99. free(instance);
  100. DSP_PRINT(ERROR,"create ns queue faile\n");
  101. return NULL;
  102. }
  103. instance->comm_queue=queue;
  104. INIT_LIST_HEAD(&instance->task_list);
  105. //csi_dsp_enable_heartbeat_check(instance,10);
  106. DSP_PRINT(INFO,"dsp instance create successulf\n");
  107. return instance;
  108. }
  109. int csi_dsp_create_reporter(void* dsp)
  110. {
  111. struct csi_dsp_instance *instance = (struct csi_dsp_instance *)dsp;
  112. struct xrp_report *reporter = xrp_create_reporter(instance->device,MAX_REPORT_SIZE+32);
  113. if(reporter==NULL)
  114. {
  115. DSP_PRINT(ERROR,"create ns queue\n");
  116. return -1;
  117. }
  118. instance->report_impl=reporter;
  119. DSP_PRINT(INFO,"create reporter\n");
  120. return 0;
  121. }
  122. int csi_dsp_destroy_reporter(void *dsp)
  123. {
  124. struct csi_dsp_instance *instance = (struct csi_dsp_instance *)dsp;
  125. if(0 == xrp_release_reporter(instance->device,instance->report_impl))
  126. {
  127. instance->report_impl=NULL;
  128. }
  129. else
  130. {
  131. DSP_PRINT(ERROR,"reporter destroy fail\n");
  132. return -1;
  133. }
  134. DSP_PRINT(INFO,"release reporter\n");
  135. return 0;
  136. }
  137. static void dsp_task_init(struct csi_dsp_task_handler * task)
  138. {
  139. task->algo.algo_id = DSP_INVALID_ITEM;
  140. task->algo.task_id = DSP_INVALID_ITEM;
  141. task->fe.frontend_type = CSI_DSP_FE_TYPE_INVALID;
  142. task->be.backend_type = CSI_DSP_BE_TYPE_INVALID;
  143. task->report_id =-1;
  144. }
  145. /* queue ns 应该通过DSP侧来分配保证唯一性,区分不同进程的task.*/
  146. void *csi_dsp_create_task(void* dsp,csi_dsp_task_mode_e task_type)
  147. {
  148. struct csi_dsp_task_handler * task;
  149. struct csi_dsp_task_create_resp resp;
  150. struct xrp_queue *queue;
  151. struct csi_dsp_task_create_req config_para;
  152. enum xrp_status status;
  153. struct csi_dsp_instance *instance = (struct csi_dsp_instance *)dsp;
  154. dsp_handler_item_t *task_item =NULL;
  155. if(!instance)
  156. {
  157. DSP_PRINT(ERROR,"param check fail\n");
  158. return NULL;
  159. }
  160. task_item = malloc(sizeof(*task_item));
  161. if(!task_item)
  162. {
  163. DSP_PRINT(ERROR,"malloc fail\n");
  164. goto error1;
  165. }
  166. task= malloc(sizeof(*task));
  167. if(!task)
  168. {
  169. DSP_PRINT(ERROR,"malloc fail\n");
  170. goto error;
  171. }
  172. task_item->handler = task;
  173. config_para.type=task_type;
  174. if(csi_dsp_cmd_send(instance->comm_queue,PS_CMD_TASK_ALLOC,&config_para,sizeof(struct csi_dsp_task_create_req),&resp,sizeof(resp),NULL))
  175. {
  176. DSP_PRINT(ERROR,"PS_CMD_TASK_ALLOC fail\n");
  177. goto error;
  178. }
  179. if(resp.status!=CSI_DSP_OK)
  180. {
  181. DSP_PRINT(ERROR,"task creat fail:%d\n",resp.status);
  182. goto error;
  183. }
  184. task->queue = NULL;
  185. if(task_type == CSI_DSP_TASK_SW_TO_SW || task_type == CSI_DSP_TASK_SW_TO_HW)
  186. {
  187. queue = xrp_create_ns_queue(instance->device, resp.task_ns, &status);
  188. if(status!=XRP_STATUS_SUCCESS)
  189. {
  190. DSP_PRINT(ERROR,"queue creat fail\n");
  191. goto error;
  192. }
  193. task->queue=queue;
  194. csi_dsp_sw_task_manager_t *sw_task_ctx = malloc(sizeof(*sw_task_ctx));
  195. if(sw_task_ctx ==NULL)
  196. {
  197. DSP_PRINT(ERROR,"malloc fail\n");
  198. xrp_release_queue(queue);
  199. goto error;
  200. }
  201. INIT_LIST_HEAD(&sw_task_ctx->event_list);
  202. sw_task_ctx->event_num = 0;
  203. pthread_mutex_init(&sw_task_ctx->mutex, NULL);
  204. task->private = sw_task_ctx;
  205. if(status != XRP_STATUS_SUCCESS)
  206. {
  207. DSP_PRINT(ERROR,"xrp_create_buffer_group fail\n");
  208. free(sw_task_ctx);
  209. xrp_release_queue(queue);
  210. goto error;
  211. }
  212. }
  213. else{
  214. // HW task create report
  215. }
  216. task->buffers = xrp_create_buffer_group(&status);
  217. memcpy(task->task_ns,resp.task_ns,TASK_NAME_LINE);
  218. task->task_id=resp.task_id;
  219. task->instance=instance;
  220. task->mode=task_type;
  221. list_add_tail(&task_item->head,&instance->task_list);
  222. dsp_task_init(task);
  223. // task->report_id=-1;
  224. DSP_PRINT(INFO,"task(%d) ,ns(%x)create successful!\n",task->task_id,task->task_ns[0]);
  225. return task;
  226. error:
  227. free(task);
  228. error1:
  229. free(task_item);
  230. return NULL;
  231. }
  232. void csi_dsp_destroy_task(void *task_ctx)
  233. {
  234. csi_dsp_status_e resp;
  235. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  236. if(task_ctx ==NULL)
  237. {
  238. return;
  239. }
  240. if(task->queue)
  241. xrp_release_queue(task->queue);
  242. if(task->report_id>=0)
  243. {
  244. xrp_remove_report_item(task->instance->report_impl,task->report_id);
  245. }
  246. if(task->buffers)
  247. {
  248. xrp_release_buffer_group(task->buffers);
  249. }
  250. struct csi_dsp_task_free_req config_para;
  251. config_para.task_id = task->task_id;
  252. memcpy(config_para.task_ns,task->task_ns,sizeof(config_para.task_ns));
  253. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_TASK_FREE,&config_para,sizeof(struct csi_dsp_task_free_req),&resp,sizeof(resp),NULL))
  254. {
  255. DSP_PRINT(ERROR,"send PS_CMD_TASK_FREE fail\n");
  256. }
  257. if(resp!=CSI_DSP_OK)
  258. {
  259. DSP_PRINT(ERROR,"TASK FREE Fail due to %d\n",resp);
  260. }
  261. DSP_PRINT(INFO,"task(%d) ,ns(%x) destroy successful!\n",task->task_id,task->task_ns[0]);
  262. free(task);
  263. }
  264. int csi_dsp_task_config_frontend(void *task_ctx,struct csi_dsp_task_fe_para* config_para)
  265. {
  266. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  267. csi_dsp_status_e resp =0;
  268. if(!task || !task->instance || !task->instance->comm_queue)
  269. {
  270. DSP_PRINT(ERROR,"param check fail\n");
  271. return -1;
  272. }
  273. config_para->task_id= task->task_id ;
  274. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_FE_CONFIG,config_para,sizeof(struct csi_dsp_task_fe_para),&resp,sizeof(resp),NULL))
  275. {
  276. DSP_PRINT(ERROR,"config_frontend cmd send fail\n");
  277. return -1;
  278. }
  279. if(resp != CSI_DSP_OK)
  280. {
  281. DSP_PRINT(ERROR,"PS_CMD_FE_CONFIG fail %d\n",resp);
  282. return -1;
  283. }
  284. memcpy(&task->fe,config_para,sizeof(task->fe));
  285. DSP_PRINT(INFO,"task(%d) set frontend %d!\n",config_para->task_id,config_para->frontend_type);
  286. return 0;
  287. }
  288. int csi_dsp_task_get_frontend(void *task_ctx,struct csi_dsp_task_fe_para *config_para)
  289. {
  290. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  291. csi_dsp_status_e resp =0;
  292. if(!config_para|!task || !task->instance || !task->instance->comm_queue)
  293. {
  294. DSP_PRINT(ERROR,"param check fail\n");
  295. return -1;
  296. }
  297. memcpy(config_para,&task->fe,sizeof(task->fe));
  298. return 0;
  299. }
  300. int csi_dsp_task_config_backend(void *task_ctx,struct csi_dsp_task_be_para* config_para)
  301. {
  302. csi_dsp_status_e resp =0;
  303. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  304. size_t sz;
  305. if(!task || !task->instance || !task->instance->comm_queue)
  306. {
  307. DSP_PRINT(ERROR,"param check fail\n");
  308. return -1;
  309. }
  310. config_para->task_id= task->task_id ;
  311. if(config_para->backend_type == CSI_DSP_BE_TYPE_HOST)
  312. {
  313. sz= sizeof(struct csi_dsp_task_be_para)+sizeof(struct csi_dsp_buffer)*config_para->sw_param.num_buf;
  314. }
  315. else
  316. {
  317. sz= sizeof(struct csi_dsp_task_be_para);
  318. }
  319. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_BE_CONFIG, config_para,sz,&resp,sizeof(resp),NULL))
  320. {
  321. DSP_PRINT(ERROR,"send cmd fail\n");
  322. return -1;
  323. }
  324. if(resp != CSI_DSP_OK)
  325. {
  326. DSP_PRINT(ERROR,"resp ERROR: %d\n",resp);
  327. return -1;
  328. }
  329. memcpy(&task->be,config_para,sizeof(task->be));
  330. DSP_PRINT(INFO,"task(%d) set backend %d!\n",task->task_id,config_para->backend_type);
  331. return 0;
  332. }
  333. int csi_dsp_task_update_backend_buf(void *task_ctx,struct csi_dsp_task_be_para* config_para)
  334. {
  335. csi_dsp_status_e resp =0;
  336. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  337. size_t sz;
  338. if(!task || !task->instance || !task->instance->comm_queue)
  339. {
  340. DSP_PRINT(ERROR,"param check fail\n");
  341. return -1;
  342. }
  343. if(config_para->backend_type !=CSI_DSP_BE_TYPE_HOST)
  344. {
  345. DSP_PRINT(ERROR,"unsport backend type\n");
  346. return -1;
  347. }
  348. config_para->task_id= task->task_id ;
  349. if(config_para->backend_type == CSI_DSP_BE_TYPE_HOST)
  350. {
  351. sz= sizeof(struct csi_dsp_task_be_para)+sizeof(struct csi_dsp_buffer)*config_para->sw_param.num_buf;
  352. }
  353. else
  354. {
  355. sz= sizeof(struct csi_dsp_task_be_para);
  356. }
  357. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_BE_ASSGIN_BUF, config_para,sz,&resp,sizeof(resp),NULL))
  358. {
  359. DSP_PRINT(ERROR,"send cmd fail\n");
  360. return -1;
  361. }
  362. if(resp != CSI_DSP_OK)
  363. {
  364. DSP_PRINT(ERROR,"resp ERROR: %d\n",resp);
  365. return -1;
  366. }
  367. DSP_PRINT(INFO,"task(%d) set backend %d!\n",task->task_id,config_para->backend_type);
  368. return 0;
  369. }
  370. int csi_dsp_task_get_backend(void *task_ctx,struct csi_dsp_task_be_para* config_para)
  371. {
  372. csi_dsp_status_e resp =0;
  373. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  374. if(!task || !task->instance || !task->instance->comm_queue)
  375. {
  376. DSP_PRINT(ERROR,"param check fail\n");
  377. return -1;
  378. }
  379. memcpy(config_para,&task->be,sizeof(task->be));
  380. return 0;
  381. }
  382. int csi_dsp_task_config_algo(void *task_ctx,struct csi_dsp_algo_config_par* config_para)
  383. {
  384. csi_dsp_status_e resp =0;
  385. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  386. struct csi_dsp_algo_config_par config;
  387. struct xrp_buffer *buffer = NULL;
  388. struct xrp_buffer *algo_buffer = NULL;
  389. struct xrp_buffer *algo_set_buffer = NULL;
  390. enum xrp_status status;
  391. void* buf_ptr;
  392. struct xrp_buffer_group *buffer_group =NULL;
  393. if(!task || !task->instance || !task->instance->comm_queue)
  394. {
  395. DSP_PRINT(ERROR,"param check fail\n");
  396. return -1;
  397. }
  398. config.task_id= task->task_id ;
  399. config.algo_id = config_para->algo_id;
  400. config.algo_ptr = 0;
  401. config.sett_length = config_para->sett_length;
  402. config.sett_ptr = 0;
  403. config.bufs_ptr = 0;
  404. config.buf_num = config_para->buf_num;
  405. config.algo_size = config_para->algo_size;
  406. buffer_group = xrp_create_buffer_group(&status);
  407. if(status != XRP_STATUS_SUCCESS)
  408. {
  409. DSP_PRINT(ERROR,"malloc buf group fail\n");
  410. return -1;
  411. }
  412. if(config_para->sett_ptr && (config_para->sett_length !=0))
  413. {
  414. buffer = xrp_create_buffer(task->instance->device,config.sett_length,NULL,&status);
  415. if(status != XRP_STATUS_SUCCESS)
  416. {
  417. DSP_PRINT(ERROR,"malloc buf fail\n");
  418. goto Err3;
  419. }
  420. xrp_add_buffer_to_group(buffer_group,buffer,XRP_READ,&status);
  421. xrp_buffer_get_info(buffer,XRP_BUFFER_PHY_ADDR,&config.sett_ptr,sizeof(config.sett_ptr),&status);
  422. xrp_buffer_get_info(buffer,XRP_BUFFER_USER_ADDR,&buf_ptr,sizeof(buf_ptr),&status);
  423. memcpy(buf_ptr,(void*)config_para->sett_ptr,config_para->sett_length);
  424. DSP_PRINT(INFO," setting buf phy:%llx\n",config.sett_ptr);
  425. }
  426. if(config_para->bufs_ptr && (config_para->buf_num !=0))
  427. {
  428. algo_set_buffer = xrp_create_buffer(task->instance->device,config_para->buf_num*sizeof(struct csi_dsp_buffer),NULL,&status);
  429. if(status != XRP_STATUS_SUCCESS)
  430. {
  431. DSP_PRINT(ERROR,"malloc buf fail\n");
  432. goto Err3;
  433. }
  434. xrp_add_buffer_to_group(buffer_group,algo_set_buffer,XRP_READ,&status);
  435. xrp_buffer_get_info(algo_set_buffer,XRP_BUFFER_PHY_ADDR,&config.bufs_ptr,sizeof(config.sett_ptr),&status);
  436. xrp_buffer_get_info(algo_set_buffer,XRP_BUFFER_USER_ADDR,&buf_ptr,sizeof(buf_ptr),&status);
  437. memcpy(buf_ptr,(void*)config_para->bufs_ptr,config_para->buf_num*sizeof(struct csi_dsp_buffer));
  438. DSP_PRINT(INFO," algo_set_buffer phy:%llx\n",config.bufs_ptr);
  439. }
  440. if(config_para->algo_ptr && (config_para->algo_size !=0))
  441. {
  442. algo_buffer = xrp_create_buffer(task->instance->device,config_para->algo_size,NULL,&status);
  443. if(status != XRP_STATUS_SUCCESS)
  444. {
  445. DSP_PRINT(ERROR,"malloc buf fail\n");
  446. goto Err2;
  447. }
  448. xrp_add_buffer_to_group(buffer_group,algo_buffer,XRP_READ,&status);
  449. xrp_buffer_get_info(algo_buffer,XRP_BUFFER_PHY_ADDR,&config.algo_ptr,sizeof(config.algo_ptr),&status);
  450. xrp_buffer_get_info(algo_buffer,XRP_BUFFER_USER_ADDR,&buf_ptr,sizeof(buf_ptr),&status);
  451. memcpy(buf_ptr,(void*)config_para->algo_ptr,config_para->algo_size);
  452. DSP_PRINT(INFO,"algo buf phy:%llx\n",config_para->algo_ptr);
  453. }
  454. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_ALGO_CONFIG, &config,sizeof(struct csi_dsp_algo_config_par),&resp,sizeof(resp),buffer_group))
  455. {
  456. DSP_PRINT(ERROR,"send cmd fail\n");
  457. goto Err1;
  458. }
  459. if(resp != CSI_DSP_OK)
  460. {
  461. DSP_PRINT(ERROR,"resp fail %d\n",resp);
  462. goto Err1;
  463. }
  464. memcpy(&task->algo,&config,sizeof(config));
  465. if(buffer)
  466. {
  467. xrp_release_buffer(buffer);
  468. }
  469. if(algo_set_buffer)
  470. {
  471. xrp_release_buffer(algo_set_buffer);
  472. }
  473. if(algo_buffer)
  474. {
  475. xrp_release_buffer(algo_buffer);
  476. }
  477. xrp_release_buffer_group(buffer_group);
  478. DSP_PRINT(INFO,"task %d sucessful!\n",task->task_id);
  479. return 0;
  480. Err1:
  481. if(algo_buffer) xrp_release_buffer(algo_buffer);
  482. Err2:
  483. if(buffer) xrp_release_buffer(buffer);
  484. Err3:
  485. xrp_release_buffer_group(buffer_group);
  486. return -1;
  487. }
  488. int csi_dsp_task_load_algo(void *task_ctx, csi_dsp_algo_load_req_t* config_para)
  489. {
  490. csi_dsp_algo_load_resp_t resp;
  491. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  492. if(!task || !task->instance || !task->instance->comm_queue)
  493. {
  494. DSP_PRINT(ERROR,"param check fail\n");
  495. return -1;
  496. }
  497. config_para->task_id= task->task_id;
  498. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_ALGO_LOAD, config_para,sizeof(csi_dsp_algo_load_req_t),&resp,sizeof(resp),NULL))
  499. {
  500. DSP_PRINT(ERROR,"send cmd fail\n");
  501. return -1;
  502. }
  503. if(resp.status != CSI_DSP_OK)
  504. {
  505. DSP_PRINT(ERROR,"resp fail %d\n",resp.status);
  506. return -1;
  507. }
  508. task->algo.algo_id=config_para->algo_id;
  509. task->algo.algo_ptr = config_para->algo_ptr;
  510. // task->algo.buf_num = resp.buf_desc_num;
  511. // task->algo.sett_length = resp.
  512. DSP_PRINT(INFO,"task %d load algo sucessful!\n",task->task_id);
  513. return 0;
  514. }
  515. int csi_dsp_task_acquire_algo(void *task_ctx,char*name)
  516. {
  517. FILE * fp;
  518. int size;
  519. int rev = 0;
  520. char file[128];
  521. struct xrp_buffer *buffer = NULL;
  522. enum xrp_status status;
  523. void* buf_ptr;
  524. uint64_t buf_phy;
  525. struct xrp_buffer_group *buffer_group =NULL;
  526. csi_dsp_algo_load_resp_t resp;
  527. csi_dsp_algo_load_req_t config_para;
  528. if(task_ctx == NULL || name ==NULL || strlen(name)>100 )
  529. {
  530. DSP_PRINT(ERROR,"param check fail\n");
  531. return -1;
  532. }
  533. sprintf(file,"/lib/firmware/%s.lib",name);
  534. DSP_PRINT(DEBUG,"open file:%s\n",file);
  535. fp = fopen(file, "rb");
  536. if(fp==NULL)
  537. {
  538. DSP_PRINT(ERROR,"open file fail\n");
  539. return -1;
  540. }
  541. fseek(fp , 0, SEEK_END);
  542. size = ftell(fp);
  543. rewind(fp);
  544. if(size ==0)
  545. {
  546. return -1;
  547. }
  548. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  549. buffer_group = xrp_create_buffer_group(&status);
  550. if(status != XRP_STATUS_SUCCESS)
  551. {
  552. DSP_PRINT(ERROR,"malloc buf group fail\n");
  553. return -1;
  554. }
  555. buffer = xrp_create_buffer(task->instance->device,size,NULL,&status);
  556. if(status != XRP_STATUS_SUCCESS)
  557. {
  558. DSP_PRINT(ERROR,"malloc buf fail\n");
  559. return -1;
  560. }
  561. xrp_add_buffer_to_group(buffer_group,buffer,XRP_READ,&status);
  562. xrp_buffer_get_info(buffer,XRP_BUFFER_PHY_ADDR,&buf_phy,sizeof(buf_phy),&status);
  563. xrp_buffer_get_info(buffer,XRP_BUFFER_USER_ADDR,&buf_ptr,sizeof(buf_ptr),&status);
  564. DSP_PRINT(DEBUG,"algo buf virtual:0x%p, phy:0x%lx\n", buf_ptr,buf_phy);
  565. rev = fread(buf_ptr, 1, size, fp);
  566. if(rev != size)
  567. {
  568. DSP_PRINT(ERROR,"Loading file failed\n");
  569. return -1;
  570. }
  571. fclose(fp);
  572. config_para.task_id= task->task_id;
  573. config_para.algo_id =-1;
  574. config_para.algo_ptr = buf_phy;
  575. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_ALGO_LOAD, &config_para,sizeof(csi_dsp_algo_load_req_t),&resp,sizeof(resp),buffer_group))
  576. {
  577. DSP_PRINT(ERROR,"send cmd fail\n");
  578. return -1;
  579. }
  580. if(resp.status != CSI_DSP_OK)
  581. {
  582. DSP_PRINT(ERROR,"resp fail %d\n",resp.status);
  583. return -1;
  584. }
  585. if(buffer)
  586. {
  587. // memset(buf_ptr,0xee,config_para->sett_length);
  588. xrp_release_buffer(buffer);
  589. xrp_release_buffer_group(buffer_group);
  590. }
  591. task->algo.algo_id=config_para.algo_id;
  592. task->algo.algo_ptr = config_para.algo_ptr;
  593. DSP_PRINT(INFO,"task %d acquire algo:%s sucessful!\n",task->task_id,name);
  594. return 0;
  595. }
  596. int csi_dsp_task_start(void *task_ctx)
  597. {
  598. csi_dsp_status_e resp;
  599. struct csi_dsp_task_start_req req;
  600. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  601. req.task_id = task->task_id;
  602. if(task== NULL)
  603. {
  604. DSP_PRINT(ERROR,"ERR Invalid task \n");
  605. return -1;
  606. }
  607. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_TASK_START,&req,sizeof(struct csi_dsp_task_start_req),&resp,sizeof(resp),NULL))
  608. {
  609. DSP_PRINT(ERROR,"csi_dsp_task_start fail \n",resp);
  610. return -1;
  611. }
  612. if(resp != CSI_DSP_OK )
  613. {
  614. DSP_PRINT(ERROR,"csi_dsp_task_start resp due to %d\n",resp);
  615. return -1;
  616. }
  617. DSP_PRINT(INFO,"task:%d start sucessful!\n",task->task_id);
  618. return 0;
  619. }
  620. int csi_dsp_task_stop(void *task_ctx)
  621. {
  622. csi_dsp_status_e resp;
  623. struct csi_dsp_task_start_req req;
  624. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  625. if(task== NULL)
  626. {
  627. DSP_PRINT(ERROR,"ERR Invalid task \n");
  628. return -1;
  629. }
  630. req.task_id = task->task_id;
  631. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_TASK_STOP,&req,sizeof(req),&resp,sizeof(resp),NULL))
  632. {
  633. return -1;
  634. }
  635. if(resp != CSI_DSP_OK)
  636. {
  637. DSP_PRINT(ERROR,"csi_dsp_task_start fail due to %d\n",resp);
  638. }
  639. DSP_PRINT(INFO,"task:%d stop!\n",task->task_id);
  640. return 0;
  641. }
  642. static int csi_dsp_config_report_item_to_dsp(void *task_ctx,enum cmd_type flag)
  643. {
  644. csi_dsp_status_e resp;
  645. struct report_config_msg config;
  646. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  647. if(!task || task->report_id<0 || !task->instance || !task->instance->comm_queue)
  648. {
  649. DSP_PRINT(ERROR,"param check fail\n");
  650. return -1;
  651. }
  652. config.report_id=task->report_id;
  653. config.flag = flag;
  654. memcpy(config.task,task->task_ns,TASK_NAME_LINE);
  655. config.addr = 0xdeadbeef;
  656. config.size = task->report_size;
  657. if(csi_dsp_cmd_send(task->instance->comm_queue,PS_CMD_REPORT_CONFIG,&config,sizeof(struct report_config_msg),&resp,sizeof(resp),NULL))
  658. {
  659. DSP_PRINT(ERROR,"send PS_CMD_REPORT_CONFIG fail\n");
  660. return -1;
  661. }
  662. if(resp != CSI_DSP_OK)
  663. {
  664. DSP_PRINT(ERROR,"PS_CMD_REPORT_CONFIG fail due to %d\n",resp);
  665. return -1;
  666. }
  667. return 0;
  668. }
  669. int csi_dsp_task_register_cb(void *task_ctx,
  670. int (*cb)(void*context,void*data),
  671. void* context,
  672. size_t data_size)
  673. {
  674. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  675. task->report_id = task->task_id;
  676. if(task->report_id<0)
  677. {
  678. DSP_PRINT(WARNING,"report id is invalid\n");
  679. return -1;
  680. }
  681. task->report_size = data_size;
  682. if(xrp_add_report_item_with_id(task->instance->report_impl,
  683. cb,task->report_id,context,data_size)<0)
  684. {
  685. DSP_PRINT(WARNING,"report id is invalid\n");
  686. return -1;
  687. }
  688. if(csi_dsp_config_report_item_to_dsp(task,CMD_SETUP))
  689. {
  690. DSP_PRINT(WARNING,"report id is invalid\n");
  691. return -1;
  692. }
  693. task->cb = cb;
  694. task->context = context;
  695. DSP_PRINT(INFO,"new reprot %d is created and register to DSP\n",task->report_id);
  696. return 0;
  697. }
  698. int csi_dsp_ps_task_unregister_cb(void *task_ctx)
  699. {
  700. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  701. if(task->report_id<0)
  702. {
  703. DSP_PRINT(WARNING,"report id is invalid\n");
  704. return -1;
  705. }
  706. if(csi_dsp_config_report_item_to_dsp(task,CMD_RELEASE))
  707. {
  708. DSP_PRINT(WARNING,"report release fail\n");
  709. return -1;
  710. }
  711. xrp_remove_report_item(task->instance->report_impl,task->report_id);
  712. DSP_PRINT(INFO,"new reprot %d is unregister to DSP\n",task->report_id);
  713. task->report_id =-1;
  714. return 0;
  715. }
  716. struct csi_sw_task_req* csi_dsp_task_create_request(void *task_ctx)
  717. {
  718. struct csi_dsp_task_handler * task = (struct csi_dsp_task_handler *)task_ctx;
  719. struct csi_sw_task_req* req = NULL;
  720. void* req_ptr,*event_ptr;
  721. enum xrp_status status;
  722. if(task->mode != CSI_DSP_TASK_SW_TO_SW)
  723. {
  724. DSP_PRINT(WARNING,"un-support for task type:%d\n",task->mode);
  725. return NULL;
  726. }
  727. if(task->algo.algo_id == DSP_INVALID_ITEM)
  728. {
  729. DSP_PRINT(WARNING,"algo is not loaded:%d\n",task->algo.algo_id);
  730. return NULL;
  731. }
  732. req = malloc(sizeof(*req));
  733. if(req == NULL)
  734. {
  735. DSP_PRINT(WARNING,"memroy alloc fail\n");
  736. return NULL;
  737. }
  738. req->priv = xrp_create_buffer_group(&status);
  739. if(status != XRP_STATUS_SUCCESS)
  740. {
  741. DSP_PRINT(WARNING,"buffer group create fail\n");
  742. free(req);
  743. return NULL;
  744. }
  745. req->status = CSI_DSP_SW_REQ_IDLE;
  746. req->algo_id = task->algo.algo_id;
  747. req->task = task_ctx;
  748. req->buffer_num =0;
  749. req->request_id = rand();
  750. req->sett_length =0;
  751. req->sett_ptr = NULL;
  752. DSP_PRINT(DEBUG,"new req %d is created in task %d\n",req->request_id,task->task_id);
  753. return req;
  754. }
  755. int csi_dsp_task_release_request(struct csi_sw_task_req* req)
  756. {
  757. struct xrp_buffer_group *group;
  758. enum xrp_status status;
  759. int buf_idx,plane_idx;;
  760. struct xrp_buffer *buffer;
  761. int index;
  762. size_t buf_num;
  763. struct csi_dsp_task_handler * task =(struct csi_dsp_task_handler *) req->task;
  764. if(req == NULL)
  765. {
  766. return 0;
  767. }
  768. group = (struct xrp_buffer_group *)req->priv;
  769. if(group == NULL)
  770. {
  771. free(req);
  772. DSP_PRINT(WARNING,"buffer group create fail\n");
  773. return -1;
  774. }
  775. for(buf_idx=0;buf_idx<req->buffer_num;buf_idx++)
  776. {
  777. if(req->buffers[buf_idx].type == CSI_DSP_BUF_TYPE_DMA_BUF_IMPORT ||
  778. req->buffers[buf_idx].type == CSI_DSP_BUF_TYPE_DMA_BUF_EXPORT)
  779. {
  780. for(plane_idx =0 ;plane_idx<req->buffers[buf_idx].plane_count;plane_idx++)
  781. {
  782. xrp_release_dma_buf(task->instance->device,req->buffers[buf_idx].planes[plane_idx].fd,
  783. req->buffers[buf_idx].planes[plane_idx].buf_vir,
  784. req->buffers[buf_idx].planes[plane_idx].size,&status);
  785. }
  786. }
  787. else
  788. {
  789. for(plane_idx =0 ;plane_idx<req->buffers[buf_idx].plane_count;plane_idx++)
  790. {
  791. // printf("release buf %d plane %d\n",buf_idx,plane_idx);
  792. buffer = xrp_get_buffer_from_group(group,req->buffers[buf_idx].planes[plane_idx].fd,&status);
  793. xrp_unmap_buffer(buffer,(void *)req->buffers[buf_idx].planes[plane_idx].buf_vir,&status);
  794. xrp_release_buffer(buffer);
  795. }
  796. }
  797. }
  798. /*free sett buf */
  799. xrp_buffer_group_get_info(group,XRP_BUFFER_GROUP_SIZE_SIZE_T, 0,&buf_num,sizeof(buf_num),&status);
  800. if(status!=XRP_STATUS_SUCCESS)
  801. {
  802. DSP_PRINT(WARNING,"Get sett buffer release fail\n");
  803. return -1;
  804. }
  805. uint64_t buf_phy;
  806. for(index=0; index<buf_num; index++)
  807. {
  808. buffer = xrp_get_buffer_from_group(group,index,&status);
  809. xrp_buffer_get_info(buffer,XRP_BUFFER_PHY_ADDR,&buf_phy,sizeof(buf_phy),&status);
  810. if(buf_phy == req->sett_ptr)
  811. {
  812. // xrp_unmap_buffer(buffer,(void *)req->buffers[buf_idx].planes[plane_idx].buf_vir,&status);
  813. xrp_release_buffer(buffer);
  814. break;
  815. }
  816. }
  817. xrp_release_buffer_group(group);
  818. DSP_PRINT(DEBUG,"req %d is release successful!\n",req->request_id);
  819. free(req);
  820. return 0;
  821. }
  822. int csi_dsp_task_create_buffer(void * task_ctx,struct csi_dsp_buffer * buffer)
  823. {
  824. struct csi_dsp_task_handler * task= (struct csi_dsp_task_handler *)task_ctx;
  825. int i,j;
  826. struct xrp_buffer * buf=NULL;
  827. size_t buf_size;
  828. int fail_release =0;
  829. uint64_t phy_addr;
  830. enum xrp_status status;
  831. if(task == NULL || buffer==NULL)
  832. {
  833. DSP_PRINT(WARNING,"param check fail\n");
  834. return -1;
  835. }
  836. if(task->buffers==NULL)
  837. {
  838. DSP_PRINT(WARNING,"Buffrs not init\n");
  839. return -1;
  840. }
  841. switch(buffer->type)
  842. {
  843. case CSI_DSP_BUF_ALLOC_DRV:
  844. for(i=0;i<buffer->plane_count;i++)
  845. {
  846. buf = xrp_create_buffer(task->instance->device,buffer->planes[i].size,NULL,&status);
  847. if(status != XRP_STATUS_SUCCESS)
  848. {
  849. DSP_PRINT(WARNING,"create xrp buffer fail\n");
  850. goto err_1;
  851. }
  852. else
  853. {
  854. int flag = buffer->dir == CSI_DSP_BUFFER_IN ?XRP_READ:XRP_WRITE;
  855. buffer->planes[i].buf_vir = xrp_map_buffer(buf,0,buffer->planes[i].size,flag,&status);
  856. if(status != XRP_STATUS_SUCCESS)
  857. {
  858. xrp_release_buffer(buf);
  859. DSP_PRINT(WARNING,"Error Map Buffrs not fail\n");
  860. goto err_1;
  861. }
  862. // printf("%s,Debug V:%llx,P:%llx\n",__FUNCTION__,buf->ptr,buf->phy_addr);
  863. xrp_buffer_get_info(buf,XRP_BUFFER_PHY_ADDR,&buffer->planes[i].buf_phy,sizeof(phy_addr),&status);
  864. if(status != XRP_STATUS_SUCCESS)
  865. {
  866. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  867. xrp_release_buffer(buf);
  868. DSP_PRINT(WARNING,"Error get phy addr fail\n");
  869. goto err_1;
  870. }
  871. buffer->planes[i].fd = xrp_add_buffer_to_group(task->buffers,buf,flag,&status);
  872. if(status !=XRP_STATUS_SUCCESS)
  873. {
  874. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  875. xrp_release_buffer(buf);
  876. DSP_PRINT(WARNING,"add to buffer group fail\n");
  877. goto err_1;
  878. }
  879. }
  880. DSP_PRINT(DEBUG,"create buffer:Vaddr(0x%llx),Paddr(0x%llx)\n", buffer->planes[i].buf_vir,buffer->planes[i].buf_phy);
  881. }
  882. return 0;
  883. err_1:
  884. for(j=0;j<i;j++)
  885. {
  886. buf = xrp_get_buffer_from_group(task->buffers, buffer->planes[j].fd,&status);
  887. xrp_unmap_buffer(buf,buffer->planes[j].buf_vir,&status);
  888. xrp_release_buffer(buf);
  889. }
  890. return -1;
  891. case CSI_DSP_BUF_ALLOC_APP:
  892. return 0;
  893. case CSI_DSP_BUF_TYPE_DMA_BUF_EXPORT:
  894. DSP_PRINT(INFO,"ERR DMA buffer export not support\n");
  895. return 0;
  896. case CSI_DSP_BUF_TYPE_DMA_BUF_IMPORT:
  897. for(i=0;i<buffer->plane_count;i++)
  898. {
  899. int flag = buffer->dir == CSI_DSP_BUFFER_IN ?XRP_READ:XRP_WRITE;
  900. xrp_import_dma_buf(task->instance->device,buffer->planes[i].fd,flag,&buffer->planes[i].buf_phy,
  901. &buffer->planes[i].buf_vir,&buffer->planes[i].size,&status);
  902. if(status !=XRP_STATUS_SUCCESS )
  903. {
  904. DSP_PRINT(WARNING,"dma buf import fail\n");
  905. goto err_2;
  906. }
  907. }
  908. break;
  909. err_2:
  910. for(j=0;j<i;j++)
  911. {
  912. xrp_release_dma_buf(task->instance->device,buffer->planes[i].fd,buffer->planes[i].buf_vir,buffer->planes[i].size,&status);
  913. }
  914. return -1;
  915. default:
  916. DSP_PRINT(WARNING,"buffer type:%d not support\n",buffer->type);
  917. return -1;
  918. }
  919. return 0;
  920. }
  921. int csi_dsp_task_free_buffer(void * task_ctx,struct csi_dsp_buffer * buffer)
  922. {
  923. struct csi_dsp_task_handler * task= (struct csi_dsp_task_handler *)task_ctx;
  924. int i,j;
  925. struct xrp_buffer * buf=NULL;
  926. size_t buf_size;
  927. int fail_release =0;
  928. uint64_t phy_addr;
  929. enum xrp_status status;
  930. if(task == NULL||task->buffers==NULL||buffer==NULL)
  931. {
  932. DSP_PRINT(WARNING,"param check fail\n")
  933. return -1;
  934. }
  935. switch(buffer->type)
  936. {
  937. case CSI_DSP_BUF_ALLOC_DRV:
  938. for(i=0;i<buffer->plane_count;i++)
  939. {
  940. buf = xrp_get_buffer_from_group(task->buffers,buffer->planes[i].fd,&status);
  941. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  942. xrp_release_buffer(buf);
  943. }
  944. break;
  945. case CSI_DSP_BUF_ALLOC_APP:
  946. break;
  947. case CSI_DSP_BUF_TYPE_DMA_BUF_EXPORT:
  948. DSP_PRINT(WARNING,"buffer type not support\n");
  949. break;
  950. case CSI_DSP_BUF_TYPE_DMA_BUF_IMPORT:
  951. for(i=0;i<buffer->plane_count;i++)
  952. {
  953. xrp_release_dma_buf(task->instance->device,buffer->planes[i].fd,buffer->planes[i].buf_vir,buffer->planes[i].size,&status);
  954. if(status != XRP_STATUS_SUCCESS)
  955. {
  956. DSP_PRINT(WARNING,"ERR DMA Buffrs(%d) Release fail\n",buffer->planes[i].fd);
  957. return -1;
  958. }
  959. }
  960. break;
  961. default:
  962. DSP_PRINT(WARNING,"buffer type:%d not support\n",buffer->type);
  963. return -1;
  964. }
  965. return 0;
  966. }
  967. int csi_dsp_request_add_buffer(struct csi_sw_task_req* req,struct csi_dsp_buffer * buffer)
  968. {
  969. struct csi_dsp_task_handler * task=NULL;
  970. struct xrp_buffer * buf=NULL;
  971. enum xrp_status status;
  972. int i,j;
  973. struct xrp_buffer_group *buf_gp ;
  974. if(!req )
  975. {
  976. return -1;
  977. }
  978. buf_gp = (struct xrp_buffer_group *)req->priv;
  979. task = req->task;
  980. int flag = buffer->dir == CSI_DSP_BUFFER_IN ?XRP_READ:XRP_WRITE;
  981. switch(buffer->type)
  982. {
  983. case CSI_DSP_BUF_ALLOC_DRV:
  984. for(i=0;i<buffer->plane_count;i++)
  985. {
  986. buf = xrp_create_buffer(task->instance->device,buffer->planes[i].size,NULL,&status);
  987. if(status != XRP_STATUS_SUCCESS)
  988. {
  989. DSP_PRINT(WARNING,"create buffer failed\n");
  990. goto err_1;
  991. }
  992. else
  993. {
  994. buffer->planes[i].buf_vir = xrp_map_buffer(buf,0,buffer->planes[i].size,flag,&status);
  995. if(status != XRP_STATUS_SUCCESS)
  996. {
  997. DSP_PRINT(WARNING,"Map Buffrs not init\n");
  998. xrp_release_buffer(buf);
  999. goto err_1;
  1000. }
  1001. xrp_buffer_get_info(buf,XRP_BUFFER_PHY_ADDR,&buffer->planes[i].buf_phy,sizeof(uint64_t),&status);
  1002. if(status != XRP_STATUS_SUCCESS)
  1003. {
  1004. DSP_PRINT(WARNING,"xrp_buffer_get_info failed\n");
  1005. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  1006. xrp_release_buffer(buf);
  1007. goto err_1;
  1008. }
  1009. buffer->planes[i].fd = xrp_add_buffer_to_group(buf_gp,buf,flag,&status);
  1010. if(status !=XRP_STATUS_SUCCESS)
  1011. {
  1012. DSP_PRINT(WARNING,"xrp_add_buffer_to_group failed\n");
  1013. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  1014. xrp_release_buffer(buf);
  1015. goto err_1;
  1016. }
  1017. }
  1018. DSP_PRINT(DEBUG,"create buffer:Vaddr(0x%llx),Paddr(0x%llx)\n", buffer->planes[i].buf_vir,buffer->planes[i].buf_phy);
  1019. }
  1020. memcpy(&req->buffers[req->buffer_num++],buffer,sizeof(*buffer));
  1021. return 0;
  1022. err_1:
  1023. for(j=0;j<i;j++)
  1024. {
  1025. buf = xrp_get_buffer_from_group(buf_gp,buffer->planes[j].fd,&status);
  1026. xrp_unmap_buffer(buf,buffer->planes[j].buf_vir,&status);
  1027. xrp_release_buffer(buf);
  1028. }
  1029. return -1;
  1030. case CSI_DSP_BUF_TYPE_DMA_BUF_EXPORT:
  1031. DSP_PRINT(INFO,"ERR DMA buffer export not support\n");
  1032. return -1;
  1033. case CSI_DSP_BUF_TYPE_DMA_BUF_IMPORT:
  1034. for(i=0;i<buffer->plane_count;i++)
  1035. {
  1036. xrp_import_dma_buf(task->instance->device,buffer->planes[i].fd,flag,&buffer->planes[i].buf_phy,
  1037. & buffer->planes[i].buf_vir,&buffer->planes[i].size,&status);
  1038. if(status !=XRP_STATUS_SUCCESS )
  1039. {
  1040. DSP_PRINT(WARNING,"dma buf import fail\n");
  1041. goto err_2;
  1042. }
  1043. }
  1044. memcpy(&req->buffers[req->buffer_num++],buffer,sizeof(*buffer));
  1045. break;
  1046. err_2:
  1047. for(j=0;j<i;j++)
  1048. {
  1049. xrp_release_dma_buf(task->instance->device,buffer->planes[i].fd,buffer->planes[i].buf_vir,buffer->planes[i].size,&status);
  1050. }
  1051. return -1;
  1052. case CSI_DSP_BUF_ALLOC_APP:
  1053. return 0;
  1054. default:
  1055. DSP_PRINT(WARNING,"buffer type not support\n");
  1056. return -1;
  1057. }
  1058. return 0;
  1059. }
  1060. int csi_dsp_request_set_property(struct csi_sw_task_req* req,void* property,size_t sz)
  1061. {
  1062. if(!req || !property || sz==0)
  1063. {
  1064. return -1;
  1065. }
  1066. csi_dsp_status_e status;
  1067. struct csi_dsp_task_handler * task=req->task;
  1068. struct xrp_buffer * buf=NULL;
  1069. void *sett_virt_addr =NULL;
  1070. struct xrp_buffer_group *buf_gp ;
  1071. buf_gp = (struct xrp_buffer_group *)req->priv;
  1072. req->sett_length = sz;
  1073. buf = xrp_create_buffer(task->instance->device,req->sett_length,NULL,&status);
  1074. if(status != XRP_STATUS_SUCCESS)
  1075. {
  1076. DSP_PRINT(WARNING,"create buffer failed\n");
  1077. return -1;
  1078. }
  1079. else
  1080. {
  1081. xrp_buffer_get_info(buf,XRP_BUFFER_USER_ADDR,&sett_virt_addr,sizeof(uint64_t),&status);
  1082. if(status != XRP_STATUS_SUCCESS)
  1083. {
  1084. DSP_PRINT(WARNING,"ERROR get virtual addr\n");
  1085. xrp_release_buffer(buf);
  1086. return -1;
  1087. }
  1088. xrp_buffer_get_info(buf,XRP_BUFFER_PHY_ADDR,&req->sett_ptr,sizeof(uint64_t),&status);
  1089. if(status != XRP_STATUS_SUCCESS)
  1090. {
  1091. DSP_PRINT(WARNING,"Get PHY_ADDR failed\n");
  1092. xrp_release_buffer(buf);
  1093. return -1;
  1094. }
  1095. xrp_add_buffer_to_group(buf_gp,buf,XRP_READ,&status);
  1096. if(status !=XRP_STATUS_SUCCESS)
  1097. {
  1098. DSP_PRINT(WARNING,"xrp_add_buffer_to_group failed\n");
  1099. xrp_release_buffer(buf);
  1100. return -1;
  1101. }
  1102. }
  1103. memcpy(sett_virt_addr,property,req->sett_length);
  1104. DSP_PRINT(DEBUG,"setting propert in %p succeful!\n",req->sett_ptr);
  1105. return 0;
  1106. }
  1107. int csi_dsp_request_enqueue(struct csi_sw_task_req* req)
  1108. {
  1109. struct csi_dsp_task_handler * task=NULL;
  1110. struct xrp_buffer * buf=NULL;
  1111. task_event_item_t *event_item;
  1112. csi_dsp_status_e status;
  1113. struct xrp_event *evt;
  1114. csi_dsp_sw_task_manager_t * sw_task_ctx;
  1115. enum xrp_status s;
  1116. int loop;
  1117. if(!req )
  1118. {
  1119. return -1;
  1120. }
  1121. task = (struct csi_dsp_task_handler *)req->task;
  1122. sw_task_ctx = (csi_dsp_sw_task_manager_t *)task->private;
  1123. event_item = malloc(sizeof(task_event_item_t));
  1124. if(event_item==NULL)
  1125. {
  1126. DSP_PRINT(WARNING,"malloc fail\n");
  1127. return -1;
  1128. }
  1129. // for(loop =0;loop<req->buffer_num;loop++)
  1130. // {
  1131. // csi_dsp_buf_flush(task->instance->device,&req->buffers[loop]);
  1132. // }
  1133. xrp_enqueue_command(task->queue, req, sizeof(struct csi_sw_task_req),
  1134. &event_item->req_status, sizeof(event_item->req_status),
  1135. req->priv, &evt, &s);
  1136. if (s != XRP_STATUS_SUCCESS) {
  1137. DSP_PRINT(WARNING,"enqueue task to dsp fail\n");
  1138. return -1;
  1139. }
  1140. req->status = CSI_DSP_SW_REQ_RUNNING;
  1141. event_item->event = evt;
  1142. event_item->req = req;
  1143. pthread_mutex_lock(&sw_task_ctx->mutex);
  1144. list_add_tail(&event_item->head,&sw_task_ctx->event_list);
  1145. sw_task_ctx->event_num++;
  1146. pthread_mutex_unlock(&sw_task_ctx->mutex);
  1147. DSP_PRINT(DEBUG,"Req %d is enqueue \n",req->request_id);
  1148. return 0;
  1149. }
  1150. struct csi_sw_task_req* csi_dsp_request_dequeue(void *task_ctx)
  1151. {
  1152. struct csi_dsp_task_handler * task=(struct csi_dsp_task_handler *)task_ctx;
  1153. csi_dsp_sw_task_manager_t * sw_task_ctx = (csi_dsp_sw_task_manager_t *)task->private;
  1154. struct csi_sw_task_req* req=NULL;
  1155. int id=0;
  1156. task_event_item_t *item;
  1157. enum xrp_status status;
  1158. int loop;
  1159. pthread_mutex_lock(&sw_task_ctx->mutex);
  1160. struct xrp_event **evts = malloc(sw_task_ctx->event_num*sizeof(struct xrp_event*));
  1161. list_for_each_entry(item,&sw_task_ctx->event_list,head){
  1162. evts[id++]=item->event;
  1163. }
  1164. pthread_mutex_unlock(&sw_task_ctx->mutex);
  1165. DSP_PRINT(DEBUG,"Wait for Req event \n");
  1166. id= xrp_wait_any(evts,sw_task_ctx->event_num,&status);
  1167. if(id>=sw_task_ctx->event_num || status !=XRP_STATUS_SUCCESS)
  1168. {
  1169. DSP_PRINT(WARNING,"id invalid:%d\n",id);
  1170. return NULL;
  1171. }
  1172. list_for_each_entry(item,&sw_task_ctx->event_list,head){
  1173. if(item->event == evts[id])
  1174. break;
  1175. }
  1176. pthread_mutex_lock(&sw_task_ctx->mutex);
  1177. list_del(&item->head);
  1178. sw_task_ctx->event_num--;
  1179. pthread_mutex_unlock(&sw_task_ctx->mutex);
  1180. req = item->req;
  1181. xrp_release_event(item->event);
  1182. if(item->req_status !=CSI_DSP_OK)
  1183. {
  1184. DSP_PRINT(WARNING,"req fail with resp:%d\n",item->req_status);
  1185. req->status = CSI_DSP_SW_REQ_FAIL;
  1186. }
  1187. else{
  1188. req->status = CSI_DSP_SW_REQ_DONE;
  1189. }
  1190. free(evts);
  1191. free(item);
  1192. // for(loop =0;loop<req->buffer_num;loop++)
  1193. // {
  1194. // csi_dsp_buf_flush(task->instance->device,&req->buffers[loop]);
  1195. // }
  1196. DSP_PRINT(DEBUG,"Req %d is deuque \n",req->request_id);
  1197. return req;
  1198. }
  1199. int csi_dsp_test_config(void* dsp ,struct csi_dsp_ip_test_par* config_para,void* buf)
  1200. {
  1201. struct csi_dsp_instance * instance = (struct csi_dsp_instance *)dsp;
  1202. if(!dsp || !buf || !config_para->result_buf_size)
  1203. {
  1204. DSP_PRINT(WARNING,"param check fail\n");
  1205. return -1;
  1206. }
  1207. if(csi_dsp_cmd_send(instance->comm_queue,PS_CMD_DSP_IP_TEST, config_para,sizeof(struct csi_dsp_ip_test_par),buf,config_para->result_buf_size,NULL))
  1208. {
  1209. DSP_PRINT(WARNING," send cmd fail\n");
  1210. return -1;
  1211. }
  1212. return 0;
  1213. }