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