1
0

cache.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cache.c - Intel VT-d cache invalidation
  4. *
  5. * Copyright (C) 2024 Intel Corporation
  6. *
  7. * Author: Lu Baolu <baolu.lu@linux.intel.com>
  8. */
  9. #define pr_fmt(fmt) "DMAR: " fmt
  10. #include <linux/dmar.h>
  11. #include <linux/iommu.h>
  12. #include <linux/memory.h>
  13. #include <linux/pci.h>
  14. #include <linux/spinlock.h>
  15. #include "iommu.h"
  16. #include "pasid.h"
  17. #include "trace.h"
  18. /* Check if an existing cache tag can be reused for a new association. */
  19. static bool cache_tage_match(struct cache_tag *tag, u16 domain_id,
  20. struct intel_iommu *iommu, struct device *dev,
  21. ioasid_t pasid, enum cache_tag_type type)
  22. {
  23. if (tag->type != type)
  24. return false;
  25. if (tag->domain_id != domain_id || tag->pasid != pasid)
  26. return false;
  27. if (type == CACHE_TAG_IOTLB || type == CACHE_TAG_NESTING_IOTLB)
  28. return tag->iommu == iommu;
  29. if (type == CACHE_TAG_DEVTLB || type == CACHE_TAG_NESTING_DEVTLB)
  30. return tag->dev == dev;
  31. return false;
  32. }
  33. /* Assign a cache tag with specified type to domain. */
  34. int cache_tag_assign(struct dmar_domain *domain, u16 did, struct device *dev,
  35. ioasid_t pasid, enum cache_tag_type type)
  36. {
  37. struct device_domain_info *info = dev_iommu_priv_get(dev);
  38. struct intel_iommu *iommu = info->iommu;
  39. struct cache_tag *tag, *temp;
  40. struct list_head *prev;
  41. unsigned long flags;
  42. tag = kzalloc_obj(*tag);
  43. if (!tag)
  44. return -ENOMEM;
  45. tag->type = type;
  46. tag->iommu = iommu;
  47. tag->domain_id = did;
  48. tag->pasid = pasid;
  49. tag->users = 1;
  50. if (type == CACHE_TAG_DEVTLB || type == CACHE_TAG_NESTING_DEVTLB)
  51. tag->dev = dev;
  52. else
  53. tag->dev = iommu->iommu.dev;
  54. spin_lock_irqsave(&domain->cache_lock, flags);
  55. prev = &domain->cache_tags;
  56. list_for_each_entry(temp, &domain->cache_tags, node) {
  57. if (cache_tage_match(temp, did, iommu, dev, pasid, type)) {
  58. temp->users++;
  59. spin_unlock_irqrestore(&domain->cache_lock, flags);
  60. kfree(tag);
  61. trace_cache_tag_assign(temp);
  62. return 0;
  63. }
  64. if (temp->iommu == iommu)
  65. prev = &temp->node;
  66. }
  67. /*
  68. * Link cache tags of same iommu unit together, so corresponding
  69. * flush ops can be batched for iommu unit.
  70. */
  71. list_add(&tag->node, prev);
  72. spin_unlock_irqrestore(&domain->cache_lock, flags);
  73. trace_cache_tag_assign(tag);
  74. return 0;
  75. }
  76. /* Unassign a cache tag with specified type from domain. */
  77. static void cache_tag_unassign(struct dmar_domain *domain, u16 did,
  78. struct device *dev, ioasid_t pasid,
  79. enum cache_tag_type type)
  80. {
  81. struct device_domain_info *info = dev_iommu_priv_get(dev);
  82. struct intel_iommu *iommu = info->iommu;
  83. struct cache_tag *tag;
  84. unsigned long flags;
  85. spin_lock_irqsave(&domain->cache_lock, flags);
  86. list_for_each_entry(tag, &domain->cache_tags, node) {
  87. if (cache_tage_match(tag, did, iommu, dev, pasid, type)) {
  88. trace_cache_tag_unassign(tag);
  89. if (--tag->users == 0) {
  90. list_del(&tag->node);
  91. kfree(tag);
  92. }
  93. break;
  94. }
  95. }
  96. spin_unlock_irqrestore(&domain->cache_lock, flags);
  97. }
  98. /* domain->qi_batch will be freed in iommu_free_domain() path. */
  99. static int domain_qi_batch_alloc(struct dmar_domain *domain)
  100. {
  101. unsigned long flags;
  102. int ret = 0;
  103. spin_lock_irqsave(&domain->cache_lock, flags);
  104. if (domain->qi_batch)
  105. goto out_unlock;
  106. domain->qi_batch = kzalloc_obj(*domain->qi_batch, GFP_ATOMIC);
  107. if (!domain->qi_batch)
  108. ret = -ENOMEM;
  109. out_unlock:
  110. spin_unlock_irqrestore(&domain->cache_lock, flags);
  111. return ret;
  112. }
  113. static int __cache_tag_assign_domain(struct dmar_domain *domain, u16 did,
  114. struct device *dev, ioasid_t pasid)
  115. {
  116. struct device_domain_info *info = dev_iommu_priv_get(dev);
  117. int ret;
  118. ret = domain_qi_batch_alloc(domain);
  119. if (ret)
  120. return ret;
  121. ret = cache_tag_assign(domain, did, dev, pasid, CACHE_TAG_IOTLB);
  122. if (ret || !info->ats_enabled)
  123. return ret;
  124. ret = cache_tag_assign(domain, did, dev, pasid, CACHE_TAG_DEVTLB);
  125. if (ret)
  126. cache_tag_unassign(domain, did, dev, pasid, CACHE_TAG_IOTLB);
  127. return ret;
  128. }
  129. static void __cache_tag_unassign_domain(struct dmar_domain *domain, u16 did,
  130. struct device *dev, ioasid_t pasid)
  131. {
  132. struct device_domain_info *info = dev_iommu_priv_get(dev);
  133. cache_tag_unassign(domain, did, dev, pasid, CACHE_TAG_IOTLB);
  134. if (info->ats_enabled)
  135. cache_tag_unassign(domain, did, dev, pasid, CACHE_TAG_DEVTLB);
  136. }
  137. static int __cache_tag_assign_parent_domain(struct dmar_domain *domain, u16 did,
  138. struct device *dev, ioasid_t pasid)
  139. {
  140. struct device_domain_info *info = dev_iommu_priv_get(dev);
  141. int ret;
  142. ret = domain_qi_batch_alloc(domain);
  143. if (ret)
  144. return ret;
  145. ret = cache_tag_assign(domain, did, dev, pasid, CACHE_TAG_NESTING_IOTLB);
  146. if (ret || !info->ats_enabled)
  147. return ret;
  148. ret = cache_tag_assign(domain, did, dev, pasid, CACHE_TAG_NESTING_DEVTLB);
  149. if (ret)
  150. cache_tag_unassign(domain, did, dev, pasid, CACHE_TAG_NESTING_IOTLB);
  151. return ret;
  152. }
  153. static void __cache_tag_unassign_parent_domain(struct dmar_domain *domain, u16 did,
  154. struct device *dev, ioasid_t pasid)
  155. {
  156. struct device_domain_info *info = dev_iommu_priv_get(dev);
  157. cache_tag_unassign(domain, did, dev, pasid, CACHE_TAG_NESTING_IOTLB);
  158. if (info->ats_enabled)
  159. cache_tag_unassign(domain, did, dev, pasid, CACHE_TAG_NESTING_DEVTLB);
  160. }
  161. static u16 domain_get_id_for_dev(struct dmar_domain *domain, struct device *dev)
  162. {
  163. struct device_domain_info *info = dev_iommu_priv_get(dev);
  164. struct intel_iommu *iommu = info->iommu;
  165. /*
  166. * The driver assigns different domain IDs for all domains except
  167. * the SVA type.
  168. */
  169. if (domain->domain.type == IOMMU_DOMAIN_SVA)
  170. return FLPT_DEFAULT_DID;
  171. return domain_id_iommu(domain, iommu);
  172. }
  173. /*
  174. * Assign cache tags to a domain when it's associated with a device's
  175. * PASID using a specific domain ID.
  176. *
  177. * On success (return value of 0), cache tags are created and added to the
  178. * domain's cache tag list. On failure (negative return value), an error
  179. * code is returned indicating the reason for the failure.
  180. */
  181. int cache_tag_assign_domain(struct dmar_domain *domain,
  182. struct device *dev, ioasid_t pasid)
  183. {
  184. u16 did = domain_get_id_for_dev(domain, dev);
  185. int ret;
  186. ret = __cache_tag_assign_domain(domain, did, dev, pasid);
  187. if (ret || domain->domain.type != IOMMU_DOMAIN_NESTED)
  188. return ret;
  189. ret = __cache_tag_assign_parent_domain(domain->s2_domain, did, dev, pasid);
  190. if (ret)
  191. __cache_tag_unassign_domain(domain, did, dev, pasid);
  192. return ret;
  193. }
  194. /*
  195. * Remove the cache tags associated with a device's PASID when the domain is
  196. * detached from the device.
  197. *
  198. * The cache tags must be previously assigned to the domain by calling the
  199. * assign interface.
  200. */
  201. void cache_tag_unassign_domain(struct dmar_domain *domain,
  202. struct device *dev, ioasid_t pasid)
  203. {
  204. u16 did = domain_get_id_for_dev(domain, dev);
  205. __cache_tag_unassign_domain(domain, did, dev, pasid);
  206. if (domain->domain.type == IOMMU_DOMAIN_NESTED)
  207. __cache_tag_unassign_parent_domain(domain->s2_domain, did, dev, pasid);
  208. }
  209. static unsigned long calculate_psi_aligned_address(unsigned long start,
  210. unsigned long end,
  211. unsigned long *_pages,
  212. unsigned long *_mask)
  213. {
  214. unsigned long pages = aligned_nrpages(start, end - start + 1);
  215. unsigned long aligned_pages = __roundup_pow_of_two(pages);
  216. unsigned long bitmask = aligned_pages - 1;
  217. unsigned long mask = ilog2(aligned_pages);
  218. unsigned long pfn = IOVA_PFN(start);
  219. /*
  220. * PSI masks the low order bits of the base address. If the
  221. * address isn't aligned to the mask, then compute a mask value
  222. * needed to ensure the target range is flushed.
  223. */
  224. if (unlikely(bitmask & pfn)) {
  225. unsigned long end_pfn = pfn + pages - 1, shared_bits;
  226. /*
  227. * Since end_pfn <= pfn + bitmask, the only way bits
  228. * higher than bitmask can differ in pfn and end_pfn is
  229. * by carrying. This means after masking out bitmask,
  230. * high bits starting with the first set bit in
  231. * shared_bits are all equal in both pfn and end_pfn.
  232. */
  233. shared_bits = ~(pfn ^ end_pfn) & ~bitmask;
  234. mask = shared_bits ? __ffs(shared_bits) : MAX_AGAW_PFN_WIDTH;
  235. aligned_pages = 1UL << mask;
  236. }
  237. *_pages = aligned_pages;
  238. *_mask = mask;
  239. return ALIGN_DOWN(start, VTD_PAGE_SIZE << mask);
  240. }
  241. static void qi_batch_flush_descs(struct intel_iommu *iommu, struct qi_batch *batch)
  242. {
  243. if (!iommu || !batch->index)
  244. return;
  245. qi_submit_sync(iommu, batch->descs, batch->index, 0);
  246. /* Reset the index value and clean the whole batch buffer. */
  247. memset(batch, 0, sizeof(*batch));
  248. }
  249. static void qi_batch_increment_index(struct intel_iommu *iommu, struct qi_batch *batch)
  250. {
  251. if (++batch->index == QI_MAX_BATCHED_DESC_COUNT)
  252. qi_batch_flush_descs(iommu, batch);
  253. }
  254. static void qi_batch_add_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
  255. unsigned int size_order, u64 type,
  256. struct qi_batch *batch)
  257. {
  258. qi_desc_iotlb(iommu, did, addr, size_order, type, &batch->descs[batch->index]);
  259. qi_batch_increment_index(iommu, batch);
  260. }
  261. static void qi_batch_add_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 pfsid,
  262. u16 qdep, u64 addr, unsigned int mask,
  263. struct qi_batch *batch)
  264. {
  265. /*
  266. * According to VT-d spec, software is recommended to not submit any Device-TLB
  267. * invalidation requests while address remapping hardware is disabled.
  268. */
  269. if (!(iommu->gcmd & DMA_GCMD_TE))
  270. return;
  271. qi_desc_dev_iotlb(sid, pfsid, qdep, addr, mask, &batch->descs[batch->index]);
  272. qi_batch_increment_index(iommu, batch);
  273. }
  274. static void qi_batch_add_piotlb(struct intel_iommu *iommu, u16 did, u32 pasid,
  275. u64 addr, unsigned long npages, bool ih,
  276. struct qi_batch *batch)
  277. {
  278. /*
  279. * npages == -1 means a PASID-selective invalidation, otherwise,
  280. * a positive value for Page-selective-within-PASID invalidation.
  281. * 0 is not a valid input.
  282. */
  283. if (!npages)
  284. return;
  285. qi_desc_piotlb(did, pasid, addr, npages, ih, &batch->descs[batch->index]);
  286. qi_batch_increment_index(iommu, batch);
  287. }
  288. static void qi_batch_add_pasid_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 pfsid,
  289. u32 pasid, u16 qdep, u64 addr,
  290. unsigned int size_order, struct qi_batch *batch)
  291. {
  292. /*
  293. * According to VT-d spec, software is recommended to not submit any
  294. * Device-TLB invalidation requests while address remapping hardware
  295. * is disabled.
  296. */
  297. if (!(iommu->gcmd & DMA_GCMD_TE))
  298. return;
  299. qi_desc_dev_iotlb_pasid(sid, pfsid, pasid, qdep, addr, size_order,
  300. &batch->descs[batch->index]);
  301. qi_batch_increment_index(iommu, batch);
  302. }
  303. static bool intel_domain_use_piotlb(struct dmar_domain *domain)
  304. {
  305. return domain->domain.type == IOMMU_DOMAIN_SVA ||
  306. domain->domain.type == IOMMU_DOMAIN_NESTED ||
  307. intel_domain_is_fs_paging(domain);
  308. }
  309. static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag *tag,
  310. unsigned long addr, unsigned long pages,
  311. unsigned long mask, int ih)
  312. {
  313. struct intel_iommu *iommu = tag->iommu;
  314. u64 type = DMA_TLB_PSI_FLUSH;
  315. if (intel_domain_use_piotlb(domain)) {
  316. qi_batch_add_piotlb(iommu, tag->domain_id, tag->pasid, addr,
  317. pages, ih, domain->qi_batch);
  318. return;
  319. }
  320. /*
  321. * Fallback to domain selective flush if no PSI support or the size
  322. * is too big.
  323. */
  324. if (!cap_pgsel_inv(iommu->cap) ||
  325. mask > cap_max_amask_val(iommu->cap) || pages == -1) {
  326. addr = 0;
  327. mask = 0;
  328. ih = 0;
  329. type = DMA_TLB_DSI_FLUSH;
  330. }
  331. if (ecap_qis(iommu->ecap))
  332. qi_batch_add_iotlb(iommu, tag->domain_id, addr | ih, mask, type,
  333. domain->qi_batch);
  334. else
  335. __iommu_flush_iotlb(iommu, tag->domain_id, addr | ih, mask, type);
  336. }
  337. static void cache_tag_flush_devtlb_psi(struct dmar_domain *domain, struct cache_tag *tag,
  338. unsigned long addr, unsigned long mask)
  339. {
  340. struct intel_iommu *iommu = tag->iommu;
  341. struct device_domain_info *info;
  342. u16 sid;
  343. info = dev_iommu_priv_get(tag->dev);
  344. sid = PCI_DEVID(info->bus, info->devfn);
  345. if (tag->pasid == IOMMU_NO_PASID) {
  346. qi_batch_add_dev_iotlb(iommu, sid, info->pfsid, info->ats_qdep,
  347. addr, mask, domain->qi_batch);
  348. if (info->dtlb_extra_inval)
  349. qi_batch_add_dev_iotlb(iommu, sid, info->pfsid, info->ats_qdep,
  350. addr, mask, domain->qi_batch);
  351. return;
  352. }
  353. qi_batch_add_pasid_dev_iotlb(iommu, sid, info->pfsid, tag->pasid,
  354. info->ats_qdep, addr, mask, domain->qi_batch);
  355. if (info->dtlb_extra_inval)
  356. qi_batch_add_pasid_dev_iotlb(iommu, sid, info->pfsid, tag->pasid,
  357. info->ats_qdep, addr, mask,
  358. domain->qi_batch);
  359. }
  360. /*
  361. * Invalidates a range of IOVA from @start (inclusive) to @end (inclusive)
  362. * when the memory mappings in the target domain have been modified.
  363. */
  364. void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start,
  365. unsigned long end, int ih)
  366. {
  367. struct intel_iommu *iommu = NULL;
  368. unsigned long pages, mask, addr;
  369. struct cache_tag *tag;
  370. unsigned long flags;
  371. if (start == 0 && end == ULONG_MAX) {
  372. addr = 0;
  373. pages = -1;
  374. mask = MAX_AGAW_PFN_WIDTH;
  375. } else {
  376. addr = calculate_psi_aligned_address(start, end, &pages, &mask);
  377. }
  378. spin_lock_irqsave(&domain->cache_lock, flags);
  379. list_for_each_entry(tag, &domain->cache_tags, node) {
  380. if (iommu && iommu != tag->iommu)
  381. qi_batch_flush_descs(iommu, domain->qi_batch);
  382. iommu = tag->iommu;
  383. switch (tag->type) {
  384. case CACHE_TAG_IOTLB:
  385. case CACHE_TAG_NESTING_IOTLB:
  386. cache_tag_flush_iotlb(domain, tag, addr, pages, mask, ih);
  387. break;
  388. case CACHE_TAG_NESTING_DEVTLB:
  389. /*
  390. * Address translation cache in device side caches the
  391. * result of nested translation. There is no easy way
  392. * to identify the exact set of nested translations
  393. * affected by a change in S2. So just flush the entire
  394. * device cache.
  395. */
  396. addr = 0;
  397. mask = MAX_AGAW_PFN_WIDTH;
  398. fallthrough;
  399. case CACHE_TAG_DEVTLB:
  400. cache_tag_flush_devtlb_psi(domain, tag, addr, mask);
  401. break;
  402. }
  403. trace_cache_tag_flush_range(tag, start, end, addr, pages, mask);
  404. }
  405. qi_batch_flush_descs(iommu, domain->qi_batch);
  406. spin_unlock_irqrestore(&domain->cache_lock, flags);
  407. }
  408. /*
  409. * Invalidates all ranges of IOVA when the memory mappings in the target
  410. * domain have been modified.
  411. */
  412. void cache_tag_flush_all(struct dmar_domain *domain)
  413. {
  414. cache_tag_flush_range(domain, 0, ULONG_MAX, 0);
  415. }
  416. /*
  417. * Invalidate a range of IOVA when new mappings are created in the target
  418. * domain.
  419. *
  420. * - VT-d spec, Section 6.1 Caching Mode: When the CM field is reported as
  421. * Set, any software updates to remapping structures other than first-
  422. * stage mapping requires explicit invalidation of the caches.
  423. * - VT-d spec, Section 6.8 Write Buffer Flushing: For hardware that requires
  424. * write buffer flushing, software must explicitly perform write-buffer
  425. * flushing, if cache invalidation is not required.
  426. */
  427. void cache_tag_flush_range_np(struct dmar_domain *domain, unsigned long start,
  428. unsigned long end)
  429. {
  430. struct intel_iommu *iommu = NULL;
  431. unsigned long pages, mask, addr;
  432. struct cache_tag *tag;
  433. unsigned long flags;
  434. addr = calculate_psi_aligned_address(start, end, &pages, &mask);
  435. spin_lock_irqsave(&domain->cache_lock, flags);
  436. list_for_each_entry(tag, &domain->cache_tags, node) {
  437. if (iommu && iommu != tag->iommu)
  438. qi_batch_flush_descs(iommu, domain->qi_batch);
  439. iommu = tag->iommu;
  440. if (!cap_caching_mode(iommu->cap) ||
  441. intel_domain_is_fs_paging(domain)) {
  442. iommu_flush_write_buffer(iommu);
  443. continue;
  444. }
  445. if (tag->type == CACHE_TAG_IOTLB ||
  446. tag->type == CACHE_TAG_NESTING_IOTLB)
  447. cache_tag_flush_iotlb(domain, tag, addr, pages, mask, 0);
  448. trace_cache_tag_flush_range_np(tag, start, end, addr, pages, mask);
  449. }
  450. qi_batch_flush_descs(iommu, domain->qi_batch);
  451. spin_unlock_irqrestore(&domain->cache_lock, flags);
  452. }