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>
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->version != IOMMU_GPASID_BIND_VERSION_1 ||
288 data->format != IOMMU_PASID_FORMAT_INTEL_VTD)
291 if (!dev_is_pci(dev))
294 /* VT-d supports devices with full 20 bit PASIDs only */
295 if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX)
299 * We only check host PASID range, we have no knowledge to check
302 if (data->hpasid <= 0 || data->hpasid >= PASID_MAX)
305 dmar_domain = to_dmar_domain(domain);
307 mutex_lock(&pasid_mutex);
308 ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev);
314 * Do not allow multiple bindings of the same device-PASID since
315 * there is only one SL page tables per PASID. We may revisit
316 * once sharing PGD across domains are supported.
318 dev_warn_ratelimited(dev, "Already bound with PASID %u\n",
325 /* We come here when PASID has never been bond to a device. */
326 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
331 /* REVISIT: upper layer/VFIO can track host process that bind
332 * the PASID. ioasid_set = mm might be sufficient for vfio to
333 * check pasid VMM ownership. We can drop the following line
334 * once VFIO and IOASID set check is in place.
336 svm->mm = get_task_mm(current);
337 svm->pasid = data->hpasid;
338 if (data->flags & IOMMU_SVA_GPASID_VAL) {
339 svm->gpasid = data->gpasid;
340 svm->flags |= SVM_FLAG_GUEST_PASID;
342 ioasid_set_data(data->hpasid, svm);
343 INIT_LIST_HEAD_RCU(&svm->devs);
346 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
353 /* Only count users if device has aux domains */
354 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
357 /* Set up device context entry for PASID if not enabled already */
358 ret = intel_iommu_enable_pasid(iommu, sdev->dev);
360 dev_err_ratelimited(dev, "Failed to enable PASID capability\n");
366 * PASID table is per device for better security. Therefore, for
367 * each bind of a new device even with an existing PASID, we need to
368 * call the nested mode setup function here.
370 spin_lock(&iommu->lock);
371 ret = intel_pasid_setup_nested(iommu, dev,
372 (pgd_t *)(uintptr_t)data->gpgd,
373 data->hpasid, &data->vtd, dmar_domain,
375 spin_unlock(&iommu->lock);
377 dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",
380 * PASID entry should be in cleared state if nested mode
381 * set up failed. So we only need to clear IOASID tracking
382 * data such that free call will succeed.
388 svm->flags |= SVM_FLAG_GUEST_MODE;
390 init_rcu_head(&sdev->rcu);
391 list_add_rcu(&sdev->list, &svm->devs);
393 if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
394 ioasid_set_data(data->hpasid, NULL);
398 mutex_unlock(&pasid_mutex);
402 int intel_svm_unbind_gpasid(struct device *dev, int pasid)
404 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
405 struct intel_svm_dev *sdev;
406 struct intel_svm *svm;
412 mutex_lock(&pasid_mutex);
413 ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
418 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
421 list_del_rcu(&sdev->list);
422 intel_pasid_tear_down_entry(iommu, dev,
424 intel_svm_drain_prq(dev, svm->pasid);
425 kfree_rcu(sdev, rcu);
427 if (list_empty(&svm->devs)) {
429 * We do not free the IOASID here in that
430 * IOMMU driver did not allocate it.
431 * Unlike native SVM, IOASID for guest use was
432 * allocated prior to the bind call.
433 * In any case, if the free call comes before
434 * the unbind, IOMMU driver will get notified
435 * and perform cleanup.
437 ioasid_set_data(pasid, NULL);
443 mutex_unlock(&pasid_mutex);
447 /* Caller must hold pasid_mutex, mm reference */
449 intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops,
450 struct mm_struct *mm, struct intel_svm_dev **sd)
452 struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
453 struct device_domain_info *info;
454 struct intel_svm_dev *sdev;
455 struct intel_svm *svm = NULL;
459 if (!iommu || dmar_disabled)
462 if (!intel_svm_capable(iommu))
465 if (dev_is_pci(dev)) {
466 pasid_max = pci_max_pasids(to_pci_dev(dev));
472 /* Bind supervisor PASID shuld have mm = NULL */
473 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
474 if (!ecap_srs(iommu->ecap) || mm) {
475 pr_err("Supervisor PASID with user provided mm.\n");
480 if (!(flags & SVM_FLAG_PRIVATE_PASID)) {
483 list_for_each_entry(t, &global_svm_list, list) {
484 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
488 if (svm->pasid >= pasid_max) {
490 "Limited PASID width. Cannot use existing PASID %d\n",
496 /* Find the matching device in svm list */
497 for_each_svm_dev(sdev, svm, dev) {
498 if (sdev->ops != ops) {
510 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
517 ret = intel_iommu_enable_pasid(iommu, dev);
523 info = get_domain_info(dev);
524 sdev->did = FLPT_DEFAULT_DID;
525 sdev->sid = PCI_DEVID(info->bus, info->devfn);
526 if (info->ats_enabled) {
528 sdev->qdep = info->ats_qdep;
529 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
533 /* Finish the setup now we know we're keeping it */
536 init_rcu_head(&sdev->rcu);
539 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
547 if (pasid_max > intel_pasid_max_id)
548 pasid_max = intel_pasid_max_id;
550 /* Do not use PASID 0, reserved for RID to PASID */
551 svm->pasid = ioasid_alloc(NULL, PASID_MIN,
553 if (svm->pasid == INVALID_IOASID) {
559 svm->notifier.ops = &intel_mmuops;
562 INIT_LIST_HEAD_RCU(&svm->devs);
563 INIT_LIST_HEAD(&svm->list);
566 ret = mmu_notifier_register(&svm->notifier, mm);
568 ioasid_free(svm->pasid);
575 spin_lock(&iommu->lock);
576 ret = intel_pasid_setup_first_level(iommu, dev,
577 mm ? mm->pgd : init_mm.pgd,
578 svm->pasid, FLPT_DEFAULT_DID,
579 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
580 (cpu_feature_enabled(X86_FEATURE_LA57) ?
581 PASID_FLAG_FL5LP : 0));
582 spin_unlock(&iommu->lock);
585 mmu_notifier_unregister(&svm->notifier, mm);
586 ioasid_free(svm->pasid);
592 list_add_tail(&svm->list, &global_svm_list);
595 * Binding a new device with existing PASID, need to setup
598 spin_lock(&iommu->lock);
599 ret = intel_pasid_setup_first_level(iommu, dev,
600 mm ? mm->pgd : init_mm.pgd,
601 svm->pasid, FLPT_DEFAULT_DID,
602 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
603 (cpu_feature_enabled(X86_FEATURE_LA57) ?
604 PASID_FLAG_FL5LP : 0));
605 spin_unlock(&iommu->lock);
611 list_add_rcu(&sdev->list, &svm->devs);
613 sdev->pasid = svm->pasid;
622 /* Caller must hold pasid_mutex */
623 static int intel_svm_unbind_mm(struct device *dev, int pasid)
625 struct intel_svm_dev *sdev;
626 struct intel_iommu *iommu;
627 struct intel_svm *svm;
630 iommu = device_to_iommu(dev, NULL, NULL);
634 ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
641 list_del_rcu(&sdev->list);
642 /* Flush the PASID cache and IOTLB for this device.
643 * Note that we do depend on the hardware *not* using
644 * the PASID any more. Just as we depend on other
645 * devices never using PASIDs that they have no right
646 * to use. We have a *shared* PASID table, because it's
647 * large and has to be physically contiguous. So it's
648 * hard to be as defensive as we might like. */
649 intel_pasid_tear_down_entry(iommu, dev,
651 intel_svm_drain_prq(dev, svm->pasid);
652 kfree_rcu(sdev, rcu);
654 if (list_empty(&svm->devs)) {
655 ioasid_free(svm->pasid);
657 mmu_notifier_unregister(&svm->notifier, svm->mm);
658 list_del(&svm->list);
659 /* We mandate that no page faults may be outstanding
660 * for the PASID when intel_svm_unbind_mm() is called.
661 * If that is not obeyed, subtle errors will happen.
662 * Let's make them less subtle... */
663 memset(svm, 0x6b, sizeof(*svm));
672 /* Page request queue descriptor */
673 struct page_req_dsc {
678 u64 priv_data_present:1;
701 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20)
703 static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
705 unsigned long requested = 0;
708 requested |= VM_EXEC;
711 requested |= VM_READ;
714 requested |= VM_WRITE;
716 return (requested & ~vma->vm_flags) != 0;
719 static bool is_canonical_address(u64 addr)
721 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
722 long saddr = (long) addr;
724 return (((saddr << shift) >> shift) == saddr);
728 * intel_svm_drain_prq - Drain page requests and responses for a pasid
729 * @dev: target device
730 * @pasid: pasid for draining
732 * Drain all pending page requests and responses related to @pasid in both
733 * software and hardware. This is supposed to be called after the device
734 * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
735 * and DevTLB have been invalidated.
737 * It waits until all pending page requests for @pasid in the page fault
738 * queue are completed by the prq handling thread. Then follow the steps
739 * described in VT-d spec CH7.10 to drain all page requests and page
740 * responses pending in the hardware.
742 static void intel_svm_drain_prq(struct device *dev, int pasid)
744 struct device_domain_info *info;
745 struct dmar_domain *domain;
746 struct intel_iommu *iommu;
747 struct qi_desc desc[3];
748 struct pci_dev *pdev;
753 info = get_domain_info(dev);
754 if (WARN_ON(!info || !dev_is_pci(dev)))
757 if (!info->pri_enabled)
761 domain = info->domain;
762 pdev = to_pci_dev(dev);
763 sid = PCI_DEVID(info->bus, info->devfn);
764 did = domain->iommu_did[iommu->seq_id];
765 qdep = pci_ats_queue_depth(pdev);
768 * Check and wait until all pending page requests in the queue are
769 * handled by the prq handling thread.
772 reinit_completion(&iommu->prq_complete);
773 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
774 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
775 while (head != tail) {
776 struct page_req_dsc *req;
778 req = &iommu->prq[head / sizeof(*req)];
779 if (!req->pasid_present || req->pasid != pasid) {
780 head = (head + sizeof(*req)) & PRQ_RING_MASK;
784 wait_for_completion(&iommu->prq_complete);
789 * Perform steps described in VT-d spec CH7.10 to drain page
790 * requests and responses in hardware.
792 memset(desc, 0, sizeof(desc));
793 desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
796 desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
798 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
800 desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
801 QI_DEV_EIOTLB_SID(sid) |
802 QI_DEV_EIOTLB_QDEP(qdep) |
804 QI_DEV_IOTLB_PFSID(info->pfsid);
806 reinit_completion(&iommu->prq_complete);
807 qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
808 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
809 wait_for_completion(&iommu->prq_complete);
814 static int prq_to_iommu_prot(struct page_req_dsc *req)
819 prot |= IOMMU_FAULT_PERM_READ;
821 prot |= IOMMU_FAULT_PERM_WRITE;
823 prot |= IOMMU_FAULT_PERM_EXEC;
825 prot |= IOMMU_FAULT_PERM_PRIV;
831 intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc)
833 struct iommu_fault_event event;
835 if (!dev || !dev_is_pci(dev))
838 /* Fill in event data for device specific processing */
839 memset(&event, 0, sizeof(struct iommu_fault_event));
840 event.fault.type = IOMMU_FAULT_PAGE_REQ;
841 event.fault.prm.addr = desc->addr;
842 event.fault.prm.pasid = desc->pasid;
843 event.fault.prm.grpid = desc->prg_index;
844 event.fault.prm.perm = prq_to_iommu_prot(desc);
847 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
848 if (desc->pasid_present) {
849 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
850 event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
852 if (desc->priv_data_present) {
854 * Set last page in group bit if private data is present,
855 * page response is required as it does for LPIG.
856 * iommu_report_device_fault() doesn't understand this vendor
857 * specific requirement thus we set last_page as a workaround.
859 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
860 event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
861 memcpy(event.fault.prm.private_data, desc->priv_data,
862 sizeof(desc->priv_data));
865 return iommu_report_device_fault(dev, &event);
868 static irqreturn_t prq_event_thread(int irq, void *d)
870 struct intel_svm_dev *sdev = NULL;
871 struct intel_iommu *iommu = d;
872 struct intel_svm *svm = NULL;
873 int head, tail, handled = 0;
875 /* Clear PPR bit before reading head/tail registers, to
876 * ensure that we get a new interrupt if needed. */
877 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
879 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
880 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
881 while (head != tail) {
882 struct vm_area_struct *vma;
883 struct page_req_dsc *req;
891 req = &iommu->prq[head / sizeof(*req)];
893 result = QI_RESP_FAILURE;
894 address = (u64)req->addr << VTD_PAGE_SHIFT;
895 if (!req->pasid_present) {
896 pr_err("%s: Page request without PASID: %08llx %08llx\n",
897 iommu->name, ((unsigned long long *)req)[0],
898 ((unsigned long long *)req)[1]);
902 if (!svm || svm->pasid != req->pasid) {
904 svm = ioasid_find(NULL, req->pasid, NULL);
905 /* It *can't* go away, because the driver is not permitted
906 * to unbind the mm while any page faults are outstanding.
907 * So we only need RCU to protect the internal idr code. */
909 if (IS_ERR_OR_NULL(svm)) {
910 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
911 iommu->name, req->pasid, ((unsigned long long *)req)[0],
912 ((unsigned long long *)req)[1]);
917 if (!sdev || sdev->sid != req->rid) {
918 struct intel_svm_dev *t;
922 list_for_each_entry_rcu(t, &svm->devs, list) {
923 if (t->sid == req->rid) {
931 result = QI_RESP_INVALID;
932 /* Since we're using init_mm.pgd directly, we should never take
933 * any faults on kernel addresses. */
937 /* If address is not canonical, return invalid response */
938 if (!is_canonical_address(address))
942 * If prq is to be handled outside iommu driver via receiver of
943 * the fault notifiers, we skip the page response here.
945 if (svm->flags & SVM_FLAG_GUEST_MODE) {
946 if (sdev && !intel_svm_prq_report(sdev->dev, req))
952 /* If the mm is already defunct, don't handle faults. */
953 if (!mmget_not_zero(svm->mm))
956 mmap_read_lock(svm->mm);
957 vma = find_extend_vma(svm->mm, address);
958 if (!vma || address < vma->vm_start)
961 if (access_error(vma, req))
964 ret = handle_mm_fault(vma, address,
965 req->wr_req ? FAULT_FLAG_WRITE : 0,
967 if (ret & VM_FAULT_ERROR)
970 result = QI_RESP_SUCCESS;
972 mmap_read_unlock(svm->mm);
976 if (sdev && sdev->ops && sdev->ops->fault_cb) {
977 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
978 (req->exe_req << 1) | (req->pm_req);
979 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
980 req->priv_data, rwxp, result);
982 /* We get here in the error case where the PASID lookup failed,
983 and these can be NULL. Do not use them below this point! */
987 if (req->lpig || req->priv_data_present) {
989 * Per VT-d spec. v3.0 ch7.7, system software must
990 * respond with page group response if private data
991 * is present (PDP) or last page in group (LPIG) bit
992 * is set. This is an additional VT-d feature beyond
995 resp.qw0 = QI_PGRP_PASID(req->pasid) |
996 QI_PGRP_DID(req->rid) |
997 QI_PGRP_PASID_P(req->pasid_present) |
998 QI_PGRP_PDP(req->pasid_present) |
999 QI_PGRP_RESP_CODE(result) |
1001 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
1002 QI_PGRP_LPIG(req->lpig);
1004 if (req->priv_data_present)
1005 memcpy(&resp.qw2, req->priv_data,
1006 sizeof(req->priv_data));
1009 qi_submit_sync(iommu, &resp, 1, 0);
1012 head = (head + sizeof(*req)) & PRQ_RING_MASK;
1015 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
1018 * Clear the page request overflow bit and wake up all threads that
1019 * are waiting for the completion of this handling.
1021 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO)
1022 writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
1024 if (!completion_done(&iommu->prq_complete))
1025 complete(&iommu->prq_complete);
1027 return IRQ_RETVAL(handled);
1030 #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
1032 intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
1034 struct iommu_sva *sva = ERR_PTR(-EINVAL);
1035 struct intel_svm_dev *sdev = NULL;
1040 * TODO: Consolidate with generic iommu-sva bind after it is merged.
1041 * It will require shared SVM data structures, i.e. combine io_mm
1042 * and intel_svm etc.
1045 flags = *(int *)drvdata;
1046 mutex_lock(&pasid_mutex);
1047 ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev);
1053 WARN(!sdev, "SVM bind succeeded with no sdev!\n");
1055 mutex_unlock(&pasid_mutex);
1060 void intel_svm_unbind(struct iommu_sva *sva)
1062 struct intel_svm_dev *sdev;
1064 mutex_lock(&pasid_mutex);
1065 sdev = to_intel_svm_dev(sva);
1066 intel_svm_unbind_mm(sdev->dev, sdev->pasid);
1067 mutex_unlock(&pasid_mutex);
1070 int intel_svm_get_pasid(struct iommu_sva *sva)
1072 struct intel_svm_dev *sdev;
1075 mutex_lock(&pasid_mutex);
1076 sdev = to_intel_svm_dev(sva);
1077 pasid = sdev->pasid;
1078 mutex_unlock(&pasid_mutex);
1083 int intel_svm_page_response(struct device *dev,
1084 struct iommu_fault_event *evt,
1085 struct iommu_page_response *msg)
1087 struct iommu_fault_page_request *prm;
1088 struct intel_svm_dev *sdev = NULL;
1089 struct intel_svm *svm = NULL;
1090 struct intel_iommu *iommu;
1091 bool private_present;
1098 if (!dev || !dev_is_pci(dev))
1101 iommu = device_to_iommu(dev, &bus, &devfn);
1108 mutex_lock(&pasid_mutex);
1110 prm = &evt->fault.prm;
1111 sid = PCI_DEVID(bus, devfn);
1112 pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1113 private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
1114 last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
1116 if (!pasid_present) {
1121 if (prm->pasid == 0 || prm->pasid >= PASID_MAX) {
1126 ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev);
1133 * For responses from userspace, need to make sure that the
1134 * pasid has been bound to its mm.
1136 if (svm->flags & SVM_FLAG_GUEST_MODE) {
1137 struct mm_struct *mm;
1139 mm = get_task_mm(current);
1145 if (mm != svm->mm) {
1155 * Per VT-d spec. v3.0 ch7.7, system software must respond
1156 * with page group response if private data is present (PDP)
1157 * or last page in group (LPIG) bit is set. This is an
1158 * additional VT-d requirement beyond PCI ATS spec.
1160 if (last_page || private_present) {
1161 struct qi_desc desc;
1163 desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) |
1164 QI_PGRP_PASID_P(pasid_present) |
1165 QI_PGRP_PDP(private_present) |
1166 QI_PGRP_RESP_CODE(msg->code) |
1168 desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page);
1171 if (private_present)
1172 memcpy(&desc.qw2, prm->private_data,
1173 sizeof(prm->private_data));
1175 qi_submit_sync(iommu, &desc, 1, 0);
1178 mutex_unlock(&pasid_mutex);