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