Re: [PATCH 18/24] iommu/amd: Introduce helper function for updating device ID mapping table
From: Suthikulpanit, Suravee
Date: Tue Sep 08 2026 - 06:00:49 EST
On 8/19/2026 5:26 PM, guanghuifeng@xxxxxxxxxxxxxxxxx wrote:
+static void clear_device_mapping(struct amd_iommu *iommu, u16 guestId, u16 gDevId)
+{
+ u64 val;
+ u8 __iomem *vfctrl;
+
+ /*
+ * Clear the DevID in VFCTRL registers
+ */
+ val = FIELD_PREP(DEVID_ENTRY_GDEVID_MASK, gDevId) |
+ FIELD_PREP(DEVID_ENTRY_HDEVID_MASK, 0) |
+ DEVID_ENTRY_WRITE | DEVID_ENTRY_VALID;
+
+ vfctrl = VIOMMU_VFCTRL_MMIO_BASE(iommu, guestId);
+ writeq(val, vfctrl + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET);
+}
The clear path sets Vld=1 with hDevID=0. This semantics is ambiguous:
Vld=1 marks the entry as a *valid* mapping, and hDevID=0 is a legal PCI
BDF (Bus 0, Dev 0, Func 0), instead of treating the entry as invalid. To actually invalidate the mapping,
please use Vld=0
val = FIELD_PREP(DEVID_ENTRY_GDEVID_MASK, gDevId) |
FIELD_PREP(DEVID_ENTRY_HDEVID_MASK, 0) |
DEVID_ENTRY_WRITE;
Alternatively, during the clear dev mapping process, you might directly specify the original hdevid being used.
Based on the IOMMU implementation, it requires the valid bit to be set to 1. Otherwise, guest command INVALIDATE_IOMMU_ALL would trigger error ILLEGAL_COMMAND_ERROR event.
Generally, host device id BDF 0:00.0 is the host bridge, which is not part of the list of devices managed by IOMMU. So, it is safe.
Thanks,
Suravee