Angel Pons | 986d50e | 2020-04-02 23:48:53 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 2 | |
Kyösti Mälkki | 7336f97 | 2020-06-08 06:05:03 +0300 | [diff] [blame] | 3 | #if ENV_X86 |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 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 |
| 12 | |
| 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> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 22 | #include <acpi/acpi.h> |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 23 | |
| 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) |
| 28 | { |
| 29 | const struct range_entry *r; |
| 30 | |
| 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; |
| 35 | |
| 36 | if (ALIGN_UP(range_entry_base(r) + size, align) + size > |
| 37 | range_entry_end(r)) |
| 38 | continue; |
| 39 | |
| 40 | return ALIGN_UP(range_entry_base(r) + size, align); |
| 41 | } |
| 42 | printk(BIOS_ERR, "%s: Couldn't find free memory range\n", __func__); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /* |
| 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. |
| 51 | * |
| 52 | * @return 0 on success, 1 on error |
| 53 | */ |
| 54 | static void clear_memory(void *unused) |
| 55 | { |
| 56 | const struct range_entry *r; |
| 57 | struct memranges mem; |
| 58 | uintptr_t pgtbl, vmem_addr; |
| 59 | |
| 60 | if (acpi_is_wakeup_s3()) |
| 61 | return; |
| 62 | |
| 63 | if (!security_clear_dram_request()) |
| 64 | return; |
| 65 | |
| 66 | /* FSP1.0 is marked as MMIO and won't appear here */ |
| 67 | |
| 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); |
| 75 | |
| 76 | /* Add reserved entries */ |
| 77 | void *baseptr = NULL; |
| 78 | size_t size = 0; |
| 79 | |
Kyösti Mälkki | 18a8ba4 | 2020-07-02 21:48:38 +0300 | [diff] [blame] | 80 | /* Only skip CBMEM, stage program, stack and heap are included there. */ |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 81 | |
| 82 | cbmem_get_region(&baseptr, &size); |
| 83 | memranges_insert(&mem, (uintptr_t)baseptr, size, BM_MEM_TABLE); |
| 84 | |
Kyösti Mälkki | 7336f97 | 2020-06-08 06:05:03 +0300 | [diff] [blame] | 85 | if (ENV_X86) { |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 86 | /* Find space for PAE enabled memset */ |
| 87 | pgtbl = get_free_memory_range(&mem, MEMSET_PAE_PGTL_ALIGN, |
| 88 | MEMSET_PAE_PGTL_SIZE); |
| 89 | |
| 90 | /* Don't touch page tables while clearing */ |
| 91 | memranges_insert(&mem, pgtbl, MEMSET_PAE_PGTL_SIZE, |
| 92 | BM_MEM_TABLE); |
| 93 | |
| 94 | vmem_addr = get_free_memory_range(&mem, MEMSET_PAE_VMEM_ALIGN, |
| 95 | MEMSET_PAE_PGTL_SIZE); |
| 96 | |
| 97 | printk(BIOS_SPEW, "%s: pgtbl at %p, virt memory at %p\n", |
| 98 | __func__, (void *)pgtbl, (void *)vmem_addr); |
| 99 | } |
| 100 | |
| 101 | /* Now clear all useable 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)); |
| 107 | |
| 108 | /* Does regular memset work? */ |
Patrick Rudolph | 0e6e457 | 2019-09-28 17:53:24 +0200 | [diff] [blame] | 109 | if (sizeof(resource_t) == sizeof(void *) || |
| 110 | !(range_entry_end(r) >> (sizeof(void *) * 8))) { |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 111 | /* fastpath */ |
| 112 | memset((void *)(uintptr_t)range_entry_base(r), 0, |
| 113 | range_entry_size(r)); |
| 114 | } |
| 115 | /* Use PAE if available */ |
Kyösti Mälkki | 7336f97 | 2020-06-08 06:05:03 +0300 | [diff] [blame] | 116 | else if (ENV_X86) { |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 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__); |
| 125 | } |
| 126 | } |
| 127 | |
Kyösti Mälkki | 7336f97 | 2020-06-08 06:05:03 +0300 | [diff] [blame] | 128 | if (ENV_X86) { |
Patrick Rudolph | a19b07f | 2019-02-21 12:11:14 +0100 | [diff] [blame] | 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); |
| 132 | |
| 133 | memset((void *)pgtbl, 0, MEMSET_PAE_PGTL_SIZE); |
| 134 | } |
| 135 | |
| 136 | memranges_teardown(&mem); |
| 137 | } |
| 138 | |
| 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); |