Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 1 | // Coreboot interface support. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 6 | |
| 7 | #include "memmap.h" // add_e820 |
| 8 | #include "util.h" // dprintf |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 9 | #include "pci.h" // struct pir_header |
| 10 | #include "acpi.h" // struct rsdp_descriptor |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 11 | #include "biosvar.h" // GET_EBDA |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 12 | |
| 13 | |
| 14 | /**************************************************************** |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 15 | * BIOS table copying |
| 16 | ****************************************************************/ |
| 17 | |
| 18 | static void |
| 19 | copy_pir(void *pos) |
| 20 | { |
| 21 | struct pir_header *p = pos; |
| 22 | if (p->signature != PIR_SIGNATURE) |
| 23 | return; |
Kevin O'Connor | 51358db | 2008-12-28 21:50:29 -0500 | [diff] [blame] | 24 | if (PirOffset) |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 25 | return; |
| 26 | if (p->size < sizeof(*p)) |
| 27 | return; |
| 28 | if (checksum(pos, p->size) != 0) |
| 29 | return; |
| 30 | bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16); |
| 31 | if (bios_table_cur_addr + p->size > bios_table_end_addr) { |
| 32 | dprintf(1, "No room to copy PIR table!\n"); |
| 33 | return; |
| 34 | } |
| 35 | dprintf(1, "Copying PIR from %p to %x\n", pos, bios_table_cur_addr); |
| 36 | memcpy((void*)bios_table_cur_addr, pos, p->size); |
Kevin O'Connor | 51358db | 2008-12-28 21:50:29 -0500 | [diff] [blame] | 37 | PirOffset = bios_table_cur_addr - BUILD_BIOS_ADDR; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 38 | bios_table_cur_addr += p->size; |
| 39 | } |
| 40 | |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 41 | static void |
| 42 | copy_mptable(void *pos) |
| 43 | { |
| 44 | struct mptable_floating_s *p = pos; |
| 45 | if (p->signature != MPTABLE_SIGNAURE) |
| 46 | return; |
| 47 | if (!p->physaddr) |
| 48 | return; |
| 49 | if (checksum(pos, sizeof(*p)) != 0) |
| 50 | return; |
| 51 | u32 length = p->length * 16; |
| 52 | bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16); |
| 53 | if (bios_table_cur_addr + length > bios_table_end_addr) { |
| 54 | dprintf(1, "No room to copy MPTABLE!\n"); |
| 55 | return; |
| 56 | } |
| 57 | dprintf(1, "Copying MPTABLE from %p to %x\n", pos, bios_table_cur_addr); |
| 58 | memcpy((void*)bios_table_cur_addr, pos, length); |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 59 | bios_table_cur_addr += length; |
| 60 | } |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 61 | |
| 62 | static void |
| 63 | copy_acpi_rsdp(void *pos) |
| 64 | { |
Kevin O'Connor | 9967ab7 | 2008-12-18 21:57:33 -0500 | [diff] [blame] | 65 | if (RsdpAddr) |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 66 | return; |
| 67 | struct rsdp_descriptor *p = pos; |
Kevin O'Connor | 9967ab7 | 2008-12-18 21:57:33 -0500 | [diff] [blame] | 68 | if (p->signature != RSDP_SIGNATURE) |
| 69 | return; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 70 | u32 length = 20; |
| 71 | if (checksum(pos, length) != 0) |
| 72 | return; |
| 73 | if (p->revision > 1) { |
| 74 | length = p->length; |
| 75 | if (checksum(pos, length) != 0) |
| 76 | return; |
| 77 | } |
| 78 | bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16); |
| 79 | if (bios_table_cur_addr + length > bios_table_end_addr) { |
| 80 | dprintf(1, "No room to copy ACPI RSDP table!\n"); |
| 81 | return; |
| 82 | } |
| 83 | dprintf(1, "Copying ACPI RSDP from %p to %x\n", pos, bios_table_cur_addr); |
Kevin O'Connor | 9967ab7 | 2008-12-18 21:57:33 -0500 | [diff] [blame] | 84 | RsdpAddr = (void*)bios_table_cur_addr; |
| 85 | memcpy(RsdpAddr, pos, length); |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 86 | bios_table_cur_addr += length; |
| 87 | } |
| 88 | |
| 89 | // Attempt to find (and relocate) any standard bios tables found in a |
| 90 | // given address range. |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 91 | static void |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 92 | scan_tables(u32 start, u32 size) |
| 93 | { |
| 94 | void *p = (void*)ALIGN(start, 16); |
| 95 | void *end = (void*)start + size; |
| 96 | for (; p<end; p += 16) { |
| 97 | copy_pir(p); |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 98 | copy_mptable(p); |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 99 | copy_acpi_rsdp(p); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /**************************************************************** |
| 105 | * Memory map |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 106 | ****************************************************************/ |
| 107 | |
| 108 | struct cb_header { |
| 109 | u32 signature; |
| 110 | u32 header_bytes; |
| 111 | u32 header_checksum; |
| 112 | u32 table_bytes; |
| 113 | u32 table_checksum; |
| 114 | u32 table_entries; |
| 115 | }; |
| 116 | |
| 117 | #define CB_SIGNATURE 0x4f49424C // "LBIO" |
| 118 | |
| 119 | struct cb_memory_range { |
| 120 | u64 start; |
| 121 | u64 size; |
| 122 | u32 type; |
| 123 | }; |
| 124 | |
| 125 | #define CB_MEM_TABLE 16 |
| 126 | |
| 127 | struct cb_memory { |
| 128 | u32 tag; |
| 129 | u32 size; |
| 130 | struct cb_memory_range map[0]; |
| 131 | }; |
| 132 | |
| 133 | #define CB_TAG_MEMORY 0x01 |
| 134 | |
| 135 | #define MEM_RANGE_COUNT(_rec) \ |
| 136 | (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0])) |
| 137 | |
| 138 | static u16 |
| 139 | ipchksum(char *buf, int count) |
| 140 | { |
| 141 | u16 *p = (u16*)buf; |
| 142 | u32 sum = 0; |
| 143 | while (count > 1) { |
| 144 | sum += *p++; |
| 145 | count -= 2; |
| 146 | } |
| 147 | if (count) |
| 148 | sum += *(u8*)p; |
| 149 | sum = (sum >> 16) + (sum & 0xffff); |
| 150 | sum += (sum >> 16); |
| 151 | return ~sum; |
| 152 | } |
| 153 | |
| 154 | // Try to locate the coreboot header in a given address range. |
| 155 | static struct cb_header * |
| 156 | find_cb_header(char *addr, int len) |
| 157 | { |
| 158 | char *end = addr + len; |
| 159 | for (; addr < end; addr += 16) { |
| 160 | struct cb_header *cbh = (struct cb_header *)addr; |
| 161 | if (cbh->signature != CB_SIGNATURE) |
| 162 | continue; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 163 | if (! cbh->table_bytes) |
| 164 | continue; |
| 165 | if (ipchksum(addr, sizeof(*cbh)) != 0) |
| 166 | continue; |
| 167 | if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes) |
| 168 | != cbh->table_checksum) |
| 169 | continue; |
| 170 | return cbh; |
| 171 | } |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | // Try to find the coreboot memory table in the given coreboot table. |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 176 | static void * |
| 177 | find_cb_subtable(struct cb_header *cbh, u32 tag) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 178 | { |
| 179 | char *tbl = (char *)cbh + sizeof(*cbh); |
| 180 | int i; |
| 181 | for (i=0; i<cbh->table_entries; i++) { |
| 182 | struct cb_memory *cbm = (struct cb_memory *)tbl; |
| 183 | tbl += cbm->size; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 184 | if (cbm->tag == tag) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 185 | return cbm; |
| 186 | } |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | // Populate max ram and e820 map info by scanning for a coreboot table. |
| 191 | void |
| 192 | coreboot_fill_map() |
| 193 | { |
| 194 | dprintf(3, "Attempting to find coreboot table\n"); |
Kevin O'Connor | ef3d882 | 2009-02-05 19:51:12 -0500 | [diff] [blame^] | 195 | |
| 196 | // Init variables set in coreboot table memory scan. |
| 197 | PirOffset = 0; |
| 198 | RsdpAddr = 0; |
| 199 | |
| 200 | // Find coreboot table. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 201 | struct cb_header *cbh = find_cb_header(0, 0x1000); |
| 202 | if (!cbh) |
| 203 | goto fail; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 204 | struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 205 | if (!cbm) |
| 206 | goto fail; |
| 207 | |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 208 | u64 maxram = 0, maxram_over4G = 0; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 209 | int i, count = MEM_RANGE_COUNT(cbm); |
| 210 | for (i=0; i<count; i++) { |
| 211 | struct cb_memory_range *m = &cbm->map[i]; |
| 212 | u32 type = m->type; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 213 | if (type == CB_MEM_TABLE) { |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 214 | type = E820_RESERVED; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 215 | scan_tables(m->start, m->size); |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 216 | } else if (type == E820_ACPI || type == E820_RAM) { |
| 217 | u64 end = m->start + m->size; |
| 218 | if (end > 0x100000000ull) { |
| 219 | end -= 0x100000000ull; |
| 220 | if (end > maxram_over4G) |
| 221 | maxram_over4G = end; |
| 222 | } else if (end > maxram) |
| 223 | maxram = end; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 224 | } |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 225 | add_e820(m->start, m->size, type); |
| 226 | } |
| 227 | |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 228 | RamSize = maxram; |
| 229 | RamSizeOver4G = maxram_over4G; |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 230 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 231 | // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this |
| 232 | // confuses grub. So, override it. |
| 233 | add_e820(0, 16*1024, E820_RAM); |
| 234 | |
Kevin O'Connor | 1d247db | 2008-08-29 21:19:53 -0400 | [diff] [blame] | 235 | // XXX - just create dummy smbios table for now - should detect if |
| 236 | // smbios/dmi table is found from coreboot and use that instead. |
| 237 | smbios_init(); |
| 238 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 239 | return; |
| 240 | |
| 241 | fail: |
| 242 | // No table found.. Use 16Megs as a dummy value. |
| 243 | dprintf(1, "Unable to find coreboot table!\n"); |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 244 | RamSize = 16*1024*1024; |
| 245 | RamSizeOver4G = 0; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 246 | add_e820(0, 16*1024*1024, E820_RAM); |
| 247 | return; |
| 248 | } |