Skip to content

Commit 77cc0da

Browse files
committed
drm/amdgpu: track ring state associated with a fence
We need to know the wptr and sequence number associated with a fence so that we can re-emit the unprocessed state after a ring reset. Pre-allocate storage space for the ring buffer contents and add helpers to save off and re-emit the unprocessed state so that it can be re-emitted after the queue is reset. Reviewed-by: Christian König <christian.koenig@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
1 parent bc29c03 commit 77cc0da

File tree

6 files changed

+195
-3
lines changed

6 files changed

+195
-3
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,15 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
120120
am_fence = kzalloc(sizeof(*am_fence), GFP_KERNEL);
121121
if (!am_fence)
122122
return -ENOMEM;
123+
am_fence->context = 0;
123124
} else {
124125
am_fence = af;
125126
}
126127
fence = &am_fence->base;
127128
am_fence->ring = ring;
128129

129130
seq = ++ring->fence_drv.sync_seq;
131+
am_fence->seq = seq;
130132
if (af) {
131133
dma_fence_init(fence, &amdgpu_job_fence_ops,
132134
&ring->fence_drv.lock,
@@ -141,6 +143,7 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
141143

142144
amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
143145
seq, flags | AMDGPU_FENCE_FLAG_INT);
146+
amdgpu_fence_save_wptr(fence);
144147
pm_runtime_get_noresume(adev_to_drm(adev)->dev);
145148
ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
146149
if (unlikely(rcu_dereference_protected(*ptr, 1))) {
@@ -253,6 +256,7 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
253256

254257
do {
255258
struct dma_fence *fence, **ptr;
259+
struct amdgpu_fence *am_fence;
256260

257261
++last_seq;
258262
last_seq &= drv->num_fences_mask;
@@ -265,6 +269,12 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
265269
if (!fence)
266270
continue;
267271

272+
/* Save the wptr in the fence driver so we know what the last processed
273+
* wptr was. This is required for re-emitting the ring state for
274+
* queues that are reset but are not guilty and thus have no guilty fence.
275+
*/
276+
am_fence = container_of(fence, struct amdgpu_fence, base);
277+
drv->signalled_wptr = am_fence->wptr;
268278
dma_fence_signal(fence);
269279
dma_fence_put(fence);
270280
pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
@@ -727,6 +737,86 @@ void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
727737
amdgpu_fence_process(ring);
728738
}
729739

740+
741+
/**
742+
* Kernel queue reset handling
743+
*
744+
* The driver can reset individual queues for most engines, but those queues
745+
* may contain work from multiple contexts. Resetting the queue will reset
746+
* lose all of that state. In order to minimize the collateral damage, the
747+
* driver will save the ring contents which are not associated with the guilty
748+
* context prior to resetting the queue. After resetting the queue the queue
749+
* contents from the other contexts is re-emitted to the rings so that it can
750+
* be processed by the engine. To handle this, we save the queue's write
751+
* pointer (wptr) in the fences associated with each context. If we get a
752+
* queue timeout, we can then use the wptrs from the fences to determine
753+
* which data needs to be saved out of the queue's ring buffer.
754+
*/
755+
756+
/**
757+
* amdgpu_fence_driver_guilty_force_completion - force signal of specified sequence
758+
*
759+
* @fence: fence of the ring to signal
760+
*
761+
*/
762+
void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence)
763+
{
764+
dma_fence_set_error(&fence->base, -ETIME);
765+
amdgpu_fence_write(fence->ring, fence->seq);
766+
amdgpu_fence_process(fence->ring);
767+
}
768+
769+
void amdgpu_fence_save_wptr(struct dma_fence *fence)
770+
{
771+
struct amdgpu_fence *am_fence = container_of(fence, struct amdgpu_fence, base);
772+
773+
am_fence->wptr = am_fence->ring->wptr;
774+
}
775+
776+
static void amdgpu_ring_backup_unprocessed_command(struct amdgpu_ring *ring,
777+
u64 start_wptr, u32 end_wptr)
778+
{
779+
unsigned int first_idx = start_wptr & ring->buf_mask;
780+
unsigned int last_idx = end_wptr & ring->buf_mask;
781+
unsigned int i;
782+
783+
/* Backup the contents of the ring buffer. */
784+
for (i = first_idx; i != last_idx; ++i, i &= ring->buf_mask)
785+
ring->ring_backup[ring->ring_backup_entries_to_copy++] = ring->ring[i];
786+
}
787+
788+
void amdgpu_ring_backup_unprocessed_commands(struct amdgpu_ring *ring,
789+
struct amdgpu_fence *guilty_fence)
790+
{
791+
struct dma_fence *unprocessed;
792+
struct dma_fence __rcu **ptr;
793+
struct amdgpu_fence *fence;
794+
u64 wptr, i, seqno;
795+
796+
seqno = amdgpu_fence_read(ring);
797+
wptr = ring->fence_drv.signalled_wptr;
798+
ring->ring_backup_entries_to_copy = 0;
799+
800+
for (i = seqno + 1; i <= ring->fence_drv.sync_seq; ++i) {
801+
ptr = &ring->fence_drv.fences[i & ring->fence_drv.num_fences_mask];
802+
rcu_read_lock();
803+
unprocessed = rcu_dereference(*ptr);
804+
805+
if (unprocessed && !dma_fence_is_signaled(unprocessed)) {
806+
fence = container_of(unprocessed, struct amdgpu_fence, base);
807+
808+
/* save everything if the ring is not guilty, otherwise
809+
* just save the content from other contexts.
810+
*/
811+
if (!guilty_fence || (fence->context != guilty_fence->context))
812+
amdgpu_ring_backup_unprocessed_command(ring, wptr,
813+
fence->wptr);
814+
wptr = fence->wptr;
815+
}
816+
rcu_read_unlock();
817+
}
818+
}
819+
730820
/*
731821
* Common fence implementation
732822
*/

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
139139
int vmid = AMDGPU_JOB_GET_VMID(job);
140140
bool need_pipe_sync = false;
141141
unsigned int cond_exec;
142-
143142
unsigned int i;
144143
int r = 0;
145144

@@ -156,6 +155,11 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
156155
gds_va = job->gds_va;
157156
init_shadow = job->init_shadow;
158157
af = &job->hw_fence;
158+
/* Save the context of the job for reset handling.
159+
* The driver needs this so it can skip the ring
160+
* contents for guilty contexts.
161+
*/
162+
af->context = job->base.s_fence ? job->base.s_fence->finished.context : 0;
159163
} else {
160164
vm = NULL;
161165
fence_ctx = 0;
@@ -307,8 +311,17 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
307311
ring->hw_prio == AMDGPU_GFX_PIPE_PRIO_HIGH)
308312
ring->funcs->emit_wave_limit(ring, false);
309313

