1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <cpu/x86/pae.h>
6 #define memset_pae(a, b, c, d, e) 0
7 #define MEMSET_PAE_PGTL_ALIGN 0
8 #define MEMSET_PAE_PGTL_SIZE 0
9 #define MEMSET_PAE_PGTL_SIZE 0
10 #define MEMSET_PAE_VMEM_ALIGN 0
15 #include <bootstate.h>
17 #include <console/console.h>
18 #include <arch/memory_clear.h>
20 #include <security/memory/memory.h>
22 #include <acpi/acpi.h>
24 /* Helper to find free space for memset_pae. */
25 static uintptr_t get_free_memory_range(struct memranges
*mem
,
26 const resource_t align
,
27 const resource_t size
)
29 const struct range_entry
*r
;
31 /* Find a spot for virtual memory address */
32 memranges_each_entry(r
, mem
) {
33 if (range_entry_tag(r
) != BM_MEM_RAM
)
36 if (ALIGN_UP(range_entry_base(r
) + size
, align
) + size
>
40 return ALIGN_UP(range_entry_base(r
) + size
, align
);
42 printk(BIOS_ERR
, "%s: Couldn't find free memory range\n", __func__
);
48 * Clears all memory regions marked as BM_MEM_RAM.
49 * Uses memset_pae if the memory region can't be accessed by memset and
50 * architecture is x86.
52 * @return 0 on success, 1 on error
54 static void clear_memory(void *unused
)
56 const struct range_entry
*r
;
58 uintptr_t pgtbl
, vmem_addr
;
60 if (acpi_is_wakeup_s3())
63 if (!security_clear_dram_request())
66 /* FSP1.0 is marked as MMIO and won't appear here */
68 memranges_init(&mem
, IORESOURCE_MEM
| IORESOURCE_FIXED
|
69 IORESOURCE_STORED
| IORESOURCE_ASSIGNED
|
71 IORESOURCE_MEM
| IORESOURCE_FIXED
|
72 IORESOURCE_STORED
| IORESOURCE_ASSIGNED
|
76 /* Add reserved entries */
80 /* Only skip CBMEM, stage program, stack and heap are included there. */
82 cbmem_get_region(&baseptr
, &size
);
83 memranges_insert(&mem
, (uintptr_t)baseptr
, size
, BM_MEM_TABLE
);
86 /* Find space for PAE enabled memset */
87 pgtbl
= get_free_memory_range(&mem
, MEMSET_PAE_PGTL_ALIGN
,
88 MEMSET_PAE_PGTL_SIZE
);
90 /* Don't touch page tables while clearing */
91 memranges_insert(&mem
, pgtbl
, MEMSET_PAE_PGTL_SIZE
,
94 vmem_addr
= get_free_memory_range(&mem
, MEMSET_PAE_VMEM_ALIGN
,
95 MEMSET_PAE_PGTL_SIZE
);
97 printk(BIOS_SPEW
, "%s: pgtbl at %p, virt memory at %p\n",
98 __func__
, (void *)pgtbl
, (void *)vmem_addr
);
101 /* Now clear all usable DRAM */
102 memranges_each_entry(r
, &mem
) {
103 if (range_entry_tag(r
) != BM_MEM_RAM
)
105 printk(BIOS_DEBUG
, "%s: Clearing DRAM %016llx-%016llx\n",
106 __func__
, range_entry_base(r
), range_entry_end(r
));
108 /* Does regular memset work? */
109 if (sizeof(resource_t
) == sizeof(void *) ||
110 !(range_entry_end(r
) >> (sizeof(void *) * 8))) {
112 memset((void *)(uintptr_t)range_entry_base(r
), 0,
113 range_entry_size(r
));
115 /* Use PAE if available */
117 if (memset_pae(range_entry_base(r
), 0,
118 range_entry_size(r
), (void *)pgtbl
,
120 printk(BIOS_ERR
, "%s: Failed to memset "
121 "memory\n", __func__
);
123 printk(BIOS_ERR
, "%s: Failed to memset memory\n",
129 /* Clear previously skipped memory reserved for pagetables */
130 printk(BIOS_DEBUG
, "%s: Clearing DRAM %016lx-%016lx\n",
131 __func__
, pgtbl
, pgtbl
+ MEMSET_PAE_PGTL_SIZE
);
133 memset((void *)pgtbl
, 0, MEMSET_PAE_PGTL_SIZE
);
136 memranges_teardown(&mem
);
139 /* After DEV_INIT as MTRRs needs to be configured on x86 */
140 BOOT_STATE_INIT_ENTRY(BS_DEV_INIT
, BS_ON_EXIT
, clear_memory
, NULL
);