intel-svm.c 19 KB

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