@@ -1949,90 +1949,109 @@ int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid)
1949
1949
return ret ;
1950
1950
}
1951
1951
1952
+ static void make_clear_dte (struct iommu_dev_data * dev_data , struct dev_table_entry * ptr ,
1953
+ struct dev_table_entry * new )
1954
+ {
1955
+ /* All existing DTE must have V bit set */
1956
+ new -> data128 [0 ] = DTE_FLAG_V ;
1957
+ new -> data128 [1 ] = 0 ;
1958
+ }
1959
+
1960
+ /*
1961
+ * Note:
1962
+ * The old value for GCR3 table and GPT have been cleared from caller.
1963
+ */
1964
+ static void set_dte_gcr3_table (struct amd_iommu * iommu ,
1965
+ struct iommu_dev_data * dev_data ,
1966
+ struct dev_table_entry * target )
1967
+ {
1968
+ struct gcr3_tbl_info * gcr3_info = & dev_data -> gcr3_info ;
1969
+ u64 gcr3 ;
1970
+
1971
+ if (!gcr3_info -> gcr3_tbl )
1972
+ return ;
1973
+
1974
+ pr_debug ("%s: devid=%#x, glx=%#x, gcr3_tbl=%#llx\n" ,
1975
+ __func__ , dev_data -> devid , gcr3_info -> glx ,
1976
+ (unsigned long long )gcr3_info -> gcr3_tbl );
1977
+
1978
+ gcr3 = iommu_virt_to_phys (gcr3_info -> gcr3_tbl );
1979
+
1980
+ target -> data [0 ] |= DTE_FLAG_GV |
1981
+ FIELD_PREP (DTE_GLX , gcr3_info -> glx ) |
1982
+ FIELD_PREP (DTE_GCR3_14_12 , gcr3 >> 12 );
1983
+ if (pdom_is_v2_pgtbl_mode (dev_data -> domain ))
1984
+ target -> data [0 ] |= DTE_FLAG_GIOV ;
1985
+
1986
+ target -> data [1 ] |= FIELD_PREP (DTE_GCR3_30_15 , gcr3 >> 15 ) |
1987
+ FIELD_PREP (DTE_GCR3_51_31 , gcr3 >> 31 );
1988
+
1989
+ /* Guest page table can only support 4 and 5 levels */
1990
+ if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL )
1991
+ target -> data [2 ] |= FIELD_PREP (DTE_GPT_LEVEL_MASK , GUEST_PGTABLE_5_LEVEL );
1992
+ else
1993
+ target -> data [2 ] |= FIELD_PREP (DTE_GPT_LEVEL_MASK , GUEST_PGTABLE_4_LEVEL );
1994
+ }
1995
+
1952
1996
static void set_dte_entry (struct amd_iommu * iommu ,
1953
1997
struct iommu_dev_data * dev_data )
1954
1998
{
1955
- u64 pte_root = 0 ;
1956
- u64 flags = 0 ;
1957
- u32 old_domid ;
1958
- u16 devid = dev_data -> devid ;
1959
1999
u16 domid ;
2000
+ u32 old_domid ;
2001
+ struct dev_table_entry * initial_dte ;
2002
+ struct dev_table_entry new = {};
1960
2003
struct protection_domain * domain = dev_data -> domain ;
1961
- struct dev_table_entry * dev_table = get_dev_table (iommu );
1962
2004
struct gcr3_tbl_info * gcr3_info = & dev_data -> gcr3_info ;
2005
+ struct dev_table_entry * dte = & get_dev_table (iommu )[dev_data -> devid ];
1963
2006
1964
2007
if (gcr3_info && gcr3_info -> gcr3_tbl )
1965
2008
domid = dev_data -> gcr3_info .domid ;
1966
2009
else
1967
2010
domid = domain -> id ;
1968
2011
2012
+ make_clear_dte (dev_data , dte , & new );
2013
+
1969
2014
if (domain -> iop .mode != PAGE_MODE_NONE )
1970
- pte_root = iommu_virt_to_phys (domain -> iop .root );
2015
+ new . data [ 0 ] = iommu_virt_to_phys (domain -> iop .root );
1971
2016
1972
- pte_root |= (domain -> iop .mode & DEV_ENTRY_MODE_MASK )
2017
+ new . data [ 0 ] |= (domain -> iop .mode & DEV_ENTRY_MODE_MASK )
1973
2018
<< DEV_ENTRY_MODE_SHIFT ;
1974
2019
1975
- pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V ;
2020
+ new . data [ 0 ] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V ;
1976
2021
1977
2022
/*
1978
- * When SNP is enabled, Only set TV bit when IOMMU
1979
- * page translation is in use.
2023
+ * When SNP is enabled, we can only support TV=1 with non-zero domain ID.
2024
+ * This is prevented by the SNP-enable and IOMMU_DOMAIN_IDENTITY check in
2025
+ * do_iommu_domain_alloc().
1980
2026
*/
1981
- if (!amd_iommu_snp_en || (domid != 0 ))
1982
- pte_root |= DTE_FLAG_TV ;
1983
-
1984
- flags = dev_table [devid ].data [1 ];
1985
-
1986
- if (dev_data -> ats_enabled )
1987
- flags |= DTE_FLAG_IOTLB ;
2027
+ WARN_ON (amd_iommu_snp_en && (domid == 0 ));
2028
+ new .data [0 ] |= DTE_FLAG_TV ;
1988
2029
1989
2030
if (dev_data -> ppr )
1990
- pte_root |= 1ULL << DEV_ENTRY_PPR ;
2031
+ new . data [ 0 ] |= 1ULL << DEV_ENTRY_PPR ;
1991
2032
1992
2033
if (domain -> dirty_tracking )
1993
- pte_root |= DTE_FLAG_HAD ;
1994
-
1995
- if (gcr3_info && gcr3_info -> gcr3_tbl ) {
1996
- u64 gcr3 = iommu_virt_to_phys (gcr3_info -> gcr3_tbl );
1997
- u64 glx = gcr3_info -> glx ;
1998
- u64 tmp ;
1999
-
2000
- pte_root |= DTE_FLAG_GV ;
2001
- pte_root |= (glx & DTE_GLX_MASK ) << DTE_GLX_SHIFT ;
2034
+ new .data [0 ] |= DTE_FLAG_HAD ;
2002
2035
2003
- /* First mask out possible old values for GCR3 table */
2004
- tmp = DTE_GCR3_VAL_B (~0ULL ) << DTE_GCR3_SHIFT_B ;
2005
- flags &= ~tmp ;
2006
-
2007
- tmp = DTE_GCR3_VAL_C (~0ULL ) << DTE_GCR3_SHIFT_C ;
2008
- flags &= ~tmp ;
2009
-
2010
- /* Encode GCR3 table into DTE */
2011
- tmp = DTE_GCR3_VAL_A (gcr3 ) << DTE_GCR3_SHIFT_A ;
2012
- pte_root |= tmp ;
2013
-
2014
- tmp = DTE_GCR3_VAL_B (gcr3 ) << DTE_GCR3_SHIFT_B ;
2015
- flags |= tmp ;
2016
-
2017
- tmp = DTE_GCR3_VAL_C (gcr3 ) << DTE_GCR3_SHIFT_C ;
2018
- flags |= tmp ;
2036
+ if (dev_data -> ats_enabled )
2037
+ new .data [1 ] |= DTE_FLAG_IOTLB ;
2019
2038
2020
- if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL ) {
2021
- dev_table [devid ].data [2 ] |=
2022
- ((u64 )GUEST_PGTABLE_5_LEVEL << DTE_GPT_LEVEL_SHIFT );
2023
- }
2039
+ old_domid = READ_ONCE (dte -> data [1 ]) & DEV_DOMID_MASK ;
2040
+ new .data [1 ] |= domid ;
2024
2041
2025
- /* GIOV is supported with V2 page table mode only */
2026
- if (pdom_is_v2_pgtbl_mode (domain ))
2027
- pte_root |= DTE_FLAG_GIOV ;
2042
+ /*
2043
+ * Restore cached persistent DTE bits, which can be set by information
2044
+ * in IVRS table. See set_dev_entry_from_acpi().
2045
+ */
2046
+ initial_dte = amd_iommu_get_ivhd_dte_flags (iommu -> pci_seg -> id , dev_data -> devid );
2047
+ if (initial_dte ) {
2048
+ new .data128 [0 ] |= initial_dte -> data128 [0 ];
2049
+ new .data128 [1 ] |= initial_dte -> data128 [1 ];
2028
2050
}
2029
2051
2030
- flags &= ~DEV_DOMID_MASK ;
2031
- flags |= domid ;
2052
+ set_dte_gcr3_table (iommu , dev_data , & new );
2032
2053
2033
- old_domid = dev_table [devid ].data [1 ] & DEV_DOMID_MASK ;
2034
- dev_table [devid ].data [1 ] = flags ;
2035
- dev_table [devid ].data [0 ] = pte_root ;
2054
+ update_dte256 (iommu , dev_data , & new );
2036
2055
2037
2056
/*
2038
2057
* A kdump kernel might be replacing a domain ID that was copied from
0 commit comments