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 2019 Collabora ltd. */ 5 6 #include <linux/bitfield.h> 7 #include <linux/bitmap.h> 8 #include <linux/delay.h> 9 #include <linux/dma-mapping.h> 10 #include <linux/interrupt.h> 11 #include <linux/io.h> 12 #include <linux/iopoll.h> 13 #include <linux/platform_device.h> 14 #include <linux/pm_runtime.h> 15 16 #include <drm/drm_drv.h> 17 #include <drm/drm_managed.h> 18 #include <drm/drm_print.h> 19 20 #include "panthor_device.h" 21 #include "panthor_gpu.h" 22 #include "panthor_gpu_regs.h" 23 #include "panthor_hw.h" 24 25 #define CREATE_TRACE_POINTS 26 #include "panthor_trace.h" 27 28 /** 29 * struct panthor_gpu - GPU block management data. 30 */ 31 struct panthor_gpu { 32 /** @iomem: CPU mapping of GPU_CONTROL iomem region */ 33 void __iomem *iomem; 34 35 /** @irq: GPU irq. */ 36 struct panthor_irq irq; 37 38 /** @reqs_lock: Lock protecting access to pending_reqs. */ 39 spinlock_t reqs_lock; 40 41 /** @pending_reqs: Pending GPU requests. */ 42 u32 pending_reqs; 43 44 /** @reqs_acked: GPU request wait queue. */ 45 wait_queue_head_t reqs_acked; 46 47 /** @cache_flush_lock: Lock to serialize cache flushes */ 48 struct mutex cache_flush_lock; 49 }; 50 51 #define GPU_INTERRUPTS_MASK \ 52 (GPU_IRQ_FAULT | \ 53 GPU_IRQ_PROTM_FAULT | \ 54 GPU_IRQ_RESET_COMPLETED | \ 55 GPU_IRQ_CLEAN_CACHES_COMPLETED) 56 57 #define GPU_POWER_INTERRUPTS_MASK \ 58 (GPU_IRQ_POWER_CHANGED | GPU_IRQ_POWER_CHANGED_ALL) 59 60 static void panthor_gpu_coherency_set(struct panthor_device *ptdev) 61 { 62 gpu_write(ptdev->gpu->iomem, GPU_COHERENCY_PROTOCOL, 63 ptdev->gpu_info.selected_coherency); 64 } 65 66 static void panthor_gpu_l2_config_set(struct panthor_device *ptdev) 67 { 68 struct panthor_gpu *gpu = ptdev->gpu; 69 const struct panthor_soc_data *data = ptdev->soc_data; 70 u32 l2_config; 71 u32 i; 72 73 if (!data || !data->asn_hash_enable) 74 return; 75 76 if (GPU_ARCH_MAJOR(ptdev->gpu_info.gpu_id) < 11) { 77 drm_err(&ptdev->base, "Custom ASN hash not supported by the device"); 78 return; 79 } 80 81 for (i = 0; i < ARRAY_SIZE(data->asn_hash); i++) 82 gpu_write(gpu->iomem, GPU_ASN_HASH(i), data->asn_hash[i]); 83 84 l2_config = gpu_read(gpu->iomem, GPU_L2_CONFIG); 85 l2_config |= GPU_L2_CONFIG_ASN_HASH_ENABLE; 86 gpu_write(gpu->iomem, GPU_L2_CONFIG, l2_config); 87 } 88 89 static void panthor_gpu_irq_handler(struct panthor_irq *pirq, u32 status) 90 { 91 struct panthor_device *ptdev = pirq->ptdev; 92 struct panthor_gpu *gpu = ptdev->gpu; 93 94 gpu_write(gpu->irq.iomem, INT_CLEAR, status); 95 96 if (tracepoint_enabled(gpu_power_status) && (status & GPU_POWER_INTERRUPTS_MASK)) 97 trace_gpu_power_status(ptdev->base.dev, 98 gpu_read64(gpu->iomem, SHADER_READY), 99 gpu_read64(gpu->iomem, TILER_READY), 100 gpu_read64(gpu->iomem, L2_READY)); 101 102 if (status & GPU_IRQ_FAULT) { 103 u32 fault_status = gpu_read(gpu->iomem, GPU_FAULT_STATUS); 104 u64 address = gpu_read64(gpu->iomem, GPU_FAULT_ADDR); 105 106 drm_warn(&ptdev->base, "GPU Fault 0x%08x (%s) at 0x%016llx\n", 107 fault_status, panthor_exception_name(ptdev, fault_status & 0xFF), 108 address); 109 } 110 if (status & GPU_IRQ_PROTM_FAULT) 111 drm_warn(&ptdev->base, "GPU Fault in protected mode\n"); 112 113 spin_lock(&ptdev->gpu->reqs_lock); 114 if (status & ptdev->gpu->pending_reqs) { 115 ptdev->gpu->pending_reqs &= ~status; 116 wake_up_all(&ptdev->gpu->reqs_acked); 117 } 118 spin_unlock(&ptdev->gpu->reqs_lock); 119 } 120 121 static irqreturn_t panthor_gpu_irq_threaded_handler(int irq, void *data) 122 { 123 return panthor_irq_default_threaded_handler(data, panthor_gpu_irq_handler); 124 } 125 126 /** 127 * panthor_gpu_unplug() - Called when the GPU is unplugged. 128 * @ptdev: Device to unplug. 129 */ 130 void panthor_gpu_unplug(struct panthor_device *ptdev) 131 { 132 unsigned long flags; 133 134 /* Make sure the IRQ handler is not running after that point. */ 135 if (!IS_ENABLED(CONFIG_PM) || pm_runtime_active(ptdev->base.dev)) 136 panthor_irq_suspend(&ptdev->gpu->irq); 137 138 /* Wake-up all waiters. */ 139 spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags); 140 ptdev->gpu->pending_reqs = 0; 141 wake_up_all(&ptdev->gpu->reqs_acked); 142 spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags); 143 } 144 145 /** 146 * panthor_gpu_init() - Initialize the GPU block 147 * @ptdev: Device. 148 * 149 * Return: 0 on success, a negative error code otherwise. 150 */ 151 int panthor_gpu_init(struct panthor_device *ptdev) 152 { 153 struct panthor_gpu *gpu; 154 u32 pa_bits; 155 int ret, irq; 156 157 gpu = drmm_kzalloc(&ptdev->base, sizeof(*gpu), GFP_KERNEL); 158 if (!gpu) 159 return -ENOMEM; 160 161 gpu->iomem = ptdev->iomem + GPU_CONTROL_BASE; 162 spin_lock_init(&gpu->reqs_lock); 163 init_waitqueue_head(&gpu->reqs_acked); 164 mutex_init(&gpu->cache_flush_lock); 165 ptdev->gpu = gpu; 166 167 dma_set_max_seg_size(ptdev->base.dev, UINT_MAX); 168 pa_bits = GPU_MMU_FEATURES_PA_BITS(ptdev->gpu_info.mmu_features); 169 ret = dma_set_mask_and_coherent(ptdev->base.dev, DMA_BIT_MASK(pa_bits)); 170 if (ret) 171 return ret; 172 173 irq = platform_get_irq_byname(to_platform_device(ptdev->base.dev), "gpu"); 174 if (irq < 0) 175 return irq; 176 177 ret = panthor_irq_request(ptdev, &ptdev->gpu->irq, irq, 178 ptdev->iomem + GPU_INT_BASE, "gpu", 179 panthor_gpu_irq_threaded_handler); 180 if (ret) 181 return ret; 182 183 panthor_irq_enable_events(&ptdev->gpu->irq, GPU_INTERRUPTS_MASK); 184 panthor_irq_resume(&ptdev->gpu->irq); 185 return 0; 186 } 187 188 int panthor_gpu_power_changed_on(struct panthor_device *ptdev) 189 { 190 guard(pm_runtime_active)(ptdev->base.dev); 191 192 panthor_irq_enable_events(&ptdev->gpu->irq, GPU_POWER_INTERRUPTS_MASK); 193 194 return 0; 195 } 196 197 void panthor_gpu_power_changed_off(struct panthor_device *ptdev) 198 { 199 guard(pm_runtime_active)(ptdev->base.dev); 200 201 panthor_irq_disable_events(&ptdev->gpu->irq, GPU_POWER_INTERRUPTS_MASK); 202 } 203 204 /** 205 * panthor_gpu_block_power_off() - Power-off a specific block of the GPU 206 * @ptdev: Device. 207 * @blk_name: Block name. 208 * @pwroff_reg: Power-off register for this block. 209 * @pwrtrans_reg: Power transition register for this block. 210 * @mask: Sub-elements to power-off. 211 * @timeout_us: Timeout in microseconds. 212 * 213 * Return: 0 on success, a negative error code otherwise. 214 */ 215 int panthor_gpu_block_power_off(struct panthor_device *ptdev, 216 const char *blk_name, 217 u32 pwroff_reg, u32 pwrtrans_reg, 218 u64 mask, u32 timeout_us) 219 { 220 struct panthor_gpu *gpu = ptdev->gpu; 221 u32 val; 222 int ret; 223 224 ret = gpu_read64_relaxed_poll_timeout(gpu->iomem, pwrtrans_reg, val, 225 !(mask & val), 100, timeout_us); 226 if (ret) { 227 drm_err(&ptdev->base, 228 "timeout waiting on %s:%llx power transition", blk_name, 229 mask); 230 return ret; 231 } 232 233 gpu_write64(gpu->iomem, pwroff_reg, mask); 234 235 ret = gpu_read64_relaxed_poll_timeout(gpu->iomem, pwrtrans_reg, val, 236 !(mask & val), 100, timeout_us); 237 if (ret) { 238 drm_err(&ptdev->base, 239 "timeout waiting on %s:%llx power transition", blk_name, 240 mask); 241 return ret; 242 } 243 244 return 0; 245 } 246 247 /** 248 * panthor_gpu_block_power_on() - Power-on a specific block of the GPU 249 * @ptdev: Device. 250 * @blk_name: Block name. 251 * @pwron_reg: Power-on register for this block. 252 * @pwrtrans_reg: Power transition register for this block. 253 * @rdy_reg: Power transition ready register. 254 * @mask: Sub-elements to power-on. 255 * @timeout_us: Timeout in microseconds. 256 * 257 * Return: 0 on success, a negative error code otherwise. 258 */ 259 int panthor_gpu_block_power_on(struct panthor_device *ptdev, 260 const char *blk_name, 261 u32 pwron_reg, u32 pwrtrans_reg, 262 u32 rdy_reg, u64 mask, u32 timeout_us) 263 { 264 struct panthor_gpu *gpu = ptdev->gpu; 265 u32 val; 266 int ret; 267 268 ret = gpu_read64_relaxed_poll_timeout(gpu->iomem, pwrtrans_reg, val, 269 !(mask & val), 100, timeout_us); 270 if (ret) { 271 drm_err(&ptdev->base, 272 "timeout waiting on %s:%llx power transition", blk_name, 273 mask); 274 return ret; 275 } 276 277 gpu_write64(gpu->iomem, pwron_reg, mask); 278 279 ret = gpu_read64_relaxed_poll_timeout(gpu->iomem, rdy_reg, val, 280 (mask & val) == val, 281 100, timeout_us); 282 if (ret) { 283 drm_err(&ptdev->base, "timeout waiting on %s:%llx readiness", 284 blk_name, mask); 285 return ret; 286 } 287 288 return 0; 289 } 290 291 void panthor_gpu_l2_power_off(struct panthor_device *ptdev) 292 { 293 panthor_gpu_power_off(ptdev, L2, ptdev->gpu_info.l2_present, 20000); 294 } 295 296 /** 297 * panthor_gpu_l2_power_on() - Power-on the L2-cache 298 * @ptdev: Device. 299 * 300 * Return: 0 on success, a negative error code otherwise. 301 */ 302 int panthor_gpu_l2_power_on(struct panthor_device *ptdev) 303 { 304 if (ptdev->gpu_info.l2_present != 1) { 305 /* 306 * Only support one core group now. 307 * ~(l2_present - 1) unsets all bits in l2_present except 308 * the bottom bit. (l2_present - 2) has all the bits in 309 * the first core group set. AND them together to generate 310 * a mask of cores in the first core group. 311 */ 312 u64 core_mask = ~(ptdev->gpu_info.l2_present - 1) & 313 (ptdev->gpu_info.l2_present - 2); 314 drm_info_once(&ptdev->base, "using only 1st core group (%lu cores from %lu)\n", 315 hweight64(core_mask), 316 hweight64(ptdev->gpu_info.shader_present)); 317 } 318 319 /* Set the desired coherency mode and L2 config before the power up of L2 */ 320 panthor_gpu_coherency_set(ptdev); 321 panthor_gpu_l2_config_set(ptdev); 322 323 return panthor_gpu_power_on(ptdev, L2, 1, 20000); 324 } 325 326 static inline void panthor_gpu_emit_flush_caches_tp(struct panthor_device *ptdev, 327 u64 start, u32 l2, u32 lsc, 328 u32 other, int ret) 329 { 330 u32 duration; 331 332 if (!tracepoint_enabled(gpu_cache_flush) || !start) 333 return; 334 335 if (check_sub_overflow(ktime_get_ns(), start, &duration)) 336 duration = U32_MAX; 337 338 trace_gpu_cache_flush(ptdev->base.dev, l2, lsc, other, duration, ret); 339 } 340 341 /** 342 * panthor_gpu_flush_caches() - Flush caches 343 * @ptdev: Device. 344 * @l2: L2 flush type. 345 * @lsc: LSC flush type. 346 * @other: Other flush type. 347 * 348 * Return: 0 on success, a negative error code otherwise. 349 */ 350 int panthor_gpu_flush_caches(struct panthor_device *ptdev, 351 u32 l2, u32 lsc, u32 other) 352 { 353 struct panthor_gpu *gpu = ptdev->gpu; 354 u64 start = 0; 355 int ret = 0; 356 357 /* Serialize cache flush operations. */ 358 guard(mutex)(&ptdev->gpu->cache_flush_lock); 359 360 scoped_guard(spinlock, &ptdev->gpu->reqs_lock) { 361 if (tracepoint_enabled(gpu_cache_flush)) 362 start = ktime_get_ns(); 363 364 if (!(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED)) { 365 ptdev->gpu->pending_reqs |= GPU_IRQ_CLEAN_CACHES_COMPLETED; 366 gpu_write(gpu->iomem, GPU_CMD, GPU_FLUSH_CACHES(l2, lsc, other)); 367 } else { 368 panthor_gpu_emit_flush_caches_tp(ptdev, start, l2, lsc, 369 other, -EIO); 370 return -EIO; 371 } 372 } 373 374 if (!wait_event_timeout(ptdev->gpu->reqs_acked, 375 !(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED), 376 msecs_to_jiffies(100))) { 377 scoped_guard(spinlock, &ptdev->gpu->reqs_lock) { 378 if ((ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED) != 0 && 379 !(gpu_read(gpu->irq.iomem, INT_RAWSTAT) & GPU_IRQ_CLEAN_CACHES_COMPLETED)) 380 ret = -ETIMEDOUT; 381 else 382 ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED; 383 } 384 } 385 386 panthor_gpu_emit_flush_caches_tp(ptdev, start, l2, lsc, other, ret); 387 388 if (ret) { 389 panthor_device_schedule_reset(ptdev); 390 drm_err(&ptdev->base, "Flush caches timeout"); 391 } 392 393 return ret; 394 } 395 396 /** 397 * panthor_gpu_soft_reset() - Issue a soft-reset 398 * @ptdev: Device. 399 * 400 * Return: 0 on success, a negative error code otherwise. 401 */ 402 int panthor_gpu_soft_reset(struct panthor_device *ptdev) 403 { 404 struct panthor_gpu *gpu = ptdev->gpu; 405 bool timedout = false; 406 407 scoped_guard(spinlock, &ptdev->gpu->reqs_lock) { 408 if (!drm_WARN_ON(&ptdev->base, 409 ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED)) { 410 ptdev->gpu->pending_reqs |= GPU_IRQ_RESET_COMPLETED; 411 gpu_write(gpu->irq.iomem, INT_CLEAR, GPU_IRQ_RESET_COMPLETED); 412 gpu_write(gpu->iomem, GPU_CMD, GPU_SOFT_RESET); 413 } 414 } 415 416 if (!wait_event_timeout(ptdev->gpu->reqs_acked, 417 !(ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED), 418 msecs_to_jiffies(100))) { 419 scoped_guard(spinlock, &ptdev->gpu->reqs_lock) { 420 if ((ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED) != 0 && 421 !(gpu_read(gpu->irq.iomem, INT_RAWSTAT) & GPU_IRQ_RESET_COMPLETED)) 422 timedout = true; 423 else 424 ptdev->gpu->pending_reqs &= ~GPU_IRQ_RESET_COMPLETED; 425 } 426 } 427 428 if (timedout) { 429 drm_err(&ptdev->base, "Soft reset timeout"); 430 return -ETIMEDOUT; 431 } 432 433 scoped_guard(spinlock, &ptdev->gpu->reqs_lock) { 434 ptdev->gpu->pending_reqs = 0; 435 } 436 437 return 0; 438 } 439 440 /** 441 * panthor_gpu_suspend() - Suspend the GPU block. 442 * @ptdev: Device. 443 * 444 * Suspend the GPU irq. This should be called last in the suspend procedure, 445 * after all other blocks have been suspented. 446 */ 447 void panthor_gpu_suspend(struct panthor_device *ptdev) 448 { 449 /* On a fast reset, simply power down the L2. */ 450 if (!ptdev->reset.fast) 451 panthor_hw_soft_reset(ptdev); 452 else 453 panthor_hw_l2_power_off(ptdev); 454 455 panthor_irq_suspend(&ptdev->gpu->irq); 456 } 457 458 /** 459 * panthor_gpu_resume() - Resume the GPU block. 460 * @ptdev: Device. 461 * 462 * Resume the IRQ handler and power-on the L2-cache. 463 * The FW takes care of powering the other blocks. 464 */ 465 void panthor_gpu_resume(struct panthor_device *ptdev) 466 { 467 panthor_irq_resume(&ptdev->gpu->irq); 468 panthor_hw_l2_power_on(ptdev); 469 } 470 471 u64 panthor_gpu_get_timestamp(struct panthor_device *ptdev) 472 { 473 return gpu_read64_counter(ptdev->gpu->iomem, GPU_TIMESTAMP); 474 } 475 476 u64 panthor_gpu_get_timestamp_offset(struct panthor_device *ptdev) 477 { 478 return gpu_read64(ptdev->gpu->iomem, GPU_TIMESTAMP_OFFSET); 479 } 480 481 u64 panthor_gpu_get_cycle_count(struct panthor_device *ptdev) 482 { 483 return gpu_read64_counter(ptdev->gpu->iomem, GPU_CYCLE_COUNT); 484 } 485 486 int panthor_gpu_coherency_init(struct panthor_device *ptdev) 487 { 488 BUILD_BUG_ON(GPU_COHERENCY_NONE != DRM_PANTHOR_GPU_COHERENCY_NONE); 489 BUILD_BUG_ON(GPU_COHERENCY_ACE_LITE != DRM_PANTHOR_GPU_COHERENCY_ACE_LITE); 490 BUILD_BUG_ON(GPU_COHERENCY_ACE != DRM_PANTHOR_GPU_COHERENCY_ACE); 491 492 /* Start with no coherency, and update it if the device is flagged coherent. */ 493 ptdev->gpu_info.selected_coherency = GPU_COHERENCY_NONE; 494 ptdev->coherent = device_get_dma_attr(ptdev->base.dev) == DEV_DMA_COHERENT; 495 496 if (!ptdev->coherent) 497 return 0; 498 499 /* Check if the ACE-Lite coherency protocol is actually supported by the GPU. 500 * ACE protocol has never been supported for command stream frontend GPUs. 501 */ 502 if ((gpu_read(ptdev->gpu->iomem, GPU_COHERENCY_FEATURES) & 503 GPU_COHERENCY_PROT_BIT(ACE_LITE))) { 504 ptdev->gpu_info.selected_coherency = GPU_COHERENCY_ACE_LITE; 505 return 0; 506 } 507 508 drm_err(&ptdev->base, "Coherency not supported by the device"); 509 return -ENOTSUPP; 510 } 511