314+
/* Save the wptr associated with this fence.
315+
* This must be last for resets to work properly
316+
* as we need to save the wptr associated with this
317+
* fence so we know what rings contents to backup
318+
* after we reset the queue.
319+
*/
320+
amdgpu_fence_save_wptr(*f);
321+
310322
amdgpu_ring_ib_end(ring);
311323
amdgpu_ring_commit(ring);
324+
312325
return 0;
313326
}
314327

drivers/gpu/drm/amd/amdgpu/amdgpu_job.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ static enum drm_gpu_sched_stat amdgpu_job_timedout(struct drm_sched_job *s_job)
9090
struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched);
9191
struct amdgpu_job *job = to_amdgpu_job(s_job);
9292
struct drm_wedge_task_info *info = NULL;
93-
struct amdgpu_task_info *ti;
9493
struct amdgpu_device *adev = ring->adev;
94+
struct amdgpu_task_info *ti;
9595
int idx, r;
9696

9797
if (!drm_dev_enter(adev_to_drm(adev), &idx)) {
@@ -134,7 +134,7 @@ static enum drm_gpu_sched_stat amdgpu_job_timedout(struct drm_sched_job *s_job)
134134
} else if (amdgpu_gpu_recovery && ring->funcs->reset) {
135135
dev_err(adev->dev, "Starting %s ring reset\n",
136136
s_job->sched->name);
137-
r = amdgpu_ring_reset(ring, job->vmid, NULL);
137+
r = amdgpu_ring_reset(ring, job->vmid, &job->hw_fence);
138138
if (!r) {
139139
atomic_inc(&ring->adev->gpu_reset_counter);
140140
dev_err(adev->dev, "Ring %s reset succeeded\n",

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned int ndw)
9999
return 0;
100100
}
101101

102+
/**
103+
* amdgpu_ring_alloc_reemit - allocate space on the ring buffer for reemit
104+
*
105+
* @ring: amdgpu_ring structure holding ring information
106+
* @ndw: number of dwords to allocate in the ring buffer
107+
*
108+
* Allocate @ndw dwords in the ring buffer (all asics).
109+
* doesn't check the max_dw limit as we may be reemitting
110+
* several submissions.
111+
*/
112+
static void amdgpu_ring_alloc_reemit(struct amdgpu_ring *ring, unsigned int ndw)
113+
{
114+
/* Align requested size with padding so unlock_commit can
115+
* pad safely */
116+
ndw = (ndw + ring->funcs->align_mask) & ~ring->funcs->align_mask;
117+
118+
ring->count_dw = ndw;
119+
ring->wptr_old = ring->wptr;
120+
121+
if (ring->funcs->begin_use)
122+
ring->funcs->begin_use(ring);
123+
}
124+
102125
/** amdgpu_ring_insert_nop - insert NOP packets
103126
*
104127
* @ring: amdgpu_ring structure holding ring information
@@ -333,6 +356,12 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
333356
/* Initialize cached_rptr to 0 */
334357
ring->cached_rptr = 0;
335358

359+
if (!ring->ring_backup) {
360+
ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL);
361+
if (!ring->ring_backup)
362+
return -ENOMEM;
363+
}
364+
336365
/* Allocate ring buffer */
337366
if (ring->ring_obj == NULL) {
338367
r = amdgpu_bo_create_kernel(adev, ring->ring_size + ring->funcs->extra_dw, PAGE_SIZE,
@@ -342,6 +371,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
342371
(void **)&ring->ring);
343372
if (r) {
344373
dev_err(adev->dev, "(%d) ring create failed\n", r);
374+
kvfree(ring->ring_backup);
345375
return r;
346376
}
347377
amdgpu_ring_clear_ring(ring);
@@ -385,6 +415,8 @@ void amdgpu_ring_fini(struct amdgpu_ring *ring)
385415
amdgpu_bo_free_kernel(&ring->ring_obj,
386416
&ring->gpu_addr,
387417
(void **)&ring->ring);
418+
kvfree(ring->ring_backup);
419+
ring->ring_backup = NULL;
388420

389421
dma_fence_put(ring->vmid_wait);
390422
ring->vmid_wait = NULL;
@@ -753,3 +785,38 @@ bool amdgpu_ring_sched_ready(struct amdgpu_ring *ring)
753785

754786
return true;
755787
}
788+
789+
void amdgpu_ring_reset_helper_begin(struct amdgpu_ring *ring,
790+
struct amdgpu_fence *guilty_fence)
791+
{
792+
/* Stop the scheduler to prevent anybody else from touching the ring buffer. */
793+
drm_sched_wqueue_stop(&ring->sched);
794+
/* back up the non-guilty commands */
795+
amdgpu_ring_backup_unprocessed_commands(ring, guilty_fence);
796+
}
797+
798+
int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
799+
struct amdgpu_fence *guilty_fence)
800+
{
801+
unsigned int i;
802+
int r;
803+
804+
/* verify that the ring is functional */
805+
r = amdgpu_ring_test_ring(ring);
806+
if (r)
807+
return r;
808+
809+
/* signal the fence of the bad job */
810+
if (guilty_fence)
811+
amdgpu_fence_driver_guilty_force_completion(guilty_fence);
812+
/* Re-emit the non-guilty commands */
813+
if (ring->ring_backup_entries_to_copy) {
814+
amdgpu_ring_alloc_reemit(ring, ring->ring_backup_entries_to_copy);
815+
for (i = 0; i < ring->ring_backup_entries_to_copy; i++)
816+
amdgpu_ring_write(ring, ring->ring_backup[i]);
817+
amdgpu_ring_commit(ring);
818+
}
819+
/* Start the scheduler again */
820+
drm_sched_wqueue_start(&ring->sched);
821+
return 0;
822+
}

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct amdgpu_fence_driver {
118118
/* sync_seq is protected by ring emission lock */
119119
uint32_t sync_seq;
120120
atomic_t last_seq;
121+
u64 signalled_wptr;
121122
bool initialized;
122123
struct amdgpu_irq_src *irq_src;
123124
unsigned irq_type;
@@ -141,13 +142,21 @@ struct amdgpu_fence {
141142
/* RB, DMA, etc. */
142143
struct amdgpu_ring *ring;
143144
ktime_t start_timestamp;
145+
146+
/* wptr for the fence for resets */
147+
u64 wptr;
148+
/* fence context for resets */
149+
u64 context;
150+
uint32_t seq;
144151
};
145152

146153
extern const struct drm_sched_backend_ops amdgpu_sched_ops;
147154

148155
void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
149156
void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
150157
void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
158+
void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence);
159+
void amdgpu_fence_save_wptr(struct dma_fence *fence);
151160

