From: Nishad Saraf <[email protected]>

Firmware sends notifications, such as context errors, as messages with
ID 0. Add an optional mailbox handler for ID 0 messages, and use it on
the AIE4 management channel to reset the context on a context error.

Signed-off-by: Nishad Saraf <[email protected]>
Signed-off-by: David Zhang <[email protected]>
---
 drivers/accel/amdxdna/aie4_error.c      | 104 ++++++++++++++++++++++++
 drivers/accel/amdxdna/aie4_msg_priv.h   |  31 +++++++
 drivers/accel/amdxdna/aie4_pci.c        |   2 +
 drivers/accel/amdxdna/aie4_pci.h        |   1 +
 drivers/accel/amdxdna/amdxdna_mailbox.c |  18 ++++
 drivers/accel/amdxdna/amdxdna_mailbox.h |  17 ++++
 6 files changed, 173 insertions(+)

diff --git a/drivers/accel/amdxdna/aie4_error.c 
b/drivers/accel/amdxdna/aie4_error.c
index a56bf5891bf1..9ca99af3e2c1 100644
--- a/drivers/accel/amdxdna/aie4_error.c
+++ b/drivers/accel/amdxdna/aie4_error.c
@@ -407,3 +407,107 @@ void aie4_async_events_stop(struct amdxdna_dev_hdl *ndev)
        if (events)
                WRITE_ONCE(events->gen, events->gen + 1);
 }
