intel-svm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  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. #include <linux/intel-iommu.h>
  16. #include <linux/mmu_notifier.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/intel-svm.h>
  20. #include <linux/rculist.h>
  21. #include <linux/pci.h>
  22. #include <linux/pci-ats.h>
  23. #include <linux/dmar.h>
  24. #include <linux/interrupt.h>
  25. static irqreturn_t prq_event_thread(int irq, void *d);
  26. struct pasid_entry {
  27. u64 val;
  28. };
  29. struct pasid_state_entry {
  30. u64 val;
  31. };
  32. int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
  33. {
  34. struct page *pages;
  35. int order;
  36. order = ecap_pss(iommu->ecap) + 7 - PAGE_SHIFT;
  37. if (order < 0)
  38. order = 0;
  39. pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
  40. if (!pages) {
  41. pr_warn("IOMMU: %s: Failed to allocate PASID table\n",
  42. iommu->name);
  43. return -ENOMEM;
  44. }
  45. iommu->pasid_table = page_address(pages);
  46. pr_info("%s: Allocated order %d PASID table.\n", iommu->name, order);
  47. if (ecap_dis(iommu->ecap)) {
  48. pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
  49. if (pages)
  50. iommu->pasid_state_table = page_address(pages);
  51. else
  52. pr_warn("IOMMU: %s: Failed to allocate PASID state table\n",
  53. iommu->name);
  54. }
  55. idr_init(&iommu->pasid_idr);
  56. return 0;
  57. }
  58. int intel_svm_free_pasid_tables(struct intel_iommu *iommu)
  59. {
  60. int order;
  61. order = ecap_pss(iommu->ecap) + 7 - PAGE_SHIFT;
  62. if (order < 0)
  63. order = 0;
  64. if (iommu->pasid_table) {
  65. free_pages((unsigned long)iommu->pasid_table, order);
  66. iommu->pasid_table = NULL;
  67. }
  68. if (iommu->pasid_state_table) {
  69. free_pages((unsigned long)iommu->pasid_state_table, order);
  70. iommu->pasid_state_table = NULL;
  71. }
  72. idr_destroy(&iommu->pasid_idr);
  73. return 0;
  74. }
  75. #define PRQ_ORDER 0
  76. int intel_svm_enable_prq(struct intel_iommu *iommu)
  77. {
  78. struct page *pages;
  79. int irq, ret;
  80. pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
  81. if (!pages) {
  82. pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
  83. iommu->name);
  84. return -ENOMEM;
  85. }
  86. iommu->prq = page_address(pages);
  87. irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
  88. if (irq <= 0) {
  89. pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
  90. iommu->name);
  91. ret = -EINVAL;
  92. err:
  93. free_pages((unsigned long)iommu->prq, PRQ_ORDER);
  94. iommu->prq = NULL;
  95. return ret;
  96. }
  97. iommu->pr_irq = irq;
  98. snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
  99. ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
  100. iommu->prq_name, iommu);
  101. if (ret) {
  102. pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
  103. iommu->name);
  104. dmar_free_hwirq(irq);
  105. goto err;
  106. }
  107. dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
  108. dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
  109. dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
  110. return 0;
  111. }
  112. int intel_svm_finish_prq(struct intel_iommu *iommu)
  113. {
  114. dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
  115. dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
  116. dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
  117. free_irq(iommu->pr_irq, iommu);
  118. dmar_free_hwirq(iommu->pr_irq);
  119. iommu->pr_irq = 0;
  120. free_pages((unsigned long)iommu->prq, PRQ_ORDER);
  121. iommu->prq = NULL;
  122. return 0;
  123. }
  124. static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
  125. unsigned long address, unsigned long pages, int ih, int gl)
  126. {
  127. struct qi_desc desc;
  128. if (pages == -1) {
  129. /* For global kernel pages we have to flush them in *all* PASIDs
  130. * because that's the only option the hardware gives us. Despite
  131. * the fact that they are actually only accessible through one. */
  132. if (gl)
  133. desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
  134. QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) | QI_EIOTLB_TYPE;
  135. else
  136. desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
  137. QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;
  138. desc.high = 0;
  139. } else {
  140. int mask = ilog2(__roundup_pow_of_two(pages));
  141. desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) |
  142. QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | QI_EIOTLB_TYPE;
  143. desc.high = QI_EIOTLB_ADDR(address) | QI_EIOTLB_GL(gl) |
  144. QI_EIOTLB_IH(ih) | QI_EIOTLB_AM(mask);
  145. }
  146. qi_submit_sync(&desc, svm->iommu);
  147. if (sdev->dev_iotlb) {
  148. desc.low = QI_DEV_EIOTLB_PASID(svm->pasid) | QI_DEV_EIOTLB_SID(sdev->sid) |
  149. QI_DEV_EIOTLB_QDEP(sdev->qdep) | QI_DEIOTLB_TYPE;
  150. if (pages == -1) {
  151. desc.high = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | QI_DEV_EIOTLB_SIZE;
  152. } else if (pages > 1) {
  153. /* The least significant zero bit indicates the size. So,
  154. * for example, an "address" value of 0x12345f000 will
  155. * flush from 0x123440000 to 0x12347ffff (256KiB). */
  156. unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
  157. unsigned long mask = __rounddown_pow_of_two(address ^ last);;
  158. desc.high = QI_DEV_EIOTLB_ADDR((address & ~mask) | (mask - 1)) | QI_DEV_EIOTLB_SIZE;
  159. } else {
  160. desc.high = QI_DEV_EIOTLB_ADDR(address);
  161. }
  162. qi_submit_sync(&desc, svm->iommu);
  163. }
  164. }
  165. static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
  166. unsigned long pages, int ih, int gl)
  167. {
  168. struct intel_svm_dev *sdev;
  169. /* Try deferred invalidate if available */
  170. if (svm->iommu->pasid_state_table &&
  171. !cmpxchg64(&svm->iommu->pasid_state_table[svm->pasid].val, 0, 1ULL << 63))
  172. return;
  173. rcu_read_lock();
  174. list_for_each_entry_rcu(sdev, &svm->devs, list)
  175. intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl);
  176. rcu_read_unlock();
  177. }
  178. static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm,
  179. unsigned long address, pte_t pte)
  180. {
  181. struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
  182. intel_flush_svm_range(svm, address, 1, 1, 0);
  183. }
  184. static void intel_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm,
  185. unsigned long address)
  186. {
  187. struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
  188. intel_flush_svm_range(svm, address, 1, 1, 0);
  189. }
  190. /* Pages have been freed at this point */
  191. static void intel_invalidate_range(struct mmu_notifier *mn,
  192. struct mm_struct *mm,
  193. unsigned long start, unsigned long end)
  194. {
  195. struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
  196. intel_flush_svm_range(svm, start,
  197. (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0);
  198. }
  199. static void intel_flush_pasid_dev(struct intel_svm *svm, struct intel_svm_dev *sdev, int pasid)
  200. {
  201. struct qi_desc desc;
  202. desc.high = 0;
  203. desc.low = QI_PC_TYPE | QI_PC_DID(sdev->did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid);
  204. qi_submit_sync(&desc, svm->iommu);
  205. }
  206. static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
  207. {
  208. struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
  209. struct intel_svm_dev *sdev;
  210. /* This might end up being called from exit_mmap(), *before* the page
  211. * tables are cleared. And __mmu_notifier_release() will delete us from
  212. * the list of notifiers so that our invalidate_range() callback doesn't
  213. * get called when the page tables are cleared. So we need to protect
  214. * against hardware accessing those page tables.
  215. *
  216. * We do it by clearing the entry in the PASID table and then flushing
  217. * the IOTLB and the PASID table caches. This might upset hardware;
  218. * perhaps we'll want to point the PASID to a dummy PGD (like the zero
  219. * page) so that we end up taking a fault that the hardware really
  220. * *has* to handle gracefully without affecting other processes.
  221. */
  222. svm->iommu->pasid_table[svm->pasid].val = 0;
  223. wmb();
  224. rcu_read_lock();
  225. list_for_each_entry_rcu(sdev, &svm->devs, list) {
  226. intel_flush_pasid_dev(svm, sdev, svm->pasid);
  227. intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
  228. }
  229. rcu_read_unlock();
  230. }
  231. static const struct mmu_notifier_ops intel_mmuops = {
  232. .release = intel_mm_release,
  233. .change_pte = intel_change_pte,
  234. .invalidate_page = intel_invalidate_page,
  235. .invalidate_range = intel_invalidate_range,
  236. };
  237. static DEFINE_MUTEX(pasid_mutex);
  238. int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops)
  239. {
  240. struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
  241. struct intel_svm_dev *sdev;
  242. struct intel_svm *svm = NULL;
  243. struct mm_struct *mm = NULL;
  244. int pasid_max;
  245. int ret;
  246. if (WARN_ON(!iommu))
  247. return -EINVAL;
  248. if (dev_is_pci(dev)) {
  249. pasid_max = pci_max_pasids(to_pci_dev(dev));
  250. if (pasid_max < 0)
  251. return -EINVAL;
  252. } else
  253. pasid_max = 1 << 20;
  254. if ((flags & SVM_FLAG_SUPERVISOR_MODE)) {
  255. if (!ecap_srs(iommu->ecap))
  256. return -EINVAL;
  257. } else if (pasid) {
  258. mm = get_task_mm(current);
  259. BUG_ON(!mm);
  260. }
  261. mutex_lock(&pasid_mutex);
  262. if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) {
  263. int i;
  264. idr_for_each_entry(&iommu->pasid_idr, svm, i) {
  265. if (svm->mm != mm ||
  266. (svm->flags & SVM_FLAG_PRIVATE_PASID))
  267. continue;
  268. if (svm->pasid >= pasid_max) {
  269. dev_warn(dev,
  270. "Limited PASID width. Cannot use existing PASID %d\n",
  271. svm->pasid);
  272. ret = -ENOSPC;
  273. goto out;
  274. }
  275. list_for_each_entry(sdev, &svm->devs, list) {
  276. if (dev == sdev->dev) {
  277. if (sdev->ops != ops) {
  278. ret = -EBUSY;
  279. goto out;
  280. }
  281. sdev->users++;
  282. goto success;
  283. }
  284. }
  285. break;
  286. }
  287. }
  288. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  289. if (!sdev) {
  290. ret = -ENOMEM;
  291. goto out;
  292. }
  293. sdev->dev = dev;
  294. ret = intel_iommu_enable_pasid(iommu, sdev);
  295. if (ret || !pasid) {
  296. /* If they don't actually want to assign a PASID, this is
  297. * just an enabling check/preparation. */
  298. kfree(sdev);
  299. goto out;
  300. }
  301. /* Finish the setup now we know we're keeping it */
  302. sdev->users = 1;
  303. sdev->ops = ops;
  304. init_rcu_head(&sdev->rcu);
  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. if (pasid_max > 2 << ecap_pss(iommu->ecap))
  314. pasid_max = 2 << ecap_pss(iommu->ecap);
  315. /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
  316. ret = idr_alloc(&iommu->pasid_idr, svm,
  317. !!cap_caching_mode(iommu->cap),
  318. pasid_max - 1, GFP_KERNEL);
  319. if (ret < 0) {
  320. kfree(svm);
  321. goto out;
  322. }
  323. svm->pasid = ret;
  324. svm->notifier.ops = &intel_mmuops;
  325. svm->mm = mm;
  326. svm->flags = flags;
  327. INIT_LIST_HEAD_RCU(&svm->devs);
  328. ret = -ENOMEM;
  329. if (mm) {
  330. ret = mmu_notifier_register(&svm->notifier, mm);
  331. if (ret) {
  332. idr_remove(&svm->iommu->pasid_idr, svm->pasid);
  333. kfree(svm);
  334. kfree(sdev);
  335. goto out;
  336. }
  337. iommu->pasid_table[svm->pasid].val = (u64)__pa(mm->pgd) | 1;
  338. } else
  339. iommu->pasid_table[svm->pasid].val = (u64)__pa(init_mm.pgd) | 1 | (1ULL << 11);
  340. wmb();
  341. /* In caching mode, we still have to flush with PASID 0 when
  342. * a PASID table entry becomes present. Not entirely clear
  343. * *why* that would be the case — surely we could just issue
  344. * a flush with the PASID value that we've changed? The PASID
  345. * is the index into the table, after all. It's not like domain
  346. * IDs in the case of the equivalent context-entry change in
  347. * caching mode. And for that matter it's not entirely clear why
  348. * a VMM would be in the business of caching the PASID table
  349. * anyway. Surely that can be left entirely to the guest? */
  350. if (cap_caching_mode(iommu->cap))
  351. intel_flush_pasid_dev(svm, sdev, 0);
  352. }
  353. list_add_rcu(&sdev->list, &svm->devs);
  354. success:
  355. *pasid = svm->pasid;
  356. ret = 0;
  357. out:
  358. mutex_unlock(&pasid_mutex);
  359. if (mm)
  360. mmput(mm);
  361. return ret;
  362. }
  363. EXPORT_SYMBOL_GPL(intel_svm_bind_mm);
  364. int intel_svm_unbind_mm(struct device *dev, int pasid)
  365. {
  366. struct intel_svm_dev *sdev;
  367. struct intel_iommu *iommu;
  368. struct intel_svm *svm;
  369. int ret = -EINVAL;
  370. mutex_lock(&pasid_mutex);
  371. iommu = intel_svm_device_to_iommu(dev);
  372. if (!iommu || !iommu->pasid_table)
  373. goto out;
  374. svm = idr_find(&iommu->pasid_idr, pasid);
  375. if (!svm)
  376. goto out;
  377. list_for_each_entry(sdev, &svm->devs, list) {
  378. if (dev == sdev->dev) {
  379. ret = 0;
  380. sdev->users--;
  381. if (!sdev->users) {
  382. list_del_rcu(&sdev->list);
  383. /* Flush the PASID cache and IOTLB for this device.
  384. * Note that we do depend on the hardware *not* using
  385. * the PASID any more. Just as we depend on other
  386. * devices never using PASIDs that they have no right
  387. * to use. We have a *shared* PASID table, because it's
  388. * large and has to be physically contiguous. So it's
  389. * hard to be as defensive as we might like. */
  390. intel_flush_pasid_dev(svm, sdev, svm->pasid);
  391. intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm);
  392. kfree_rcu(sdev, rcu);
  393. if (list_empty(&svm->devs)) {
  394. idr_remove(&svm->iommu->pasid_idr, svm->pasid);
  395. if (svm->mm)
  396. mmu_notifier_unregister(&svm->notifier, svm->mm);
  397. /* We mandate that no page faults may be outstanding
  398. * for the PASID when intel_svm_unbind_mm() is called.
  399. * If that is not obeyed, subtle errors will happen.
  400. * Let's make them less subtle... */
  401. memset(svm, 0x6b, sizeof(*svm));
  402. kfree(svm);
  403. }
  404. }
  405. break;
  406. }
  407. }
  408. out:
  409. mutex_unlock(&pasid_mutex);
  410. return ret;
  411. }
  412. EXPORT_SYMBOL_GPL(intel_svm_unbind_mm);
  413. /* Page request queue descriptor */
  414. struct page_req_dsc {
  415. u64 srr:1;
  416. u64 bof:1;
  417. u64 pasid_present:1;
  418. u64 lpig:1;
  419. u64 pasid:20;
  420. u64 bus:8;
  421. u64 private:23;
  422. u64 prg_index:9;
  423. u64 rd_req:1;
  424. u64 wr_req:1;
  425. u64 exe_req:1;
  426. u64 priv_req:1;
  427. u64 devfn:8;
  428. u64 addr:52;
  429. };
  430. #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
  431. static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
  432. {
  433. unsigned long requested = 0;
  434. if (req->exe_req)
  435. requested |= VM_EXEC;
  436. if (req->rd_req)
  437. requested |= VM_READ;
  438. if (req->wr_req)
  439. requested |= VM_WRITE;
  440. return (requested & ~vma->vm_flags) != 0;
  441. }
  442. static irqreturn_t prq_event_thread(int irq, void *d)
  443. {
  444. struct intel_iommu *iommu = d;
  445. struct intel_svm *svm = NULL;
  446. int head, tail, handled = 0;
  447. /* Clear PPR bit before reading head/tail registers, to
  448. * ensure that we get a new interrupt if needed. */
  449. writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
  450. tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
  451. head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
  452. while (head != tail) {
  453. struct intel_svm_dev *sdev;
  454. struct vm_area_struct *vma;
  455. struct page_req_dsc *req;
  456. struct qi_desc resp;
  457. int ret, result;
  458. u64 address;
  459. handled = 1;
  460. req = &iommu->prq[head / sizeof(*req)];
  461. result = QI_RESP_FAILURE;
  462. address = (u64)req->addr << VTD_PAGE_SHIFT;
  463. if (!req->pasid_present) {
  464. pr_err("%s: Page request without PASID: %08llx %08llx\n",
  465. iommu->name, ((unsigned long long *)req)[0],
  466. ((unsigned long long *)req)[1]);
  467. goto bad_req;
  468. }
  469. if (!svm || svm->pasid != req->pasid) {
  470. rcu_read_lock();
  471. svm = idr_find(&iommu->pasid_idr, req->pasid);
  472. /* It *can't* go away, because the driver is not permitted
  473. * to unbind the mm while any page faults are outstanding.
  474. * So we only need RCU to protect the internal idr code. */
  475. rcu_read_unlock();
  476. if (!svm) {
  477. pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
  478. iommu->name, req->pasid, ((unsigned long long *)req)[0],
  479. ((unsigned long long *)req)[1]);
  480. goto no_pasid;
  481. }
  482. }
  483. result = QI_RESP_INVALID;
  484. /* Since we're using init_mm.pgd directly, we should never take
  485. * any faults on kernel addresses. */
  486. if (!svm->mm)
  487. goto bad_req;
  488. /* If the mm is already defunct, don't handle faults. */
  489. if (!atomic_inc_not_zero(&svm->mm->mm_users))
  490. goto bad_req;
  491. down_read(&svm->mm->mmap_sem);
  492. vma = find_extend_vma(svm->mm, address);
  493. if (!vma || address < vma->vm_start)
  494. goto invalid;
  495. if (access_error(vma, req))
  496. goto invalid;
  497. ret = handle_mm_fault(vma, address,
  498. req->wr_req ? FAULT_FLAG_WRITE : 0);
  499. if (ret & VM_FAULT_ERROR)
  500. goto invalid;
  501. result = QI_RESP_SUCCESS;
  502. invalid:
  503. up_read(&svm->mm->mmap_sem);
  504. mmput(svm->mm);
  505. bad_req:
  506. /* Accounting for major/minor faults? */
  507. rcu_read_lock();
  508. list_for_each_entry_rcu(sdev, &svm->devs, list) {
  509. if (sdev->sid == PCI_DEVID(req->bus, req->devfn))
  510. break;
  511. }
  512. /* Other devices can go away, but the drivers are not permitted
  513. * to unbind while any page faults might be in flight. So it's
  514. * OK to drop the 'lock' here now we have it. */
  515. rcu_read_unlock();
  516. if (WARN_ON(&sdev->list == &svm->devs))
  517. sdev = NULL;
  518. if (sdev && sdev->ops && sdev->ops->fault_cb) {
  519. int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
  520. (req->exe_req << 1) | (req->priv_req);
  521. sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, req->private, rwxp, result);
  522. }
  523. /* We get here in the error case where the PASID lookup failed,
  524. and these can be NULL. Do not use them below this point! */
  525. sdev = NULL;
  526. svm = NULL;
  527. no_pasid:
  528. if (req->lpig) {
  529. /* Page Group Response */
  530. resp.low = QI_PGRP_PASID(req->pasid) |
  531. QI_PGRP_DID((req->bus << 8) | req->devfn) |
  532. QI_PGRP_PASID_P(req->pasid_present) |
  533. QI_PGRP_RESP_TYPE;
  534. resp.high = QI_PGRP_IDX(req->prg_index) |
  535. QI_PGRP_PRIV(req->private) | QI_PGRP_RESP_CODE(result);
  536. qi_submit_sync(&resp, iommu);
  537. } else if (req->srr) {
  538. /* Page Stream Response */
  539. resp.low = QI_PSTRM_IDX(req->prg_index) |
  540. QI_PSTRM_PRIV(req->private) | QI_PSTRM_BUS(req->bus) |
  541. QI_PSTRM_PASID(req->pasid) | QI_PSTRM_RESP_TYPE;
  542. resp.high = QI_PSTRM_ADDR(address) | QI_PSTRM_DEVFN(req->devfn) |
  543. QI_PSTRM_RESP_CODE(result);
  544. qi_submit_sync(&resp, iommu);
  545. }
  546. head = (head + sizeof(*req)) & PRQ_RING_MASK;
  547. }
  548. dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
  549. return IRQ_RETVAL(handled);
  550. }