blob: db301efe126d4ac9cf2aeb606489565135db7e7d [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>
Souptick Joarder50a7ca32018-08-17 15:44:47 -070027#include <linux/mm_types.h>
Ashok Raj9d8c3af2017-08-08 13:29:27 -070028#include <asm/page.h>
David Woodhousea222a7f2015-10-07 23:35:18 +010029
Lu Baoluaf395072018-07-14 15:46:56 +080030#include "intel-pasid.h"
31
Sohil Mehta2f13eb72017-12-20 11:59:27 -080032#define PASID_ENTRY_P BIT_ULL(0)
33#define PASID_ENTRY_FLPM_5LP BIT_ULL(9)
34#define PASID_ENTRY_SRE BIT_ULL(11)
35
David Woodhousea222a7f2015-10-07 23:35:18 +010036static irqreturn_t prq_event_thread(int irq, void *d);
David Woodhouse2f26e0a2015-09-09 11:40:47 +010037
David Woodhouse907fea32015-10-13 14:11:13 +010038struct pasid_state_entry {
39 u64 val;
40};
41
Lu Baolud9737952018-07-14 15:47:02 +080042int intel_svm_init(struct intel_iommu *iommu)
David Woodhouse8a94ade2015-03-24 14:54:56 +000043{
44 struct page *pages;
45 int order;
46
Sohil Mehta59103ca2017-12-20 11:59:25 -080047 if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
48 !cap_fl1gp_support(iommu->cap))
49 return -EINVAL;
50
Sohil Mehtaf1ac10c2017-12-20 11:59:26 -080051 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
52 !cap_5lp_support(iommu->cap))
53 return -EINVAL;
54
David Woodhouse91017042016-09-12 10:49:11 +080055 /* Start at 2 because it's defined as 2^(1+PSS) */
56 iommu->pasid_max = 2 << ecap_pss(iommu->ecap);
David Woodhouse8a94ade2015-03-24 14:54:56 +000057
David Woodhouse91017042016-09-12 10:49:11 +080058 /* Eventually I'm promised we will get a multi-level PASID table
59 * and it won't have to be physically contiguous. Until then,
60 * limit the size because 8MiB contiguous allocations can be hard
61 * to come by. The limit of 0x20000, which is 1MiB for each of
62 * the PASID and PASID-state tables, is somewhat arbitrary. */
63 if (iommu->pasid_max > 0x20000)
64 iommu->pasid_max = 0x20000;
65
66 order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);
David Woodhouse8a94ade2015-03-24 14:54:56 +000067 if (ecap_dis(iommu->ecap)) {
David Woodhouse91017042016-09-12 10:49:11 +080068 /* Just making it explicit... */
69 BUILD_BUG_ON(sizeof(struct pasid_entry) != sizeof(struct pasid_state_entry));
David Woodhouse8a94ade2015-03-24 14:54:56 +000070 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
71 if (pages)
72 iommu->pasid_state_table = page_address(pages);
73 else
74 pr_warn("IOMMU: %s: Failed to allocate PASID state table\n",
75 iommu->name);
76 }
77
78 return 0;
79}
80
Lu Baolud9737952018-07-14 15:47:02 +080081int intel_svm_exit(struct intel_iommu *iommu)
David Woodhouse8a94ade2015-03-24 14:54:56 +000082{
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
David Woodhouse8a94ade2015-03-24 14:54:56 +000085 if (iommu->pasid_state_table) {
86 free_pages((unsigned long)iommu->pasid_state_table, order);
87 iommu->pasid_state_table = NULL;
88 }
Lu Baoluaf395072018-07-14 15:46:56 +080089
David Woodhouse8a94ade2015-03-24 14:54:56 +000090 return 0;
91}
David Woodhouse2f26e0a2015-09-09 11:40:47 +010092
David Woodhousea222a7f2015-10-07 23:35:18 +010093#define PRQ_ORDER 0
94
95int intel_svm_enable_prq(struct intel_iommu *iommu)
96{
97 struct page *pages;
98 int irq, ret;
99
100 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
101 if (!pages) {
102 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
103 iommu->name);
104 return -ENOMEM;
105 }
106 iommu->prq = page_address(pages);
107
108 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
109 if (irq <= 0) {
110 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
111 iommu->name);
112 ret = -EINVAL;
113 err:
114 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
115 iommu->prq = NULL;
116 return ret;
117 }
118 iommu->pr_irq = irq;
119
120 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
121
122 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
123 iommu->prq_name, iommu);
124 if (ret) {
125 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
126 iommu->name);
127 dmar_free_hwirq(irq);
Jerry Snitselaar72d54812017-12-20 09:48:56 -0700128 iommu->pr_irq = 0;
David Woodhousea222a7f2015-10-07 23:35:18 +0100129 goto err;
130 }
131 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
132 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
133 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
134
135 return 0;
136}
137
138int intel_svm_finish_prq(struct intel_iommu *iommu)
139{
140 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
141 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
142 dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
143
Jerry Snitselaar72d54812017-12-20 09:48:56 -0700144 if (iommu->pr_irq) {
145 free_irq(iommu->pr_irq, iommu);
146 dmar_free_hwirq(iommu->pr_irq);
147 iommu->pr_irq = 0;
148 }
David Woodhousea222a7f2015-10-07 23:35:18 +0100149
150 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
151 iommu->prq = NULL;
152
153 return 0;
154}
155
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100156static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
David Woodhouse5d52f482015-10-20 15:52:13 +0100157 unsigned long address, unsigned long pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100158{
159 struct qi_desc desc;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100160
David Woodhouse5d52f482015-10-20 15:52:13 +0100161 if (pages == -1) {
David Woodhousee0349922015-10-16 19:36:53 +0100162 /* For global kernel pages we have to flush them in *all* PASIDs
163 * because that's the only option the hardware gives us. Despite
164 * the fact that they are actually only accessible through one. */
165 if (gl)
166 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
167 QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) | QI_EIOTLB_TYPE;
168 else
169 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
170 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100171 desc.high = 0;
172 } else {
David Woodhouse5d52f482015-10-20 15:52:13 +0100173 int mask = ilog2(__roundup_pow_of_two(pages));
174
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100175 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
176 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | QI_EIOTLB_TYPE;
David Woodhousee0349922015-10-16 19:36:53 +0100177 desc.high = QI_EIOTLB_ADDR(address) | QI_EIOTLB_GL(gl) |
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100178 QI_EIOTLB_IH(ih) | QI_EIOTLB_AM(mask);
179 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100180 qi_submit_sync(&desc, svm->iommu);
181
182 if (sdev->dev_iotlb) {
183 desc.low = QI_DEV_EIOTLB_PASID(svm->pasid) | QI_DEV_EIOTLB_SID(sdev->sid) |
184 QI_DEV_EIOTLB_QDEP(sdev->qdep) | QI_DEIOTLB_TYPE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100185 if (pages == -1) {
186 desc.high = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | QI_DEV_EIOTLB_SIZE;
187 } else if (pages > 1) {
188 /* The least significant zero bit indicates the size. So,
189 * for example, an "address" value of 0x12345f000 will
190 * flush from 0x123440000 to 0x12347ffff (256KiB). */
191 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
Ingo Molnared7158b2018-02-22 10:54:55 +0100192 unsigned long mask = __rounddown_pow_of_two(address ^ last);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100193
David Woodhouse5d52f482015-10-20 15:52:13 +0100194 desc.high = QI_DEV_EIOTLB_ADDR((address & ~mask) | (mask - 1)) | QI_DEV_EIOTLB_SIZE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100195 } else {
196 desc.high = QI_DEV_EIOTLB_ADDR(address);
197 }
198 qi_submit_sync(&desc, svm->iommu);
199 }
200}
201
202static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
David Woodhouse5d52f482015-10-20 15:52:13 +0100203 unsigned long pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100204{
205 struct intel_svm_dev *sdev;
206
David Woodhouse907fea32015-10-13 14:11:13 +0100207 /* Try deferred invalidate if available */
208 if (svm->iommu->pasid_state_table &&
209 !cmpxchg64(&svm->iommu->pasid_state_table[svm->pasid].val, 0, 1ULL << 63))
210 return;
211
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100212 rcu_read_lock();
213 list_for_each_entry_rcu(sdev, &svm->devs, list)
David Woodhousee0349922015-10-16 19:36:53 +0100214 intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100215 rcu_read_unlock();
216}
217
218static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm,
219 unsigned long address, pte_t pte)
220{
221 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
222
David Woodhousee0349922015-10-16 19:36:53 +0100223 intel_flush_svm_range(svm, address, 1, 1, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100224}
225
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100226/* Pages have been freed at this point */
227static void intel_invalidate_range(struct mmu_notifier *mn,
228 struct mm_struct *mm,
229 unsigned long start, unsigned long end)
230{
231 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
232
233 intel_flush_svm_range(svm, start,
David Woodhousee0349922015-10-16 19:36:53 +0100234 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100235}
236
237
David Woodhouse5a10ba22015-10-24 21:06:39 +0200238static void intel_flush_pasid_dev(struct intel_svm *svm, struct intel_svm_dev *sdev, int pasid)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100239{
240 struct qi_desc desc;
241
242 desc.high = 0;
David Woodhouse5a10ba22015-10-24 21:06:39 +0200243 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 +0100244
245 qi_submit_sync(&desc, svm->iommu);
246}
247
248static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
249{
250 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
David Woodhousee57e58b2016-01-12 19:18:06 +0000251 struct intel_svm_dev *sdev;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100252
David Woodhousee57e58b2016-01-12 19:18:06 +0000253 /* This might end up being called from exit_mmap(), *before* the page
254 * tables are cleared. And __mmu_notifier_release() will delete us from
255 * the list of notifiers so that our invalidate_range() callback doesn't
256 * get called when the page tables are cleared. So we need to protect
257 * against hardware accessing those page tables.
258 *
259 * We do it by clearing the entry in the PASID table and then flushing
260 * the IOTLB and the PASID table caches. This might upset hardware;
261 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
262 * page) so that we end up taking a fault that the hardware really
263 * *has* to handle gracefully without affecting other processes.
264 */
David Woodhousee57e58b2016-01-12 19:18:06 +0000265 rcu_read_lock();
266 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Lu Baolu4774cc52018-07-14 15:47:01 +0800267 intel_pasid_clear_entry(sdev->dev, svm->pasid);
David Woodhousee57e58b2016-01-12 19:18:06 +0000268 intel_flush_pasid_dev(svm, sdev, svm->pasid);
269 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
270 }
271 rcu_read_unlock();
272
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100273}
274
275static const struct mmu_notifier_ops intel_mmuops = {
276 .release = intel_mm_release,
277 .change_pte = intel_change_pte,
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100278 .invalidate_range = intel_invalidate_range,
279};
280
281static DEFINE_MUTEX(pasid_mutex);
Lu Baolu51261aa2018-07-14 15:46:55 +0800282static LIST_HEAD(global_svm_list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100283
David Woodhouse0204a492015-10-13 17:18:10 +0100284int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100285{
286 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800287 struct pasid_entry *entry;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100288 struct intel_svm_dev *sdev;
289 struct intel_svm *svm = NULL;
David Woodhouse5cec7532015-10-15 15:52:15 +0100290 struct mm_struct *mm = NULL;
Sohil Mehta2f13eb72017-12-20 11:59:27 -0800291 u64 pasid_entry_val;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100292 int pasid_max;
293 int ret;
294
Lu Baolu4774cc52018-07-14 15:47:01 +0800295 if (!iommu)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100296 return -EINVAL;
297
298 if (dev_is_pci(dev)) {
299 pasid_max = pci_max_pasids(to_pci_dev(dev));
300 if (pasid_max < 0)
301 return -EINVAL;
302 } else
303 pasid_max = 1 << 20;
304
Lu Baolubb37f7d2018-05-04 13:08:19 +0800305 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
David Woodhouse5cec7532015-10-15 15:52:15 +0100306 if (!ecap_srs(iommu->ecap))
307 return -EINVAL;
308 } else if (pasid) {
309 mm = get_task_mm(current);
310 BUG_ON(!mm);
311 }
312
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100313 mutex_lock(&pasid_mutex);
David Woodhouse569e4f72015-10-15 13:59:14 +0100314 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
Lu Baolu51261aa2018-07-14 15:46:55 +0800315 struct intel_svm *t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100316
Lu Baolu51261aa2018-07-14 15:46:55 +0800317 list_for_each_entry(t, &global_svm_list, list) {
318 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100319 continue;
320
Lu Baolu51261aa2018-07-14 15:46:55 +0800321 svm = t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100322 if (svm->pasid >= pasid_max) {
323 dev_warn(dev,
324 "Limited PASID width. Cannot use existing PASID %d\n",
325 svm->pasid);
326 ret = -ENOSPC;
327 goto out;
328 }
329
330 list_for_each_entry(sdev, &svm->devs, list) {
331 if (dev == sdev->dev) {
David Woodhouse0204a492015-10-13 17:18:10 +0100332 if (sdev->ops != ops) {
333 ret = -EBUSY;
334 goto out;
335 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100336 sdev->users++;
337 goto success;
338 }
339 }
340
341 break;
342 }
343 }
344
345 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
346 if (!sdev) {
347 ret = -ENOMEM;
348 goto out;
349 }
350 sdev->dev = dev;
351
352 ret = intel_iommu_enable_pasid(iommu, sdev);
353 if (ret || !pasid) {
354 /* If they don't actually want to assign a PASID, this is
355 * just an enabling check/preparation. */
356 kfree(sdev);
357 goto out;
358 }
359 /* Finish the setup now we know we're keeping it */
360 sdev->users = 1;
David Woodhouse0204a492015-10-13 17:18:10 +0100361 sdev->ops = ops;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100362 init_rcu_head(&sdev->rcu);
363
364 if (!svm) {
365 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
366 if (!svm) {
367 ret = -ENOMEM;
368 kfree(sdev);
369 goto out;
370 }
371 svm->iommu = iommu;
372
Lu Baolu4774cc52018-07-14 15:47:01 +0800373 if (pasid_max > intel_pasid_max_id)
374 pasid_max = intel_pasid_max_id;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100375
David Woodhouse5a10ba22015-10-24 21:06:39 +0200376 /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
Lu Baoluaf395072018-07-14 15:46:56 +0800377 ret = intel_pasid_alloc_id(svm,
378 !!cap_caching_mode(iommu->cap),
379 pasid_max - 1, GFP_KERNEL);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100380 if (ret < 0) {
381 kfree(svm);
Lu Baolubbe4b3a2018-02-24 13:42:27 +0800382 kfree(sdev);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100383 goto out;
384 }
385 svm->pasid = ret;
386 svm->notifier.ops = &intel_mmuops;
David Woodhouse5cec7532015-10-15 15:52:15 +0100387 svm->mm = mm;
David Woodhouse569e4f72015-10-15 13:59:14 +0100388 svm->flags = flags;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100389 INIT_LIST_HEAD_RCU(&svm->devs);
Lu Baolu51261aa2018-07-14 15:46:55 +0800390 INIT_LIST_HEAD(&svm->list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100391 ret = -ENOMEM;
David Woodhouse5cec7532015-10-15 15:52:15 +0100392 if (mm) {
393 ret = mmu_notifier_register(&svm->notifier, mm);
394 if (ret) {
Lu Baoluaf395072018-07-14 15:46:56 +0800395 intel_pasid_free_id(svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100396 kfree(svm);
397 kfree(sdev);
398 goto out;
399 }
Sohil Mehta2f13eb72017-12-20 11:59:27 -0800400 pasid_entry_val = (u64)__pa(mm->pgd) | PASID_ENTRY_P;
David Woodhouse5cec7532015-10-15 15:52:15 +0100401 } else
Sohil Mehta2f13eb72017-12-20 11:59:27 -0800402 pasid_entry_val = (u64)__pa(init_mm.pgd) |
403 PASID_ENTRY_P | PASID_ENTRY_SRE;
404 if (cpu_feature_enabled(X86_FEATURE_LA57))
405 pasid_entry_val |= PASID_ENTRY_FLPM_5LP;
406
Lu Baolu4774cc52018-07-14 15:47:01 +0800407 entry = intel_pasid_get_entry(dev, svm->pasid);
408 entry->val = pasid_entry_val;
Sohil Mehta2f13eb72017-12-20 11:59:27 -0800409
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100410 wmb();
Lu Baolu97140102018-03-16 12:31:36 +0800411
412 /*
413 * Flush PASID cache when a PASID table entry becomes
414 * present.
415 */
David Woodhouse5a10ba22015-10-24 21:06:39 +0200416 if (cap_caching_mode(iommu->cap))
Lu Baolu97140102018-03-16 12:31:36 +0800417 intel_flush_pasid_dev(svm, sdev, svm->pasid);
Lu Baolu51261aa2018-07-14 15:46:55 +0800418
419 list_add_tail(&svm->list, &global_svm_list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100420 }
421 list_add_rcu(&sdev->list, &svm->devs);
422
423 success:
424 *pasid = svm->pasid;
425 ret = 0;
426 out:
427 mutex_unlock(&pasid_mutex);
David Woodhouse5cec7532015-10-15 15:52:15 +0100428 if (mm)
429 mmput(mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100430 return ret;
431}
432EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
433
434int intel_svm_unbind_mm(struct device *dev, int pasid)
435{
436 struct intel_svm_dev *sdev;
437 struct intel_iommu *iommu;
438 struct intel_svm *svm;
439 int ret = -EINVAL;
440
441 mutex_lock(&pasid_mutex);
442 iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800443 if (!iommu)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100444 goto out;
445
Lu Baoluaf395072018-07-14 15:46:56 +0800446 svm = intel_pasid_lookup_id(pasid);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100447 if (!svm)
448 goto out;
449
450 list_for_each_entry(sdev, &svm->devs, list) {
451 if (dev == sdev->dev) {
452 ret = 0;
453 sdev->users--;
454 if (!sdev->users) {
455 list_del_rcu(&sdev->list);
456 /* Flush the PASID cache and IOTLB for this device.
457 * Note that we do depend on the hardware *not* using
458 * the PASID any more. Just as we depend on other
459 * devices never using PASIDs that they have no right
460 * to use. We have a *shared* PASID table, because it's
461 * large and has to be physically contiguous. So it's
462 * hard to be as defensive as we might like. */
David Woodhouse5a10ba22015-10-24 21:06:39 +0200463 intel_flush_pasid_dev(svm, sdev, svm->pasid);
David Woodhousee0349922015-10-16 19:36:53 +0100464 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100465 kfree_rcu(sdev, rcu);
Lu Baolu4774cc52018-07-14 15:47:01 +0800466 intel_pasid_clear_entry(dev, svm->pasid);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100467
468 if (list_empty(&svm->devs)) {
Lu Baoluaf395072018-07-14 15:46:56 +0800469 intel_pasid_free_id(svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100470 if (svm->mm)
David Woodhousee57e58b2016-01-12 19:18:06 +0000471 mmu_notifier_unregister(&svm->notifier, svm->mm);
472
Lu Baolu51261aa2018-07-14 15:46:55 +0800473 list_del(&svm->list);
474
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100475 /* We mandate that no page faults may be outstanding
476 * for the PASID when intel_svm_unbind_mm() is called.
477 * If that is not obeyed, subtle errors will happen.
478 * Let's make them less subtle... */
479 memset(svm, 0x6b, sizeof(*svm));
480 kfree(svm);
481 }
482 }
483 break;
484 }
485 }
486 out:
487 mutex_unlock(&pasid_mutex);
488
489 return ret;
490}
491EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100492
CQ Tang15060ab2017-05-10 11:39:03 -0700493int intel_svm_is_pasid_valid(struct device *dev, int pasid)
494{
495 struct intel_iommu *iommu;
496 struct intel_svm *svm;
497 int ret = -EINVAL;
498
499 mutex_lock(&pasid_mutex);
500 iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800501 if (!iommu)
CQ Tang15060ab2017-05-10 11:39:03 -0700502 goto out;
503
Lu Baoluaf395072018-07-14 15:46:56 +0800504 svm = intel_pasid_lookup_id(pasid);
CQ Tang15060ab2017-05-10 11:39:03 -0700505 if (!svm)
506 goto out;
507
508 /* init_mm is used in this case */
509 if (!svm->mm)
510 ret = 1;
511 else if (atomic_read(&svm->mm->mm_users) > 0)
512 ret = 1;
513 else
514 ret = 0;
515
516 out:
517 mutex_unlock(&pasid_mutex);
518
519 return ret;
520}
521EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid);
522
David Woodhousea222a7f2015-10-07 23:35:18 +0100523/* Page request queue descriptor */
524struct page_req_dsc {
525 u64 srr:1;
526 u64 bof:1;
527 u64 pasid_present:1;
528 u64 lpig:1;
529 u64 pasid:20;
530 u64 bus:8;
531 u64 private:23;
532 u64 prg_index:9;
533 u64 rd_req:1;
534 u64 wr_req:1;
535 u64 exe_req:1;
536 u64 priv_req:1;
537 u64 devfn:8;
538 u64 addr:52;
539};
540
541#define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100542
543static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
544{
545 unsigned long requested = 0;
546
547 if (req->exe_req)
548 requested |= VM_EXEC;
549
550 if (req->rd_req)
551 requested |= VM_READ;
552
553 if (req->wr_req)
554 requested |= VM_WRITE;
555
556 return (requested & ~vma->vm_flags) != 0;
557}
558
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700559static bool is_canonical_address(u64 addr)
560{
561 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
562 long saddr = (long) addr;
563
564 return (((saddr << shift) >> shift) == saddr);
565}
566
David Woodhousea222a7f2015-10-07 23:35:18 +0100567static irqreturn_t prq_event_thread(int irq, void *d)
568{
569 struct intel_iommu *iommu = d;
570 struct intel_svm *svm = NULL;
571 int head, tail, handled = 0;
572
David Woodhouse46924002016-02-15 12:42:38 +0000573 /* Clear PPR bit before reading head/tail registers, to
574 * ensure that we get a new interrupt if needed. */
575 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
576
David Woodhousea222a7f2015-10-07 23:35:18 +0100577 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
578 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
579 while (head != tail) {
David Woodhouse0204a492015-10-13 17:18:10 +0100580 struct intel_svm_dev *sdev;
David Woodhousea222a7f2015-10-07 23:35:18 +0100581 struct vm_area_struct *vma;
582 struct page_req_dsc *req;
583 struct qi_desc resp;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700584 int result;
585 vm_fault_t ret;
David Woodhousea222a7f2015-10-07 23:35:18 +0100586 u64 address;
587
588 handled = 1;
589
590 req = &iommu->prq[head / sizeof(*req)];
591
592 result = QI_RESP_FAILURE;
David Woodhouse7f92a2e2015-10-16 17:22:31 +0100593 address = (u64)req->addr << VTD_PAGE_SHIFT;
David Woodhousea222a7f2015-10-07 23:35:18 +0100594 if (!req->pasid_present) {
595 pr_err("%s: Page request without PASID: %08llx %08llx\n",
596 iommu->name, ((unsigned long long *)req)[0],
597 ((unsigned long long *)req)[1]);
598 goto bad_req;
599 }
600
601 if (!svm || svm->pasid != req->pasid) {
602 rcu_read_lock();
Lu Baoluaf395072018-07-14 15:46:56 +0800603 svm = intel_pasid_lookup_id(req->pasid);
David Woodhousea222a7f2015-10-07 23:35:18 +0100604 /* It *can't* go away, because the driver is not permitted
605 * to unbind the mm while any page faults are outstanding.
606 * So we only need RCU to protect the internal idr code. */
607 rcu_read_unlock();
608
609 if (!svm) {
610 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
611 iommu->name, req->pasid, ((unsigned long long *)req)[0],
612 ((unsigned long long *)req)[1]);
David Woodhouse26322ab2015-10-15 21:12:56 +0100613 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100614 }
615 }
616
617 result = QI_RESP_INVALID;
David Woodhouse5cec7532015-10-15 15:52:15 +0100618 /* Since we're using init_mm.pgd directly, we should never take
619 * any faults on kernel addresses. */
620 if (!svm->mm)
621 goto bad_req;
David Woodhousee57e58b2016-01-12 19:18:06 +0000622 /* If the mm is already defunct, don't handle faults. */
Vegard Nossum388f7932017-02-27 14:30:13 -0800623 if (!mmget_not_zero(svm->mm))
David Woodhousee57e58b2016-01-12 19:18:06 +0000624 goto bad_req;
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700625
626 /* If address is not canonical, return invalid response */
627 if (!is_canonical_address(address))
628 goto bad_req;
629
David Woodhousea222a7f2015-10-07 23:35:18 +0100630 down_read(&svm->mm->mmap_sem);
631 vma = find_extend_vma(svm->mm, address);
632 if (!vma || address < vma->vm_start)
633 goto invalid;
634
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100635 if (access_error(vma, req))
636 goto invalid;
637
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700638 ret = handle_mm_fault(vma, address,
David Woodhousea222a7f2015-10-07 23:35:18 +0100639 req->wr_req ? FAULT_FLAG_WRITE : 0);
640 if (ret & VM_FAULT_ERROR)
641 goto invalid;
642
643 result = QI_RESP_SUCCESS;
644 invalid:
645 up_read(&svm->mm->mmap_sem);
David Woodhousee57e58b2016-01-12 19:18:06 +0000646 mmput(svm->mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100647 bad_req:
648 /* Accounting for major/minor faults? */
David Woodhouse0204a492015-10-13 17:18:10 +0100649 rcu_read_lock();
650 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Dan Carpenter3c7c2f32015-10-17 08:18:47 +0300651 if (sdev->sid == PCI_DEVID(req->bus, req->devfn))
David Woodhouse0204a492015-10-13 17:18:10 +0100652 break;
653 }
654 /* Other devices can go away, but the drivers are not permitted
655 * to unbind while any page faults might be in flight. So it's
656 * OK to drop the 'lock' here now we have it. */
657 rcu_read_unlock();
658
659 if (WARN_ON(&sdev->list == &svm->devs))
660 sdev = NULL;
661
662 if (sdev && sdev->ops && sdev->ops->fault_cb) {
663 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
David Woodhouse0bdec952015-10-28 15:14:09 +0900664 (req->exe_req << 1) | (req->priv_req);
David Woodhouse0204a492015-10-13 17:18:10 +0100665 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, req->private, rwxp, result);
666 }
David Woodhouse26322ab2015-10-15 21:12:56 +0100667 /* We get here in the error case where the PASID lookup failed,
668 and these can be NULL. Do not use them below this point! */
669 sdev = NULL;
670 svm = NULL;
671 no_pasid:
David Woodhousea222a7f2015-10-07 23:35:18 +0100672 if (req->lpig) {
673 /* Page Group Response */
674 resp.low = QI_PGRP_PASID(req->pasid) |
675 QI_PGRP_DID((req->bus << 8) | req->devfn) |
676 QI_PGRP_PASID_P(req->pasid_present) |
677 QI_PGRP_RESP_TYPE;
678 resp.high = QI_PGRP_IDX(req->prg_index) |
679 QI_PGRP_PRIV(req->private) | QI_PGRP_RESP_CODE(result);
680
David Woodhouse26322ab2015-10-15 21:12:56 +0100681 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100682 } else if (req->srr) {
683 /* Page Stream Response */
684 resp.low = QI_PSTRM_IDX(req->prg_index) |
685 QI_PSTRM_PRIV(req->private) | QI_PSTRM_BUS(req->bus) |
686 QI_PSTRM_PASID(req->pasid) | QI_PSTRM_RESP_TYPE;
687 resp.high = QI_PSTRM_ADDR(address) | QI_PSTRM_DEVFN(req->devfn) |
688 QI_PSTRM_RESP_CODE(result);
689
David Woodhouse26322ab2015-10-15 21:12:56 +0100690 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100691 }
692
693 head = (head + sizeof(*req)) & PRQ_RING_MASK;
694 }
695
696 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
697
698 return IRQ_RETVAL(handled);
699}