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 | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 12 | #include "pci.h" // pci_find_class |
| 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 |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 16 | |
| 17 | |
| 18 | /**************************************************************** |
| 19 | * Definitions |
| 20 | ****************************************************************/ |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 21 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 22 | struct rom_header { |
| 23 | u16 signature; |
| 24 | u8 size; |
| 25 | u8 initVector[4]; |
| 26 | u8 reserved[17]; |
| 27 | u16 pcioffset; |
| 28 | u16 pnpoffset; |
| 29 | } PACKED; |
| 30 | |
| 31 | struct pci_data { |
| 32 | u32 signature; |
| 33 | u16 vendor; |
| 34 | u16 device; |
| 35 | u16 vitaldata; |
| 36 | u16 dlen; |
| 37 | u8 drevision; |
| 38 | u8 class_lo; |
| 39 | u16 class_hi; |
| 40 | u16 ilen; |
| 41 | u16 irevision; |
| 42 | u8 type; |
| 43 | u8 indicator; |
| 44 | u16 reserved; |
| 45 | } PACKED; |
| 46 | |
| 47 | struct pnp_data { |
| 48 | u32 signature; |
| 49 | u8 revision; |
| 50 | u8 len; |
| 51 | u16 nextoffset; |
| 52 | u8 reserved_08; |
| 53 | u8 checksum; |
| 54 | u32 devid; |
| 55 | u16 manufacturer; |
| 56 | u16 productname; |
| 57 | u8 type_lo; |
| 58 | u16 type_hi; |
| 59 | u8 dev_flags; |
| 60 | u16 bcv; |
| 61 | u16 dv; |
| 62 | u16 bev; |
| 63 | u16 reserved_1c; |
| 64 | u16 staticresource; |
| 65 | } PACKED; |
| 66 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 67 | #define OPTION_ROM_START 0xc0000 |
| 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 | |
| 74 | // Next available position for an option rom. |
| 75 | static u32 next_rom; |
| 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 | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 91 | br.ax = bdf; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 92 | br.bx = 0xffff; |
| 93 | br.dx = 0xffff; |
| 94 | br.es = SEG_BIOS; |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 95 | br.di = get_pnp_offset(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 96 | br.cs = seg; |
| 97 | br.ip = offset; |
Kevin O'Connor | 6e5b4a4 | 2008-12-06 13:03:52 -0500 | [diff] [blame] | 98 | call16big(&br); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 99 | |
| 100 | debug_serial_setup(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 103 | // Execute a given option rom at the standard entry vector. |
| 104 | static void |
| 105 | callrom(struct rom_header *rom, u16 bdf) |
| 106 | { |
| 107 | __callrom(rom, OPTION_ROM_INITVECTOR, bdf); |
| 108 | } |
| 109 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 110 | // Execute a BCV option rom registered via add_bcv(). |
| 111 | void |
| 112 | call_bcv(u16 seg, u16 ip) |
| 113 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 114 | __callrom(MAKE_FLATPTR(seg, 0), ip, 0); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 117 | // Verify that an option rom looks valid |
| 118 | static int |
| 119 | is_valid_rom(struct rom_header *rom) |
| 120 | { |
Kevin O'Connor | c36273f | 2009-04-16 20:43:07 -0400 | [diff] [blame] | 121 | dprintf(6, "Checking rom %p (sig %x size %d)\n" |
| 122 | , rom, rom->signature, rom->size); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 123 | if (rom->signature != OPTION_ROM_SIGNATURE) |
| 124 | return 0; |
Kevin O'Connor | 674e460 | 2009-04-13 19:19:22 -0400 | [diff] [blame] | 125 | if (! rom->size) |
| 126 | return 0; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 127 | u32 len = rom->size * 512; |
Kevin O'Connor | 94fd47e | 2009-02-15 15:21:10 -0500 | [diff] [blame] | 128 | u8 sum = checksum(rom, len); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 129 | if (sum != 0) { |
| 130 | dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n" |
| 131 | , rom, len, sum); |
| 132 | return 0; |
| 133 | } |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | // Check if a valid option rom has a pnp struct; return it if so. |
| 138 | static struct pnp_data * |
| 139 | get_pnp_rom(struct rom_header *rom) |
| 140 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 141 | struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 142 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 143 | return NULL; |
| 144 | return pnp; |
| 145 | } |
| 146 | |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 147 | // Check for multiple pnp option rom headers. |
| 148 | static struct pnp_data * |
| 149 | get_pnp_next(struct rom_header *rom, struct pnp_data *pnp) |
| 150 | { |
| 151 | if (! pnp->nextoffset) |
| 152 | return NULL; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 153 | pnp = (void*)((u8*)rom + pnp->nextoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 154 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 155 | return NULL; |
| 156 | return pnp; |
| 157 | } |
| 158 | |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 159 | // Check if a valid option rom has a pci struct; return it if so. |
| 160 | static struct pci_data * |
| 161 | get_pci_rom(struct rom_header *rom) |
| 162 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 163 | struct pci_data *pci = (void*)((u32)rom + rom->pcioffset); |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 164 | if (pci->signature != PCI_ROM_SIGNATURE) |
| 165 | return NULL; |
| 166 | return pci; |
| 167 | } |
| 168 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 169 | // Copy a rom to its permanent location below 1MiB |
| 170 | static struct rom_header * |
| 171 | copy_rom(struct rom_header *rom) |
| 172 | { |
| 173 | u32 romsize = rom->size * 512; |
| 174 | if (next_rom + romsize > BUILD_BIOS_ADDR) { |
| 175 | // Option rom doesn't fit. |
| 176 | dprintf(1, "Option rom %p doesn't fit.\n", rom); |
| 177 | return NULL; |
| 178 | } |
Kevin O'Connor | c36273f | 2009-04-16 20:43:07 -0400 | [diff] [blame] | 179 | dprintf(4, "Copying option rom (size %d) from %p to %x\n" |
| 180 | , romsize, rom, next_rom); |
Kevin O'Connor | 942d495 | 2009-06-10 22:44:06 -0400 | [diff] [blame] | 181 | memcpy((void*)next_rom, rom, romsize); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 182 | return (void*)next_rom; |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 185 | // Run rom init code and note rom size. |
| 186 | static int |
| 187 | init_optionrom(struct rom_header *rom, u16 bdf, int isvga) |
| 188 | { |
| 189 | if (! is_valid_rom(rom)) |
| 190 | return -1; |
| 191 | |
| 192 | if (isvga || get_pnp_rom(rom)) |
| 193 | // Only init vga and PnP roms here. |
| 194 | callrom(rom, bdf); |
| 195 | |
| 196 | next_rom = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /**************************************************************** |
| 203 | * Roms in CBFS |
| 204 | ****************************************************************/ |
| 205 | |
| 206 | // 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] | 207 | static struct rom_header * |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 208 | lookup_hardcode(u32 vendev) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 209 | { |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 210 | if (OPTIONROM_VENDEV_1 |
| 211 | && ((OPTIONROM_VENDEV_1 >> 16) |
| 212 | | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 213 | return copy_rom((void*)OPTIONROM_MEM_1); |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 214 | if (OPTIONROM_VENDEV_2 |
| 215 | && ((OPTIONROM_VENDEV_2 >> 16) |
| 216 | | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 217 | return copy_rom((void*)OPTIONROM_MEM_2); |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 218 | int ret = cbfs_copy_optionrom((void*)next_rom, BUILD_BIOS_ADDR - next_rom |
| 219 | , vendev); |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 220 | if (ret < 0) |
| 221 | return NULL; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 222 | return (void*)next_rom; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 225 | // Run all roms in a given CBFS directory. |
| 226 | static void |
| 227 | run_cbfs_roms(const char *prefix, int isvga) |
| 228 | { |
| 229 | struct cbfs_file *tmp = NULL; |
| 230 | for (;;) { |
| 231 | tmp = cbfs_copyfile_prefix( |
| 232 | (void*)next_rom, BUILD_BIOS_ADDR - next_rom, prefix, tmp); |
| 233 | if (!tmp) |
| 234 | break; |
| 235 | init_optionrom((void*)next_rom, 0, isvga); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /**************************************************************** |
| 241 | * PCI roms |
| 242 | ****************************************************************/ |
| 243 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 244 | // Map the option rom of a given PCI device. |
| 245 | static struct rom_header * |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 246 | map_pcirom(u16 bdf, u32 vendev) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 247 | { |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 248 | dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n" |
| 249 | , 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] | 250 | |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 251 | u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE); |
| 252 | if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) { |
| 253 | dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype); |
| 254 | return NULL; |
| 255 | } |
| 256 | |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 257 | u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
| 258 | pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE); |
| 259 | u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 260 | |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 261 | dprintf(6, "Option rom sizing returned %x %x\n", orig, sz); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 262 | orig &= ~PCI_ROM_ADDRESS_ENABLE; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 263 | if (!sz || sz == 0xffffffff) |
| 264 | goto fail; |
| 265 | |
Kevin O'Connor | 64cf2fb | 2009-04-18 17:01:02 -0400 | [diff] [blame] | 266 | if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) { |
| 267 | // Don't try to map to a pci addresses at its max, in the last |
| 268 | // 4MiB of ram, or the first 16MiB of ram. |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 269 | dprintf(6, "Preset rom address doesn't look valid\n"); |
| 270 | goto fail; |
| 271 | } |
| 272 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 273 | // Looks like a rom - enable it. |
| 274 | 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] | 275 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 276 | struct rom_header *rom = (void*)orig; |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 277 | for (;;) { |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 278 | dprintf(5, "Inspecting possible rom at %p (dv=%x bdf=%x)\n" |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 279 | , rom, vendev, bdf); |
| 280 | if (rom->signature != OPTION_ROM_SIGNATURE) { |
| 281 | dprintf(6, "No option rom signature (got %x)\n", rom->signature); |
| 282 | goto fail; |
| 283 | } |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 284 | struct pci_data *pci = get_pci_rom(rom); |
| 285 | if (! pci) { |
| 286 | dprintf(6, "No valid pci signature found\n"); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 287 | goto fail; |
| 288 | } |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 289 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 290 | u32 vd = (pci->device << 16) | pci->vendor; |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 291 | if (vd == vendev && pci->type == PCIROM_CODETYPE_X86) |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 292 | // A match |
| 293 | break; |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 294 | dprintf(6, "Didn't match dev/ven (got %x) or type (got %d)\n" |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 295 | , vd, pci->type); |
| 296 | if (pci->indicator & 0x80) { |
| 297 | dprintf(6, "No more images left\n"); |
| 298 | goto fail; |
| 299 | } |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 300 | rom = (void*)((u32)rom + pci->ilen * 512); |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 301 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 302 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 303 | rom = copy_rom(rom); |
| 304 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 305 | return rom; |
| 306 | fail: |
| 307 | // Not valid - restore original and exit. |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 308 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | // 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^] | 313 | static int |
| 314 | init_pcirom(u16 bdf, int isvga) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 315 | { |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 316 | u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID); |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 317 | dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %x)\n" |
| 318 | , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf) |
| 319 | , vendev); |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 320 | struct rom_header *rom = lookup_hardcode(vendev); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 321 | if (! rom) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 322 | rom = map_pcirom(bdf, vendev); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 323 | if (! rom) |
| 324 | // No ROM present. |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 325 | return -1; |
| 326 | return init_optionrom(rom, bdf, isvga); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | /**************************************************************** |
| 331 | * Non-VGA option rom init |
| 332 | ****************************************************************/ |
| 333 | |
| 334 | void |
| 335 | optionrom_setup() |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 336 | { |
| 337 | if (! CONFIG_OPTIONROMS) |
| 338 | return; |
| 339 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 340 | dprintf(1, "Scan for option roms\n"); |
| 341 | |
| 342 | u32 post_vga = next_rom; |
| 343 | |
| 344 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 345 | // Option roms are already deployed on the system. |
| 346 | u32 pos = next_rom; |
| 347 | while (pos < BUILD_BIOS_ADDR) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 348 | int ret = init_optionrom((void*)pos, 0, 0); |
| 349 | if (ret) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 350 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 351 | else |
| 352 | pos = next_rom; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 353 | } |
| 354 | } else { |
| 355 | // Find and deploy PCI roms. |
Kevin O'Connor | e633832 | 2008-11-29 20:31:49 -0500 | [diff] [blame] | 356 | int bdf, max; |
Kevin O'Connor | 4132e02 | 2008-12-04 19:39:10 -0500 | [diff] [blame] | 357 | foreachpci(bdf, max) { |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 358 | u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE); |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 359 | if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA |
| 360 | || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE)) |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 361 | continue; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 362 | init_pcirom(bdf, 0); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 363 | } |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 364 | |
| 365 | // Find and deploy CBFS roms not associated with a device. |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 366 | run_cbfs_roms("genroms/", 0); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // All option roms found and deployed - now build BEV/BCV vectors. |
| 370 | |
| 371 | u32 pos = post_vga; |
| 372 | while (pos < next_rom) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 373 | struct rom_header *rom = (void*)pos; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 374 | if (! is_valid_rom(rom)) { |
| 375 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 376 | continue; |
| 377 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 378 | pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 379 | struct pnp_data *pnp = get_pnp_rom(rom); |
| 380 | if (! pnp) { |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 381 | // Legacy rom. |
| 382 | add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 383 | continue; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 384 | } |
| 385 | // PnP rom. |
| 386 | if (pnp->bev) |
| 387 | // Can boot system - add to IPL list. |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 388 | add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname); |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 389 | else |
| 390 | // Check for BCV (there may be multiple). |
| 391 | while (pnp && pnp->bcv) { |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 392 | add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname); |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 393 | pnp = get_pnp_next(rom, pnp); |
| 394 | } |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 398 | |
| 399 | /**************************************************************** |
| 400 | * VGA init |
| 401 | ****************************************************************/ |
| 402 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 403 | // Call into vga code to turn on console. |
| 404 | void |
| 405 | vga_setup() |
| 406 | { |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 407 | if (! CONFIG_OPTIONROMS) |
| 408 | return; |
| 409 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 410 | dprintf(1, "Scan for VGA option rom\n"); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 411 | next_rom = OPTION_ROM_START; |
| 412 | |
| 413 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 414 | // Option roms are already deployed on the system. |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 415 | init_optionrom((void*)OPTION_ROM_START, 0, 1); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 416 | } else { |
| 417 | // Find and deploy PCI VGA rom. |
Kevin O'Connor | 4132e02 | 2008-12-04 19:39:10 -0500 | [diff] [blame] | 418 | int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 419 | if (bdf >= 0) |
| 420 | init_pcirom(bdf, 1); |
Kevin O'Connor | 09880da | 2009-06-17 20:35:41 -0400 | [diff] [blame] | 421 | |
| 422 | // Find and deploy CBFS vga-style roms not associated with a device. |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame^] | 423 | run_cbfs_roms("vgaroms/", 1); |
| 424 | } |
| 425 | |
| 426 | if (next_rom == OPTION_ROM_START) { |
| 427 | // No VGA rom found |
| 428 | next_rom += OPTION_ROM_ALIGN; |
| 429 | return; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 430 | } |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 431 | |
| 432 | dprintf(1, "Turning on vga console\n"); |
| 433 | struct bregs br; |
| 434 | memset(&br, 0, sizeof(br)); |
| 435 | br.ax = 0x0003; |
| 436 | call16_int(0x10, &br); |
| 437 | |
| 438 | // Write to screen. |
| 439 | printf("Starting SeaBIOS\n\n"); |
| 440 | } |