blob: da73d2b13f5479a35c1583387b0db5228a2442c6 [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>
19#include <linux/slab.h>
20#include <linux/intel-svm.h>
21#include <linux/rculist.h>
22#include <linux/pci.h>
23#include <linux/pci-ats.h>
David Woodhousea222a7f2015-10-07 23:35:18 +010024#include <linux/dmar.h>
25#include <linux/interrupt.h>
26
27static irqreturn_t prq_event_thread(int irq, void *d);
David Woodhouse2f26e0a2015-09-09 11:40:47 +010028
29struct pasid_entry {
30 u64 val;
31};
David Woodhouse8a94ade2015-03-24 14:54:56 +000032
David Woodhouse907fea32015-10-13 14:11:13 +010033struct pasid_state_entry {
34 u64 val;
35};
36
David Woodhouse8a94ade2015-03-24 14:54:56 +000037int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
38{
39 struct page *pages;
40 int order;
41
42 order = ecap_pss(iommu->ecap) + 7 - PAGE_SHIFT;
43 if (order < 0)
44 order = 0;
45
46 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
47 if (!pages) {
48 pr_warn("IOMMU: %s: Failed to allocate PASID table\n",
49 iommu->name);
50 return -ENOMEM;
51 }
52 iommu->pasid_table = page_address(pages);
53 pr_info("%s: Allocated order %d PASID table.\n", iommu->name, order);
54
55 if (ecap_dis(iommu->ecap)) {
56 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
57 if (pages)
58 iommu->pasid_state_table = page_address(pages);
59 else
60 pr_warn("IOMMU: %s: Failed to allocate PASID state table\n",
61 iommu->name);
62 }
63
David Woodhouse2f26e0a2015-09-09 11:40:47 +010064 idr_init(&iommu->pasid_idr);
65
David Woodhouse8a94ade2015-03-24 14:54:56 +000066 return 0;
67}
68
69int intel_svm_free_pasid_tables(struct intel_iommu *iommu)
70{
71 int order;
72
73 order = ecap_pss(iommu->ecap) + 7 - PAGE_SHIFT;
74 if (order < 0)
75 order = 0;
76
77 if (iommu->pasid_table) {
78 free_pages((unsigned long)iommu->pasid_table, order);
79 iommu->pasid_table = NULL;
80 }
81 if (iommu->pasid_state_table) {
82 free_pages((unsigned long)iommu->pasid_state_table, order);
83 iommu->pasid_state_table = NULL;
84 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +010085 idr_destroy(&iommu->pasid_idr);
David Woodhouse8a94ade2015-03-24 14:54:56 +000086 return 0;
87}
David Woodhouse2f26e0a2015-09-09 11:40:47 +010088
David Woodhousea222a7f2015-10-07 23:35:18 +010089#define PRQ_ORDER 0
90
91int intel_svm_enable_prq(struct intel_iommu *iommu)
92{
93 struct page *pages;
94 int irq, ret;
95
96 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
97 if (!pages) {
98 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
99 iommu->name);
100 return -ENOMEM;
101 }
102 iommu->prq = page_address(pages);
103
104 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
105 if (irq <= 0) {
106 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
107 iommu->name);
108 ret = -EINVAL;
109 err:
110 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
111 iommu->prq = NULL;
112 return ret;
113 }
114 iommu->pr_irq = irq;
115
116 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
117
118 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
119 iommu->prq_name, iommu);
120 if (ret) {
121 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
122 iommu->name);
123 dmar_free_hwirq(irq);
124 goto err;
125 }
126 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
127 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
128 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
129
130 return 0;
131}
132
133int intel_svm_finish_prq(struct intel_iommu *iommu)
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, 0ULL);
138
139 free_irq(iommu->pr_irq, iommu);
140 dmar_free_hwirq(iommu->pr_irq);
141 iommu->pr_irq = 0;
142
143 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
144 iommu->prq = NULL;
145
146 return 0;
147}
148
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100149static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
David Woodhousee0349922015-10-16 19:36:53 +0100150 unsigned long address, int pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100151{
152 struct qi_desc desc;
153 int mask = ilog2(__roundup_pow_of_two(pages));
154
155 if (pages == -1 || !cap_pgsel_inv(svm->iommu->cap) ||
156 mask > cap_max_amask_val(svm->iommu->cap)) {
David Woodhousee0349922015-10-16 19:36:53 +0100157 /* For global kernel pages we have to flush them in *all* PASIDs
158 * because that's the only option the hardware gives us. Despite
159 * the fact that they are actually only accessible through one. */
160 if (gl)
161 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
162 QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) | QI_EIOTLB_TYPE;
163 else
164 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
165 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100166 desc.high = 0;
167 } else {
168 desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
169 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | QI_EIOTLB_TYPE;
David Woodhousee0349922015-10-16 19:36:53 +0100170 desc.high = QI_EIOTLB_ADDR(address) | QI_EIOTLB_GL(gl) |
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100171 QI_EIOTLB_IH(ih) | QI_EIOTLB_AM(mask);
172 }
173
174 qi_submit_sync(&desc, svm->iommu);
175
176 if (sdev->dev_iotlb) {
177 desc.low = QI_DEV_EIOTLB_PASID(svm->pasid) | QI_DEV_EIOTLB_SID(sdev->sid) |
178 QI_DEV_EIOTLB_QDEP(sdev->qdep) | QI_DEIOTLB_TYPE;
179 if (mask) {
180 unsigned long adr, delta;
181
182 /* Least significant zero bits in the address indicate the
183 * range of the request. So mask them out according to the
184 * size. */
185 adr = address & ((1<<(VTD_PAGE_SHIFT + mask)) - 1);
186
187 /* Now ensure that we round down further if the original
188 * request was not aligned w.r.t. its size */
189 delta = address - adr;
190 if (delta + (pages << VTD_PAGE_SHIFT) >= (1 << (VTD_PAGE_SHIFT + mask)))
191 adr &= ~(1 << (VTD_PAGE_SHIFT + mask));
192 desc.high = QI_DEV_EIOTLB_ADDR(adr) | QI_DEV_EIOTLB_SIZE;
193 } else {
194 desc.high = QI_DEV_EIOTLB_ADDR(address);
195 }
196 qi_submit_sync(&desc, svm->iommu);
197 }
198}
199
200static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
David Woodhousee0349922015-10-16 19:36:53 +0100201 int pages, int ih, int gl)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100202{
203 struct intel_svm_dev *sdev;
204
David Woodhouse907fea32015-10-13 14:11:13 +0100205 /* Try deferred invalidate if available */
206 if (svm->iommu->pasid_state_table &&
207 !cmpxchg64(&svm->iommu->pasid_state_table[svm->pasid].val, 0, 1ULL << 63))
208 return;
209
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100210 rcu_read_lock();
211 list_for_each_entry_rcu(sdev, &svm->devs, list)
David Woodhousee0349922015-10-16 19:36:53 +0100212 intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100213 rcu_read_unlock();
214}
215
216static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm,
217 unsigned long address, pte_t pte)
218{
219 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
220
David Woodhousee0349922015-10-16 19:36:53 +0100221 intel_flush_svm_range(svm, address, 1, 1, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100222}
223
224static void intel_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm,
225 unsigned long address)
226{
227 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
228
David Woodhousee0349922015-10-16 19:36:53 +0100229 intel_flush_svm_range(svm, address, 1, 1, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100230}
231
232/* Pages have been freed at this point */
233static void intel_invalidate_range(struct mmu_notifier *mn,
234 struct mm_struct *mm,
235 unsigned long start, unsigned long end)
236{
237 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
238
239 intel_flush_svm_range(svm, start,
David Woodhousee0349922015-10-16 19:36:53 +0100240 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100241}
242
243
244static void intel_flush_pasid_dev(struct intel_svm *svm, struct intel_svm_dev *sdev)
245{
246 struct qi_desc desc;
247
248 desc.high = 0;
249 desc.low = QI_PC_TYPE | QI_PC_DID(sdev->did) | QI_PC_PASID_SEL | QI_PC_PASID(svm->pasid);
250
251 qi_submit_sync(&desc, svm->iommu);
252}
253
254static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
255{
256 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
257
258 svm->iommu->pasid_table[svm->pasid].val = 0;
259
260 /* There's no need to do any flush because we can't get here if there
261 * are any devices left anyway. */
262 WARN_ON(!list_empty(&svm->devs));
263}
264
265static const struct mmu_notifier_ops intel_mmuops = {
266 .release = intel_mm_release,
267 .change_pte = intel_change_pte,
268 .invalidate_page = intel_invalidate_page,
269 .invalidate_range = intel_invalidate_range,
270};
271
272static DEFINE_MUTEX(pasid_mutex);
273
David Woodhouse0204a492015-10-13 17:18:10 +0100274int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100275{
276 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
277 struct intel_svm_dev *sdev;
278 struct intel_svm *svm = NULL;
David Woodhouse5cec7532015-10-15 15:52:15 +0100279 struct mm_struct *mm = NULL;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100280 int pasid_max;
281 int ret;
282
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100283 if (WARN_ON(!iommu))
284 return -EINVAL;
285
286 if (dev_is_pci(dev)) {
287 pasid_max = pci_max_pasids(to_pci_dev(dev));
288 if (pasid_max < 0)
289 return -EINVAL;
290 } else
291 pasid_max = 1 << 20;
292
David Woodhouse5cec7532015-10-15 15:52:15 +0100293 if ((flags & SVM_FLAG_SUPERVISOR_MODE)) {
294 if (!ecap_srs(iommu->ecap))
295 return -EINVAL;
296 } else if (pasid) {
297 mm = get_task_mm(current);
298 BUG_ON(!mm);
299 }
300
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100301 mutex_lock(&pasid_mutex);
David Woodhouse569e4f72015-10-15 13:59:14 +0100302 if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100303 int i;
304
305 idr_for_each_entry(&iommu->pasid_idr, svm, i) {
David Woodhouse5cec7532015-10-15 15:52:15 +0100306 if (svm->mm != mm ||
David Woodhouse569e4f72015-10-15 13:59:14 +0100307 (svm->flags & SVM_FLAG_PRIVATE_PASID))
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100308 continue;
309
310 if (svm->pasid >= pasid_max) {
311 dev_warn(dev,
312 "Limited PASID width. Cannot use existing PASID %d\n",
313 svm->pasid);
314 ret = -ENOSPC;
315 goto out;
316 }
317
318 list_for_each_entry(sdev, &svm->devs, list) {
319 if (dev == sdev->dev) {
David Woodhouse0204a492015-10-13 17:18:10 +0100320 if (sdev->ops != ops) {
321 ret = -EBUSY;
322 goto out;
323 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100324 sdev->users++;
325 goto success;
326 }
327 }
328
329 break;
330 }
331 }
332
333 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
334 if (!sdev) {
335 ret = -ENOMEM;
336 goto out;
337 }
338 sdev->dev = dev;
339
340 ret = intel_iommu_enable_pasid(iommu, sdev);
341 if (ret || !pasid) {
342 /* If they don't actually want to assign a PASID, this is
343 * just an enabling check/preparation. */
344 kfree(sdev);
345 goto out;
346 }
347 /* Finish the setup now we know we're keeping it */
348 sdev->users = 1;
David Woodhouse0204a492015-10-13 17:18:10 +0100349 sdev->ops = ops;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100350 init_rcu_head(&sdev->rcu);
351
352 if (!svm) {
353 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
354 if (!svm) {
355 ret = -ENOMEM;
356 kfree(sdev);
357 goto out;
358 }
359 svm->iommu = iommu;
360
361 if (pasid_max > 2 << ecap_pss(iommu->ecap))
362 pasid_max = 2 << ecap_pss(iommu->ecap);
363
364 ret = idr_alloc(&iommu->pasid_idr, svm, 0, pasid_max - 1,
365 GFP_KERNEL);
366 if (ret < 0) {
367 kfree(svm);
368 goto out;
369 }
370 svm->pasid = ret;
371 svm->notifier.ops = &intel_mmuops;
David Woodhouse5cec7532015-10-15 15:52:15 +0100372 svm->mm = mm;
David Woodhouse569e4f72015-10-15 13:59:14 +0100373 svm->flags = flags;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100374 INIT_LIST_HEAD_RCU(&svm->devs);
375 ret = -ENOMEM;
David Woodhouse5cec7532015-10-15 15:52:15 +0100376 if (mm) {
377 ret = mmu_notifier_register(&svm->notifier, mm);
378 if (ret) {
379 idr_remove(&svm->iommu->pasid_idr, svm->pasid);
380 kfree(svm);
381 kfree(sdev);
382 goto out;
383 }
384 iommu->pasid_table[svm->pasid].val = (u64)__pa(mm->pgd) | 1;
385 mm = NULL;
386 } else
387 iommu->pasid_table[svm->pasid].val = (u64)__pa(init_mm.pgd) | 1 | (1ULL << 11);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100388 wmb();
389 }
390 list_add_rcu(&sdev->list, &svm->devs);
391
392 success:
393 *pasid = svm->pasid;
394 ret = 0;
395 out:
396 mutex_unlock(&pasid_mutex);
David Woodhouse5cec7532015-10-15 15:52:15 +0100397 if (mm)
398 mmput(mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100399 return ret;
400}
401EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
402
403int intel_svm_unbind_mm(struct device *dev, int pasid)
404{
405 struct intel_svm_dev *sdev;
406 struct intel_iommu *iommu;
407 struct intel_svm *svm;
408 int ret = -EINVAL;
409
410 mutex_lock(&pasid_mutex);
411 iommu = intel_svm_device_to_iommu(dev);
412 if (!iommu || !iommu->pasid_table)
413 goto out;
414
415 svm = idr_find(&iommu->pasid_idr, pasid);
416 if (!svm)
417 goto out;
418
419 list_for_each_entry(sdev, &svm->devs, list) {
420 if (dev == sdev->dev) {
421 ret = 0;
422 sdev->users--;
423 if (!sdev->users) {
424 list_del_rcu(&sdev->list);
425 /* Flush the PASID cache and IOTLB for this device.
426 * Note that we do depend on the hardware *not* using
427 * the PASID any more. Just as we depend on other
428 * devices never using PASIDs that they have no right
429 * to use. We have a *shared* PASID table, because it's
430 * large and has to be physically contiguous. So it's
431 * hard to be as defensive as we might like. */
432 intel_flush_pasid_dev(svm, sdev);
David Woodhousee0349922015-10-16 19:36:53 +0100433 intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100434 kfree_rcu(sdev, rcu);
435
436 if (list_empty(&svm->devs)) {
437 mmu_notifier_unregister(&svm->notifier, svm->mm);
438
439 idr_remove(&svm->iommu->pasid_idr, svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100440 if (svm->mm)
441 mmput(svm->mm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100442 /* We mandate that no page faults may be outstanding
443 * for the PASID when intel_svm_unbind_mm() is called.
444 * If that is not obeyed, subtle errors will happen.
445 * Let's make them less subtle... */
446 memset(svm, 0x6b, sizeof(*svm));
447 kfree(svm);
448 }
449 }
450 break;
451 }
452 }
453 out:
454 mutex_unlock(&pasid_mutex);
455
456 return ret;
457}
458EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100459
460/* Page request queue descriptor */
461struct page_req_dsc {
462 u64 srr:1;
463 u64 bof:1;
464 u64 pasid_present:1;
465 u64 lpig:1;
466 u64 pasid:20;
467 u64 bus:8;
468 u64 private:23;
469 u64 prg_index:9;
470 u64 rd_req:1;
471 u64 wr_req:1;
472 u64 exe_req:1;
473 u64 priv_req:1;
474 u64 devfn:8;
475 u64 addr:52;
476};
477
478#define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
479static irqreturn_t prq_event_thread(int irq, void *d)
480{
481 struct intel_iommu *iommu = d;
482 struct intel_svm *svm = NULL;
483 int head, tail, handled = 0;
484
485 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
486 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
487 while (head != tail) {
David Woodhouse0204a492015-10-13 17:18:10 +0100488 struct intel_svm_dev *sdev;
David Woodhousea222a7f2015-10-07 23:35:18 +0100489 struct vm_area_struct *vma;
490 struct page_req_dsc *req;
491 struct qi_desc resp;
492 int ret, result;
493 u64 address;
494
495 handled = 1;
496
497 req = &iommu->prq[head / sizeof(*req)];
498
499 result = QI_RESP_FAILURE;
David Woodhouse7f92a2e2015-10-16 17:22:31 +0100500 address = (u64)req->addr << VTD_PAGE_SHIFT;
David Woodhousea222a7f2015-10-07 23:35:18 +0100501 if (!req->pasid_present) {
502 pr_err("%s: Page request without PASID: %08llx %08llx\n",
503 iommu->name, ((unsigned long long *)req)[0],
504 ((unsigned long long *)req)[1]);
505 goto bad_req;
506 }
507
508 if (!svm || svm->pasid != req->pasid) {
509 rcu_read_lock();
510 svm = idr_find(&iommu->pasid_idr, req->pasid);
511 /* It *can't* go away, because the driver is not permitted
512 * to unbind the mm while any page faults are outstanding.
513 * So we only need RCU to protect the internal idr code. */
514 rcu_read_unlock();
515
516 if (!svm) {
517 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
518 iommu->name, req->pasid, ((unsigned long long *)req)[0],
519 ((unsigned long long *)req)[1]);
David Woodhouse26322ab2015-10-15 21:12:56 +0100520 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100521 }
522 }
523
524 result = QI_RESP_INVALID;
David Woodhouse5cec7532015-10-15 15:52:15 +0100525 /* Since we're using init_mm.pgd directly, we should never take
526 * any faults on kernel addresses. */
527 if (!svm->mm)
528 goto bad_req;
David Woodhousea222a7f2015-10-07 23:35:18 +0100529 down_read(&svm->mm->mmap_sem);
530 vma = find_extend_vma(svm->mm, address);
531 if (!vma || address < vma->vm_start)
532 goto invalid;
533
534 ret = handle_mm_fault(svm->mm, vma, address,
535 req->wr_req ? FAULT_FLAG_WRITE : 0);
536 if (ret & VM_FAULT_ERROR)
537 goto invalid;
538
539 result = QI_RESP_SUCCESS;
540 invalid:
541 up_read(&svm->mm->mmap_sem);
542 bad_req:
543 /* Accounting for major/minor faults? */
David Woodhouse0204a492015-10-13 17:18:10 +0100544 rcu_read_lock();
545 list_for_each_entry_rcu(sdev, &svm->devs, list) {
546 if (sdev->sid == PCI_DEVID(req->bus, req->devfn));
547 break;
548 }
549 /* Other devices can go away, but the drivers are not permitted
550 * to unbind while any page faults might be in flight. So it's
551 * OK to drop the 'lock' here now we have it. */
552 rcu_read_unlock();
553
554 if (WARN_ON(&sdev->list == &svm->devs))
555 sdev = NULL;
556
557 if (sdev && sdev->ops && sdev->ops->fault_cb) {
558 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
559 (req->wr_req << 1) | (req->exe_req);
560 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, req->private, rwxp, result);
561 }
David Woodhouse26322ab2015-10-15 21:12:56 +0100562 /* We get here in the error case where the PASID lookup failed,
563 and these can be NULL. Do not use them below this point! */
564 sdev = NULL;
565 svm = NULL;
566 no_pasid:
David Woodhousea222a7f2015-10-07 23:35:18 +0100567 if (req->lpig) {
568 /* Page Group Response */
569 resp.low = QI_PGRP_PASID(req->pasid) |
570 QI_PGRP_DID((req->bus << 8) | req->devfn) |
571 QI_PGRP_PASID_P(req->pasid_present) |
572 QI_PGRP_RESP_TYPE;
573 resp.high = QI_PGRP_IDX(req->prg_index) |
574 QI_PGRP_PRIV(req->private) | QI_PGRP_RESP_CODE(result);
575
David Woodhouse26322ab2015-10-15 21:12:56 +0100576 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100577 } else if (req->srr) {
578 /* Page Stream Response */
579 resp.low = QI_PSTRM_IDX(req->prg_index) |
580 QI_PSTRM_PRIV(req->private) | QI_PSTRM_BUS(req->bus) |
581 QI_PSTRM_PASID(req->pasid) | QI_PSTRM_RESP_TYPE;
582 resp.high = QI_PSTRM_ADDR(address) | QI_PSTRM_DEVFN(req->devfn) |
583 QI_PSTRM_RESP_CODE(result);
584
David Woodhouse26322ab2015-10-15 21:12:56 +0100585 qi_submit_sync(&resp, iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +0100586 }
587
588 head = (head + sizeof(*req)) & PRQ_RING_MASK;
589 }
590
591 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
592
593 return IRQ_RETVAL(handled);
594}