pasid.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 <[email protected]>
  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. /*
  21. * Intel IOMMU system wide PASID name space:
  22. */
  23. u32 intel_pasid_max_id = PASID_MAX;
  24. int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid)
  25. {
  26. unsigned long flags;
  27. u8 status_code;
  28. int ret = 0;
  29. u64 res;
  30. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  31. dmar_writeq(iommu->reg + DMAR_VCMD_REG, VCMD_CMD_ALLOC);
  32. IOMMU_WAIT_OP(iommu, DMAR_VCRSP_REG, dmar_readq,
  33. !(res & VCMD_VRSP_IP), res);
  34. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  35. status_code = VCMD_VRSP_SC(res);
  36. switch (status_code) {
  37. case VCMD_VRSP_SC_SUCCESS:
  38. *pasid = VCMD_VRSP_RESULT_PASID(res);
  39. break;
  40. case VCMD_VRSP_SC_NO_PASID_AVAIL:
  41. pr_info("IOMMU: %s: No PASID available\n", iommu->name);
  42. ret = -ENOSPC;
  43. break;
  44. default:
  45. ret = -ENODEV;
  46. pr_warn("IOMMU: %s: Unexpected error code %d\n",
  47. iommu->name, status_code);
  48. }
  49. return ret;
  50. }
  51. void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid)
  52. {
  53. unsigned long flags;
  54. u8 status_code;
  55. u64 res;
  56. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  57. dmar_writeq(iommu->reg + DMAR_VCMD_REG,
  58. VCMD_CMD_OPERAND(pasid) | VCMD_CMD_FREE);
  59. IOMMU_WAIT_OP(iommu, DMAR_VCRSP_REG, dmar_readq,
  60. !(res & VCMD_VRSP_IP), res);
  61. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  62. status_code = VCMD_VRSP_SC(res);
  63. switch (status_code) {
  64. case VCMD_VRSP_SC_SUCCESS:
  65. break;
  66. case VCMD_VRSP_SC_INVALID_PASID:
  67. pr_info("IOMMU: %s: Invalid PASID\n", iommu->name);
  68. break;
  69. default:
  70. pr_warn("IOMMU: %s: Unexpected error code %d\n",
  71. iommu->name, status_code);
  72. }
  73. }
  74. /*
  75. * Per device pasid table management:
  76. */
  77. /*
  78. * Allocate a pasid table for @dev. It should be called in a
  79. * single-thread context.
  80. */
  81. int intel_pasid_alloc_table(struct device *dev)
  82. {
  83. struct device_domain_info *info;
  84. struct pasid_table *pasid_table;
  85. struct page *pages;
  86. u32 max_pasid = 0;
  87. int order, size;
  88. might_sleep();
  89. info = dev_iommu_priv_get(dev);
  90. if (WARN_ON(!info || !dev_is_pci(dev) || info->pasid_table))
  91. return -EINVAL;
  92. pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL);
  93. if (!pasid_table)
  94. return -ENOMEM;
  95. if (info->pasid_supported)
  96. max_pasid = min_t(u32, pci_max_pasids(to_pci_dev(dev)),
  97. intel_pasid_max_id);
  98. size = max_pasid >> (PASID_PDE_SHIFT - 3);
  99. order = size ? get_order(size) : 0;
  100. pages = alloc_pages_node(info->iommu->node,
  101. GFP_KERNEL | __GFP_ZERO, order);
  102. if (!pages) {
  103. kfree(pasid_table);
  104. return -ENOMEM;
  105. }
  106. pasid_table->table = page_address(pages);
  107. pasid_table->order = order;
  108. pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
  109. info->pasid_table = pasid_table;
  110. if (!ecap_coherent(info->iommu->ecap))
  111. clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE);
  112. return 0;
  113. }
  114. void intel_pasid_free_table(struct device *dev)
  115. {
  116. struct device_domain_info *info;
  117. struct pasid_table *pasid_table;
  118. struct pasid_dir_entry *dir;
  119. struct pasid_entry *table;
  120. int i, max_pde;
  121. info = dev_iommu_priv_get(dev);
  122. if (!info || !dev_is_pci(dev) || !info->pasid_table)
  123. return;
  124. pasid_table = info->pasid_table;
  125. info->pasid_table = NULL;
  126. /* Free scalable mode PASID directory tables: */
  127. dir = pasid_table->table;
  128. max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
  129. for (i = 0; i < max_pde; i++) {
  130. table = get_pasid_table_from_pde(&dir[i]);
  131. free_pgtable_page(table);
  132. }
  133. free_pages((unsigned long)pasid_table->table, pasid_table->order);
  134. kfree(pasid_table);
  135. }
  136. struct pasid_table *intel_pasid_get_table(struct device *dev)
  137. {
  138. struct device_domain_info *info;
  139. info = dev_iommu_priv_get(dev);
  140. if (!info)
  141. return NULL;
  142. return info->pasid_table;
  143. }
  144. static int intel_pasid_get_dev_max_id(struct device *dev)
  145. {
  146. struct device_domain_info *info;
  147. info = dev_iommu_priv_get(dev);
  148. if (!info || !info->pasid_table)
  149. return 0;
  150. return info->pasid_table->max_pasid;
  151. }
  152. static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
  153. {
  154. struct device_domain_info *info;
  155. struct pasid_table *pasid_table;
  156. struct pasid_dir_entry *dir;
  157. struct pasid_entry *entries;
  158. int dir_index, index;
  159. pasid_table = intel_pasid_get_table(dev);
  160. if (WARN_ON(!pasid_table || pasid >= intel_pasid_get_dev_max_id(dev)))
  161. return NULL;
  162. dir = pasid_table->table;
  163. info = dev_iommu_priv_get(dev);
  164. dir_index = pasid >> PASID_PDE_SHIFT;
  165. index = pasid & PASID_PTE_MASK;
  166. retry:
  167. entries = get_pasid_table_from_pde(&dir[dir_index]);
  168. if (!entries) {
  169. entries = alloc_pgtable_page(info->iommu->node);
  170. if (!entries)
  171. return NULL;
  172. /*
  173. * The pasid directory table entry won't be freed after
  174. * allocation. No worry about the race with free and
  175. * clear. However, this entry might be populated by others
  176. * while we are preparing it. Use theirs with a retry.
  177. */
  178. if (cmpxchg64(&dir[dir_index].val, 0ULL,
  179. (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {
  180. free_pgtable_page(entries);
  181. goto retry;
  182. }
  183. if (!ecap_coherent(info->iommu->ecap)) {
  184. clflush_cache_range(entries, VTD_PAGE_SIZE);
  185. clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
  186. }
  187. }
  188. return &entries[index];
  189. }
  190. /*
  191. * Interfaces for PASID table entry manipulation:
  192. */
  193. static inline void pasid_clear_entry(struct pasid_entry *pe)
  194. {
  195. WRITE_ONCE(pe->val[0], 0);
  196. WRITE_ONCE(pe->val[1], 0);
  197. WRITE_ONCE(pe->val[2], 0);
  198. WRITE_ONCE(pe->val[3], 0);
  199. WRITE_ONCE(pe->val[4], 0);
  200. WRITE_ONCE(pe->val[5], 0);
  201. WRITE_ONCE(pe->val[6], 0);
  202. WRITE_ONCE(pe->val[7], 0);
  203. }
  204. static inline void pasid_clear_entry_with_fpd(struct pasid_entry *pe)
  205. {
  206. WRITE_ONCE(pe->val[0], PASID_PTE_FPD);
  207. WRITE_ONCE(pe->val[1], 0);
  208. WRITE_ONCE(pe->val[2], 0);
  209. WRITE_ONCE(pe->val[3], 0);
  210. WRITE_ONCE(pe->val[4], 0);
  211. WRITE_ONCE(pe->val[5], 0);
  212. WRITE_ONCE(pe->val[6], 0);
  213. WRITE_ONCE(pe->val[7], 0);
  214. }
  215. static void
  216. intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore)
  217. {
  218. struct pasid_entry *pe;
  219. pe = intel_pasid_get_entry(dev, pasid);
  220. if (WARN_ON(!pe))
  221. return;
  222. if (fault_ignore && pasid_pte_is_present(pe))
  223. pasid_clear_entry_with_fpd(pe);
  224. else
  225. pasid_clear_entry(pe);
  226. }
  227. static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits)
  228. {
  229. u64 old;
  230. old = READ_ONCE(*ptr);
  231. WRITE_ONCE(*ptr, (old & ~mask) | bits);
  232. }
  233. /*
  234. * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode
  235. * PASID entry.
  236. */
  237. static inline void
  238. pasid_set_domain_id(struct pasid_entry *pe, u64 value)
  239. {
  240. pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value);
  241. }
  242. /*
  243. * Get domain ID value of a scalable mode PASID entry.
  244. */
  245. static inline u16
  246. pasid_get_domain_id(struct pasid_entry *pe)
  247. {
  248. return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0));
  249. }
  250. /*
  251. * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63)
  252. * of a scalable mode PASID entry.
  253. */
  254. static inline void
  255. pasid_set_slptr(struct pasid_entry *pe, u64 value)
  256. {
  257. pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value);
  258. }
  259. /*
  260. * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID
  261. * entry.
  262. */
  263. static inline void
  264. pasid_set_address_width(struct pasid_entry *pe, u64 value)
  265. {
  266. pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2);
  267. }
  268. /*
  269. * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8)
  270. * of a scalable mode PASID entry.
  271. */
  272. static inline void
  273. pasid_set_translation_type(struct pasid_entry *pe, u64 value)
  274. {
  275. pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6);
  276. }
  277. /*
  278. * Enable fault processing by clearing the FPD(Fault Processing
  279. * Disable) field (Bit 1) of a scalable mode PASID entry.
  280. */
  281. static inline void pasid_set_fault_enable(struct pasid_entry *pe)
  282. {
  283. pasid_set_bits(&pe->val[0], 1 << 1, 0);
  284. }
  285. /*
  286. * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a
  287. * scalable mode PASID entry.
  288. */
  289. static inline void pasid_set_sre(struct pasid_entry *pe)
  290. {
  291. pasid_set_bits(&pe->val[2], 1 << 0, 1);
  292. }
  293. /*
  294. * Setup the WPE(Write Protect Enable) field (Bit 132) of a
  295. * scalable mode PASID entry.
  296. */
  297. static inline void pasid_set_wpe(struct pasid_entry *pe)
  298. {
  299. pasid_set_bits(&pe->val[2], 1 << 4, 1 << 4);
  300. }
  301. /*
  302. * Setup the P(Present) field (Bit 0) of a scalable mode PASID
  303. * entry.
  304. */
  305. static inline void pasid_set_present(struct pasid_entry *pe)
  306. {
  307. pasid_set_bits(&pe->val[0], 1 << 0, 1);
  308. }
  309. /*
  310. * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID
  311. * entry.
  312. */
  313. static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value)
  314. {
  315. pasid_set_bits(&pe->val[1], 1 << 23, value << 23);
  316. }
  317. /*
  318. * Setup No Execute Enable bit (Bit 133) of a scalable mode PASID
  319. * entry. It is required when XD bit of the first level page table
  320. * entry is about to be set.
  321. */
  322. static inline void pasid_set_nxe(struct pasid_entry *pe)
  323. {
  324. pasid_set_bits(&pe->val[2], 1 << 5, 1 << 5);
  325. }
  326. /*
  327. * Setup the Page Snoop (PGSNP) field (Bit 88) of a scalable mode
  328. * PASID entry.
  329. */
  330. static inline void
  331. pasid_set_pgsnp(struct pasid_entry *pe)
  332. {
  333. pasid_set_bits(&pe->val[1], 1ULL << 24, 1ULL << 24);
  334. }
  335. /*
  336. * Setup the First Level Page table Pointer field (Bit 140~191)
  337. * of a scalable mode PASID entry.
  338. */
  339. static inline void
  340. pasid_set_flptr(struct pasid_entry *pe, u64 value)
  341. {
  342. pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value);
  343. }
  344. /*
  345. * Setup the First Level Paging Mode field (Bit 130~131) of a
  346. * scalable mode PASID entry.
  347. */
  348. static inline void
  349. pasid_set_flpm(struct pasid_entry *pe, u64 value)
  350. {
  351. pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2);
  352. }
  353. static void
  354. pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
  355. u16 did, u32 pasid)
  356. {
  357. struct qi_desc desc;
  358. desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) |
  359. QI_PC_PASID(pasid) | QI_PC_TYPE;
  360. desc.qw1 = 0;
  361. desc.qw2 = 0;
  362. desc.qw3 = 0;
  363. qi_submit_sync(iommu, &desc, 1, 0);
  364. }
  365. static void
  366. devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
  367. struct device *dev, u32 pasid)
  368. {
  369. struct device_domain_info *info;
  370. u16 sid, qdep, pfsid;
  371. info = dev_iommu_priv_get(dev);
  372. if (!info || !info->ats_enabled)
  373. return;
  374. sid = info->bus << 8 | info->devfn;
  375. qdep = info->ats_qdep;
  376. pfsid = info->pfsid;
  377. /*
  378. * When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID),
  379. * devTLB flush w/o PASID should be used. For non-zero PASID under
  380. * SVA usage, device could do DMA with multiple PASIDs. It is more
  381. * efficient to flush devTLB specific to the PASID.
  382. */
  383. if (pasid == PASID_RID2PASID)
  384. qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
  385. else
  386. qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
  387. }
  388. void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
  389. u32 pasid, bool fault_ignore)
  390. {
  391. struct pasid_entry *pte;
  392. u16 did, pgtt;
  393. spin_lock(&iommu->lock);
  394. pte = intel_pasid_get_entry(dev, pasid);
  395. if (WARN_ON(!pte) || !pasid_pte_is_present(pte)) {
  396. spin_unlock(&iommu->lock);
  397. return;
  398. }
  399. did = pasid_get_domain_id(pte);
  400. pgtt = pasid_pte_get_pgtt(pte);
  401. intel_pasid_clear_entry(dev, pasid, fault_ignore);
  402. spin_unlock(&iommu->lock);
  403. if (!ecap_coherent(iommu->ecap))
  404. clflush_cache_range(pte, sizeof(*pte));
  405. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  406. if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY)
  407. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  408. else
  409. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  410. /* Device IOTLB doesn't need to be flushed in caching mode. */
  411. if (!cap_caching_mode(iommu->cap))
  412. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  413. }
  414. /*
  415. * This function flushes cache for a newly setup pasid table entry.
  416. * Caller of it should not modify the in-use pasid table entries.
  417. */
  418. static void pasid_flush_caches(struct intel_iommu *iommu,
  419. struct pasid_entry *pte,
  420. u32 pasid, u16 did)
  421. {
  422. if (!ecap_coherent(iommu->ecap))
  423. clflush_cache_range(pte, sizeof(*pte));
  424. if (cap_caching_mode(iommu->cap)) {
  425. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  426. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  427. } else {
  428. iommu_flush_write_buffer(iommu);
  429. }
  430. }
  431. /*
  432. * Set up the scalable mode pasid table entry for first only
  433. * translation type.
  434. */
  435. int intel_pasid_setup_first_level(struct intel_iommu *iommu,
  436. struct device *dev, pgd_t *pgd,
  437. u32 pasid, u16 did, int flags)
  438. {
  439. struct pasid_entry *pte;
  440. if (!ecap_flts(iommu->ecap)) {
  441. pr_err("No first level translation support on %s\n",
  442. iommu->name);
  443. return -EINVAL;
  444. }
  445. if (flags & PASID_FLAG_SUPERVISOR_MODE) {
  446. #ifdef CONFIG_X86
  447. unsigned long cr0 = read_cr0();
  448. /* CR0.WP is normally set but just to be sure */
  449. if (unlikely(!(cr0 & X86_CR0_WP))) {
  450. pr_err("No CPU write protect!\n");
  451. return -EINVAL;
  452. }
  453. #endif
  454. if (!ecap_srs(iommu->ecap)) {
  455. pr_err("No supervisor request support on %s\n",
  456. iommu->name);
  457. return -EINVAL;
  458. }
  459. }
  460. if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
  461. pr_err("No 5-level paging support for first-level on %s\n",
  462. iommu->name);
  463. return -EINVAL;
  464. }
  465. spin_lock(&iommu->lock);
  466. pte = intel_pasid_get_entry(dev, pasid);
  467. if (!pte) {
  468. spin_unlock(&iommu->lock);
  469. return -ENODEV;
  470. }
  471. if (pasid_pte_is_present(pte)) {
  472. spin_unlock(&iommu->lock);
  473. return -EBUSY;
  474. }
  475. pasid_clear_entry(pte);
  476. /* Setup the first level page table pointer: */
  477. pasid_set_flptr(pte, (u64)__pa(pgd));
  478. if (flags & PASID_FLAG_SUPERVISOR_MODE) {
  479. pasid_set_sre(pte);
  480. pasid_set_wpe(pte);
  481. }
  482. if (flags & PASID_FLAG_FL5LP)
  483. pasid_set_flpm(pte, 1);
  484. if (flags & PASID_FLAG_PAGE_SNOOP)
  485. pasid_set_pgsnp(pte);
  486. pasid_set_domain_id(pte, did);
  487. pasid_set_address_width(pte, iommu->agaw);
  488. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  489. pasid_set_nxe(pte);
  490. /* Setup Present and PASID Granular Transfer Type: */
  491. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
  492. pasid_set_present(pte);
  493. spin_unlock(&iommu->lock);
  494. pasid_flush_caches(iommu, pte, pasid, did);
  495. return 0;
  496. }
  497. /*
  498. * Skip top levels of page tables for iommu which has less agaw
  499. * than default. Unnecessary for PT mode.
  500. */
  501. static inline int iommu_skip_agaw(struct dmar_domain *domain,
  502. struct intel_iommu *iommu,
  503. struct dma_pte **pgd)
  504. {
  505. int agaw;
  506. for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
  507. *pgd = phys_to_virt(dma_pte_addr(*pgd));
  508. if (!dma_pte_present(*pgd))
  509. return -EINVAL;
  510. }
  511. return agaw;
  512. }
  513. /*
  514. * Set up the scalable mode pasid entry for second only translation type.
  515. */
  516. int intel_pasid_setup_second_level(struct intel_iommu *iommu,
  517. struct dmar_domain *domain,
  518. struct device *dev, u32 pasid)
  519. {
  520. struct pasid_entry *pte;
  521. struct dma_pte *pgd;
  522. u64 pgd_val;
  523. int agaw;
  524. u16 did;
  525. /*
  526. * If hardware advertises no support for second level
  527. * translation, return directly.
  528. */
  529. if (!ecap_slts(iommu->ecap)) {
  530. pr_err("No second level translation support on %s\n",
  531. iommu->name);
  532. return -EINVAL;
  533. }
  534. pgd = domain->pgd;
  535. agaw = iommu_skip_agaw(domain, iommu, &pgd);
  536. if (agaw < 0) {
  537. dev_err(dev, "Invalid domain page table\n");
  538. return -EINVAL;
  539. }
  540. pgd_val = virt_to_phys(pgd);
  541. did = domain_id_iommu(domain, iommu);
  542. spin_lock(&iommu->lock);
  543. pte = intel_pasid_get_entry(dev, pasid);
  544. if (!pte) {
  545. spin_unlock(&iommu->lock);
  546. return -ENODEV;
  547. }
  548. if (pasid_pte_is_present(pte)) {
  549. spin_unlock(&iommu->lock);
  550. return -EBUSY;
  551. }
  552. pasid_clear_entry(pte);
  553. pasid_set_domain_id(pte, did);
  554. pasid_set_slptr(pte, pgd_val);
  555. pasid_set_address_width(pte, agaw);
  556. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
  557. pasid_set_fault_enable(pte);
  558. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  559. /*
  560. * Since it is a second level only translation setup, we should
  561. * set SRE bit as well (addresses are expected to be GPAs).
  562. */
  563. if (pasid != PASID_RID2PASID && ecap_srs(iommu->ecap))
  564. pasid_set_sre(pte);
  565. pasid_set_present(pte);
  566. spin_unlock(&iommu->lock);
  567. pasid_flush_caches(iommu, pte, pasid, did);
  568. return 0;
  569. }
  570. /*
  571. * Set up the scalable mode pasid entry for passthrough translation type.
  572. */
  573. int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
  574. struct dmar_domain *domain,
  575. struct device *dev, u32 pasid)
  576. {
  577. u16 did = FLPT_DEFAULT_DID;
  578. struct pasid_entry *pte;
  579. spin_lock(&iommu->lock);
  580. pte = intel_pasid_get_entry(dev, pasid);
  581. if (!pte) {
  582. spin_unlock(&iommu->lock);
  583. return -ENODEV;
  584. }
  585. if (pasid_pte_is_present(pte)) {
  586. spin_unlock(&iommu->lock);
  587. return -EBUSY;
  588. }
  589. pasid_clear_entry(pte);
  590. pasid_set_domain_id(pte, did);
  591. pasid_set_address_width(pte, iommu->agaw);
  592. pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
  593. pasid_set_fault_enable(pte);
  594. pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
  595. /*
  596. * We should set SRE bit as well since the addresses are expected
  597. * to be GPAs.
  598. */
  599. if (ecap_srs(iommu->ecap))
  600. pasid_set_sre(pte);
  601. pasid_set_present(pte);
  602. spin_unlock(&iommu->lock);
  603. pasid_flush_caches(iommu, pte, pasid, did);
  604. return 0;
  605. }
  606. /*
  607. * Set the page snoop control for a pasid entry which has been set up.
  608. */
  609. void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
  610. struct device *dev, u32 pasid)
  611. {
  612. struct pasid_entry *pte;
  613. u16 did;
  614. spin_lock(&iommu->lock);
  615. pte = intel_pasid_get_entry(dev, pasid);
  616. if (WARN_ON(!pte || !pasid_pte_is_present(pte))) {
  617. spin_unlock(&iommu->lock);
  618. return;
  619. }
  620. pasid_set_pgsnp(pte);
  621. did = pasid_get_domain_id(pte);
  622. spin_unlock(&iommu->lock);
  623. if (!ecap_coherent(iommu->ecap))
  624. clflush_cache_range(pte, sizeof(*pte));
  625. /*
  626. * VT-d spec 3.4 table23 states guides for cache invalidation:
  627. *
  628. * - PASID-selective-within-Domain PASID-cache invalidation
  629. * - PASID-selective PASID-based IOTLB invalidation
  630. * - If (pasid is RID_PASID)
  631. * - Global Device-TLB invalidation to affected functions
  632. * Else
  633. * - PASID-based Device-TLB invalidation (with S=1 and
  634. * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
  635. */
  636. pasid_cache_invalidation_with_pasid(iommu, did, pasid);
  637. qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
  638. /* Device IOTLB doesn't need to be flushed in caching mode. */
  639. if (!cap_caching_mode(iommu->cap))
  640. devtlb_invalidation_with_pasid(iommu, dev, pasid);
  641. }