]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
iommu/amd: Add support to remap/unmap IOMMU buffers for kdump
authorAshish Kalra <ashish.kalra@amd.com>
Mon, 25 Aug 2025 21:46:01 +0000 (21:46 +0000)
committerJoerg Roedel <joerg.roedel@amd.com>
Fri, 5 Sep 2025 12:44:30 +0000 (14:44 +0200)
After a panic if SNP is enabled in the previous kernel then the kdump
kernel boots with IOMMU SNP enforcement still enabled.

IOMMU completion wait buffers (CWBs), command buffers and event buffer
registers remain locked and exclusive to the previous kernel. Attempts
to allocate and use new buffers in the kdump kernel fail, as hardware
ignores writes to the locked MMIO registers as per AMD IOMMU spec
Section 2.12.2.1.

This results in repeated "Completion-Wait loop timed out" errors and a
second kernel panic: "Kernel panic - not syncing: timer doesn't work
through Interrupt-remapped IO-APIC"

The list of MMIO registers locked and which ignore writes after failed
SNP shutdown are mentioned in the AMD IOMMU specifications below:

Section 2.12.2.1.
https://docs.amd.com/v/u/en-US/48882_3.10_PUB

Reuse the pages of the previous kernel for completion wait buffers,
command buffers, event buffers and memremap them during kdump boot
and essentially work with an already enabled IOMMU configuration and
re-using the previous kernel’s data structures.

Reusing of command buffers and event buffers is now done for kdump boot
irrespective of SNP being enabled during kdump.

Re-use of completion wait buffers is only done when SNP is enabled as
the exclusion base register is used for the completion wait buffer
(CWB) address only when SNP is enabled.

Reviewed-by: Vasant Hegde <vasant.hegde@amd.com>
Tested-by: Sairaj Kodilkar <sarunkod@amd.com>
Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
Link: https://lore.kernel.org/r/ff04b381a8fe774b175c23c1a336b28bc1396511.1756157913.git.ashish.kalra@amd.com
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
drivers/iommu/amd/amd_iommu_types.h
drivers/iommu/amd/init.c
drivers/iommu/amd/iommu.c

index 5219d7ddfdaa8bb6b6a2cd75018752639f5f1782..8a863cae99dbbca1c049bb6e9607e9c25ae6d8e1 100644 (file)
@@ -791,6 +791,11 @@ struct amd_iommu {
        u32 flags;
        volatile u64 *cmd_sem;
        atomic64_t cmd_sem_val;
+       /*
+        * Track physical address to directly use it in build_completion_wait()
+        * and avoid adding any special checks and handling for kdump.
+        */
+       u64 cmd_sem_paddr;
 
 #ifdef CONFIG_AMD_IOMMU_DEBUGFS
        /* DebugFS Info */
index 7b5af6176de99074e91e07825fcc55e3310e95f7..5140c820bda6f82cbc0c0434893f461c75eb4406 100644 (file)
@@ -710,6 +710,26 @@ static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)
        pci_seg->alias_table = NULL;
 }
 
