···133133 * or equal to the system's PAGE_SIZE, with a preference if134134 * both are equal.135135 */136136- pgsize_bitmap = tdev->iommu.domain->ops->pgsize_bitmap;136136+ pgsize_bitmap = tdev->iommu.domain->pgsize_bitmap;137137 if (pgsize_bitmap & PAGE_SIZE) {138138 tdev->iommu.pgshift = PAGE_SHIFT;139139 } else {
···537537 ret = NOTIFY_DONE;538538539539 /* In kdump kernel pci dev is not initialized yet -> send INVALID */540540- if (amd_iommu_is_attach_deferred(NULL, &pdev->dev)) {540540+ if (amd_iommu_is_attach_deferred(&pdev->dev)) {541541 amd_iommu_complete_ppr(pdev, iommu_fault->pasid,542542 PPR_INVALID, tag);543543 goto out;
···525525 struct iommu_dma_cookie *cookie = domain->iova_cookie;526526 unsigned long order, base_pfn;527527 struct iova_domain *iovad;528528+ int ret;528529529530 if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE)530531 return -EINVAL;···560559 }561560562561 init_iova_domain(iovad, 1UL << order, base_pfn);562562+ ret = iova_domain_init_rcaches(iovad);563563+ if (ret)564564+ return ret;563565564566 /* If the FQ fails we can simply fall back to strict mode */565567 if (domain->type == IOMMU_DOMAIN_DMA_FQ && iommu_dma_init_fq(domain))
···15731573 break;15741574 }1575157515761576- if (!has_iotlb_device) {15771577- struct subdev_domain_info *sinfo;15781578-15791579- list_for_each_entry(sinfo, &domain->subdevices, link_domain) {15801580- info = get_domain_info(sinfo->pdev);15811581- if (info && info->ats_enabled) {15821582- has_iotlb_device = true;15831583- break;15841584- }15851585- }15861586- }15871587-15881576 domain->has_iotlb_device = has_iotlb_device;15891577}15901578···16701682{16711683 unsigned long flags;16721684 struct device_domain_info *info;16731673- struct subdev_domain_info *sinfo;1674168516751686 if (!domain->has_iotlb_device)16761687 return;···16781691 list_for_each_entry(info, &domain->devices, link)16791692 __iommu_flush_dev_iotlb(info, addr, mask);1680169316811681- list_for_each_entry(sinfo, &domain->subdevices, link_domain) {16821682- info = get_domain_info(sinfo->pdev);16831683- __iommu_flush_dev_iotlb(info, addr, mask);16841684- }16851694 spin_unlock_irqrestore(&device_domain_lock, flags);16861686-}16871687-16881688-static void domain_flush_piotlb(struct intel_iommu *iommu,16891689- struct dmar_domain *domain,16901690- u64 addr, unsigned long npages, bool ih)16911691-{16921692- u16 did = domain->iommu_did[iommu->seq_id];16931693-16941694- if (domain->default_pasid)16951695- qi_flush_piotlb(iommu, did, domain->default_pasid,16961696- addr, npages, ih);16971697-16981698- if (!list_empty(&domain->devices))16991699- qi_flush_piotlb(iommu, did, PASID_RID2PASID, addr, npages, ih);17001695}1701169617021697static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,···16961727 ih = 1 << 6;1697172816981729 if (domain_use_first_level(domain)) {16991699- domain_flush_piotlb(iommu, domain, addr, pages, ih);17301730+ qi_flush_piotlb(iommu, did, PASID_RID2PASID, addr, pages, ih);17001731 } else {17011732 /*17021733 * Fallback to domain selective flush if no PSI support or···17451776 u16 did = dmar_domain->iommu_did[iommu->seq_id];1746177717471778 if (domain_use_first_level(dmar_domain))17481748- domain_flush_piotlb(iommu, dmar_domain, 0, -1, 0);17791779+ qi_flush_piotlb(iommu, did, PASID_RID2PASID, 0, -1, 0);17491780 else17501781 iommu->flush.flush_iotlb(iommu, did, 0, 0,17511782 DMA_TLB_DSI_FLUSH);···19521983 domain->flags |= DOMAIN_FLAG_USE_FIRST_LEVEL;19531984 domain->has_iotlb_device = false;19541985 INIT_LIST_HEAD(&domain->devices);19551955- INIT_LIST_HEAD(&domain->subdevices);1956198619571987 return domain;19581988}···26442676 info->domain = domain;26452677 info->iommu = iommu;26462678 info->pasid_table = NULL;26472647- info->auxd_enabled = 0;26482648- INIT_LIST_HEAD(&info->subdevices);2649267926502680 if (dev && dev_is_pci(dev)) {26512681 struct pci_dev *pdev = to_pci_dev(info->dev);···46034637 domain_exit(to_dmar_domain(domain));46044638}4605463946064606-/*46074607- * Check whether a @domain could be attached to the @dev through the46084608- * aux-domain attach/detach APIs.46094609- */46104610-static inline bool46114611-is_aux_domain(struct device *dev, struct iommu_domain *domain)46124612-{46134613- struct device_domain_info *info = get_domain_info(dev);46144614-46154615- return info && info->auxd_enabled &&46164616- domain->type == IOMMU_DOMAIN_UNMANAGED;46174617-}46184618-46194619-static inline struct subdev_domain_info *46204620-lookup_subdev_info(struct dmar_domain *domain, struct device *dev)46214621-{46224622- struct subdev_domain_info *sinfo;46234623-46244624- if (!list_empty(&domain->subdevices)) {46254625- list_for_each_entry(sinfo, &domain->subdevices, link_domain) {46264626- if (sinfo->pdev == dev)46274627- return sinfo;46284628- }46294629- }46304630-46314631- return NULL;46324632-}46334633-46344634-static int auxiliary_link_device(struct dmar_domain *domain,46354635- struct device *dev)46364636-{46374637- struct device_domain_info *info = get_domain_info(dev);46384638- struct subdev_domain_info *sinfo = lookup_subdev_info(domain, dev);46394639-46404640- assert_spin_locked(&device_domain_lock);46414641- if (WARN_ON(!info))46424642- return -EINVAL;46434643-46444644- if (!sinfo) {46454645- sinfo = kzalloc(sizeof(*sinfo), GFP_ATOMIC);46464646- if (!sinfo)46474647- return -ENOMEM;46484648- sinfo->domain = domain;46494649- sinfo->pdev = dev;46504650- list_add(&sinfo->link_phys, &info->subdevices);46514651- list_add(&sinfo->link_domain, &domain->subdevices);46524652- }46534653-46544654- return ++sinfo->users;46554655-}46564656-46574657-static int auxiliary_unlink_device(struct dmar_domain *domain,46584658- struct device *dev)46594659-{46604660- struct device_domain_info *info = get_domain_info(dev);46614661- struct subdev_domain_info *sinfo = lookup_subdev_info(domain, dev);46624662- int ret;46634663-46644664- assert_spin_locked(&device_domain_lock);46654665- if (WARN_ON(!info || !sinfo || sinfo->users <= 0))46664666- return -EINVAL;46674667-46684668- ret = --sinfo->users;46694669- if (!ret) {46704670- list_del(&sinfo->link_phys);46714671- list_del(&sinfo->link_domain);46724672- kfree(sinfo);46734673- }46744674-46754675- return ret;46764676-}46774677-46784678-static int aux_domain_add_dev(struct dmar_domain *domain,46794679- struct device *dev)46804680-{46814681- int ret;46824682- unsigned long flags;46834683- struct intel_iommu *iommu;46844684-46854685- iommu = device_to_iommu(dev, NULL, NULL);46864686- if (!iommu)46874687- return -ENODEV;46884688-46894689- if (domain->default_pasid <= 0) {46904690- u32 pasid;46914691-46924692- /* No private data needed for the default pasid */46934693- pasid = ioasid_alloc(NULL, PASID_MIN,46944694- pci_max_pasids(to_pci_dev(dev)) - 1,46954695- NULL);46964696- if (pasid == INVALID_IOASID) {46974697- pr_err("Can't allocate default pasid\n");46984698- return -ENODEV;46994699- }47004700- domain->default_pasid = pasid;47014701- }47024702-47034703- spin_lock_irqsave(&device_domain_lock, flags);47044704- ret = auxiliary_link_device(domain, dev);47054705- if (ret <= 0)47064706- goto link_failed;47074707-47084708- /*47094709- * Subdevices from the same physical device can be attached to the47104710- * same domain. For such cases, only the first subdevice attachment47114711- * needs to go through the full steps in this function. So if ret >47124712- * 1, just goto out.47134713- */47144714- if (ret > 1)47154715- goto out;47164716-47174717- /*47184718- * iommu->lock must be held to attach domain to iommu and setup the47194719- * pasid entry for second level translation.47204720- */47214721- spin_lock(&iommu->lock);47224722- ret = domain_attach_iommu(domain, iommu);47234723- if (ret)47244724- goto attach_failed;47254725-47264726- /* Setup the PASID entry for mediated devices: */47274727- if (domain_use_first_level(domain))47284728- ret = domain_setup_first_level(iommu, domain, dev,47294729- domain->default_pasid);47304730- else47314731- ret = intel_pasid_setup_second_level(iommu, domain, dev,47324732- domain->default_pasid);47334733- if (ret)47344734- goto table_failed;47354735-47364736- spin_unlock(&iommu->lock);47374737-out:47384738- spin_unlock_irqrestore(&device_domain_lock, flags);47394739-47404740- return 0;47414741-47424742-table_failed:47434743- domain_detach_iommu(domain, iommu);47444744-attach_failed:47454745- spin_unlock(&iommu->lock);47464746- auxiliary_unlink_device(domain, dev);47474747-link_failed:47484748- spin_unlock_irqrestore(&device_domain_lock, flags);47494749- if (list_empty(&domain->subdevices) && domain->default_pasid > 0)47504750- ioasid_put(domain->default_pasid);47514751-47524752- return ret;47534753-}47544754-47554755-static void aux_domain_remove_dev(struct dmar_domain *domain,47564756- struct device *dev)47574757-{47584758- struct device_domain_info *info;47594759- struct intel_iommu *iommu;47604760- unsigned long flags;47614761-47624762- if (!is_aux_domain(dev, &domain->domain))47634763- return;47644764-47654765- spin_lock_irqsave(&device_domain_lock, flags);47664766- info = get_domain_info(dev);47674767- iommu = info->iommu;47684768-47694769- if (!auxiliary_unlink_device(domain, dev)) {47704770- spin_lock(&iommu->lock);47714771- intel_pasid_tear_down_entry(iommu, dev,47724772- domain->default_pasid, false);47734773- domain_detach_iommu(domain, iommu);47744774- spin_unlock(&iommu->lock);47754775- }47764776-47774777- spin_unlock_irqrestore(&device_domain_lock, flags);47784778-47794779- if (list_empty(&domain->subdevices) && domain->default_pasid > 0)47804780- ioasid_put(domain->default_pasid);47814781-}47824782-47834640static int prepare_domain_attach_device(struct iommu_domain *domain,47844641 struct device *dev)47854642{···46134824 iommu = device_to_iommu(dev, NULL, NULL);46144825 if (!iommu)46154826 return -ENODEV;46164616-46174617- if ((dmar_domain->flags & DOMAIN_FLAG_NESTING_MODE) &&46184618- !ecap_nest(iommu->ecap)) {46194619- dev_err(dev, "%s: iommu not support nested translation\n",46204620- iommu->name);46214621- return -EINVAL;46224622- }4623482746244828 /* check if this iommu agaw is sufficient for max mapped address */46254829 addr_width = agaw_to_width(iommu->agaw);···46554873 return -EPERM;46564874 }4657487546584658- if (is_aux_domain(dev, domain))46594659- return -EPERM;46604660-46614876 /* normally dev is not mapped */46624877 if (unlikely(domain_context_mapped(dev))) {46634878 struct dmar_domain *old_domain;···46714892 return domain_add_dev_info(to_dmar_domain(domain), dev);46724893}4673489446744674-static int intel_iommu_aux_attach_device(struct iommu_domain *domain,46754675- struct device *dev)46764676-{46774677- int ret;46784678-46794679- if (!is_aux_domain(dev, domain))46804680- return -EPERM;46814681-46824682- ret = prepare_domain_attach_device(domain, dev);46834683- if (ret)46844684- return ret;46854685-46864686- return aux_domain_add_dev(to_dmar_domain(domain), dev);46874687-}46884688-46894895static void intel_iommu_detach_device(struct iommu_domain *domain,46904896 struct device *dev)46914897{46924898 dmar_remove_one_dev_info(dev);46934899}46944694-46954695-static void intel_iommu_aux_detach_device(struct iommu_domain *domain,46964696- struct device *dev)46974697-{46984698- aux_domain_remove_dev(to_dmar_domain(domain), dev);46994699-}47004700-47014701-#ifdef CONFIG_INTEL_IOMMU_SVM47024702-/*47034703- * 2D array for converting and sanitizing IOMMU generic TLB granularity to47044704- * VT-d granularity. Invalidation is typically included in the unmap operation47054705- * as a result of DMA or VFIO unmap. However, for assigned devices guest47064706- * owns the first level page tables. Invalidations of translation caches in the47074707- * guest are trapped and passed down to the host.47084708- *47094709- * vIOMMU in the guest will only expose first level page tables, therefore47104710- * we do not support IOTLB granularity for request without PASID (second level).47114711- *47124712- * For example, to find the VT-d granularity encoding for IOTLB47134713- * type and page selective granularity within PASID:47144714- * X: indexed by iommu cache type47154715- * Y: indexed by enum iommu_inv_granularity47164716- * [IOMMU_CACHE_INV_TYPE_IOTLB][IOMMU_INV_GRANU_ADDR]47174717- */47184718-47194719-static const int47204720-inv_type_granu_table[IOMMU_CACHE_INV_TYPE_NR][IOMMU_INV_GRANU_NR] = {47214721- /*47224722- * PASID based IOTLB invalidation: PASID selective (per PASID),47234723- * page selective (address granularity)47244724- */47254725- {-EINVAL, QI_GRAN_NONG_PASID, QI_GRAN_PSI_PASID},47264726- /* PASID based dev TLBs */47274727- {-EINVAL, -EINVAL, QI_DEV_IOTLB_GRAN_PASID_SEL},47284728- /* PASID cache */47294729- {-EINVAL, -EINVAL, -EINVAL}47304730-};47314731-47324732-static inline int to_vtd_granularity(int type, int granu)47334733-{47344734- return inv_type_granu_table[type][granu];47354735-}47364736-47374737-static inline u64 to_vtd_size(u64 granu_size, u64 nr_granules)47384738-{47394739- u64 nr_pages = (granu_size * nr_granules) >> VTD_PAGE_SHIFT;47404740-47414741- /* VT-d size is encoded as 2^size of 4K pages, 0 for 4k, 9 for 2MB, etc.47424742- * IOMMU cache invalidate API passes granu_size in bytes, and number of47434743- * granu size in contiguous memory.47444744- */47454745- return order_base_2(nr_pages);47464746-}47474747-47484748-static int47494749-intel_iommu_sva_invalidate(struct iommu_domain *domain, struct device *dev,47504750- struct iommu_cache_invalidate_info *inv_info)47514751-{47524752- struct dmar_domain *dmar_domain = to_dmar_domain(domain);47534753- struct device_domain_info *info;47544754- struct intel_iommu *iommu;47554755- unsigned long flags;47564756- int cache_type;47574757- u8 bus, devfn;47584758- u16 did, sid;47594759- int ret = 0;47604760- u64 size = 0;47614761-47624762- if (!inv_info || !dmar_domain)47634763- return -EINVAL;47644764-47654765- if (!dev || !dev_is_pci(dev))47664766- return -ENODEV;47674767-47684768- iommu = device_to_iommu(dev, &bus, &devfn);47694769- if (!iommu)47704770- return -ENODEV;47714771-47724772- if (!(dmar_domain->flags & DOMAIN_FLAG_NESTING_MODE))47734773- return -EINVAL;47744774-47754775- spin_lock_irqsave(&device_domain_lock, flags);47764776- spin_lock(&iommu->lock);47774777- info = get_domain_info(dev);47784778- if (!info) {47794779- ret = -EINVAL;47804780- goto out_unlock;47814781- }47824782- did = dmar_domain->iommu_did[iommu->seq_id];47834783- sid = PCI_DEVID(bus, devfn);47844784-47854785- /* Size is only valid in address selective invalidation */47864786- if (inv_info->granularity == IOMMU_INV_GRANU_ADDR)47874787- size = to_vtd_size(inv_info->granu.addr_info.granule_size,47884788- inv_info->granu.addr_info.nb_granules);47894789-47904790- for_each_set_bit(cache_type,47914791- (unsigned long *)&inv_info->cache,47924792- IOMMU_CACHE_INV_TYPE_NR) {47934793- int granu = 0;47944794- u64 pasid = 0;47954795- u64 addr = 0;47964796-47974797- granu = to_vtd_granularity(cache_type, inv_info->granularity);47984798- if (granu == -EINVAL) {47994799- pr_err_ratelimited("Invalid cache type and granu combination %d/%d\n",48004800- cache_type, inv_info->granularity);48014801- break;48024802- }48034803-48044804- /*48054805- * PASID is stored in different locations based on the48064806- * granularity.48074807- */48084808- if (inv_info->granularity == IOMMU_INV_GRANU_PASID &&48094809- (inv_info->granu.pasid_info.flags & IOMMU_INV_PASID_FLAGS_PASID))48104810- pasid = inv_info->granu.pasid_info.pasid;48114811- else if (inv_info->granularity == IOMMU_INV_GRANU_ADDR &&48124812- (inv_info->granu.addr_info.flags & IOMMU_INV_ADDR_FLAGS_PASID))48134813- pasid = inv_info->granu.addr_info.pasid;48144814-48154815- switch (BIT(cache_type)) {48164816- case IOMMU_CACHE_INV_TYPE_IOTLB:48174817- /* HW will ignore LSB bits based on address mask */48184818- if (inv_info->granularity == IOMMU_INV_GRANU_ADDR &&48194819- size &&48204820- (inv_info->granu.addr_info.addr & ((BIT(VTD_PAGE_SHIFT + size)) - 1))) {48214821- pr_err_ratelimited("User address not aligned, 0x%llx, size order %llu\n",48224822- inv_info->granu.addr_info.addr, size);48234823- }48244824-48254825- /*48264826- * If granu is PASID-selective, address is ignored.48274827- * We use npages = -1 to indicate that.48284828- */48294829- qi_flush_piotlb(iommu, did, pasid,48304830- mm_to_dma_pfn(inv_info->granu.addr_info.addr),48314831- (granu == QI_GRAN_NONG_PASID) ? -1 : 1 << size,48324832- inv_info->granu.addr_info.flags & IOMMU_INV_ADDR_FLAGS_LEAF);48334833-48344834- if (!info->ats_enabled)48354835- break;48364836- /*48374837- * Always flush device IOTLB if ATS is enabled. vIOMMU48384838- * in the guest may assume IOTLB flush is inclusive,48394839- * which is more efficient.48404840- */48414841- fallthrough;48424842- case IOMMU_CACHE_INV_TYPE_DEV_IOTLB:48434843- /*48444844- * PASID based device TLB invalidation does not support48454845- * IOMMU_INV_GRANU_PASID granularity but only supports48464846- * IOMMU_INV_GRANU_ADDR.48474847- * The equivalent of that is we set the size to be the48484848- * entire range of 64 bit. User only provides PASID info48494849- * without address info. So we set addr to 0.48504850- */48514851- if (inv_info->granularity == IOMMU_INV_GRANU_PASID) {48524852- size = 64 - VTD_PAGE_SHIFT;48534853- addr = 0;48544854- } else if (inv_info->granularity == IOMMU_INV_GRANU_ADDR) {48554855- addr = inv_info->granu.addr_info.addr;48564856- }48574857-48584858- if (info->ats_enabled)48594859- qi_flush_dev_iotlb_pasid(iommu, sid,48604860- info->pfsid, pasid,48614861- info->ats_qdep, addr,48624862- size);48634863- else48644864- pr_warn_ratelimited("Passdown device IOTLB flush w/o ATS!\n");48654865- break;48664866- default:48674867- dev_err_ratelimited(dev, "Unsupported IOMMU invalidation type %d\n",48684868- cache_type);48694869- ret = -EINVAL;48704870- }48714871- }48724872-out_unlock:48734873- spin_unlock(&iommu->lock);48744874- spin_unlock_irqrestore(&device_domain_lock, flags);48754875-48764876- return ret;48774877-}48784878-#endif4879490048804901static int intel_iommu_map(struct iommu_domain *domain,48814902 unsigned long iova, phys_addr_t hpa,···49705391 return generic_device_group(dev);49715392}4972539349734973-static int intel_iommu_enable_auxd(struct device *dev)49744974-{49754975- struct device_domain_info *info;49764976- struct intel_iommu *iommu;49774977- unsigned long flags;49784978- int ret;49794979-49804980- iommu = device_to_iommu(dev, NULL, NULL);49814981- if (!iommu || dmar_disabled)49824982- return -EINVAL;49834983-49844984- if (!sm_supported(iommu) || !pasid_supported(iommu))49854985- return -EINVAL;49864986-49874987- ret = intel_iommu_enable_pasid(iommu, dev);49884988- if (ret)49894989- return -ENODEV;49904990-49914991- spin_lock_irqsave(&device_domain_lock, flags);49924992- info = get_domain_info(dev);49934993- info->auxd_enabled = 1;49944994- spin_unlock_irqrestore(&device_domain_lock, flags);49954995-49964996- return 0;49974997-}49984998-49994999-static int intel_iommu_disable_auxd(struct device *dev)50005000-{50015001- struct device_domain_info *info;50025002- unsigned long flags;50035003-50045004- spin_lock_irqsave(&device_domain_lock, flags);50055005- info = get_domain_info(dev);50065006- if (!WARN_ON(!info))50075007- info->auxd_enabled = 0;50085008- spin_unlock_irqrestore(&device_domain_lock, flags);50095009-50105010- return 0;50115011-}50125012-50135394static int intel_iommu_enable_sva(struct device *dev)50145395{50155396 struct device_domain_info *info = get_domain_info(dev);···50265487intel_iommu_dev_enable_feat(struct device *dev, enum iommu_dev_features feat)50275488{50285489 switch (feat) {50295029- case IOMMU_DEV_FEAT_AUX:50305030- return intel_iommu_enable_auxd(dev);50315031-50325490 case IOMMU_DEV_FEAT_IOPF:50335491 return intel_iommu_enable_iopf(dev);50345492···50415505intel_iommu_dev_disable_feat(struct device *dev, enum iommu_dev_features feat)50425506{50435507 switch (feat) {50445044- case IOMMU_DEV_FEAT_AUX:50455045- return intel_iommu_disable_auxd(dev);50465046-50475508 case IOMMU_DEV_FEAT_IOPF:50485509 return 0;50495510···50525519 }50535520}5054552150555055-static bool50565056-intel_iommu_dev_feat_enabled(struct device *dev, enum iommu_dev_features feat)50575057-{50585058- struct device_domain_info *info = get_domain_info(dev);50595059-50605060- if (feat == IOMMU_DEV_FEAT_AUX)50615061- return scalable_mode_support() && info && info->auxd_enabled;50625062-50635063- return false;50645064-}50655065-50665066-static int50675067-intel_iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)50685068-{50695069- struct dmar_domain *dmar_domain = to_dmar_domain(domain);50705070-50715071- return dmar_domain->default_pasid > 0 ?50725072- dmar_domain->default_pasid : -EINVAL;50735073-}50745074-50755075-static bool intel_iommu_is_attach_deferred(struct iommu_domain *domain,50765076- struct device *dev)55225522+static bool intel_iommu_is_attach_deferred(struct device *dev)50775523{50785524 return attach_deferred(dev);50795079-}50805080-50815081-static int50825082-intel_iommu_enable_nesting(struct iommu_domain *domain)50835083-{50845084- struct dmar_domain *dmar_domain = to_dmar_domain(domain);50855085- unsigned long flags;50865086- int ret = -ENODEV;50875087-50885088- spin_lock_irqsave(&device_domain_lock, flags);50895089- if (list_empty(&dmar_domain->devices)) {50905090- dmar_domain->flags |= DOMAIN_FLAG_NESTING_MODE;50915091- dmar_domain->flags &= ~DOMAIN_FLAG_USE_FIRST_LEVEL;50925092- ret = 0;50935093- }50945094- spin_unlock_irqrestore(&device_domain_lock, flags);50955095-50965096- return ret;50975525}5098552650995527/*···50925598const struct iommu_ops intel_iommu_ops = {50935599 .capable = intel_iommu_capable,50945600 .domain_alloc = intel_iommu_domain_alloc,50955095- .domain_free = intel_iommu_domain_free,50965096- .enable_nesting = intel_iommu_enable_nesting,50975097- .attach_dev = intel_iommu_attach_device,50985098- .detach_dev = intel_iommu_detach_device,50995099- .aux_attach_dev = intel_iommu_aux_attach_device,51005100- .aux_detach_dev = intel_iommu_aux_detach_device,51015101- .aux_get_pasid = intel_iommu_aux_get_pasid,51025102- .map_pages = intel_iommu_map_pages,51035103- .unmap_pages = intel_iommu_unmap_pages,51045104- .iotlb_sync_map = intel_iommu_iotlb_sync_map,51055105- .flush_iotlb_all = intel_flush_iotlb_all,51065106- .iotlb_sync = intel_iommu_tlb_sync,51075107- .iova_to_phys = intel_iommu_iova_to_phys,51085601 .probe_device = intel_iommu_probe_device,51095602 .probe_finalize = intel_iommu_probe_finalize,51105603 .release_device = intel_iommu_release_device,51115604 .get_resv_regions = intel_iommu_get_resv_regions,51125605 .put_resv_regions = generic_iommu_put_resv_regions,51135606 .device_group = intel_iommu_device_group,51145114- .dev_feat_enabled = intel_iommu_dev_feat_enabled,51155607 .dev_enable_feat = intel_iommu_dev_enable_feat,51165608 .dev_disable_feat = intel_iommu_dev_disable_feat,51175609 .is_attach_deferred = intel_iommu_is_attach_deferred,51185610 .def_domain_type = device_def_domain_type,51195611 .pgsize_bitmap = SZ_4K,51205612#ifdef CONFIG_INTEL_IOMMU_SVM51215121- .cache_invalidate = intel_iommu_sva_invalidate,51225122- .sva_bind_gpasid = intel_svm_bind_gpasid,51235123- .sva_unbind_gpasid = intel_svm_unbind_gpasid,51245613 .sva_bind = intel_svm_bind,51255614 .sva_unbind = intel_svm_unbind,51265615 .sva_get_pasid = intel_svm_get_pasid,51275616 .page_response = intel_svm_page_response,51285617#endif56185618+ .default_domain_ops = &(const struct iommu_domain_ops) {56195619+ .attach_dev = intel_iommu_attach_device,56205620+ .detach_dev = intel_iommu_detach_device,56215621+ .map_pages = intel_iommu_map_pages,56225622+ .unmap_pages = intel_iommu_unmap_pages,56235623+ .iotlb_sync_map = intel_iommu_iotlb_sync_map,56245624+ .flush_iotlb_all = intel_flush_iotlb_all,56255625+ .iotlb_sync = intel_iommu_tlb_sync,56265626+ .iova_to_phys = intel_iommu_iova_to_phys,56275627+ .free = intel_iommu_domain_free,56285628+ }51295629};5130563051315631static void quirk_iommu_igfx(struct pci_dev *dev)
-161
drivers/iommu/intel/pasid.c
···762762763763 return 0;764764}765765-766766-static int767767-intel_pasid_setup_bind_data(struct intel_iommu *iommu, struct pasid_entry *pte,768768- struct iommu_gpasid_bind_data_vtd *pasid_data)769769-{770770- /*771771- * Not all guest PASID table entry fields are passed down during bind,772772- * here we only set up the ones that are dependent on guest settings.773773- * Execution related bits such as NXE, SMEP are not supported.774774- * Other fields, such as snoop related, are set based on host needs775775- * regardless of guest settings.776776- */777777- if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_SRE) {778778- if (!ecap_srs(iommu->ecap)) {779779- pr_err_ratelimited("No supervisor request support on %s\n",780780- iommu->name);781781- return -EINVAL;782782- }783783- pasid_set_sre(pte);784784- /* Enable write protect WP if guest requested */785785- if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_WPE)786786- pasid_set_wpe(pte);787787- }788788-789789- if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_EAFE) {790790- if (!ecap_eafs(iommu->ecap)) {791791- pr_err_ratelimited("No extended access flag support on %s\n",792792- iommu->name);793793- return -EINVAL;794794- }795795- pasid_set_eafe(pte);796796- }797797-798798- /*799799- * Memory type is only applicable to devices inside processor coherent800800- * domain. Will add MTS support once coherent devices are available.801801- */802802- if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_MTS_MASK) {803803- pr_warn_ratelimited("No memory type support %s\n",804804- iommu->name);805805- return -EINVAL;806806- }807807-808808- return 0;809809-}810810-811811-/**812812- * intel_pasid_setup_nested() - Set up PASID entry for nested translation.813813- * This could be used for guest shared virtual address. In this case, the814814- * first level page tables are used for GVA-GPA translation in the guest,815815- * second level page tables are used for GPA-HPA translation.816816- *817817- * @iommu: IOMMU which the device belong to818818- * @dev: Device to be set up for translation819819- * @gpgd: FLPTPTR: First Level Page translation pointer in GPA820820- * @pasid: PASID to be programmed in the device PASID table821821- * @pasid_data: Additional PASID info from the guest bind request822822- * @domain: Domain info for setting up second level page tables823823- * @addr_width: Address width of the first level (guest)824824- */825825-int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,826826- pgd_t *gpgd, u32 pasid,827827- struct iommu_gpasid_bind_data_vtd *pasid_data,828828- struct dmar_domain *domain, int addr_width)829829-{830830- struct pasid_entry *pte;831831- struct dma_pte *pgd;832832- int ret = 0;833833- u64 pgd_val;834834- int agaw;835835- u16 did;836836-837837- if (!ecap_nest(iommu->ecap)) {838838- pr_err_ratelimited("IOMMU: %s: No nested translation support\n",839839- iommu->name);840840- return -EINVAL;841841- }842842-843843- if (!(domain->flags & DOMAIN_FLAG_NESTING_MODE)) {844844- pr_err_ratelimited("Domain is not in nesting mode, %x\n",845845- domain->flags);846846- return -EINVAL;847847- }848848-849849- pte = intel_pasid_get_entry(dev, pasid);850850- if (WARN_ON(!pte))851851- return -EINVAL;852852-853853- /*854854- * Caller must ensure PASID entry is not in use, i.e. not bind the855855- * same PASID to the same device twice.856856- */857857- if (pasid_pte_is_present(pte))858858- return -EBUSY;859859-860860- pasid_clear_entry(pte);861861-862862- /* Sanity checking performed by caller to make sure address863863- * width matching in two dimensions:864864- * 1. CPU vs. IOMMU865865- * 2. Guest vs. Host.866866- */867867- switch (addr_width) {868868-#ifdef CONFIG_X86869869- case ADDR_WIDTH_5LEVEL:870870- if (!cpu_feature_enabled(X86_FEATURE_LA57) ||871871- !cap_5lp_support(iommu->cap)) {872872- dev_err_ratelimited(dev,873873- "5-level paging not supported\n");874874- return -EINVAL;875875- }876876-877877- pasid_set_flpm(pte, 1);878878- break;879879-#endif880880- case ADDR_WIDTH_4LEVEL:881881- pasid_set_flpm(pte, 0);882882- break;883883- default:884884- dev_err_ratelimited(dev, "Invalid guest address width %d\n",885885- addr_width);886886- return -EINVAL;887887- }888888-889889- /* First level PGD is in GPA, must be supported by the second level */890890- if ((uintptr_t)gpgd > domain->max_addr) {891891- dev_err_ratelimited(dev,892892- "Guest PGD %lx not supported, max %llx\n",893893- (uintptr_t)gpgd, domain->max_addr);894894- return -EINVAL;895895- }896896- pasid_set_flptr(pte, (uintptr_t)gpgd);897897-898898- ret = intel_pasid_setup_bind_data(iommu, pte, pasid_data);899899- if (ret)900900- return ret;901901-902902- /* Setup the second level based on the given domain */903903- pgd = domain->pgd;904904-905905- agaw = iommu_skip_agaw(domain, iommu, &pgd);906906- if (agaw < 0) {907907- dev_err_ratelimited(dev, "Invalid domain page table\n");908908- return -EINVAL;909909- }910910- pgd_val = virt_to_phys(pgd);911911- pasid_set_slptr(pte, pgd_val);912912- pasid_set_fault_enable(pte);913913-914914- did = domain->iommu_did[iommu->seq_id];915915- pasid_set_domain_id(pte, did);916916-917917- pasid_set_address_width(pte, agaw);918918- pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));919919-920920- pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED);921921- pasid_set_present(pte);922922- pasid_flush_caches(iommu, pte, pasid, did);923923-924924- return ret;925925-}
···318318 return 0;319319}320320321321-int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,322322- struct iommu_gpasid_bind_data *data)323323-{324324- struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);325325- struct intel_svm_dev *sdev = NULL;326326- struct dmar_domain *dmar_domain;327327- struct device_domain_info *info;328328- struct intel_svm *svm = NULL;329329- unsigned long iflags;330330- int ret = 0;331331-332332- if (WARN_ON(!iommu) || !data)333333- return -EINVAL;334334-335335- if (data->format != IOMMU_PASID_FORMAT_INTEL_VTD)336336- return -EINVAL;337337-338338- /* IOMMU core ensures argsz is more than the start of the union */339339- if (data->argsz < offsetofend(struct iommu_gpasid_bind_data, vendor.vtd))340340- return -EINVAL;341341-342342- /* Make sure no undefined flags are used in vendor data */343343- if (data->vendor.vtd.flags & ~(IOMMU_SVA_VTD_GPASID_LAST - 1))344344- return -EINVAL;345345-346346- if (!dev_is_pci(dev))347347- return -ENOTSUPP;348348-349349- /* VT-d supports devices with full 20 bit PASIDs only */350350- if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX)351351- return -EINVAL;352352-353353- /*354354- * We only check host PASID range, we have no knowledge to check355355- * guest PASID range.356356- */357357- if (data->hpasid <= 0 || data->hpasid >= PASID_MAX)358358- return -EINVAL;359359-360360- info = get_domain_info(dev);361361- if (!info)362362- return -EINVAL;363363-364364- dmar_domain = to_dmar_domain(domain);365365-366366- mutex_lock(&pasid_mutex);367367- ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev);368368- if (ret)369369- goto out;370370-371371- if (sdev) {372372- /*373373- * Do not allow multiple bindings of the same device-PASID since374374- * there is only one SL page tables per PASID. We may revisit375375- * once sharing PGD across domains are supported.376376- */377377- dev_warn_ratelimited(dev, "Already bound with PASID %u\n",378378- svm->pasid);379379- ret = -EBUSY;380380- goto out;381381- }382382-383383- if (!svm) {384384- /* We come here when PASID has never been bond to a device. */385385- svm = kzalloc(sizeof(*svm), GFP_KERNEL);386386- if (!svm) {387387- ret = -ENOMEM;388388- goto out;389389- }390390- /* REVISIT: upper layer/VFIO can track host process that bind391391- * the PASID. ioasid_set = mm might be sufficient for vfio to392392- * check pasid VMM ownership. We can drop the following line393393- * once VFIO and IOASID set check is in place.394394- */395395- svm->mm = get_task_mm(current);396396- svm->pasid = data->hpasid;397397- if (data->flags & IOMMU_SVA_GPASID_VAL) {398398- svm->gpasid = data->gpasid;399399- svm->flags |= SVM_FLAG_GUEST_PASID;400400- }401401- pasid_private_add(data->hpasid, svm);402402- INIT_LIST_HEAD_RCU(&svm->devs);403403- mmput(svm->mm);404404- }405405- sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);406406- if (!sdev) {407407- ret = -ENOMEM;408408- goto out;409409- }410410- sdev->dev = dev;411411- sdev->sid = PCI_DEVID(info->bus, info->devfn);412412- sdev->iommu = iommu;413413-414414- /* Only count users if device has aux domains */415415- if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))416416- sdev->users = 1;417417-418418- /* Set up device context entry for PASID if not enabled already */419419- ret = intel_iommu_enable_pasid(iommu, sdev->dev);420420- if (ret) {421421- dev_err_ratelimited(dev, "Failed to enable PASID capability\n");422422- kfree(sdev);423423- goto out;424424- }425425-426426- /*427427- * PASID table is per device for better security. Therefore, for428428- * each bind of a new device even with an existing PASID, we need to429429- * call the nested mode setup function here.430430- */431431- spin_lock_irqsave(&iommu->lock, iflags);432432- ret = intel_pasid_setup_nested(iommu, dev,433433- (pgd_t *)(uintptr_t)data->gpgd,434434- data->hpasid, &data->vendor.vtd, dmar_domain,435435- data->addr_width);436436- spin_unlock_irqrestore(&iommu->lock, iflags);437437- if (ret) {438438- dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",439439- data->hpasid, ret);440440- /*441441- * PASID entry should be in cleared state if nested mode442442- * set up failed. So we only need to clear IOASID tracking443443- * data such that free call will succeed.444444- */445445- kfree(sdev);446446- goto out;447447- }448448-449449- svm->flags |= SVM_FLAG_GUEST_MODE;450450-451451- init_rcu_head(&sdev->rcu);452452- list_add_rcu(&sdev->list, &svm->devs);453453- out:454454- if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {455455- pasid_private_remove(data->hpasid);456456- kfree(svm);457457- }458458-459459- mutex_unlock(&pasid_mutex);460460- return ret;461461-}462462-463463-int intel_svm_unbind_gpasid(struct device *dev, u32 pasid)464464-{465465- struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);466466- struct intel_svm_dev *sdev;467467- struct intel_svm *svm;468468- int ret;469469-470470- if (WARN_ON(!iommu))471471- return -EINVAL;472472-473473- mutex_lock(&pasid_mutex);474474- ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);475475- if (ret)476476- goto out;477477-478478- if (sdev) {479479- if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))480480- sdev->users--;481481- if (!sdev->users) {482482- list_del_rcu(&sdev->list);483483- intel_pasid_tear_down_entry(iommu, dev,484484- svm->pasid, false);485485- intel_svm_drain_prq(dev, svm->pasid);486486- kfree_rcu(sdev, rcu);487487-488488- if (list_empty(&svm->devs)) {489489- /*490490- * We do not free the IOASID here in that491491- * IOMMU driver did not allocate it.492492- * Unlike native SVM, IOASID for guest use was493493- * allocated prior to the bind call.494494- * In any case, if the free call comes before495495- * the unbind, IOMMU driver will get notified496496- * and perform cleanup.497497- */498498- pasid_private_remove(pasid);499499- kfree(svm);500500- }501501- }502502- }503503-out:504504- mutex_unlock(&pasid_mutex);505505- return ret;506506-}507507-508321static int intel_svm_alloc_pasid(struct device *dev, struct mm_struct *mm,509322 unsigned int flags)510323{···9361123 if (ret || !sdev) {9371124 ret = -ENODEV;9381125 goto out;939939- }940940-941941- /*942942- * For responses from userspace, need to make sure that the943943- * pasid has been bound to its mm.944944- */945945- if (svm->flags & SVM_FLAG_GUEST_MODE) {946946- struct mm_struct *mm;947947-948948- mm = get_task_mm(current);949949- if (!mm) {950950- ret = -EINVAL;951951- goto out;952952- }953953-954954- if (mm != svm->mm) {955955- ret = -ENODEV;956956- mmput(mm);957957- goto out;958958- }959959-960960- mmput(mm);9611126 }96211279631128 /*
+39-300
drivers/iommu/iommu.c
···323323324324void iommu_release_device(struct device *dev)325325{326326- const struct iommu_ops *ops = dev->bus->iommu_ops;326326+ const struct iommu_ops *ops;327327328328 if (!dev->iommu)329329 return;330330331331 iommu_device_unlink(dev->iommu->iommu_dev, dev);332332333333+ ops = dev_iommu_ops(dev);333334 ops->release_device(dev);334335335336 iommu_group_remove_device(dev);···791790 dma_addr_t start, end, addr;792791 size_t map_size = 0;793792794794- if (domain->ops->apply_resv_region)795795- domain->ops->apply_resv_region(dev, domain, entry);796796-797793 start = ALIGN(entry->start, pg_size);798794 end = ALIGN(entry->start + entry->length, pg_size);799795···831833 return ret;832834}833835834834-static bool iommu_is_attach_deferred(struct iommu_domain *domain,835835- struct device *dev)836836+static bool iommu_is_attach_deferred(struct device *dev)836837{837837- if (domain->ops->is_attach_deferred)838838- return domain->ops->is_attach_deferred(domain, dev);838838+ const struct iommu_ops *ops = dev_iommu_ops(dev);839839+840840+ if (ops->is_attach_deferred)841841+ return ops->is_attach_deferred(dev);839842840843 return false;841844}···893894894895 mutex_lock(&group->mutex);895896 list_add_tail(&device->list, &group->devices);896896- if (group->domain && !iommu_is_attach_deferred(group->domain, dev))897897+ if (group->domain && !iommu_is_attach_deferred(dev))897898 ret = __iommu_attach_device(group->domain, dev);898899 mutex_unlock(&group->mutex);899900 if (ret)···12541255 struct iommu_fault_event *evt;12551256 struct iommu_fault_page_request *prm;12561257 struct dev_iommu *param = dev->iommu;12581258+ const struct iommu_ops *ops = dev_iommu_ops(dev);12571259 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;12581258- struct iommu_domain *domain = iommu_get_domain_for_dev(dev);1259126012601260- if (!domain || !domain->ops->page_response)12611261+ if (!ops->page_response)12611262 return -ENODEV;1262126312631264 if (!param || !param->fault_param)···12981299 msg->pasid = 0;12991300 }1300130113011301- ret = domain->ops->page_response(dev, evt, msg);13021302+ ret = ops->page_response(dev, evt, msg);13021303 list_del(&evt->list);13031304 kfree(evt);13041305 break;···1523152415241525static int iommu_get_def_domain_type(struct device *dev)15251526{15261526- const struct iommu_ops *ops = dev->bus->iommu_ops;15271527+ const struct iommu_ops *ops = dev_iommu_ops(dev);1527152815281529 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)15291530 return IOMMU_DOMAIN_DMA;···15821583 */15831584static struct iommu_group *iommu_group_get_for_dev(struct device *dev)15841585{15851585- const struct iommu_ops *ops = dev->bus->iommu_ops;15861586+ const struct iommu_ops *ops = dev_iommu_ops(dev);15861587 struct iommu_group *group;15871588 int ret;1588158915891590 group = iommu_group_get(dev);15901591 if (group)15911592 return group;15921592-15931593- if (!ops)15941594- return ERR_PTR(-EINVAL);1595159315961594 group = ops->device_group(dev);15971595 if (WARN_ON_ONCE(group == NULL))···17441748 struct iommu_domain *domain = data;17451749 int ret = 0;1746175017471747- if (!iommu_is_attach_deferred(domain, dev))17511751+ if (!iommu_is_attach_deferred(dev))17481752 ret = __iommu_attach_device(domain, dev);1749175317501754 return ret;···1758176217591763static int iommu_group_do_probe_finalize(struct device *dev, void *data)17601764{17611761- struct iommu_domain *domain = data;17651765+ const struct iommu_ops *ops = dev_iommu_ops(dev);1762176617631763- if (domain->ops->probe_finalize)17641764- domain->ops->probe_finalize(dev);17671767+ if (ops->probe_finalize)17681768+ ops->probe_finalize(dev);1765176917661770 return 0;17671771}···19501954 if (!domain)19511955 return NULL;1952195619531953- domain->ops = bus->iommu_ops;19541957 domain->type = type;19551958 /* Assume all sizes by default; the driver may override this later */19561956- domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;19591959+ domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;19601960+ if (!domain->ops)19611961+ domain->ops = bus->iommu_ops->default_domain_ops;1957196219581963 if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) {19591964 iommu_domain_free(domain);···19721975void iommu_domain_free(struct iommu_domain *domain)19731976{19741977 iommu_put_dma_cookie(domain);19751975- domain->ops->domain_free(domain);19781978+ domain->ops->free(domain);19761979}19771980EXPORT_SYMBOL_GPL(iommu_domain_free);19781981···2020202320212024int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)20222025{20232023- const struct iommu_ops *ops = domain->ops;20242024-20252025- if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))20262026+ if (iommu_is_attach_deferred(dev))20262027 return __iommu_attach_device(domain, dev);2027202820282029 return 0;20292030}2030203120312031-/*20322032- * Check flags and other user provided data for valid combinations. We also20332033- * make sure no reserved fields or unused flags are set. This is to ensure20342034- * not breaking userspace in the future when these fields or flags are used.20352035- */20362036-static int iommu_check_cache_invl_data(struct iommu_cache_invalidate_info *info)20372037-{20382038- u32 mask;20392039- int i;20402040-20412041- if (info->version != IOMMU_CACHE_INVALIDATE_INFO_VERSION_1)20422042- return -EINVAL;20432043-20442044- mask = (1 << IOMMU_CACHE_INV_TYPE_NR) - 1;20452045- if (info->cache & ~mask)20462046- return -EINVAL;20472047-20482048- if (info->granularity >= IOMMU_INV_GRANU_NR)20492049- return -EINVAL;20502050-20512051- switch (info->granularity) {20522052- case IOMMU_INV_GRANU_ADDR:20532053- if (info->cache & IOMMU_CACHE_INV_TYPE_PASID)20542054- return -EINVAL;20552055-20562056- mask = IOMMU_INV_ADDR_FLAGS_PASID |20572057- IOMMU_INV_ADDR_FLAGS_ARCHID |20582058- IOMMU_INV_ADDR_FLAGS_LEAF;20592059-20602060- if (info->granu.addr_info.flags & ~mask)20612061- return -EINVAL;20622062- break;20632063- case IOMMU_INV_GRANU_PASID:20642064- mask = IOMMU_INV_PASID_FLAGS_PASID |20652065- IOMMU_INV_PASID_FLAGS_ARCHID;20662066- if (info->granu.pasid_info.flags & ~mask)20672067- return -EINVAL;20682068-20692069- break;20702070- case IOMMU_INV_GRANU_DOMAIN:20712071- if (info->cache & IOMMU_CACHE_INV_TYPE_DEV_IOTLB)20722072- return -EINVAL;20732073- break;20742074- default:20752075- return -EINVAL;20762076- }20772077-20782078- /* Check reserved padding fields */20792079- for (i = 0; i < sizeof(info->padding); i++) {20802080- if (info->padding[i])20812081- return -EINVAL;20822082- }20832083-20842084- return 0;20852085-}20862086-20872087-int iommu_uapi_cache_invalidate(struct iommu_domain *domain, struct device *dev,20882088- void __user *uinfo)20892089-{20902090- struct iommu_cache_invalidate_info inv_info = { 0 };20912091- u32 minsz;20922092- int ret;20932093-20942094- if (unlikely(!domain->ops->cache_invalidate))20952095- return -ENODEV;20962096-20972097- /*20982098- * No new spaces can be added before the variable sized union, the20992099- * minimum size is the offset to the union.21002100- */21012101- minsz = offsetof(struct iommu_cache_invalidate_info, granu);21022102-21032103- /* Copy minsz from user to get flags and argsz */21042104- if (copy_from_user(&inv_info, uinfo, minsz))21052105- return -EFAULT;21062106-21072107- /* Fields before the variable size union are mandatory */21082108- if (inv_info.argsz < minsz)21092109- return -EINVAL;21102110-21112111- /* PASID and address granu require additional info beyond minsz */21122112- if (inv_info.granularity == IOMMU_INV_GRANU_PASID &&21132113- inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.pasid_info))21142114- return -EINVAL;21152115-21162116- if (inv_info.granularity == IOMMU_INV_GRANU_ADDR &&21172117- inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.addr_info))21182118- return -EINVAL;21192119-21202120- /*21212121- * User might be using a newer UAPI header which has a larger data21222122- * size, we shall support the existing flags within the current21232123- * size. Copy the remaining user data _after_ minsz but not more21242124- * than the current kernel supported size.21252125- */21262126- if (copy_from_user((void *)&inv_info + minsz, uinfo + minsz,21272127- min_t(u32, inv_info.argsz, sizeof(inv_info)) - minsz))21282128- return -EFAULT;21292129-21302130- /* Now the argsz is validated, check the content */21312131- ret = iommu_check_cache_invl_data(&inv_info);21322132- if (ret)21332133- return ret;21342134-21352135- return domain->ops->cache_invalidate(domain, dev, &inv_info);21362136-}21372137-EXPORT_SYMBOL_GPL(iommu_uapi_cache_invalidate);21382138-21392139-static int iommu_check_bind_data(struct iommu_gpasid_bind_data *data)21402140-{21412141- u64 mask;21422142- int i;21432143-21442144- if (data->version != IOMMU_GPASID_BIND_VERSION_1)21452145- return -EINVAL;21462146-21472147- /* Check the range of supported formats */21482148- if (data->format >= IOMMU_PASID_FORMAT_LAST)21492149- return -EINVAL;21502150-21512151- /* Check all flags */21522152- mask = IOMMU_SVA_GPASID_VAL;21532153- if (data->flags & ~mask)21542154- return -EINVAL;21552155-21562156- /* Check reserved padding fields */21572157- for (i = 0; i < sizeof(data->padding); i++) {21582158- if (data->padding[i])21592159- return -EINVAL;21602160- }21612161-21622162- return 0;21632163-}21642164-21652165-static int iommu_sva_prepare_bind_data(void __user *udata,21662166- struct iommu_gpasid_bind_data *data)21672167-{21682168- u32 minsz;21692169-21702170- /*21712171- * No new spaces can be added before the variable sized union, the21722172- * minimum size is the offset to the union.21732173- */21742174- minsz = offsetof(struct iommu_gpasid_bind_data, vendor);21752175-21762176- /* Copy minsz from user to get flags and argsz */21772177- if (copy_from_user(data, udata, minsz))21782178- return -EFAULT;21792179-21802180- /* Fields before the variable size union are mandatory */21812181- if (data->argsz < minsz)21822182- return -EINVAL;21832183- /*21842184- * User might be using a newer UAPI header, we shall let IOMMU vendor21852185- * driver decide on what size it needs. Since the guest PASID bind data21862186- * can be vendor specific, larger argsz could be the result of extension21872187- * for one vendor but it should not affect another vendor.21882188- * Copy the remaining user data _after_ minsz21892189- */21902190- if (copy_from_user((void *)data + minsz, udata + minsz,21912191- min_t(u32, data->argsz, sizeof(*data)) - minsz))21922192- return -EFAULT;21932193-21942194- return iommu_check_bind_data(data);21952195-}21962196-21972197-int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev,21982198- void __user *udata)21992199-{22002200- struct iommu_gpasid_bind_data data = { 0 };22012201- int ret;22022202-22032203- if (unlikely(!domain->ops->sva_bind_gpasid))22042204- return -ENODEV;22052205-22062206- ret = iommu_sva_prepare_bind_data(udata, &data);22072207- if (ret)22082208- return ret;22092209-22102210- return domain->ops->sva_bind_gpasid(domain, dev, &data);22112211-}22122212-EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid);22132213-22142214-int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,22152215- ioasid_t pasid)22162216-{22172217- if (unlikely(!domain->ops->sva_unbind_gpasid))22182218- return -ENODEV;22192219-22202220- return domain->ops->sva_unbind_gpasid(dev, pasid);22212221-}22222222-EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);22232223-22242224-int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,22252225- void __user *udata)22262226-{22272227- struct iommu_gpasid_bind_data data = { 0 };22282228- int ret;22292229-22302230- if (unlikely(!domain->ops->sva_bind_gpasid))22312231- return -ENODEV;22322232-22332233- ret = iommu_sva_prepare_bind_data(udata, &data);22342234- if (ret)22352235- return ret;22362236-22372237- return iommu_sva_unbind_gpasid(domain, dev, data.hpasid);22382238-}22392239-EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid);22402240-22412032static void __iommu_detach_device(struct iommu_domain *domain,22422033 struct device *dev)22432034{22442244- if (iommu_is_attach_deferred(domain, dev))20352035+ if (iommu_is_attach_deferred(dev))22452036 return;2246203722472038 if (unlikely(domain->ops->detach_dev == NULL))···22432458 phys_addr_t paddr, size_t size, int prot,22442459 gfp_t gfp, size_t *mapped)22452460{22462246- const struct iommu_ops *ops = domain->ops;24612461+ const struct iommu_domain_ops *ops = domain->ops;22472462 size_t pgsize, count;22482463 int ret;22492464···22662481static int __iommu_map(struct iommu_domain *domain, unsigned long iova,22672482 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)22682483{22692269- const struct iommu_ops *ops = domain->ops;24842484+ const struct iommu_domain_ops *ops = domain->ops;22702485 unsigned long orig_iova = iova;22712486 unsigned int min_pagesz;22722487 size_t orig_size = size;···23262541static int _iommu_map(struct iommu_domain *domain, unsigned long iova,23272542 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)23282543{23292329- const struct iommu_ops *ops = domain->ops;25442544+ const struct iommu_domain_ops *ops = domain->ops;23302545 int ret;2331254623322547 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);···23552570 unsigned long iova, size_t size,23562571 struct iommu_iotlb_gather *iotlb_gather)23572572{23582358- const struct iommu_ops *ops = domain->ops;25732573+ const struct iommu_domain_ops *ops = domain->ops;23592574 size_t pgsize, count;2360257523612576 pgsize = iommu_pgsize(domain, iova, iova, size, &count);···23682583 unsigned long iova, size_t size,23692584 struct iommu_iotlb_gather *iotlb_gather)23702585{23712371- const struct iommu_ops *ops = domain->ops;25862586+ const struct iommu_domain_ops *ops = domain->ops;23722587 size_t unmapped_page, unmapped = 0;23732588 unsigned long orig_iova = iova;23742589 unsigned int min_pagesz;···24442659 struct scatterlist *sg, unsigned int nents, int prot,24452660 gfp_t gfp)24462661{24472447- const struct iommu_ops *ops = domain->ops;26622662+ const struct iommu_domain_ops *ops = domain->ops;24482663 size_t len = 0, mapped = 0;24492664 phys_addr_t start;24502665 unsigned int i = 0;···2577279225782793void iommu_get_resv_regions(struct device *dev, struct list_head *list)25792794{25802580- const struct iommu_ops *ops = dev->bus->iommu_ops;27952795+ const struct iommu_ops *ops = dev_iommu_ops(dev);2581279625822582- if (ops && ops->get_resv_regions)27972797+ if (ops->get_resv_regions)25832798 ops->get_resv_regions(dev, list);25842799}2585280025862801void iommu_put_resv_regions(struct device *dev, struct list_head *list)25872802{25882588- const struct iommu_ops *ops = dev->bus->iommu_ops;28032803+ const struct iommu_ops *ops = dev_iommu_ops(dev);2589280425902590- if (ops && ops->put_resv_regions)28052805+ if (ops->put_resv_regions)25912806 ops->put_resv_regions(dev, list);25922807}25932808···2744295927452960/*27462961 * The device drivers should do the necessary cleanups before calling this.27472747- * For example, before disabling the aux-domain feature, the device driver27482748- * should detach all aux-domains. Otherwise, this will return -EBUSY.27492962 */27502963int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)27512964{···27712988}27722989EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);2773299027742774-/*27752775- * Aux-domain specific attach/detach.27762776- *27772777- * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns27782778- * true. Also, as long as domains are attached to a device through this27792779- * interface, any tries to call iommu_attach_device() should fail27802780- * (iommu_detach_device() can't fail, so we fail when trying to re-attach).27812781- * This should make us safe against a device being attached to a guest as a27822782- * whole while there are still pasid users on it (aux and sva).27832783- */27842784-int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)27852785-{27862786- int ret = -ENODEV;27872787-27882788- if (domain->ops->aux_attach_dev)27892789- ret = domain->ops->aux_attach_dev(domain, dev);27902790-27912791- if (!ret)27922792- trace_attach_device_to_domain(dev);27932793-27942794- return ret;27952795-}27962796-EXPORT_SYMBOL_GPL(iommu_aux_attach_device);27972797-27982798-void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)27992799-{28002800- if (domain->ops->aux_detach_dev) {28012801- domain->ops->aux_detach_dev(domain, dev);28022802- trace_detach_device_from_domain(dev);28032803- }28042804-}28052805-EXPORT_SYMBOL_GPL(iommu_aux_detach_device);28062806-28072807-int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)28082808-{28092809- int ret = -ENODEV;28102810-28112811- if (domain->ops->aux_get_pasid)28122812- ret = domain->ops->aux_get_pasid(domain, dev);28132813-28142814- return ret;28152815-}28162816-EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);28172817-28182991/**28192992 * iommu_sva_bind_device() - Bind a process address space to a device28202993 * @dev: the device···27923053{27933054 struct iommu_group *group;27943055 struct iommu_sva *handle = ERR_PTR(-EINVAL);27952795- const struct iommu_ops *ops = dev->bus->iommu_ops;30563056+ const struct iommu_ops *ops = dev_iommu_ops(dev);2796305727972797- if (!ops || !ops->sva_bind)30583058+ if (!ops->sva_bind)27983059 return ERR_PTR(-ENODEV);2799306028003061 group = iommu_group_get(dev);···28353096{28363097 struct iommu_group *group;28373098 struct device *dev = handle->dev;28382838- const struct iommu_ops *ops = dev->bus->iommu_ops;30993099+ const struct iommu_ops *ops = dev_iommu_ops(dev);2839310028402840- if (!ops || !ops->sva_unbind)31013101+ if (!ops->sva_unbind)28413102 return;2842310328433104 group = iommu_group_get(dev);···2854311528553116u32 iommu_sva_get_pasid(struct iommu_sva *handle)28563117{28572857- const struct iommu_ops *ops = handle->dev->bus->iommu_ops;31183118+ const struct iommu_ops *ops = dev_iommu_ops(handle->dev);2858311928592859- if (!ops || !ops->sva_get_pasid)31203120+ if (!ops->sva_get_pasid)28603121 return IOMMU_PASID_INVALID;2861312228623123 return ops->sva_get_pasid(handle);
+62-16
drivers/iommu/iova.c
···1515/* The anchor node sits above the top of the usable address space */1616#define IOVA_ANCHOR ~0UL17171818+#define IOVA_RANGE_CACHE_MAX_SIZE 6 /* log of max cached IOVA range size (in pages) */1919+1820static bool iova_rcache_insert(struct iova_domain *iovad,1921 unsigned long pfn,2022 unsigned long size);2123static unsigned long iova_rcache_get(struct iova_domain *iovad,2224 unsigned long size,2325 unsigned long limit_pfn);2424-static void init_iova_rcaches(struct iova_domain *iovad);2526static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);2627static void free_iova_rcaches(struct iova_domain *iovad);2728···6564 iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;6665 rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);6766 rb_insert_color(&iovad->anchor.node, &iovad->rbroot);6868- cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD, &iovad->cpuhp_dead);6969- init_iova_rcaches(iovad);7067}7168EXPORT_SYMBOL_GPL(init_iova_domain);7269···9495 cached_iova = to_iova(iovad->cached32_node);9596 if (free == cached_iova ||9697 (free->pfn_hi < iovad->dma_32bit_pfn &&9797- free->pfn_lo >= cached_iova->pfn_lo)) {9898+ free->pfn_lo >= cached_iova->pfn_lo))9899 iovad->cached32_node = rb_next(&free->node);100100+101101+ if (free->pfn_lo < iovad->dma_32bit_pfn)99102 iovad->max32_alloc_size = iovad->dma_32bit_pfn;100100- }101103102104 cached_iova = to_iova(iovad->cached_node);103105 if (free->pfn_lo >= cached_iova->pfn_lo)···488488}489489EXPORT_SYMBOL_GPL(free_iova_fast);490490491491+static void iova_domain_free_rcaches(struct iova_domain *iovad)492492+{493493+ cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,494494+ &iovad->cpuhp_dead);495495+ free_iova_rcaches(iovad);496496+}497497+491498/**492499 * put_iova_domain - destroys the iova domain493500 * @iovad: - iova domain in question.···504497{505498 struct iova *iova, *tmp;506499507507- cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,508508- &iovad->cpuhp_dead);509509- free_iova_rcaches(iovad);500500+ if (iovad->rcaches)501501+ iova_domain_free_rcaches(iovad);502502+510503 rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)511504 free_iova_mem(iova);512505}···615608 */616609617610#define IOVA_MAG_SIZE 128611611+#define MAX_GLOBAL_MAGS 32 /* magazines per bin */618612619613struct iova_magazine {620614 unsigned long size;···626618 spinlock_t lock;627619 struct iova_magazine *loaded;628620 struct iova_magazine *prev;621621+};622622+623623+struct iova_rcache {624624+ spinlock_t lock;625625+ unsigned long depot_size;626626+ struct iova_magazine *depot[MAX_GLOBAL_MAGS];627627+ struct iova_cpu_rcache __percpu *cpu_rcaches;629628};630629631630static struct iova_magazine *iova_magazine_alloc(gfp_t flags)···708693 mag->pfns[mag->size++] = pfn;709694}710695711711-static void init_iova_rcaches(struct iova_domain *iovad)696696+int iova_domain_init_rcaches(struct iova_domain *iovad)712697{713713- struct iova_cpu_rcache *cpu_rcache;714714- struct iova_rcache *rcache;715698 unsigned int cpu;716716- int i;699699+ int i, ret;700700+701701+ iovad->rcaches = kcalloc(IOVA_RANGE_CACHE_MAX_SIZE,702702+ sizeof(struct iova_rcache),703703+ GFP_KERNEL);704704+ if (!iovad->rcaches)705705+ return -ENOMEM;717706718707 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {708708+ struct iova_cpu_rcache *cpu_rcache;709709+ struct iova_rcache *rcache;710710+719711 rcache = &iovad->rcaches[i];720712 spin_lock_init(&rcache->lock);721713 rcache->depot_size = 0;722722- rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size());723723- if (WARN_ON(!rcache->cpu_rcaches))724724- continue;714714+ rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache),715715+ cache_line_size());716716+ if (!rcache->cpu_rcaches) {717717+ ret = -ENOMEM;718718+ goto out_err;719719+ }725720 for_each_possible_cpu(cpu) {726721 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);722722+727723 spin_lock_init(&cpu_rcache->lock);728724 cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);729725 cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);726726+ if (!cpu_rcache->loaded || !cpu_rcache->prev) {727727+ ret = -ENOMEM;728728+ goto out_err;729729+ }730730 }731731 }732732+733733+ ret = cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,734734+ &iovad->cpuhp_dead);735735+ if (ret)736736+ goto out_err;737737+ return 0;738738+739739+out_err:740740+ free_iova_rcaches(iovad);741741+ return ret;732742}743743+EXPORT_SYMBOL_GPL(iova_domain_init_rcaches);733744734745/*735746 * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and···872831{873832 unsigned int log_size = order_base_2(size);874833875875- if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)834834+ if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE || !iovad->rcaches)876835 return 0;877836878837 return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);···890849891850 for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {892851 rcache = &iovad->rcaches[i];852852+ if (!rcache->cpu_rcaches)853853+ break;893854 for_each_possible_cpu(cpu) {894855 cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);895856 iova_magazine_free(cpu_rcache->loaded);···901858 for (j = 0; j < rcache->depot_size; ++j)902859 iova_magazine_free(rcache->depot[j]);903860 }861861+862862+ kfree(iovad->rcaches);863863+ iovad->rcaches = NULL;904864}905865906866/*
···480480 struct file *file;481481 struct vduse_bounce_map *map;482482 unsigned long pfn, bounce_pfns;483483+ int ret;483484484485 bounce_pfns = PAGE_ALIGN(bounce_size) >> PAGE_SHIFT;485486 if (iova_limit <= bounce_size)···514513 spin_lock_init(&domain->iotlb_lock);515514 init_iova_domain(&domain->stream_iovad,516515 PAGE_SIZE, IOVA_START_PFN);516516+ ret = iova_domain_init_rcaches(&domain->stream_iovad);517517+ if (ret)518518+ goto err_iovad_stream;517519 init_iova_domain(&domain->consistent_iovad,518520 PAGE_SIZE, bounce_pfns);521521+ ret = iova_domain_init_rcaches(&domain->consistent_iovad);522522+ if (ret)523523+ goto err_iovad_consistent;519524520525 return domain;526526+err_iovad_consistent:527527+ put_iova_domain(&domain->stream_iovad);528528+err_iovad_stream:529529+ fput(file);521530err_file:522531 vfree(domain->bounce_maps);523532err_map:
-27
include/linux/intel-iommu.h
···525525 */526526#define DOMAIN_FLAG_USE_FIRST_LEVEL BIT(1)527527528528-/*529529- * Domain represents a virtual machine which demands iommu nested530530- * translation mode support.531531- */532532-#define DOMAIN_FLAG_NESTING_MODE BIT(2)533533-534528struct dmar_domain {535529 int nid; /* node id */536530···542548 u8 iommu_snooping: 1; /* indicate snooping control feature */543549544550 struct list_head devices; /* all devices' list */545545- struct list_head subdevices; /* all subdevices' list */546551 struct iova_domain iovad; /* iova's that belong to this domain */547552548553 struct dma_pte *pgd; /* virtual address */···555562 0 == 4KiB (no superpages), 1 == 2MiB,556563 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */557564 u64 max_addr; /* maximum mapped address */558558-559559- u32 default_pasid; /*560560- * The default pasid used for non-SVM561561- * traffic on mediated devices.562562- */563565564566 struct iommu_domain domain; /* generic domain data structure for565567 iommu core */···608620 void *perf_statistic;609621};610622611611-/* Per subdevice private data */612612-struct subdev_domain_info {613613- struct list_head link_phys; /* link to phys device siblings */614614- struct list_head link_domain; /* link to domain siblings */615615- struct device *pdev; /* physical device derived from */616616- struct dmar_domain *domain; /* aux-domain */617617- int users; /* user count */618618-};619619-620623/* PCI domain-device relationship */621624struct device_domain_info {622625 struct list_head link; /* link to domain siblings */623626 struct list_head global; /* link to global list */624627 struct list_head table; /* link to pasid table */625625- struct list_head subdevices; /* subdevices sibling */626628 u32 segment; /* PCI segment number */627629 u8 bus; /* PCI bus number */628630 u8 devfn; /* PCI devfn number */···623645 u8 pri_enabled:1;624646 u8 ats_supported:1;625647 u8 ats_enabled:1;626626- u8 auxd_enabled:1; /* Multiple domains per device */627648 u8 ats_qdep;628649 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */629650 struct intel_iommu *iommu; /* IOMMU used by this device */···742765extern void intel_svm_check(struct intel_iommu *iommu);743766extern int intel_svm_enable_prq(struct intel_iommu *iommu);744767extern int intel_svm_finish_prq(struct intel_iommu *iommu);745745-int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,746746- struct iommu_gpasid_bind_data *data);747747-int intel_svm_unbind_gpasid(struct device *dev, u32 pasid);748768struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm,749769 void *drvdata);750770void intel_svm_unbind(struct iommu_sva *handle);···769795770796 unsigned int flags;771797 u32 pasid;772772- int gpasid; /* In case that guest PASID is different from host PASID */773798 struct list_head devs;774799};775800#else
-12
include/linux/intel-svm.h
···2525 * do such IOTLB flushes automatically.2626 */2727#define SVM_FLAG_SUPERVISOR_MODE BIT(0)2828-/*2929- * The SVM_FLAG_GUEST_MODE flag is used when a PASID bind is for guest3030- * processes. Compared to the host bind, the primary differences are:3131- * 1. mm life cycle management3232- * 2. fault reporting3333- */3434-#define SVM_FLAG_GUEST_MODE BIT(1)3535-/*3636- * The SVM_FLAG_GUEST_PASID flag is used when a guest has its own PASID space,3737- * which requires guest and host PASID translation at both directions.3838- */3939-#define SVM_FLAG_GUEST_PASID BIT(2)40284129#endif /* __INTEL_SVM_H__ */
+92-143
include/linux/iommu.h
···3737struct bus_type;3838struct device;3939struct iommu_domain;4040+struct iommu_domain_ops;4041struct notifier_block;4142struct iommu_sva;4243struct iommu_fault_event;···89889089struct iommu_domain {9190 unsigned type;9292- const struct iommu_ops *ops;9191+ const struct iommu_domain_ops *ops;9392 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */9493 iommu_fault_handler_t handler;9594 void *handler_token;···145144146145/**147146 * enum iommu_dev_features - Per device IOMMU features148148- * @IOMMU_DEV_FEAT_AUX: Auxiliary domain feature149147 * @IOMMU_DEV_FEAT_SVA: Shared Virtual Addresses150148 * @IOMMU_DEV_FEAT_IOPF: I/O Page Faults such as PRI or Stall. Generally151149 * enabling %IOMMU_DEV_FEAT_SVA requires···157157 * iommu_dev_has_feature(), and enable it using iommu_dev_enable_feature().158158 */159159enum iommu_dev_features {160160- IOMMU_DEV_FEAT_AUX,161160 IOMMU_DEV_FEAT_SVA,162161 IOMMU_DEV_FEAT_IOPF,163162};···193194 * struct iommu_ops - iommu ops and capabilities194195 * @capable: check capability195196 * @domain_alloc: allocate iommu domain196196- * @domain_free: free iommu domain197197- * @attach_dev: attach device to an iommu domain198198- * @detach_dev: detach device from an iommu domain197197+ * @probe_device: Add device to iommu driver handling198198+ * @release_device: Remove device from iommu driver handling199199+ * @probe_finalize: Do final setup work after the device is added to an IOMMU200200+ * group and attached to the groups domain201201+ * @device_group: find iommu group for a particular device202202+ * @get_resv_regions: Request list of reserved regions for a device203203+ * @put_resv_regions: Free list of reserved regions for a device204204+ * @of_xlate: add OF master IDs to iommu grouping205205+ * @is_attach_deferred: Check if domain attach should be deferred from iommu206206+ * driver init to device driver init (default no)207207+ * @dev_has/enable/disable_feat: per device entries to check/enable/disable208208+ * iommu specific features.209209+ * @dev_feat_enabled: check enabled feature210210+ * @sva_bind: Bind process address space to device211211+ * @sva_unbind: Unbind process address space from device212212+ * @sva_get_pasid: Get PASID associated to a SVA handle213213+ * @page_response: handle page request response214214+ * @def_domain_type: device default domain type, return value:215215+ * - IOMMU_DOMAIN_IDENTITY: must use an identity domain216216+ * - IOMMU_DOMAIN_DMA: must use a dma domain217217+ * - 0: use the default setting218218+ * @default_domain_ops: the default ops for domains219219+ * @pgsize_bitmap: bitmap of all possible supported page sizes220220+ * @owner: Driver module providing these ops221221+ */222222+struct iommu_ops {223223+ bool (*capable)(enum iommu_cap);224224+225225+ /* Domain allocation and freeing by the iommu driver */226226+ struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);227227+228228+ struct iommu_device *(*probe_device)(struct device *dev);229229+ void (*release_device)(struct device *dev);230230+ void (*probe_finalize)(struct device *dev);231231+ struct iommu_group *(*device_group)(struct device *dev);232232+233233+ /* Request/Free a list of reserved regions for a device */234234+ void (*get_resv_regions)(struct device *dev, struct list_head *list);235235+ void (*put_resv_regions)(struct device *dev, struct list_head *list);236236+237237+ int (*of_xlate)(struct device *dev, struct of_phandle_args *args);238238+ bool (*is_attach_deferred)(struct device *dev);239239+240240+ /* Per device IOMMU features */241241+ bool (*dev_has_feat)(struct device *dev, enum iommu_dev_features f);242242+ bool (*dev_feat_enabled)(struct device *dev, enum iommu_dev_features f);243243+ int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);244244+ int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);245245+246246+ struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm,247247+ void *drvdata);248248+ void (*sva_unbind)(struct iommu_sva *handle);249249+ u32 (*sva_get_pasid)(struct iommu_sva *handle);250250+251251+ int (*page_response)(struct device *dev,252252+ struct iommu_fault_event *evt,253253+ struct iommu_page_response *msg);254254+255255+ int (*def_domain_type)(struct device *dev);256256+257257+ const struct iommu_domain_ops *default_domain_ops;258258+ unsigned long pgsize_bitmap;259259+ struct module *owner;260260+};261261+262262+/**263263+ * struct iommu_domain_ops - domain specific operations264264+ * @attach_dev: attach an iommu domain to a device265265+ * @detach_dev: detach an iommu domain from a device199266 * @map: map a physically contiguous memory region to an iommu domain200267 * @map_pages: map a physically contiguous set of pages of the same size to201268 * an iommu domain.···272207 * @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush273208 * queue274209 * @iova_to_phys: translate iova to physical address275275- * @probe_device: Add device to iommu driver handling276276- * @release_device: Remove device from iommu driver handling277277- * @probe_finalize: Do final setup work after the device is added to an IOMMU278278- * group and attached to the groups domain279279- * @device_group: find iommu group for a particular device280210 * @enable_nesting: Enable nesting281211 * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*)282282- * @get_resv_regions: Request list of reserved regions for a device283283- * @put_resv_regions: Free list of reserved regions for a device284284- * @apply_resv_region: Temporary helper call-back for iova reserved ranges285285- * @of_xlate: add OF master IDs to iommu grouping286286- * @is_attach_deferred: Check if domain attach should be deferred from iommu287287- * driver init to device driver init (default no)288288- * @dev_has/enable/disable_feat: per device entries to check/enable/disable289289- * iommu specific features.290290- * @dev_feat_enabled: check enabled feature291291- * @aux_attach/detach_dev: aux-domain specific attach/detach entries.292292- * @aux_get_pasid: get the pasid given an aux-domain293293- * @sva_bind: Bind process address space to device294294- * @sva_unbind: Unbind process address space from device295295- * @sva_get_pasid: Get PASID associated to a SVA handle296296- * @page_response: handle page request response297297- * @cache_invalidate: invalidate translation caches298298- * @sva_bind_gpasid: bind guest pasid and mm299299- * @sva_unbind_gpasid: unbind guest pasid and mm300300- * @def_domain_type: device default domain type, return value:301301- * - IOMMU_DOMAIN_IDENTITY: must use an identity domain302302- * - IOMMU_DOMAIN_DMA: must use a dma domain303303- * - 0: use the default setting304304- * @pgsize_bitmap: bitmap of all possible supported page sizes305305- * @owner: Driver module providing these ops212212+ * @free: Release the domain after use.306213 */307307-struct iommu_ops {308308- bool (*capable)(enum iommu_cap);309309-310310- /* Domain allocation and freeing by the iommu driver */311311- struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);312312- void (*domain_free)(struct iommu_domain *);313313-214214+struct iommu_domain_ops {314215 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);315216 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);217217+316218 int (*map)(struct iommu_domain *domain, unsigned long iova,317219 phys_addr_t paddr, size_t size, int prot, gfp_t gfp);318220 int (*map_pages)(struct iommu_domain *domain, unsigned long iova,319221 phys_addr_t paddr, size_t pgsize, size_t pgcount,320222 int prot, gfp_t gfp, size_t *mapped);321223 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,322322- size_t size, struct iommu_iotlb_gather *iotlb_gather);224224+ size_t size, struct iommu_iotlb_gather *iotlb_gather);323225 size_t (*unmap_pages)(struct iommu_domain *domain, unsigned long iova,324226 size_t pgsize, size_t pgcount,325227 struct iommu_iotlb_gather *iotlb_gather);228228+326229 void (*flush_iotlb_all)(struct iommu_domain *domain);327230 void (*iotlb_sync_map)(struct iommu_domain *domain, unsigned long iova,328231 size_t size);329232 void (*iotlb_sync)(struct iommu_domain *domain,330233 struct iommu_iotlb_gather *iotlb_gather);331331- phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);332332- struct iommu_device *(*probe_device)(struct device *dev);333333- void (*release_device)(struct device *dev);334334- void (*probe_finalize)(struct device *dev);335335- struct iommu_group *(*device_group)(struct device *dev);234234+235235+ phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,236236+ dma_addr_t iova);237237+336238 int (*enable_nesting)(struct iommu_domain *domain);337239 int (*set_pgtable_quirks)(struct iommu_domain *domain,338240 unsigned long quirks);339241340340- /* Request/Free a list of reserved regions for a device */341341- void (*get_resv_regions)(struct device *dev, struct list_head *list);342342- void (*put_resv_regions)(struct device *dev, struct list_head *list);343343- void (*apply_resv_region)(struct device *dev,344344- struct iommu_domain *domain,345345- struct iommu_resv_region *region);346346-347347- int (*of_xlate)(struct device *dev, struct of_phandle_args *args);348348- bool (*is_attach_deferred)(struct iommu_domain *domain, struct device *dev);349349-350350- /* Per device IOMMU features */351351- bool (*dev_has_feat)(struct device *dev, enum iommu_dev_features f);352352- bool (*dev_feat_enabled)(struct device *dev, enum iommu_dev_features f);353353- int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);354354- int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);355355-356356- /* Aux-domain specific attach/detach entries */357357- int (*aux_attach_dev)(struct iommu_domain *domain, struct device *dev);358358- void (*aux_detach_dev)(struct iommu_domain *domain, struct device *dev);359359- int (*aux_get_pasid)(struct iommu_domain *domain, struct device *dev);360360-361361- struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm,362362- void *drvdata);363363- void (*sva_unbind)(struct iommu_sva *handle);364364- u32 (*sva_get_pasid)(struct iommu_sva *handle);365365-366366- int (*page_response)(struct device *dev,367367- struct iommu_fault_event *evt,368368- struct iommu_page_response *msg);369369- int (*cache_invalidate)(struct iommu_domain *domain, struct device *dev,370370- struct iommu_cache_invalidate_info *inv_info);371371- int (*sva_bind_gpasid)(struct iommu_domain *domain,372372- struct device *dev, struct iommu_gpasid_bind_data *data);373373-374374- int (*sva_unbind_gpasid)(struct device *dev, u32 pasid);375375-376376- int (*def_domain_type)(struct device *dev);377377-378378- unsigned long pgsize_bitmap;379379- struct module *owner;242242+ void (*free)(struct iommu_domain *domain);380243};381244382245/**···396403 };397404}398405406406+static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)407407+{408408+ /*409409+ * Assume that valid ops must be installed if iommu_probe_device()410410+ * has succeeded. The device ops are essentially for internal use411411+ * within the IOMMU subsystem itself, so we should be able to trust412412+ * ourselves not to misuse the helper.413413+ */414414+ return dev->iommu->iommu_dev->ops;415415+}416416+399417#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */400418#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2 /* Pre Device removed */401419#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3 /* Pre Driver bind */···425421 struct device *dev);426422extern void iommu_detach_device(struct iommu_domain *domain,427423 struct device *dev);428428-extern int iommu_uapi_cache_invalidate(struct iommu_domain *domain,429429- struct device *dev,430430- void __user *uinfo);431431-432432-extern int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain,433433- struct device *dev, void __user *udata);434434-extern int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain,435435- struct device *dev, void __user *udata);436424extern int iommu_sva_unbind_gpasid(struct iommu_domain *domain,437425 struct device *dev, ioasid_t pasid);438426extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);···668672int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);669673int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);670674bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features f);671671-int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev);672672-void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev);673673-int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev);674675675676struct iommu_sva *iommu_sva_bind_device(struct device *dev,676677 struct mm_struct *mm,···10121019 return -ENODEV;10131020}1014102110151015-static inline int10161016-iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)10171017-{10181018- return -ENODEV;10191019-}10201020-10211021-static inline void10221022-iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)10231023-{10241024-}10251025-10261026-static inline int10271027-iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)10281028-{10291029- return -ENODEV;10301030-}10311031-10321022static inline struct iommu_sva *10331023iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)10341024{···10251049static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)10261050{10271051 return IOMMU_PASID_INVALID;10281028-}10291029-10301030-static inline int10311031-iommu_uapi_cache_invalidate(struct iommu_domain *domain,10321032- struct device *dev,10331033- struct iommu_cache_invalidate_info *inv_info)10341034-{10351035- return -ENODEV;10361036-}10371037-10381038-static inline int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain,10391039- struct device *dev, void __user *udata)10401040-{10411041- return -ENODEV;10421042-}10431043-10441044-static inline int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain,10451045- struct device *dev, void __user *udata)10461046-{10471047- return -ENODEV;10481048-}10491049-10501050-static inline int iommu_sva_unbind_gpasid(struct iommu_domain *domain,10511051- struct device *dev,10521052- ioasid_t pasid)10531053-{10541054- return -ENODEV;10551052}1056105310571054static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
+3-12
include/linux/iova.h
···2121 unsigned long pfn_lo; /* Lowest allocated pfn */2222};23232424-struct iova_magazine;2525-struct iova_cpu_rcache;26242727-#define IOVA_RANGE_CACHE_MAX_SIZE 6 /* log of max cached IOVA range size (in pages) */2828-#define MAX_GLOBAL_MAGS 32 /* magazines per bin */2929-3030-struct iova_rcache {3131- spinlock_t lock;3232- unsigned long depot_size;3333- struct iova_magazine *depot[MAX_GLOBAL_MAGS];3434- struct iova_cpu_rcache __percpu *cpu_rcaches;3535-};2525+struct iova_rcache;36263727/* holds all the iova translations for a domain */3828struct iova_domain {···3646 unsigned long max32_alloc_size; /* Size of last failed allocation */3747 struct iova anchor; /* rbtree lookup anchor */38483939- struct iova_rcache rcaches[IOVA_RANGE_CACHE_MAX_SIZE]; /* IOVA range caches */4949+ struct iova_rcache *rcaches;4050 struct hlist_node cpuhp_dead;4151};4252···92102 unsigned long pfn_hi);93103void init_iova_domain(struct iova_domain *iovad, unsigned long granule,94104 unsigned long start_pfn);105105+int iova_domain_init_rcaches(struct iova_domain *iovad);95106struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn);96107void put_iova_domain(struct iova_domain *iovad);97108#else
-181
include/uapi/linux/iommu.h
···158158 __u32 code;159159};160160161161-/* defines the granularity of the invalidation */162162-enum iommu_inv_granularity {163163- IOMMU_INV_GRANU_DOMAIN, /* domain-selective invalidation */164164- IOMMU_INV_GRANU_PASID, /* PASID-selective invalidation */165165- IOMMU_INV_GRANU_ADDR, /* page-selective invalidation */166166- IOMMU_INV_GRANU_NR, /* number of invalidation granularities */167167-};168168-169169-/**170170- * struct iommu_inv_addr_info - Address Selective Invalidation Structure171171- *172172- * @flags: indicates the granularity of the address-selective invalidation173173- * - If the PASID bit is set, the @pasid field is populated and the invalidation174174- * relates to cache entries tagged with this PASID and matching the address175175- * range.176176- * - If ARCHID bit is set, @archid is populated and the invalidation relates177177- * to cache entries tagged with this architecture specific ID and matching178178- * the address range.179179- * - Both PASID and ARCHID can be set as they may tag different caches.180180- * - If neither PASID or ARCHID is set, global addr invalidation applies.181181- * - The LEAF flag indicates whether only the leaf PTE caching needs to be182182- * invalidated and other paging structure caches can be preserved.183183- * @pasid: process address space ID184184- * @archid: architecture-specific ID185185- * @addr: first stage/level input address186186- * @granule_size: page/block size of the mapping in bytes187187- * @nb_granules: number of contiguous granules to be invalidated188188- */189189-struct iommu_inv_addr_info {190190-#define IOMMU_INV_ADDR_FLAGS_PASID (1 << 0)191191-#define IOMMU_INV_ADDR_FLAGS_ARCHID (1 << 1)192192-#define IOMMU_INV_ADDR_FLAGS_LEAF (1 << 2)193193- __u32 flags;194194- __u32 archid;195195- __u64 pasid;196196- __u64 addr;197197- __u64 granule_size;198198- __u64 nb_granules;199199-};200200-201201-/**202202- * struct iommu_inv_pasid_info - PASID Selective Invalidation Structure203203- *204204- * @flags: indicates the granularity of the PASID-selective invalidation205205- * - If the PASID bit is set, the @pasid field is populated and the invalidation206206- * relates to cache entries tagged with this PASID and matching the address207207- * range.208208- * - If the ARCHID bit is set, the @archid is populated and the invalidation209209- * relates to cache entries tagged with this architecture specific ID and210210- * matching the address range.211211- * - Both PASID and ARCHID can be set as they may tag different caches.212212- * - At least one of PASID or ARCHID must be set.213213- * @pasid: process address space ID214214- * @archid: architecture-specific ID215215- */216216-struct iommu_inv_pasid_info {217217-#define IOMMU_INV_PASID_FLAGS_PASID (1 << 0)218218-#define IOMMU_INV_PASID_FLAGS_ARCHID (1 << 1)219219- __u32 flags;220220- __u32 archid;221221- __u64 pasid;222222-};223223-224224-/**225225- * struct iommu_cache_invalidate_info - First level/stage invalidation226226- * information227227- * @argsz: User filled size of this data228228- * @version: API version of this structure229229- * @cache: bitfield that allows to select which caches to invalidate230230- * @granularity: defines the lowest granularity used for the invalidation:231231- * domain > PASID > addr232232- * @padding: reserved for future use (should be zero)233233- * @pasid_info: invalidation data when @granularity is %IOMMU_INV_GRANU_PASID234234- * @addr_info: invalidation data when @granularity is %IOMMU_INV_GRANU_ADDR235235- *236236- * Not all the combinations of cache/granularity are valid:237237- *238238- * +--------------+---------------+---------------+---------------+239239- * | type / | DEV_IOTLB | IOTLB | PASID |240240- * | granularity | | | cache |241241- * +==============+===============+===============+===============+242242- * | DOMAIN | N/A | Y | Y |243243- * +--------------+---------------+---------------+---------------+244244- * | PASID | Y | Y | Y |245245- * +--------------+---------------+---------------+---------------+246246- * | ADDR | Y | Y | N/A |247247- * +--------------+---------------+---------------+---------------+248248- *249249- * Invalidations by %IOMMU_INV_GRANU_DOMAIN don't take any argument other than250250- * @version and @cache.251251- *252252- * If multiple cache types are invalidated simultaneously, they all253253- * must support the used granularity.254254- */255255-struct iommu_cache_invalidate_info {256256- __u32 argsz;257257-#define IOMMU_CACHE_INVALIDATE_INFO_VERSION_1 1258258- __u32 version;259259-/* IOMMU paging structure cache */260260-#define IOMMU_CACHE_INV_TYPE_IOTLB (1 << 0) /* IOMMU IOTLB */261261-#define IOMMU_CACHE_INV_TYPE_DEV_IOTLB (1 << 1) /* Device IOTLB */262262-#define IOMMU_CACHE_INV_TYPE_PASID (1 << 2) /* PASID cache */263263-#define IOMMU_CACHE_INV_TYPE_NR (3)264264- __u8 cache;265265- __u8 granularity;266266- __u8 padding[6];267267- union {268268- struct iommu_inv_pasid_info pasid_info;269269- struct iommu_inv_addr_info addr_info;270270- } granu;271271-};272272-273273-/**274274- * struct iommu_gpasid_bind_data_vtd - Intel VT-d specific data on device and guest275275- * SVA binding.276276- *277277- * @flags: VT-d PASID table entry attributes278278- * @pat: Page attribute table data to compute effective memory type279279- * @emt: Extended memory type280280- *281281- * Only guest vIOMMU selectable and effective options are passed down to282282- * the host IOMMU.283283- */284284-struct iommu_gpasid_bind_data_vtd {285285-#define IOMMU_SVA_VTD_GPASID_SRE (1 << 0) /* supervisor request */286286-#define IOMMU_SVA_VTD_GPASID_EAFE (1 << 1) /* extended access enable */287287-#define IOMMU_SVA_VTD_GPASID_PCD (1 << 2) /* page-level cache disable */288288-#define IOMMU_SVA_VTD_GPASID_PWT (1 << 3) /* page-level write through */289289-#define IOMMU_SVA_VTD_GPASID_EMTE (1 << 4) /* extended mem type enable */290290-#define IOMMU_SVA_VTD_GPASID_CD (1 << 5) /* PASID-level cache disable */291291-#define IOMMU_SVA_VTD_GPASID_WPE (1 << 6) /* Write protect enable */292292-#define IOMMU_SVA_VTD_GPASID_LAST (1 << 7)293293- __u64 flags;294294- __u32 pat;295295- __u32 emt;296296-};297297-298298-#define IOMMU_SVA_VTD_GPASID_MTS_MASK (IOMMU_SVA_VTD_GPASID_CD | \299299- IOMMU_SVA_VTD_GPASID_EMTE | \300300- IOMMU_SVA_VTD_GPASID_PCD | \301301- IOMMU_SVA_VTD_GPASID_PWT)302302-303303-/**304304- * struct iommu_gpasid_bind_data - Information about device and guest PASID binding305305- * @argsz: User filled size of this data306306- * @version: Version of this data structure307307- * @format: PASID table entry format308308- * @flags: Additional information on guest bind request309309- * @gpgd: Guest page directory base of the guest mm to bind310310- * @hpasid: Process address space ID used for the guest mm in host IOMMU311311- * @gpasid: Process address space ID used for the guest mm in guest IOMMU312312- * @addr_width: Guest virtual address width313313- * @padding: Reserved for future use (should be zero)314314- * @vtd: Intel VT-d specific data315315- *316316- * Guest to host PASID mapping can be an identity or non-identity, where guest317317- * has its own PASID space. For non-identify mapping, guest to host PASID lookup318318- * is needed when VM programs guest PASID into an assigned device. VMM may319319- * trap such PASID programming then request host IOMMU driver to convert guest320320- * PASID to host PASID based on this bind data.321321- */322322-struct iommu_gpasid_bind_data {323323- __u32 argsz;324324-#define IOMMU_GPASID_BIND_VERSION_1 1325325- __u32 version;326326-#define IOMMU_PASID_FORMAT_INTEL_VTD 1327327-#define IOMMU_PASID_FORMAT_LAST 2328328- __u32 format;329329- __u32 addr_width;330330-#define IOMMU_SVA_GPASID_VAL (1 << 0) /* guest PASID valid */331331- __u64 flags;332332- __u64 gpgd;333333- __u64 hpasid;334334- __u64 gpasid;335335- __u8 padding[8];336336- /* Vendor specific data */337337- union {338338- struct iommu_gpasid_bind_data_vtd vtd;339339- } vendor;340340-};341341-342161#endif /* _UAPI_IOMMU_H */