Linux 5.1.15
[linux/fpc-iii.git] / drivers / iommu / intel-svm.c
blob3a4b09ae8561d965a6ae709b766bd4fe5862c852
1 /*
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
11 * more details.
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>
27 #include <linux/mm_types.h>
28 #include <asm/page.h>
30 #include "intel-pasid.h"
32 static irqreturn_t prq_event_thread(int irq, void *d);
34 int intel_svm_init(struct intel_iommu *iommu)
36 if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
37 !cap_fl1gp_support(iommu->cap))
38 return -EINVAL;
40 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
41 !cap_5lp_support(iommu->cap))
42 return -EINVAL;
44 return 0;
47 #define PRQ_ORDER 0
49 int intel_svm_enable_prq(struct intel_iommu *iommu)
51 struct page *pages;
52 int irq, ret;
54 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
55 if (!pages) {
56 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
57 iommu->name);
58 return -ENOMEM;
60 iommu->prq = page_address(pages);
62 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
63 if (irq <= 0) {
64 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
65 iommu->name);
66 ret = -EINVAL;
67 err:
68 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
69 iommu->prq = NULL;
70 return ret;
72 iommu->pr_irq = irq;
74 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
76 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
77 iommu->prq_name, iommu);
78 if (ret) {
79 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
80 iommu->name);
81 dmar_free_hwirq(irq);
82 iommu->pr_irq = 0;
83 goto err;
85 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
86 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
87 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
89 return 0;
92 int intel_svm_finish_prq(struct intel_iommu *iommu)
94 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
95 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
96 dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
98 if (iommu->pr_irq) {
99 free_irq(iommu->pr_irq, iommu);
100 dmar_free_hwirq(iommu->pr_irq);
101 iommu->pr_irq = 0;
104 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
105 iommu->prq = NULL;
107 return 0;
110 static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
111 unsigned long address, unsigned long pages, int ih, int gl)
113 struct qi_desc desc;
115 if (pages == -1) {
116 /* For global kernel pages we have to flush them in *all* PASIDs
117 * because that's the only option the hardware gives us. Despite
118 * the fact that they are actually only accessible through one. */
119 if (gl)
120 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
121 QI_EIOTLB_DID(sdev->did) |
122 QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) |
123 QI_EIOTLB_TYPE;
124 else
125 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
126 QI_EIOTLB_DID(sdev->did) |
127 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
128 QI_EIOTLB_TYPE;
129 desc.qw1 = 0;
130 } else {
131 int mask = ilog2(__roundup_pow_of_two(pages));
133 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
134 QI_EIOTLB_DID(sdev->did) |
135 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |
136 QI_EIOTLB_TYPE;
137 desc.qw1 = QI_EIOTLB_ADDR(address) |
138 QI_EIOTLB_GL(gl) |
139 QI_EIOTLB_IH(ih) |
140 QI_EIOTLB_AM(mask);
142 desc.qw2 = 0;
143 desc.qw3 = 0;
144 qi_submit_sync(&desc, svm->iommu);
146 if (sdev->dev_iotlb) {
147 desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
148 QI_DEV_EIOTLB_SID(sdev->sid) |
149 QI_DEV_EIOTLB_QDEP(sdev->qdep) |
150 QI_DEIOTLB_TYPE;
151 if (pages == -1) {
152 desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
153 QI_DEV_EIOTLB_SIZE;
154 } else if (pages > 1) {
155 /* The least significant zero bit indicates the size. So,
156 * for example, an "address" value of 0x12345f000 will
157 * flush from 0x123440000 to 0x12347ffff (256KiB). */
158 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
159 unsigned long mask = __rounddown_pow_of_two(address ^ last);
161 desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |
162 (mask - 1)) | QI_DEV_EIOTLB_SIZE;
163 } else {
164 desc.qw1 = QI_DEV_EIOTLB_ADDR(address);
166 desc.qw2 = 0;
167 desc.qw3 = 0;
168 qi_submit_sync(&desc, svm->iommu);
172 static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
173 unsigned long pages, int ih, int gl)
175 struct intel_svm_dev *sdev;
177 rcu_read_lock();
178 list_for_each_entry_rcu(sdev, &svm->devs, list)
179 intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl);
180 rcu_read_unlock();
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, 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.
211 rcu_read_lock();
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, !svm->mm);
216 rcu_read_unlock();
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 intel_svm_dev *sdev;
232 struct intel_svm *svm = NULL;
233 struct mm_struct *mm = NULL;
234 int pasid_max;
235 int ret;
237 if (!iommu || dmar_disabled)
238 return -EINVAL;
240 if (dev_is_pci(dev)) {
241 pasid_max = pci_max_pasids(to_pci_dev(dev));
242 if (pasid_max < 0)
243 return -EINVAL;
244 } else
245 pasid_max = 1 << 20;
247 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
248 if (!ecap_srs(iommu->ecap))
249 return -EINVAL;
250 } else if (pasid) {
251 mm = get_task_mm(current);
252 BUG_ON(!mm);
255 mutex_lock(&pasid_mutex);
256 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
257 struct intel_svm *t;
259 list_for_each_entry(t, &global_svm_list, list) {
260 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
261 continue;
263 svm = t;
264 if (svm->pasid >= pasid_max) {
265 dev_warn(dev,
266 "Limited PASID width. Cannot use existing PASID %d\n",
267 svm->pasid);
268 ret = -ENOSPC;
269 goto out;
272 list_for_each_entry(sdev, &svm->devs, list) {
273 if (dev == sdev->dev) {
274 if (sdev->ops != ops) {
275 ret = -EBUSY;
276 goto out;
278 sdev->users++;
279 goto success;
283 break;
287 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
288 if (!sdev) {
289 ret = -ENOMEM;
290 goto out;
292 sdev->dev = dev;
294 ret = intel_iommu_enable_pasid(iommu, sdev);
295 if (ret || !pasid) {
296 /* If they don't actually want to assign a PASID, this is
297 * just an enabling check/preparation. */
298 kfree(sdev);
299 goto out;
301 /* Finish the setup now we know we're keeping it */
302 sdev->users = 1;
303 sdev->ops = ops;
304 init_rcu_head(&sdev->rcu);
306 if (!svm) {
307 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
308 if (!svm) {
309 ret = -ENOMEM;
310 kfree(sdev);
311 goto out;
313 svm->iommu = iommu;
315 if (pasid_max > intel_pasid_max_id)
316 pasid_max = intel_pasid_max_id;
318 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
319 ret = intel_pasid_alloc_id(svm,
320 !!cap_caching_mode(iommu->cap),
321 pasid_max - 1, GFP_KERNEL);
322 if (ret < 0) {
323 kfree(svm);
324 kfree(sdev);
325 goto out;
327 svm->pasid = ret;
328 svm->notifier.ops = &intel_mmuops;
329 svm->mm = mm;
330 svm->flags = flags;
331 INIT_LIST_HEAD_RCU(&svm->devs);
332 INIT_LIST_HEAD(&svm->list);
333 ret = -ENOMEM;
334 if (mm) {
335 ret = mmu_notifier_register(&svm->notifier, mm);
336 if (ret) {
337 intel_pasid_free_id(svm->pasid);
338 kfree(svm);
339 kfree(sdev);
340 goto out;
344 spin_lock(&iommu->lock);
345 ret = intel_pasid_setup_first_level(iommu, dev,
346 mm ? mm->pgd : init_mm.pgd,
347 svm->pasid, FLPT_DEFAULT_DID,
348 mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);
349 spin_unlock(&iommu->lock);
350 if (ret) {
351 if (mm)
352 mmu_notifier_unregister(&svm->notifier, mm);
353 intel_pasid_free_id(svm->pasid);
354 kfree(svm);
355 kfree(sdev);
356 goto out;
359 list_add_tail(&svm->list, &global_svm_list);
361 list_add_rcu(&sdev->list, &svm->devs);
363 success:
364 *pasid = svm->pasid;
365 ret = 0;
366 out:
367 mutex_unlock(&pasid_mutex);
368 if (mm)
369 mmput(mm);
370 return ret;
372 EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
374 int intel_svm_unbind_mm(struct device *dev, int pasid)
376 struct intel_svm_dev *sdev;
377 struct intel_iommu *iommu;
378 struct intel_svm *svm;
379 int ret = -EINVAL;
381 mutex_lock(&pasid_mutex);
382 iommu = intel_svm_device_to_iommu(dev);
383 if (!iommu)
384 goto out;
386 svm = intel_pasid_lookup_id(pasid);
387 if (!svm)
388 goto out;
390 list_for_each_entry(sdev, &svm->devs, list) {
391 if (dev == sdev->dev) {
392 ret = 0;
393 sdev->users--;
394 if (!sdev->users) {
395 list_del_rcu(&sdev->list);
396 /* Flush the PASID cache and IOTLB for this device.
397 * Note that we do depend on the hardware *not* using
398 * the PASID any more. Just as we depend on other
399 * devices never using PASIDs that they have no right
400 * to use. We have a *shared* PASID table, because it's
401 * large and has to be physically contiguous. So it's
402 * hard to be as defensive as we might like. */
403 intel_pasid_tear_down_entry(iommu, dev, svm->pasid);
404 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
405 kfree_rcu(sdev, rcu);
407 if (list_empty(&svm->devs)) {
408 intel_pasid_free_id(svm->pasid);
409 if (svm->mm)
410 mmu_notifier_unregister(&svm->notifier, svm->mm);
412 list_del(&svm->list);
414 /* We mandate that no page faults may be outstanding
415 * for the PASID when intel_svm_unbind_mm() is called.
416 * If that is not obeyed, subtle errors will happen.
417 * Let's make them less subtle... */
418 memset(svm, 0x6b, sizeof(*svm));
419 kfree(svm);
422 break;
425 out:
426 mutex_unlock(&pasid_mutex);
428 return ret;
430 EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
432 int intel_svm_is_pasid_valid(struct device *dev, int pasid)
434 struct intel_iommu *iommu;
435 struct intel_svm *svm;
436 int ret = -EINVAL;
438 mutex_lock(&pasid_mutex);
439 iommu = intel_svm_device_to_iommu(dev);
440 if (!iommu)
441 goto out;
443 svm = intel_pasid_lookup_id(pasid);
444 if (!svm)
445 goto out;
447 /* init_mm is used in this case */
448 if (!svm->mm)
449 ret = 1;
450 else if (atomic_read(&svm->mm->mm_users) > 0)
451 ret = 1;
452 else
453 ret = 0;
455 out:
456 mutex_unlock(&pasid_mutex);
458 return ret;
460 EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid);
462 /* Page request queue descriptor */
463 struct page_req_dsc {
464 union {
465 struct {
466 u64 type:8;
467 u64 pasid_present:1;
468 u64 priv_data_present:1;
469 u64 rsvd:6;
470 u64 rid:16;
471 u64 pasid:20;
472 u64 exe_req:1;
473 u64 pm_req:1;
474 u64 rsvd2:10;
476 u64 qw_0;
478 union {
479 struct {
480 u64 rd_req:1;
481 u64 wr_req:1;
482 u64 lpig:1;
483 u64 prg_index:9;
484 u64 addr:52;
486 u64 qw_1;
488 u64 priv_data[2];
491 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
493 static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
495 unsigned long requested = 0;
497 if (req->exe_req)
498 requested |= VM_EXEC;
500 if (req->rd_req)
501 requested |= VM_READ;
503 if (req->wr_req)
504 requested |= VM_WRITE;
506 return (requested & ~vma->vm_flags) != 0;
509 static bool is_canonical_address(u64 addr)
511 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
512 long saddr = (long) addr;
514 return (((saddr << shift) >> shift) == saddr);
517 static irqreturn_t prq_event_thread(int irq, void *d)
519 struct intel_iommu *iommu = d;
520 struct intel_svm *svm = NULL;
521 int head, tail, handled = 0;
523 /* Clear PPR bit before reading head/tail registers, to
524 * ensure that we get a new interrupt if needed. */
525 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
527 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
528 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
529 while (head != tail) {
530 struct intel_svm_dev *sdev;
531 struct vm_area_struct *vma;
532 struct page_req_dsc *req;
533 struct qi_desc resp;
534 int result;
535 vm_fault_t ret;
536 u64 address;
538 handled = 1;
540 req = &iommu->prq[head / sizeof(*req)];
542 result = QI_RESP_FAILURE;
543 address = (u64)req->addr << VTD_PAGE_SHIFT;
544 if (!req->pasid_present) {
545 pr_err("%s: Page request without PASID: %08llx %08llx\n",
546 iommu->name, ((unsigned long long *)req)[0],
547 ((unsigned long long *)req)[1]);
548 goto no_pasid;
551 if (!svm || svm->pasid != req->pasid) {
552 rcu_read_lock();
553 svm = intel_pasid_lookup_id(req->pasid);
554 /* It *can't* go away, because the driver is not permitted
555 * to unbind the mm while any page faults are outstanding.
556 * So we only need RCU to protect the internal idr code. */
557 rcu_read_unlock();
559 if (!svm) {
560 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
561 iommu->name, req->pasid, ((unsigned long long *)req)[0],
562 ((unsigned long long *)req)[1]);
563 goto no_pasid;
567 result = QI_RESP_INVALID;
568 /* Since we're using init_mm.pgd directly, we should never take
569 * any faults on kernel addresses. */
570 if (!svm->mm)
571 goto bad_req;
572 /* If the mm is already defunct, don't handle faults. */
573 if (!mmget_not_zero(svm->mm))
574 goto bad_req;
576 /* If address is not canonical, return invalid response */
577 if (!is_canonical_address(address))
578 goto bad_req;
580 down_read(&svm->mm->mmap_sem);
581 vma = find_extend_vma(svm->mm, address);
582 if (!vma || address < vma->vm_start)
583 goto invalid;
585 if (access_error(vma, req))
586 goto invalid;
588 ret = handle_mm_fault(vma, address,
589 req->wr_req ? FAULT_FLAG_WRITE : 0);
590 if (ret & VM_FAULT_ERROR)
591 goto invalid;
593 result = QI_RESP_SUCCESS;
594 invalid:
595 up_read(&svm->mm->mmap_sem);
596 mmput(svm->mm);
597 bad_req:
598 /* Accounting for major/minor faults? */
599 rcu_read_lock();
600 list_for_each_entry_rcu(sdev, &svm->devs, list) {
601 if (sdev->sid == req->rid)
602 break;
604 /* Other devices can go away, but the drivers are not permitted
605 * to unbind while any page faults might be in flight. So it's
606 * OK to drop the 'lock' here now we have it. */
607 rcu_read_unlock();
609 if (WARN_ON(&sdev->list == &svm->devs))
610 sdev = NULL;
612 if (sdev && sdev->ops && sdev->ops->fault_cb) {
613 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
614 (req->exe_req << 1) | (req->pm_req);
615 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
616 req->priv_data, rwxp, result);
618 /* We get here in the error case where the PASID lookup failed,
619 and these can be NULL. Do not use them below this point! */
620 sdev = NULL;
621 svm = NULL;
622 no_pasid:
623 if (req->lpig || req->priv_data_present) {
625 * Per VT-d spec. v3.0 ch7.7, system software must
626 * respond with page group response if private data
627 * is present (PDP) or last page in group (LPIG) bit
628 * is set. This is an additional VT-d feature beyond
629 * PCI ATS spec.
631 resp.qw0 = QI_PGRP_PASID(req->pasid) |
632 QI_PGRP_DID(req->rid) |
633 QI_PGRP_PASID_P(req->pasid_present) |
634 QI_PGRP_PDP(req->pasid_present) |
635 QI_PGRP_RESP_CODE(result) |
636 QI_PGRP_RESP_TYPE;
637 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
638 QI_PGRP_LPIG(req->lpig);
640 if (req->priv_data_present)
641 memcpy(&resp.qw2, req->priv_data,
642 sizeof(req->priv_data));
644 resp.qw2 = 0;
645 resp.qw3 = 0;
646 qi_submit_sync(&resp, iommu);
648 head = (head + sizeof(*req)) & PRQ_RING_MASK;
651 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
653 return IRQ_RETVAL(handled);