csi_dsp_core.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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,
  823. req->buffers[buf_idx].planes[plane_idx].buf_vir,
  824. req->buffers[buf_idx].planes[plane_idx].size,&status);
  825. }
  826. }
  827. else
  828. {
  829. for(plane_idx =0 ;plane_idx<req->buffers[buf_idx].plane_count;plane_idx++)
  830. {
  831. // printf("release buf %d plane %d\n",buf_idx,plane_idx);
  832. buffer = xrp_get_buffer_from_group(group,req->buffers[buf_idx].planes[plane_idx].fd,&status);
  833. xrp_unmap_buffer(buffer,(void *)req->buffers[buf_idx].planes[plane_idx].buf_vir,&status);
  834. xrp_release_buffer(buffer);
  835. }
  836. }
  837. }
  838. /*free sett buf */
  839. xrp_buffer_group_get_info(group,XRP_BUFFER_GROUP_SIZE_SIZE_T, 0,&buf_num,sizeof(buf_num),&status);
  840. if(status!=XRP_STATUS_SUCCESS)
  841. {
  842. DSP_PRINT(WARNING,"Get sett buffer release fail\n");
  843. return -1;
  844. }
  845. uint64_t buf_phy;
  846. for(index=0; index<buf_num; index++)
  847. {
  848. buffer = xrp_get_buffer_from_group(group,index,&status);
  849. xrp_buffer_get_info(buffer,XRP_BUFFER_PHY_ADDR,&buf_phy,sizeof(buf_phy),&status);
  850. if(buf_phy == req->sett_ptr)
  851. {
  852. // xrp_unmap_buffer(buffer,(void *)req->buffers[buf_idx].planes[plane_idx].buf_vir,&status);
  853. xrp_release_buffer(buffer);
  854. break;
  855. }
  856. }
  857. xrp_release_buffer_group(group);
  858. DSP_PRINT(DEBUG,"req %d is release successful!\n",req->request_id);
  859. free(req);
  860. return 0;
  861. }
  862. int csi_dsp_task_create_buffer(void * task_ctx,struct csi_dsp_buffer * buffer)
  863. {
  864. struct csi_dsp_task_handler * task= (struct csi_dsp_task_handler *)task_ctx;
  865. int i,j;
  866. struct xrp_buffer * buf=NULL;
  867. size_t buf_size;
  868. int fail_release =0;
  869. uint64_t phy_addr;
  870. enum xrp_status status;
  871. if(task == NULL || buffer==NULL)
  872. {
  873. DSP_PRINT(WARNING,"param check fail\n");
  874. return -1;
  875. }
  876. if(task->buffers==NULL)
  877. {
  878. DSP_PRINT(WARNING,"Buffrs not init\n");
  879. return -1;
  880. }
  881. switch(buffer->type)
  882. {
  883. case CSI_DSP_BUF_ALLOC_DRV:
  884. for(i=0;i<buffer->plane_count;i++)
  885. {
  886. buf = xrp_create_buffer(task->instance->device,buffer->planes[i].size,NULL,&status);
  887. if(status != XRP_STATUS_SUCCESS)
  888. {
  889. DSP_PRINT(WARNING,"create xrp buffer fail\n");
  890. goto err_1;
  891. }
  892. else
  893. {
  894. int flag = buffer->dir == CSI_DSP_BUFFER_IN ?XRP_READ:XRP_WRITE;
  895. buffer->planes[i].buf_vir = xrp_map_buffer(buf,0,buffer->planes[i].size,flag,&status);
  896. if(status != XRP_STATUS_SUCCESS)
  897. {
  898. xrp_release_buffer(buf);
  899. DSP_PRINT(WARNING,"Error Map Buffrs not fail\n");
  900. goto err_1;
  901. }
  902. // printf("%s,Debug V:%llx,P:%llx\n",__FUNCTION__,buf->ptr,buf->phy_addr);
  903. xrp_buffer_get_info(buf,XRP_BUFFER_PHY_ADDR,&buffer->planes[i].buf_phy,sizeof(phy_addr),&status);
  904. if(status != XRP_STATUS_SUCCESS)
  905. {
  906. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  907. xrp_release_buffer(buf);
  908. DSP_PRINT(WARNING,"Error get phy addr fail\n");
  909. goto err_1;
  910. }
  911. buffer->planes[i].fd = xrp_add_buffer_to_group(task->buffers,buf,flag,&status);
  912. if(status !=XRP_STATUS_SUCCESS)
  913. {
  914. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  915. xrp_release_buffer(buf);
  916. DSP_PRINT(WARNING,"add to buffer group fail\n");
  917. goto err_1;
  918. }
  919. }
  920. DSP_PRINT(DEBUG,"create buffer:Vaddr(0x%llx),Paddr(0x%llx)\n", buffer->planes[i].buf_vir,buffer->planes[i].buf_phy);
  921. }
  922. return 0;
  923. err_1:
  924. for(j=0;j<i;j++)
  925. {
  926. buf = xrp_get_buffer_from_group(task->buffers, buffer->planes[j].fd,&status);
  927. xrp_unmap_buffer(buf,buffer->planes[j].buf_vir,&status);
  928. xrp_release_buffer(buf);
  929. }
  930. return -1;
  931. case CSI_DSP_BUF_ALLOC_APP:
  932. return 0;
  933. case CSI_DSP_BUF_TYPE_DMA_BUF_EXPORT:
  934. DSP_PRINT(INFO,"ERR DMA buffer export not support\n");
  935. return 0;
  936. case CSI_DSP_BUF_TYPE_DMA_BUF_IMPORT:
  937. for(i=0;i<buffer->plane_count;i++)
  938. {
  939. int flag = buffer->dir == CSI_DSP_BUFFER_IN ?XRP_READ:XRP_WRITE;
  940. xrp_import_dma_buf(task->instance->device,buffer->planes[i].fd,flag,&buffer->planes[i].buf_phy,
  941. &buffer->planes[i].buf_vir,&buffer->planes[i].size,&status);
  942. if(status !=XRP_STATUS_SUCCESS )
  943. {
  944. DSP_PRINT(WARNING,"dma buf import fail\n");
  945. goto err_2;
  946. }
  947. }
  948. break;
  949. err_2:
  950. for(j=0;j<i;j++)
  951. {
  952. xrp_release_dma_buf(task->instance->device,buffer->planes[i].fd,buffer->planes[i].buf_vir,buffer->planes[i].size,&status);
  953. }
  954. return -1;
  955. default:
  956. DSP_PRINT(WARNING,"buffer type:%d not support\n",buffer->type);
  957. return -1;
  958. }
  959. return 0;
  960. }
  961. int csi_dsp_task_free_buffer(void * task_ctx,struct csi_dsp_buffer * buffer)
  962. {
  963. struct csi_dsp_task_handler * task= (struct csi_dsp_task_handler *)task_ctx;
  964. int i,j;
  965. struct xrp_buffer * buf=NULL;
  966. size_t buf_size;
  967. int fail_release =0;
  968. uint64_t phy_addr;
  969. enum xrp_status status;
  970. if(task == NULL||task->buffers==NULL||buffer==NULL)
  971. {
  972. DSP_PRINT(WARNING,"param check fail\n")
  973. return -1;
  974. }
  975. switch(buffer->type)
  976. {
  977. case CSI_DSP_BUF_ALLOC_DRV:
  978. for(i=0;i<buffer->plane_count;i++)
  979. {
  980. buf = xrp_get_buffer_from_group(task->buffers,buffer->planes[i].fd,&status);
  981. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  982. xrp_release_buffer(buf);
  983. }
  984. break;
  985. case CSI_DSP_BUF_ALLOC_APP:
  986. break;
  987. case CSI_DSP_BUF_TYPE_DMA_BUF_EXPORT:
  988. DSP_PRINT(WARNING,"buffer type not support\n");
  989. break;
  990. case CSI_DSP_BUF_TYPE_DMA_BUF_IMPORT:
  991. for(i=0;i<buffer->plane_count;i++)
  992. {
  993. xrp_release_dma_buf(task->instance->device,buffer->planes[i].fd,buffer->planes[i].buf_vir,buffer->planes[i].size,&status);
  994. if(status != XRP_STATUS_SUCCESS)
  995. {
  996. DSP_PRINT(WARNING,"ERR DMA Buffrs(%d) Release fail\n",buffer->planes[i].fd);
  997. return -1;
  998. }
  999. }
  1000. break;
  1001. default:
  1002. DSP_PRINT(WARNING,"buffer type:%d not support\n",buffer->type);
  1003. return -1;
  1004. }
  1005. return 0;
  1006. }
  1007. int csi_dsp_request_add_buffer(struct csi_sw_task_req* req,struct csi_dsp_buffer * buffer)
  1008. {
  1009. struct csi_dsp_task_handler * task=NULL;
  1010. struct xrp_buffer * buf=NULL;
  1011. enum xrp_status status;
  1012. int i,j;
  1013. struct xrp_buffer_group *buf_gp ;
  1014. if(!req )
  1015. {
  1016. return -1;
  1017. }
  1018. buf_gp = (struct xrp_buffer_group *)req->priv;
  1019. task = req->task;
  1020. int flag = buffer->dir == CSI_DSP_BUFFER_IN ?XRP_READ:XRP_WRITE;
  1021. switch(buffer->type)
  1022. {
  1023. case CSI_DSP_BUF_ALLOC_DRV:
  1024. for(i=0;i<buffer->plane_count;i++)
  1025. {
  1026. buf = xrp_create_buffer(task->instance->device,buffer->planes[i].size,NULL,&status);
  1027. if(status != XRP_STATUS_SUCCESS)
  1028. {
  1029. DSP_PRINT(WARNING,"create buffer failed\n");
  1030. goto err_1;
  1031. }
  1032. else
  1033. {
  1034. buffer->planes[i].buf_vir = xrp_map_buffer(buf,0,buffer->planes[i].size,flag,&status);
  1035. if(status != XRP_STATUS_SUCCESS)
  1036. {
  1037. DSP_PRINT(WARNING,"Map Buffrs not init\n");
  1038. xrp_release_buffer(buf);
  1039. goto err_1;
  1040. }
  1041. xrp_buffer_get_info(buf,XRP_BUFFER_PHY_ADDR,&buffer->planes[i].buf_phy,sizeof(uint64_t),&status);
  1042. if(status != XRP_STATUS_SUCCESS)
  1043. {
  1044. DSP_PRINT(WARNING,"xrp_buffer_get_info failed\n");
  1045. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  1046. xrp_release_buffer(buf);
  1047. goto err_1;
  1048. }
  1049. buffer->planes[i].fd = xrp_add_buffer_to_group(buf_gp,buf,flag,&status);
  1050. if(status !=XRP_STATUS_SUCCESS)
  1051. {
  1052. DSP_PRINT(WARNING,"xrp_add_buffer_to_group failed\n");
  1053. xrp_unmap_buffer(buf,buffer->planes[i].buf_vir,&status);
  1054. xrp_release_buffer(buf);
  1055. goto err_1;
  1056. }
  1057. }
  1058. DSP_PRINT(DEBUG,"create buffer:Vaddr(0x%llx),Paddr(0x%llx)\n", buffer->planes[i].buf_vir,buffer->planes[i].buf_phy);
  1059. }
  1060. memcpy(&req->buffers[req->buffer_num++],buffer,sizeof(*buffer));
  1061. return 0;
  1062. err_1:
  1063. for(j=0;j<i;j++)
  1064. {
  1065. buf = xrp_get_buffer_from_group(buf_gp,buffer->planes[j].fd,&status);
  1066. xrp_unmap_buffer(buf,buffer->planes[j].buf_vir,&status);
  1067. xrp_release_buffer(buf);
  1068. }
  1069. return -1;
  1070. case CSI_DSP_BUF_TYPE_DMA_BUF_EXPORT:
  1071. DSP_PRINT(INFO,"ERR DMA buffer export not support\n");
  1072. return -1;
  1073. case CSI_DSP_BUF_TYPE_DMA_BUF_IMPORT:
  1074. for(i=0;i<buffer->plane_count;i++)
  1075. {
  1076. xrp_import_dma_buf(task->instance->device,buffer->planes[i].fd,flag,&buffer->planes[i].buf_phy,
  1077. & buffer->planes[i].buf_vir,&buffer->planes[i].size,&status);
  1078. if(status !=XRP_STATUS_SUCCESS )
  1079. {
  1080. DSP_PRINT(WARNING,"dma buf import fail\n");
  1081. goto err_2;
  1082. }
  1083. }
  1084. memcpy(&req->buffers[req->buffer_num++],buffer,sizeof(*buffer));
  1085. break;
  1086. err_2:
  1087. for(j=0;j<i;j++)
  1088. {
  1089. xrp_release_dma_buf(task->instance->device,buffer->planes[i].fd,buffer->planes[i].buf_vir,buffer->planes[i].size,&status);
  1090. }
  1091. return -1;
  1092. case CSI_DSP_BUF_ALLOC_APP:
  1093. return 0;
  1094. default:
  1095. DSP_PRINT(WARNING,"buffer type not support\n");
  1096. return -1;
  1097. }
  1098. return 0;
  1099. }
  1100. int csi_dsp_request_set_property(struct csi_sw_task_req* req,void* property,size_t sz)
  1101. {
  1102. if(!req || !property || sz==0)
  1103. {
  1104. return -1;
  1105. }
  1106. csi_dsp_status_e status;
  1107. struct csi_dsp_task_handler * task=req->task;
  1108. struct xrp_buffer * buf=NULL;
  1109. void *sett_virt_addr =NULL;
  1110. struct xrp_buffer_group *buf_gp ;
  1111. buf_gp = (struct xrp_buffer_group *)req->priv;
  1112. req->sett_length = sz;
  1113. buf = xrp_create_buffer(task->instance->device,req->sett_length,NULL,&status);
  1114. if(status != XRP_STATUS_SUCCESS)
  1115. {
  1116. DSP_PRINT(WARNING,"create buffer failed\n");
  1117. return -1;
  1118. }
  1119. else
  1120. {
  1121. xrp_buffer_get_info(buf,XRP_BUFFER_USER_ADDR,&sett_virt_addr,sizeof(uint64_t),&status);
  1122. if(status != XRP_STATUS_SUCCESS)
  1123. {
  1124. DSP_PRINT(WARNING,"ERROR get virtual addr\n");
  1125. xrp_release_buffer(buf);
  1126. return -1;
  1127. }
  1128. xrp_buffer_get_info(buf,XRP_BUFFER_PHY_ADDR,&req->sett_ptr,sizeof(uint64_t),&status);
  1129. if(status != XRP_STATUS_SUCCESS)
  1130. {
  1131. DSP_PRINT(WARNING,"Get PHY_ADDR failed\n");
  1132. xrp_release_buffer(buf);
  1133. return -1;
  1134. }
  1135. xrp_add_buffer_to_group(buf_gp,buf,XRP_READ,&status);
  1136. if(status !=XRP_STATUS_SUCCESS)
  1137. {
  1138. DSP_PRINT(WARNING,"xrp_add_buffer_to_group failed\n");
  1139. xrp_release_buffer(buf);
  1140. return -1;
  1141. }
  1142. }
  1143. memcpy(sett_virt_addr,property,req->sett_length);
  1144. DSP_PRINT(DEBUG,"setting propert in %p succeful!\n",req->sett_ptr);
  1145. return 0;
  1146. }
  1147. int csi_dsp_request_enqueue(struct csi_sw_task_req* req)
  1148. {
  1149. struct csi_dsp_task_handler * task=NULL;
  1150. struct xrp_buffer * buf=NULL;
  1151. task_event_item_t *event_item;
  1152. csi_dsp_status_e status;
  1153. struct xrp_event *evt;
  1154. csi_dsp_sw_task_manager_t * sw_task_ctx;
  1155. enum xrp_status s;
  1156. int loop;
  1157. if(!req )
  1158. {
  1159. return -1;
  1160. }
  1161. task = (struct csi_dsp_task_handler *)req->task;
  1162. sw_task_ctx = (csi_dsp_sw_task_manager_t *)task->private;
  1163. event_item = malloc(sizeof(task_event_item_t));
  1164. if(event_item==NULL)
  1165. {
  1166. DSP_PRINT(WARNING,"malloc fail\n");
  1167. return -1;
  1168. }
  1169. // for(loop =0;loop<req->buffer_num;loop++)
  1170. // {
  1171. // csi_dsp_buf_flush(task->instance->device,&req->buffers[loop]);
  1172. // }
  1173. xrp_enqueue_command(task->queue, req, sizeof(struct csi_sw_task_req),
  1174. &event_item->req_status, sizeof(event_item->req_status),
  1175. req->priv, &evt, &s);
  1176. if (s != XRP_STATUS_SUCCESS) {
  1177. DSP_PRINT(WARNING,"enqueue task to dsp fail\n");
  1178. return -1;
  1179. }
  1180. req->status = CSI_DSP_SW_REQ_RUNNING;
  1181. event_item->event = evt;
  1182. event_item->req = req;
  1183. pthread_mutex_lock(&sw_task_ctx->mutex);
  1184. list_add_tail(&event_item->head,&sw_task_ctx->event_list);
  1185. sw_task_ctx->event_num++;
  1186. pthread_mutex_unlock(&sw_task_ctx->mutex);
  1187. DSP_PRINT(DEBUG,"Req %d is enqueue \n",req->request_id);
  1188. return 0;
  1189. }
  1190. struct csi_sw_task_req* csi_dsp_request_dequeue(void *task_ctx)
  1191. {
  1192. struct csi_dsp_task_handler * task=(struct csi_dsp_task_handler *)task_ctx;
  1193. csi_dsp_sw_task_manager_t * sw_task_ctx = (csi_dsp_sw_task_manager_t *)task->private;
  1194. struct csi_sw_task_req* req=NULL;
  1195. int id=0;
  1196. task_event_item_t *item;
  1197. enum xrp_status status;
  1198. int loop;
  1199. pthread_mutex_lock(&sw_task_ctx->mutex);
  1200. struct xrp_event **evts = malloc(sw_task_ctx->event_num*sizeof(struct xrp_event*));
  1201. list_for_each_entry(item,&sw_task_ctx->event_list,head){
  1202. evts[id++]=item->event;
  1203. }
  1204. pthread_mutex_unlock(&sw_task_ctx->mutex);
  1205. DSP_PRINT(DEBUG,"Wait for Req event \n");
  1206. id= xrp_wait_any(evts,sw_task_ctx->event_num,&status);
  1207. if(id>=sw_task_ctx->event_num || status !=XRP_STATUS_SUCCESS)
  1208. {
  1209. DSP_PRINT(WARNING,"id invalid:%d\n",id);
  1210. return NULL;
  1211. }
  1212. list_for_each_entry(item,&sw_task_ctx->event_list,head){
  1213. if(item->event == evts[id])
  1214. break;
  1215. }
  1216. pthread_mutex_lock(&sw_task_ctx->mutex);
  1217. list_del(&item->head);
  1218. sw_task_ctx->event_num--;
  1219. pthread_mutex_unlock(&sw_task_ctx->mutex);
  1220. req = item->req;
  1221. xrp_release_event(item->event);
  1222. if(item->req_status !=CSI_DSP_OK)
  1223. {
  1224. DSP_PRINT(WARNING,"req fail with resp:%d\n",item->req_status);
  1225. req->status = CSI_DSP_SW_REQ_FAIL;
  1226. }
  1227. else{
  1228. req->status = CSI_DSP_SW_REQ_DONE;
  1229. }
  1230. free(evts);
  1231. free(item);
  1232. // for(loop =0;loop<req->buffer_num;loop++)
  1233. // {
  1234. // csi_dsp_buf_flush(task->instance->device,&req->buffers[loop]);
  1235. // }
  1236. DSP_PRINT(DEBUG,"Req %d is deuque \n",req->request_id);
  1237. return req;
  1238. }
  1239. int csi_dsp_test_config(void* dsp ,struct csi_dsp_ip_test_par* config_para,void* buf)
  1240. {
  1241. struct csi_dsp_instance * instance = (struct csi_dsp_instance *)dsp;
  1242. if(!dsp || !buf || !config_para->result_buf_size)
  1243. {
  1244. DSP_PRINT(WARNING,"param check fail\n");
  1245. return -1;
  1246. }
  1247. 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))
  1248. {
  1249. DSP_PRINT(WARNING," send cmd fail\n");
  1250. return -1;
  1251. }
  1252. return 0;
  1253. }