xref: /linux/drivers/media/platform/qcom/venus/firmware.c (revision 8c3854d03bd7b86e8f36e6d9b07b4a6bc20deccd)
1 /*
2  * Copyright (C) 2017 Linaro Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 
15 #include <linux/device.h>
16 #include <linux/firmware.h>
17 #include <linux/kernel.h>
18 #include <linux/iommu.h>
19 #include <linux/io.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/platform_device.h>
23 #include <linux/of_device.h>
24 #include <linux/qcom_scm.h>
25 #include <linux/sizes.h>
26 #include <linux/soc/qcom/mdt_loader.h>
27 
28 #include "core.h"
29 #include "firmware.h"
30 #include "hfi_venus_io.h"
31 
32 #define VENUS_PAS_ID			9
33 #define VENUS_FW_MEM_SIZE		(6 * SZ_1M)
34 #define VENUS_FW_START_ADDR		0x0
35 
36 static void venus_reset_cpu(struct venus_core *core)
37 {
38 	u32 fw_size = core->fw.mapped_mem_size;
39 	void __iomem *base = core->base;
40 
41 	writel(0, base + WRAPPER_FW_START_ADDR);
42 	writel(fw_size, base + WRAPPER_FW_END_ADDR);
43 	writel(0, base + WRAPPER_CPA_START_ADDR);
44 	writel(fw_size, base + WRAPPER_CPA_END_ADDR);
45 	writel(fw_size, base + WRAPPER_NONPIX_START_ADDR);
46 	writel(fw_size, base + WRAPPER_NONPIX_END_ADDR);
47 	writel(0x0, base + WRAPPER_CPU_CGC_DIS);
48 	writel(0x0, base + WRAPPER_CPU_CLOCK_CONFIG);
49 
50 	/* Bring ARM9 out of reset */
51 	writel(0, base + WRAPPER_A9SS_SW_RESET);
52 }
53 
54 int venus_set_hw_state(struct venus_core *core, bool resume)
55 {
56 	if (core->use_tz)
57 		return qcom_scm_set_remote_state(resume, 0);
58 
59 	if (resume)
60 		venus_reset_cpu(core);
61 	else
62 		writel(1, core->base + WRAPPER_A9SS_SW_RESET);
63 
64 	return 0;
65 }
66 
67 static int venus_load_fw(struct venus_core *core, const char *fwname,
68 			 phys_addr_t *mem_phys, size_t *mem_size)
69 {
70 	const struct firmware *mdt;
71 	struct device_node *node;
72 	struct device *dev;
73 	struct resource r;
74 	ssize_t fw_size;
75 	void *mem_va;
76 	int ret;
77 
78 	*mem_phys = 0;
79 	*mem_size = 0;
80 
81 	dev = core->dev;
82 	node = of_parse_phandle(dev->of_node, "memory-region", 0);
83 	if (!node) {
84 		dev_err(dev, "no memory-region specified\n");
85 		return -EINVAL;
86 	}
87 
88 	ret = of_address_to_resource(node, 0, &r);
89 	if (ret)
90 		goto err_put_node;
91 
92 	ret = request_firmware(&mdt, fwname, dev);
93 	if (ret < 0)
94 		goto err_put_node;
95 
96 	fw_size = qcom_mdt_get_size(mdt);
97 	if (fw_size < 0) {
98 		ret = fw_size;
99 		goto err_release_fw;
100 	}
101 
102 	*mem_phys = r.start;
103 	*mem_size = resource_size(&r);
104 
105 	if (*mem_size < fw_size || fw_size > VENUS_FW_MEM_SIZE) {
106 		ret = -EINVAL;
107 		goto err_release_fw;
108 	}
109 
110 	mem_va = memremap(r.start, *mem_size, MEMREMAP_WC);
111 	if (!mem_va) {
112 		dev_err(dev, "unable to map memory region: %pa+%zx\n",
113 			&r.start, *mem_size);
114 		ret = -ENOMEM;
115 		goto err_release_fw;
116 	}
117 
118 	if (core->use_tz)
119 		ret = qcom_mdt_load(dev, mdt, fwname, VENUS_PAS_ID,
120 				    mem_va, *mem_phys, *mem_size, NULL);
121 	else
122 		ret = qcom_mdt_load_no_init(dev, mdt, fwname, VENUS_PAS_ID,
123 					    mem_va, *mem_phys, *mem_size, NULL);
124 
125 	memunmap(mem_va);
126 err_release_fw:
127 	release_firmware(mdt);
128 err_put_node:
129 	of_node_put(node);
130 	return ret;
131 }
132 
133 static int venus_boot_no_tz(struct venus_core *core, phys_addr_t mem_phys,
134 			    size_t mem_size)
135 {
136 	struct iommu_domain *iommu;
137 	struct device *dev;
138 	int ret;
139 
140 	dev = core->fw.dev;
141 	if (!dev)
142 		return -EPROBE_DEFER;
143 
144 	iommu = core->fw.iommu_domain;
145 	core->fw.mapped_mem_size = mem_size;
146 
147 	ret = iommu_map(iommu, VENUS_FW_START_ADDR, mem_phys, mem_size,
148 			IOMMU_READ | IOMMU_WRITE | IOMMU_PRIV);
149 	if (ret) {
150 		dev_err(dev, "could not map video firmware region\n");
151 		return ret;
152 	}
153 
154 	venus_reset_cpu(core);
155 
156 	return 0;
157 }
158 
159 static int venus_shutdown_no_tz(struct venus_core *core)
160 {
161 	const size_t mapped = core->fw.mapped_mem_size;
162 	struct iommu_domain *iommu;
163 	size_t unmapped;
164 	u32 reg;
165 	struct device *dev = core->fw.dev;
166 	void __iomem *base = core->base;
167 
168 	/* Assert the reset to ARM9 */
169 	reg = readl_relaxed(base + WRAPPER_A9SS_SW_RESET);
170 	reg |= WRAPPER_A9SS_SW_RESET_BIT;
171 	writel_relaxed(reg, base + WRAPPER_A9SS_SW_RESET);
172 
173 	/* Make sure reset is asserted before the mapping is removed */
174 	mb();
175 
176 	iommu = core->fw.iommu_domain;
177 
178 	unmapped = iommu_unmap(iommu, VENUS_FW_START_ADDR, mapped);
179 	if (unmapped != mapped)
180 		dev_err(dev, "failed to unmap firmware\n");
181 
182 	return 0;
183 }
184 
185 int venus_boot(struct venus_core *core)
186 {
187 	struct device *dev = core->dev;
188 	phys_addr_t mem_phys;
189 	size_t mem_size;
190 	int ret;
191 
192 	if (!IS_ENABLED(CONFIG_QCOM_MDT_LOADER) ||
193 	    (core->use_tz && !qcom_scm_is_available()))
194 		return -EPROBE_DEFER;
195 
196 	ret = venus_load_fw(core, core->res->fwname, &mem_phys, &mem_size);
197 	if (ret) {
198 		dev_err(dev, "fail to load video firmware\n");
199 		return -EINVAL;
200 	}
201 
202 	if (core->use_tz)
203 		ret = qcom_scm_pas_auth_and_reset(VENUS_PAS_ID);
204 	else
205 		ret = venus_boot_no_tz(core, mem_phys, mem_size);
206 
207 	return ret;
208 }
209 
210 int venus_shutdown(struct venus_core *core)
211 {
212 	int ret;
213 
214 	if (core->use_tz)
215 		ret = qcom_scm_pas_shutdown(VENUS_PAS_ID);
216 	else
217 		ret = venus_shutdown_no_tz(core);
218 
219 	return ret;
220 }
221 
222 int venus_firmware_init(struct venus_core *core)
223 {
224 	struct platform_device_info info;
225 	struct iommu_domain *iommu_dom;
226 	struct platform_device *pdev;
227 	struct device_node *np;
228 	int ret;
229 
230 	np = of_get_child_by_name(core->dev->of_node, "video-firmware");
231 	if (!np) {
232 		core->use_tz = true;
233 		return 0;
234 	}
235 
236 	memset(&info, 0, sizeof(info));
237 	info.fwnode = &np->fwnode;
238 	info.parent = core->dev;
239 	info.name = np->name;
240 	info.dma_mask = DMA_BIT_MASK(32);
241 
242 	pdev = platform_device_register_full(&info);
243 	if (IS_ERR(pdev)) {
244 		of_node_put(np);
245 		return PTR_ERR(pdev);
246 	}
247 
248 	pdev->dev.of_node = np;
249 
250 	ret = of_dma_configure(&pdev->dev, np, true);
251 	if (ret) {
252 		dev_err(core->dev, "dma configure fail\n");
253 		goto err_unregister;
254 	}
255 
256 	core->fw.dev = &pdev->dev;
257 
258 	iommu_dom = iommu_domain_alloc(&platform_bus_type);
259 	if (!iommu_dom) {
260 		dev_err(core->fw.dev, "Failed to allocate iommu domain\n");
261 		ret = -ENOMEM;
262 		goto err_unregister;
263 	}
264 
265 	ret = iommu_attach_device(iommu_dom, core->fw.dev);
266 	if (ret) {
267 		dev_err(core->fw.dev, "could not attach device\n");
268 		goto err_iommu_free;
269 	}
270 
271 	core->fw.iommu_domain = iommu_dom;
272 
273 	of_node_put(np);
274 
275 	return 0;
276 
277 err_iommu_free:
278 	iommu_domain_free(iommu_dom);
279 err_unregister:
280 	platform_device_unregister(pdev);
281 	of_node_put(np);
282 	return ret;
283 }
284 
285 void venus_firmware_deinit(struct venus_core *core)
286 {
287 	struct iommu_domain *iommu;
288 
289 	if (!core->fw.dev)
290 		return;
291 
292 	iommu = core->fw.iommu_domain;
293 
294 	iommu_detach_device(iommu, core->fw.dev);
295 	iommu_domain_free(iommu);
296 
297 	platform_device_unregister(to_platform_device(core->fw.dev));
298 }
299