Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 1 | // Option rom scanning code. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 7 | |
| 8 | #include "bregs.h" // struct bregs |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 9 | #include "farptr.h" // FLATPTR_TO_SEG |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 10 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 11 | #include "util.h" // dprintf |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 12 | #include "pci.h" // foreachpci |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 13 | #include "pci_regs.h" // PCI_ROM_ADDRESS |
| 14 | #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 15 | #include "boot.h" // IPL |
Gerd Hoffmann | c4c9fae | 2009-12-18 12:16:04 +0100 | [diff] [blame] | 16 | #include "paravirt.h" // qemu_cfg_* |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 17 | |
| 18 | |
| 19 | /**************************************************************** |
| 20 | * Definitions |
| 21 | ****************************************************************/ |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 22 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 23 | struct rom_header { |
| 24 | u16 signature; |
| 25 | u8 size; |
| 26 | u8 initVector[4]; |
| 27 | u8 reserved[17]; |
| 28 | u16 pcioffset; |
| 29 | u16 pnpoffset; |
| 30 | } PACKED; |
| 31 | |
| 32 | struct pci_data { |
| 33 | u32 signature; |
| 34 | u16 vendor; |
| 35 | u16 device; |
| 36 | u16 vitaldata; |
| 37 | u16 dlen; |
| 38 | u8 drevision; |
| 39 | u8 class_lo; |
| 40 | u16 class_hi; |
| 41 | u16 ilen; |
| 42 | u16 irevision; |
| 43 | u8 type; |
| 44 | u8 indicator; |
| 45 | u16 reserved; |
| 46 | } PACKED; |
| 47 | |
| 48 | struct pnp_data { |
| 49 | u32 signature; |
| 50 | u8 revision; |
| 51 | u8 len; |
| 52 | u16 nextoffset; |
| 53 | u8 reserved_08; |
| 54 | u8 checksum; |
| 55 | u32 devid; |
| 56 | u16 manufacturer; |
| 57 | u16 productname; |
| 58 | u8 type_lo; |
| 59 | u16 type_hi; |
| 60 | u8 dev_flags; |
| 61 | u16 bcv; |
| 62 | u16 dv; |
| 63 | u16 bev; |
| 64 | u16 reserved_1c; |
| 65 | u16 staticresource; |
| 66 | } PACKED; |
| 67 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 68 | #define OPTION_ROM_SIGNATURE 0xaa55 |
| 69 | #define OPTION_ROM_ALIGN 2048 |
| 70 | #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0]) |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 71 | #define PCI_ROM_SIGNATURE 0x52494350 // PCIR |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 72 | #define PCIROM_CODETYPE_X86 0 |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 73 | |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 74 | // The end of the last deployed rom. |
Kevin O'Connor | 4d96edc | 2010-09-25 14:53:15 -0400 | [diff] [blame] | 75 | u32 RomEnd = BUILD_ROM_START; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /**************************************************************** |
| 79 | * Helper functions |
| 80 | ****************************************************************/ |
| 81 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 82 | // Execute a given option rom. |
| 83 | static void |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 84 | __callrom(struct rom_header *rom, u16 offset, u16 bdf) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 85 | { |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 86 | u16 seg = FLATPTR_TO_SEG(rom); |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 87 | dprintf(1, "Running option rom at %04x:%04x\n", seg, offset); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 88 | |
| 89 | struct bregs br; |
| 90 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 91 | br.flags = F_IF; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 92 | br.ax = bdf; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 93 | br.bx = 0xffff; |
| 94 | br.dx = 0xffff; |
| 95 | br.es = SEG_BIOS; |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 96 | br.di = get_pnp_offset(); |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 97 | br.code = SEGOFF(seg, offset); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 98 | start_preempt(); |
Kevin O'Connor | 6e5b4a4 | 2008-12-06 13:03:52 -0500 | [diff] [blame] | 99 | call16big(&br); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 100 | finish_preempt(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 101 | |
| 102 | debug_serial_setup(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 105 | // Execute a given option rom at the standard entry vector. |
| 106 | static void |
| 107 | callrom(struct rom_header *rom, u16 bdf) |
| 108 | { |
| 109 | __callrom(rom, OPTION_ROM_INITVECTOR, bdf); |
| 110 | } |
| 111 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 112 | // Execute a BCV option rom registered via add_bcv(). |
| 113 | void |
| 114 | call_bcv(u16 seg, u16 ip) |
| 115 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 116 | __callrom(MAKE_FLATPTR(seg, 0), ip, 0); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 119 | // Verify that an option rom looks valid |
| 120 | static int |
| 121 | is_valid_rom(struct rom_header *rom) |
| 122 | { |
Kevin O'Connor | c36273f | 2009-04-16 20:43:07 -0400 | [diff] [blame] | 123 | dprintf(6, "Checking rom %p (sig %x size %d)\n" |
| 124 | , rom, rom->signature, rom->size); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 125 | if (rom->signature != OPTION_ROM_SIGNATURE) |
| 126 | return 0; |
Kevin O'Connor | 674e460 | 2009-04-13 19:19:22 -0400 | [diff] [blame] | 127 | if (! rom->size) |
| 128 | return 0; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 129 | u32 len = rom->size * 512; |
Kevin O'Connor | 94fd47e | 2009-02-15 15:21:10 -0500 | [diff] [blame] | 130 | u8 sum = checksum(rom, len); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 131 | if (sum != 0) { |
| 132 | dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n" |
| 133 | , rom, len, sum); |
Kevin O'Connor | cc97564 | 2011-03-06 19:22:46 -0500 | [diff] [blame] | 134 | if (CONFIG_OPTIONROMS_CHECKSUM) |
| 135 | return 0; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 136 | } |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | // Check if a valid option rom has a pnp struct; return it if so. |
| 141 | static struct pnp_data * |
| 142 | get_pnp_rom(struct rom_header *rom) |
| 143 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 144 | struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 145 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 146 | return NULL; |
| 147 | return pnp; |
| 148 | } |
| 149 | |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 150 | // Check for multiple pnp option rom headers. |
| 151 | static struct pnp_data * |
| 152 | get_pnp_next(struct rom_header *rom, struct pnp_data *pnp) |
| 153 | { |
| 154 | if (! pnp->nextoffset) |
| 155 | return NULL; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 156 | pnp = (void*)((u8*)rom + pnp->nextoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 157 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 158 | return NULL; |
| 159 | return pnp; |
| 160 | } |
| 161 | |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 162 | // Check if a valid option rom has a pci struct; return it if so. |
| 163 | static struct pci_data * |
| 164 | get_pci_rom(struct rom_header *rom) |
| 165 | { |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 166 | struct pci_data *pd = (void*)((u32)rom + rom->pcioffset); |
| 167 | if (pd->signature != PCI_ROM_SIGNATURE) |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 168 | return NULL; |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 169 | return pd; |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Kevin O'Connor | e826465 | 2010-09-15 22:06:19 -0400 | [diff] [blame] | 172 | // Return start of code in 0xc0000-0xf0000 space. |
| 173 | static inline u32 _max_rom(void) { |
| 174 | extern u8 code32flat_start[], code32init_end[]; |
Kevin O'Connor | 4a446d7 | 2010-09-25 12:46:38 -0400 | [diff] [blame] | 175 | return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start; |
Kevin O'Connor | e826465 | 2010-09-15 22:06:19 -0400 | [diff] [blame] | 176 | } |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 177 | // Return the memory position up to which roms may be located. |
Kevin O'Connor | e826465 | 2010-09-15 22:06:19 -0400 | [diff] [blame] | 178 | static inline u32 max_rom(void) { |
| 179 | u32 end = _max_rom(); |
| 180 | return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end; |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 183 | // Copy a rom to its permanent location below 1MiB |
| 184 | static struct rom_header * |
| 185 | copy_rom(struct rom_header *rom) |
| 186 | { |
| 187 | u32 romsize = rom->size * 512; |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 188 | if (RomEnd + romsize > max_rom()) { |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 189 | // Option rom doesn't fit. |
Kevin O'Connor | cfdc13f | 2010-02-14 13:07:54 -0500 | [diff] [blame] | 190 | warn_noalloc(); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
Kevin O'Connor | c36273f | 2009-04-16 20:43:07 -0400 | [diff] [blame] | 193 | dprintf(4, "Copying option rom (size %d) from %p to %x\n" |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 194 | , romsize, rom, RomEnd); |
Kevin O'Connor | 3403696 | 2009-12-05 18:51:53 -0500 | [diff] [blame] | 195 | iomemcpy((void*)RomEnd, rom, romsize); |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 196 | return (void*)RomEnd; |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 199 | // Run rom init code and note rom size. |
| 200 | static int |
| 201 | init_optionrom(struct rom_header *rom, u16 bdf, int isvga) |
| 202 | { |
| 203 | if (! is_valid_rom(rom)) |
| 204 | return -1; |
| 205 | |
| 206 | if (isvga || get_pnp_rom(rom)) |
| 207 | // Only init vga and PnP roms here. |
| 208 | callrom(rom, bdf); |
| 209 | |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 210 | RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 215 | #define RS_PCIROM (1LL<<33) |
| 216 | |
| 217 | static void |
| 218 | setRomSource(u64 *sources, struct rom_header *rom, u64 source) |
| 219 | { |
| 220 | if (sources) |
| 221 | sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source; |
| 222 | } |
| 223 | |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 224 | static int |
| 225 | getRomPriority(u64 *sources, struct rom_header *rom, int instance) |
| 226 | { |
| 227 | u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN]; |
| 228 | if (!source) |
| 229 | return -1; |
| 230 | if (source & RS_PCIROM) |
| 231 | return bootprio_find_pci_rom(source, instance); |
| 232 | return bootprio_find_named_rom(romfile_name(source), instance); |
| 233 | } |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 234 | |
| 235 | /**************************************************************** |
| 236 | * Roms in CBFS |
| 237 | ****************************************************************/ |
| 238 | |
| 239 | // Check if an option rom is at a hardcoded location or in CBFS. |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 240 | static struct rom_header * |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 241 | lookup_hardcode(struct pci_device *pci) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 242 | { |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 243 | char fname[17]; |
| 244 | snprintf(fname, sizeof(fname), "pci%04x,%04x.rom" |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 245 | , pci->vendor, pci->device); |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 246 | int ret = romfile_copy(romfile_find(fname), (void*)RomEnd |
| 247 | , max_rom() - RomEnd); |
| 248 | if (ret <= 0) |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 249 | return NULL; |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 250 | return (void*)RomEnd; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 253 | // Run all roms in a given CBFS directory. |
| 254 | static void |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 255 | run_file_roms(const char *prefix, int isvga, u64 *sources) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 256 | { |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 257 | u32 file = 0; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 258 | for (;;) { |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 259 | file = romfile_findprefix(prefix, file); |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 260 | if (!file) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 261 | break; |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 262 | struct rom_header *rom = (void*)RomEnd; |
| 263 | int ret = romfile_copy(file, rom, max_rom() - RomEnd); |
| 264 | if (ret > 0) { |
| 265 | setRomSource(sources, rom, file); |
| 266 | init_optionrom(rom, 0, isvga); |
| 267 | } |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /**************************************************************** |
| 273 | * PCI roms |
| 274 | ****************************************************************/ |
| 275 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 276 | // Verify device is a vga device with legacy address decoding enabled. |
| 277 | static int |
| 278 | is_pci_vga(struct pci_device *pci) |
| 279 | { |
| 280 | if (pci->class != PCI_CLASS_DISPLAY_VGA) |
| 281 | return 0; |
| 282 | u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND); |
| 283 | if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY)) |
| 284 | return 0; |
| 285 | while (pci->parent) { |
| 286 | pci = pci->parent; |
| 287 | u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL); |
| 288 | if (!(ctrl & PCI_BRIDGE_CTL_VGA)) |
| 289 | return 0; |
| 290 | } |
| 291 | return 1; |
| 292 | } |
| 293 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 294 | // Map the option rom of a given PCI device. |
| 295 | static struct rom_header * |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 296 | map_pcirom(struct pci_device *pci) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 297 | { |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 298 | u16 bdf = pci->bdf; |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 299 | dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n" |
| 300 | , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)); |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 301 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 302 | if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) { |
| 303 | dprintf(6, "Skipping non-normal pci device (type=%x)\n" |
| 304 | , pci->header_type); |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
| 307 | |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 308 | u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
| 309 | pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE); |
| 310 | u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 311 | |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 312 | dprintf(6, "Option rom sizing returned %x %x\n", orig, sz); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 313 | orig &= ~PCI_ROM_ADDRESS_ENABLE; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 314 | if (!sz || sz == 0xffffffff) |
| 315 | goto fail; |
| 316 | |
Kevin O'Connor | 64cf2fb | 2009-04-18 17:01:02 -0400 | [diff] [blame] | 317 | if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) { |
| 318 | // Don't try to map to a pci addresses at its max, in the last |
| 319 | // 4MiB of ram, or the first 16MiB of ram. |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 320 | dprintf(6, "Preset rom address doesn't look valid\n"); |
| 321 | goto fail; |
| 322 | } |
| 323 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 324 | // Looks like a rom - enable it. |
| 325 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 326 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 327 | struct rom_header *rom = (void*)orig; |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 328 | for (;;) { |
Kevin O'Connor | 49cc72b | 2010-05-23 10:22:23 -0400 | [diff] [blame] | 329 | dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x" |
| 330 | " bdf=%02x:%02x.%x)\n" |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 331 | , rom, pci->vendor, pci->device |
Kevin O'Connor | 49cc72b | 2010-05-23 10:22:23 -0400 | [diff] [blame] | 332 | , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 333 | if (rom->signature != OPTION_ROM_SIGNATURE) { |
| 334 | dprintf(6, "No option rom signature (got %x)\n", rom->signature); |
| 335 | goto fail; |
| 336 | } |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 337 | struct pci_data *pd = get_pci_rom(rom); |
| 338 | if (! pd) { |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 339 | dprintf(6, "No valid pci signature found\n"); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 340 | goto fail; |
| 341 | } |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 342 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 343 | if (pd->vendor == pci->vendor && pd->device == pci->device |
| 344 | && pd->type == PCIROM_CODETYPE_X86) |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 345 | // A match |
| 346 | break; |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 347 | dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n" |
| 348 | , pd->vendor, pd->device, pd->type); |
| 349 | if (pd->indicator & 0x80) { |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 350 | dprintf(6, "No more images left\n"); |
| 351 | goto fail; |
| 352 | } |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 353 | rom = (void*)((u32)rom + pd->ilen * 512); |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 354 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 355 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 356 | rom = copy_rom(rom); |
| 357 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 358 | return rom; |
| 359 | fail: |
| 360 | // Not valid - restore original and exit. |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 361 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | // Attempt to map and initialize the option rom on a given PCI device. |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 366 | static int |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 367 | init_pcirom(struct pci_device *pci, int isvga, u64 *sources) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 368 | { |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 369 | u16 bdf = pci->bdf; |
Kevin O'Connor | 49cc72b | 2010-05-23 10:22:23 -0400 | [diff] [blame] | 370 | dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n" |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 371 | , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf) |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 372 | , pci->vendor, pci->device); |
| 373 | struct rom_header *rom = lookup_hardcode(pci); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 374 | if (! rom) |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 375 | rom = map_pcirom(pci); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 376 | if (! rom) |
| 377 | // No ROM present. |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 378 | return -1; |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 379 | setRomSource(sources, rom, RS_PCIROM | bdf); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 380 | return init_optionrom(rom, bdf, isvga); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
| 384 | /**************************************************************** |
| 385 | * Non-VGA option rom init |
| 386 | ****************************************************************/ |
| 387 | |
| 388 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 389 | optionrom_setup(void) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 390 | { |
| 391 | if (! CONFIG_OPTIONROMS) |
| 392 | return; |
| 393 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 394 | dprintf(1, "Scan for option roms\n"); |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 395 | u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN]; |
| 396 | memset(sources, 0, sizeof(sources)); |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 397 | u32 post_vga = RomEnd; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 398 | |
| 399 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 400 | // Option roms are already deployed on the system. |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 401 | u32 pos = RomEnd; |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 402 | while (pos < max_rom()) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 403 | int ret = init_optionrom((void*)pos, 0, 0); |
| 404 | if (ret) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 405 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 406 | else |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 407 | pos = RomEnd; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 408 | } |
| 409 | } else { |
| 410 | // Find and deploy PCI roms. |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 411 | struct pci_device *pci; |
| 412 | foreachpci(pci) { |
| 413 | u16 v = pci->class; |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 414 | if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA |
| 415 | || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE)) |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 416 | continue; |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 417 | init_pcirom(pci, 0, sources); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 418 | } |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 419 | |
| 420 | // Find and deploy CBFS roms not associated with a device. |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 421 | run_file_roms("genroms/", 0, sources); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // All option roms found and deployed - now build BEV/BCV vectors. |
| 425 | |
| 426 | u32 pos = post_vga; |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 427 | while (pos < RomEnd) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 428 | struct rom_header *rom = (void*)pos; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 429 | if (! is_valid_rom(rom)) { |
| 430 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 431 | continue; |
| 432 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 433 | pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 434 | struct pnp_data *pnp = get_pnp_rom(rom); |
| 435 | if (! pnp) { |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 436 | // Legacy rom. |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 437 | boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0 |
| 438 | , getRomPriority(sources, rom, 0)); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 439 | continue; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 440 | } |
| 441 | // PnP rom. |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 442 | if (pnp->bev) { |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 443 | // Can boot system - add to IPL list. |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 444 | boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname |
| 445 | , getRomPriority(sources, rom, 0)); |
| 446 | } else { |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 447 | // Check for BCV (there may be multiple). |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 448 | int instance = 0; |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 449 | while (pnp && pnp->bcv) { |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 450 | boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname |
| 451 | , getRomPriority(sources, rom, instance++)); |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 452 | pnp = get_pnp_next(rom, pnp); |
| 453 | } |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 454 | } |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 458 | |
| 459 | /**************************************************************** |
| 460 | * VGA init |
| 461 | ****************************************************************/ |
| 462 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 463 | // Call into vga code to turn on console. |
| 464 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 465 | vga_setup(void) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 466 | { |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 467 | if (! CONFIG_OPTIONROMS) |
| 468 | return; |
| 469 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 470 | dprintf(1, "Scan for VGA option rom\n"); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 471 | |
| 472 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 473 | // Option roms are already deployed on the system. |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 474 | init_optionrom((void*)BUILD_ROM_START, 0, 1); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 475 | } else { |
Kevin O'Connor | e826465 | 2010-09-15 22:06:19 -0400 | [diff] [blame] | 476 | // Clear option rom memory |
| 477 | memset((void*)RomEnd, 0, _max_rom() - RomEnd); |
| 478 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 479 | // Find and deploy PCI VGA rom. |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame^] | 480 | struct pci_device *pci; |
| 481 | foreachpci(pci) { |
| 482 | if (!is_pci_vga(pci)) |
| 483 | continue; |
| 484 | VGAbdf = pci->bdf; |
| 485 | init_pcirom(pci, 1, NULL); |
| 486 | break; |
| 487 | } |
Kevin O'Connor | 09880da | 2009-06-17 20:35:41 -0400 | [diff] [blame] | 488 | |
| 489 | // Find and deploy CBFS vga-style roms not associated with a device. |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 490 | run_file_roms("vgaroms/", 1, NULL); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 493 | if (RomEnd == BUILD_ROM_START) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 494 | // No VGA rom found |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 495 | RomEnd += OPTION_ROM_ALIGN; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 496 | return; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 497 | } |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 498 | |
Kevin O'Connor | afbed1b | 2010-06-28 07:34:53 -0400 | [diff] [blame] | 499 | enable_vga_console(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 500 | } |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 501 | |
| 502 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 503 | s3_resume_vga_init(void) |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 504 | { |
| 505 | if (!CONFIG_S3_RESUME_VGA_INIT) |
| 506 | return; |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 507 | struct rom_header *rom = (void*)BUILD_ROM_START; |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 508 | if (! is_valid_rom(rom)) |
| 509 | return; |
| 510 | callrom(rom, 0); |
| 511 | } |