Skip to content

Commit b0d1f87

Browse files
Jacob Panjoergroedel
authored andcommitted
iommu/vt-d: Add nested translation helper function
Nested translation mode is supported in VT-d 3.0 Spec.CH 3.8. With PASID granular translation type set to 0x11b, translation result from the first level(FL) also subject to a second level(SL) page table translation. This mode is used for SVA virtualization, where FL performs guest virtual to guest physical translation and SL performs guest physical to host physical translation. This patch adds a helper function for setting up nested translation where second level comes from a domain and first level comes from a guest PGD. Signed-off-by: Jacob Pan <[email protected]> Signed-off-by: Liu Yi L <[email protected]> Signed-off-by: Lu Baolu <[email protected]> Reviewed-by: Eric Auger <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 3aef9ca commit b0d1f87

File tree

5 files changed

+206
-28
lines changed

5 files changed

+206
-28
lines changed

drivers/iommu/intel-iommu.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -296,31 +296,6 @@ static inline void context_clear_entry(struct context_entry *context)
296296
static struct dmar_domain *si_domain;
297297
static int hw_pass_through = 1;
298298

299-
/* si_domain contains mulitple devices */
300-
#define DOMAIN_FLAG_STATIC_IDENTITY BIT(0)
301-
302-
/*
303-
* This is a DMA domain allocated through the iommu domain allocation
304-
* interface. But one or more devices belonging to this domain have
305-
* been chosen to use a private domain. We should avoid to use the
306-
* map/unmap/iova_to_phys APIs on it.
307-
*/
308-
#define DOMAIN_FLAG_LOSE_CHILDREN BIT(1)
309-
310-
/*
311-
* When VT-d works in the scalable mode, it allows DMA translation to
312-
* happen through either first level or second level page table. This
313-
* bit marks that the DMA translation for the domain goes through the
314-
* first level page table, otherwise, it goes through the second level.
315-
*/
316-
#define DOMAIN_FLAG_USE_FIRST_LEVEL BIT(2)
317-
318-
/*
319-
* Domain represents a virtual machine which demands iommu nested
320-
* translation mode support.
321-
*/
322-
#define DOMAIN_FLAG_NESTING_MODE BIT(3)
323-
324299
#define for_each_domain_iommu(idx, domain) \
325300
for (idx = 0; idx < g_num_of_iommus; idx++) \
326301
if (domain->iommu_refcnt[idx])

drivers/iommu/intel-pasid.c

Lines changed: 171 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ pasid_set_flpm(struct pasid_entry *pe, u64 value)
359359
pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2);
360360
}
361361

