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 intel_flush_svm_range_dev (struct intel_svm
*svm
, struct intel_svm_dev
*sdev
,
122 unsigned long address
, unsigned long pages
, int ih
)
127 desc
.qw0
= QI_EIOTLB_PASID(svm
->pasid
) |
128 QI_EIOTLB_DID(sdev
->did
) |
129 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID
) |
133 int mask
= ilog2(__roundup_pow_of_two(pages
));
135 desc
.qw0
= QI_EIOTLB_PASID(svm
->pasid
) |
136 QI_EIOTLB_DID(sdev
->did
) |
137 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID
) |
139 desc
.qw1
= QI_EIOTLB_ADDR(address
) |
145 qi_submit_sync(sdev
->iommu
, &desc
, 1, 0);
147 if (sdev
->dev_iotlb
) {
148 desc
.qw0
= QI_DEV_EIOTLB_PASID(svm
->pasid
) |
149 QI_DEV_EIOTLB_SID(sdev
->sid
) |
150 QI_DEV_EIOTLB_QDEP(sdev
->qdep
) |
153 desc
.qw1
= QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
155 } else if (pages
> 1) {
156 /* The least significant zero bit indicates the size. So,
157 * for example, an "address" value of 0x12345f000 will
158 * flush from 0x123440000 to 0x12347ffff (256KiB). */
159 unsigned long last
= address
+ ((unsigned long)(pages
- 1) << VTD_PAGE_SHIFT
);
160 unsigned long mask
= __rounddown_pow_of_two(address
^ last
);
162 desc
.qw1
= QI_DEV_EIOTLB_ADDR((address
& ~mask
) |
163 (mask
- 1)) | QI_DEV_EIOTLB_SIZE
;
165 desc
.qw1
= QI_DEV_EIOTLB_ADDR(address
);
169 qi_submit_sync(sdev
->iommu
, &desc
, 1, 0);
173 static void intel_flush_svm_range(struct intel_svm
*svm
, unsigned long address
,
174 unsigned long pages
, int ih
)
176 struct intel_svm_dev
*sdev
;
179 list_for_each_entry_rcu(sdev
, &svm
->devs
, list
)
180 intel_flush_svm_range_dev(svm
, sdev
, address
, pages
, ih
);
184 /* Pages have been freed at this point */
185 static void intel_invalidate_range(struct mmu_notifier
*mn
,
186 struct mm_struct
*mm
,
187 unsigned long start
, unsigned long end
)
189 struct intel_svm
*svm
= container_of(mn
, struct intel_svm
, notifier
);
191 intel_flush_svm_range(svm
, start
,
192 (end
- start
+ PAGE_SIZE
- 1) >> VTD_PAGE_SHIFT
, 0);
195 static void intel_mm_release(struct mmu_notifier
*mn
, struct mm_struct
*mm
)
197 struct intel_svm
*svm
= container_of(mn
, struct intel_svm
, notifier
);
198 struct intel_svm_dev
*sdev
;
200 /* This might end up being called from exit_mmap(), *before* the page
201 * tables are cleared. And __mmu_notifier_release() will delete us from
202 * the list of notifiers so that our invalidate_range() callback doesn't
203 * get called when the page tables are cleared. So we need to protect
204 * against hardware accessing those page tables.
206 * We do it by clearing the entry in the PASID table and then flushing
207 * the IOTLB and the PASID table caches. This might upset hardware;
208 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
209 * page) so that we end up taking a fault that the hardware really
210 * *has* to handle gracefully without affecting other processes.
213 list_for_each_entry_rcu(sdev
, &svm
->devs
, list
)
214 intel_pasid_tear_down_entry(sdev
->iommu
, sdev
->dev
,
220 static const struct mmu_notifier_ops intel_mmuops
= {
221 .release
= intel_mm_release
,
222 .invalidate_range
= intel_invalidate_range
,
225 static DEFINE_MUTEX(pasid_mutex
);
226 static LIST_HEAD(global_svm_list
);
228 #define for_each_svm_dev(sdev, svm, d) \
229 list_for_each_entry((sdev), &(svm)->devs, list) \
230 if ((d) != (sdev)->dev) {} else
232 static int pasid_to_svm_sdev(struct device
*dev
, unsigned int pasid
,
233 struct intel_svm
**rsvm
,
234 struct intel_svm_dev
**rsdev
)
236 struct intel_svm_dev
*d
, *sdev
= NULL
;
237 struct intel_svm
*svm
;
239 /* The caller should hold the pasid_mutex lock */
240 if (WARN_ON(!mutex_is_locked(&pasid_mutex
)))
243 if (pasid
== INVALID_IOASID
|| pasid
>= PASID_MAX
)
246 svm
= ioasid_find(NULL
, pasid
, NULL
);
254 * If we found svm for the PASID, there must be at least one device
257 if (WARN_ON(list_empty(&svm
->devs
)))
261 list_for_each_entry_rcu(d
, &svm
->devs
, list
) {
276 int intel_svm_bind_gpasid(struct iommu_domain
*domain
, struct device
*dev
,
277 struct iommu_gpasid_bind_data
*data
)
279 struct intel_iommu
*iommu
= device_to_iommu(dev
, NULL
, NULL
);
280 struct intel_svm_dev
*sdev
= NULL
;
281 struct dmar_domain
*dmar_domain
;
282 struct device_domain_info
*info
;
283 struct intel_svm
*svm
= NULL
;
284 unsigned long iflags
;
287 if (WARN_ON(!iommu
) || !data
)
290 if (data
->format
!= IOMMU_PASID_FORMAT_INTEL_VTD
)
293 /* IOMMU core ensures argsz is more than the start of the union */
294 if (data
->argsz
< offsetofend(struct iommu_gpasid_bind_data
, vendor
.vtd
))
297 /* Make sure no undefined flags are used in vendor data */
298 if (data
->vendor
.vtd
.flags
& ~(IOMMU_SVA_VTD_GPASID_LAST
- 1))
301 if (!dev_is_pci(dev
))
304 /* VT-d supports devices with full 20 bit PASIDs only */
305 if (pci_max_pasids(to_pci_dev(dev
)) != PASID_MAX
)
309 * We only check host PASID range, we have no knowledge to check
312 if (data
->hpasid
<= 0 || data
->hpasid
>= PASID_MAX
)
315 info
= get_domain_info(dev
);
319 dmar_domain
= to_dmar_domain(domain
);
321 mutex_lock(&pasid_mutex
);
322 ret
= pasid_to_svm_sdev(dev
, data
->hpasid
, &svm
, &sdev
);
328 * Do not allow multiple bindings of the same device-PASID since
329 * there is only one SL page tables per PASID. We may revisit
330 * once sharing PGD across domains are supported.
332 dev_warn_ratelimited(dev
, "Already bound with PASID %u\n",
339 /* We come here when PASID has never been bond to a device. */
340 svm
= kzalloc(sizeof(*svm
), GFP_KERNEL
);
345 /* REVISIT: upper layer/VFIO can track host process that bind
346 * the PASID. ioasid_set = mm might be sufficient for vfio to
347 * check pasid VMM ownership. We can drop the following line
348 * once VFIO and IOASID set check is in place.
350 svm
->mm
= get_task_mm(current
);
351 svm
->pasid
= data
->hpasid
;
352 if (data
->flags
& IOMMU_SVA_GPASID_VAL
) {
353 svm
->gpasid
= data
->gpasid
;
354 svm
->flags
|= SVM_FLAG_GUEST_PASID
;
356 ioasid_set_data(data
->hpasid
, svm
);
357 INIT_LIST_HEAD_RCU(&svm
->devs
);
360 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
366 sdev
->sid
= PCI_DEVID(info
->bus
, info
->devfn
);
369 /* Only count users if device has aux domains */
370 if (iommu_dev_feature_enabled(dev
, IOMMU_DEV_FEAT_AUX
))
373 /* Set up device context entry for PASID if not enabled already */
374 ret
= intel_iommu_enable_pasid(iommu
, sdev
->dev
);
376 dev_err_ratelimited(dev
, "Failed to enable PASID capability\n");
382 * PASID table is per device for better security. Therefore, for
383 * each bind of a new device even with an existing PASID, we need to
384 * call the nested mode setup function here.
386 spin_lock_irqsave(&iommu
->lock
, iflags
);
387 ret
= intel_pasid_setup_nested(iommu
, dev
,
388 (pgd_t
*)(uintptr_t)data
->gpgd
,
389 data
->hpasid
, &data
->vendor
.vtd
, dmar_domain
,
391 spin_unlock_irqrestore(&iommu
->lock
, iflags
);
393 dev_err_ratelimited(dev
, "Failed to set up PASID %llu in nested mode, Err %d\n",
396 * PASID entry should be in cleared state if nested mode
397 * set up failed. So we only need to clear IOASID tracking
398 * data such that free call will succeed.
404 svm
->flags
|= SVM_FLAG_GUEST_MODE
;
406 init_rcu_head(&sdev
->rcu
);
407 list_add_rcu(&sdev
->list
, &svm
->devs
);
409 if (!IS_ERR_OR_NULL(svm
) && list_empty(&svm
->devs
)) {
410 ioasid_set_data(data
->hpasid
, NULL
);
414 mutex_unlock(&pasid_mutex
);
418 int intel_svm_unbind_gpasid(struct device
*dev
, u32 pasid
)
420 struct intel_iommu
*iommu
= device_to_iommu(dev
, NULL
, NULL
);
421 struct intel_svm_dev
*sdev
;
422 struct intel_svm
*svm
;
428 mutex_lock(&pasid_mutex
);
429 ret
= pasid_to_svm_sdev(dev
, pasid
, &svm
, &sdev
);
434 if (iommu_dev_feature_enabled(dev
, IOMMU_DEV_FEAT_AUX
))
437 list_del_rcu(&sdev
->list
);
438 intel_pasid_tear_down_entry(iommu
, dev
,
440 intel_svm_drain_prq(dev
, svm
->pasid
);
441 kfree_rcu(sdev
, rcu
);
443 if (list_empty(&svm
->devs
)) {
445 * We do not free the IOASID here in that
446 * IOMMU driver did not allocate it.
447 * Unlike native SVM, IOASID for guest use was
448 * allocated prior to the bind call.
449 * In any case, if the free call comes before
450 * the unbind, IOMMU driver will get notified
451 * and perform cleanup.
453 ioasid_set_data(pasid
, NULL
);
459 mutex_unlock(&pasid_mutex
);
463 static void _load_pasid(void *unused
)
468 static void load_pasid(struct mm_struct
*mm
, u32 pasid
)
470 mutex_lock(&mm
->context
.lock
);
472 /* Synchronize with READ_ONCE in update_pasid(). */
473 smp_store_release(&mm
->pasid
, pasid
);
475 /* Update PASID MSR on all CPUs running the mm's tasks. */
476 on_each_cpu_mask(mm_cpumask(mm
), _load_pasid
, NULL
, true);
478 mutex_unlock(&mm
->context
.lock
);
481 /* Caller must hold pasid_mutex, mm reference */
483 intel_svm_bind_mm(struct device
*dev
, unsigned int flags
,
484 struct svm_dev_ops
*ops
,
485 struct mm_struct
*mm
, struct intel_svm_dev
**sd
)
487 struct intel_iommu
*iommu
= device_to_iommu(dev
, NULL
, NULL
);
488 struct device_domain_info
*info
;
489 struct intel_svm_dev
*sdev
;
490 struct intel_svm
*svm
= NULL
;
491 unsigned long iflags
;
495 if (!iommu
|| dmar_disabled
)
498 if (!intel_svm_capable(iommu
))
501 if (dev_is_pci(dev
)) {
502 pasid_max
= pci_max_pasids(to_pci_dev(dev
));
508 /* Bind supervisor PASID shuld have mm = NULL */
509 if (flags
& SVM_FLAG_SUPERVISOR_MODE
) {
510 if (!ecap_srs(iommu
->ecap
) || mm
) {
511 pr_err("Supervisor PASID with user provided mm.\n");
516 if (!(flags
& SVM_FLAG_PRIVATE_PASID
)) {
519 list_for_each_entry(t
, &global_svm_list
, list
) {
520 if (t
->mm
!= mm
|| (t
->flags
& SVM_FLAG_PRIVATE_PASID
))
524 if (svm
->pasid
>= pasid_max
) {
526 "Limited PASID width. Cannot use existing PASID %d\n",
532 /* Find the matching device in svm list */
533 for_each_svm_dev(sdev
, svm
, dev
) {
534 if (sdev
->ops
!= ops
) {
546 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
554 ret
= intel_iommu_enable_pasid(iommu
, dev
);
560 info
= get_domain_info(dev
);
561 sdev
->did
= FLPT_DEFAULT_DID
;
562 sdev
->sid
= PCI_DEVID(info
->bus
, info
->devfn
);
563 if (info
->ats_enabled
) {
565 sdev
->qdep
= info
->ats_qdep
;
566 if (sdev
->qdep
>= QI_DEV_EIOTLB_MAX_INVS
)
570 /* Finish the setup now we know we're keeping it */
573 init_rcu_head(&sdev
->rcu
);
576 svm
= kzalloc(sizeof(*svm
), GFP_KERNEL
);
583 if (pasid_max
> intel_pasid_max_id
)
584 pasid_max
= intel_pasid_max_id
;
586 /* Do not use PASID 0, reserved for RID to PASID */
587 svm
->pasid
= ioasid_alloc(NULL
, PASID_MIN
,
589 if (svm
->pasid
== INVALID_IOASID
) {
595 svm
->notifier
.ops
= &intel_mmuops
;
598 INIT_LIST_HEAD_RCU(&svm
->devs
);
599 INIT_LIST_HEAD(&svm
->list
);
602 ret
= mmu_notifier_register(&svm
->notifier
, mm
);
604 ioasid_put(svm
->pasid
);
611 spin_lock_irqsave(&iommu
->lock
, iflags
);
612 ret
= intel_pasid_setup_first_level(iommu
, dev
,
613 mm
? mm
->pgd
: init_mm
.pgd
,
614 svm
->pasid
, FLPT_DEFAULT_DID
,
615 (mm
? 0 : PASID_FLAG_SUPERVISOR_MODE
) |
616 (cpu_feature_enabled(X86_FEATURE_LA57
) ?
617 PASID_FLAG_FL5LP
: 0));
618 spin_unlock_irqrestore(&iommu
->lock
, iflags
);
621 mmu_notifier_unregister(&svm
->notifier
, mm
);
622 ioasid_put(svm
->pasid
);
628 list_add_tail(&svm
->list
, &global_svm_list
);
630 /* The newly allocated pasid is loaded to the mm. */
631 load_pasid(mm
, svm
->pasid
);
635 * Binding a new device with existing PASID, need to setup
638 spin_lock_irqsave(&iommu
->lock
, iflags
);
639 ret
= intel_pasid_setup_first_level(iommu
, dev
,
640 mm
? mm
->pgd
: init_mm
.pgd
,
641 svm
->pasid
, FLPT_DEFAULT_DID
,
642 (mm
? 0 : PASID_FLAG_SUPERVISOR_MODE
) |
643 (cpu_feature_enabled(X86_FEATURE_LA57
) ?
644 PASID_FLAG_FL5LP
: 0));
645 spin_unlock_irqrestore(&iommu
->lock
, iflags
);
651 list_add_rcu(&sdev
->list
, &svm
->devs
);
653 sdev
->pasid
= svm
->pasid
;
662 /* Caller must hold pasid_mutex */
663 static int intel_svm_unbind_mm(struct device
*dev
, u32 pasid
)
665 struct intel_svm_dev
*sdev
;
666 struct intel_iommu
*iommu
;
667 struct intel_svm
*svm
;
670 iommu
= device_to_iommu(dev
, NULL
, NULL
);
674 ret
= pasid_to_svm_sdev(dev
, pasid
, &svm
, &sdev
);
681 list_del_rcu(&sdev
->list
);
682 /* Flush the PASID cache and IOTLB for this device.
683 * Note that we do depend on the hardware *not* using
684 * the PASID any more. Just as we depend on other
685 * devices never using PASIDs that they have no right
686 * to use. We have a *shared* PASID table, because it's
687 * large and has to be physically contiguous. So it's
688 * hard to be as defensive as we might like. */
689 intel_pasid_tear_down_entry(iommu
, dev
,
691 intel_svm_drain_prq(dev
, svm
->pasid
);
692 kfree_rcu(sdev
, rcu
);
694 if (list_empty(&svm
->devs
)) {
695 ioasid_put(svm
->pasid
);
697 mmu_notifier_unregister(&svm
->notifier
, svm
->mm
);
698 /* Clear mm's pasid. */
699 load_pasid(svm
->mm
, PASID_DISABLED
);
701 list_del(&svm
->list
);
702 /* We mandate that no page faults may be outstanding
703 * for the PASID when intel_svm_unbind_mm() is called.
704 * If that is not obeyed, subtle errors will happen.
705 * Let's make them less subtle... */
706 memset(svm
, 0x6b, sizeof(*svm
));
715 /* Page request queue descriptor */
716 struct page_req_dsc
{
721 u64 priv_data_present
:1;
744 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20)
746 static bool access_error(struct vm_area_struct
*vma
, struct page_req_dsc
*req
)
748 unsigned long requested
= 0;
751 requested
|= VM_EXEC
;
754 requested
|= VM_READ
;
757 requested
|= VM_WRITE
;
759 return (requested
& ~vma
->vm_flags
) != 0;
762 static bool is_canonical_address(u64 addr
)
764 int shift
= 64 - (__VIRTUAL_MASK_SHIFT
+ 1);
765 long saddr
= (long) addr
;
767 return (((saddr
<< shift
) >> shift
) == saddr
);
771 * intel_svm_drain_prq - Drain page requests and responses for a pasid
772 * @dev: target device
773 * @pasid: pasid for draining
775 * Drain all pending page requests and responses related to @pasid in both
776 * software and hardware. This is supposed to be called after the device
777 * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
778 * and DevTLB have been invalidated.
780 * It waits until all pending page requests for @pasid in the page fault
781 * queue are completed by the prq handling thread. Then follow the steps
782 * described in VT-d spec CH7.10 to drain all page requests and page
783 * responses pending in the hardware.
785 static void intel_svm_drain_prq(struct device
*dev
, u32 pasid
)
787 struct device_domain_info
*info
;
788 struct dmar_domain
*domain
;
789 struct intel_iommu
*iommu
;
790 struct qi_desc desc
[3];
791 struct pci_dev
*pdev
;
796 info
= get_domain_info(dev
);
797 if (WARN_ON(!info
|| !dev_is_pci(dev
)))
800 if (!info
->pri_enabled
)
804 domain
= info
->domain
;
805 pdev
= to_pci_dev(dev
);
806 sid
= PCI_DEVID(info
->bus
, info
->devfn
);
807 did
= domain
->iommu_did
[iommu
->seq_id
];
808 qdep
= pci_ats_queue_depth(pdev
);
811 * Check and wait until all pending page requests in the queue are
812 * handled by the prq handling thread.
815 reinit_completion(&iommu
->prq_complete
);
816 tail
= dmar_readq(iommu
->reg
+ DMAR_PQT_REG
) & PRQ_RING_MASK
;
817 head
= dmar_readq(iommu
->reg
+ DMAR_PQH_REG
) & PRQ_RING_MASK
;
818 while (head
!= tail
) {
819 struct page_req_dsc
*req
;
821 req
= &iommu
->prq
[head
/ sizeof(*req
)];
822 if (!req
->pasid_present
|| req
->pasid
!= pasid
) {
823 head
= (head
+ sizeof(*req
)) & PRQ_RING_MASK
;
827 wait_for_completion(&iommu
->prq_complete
);
832 * Perform steps described in VT-d spec CH7.10 to drain page
833 * requests and responses in hardware.
835 memset(desc
, 0, sizeof(desc
));
836 desc
[0].qw0
= QI_IWD_STATUS_DATA(QI_DONE
) |
839 desc
[1].qw0
= QI_EIOTLB_PASID(pasid
) |
841 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID
) |
843 desc
[2].qw0
= QI_DEV_EIOTLB_PASID(pasid
) |
844 QI_DEV_EIOTLB_SID(sid
) |
845 QI_DEV_EIOTLB_QDEP(qdep
) |
847 QI_DEV_IOTLB_PFSID(info
->pfsid
);
849 reinit_completion(&iommu
->prq_complete
);
850 qi_submit_sync(iommu
, desc
, 3, QI_OPT_WAIT_DRAIN
);
851 if (readl(iommu
->reg
+ DMAR_PRS_REG
) & DMA_PRS_PRO
) {
852 wait_for_completion(&iommu
->prq_complete
);
857 static int prq_to_iommu_prot(struct page_req_dsc
*req
)
862 prot
|= IOMMU_FAULT_PERM_READ
;
864 prot
|= IOMMU_FAULT_PERM_WRITE
;
866 prot
|= IOMMU_FAULT_PERM_EXEC
;
868 prot
|= IOMMU_FAULT_PERM_PRIV
;
874 intel_svm_prq_report(struct device
*dev
, struct page_req_dsc
*desc
)
876 struct iommu_fault_event event
;
878 if (!dev
|| !dev_is_pci(dev
))
881 /* Fill in event data for device specific processing */
882 memset(&event
, 0, sizeof(struct iommu_fault_event
));
883 event
.fault
.type
= IOMMU_FAULT_PAGE_REQ
;
884 event
.fault
.prm
.addr
= desc
->addr
;
885 event
.fault
.prm
.pasid
= desc
->pasid
;
886 event
.fault
.prm
.grpid
= desc
->prg_index
;
887 event
.fault
.prm
.perm
= prq_to_iommu_prot(desc
);
890 event
.fault
.prm
.flags
|= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE
;
891 if (desc
->pasid_present
) {
892 event
.fault
.prm
.flags
|= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID
;
893 event
.fault
.prm
.flags
|= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID
;
895 if (desc
->priv_data_present
) {
897 * Set last page in group bit if private data is present,
898 * page response is required as it does for LPIG.
899 * iommu_report_device_fault() doesn't understand this vendor
900 * specific requirement thus we set last_page as a workaround.
902 event
.fault
.prm
.flags
|= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE
;
903 event
.fault
.prm
.flags
|= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA
;
904 memcpy(event
.fault
.prm
.private_data
, desc
->priv_data
,
905 sizeof(desc
->priv_data
));
908 return iommu_report_device_fault(dev
, &event
);
911 static irqreturn_t
prq_event_thread(int irq
, void *d
)
913 struct intel_svm_dev
*sdev
= NULL
;
914 struct intel_iommu
*iommu
= d
;
915 struct intel_svm
*svm
= NULL
;
916 int head
, tail
, handled
= 0;
918 /* Clear PPR bit before reading head/tail registers, to
919 * ensure that we get a new interrupt if needed. */
920 writel(DMA_PRS_PPR
, iommu
->reg
+ DMAR_PRS_REG
);
922 tail
= dmar_readq(iommu
->reg
+ DMAR_PQT_REG
) & PRQ_RING_MASK
;
923 head
= dmar_readq(iommu
->reg
+ DMAR_PQH_REG
) & PRQ_RING_MASK
;
924 while (head
!= tail
) {
925 struct vm_area_struct
*vma
;
926 struct page_req_dsc
*req
;
934 req
= &iommu
->prq
[head
/ sizeof(*req
)];
936 result
= QI_RESP_FAILURE
;
937 address
= (u64
)req
->addr
<< VTD_PAGE_SHIFT
;
938 if (!req
->pasid_present
) {
939 pr_err("%s: Page request without PASID: %08llx %08llx\n",
940 iommu
->name
, ((unsigned long long *)req
)[0],
941 ((unsigned long long *)req
)[1]);
945 if (!svm
|| svm
->pasid
!= req
->pasid
) {
947 svm
= ioasid_find(NULL
, req
->pasid
, NULL
);
948 /* It *can't* go away, because the driver is not permitted
949 * to unbind the mm while any page faults are outstanding.
950 * So we only need RCU to protect the internal idr code. */
952 if (IS_ERR_OR_NULL(svm
)) {
953 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
954 iommu
->name
, req
->pasid
, ((unsigned long long *)req
)[0],
955 ((unsigned long long *)req
)[1]);
960 if (!sdev
|| sdev
->sid
!= req
->rid
) {
961 struct intel_svm_dev
*t
;
965 list_for_each_entry_rcu(t
, &svm
->devs
, list
) {
966 if (t
->sid
== req
->rid
) {
974 result
= QI_RESP_INVALID
;
975 /* Since we're using init_mm.pgd directly, we should never take
976 * any faults on kernel addresses. */
980 /* If address is not canonical, return invalid response */
981 if (!is_canonical_address(address
))
985 * If prq is to be handled outside iommu driver via receiver of
986 * the fault notifiers, we skip the page response here.
988 if (svm
->flags
& SVM_FLAG_GUEST_MODE
) {
989 if (sdev
&& !intel_svm_prq_report(sdev
->dev
, req
))
995 /* If the mm is already defunct, don't handle faults. */
996 if (!mmget_not_zero(svm
->mm
))
999 mmap_read_lock(svm
->mm
);
1000 vma
= find_extend_vma(svm
->mm
, address
);
1001 if (!vma
|| address
< vma
->vm_start
)
1004 if (access_error(vma
, req
))
1007 ret
= handle_mm_fault(vma
, address
,
1008 req
->wr_req
? FAULT_FLAG_WRITE
: 0,
1010 if (ret
& VM_FAULT_ERROR
)
1013 result
= QI_RESP_SUCCESS
;
1015 mmap_read_unlock(svm
->mm
);
1019 if (sdev
&& sdev
->ops
&& sdev
->ops
->fault_cb
) {
1020 int rwxp
= (req
->rd_req
<< 3) | (req
->wr_req
<< 2) |
1021 (req
->exe_req
<< 1) | (req
->pm_req
);
1022 sdev
->ops
->fault_cb(sdev
->dev
, req
->pasid
, req
->addr
,
1023 req
->priv_data
, rwxp
, result
);
1025 /* We get here in the error case where the PASID lookup failed,
1026 and these can be NULL. Do not use them below this point! */
1030 if (req
->lpig
|| req
->priv_data_present
) {
1032 * Per VT-d spec. v3.0 ch7.7, system software must
1033 * respond with page group response if private data
1034 * is present (PDP) or last page in group (LPIG) bit
1035 * is set. This is an additional VT-d feature beyond
1038 resp
.qw0
= QI_PGRP_PASID(req
->pasid
) |
1039 QI_PGRP_DID(req
->rid
) |
1040 QI_PGRP_PASID_P(req
->pasid_present
) |
1041 QI_PGRP_PDP(req
->priv_data_present
) |
1042 QI_PGRP_RESP_CODE(result
) |
1044 resp
.qw1
= QI_PGRP_IDX(req
->prg_index
) |
1045 QI_PGRP_LPIG(req
->lpig
);
1047 if (req
->priv_data_present
)
1048 memcpy(&resp
.qw2
, req
->priv_data
,
1049 sizeof(req
->priv_data
));
1052 qi_submit_sync(iommu
, &resp
, 1, 0);
1055 head
= (head
+ sizeof(*req
)) & PRQ_RING_MASK
;
1058 dmar_writeq(iommu
->reg
+ DMAR_PQH_REG
, tail
);
1061 * Clear the page request overflow bit and wake up all threads that
1062 * are waiting for the completion of this handling.
1064 if (readl(iommu
->reg
+ DMAR_PRS_REG
) & DMA_PRS_PRO
)
1065 writel(DMA_PRS_PRO
, iommu
->reg
+ DMAR_PRS_REG
);
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
);