intel-svm.c 18 KB

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