152161
int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
153162
int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
@@ -284,6 +293,9 @@ struct amdgpu_ring {
284293

285294
struct amdgpu_bo *ring_obj;
286295
uint32_t *ring;
296+
/* backups for resets */
297+
uint32_t *ring_backup;
298+
unsigned int ring_backup_entries_to_copy;
287299
unsigned rptr_offs;
288300
u64 rptr_gpu_addr;
289301
volatile u32 *rptr_cpu_addr;
@@ -550,4 +562,10 @@ int amdgpu_ib_pool_init(struct amdgpu_device *adev);
550562
void amdgpu_ib_pool_fini(struct amdgpu_device *adev);
551563
int amdgpu_ib_ring_tests(struct amdgpu_device *adev);
552564
bool amdgpu_ring_sched_ready(struct amdgpu_ring *ring);
565+
void amdgpu_ring_backup_unprocessed_commands(struct amdgpu_ring *ring,
566+
struct amdgpu_fence *guilty_fence);
567+
void amdgpu_ring_reset_helper_begin(struct amdgpu_ring *ring,
568+
struct amdgpu_fence *guilty_fence);
569+
int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
570+
struct amdgpu_fence *guilty_fence);
553571
#endif

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
765765
bool cleaner_shader_needed = false;
766766
bool pasid_mapping_needed = false;
767767
struct dma_fence *fence = NULL;
768+
struct amdgpu_fence *af;
768769
unsigned int patch;
769770
int r;
770771

@@ -830,6 +831,9 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
830831
r = amdgpu_fence_emit(ring, &fence, NULL, 0);
831832
if (r)
832833
return r;
834+
/* this is part of the job's context */
835+
af = container_of(fence, struct amdgpu_fence, base);
836+
af->context = job->base.s_fence ? job->base.s_fence->finished.context : 0;
833837
}
834838

835839
if (vm_flush_needed) {

0 commit comments

Comments
 (0)