svm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2015 Intel Corporation.
  4. *
  5. * Authors: David Woodhouse <[email protected]>
  6. */
  7. #include <linux/mmu_notifier.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/mm.h>
  10. #include <linux/slab.h>
  11. #include <linux/intel-svm.h>
  12. #include <linux/rculist.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci-ats.h>
  15. #include <linux/dmar.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mm_types.h>
  18. #include <linux/xarray.h>
  19. #include <linux/ioasid.h>
  20. #include <asm/page.h>
  21. #include <asm/fpu/api.h>
  22. #include "iommu.h"
  23. #include "pasid.h"
  24. #include "perf.h"
  25. #include "../iommu-sva.h"
  26. #include "trace.h"
  27. static irqreturn_t prq_event_thread(int irq, void *d);
  28. static void intel_svm_drain_prq(struct device *dev, u32 pasid);
  29. #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
  30. static DEFINE_XARRAY_ALLOC(pasid_private_array);
  31. static int pasid_private_add(ioasid_t pasid, void *priv)
  32. {
  33. return xa_alloc(&pasid_private_array, &pasid, priv,
  34. XA_LIMIT(pasid, pasid), GFP_ATOMIC);
  35. }
  36. static void pasid_private_remove(ioasid_t pasid)
  37. {
  38. xa_erase(&pasid_private_array, pasid);
  39. }
  40. static void *pasid_private_find(ioasid_t pasid)
  41. {
  42. return xa_load(&pasid_private_array, pasid);
  43. }
  44. static struct intel_svm_dev *
  45. svm_lookup_device_by_dev(struct intel_svm *svm, struct device *dev)
  46. {
  47. struct intel_svm_dev *sdev = NULL, *t;
  48. rcu_read_lock();
  49. list_for_each_entry_rcu(t, &svm->devs, list) {
  50. if (t->dev == dev) {
  51. sdev = t;
  52. break;
  53. }
  54. }
  55. rcu_read_unlock();
  56. return sdev;
  57. }
  58. int intel_svm_enable_prq(struct intel_iommu *iommu)
  59. {
  60. struct iopf_queue *iopfq;
  61. struct page *pages;
  62. int irq, ret;
  63. pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
  64. if (!pages) {
  65. pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
  66. iommu->name);
  67. return -ENOMEM;
  68. }
  69. iommu->prq = page_address(pages);
  70. irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
  71. if (irq <= 0) {
  72. pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
  73. iommu->name);
  74. ret = -EINVAL;
  75. goto free_prq;
  76. }
  77. iommu->pr_irq = irq;
  78. snprintf(iommu->iopfq_name, sizeof(iommu->iopfq_name),
  79. "dmar%d-iopfq", iommu->seq_id);
  80. iopfq = iopf_queue_alloc(iommu->iopfq_name);
  81. if (!iopfq) {
  82. pr_err("IOMMU: %s: Failed to allocate iopf queue\n", iommu->name);
  83. ret = -ENOMEM;
  84. goto free_hwirq;
  85. }
  86. iommu->iopf_queue = iopfq;
  87. snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
  88. ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
  89. iommu->prq_name, iommu);
  90. if (ret) {
  91. pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
  92. iommu->name);
  93. goto free_iopfq;
  94. }
  95. dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
  96. dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
  97. dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
  98. init_completion(&iommu->prq_complete);
  99. return 0;
  100. free_iopfq:
  101. iopf_queue_free(iommu->iopf_queue);
  102. iommu->iopf_queue = NULL;
  103. free_hwirq:
  104. dmar_free_hwirq(irq);
  105. iommu->pr_irq = 0;
  106. free_prq:
  107. free_pages((unsigned long)iommu->prq, PRQ_ORDER);
  108. iommu->prq = NULL;
  109. return ret;
  110. }
  111. int intel_svm_finish_prq(struct intel_iommu *iommu)
  112. {
  113. dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
  114. dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
  115. dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
  116. if (iommu->pr_irq) {
  117. free_irq(iommu->pr_irq, iommu);
  118. dmar_free_hwirq(iommu->pr_irq);
  119. iommu->pr_irq = 0;
  120. }
  121. if (iommu->iopf_queue) {
  122. iopf_queue_free(iommu->iopf_queue);
  123. iommu->iopf_queue = NULL;
  124. }
  125. free_pages((unsigned long)iommu->prq, PRQ_ORDER);
  126. iommu->prq = NULL;
  127. return 0;
  128. }
  129. void intel_svm_check(struct intel_iommu *iommu)
  130. {
  131. if (!pasid_supported(iommu))
  132. return;
  133. if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
  134. !cap_fl1gp_support(iommu->cap)) {
  135. pr_err("%s SVM disabled, incompatible 1GB page capability\n",
  136. iommu->name);
  137. return;
  138. }
  139. if (cpu_feature_enabled(X86_FEATURE_LA57) &&
  140. !cap_fl5lp_support(iommu->cap)) {
  141. pr_err("%s SVM disabled, incompatible paging mode\n",
  142. iommu->name);
  143. return;
  144. }
  145. iommu->flags |= VTD_FLAG_SVM_CAPABLE;
  146. }
  147. static void __flush_svm_range_dev(struct intel_svm *svm,
  148. struct intel_svm_dev *sdev,
  149. unsigned long address,
  150. unsigned long pages, int ih)
  151. {
  152. struct device_domain_info *info = dev_iommu_priv_get(sdev->dev);
  153. if (WARN_ON(!pages))
  154. return;
  155. qi_flush_piotlb(sdev->iommu, sdev->did, svm->pasid, address, pages, ih);
  156. if (info->ats_enabled) {
  157. qi_flush_dev_iotlb_pasid(sdev->iommu, sdev->sid, info->pfsid,
  158. svm->pasid, sdev->qdep, address,
  159. order_base_2(pages));
  160. quirk_extra_dev_tlb_flush(info, address, order_base_2(pages),
  161. svm->pasid, sdev->qdep);
  162. }
  163. }
  164. static void intel_flush_svm_range_dev(struct intel_svm *svm,
  165. struct intel_svm_dev *sdev,
  166. unsigned long address,
  167. unsigned long pages, int ih)
  168. {
  169. unsigned long shift = ilog2(__roundup_pow_of_two(pages));
  170. unsigned long align = (1ULL << (VTD_PAGE_SHIFT + shift));
  171. unsigned long start = ALIGN_DOWN(address, align);
  172. unsigned long end = ALIGN(address + (pages << VTD_PAGE_SHIFT), align);
  173. while (start < end) {
  174. __flush_svm_range_dev(svm, sdev, start, align >> VTD_PAGE_SHIFT, ih);
  175. start += align;
  176. }
  177. }
  178. static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
  179. unsigned long pages, int ih)
  180. {
  181. struct intel_svm_dev *sdev;
  182. rcu_read_lock();
  183. list_for_each_entry_rcu(sdev, &svm->devs, list)
  184. intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
  185. rcu_read_unlock();
  186. }
  187. /* Pages have been freed at this point */
  188. static void intel_invalidate_range(struct mmu_notifier *mn,
  189. struct mm_struct *mm,
  190. unsigned long start, unsigned long end)
  191. {
  192. struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
  193. intel_flush_svm_range(svm, start,
  194. (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
  195. }
  196. static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
  197. {
  198. struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
  199. struct intel_svm_dev *sdev;
  200. /* This might end up being called from exit_mmap(), *before* the page
  201. * tables are cleared. And __mmu_notifier_release() will delete us from
  202. * the list of notifiers so that our invalidate_range() callback doesn't
  203. * get called when the page tables are cleared. So we need to protect
  204. * against hardware accessing those page tables.
  205. *
  206. * We do it by clearing the entry in the PASID table and then flushing
  207. * the IOTLB and the PASID table caches. This might upset hardware;
  208. * perhaps we'll want to point the PASID to a dummy PGD (like the zero
  209. * page) so that we end up taking a fault that the hardware really
  210. * *has* to handle gracefully without affecting other processes.
  211. */
  212. rcu_read_lock();
  213. list_for_each_entry_rcu(sdev, &svm->devs, list)
  214. intel_pasid_tear_down_entry(sdev->iommu, sdev->dev,
  215. svm->pasid, true);
  216. rcu_read_unlock();
  217. }
  218. static const struct mmu_notifier_ops intel_mmuops = {
  219. .release = intel_mm_release,
  220. .invalidate_range = intel_invalidate_range,
  221. };
  222. static DEFINE_MUTEX(pasid_mutex);
  223. static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
  224. struct intel_svm **rsvm,
  225. struct intel_svm_dev **rsdev)
  226. {
  227. struct intel_svm_dev *sdev = NULL;
  228. struct intel_svm *svm;
  229. /* The caller should hold the pasid_mutex lock */
  230. if (WARN_ON(!mutex_is_locked(&pasid_mutex)))
  231. return -EINVAL;
  232. if (pasid == INVALID_IOASID || pasid >= PASID_MAX)
  233. return -EINVAL;
  234. svm = pasid_private_find(pasid);
  235. if (IS_ERR(svm))
  236. return PTR_ERR(svm);
  237. if (!svm)
  238. goto out;
  239. /*
  240. * If we found svm for the PASID, there must be at least one device
  241. * bond.
  242. */
  243. if (WARN_ON(list_empty(&svm->devs)))
  244. return -EINVAL;
  245. sdev = svm_lookup_device_by_dev(svm, dev);
  246. out:
  247. *rsvm = svm;
  248. *rsdev = sdev;
  249. return 0;
  250. }
  251. static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu,
  252. struct device *dev,
  253. struct mm_struct *mm)
  254. {
  255. struct device_domain_info *info = dev_iommu_priv_get(dev);
  256. struct intel_svm_dev *sdev;
  257. struct intel_svm *svm;
  258. unsigned long sflags;
  259. int ret = 0;
  260. svm = pasid_private_find(mm->pasid);
  261. if (!svm) {
  262. svm = kzalloc(sizeof(*svm), GFP_KERNEL);
  263. if (!svm)
  264. return ERR_PTR(-ENOMEM);
  265. svm->pasid = mm->pasid;
  266. svm->mm = mm;
  267. INIT_LIST_HEAD_RCU(&svm->devs);
  268. svm->notifier.ops = &intel_mmuops;
  269. ret = mmu_notifier_register(&svm->notifier, mm);
  270. if (ret) {
  271. kfree(svm);
  272. return ERR_PTR(ret);
  273. }
  274. ret = pasid_private_add(svm->pasid, svm);
  275. if (ret) {
  276. mmu_notifier_unregister(&svm->notifier, mm);
  277. kfree(svm);
  278. return ERR_PTR(ret);
  279. }
  280. }
  281. /* Find the matching device in svm list */
  282. sdev = svm_lookup_device_by_dev(svm, dev);
  283. if (sdev) {
  284. sdev->users++;
  285. goto success;
  286. }
  287. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  288. if (!sdev) {
  289. ret = -ENOMEM;
  290. goto free_svm;
  291. }
  292. sdev->dev = dev;
  293. sdev->iommu = iommu;
  294. sdev->did = FLPT_DEFAULT_DID;
  295. sdev->sid = PCI_DEVID(info->bus, info->devfn);
  296. sdev->users = 1;
  297. sdev->pasid = svm->pasid;
  298. sdev->sva.dev = dev;
  299. init_rcu_head(&sdev->rcu);
  300. if (info->ats_enabled) {
  301. sdev->dev_iotlb = 1;
  302. sdev->qdep = info->ats_qdep;
  303. if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
  304. sdev->qdep = 0;
  305. }
  306. /* Setup the pasid table: */
  307. sflags = cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0;
  308. ret = intel_pasid_setup_first_level(iommu, dev, mm->pgd, mm->pasid,
  309. FLPT_DEFAULT_DID, sflags);
  310. if (ret)
  311. goto free_sdev;
  312. list_add_rcu(&sdev->list, &svm->devs);
  313. success:
  314. return &sdev->sva;
  315. free_sdev:
  316. kfree(sdev);
  317. free_svm:
  318. if (list_empty(&svm->devs)) {
  319. mmu_notifier_unregister(&svm->notifier, mm);
  320. pasid_private_remove(mm->pasid);
  321. kfree(svm);
  322. }
  323. return ERR_PTR(ret);
  324. }
  325. /* Caller must hold pasid_mutex */
  326. static int intel_svm_unbind_mm(struct device *dev, u32 pasid)
  327. {
  328. struct intel_svm_dev *sdev;
  329. struct intel_iommu *iommu;
  330. struct intel_svm *svm;
  331. struct mm_struct *mm;
  332. int ret = -EINVAL;
  333. iommu = device_to_iommu(dev, NULL, NULL);
  334. if (!iommu)
  335. goto out;
  336. ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
  337. if (ret)
  338. goto out;
  339. mm = svm->mm;
  340. if (sdev) {
  341. sdev->users--;
  342. if (!sdev->users) {
  343. list_del_rcu(&sdev->list);
  344. /* Flush the PASID cache and IOTLB for this device.
  345. * Note that we do depend on the hardware *not* using
  346. * the PASID any more. Just as we depend on other
  347. * devices never using PASIDs that they have no right
  348. * to use. We have a *shared* PASID table, because it's
  349. * large and has to be physically contiguous. So it's
  350. * hard to be as defensive as we might like. */
  351. intel_pasid_tear_down_entry(iommu, dev,
  352. svm->pasid, false);
  353. intel_svm_drain_prq(dev, svm->pasid);
  354. kfree_rcu(sdev, rcu);
  355. if (list_empty(&svm->devs)) {
  356. if (svm->notifier.ops)
  357. mmu_notifier_unregister(&svm->notifier, mm);
  358. pasid_private_remove(svm->pasid);
  359. /* We mandate that no page faults may be outstanding
  360. * for the PASID when intel_svm_unbind_mm() is called.
  361. * If that is not obeyed, subtle errors will happen.
  362. * Let's make them less subtle... */
  363. memset(svm, 0x6b, sizeof(*svm));
  364. kfree(svm);
  365. }
  366. }
  367. }
  368. out:
  369. return ret;
  370. }
  371. /* Page request queue descriptor */
  372. struct page_req_dsc {
  373. union {
  374. struct {
  375. u64 type:8;
  376. u64 pasid_present:1;
  377. u64 priv_data_present:1;
  378. u64 rsvd:6;
  379. u64 rid:16;
  380. u64 pasid:20;
  381. u64 exe_req:1;
  382. u64 pm_req:1;
  383. u64 rsvd2:10;
  384. };
  385. u64 qw_0;
  386. };
  387. union {
  388. struct {
  389. u64 rd_req:1;
  390. u64 wr_req:1;
  391. u64 lpig:1;
  392. u64 prg_index:9;
  393. u64 addr:52;
  394. };
  395. u64 qw_1;
  396. };
  397. u64 priv_data[2];
  398. };
  399. static bool is_canonical_address(u64 addr)
  400. {
  401. int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
  402. long saddr = (long) addr;
  403. return (((saddr << shift) >> shift) == saddr);
  404. }
  405. /**
  406. * intel_svm_drain_prq - Drain page requests and responses for a pasid
  407. * @dev: target device
  408. * @pasid: pasid for draining
  409. *
  410. * Drain all pending page requests and responses related to @pasid in both
  411. * software and hardware. This is supposed to be called after the device
  412. * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
  413. * and DevTLB have been invalidated.
  414. *
  415. * It waits until all pending page requests for @pasid in the page fault
  416. * queue are completed by the prq handling thread. Then follow the steps
  417. * described in VT-d spec CH7.10 to drain all page requests and page
  418. * responses pending in the hardware.
  419. */
  420. static void intel_svm_drain_prq(struct device *dev, u32 pasid)
  421. {
  422. struct device_domain_info *info;
  423. struct dmar_domain *domain;
  424. struct intel_iommu *iommu;
  425. struct qi_desc desc[3];
  426. struct pci_dev *pdev;
  427. int head, tail;
  428. u16 sid, did;
  429. int qdep;
  430. info = dev_iommu_priv_get(dev);
  431. if (WARN_ON(!info || !dev_is_pci(dev)))
  432. return;
  433. if (!info->pri_enabled)
  434. return;
  435. iommu = info->iommu;
  436. domain = info->domain;
  437. pdev = to_pci_dev(dev);
  438. sid = PCI_DEVID(info->bus, info->devfn);
  439. did = domain_id_iommu(domain, iommu);
  440. qdep = pci_ats_queue_depth(pdev);
  441. /*
  442. * Check and wait until all pending page requests in the queue are
  443. * handled by the prq handling thread.
  444. */
  445. prq_retry:
  446. reinit_completion(&iommu->prq_complete);
  447. tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
  448. head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
  449. while (head != tail) {
  450. struct page_req_dsc *req;
  451. req = &iommu->prq[head / sizeof(*req)];
  452. if (!req->pasid_present || req->pasid != pasid) {
  453. head = (head + sizeof(*req)) & PRQ_RING_MASK;
  454. continue;
  455. }
  456. wait_for_completion(&iommu->prq_complete);
  457. goto prq_retry;
  458. }
  459. /*
  460. * A work in IO page fault workqueue may try to lock pasid_mutex now.
  461. * Holding pasid_mutex while waiting in iopf_queue_flush_dev() for
  462. * all works in the workqueue to finish may cause deadlock.
  463. *
  464. * It's unnecessary to hold pasid_mutex in iopf_queue_flush_dev().
  465. * Unlock it to allow the works to be handled while waiting for
  466. * them to finish.
  467. */
  468. lockdep_assert_held(&pasid_mutex);
  469. mutex_unlock(&pasid_mutex);
  470. iopf_queue_flush_dev(dev);
  471. mutex_lock(&pasid_mutex);
  472. /*
  473. * Perform steps described in VT-d spec CH7.10 to drain page
  474. * requests and responses in hardware.
  475. */
  476. memset(desc, 0, sizeof(desc));
  477. desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
  478. QI_IWD_FENCE |
  479. QI_IWD_TYPE;
  480. desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
  481. QI_EIOTLB_DID(did) |
  482. QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
  483. QI_EIOTLB_TYPE;
  484. desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
  485. QI_DEV_EIOTLB_SID(sid) |
  486. QI_DEV_EIOTLB_QDEP(qdep) |
  487. QI_DEIOTLB_TYPE |
  488. QI_DEV_IOTLB_PFSID(info->pfsid);
  489. qi_retry:
  490. reinit_completion(&iommu->prq_complete);
  491. qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
  492. if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
  493. wait_for_completion(&iommu->prq_complete);
  494. goto qi_retry;
  495. }
  496. }
  497. static int prq_to_iommu_prot(struct page_req_dsc *req)
  498. {
  499. int prot = 0;
  500. if (req->rd_req)
  501. prot |= IOMMU_FAULT_PERM_READ;
  502. if (req->wr_req)
  503. prot |= IOMMU_FAULT_PERM_WRITE;
  504. if (req->exe_req)
  505. prot |= IOMMU_FAULT_PERM_EXEC;
  506. if (req->pm_req)
  507. prot |= IOMMU_FAULT_PERM_PRIV;
  508. return prot;
  509. }
  510. static int intel_svm_prq_report(struct intel_iommu *iommu, struct device *dev,
  511. struct page_req_dsc *desc)
  512. {
  513. struct iommu_fault_event event;
  514. if (!dev || !dev_is_pci(dev))
  515. return -ENODEV;
  516. /* Fill in event data for device specific processing */
  517. memset(&event, 0, sizeof(struct iommu_fault_event));
  518. event.fault.type = IOMMU_FAULT_PAGE_REQ;
  519. event.fault.prm.addr = (u64)desc->addr << VTD_PAGE_SHIFT;
  520. event.fault.prm.pasid = desc->pasid;
  521. event.fault.prm.grpid = desc->prg_index;
  522. event.fault.prm.perm = prq_to_iommu_prot(desc);
  523. if (desc->lpig)
  524. event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
  525. if (desc->pasid_present) {
  526. event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
  527. event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
  528. }
  529. if (desc->priv_data_present) {
  530. /*
  531. * Set last page in group bit if private data is present,
  532. * page response is required as it does for LPIG.
  533. * iommu_report_device_fault() doesn't understand this vendor
  534. * specific requirement thus we set last_page as a workaround.
  535. */
  536. event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
  537. event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
  538. event.fault.prm.private_data[0] = desc->priv_data[0];
  539. event.fault.prm.private_data[1] = desc->priv_data[1];
  540. } else if (dmar_latency_enabled(iommu, DMAR_LATENCY_PRQ)) {
  541. /*
  542. * If the private data fields are not used by hardware, use it
  543. * to monitor the prq handle latency.
  544. */
  545. event.fault.prm.private_data[0] = ktime_to_ns(ktime_get());
  546. }
  547. return iommu_report_device_fault(dev, &event);
  548. }
  549. static void handle_bad_prq_event(struct intel_iommu *iommu,
  550. struct page_req_dsc *req, int result)
  551. {
  552. struct qi_desc desc;
  553. pr_err("%s: Invalid page request: %08llx %08llx\n",
  554. iommu->name, ((unsigned long long *)req)[0],
  555. ((unsigned long long *)req)[1]);
  556. /*
  557. * Per VT-d spec. v3.0 ch7.7, system software must
  558. * respond with page group response if private data
  559. * is present (PDP) or last page in group (LPIG) bit
  560. * is set. This is an additional VT-d feature beyond
  561. * PCI ATS spec.
  562. */
  563. if (!req->lpig && !req->priv_data_present)
  564. return;
  565. desc.qw0 = QI_PGRP_PASID(req->pasid) |
  566. QI_PGRP_DID(req->rid) |
  567. QI_PGRP_PASID_P(req->pasid_present) |
  568. QI_PGRP_PDP(req->priv_data_present) |
  569. QI_PGRP_RESP_CODE(result) |
  570. QI_PGRP_RESP_TYPE;
  571. desc.qw1 = QI_PGRP_IDX(req->prg_index) |
  572. QI_PGRP_LPIG(req->lpig);
  573. if (req->priv_data_present) {
  574. desc.qw2 = req->priv_data[0];
  575. desc.qw3 = req->priv_data[1];
  576. } else {
  577. desc.qw2 = 0;
  578. desc.qw3 = 0;
  579. }
  580. qi_submit_sync(iommu, &desc, 1, 0);
  581. }
  582. static irqreturn_t prq_event_thread(int irq, void *d)
  583. {
  584. struct intel_iommu *iommu = d;
  585. struct page_req_dsc *req;
  586. int head, tail, handled;
  587. struct pci_dev *pdev;
  588. u64 address;
  589. /*
  590. * Clear PPR bit before reading head/tail registers, to ensure that
  591. * we get a new interrupt if needed.
  592. */
  593. writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
  594. tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
  595. head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
  596. handled = (head != tail);
  597. while (head != tail) {
  598. req = &iommu->prq[head / sizeof(*req)];
  599. address = (u64)req->addr << VTD_PAGE_SHIFT;
  600. if (unlikely(!req->pasid_present)) {
  601. pr_err("IOMMU: %s: Page request without PASID\n",
  602. iommu->name);
  603. bad_req:
  604. handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
  605. goto prq_advance;
  606. }
  607. if (unlikely(!is_canonical_address(address))) {
  608. pr_err("IOMMU: %s: Address is not canonical\n",
  609. iommu->name);
  610. goto bad_req;
  611. }
  612. if (unlikely(req->pm_req && (req->rd_req | req->wr_req))) {
  613. pr_err("IOMMU: %s: Page request in Privilege Mode\n",
  614. iommu->name);
  615. goto bad_req;
  616. }
  617. if (unlikely(req->exe_req && req->rd_req)) {
  618. pr_err("IOMMU: %s: Execution request not supported\n",
  619. iommu->name);
  620. goto bad_req;
  621. }
  622. /* Drop Stop Marker message. No need for a response. */
  623. if (unlikely(req->lpig && !req->rd_req && !req->wr_req))
  624. goto prq_advance;
  625. pdev = pci_get_domain_bus_and_slot(iommu->segment,
  626. PCI_BUS_NUM(req->rid),
  627. req->rid & 0xff);
  628. /*
  629. * If prq is to be handled outside iommu driver via receiver of
  630. * the fault notifiers, we skip the page response here.
  631. */
  632. if (!pdev)
  633. goto bad_req;
  634. if (intel_svm_prq_report(iommu, &pdev->dev, req))
  635. handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
  636. else
  637. trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
  638. req->priv_data[0], req->priv_data[1],
  639. iommu->prq_seq_number++);
  640. pci_dev_put(pdev);
  641. prq_advance:
  642. head = (head + sizeof(*req)) & PRQ_RING_MASK;
  643. }
  644. dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
  645. /*
  646. * Clear the page request overflow bit and wake up all threads that
  647. * are waiting for the completion of this handling.
  648. */
  649. if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
  650. pr_info_ratelimited("IOMMU: %s: PRQ overflow detected\n",
  651. iommu->name);
  652. head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
  653. tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
  654. if (head == tail) {
  655. iopf_queue_discard_partial(iommu->iopf_queue);
  656. writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
  657. pr_info_ratelimited("IOMMU: %s: PRQ overflow cleared",
  658. iommu->name);
  659. }
  660. }
  661. if (!completion_done(&iommu->prq_complete))
  662. complete(&iommu->prq_complete);
  663. return IRQ_RETVAL(handled);
  664. }
  665. int intel_svm_page_response(struct device *dev,
  666. struct iommu_fault_event *evt,
  667. struct iommu_page_response *msg)
  668. {
  669. struct iommu_fault_page_request *prm;
  670. struct intel_iommu *iommu;
  671. bool private_present;
  672. bool pasid_present;
  673. bool last_page;
  674. u8 bus, devfn;
  675. int ret = 0;
  676. u16 sid;
  677. if (!dev || !dev_is_pci(dev))
  678. return -ENODEV;
  679. iommu = device_to_iommu(dev, &bus, &devfn);
  680. if (!iommu)
  681. return -ENODEV;
  682. if (!msg || !evt)
  683. return -EINVAL;
  684. prm = &evt->fault.prm;
  685. sid = PCI_DEVID(bus, devfn);
  686. pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
  687. private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
  688. last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
  689. if (!pasid_present) {
  690. ret = -EINVAL;
  691. goto out;
  692. }
  693. if (prm->pasid == 0 || prm->pasid >= PASID_MAX) {
  694. ret = -EINVAL;
  695. goto out;
  696. }
  697. /*
  698. * Per VT-d spec. v3.0 ch7.7, system software must respond
  699. * with page group response if private data is present (PDP)
  700. * or last page in group (LPIG) bit is set. This is an
  701. * additional VT-d requirement beyond PCI ATS spec.
  702. */
  703. if (last_page || private_present) {
  704. struct qi_desc desc;
  705. desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) |
  706. QI_PGRP_PASID_P(pasid_present) |
  707. QI_PGRP_PDP(private_present) |
  708. QI_PGRP_RESP_CODE(msg->code) |
  709. QI_PGRP_RESP_TYPE;
  710. desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page);
  711. desc.qw2 = 0;
  712. desc.qw3 = 0;
  713. if (private_present) {
  714. desc.qw2 = prm->private_data[0];
  715. desc.qw3 = prm->private_data[1];
  716. } else if (prm->private_data[0]) {
  717. dmar_latency_update(iommu, DMAR_LATENCY_PRQ,
  718. ktime_to_ns(ktime_get()) - prm->private_data[0]);
  719. }
  720. qi_submit_sync(iommu, &desc, 1, 0);
  721. }
  722. out:
  723. return ret;
  724. }
  725. void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid)
  726. {
  727. mutex_lock(&pasid_mutex);
  728. intel_svm_unbind_mm(dev, pasid);
  729. mutex_unlock(&pasid_mutex);
  730. }
  731. static int intel_svm_set_dev_pasid(struct iommu_domain *domain,
  732. struct device *dev, ioasid_t pasid)
  733. {
  734. struct device_domain_info *info = dev_iommu_priv_get(dev);
  735. struct intel_iommu *iommu = info->iommu;
  736. struct mm_struct *mm = domain->mm;
  737. struct iommu_sva *sva;
  738. int ret = 0;
  739. mutex_lock(&pasid_mutex);
  740. sva = intel_svm_bind_mm(iommu, dev, mm);
  741. if (IS_ERR(sva))
  742. ret = PTR_ERR(sva);
  743. mutex_unlock(&pasid_mutex);
  744. return ret;
  745. }
  746. static void intel_svm_domain_free(struct iommu_domain *domain)
  747. {
  748. kfree(to_dmar_domain(domain));
  749. }
  750. static const struct iommu_domain_ops intel_svm_domain_ops = {
  751. .set_dev_pasid = intel_svm_set_dev_pasid,
  752. .free = intel_svm_domain_free
  753. };
  754. struct iommu_domain *intel_svm_domain_alloc(void)
  755. {
  756. struct dmar_domain *domain;
  757. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  758. if (!domain)
  759. return NULL;
  760. domain->domain.ops = &intel_svm_domain_ops;
  761. return &domain->domain;
  762. }