1672cf6dfSJoerg Roedel // SPDX-License-Identifier: GPL-2.0-only 2672cf6dfSJoerg Roedel /* 3672cf6dfSJoerg Roedel * Copyright © 2015 Intel Corporation. 4672cf6dfSJoerg Roedel * 5672cf6dfSJoerg Roedel * Authors: David Woodhouse <dwmw2@infradead.org> 6672cf6dfSJoerg Roedel */ 7672cf6dfSJoerg Roedel 8672cf6dfSJoerg Roedel #include <linux/intel-iommu.h> 9672cf6dfSJoerg Roedel #include <linux/mmu_notifier.h> 10672cf6dfSJoerg Roedel #include <linux/sched.h> 11672cf6dfSJoerg Roedel #include <linux/sched/mm.h> 12672cf6dfSJoerg Roedel #include <linux/slab.h> 13672cf6dfSJoerg Roedel #include <linux/intel-svm.h> 14672cf6dfSJoerg Roedel #include <linux/rculist.h> 15672cf6dfSJoerg Roedel #include <linux/pci.h> 16672cf6dfSJoerg Roedel #include <linux/pci-ats.h> 17672cf6dfSJoerg Roedel #include <linux/dmar.h> 18672cf6dfSJoerg Roedel #include <linux/interrupt.h> 19672cf6dfSJoerg Roedel #include <linux/mm_types.h> 20100b8a14SLu Baolu #include <linux/xarray.h> 21672cf6dfSJoerg Roedel #include <linux/ioasid.h> 22672cf6dfSJoerg Roedel #include <asm/page.h> 2320f0afd1SFenghua Yu #include <asm/fpu/api.h> 24672cf6dfSJoerg Roedel 2502f3effdSLu Baolu #include "pasid.h" 2640483774SLu Baolu #include "../iommu-sva-lib.h" 27672cf6dfSJoerg Roedel 28672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d); 29c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid); 3040483774SLu Baolu #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva) 31672cf6dfSJoerg Roedel 32672cf6dfSJoerg Roedel #define PRQ_ORDER 0 33672cf6dfSJoerg Roedel 34100b8a14SLu Baolu static DEFINE_XARRAY_ALLOC(pasid_private_array); 35100b8a14SLu Baolu static int pasid_private_add(ioasid_t pasid, void *priv) 36100b8a14SLu Baolu { 37100b8a14SLu Baolu return xa_alloc(&pasid_private_array, &pasid, priv, 38100b8a14SLu Baolu XA_LIMIT(pasid, pasid), GFP_ATOMIC); 39100b8a14SLu Baolu } 40100b8a14SLu Baolu 41100b8a14SLu Baolu static void pasid_private_remove(ioasid_t pasid) 42100b8a14SLu Baolu { 43100b8a14SLu Baolu xa_erase(&pasid_private_array, pasid); 44100b8a14SLu Baolu } 45100b8a14SLu Baolu 46100b8a14SLu Baolu static void *pasid_private_find(ioasid_t pasid) 47100b8a14SLu Baolu { 48100b8a14SLu Baolu return xa_load(&pasid_private_array, pasid); 49100b8a14SLu Baolu } 50100b8a14SLu Baolu 51*9e52cc0fSLu Baolu static struct intel_svm_dev * 52*9e52cc0fSLu Baolu svm_lookup_device_by_sid(struct intel_svm *svm, u16 sid) 53*9e52cc0fSLu Baolu { 54*9e52cc0fSLu Baolu struct intel_svm_dev *sdev = NULL, *t; 55*9e52cc0fSLu Baolu 56*9e52cc0fSLu Baolu rcu_read_lock(); 57*9e52cc0fSLu Baolu list_for_each_entry_rcu(t, &svm->devs, list) { 58*9e52cc0fSLu Baolu if (t->sid == sid) { 59*9e52cc0fSLu Baolu sdev = t; 60*9e52cc0fSLu Baolu break; 61*9e52cc0fSLu Baolu } 62*9e52cc0fSLu Baolu } 63*9e52cc0fSLu Baolu rcu_read_unlock(); 64*9e52cc0fSLu Baolu 65*9e52cc0fSLu Baolu return sdev; 66*9e52cc0fSLu Baolu } 67*9e52cc0fSLu Baolu 68*9e52cc0fSLu Baolu static struct intel_svm_dev * 69*9e52cc0fSLu Baolu svm_lookup_device_by_dev(struct intel_svm *svm, struct device *dev) 70*9e52cc0fSLu Baolu { 71*9e52cc0fSLu Baolu struct intel_svm_dev *sdev = NULL, *t; 72*9e52cc0fSLu Baolu 73*9e52cc0fSLu Baolu rcu_read_lock(); 74*9e52cc0fSLu Baolu list_for_each_entry_rcu(t, &svm->devs, list) { 75*9e52cc0fSLu Baolu if (t->dev == dev) { 76*9e52cc0fSLu Baolu sdev = t; 77*9e52cc0fSLu Baolu break; 78*9e52cc0fSLu Baolu } 79*9e52cc0fSLu Baolu } 80*9e52cc0fSLu Baolu rcu_read_unlock(); 81*9e52cc0fSLu Baolu 82*9e52cc0fSLu Baolu return sdev; 83*9e52cc0fSLu Baolu } 84*9e52cc0fSLu Baolu 85672cf6dfSJoerg Roedel int intel_svm_enable_prq(struct intel_iommu *iommu) 86672cf6dfSJoerg Roedel { 87672cf6dfSJoerg Roedel struct page *pages; 88672cf6dfSJoerg Roedel int irq, ret; 89672cf6dfSJoerg Roedel 90672cf6dfSJoerg Roedel pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER); 91672cf6dfSJoerg Roedel if (!pages) { 92672cf6dfSJoerg Roedel pr_warn("IOMMU: %s: Failed to allocate page request queue\n", 93672cf6dfSJoerg Roedel iommu->name); 94672cf6dfSJoerg Roedel return -ENOMEM; 95672cf6dfSJoerg Roedel } 96672cf6dfSJoerg Roedel iommu->prq = page_address(pages); 97672cf6dfSJoerg Roedel 98672cf6dfSJoerg Roedel irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu); 99672cf6dfSJoerg Roedel if (irq <= 0) { 100672cf6dfSJoerg Roedel pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n", 101672cf6dfSJoerg Roedel iommu->name); 102672cf6dfSJoerg Roedel ret = -EINVAL; 103672cf6dfSJoerg Roedel err: 104672cf6dfSJoerg Roedel free_pages((unsigned long)iommu->prq, PRQ_ORDER); 105672cf6dfSJoerg Roedel iommu->prq = NULL; 106672cf6dfSJoerg Roedel return ret; 107672cf6dfSJoerg Roedel } 108672cf6dfSJoerg Roedel iommu->pr_irq = irq; 109672cf6dfSJoerg Roedel 110672cf6dfSJoerg Roedel snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id); 111672cf6dfSJoerg Roedel 112672cf6dfSJoerg Roedel ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT, 113672cf6dfSJoerg Roedel iommu->prq_name, iommu); 114672cf6dfSJoerg Roedel if (ret) { 115672cf6dfSJoerg Roedel pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n", 116672cf6dfSJoerg Roedel iommu->name); 117672cf6dfSJoerg Roedel dmar_free_hwirq(irq); 118672cf6dfSJoerg Roedel iommu->pr_irq = 0; 119672cf6dfSJoerg Roedel goto err; 120672cf6dfSJoerg Roedel } 121672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); 122672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); 123672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER); 124672cf6dfSJoerg Roedel 125672cf6dfSJoerg Roedel init_completion(&iommu->prq_complete); 126672cf6dfSJoerg Roedel 127672cf6dfSJoerg Roedel return 0; 128672cf6dfSJoerg Roedel } 129672cf6dfSJoerg Roedel 130672cf6dfSJoerg Roedel int intel_svm_finish_prq(struct intel_iommu *iommu) 131672cf6dfSJoerg Roedel { 132672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); 133672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); 134672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL); 135672cf6dfSJoerg Roedel 136672cf6dfSJoerg Roedel if (iommu->pr_irq) { 137672cf6dfSJoerg Roedel free_irq(iommu->pr_irq, iommu); 138672cf6dfSJoerg Roedel dmar_free_hwirq(iommu->pr_irq); 139672cf6dfSJoerg Roedel iommu->pr_irq = 0; 140672cf6dfSJoerg Roedel } 141672cf6dfSJoerg Roedel 142672cf6dfSJoerg Roedel free_pages((unsigned long)iommu->prq, PRQ_ORDER); 143672cf6dfSJoerg Roedel iommu->prq = NULL; 144672cf6dfSJoerg Roedel 145672cf6dfSJoerg Roedel return 0; 146672cf6dfSJoerg Roedel } 147672cf6dfSJoerg Roedel 148672cf6dfSJoerg Roedel static inline bool intel_svm_capable(struct intel_iommu *iommu) 149672cf6dfSJoerg Roedel { 150672cf6dfSJoerg Roedel return iommu->flags & VTD_FLAG_SVM_CAPABLE; 151672cf6dfSJoerg Roedel } 152672cf6dfSJoerg Roedel 153672cf6dfSJoerg Roedel void intel_svm_check(struct intel_iommu *iommu) 154672cf6dfSJoerg Roedel { 155672cf6dfSJoerg Roedel if (!pasid_supported(iommu)) 156672cf6dfSJoerg Roedel return; 157672cf6dfSJoerg Roedel 158672cf6dfSJoerg Roedel if (cpu_feature_enabled(X86_FEATURE_GBPAGES) && 159672cf6dfSJoerg Roedel !cap_fl1gp_support(iommu->cap)) { 160672cf6dfSJoerg Roedel pr_err("%s SVM disabled, incompatible 1GB page capability\n", 161672cf6dfSJoerg Roedel iommu->name); 162672cf6dfSJoerg Roedel return; 163672cf6dfSJoerg Roedel } 164672cf6dfSJoerg Roedel 165672cf6dfSJoerg Roedel if (cpu_feature_enabled(X86_FEATURE_LA57) && 166672cf6dfSJoerg Roedel !cap_5lp_support(iommu->cap)) { 167672cf6dfSJoerg Roedel pr_err("%s SVM disabled, incompatible paging mode\n", 168672cf6dfSJoerg Roedel iommu->name); 169672cf6dfSJoerg Roedel return; 170672cf6dfSJoerg Roedel } 171672cf6dfSJoerg Roedel 172672cf6dfSJoerg Roedel iommu->flags |= VTD_FLAG_SVM_CAPABLE; 173672cf6dfSJoerg Roedel } 174672cf6dfSJoerg Roedel 1752d6ffc63SLu Baolu static void __flush_svm_range_dev(struct intel_svm *svm, 1762d6ffc63SLu Baolu struct intel_svm_dev *sdev, 1772d6ffc63SLu Baolu unsigned long address, 1782d6ffc63SLu Baolu unsigned long pages, int ih) 179672cf6dfSJoerg Roedel { 1809872f9bdSLu Baolu struct device_domain_info *info = get_domain_info(sdev->dev); 181672cf6dfSJoerg Roedel 1829872f9bdSLu Baolu if (WARN_ON(!pages)) 1839872f9bdSLu Baolu return; 184672cf6dfSJoerg Roedel 1859872f9bdSLu Baolu qi_flush_piotlb(sdev->iommu, sdev->did, svm->pasid, address, pages, ih); 1869872f9bdSLu Baolu if (info->ats_enabled) 1879872f9bdSLu Baolu qi_flush_dev_iotlb_pasid(sdev->iommu, sdev->sid, info->pfsid, 1889872f9bdSLu Baolu svm->pasid, sdev->qdep, address, 1899872f9bdSLu Baolu order_base_2(pages)); 190672cf6dfSJoerg Roedel } 191672cf6dfSJoerg Roedel 1922d6ffc63SLu Baolu static void intel_flush_svm_range_dev(struct intel_svm *svm, 1932d6ffc63SLu Baolu struct intel_svm_dev *sdev, 1942d6ffc63SLu Baolu unsigned long address, 1952d6ffc63SLu Baolu unsigned long pages, int ih) 1962d6ffc63SLu Baolu { 1972d6ffc63SLu Baolu unsigned long shift = ilog2(__roundup_pow_of_two(pages)); 1982d6ffc63SLu Baolu unsigned long align = (1ULL << (VTD_PAGE_SHIFT + shift)); 1992d6ffc63SLu Baolu unsigned long start = ALIGN_DOWN(address, align); 2002d6ffc63SLu Baolu unsigned long end = ALIGN(address + (pages << VTD_PAGE_SHIFT), align); 2012d6ffc63SLu Baolu 2022d6ffc63SLu Baolu while (start < end) { 2032d6ffc63SLu Baolu __flush_svm_range_dev(svm, sdev, start, align >> VTD_PAGE_SHIFT, ih); 2042d6ffc63SLu Baolu start += align; 2052d6ffc63SLu Baolu } 2062d6ffc63SLu Baolu } 2072d6ffc63SLu Baolu 208672cf6dfSJoerg Roedel static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address, 209672cf6dfSJoerg Roedel unsigned long pages, int ih) 210672cf6dfSJoerg Roedel { 211672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 212672cf6dfSJoerg Roedel 213672cf6dfSJoerg Roedel rcu_read_lock(); 214672cf6dfSJoerg Roedel list_for_each_entry_rcu(sdev, &svm->devs, list) 215672cf6dfSJoerg Roedel intel_flush_svm_range_dev(svm, sdev, address, pages, ih); 216672cf6dfSJoerg Roedel rcu_read_unlock(); 217672cf6dfSJoerg Roedel } 218672cf6dfSJoerg Roedel 219672cf6dfSJoerg Roedel /* Pages have been freed at this point */ 220672cf6dfSJoerg Roedel static void intel_invalidate_range(struct mmu_notifier *mn, 221672cf6dfSJoerg Roedel struct mm_struct *mm, 222672cf6dfSJoerg Roedel unsigned long start, unsigned long end) 223672cf6dfSJoerg Roedel { 224672cf6dfSJoerg Roedel struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); 225672cf6dfSJoerg Roedel 226672cf6dfSJoerg Roedel intel_flush_svm_range(svm, start, 227672cf6dfSJoerg Roedel (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0); 228672cf6dfSJoerg Roedel } 229672cf6dfSJoerg Roedel 230672cf6dfSJoerg Roedel static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) 231672cf6dfSJoerg Roedel { 232672cf6dfSJoerg Roedel struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); 233672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 234672cf6dfSJoerg Roedel 235672cf6dfSJoerg Roedel /* This might end up being called from exit_mmap(), *before* the page 236672cf6dfSJoerg Roedel * tables are cleared. And __mmu_notifier_release() will delete us from 237672cf6dfSJoerg Roedel * the list of notifiers so that our invalidate_range() callback doesn't 238672cf6dfSJoerg Roedel * get called when the page tables are cleared. So we need to protect 239672cf6dfSJoerg Roedel * against hardware accessing those page tables. 240672cf6dfSJoerg Roedel * 241672cf6dfSJoerg Roedel * We do it by clearing the entry in the PASID table and then flushing 242672cf6dfSJoerg Roedel * the IOTLB and the PASID table caches. This might upset hardware; 243672cf6dfSJoerg Roedel * perhaps we'll want to point the PASID to a dummy PGD (like the zero 244672cf6dfSJoerg Roedel * page) so that we end up taking a fault that the hardware really 245672cf6dfSJoerg Roedel * *has* to handle gracefully without affecting other processes. 246672cf6dfSJoerg Roedel */ 247672cf6dfSJoerg Roedel rcu_read_lock(); 248672cf6dfSJoerg Roedel list_for_each_entry_rcu(sdev, &svm->devs, list) 2499ad9f45bSLiu Yi L intel_pasid_tear_down_entry(sdev->iommu, sdev->dev, 250672cf6dfSJoerg Roedel svm->pasid, true); 251672cf6dfSJoerg Roedel rcu_read_unlock(); 252672cf6dfSJoerg Roedel 253672cf6dfSJoerg Roedel } 254672cf6dfSJoerg Roedel 255672cf6dfSJoerg Roedel static const struct mmu_notifier_ops intel_mmuops = { 256672cf6dfSJoerg Roedel .release = intel_mm_release, 257672cf6dfSJoerg Roedel .invalidate_range = intel_invalidate_range, 258672cf6dfSJoerg Roedel }; 259672cf6dfSJoerg Roedel 260672cf6dfSJoerg Roedel static DEFINE_MUTEX(pasid_mutex); 261672cf6dfSJoerg Roedel 26219abcf70SLu Baolu static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid, 26319abcf70SLu Baolu struct intel_svm **rsvm, 26419abcf70SLu Baolu struct intel_svm_dev **rsdev) 26519abcf70SLu Baolu { 266*9e52cc0fSLu Baolu struct intel_svm_dev *sdev = NULL; 26719abcf70SLu Baolu struct intel_svm *svm; 26819abcf70SLu Baolu 26919abcf70SLu Baolu /* The caller should hold the pasid_mutex lock */ 27019abcf70SLu Baolu if (WARN_ON(!mutex_is_locked(&pasid_mutex))) 27119abcf70SLu Baolu return -EINVAL; 27219abcf70SLu Baolu 27319abcf70SLu Baolu if (pasid == INVALID_IOASID || pasid >= PASID_MAX) 27419abcf70SLu Baolu return -EINVAL; 27519abcf70SLu Baolu 276100b8a14SLu Baolu svm = pasid_private_find(pasid); 27719abcf70SLu Baolu if (IS_ERR(svm)) 27819abcf70SLu Baolu return PTR_ERR(svm); 27919abcf70SLu Baolu 28019abcf70SLu Baolu if (!svm) 28119abcf70SLu Baolu goto out; 28219abcf70SLu Baolu 28319abcf70SLu Baolu /* 28419abcf70SLu Baolu * If we found svm for the PASID, there must be at least one device 28519abcf70SLu Baolu * bond. 28619abcf70SLu Baolu */ 28719abcf70SLu Baolu if (WARN_ON(list_empty(&svm->devs))) 28819abcf70SLu Baolu return -EINVAL; 289*9e52cc0fSLu Baolu sdev = svm_lookup_device_by_dev(svm, dev); 29019abcf70SLu Baolu 29119abcf70SLu Baolu out: 29219abcf70SLu Baolu *rsvm = svm; 29319abcf70SLu Baolu *rsdev = sdev; 29419abcf70SLu Baolu 29519abcf70SLu Baolu return 0; 29619abcf70SLu Baolu } 29719abcf70SLu Baolu 298672cf6dfSJoerg Roedel int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, 299672cf6dfSJoerg Roedel struct iommu_gpasid_bind_data *data) 300672cf6dfSJoerg Roedel { 301dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 30219abcf70SLu Baolu struct intel_svm_dev *sdev = NULL; 303672cf6dfSJoerg Roedel struct dmar_domain *dmar_domain; 304eea4e29aSLiu Yi L struct device_domain_info *info; 30519abcf70SLu Baolu struct intel_svm *svm = NULL; 306420d42f6SLu Baolu unsigned long iflags; 307672cf6dfSJoerg Roedel int ret = 0; 308672cf6dfSJoerg Roedel 309672cf6dfSJoerg Roedel if (WARN_ON(!iommu) || !data) 310672cf6dfSJoerg Roedel return -EINVAL; 311672cf6dfSJoerg Roedel 3126278eecbSJacob Pan if (data->format != IOMMU_PASID_FORMAT_INTEL_VTD) 3136278eecbSJacob Pan return -EINVAL; 3146278eecbSJacob Pan 3156278eecbSJacob Pan /* IOMMU core ensures argsz is more than the start of the union */ 3166278eecbSJacob Pan if (data->argsz < offsetofend(struct iommu_gpasid_bind_data, vendor.vtd)) 3176278eecbSJacob Pan return -EINVAL; 3186278eecbSJacob Pan 3196278eecbSJacob Pan /* Make sure no undefined flags are used in vendor data */ 3206278eecbSJacob Pan if (data->vendor.vtd.flags & ~(IOMMU_SVA_VTD_GPASID_LAST - 1)) 321672cf6dfSJoerg Roedel return -EINVAL; 322672cf6dfSJoerg Roedel 323672cf6dfSJoerg Roedel if (!dev_is_pci(dev)) 324672cf6dfSJoerg Roedel return -ENOTSUPP; 325672cf6dfSJoerg Roedel 326672cf6dfSJoerg Roedel /* VT-d supports devices with full 20 bit PASIDs only */ 327672cf6dfSJoerg Roedel if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX) 328672cf6dfSJoerg Roedel return -EINVAL; 329672cf6dfSJoerg Roedel 330672cf6dfSJoerg Roedel /* 331672cf6dfSJoerg Roedel * We only check host PASID range, we have no knowledge to check 332672cf6dfSJoerg Roedel * guest PASID range. 333672cf6dfSJoerg Roedel */ 334672cf6dfSJoerg Roedel if (data->hpasid <= 0 || data->hpasid >= PASID_MAX) 335672cf6dfSJoerg Roedel return -EINVAL; 336672cf6dfSJoerg Roedel 337eea4e29aSLiu Yi L info = get_domain_info(dev); 338eea4e29aSLiu Yi L if (!info) 339eea4e29aSLiu Yi L return -EINVAL; 340eea4e29aSLiu Yi L 341672cf6dfSJoerg Roedel dmar_domain = to_dmar_domain(domain); 342672cf6dfSJoerg Roedel 343672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 34419abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev); 34519abcf70SLu Baolu if (ret) 346672cf6dfSJoerg Roedel goto out; 347672cf6dfSJoerg Roedel 34819abcf70SLu Baolu if (sdev) { 349672cf6dfSJoerg Roedel /* 350d315e9e6SJacob Pan * Do not allow multiple bindings of the same device-PASID since 351d315e9e6SJacob Pan * there is only one SL page tables per PASID. We may revisit 352d315e9e6SJacob Pan * once sharing PGD across domains are supported. 353672cf6dfSJoerg Roedel */ 35419abcf70SLu Baolu dev_warn_ratelimited(dev, "Already bound with PASID %u\n", 355672cf6dfSJoerg Roedel svm->pasid); 356672cf6dfSJoerg Roedel ret = -EBUSY; 357672cf6dfSJoerg Roedel goto out; 358672cf6dfSJoerg Roedel } 35919abcf70SLu Baolu 36019abcf70SLu Baolu if (!svm) { 361672cf6dfSJoerg Roedel /* We come here when PASID has never been bond to a device. */ 362672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 363672cf6dfSJoerg Roedel if (!svm) { 364672cf6dfSJoerg Roedel ret = -ENOMEM; 365672cf6dfSJoerg Roedel goto out; 366672cf6dfSJoerg Roedel } 367672cf6dfSJoerg Roedel /* REVISIT: upper layer/VFIO can track host process that bind 368672cf6dfSJoerg Roedel * the PASID. ioasid_set = mm might be sufficient for vfio to 369672cf6dfSJoerg Roedel * check pasid VMM ownership. We can drop the following line 370672cf6dfSJoerg Roedel * once VFIO and IOASID set check is in place. 371672cf6dfSJoerg Roedel */ 372672cf6dfSJoerg Roedel svm->mm = get_task_mm(current); 373672cf6dfSJoerg Roedel svm->pasid = data->hpasid; 374672cf6dfSJoerg Roedel if (data->flags & IOMMU_SVA_GPASID_VAL) { 375672cf6dfSJoerg Roedel svm->gpasid = data->gpasid; 376672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_PASID; 377672cf6dfSJoerg Roedel } 378100b8a14SLu Baolu pasid_private_add(data->hpasid, svm); 379672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 380672cf6dfSJoerg Roedel mmput(svm->mm); 381672cf6dfSJoerg Roedel } 382672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 383672cf6dfSJoerg Roedel if (!sdev) { 384672cf6dfSJoerg Roedel ret = -ENOMEM; 385672cf6dfSJoerg Roedel goto out; 386672cf6dfSJoerg Roedel } 387672cf6dfSJoerg Roedel sdev->dev = dev; 388eea4e29aSLiu Yi L sdev->sid = PCI_DEVID(info->bus, info->devfn); 3899ad9f45bSLiu Yi L sdev->iommu = iommu; 390672cf6dfSJoerg Roedel 391672cf6dfSJoerg Roedel /* Only count users if device has aux domains */ 392672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 393672cf6dfSJoerg Roedel sdev->users = 1; 394672cf6dfSJoerg Roedel 395672cf6dfSJoerg Roedel /* Set up device context entry for PASID if not enabled already */ 396672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, sdev->dev); 397672cf6dfSJoerg Roedel if (ret) { 398672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to enable PASID capability\n"); 399672cf6dfSJoerg Roedel kfree(sdev); 400672cf6dfSJoerg Roedel goto out; 401672cf6dfSJoerg Roedel } 402672cf6dfSJoerg Roedel 403672cf6dfSJoerg Roedel /* 404672cf6dfSJoerg Roedel * PASID table is per device for better security. Therefore, for 405672cf6dfSJoerg Roedel * each bind of a new device even with an existing PASID, we need to 406672cf6dfSJoerg Roedel * call the nested mode setup function here. 407672cf6dfSJoerg Roedel */ 408420d42f6SLu Baolu spin_lock_irqsave(&iommu->lock, iflags); 409672cf6dfSJoerg Roedel ret = intel_pasid_setup_nested(iommu, dev, 410672cf6dfSJoerg Roedel (pgd_t *)(uintptr_t)data->gpgd, 4118d3bb3b8SJacob Pan data->hpasid, &data->vendor.vtd, dmar_domain, 412672cf6dfSJoerg Roedel data->addr_width); 413420d42f6SLu Baolu spin_unlock_irqrestore(&iommu->lock, iflags); 414672cf6dfSJoerg Roedel if (ret) { 415672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n", 416672cf6dfSJoerg Roedel data->hpasid, ret); 417672cf6dfSJoerg Roedel /* 418672cf6dfSJoerg Roedel * PASID entry should be in cleared state if nested mode 419672cf6dfSJoerg Roedel * set up failed. So we only need to clear IOASID tracking 420672cf6dfSJoerg Roedel * data such that free call will succeed. 421672cf6dfSJoerg Roedel */ 422672cf6dfSJoerg Roedel kfree(sdev); 423672cf6dfSJoerg Roedel goto out; 424672cf6dfSJoerg Roedel } 425672cf6dfSJoerg Roedel 426672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_MODE; 427672cf6dfSJoerg Roedel 428672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 429672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 430672cf6dfSJoerg Roedel out: 431672cf6dfSJoerg Roedel if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) { 432100b8a14SLu Baolu pasid_private_remove(data->hpasid); 433672cf6dfSJoerg Roedel kfree(svm); 434672cf6dfSJoerg Roedel } 435672cf6dfSJoerg Roedel 436672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 437672cf6dfSJoerg Roedel return ret; 438672cf6dfSJoerg Roedel } 439672cf6dfSJoerg Roedel 440c7b6bac9SFenghua Yu int intel_svm_unbind_gpasid(struct device *dev, u32 pasid) 441672cf6dfSJoerg Roedel { 442dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 443672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 444672cf6dfSJoerg Roedel struct intel_svm *svm; 44519abcf70SLu Baolu int ret; 446672cf6dfSJoerg Roedel 447672cf6dfSJoerg Roedel if (WARN_ON(!iommu)) 448672cf6dfSJoerg Roedel return -EINVAL; 449672cf6dfSJoerg Roedel 450672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 45119abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 45219abcf70SLu Baolu if (ret) 453672cf6dfSJoerg Roedel goto out; 454672cf6dfSJoerg Roedel 45519abcf70SLu Baolu if (sdev) { 456672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 457672cf6dfSJoerg Roedel sdev->users--; 458672cf6dfSJoerg Roedel if (!sdev->users) { 459672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 460672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 461672cf6dfSJoerg Roedel svm->pasid, false); 462672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 463672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 464672cf6dfSJoerg Roedel 465672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 466672cf6dfSJoerg Roedel /* 467672cf6dfSJoerg Roedel * We do not free the IOASID here in that 468672cf6dfSJoerg Roedel * IOMMU driver did not allocate it. 469672cf6dfSJoerg Roedel * Unlike native SVM, IOASID for guest use was 470672cf6dfSJoerg Roedel * allocated prior to the bind call. 471672cf6dfSJoerg Roedel * In any case, if the free call comes before 472672cf6dfSJoerg Roedel * the unbind, IOMMU driver will get notified 473672cf6dfSJoerg Roedel * and perform cleanup. 474672cf6dfSJoerg Roedel */ 475100b8a14SLu Baolu pasid_private_remove(pasid); 476672cf6dfSJoerg Roedel kfree(svm); 477672cf6dfSJoerg Roedel } 478672cf6dfSJoerg Roedel } 479672cf6dfSJoerg Roedel } 480672cf6dfSJoerg Roedel out: 481672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 482672cf6dfSJoerg Roedel return ret; 483672cf6dfSJoerg Roedel } 484672cf6dfSJoerg Roedel 48520f0afd1SFenghua Yu static void _load_pasid(void *unused) 48620f0afd1SFenghua Yu { 48720f0afd1SFenghua Yu update_pasid(); 48820f0afd1SFenghua Yu } 48920f0afd1SFenghua Yu 49020f0afd1SFenghua Yu static void load_pasid(struct mm_struct *mm, u32 pasid) 49120f0afd1SFenghua Yu { 49220f0afd1SFenghua Yu mutex_lock(&mm->context.lock); 49320f0afd1SFenghua Yu 49420f0afd1SFenghua Yu /* Synchronize with READ_ONCE in update_pasid(). */ 49520f0afd1SFenghua Yu smp_store_release(&mm->pasid, pasid); 49620f0afd1SFenghua Yu 49720f0afd1SFenghua Yu /* Update PASID MSR on all CPUs running the mm's tasks. */ 49820f0afd1SFenghua Yu on_each_cpu_mask(mm_cpumask(mm), _load_pasid, NULL, true); 49920f0afd1SFenghua Yu 50020f0afd1SFenghua Yu mutex_unlock(&mm->context.lock); 50120f0afd1SFenghua Yu } 50220f0afd1SFenghua Yu 50340483774SLu Baolu static int intel_svm_alloc_pasid(struct device *dev, struct mm_struct *mm, 50440483774SLu Baolu unsigned int flags) 505672cf6dfSJoerg Roedel { 50640483774SLu Baolu ioasid_t max_pasid = dev_is_pci(dev) ? 50740483774SLu Baolu pci_max_pasids(to_pci_dev(dev)) : intel_pasid_max_id; 50840483774SLu Baolu 50940483774SLu Baolu return iommu_sva_alloc_pasid(mm, PASID_MIN, max_pasid - 1); 51040483774SLu Baolu } 51140483774SLu Baolu 51240483774SLu Baolu static void intel_svm_free_pasid(struct mm_struct *mm) 51340483774SLu Baolu { 51440483774SLu Baolu iommu_sva_free_pasid(mm); 51540483774SLu Baolu } 51640483774SLu Baolu 51740483774SLu Baolu static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu, 51840483774SLu Baolu struct device *dev, 51940483774SLu Baolu struct mm_struct *mm, 52040483774SLu Baolu unsigned int flags) 52140483774SLu Baolu { 52240483774SLu Baolu struct device_domain_info *info = get_domain_info(dev); 52340483774SLu Baolu unsigned long iflags, sflags; 524672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 52540483774SLu Baolu struct intel_svm *svm; 52640483774SLu Baolu int ret = 0; 527672cf6dfSJoerg Roedel 52840483774SLu Baolu svm = pasid_private_find(mm->pasid); 52940483774SLu Baolu if (!svm) { 53040483774SLu Baolu svm = kzalloc(sizeof(*svm), GFP_KERNEL); 53140483774SLu Baolu if (!svm) 53240483774SLu Baolu return ERR_PTR(-ENOMEM); 533672cf6dfSJoerg Roedel 53440483774SLu Baolu svm->pasid = mm->pasid; 53540483774SLu Baolu svm->mm = mm; 53640483774SLu Baolu svm->flags = flags; 53740483774SLu Baolu INIT_LIST_HEAD_RCU(&svm->devs); 538672cf6dfSJoerg Roedel 53940483774SLu Baolu if (!(flags & SVM_FLAG_SUPERVISOR_MODE)) { 54040483774SLu Baolu svm->notifier.ops = &intel_mmuops; 54140483774SLu Baolu ret = mmu_notifier_register(&svm->notifier, mm); 54240483774SLu Baolu if (ret) { 54340483774SLu Baolu kfree(svm); 54440483774SLu Baolu return ERR_PTR(ret); 545672cf6dfSJoerg Roedel } 546672cf6dfSJoerg Roedel } 547672cf6dfSJoerg Roedel 54840483774SLu Baolu ret = pasid_private_add(svm->pasid, svm); 54940483774SLu Baolu if (ret) { 55040483774SLu Baolu if (svm->notifier.ops) 55140483774SLu Baolu mmu_notifier_unregister(&svm->notifier, mm); 55240483774SLu Baolu kfree(svm); 55340483774SLu Baolu return ERR_PTR(ret); 55440483774SLu Baolu } 555672cf6dfSJoerg Roedel } 556672cf6dfSJoerg Roedel 557672cf6dfSJoerg Roedel /* Find the matching device in svm list */ 558*9e52cc0fSLu Baolu sdev = svm_lookup_device_by_dev(svm, dev); 559*9e52cc0fSLu Baolu if (sdev) { 560672cf6dfSJoerg Roedel sdev->users++; 561672cf6dfSJoerg Roedel goto success; 562672cf6dfSJoerg Roedel } 563672cf6dfSJoerg Roedel 564672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 565672cf6dfSJoerg Roedel if (!sdev) { 566672cf6dfSJoerg Roedel ret = -ENOMEM; 56740483774SLu Baolu goto free_svm; 568672cf6dfSJoerg Roedel } 56940483774SLu Baolu 570672cf6dfSJoerg Roedel sdev->dev = dev; 5719ad9f45bSLiu Yi L sdev->iommu = iommu; 572672cf6dfSJoerg Roedel sdev->did = FLPT_DEFAULT_DID; 573672cf6dfSJoerg Roedel sdev->sid = PCI_DEVID(info->bus, info->devfn); 57440483774SLu Baolu sdev->users = 1; 57540483774SLu Baolu sdev->pasid = svm->pasid; 57640483774SLu Baolu sdev->sva.dev = dev; 57740483774SLu Baolu init_rcu_head(&sdev->rcu); 578672cf6dfSJoerg Roedel if (info->ats_enabled) { 579672cf6dfSJoerg Roedel sdev->dev_iotlb = 1; 580672cf6dfSJoerg Roedel sdev->qdep = info->ats_qdep; 581672cf6dfSJoerg Roedel if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) 582672cf6dfSJoerg Roedel sdev->qdep = 0; 583672cf6dfSJoerg Roedel } 584672cf6dfSJoerg Roedel 58540483774SLu Baolu /* Setup the pasid table: */ 58640483774SLu Baolu sflags = (flags & SVM_FLAG_SUPERVISOR_MODE) ? 58740483774SLu Baolu PASID_FLAG_SUPERVISOR_MODE : 0; 58840483774SLu Baolu sflags |= cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0; 589420d42f6SLu Baolu spin_lock_irqsave(&iommu->lock, iflags); 59040483774SLu Baolu ret = intel_pasid_setup_first_level(iommu, dev, mm->pgd, mm->pasid, 59140483774SLu Baolu FLPT_DEFAULT_DID, sflags); 592420d42f6SLu Baolu spin_unlock_irqrestore(&iommu->lock, iflags); 593672cf6dfSJoerg Roedel 59440483774SLu Baolu if (ret) 59540483774SLu Baolu goto free_sdev; 59640483774SLu Baolu 59720f0afd1SFenghua Yu /* The newly allocated pasid is loaded to the mm. */ 59840483774SLu Baolu if (!(flags & SVM_FLAG_SUPERVISOR_MODE) && list_empty(&svm->devs)) 59920f0afd1SFenghua Yu load_pasid(mm, svm->pasid); 60040483774SLu Baolu 601672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 602672cf6dfSJoerg Roedel success: 60340483774SLu Baolu return &sdev->sva; 60440483774SLu Baolu 60540483774SLu Baolu free_sdev: 60640483774SLu Baolu kfree(sdev); 60740483774SLu Baolu free_svm: 60840483774SLu Baolu if (list_empty(&svm->devs)) { 60940483774SLu Baolu if (svm->notifier.ops) 61040483774SLu Baolu mmu_notifier_unregister(&svm->notifier, mm); 61140483774SLu Baolu pasid_private_remove(mm->pasid); 61240483774SLu Baolu kfree(svm); 61340483774SLu Baolu } 61440483774SLu Baolu 61540483774SLu Baolu return ERR_PTR(ret); 616672cf6dfSJoerg Roedel } 617672cf6dfSJoerg Roedel 618672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex */ 619c7b6bac9SFenghua Yu static int intel_svm_unbind_mm(struct device *dev, u32 pasid) 620672cf6dfSJoerg Roedel { 621672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 622672cf6dfSJoerg Roedel struct intel_iommu *iommu; 623672cf6dfSJoerg Roedel struct intel_svm *svm; 62440483774SLu Baolu struct mm_struct *mm; 625672cf6dfSJoerg Roedel int ret = -EINVAL; 626672cf6dfSJoerg Roedel 627dd6692f1SLu Baolu iommu = device_to_iommu(dev, NULL, NULL); 628672cf6dfSJoerg Roedel if (!iommu) 629672cf6dfSJoerg Roedel goto out; 630672cf6dfSJoerg Roedel 63119abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 63219abcf70SLu Baolu if (ret) 633672cf6dfSJoerg Roedel goto out; 63440483774SLu Baolu mm = svm->mm; 635672cf6dfSJoerg Roedel 63619abcf70SLu Baolu if (sdev) { 637672cf6dfSJoerg Roedel sdev->users--; 638672cf6dfSJoerg Roedel if (!sdev->users) { 639672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 640672cf6dfSJoerg Roedel /* Flush the PASID cache and IOTLB for this device. 641672cf6dfSJoerg Roedel * Note that we do depend on the hardware *not* using 642672cf6dfSJoerg Roedel * the PASID any more. Just as we depend on other 643672cf6dfSJoerg Roedel * devices never using PASIDs that they have no right 644672cf6dfSJoerg Roedel * to use. We have a *shared* PASID table, because it's 645672cf6dfSJoerg Roedel * large and has to be physically contiguous. So it's 646672cf6dfSJoerg Roedel * hard to be as defensive as we might like. */ 647672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 648672cf6dfSJoerg Roedel svm->pasid, false); 649672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 650672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 651672cf6dfSJoerg Roedel 652672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 65340483774SLu Baolu intel_svm_free_pasid(mm); 65440483774SLu Baolu if (svm->notifier.ops) { 65540483774SLu Baolu mmu_notifier_unregister(&svm->notifier, mm); 65620f0afd1SFenghua Yu /* Clear mm's pasid. */ 65740483774SLu Baolu load_pasid(mm, PASID_DISABLED); 65820f0afd1SFenghua Yu } 659100b8a14SLu Baolu pasid_private_remove(svm->pasid); 660672cf6dfSJoerg Roedel /* We mandate that no page faults may be outstanding 661672cf6dfSJoerg Roedel * for the PASID when intel_svm_unbind_mm() is called. 662672cf6dfSJoerg Roedel * If that is not obeyed, subtle errors will happen. 663672cf6dfSJoerg Roedel * Let's make them less subtle... */ 664672cf6dfSJoerg Roedel memset(svm, 0x6b, sizeof(*svm)); 665672cf6dfSJoerg Roedel kfree(svm); 666672cf6dfSJoerg Roedel } 667672cf6dfSJoerg Roedel } 668672cf6dfSJoerg Roedel } 669672cf6dfSJoerg Roedel out: 670672cf6dfSJoerg Roedel return ret; 671672cf6dfSJoerg Roedel } 672672cf6dfSJoerg Roedel 673672cf6dfSJoerg Roedel /* Page request queue descriptor */ 674672cf6dfSJoerg Roedel struct page_req_dsc { 675672cf6dfSJoerg Roedel union { 676672cf6dfSJoerg Roedel struct { 677672cf6dfSJoerg Roedel u64 type:8; 678672cf6dfSJoerg Roedel u64 pasid_present:1; 679672cf6dfSJoerg Roedel u64 priv_data_present:1; 680672cf6dfSJoerg Roedel u64 rsvd:6; 681672cf6dfSJoerg Roedel u64 rid:16; 682672cf6dfSJoerg Roedel u64 pasid:20; 683672cf6dfSJoerg Roedel u64 exe_req:1; 684672cf6dfSJoerg Roedel u64 pm_req:1; 685672cf6dfSJoerg Roedel u64 rsvd2:10; 686672cf6dfSJoerg Roedel }; 687672cf6dfSJoerg Roedel u64 qw_0; 688672cf6dfSJoerg Roedel }; 689672cf6dfSJoerg Roedel union { 690672cf6dfSJoerg Roedel struct { 691672cf6dfSJoerg Roedel u64 rd_req:1; 692672cf6dfSJoerg Roedel u64 wr_req:1; 693672cf6dfSJoerg Roedel u64 lpig:1; 694672cf6dfSJoerg Roedel u64 prg_index:9; 695672cf6dfSJoerg Roedel u64 addr:52; 696672cf6dfSJoerg Roedel }; 697672cf6dfSJoerg Roedel u64 qw_1; 698672cf6dfSJoerg Roedel }; 699672cf6dfSJoerg Roedel u64 priv_data[2]; 700672cf6dfSJoerg Roedel }; 701672cf6dfSJoerg Roedel 702672cf6dfSJoerg Roedel #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20) 703672cf6dfSJoerg Roedel 704672cf6dfSJoerg Roedel static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req) 705672cf6dfSJoerg Roedel { 706672cf6dfSJoerg Roedel unsigned long requested = 0; 707672cf6dfSJoerg Roedel 708672cf6dfSJoerg Roedel if (req->exe_req) 709672cf6dfSJoerg Roedel requested |= VM_EXEC; 710672cf6dfSJoerg Roedel 711672cf6dfSJoerg Roedel if (req->rd_req) 712672cf6dfSJoerg Roedel requested |= VM_READ; 713672cf6dfSJoerg Roedel 714672cf6dfSJoerg Roedel if (req->wr_req) 715672cf6dfSJoerg Roedel requested |= VM_WRITE; 716672cf6dfSJoerg Roedel 717672cf6dfSJoerg Roedel return (requested & ~vma->vm_flags) != 0; 718672cf6dfSJoerg Roedel } 719672cf6dfSJoerg Roedel 720672cf6dfSJoerg Roedel static bool is_canonical_address(u64 addr) 721672cf6dfSJoerg Roedel { 722672cf6dfSJoerg Roedel int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); 723672cf6dfSJoerg Roedel long saddr = (long) addr; 724672cf6dfSJoerg Roedel 725672cf6dfSJoerg Roedel return (((saddr << shift) >> shift) == saddr); 726672cf6dfSJoerg Roedel } 727672cf6dfSJoerg Roedel 728672cf6dfSJoerg Roedel /** 729672cf6dfSJoerg Roedel * intel_svm_drain_prq - Drain page requests and responses for a pasid 730672cf6dfSJoerg Roedel * @dev: target device 731672cf6dfSJoerg Roedel * @pasid: pasid for draining 732672cf6dfSJoerg Roedel * 733672cf6dfSJoerg Roedel * Drain all pending page requests and responses related to @pasid in both 734672cf6dfSJoerg Roedel * software and hardware. This is supposed to be called after the device 735672cf6dfSJoerg Roedel * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB 736672cf6dfSJoerg Roedel * and DevTLB have been invalidated. 737672cf6dfSJoerg Roedel * 738672cf6dfSJoerg Roedel * It waits until all pending page requests for @pasid in the page fault 739672cf6dfSJoerg Roedel * queue are completed by the prq handling thread. Then follow the steps 740672cf6dfSJoerg Roedel * described in VT-d spec CH7.10 to drain all page requests and page 741672cf6dfSJoerg Roedel * responses pending in the hardware. 742672cf6dfSJoerg Roedel */ 743c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid) 744672cf6dfSJoerg Roedel { 745672cf6dfSJoerg Roedel struct device_domain_info *info; 746672cf6dfSJoerg Roedel struct dmar_domain *domain; 747672cf6dfSJoerg Roedel struct intel_iommu *iommu; 748672cf6dfSJoerg Roedel struct qi_desc desc[3]; 749672cf6dfSJoerg Roedel struct pci_dev *pdev; 750672cf6dfSJoerg Roedel int head, tail; 751672cf6dfSJoerg Roedel u16 sid, did; 752672cf6dfSJoerg Roedel int qdep; 753672cf6dfSJoerg Roedel 754672cf6dfSJoerg Roedel info = get_domain_info(dev); 755672cf6dfSJoerg Roedel if (WARN_ON(!info || !dev_is_pci(dev))) 756672cf6dfSJoerg Roedel return; 757672cf6dfSJoerg Roedel 758672cf6dfSJoerg Roedel if (!info->pri_enabled) 759672cf6dfSJoerg Roedel return; 760672cf6dfSJoerg Roedel 761672cf6dfSJoerg Roedel iommu = info->iommu; 762672cf6dfSJoerg Roedel domain = info->domain; 763672cf6dfSJoerg Roedel pdev = to_pci_dev(dev); 764672cf6dfSJoerg Roedel sid = PCI_DEVID(info->bus, info->devfn); 765672cf6dfSJoerg Roedel did = domain->iommu_did[iommu->seq_id]; 766672cf6dfSJoerg Roedel qdep = pci_ats_queue_depth(pdev); 767672cf6dfSJoerg Roedel 768672cf6dfSJoerg Roedel /* 769672cf6dfSJoerg Roedel * Check and wait until all pending page requests in the queue are 770672cf6dfSJoerg Roedel * handled by the prq handling thread. 771672cf6dfSJoerg Roedel */ 772672cf6dfSJoerg Roedel prq_retry: 773672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 774672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 775672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 776672cf6dfSJoerg Roedel while (head != tail) { 777672cf6dfSJoerg Roedel struct page_req_dsc *req; 778672cf6dfSJoerg Roedel 779672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 780672cf6dfSJoerg Roedel if (!req->pasid_present || req->pasid != pasid) { 781672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 782672cf6dfSJoerg Roedel continue; 783672cf6dfSJoerg Roedel } 784672cf6dfSJoerg Roedel 785672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 786672cf6dfSJoerg Roedel goto prq_retry; 787672cf6dfSJoerg Roedel } 788672cf6dfSJoerg Roedel 789672cf6dfSJoerg Roedel /* 790672cf6dfSJoerg Roedel * Perform steps described in VT-d spec CH7.10 to drain page 791672cf6dfSJoerg Roedel * requests and responses in hardware. 792672cf6dfSJoerg Roedel */ 793672cf6dfSJoerg Roedel memset(desc, 0, sizeof(desc)); 794672cf6dfSJoerg Roedel desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) | 795672cf6dfSJoerg Roedel QI_IWD_FENCE | 796672cf6dfSJoerg Roedel QI_IWD_TYPE; 797672cf6dfSJoerg Roedel desc[1].qw0 = QI_EIOTLB_PASID(pasid) | 798672cf6dfSJoerg Roedel QI_EIOTLB_DID(did) | 799672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | 800672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 801672cf6dfSJoerg Roedel desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) | 802672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SID(sid) | 803672cf6dfSJoerg Roedel QI_DEV_EIOTLB_QDEP(qdep) | 804672cf6dfSJoerg Roedel QI_DEIOTLB_TYPE | 805672cf6dfSJoerg Roedel QI_DEV_IOTLB_PFSID(info->pfsid); 806672cf6dfSJoerg Roedel qi_retry: 807672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 808672cf6dfSJoerg Roedel qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN); 809672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) { 810672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 811672cf6dfSJoerg Roedel goto qi_retry; 812672cf6dfSJoerg Roedel } 813672cf6dfSJoerg Roedel } 814672cf6dfSJoerg Roedel 815eb8d93eaSLu Baolu static int prq_to_iommu_prot(struct page_req_dsc *req) 816eb8d93eaSLu Baolu { 817eb8d93eaSLu Baolu int prot = 0; 818eb8d93eaSLu Baolu 819eb8d93eaSLu Baolu if (req->rd_req) 820eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_READ; 821eb8d93eaSLu Baolu if (req->wr_req) 822eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_WRITE; 823eb8d93eaSLu Baolu if (req->exe_req) 824eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_EXEC; 825eb8d93eaSLu Baolu if (req->pm_req) 826eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_PRIV; 827eb8d93eaSLu Baolu 828eb8d93eaSLu Baolu return prot; 829eb8d93eaSLu Baolu } 830eb8d93eaSLu Baolu 831eb8d93eaSLu Baolu static int 832eb8d93eaSLu Baolu intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc) 833eb8d93eaSLu Baolu { 834eb8d93eaSLu Baolu struct iommu_fault_event event; 835eb8d93eaSLu Baolu 836eb8d93eaSLu Baolu if (!dev || !dev_is_pci(dev)) 837eb8d93eaSLu Baolu return -ENODEV; 838eb8d93eaSLu Baolu 839eb8d93eaSLu Baolu /* Fill in event data for device specific processing */ 840eb8d93eaSLu Baolu memset(&event, 0, sizeof(struct iommu_fault_event)); 841eb8d93eaSLu Baolu event.fault.type = IOMMU_FAULT_PAGE_REQ; 84203d20509SLu Baolu event.fault.prm.addr = (u64)desc->addr << VTD_PAGE_SHIFT; 843eb8d93eaSLu Baolu event.fault.prm.pasid = desc->pasid; 844eb8d93eaSLu Baolu event.fault.prm.grpid = desc->prg_index; 845eb8d93eaSLu Baolu event.fault.prm.perm = prq_to_iommu_prot(desc); 846eb8d93eaSLu Baolu 847eb8d93eaSLu Baolu if (desc->lpig) 848eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 849eb8d93eaSLu Baolu if (desc->pasid_present) { 850eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 851eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID; 852eb8d93eaSLu Baolu } 853eb8d93eaSLu Baolu if (desc->priv_data_present) { 854eb8d93eaSLu Baolu /* 855eb8d93eaSLu Baolu * Set last page in group bit if private data is present, 856eb8d93eaSLu Baolu * page response is required as it does for LPIG. 857eb8d93eaSLu Baolu * iommu_report_device_fault() doesn't understand this vendor 858eb8d93eaSLu Baolu * specific requirement thus we set last_page as a workaround. 859eb8d93eaSLu Baolu */ 860eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 861eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 862eb8d93eaSLu Baolu memcpy(event.fault.prm.private_data, desc->priv_data, 863eb8d93eaSLu Baolu sizeof(desc->priv_data)); 864eb8d93eaSLu Baolu } 865eb8d93eaSLu Baolu 866eb8d93eaSLu Baolu return iommu_report_device_fault(dev, &event); 867eb8d93eaSLu Baolu } 868eb8d93eaSLu Baolu 869672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d) 870672cf6dfSJoerg Roedel { 871eb8d93eaSLu Baolu struct intel_svm_dev *sdev = NULL; 872672cf6dfSJoerg Roedel struct intel_iommu *iommu = d; 873672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 874672cf6dfSJoerg Roedel int head, tail, handled = 0; 875396bd6f3SJacob Pan unsigned int flags = 0; 876672cf6dfSJoerg Roedel 877672cf6dfSJoerg Roedel /* Clear PPR bit before reading head/tail registers, to 878672cf6dfSJoerg Roedel * ensure that we get a new interrupt if needed. */ 879672cf6dfSJoerg Roedel writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG); 880672cf6dfSJoerg Roedel 881672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 882672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 883672cf6dfSJoerg Roedel while (head != tail) { 884672cf6dfSJoerg Roedel struct vm_area_struct *vma; 885672cf6dfSJoerg Roedel struct page_req_dsc *req; 886672cf6dfSJoerg Roedel struct qi_desc resp; 887672cf6dfSJoerg Roedel int result; 888672cf6dfSJoerg Roedel vm_fault_t ret; 889672cf6dfSJoerg Roedel u64 address; 890672cf6dfSJoerg Roedel 891672cf6dfSJoerg Roedel handled = 1; 892672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 8933aa7c62cSLu Baolu result = QI_RESP_INVALID; 894672cf6dfSJoerg Roedel address = (u64)req->addr << VTD_PAGE_SHIFT; 895672cf6dfSJoerg Roedel if (!req->pasid_present) { 896672cf6dfSJoerg Roedel pr_err("%s: Page request without PASID: %08llx %08llx\n", 897672cf6dfSJoerg Roedel iommu->name, ((unsigned long long *)req)[0], 898672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 899672cf6dfSJoerg Roedel goto no_pasid; 900672cf6dfSJoerg Roedel } 90178a523feSJacob Pan /* We shall not receive page request for supervisor SVM */ 90278a523feSJacob Pan if (req->pm_req && (req->rd_req | req->wr_req)) { 90378a523feSJacob Pan pr_err("Unexpected page request in Privilege Mode"); 90478a523feSJacob Pan /* No need to find the matching sdev as for bad_req */ 90578a523feSJacob Pan goto no_pasid; 90678a523feSJacob Pan } 90778a523feSJacob Pan /* DMA read with exec requeset is not supported. */ 90878a523feSJacob Pan if (req->exe_req && req->rd_req) { 90978a523feSJacob Pan pr_err("Execution request not supported\n"); 91078a523feSJacob Pan goto no_pasid; 91178a523feSJacob Pan } 912672cf6dfSJoerg Roedel if (!svm || svm->pasid != req->pasid) { 913672cf6dfSJoerg Roedel rcu_read_lock(); 914100b8a14SLu Baolu svm = pasid_private_find(req->pasid); 915672cf6dfSJoerg Roedel /* It *can't* go away, because the driver is not permitted 916672cf6dfSJoerg Roedel * to unbind the mm while any page faults are outstanding. 917672cf6dfSJoerg Roedel * So we only need RCU to protect the internal idr code. */ 918672cf6dfSJoerg Roedel rcu_read_unlock(); 919672cf6dfSJoerg Roedel if (IS_ERR_OR_NULL(svm)) { 920672cf6dfSJoerg Roedel pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n", 921672cf6dfSJoerg Roedel iommu->name, req->pasid, ((unsigned long long *)req)[0], 922672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 923672cf6dfSJoerg Roedel goto no_pasid; 924672cf6dfSJoerg Roedel } 925672cf6dfSJoerg Roedel } 926672cf6dfSJoerg Roedel 927*9e52cc0fSLu Baolu if (!sdev || sdev->sid != req->rid) 928*9e52cc0fSLu Baolu sdev = svm_lookup_device_by_sid(svm, req->rid); 929eb8d93eaSLu Baolu 930672cf6dfSJoerg Roedel /* Since we're using init_mm.pgd directly, we should never take 931672cf6dfSJoerg Roedel * any faults on kernel addresses. */ 932672cf6dfSJoerg Roedel if (!svm->mm) 933672cf6dfSJoerg Roedel goto bad_req; 934672cf6dfSJoerg Roedel 935672cf6dfSJoerg Roedel /* If address is not canonical, return invalid response */ 936672cf6dfSJoerg Roedel if (!is_canonical_address(address)) 937672cf6dfSJoerg Roedel goto bad_req; 938672cf6dfSJoerg Roedel 939eb8d93eaSLu Baolu /* 940eb8d93eaSLu Baolu * If prq is to be handled outside iommu driver via receiver of 941eb8d93eaSLu Baolu * the fault notifiers, we skip the page response here. 942eb8d93eaSLu Baolu */ 943eb8d93eaSLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 944eb8d93eaSLu Baolu if (sdev && !intel_svm_prq_report(sdev->dev, req)) 945eb8d93eaSLu Baolu goto prq_advance; 946eb8d93eaSLu Baolu else 947eb8d93eaSLu Baolu goto bad_req; 948eb8d93eaSLu Baolu } 949eb8d93eaSLu Baolu 950672cf6dfSJoerg Roedel /* If the mm is already defunct, don't handle faults. */ 951672cf6dfSJoerg Roedel if (!mmget_not_zero(svm->mm)) 952672cf6dfSJoerg Roedel goto bad_req; 953672cf6dfSJoerg Roedel 9548f02f363SLinus Torvalds mmap_read_lock(svm->mm); 955672cf6dfSJoerg Roedel vma = find_extend_vma(svm->mm, address); 956672cf6dfSJoerg Roedel if (!vma || address < vma->vm_start) 957672cf6dfSJoerg Roedel goto invalid; 958672cf6dfSJoerg Roedel 959672cf6dfSJoerg Roedel if (access_error(vma, req)) 960672cf6dfSJoerg Roedel goto invalid; 961672cf6dfSJoerg Roedel 962396bd6f3SJacob Pan flags = FAULT_FLAG_USER | FAULT_FLAG_REMOTE; 963396bd6f3SJacob Pan if (req->wr_req) 964396bd6f3SJacob Pan flags |= FAULT_FLAG_WRITE; 965396bd6f3SJacob Pan 966396bd6f3SJacob Pan ret = handle_mm_fault(vma, address, flags, NULL); 967672cf6dfSJoerg Roedel if (ret & VM_FAULT_ERROR) 968672cf6dfSJoerg Roedel goto invalid; 969672cf6dfSJoerg Roedel 970672cf6dfSJoerg Roedel result = QI_RESP_SUCCESS; 971672cf6dfSJoerg Roedel invalid: 9728f02f363SLinus Torvalds mmap_read_unlock(svm->mm); 973672cf6dfSJoerg Roedel mmput(svm->mm); 974672cf6dfSJoerg Roedel bad_req: 975672cf6dfSJoerg Roedel /* We get here in the error case where the PASID lookup failed, 976672cf6dfSJoerg Roedel and these can be NULL. Do not use them below this point! */ 977672cf6dfSJoerg Roedel sdev = NULL; 978672cf6dfSJoerg Roedel svm = NULL; 979672cf6dfSJoerg Roedel no_pasid: 980672cf6dfSJoerg Roedel if (req->lpig || req->priv_data_present) { 981672cf6dfSJoerg Roedel /* 982672cf6dfSJoerg Roedel * Per VT-d spec. v3.0 ch7.7, system software must 983672cf6dfSJoerg Roedel * respond with page group response if private data 984672cf6dfSJoerg Roedel * is present (PDP) or last page in group (LPIG) bit 985672cf6dfSJoerg Roedel * is set. This is an additional VT-d feature beyond 986672cf6dfSJoerg Roedel * PCI ATS spec. 987672cf6dfSJoerg Roedel */ 988672cf6dfSJoerg Roedel resp.qw0 = QI_PGRP_PASID(req->pasid) | 989672cf6dfSJoerg Roedel QI_PGRP_DID(req->rid) | 990672cf6dfSJoerg Roedel QI_PGRP_PASID_P(req->pasid_present) | 99171cd8e2dSLiu, Yi L QI_PGRP_PDP(req->priv_data_present) | 992672cf6dfSJoerg Roedel QI_PGRP_RESP_CODE(result) | 993672cf6dfSJoerg Roedel QI_PGRP_RESP_TYPE; 994672cf6dfSJoerg Roedel resp.qw1 = QI_PGRP_IDX(req->prg_index) | 995672cf6dfSJoerg Roedel QI_PGRP_LPIG(req->lpig); 9961d421058SLu Baolu resp.qw2 = 0; 9971d421058SLu Baolu resp.qw3 = 0; 998672cf6dfSJoerg Roedel 999672cf6dfSJoerg Roedel if (req->priv_data_present) 1000672cf6dfSJoerg Roedel memcpy(&resp.qw2, req->priv_data, 1001672cf6dfSJoerg Roedel sizeof(req->priv_data)); 1002672cf6dfSJoerg Roedel qi_submit_sync(iommu, &resp, 1, 0); 1003672cf6dfSJoerg Roedel } 1004eb8d93eaSLu Baolu prq_advance: 1005672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 1006672cf6dfSJoerg Roedel } 1007672cf6dfSJoerg Roedel 1008672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, tail); 1009672cf6dfSJoerg Roedel 1010672cf6dfSJoerg Roedel /* 1011672cf6dfSJoerg Roedel * Clear the page request overflow bit and wake up all threads that 1012672cf6dfSJoerg Roedel * are waiting for the completion of this handling. 1013672cf6dfSJoerg Roedel */ 101428a77185SLu Baolu if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) { 101528a77185SLu Baolu pr_info_ratelimited("IOMMU: %s: PRQ overflow detected\n", 101628a77185SLu Baolu iommu->name); 101728a77185SLu Baolu head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 101828a77185SLu Baolu tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 101928a77185SLu Baolu if (head == tail) { 1020672cf6dfSJoerg Roedel writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG); 102128a77185SLu Baolu pr_info_ratelimited("IOMMU: %s: PRQ overflow cleared", 102228a77185SLu Baolu iommu->name); 102328a77185SLu Baolu } 102428a77185SLu Baolu } 1025672cf6dfSJoerg Roedel 1026672cf6dfSJoerg Roedel if (!completion_done(&iommu->prq_complete)) 1027672cf6dfSJoerg Roedel complete(&iommu->prq_complete); 1028672cf6dfSJoerg Roedel 1029672cf6dfSJoerg Roedel return IRQ_RETVAL(handled); 1030672cf6dfSJoerg Roedel } 1031672cf6dfSJoerg Roedel 103240483774SLu Baolu struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata) 1033672cf6dfSJoerg Roedel { 103440483774SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 10352a5054c6SFenghua Yu unsigned int flags = 0; 103640483774SLu Baolu struct iommu_sva *sva; 1037672cf6dfSJoerg Roedel int ret; 1038672cf6dfSJoerg Roedel 1039672cf6dfSJoerg Roedel if (drvdata) 10402a5054c6SFenghua Yu flags = *(unsigned int *)drvdata; 1041672cf6dfSJoerg Roedel 104240483774SLu Baolu if (flags & SVM_FLAG_SUPERVISOR_MODE) { 104340483774SLu Baolu if (!ecap_srs(iommu->ecap)) { 104440483774SLu Baolu dev_err(dev, "%s: Supervisor PASID not supported\n", 104540483774SLu Baolu iommu->name); 104640483774SLu Baolu return ERR_PTR(-EOPNOTSUPP); 104740483774SLu Baolu } 104840483774SLu Baolu 104940483774SLu Baolu if (mm) { 105040483774SLu Baolu dev_err(dev, "%s: Supervisor PASID with user provided mm\n", 105140483774SLu Baolu iommu->name); 105240483774SLu Baolu return ERR_PTR(-EINVAL); 105340483774SLu Baolu } 105440483774SLu Baolu 105540483774SLu Baolu mm = &init_mm; 105640483774SLu Baolu } 105740483774SLu Baolu 105840483774SLu Baolu mutex_lock(&pasid_mutex); 105940483774SLu Baolu ret = intel_svm_alloc_pasid(dev, mm, flags); 106040483774SLu Baolu if (ret) { 106140483774SLu Baolu mutex_unlock(&pasid_mutex); 106240483774SLu Baolu return ERR_PTR(ret); 106340483774SLu Baolu } 106440483774SLu Baolu 106540483774SLu Baolu sva = intel_svm_bind_mm(iommu, dev, mm, flags); 106640483774SLu Baolu if (IS_ERR_OR_NULL(sva)) 106740483774SLu Baolu intel_svm_free_pasid(mm); 1068672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1069672cf6dfSJoerg Roedel 1070672cf6dfSJoerg Roedel return sva; 1071672cf6dfSJoerg Roedel } 1072672cf6dfSJoerg Roedel 1073672cf6dfSJoerg Roedel void intel_svm_unbind(struct iommu_sva *sva) 1074672cf6dfSJoerg Roedel { 107540483774SLu Baolu struct intel_svm_dev *sdev = to_intel_svm_dev(sva); 1076672cf6dfSJoerg Roedel 1077672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1078672cf6dfSJoerg Roedel intel_svm_unbind_mm(sdev->dev, sdev->pasid); 1079672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1080672cf6dfSJoerg Roedel } 1081672cf6dfSJoerg Roedel 1082c7b6bac9SFenghua Yu u32 intel_svm_get_pasid(struct iommu_sva *sva) 1083672cf6dfSJoerg Roedel { 1084672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1085c7b6bac9SFenghua Yu u32 pasid; 1086672cf6dfSJoerg Roedel 1087672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1088672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1089672cf6dfSJoerg Roedel pasid = sdev->pasid; 1090672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1091672cf6dfSJoerg Roedel 1092672cf6dfSJoerg Roedel return pasid; 1093672cf6dfSJoerg Roedel } 10948b737121SLu Baolu 10958b737121SLu Baolu int intel_svm_page_response(struct device *dev, 10968b737121SLu Baolu struct iommu_fault_event *evt, 10978b737121SLu Baolu struct iommu_page_response *msg) 10988b737121SLu Baolu { 10998b737121SLu Baolu struct iommu_fault_page_request *prm; 11008b737121SLu Baolu struct intel_svm_dev *sdev = NULL; 11018b737121SLu Baolu struct intel_svm *svm = NULL; 11028b737121SLu Baolu struct intel_iommu *iommu; 11038b737121SLu Baolu bool private_present; 11048b737121SLu Baolu bool pasid_present; 11058b737121SLu Baolu bool last_page; 11068b737121SLu Baolu u8 bus, devfn; 11078b737121SLu Baolu int ret = 0; 11088b737121SLu Baolu u16 sid; 11098b737121SLu Baolu 11108b737121SLu Baolu if (!dev || !dev_is_pci(dev)) 11118b737121SLu Baolu return -ENODEV; 11128b737121SLu Baolu 11138b737121SLu Baolu iommu = device_to_iommu(dev, &bus, &devfn); 11148b737121SLu Baolu if (!iommu) 11158b737121SLu Baolu return -ENODEV; 11168b737121SLu Baolu 11178b737121SLu Baolu if (!msg || !evt) 11188b737121SLu Baolu return -EINVAL; 11198b737121SLu Baolu 11208b737121SLu Baolu mutex_lock(&pasid_mutex); 11218b737121SLu Baolu 11228b737121SLu Baolu prm = &evt->fault.prm; 11238b737121SLu Baolu sid = PCI_DEVID(bus, devfn); 11248b737121SLu Baolu pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 11258b737121SLu Baolu private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 11268b737121SLu Baolu last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 11278b737121SLu Baolu 11288b737121SLu Baolu if (!pasid_present) { 11298b737121SLu Baolu ret = -EINVAL; 11308b737121SLu Baolu goto out; 11318b737121SLu Baolu } 11328b737121SLu Baolu 11338b737121SLu Baolu if (prm->pasid == 0 || prm->pasid >= PASID_MAX) { 11348b737121SLu Baolu ret = -EINVAL; 11358b737121SLu Baolu goto out; 11368b737121SLu Baolu } 11378b737121SLu Baolu 11388b737121SLu Baolu ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev); 11398b737121SLu Baolu if (ret || !sdev) { 11408b737121SLu Baolu ret = -ENODEV; 11418b737121SLu Baolu goto out; 11428b737121SLu Baolu } 11438b737121SLu Baolu 11448b737121SLu Baolu /* 11458b737121SLu Baolu * For responses from userspace, need to make sure that the 11468b737121SLu Baolu * pasid has been bound to its mm. 11478b737121SLu Baolu */ 11488b737121SLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 11498b737121SLu Baolu struct mm_struct *mm; 11508b737121SLu Baolu 11518b737121SLu Baolu mm = get_task_mm(current); 11528b737121SLu Baolu if (!mm) { 11538b737121SLu Baolu ret = -EINVAL; 11548b737121SLu Baolu goto out; 11558b737121SLu Baolu } 11568b737121SLu Baolu 11578b737121SLu Baolu if (mm != svm->mm) { 11588b737121SLu Baolu ret = -ENODEV; 11598b737121SLu Baolu mmput(mm); 11608b737121SLu Baolu goto out; 11618b737121SLu Baolu } 11628b737121SLu Baolu 11638b737121SLu Baolu mmput(mm); 11648b737121SLu Baolu } 11658b737121SLu Baolu 11668b737121SLu Baolu /* 11678b737121SLu Baolu * Per VT-d spec. v3.0 ch7.7, system software must respond 11688b737121SLu Baolu * with page group response if private data is present (PDP) 11698b737121SLu Baolu * or last page in group (LPIG) bit is set. This is an 11708b737121SLu Baolu * additional VT-d requirement beyond PCI ATS spec. 11718b737121SLu Baolu */ 11728b737121SLu Baolu if (last_page || private_present) { 11738b737121SLu Baolu struct qi_desc desc; 11748b737121SLu Baolu 11758b737121SLu Baolu desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) | 11768b737121SLu Baolu QI_PGRP_PASID_P(pasid_present) | 11778b737121SLu Baolu QI_PGRP_PDP(private_present) | 11788b737121SLu Baolu QI_PGRP_RESP_CODE(msg->code) | 11798b737121SLu Baolu QI_PGRP_RESP_TYPE; 11808b737121SLu Baolu desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page); 11818b737121SLu Baolu desc.qw2 = 0; 11828b737121SLu Baolu desc.qw3 = 0; 11838b737121SLu Baolu if (private_present) 11848b737121SLu Baolu memcpy(&desc.qw2, prm->private_data, 11858b737121SLu Baolu sizeof(prm->private_data)); 11868b737121SLu Baolu 11878b737121SLu Baolu qi_submit_sync(iommu, &desc, 1, 0); 11888b737121SLu Baolu } 11898b737121SLu Baolu out: 11908b737121SLu Baolu mutex_unlock(&pasid_mutex); 11918b737121SLu Baolu return ret; 11928b737121SLu Baolu } 1193