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> 20672cf6dfSJoerg Roedel #include <linux/ioasid.h> 21672cf6dfSJoerg Roedel #include <asm/page.h> 2220f0afd1SFenghua Yu #include <asm/fpu/api.h> 23672cf6dfSJoerg Roedel 2402f3effdSLu Baolu #include "pasid.h" 25672cf6dfSJoerg Roedel 26672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d); 27c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid); 28672cf6dfSJoerg Roedel 29672cf6dfSJoerg Roedel #define PRQ_ORDER 0 30672cf6dfSJoerg Roedel 31672cf6dfSJoerg Roedel int intel_svm_enable_prq(struct intel_iommu *iommu) 32672cf6dfSJoerg Roedel { 33672cf6dfSJoerg Roedel struct page *pages; 34672cf6dfSJoerg Roedel int irq, ret; 35672cf6dfSJoerg Roedel 36672cf6dfSJoerg Roedel pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER); 37672cf6dfSJoerg Roedel if (!pages) { 38672cf6dfSJoerg Roedel pr_warn("IOMMU: %s: Failed to allocate page request queue\n", 39672cf6dfSJoerg Roedel iommu->name); 40672cf6dfSJoerg Roedel return -ENOMEM; 41672cf6dfSJoerg Roedel } 42672cf6dfSJoerg Roedel iommu->prq = page_address(pages); 43672cf6dfSJoerg Roedel 44672cf6dfSJoerg Roedel irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu); 45672cf6dfSJoerg Roedel if (irq <= 0) { 46672cf6dfSJoerg Roedel pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n", 47672cf6dfSJoerg Roedel iommu->name); 48672cf6dfSJoerg Roedel ret = -EINVAL; 49672cf6dfSJoerg Roedel err: 50672cf6dfSJoerg Roedel free_pages((unsigned long)iommu->prq, PRQ_ORDER); 51672cf6dfSJoerg Roedel iommu->prq = NULL; 52672cf6dfSJoerg Roedel return ret; 53672cf6dfSJoerg Roedel } 54672cf6dfSJoerg Roedel iommu->pr_irq = irq; 55672cf6dfSJoerg Roedel 56672cf6dfSJoerg Roedel snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id); 57672cf6dfSJoerg Roedel 58672cf6dfSJoerg Roedel ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT, 59672cf6dfSJoerg Roedel iommu->prq_name, iommu); 60672cf6dfSJoerg Roedel if (ret) { 61672cf6dfSJoerg Roedel pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n", 62672cf6dfSJoerg Roedel iommu->name); 63672cf6dfSJoerg Roedel dmar_free_hwirq(irq); 64672cf6dfSJoerg Roedel iommu->pr_irq = 0; 65672cf6dfSJoerg Roedel goto err; 66672cf6dfSJoerg Roedel } 67672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); 68672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); 69672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER); 70672cf6dfSJoerg Roedel 71672cf6dfSJoerg Roedel init_completion(&iommu->prq_complete); 72672cf6dfSJoerg Roedel 73672cf6dfSJoerg Roedel return 0; 74672cf6dfSJoerg Roedel } 75672cf6dfSJoerg Roedel 76672cf6dfSJoerg Roedel int intel_svm_finish_prq(struct intel_iommu *iommu) 77672cf6dfSJoerg Roedel { 78672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); 79672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); 80672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL); 81672cf6dfSJoerg Roedel 82672cf6dfSJoerg Roedel if (iommu->pr_irq) { 83672cf6dfSJoerg Roedel free_irq(iommu->pr_irq, iommu); 84672cf6dfSJoerg Roedel dmar_free_hwirq(iommu->pr_irq); 85672cf6dfSJoerg Roedel iommu->pr_irq = 0; 86672cf6dfSJoerg Roedel } 87672cf6dfSJoerg Roedel 88672cf6dfSJoerg Roedel free_pages((unsigned long)iommu->prq, PRQ_ORDER); 89672cf6dfSJoerg Roedel iommu->prq = NULL; 90672cf6dfSJoerg Roedel 91672cf6dfSJoerg Roedel return 0; 92672cf6dfSJoerg Roedel } 93672cf6dfSJoerg Roedel 94672cf6dfSJoerg Roedel static inline bool intel_svm_capable(struct intel_iommu *iommu) 95672cf6dfSJoerg Roedel { 96672cf6dfSJoerg Roedel return iommu->flags & VTD_FLAG_SVM_CAPABLE; 97672cf6dfSJoerg Roedel } 98672cf6dfSJoerg Roedel 99672cf6dfSJoerg Roedel void intel_svm_check(struct intel_iommu *iommu) 100672cf6dfSJoerg Roedel { 101672cf6dfSJoerg Roedel if (!pasid_supported(iommu)) 102672cf6dfSJoerg Roedel return; 103672cf6dfSJoerg Roedel 104672cf6dfSJoerg Roedel if (cpu_feature_enabled(X86_FEATURE_GBPAGES) && 105672cf6dfSJoerg Roedel !cap_fl1gp_support(iommu->cap)) { 106672cf6dfSJoerg Roedel pr_err("%s SVM disabled, incompatible 1GB page capability\n", 107672cf6dfSJoerg Roedel iommu->name); 108672cf6dfSJoerg Roedel return; 109672cf6dfSJoerg Roedel } 110672cf6dfSJoerg Roedel 111672cf6dfSJoerg Roedel if (cpu_feature_enabled(X86_FEATURE_LA57) && 112672cf6dfSJoerg Roedel !cap_5lp_support(iommu->cap)) { 113672cf6dfSJoerg Roedel pr_err("%s SVM disabled, incompatible paging mode\n", 114672cf6dfSJoerg Roedel iommu->name); 115672cf6dfSJoerg Roedel return; 116672cf6dfSJoerg Roedel } 117672cf6dfSJoerg Roedel 118672cf6dfSJoerg Roedel iommu->flags |= VTD_FLAG_SVM_CAPABLE; 119672cf6dfSJoerg Roedel } 120672cf6dfSJoerg Roedel 121672cf6dfSJoerg Roedel static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev, 122672cf6dfSJoerg Roedel unsigned long address, unsigned long pages, int ih) 123672cf6dfSJoerg Roedel { 124672cf6dfSJoerg Roedel struct qi_desc desc; 125672cf6dfSJoerg Roedel 126672cf6dfSJoerg Roedel if (pages == -1) { 127672cf6dfSJoerg Roedel desc.qw0 = QI_EIOTLB_PASID(svm->pasid) | 128672cf6dfSJoerg Roedel QI_EIOTLB_DID(sdev->did) | 129672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | 130672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 131672cf6dfSJoerg Roedel desc.qw1 = 0; 132672cf6dfSJoerg Roedel } else { 133672cf6dfSJoerg Roedel int mask = ilog2(__roundup_pow_of_two(pages)); 134672cf6dfSJoerg Roedel 135672cf6dfSJoerg Roedel desc.qw0 = QI_EIOTLB_PASID(svm->pasid) | 136672cf6dfSJoerg Roedel QI_EIOTLB_DID(sdev->did) | 137672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | 138672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 139672cf6dfSJoerg Roedel desc.qw1 = QI_EIOTLB_ADDR(address) | 140672cf6dfSJoerg Roedel QI_EIOTLB_IH(ih) | 141672cf6dfSJoerg Roedel QI_EIOTLB_AM(mask); 142672cf6dfSJoerg Roedel } 143672cf6dfSJoerg Roedel desc.qw2 = 0; 144672cf6dfSJoerg Roedel desc.qw3 = 0; 145672cf6dfSJoerg Roedel qi_submit_sync(svm->iommu, &desc, 1, 0); 146672cf6dfSJoerg Roedel 147672cf6dfSJoerg Roedel if (sdev->dev_iotlb) { 148672cf6dfSJoerg Roedel desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) | 149672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SID(sdev->sid) | 150672cf6dfSJoerg Roedel QI_DEV_EIOTLB_QDEP(sdev->qdep) | 151672cf6dfSJoerg Roedel QI_DEIOTLB_TYPE; 152672cf6dfSJoerg Roedel if (pages == -1) { 153672cf6dfSJoerg Roedel desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | 154672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SIZE; 155672cf6dfSJoerg Roedel } else if (pages > 1) { 156672cf6dfSJoerg Roedel /* The least significant zero bit indicates the size. So, 157672cf6dfSJoerg Roedel * for example, an "address" value of 0x12345f000 will 158672cf6dfSJoerg Roedel * flush from 0x123440000 to 0x12347ffff (256KiB). */ 159672cf6dfSJoerg Roedel unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT); 160672cf6dfSJoerg Roedel unsigned long mask = __rounddown_pow_of_two(address ^ last); 161672cf6dfSJoerg Roedel 162672cf6dfSJoerg Roedel desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) | 163672cf6dfSJoerg Roedel (mask - 1)) | QI_DEV_EIOTLB_SIZE; 164672cf6dfSJoerg Roedel } else { 165672cf6dfSJoerg Roedel desc.qw1 = QI_DEV_EIOTLB_ADDR(address); 166672cf6dfSJoerg Roedel } 167672cf6dfSJoerg Roedel desc.qw2 = 0; 168672cf6dfSJoerg Roedel desc.qw3 = 0; 169672cf6dfSJoerg Roedel qi_submit_sync(svm->iommu, &desc, 1, 0); 170672cf6dfSJoerg Roedel } 171672cf6dfSJoerg Roedel } 172672cf6dfSJoerg Roedel 173672cf6dfSJoerg Roedel static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address, 174672cf6dfSJoerg Roedel unsigned long pages, int ih) 175672cf6dfSJoerg Roedel { 176672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 177672cf6dfSJoerg Roedel 178672cf6dfSJoerg Roedel rcu_read_lock(); 179672cf6dfSJoerg Roedel list_for_each_entry_rcu(sdev, &svm->devs, list) 180672cf6dfSJoerg Roedel intel_flush_svm_range_dev(svm, sdev, address, pages, ih); 181672cf6dfSJoerg Roedel rcu_read_unlock(); 182672cf6dfSJoerg Roedel } 183672cf6dfSJoerg Roedel 184672cf6dfSJoerg Roedel /* Pages have been freed at this point */ 185672cf6dfSJoerg Roedel static void intel_invalidate_range(struct mmu_notifier *mn, 186672cf6dfSJoerg Roedel struct mm_struct *mm, 187672cf6dfSJoerg Roedel unsigned long start, unsigned long end) 188672cf6dfSJoerg Roedel { 189672cf6dfSJoerg Roedel struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); 190672cf6dfSJoerg Roedel 191672cf6dfSJoerg Roedel intel_flush_svm_range(svm, start, 192672cf6dfSJoerg Roedel (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0); 193672cf6dfSJoerg Roedel } 194672cf6dfSJoerg Roedel 195672cf6dfSJoerg Roedel static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) 196672cf6dfSJoerg Roedel { 197672cf6dfSJoerg Roedel struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); 198672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 199672cf6dfSJoerg Roedel 200672cf6dfSJoerg Roedel /* This might end up being called from exit_mmap(), *before* the page 201672cf6dfSJoerg Roedel * tables are cleared. And __mmu_notifier_release() will delete us from 202672cf6dfSJoerg Roedel * the list of notifiers so that our invalidate_range() callback doesn't 203672cf6dfSJoerg Roedel * get called when the page tables are cleared. So we need to protect 204672cf6dfSJoerg Roedel * against hardware accessing those page tables. 205672cf6dfSJoerg Roedel * 206672cf6dfSJoerg Roedel * We do it by clearing the entry in the PASID table and then flushing 207672cf6dfSJoerg Roedel * the IOTLB and the PASID table caches. This might upset hardware; 208672cf6dfSJoerg Roedel * perhaps we'll want to point the PASID to a dummy PGD (like the zero 209672cf6dfSJoerg Roedel * page) so that we end up taking a fault that the hardware really 210672cf6dfSJoerg Roedel * *has* to handle gracefully without affecting other processes. 211672cf6dfSJoerg Roedel */ 212672cf6dfSJoerg Roedel rcu_read_lock(); 213672cf6dfSJoerg Roedel list_for_each_entry_rcu(sdev, &svm->devs, list) 214672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(svm->iommu, sdev->dev, 215672cf6dfSJoerg Roedel svm->pasid, true); 216672cf6dfSJoerg Roedel rcu_read_unlock(); 217672cf6dfSJoerg Roedel 218672cf6dfSJoerg Roedel } 219672cf6dfSJoerg Roedel 220672cf6dfSJoerg Roedel static const struct mmu_notifier_ops intel_mmuops = { 221672cf6dfSJoerg Roedel .release = intel_mm_release, 222672cf6dfSJoerg Roedel .invalidate_range = intel_invalidate_range, 223672cf6dfSJoerg Roedel }; 224672cf6dfSJoerg Roedel 225672cf6dfSJoerg Roedel static DEFINE_MUTEX(pasid_mutex); 226672cf6dfSJoerg Roedel static LIST_HEAD(global_svm_list); 227672cf6dfSJoerg Roedel 228672cf6dfSJoerg Roedel #define for_each_svm_dev(sdev, svm, d) \ 229672cf6dfSJoerg Roedel list_for_each_entry((sdev), &(svm)->devs, list) \ 230672cf6dfSJoerg Roedel if ((d) != (sdev)->dev) {} else 231672cf6dfSJoerg Roedel 23219abcf70SLu Baolu static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid, 23319abcf70SLu Baolu struct intel_svm **rsvm, 23419abcf70SLu Baolu struct intel_svm_dev **rsdev) 23519abcf70SLu Baolu { 23619abcf70SLu Baolu struct intel_svm_dev *d, *sdev = NULL; 23719abcf70SLu Baolu struct intel_svm *svm; 23819abcf70SLu Baolu 23919abcf70SLu Baolu /* The caller should hold the pasid_mutex lock */ 24019abcf70SLu Baolu if (WARN_ON(!mutex_is_locked(&pasid_mutex))) 24119abcf70SLu Baolu return -EINVAL; 24219abcf70SLu Baolu 24319abcf70SLu Baolu if (pasid == INVALID_IOASID || pasid >= PASID_MAX) 24419abcf70SLu Baolu return -EINVAL; 24519abcf70SLu Baolu 24619abcf70SLu Baolu svm = ioasid_find(NULL, pasid, NULL); 24719abcf70SLu Baolu if (IS_ERR(svm)) 24819abcf70SLu Baolu return PTR_ERR(svm); 24919abcf70SLu Baolu 25019abcf70SLu Baolu if (!svm) 25119abcf70SLu Baolu goto out; 25219abcf70SLu Baolu 25319abcf70SLu Baolu /* 25419abcf70SLu Baolu * If we found svm for the PASID, there must be at least one device 25519abcf70SLu Baolu * bond. 25619abcf70SLu Baolu */ 25719abcf70SLu Baolu if (WARN_ON(list_empty(&svm->devs))) 25819abcf70SLu Baolu return -EINVAL; 25919abcf70SLu Baolu 26019abcf70SLu Baolu rcu_read_lock(); 26119abcf70SLu Baolu list_for_each_entry_rcu(d, &svm->devs, list) { 26219abcf70SLu Baolu if (d->dev == dev) { 26319abcf70SLu Baolu sdev = d; 26419abcf70SLu Baolu break; 26519abcf70SLu Baolu } 26619abcf70SLu Baolu } 26719abcf70SLu Baolu rcu_read_unlock(); 26819abcf70SLu Baolu 26919abcf70SLu Baolu out: 27019abcf70SLu Baolu *rsvm = svm; 27119abcf70SLu Baolu *rsdev = sdev; 27219abcf70SLu Baolu 27319abcf70SLu Baolu return 0; 27419abcf70SLu Baolu } 27519abcf70SLu Baolu 276672cf6dfSJoerg Roedel int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, 277672cf6dfSJoerg Roedel struct iommu_gpasid_bind_data *data) 278672cf6dfSJoerg Roedel { 279dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 28019abcf70SLu Baolu struct intel_svm_dev *sdev = NULL; 281672cf6dfSJoerg Roedel struct dmar_domain *dmar_domain; 282eea4e29aSLiu Yi L struct device_domain_info *info; 28319abcf70SLu Baolu struct intel_svm *svm = NULL; 284672cf6dfSJoerg Roedel int ret = 0; 285672cf6dfSJoerg Roedel 286672cf6dfSJoerg Roedel if (WARN_ON(!iommu) || !data) 287672cf6dfSJoerg Roedel return -EINVAL; 288672cf6dfSJoerg Roedel 2896278eecbSJacob Pan if (data->format != IOMMU_PASID_FORMAT_INTEL_VTD) 2906278eecbSJacob Pan return -EINVAL; 2916278eecbSJacob Pan 2926278eecbSJacob Pan /* IOMMU core ensures argsz is more than the start of the union */ 2936278eecbSJacob Pan if (data->argsz < offsetofend(struct iommu_gpasid_bind_data, vendor.vtd)) 2946278eecbSJacob Pan return -EINVAL; 2956278eecbSJacob Pan 2966278eecbSJacob Pan /* Make sure no undefined flags are used in vendor data */ 2976278eecbSJacob Pan if (data->vendor.vtd.flags & ~(IOMMU_SVA_VTD_GPASID_LAST - 1)) 298672cf6dfSJoerg Roedel return -EINVAL; 299672cf6dfSJoerg Roedel 300672cf6dfSJoerg Roedel if (!dev_is_pci(dev)) 301672cf6dfSJoerg Roedel return -ENOTSUPP; 302672cf6dfSJoerg Roedel 303672cf6dfSJoerg Roedel /* VT-d supports devices with full 20 bit PASIDs only */ 304672cf6dfSJoerg Roedel if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX) 305672cf6dfSJoerg Roedel return -EINVAL; 306672cf6dfSJoerg Roedel 307672cf6dfSJoerg Roedel /* 308672cf6dfSJoerg Roedel * We only check host PASID range, we have no knowledge to check 309672cf6dfSJoerg Roedel * guest PASID range. 310672cf6dfSJoerg Roedel */ 311672cf6dfSJoerg Roedel if (data->hpasid <= 0 || data->hpasid >= PASID_MAX) 312672cf6dfSJoerg Roedel return -EINVAL; 313672cf6dfSJoerg Roedel 314eea4e29aSLiu Yi L info = get_domain_info(dev); 315eea4e29aSLiu Yi L if (!info) 316eea4e29aSLiu Yi L return -EINVAL; 317eea4e29aSLiu Yi L 318672cf6dfSJoerg Roedel dmar_domain = to_dmar_domain(domain); 319672cf6dfSJoerg Roedel 320672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 32119abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev); 32219abcf70SLu Baolu if (ret) 323672cf6dfSJoerg Roedel goto out; 324672cf6dfSJoerg Roedel 32519abcf70SLu Baolu if (sdev) { 326672cf6dfSJoerg Roedel /* 327d315e9e6SJacob Pan * Do not allow multiple bindings of the same device-PASID since 328d315e9e6SJacob Pan * there is only one SL page tables per PASID. We may revisit 329d315e9e6SJacob Pan * once sharing PGD across domains are supported. 330672cf6dfSJoerg Roedel */ 33119abcf70SLu Baolu dev_warn_ratelimited(dev, "Already bound with PASID %u\n", 332672cf6dfSJoerg Roedel svm->pasid); 333672cf6dfSJoerg Roedel ret = -EBUSY; 334672cf6dfSJoerg Roedel goto out; 335672cf6dfSJoerg Roedel } 33619abcf70SLu Baolu 33719abcf70SLu Baolu if (!svm) { 338672cf6dfSJoerg Roedel /* We come here when PASID has never been bond to a device. */ 339672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 340672cf6dfSJoerg Roedel if (!svm) { 341672cf6dfSJoerg Roedel ret = -ENOMEM; 342672cf6dfSJoerg Roedel goto out; 343672cf6dfSJoerg Roedel } 344672cf6dfSJoerg Roedel /* REVISIT: upper layer/VFIO can track host process that bind 345672cf6dfSJoerg Roedel * the PASID. ioasid_set = mm might be sufficient for vfio to 346672cf6dfSJoerg Roedel * check pasid VMM ownership. We can drop the following line 347672cf6dfSJoerg Roedel * once VFIO and IOASID set check is in place. 348672cf6dfSJoerg Roedel */ 349672cf6dfSJoerg Roedel svm->mm = get_task_mm(current); 350672cf6dfSJoerg Roedel svm->pasid = data->hpasid; 351672cf6dfSJoerg Roedel if (data->flags & IOMMU_SVA_GPASID_VAL) { 352672cf6dfSJoerg Roedel svm->gpasid = data->gpasid; 353672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_PASID; 354672cf6dfSJoerg Roedel } 355672cf6dfSJoerg Roedel ioasid_set_data(data->hpasid, svm); 356672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 357672cf6dfSJoerg Roedel mmput(svm->mm); 358672cf6dfSJoerg Roedel } 359672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 360672cf6dfSJoerg Roedel if (!sdev) { 361672cf6dfSJoerg Roedel ret = -ENOMEM; 362672cf6dfSJoerg Roedel goto out; 363672cf6dfSJoerg Roedel } 364672cf6dfSJoerg Roedel sdev->dev = dev; 365eea4e29aSLiu Yi L sdev->sid = PCI_DEVID(info->bus, info->devfn); 366672cf6dfSJoerg Roedel 367672cf6dfSJoerg Roedel /* Only count users if device has aux domains */ 368672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 369672cf6dfSJoerg Roedel sdev->users = 1; 370672cf6dfSJoerg Roedel 371672cf6dfSJoerg Roedel /* Set up device context entry for PASID if not enabled already */ 372672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, sdev->dev); 373672cf6dfSJoerg Roedel if (ret) { 374672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to enable PASID capability\n"); 375672cf6dfSJoerg Roedel kfree(sdev); 376672cf6dfSJoerg Roedel goto out; 377672cf6dfSJoerg Roedel } 378672cf6dfSJoerg Roedel 379672cf6dfSJoerg Roedel /* 380672cf6dfSJoerg Roedel * PASID table is per device for better security. Therefore, for 381672cf6dfSJoerg Roedel * each bind of a new device even with an existing PASID, we need to 382672cf6dfSJoerg Roedel * call the nested mode setup function here. 383672cf6dfSJoerg Roedel */ 384672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 385672cf6dfSJoerg Roedel ret = intel_pasid_setup_nested(iommu, dev, 386672cf6dfSJoerg Roedel (pgd_t *)(uintptr_t)data->gpgd, 3878d3bb3b8SJacob Pan data->hpasid, &data->vendor.vtd, dmar_domain, 388672cf6dfSJoerg Roedel data->addr_width); 389672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 390672cf6dfSJoerg Roedel if (ret) { 391672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n", 392672cf6dfSJoerg Roedel data->hpasid, ret); 393672cf6dfSJoerg Roedel /* 394672cf6dfSJoerg Roedel * PASID entry should be in cleared state if nested mode 395672cf6dfSJoerg Roedel * set up failed. So we only need to clear IOASID tracking 396672cf6dfSJoerg Roedel * data such that free call will succeed. 397672cf6dfSJoerg Roedel */ 398672cf6dfSJoerg Roedel kfree(sdev); 399672cf6dfSJoerg Roedel goto out; 400672cf6dfSJoerg Roedel } 401672cf6dfSJoerg Roedel 402672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_MODE; 403672cf6dfSJoerg Roedel 404672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 405672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 406672cf6dfSJoerg Roedel out: 407672cf6dfSJoerg Roedel if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) { 408672cf6dfSJoerg Roedel ioasid_set_data(data->hpasid, NULL); 409672cf6dfSJoerg Roedel kfree(svm); 410672cf6dfSJoerg Roedel } 411672cf6dfSJoerg Roedel 412672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 413672cf6dfSJoerg Roedel return ret; 414672cf6dfSJoerg Roedel } 415672cf6dfSJoerg Roedel 416c7b6bac9SFenghua Yu int intel_svm_unbind_gpasid(struct device *dev, u32 pasid) 417672cf6dfSJoerg Roedel { 418dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 419672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 420672cf6dfSJoerg Roedel struct intel_svm *svm; 42119abcf70SLu Baolu int ret; 422672cf6dfSJoerg Roedel 423672cf6dfSJoerg Roedel if (WARN_ON(!iommu)) 424672cf6dfSJoerg Roedel return -EINVAL; 425672cf6dfSJoerg Roedel 426672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 42719abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 42819abcf70SLu Baolu if (ret) 429672cf6dfSJoerg Roedel goto out; 430672cf6dfSJoerg Roedel 43119abcf70SLu Baolu if (sdev) { 432672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 433672cf6dfSJoerg Roedel sdev->users--; 434672cf6dfSJoerg Roedel if (!sdev->users) { 435672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 436672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 437672cf6dfSJoerg Roedel svm->pasid, false); 438672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 439672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 440672cf6dfSJoerg Roedel 441672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 442672cf6dfSJoerg Roedel /* 443672cf6dfSJoerg Roedel * We do not free the IOASID here in that 444672cf6dfSJoerg Roedel * IOMMU driver did not allocate it. 445672cf6dfSJoerg Roedel * Unlike native SVM, IOASID for guest use was 446672cf6dfSJoerg Roedel * allocated prior to the bind call. 447672cf6dfSJoerg Roedel * In any case, if the free call comes before 448672cf6dfSJoerg Roedel * the unbind, IOMMU driver will get notified 449672cf6dfSJoerg Roedel * and perform cleanup. 450672cf6dfSJoerg Roedel */ 451672cf6dfSJoerg Roedel ioasid_set_data(pasid, NULL); 452672cf6dfSJoerg Roedel kfree(svm); 453672cf6dfSJoerg Roedel } 454672cf6dfSJoerg Roedel } 455672cf6dfSJoerg Roedel } 456672cf6dfSJoerg Roedel out: 457672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 458672cf6dfSJoerg Roedel return ret; 459672cf6dfSJoerg Roedel } 460672cf6dfSJoerg Roedel 46120f0afd1SFenghua Yu static void _load_pasid(void *unused) 46220f0afd1SFenghua Yu { 46320f0afd1SFenghua Yu update_pasid(); 46420f0afd1SFenghua Yu } 46520f0afd1SFenghua Yu 46620f0afd1SFenghua Yu static void load_pasid(struct mm_struct *mm, u32 pasid) 46720f0afd1SFenghua Yu { 46820f0afd1SFenghua Yu mutex_lock(&mm->context.lock); 46920f0afd1SFenghua Yu 47020f0afd1SFenghua Yu /* Synchronize with READ_ONCE in update_pasid(). */ 47120f0afd1SFenghua Yu smp_store_release(&mm->pasid, pasid); 47220f0afd1SFenghua Yu 47320f0afd1SFenghua Yu /* Update PASID MSR on all CPUs running the mm's tasks. */ 47420f0afd1SFenghua Yu on_each_cpu_mask(mm_cpumask(mm), _load_pasid, NULL, true); 47520f0afd1SFenghua Yu 47620f0afd1SFenghua Yu mutex_unlock(&mm->context.lock); 47720f0afd1SFenghua Yu } 47820f0afd1SFenghua Yu 479672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex, mm reference */ 480672cf6dfSJoerg Roedel static int 4812a5054c6SFenghua Yu intel_svm_bind_mm(struct device *dev, unsigned int flags, 4822a5054c6SFenghua Yu struct svm_dev_ops *ops, 483672cf6dfSJoerg Roedel struct mm_struct *mm, struct intel_svm_dev **sd) 484672cf6dfSJoerg Roedel { 485dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 486672cf6dfSJoerg Roedel struct device_domain_info *info; 487672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 488672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 489672cf6dfSJoerg Roedel int pasid_max; 490672cf6dfSJoerg Roedel int ret; 491672cf6dfSJoerg Roedel 492672cf6dfSJoerg Roedel if (!iommu || dmar_disabled) 493672cf6dfSJoerg Roedel return -EINVAL; 494672cf6dfSJoerg Roedel 495672cf6dfSJoerg Roedel if (!intel_svm_capable(iommu)) 496672cf6dfSJoerg Roedel return -ENOTSUPP; 497672cf6dfSJoerg Roedel 498672cf6dfSJoerg Roedel if (dev_is_pci(dev)) { 499672cf6dfSJoerg Roedel pasid_max = pci_max_pasids(to_pci_dev(dev)); 500672cf6dfSJoerg Roedel if (pasid_max < 0) 501672cf6dfSJoerg Roedel return -EINVAL; 502672cf6dfSJoerg Roedel } else 503672cf6dfSJoerg Roedel pasid_max = 1 << 20; 504672cf6dfSJoerg Roedel 505672cf6dfSJoerg Roedel /* Bind supervisor PASID shuld have mm = NULL */ 506672cf6dfSJoerg Roedel if (flags & SVM_FLAG_SUPERVISOR_MODE) { 507672cf6dfSJoerg Roedel if (!ecap_srs(iommu->ecap) || mm) { 508672cf6dfSJoerg Roedel pr_err("Supervisor PASID with user provided mm.\n"); 509672cf6dfSJoerg Roedel return -EINVAL; 510672cf6dfSJoerg Roedel } 511672cf6dfSJoerg Roedel } 512672cf6dfSJoerg Roedel 513672cf6dfSJoerg Roedel if (!(flags & SVM_FLAG_PRIVATE_PASID)) { 514672cf6dfSJoerg Roedel struct intel_svm *t; 515672cf6dfSJoerg Roedel 516672cf6dfSJoerg Roedel list_for_each_entry(t, &global_svm_list, list) { 517672cf6dfSJoerg Roedel if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID)) 518672cf6dfSJoerg Roedel continue; 519672cf6dfSJoerg Roedel 520672cf6dfSJoerg Roedel svm = t; 521672cf6dfSJoerg Roedel if (svm->pasid >= pasid_max) { 522672cf6dfSJoerg Roedel dev_warn(dev, 523672cf6dfSJoerg Roedel "Limited PASID width. Cannot use existing PASID %d\n", 524672cf6dfSJoerg Roedel svm->pasid); 525672cf6dfSJoerg Roedel ret = -ENOSPC; 526672cf6dfSJoerg Roedel goto out; 527672cf6dfSJoerg Roedel } 528672cf6dfSJoerg Roedel 529672cf6dfSJoerg Roedel /* Find the matching device in svm list */ 530672cf6dfSJoerg Roedel for_each_svm_dev(sdev, svm, dev) { 531672cf6dfSJoerg Roedel if (sdev->ops != ops) { 532672cf6dfSJoerg Roedel ret = -EBUSY; 533672cf6dfSJoerg Roedel goto out; 534672cf6dfSJoerg Roedel } 535672cf6dfSJoerg Roedel sdev->users++; 536672cf6dfSJoerg Roedel goto success; 537672cf6dfSJoerg Roedel } 538672cf6dfSJoerg Roedel 539672cf6dfSJoerg Roedel break; 540672cf6dfSJoerg Roedel } 541672cf6dfSJoerg Roedel } 542672cf6dfSJoerg Roedel 543672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 544672cf6dfSJoerg Roedel if (!sdev) { 545672cf6dfSJoerg Roedel ret = -ENOMEM; 546672cf6dfSJoerg Roedel goto out; 547672cf6dfSJoerg Roedel } 548672cf6dfSJoerg Roedel sdev->dev = dev; 549672cf6dfSJoerg Roedel 550672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, dev); 551672cf6dfSJoerg Roedel if (ret) { 552672cf6dfSJoerg Roedel kfree(sdev); 553672cf6dfSJoerg Roedel goto out; 554672cf6dfSJoerg Roedel } 555672cf6dfSJoerg Roedel 556672cf6dfSJoerg Roedel info = get_domain_info(dev); 557672cf6dfSJoerg Roedel sdev->did = FLPT_DEFAULT_DID; 558672cf6dfSJoerg Roedel sdev->sid = PCI_DEVID(info->bus, info->devfn); 559672cf6dfSJoerg Roedel if (info->ats_enabled) { 560672cf6dfSJoerg Roedel sdev->dev_iotlb = 1; 561672cf6dfSJoerg Roedel sdev->qdep = info->ats_qdep; 562672cf6dfSJoerg Roedel if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) 563672cf6dfSJoerg Roedel sdev->qdep = 0; 564672cf6dfSJoerg Roedel } 565672cf6dfSJoerg Roedel 566672cf6dfSJoerg Roedel /* Finish the setup now we know we're keeping it */ 567672cf6dfSJoerg Roedel sdev->users = 1; 568672cf6dfSJoerg Roedel sdev->ops = ops; 569672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 570672cf6dfSJoerg Roedel 571672cf6dfSJoerg Roedel if (!svm) { 572672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 573672cf6dfSJoerg Roedel if (!svm) { 574672cf6dfSJoerg Roedel ret = -ENOMEM; 575672cf6dfSJoerg Roedel kfree(sdev); 576672cf6dfSJoerg Roedel goto out; 577672cf6dfSJoerg Roedel } 578672cf6dfSJoerg Roedel svm->iommu = iommu; 579672cf6dfSJoerg Roedel 580672cf6dfSJoerg Roedel if (pasid_max > intel_pasid_max_id) 581672cf6dfSJoerg Roedel pasid_max = intel_pasid_max_id; 582672cf6dfSJoerg Roedel 583672cf6dfSJoerg Roedel /* Do not use PASID 0, reserved for RID to PASID */ 584672cf6dfSJoerg Roedel svm->pasid = ioasid_alloc(NULL, PASID_MIN, 585672cf6dfSJoerg Roedel pasid_max - 1, svm); 586672cf6dfSJoerg Roedel if (svm->pasid == INVALID_IOASID) { 587672cf6dfSJoerg Roedel kfree(svm); 588672cf6dfSJoerg Roedel kfree(sdev); 589672cf6dfSJoerg Roedel ret = -ENOSPC; 590672cf6dfSJoerg Roedel goto out; 591672cf6dfSJoerg Roedel } 592672cf6dfSJoerg Roedel svm->notifier.ops = &intel_mmuops; 593672cf6dfSJoerg Roedel svm->mm = mm; 594672cf6dfSJoerg Roedel svm->flags = flags; 595672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 596672cf6dfSJoerg Roedel INIT_LIST_HEAD(&svm->list); 597672cf6dfSJoerg Roedel ret = -ENOMEM; 598672cf6dfSJoerg Roedel if (mm) { 599672cf6dfSJoerg Roedel ret = mmu_notifier_register(&svm->notifier, mm); 600672cf6dfSJoerg Roedel if (ret) { 601672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 602672cf6dfSJoerg Roedel kfree(svm); 603672cf6dfSJoerg Roedel kfree(sdev); 604672cf6dfSJoerg Roedel goto out; 605672cf6dfSJoerg Roedel } 606672cf6dfSJoerg Roedel } 607672cf6dfSJoerg Roedel 608672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 609672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 610672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 611672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 612672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 613672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 614672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 615672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 616672cf6dfSJoerg Roedel if (ret) { 617672cf6dfSJoerg Roedel if (mm) 618672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, mm); 619672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 620672cf6dfSJoerg Roedel kfree(svm); 621672cf6dfSJoerg Roedel kfree(sdev); 622672cf6dfSJoerg Roedel goto out; 623672cf6dfSJoerg Roedel } 624672cf6dfSJoerg Roedel 625672cf6dfSJoerg Roedel list_add_tail(&svm->list, &global_svm_list); 62620f0afd1SFenghua Yu if (mm) { 62720f0afd1SFenghua Yu /* The newly allocated pasid is loaded to the mm. */ 62820f0afd1SFenghua Yu load_pasid(mm, svm->pasid); 62920f0afd1SFenghua Yu } 630672cf6dfSJoerg Roedel } else { 631672cf6dfSJoerg Roedel /* 632672cf6dfSJoerg Roedel * Binding a new device with existing PASID, need to setup 633672cf6dfSJoerg Roedel * the PASID entry. 634672cf6dfSJoerg Roedel */ 635672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 636672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 637672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 638672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 639672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 640672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 641672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 642672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 643672cf6dfSJoerg Roedel if (ret) { 644672cf6dfSJoerg Roedel kfree(sdev); 645672cf6dfSJoerg Roedel goto out; 646672cf6dfSJoerg Roedel } 647672cf6dfSJoerg Roedel } 648672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 649672cf6dfSJoerg Roedel success: 650672cf6dfSJoerg Roedel sdev->pasid = svm->pasid; 651672cf6dfSJoerg Roedel sdev->sva.dev = dev; 652672cf6dfSJoerg Roedel if (sd) 653672cf6dfSJoerg Roedel *sd = sdev; 654672cf6dfSJoerg Roedel ret = 0; 655672cf6dfSJoerg Roedel out: 656672cf6dfSJoerg Roedel return ret; 657672cf6dfSJoerg Roedel } 658672cf6dfSJoerg Roedel 659672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex */ 660c7b6bac9SFenghua Yu static int intel_svm_unbind_mm(struct device *dev, u32 pasid) 661672cf6dfSJoerg Roedel { 662672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 663672cf6dfSJoerg Roedel struct intel_iommu *iommu; 664672cf6dfSJoerg Roedel struct intel_svm *svm; 665672cf6dfSJoerg Roedel int ret = -EINVAL; 666672cf6dfSJoerg Roedel 667dd6692f1SLu Baolu iommu = device_to_iommu(dev, NULL, NULL); 668672cf6dfSJoerg Roedel if (!iommu) 669672cf6dfSJoerg Roedel goto out; 670672cf6dfSJoerg Roedel 67119abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 67219abcf70SLu Baolu if (ret) 673672cf6dfSJoerg Roedel goto out; 674672cf6dfSJoerg Roedel 67519abcf70SLu Baolu if (sdev) { 676672cf6dfSJoerg Roedel sdev->users--; 677672cf6dfSJoerg Roedel if (!sdev->users) { 678672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 679672cf6dfSJoerg Roedel /* Flush the PASID cache and IOTLB for this device. 680672cf6dfSJoerg Roedel * Note that we do depend on the hardware *not* using 681672cf6dfSJoerg Roedel * the PASID any more. Just as we depend on other 682672cf6dfSJoerg Roedel * devices never using PASIDs that they have no right 683672cf6dfSJoerg Roedel * to use. We have a *shared* PASID table, because it's 684672cf6dfSJoerg Roedel * large and has to be physically contiguous. So it's 685672cf6dfSJoerg Roedel * hard to be as defensive as we might like. */ 686672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 687672cf6dfSJoerg Roedel svm->pasid, false); 688672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 689672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 690672cf6dfSJoerg Roedel 691672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 692672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 69320f0afd1SFenghua Yu if (svm->mm) { 694672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, svm->mm); 69520f0afd1SFenghua Yu /* Clear mm's pasid. */ 69620f0afd1SFenghua Yu load_pasid(svm->mm, PASID_DISABLED); 69720f0afd1SFenghua Yu } 698672cf6dfSJoerg Roedel list_del(&svm->list); 699672cf6dfSJoerg Roedel /* We mandate that no page faults may be outstanding 700672cf6dfSJoerg Roedel * for the PASID when intel_svm_unbind_mm() is called. 701672cf6dfSJoerg Roedel * If that is not obeyed, subtle errors will happen. 702672cf6dfSJoerg Roedel * Let's make them less subtle... */ 703672cf6dfSJoerg Roedel memset(svm, 0x6b, sizeof(*svm)); 704672cf6dfSJoerg Roedel kfree(svm); 705672cf6dfSJoerg Roedel } 706672cf6dfSJoerg Roedel } 707672cf6dfSJoerg Roedel } 708672cf6dfSJoerg Roedel out: 709672cf6dfSJoerg Roedel return ret; 710672cf6dfSJoerg Roedel } 711672cf6dfSJoerg Roedel 712672cf6dfSJoerg Roedel /* Page request queue descriptor */ 713672cf6dfSJoerg Roedel struct page_req_dsc { 714672cf6dfSJoerg Roedel union { 715672cf6dfSJoerg Roedel struct { 716672cf6dfSJoerg Roedel u64 type:8; 717672cf6dfSJoerg Roedel u64 pasid_present:1; 718672cf6dfSJoerg Roedel u64 priv_data_present:1; 719672cf6dfSJoerg Roedel u64 rsvd:6; 720672cf6dfSJoerg Roedel u64 rid:16; 721672cf6dfSJoerg Roedel u64 pasid:20; 722672cf6dfSJoerg Roedel u64 exe_req:1; 723672cf6dfSJoerg Roedel u64 pm_req:1; 724672cf6dfSJoerg Roedel u64 rsvd2:10; 725672cf6dfSJoerg Roedel }; 726672cf6dfSJoerg Roedel u64 qw_0; 727672cf6dfSJoerg Roedel }; 728672cf6dfSJoerg Roedel union { 729672cf6dfSJoerg Roedel struct { 730672cf6dfSJoerg Roedel u64 rd_req:1; 731672cf6dfSJoerg Roedel u64 wr_req:1; 732672cf6dfSJoerg Roedel u64 lpig:1; 733672cf6dfSJoerg Roedel u64 prg_index:9; 734672cf6dfSJoerg Roedel u64 addr:52; 735672cf6dfSJoerg Roedel }; 736672cf6dfSJoerg Roedel u64 qw_1; 737672cf6dfSJoerg Roedel }; 738672cf6dfSJoerg Roedel u64 priv_data[2]; 739672cf6dfSJoerg Roedel }; 740672cf6dfSJoerg Roedel 741672cf6dfSJoerg Roedel #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20) 742672cf6dfSJoerg Roedel 743672cf6dfSJoerg Roedel static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req) 744672cf6dfSJoerg Roedel { 745672cf6dfSJoerg Roedel unsigned long requested = 0; 746672cf6dfSJoerg Roedel 747672cf6dfSJoerg Roedel if (req->exe_req) 748672cf6dfSJoerg Roedel requested |= VM_EXEC; 749672cf6dfSJoerg Roedel 750672cf6dfSJoerg Roedel if (req->rd_req) 751672cf6dfSJoerg Roedel requested |= VM_READ; 752672cf6dfSJoerg Roedel 753672cf6dfSJoerg Roedel if (req->wr_req) 754672cf6dfSJoerg Roedel requested |= VM_WRITE; 755672cf6dfSJoerg Roedel 756672cf6dfSJoerg Roedel return (requested & ~vma->vm_flags) != 0; 757672cf6dfSJoerg Roedel } 758672cf6dfSJoerg Roedel 759672cf6dfSJoerg Roedel static bool is_canonical_address(u64 addr) 760672cf6dfSJoerg Roedel { 761672cf6dfSJoerg Roedel int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); 762672cf6dfSJoerg Roedel long saddr = (long) addr; 763672cf6dfSJoerg Roedel 764672cf6dfSJoerg Roedel return (((saddr << shift) >> shift) == saddr); 765672cf6dfSJoerg Roedel } 766672cf6dfSJoerg Roedel 767672cf6dfSJoerg Roedel /** 768672cf6dfSJoerg Roedel * intel_svm_drain_prq - Drain page requests and responses for a pasid 769672cf6dfSJoerg Roedel * @dev: target device 770672cf6dfSJoerg Roedel * @pasid: pasid for draining 771672cf6dfSJoerg Roedel * 772672cf6dfSJoerg Roedel * Drain all pending page requests and responses related to @pasid in both 773672cf6dfSJoerg Roedel * software and hardware. This is supposed to be called after the device 774672cf6dfSJoerg Roedel * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB 775672cf6dfSJoerg Roedel * and DevTLB have been invalidated. 776672cf6dfSJoerg Roedel * 777672cf6dfSJoerg Roedel * It waits until all pending page requests for @pasid in the page fault 778672cf6dfSJoerg Roedel * queue are completed by the prq handling thread. Then follow the steps 779672cf6dfSJoerg Roedel * described in VT-d spec CH7.10 to drain all page requests and page 780672cf6dfSJoerg Roedel * responses pending in the hardware. 781672cf6dfSJoerg Roedel */ 782c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid) 783672cf6dfSJoerg Roedel { 784672cf6dfSJoerg Roedel struct device_domain_info *info; 785672cf6dfSJoerg Roedel struct dmar_domain *domain; 786672cf6dfSJoerg Roedel struct intel_iommu *iommu; 787672cf6dfSJoerg Roedel struct qi_desc desc[3]; 788672cf6dfSJoerg Roedel struct pci_dev *pdev; 789672cf6dfSJoerg Roedel int head, tail; 790672cf6dfSJoerg Roedel u16 sid, did; 791672cf6dfSJoerg Roedel int qdep; 792672cf6dfSJoerg Roedel 793672cf6dfSJoerg Roedel info = get_domain_info(dev); 794672cf6dfSJoerg Roedel if (WARN_ON(!info || !dev_is_pci(dev))) 795672cf6dfSJoerg Roedel return; 796672cf6dfSJoerg Roedel 797672cf6dfSJoerg Roedel if (!info->pri_enabled) 798672cf6dfSJoerg Roedel return; 799672cf6dfSJoerg Roedel 800672cf6dfSJoerg Roedel iommu = info->iommu; 801672cf6dfSJoerg Roedel domain = info->domain; 802672cf6dfSJoerg Roedel pdev = to_pci_dev(dev); 803672cf6dfSJoerg Roedel sid = PCI_DEVID(info->bus, info->devfn); 804672cf6dfSJoerg Roedel did = domain->iommu_did[iommu->seq_id]; 805672cf6dfSJoerg Roedel qdep = pci_ats_queue_depth(pdev); 806672cf6dfSJoerg Roedel 807672cf6dfSJoerg Roedel /* 808672cf6dfSJoerg Roedel * Check and wait until all pending page requests in the queue are 809672cf6dfSJoerg Roedel * handled by the prq handling thread. 810672cf6dfSJoerg Roedel */ 811672cf6dfSJoerg Roedel prq_retry: 812672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 813672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 814672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 815672cf6dfSJoerg Roedel while (head != tail) { 816672cf6dfSJoerg Roedel struct page_req_dsc *req; 817672cf6dfSJoerg Roedel 818672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 819672cf6dfSJoerg Roedel if (!req->pasid_present || req->pasid != pasid) { 820672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 821672cf6dfSJoerg Roedel continue; 822672cf6dfSJoerg Roedel } 823672cf6dfSJoerg Roedel 824672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 825672cf6dfSJoerg Roedel goto prq_retry; 826672cf6dfSJoerg Roedel } 827672cf6dfSJoerg Roedel 828672cf6dfSJoerg Roedel /* 829672cf6dfSJoerg Roedel * Perform steps described in VT-d spec CH7.10 to drain page 830672cf6dfSJoerg Roedel * requests and responses in hardware. 831672cf6dfSJoerg Roedel */ 832672cf6dfSJoerg Roedel memset(desc, 0, sizeof(desc)); 833672cf6dfSJoerg Roedel desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) | 834672cf6dfSJoerg Roedel QI_IWD_FENCE | 835672cf6dfSJoerg Roedel QI_IWD_TYPE; 836672cf6dfSJoerg Roedel desc[1].qw0 = QI_EIOTLB_PASID(pasid) | 837672cf6dfSJoerg Roedel QI_EIOTLB_DID(did) | 838672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | 839672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 840672cf6dfSJoerg Roedel desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) | 841672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SID(sid) | 842672cf6dfSJoerg Roedel QI_DEV_EIOTLB_QDEP(qdep) | 843672cf6dfSJoerg Roedel QI_DEIOTLB_TYPE | 844672cf6dfSJoerg Roedel QI_DEV_IOTLB_PFSID(info->pfsid); 845672cf6dfSJoerg Roedel qi_retry: 846672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 847672cf6dfSJoerg Roedel qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN); 848672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) { 849672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 850672cf6dfSJoerg Roedel goto qi_retry; 851672cf6dfSJoerg Roedel } 852672cf6dfSJoerg Roedel } 853672cf6dfSJoerg Roedel 854eb8d93eaSLu Baolu static int prq_to_iommu_prot(struct page_req_dsc *req) 855eb8d93eaSLu Baolu { 856eb8d93eaSLu Baolu int prot = 0; 857eb8d93eaSLu Baolu 858eb8d93eaSLu Baolu if (req->rd_req) 859eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_READ; 860eb8d93eaSLu Baolu if (req->wr_req) 861eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_WRITE; 862eb8d93eaSLu Baolu if (req->exe_req) 863eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_EXEC; 864eb8d93eaSLu Baolu if (req->pm_req) 865eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_PRIV; 866eb8d93eaSLu Baolu 867eb8d93eaSLu Baolu return prot; 868eb8d93eaSLu Baolu } 869eb8d93eaSLu Baolu 870eb8d93eaSLu Baolu static int 871eb8d93eaSLu Baolu intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc) 872eb8d93eaSLu Baolu { 873eb8d93eaSLu Baolu struct iommu_fault_event event; 874eb8d93eaSLu Baolu 875eb8d93eaSLu Baolu if (!dev || !dev_is_pci(dev)) 876eb8d93eaSLu Baolu return -ENODEV; 877eb8d93eaSLu Baolu 878eb8d93eaSLu Baolu /* Fill in event data for device specific processing */ 879eb8d93eaSLu Baolu memset(&event, 0, sizeof(struct iommu_fault_event)); 880eb8d93eaSLu Baolu event.fault.type = IOMMU_FAULT_PAGE_REQ; 881eb8d93eaSLu Baolu event.fault.prm.addr = desc->addr; 882eb8d93eaSLu Baolu event.fault.prm.pasid = desc->pasid; 883eb8d93eaSLu Baolu event.fault.prm.grpid = desc->prg_index; 884eb8d93eaSLu Baolu event.fault.prm.perm = prq_to_iommu_prot(desc); 885eb8d93eaSLu Baolu 886eb8d93eaSLu Baolu if (desc->lpig) 887eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 888eb8d93eaSLu Baolu if (desc->pasid_present) { 889eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 890eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID; 891eb8d93eaSLu Baolu } 892eb8d93eaSLu Baolu if (desc->priv_data_present) { 893eb8d93eaSLu Baolu /* 894eb8d93eaSLu Baolu * Set last page in group bit if private data is present, 895eb8d93eaSLu Baolu * page response is required as it does for LPIG. 896eb8d93eaSLu Baolu * iommu_report_device_fault() doesn't understand this vendor 897eb8d93eaSLu Baolu * specific requirement thus we set last_page as a workaround. 898eb8d93eaSLu Baolu */ 899eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 900eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 901eb8d93eaSLu Baolu memcpy(event.fault.prm.private_data, desc->priv_data, 902eb8d93eaSLu Baolu sizeof(desc->priv_data)); 903eb8d93eaSLu Baolu } 904eb8d93eaSLu Baolu 905eb8d93eaSLu Baolu return iommu_report_device_fault(dev, &event); 906eb8d93eaSLu Baolu } 907eb8d93eaSLu Baolu 908672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d) 909672cf6dfSJoerg Roedel { 910eb8d93eaSLu Baolu struct intel_svm_dev *sdev = NULL; 911672cf6dfSJoerg Roedel struct intel_iommu *iommu = d; 912672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 913672cf6dfSJoerg Roedel int head, tail, handled = 0; 914672cf6dfSJoerg Roedel 915672cf6dfSJoerg Roedel /* Clear PPR bit before reading head/tail registers, to 916672cf6dfSJoerg Roedel * ensure that we get a new interrupt if needed. */ 917672cf6dfSJoerg Roedel writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG); 918672cf6dfSJoerg Roedel 919672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 920672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 921672cf6dfSJoerg Roedel while (head != tail) { 922672cf6dfSJoerg Roedel struct vm_area_struct *vma; 923672cf6dfSJoerg Roedel struct page_req_dsc *req; 924672cf6dfSJoerg Roedel struct qi_desc resp; 925672cf6dfSJoerg Roedel int result; 926672cf6dfSJoerg Roedel vm_fault_t ret; 927672cf6dfSJoerg Roedel u64 address; 928672cf6dfSJoerg Roedel 929672cf6dfSJoerg Roedel handled = 1; 930672cf6dfSJoerg Roedel 931672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 932672cf6dfSJoerg Roedel 933672cf6dfSJoerg Roedel result = QI_RESP_FAILURE; 934672cf6dfSJoerg Roedel address = (u64)req->addr << VTD_PAGE_SHIFT; 935672cf6dfSJoerg Roedel if (!req->pasid_present) { 936672cf6dfSJoerg Roedel pr_err("%s: Page request without PASID: %08llx %08llx\n", 937672cf6dfSJoerg Roedel iommu->name, ((unsigned long long *)req)[0], 938672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 939672cf6dfSJoerg Roedel goto no_pasid; 940672cf6dfSJoerg Roedel } 941672cf6dfSJoerg Roedel 942672cf6dfSJoerg Roedel if (!svm || svm->pasid != req->pasid) { 943672cf6dfSJoerg Roedel rcu_read_lock(); 944672cf6dfSJoerg Roedel svm = ioasid_find(NULL, req->pasid, NULL); 945672cf6dfSJoerg Roedel /* It *can't* go away, because the driver is not permitted 946672cf6dfSJoerg Roedel * to unbind the mm while any page faults are outstanding. 947672cf6dfSJoerg Roedel * So we only need RCU to protect the internal idr code. */ 948672cf6dfSJoerg Roedel rcu_read_unlock(); 949672cf6dfSJoerg Roedel if (IS_ERR_OR_NULL(svm)) { 950672cf6dfSJoerg Roedel pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n", 951672cf6dfSJoerg Roedel iommu->name, req->pasid, ((unsigned long long *)req)[0], 952672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 953672cf6dfSJoerg Roedel goto no_pasid; 954672cf6dfSJoerg Roedel } 955672cf6dfSJoerg Roedel } 956672cf6dfSJoerg Roedel 957eb8d93eaSLu Baolu if (!sdev || sdev->sid != req->rid) { 958eb8d93eaSLu Baolu struct intel_svm_dev *t; 959eb8d93eaSLu Baolu 960eb8d93eaSLu Baolu sdev = NULL; 961eb8d93eaSLu Baolu rcu_read_lock(); 962eb8d93eaSLu Baolu list_for_each_entry_rcu(t, &svm->devs, list) { 963eb8d93eaSLu Baolu if (t->sid == req->rid) { 964eb8d93eaSLu Baolu sdev = t; 965eb8d93eaSLu Baolu break; 966eb8d93eaSLu Baolu } 967eb8d93eaSLu Baolu } 968eb8d93eaSLu Baolu rcu_read_unlock(); 969eb8d93eaSLu Baolu } 970eb8d93eaSLu Baolu 971672cf6dfSJoerg Roedel result = QI_RESP_INVALID; 972672cf6dfSJoerg Roedel /* Since we're using init_mm.pgd directly, we should never take 973672cf6dfSJoerg Roedel * any faults on kernel addresses. */ 974672cf6dfSJoerg Roedel if (!svm->mm) 975672cf6dfSJoerg Roedel goto bad_req; 976672cf6dfSJoerg Roedel 977672cf6dfSJoerg Roedel /* If address is not canonical, return invalid response */ 978672cf6dfSJoerg Roedel if (!is_canonical_address(address)) 979672cf6dfSJoerg Roedel goto bad_req; 980672cf6dfSJoerg Roedel 981eb8d93eaSLu Baolu /* 982eb8d93eaSLu Baolu * If prq is to be handled outside iommu driver via receiver of 983eb8d93eaSLu Baolu * the fault notifiers, we skip the page response here. 984eb8d93eaSLu Baolu */ 985eb8d93eaSLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 986eb8d93eaSLu Baolu if (sdev && !intel_svm_prq_report(sdev->dev, req)) 987eb8d93eaSLu Baolu goto prq_advance; 988eb8d93eaSLu Baolu else 989eb8d93eaSLu Baolu goto bad_req; 990eb8d93eaSLu Baolu } 991eb8d93eaSLu Baolu 992672cf6dfSJoerg Roedel /* If the mm is already defunct, don't handle faults. */ 993672cf6dfSJoerg Roedel if (!mmget_not_zero(svm->mm)) 994672cf6dfSJoerg Roedel goto bad_req; 995672cf6dfSJoerg Roedel 9968f02f363SLinus Torvalds mmap_read_lock(svm->mm); 997672cf6dfSJoerg Roedel vma = find_extend_vma(svm->mm, address); 998672cf6dfSJoerg Roedel if (!vma || address < vma->vm_start) 999672cf6dfSJoerg Roedel goto invalid; 1000672cf6dfSJoerg Roedel 1001672cf6dfSJoerg Roedel if (access_error(vma, req)) 1002672cf6dfSJoerg Roedel goto invalid; 1003672cf6dfSJoerg Roedel 1004672cf6dfSJoerg Roedel ret = handle_mm_fault(vma, address, 1005bce617edSPeter Xu req->wr_req ? FAULT_FLAG_WRITE : 0, 1006bce617edSPeter Xu NULL); 1007672cf6dfSJoerg Roedel if (ret & VM_FAULT_ERROR) 1008672cf6dfSJoerg Roedel goto invalid; 1009672cf6dfSJoerg Roedel 1010672cf6dfSJoerg Roedel result = QI_RESP_SUCCESS; 1011672cf6dfSJoerg Roedel invalid: 10128f02f363SLinus Torvalds mmap_read_unlock(svm->mm); 1013672cf6dfSJoerg Roedel mmput(svm->mm); 1014672cf6dfSJoerg Roedel bad_req: 1015eb8d93eaSLu Baolu WARN_ON(!sdev); 1016672cf6dfSJoerg Roedel if (sdev && sdev->ops && sdev->ops->fault_cb) { 1017672cf6dfSJoerg Roedel int rwxp = (req->rd_req << 3) | (req->wr_req << 2) | 1018672cf6dfSJoerg Roedel (req->exe_req << 1) | (req->pm_req); 1019672cf6dfSJoerg Roedel sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, 1020672cf6dfSJoerg Roedel req->priv_data, rwxp, result); 1021672cf6dfSJoerg Roedel } 1022672cf6dfSJoerg Roedel /* We get here in the error case where the PASID lookup failed, 1023672cf6dfSJoerg Roedel and these can be NULL. Do not use them below this point! */ 1024672cf6dfSJoerg Roedel sdev = NULL; 1025672cf6dfSJoerg Roedel svm = NULL; 1026672cf6dfSJoerg Roedel no_pasid: 1027672cf6dfSJoerg Roedel if (req->lpig || req->priv_data_present) { 1028672cf6dfSJoerg Roedel /* 1029672cf6dfSJoerg Roedel * Per VT-d spec. v3.0 ch7.7, system software must 1030672cf6dfSJoerg Roedel * respond with page group response if private data 1031672cf6dfSJoerg Roedel * is present (PDP) or last page in group (LPIG) bit 1032672cf6dfSJoerg Roedel * is set. This is an additional VT-d feature beyond 1033672cf6dfSJoerg Roedel * PCI ATS spec. 1034672cf6dfSJoerg Roedel */ 1035672cf6dfSJoerg Roedel resp.qw0 = QI_PGRP_PASID(req->pasid) | 1036672cf6dfSJoerg Roedel QI_PGRP_DID(req->rid) | 1037672cf6dfSJoerg Roedel QI_PGRP_PASID_P(req->pasid_present) | 1038*71cd8e2dSLiu, Yi L QI_PGRP_PDP(req->priv_data_present) | 1039672cf6dfSJoerg Roedel QI_PGRP_RESP_CODE(result) | 1040672cf6dfSJoerg Roedel QI_PGRP_RESP_TYPE; 1041672cf6dfSJoerg Roedel resp.qw1 = QI_PGRP_IDX(req->prg_index) | 1042672cf6dfSJoerg Roedel QI_PGRP_LPIG(req->lpig); 1043672cf6dfSJoerg Roedel 1044672cf6dfSJoerg Roedel if (req->priv_data_present) 1045672cf6dfSJoerg Roedel memcpy(&resp.qw2, req->priv_data, 1046672cf6dfSJoerg Roedel sizeof(req->priv_data)); 1047672cf6dfSJoerg Roedel resp.qw2 = 0; 1048672cf6dfSJoerg Roedel resp.qw3 = 0; 1049672cf6dfSJoerg Roedel qi_submit_sync(iommu, &resp, 1, 0); 1050672cf6dfSJoerg Roedel } 1051eb8d93eaSLu Baolu prq_advance: 1052672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 1053672cf6dfSJoerg Roedel } 1054672cf6dfSJoerg Roedel 1055672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, tail); 1056672cf6dfSJoerg Roedel 1057672cf6dfSJoerg Roedel /* 1058672cf6dfSJoerg Roedel * Clear the page request overflow bit and wake up all threads that 1059672cf6dfSJoerg Roedel * are waiting for the completion of this handling. 1060672cf6dfSJoerg Roedel */ 1061672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) 1062672cf6dfSJoerg Roedel writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG); 1063672cf6dfSJoerg Roedel 1064672cf6dfSJoerg Roedel if (!completion_done(&iommu->prq_complete)) 1065672cf6dfSJoerg Roedel complete(&iommu->prq_complete); 1066672cf6dfSJoerg Roedel 1067672cf6dfSJoerg Roedel return IRQ_RETVAL(handled); 1068672cf6dfSJoerg Roedel } 1069672cf6dfSJoerg Roedel 1070672cf6dfSJoerg Roedel #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva) 1071672cf6dfSJoerg Roedel struct iommu_sva * 1072672cf6dfSJoerg Roedel intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata) 1073672cf6dfSJoerg Roedel { 1074672cf6dfSJoerg Roedel struct iommu_sva *sva = ERR_PTR(-EINVAL); 1075672cf6dfSJoerg Roedel struct intel_svm_dev *sdev = NULL; 10762a5054c6SFenghua Yu unsigned int flags = 0; 1077672cf6dfSJoerg Roedel int ret; 1078672cf6dfSJoerg Roedel 1079672cf6dfSJoerg Roedel /* 1080672cf6dfSJoerg Roedel * TODO: Consolidate with generic iommu-sva bind after it is merged. 1081672cf6dfSJoerg Roedel * It will require shared SVM data structures, i.e. combine io_mm 1082672cf6dfSJoerg Roedel * and intel_svm etc. 1083672cf6dfSJoerg Roedel */ 1084672cf6dfSJoerg Roedel if (drvdata) 10852a5054c6SFenghua Yu flags = *(unsigned int *)drvdata; 1086672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1087672cf6dfSJoerg Roedel ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev); 1088672cf6dfSJoerg Roedel if (ret) 1089672cf6dfSJoerg Roedel sva = ERR_PTR(ret); 1090672cf6dfSJoerg Roedel else if (sdev) 1091672cf6dfSJoerg Roedel sva = &sdev->sva; 1092672cf6dfSJoerg Roedel else 1093672cf6dfSJoerg Roedel WARN(!sdev, "SVM bind succeeded with no sdev!\n"); 1094672cf6dfSJoerg Roedel 1095672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1096672cf6dfSJoerg Roedel 1097672cf6dfSJoerg Roedel return sva; 1098672cf6dfSJoerg Roedel } 1099672cf6dfSJoerg Roedel 1100672cf6dfSJoerg Roedel void intel_svm_unbind(struct iommu_sva *sva) 1101672cf6dfSJoerg Roedel { 1102672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1103672cf6dfSJoerg Roedel 1104672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1105672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1106672cf6dfSJoerg Roedel intel_svm_unbind_mm(sdev->dev, sdev->pasid); 1107672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1108672cf6dfSJoerg Roedel } 1109672cf6dfSJoerg Roedel 1110c7b6bac9SFenghua Yu u32 intel_svm_get_pasid(struct iommu_sva *sva) 1111672cf6dfSJoerg Roedel { 1112672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1113c7b6bac9SFenghua Yu u32 pasid; 1114672cf6dfSJoerg Roedel 1115672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1116672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1117672cf6dfSJoerg Roedel pasid = sdev->pasid; 1118672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1119672cf6dfSJoerg Roedel 1120672cf6dfSJoerg Roedel return pasid; 1121672cf6dfSJoerg Roedel } 11228b737121SLu Baolu 11238b737121SLu Baolu int intel_svm_page_response(struct device *dev, 11248b737121SLu Baolu struct iommu_fault_event *evt, 11258b737121SLu Baolu struct iommu_page_response *msg) 11268b737121SLu Baolu { 11278b737121SLu Baolu struct iommu_fault_page_request *prm; 11288b737121SLu Baolu struct intel_svm_dev *sdev = NULL; 11298b737121SLu Baolu struct intel_svm *svm = NULL; 11308b737121SLu Baolu struct intel_iommu *iommu; 11318b737121SLu Baolu bool private_present; 11328b737121SLu Baolu bool pasid_present; 11338b737121SLu Baolu bool last_page; 11348b737121SLu Baolu u8 bus, devfn; 11358b737121SLu Baolu int ret = 0; 11368b737121SLu Baolu u16 sid; 11378b737121SLu Baolu 11388b737121SLu Baolu if (!dev || !dev_is_pci(dev)) 11398b737121SLu Baolu return -ENODEV; 11408b737121SLu Baolu 11418b737121SLu Baolu iommu = device_to_iommu(dev, &bus, &devfn); 11428b737121SLu Baolu if (!iommu) 11438b737121SLu Baolu return -ENODEV; 11448b737121SLu Baolu 11458b737121SLu Baolu if (!msg || !evt) 11468b737121SLu Baolu return -EINVAL; 11478b737121SLu Baolu 11488b737121SLu Baolu mutex_lock(&pasid_mutex); 11498b737121SLu Baolu 11508b737121SLu Baolu prm = &evt->fault.prm; 11518b737121SLu Baolu sid = PCI_DEVID(bus, devfn); 11528b737121SLu Baolu pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 11538b737121SLu Baolu private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 11548b737121SLu Baolu last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 11558b737121SLu Baolu 11568b737121SLu Baolu if (!pasid_present) { 11578b737121SLu Baolu ret = -EINVAL; 11588b737121SLu Baolu goto out; 11598b737121SLu Baolu } 11608b737121SLu Baolu 11618b737121SLu Baolu if (prm->pasid == 0 || prm->pasid >= PASID_MAX) { 11628b737121SLu Baolu ret = -EINVAL; 11638b737121SLu Baolu goto out; 11648b737121SLu Baolu } 11658b737121SLu Baolu 11668b737121SLu Baolu ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev); 11678b737121SLu Baolu if (ret || !sdev) { 11688b737121SLu Baolu ret = -ENODEV; 11698b737121SLu Baolu goto out; 11708b737121SLu Baolu } 11718b737121SLu Baolu 11728b737121SLu Baolu /* 11738b737121SLu Baolu * For responses from userspace, need to make sure that the 11748b737121SLu Baolu * pasid has been bound to its mm. 11758b737121SLu Baolu */ 11768b737121SLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 11778b737121SLu Baolu struct mm_struct *mm; 11788b737121SLu Baolu 11798b737121SLu Baolu mm = get_task_mm(current); 11808b737121SLu Baolu if (!mm) { 11818b737121SLu Baolu ret = -EINVAL; 11828b737121SLu Baolu goto out; 11838b737121SLu Baolu } 11848b737121SLu Baolu 11858b737121SLu Baolu if (mm != svm->mm) { 11868b737121SLu Baolu ret = -ENODEV; 11878b737121SLu Baolu mmput(mm); 11888b737121SLu Baolu goto out; 11898b737121SLu Baolu } 11908b737121SLu Baolu 11918b737121SLu Baolu mmput(mm); 11928b737121SLu Baolu } 11938b737121SLu Baolu 11948b737121SLu Baolu /* 11958b737121SLu Baolu * Per VT-d spec. v3.0 ch7.7, system software must respond 11968b737121SLu Baolu * with page group response if private data is present (PDP) 11978b737121SLu Baolu * or last page in group (LPIG) bit is set. This is an 11988b737121SLu Baolu * additional VT-d requirement beyond PCI ATS spec. 11998b737121SLu Baolu */ 12008b737121SLu Baolu if (last_page || private_present) { 12018b737121SLu Baolu struct qi_desc desc; 12028b737121SLu Baolu 12038b737121SLu Baolu desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) | 12048b737121SLu Baolu QI_PGRP_PASID_P(pasid_present) | 12058b737121SLu Baolu QI_PGRP_PDP(private_present) | 12068b737121SLu Baolu QI_PGRP_RESP_CODE(msg->code) | 12078b737121SLu Baolu QI_PGRP_RESP_TYPE; 12088b737121SLu Baolu desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page); 12098b737121SLu Baolu desc.qw2 = 0; 12108b737121SLu Baolu desc.qw3 = 0; 12118b737121SLu Baolu if (private_present) 12128b737121SLu Baolu memcpy(&desc.qw2, prm->private_data, 12138b737121SLu Baolu sizeof(prm->private_data)); 12148b737121SLu Baolu 12158b737121SLu Baolu qi_submit_sync(iommu, &desc, 1, 0); 12168b737121SLu Baolu } 12178b737121SLu Baolu out: 12188b737121SLu Baolu mutex_unlock(&pasid_mutex); 12198b737121SLu Baolu return ret; 12208b737121SLu Baolu } 1221