blob: 780de0caafe845cb1b2c031010fcc25402decf31 [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,
David Woodhouse5d52f482015-10-20 15:52:13 +0100103 unsigned long address, unsigned long pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100104{
105 struct qi_desc desc;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100106
David Woodhouse5d52f482015-10-20 15:52:13 +0100107 if (pages == -1) {
David Woodhousee0349922015-10-16 19:36:53 +0100108 /* For global kernel pages we have to flush them in *all* PASIDs
109 * because that's the only option the hardware gives us. Despite
110 * the fact that they are actually only accessible through one. */
111 if (gl)
Lu Baolu5d308fc2018-12-10 09:58:58 +0800112 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
113 QI_EIOTLB_DID(sdev->did) |
114 QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) |
115 QI_EIOTLB_TYPE;
David Woodhousee0349922015-10-16 19:36:53 +0100116 else
Lu Baolu5d308fc2018-12-10 09:58:58 +0800117 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
118 QI_EIOTLB_DID(sdev->did) |
119 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
120 QI_EIOTLB_TYPE;
121 desc.qw1 = 0;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100122 } else {
David Woodhouse5d52f482015-10-20 15:52:13 +0100123 int mask = ilog2(__roundup_pow_of_two(pages));
124
Lu Baolu5d308fc2018-12-10 09:58:58 +0800125 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
126 QI_EIOTLB_DID(sdev->did) |
127 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |
128 QI_EIOTLB_TYPE;
129 desc.qw1 = QI_EIOTLB_ADDR(address) |
130 QI_EIOTLB_GL(gl) |
131 QI_EIOTLB_IH(ih) |
132 QI_EIOTLB_AM(mask);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100133 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800134 desc.qw2 = 0;
135 desc.qw3 = 0;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100136 qi_submit_sync(&desc, svm->iommu);
137
138 if (sdev->dev_iotlb) {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800139 desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
140 QI_DEV_EIOTLB_SID(sdev->sid) |
141 QI_DEV_EIOTLB_QDEP(sdev->qdep) |
142 QI_DEIOTLB_TYPE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100143 if (pages == -1) {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800144 desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
145 QI_DEV_EIOTLB_SIZE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100146 } else if (pages > 1) {
147 /* The least significant zero bit indicates the size. So,
148 * for example, an "address" value of 0x12345f000 will
149 * flush from 0x123440000 to 0x12347ffff (256KiB). */
150 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
Ingo Molnared7158b2018-02-22 10:54:55 +0100151 unsigned long mask = __rounddown_pow_of_two(address ^ last);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100152
Lu Baolu5d308fc2018-12-10 09:58:58 +0800153 desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |
154 (mask - 1)) | QI_DEV_EIOTLB_SIZE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100155 } else {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800156 desc.qw1 = QI_DEV_EIOTLB_ADDR(address);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100157 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800158 desc.qw2 = 0;
159 desc.qw3 = 0;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100160 qi_submit_sync(&desc, svm->iommu);
161 }
162}
163
164static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
David Woodhouse5d52f482015-10-20 15:52:13 +0100165 unsigned long pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100166{
167 struct intel_svm_dev *sdev;
168
169 rcu_read_lock();
170 list_for_each_entry_rcu(sdev, &svm->devs, list)
David Woodhousee0349922015-10-16 19:36:53 +0100171 intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100172 rcu_read_unlock();
173}
174
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100175/* Pages have been freed at this point */
176static void intel_invalidate_range(struct mmu_notifier *mn,
177 struct mm_struct *mm,
178 unsigned long start, unsigned long end)
179{
180 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
181
182 intel_flush_svm_range(svm, start,
David Woodhousee0349922015-10-16 19:36:53 +0100183 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100184}
185
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100186static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
187{
188 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
David Woodhousee57e58b2016-01-12 19:18:06 +0000189 struct intel_svm_dev *sdev;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100190
David Woodhousee57e58b2016-01-12 19:18:06 +0000191 /* This might end up being called from exit_mmap(), *before* the page
192 * tables are cleared. And __mmu_notifier_release() will delete us from
193 * the list of notifiers so that our invalidate_range() callback doesn't
194 * get called when the page tables are cleared. So we need to protect
195 * against hardware accessing those page tables.
196 *
197 * We do it by clearing the entry in the PASID table and then flushing
198 * the IOTLB and the PASID table caches. This might upset hardware;
199 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
200 * page) so that we end up taking a fault that the hardware really
201 * *has* to handle gracefully without affecting other processes.
202 */
David Woodhousee57e58b2016-01-12 19:18:06 +0000203 rcu_read_lock();
204 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800205 intel_pasid_tear_down_entry(svm->iommu, sdev->dev, svm->pasid);
David Woodhousee57e58b2016-01-12 19:18:06 +0000206 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
207 }
208 rcu_read_unlock();
209
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100210}
211
212static const struct mmu_notifier_ops intel_mmuops = {
213 .release = intel_mm_release,
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100214 .invalidate_range = intel_invalidate_range,
215};
216
217static DEFINE_MUTEX(pasid_mutex);
Lu Baolu51261aa2018-07-14 15:46:55 +0800218static LIST_HEAD(global_svm_list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100219
David Woodhouse0204a492015-10-13 17:18:10 +0100220int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100221{
222 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800223 struct device_domain_info *info;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100224 struct intel_svm_dev *sdev;
225 struct intel_svm *svm = NULL;
David Woodhouse5cec7532015-10-15 15:52:15 +0100226 struct mm_struct *mm = NULL;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100227 int pasid_max;
228 int ret;
229
Lu Baoluc56cba52019-03-01 11:23:12 +0800230 if (!iommu || dmar_disabled)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100231 return -EINVAL;
232
233 if (dev_is_pci(dev)) {
234 pasid_max = pci_max_pasids(to_pci_dev(dev));
235 if (pasid_max < 0)
236 return -EINVAL;
237 } else
238 pasid_max = 1 << 20;
239
Lu Baolubb37f7d2018-05-04 13:08:19 +0800240 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
David Woodhouse5cec7532015-10-15 15:52:15 +0100241 if (!ecap_srs(iommu->ecap))
242 return -EINVAL;
243 } else if (pasid) {
244 mm = get_task_mm(current);
245 BUG_ON(!mm);
246 }
247
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100248 mutex_lock(&pasid_mutex);
David Woodhouse569e4f72015-10-15 13:59:14 +0100249 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
Lu Baolu51261aa2018-07-14 15:46:55 +0800250 struct intel_svm *t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100251
Lu Baolu51261aa2018-07-14 15:46:55 +0800252 list_for_each_entry(t, &global_svm_list, list) {
253 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100254 continue;
255
Lu Baolu51261aa2018-07-14 15:46:55 +0800256 svm = t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100257 if (svm->pasid >= pasid_max) {
258 dev_warn(dev,
259 "Limited PASID width. Cannot use existing PASID %d\n",
260 svm->pasid);
261 ret = -ENOSPC;
262 goto out;
263 }
264
265 list_for_each_entry(sdev, &svm->devs, list) {
266 if (dev == sdev->dev) {
David Woodhouse0204a492015-10-13 17:18:10 +0100267 if (sdev->ops != ops) {
268 ret = -EBUSY;
269 goto out;
270 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100271 sdev->users++;
272 goto success;
273 }
274 }
275
276 break;
277 }
278 }
279
280 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
281 if (!sdev) {
282 ret = -ENOMEM;
283 goto out;
284 }
285 sdev->dev = dev;
286
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800287 ret = intel_iommu_enable_pasid(iommu, dev);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100288 if (ret || !pasid) {
289 /* If they don't actually want to assign a PASID, this is
290 * just an enabling check/preparation. */
291 kfree(sdev);
292 goto out;
293 }
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800294
295 info = dev->archdata.iommu;
296 if (!info || !info->pasid_supported) {
297 kfree(sdev);
298 goto out;
299 }
300
301 sdev->did = FLPT_DEFAULT_DID;
302 sdev->sid = PCI_DEVID(info->bus, info->devfn);
303 if (info->ats_enabled) {
304 sdev->dev_iotlb = 1;
305 sdev->qdep = info->ats_qdep;
306 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
307 sdev->qdep = 0;
308 }
309
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100310 /* Finish the setup now we know we're keeping it */
311 sdev->users = 1;
David Woodhouse0204a492015-10-13 17:18:10 +0100312 sdev->ops = ops;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100313 init_rcu_head(&sdev->rcu);
314
315 if (!svm) {
316 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
317 if (!svm) {
318 ret = -ENOMEM;
319 kfree(sdev);
320 goto out;
321 }
322 svm->iommu = iommu;
323
Lu Baolu4774cc52018-07-14 15:47:01 +0800324 if (pasid_max > intel_pasid_max_id)
325 pasid_max = intel_pasid_max_id;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100326
David Woodhouse5a10ba22015-10-24 21:06:39 +0200327 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
Lu Baoluaf395072018-07-14 15:46:56 +0800328 ret = intel_pasid_alloc_id(svm,
329 !!cap_caching_mode(iommu->cap),
330 pasid_max - 1, GFP_KERNEL);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100331 if (ret < 0) {
332 kfree(svm);
Lu Baolubbe4b3a2018-02-24 13:42:27 +0800333 kfree(sdev);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100334 goto out;
335 }
336 svm->pasid = ret;
337 svm->notifier.ops = &intel_mmuops;
David Woodhouse5cec7532015-10-15 15:52:15 +0100338 svm->mm = mm;
David Woodhouse569e4f72015-10-15 13:59:14 +0100339 svm->flags = flags;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100340 INIT_LIST_HEAD_RCU(&svm->devs);
Lu Baolu51261aa2018-07-14 15:46:55 +0800341 INIT_LIST_HEAD(&svm->list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100342 ret = -ENOMEM;
David Woodhouse5cec7532015-10-15 15:52:15 +0100343 if (mm) {
344 ret = mmu_notifier_register(&svm->notifier, mm);
345 if (ret) {
Lu Baoluaf395072018-07-14 15:46:56 +0800346 intel_pasid_free_id(svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100347 kfree(svm);
348 kfree(sdev);
349 goto out;
350 }
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800351 }
Sohil Mehta2f13eb72017-12-20 11:59:27 -0800352
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800353 spin_lock(&iommu->lock);
354 ret = intel_pasid_setup_first_level(iommu, dev,
355 mm ? mm->pgd : init_mm.pgd,
356 svm->pasid, FLPT_DEFAULT_DID,
357 mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);
358 spin_unlock(&iommu->lock);
359 if (ret) {
360 if (mm)
361 mmu_notifier_unregister(&svm->notifier, mm);
362 intel_pasid_free_id(svm->pasid);
363 kfree(svm);
364 kfree(sdev);
365 goto out;
366 }
Lu Baolu51261aa2018-07-14 15:46:55 +0800367
368 list_add_tail(&svm->list, &global_svm_list);
Jacob Pand7af4d92019-05-08 12:22:46 -0700369 } else {
370 /*
371 * Binding a new device with existing PASID, need to setup
372 * the PASID entry.
373 */
374 spin_lock(&iommu->lock);
375 ret = intel_pasid_setup_first_level(iommu, dev,
376 mm ? mm->pgd : init_mm.pgd,
377 svm->pasid, FLPT_DEFAULT_DID,
378 mm ? 0 : PASID_FLAG_SUPERVISOR_MODE);
379 spin_unlock(&iommu->lock);
380 if (ret) {
381 kfree(sdev);
382 goto out;
383 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100384 }
385 list_add_rcu(&sdev->list, &svm->devs);
386
387 success:
388 *pasid = svm->pasid;
389 ret = 0;
390 out:
391 mutex_unlock(&pasid_mutex);
David Woodhouse5cec7532015-10-15 15:52:15 +0100392 if (mm)
393 mmput(mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100394 return ret;
395}
396EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
397
398int intel_svm_unbind_mm(struct device *dev, int pasid)
399{
400 struct intel_svm_dev *sdev;
401 struct intel_iommu *iommu;
402 struct intel_svm *svm;
403 int ret = -EINVAL;
404
405 mutex_lock(&pasid_mutex);
406 iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800407 if (!iommu)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100408 goto out;
409
Lu Baoluaf395072018-07-14 15:46:56 +0800410 svm = intel_pasid_lookup_id(pasid);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100411 if (!svm)
412 goto out;
413
414 list_for_each_entry(sdev, &svm->devs, list) {
415 if (dev == sdev->dev) {
416 ret = 0;
417 sdev->users--;
418 if (!sdev->users) {
419 list_del_rcu(&sdev->list);
420 /* Flush the PASID cache and IOTLB for this device.
421 * Note that we do depend on the hardware *not* using
422 * the PASID any more. Just as we depend on other
423 * devices never using PASIDs that they have no right
424 * to use. We have a *shared* PASID table, because it's
425 * large and has to be physically contiguous. So it's
426 * hard to be as defensive as we might like. */
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800427 intel_pasid_tear_down_entry(iommu, dev, svm->pasid);
David Woodhousee0349922015-10-16 19:36:53 +0100428 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100429 kfree_rcu(sdev, rcu);
430
431 if (list_empty(&svm->devs)) {
Lu Baoluaf395072018-07-14 15:46:56 +0800432 intel_pasid_free_id(svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100433 if (svm->mm)
David Woodhousee57e58b2016-01-12 19:18:06 +0000434 mmu_notifier_unregister(&svm->notifier, svm->mm);
435
Lu Baolu51261aa2018-07-14 15:46:55 +0800436 list_del(&svm->list);
437
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100438 /* We mandate that no page faults may be outstanding
439 * for the PASID when intel_svm_unbind_mm() is called.
440 * If that is not obeyed, subtle errors will happen.
441 * Let's make them less subtle... */
442 memset(svm, 0x6b, sizeof(*svm));
443 kfree(svm);
444 }
445 }
446 break;
447 }
448 }
449 out:
450 mutex_unlock(&pasid_mutex);
451
452 return ret;
453}
454EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100455
CQ Tang15060ab2017-05-10 11:39:03 -0700456int intel_svm_is_pasid_valid(struct device *dev, int pasid)
457{
458 struct intel_iommu *iommu;
459 struct intel_svm *svm;
460 int ret = -EINVAL;
461
462 mutex_lock(&pasid_mutex);
463 iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800464 if (!iommu)
CQ Tang15060ab2017-05-10 11:39:03 -0700465 goto out;
466
Lu Baoluaf395072018-07-14 15:46:56 +0800467 svm = intel_pasid_lookup_id(pasid);
CQ Tang15060ab2017-05-10 11:39:03 -0700468 if (!svm)
469 goto out;
470
471 /* init_mm is used in this case */
472 if (!svm->mm)
473 ret = 1;
474 else if (atomic_read(&svm->mm->mm_users) > 0)
475 ret = 1;
476 else
477 ret = 0;
478
479 out:
480 mutex_unlock(&pasid_mutex);
481
482 return ret;
483}
484EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid);
485
David Woodhousea222a7f2015-10-07 23:35:18 +0100486/* Page request queue descriptor */
487struct page_req_dsc {
Jacob Pan5b438f42019-01-11 13:04:57 +0800488 union {
489 struct {
490 u64 type:8;
491 u64 pasid_present:1;
492 u64 priv_data_present:1;
493 u64 rsvd:6;
494 u64 rid:16;
495 u64 pasid:20;
496 u64 exe_req:1;
497 u64 pm_req:1;
498 u64 rsvd2:10;
499 };
500 u64 qw_0;
501 };
502 union {
503 struct {
504 u64 rd_req:1;
505 u64 wr_req:1;
506 u64 lpig:1;
507 u64 prg_index:9;
508 u64 addr:52;
509 };
510 u64 qw_1;
511 };
512 u64 priv_data[2];
David Woodhousea222a7f2015-10-07 23:35:18 +0100513};
514
515#define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100516
517static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
518{
519 unsigned long requested = 0;
520
521 if (req->exe_req)
522 requested |= VM_EXEC;
523
524 if (req->rd_req)
525 requested |= VM_READ;
526
527 if (req->wr_req)
528 requested |= VM_WRITE;
529
530 return (requested & ~vma->vm_flags) != 0;
531}
532
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700533static bool is_canonical_address(u64 addr)
534{
535 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
536 long saddr = (long) addr;
537
538 return (((saddr << shift) >> shift) == saddr);
539}
540
David Woodhousea222a7f2015-10-07 23:35:18 +0100541static irqreturn_t prq_event_thread(int irq, void *d)
542{
543 struct intel_iommu *iommu = d;
544 struct intel_svm *svm = NULL;
545 int head, tail, handled = 0;
546
David Woodhouse46924002016-02-15 12:42:38 +0000547 /* Clear PPR bit before reading head/tail registers, to
548 * ensure that we get a new interrupt if needed. */
549 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
550
David Woodhousea222a7f2015-10-07 23:35:18 +0100551 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
552 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
553 while (head != tail) {
David Woodhouse0204a492015-10-13 17:18:10 +0100554 struct intel_svm_dev *sdev;
David Woodhousea222a7f2015-10-07 23:35:18 +0100555 struct vm_area_struct *vma;
556 struct page_req_dsc *req;
557 struct qi_desc resp;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700558 int result;
559 vm_fault_t ret;
David Woodhousea222a7f2015-10-07 23:35:18 +0100560 u64 address;
561
562 handled = 1;
563
564 req = &iommu->prq[head / sizeof(*req)];
565
566 result = QI_RESP_FAILURE;
David Woodhouse7f92a2e2015-10-16 17:22:31 +0100567 address = (u64)req->addr << VTD_PAGE_SHIFT;
David Woodhousea222a7f2015-10-07 23:35:18 +0100568 if (!req->pasid_present) {
569 pr_err("%s: Page request without PASID: %08llx %08llx\n",
570 iommu->name, ((unsigned long long *)req)[0],
571 ((unsigned long long *)req)[1]);
Lu Baolu19ed3e22018-11-05 10:18:58 +0800572 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100573 }
574
575 if (!svm || svm->pasid != req->pasid) {
576 rcu_read_lock();
Lu Baoluaf395072018-07-14 15:46:56 +0800577 svm = intel_pasid_lookup_id(req->pasid);
David Woodhousea222a7f2015-10-07 23:35:18 +0100578 /* It *can't* go away, because the driver is not permitted
579 * to unbind the mm while any page faults are outstanding.
580 * So we only need RCU to protect the internal idr code. */
581 rcu_read_unlock();
582
583 if (!svm) {
584 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
585 iommu->name, req->pasid, ((unsigned long long *)req)[0],
586 ((unsigned long long *)req)[1]);
David Woodhouse26322ab2015-10-15 21:12:56 +0100587 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100588 }
589 }
590
591 result = QI_RESP_INVALID;
David Woodhouse5cec7532015-10-15 15:52:15 +0100592 /* Since we're using init_mm.pgd directly, we should never take
593 * any faults on kernel addresses. */
594 if (!svm->mm)
595 goto bad_req;
David Woodhousee57e58b2016-01-12 19:18:06 +0000596 /* If the mm is already defunct, don't handle faults. */
Vegard Nossum388f7932017-02-27 14:30:13 -0800597 if (!mmget_not_zero(svm->mm))
David Woodhousee57e58b2016-01-12 19:18:06 +0000598 goto bad_req;
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700599
600 /* If address is not canonical, return invalid response */
601 if (!is_canonical_address(address))
602 goto bad_req;
603
David Woodhousea222a7f2015-10-07 23:35:18 +0100604 down_read(&svm->mm->mmap_sem);
605 vma = find_extend_vma(svm->mm, address);
606 if (!vma || address < vma->vm_start)
607 goto invalid;
608
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100609 if (access_error(vma, req))
610 goto invalid;
611
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700612 ret = handle_mm_fault(vma, address,
David Woodhousea222a7f2015-10-07 23:35:18 +0100613 req->wr_req ? FAULT_FLAG_WRITE : 0);
614 if (ret & VM_FAULT_ERROR)
615 goto invalid;
616
617 result = QI_RESP_SUCCESS;
618 invalid:
619 up_read(&svm->mm->mmap_sem);
David Woodhousee57e58b2016-01-12 19:18:06 +0000620 mmput(svm->mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100621 bad_req:
622 /* Accounting for major/minor faults? */
David Woodhouse0204a492015-10-13 17:18:10 +0100623 rcu_read_lock();
624 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Jacob Pan5b438f42019-01-11 13:04:57 +0800625 if (sdev->sid == req->rid)
David Woodhouse0204a492015-10-13 17:18:10 +0100626 break;
627 }
628 /* Other devices can go away, but the drivers are not permitted
629 * to unbind while any page faults might be in flight. So it's
630 * OK to drop the 'lock' here now we have it. */
631 rcu_read_unlock();
632
633 if (WARN_ON(&sdev->list == &svm->devs))
634 sdev = NULL;
635
636 if (sdev && sdev->ops && sdev->ops->fault_cb) {
637 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800638 (req->exe_req << 1) | (req->pm_req);
639 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
640 req->priv_data, rwxp, result);
David Woodhouse0204a492015-10-13 17:18:10 +0100641 }
David Woodhouse26322ab2015-10-15 21:12:56 +0100642 /* We get here in the error case where the PASID lookup failed,
643 and these can be NULL. Do not use them below this point! */
644 sdev = NULL;
645 svm = NULL;
646 no_pasid:
Jacob Pan5b438f42019-01-11 13:04:57 +0800647 if (req->lpig || req->priv_data_present) {
648 /*
649 * Per VT-d spec. v3.0 ch7.7, system software must
650 * respond with page group response if private data
651 * is present (PDP) or last page in group (LPIG) bit
652 * is set. This is an additional VT-d feature beyond
653 * PCI ATS spec.
654 */
Lu Baolu5d308fc2018-12-10 09:58:58 +0800655 resp.qw0 = QI_PGRP_PASID(req->pasid) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800656 QI_PGRP_DID(req->rid) |
David Woodhousea222a7f2015-10-07 23:35:18 +0100657 QI_PGRP_PASID_P(req->pasid_present) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800658 QI_PGRP_PDP(req->pasid_present) |
659 QI_PGRP_RESP_CODE(result) |
David Woodhousea222a7f2015-10-07 23:35:18 +0100660 QI_PGRP_RESP_TYPE;
Lu Baolu5d308fc2018-12-10 09:58:58 +0800661 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800662 QI_PGRP_LPIG(req->lpig);
663
664 if (req->priv_data_present)
665 memcpy(&resp.qw2, req->priv_data,
666 sizeof(req->priv_data));
David Woodhousea222a7f2015-10-07 23:35:18 +0100667 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800668 resp.qw2 = 0;
669 resp.qw3 = 0;
670 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100671
672 head = (head + sizeof(*req)) & PRQ_RING_MASK;
673 }
674
675 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
676
677 return IRQ_RETVAL(handled);
678}