+static inline void *iommu_memremap(unsigned long paddr, size_t size)
+{
+       phys_addr_t phys;
+
+       if (!paddr)
+               return NULL;
+
+       /*
+        * Obtain true physical address in kdump kernel when SME is enabled.
+        * Currently, previous kernel with SME enabled and kdump kernel
+        * with SME support disabled is not supported.
+        */
+       phys = __sme_clr(paddr);
+
+       if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
+               return (__force void *)ioremap_encrypted(phys, size);
+       else
+               return memremap(phys, size, MEMREMAP_WB);
+}
+
 /*
  * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  * write commands to that buffer later and the IOMMU will execute them
@@ -942,8 +962,91 @@ err_out:
 static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
 {
        iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL, 1);
+       if (!iommu->cmd_sem)
+               return -ENOMEM;
+       iommu->cmd_sem_paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
+       return 0;
+}
+
+static int __init remap_event_buffer(struct amd_iommu *iommu)
+{
+       u64 paddr;
+
+       pr_info_once("Re-using event buffer from the previous kernel\n");
+       paddr = readq(iommu->mmio_base + MMIO_EVT_BUF_OFFSET) & PM_ADDR_MASK;
+       iommu->evt_buf = iommu_memremap(paddr, EVT_BUFFER_SIZE);
+
+       return iommu->evt_buf ? 0 : -ENOMEM;
+}
+
+static int __init remap_command_buffer(struct amd_iommu *iommu)
+{
+       u64 paddr;
 
-       return iommu->cmd_sem ? 0 : -ENOMEM;
+       pr_info_once("Re-using command buffer from the previous kernel\n");
+       paddr = readq(iommu->mmio_base + MMIO_CMD_BUF_OFFSET) & PM_ADDR_MASK;
+       iommu->cmd_buf = iommu_memremap(paddr, CMD_BUFFER_SIZE);
+
+       return iommu->cmd_buf ? 0 : -ENOMEM;
+}
+
+static int __init remap_or_alloc_cwwb_sem(struct amd_iommu *iommu)
+{
+       u64 paddr;
+
+       if (check_feature(FEATURE_SNP)) {
+               /*
+                * When SNP is enabled, the exclusion base register is used for the
+                * completion wait buffer (CWB) address. Read and re-use it.
+                */
+               pr_info_once("Re-using CWB buffers from the previous kernel\n");
+               paddr = readq(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET) & PM_ADDR_MASK;
+               iommu->cmd_sem = iommu_memremap(paddr, PAGE_SIZE);
+               if (!iommu->cmd_sem)
+                       return -ENOMEM;
+               iommu->cmd_sem_paddr = paddr;
+       } else {
+               return alloc_cwwb_sem(iommu);
+       }
+
+       return 0;
+}
+
+static int __init alloc_iommu_buffers(struct amd_iommu *iommu)
+{
+       int ret;
+
+       /*
+        * Reuse/Remap the previous kernel's allocated completion wait
+        * command and event buffers for kdump boot.
+        */
+       if (is_kdump_kernel()) {
+               ret = remap_or_alloc_cwwb_sem(iommu);
+               if (ret)
+                       return ret;
+
+               ret = remap_command_buffer(iommu);
+               if (ret)
+                       return ret;
+
+               ret = remap_event_buffer(iommu);
+               if (ret)
+                       return ret;
+       } else {
+               ret = alloc_cwwb_sem(iommu);
+               if (ret)
+                       return ret;
+
+               ret = alloc_command_buffer(iommu);
+               if (ret)
+                       return ret;
+
+               ret = alloc_event_buffer(iommu);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
 }
 
 static void __init free_cwwb_sem(struct amd_iommu *iommu)
@@ -951,6 +1054,38 @@ static void __init free_cwwb_sem(struct amd_iommu *iommu)
        if (iommu->cmd_sem)
                iommu_free_pages((void *)iommu->cmd_sem);
 }
+static void __init unmap_cwwb_sem(struct amd_iommu *iommu)
+{
+       if (iommu->cmd_sem) {
+               if (check_feature(FEATURE_SNP))
+                       memunmap((void *)iommu->cmd_sem);
+               else
+                       iommu_free_pages((void *)iommu->cmd_sem);
+       }
+}
+
+static void __init unmap_command_buffer(struct amd_iommu *iommu)
+{
+       memunmap((void *)iommu->cmd_buf);
+}
+
+static void __init unmap_event_buffer(struct amd_iommu *iommu)
+{
+       memunmap(iommu->evt_buf);
+}
+
+static void __init free_iommu_buffers(struct amd_iommu *iommu)
+{
+       if (is_kdump_kernel()) {
+               unmap_cwwb_sem(iommu);
+               unmap_command_buffer(iommu);
+               unmap_event_buffer(iommu);
+       } else {
+               free_cwwb_sem(iommu);
+               free_command_buffer(iommu);
+               free_event_buffer(iommu);
+       }
+}
 
 static void iommu_enable_xt(struct amd_iommu *iommu)
 {
@@ -1655,9 +1790,7 @@ static void __init free_sysfs(struct amd_iommu *iommu)
 static void __init free_iommu_one(struct amd_iommu *iommu)
 {
        free_sysfs(iommu);
-       free_cwwb_sem(iommu);
-       free_command_buffer(iommu);
-       free_event_buffer(iommu);
+       free_iommu_buffers(iommu);
        amd_iommu_free_ppr_log(iommu);
        free_ga_log(iommu);
        iommu_unmap_mmio_space(iommu);
@@ -1821,14 +1954,9 @@ static int __init init_iommu_one_late(struct amd_iommu *iommu)
 {
        int ret;
 
-       if (alloc_cwwb_sem(iommu))
-               return -ENOMEM;
-
-       if (alloc_command_buffer(iommu))
-               return -ENOMEM;
-
-       if (alloc_event_buffer(iommu))
-               return -ENOMEM;
+       ret = alloc_iommu_buffers(iommu);
+       if (ret)
+               return ret;
 
        iommu->int_enabled = false;
 
index fc11a5dab8ecae604b123566b86efcdcd0619689..2e1865daa1cee87aedd759063a19f1f5f31951e6 100644 (file)
@@ -1196,7 +1196,7 @@ static void build_completion_wait(struct iommu_cmd *cmd,
                                  struct amd_iommu *iommu,
                                  u64 data)
 {
-       u64 paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
+       u64 paddr = iommu->cmd_sem_paddr;
 
        memset(cmd, 0, sizeof(*cmd));
        cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;