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> 22672cf6dfSJoerg Roedel 2302f3effdSLu Baolu #include "pasid.h" 24672cf6dfSJoerg Roedel 25672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d); 26672cf6dfSJoerg Roedel static void intel_svm_drain_prq(struct device *dev, int pasid); 27672cf6dfSJoerg Roedel 28672cf6dfSJoerg Roedel #define PRQ_ORDER 0 29672cf6dfSJoerg Roedel 30672cf6dfSJoerg Roedel int intel_svm_enable_prq(struct intel_iommu *iommu) 31672cf6dfSJoerg Roedel { 32672cf6dfSJoerg Roedel struct page *pages; 33672cf6dfSJoerg Roedel int irq, ret; 34672cf6dfSJoerg Roedel 35672cf6dfSJoerg Roedel pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER); 36672cf6dfSJoerg Roedel if (!pages) { 37672cf6dfSJoerg Roedel pr_warn("IOMMU: %s: Failed to allocate page request queue\n", 38672cf6dfSJoerg Roedel iommu->name); 39672cf6dfSJoerg Roedel return -ENOMEM; 40672cf6dfSJoerg Roedel } 41672cf6dfSJoerg Roedel iommu->prq = page_address(pages); 42672cf6dfSJoerg Roedel 43672cf6dfSJoerg Roedel irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu); 44672cf6dfSJoerg Roedel if (irq <= 0) { 45672cf6dfSJoerg Roedel pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n", 46672cf6dfSJoerg Roedel iommu->name); 47672cf6dfSJoerg Roedel ret = -EINVAL; 48672cf6dfSJoerg Roedel err: 49672cf6dfSJoerg Roedel free_pages((unsigned long)iommu->prq, PRQ_ORDER); 50672cf6dfSJoerg Roedel iommu->prq = NULL; 51672cf6dfSJoerg Roedel return ret; 52672cf6dfSJoerg Roedel } 53672cf6dfSJoerg Roedel iommu->pr_irq = irq; 54672cf6dfSJoerg Roedel 55672cf6dfSJoerg Roedel snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id); 56672cf6dfSJoerg Roedel 57672cf6dfSJoerg Roedel ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT, 58672cf6dfSJoerg Roedel iommu->prq_name, iommu); 59672cf6dfSJoerg Roedel if (ret) { 60672cf6dfSJoerg Roedel pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n", 61672cf6dfSJoerg Roedel iommu->name); 62672cf6dfSJoerg Roedel dmar_free_hwirq(irq); 63672cf6dfSJoerg Roedel iommu->pr_irq = 0; 64672cf6dfSJoerg Roedel goto err; 65672cf6dfSJoerg Roedel } 66672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); 67672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); 68672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER); 69672cf6dfSJoerg Roedel 70672cf6dfSJoerg Roedel init_completion(&iommu->prq_complete); 71672cf6dfSJoerg Roedel 72672cf6dfSJoerg Roedel return 0; 73672cf6dfSJoerg Roedel } 74672cf6dfSJoerg Roedel 75672cf6dfSJoerg Roedel int intel_svm_finish_prq(struct intel_iommu *iommu) 76672cf6dfSJoerg Roedel { 77672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); 78672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); 79672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL); 80672cf6dfSJoerg Roedel 81672cf6dfSJoerg Roedel if (iommu->pr_irq) { 82672cf6dfSJoerg Roedel free_irq(iommu->pr_irq, iommu); 83672cf6dfSJoerg Roedel dmar_free_hwirq(iommu->pr_irq); 84672cf6dfSJoerg Roedel iommu->pr_irq = 0; 85672cf6dfSJoerg Roedel } 86672cf6dfSJoerg Roedel 87672cf6dfSJoerg Roedel free_pages((unsigned long)iommu->prq, PRQ_ORDER); 88672cf6dfSJoerg Roedel iommu->prq = NULL; 89672cf6dfSJoerg Roedel 90672cf6dfSJoerg Roedel return 0; 91672cf6dfSJoerg Roedel } 92672cf6dfSJoerg Roedel 93672cf6dfSJoerg Roedel static inline bool intel_svm_capable(struct intel_iommu *iommu) 94672cf6dfSJoerg Roedel { 95672cf6dfSJoerg Roedel return iommu->flags & VTD_FLAG_SVM_CAPABLE; 96672cf6dfSJoerg Roedel } 97672cf6dfSJoerg Roedel 98672cf6dfSJoerg Roedel void intel_svm_check(struct intel_iommu *iommu) 99672cf6dfSJoerg Roedel { 100672cf6dfSJoerg Roedel if (!pasid_supported(iommu)) 101672cf6dfSJoerg Roedel return; 102672cf6dfSJoerg Roedel 103672cf6dfSJoerg Roedel if (cpu_feature_enabled(X86_FEATURE_GBPAGES) && 104672cf6dfSJoerg Roedel !cap_fl1gp_support(iommu->cap)) { 105672cf6dfSJoerg Roedel pr_err("%s SVM disabled, incompatible 1GB page capability\n", 106672cf6dfSJoerg Roedel iommu->name); 107672cf6dfSJoerg Roedel return; 108672cf6dfSJoerg Roedel } 109672cf6dfSJoerg Roedel 110672cf6dfSJoerg Roedel if (cpu_feature_enabled(X86_FEATURE_LA57) && 111672cf6dfSJoerg Roedel !cap_5lp_support(iommu->cap)) { 112672cf6dfSJoerg Roedel pr_err("%s SVM disabled, incompatible paging mode\n", 113672cf6dfSJoerg Roedel iommu->name); 114672cf6dfSJoerg Roedel return; 115672cf6dfSJoerg Roedel } 116672cf6dfSJoerg Roedel 117672cf6dfSJoerg Roedel iommu->flags |= VTD_FLAG_SVM_CAPABLE; 118672cf6dfSJoerg Roedel } 119672cf6dfSJoerg Roedel 120672cf6dfSJoerg Roedel static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev, 121672cf6dfSJoerg Roedel unsigned long address, unsigned long pages, int ih) 122672cf6dfSJoerg Roedel { 123672cf6dfSJoerg Roedel struct qi_desc desc; 124672cf6dfSJoerg Roedel 125672cf6dfSJoerg Roedel if (pages == -1) { 126672cf6dfSJoerg Roedel desc.qw0 = QI_EIOTLB_PASID(svm->pasid) | 127672cf6dfSJoerg Roedel QI_EIOTLB_DID(sdev->did) | 128672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | 129672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 130672cf6dfSJoerg Roedel desc.qw1 = 0; 131672cf6dfSJoerg Roedel } else { 132672cf6dfSJoerg Roedel int mask = ilog2(__roundup_pow_of_two(pages)); 133672cf6dfSJoerg Roedel 134672cf6dfSJoerg Roedel desc.qw0 = QI_EIOTLB_PASID(svm->pasid) | 135672cf6dfSJoerg Roedel QI_EIOTLB_DID(sdev->did) | 136672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | 137672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 138672cf6dfSJoerg Roedel desc.qw1 = QI_EIOTLB_ADDR(address) | 139672cf6dfSJoerg Roedel QI_EIOTLB_IH(ih) | 140672cf6dfSJoerg Roedel QI_EIOTLB_AM(mask); 141672cf6dfSJoerg Roedel } 142672cf6dfSJoerg Roedel desc.qw2 = 0; 143672cf6dfSJoerg Roedel desc.qw3 = 0; 144672cf6dfSJoerg Roedel qi_submit_sync(svm->iommu, &desc, 1, 0); 145672cf6dfSJoerg Roedel 146672cf6dfSJoerg Roedel if (sdev->dev_iotlb) { 147672cf6dfSJoerg Roedel desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) | 148672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SID(sdev->sid) | 149672cf6dfSJoerg Roedel QI_DEV_EIOTLB_QDEP(sdev->qdep) | 150672cf6dfSJoerg Roedel QI_DEIOTLB_TYPE; 151672cf6dfSJoerg Roedel if (pages == -1) { 152672cf6dfSJoerg Roedel desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | 153672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SIZE; 154672cf6dfSJoerg Roedel } else if (pages > 1) { 155672cf6dfSJoerg Roedel /* The least significant zero bit indicates the size. So, 156672cf6dfSJoerg Roedel * for example, an "address" value of 0x12345f000 will 157672cf6dfSJoerg Roedel * flush from 0x123440000 to 0x12347ffff (256KiB). */ 158672cf6dfSJoerg Roedel unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT); 159672cf6dfSJoerg Roedel unsigned long mask = __rounddown_pow_of_two(address ^ last); 160672cf6dfSJoerg Roedel 161672cf6dfSJoerg Roedel desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) | 162672cf6dfSJoerg Roedel (mask - 1)) | QI_DEV_EIOTLB_SIZE; 163672cf6dfSJoerg Roedel } else { 164672cf6dfSJoerg Roedel desc.qw1 = QI_DEV_EIOTLB_ADDR(address); 165672cf6dfSJoerg Roedel } 166672cf6dfSJoerg Roedel desc.qw2 = 0; 167672cf6dfSJoerg Roedel desc.qw3 = 0; 168672cf6dfSJoerg Roedel qi_submit_sync(svm->iommu, &desc, 1, 0); 169672cf6dfSJoerg Roedel } 170672cf6dfSJoerg Roedel } 171672cf6dfSJoerg Roedel 172672cf6dfSJoerg Roedel static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address, 173672cf6dfSJoerg Roedel unsigned long pages, int ih) 174672cf6dfSJoerg Roedel { 175672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 176672cf6dfSJoerg Roedel 177672cf6dfSJoerg Roedel rcu_read_lock(); 178672cf6dfSJoerg Roedel list_for_each_entry_rcu(sdev, &svm->devs, list) 179672cf6dfSJoerg Roedel intel_flush_svm_range_dev(svm, sdev, address, pages, ih); 180672cf6dfSJoerg Roedel rcu_read_unlock(); 181672cf6dfSJoerg Roedel } 182672cf6dfSJoerg Roedel 183672cf6dfSJoerg Roedel /* Pages have been freed at this point */ 184672cf6dfSJoerg Roedel static void intel_invalidate_range(struct mmu_notifier *mn, 185672cf6dfSJoerg Roedel struct mm_struct *mm, 186672cf6dfSJoerg Roedel unsigned long start, unsigned long end) 187672cf6dfSJoerg Roedel { 188672cf6dfSJoerg Roedel struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); 189672cf6dfSJoerg Roedel 190672cf6dfSJoerg Roedel intel_flush_svm_range(svm, start, 191672cf6dfSJoerg Roedel (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0); 192672cf6dfSJoerg Roedel } 193672cf6dfSJoerg Roedel 194672cf6dfSJoerg Roedel static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) 195672cf6dfSJoerg Roedel { 196672cf6dfSJoerg Roedel struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); 197672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 198672cf6dfSJoerg Roedel 199672cf6dfSJoerg Roedel /* This might end up being called from exit_mmap(), *before* the page 200672cf6dfSJoerg Roedel * tables are cleared. And __mmu_notifier_release() will delete us from 201672cf6dfSJoerg Roedel * the list of notifiers so that our invalidate_range() callback doesn't 202672cf6dfSJoerg Roedel * get called when the page tables are cleared. So we need to protect 203672cf6dfSJoerg Roedel * against hardware accessing those page tables. 204672cf6dfSJoerg Roedel * 205672cf6dfSJoerg Roedel * We do it by clearing the entry in the PASID table and then flushing 206672cf6dfSJoerg Roedel * the IOTLB and the PASID table caches. This might upset hardware; 207672cf6dfSJoerg Roedel * perhaps we'll want to point the PASID to a dummy PGD (like the zero 208672cf6dfSJoerg Roedel * page) so that we end up taking a fault that the hardware really 209672cf6dfSJoerg Roedel * *has* to handle gracefully without affecting other processes. 210672cf6dfSJoerg Roedel */ 211672cf6dfSJoerg Roedel rcu_read_lock(); 212672cf6dfSJoerg Roedel list_for_each_entry_rcu(sdev, &svm->devs, list) 213672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(svm->iommu, sdev->dev, 214672cf6dfSJoerg Roedel svm->pasid, true); 215672cf6dfSJoerg Roedel rcu_read_unlock(); 216672cf6dfSJoerg Roedel 217672cf6dfSJoerg Roedel } 218672cf6dfSJoerg Roedel 219672cf6dfSJoerg Roedel static const struct mmu_notifier_ops intel_mmuops = { 220672cf6dfSJoerg Roedel .release = intel_mm_release, 221672cf6dfSJoerg Roedel .invalidate_range = intel_invalidate_range, 222672cf6dfSJoerg Roedel }; 223672cf6dfSJoerg Roedel 224672cf6dfSJoerg Roedel static DEFINE_MUTEX(pasid_mutex); 225672cf6dfSJoerg Roedel static LIST_HEAD(global_svm_list); 226672cf6dfSJoerg Roedel 227672cf6dfSJoerg Roedel #define for_each_svm_dev(sdev, svm, d) \ 228672cf6dfSJoerg Roedel list_for_each_entry((sdev), &(svm)->devs, list) \ 229672cf6dfSJoerg Roedel if ((d) != (sdev)->dev) {} else 230672cf6dfSJoerg Roedel 23119abcf70SLu Baolu static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid, 23219abcf70SLu Baolu struct intel_svm **rsvm, 23319abcf70SLu Baolu struct intel_svm_dev **rsdev) 23419abcf70SLu Baolu { 23519abcf70SLu Baolu struct intel_svm_dev *d, *sdev = NULL; 23619abcf70SLu Baolu struct intel_svm *svm; 23719abcf70SLu Baolu 23819abcf70SLu Baolu /* The caller should hold the pasid_mutex lock */ 23919abcf70SLu Baolu if (WARN_ON(!mutex_is_locked(&pasid_mutex))) 24019abcf70SLu Baolu return -EINVAL; 24119abcf70SLu Baolu 24219abcf70SLu Baolu if (pasid == INVALID_IOASID || pasid >= PASID_MAX) 24319abcf70SLu Baolu return -EINVAL; 24419abcf70SLu Baolu 24519abcf70SLu Baolu svm = ioasid_find(NULL, pasid, NULL); 24619abcf70SLu Baolu if (IS_ERR(svm)) 24719abcf70SLu Baolu return PTR_ERR(svm); 24819abcf70SLu Baolu 24919abcf70SLu Baolu if (!svm) 25019abcf70SLu Baolu goto out; 25119abcf70SLu Baolu 25219abcf70SLu Baolu /* 25319abcf70SLu Baolu * If we found svm for the PASID, there must be at least one device 25419abcf70SLu Baolu * bond. 25519abcf70SLu Baolu */ 25619abcf70SLu Baolu if (WARN_ON(list_empty(&svm->devs))) 25719abcf70SLu Baolu return -EINVAL; 25819abcf70SLu Baolu 25919abcf70SLu Baolu rcu_read_lock(); 26019abcf70SLu Baolu list_for_each_entry_rcu(d, &svm->devs, list) { 26119abcf70SLu Baolu if (d->dev == dev) { 26219abcf70SLu Baolu sdev = d; 26319abcf70SLu Baolu break; 26419abcf70SLu Baolu } 26519abcf70SLu Baolu } 26619abcf70SLu Baolu rcu_read_unlock(); 26719abcf70SLu Baolu 26819abcf70SLu Baolu out: 26919abcf70SLu Baolu *rsvm = svm; 27019abcf70SLu Baolu *rsdev = sdev; 27119abcf70SLu Baolu 27219abcf70SLu Baolu return 0; 27319abcf70SLu Baolu } 27419abcf70SLu Baolu 275672cf6dfSJoerg Roedel int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, 276672cf6dfSJoerg Roedel struct iommu_gpasid_bind_data *data) 277672cf6dfSJoerg Roedel { 278dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 27919abcf70SLu Baolu struct intel_svm_dev *sdev = NULL; 280672cf6dfSJoerg Roedel struct dmar_domain *dmar_domain; 28119abcf70SLu Baolu struct intel_svm *svm = NULL; 282672cf6dfSJoerg Roedel int ret = 0; 283672cf6dfSJoerg Roedel 284672cf6dfSJoerg Roedel if (WARN_ON(!iommu) || !data) 285672cf6dfSJoerg Roedel return -EINVAL; 286672cf6dfSJoerg Roedel 287*6278eecbSJacob Pan if (data->format != IOMMU_PASID_FORMAT_INTEL_VTD) 288*6278eecbSJacob Pan return -EINVAL; 289*6278eecbSJacob Pan 290*6278eecbSJacob Pan /* IOMMU core ensures argsz is more than the start of the union */ 291*6278eecbSJacob Pan if (data->argsz < offsetofend(struct iommu_gpasid_bind_data, vendor.vtd)) 292*6278eecbSJacob Pan return -EINVAL; 293*6278eecbSJacob Pan 294*6278eecbSJacob Pan /* Make sure no undefined flags are used in vendor data */ 295*6278eecbSJacob Pan if (data->vendor.vtd.flags & ~(IOMMU_SVA_VTD_GPASID_LAST - 1)) 296672cf6dfSJoerg Roedel return -EINVAL; 297672cf6dfSJoerg Roedel 298672cf6dfSJoerg Roedel if (!dev_is_pci(dev)) 299672cf6dfSJoerg Roedel return -ENOTSUPP; 300672cf6dfSJoerg Roedel 301672cf6dfSJoerg Roedel /* VT-d supports devices with full 20 bit PASIDs only */ 302672cf6dfSJoerg Roedel if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX) 303672cf6dfSJoerg Roedel return -EINVAL; 304672cf6dfSJoerg Roedel 305672cf6dfSJoerg Roedel /* 306672cf6dfSJoerg Roedel * We only check host PASID range, we have no knowledge to check 307672cf6dfSJoerg Roedel * guest PASID range. 308672cf6dfSJoerg Roedel */ 309672cf6dfSJoerg Roedel if (data->hpasid <= 0 || data->hpasid >= PASID_MAX) 310672cf6dfSJoerg Roedel return -EINVAL; 311672cf6dfSJoerg Roedel 312672cf6dfSJoerg Roedel dmar_domain = to_dmar_domain(domain); 313672cf6dfSJoerg Roedel 314672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 31519abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev); 31619abcf70SLu Baolu if (ret) 317672cf6dfSJoerg Roedel goto out; 318672cf6dfSJoerg Roedel 31919abcf70SLu Baolu if (sdev) { 320672cf6dfSJoerg Roedel /* 321d315e9e6SJacob Pan * Do not allow multiple bindings of the same device-PASID since 322d315e9e6SJacob Pan * there is only one SL page tables per PASID. We may revisit 323d315e9e6SJacob Pan * once sharing PGD across domains are supported. 324672cf6dfSJoerg Roedel */ 32519abcf70SLu Baolu dev_warn_ratelimited(dev, "Already bound with PASID %u\n", 326672cf6dfSJoerg Roedel svm->pasid); 327672cf6dfSJoerg Roedel ret = -EBUSY; 328672cf6dfSJoerg Roedel goto out; 329672cf6dfSJoerg Roedel } 33019abcf70SLu Baolu 33119abcf70SLu Baolu if (!svm) { 332672cf6dfSJoerg Roedel /* We come here when PASID has never been bond to a device. */ 333672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 334672cf6dfSJoerg Roedel if (!svm) { 335672cf6dfSJoerg Roedel ret = -ENOMEM; 336672cf6dfSJoerg Roedel goto out; 337672cf6dfSJoerg Roedel } 338672cf6dfSJoerg Roedel /* REVISIT: upper layer/VFIO can track host process that bind 339672cf6dfSJoerg Roedel * the PASID. ioasid_set = mm might be sufficient for vfio to 340672cf6dfSJoerg Roedel * check pasid VMM ownership. We can drop the following line 341672cf6dfSJoerg Roedel * once VFIO and IOASID set check is in place. 342672cf6dfSJoerg Roedel */ 343672cf6dfSJoerg Roedel svm->mm = get_task_mm(current); 344672cf6dfSJoerg Roedel svm->pasid = data->hpasid; 345672cf6dfSJoerg Roedel if (data->flags & IOMMU_SVA_GPASID_VAL) { 346672cf6dfSJoerg Roedel svm->gpasid = data->gpasid; 347672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_PASID; 348672cf6dfSJoerg Roedel } 349672cf6dfSJoerg Roedel ioasid_set_data(data->hpasid, svm); 350672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 351672cf6dfSJoerg Roedel mmput(svm->mm); 352672cf6dfSJoerg Roedel } 353672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 354672cf6dfSJoerg Roedel if (!sdev) { 355672cf6dfSJoerg Roedel ret = -ENOMEM; 356672cf6dfSJoerg Roedel goto out; 357672cf6dfSJoerg Roedel } 358672cf6dfSJoerg Roedel sdev->dev = dev; 359672cf6dfSJoerg Roedel 360672cf6dfSJoerg Roedel /* Only count users if device has aux domains */ 361672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 362672cf6dfSJoerg Roedel sdev->users = 1; 363672cf6dfSJoerg Roedel 364672cf6dfSJoerg Roedel /* Set up device context entry for PASID if not enabled already */ 365672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, sdev->dev); 366672cf6dfSJoerg Roedel if (ret) { 367672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to enable PASID capability\n"); 368672cf6dfSJoerg Roedel kfree(sdev); 369672cf6dfSJoerg Roedel goto out; 370672cf6dfSJoerg Roedel } 371672cf6dfSJoerg Roedel 372672cf6dfSJoerg Roedel /* 373672cf6dfSJoerg Roedel * PASID table is per device for better security. Therefore, for 374672cf6dfSJoerg Roedel * each bind of a new device even with an existing PASID, we need to 375672cf6dfSJoerg Roedel * call the nested mode setup function here. 376672cf6dfSJoerg Roedel */ 377672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 378672cf6dfSJoerg Roedel ret = intel_pasid_setup_nested(iommu, dev, 379672cf6dfSJoerg Roedel (pgd_t *)(uintptr_t)data->gpgd, 3808d3bb3b8SJacob Pan data->hpasid, &data->vendor.vtd, dmar_domain, 381672cf6dfSJoerg Roedel data->addr_width); 382672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 383672cf6dfSJoerg Roedel if (ret) { 384672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n", 385672cf6dfSJoerg Roedel data->hpasid, ret); 386672cf6dfSJoerg Roedel /* 387672cf6dfSJoerg Roedel * PASID entry should be in cleared state if nested mode 388672cf6dfSJoerg Roedel * set up failed. So we only need to clear IOASID tracking 389672cf6dfSJoerg Roedel * data such that free call will succeed. 390672cf6dfSJoerg Roedel */ 391672cf6dfSJoerg Roedel kfree(sdev); 392672cf6dfSJoerg Roedel goto out; 393672cf6dfSJoerg Roedel } 394672cf6dfSJoerg Roedel 395672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_MODE; 396672cf6dfSJoerg Roedel 397672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 398672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 399672cf6dfSJoerg Roedel out: 400672cf6dfSJoerg Roedel if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) { 401672cf6dfSJoerg Roedel ioasid_set_data(data->hpasid, NULL); 402672cf6dfSJoerg Roedel kfree(svm); 403672cf6dfSJoerg Roedel } 404672cf6dfSJoerg Roedel 405672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 406672cf6dfSJoerg Roedel return ret; 407672cf6dfSJoerg Roedel } 408672cf6dfSJoerg Roedel 409672cf6dfSJoerg Roedel int intel_svm_unbind_gpasid(struct device *dev, int pasid) 410672cf6dfSJoerg Roedel { 411dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 412672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 413672cf6dfSJoerg Roedel struct intel_svm *svm; 41419abcf70SLu Baolu int ret; 415672cf6dfSJoerg Roedel 416672cf6dfSJoerg Roedel if (WARN_ON(!iommu)) 417672cf6dfSJoerg Roedel return -EINVAL; 418672cf6dfSJoerg Roedel 419672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 42019abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 42119abcf70SLu Baolu if (ret) 422672cf6dfSJoerg Roedel goto out; 423672cf6dfSJoerg Roedel 42419abcf70SLu Baolu if (sdev) { 425672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 426672cf6dfSJoerg Roedel sdev->users--; 427672cf6dfSJoerg Roedel if (!sdev->users) { 428672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 429672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 430672cf6dfSJoerg Roedel svm->pasid, false); 431672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 432672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 433672cf6dfSJoerg Roedel 434672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 435672cf6dfSJoerg Roedel /* 436672cf6dfSJoerg Roedel * We do not free the IOASID here in that 437672cf6dfSJoerg Roedel * IOMMU driver did not allocate it. 438672cf6dfSJoerg Roedel * Unlike native SVM, IOASID for guest use was 439672cf6dfSJoerg Roedel * allocated prior to the bind call. 440672cf6dfSJoerg Roedel * In any case, if the free call comes before 441672cf6dfSJoerg Roedel * the unbind, IOMMU driver will get notified 442672cf6dfSJoerg Roedel * and perform cleanup. 443672cf6dfSJoerg Roedel */ 444672cf6dfSJoerg Roedel ioasid_set_data(pasid, NULL); 445672cf6dfSJoerg Roedel kfree(svm); 446672cf6dfSJoerg Roedel } 447672cf6dfSJoerg Roedel } 448672cf6dfSJoerg Roedel } 449672cf6dfSJoerg Roedel out: 450672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 451672cf6dfSJoerg Roedel return ret; 452672cf6dfSJoerg Roedel } 453672cf6dfSJoerg Roedel 454672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex, mm reference */ 455672cf6dfSJoerg Roedel static int 456672cf6dfSJoerg Roedel intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops, 457672cf6dfSJoerg Roedel struct mm_struct *mm, struct intel_svm_dev **sd) 458672cf6dfSJoerg Roedel { 459dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 460672cf6dfSJoerg Roedel struct device_domain_info *info; 461672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 462672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 463672cf6dfSJoerg Roedel int pasid_max; 464672cf6dfSJoerg Roedel int ret; 465672cf6dfSJoerg Roedel 466672cf6dfSJoerg Roedel if (!iommu || dmar_disabled) 467672cf6dfSJoerg Roedel return -EINVAL; 468672cf6dfSJoerg Roedel 469672cf6dfSJoerg Roedel if (!intel_svm_capable(iommu)) 470672cf6dfSJoerg Roedel return -ENOTSUPP; 471672cf6dfSJoerg Roedel 472672cf6dfSJoerg Roedel if (dev_is_pci(dev)) { 473672cf6dfSJoerg Roedel pasid_max = pci_max_pasids(to_pci_dev(dev)); 474672cf6dfSJoerg Roedel if (pasid_max < 0) 475672cf6dfSJoerg Roedel return -EINVAL; 476672cf6dfSJoerg Roedel } else 477672cf6dfSJoerg Roedel pasid_max = 1 << 20; 478672cf6dfSJoerg Roedel 479672cf6dfSJoerg Roedel /* Bind supervisor PASID shuld have mm = NULL */ 480672cf6dfSJoerg Roedel if (flags & SVM_FLAG_SUPERVISOR_MODE) { 481672cf6dfSJoerg Roedel if (!ecap_srs(iommu->ecap) || mm) { 482672cf6dfSJoerg Roedel pr_err("Supervisor PASID with user provided mm.\n"); 483672cf6dfSJoerg Roedel return -EINVAL; 484672cf6dfSJoerg Roedel } 485672cf6dfSJoerg Roedel } 486672cf6dfSJoerg Roedel 487672cf6dfSJoerg Roedel if (!(flags & SVM_FLAG_PRIVATE_PASID)) { 488672cf6dfSJoerg Roedel struct intel_svm *t; 489672cf6dfSJoerg Roedel 490672cf6dfSJoerg Roedel list_for_each_entry(t, &global_svm_list, list) { 491672cf6dfSJoerg Roedel if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID)) 492672cf6dfSJoerg Roedel continue; 493672cf6dfSJoerg Roedel 494672cf6dfSJoerg Roedel svm = t; 495672cf6dfSJoerg Roedel if (svm->pasid >= pasid_max) { 496672cf6dfSJoerg Roedel dev_warn(dev, 497672cf6dfSJoerg Roedel "Limited PASID width. Cannot use existing PASID %d\n", 498672cf6dfSJoerg Roedel svm->pasid); 499672cf6dfSJoerg Roedel ret = -ENOSPC; 500672cf6dfSJoerg Roedel goto out; 501672cf6dfSJoerg Roedel } 502672cf6dfSJoerg Roedel 503672cf6dfSJoerg Roedel /* Find the matching device in svm list */ 504672cf6dfSJoerg Roedel for_each_svm_dev(sdev, svm, dev) { 505672cf6dfSJoerg Roedel if (sdev->ops != ops) { 506672cf6dfSJoerg Roedel ret = -EBUSY; 507672cf6dfSJoerg Roedel goto out; 508672cf6dfSJoerg Roedel } 509672cf6dfSJoerg Roedel sdev->users++; 510672cf6dfSJoerg Roedel goto success; 511672cf6dfSJoerg Roedel } 512672cf6dfSJoerg Roedel 513672cf6dfSJoerg Roedel break; 514672cf6dfSJoerg Roedel } 515672cf6dfSJoerg Roedel } 516672cf6dfSJoerg Roedel 517672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 518672cf6dfSJoerg Roedel if (!sdev) { 519672cf6dfSJoerg Roedel ret = -ENOMEM; 520672cf6dfSJoerg Roedel goto out; 521672cf6dfSJoerg Roedel } 522672cf6dfSJoerg Roedel sdev->dev = dev; 523672cf6dfSJoerg Roedel 524672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, dev); 525672cf6dfSJoerg Roedel if (ret) { 526672cf6dfSJoerg Roedel kfree(sdev); 527672cf6dfSJoerg Roedel goto out; 528672cf6dfSJoerg Roedel } 529672cf6dfSJoerg Roedel 530672cf6dfSJoerg Roedel info = get_domain_info(dev); 531672cf6dfSJoerg Roedel sdev->did = FLPT_DEFAULT_DID; 532672cf6dfSJoerg Roedel sdev->sid = PCI_DEVID(info->bus, info->devfn); 533672cf6dfSJoerg Roedel if (info->ats_enabled) { 534672cf6dfSJoerg Roedel sdev->dev_iotlb = 1; 535672cf6dfSJoerg Roedel sdev->qdep = info->ats_qdep; 536672cf6dfSJoerg Roedel if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) 537672cf6dfSJoerg Roedel sdev->qdep = 0; 538672cf6dfSJoerg Roedel } 539672cf6dfSJoerg Roedel 540672cf6dfSJoerg Roedel /* Finish the setup now we know we're keeping it */ 541672cf6dfSJoerg Roedel sdev->users = 1; 542672cf6dfSJoerg Roedel sdev->ops = ops; 543672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 544672cf6dfSJoerg Roedel 545672cf6dfSJoerg Roedel if (!svm) { 546672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 547672cf6dfSJoerg Roedel if (!svm) { 548672cf6dfSJoerg Roedel ret = -ENOMEM; 549672cf6dfSJoerg Roedel kfree(sdev); 550672cf6dfSJoerg Roedel goto out; 551672cf6dfSJoerg Roedel } 552672cf6dfSJoerg Roedel svm->iommu = iommu; 553672cf6dfSJoerg Roedel 554672cf6dfSJoerg Roedel if (pasid_max > intel_pasid_max_id) 555672cf6dfSJoerg Roedel pasid_max = intel_pasid_max_id; 556672cf6dfSJoerg Roedel 557672cf6dfSJoerg Roedel /* Do not use PASID 0, reserved for RID to PASID */ 558672cf6dfSJoerg Roedel svm->pasid = ioasid_alloc(NULL, PASID_MIN, 559672cf6dfSJoerg Roedel pasid_max - 1, svm); 560672cf6dfSJoerg Roedel if (svm->pasid == INVALID_IOASID) { 561672cf6dfSJoerg Roedel kfree(svm); 562672cf6dfSJoerg Roedel kfree(sdev); 563672cf6dfSJoerg Roedel ret = -ENOSPC; 564672cf6dfSJoerg Roedel goto out; 565672cf6dfSJoerg Roedel } 566672cf6dfSJoerg Roedel svm->notifier.ops = &intel_mmuops; 567672cf6dfSJoerg Roedel svm->mm = mm; 568672cf6dfSJoerg Roedel svm->flags = flags; 569672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 570672cf6dfSJoerg Roedel INIT_LIST_HEAD(&svm->list); 571672cf6dfSJoerg Roedel ret = -ENOMEM; 572672cf6dfSJoerg Roedel if (mm) { 573672cf6dfSJoerg Roedel ret = mmu_notifier_register(&svm->notifier, mm); 574672cf6dfSJoerg Roedel if (ret) { 575672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 576672cf6dfSJoerg Roedel kfree(svm); 577672cf6dfSJoerg Roedel kfree(sdev); 578672cf6dfSJoerg Roedel goto out; 579672cf6dfSJoerg Roedel } 580672cf6dfSJoerg Roedel } 581672cf6dfSJoerg Roedel 582672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 583672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 584672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 585672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 586672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 587672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 588672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 589672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 590672cf6dfSJoerg Roedel if (ret) { 591672cf6dfSJoerg Roedel if (mm) 592672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, mm); 593672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 594672cf6dfSJoerg Roedel kfree(svm); 595672cf6dfSJoerg Roedel kfree(sdev); 596672cf6dfSJoerg Roedel goto out; 597672cf6dfSJoerg Roedel } 598672cf6dfSJoerg Roedel 599672cf6dfSJoerg Roedel list_add_tail(&svm->list, &global_svm_list); 600672cf6dfSJoerg Roedel } else { 601672cf6dfSJoerg Roedel /* 602672cf6dfSJoerg Roedel * Binding a new device with existing PASID, need to setup 603672cf6dfSJoerg Roedel * the PASID entry. 604672cf6dfSJoerg Roedel */ 605672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 606672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 607672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 608672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 609672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 610672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 611672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 612672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 613672cf6dfSJoerg Roedel if (ret) { 614672cf6dfSJoerg Roedel kfree(sdev); 615672cf6dfSJoerg Roedel goto out; 616672cf6dfSJoerg Roedel } 617672cf6dfSJoerg Roedel } 618672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 619672cf6dfSJoerg Roedel success: 620672cf6dfSJoerg Roedel sdev->pasid = svm->pasid; 621672cf6dfSJoerg Roedel sdev->sva.dev = dev; 622672cf6dfSJoerg Roedel if (sd) 623672cf6dfSJoerg Roedel *sd = sdev; 624672cf6dfSJoerg Roedel ret = 0; 625672cf6dfSJoerg Roedel out: 626672cf6dfSJoerg Roedel return ret; 627672cf6dfSJoerg Roedel } 628672cf6dfSJoerg Roedel 629672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex */ 630672cf6dfSJoerg Roedel static int intel_svm_unbind_mm(struct device *dev, int pasid) 631672cf6dfSJoerg Roedel { 632672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 633672cf6dfSJoerg Roedel struct intel_iommu *iommu; 634672cf6dfSJoerg Roedel struct intel_svm *svm; 635672cf6dfSJoerg Roedel int ret = -EINVAL; 636672cf6dfSJoerg Roedel 637dd6692f1SLu Baolu iommu = device_to_iommu(dev, NULL, NULL); 638672cf6dfSJoerg Roedel if (!iommu) 639672cf6dfSJoerg Roedel goto out; 640672cf6dfSJoerg Roedel 64119abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 64219abcf70SLu Baolu if (ret) 643672cf6dfSJoerg Roedel goto out; 644672cf6dfSJoerg Roedel 64519abcf70SLu Baolu if (sdev) { 646672cf6dfSJoerg Roedel sdev->users--; 647672cf6dfSJoerg Roedel if (!sdev->users) { 648672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 649672cf6dfSJoerg Roedel /* Flush the PASID cache and IOTLB for this device. 650672cf6dfSJoerg Roedel * Note that we do depend on the hardware *not* using 651672cf6dfSJoerg Roedel * the PASID any more. Just as we depend on other 652672cf6dfSJoerg Roedel * devices never using PASIDs that they have no right 653672cf6dfSJoerg Roedel * to use. We have a *shared* PASID table, because it's 654672cf6dfSJoerg Roedel * large and has to be physically contiguous. So it's 655672cf6dfSJoerg Roedel * hard to be as defensive as we might like. */ 656672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 657672cf6dfSJoerg Roedel svm->pasid, false); 658672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 659672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 660672cf6dfSJoerg Roedel 661672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 662672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 663672cf6dfSJoerg Roedel if (svm->mm) 664672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, svm->mm); 665672cf6dfSJoerg Roedel list_del(&svm->list); 666672cf6dfSJoerg Roedel /* We mandate that no page faults may be outstanding 667672cf6dfSJoerg Roedel * for the PASID when intel_svm_unbind_mm() is called. 668672cf6dfSJoerg Roedel * If that is not obeyed, subtle errors will happen. 669672cf6dfSJoerg Roedel * Let's make them less subtle... */ 670672cf6dfSJoerg Roedel memset(svm, 0x6b, sizeof(*svm)); 671672cf6dfSJoerg Roedel kfree(svm); 672672cf6dfSJoerg Roedel } 673672cf6dfSJoerg Roedel } 674672cf6dfSJoerg Roedel } 675672cf6dfSJoerg Roedel out: 676672cf6dfSJoerg Roedel return ret; 677672cf6dfSJoerg Roedel } 678672cf6dfSJoerg Roedel 679672cf6dfSJoerg Roedel /* Page request queue descriptor */ 680672cf6dfSJoerg Roedel struct page_req_dsc { 681672cf6dfSJoerg Roedel union { 682672cf6dfSJoerg Roedel struct { 683672cf6dfSJoerg Roedel u64 type:8; 684672cf6dfSJoerg Roedel u64 pasid_present:1; 685672cf6dfSJoerg Roedel u64 priv_data_present:1; 686672cf6dfSJoerg Roedel u64 rsvd:6; 687672cf6dfSJoerg Roedel u64 rid:16; 688672cf6dfSJoerg Roedel u64 pasid:20; 689672cf6dfSJoerg Roedel u64 exe_req:1; 690672cf6dfSJoerg Roedel u64 pm_req:1; 691672cf6dfSJoerg Roedel u64 rsvd2:10; 692672cf6dfSJoerg Roedel }; 693672cf6dfSJoerg Roedel u64 qw_0; 694672cf6dfSJoerg Roedel }; 695672cf6dfSJoerg Roedel union { 696672cf6dfSJoerg Roedel struct { 697672cf6dfSJoerg Roedel u64 rd_req:1; 698672cf6dfSJoerg Roedel u64 wr_req:1; 699672cf6dfSJoerg Roedel u64 lpig:1; 700672cf6dfSJoerg Roedel u64 prg_index:9; 701672cf6dfSJoerg Roedel u64 addr:52; 702672cf6dfSJoerg Roedel }; 703672cf6dfSJoerg Roedel u64 qw_1; 704672cf6dfSJoerg Roedel }; 705672cf6dfSJoerg Roedel u64 priv_data[2]; 706672cf6dfSJoerg Roedel }; 707672cf6dfSJoerg Roedel 708672cf6dfSJoerg Roedel #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20) 709672cf6dfSJoerg Roedel 710672cf6dfSJoerg Roedel static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req) 711672cf6dfSJoerg Roedel { 712672cf6dfSJoerg Roedel unsigned long requested = 0; 713672cf6dfSJoerg Roedel 714672cf6dfSJoerg Roedel if (req->exe_req) 715672cf6dfSJoerg Roedel requested |= VM_EXEC; 716672cf6dfSJoerg Roedel 717672cf6dfSJoerg Roedel if (req->rd_req) 718672cf6dfSJoerg Roedel requested |= VM_READ; 719672cf6dfSJoerg Roedel 720672cf6dfSJoerg Roedel if (req->wr_req) 721672cf6dfSJoerg Roedel requested |= VM_WRITE; 722672cf6dfSJoerg Roedel 723672cf6dfSJoerg Roedel return (requested & ~vma->vm_flags) != 0; 724672cf6dfSJoerg Roedel } 725672cf6dfSJoerg Roedel 726672cf6dfSJoerg Roedel static bool is_canonical_address(u64 addr) 727672cf6dfSJoerg Roedel { 728672cf6dfSJoerg Roedel int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); 729672cf6dfSJoerg Roedel long saddr = (long) addr; 730672cf6dfSJoerg Roedel 731672cf6dfSJoerg Roedel return (((saddr << shift) >> shift) == saddr); 732672cf6dfSJoerg Roedel } 733672cf6dfSJoerg Roedel 734672cf6dfSJoerg Roedel /** 735672cf6dfSJoerg Roedel * intel_svm_drain_prq - Drain page requests and responses for a pasid 736672cf6dfSJoerg Roedel * @dev: target device 737672cf6dfSJoerg Roedel * @pasid: pasid for draining 738672cf6dfSJoerg Roedel * 739672cf6dfSJoerg Roedel * Drain all pending page requests and responses related to @pasid in both 740672cf6dfSJoerg Roedel * software and hardware. This is supposed to be called after the device 741672cf6dfSJoerg Roedel * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB 742672cf6dfSJoerg Roedel * and DevTLB have been invalidated. 743672cf6dfSJoerg Roedel * 744672cf6dfSJoerg Roedel * It waits until all pending page requests for @pasid in the page fault 745672cf6dfSJoerg Roedel * queue are completed by the prq handling thread. Then follow the steps 746672cf6dfSJoerg Roedel * described in VT-d spec CH7.10 to drain all page requests and page 747672cf6dfSJoerg Roedel * responses pending in the hardware. 748672cf6dfSJoerg Roedel */ 749672cf6dfSJoerg Roedel static void intel_svm_drain_prq(struct device *dev, int pasid) 750672cf6dfSJoerg Roedel { 751672cf6dfSJoerg Roedel struct device_domain_info *info; 752672cf6dfSJoerg Roedel struct dmar_domain *domain; 753672cf6dfSJoerg Roedel struct intel_iommu *iommu; 754672cf6dfSJoerg Roedel struct qi_desc desc[3]; 755672cf6dfSJoerg Roedel struct pci_dev *pdev; 756672cf6dfSJoerg Roedel int head, tail; 757672cf6dfSJoerg Roedel u16 sid, did; 758672cf6dfSJoerg Roedel int qdep; 759672cf6dfSJoerg Roedel 760672cf6dfSJoerg Roedel info = get_domain_info(dev); 761672cf6dfSJoerg Roedel if (WARN_ON(!info || !dev_is_pci(dev))) 762672cf6dfSJoerg Roedel return; 763672cf6dfSJoerg Roedel 764672cf6dfSJoerg Roedel if (!info->pri_enabled) 765672cf6dfSJoerg Roedel return; 766672cf6dfSJoerg Roedel 767672cf6dfSJoerg Roedel iommu = info->iommu; 768672cf6dfSJoerg Roedel domain = info->domain; 769672cf6dfSJoerg Roedel pdev = to_pci_dev(dev); 770672cf6dfSJoerg Roedel sid = PCI_DEVID(info->bus, info->devfn); 771672cf6dfSJoerg Roedel did = domain->iommu_did[iommu->seq_id]; 772672cf6dfSJoerg Roedel qdep = pci_ats_queue_depth(pdev); 773672cf6dfSJoerg Roedel 774672cf6dfSJoerg Roedel /* 775672cf6dfSJoerg Roedel * Check and wait until all pending page requests in the queue are 776672cf6dfSJoerg Roedel * handled by the prq handling thread. 777672cf6dfSJoerg Roedel */ 778672cf6dfSJoerg Roedel prq_retry: 779672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 780672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 781672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 782672cf6dfSJoerg Roedel while (head != tail) { 783672cf6dfSJoerg Roedel struct page_req_dsc *req; 784672cf6dfSJoerg Roedel 785672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 786672cf6dfSJoerg Roedel if (!req->pasid_present || req->pasid != pasid) { 787672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 788672cf6dfSJoerg Roedel continue; 789672cf6dfSJoerg Roedel } 790672cf6dfSJoerg Roedel 791672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 792672cf6dfSJoerg Roedel goto prq_retry; 793672cf6dfSJoerg Roedel } 794672cf6dfSJoerg Roedel 795672cf6dfSJoerg Roedel /* 796672cf6dfSJoerg Roedel * Perform steps described in VT-d spec CH7.10 to drain page 797672cf6dfSJoerg Roedel * requests and responses in hardware. 798672cf6dfSJoerg Roedel */ 799672cf6dfSJoerg Roedel memset(desc, 0, sizeof(desc)); 800672cf6dfSJoerg Roedel desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) | 801672cf6dfSJoerg Roedel QI_IWD_FENCE | 802672cf6dfSJoerg Roedel QI_IWD_TYPE; 803672cf6dfSJoerg Roedel desc[1].qw0 = QI_EIOTLB_PASID(pasid) | 804672cf6dfSJoerg Roedel QI_EIOTLB_DID(did) | 805672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | 806672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 807672cf6dfSJoerg Roedel desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) | 808672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SID(sid) | 809672cf6dfSJoerg Roedel QI_DEV_EIOTLB_QDEP(qdep) | 810672cf6dfSJoerg Roedel QI_DEIOTLB_TYPE | 811672cf6dfSJoerg Roedel QI_DEV_IOTLB_PFSID(info->pfsid); 812672cf6dfSJoerg Roedel qi_retry: 813672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 814672cf6dfSJoerg Roedel qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN); 815672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) { 816672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 817672cf6dfSJoerg Roedel goto qi_retry; 818672cf6dfSJoerg Roedel } 819672cf6dfSJoerg Roedel } 820672cf6dfSJoerg Roedel 821eb8d93eaSLu Baolu static int prq_to_iommu_prot(struct page_req_dsc *req) 822eb8d93eaSLu Baolu { 823eb8d93eaSLu Baolu int prot = 0; 824eb8d93eaSLu Baolu 825eb8d93eaSLu Baolu if (req->rd_req) 826eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_READ; 827eb8d93eaSLu Baolu if (req->wr_req) 828eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_WRITE; 829eb8d93eaSLu Baolu if (req->exe_req) 830eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_EXEC; 831eb8d93eaSLu Baolu if (req->pm_req) 832eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_PRIV; 833eb8d93eaSLu Baolu 834eb8d93eaSLu Baolu return prot; 835eb8d93eaSLu Baolu } 836eb8d93eaSLu Baolu 837eb8d93eaSLu Baolu static int 838eb8d93eaSLu Baolu intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc) 839eb8d93eaSLu Baolu { 840eb8d93eaSLu Baolu struct iommu_fault_event event; 841eb8d93eaSLu Baolu 842eb8d93eaSLu Baolu if (!dev || !dev_is_pci(dev)) 843eb8d93eaSLu Baolu return -ENODEV; 844eb8d93eaSLu Baolu 845eb8d93eaSLu Baolu /* Fill in event data for device specific processing */ 846eb8d93eaSLu Baolu memset(&event, 0, sizeof(struct iommu_fault_event)); 847eb8d93eaSLu Baolu event.fault.type = IOMMU_FAULT_PAGE_REQ; 848eb8d93eaSLu Baolu event.fault.prm.addr = desc->addr; 849eb8d93eaSLu Baolu event.fault.prm.pasid = desc->pasid; 850eb8d93eaSLu Baolu event.fault.prm.grpid = desc->prg_index; 851eb8d93eaSLu Baolu event.fault.prm.perm = prq_to_iommu_prot(desc); 852eb8d93eaSLu Baolu 853eb8d93eaSLu Baolu if (desc->lpig) 854eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 855eb8d93eaSLu Baolu if (desc->pasid_present) { 856eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 857eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID; 858eb8d93eaSLu Baolu } 859eb8d93eaSLu Baolu if (desc->priv_data_present) { 860eb8d93eaSLu Baolu /* 861eb8d93eaSLu Baolu * Set last page in group bit if private data is present, 862eb8d93eaSLu Baolu * page response is required as it does for LPIG. 863eb8d93eaSLu Baolu * iommu_report_device_fault() doesn't understand this vendor 864eb8d93eaSLu Baolu * specific requirement thus we set last_page as a workaround. 865eb8d93eaSLu Baolu */ 866eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 867eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 868eb8d93eaSLu Baolu memcpy(event.fault.prm.private_data, desc->priv_data, 869eb8d93eaSLu Baolu sizeof(desc->priv_data)); 870eb8d93eaSLu Baolu } 871eb8d93eaSLu Baolu 872eb8d93eaSLu Baolu return iommu_report_device_fault(dev, &event); 873eb8d93eaSLu Baolu } 874eb8d93eaSLu Baolu 875672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d) 876672cf6dfSJoerg Roedel { 877eb8d93eaSLu Baolu struct intel_svm_dev *sdev = NULL; 878672cf6dfSJoerg Roedel struct intel_iommu *iommu = d; 879672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 880672cf6dfSJoerg Roedel int head, tail, handled = 0; 881672cf6dfSJoerg Roedel 882672cf6dfSJoerg Roedel /* Clear PPR bit before reading head/tail registers, to 883672cf6dfSJoerg Roedel * ensure that we get a new interrupt if needed. */ 884672cf6dfSJoerg Roedel writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG); 885672cf6dfSJoerg Roedel 886672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 887672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 888672cf6dfSJoerg Roedel while (head != tail) { 889672cf6dfSJoerg Roedel struct vm_area_struct *vma; 890672cf6dfSJoerg Roedel struct page_req_dsc *req; 891672cf6dfSJoerg Roedel struct qi_desc resp; 892672cf6dfSJoerg Roedel int result; 893672cf6dfSJoerg Roedel vm_fault_t ret; 894672cf6dfSJoerg Roedel u64 address; 895672cf6dfSJoerg Roedel 896672cf6dfSJoerg Roedel handled = 1; 897672cf6dfSJoerg Roedel 898672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 899672cf6dfSJoerg Roedel 900672cf6dfSJoerg Roedel result = QI_RESP_FAILURE; 901672cf6dfSJoerg Roedel address = (u64)req->addr << VTD_PAGE_SHIFT; 902672cf6dfSJoerg Roedel if (!req->pasid_present) { 903672cf6dfSJoerg Roedel pr_err("%s: Page request without PASID: %08llx %08llx\n", 904672cf6dfSJoerg Roedel iommu->name, ((unsigned long long *)req)[0], 905672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 906672cf6dfSJoerg Roedel goto no_pasid; 907672cf6dfSJoerg Roedel } 908672cf6dfSJoerg Roedel 909672cf6dfSJoerg Roedel if (!svm || svm->pasid != req->pasid) { 910672cf6dfSJoerg Roedel rcu_read_lock(); 911672cf6dfSJoerg Roedel svm = ioasid_find(NULL, req->pasid, NULL); 912672cf6dfSJoerg Roedel /* It *can't* go away, because the driver is not permitted 913672cf6dfSJoerg Roedel * to unbind the mm while any page faults are outstanding. 914672cf6dfSJoerg Roedel * So we only need RCU to protect the internal idr code. */ 915672cf6dfSJoerg Roedel rcu_read_unlock(); 916672cf6dfSJoerg Roedel if (IS_ERR_OR_NULL(svm)) { 917672cf6dfSJoerg Roedel pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n", 918672cf6dfSJoerg Roedel iommu->name, req->pasid, ((unsigned long long *)req)[0], 919672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 920672cf6dfSJoerg Roedel goto no_pasid; 921672cf6dfSJoerg Roedel } 922672cf6dfSJoerg Roedel } 923672cf6dfSJoerg Roedel 924eb8d93eaSLu Baolu if (!sdev || sdev->sid != req->rid) { 925eb8d93eaSLu Baolu struct intel_svm_dev *t; 926eb8d93eaSLu Baolu 927eb8d93eaSLu Baolu sdev = NULL; 928eb8d93eaSLu Baolu rcu_read_lock(); 929eb8d93eaSLu Baolu list_for_each_entry_rcu(t, &svm->devs, list) { 930eb8d93eaSLu Baolu if (t->sid == req->rid) { 931eb8d93eaSLu Baolu sdev = t; 932eb8d93eaSLu Baolu break; 933eb8d93eaSLu Baolu } 934eb8d93eaSLu Baolu } 935eb8d93eaSLu Baolu rcu_read_unlock(); 936eb8d93eaSLu Baolu } 937eb8d93eaSLu Baolu 938672cf6dfSJoerg Roedel result = QI_RESP_INVALID; 939672cf6dfSJoerg Roedel /* Since we're using init_mm.pgd directly, we should never take 940672cf6dfSJoerg Roedel * any faults on kernel addresses. */ 941672cf6dfSJoerg Roedel if (!svm->mm) 942672cf6dfSJoerg Roedel goto bad_req; 943672cf6dfSJoerg Roedel 944672cf6dfSJoerg Roedel /* If address is not canonical, return invalid response */ 945672cf6dfSJoerg Roedel if (!is_canonical_address(address)) 946672cf6dfSJoerg Roedel goto bad_req; 947672cf6dfSJoerg Roedel 948eb8d93eaSLu Baolu /* 949eb8d93eaSLu Baolu * If prq is to be handled outside iommu driver via receiver of 950eb8d93eaSLu Baolu * the fault notifiers, we skip the page response here. 951eb8d93eaSLu Baolu */ 952eb8d93eaSLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 953eb8d93eaSLu Baolu if (sdev && !intel_svm_prq_report(sdev->dev, req)) 954eb8d93eaSLu Baolu goto prq_advance; 955eb8d93eaSLu Baolu else 956eb8d93eaSLu Baolu goto bad_req; 957eb8d93eaSLu Baolu } 958eb8d93eaSLu Baolu 959672cf6dfSJoerg Roedel /* If the mm is already defunct, don't handle faults. */ 960672cf6dfSJoerg Roedel if (!mmget_not_zero(svm->mm)) 961672cf6dfSJoerg Roedel goto bad_req; 962672cf6dfSJoerg Roedel 9638f02f363SLinus Torvalds mmap_read_lock(svm->mm); 964672cf6dfSJoerg Roedel vma = find_extend_vma(svm->mm, address); 965672cf6dfSJoerg Roedel if (!vma || address < vma->vm_start) 966672cf6dfSJoerg Roedel goto invalid; 967672cf6dfSJoerg Roedel 968672cf6dfSJoerg Roedel if (access_error(vma, req)) 969672cf6dfSJoerg Roedel goto invalid; 970672cf6dfSJoerg Roedel 971672cf6dfSJoerg Roedel ret = handle_mm_fault(vma, address, 972bce617edSPeter Xu req->wr_req ? FAULT_FLAG_WRITE : 0, 973bce617edSPeter Xu NULL); 974672cf6dfSJoerg Roedel if (ret & VM_FAULT_ERROR) 975672cf6dfSJoerg Roedel goto invalid; 976672cf6dfSJoerg Roedel 977672cf6dfSJoerg Roedel result = QI_RESP_SUCCESS; 978672cf6dfSJoerg Roedel invalid: 9798f02f363SLinus Torvalds mmap_read_unlock(svm->mm); 980672cf6dfSJoerg Roedel mmput(svm->mm); 981672cf6dfSJoerg Roedel bad_req: 982eb8d93eaSLu Baolu WARN_ON(!sdev); 983672cf6dfSJoerg Roedel if (sdev && sdev->ops && sdev->ops->fault_cb) { 984672cf6dfSJoerg Roedel int rwxp = (req->rd_req << 3) | (req->wr_req << 2) | 985672cf6dfSJoerg Roedel (req->exe_req << 1) | (req->pm_req); 986672cf6dfSJoerg Roedel sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, 987672cf6dfSJoerg Roedel req->priv_data, rwxp, result); 988672cf6dfSJoerg Roedel } 989672cf6dfSJoerg Roedel /* We get here in the error case where the PASID lookup failed, 990672cf6dfSJoerg Roedel and these can be NULL. Do not use them below this point! */ 991672cf6dfSJoerg Roedel sdev = NULL; 992672cf6dfSJoerg Roedel svm = NULL; 993672cf6dfSJoerg Roedel no_pasid: 994672cf6dfSJoerg Roedel if (req->lpig || req->priv_data_present) { 995672cf6dfSJoerg Roedel /* 996672cf6dfSJoerg Roedel * Per VT-d spec. v3.0 ch7.7, system software must 997672cf6dfSJoerg Roedel * respond with page group response if private data 998672cf6dfSJoerg Roedel * is present (PDP) or last page in group (LPIG) bit 999672cf6dfSJoerg Roedel * is set. This is an additional VT-d feature beyond 1000672cf6dfSJoerg Roedel * PCI ATS spec. 1001672cf6dfSJoerg Roedel */ 1002672cf6dfSJoerg Roedel resp.qw0 = QI_PGRP_PASID(req->pasid) | 1003672cf6dfSJoerg Roedel QI_PGRP_DID(req->rid) | 1004672cf6dfSJoerg Roedel QI_PGRP_PASID_P(req->pasid_present) | 1005672cf6dfSJoerg Roedel QI_PGRP_PDP(req->pasid_present) | 1006672cf6dfSJoerg Roedel QI_PGRP_RESP_CODE(result) | 1007672cf6dfSJoerg Roedel QI_PGRP_RESP_TYPE; 1008672cf6dfSJoerg Roedel resp.qw1 = QI_PGRP_IDX(req->prg_index) | 1009672cf6dfSJoerg Roedel QI_PGRP_LPIG(req->lpig); 1010672cf6dfSJoerg Roedel 1011672cf6dfSJoerg Roedel if (req->priv_data_present) 1012672cf6dfSJoerg Roedel memcpy(&resp.qw2, req->priv_data, 1013672cf6dfSJoerg Roedel sizeof(req->priv_data)); 1014672cf6dfSJoerg Roedel resp.qw2 = 0; 1015672cf6dfSJoerg Roedel resp.qw3 = 0; 1016672cf6dfSJoerg Roedel qi_submit_sync(iommu, &resp, 1, 0); 1017672cf6dfSJoerg Roedel } 1018eb8d93eaSLu Baolu prq_advance: 1019672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 1020672cf6dfSJoerg Roedel } 1021672cf6dfSJoerg Roedel 1022672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, tail); 1023672cf6dfSJoerg Roedel 1024672cf6dfSJoerg Roedel /* 1025672cf6dfSJoerg Roedel * Clear the page request overflow bit and wake up all threads that 1026672cf6dfSJoerg Roedel * are waiting for the completion of this handling. 1027672cf6dfSJoerg Roedel */ 1028672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) 1029672cf6dfSJoerg Roedel writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG); 1030672cf6dfSJoerg Roedel 1031672cf6dfSJoerg Roedel if (!completion_done(&iommu->prq_complete)) 1032672cf6dfSJoerg Roedel complete(&iommu->prq_complete); 1033672cf6dfSJoerg Roedel 1034672cf6dfSJoerg Roedel return IRQ_RETVAL(handled); 1035672cf6dfSJoerg Roedel } 1036672cf6dfSJoerg Roedel 1037672cf6dfSJoerg Roedel #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva) 1038672cf6dfSJoerg Roedel struct iommu_sva * 1039672cf6dfSJoerg Roedel intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata) 1040672cf6dfSJoerg Roedel { 1041672cf6dfSJoerg Roedel struct iommu_sva *sva = ERR_PTR(-EINVAL); 1042672cf6dfSJoerg Roedel struct intel_svm_dev *sdev = NULL; 1043672cf6dfSJoerg Roedel int flags = 0; 1044672cf6dfSJoerg Roedel int ret; 1045672cf6dfSJoerg Roedel 1046672cf6dfSJoerg Roedel /* 1047672cf6dfSJoerg Roedel * TODO: Consolidate with generic iommu-sva bind after it is merged. 1048672cf6dfSJoerg Roedel * It will require shared SVM data structures, i.e. combine io_mm 1049672cf6dfSJoerg Roedel * and intel_svm etc. 1050672cf6dfSJoerg Roedel */ 1051672cf6dfSJoerg Roedel if (drvdata) 1052672cf6dfSJoerg Roedel flags = *(int *)drvdata; 1053672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1054672cf6dfSJoerg Roedel ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev); 1055672cf6dfSJoerg Roedel if (ret) 1056672cf6dfSJoerg Roedel sva = ERR_PTR(ret); 1057672cf6dfSJoerg Roedel else if (sdev) 1058672cf6dfSJoerg Roedel sva = &sdev->sva; 1059672cf6dfSJoerg Roedel else 1060672cf6dfSJoerg Roedel WARN(!sdev, "SVM bind succeeded with no sdev!\n"); 1061672cf6dfSJoerg Roedel 1062672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1063672cf6dfSJoerg Roedel 1064672cf6dfSJoerg Roedel return sva; 1065672cf6dfSJoerg Roedel } 1066672cf6dfSJoerg Roedel 1067672cf6dfSJoerg Roedel void intel_svm_unbind(struct iommu_sva *sva) 1068672cf6dfSJoerg Roedel { 1069672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1070672cf6dfSJoerg Roedel 1071672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1072672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1073672cf6dfSJoerg Roedel intel_svm_unbind_mm(sdev->dev, sdev->pasid); 1074672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1075672cf6dfSJoerg Roedel } 1076672cf6dfSJoerg Roedel 1077672cf6dfSJoerg Roedel int intel_svm_get_pasid(struct iommu_sva *sva) 1078672cf6dfSJoerg Roedel { 1079672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1080672cf6dfSJoerg Roedel int pasid; 1081672cf6dfSJoerg Roedel 1082672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1083672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1084672cf6dfSJoerg Roedel pasid = sdev->pasid; 1085672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1086672cf6dfSJoerg Roedel 1087672cf6dfSJoerg Roedel return pasid; 1088672cf6dfSJoerg Roedel } 10898b737121SLu Baolu 10908b737121SLu Baolu int intel_svm_page_response(struct device *dev, 10918b737121SLu Baolu struct iommu_fault_event *evt, 10928b737121SLu Baolu struct iommu_page_response *msg) 10938b737121SLu Baolu { 10948b737121SLu Baolu struct iommu_fault_page_request *prm; 10958b737121SLu Baolu struct intel_svm_dev *sdev = NULL; 10968b737121SLu Baolu struct intel_svm *svm = NULL; 10978b737121SLu Baolu struct intel_iommu *iommu; 10988b737121SLu Baolu bool private_present; 10998b737121SLu Baolu bool pasid_present; 11008b737121SLu Baolu bool last_page; 11018b737121SLu Baolu u8 bus, devfn; 11028b737121SLu Baolu int ret = 0; 11038b737121SLu Baolu u16 sid; 11048b737121SLu Baolu 11058b737121SLu Baolu if (!dev || !dev_is_pci(dev)) 11068b737121SLu Baolu return -ENODEV; 11078b737121SLu Baolu 11088b737121SLu Baolu iommu = device_to_iommu(dev, &bus, &devfn); 11098b737121SLu Baolu if (!iommu) 11108b737121SLu Baolu return -ENODEV; 11118b737121SLu Baolu 11128b737121SLu Baolu if (!msg || !evt) 11138b737121SLu Baolu return -EINVAL; 11148b737121SLu Baolu 11158b737121SLu Baolu mutex_lock(&pasid_mutex); 11168b737121SLu Baolu 11178b737121SLu Baolu prm = &evt->fault.prm; 11188b737121SLu Baolu sid = PCI_DEVID(bus, devfn); 11198b737121SLu Baolu pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 11208b737121SLu Baolu private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 11218b737121SLu Baolu last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 11228b737121SLu Baolu 11238b737121SLu Baolu if (!pasid_present) { 11248b737121SLu Baolu ret = -EINVAL; 11258b737121SLu Baolu goto out; 11268b737121SLu Baolu } 11278b737121SLu Baolu 11288b737121SLu Baolu if (prm->pasid == 0 || prm->pasid >= PASID_MAX) { 11298b737121SLu Baolu ret = -EINVAL; 11308b737121SLu Baolu goto out; 11318b737121SLu Baolu } 11328b737121SLu Baolu 11338b737121SLu Baolu ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev); 11348b737121SLu Baolu if (ret || !sdev) { 11358b737121SLu Baolu ret = -ENODEV; 11368b737121SLu Baolu goto out; 11378b737121SLu Baolu } 11388b737121SLu Baolu 11398b737121SLu Baolu /* 11408b737121SLu Baolu * For responses from userspace, need to make sure that the 11418b737121SLu Baolu * pasid has been bound to its mm. 11428b737121SLu Baolu */ 11438b737121SLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 11448b737121SLu Baolu struct mm_struct *mm; 11458b737121SLu Baolu 11468b737121SLu Baolu mm = get_task_mm(current); 11478b737121SLu Baolu if (!mm) { 11488b737121SLu Baolu ret = -EINVAL; 11498b737121SLu Baolu goto out; 11508b737121SLu Baolu } 11518b737121SLu Baolu 11528b737121SLu Baolu if (mm != svm->mm) { 11538b737121SLu Baolu ret = -ENODEV; 11548b737121SLu Baolu mmput(mm); 11558b737121SLu Baolu goto out; 11568b737121SLu Baolu } 11578b737121SLu Baolu 11588b737121SLu Baolu mmput(mm); 11598b737121SLu Baolu } 11608b737121SLu Baolu 11618b737121SLu Baolu /* 11628b737121SLu Baolu * Per VT-d spec. v3.0 ch7.7, system software must respond 11638b737121SLu Baolu * with page group response if private data is present (PDP) 11648b737121SLu Baolu * or last page in group (LPIG) bit is set. This is an 11658b737121SLu Baolu * additional VT-d requirement beyond PCI ATS spec. 11668b737121SLu Baolu */ 11678b737121SLu Baolu if (last_page || private_present) { 11688b737121SLu Baolu struct qi_desc desc; 11698b737121SLu Baolu 11708b737121SLu Baolu desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) | 11718b737121SLu Baolu QI_PGRP_PASID_P(pasid_present) | 11728b737121SLu Baolu QI_PGRP_PDP(private_present) | 11738b737121SLu Baolu QI_PGRP_RESP_CODE(msg->code) | 11748b737121SLu Baolu QI_PGRP_RESP_TYPE; 11758b737121SLu Baolu desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page); 11768b737121SLu Baolu desc.qw2 = 0; 11778b737121SLu Baolu desc.qw3 = 0; 11788b737121SLu Baolu if (private_present) 11798b737121SLu Baolu memcpy(&desc.qw2, prm->private_data, 11808b737121SLu Baolu sizeof(prm->private_data)); 11818b737121SLu Baolu 11828b737121SLu Baolu qi_submit_sync(iommu, &desc, 1, 0); 11838b737121SLu Baolu } 11848b737121SLu Baolu out: 11858b737121SLu Baolu mutex_unlock(&pasid_mutex); 11868b737121SLu Baolu return ret; 11878b737121SLu Baolu } 1188