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