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); 26c7b6bac9SFenghua 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 402c7b6bac9SFenghua 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 449*2a5054c6SFenghua Yu intel_svm_bind_mm(struct device *dev, unsigned int flags, 450*2a5054c6SFenghua Yu struct svm_dev_ops *ops, 451672cf6dfSJoerg Roedel struct mm_struct *mm, struct intel_svm_dev **sd) 452672cf6dfSJoerg Roedel { 453dd6692f1SLu Baolu struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL); 454672cf6dfSJoerg Roedel struct device_domain_info *info; 455672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 456672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 457672cf6dfSJoerg Roedel int pasid_max; 458672cf6dfSJoerg Roedel int ret; 459672cf6dfSJoerg Roedel 460672cf6dfSJoerg Roedel if (!iommu || dmar_disabled) 461672cf6dfSJoerg Roedel return -EINVAL; 462672cf6dfSJoerg Roedel 463672cf6dfSJoerg Roedel if (!intel_svm_capable(iommu)) 464672cf6dfSJoerg Roedel return -ENOTSUPP; 465672cf6dfSJoerg Roedel 466672cf6dfSJoerg Roedel if (dev_is_pci(dev)) { 467672cf6dfSJoerg Roedel pasid_max = pci_max_pasids(to_pci_dev(dev)); 468672cf6dfSJoerg Roedel if (pasid_max < 0) 469672cf6dfSJoerg Roedel return -EINVAL; 470672cf6dfSJoerg Roedel } else 471672cf6dfSJoerg Roedel pasid_max = 1 << 20; 472672cf6dfSJoerg Roedel 473672cf6dfSJoerg Roedel /* Bind supervisor PASID shuld have mm = NULL */ 474672cf6dfSJoerg Roedel if (flags & SVM_FLAG_SUPERVISOR_MODE) { 475672cf6dfSJoerg Roedel if (!ecap_srs(iommu->ecap) || mm) { 476672cf6dfSJoerg Roedel pr_err("Supervisor PASID with user provided mm.\n"); 477672cf6dfSJoerg Roedel return -EINVAL; 478672cf6dfSJoerg Roedel } 479672cf6dfSJoerg Roedel } 480672cf6dfSJoerg Roedel 481672cf6dfSJoerg Roedel if (!(flags & SVM_FLAG_PRIVATE_PASID)) { 482672cf6dfSJoerg Roedel struct intel_svm *t; 483672cf6dfSJoerg Roedel 484672cf6dfSJoerg Roedel list_for_each_entry(t, &global_svm_list, list) { 485672cf6dfSJoerg Roedel if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID)) 486672cf6dfSJoerg Roedel continue; 487672cf6dfSJoerg Roedel 488672cf6dfSJoerg Roedel svm = t; 489672cf6dfSJoerg Roedel if (svm->pasid >= pasid_max) { 490672cf6dfSJoerg Roedel dev_warn(dev, 491672cf6dfSJoerg Roedel "Limited PASID width. Cannot use existing PASID %d\n", 492672cf6dfSJoerg Roedel svm->pasid); 493672cf6dfSJoerg Roedel ret = -ENOSPC; 494672cf6dfSJoerg Roedel goto out; 495672cf6dfSJoerg Roedel } 496672cf6dfSJoerg Roedel 497672cf6dfSJoerg Roedel /* Find the matching device in svm list */ 498672cf6dfSJoerg Roedel for_each_svm_dev(sdev, svm, dev) { 499672cf6dfSJoerg Roedel if (sdev->ops != ops) { 500672cf6dfSJoerg Roedel ret = -EBUSY; 501672cf6dfSJoerg Roedel goto out; 502672cf6dfSJoerg Roedel } 503672cf6dfSJoerg Roedel sdev->users++; 504672cf6dfSJoerg Roedel goto success; 505672cf6dfSJoerg Roedel } 506672cf6dfSJoerg Roedel 507672cf6dfSJoerg Roedel break; 508672cf6dfSJoerg Roedel } 509672cf6dfSJoerg Roedel } 510672cf6dfSJoerg Roedel 511672cf6dfSJoerg Roedel sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 512672cf6dfSJoerg Roedel if (!sdev) { 513672cf6dfSJoerg Roedel ret = -ENOMEM; 514672cf6dfSJoerg Roedel goto out; 515672cf6dfSJoerg Roedel } 516672cf6dfSJoerg Roedel sdev->dev = dev; 517672cf6dfSJoerg Roedel 518672cf6dfSJoerg Roedel ret = intel_iommu_enable_pasid(iommu, dev); 519672cf6dfSJoerg Roedel if (ret) { 520672cf6dfSJoerg Roedel kfree(sdev); 521672cf6dfSJoerg Roedel goto out; 522672cf6dfSJoerg Roedel } 523672cf6dfSJoerg Roedel 524672cf6dfSJoerg Roedel info = get_domain_info(dev); 525672cf6dfSJoerg Roedel sdev->did = FLPT_DEFAULT_DID; 526672cf6dfSJoerg Roedel sdev->sid = PCI_DEVID(info->bus, info->devfn); 527672cf6dfSJoerg Roedel if (info->ats_enabled) { 528672cf6dfSJoerg Roedel sdev->dev_iotlb = 1; 529672cf6dfSJoerg Roedel sdev->qdep = info->ats_qdep; 530672cf6dfSJoerg Roedel if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) 531672cf6dfSJoerg Roedel sdev->qdep = 0; 532672cf6dfSJoerg Roedel } 533672cf6dfSJoerg Roedel 534672cf6dfSJoerg Roedel /* Finish the setup now we know we're keeping it */ 535672cf6dfSJoerg Roedel sdev->users = 1; 536672cf6dfSJoerg Roedel sdev->ops = ops; 537672cf6dfSJoerg Roedel init_rcu_head(&sdev->rcu); 538672cf6dfSJoerg Roedel 539672cf6dfSJoerg Roedel if (!svm) { 540672cf6dfSJoerg Roedel svm = kzalloc(sizeof(*svm), GFP_KERNEL); 541672cf6dfSJoerg Roedel if (!svm) { 542672cf6dfSJoerg Roedel ret = -ENOMEM; 543672cf6dfSJoerg Roedel kfree(sdev); 544672cf6dfSJoerg Roedel goto out; 545672cf6dfSJoerg Roedel } 546672cf6dfSJoerg Roedel svm->iommu = iommu; 547672cf6dfSJoerg Roedel 548672cf6dfSJoerg Roedel if (pasid_max > intel_pasid_max_id) 549672cf6dfSJoerg Roedel pasid_max = intel_pasid_max_id; 550672cf6dfSJoerg Roedel 551672cf6dfSJoerg Roedel /* Do not use PASID 0, reserved for RID to PASID */ 552672cf6dfSJoerg Roedel svm->pasid = ioasid_alloc(NULL, PASID_MIN, 553672cf6dfSJoerg Roedel pasid_max - 1, svm); 554672cf6dfSJoerg Roedel if (svm->pasid == INVALID_IOASID) { 555672cf6dfSJoerg Roedel kfree(svm); 556672cf6dfSJoerg Roedel kfree(sdev); 557672cf6dfSJoerg Roedel ret = -ENOSPC; 558672cf6dfSJoerg Roedel goto out; 559672cf6dfSJoerg Roedel } 560672cf6dfSJoerg Roedel svm->notifier.ops = &intel_mmuops; 561672cf6dfSJoerg Roedel svm->mm = mm; 562672cf6dfSJoerg Roedel svm->flags = flags; 563672cf6dfSJoerg Roedel INIT_LIST_HEAD_RCU(&svm->devs); 564672cf6dfSJoerg Roedel INIT_LIST_HEAD(&svm->list); 565672cf6dfSJoerg Roedel ret = -ENOMEM; 566672cf6dfSJoerg Roedel if (mm) { 567672cf6dfSJoerg Roedel ret = mmu_notifier_register(&svm->notifier, mm); 568672cf6dfSJoerg Roedel if (ret) { 569672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 570672cf6dfSJoerg Roedel kfree(svm); 571672cf6dfSJoerg Roedel kfree(sdev); 572672cf6dfSJoerg Roedel goto out; 573672cf6dfSJoerg Roedel } 574672cf6dfSJoerg Roedel } 575672cf6dfSJoerg Roedel 576672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 577672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 578672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 579672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 580672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 581672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 582672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 583672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 584672cf6dfSJoerg Roedel if (ret) { 585672cf6dfSJoerg Roedel if (mm) 586672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, mm); 587672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 588672cf6dfSJoerg Roedel kfree(svm); 589672cf6dfSJoerg Roedel kfree(sdev); 590672cf6dfSJoerg Roedel goto out; 591672cf6dfSJoerg Roedel } 592672cf6dfSJoerg Roedel 593672cf6dfSJoerg Roedel list_add_tail(&svm->list, &global_svm_list); 594672cf6dfSJoerg Roedel } else { 595672cf6dfSJoerg Roedel /* 596672cf6dfSJoerg Roedel * Binding a new device with existing PASID, need to setup 597672cf6dfSJoerg Roedel * the PASID entry. 598672cf6dfSJoerg Roedel */ 599672cf6dfSJoerg Roedel spin_lock(&iommu->lock); 600672cf6dfSJoerg Roedel ret = intel_pasid_setup_first_level(iommu, dev, 601672cf6dfSJoerg Roedel mm ? mm->pgd : init_mm.pgd, 602672cf6dfSJoerg Roedel svm->pasid, FLPT_DEFAULT_DID, 603672cf6dfSJoerg Roedel (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | 604672cf6dfSJoerg Roedel (cpu_feature_enabled(X86_FEATURE_LA57) ? 605672cf6dfSJoerg Roedel PASID_FLAG_FL5LP : 0)); 606672cf6dfSJoerg Roedel spin_unlock(&iommu->lock); 607672cf6dfSJoerg Roedel if (ret) { 608672cf6dfSJoerg Roedel kfree(sdev); 609672cf6dfSJoerg Roedel goto out; 610672cf6dfSJoerg Roedel } 611672cf6dfSJoerg Roedel } 612672cf6dfSJoerg Roedel list_add_rcu(&sdev->list, &svm->devs); 613672cf6dfSJoerg Roedel success: 614672cf6dfSJoerg Roedel sdev->pasid = svm->pasid; 615672cf6dfSJoerg Roedel sdev->sva.dev = dev; 616672cf6dfSJoerg Roedel if (sd) 617672cf6dfSJoerg Roedel *sd = sdev; 618672cf6dfSJoerg Roedel ret = 0; 619672cf6dfSJoerg Roedel out: 620672cf6dfSJoerg Roedel return ret; 621672cf6dfSJoerg Roedel } 622672cf6dfSJoerg Roedel 623672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex */ 624c7b6bac9SFenghua Yu static int intel_svm_unbind_mm(struct device *dev, u32 pasid) 625672cf6dfSJoerg Roedel { 626672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 627672cf6dfSJoerg Roedel struct intel_iommu *iommu; 628672cf6dfSJoerg Roedel struct intel_svm *svm; 629672cf6dfSJoerg Roedel int ret = -EINVAL; 630672cf6dfSJoerg Roedel 631dd6692f1SLu Baolu iommu = device_to_iommu(dev, NULL, NULL); 632672cf6dfSJoerg Roedel if (!iommu) 633672cf6dfSJoerg Roedel goto out; 634672cf6dfSJoerg Roedel 63519abcf70SLu Baolu ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev); 63619abcf70SLu Baolu if (ret) 637672cf6dfSJoerg Roedel goto out; 638672cf6dfSJoerg Roedel 63919abcf70SLu Baolu if (sdev) { 640672cf6dfSJoerg Roedel sdev->users--; 641672cf6dfSJoerg Roedel if (!sdev->users) { 642672cf6dfSJoerg Roedel list_del_rcu(&sdev->list); 643672cf6dfSJoerg Roedel /* Flush the PASID cache and IOTLB for this device. 644672cf6dfSJoerg Roedel * Note that we do depend on the hardware *not* using 645672cf6dfSJoerg Roedel * the PASID any more. Just as we depend on other 646672cf6dfSJoerg Roedel * devices never using PASIDs that they have no right 647672cf6dfSJoerg Roedel * to use. We have a *shared* PASID table, because it's 648672cf6dfSJoerg Roedel * large and has to be physically contiguous. So it's 649672cf6dfSJoerg Roedel * hard to be as defensive as we might like. */ 650672cf6dfSJoerg Roedel intel_pasid_tear_down_entry(iommu, dev, 651672cf6dfSJoerg Roedel svm->pasid, false); 652672cf6dfSJoerg Roedel intel_svm_drain_prq(dev, svm->pasid); 653672cf6dfSJoerg Roedel kfree_rcu(sdev, rcu); 654672cf6dfSJoerg Roedel 655672cf6dfSJoerg Roedel if (list_empty(&svm->devs)) { 656672cf6dfSJoerg Roedel ioasid_free(svm->pasid); 657672cf6dfSJoerg Roedel if (svm->mm) 658672cf6dfSJoerg Roedel mmu_notifier_unregister(&svm->notifier, svm->mm); 659672cf6dfSJoerg Roedel list_del(&svm->list); 660672cf6dfSJoerg Roedel /* We mandate that no page faults may be outstanding 661672cf6dfSJoerg Roedel * for the PASID when intel_svm_unbind_mm() is called. 662672cf6dfSJoerg Roedel * If that is not obeyed, subtle errors will happen. 663672cf6dfSJoerg Roedel * Let's make them less subtle... */ 664672cf6dfSJoerg Roedel memset(svm, 0x6b, sizeof(*svm)); 665672cf6dfSJoerg Roedel kfree(svm); 666672cf6dfSJoerg Roedel } 667672cf6dfSJoerg Roedel } 668672cf6dfSJoerg Roedel } 669672cf6dfSJoerg Roedel out: 670672cf6dfSJoerg Roedel return ret; 671672cf6dfSJoerg Roedel } 672672cf6dfSJoerg Roedel 673672cf6dfSJoerg Roedel /* Page request queue descriptor */ 674672cf6dfSJoerg Roedel struct page_req_dsc { 675672cf6dfSJoerg Roedel union { 676672cf6dfSJoerg Roedel struct { 677672cf6dfSJoerg Roedel u64 type:8; 678672cf6dfSJoerg Roedel u64 pasid_present:1; 679672cf6dfSJoerg Roedel u64 priv_data_present:1; 680672cf6dfSJoerg Roedel u64 rsvd:6; 681672cf6dfSJoerg Roedel u64 rid:16; 682672cf6dfSJoerg Roedel u64 pasid:20; 683672cf6dfSJoerg Roedel u64 exe_req:1; 684672cf6dfSJoerg Roedel u64 pm_req:1; 685672cf6dfSJoerg Roedel u64 rsvd2:10; 686672cf6dfSJoerg Roedel }; 687672cf6dfSJoerg Roedel u64 qw_0; 688672cf6dfSJoerg Roedel }; 689672cf6dfSJoerg Roedel union { 690672cf6dfSJoerg Roedel struct { 691672cf6dfSJoerg Roedel u64 rd_req:1; 692672cf6dfSJoerg Roedel u64 wr_req:1; 693672cf6dfSJoerg Roedel u64 lpig:1; 694672cf6dfSJoerg Roedel u64 prg_index:9; 695672cf6dfSJoerg Roedel u64 addr:52; 696672cf6dfSJoerg Roedel }; 697672cf6dfSJoerg Roedel u64 qw_1; 698672cf6dfSJoerg Roedel }; 699672cf6dfSJoerg Roedel u64 priv_data[2]; 700672cf6dfSJoerg Roedel }; 701672cf6dfSJoerg Roedel 702672cf6dfSJoerg Roedel #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20) 703672cf6dfSJoerg Roedel 704672cf6dfSJoerg Roedel static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req) 705672cf6dfSJoerg Roedel { 706672cf6dfSJoerg Roedel unsigned long requested = 0; 707672cf6dfSJoerg Roedel 708672cf6dfSJoerg Roedel if (req->exe_req) 709672cf6dfSJoerg Roedel requested |= VM_EXEC; 710672cf6dfSJoerg Roedel 711672cf6dfSJoerg Roedel if (req->rd_req) 712672cf6dfSJoerg Roedel requested |= VM_READ; 713672cf6dfSJoerg Roedel 714672cf6dfSJoerg Roedel if (req->wr_req) 715672cf6dfSJoerg Roedel requested |= VM_WRITE; 716672cf6dfSJoerg Roedel 717672cf6dfSJoerg Roedel return (requested & ~vma->vm_flags) != 0; 718672cf6dfSJoerg Roedel } 719672cf6dfSJoerg Roedel 720672cf6dfSJoerg Roedel static bool is_canonical_address(u64 addr) 721672cf6dfSJoerg Roedel { 722672cf6dfSJoerg Roedel int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); 723672cf6dfSJoerg Roedel long saddr = (long) addr; 724672cf6dfSJoerg Roedel 725672cf6dfSJoerg Roedel return (((saddr << shift) >> shift) == saddr); 726672cf6dfSJoerg Roedel } 727672cf6dfSJoerg Roedel 728672cf6dfSJoerg Roedel /** 729672cf6dfSJoerg Roedel * intel_svm_drain_prq - Drain page requests and responses for a pasid 730672cf6dfSJoerg Roedel * @dev: target device 731672cf6dfSJoerg Roedel * @pasid: pasid for draining 732672cf6dfSJoerg Roedel * 733672cf6dfSJoerg Roedel * Drain all pending page requests and responses related to @pasid in both 734672cf6dfSJoerg Roedel * software and hardware. This is supposed to be called after the device 735672cf6dfSJoerg Roedel * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB 736672cf6dfSJoerg Roedel * and DevTLB have been invalidated. 737672cf6dfSJoerg Roedel * 738672cf6dfSJoerg Roedel * It waits until all pending page requests for @pasid in the page fault 739672cf6dfSJoerg Roedel * queue are completed by the prq handling thread. Then follow the steps 740672cf6dfSJoerg Roedel * described in VT-d spec CH7.10 to drain all page requests and page 741672cf6dfSJoerg Roedel * responses pending in the hardware. 742672cf6dfSJoerg Roedel */ 743c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid) 744672cf6dfSJoerg Roedel { 745672cf6dfSJoerg Roedel struct device_domain_info *info; 746672cf6dfSJoerg Roedel struct dmar_domain *domain; 747672cf6dfSJoerg Roedel struct intel_iommu *iommu; 748672cf6dfSJoerg Roedel struct qi_desc desc[3]; 749672cf6dfSJoerg Roedel struct pci_dev *pdev; 750672cf6dfSJoerg Roedel int head, tail; 751672cf6dfSJoerg Roedel u16 sid, did; 752672cf6dfSJoerg Roedel int qdep; 753672cf6dfSJoerg Roedel 754672cf6dfSJoerg Roedel info = get_domain_info(dev); 755672cf6dfSJoerg Roedel if (WARN_ON(!info || !dev_is_pci(dev))) 756672cf6dfSJoerg Roedel return; 757672cf6dfSJoerg Roedel 758672cf6dfSJoerg Roedel if (!info->pri_enabled) 759672cf6dfSJoerg Roedel return; 760672cf6dfSJoerg Roedel 761672cf6dfSJoerg Roedel iommu = info->iommu; 762672cf6dfSJoerg Roedel domain = info->domain; 763672cf6dfSJoerg Roedel pdev = to_pci_dev(dev); 764672cf6dfSJoerg Roedel sid = PCI_DEVID(info->bus, info->devfn); 765672cf6dfSJoerg Roedel did = domain->iommu_did[iommu->seq_id]; 766672cf6dfSJoerg Roedel qdep = pci_ats_queue_depth(pdev); 767672cf6dfSJoerg Roedel 768672cf6dfSJoerg Roedel /* 769672cf6dfSJoerg Roedel * Check and wait until all pending page requests in the queue are 770672cf6dfSJoerg Roedel * handled by the prq handling thread. 771672cf6dfSJoerg Roedel */ 772672cf6dfSJoerg Roedel prq_retry: 773672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 774672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 775672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 776672cf6dfSJoerg Roedel while (head != tail) { 777672cf6dfSJoerg Roedel struct page_req_dsc *req; 778672cf6dfSJoerg Roedel 779672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 780672cf6dfSJoerg Roedel if (!req->pasid_present || req->pasid != pasid) { 781672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 782672cf6dfSJoerg Roedel continue; 783672cf6dfSJoerg Roedel } 784672cf6dfSJoerg Roedel 785672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 786672cf6dfSJoerg Roedel goto prq_retry; 787672cf6dfSJoerg Roedel } 788672cf6dfSJoerg Roedel 789672cf6dfSJoerg Roedel /* 790672cf6dfSJoerg Roedel * Perform steps described in VT-d spec CH7.10 to drain page 791672cf6dfSJoerg Roedel * requests and responses in hardware. 792672cf6dfSJoerg Roedel */ 793672cf6dfSJoerg Roedel memset(desc, 0, sizeof(desc)); 794672cf6dfSJoerg Roedel desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) | 795672cf6dfSJoerg Roedel QI_IWD_FENCE | 796672cf6dfSJoerg Roedel QI_IWD_TYPE; 797672cf6dfSJoerg Roedel desc[1].qw0 = QI_EIOTLB_PASID(pasid) | 798672cf6dfSJoerg Roedel QI_EIOTLB_DID(did) | 799672cf6dfSJoerg Roedel QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | 800672cf6dfSJoerg Roedel QI_EIOTLB_TYPE; 801672cf6dfSJoerg Roedel desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) | 802672cf6dfSJoerg Roedel QI_DEV_EIOTLB_SID(sid) | 803672cf6dfSJoerg Roedel QI_DEV_EIOTLB_QDEP(qdep) | 804672cf6dfSJoerg Roedel QI_DEIOTLB_TYPE | 805672cf6dfSJoerg Roedel QI_DEV_IOTLB_PFSID(info->pfsid); 806672cf6dfSJoerg Roedel qi_retry: 807672cf6dfSJoerg Roedel reinit_completion(&iommu->prq_complete); 808672cf6dfSJoerg Roedel qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN); 809672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) { 810672cf6dfSJoerg Roedel wait_for_completion(&iommu->prq_complete); 811672cf6dfSJoerg Roedel goto qi_retry; 812672cf6dfSJoerg Roedel } 813672cf6dfSJoerg Roedel } 814672cf6dfSJoerg Roedel 815eb8d93eaSLu Baolu static int prq_to_iommu_prot(struct page_req_dsc *req) 816eb8d93eaSLu Baolu { 817eb8d93eaSLu Baolu int prot = 0; 818eb8d93eaSLu Baolu 819eb8d93eaSLu Baolu if (req->rd_req) 820eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_READ; 821eb8d93eaSLu Baolu if (req->wr_req) 822eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_WRITE; 823eb8d93eaSLu Baolu if (req->exe_req) 824eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_EXEC; 825eb8d93eaSLu Baolu if (req->pm_req) 826eb8d93eaSLu Baolu prot |= IOMMU_FAULT_PERM_PRIV; 827eb8d93eaSLu Baolu 828eb8d93eaSLu Baolu return prot; 829eb8d93eaSLu Baolu } 830eb8d93eaSLu Baolu 831eb8d93eaSLu Baolu static int 832eb8d93eaSLu Baolu intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc) 833eb8d93eaSLu Baolu { 834eb8d93eaSLu Baolu struct iommu_fault_event event; 835eb8d93eaSLu Baolu 836eb8d93eaSLu Baolu if (!dev || !dev_is_pci(dev)) 837eb8d93eaSLu Baolu return -ENODEV; 838eb8d93eaSLu Baolu 839eb8d93eaSLu Baolu /* Fill in event data for device specific processing */ 840eb8d93eaSLu Baolu memset(&event, 0, sizeof(struct iommu_fault_event)); 841eb8d93eaSLu Baolu event.fault.type = IOMMU_FAULT_PAGE_REQ; 842eb8d93eaSLu Baolu event.fault.prm.addr = desc->addr; 843eb8d93eaSLu Baolu event.fault.prm.pasid = desc->pasid; 844eb8d93eaSLu Baolu event.fault.prm.grpid = desc->prg_index; 845eb8d93eaSLu Baolu event.fault.prm.perm = prq_to_iommu_prot(desc); 846eb8d93eaSLu Baolu 847eb8d93eaSLu Baolu if (desc->lpig) 848eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 849eb8d93eaSLu Baolu if (desc->pasid_present) { 850eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 851eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID; 852eb8d93eaSLu Baolu } 853eb8d93eaSLu Baolu if (desc->priv_data_present) { 854eb8d93eaSLu Baolu /* 855eb8d93eaSLu Baolu * Set last page in group bit if private data is present, 856eb8d93eaSLu Baolu * page response is required as it does for LPIG. 857eb8d93eaSLu Baolu * iommu_report_device_fault() doesn't understand this vendor 858eb8d93eaSLu Baolu * specific requirement thus we set last_page as a workaround. 859eb8d93eaSLu Baolu */ 860eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 861eb8d93eaSLu Baolu event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 862eb8d93eaSLu Baolu memcpy(event.fault.prm.private_data, desc->priv_data, 863eb8d93eaSLu Baolu sizeof(desc->priv_data)); 864eb8d93eaSLu Baolu } 865eb8d93eaSLu Baolu 866eb8d93eaSLu Baolu return iommu_report_device_fault(dev, &event); 867eb8d93eaSLu Baolu } 868eb8d93eaSLu Baolu 869672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d) 870672cf6dfSJoerg Roedel { 871eb8d93eaSLu Baolu struct intel_svm_dev *sdev = NULL; 872672cf6dfSJoerg Roedel struct intel_iommu *iommu = d; 873672cf6dfSJoerg Roedel struct intel_svm *svm = NULL; 874672cf6dfSJoerg Roedel int head, tail, handled = 0; 875672cf6dfSJoerg Roedel 876672cf6dfSJoerg Roedel /* Clear PPR bit before reading head/tail registers, to 877672cf6dfSJoerg Roedel * ensure that we get a new interrupt if needed. */ 878672cf6dfSJoerg Roedel writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG); 879672cf6dfSJoerg Roedel 880672cf6dfSJoerg Roedel tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; 881672cf6dfSJoerg Roedel head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; 882672cf6dfSJoerg Roedel while (head != tail) { 883672cf6dfSJoerg Roedel struct vm_area_struct *vma; 884672cf6dfSJoerg Roedel struct page_req_dsc *req; 885672cf6dfSJoerg Roedel struct qi_desc resp; 886672cf6dfSJoerg Roedel int result; 887672cf6dfSJoerg Roedel vm_fault_t ret; 888672cf6dfSJoerg Roedel u64 address; 889672cf6dfSJoerg Roedel 890672cf6dfSJoerg Roedel handled = 1; 891672cf6dfSJoerg Roedel 892672cf6dfSJoerg Roedel req = &iommu->prq[head / sizeof(*req)]; 893672cf6dfSJoerg Roedel 894672cf6dfSJoerg Roedel result = QI_RESP_FAILURE; 895672cf6dfSJoerg Roedel address = (u64)req->addr << VTD_PAGE_SHIFT; 896672cf6dfSJoerg Roedel if (!req->pasid_present) { 897672cf6dfSJoerg Roedel pr_err("%s: Page request without PASID: %08llx %08llx\n", 898672cf6dfSJoerg Roedel iommu->name, ((unsigned long long *)req)[0], 899672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 900672cf6dfSJoerg Roedel goto no_pasid; 901672cf6dfSJoerg Roedel } 902672cf6dfSJoerg Roedel 903672cf6dfSJoerg Roedel if (!svm || svm->pasid != req->pasid) { 904672cf6dfSJoerg Roedel rcu_read_lock(); 905672cf6dfSJoerg Roedel svm = ioasid_find(NULL, req->pasid, NULL); 906672cf6dfSJoerg Roedel /* It *can't* go away, because the driver is not permitted 907672cf6dfSJoerg Roedel * to unbind the mm while any page faults are outstanding. 908672cf6dfSJoerg Roedel * So we only need RCU to protect the internal idr code. */ 909672cf6dfSJoerg Roedel rcu_read_unlock(); 910672cf6dfSJoerg Roedel if (IS_ERR_OR_NULL(svm)) { 911672cf6dfSJoerg Roedel pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n", 912672cf6dfSJoerg Roedel iommu->name, req->pasid, ((unsigned long long *)req)[0], 913672cf6dfSJoerg Roedel ((unsigned long long *)req)[1]); 914672cf6dfSJoerg Roedel goto no_pasid; 915672cf6dfSJoerg Roedel } 916672cf6dfSJoerg Roedel } 917672cf6dfSJoerg Roedel 918eb8d93eaSLu Baolu if (!sdev || sdev->sid != req->rid) { 919eb8d93eaSLu Baolu struct intel_svm_dev *t; 920eb8d93eaSLu Baolu 921eb8d93eaSLu Baolu sdev = NULL; 922eb8d93eaSLu Baolu rcu_read_lock(); 923eb8d93eaSLu Baolu list_for_each_entry_rcu(t, &svm->devs, list) { 924eb8d93eaSLu Baolu if (t->sid == req->rid) { 925eb8d93eaSLu Baolu sdev = t; 926eb8d93eaSLu Baolu break; 927eb8d93eaSLu Baolu } 928eb8d93eaSLu Baolu } 929eb8d93eaSLu Baolu rcu_read_unlock(); 930eb8d93eaSLu Baolu } 931eb8d93eaSLu Baolu 932672cf6dfSJoerg Roedel result = QI_RESP_INVALID; 933672cf6dfSJoerg Roedel /* Since we're using init_mm.pgd directly, we should never take 934672cf6dfSJoerg Roedel * any faults on kernel addresses. */ 935672cf6dfSJoerg Roedel if (!svm->mm) 936672cf6dfSJoerg Roedel goto bad_req; 937672cf6dfSJoerg Roedel 938672cf6dfSJoerg Roedel /* If address is not canonical, return invalid response */ 939672cf6dfSJoerg Roedel if (!is_canonical_address(address)) 940672cf6dfSJoerg Roedel goto bad_req; 941672cf6dfSJoerg Roedel 942eb8d93eaSLu Baolu /* 943eb8d93eaSLu Baolu * If prq is to be handled outside iommu driver via receiver of 944eb8d93eaSLu Baolu * the fault notifiers, we skip the page response here. 945eb8d93eaSLu Baolu */ 946eb8d93eaSLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 947eb8d93eaSLu Baolu if (sdev && !intel_svm_prq_report(sdev->dev, req)) 948eb8d93eaSLu Baolu goto prq_advance; 949eb8d93eaSLu Baolu else 950eb8d93eaSLu Baolu goto bad_req; 951eb8d93eaSLu Baolu } 952eb8d93eaSLu Baolu 953672cf6dfSJoerg Roedel /* If the mm is already defunct, don't handle faults. */ 954672cf6dfSJoerg Roedel if (!mmget_not_zero(svm->mm)) 955672cf6dfSJoerg Roedel goto bad_req; 956672cf6dfSJoerg Roedel 9578f02f363SLinus Torvalds mmap_read_lock(svm->mm); 958672cf6dfSJoerg Roedel vma = find_extend_vma(svm->mm, address); 959672cf6dfSJoerg Roedel if (!vma || address < vma->vm_start) 960672cf6dfSJoerg Roedel goto invalid; 961672cf6dfSJoerg Roedel 962672cf6dfSJoerg Roedel if (access_error(vma, req)) 963672cf6dfSJoerg Roedel goto invalid; 964672cf6dfSJoerg Roedel 965672cf6dfSJoerg Roedel ret = handle_mm_fault(vma, address, 966bce617edSPeter Xu req->wr_req ? FAULT_FLAG_WRITE : 0, 967bce617edSPeter Xu NULL); 968672cf6dfSJoerg Roedel if (ret & VM_FAULT_ERROR) 969672cf6dfSJoerg Roedel goto invalid; 970672cf6dfSJoerg Roedel 971672cf6dfSJoerg Roedel result = QI_RESP_SUCCESS; 972672cf6dfSJoerg Roedel invalid: 9738f02f363SLinus Torvalds mmap_read_unlock(svm->mm); 974672cf6dfSJoerg Roedel mmput(svm->mm); 975672cf6dfSJoerg Roedel bad_req: 976eb8d93eaSLu Baolu WARN_ON(!sdev); 977672cf6dfSJoerg Roedel if (sdev && sdev->ops && sdev->ops->fault_cb) { 978672cf6dfSJoerg Roedel int rwxp = (req->rd_req << 3) | (req->wr_req << 2) | 979672cf6dfSJoerg Roedel (req->exe_req << 1) | (req->pm_req); 980672cf6dfSJoerg Roedel sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, 981672cf6dfSJoerg Roedel req->priv_data, rwxp, result); 982672cf6dfSJoerg Roedel } 983672cf6dfSJoerg Roedel /* We get here in the error case where the PASID lookup failed, 984672cf6dfSJoerg Roedel and these can be NULL. Do not use them below this point! */ 985672cf6dfSJoerg Roedel sdev = NULL; 986672cf6dfSJoerg Roedel svm = NULL; 987672cf6dfSJoerg Roedel no_pasid: 988672cf6dfSJoerg Roedel if (req->lpig || req->priv_data_present) { 989672cf6dfSJoerg Roedel /* 990672cf6dfSJoerg Roedel * Per VT-d spec. v3.0 ch7.7, system software must 991672cf6dfSJoerg Roedel * respond with page group response if private data 992672cf6dfSJoerg Roedel * is present (PDP) or last page in group (LPIG) bit 993672cf6dfSJoerg Roedel * is set. This is an additional VT-d feature beyond 994672cf6dfSJoerg Roedel * PCI ATS spec. 995672cf6dfSJoerg Roedel */ 996672cf6dfSJoerg Roedel resp.qw0 = QI_PGRP_PASID(req->pasid) | 997672cf6dfSJoerg Roedel QI_PGRP_DID(req->rid) | 998672cf6dfSJoerg Roedel QI_PGRP_PASID_P(req->pasid_present) | 999672cf6dfSJoerg Roedel QI_PGRP_PDP(req->pasid_present) | 1000672cf6dfSJoerg Roedel QI_PGRP_RESP_CODE(result) | 1001672cf6dfSJoerg Roedel QI_PGRP_RESP_TYPE; 1002672cf6dfSJoerg Roedel resp.qw1 = QI_PGRP_IDX(req->prg_index) | 1003672cf6dfSJoerg Roedel QI_PGRP_LPIG(req->lpig); 1004672cf6dfSJoerg Roedel 1005672cf6dfSJoerg Roedel if (req->priv_data_present) 1006672cf6dfSJoerg Roedel memcpy(&resp.qw2, req->priv_data, 1007672cf6dfSJoerg Roedel sizeof(req->priv_data)); 1008672cf6dfSJoerg Roedel resp.qw2 = 0; 1009672cf6dfSJoerg Roedel resp.qw3 = 0; 1010672cf6dfSJoerg Roedel qi_submit_sync(iommu, &resp, 1, 0); 1011672cf6dfSJoerg Roedel } 1012eb8d93eaSLu Baolu prq_advance: 1013672cf6dfSJoerg Roedel head = (head + sizeof(*req)) & PRQ_RING_MASK; 1014672cf6dfSJoerg Roedel } 1015672cf6dfSJoerg Roedel 1016672cf6dfSJoerg Roedel dmar_writeq(iommu->reg + DMAR_PQH_REG, tail); 1017672cf6dfSJoerg Roedel 1018672cf6dfSJoerg Roedel /* 1019672cf6dfSJoerg Roedel * Clear the page request overflow bit and wake up all threads that 1020672cf6dfSJoerg Roedel * are waiting for the completion of this handling. 1021672cf6dfSJoerg Roedel */ 1022672cf6dfSJoerg Roedel if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) 1023672cf6dfSJoerg Roedel writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG); 1024672cf6dfSJoerg Roedel 1025672cf6dfSJoerg Roedel if (!completion_done(&iommu->prq_complete)) 1026672cf6dfSJoerg Roedel complete(&iommu->prq_complete); 1027672cf6dfSJoerg Roedel 1028672cf6dfSJoerg Roedel return IRQ_RETVAL(handled); 1029672cf6dfSJoerg Roedel } 1030672cf6dfSJoerg Roedel 1031672cf6dfSJoerg Roedel #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva) 1032672cf6dfSJoerg Roedel struct iommu_sva * 1033672cf6dfSJoerg Roedel intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata) 1034672cf6dfSJoerg Roedel { 1035672cf6dfSJoerg Roedel struct iommu_sva *sva = ERR_PTR(-EINVAL); 1036672cf6dfSJoerg Roedel struct intel_svm_dev *sdev = NULL; 1037*2a5054c6SFenghua Yu unsigned int flags = 0; 1038672cf6dfSJoerg Roedel int ret; 1039672cf6dfSJoerg Roedel 1040672cf6dfSJoerg Roedel /* 1041672cf6dfSJoerg Roedel * TODO: Consolidate with generic iommu-sva bind after it is merged. 1042672cf6dfSJoerg Roedel * It will require shared SVM data structures, i.e. combine io_mm 1043672cf6dfSJoerg Roedel * and intel_svm etc. 1044672cf6dfSJoerg Roedel */ 1045672cf6dfSJoerg Roedel if (drvdata) 1046*2a5054c6SFenghua Yu flags = *(unsigned int *)drvdata; 1047672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1048672cf6dfSJoerg Roedel ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev); 1049672cf6dfSJoerg Roedel if (ret) 1050672cf6dfSJoerg Roedel sva = ERR_PTR(ret); 1051672cf6dfSJoerg Roedel else if (sdev) 1052672cf6dfSJoerg Roedel sva = &sdev->sva; 1053672cf6dfSJoerg Roedel else 1054672cf6dfSJoerg Roedel WARN(!sdev, "SVM bind succeeded with no sdev!\n"); 1055672cf6dfSJoerg Roedel 1056672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1057672cf6dfSJoerg Roedel 1058672cf6dfSJoerg Roedel return sva; 1059672cf6dfSJoerg Roedel } 1060672cf6dfSJoerg Roedel 1061672cf6dfSJoerg Roedel void intel_svm_unbind(struct iommu_sva *sva) 1062672cf6dfSJoerg Roedel { 1063672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1064672cf6dfSJoerg Roedel 1065672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1066672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1067672cf6dfSJoerg Roedel intel_svm_unbind_mm(sdev->dev, sdev->pasid); 1068672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1069672cf6dfSJoerg Roedel } 1070672cf6dfSJoerg Roedel 1071c7b6bac9SFenghua Yu u32 intel_svm_get_pasid(struct iommu_sva *sva) 1072672cf6dfSJoerg Roedel { 1073672cf6dfSJoerg Roedel struct intel_svm_dev *sdev; 1074c7b6bac9SFenghua Yu u32 pasid; 1075672cf6dfSJoerg Roedel 1076672cf6dfSJoerg Roedel mutex_lock(&pasid_mutex); 1077672cf6dfSJoerg Roedel sdev = to_intel_svm_dev(sva); 1078672cf6dfSJoerg Roedel pasid = sdev->pasid; 1079672cf6dfSJoerg Roedel mutex_unlock(&pasid_mutex); 1080672cf6dfSJoerg Roedel 1081672cf6dfSJoerg Roedel return pasid; 1082672cf6dfSJoerg Roedel } 10838b737121SLu Baolu 10848b737121SLu Baolu int intel_svm_page_response(struct device *dev, 10858b737121SLu Baolu struct iommu_fault_event *evt, 10868b737121SLu Baolu struct iommu_page_response *msg) 10878b737121SLu Baolu { 10888b737121SLu Baolu struct iommu_fault_page_request *prm; 10898b737121SLu Baolu struct intel_svm_dev *sdev = NULL; 10908b737121SLu Baolu struct intel_svm *svm = NULL; 10918b737121SLu Baolu struct intel_iommu *iommu; 10928b737121SLu Baolu bool private_present; 10938b737121SLu Baolu bool pasid_present; 10948b737121SLu Baolu bool last_page; 10958b737121SLu Baolu u8 bus, devfn; 10968b737121SLu Baolu int ret = 0; 10978b737121SLu Baolu u16 sid; 10988b737121SLu Baolu 10998b737121SLu Baolu if (!dev || !dev_is_pci(dev)) 11008b737121SLu Baolu return -ENODEV; 11018b737121SLu Baolu 11028b737121SLu Baolu iommu = device_to_iommu(dev, &bus, &devfn); 11038b737121SLu Baolu if (!iommu) 11048b737121SLu Baolu return -ENODEV; 11058b737121SLu Baolu 11068b737121SLu Baolu if (!msg || !evt) 11078b737121SLu Baolu return -EINVAL; 11088b737121SLu Baolu 11098b737121SLu Baolu mutex_lock(&pasid_mutex); 11108b737121SLu Baolu 11118b737121SLu Baolu prm = &evt->fault.prm; 11128b737121SLu Baolu sid = PCI_DEVID(bus, devfn); 11138b737121SLu Baolu pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; 11148b737121SLu Baolu private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA; 11158b737121SLu Baolu last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE; 11168b737121SLu Baolu 11178b737121SLu Baolu if (!pasid_present) { 11188b737121SLu Baolu ret = -EINVAL; 11198b737121SLu Baolu goto out; 11208b737121SLu Baolu } 11218b737121SLu Baolu 11228b737121SLu Baolu if (prm->pasid == 0 || prm->pasid >= PASID_MAX) { 11238b737121SLu Baolu ret = -EINVAL; 11248b737121SLu Baolu goto out; 11258b737121SLu Baolu } 11268b737121SLu Baolu 11278b737121SLu Baolu ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev); 11288b737121SLu Baolu if (ret || !sdev) { 11298b737121SLu Baolu ret = -ENODEV; 11308b737121SLu Baolu goto out; 11318b737121SLu Baolu } 11328b737121SLu Baolu 11338b737121SLu Baolu /* 11348b737121SLu Baolu * For responses from userspace, need to make sure that the 11358b737121SLu Baolu * pasid has been bound to its mm. 11368b737121SLu Baolu */ 11378b737121SLu Baolu if (svm->flags & SVM_FLAG_GUEST_MODE) { 11388b737121SLu Baolu struct mm_struct *mm; 11398b737121SLu Baolu 11408b737121SLu Baolu mm = get_task_mm(current); 11418b737121SLu Baolu if (!mm) { 11428b737121SLu Baolu ret = -EINVAL; 11438b737121SLu Baolu goto out; 11448b737121SLu Baolu } 11458b737121SLu Baolu 11468b737121SLu Baolu if (mm != svm->mm) { 11478b737121SLu Baolu ret = -ENODEV; 11488b737121SLu Baolu mmput(mm); 11498b737121SLu Baolu goto out; 11508b737121SLu Baolu } 11518b737121SLu Baolu 11528b737121SLu Baolu mmput(mm); 11538b737121SLu Baolu } 11548b737121SLu Baolu 11558b737121SLu Baolu /* 11568b737121SLu Baolu * Per VT-d spec. v3.0 ch7.7, system software must respond 11578b737121SLu Baolu * with page group response if private data is present (PDP) 11588b737121SLu Baolu * or last page in group (LPIG) bit is set. This is an 11598b737121SLu Baolu * additional VT-d requirement beyond PCI ATS spec. 11608b737121SLu Baolu */ 11618b737121SLu Baolu if (last_page || private_present) { 11628b737121SLu Baolu struct qi_desc desc; 11638b737121SLu Baolu 11648b737121SLu Baolu desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) | 11658b737121SLu Baolu QI_PGRP_PASID_P(pasid_present) | 11668b737121SLu Baolu QI_PGRP_PDP(private_present) | 11678b737121SLu Baolu QI_PGRP_RESP_CODE(msg->code) | 11688b737121SLu Baolu QI_PGRP_RESP_TYPE; 11698b737121SLu Baolu desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page); 11708b737121SLu Baolu desc.qw2 = 0; 11718b737121SLu Baolu desc.qw3 = 0; 11728b737121SLu Baolu if (private_present) 11738b737121SLu Baolu memcpy(&desc.qw2, prm->private_data, 11748b737121SLu Baolu sizeof(prm->private_data)); 11758b737121SLu Baolu 11768b737121SLu Baolu qi_submit_sync(iommu, &desc, 1, 0); 11778b737121SLu Baolu } 11788b737121SLu Baolu out: 11798b737121SLu Baolu mutex_unlock(&pasid_mutex); 11808b737121SLu Baolu return ret; 11818b737121SLu Baolu } 1182