soc/intel: Remove blank lines before '}' and after '{'
[coreboot2.git] / src / security / memory / memory_clear.c
blob03c6f8bd75a94549d4de7c25c61a1e02aef8732f
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #if ENV_X86
4 #include <cpu/x86/pae.h>
5 #else
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
11 #endif
13 #include <memrange.h>
14 #include <bootmem.h>
15 #include <bootstate.h>
16 #include <symbols.h>
17 #include <console/console.h>
18 #include <arch/memory_clear.h>
19 #include <string.h>
20 #include <security/memory/memory.h>
21 #include <cbmem.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)
34 continue;
36 if (ALIGN_UP(range_entry_base(r) + size, align) + size >
37 range_entry_end(r))
38 continue;
40 return ALIGN_UP(range_entry_base(r) + size, align);
42 printk(BIOS_ERR, "%s: Couldn't find free memory range\n", __func__);
44 return 0;
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;
57 struct memranges mem;
58 uintptr_t pgtbl, vmem_addr;
60 if (acpi_is_wakeup_s3())
61 return;
63 if (!security_clear_dram_request())
64 return;
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 |
70 IORESOURCE_CACHEABLE,
71 IORESOURCE_MEM | IORESOURCE_FIXED |
72 IORESOURCE_STORED | IORESOURCE_ASSIGNED |
73 IORESOURCE_CACHEABLE,
74 BM_MEM_RAM);
76 /* Add reserved entries */
77 void *baseptr = NULL;
78 size_t size = 0;
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);
85 if (ENV_X86) {
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,
92 BM_MEM_TABLE);
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)
104 continue;
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))) {
111 /* fastpath */
112 memset((void *)(uintptr_t)range_entry_base(r), 0,
113 range_entry_size(r));
115 /* Use PAE if available */
116 else if (ENV_X86) {
117 if (memset_pae(range_entry_base(r), 0,
118 range_entry_size(r), (void *)pgtbl,
119 (void *)vmem_addr))
120 printk(BIOS_ERR, "%s: Failed to memset "
121 "memory\n", __func__);
122 } else {
123 printk(BIOS_ERR, "%s: Failed to memset memory\n",
124 __func__);
128 if (ENV_X86) {
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);