intel-svm.c 19 KB

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