diff --git a/sys/x86/iommu/amd_drv.c b/sys/x86/iommu/amd_drv.c --- a/sys/x86/iommu/amd_drv.c +++ b/sys/x86/iommu/amd_drv.c @@ -224,10 +224,24 @@ u_int devtbl_sz, dom, i, reclaimno, segnum_log, segnum, seg_sz; int error; + static const int devtab_base_regs[] = { + AMDIOMMU_DEVTAB_BASE, + AMDIOMMU_DEVTAB_S1_BASE, + AMDIOMMU_DEVTAB_S2_BASE, + AMDIOMMU_DEVTAB_S3_BASE, + AMDIOMMU_DEVTAB_S4_BASE, + AMDIOMMU_DEVTAB_S5_BASE, + AMDIOMMU_DEVTAB_S6_BASE, + AMDIOMMU_DEVTAB_S7_BASE + }; + segnum_log = (sc->efr & AMDIOMMU_EFR_DEVTBLSEG_MASK) >> AMDIOMMU_EFR_DEVTBLSEG_SHIFT; segnum = 1 << segnum_log; + KASSERT(segnum <= nitems(devtab_base_regs), + ("%s: unsupported devtab segment count %u", __func__, segnum)); + devtbl_sz = amdiommu_devtbl_sz(sc); seg_sz = devtbl_sz / segnum; sc->devtbl_obj = vm_pager_allocate(OBJT_PHYS, NULL, atop(devtbl_sz), @@ -248,7 +262,6 @@ for (i = 0; i < segnum; i++) { vm_page_t m; uint64_t rval; - u_int reg; for (reclaimno = 0; reclaimno < 3; reclaimno++) { VM_OBJECT_WLOCK(sc->devtbl_obj); @@ -276,9 +289,7 @@ pmap_zero_page(m); pmap_qenter(seg_vaddr, &m, 1); } - reg = i == 0 ? AMDIOMMU_DEVTAB_BASE : AMDIOMMU_DEVTAB_S1_BASE + - ((i - 1) << 3); - amdiommu_write8(sc, reg, rval); + amdiommu_write8(sc, devtab_base_regs[i], rval); } return (0);