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 | // |
| 6 | // This file may be distributed under the terms of the GNU GPLv3 license. |
| 7 | |
| 8 | #include "bregs.h" // struct bregs |
| 9 | #include "biosvar.h" // struct ipl_entry_s |
| 10 | #include "util.h" // dprintf |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 11 | #include "pci.h" // pci_find_class |
| 12 | #include "pci_regs.h" // PCI_ROM_ADDRESS |
| 13 | #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA |
| 14 | |
| 15 | |
| 16 | /**************************************************************** |
| 17 | * Definitions |
| 18 | ****************************************************************/ |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 19 | |
| 20 | // $PnP string with special alignment in romlayout.S |
| 21 | extern char pnp_string[]; |
| 22 | |
| 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 OPTIONROM_BDF_1 0x0000 |
| 69 | #define OPTIONROM_MEM_1 0x00000000 |
| 70 | #define OPTIONROM_BDF_2 0x0000 |
| 71 | #define OPTIONROM_MEM_2 0x00000000 |
| 72 | |
| 73 | #define OPTION_ROM_START 0xc0000 |
| 74 | #define OPTION_ROM_SIGNATURE 0xaa55 |
| 75 | #define OPTION_ROM_ALIGN 2048 |
| 76 | #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0]) |
| 77 | |
| 78 | // Next available position for an option rom. |
| 79 | static u32 next_rom; |
| 80 | |
| 81 | |
| 82 | /**************************************************************** |
| 83 | * Helper functions |
| 84 | ****************************************************************/ |
| 85 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 86 | // Execute a given option rom. |
| 87 | static void |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 88 | callrom(struct rom_header *rom, u16 offset, u16 bdf) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 89 | { |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 90 | u16 seg = FARPTR_TO_SEG(rom); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 91 | dprintf(1, "Running option rom at %x:%x\n", seg, offset); |
| 92 | |
| 93 | struct bregs br; |
| 94 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 95 | br.ax = bdf; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 96 | br.bx = 0xffff; |
| 97 | br.dx = 0xffff; |
| 98 | br.es = SEG_BIOS; |
| 99 | br.di = (u32)pnp_string - BUILD_BIOS_ADDR; |
| 100 | br.cs = seg; |
| 101 | br.ip = offset; |
| 102 | call16(&br); |
| 103 | |
| 104 | debug_serial_setup(); |
| 105 | |
| 106 | if (GET_BDA(ebda_seg) != SEG_EBDA) |
| 107 | BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n" |
| 108 | , seg, offset, SEG_EBDA, GET_BDA(ebda_seg)); |
| 109 | } |
| 110 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 111 | // Verify that an option rom looks valid |
| 112 | static int |
| 113 | is_valid_rom(struct rom_header *rom) |
| 114 | { |
| 115 | if (rom->signature != OPTION_ROM_SIGNATURE) |
| 116 | return 0; |
| 117 | u32 len = rom->size * 512; |
| 118 | u8 sum = checksum((void*)rom, len); |
| 119 | if (sum != 0) { |
| 120 | dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n" |
| 121 | , rom, len, sum); |
| 122 | return 0; |
| 123 | } |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | // Check if a valid option rom has a pnp struct; return it if so. |
| 128 | static struct pnp_data * |
| 129 | get_pnp_rom(struct rom_header *rom) |
| 130 | { |
| 131 | struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset); |
| 132 | if (pnp->signature != *(u32*)pnp_string) |
| 133 | return NULL; |
| 134 | return pnp; |
| 135 | } |
| 136 | |
| 137 | // Add a BEV vector for a given pnp compatible option rom. |
| 138 | static void |
| 139 | add_ipl(struct rom_header *rom, struct pnp_data *pnp) |
| 140 | { |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 141 | #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0)) |
| 142 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 143 | // Found a device that thinks it can boot the system. Record |
| 144 | // its BEV and product name string. |
| 145 | |
| 146 | if (! CONFIG_BOOT) |
| 147 | return; |
| 148 | |
| 149 | if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table)) |
| 150 | return; |
| 151 | |
| 152 | struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count]; |
| 153 | ip->type = IPL_TYPE_BEV; |
| 154 | ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev; |
| 155 | |
| 156 | u16 desc = pnp->productname; |
| 157 | if (desc) |
| 158 | ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc); |
| 159 | |
| 160 | ebda->ipl.count++; |
| 161 | } |
| 162 | |
| 163 | // Check if an option rom is at a hardcoded location for a device. |
| 164 | static struct rom_header * |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 165 | lookup_hardcode(u16 bdf) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 166 | { |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 167 | if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 168 | return (struct rom_header *)OPTIONROM_MEM_1; |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 169 | else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 170 | return (struct rom_header *)OPTIONROM_MEM_2; |
| 171 | // XXX - check LAR when in coreboot? |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | // Map the option rom of a given PCI device. |
| 176 | static struct rom_header * |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 177 | map_optionrom(u16 bdf) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 178 | { |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 179 | dprintf(6, "Attempting to map option rom on dev %x\n", bdf); |
| 180 | |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 181 | u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
| 182 | pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE); |
| 183 | u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 184 | |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 185 | dprintf(6, "Option rom sizing returned %x %x\n", orig, sz); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 186 | if (!sz || sz == 0xffffffff) |
| 187 | goto fail; |
| 188 | |
| 189 | // Looks like a rom - map it to just above end of memory. |
| 190 | u32 mappos = ALIGN(GET_EBDA(ram_size), OPTION_ROM_ALIGN); |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 191 | pci_config_writel(bdf, PCI_ROM_ADDRESS, mappos | PCI_ROM_ADDRESS_ENABLE); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 192 | |
| 193 | struct rom_header *rom = (struct rom_header *)mappos; |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 194 | if (rom->signature != OPTION_ROM_SIGNATURE) { |
| 195 | dprintf(6, "No option rom signature (got %x)\n", rom->signature); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 196 | goto fail; |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 197 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 198 | |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 199 | dprintf(6, "Card %x option rom mapped at %p\n", bdf, rom); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 200 | return rom; |
| 201 | fail: |
| 202 | // Not valid - restore original and exit. |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 203 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | // Attempt to map and initialize the option rom on a given PCI device. |
| 208 | static struct rom_header * |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 209 | init_optionrom(u16 bdf) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 210 | { |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 211 | dprintf(4, "Attempting to init PCI bdf %x\n", bdf); |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 212 | struct rom_header *rom = lookup_hardcode(bdf); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 213 | if (! rom) |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 214 | rom = map_optionrom(bdf); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 215 | if (! rom) |
| 216 | // No ROM present. |
| 217 | return NULL; |
| 218 | |
| 219 | u32 romsize = rom->size * 512; |
| 220 | if (next_rom + romsize > BUILD_BIOS_ADDR) { |
| 221 | // Option rom doesn't fit. |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 222 | dprintf(1, "Option rom %x doesn't fit.\n", bdf); |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 223 | pci_config_writel(bdf, PCI_ROM_ADDRESS, next_rom); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 224 | return NULL; |
| 225 | } |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 226 | dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 227 | memcpy((void*)next_rom, rom, romsize); |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 228 | pci_config_writel(bdf, PCI_ROM_ADDRESS, next_rom); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 229 | rom = (struct rom_header *)next_rom; |
| 230 | |
| 231 | if (! is_valid_rom(rom)) |
| 232 | return NULL; |
| 233 | |
| 234 | if (get_pnp_rom(rom)) |
| 235 | // Init the PnP rom. |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 236 | callrom(rom, OPTION_ROM_INITVECTOR, bdf); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 237 | |
| 238 | next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 239 | |
| 240 | return rom; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /**************************************************************** |
| 245 | * Non-VGA option rom init |
| 246 | ****************************************************************/ |
| 247 | |
| 248 | void |
| 249 | optionrom_setup() |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 250 | { |
| 251 | if (! CONFIG_OPTIONROMS) |
| 252 | return; |
| 253 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 254 | dprintf(1, "Scan for option roms\n"); |
| 255 | |
| 256 | u32 post_vga = next_rom; |
| 257 | |
| 258 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 259 | // Option roms are already deployed on the system. |
| 260 | u32 pos = next_rom; |
| 261 | while (pos < BUILD_BIOS_ADDR) { |
| 262 | struct rom_header *rom = (struct rom_header *)pos; |
| 263 | if (! is_valid_rom(rom)) { |
| 264 | pos += OPTION_ROM_ALIGN; |
| 265 | continue; |
| 266 | } |
| 267 | if (get_pnp_rom(rom)) |
| 268 | callrom(rom, OPTION_ROM_INITVECTOR, 0); |
| 269 | pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 270 | next_rom = pos; |
| 271 | } |
| 272 | } else { |
| 273 | // Find and deploy PCI roms. |
Kevin O'Connor | e633832 | 2008-11-29 20:31:49 -0500 | [diff] [blame^] | 274 | int bdf, max; |
| 275 | foreachpci(bdf, max, 0) { |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 276 | u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE); |
| 277 | if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA) |
| 278 | continue; |
| 279 | init_optionrom(bdf); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | // All option roms found and deployed - now build BEV/BCV vectors. |
| 284 | |
| 285 | u32 pos = post_vga; |
| 286 | while (pos < next_rom) { |
| 287 | struct rom_header *rom = (struct rom_header *)pos; |
| 288 | if (! is_valid_rom(rom)) { |
| 289 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 290 | continue; |
| 291 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 292 | pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 293 | struct pnp_data *pnp = get_pnp_rom(rom); |
| 294 | if (! pnp) { |
| 295 | // Legacy rom - run init vector now. |
| 296 | callrom(rom, OPTION_ROM_INITVECTOR, 0); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 297 | continue; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 298 | } |
| 299 | // PnP rom. |
| 300 | if (pnp->bev) |
| 301 | // Can boot system - add to IPL list. |
| 302 | add_ipl(rom, pnp); |
| 303 | else if (pnp->bcv) |
| 304 | // Has BCV - run it now. |
| 305 | callrom(rom, pnp->bcv, 0); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 309 | |
| 310 | /**************************************************************** |
| 311 | * VGA init |
| 312 | ****************************************************************/ |
| 313 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 314 | // Call into vga code to turn on console. |
| 315 | void |
| 316 | vga_setup() |
| 317 | { |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 318 | if (! CONFIG_OPTIONROMS) |
| 319 | return; |
| 320 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 321 | dprintf(1, "Scan for VGA option rom\n"); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 322 | next_rom = OPTION_ROM_START; |
| 323 | |
| 324 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 325 | // Option roms are already deployed on the system. |
| 326 | struct rom_header *rom = (struct rom_header *)OPTION_ROM_START; |
| 327 | if (! is_valid_rom(rom)) |
| 328 | return; |
| 329 | callrom(rom, OPTION_ROM_INITVECTOR, 0); |
| 330 | next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 331 | } else { |
| 332 | // Find and deploy PCI VGA rom. |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 333 | int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA, 0); |
| 334 | if (bdf < 0) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 335 | // Device not found |
| 336 | return; |
| 337 | |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 338 | struct rom_header *rom = init_optionrom(bdf); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 339 | if (rom && !get_pnp_rom(rom)) |
| 340 | // Call rom even if it isn't a pnp rom. |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 341 | callrom(rom, OPTION_ROM_INITVECTOR, bdf); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 342 | } |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 343 | |
| 344 | dprintf(1, "Turning on vga console\n"); |
| 345 | struct bregs br; |
| 346 | memset(&br, 0, sizeof(br)); |
| 347 | br.ax = 0x0003; |
| 348 | call16_int(0x10, &br); |
| 349 | |
| 350 | // Write to screen. |
| 351 | printf("Starting SeaBIOS\n\n"); |
| 352 | } |