1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "../decoder/mtk_vcodec_dec_drv.h"
4 #include "../encoder/mtk_vcodec_enc_drv.h"
5 #include "mtk_vcodec_fw_priv.h"
6 
7 static int mtk_vcodec_vpu_load_firmware(struct mtk_vcodec_fw *fw)
8 {
9 	return vpu_load_firmware(fw->pdev);
10 }
11 
12 static unsigned int mtk_vcodec_vpu_get_vdec_capa(struct mtk_vcodec_fw *fw)
13 {
14 	return vpu_get_vdec_hw_capa(fw->pdev);
15 }
16 
17 static unsigned int mtk_vcodec_vpu_get_venc_capa(struct mtk_vcodec_fw *fw)
18 {
19 	return vpu_get_venc_hw_capa(fw->pdev);
20 }
21 
22 static void *mtk_vcodec_vpu_map_dm_addr(struct mtk_vcodec_fw *fw,
23 					u32 dtcm_dmem_addr)
24 {
25 	return vpu_mapping_dm_addr(fw->pdev, dtcm_dmem_addr);
26 }
27 
28 static int mtk_vcodec_vpu_set_ipi_register(struct mtk_vcodec_fw *fw, int id,
29 					   mtk_vcodec_ipi_handler handler,
30 					   const char *name, void *priv)
31 {
32 	/*
33 	 * The handler we receive takes a void * as its first argument. We
34 	 * cannot change this because it needs to be passed down to the rproc
35 	 * subsystem when SCP is used. VPU takes a const argument, which is
36 	 * more constrained, so the conversion below is safe.
37 	 */
38 	ipi_handler_t handler_const = (ipi_handler_t)handler;
39 
40 	return vpu_ipi_register(fw->pdev, id, handler_const, name, priv);
41 }
42 
43 static int mtk_vcodec_vpu_ipi_send(struct mtk_vcodec_fw *fw, int id, void *buf,
44 				   unsigned int len, unsigned int wait)
45 {
46 	return vpu_ipi_send(fw->pdev, id, buf, len);
47 }
48 
49 static void mtk_vcodec_vpu_release(struct mtk_vcodec_fw *fw)
50 {
51 	put_device(&fw->pdev->dev);
52 }
53 
54 static void mtk_vcodec_vpu_reset_dec_handler(void *priv)
55 {
56 	struct mtk_vcodec_dec_dev *dev = priv;
57 	struct mtk_vcodec_dec_ctx *ctx;
58 
59 	dev_err(&dev->plat_dev->dev, "Watchdog timeout!!");
60 
61 	mutex_lock(&dev->dev_mutex);
62 	list_for_each_entry(ctx, &dev->ctx_list, list) {
63 		ctx->state = MTK_STATE_ABORT;
64 		mtk_v4l2_vdec_dbg(0, ctx, "[%d] Change to state MTK_STATE_ABORT", ctx->id);
65 	}
66 	mutex_unlock(&dev->dev_mutex);
67 }
68 
69 static void mtk_vcodec_vpu_reset_enc_handler(void *priv)
70 {
71 	struct mtk_vcodec_enc_dev *dev = priv;
72 	struct mtk_vcodec_enc_ctx *ctx;
73 
74 	dev_err(&dev->plat_dev->dev, "Watchdog timeout!!");
75 
76 	mutex_lock(&dev->dev_mutex);
77 	list_for_each_entry(ctx, &dev->ctx_list, list) {
78 		ctx->state = MTK_STATE_ABORT;
79 		mtk_v4l2_vdec_dbg(0, ctx, "[%d] Change to state MTK_STATE_ABORT", ctx->id);
80 	}
81 	mutex_unlock(&dev->dev_mutex);
82 }
83 
84 static const struct mtk_vcodec_fw_ops mtk_vcodec_vpu_msg = {
85 	.load_firmware = mtk_vcodec_vpu_load_firmware,
86 	.get_vdec_capa = mtk_vcodec_vpu_get_vdec_capa,
87 	.get_venc_capa = mtk_vcodec_vpu_get_venc_capa,
88 	.map_dm_addr = mtk_vcodec_vpu_map_dm_addr,
89 	.ipi_register = mtk_vcodec_vpu_set_ipi_register,
90 	.ipi_send = mtk_vcodec_vpu_ipi_send,
91 	.release = mtk_vcodec_vpu_release,
92 };
93 
94 struct mtk_vcodec_fw *mtk_vcodec_fw_vpu_init(void *priv, enum mtk_vcodec_fw_use fw_use)
95 {
96 	struct platform_device *fw_pdev;
97 	struct platform_device *plat_dev;
98 	struct mtk_vcodec_fw *fw;
99 	enum rst_id rst_id;
100 
101 	if (fw_use == ENCODER) {
102 		struct mtk_vcodec_enc_dev *enc_dev = priv;
103 
104 		plat_dev = enc_dev->plat_dev;
105 		rst_id = VPU_RST_ENC;
106 	} else if (fw_use == DECODER) {
107 		struct mtk_vcodec_dec_dev *dec_dev = priv;
108 
109 		plat_dev = dec_dev->plat_dev;
110 		rst_id = VPU_RST_DEC;
111 	} else {
112 		pr_err("Invalid fw_use %d (use a reasonable fw id here)\n", fw_use);
113 		return ERR_PTR(-EINVAL);
114 	}
115 
116 	fw_pdev = vpu_get_plat_device(plat_dev);
117 	if (!fw_pdev) {
118 		dev_err(&plat_dev->dev, "firmware device is not ready");
119 		return ERR_PTR(-EINVAL);
120 	}
121 
122 	if (fw_use == DECODER)
123 		vpu_wdt_reg_handler(fw_pdev, mtk_vcodec_vpu_reset_dec_handler, priv, rst_id);
124 	else
125 		vpu_wdt_reg_handler(fw_pdev, mtk_vcodec_vpu_reset_enc_handler, priv, rst_id);
126 
127 	fw = devm_kzalloc(&plat_dev->dev, sizeof(*fw), GFP_KERNEL);
128 	if (!fw)
129 		return ERR_PTR(-ENOMEM);
130 	fw->type = VPU;
131 	fw->ops = &mtk_vcodec_vpu_msg;
132 	fw->pdev = fw_pdev;
133 	fw->fw_use = fw_use;
134 
135 	return fw;
136 }
137