History

accel/amdxdna: Add command doorbell and wait support

Expose the command doorbell register to userspace on a per-hardware context basis, enabling applications to notify the firmware of pending commands via doorbell writes. Introduce DRM_IOCTL_AMDXDNA_WAIT_CMD to allow userspace to wait for completion of individual commands.

David Zhang committed 6 months ago

Commit 91f4da81 parent 193612711 files · +198 −7Browse files

Unified · Split

11 files
  1. drivers/accel/amdxdna/aie4_ctx.c +75 −0
  2. drivers/accel/amdxdna/aie4_host_queue.h +2 −0
  3. drivers/accel/amdxdna/aie4_pci.c +34 −0
  4. drivers/accel/amdxdna/aie4_pci.h +3 −0
  5. drivers/accel/amdxdna/amdxdna_ctx.c +34 −0
  6. drivers/accel/amdxdna/amdxdna_ctx.h +1 −3
  7. drivers/accel/amdxdna/amdxdna_gem.c +3 −2
  8. drivers/accel/amdxdna/amdxdna_pci_drv.c +17 −1
  9. drivers/accel/amdxdna/amdxdna_pci_drv.h +3 −0
  10. drivers/accel/amdxdna/npu3_regs.c +5 −0
  11. include/uapi/drm/amdxdna_accel.h +21 −1
