intel-svm.c 19 KB

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