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 __flush_svm_range_dev(struct intel_svm *svm,
103 struct intel_svm_dev *sdev,
104 unsigned long address,
105 unsigned long pages, int ih)
110 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
111 QI_EIOTLB_DID(sdev->did) |
112 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
116 int mask = ilog2(__roundup_pow_of_two(pages));
118 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
119 QI_EIOTLB_DID(sdev->did) |
120 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |
122 desc.qw1 = QI_EIOTLB_ADDR(address) |
128 qi_submit_sync(&desc, svm->iommu);
130 if (sdev->dev_iotlb) {
131 desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
132 QI_DEV_EIOTLB_SID(sdev->sid) |
133 QI_DEV_EIOTLB_QDEP(sdev->qdep) |
136 desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
138 } else if (pages > 1) {
139 /* The least significant zero bit indicates the size. So,
140 * for example, an "address" value of 0x12345f000 will
141 * flush from 0x123440000 to 0x12347ffff (256KiB). */
142 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
143 unsigned long mask = __rounddown_pow_of_two(address ^ last);
145 desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |
146 (mask - 1)) | QI_DEV_EIOTLB_SIZE;
148 desc.qw1 = QI_DEV_EIOTLB_ADDR(address);
152 qi_submit_sync(&desc, svm->iommu);
156 static void intel_flush_svm_range_dev(struct intel_svm *svm,
157 struct intel_svm_dev *sdev,
158 unsigned long address,
159 unsigned long pages, int ih)
161 unsigned long shift = ilog2(__roundup_pow_of_two(pages));
162 unsigned long align = (1ULL << (VTD_PAGE_SHIFT + shift));
163 unsigned long start = ALIGN_DOWN(address, align);
164 unsigned long end = ALIGN(address + (pages << VTD_PAGE_SHIFT), align);
166 while (start < end) {
167 __flush_svm_range_dev(svm, sdev, start, align >> VTD_PAGE_SHIFT, ih);
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, svm->pasid);
214 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0);
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 int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
230 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
231 struct device_domain_info *info;
232 struct intel_svm_dev *sdev;
233 struct intel_svm *svm = NULL;
234 struct mm_struct *mm = NULL;
238 if (!iommu || dmar_disabled)
241 if (dev_is_pci(dev)) {
242 pasid_max = pci_max_pasids(to_pci_dev(dev));
248 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
249 if (!ecap_srs(iommu->ecap))
252 mm = get_task_mm(current);
256 mutex_lock(&pasid_mutex);
257 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
260 list_for_each_entry(t, &global_svm_list, list) {
261 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
265 if (svm->pasid >= pasid_max) {
267 "Limited PASID width. Cannot use existing PASID %d\n",
273 list_for_each_entry(sdev, &svm->devs, list) {
274 if (dev == sdev->dev) {
275 if (sdev->ops != ops) {
288 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
295 ret = intel_iommu_enable_pasid(iommu, dev);
297 /* If they don't actually want to assign a PASID, this is
298 * just an enabling check/preparation. */
303 info = dev->archdata.iommu;
304 if (!info || !info->pasid_supported) {
309 sdev->did = FLPT_DEFAULT_DID;
310 sdev->sid = PCI_DEVID(info->bus, info->devfn);
311 if (info->ats_enabled) {
313 sdev->qdep = info->ats_qdep;
314 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
318 /* Finish the setup now we know we're keeping it */
321 init_rcu_head(&sdev->rcu);
324 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
332 if (pasid_max > intel_pasid_max_id)
333 pasid_max = intel_pasid_max_id;
335 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
336 ret = intel_pasid_alloc_id(svm,
337 !!cap_caching_mode(iommu->cap),
338 pasid_max, GFP_KERNEL);
345 svm->notifier.ops = &intel_mmuops;
348 INIT_LIST_HEAD_RCU(&svm->devs);
349 INIT_LIST_HEAD(&svm->list);
352 ret = mmu_notifier_register(&svm->notifier, mm);
354 intel_pasid_free_id(svm->pasid);
361 spin_lock(&iommu->lock);
362 ret = intel_pasid_setup_first_level(iommu, dev,
363 mm ? mm->pgd : init_mm.pgd,
364 svm->pasid, FLPT_DEFAULT_DID,
365 mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);
366 spin_unlock(&iommu->lock);
369 mmu_notifier_unregister(&svm->notifier, mm);
370 intel_pasid_free_id(svm->pasid);
376 list_add_tail(&svm->list, &global_svm_list);
379 * Binding a new device with existing PASID, need to setup
382 spin_lock(&iommu->lock);
383 ret = intel_pasid_setup_first_level(iommu, dev,
384 mm ? mm->pgd : init_mm.pgd,
385 svm->pasid, FLPT_DEFAULT_DID,
386 mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);
387 spin_unlock(&iommu->lock);
393 list_add_rcu(&sdev->list, &svm->devs);
399 mutex_unlock(&pasid_mutex);
404 EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
406 int intel_svm_unbind_mm(struct device *dev, int pasid)
408 struct intel_svm_dev *sdev;
409 struct intel_iommu *iommu;
410 struct intel_svm *svm;
413 mutex_lock(&pasid_mutex);
414 iommu = intel_svm_device_to_iommu(dev);
418 svm = intel_pasid_lookup_id(pasid);
422 list_for_each_entry(sdev, &svm->devs, list) {
423 if (dev == sdev->dev) {
427 list_del_rcu(&sdev->list);
428 /* Flush the PASID cache and IOTLB for this device.
429 * Note that we do depend on the hardware *not* using
430 * the PASID any more. Just as we depend on other
431 * devices never using PASIDs that they have no right
432 * to use. We have a *shared* PASID table, because it's
433 * large and has to be physically contiguous. So it's
434 * hard to be as defensive as we might like. */
435 intel_pasid_tear_down_entry(iommu, dev, svm->pasid);
436 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0);
437 kfree_rcu(sdev, rcu);
439 if (list_empty(&svm->devs)) {
440 intel_pasid_free_id(svm->pasid);
442 mmu_notifier_unregister(&svm->notifier, svm->mm);
444 list_del(&svm->list);
446 /* We mandate that no page faults may be outstanding
447 * for the PASID when intel_svm_unbind_mm() is called.
448 * If that is not obeyed, subtle errors will happen.
449 * Let's make them less subtle... */
450 memset(svm, 0x6b, sizeof(*svm));
458 mutex_unlock(&pasid_mutex);
462 EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
464 int intel_svm_is_pasid_valid(struct device *dev, int pasid)
466 struct intel_iommu *iommu;
467 struct intel_svm *svm;
470 mutex_lock(&pasid_mutex);
471 iommu = intel_svm_device_to_iommu(dev);
475 svm = intel_pasid_lookup_id(pasid);
479 /* init_mm is used in this case */
482 else if (atomic_read(&svm->mm->mm_users) > 0)
488 mutex_unlock(&pasid_mutex);
492 EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid);
494 /* Page request queue descriptor */
495 struct page_req_dsc {
500 u64 priv_data_present:1;
523 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20)
525 static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
527 unsigned long requested = 0;
530 requested |= VM_EXEC;
533 requested |= VM_READ;
536 requested |= VM_WRITE;
538 return (requested & ~vma->vm_flags) != 0;
541 static bool is_canonical_address(u64 addr)
543 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
544 long saddr = (long) addr;
546 return (((saddr << shift) >> shift) == saddr);
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;
572 req = &iommu->prq[head / sizeof(*req)];
574 result = QI_RESP_FAILURE;
575 address = (u64)req->addr << VTD_PAGE_SHIFT;
576 if (!req->pasid_present) {
577 pr_err("%s: Page request without PASID: %08llx %08llx\n",
578 iommu->name, ((unsigned long long *)req)[0],
579 ((unsigned long long *)req)[1]);
583 if (!svm || svm->pasid != req->pasid) {
585 svm = intel_pasid_lookup_id(req->pasid);
586 /* It *can't* go away, because the driver is not permitted
587 * to unbind the mm while any page faults are outstanding.
588 * So we only need RCU to protect the internal idr code. */
592 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
593 iommu->name, req->pasid, ((unsigned long long *)req)[0],
594 ((unsigned long long *)req)[1]);
599 result = QI_RESP_INVALID;
600 /* Since we're using init_mm.pgd directly, we should never take
601 * any faults on kernel addresses. */
605 /* If address is not canonical, return invalid response */
606 if (!is_canonical_address(address))
609 /* If the mm is already defunct, don't handle faults. */
610 if (!mmget_not_zero(svm->mm))
613 down_read(&svm->mm->mmap_sem);
614 vma = find_extend_vma(svm->mm, address);
615 if (!vma || address < vma->vm_start)
618 if (access_error(vma, req))
621 ret = handle_mm_fault(vma, address,
622 req->wr_req ? FAULT_FLAG_WRITE : 0);
623 if (ret & VM_FAULT_ERROR)
626 result = QI_RESP_SUCCESS;
628 up_read(&svm->mm->mmap_sem);
631 /* Accounting for major/minor faults? */
633 list_for_each_entry_rcu(sdev, &svm->devs, list) {
634 if (sdev->sid == req->rid)
637 /* Other devices can go away, but the drivers are not permitted
638 * to unbind while any page faults might be in flight. So it's
639 * OK to drop the 'lock' here now we have it. */
642 if (WARN_ON(&sdev->list == &svm->devs))
645 if (sdev && sdev->ops && sdev->ops->fault_cb) {
646 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
647 (req->exe_req << 1) | (req->pm_req);
648 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
649 req->priv_data, rwxp, result);
651 /* We get here in the error case where the PASID lookup failed,
652 and these can be NULL. Do not use them below this point! */
656 if (req->lpig || req->priv_data_present) {
658 * Per VT-d spec. v3.0 ch7.7, system software must
659 * respond with page group response if private data
660 * is present (PDP) or last page in group (LPIG) bit
661 * is set. This is an additional VT-d feature beyond
664 resp.qw0 = QI_PGRP_PASID(req->pasid) |
665 QI_PGRP_DID(req->rid) |
666 QI_PGRP_PASID_P(req->pasid_present) |
667 QI_PGRP_PDP(req->priv_data_present) |
668 QI_PGRP_RESP_CODE(result) |
670 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
671 QI_PGRP_LPIG(req->lpig);
673 if (req->priv_data_present)
674 memcpy(&resp.qw2, req->priv_data,
675 sizeof(req->priv_data));
678 qi_submit_sync(&resp, iommu);
680 head = (head + sizeof(*req)) & PRQ_RING_MASK;
683 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
685 return IRQ_RETVAL(handled);