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); 26*c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 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 287672cf6dfSJoerg Roedel if (data->version != IOMMU_GPASID_BIND_VERSION_1 || 288672cf6dfSJoerg Roedel data->format != IOMMU_PASID_FORMAT_INTEL_VTD) 289672cf6dfSJoerg Roedel return -EINVAL; 290672cf6dfSJoerg Roedel 291672cf6dfSJoerg Roedel if (!dev_is_pci(dev)) 292672cf6dfSJoerg Roedel return -ENOTSUPP; 293672cf6dfSJoerg Roedel 294672cf6dfSJoerg Roedel /* VT-d supports devices with full 20 bit PASIDs only */ 295672cf6dfSJoerg Roedel if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX) 296672cf6dfSJoerg Roedel return -EINVAL; 297672cf6dfSJoerg Roedel 298672cf6dfSJoerg Roedel /* 299672cf6dfSJoerg Roedel * We only check host PASID range, we have no knowledge to check 300672cf6dfSJoerg Roedel * guest PASID range. 301672cf6dfSJoerg Roedel */ 302672cf6dfSJoerg Roedel if (data->hpasid <= 0 || data->hpasid >= PASID_MAX) 303672cf6dfSJoerg Roedel return -EINVAL; 304672cf6dfSJoerg Roedel 305672cf6dfSJoerg Roedel dmar_domain = to_dmar_domain(domain); 306672cf6dfSJoerg Roedel 307672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 30819abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev); 30919abcf70SLu Baolu if (ret) 310672cf6dfSJoerg Roedel goto out; 311672cf6dfSJoerg Roedel 31219abcf70SLu Baolu if (sdev) { 313672cf6dfSJoerg Roedel /* 314d315e9e6SJacob Pan * Do not allow multiple bindings of the same device-PASID since 315d315e9e6SJacob Pan * there is only one SL page tables per PASID. We may revisit 316d315e9e6SJacob Pan * once sharing PGD across domains are supported. 317672cf6dfSJoerg Roedel */ 31819abcf70SLu Baolu dev_warn_ratelimited(dev, "Already bound with PASID %u\n", 319672cf6dfSJoerg Roedel svm->pasid); 320672cf6dfSJoerg Roedel ret = -EBUSY; 321672cf6dfSJoerg Roedel goto out; 322672cf6dfSJoerg Roedel } 32319abcf70SLu Baolu 32419abcf70SLu Baolu if (!svm) { 325672cf6dfSJoerg Roedel /* We come here when PASID has never been bond to a device. */ 326672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 327672cf6dfSJoerg Roedel if (!svm) { 328672cf6dfSJoerg Roedel ret = -ENOMEM; 329672cf6dfSJoerg Roedel goto out; 330672cf6dfSJoerg Roedel } 331672cf6dfSJoerg Roedel /* REVISIT: upper layer/VFIO can track host process that bind 332672cf6dfSJoerg Roedel * the PASID. ioasid_set = mm might be sufficient for vfio to 333672cf6dfSJoerg Roedel * check pasid VMM ownership. We can drop the following line 334672cf6dfSJoerg Roedel * once VFIO and IOASID set check is in place. 335672cf6dfSJoerg Roedel */ 336672cf6dfSJoerg Roedel svm->mm = get_task_mm(current); 337672cf6dfSJoerg Roedel svm->pasid = data->hpasid; 338672cf6dfSJoerg Roedel if (data->flags & IOMMU_SVA_GPASID_VAL) { 339672cf6dfSJoerg Roedel svm->gpasid = data->gpasid; 340672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_PASID; 341672cf6dfSJoerg Roedel } 342672cf6dfSJoerg Roedel ioasid_set_data(data->hpasid, svm); 343672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 344672cf6dfSJoerg Roedel mmput(svm->mm); 345672cf6dfSJoerg Roedel } 346672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 347672cf6dfSJoerg Roedel if (!sdev) { 348672cf6dfSJoerg Roedel ret = -ENOMEM; 349672cf6dfSJoerg Roedel goto out; 350672cf6dfSJoerg Roedel } 351672cf6dfSJoerg Roedel sdev->dev = dev; 352672cf6dfSJoerg Roedel 353672cf6dfSJoerg Roedel /* Only count users if device has aux domains */ 354672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 355672cf6dfSJoerg Roedel sdev->users = 1; 356672cf6dfSJoerg Roedel 357672cf6dfSJoerg Roedel /* Set up device context entry for PASID if not enabled already */ 358672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, sdev->dev); 359672cf6dfSJoerg Roedel if (ret) { 360672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to enable PASID capability\n"); 361672cf6dfSJoerg Roedel kfree(sdev); 362672cf6dfSJoerg Roedel goto out; 363672cf6dfSJoerg Roedel } 364672cf6dfSJoerg Roedel 365672cf6dfSJoerg Roedel /* 366672cf6dfSJoerg Roedel * PASID table is per device for better security. Therefore, for 367672cf6dfSJoerg Roedel * each bind of a new device even with an existing PASID, we need to 368672cf6dfSJoerg Roedel * call the nested mode setup function here. 369672cf6dfSJoerg Roedel */ 370672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 371672cf6dfSJoerg Roedel ret = intel_pasid_setup_nested(iommu, dev, 372672cf6dfSJoerg Roedel (pgd_t *)(uintptr_t)data->gpgd, 373672cf6dfSJoerg Roedel data->hpasid, &data->vtd, dmar_domain, 374672cf6dfSJoerg Roedel data->addr_width); 375672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 376672cf6dfSJoerg Roedel if (ret) { 377672cf6dfSJoerg Roedel dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n", 378672cf6dfSJoerg Roedel data->hpasid, ret); 379672cf6dfSJoerg Roedel /* 380672cf6dfSJoerg Roedel * PASID entry should be in cleared state if nested mode 381672cf6dfSJoerg Roedel * set up failed. So we only need to clear IOASID tracking 382672cf6dfSJoerg Roedel * data such that free call will succeed. 383672cf6dfSJoerg Roedel */ 384672cf6dfSJoerg Roedel kfree(sdev); 385672cf6dfSJoerg Roedel goto out; 386672cf6dfSJoerg Roedel } 387672cf6dfSJoerg Roedel 388672cf6dfSJoerg Roedel svm->flags |= SVM_FLAG_GUEST_MODE; 389672cf6dfSJoerg Roedel 390672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 391672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 392672cf6dfSJoerg Roedel out: 393672cf6dfSJoerg Roedel if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) { 394672cf6dfSJoerg Roedel ioasid_set_data(data->hpasid, NULL); 395672cf6dfSJoerg Roedel kfree(svm); 396672cf6dfSJoerg Roedel } 397672cf6dfSJoerg Roedel 398672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 399672cf6dfSJoerg Roedel return ret; 400672cf6dfSJoerg Roedel } 401672cf6dfSJoerg Roedel 402*c7b6bac9SFenghua Yu int intel_svm_unbind_gpasid(struct device *dev, u32 pasid) 403672cf6dfSJoerg Roedel { 404dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 405672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 406672cf6dfSJoerg Roedel struct intel_svm *svm; 40719abcf70SLu Baolu int ret; 408672cf6dfSJoerg Roedel 409672cf6dfSJoerg Roedel if (WARN_ON(!iommu)) 410672cf6dfSJoerg Roedel return -EINVAL; 411672cf6dfSJoerg Roedel 412672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 41319abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 41419abcf70SLu Baolu if (ret) 415672cf6dfSJoerg Roedel goto out; 416672cf6dfSJoerg Roedel 41719abcf70SLu Baolu if (sdev) { 418672cf6dfSJoerg Roedel if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) 419672cf6dfSJoerg Roedel sdev->users--; 420672cf6dfSJoerg Roedel if (!sdev->users) { 421672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 422672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 423672cf6dfSJoerg Roedel svm->pasid, false); 424672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 425672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 426672cf6dfSJoerg Roedel 427672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 428672cf6dfSJoerg Roedel /* 429672cf6dfSJoerg Roedel * We do not free the IOASID here in that 430672cf6dfSJoerg Roedel * IOMMU driver did not allocate it. 431672cf6dfSJoerg Roedel * Unlike native SVM, IOASID for guest use was 432672cf6dfSJoerg Roedel * allocated prior to the bind call. 433672cf6dfSJoerg Roedel * In any case, if the free call comes before 434672cf6dfSJoerg Roedel * the unbind, IOMMU driver will get notified 435672cf6dfSJoerg Roedel * and perform cleanup. 436672cf6dfSJoerg Roedel */ 437672cf6dfSJoerg Roedel ioasid_set_data(pasid, NULL); 438672cf6dfSJoerg Roedel kfree(svm); 439672cf6dfSJoerg Roedel } 440672cf6dfSJoerg Roedel } 441672cf6dfSJoerg Roedel } 442672cf6dfSJoerg Roedel out: 443672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 444672cf6dfSJoerg Roedel return ret; 445672cf6dfSJoerg Roedel } 446672cf6dfSJoerg Roedel 447672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex, mm reference */ 448672cf6dfSJoerg Roedel static int 449672cf6dfSJoerg Roedel intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops, 450672cf6dfSJoerg Roedel struct mm_struct *mm, struct intel_svm_dev **sd) 451672cf6dfSJoerg Roedel { 452dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 453672cf6dfSJoerg Roedel struct device_domain_info *info; 454672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 455672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 456672cf6dfSJoerg Roedel int pasid_max; 457672cf6dfSJoerg Roedel int ret; 458672cf6dfSJoerg Roedel 459672cf6dfSJoerg Roedel if (!iommu || dmar_disabled) 460672cf6dfSJoerg Roedel return -EINVAL; 461672cf6dfSJoerg Roedel 462672cf6dfSJoerg Roedel if (!intel_svm_capable(iommu)) 463672cf6dfSJoerg Roedel return -ENOTSUPP; 464672cf6dfSJoerg Roedel 465672cf6dfSJoerg Roedel if (dev_is_pci(dev)) { 466672cf6dfSJoerg Roedel pasid_max = pci_max_pasids(to_pci_dev(dev)); 467672cf6dfSJoerg Roedel if (pasid_max < 0) 468672cf6dfSJoerg Roedel return -EINVAL; 469672cf6dfSJoerg Roedel } else 470672cf6dfSJoerg Roedel pasid_max = 1 << 20; 471672cf6dfSJoerg Roedel 472672cf6dfSJoerg Roedel /* Bind supervisor PASID shuld have mm = NULL */ 473672cf6dfSJoerg Roedel if (flags & SVM_FLAG_SUPERVISOR_MODE) { 474672cf6dfSJoerg Roedel if (!ecap_srs(iommu->ecap) || mm) { 475672cf6dfSJoerg Roedel pr_err("Supervisor PASID with user provided mm.\n"); 476672cf6dfSJoerg Roedel return -EINVAL; 477672cf6dfSJoerg Roedel } 478672cf6dfSJoerg Roedel } 479672cf6dfSJoerg Roedel 480672cf6dfSJoerg Roedel if (!(flags & SVM_FLAG_PRIVATE_PASID)) { 481672cf6dfSJoerg Roedel struct intel_svm *t; 482672cf6dfSJoerg Roedel 483672cf6dfSJoerg Roedel list_for_each_entry(t, &global_svm_list, list) { 484672cf6dfSJoerg Roedel if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID)) 485672cf6dfSJoerg Roedel continue; 486672cf6dfSJoerg Roedel 487672cf6dfSJoerg Roedel svm = t; 488672cf6dfSJoerg Roedel if (svm->pasid >= pasid_max) { 489672cf6dfSJoerg Roedel dev_warn(dev, 490672cf6dfSJoerg Roedel "Limited PASID width. Cannot use existing PASID %d\n", 491672cf6dfSJoerg Roedel svm->pasid); 492672cf6dfSJoerg Roedel ret = -ENOSPC; 493672cf6dfSJoerg Roedel goto out; 494672cf6dfSJoerg Roedel } 495672cf6dfSJoerg Roedel 496672cf6dfSJoerg Roedel /* Find the matching device in svm list */ 497672cf6dfSJoerg Roedel for_each_svm_dev(sdev, svm, dev) { 498672cf6dfSJoerg Roedel if (sdev->ops != ops) { 499672cf6dfSJoerg Roedel ret = -EBUSY; 500672cf6dfSJoerg Roedel goto out; 501672cf6dfSJoerg Roedel } 502672cf6dfSJoerg Roedel sdev->users++; 503672cf6dfSJoerg Roedel goto success; 504672cf6dfSJoerg Roedel } 505672cf6dfSJoerg Roedel 506672cf6dfSJoerg Roedel break; 507672cf6dfSJoerg Roedel } 508672cf6dfSJoerg Roedel } 509672cf6dfSJoerg Roedel 510672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 511672cf6dfSJoerg Roedel if (!sdev) { 512672cf6dfSJoerg Roedel ret = -ENOMEM; 513672cf6dfSJoerg Roedel goto out; 514672cf6dfSJoerg Roedel } 515672cf6dfSJoerg Roedel sdev->dev = dev; 516672cf6dfSJoerg Roedel 517672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, dev); 518672cf6dfSJoerg Roedel if (ret) { 519672cf6dfSJoerg Roedel kfree(sdev); 520672cf6dfSJoerg Roedel goto out; 521672cf6dfSJoerg Roedel } 522672cf6dfSJoerg Roedel 523672cf6dfSJoerg Roedel info = get_domain_info(dev); 524672cf6dfSJoerg Roedel sdev->did = FLPT_DEFAULT_DID; 525672cf6dfSJoerg Roedel sdev->sid = PCI_DEVID(info->bus, info->devfn); 526672cf6dfSJoerg Roedel if (info->ats_enabled) { 527672cf6dfSJoerg Roedel sdev->dev_iotlb = 1; 528672cf6dfSJoerg Roedel sdev->qdep = info->ats_qdep; 529672cf6dfSJoerg Roedel if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) 530672cf6dfSJoerg Roedel sdev->qdep = 0; 531672cf6dfSJoerg Roedel } 532672cf6dfSJoerg Roedel 533672cf6dfSJoerg Roedel /* Finish the setup now we know we're keeping it */ 534672cf6dfSJoerg Roedel sdev->users = 1; 535672cf6dfSJoerg Roedel sdev->ops = ops; 536672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 537672cf6dfSJoerg Roedel 538672cf6dfSJoerg Roedel if (!svm) { 539672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 540672cf6dfSJoerg Roedel if (!svm) { 541672cf6dfSJoerg Roedel ret = -ENOMEM; 542672cf6dfSJoerg Roedel kfree(sdev); 543672cf6dfSJoerg Roedel goto out; 544672cf6dfSJoerg Roedel } 545672cf6dfSJoerg Roedel svm->iommu = iommu; 546672cf6dfSJoerg Roedel 547672cf6dfSJoerg Roedel if (pasid_max > intel_pasid_max_id) 548672cf6dfSJoerg Roedel pasid_max = intel_pasid_max_id; 549672cf6dfSJoerg Roedel 550672cf6dfSJoerg Roedel /* Do not use PASID 0, reserved for RID to PASID */ 551672cf6dfSJoerg Roedel svm->pasid = ioasid_alloc(NULL, PASID_MIN, 552672cf6dfSJoerg Roedel pasid_max - 1, svm); 553672cf6dfSJoerg Roedel if (svm->pasid == INVALID_IOASID) { 554672cf6dfSJoerg Roedel kfree(svm); 555672cf6dfSJoerg Roedel kfree(sdev); 556672cf6dfSJoerg Roedel ret = -ENOSPC; 557672cf6dfSJoerg Roedel goto out; 558672cf6dfSJoerg Roedel } 559672cf6dfSJoerg Roedel svm->notifier.ops = &intel_mmuops; 560672cf6dfSJoerg Roedel svm->mm = mm; 561672cf6dfSJoerg Roedel svm->flags = flags; 562672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 563672cf6dfSJoerg Roedel INIT_LIST_HEAD(&svm->list); 564672cf6dfSJoerg Roedel ret = -ENOMEM; 565672cf6dfSJoerg Roedel if (mm) { 566672cf6dfSJoerg Roedel ret = mmu_notifier_register(&svm->notifier, mm); 567672cf6dfSJoerg Roedel if (ret) { 568672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 569672cf6dfSJoerg Roedel kfree(svm); 570672cf6dfSJoerg Roedel kfree(sdev); 571672cf6dfSJoerg Roedel goto out; 572672cf6dfSJoerg Roedel } 573672cf6dfSJoerg Roedel } 574672cf6dfSJoerg Roedel 575672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 576672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 577672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 578672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 579672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 580672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 581672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 582672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 583672cf6dfSJoerg Roedel if (ret) { 584672cf6dfSJoerg Roedel if (mm) 585672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, mm); 586672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 587672cf6dfSJoerg Roedel kfree(svm); 588672cf6dfSJoerg Roedel kfree(sdev); 589672cf6dfSJoerg Roedel goto out; 590672cf6dfSJoerg Roedel } 591672cf6dfSJoerg Roedel 592672cf6dfSJoerg Roedel list_add_tail(&svm->list, &global_svm_list); 593672cf6dfSJoerg Roedel } else { 594672cf6dfSJoerg Roedel /* 595672cf6dfSJoerg Roedel * Binding a new device with existing PASID, need to setup 596672cf6dfSJoerg Roedel * the PASID entry. 597672cf6dfSJoerg Roedel */ 598672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 599672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 600672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 601672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 602672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 603672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 604672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 605672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 606672cf6dfSJoerg Roedel if (ret) { 607672cf6dfSJoerg Roedel kfree(sdev); 608672cf6dfSJoerg Roedel goto out; 609672cf6dfSJoerg Roedel } 610672cf6dfSJoerg Roedel } 611672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 612672cf6dfSJoerg Roedel success: 613672cf6dfSJoerg Roedel sdev->pasid = svm->pasid; 614672cf6dfSJoerg Roedel sdev->sva.dev = dev; 615672cf6dfSJoerg Roedel if (sd) 616672cf6dfSJoerg Roedel *sd = sdev; 617672cf6dfSJoerg Roedel ret = 0; 618672cf6dfSJoerg Roedel out: 619672cf6dfSJoerg Roedel return ret; 620672cf6dfSJoerg Roedel } 621672cf6dfSJoerg Roedel 622672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex */ 623*c7b6bac9SFenghua Yu static int intel_svm_unbind_mm(struct device *dev, u32 pasid) 624672cf6dfSJoerg Roedel { 625672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 626672cf6dfSJoerg Roedel struct intel_iommu *iommu; 627672cf6dfSJoerg Roedel struct intel_svm *svm; 628672cf6dfSJoerg Roedel int ret = -EINVAL; 629672cf6dfSJoerg Roedel 630dd6692f1SLu Baolu iommu = device_to_iommu(dev, NULL, NULL); 631672cf6dfSJoerg Roedel if (!iommu) 632672cf6dfSJoerg Roedel goto out; 633672cf6dfSJoerg Roedel 63419abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 63519abcf70SLu Baolu if (ret) 636672cf6dfSJoerg Roedel goto out; 637672cf6dfSJoerg Roedel 63819abcf70SLu Baolu if (sdev) { 639672cf6dfSJoerg Roedel sdev->users--; 640672cf6dfSJoerg Roedel if (!sdev->users) { 641672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 642672cf6dfSJoerg Roedel /* Flush the PASID cache and IOTLB for this device. 643672cf6dfSJoerg Roedel * Note that we do depend on the hardware *not* using 644672cf6dfSJoerg Roedel * the PASID any more. Just as we depend on other 645672cf6dfSJoerg Roedel * devices never using PASIDs that they have no right 646672cf6dfSJoerg Roedel * to use. We have a *shared* PASID table, because it's 647672cf6dfSJoerg Roedel * large and has to be physically contiguous. So it's 648672cf6dfSJoerg Roedel * hard to be as defensive as we might like. */ 649672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 650672cf6dfSJoerg Roedel svm->pasid, false); 651672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 652672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 653672cf6dfSJoerg Roedel 654672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 655672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 656672cf6dfSJoerg Roedel if (svm->mm) 657672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, svm->mm); 658672cf6dfSJoerg Roedel list_del(&svm->list); 659672cf6dfSJoerg Roedel /* We mandate that no page faults may be outstanding 660672cf6dfSJoerg Roedel * for the PASID when intel_svm_unbind_mm() is called. 661672cf6dfSJoerg Roedel * If that is not obeyed, subtle errors will happen. 662672cf6dfSJoerg Roedel * Let's make them less subtle... */ 663672cf6dfSJoerg Roedel memset(svm, 0x6b, sizeof(*svm)); 664672cf6dfSJoerg Roedel kfree(svm); 665672cf6dfSJoerg Roedel } 666672cf6dfSJoerg Roedel } 667672cf6dfSJoerg Roedel } 668672cf6dfSJoerg Roedel out: 669672cf6dfSJoerg Roedel return ret; 670672cf6dfSJoerg Roedel } 671672cf6dfSJoerg Roedel 672672cf6dfSJoerg Roedel /* Page request queue descriptor */ 673672cf6dfSJoerg Roedel struct page_req_dsc { 674672cf6dfSJoerg Roedel union { 675672cf6dfSJoerg Roedel struct { 676672cf6dfSJoerg Roedel u64 type:8; 677672cf6dfSJoerg Roedel u64 pasid_present:1; 678672cf6dfSJoerg Roedel u64 priv_data_present:1; 679672cf6dfSJoerg Roedel u64 rsvd:6; 680672cf6dfSJoerg Roedel u64 rid:16; 681672cf6dfSJoerg Roedel u64 pasid:20; 682672cf6dfSJoerg Roedel u64 exe_req:1; 683672cf6dfSJoerg Roedel u64 pm_req:1; 684672cf6dfSJoerg Roedel u64 rsvd2:10; 685672cf6dfSJoerg Roedel }; 686672cf6dfSJoerg Roedel u64 qw_0; 687672cf6dfSJoerg Roedel }; 688672cf6dfSJoerg Roedel union { 689672cf6dfSJoerg Roedel struct { 690672cf6dfSJoerg Roedel u64 rd_req:1; 691672cf6dfSJoerg Roedel u64 wr_req:1; 692672cf6dfSJoerg Roedel u64 lpig:1; 693672cf6dfSJoerg Roedel u64 prg_index:9; 694672cf6dfSJoerg Roedel u64 addr:52; 695672cf6dfSJoerg Roedel }; 696672cf6dfSJoerg Roedel u64 qw_1; 697672cf6dfSJoerg Roedel }; 698672cf6dfSJoerg Roedel u64 priv_data[2]; 699672cf6dfSJoerg Roedel }; 700672cf6dfSJoerg Roedel 701672cf6dfSJoerg Roedel #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20) 702672cf6dfSJoerg Roedel 703672cf6dfSJoerg Roedel static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req) 704672cf6dfSJoerg Roedel { 705672cf6dfSJoerg Roedel unsigned long requested = 0; 706672cf6dfSJoerg Roedel 707672cf6dfSJoerg Roedel if (req->exe_req) 708672cf6dfSJoerg Roedel requested |= VM_EXEC; 709672cf6dfSJoerg Roedel 710672cf6dfSJoerg Roedel if (req->rd_req) 711672cf6dfSJoerg Roedel requested |= VM_READ; 712672cf6dfSJoerg Roedel 713672cf6dfSJoerg Roedel if (req->wr_req) 714672cf6dfSJoerg Roedel requested |= VM_WRITE; 715672cf6dfSJoerg Roedel 716672cf6dfSJoerg Roedel return (requested & ~vma->vm_flags) != 0; 717672cf6dfSJoerg Roedel } 718672cf6dfSJoerg Roedel 719672cf6dfSJoerg Roedel static bool is_canonical_address(u64 addr) 720672cf6dfSJoerg Roedel { 721672cf6dfSJoerg Roedel int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); 722672cf6dfSJoerg Roedel long saddr = (long) addr; 723672cf6dfSJoerg Roedel 724672cf6dfSJoerg Roedel return (((saddr << shift) >> shift) == saddr); 725672cf6dfSJoerg Roedel } 726672cf6dfSJoerg Roedel 727672cf6dfSJoerg Roedel /** 728672cf6dfSJoerg Roedel * intel_svm_drain_prq - Drain page requests and responses for a pasid 729672cf6dfSJoerg Roedel * @dev: target device 730672cf6dfSJoerg Roedel * @pasid: pasid for draining 731672cf6dfSJoerg Roedel * 732672cf6dfSJoerg Roedel * Drain all pending page requests and responses related to @pasid in both 733672cf6dfSJoerg Roedel * software and hardware. This is supposed to be called after the device 734672cf6dfSJoerg Roedel * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB 735672cf6dfSJoerg Roedel * and DevTLB have been invalidated. 736672cf6dfSJoerg Roedel * 737672cf6dfSJoerg Roedel * It waits until all pending page requests for @pasid in the page fault 738672cf6dfSJoerg Roedel * queue are completed by the prq handling thread. Then follow the steps 739672cf6dfSJoerg Roedel * described in VT-d spec CH7.10 to drain all page requests and page 740672cf6dfSJoerg Roedel * responses pending in the hardware. 741672cf6dfSJoerg Roedel */ 742*c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid) 743672cf6dfSJoerg Roedel { 744672cf6dfSJoerg Roedel struct device_domain_info *info; 745672cf6dfSJoerg Roedel struct dmar_domain *domain; 746672cf6dfSJoerg Roedel struct intel_iommu *iommu; 747672cf6dfSJoerg Roedel struct qi_desc desc[3]; 748672cf6dfSJoerg Roedel struct pci_dev *pdev; 749672cf6dfSJoerg Roedel int head, tail; 750672cf6dfSJoerg Roedel u16 sid, did; 751672cf6dfSJoerg Roedel int qdep; 752672cf6dfSJoerg Roedel 753672cf6dfSJoerg Roedel info = get_domain_info(dev); 754672cf6dfSJoerg Roedel if (WARN_ON(!info || !dev_is_pci(dev))) 755672cf6dfSJoerg Roedel return; 756672cf6dfSJoerg Roedel 757672cf6dfSJoerg Roedel if (!info->pri_enabled) 758672cf6dfSJoerg Roedel return; 759672cf6dfSJoerg Roedel 760672cf6dfSJoerg Roedel iommu = info->iommu; 761672cf6dfSJoerg Roedel domain = info->domain; 762672cf6dfSJoerg Roedel pdev = to_pci_dev(dev); 763672cf6dfSJoerg Roedel sid = PCI_DEVID(info->bus, info->devfn); 764672cf6dfSJoerg Roedel did = domain->iommu_did[iommu->seq_id]; 765672cf6dfSJoerg Roedel qdep = pci_ats_queue_depth(pdev); 766672cf6dfSJoerg Roedel 767672cf6dfSJoerg Roedel /* 768672cf6dfSJoerg Roedel * Check and wait until all pending page requests in the queue are 769672cf6dfSJoerg Roedel * handled by the prq handling thread. 770672cf6dfSJoerg Roedel */ 771672cf6dfSJoerg Roedel prq_retry: 772672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 773672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 774672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 775672cf6dfSJoerg Roedel while (head != tail) { 776672cf6dfSJoerg Roedel struct page_req_dsc *req; 777672cf6dfSJoerg Roedel 778672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 779672cf6dfSJoerg Roedel if (!req->pasid_present || req->pasid != pasid) { 780672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 781672cf6dfSJoerg Roedel continue; 782672cf6dfSJoerg Roedel } 783672cf6dfSJoerg Roedel 784672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 785672cf6dfSJoerg Roedel goto prq_retry; 786672cf6dfSJoerg Roedel } 787672cf6dfSJoerg Roedel 788672cf6dfSJoerg Roedel /* 789672cf6dfSJoerg Roedel * Perform steps described in VT-d spec CH7.10 to drain page 790672cf6dfSJoerg Roedel * requests and responses in hardware. 791672cf6dfSJoerg Roedel */ 792672cf6dfSJoerg Roedel memset(desc, 0, sizeof(desc)); 793672cf6dfSJoerg Roedel desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) | 794672cf6dfSJoerg Roedel QI_IWD_FENCE | 795672cf6dfSJoerg Roedel QI_IWD_TYPE; 796672cf6dfSJoerg Roedel desc[1].qw0 = QI_EIOTLB_PASID(pasid) | 797672cf6dfSJoerg Roedel QI_EIOTLB_DID(did) | 798672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | 799672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 800672cf6dfSJoerg Roedel desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) | 801672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SID(sid) | 802672cf6dfSJoerg Roedel QI_DEV_EIOTLB_QDEP(qdep) | 803672cf6dfSJoerg Roedel QI_DEIOTLB_TYPE | 804672cf6dfSJoerg Roedel QI_DEV_IOTLB_PFSID(info->pfsid); 805672cf6dfSJoerg Roedel qi_retry: 806672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 807672cf6dfSJoerg Roedel qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN); 808672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) { 809672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 810672cf6dfSJoerg Roedel goto qi_retry; 811672cf6dfSJoerg Roedel } 812672cf6dfSJoerg Roedel } 813672cf6dfSJoerg Roedel 814eb8d93eaSLu Baolu static int prq_to_iommu_prot(struct page_req_dsc *req) 815eb8d93eaSLu Baolu { 816eb8d93eaSLu Baolu int prot = 0; 817eb8d93eaSLu Baolu 818eb8d93eaSLu Baolu if (req->rd_req) 819eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_READ; 820eb8d93eaSLu Baolu if (req->wr_req) 821eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_WRITE; 822eb8d93eaSLu Baolu if (req->exe_req) 823eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_EXEC; 824eb8d93eaSLu Baolu if (req->pm_req) 825eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_PRIV; 826eb8d93eaSLu Baolu 827eb8d93eaSLu Baolu return prot; 828eb8d93eaSLu Baolu } 829eb8d93eaSLu Baolu 830eb8d93eaSLu Baolu static int 831eb8d93eaSLu Baolu intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc) 832eb8d93eaSLu Baolu { 833eb8d93eaSLu Baolu struct iommu_fault_event event; 834eb8d93eaSLu Baolu 835eb8d93eaSLu Baolu if (!dev || !dev_is_pci(dev)) 836eb8d93eaSLu Baolu return -ENODEV; 837eb8d93eaSLu Baolu 838eb8d93eaSLu Baolu /* Fill in event data for device specific processing */ 839eb8d93eaSLu Baolu memset(&event, 0, sizeof(struct iommu_fault_event)); 840eb8d93eaSLu Baolu event.fault.type = IOMMU_FAULT_PAGE_REQ; 841eb8d93eaSLu Baolu event.fault.prm.addr = desc->addr; 842eb8d93eaSLu Baolu event.fault.prm.pasid = desc->pasid; 843eb8d93eaSLu Baolu event.fault.prm.grpid = desc->prg_index; 844eb8d93eaSLu Baolu event.fault.prm.perm = prq_to_iommu_prot(desc); 845eb8d93eaSLu Baolu 846eb8d93eaSLu Baolu if (desc->lpig) 847eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 848eb8d93eaSLu Baolu if (desc->pasid_present) { 849eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 850eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID; 851eb8d93eaSLu Baolu } 852eb8d93eaSLu Baolu if (desc->priv_data_present) { 853eb8d93eaSLu Baolu /* 854eb8d93eaSLu Baolu * Set last page in group bit if private data is present, 855eb8d93eaSLu Baolu * page response is required as it does for LPIG. 856eb8d93eaSLu Baolu * iommu_report_device_fault() doesn't understand this vendor 857eb8d93eaSLu Baolu * specific requirement thus we set last_page as a workaround. 858eb8d93eaSLu Baolu */ 859eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 860eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 861eb8d93eaSLu Baolu memcpy(event.fault.prm.private_data, desc->priv_data, 862eb8d93eaSLu Baolu sizeof(desc->priv_data)); 863eb8d93eaSLu Baolu } 864eb8d93eaSLu Baolu 865eb8d93eaSLu Baolu return iommu_report_device_fault(dev, &event); 866eb8d93eaSLu Baolu } 867eb8d93eaSLu Baolu 868672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d) 869672cf6dfSJoerg Roedel { 870eb8d93eaSLu Baolu struct intel_svm_dev *sdev = NULL; 871672cf6dfSJoerg Roedel struct intel_iommu *iommu = d; 872672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 873672cf6dfSJoerg Roedel int head, tail, handled = 0; 874672cf6dfSJoerg Roedel 875672cf6dfSJoerg Roedel /* Clear PPR bit before reading head/tail registers, to 876672cf6dfSJoerg Roedel * ensure that we get a new interrupt if needed. */ 877672cf6dfSJoerg Roedel writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG); 878672cf6dfSJoerg Roedel 879672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 880672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 881672cf6dfSJoerg Roedel while (head != tail) { 882672cf6dfSJoerg Roedel struct vm_area_struct *vma; 883672cf6dfSJoerg Roedel struct page_req_dsc *req; 884672cf6dfSJoerg Roedel struct qi_desc resp; 885672cf6dfSJoerg Roedel int result; 886672cf6dfSJoerg Roedel vm_fault_t ret; 887672cf6dfSJoerg Roedel u64 address; 888672cf6dfSJoerg Roedel 889672cf6dfSJoerg Roedel handled = 1; 890672cf6dfSJoerg Roedel 891672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 892672cf6dfSJoerg Roedel 893672cf6dfSJoerg Roedel result = QI_RESP_FAILURE; 894672cf6dfSJoerg Roedel address = (u64)req->addr << VTD_PAGE_SHIFT; 895672cf6dfSJoerg Roedel if (!req->pasid_present) { 896672cf6dfSJoerg Roedel pr_err("%s: Page request without PASID: %08llx %08llx\n", 897672cf6dfSJoerg Roedel iommu->name, ((unsigned long long *)req)[0], 898672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 899672cf6dfSJoerg Roedel goto no_pasid; 900672cf6dfSJoerg Roedel } 901672cf6dfSJoerg Roedel 902672cf6dfSJoerg Roedel if (!svm || svm->pasid != req->pasid) { 903672cf6dfSJoerg Roedel rcu_read_lock(); 904672cf6dfSJoerg Roedel svm = ioasid_find(NULL, req->pasid, NULL); 905672cf6dfSJoerg Roedel /* It *can't* go away, because the driver is not permitted 906672cf6dfSJoerg Roedel * to unbind the mm while any page faults are outstanding. 907672cf6dfSJoerg Roedel * So we only need RCU to protect the internal idr code. */ 908672cf6dfSJoerg Roedel rcu_read_unlock(); 909672cf6dfSJoerg Roedel if (IS_ERR_OR_NULL(svm)) { 910672cf6dfSJoerg Roedel pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n", 911672cf6dfSJoerg Roedel iommu->name, req->pasid, ((unsigned long long *)req)[0], 912672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 913672cf6dfSJoerg Roedel goto no_pasid; 914672cf6dfSJoerg Roedel } 915672cf6dfSJoerg Roedel } 916672cf6dfSJoerg Roedel 917eb8d93eaSLu Baolu if (!sdev || sdev->sid != req->rid) { 918eb8d93eaSLu Baolu struct intel_svm_dev *t; 919eb8d93eaSLu Baolu 920eb8d93eaSLu Baolu sdev = NULL; 921eb8d93eaSLu Baolu rcu_read_lock(); 922eb8d93eaSLu Baolu list_for_each_entry_rcu(t, &svm->devs, list) { 923eb8d93eaSLu Baolu if (t->sid == req->rid) { 924eb8d93eaSLu Baolu sdev = t; 925eb8d93eaSLu Baolu break; 926eb8d93eaSLu Baolu } 927eb8d93eaSLu Baolu } 928eb8d93eaSLu Baolu rcu_read_unlock(); 929eb8d93eaSLu Baolu } 930eb8d93eaSLu Baolu 931672cf6dfSJoerg Roedel result = QI_RESP_INVALID; 932672cf6dfSJoerg Roedel /* Since we're using init_mm.pgd directly, we should never take 933672cf6dfSJoerg Roedel * any faults on kernel addresses. */ 934672cf6dfSJoerg Roedel if (!svm->mm) 935672cf6dfSJoerg Roedel goto bad_req; 936672cf6dfSJoerg Roedel 937672cf6dfSJoerg Roedel /* If address is not canonical, return invalid response */ 938672cf6dfSJoerg Roedel if (!is_canonical_address(address)) 939672cf6dfSJoerg Roedel goto bad_req; 940672cf6dfSJoerg Roedel 941eb8d93eaSLu Baolu /* 942eb8d93eaSLu Baolu * If prq is to be handled outside iommu driver via receiver of 943eb8d93eaSLu Baolu * the fault notifiers, we skip the page response here. 944eb8d93eaSLu Baolu */ 945eb8d93eaSLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 946eb8d93eaSLu Baolu if (sdev && !intel_svm_prq_report(sdev->dev, req)) 947eb8d93eaSLu Baolu goto prq_advance; 948eb8d93eaSLu Baolu else 949eb8d93eaSLu Baolu goto bad_req; 950eb8d93eaSLu Baolu } 951eb8d93eaSLu Baolu 952672cf6dfSJoerg Roedel /* If the mm is already defunct, don't handle faults. */ 953672cf6dfSJoerg Roedel if (!mmget_not_zero(svm->mm)) 954672cf6dfSJoerg Roedel goto bad_req; 955672cf6dfSJoerg Roedel 9568f02f363SLinus Torvalds mmap_read_lock(svm->mm); 957672cf6dfSJoerg Roedel vma = find_extend_vma(svm->mm, address); 958672cf6dfSJoerg Roedel if (!vma || address < vma->vm_start) 959672cf6dfSJoerg Roedel goto invalid; 960672cf6dfSJoerg Roedel 961672cf6dfSJoerg Roedel if (access_error(vma, req)) 962672cf6dfSJoerg Roedel goto invalid; 963672cf6dfSJoerg Roedel 964672cf6dfSJoerg Roedel ret = handle_mm_fault(vma, address, 965bce617edSPeter Xu req->wr_req ? FAULT_FLAG_WRITE : 0, 966bce617edSPeter Xu NULL); 967672cf6dfSJoerg Roedel if (ret & VM_FAULT_ERROR) 968672cf6dfSJoerg Roedel goto invalid; 969672cf6dfSJoerg Roedel 970672cf6dfSJoerg Roedel result = QI_RESP_SUCCESS; 971672cf6dfSJoerg Roedel invalid: 9728f02f363SLinus Torvalds mmap_read_unlock(svm->mm); 973672cf6dfSJoerg Roedel mmput(svm->mm); 974672cf6dfSJoerg Roedel bad_req: 975eb8d93eaSLu Baolu WARN_ON(!sdev); 976672cf6dfSJoerg Roedel if (sdev && sdev->ops && sdev->ops->fault_cb) { 977672cf6dfSJoerg Roedel int rwxp = (req->rd_req << 3) | (req->wr_req << 2) | 978672cf6dfSJoerg Roedel (req->exe_req << 1) | (req->pm_req); 979672cf6dfSJoerg Roedel sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, 980672cf6dfSJoerg Roedel req->priv_data, rwxp, result); 981672cf6dfSJoerg Roedel } 982672cf6dfSJoerg Roedel /* We get here in the error case where the PASID lookup failed, 983672cf6dfSJoerg Roedel and these can be NULL. Do not use them below this point! */ 984672cf6dfSJoerg Roedel sdev = NULL; 985672cf6dfSJoerg Roedel svm = NULL; 986672cf6dfSJoerg Roedel no_pasid: 987672cf6dfSJoerg Roedel if (req->lpig || req->priv_data_present) { 988672cf6dfSJoerg Roedel /* 989672cf6dfSJoerg Roedel * Per VT-d spec. v3.0 ch7.7, system software must 990672cf6dfSJoerg Roedel * respond with page group response if private data 991672cf6dfSJoerg Roedel * is present (PDP) or last page in group (LPIG) bit 992672cf6dfSJoerg Roedel * is set. This is an additional VT-d feature beyond 993672cf6dfSJoerg Roedel * PCI ATS spec. 994672cf6dfSJoerg Roedel */ 995672cf6dfSJoerg Roedel resp.qw0 = QI_PGRP_PASID(req->pasid) | 996672cf6dfSJoerg Roedel QI_PGRP_DID(req->rid) | 997672cf6dfSJoerg Roedel QI_PGRP_PASID_P(req->pasid_present) | 998672cf6dfSJoerg Roedel QI_PGRP_PDP(req->pasid_present) | 999672cf6dfSJoerg Roedel QI_PGRP_RESP_CODE(result) | 1000672cf6dfSJoerg Roedel QI_PGRP_RESP_TYPE; 1001672cf6dfSJoerg Roedel resp.qw1 = QI_PGRP_IDX(req->prg_index) | 1002672cf6dfSJoerg Roedel QI_PGRP_LPIG(req->lpig); 1003672cf6dfSJoerg Roedel 1004672cf6dfSJoerg Roedel if (req->priv_data_present) 1005672cf6dfSJoerg Roedel memcpy(&resp.qw2, req->priv_data, 1006672cf6dfSJoerg Roedel sizeof(req->priv_data)); 1007672cf6dfSJoerg Roedel resp.qw2 = 0; 1008672cf6dfSJoerg Roedel resp.qw3 = 0; 1009672cf6dfSJoerg Roedel qi_submit_sync(iommu, &resp, 1, 0); 1010672cf6dfSJoerg Roedel } 1011eb8d93eaSLu Baolu prq_advance: 1012672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 1013672cf6dfSJoerg Roedel } 1014672cf6dfSJoerg Roedel 1015672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, tail); 1016672cf6dfSJoerg Roedel 1017672cf6dfSJoerg Roedel /* 1018672cf6dfSJoerg Roedel * Clear the page request overflow bit and wake up all threads that 1019672cf6dfSJoerg Roedel * are waiting for the completion of this handling. 1020672cf6dfSJoerg Roedel */ 1021672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) 1022672cf6dfSJoerg Roedel writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG); 1023672cf6dfSJoerg Roedel 1024672cf6dfSJoerg Roedel if (!completion_done(&iommu->prq_complete)) 1025672cf6dfSJoerg Roedel complete(&iommu->prq_complete); 1026672cf6dfSJoerg Roedel 1027672cf6dfSJoerg Roedel return IRQ_RETVAL(handled); 1028672cf6dfSJoerg Roedel } 1029672cf6dfSJoerg Roedel 1030672cf6dfSJoerg Roedel #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva) 1031672cf6dfSJoerg Roedel struct iommu_sva * 1032672cf6dfSJoerg Roedel intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata) 1033672cf6dfSJoerg Roedel { 1034672cf6dfSJoerg Roedel struct iommu_sva *sva = ERR_PTR(-EINVAL); 1035672cf6dfSJoerg Roedel struct intel_svm_dev *sdev = NULL; 1036672cf6dfSJoerg Roedel int flags = 0; 1037672cf6dfSJoerg Roedel int ret; 1038672cf6dfSJoerg Roedel 1039672cf6dfSJoerg Roedel /* 1040672cf6dfSJoerg Roedel * TODO: Consolidate with generic iommu-sva bind after it is merged. 1041672cf6dfSJoerg Roedel * It will require shared SVM data structures, i.e. combine io_mm 1042672cf6dfSJoerg Roedel * and intel_svm etc. 1043672cf6dfSJoerg Roedel */ 1044672cf6dfSJoerg Roedel if (drvdata) 1045672cf6dfSJoerg Roedel flags = *(int *)drvdata; 1046672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1047672cf6dfSJoerg Roedel ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev); 1048672cf6dfSJoerg Roedel if (ret) 1049672cf6dfSJoerg Roedel sva = ERR_PTR(ret); 1050672cf6dfSJoerg Roedel else if (sdev) 1051672cf6dfSJoerg Roedel sva = &sdev->sva; 1052672cf6dfSJoerg Roedel else 1053672cf6dfSJoerg Roedel WARN(!sdev, "SVM bind succeeded with no sdev!\n"); 1054672cf6dfSJoerg Roedel 1055672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1056672cf6dfSJoerg Roedel 1057672cf6dfSJoerg Roedel return sva; 1058672cf6dfSJoerg Roedel } 1059672cf6dfSJoerg Roedel 1060672cf6dfSJoerg Roedel void intel_svm_unbind(struct iommu_sva *sva) 1061672cf6dfSJoerg Roedel { 1062672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1063672cf6dfSJoerg Roedel 1064672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1065672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1066672cf6dfSJoerg Roedel intel_svm_unbind_mm(sdev->dev, sdev->pasid); 1067672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1068672cf6dfSJoerg Roedel } 1069672cf6dfSJoerg Roedel 1070*c7b6bac9SFenghua Yu u32 intel_svm_get_pasid(struct iommu_sva *sva) 1071672cf6dfSJoerg Roedel { 1072672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1073*c7b6bac9SFenghua Yu u32 pasid; 1074672cf6dfSJoerg Roedel 1075672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1076672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1077672cf6dfSJoerg Roedel pasid = sdev->pasid; 1078672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1079672cf6dfSJoerg Roedel 1080672cf6dfSJoerg Roedel return pasid; 1081672cf6dfSJoerg Roedel } 10828b737121SLu Baolu 10838b737121SLu Baolu int intel_svm_page_response(struct device *dev, 10848b737121SLu Baolu struct iommu_fault_event *evt, 10858b737121SLu Baolu struct iommu_page_response *msg) 10868b737121SLu Baolu { 10878b737121SLu Baolu struct iommu_fault_page_request *prm; 10888b737121SLu Baolu struct intel_svm_dev *sdev = NULL; 10898b737121SLu Baolu struct intel_svm *svm = NULL; 10908b737121SLu Baolu struct intel_iommu *iommu; 10918b737121SLu Baolu bool private_present; 10928b737121SLu Baolu bool pasid_present; 10938b737121SLu Baolu bool last_page; 10948b737121SLu Baolu u8 bus, devfn; 10958b737121SLu Baolu int ret = 0; 10968b737121SLu Baolu u16 sid; 10978b737121SLu Baolu 10988b737121SLu Baolu if (!dev || !dev_is_pci(dev)) 10998b737121SLu Baolu return -ENODEV; 11008b737121SLu Baolu 11018b737121SLu Baolu iommu = device_to_iommu(dev, &bus, &devfn); 11028b737121SLu Baolu if (!iommu) 11038b737121SLu Baolu return -ENODEV; 11048b737121SLu Baolu 11058b737121SLu Baolu if (!msg || !evt) 11068b737121SLu Baolu return -EINVAL; 11078b737121SLu Baolu 11088b737121SLu Baolu mutex_lock(&pasid_mutex); 11098b737121SLu Baolu 11108b737121SLu Baolu prm = &evt->fault.prm; 11118b737121SLu Baolu sid = PCI_DEVID(bus, devfn); 11128b737121SLu Baolu pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 11138b737121SLu Baolu private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 11148b737121SLu Baolu last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 11158b737121SLu Baolu 11168b737121SLu Baolu if (!pasid_present) { 11178b737121SLu Baolu ret = -EINVAL; 11188b737121SLu Baolu goto out; 11198b737121SLu Baolu } 11208b737121SLu Baolu 11218b737121SLu Baolu if (prm->pasid == 0 || prm->pasid >= PASID_MAX) { 11228b737121SLu Baolu ret = -EINVAL; 11238b737121SLu Baolu goto out; 11248b737121SLu Baolu } 11258b737121SLu Baolu 11268b737121SLu Baolu ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev); 11278b737121SLu Baolu if (ret || !sdev) { 11288b737121SLu Baolu ret = -ENODEV; 11298b737121SLu Baolu goto out; 11308b737121SLu Baolu } 11318b737121SLu Baolu 11328b737121SLu Baolu /* 11338b737121SLu Baolu * For responses from userspace, need to make sure that the 11348b737121SLu Baolu * pasid has been bound to its mm. 11358b737121SLu Baolu */ 11368b737121SLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 11378b737121SLu Baolu struct mm_struct *mm; 11388b737121SLu Baolu 11398b737121SLu Baolu mm = get_task_mm(current); 11408b737121SLu Baolu if (!mm) { 11418b737121SLu Baolu ret = -EINVAL; 11428b737121SLu Baolu goto out; 11438b737121SLu Baolu } 11448b737121SLu Baolu 11458b737121SLu Baolu if (mm != svm->mm) { 11468b737121SLu Baolu ret = -ENODEV; 11478b737121SLu Baolu mmput(mm); 11488b737121SLu Baolu goto out; 11498b737121SLu Baolu } 11508b737121SLu Baolu 11518b737121SLu Baolu mmput(mm); 11528b737121SLu Baolu } 11538b737121SLu Baolu 11548b737121SLu Baolu /* 11558b737121SLu Baolu * Per VT-d spec. v3.0 ch7.7, system software must respond 11568b737121SLu Baolu * with page group response if private data is present (PDP) 11578b737121SLu Baolu * or last page in group (LPIG) bit is set. This is an 11588b737121SLu Baolu * additional VT-d requirement beyond PCI ATS spec. 11598b737121SLu Baolu */ 11608b737121SLu Baolu if (last_page || private_present) { 11618b737121SLu Baolu struct qi_desc desc; 11628b737121SLu Baolu 11638b737121SLu Baolu desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) | 11648b737121SLu Baolu QI_PGRP_PASID_P(pasid_present) | 11658b737121SLu Baolu QI_PGRP_PDP(private_present) | 11668b737121SLu Baolu QI_PGRP_RESP_CODE(msg->code) | 11678b737121SLu Baolu QI_PGRP_RESP_TYPE; 11688b737121SLu Baolu desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page); 11698b737121SLu Baolu desc.qw2 = 0; 11708b737121SLu Baolu desc.qw3 = 0; 11718b737121SLu Baolu if (private_present) 11728b737121SLu Baolu memcpy(&desc.qw2, prm->private_data, 11738b737121SLu Baolu sizeof(prm->private_data)); 11748b737121SLu Baolu 11758b737121SLu Baolu qi_submit_sync(iommu, &desc, 1, 0); 11768b737121SLu Baolu } 11778b737121SLu Baolu out: 11788b737121SLu Baolu mutex_unlock(&pasid_mutex); 11798b737121SLu Baolu return ret; 11808b737121SLu Baolu } 1181