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>
22 #include "intel-pasid.h"
24 static irqreturn_t
prq_event_thread(int irq
, void *d
);
26 int intel_svm_init(struct intel_iommu
*iommu
)
28 if (cpu_feature_enabled(X86_FEATURE_GBPAGES
) &&
29 !cap_fl1gp_support(iommu
->cap
))
32 if (cpu_feature_enabled(X86_FEATURE_LA57
) &&
33 !cap_5lp_support(iommu
->cap
))
41 int intel_svm_enable_prq(struct intel_iommu
*iommu
)
46 pages
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, PRQ_ORDER
);
48 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
52 iommu
->prq
= page_address(pages
);
54 irq
= dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED
+ iommu
->seq_id
, iommu
->node
, iommu
);
56 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
60 free_pages((unsigned long)iommu
->prq
, PRQ_ORDER
);
66 snprintf(iommu
->prq_name
, sizeof(iommu
->prq_name
), "dmar%d-prq", iommu
->seq_id
);
68 ret
= request_threaded_irq(irq
, NULL
, prq_event_thread
, IRQF_ONESHOT
,
69 iommu
->prq_name
, iommu
);
71 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
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
, virt_to_phys(iommu
->prq
) | PRQ_ORDER
);
84 int intel_svm_finish_prq(struct intel_iommu
*iommu
)
86 dmar_writeq(iommu
->reg
+ DMAR_PQH_REG
, 0ULL);
87 dmar_writeq(iommu
->reg
+ DMAR_PQT_REG
, 0ULL);
88 dmar_writeq(iommu
->reg
+ DMAR_PQA_REG
, 0ULL);
91 free_irq(iommu
->pr_irq
, iommu
);
92 dmar_free_hwirq(iommu
->pr_irq
);
96 free_pages((unsigned long)iommu
->prq
, PRQ_ORDER
);
102 static void intel_flush_svm_range_dev (struct intel_svm
*svm
, struct intel_svm_dev
*sdev
,
103 unsigned long address
, unsigned long pages
, int ih
)
108 desc
.qw0
= QI_EIOTLB_PASID(svm
->pasid
) |
109 QI_EIOTLB_DID(sdev
->did
) |
110 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID
) |
114 int mask
= ilog2(__roundup_pow_of_two(pages
));
116 desc
.qw0
= QI_EIOTLB_PASID(svm
->pasid
) |
117 QI_EIOTLB_DID(sdev
->did
) |
118 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID
) |
120 desc
.qw1
= QI_EIOTLB_ADDR(address
) |
126 qi_submit_sync(&desc
, svm
->iommu
);
128 if (sdev
->dev_iotlb
) {
129 desc
.qw0
= QI_DEV_EIOTLB_PASID(svm
->pasid
) |
130 QI_DEV_EIOTLB_SID(sdev
->sid
) |
131 QI_DEV_EIOTLB_QDEP(sdev
->qdep
) |
134 desc
.qw1
= QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
136 } else if (pages
> 1) {
137 /* The least significant zero bit indicates the size. So,
138 * for example, an "address" value of 0x12345f000 will
139 * flush from 0x123440000 to 0x12347ffff (256KiB). */
140 unsigned long last
= address
+ ((unsigned long)(pages
- 1) << VTD_PAGE_SHIFT
);
141 unsigned long mask
= __rounddown_pow_of_two(address
^ last
);
143 desc
.qw1
= QI_DEV_EIOTLB_ADDR((address
& ~mask
) |
144 (mask
- 1)) | QI_DEV_EIOTLB_SIZE
;
146 desc
.qw1
= QI_DEV_EIOTLB_ADDR(address
);
150 qi_submit_sync(&desc
, svm
->iommu
);
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(svm
->iommu
, sdev
->dev
, svm
->pasid
);
196 intel_flush_svm_range_dev(svm
, sdev
, 0, -1, 0);
202 static const struct mmu_notifier_ops intel_mmuops
= {
203 .release
= intel_mm_release
,
204 .invalidate_range
= intel_invalidate_range
,
207 static DEFINE_MUTEX(pasid_mutex
);
208 static LIST_HEAD(global_svm_list
);
210 int intel_svm_bind_mm(struct device
*dev
, int *pasid
, int flags
, struct svm_dev_ops
*ops
)
212 struct intel_iommu
*iommu
= intel_svm_device_to_iommu(dev
);
213 struct device_domain_info
*info
;
214 struct intel_svm_dev
*sdev
;
215 struct intel_svm
*svm
= NULL
;
216 struct mm_struct
*mm
= NULL
;
220 if (!iommu
|| dmar_disabled
)
223 if (dev_is_pci(dev
)) {
224 pasid_max
= pci_max_pasids(to_pci_dev(dev
));
230 if (flags
& SVM_FLAG_SUPERVISOR_MODE
) {
231 if (!ecap_srs(iommu
->ecap
))
234 mm
= get_task_mm(current
);
238 mutex_lock(&pasid_mutex
);
239 if (pasid
&& !(flags
& SVM_FLAG_PRIVATE_PASID
)) {
242 list_for_each_entry(t
, &global_svm_list
, list
) {
243 if (t
->mm
!= mm
|| (t
->flags
& SVM_FLAG_PRIVATE_PASID
))
247 if (svm
->pasid
>= pasid_max
) {
249 "Limited PASID width. Cannot use existing PASID %d\n",
255 list_for_each_entry(sdev
, &svm
->devs
, list
) {
256 if (dev
== sdev
->dev
) {
257 if (sdev
->ops
!= ops
) {
270 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
277 ret
= intel_iommu_enable_pasid(iommu
, dev
);
279 /* If they don't actually want to assign a PASID, this is
280 * just an enabling check/preparation. */
285 info
= dev
->archdata
.iommu
;
286 if (!info
|| !info
->pasid_supported
) {
291 sdev
->did
= FLPT_DEFAULT_DID
;
292 sdev
->sid
= PCI_DEVID(info
->bus
, info
->devfn
);
293 if (info
->ats_enabled
) {
295 sdev
->qdep
= info
->ats_qdep
;
296 if (sdev
->qdep
>= QI_DEV_EIOTLB_MAX_INVS
)
300 /* Finish the setup now we know we're keeping it */
303 init_rcu_head(&sdev
->rcu
);
306 svm
= kzalloc(sizeof(*svm
), GFP_KERNEL
);
314 if (pasid_max
> intel_pasid_max_id
)
315 pasid_max
= intel_pasid_max_id
;
317 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
318 ret
= intel_pasid_alloc_id(svm
,
319 !!cap_caching_mode(iommu
->cap
),
320 pasid_max
, GFP_KERNEL
);
327 svm
->notifier
.ops
= &intel_mmuops
;
330 INIT_LIST_HEAD_RCU(&svm
->devs
);
331 INIT_LIST_HEAD(&svm
->list
);
334 ret
= mmu_notifier_register(&svm
->notifier
, mm
);
336 intel_pasid_free_id(svm
->pasid
);
343 spin_lock(&iommu
->lock
);
344 ret
= intel_pasid_setup_first_level(iommu
, dev
,
345 mm
? mm
->pgd
: init_mm
.pgd
,
346 svm
->pasid
, FLPT_DEFAULT_DID
,
347 mm
? 0 : PASID_FLAG_SUPERVISOR_MODE
);
348 spin_unlock(&iommu
->lock
);
351 mmu_notifier_unregister(&svm
->notifier
, mm
);
352 intel_pasid_free_id(svm
->pasid
);
358 list_add_tail(&svm
->list
, &global_svm_list
);
361 * Binding a new device with existing PASID, need to setup
364 spin_lock(&iommu
->lock
);
365 ret
= intel_pasid_setup_first_level(iommu
, dev
,
366 mm
? mm
->pgd
: init_mm
.pgd
,
367 svm
->pasid
, FLPT_DEFAULT_DID
,
368 mm
? 0 : PASID_FLAG_SUPERVISOR_MODE
);
369 spin_unlock(&iommu
->lock
);
375 list_add_rcu(&sdev
->list
, &svm
->devs
);
381 mutex_unlock(&pasid_mutex
);
386 EXPORT_SYMBOL_GPL(intel_svm_bind_mm
);
388 int intel_svm_unbind_mm(struct device
*dev
, int pasid
)
390 struct intel_svm_dev
*sdev
;
391 struct intel_iommu
*iommu
;
392 struct intel_svm
*svm
;
395 mutex_lock(&pasid_mutex
);
396 iommu
= intel_svm_device_to_iommu(dev
);
400 svm
= intel_pasid_lookup_id(pasid
);
404 list_for_each_entry(sdev
, &svm
->devs
, list
) {
405 if (dev
== sdev
->dev
) {
409 list_del_rcu(&sdev
->list
);
410 /* Flush the PASID cache and IOTLB for this device.
411 * Note that we do depend on the hardware *not* using
412 * the PASID any more. Just as we depend on other
413 * devices never using PASIDs that they have no right
414 * to use. We have a *shared* PASID table, because it's
415 * large and has to be physically contiguous. So it's
416 * hard to be as defensive as we might like. */
417 intel_pasid_tear_down_entry(iommu
, dev
, svm
->pasid
);
418 intel_flush_svm_range_dev(svm
, sdev
, 0, -1, 0);
419 kfree_rcu(sdev
, rcu
);
421 if (list_empty(&svm
->devs
)) {
422 intel_pasid_free_id(svm
->pasid
);
424 mmu_notifier_unregister(&svm
->notifier
, svm
->mm
);
426 list_del(&svm
->list
);
428 /* We mandate that no page faults may be outstanding
429 * for the PASID when intel_svm_unbind_mm() is called.
430 * If that is not obeyed, subtle errors will happen.
431 * Let's make them less subtle... */
432 memset(svm
, 0x6b, sizeof(*svm
));
440 mutex_unlock(&pasid_mutex
);
444 EXPORT_SYMBOL_GPL(intel_svm_unbind_mm
);
446 int intel_svm_is_pasid_valid(struct device
*dev
, int pasid
)
448 struct intel_iommu
*iommu
;
449 struct intel_svm
*svm
;
452 mutex_lock(&pasid_mutex
);
453 iommu
= intel_svm_device_to_iommu(dev
);
457 svm
= intel_pasid_lookup_id(pasid
);
461 /* init_mm is used in this case */
464 else if (atomic_read(&svm
->mm
->mm_users
) > 0)
470 mutex_unlock(&pasid_mutex
);
474 EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid
);
476 /* Page request queue descriptor */
477 struct page_req_dsc
{
482 u64 priv_data_present
:1;
505 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20)
507 static bool access_error(struct vm_area_struct
*vma
, struct page_req_dsc
*req
)
509 unsigned long requested
= 0;
512 requested
|= VM_EXEC
;
515 requested
|= VM_READ
;
518 requested
|= VM_WRITE
;
520 return (requested
& ~vma
->vm_flags
) != 0;
523 static bool is_canonical_address(u64 addr
)
525 int shift
= 64 - (__VIRTUAL_MASK_SHIFT
+ 1);
526 long saddr
= (long) addr
;
528 return (((saddr
<< shift
) >> shift
) == saddr
);
531 static irqreturn_t
prq_event_thread(int irq
, void *d
)
533 struct intel_iommu
*iommu
= d
;
534 struct intel_svm
*svm
= NULL
;
535 int head
, tail
, handled
= 0;
537 /* Clear PPR bit before reading head/tail registers, to
538 * ensure that we get a new interrupt if needed. */
539 writel(DMA_PRS_PPR
, iommu
->reg
+ DMAR_PRS_REG
);
541 tail
= dmar_readq(iommu
->reg
+ DMAR_PQT_REG
) & PRQ_RING_MASK
;
542 head
= dmar_readq(iommu
->reg
+ DMAR_PQH_REG
) & PRQ_RING_MASK
;
543 while (head
!= tail
) {
544 struct intel_svm_dev
*sdev
;
545 struct vm_area_struct
*vma
;
546 struct page_req_dsc
*req
;
554 req
= &iommu
->prq
[head
/ sizeof(*req
)];
556 result
= QI_RESP_FAILURE
;
557 address
= (u64
)req
->addr
<< VTD_PAGE_SHIFT
;
558 if (!req
->pasid_present
) {
559 pr_err("%s: Page request without PASID: %08llx %08llx\n",
560 iommu
->name
, ((unsigned long long *)req
)[0],
561 ((unsigned long long *)req
)[1]);
565 if (!svm
|| svm
->pasid
!= req
->pasid
) {
567 svm
= intel_pasid_lookup_id(req
->pasid
);
568 /* It *can't* go away, because the driver is not permitted
569 * to unbind the mm while any page faults are outstanding.
570 * So we only need RCU to protect the internal idr code. */
574 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
575 iommu
->name
, req
->pasid
, ((unsigned long long *)req
)[0],
576 ((unsigned long long *)req
)[1]);
581 result
= QI_RESP_INVALID
;
582 /* Since we're using init_mm.pgd directly, we should never take
583 * any faults on kernel addresses. */
587 /* If address is not canonical, return invalid response */
588 if (!is_canonical_address(address
))
591 /* If the mm is already defunct, don't handle faults. */
592 if (!mmget_not_zero(svm
->mm
))
595 down_read(&svm
->mm
->mmap_sem
);
596 vma
= find_extend_vma(svm
->mm
, address
);
597 if (!vma
|| address
< vma
->vm_start
)
600 if (access_error(vma
, req
))
603 ret
= handle_mm_fault(vma
, address
,
604 req
->wr_req
? FAULT_FLAG_WRITE
: 0);
605 if (ret
& VM_FAULT_ERROR
)
608 result
= QI_RESP_SUCCESS
;
610 up_read(&svm
->mm
->mmap_sem
);
613 /* Accounting for major/minor faults? */
615 list_for_each_entry_rcu(sdev
, &svm
->devs
, list
) {
616 if (sdev
->sid
== req
->rid
)
619 /* Other devices can go away, but the drivers are not permitted
620 * to unbind while any page faults might be in flight. So it's
621 * OK to drop the 'lock' here now we have it. */
624 if (WARN_ON(&sdev
->list
== &svm
->devs
))
627 if (sdev
&& sdev
->ops
&& sdev
->ops
->fault_cb
) {
628 int rwxp
= (req
->rd_req
<< 3) | (req
->wr_req
<< 2) |
629 (req
->exe_req
<< 1) | (req
->pm_req
);
630 sdev
->ops
->fault_cb(sdev
->dev
, req
->pasid
, req
->addr
,
631 req
->priv_data
, rwxp
, result
);
633 /* We get here in the error case where the PASID lookup failed,
634 and these can be NULL. Do not use them below this point! */
638 if (req
->lpig
|| req
->priv_data_present
) {
640 * Per VT-d spec. v3.0 ch7.7, system software must
641 * respond with page group response if private data
642 * is present (PDP) or last page in group (LPIG) bit
643 * is set. This is an additional VT-d feature beyond
646 resp
.qw0
= QI_PGRP_PASID(req
->pasid
) |
647 QI_PGRP_DID(req
->rid
) |
648 QI_PGRP_PASID_P(req
->pasid_present
) |
649 QI_PGRP_PDP(req
->pasid_present
) |
650 QI_PGRP_RESP_CODE(result
) |
652 resp
.qw1
= QI_PGRP_IDX(req
->prg_index
) |
653 QI_PGRP_LPIG(req
->lpig
);
655 if (req
->priv_data_present
)
656 memcpy(&resp
.qw2
, req
->priv_data
,
657 sizeof(req
->priv_data
));
660 qi_submit_sync(&resp
, iommu
);
662 head
= (head
+ sizeof(*req
)) & PRQ_RING_MASK
;
665 dmar_writeq(iommu
->reg
+ DMAR_PQH_REG
, tail
);
667 return IRQ_RETVAL(handled
);