drivers/accel/amdxdna/aie4_ctx.c+75−0
@@ -256,3 +256,78 @@ void aie4_hwctx_fini(struct amdxdna_hwctx *hwctx) · 255 unmodified lines
256256 aie4_hwctx_umq_fini(hwctx);
257257 kfree(hwctx->priv);
258258 }
259+
260+static inline bool valid_queue_index(u64 read, u64 write, u32 capacity)
261+{
262+ return (write >= read) && ((write - read) <= capacity);
263+}
264+
265+static u64 get_read_index(struct amdxdna_hwctx *hwctx)
266+{
267+ u64 wi = READ_ONCE(*hwctx->priv->umq_write_index);
268+ u64 ri = READ_ONCE(*hwctx->priv->umq_read_index);
269+ struct amdxdna_dev *xdna = hwctx->client->xdna;
270+
271+ /*
272+ * CERT cannot update read index as uint64 atomically. Driver may read
273+ * half-updated read index when it has bits in high 32bit. In case read
274+ * index is not valid, wait for some time and retry once. It should
275+ * allow CERT to complete the read index update.
276+ */
277+ if (!valid_queue_index(ri, wi, CTX_MAX_CMDS)) {
278+ XDNA_WARN(xdna, "Invalid index, ri %llu, wi %llu", ri, wi);
279+ usleep_range(100, 200);
280+ ri = READ_ONCE(*hwctx->priv->umq_read_index);
281+ if (!valid_queue_index(ri, wi, CTX_MAX_CMDS)) {
282+ XDNA_ERR(xdna, "Invalid index after retry, ri %llu, wi %llu", ri, wi);
283+ ri = 0;
284+ }
285+ }
286+
287+ return ri;
288+}
289+
290+static inline bool check_cmd_done(struct amdxdna_hwctx *hwctx, u64 seq)
291+{
292+ u64 read_idx = get_read_index(hwctx);
293+
294+ return read_idx > seq;
295+}
296+
297+int aie4_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout)
298+{
299+ unsigned long wait_jifs = MAX_SCHEDULE_TIMEOUT;
300+ struct amdxdna_hwctx_priv *priv = hwctx->priv;
301+ struct cert_comp *cert_comp = priv->cert_comp;
302+ long ret;
303+
304+ if (timeout)
305+ wait_jifs = msecs_to_jiffies(timeout);
306+
307+ ret = wait_event_interruptible_timeout(cert_comp->waitq,
308+ (check_cmd_done(hwctx, seq)),
309+ wait_jifs);
310+
311+ if (!ret)
312+ ret = -ETIME;
313+
314+ return ret <= 0 ? ret : 0;
315+}
316+
317+int aie4_hwctx_valid_doorbell(struct amdxdna_client *client, u32 vm_pgoff)
318+{
319+ struct amdxdna_hwctx *hwctx;
320+ unsigned long hwctx_id;
321+ int idx;
322+
323+ idx = srcu_read_lock(&client->hwctx_srcu);
324+ amdxdna_for_each_hwctx(client, hwctx_id, hwctx) {
325+ if (vm_pgoff == (hwctx->doorbell_offset >> PAGE_SHIFT)) {
326+ srcu_read_unlock(&client->hwctx_srcu, idx);
327+ return 1;
328+ }
329+ }
330+ srcu_read_unlock(&client->hwctx_srcu, idx);
331+
332+ return 0;
333+}
drivers/accel/amdxdna/aie4_host_queue.h+2−0
@@ -8,6 +8,8 @@ · 7 unmodified lines
88
99 #include <linux/types.h>
1010
11+#define CTX_MAX_CMDS 32
12+
1113 struct host_queue_header {
1214 __u64 read_index;
1315 struct {
drivers/accel/amdxdna/aie4_pci.c+34−0
@@ -503,6 +503,38 @@ static int aie4m_pcidev_init(struct amdxdna_dev *xdna) · 502 unmodified lines
503503 return 0;
504504 }
505505
506+static int aie4_doorbell_mmap(struct amdxdna_client *client, struct vm_area_struct *vma)
507+{
508+ struct amdxdna_dev *xdna = client->xdna;
509+ struct pci_dev *pdev = to_pci_dev(xdna->ddev.dev);
510+ const struct amdxdna_dev_priv *npriv = xdna->dev_info->dev_priv;
511+ phys_addr_t res_start;
512+ unsigned long pfn;
513+ int ret;
514+
515+ if (!aie4_hwctx_valid_doorbell(client, vma->vm_pgoff)) {
516+ XDNA_ERR(xdna, "Invalid doorbell page offset 0x%lx", vma->vm_pgoff);
517+ return -EINVAL;
518+ }
519+
520+ if (vma_pages(vma) != 1) {
521+ XDNA_ERR(xdna, "can only map one page, got %ld", vma_pages(vma));
522+ return -EINVAL;
523+ }
524+
525+ res_start = pci_resource_start(pdev, xdna->dev_info->doorbell_bar) + npriv->doorbell_off;
526+ pfn = PHYS_PFN(res_start) + vma->vm_pgoff;
527+ vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
528+ vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
529+ ret = io_remap_pfn_range(vma, vma->vm_start,
530+ pfn,
531+ PAGE_SIZE,
532+ vma->vm_page_prot);
533+
534+ XDNA_DBG(xdna, "doorbell ret %d", ret);
535+ return ret;
536+}
537+
506538 static int aie4_pf_init(struct amdxdna_dev *xdna)
507539 {
508540 int ret;
@@ -547,4 +579,6 @@ const struct amdxdna_dev_ops aie4_vf_ops = { · 38 unmodified lines
547579 .fini = aie4_vf_fini,
548580 .hwctx_init = aie4_hwctx_init,
549581 .hwctx_fini = aie4_hwctx_fini,
582+ .mmap = aie4_doorbell_mmap,
583+ .cmd_wait = aie4_cmd_wait,
550584 };
drivers/accel/amdxdna/aie4_pci.h+3−0
@@ -36,6 +36,7 @@ struct amdxdna_dev_priv { · 35 unmodified lines
3636 u32 mbox_bar;
3737 u32 mbox_rbuf_bar;
3838 u64 mbox_info_off;
39+ u32 doorbell_off;
3940
4041 struct aie_bar_off_pair psp_regs_off[PSP_MAX_REGS];
4142 struct aie_bar_off_pair smu_regs_off[SMU_MAX_REGS];
@@ -60,6 +61,8 @@ int aie4_suspend_fw(struct amdxdna_dev_hdl *ndev); · 18 unmodified lines
6061 /* aie4_ctx.c */
6162 int aie4_hwctx_init(struct amdxdna_hwctx *hwctx);
6263 void aie4_hwctx_fini(struct amdxdna_hwctx *hwctx);
64+int aie4_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout);
65+int aie4_hwctx_valid_doorbell(struct amdxdna_client *client, u32 vm_pgoff);
6366
6467 /* aie4_sriov.c */
6568 #if IS_ENABLED(CONFIG_PCI_IOV)
drivers/accel/amdxdna/amdxdna_ctx.c+34−0
@@ -627,3 +627,37 @@ int amdxdna_drm_submit_cmd_ioctl(struct drm_device *dev, void *data, struct drm_ · 626 unmodified lines
627627 XDNA_ERR(client->xdna, "Invalid command type %d", args->type);
628628 return -EINVAL;
629629 }
630+
631+int amdxdna_drm_wait_cmd_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
632+{
633+ struct amdxdna_client *client = filp->driver_priv;
634+ struct amdxdna_dev *xdna = to_xdna_dev(dev);
635+ struct amdxdna_drm_wait_cmd *args = data;
636+ struct amdxdna_hwctx *hwctx;
637+ int ret, idx;
638+
639+ XDNA_DBG(xdna, "PID %d ctx %d timeout set %d ms for cmd %llu",
640+ client->pid, args->hwctx, args->timeout, args->seq);
641+
642+ if (!xdna->dev_info->ops->cmd_wait)
643+ return -EOPNOTSUPP;
644+
645+ idx = srcu_read_lock(&client->hwctx_srcu);
646+ hwctx = xa_load(&client->hwctx_xa, args->hwctx);
647+ if (!hwctx) {
648+ XDNA_DBG(xdna, "PID %d failed to get ctx %d", client->pid, args->hwctx);
649+ ret = -EINVAL;
650+ goto unlock_ctx_srcu;
651+ }
652+
653+ ret = xdna->dev_info->ops->cmd_wait(hwctx, args->seq, args->timeout);
654+
655+ XDNA_DBG(xdna, "PID %d ctx %d cmd %lld wait finished, ret %d",
656+ client->pid, args->hwctx, args->seq, ret);
657+
658+ trace_amdxdna_debug_point(current->comm, args->seq, "job returned to user");
659+
660+unlock_ctx_srcu:
661+ srcu_read_unlock(&client->hwctx_srcu, idx);
662+ return ret;
663+}
drivers/accel/amdxdna/amdxdna_ctx.h+1−3
@@ -211,12 +211,10 @@ int amdxdna_cmd_submit(struct amdxdna_client *client, · 210 unmodified lines
211211 u32 *arg_bo_hdls, u32 arg_bo_cnt,
212212 u32 hwctx_hdl, u64 *seq);
213213
214−int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
215− u64 seq, u32 timeout);
216−
217214 int amdxdna_drm_create_hwctx_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
218215 int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
219216 int amdxdna_drm_destroy_hwctx_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
220217 int amdxdna_drm_submit_cmd_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
218+int amdxdna_drm_wait_cmd_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
221219
222220 #endif /* _AMDXDNA_CTX_H_ */
drivers/accel/amdxdna/amdxdna_gem.c+3−2
@@ -212,7 +212,8 @@ static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni, · 211 unmodified lines
212212 mmu_interval_set_seq(&mapp->notifier, cur_seq);
213213 up_write(&xdna->notifier_lock);
214214
215− xdna->dev_info->ops->hmm_invalidate(abo, cur_seq);
215+ if (xdna->dev_info->ops->hmm_invalidate)
216+ xdna->dev_info->ops->hmm_invalidate(abo, cur_seq);
216217
217218 if (range->event == MMU_NOTIFY_UNMAP) {
218219 down_write(&xdna->notifier_lock);
@@ -295,7 +296,7 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo, · 76 unmodified lines
295296 u32 nr_pages;
296297 int ret;
297298
298− if (!xdna->dev_info->ops->hmm_invalidate)
299+ if (!amdxdna_pasid_on(abo->client))
299300 return 0;
300301
301302 mapp = kzalloc_obj(*mapp);
drivers/accel/amdxdna/amdxdna_pci_drv.c+17−1
@@ -224,6 +224,21 @@ static int amdxdna_drm_set_state_ioctl(struct drm_device *dev, void *data, struc · 223 unmodified lines
224224 return ret;
225225 }
226226
227+static int amdxdna_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
228+{
229+ struct drm_file *drm_filp = filp->private_data;
230+ struct amdxdna_client *client = drm_filp->driver_priv;
231+ struct amdxdna_dev *xdna = client->xdna;
232+
233+ if (likely(vma->vm_pgoff >= DRM_FILE_PAGE_OFFSET_START))
234+ return drm_gem_mmap(filp, vma);
235+
236+ if (!xdna->dev_info->ops->mmap)
237+ return -EOPNOTSUPP;
238+
239+ return xdna->dev_info->ops->mmap(client, vma);
240+}
241+
227242 static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {
228243 /* Context */
229244 DRM_IOCTL_DEF_DRV(AMDXDNA_CREATE_HWCTX, amdxdna_drm_create_hwctx_ioctl, 0),
@@ -235,6 +250,7 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = { · 5 unmodified lines
235250 DRM_IOCTL_DEF_DRV(AMDXDNA_SYNC_BO, amdxdna_drm_sync_bo_ioctl, 0),
236251 /* Execution */
237252 DRM_IOCTL_DEF_DRV(AMDXDNA_EXEC_CMD, amdxdna_drm_submit_cmd_ioctl, 0),
253+ DRM_IOCTL_DEF_DRV(AMDXDNA_WAIT_CMD, amdxdna_drm_wait_cmd_ioctl, 0),
238254 /* AIE hardware */
239255 DRM_IOCTL_DEF_DRV(AMDXDNA_GET_INFO, amdxdna_drm_get_info_ioctl, 0),
240256 DRM_IOCTL_DEF_DRV(AMDXDNA_GET_ARRAY, amdxdna_drm_get_array_ioctl, 0),
@@ -281,7 +297,7 @@ static const struct file_operations amdxdna_fops = { · 40 unmodified lines
281297 .poll = drm_poll,
282298 .read = drm_read,
283299 .llseek = noop_llseek,
284− .mmap = drm_gem_mmap,
300+ .mmap = amdxdna_drm_gem_mmap,
285301 .show_fdinfo = drm_show_fdinfo,
286302 .fop_flags = FOP_UNSIGNED_OFFSET,
287303 };
drivers/accel/amdxdna/amdxdna_pci_drv.h+3−0
@@ -56,12 +56,14 @@ struct amdxdna_dev_ops { · 55 unmodified lines
5656 int (*resume)(struct amdxdna_dev *xdna);
5757 int (*suspend)(struct amdxdna_dev *xdna);
5858 int (*sriov_configure)(struct amdxdna_dev *xdna, int num_vfs);
59+ int (*mmap)(struct amdxdna_client *client, struct vm_area_struct *vma);
5960 int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
6061 void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
6162 int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
6263 int (*hwctx_sync_debug_bo)(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl);
6364 void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
6465 int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);
66+ int (*cmd_wait)(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout);
6567 int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args);
6668 int (*set_aie_state)(struct amdxdna_client *client, struct amdxdna_drm_set_state *args);
6769 int (*get_array)(struct amdxdna_client *client, struct amdxdna_drm_get_array *args);
@@ -85,6 +87,7 @@ struct amdxdna_dev_info { · 17 unmodified lines
8587 int sram_bar;
8688 int psp_bar;
8789 int smu_bar;
90+ int doorbell_bar;
8891 int device_type;
8992 int first_col;
9093 u32 dev_mem_buf_shift;
drivers/accel/amdxdna/npu3_regs.c+5−0
@@ -14,6 +14,9 @@ · 13 unmodified lines
1414 #define NPU3_MBOX_BUFFER_BAR 2
1515 #define NPU3_MBOX_INFO_OFF 0x0
1616
17+#define NPU3_DOORBELL_BAR 2
18+#define NPU3_DOORBELL_OFF 0x0
19+
1720 /* PCIe BAR Index for NPU3 */
1821 #define NPU3_REG_BAR_INDEX 0
1922 #define NPU3_PSP_BAR_INDEX 4
@@ -45,6 +48,7 @@ static const struct amdxdna_dev_priv npu3_dev_priv = { · 25 unmodified lines
4548 .mbox_bar = NPU3_MBOX_BAR,
4649 .mbox_rbuf_bar = NPU3_MBOX_BUFFER_BAR,
4750 .mbox_info_off = NPU3_MBOX_INFO_OFF,
51+ .doorbell_off = NPU3_DOORBELL_OFF,
4852 .psp_regs_off = {
4953 DEFINE_BAR_OFFSET(PSP_CMD_REG, NPU3_PSP, MPASP_C2PMSG_123_ALT_1),
5054 DEFINE_BAR_OFFSET(PSP_ARG0_REG, NPU3_PSP, MPASP_C2PMSG_156_ALT_1),
@@ -87,6 +91,7 @@ const struct amdxdna_dev_info dev_npu3_pf_info = { · 36 unmodified lines
8791 const struct amdxdna_dev_info dev_npu3_vf_info = {
8892 .mbox_bar = NPU3_MBOX_BAR,
8993 .sram_bar = NPU3_MBOX_BUFFER_BAR,
94+ .doorbell_bar = NPU3_DOORBELL_BAR,
9095 .default_vbnv = "RyzenAI-npu3-vf",
9196 .device_type = AMDXDNA_DEV_TYPE_UMQ,
9297 .dev_priv = &npu3_dev_vf_priv,
include/uapi/drm/amdxdna_accel.h+21−1
@@ -45,7 +45,8 @@ enum amdxdna_drm_ioctl_id { · 44 unmodified lines
4545 DRM_AMDXDNA_EXEC_CMD,
4646 DRM_AMDXDNA_GET_INFO,
4747 DRM_AMDXDNA_SET_STATE,
48− DRM_AMDXDNA_GET_ARRAY = 10,
48+ DRM_AMDXDNA_WAIT_CMD,
49+ DRM_AMDXDNA_GET_ARRAY,
4950 };
5051
5152 /**
@@ -274,6 +275,21 @@ struct amdxdna_drm_exec_cmd { · 222 unmodified lines
274275 __u64 seq;
275276 };
276277
278+/**
279+ * struct amdxdna_drm_wait_cmd - Wait execution command.
280+ *
281+ * @hwctx: Context handle.
282+ * @timeout: timeout in ms, 0 implies infinite wait.
283+ * @seq: sequence number of the command returned by execute command.
284+ *
285+ * Wait a command specified by seq to be completed.
286+ */
287+struct amdxdna_drm_wait_cmd {
288+ __u32 hwctx;
289+ __u32 timeout;
290+ __u64 seq;
291+};
292+
277293 /**
278294 * struct amdxdna_drm_query_aie_status - Query the status of the AIE hardware
279295 * @buffer: The user space buffer that will return the AIE status.
@@ -739,6 +755,10 @@ struct amdxdna_drm_set_power_mode { · 459 unmodified lines
739755 DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDXDNA_GET_ARRAY, \
740756 struct amdxdna_drm_get_array)
741757
758+#define DRM_IOCTL_AMDXDNA_WAIT_CMD \
759+ DRM_IOW(DRM_COMMAND_BASE + DRM_AMDXDNA_WAIT_CMD, \
760+ struct amdxdna_drm_wait_cmd)
761+
742762 #if defined(__cplusplus)
743763 } /* extern c end */
744764 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t