Guest User

Untitled

a guest
Jun 30th, 2026
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.64 KB | None | 0 0
  1. 1c1
  2. < // SPDX-License-Identifier: GPL-2.0
  3. ---
  4. > /* SPDX-License-Identifier: GPL-2.0 */
  5. 3c3
  6. < * Rockchip Video Decoder driver
  7. ---
  8. > * Apple Video Decoder driver
  9. 5c5
  10. < * Copyright (C) 2019 Collabora, Ltd.
  11. ---
  12. > * Copyright The Asahi Linux Contributors
  13. 6a7,8
  14. > * Based on rkvdec driver by Collabora, Ltd.
  15. > * Copyright (C) 2019 Collabora, Ltd.
  16. 12,19d13
  17. < #include <linux/clk.h>
  18. < #include <linux/interrupt.h>
  19. < #include <linux/iommu.h>
  20. < #include <linux/module.h>
  21. < #include <linux/of.h>
  22. < #include <linux/of_device.h>
  23. < #include <linux/platform_device.h>
  24. < #include <linux/pm.h>
  25. 21,23c15
  26. < #include <linux/slab.h>
  27. < #include <linux/videodev2.h>
  28. < #include <linux/workqueue.h>
  29. ---
  30. >
  31. 26,27d17
  32. < #include <media/videobuf2-core.h>
  33. < #include <media/videobuf2-vmalloc.h>
  34. 29d18
  35. < #include "rkvdec-regs.h"
  36. 34,35c23
  37. < return fmt1 == fmt2 || fmt2 == RKVDEC_IMG_FMT_ANY ||
  38. < fmt1 == RKVDEC_IMG_FMT_ANY;
  39. ---
  40. > return fmt1 == fmt2 || fmt2 == AVD_IMG_FMT_ANY || fmt1 == AVD_IMG_FMT_ANY;
  41. 40c28
  42. < if (image_fmt == RKVDEC_IMG_FMT_ANY)
  43. ---
  44. > if (image_fmt == AVD_IMG_FMT_ANY)
  45. 84,85c72,75
  46. < pix_mp->plane_fmt[0].sizeimage +=
  47. < 128 * DIV_ROUND_UP(pix_mp->width, 16) * DIV_ROUND_UP(pix_mp->height, 16);
  48. ---
  49. > fill_rvra(&ctx->rvra, ctx->image_fmt, pix_mp->width, pix_mp->height);
  50. > pix_mp->plane_fmt[0].sizeimage += ctx->rvra.size;
  51. > if (ctx->coded_fmt_desc->ops->adjust_decoded_fmt)
  52. > ctx->coded_fmt_desc->ops->adjust_decoded_fmt(ctx, pix_mp);
  53. 99c89
  54. < static void rkvdec_reset_decoded_fmt(struct rkvdec_ctx *ctx) {
  55. ---
  56. > void rkvdec_reset_decoded_fmt(struct rkvdec_ctx *ctx) {
  57. 146c136
  58. < static const struct v4l2_ctrl_ops rkvdec_ctrl_ops = {
  59. ---
  60. > const struct v4l2_ctrl_ops rkvdec_ctrl_ops = {
  61. 173,175c163,165
  62. < .cfg.min = V4L2_STATELESS_HEVC_DECODE_MODE_FRAME_BASED,
  63. < .cfg.max = V4L2_STATELESS_HEVC_DECODE_MODE_FRAME_BASED,
  64. < .cfg.def = V4L2_STATELESS_HEVC_DECODE_MODE_FRAME_BASED,
  65. ---
  66. > .cfg.min = V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED,
  67. > .cfg.max = V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED,
  68. > .cfg.def = V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED,
  69. 179,181c169,171
  70. < .cfg.min = V4L2_STATELESS_HEVC_START_CODE_ANNEX_B,
  71. < .cfg.def = V4L2_STATELESS_HEVC_START_CODE_ANNEX_B,
  72. < .cfg.max = V4L2_STATELESS_HEVC_START_CODE_ANNEX_B,
  73. ---
  74. > .cfg.min = V4L2_STATELESS_HEVC_START_CODE_NONE,
  75. > .cfg.def = V4L2_STATELESS_HEVC_START_CODE_NONE,
  76. > .cfg.max = V4L2_STATELESS_HEVC_START_CODE_NONE,
  77. 204c194
  78. < .image_fmt = RKVDEC_IMG_FMT_420_8BIT,
  79. ---
  80. > .image_fmt = AVD_IMG_FMT_420_8BIT,
  81. 208c198
  82. < .image_fmt = RKVDEC_IMG_FMT_420_10BIT,
  83. ---
  84. > .image_fmt = AVD_IMG_FMT_420_10BIT,
  85. 221a212
  86. > .cfg.ops = &rkvdec_ctrl_ops,
  87. 226a218,223
  88. > .cfg.id = V4L2_CID_STATELESS_H264_PRED_WEIGHTS,
  89. > },
  90. > {
  91. > .cfg.id = V4L2_CID_STATELESS_H264_SLICE_PARAMS,
  92. > },
  93. > {
  94. 228,230c225,227
  95. < .cfg.min = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
  96. < .cfg.max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
  97. < .cfg.def = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
  98. ---
  99. > .cfg.min = V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED,
  100. > .cfg.max = V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED,
  101. > .cfg.def = V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED,
  102. 234,236c231,234
  103. < .cfg.min = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
  104. < .cfg.def = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
  105. < .cfg.max = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
  106. ---
  107. > .cfg.min = V4L2_STATELESS_H264_START_CODE_NONE,
  108. > .cfg.max = V4L2_STATELESS_H264_START_CODE_NONE,
  109. > .cfg.def = V4L2_STATELESS_H264_START_CODE_NONE,
  110. > /* annex b is also possibly but a bit more painfull */
  111. 262c260
  112. < .image_fmt = RKVDEC_IMG_FMT_420_8BIT,
  113. ---
  114. > .image_fmt = AVD_IMG_FMT_420_8BIT,
  115. 265,266c263,264
  116. < .fourcc = V4L2_PIX_FMT_NV15,
  117. < .image_fmt = RKVDEC_IMG_FMT_420_10BIT,
  118. ---
  119. > .fourcc = V4L2_PIX_FMT_P010,
  120. > .image_fmt = AVD_IMG_FMT_420_10BIT,
  121. 270c268
  122. < .image_fmt = RKVDEC_IMG_FMT_422_8BIT,
  123. ---
  124. > .image_fmt = AVD_IMG_FMT_422_8BIT,
  125. 273,274c271,273
  126. < .fourcc = V4L2_PIX_FMT_NV20,
  127. < .image_fmt = RKVDEC_IMG_FMT_422_10BIT,
  128. ---
  129. > /* TODO: missing P210 */
  130. > .fourcc = V4L2_PIX_FMT_P010,
  131. > .image_fmt = AVD_IMG_FMT_422_10BIT,
  132. 301c300
  133. < .image_fmt = RKVDEC_IMG_FMT_420_8BIT,
  134. ---
  135. > .image_fmt = AVD_IMG_FMT_420_8BIT,
  136. 312c311
  137. < .step_width = 64,
  138. ---
  139. > .step_width = 16,
  140. 314c313
  141. < .max_height = 2304,
  142. ---
  143. > .max_height = 4096,
  144. 319a319
  145. > .subsystem_flags = VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF,
  146. 321c321
  147. < .capability = RKVDEC_CAPABILITY_HEVC,
  148. ---
  149. > .capability = AVD_CAPABILITY_HEVC,
  150. 330,331c330,331
  151. < .min_height = 48,
  152. < .max_height = 2560,
  153. ---
  154. > .min_height = 64,
  155. > .max_height = 4096,
  156. 339c339
  157. < .capability = RKVDEC_CAPABILITY_H264,
  158. ---
  159. > .capability = AVD_CAPABILITY_H264,
  160. 347c347
  161. < .step_width = 64,
  162. ---
  163. > .step_width = 16,
  164. 349,350c349,350
  165. < .max_height = 2304,
  166. < .step_height = 64,
  167. ---
  168. > .max_height = 4096,
  169. > .step_height = 16,
  170. 356c356
  171. < .capability = RKVDEC_CAPABILITY_VP9,
  172. ---
  173. > .capability = AVD_CAPABILITY_VP9,
  174. 360c360,361
  175. < return (ctx->dev->variant->capabilities & capability) == capability;
  176. ---
  177. > return capability == AVD_CAPABILITY_H264;
  178. > /* return (ctx->dev->variant->capabilities & capability) == capability; */
  179. 392c393
  180. < static void rkvdec_reset_coded_fmt(struct rkvdec_ctx *ctx) {
  181. ---
  182. > void rkvdec_reset_coded_fmt(struct rkvdec_ctx *ctx) {
  183. 402,403c403,406
  184. < if (ctx->coded_fmt_desc->ops->adjust_fmt)
  185. < ctx->coded_fmt_desc->ops->adjust_fmt(ctx, f);
  186. ---
  187. > f->fmt.pix_mp.num_planes = 1;
  188. > if (!f->fmt.pix_mp.plane_fmt[0].sizeimage)
  189. > f->fmt.pix_mp.plane_fmt[0].sizeimage =
  190. > f->fmt.pix_mp.width * f->fmt.pix_mp.height;
  191. 408c411
  192. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  193. ---
  194. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  195. 444c447
  196. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  197. ---
  198. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  199. 453c456,457
  200. < if (WARN_ON(!coded_desc))
  201. ---
  202. > if (WARN_ON(!coded_desc)) {
  203. > dev_err(ctx->dev->dev, "no coded desc!");
  204. 454a459
  205. > }
  206. 474c479
  207. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  208. ---
  209. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  210. 490,496c495,496
  211. < if (desc->ops->adjust_fmt) {
  212. < int ret;
  213. <
  214. < ret = desc->ops->adjust_fmt(ctx, f);
  215. < if (ret)
  216. < return ret;
  217. < }
  218. ---
  219. > if (!pix_mp->plane_fmt[0].sizeimage)
  220. > pix_mp->plane_fmt[0].sizeimage = pix_mp->width * f->fmt.pix_mp.height;
  221. 503c503
  222. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  223. ---
  224. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  225. 522c522
  226. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  227. ---
  228. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  229. 568c568
  230. < ctx->image_fmt = RKVDEC_IMG_FMT_ANY;
  231. ---
  232. > ctx->image_fmt = AVD_IMG_FMT_ANY;
  233. 586c586
  234. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  235. ---
  236. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  237. 594c594
  238. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  239. ---
  240. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  241. 602c602
  242. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  243. ---
  244. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  245. 615c615
  246. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(file);
  247. ---
  248. > struct rkvdec_ctx *ctx = file_to_ctx(file);
  249. 626c626
  250. < static const struct v4l2_ioctl_ops rkvdec_ioctl_ops = {
  251. ---
  252. > const struct v4l2_ioctl_ops rkvdec_ioctl_ops = {
  253. 791c791
  254. < static const struct vb2_ops rkvdec_queue_ops = {
  255. ---
  256. > const struct vb2_ops rkvdec_queue_ops = {
  257. 801,819c801,802
  258. < static int rkvdec_request_validate(struct media_request *req) {
  259. < unsigned int count;
  260. <
  261. < count = vb2_request_buffer_cnt(req);
  262. < if (!count)
  263. < return -ENOENT;
  264. < else if (count > 1)
  265. < return -EINVAL;
  266. <
  267. < return vb2_request_validate(req);
  268. < }
  269. <
  270. < static const struct media_device_ops rkvdec_media_ops = {
  271. < .req_validate = rkvdec_request_validate,
  272. < .req_queue = v4l2_m2m_request_queue,
  273. < };
  274. <
  275. < static void rkvdec_job_finish_no_pm(struct rkvdec_ctx *ctx,
  276. < enum vb2_buffer_state result) {
  277. ---
  278. > void rkvdec_job_finish_no_pm(struct rkvdec_ctx *ctx,
  279. > enum vb2_buffer_state result) {
  280. 831,832c814
  281. < static void rkvdec_job_finish(struct rkvdec_ctx *ctx,
  282. < enum vb2_buffer_state result) {
  283. ---
  284. > void rkvdec_job_finish(struct rkvdec_ctx *ctx, enum vb2_buffer_state result) {
  285. 862,940d843
  286. < void rkvdec_quirks_disable_qos(struct rkvdec_ctx *ctx) {
  287. < struct rkvdec_dev *rkvdec = ctx->dev;
  288. < u32 reg;
  289. <
  290. < /* Set undocumented swreg_block_gating_e field */
  291. < reg = readl(rkvdec->regs + RKVDEC_REG_QOS_CTRL);
  292. < reg &= GENMASK(31, 16);
  293. < reg |= 0xEFFF;
  294. < writel(reg, rkvdec->regs + RKVDEC_REG_QOS_CTRL);
  295. < }
  296. <
  297. < static void rkvdec_device_run(void *priv) {
  298. < struct rkvdec_ctx *ctx = priv;
  299. < struct rkvdec_dev *rkvdec = ctx->dev;
  300. < const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
  301. < int ret;
  302. <
  303. < if (WARN_ON(!desc))
  304. < return;
  305. <
  306. < ret = pm_runtime_resume_and_get(rkvdec->dev);
  307. < if (ret < 0) {
  308. < rkvdec_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
  309. < return;
  310. < }
  311. <
  312. < ret = desc->ops->run(ctx);
  313. < if (ret)
  314. < rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
  315. < }
  316. <
  317. < static const struct v4l2_m2m_ops rkvdec_m2m_ops = {
  318. < .device_run = rkvdec_device_run,
  319. < };
  320. <
  321. < static int rkvdec_queue_init(void *priv, struct vb2_queue *src_vq,
  322. < struct vb2_queue *dst_vq) {
  323. < struct rkvdec_ctx *ctx = priv;
  324. < struct rkvdec_dev *rkvdec = ctx->dev;
  325. < int ret;
  326. <
  327. < src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  328. < src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  329. < src_vq->drv_priv = ctx;
  330. < src_vq->ops = &rkvdec_queue_ops;
  331. < src_vq->mem_ops = &vb2_dma_contig_memops;
  332. <
  333. < /*
  334. < * Driver does mostly sequential access, so sacrifice TLB efficiency
  335. < * for faster allocation. Also, no CPU access on the source queue,
  336. < * so no kernel mapping needed.
  337. < */
  338. < src_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES | DMA_ATTR_NO_KERNEL_MAPPING;
  339. < src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  340. < src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  341. < src_vq->lock = &rkvdec->vdev_lock;
  342. < src_vq->dev = rkvdec->v4l2_dev.dev;
  343. < src_vq->supports_requests = true;
  344. < src_vq->requires_requests = true;
  345. <
  346. < ret = vb2_queue_init(src_vq);
  347. < if (ret)
  348. < return ret;
  349. <
  350. < dst_vq->bidirectional = true;
  351. < dst_vq->mem_ops = &vb2_dma_contig_memops;
  352. < dst_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES | DMA_ATTR_NO_KERNEL_MAPPING;
  353. < dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  354. < dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  355. < dst_vq->drv_priv = ctx;
  356. < dst_vq->ops = &rkvdec_queue_ops;
  357. < dst_vq->buf_struct_size = sizeof(struct rkvdec_decoded_buffer);
  358. < dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  359. < dst_vq->lock = &rkvdec->vdev_lock;
  360. < dst_vq->dev = rkvdec->v4l2_dev.dev;
  361. <
  362. < return vb2_queue_init(dst_vq);
  363. < }
  364. <
  365. 956c859
  366. < static int rkvdec_init_ctrls(struct rkvdec_ctx *ctx) {
  367. ---
  368. > int rkvdec_init_ctrls(struct rkvdec_ctx *ctx) {
  369. 985,1339d887
  370. <
  371. < static int rkvdec_open(struct file *filp) {
  372. < struct rkvdec_dev *rkvdec = video_drvdata(filp);
  373. < struct rkvdec_ctx *ctx;
  374. < int ret;
  375. <
  376. < ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  377. < if (!ctx)
  378. < return -ENOMEM;
  379. <
  380. < ctx->dev = rkvdec;
  381. < rkvdec_reset_coded_fmt(ctx);
  382. < rkvdec_reset_decoded_fmt(ctx);
  383. < v4l2_fh_init(&ctx->fh, video_devdata(filp));
  384. <
  385. < ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rkvdec->m2m_dev, ctx, rkvdec_queue_init);
  386. < if (IS_ERR(ctx->fh.m2m_ctx)) {
  387. < ret = PTR_ERR(ctx->fh.m2m_ctx);
  388. < goto err_free_ctx;
  389. < }
  390. <
  391. < ret = rkvdec_init_ctrls(ctx);
  392. < if (ret)
  393. < goto err_cleanup_m2m_ctx;
  394. <
  395. < v4l2_fh_add(&ctx->fh, filp);
  396. <
  397. < return 0;
  398. <
  399. < err_cleanup_m2m_ctx:
  400. < v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  401. <
  402. < err_free_ctx:
  403. < kfree(ctx);
  404. < return ret;
  405. < }
  406. <
  407. < static int rkvdec_release(struct file *filp) {
  408. < struct rkvdec_ctx *ctx = file_to_rkvdec_ctx(filp);
  409. <
  410. < v4l2_fh_del(&ctx->fh, filp);
  411. < v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  412. < v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  413. < v4l2_fh_exit(&ctx->fh);
  414. < kfree(ctx);
  415. <
  416. < return 0;
  417. < }
  418. <
  419. < static const struct v4l2_file_operations rkvdec_fops = {
  420. < .owner = THIS_MODULE,
  421. < .open = rkvdec_open,
  422. < .release = rkvdec_release,
  423. < .poll = v4l2_m2m_fop_poll,
  424. < .unlocked_ioctl = video_ioctl2,
  425. < .mmap = v4l2_m2m_fop_mmap,
  426. < };
  427. <
  428. < static int rkvdec_v4l2_init(struct rkvdec_dev *rkvdec) {
  429. < int ret;
  430. <
  431. < ret = v4l2_device_register(rkvdec->dev, &rkvdec->v4l2_dev);
  432. < if (ret) {
  433. < dev_err(rkvdec->dev, "Failed to register V4L2 device\n");
  434. < return ret;
  435. < }
  436. <
  437. < rkvdec->m2m_dev = v4l2_m2m_init(&rkvdec_m2m_ops);
  438. < if (IS_ERR(rkvdec->m2m_dev)) {
  439. < v4l2_err(&rkvdec->v4l2_dev, "Failed to init mem2mem device\n");
  440. < ret = PTR_ERR(rkvdec->m2m_dev);
  441. < goto err_unregister_v4l2;
  442. < }
  443. <
  444. < rkvdec->mdev.dev = rkvdec->dev;
  445. < strscpy(rkvdec->mdev.model, "rkvdec", sizeof(rkvdec->mdev.model));
  446. < strscpy(rkvdec->mdev.bus_info, "platform:rkvdec",
  447. < sizeof(rkvdec->mdev.bus_info));
  448. < media_device_init(&rkvdec->mdev);
  449. < rkvdec->mdev.ops = &rkvdec_media_ops;
  450. < rkvdec->v4l2_dev.mdev = &rkvdec->mdev;
  451. <
  452. < rkvdec->vdev.lock = &rkvdec->vdev_lock;
  453. < rkvdec->vdev.v4l2_dev = &rkvdec->v4l2_dev;
  454. < rkvdec->vdev.fops = &rkvdec_fops;
  455. < rkvdec->vdev.release = video_device_release_empty;
  456. < rkvdec->vdev.vfl_dir = VFL_DIR_M2M;
  457. < rkvdec->vdev.device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
  458. < rkvdec->vdev.ioctl_ops = &rkvdec_ioctl_ops;
  459. < video_set_drvdata(&rkvdec->vdev, rkvdec);
  460. < strscpy(rkvdec->vdev.name, "rkvdec", sizeof(rkvdec->vdev.name));
  461. <
  462. < ret = video_register_device(&rkvdec->vdev, VFL_TYPE_VIDEO, -1);
  463. < if (ret) {
  464. < v4l2_err(&rkvdec->v4l2_dev, "Failed to register video device\n");
  465. < goto err_cleanup_mc;
  466. < }
  467. <
  468. < ret = v4l2_m2m_register_media_controller(rkvdec->m2m_dev, &rkvdec->vdev,
  469. < MEDIA_ENT_F_PROC_VIDEO_DECODER);
  470. < if (ret) {
  471. < v4l2_err(&rkvdec->v4l2_dev,
  472. < "Failed to initialize V4L2 M2M media controller\n");
  473. < goto err_unregister_vdev;
  474. < }
  475. <
  476. < ret = media_device_register(&rkvdec->mdev);
  477. < if (ret) {
  478. < v4l2_err(&rkvdec->v4l2_dev, "Failed to register media device\n");
  479. < goto err_unregister_mc;
  480. < }
  481. <
  482. < return 0;
  483. <
  484. < err_unregister_mc:
  485. < v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
  486. <
  487. < err_unregister_vdev:
  488. < video_unregister_device(&rkvdec->vdev);
  489. <
  490. < err_cleanup_mc:
  491. < media_device_cleanup(&rkvdec->mdev);
  492. < v4l2_m2m_release(rkvdec->m2m_dev);
  493. <
  494. < err_unregister_v4l2:
  495. < v4l2_device_unregister(&rkvdec->v4l2_dev);
  496. < return ret;
  497. < }
  498. <
  499. < static void rkvdec_v4l2_cleanup(struct rkvdec_dev *rkvdec) {
  500. < media_device_unregister(&rkvdec->mdev);
  501. < v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
  502. < video_unregister_device(&rkvdec->vdev);
  503. < media_device_cleanup(&rkvdec->mdev);
  504. < v4l2_m2m_release(rkvdec->m2m_dev);
  505. < v4l2_device_unregister(&rkvdec->v4l2_dev);
  506. < }
  507. <
  508. < static void rkvdec_iommu_restore(struct rkvdec_dev *rkvdec) {
  509. < if (rkvdec->empty_domain) {
  510. < /*
  511. < * To rewrite mapping into the attached IOMMU core, attach a new empty
  512. < * domain that will program an empty table, then detach it to restore the
  513. < * default domain and all cached mappings. This is safely done in this
  514. < * interrupt handler to make sure no memory get mapped through the IOMMU
  515. < * while the empty domain is attached.
  516. < */
  517. < iommu_attach_device(rkvdec->empty_domain, rkvdec->dev);
  518. < iommu_detach_device(rkvdec->empty_domain, rkvdec->dev);
  519. < }
  520. < }
  521. <
  522. < static irqreturn_t rkvdec_irq_handler(int irq, void *priv) {
  523. < struct rkvdec_dev *rkvdec = priv;
  524. < struct rkvdec_ctx *ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
  525. < enum vb2_buffer_state state;
  526. < u32 status;
  527. <
  528. < status = readl(rkvdec->regs + RKVDEC_REG_INTERRUPT);
  529. < writel(0, rkvdec->regs + RKVDEC_REG_INTERRUPT);
  530. <
  531. < if (status & RKVDEC_RDY_STA) {
  532. < state = VB2_BUF_STATE_DONE;
  533. < } else {
  534. < state = VB2_BUF_STATE_ERROR;
  535. < if (status & RKVDEC_SOFTRESET_RDY)
  536. < rkvdec_iommu_restore(rkvdec);
  537. < }
  538. <
  539. < if (cancel_delayed_work(&rkvdec->watchdog_work))
  540. < rkvdec_job_finish(ctx, state);
  541. <
  542. < return IRQ_HANDLED;
  543. < }
  544. <
  545. < static void rkvdec_watchdog_func(struct work_struct *work) {
  546. < struct rkvdec_dev *rkvdec;
  547. < struct rkvdec_ctx *ctx;
  548. <
  549. < rkvdec =
  550. < container_of(to_delayed_work(work), struct rkvdec_dev, watchdog_work);
  551. < ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
  552. < if (ctx) {
  553. < dev_err(rkvdec->dev, "Frame processing timed out!\n");
  554. < writel(RKVDEC_IRQ_DIS, rkvdec->regs + RKVDEC_REG_INTERRUPT);
  555. < writel(0, rkvdec->regs + RKVDEC_REG_SYSCTRL);
  556. < rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
  557. < }
  558. < }
  559. <
  560. < static const struct rkvdec_variant rk3288_rkvdec_variant = {
  561. < .num_regs = 68,
  562. < .capabilities = RKVDEC_CAPABILITY_HEVC,
  563. < };
  564. <
  565. < static const struct rkvdec_variant rk3328_rkvdec_variant = {
  566. < .num_regs = 109,
  567. < .capabilities =
  568. < RKVDEC_CAPABILITY_HEVC | RKVDEC_CAPABILITY_H264 | RKVDEC_CAPABILITY_VP9,
  569. < .quirks = RKVDEC_QUIRK_DISABLE_QOS,
  570. < };
  571. <
  572. < static const struct rkvdec_variant rk3399_rkvdec_variant = {
  573. < .num_regs = 78,
  574. < .capabilities =
  575. < RKVDEC_CAPABILITY_HEVC | RKVDEC_CAPABILITY_H264 | RKVDEC_CAPABILITY_VP9,
  576. < };
  577. <
  578. < static const struct of_device_id of_rkvdec_match[] = {
  579. < {
  580. < .compatible = "rockchip,rk3288-vdec",
  581. < .data = &rk3288_rkvdec_variant,
  582. < },
  583. < {
  584. < .compatible = "rockchip,rk3328-vdec",
  585. < .data = &rk3328_rkvdec_variant,
  586. < },
  587. < {
  588. < .compatible = "rockchip,rk3399-vdec",
  589. < .data = &rk3399_rkvdec_variant,
  590. < },
  591. < {/* sentinel */}};
  592. < MODULE_DEVICE_TABLE(of, of_rkvdec_match);
  593. <
  594. < static const char *const rkvdec_clk_names[] = {"axi", "ahb", "cabac", "core"};
  595. <
  596. < static int rkvdec_probe(struct platform_device *pdev) {
  597. < const struct rkvdec_variant *variant;
  598. < struct rkvdec_dev *rkvdec;
  599. < unsigned int i;
  600. < int ret, irq;
  601. <
  602. < variant = of_device_get_match_data(&pdev->dev);
  603. < if (!variant)
  604. < return -EINVAL;
  605. <
  606. < rkvdec = devm_kzalloc(&pdev->dev, sizeof(*rkvdec), GFP_KERNEL);
  607. < if (!rkvdec)
  608. < return -ENOMEM;
  609. <
  610. < platform_set_drvdata(pdev, rkvdec);
  611. < rkvdec->dev = &pdev->dev;
  612. < rkvdec->variant = variant;
  613. < mutex_init(&rkvdec->vdev_lock);
  614. < INIT_DELAYED_WORK(&rkvdec->watchdog_work, rkvdec_watchdog_func);
  615. <
  616. < rkvdec->clocks = devm_kcalloc(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
  617. < sizeof(*rkvdec->clocks), GFP_KERNEL);
  618. < if (!rkvdec->clocks)
  619. < return -ENOMEM;
  620. <
  621. < for (i = 0; i < ARRAY_SIZE(rkvdec_clk_names); i++)
  622. < rkvdec->clocks[i].id = rkvdec_clk_names[i];
  623. <
  624. < ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
  625. < rkvdec->clocks);
  626. < if (ret)
  627. < return ret;
  628. <
  629. < rkvdec->regs = devm_platform_ioremap_resource(pdev, 0);
  630. < if (IS_ERR(rkvdec->regs))
  631. < return PTR_ERR(rkvdec->regs);
  632. <
  633. < ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  634. < if (ret) {
  635. < dev_err(&pdev->dev, "Could not set DMA coherent mask.\n");
  636. < return ret;
  637. < }
  638. <
  639. < vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
  640. <
  641. < irq = platform_get_irq(pdev, 0);
  642. < if (irq <= 0)
  643. < return -ENXIO;
  644. <
  645. < ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, rkvdec_irq_handler,
  646. < IRQF_ONESHOT, dev_name(&pdev->dev), rkvdec);
  647. < if (ret) {
  648. < dev_err(&pdev->dev, "Could not request vdec IRQ\n");
  649. < return ret;
  650. < }
  651. <
  652. < pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
  653. < pm_runtime_use_autosuspend(&pdev->dev);
  654. < pm_runtime_enable(&pdev->dev);
  655. <
  656. < ret = rkvdec_v4l2_init(rkvdec);
  657. < if (ret)
  658. < goto err_disable_runtime_pm;
  659. <
  660. < if (iommu_get_domain_for_dev(&pdev->dev)) {
  661. < rkvdec->empty_domain = iommu_paging_domain_alloc(rkvdec->dev);
  662. <
  663. < if (IS_ERR(rkvdec->empty_domain)) {
  664. < rkvdec->empty_domain = NULL;
  665. < dev_warn(rkvdec->dev, "cannot alloc new empty domain\n");
  666. < }
  667. < }
  668. <
  669. < return 0;
  670. <
  671. < err_disable_runtime_pm:
  672. < pm_runtime_dont_use_autosuspend(&pdev->dev);
  673. < pm_runtime_disable(&pdev->dev);
  674. < return ret;
  675. < }
  676. <
  677. < static void rkvdec_remove(struct platform_device *pdev) {
  678. < struct rkvdec_dev *rkvdec = platform_get_drvdata(pdev);
  679. <
  680. < cancel_delayed_work_sync(&rkvdec->watchdog_work);
  681. <
  682. < rkvdec_v4l2_cleanup(rkvdec);
  683. < pm_runtime_disable(&pdev->dev);
  684. < pm_runtime_dont_use_autosuspend(&pdev->dev);
  685. <
  686. < if (rkvdec->empty_domain)
  687. < iommu_domain_free(rkvdec->empty_domain);
  688. < }
  689. <
  690. < #ifdef CONFIG_PM
  691. < static int rkvdec_runtime_resume(struct device *dev) {
  692. < struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
  693. <
  694. < return clk_bulk_prepare_enable(ARRAY_SIZE(rkvdec_clk_names), rkvdec->clocks);
  695. < }
  696. <
  697. < static int rkvdec_runtime_suspend(struct device *dev) {
  698. < struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
  699. <
  700. < clk_bulk_disable_unprepare(ARRAY_SIZE(rkvdec_clk_names), rkvdec->clocks);
  701. < return 0;
  702. < }
  703. < #endif
  704. <
  705. < static const struct dev_pm_ops rkvdec_pm_ops = {
  706. < SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
  707. < SET_RUNTIME_PM_OPS(rkvdec_runtime_suspend, rkvdec_runtime_resume,
  708. < NULL)};
  709. <
  710. < static struct platform_driver rkvdec_driver = {
  711. < .probe = rkvdec_probe,
  712. < .remove = rkvdec_remove,
  713. < .driver =
  714. < {
  715. < .name = "rkvdec",
  716. < .of_match_table = of_rkvdec_match,
  717. < .pm = &rkvdec_pm_ops,
  718. < },
  719. < };
  720. < module_platform_driver(rkvdec_driver);
  721. <
  722. < MODULE_AUTHOR("Boris Brezillon <[email protected]>");
  723. < MODULE_DESCRIPTION("Rockchip Video Decoder driver");
  724. < MODULE_LICENSE("GPL v2");
  725.  
Advertisement
Add Comment
Please, Sign In to add comment