1 // SPDX-License-Identifier: GPL-2.0 or MIT 2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */ 3 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */ 4 /* Copyright 2023 Collabora ltd. */ 5 6 #include <linux/clk.h> 7 #include <linux/mm.h> 8 #include <linux/platform_device.h> 9 #include <linux/pm_domain.h> 10 #include <linux/pm_runtime.h> 11 #include <linux/regulator/consumer.h> 12 #include <linux/reset.h> 13 14 #include <drm/drm_drv.h> 15 #include <drm/drm_managed.h> 16 17 #include "panthor_devfreq.h" 18 #include "panthor_device.h" 19 #include "panthor_fw.h" 20 #include "panthor_gpu.h" 21 #include "panthor_mmu.h" 22 #include "panthor_regs.h" 23 #include "panthor_sched.h" 24 25 static int panthor_clk_init(struct panthor_device *ptdev) 26 { 27 ptdev->clks.core = devm_clk_get(ptdev->base.dev, NULL); 28 if (IS_ERR(ptdev->clks.core)) 29 return dev_err_probe(ptdev->base.dev, 30 PTR_ERR(ptdev->clks.core), 31 "get 'core' clock failed"); 32 33 ptdev->clks.stacks = devm_clk_get_optional(ptdev->base.dev, "stacks"); 34 if (IS_ERR(ptdev->clks.stacks)) 35 return dev_err_probe(ptdev->base.dev, 36 PTR_ERR(ptdev->clks.stacks), 37 "get 'stacks' clock failed"); 38 39 ptdev->clks.coregroup = devm_clk_get_optional(ptdev->base.dev, "coregroup"); 40 if (IS_ERR(ptdev->clks.coregroup)) 41 return dev_err_probe(ptdev->base.dev, 42 PTR_ERR(ptdev->clks.coregroup), 43 "get 'coregroup' clock failed"); 44 45 drm_info(&ptdev->base, "clock rate = %lu\n", clk_get_rate(ptdev->clks.core)); 46 return 0; 47 } 48 49 void panthor_device_unplug(struct panthor_device *ptdev) 50 { 51 /* This function can be called from two different path: the reset work 52 * and the platform device remove callback. drm_dev_unplug() doesn't 53 * deal with concurrent callers, so we have to protect drm_dev_unplug() 54 * calls with our own lock, and bail out if the device is already 55 * unplugged. 56 */ 57 mutex_lock(&ptdev->unplug.lock); 58 if (drm_dev_is_unplugged(&ptdev->base)) { 59 /* Someone beat us, release the lock and wait for the unplug 60 * operation to be reported as done. 61 **/ 62 mutex_unlock(&ptdev->unplug.lock); 63 wait_for_completion(&ptdev->unplug.done); 64 return; 65 } 66 67 /* Call drm_dev_unplug() so any access to HW blocks happening after 68 * that point get rejected. 69 */ 70 drm_dev_unplug(&ptdev->base); 71 72 /* We do the rest of the unplug with the unplug lock released, 73 * future callers will wait on ptdev->unplug.done anyway. 74 */ 75 mutex_unlock(&ptdev->unplug.lock); 76 77 drm_WARN_ON(&ptdev->base, pm_runtime_get_sync(ptdev->base.dev) < 0); 78 79 /* Now, try to cleanly shutdown the GPU before the device resources 80 * get reclaimed. 81 */ 82 panthor_sched_unplug(ptdev); 83 panthor_fw_unplug(ptdev); 84 panthor_mmu_unplug(ptdev); 85 panthor_gpu_unplug(ptdev); 86 87 pm_runtime_dont_use_autosuspend(ptdev->base.dev); 88 pm_runtime_put_sync_suspend(ptdev->base.dev); 89 90 /* If PM is disabled, we need to call the suspend handler manually. */ 91 if (!IS_ENABLED(CONFIG_PM)) 92 panthor_device_suspend(ptdev->base.dev); 93 94 /* Report the unplug operation as done to unblock concurrent 95 * panthor_device_unplug() callers. 96 */ 97 complete_all(&ptdev->unplug.done); 98 } 99 100 static void panthor_device_reset_cleanup(struct drm_device *ddev, void *data) 101 { 102 struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base); 103 104 cancel_work_sync(&ptdev->reset.work); 105 destroy_workqueue(ptdev->reset.wq); 106 } 107 108 static void panthor_device_reset_work(struct work_struct *work) 109 { 110 struct panthor_device *ptdev = container_of(work, struct panthor_device, reset.work); 111 int ret = 0, cookie; 112 113 if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_ACTIVE) { 114 /* 115 * No need for a reset as the device has been (or will be) 116 * powered down 117 */ 118 atomic_set(&ptdev->reset.pending, 0); 119 return; 120 } 121 122 if (!drm_dev_enter(&ptdev->base, &cookie)) 123 return; 124 125 panthor_sched_pre_reset(ptdev); 126 panthor_fw_pre_reset(ptdev, true); 127 panthor_mmu_pre_reset(ptdev); 128 panthor_gpu_soft_reset(ptdev); 129 panthor_gpu_l2_power_on(ptdev); 130 panthor_mmu_post_reset(ptdev); 131 ret = panthor_fw_post_reset(ptdev); 132 atomic_set(&ptdev->reset.pending, 0); 133 panthor_sched_post_reset(ptdev, ret != 0); 134 drm_dev_exit(cookie); 135 136 if (ret) { 137 panthor_device_unplug(ptdev); 138 drm_err(&ptdev->base, "Failed to boot MCU after reset, making device unusable."); 139 } 140 } 141 142 static bool panthor_device_is_initialized(struct panthor_device *ptdev) 143 { 144 return !!ptdev->scheduler; 145 } 146 147 static void panthor_device_free_page(struct drm_device *ddev, void *data) 148 { 149 __free_page(data); 150 } 151 152 int panthor_device_init(struct panthor_device *ptdev) 153 { 154 u32 *dummy_page_virt; 155 struct resource *res; 156 struct page *p; 157 int ret; 158 159 ptdev->coherent = device_get_dma_attr(ptdev->base.dev) == DEV_DMA_COHERENT; 160 161 init_completion(&ptdev->unplug.done); 162 ret = drmm_mutex_init(&ptdev->base, &ptdev->unplug.lock); 163 if (ret) 164 return ret; 165 166 ret = drmm_mutex_init(&ptdev->base, &ptdev->pm.mmio_lock); 167 if (ret) 168 return ret; 169 170 atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED); 171 p = alloc_page(GFP_KERNEL | __GFP_ZERO); 172 if (!p) 173 return -ENOMEM; 174 175 ptdev->pm.dummy_latest_flush = p; 176 dummy_page_virt = page_address(p); 177 ret = drmm_add_action_or_reset(&ptdev->base, panthor_device_free_page, 178 ptdev->pm.dummy_latest_flush); 179 if (ret) 180 return ret; 181 182 /* 183 * Set the dummy page holding the latest flush to 1. This will cause the 184 * flush to avoided as we know it isn't necessary if the submission 185 * happens while the dummy page is mapped. Zero cannot be used because 186 * that means 'always flush'. 187 */ 188 *dummy_page_virt = 1; 189 190 INIT_WORK(&ptdev->reset.work, panthor_device_reset_work); 191 ptdev->reset.wq = alloc_ordered_workqueue("panthor-reset-wq", 0); 192 if (!ptdev->reset.wq) 193 return -ENOMEM; 194 195 ret = drmm_add_action_or_reset(&ptdev->base, panthor_device_reset_cleanup, NULL); 196 if (ret) 197 return ret; 198 199 ret = panthor_clk_init(ptdev); 200 if (ret) 201 return ret; 202 203 ret = panthor_devfreq_init(ptdev); 204 if (ret) 205 return ret; 206 207 ptdev->iomem = devm_platform_get_and_ioremap_resource(to_platform_device(ptdev->base.dev), 208 0, &res); 209 if (IS_ERR(ptdev->iomem)) 210 return PTR_ERR(ptdev->iomem); 211 212 ptdev->phys_addr = res->start; 213 214 ret = devm_pm_runtime_enable(ptdev->base.dev); 215 if (ret) 216 return ret; 217 218 ret = pm_runtime_resume_and_get(ptdev->base.dev); 219 if (ret) 220 return ret; 221 222 /* If PM is disabled, we need to call panthor_device_resume() manually. */ 223 if (!IS_ENABLED(CONFIG_PM)) { 224 ret = panthor_device_resume(ptdev->base.dev); 225 if (ret) 226 return ret; 227 } 228 229 ret = panthor_gpu_init(ptdev); 230 if (ret) 231 goto err_rpm_put; 232 233 ret = panthor_mmu_init(ptdev); 234 if (ret) 235 goto err_unplug_gpu; 236 237 ret = panthor_fw_init(ptdev); 238 if (ret) 239 goto err_unplug_mmu; 240 241 ret = panthor_sched_init(ptdev); 242 if (ret) 243 goto err_unplug_fw; 244 245 /* ~3 frames */ 246 pm_runtime_set_autosuspend_delay(ptdev->base.dev, 50); 247 pm_runtime_use_autosuspend(ptdev->base.dev); 248 249 ret = drm_dev_register(&ptdev->base, 0); 250 if (ret) 251 goto err_disable_autosuspend; 252 253 pm_runtime_put_autosuspend(ptdev->base.dev); 254 return 0; 255 256 err_disable_autosuspend: 257 pm_runtime_dont_use_autosuspend(ptdev->base.dev); 258 panthor_sched_unplug(ptdev); 259 260 err_unplug_fw: 261 panthor_fw_unplug(ptdev); 262 263 err_unplug_mmu: 264 panthor_mmu_unplug(ptdev); 265 266 err_unplug_gpu: 267 panthor_gpu_unplug(ptdev); 268 269 err_rpm_put: 270 pm_runtime_put_sync_suspend(ptdev->base.dev); 271 return ret; 272 } 273 274 #define PANTHOR_EXCEPTION(id) \ 275 [DRM_PANTHOR_EXCEPTION_ ## id] = { \ 276 .name = #id, \ 277 } 278 279 struct panthor_exception_info { 280 const char *name; 281 }; 282 283 static const struct panthor_exception_info panthor_exception_infos[] = { 284 PANTHOR_EXCEPTION(OK), 285 PANTHOR_EXCEPTION(TERMINATED), 286 PANTHOR_EXCEPTION(KABOOM), 287 PANTHOR_EXCEPTION(EUREKA), 288 PANTHOR_EXCEPTION(ACTIVE), 289 PANTHOR_EXCEPTION(CS_RES_TERM), 290 PANTHOR_EXCEPTION(CS_CONFIG_FAULT), 291 PANTHOR_EXCEPTION(CS_UNRECOVERABLE), 292 PANTHOR_EXCEPTION(CS_ENDPOINT_FAULT), 293 PANTHOR_EXCEPTION(CS_BUS_FAULT), 294 PANTHOR_EXCEPTION(CS_INSTR_INVALID), 295 PANTHOR_EXCEPTION(CS_CALL_STACK_OVERFLOW), 296 PANTHOR_EXCEPTION(CS_INHERIT_FAULT), 297 PANTHOR_EXCEPTION(INSTR_INVALID_PC), 298 PANTHOR_EXCEPTION(INSTR_INVALID_ENC), 299 PANTHOR_EXCEPTION(INSTR_BARRIER_FAULT), 300 PANTHOR_EXCEPTION(DATA_INVALID_FAULT), 301 PANTHOR_EXCEPTION(TILE_RANGE_FAULT), 302 PANTHOR_EXCEPTION(ADDR_RANGE_FAULT), 303 PANTHOR_EXCEPTION(IMPRECISE_FAULT), 304 PANTHOR_EXCEPTION(OOM), 305 PANTHOR_EXCEPTION(CSF_FW_INTERNAL_ERROR), 306 PANTHOR_EXCEPTION(CSF_RES_EVICTION_TIMEOUT), 307 PANTHOR_EXCEPTION(GPU_BUS_FAULT), 308 PANTHOR_EXCEPTION(GPU_SHAREABILITY_FAULT), 309 PANTHOR_EXCEPTION(SYS_SHAREABILITY_FAULT), 310 PANTHOR_EXCEPTION(GPU_CACHEABILITY_FAULT), 311 PANTHOR_EXCEPTION(TRANSLATION_FAULT_0), 312 PANTHOR_EXCEPTION(TRANSLATION_FAULT_1), 313 PANTHOR_EXCEPTION(TRANSLATION_FAULT_2), 314 PANTHOR_EXCEPTION(TRANSLATION_FAULT_3), 315 PANTHOR_EXCEPTION(TRANSLATION_FAULT_4), 316 PANTHOR_EXCEPTION(PERM_FAULT_0), 317 PANTHOR_EXCEPTION(PERM_FAULT_1), 318 PANTHOR_EXCEPTION(PERM_FAULT_2), 319 PANTHOR_EXCEPTION(PERM_FAULT_3), 320 PANTHOR_EXCEPTION(ACCESS_FLAG_1), 321 PANTHOR_EXCEPTION(ACCESS_FLAG_2), 322 PANTHOR_EXCEPTION(ACCESS_FLAG_3), 323 PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_IN), 324 PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT0), 325 PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT1), 326 PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT2), 327 PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT3), 328 PANTHOR_EXCEPTION(MEM_ATTR_FAULT_0), 329 PANTHOR_EXCEPTION(MEM_ATTR_FAULT_1), 330 PANTHOR_EXCEPTION(MEM_ATTR_FAULT_2), 331 PANTHOR_EXCEPTION(MEM_ATTR_FAULT_3), 332 }; 333 334 const char *panthor_exception_name(struct panthor_device *ptdev, u32 exception_code) 335 { 336 if (exception_code >= ARRAY_SIZE(panthor_exception_infos) || 337 !panthor_exception_infos[exception_code].name) 338 return "Unknown exception type"; 339 340 return panthor_exception_infos[exception_code].name; 341 } 342 343 static vm_fault_t panthor_mmio_vm_fault(struct vm_fault *vmf) 344 { 345 struct vm_area_struct *vma = vmf->vma; 346 struct panthor_device *ptdev = vma->vm_private_data; 347 u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT; 348 unsigned long pfn; 349 pgprot_t pgprot; 350 vm_fault_t ret; 351 bool active; 352 int cookie; 353 354 if (!drm_dev_enter(&ptdev->base, &cookie)) 355 return VM_FAULT_SIGBUS; 356 357 mutex_lock(&ptdev->pm.mmio_lock); 358 active = atomic_read(&ptdev->pm.state) == PANTHOR_DEVICE_PM_STATE_ACTIVE; 359 360 switch (offset) { 361 case DRM_PANTHOR_USER_FLUSH_ID_MMIO_OFFSET: 362 if (active) 363 pfn = __phys_to_pfn(ptdev->phys_addr + CSF_GPU_LATEST_FLUSH_ID); 364 else 365 pfn = page_to_pfn(ptdev->pm.dummy_latest_flush); 366 break; 367 368 default: 369 ret = VM_FAULT_SIGBUS; 370 goto out_unlock; 371 } 372 373 pgprot = vma->vm_page_prot; 374 if (active) 375 pgprot = pgprot_noncached(pgprot); 376 377 ret = vmf_insert_pfn_prot(vma, vmf->address, pfn, pgprot); 378 379 out_unlock: 380 mutex_unlock(&ptdev->pm.mmio_lock); 381 drm_dev_exit(cookie); 382 return ret; 383 } 384 385 static const struct vm_operations_struct panthor_mmio_vm_ops = { 386 .fault = panthor_mmio_vm_fault, 387 }; 388 389 int panthor_device_mmap_io(struct panthor_device *ptdev, struct vm_area_struct *vma) 390 { 391 u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT; 392 393 switch (offset) { 394 case DRM_PANTHOR_USER_FLUSH_ID_MMIO_OFFSET: 395 if (vma->vm_end - vma->vm_start != PAGE_SIZE || 396 (vma->vm_flags & (VM_WRITE | VM_EXEC))) 397 return -EINVAL; 398 399 break; 400 401 default: 402 return -EINVAL; 403 } 404 405 /* Defer actual mapping to the fault handler. */ 406 vma->vm_private_data = ptdev; 407 vma->vm_ops = &panthor_mmio_vm_ops; 408 vm_flags_set(vma, 409 VM_IO | VM_DONTCOPY | VM_DONTEXPAND | 410 VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP); 411 return 0; 412 } 413 414 int panthor_device_resume(struct device *dev) 415 { 416 struct panthor_device *ptdev = dev_get_drvdata(dev); 417 int ret, cookie; 418 419 if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_SUSPENDED) 420 return -EINVAL; 421 422 atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_RESUMING); 423 424 ret = clk_prepare_enable(ptdev->clks.core); 425 if (ret) 426 goto err_set_suspended; 427 428 ret = clk_prepare_enable(ptdev->clks.stacks); 429 if (ret) 430 goto err_disable_core_clk; 431 432 ret = clk_prepare_enable(ptdev->clks.coregroup); 433 if (ret) 434 goto err_disable_stacks_clk; 435 436 ret = panthor_devfreq_resume(ptdev); 437 if (ret) 438 goto err_disable_coregroup_clk; 439 440 if (panthor_device_is_initialized(ptdev) && 441 drm_dev_enter(&ptdev->base, &cookie)) { 442 panthor_gpu_resume(ptdev); 443 panthor_mmu_resume(ptdev); 444 ret = drm_WARN_ON(&ptdev->base, panthor_fw_resume(ptdev)); 445 if (!ret) { 446 panthor_sched_resume(ptdev); 447 } else { 448 panthor_mmu_suspend(ptdev); 449 panthor_gpu_suspend(ptdev); 450 } 451 452 drm_dev_exit(cookie); 453 454 if (ret) 455 goto err_suspend_devfreq; 456 } 457 458 if (atomic_read(&ptdev->reset.pending)) 459 queue_work(ptdev->reset.wq, &ptdev->reset.work); 460 461 /* Clear all IOMEM mappings pointing to this device after we've 462 * resumed. This way the fake mappings pointing to the dummy pages 463 * are removed and the real iomem mapping will be restored on next 464 * access. 465 */ 466 mutex_lock(&ptdev->pm.mmio_lock); 467 unmap_mapping_range(ptdev->base.anon_inode->i_mapping, 468 DRM_PANTHOR_USER_MMIO_OFFSET, 0, 1); 469 atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_ACTIVE); 470 mutex_unlock(&ptdev->pm.mmio_lock); 471 return 0; 472 473 err_suspend_devfreq: 474 panthor_devfreq_suspend(ptdev); 475 476 err_disable_coregroup_clk: 477 clk_disable_unprepare(ptdev->clks.coregroup); 478 479 err_disable_stacks_clk: 480 clk_disable_unprepare(ptdev->clks.stacks); 481 482 err_disable_core_clk: 483 clk_disable_unprepare(ptdev->clks.core); 484 485 err_set_suspended: 486 atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED); 487 return ret; 488 } 489 490 int panthor_device_suspend(struct device *dev) 491 { 492 struct panthor_device *ptdev = dev_get_drvdata(dev); 493 int ret, cookie; 494 495 if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_ACTIVE) 496 return -EINVAL; 497 498 /* Clear all IOMEM mappings pointing to this device before we 499 * shutdown the power-domain and clocks. Failing to do that results 500 * in external aborts when the process accesses the iomem region. 501 * We change the state and call unmap_mapping_range() with the 502 * mmio_lock held to make sure the vm_fault handler won't set up 503 * invalid mappings. 504 */ 505 mutex_lock(&ptdev->pm.mmio_lock); 506 atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDING); 507 unmap_mapping_range(ptdev->base.anon_inode->i_mapping, 508 DRM_PANTHOR_USER_MMIO_OFFSET, 0, 1); 509 mutex_unlock(&ptdev->pm.mmio_lock); 510 511 if (panthor_device_is_initialized(ptdev) && 512 drm_dev_enter(&ptdev->base, &cookie)) { 513 cancel_work_sync(&ptdev->reset.work); 514 515 /* We prepare everything as if we were resetting the GPU. 516 * The end of the reset will happen in the resume path though. 517 */ 518 panthor_sched_suspend(ptdev); 519 panthor_fw_suspend(ptdev); 520 panthor_mmu_suspend(ptdev); 521 panthor_gpu_suspend(ptdev); 522 drm_dev_exit(cookie); 523 } 524 525 ret = panthor_devfreq_suspend(ptdev); 526 if (ret) { 527 if (panthor_device_is_initialized(ptdev) && 528 drm_dev_enter(&ptdev->base, &cookie)) { 529 panthor_gpu_resume(ptdev); 530 panthor_mmu_resume(ptdev); 531 drm_WARN_ON(&ptdev->base, panthor_fw_resume(ptdev)); 532 panthor_sched_resume(ptdev); 533 drm_dev_exit(cookie); 534 } 535 536 goto err_set_active; 537 } 538 539 clk_disable_unprepare(ptdev->clks.coregroup); 540 clk_disable_unprepare(ptdev->clks.stacks); 541 clk_disable_unprepare(ptdev->clks.core); 542 atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED); 543 return 0; 544 545 err_set_active: 546 /* If something failed and we have to revert back to an 547 * active state, we also need to clear the MMIO userspace 548 * mappings, so any dumb pages that were mapped while we 549 * were trying to suspend gets invalidated. 550 */ 551 mutex_lock(&ptdev->pm.mmio_lock); 552 atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_ACTIVE); 553 unmap_mapping_range(ptdev->base.anon_inode->i_mapping, 554 DRM_PANTHOR_USER_MMIO_OFFSET, 0, 1); 555 mutex_unlock(&ptdev->pm.mmio_lock); 556 return ret; 557 } 558