18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* Copyright (c) 2017 The Linux Foundation. All rights reserved. 38c2ecf20Sopenharmony_ci */ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include "msm_gem.h" 68c2ecf20Sopenharmony_ci#include "a5xx_gpu.h" 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* 98c2ecf20Sopenharmony_ci * Try to transition the preemption state from old to new. Return 108c2ecf20Sopenharmony_ci * true on success or false if the original state wasn't 'old' 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_cistatic inline bool try_preempt_state(struct a5xx_gpu *a5xx_gpu, 138c2ecf20Sopenharmony_ci enum preempt_state old, enum preempt_state new) 148c2ecf20Sopenharmony_ci{ 158c2ecf20Sopenharmony_ci enum preempt_state cur = atomic_cmpxchg(&a5xx_gpu->preempt_state, 168c2ecf20Sopenharmony_ci old, new); 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci return (cur == old); 198c2ecf20Sopenharmony_ci} 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * Force the preemption state to the specified state. This is used in cases 238c2ecf20Sopenharmony_ci * where the current state is known and won't change 248c2ecf20Sopenharmony_ci */ 258c2ecf20Sopenharmony_cistatic inline void set_preempt_state(struct a5xx_gpu *gpu, 268c2ecf20Sopenharmony_ci enum preempt_state new) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci /* 298c2ecf20Sopenharmony_ci * preempt_state may be read by other cores trying to trigger a 308c2ecf20Sopenharmony_ci * preemption or in the interrupt handler so barriers are needed 318c2ecf20Sopenharmony_ci * before... 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci smp_mb__before_atomic(); 348c2ecf20Sopenharmony_ci atomic_set(&gpu->preempt_state, new); 358c2ecf20Sopenharmony_ci /* ... and after*/ 368c2ecf20Sopenharmony_ci smp_mb__after_atomic(); 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* Write the most recent wptr for the given ring into the hardware */ 408c2ecf20Sopenharmony_cistatic inline void update_wptr(struct msm_gpu *gpu, struct msm_ringbuffer *ring) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci unsigned long flags; 438c2ecf20Sopenharmony_ci uint32_t wptr; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci if (!ring) 468c2ecf20Sopenharmony_ci return; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci spin_lock_irqsave(&ring->preempt_lock, flags); 498c2ecf20Sopenharmony_ci wptr = get_wptr(ring); 508c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ring->preempt_lock, flags); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci gpu_write(gpu, REG_A5XX_CP_RB_WPTR, wptr); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/* Return the highest priority ringbuffer with something in it */ 568c2ecf20Sopenharmony_cistatic struct msm_ringbuffer *get_next_ring(struct msm_gpu *gpu) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci unsigned long flags; 598c2ecf20Sopenharmony_ci int i; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci for (i = 0; i < gpu->nr_rings; i++) { 628c2ecf20Sopenharmony_ci bool empty; 638c2ecf20Sopenharmony_ci struct msm_ringbuffer *ring = gpu->rb[i]; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci spin_lock_irqsave(&ring->preempt_lock, flags); 668c2ecf20Sopenharmony_ci empty = (get_wptr(ring) == gpu->funcs->get_rptr(gpu, ring)); 678c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ring->preempt_lock, flags); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci if (!empty) 708c2ecf20Sopenharmony_ci return ring; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci return NULL; 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic void a5xx_preempt_timer(struct timer_list *t) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci struct a5xx_gpu *a5xx_gpu = from_timer(a5xx_gpu, t, preempt_timer); 798c2ecf20Sopenharmony_ci struct msm_gpu *gpu = &a5xx_gpu->base.base; 808c2ecf20Sopenharmony_ci struct drm_device *dev = gpu->dev; 818c2ecf20Sopenharmony_ci struct msm_drm_private *priv = dev->dev_private; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (!try_preempt_state(a5xx_gpu, PREEMPT_TRIGGERED, PREEMPT_FAULTED)) 848c2ecf20Sopenharmony_ci return; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci DRM_DEV_ERROR(dev->dev, "%s: preemption timed out\n", gpu->name); 878c2ecf20Sopenharmony_ci queue_work(priv->wq, &gpu->recover_work); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/* Try to trigger a preemption switch */ 918c2ecf20Sopenharmony_civoid a5xx_preempt_trigger(struct msm_gpu *gpu) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 948c2ecf20Sopenharmony_ci struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu); 958c2ecf20Sopenharmony_ci unsigned long flags; 968c2ecf20Sopenharmony_ci struct msm_ringbuffer *ring; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (gpu->nr_rings == 1) 998c2ecf20Sopenharmony_ci return; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci /* 1028c2ecf20Sopenharmony_ci * Try to start preemption by moving from NONE to START. If 1038c2ecf20Sopenharmony_ci * unsuccessful, a preemption is already in flight 1048c2ecf20Sopenharmony_ci */ 1058c2ecf20Sopenharmony_ci if (!try_preempt_state(a5xx_gpu, PREEMPT_NONE, PREEMPT_START)) 1068c2ecf20Sopenharmony_ci return; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci /* Get the next ring to preempt to */ 1098c2ecf20Sopenharmony_ci ring = get_next_ring(gpu); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci /* 1128c2ecf20Sopenharmony_ci * If no ring is populated or the highest priority ring is the current 1138c2ecf20Sopenharmony_ci * one do nothing except to update the wptr to the latest and greatest 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci if (!ring || (a5xx_gpu->cur_ring == ring)) { 1168c2ecf20Sopenharmony_ci /* 1178c2ecf20Sopenharmony_ci * Its possible that while a preemption request is in progress 1188c2ecf20Sopenharmony_ci * from an irq context, a user context trying to submit might 1198c2ecf20Sopenharmony_ci * fail to update the write pointer, because it determines 1208c2ecf20Sopenharmony_ci * that the preempt state is not PREEMPT_NONE. 1218c2ecf20Sopenharmony_ci * 1228c2ecf20Sopenharmony_ci * Close the race by introducing an intermediate 1238c2ecf20Sopenharmony_ci * state PREEMPT_ABORT to let the submit path 1248c2ecf20Sopenharmony_ci * know that the ringbuffer is not going to change 1258c2ecf20Sopenharmony_ci * and can safely update the write pointer. 1268c2ecf20Sopenharmony_ci */ 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci set_preempt_state(a5xx_gpu, PREEMPT_ABORT); 1298c2ecf20Sopenharmony_ci update_wptr(gpu, a5xx_gpu->cur_ring); 1308c2ecf20Sopenharmony_ci set_preempt_state(a5xx_gpu, PREEMPT_NONE); 1318c2ecf20Sopenharmony_ci return; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci /* Make sure the wptr doesn't update while we're in motion */ 1358c2ecf20Sopenharmony_ci spin_lock_irqsave(&ring->preempt_lock, flags); 1368c2ecf20Sopenharmony_ci a5xx_gpu->preempt[ring->id]->wptr = get_wptr(ring); 1378c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ring->preempt_lock, flags); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* Set the address of the incoming preemption record */ 1408c2ecf20Sopenharmony_ci gpu_write64(gpu, REG_A5XX_CP_CONTEXT_SWITCH_RESTORE_ADDR_LO, 1418c2ecf20Sopenharmony_ci REG_A5XX_CP_CONTEXT_SWITCH_RESTORE_ADDR_HI, 1428c2ecf20Sopenharmony_ci a5xx_gpu->preempt_iova[ring->id]); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci a5xx_gpu->next_ring = ring; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci /* Start a timer to catch a stuck preemption */ 1478c2ecf20Sopenharmony_ci mod_timer(&a5xx_gpu->preempt_timer, jiffies + msecs_to_jiffies(10000)); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci /* Set the preemption state to triggered */ 1508c2ecf20Sopenharmony_ci set_preempt_state(a5xx_gpu, PREEMPT_TRIGGERED); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci /* Make sure everything is written before hitting the button */ 1538c2ecf20Sopenharmony_ci wmb(); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci /* And actually start the preemption */ 1568c2ecf20Sopenharmony_ci gpu_write(gpu, REG_A5XX_CP_CONTEXT_SWITCH_CNTL, 1); 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_civoid a5xx_preempt_irq(struct msm_gpu *gpu) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci uint32_t status; 1628c2ecf20Sopenharmony_ci struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 1638c2ecf20Sopenharmony_ci struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu); 1648c2ecf20Sopenharmony_ci struct drm_device *dev = gpu->dev; 1658c2ecf20Sopenharmony_ci struct msm_drm_private *priv = dev->dev_private; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci if (!try_preempt_state(a5xx_gpu, PREEMPT_TRIGGERED, PREEMPT_PENDING)) 1688c2ecf20Sopenharmony_ci return; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci /* Delete the preemption watchdog timer */ 1718c2ecf20Sopenharmony_ci del_timer(&a5xx_gpu->preempt_timer); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci /* 1748c2ecf20Sopenharmony_ci * The hardware should be setting CP_CONTEXT_SWITCH_CNTL to zero before 1758c2ecf20Sopenharmony_ci * firing the interrupt, but there is a non zero chance of a hardware 1768c2ecf20Sopenharmony_ci * condition or a software race that could set it again before we have a 1778c2ecf20Sopenharmony_ci * chance to finish. If that happens, log and go for recovery 1788c2ecf20Sopenharmony_ci */ 1798c2ecf20Sopenharmony_ci status = gpu_read(gpu, REG_A5XX_CP_CONTEXT_SWITCH_CNTL); 1808c2ecf20Sopenharmony_ci if (unlikely(status)) { 1818c2ecf20Sopenharmony_ci set_preempt_state(a5xx_gpu, PREEMPT_FAULTED); 1828c2ecf20Sopenharmony_ci DRM_DEV_ERROR(dev->dev, "%s: Preemption failed to complete\n", 1838c2ecf20Sopenharmony_ci gpu->name); 1848c2ecf20Sopenharmony_ci queue_work(priv->wq, &gpu->recover_work); 1858c2ecf20Sopenharmony_ci return; 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci a5xx_gpu->cur_ring = a5xx_gpu->next_ring; 1898c2ecf20Sopenharmony_ci a5xx_gpu->next_ring = NULL; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci update_wptr(gpu, a5xx_gpu->cur_ring); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci set_preempt_state(a5xx_gpu, PREEMPT_NONE); 1948c2ecf20Sopenharmony_ci} 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_civoid a5xx_preempt_hw_init(struct msm_gpu *gpu) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 1998c2ecf20Sopenharmony_ci struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu); 2008c2ecf20Sopenharmony_ci int i; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* Always come up on rb 0 */ 2038c2ecf20Sopenharmony_ci a5xx_gpu->cur_ring = gpu->rb[0]; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci /* No preemption if we only have one ring */ 2068c2ecf20Sopenharmony_ci if (gpu->nr_rings == 1) 2078c2ecf20Sopenharmony_ci return; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci for (i = 0; i < gpu->nr_rings; i++) { 2108c2ecf20Sopenharmony_ci a5xx_gpu->preempt[i]->wptr = 0; 2118c2ecf20Sopenharmony_ci a5xx_gpu->preempt[i]->rptr = 0; 2128c2ecf20Sopenharmony_ci a5xx_gpu->preempt[i]->rbase = gpu->rb[i]->iova; 2138c2ecf20Sopenharmony_ci a5xx_gpu->preempt[i]->rptr_addr = shadowptr(a5xx_gpu, gpu->rb[i]); 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* Write a 0 to signal that we aren't switching pagetables */ 2178c2ecf20Sopenharmony_ci gpu_write64(gpu, REG_A5XX_CP_CONTEXT_SWITCH_SMMU_INFO_LO, 2188c2ecf20Sopenharmony_ci REG_A5XX_CP_CONTEXT_SWITCH_SMMU_INFO_HI, 0); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci /* Reset the preemption state */ 2218c2ecf20Sopenharmony_ci set_preempt_state(a5xx_gpu, PREEMPT_NONE); 2228c2ecf20Sopenharmony_ci} 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cistatic int preempt_init_ring(struct a5xx_gpu *a5xx_gpu, 2258c2ecf20Sopenharmony_ci struct msm_ringbuffer *ring) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci struct adreno_gpu *adreno_gpu = &a5xx_gpu->base; 2288c2ecf20Sopenharmony_ci struct msm_gpu *gpu = &adreno_gpu->base; 2298c2ecf20Sopenharmony_ci struct a5xx_preempt_record *ptr; 2308c2ecf20Sopenharmony_ci void *counters; 2318c2ecf20Sopenharmony_ci struct drm_gem_object *bo = NULL, *counters_bo = NULL; 2328c2ecf20Sopenharmony_ci u64 iova = 0, counters_iova = 0; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci ptr = msm_gem_kernel_new(gpu->dev, 2358c2ecf20Sopenharmony_ci A5XX_PREEMPT_RECORD_SIZE + A5XX_PREEMPT_COUNTER_SIZE, 2368c2ecf20Sopenharmony_ci MSM_BO_UNCACHED | MSM_BO_MAP_PRIV, gpu->aspace, &bo, &iova); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci if (IS_ERR(ptr)) 2398c2ecf20Sopenharmony_ci return PTR_ERR(ptr); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci /* The buffer to store counters needs to be unprivileged */ 2428c2ecf20Sopenharmony_ci counters = msm_gem_kernel_new(gpu->dev, 2438c2ecf20Sopenharmony_ci A5XX_PREEMPT_COUNTER_SIZE, 2448c2ecf20Sopenharmony_ci MSM_BO_UNCACHED, gpu->aspace, &counters_bo, &counters_iova); 2458c2ecf20Sopenharmony_ci if (IS_ERR(counters)) { 2468c2ecf20Sopenharmony_ci msm_gem_kernel_put(bo, gpu->aspace, true); 2478c2ecf20Sopenharmony_ci return PTR_ERR(counters); 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci msm_gem_object_set_name(bo, "preempt"); 2518c2ecf20Sopenharmony_ci msm_gem_object_set_name(counters_bo, "preempt_counters"); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci a5xx_gpu->preempt_bo[ring->id] = bo; 2548c2ecf20Sopenharmony_ci a5xx_gpu->preempt_counters_bo[ring->id] = counters_bo; 2558c2ecf20Sopenharmony_ci a5xx_gpu->preempt_iova[ring->id] = iova; 2568c2ecf20Sopenharmony_ci a5xx_gpu->preempt[ring->id] = ptr; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci /* Set up the defaults on the preemption record */ 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci ptr->magic = A5XX_PREEMPT_RECORD_MAGIC; 2618c2ecf20Sopenharmony_ci ptr->info = 0; 2628c2ecf20Sopenharmony_ci ptr->data = 0; 2638c2ecf20Sopenharmony_ci ptr->cntl = MSM_GPU_RB_CNTL_DEFAULT | AXXX_CP_RB_CNTL_NO_UPDATE; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci ptr->counter = counters_iova; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci return 0; 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_civoid a5xx_preempt_fini(struct msm_gpu *gpu) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 2738c2ecf20Sopenharmony_ci struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu); 2748c2ecf20Sopenharmony_ci int i; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci for (i = 0; i < gpu->nr_rings; i++) { 2778c2ecf20Sopenharmony_ci msm_gem_kernel_put(a5xx_gpu->preempt_bo[i], gpu->aspace, true); 2788c2ecf20Sopenharmony_ci msm_gem_kernel_put(a5xx_gpu->preempt_counters_bo[i], 2798c2ecf20Sopenharmony_ci gpu->aspace, true); 2808c2ecf20Sopenharmony_ci } 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_civoid a5xx_preempt_init(struct msm_gpu *gpu) 2848c2ecf20Sopenharmony_ci{ 2858c2ecf20Sopenharmony_ci struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 2868c2ecf20Sopenharmony_ci struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu); 2878c2ecf20Sopenharmony_ci int i; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci /* No preemption if we only have one ring */ 2908c2ecf20Sopenharmony_ci if (gpu->nr_rings <= 1) 2918c2ecf20Sopenharmony_ci return; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci for (i = 0; i < gpu->nr_rings; i++) { 2948c2ecf20Sopenharmony_ci if (preempt_init_ring(a5xx_gpu, gpu->rb[i])) { 2958c2ecf20Sopenharmony_ci /* 2968c2ecf20Sopenharmony_ci * On any failure our adventure is over. Clean up and 2978c2ecf20Sopenharmony_ci * set nr_rings to 1 to force preemption off 2988c2ecf20Sopenharmony_ci */ 2998c2ecf20Sopenharmony_ci a5xx_preempt_fini(gpu); 3008c2ecf20Sopenharmony_ci gpu->nr_rings = 1; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci return; 3038c2ecf20Sopenharmony_ci } 3048c2ecf20Sopenharmony_ci } 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci timer_setup(&a5xx_gpu->preempt_timer, a5xx_preempt_timer, 0); 3078c2ecf20Sopenharmony_ci} 308