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>
29 static irqreturn_t prq_event_thread(int irq, void *d);
35 struct pasid_state_entry {
39 int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
44 /* Start at 2 because it's defined as 2^(1+PSS) */
45 iommu->pasid_max = 2 << ecap_pss(iommu->ecap);
47 /* Eventually I'm promised we will get a multi-level PASID table
48 * and it won't have to be physically contiguous. Until then,
49 * limit the size because 8MiB contiguous allocations can be hard
50 * to come by. The limit of 0x20000, which is 1MiB for each of
51 * the PASID and PASID-state tables, is somewhat arbitrary. */
52 if (iommu->pasid_max > 0x20000)
53 iommu->pasid_max = 0x20000;
55 order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);
56 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
58 pr_warn("IOMMU: %s: Failed to allocate PASID table\n",
62 iommu->pasid_table = page_address(pages);
63 pr_info("%s: Allocated order %d PASID table.\n", iommu->name, order);
65 if (ecap_dis(iommu->ecap)) {
66 /* Just making it explicit... */
67 BUILD_BUG_ON(sizeof(struct pasid_entry) != sizeof(struct pasid_state_entry));
68 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
70 iommu->pasid_state_table = page_address(pages);
72 pr_warn("IOMMU: %s: Failed to allocate PASID state table\n",
76 idr_init(&iommu->pasid_idr);
81 int intel_svm_free_pasid_tables(struct intel_iommu *iommu)
83 int order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);
85 if (iommu->pasid_table) {
86 free_pages((unsigned long)iommu->pasid_table, order);
87 iommu->pasid_table = NULL;
89 if (iommu->pasid_state_table) {
90 free_pages((unsigned long)iommu->pasid_state_table, order);
91 iommu->pasid_state_table = NULL;
93 idr_destroy(&iommu->pasid_idr);
99 int intel_svm_enable_prq(struct intel_iommu *iommu)
104 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
106 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
110 iommu->prq = page_address(pages);
112 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
114 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
118 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
124 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
126 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
127 iommu->prq_name, iommu);
129 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
131 dmar_free_hwirq(irq);
134 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
135 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
136 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
141 int intel_svm_finish_prq(struct intel_iommu *iommu)
143 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
144 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
145 dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
147 free_irq(iommu->pr_irq, iommu);
148 dmar_free_hwirq(iommu->pr_irq);
151 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
157 static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
158 unsigned long address, unsigned long pages, int ih, int gl)
163 /* For global kernel pages we have to flush them in *all* PASIDs
164 * because that's the only option the hardware gives us. Despite
165 * the fact that they are actually only accessible through one. */
167 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
168 QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) | QI_EIOTLB_TYPE;
170 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
171 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;
174 int mask = ilog2(__roundup_pow_of_two(pages));
176 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
177 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | QI_EIOTLB_TYPE;
178 desc.high = QI_EIOTLB_ADDR(address) | QI_EIOTLB_GL(gl) |
179 QI_EIOTLB_IH(ih) | QI_EIOTLB_AM(mask);
181 qi_submit_sync(&desc, svm->iommu);
183 if (sdev->dev_iotlb) {
184 desc.low = QI_DEV_EIOTLB_PASID(svm->pasid) | QI_DEV_EIOTLB_SID(sdev->sid) |
185 QI_DEV_EIOTLB_QDEP(sdev->qdep) | QI_DEIOTLB_TYPE;
187 desc.high = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | QI_DEV_EIOTLB_SIZE;
188 } else if (pages > 1) {
189 /* The least significant zero bit indicates the size. So,
190 * for example, an "address" value of 0x12345f000 will
191 * flush from 0x123440000 to 0x12347ffff (256KiB). */
192 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
193 unsigned long mask = __rounddown_pow_of_two(address ^ last);;
195 desc.high = QI_DEV_EIOTLB_ADDR((address & ~mask) | (mask - 1)) | QI_DEV_EIOTLB_SIZE;
197 desc.high = QI_DEV_EIOTLB_ADDR(address);
199 qi_submit_sync(&desc, svm->iommu);
203 static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
204 unsigned long pages, int ih, int gl)
206 struct intel_svm_dev *sdev;
208 /* Try deferred invalidate if available */
209 if (svm->iommu->pasid_state_table &&
210 !cmpxchg64(&svm->iommu->pasid_state_table[svm->pasid].val, 0, 1ULL << 63))
214 list_for_each_entry_rcu(sdev, &svm->devs, list)
215 intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl);
219 static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm,
220 unsigned long address, pte_t pte)
222 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
224 intel_flush_svm_range(svm, address, 1, 1, 0);
227 /* Pages have been freed at this point */
228 static void intel_invalidate_range(struct mmu_notifier *mn,
229 struct mm_struct *mm,
230 unsigned long start, unsigned long end)
232 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
234 intel_flush_svm_range(svm, start,
235 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0);
239 static void intel_flush_pasid_dev(struct intel_svm *svm, struct intel_svm_dev *sdev, int pasid)
244 desc.low = QI_PC_TYPE | QI_PC_DID(sdev->did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid);
246 qi_submit_sync(&desc, svm->iommu);
249 static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
251 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
252 struct intel_svm_dev *sdev;
254 /* This might end up being called from exit_mmap(), *before* the page
255 * tables are cleared. And __mmu_notifier_release() will delete us from
256 * the list of notifiers so that our invalidate_range() callback doesn't
257 * get called when the page tables are cleared. So we need to protect
258 * against hardware accessing those page tables.
260 * We do it by clearing the entry in the PASID table and then flushing
261 * the IOTLB and the PASID table caches. This might upset hardware;
262 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
263 * page) so that we end up taking a fault that the hardware really
264 * *has* to handle gracefully without affecting other processes.
266 svm->iommu->pasid_table[svm->pasid].val = 0;
270 list_for_each_entry_rcu(sdev, &svm->devs, list) {
271 intel_flush_pasid_dev(svm, sdev, svm->pasid);
272 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
278 static const struct mmu_notifier_ops intel_mmuops = {
279 .release = intel_mm_release,
280 .change_pte = intel_change_pte,
281 .invalidate_range = intel_invalidate_range,
284 static DEFINE_MUTEX(pasid_mutex);
286 int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
288 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
289 struct intel_svm_dev *sdev;
290 struct intel_svm *svm = NULL;
291 struct mm_struct *mm = NULL;
298 if (dev_is_pci(dev)) {
299 pasid_max = pci_max_pasids(to_pci_dev(dev));
305 if ((flags & SVM_FLAG_SUPERVISOR_MODE)) {
306 if (!ecap_srs(iommu->ecap))
309 mm = get_task_mm(current);
313 mutex_lock(&pasid_mutex);
314 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
317 idr_for_each_entry(&iommu->pasid_idr, svm, i) {
319 (svm->flags & SVM_FLAG_PRIVATE_PASID))
322 if (svm->pasid >= pasid_max) {
324 "Limited PASID width. Cannot use existing PASID %d\n",
330 list_for_each_entry(sdev, &svm->devs, list) {
331 if (dev == sdev->dev) {
332 if (sdev->ops != ops) {
345 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
352 ret = intel_iommu_enable_pasid(iommu, sdev);
354 /* If they don't actually want to assign a PASID, this is
355 * just an enabling check/preparation. */
359 /* Finish the setup now we know we're keeping it */
362 init_rcu_head(&sdev->rcu);
365 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
373 if (pasid_max > iommu->pasid_max)
374 pasid_max = iommu->pasid_max;
376 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
377 ret = idr_alloc(&iommu->pasid_idr, svm,
378 !!cap_caching_mode(iommu->cap),
379 pasid_max - 1, GFP_KERNEL);
385 svm->notifier.ops = &intel_mmuops;
388 INIT_LIST_HEAD_RCU(&svm->devs);
391 ret = mmu_notifier_register(&svm->notifier, mm);
393 idr_remove(&svm->iommu->pasid_idr, svm->pasid);
398 iommu->pasid_table[svm->pasid].val = (u64)__pa(mm->pgd) | 1;
400 iommu->pasid_table[svm->pasid].val = (u64)__pa(init_mm.pgd) | 1 | (1ULL << 11);
402 /* In caching mode, we still have to flush with PASID 0 when
403 * a PASID table entry becomes present. Not entirely clear
404 * *why* that would be the case — surely we could just issue
405 * a flush with the PASID value that we've changed? The PASID
406 * is the index into the table, after all. It's not like domain
407 * IDs in the case of the equivalent context-entry change in
408 * caching mode. And for that matter it's not entirely clear why
409 * a VMM would be in the business of caching the PASID table
410 * anyway. Surely that can be left entirely to the guest? */
411 if (cap_caching_mode(iommu->cap))
412 intel_flush_pasid_dev(svm, sdev, 0);
414 list_add_rcu(&sdev->list, &svm->devs);
420 mutex_unlock(&pasid_mutex);
425 EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
427 int intel_svm_unbind_mm(struct device *dev, int pasid)
429 struct intel_svm_dev *sdev;
430 struct intel_iommu *iommu;
431 struct intel_svm *svm;
434 mutex_lock(&pasid_mutex);
435 iommu = intel_svm_device_to_iommu(dev);
436 if (!iommu || !iommu->pasid_table)
439 svm = idr_find(&iommu->pasid_idr, pasid);
443 list_for_each_entry(sdev, &svm->devs, list) {
444 if (dev == sdev->dev) {
448 list_del_rcu(&sdev->list);
449 /* Flush the PASID cache and IOTLB for this device.
450 * Note that we do depend on the hardware *not* using
451 * the PASID any more. Just as we depend on other
452 * devices never using PASIDs that they have no right
453 * to use. We have a *shared* PASID table, because it's
454 * large and has to be physically contiguous. So it's
455 * hard to be as defensive as we might like. */
456 intel_flush_pasid_dev(svm, sdev, svm->pasid);
457 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
458 kfree_rcu(sdev, rcu);
460 if (list_empty(&svm->devs)) {
462 idr_remove(&svm->iommu->pasid_idr, svm->pasid);
464 mmu_notifier_unregister(&svm->notifier, svm->mm);
466 /* We mandate that no page faults may be outstanding
467 * for the PASID when intel_svm_unbind_mm() is called.
468 * If that is not obeyed, subtle errors will happen.
469 * Let's make them less subtle... */
470 memset(svm, 0x6b, sizeof(*svm));
478 mutex_unlock(&pasid_mutex);
482 EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
484 int intel_svm_is_pasid_valid(struct device *dev, int pasid)
486 struct intel_iommu *iommu;
487 struct intel_svm *svm;
490 mutex_lock(&pasid_mutex);
491 iommu = intel_svm_device_to_iommu(dev);
492 if (!iommu || !iommu->pasid_table)
495 svm = idr_find(&iommu->pasid_idr, pasid);
499 /* init_mm is used in this case */
502 else if (atomic_read(&svm->mm->mm_users) > 0)
508 mutex_unlock(&pasid_mutex);
512 EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid);
514 /* Page request queue descriptor */
515 struct page_req_dsc {
532 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
534 static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
536 unsigned long requested = 0;
539 requested |= VM_EXEC;
542 requested |= VM_READ;
545 requested |= VM_WRITE;
547 return (requested & ~vma->vm_flags) != 0;
550 static bool is_canonical_address(u64 addr)
552 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
553 long saddr = (long) addr;
555 return (((saddr << shift) >> shift) == saddr);
558 static irqreturn_t prq_event_thread(int irq, void *d)
560 struct intel_iommu *iommu = d;
561 struct intel_svm *svm = NULL;
562 int head, tail, handled = 0;
564 /* Clear PPR bit before reading head/tail registers, to
565 * ensure that we get a new interrupt if needed. */
566 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
568 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
569 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
570 while (head != tail) {
571 struct intel_svm_dev *sdev;
572 struct vm_area_struct *vma;
573 struct page_req_dsc *req;
580 req = &iommu->prq[head / sizeof(*req)];
582 result = QI_RESP_FAILURE;
583 address = (u64)req->addr << VTD_PAGE_SHIFT;
584 if (!req->pasid_present) {
585 pr_err("%s: Page request without PASID: %08llx %08llx\n",
586 iommu->name, ((unsigned long long *)req)[0],
587 ((unsigned long long *)req)[1]);
591 if (!svm || svm->pasid != req->pasid) {
593 svm = idr_find(&iommu->pasid_idr, req->pasid);
594 /* It *can't* go away, because the driver is not permitted
595 * to unbind the mm while any page faults are outstanding.
596 * So we only need RCU to protect the internal idr code. */
600 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
601 iommu->name, req->pasid, ((unsigned long long *)req)[0],
602 ((unsigned long long *)req)[1]);
607 result = QI_RESP_INVALID;
608 /* Since we're using init_mm.pgd directly, we should never take
609 * any faults on kernel addresses. */
612 /* If the mm is already defunct, don't handle faults. */
613 if (!mmget_not_zero(svm->mm))
616 /* If address is not canonical, return invalid response */
617 if (!is_canonical_address(address))
620 down_read(&svm->mm->mmap_sem);
621 vma = find_extend_vma(svm->mm, address);
622 if (!vma || address < vma->vm_start)
625 if (access_error(vma, req))
628 ret = handle_mm_fault(vma, address,
629 req->wr_req ? FAULT_FLAG_WRITE : 0);
630 if (ret & VM_FAULT_ERROR)
633 result = QI_RESP_SUCCESS;
635 up_read(&svm->mm->mmap_sem);
638 /* Accounting for major/minor faults? */
640 list_for_each_entry_rcu(sdev, &svm->devs, list) {
641 if (sdev->sid == PCI_DEVID(req->bus, req->devfn))
644 /* Other devices can go away, but the drivers are not permitted
645 * to unbind while any page faults might be in flight. So it's
646 * OK to drop the 'lock' here now we have it. */
649 if (WARN_ON(&sdev->list == &svm->devs))
652 if (sdev && sdev->ops && sdev->ops->fault_cb) {
653 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
654 (req->exe_req << 1) | (req->priv_req);
655 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, req->private, rwxp, result);
657 /* We get here in the error case where the PASID lookup failed,
658 and these can be NULL. Do not use them below this point! */
663 /* Page Group Response */
664 resp.low = QI_PGRP_PASID(req->pasid) |
665 QI_PGRP_DID((req->bus << 8) | req->devfn) |
666 QI_PGRP_PASID_P(req->pasid_present) |
668 resp.high = QI_PGRP_IDX(req->prg_index) |
669 QI_PGRP_PRIV(req->private) | QI_PGRP_RESP_CODE(result);
671 qi_submit_sync(&resp, iommu);
672 } else if (req->srr) {
673 /* Page Stream Response */
674 resp.low = QI_PSTRM_IDX(req->prg_index) |
675 QI_PSTRM_PRIV(req->private) | QI_PSTRM_BUS(req->bus) |
676 QI_PSTRM_PASID(req->pasid) | QI_PSTRM_RESP_TYPE;
677 resp.high = QI_PSTRM_ADDR(address) | QI_PSTRM_DEVFN(req->devfn) |
678 QI_PSTRM_RESP_CODE(result);
680 qi_submit_sync(&resp, iommu);
683 head = (head + sizeof(*req)) & PRQ_RING_MASK;
686 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
688 return IRQ_RETVAL(handled);