msm_vidc_vb2.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include "msm_vidc_vb2.h"
  6. #include "msm_vidc_core.h"
  7. #include "msm_vidc_inst.h"
  8. #include "msm_vidc_internal.h"
  9. #include "msm_vidc_driver.h"
  10. #include "msm_vidc_power.h"
  11. #include "msm_vdec.h"
  12. #include "msm_venc.h"
  13. #include "msm_vidc_debug.h"
  14. #include "msm_vidc_control.h"
  15. struct vb2_queue *msm_vidc_get_vb2q(struct msm_vidc_inst *inst,
  16. u32 type, const char *func)
  17. {
  18. struct vb2_queue *q = NULL;
  19. if (!inst) {
  20. d_vpr_e("%s: invalid buffer type %d\n", func);
  21. return NULL;
  22. }
  23. if (type == INPUT_MPLANE) {
  24. q = &inst->vb2q[INPUT_PORT];
  25. } else if (type == OUTPUT_MPLANE) {
  26. q = &inst->vb2q[OUTPUT_PORT];
  27. } else if (type == INPUT_META_PLANE) {
  28. q = &inst->vb2q[INPUT_META_PORT];
  29. } else if (type == OUTPUT_META_PLANE) {
  30. q = &inst->vb2q[OUTPUT_META_PORT];
  31. } else {
  32. i_vpr_e(inst, "%s: invalid buffer type %d\n",
  33. __func__, type);
  34. }
  35. return q;
  36. }
  37. void *msm_vb2_get_userptr(struct device *dev, unsigned long vaddr,
  38. unsigned long size, enum dma_data_direction dma_dir)
  39. {
  40. return (void *)0xdeadbeef;
  41. }
  42. void msm_vb2_put_userptr(void *buf_priv)
  43. {
  44. }
  45. void* msm_vb2_attach_dmabuf(struct device* dev, struct dma_buf* dbuf,
  46. unsigned long size, enum dma_data_direction dma_dir)
  47. {
  48. return (void*)0xdeadbeef;
  49. }
  50. void msm_vb2_detach_dmabuf(void* buf_priv)
  51. {
  52. }
  53. int msm_vb2_map_dmabuf(void* buf_priv)
  54. {
  55. return 0;
  56. }
  57. void msm_vb2_unmap_dmabuf(void* buf_priv)
  58. {
  59. }
  60. int msm_vidc_queue_setup(struct vb2_queue *q,
  61. unsigned int *num_buffers, unsigned int *num_planes,
  62. unsigned int sizes[], struct device *alloc_devs[])
  63. {
  64. int rc = 0;
  65. struct msm_vidc_inst *inst;
  66. int port;
  67. if (!q || !num_buffers || !num_planes
  68. || !sizes || !q->drv_priv) {
  69. d_vpr_e("%s: invalid params, q = %pK, %pK, %pK\n",
  70. __func__, q, num_buffers, num_planes);
  71. return -EINVAL;
  72. }
  73. inst = q->drv_priv;
  74. if (!inst || !inst->core) {
  75. d_vpr_e("%s: invalid params %pK\n", __func__, inst);
  76. return -EINVAL;
  77. }
  78. if (inst->state == MSM_VIDC_START) {
  79. i_vpr_e(inst, "%s: invalid state %d\n", __func__, inst->state);
  80. return -EINVAL;
  81. }
  82. port = v4l2_type_to_driver_port(inst, q->type, __func__);
  83. if (port < 0)
  84. return -EINVAL;
  85. if (port == INPUT_PORT) {
  86. *num_planes = 1;
  87. if (*num_buffers < inst->buffers.input.min_count +
  88. inst->buffers.input.extra_count)
  89. *num_buffers = inst->buffers.input.min_count +
  90. inst->buffers.input.extra_count;
  91. inst->buffers.input.actual_count = *num_buffers;
  92. } else if (port == INPUT_META_PORT) {
  93. *num_planes = 1;
  94. if (*num_buffers < inst->buffers.input_meta.min_count +
  95. inst->buffers.input_meta.extra_count)
  96. *num_buffers = inst->buffers.input_meta.min_count +
  97. inst->buffers.input_meta.extra_count;
  98. inst->buffers.input_meta.actual_count = *num_buffers;
  99. } else if (port == OUTPUT_PORT) {
  100. *num_planes = 1;
  101. if (*num_buffers < inst->buffers.output.min_count +
  102. inst->buffers.output.extra_count)
  103. *num_buffers = inst->buffers.output.min_count +
  104. inst->buffers.output.extra_count;
  105. inst->buffers.output.actual_count = *num_buffers;
  106. } else if (port == OUTPUT_META_PORT) {
  107. *num_planes = 1;
  108. if (*num_buffers < inst->buffers.output_meta.min_count +
  109. inst->buffers.output_meta.extra_count)
  110. *num_buffers = inst->buffers.output_meta.min_count +
  111. inst->buffers.output_meta.extra_count;
  112. inst->buffers.output_meta.actual_count = *num_buffers;
  113. }
  114. if (port == INPUT_PORT || port == OUTPUT_PORT) {
  115. sizes[0] = inst->fmts[port].fmt.pix_mp.plane_fmt[0].sizeimage;
  116. } else if (port == OUTPUT_META_PORT) {
  117. sizes[0] = inst->fmts[port].fmt.meta.buffersize;
  118. } else if (port == INPUT_META_PORT) {
  119. if (inst->capabilities->cap[SUPER_FRAME].value)
  120. sizes[0] = inst->capabilities->cap[SUPER_FRAME].value *
  121. inst->fmts[port].fmt.meta.buffersize;
  122. else
  123. sizes[0] = inst->fmts[port].fmt.meta.buffersize;
  124. }
  125. i_vpr_h(inst,
  126. "queue_setup: type %s num_buffers %d sizes[0] %d\n",
  127. v4l2_type_name(q->type), *num_buffers, sizes[0]);
  128. return rc;
  129. }
  130. int msm_vidc_start_streaming(struct vb2_queue *q, unsigned int count)
  131. {
  132. int rc = 0;
  133. struct msm_vidc_inst *inst;
  134. enum msm_vidc_buffer_type buf_type;
  135. if (!q || !q->drv_priv) {
  136. d_vpr_e("%s: invalid input, q = %pK\n", q);
  137. return -EINVAL;
  138. }
  139. inst = q->drv_priv;
  140. if (!inst || !inst->core) {
  141. d_vpr_e("%s: invalid params\n", __func__);
  142. return -EINVAL;
  143. }
  144. if (q->type == INPUT_META_PLANE || q->type == OUTPUT_META_PLANE) {
  145. i_vpr_h(inst, "%s: nothing to start on %s\n",
  146. __func__, v4l2_type_name(q->type));
  147. return 0;
  148. }
  149. if (!is_decode_session(inst) && !is_encode_session(inst)) {
  150. i_vpr_e(inst, "%s: invalid session %d\n",
  151. __func__, inst->domain);
  152. return -EINVAL;
  153. }
  154. i_vpr_h(inst, "Streamon: %s\n", v4l2_type_name(q->type));
  155. if (!inst->once_per_session_set) {
  156. inst->once_per_session_set = true;
  157. rc = msm_vidc_session_set_codec(inst);
  158. if (rc)
  159. return rc;
  160. rc = msm_vidc_session_set_secure_mode(inst);
  161. if (rc)
  162. return rc;
  163. if (is_encode_session(inst)) {
  164. rc = msm_vidc_alloc_and_queue_session_internal_buffers(inst,
  165. MSM_VIDC_BUF_ARP);
  166. if (rc)
  167. goto error;
  168. } else if(is_decode_session(inst)) {
  169. rc = msm_vidc_session_set_default_header(inst);
  170. if (rc)
  171. return rc;
  172. rc = msm_vidc_alloc_and_queue_session_internal_buffers(inst,
  173. MSM_VIDC_BUF_PERSIST);
  174. if (rc)
  175. goto error;
  176. }
  177. }
  178. if (is_decode_session(inst))
  179. inst->decode_batch.enable = msm_vidc_allow_decode_batch(inst);
  180. msm_vidc_allow_dcvs(inst);
  181. msm_vidc_power_data_reset(inst);
  182. if (q->type == INPUT_MPLANE) {
  183. if (is_decode_session(inst))
  184. rc = msm_vdec_streamon_input(inst);
  185. else if (is_encode_session(inst))
  186. rc = msm_venc_streamon_input(inst);
  187. else
  188. goto error;
  189. } else if (q->type == OUTPUT_MPLANE) {
  190. if (is_decode_session(inst))
  191. rc = msm_vdec_streamon_output(inst);
  192. else if (is_encode_session(inst))
  193. rc = msm_venc_streamon_output(inst);
  194. else
  195. goto error;
  196. } else {
  197. i_vpr_e(inst, "%s: invalid type %d\n", q->type);
  198. goto error;
  199. }
  200. if (rc)
  201. goto error;
  202. /* print final buffer counts & size details */
  203. msm_vidc_print_buffer_info(inst);
  204. buf_type = v4l2_type_to_driver(q->type, __func__);
  205. if (!buf_type)
  206. goto error;
  207. /* queue pending buffers */
  208. rc = msm_vidc_queue_deferred_buffers(inst, buf_type);
  209. if (rc)
  210. goto error;
  211. /* initialize statistics timer(one time) */
  212. if (!inst->stats.time_ms)
  213. inst->stats.time_ms = ktime_get_ns() / 1000 / 1000;
  214. /* schedule to print buffer statistics */
  215. rc = schedule_stats_work(inst);
  216. if (rc)
  217. goto error;
  218. if ((q->type == INPUT_MPLANE && inst->vb2q[OUTPUT_PORT].streaming) ||
  219. (q->type == OUTPUT_MPLANE && inst->vb2q[INPUT_PORT].streaming)) {
  220. rc = msm_vidc_get_properties(inst);
  221. if (rc)
  222. goto error;
  223. }
  224. i_vpr_h(inst, "Streamon: %s successful\n", v4l2_type_name(q->type));
  225. return rc;
  226. error:
  227. i_vpr_e(inst, "Streamon: %s failed\n", v4l2_type_name(q->type));
  228. return -EINVAL;
  229. }
  230. void msm_vidc_stop_streaming(struct vb2_queue *q)
  231. {
  232. int rc = 0;
  233. struct msm_vidc_inst *inst;
  234. if (!q || !q->drv_priv) {
  235. d_vpr_e("%s: invalid input, q = %pK\n", q);
  236. return;
  237. }
  238. inst = q->drv_priv;
  239. if (!inst || !inst->core) {
  240. d_vpr_e("%s: invalid params\n", __func__);
  241. return;
  242. }
  243. if (q->type == INPUT_META_PLANE || q->type == OUTPUT_META_PLANE) {
  244. i_vpr_h(inst, "%s: nothing to stop on %s\n",
  245. __func__, v4l2_type_name(q->type));
  246. return;
  247. }
  248. if (!is_decode_session(inst) && !is_encode_session(inst)) {
  249. i_vpr_e(inst, "%s: invalid session %d\n",
  250. __func__, inst->domain);
  251. return;
  252. }
  253. i_vpr_h(inst, "Streamoff: %s\n", v4l2_type_name(q->type));
  254. if (q->type == INPUT_MPLANE) {
  255. if (is_decode_session(inst))
  256. rc = msm_vdec_streamoff_input(inst);
  257. else if (is_encode_session(inst))
  258. rc = msm_venc_streamoff_input(inst);
  259. } else if (q->type == OUTPUT_MPLANE) {
  260. if (is_decode_session(inst))
  261. rc = msm_vdec_streamoff_output(inst);
  262. else if (is_encode_session(inst))
  263. rc = msm_venc_streamoff_output(inst);
  264. } else {
  265. i_vpr_e(inst, "%s: invalid type %d\n", q->type);
  266. goto error;
  267. }
  268. if (rc)
  269. goto error;
  270. /* Input port streamoff - flush timestamps list*/
  271. if (q->type == INPUT_MPLANE)
  272. msm_vidc_flush_ts(inst);
  273. i_vpr_h(inst, "Streamoff: %s successful\n", v4l2_type_name(q->type));
  274. return;
  275. error:
  276. i_vpr_e(inst, "Streamoff: %s failed\n", v4l2_type_name(q->type));
  277. return;
  278. }
  279. void msm_vidc_buf_queue(struct vb2_buffer *vb2)
  280. {
  281. int rc = 0;
  282. struct msm_vidc_inst *inst;
  283. inst = vb2_get_drv_priv(vb2->vb2_queue);
  284. if (!inst) {
  285. d_vpr_e("%s: invalid params\n", __func__);
  286. return;
  287. }
  288. if (is_decode_session(inst))
  289. rc = msm_vdec_qbuf(inst, vb2);
  290. else if (is_encode_session(inst))
  291. rc = msm_venc_qbuf(inst, vb2);
  292. else
  293. rc = -EINVAL;
  294. if (rc) {
  295. print_vb2_buffer("failed vb2-qbuf", inst, vb2);
  296. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  297. vb2_buffer_done(vb2, VB2_BUF_STATE_ERROR);
  298. }
  299. }
  300. void msm_vidc_buf_cleanup(struct vb2_buffer *vb)
  301. {
  302. }