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 void __iomem *base = core->base; 39 40 writel(0, base + WRAPPER_FW_START_ADDR); 41 writel(VENUS_FW_MEM_SIZE, base + WRAPPER_FW_END_ADDR); 42 writel(0, base + WRAPPER_CPA_START_ADDR); 43 writel(VENUS_FW_MEM_SIZE, base + WRAPPER_CPA_END_ADDR); 44 writel(VENUS_FW_MEM_SIZE, base + WRAPPER_NONPIX_START_ADDR); 45 writel(VENUS_FW_MEM_SIZE, base + WRAPPER_NONPIX_END_ADDR); 46 writel(0x0, base + WRAPPER_CPU_CGC_DIS); 47 writel(0x0, base + WRAPPER_CPU_CLOCK_CONFIG); 48 49 /* Bring ARM9 out of reset */ 50 writel(0, base + WRAPPER_A9SS_SW_RESET); 51 } 52 53 int venus_set_hw_state(struct venus_core *core, bool resume) 54 { 55 if (core->use_tz) 56 return qcom_scm_set_remote_state(resume, 0); 57 58 if (resume) 59 venus_reset_cpu(core); 60 else 61 writel(1, core->base + WRAPPER_A9SS_SW_RESET); 62 63 return 0; 64 } 65 66 static int venus_load_fw(struct venus_core *core, const char *fwname, 67 phys_addr_t *mem_phys, size_t *mem_size) 68 { 69 const struct firmware *mdt; 70 struct device_node *node; 71 struct device *dev; 72 struct resource r; 73 ssize_t fw_size; 74 void *mem_va; 75 int ret; 76 77 dev = core->dev; 78 node = of_parse_phandle(dev->of_node, "memory-region", 0); 79 if (!node) { 80 dev_err(dev, "no memory-region specified\n"); 81 return -EINVAL; 82 } 83 84 ret = of_address_to_resource(node, 0, &r); 85 if (ret) 86 return ret; 87 88 *mem_phys = r.start; 89 *mem_size = resource_size(&r); 90 91 if (*mem_size < VENUS_FW_MEM_SIZE) 92 return -EINVAL; 93 94 mem_va = memremap(r.start, *mem_size, MEMREMAP_WC); 95 if (!mem_va) { 96 dev_err(dev, "unable to map memory region: %pa+%zx\n", 97 &r.start, *mem_size); 98 return -ENOMEM; 99 } 100 101 ret = request_firmware(&mdt, fwname, dev); 102 if (ret < 0) 103 goto err_unmap; 104 105 fw_size = qcom_mdt_get_size(mdt); 106 if (fw_size < 0) { 107 ret = fw_size; 108 release_firmware(mdt); 109 goto err_unmap; 110 } 111 112 if (core->use_tz) 113 ret = qcom_mdt_load(dev, mdt, fwname, VENUS_PAS_ID, 114 mem_va, *mem_phys, *mem_size, NULL); 115 else 116 ret = qcom_mdt_load_no_init(dev, mdt, fwname, VENUS_PAS_ID, 117 mem_va, *mem_phys, *mem_size, NULL); 118 119 release_firmware(mdt); 120 121 err_unmap: 122 memunmap(mem_va); 123 return ret; 124 } 125 126 static int venus_boot_no_tz(struct venus_core *core, phys_addr_t mem_phys, 127 size_t mem_size) 128 { 129 struct iommu_domain *iommu; 130 struct device *dev; 131 int ret; 132 133 dev = core->fw.dev; 134 if (!dev) 135 return -EPROBE_DEFER; 136 137 iommu = core->fw.iommu_domain; 138 139 ret = iommu_map(iommu, VENUS_FW_START_ADDR, mem_phys, mem_size, 140 IOMMU_READ | IOMMU_WRITE | IOMMU_PRIV); 141 if (ret) { 142 dev_err(dev, "could not map video firmware region\n"); 143 return ret; 144 } 145 146 venus_reset_cpu(core); 147 148 return 0; 149 } 150 151 static int venus_shutdown_no_tz(struct venus_core *core) 152 { 153 struct iommu_domain *iommu; 154 size_t unmapped; 155 u32 reg; 156 struct device *dev = core->fw.dev; 157 void __iomem *base = core->base; 158 159 /* Assert the reset to ARM9 */ 160 reg = readl_relaxed(base + WRAPPER_A9SS_SW_RESET); 161 reg |= WRAPPER_A9SS_SW_RESET_BIT; 162 writel_relaxed(reg, base + WRAPPER_A9SS_SW_RESET); 163 164 /* Make sure reset is asserted before the mapping is removed */ 165 mb(); 166 167 iommu = core->fw.iommu_domain; 168 169 unmapped = iommu_unmap(iommu, VENUS_FW_START_ADDR, VENUS_FW_MEM_SIZE); 170 if (unmapped != VENUS_FW_MEM_SIZE) 171 dev_err(dev, "failed to unmap firmware\n"); 172 173 return 0; 174 } 175 176 int venus_boot(struct venus_core *core) 177 { 178 struct device *dev = core->dev; 179 phys_addr_t mem_phys; 180 size_t mem_size; 181 int ret; 182 183 if (!IS_ENABLED(CONFIG_QCOM_MDT_LOADER) || 184 (core->use_tz && !qcom_scm_is_available())) 185 return -EPROBE_DEFER; 186 187 ret = venus_load_fw(core, core->res->fwname, &mem_phys, &mem_size); 188 if (ret) { 189 dev_err(dev, "fail to load video firmware\n"); 190 return -EINVAL; 191 } 192 193 if (core->use_tz) 194 ret = qcom_scm_pas_auth_and_reset(VENUS_PAS_ID); 195 else 196 ret = venus_boot_no_tz(core, mem_phys, mem_size); 197 198 return ret; 199 } 200 201 int venus_shutdown(struct venus_core *core) 202 { 203 int ret; 204 205 if (core->use_tz) 206 ret = qcom_scm_pas_shutdown(VENUS_PAS_ID); 207 else 208 ret = venus_shutdown_no_tz(core); 209 210 return ret; 211 } 212 213 int venus_firmware_init(struct venus_core *core) 214 { 215 struct platform_device_info info; 216 struct iommu_domain *iommu_dom; 217 struct platform_device *pdev; 218 struct device_node *np; 219 int ret; 220 221 np = of_get_child_by_name(core->dev->of_node, "video-firmware"); 222 if (!np) { 223 core->use_tz = true; 224 return 0; 225 } 226 227 memset(&info, 0, sizeof(info)); 228 info.fwnode = &np->fwnode; 229 info.parent = core->dev; 230 info.name = np->name; 231 info.dma_mask = DMA_BIT_MASK(32); 232 233 pdev = platform_device_register_full(&info); 234 if (IS_ERR(pdev)) { 235 of_node_put(np); 236 return PTR_ERR(pdev); 237 } 238 239 pdev->dev.of_node = np; 240 241 ret = of_dma_configure(&pdev->dev, np, true); 242 if (ret) { 243 dev_err(core->dev, "dma configure fail\n"); 244 goto err_unregister; 245 } 246 247 core->fw.dev = &pdev->dev; 248 249 iommu_dom = iommu_domain_alloc(&platform_bus_type); 250 if (!iommu_dom) { 251 dev_err(core->fw.dev, "Failed to allocate iommu domain\n"); 252 ret = -ENOMEM; 253 goto err_unregister; 254 } 255 256 ret = iommu_attach_device(iommu_dom, core->fw.dev); 257 if (ret) { 258 dev_err(core->fw.dev, "could not attach device\n"); 259 goto err_iommu_free; 260 } 261 262 core->fw.iommu_domain = iommu_dom; 263 264 of_node_put(np); 265 266 return 0; 267 268 err_iommu_free: 269 iommu_domain_free(iommu_dom); 270 err_unregister: 271 platform_device_unregister(pdev); 272 of_node_put(np); 273 return ret; 274 } 275 276 void venus_firmware_deinit(struct venus_core *core) 277 { 278 struct iommu_domain *iommu; 279 280 if (!core->fw.dev) 281 return; 282 283 iommu = core->fw.iommu_domain; 284 285 iommu_detach_device(iommu, core->fw.dev); 286 iommu_domain_free(iommu); 287 288 platform_device_unregister(to_platform_device(core->fw.dev)); 289 } 290