362+
/*
363+
* Setup the Extended Access Flag Enable (EAFE) field (Bit 135)
364+
* of a scalable mode PASID entry.
365+
*/
366+
static inline void
367+
pasid_set_eafe(struct pasid_entry *pe)
368+
{
369+
pasid_set_bits(&pe->val[2], 1 << 7, 1 << 7);
370+
}
371+
362372
static void
363373
pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
364374
u16 did, int pasid)
@@ -492,7 +502,7 @@ int intel_pasid_setup_first_level(struct intel_iommu *iommu,
492502
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
493503

494504
/* Setup Present and PASID Granular Transfer Type: */
495-
pasid_set_translation_type(pte, 1);
505+
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
496506
pasid_set_present(pte);
497507
pasid_flush_caches(iommu, pte, pasid, did);
498508

@@ -561,7 +571,7 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,
561571
pasid_set_domain_id(pte, did);
562572
pasid_set_slptr(pte, pgd_val);
563573
pasid_set_address_width(pte, agaw);
564-
pasid_set_translation_type(pte, 2);
574+
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
565575
pasid_set_fault_enable(pte);
566576
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
567577

@@ -595,7 +605,7 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
595605
pasid_clear_entry(pte);
596606
pasid_set_domain_id(pte, did);
597607
pasid_set_address_width(pte, iommu->agaw);
598-
pasid_set_translation_type(pte, 4);
608+
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
599609
pasid_set_fault_enable(pte);
600610
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
601611

@@ -609,3 +619,161 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
609619

610620
return 0;
611621
}
622+
623+
static int
624+
intel_pasid_setup_bind_data(struct intel_iommu *iommu, struct pasid_entry *pte,
625+
struct iommu_gpasid_bind_data_vtd *pasid_data)
626+
{
627+
/*
628+
* Not all guest PASID table entry fields are passed down during bind,
629+
* here we only set up the ones that are dependent on guest settings.
630+
* Execution related bits such as NXE, SMEP are not supported.
631+
* Other fields, such as snoop related, are set based on host needs
632+
* regardless of guest settings.
633+
*/
634+
if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_SRE) {
635+
if (!ecap_srs(iommu->ecap)) {
636+
pr_err_ratelimited("No supervisor request support on %s\n",
637+
iommu->name);
638+
return -EINVAL;
639+
}
640+
pasid_set_sre(pte);
641+
}
642+
643+
if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_EAFE) {
644+
if (!ecap_eafs(iommu->ecap)) {
645+
pr_err_ratelimited("No extended access flag support on %s\n",
646+
iommu->name);
647+
return -EINVAL;
648+
}
649+
pasid_set_eafe(pte);
650+
}
651+
652+
/*
653+
* Memory type is only applicable to devices inside processor coherent
654+
* domain. Will add MTS support once coherent devices are available.
655+
*/
656+
if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_MTS_MASK) {
657+
pr_warn_ratelimited("No memory type support %s\n",
658+
iommu->name);
659+
return -EINVAL;
660+
}
661+
662+
return 0;
663+
}
664+
665+
/**
666+
* intel_pasid_setup_nested() - Set up PASID entry for nested translation.
667+
* This could be used for guest shared virtual address. In this case, the
668+
* first level page tables are used for GVA-GPA translation in the guest,
669+
* second level page tables are used for GPA-HPA translation.
670+
*
671+
* @iommu: IOMMU which the device belong to
672+
* @dev: Device to be set up for translation
673+
* @gpgd: FLPTPTR: First Level Page translation pointer in GPA
674+
* @pasid: PASID to be programmed in the device PASID table
675+
* @pasid_data: Additional PASID info from the guest bind request
676+
* @domain: Domain info for setting up second level page tables
677+
* @addr_width: Address width of the first level (guest)
678+
*/
679+
int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
680+
pgd_t *gpgd, int pasid,
681+
struct iommu_gpasid_bind_data_vtd *pasid_data,
682+
struct dmar_domain *domain, int addr_width)
683+
{
684+
struct pasid_entry *pte;
685+
struct dma_pte *pgd;
686+
int ret = 0;
687+
u64 pgd_val;
688+
int agaw;
689+
u16 did;
690+
691+
if (!ecap_nest(iommu->ecap)) {
692+
pr_err_ratelimited("IOMMU: %s: No nested translation support\n",
693+
iommu->name);
694+
return -EINVAL;
695+
}
696+
697+
if (!(domain->flags & DOMAIN_FLAG_NESTING_MODE)) {
698+
pr_err_ratelimited("Domain is not in nesting mode, %x\n",
699+
domain->flags);
700+
return -EINVAL;
701+
}
702+
703+
pte = intel_pasid_get_entry(dev, pasid);
704+
if (WARN_ON(!pte))
705+
return -EINVAL;
706+
707+
/*
708+
* Caller must ensure PASID entry is not in use, i.e. not bind the
709+
* same PASID to the same device twice.
710+
*/
711+
if (pasid_pte_is_present(pte))
712+
return -EBUSY;
713+
714+
pasid_clear_entry(pte);
715+
716+
/* Sanity checking performed by caller to make sure address
717+
* width matching in two dimensions:
718+
* 1. CPU vs. IOMMU
719+
* 2. Guest vs. Host.
720+
*/
721+
switch (addr_width) {
722+
#ifdef CONFIG_X86
723+
case ADDR_WIDTH_5LEVEL:
724+
if (!cpu_feature_enabled(X86_FEATURE_LA57) ||
725+
!cap_5lp_support(iommu->cap)) {
726+
dev_err_ratelimited(dev,
727+
"5-level paging not supported\n");
728+
return -EINVAL;
729+
}
730+
731+
pasid_set_flpm(pte, 1);
732+
break;
733+
#endif
734+
case ADDR_WIDTH_4LEVEL:
735+
pasid_set_flpm(pte, 0);
736+
break;
737+
default:
738+
dev_err_ratelimited(dev, "Invalid guest address width %d\n",
739+
addr_width);
740+
return -EINVAL;
741+
}
742+
743+
/* First level PGD is in GPA, must be supported by the second level */
744+
if ((unsigned long long)gpgd > domain->max_addr) {
745+
dev_err_ratelimited(dev,
746+
"Guest PGD %llx not supported, max %llx\n",
747+
(unsigned long long)gpgd, domain->max_addr);
748+
return -EINVAL;
749+
}
750+
pasid_set_flptr(pte, (u64)gpgd);
751+
752+
ret = intel_pasid_setup_bind_data(iommu, pte, pasid_data);
753+
if (ret)
754+
return ret;
755+
756+
/* Setup the second level based on the given domain */
757+
pgd = domain->pgd;
758+
759+
agaw = iommu_skip_agaw(domain, iommu, &pgd);
760+
if (agaw < 0) {
761+
dev_err_ratelimited(dev, "Invalid domain page table\n");
762+
return -EINVAL;
763+
}
764+
pgd_val = virt_to_phys(pgd);
765+
pasid_set_slptr(pte, pgd_val);
766+
pasid_set_fault_enable(pte);
767+
768+
did = domain->iommu_did[iommu->seq_id];
769+
pasid_set_domain_id(pte, did);
770+
771+
pasid_set_address_width(pte, agaw);
772+
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
773+
774+
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED);
775+
pasid_set_present(pte);
776+
pasid_flush_caches(iommu, pte, pasid, did);
777+
778+
return ret;
779+
}