+
+/*
+ * struct aie4_async_note - context error notification from firmware
+ * @work: worker that resets the context
+ * @ndev: device handle
+ * @events: pool owning the workqueue
+ * @gen: @events gen when the notification arrived
+ * @ctx_gen: device context generation when the notification arrived
+ * @ctx_id: firmware context ID
+ */
+struct aie4_async_note {
+       struct work_struct              work;
+       struct amdxdna_dev_hdl          *ndev;
+       struct aie4_async_events        *events;
+       u32                             gen;
+       u64                             ctx_gen;
+       u32                             ctx_id;
+};
+
+static void aie4_async_note_worker(struct work_struct *work)
+{
+       struct aie4_async_note *note = container_of(work, struct 
aie4_async_note, work);
+       struct amdxdna_dev_hdl *ndev = note->ndev;
+       struct amdxdna_dev *xdna = ndev->aie.xdna;
+
+       mutex_lock(&xdna->dev_lock);
+       /* Skip a notification left over from a previous channel. */
+       if (note->gen != note->events->gen)
+               goto unlock;
+
+       /* There is no report, so record it like a job timeout. */
+       aie4_record_async_error(ndev, AMDXDNA_ERROR_NUM_KDS_EXEC, 0, 
note->ctx_id);
+       aie4_reset_hwctx_by_id(ndev, note->ctx_id, note->ctx_gen, NULL);
+
+unlock:
+       mutex_unlock(&xdna->dev_lock);
+       kfree(note);
+}
+
+/*
+ * Called from the mailbox receive worker for each message firmware sends on
+ * its own, so defer the context reset to the async event workqueue.
+ */
+int aie4_async_note_cb(void *handle, u32 opcode, void __iomem *data, size_t 
size)
+{
+       struct amdxdna_dev_hdl *ndev = handle;
+       struct aie4_async_events *events = ndev->async_events;
+       struct amdxdna_dev *xdna = ndev->aie.xdna;
+       struct aie4_async_note *note;
+       u32 event_id, type, ctx_id;
+
+       if (opcode != AIE4_MSG_OP_ASYNC_EVENT_NOTE ||
+           size < sizeof(struct aie4_msg_async_note))
+               return -EINVAL;
+
+       event_id = readl(data + offsetof(struct aie4_msg_async_note, event_id));
+       switch (event_id) {
+       case AIE4_ASYNC_NOTE_CONTEXT_ERROR:
+               ctx_id = readl(data + offsetof(struct aie4_msg_async_note,
+                                              context_error.ctx_id));
+               XDNA_ERR(xdna, "Firmware reported context %u error", ctx_id);
+               break;
+       case AIE4_ASYNC_NOTE_REPORT_DROPPED:
+               type = readl(data + offsetof(struct aie4_msg_async_note,
+                                            report_dropped.async_event_type));
+               ctx_id = readl(data + offsetof(struct aie4_msg_async_note,
+                                              report_dropped.ctx_id));
+               /* An AIE error also sends CONTEXT_ERROR; reset only for this 
one. */
+               if (type != AIE4_ASYNC_EVENT_TYPE_CTX_ERROR) {
+                       XDNA_ERR(xdna, "Firmware had no async event buffer, 
type %u context %u",
+                                type, ctx_id);
+                       return 0;
+               }
+               XDNA_ERR(xdna,
+                        "TDR or uC error on context %u without a report",
+                        ctx_id);
+               break;
+       case AIE4_ASYNC_NOTE_POWER_ERROR:
+               XDNA_ERR(xdna, "Firmware reported a power sequencing error");
+               return 0;
+       default:
+               XDNA_DBG(xdna, "Firmware notification %u ignored", event_id);
+               return 0;
+       }
+
+       /* The PF runs no contexts. */
+       if (!events)
+               return 0;
+
+       /* Called from the mailbox receive worker, which must not block. */
+       note = kzalloc_obj(*note, GFP_NOWAIT);
+       if (!note)
+               return -ENOMEM;
+
+       note->ndev = ndev;
+       note->events = events;
+       note->gen = READ_ONCE(events->gen);
+       /* Contexts created after this notification are not the one it reports. 
*/
+       note->ctx_gen = READ_ONCE(ndev->ctx_gen);
+       note->ctx_id = ctx_id;
+       INIT_WORK(&note->work, aie4_async_note_worker);
+       queue_work(events->wq, &note->work);
+       return 0;
+}
diff --git a/drivers/accel/amdxdna/aie4_msg_priv.h 
b/drivers/accel/amdxdna/aie4_msg_priv.h
index f8c94cf316fd..7a0f89150bff 100644
--- a/drivers/accel/amdxdna/aie4_msg_priv.h
+++ b/drivers/accel/amdxdna/aie4_msg_priv.h
@@ -16,6 +16,8 @@ enum aie4_msg_opcode {
        AIE4_MSG_OP_ASYNC_EVENT_REPORT               = 0x10004,
        AIE4_MSG_OP_SET_RUNTIME_CONFIG               = 0x10007,
        AIE4_MSG_OP_QUERY_CERT_FIRMWARE_VERSION      = 0x1000F,
+       /* Sent by firmware with message ID 0; no response is expected. */
+       AIE4_MSG_OP_ASYNC_EVENT_NOTE                 = 0x10012,
 
        AIE4_MSG_OP_CREATE_VFS                       = 0x20001,
        AIE4_MSG_OP_DESTROY_VFS                      = 0x20002,
@@ -402,4 +404,33 @@ struct aie4_async_ctx_error {
        struct aie4_msg_app_health_report app_health_report;
 };
 
+/* Events reported with AIE4_MSG_OP_ASYNC_EVENT_NOTE. */
+enum aie4_msg_async_note_id {
+       AIE4_ASYNC_NOTE_FW_LOG_WATERMARK,
+       AIE4_ASYNC_NOTE_FW_TRACE_WATERMARK,
+       AIE4_ASYNC_NOTE_DEBUG_MODE_ACTIVATED,
+       AIE4_ASYNC_NOTE_CONTEXT_ERROR,
+       AIE4_ASYNC_NOTE_REPORT_DROPPED,
+       AIE4_ASYNC_NOTE_POWER_ERROR,
+};
+
+/*
+ * AIE4_MSG_OP_ASYNC_EVENT_NOTE: an event small enough to fit in the message.
+ * @event_id selects the union member.
+ */
+struct aie4_msg_async_note {
+       __u32 event_id;
+       union {
+               __u32 reserved[4];
+               struct {
+                       __u32 ctx_id;
+               } context_error;
+               /* A report that firmware had no async event buffer for. */
+               struct {
+                       __u32 async_event_type;
+                       __u32 ctx_id;
+               } report_dropped;
+       };
+} __packed;
+
 #endif /* _AIE4_MSG_PRIV_H_ */
diff --git a/drivers/accel/amdxdna/aie4_pci.c b/drivers/accel/amdxdna/aie4_pci.c
index a45c6bb21aaf..78a739573ad3 100644
--- a/drivers/accel/amdxdna/aie4_pci.c
+++ b/drivers/accel/amdxdna/aie4_pci.c
@@ -250,6 +250,7 @@ static int aie4_mailbox_start(struct amdxdna_dev *xdna,
                XDNA_ERR(xdna, "failed to alloc mailbox channel");
                return -ENODEV;
        }
+       xdna_mailbox_set_async_cb(ndev->aie.mgmt_chann, ndev, 
aie4_async_note_cb);
 
        mgmt_mb_irq = pci_irq_vector(pdev, ndev->aie.mgmt_chan_idx);
        if (mgmt_mb_irq < 0) {
@@ -491,6 +492,7 @@ static int aie4_vf_hw_start(struct amdxdna_dev_hdl *ndev)
        /*
         * A VF registers no async event buffers, so firmware keeps none of
         * its memory across a VF reset or driver reload.
+        * Firmware reports VF context errors with notifications instead.
         */
 
        ret = aie4_restore_power_mode(ndev);
diff --git a/drivers/accel/amdxdna/aie4_pci.h b/drivers/accel/amdxdna/aie4_pci.h
index b24865c9ebc8..6da2810c3fb4 100644
--- a/drivers/accel/amdxdna/aie4_pci.h
+++ b/drivers/accel/amdxdna/aie4_pci.h
@@ -161,6 +161,7 @@ int aie4_async_events_alloc(struct amdxdna_dev_hdl *ndev, 
u32 num_events);
 void aie4_async_events_free(struct amdxdna_dev_hdl *ndev);
 int aie4_async_events_register(struct amdxdna_dev_hdl *ndev, u32 num_events);
 void aie4_async_events_stop(struct amdxdna_dev_hdl *ndev);
+int aie4_async_note_cb(void *handle, u32 opcode, void __iomem *data, size_t 
size);
 
 /* aie4_message.c */
 int aie4_query_aie_metadata(struct amdxdna_dev_hdl *ndev,
diff --git a/drivers/accel/amdxdna/amdxdna_mailbox.c 
b/drivers/accel/amdxdna/amdxdna_mailbox.c
index 967f6f1eb764..2d6cce06ecf6 100644
--- a/drivers/accel/amdxdna/amdxdna_mailbox.c
+++ b/drivers/accel/amdxdna/amdxdna_mailbox.c
@@ -66,6 +66,8 @@ struct mailbox_channel {
        struct work_struct              rx_work;
        u32                             i2x_head;
        bool                            bad_state;
+       void                            *async_handle;
+       xdna_mailbox_async_cb_t         async_cb;
 
        /* Pre-allocated message buffer pool */
        void                            *msg_buf;
@@ -302,6 +304,15 @@ mailbox_get_resp(struct mailbox_channel *mb_chann, struct 
xdna_msg_header *heade
        u32 slot;
 
        msg_id = header->id;
+       if (!msg_id && mb_chann->async_cb) {
+               ret = mb_chann->async_cb(mb_chann->async_handle, header->opcode,
+                                        data, header->total_size);
+               if (ret)
+                       MB_ERR(mb_chann, "Async handler for opcode 0x%x ret %d",
+                              header->opcode, ret);
+               return 0;
+       }
+
        if (!mailbox_validate_msgid(msg_id)) {
                MB_ERR(mb_chann, "Bad message ID 0x%x", msg_id);
                return -EINVAL;
@@ -549,6 +560,13 @@ struct mailbox_channel *xdna_mailbox_alloc_channel(struct 
mailbox *mb)
        return NULL;
 }
 
+void xdna_mailbox_set_async_cb(struct mailbox_channel *mb_chann, void 
*async_handle,
+                              xdna_mailbox_async_cb_t async_cb)
+{
+       mb_chann->async_handle = async_handle;
+       mb_chann->async_cb = async_cb;
+}
+
 void xdna_mailbox_free_channel(struct mailbox_channel *mb_chann)
 {
        if (!mb_chann)
diff --git a/drivers/accel/amdxdna/amdxdna_mailbox.h 
b/drivers/accel/amdxdna/amdxdna_mailbox.h
index 7f4a40b4230b..cf06bc8c6c09 100644
--- a/drivers/accel/amdxdna/amdxdna_mailbox.h
+++ b/drivers/accel/amdxdna/amdxdna_mailbox.h
@@ -9,6 +9,10 @@
 struct mailbox;
 struct mailbox_channel;
 
+/* Handler for a message firmware sends on its own, with message ID 0. */
+typedef int (*xdna_mailbox_async_cb_t)(void *handle, u32 opcode,
+                                      void __iomem *data, size_t size);
+
 /*
  * xdna_mailbox_msg - message struct
  *
@@ -102,6 +106,19 @@ xdna_mailbox_start_channel(struct mailbox_channel 
*mb_chann,
                           u32 xdna_mailbox_intr_reg,
                           int mb_irq, u32 n_msg);
 
+/*
+ * xdna_mailbox_set_async_cb() -- set the handler for firmware notifications
+ *
+ * @mb_chann: the handle return from xdna_mailbox_alloc_channel()
+ * @async_handle: handle passed to @async_cb
+ * @async_cb: called from the receive worker for each message with ID 0;
+ *            must not block. Its return value is only logged.
+ *
+ * Call before xdna_mailbox_start_channel().
+ */
+void xdna_mailbox_set_async_cb(struct mailbox_channel *mb_chann, void 
*async_handle,
+                              xdna_mailbox_async_cb_t async_cb);
+
 /*
  * xdna_mailbox_free_channel() -- free mailbox channel
  *
-- 
2.34.1

Reply via email to