pasid.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * intel-pasid.c - PASID idr, table and entry manipulation
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. *
  7. * Author: Lu Baolu <baolu.lu@linux.intel.com>
  8. */
  9. #define pr_fmt(fmt) "DMAR: " fmt
  10. #include <linux/bitops.h>
  11. #include <linux/cpufeature.h>
  12. #include <linux/dmar.h>
  13. #include <linux/iommu.h>
  14. #include <linux/memory.h>
  15. #include <linux/pci.h>
  16. #include <linux/pci-ats.h>
  17. #include <linux/spinlock.h>
  18. #include "iommu.h"
  19. #include "pasid.h"
  20. #include "../iommu-pages.h"
  21. /*
  22. * Intel IOMMU system wide PASID name space:
  23. */
  24. u32 intel_pasid_max_id = PASID_MAX;
  25. /*
  26. * Per device pasid table management:
  27. */
  28. /*
  29. * Allocate a pasid table for @dev. It should be called in a
  30. * single-thread context.
  31. */
  32. int intel_pasid_alloc_table(struct device *dev)
  33. {
  34. struct device_domain_info *info;
  35. struct pasid_table *pasid_table;
  36. struct pasid_dir_entry *dir;
  37. u32 max_pasid = 0;
  38. int order, size;
  39. might_sleep();
  40. info = dev_iommu_priv_get(dev);
  41. if (WARN_ON(!info || !dev_is_pci(dev)))
  42. return -ENODEV;
  43. if (WARN_ON(info->pasid_table))
  44. return -EEXIST;
  45. pasid_table = kzalloc_obj(*pasid_table);
  46. if (!pasid_table)
  47. return -ENOMEM;
  48. if (info->pasid_supported)
  49. max_pasid = min_t(u32, pci_max_pasids(to_pci_dev(dev)),
  50. intel_pasid_max_id);
  51. size = max_pasid >> (PASID_PDE_SHIFT - 3);
  52. order = size ? get_order(size) : 0;
  53. dir = iommu_alloc_pages_node_sz(info->iommu->node, GFP_KERNEL,
  54. 1 << (order + PAGE_SHIFT));
  55. if (!dir) {
  56. kfree(pasid_table);
  57. return -ENOMEM;
  58. }
  59. pasid_table->table = dir;
  60. pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
  61. info->pasid_table = pasid_table;
  62. if (!ecap_coherent(info->iommu->ecap))
  63. clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE);
  64. return 0;
  65. }
  66. void intel_pasid_free_table(struct device *dev)
  67. {
  68. struct device_domain_info *info;
  69. struct pasid_table *pasid_table;
  70. struct pasid_dir_entry *dir;
  71. struct pasid_entry *table;
  72. int i, max_pde;
  73. info = dev_iommu_priv_get(dev);
  74. if (!info || !dev_is_pci(dev) || !info->pasid_table)
  75. return;
  76. pasid_table = info->pasid_table;
  77. info->pasid_table = NULL;
  78. /* Free scalable mode PASID directory tables: */
  79. dir = pasid_table->table;
  80. max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
  81. for (i = 0; i < max_pde; i++) {
  82. table = get_pasid_table_from_pde(&dir[i]);
  83. iommu_free_pages(table);
  84. }
  85. iommu_free_pages(pasid_table->table);
  86. kfree(pasid_table);
  87. }
  88. struct pasid_table *intel_pasid_get_table(struct device *dev)
  89. {
  90. struct device_domain_info *info;
  91. info = dev_iommu_priv_get(dev);
  92. if (!info)
  93. return NULL;
  94. return info->pasid_table;
  95. }
  96. static int intel_pasid_get_dev_max_id(struct device *dev)
  97. {
  98. struct device_domain_info *info;
  99. info = dev_iommu_priv_get(dev);
  100. if (!info || !info->pasid_table)
  101. return 0;
  102. return info->pasid_table->max_pasid;
  103. }
  104. static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
  105. {
  106. struct device_domain_info *info;
  107. struct pasid_table *pasid_table;
  108. struct pasid_dir_entry *dir;
  109. struct pasid_entry *entries;
  110. int dir_index, index;
  111. pasid_table = intel_pasid_get_table(dev);
  112. if (WARN_ON(!pasid_table || pasid >= intel_pasid_get_dev_max_id(dev)))
  113. return NULL;
  114. dir = pasid_table->table;
  115. info = dev_iommu_priv_get(dev);
  116. dir_index = pasid >> PASID_PDE_SHIFT;
  117. index = pasid & PASID_PTE_MASK;
  118. retry:
  119. entries = get_pasid_table_from_pde(&dir[dir_index]);
  120. if (!entries) {
  121. u64 tmp;
  122. entries = iommu_alloc_pages_node_sz(info->iommu->node,
  123. GFP_ATOMIC, SZ_4K);
  124. if (!entries)
  125. return NULL;
  126. if (!ecap_coherent(info->iommu->ecap))
  127. clflush_cache_range(entries, VTD_PAGE_SIZE);
  128. /*
  129. * The pasid directory table entry won't be freed after
  130. * allocation. No worry about the race with free and
  131. * clear. However, this entry might be populated by others
  132. * while we are preparing it. Use theirs with a retry.
  133. */
  134. tmp = 0ULL;
  135. if (!try_cmpxchg64(&dir[dir_index].val, &tmp,
  136. (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {
  137. iommu_free_pages(entries);
  138. goto retry;
  139. }
  140. if (!ecap_coherent(info->iommu->ecap))
  141. clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
  142. }
  143. return &entries[index];
  144. }
  145. /*
  146. * Interfaces for PASID table entry manipulation:
  147. */
  148. static void
  149. intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore)
  150. {
  151. struct pasid_entry *pe;
  152. pe = intel_pasid_get_entry(dev, pasid);
  153. if (WARN_ON(!pe))
  154. return;
  155. if (fault_ignore && pasid_pte_is_present(pe))
  156. pasid_clear_entry_with_fpd(pe);
  157. else
  158. pasid_clear_entry(pe);
  159. }
  160. static void
  161. pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
  162. u16 did, u32 pasid)
  163. {
  164. struct qi_desc desc;
  165. desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) |
  166. QI_PC_PASID(pasid) | QI_PC_TYPE;
  167. desc.qw1 = 0;
  168. desc.qw2 = 0;
  169. desc.qw3 = 0;
  170. qi_submit_sync(iommu, &desc, 1, 0);
  171. }
  172. static void
  173. devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
  174. struct device *dev, u32 pasid)
  175. {
  176. struct device_domain_info *info;
  177. u16 sid, qdep, pfsid;
  178. info = dev_iommu_priv_get(dev);
  179. if (!info || !info->ats_enabled)
  180. return;
  181. if (!pci_device_is_present(to_pci_dev(dev)))
  182. return;
  183. sid = PCI_DEVID(info->bus, info->devfn);
  184. qdep = info->ats_qdep;
  185. pfsid = info->pfsid;
  186. /*
  187. * When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID),
  188. * devTLB flush w/o PASID should be used. For non-zero PASID under
  189. * SVA usage, device could do DMA with multiple PASIDs. It is more
  190. * efficient to flush devTLB specific to the PASID.
  191. */
  192. if (pasid == IOMMU_NO_PASID)
  193. qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
  194. else
  195. qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
  196. }
  197. void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
  198. u32 pasid, bool fault_ignore)
  199. {
  200. struct pasid_entry *pte;
  201. u16 did, pgtt;
  202. spin_lock(&iommu->lock);
  203. pte = intel_pasid_get_entry(dev, pasid);
  204. if (WARN_ON(!pte)) {
  205. spin_unlock(&iommu->lock);
  206. return;
  207. }
  208. if (!pasid_pte_is_present(pte)) {
  209. if (!pasid_pte_is_fault_disabled(pte)) {
  210. WARN_ON(READ_ONCE(pte->val[0]) != 0);
  211. spin_unlock(&iommu->lock);
  212. return;
  213. }
  214. /*
  215. * When a PASID is used for SVA by a device, it's possible
  216. * that the pasid entry is non-present with the Fault
  217. * Processing Disabled bit set. Clear the pasid entry and
  218. * drain the PRQ for the PASID before return.
  219. */
  220. pasid_clear_entry(pte);
  221. spin_unlock(&iommu->lock);
  222. intel_iommu_drain_pasid_prq(dev, pasid);
  223. return;
  224. }
  225. did = pasid_get_domain_id(pte);
  226. pgtt = pasid_pte_get_pgtt(pte);
  227. pasid_clear_present(pte);
  228. spin_unlock(&iommu->lock);
  229. if (!ecap_coherent(iommu->ecap))
  230. clflush_cache_range(pte, sizeof(*pte));
  231. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  232. if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY)
  233. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  234. else
  235. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  236. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  237. intel_pasid_clear_entry(dev, pasid, fault_ignore);
  238. if (!ecap_coherent(iommu->ecap))
  239. clflush_cache_range(pte, sizeof(*pte));
  240. if (!fault_ignore)
  241. intel_iommu_drain_pasid_prq(dev, pasid);
  242. }
  243. /*
  244. * This function flushes cache for a newly setup pasid table entry.
  245. * Caller of it should not modify the in-use pasid table entries.
  246. */
  247. static void pasid_flush_caches(struct intel_iommu *iommu,
  248. struct pasid_entry *pte,
  249. u32 pasid, u16 did)
  250. {
  251. if (!ecap_coherent(iommu->ecap))
  252. clflush_cache_range(pte, sizeof(*pte));
  253. if (cap_caching_mode(iommu->cap)) {
  254. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  255. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  256. } else {
  257. iommu_flush_write_buffer(iommu);
  258. }
  259. }
  260. /*
  261. * This function is supposed to be used after caller updates the fields
  262. * except for the SSADE and P bit of a pasid table entry. It does the
  263. * below:
  264. * - Flush cacheline if needed
  265. * - Flush the caches per Table 28 ”Guidance to Software for Invalidations“
  266. * of VT-d spec 5.0.
  267. */
  268. static void intel_pasid_flush_present(struct intel_iommu *iommu,
  269. struct device *dev,
  270. u32 pasid, u16 did,
  271. struct pasid_entry *pte)
  272. {
  273. if (!ecap_coherent(iommu->ecap))
  274. clflush_cache_range(pte, sizeof(*pte));
  275. /*
  276. * VT-d spec 5.0 table28 states guides for cache invalidation:
  277. *
  278. * - PASID-selective-within-Domain PASID-cache invalidation
  279. * - PASID-selective PASID-based IOTLB invalidation
  280. * - If (pasid is RID_PASID)
  281. * - Global Device-TLB invalidation to affected functions
  282. * Else
  283. * - PASID-based Device-TLB invalidation (with S=1 and
  284. * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
  285. */
  286. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  287. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  288. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  289. }
  290. /*
  291. * Set up the scalable mode pasid table entry for first only
  292. * translation type.
  293. */
  294. static void pasid_pte_config_first_level(struct intel_iommu *iommu,
  295. struct pasid_entry *pte,
  296. phys_addr_t fsptptr, u16 did,
  297. int flags)
  298. {
  299. lockdep_assert_held(&iommu->lock);
  300. pasid_clear_entry(pte);
  301. /* Setup the first level page table pointer: */
  302. pasid_set_flptr(pte, fsptptr);
  303. if (flags & PASID_FLAG_FL5LP)
  304. pasid_set_flpm(pte, 1);
  305. if (flags & PASID_FLAG_PAGE_SNOOP)
  306. pasid_set_pgsnp(pte);
  307. pasid_set_domain_id(pte, did);
  308. pasid_set_address_width(pte, iommu->agaw);
  309. pasid_set_page_snoop(pte, flags & PASID_FLAG_PWSNP);
  310. /* Setup Present and PASID Granular Transfer Type: */
  311. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
  312. pasid_set_present(pte);
  313. }
  314. int intel_pasid_setup_first_level(struct intel_iommu *iommu, struct device *dev,
  315. phys_addr_t fsptptr, u32 pasid, u16 did,
  316. int flags)
  317. {
  318. struct pasid_entry *pte;
  319. if (!ecap_flts(iommu->ecap)) {
  320. pr_err("No first level translation support on %s\n",
  321. iommu->name);
  322. return -EINVAL;
  323. }
  324. if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
  325. pr_err("No 5-level paging support for first-level on %s\n",
  326. iommu->name);
  327. return -EINVAL;
  328. }
  329. spin_lock(&iommu->lock);
  330. pte = intel_pasid_get_entry(dev, pasid);
  331. if (!pte) {
  332. spin_unlock(&iommu->lock);
  333. return -ENODEV;
  334. }
  335. if (pasid_pte_is_present(pte)) {
  336. spin_unlock(&iommu->lock);
  337. return -EBUSY;
  338. }
  339. pasid_pte_config_first_level(iommu, pte, fsptptr, did, flags);
  340. spin_unlock(&iommu->lock);
  341. pasid_flush_caches(iommu, pte, pasid, did);
  342. return 0;
  343. }
  344. /*
  345. * Set up the scalable mode pasid entry for second only translation type.
  346. */
  347. static void pasid_pte_config_second_level(struct intel_iommu *iommu,
  348. struct pasid_entry *pte,
  349. struct dmar_domain *domain, u16 did)
  350. {
  351. struct pt_iommu_vtdss_hw_info pt_info;
  352. lockdep_assert_held(&iommu->lock);
  353. pt_iommu_vtdss_hw_info(&domain->sspt, &pt_info);
  354. pasid_clear_entry(pte);
  355. pasid_set_domain_id(pte, did);
  356. pasid_set_slptr(pte, pt_info.ssptptr);
  357. pasid_set_address_width(pte, pt_info.aw);
  358. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
  359. pasid_set_fault_enable(pte);
  360. pasid_set_page_snoop(pte, !(domain->sspt.vtdss_pt.common.features &
  361. BIT(PT_FEAT_DMA_INCOHERENT)));
  362. if (domain->dirty_tracking)
  363. pasid_set_ssade(pte);
  364. pasid_set_present(pte);
  365. }
  366. int intel_pasid_setup_second_level(struct intel_iommu *iommu,
  367. struct dmar_domain *domain,
  368. struct device *dev, u32 pasid)
  369. {
  370. struct pasid_entry *pte;
  371. u16 did;
  372. /*
  373. * If hardware advertises no support for second level
  374. * translation, return directly.
  375. */
  376. if (!ecap_slts(iommu->ecap)) {
  377. pr_err("No second level translation support on %s\n",
  378. iommu->name);
  379. return -EINVAL;
  380. }
  381. did = domain_id_iommu(domain, iommu);
  382. spin_lock(&iommu->lock);
  383. pte = intel_pasid_get_entry(dev, pasid);
  384. if (!pte) {
  385. spin_unlock(&iommu->lock);
  386. return -ENODEV;
  387. }
  388. if (pasid_pte_is_present(pte)) {
  389. spin_unlock(&iommu->lock);
  390. return -EBUSY;
  391. }
  392. pasid_pte_config_second_level(iommu, pte, domain, did);
  393. spin_unlock(&iommu->lock);
  394. pasid_flush_caches(iommu, pte, pasid, did);
  395. return 0;
  396. }
  397. /*
  398. * Set up dirty tracking on a second only or nested translation type.
  399. */
  400. int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu,
  401. struct device *dev, u32 pasid,
  402. bool enabled)
  403. {
  404. struct pasid_entry *pte;
  405. u16 did, pgtt;
  406. spin_lock(&iommu->lock);
  407. pte = intel_pasid_get_entry(dev, pasid);
  408. if (!pte) {
  409. spin_unlock(&iommu->lock);
  410. dev_err_ratelimited(
  411. dev, "Failed to get pasid entry of PASID %d\n", pasid);
  412. return -ENODEV;
  413. }
  414. did = pasid_get_domain_id(pte);
  415. pgtt = pasid_pte_get_pgtt(pte);
  416. if (pgtt != PASID_ENTRY_PGTT_SL_ONLY &&
  417. pgtt != PASID_ENTRY_PGTT_NESTED) {
  418. spin_unlock(&iommu->lock);
  419. dev_err_ratelimited(
  420. dev,
  421. "Dirty tracking not supported on translation type %d\n",
  422. pgtt);
  423. return -EOPNOTSUPP;
  424. }
  425. if (pasid_get_ssade(pte) == enabled) {
  426. spin_unlock(&iommu->lock);
  427. return 0;
  428. }
  429. if (enabled)
  430. pasid_set_ssade(pte);
  431. else
  432. pasid_clear_ssade(pte);
  433. spin_unlock(&iommu->lock);
  434. if (!ecap_coherent(iommu->ecap))
  435. clflush_cache_range(pte, sizeof(*pte));
  436. /*
  437. * From VT-d spec table 25 "Guidance to Software for Invalidations":
  438. *
  439. * - PASID-selective-within-Domain PASID-cache invalidation
  440. * If (PGTT=SS or Nested)
  441. * - Domain-selective IOTLB invalidation
  442. * Else
  443. * - PASID-selective PASID-based IOTLB invalidation
  444. * - If (pasid is RID_PASID)
  445. * - Global Device-TLB invalidation to affected functions
  446. * Else
  447. * - PASID-based Device-TLB invalidation (with S=1 and
  448. * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
  449. */
  450. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  451. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  452. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  453. return 0;
  454. }
  455. /*
  456. * Set up the scalable mode pasid entry for passthrough translation type.
  457. */
  458. static void pasid_pte_config_pass_through(struct intel_iommu *iommu,
  459. struct pasid_entry *pte, u16 did)
  460. {
  461. lockdep_assert_held(&iommu->lock);
  462. pasid_clear_entry(pte);
  463. pasid_set_domain_id(pte, did);
  464. pasid_set_address_width(pte, iommu->agaw);
  465. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
  466. pasid_set_fault_enable(pte);
  467. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  468. pasid_set_present(pte);
  469. }
  470. int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
  471. struct device *dev, u32 pasid)
  472. {
  473. u16 did = FLPT_DEFAULT_DID;
  474. struct pasid_entry *pte;
  475. spin_lock(&iommu->lock);
  476. pte = intel_pasid_get_entry(dev, pasid);
  477. if (!pte) {
  478. spin_unlock(&iommu->lock);
  479. return -ENODEV;
  480. }
  481. if (pasid_pte_is_present(pte)) {
  482. spin_unlock(&iommu->lock);
  483. return -EBUSY;
  484. }
  485. pasid_pte_config_pass_through(iommu, pte, did);
  486. spin_unlock(&iommu->lock);
  487. pasid_flush_caches(iommu, pte, pasid, did);
  488. return 0;
  489. }
  490. /*
  491. * Set the page snoop control for a pasid entry which has been set up.
  492. */
  493. void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
  494. struct device *dev, u32 pasid)
  495. {
  496. struct pasid_entry *pte;
  497. u16 did;
  498. spin_lock(&iommu->lock);
  499. pte = intel_pasid_get_entry(dev, pasid);
  500. if (WARN_ON(!pte || !pasid_pte_is_present(pte))) {
  501. spin_unlock(&iommu->lock);
  502. return;
  503. }
  504. pasid_set_pgsnp(pte);
  505. did = pasid_get_domain_id(pte);
  506. spin_unlock(&iommu->lock);
  507. intel_pasid_flush_present(iommu, dev, pasid, did, pte);
  508. }
  509. static void pasid_pte_config_nestd(struct intel_iommu *iommu,
  510. struct pasid_entry *pte,
  511. struct iommu_hwpt_vtd_s1 *s1_cfg,
  512. struct dmar_domain *s2_domain,
  513. u16 did)
  514. {
  515. struct pt_iommu_vtdss_hw_info pt_info;
  516. lockdep_assert_held(&iommu->lock);
  517. pt_iommu_vtdss_hw_info(&s2_domain->sspt, &pt_info);
  518. pasid_clear_entry(pte);
  519. if (s1_cfg->addr_width == ADDR_WIDTH_5LEVEL)
  520. pasid_set_flpm(pte, 1);
  521. pasid_set_flptr(pte, s1_cfg->pgtbl_addr);
  522. if (s1_cfg->flags & IOMMU_VTD_S1_SRE) {
  523. pasid_set_sre(pte);
  524. if (s1_cfg->flags & IOMMU_VTD_S1_WPE)
  525. pasid_set_wpe(pte);
  526. }
  527. if (s1_cfg->flags & IOMMU_VTD_S1_EAFE)
  528. pasid_set_eafe(pte);
  529. if (s2_domain->force_snooping)
  530. pasid_set_pgsnp(pte);
  531. pasid_set_slptr(pte, pt_info.ssptptr);
  532. pasid_set_fault_enable(pte);
  533. pasid_set_domain_id(pte, did);
  534. pasid_set_address_width(pte, pt_info.aw);
  535. pasid_set_page_snoop(pte, !(s2_domain->sspt.vtdss_pt.common.features &
  536. BIT(PT_FEAT_DMA_INCOHERENT)));
  537. if (s2_domain->dirty_tracking)
  538. pasid_set_ssade(pte);
  539. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED);
  540. pasid_set_present(pte);
  541. }
  542. /**
  543. * intel_pasid_setup_nested() - Set up PASID entry for nested translation.
  544. * @iommu: IOMMU which the device belong to
  545. * @dev: Device to be set up for translation
  546. * @pasid: PASID to be programmed in the device PASID table
  547. * @domain: User stage-1 domain nested on a stage-2 domain
  548. *
  549. * This is used for nested translation. The input domain should be
  550. * nested type and nested on a parent with 'is_nested_parent' flag
  551. * set.
  552. */
  553. int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
  554. u32 pasid, struct dmar_domain *domain)
  555. {
  556. struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg;
  557. struct dmar_domain *s2_domain = domain->s2_domain;
  558. u16 did = domain_id_iommu(domain, iommu);
  559. struct pasid_entry *pte;
  560. /* Address width should match the address width supported by hardware */
  561. switch (s1_cfg->addr_width) {
  562. case ADDR_WIDTH_4LEVEL:
  563. break;
  564. case ADDR_WIDTH_5LEVEL:
  565. if (!cap_fl5lp_support(iommu->cap)) {
  566. dev_err_ratelimited(dev,
  567. "5-level paging not supported\n");
  568. return -EINVAL;
  569. }
  570. break;
  571. default:
  572. dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n",
  573. s1_cfg->addr_width);
  574. return -EINVAL;
  575. }
  576. if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) {
  577. pr_err_ratelimited("No supervisor request support on %s\n",
  578. iommu->name);
  579. return -EINVAL;
  580. }
  581. if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) {
  582. pr_err_ratelimited("No extended access flag support on %s\n",
  583. iommu->name);
  584. return -EINVAL;
  585. }
  586. spin_lock(&iommu->lock);
  587. pte = intel_pasid_get_entry(dev, pasid);
  588. if (!pte) {
  589. spin_unlock(&iommu->lock);
  590. return -ENODEV;
  591. }
  592. if (pasid_pte_is_present(pte)) {
  593. spin_unlock(&iommu->lock);
  594. return -EBUSY;
  595. }
  596. pasid_pte_config_nestd(iommu, pte, s1_cfg, s2_domain, did);
  597. spin_unlock(&iommu->lock);
  598. pasid_flush_caches(iommu, pte, pasid, did);
  599. return 0;
  600. }
  601. /*
  602. * Interfaces to setup or teardown a pasid table to the scalable-mode
  603. * context table entry:
  604. */
  605. static void device_pasid_table_teardown(struct device *dev, u8 bus, u8 devfn)
  606. {
  607. struct device_domain_info *info = dev_iommu_priv_get(dev);
  608. struct intel_iommu *iommu = info->iommu;
  609. struct context_entry *context;
  610. u16 did;
  611. spin_lock(&iommu->lock);
  612. context = iommu_context_addr(iommu, bus, devfn, false);
  613. if (!context) {
  614. spin_unlock(&iommu->lock);
  615. return;
  616. }
  617. did = context_domain_id(context);
  618. context_clear_entry(context);
  619. __iommu_flush_cache(iommu, context, sizeof(*context));
  620. spin_unlock(&iommu->lock);
  621. intel_context_flush_no_pasid(info, context, did);
  622. }
  623. static int pci_pasid_table_teardown(struct pci_dev *pdev, u16 alias, void *data)
  624. {
  625. struct device *dev = data;
  626. if (dev == &pdev->dev)
  627. device_pasid_table_teardown(dev, PCI_BUS_NUM(alias), alias & 0xff);
  628. return 0;
  629. }
  630. void intel_pasid_teardown_sm_context(struct device *dev)
  631. {
  632. struct device_domain_info *info = dev_iommu_priv_get(dev);
  633. if (!dev_is_pci(dev)) {
  634. device_pasid_table_teardown(dev, info->bus, info->devfn);
  635. return;
  636. }
  637. pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_teardown, dev);
  638. }
  639. /*
  640. * Get the PASID directory size for scalable mode context entry.
  641. * Value of X in the PDTS field of a scalable mode context entry
  642. * indicates PASID directory with 2^(X + 7) entries.
  643. */
  644. static unsigned long context_get_sm_pds(struct pasid_table *table)
  645. {
  646. unsigned long pds, max_pde;
  647. max_pde = table->max_pasid >> PASID_PDE_SHIFT;
  648. pds = find_first_bit(&max_pde, MAX_NR_PASID_BITS);
  649. if (pds < 7)
  650. return 0;
  651. return pds - 7;
  652. }
  653. static int context_entry_set_pasid_table(struct context_entry *context,
  654. struct device *dev)
  655. {
  656. struct device_domain_info *info = dev_iommu_priv_get(dev);
  657. struct pasid_table *table = info->pasid_table;
  658. struct intel_iommu *iommu = info->iommu;
  659. unsigned long pds;
  660. context_clear_entry(context);
  661. pds = context_get_sm_pds(table);
  662. context->lo = (u64)virt_to_phys(table->table) | context_pdts(pds);
  663. context_set_sm_rid2pasid(context, IOMMU_NO_PASID);
  664. if (info->ats_supported)
  665. context_set_sm_dte(context);
  666. if (info->pasid_supported)
  667. context_set_pasid(context);
  668. if (info->pri_supported)
  669. context_set_sm_pre(context);
  670. context_set_fault_enable(context);
  671. context_set_present(context);
  672. __iommu_flush_cache(iommu, context, sizeof(*context));
  673. return 0;
  674. }
  675. static int device_pasid_table_setup(struct device *dev, u8 bus, u8 devfn)
  676. {
  677. struct device_domain_info *info = dev_iommu_priv_get(dev);
  678. struct intel_iommu *iommu = info->iommu;
  679. struct context_entry *context;
  680. spin_lock(&iommu->lock);
  681. context = iommu_context_addr(iommu, bus, devfn, true);
  682. if (!context) {
  683. spin_unlock(&iommu->lock);
  684. return -ENOMEM;
  685. }
  686. if (context_present(context) && !context_copied(iommu, bus, devfn)) {
  687. spin_unlock(&iommu->lock);
  688. return 0;
  689. }
  690. if (context_copied(iommu, bus, devfn)) {
  691. context_clear_present(context);
  692. __iommu_flush_cache(iommu, context, sizeof(*context));
  693. /*
  694. * For kdump cases, old valid entries may be cached due to
  695. * the in-flight DMA and copied pgtable, but there is no
  696. * unmapping behaviour for them, thus we need explicit cache
  697. * flushes for all affected domain IDs and PASIDs used in
  698. * the copied PASID table. Given that we have no idea about
  699. * which domain IDs and PASIDs were used in the copied tables,
  700. * upgrade them to global PASID and IOTLB cache invalidation.
  701. */
  702. iommu->flush.flush_context(iommu, 0,
  703. PCI_DEVID(bus, devfn),
  704. DMA_CCMD_MASK_NOBIT,
  705. DMA_CCMD_DEVICE_INVL);
  706. qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
  707. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
  708. devtlb_invalidation_with_pasid(iommu, dev, IOMMU_NO_PASID);
  709. context_clear_entry(context);
  710. __iommu_flush_cache(iommu, context, sizeof(*context));
  711. /*
  712. * At this point, the device is supposed to finish reset at
  713. * its driver probe stage, so no in-flight DMA will exist,
  714. * and we don't need to worry anymore hereafter.
  715. */
  716. clear_context_copied(iommu, bus, devfn);
  717. }
  718. context_entry_set_pasid_table(context, dev);
  719. spin_unlock(&iommu->lock);
  720. /*
  721. * It's a non-present to present mapping. If hardware doesn't cache
  722. * non-present entry we don't need to flush the caches. If it does
  723. * cache non-present entries, then it does so in the special
  724. * domain #0, which we have to flush:
  725. */
  726. if (cap_caching_mode(iommu->cap)) {
  727. iommu->flush.flush_context(iommu, 0,
  728. PCI_DEVID(bus, devfn),
  729. DMA_CCMD_MASK_NOBIT,
  730. DMA_CCMD_DEVICE_INVL);
  731. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
  732. }
  733. return 0;
  734. }
  735. static int pci_pasid_table_setup(struct pci_dev *pdev, u16 alias, void *data)
  736. {
  737. struct device *dev = data;
  738. if (dev != &pdev->dev)
  739. return 0;
  740. return device_pasid_table_setup(dev, PCI_BUS_NUM(alias), alias & 0xff);
  741. }
  742. /*
  743. * Set the device's PASID table to its context table entry.
  744. *
  745. * The PASID table is set to the context entries of both device itself
  746. * and its alias requester ID for DMA.
  747. */
  748. int intel_pasid_setup_sm_context(struct device *dev)
  749. {
  750. struct device_domain_info *info = dev_iommu_priv_get(dev);
  751. if (!dev_is_pci(dev))
  752. return device_pasid_table_setup(dev, info->bus, info->devfn);
  753. return pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_setup, dev);
  754. }
  755. /*
  756. * Global Device-TLB invalidation following changes in a context entry which
  757. * was present.
  758. */
  759. static void __context_flush_dev_iotlb(struct device_domain_info *info)
  760. {
  761. if (!info->ats_enabled)
  762. return;
  763. /*
  764. * Skip dev-IOTLB flush for inaccessible PCIe devices to prevent the
  765. * Intel IOMMU from waiting indefinitely for an ATS invalidation that
  766. * cannot complete.
  767. */
  768. if (!pci_device_is_present(to_pci_dev(info->dev)))
  769. return;
  770. qi_flush_dev_iotlb(info->iommu, PCI_DEVID(info->bus, info->devfn),
  771. info->pfsid, info->ats_qdep, 0, MAX_AGAW_PFN_WIDTH);
  772. /*
  773. * There is no guarantee that the device DMA is stopped when it reaches
  774. * here. Therefore, always attempt the extra device TLB invalidation
  775. * quirk. The impact on performance is acceptable since this is not a
  776. * performance-critical path.
  777. */
  778. quirk_extra_dev_tlb_flush(info, 0, MAX_AGAW_PFN_WIDTH, IOMMU_NO_PASID,
  779. info->ats_qdep);
  780. }
  781. /*
  782. * Cache invalidations after change in a context table entry that was present
  783. * according to the Spec 6.5.3.3 (Guidance to Software for Invalidations).
  784. * This helper can only be used when IOMMU is working in the legacy mode or
  785. * IOMMU is in scalable mode but all PASID table entries of the device are
  786. * non-present.
  787. */
  788. void intel_context_flush_no_pasid(struct device_domain_info *info,
  789. struct context_entry *context, u16 did)
  790. {
  791. struct intel_iommu *iommu = info->iommu;
  792. /*
  793. * Device-selective context-cache invalidation. The Domain-ID field
  794. * of the Context-cache Invalidate Descriptor is ignored by hardware
  795. * when operating in scalable mode. Therefore the @did value doesn't
  796. * matter in scalable mode.
  797. */
  798. iommu->flush.flush_context(iommu, did, PCI_DEVID(info->bus, info->devfn),
  799. DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL);
  800. /*
  801. * For legacy mode:
  802. * - Domain-selective IOTLB invalidation
  803. * - Global Device-TLB invalidation to all affected functions
  804. */
  805. if (!sm_supported(iommu)) {
  806. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  807. __context_flush_dev_iotlb(info);
  808. return;
  809. }
  810. __context_flush_dev_iotlb(info);
  811. }