intel-svm.c 19 KB

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