pasid.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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(sizeof(*pasid_table), GFP_KERNEL);
  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(info->iommu->node, GFP_KERNEL, order);
  54. if (!dir) {
  55. kfree(pasid_table);
  56. return -ENOMEM;
  57. }
  58. pasid_table->table = dir;
  59. pasid_table->order = order;
  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_page(table);
  84. }
  85. iommu_free_pages(pasid_table->table, pasid_table->order);
  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_page_node(info->iommu->node, GFP_ATOMIC);
  123. if (!entries)
  124. return NULL;
  125. /*
  126. * The pasid directory table entry won't be freed after
  127. * allocation. No worry about the race with free and
  128. * clear. However, this entry might be populated by others
  129. * while we are preparing it. Use theirs with a retry.
  130. */
  131. tmp = 0ULL;
  132. if (!try_cmpxchg64(&dir[dir_index].val, &tmp,
  133. (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {
  134. iommu_free_page(entries);
  135. goto retry;
  136. }
  137. if (!ecap_coherent(info->iommu->ecap)) {
  138. clflush_cache_range(entries, VTD_PAGE_SIZE);
  139. clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
  140. }
  141. }
  142. return &entries[index];
  143. }
  144. /*
  145. * Interfaces for PASID table entry manipulation:
  146. */
  147. static void
  148. intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore)
  149. {
  150. struct pasid_entry *pe;
  151. pe = intel_pasid_get_entry(dev, pasid);
  152. if (WARN_ON(!pe))
  153. return;
  154. if (fault_ignore && pasid_pte_is_present(pe))
  155. pasid_clear_entry_with_fpd(pe);
  156. else
  157. pasid_clear_entry(pe);
  158. }
  159. static void
  160. pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
  161. u16 did, u32 pasid)
  162. {
  163. struct qi_desc desc;
  164. desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) |
  165. QI_PC_PASID(pasid) | QI_PC_TYPE;
  166. desc.qw1 = 0;
  167. desc.qw2 = 0;
  168. desc.qw3 = 0;
  169. qi_submit_sync(iommu, &desc, 1, 0);
  170. }
  171. static void
  172. devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
  173. struct device *dev, u32 pasid)
  174. {
  175. struct device_domain_info *info;
  176. u16 sid, qdep, pfsid;
  177. info = dev_iommu_priv_get(dev);
  178. if (!info || !info->ats_enabled)
  179. return;
  180. if (pci_dev_is_disconnected(to_pci_dev(dev)))
  181. return;
  182. sid = info->bus << 8 | info->devfn;
  183. qdep = info->ats_qdep;
  184. pfsid = info->pfsid;
  185. /*
  186. * When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID),
  187. * devTLB flush w/o PASID should be used. For non-zero PASID under
  188. * SVA usage, device could do DMA with multiple PASIDs. It is more
  189. * efficient to flush devTLB specific to the PASID.
  190. */
  191. if (pasid == IOMMU_NO_PASID)
  192. qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
  193. else
  194. qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
  195. }
  196. void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
  197. u32 pasid, bool fault_ignore)
  198. {
  199. struct pasid_entry *pte;
  200. u16 did, pgtt;
  201. spin_lock(&iommu->lock);
  202. pte = intel_pasid_get_entry(dev, pasid);
  203. if (WARN_ON(!pte) || !pasid_pte_is_present(pte)) {
  204. spin_unlock(&iommu->lock);
  205. return;
  206. }
  207. did = pasid_get_domain_id(pte);
  208. pgtt = pasid_pte_get_pgtt(pte);
  209. intel_pasid_clear_entry(dev, pasid, fault_ignore);
  210. spin_unlock(&iommu->lock);
  211. if (!ecap_coherent(iommu->ecap))
  212. clflush_cache_range(pte, sizeof(*pte));
  213. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  214. if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY)
  215. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  216. else
  217. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  218. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  219. }
  220. /*
  221. * This function flushes cache for a newly setup pasid table entry.
  222. * Caller of it should not modify the in-use pasid table entries.
  223. */
  224. static void pasid_flush_caches(struct intel_iommu *iommu,
  225. struct pasid_entry *pte,
  226. u32 pasid, u16 did)
  227. {
  228. if (!ecap_coherent(iommu->ecap))
  229. clflush_cache_range(pte, sizeof(*pte));
  230. if (cap_caching_mode(iommu->cap)) {
  231. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  232. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  233. } else {
  234. iommu_flush_write_buffer(iommu);
  235. }
  236. }
  237. /*
  238. * Set up the scalable mode pasid table entry for first only
  239. * translation type.
  240. */
  241. int intel_pasid_setup_first_level(struct intel_iommu *iommu,
  242. struct device *dev, pgd_t *pgd,
  243. u32 pasid, u16 did, int flags)
  244. {
  245. struct pasid_entry *pte;
  246. if (!ecap_flts(iommu->ecap)) {
  247. pr_err("No first level translation support on %s\n",
  248. iommu->name);
  249. return -EINVAL;
  250. }
  251. if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
  252. pr_err("No 5-level paging support for first-level on %s\n",
  253. iommu->name);
  254. return -EINVAL;
  255. }
  256. spin_lock(&iommu->lock);
  257. pte = intel_pasid_get_entry(dev, pasid);
  258. if (!pte) {
  259. spin_unlock(&iommu->lock);
  260. return -ENODEV;
  261. }
  262. if (pasid_pte_is_present(pte)) {
  263. spin_unlock(&iommu->lock);
  264. return -EBUSY;
  265. }
  266. pasid_clear_entry(pte);
  267. /* Setup the first level page table pointer: */
  268. pasid_set_flptr(pte, (u64)__pa(pgd));
  269. if (flags & PASID_FLAG_FL5LP)
  270. pasid_set_flpm(pte, 1);
  271. if (flags & PASID_FLAG_PAGE_SNOOP)
  272. pasid_set_pgsnp(pte);
  273. pasid_set_domain_id(pte, did);
  274. pasid_set_address_width(pte, iommu->agaw);
  275. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  276. /* Setup Present and PASID Granular Transfer Type: */
  277. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
  278. pasid_set_present(pte);
  279. spin_unlock(&iommu->lock);
  280. pasid_flush_caches(iommu, pte, pasid, did);
  281. return 0;
  282. }
  283. /*
  284. * Skip top levels of page tables for iommu which has less agaw
  285. * than default. Unnecessary for PT mode.
  286. */
  287. static int iommu_skip_agaw(struct dmar_domain *domain,
  288. struct intel_iommu *iommu,
  289. struct dma_pte **pgd)
  290. {
  291. int agaw;
  292. for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
  293. *pgd = phys_to_virt(dma_pte_addr(*pgd));
  294. if (!dma_pte_present(*pgd))
  295. return -EINVAL;
  296. }
  297. return agaw;
  298. }
  299. /*
  300. * Set up the scalable mode pasid entry for second only translation type.
  301. */
  302. int intel_pasid_setup_second_level(struct intel_iommu *iommu,
  303. struct dmar_domain *domain,
  304. struct device *dev, u32 pasid)
  305. {
  306. struct pasid_entry *pte;
  307. struct dma_pte *pgd;
  308. u64 pgd_val;
  309. int agaw;
  310. u16 did;
  311. /*
  312. * If hardware advertises no support for second level
  313. * translation, return directly.
  314. */
  315. if (!ecap_slts(iommu->ecap)) {
  316. pr_err("No second level translation support on %s\n",
  317. iommu->name);
  318. return -EINVAL;
  319. }
  320. pgd = domain->pgd;
  321. agaw = iommu_skip_agaw(domain, iommu, &pgd);
  322. if (agaw < 0) {
  323. dev_err(dev, "Invalid domain page table\n");
  324. return -EINVAL;
  325. }
  326. pgd_val = virt_to_phys(pgd);
  327. did = domain_id_iommu(domain, iommu);
  328. spin_lock(&iommu->lock);
  329. pte = intel_pasid_get_entry(dev, pasid);
  330. if (!pte) {
  331. spin_unlock(&iommu->lock);
  332. return -ENODEV;
  333. }
  334. if (pasid_pte_is_present(pte)) {
  335. spin_unlock(&iommu->lock);
  336. return -EBUSY;
  337. }
  338. pasid_clear_entry(pte);
  339. pasid_set_domain_id(pte, did);
  340. pasid_set_slptr(pte, pgd_val);
  341. pasid_set_address_width(pte, agaw);
  342. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
  343. pasid_set_fault_enable(pte);
  344. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  345. if (domain->dirty_tracking)
  346. pasid_set_ssade(pte);
  347. pasid_set_present(pte);
  348. spin_unlock(&iommu->lock);
  349. pasid_flush_caches(iommu, pte, pasid, did);
  350. return 0;
  351. }
  352. /*
  353. * Set up dirty tracking on a second only or nested translation type.
  354. */
  355. int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu,
  356. struct device *dev, u32 pasid,
  357. bool enabled)
  358. {
  359. struct pasid_entry *pte;
  360. u16 did, pgtt;
  361. spin_lock(&iommu->lock);
  362. pte = intel_pasid_get_entry(dev, pasid);
  363. if (!pte) {
  364. spin_unlock(&iommu->lock);
  365. dev_err_ratelimited(
  366. dev, "Failed to get pasid entry of PASID %d\n", pasid);
  367. return -ENODEV;
  368. }
  369. did = pasid_get_domain_id(pte);
  370. pgtt = pasid_pte_get_pgtt(pte);
  371. if (pgtt != PASID_ENTRY_PGTT_SL_ONLY &&
  372. pgtt != PASID_ENTRY_PGTT_NESTED) {
  373. spin_unlock(&iommu->lock);
  374. dev_err_ratelimited(
  375. dev,
  376. "Dirty tracking not supported on translation type %d\n",
  377. pgtt);
  378. return -EOPNOTSUPP;
  379. }
  380. if (pasid_get_ssade(pte) == enabled) {
  381. spin_unlock(&iommu->lock);
  382. return 0;
  383. }
  384. if (enabled)
  385. pasid_set_ssade(pte);
  386. else
  387. pasid_clear_ssade(pte);
  388. spin_unlock(&iommu->lock);
  389. if (!ecap_coherent(iommu->ecap))
  390. clflush_cache_range(pte, sizeof(*pte));
  391. /*
  392. * From VT-d spec table 25 "Guidance to Software for Invalidations":
  393. *
  394. * - PASID-selective-within-Domain PASID-cache invalidation
  395. * If (PGTT=SS or Nested)
  396. * - Domain-selective IOTLB invalidation
  397. * Else
  398. * - PASID-selective PASID-based IOTLB invalidation
  399. * - If (pasid is RID_PASID)
  400. * - Global Device-TLB invalidation to affected functions
  401. * Else
  402. * - PASID-based Device-TLB invalidation (with S=1 and
  403. * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
  404. */
  405. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  406. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  407. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  408. return 0;
  409. }
  410. /*
  411. * Set up the scalable mode pasid entry for passthrough translation type.
  412. */
  413. int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
  414. struct device *dev, u32 pasid)
  415. {
  416. u16 did = FLPT_DEFAULT_DID;
  417. struct pasid_entry *pte;
  418. spin_lock(&iommu->lock);
  419. pte = intel_pasid_get_entry(dev, pasid);
  420. if (!pte) {
  421. spin_unlock(&iommu->lock);
  422. return -ENODEV;
  423. }
  424. if (pasid_pte_is_present(pte)) {
  425. spin_unlock(&iommu->lock);
  426. return -EBUSY;
  427. }
  428. pasid_clear_entry(pte);
  429. pasid_set_domain_id(pte, did);
  430. pasid_set_address_width(pte, iommu->agaw);
  431. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
  432. pasid_set_fault_enable(pte);
  433. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  434. pasid_set_present(pte);
  435. spin_unlock(&iommu->lock);
  436. pasid_flush_caches(iommu, pte, pasid, did);
  437. return 0;
  438. }
  439. /*
  440. * Set the page snoop control for a pasid entry which has been set up.
  441. */
  442. void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
  443. struct device *dev, u32 pasid)
  444. {
  445. struct pasid_entry *pte;
  446. u16 did;
  447. spin_lock(&iommu->lock);
  448. pte = intel_pasid_get_entry(dev, pasid);
  449. if (WARN_ON(!pte || !pasid_pte_is_present(pte))) {
  450. spin_unlock(&iommu->lock);
  451. return;
  452. }
  453. pasid_set_pgsnp(pte);
  454. did = pasid_get_domain_id(pte);
  455. spin_unlock(&iommu->lock);
  456. if (!ecap_coherent(iommu->ecap))
  457. clflush_cache_range(pte, sizeof(*pte));
  458. /*
  459. * VT-d spec 3.4 table23 states guides for cache invalidation:
  460. *
  461. * - PASID-selective-within-Domain PASID-cache invalidation
  462. * - PASID-selective PASID-based IOTLB invalidation
  463. * - If (pasid is RID_PASID)
  464. * - Global Device-TLB invalidation to affected functions
  465. * Else
  466. * - PASID-based Device-TLB invalidation (with S=1 and
  467. * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
  468. */
  469. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  470. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  471. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  472. }
  473. /**
  474. * intel_pasid_setup_nested() - Set up PASID entry for nested translation.
  475. * @iommu: IOMMU which the device belong to
  476. * @dev: Device to be set up for translation
  477. * @pasid: PASID to be programmed in the device PASID table
  478. * @domain: User stage-1 domain nested on a stage-2 domain
  479. *
  480. * This is used for nested translation. The input domain should be
  481. * nested type and nested on a parent with 'is_nested_parent' flag
  482. * set.
  483. */
  484. int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
  485. u32 pasid, struct dmar_domain *domain)
  486. {
  487. struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg;
  488. pgd_t *s1_gpgd = (pgd_t *)(uintptr_t)domain->s1_pgtbl;
  489. struct dmar_domain *s2_domain = domain->s2_domain;
  490. u16 did = domain_id_iommu(domain, iommu);
  491. struct dma_pte *pgd = s2_domain->pgd;
  492. struct pasid_entry *pte;
  493. /* Address width should match the address width supported by hardware */
  494. switch (s1_cfg->addr_width) {
  495. case ADDR_WIDTH_4LEVEL:
  496. break;
  497. case ADDR_WIDTH_5LEVEL:
  498. if (!cap_fl5lp_support(iommu->cap)) {
  499. dev_err_ratelimited(dev,
  500. "5-level paging not supported\n");
  501. return -EINVAL;
  502. }
  503. break;
  504. default:
  505. dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n",
  506. s1_cfg->addr_width);
  507. return -EINVAL;
  508. }
  509. if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) {
  510. pr_err_ratelimited("No supervisor request support on %s\n",
  511. iommu->name);
  512. return -EINVAL;
  513. }
  514. if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) {
  515. pr_err_ratelimited("No extended access flag support on %s\n",
  516. iommu->name);
  517. return -EINVAL;
  518. }
  519. spin_lock(&iommu->lock);
  520. pte = intel_pasid_get_entry(dev, pasid);
  521. if (!pte) {
  522. spin_unlock(&iommu->lock);
  523. return -ENODEV;
  524. }
  525. if (pasid_pte_is_present(pte)) {
  526. spin_unlock(&iommu->lock);
  527. return -EBUSY;
  528. }
  529. pasid_clear_entry(pte);
  530. if (s1_cfg->addr_width == ADDR_WIDTH_5LEVEL)
  531. pasid_set_flpm(pte, 1);
  532. pasid_set_flptr(pte, (uintptr_t)s1_gpgd);
  533. if (s1_cfg->flags & IOMMU_VTD_S1_SRE) {
  534. pasid_set_sre(pte);
  535. if (s1_cfg->flags & IOMMU_VTD_S1_WPE)
  536. pasid_set_wpe(pte);
  537. }
  538. if (s1_cfg->flags & IOMMU_VTD_S1_EAFE)
  539. pasid_set_eafe(pte);
  540. if (s2_domain->force_snooping)
  541. pasid_set_pgsnp(pte);
  542. pasid_set_slptr(pte, virt_to_phys(pgd));
  543. pasid_set_fault_enable(pte);
  544. pasid_set_domain_id(pte, did);
  545. pasid_set_address_width(pte, s2_domain->agaw);
  546. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  547. if (s2_domain->dirty_tracking)
  548. pasid_set_ssade(pte);
  549. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED);
  550. pasid_set_present(pte);
  551. spin_unlock(&iommu->lock);
  552. pasid_flush_caches(iommu, pte, pasid, did);
  553. return 0;
  554. }
  555. /*
  556. * Interfaces to setup or teardown a pasid table to the scalable-mode
  557. * context table entry:
  558. */
  559. static void device_pasid_table_teardown(struct device *dev, u8 bus, u8 devfn)
  560. {
  561. struct device_domain_info *info = dev_iommu_priv_get(dev);
  562. struct intel_iommu *iommu = info->iommu;
  563. struct context_entry *context;
  564. u16 did;
  565. spin_lock(&iommu->lock);
  566. context = iommu_context_addr(iommu, bus, devfn, false);
  567. if (!context) {
  568. spin_unlock(&iommu->lock);
  569. return;
  570. }
  571. did = context_domain_id(context);
  572. context_clear_entry(context);
  573. __iommu_flush_cache(iommu, context, sizeof(*context));
  574. spin_unlock(&iommu->lock);
  575. intel_context_flush_present(info, context, did, false);
  576. }
  577. static int pci_pasid_table_teardown(struct pci_dev *pdev, u16 alias, void *data)
  578. {
  579. struct device *dev = data;
  580. if (dev == &pdev->dev)
  581. device_pasid_table_teardown(dev, PCI_BUS_NUM(alias), alias & 0xff);
  582. return 0;
  583. }
  584. void intel_pasid_teardown_sm_context(struct device *dev)
  585. {
  586. struct device_domain_info *info = dev_iommu_priv_get(dev);
  587. if (!dev_is_pci(dev)) {
  588. device_pasid_table_teardown(dev, info->bus, info->devfn);
  589. return;
  590. }
  591. pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_teardown, dev);
  592. }
  593. /*
  594. * Get the PASID directory size for scalable mode context entry.
  595. * Value of X in the PDTS field of a scalable mode context entry
  596. * indicates PASID directory with 2^(X + 7) entries.
  597. */
  598. static unsigned long context_get_sm_pds(struct pasid_table *table)
  599. {
  600. unsigned long pds, max_pde;
  601. max_pde = table->max_pasid >> PASID_PDE_SHIFT;
  602. pds = find_first_bit(&max_pde, MAX_NR_PASID_BITS);
  603. if (pds < 7)
  604. return 0;
  605. return pds - 7;
  606. }
  607. static int context_entry_set_pasid_table(struct context_entry *context,
  608. struct device *dev)
  609. {
  610. struct device_domain_info *info = dev_iommu_priv_get(dev);
  611. struct pasid_table *table = info->pasid_table;
  612. struct intel_iommu *iommu = info->iommu;
  613. unsigned long pds;
  614. context_clear_entry(context);
  615. pds = context_get_sm_pds(table);
  616. context->lo = (u64)virt_to_phys(table->table) | context_pdts(pds);
  617. context_set_sm_rid2pasid(context, IOMMU_NO_PASID);
  618. if (info->ats_supported)
  619. context_set_sm_dte(context);
  620. if (info->pasid_supported)
  621. context_set_pasid(context);
  622. context_set_fault_enable(context);
  623. context_set_present(context);
  624. __iommu_flush_cache(iommu, context, sizeof(*context));
  625. return 0;
  626. }
  627. static int device_pasid_table_setup(struct device *dev, u8 bus, u8 devfn)
  628. {
  629. struct device_domain_info *info = dev_iommu_priv_get(dev);
  630. struct intel_iommu *iommu = info->iommu;
  631. struct context_entry *context;
  632. spin_lock(&iommu->lock);
  633. context = iommu_context_addr(iommu, bus, devfn, true);
  634. if (!context) {
  635. spin_unlock(&iommu->lock);
  636. return -ENOMEM;
  637. }
  638. if (context_present(context) && !context_copied(iommu, bus, devfn)) {
  639. spin_unlock(&iommu->lock);
  640. return 0;
  641. }
  642. if (context_copied(iommu, bus, devfn)) {
  643. context_clear_entry(context);
  644. __iommu_flush_cache(iommu, context, sizeof(*context));
  645. /*
  646. * For kdump cases, old valid entries may be cached due to
  647. * the in-flight DMA and copied pgtable, but there is no
  648. * unmapping behaviour for them, thus we need explicit cache
  649. * flushes for all affected domain IDs and PASIDs used in
  650. * the copied PASID table. Given that we have no idea about
  651. * which domain IDs and PASIDs were used in the copied tables,
  652. * upgrade them to global PASID and IOTLB cache invalidation.
  653. */
  654. iommu->flush.flush_context(iommu, 0,
  655. PCI_DEVID(bus, devfn),
  656. DMA_CCMD_MASK_NOBIT,
  657. DMA_CCMD_DEVICE_INVL);
  658. qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
  659. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
  660. devtlb_invalidation_with_pasid(iommu, dev, IOMMU_NO_PASID);
  661. /*
  662. * At this point, the device is supposed to finish reset at
  663. * its driver probe stage, so no in-flight DMA will exist,
  664. * and we don't need to worry anymore hereafter.
  665. */
  666. clear_context_copied(iommu, bus, devfn);
  667. }
  668. context_entry_set_pasid_table(context, dev);
  669. spin_unlock(&iommu->lock);
  670. /*
  671. * It's a non-present to present mapping. If hardware doesn't cache
  672. * non-present entry we don't need to flush the caches. If it does
  673. * cache non-present entries, then it does so in the special
  674. * domain #0, which we have to flush:
  675. */
  676. if (cap_caching_mode(iommu->cap)) {
  677. iommu->flush.flush_context(iommu, 0,
  678. PCI_DEVID(bus, devfn),
  679. DMA_CCMD_MASK_NOBIT,
  680. DMA_CCMD_DEVICE_INVL);
  681. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
  682. }
  683. return 0;
  684. }
  685. static int pci_pasid_table_setup(struct pci_dev *pdev, u16 alias, void *data)
  686. {
  687. struct device *dev = data;
  688. if (dev != &pdev->dev)
  689. return 0;
  690. return device_pasid_table_setup(dev, PCI_BUS_NUM(alias), alias & 0xff);
  691. }
  692. /*
  693. * Set the device's PASID table to its context table entry.
  694. *
  695. * The PASID table is set to the context entries of both device itself
  696. * and its alias requester ID for DMA.
  697. */
  698. int intel_pasid_setup_sm_context(struct device *dev)
  699. {
  700. struct device_domain_info *info = dev_iommu_priv_get(dev);
  701. if (!dev_is_pci(dev))
  702. return device_pasid_table_setup(dev, info->bus, info->devfn);
  703. return pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_setup, dev);
  704. }
  705. /*
  706. * Global Device-TLB invalidation following changes in a context entry which
  707. * was present.
  708. */
  709. static void __context_flush_dev_iotlb(struct device_domain_info *info)
  710. {
  711. if (!info->ats_enabled)
  712. return;
  713. qi_flush_dev_iotlb(info->iommu, PCI_DEVID(info->bus, info->devfn),
  714. info->pfsid, info->ats_qdep, 0, MAX_AGAW_PFN_WIDTH);
  715. /*
  716. * There is no guarantee that the device DMA is stopped when it reaches
  717. * here. Therefore, always attempt the extra device TLB invalidation
  718. * quirk. The impact on performance is acceptable since this is not a
  719. * performance-critical path.
  720. */
  721. quirk_extra_dev_tlb_flush(info, 0, MAX_AGAW_PFN_WIDTH, IOMMU_NO_PASID,
  722. info->ats_qdep);
  723. }
  724. /*
  725. * Cache invalidations after change in a context table entry that was present
  726. * according to the Spec 6.5.3.3 (Guidance to Software for Invalidations). If
  727. * IOMMU is in scalable mode and all PASID table entries of the device were
  728. * non-present, set flush_domains to false. Otherwise, true.
  729. */
  730. void intel_context_flush_present(struct device_domain_info *info,
  731. struct context_entry *context,
  732. u16 did, bool flush_domains)
  733. {
  734. struct intel_iommu *iommu = info->iommu;
  735. struct pasid_entry *pte;
  736. int i;
  737. /*
  738. * Device-selective context-cache invalidation. The Domain-ID field
  739. * of the Context-cache Invalidate Descriptor is ignored by hardware
  740. * when operating in scalable mode. Therefore the @did value doesn't
  741. * matter in scalable mode.
  742. */
  743. iommu->flush.flush_context(iommu, did, PCI_DEVID(info->bus, info->devfn),
  744. DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL);
  745. /*
  746. * For legacy mode:
  747. * - Domain-selective IOTLB invalidation
  748. * - Global Device-TLB invalidation to all affected functions
  749. */
  750. if (!sm_supported(iommu)) {
  751. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  752. __context_flush_dev_iotlb(info);
  753. return;
  754. }
  755. /*
  756. * For scalable mode:
  757. * - Domain-selective PASID-cache invalidation to affected domains
  758. * - Domain-selective IOTLB invalidation to affected domains
  759. * - Global Device-TLB invalidation to affected functions
  760. */
  761. if (flush_domains) {
  762. /*
  763. * If the IOMMU is running in scalable mode and there might
  764. * be potential PASID translations, the caller should hold
  765. * the lock to ensure that context changes and cache flushes
  766. * are atomic.
  767. */
  768. assert_spin_locked(&iommu->lock);
  769. for (i = 0; i < info->pasid_table->max_pasid; i++) {
  770. pte = intel_pasid_get_entry(info->dev, i);
  771. if (!pte || !pasid_pte_is_present(pte))
  772. continue;
  773. did = pasid_get_domain_id(pte);
  774. qi_flush_pasid_cache(iommu, did, QI_PC_ALL_PASIDS, 0);
  775. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  776. }
  777. }
  778. __context_flush_dev_iotlb(info);
  779. }