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 
mtk_vcodec_vpu_load_firmware(struct mtk_vcodec_fw * fw)7 static int mtk_vcodec_vpu_load_firmware(struct mtk_vcodec_fw *fw)
8 {
9 	return vpu_load_firmware(fw->pdev);
10 }
11 
mtk_vcodec_vpu_get_vdec_capa(struct mtk_vcodec_fw * fw)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 
mtk_vcodec_vpu_get_venc_capa(struct mtk_vcodec_fw * fw)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 
mtk_vcodec_vpu_map_dm_addr(struct mtk_vcodec_fw * fw,u32 dtcm_dmem_addr)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 
mtk_vcodec_vpu_set_ipi_register(struct mtk_vcodec_fw * fw,int id,mtk_vcodec_ipi_handler handler,const char * name,void * priv)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 	return vpu_ipi_register(fw->pdev, id, handler, name, priv);
33 }
34 
mtk_vcodec_vpu_ipi_send(struct mtk_vcodec_fw * fw,int id,void * buf,unsigned int len,unsigned int wait)35 static int mtk_vcodec_vpu_ipi_send(struct mtk_vcodec_fw *fw, int id, void *buf,
36 				   unsigned int len, unsigned int wait)
37 {
38 	return vpu_ipi_send(fw->pdev, id, buf, len);
39 }
40 
mtk_vcodec_vpu_release(struct mtk_vcodec_fw * fw)41 static void mtk_vcodec_vpu_release(struct mtk_vcodec_fw *fw)
42 {
43 	put_device(&fw->pdev->dev);
44 }
45 
mtk_vcodec_vpu_reset_dec_handler(void * priv)46 static void mtk_vcodec_vpu_reset_dec_handler(void *priv)
47 {
48 	struct mtk_vcodec_dec_dev *dev = priv;
49 	struct mtk_vcodec_dec_ctx *ctx;
50 
51 	dev_err(&dev->plat_dev->dev, "Watchdog timeout!!");
52 
53 	mutex_lock(&dev->dev_ctx_lock);
54 	list_for_each_entry(ctx, &dev->ctx_list, list) {
55 		ctx->state = MTK_STATE_ABORT;
56 		mtk_v4l2_vdec_dbg(0, ctx, "[%d] Change to state MTK_STATE_ABORT", ctx->id);
57 	}
58 	mutex_unlock(&dev->dev_ctx_lock);
59 }
60 
mtk_vcodec_vpu_reset_enc_handler(void * priv)61 static void mtk_vcodec_vpu_reset_enc_handler(void *priv)
62 {
63 	struct mtk_vcodec_enc_dev *dev = priv;
64 	struct mtk_vcodec_enc_ctx *ctx;
65 
66 	dev_err(&dev->plat_dev->dev, "Watchdog timeout!!");
67 
68 	mutex_lock(&dev->dev_ctx_lock);
69 	list_for_each_entry(ctx, &dev->ctx_list, list) {
70 		ctx->state = MTK_STATE_ABORT;
71 		mtk_v4l2_vdec_dbg(0, ctx, "[%d] Change to state MTK_STATE_ABORT", ctx->id);
72 	}
73 	mutex_unlock(&dev->dev_ctx_lock);
74 }
75 
76 static const struct mtk_vcodec_fw_ops mtk_vcodec_vpu_msg = {
77 	.load_firmware = mtk_vcodec_vpu_load_firmware,
78 	.get_vdec_capa = mtk_vcodec_vpu_get_vdec_capa,
79 	.get_venc_capa = mtk_vcodec_vpu_get_venc_capa,
80 	.map_dm_addr = mtk_vcodec_vpu_map_dm_addr,
81 	.ipi_register = mtk_vcodec_vpu_set_ipi_register,
82 	.ipi_send = mtk_vcodec_vpu_ipi_send,
83 	.release = mtk_vcodec_vpu_release,
84 };
85 
mtk_vcodec_fw_vpu_init(void * priv,enum mtk_vcodec_fw_use fw_use)86 struct mtk_vcodec_fw *mtk_vcodec_fw_vpu_init(void *priv, enum mtk_vcodec_fw_use fw_use)
87 {
88 	struct platform_device *fw_pdev;
89 	struct platform_device *plat_dev;
90 	struct mtk_vcodec_fw *fw;
91 	enum rst_id rst_id;
92 
93 	if (fw_use == ENCODER) {
94 		struct mtk_vcodec_enc_dev *enc_dev = priv;
95 
96 		plat_dev = enc_dev->plat_dev;
97 		rst_id = VPU_RST_ENC;
98 	} else if (fw_use == DECODER) {
99 		struct mtk_vcodec_dec_dev *dec_dev = priv;
100 
101 		plat_dev = dec_dev->plat_dev;
102 		rst_id = VPU_RST_DEC;
103 	} else {
104 		pr_err("Invalid fw_use %d (use a resonable fw id here)\n", fw_use);
105 		return ERR_PTR(-EINVAL);
106 	}
107 
108 	fw_pdev = vpu_get_plat_device(plat_dev);
109 	if (!fw_pdev) {
110 		dev_err(&plat_dev->dev, "firmware device is not ready");
111 		return ERR_PTR(-EINVAL);
112 	}
113 
114 	if (fw_use == DECODER)
115 		vpu_wdt_reg_handler(fw_pdev, mtk_vcodec_vpu_reset_dec_handler, priv, rst_id);
116 	else
117 		vpu_wdt_reg_handler(fw_pdev, mtk_vcodec_vpu_reset_enc_handler, priv, rst_id);
118 
119 	fw = devm_kzalloc(&plat_dev->dev, sizeof(*fw), GFP_KERNEL);
120 	if (!fw)
121 		return ERR_PTR(-ENOMEM);
122 	fw->type = VPU;
123 	fw->ops = &mtk_vcodec_vpu_msg;
124 	fw->pdev = fw_pdev;
125 	fw->fw_use = fw_use;
126 
127 	return fw;
128 }
129