blob: 6643277e321e22bb6cb40c8648b2026217d6569d [file] [log] [blame]
David Woodhouse8a94ade2015-03-24 14:54:56 +00001/*
2 * Copyright © 2015 Intel Corporation.
3 *
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.
7 *
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.
12 *
13 * Authors: David Woodhouse <dwmw2@infradead.org>
14 */
15
16#include <linux/intel-iommu.h>
David Woodhouse2f26e0a2015-09-09 11:40:47 +010017#include <linux/mmu_notifier.h>
18#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010019#include <linux/sched/mm.h>
David Woodhouse2f26e0a2015-09-09 11:40:47 +010020#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>
David Woodhousea222a7f2015-10-07 23:35:18 +010025#include <linux/dmar.h>
26#include <linux/interrupt.h>
Ashok Raj9d8c3af2017-08-08 13:29:27 -070027#include <asm/page.h>
David Woodhousea222a7f2015-10-07 23:35:18 +010028
29static irqreturn_t prq_event_thread(int irq, void *d);
David Woodhouse2f26e0a2015-09-09 11:40:47 +010030
31struct pasid_entry {
32 u64 val;
33};
David Woodhouse8a94ade2015-03-24 14:54:56 +000034
David Woodhouse907fea32015-10-13 14:11:13 +010035struct pasid_state_entry {
36 u64 val;
37};
38
David Woodhouse8a94ade2015-03-24 14:54:56 +000039int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
40{
41 struct page *pages;
42 int order;
43
David Woodhouse91017042016-09-12 10:49:11 +080044 /* Start at 2 because it's defined as 2^(1+PSS) */
45 iommu->pasid_max = 2 << ecap_pss(iommu->ecap);
David Woodhouse8a94ade2015-03-24 14:54:56 +000046
David Woodhouse91017042016-09-12 10:49:11 +080047 /* 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;
54
55 order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);
David Woodhouse8a94ade2015-03-24 14:54:56 +000056 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
57 if (!pages) {
58 pr_warn("IOMMU: %s: Failed to allocate PASID table\n",
59 iommu->name);
60 return -ENOMEM;
61 }
62 iommu->pasid_table = page_address(pages);
63 pr_info("%s: Allocated order %d PASID table.\n", iommu->name, order);
64
65 if (ecap_dis(iommu->ecap)) {
David Woodhouse91017042016-09-12 10:49:11 +080066 /* Just making it explicit... */
67 BUILD_BUG_ON(sizeof(struct pasid_entry) != sizeof(struct pasid_state_entry));
David Woodhouse8a94ade2015-03-24 14:54:56 +000068 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
69 if (pages)
70 iommu->pasid_state_table = page_address(pages);
71 else
72 pr_warn("IOMMU: %s: Failed to allocate PASID state table\n",
73 iommu->name);
74 }
75
David Woodhouse2f26e0a2015-09-09 11:40:47 +010076 idr_init(&iommu->pasid_idr);
77
David Woodhouse8a94ade2015-03-24 14:54:56 +000078 return 0;
79}
80
81int intel_svm_free_pasid_tables(struct intel_iommu *iommu)
82{
David Woodhouse91017042016-09-12 10:49:11 +080083 int order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);
David Woodhouse8a94ade2015-03-24 14:54:56 +000084
85 if (iommu->pasid_table) {
86 free_pages((unsigned long)iommu->pasid_table, order);
87 iommu->pasid_table = NULL;
88 }
89 if (iommu->pasid_state_table) {
90 free_pages((unsigned long)iommu->pasid_state_table, order);
91 iommu->pasid_state_table = NULL;
92 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +010093 idr_destroy(&iommu->pasid_idr);
David Woodhouse8a94ade2015-03-24 14:54:56 +000094 return 0;
95}
David Woodhouse2f26e0a2015-09-09 11:40:47 +010096
David Woodhousea222a7f2015-10-07 23:35:18 +010097#define PRQ_ORDER 0
98
99int intel_svm_enable_prq(struct intel_iommu *iommu)
100{
101 struct page *pages;
102 int irq, ret;
103
104 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
105 if (!pages) {
106 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
107 iommu->name);
108 return -ENOMEM;
109 }
110 iommu->prq = page_address(pages);
111
112 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
113 if (irq <= 0) {
114 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
115 iommu->name);
116 ret = -EINVAL;
117 err:
118 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
119 iommu->prq = NULL;
120 return ret;
121 }
122 iommu->pr_irq = irq;
123
124 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
125
126 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
127 iommu->prq_name, iommu);
128 if (ret) {
129 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
130 iommu->name);
131 dmar_free_hwirq(irq);
Jerry Snitselaar72d54812017-12-20 09:48:56 -0700132 iommu->pr_irq = 0;
David Woodhousea222a7f2015-10-07 23:35:18 +0100133 goto err;
134 }
135 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
136 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
137 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
138
139 return 0;
140}
141
142int intel_svm_finish_prq(struct intel_iommu *iommu)
143{
144 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
145 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
146 dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
147
Jerry Snitselaar72d54812017-12-20 09:48:56 -0700148 if (iommu->pr_irq) {
149 free_irq(iommu->pr_irq, iommu);
150 dmar_free_hwirq(iommu->pr_irq);
151 iommu->pr_irq = 0;
152 }
David Woodhousea222a7f2015-10-07 23:35:18 +0100153
154 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
155 iommu->prq = NULL;
156
157 return 0;
158}
159
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100160static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
David Woodhouse5d52f482015-10-20 15:52:13 +0100161 unsigned long address, unsigned long pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100162{
163 struct qi_desc desc;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100164
David Woodhouse5d52f482015-10-20 15:52:13 +0100165 if (pages == -1) {
David Woodhousee0349922015-10-16 19:36:53 +0100166 /* For global kernel pages we have to flush them in *all* PASIDs
167 * because that's the only option the hardware gives us. Despite
168 * the fact that they are actually only accessible through one. */
169 if (gl)
170 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
171 QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) | QI_EIOTLB_TYPE;
172 else
173 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
174 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100175 desc.high = 0;
176 } else {
David Woodhouse5d52f482015-10-20 15:52:13 +0100177 int mask = ilog2(__roundup_pow_of_two(pages));
178
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100179 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
180 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | QI_EIOTLB_TYPE;
David Woodhousee0349922015-10-16 19:36:53 +0100181 desc.high = QI_EIOTLB_ADDR(address) | QI_EIOTLB_GL(gl) |
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100182 QI_EIOTLB_IH(ih) | QI_EIOTLB_AM(mask);
183 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100184 qi_submit_sync(&desc, svm->iommu);
185
186 if (sdev->dev_iotlb) {
187 desc.low = QI_DEV_EIOTLB_PASID(svm->pasid) | QI_DEV_EIOTLB_SID(sdev->sid) |
188 QI_DEV_EIOTLB_QDEP(sdev->qdep) | QI_DEIOTLB_TYPE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100189 if (pages == -1) {
190 desc.high = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | QI_DEV_EIOTLB_SIZE;
191 } else if (pages > 1) {
192 /* The least significant zero bit indicates the size. So,
193 * for example, an "address" value of 0x12345f000 will
194 * flush from 0x123440000 to 0x12347ffff (256KiB). */
195 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
196 unsigned long mask = __rounddown_pow_of_two(address ^ last);;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100197
David Woodhouse5d52f482015-10-20 15:52:13 +0100198 desc.high = QI_DEV_EIOTLB_ADDR((address & ~mask) | (mask - 1)) | QI_DEV_EIOTLB_SIZE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100199 } else {
200 desc.high = QI_DEV_EIOTLB_ADDR(address);
201 }
202 qi_submit_sync(&desc, svm->iommu);
203 }
204}
205
206static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
David Woodhouse5d52f482015-10-20 15:52:13 +0100207 unsigned long pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100208{
209 struct intel_svm_dev *sdev;
210
David Woodhouse907fea32015-10-13 14:11:13 +0100211 /* Try deferred invalidate if available */
212 if (svm->iommu->pasid_state_table &&
213 !cmpxchg64(&svm->iommu->pasid_state_table[svm->pasid].val, 0, 1ULL << 63))
214 return;
215
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100216 rcu_read_lock();
217 list_for_each_entry_rcu(sdev, &svm->devs, list)
David Woodhousee0349922015-10-16 19:36:53 +0100218 intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100219 rcu_read_unlock();
220}
221
222static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm,
223 unsigned long address, pte_t pte)
224{
225 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
226
David Woodhousee0349922015-10-16 19:36:53 +0100227 intel_flush_svm_range(svm, address, 1, 1, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100228}
229
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100230/* Pages have been freed at this point */
231static void intel_invalidate_range(struct mmu_notifier *mn,
232 struct mm_struct *mm,
233 unsigned long start, unsigned long end)
234{
235 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
236
237 intel_flush_svm_range(svm, start,
David Woodhousee0349922015-10-16 19:36:53 +0100238 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100239}
240
241
David Woodhouse5a10ba22015-10-24 21:06:39 +0200242static void intel_flush_pasid_dev(struct intel_svm *svm, struct intel_svm_dev *sdev, int pasid)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100243{
244 struct qi_desc desc;
245
246 desc.high = 0;
David Woodhouse5a10ba22015-10-24 21:06:39 +0200247 desc.low = QI_PC_TYPE | QI_PC_DID(sdev->did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100248
249 qi_submit_sync(&desc, svm->iommu);
250}
251
252static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
253{
254 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
David Woodhousee57e58b2016-01-12 19:18:06 +0000255 struct intel_svm_dev *sdev;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100256
David Woodhousee57e58b2016-01-12 19:18:06 +0000257 /* This might end up being called from exit_mmap(), *before* the page
258 * tables are cleared. And __mmu_notifier_release() will delete us from
259 * the list of notifiers so that our invalidate_range() callback doesn't
260 * get called when the page tables are cleared. So we need to protect
261 * against hardware accessing those page tables.
262 *
263 * We do it by clearing the entry in the PASID table and then flushing
264 * the IOTLB and the PASID table caches. This might upset hardware;
265 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
266 * page) so that we end up taking a fault that the hardware really
267 * *has* to handle gracefully without affecting other processes.
268 */
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100269 svm->iommu->pasid_table[svm->pasid].val = 0;
David Woodhousee57e58b2016-01-12 19:18:06 +0000270 wmb();
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100271
David Woodhousee57e58b2016-01-12 19:18:06 +0000272 rcu_read_lock();
273 list_for_each_entry_rcu(sdev, &svm->devs, list) {
274 intel_flush_pasid_dev(svm, sdev, svm->pasid);
275 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
276 }
277 rcu_read_unlock();
278
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100279}
280
281static const struct mmu_notifier_ops intel_mmuops = {
282 .release = intel_mm_release,
283 .change_pte = intel_change_pte,
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100284 .invalidate_range = intel_invalidate_range,
285};
286
287static DEFINE_MUTEX(pasid_mutex);
288
David Woodhouse0204a492015-10-13 17:18:10 +0100289int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100290{
291 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
292 struct intel_svm_dev *sdev;
293 struct intel_svm *svm = NULL;
David Woodhouse5cec7532015-10-15 15:52:15 +0100294 struct mm_struct *mm = NULL;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100295 int pasid_max;
296 int ret;
297
Lu Baolu2e2e35d2017-11-03 10:51:32 -0600298 if (WARN_ON(!iommu || !iommu->pasid_table))
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100299 return -EINVAL;
300
301 if (dev_is_pci(dev)) {
302 pasid_max = pci_max_pasids(to_pci_dev(dev));
303 if (pasid_max < 0)
304 return -EINVAL;
305 } else
306 pasid_max = 1 << 20;
307
David Woodhouse5cec7532015-10-15 15:52:15 +0100308 if ((flags & SVM_FLAG_SUPERVISOR_MODE)) {
309 if (!ecap_srs(iommu->ecap))
310 return -EINVAL;
311 } else if (pasid) {
312 mm = get_task_mm(current);
313 BUG_ON(!mm);
314 }
315
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100316 mutex_lock(&pasid_mutex);
David Woodhouse569e4f72015-10-15 13:59:14 +0100317 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100318 int i;
319
320 idr_for_each_entry(&iommu->pasid_idr, svm, i) {
David Woodhouse5cec7532015-10-15 15:52:15 +0100321 if (svm->mm != mm ||
David Woodhouse569e4f72015-10-15 13:59:14 +0100322 (svm->flags & SVM_FLAG_PRIVATE_PASID))
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100323 continue;
324
325 if (svm->pasid >= pasid_max) {
326 dev_warn(dev,
327 "Limited PASID width. Cannot use existing PASID %d\n",
328 svm->pasid);
329 ret = -ENOSPC;
330 goto out;
331 }
332
333 list_for_each_entry(sdev, &svm->devs, list) {
334 if (dev == sdev->dev) {
David Woodhouse0204a492015-10-13 17:18:10 +0100335 if (sdev->ops != ops) {
336 ret = -EBUSY;
337 goto out;
338 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100339 sdev->users++;
340 goto success;
341 }
342 }
343
344 break;
345 }
346 }
347
348 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
349 if (!sdev) {
350 ret = -ENOMEM;
351 goto out;
352 }
353 sdev->dev = dev;
354
355 ret = intel_iommu_enable_pasid(iommu, sdev);
356 if (ret || !pasid) {
357 /* If they don't actually want to assign a PASID, this is
358 * just an enabling check/preparation. */
359 kfree(sdev);
360 goto out;
361 }
362 /* Finish the setup now we know we're keeping it */
363 sdev->users = 1;
David Woodhouse0204a492015-10-13 17:18:10 +0100364 sdev->ops = ops;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100365 init_rcu_head(&sdev->rcu);
366
367 if (!svm) {
368 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
369 if (!svm) {
370 ret = -ENOMEM;
371 kfree(sdev);
372 goto out;
373 }
374 svm->iommu = iommu;
375
David Woodhouse91017042016-09-12 10:49:11 +0800376 if (pasid_max > iommu->pasid_max)
377 pasid_max = iommu->pasid_max;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100378
David Woodhouse5a10ba22015-10-24 21:06:39 +0200379 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
380 ret = idr_alloc(&iommu->pasid_idr, svm,
381 !!cap_caching_mode(iommu->cap),
382 pasid_max - 1, GFP_KERNEL);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100383 if (ret < 0) {
384 kfree(svm);
385 goto out;
386 }
387 svm->pasid = ret;
388 svm->notifier.ops = &intel_mmuops;
David Woodhouse5cec7532015-10-15 15:52:15 +0100389 svm->mm = mm;
David Woodhouse569e4f72015-10-15 13:59:14 +0100390 svm->flags = flags;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100391 INIT_LIST_HEAD_RCU(&svm->devs);
392 ret = -ENOMEM;
David Woodhouse5cec7532015-10-15 15:52:15 +0100393 if (mm) {
394 ret = mmu_notifier_register(&svm->notifier, mm);
395 if (ret) {
396 idr_remove(&svm->iommu->pasid_idr, svm->pasid);
397 kfree(svm);
398 kfree(sdev);
399 goto out;
400 }
401 iommu->pasid_table[svm->pasid].val = (u64)__pa(mm->pgd) | 1;
David Woodhouse5cec7532015-10-15 15:52:15 +0100402 } else
403 iommu->pasid_table[svm->pasid].val = (u64)__pa(init_mm.pgd) | 1 | (1ULL << 11);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100404 wmb();
David Woodhouse5a10ba22015-10-24 21:06:39 +0200405 /* In caching mode, we still have to flush with PASID 0 when
406 * a PASID table entry becomes present. Not entirely clear
407 * *why* that would be the case — surely we could just issue
408 * a flush with the PASID value that we've changed? The PASID
409 * is the index into the table, after all. It's not like domain
410 * IDs in the case of the equivalent context-entry change in
411 * caching mode. And for that matter it's not entirely clear why
412 * a VMM would be in the business of caching the PASID table
413 * anyway. Surely that can be left entirely to the guest? */
414 if (cap_caching_mode(iommu->cap))
415 intel_flush_pasid_dev(svm, sdev, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100416 }
417 list_add_rcu(&sdev->list, &svm->devs);
418
419 success:
420 *pasid = svm->pasid;
421 ret = 0;
422 out:
423 mutex_unlock(&pasid_mutex);
David Woodhouse5cec7532015-10-15 15:52:15 +0100424 if (mm)
425 mmput(mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100426 return ret;
427}
428EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
429
430int intel_svm_unbind_mm(struct device *dev, int pasid)
431{
432 struct intel_svm_dev *sdev;
433 struct intel_iommu *iommu;
434 struct intel_svm *svm;
435 int ret = -EINVAL;
436
437 mutex_lock(&pasid_mutex);
438 iommu = intel_svm_device_to_iommu(dev);
439 if (!iommu || !iommu->pasid_table)
440 goto out;
441
442 svm = idr_find(&iommu->pasid_idr, pasid);
443 if (!svm)
444 goto out;
445
446 list_for_each_entry(sdev, &svm->devs, list) {
447 if (dev == sdev->dev) {
448 ret = 0;
449 sdev->users--;
450 if (!sdev->users) {
451 list_del_rcu(&sdev->list);
452 /* Flush the PASID cache and IOTLB for this device.
453 * Note that we do depend on the hardware *not* using
454 * the PASID any more. Just as we depend on other
455 * devices never using PASIDs that they have no right
456 * to use. We have a *shared* PASID table, because it's
457 * large and has to be physically contiguous. So it's
458 * hard to be as defensive as we might like. */
David Woodhouse5a10ba22015-10-24 21:06:39 +0200459 intel_flush_pasid_dev(svm, sdev, svm->pasid);
David Woodhousee0349922015-10-16 19:36:53 +0100460 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100461 kfree_rcu(sdev, rcu);
462
463 if (list_empty(&svm->devs)) {
Lu Baolu4fa064b2017-11-03 10:51:34 -0600464 svm->iommu->pasid_table[svm->pasid].val = 0;
465 wmb();
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100466
467 idr_remove(&svm->iommu->pasid_idr, svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100468 if (svm->mm)
David Woodhousee57e58b2016-01-12 19:18:06 +0000469 mmu_notifier_unregister(&svm->notifier, svm->mm);
470
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100471 /* We mandate that no page faults may be outstanding
472 * for the PASID when intel_svm_unbind_mm() is called.
473 * If that is not obeyed, subtle errors will happen.
474 * Let's make them less subtle... */
475 memset(svm, 0x6b, sizeof(*svm));
476 kfree(svm);
477 }
478 }
479 break;
480 }
481 }
482 out:
483 mutex_unlock(&pasid_mutex);
484
485 return ret;
486}
487EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100488
CQ Tang15060ab2017-05-10 11:39:03 -0700489int intel_svm_is_pasid_valid(struct device *dev, int pasid)
490{
491 struct intel_iommu *iommu;
492 struct intel_svm *svm;
493 int ret = -EINVAL;
494
495 mutex_lock(&pasid_mutex);
496 iommu = intel_svm_device_to_iommu(dev);
497 if (!iommu || !iommu->pasid_table)
498 goto out;
499
500 svm = idr_find(&iommu->pasid_idr, pasid);
501 if (!svm)
502 goto out;
503
504 /* init_mm is used in this case */
505 if (!svm->mm)
506 ret = 1;
507 else if (atomic_read(&svm->mm->mm_users) > 0)
508 ret = 1;
509 else
510 ret = 0;
511
512 out:
513 mutex_unlock(&pasid_mutex);
514
515 return ret;
516}
517EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid);
518
David Woodhousea222a7f2015-10-07 23:35:18 +0100519/* Page request queue descriptor */
520struct page_req_dsc {
521 u64 srr:1;
522 u64 bof:1;
523 u64 pasid_present:1;
524 u64 lpig:1;
525 u64 pasid:20;
526 u64 bus:8;
527 u64 private:23;
528 u64 prg_index:9;
529 u64 rd_req:1;
530 u64 wr_req:1;
531 u64 exe_req:1;
532 u64 priv_req:1;
533 u64 devfn:8;
534 u64 addr:52;
535};
536
537#define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100538
539static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
540{
541 unsigned long requested = 0;
542
543 if (req->exe_req)
544 requested |= VM_EXEC;
545
546 if (req->rd_req)
547 requested |= VM_READ;
548
549 if (req->wr_req)
550 requested |= VM_WRITE;
551
552 return (requested & ~vma->vm_flags) != 0;
553}
554
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700555static bool is_canonical_address(u64 addr)
556{
557 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
558 long saddr = (long) addr;
559
560 return (((saddr << shift) >> shift) == saddr);
561}
562
David Woodhousea222a7f2015-10-07 23:35:18 +0100563static irqreturn_t prq_event_thread(int irq, void *d)
564{
565 struct intel_iommu *iommu = d;
566 struct intel_svm *svm = NULL;
567 int head, tail, handled = 0;
568
David Woodhouse46924002016-02-15 12:42:38 +0000569 /* Clear PPR bit before reading head/tail registers, to
570 * ensure that we get a new interrupt if needed. */
571 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
572
David Woodhousea222a7f2015-10-07 23:35:18 +0100573 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
574 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
575 while (head != tail) {
David Woodhouse0204a492015-10-13 17:18:10 +0100576 struct intel_svm_dev *sdev;
David Woodhousea222a7f2015-10-07 23:35:18 +0100577 struct vm_area_struct *vma;
578 struct page_req_dsc *req;
579 struct qi_desc resp;
580 int ret, result;
581 u64 address;
582
583 handled = 1;
584
585 req = &iommu->prq[head / sizeof(*req)];
586
587 result = QI_RESP_FAILURE;
David Woodhouse7f92a2e2015-10-16 17:22:31 +0100588 address = (u64)req->addr << VTD_PAGE_SHIFT;
David Woodhousea222a7f2015-10-07 23:35:18 +0100589 if (!req->pasid_present) {
590 pr_err("%s: Page request without PASID: %08llx %08llx\n",
591 iommu->name, ((unsigned long long *)req)[0],
592 ((unsigned long long *)req)[1]);
593 goto bad_req;
594 }
595
596 if (!svm || svm->pasid != req->pasid) {
597 rcu_read_lock();
598 svm = idr_find(&iommu->pasid_idr, req->pasid);
599 /* It *can't* go away, because the driver is not permitted
600 * to unbind the mm while any page faults are outstanding.
601 * So we only need RCU to protect the internal idr code. */
602 rcu_read_unlock();
603
604 if (!svm) {
605 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
606 iommu->name, req->pasid, ((unsigned long long *)req)[0],
607 ((unsigned long long *)req)[1]);
David Woodhouse26322ab2015-10-15 21:12:56 +0100608 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100609 }
610 }
611
612 result = QI_RESP_INVALID;
David Woodhouse5cec7532015-10-15 15:52:15 +0100613 /* Since we're using init_mm.pgd directly, we should never take
614 * any faults on kernel addresses. */
615 if (!svm->mm)
616 goto bad_req;
David Woodhousee57e58b2016-01-12 19:18:06 +0000617 /* If the mm is already defunct, don't handle faults. */
Vegard Nossum388f7932017-02-27 14:30:13 -0800618 if (!mmget_not_zero(svm->mm))
David Woodhousee57e58b2016-01-12 19:18:06 +0000619 goto bad_req;
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700620
621 /* If address is not canonical, return invalid response */
622 if (!is_canonical_address(address))
623 goto bad_req;
624
David Woodhousea222a7f2015-10-07 23:35:18 +0100625 down_read(&svm->mm->mmap_sem);
626 vma = find_extend_vma(svm->mm, address);
627 if (!vma || address < vma->vm_start)
628 goto invalid;
629
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100630 if (access_error(vma, req))
631 goto invalid;
632
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700633 ret = handle_mm_fault(vma, address,
David Woodhousea222a7f2015-10-07 23:35:18 +0100634 req->wr_req ? FAULT_FLAG_WRITE : 0);
635 if (ret & VM_FAULT_ERROR)
636 goto invalid;
637
638 result = QI_RESP_SUCCESS;
639 invalid:
640 up_read(&svm->mm->mmap_sem);
David Woodhousee57e58b2016-01-12 19:18:06 +0000641 mmput(svm->mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100642 bad_req:
643 /* Accounting for major/minor faults? */
David Woodhouse0204a492015-10-13 17:18:10 +0100644 rcu_read_lock();
645 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Dan Carpenter3c7c2f32015-10-17 08:18:47 +0300646 if (sdev->sid == PCI_DEVID(req->bus, req->devfn))
David Woodhouse0204a492015-10-13 17:18:10 +0100647 break;
648 }
649 /* Other devices can go away, but the drivers are not permitted
650 * to unbind while any page faults might be in flight. So it's
651 * OK to drop the 'lock' here now we have it. */
652 rcu_read_unlock();
653
654 if (WARN_ON(&sdev->list == &svm->devs))
655 sdev = NULL;
656
657 if (sdev && sdev->ops && sdev->ops->fault_cb) {
658 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
David Woodhouse0bdec952015-10-28 15:14:09 +0900659 (req->exe_req << 1) | (req->priv_req);
David Woodhouse0204a492015-10-13 17:18:10 +0100660 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, req->private, rwxp, result);
661 }
David Woodhouse26322ab2015-10-15 21:12:56 +0100662 /* We get here in the error case where the PASID lookup failed,
663 and these can be NULL. Do not use them below this point! */
664 sdev = NULL;
665 svm = NULL;
666 no_pasid:
David Woodhousea222a7f2015-10-07 23:35:18 +0100667 if (req->lpig) {
668 /* Page Group Response */
669 resp.low = QI_PGRP_PASID(req->pasid) |
670 QI_PGRP_DID((req->bus << 8) | req->devfn) |
671 QI_PGRP_PASID_P(req->pasid_present) |
672 QI_PGRP_RESP_TYPE;
673 resp.high = QI_PGRP_IDX(req->prg_index) |
674 QI_PGRP_PRIV(req->private) | QI_PGRP_RESP_CODE(result);
675
David Woodhouse26322ab2015-10-15 21:12:56 +0100676 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100677 } else if (req->srr) {
678 /* Page Stream Response */
679 resp.low = QI_PSTRM_IDX(req->prg_index) |
680 QI_PSTRM_PRIV(req->private) | QI_PSTRM_BUS(req->bus) |
681 QI_PSTRM_PASID(req->pasid) | QI_PSTRM_RESP_TYPE;
682 resp.high = QI_PSTRM_ADDR(address) | QI_PSTRM_DEVFN(req->devfn) |
683 QI_PSTRM_RESP_CODE(result);
684
David Woodhouse26322ab2015-10-15 21:12:56 +0100685 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100686 }
687
688 head = (head + sizeof(*req)) & PRQ_RING_MASK;
689 }
690
691 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
692
693 return IRQ_RETVAL(handled);
694}