1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2015 Intel Corporation.
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>
25 static irqreturn_t prq_event_thread(int irq, void *d);
26 static void intel_svm_drain_prq(struct device *dev, int pasid);
30 int intel_svm_enable_prq(struct intel_iommu *iommu)
35 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
37 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
41 iommu->prq = page_address(pages);
43 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
45 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
49 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
55 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
57 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
58 iommu->prq_name, iommu);
60 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
66 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
67 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
68 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
70 init_completion(&iommu->prq_complete);
75 int intel_svm_finish_prq(struct intel_iommu *iommu)
77 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
78 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
79 dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
82 free_irq(iommu->pr_irq, iommu);
83 dmar_free_hwirq(iommu->pr_irq);
87 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
93 static inline bool intel_svm_capable(struct intel_iommu *iommu)
95 return iommu->flags & VTD_FLAG_SVM_CAPABLE;
98 void intel_svm_check(struct intel_iommu *iommu)
100 if (!pasid_supported(iommu))
103 if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
104 !cap_fl1gp_support(iommu->cap)) {
105 pr_err("%s SVM disabled, incompatible 1GB page capability\n",
110 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
111 !cap_5lp_support(iommu->cap)) {
112 pr_err("%s SVM disabled, incompatible paging mode\n",
117 iommu->flags |= VTD_FLAG_SVM_CAPABLE;
120 static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
121 unsigned long address, unsigned long pages, int ih)
126 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
127 QI_EIOTLB_DID(sdev->did) |
128 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
132 int mask = ilog2(__roundup_pow_of_two(pages));
134 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
135 QI_EIOTLB_DID(sdev->did) |
136 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |
138 desc.qw1 = QI_EIOTLB_ADDR(address) |
144 qi_submit_sync(svm->iommu, &desc, 1, 0);
146 if (sdev->dev_iotlb) {
147 desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
148 QI_DEV_EIOTLB_SID(sdev->sid) |
149 QI_DEV_EIOTLB_QDEP(sdev->qdep) |
152 desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
154 } else if (pages > 1) {
155 /* The least significant zero bit indicates the size. So,
156 * for example, an "address" value of 0x12345f000 will
157 * flush from 0x123440000 to 0x12347ffff (256KiB). */
158 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
159 unsigned long mask = __rounddown_pow_of_two(address ^ last);
161 desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |
162 (mask - 1)) | QI_DEV_EIOTLB_SIZE;
164 desc.qw1 = QI_DEV_EIOTLB_ADDR(address);
168 qi_submit_sync(svm->iommu, &desc, 1, 0);
172 static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
173 unsigned long pages, int ih)
175 struct intel_svm_dev *sdev;
178 list_for_each_entry_rcu(sdev, &svm->devs, list)
179 intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
183 /* Pages have been freed at this point */
184 static void intel_invalidate_range(struct mmu_notifier *mn,
185 struct mm_struct *mm,
186 unsigned long start, unsigned long end)
188 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
190 intel_flush_svm_range(svm, start,
191 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
194 static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
196 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
197 struct intel_svm_dev *sdev;
199 /* This might end up being called from exit_mmap(), *before* the page
200 * tables are cleared. And __mmu_notifier_release() will delete us from
201 * the list of notifiers so that our invalidate_range() callback doesn't
202 * get called when the page tables are cleared. So we need to protect
203 * against hardware accessing those page tables.
205 * We do it by clearing the entry in the PASID table and then flushing
206 * the IOTLB and the PASID table caches. This might upset hardware;
207 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
208 * page) so that we end up taking a fault that the hardware really
209 * *has* to handle gracefully without affecting other processes.
212 list_for_each_entry_rcu(sdev, &svm->devs, list)
213 intel_pasid_tear_down_entry(svm->iommu, sdev->dev,
219 static const struct mmu_notifier_ops intel_mmuops = {
220 .release = intel_mm_release,
221 .invalidate_range = intel_invalidate_range,
224 static DEFINE_MUTEX(pasid_mutex);
225 static LIST_HEAD(global_svm_list);
227 #define for_each_svm_dev(sdev, svm, d) \
228 list_for_each_entry((sdev), &(svm)->devs, list) \
229 if ((d) != (sdev)->dev) {} else
231 static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
232 struct intel_svm **rsvm,
233 struct intel_svm_dev **rsdev)
235 struct intel_svm_dev *d, *sdev = NULL;
236 struct intel_svm *svm;
238 /* The caller should hold the pasid_mutex lock */
239 if (WARN_ON(!mutex_is_locked(&pasid_mutex)))
242 if (pasid == INVALID_IOASID || pasid >= PASID_MAX)
245 svm = ioasid_find(NULL, pasid, NULL);
253 * If we found svm for the PASID, there must be at least one device
256 if (WARN_ON(list_empty(&svm->devs)))
260 list_for_each_entry_rcu(d, &svm->devs, list) {
275 int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
276 struct iommu_gpasid_bind_data *data)
278 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
279 struct intel_svm_dev *sdev = NULL;
280 struct dmar_domain *dmar_domain;
281 struct intel_svm *svm = NULL;
284 if (WARN_ON(!iommu) || !data)
287 if (data->format != IOMMU_PASID_FORMAT_INTEL_VTD)
290 /* IOMMU core ensures argsz is more than the start of the union */
291 if (data->argsz < offsetofend(struct iommu_gpasid_bind_data, vendor.vtd))
294 /* Make sure no undefined flags are used in vendor data */
295 if (data->vendor.vtd.flags & ~(IOMMU_SVA_VTD_GPASID_LAST - 1))
298 if (!dev_is_pci(dev))
301 /* VT-d supports devices with full 20 bit PASIDs only */
302 if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX)
306 * We only check host PASID range, we have no knowledge to check
309 if (data->hpasid <= 0 || data->hpasid >= PASID_MAX)
312 dmar_domain = to_dmar_domain(domain);
314 mutex_lock(&pasid_mutex);
315 ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev);
321 * Do not allow multiple bindings of the same device-PASID since
322 * there is only one SL page tables per PASID. We may revisit
323 * once sharing PGD across domains are supported.
325 dev_warn_ratelimited(dev, "Already bound with PASID %u\n",
332 /* We come here when PASID has never been bond to a device. */
333 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
338 /* REVISIT: upper layer/VFIO can track host process that bind
339 * the PASID. ioasid_set = mm might be sufficient for vfio to
340 * check pasid VMM ownership. We can drop the following line
341 * once VFIO and IOASID set check is in place.
343 svm->mm = get_task_mm(current);
344 svm->pasid = data->hpasid;
345 if (data->flags & IOMMU_SVA_GPASID_VAL) {
346 svm->gpasid = data->gpasid;
347 svm->flags |= SVM_FLAG_GUEST_PASID;
349 ioasid_set_data(data->hpasid, svm);
350 INIT_LIST_HEAD_RCU(&svm->devs);
353 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
360 /* Only count users if device has aux domains */
361 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
364 /* Set up device context entry for PASID if not enabled already */
365 ret = intel_iommu_enable_pasid(iommu, sdev->dev);
367 dev_err_ratelimited(dev, "Failed to enable PASID capability\n");
373 * PASID table is per device for better security. Therefore, for
374 * each bind of a new device even with an existing PASID, we need to
375 * call the nested mode setup function here.
377 spin_lock(&iommu->lock);
378 ret = intel_pasid_setup_nested(iommu, dev,
379 (pgd_t *)(uintptr_t)data->gpgd,
380 data->hpasid, &data->vendor.vtd, dmar_domain,
382 spin_unlock(&iommu->lock);
384 dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",
387 * PASID entry should be in cleared state if nested mode
388 * set up failed. So we only need to clear IOASID tracking
389 * data such that free call will succeed.
395 svm->flags |= SVM_FLAG_GUEST_MODE;
397 init_rcu_head(&sdev->rcu);
398 list_add_rcu(&sdev->list, &svm->devs);
400 if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
401 ioasid_set_data(data->hpasid, NULL);
405 mutex_unlock(&pasid_mutex);
409 int intel_svm_unbind_gpasid(struct device *dev, int pasid)
411 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
412 struct intel_svm_dev *sdev;
413 struct intel_svm *svm;
419 mutex_lock(&pasid_mutex);
420 ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
425 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
428 list_del_rcu(&sdev->list);
429 intel_pasid_tear_down_entry(iommu, dev,
431 intel_svm_drain_prq(dev, svm->pasid);
432 kfree_rcu(sdev, rcu);
434 if (list_empty(&svm->devs)) {
436 * We do not free the IOASID here in that
437 * IOMMU driver did not allocate it.
438 * Unlike native SVM, IOASID for guest use was
439 * allocated prior to the bind call.
440 * In any case, if the free call comes before
441 * the unbind, IOMMU driver will get notified
442 * and perform cleanup.
444 ioasid_set_data(pasid, NULL);
450 mutex_unlock(&pasid_mutex);
454 /* Caller must hold pasid_mutex, mm reference */
456 intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops,
457 struct mm_struct *mm, struct intel_svm_dev **sd)
459 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
460 struct device_domain_info *info;
461 struct intel_svm_dev *sdev;
462 struct intel_svm *svm = NULL;
466 if (!iommu || dmar_disabled)
469 if (!intel_svm_capable(iommu))
472 if (dev_is_pci(dev)) {
473 pasid_max = pci_max_pasids(to_pci_dev(dev));
479 /* Bind supervisor PASID shuld have mm = NULL */
480 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
481 if (!ecap_srs(iommu->ecap) || mm) {
482 pr_err("Supervisor PASID with user provided mm.\n");
487 if (!(flags & SVM_FLAG_PRIVATE_PASID)) {
490 list_for_each_entry(t, &global_svm_list, list) {
491 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
495 if (svm->pasid >= pasid_max) {
497 "Limited PASID width. Cannot use existing PASID %d\n",
503 /* Find the matching device in svm list */
504 for_each_svm_dev(sdev, svm, dev) {
505 if (sdev->ops != ops) {
517 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
524 ret = intel_iommu_enable_pasid(iommu, dev);
530 info = get_domain_info(dev);
531 sdev->did = FLPT_DEFAULT_DID;
532 sdev->sid = PCI_DEVID(info->bus, info->devfn);
533 if (info->ats_enabled) {
535 sdev->qdep = info->ats_qdep;
536 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
540 /* Finish the setup now we know we're keeping it */
543 init_rcu_head(&sdev->rcu);
546 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
554 if (pasid_max > intel_pasid_max_id)
555 pasid_max = intel_pasid_max_id;
557 /* Do not use PASID 0, reserved for RID to PASID */
558 svm->pasid = ioasid_alloc(NULL, PASID_MIN,
560 if (svm->pasid == INVALID_IOASID) {
566 svm->notifier.ops = &intel_mmuops;
569 INIT_LIST_HEAD_RCU(&svm->devs);
570 INIT_LIST_HEAD(&svm->list);
573 ret = mmu_notifier_register(&svm->notifier, mm);
575 ioasid_free(svm->pasid);
582 spin_lock(&iommu->lock);
583 ret = intel_pasid_setup_first_level(iommu, dev,
584 mm ? mm->pgd : init_mm.pgd,
585 svm->pasid, FLPT_DEFAULT_DID,
586 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
587 (cpu_feature_enabled(X86_FEATURE_LA57) ?
588 PASID_FLAG_FL5LP : 0));
589 spin_unlock(&iommu->lock);
592 mmu_notifier_unregister(&svm->notifier, mm);
593 ioasid_free(svm->pasid);
599 list_add_tail(&svm->list, &global_svm_list);
602 * Binding a new device with existing PASID, need to setup
605 spin_lock(&iommu->lock);
606 ret = intel_pasid_setup_first_level(iommu, dev,
607 mm ? mm->pgd : init_mm.pgd,
608 svm->pasid, FLPT_DEFAULT_DID,
609 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
610 (cpu_feature_enabled(X86_FEATURE_LA57) ?
611 PASID_FLAG_FL5LP : 0));
612 spin_unlock(&iommu->lock);
618 list_add_rcu(&sdev->list, &svm->devs);
620 sdev->pasid = svm->pasid;
629 /* Caller must hold pasid_mutex */
630 static int intel_svm_unbind_mm(struct device *dev, int pasid)
632 struct intel_svm_dev *sdev;
633 struct intel_iommu *iommu;
634 struct intel_svm *svm;
637 iommu = device_to_iommu(dev, NULL, NULL);
641 ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
648 list_del_rcu(&sdev->list);
649 /* Flush the PASID cache and IOTLB for this device.
650 * Note that we do depend on the hardware *not* using
651 * the PASID any more. Just as we depend on other
652 * devices never using PASIDs that they have no right
653 * to use. We have a *shared* PASID table, because it's
654 * large and has to be physically contiguous. So it's
655 * hard to be as defensive as we might like. */
656 intel_pasid_tear_down_entry(iommu, dev,
658 intel_svm_drain_prq(dev, svm->pasid);
659 kfree_rcu(sdev, rcu);
661 if (list_empty(&svm->devs)) {
662 ioasid_free(svm->pasid);
664 mmu_notifier_unregister(&svm->notifier, svm->mm);
665 list_del(&svm->list);
666 /* We mandate that no page faults may be outstanding
667 * for the PASID when intel_svm_unbind_mm() is called.
668 * If that is not obeyed, subtle errors will happen.
669 * Let's make them less subtle... */
670 memset(svm, 0x6b, sizeof(*svm));
679 /* Page request queue descriptor */
680 struct page_req_dsc {
685 u64 priv_data_present:1;
708 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20)
710 static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
712 unsigned long requested = 0;
715 requested |= VM_EXEC;
718 requested |= VM_READ;
721 requested |= VM_WRITE;
723 return (requested & ~vma->vm_flags) != 0;
726 static bool is_canonical_address(u64 addr)
728 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
729 long saddr = (long) addr;
731 return (((saddr << shift) >> shift) == saddr);
735 * intel_svm_drain_prq - Drain page requests and responses for a pasid
736 * @dev: target device
737 * @pasid: pasid for draining
739 * Drain all pending page requests and responses related to @pasid in both
740 * software and hardware. This is supposed to be called after the device
741 * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
742 * and DevTLB have been invalidated.
744 * It waits until all pending page requests for @pasid in the page fault
745 * queue are completed by the prq handling thread. Then follow the steps
746 * described in VT-d spec CH7.10 to drain all page requests and page
747 * responses pending in the hardware.
749 static void intel_svm_drain_prq(struct device *dev, int pasid)
751 struct device_domain_info *info;
752 struct dmar_domain *domain;
753 struct intel_iommu *iommu;
754 struct qi_desc desc[3];
755 struct pci_dev *pdev;
760 info = get_domain_info(dev);
761 if (WARN_ON(!info || !dev_is_pci(dev)))
764 if (!info->pri_enabled)
768 domain = info->domain;
769 pdev = to_pci_dev(dev);
770 sid = PCI_DEVID(info->bus, info->devfn);
771 did = domain->iommu_did[iommu->seq_id];
772 qdep = pci_ats_queue_depth(pdev);
775 * Check and wait until all pending page requests in the queue are
776 * handled by the prq handling thread.
779 reinit_completion(&iommu->prq_complete);
780 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
781 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
782 while (head != tail) {
783 struct page_req_dsc *req;
785 req = &iommu->prq[head / sizeof(*req)];
786 if (!req->pasid_present || req->pasid != pasid) {
787 head = (head + sizeof(*req)) & PRQ_RING_MASK;
791 wait_for_completion(&iommu->prq_complete);
796 * Perform steps described in VT-d spec CH7.10 to drain page
797 * requests and responses in hardware.
799 memset(desc, 0, sizeof(desc));
800 desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
803 desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
805 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
807 desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
808 QI_DEV_EIOTLB_SID(sid) |
809 QI_DEV_EIOTLB_QDEP(qdep) |
811 QI_DEV_IOTLB_PFSID(info->pfsid);
813 reinit_completion(&iommu->prq_complete);
814 qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
815 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
816 wait_for_completion(&iommu->prq_complete);
821 static int prq_to_iommu_prot(struct page_req_dsc *req)
826 prot |= IOMMU_FAULT_PERM_READ;
828 prot |= IOMMU_FAULT_PERM_WRITE;
830 prot |= IOMMU_FAULT_PERM_EXEC;
832 prot |= IOMMU_FAULT_PERM_PRIV;
838 intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc)
840 struct iommu_fault_event event;
842 if (!dev || !dev_is_pci(dev))
845 /* Fill in event data for device specific processing */
846 memset(&event, 0, sizeof(struct iommu_fault_event));
847 event.fault.type = IOMMU_FAULT_PAGE_REQ;
848 event.fault.prm.addr = desc->addr;
849 event.fault.prm.pasid = desc->pasid;
850 event.fault.prm.grpid = desc->prg_index;
851 event.fault.prm.perm = prq_to_iommu_prot(desc);
854 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
855 if (desc->pasid_present) {
856 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
857 event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
859 if (desc->priv_data_present) {
861 * Set last page in group bit if private data is present,
862 * page response is required as it does for LPIG.
863 * iommu_report_device_fault() doesn't understand this vendor
864 * specific requirement thus we set last_page as a workaround.
866 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
867 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
868 memcpy(event.fault.prm.private_data, desc->priv_data,
869 sizeof(desc->priv_data));
872 return iommu_report_device_fault(dev, &event);
875 static irqreturn_t prq_event_thread(int irq, void *d)
877 struct intel_svm_dev *sdev = NULL;
878 struct intel_iommu *iommu = d;
879 struct intel_svm *svm = NULL;
880 int head, tail, handled = 0;
882 /* Clear PPR bit before reading head/tail registers, to
883 * ensure that we get a new interrupt if needed. */
884 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
886 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
887 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
888 while (head != tail) {
889 struct vm_area_struct *vma;
890 struct page_req_dsc *req;
898 req = &iommu->prq[head / sizeof(*req)];
900 result = QI_RESP_FAILURE;
901 address = (u64)req->addr << VTD_PAGE_SHIFT;
902 if (!req->pasid_present) {
903 pr_err("%s: Page request without PASID: %08llx %08llx\n",
904 iommu->name, ((unsigned long long *)req)[0],
905 ((unsigned long long *)req)[1]);
909 if (!svm || svm->pasid != req->pasid) {
911 svm = ioasid_find(NULL, req->pasid, NULL);
912 /* It *can't* go away, because the driver is not permitted
913 * to unbind the mm while any page faults are outstanding.
914 * So we only need RCU to protect the internal idr code. */
916 if (IS_ERR_OR_NULL(svm)) {
917 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
918 iommu->name, req->pasid, ((unsigned long long *)req)[0],
919 ((unsigned long long *)req)[1]);
924 if (!sdev || sdev->sid != req->rid) {
925 struct intel_svm_dev *t;
929 list_for_each_entry_rcu(t, &svm->devs, list) {
930 if (t->sid == req->rid) {
938 result = QI_RESP_INVALID;
939 /* Since we're using init_mm.pgd directly, we should never take
940 * any faults on kernel addresses. */
944 /* If address is not canonical, return invalid response */
945 if (!is_canonical_address(address))
949 * If prq is to be handled outside iommu driver via receiver of
950 * the fault notifiers, we skip the page response here.
952 if (svm->flags & SVM_FLAG_GUEST_MODE) {
953 if (sdev && !intel_svm_prq_report(sdev->dev, req))
959 /* If the mm is already defunct, don't handle faults. */
960 if (!mmget_not_zero(svm->mm))
963 mmap_read_lock(svm->mm);
964 vma = find_extend_vma(svm->mm, address);
965 if (!vma || address < vma->vm_start)
968 if (access_error(vma, req))
971 ret = handle_mm_fault(vma, address,
972 req->wr_req ? FAULT_FLAG_WRITE : 0,
974 if (ret & VM_FAULT_ERROR)
977 result = QI_RESP_SUCCESS;
979 mmap_read_unlock(svm->mm);
983 if (sdev && sdev->ops && sdev->ops->fault_cb) {
984 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
985 (req->exe_req << 1) | (req->pm_req);
986 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
987 req->priv_data, rwxp, result);
989 /* We get here in the error case where the PASID lookup failed,
990 and these can be NULL. Do not use them below this point! */
994 if (req->lpig || req->priv_data_present) {
996 * Per VT-d spec. v3.0 ch7.7, system software must
997 * respond with page group response if private data
998 * is present (PDP) or last page in group (LPIG) bit
999 * is set. This is an additional VT-d feature beyond
1002 resp.qw0 = QI_PGRP_PASID(req->pasid) |
1003 QI_PGRP_DID(req->rid) |
1004 QI_PGRP_PASID_P(req->pasid_present) |
1005 QI_PGRP_PDP(req->pasid_present) |
1006 QI_PGRP_RESP_CODE(result) |
1008 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
1009 QI_PGRP_LPIG(req->lpig);
1011 if (req->priv_data_present)
1012 memcpy(&resp.qw2, req->priv_data,
1013 sizeof(req->priv_data));
1016 qi_submit_sync(iommu, &resp, 1, 0);
1019 head = (head + sizeof(*req)) & PRQ_RING_MASK;
1022 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
1025 * Clear the page request overflow bit and wake up all threads that
1026 * are waiting for the completion of this handling.
1028 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO)
1029 writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
1031 if (!completion_done(&iommu->prq_complete))
1032 complete(&iommu->prq_complete);
1034 return IRQ_RETVAL(handled);
1037 #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
1039 intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
1041 struct iommu_sva *sva = ERR_PTR(-EINVAL);
1042 struct intel_svm_dev *sdev = NULL;
1047 * TODO: Consolidate with generic iommu-sva bind after it is merged.
1048 * It will require shared SVM data structures, i.e. combine io_mm
1049 * and intel_svm etc.
1052 flags = *(int *)drvdata;
1053 mutex_lock(&pasid_mutex);
1054 ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev);
1060 WARN(!sdev, "SVM bind succeeded with no sdev!\n");
1062 mutex_unlock(&pasid_mutex);
1067 void intel_svm_unbind(struct iommu_sva *sva)
1069 struct intel_svm_dev *sdev;
1071 mutex_lock(&pasid_mutex);
1072 sdev = to_intel_svm_dev(sva);
1073 intel_svm_unbind_mm(sdev->dev, sdev->pasid);
1074 mutex_unlock(&pasid_mutex);
1077 int intel_svm_get_pasid(struct iommu_sva *sva)
1079 struct intel_svm_dev *sdev;
1082 mutex_lock(&pasid_mutex);
1083 sdev = to_intel_svm_dev(sva);
1084 pasid = sdev->pasid;
1085 mutex_unlock(&pasid_mutex);
1090 int intel_svm_page_response(struct device *dev,
1091 struct iommu_fault_event *evt,
1092 struct iommu_page_response *msg)
1094 struct iommu_fault_page_request *prm;
1095 struct intel_svm_dev *sdev = NULL;
1096 struct intel_svm *svm = NULL;
1097 struct intel_iommu *iommu;
1098 bool private_present;
1105 if (!dev || !dev_is_pci(dev))
1108 iommu = device_to_iommu(dev, &bus, &devfn);
1115 mutex_lock(&pasid_mutex);
1117 prm = &evt->fault.prm;
1118 sid = PCI_DEVID(bus, devfn);
1119 pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1120 private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
1121 last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
1123 if (!pasid_present) {
1128 if (prm->pasid == 0 || prm->pasid >= PASID_MAX) {
1133 ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev);
1140 * For responses from userspace, need to make sure that the
1141 * pasid has been bound to its mm.
1143 if (svm->flags & SVM_FLAG_GUEST_MODE) {
1144 struct mm_struct *mm;
1146 mm = get_task_mm(current);
1152 if (mm != svm->mm) {
1162 * Per VT-d spec. v3.0 ch7.7, system software must respond
1163 * with page group response if private data is present (PDP)
1164 * or last page in group (LPIG) bit is set. This is an
1165 * additional VT-d requirement beyond PCI ATS spec.
1167 if (last_page || private_present) {
1168 struct qi_desc desc;
1170 desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) |
1171 QI_PGRP_PASID_P(pasid_present) |
1172 QI_PGRP_PDP(private_present) |
1173 QI_PGRP_RESP_CODE(msg->code) |
1175 desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page);
1178 if (private_present)
1179 memcpy(&desc.qw2, prm->private_data,
1180 sizeof(prm->private_data));
1182 qi_submit_sync(iommu, &desc, 1, 0);
1185 mutex_unlock(&pasid_mutex);