An engine reset, eg. of the gfx engine, only resets the queues of one
engine of the GFX IP; the queues of the other engines aren't reset.
amdgpu_userq_pre_reset() and amdgpu_userq_post_reset() handle all the
user queues for a full reset. Add separate helpers for the queues of one
engine, which keep them in the state they had:

- amdgpu_userq_save_hqds(), called after the MES suspend, saves the HQD
  of the mapped queues still on a HQD: the reset clears their HQD, and
  MES couldn't preempt them into their MQD. The queues MES preempted
  have their state in their MQD already.
- amdgpu_userq_restore_hqds(), called after the reset but before MES
  resumes, unmaps those queues, writes the saved HQD into their MQD and
  maps them again: MES doesn't map a queue whose HQD the reset cleared
  again on its own. The queues keep their state and their work, except
  the one the pipe executed: its work in flight depends on pipe state
  the reset cleared, it is dropped and its fences signaled. The queue
  that hung, if any, is unmapped and hung.
- amdgpu_userq_clear_hqds() drops the saved HQDs for the full reset that
  follows a failed engine reset.

The helpers walk the queues and unmap and map them as
amdgpu_userq_pre_reset() and amdgpu_userq_post_reset() do. The saved
HQDs are kept in the gfx IP, in saved_gfx_hqds[], and matched to a queue
by its doorbell and MQD address.

v2: Drop the user queue reset lock and the walk over the queues of one
    type (Christian).
v4: Rename the helpers and the saved HQD array (Timur).

Reviewed-by: Timur Kristóf <[email protected]>
Signed-off-by: Jesse Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h   |   3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 107 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |   4 +
 3 files changed, 114 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
index ec77f24db0e2..0d42ada0bc39 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
@@ -513,6 +513,9 @@ struct amdgpu_gfx {
 
        /* reset mask */
        uint32_t                        gfx_supported_reset;
+       /* HQDs of the gfx user queues, saved by an engine reset */
+       struct amdgpu_gfx_saved_hqd     saved_gfx_hqds[AMDGPU_MAX_GFX_QUEUES];
+       u32                             num_saved_gfx_hqds;
        uint32_t                        compute_supported_reset;
 
        /* gfx off */
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
index ed6f3b67e872..495198ba1b0b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
@@ -1594,3 +1594,110 @@ int amdgpu_userq_post_reset(struct amdgpu_device *adev, 
bool vram_lost)
 
        return ret;
 }
+
+/**
+ * amdgpu_userq_save_hqds - save the HQDs of the user queues before an engine 
reset
+ * @adev: amdgpu device
+ * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine
+ */
+void amdgpu_userq_save_hqds(struct amdgpu_device *adev, u32 queue_type)
+{
+       struct amdgpu_gfx_saved_hqd hqd;
+       struct amdgpu_usermode_queue *queue;
+       unsigned long queue_id;
+
+       adev->gfx.num_saved_gfx_hqds = 0;
+       if (!adev->gfx.funcs->save_hqd)
+               return;
+
+       xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+               if (adev->gfx.num_saved_gfx_hqds >= AMDGPU_MAX_GFX_QUEUES)
+                       break;
+               if (queue->queue_type != queue_type ||
+                   queue->state != AMDGPU_USERQ_STATE_MAPPED)
+                       continue;
+               if (adev->gfx.funcs->save_hqd(adev, queue->doorbell_index, 
&hqd) &&
+                   hqd.on_hqd)
+                       
adev->gfx.saved_gfx_hqds[adev->gfx.num_saved_gfx_hqds++] = hqd;
+       }
+}
+
+static struct amdgpu_gfx_saved_hqd *
+amdgpu_userq_find_saved_hqd(struct amdgpu_device *adev,
+                           struct amdgpu_usermode_queue *queue)
+{
+       struct amdgpu_gfx_saved_hqd *hqd;
+       u32 i;
+
+       for (i = 0; i < adev->gfx.num_saved_gfx_hqds; i++) {
+               hqd = &adev->gfx.saved_gfx_hqds[i];
+               /* Check the MQD too: a new queue may reuse the doorbell. */
+               if (hqd->doorbell_index == queue->doorbell_index &&
+                   hqd->mqd_addr == queue->mqd.gpu_addr)
+                       return hqd;
+       }
+       return NULL;
+}
+
+/**
+ * amdgpu_userq_restore_hqds - restore the user queues after an engine reset
+ * @adev: amdgpu device
+ * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine
+ * @guilty: the queue that hung, or NULL
+ *
+ * MES doesn't map a queue whose HQD the reset cleared again on its own: unmap
+ * it from MES, restore its MQD, and map it again.
+ */
+void amdgpu_userq_restore_hqds(struct amdgpu_device *adev, u32 queue_type,
+                              struct amdgpu_usermode_queue *guilty)
+{
+       const struct amdgpu_userq_funcs *userq_funcs = 
adev->userq_funcs[queue_type];
+       struct amdgpu_usermode_queue *queue;
+       struct amdgpu_gfx_saved_hqd *hqd;
+       unsigned long queue_id;
+       int r;
+
+       xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+               if (queue->queue_type != queue_type ||
+                   queue->state != AMDGPU_USERQ_STATE_MAPPED)
+                       continue;
+
+               if (queue == guilty) {
+                       userq_funcs->unmap(queue);
+                       amdgpu_userq_fence_driver_force_completion(queue);
+                       trace_amdgpu_userq_state_changed(queue, 
AMDGPU_USERQ_STATE_HUNG);
+                       queue->state = AMDGPU_USERQ_STATE_HUNG;
+                       continue;
+               }
+
+               hqd = amdgpu_userq_find_saved_hqd(adev, queue);
+               if (!hqd)
+                       continue;
+
+               userq_funcs->unmap(queue);
+               if (adev->gfx.funcs->restore_hqd(adev, queue->mqd.cpu_ptr,
+                                                queue->userq_prop->queue_size,
+                                                hqd))
+                       
amdgpu_userq_fence_driver_force_completion_with_seqno(queue,
+                                                                             
hqd->wptr);
+               r = userq_funcs->map(queue);
+               if (r) {
+                       dev_err(adev->dev, "Failed to remap user queue %llu: 
%d\n",
+                               queue->doorbell_index, r);
+                       amdgpu_userq_fence_driver_force_completion(queue);
+                       trace_amdgpu_userq_state_changed(queue, 
AMDGPU_USERQ_STATE_HUNG);
+                       queue->state = AMDGPU_USERQ_STATE_HUNG;
+               }
+       }
+       adev->gfx.num_saved_gfx_hqds = 0;
+}
+
+/**
+ * amdgpu_userq_clear_hqds - drop the saved HQDs after a failed engine reset
+ * @adev: amdgpu device
+ * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine
+ */
+void amdgpu_userq_clear_hqds(struct amdgpu_device *adev, u32 queue_type)
+{
+       adev->gfx.num_saved_gfx_hqds = 0;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index 488dc21d7c81..b5195031877d 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
@@ -181,6 +181,10 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct 
amdgpu_device *adev,
 void amdgpu_userq_reset_work(struct work_struct *work);
 void amdgpu_userq_pre_reset(struct amdgpu_device *adev);
 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost);
+void amdgpu_userq_save_hqds(struct amdgpu_device *adev, u32 queue_type);
+void amdgpu_userq_restore_hqds(struct amdgpu_device *adev, u32 queue_type,
+                              struct amdgpu_usermode_queue *guilty);
+void amdgpu_userq_clear_hqds(struct amdgpu_device *adev, u32 queue_type);
 void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue);
 void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);
 
-- 
2.50.1

Reply via email to