···43434444** System MMU optional properties:45454646+- dma-coherent : Present if page table walks made by the SMMU are4747+ cache coherent with the CPU.4848+4949+ NOTE: this only applies to the SMMU itself, not5050+ masters connected upstream of the SMMU.5151+4652- calxeda,smmu-secure-config-access : Enable proper handling of buggy4753 implementations that always use secure access to4854 SMMU configuration registers. In this case non-secure
+2-1
drivers/iommu/Kconfig
···2323config IOMMU_IO_PGTABLE_LPAE2424 bool "ARMv7/v8 Long Descriptor Format"2525 select IOMMU_IO_PGTABLE2626- depends on ARM || ARM64 || COMPILE_TEST2626+ # SWIOTLB guarantees a dma_to_phys() implementation2727+ depends on ARM || ARM64 || (COMPILE_TEST && SWIOTLB)2728 help2829 Enable support for the ARM long descriptor pagetable format.2930 This allocator supports 4K/2M/1G, 16K/32M and 64K/512M page
+27-39
drivers/iommu/arm-smmu-v3.c
···118118119119#define ARM_SMMU_IRQ_CTRL 0x50120120#define IRQ_CTRL_EVTQ_IRQEN (1 << 2)121121+#define IRQ_CTRL_PRIQ_IRQEN (1 << 1)121122#define IRQ_CTRL_GERROR_IRQEN (1 << 0)122123123124#define ARM_SMMU_IRQ_CTRLACK 0x54···174173#define ARM_SMMU_PRIQ_IRQ_CFG2 0xdc175174176175/* Common MSI config fields */177177-#define MSI_CFG0_SH_SHIFT 60178178-#define MSI_CFG0_SH_NSH (0UL << MSI_CFG0_SH_SHIFT)179179-#define MSI_CFG0_SH_OSH (2UL << MSI_CFG0_SH_SHIFT)180180-#define MSI_CFG0_SH_ISH (3UL << MSI_CFG0_SH_SHIFT)181181-#define MSI_CFG0_MEMATTR_SHIFT 56182182-#define MSI_CFG0_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG0_MEMATTR_SHIFT)183176#define MSI_CFG0_ADDR_SHIFT 2184177#define MSI_CFG0_ADDR_MASK 0x3fffffffffffUL178178+#define MSI_CFG2_SH_SHIFT 4179179+#define MSI_CFG2_SH_NSH (0UL << MSI_CFG2_SH_SHIFT)180180+#define MSI_CFG2_SH_OSH (2UL << MSI_CFG2_SH_SHIFT)181181+#define MSI_CFG2_SH_ISH (3UL << MSI_CFG2_SH_SHIFT)182182+#define MSI_CFG2_MEMATTR_SHIFT 0183183+#define MSI_CFG2_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG2_MEMATTR_SHIFT)185184186185#define Q_IDX(q, p) ((p) & ((1 << (q)->max_n_shift) - 1))187186#define Q_WRP(q, p) ((p) & (1 << (q)->max_n_shift))···13311330 arm_smmu_cmdq_issue_cmd(smmu, &cmd);13321331}1333133213341334-static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie)13351335-{13361336- struct arm_smmu_domain *smmu_domain = cookie;13371337- struct arm_smmu_device *smmu = smmu_domain->smmu;13381338- unsigned long offset = (unsigned long)addr & ~PAGE_MASK;13391339-13401340- if (smmu->features & ARM_SMMU_FEAT_COHERENCY) {13411341- dsb(ishst);13421342- } else {13431343- dma_addr_t dma_addr;13441344- struct device *dev = smmu->dev;13451345-13461346- dma_addr = dma_map_page(dev, virt_to_page(addr), offset, size,13471347- DMA_TO_DEVICE);13481348-13491349- if (dma_mapping_error(dev, dma_addr))13501350- dev_err(dev, "failed to flush pgtable at %p\n", addr);13511351- else13521352- dma_unmap_page(dev, dma_addr, size, DMA_TO_DEVICE);13531353- }13541354-}13551355-13561333static struct iommu_gather_ops arm_smmu_gather_ops = {13571334 .tlb_flush_all = arm_smmu_tlb_inv_context,13581335 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,13591336 .tlb_sync = arm_smmu_tlb_sync,13601360- .flush_pgtable = arm_smmu_flush_pgtable,13611337};1362133813631339/* IOMMU API */···15091531 .ias = ias,15101532 .oas = oas,15111533 .tlb = &arm_smmu_gather_ops,15341534+ .iommu_dev = smmu->dev,15121535 };1513153615141537 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);···20322053 int ret;20332054 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;2034205520352035- /* Calculate the L1 size, capped to the SIDSIZE */20362036- size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);20372037- size = min(size, smmu->sid_bits - STRTAB_SPLIT);20562056+ /*20572057+ * If we can resolve everything with a single L2 table, then we20582058+ * just need a single L1 descriptor. Otherwise, calculate the L120592059+ * size, capped to the SIDSIZE.20602060+ */20612061+ if (smmu->sid_bits < STRTAB_SPLIT) {20622062+ size = 0;20632063+ } else {20642064+ size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);20652065+ size = min(size, smmu->sid_bits - STRTAB_SPLIT);20662066+ }20382067 cfg->num_l1_ents = 1 << size;2039206820402069 size += STRTAB_SPLIT;···21852198static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)21862199{21872200 int ret, irq;22012201+ u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;2188220221892203 /* Disable IRQs first */21902204 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,···22402252 if (IS_ERR_VALUE(ret))22412253 dev_warn(smmu->dev,22422254 "failed to enable priq irq\n");22552255+ else22562256+ irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;22432257 }22442258 }2245225922462260 /* Enable interrupt generation on the SMMU */22472247- ret = arm_smmu_write_reg_sync(smmu,22482248- IRQ_CTRL_EVTQ_IRQEN |22492249- IRQ_CTRL_GERROR_IRQEN,22612261+ ret = arm_smmu_write_reg_sync(smmu, irqen_flags,22502262 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);22512263 if (ret)22522264 dev_warn(smmu->dev, "failed to enable irqs\n");···25282540 case IDR5_OAS_44_BIT:25292541 smmu->oas = 44;25302542 break;25432543+ default:25442544+ dev_info(smmu->dev,25452545+ "unknown output address size. Truncating to 48-bit\n");25462546+ /* Fallthrough */25312547 case IDR5_OAS_48_BIT:25322548 smmu->oas = 48;25332533- break;25342534- default:25352535- dev_err(smmu->dev, "unknown output address size!\n");25362536- return -ENXIO;25372549 }2538255025392551 /* Set the DMA mask for our table walker */
+18-27
drivers/iommu/arm-smmu.c
···3737#include <linux/iopoll.h>3838#include <linux/module.h>3939#include <linux/of.h>4040+#include <linux/of_address.h>4041#include <linux/pci.h>4142#include <linux/platform_device.h>4243#include <linux/slab.h>···608607 }609608}610609611611-static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie)612612-{613613- struct arm_smmu_domain *smmu_domain = cookie;614614- struct arm_smmu_device *smmu = smmu_domain->smmu;615615- unsigned long offset = (unsigned long)addr & ~PAGE_MASK;616616-617617-618618- /* Ensure new page tables are visible to the hardware walker */619619- if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {620620- dsb(ishst);621621- } else {622622- /*623623- * If the SMMU can't walk tables in the CPU caches, treat them624624- * like non-coherent DMA since we need to flush the new entries625625- * all the way out to memory. There's no possibility of626626- * recursion here as the SMMU table walker will not be wired627627- * through another SMMU.628628- */629629- dma_map_page(smmu->dev, virt_to_page(addr), offset, size,630630- DMA_TO_DEVICE);631631- }632632-}633633-634610static struct iommu_gather_ops arm_smmu_gather_ops = {635611 .tlb_flush_all = arm_smmu_tlb_inv_context,636612 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,637613 .tlb_sync = arm_smmu_tlb_sync,638638- .flush_pgtable = arm_smmu_flush_pgtable,639614};640615641616static irqreturn_t arm_smmu_context_fault(int irq, void *dev)···875898 .ias = ias,876899 .oas = oas,877900 .tlb = &arm_smmu_gather_ops,901901+ .iommu_dev = smmu->dev,878902 };879903880904 smmu_domain->smmu = smmu;···15101532 unsigned long size;15111533 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);15121534 u32 id;15351535+ bool cttw_dt, cttw_reg;1513153615141537 dev_notice(smmu->dev, "probing hardware configuration...\n");15151538 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);···15501571 dev_notice(smmu->dev, "\taddress translation ops\n");15511572 }1552157315531553- if (id & ID0_CTTW) {15741574+ /*15751575+ * In order for DMA API calls to work properly, we must defer to what15761576+ * the DT says about coherency, regardless of what the hardware claims.15771577+ * Fortunately, this also opens up a workaround for systems where the15781578+ * ID register value has ended up configured incorrectly.15791579+ */15801580+ cttw_dt = of_dma_is_coherent(smmu->dev->of_node);15811581+ cttw_reg = !!(id & ID0_CTTW);15821582+ if (cttw_dt)15541583 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;15551555- dev_notice(smmu->dev, "\tcoherent table walk\n");15561556- }15841584+ if (cttw_dt || cttw_reg)15851585+ dev_notice(smmu->dev, "\t%scoherent table walk\n",15861586+ cttw_dt ? "" : "non-");15871587+ if (cttw_dt != cttw_reg)15881588+ dev_notice(smmu->dev,15891589+ "\t(IDR0.CTTW overridden by dma-coherent property)\n");1557159015581591 if (id & ID0_SMS) {15591592 u32 smr, sid, mask;
+93-33
drivers/iommu/io-pgtable-arm.c
···2626#include <linux/slab.h>2727#include <linux/types.h>28282929+#include <asm/barrier.h>3030+2931#include "io-pgtable.h"30323133#define ARM_LPAE_MAX_ADDR_BITS 48···202200203201static bool selftest_running = false;204202203203+static dma_addr_t __arm_lpae_dma_addr(struct device *dev, void *pages)204204+{205205+ return phys_to_dma(dev, virt_to_phys(pages));206206+}207207+208208+static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,209209+ struct io_pgtable_cfg *cfg)210210+{211211+ struct device *dev = cfg->iommu_dev;212212+ dma_addr_t dma;213213+ void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO);214214+215215+ if (!pages)216216+ return NULL;217217+218218+ if (!selftest_running) {219219+ dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);220220+ if (dma_mapping_error(dev, dma))221221+ goto out_free;222222+ /*223223+ * We depend on the IOMMU being able to work with any physical224224+ * address directly, so if the DMA layer suggests it can't by225225+ * giving us back some translation, that bodes very badly...226226+ */227227+ if (dma != __arm_lpae_dma_addr(dev, pages))228228+ goto out_unmap;229229+ }230230+231231+ return pages;232232+233233+out_unmap:234234+ dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");235235+ dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);236236+out_free:237237+ free_pages_exact(pages, size);238238+ return NULL;239239+}240240+241241+static void __arm_lpae_free_pages(void *pages, size_t size,242242+ struct io_pgtable_cfg *cfg)243243+{244244+ struct device *dev = cfg->iommu_dev;245245+246246+ if (!selftest_running)247247+ dma_unmap_single(dev, __arm_lpae_dma_addr(dev, pages),248248+ size, DMA_TO_DEVICE);249249+ free_pages_exact(pages, size);250250+}251251+252252+static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,253253+ struct io_pgtable_cfg *cfg)254254+{255255+ struct device *dev = cfg->iommu_dev;256256+257257+ *ptep = pte;258258+259259+ if (!selftest_running)260260+ dma_sync_single_for_device(dev, __arm_lpae_dma_addr(dev, ptep),261261+ sizeof(pte), DMA_TO_DEVICE);262262+}263263+205264static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,206265 unsigned long iova, phys_addr_t paddr,207266 arm_lpae_iopte prot, int lvl,208267 arm_lpae_iopte *ptep)209268{210269 arm_lpae_iopte pte = prot;270270+ struct io_pgtable_cfg *cfg = &data->iop.cfg;211271212272 /* We require an unmap first */213273 if (iopte_leaf(*ptep, lvl)) {···277213 return -EEXIST;278214 }279215280280- if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)216216+ if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)281217 pte |= ARM_LPAE_PTE_NS;282218283219 if (lvl == ARM_LPAE_MAX_LEVELS - 1)···288224 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;289225 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);290226291291- *ptep = pte;292292- data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), data->iop.cookie);227227+ __arm_lpae_set_pte(ptep, pte, cfg);293228 return 0;294229}295230···297234 int lvl, arm_lpae_iopte *ptep)298235{299236 arm_lpae_iopte *cptep, pte;300300- void *cookie = data->iop.cookie;301237 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);238238+ struct io_pgtable_cfg *cfg = &data->iop.cfg;302239303240 /* Find our entry at the current level */304241 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);305242306243 /* If we can install a leaf entry at this level, then do so */307307- if (size == block_size && (size & data->iop.cfg.pgsize_bitmap))244244+ if (size == block_size && (size & cfg->pgsize_bitmap))308245 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);309246310247 /* We can't allocate tables at the final level */···314251 /* Grab a pointer to the next level */315252 pte = *ptep;316253 if (!pte) {317317- cptep = alloc_pages_exact(1UL << data->pg_shift,318318- GFP_ATOMIC | __GFP_ZERO);254254+ cptep = __arm_lpae_alloc_pages(1UL << data->pg_shift,255255+ GFP_ATOMIC, cfg);319256 if (!cptep)320257 return -ENOMEM;321258322322- data->iop.cfg.tlb->flush_pgtable(cptep, 1UL << data->pg_shift,323323- cookie);324259 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;325325- if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)260260+ if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)326261 pte |= ARM_LPAE_PTE_NSTABLE;327327- *ptep = pte;328328- data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), cookie);262262+ __arm_lpae_set_pte(ptep, pte, cfg);329263 } else {330264 cptep = iopte_deref(pte, data);331265 }···369309{370310 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);371311 arm_lpae_iopte *ptep = data->pgd;372372- int lvl = ARM_LPAE_START_LVL(data);312312+ int ret, lvl = ARM_LPAE_START_LVL(data);373313 arm_lpae_iopte prot;374314375315 /* If no access, then nothing to do */···377317 return 0;378318379319 prot = arm_lpae_prot_to_pte(data, iommu_prot);380380- return __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);320320+ ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);321321+ /*322322+ * Synchronise all PTE updates for the new mapping before there's323323+ * a chance for anything to kick off a table walk for the new iova.324324+ */325325+ wmb();326326+327327+ return ret;381328}382329383330static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,···414347 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));415348 }416349417417- free_pages_exact(start, table_size);350350+ __arm_lpae_free_pages(start, table_size, &data->iop.cfg);418351}419352420353static void arm_lpae_free_pgtable(struct io_pgtable *iop)···433366 unsigned long blk_start, blk_end;434367 phys_addr_t blk_paddr;435368 arm_lpae_iopte table = 0;436436- void *cookie = data->iop.cookie;437437- const struct iommu_gather_ops *tlb = data->iop.cfg.tlb;369369+ struct io_pgtable_cfg *cfg = &data->iop.cfg;438370439371 blk_start = iova & ~(blk_size - 1);440372 blk_end = blk_start + blk_size;···459393 }460394 }461395462462- *ptep = table;463463- tlb->flush_pgtable(ptep, sizeof(*ptep), cookie);396396+ __arm_lpae_set_pte(ptep, table, cfg);464397 iova &= ~(blk_size - 1);465465- tlb->tlb_add_flush(iova, blk_size, true, cookie);398398+ cfg->tlb->tlb_add_flush(iova, blk_size, true, data->iop.cookie);466399 return size;467400}468401···483418484419 /* If the size matches this level, we're in the right place */485420 if (size == blk_size) {486486- *ptep = 0;487487- tlb->flush_pgtable(ptep, sizeof(*ptep), cookie);421421+ __arm_lpae_set_pte(ptep, 0, &data->iop.cfg);488422489423 if (!iopte_leaf(pte, lvl)) {490424 /* Also flush any partial walks */491425 tlb->tlb_add_flush(iova, size, false, cookie);492492- tlb->tlb_sync(data->iop.cookie);426426+ tlb->tlb_sync(cookie);493427 ptep = iopte_deref(pte, data);494428 __arm_lpae_free_pgtable(data, lvl + 1, ptep);495429 } else {···704640 cfg->arm_lpae_s1_cfg.mair[1] = 0;705641706642 /* Looking good; allocate a pgd */707707- data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO);643643+ data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);708644 if (!data->pgd)709645 goto out_free_data;710646711711- cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie);647647+ /* Ensure the empty pgd is visible before any actual TTBR write */648648+ wmb();712649713650 /* TTBRs */714651 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);···793728 cfg->arm_lpae_s2_cfg.vtcr = reg;794729795730 /* Allocate pgd pages */796796- data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO);731731+ data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);797732 if (!data->pgd)798733 goto out_free_data;799734800800- cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie);735735+ /* Ensure the empty pgd is visible before any actual TTBR write */736736+ wmb();801737802738 /* VTTBR */803739 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);···884818 WARN_ON(cookie != cfg_cookie);885819}886820887887-static void dummy_flush_pgtable(void *ptr, size_t size, void *cookie)888888-{889889- WARN_ON(cookie != cfg_cookie);890890-}891891-892821static struct iommu_gather_ops dummy_tlb_ops __initdata = {893822 .tlb_flush_all = dummy_tlb_flush_all,894823 .tlb_add_flush = dummy_tlb_add_flush,895824 .tlb_sync = dummy_tlb_sync,896896- .flush_pgtable = dummy_flush_pgtable,897825};898826899827static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
+6-3
drivers/iommu/io-pgtable.h
···1717 *1818 * @tlb_flush_all: Synchronously invalidate the entire TLB context.1919 * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range.2020- * @tlb_sync: Ensure any queue TLB invalidation has taken effect.2121- * @flush_pgtable: Ensure page table updates are visible to the IOMMU.2020+ * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and2121+ * any corresponding page table updates are visible to the2222+ * IOMMU.2223 *2324 * Note that these can all be called in atomic context and must therefore2425 * not block.···2928 void (*tlb_add_flush)(unsigned long iova, size_t size, bool leaf,3029 void *cookie);3130 void (*tlb_sync)(void *cookie);3232- void (*flush_pgtable)(void *ptr, size_t size, void *cookie);3331};34323533/**···4141 * @ias: Input address (iova) size, in bits.4242 * @oas: Output address (paddr) size, in bits.4343 * @tlb: TLB management callbacks for this set of tables.4444+ * @iommu_dev: The device representing the DMA configuration for the4545+ * page table walker.4446 */4547struct io_pgtable_cfg {4648 #define IO_PGTABLE_QUIRK_ARM_NS (1 << 0) /* Set NS bit in PTEs */···5149 unsigned int ias;5250 unsigned int oas;5351 const struct iommu_gather_ops *tlb;5252+ struct device *iommu_dev;54535554 /* Low-level data specific to the table format */5655 union {
+5-14
drivers/iommu/ipmmu-vmsa.c
···283283 /* The hardware doesn't support selective TLB flush. */284284}285285286286-static void ipmmu_flush_pgtable(void *ptr, size_t size, void *cookie)287287-{288288- unsigned long offset = (unsigned long)ptr & ~PAGE_MASK;289289- struct ipmmu_vmsa_domain *domain = cookie;290290-291291- /*292292- * TODO: Add support for coherent walk through CCI with DVM and remove293293- * cache handling.294294- */295295- dma_map_page(domain->mmu->dev, virt_to_page(ptr), offset, size,296296- DMA_TO_DEVICE);297297-}298298-299286static struct iommu_gather_ops ipmmu_gather_ops = {300287 .tlb_flush_all = ipmmu_tlb_flush_all,301288 .tlb_add_flush = ipmmu_tlb_add_flush,302289 .tlb_sync = ipmmu_tlb_flush_all,303303- .flush_pgtable = ipmmu_flush_pgtable,304290};305291306292/* -----------------------------------------------------------------------------···313327 domain->cfg.ias = 32;314328 domain->cfg.oas = 40;315329 domain->cfg.tlb = &ipmmu_gather_ops;330330+ /*331331+ * TODO: Add support for coherent walk through CCI with DVM and remove332332+ * cache handling. For now, delegate it to the io-pgtable code.333333+ */334334+ domain->cfg.iommu_dev = domain->mmu->dev;316335317336 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,318337 domain);