blob: dca88f9fdf29a10b574ed5d123eabf4df7ac7a06 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
David Woodhouse8a94ade2015-03-24 14:54:56 +00002/*
3 * Copyright © 2015 Intel Corporation.
4 *
David Woodhouse8a94ade2015-03-24 14:54:56 +00005 * Authors: David Woodhouse <dwmw2@infradead.org>
6 */
7
8#include <linux/intel-iommu.h>
David Woodhouse2f26e0a2015-09-09 11:40:47 +01009#include <linux/mmu_notifier.h>
10#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010011#include <linux/sched/mm.h>
David Woodhouse2f26e0a2015-09-09 11:40:47 +010012#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>
David Woodhousea222a7f2015-10-07 23:35:18 +010017#include <linux/dmar.h>
18#include <linux/interrupt.h>
Souptick Joarder50a7ca32018-08-17 15:44:47 -070019#include <linux/mm_types.h>
Ashok Raj9d8c3af2017-08-08 13:29:27 -070020#include <asm/page.h>
David Woodhousea222a7f2015-10-07 23:35:18 +010021
Lu Baoluaf395072018-07-14 15:46:56 +080022#include "intel-pasid.h"
23
David Woodhousea222a7f2015-10-07 23:35:18 +010024static irqreturn_t prq_event_thread(int irq, void *d);
David Woodhouse2f26e0a2015-09-09 11:40:47 +010025
Lu Baolud9737952018-07-14 15:47:02 +080026int intel_svm_init(struct intel_iommu *iommu)
David Woodhouse8a94ade2015-03-24 14:54:56 +000027{
Sohil Mehta59103ca2017-12-20 11:59:25 -080028 if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
29 !cap_fl1gp_support(iommu->cap))
30 return -EINVAL;
31
Sohil Mehtaf1ac10c2017-12-20 11:59:26 -080032 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
33 !cap_5lp_support(iommu->cap))
34 return -EINVAL;
35
David Woodhouse8a94ade2015-03-24 14:54:56 +000036 return 0;
37}
David Woodhouse2f26e0a2015-09-09 11:40:47 +010038
David Woodhousea222a7f2015-10-07 23:35:18 +010039#define PRQ_ORDER 0
40
41int intel_svm_enable_prq(struct intel_iommu *iommu)
42{
43 struct page *pages;
44 int irq, ret;
45
46 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
47 if (!pages) {
48 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
49 iommu->name);
50 return -ENOMEM;
51 }
52 iommu->prq = page_address(pages);
53
54 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
55 if (irq <= 0) {
56 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
57 iommu->name);
58 ret = -EINVAL;
59 err:
60 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
61 iommu->prq = NULL;
62 return ret;
63 }
64 iommu->pr_irq = irq;
65
66 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
67
68 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
69 iommu->prq_name, iommu);
70 if (ret) {
71 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
72 iommu->name);
73 dmar_free_hwirq(irq);
Jerry Snitselaar72d54812017-12-20 09:48:56 -070074 iommu->pr_irq = 0;
David Woodhousea222a7f2015-10-07 23:35:18 +010075 goto err;
76 }
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);
80
81 return 0;
82}
83
84int intel_svm_finish_prq(struct intel_iommu *iommu)
85{
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);
89
Jerry Snitselaar72d54812017-12-20 09:48:56 -070090 if (iommu->pr_irq) {
91 free_irq(iommu->pr_irq, iommu);
92 dmar_free_hwirq(iommu->pr_irq);
93 iommu->pr_irq = 0;
94 }
David Woodhousea222a7f2015-10-07 23:35:18 +010095
96 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
97 iommu->prq = NULL;
98
99 return 0;
100}
101
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100102static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
Jacob Pan8744daf2019-08-26 08:53:29 -0700103 unsigned long address, unsigned long pages, int ih)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100104{
105 struct qi_desc desc;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100106
Lu Baoluf81b8462019-11-20 14:10:16 +0800107 if (pages == -1) {
Jacob Pan8744daf2019-08-26 08:53:29 -0700108 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
109 QI_EIOTLB_DID(sdev->did) |
110 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
111 QI_EIOTLB_TYPE;
Lu Baolu5d308fc2018-12-10 09:58:58 +0800112 desc.qw1 = 0;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100113 } else {
David Woodhouse5d52f482015-10-20 15:52:13 +0100114 int mask = ilog2(__roundup_pow_of_two(pages));
115
Lu Baolu5d308fc2018-12-10 09:58:58 +0800116 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
117 QI_EIOTLB_DID(sdev->did) |
118 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |
119 QI_EIOTLB_TYPE;
120 desc.qw1 = QI_EIOTLB_ADDR(address) |
Lu Baolu5d308fc2018-12-10 09:58:58 +0800121 QI_EIOTLB_IH(ih) |
122 QI_EIOTLB_AM(mask);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100123 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800124 desc.qw2 = 0;
125 desc.qw3 = 0;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100126 qi_submit_sync(&desc, svm->iommu);
127
128 if (sdev->dev_iotlb) {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800129 desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
130 QI_DEV_EIOTLB_SID(sdev->sid) |
131 QI_DEV_EIOTLB_QDEP(sdev->qdep) |
132 QI_DEIOTLB_TYPE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100133 if (pages == -1) {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800134 desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
135 QI_DEV_EIOTLB_SIZE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100136 } else if (pages > 1) {
137 /* The least significant zero bit indicates the size. So,
138 * for example, an "address" value of 0x12345f000 will
139 * flush from 0x123440000 to 0x12347ffff (256KiB). */
140 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
Ingo Molnared7158b2018-02-22 10:54:55 +0100141 unsigned long mask = __rounddown_pow_of_two(address ^ last);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100142
Lu Baolu5d308fc2018-12-10 09:58:58 +0800143 desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |
144 (mask - 1)) | QI_DEV_EIOTLB_SIZE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100145 } else {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800146 desc.qw1 = QI_DEV_EIOTLB_ADDR(address);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100147 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800148 desc.qw2 = 0;
149 desc.qw3 = 0;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100150 qi_submit_sync(&desc, svm->iommu);
151 }
152}
153
154static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
Jacob Pan8744daf2019-08-26 08:53:29 -0700155 unsigned long pages, int ih)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100156{
157 struct intel_svm_dev *sdev;
158
159 rcu_read_lock();
160 list_for_each_entry_rcu(sdev, &svm->devs, list)
Jacob Pan8744daf2019-08-26 08:53:29 -0700161 intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100162 rcu_read_unlock();
163}
164
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100165/* Pages have been freed at this point */
166static void intel_invalidate_range(struct mmu_notifier *mn,
167 struct mm_struct *mm,
168 unsigned long start, unsigned long end)
169{
170 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
171
172 intel_flush_svm_range(svm, start,
Jacob Pan8744daf2019-08-26 08:53:29 -0700173 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100174}
175
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100176static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
177{
178 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
David Woodhousee57e58b2016-01-12 19:18:06 +0000179 struct intel_svm_dev *sdev;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100180
David Woodhousee57e58b2016-01-12 19:18:06 +0000181 /* This might end up being called from exit_mmap(), *before* the page
182 * tables are cleared. And __mmu_notifier_release() will delete us from
183 * the list of notifiers so that our invalidate_range() callback doesn't
184 * get called when the page tables are cleared. So we need to protect
185 * against hardware accessing those page tables.
186 *
187 * We do it by clearing the entry in the PASID table and then flushing
188 * the IOTLB and the PASID table caches. This might upset hardware;
189 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
190 * page) so that we end up taking a fault that the hardware really
191 * *has* to handle gracefully without affecting other processes.
192 */
David Woodhousee57e58b2016-01-12 19:18:06 +0000193 rcu_read_lock();
194 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800195 intel_pasid_tear_down_entry(svm->iommu, sdev->dev, svm->pasid);
Jacob Pan8744daf2019-08-26 08:53:29 -0700196 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0);
David Woodhousee57e58b2016-01-12 19:18:06 +0000197 }
198 rcu_read_unlock();
199
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100200}
201
202static const struct mmu_notifier_ops intel_mmuops = {
203 .release = intel_mm_release,
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100204 .invalidate_range = intel_invalidate_range,
205};
206
207static DEFINE_MUTEX(pasid_mutex);
Lu Baolu51261aa2018-07-14 15:46:55 +0800208static LIST_HEAD(global_svm_list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100209
David Woodhouse0204a492015-10-13 17:18:10 +0100210int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100211{
212 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800213 struct device_domain_info *info;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100214 struct intel_svm_dev *sdev;
215 struct intel_svm *svm = NULL;
David Woodhouse5cec7532015-10-15 15:52:15 +0100216 struct mm_struct *mm = NULL;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100217 int pasid_max;
218 int ret;
219
Lu Baoluc56cba52019-03-01 11:23:12 +0800220 if (!iommu || dmar_disabled)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100221 return -EINVAL;
222
223 if (dev_is_pci(dev)) {
224 pasid_max = pci_max_pasids(to_pci_dev(dev));
225 if (pasid_max < 0)
226 return -EINVAL;
227 } else
228 pasid_max = 1 << 20;
229
Lu Baolubb37f7d2018-05-04 13:08:19 +0800230 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
David Woodhouse5cec7532015-10-15 15:52:15 +0100231 if (!ecap_srs(iommu->ecap))
232 return -EINVAL;
233 } else if (pasid) {
234 mm = get_task_mm(current);
235 BUG_ON(!mm);
236 }
237
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100238 mutex_lock(&pasid_mutex);
David Woodhouse569e4f72015-10-15 13:59:14 +0100239 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
Lu Baolu51261aa2018-07-14 15:46:55 +0800240 struct intel_svm *t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100241
Lu Baolu51261aa2018-07-14 15:46:55 +0800242 list_for_each_entry(t, &global_svm_list, list) {
243 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100244 continue;
245
Lu Baolu51261aa2018-07-14 15:46:55 +0800246 svm = t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100247 if (svm->pasid >= pasid_max) {
248 dev_warn(dev,
249 "Limited PASID width. Cannot use existing PASID %d\n",
250 svm->pasid);
251 ret = -ENOSPC;
252 goto out;
253 }
254
255 list_for_each_entry(sdev, &svm->devs, list) {
256 if (dev == sdev->dev) {
David Woodhouse0204a492015-10-13 17:18:10 +0100257 if (sdev->ops != ops) {
258 ret = -EBUSY;
259 goto out;
260 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100261 sdev->users++;
262 goto success;
263 }
264 }
265
266 break;
267 }
268 }
269
270 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
271 if (!sdev) {
272 ret = -ENOMEM;
273 goto out;
274 }
275 sdev->dev = dev;
276
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800277 ret = intel_iommu_enable_pasid(iommu, dev);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100278 if (ret || !pasid) {
279 /* If they don't actually want to assign a PASID, this is
280 * just an enabling check/preparation. */
281 kfree(sdev);
282 goto out;
283 }
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800284
285 info = dev->archdata.iommu;
286 if (!info || !info->pasid_supported) {
287 kfree(sdev);
288 goto out;
289 }
290
291 sdev->did = FLPT_DEFAULT_DID;
292 sdev->sid = PCI_DEVID(info->bus, info->devfn);
293 if (info->ats_enabled) {
294 sdev->dev_iotlb = 1;
295 sdev->qdep = info->ats_qdep;
296 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
297 sdev->qdep = 0;
298 }
299
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100300 /* Finish the setup now we know we're keeping it */
301 sdev->users = 1;
David Woodhouse0204a492015-10-13 17:18:10 +0100302 sdev->ops = ops;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100303 init_rcu_head(&sdev->rcu);
304
305 if (!svm) {
306 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
307 if (!svm) {
308 ret = -ENOMEM;
309 kfree(sdev);
310 goto out;
311 }
312 svm->iommu = iommu;
313
Lu Baolu4774cc52018-07-14 15:47:01 +0800314 if (pasid_max > intel_pasid_max_id)
315 pasid_max = intel_pasid_max_id;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100316
David Woodhouse5a10ba22015-10-24 21:06:39 +0200317 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
Lu Baoluaf395072018-07-14 15:46:56 +0800318 ret = intel_pasid_alloc_id(svm,
319 !!cap_caching_mode(iommu->cap),
320 pasid_max - 1, GFP_KERNEL);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100321 if (ret < 0) {
322 kfree(svm);
Lu Baolubbe4b3a2018-02-24 13:42:27 +0800323 kfree(sdev);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100324 goto out;
325 }
326 svm->pasid = ret;
327 svm->notifier.ops = &intel_mmuops;
David Woodhouse5cec7532015-10-15 15:52:15 +0100328 svm->mm = mm;
David Woodhouse569e4f72015-10-15 13:59:14 +0100329 svm->flags = flags;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100330 INIT_LIST_HEAD_RCU(&svm->devs);
Lu Baolu51261aa2018-07-14 15:46:55 +0800331 INIT_LIST_HEAD(&svm->list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100332 ret = -ENOMEM;
David Woodhouse5cec7532015-10-15 15:52:15 +0100333 if (mm) {
334 ret = mmu_notifier_register(&svm->notifier, mm);
335 if (ret) {
Lu Baoluaf395072018-07-14 15:46:56 +0800336 intel_pasid_free_id(svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100337 kfree(svm);
338 kfree(sdev);
339 goto out;
340 }
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800341 }
Sohil Mehta2f13eb72017-12-20 11:59:27 -0800342
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800343 spin_lock(&iommu->lock);
344 ret = intel_pasid_setup_first_level(iommu, dev,
345 mm ? mm->pgd : init_mm.pgd,
346 svm->pasid, FLPT_DEFAULT_DID,
347 mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);
348 spin_unlock(&iommu->lock);
349 if (ret) {
350 if (mm)
351 mmu_notifier_unregister(&svm->notifier, mm);
352 intel_pasid_free_id(svm->pasid);
353 kfree(svm);
354 kfree(sdev);
355 goto out;
356 }
Lu Baolu51261aa2018-07-14 15:46:55 +0800357
358 list_add_tail(&svm->list, &global_svm_list);
Jacob Pand7af4d92019-05-08 12:22:46 -0700359 } else {
360 /*
361 * Binding a new device with existing PASID, need to setup
362 * the PASID entry.
363 */
364 spin_lock(&iommu->lock);
365 ret = intel_pasid_setup_first_level(iommu, dev,
366 mm ? mm->pgd : init_mm.pgd,
367 svm->pasid, FLPT_DEFAULT_DID,
368 mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);
369 spin_unlock(&iommu->lock);
370 if (ret) {
371 kfree(sdev);
372 goto out;
373 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100374 }
375 list_add_rcu(&sdev->list, &svm->devs);
376
377 success:
378 *pasid = svm->pasid;
379 ret = 0;
380 out:
381 mutex_unlock(&pasid_mutex);
David Woodhouse5cec7532015-10-15 15:52:15 +0100382 if (mm)
383 mmput(mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100384 return ret;
385}
386EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
387
388int intel_svm_unbind_mm(struct device *dev, int pasid)
389{
390 struct intel_svm_dev *sdev;
391 struct intel_iommu *iommu;
392 struct intel_svm *svm;
393 int ret = -EINVAL;
394
395 mutex_lock(&pasid_mutex);
396 iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800397 if (!iommu)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100398 goto out;
399
Lu Baoluaf395072018-07-14 15:46:56 +0800400 svm = intel_pasid_lookup_id(pasid);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100401 if (!svm)
402 goto out;
403
404 list_for_each_entry(sdev, &svm->devs, list) {
405 if (dev == sdev->dev) {
406 ret = 0;
407 sdev->users--;
408 if (!sdev->users) {
409 list_del_rcu(&sdev->list);
410 /* Flush the PASID cache and IOTLB for this device.
411 * Note that we do depend on the hardware *not* using
412 * the PASID any more. Just as we depend on other
413 * devices never using PASIDs that they have no right
414 * to use. We have a *shared* PASID table, because it's
415 * large and has to be physically contiguous. So it's
416 * hard to be as defensive as we might like. */
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800417 intel_pasid_tear_down_entry(iommu, dev, svm->pasid);
Jacob Pan8744daf2019-08-26 08:53:29 -0700418 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100419 kfree_rcu(sdev, rcu);
420
421 if (list_empty(&svm->devs)) {
Lu Baoluaf395072018-07-14 15:46:56 +0800422 intel_pasid_free_id(svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100423 if (svm->mm)
David Woodhousee57e58b2016-01-12 19:18:06 +0000424 mmu_notifier_unregister(&svm->notifier, svm->mm);
425
Lu Baolu51261aa2018-07-14 15:46:55 +0800426 list_del(&svm->list);
427
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100428 /* We mandate that no page faults may be outstanding
429 * for the PASID when intel_svm_unbind_mm() is called.
430 * If that is not obeyed, subtle errors will happen.
431 * Let's make them less subtle... */
432 memset(svm, 0x6b, sizeof(*svm));
433 kfree(svm);
434 }
435 }
436 break;
437 }
438 }
439 out:
440 mutex_unlock(&pasid_mutex);
441
442 return ret;
443}
444EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100445
CQ Tang15060ab2017-05-10 11:39:03 -0700446int intel_svm_is_pasid_valid(struct device *dev, int pasid)
447{
448 struct intel_iommu *iommu;
449 struct intel_svm *svm;
450 int ret = -EINVAL;
451
452 mutex_lock(&pasid_mutex);
453 iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800454 if (!iommu)
CQ Tang15060ab2017-05-10 11:39:03 -0700455 goto out;
456
Lu Baoluaf395072018-07-14 15:46:56 +0800457 svm = intel_pasid_lookup_id(pasid);
CQ Tang15060ab2017-05-10 11:39:03 -0700458 if (!svm)
459 goto out;
460
461 /* init_mm is used in this case */
462 if (!svm->mm)
463 ret = 1;
464 else if (atomic_read(&svm->mm->mm_users) > 0)
465 ret = 1;
466 else
467 ret = 0;
468
469 out:
470 mutex_unlock(&pasid_mutex);
471
472 return ret;
473}
474EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid);
475
David Woodhousea222a7f2015-10-07 23:35:18 +0100476/* Page request queue descriptor */
477struct page_req_dsc {
Jacob Pan5b438f42019-01-11 13:04:57 +0800478 union {
479 struct {
480 u64 type:8;
481 u64 pasid_present:1;
482 u64 priv_data_present:1;
483 u64 rsvd:6;
484 u64 rid:16;
485 u64 pasid:20;
486 u64 exe_req:1;
487 u64 pm_req:1;
488 u64 rsvd2:10;
489 };
490 u64 qw_0;
491 };
492 union {
493 struct {
494 u64 rd_req:1;
495 u64 wr_req:1;
496 u64 lpig:1;
497 u64 prg_index:9;
498 u64 addr:52;
499 };
500 u64 qw_1;
501 };
502 u64 priv_data[2];
David Woodhousea222a7f2015-10-07 23:35:18 +0100503};
504
505#define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100506
507static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
508{
509 unsigned long requested = 0;
510
511 if (req->exe_req)
512 requested |= VM_EXEC;
513
514 if (req->rd_req)
515 requested |= VM_READ;
516
517 if (req->wr_req)
518 requested |= VM_WRITE;
519
520 return (requested & ~vma->vm_flags) != 0;
521}
522
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700523static bool is_canonical_address(u64 addr)
524{
525 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
526 long saddr = (long) addr;
527
528 return (((saddr << shift) >> shift) == saddr);
529}
530
David Woodhousea222a7f2015-10-07 23:35:18 +0100531static irqreturn_t prq_event_thread(int irq, void *d)
532{
533 struct intel_iommu *iommu = d;
534 struct intel_svm *svm = NULL;
535 int head, tail, handled = 0;
536
David Woodhouse46924002016-02-15 12:42:38 +0000537 /* Clear PPR bit before reading head/tail registers, to
538 * ensure that we get a new interrupt if needed. */
539 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
540
David Woodhousea222a7f2015-10-07 23:35:18 +0100541 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
542 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
543 while (head != tail) {
David Woodhouse0204a492015-10-13 17:18:10 +0100544 struct intel_svm_dev *sdev;
David Woodhousea222a7f2015-10-07 23:35:18 +0100545 struct vm_area_struct *vma;
546 struct page_req_dsc *req;
547 struct qi_desc resp;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700548 int result;
549 vm_fault_t ret;
David Woodhousea222a7f2015-10-07 23:35:18 +0100550 u64 address;
551
552 handled = 1;
553
554 req = &iommu->prq[head / sizeof(*req)];
555
556 result = QI_RESP_FAILURE;
David Woodhouse7f92a2e2015-10-16 17:22:31 +0100557 address = (u64)req->addr << VTD_PAGE_SHIFT;
David Woodhousea222a7f2015-10-07 23:35:18 +0100558 if (!req->pasid_present) {
559 pr_err("%s: Page request without PASID: %08llx %08llx\n",
560 iommu->name, ((unsigned long long *)req)[0],
561 ((unsigned long long *)req)[1]);
Lu Baolu19ed3e22018-11-05 10:18:58 +0800562 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100563 }
564
565 if (!svm || svm->pasid != req->pasid) {
566 rcu_read_lock();
Lu Baoluaf395072018-07-14 15:46:56 +0800567 svm = intel_pasid_lookup_id(req->pasid);
David Woodhousea222a7f2015-10-07 23:35:18 +0100568 /* It *can't* go away, because the driver is not permitted
569 * to unbind the mm while any page faults are outstanding.
570 * So we only need RCU to protect the internal idr code. */
571 rcu_read_unlock();
572
573 if (!svm) {
574 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
575 iommu->name, req->pasid, ((unsigned long long *)req)[0],
576 ((unsigned long long *)req)[1]);
David Woodhouse26322ab2015-10-15 21:12:56 +0100577 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100578 }
579 }
580
581 result = QI_RESP_INVALID;
David Woodhouse5cec7532015-10-15 15:52:15 +0100582 /* Since we're using init_mm.pgd directly, we should never take
583 * any faults on kernel addresses. */
584 if (!svm->mm)
585 goto bad_req;
David Woodhousee57e58b2016-01-12 19:18:06 +0000586 /* If the mm is already defunct, don't handle faults. */
Vegard Nossum388f7932017-02-27 14:30:13 -0800587 if (!mmget_not_zero(svm->mm))
David Woodhousee57e58b2016-01-12 19:18:06 +0000588 goto bad_req;
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700589
590 /* If address is not canonical, return invalid response */
591 if (!is_canonical_address(address))
592 goto bad_req;
593
David Woodhousea222a7f2015-10-07 23:35:18 +0100594 down_read(&svm->mm->mmap_sem);
595 vma = find_extend_vma(svm->mm, address);
596 if (!vma || address < vma->vm_start)
597 goto invalid;
598
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100599 if (access_error(vma, req))
600 goto invalid;
601
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700602 ret = handle_mm_fault(vma, address,
David Woodhousea222a7f2015-10-07 23:35:18 +0100603 req->wr_req ? FAULT_FLAG_WRITE : 0);
604 if (ret & VM_FAULT_ERROR)
605 goto invalid;
606
607 result = QI_RESP_SUCCESS;
608 invalid:
609 up_read(&svm->mm->mmap_sem);
David Woodhousee57e58b2016-01-12 19:18:06 +0000610 mmput(svm->mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100611 bad_req:
612 /* Accounting for major/minor faults? */
David Woodhouse0204a492015-10-13 17:18:10 +0100613 rcu_read_lock();
614 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Jacob Pan5b438f42019-01-11 13:04:57 +0800615 if (sdev->sid == req->rid)
David Woodhouse0204a492015-10-13 17:18:10 +0100616 break;
617 }
618 /* Other devices can go away, but the drivers are not permitted
619 * to unbind while any page faults might be in flight. So it's
620 * OK to drop the 'lock' here now we have it. */
621 rcu_read_unlock();
622
623 if (WARN_ON(&sdev->list == &svm->devs))
624 sdev = NULL;
625
626 if (sdev && sdev->ops && sdev->ops->fault_cb) {
627 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800628 (req->exe_req << 1) | (req->pm_req);
629 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
630 req->priv_data, rwxp, result);
David Woodhouse0204a492015-10-13 17:18:10 +0100631 }
David Woodhouse26322ab2015-10-15 21:12:56 +0100632 /* We get here in the error case where the PASID lookup failed,
633 and these can be NULL. Do not use them below this point! */
634 sdev = NULL;
635 svm = NULL;
636 no_pasid:
Jacob Pan5b438f42019-01-11 13:04:57 +0800637 if (req->lpig || req->priv_data_present) {
638 /*
639 * Per VT-d spec. v3.0 ch7.7, system software must
640 * respond with page group response if private data
641 * is present (PDP) or last page in group (LPIG) bit
642 * is set. This is an additional VT-d feature beyond
643 * PCI ATS spec.
644 */
Lu Baolu5d308fc2018-12-10 09:58:58 +0800645 resp.qw0 = QI_PGRP_PASID(req->pasid) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800646 QI_PGRP_DID(req->rid) |
David Woodhousea222a7f2015-10-07 23:35:18 +0100647 QI_PGRP_PASID_P(req->pasid_present) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800648 QI_PGRP_PDP(req->pasid_present) |
649 QI_PGRP_RESP_CODE(result) |
David Woodhousea222a7f2015-10-07 23:35:18 +0100650 QI_PGRP_RESP_TYPE;
Lu Baolu5d308fc2018-12-10 09:58:58 +0800651 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800652 QI_PGRP_LPIG(req->lpig);
653
654 if (req->priv_data_present)
655 memcpy(&resp.qw2, req->priv_data,
656 sizeof(req->priv_data));
David Woodhousea222a7f2015-10-07 23:35:18 +0100657 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800658 resp.qw2 = 0;
659 resp.qw3 = 0;
660 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100661
662 head = (head + sizeof(*req)) & PRQ_RING_MASK;
663 }
664
665 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
666
667 return IRQ_RETVAL(handled);
668}