intel-svm.c 19 KB

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