1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, The Linux Foundation. All rights reserved. */
3 /* Copyright (c) 2023 Collabora, Ltd. */
4 /* Copyright (c) 2024 Valve Corporation */
8 #include "a6xx_gmu.xml.h"
10 #include "msm_gpu_trace.h"
13 * Try to transition the preemption state from old to new. Return
14 * true on success or false if the original state wasn't 'old'
16 static inline bool try_preempt_state(struct a6xx_gpu *a6xx_gpu,
17 enum a6xx_preempt_state old, enum a6xx_preempt_state new)
19 enum a6xx_preempt_state cur = atomic_cmpxchg(&a6xx_gpu->preempt_state,
26 * Force the preemption state to the specified state. This is used in cases
27 * where the current state is known and won't change
29 static inline void set_preempt_state(struct a6xx_gpu *gpu,
30 enum a6xx_preempt_state new)
33 * preempt_state may be read by other cores trying to trigger a
34 * preemption or in the interrupt handler so barriers are needed
37 smp_mb__before_atomic();
38 atomic_set(&gpu->preempt_state, new);
40 smp_mb__after_atomic();
43 /* Write the most recent wptr for the given ring into the hardware */
44 static inline void update_wptr(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
49 spin_lock_irqsave(&ring->preempt_lock, flags);
51 if (ring->restore_wptr) {
52 wptr = get_wptr(ring);
54 gpu_write(gpu, REG_A6XX_CP_RB_WPTR, wptr);
56 ring->restore_wptr = false;
59 spin_unlock_irqrestore(&ring->preempt_lock, flags);
62 /* Return the highest priority ringbuffer with something in it */
63 static struct msm_ringbuffer *get_next_ring(struct msm_gpu *gpu)
65 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
66 struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
71 for (i = 0; i < gpu->nr_rings; i++) {
73 struct msm_ringbuffer *ring = gpu->rb[i];
75 spin_lock_irqsave(&ring->preempt_lock, flags);
76 empty = (get_wptr(ring) == gpu->funcs->get_rptr(gpu, ring));
77 if (!empty && ring == a6xx_gpu->cur_ring)
78 empty = ring->memptrs->fence == a6xx_gpu->last_seqno[i];
79 spin_unlock_irqrestore(&ring->preempt_lock, flags);
88 static void a6xx_preempt_timer(struct timer_list *t)
90 struct a6xx_gpu *a6xx_gpu = from_timer(a6xx_gpu, t, preempt_timer);
91 struct msm_gpu *gpu = &a6xx_gpu->base.base;
92 struct drm_device *dev = gpu->dev;
94 if (!try_preempt_state(a6xx_gpu, PREEMPT_TRIGGERED, PREEMPT_FAULTED))
97 dev_err(dev->dev, "%s: preemption timed out\n", gpu->name);
98 kthread_queue_work(gpu->worker, &gpu->recover_work);
101 static void preempt_prepare_postamble(struct a6xx_gpu *a6xx_gpu)
103 u32 *postamble = a6xx_gpu->preempt_postamble_ptr;
106 postamble[count++] = PKT7(CP_REG_RMW, 3);
107 postamble[count++] = REG_A6XX_RBBM_PERFCTR_SRAM_INIT_CMD;
108 postamble[count++] = 0;
109 postamble[count++] = 1;
111 postamble[count++] = PKT7(CP_WAIT_REG_MEM, 6);
112 postamble[count++] = CP_WAIT_REG_MEM_0_FUNCTION(WRITE_EQ);
113 postamble[count++] = CP_WAIT_REG_MEM_1_POLL_ADDR_LO(
114 REG_A6XX_RBBM_PERFCTR_SRAM_INIT_STATUS);
115 postamble[count++] = CP_WAIT_REG_MEM_2_POLL_ADDR_HI(0);
116 postamble[count++] = CP_WAIT_REG_MEM_3_REF(0x1);
117 postamble[count++] = CP_WAIT_REG_MEM_4_MASK(0x1);
118 postamble[count++] = CP_WAIT_REG_MEM_5_DELAY_LOOP_CYCLES(0);
120 a6xx_gpu->preempt_postamble_len = count;
122 a6xx_gpu->postamble_enabled = true;
125 static void preempt_disable_postamble(struct a6xx_gpu *a6xx_gpu)
127 u32 *postamble = a6xx_gpu->preempt_postamble_ptr;
130 * Disable the postamble by replacing the first packet header with a NOP
131 * that covers the whole buffer.
133 *postamble = PKT7(CP_NOP, (a6xx_gpu->preempt_postamble_len - 1));
135 a6xx_gpu->postamble_enabled = false;
138 void a6xx_preempt_irq(struct msm_gpu *gpu)
141 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
142 struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
143 struct drm_device *dev = gpu->dev;
145 if (!try_preempt_state(a6xx_gpu, PREEMPT_TRIGGERED, PREEMPT_PENDING))
148 /* Delete the preemption watchdog timer */
149 del_timer(&a6xx_gpu->preempt_timer);
152 * The hardware should be setting the stop bit of CP_CONTEXT_SWITCH_CNTL
153 * to zero before firing the interrupt, but there is a non zero chance
154 * of a hardware condition or a software race that could set it again
155 * before we have a chance to finish. If that happens, log and go for
158 status = gpu_read(gpu, REG_A6XX_CP_CONTEXT_SWITCH_CNTL);
159 if (unlikely(status & A6XX_CP_CONTEXT_SWITCH_CNTL_STOP)) {
160 DRM_DEV_ERROR(&gpu->pdev->dev,
161 "!!!!!!!!!!!!!!!! preemption faulted !!!!!!!!!!!!!! irq\n");
162 set_preempt_state(a6xx_gpu, PREEMPT_FAULTED);
163 dev_err(dev->dev, "%s: Preemption failed to complete\n",
165 kthread_queue_work(gpu->worker, &gpu->recover_work);
169 a6xx_gpu->cur_ring = a6xx_gpu->next_ring;
170 a6xx_gpu->next_ring = NULL;
172 set_preempt_state(a6xx_gpu, PREEMPT_FINISH);
174 update_wptr(gpu, a6xx_gpu->cur_ring);
176 set_preempt_state(a6xx_gpu, PREEMPT_NONE);
178 trace_msm_gpu_preemption_irq(a6xx_gpu->cur_ring->id);
181 * Retrigger preemption to avoid a deadlock that might occur when preemption
182 * is skipped due to it being already in flight when requested.
184 a6xx_preempt_trigger(gpu);
187 void a6xx_preempt_hw_init(struct msm_gpu *gpu)
189 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
190 struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
193 /* No preemption if we only have one ring */
194 if (gpu->nr_rings == 1)
197 for (i = 0; i < gpu->nr_rings; i++) {
198 struct a6xx_preempt_record *record_ptr = a6xx_gpu->preempt[i];
200 record_ptr->wptr = 0;
201 record_ptr->rptr = 0;
202 record_ptr->rptr_addr = shadowptr(a6xx_gpu, gpu->rb[i]);
203 record_ptr->info = 0;
204 record_ptr->data = 0;
205 record_ptr->rbase = gpu->rb[i]->iova;
208 /* Write a 0 to signal that we aren't switching pagetables */
209 gpu_write64(gpu, REG_A6XX_CP_CONTEXT_SWITCH_SMMU_INFO, 0);
211 /* Enable the GMEM save/restore feature for preemption */
212 gpu_write(gpu, REG_A6XX_RB_CONTEXT_SWITCH_GMEM_SAVE_RESTORE, 0x1);
214 /* Reset the preemption state */
215 set_preempt_state(a6xx_gpu, PREEMPT_NONE);
217 spin_lock_init(&a6xx_gpu->eval_lock);
219 /* Always come up on rb 0 */
220 a6xx_gpu->cur_ring = gpu->rb[0];
223 void a6xx_preempt_trigger(struct msm_gpu *gpu)
225 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
226 struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
228 struct msm_ringbuffer *ring;
232 if (gpu->nr_rings == 1)
236 * Lock to make sure another thread attempting preemption doesn't skip it
237 * while we are still evaluating the next ring. This makes sure the other
238 * thread does start preemption if we abort it and avoids a soft lock.
240 spin_lock_irqsave(&a6xx_gpu->eval_lock, flags);
243 * Try to start preemption by moving from NONE to START. If
244 * unsuccessful, a preemption is already in flight
246 if (!try_preempt_state(a6xx_gpu, PREEMPT_NONE, PREEMPT_START)) {
247 spin_unlock_irqrestore(&a6xx_gpu->eval_lock, flags);
251 cntl = A6XX_CP_CONTEXT_SWITCH_CNTL_LEVEL(a6xx_gpu->preempt_level);
253 if (a6xx_gpu->skip_save_restore)
254 cntl |= A6XX_CP_CONTEXT_SWITCH_CNTL_SKIP_SAVE_RESTORE;
256 if (a6xx_gpu->uses_gmem)
257 cntl |= A6XX_CP_CONTEXT_SWITCH_CNTL_USES_GMEM;
259 cntl |= A6XX_CP_CONTEXT_SWITCH_CNTL_STOP;
261 /* Get the next ring to preempt to */
262 ring = get_next_ring(gpu);
265 * If no ring is populated or the highest priority ring is the current
266 * one do nothing except to update the wptr to the latest and greatest
268 if (!ring || (a6xx_gpu->cur_ring == ring)) {
269 set_preempt_state(a6xx_gpu, PREEMPT_FINISH);
270 update_wptr(gpu, a6xx_gpu->cur_ring);
271 set_preempt_state(a6xx_gpu, PREEMPT_NONE);
272 spin_unlock_irqrestore(&a6xx_gpu->eval_lock, flags);
276 spin_unlock_irqrestore(&a6xx_gpu->eval_lock, flags);
278 spin_lock_irqsave(&ring->preempt_lock, flags);
280 struct a7xx_cp_smmu_info *smmu_info_ptr =
281 a6xx_gpu->preempt_smmu[ring->id];
282 struct a6xx_preempt_record *record_ptr = a6xx_gpu->preempt[ring->id];
283 u64 ttbr0 = ring->memptrs->ttbr0;
284 u32 context_idr = ring->memptrs->context_idr;
286 smmu_info_ptr->ttbr0 = ttbr0;
287 smmu_info_ptr->context_idr = context_idr;
288 record_ptr->wptr = get_wptr(ring);
291 * The GPU will write the wptr we set above when we preempt. Reset
292 * restore_wptr to make sure that we don't write WPTR to the same
293 * thing twice. It's still possible subsequent submissions will update
294 * wptr again, in which case they will set the flag to true. This has
295 * to be protected by the lock for setting the flag and updating wptr
298 ring->restore_wptr = false;
300 trace_msm_gpu_preemption_trigger(a6xx_gpu->cur_ring->id, ring->id);
302 spin_unlock_irqrestore(&ring->preempt_lock, flags);
305 REG_A6XX_CP_CONTEXT_SWITCH_SMMU_INFO,
306 a6xx_gpu->preempt_smmu_iova[ring->id]);
309 REG_A6XX_CP_CONTEXT_SWITCH_PRIV_NON_SECURE_RESTORE_ADDR,
310 a6xx_gpu->preempt_iova[ring->id]);
312 a6xx_gpu->next_ring = ring;
314 /* Start a timer to catch a stuck preemption */
315 mod_timer(&a6xx_gpu->preempt_timer, jiffies + msecs_to_jiffies(10000));
317 /* Enable or disable postamble as needed */
318 sysprof = refcount_read(&a6xx_gpu->base.base.sysprof_active) > 1;
320 if (!sysprof && !a6xx_gpu->postamble_enabled)
321 preempt_prepare_postamble(a6xx_gpu);
323 if (sysprof && a6xx_gpu->postamble_enabled)
324 preempt_disable_postamble(a6xx_gpu);
326 /* Set the preemption state to triggered */
327 set_preempt_state(a6xx_gpu, PREEMPT_TRIGGERED);
329 /* Trigger the preemption */
330 gpu_write(gpu, REG_A6XX_CP_CONTEXT_SWITCH_CNTL, cntl);
333 static int preempt_init_ring(struct a6xx_gpu *a6xx_gpu,
334 struct msm_ringbuffer *ring)
336 struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
337 struct msm_gpu *gpu = &adreno_gpu->base;
338 struct drm_gem_object *bo = NULL;
344 ptr = msm_gem_kernel_new(gpu->dev,
345 PREEMPT_RECORD_SIZE(adreno_gpu),
346 MSM_BO_WC | MSM_BO_MAP_PRIV, gpu->aspace, &bo, &iova);
351 memset(ptr, 0, PREEMPT_RECORD_SIZE(adreno_gpu));
353 msm_gem_object_set_name(bo, "preempt_record ring%d", ring->id);
355 a6xx_gpu->preempt_bo[ring->id] = bo;
356 a6xx_gpu->preempt_iova[ring->id] = iova;
357 a6xx_gpu->preempt[ring->id] = ptr;
359 struct a6xx_preempt_record *record_ptr = ptr;
361 ptr = msm_gem_kernel_new(gpu->dev,
362 PREEMPT_SMMU_INFO_SIZE,
363 MSM_BO_WC | MSM_BO_MAP_PRIV | MSM_BO_GPU_READONLY,
364 gpu->aspace, &bo, &iova);
369 memset(ptr, 0, PREEMPT_SMMU_INFO_SIZE);
371 msm_gem_object_set_name(bo, "preempt_smmu_info ring%d", ring->id);
373 a6xx_gpu->preempt_smmu_bo[ring->id] = bo;
374 a6xx_gpu->preempt_smmu_iova[ring->id] = iova;
375 a6xx_gpu->preempt_smmu[ring->id] = ptr;
377 struct a7xx_cp_smmu_info *smmu_info_ptr = ptr;
379 msm_iommu_pagetable_params(gpu->aspace->mmu, &ttbr, &asid);
381 smmu_info_ptr->magic = GEN7_CP_SMMU_INFO_MAGIC;
382 smmu_info_ptr->ttbr0 = ttbr;
383 smmu_info_ptr->asid = 0xdecafbad;
384 smmu_info_ptr->context_idr = 0;
386 /* Set up the defaults on the preemption record */
387 record_ptr->magic = A6XX_PREEMPT_RECORD_MAGIC;
388 record_ptr->info = 0;
389 record_ptr->data = 0;
390 record_ptr->rptr = 0;
391 record_ptr->wptr = 0;
392 record_ptr->cntl = MSM_GPU_RB_CNTL_DEFAULT;
393 record_ptr->rbase = ring->iova;
394 record_ptr->counter = 0;
395 record_ptr->bv_rptr_addr = rbmemptr(ring, bv_rptr);
400 void a6xx_preempt_fini(struct msm_gpu *gpu)
402 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
403 struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
406 for (i = 0; i < gpu->nr_rings; i++)
407 msm_gem_kernel_put(a6xx_gpu->preempt_bo[i], gpu->aspace);
410 void a6xx_preempt_init(struct msm_gpu *gpu)
412 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
413 struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
416 /* No preemption if we only have one ring */
417 if (gpu->nr_rings <= 1)
420 for (i = 0; i < gpu->nr_rings; i++) {
421 if (preempt_init_ring(a6xx_gpu, gpu->rb[i]))
425 /* TODO: make this configurable? */
426 a6xx_gpu->preempt_level = 1;
427 a6xx_gpu->uses_gmem = 1;
428 a6xx_gpu->skip_save_restore = 1;
430 a6xx_gpu->preempt_postamble_ptr = msm_gem_kernel_new(gpu->dev,
432 MSM_BO_WC | MSM_BO_MAP_PRIV | MSM_BO_GPU_READONLY,
433 gpu->aspace, &a6xx_gpu->preempt_postamble_bo,
434 &a6xx_gpu->preempt_postamble_iova);
436 preempt_prepare_postamble(a6xx_gpu);
438 if (IS_ERR(a6xx_gpu->preempt_postamble_ptr))
441 timer_setup(&a6xx_gpu->preempt_timer, a6xx_preempt_timer, 0);
446 * On any failure our adventure is over. Clean up and
447 * set nr_rings to 1 to force preemption off
449 a6xx_preempt_fini(gpu);
452 DRM_DEV_ERROR(&gpu->pdev->dev,
453 "preemption init failed, disabling preemption\n");