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);
162 pasid_table->table = page_address(pages);
163 pasid_table->order = order;
164 pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
167 device_attach_pasid_table(info, pasid_table);
169 if (!ecap_coherent(info->iommu->ecap))
170 clflush_cache_range(pasid_table->table, size);
175 void intel_pasid_free_table(struct device *dev)
177 struct device_domain_info *info;
178 struct pasid_table *pasid_table;
179 struct pasid_dir_entry *dir;
180 struct pasid_entry *table;
183 info = dev->archdata.iommu;
184 if (!info || !dev_is_pci(dev) || !info->pasid_table)
187 pasid_table = info->pasid_table;
188 device_detach_pasid_table(info, pasid_table);
190 if (!list_empty(&pasid_table->dev))
193 /* Free scalable mode PASID directory tables: */
194 dir = pasid_table->table;
195 max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
196 for (i = 0; i < max_pde; i++) {
197 table = get_pasid_table_from_pde(&dir[i]);
198 free_pgtable_page(table);
201 free_pages((unsigned long)pasid_table->table, pasid_table->order);
205 struct pasid_table *intel_pasid_get_table(struct device *dev)
207 struct device_domain_info *info;
209 info = dev->archdata.iommu;
213 return info->pasid_table;
216 int intel_pasid_get_dev_max_id(struct device *dev)
218 struct device_domain_info *info;
220 info = dev->archdata.iommu;
221 if (!info || !info->pasid_table)
224 return info->pasid_table->max_pasid;
227 struct pasid_entry *intel_pasid_get_entry(struct device *dev, int pasid)
229 struct device_domain_info *info;
230 struct pasid_table *pasid_table;
231 struct pasid_dir_entry *dir;
232 struct pasid_entry *entries;
233 int dir_index, index;
235 pasid_table = intel_pasid_get_table(dev);
236 if (WARN_ON(!pasid_table || pasid < 0 ||
237 pasid >= intel_pasid_get_dev_max_id(dev)))
240 dir = pasid_table->table;
241 info = dev->archdata.iommu;
242 dir_index = pasid >> PASID_PDE_SHIFT;
243 index = pasid & PASID_PTE_MASK;
245 spin_lock(&pasid_lock);
246 entries = get_pasid_table_from_pde(&dir[dir_index]);
248 entries = alloc_pgtable_page(info->iommu->node);
250 spin_unlock(&pasid_lock);
254 WRITE_ONCE(dir[dir_index].val,
255 (u64)virt_to_phys(entries) | PASID_PTE_PRESENT);
256 if (!ecap_coherent(info->iommu->ecap)) {
257 clflush_cache_range(entries, VTD_PAGE_SIZE);
258 clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
261 spin_unlock(&pasid_lock);
263 return &entries[index];
267 * Interfaces for PASID table entry manipulation:
269 static inline void pasid_clear_entry(struct pasid_entry *pe)
271 WRITE_ONCE(pe->val[0], 0);
272 WRITE_ONCE(pe->val[1], 0);
273 WRITE_ONCE(pe->val[2], 0);
274 WRITE_ONCE(pe->val[3], 0);
275 WRITE_ONCE(pe->val[4], 0);
276 WRITE_ONCE(pe->val[5], 0);
277 WRITE_ONCE(pe->val[6], 0);
278 WRITE_ONCE(pe->val[7], 0);
281 static void intel_pasid_clear_entry(struct device *dev, int pasid)
283 struct pasid_entry *pe;
285 pe = intel_pasid_get_entry(dev, pasid);
289 pasid_clear_entry(pe);
292 static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits)
296 old = READ_ONCE(*ptr);
297 WRITE_ONCE(*ptr, (old & ~mask) | bits);
301 * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode
305 pasid_set_domain_id(struct pasid_entry *pe, u64 value)
307 pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value);
311 * Get domain ID value of a scalable mode PASID entry.
314 pasid_get_domain_id(struct pasid_entry *pe)
316 return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0));
320 * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63)
321 * of a scalable mode PASID entry.
324 pasid_set_slptr(struct pasid_entry *pe, u64 value)
326 pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value);
330 * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID
334 pasid_set_address_width(struct pasid_entry *pe, u64 value)
336 pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2);
340 * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8)
341 * of a scalable mode PASID entry.
344 pasid_set_translation_type(struct pasid_entry *pe, u64 value)
346 pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6);
350 * Enable fault processing by clearing the FPD(Fault Processing
351 * Disable) field (Bit 1) of a scalable mode PASID entry.
353 static inline void pasid_set_fault_enable(struct pasid_entry *pe)
355 pasid_set_bits(&pe->val[0], 1 << 1, 0);
359 * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a
360 * scalable mode PASID entry.
362 static inline void pasid_set_sre(struct pasid_entry *pe)
364 pasid_set_bits(&pe->val[2], 1 << 0, 1);
368 * Setup the P(Present) field (Bit 0) of a scalable mode PASID
371 static inline void pasid_set_present(struct pasid_entry *pe)
373 pasid_set_bits(&pe->val[0], 1 << 0, 1);
377 * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID
380 static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value)
382 pasid_set_bits(&pe->val[1], 1 << 23, value << 23);
386 * Setup the First Level Page table Pointer field (Bit 140~191)
387 * of a scalable mode PASID entry.
390 pasid_set_flptr(struct pasid_entry *pe, u64 value)
392 pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value);
396 * Setup the First Level Paging Mode field (Bit 130~131) of a
397 * scalable mode PASID entry.
400 pasid_set_flpm(struct pasid_entry *pe, u64 value)
402 pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2);
406 pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
411 desc.qw0 = QI_PC_DID(did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid);
416 qi_submit_sync(&desc, iommu);
420 iotlb_invalidation_with_pasid(struct intel_iommu *iommu, u16 did, u32 pasid)
424 desc.qw0 = QI_EIOTLB_PASID(pasid) | QI_EIOTLB_DID(did) |
425 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE;
430 qi_submit_sync(&desc, iommu);
434 devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
435 struct device *dev, int pasid)
437 struct device_domain_info *info;
438 u16 sid, qdep, pfsid;
440 info = dev->archdata.iommu;
441 if (!info || !info->ats_enabled)
444 sid = info->bus << 8 | info->devfn;
445 qdep = info->ats_qdep;
448 qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
451 void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
452 struct device *dev, int pasid)
454 struct pasid_entry *pte;
457 pte = intel_pasid_get_entry(dev, pasid);
461 did = pasid_get_domain_id(pte);
462 intel_pasid_clear_entry(dev, pasid);
464 if (!ecap_coherent(iommu->ecap))
465 clflush_cache_range(pte, sizeof(*pte));
467 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
468 iotlb_invalidation_with_pasid(iommu, did, pasid);
470 /* Device IOTLB doesn't need to be flushed in caching mode. */
471 if (!cap_caching_mode(iommu->cap))
472 devtlb_invalidation_with_pasid(iommu, dev, pasid);
476 * Set up the scalable mode pasid table entry for first only
479 int intel_pasid_setup_first_level(struct intel_iommu *iommu,
480 struct device *dev, pgd_t *pgd,
481 int pasid, u16 did, int flags)
483 struct pasid_entry *pte;
485 if (!ecap_flts(iommu->ecap)) {
486 pr_err("No first level translation support on %s\n",
491 pte = intel_pasid_get_entry(dev, pasid);
495 pasid_clear_entry(pte);
497 /* Setup the first level page table pointer: */
498 pasid_set_flptr(pte, (u64)__pa(pgd));
499 if (flags & PASID_FLAG_SUPERVISOR_MODE) {
500 if (!ecap_srs(iommu->ecap)) {
501 pr_err("No supervisor request support on %s\n",
509 /* Both CPU and IOMMU paging mode need to match */
510 if (cpu_feature_enabled(X86_FEATURE_LA57)) {
511 if (cap_5lp_support(iommu->cap)) {
512 pasid_set_flpm(pte, 1);
514 pr_err("VT-d has no 5-level paging support for CPU\n");
515 pasid_clear_entry(pte);
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);