| 1 |
|
| 2 |
|
| 3 |
#include <commonlib/bsd/ipchksum.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <cpu/cpu.h> |
| 6 |
#include <cpu/x86/mtrr.h> |
| 7 |
#include <fsp/util.h> |
| 8 |
#include <intelbasecode/ramtop.h> |
| 9 |
#include <pc80/mc146818rtc.h> |
| 10 |
#include <stdint.h> |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#define RAMTOP_SIGNATURE 0x504F5452 |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#if CONFIG(USE_OPTION_TABLE) |
| 22 |
#include "option_table.h" |
| 23 |
|
| 24 |
#ifndef CMOS_VSTART_ramtop |
| 25 |
#error "The `ramtop` CMOS entry is missing, please add it to your cmos.layout." |
| 26 |
#endif |
| 27 |
|
| 28 |
#if CMOS_VSTART_ramtop % 8 != 0 |
| 29 |
#error "The `ramtop` CMOS entry needs to be byte aligned, check your cmos.layout." |
| 30 |
#endif |
| 31 |
|
| 32 |
#if CMOS_VLEN_ramtop != (10 * 8) |
| 33 |
#error "The `ramtop` CMOS entry needs to be 10 bytes long, check your cmos.layout." |
| 34 |
#endif |
| 35 |
|
| 36 |
#else |
| 37 |
#define CMOS_VSTART_ramtop 800 |
| 38 |
#endif |
| 39 |
|
| 40 |
struct ramtop_table { |
| 41 |
uint32_t signature; |
| 42 |
uint32_t addr; |
| 43 |
size_t size; |
| 44 |
uint16_t checksum; |
| 45 |
} __packed; |
| 46 |
|
| 47 |
|
| 48 |
static int ramtop_cmos_read(struct ramtop_table *ramtop) |
| 49 |
{ |
| 50 |
u8 i, *p; |
| 51 |
u16 csum; |
| 52 |
|
| 53 |
for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++) |
| 54 |
*p = cmos_read((CMOS_VSTART_ramtop / 8) + i); |
| 55 |
|
| 56 |
|
| 57 |
if (ramtop->signature != RAMTOP_SIGNATURE) { |
| 58 |
printk(BIOS_DEBUG, "ramtop_table invalid signature\n"); |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
if (ramtop->size == 0) { |
| 64 |
printk(BIOS_DEBUG, "ramtop_table holds invalid size\n"); |
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
csum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum)); |
| 70 |
|
| 71 |
if (csum != ramtop->checksum) { |
| 72 |
printk(BIOS_DEBUG, "ramtop_table checksum mismatch\n"); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
static void ramtop_cmos_write(struct ramtop_table *ramtop) |
| 81 |
{ |
| 82 |
u8 i, *p; |
| 83 |
|
| 84 |
|
| 85 |
ramtop->checksum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum)); |
| 86 |
|
| 87 |
for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++) |
| 88 |
cmos_write(*p, (CMOS_VSTART_ramtop / 8) + i); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static size_t calculate_ramtop_size(uint32_t addr) |
| 110 |
{ |
| 111 |
struct range_entry fsp_mem; |
| 112 |
uint32_t fsp_reserve_base; |
| 113 |
fsp_find_reserved_memory(&fsp_mem); |
| 114 |
|
| 115 |
fsp_reserve_base = range_entry_base(&fsp_mem); |
| 116 |
size_t ramtop_size = ALIGN_UP(addr - fsp_reserve_base, 4 * MiB); |
| 117 |
|
| 118 |
return ramtop_size; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
void update_ramtop(uint32_t addr) |
| 123 |
{ |
| 124 |
struct ramtop_table ramtop; |
| 125 |
|
| 126 |
|
| 127 |
if (ramtop_cmos_read(&ramtop) < 0) { |
| 128 |
|
| 129 |
ramtop.signature = RAMTOP_SIGNATURE; |
| 130 |
ramtop.addr = 0; |
| 131 |
ramtop.size = 0; |
| 132 |
} |
| 133 |
|
| 134 |
size_t size = calculate_ramtop_size(addr); |
| 135 |
|
| 136 |
|
| 137 |
if ((ramtop.addr == addr) && (ramtop.size == size)) |
| 138 |
return; |
| 139 |
|
| 140 |
ramtop.addr = addr; |
| 141 |
ramtop.size = size; |
| 142 |
|
| 143 |
|
| 144 |
ramtop_cmos_write(&ramtop); |
| 145 |
|
| 146 |
printk(BIOS_DEBUG, "Updated the RAMTOP address (0x%x) with size (0x%zx) into CMOS\n", |
| 147 |
ramtop.addr, ramtop.size); |
| 148 |
} |
| 149 |
|
| 150 |
uint32_t get_ramtop_addr(void) |
| 151 |
{ |
| 152 |
struct ramtop_table ramtop; |
| 153 |
|
| 154 |
if (ramtop_cmos_read(&ramtop) < 0) |
| 155 |
return 0; |
| 156 |
|
| 157 |
return ramtop.addr; |
| 158 |
} |
| 159 |
|
| 160 |
static uint32_t get_ramtop_size(void) |
| 161 |
{ |
| 162 |
struct ramtop_table ramtop; |
| 163 |
|
| 164 |
if (ramtop_cmos_read(&ramtop) < 0) |
| 165 |
return 0; |
| 166 |
|
| 167 |
return ramtop.size; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
void early_ramtop_enable_cache_range(void) |
| 172 |
{ |
| 173 |
uint32_t ramtop = get_ramtop_addr(); |
| 174 |
if (!ramtop) |
| 175 |
return; |
| 176 |
|
| 177 |
int mtrr = get_free_var_mtrr(); |
| 178 |
if (mtrr == -1) { |
| 179 |
printk(BIOS_WARNING, "ramtop_table update failure due to no free MTRR available!\n"); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
size_t ramtop_size = get_ramtop_size(); |
| 184 |
if (!ramtop_size) |
| 185 |
return; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
set_var_mtrr(mtrr, ramtop - ramtop_size, ramtop_size, MTRR_TYPE_WRCOMB); |
| 199 |
} |