csi_dsp_core.c 44 KB

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