2 * Copyright © 2015 Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * Authors: David Woodhouse <dwmw2@infradead.org>
16 #include <linux/intel-iommu.h>
17 #include <linux/mmu_notifier.h>
18 #include <linux/sched.h>
19 #include <linux/sched/mm.h>
20 #include <linux/slab.h>
21 #include <linux/intel-svm.h>
22 #include <linux/rculist.h>
23 #include <linux/pci.h>
24 #include <linux/pci-ats.h>
25 #include <linux/dmar.h>
26 #include <linux/interrupt.h>
28 static irqreturn_t
prq_event_thread(int irq
, void *d
);
34 struct pasid_state_entry
{
38 int intel_svm_alloc_pasid_tables(struct intel_iommu
*iommu
)
43 /* Start at 2 because it's defined as 2^(1+PSS) */
44 iommu
->pasid_max
= 2 << ecap_pss(iommu
->ecap
);
46 /* Eventually I'm promised we will get a multi-level PASID table
47 * and it won't have to be physically contiguous. Until then,
48 * limit the size because 8MiB contiguous allocations can be hard
49 * to come by. The limit of 0x20000, which is 1MiB for each of
50 * the PASID and PASID-state tables, is somewhat arbitrary. */
51 if (iommu
->pasid_max
> 0x20000)
52 iommu
->pasid_max
= 0x20000;
54 order
= get_order(sizeof(struct pasid_entry
) * iommu
->pasid_max
);
55 pages
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
57 pr_warn("IOMMU: %s: Failed to allocate PASID table\n",
61 iommu
->pasid_table
= page_address(pages
);
62 pr_info("%s: Allocated order %d PASID table.\n", iommu
->name
, order
);
64 if (ecap_dis(iommu
->ecap
)) {
65 /* Just making it explicit... */
66 BUILD_BUG_ON(sizeof(struct pasid_entry
) != sizeof(struct pasid_state_entry
));
67 pages
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
69 iommu
->pasid_state_table
= page_address(pages
);
71 pr_warn("IOMMU: %s: Failed to allocate PASID state table\n",
75 idr_init(&iommu
->pasid_idr
);
80 int intel_svm_free_pasid_tables(struct intel_iommu
*iommu
)
82 int order
= get_order(sizeof(struct pasid_entry
) * iommu
->pasid_max
);
84 if (iommu
->pasid_table
) {
85 free_pages((unsigned long)iommu
->pasid_table
, order
);
86 iommu
->pasid_table
= NULL
;
88 if (iommu
->pasid_state_table
) {
89 free_pages((unsigned long)iommu
->pasid_state_table
, order
);
90 iommu
->pasid_state_table
= NULL
;
92 idr_destroy(&iommu
->pasid_idr
);
98 int intel_svm_enable_prq(struct intel_iommu
*iommu
)
103 pages
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, PRQ_ORDER
);
105 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
109 iommu
->prq
= page_address(pages
);
111 irq
= dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED
+ iommu
->seq_id
, iommu
->node
, iommu
);
113 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
117 free_pages((unsigned long)iommu
->prq
, PRQ_ORDER
);
123 snprintf(iommu
->prq_name
, sizeof(iommu
->prq_name
), "dmar%d-prq", iommu
->seq_id
);
125 ret
= request_threaded_irq(irq
, NULL
, prq_event_thread
, IRQF_ONESHOT
,
126 iommu
->prq_name
, iommu
);
128 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
130 dmar_free_hwirq(irq
);
133 dmar_writeq(iommu
->reg
+ DMAR_PQH_REG
, 0ULL);
134 dmar_writeq(iommu
->reg
+ DMAR_PQT_REG
, 0ULL);
135 dmar_writeq(iommu
->reg
+ DMAR_PQA_REG
, virt_to_phys(iommu
->prq
) | PRQ_ORDER
);
140 int intel_svm_finish_prq(struct intel_iommu
*iommu
)
142 dmar_writeq(iommu
->reg
+ DMAR_PQH_REG
, 0ULL);
143 dmar_writeq(iommu
->reg
+ DMAR_PQT_REG
, 0ULL);
144 dmar_writeq(iommu
->reg
+ DMAR_PQA_REG
, 0ULL);
146 free_irq(iommu
->pr_irq
, iommu
);
147 dmar_free_hwirq(iommu
->pr_irq
);
150 free_pages((unsigned long)iommu
->prq
, PRQ_ORDER
);
156 static void intel_flush_svm_range_dev (struct intel_svm
*svm
, struct intel_svm_dev
*sdev
,
157 unsigned long address
, unsigned long pages
, int ih
, int gl
)
162 /* For global kernel pages we have to flush them in *all* PASIDs
163 * because that's the only option the hardware gives us. Despite
164 * the fact that they are actually only accessible through one. */
166 desc
.low
= QI_EIOTLB_PASID(svm
->pasid
) | QI_EIOTLB_DID(sdev
->did
) |
167 QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL
) | QI_EIOTLB_TYPE
;
169 desc
.low
= QI_EIOTLB_PASID(svm
->pasid
) | QI_EIOTLB_DID(sdev
->did
) |
170 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID
) | QI_EIOTLB_TYPE
;
173 int mask
= ilog2(__roundup_pow_of_two(pages
));
175 desc
.low
= QI_EIOTLB_PASID(svm
->pasid
) | QI_EIOTLB_DID(sdev
->did
) |
176 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID
) | QI_EIOTLB_TYPE
;
177 desc
.high
= QI_EIOTLB_ADDR(address
) | QI_EIOTLB_GL(gl
) |
178 QI_EIOTLB_IH(ih
) | QI_EIOTLB_AM(mask
);
180 qi_submit_sync(&desc
, svm
->iommu
);
182 if (sdev
->dev_iotlb
) {
183 desc
.low
= QI_DEV_EIOTLB_PASID(svm
->pasid
) | QI_DEV_EIOTLB_SID(sdev
->sid
) |
184 QI_DEV_EIOTLB_QDEP(sdev
->qdep
) | QI_DEIOTLB_TYPE
;
186 desc
.high
= QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | QI_DEV_EIOTLB_SIZE
;
187 } else if (pages
> 1) {
188 /* The least significant zero bit indicates the size. So,
189 * for example, an "address" value of 0x12345f000 will
190 * flush from 0x123440000 to 0x12347ffff (256KiB). */
191 unsigned long last
= address
+ ((unsigned long)(pages
- 1) << VTD_PAGE_SHIFT
);
192 unsigned long mask
= __rounddown_pow_of_two(address
^ last
);;
194 desc
.high
= QI_DEV_EIOTLB_ADDR((address
& ~mask
) | (mask
- 1)) | QI_DEV_EIOTLB_SIZE
;
196 desc
.high
= QI_DEV_EIOTLB_ADDR(address
);
198 qi_submit_sync(&desc
, svm
->iommu
);
202 static void intel_flush_svm_range(struct intel_svm
*svm
, unsigned long address
,
203 unsigned long pages
, int ih
, int gl
)
205 struct intel_svm_dev
*sdev
;
207 /* Try deferred invalidate if available */
208 if (svm
->iommu
->pasid_state_table
&&
209 !cmpxchg64(&svm
->iommu
->pasid_state_table
[svm
->pasid
].val
, 0, 1ULL << 63))
213 list_for_each_entry_rcu(sdev
, &svm
->devs
, list
)
214 intel_flush_svm_range_dev(svm
, sdev
, address
, pages
, ih
, gl
);
218 static void intel_change_pte(struct mmu_notifier
*mn
, struct mm_struct
*mm
,
219 unsigned long address
, pte_t pte
)
221 struct intel_svm
*svm
= container_of(mn
, struct intel_svm
, notifier
);
223 intel_flush_svm_range(svm
, address
, 1, 1, 0);
226 /* Pages have been freed at this point */
227 static void intel_invalidate_range(struct mmu_notifier
*mn
,
228 struct mm_struct
*mm
,
229 unsigned long start
, unsigned long end
)
231 struct intel_svm
*svm
= container_of(mn
, struct intel_svm
, notifier
);
233 intel_flush_svm_range(svm
, start
,
234 (end
- start
+ PAGE_SIZE
- 1) >> VTD_PAGE_SHIFT
, 0, 0);
238 static void intel_flush_pasid_dev(struct intel_svm
*svm
, struct intel_svm_dev
*sdev
, int pasid
)
243 desc
.low
= QI_PC_TYPE
| QI_PC_DID(sdev
->did
) | QI_PC_PASID_SEL
| QI_PC_PASID(pasid
);
245 qi_submit_sync(&desc
, svm
->iommu
);
248 static void intel_mm_release(struct mmu_notifier
*mn
, struct mm_struct
*mm
)
250 struct intel_svm
*svm
= container_of(mn
, struct intel_svm
, notifier
);
251 struct intel_svm_dev
*sdev
;
253 /* This might end up being called from exit_mmap(), *before* the page
254 * tables are cleared. And __mmu_notifier_release() will delete us from
255 * the list of notifiers so that our invalidate_range() callback doesn't
256 * get called when the page tables are cleared. So we need to protect
257 * against hardware accessing those page tables.
259 * We do it by clearing the entry in the PASID table and then flushing
260 * the IOTLB and the PASID table caches. This might upset hardware;
261 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
262 * page) so that we end up taking a fault that the hardware really
263 * *has* to handle gracefully without affecting other processes.
265 svm
->iommu
->pasid_table
[svm
->pasid
].val
= 0;
269 list_for_each_entry_rcu(sdev
, &svm
->devs
, list
) {
270 intel_flush_pasid_dev(svm
, sdev
, svm
->pasid
);
271 intel_flush_svm_range_dev(svm
, sdev
, 0, -1, 0, !svm
->mm
);
277 static const struct mmu_notifier_ops intel_mmuops
= {
278 .release
= intel_mm_release
,
279 .change_pte
= intel_change_pte
,
280 .invalidate_range
= intel_invalidate_range
,
283 static DEFINE_MUTEX(pasid_mutex
);
285 int intel_svm_bind_mm(struct device
*dev
, int *pasid
, int flags
, struct svm_dev_ops
*ops
)
287 struct intel_iommu
*iommu
= intel_svm_device_to_iommu(dev
);
288 struct intel_svm_dev
*sdev
;
289 struct intel_svm
*svm
= NULL
;
290 struct mm_struct
*mm
= NULL
;
297 if (dev_is_pci(dev
)) {
298 pasid_max
= pci_max_pasids(to_pci_dev(dev
));
304 if ((flags
& SVM_FLAG_SUPERVISOR_MODE
)) {
305 if (!ecap_srs(iommu
->ecap
))
308 mm
= get_task_mm(current
);
312 mutex_lock(&pasid_mutex
);
313 if (pasid
&& !(flags
& SVM_FLAG_PRIVATE_PASID
)) {
316 idr_for_each_entry(&iommu
->pasid_idr
, svm
, i
) {
318 (svm
->flags
& SVM_FLAG_PRIVATE_PASID
))
321 if (svm
->pasid
>= pasid_max
) {
323 "Limited PASID width. Cannot use existing PASID %d\n",
329 list_for_each_entry(sdev
, &svm
->devs
, list
) {
330 if (dev
== sdev
->dev
) {
331 if (sdev
->ops
!= ops
) {
344 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
351 ret
= intel_iommu_enable_pasid(iommu
, sdev
);
353 /* If they don't actually want to assign a PASID, this is
354 * just an enabling check/preparation. */
358 /* Finish the setup now we know we're keeping it */
361 init_rcu_head(&sdev
->rcu
);
364 svm
= kzalloc(sizeof(*svm
), GFP_KERNEL
);
372 if (pasid_max
> iommu
->pasid_max
)
373 pasid_max
= iommu
->pasid_max
;
375 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
376 ret
= idr_alloc(&iommu
->pasid_idr
, svm
,
377 !!cap_caching_mode(iommu
->cap
),
378 pasid_max
- 1, GFP_KERNEL
);
384 svm
->notifier
.ops
= &intel_mmuops
;
387 INIT_LIST_HEAD_RCU(&svm
->devs
);
390 ret
= mmu_notifier_register(&svm
->notifier
, mm
);
392 idr_remove(&svm
->iommu
->pasid_idr
, svm
->pasid
);
397 iommu
->pasid_table
[svm
->pasid
].val
= (u64
)__pa(mm
->pgd
) | 1;
399 iommu
->pasid_table
[svm
->pasid
].val
= (u64
)__pa(init_mm
.pgd
) | 1 | (1ULL << 11);
401 /* In caching mode, we still have to flush with PASID 0 when
402 * a PASID table entry becomes present. Not entirely clear
403 * *why* that would be the case — surely we could just issue
404 * a flush with the PASID value that we've changed? The PASID
405 * is the index into the table, after all. It's not like domain
406 * IDs in the case of the equivalent context-entry change in
407 * caching mode. And for that matter it's not entirely clear why
408 * a VMM would be in the business of caching the PASID table
409 * anyway. Surely that can be left entirely to the guest? */
410 if (cap_caching_mode(iommu
->cap
))
411 intel_flush_pasid_dev(svm
, sdev
, 0);
413 list_add_rcu(&sdev
->list
, &svm
->devs
);
419 mutex_unlock(&pasid_mutex
);
424 EXPORT_SYMBOL_GPL(intel_svm_bind_mm
);
426 int intel_svm_unbind_mm(struct device
*dev
, int pasid
)
428 struct intel_svm_dev
*sdev
;
429 struct intel_iommu
*iommu
;
430 struct intel_svm
*svm
;
433 mutex_lock(&pasid_mutex
);
434 iommu
= intel_svm_device_to_iommu(dev
);
435 if (!iommu
|| !iommu
->pasid_table
)
438 svm
= idr_find(&iommu
->pasid_idr
, pasid
);
442 list_for_each_entry(sdev
, &svm
->devs
, list
) {
443 if (dev
== sdev
->dev
) {
447 list_del_rcu(&sdev
->list
);
448 /* Flush the PASID cache and IOTLB for this device.
449 * Note that we do depend on the hardware *not* using
450 * the PASID any more. Just as we depend on other
451 * devices never using PASIDs that they have no right
452 * to use. We have a *shared* PASID table, because it's
453 * large and has to be physically contiguous. So it's
454 * hard to be as defensive as we might like. */
455 intel_flush_pasid_dev(svm
, sdev
, svm
->pasid
);
456 intel_flush_svm_range_dev(svm
, sdev
, 0, -1, 0, !svm
->mm
);
457 kfree_rcu(sdev
, rcu
);
459 if (list_empty(&svm
->devs
)) {
461 idr_remove(&svm
->iommu
->pasid_idr
, svm
->pasid
);
463 mmu_notifier_unregister(&svm
->notifier
, svm
->mm
);
465 /* We mandate that no page faults may be outstanding
466 * for the PASID when intel_svm_unbind_mm() is called.
467 * If that is not obeyed, subtle errors will happen.
468 * Let's make them less subtle... */
469 memset(svm
, 0x6b, sizeof(*svm
));
477 mutex_unlock(&pasid_mutex
);
481 EXPORT_SYMBOL_GPL(intel_svm_unbind_mm
);
483 int intel_svm_is_pasid_valid(struct device
*dev
, int pasid
)
485 struct intel_iommu
*iommu
;
486 struct intel_svm
*svm
;
489 mutex_lock(&pasid_mutex
);
490 iommu
= intel_svm_device_to_iommu(dev
);
491 if (!iommu
|| !iommu
->pasid_table
)
494 svm
= idr_find(&iommu
->pasid_idr
, pasid
);
498 /* init_mm is used in this case */
501 else if (atomic_read(&svm
->mm
->mm_users
) > 0)
507 mutex_unlock(&pasid_mutex
);
511 EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid
);
513 /* Page request queue descriptor */
514 struct page_req_dsc
{
531 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
533 static bool access_error(struct vm_area_struct
*vma
, struct page_req_dsc
*req
)
535 unsigned long requested
= 0;
538 requested
|= VM_EXEC
;
541 requested
|= VM_READ
;
544 requested
|= VM_WRITE
;
546 return (requested
& ~vma
->vm_flags
) != 0;
549 static irqreturn_t
prq_event_thread(int irq
, void *d
)
551 struct intel_iommu
*iommu
= d
;
552 struct intel_svm
*svm
= NULL
;
553 int head
, tail
, handled
= 0;
555 /* Clear PPR bit before reading head/tail registers, to
556 * ensure that we get a new interrupt if needed. */
557 writel(DMA_PRS_PPR
, iommu
->reg
+ DMAR_PRS_REG
);
559 tail
= dmar_readq(iommu
->reg
+ DMAR_PQT_REG
) & PRQ_RING_MASK
;
560 head
= dmar_readq(iommu
->reg
+ DMAR_PQH_REG
) & PRQ_RING_MASK
;
561 while (head
!= tail
) {
562 struct intel_svm_dev
*sdev
;
563 struct vm_area_struct
*vma
;
564 struct page_req_dsc
*req
;
571 req
= &iommu
->prq
[head
/ sizeof(*req
)];
573 result
= QI_RESP_FAILURE
;
574 address
= (u64
)req
->addr
<< VTD_PAGE_SHIFT
;
575 if (!req
->pasid_present
) {
576 pr_err("%s: Page request without PASID: %08llx %08llx\n",
577 iommu
->name
, ((unsigned long long *)req
)[0],
578 ((unsigned long long *)req
)[1]);
582 if (!svm
|| svm
->pasid
!= req
->pasid
) {
584 svm
= idr_find(&iommu
->pasid_idr
, req
->pasid
);
585 /* It *can't* go away, because the driver is not permitted
586 * to unbind the mm while any page faults are outstanding.
587 * So we only need RCU to protect the internal idr code. */
591 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
592 iommu
->name
, req
->pasid
, ((unsigned long long *)req
)[0],
593 ((unsigned long long *)req
)[1]);
598 result
= QI_RESP_INVALID
;
599 /* Since we're using init_mm.pgd directly, we should never take
600 * any faults on kernel addresses. */
603 /* If the mm is already defunct, don't handle faults. */
604 if (!mmget_not_zero(svm
->mm
))
606 down_read(&svm
->mm
->mmap_sem
);
607 vma
= find_extend_vma(svm
->mm
, address
);
608 if (!vma
|| address
< vma
->vm_start
)
611 if (access_error(vma
, req
))
614 ret
= handle_mm_fault(vma
, address
,
615 req
->wr_req
? FAULT_FLAG_WRITE
: 0);
616 if (ret
& VM_FAULT_ERROR
)
619 result
= QI_RESP_SUCCESS
;
621 up_read(&svm
->mm
->mmap_sem
);
624 /* Accounting for major/minor faults? */
626 list_for_each_entry_rcu(sdev
, &svm
->devs
, list
) {
627 if (sdev
->sid
== PCI_DEVID(req
->bus
, req
->devfn
))
630 /* Other devices can go away, but the drivers are not permitted
631 * to unbind while any page faults might be in flight. So it's
632 * OK to drop the 'lock' here now we have it. */
635 if (WARN_ON(&sdev
->list
== &svm
->devs
))
638 if (sdev
&& sdev
->ops
&& sdev
->ops
->fault_cb
) {
639 int rwxp
= (req
->rd_req
<< 3) | (req
->wr_req
<< 2) |
640 (req
->exe_req
<< 1) | (req
->priv_req
);
641 sdev
->ops
->fault_cb(sdev
->dev
, req
->pasid
, req
->addr
, req
->private, rwxp
, result
);
643 /* We get here in the error case where the PASID lookup failed,
644 and these can be NULL. Do not use them below this point! */
649 /* Page Group Response */
650 resp
.low
= QI_PGRP_PASID(req
->pasid
) |
651 QI_PGRP_DID((req
->bus
<< 8) | req
->devfn
) |
652 QI_PGRP_PASID_P(req
->pasid_present
) |
654 resp
.high
= QI_PGRP_IDX(req
->prg_index
) |
655 QI_PGRP_PRIV(req
->private) | QI_PGRP_RESP_CODE(result
);
657 qi_submit_sync(&resp
, iommu
);
658 } else if (req
->srr
) {
659 /* Page Stream Response */
660 resp
.low
= QI_PSTRM_IDX(req
->prg_index
) |
661 QI_PSTRM_PRIV(req
->private) | QI_PSTRM_BUS(req
->bus
) |
662 QI_PSTRM_PASID(req
->pasid
) | QI_PSTRM_RESP_TYPE
;
663 resp
.high
= QI_PSTRM_ADDR(address
) | QI_PSTRM_DEVFN(req
->devfn
) |
664 QI_PSTRM_RESP_CODE(result
);
666 qi_submit_sync(&resp
, iommu
);
669 head
= (head
+ sizeof(*req
)) & PRQ_RING_MASK
;
672 dmar_writeq(iommu
->reg
+ DMAR_PQH_REG
, tail
);
674 return IRQ_RETVAL(handled
);