1 // SPDX-License-Identifier: GPL-2.0
3 * intel-pasid.c - PASID idr, table and entry manipulation
5 * Copyright (C) 2018 Intel Corporation
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
10 #define pr_fmt(fmt) "DMAR: " fmt
12 #include <linux/bitops.h>
13 #include <linux/cpufeature.h>
14 #include <linux/dmar.h>
15 #include <linux/intel-iommu.h>
16 #include <linux/iommu.h>
17 #include <linux/memory.h>
18 #include <linux/pci.h>
19 #include <linux/pci-ats.h>
20 #include <linux/spinlock.h>
22 #include "intel-pasid.h"
25 * Intel IOMMU system wide PASID name space:
27 static DEFINE_SPINLOCK(pasid_lock);
28 u32 intel_pasid_max_id = PASID_MAX;
29 static DEFINE_IDR(pasid_idr);
31 int intel_pasid_alloc_id(void *ptr, int start, int end, gfp_t gfp)
35 min = max_t(int, start, PASID_MIN);
36 max = min_t(int, end, intel_pasid_max_id);
38 WARN_ON(in_interrupt());
40 spin_lock(&pasid_lock);
41 ret = idr_alloc(&pasid_idr, ptr, min, max, GFP_ATOMIC);
42 spin_unlock(&pasid_lock);
48 void intel_pasid_free_id(int pasid)
50 spin_lock(&pasid_lock);
51 idr_remove(&pasid_idr, pasid);
52 spin_unlock(&pasid_lock);
55 void *intel_pasid_lookup_id(int pasid)
59 spin_lock(&pasid_lock);
60 p = idr_find(&pasid_idr, pasid);
61 spin_unlock(&pasid_lock);
67 * Per device pasid table management:
70 device_attach_pasid_table(struct device_domain_info *info,
71 struct pasid_table *pasid_table)
73 info->pasid_table = pasid_table;
74 list_add(&info->table, &pasid_table->dev);
78 device_detach_pasid_table(struct device_domain_info *info,
79 struct pasid_table *pasid_table)
81 info->pasid_table = NULL;
82 list_del(&info->table);
85 struct pasid_table_opaque {
86 struct pasid_table **pasid_table;
92 static int search_pasid_table(struct device_domain_info *info, void *opaque)
94 struct pasid_table_opaque *data = opaque;
96 if (info->iommu->segment == data->segment &&
97 info->bus == data->bus &&
98 info->devfn == data->devfn &&
100 *data->pasid_table = info->pasid_table;
107 static int get_alias_pasid_table(struct pci_dev *pdev, u16 alias, void *opaque)
109 struct pasid_table_opaque *data = opaque;
111 data->segment = pci_domain_nr(pdev->bus);
112 data->bus = PCI_BUS_NUM(alias);
113 data->devfn = alias & 0xff;
115 return for_each_device_domain(&search_pasid_table, data);
119 * Allocate a pasid table for @dev. It should be called in a
120 * single-thread context.
122 int intel_pasid_alloc_table(struct device *dev)
124 struct device_domain_info *info;
125 struct pasid_table *pasid_table;
126 struct pasid_table_opaque data;
133 info = dev->archdata.iommu;
134 if (WARN_ON(!info || !dev_is_pci(dev) || info->pasid_table))
137 /* DMA alias device already has a pasid table, use it: */
138 data.pasid_table = &pasid_table;
139 ret = pci_for_each_dma_alias(to_pci_dev(dev),
140 &get_alias_pasid_table, &data);
144 pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL);
147 INIT_LIST_HEAD(&pasid_table->dev);
149 if (info->pasid_supported)
150 max_pasid = min_t(int, pci_max_pasids(to_pci_dev(dev)),
153 size = max_pasid >> (PASID_PDE_SHIFT - 3);
154 order = size ? get_order(size) : 0;
155 pages = alloc_pages_node(info->iommu->node,
156 GFP_KERNEL | __GFP_ZERO, order);
160 pasid_table->table = page_address(pages);
161 pasid_table->order = order;
162 pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
165 device_attach_pasid_table(info, pasid_table);
170 /* Get PRESENT bit of a PASID directory entry. */
172 pasid_pde_is_present(struct pasid_dir_entry *pde)
174 return READ_ONCE(pde->val) & PASID_PTE_PRESENT;
177 /* Get PASID table from a PASID directory entry. */
178 static inline struct pasid_entry *
179 get_pasid_table_from_pde(struct pasid_dir_entry *pde)
181 if (!pasid_pde_is_present(pde))
184 return phys_to_virt(READ_ONCE(pde->val) & PDE_PFN_MASK);
187 void intel_pasid_free_table(struct device *dev)
189 struct device_domain_info *info;
190 struct pasid_table *pasid_table;
191 struct pasid_dir_entry *dir;
192 struct pasid_entry *table;
195 info = dev->archdata.iommu;
196 if (!info || !dev_is_pci(dev) || !info->pasid_table)
199 pasid_table = info->pasid_table;
200 device_detach_pasid_table(info, pasid_table);
202 if (!list_empty(&pasid_table->dev))
205 /* Free scalable mode PASID directory tables: */
206 dir = pasid_table->table;
207 max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
208 for (i = 0; i < max_pde; i++) {
209 table = get_pasid_table_from_pde(&dir[i]);
210 free_pgtable_page(table);
213 free_pages((unsigned long)pasid_table->table, pasid_table->order);
217 struct pasid_table *intel_pasid_get_table(struct device *dev)
219 struct device_domain_info *info;
221 info = dev->archdata.iommu;
225 return info->pasid_table;
228 int intel_pasid_get_dev_max_id(struct device *dev)
230 struct device_domain_info *info;
232 info = dev->archdata.iommu;
233 if (!info || !info->pasid_table)
236 return info->pasid_table->max_pasid;
239 struct pasid_entry *intel_pasid_get_entry(struct device *dev, int pasid)
241 struct device_domain_info *info;
242 struct pasid_table *pasid_table;
243 struct pasid_dir_entry *dir;
244 struct pasid_entry *entries;
245 int dir_index, index;
247 pasid_table = intel_pasid_get_table(dev);
248 if (WARN_ON(!pasid_table || pasid < 0 ||
249 pasid >= intel_pasid_get_dev_max_id(dev)))
252 dir = pasid_table->table;
253 info = dev->archdata.iommu;
254 dir_index = pasid >> PASID_PDE_SHIFT;
255 index = pasid & PASID_PTE_MASK;
257 spin_lock(&pasid_lock);
258 entries = get_pasid_table_from_pde(&dir[dir_index]);
260 entries = alloc_pgtable_page(info->iommu->node);
262 spin_unlock(&pasid_lock);
266 WRITE_ONCE(dir[dir_index].val,
267 (u64)virt_to_phys(entries) | PASID_PTE_PRESENT);
269 spin_unlock(&pasid_lock);
271 return &entries[index];
275 * Interfaces for PASID table entry manipulation:
277 static inline void pasid_clear_entry(struct pasid_entry *pe)
279 WRITE_ONCE(pe->val[0], 0);
280 WRITE_ONCE(pe->val[1], 0);
281 WRITE_ONCE(pe->val[2], 0);
282 WRITE_ONCE(pe->val[3], 0);
283 WRITE_ONCE(pe->val[4], 0);
284 WRITE_ONCE(pe->val[5], 0);
285 WRITE_ONCE(pe->val[6], 0);
286 WRITE_ONCE(pe->val[7], 0);
289 static void intel_pasid_clear_entry(struct device *dev, int pasid)
291 struct pasid_entry *pe;
293 pe = intel_pasid_get_entry(dev, pasid);
297 pasid_clear_entry(pe);
300 static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits)
304 old = READ_ONCE(*ptr);
305 WRITE_ONCE(*ptr, (old & ~mask) | bits);
309 * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode
313 pasid_set_domain_id(struct pasid_entry *pe, u64 value)
315 pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value);
319 * Get domain ID value of a scalable mode PASID entry.
322 pasid_get_domain_id(struct pasid_entry *pe)
324 return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0));
328 * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63)
329 * of a scalable mode PASID entry.
332 pasid_set_slptr(struct pasid_entry *pe, u64 value)
334 pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value);
338 * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID
342 pasid_set_address_width(struct pasid_entry *pe, u64 value)
344 pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2);
348 * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8)
349 * of a scalable mode PASID entry.
352 pasid_set_translation_type(struct pasid_entry *pe, u64 value)
354 pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6);
358 * Enable fault processing by clearing the FPD(Fault Processing
359 * Disable) field (Bit 1) of a scalable mode PASID entry.
361 static inline void pasid_set_fault_enable(struct pasid_entry *pe)
363 pasid_set_bits(&pe->val[0], 1 << 1, 0);
367 * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a
368 * scalable mode PASID entry.
370 static inline void pasid_set_sre(struct pasid_entry *pe)
372 pasid_set_bits(&pe->val[2], 1 << 0, 1);
376 * Setup the P(Present) field (Bit 0) of a scalable mode PASID
379 static inline void pasid_set_present(struct pasid_entry *pe)
381 pasid_set_bits(&pe->val[0], 1 << 0, 1);
385 * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID
388 static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value)
390 pasid_set_bits(&pe->val[1], 1 << 23, value);
394 * Setup the First Level Page table Pointer field (Bit 140~191)
395 * of a scalable mode PASID entry.
398 pasid_set_flptr(struct pasid_entry *pe, u64 value)
400 pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value);
404 * Setup the First Level Paging Mode field (Bit 130~131) of a
405 * scalable mode PASID entry.
408 pasid_set_flpm(struct pasid_entry *pe, u64 value)
410 pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2);
414 pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
419 desc.qw0 = QI_PC_DID(did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid);
424 qi_submit_sync(&desc, iommu);
428 iotlb_invalidation_with_pasid(struct intel_iommu *iommu, u16 did, u32 pasid)
432 desc.qw0 = QI_EIOTLB_PASID(pasid) | QI_EIOTLB_DID(did) |
433 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;
438 qi_submit_sync(&desc, iommu);
442 devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
443 struct device *dev, int pasid)
445 struct device_domain_info *info;
446 u16 sid, qdep, pfsid;
448 info = dev->archdata.iommu;
449 if (!info || !info->ats_enabled)
452 sid = info->bus << 8 | info->devfn;
453 qdep = info->ats_qdep;
456 qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
459 void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
460 struct device *dev, int pasid)
462 struct pasid_entry *pte;
465 pte = intel_pasid_get_entry(dev, pasid);
469 did = pasid_get_domain_id(pte);
470 intel_pasid_clear_entry(dev, pasid);
472 if (!ecap_coherent(iommu->ecap))
473 clflush_cache_range(pte, sizeof(*pte));
475 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
476 iotlb_invalidation_with_pasid(iommu, did, pasid);
478 /* Device IOTLB doesn't need to be flushed in caching mode. */
479 if (!cap_caching_mode(iommu->cap))
480 devtlb_invalidation_with_pasid(iommu, dev, pasid);
484 * Set up the scalable mode pasid table entry for first only
487 int intel_pasid_setup_first_level(struct intel_iommu *iommu,
488 struct device *dev, pgd_t *pgd,
489 int pasid, u16 did, int flags)
491 struct pasid_entry *pte;
493 if (!ecap_flts(iommu->ecap)) {
494 pr_err("No first level translation support on %s\n",
499 pte = intel_pasid_get_entry(dev, pasid);
503 pasid_clear_entry(pte);
505 /* Setup the first level page table pointer: */
506 pasid_set_flptr(pte, (u64)__pa(pgd));
507 if (flags & PASID_FLAG_SUPERVISOR_MODE) {
508 if (!ecap_srs(iommu->ecap)) {
509 pr_err("No supervisor request support on %s\n",
517 if (cpu_feature_enabled(X86_FEATURE_LA57))
518 pasid_set_flpm(pte, 1);
519 #endif /* CONFIG_X86 */
521 pasid_set_domain_id(pte, did);
522 pasid_set_address_width(pte, iommu->agaw);
523 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
525 /* Setup Present and PASID Granular Transfer Type: */
526 pasid_set_translation_type(pte, 1);
527 pasid_set_present(pte);
529 if (!ecap_coherent(iommu->ecap))
530 clflush_cache_range(pte, sizeof(*pte));
532 if (cap_caching_mode(iommu->cap)) {
533 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
534 iotlb_invalidation_with_pasid(iommu, did, pasid);
536 iommu_flush_write_buffer(iommu);
543 * Set up the scalable mode pasid entry for second only translation type.
545 int intel_pasid_setup_second_level(struct intel_iommu *iommu,
546 struct dmar_domain *domain,
547 struct device *dev, int pasid)
549 struct pasid_entry *pte;
556 * If hardware advertises no support for second level
557 * translation, return directly.
559 if (!ecap_slts(iommu->ecap)) {
560 pr_err("No second level translation support on %s\n",
566 * Skip top levels of page tables for iommu which has less agaw
567 * than default. Unnecessary for PT mode.
570 for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
571 pgd = phys_to_virt(dma_pte_addr(pgd));
572 if (!dma_pte_present(pgd)) {
573 dev_err(dev, "Invalid domain page table\n");
578 pgd_val = virt_to_phys(pgd);
579 did = domain->iommu_did[iommu->seq_id];
581 pte = intel_pasid_get_entry(dev, pasid);
583 dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);
587 pasid_clear_entry(pte);
588 pasid_set_domain_id(pte, did);
589 pasid_set_slptr(pte, pgd_val);
590 pasid_set_address_width(pte, agaw);
591 pasid_set_translation_type(pte, 2);
592 pasid_set_fault_enable(pte);
593 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
596 * Since it is a second level only translation setup, we should
597 * set SRE bit as well (addresses are expected to be GPAs).
600 pasid_set_present(pte);
602 if (!ecap_coherent(iommu->ecap))
603 clflush_cache_range(pte, sizeof(*pte));
605 if (cap_caching_mode(iommu->cap)) {
606 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
607 iotlb_invalidation_with_pasid(iommu, did, pasid);
609 iommu_flush_write_buffer(iommu);
616 * Set up the scalable mode pasid entry for passthrough translation type.
618 int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
619 struct dmar_domain *domain,
620 struct device *dev, int pasid)
622 u16 did = FLPT_DEFAULT_DID;
623 struct pasid_entry *pte;
625 pte = intel_pasid_get_entry(dev, pasid);
627 dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);
631 pasid_clear_entry(pte);
632 pasid_set_domain_id(pte, did);
633 pasid_set_address_width(pte, iommu->agaw);
634 pasid_set_translation_type(pte, 4);
635 pasid_set_fault_enable(pte);
636 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
639 * We should set SRE bit as well since the addresses are expected
643 pasid_set_present(pte);
645 if (!ecap_coherent(iommu->ecap))
646 clflush_cache_range(pte, sizeof(*pte));
648 if (cap_caching_mode(iommu->cap)) {
649 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
650 iotlb_invalidation_with_pasid(iommu, did, pasid);
652 iommu_flush_write_buffer(iommu);