venus_hfi_response.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. u32 vidc_port_from_hfi(struct msm_vidc_inst *inst,
  12. enum hfi_packet_port_type hfi_port)
  13. {
  14. enum msm_vidc_port_type port = MAX_PORT;
  15. if (is_decode_session(inst)) {
  16. switch (hfi_port) {
  17. case HFI_PORT_BITSTREAM:
  18. port = INPUT_PORT;
  19. break;
  20. case HFI_PORT_RAW:
  21. port = OUTPUT_PORT;
  22. break;
  23. default:
  24. s_vpr_e(inst->sid, "%s: invalid hfi port type %d\n",
  25. __func__, hfi_port);
  26. break;
  27. }
  28. } else if (is_encode_session(inst)) {
  29. switch (hfi_port) {
  30. case HFI_PORT_RAW:
  31. port = INPUT_PORT;
  32. break;
  33. case HFI_PORT_BITSTREAM:
  34. port = OUTPUT_PORT;
  35. break;
  36. default:
  37. s_vpr_e(inst->sid, "%s: invalid hfi port type %d\n",
  38. __func__, hfi_port);
  39. break;
  40. }
  41. } else {
  42. s_vpr_e(inst->sid, "%s: invalid domain %#x\n",
  43. __func__, inst->domain);
  44. }
  45. return port;
  46. }
  47. bool is_valid_hfi_port(struct msm_vidc_inst *inst, u32 port,
  48. const char *func)
  49. {
  50. if (!inst) {
  51. s_vpr_e(inst->sid, "%s: invalid params\n", func);
  52. return false;
  53. }
  54. if (port != HFI_PORT_BITSTREAM && port != HFI_PORT_RAW) {
  55. s_vpr_e(inst->sid, "%s: invalid port %#x\n", func, port);
  56. return false;
  57. }
  58. return true;
  59. }
  60. bool is_valid_hfi_buffer_type(struct msm_vidc_inst *inst,
  61. u32 buffer_type, const char *func)
  62. {
  63. if (!inst) {
  64. s_vpr_e(inst->sid, "%s: invalid params\n", func);
  65. return false;
  66. }
  67. if (buffer_type != HFI_BUFFER_BITSTREAM &&
  68. buffer_type != HFI_BUFFER_RAW &&
  69. buffer_type != HFI_BUFFER_METADATA &&
  70. buffer_type != HFI_BUFFER_BIN &&
  71. buffer_type != HFI_BUFFER_ARP &&
  72. buffer_type != HFI_BUFFER_COMV &&
  73. buffer_type != HFI_BUFFER_NON_COMV &&
  74. buffer_type != HFI_BUFFER_LINE &&
  75. buffer_type != HFI_BUFFER_DPB &&
  76. buffer_type != HFI_BUFFER_PERSIST) {
  77. s_vpr_e(inst->sid, "%s: invalid buffer type %#x\n",
  78. func, buffer_type);
  79. return false;
  80. }
  81. return true;
  82. }
  83. static int signal_session_msg_receipt(struct msm_vidc_inst *inst,
  84. enum signal_session_response cmd)
  85. {
  86. if (cmd < MAX_SIGNAL)
  87. complete(&inst->completions[cmd]);
  88. return 0;
  89. }
  90. int validate_packet(u8 *response_pkt, u8 *core_resp_pkt,
  91. u32 core_resp_pkt_size, const char *func)
  92. {
  93. u8 *response_limit;
  94. u32 response_pkt_size = 0;
  95. if (!response_pkt || !core_resp_pkt || !core_resp_pkt_size) {
  96. d_vpr_e("%s: invalid params\n", func);
  97. return -EINVAL;
  98. }
  99. response_limit = core_resp_pkt + core_resp_pkt_size -
  100. max(sizeof(struct hfi_header), sizeof(struct hfi_packet));
  101. if (response_pkt < core_resp_pkt || response_pkt > response_limit) {
  102. d_vpr_e("%s: invalid packet address\n", func);
  103. return -EINVAL;
  104. }
  105. response_pkt_size = *(u32 *)response_pkt;
  106. if (!response_pkt_size) {
  107. d_vpr_e("%s: response packet size cannot be zero\n", func);
  108. return -EINVAL;
  109. }
  110. if (response_pkt + response_pkt_size > response_limit) {
  111. d_vpr_e("%s: invalid packet size %d\n",
  112. func, *(u32 *)response_pkt);
  113. return -EINVAL;
  114. }
  115. return 0;
  116. }
  117. static int handle_session_info(struct msm_vidc_inst *inst,
  118. struct hfi_packet *pkt)
  119. {
  120. int rc = 0;
  121. char *info;
  122. switch (pkt->type) {
  123. case HFI_INFO_UNSUPPORTED:
  124. info = "unsupported";
  125. break;
  126. case HFI_INFO_DATA_CORRUPT:
  127. info = "data corrupt";
  128. break;
  129. default:
  130. info = "unknown";
  131. break;
  132. }
  133. s_vpr_e(inst->sid, "session info (%#x): %s\n", pkt->type, info);
  134. return rc;
  135. }
  136. static int handle_session_error(struct msm_vidc_inst *inst,
  137. struct hfi_packet *pkt)
  138. {
  139. int rc = 0;
  140. char *error;
  141. switch (pkt->type) {
  142. case HFI_ERROR_MAX_SESSIONS:
  143. error = "exceeded max sessions";
  144. break;
  145. case HFI_ERROR_UNKNOWN_SESSION:
  146. error = "unknown session id";
  147. break;
  148. case HFI_ERROR_INVALID_STATE:
  149. error = "invalid operation for current state";
  150. break;
  151. case HFI_ERROR_INSUFFICIENT_RESOURCES:
  152. error = "insufficient resources";
  153. break;
  154. case HFI_ERROR_BUFFER_NOT_SET:
  155. error = "internal buffers not set";
  156. break;
  157. case HFI_ERROR_FATAL:
  158. error = "fatal error";
  159. break;
  160. default:
  161. error = "unknown";
  162. break;
  163. }
  164. s_vpr_e(inst->sid, "session error (%#x): %s\n", pkt->type, error);
  165. rc = msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  166. return rc;
  167. }
  168. static int handle_system_error(struct msm_vidc_core *core,
  169. struct hfi_packet *pkt)
  170. {
  171. mutex_lock(&core->lock);
  172. if (core->state == MSM_VIDC_CORE_DEINIT) {
  173. d_vpr_e("%s: core already deinitialized\n", __func__);
  174. mutex_unlock(&core->lock);
  175. return 0;
  176. }
  177. d_vpr_e("%s: system error received\n", __func__);
  178. core->state = MSM_VIDC_CORE_DEINIT;
  179. mutex_unlock(&core->lock);
  180. return 0;
  181. }
  182. static int handle_system_init(struct msm_vidc_core *core,
  183. struct hfi_packet *pkt)
  184. {
  185. if (pkt->flags & HFI_FW_FLAGS_SYSTEM_ERROR) {
  186. d_vpr_e("%s: received system error\n", __func__);
  187. return 0;
  188. }
  189. if (pkt->flags & HFI_FW_FLAGS_SUCCESS) {
  190. d_vpr_h("%s: successful\n", __func__);
  191. complete(&core->init_done);
  192. }
  193. return 0;
  194. }
  195. static int handle_session_open(struct msm_vidc_inst *inst,
  196. struct hfi_packet *pkt)
  197. {
  198. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  199. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  200. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  201. }
  202. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  203. s_vpr_h(inst->sid, "%s: successful\n", __func__);
  204. return 0;
  205. }
  206. static int handle_session_close(struct msm_vidc_inst *inst,
  207. struct hfi_packet *pkt)
  208. {
  209. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  210. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  211. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  212. }
  213. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  214. s_vpr_h(inst->sid, "%s: successful\n", __func__);
  215. signal_session_msg_receipt(inst, SIGNAL_CMD_CLOSE);
  216. return 0;
  217. }
  218. static int handle_session_start(struct msm_vidc_inst *inst,
  219. struct hfi_packet *pkt)
  220. {
  221. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  222. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  223. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  224. }
  225. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  226. s_vpr_h(inst->sid, "%s: successful for port %d\n",
  227. __func__, pkt->port);
  228. return 0;
  229. }
  230. static int handle_session_stop(struct msm_vidc_inst *inst,
  231. struct hfi_packet *pkt)
  232. {
  233. int signal_type = -1;
  234. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  235. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  236. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  237. }
  238. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  239. s_vpr_h(inst->sid, "%s: successful for port %d\n",
  240. __func__, pkt->port);
  241. if (is_encode_session(inst)) {
  242. if (pkt->port == HFI_PORT_RAW) {
  243. signal_type = SIGNAL_CMD_STOP_INPUT;
  244. } else if (pkt->port == HFI_PORT_BITSTREAM) {
  245. signal_type = SIGNAL_CMD_STOP_OUTPUT;
  246. } else {
  247. s_vpr_e(inst->sid, "%s: invalid port: %d\n",
  248. __func__, pkt->port);
  249. return -EINVAL;
  250. }
  251. } else if (is_decode_session(inst)) {
  252. if (pkt->port == HFI_PORT_RAW) {
  253. signal_type = SIGNAL_CMD_STOP_OUTPUT;
  254. } else if (pkt->port == HFI_PORT_BITSTREAM) {
  255. signal_type = SIGNAL_CMD_STOP_INPUT;
  256. } else {
  257. s_vpr_e(inst->sid, "%s: invalid port: %d\n",
  258. __func__, pkt->port);
  259. return -EINVAL;
  260. }
  261. } else {
  262. s_vpr_e(inst->sid, "%s: invalid session\n", __func__);
  263. return -EINVAL;
  264. }
  265. if (signal_type != -1)
  266. signal_session_msg_receipt(inst, signal_type);
  267. return 0;
  268. }
  269. static int handle_session_drain(struct msm_vidc_inst *inst,
  270. struct hfi_packet *pkt)
  271. {
  272. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  273. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  274. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  275. }
  276. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  277. s_vpr_h(inst->sid, "%s: successful\n", __func__);
  278. return 0;
  279. }
  280. static int handle_input_buffer(struct msm_vidc_inst *inst,
  281. struct hfi_buffer *buffer)
  282. {
  283. int rc = 0;
  284. struct msm_vidc_buffers *buffers;
  285. struct msm_vidc_buffer *buf;
  286. bool found;
  287. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_INPUT, __func__);
  288. if (!buffers)
  289. return -EINVAL;
  290. found = false;
  291. list_for_each_entry(buf, &buffers->list, list) {
  292. if (buf->device_addr == buffer->base_address) {
  293. found = true;
  294. break;
  295. }
  296. }
  297. if (!found) {
  298. s_vpr_e(inst->sid, "%s: buffer not found for idx %d addr %#x\n",
  299. __func__, buffer->index, buffer->base_address);
  300. return -EINVAL;
  301. }
  302. buf->data_offset = buffer->data_offset;
  303. buf->data_size = buffer->data_size;
  304. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  305. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  306. buf->flags = 0;
  307. //todo:
  308. /*if (buffer->flags & HFI_BUF_FW_FLAG_CORRUPT) {
  309. s_vpr_h(inst->sid, "%s: data corrupted\n", __func__);
  310. buf->flags |= MSM_VIDC_BUF_FLAG_ERROR;
  311. }
  312. if (buffer->flags & HFI_BUF_FW_FLAG_UNSUPPORTED) {
  313. s_vpr_e(inst->sid, "%s: unsupported input\n", __func__);
  314. buf->flags |= MSM_VIDC_BUF_FLAG_ERROR;
  315. // TODO: move inst->state to error state
  316. }*/
  317. print_vidc_buffer(VIDC_HIGH, "EBD", inst, buf);
  318. return rc;
  319. }
  320. static int handle_output_buffer(struct msm_vidc_inst *inst,
  321. struct hfi_buffer *buffer)
  322. {
  323. int rc = 0;
  324. struct msm_vidc_buffers *buffers;
  325. struct msm_vidc_buffer *buf;
  326. bool found;
  327. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_OUTPUT, __func__);
  328. if (!buffers)
  329. return -EINVAL;
  330. found = false;
  331. list_for_each_entry(buf, &buffers->list, list) {
  332. if (buf->device_addr == buffer->base_address) {
  333. found = true;
  334. break;
  335. }
  336. }
  337. if (!found) {
  338. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  339. __func__, buffer->index, buffer->base_address);
  340. return -EINVAL;
  341. }
  342. buf->data_offset = buffer->data_offset;
  343. buf->data_size = buffer->data_size;
  344. buf->timestamp = buffer->timestamp;
  345. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  346. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  347. if (buffer->flags & HFI_BUF_FW_FLAG_READONLY)
  348. buf->attr |= MSM_VIDC_ATTR_READ_ONLY;
  349. else
  350. buf->attr &= ~MSM_VIDC_ATTR_READ_ONLY;
  351. buf->flags = 0;
  352. //todo: moved to HFI_PROP_PICTURE_TYPE
  353. /*if (buffer->flags & HFI_BUF_FW_FLAG_KEYFRAME)
  354. buf->flags |= MSM_VIDC_BUF_FLAG_KEYFRAME;*/
  355. if (buffer->flags & HFI_BUF_FW_FLAG_LAST)
  356. buf->flags |= MSM_VIDC_BUF_FLAG_LAST;
  357. //moved to HFI_INFO_DATA_CORRUPT
  358. /*if (buffer->flags & HFI_BUF_FW_FLAG_CORRUPT)
  359. buf->flags |= MSM_VIDC_BUF_FLAG_ERROR;*/
  360. if (buffer->flags & HFI_BUF_FW_FLAG_CODEC_CONFIG)
  361. buf->flags |= MSM_VIDC_BUF_FLAG_CODECCONFIG;
  362. //moved to HFI_PROP_SUBFRAME_OUTPUT
  363. /*if (buffer->flags & HFI_BUF_FW_FLAG_SUBFRAME)
  364. buf->flags |= MSM_VIDC_BUF_FLAG_SUBFRAME;*/
  365. print_vidc_buffer(VIDC_HIGH, "FBD", inst, buf);
  366. return rc;
  367. }
  368. static int handle_input_metadata_buffer(struct msm_vidc_inst *inst,
  369. struct hfi_buffer *buffer)
  370. {
  371. int rc = 0;
  372. struct msm_vidc_buffers *buffers;
  373. struct msm_vidc_buffer *buf;
  374. bool found;
  375. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_INPUT_META, __func__);
  376. if (!buffers)
  377. return -EINVAL;
  378. found = false;
  379. list_for_each_entry(buf, &buffers->list, list) {
  380. if (buf->device_addr == buffer->base_address) {
  381. found = true;
  382. break;
  383. }
  384. }
  385. if (!found) {
  386. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  387. __func__, buffer->index, buffer->base_address);
  388. return -EINVAL;
  389. }
  390. buf->data_size = buffer->data_size;
  391. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  392. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  393. buf->flags = 0;
  394. if (buffer->flags & HFI_BUF_FW_FLAG_LAST)
  395. buf->flags |= MSM_VIDC_BUF_FLAG_LAST;
  396. print_vidc_buffer(VIDC_HIGH, "EBD META", inst, buf);
  397. return rc;
  398. }
  399. static int handle_output_metadata_buffer(struct msm_vidc_inst *inst,
  400. struct hfi_buffer *buffer)
  401. {
  402. int rc = 0;
  403. struct msm_vidc_buffers *buffers;
  404. struct msm_vidc_buffer *buf;
  405. bool found;
  406. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_OUTPUT_META, __func__);
  407. if (!buffers)
  408. return -EINVAL;
  409. found = false;
  410. list_for_each_entry(buf, &buffers->list, list) {
  411. if (buf->device_addr == buffer->base_address) {
  412. found = true;
  413. break;
  414. }
  415. }
  416. if (!found) {
  417. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  418. __func__, buffer->index, buffer->base_address);
  419. return -EINVAL;
  420. }
  421. buf->data_size = buffer->data_size;
  422. buf->attr &= ~MSM_VIDC_ATTR_QUEUED;
  423. buf->attr |= MSM_VIDC_ATTR_DEQUEUED;
  424. buf->flags = 0;
  425. if (buffer->flags & HFI_BUF_FW_FLAG_LAST)
  426. buf->flags |= MSM_VIDC_BUF_FLAG_LAST;
  427. print_vidc_buffer(VIDC_HIGH, "FBD META", inst, buf);
  428. return rc;
  429. }
  430. static int handle_dequeue_buffers(struct msm_vidc_inst* inst)
  431. {
  432. int rc = 0;
  433. int i;
  434. struct msm_vidc_buffers* buffers;
  435. struct msm_vidc_buffer* buf;
  436. struct msm_vidc_buffer* dummy;
  437. enum msm_vidc_buffer_type buffer_type[] = {
  438. MSM_VIDC_BUF_INPUT_META,
  439. MSM_VIDC_BUF_INPUT,
  440. MSM_VIDC_BUF_OUTPUT_META,
  441. MSM_VIDC_BUF_OUTPUT,
  442. };
  443. for (i = 0; i < ARRAY_SIZE(buffer_type); i++) {
  444. buffers = msm_vidc_get_buffers(inst, buffer_type[i], __func__);
  445. if (!buffers)
  446. return -EINVAL;
  447. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  448. if (buf->attr & MSM_VIDC_ATTR_DEQUEUED) {
  449. buf->attr &= ~MSM_VIDC_ATTR_DEQUEUED;
  450. msm_vidc_vb2_buffer_done(inst, buf);
  451. msm_vidc_put_driver_buf(inst, buf);
  452. }
  453. }
  454. }
  455. return rc;
  456. }
  457. static int handle_persist_buffer(struct msm_vidc_inst *inst,
  458. struct hfi_buffer *buffer)
  459. {
  460. int rc = 0;
  461. struct msm_vidc_buffers *buffers;
  462. struct msm_vidc_buffer *buf;
  463. bool found;
  464. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_PERSIST, __func__);
  465. if (!buffers)
  466. return -EINVAL;
  467. found = false;
  468. list_for_each_entry(buf, &buffers->list, list) {
  469. if (buf->device_addr == buffer->base_address) {
  470. found = true;
  471. break;
  472. }
  473. }
  474. if (found) {
  475. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  476. } else {
  477. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  478. __func__, buffer->index, buffer->base_address);
  479. return -EINVAL;
  480. }
  481. return rc;
  482. }
  483. static int handle_line_buffer(struct msm_vidc_inst *inst,
  484. struct hfi_buffer *buffer)
  485. {
  486. int rc = 0;
  487. struct msm_vidc_buffers *buffers;
  488. struct msm_vidc_buffer *buf;
  489. bool found;
  490. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_LINE, __func__);
  491. if (!buffers)
  492. return -EINVAL;
  493. found = false;
  494. list_for_each_entry(buf, &buffers->list, list) {
  495. if (buf->device_addr == buffer->base_address) {
  496. found = true;
  497. break;
  498. }
  499. }
  500. if (found) {
  501. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  502. } else {
  503. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  504. __func__, buffer->index, buffer->base_address);
  505. return -EINVAL;
  506. }
  507. return rc;
  508. }
  509. static int handle_non_comv_buffer(struct msm_vidc_inst *inst,
  510. struct hfi_buffer *buffer)
  511. {
  512. int rc = 0;
  513. struct msm_vidc_buffers *buffers;
  514. struct msm_vidc_buffer *buf;
  515. bool found;
  516. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_NON_COMV, __func__);
  517. if (!buffers)
  518. return -EINVAL;
  519. found = false;
  520. list_for_each_entry(buf, &buffers->list, list) {
  521. if (buf->device_addr == buffer->base_address) {
  522. found = true;
  523. break;
  524. }
  525. }
  526. if (found) {
  527. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  528. } else {
  529. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  530. __func__, buffer->index, buffer->base_address);
  531. return -EINVAL;
  532. }
  533. return rc;
  534. }
  535. static int handle_comv_buffer(struct msm_vidc_inst *inst,
  536. struct hfi_buffer *buffer)
  537. {
  538. int rc = 0;
  539. struct msm_vidc_buffers *buffers;
  540. struct msm_vidc_buffer *buf;
  541. bool found;
  542. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_COMV, __func__);
  543. if (!buffers)
  544. return -EINVAL;
  545. found = false;
  546. list_for_each_entry(buf, &buffers->list, list) {
  547. if (buf->device_addr == buffer->base_address) {
  548. found = true;
  549. break;
  550. }
  551. }
  552. if (found) {
  553. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  554. } else {
  555. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  556. __func__, buffer->index, buffer->base_address);
  557. return -EINVAL;
  558. }
  559. return rc;
  560. }
  561. static int handle_bin_buffer(struct msm_vidc_inst *inst,
  562. struct hfi_buffer *buffer)
  563. {
  564. int rc = 0;
  565. struct msm_vidc_buffers *buffers;
  566. struct msm_vidc_buffer *buf;
  567. bool found;
  568. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_BIN, __func__);
  569. if (!buffers)
  570. return -EINVAL;
  571. found = false;
  572. list_for_each_entry(buf, &buffers->list, list) {
  573. if (buf->device_addr == buffer->base_address) {
  574. found = true;
  575. break;
  576. }
  577. }
  578. if (found) {
  579. rc = msm_vidc_destroy_internal_buffer(inst, buf);
  580. } else {
  581. s_vpr_e(inst->sid, "%s: invalid idx %d daddr %#x\n",
  582. __func__, buffer->index, buffer->base_address);
  583. return -EINVAL;
  584. }
  585. return rc;
  586. }
  587. static int handle_session_buffer(struct msm_vidc_inst *inst,
  588. struct hfi_packet *pkt)
  589. {
  590. int rc = 0;
  591. struct hfi_buffer *buffer;
  592. u32 buf_type = 0, port_type = 0;
  593. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  594. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  595. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  596. return 0;
  597. }
  598. port_type = pkt->port;
  599. if (!is_valid_hfi_port(inst, port_type, __func__)) {
  600. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  601. return 0;
  602. }
  603. buffer = (struct hfi_buffer *)((u8 *)pkt + sizeof(struct hfi_packet));
  604. buf_type = buffer->type;
  605. if (!is_valid_hfi_buffer_type(inst, buf_type, __func__)) {
  606. /* TODO */
  607. //msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  608. return 0;
  609. }
  610. if (is_encode_session(inst)) {
  611. if (port_type == HFI_PORT_BITSTREAM) {
  612. if (buf_type == HFI_BUFFER_METADATA)
  613. rc = handle_output_metadata_buffer(inst, buffer);
  614. else if (buf_type == HFI_BUFFER_BITSTREAM)
  615. rc = handle_output_buffer(inst, buffer);
  616. } else if (port_type == HFI_PORT_RAW) {
  617. if (buf_type == HFI_BUFFER_METADATA)
  618. rc = handle_input_metadata_buffer(inst, buffer);
  619. else if (buf_type == HFI_BUFFER_RAW)
  620. rc = handle_input_buffer(inst, buffer);
  621. }
  622. } else if (is_decode_session(inst)) {
  623. if (port_type == HFI_PORT_BITSTREAM) {
  624. if (buf_type == HFI_BUFFER_METADATA)
  625. rc = handle_input_metadata_buffer(inst, buffer);
  626. else if (buf_type == HFI_BUFFER_BITSTREAM)
  627. rc = handle_input_buffer(inst, buffer);
  628. else if (buf_type == HFI_BUFFER_BIN)
  629. rc = handle_bin_buffer(inst, buffer);
  630. else if (buf_type == HFI_BUFFER_COMV)
  631. rc = handle_comv_buffer(inst, buffer);
  632. else if (buf_type == HFI_BUFFER_NON_COMV)
  633. rc = handle_non_comv_buffer(inst, buffer);
  634. else if (buf_type == HFI_BUFFER_LINE)
  635. rc = handle_line_buffer(inst, buffer);
  636. else if (buf_type == HFI_BUFFER_PERSIST)
  637. rc = handle_persist_buffer(inst, buffer);
  638. } else if (port_type == HFI_PORT_RAW) {
  639. if (buf_type == HFI_BUFFER_METADATA)
  640. rc = handle_output_metadata_buffer(inst, buffer);
  641. else if (buf_type == HFI_BUFFER_RAW)
  642. rc = handle_output_buffer(inst, buffer);
  643. }
  644. } else {
  645. s_vpr_e(inst->sid, "%s: invalid session %d\n",
  646. __func__, inst->domain);
  647. return -EINVAL;
  648. }
  649. return rc;
  650. }
  651. static int handle_port_settings_change(struct msm_vidc_inst *inst,
  652. struct hfi_packet *pkt)
  653. {
  654. s_vpr_h(inst->sid, "%s: Received port settings change, type %d\n",
  655. __func__, pkt->port);
  656. return 0;
  657. }
  658. static int handle_session_subscribe_mode(struct msm_vidc_inst *inst,
  659. struct hfi_packet *pkt)
  660. {
  661. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  662. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  663. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  664. }
  665. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  666. s_vpr_h(inst->sid, "%s: successful\n", __func__);
  667. return 0;
  668. }
  669. static int handle_session_delivery_mode(struct msm_vidc_inst *inst,
  670. struct hfi_packet *pkt)
  671. {
  672. if (pkt->flags & HFI_FW_FLAGS_SESSION_ERROR) {
  673. s_vpr_e(inst->sid, "%s: received session error\n", __func__);
  674. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  675. }
  676. if (pkt->flags & HFI_FW_FLAGS_SUCCESS)
  677. s_vpr_h(inst->sid, "%s: successful\n", __func__);
  678. return 0;
  679. }
  680. static int handle_session_command(struct msm_vidc_inst *inst,
  681. struct hfi_packet *pkt)
  682. {
  683. switch (pkt->type) {
  684. case HFI_CMD_OPEN:
  685. return handle_session_open(inst, pkt);
  686. case HFI_CMD_CLOSE:
  687. return handle_session_close(inst, pkt);
  688. case HFI_CMD_START:
  689. return handle_session_start(inst, pkt);
  690. case HFI_CMD_STOP:
  691. return handle_session_stop(inst, pkt);
  692. case HFI_CMD_DRAIN:
  693. return handle_session_drain(inst, pkt);
  694. case HFI_CMD_BUFFER:
  695. return handle_session_buffer(inst, pkt);
  696. case HFI_CMD_SETTINGS_CHANGE:
  697. return handle_port_settings_change(inst, pkt);
  698. case HFI_CMD_SUBSCRIBE_MODE:
  699. return handle_session_subscribe_mode(inst, pkt);
  700. case HFI_CMD_DELIVERY_MODE:
  701. return handle_session_delivery_mode(inst, pkt);
  702. default:
  703. s_vpr_e(inst->sid, "%s: Unsupported command type: %#x\n",
  704. __func__, pkt->type);
  705. return -EINVAL;
  706. }
  707. return 0;
  708. }
  709. static int handle_session_property(struct msm_vidc_inst *inst,
  710. struct hfi_packet *pkt)
  711. {
  712. u32 port;
  713. u32 *payload_ptr;
  714. s_vpr_h(inst->sid, "%s: property type %#x\n", __func__, pkt->type);
  715. port = vidc_port_from_hfi(inst, pkt->port);
  716. payload_ptr = (u32 *)((u8 *)pkt + sizeof(struct hfi_packet));
  717. switch (pkt->type) {
  718. case HFI_PROP_BITSTREAM_RESOLUTION:
  719. inst->subcr_params[port].bitstream_resolution = payload_ptr[0];
  720. break;
  721. case HFI_PROP_CROP_OFFSETS:
  722. inst->subcr_params[port].crop_offsets =
  723. (u64)payload_ptr[0] << 32 | payload_ptr[1];
  724. break;
  725. case HFI_PROP_LUMA_CHROMA_BIT_DEPTH:
  726. inst->subcr_params[port].bit_depth = payload_ptr[0];
  727. break;
  728. case HFI_PROP_CABAC_SESSION:
  729. inst->subcr_params[port].cabac = payload_ptr[0];
  730. break;
  731. case HFI_PROP_CODED_FRAMES:
  732. inst->subcr_params[port].coded_frames = payload_ptr[0];
  733. break;
  734. case HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT:
  735. inst->subcr_params[port].fw_min_count = payload_ptr[0];
  736. break;
  737. case HFI_PROP_PIC_ORDER_CNT_TYPE:
  738. inst->subcr_params[port].pic_order_cnt = payload_ptr[0];
  739. break;
  740. case HFI_PROP_SIGNAL_COLOR_INFO:
  741. inst->subcr_params[port].color_info = payload_ptr[0];
  742. break;
  743. case HFI_PROP_PROFILE:
  744. inst->subcr_params[port].profile = payload_ptr[0];
  745. break;
  746. case HFI_PROP_LEVEL:
  747. inst->subcr_params[port].level = payload_ptr[0];
  748. break;
  749. case HFI_PROP_TIER:
  750. inst->subcr_params[port].tier = payload_ptr[0];
  751. break;
  752. default:
  753. s_vpr_e(inst->sid, "%s: invalid port settings property %#x\n",
  754. __func__, pkt->type);
  755. return -EINVAL;
  756. }
  757. return 0;
  758. }
  759. static int handle_image_version_property(struct hfi_packet *pkt)
  760. {
  761. u32 i = 0;
  762. char version[256];
  763. const u32 version_string_size = 128;
  764. u8 *str_image_version;
  765. u32 req_bytes;
  766. req_bytes = pkt->size - sizeof(*pkt);
  767. if (req_bytes < version_string_size) {
  768. d_vpr_e("%s: bad_pkt: %d\n", __func__, req_bytes);
  769. return -EINVAL;
  770. }
  771. str_image_version = (u8 *)pkt + sizeof(struct hfi_packet);
  772. /*
  773. * The version string returned by firmware includes null
  774. * characters at the start and in between. Replace the null
  775. * characters with space, to print the version info.
  776. */
  777. for (i = 0; i < version_string_size; i++) {
  778. if (str_image_version[i] != '\0')
  779. version[i] = str_image_version[i];
  780. else
  781. version[i] = ' ';
  782. }
  783. version[i] = '\0';
  784. d_vpr_h("%s: F/W version: %s\n", __func__, version);
  785. return 0;
  786. }
  787. static int handle_system_property(struct msm_vidc_core *core,
  788. struct hfi_packet *pkt)
  789. {
  790. int rc = 0;
  791. if (pkt->flags & HFI_FW_FLAGS_SYSTEM_ERROR) {
  792. d_vpr_e("%s: received system error for property type %#x\n",
  793. __func__, pkt->type);
  794. return handle_system_error(core, pkt);
  795. }
  796. switch (pkt->type) {
  797. case HFI_PROP_IMAGE_VERSION:
  798. rc = handle_image_version_property(pkt);
  799. break;
  800. default:
  801. d_vpr_h("%s: property type %#x successful\n",
  802. __func__, pkt->type);
  803. break;
  804. }
  805. return rc;
  806. }
  807. static int handle_system_response(struct msm_vidc_core *core,
  808. struct hfi_header *hdr)
  809. {
  810. int rc = 0;
  811. struct hfi_packet *packet;
  812. u8 *pkt;
  813. int i;
  814. pkt = (u8 *)((u8 *)hdr + sizeof(struct hfi_header));
  815. for (i = 0; i < hdr->num_packets; i++) {
  816. if (validate_packet((u8 *)pkt, core->response_packet,
  817. core->packet_size, __func__)) {
  818. rc = -EINVAL;
  819. goto exit;
  820. }
  821. packet = (struct hfi_packet *)pkt;
  822. if (packet->type == HFI_CMD_INIT) {
  823. rc = handle_system_init(core, packet);
  824. } else if (packet->type > HFI_SYSTEM_ERROR_BEGIN &&
  825. packet->type < HFI_SYSTEM_ERROR_END) {
  826. rc = handle_system_error(core, packet);
  827. } else if (packet->type > HFI_PROP_BEGIN &&
  828. packet->type < HFI_PROP_CODEC) {
  829. rc = handle_system_property(core, packet);
  830. } else {
  831. d_vpr_e("%s: Unknown packet type: %#x\n",
  832. __func__, packet->type);
  833. rc = -EINVAL;
  834. goto exit;
  835. }
  836. pkt += packet->size;
  837. }
  838. exit:
  839. return rc;
  840. }
  841. static int handle_session_response(struct msm_vidc_core *core,
  842. struct hfi_header *hdr)
  843. {
  844. int rc = 0;
  845. struct msm_vidc_inst *inst;
  846. struct hfi_packet *packet;
  847. u8 *pkt;
  848. u32 hfi_cmd_type = 0;
  849. u32 hfi_port = 0;
  850. int i;
  851. inst = get_inst(core, hdr->session_id);
  852. if (!inst) {
  853. d_vpr_e("%s: invalid params\n", __func__);
  854. return -EINVAL;
  855. }
  856. mutex_lock(&inst->lock);
  857. hfi_cmd_type = 0;
  858. hfi_port = 0;
  859. pkt = (u8 *)((u8 *)hdr + sizeof(struct hfi_header));
  860. for (i = 0; i < hdr->num_packets; i++) {
  861. if (validate_packet(pkt, core->response_packet,
  862. core->packet_size, __func__)) {
  863. rc = -EINVAL;
  864. goto exit;
  865. }
  866. packet = (struct hfi_packet *)pkt;
  867. if (packet->type > HFI_CMD_BEGIN &&
  868. packet->type < HFI_CMD_END) {
  869. if (hfi_cmd_type == HFI_CMD_SETTINGS_CHANGE) {
  870. s_vpr_e(inst->sid,
  871. "%s: invalid packet type %d in port settings change\n",
  872. __func__, packet->type);
  873. rc = -EINVAL;
  874. goto exit;
  875. }
  876. hfi_cmd_type = packet->type;
  877. hfi_port = packet->port;
  878. rc = handle_session_command(inst, packet);
  879. } else if (packet->type > HFI_PROP_BEGIN &&
  880. packet->type < HFI_PROP_END) {
  881. rc = handle_session_property(inst, packet);
  882. } else if (packet->type > HFI_SESSION_ERROR_BEGIN &&
  883. packet->type < HFI_SESSION_ERROR_END) {
  884. rc = handle_session_error(inst, packet);
  885. } else if (packet->type > HFI_INFORMATION_BEGIN &&
  886. packet->type < HFI_INFORMATION_END) {
  887. rc = handle_session_info(inst, packet);
  888. } else {
  889. s_vpr_e(inst->sid, "%s: Unknown packet type: %#x\n",
  890. __func__, packet->type);
  891. rc = -EINVAL;
  892. goto exit;
  893. }
  894. pkt += packet->size;
  895. }
  896. if (hfi_cmd_type == HFI_CMD_BUFFER) {
  897. rc = handle_dequeue_buffers(inst);
  898. if (rc)
  899. goto exit;
  900. }
  901. if (hfi_cmd_type == HFI_CMD_SETTINGS_CHANGE) {
  902. if (hfi_port == HFI_PORT_BITSTREAM) {
  903. rc = msm_vdec_input_port_settings_change(inst);
  904. if (rc)
  905. goto exit;
  906. } else if (hfi_port == HFI_PORT_RAW) {
  907. rc = msm_vdec_output_port_settings_change(inst);
  908. if (rc)
  909. goto exit;
  910. }
  911. }
  912. exit:
  913. mutex_unlock(&inst->lock);
  914. put_inst(inst);
  915. return rc;
  916. }
  917. int handle_response(struct msm_vidc_core *core, void *response)
  918. {
  919. struct hfi_header *hdr;
  920. if (!core || !response) {
  921. d_vpr_e("%s: invalid params\n", __func__);
  922. return -EINVAL;
  923. }
  924. hdr = (struct hfi_header *)response;
  925. if (validate_packet((u8 *)hdr, core->response_packet,
  926. core->packet_size, __func__))
  927. return -EINVAL;
  928. if (!hdr->session_id)
  929. return handle_system_response(core, hdr);
  930. else
  931. return handle_session_response(core, hdr);
  932. return 0;
  933. }