A device behind an accelerated IOMMU must have a HWPT matching its guest device context, bypass, abort, or translate. The bypass and abort proxy HWPTs are pre-allocated; a translate HWPT needs a vDEVICE object first, which provides the virtual-to-physical devid mapping required for invalidations and event reporting.
When the guest issues IODIR.INVAL_DDT, invalidate the context cache and re-install the HWPT matching the current DC. The DC is resolved straight from guest memory via riscv_iommu_ctx_fetch(), so a freshly written context always takes effect; a missing or invalid DC aborts the device's DMA. The translate HWPT is released when the device is detached. This follows the approach of the ARM SMMUv3. Signed-off-by: Gong Shuai <[email protected]> Signed-off-by: Wu Fei <[email protected]> --- hw/riscv/riscv-iommu-accel.c | 189 +++++++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu-accel.h | 18 ++++ hw/riscv/riscv-iommu.c | 34 ++++++- hw/riscv/riscv-iommu.h | 3 + hw/riscv/trace-events | 2 + 5 files changed, 241 insertions(+), 5 deletions(-) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index 7b8dee5090..f6cb2f77df 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -276,6 +276,7 @@ static void riscv_iommu_accel_unset_iommu_device(PCIBus *bus, void *opaque, HostIOMMUDeviceIOMMUFD *idev; RISCVIOMMUAccelDevice *accel_dev; RISCVIOMMUAccelState *accel; + IOMMUFDVdev *vdev; RISCVIOMMUSpace *as; as = riscv_iommu_get_space(s, bus, devfn); @@ -293,6 +294,20 @@ static void riscv_iommu_accel_unset_iommu_device(PCIBus *bus, void *opaque, "0x%x", idev->devid); } + if (accel_dev->s1_hwpt) { + iommufd_backend_free_id(accel_dev->idev->iommufd, + accel_dev->s1_hwpt->hwpt_id); + g_free(accel_dev->s1_hwpt); + accel_dev->s1_hwpt = NULL; + } + + vdev = accel_dev->vdev; + if (vdev) { + iommufd_backend_free_id(accel->viommu->iommufd, vdev->vdevice_id); + g_free(vdev); + accel_dev->vdev = NULL; + } + accel_dev->idev = NULL; accel_dev->accel = NULL; QLIST_REMOVE(accel_dev, next); @@ -307,6 +322,180 @@ static void riscv_iommu_accel_unset_iommu_device(PCIBus *bus, void *opaque, } } +static bool riscv_iommu_accel_alloc_vdev(RISCVIOMMUAccelDevice *accel_dev, + int devid, Error **errp) +{ + RISCVIOMMUAccelState *accel = accel_dev->accel; + HostIOMMUDeviceIOMMUFD *idev = accel_dev->idev; + IOMMUFDVdev *vdev = accel_dev->vdev; + uint32_t vdevice_id; + + if (!idev || vdev) { + return true; + } + + if (!iommufd_backend_alloc_vdev(idev->iommufd, idev->devid, + accel->viommu->viommu_id, devid, + &vdevice_id, errp)) { + return false; + } + + vdev = g_new(IOMMUFDVdev, 1); + vdev->vdevice_id = vdevice_id; + vdev->virt_id = devid; + accel_dev->vdev = vdev; + return true; +} + +static RISCVIOMMUS1Hwpt * +riscv_iommu_accel_dev_alloc_translate(RISCVIOMMUAccelDevice *accel_dev, + RISCVIOMMUContext *ctx, Error **errp) +{ + HostIOMMUDeviceIOMMUFD *idev = accel_dev->idev; + RISCVIOMMUAccelState *accel = accel_dev->accel; + struct iommu_hwpt_riscv nested_data = { + .dc[0] = ctx->tc, + .dc[1] = ctx->gatp, + .dc[2] = ctx->ta, + .dc[3] = ctx->satp, + }; + uint32_t hwpt_id = 0, flags = 0; + RISCVIOMMUS1Hwpt *s1_hwpt; + + if (!iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid, + accel->viommu->viommu_id, flags, + IOMMU_HWPT_DATA_RISCV_S1, + sizeof(nested_data), &nested_data, + &hwpt_id, errp)) { + return NULL; + } + + s1_hwpt = g_new0(RISCVIOMMUS1Hwpt, 1); + s1_hwpt->hwpt_id = hwpt_id; + trace_riscv_iommu_accel_translate_dc(accel_dev->vdev->virt_id, hwpt_id, + ctx->tc, ctx->satp); + return s1_hwpt; +} + +bool riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int devid) +{ + RISCVIOMMUAccelState *accel = s->accel_state; + RISCVIOMMUAccelDevice *accel_dev; + HostIOMMUDeviceIOMMUFD *idev; + uint32_t hwpt_id = 0; + RISCVIOMMUS1Hwpt *s1_hwpt = NULL; + const char *type; + RISCVIOMMUContext ctx; + RISCVIOMMUSpace *as; + Error *local_err = NULL; + Error **errp = &local_err; + + /* + * Nothing to install when accelerated mode is inactive, or when no + * vIOMMU has been allocated yet. + */ + if (!accel || !accel->viommu) { + return true; + } + + as = riscv_iommu_get_space_from_devid(s, devid); + if (!as) { + return true; + } + + accel_dev = container_of(as, RISCVIOMMUAccelDevice, as); + if (!accel_dev->accel) { + return true; + } + + idev = accel_dev->idev; + if (!riscv_iommu_accel_alloc_vdev(accel_dev, devid, errp)) { + goto err; + } + + /* + * Resolve the DC straight from guest memory so INVAL_DDT sees the + * freshly written context. Abort the device's DMA if the DC is + * missing or invalid. + * + * The vdev stays attached even if HWPT alloc/attach fails, since the + * Guest-Host DEVID mapping remains valid. + */ + if (riscv_iommu_ctx_fetch(s, devid, 0, &ctx)) { + /* DC missing or invalid, abort all device DMA */ + hwpt_id = accel->abort_hwpt_id; + } else if (get_field(ctx.satp, RISCV_IOMMU_DC_FSC_MODE) == + RISCV_IOMMU_DC_FSC_MODE_BARE) { + hwpt_id = accel->bypass_hwpt_id; + } else { + s1_hwpt = riscv_iommu_accel_dev_alloc_translate(accel_dev, &ctx, errp); + if (!s1_hwpt) { + goto err; + } + hwpt_id = s1_hwpt->hwpt_id; + } + + if (!hwpt_id) { + error_setg(errp, "Invalid DC for device 0x%x", devid); + goto err; + } + + if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_id, + errp)) { + if (s1_hwpt) { + iommufd_backend_free_id(idev->iommufd, s1_hwpt->hwpt_id); + g_free(s1_hwpt); + } + error_append_hint(errp, "fail to attach hwpt for device 0x%x", devid); + goto err; + } + + /* Free the previous s1_hwpt */ + if (accel_dev->s1_hwpt) { + iommufd_backend_free_id(idev->iommufd, accel_dev->s1_hwpt->hwpt_id); + g_free(accel_dev->s1_hwpt); + } + + accel_dev->s1_hwpt = s1_hwpt; + if (hwpt_id == accel->abort_hwpt_id) { + type = "abort"; + } else if (hwpt_id == accel->bypass_hwpt_id) { + type = "bypass"; + } else { + type = "translate"; + } + + trace_riscv_iommu_accel_install_dc(devid, type, hwpt_id); + return true; + +err: + if (local_err) { + error_report_err(local_err); + } + return false; +} + +bool riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s) +{ + RISCVIOMMUAccelState *accel = s->accel_state; + RISCVIOMMUAccelDevice *accel_dev; + bool all_ok = true; + + if (!accel || !accel->viommu) { + return true; + } + + QLIST_FOREACH(accel_dev, &accel->device_list, next) { + uint32_t devid = riscv_iommu_space_devid(&accel_dev->as); + if (!riscv_iommu_accel_install_dc(s, devid)) { + error_report("Device 0x%x: Failed to install DC", devid); + all_ok = false; + } + } + + return all_ok; +} + static const PCIIOMMUOps riscv_iommu_accel_ops = { .supports_address_space = riscv_iommu_accel_supports_as, .get_address_space = riscv_iommu_accel_find_as, diff --git a/hw/riscv/riscv-iommu-accel.h b/hw/riscv/riscv-iommu-accel.h index 49d022817c..85a1ab6d42 100644 --- a/hw/riscv/riscv-iommu-accel.h +++ b/hw/riscv/riscv-iommu-accel.h @@ -25,19 +25,37 @@ typedef struct RISCVIOMMUAccelState { QLIST_HEAD(, RISCVIOMMUAccelDevice) device_list; } RISCVIOMMUAccelState; +typedef struct RISCVIOMMUS1Hwpt { + uint32_t hwpt_id; +} RISCVIOMMUS1Hwpt; + typedef struct RISCVIOMMUAccelDevice { RISCVIOMMUSpace as; HostIOMMUDeviceIOMMUFD *idev; + RISCVIOMMUS1Hwpt *s1_hwpt; + IOMMUFDVdev *vdev; QLIST_ENTRY(RISCVIOMMUAccelDevice) next; RISCVIOMMUAccelState *accel; } RISCVIOMMUAccelDevice; #ifdef CONFIG_RISCV_IOMMU_ACCEL void riscv_iommu_accel_init(RISCVIOMMUState *s); +bool riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int devid); +bool riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s); #else static inline void riscv_iommu_accel_init(RISCVIOMMUState *s) { } +static inline bool +riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int devid) +{ + return true; +} +static inline bool +riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s) +{ + return true; +} #endif #endif /* HW_RISCV_RISCV_IOMMU_ACCEL_H */ diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index fbc7f72cb6..fa751d566f 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -65,7 +65,7 @@ struct RISCVIOMMUEntry { /* IOMMU index for transactions without process_id specified. */ #define RISCV_IOMMU_NOPROCID 0 -static uint32_t riscv_iommu_space_devid(RISCVIOMMUSpace *as) +uint32_t riscv_iommu_space_devid(RISCVIOMMUSpace *as) { uint32_t devid = PCI_BUILD_BDF(pci_bus_num(as->bus), as->devfn); @@ -1555,6 +1555,19 @@ RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUState *s, PCIBus *bus, int devf return as; } +RISCVIOMMUSpace *riscv_iommu_get_space_from_devid(RISCVIOMMUState *s, + uint32_t devid) +{ + RISCVIOMMUSpace *as; + QLIST_FOREACH(as, &s->spaces, list) { + if (riscv_iommu_space_devid(as) == devid) { + break; + } + } + + return as; +} + /* Find or allocate address space for a given device */ AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn) { @@ -2083,19 +2096,30 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s) case RISCV_IOMMU_CMD(RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT, RISCV_IOMMU_CMD_IODIR_OPCODE): + { + uint32_t devid = get_field(cmd.dword0, RISCV_IOMMU_CMD_IODIR_DID); + if (!(cmd.dword0 & RISCV_IOMMU_CMD_IODIR_DV)) { - /* invalidate all device context cache mappings */ + /* DV=0, invalidate and re-install every accelerated device */ ctx_func = riscv_iommu_ctx_inval_all; + riscv_iommu_ctx_inval(s, ctx_func, devid, 0); + if (!riscv_iommu_accel_install_dc_all(s)) { + goto cmd_ill; + } } else { - /* invalidate all device context matching DID */ + /* DV=1, invalidate and re-install device matching DID */ ctx_func = riscv_iommu_ctx_inval_devid; + riscv_iommu_ctx_inval(s, ctx_func, devid, 0); + if (!riscv_iommu_accel_install_dc(s, devid)) { + goto cmd_ill; + } } - riscv_iommu_ctx_inval(s, ctx_func, - get_field(cmd.dword0, RISCV_IOMMU_CMD_IODIR_DID), 0); break; + } case RISCV_IOMMU_CMD(RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_PDT, RISCV_IOMMU_CMD_IODIR_OPCODE): + /* FIXME: add support for PDT inval for vIOMMU */ if (!(cmd.dword0 & RISCV_IOMMU_CMD_IODIR_DV)) { /* illegal command arguments IODIR_PDT & DV == 0 */ goto cmd_ill; diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 3262213093..ddd339aed1 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -131,6 +131,9 @@ void riscv_iommu_init_space(RISCVIOMMUState *s, RISCVIOMMUSpace *as, PCIBus *bus, int devfn); AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn); RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUState *s, PCIBus *bus, int devfn); +RISCVIOMMUSpace *riscv_iommu_get_space_from_devid(RISCVIOMMUState *s, + uint32_t devid); +uint32_t riscv_iommu_space_devid(RISCVIOMMUSpace *as); typedef struct RISCVIOMMUContext RISCVIOMMUContext; /* Device translation context state. */ diff --git a/hw/riscv/trace-events b/hw/riscv/trace-events index 9834f108b9..55f7d1f0f3 100644 --- a/hw/riscv/trace-events +++ b/hw/riscv/trace-events @@ -28,3 +28,5 @@ riscv_iommu_hpm_evt_write(uint32_t ctr_idx, uint32_t ovf, uint64_t val) "ctr_idx #riscv-iommu-accel.c riscv_iommu_accel_set_iommu_device(int devfn, uint32_t devid) "devfn=0x%x (idev devid=0x%x)" riscv_iommu_accel_unset_iommu_device(int devfn, uint32_t devid) "devfn=0x%x (idev devid=0x%x)" +riscv_iommu_accel_translate_dc(uint32_t vsid, uint32_t hwpt_id, uint64_t tc, uint64_t iosatp) "vSID=0x%x hwpt_id=0x%x dc=%"PRIx64":%"PRIx64 +riscv_iommu_accel_install_dc(uint32_t vsid, const char * type, uint32_t hwpt_id) "vSID=0x%x dc type=%s hwpt_id=0x%x" -- 2.53.0
