venus_hfi_response.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include "hfi_packet.h"
  6. #include "venus_hfi.h"
  7. #include "venus_hfi_response.h"
  8. #include "msm_vidc_debug.h"
  9. #include "msm_vidc_driver.h"
  10. #include "msm_vdec.h"
  11. extern struct msm_vidc_core *g_core;
  12. void print_psc_properties(u32 tag, const char *str, struct msm_vidc_inst *inst,
  13. struct msm_vidc_subscription_params subsc_params)
  14. {
  15. if (!(tag & msm_vidc_debug) || !inst)
  16. return;
  17. dprintk(tag, inst->sid,
  18. "%s: resolution %#x, crop offsets %#llx, bit depth %d, coded frames %d "
  19. "fw min count %d, poc %d, color info %d, profile %d, level %d, tier %d ",
  20. str, subsc_params.bitstream_resolution, subsc_params.crop_offsets,
  21. subsc_params.bit_depth, subsc_params.coded_frames,
  22. subsc_params.fw_min_count, subsc_params.pic_order_cnt,
  23. subsc_params.color_info, subsc_params.profile, subsc_params.level,
  24. subsc_params.tier);
  25. }
  26. static void print_sfr_message(struct msm_vidc_core *core)
  27. {
  28. struct msm_vidc_sfr *vsfr = NULL;
  29. u32 vsfr_size = 0;
  30. void *p = NULL;
  31. vsfr = (struct msm_vidc_sfr *)core->sfr.align_virtual_addr;
  32. if (vsfr) {
  33. if (vsfr->bufSize != core->sfr.mem_size) {
  34. d_vpr_e("Invalid SFR buf size %d actual %d\n",
  35. vsfr->bufSize, core->sfr.mem_size);
  36. return;
  37. }
  38. vsfr_size = vsfr->bufSize - sizeof(u32);
  39. p = memchr(vsfr->rg_data, '\0', vsfr_size);
  40. /* SFR isn't guaranteed to be NULL terminated */
  41. if (p == NULL)
  42. vsfr->rg_data[vsfr_size - 1] = '\0';
  43. d_vpr_e("SFR Message from FW: %s\n", vsfr->rg_data);
  44. }
  45. }
  46. u32 vidc_port_from_hfi(struct msm_vidc_inst *inst,
  47. enum hfi_packet_port_type hfi_port)
  48. {
  49. enum msm_vidc_port_type port = MAX_PORT;
  50. if (is_decode_session(inst)) {
  51. switch (hfi_port) {
  52. case HFI_PORT_BITSTREAM:
  53. port = INPUT_PORT;
  54. break;
  55. case HFI_PORT_RAW:
  56. port = OUTPUT_PORT;
  57. break;
  58. default:
  59. i_vpr_e(inst, "%s: invalid hfi port type %d\n",
  60. __func__, hfi_port);
  61. break;
  62. }
  63. } else if (is_encode_session(inst)) {
  64. switch (hfi_port) {
  65. case HFI_PORT_RAW:
  66. port = INPUT_PORT;
  67. break;
  68. case HFI_PORT_BITSTREAM:
  69. port = OUTPUT_PORT;
  70. break;
  71. default:
  72. i_vpr_e(inst, "%s: invalid hfi port type %d\n",
  73. __func__, hfi_port);
  74. break;
  75. }
  76. } else {
  77. i_vpr_e(inst, "%s: invalid domain %#x\n",
  78. __func__, inst->domain);
  79. }
  80. return port;
  81. }
  82. bool is_valid_hfi_port(struct msm_vidc_inst *inst, u32 port,
  83. u32 buffer_type, const char *func)
  84. {
  85. if (!inst) {
  86. i_vpr_e(inst, "%s: invalid params\n", func);
  87. return false;
  88. }
  89. if (port == HFI_PORT_NONE &&
  90. buffer_type != HFI_BUFFER_ARP &&
  91. buffer_type != HFI_BUFFER_PERSIST)
  92. goto invalid;
  93. if (port != HFI_PORT_BITSTREAM && port != HFI_PORT_RAW)
  94. goto invalid;
  95. return true;
  96. invalid:
  97. i_vpr_e(inst, "%s: invalid port %#x buffer_type %u\n",
  98. func, port, buffer_type);
  99. return false;
  100. }
  101. bool is_valid_hfi_buffer_type(struct msm_vidc_inst *inst,
  102. u32 buffer_type, const char *func)
  103. {
  104. if (!inst) {
  105. i_vpr_e(inst, "%s: invalid params\n", func);
  106. return false;
  107. }
  108. if (buffer_type != HFI_BUFFER_BITSTREAM &&
  109. buffer_type != HFI_BUFFER_RAW &&
  110. buffer_type != HFI_BUFFER_METADATA &&
  111. buffer_type != HFI_BUFFER_BIN &&
  112. buffer_type != HFI_BUFFER_ARP &&
  113. buffer_type != HFI_BUFFER_COMV &&
  114. buffer_type != HFI_BUFFER_NON_COMV &&
  115. buffer_type != HFI_BUFFER_LINE &&
  116. buffer_type != HFI_BUFFER_DPB &&
  117. buffer_type != HFI_BUFFER_PERSIST) {
  118. i_vpr_e(inst, "%s: invalid buffer type %#x\n",
  119. func, buffer_type);
  120. return false;
  121. }
  122. return true;
  123. }
  124. static int signal_session_msg_receipt(struct msm_vidc_inst *inst,
  125. enum signal_session_response cmd)
  126. {
  127. if (cmd < MAX_SIGNAL)
  128. complete(&inst->completions[cmd]);
  129. return 0;
  130. }
  131. int validate_packet(u8 *response_pkt, u8 *core_resp_pkt,
  132. u32 core_resp_pkt_size, const char *func)
  133. {
  134. u8 *response_limit;
  135. u32 response_pkt_size = 0;
  136. if (!response_pkt || !core_resp_pkt || !core_resp_pkt_size) {
  137. d_vpr_e("%s: invalid params\n", func);
  138. return -EINVAL;
  139. }
  140. response_limit = core_resp_pkt + core_resp_pkt_size;
  141. if (response_pkt < core_resp_pkt || response_pkt > response_limit) {
  142. d_vpr_e("%s: invalid packet address\n", func);
  143. return -EINVAL;
  144. }
  145. response_pkt_size = *(u32 *)response_pkt;
  146. if (!response_pkt_size) {
  147. d_vpr_e("%s: response packet size cannot be zero\n", func);
  148. return -EINVAL;
  149. }
  150. if (response_pkt + response_pkt_size > response_limit) {
  151. d_vpr_e("%s: invalid packet size %d\n",
  152. func, *(u32 *)response_pkt);
  153. return -EINVAL;
  154. }
  155. return 0;
  156. }
  157. static bool check_last_flag(struct msm_vidc_inst *inst,
  158. struct hfi_packet *pkt)
  159. {
  160. struct hfi_buffer *buffer;
  161. if (!inst || !pkt) {
  162. d_vpr_e("%s: invalid params %d\n", __func__);
  163. return false;
  164. }
  165. buffer = (struct hfi_buffer *)((u8 *)pkt + sizeof(struct hfi_packet));
  166. if (buffer->flags & HFI_BUF_FW_FLAG_LAST) {
  167. i_vpr_h(inst, "%s: received last flag on FBD, index: %d\n",
  168. __func__, buffer->index);
  169. return true;
  170. }
  171. return false;
  172. }
  173. static int handle_session_info(struct msm_vidc_inst *inst,
  174. struct hfi_packet *pkt)
  175. {
  176. int rc = 0;
  177. char *info;
  178. switch (pkt->type) {
  179. case HFI_INFO_UNSUPPORTED:
  180. info = "unsupported";
  181. break;
  182. case HFI_INFO_DATA_CORRUPT:
  183. info = "data corrupt";
  184. break;
  185. default:
  186. info = "unknown";
  187. break;
  188. }
  189. i_vpr_e(inst, "session info (%#x): %s\n", pkt->type, info);
  190. return rc;
  191. }
  192. static int handle_session_error(struct msm_vidc_inst *inst,
  193. struct hfi_packet *pkt)
  194. {
  195. int rc = 0;
  196. char *error;
  197. switch (pkt->type) {
  198. case HFI_ERROR_MAX_SESSIONS:
  199. error = "exceeded max sessions";
  200. break;
  201. case HFI_ERROR_UNKNOWN_SESSION:
  202. error = "unknown session id";
  203. break;
  204. case HFI_ERROR_INVALID_STATE:
  205. error = "invalid operation for current state";
  206. break;
  207. case HFI_ERROR_INSUFFICIENT_RESOURCES:
  208. error = "insufficient resources";
  209. break;
  210. case HFI_ERROR_BUFFER_NOT_SET:
  211. error = "internal buffers not set";
  212. break;
  213. case HFI_ERROR_FATAL:
  214. error = "fatal error";
  215. break;
  216. default:
  217. error = "unknown";
  218. break;
  219. }
  220. i_vpr_e(inst, "session error (%#x): %s\n", pkt->type, error);
  221. rc = msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  222. return rc;
  223. }
  224. static int handle_system_error(struct msm_vidc_core *core,
  225. struct hfi_packet *pkt)
  226. {
  227. d_vpr_e("%s: system error received\n", __func__);
  228. print_sfr_message(core);
  229. msm_vidc_core_deinit(core, true);
  230. return 0;
  231. }
  232. static int handle_system_init(struct msm_vidc_core *core,
  233. struct hfi_packet *pkt)
  234. {
  235. if (pkt->flags & HFI_FW_FLAGS_SYSTEM_ERROR) {
  236. d_vpr_e("%s: received system error\n", __func__);
  237. return 0;
  238. }
  239. if (pkt->flags & HFI_FW_FLAGS_SUCCESS) {
  240. d_vpr_h("%s: successful\n", __func__);
  241. complete(&core->init_done);
  242. }
  243. return 0;
  244. }
  245. static int handle_session_open(struct msm_vidc_inst *inst,
  246. struct hfi_packet *pkt)
  247. {
  248. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  249. i_vpr_e(inst, "%s: received session error\n", __func__);
  250. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  251. return 0;
  252. }
  253. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  254. i_vpr_h(inst, "%s: successful\n", __func__);
  255. return 0;
  256. }
  257. static int handle_session_close(struct msm_vidc_inst *inst,
  258. struct hfi_packet *pkt)
  259. {
  260. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  261. i_vpr_e(inst, "%s: received session error\n", __func__);
  262. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  263. }
  264. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  265. i_vpr_h(inst, "%s: successful\n", __func__);
  266. signal_session_msg_receipt(inst, SIGNAL_CMD_CLOSE);
  267. return 0;
  268. }
  269. static int handle_session_start(struct msm_vidc_inst *inst,
  270. struct hfi_packet *pkt)
  271. {
  272. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  273. i_vpr_e(inst, "%s: received session error\n", __func__);
  274. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  275. return 0;
  276. }
  277. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  278. i_vpr_h(inst, "%s: successful for port %d\n",
  279. __func__, pkt->port);
  280. return 0;
  281. }
  282. static int handle_session_stop(struct msm_vidc_inst *inst,
  283. struct hfi_packet *pkt)
  284. {
  285. int signal_type = -1;
  286. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  287. i_vpr_e(inst, "%s: received session error\n", __func__);
  288. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  289. }
  290. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  291. i_vpr_h(inst, "%s: successful for port %d\n",
  292. __func__, pkt->port);
  293. if (is_encode_session(inst)) {
  294. if (pkt->port == HFI_PORT_RAW) {
  295. signal_type = SIGNAL_CMD_STOP_INPUT;
  296. } else if (pkt->port == HFI_PORT_BITSTREAM) {
  297. signal_type = SIGNAL_CMD_STOP_OUTPUT;
  298. } else {
  299. i_vpr_e(inst, "%s: invalid port: %d\n",
  300. __func__, pkt->port);
  301. return -EINVAL;
  302. }
  303. } else if (is_decode_session(inst)) {
  304. if (pkt->port == HFI_PORT_RAW) {
  305. signal_type = SIGNAL_CMD_STOP_OUTPUT;
  306. } else if (pkt->port == HFI_PORT_BITSTREAM) {
  307. signal_type = SIGNAL_CMD_STOP_INPUT;
  308. } else {
  309. i_vpr_e(inst, "%s: invalid port: %d\n",
  310. __func__, pkt->port);
  311. return -EINVAL;
  312. }
  313. } else {
  314. i_vpr_e(inst, "%s: invalid session\n", __func__);
  315. return -EINVAL;
  316. }
  317. if (signal_type != -1)
  318. signal_session_msg_receipt(inst, signal_type);
  319. return 0;
  320. }
  321. static int handle_session_drain(struct msm_vidc_inst *inst,
  322. struct hfi_packet *pkt)
  323. {
  324. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  325. i_vpr_e(inst, "%s: received session error\n", __func__);
  326. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  327. return 0;
  328. }
  329. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  330. i_vpr_h(inst, "%s: successful\n", __func__);
  331. return 0;
  332. }
  333. static int get_driver_buffer_flags(struct msm_vidc_inst *inst, u32 hfi_flags)
  334. {
  335. u32 driver_flags = 0;
  336. if (inst->frame_prop.hfi_picture_type & HFI_PICTURE_IDR) {
  337. driver_flags |= MSM_VIDC_BUF_FLAG_KEYFRAME;
  338. } else if (inst->frame_prop.hfi_picture_type & HFI_PICTURE_P) {
  339. driver_flags |= MSM_VIDC_BUF_FLAG_PFRAME;
  340. } else if (inst->frame_prop.hfi_picture_type & HFI_PICTURE_B) {
  341. driver_flags |= MSM_VIDC_BUF_FLAG_BFRAME;
  342. } else if (inst->frame_prop.hfi_picture_type & HFI_PICTURE_I) {
  343. if (inst->codec == MSM_VIDC_VP9)
  344. driver_flags |= MSM_VIDC_BUF_FLAG_KEYFRAME;
  345. } else if (inst->frame_prop.hfi_picture_type & HFI_PICTURE_CRA) {
  346. driver_flags |= MSM_VIDC_BUF_FLAG_KEYFRAME;
  347. } else if (inst->frame_prop.hfi_picture_type & HFI_PICTURE_BLA) {
  348. driver_flags |= MSM_VIDC_BUF_FLAG_KEYFRAME;
  349. }
  350. if (inst->capabilities->cap[META_BUF_TAG].value) {
  351. if (inst->frame_prop.hfi_no_output &&
  352. !(hfi_flags & HFI_BUF_FW_FLAG_CODEC_CONFIG))
  353. driver_flags |= MSM_VIDC_BUF_FLAG_ERROR;
  354. }
  355. if (hfi_flags & HFI_BUF_FW_FLAG_CODEC_CONFIG)
  356. driver_flags |= MSM_VIDC_BUF_FLAG_CODECCONFIG;
  357. if (hfi_flags & HFI_BUF_FW_FLAG_LAST)
  358. driver_flags |= MSM_VIDC_BUF_FLAG_LAST;
  359. return driver_flags;
  360. }
  361. static int handle_input_buffer(struct msm_vidc_inst *inst,
  362. struct hfi_buffer *buffer)
  363. {
  364. int rc = 0;
  365. struct msm_vidc_buffers *buffers;
  366. struct msm_vidc_buffer *buf;
  367. bool found;
  368. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_INPUT, __func__);
  369. if (!buffers)
  370. return -EINVAL;
  371. found = false;
  372. list_for_each_entry(buf, &buffers->list, list) {
  373. if (buf->device_addr == buffer->base_address) {
  374. found = true;
  375. break;
  376. }
  377. }
  378. if (!found) {
  379. i_vpr_e(inst, "%s: buffer not found for idx %d addr %#x\n",
  380. __func__, buffer->index, buffer->base_address);
  381. return -EINVAL;
  382. }
  383. buf->data_offset = buffer->data_offset;
  384. buf->data_size = buffer->data_size;
  385. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  386. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  387. buf->flags = 0;
  388. buf->flags = get_driver_buffer_flags(inst, buffer->flags);
  389. print_vidc_buffer(VIDC_HIGH, "EBD", inst, buf);
  390. msm_vidc_debugfs_update(inst, MSM_VIDC_DEBUGFS_EVENT_EBD);
  391. return rc;
  392. }
  393. static int handle_output_buffer(struct msm_vidc_inst *inst,
  394. struct hfi_buffer *buffer)
  395. {
  396. int rc = 0;
  397. struct msm_vidc_buffers *buffers;
  398. struct msm_vidc_buffer *buf;
  399. bool found;
  400. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_OUTPUT, __func__);
  401. if (!buffers)
  402. return -EINVAL;
  403. found = false;
  404. list_for_each_entry(buf, &buffers->list, list) {
  405. if (buf->device_addr == buffer->base_address) {
  406. found = true;
  407. break;
  408. }
  409. }
  410. if (!found) {
  411. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  412. __func__, buffer->index, buffer->base_address);
  413. return -EINVAL;
  414. }
  415. buf->data_offset = buffer->data_offset;
  416. buf->data_size = buffer->data_size;
  417. buf->timestamp = buffer->timestamp;
  418. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  419. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  420. /*
  421. * reset read only flag for a zero length
  422. * buffer (if marked read only)
  423. */
  424. if (buffer->flags & HFI_BUF_FW_FLAG_READONLY &&
  425. !buffer->data_size)
  426. buf->attr &= ~MSM_VIDC_ATTR_READ_ONLY;
  427. else if (buffer->flags & HFI_BUF_FW_FLAG_READONLY)
  428. buf->attr |= MSM_VIDC_ATTR_READ_ONLY;
  429. else
  430. buf->attr &= ~MSM_VIDC_ATTR_READ_ONLY;
  431. buf->flags = 0;
  432. buf->flags = get_driver_buffer_flags(inst, buffer->flags);
  433. print_vidc_buffer(VIDC_HIGH, "FBD", inst, buf);
  434. msm_vidc_debugfs_update(inst, MSM_VIDC_DEBUGFS_EVENT_FBD);
  435. return rc;
  436. }
  437. static int handle_input_metadata_buffer(struct msm_vidc_inst *inst,
  438. struct hfi_buffer *buffer)
  439. {
  440. int rc = 0;
  441. struct msm_vidc_buffers *buffers;
  442. struct msm_vidc_buffer *buf;
  443. bool found;
  444. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_INPUT_META, __func__);
  445. if (!buffers)
  446. return -EINVAL;
  447. found = false;
  448. list_for_each_entry(buf, &buffers->list, list) {
  449. if (buf->device_addr == buffer->base_address) {
  450. found = true;
  451. break;
  452. }
  453. }
  454. if (!found) {
  455. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  456. __func__, buffer->index, buffer->base_address);
  457. return -EINVAL;
  458. }
  459. buf->data_size = buffer->data_size;
  460. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  461. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  462. buf->flags = 0;
  463. if (buffer->flags & HFI_BUF_FW_FLAG_LAST)
  464. buf->flags |= MSM_VIDC_BUF_FLAG_LAST;
  465. print_vidc_buffer(VIDC_HIGH, "EBD META", inst, buf);
  466. return rc;
  467. }
  468. static int handle_output_metadata_buffer(struct msm_vidc_inst *inst,
  469. struct hfi_buffer *buffer)
  470. {
  471. int rc = 0;
  472. struct msm_vidc_buffers *buffers;
  473. struct msm_vidc_buffer *buf;
  474. bool found;
  475. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_OUTPUT_META, __func__);
  476. if (!buffers)
  477. return -EINVAL;
  478. found = false;
  479. list_for_each_entry(buf, &buffers->list, list) {
  480. if (buf->device_addr == buffer->base_address) {
  481. found = true;
  482. break;
  483. }
  484. }
  485. if (!found) {
  486. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  487. __func__, buffer->index, buffer->base_address);
  488. return -EINVAL;
  489. }
  490. buf->data_size = buffer->data_size;
  491. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  492. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  493. buf->flags = 0;
  494. if (buffer->flags & HFI_BUF_FW_FLAG_LAST)
  495. buf->flags |= MSM_VIDC_BUF_FLAG_LAST;
  496. print_vidc_buffer(VIDC_HIGH, "FBD META", inst, buf);
  497. return rc;
  498. }
  499. static int handle_dequeue_buffers(struct msm_vidc_inst* inst)
  500. {
  501. int rc = 0;
  502. int i;
  503. struct msm_vidc_buffers* buffers;
  504. struct msm_vidc_buffer* buf;
  505. struct msm_vidc_buffer* dummy;
  506. enum msm_vidc_buffer_type buffer_type[] = {
  507. MSM_VIDC_BUF_INPUT_META,
  508. MSM_VIDC_BUF_INPUT,
  509. MSM_VIDC_BUF_OUTPUT_META,
  510. MSM_VIDC_BUF_OUTPUT,
  511. };
  512. for (i = 0; i < ARRAY_SIZE(buffer_type); i++) {
  513. buffers = msm_vidc_get_buffers(inst, buffer_type[i], __func__);
  514. if (!buffers)
  515. return -EINVAL;
  516. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  517. if (buf->attr & MSM_VIDC_ATTR_DEQUEUED) {
  518. buf->attr &= ~MSM_VIDC_ATTR_DEQUEUED;
  519. /*
  520. * do not send vb2_buffer_done when fw sends FBDs
  521. * with read only flag for second time
  522. */
  523. if ((buf->attr & MSM_VIDC_ATTR_BUFFER_DONE) &&
  524. buf->attr & MSM_VIDC_ATTR_READ_ONLY){
  525. print_vidc_buffer(VIDC_HIGH, "vb2 done already", inst, buf);
  526. } else {
  527. buf->attr |= MSM_VIDC_ATTR_BUFFER_DONE;
  528. msm_vidc_vb2_buffer_done(inst, buf);
  529. }
  530. /* do not unmap / delete read only buffer */
  531. if (!(buf->attr & MSM_VIDC_ATTR_READ_ONLY))
  532. msm_vidc_put_driver_buf(inst, buf);
  533. }
  534. }
  535. }
  536. return rc;
  537. }
  538. static int handle_dpb_buffer(struct msm_vidc_inst *inst,
  539. struct hfi_buffer *buffer)
  540. {
  541. int rc = 0;
  542. struct msm_vidc_buffers *buffers;
  543. struct msm_vidc_buffer *buf;
  544. bool found;
  545. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_DPB, __func__);
  546. if (!buffers)
  547. return -EINVAL;
  548. found = false;
  549. list_for_each_entry(buf, &buffers->list, list) {
  550. if (buf->device_addr == buffer->base_address) {
  551. found = true;
  552. break;
  553. }
  554. }
  555. if (found) {
  556. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  557. } else {
  558. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  559. __func__, buffer->index, buffer->base_address);
  560. return -EINVAL;
  561. }
  562. return rc;
  563. }
  564. static int handle_persist_buffer(struct msm_vidc_inst *inst,
  565. struct hfi_buffer *buffer)
  566. {
  567. int rc = 0;
  568. struct msm_vidc_buffers *buffers;
  569. struct msm_vidc_buffer *buf;
  570. bool found;
  571. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_PERSIST, __func__);
  572. if (!buffers)
  573. return -EINVAL;
  574. found = false;
  575. list_for_each_entry(buf, &buffers->list, list) {
  576. if (buf->device_addr == buffer->base_address) {
  577. found = true;
  578. break;
  579. }
  580. }
  581. if (found) {
  582. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  583. } else {
  584. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  585. __func__, buffer->index, buffer->base_address);
  586. return -EINVAL;
  587. }
  588. return rc;
  589. }
  590. static int handle_line_buffer(struct msm_vidc_inst *inst,
  591. struct hfi_buffer *buffer)
  592. {
  593. int rc = 0;
  594. struct msm_vidc_buffers *buffers;
  595. struct msm_vidc_buffer *buf;
  596. bool found;
  597. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_LINE, __func__);
  598. if (!buffers)
  599. return -EINVAL;
  600. found = false;
  601. list_for_each_entry(buf, &buffers->list, list) {
  602. if (buf->device_addr == buffer->base_address) {
  603. found = true;
  604. break;
  605. }
  606. }
  607. if (found) {
  608. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  609. } else {
  610. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  611. __func__, buffer->index, buffer->base_address);
  612. return -EINVAL;
  613. }
  614. return rc;
  615. }
  616. static int handle_non_comv_buffer(struct msm_vidc_inst *inst,
  617. struct hfi_buffer *buffer)
  618. {
  619. int rc = 0;
  620. struct msm_vidc_buffers *buffers;
  621. struct msm_vidc_buffer *buf;
  622. bool found;
  623. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_NON_COMV, __func__);
  624. if (!buffers)
  625. return -EINVAL;
  626. found = false;
  627. list_for_each_entry(buf, &buffers->list, list) {
  628. if (buf->device_addr == buffer->base_address) {
  629. found = true;
  630. break;
  631. }
  632. }
  633. if (found) {
  634. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  635. } else {
  636. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  637. __func__, buffer->index, buffer->base_address);
  638. return -EINVAL;
  639. }
  640. return rc;
  641. }
  642. static int handle_comv_buffer(struct msm_vidc_inst *inst,
  643. struct hfi_buffer *buffer)
  644. {
  645. int rc = 0;
  646. struct msm_vidc_buffers *buffers;
  647. struct msm_vidc_buffer *buf;
  648. bool found;
  649. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_COMV, __func__);
  650. if (!buffers)
  651. return -EINVAL;
  652. found = false;
  653. list_for_each_entry(buf, &buffers->list, list) {
  654. if (buf->device_addr == buffer->base_address) {
  655. found = true;
  656. break;
  657. }
  658. }
  659. if (found) {
  660. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  661. } else {
  662. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  663. __func__, buffer->index, buffer->base_address);
  664. return -EINVAL;
  665. }
  666. return rc;
  667. }
  668. static int handle_bin_buffer(struct msm_vidc_inst *inst,
  669. struct hfi_buffer *buffer)
  670. {
  671. int rc = 0;
  672. struct msm_vidc_buffers *buffers;
  673. struct msm_vidc_buffer *buf;
  674. bool found;
  675. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_BIN, __func__);
  676. if (!buffers)
  677. return -EINVAL;
  678. found = false;
  679. list_for_each_entry(buf, &buffers->list, list) {
  680. if (buf->device_addr == buffer->base_address) {
  681. found = true;
  682. break;
  683. }
  684. }
  685. if (found) {
  686. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  687. } else {
  688. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  689. __func__, buffer->index, buffer->base_address);
  690. return -EINVAL;
  691. }
  692. return rc;
  693. }
  694. static int handle_arp_buffer(struct msm_vidc_inst *inst,
  695. struct hfi_buffer *buffer)
  696. {
  697. int rc = 0;
  698. struct msm_vidc_buffers *buffers;
  699. struct msm_vidc_buffer *buf;
  700. bool found;
  701. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_ARP, __func__);
  702. if (!buffers)
  703. return -EINVAL;
  704. found = false;
  705. list_for_each_entry(buf, &buffers->list, list) {
  706. if (buf->device_addr == buffer->base_address) {
  707. found = true;
  708. break;
  709. }
  710. }
  711. if (found) {
  712. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  713. } else {
  714. i_vpr_e(inst, "%s: invalid idx %d daddr %#x\n",
  715. __func__, buffer->index, buffer->base_address);
  716. return -EINVAL;
  717. }
  718. return rc;
  719. }
  720. static int handle_session_buffer(struct msm_vidc_inst *inst,
  721. struct hfi_packet *pkt)
  722. {
  723. int rc = 0;
  724. struct hfi_buffer *buffer;
  725. u32 buf_type = 0, port_type = 0;
  726. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  727. i_vpr_e(inst, "%s: received session error\n", __func__);
  728. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  729. return 0;
  730. }
  731. port_type = pkt->port;
  732. buffer = (struct hfi_buffer *)((u8 *)pkt + sizeof(struct hfi_packet));
  733. buf_type = buffer->type;
  734. if (!is_valid_hfi_buffer_type(inst, buf_type, __func__)) {
  735. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  736. return 0;
  737. }
  738. if (!is_valid_hfi_port(inst, port_type, buf_type, __func__)) {
  739. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  740. return 0;
  741. }
  742. if (is_encode_session(inst)) {
  743. if (port_type == HFI_PORT_BITSTREAM) {
  744. if (buf_type == HFI_BUFFER_METADATA)
  745. rc = handle_output_metadata_buffer(inst, buffer);
  746. else if (buf_type == HFI_BUFFER_BITSTREAM)
  747. rc = handle_output_buffer(inst, buffer);
  748. else if (buf_type == HFI_BUFFER_BIN)
  749. rc = handle_bin_buffer(inst, buffer);
  750. else if (buf_type == HFI_BUFFER_COMV)
  751. rc = handle_comv_buffer(inst, buffer);
  752. else if (buf_type == HFI_BUFFER_NON_COMV)
  753. rc = handle_non_comv_buffer(inst, buffer);
  754. else if (buf_type == HFI_BUFFER_LINE)
  755. rc = handle_line_buffer(inst, buffer);
  756. else if (buf_type == HFI_BUFFER_ARP)
  757. rc = handle_arp_buffer(inst, buffer);
  758. else if (buf_type == HFI_BUFFER_DPB)
  759. rc = handle_dpb_buffer(inst, buffer);
  760. else
  761. i_vpr_e(inst, "%s: unknown bitstream port buffer type %#x\n",
  762. __func__, buf_type);
  763. } else if (port_type == HFI_PORT_RAW) {
  764. if (buf_type == HFI_BUFFER_METADATA)
  765. rc = handle_input_metadata_buffer(inst, buffer);
  766. else if (buf_type == HFI_BUFFER_RAW)
  767. rc = handle_input_buffer(inst, buffer);
  768. else
  769. i_vpr_e(inst, "%s: unknown raw port buffer type %#x\n",
  770. __func__, buf_type);
  771. }
  772. } else if (is_decode_session(inst)) {
  773. if (port_type == HFI_PORT_BITSTREAM) {
  774. if (buf_type == HFI_BUFFER_METADATA)
  775. rc = handle_input_metadata_buffer(inst, buffer);
  776. else if (buf_type == HFI_BUFFER_BITSTREAM)
  777. rc = handle_input_buffer(inst, buffer);
  778. else if (buf_type == HFI_BUFFER_BIN)
  779. rc = handle_bin_buffer(inst, buffer);
  780. else if (buf_type == HFI_BUFFER_COMV)
  781. rc = handle_comv_buffer(inst, buffer);
  782. else if (buf_type == HFI_BUFFER_NON_COMV)
  783. rc = handle_non_comv_buffer(inst, buffer);
  784. else if (buf_type == HFI_BUFFER_LINE)
  785. rc = handle_line_buffer(inst, buffer);
  786. else if (buf_type == HFI_BUFFER_PERSIST)
  787. rc = handle_persist_buffer(inst, buffer);
  788. else
  789. i_vpr_e(inst, "%s: unknown bitstream port buffer type %#x\n",
  790. __func__, buf_type);
  791. } else if (port_type == HFI_PORT_RAW) {
  792. if (buf_type == HFI_BUFFER_METADATA)
  793. rc = handle_output_metadata_buffer(inst, buffer);
  794. else if (buf_type == HFI_BUFFER_RAW)
  795. rc = handle_output_buffer(inst, buffer);
  796. else if (buf_type == HFI_BUFFER_DPB)
  797. rc = handle_dpb_buffer(inst, buffer);
  798. else
  799. i_vpr_e(inst, "%s: unknown raw port buffer type %#x\n",
  800. __func__, buf_type);
  801. }
  802. } else {
  803. i_vpr_e(inst, "%s: invalid session %d\n",
  804. __func__, inst->domain);
  805. return -EINVAL;
  806. }
  807. return rc;
  808. }
  809. static int handle_port_settings_change(struct msm_vidc_inst *inst,
  810. struct hfi_packet *pkt)
  811. {
  812. int rc = 0;
  813. i_vpr_h(inst, "%s: Received port settings change, type %d\n",
  814. __func__, pkt->port);
  815. if (pkt->port == HFI_PORT_RAW) {
  816. print_psc_properties(VIDC_HIGH, "OUTPUT_PSC", inst,
  817. inst->subcr_params[OUTPUT_PORT]);
  818. rc = msm_vdec_output_port_settings_change(inst);
  819. } else if (pkt->port == HFI_PORT_BITSTREAM) {
  820. print_psc_properties(VIDC_HIGH, "INPUT_PSC", inst,
  821. inst->subcr_params[INPUT_PORT]);
  822. rc = msm_vdec_input_port_settings_change(inst);
  823. } else {
  824. i_vpr_e(inst, "%s: invalid port type: %#x\n",
  825. __func__, pkt->port);
  826. rc = -EINVAL;
  827. }
  828. return rc;
  829. }
  830. static int handle_session_subscribe_mode(struct msm_vidc_inst *inst,
  831. struct hfi_packet *pkt)
  832. {
  833. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  834. i_vpr_e(inst, "%s: received session error\n", __func__);
  835. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  836. }
  837. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  838. i_vpr_h(inst, "%s: successful\n", __func__);
  839. return 0;
  840. }
  841. static int handle_session_delivery_mode(struct msm_vidc_inst *inst,
  842. struct hfi_packet *pkt)
  843. {
  844. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  845. i_vpr_e(inst, "%s: received session error\n", __func__);
  846. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  847. }
  848. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  849. i_vpr_h(inst, "%s: successful\n", __func__);
  850. return 0;
  851. }
  852. static int handle_session_command(struct msm_vidc_inst *inst,
  853. struct hfi_packet *pkt)
  854. {
  855. switch (pkt->type) {
  856. case HFI_CMD_OPEN:
  857. return handle_session_open(inst, pkt);
  858. case HFI_CMD_CLOSE:
  859. return handle_session_close(inst, pkt);
  860. case HFI_CMD_START:
  861. return handle_session_start(inst, pkt);
  862. case HFI_CMD_STOP:
  863. return handle_session_stop(inst, pkt);
  864. case HFI_CMD_DRAIN:
  865. return handle_session_drain(inst, pkt);
  866. case HFI_CMD_BUFFER:
  867. return handle_session_buffer(inst, pkt);
  868. case HFI_CMD_SETTINGS_CHANGE:
  869. return handle_port_settings_change(inst, pkt);
  870. case HFI_CMD_SUBSCRIBE_MODE:
  871. return handle_session_subscribe_mode(inst, pkt);
  872. case HFI_CMD_DELIVERY_MODE:
  873. return handle_session_delivery_mode(inst, pkt);
  874. default:
  875. i_vpr_e(inst, "%s: Unsupported command type: %#x\n",
  876. __func__, pkt->type);
  877. return -EINVAL;
  878. }
  879. return 0;
  880. }
  881. static int handle_session_property(struct msm_vidc_inst *inst,
  882. struct hfi_packet *pkt)
  883. {
  884. int rc = 0;
  885. u32 port;
  886. u32 *payload_ptr;
  887. i_vpr_h(inst, "%s: property type %#x\n", __func__, pkt->type);
  888. port = vidc_port_from_hfi(inst, pkt->port);
  889. payload_ptr = (u32 *)((u8 *)pkt + sizeof(struct hfi_packet));
  890. switch (pkt->type) {
  891. case HFI_PROP_BITSTREAM_RESOLUTION:
  892. inst->subcr_params[port].bitstream_resolution = payload_ptr[0];
  893. break;
  894. case HFI_PROP_CROP_OFFSETS:
  895. inst->subcr_params[port].crop_offsets =
  896. (u64)payload_ptr[0] << 32 | payload_ptr[1];
  897. break;
  898. case HFI_PROP_LUMA_CHROMA_BIT_DEPTH:
  899. inst->subcr_params[port].bit_depth = payload_ptr[0];
  900. break;
  901. case HFI_PROP_CODED_FRAMES:
  902. inst->subcr_params[port].coded_frames = payload_ptr[0];
  903. break;
  904. case HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT:
  905. inst->subcr_params[port].fw_min_count = payload_ptr[0];
  906. break;
  907. case HFI_PROP_PIC_ORDER_CNT_TYPE:
  908. inst->subcr_params[port].pic_order_cnt = payload_ptr[0];
  909. break;
  910. case HFI_PROP_SIGNAL_COLOR_INFO:
  911. inst->subcr_params[port].color_info = payload_ptr[0];
  912. break;
  913. case HFI_PROP_PROFILE:
  914. inst->subcr_params[port].profile = payload_ptr[0];
  915. break;
  916. case HFI_PROP_LEVEL:
  917. inst->subcr_params[port].level = payload_ptr[0];
  918. break;
  919. case HFI_PROP_TIER:
  920. inst->subcr_params[port].tier = payload_ptr[0];
  921. break;
  922. case HFI_PROP_PICTURE_TYPE:
  923. if (is_encode_session(inst) && port == INPUT_PORT) {
  924. rc = -EINVAL;
  925. i_vpr_e(inst,
  926. "%s: invalid port: %d for property %#x\n",
  927. __func__, pkt->port, pkt->type);
  928. break;
  929. }
  930. inst->frame_prop.hfi_picture_type = payload_ptr[0];
  931. break;
  932. case HFI_PROP_NO_OUTPUT:
  933. if (port != INPUT_PORT) {
  934. rc = -EINVAL;
  935. i_vpr_e(inst,
  936. "%s: invalid port: %d for property %#x\n",
  937. __func__, pkt->port, pkt->type);
  938. break;
  939. }
  940. inst->frame_prop.hfi_no_output = 1;
  941. break;
  942. default:
  943. i_vpr_e(inst, "%s: invalid port settings property %#x\n",
  944. __func__, pkt->type);
  945. return -EINVAL;
  946. }
  947. return rc;
  948. }
  949. static int handle_image_version_property(struct msm_vidc_core *core,
  950. struct hfi_packet *pkt)
  951. {
  952. u32 i = 0;
  953. u8 *str_image_version;
  954. u32 req_bytes;
  955. req_bytes = pkt->size - sizeof(*pkt);
  956. if (req_bytes < VENUS_VERSION_LENGTH - 1) {
  957. d_vpr_e("%s: bad_pkt: %d\n", __func__, req_bytes);
  958. return -EINVAL;
  959. }
  960. str_image_version = (u8 *)pkt + sizeof(struct hfi_packet);
  961. /*
  962. * The version string returned by firmware includes null
  963. * characters at the start and in between. Replace the null
  964. * characters with space, to print the version info.
  965. */
  966. for (i = 0; i < VENUS_VERSION_LENGTH - 1; i++) {
  967. if (str_image_version[i] != '\0')
  968. core->fw_version[i] = str_image_version[i];
  969. else
  970. core->fw_version[i] = ' ';
  971. }
  972. core->fw_version[i] = '\0';
  973. d_vpr_h("%s: F/W version: %s\n", __func__, core->fw_version);
  974. return 0;
  975. }
  976. static int handle_system_property(struct msm_vidc_core *core,
  977. struct hfi_packet *pkt)
  978. {
  979. int rc = 0;
  980. if (pkt->flags & HFI_FW_FLAGS_SYSTEM_ERROR) {
  981. d_vpr_e("%s: received system error for property type %#x\n",
  982. __func__, pkt->type);
  983. return handle_system_error(core, pkt);
  984. }
  985. switch (pkt->type) {
  986. case HFI_PROP_IMAGE_VERSION:
  987. rc = handle_image_version_property(core, pkt);
  988. break;
  989. default:
  990. d_vpr_h("%s: property type %#x successful\n",
  991. __func__, pkt->type);
  992. break;
  993. }
  994. return rc;
  995. }
  996. static int handle_system_response(struct msm_vidc_core *core,
  997. struct hfi_header *hdr)
  998. {
  999. int rc = 0;
  1000. struct hfi_packet *packet;
  1001. u8 *pkt;
  1002. int i;
  1003. pkt = (u8 *)((u8 *)hdr + sizeof(struct hfi_header));
  1004. for (i = 0; i < hdr->num_packets; i++) {
  1005. if (validate_packet((u8 *)pkt, core->response_packet,
  1006. core->packet_size, __func__)) {
  1007. rc = -EINVAL;
  1008. goto exit;
  1009. }
  1010. packet = (struct hfi_packet *)pkt;
  1011. if (packet->type == HFI_CMD_INIT) {
  1012. rc = handle_system_init(core, packet);
  1013. } else if (packet->type > HFI_SYSTEM_ERROR_BEGIN &&
  1014. packet->type < HFI_SYSTEM_ERROR_END) {
  1015. rc = handle_system_error(core, packet);
  1016. } else if (packet->type > HFI_PROP_BEGIN &&
  1017. packet->type < HFI_PROP_CODEC) {
  1018. rc = handle_system_property(core, packet);
  1019. } else {
  1020. d_vpr_e("%s: Unknown packet type: %#x\n",
  1021. __func__, packet->type);
  1022. rc = -EINVAL;
  1023. goto exit;
  1024. }
  1025. pkt += packet->size;
  1026. }
  1027. exit:
  1028. return rc;
  1029. }
  1030. static int process_response_packet(struct msm_vidc_inst *inst,
  1031. struct hfi_packet *packet)
  1032. {
  1033. int rc = 0;
  1034. if (packet->type > HFI_CMD_BEGIN &&
  1035. packet->type < HFI_CMD_END) {
  1036. rc = handle_session_command(inst, packet);
  1037. } else if (packet->type > HFI_PROP_BEGIN &&
  1038. packet->type < HFI_PROP_END) {
  1039. rc = handle_session_property(inst, packet);
  1040. } else if (packet->type > HFI_SESSION_ERROR_BEGIN &&
  1041. packet->type < HFI_SESSION_ERROR_END) {
  1042. rc = handle_session_error(inst, packet);
  1043. } else if (packet->type > HFI_INFORMATION_BEGIN &&
  1044. packet->type < HFI_INFORMATION_END) {
  1045. rc = handle_session_info(inst, packet);
  1046. } else {
  1047. i_vpr_e(inst, "%s: Unknown packet type: %#x\n",
  1048. __func__, packet->type);
  1049. rc = -EINVAL;
  1050. }
  1051. return rc;
  1052. }
  1053. int handle_session_response_work(struct msm_vidc_inst *inst,
  1054. struct response_work *resp_work)
  1055. {
  1056. int rc = 0;
  1057. struct hfi_header *hdr = NULL;
  1058. struct hfi_packet *packet;
  1059. u8 *pkt, *start_pkt;
  1060. u32 hfi_cmd_type = 0;
  1061. int i, j;
  1062. struct msm_vidc_cmd_range be[5] = {
  1063. {HFI_SYSTEM_ERROR_BEGIN, HFI_SYSTEM_ERROR_END},
  1064. {HFI_SESSION_ERROR_BEGIN, HFI_SESSION_ERROR_END},
  1065. {HFI_PROP_BEGIN, HFI_PROP_END},
  1066. {HFI_CMD_BEGIN, HFI_CMD_END},
  1067. {HFI_INFORMATION_BEGIN, HFI_INFORMATION_END},
  1068. };
  1069. if (!inst || !resp_work) {
  1070. d_vpr_e("%s: invalid params\n", __func__);
  1071. return -EINVAL;
  1072. }
  1073. hdr = (struct hfi_header *)resp_work->data;
  1074. if (!hdr) {
  1075. d_vpr_e("%s: invalid params\n", __func__);
  1076. return -EINVAL;
  1077. }
  1078. hfi_cmd_type = 0;
  1079. pkt = (u8 *)((u8 *)hdr + sizeof(struct hfi_header));
  1080. start_pkt = pkt;
  1081. /* validate all packets */
  1082. for (i = 0; i < hdr->num_packets; i++) {
  1083. packet = (struct hfi_packet * ) pkt;
  1084. if (validate_packet(pkt, resp_work->data,
  1085. resp_work->data_size, __func__)) {
  1086. rc = -EINVAL;
  1087. goto exit;
  1088. }
  1089. pkt += packet->size;
  1090. }
  1091. memset(&inst->frame_prop, 0,
  1092. sizeof(struct msm_vidc_frame_properties));
  1093. for (i = 0; i < ARRAY_SIZE(be); i++) {
  1094. pkt = start_pkt;
  1095. for (j = 0; j < hdr->num_packets; j++) {
  1096. packet = (struct hfi_packet * ) pkt;
  1097. if (packet->type > be[i].begin
  1098. && packet->type < be[i].end) {
  1099. if (hfi_cmd_type == HFI_CMD_SETTINGS_CHANGE) {
  1100. i_vpr_e(inst,
  1101. "%s: invalid packet type %d in port settings change\n",
  1102. __func__, packet->type);
  1103. rc = -EINVAL;
  1104. }
  1105. hfi_cmd_type = packet->type;
  1106. rc = process_response_packet(inst, packet);
  1107. if (rc)
  1108. goto exit;
  1109. }
  1110. pkt += packet->size;
  1111. }
  1112. }
  1113. if (hfi_cmd_type == HFI_CMD_BUFFER) {
  1114. rc = handle_dequeue_buffers(inst);
  1115. if (rc)
  1116. goto exit;
  1117. }
  1118. memset(&inst->frame_prop, 0,
  1119. sizeof(struct msm_vidc_frame_properties));
  1120. exit:
  1121. return rc;
  1122. }
  1123. void handle_session_response_work_handler(struct work_struct *work)
  1124. {
  1125. int rc = 0;
  1126. struct msm_vidc_inst *inst;
  1127. struct response_work *resp_work, *dummy = NULL;
  1128. inst = container_of(work, struct msm_vidc_inst, response_work.work);
  1129. inst = get_inst_ref(g_core, inst);
  1130. if (!inst) {
  1131. d_vpr_e("%s: invalid params\n", __func__);
  1132. return;
  1133. }
  1134. mutex_lock(&inst->lock);
  1135. list_for_each_entry_safe(resp_work, dummy, &inst->response_works, list) {
  1136. switch (resp_work->type) {
  1137. case RESP_WORK_INPUT_PSC:
  1138. {
  1139. enum msm_vidc_allow allow = MSM_VIDC_DISALLOW;
  1140. allow = msm_vidc_allow_input_psc(inst);
  1141. if (allow == MSM_VIDC_DISALLOW) {
  1142. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1143. break;
  1144. } else if (allow == MSM_VIDC_DEFER) {
  1145. /* continue to next entry processing */
  1146. continue;
  1147. } else if (allow == MSM_VIDC_ALLOW) {
  1148. rc = handle_session_response_work(inst, resp_work);
  1149. if (!rc)
  1150. rc = msm_vidc_state_change_input_psc(inst);
  1151. /* either handle input psc or state change failed */
  1152. if (rc)
  1153. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1154. }
  1155. break;
  1156. }
  1157. case RESP_WORK_OUTPUT_PSC:
  1158. rc = handle_session_response_work(inst, resp_work);
  1159. if (rc)
  1160. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1161. break;
  1162. case RESP_WORK_LAST_FLAG:
  1163. rc = handle_session_response_work(inst, resp_work);
  1164. if (rc) {
  1165. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1166. break;
  1167. }
  1168. if (msm_vidc_allow_last_flag(inst)) {
  1169. rc = msm_vidc_state_change_last_flag(inst);
  1170. if (rc)
  1171. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1172. }
  1173. break;
  1174. default:
  1175. d_vpr_e("%s: invalid response work type %d\n", __func__,
  1176. resp_work->type);
  1177. break;
  1178. }
  1179. list_del(&resp_work->list);
  1180. kfree(resp_work->data);
  1181. kfree(resp_work);
  1182. }
  1183. mutex_unlock(&inst->lock);
  1184. put_inst(inst);
  1185. }
  1186. static int queue_response_work(struct msm_vidc_inst *inst,
  1187. enum response_work_type type, void *hdr, u32 hdr_size)
  1188. {
  1189. struct response_work *work;
  1190. work = kzalloc(sizeof(struct response_work), GFP_KERNEL);
  1191. INIT_LIST_HEAD(&work->list);
  1192. work->type = type;
  1193. work->data_size = hdr_size;
  1194. work->data = kzalloc(hdr_size, GFP_KERNEL);
  1195. if (!work->data)
  1196. return -ENOMEM;
  1197. memcpy(work->data, hdr, hdr_size);
  1198. list_add_tail(&work->list, &inst->response_works);
  1199. queue_delayed_work(inst->response_workq,
  1200. &inst->response_work, msecs_to_jiffies(0));
  1201. return 0;
  1202. }
  1203. static int handle_session_response(struct msm_vidc_core *core,
  1204. struct hfi_header *hdr)
  1205. {
  1206. int rc = 0;
  1207. struct msm_vidc_inst *inst;
  1208. struct hfi_packet *packet;
  1209. u8 *pkt, *start_pkt;
  1210. u32 hfi_cmd_type = 0;
  1211. u32 hfi_port = 0;
  1212. int i, j;
  1213. struct msm_vidc_cmd_range be[5] = {
  1214. {HFI_SYSTEM_ERROR_BEGIN, HFI_SYSTEM_ERROR_END},
  1215. {HFI_SESSION_ERROR_BEGIN, HFI_SESSION_ERROR_END},
  1216. {HFI_PROP_BEGIN, HFI_PROP_END},
  1217. {HFI_CMD_BEGIN, HFI_CMD_END},
  1218. {HFI_INFORMATION_BEGIN, HFI_INFORMATION_END},
  1219. };
  1220. inst = get_inst(core, hdr->session_id);
  1221. if (!inst) {
  1222. d_vpr_e("%s: invalid params\n", __func__);
  1223. return -EINVAL;
  1224. }
  1225. mutex_lock(&inst->lock);
  1226. hfi_cmd_type = 0;
  1227. hfi_port = 0;
  1228. pkt = (u8 *)((u8 *)hdr + sizeof(struct hfi_header));
  1229. start_pkt = pkt;
  1230. /* validate all packets */
  1231. for (i = 0; i < hdr->num_packets; i++) {
  1232. packet = (struct hfi_packet * ) pkt;
  1233. if (validate_packet(pkt, core->response_packet,
  1234. core->packet_size, __func__)) {
  1235. rc = -EINVAL;
  1236. goto exit;
  1237. }
  1238. pkt += packet->size;
  1239. }
  1240. pkt = start_pkt;
  1241. for (j = 0; j < hdr->num_packets; j++) {
  1242. packet = (struct hfi_packet * ) pkt;
  1243. if (packet->type == HFI_CMD_SETTINGS_CHANGE) {
  1244. if (packet->port == HFI_PORT_BITSTREAM)
  1245. rc = queue_response_work(inst,
  1246. RESP_WORK_INPUT_PSC,
  1247. (void *)hdr, hdr->size);
  1248. else if (packet->port == HFI_PORT_RAW)
  1249. rc = queue_response_work(inst,
  1250. RESP_WORK_OUTPUT_PSC,
  1251. (void *)hdr, hdr->size);
  1252. goto exit;
  1253. } else if (packet->type == HFI_CMD_BUFFER &&
  1254. packet->port == HFI_PORT_RAW &&
  1255. check_last_flag(inst, packet)) {
  1256. rc = queue_response_work(inst,
  1257. RESP_WORK_LAST_FLAG,
  1258. (void *)hdr, hdr->size);
  1259. goto exit;
  1260. }
  1261. pkt += packet->size;
  1262. }
  1263. memset(&inst->frame_prop, 0, sizeof(struct msm_vidc_frame_properties));
  1264. for (i = 0; i < ARRAY_SIZE(be); i++) {
  1265. pkt = start_pkt;
  1266. for (j = 0; j < hdr->num_packets; j++) {
  1267. packet = (struct hfi_packet * ) pkt;
  1268. if (packet->type > be[i].begin && packet->type < be[i].end) {
  1269. hfi_cmd_type = packet->type;
  1270. rc = process_response_packet(inst, packet);
  1271. if (rc)
  1272. goto exit;
  1273. }
  1274. pkt += packet->size;
  1275. }
  1276. }
  1277. if (hfi_cmd_type == HFI_CMD_BUFFER) {
  1278. rc = handle_dequeue_buffers(inst);
  1279. if (rc)
  1280. goto exit;
  1281. }
  1282. memset(&inst->frame_prop, 0, sizeof(struct msm_vidc_frame_properties));
  1283. exit:
  1284. mutex_unlock(&inst->lock);
  1285. put_inst(inst);
  1286. return rc;
  1287. }
  1288. int handle_response(struct msm_vidc_core *core, void *response)
  1289. {
  1290. struct hfi_header *hdr;
  1291. if (!core || !response) {
  1292. d_vpr_e("%s: invalid params\n", __func__);
  1293. return -EINVAL;
  1294. }
  1295. hdr = (struct hfi_header *)response;
  1296. if (validate_packet((u8 *)hdr, core->response_packet,
  1297. core->packet_size, __func__))
  1298. return -EINVAL;
  1299. if (!hdr->session_id)
  1300. return handle_system_response(core, hdr);
  1301. else
  1302. return handle_session_response(core, hdr);
  1303. return 0;
  1304. }