1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* Copyright 2017-2019 Qiang Yu <[email protected]> */ 3 4 #include <linux/regulator/consumer.h> 5 #include <linux/reset.h> 6 #include <linux/clk.h> 7 #include <linux/dma-mapping.h> 8 #include <linux/platform_device.h> 9 10 #include "lima_device.h" 11 #include "lima_gp.h" 12 #include "lima_pp.h" 13 #include "lima_mmu.h" 14 #include "lima_pmu.h" 15 #include "lima_l2_cache.h" 16 #include "lima_dlbu.h" 17 #include "lima_bcast.h" 18 #include "lima_vm.h" 19 20 struct lima_ip_desc { 21 char *name; 22 char *irq_name; 23 bool must_have[lima_gpu_num]; 24 int offset[lima_gpu_num]; 25 26 int (*init)(struct lima_ip *ip); 27 void (*fini)(struct lima_ip *ip); 28 int (*resume)(struct lima_ip *ip); 29 void (*suspend)(struct lima_ip *ip); 30 }; 31 32 #define LIMA_IP_DESC(ipname, mst0, mst1, off0, off1, func, irq) \ 33 [lima_ip_##ipname] = { \ 34 .name = #ipname, \ 35 .irq_name = irq, \ 36 .must_have = { \ 37 [lima_gpu_mali400] = mst0, \ 38 [lima_gpu_mali450] = mst1, \ 39 }, \ 40 .offset = { \ 41 [lima_gpu_mali400] = off0, \ 42 [lima_gpu_mali450] = off1, \ 43 }, \ 44 .init = lima_##func##_init, \ 45 .fini = lima_##func##_fini, \ 46 .resume = lima_##func##_resume, \ 47 .suspend = lima_##func##_suspend, \ 48 } 49 50 static struct lima_ip_desc lima_ip_desc[lima_ip_num] = { 51 LIMA_IP_DESC(pmu, false, false, 0x02000, 0x02000, pmu, "pmu"), 52 LIMA_IP_DESC(l2_cache0, true, true, 0x01000, 0x10000, l2_cache, NULL), 53 LIMA_IP_DESC(l2_cache1, false, true, -1, 0x01000, l2_cache, NULL), 54 LIMA_IP_DESC(l2_cache2, false, false, -1, 0x11000, l2_cache, NULL), 55 LIMA_IP_DESC(gp, true, true, 0x00000, 0x00000, gp, "gp"), 56 LIMA_IP_DESC(pp0, true, true, 0x08000, 0x08000, pp, "pp0"), 57 LIMA_IP_DESC(pp1, false, false, 0x0A000, 0x0A000, pp, "pp1"), 58 LIMA_IP_DESC(pp2, false, false, 0x0C000, 0x0C000, pp, "pp2"), 59 LIMA_IP_DESC(pp3, false, false, 0x0E000, 0x0E000, pp, "pp3"), 60 LIMA_IP_DESC(pp4, false, false, -1, 0x28000, pp, "pp4"), 61 LIMA_IP_DESC(pp5, false, false, -1, 0x2A000, pp, "pp5"), 62 LIMA_IP_DESC(pp6, false, false, -1, 0x2C000, pp, "pp6"), 63 LIMA_IP_DESC(pp7, false, false, -1, 0x2E000, pp, "pp7"), 64 LIMA_IP_DESC(gpmmu, true, true, 0x03000, 0x03000, mmu, "gpmmu"), 65 LIMA_IP_DESC(ppmmu0, true, true, 0x04000, 0x04000, mmu, "ppmmu0"), 66 LIMA_IP_DESC(ppmmu1, false, false, 0x05000, 0x05000, mmu, "ppmmu1"), 67 LIMA_IP_DESC(ppmmu2, false, false, 0x06000, 0x06000, mmu, "ppmmu2"), 68 LIMA_IP_DESC(ppmmu3, false, false, 0x07000, 0x07000, mmu, "ppmmu3"), 69 LIMA_IP_DESC(ppmmu4, false, false, -1, 0x1C000, mmu, "ppmmu4"), 70 LIMA_IP_DESC(ppmmu5, false, false, -1, 0x1D000, mmu, "ppmmu5"), 71 LIMA_IP_DESC(ppmmu6, false, false, -1, 0x1E000, mmu, "ppmmu6"), 72 LIMA_IP_DESC(ppmmu7, false, false, -1, 0x1F000, mmu, "ppmmu7"), 73 LIMA_IP_DESC(dlbu, false, true, -1, 0x14000, dlbu, NULL), 74 LIMA_IP_DESC(bcast, false, true, -1, 0x13000, bcast, NULL), 75 LIMA_IP_DESC(pp_bcast, false, true, -1, 0x16000, pp_bcast, "pp"), 76 LIMA_IP_DESC(ppmmu_bcast, false, true, -1, 0x15000, mmu, NULL), 77 }; 78 79 const char *lima_ip_name(struct lima_ip *ip) 80 { 81 return lima_ip_desc[ip->id].name; 82 } 83 84 static int lima_clk_init(struct lima_device *dev) 85 { 86 int err; 87 88 dev->clk_bus = devm_clk_get(dev->dev, "bus"); 89 if (IS_ERR(dev->clk_bus)) { 90 err = PTR_ERR(dev->clk_bus); 91 if (err != -EPROBE_DEFER) 92 dev_err(dev->dev, "get bus clk failed %d\n", err); 93 return err; 94 } 95 96 dev->clk_gpu = devm_clk_get(dev->dev, "core"); 97 if (IS_ERR(dev->clk_gpu)) { 98 err = PTR_ERR(dev->clk_gpu); 99 if (err != -EPROBE_DEFER) 100 dev_err(dev->dev, "get core clk failed %d\n", err); 101 return err; 102 } 103 104 err = clk_prepare_enable(dev->clk_bus); 105 if (err) 106 return err; 107 108 err = clk_prepare_enable(dev->clk_gpu); 109 if (err) 110 goto error_out0; 111 112 dev->reset = devm_reset_control_array_get_optional_shared(dev->dev); 113 114 if (IS_ERR(dev->reset)) { 115 err = PTR_ERR(dev->reset); 116 if (err != -EPROBE_DEFER) 117 dev_err(dev->dev, "get reset controller failed %d\n", 118 err); 119 goto error_out1; 120 } else if (dev->reset != NULL) { 121 err = reset_control_deassert(dev->reset); 122 if (err) { 123 dev_err(dev->dev, 124 "reset controller deassert failed %d\n", err); 125 goto error_out1; 126 } 127 } 128 129 return 0; 130 131 error_out1: 132 clk_disable_unprepare(dev->clk_gpu); 133 error_out0: 134 clk_disable_unprepare(dev->clk_bus); 135 return err; 136 } 137 138 static void lima_clk_fini(struct lima_device *dev) 139 { 140 if (dev->reset != NULL) 141 reset_control_assert(dev->reset); 142 clk_disable_unprepare(dev->clk_gpu); 143 clk_disable_unprepare(dev->clk_bus); 144 } 145 146 static int lima_regulator_init(struct lima_device *dev) 147 { 148 int ret; 149 150 dev->regulator = devm_regulator_get_optional(dev->dev, "mali"); 151 if (IS_ERR(dev->regulator)) { 152 ret = PTR_ERR(dev->regulator); 153 dev->regulator = NULL; 154 if (ret == -ENODEV) 155 return 0; 156 if (ret != -EPROBE_DEFER) 157 dev_err(dev->dev, "failed to get regulator: %d\n", ret); 158 return ret; 159 } 160 161 ret = regulator_enable(dev->regulator); 162 if (ret < 0) { 163 dev_err(dev->dev, "failed to enable regulator: %d\n", ret); 164 return ret; 165 } 166 167 return 0; 168 } 169 170 static void lima_regulator_fini(struct lima_device *dev) 171 { 172 if (dev->regulator) 173 regulator_disable(dev->regulator); 174 } 175 176 static int lima_init_ip(struct lima_device *dev, int index) 177 { 178 struct platform_device *pdev = to_platform_device(dev->dev); 179 struct lima_ip_desc *desc = lima_ip_desc + index; 180 struct lima_ip *ip = dev->ip + index; 181 const char *irq_name = desc->irq_name; 182 int offset = desc->offset[dev->id]; 183 bool must = desc->must_have[dev->id]; 184 int err; 185 186 if (offset < 0) 187 return 0; 188 189 ip->dev = dev; 190 ip->id = index; 191 ip->iomem = dev->iomem + offset; 192 if (irq_name) { 193 err = must ? platform_get_irq_byname(pdev, irq_name) : 194 platform_get_irq_byname_optional(pdev, irq_name); 195 if (err < 0) 196 goto out; 197 ip->irq = err; 198 } 199 200 err = desc->init(ip); 201 if (!err) { 202 ip->present = true; 203 return 0; 204 } 205 206 out: 207 return must ? err : 0; 208 } 209 210 static void lima_fini_ip(struct lima_device *ldev, int index) 211 { 212 struct lima_ip_desc *desc = lima_ip_desc + index; 213 struct lima_ip *ip = ldev->ip + index; 214 215 if (ip->present) 216 desc->fini(ip); 217 } 218 219 static int lima_init_gp_pipe(struct lima_device *dev) 220 { 221 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_gp; 222 int err; 223 224 pipe->ldev = dev; 225 226 err = lima_sched_pipe_init(pipe, "gp"); 227 if (err) 228 return err; 229 230 pipe->l2_cache[pipe->num_l2_cache++] = dev->ip + lima_ip_l2_cache0; 231 pipe->mmu[pipe->num_mmu++] = dev->ip + lima_ip_gpmmu; 232 pipe->processor[pipe->num_processor++] = dev->ip + lima_ip_gp; 233 234 err = lima_gp_pipe_init(dev); 235 if (err) { 236 lima_sched_pipe_fini(pipe); 237 return err; 238 } 239 240 return 0; 241 } 242 243 static void lima_fini_gp_pipe(struct lima_device *dev) 244 { 245 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_gp; 246 247 lima_gp_pipe_fini(dev); 248 lima_sched_pipe_fini(pipe); 249 } 250 251 static int lima_init_pp_pipe(struct lima_device *dev) 252 { 253 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_pp; 254 int err, i; 255 256 pipe->ldev = dev; 257 258 err = lima_sched_pipe_init(pipe, "pp"); 259 if (err) 260 return err; 261 262 for (i = 0; i < LIMA_SCHED_PIPE_MAX_PROCESSOR; i++) { 263 struct lima_ip *pp = dev->ip + lima_ip_pp0 + i; 264 struct lima_ip *ppmmu = dev->ip + lima_ip_ppmmu0 + i; 265 struct lima_ip *l2_cache; 266 267 if (dev->id == lima_gpu_mali400) 268 l2_cache = dev->ip + lima_ip_l2_cache0; 269 else 270 l2_cache = dev->ip + lima_ip_l2_cache1 + (i >> 2); 271 272 if (pp->present && ppmmu->present && l2_cache->present) { 273 pipe->mmu[pipe->num_mmu++] = ppmmu; 274 pipe->processor[pipe->num_processor++] = pp; 275 if (!pipe->l2_cache[i >> 2]) 276 pipe->l2_cache[pipe->num_l2_cache++] = l2_cache; 277 } 278 } 279 280 if (dev->ip[lima_ip_bcast].present) { 281 pipe->bcast_processor = dev->ip + lima_ip_pp_bcast; 282 pipe->bcast_mmu = dev->ip + lima_ip_ppmmu_bcast; 283 } 284 285 err = lima_pp_pipe_init(dev); 286 if (err) { 287 lima_sched_pipe_fini(pipe); 288 return err; 289 } 290 291 return 0; 292 } 293 294 static void lima_fini_pp_pipe(struct lima_device *dev) 295 { 296 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_pp; 297 298 lima_pp_pipe_fini(dev); 299 lima_sched_pipe_fini(pipe); 300 } 301 302 int lima_device_init(struct lima_device *ldev) 303 { 304 struct platform_device *pdev = to_platform_device(ldev->dev); 305 int err, i; 306 307 dma_set_coherent_mask(ldev->dev, DMA_BIT_MASK(32)); 308 309 err = lima_clk_init(ldev); 310 if (err) 311 return err; 312 313 err = lima_regulator_init(ldev); 314 if (err) 315 goto err_out0; 316 317 ldev->empty_vm = lima_vm_create(ldev); 318 if (!ldev->empty_vm) { 319 err = -ENOMEM; 320 goto err_out1; 321 } 322 323 ldev->va_start = 0; 324 if (ldev->id == lima_gpu_mali450) { 325 ldev->va_end = LIMA_VA_RESERVE_START; 326 ldev->dlbu_cpu = dma_alloc_wc( 327 ldev->dev, LIMA_PAGE_SIZE, 328 &ldev->dlbu_dma, GFP_KERNEL | __GFP_NOWARN); 329 if (!ldev->dlbu_cpu) { 330 err = -ENOMEM; 331 goto err_out2; 332 } 333 } else 334 ldev->va_end = LIMA_VA_RESERVE_END; 335 336 ldev->iomem = devm_platform_ioremap_resource(pdev, 0); 337 if (IS_ERR(ldev->iomem)) { 338 dev_err(ldev->dev, "fail to ioremap iomem\n"); 339 err = PTR_ERR(ldev->iomem); 340 goto err_out3; 341 } 342 343 for (i = 0; i < lima_ip_num; i++) { 344 err = lima_init_ip(ldev, i); 345 if (err) 346 goto err_out4; 347 } 348 349 err = lima_init_gp_pipe(ldev); 350 if (err) 351 goto err_out4; 352 353 err = lima_init_pp_pipe(ldev); 354 if (err) 355 goto err_out5; 356 357 ldev->dump.magic = LIMA_DUMP_MAGIC; 358 ldev->dump.version_major = LIMA_DUMP_MAJOR; 359 ldev->dump.version_minor = LIMA_DUMP_MINOR; 360 INIT_LIST_HEAD(&ldev->error_task_list); 361 mutex_init(&ldev->error_task_list_lock); 362 363 dev_info(ldev->dev, "bus rate = %lu\n", clk_get_rate(ldev->clk_bus)); 364 dev_info(ldev->dev, "mod rate = %lu", clk_get_rate(ldev->clk_gpu)); 365 366 return 0; 367 368 err_out5: 369 lima_fini_gp_pipe(ldev); 370 err_out4: 371 while (--i >= 0) 372 lima_fini_ip(ldev, i); 373 err_out3: 374 if (ldev->dlbu_cpu) 375 dma_free_wc(ldev->dev, LIMA_PAGE_SIZE, 376 ldev->dlbu_cpu, ldev->dlbu_dma); 377 err_out2: 378 lima_vm_put(ldev->empty_vm); 379 err_out1: 380 lima_regulator_fini(ldev); 381 err_out0: 382 lima_clk_fini(ldev); 383 return err; 384 } 385 386 void lima_device_fini(struct lima_device *ldev) 387 { 388 int i; 389 struct lima_sched_error_task *et, *tmp; 390 391 list_for_each_entry_safe(et, tmp, &ldev->error_task_list, list) { 392 list_del(&et->list); 393 kvfree(et); 394 } 395 mutex_destroy(&ldev->error_task_list_lock); 396 397 lima_fini_pp_pipe(ldev); 398 lima_fini_gp_pipe(ldev); 399 400 for (i = lima_ip_num - 1; i >= 0; i--) 401 lima_fini_ip(ldev, i); 402 403 if (ldev->dlbu_cpu) 404 dma_free_wc(ldev->dev, LIMA_PAGE_SIZE, 405 ldev->dlbu_cpu, ldev->dlbu_dma); 406 407 lima_vm_put(ldev->empty_vm); 408 409 lima_regulator_fini(ldev); 410 411 lima_clk_fini(ldev); 412 } 413