1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2015 Intel Corporation.
5 * Authors: David Woodhouse <dwmw2@infradead.org>
8 #include <linux/intel-iommu.h>
9 #include <linux/mmu_notifier.h>
10 #include <linux/sched.h>
11 #include <linux/sched/mm.h>
12 #include <linux/slab.h>
13 #include <linux/intel-svm.h>
14 #include <linux/rculist.h>
15 #include <linux/pci.h>
16 #include <linux/pci-ats.h>
17 #include <linux/dmar.h>
18 #include <linux/interrupt.h>
19 #include <linux/mm_types.h>
20 #include <linux/ioasid.h>
22 #include <asm/fpu/api.h>
26 static irqreturn_t prq_event_thread(int irq, void *d);
27 static void intel_svm_drain_prq(struct device *dev, u32 pasid);
31 int intel_svm_enable_prq(struct intel_iommu *iommu)
36 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
38 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
42 iommu->prq = page_address(pages);
44 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
46 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
50 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
56 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
58 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
59 iommu->prq_name, iommu);
61 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
67 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
68 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
69 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
71 init_completion(&iommu->prq_complete);
76 int intel_svm_finish_prq(struct intel_iommu *iommu)
78 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
79 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
80 dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
83 free_irq(iommu->pr_irq, iommu);
84 dmar_free_hwirq(iommu->pr_irq);
88 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
94 static inline bool intel_svm_capable(struct intel_iommu *iommu)
96 return iommu->flags & VTD_FLAG_SVM_CAPABLE;
99 void intel_svm_check(struct intel_iommu *iommu)
101 if (!pasid_supported(iommu))
104 if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
105 !cap_fl1gp_support(iommu->cap)) {
106 pr_err("%s SVM disabled, incompatible 1GB page capability\n",
111 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
112 !cap_5lp_support(iommu->cap)) {
113 pr_err("%s SVM disabled, incompatible paging mode\n",
118 iommu->flags |= VTD_FLAG_SVM_CAPABLE;
121 static void __flush_svm_range_dev(struct intel_svm *svm,
122 struct intel_svm_dev *sdev,
123 unsigned long address,
124 unsigned long pages, int ih)
126 struct device_domain_info *info = get_domain_info(sdev->dev);
131 qi_flush_piotlb(sdev->iommu, sdev->did, svm->pasid, address, pages, ih);
132 if (info->ats_enabled)
133 qi_flush_dev_iotlb_pasid(sdev->iommu, sdev->sid, info->pfsid,
134 svm->pasid, sdev->qdep, address,
135 order_base_2(pages));
138 static void intel_flush_svm_range_dev(struct intel_svm *svm,
139 struct intel_svm_dev *sdev,
140 unsigned long address,
141 unsigned long pages, int ih)
143 unsigned long shift = ilog2(__roundup_pow_of_two(pages));
144 unsigned long align = (1ULL << (VTD_PAGE_SHIFT + shift));
145 unsigned long start = ALIGN_DOWN(address, align);
146 unsigned long end = ALIGN(address + (pages << VTD_PAGE_SHIFT), align);
148 while (start < end) {
149 __flush_svm_range_dev(svm, sdev, start, align >> VTD_PAGE_SHIFT, ih);
154 static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
155 unsigned long pages, int ih)
157 struct intel_svm_dev *sdev;
160 list_for_each_entry_rcu(sdev, &svm->devs, list)
161 intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
165 /* Pages have been freed at this point */
166 static void intel_invalidate_range(struct mmu_notifier *mn,
167 struct mm_struct *mm,
168 unsigned long start, unsigned long end)
170 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
172 intel_flush_svm_range(svm, start,
173 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
176 static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
178 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
179 struct intel_svm_dev *sdev;
181 /* This might end up being called from exit_mmap(), *before* the page
182 * tables are cleared. And __mmu_notifier_release() will delete us from
183 * the list of notifiers so that our invalidate_range() callback doesn't
184 * get called when the page tables are cleared. So we need to protect
185 * against hardware accessing those page tables.
187 * We do it by clearing the entry in the PASID table and then flushing
188 * the IOTLB and the PASID table caches. This might upset hardware;
189 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
190 * page) so that we end up taking a fault that the hardware really
191 * *has* to handle gracefully without affecting other processes.
194 list_for_each_entry_rcu(sdev, &svm->devs, list)
195 intel_pasid_tear_down_entry(sdev->iommu, sdev->dev,
201 static const struct mmu_notifier_ops intel_mmuops = {
202 .release = intel_mm_release,
203 .invalidate_range = intel_invalidate_range,
206 static DEFINE_MUTEX(pasid_mutex);
207 static LIST_HEAD(global_svm_list);
209 #define for_each_svm_dev(sdev, svm, d) \
210 list_for_each_entry((sdev), &(svm)->devs, list) \
211 if ((d) != (sdev)->dev) {} else
213 static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
214 struct intel_svm **rsvm,
215 struct intel_svm_dev **rsdev)
217 struct intel_svm_dev *d, *sdev = NULL;
218 struct intel_svm *svm;
220 /* The caller should hold the pasid_mutex lock */
221 if (WARN_ON(!mutex_is_locked(&pasid_mutex)))
224 if (pasid == INVALID_IOASID || pasid >= PASID_MAX)
227 svm = ioasid_find(NULL, pasid, NULL);
235 * If we found svm for the PASID, there must be at least one device
238 if (WARN_ON(list_empty(&svm->devs)))
242 list_for_each_entry_rcu(d, &svm->devs, list) {
257 int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
258 struct iommu_gpasid_bind_data *data)
260 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
261 struct intel_svm_dev *sdev = NULL;
262 struct dmar_domain *dmar_domain;
263 struct device_domain_info *info;
264 struct intel_svm *svm = NULL;
265 unsigned long iflags;
268 if (WARN_ON(!iommu) || !data)
271 if (data->format != IOMMU_PASID_FORMAT_INTEL_VTD)
274 /* IOMMU core ensures argsz is more than the start of the union */
275 if (data->argsz < offsetofend(struct iommu_gpasid_bind_data, vendor.vtd))
278 /* Make sure no undefined flags are used in vendor data */
279 if (data->vendor.vtd.flags & ~(IOMMU_SVA_VTD_GPASID_LAST - 1))
282 if (!dev_is_pci(dev))
285 /* VT-d supports devices with full 20 bit PASIDs only */
286 if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX)
290 * We only check host PASID range, we have no knowledge to check
293 if (data->hpasid <= 0 || data->hpasid >= PASID_MAX)
296 info = get_domain_info(dev);
300 dmar_domain = to_dmar_domain(domain);
302 mutex_lock(&pasid_mutex);
303 ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev);
309 * Do not allow multiple bindings of the same device-PASID since
310 * there is only one SL page tables per PASID. We may revisit
311 * once sharing PGD across domains are supported.
313 dev_warn_ratelimited(dev, "Already bound with PASID %u\n",
320 /* We come here when PASID has never been bond to a device. */
321 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
326 /* REVISIT: upper layer/VFIO can track host process that bind
327 * the PASID. ioasid_set = mm might be sufficient for vfio to
328 * check pasid VMM ownership. We can drop the following line
329 * once VFIO and IOASID set check is in place.
331 svm->mm = get_task_mm(current);
332 svm->pasid = data->hpasid;
333 if (data->flags & IOMMU_SVA_GPASID_VAL) {
334 svm->gpasid = data->gpasid;
335 svm->flags |= SVM_FLAG_GUEST_PASID;
337 ioasid_set_data(data->hpasid, svm);
338 INIT_LIST_HEAD_RCU(&svm->devs);
341 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
347 sdev->sid = PCI_DEVID(info->bus, info->devfn);
350 /* Only count users if device has aux domains */
351 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
354 /* Set up device context entry for PASID if not enabled already */
355 ret = intel_iommu_enable_pasid(iommu, sdev->dev);
357 dev_err_ratelimited(dev, "Failed to enable PASID capability\n");
363 * PASID table is per device for better security. Therefore, for
364 * each bind of a new device even with an existing PASID, we need to
365 * call the nested mode setup function here.
367 spin_lock_irqsave(&iommu->lock, iflags);
368 ret = intel_pasid_setup_nested(iommu, dev,
369 (pgd_t *)(uintptr_t)data->gpgd,
370 data->hpasid, &data->vendor.vtd, dmar_domain,
372 spin_unlock_irqrestore(&iommu->lock, iflags);
374 dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",
377 * PASID entry should be in cleared state if nested mode
378 * set up failed. So we only need to clear IOASID tracking
379 * data such that free call will succeed.
385 svm->flags |= SVM_FLAG_GUEST_MODE;
387 init_rcu_head(&sdev->rcu);
388 list_add_rcu(&sdev->list, &svm->devs);
390 if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
391 ioasid_set_data(data->hpasid, NULL);
395 mutex_unlock(&pasid_mutex);
399 int intel_svm_unbind_gpasid(struct device *dev, u32 pasid)
401 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
402 struct intel_svm_dev *sdev;
403 struct intel_svm *svm;
409 mutex_lock(&pasid_mutex);
410 ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
415 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
418 list_del_rcu(&sdev->list);
419 intel_pasid_tear_down_entry(iommu, dev,
421 intel_svm_drain_prq(dev, svm->pasid);
422 kfree_rcu(sdev, rcu);
424 if (list_empty(&svm->devs)) {
426 * We do not free the IOASID here in that
427 * IOMMU driver did not allocate it.
428 * Unlike native SVM, IOASID for guest use was
429 * allocated prior to the bind call.
430 * In any case, if the free call comes before
431 * the unbind, IOMMU driver will get notified
432 * and perform cleanup.
434 ioasid_set_data(pasid, NULL);
440 mutex_unlock(&pasid_mutex);
444 static void _load_pasid(void *unused)
449 static void load_pasid(struct mm_struct *mm, u32 pasid)
451 mutex_lock(&mm->context.lock);
453 /* Synchronize with READ_ONCE in update_pasid(). */
454 smp_store_release(&mm->pasid, pasid);
456 /* Update PASID MSR on all CPUs running the mm's tasks. */
457 on_each_cpu_mask(mm_cpumask(mm), _load_pasid, NULL, true);
459 mutex_unlock(&mm->context.lock);
462 /* Caller must hold pasid_mutex, mm reference */
464 intel_svm_bind_mm(struct device *dev, unsigned int flags,
465 struct svm_dev_ops *ops,
466 struct mm_struct *mm, struct intel_svm_dev **sd)
468 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
469 struct device_domain_info *info;
470 struct intel_svm_dev *sdev;
471 struct intel_svm *svm = NULL;
472 unsigned long iflags;
476 if (!iommu || dmar_disabled)
479 if (!intel_svm_capable(iommu))
482 if (dev_is_pci(dev)) {
483 pasid_max = pci_max_pasids(to_pci_dev(dev));
489 /* Bind supervisor PASID shuld have mm = NULL */
490 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
491 if (!ecap_srs(iommu->ecap) || mm) {
492 pr_err("Supervisor PASID with user provided mm.\n");
497 if (!(flags & SVM_FLAG_PRIVATE_PASID)) {
500 list_for_each_entry(t, &global_svm_list, list) {
501 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
505 if (svm->pasid >= pasid_max) {
507 "Limited PASID width. Cannot use existing PASID %d\n",
513 /* Find the matching device in svm list */
514 for_each_svm_dev(sdev, svm, dev) {
515 if (sdev->ops != ops) {
527 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
535 ret = intel_iommu_enable_pasid(iommu, dev);
541 info = get_domain_info(dev);
542 sdev->did = FLPT_DEFAULT_DID;
543 sdev->sid = PCI_DEVID(info->bus, info->devfn);
544 if (info->ats_enabled) {
546 sdev->qdep = info->ats_qdep;
547 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
551 /* Finish the setup now we know we're keeping it */
554 init_rcu_head(&sdev->rcu);
557 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
564 if (pasid_max > intel_pasid_max_id)
565 pasid_max = intel_pasid_max_id;
567 /* Do not use PASID 0, reserved for RID to PASID */
568 svm->pasid = ioasid_alloc(NULL, PASID_MIN,
570 if (svm->pasid == INVALID_IOASID) {
576 svm->notifier.ops = &intel_mmuops;
579 INIT_LIST_HEAD_RCU(&svm->devs);
580 INIT_LIST_HEAD(&svm->list);
583 ret = mmu_notifier_register(&svm->notifier, mm);
585 ioasid_free(svm->pasid);
592 spin_lock_irqsave(&iommu->lock, iflags);
593 ret = intel_pasid_setup_first_level(iommu, dev,
594 mm ? mm->pgd : init_mm.pgd,
595 svm->pasid, FLPT_DEFAULT_DID,
596 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
597 (cpu_feature_enabled(X86_FEATURE_LA57) ?
598 PASID_FLAG_FL5LP : 0));
599 spin_unlock_irqrestore(&iommu->lock, iflags);
602 mmu_notifier_unregister(&svm->notifier, mm);
603 ioasid_free(svm->pasid);
609 list_add_tail(&svm->list, &global_svm_list);
611 /* The newly allocated pasid is loaded to the mm. */
612 load_pasid(mm, svm->pasid);
616 * Binding a new device with existing PASID, need to setup
619 spin_lock_irqsave(&iommu->lock, iflags);
620 ret = intel_pasid_setup_first_level(iommu, dev,
621 mm ? mm->pgd : init_mm.pgd,
622 svm->pasid, FLPT_DEFAULT_DID,
623 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
624 (cpu_feature_enabled(X86_FEATURE_LA57) ?
625 PASID_FLAG_FL5LP : 0));
626 spin_unlock_irqrestore(&iommu->lock, iflags);
632 list_add_rcu(&sdev->list, &svm->devs);
634 sdev->pasid = svm->pasid;
643 /* Caller must hold pasid_mutex */
644 static int intel_svm_unbind_mm(struct device *dev, u32 pasid)
646 struct intel_svm_dev *sdev;
647 struct intel_iommu *iommu;
648 struct intel_svm *svm;
651 iommu = device_to_iommu(dev, NULL, NULL);
655 ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
662 list_del_rcu(&sdev->list);
663 /* Flush the PASID cache and IOTLB for this device.
664 * Note that we do depend on the hardware *not* using
665 * the PASID any more. Just as we depend on other
666 * devices never using PASIDs that they have no right
667 * to use. We have a *shared* PASID table, because it's
668 * large and has to be physically contiguous. So it's
669 * hard to be as defensive as we might like. */
670 intel_pasid_tear_down_entry(iommu, dev,
672 intel_svm_drain_prq(dev, svm->pasid);
673 kfree_rcu(sdev, rcu);
675 if (list_empty(&svm->devs)) {
676 ioasid_free(svm->pasid);
678 mmu_notifier_unregister(&svm->notifier, svm->mm);
679 /* Clear mm's pasid. */
680 load_pasid(svm->mm, PASID_DISABLED);
682 list_del(&svm->list);
683 /* We mandate that no page faults may be outstanding
684 * for the PASID when intel_svm_unbind_mm() is called.
685 * If that is not obeyed, subtle errors will happen.
686 * Let's make them less subtle... */
687 memset(svm, 0x6b, sizeof(*svm));
696 /* Page request queue descriptor */
697 struct page_req_dsc {
702 u64 priv_data_present:1;
725 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20)
727 static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
729 unsigned long requested = 0;
732 requested |= VM_EXEC;
735 requested |= VM_READ;
738 requested |= VM_WRITE;
740 return (requested & ~vma->vm_flags) != 0;
743 static bool is_canonical_address(u64 addr)
745 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
746 long saddr = (long) addr;
748 return (((saddr << shift) >> shift) == saddr);
752 * intel_svm_drain_prq - Drain page requests and responses for a pasid
753 * @dev: target device
754 * @pasid: pasid for draining
756 * Drain all pending page requests and responses related to @pasid in both
757 * software and hardware. This is supposed to be called after the device
758 * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
759 * and DevTLB have been invalidated.
761 * It waits until all pending page requests for @pasid in the page fault
762 * queue are completed by the prq handling thread. Then follow the steps
763 * described in VT-d spec CH7.10 to drain all page requests and page
764 * responses pending in the hardware.
766 static void intel_svm_drain_prq(struct device *dev, u32 pasid)
768 struct device_domain_info *info;
769 struct dmar_domain *domain;
770 struct intel_iommu *iommu;
771 struct qi_desc desc[3];
772 struct pci_dev *pdev;
777 info = get_domain_info(dev);
778 if (WARN_ON(!info || !dev_is_pci(dev)))
781 if (!info->pri_enabled)
785 domain = info->domain;
786 pdev = to_pci_dev(dev);
787 sid = PCI_DEVID(info->bus, info->devfn);
788 did = domain->iommu_did[iommu->seq_id];
789 qdep = pci_ats_queue_depth(pdev);
792 * Check and wait until all pending page requests in the queue are
793 * handled by the prq handling thread.
796 reinit_completion(&iommu->prq_complete);
797 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
798 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
799 while (head != tail) {
800 struct page_req_dsc *req;
802 req = &iommu->prq[head / sizeof(*req)];
803 if (!req->pasid_present || req->pasid != pasid) {
804 head = (head + sizeof(*req)) & PRQ_RING_MASK;
808 wait_for_completion(&iommu->prq_complete);
813 * Perform steps described in VT-d spec CH7.10 to drain page
814 * requests and responses in hardware.
816 memset(desc, 0, sizeof(desc));
817 desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
820 desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
822 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
824 desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
825 QI_DEV_EIOTLB_SID(sid) |
826 QI_DEV_EIOTLB_QDEP(qdep) |
828 QI_DEV_IOTLB_PFSID(info->pfsid);
830 reinit_completion(&iommu->prq_complete);
831 qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
832 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
833 wait_for_completion(&iommu->prq_complete);
838 static int prq_to_iommu_prot(struct page_req_dsc *req)
843 prot |= IOMMU_FAULT_PERM_READ;
845 prot |= IOMMU_FAULT_PERM_WRITE;
847 prot |= IOMMU_FAULT_PERM_EXEC;
849 prot |= IOMMU_FAULT_PERM_PRIV;
855 intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc)
857 struct iommu_fault_event event;
859 if (!dev || !dev_is_pci(dev))
862 /* Fill in event data for device specific processing */
863 memset(&event, 0, sizeof(struct iommu_fault_event));
864 event.fault.type = IOMMU_FAULT_PAGE_REQ;
865 event.fault.prm.addr = (u64)desc->addr << VTD_PAGE_SHIFT;
866 event.fault.prm.pasid = desc->pasid;
867 event.fault.prm.grpid = desc->prg_index;
868 event.fault.prm.perm = prq_to_iommu_prot(desc);
871 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
872 if (desc->pasid_present) {
873 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
874 event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
876 if (desc->priv_data_present) {
878 * Set last page in group bit if private data is present,
879 * page response is required as it does for LPIG.
880 * iommu_report_device_fault() doesn't understand this vendor
881 * specific requirement thus we set last_page as a workaround.
883 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
884 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
885 memcpy(event.fault.prm.private_data, desc->priv_data,
886 sizeof(desc->priv_data));
889 return iommu_report_device_fault(dev, &event);
892 static irqreturn_t prq_event_thread(int irq, void *d)
894 struct intel_svm_dev *sdev = NULL;
895 struct intel_iommu *iommu = d;
896 struct intel_svm *svm = NULL;
897 int head, tail, handled = 0;
899 /* Clear PPR bit before reading head/tail registers, to
900 * ensure that we get a new interrupt if needed. */
901 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
903 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
904 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
905 while (head != tail) {
906 struct vm_area_struct *vma;
907 struct page_req_dsc *req;
915 req = &iommu->prq[head / sizeof(*req)];
917 result = QI_RESP_FAILURE;
918 address = (u64)req->addr << VTD_PAGE_SHIFT;
919 if (!req->pasid_present) {
920 pr_err("%s: Page request without PASID: %08llx %08llx\n",
921 iommu->name, ((unsigned long long *)req)[0],
922 ((unsigned long long *)req)[1]);
925 /* We shall not receive page request for supervisor SVM */
926 if (req->pm_req && (req->rd_req | req->wr_req)) {
927 pr_err("Unexpected page request in Privilege Mode");
928 /* No need to find the matching sdev as for bad_req */
931 /* DMA read with exec requeset is not supported. */
932 if (req->exe_req && req->rd_req) {
933 pr_err("Execution request not supported\n");
936 if (!svm || svm->pasid != req->pasid) {
938 svm = ioasid_find(NULL, req->pasid, NULL);
939 /* It *can't* go away, because the driver is not permitted
940 * to unbind the mm while any page faults are outstanding.
941 * So we only need RCU to protect the internal idr code. */
943 if (IS_ERR_OR_NULL(svm)) {
944 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
945 iommu->name, req->pasid, ((unsigned long long *)req)[0],
946 ((unsigned long long *)req)[1]);
951 if (!sdev || sdev->sid != req->rid) {
952 struct intel_svm_dev *t;
956 list_for_each_entry_rcu(t, &svm->devs, list) {
957 if (t->sid == req->rid) {
965 result = QI_RESP_INVALID;
966 /* Since we're using init_mm.pgd directly, we should never take
967 * any faults on kernel addresses. */
971 /* If address is not canonical, return invalid response */
972 if (!is_canonical_address(address))
976 * If prq is to be handled outside iommu driver via receiver of
977 * the fault notifiers, we skip the page response here.
979 if (svm->flags & SVM_FLAG_GUEST_MODE) {
980 if (sdev && !intel_svm_prq_report(sdev->dev, req))
986 /* If the mm is already defunct, don't handle faults. */
987 if (!mmget_not_zero(svm->mm))
990 mmap_read_lock(svm->mm);
991 vma = find_extend_vma(svm->mm, address);
992 if (!vma || address < vma->vm_start)
995 if (access_error(vma, req))
998 ret = handle_mm_fault(vma, address,
999 req->wr_req ? FAULT_FLAG_WRITE : 0,
1001 if (ret & VM_FAULT_ERROR)
1004 result = QI_RESP_SUCCESS;
1006 mmap_read_unlock(svm->mm);
1010 if (sdev && sdev->ops && sdev->ops->fault_cb) {
1011 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
1012 (req->exe_req << 1) | (req->pm_req);
1013 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
1014 req->priv_data, rwxp, result);
1016 /* We get here in the error case where the PASID lookup failed,
1017 and these can be NULL. Do not use them below this point! */
1021 if (req->lpig || req->priv_data_present) {
1023 * Per VT-d spec. v3.0 ch7.7, system software must
1024 * respond with page group response if private data
1025 * is present (PDP) or last page in group (LPIG) bit
1026 * is set. This is an additional VT-d feature beyond
1029 resp.qw0 = QI_PGRP_PASID(req->pasid) |
1030 QI_PGRP_DID(req->rid) |
1031 QI_PGRP_PASID_P(req->pasid_present) |
1032 QI_PGRP_PDP(req->priv_data_present) |
1033 QI_PGRP_RESP_CODE(result) |
1035 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
1036 QI_PGRP_LPIG(req->lpig);
1040 if (req->priv_data_present)
1041 memcpy(&resp.qw2, req->priv_data,
1042 sizeof(req->priv_data));
1043 qi_submit_sync(iommu, &resp, 1, 0);
1046 head = (head + sizeof(*req)) & PRQ_RING_MASK;
1049 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
1052 * Clear the page request overflow bit and wake up all threads that
1053 * are waiting for the completion of this handling.
1055 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
1056 pr_info_ratelimited("IOMMU: %s: PRQ overflow detected\n",
1058 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
1059 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
1061 writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
1062 pr_info_ratelimited("IOMMU: %s: PRQ overflow cleared",
1067 if (!completion_done(&iommu->prq_complete))
1068 complete(&iommu->prq_complete);
1070 return IRQ_RETVAL(handled);
1073 #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
1075 intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
1077 struct iommu_sva *sva = ERR_PTR(-EINVAL);
1078 struct intel_svm_dev *sdev = NULL;
1079 unsigned int flags = 0;
1083 * TODO: Consolidate with generic iommu-sva bind after it is merged.
1084 * It will require shared SVM data structures, i.e. combine io_mm
1085 * and intel_svm etc.
1088 flags = *(unsigned int *)drvdata;
1089 mutex_lock(&pasid_mutex);
1090 ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev);
1096 WARN(!sdev, "SVM bind succeeded with no sdev!\n");
1098 mutex_unlock(&pasid_mutex);
1103 void intel_svm_unbind(struct iommu_sva *sva)
1105 struct intel_svm_dev *sdev;
1107 mutex_lock(&pasid_mutex);
1108 sdev = to_intel_svm_dev(sva);
1109 intel_svm_unbind_mm(sdev->dev, sdev->pasid);
1110 mutex_unlock(&pasid_mutex);
1113 u32 intel_svm_get_pasid(struct iommu_sva *sva)
1115 struct intel_svm_dev *sdev;
1118 mutex_lock(&pasid_mutex);
1119 sdev = to_intel_svm_dev(sva);
1120 pasid = sdev->pasid;
1121 mutex_unlock(&pasid_mutex);
1126 int intel_svm_page_response(struct device *dev,
1127 struct iommu_fault_event *evt,
1128 struct iommu_page_response *msg)
1130 struct iommu_fault_page_request *prm;
1131 struct intel_svm_dev *sdev = NULL;
1132 struct intel_svm *svm = NULL;
1133 struct intel_iommu *iommu;
1134 bool private_present;
1141 if (!dev || !dev_is_pci(dev))
1144 iommu = device_to_iommu(dev, &bus, &devfn);
1151 mutex_lock(&pasid_mutex);
1153 prm = &evt->fault.prm;
1154 sid = PCI_DEVID(bus, devfn);
1155 pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1156 private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
1157 last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
1159 if (!pasid_present) {
1164 if (prm->pasid == 0 || prm->pasid >= PASID_MAX) {
1169 ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev);
1176 * For responses from userspace, need to make sure that the
1177 * pasid has been bound to its mm.
1179 if (svm->flags & SVM_FLAG_GUEST_MODE) {
1180 struct mm_struct *mm;
1182 mm = get_task_mm(current);
1188 if (mm != svm->mm) {
1198 * Per VT-d spec. v3.0 ch7.7, system software must respond
1199 * with page group response if private data is present (PDP)
1200 * or last page in group (LPIG) bit is set. This is an
1201 * additional VT-d requirement beyond PCI ATS spec.
1203 if (last_page || private_present) {
1204 struct qi_desc desc;
1206 desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) |
1207 QI_PGRP_PASID_P(pasid_present) |
1208 QI_PGRP_PDP(private_present) |
1209 QI_PGRP_RESP_CODE(msg->code) |
1211 desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page);
1214 if (private_present)
1215 memcpy(&desc.qw2, prm->private_data,
1216 sizeof(prm->private_data));
1218 qi_submit_sync(iommu, &desc, 1, 0);
1221 mutex_unlock(&pasid_mutex);