intel-svm.c 18 KB

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