ps_front_end_wrapper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 <stdlib.h>
  21. #include <string.h>
  22. #include <xtensa/corebits.h>
  23. #include <xtensa/xtruntime.h>
  24. #if HAVE_THREADS_XOS
  25. #include <xtensa/xos.h>
  26. #endif
  27. #include "dsp_ps_debug.h"
  28. #include "xrp_api.h"
  29. #include "xrp_dsp_hw.h"
  30. #include "xrp_dsp_user.h"
  31. #include "dsp_ps_ns.h"
  32. #include "csi_dsp_task_defs.h"
  33. #include "csi_dsp_post_process_defs.h"
  34. #include "vi_hw_api.h"
  35. #include "algo.h"
  36. #include "ps_sched.h"
  37. #include "idma_if.h"
  38. #define MAX_SW_BE 3
  39. static sw_be_handler_t g_sw_be_hands[MAX_SW_BE];
  40. int ps_fill_report(void *buffer,csi_dsp_report_e type,void*payload,size_t size)
  41. {
  42. csi_dsp_report_item_t *ptr = (csi_dsp_report_item_t *)buffer;
  43. if(buffer)
  44. {
  45. ptr->type=type;
  46. if(payload!=NULL&&size!=0)
  47. {
  48. memcpy(ptr->data,payload,size);
  49. }
  50. return DSP_REPORT_TO_HOST_FLAG;
  51. }
  52. return 0;
  53. }
  54. int csi_dsp_result_report(void* report_buf,void*result,size_t result_sz,void* extra,size_t extra_sz)
  55. {
  56. csi_dsp_report_item_t *ptr = (csi_dsp_report_item_t *)report_buf;
  57. size_t filled_sz=0;
  58. if(ptr && ((result_sz+extra_sz)<MAX_REPORT_SIZE))
  59. {
  60. ptr->type=0;
  61. if(result!=NULL&&result_sz!=0)
  62. {
  63. memcpy(ptr->data,result,result_sz);
  64. filled_sz +=result_sz;
  65. ptr->type=CSI_DSP_REPORT_RESULT;
  66. }
  67. if(extra!=NULL&&extra_sz!=0)
  68. {
  69. memcpy(ptr->data+filled_sz,extra,extra_sz);
  70. filled_sz +=result_sz;
  71. ptr->type = ptr->type==CSI_DSP_REPORT_RESULT?CSI_DSP_REPORT_RESULT_WITH_EXRA_PARAM:CSI_DSP_REPORT_EXRA_PARAM;
  72. }
  73. return 0;
  74. }
  75. else
  76. {
  77. ps_err("invalid param for report filled\n");
  78. return -1;
  79. }
  80. }
  81. static int get_isp_next_entry(int id, entry_struct_t *ctrl_info){
  82. Sisp_buffer isp_buffer_list[MAX_BUFFER_BLOCK];
  83. Sisp_buffer p_isp_buffer;
  84. if (isp_get_next_entry(isp_buffer_list) != EISP_BOOL_TRUE)
  85. {
  86. return -1;
  87. }
  88. if(isp_buffer_list[1].size!=0)
  89. {
  90. return -1;
  91. }
  92. ctrl_info->x = 0;
  93. ctrl_info->y =isp_get_poped_line_num();
  94. ctrl_info->height= isp_buffer_list[0].line_num;
  95. ctrl_info->fmt = isp_buffer_list[1].fmt;
  96. ctrl_info->frame_height = isp_get_frame_height();
  97. ctrl_info->pitch= isp_buffer_list[0].line_size;
  98. ctrl_info->pBuff =isp_buffer_list[0].start_addr;
  99. memset(&ctrl_info->meta,0x0,sizeof(csi_dsp_frame_meta_t));
  100. return 0;
  101. }
  102. static int get_post_isp_next_entry(int id, struct csi_dsp_buffer * buf_entry)
  103. {
  104. Sisp_buffer p_isp_buffer;
  105. if (post_isp_get_next_entry(&p_isp_buffer) != EHW_OK)
  106. {
  107. return -1;
  108. }
  109. // ctrl_info->frame=NULL;
  110. // ctrl_info->x =0;
  111. // ctrl_info->y= isp_get_pushed_line_num();
  112. // ctrl_info->pitch = p_isp_buffer.line_size;
  113. // ctrl_info->pBuff = p_isp_buffer.start_addr;
  114. // ctrl_info->height =p_isp_buffer.line_num;
  115. // buf_entry->width = p_isp_buffer.
  116. buf_entry->height = p_isp_buffer.line_num;
  117. buf_entry->format = p_isp_buffer.fmt;
  118. buf_entry->plane_count=1;
  119. buf_entry->planes[0].buf_phy= p_isp_buffer.start_addr;
  120. buf_entry->planes[0].stride = p_isp_buffer.line_size;
  121. buf_entry->planes[0].size = p_isp_buffer.line_size*p_isp_buffer.line_num;
  122. return 0;
  123. }
  124. static inline int isp_entry_done(int id ,entry_struct_t *ctrl_info)
  125. {
  126. return isp_pop_n_line(ctrl_info->height);
  127. }
  128. enum xrp_status ps_isp_config_message_handler(void* in_data,void* context)
  129. {
  130. sisp_config_par *config = (sisp_config_par *)in_data;
  131. if(isp_initialize(config->id))
  132. {
  133. return XRP_STATUS_FAILURE;
  134. }
  135. if(isp_handler_config(config->hor,config->ver, config->data_fmt, config->line_in_entry,
  136. (uint64_t)config->buffer_addr, config->buffer_size,config->line_stride))
  137. {
  138. return XRP_STATUS_FAILURE;
  139. }
  140. ((dsp_fe_handler_t*)context)->type = CSI_DSP_FE_TYPE_ISP;
  141. ((dsp_fe_handler_t*)context)->id = config->id;
  142. ((dsp_fe_handler_t*)context)->get_next_entry = get_isp_next_entry;
  143. ((dsp_fe_handler_t*)context)->get_state = isp_get_status;
  144. ((dsp_fe_handler_t*)context)->entry_done = isp_entry_done;
  145. ((dsp_fe_handler_t*)context)->update_state= isp_recovery_state_update;
  146. return XRP_STATUS_SUCCESS;
  147. }
  148. static int get_vipre_next_entry(int id, entry_struct_t *ctrl_info){
  149. Svipre_buffer buffer_list;
  150. frame_meta_t * vipre_fmeta;
  151. if (vipre_get_next_entry(id,&buffer_list)!= EISP_BOOL_TRUE)
  152. {
  153. return -1;
  154. }
  155. ctrl_info->frame_height = vipre_get_frame_height();
  156. ctrl_info->height= buffer_list.line_num;
  157. ctrl_info->pitch= buffer_list.line_size;
  158. ctrl_info->pBuff =buffer_list.start_addr[0];
  159. if(ctrl_info->frame_height == ctrl_info->height)
  160. {
  161. ctrl_info->x = -1;
  162. ctrl_info->y = -1;
  163. }
  164. else
  165. {
  166. /*TODO VIPRE N LINE¡¡£Í£Ï£Ä£Å*/
  167. }
  168. vipre_fmeta = vipre_get_frame_meta();
  169. if(vipre_fmeta)
  170. {
  171. ctrl_info->meta.frame_id = vipre_fmeta->frame_cnt;
  172. ctrl_info->meta.temp_projector = vipre_fmeta->projection_temperature;
  173. ctrl_info->meta.temp_sensor = vipre_fmeta->floodlight_temperature;
  174. ctrl_info->meta.timestap = vipre_fmeta->frame_ts_us;
  175. }
  176. else
  177. {
  178. memset(&ctrl_info->meta,0x0,sizeof(csi_dsp_frame_meta_t));
  179. }
  180. return 0;
  181. }
  182. enum xrp_status ps_vipre_config_message_handler(void* in_data,void* out_data)
  183. {
  184. vipre_config_par_t *config = (vipre_config_par_t *)in_data;
  185. int ret =0;
  186. if(vipre_initialize())
  187. {
  188. return XRP_STATUS_FAILURE;
  189. }
  190. if(config->line_num ==0)
  191. {
  192. // M frame mode
  193. ret =vipre_frame_mode_handler_config(config->act_channel,config->hor,config->ver,config->line_stride, config->data_fmt ,
  194. config->buffer_num,config->buffer_addr);
  195. }
  196. else
  197. {
  198. //N line mode
  199. ret =vipre_handler_nline_config(config->act_channel,config->hor,config->ver,config->line_stride, config->data_fmt ,
  200. config->line_num,config->buffer_addr, config->buffer_size);
  201. }
  202. if(ret)
  203. {
  204. return XRP_STATUS_FAILURE;
  205. }
  206. ((dsp_fe_handler_t*)out_data)->id = config->id;
  207. ((dsp_fe_handler_t*)out_data)->type = CSI_DSP_FE_TYPE_VIPRE;
  208. ((dsp_fe_handler_t*)out_data)->get_next_entry = get_vipre_next_entry;
  209. ((dsp_fe_handler_t*)out_data)->get_state = vipre_check_exeptional;
  210. ((dsp_fe_handler_t*)out_data)->entry_done = vipre_pop_new_entry;
  211. ((dsp_be_handler_t*)out_data)->update_state = vipre_recovery_state_update;
  212. return XRP_STATUS_SUCCESS;
  213. }
  214. static inline int post_isp_entry_start(int id ,struct csi_dsp_buffer * buf_entry)
  215. {
  216. return post_isp_push_entry(buf_entry->height);
  217. }
  218. enum xrp_status ps_post_isp_config_message_handler(void* in_data,void* out_data)
  219. {
  220. sisp_config_par *config = (sisp_config_par *)in_data;
  221. if(post_isp_initialize(config->id))
  222. {
  223. return XRP_STATUS_FAILURE;
  224. }
  225. if(post_isp_handler_config(config->hor,config->ver, config->data_fmt, config->line_in_entry,
  226. (uint64_t)config->buffer_addr, config->buffer_size,config->line_stride))
  227. {
  228. return XRP_STATUS_FAILURE;
  229. }
  230. ((dsp_be_handler_t*)out_data)->type = CSI_DSP_BE_TYPE_POST_ISP;
  231. ((dsp_be_handler_t*)out_data)->get_next_entry = get_post_isp_next_entry;
  232. ((dsp_be_handler_t*)out_data)->get_state = post_isp_get_status;
  233. ((dsp_be_handler_t*)out_data)->entry_start = post_isp_entry_start;
  234. ((dsp_be_handler_t*)out_data)->update_state = post_isp_recovery_state_update;
  235. return XRP_STATUS_SUCCESS;
  236. }
  237. void sw_be_init()
  238. {
  239. int id;
  240. for(id=0;id<MAX_SW_BE;id++)
  241. {
  242. g_sw_be_hands[id].status = DSP_BUF_STATUS_FREE;
  243. }
  244. }
  245. static int get_sw_be_next_entry(int id, struct csi_dsp_buffer * buf_entry)
  246. {
  247. if(g_sw_be_hands[id].status != DSP_BUF_STATUS_ALLOCTED)
  248. {
  249. return -1;
  250. }
  251. if(g_sw_be_hands[id].valid_buf_num <=0)
  252. {
  253. return -1;
  254. }
  255. int buf_id = g_sw_be_hands[id].head_id;
  256. memcpy(buf_entry,&g_sw_be_hands[id].bufs[buf_id],sizeof(struct csi_dsp_buffer));
  257. return 0;
  258. }
  259. int sw_be_get_exeptional(int id)
  260. {
  261. return g_sw_be_hands[id].status ;
  262. }
  263. enum xrp_status sw_be_asgin_buf(int id ,sw_be_config_par* cfg)
  264. {
  265. int i,buf_id;
  266. if((g_sw_be_hands[id].valid_buf_num+cfg->num_buf) >g_sw_be_hands[id].buf_num)
  267. {
  268. return XRP_STATUS_FAILURE;
  269. }
  270. buf_id = g_sw_be_hands[id].head_id+g_sw_be_hands[id].valid_buf_num;
  271. for(i=0;i<cfg->num_buf;i++)
  272. {
  273. memcpy(&g_sw_be_hands[id].bufs[(i+buf_id)%g_sw_be_hands[id].buf_num],&cfg->bufs[i],sizeof(struct csi_dsp_buffer));
  274. }
  275. g_sw_be_hands[id].valid_buf_num+=cfg->num_buf;
  276. if( g_sw_be_hands[id].status == DSP_BUF_STATUS_NO_MEMORY && g_sw_be_hands[id].valid_buf_num>1)
  277. {
  278. g_sw_be_hands[id].status = DSP_BUF_STATUS_ALLOCTED;
  279. }
  280. return XRP_STATUS_SUCCESS;
  281. }
  282. static inline int sw_be_entry_start(int id ,void *extra_param)
  283. {
  284. int buf_id = g_sw_be_hands[id].head_id;
  285. if(g_sw_be_hands[id].report ==NULL)
  286. {
  287. g_sw_be_hands[id].status = DSP_BUF_STATUS_INVALID;
  288. return -1;
  289. }
  290. if(g_sw_be_hands[id].status != DSP_BUF_STATUS_ALLOCTED)
  291. {
  292. return -1;
  293. }
  294. if(g_sw_be_hands[id].valid_buf_num <=1)
  295. {
  296. g_sw_be_hands[id].status = DSP_BUF_STATUS_NO_MEMORY;
  297. ps_fill_report(g_sw_be_hands[id].report,CSI_DSP_REPORT_NO_BUF,NULL,0);
  298. return -1;
  299. }
  300. if(buf_id>=g_sw_be_hands[id].buf_num)
  301. {
  302. g_sw_be_hands[id].status = DSP_BUF_STATUS_INVALID;
  303. return -1;
  304. }
  305. if(ps_report_avaiable() <0)
  306. {
  307. ps_fill_report(g_sw_be_hands[id].report,CSI_DSP_HW_FRAME_DROP,NULL,0);
  308. return -1;
  309. }
  310. // ps_fill_report(g_sw_be_hands[id].report,CSI_DSP_REPORT_RESULT,&g_sw_be_hands[id].bufs[buf_id],sizeof(struct csi_dsp_buffer));
  311. g_sw_be_hands[id].head_id = (g_sw_be_hands[id].head_id+1)%g_sw_be_hands[id].buf_num;
  312. g_sw_be_hands[id].valid_buf_num--;
  313. return 0;
  314. }
  315. void sw_recovery_state_update(int id )
  316. {
  317. switch(g_sw_be_hands[id].status)
  318. {
  319. case EHW_RECOVERYING:
  320. g_sw_be_hands[id].status = EHW_RECOVERY_DONE;
  321. break;
  322. case EHW_ERROR_OVER_FLOW:
  323. g_sw_be_hands[id].status = EHW_RECOVERY_DONE;
  324. break;
  325. case EHW_RECOVERY_DONE:
  326. g_sw_be_hands[id].status = EHW_OK;
  327. break;
  328. default:
  329. g_sw_be_hands[id].status = EHW_RECOVERY_DONE;
  330. break;
  331. }
  332. ps_debug("update sw state:%d",g_sw_be_hands[id].status);
  333. }
  334. enum xrp_status ps_sw_task_out_config_message_handler(void* in_data,void* out_data,void * report_buf,uint32_t report_buf_size)
  335. {
  336. sw_be_config_par* cfg = (sw_be_config_par* )in_data;
  337. int id;
  338. for(id=0;id<MAX_SW_BE;id++)
  339. {
  340. if(g_sw_be_hands[id].status ==DSP_BUF_STATUS_FREE)
  341. {
  342. g_sw_be_hands[id].bufs = malloc(cfg->num_buf*sizeof(struct csi_dsp_buffer));
  343. if(g_sw_be_hands[id].bufs == NULL)
  344. {
  345. return XRP_STATUS_FAILURE;
  346. }
  347. g_sw_be_hands[id].id = id;
  348. g_sw_be_hands[id].buf_num = cfg->num_buf;
  349. g_sw_be_hands[id].head_id = 0;
  350. g_sw_be_hands[id].valid_buf_num = g_sw_be_hands[id].buf_num;
  351. g_sw_be_hands[id].report = report_buf;
  352. g_sw_be_hands[id].report_size = report_buf_size;
  353. g_sw_be_hands[id].status =DSP_BUF_STATUS_ALLOCTED;
  354. memcpy(g_sw_be_hands[id].bufs,cfg->bufs,sizeof(struct csi_dsp_buffer)*cfg->num_buf);
  355. ((dsp_be_handler_t*)out_data)->id = id;
  356. ((dsp_be_handler_t*)out_data)->type = CSI_DSP_BE_TYPE_HOST;
  357. ((dsp_be_handler_t*)out_data)->get_next_entry = get_sw_be_next_entry;
  358. ((dsp_be_handler_t*)out_data)->get_state = sw_be_get_exeptional;
  359. ((dsp_be_handler_t*)out_data)->entry_start = sw_be_entry_start;
  360. ((dsp_be_handler_t*)out_data)->update_state =sw_recovery_state_update;
  361. return XRP_STATUS_SUCCESS;
  362. }
  363. }
  364. return XRP_STATUS_FAILURE;
  365. }
  366. int ps_get_isp_irq(uint16_t id)
  367. {
  368. return isp_get_irq_num(id);
  369. }
  370. int ps_get_vipre_irq()
  371. {
  372. return vipre_get_irq_num();
  373. }
  374. bool ps_is_vipre_task_empty()
  375. {
  376. return vipre_is_empty();
  377. }