drivers/iommu/intel-pasid.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* to vmalloc or even module mappings.
3737
*/
3838
#define PASID_FLAG_SUPERVISOR_MODE BIT(0)
39+
#define PASID_FLAG_NESTED BIT(1)
3940

4041
/*
4142
* The PASID_FLAG_FL5LP flag Indicates using 5-level paging for first-
@@ -51,6 +52,11 @@ struct pasid_entry {
5152
u64 val[8];
5253
};
5354

55+
#define PASID_ENTRY_PGTT_FL_ONLY (1)
56+
#define PASID_ENTRY_PGTT_SL_ONLY (2)
57+
#define PASID_ENTRY_PGTT_NESTED (3)
58+
#define PASID_ENTRY_PGTT_PT (4)
59+
5460
/* The representative of a PASID table */
5561
struct pasid_table {
5662
void *table; /* pasid table pointer */
@@ -99,6 +105,10 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,
99105
int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
100106
struct dmar_domain *domain,
101107
struct device *dev, int pasid);
108+
int intel_pasid_setup_nested(struct intel_iommu *iommu,
109+
struct device *dev, pgd_t *pgd, int pasid,
110+
struct iommu_gpasid_bind_data_vtd *pasid_data,
111+
struct dmar_domain *domain, int addr_width);
102112
void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
103113
struct device *dev, int pasid);
104114

include/linux/intel-iommu.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#define DMA_FL_PTE_PRESENT BIT_ULL(0)
4343
#define DMA_FL_PTE_XD BIT_ULL(63)
4444

45+
#define ADDR_WIDTH_5LEVEL (57)
46+
#define ADDR_WIDTH_4LEVEL (48)
47+
4548
#define CONTEXT_TT_MULTI_LEVEL 0
4649
#define CONTEXT_TT_DEV_IOTLB 1
4750
#define CONTEXT_TT_PASS_THROUGH 2
@@ -480,6 +483,23 @@ struct context_entry {
480483
u64 hi;
481484
};
482485

486+
/* si_domain contains mulitple devices */
487+
#define DOMAIN_FLAG_STATIC_IDENTITY BIT(0)
488+
489+
/*
490+
* When VT-d works in the scalable mode, it allows DMA translation to
491+
* happen through either first level or second level page table. This
492+
* bit marks that the DMA translation for the domain goes through the
493+
* first level page table, otherwise, it goes through the second level.
494+
*/
495+
#define DOMAIN_FLAG_USE_FIRST_LEVEL BIT(1)
496+
497+
/*
498+
* Domain represents a virtual machine which demands iommu nested
499+
* translation mode support.
500+
*/
501+
#define DOMAIN_FLAG_NESTING_MODE BIT(2)
502+
483503
struct dmar_domain {
484504
int nid; /* node id */
485505

include/uapi/linux/iommu.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ struct iommu_gpasid_bind_data_vtd {
285285
__u32 emt;
286286
};
287287

288+
#define IOMMU_SVA_VTD_GPASID_MTS_MASK (IOMMU_SVA_VTD_GPASID_CD | \
289+
IOMMU_SVA_VTD_GPASID_EMTE | \
290+
IOMMU_SVA_VTD_GPASID_PCD | \
291+
IOMMU_SVA_VTD_GPASID_PWT)
292+
288293
/**
289294
* struct iommu_gpasid_bind_data - Information about device and guest PASID binding
290295
* @version: Version of this data structure

0 commit comments

Comments
 (0)