iommu/amd: Move GA log allocation to init
Subsystems (2)
Vasant Hegde
maintainerPATCH 1/1 iommu/amd: Move GA log allocation to init
Currently GA log buffer allocation is done at the time of enabling ga log. This mixes capability checking, resource allocation, and hardware enablement into a single late stage code path.
Move capability checking and buffer allocation to init path:
- check_vapic_support(): Checks AVIC support
- alloc_ga_log(): Rename iommu_init_ga_log() as alloc_ga_log(). It will allocate the GA log and GA log tail buffers for a given IOMMU. Remove redundant checks inside alloc_ga_log().
- enable_iommus_vapic() is simplified - it handles enabling GA log and GAM/SNPAVIC support for IOMMUs.
Finally update iommu_ga_log_enable() return value from int to bool as return value is used in boolean context only.
---
Base commit: adbd8a08208 from iommu/fixes branch.
-Vasant
drivers/iommu/amd/init.c | 74 +++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 32 deletions(-)
drivers/iommu/amd/init.c +42 -32
@@ -916,13 +916,13 @@ static void free_ga_log(struct amd_iommu *iommu) }#ifdef CONFIG_IRQ_REMAP -static int iommu_ga_log_enable (struct amd_iommu *iommu) +static bool iommu_ga_log_enable (struct amd_iommu *iommu) {u32 status, i;u64 entry;if (!iommu->ga_log) -return -EINVAL ; +return false; entry =iommu_virt_to_phys (iommu->ga_log) |GA_LOG_SIZE_512 ; memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
@@ -946,35 +946,31 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu) }if (WARN_ON (i >=MMIO_STATUS_TIMEOUT )) -return -EINVAL ; +return false; -return 0 ; +return true; } +#endif /* CONFIG_IRQ_REMAP */ -static int iommu_init_ga_log (struct amd_iommu *iommu) +static int alloc_ga_log (struct amd_iommu *iommu) { +#ifdef CONFIG_IRQ_REMAP int nid = iommu->dev ?dev_to_node (&iommu->dev->dev) :NUMA_NO_NODE ; -if (WARN_ON_ONCE (!AMD_IOMMU_GUEST_IR_VAPIC (amd_iommu_guest_ir))) -return -EINVAL ; - -if (iommu->ga_log && iommu->ga_log_tail) -return 0 ; - iommu->ga_log =iommu_alloc_pages_node_sz (nid,GFP_KERNEL ,GA_LOG_SIZE );if (!iommu->ga_log) - goto err_out; +return -ENOMEM ; iommu->ga_log_tail =iommu_alloc_pages_node_sz (nid,GFP_KERNEL ,8 ); -if (!iommu->ga_log_tail) - goto err_out; +if (!iommu->ga_log_tail) { +iommu_free_pages (iommu->ga_log); + iommu->ga_log =NULL ; +return -ENOMEM ; + } +#endif return 0 ; -err_out: -free_ga_log (iommu); -return -EINVAL ; } -#endif /* CONFIG_IRQ_REMAP */ static int __init alloc_cwwb_sem (struct amd_iommu *iommu) {
@@ -2152,6 +2148,27 @@ static void __init late_iommu_features_init(struct amd_iommu *iommu) } } +/* Must be called after SNP support check is complete (iommu_snp_enable()) */ +static bool check_vapic_support (void ) +{ +if (!AMD_IOMMU_GUEST_IR_VAPIC (amd_iommu_guest_ir)) +return false; + +if (!check_feature (FEATURE_GAM_VAPIC )) { + amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; +return false; + } + +if (amd_iommu_snp_en && + !FEATURE_SNPAVICSUP_GAM (amd_iommu_efr2)) { +pr_warn ("Force to disable Virtual APIC due to SNP\n" ); + amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; +return false; + } + +return true; +} +static int __init iommu_init_pci (struct amd_iommu *iommu) {int cap_ptr = iommu->cap_ptr;
@@ -2196,6 +2213,12 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)if (check_feature (FEATURE_PPR ) &&amd_iommu_alloc_ppr_log (iommu))return -ENOMEM ; +if (check_vapic_support ()) { + ret =alloc_ga_log (iommu); +if (ret) +return ret; + } +if (iommu->cap & (1UL <<IOMMU_CAP_NPCACHE )) {pr_info ("Using strict mode due to virtualization\n" );iommu_set_dma_strict ();
@@ -3010,22 +3033,9 @@ static void enable_iommus_vapic(void)if (!AMD_IOMMU_GUEST_IR_VAPIC (amd_iommu_guest_ir))return ; -if (!check_feature (FEATURE_GAM_VAPIC )) { - amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; -return ; - } - -if (amd_iommu_snp_en && - !FEATURE_SNPAVICSUP_GAM (amd_iommu_efr2)) { -pr_warn ("Force to disable Virtual APIC due to SNP\n" ); - amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; -return ; - } -/* Enabling GAM and SNPAVIC support */ for_each_iommu (iommu) { -if (iommu_init_ga_log (iommu) || -iommu_ga_log_enable (iommu)) +if (!iommu_ga_log_enable (iommu))return ;iommu_feature_enable (iommu,CONTROL_GAM_EN );
Signature · 1 line
2.31.1
Quoted 11 lines · Vasant Hegde
Currently GA log buffer allocation is done at the time of enabling ga log. This mixes capability checking, resource allocation, and hardware enablement into a single late stage code path.
Move capability checking and buffer allocation to init path:
-check_vapic_support(): Checks AVIC support-
alloc_ga_log(): Renameiommu_init_ga_log()asalloc_ga_log(). It will allocate the GA log and GA log tail buffers for a given IOMMU. Remove redundant checks insidealloc_ga_log().
Quoted 6 lines
-
enable_iommus_vapic()is simplified - it handles enabling GA log and GAM/SNPAVIC support for IOMMUs.Finally update
iommu_ga_log_enable()return value from int to bool as return value is used in boolean context only.
Quoted 146 lines
Signed-off-by Vasant Hegde <[email protected]>---
Base commit:
adbd8a08208from iommu/fixes branch.(quoted diff) +0
-1-Vasant drivers/iommu/amd/init.c | 74 +++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 32 deletions(-)drivers/iommu/amd/init.c +42
-32@@ -916,13 +916,13 @@ static void free_ga_log(struct amd_iommu *iommu) }#ifdef CONFIG_IRQ_REMAP -static int iommu_ga_log_enable (struct amd_iommu *iommu) +static bool iommu_ga_log_enable (struct amd_iommu *iommu) {u32 status, i;u64 entry;if (!iommu->ga_log) -return -EINVAL ; +return false; entry =iommu_virt_to_phys (iommu->ga_log) |GA_LOG_SIZE_512 ; memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,@@ -946,35 +946,31 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu) }if (WARN_ON (i >=MMIO_STATUS_TIMEOUT )) -return -EINVAL ; +return false; -return 0 ; +return true; } +#endif /* CONFIG_IRQ_REMAP */ -static int iommu_init_ga_log (struct amd_iommu *iommu) +static int alloc_ga_log (struct amd_iommu *iommu) { +#ifdef CONFIG_IRQ_REMAP int nid = iommu->dev ?dev_to_node (&iommu->dev->dev) :NUMA_NO_NODE ; -if (WARN_ON_ONCE (!AMD_IOMMU_GUEST_IR_VAPIC (amd_iommu_guest_ir))) -return -EINVAL ; - -if (iommu->ga_log && iommu->ga_log_tail) -return 0 ; - iommu->ga_log =iommu_alloc_pages_node_sz (nid,GFP_KERNEL ,GA_LOG_SIZE );if (!iommu->ga_log) - goto err_out; +return -ENOMEM ; iommu->ga_log_tail =iommu_alloc_pages_node_sz (nid,GFP_KERNEL ,8 ); -if (!iommu->ga_log_tail) - goto err_out; +if (!iommu->ga_log_tail) { +iommu_free_pages (iommu->ga_log); + iommu->ga_log =NULL ; +return -ENOMEM ; + } +#endif return 0 ; -err_out: -free_ga_log (iommu); -return -EINVAL ; } -#endif /* CONFIG_IRQ_REMAP */ static int __init alloc_cwwb_sem (struct amd_iommu *iommu) {@@ -2152,6 +2148,27 @@ static void __init late_iommu_features_init(struct amd_iommu *iommu) } } +/* Must be called after SNP support check is complete (iommu_snp_enable()) */ +static bool check_vapic_support (void ) +{ +if (!AMD_IOMMU_GUEST_IR_VAPIC (amd_iommu_guest_ir)) +return false; + +if (!check_feature (FEATURE_GAM_VAPIC )) { + amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; +return false; + } + +if (amd_iommu_snp_en && + !FEATURE_SNPAVICSUP_GAM (amd_iommu_efr2)) { +pr_warn ("Force to disable Virtual APIC due to SNP\n" ); + amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; +return false; + } + +return true; +} +static int __init iommu_init_pci (struct amd_iommu *iommu) {int cap_ptr = iommu->cap_ptr;@@ -2196,6 +2213,12 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)if (check_feature (FEATURE_PPR ) &&amd_iommu_alloc_ppr_log (iommu))return -ENOMEM ; +if (check_vapic_support ()) { + ret =alloc_ga_log (iommu); +if (ret) +return ret; + } +if (iommu->cap & (1UL <<IOMMU_CAP_NPCACHE )) {pr_info ("Using strict mode due to virtualization\n" );iommu_set_dma_strict ();@@ -3010,22 +3033,9 @@ static void enable_iommus_vapic(void)if (!AMD_IOMMU_GUEST_IR_VAPIC (amd_iommu_guest_ir))return ; -if (!check_feature (FEATURE_GAM_VAPIC )) { - amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; -return ; - } - -if (amd_iommu_snp_en && - !FEATURE_SNPAVICSUP_GAM (amd_iommu_efr2)) { -pr_warn ("Force to disable Virtual APIC due to SNP\n" ); - amd_iommu_guest_ir =AMD_IOMMU_GUEST_IR_LEGACY_GA ; -return ; - } -/* Enabling GAM and SNPAVIC support */ for_each_iommu (iommu) { -if (iommu_init_ga_log (iommu) || -iommu_ga_log_enable (iommu)) +if (!iommu_ga_log_enable (iommu))return ;iommu_feature_enable (iommu,CONTROL_GAM_EN );Signature · 1 line
2.31.1
Quoted 2 lines · Vasant Hegde
drivers/iommu/amd/init.c | 74 +++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 32 deletions(-)
Applied, thanks.