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 | |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 8 | #include "bregs.h" // struct bregs |
| 9 | #include "config.h" // CONFIG_* |
| 10 | #include "farptr.h" // FLATPTR_TO_SEG |
Gerd Hoffmann | 0932c20 | 2018-11-20 08:06:55 +0100 | [diff] [blame] | 11 | #include "biosvar.h" // GET_IVT |
Kevin O'Connor | 4d8510c | 2016-02-03 01:28:20 -0500 | [diff] [blame] | 12 | #include "hw/pci.h" // pci_config_readl |
| 13 | #include "hw/pcidevice.h" // foreachpci |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 14 | #include "hw/pci_ids.h" // PCI_CLASS_DISPLAY_VGA |
| 15 | #include "hw/pci_regs.h" // PCI_ROM_ADDRESS |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 16 | #include "malloc.h" // rom_confirm |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 17 | #include "output.h" // dprintf |
Kevin O'Connor | 41639f8 | 2013-09-14 19:37:36 -0400 | [diff] [blame] | 18 | #include "romfile.h" // romfile_loadint |
Kevin O'Connor | 3df600b | 2013-09-14 19:28:55 -0400 | [diff] [blame] | 19 | #include "stacks.h" // farcall16big |
Kevin O'Connor | 8fb3a5e | 2013-09-14 22:27:14 -0400 | [diff] [blame] | 20 | #include "std/optionrom.h" // struct rom_header |
Kevin O'Connor | 4f790aa | 2013-09-14 23:04:08 -0400 | [diff] [blame] | 21 | #include "std/pnpbios.h" // PNP_SIGNATURE |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 22 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 23 | #include "util.h" // get_pnp_offset |
Stefan Berger | 2aff1c1 | 2015-05-26 15:48:33 -0400 | [diff] [blame] | 24 | #include "tcgbios.h" // tpm_* |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 25 | |
tpearson@raptorengineeringinc.com | d23eba6 | 2015-02-17 15:04:17 -0600 | [diff] [blame] | 26 | static int EnforceChecksum, S3ResumeVga, RunPCIroms; |
| 27 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 28 | |
| 29 | /**************************************************************** |
| 30 | * Helper functions |
| 31 | ****************************************************************/ |
| 32 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 33 | // Execute a given option rom. |
| 34 | static void |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 35 | __callrom(struct rom_header *rom, u16 offset, u16 bdf) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 36 | { |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 37 | u16 seg = FLATPTR_TO_SEG(rom); |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 38 | dprintf(1, "Running option rom at %04x:%04x\n", seg, offset); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 39 | |
| 40 | struct bregs br; |
| 41 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 42 | br.flags = F_IF; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 43 | br.ax = bdf; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 44 | br.bx = 0xffff; |
| 45 | br.dx = 0xffff; |
| 46 | br.es = SEG_BIOS; |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 47 | br.di = get_pnp_offset(); |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 48 | br.code = SEGOFF(seg, offset); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 49 | start_preempt(); |
Kevin O'Connor | c7ffbac | 2012-03-25 11:04:10 -0400 | [diff] [blame] | 50 | farcall16big(&br); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 51 | finish_preempt(); |
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. |
David Woodhouse | 5c8dd96 | 2013-01-25 19:31:47 -0600 | [diff] [blame] | 55 | void |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 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 | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 68 | // Verify that an option rom looks valid |
| 69 | static int |
| 70 | is_valid_rom(struct rom_header *rom) |
| 71 | { |
Kevin O'Connor | c36273f | 2009-04-16 20:43:07 -0400 | [diff] [blame] | 72 | dprintf(6, "Checking rom %p (sig %x size %d)\n" |
| 73 | , rom, rom->signature, rom->size); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 74 | if (rom->signature != OPTION_ROM_SIGNATURE) |
| 75 | return 0; |
Kevin O'Connor | 674e460 | 2009-04-13 19:19:22 -0400 | [diff] [blame] | 76 | if (! rom->size) |
| 77 | return 0; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 78 | u32 len = rom->size * 512; |
Kevin O'Connor | 94fd47e | 2009-02-15 15:21:10 -0500 | [diff] [blame] | 79 | u8 sum = checksum(rom, len); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 80 | if (sum != 0) { |
| 81 | dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n" |
| 82 | , rom, len, sum); |
Kevin O'Connor | e010d85 | 2011-07-05 20:47:35 -0400 | [diff] [blame] | 83 | if (EnforceChecksum) |
Kevin O'Connor | cc97564 | 2011-03-06 19:22:46 -0500 | [diff] [blame] | 84 | return 0; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 85 | } |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | // Check if a valid option rom has a pnp struct; return it if so. |
| 90 | static struct pnp_data * |
| 91 | get_pnp_rom(struct rom_header *rom) |
| 92 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 93 | struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 94 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 95 | return NULL; |
| 96 | return pnp; |
| 97 | } |
| 98 | |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 99 | // Check for multiple pnp option rom headers. |
| 100 | static struct pnp_data * |
| 101 | get_pnp_next(struct rom_header *rom, struct pnp_data *pnp) |
| 102 | { |
| 103 | if (! pnp->nextoffset) |
| 104 | return NULL; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 105 | pnp = (void*)((u8*)rom + pnp->nextoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 106 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 107 | return NULL; |
| 108 | return pnp; |
| 109 | } |
| 110 | |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 111 | // Check if a valid option rom has a pci struct; return it if so. |
| 112 | static struct pci_data * |
| 113 | get_pci_rom(struct rom_header *rom) |
| 114 | { |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 115 | struct pci_data *pd = (void*)((u32)rom + rom->pcioffset); |
| 116 | if (pd->signature != PCI_ROM_SIGNATURE) |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 117 | return NULL; |
Kevin O'Connor | 97dce0f | 2013-02-15 22:46:09 -0500 | [diff] [blame] | 118 | if (rom->pcioffset & 3) |
| 119 | dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n" |
| 120 | , pd->vendor, pd->device); |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 121 | return pd; |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 124 | // Run rom init code and note rom size. |
| 125 | static int |
| 126 | init_optionrom(struct rom_header *rom, u16 bdf, int isvga) |
| 127 | { |
| 128 | if (! is_valid_rom(rom)) |
| 129 | return -1; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 130 | struct rom_header *newrom = rom_reserve(rom->size * 512); |
| 131 | if (!newrom) { |
| 132 | warn_noalloc(); |
| 133 | return -1; |
| 134 | } |
| 135 | if (newrom != rom) |
| 136 | memmove(newrom, rom, rom->size * 512); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 137 | |
Stefan Berger | 5deeec1 | 2015-06-09 19:56:33 -0400 | [diff] [blame] | 138 | tpm_option_rom(newrom, rom->size * 512); |
| 139 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 140 | if (isvga || get_pnp_rom(newrom)) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 141 | // Only init vga and PnP roms here. |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 142 | callrom(newrom, bdf); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 143 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 144 | return rom_confirm(newrom->size * 512); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 147 | #define RS_PCIROM (1LL<<33) |
| 148 | |
| 149 | static void |
| 150 | setRomSource(u64 *sources, struct rom_header *rom, u64 source) |
| 151 | { |
| 152 | if (sources) |
| 153 | sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source; |
| 154 | } |
| 155 | |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 156 | static int |
| 157 | getRomPriority(u64 *sources, struct rom_header *rom, int instance) |
| 158 | { |
| 159 | u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN]; |
| 160 | if (!source) |
| 161 | return -1; |
| 162 | if (source & RS_PCIROM) |
Kevin O'Connor | fce9189 | 2011-07-09 14:47:47 -0400 | [diff] [blame] | 163 | return bootprio_find_pci_rom((void*)(u32)source, instance); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 164 | struct romfile_s *file = (void*)(u32)source; |
| 165 | return bootprio_find_named_rom(file->name, instance); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 166 | } |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 167 | |
Kevin O'Connor | c1de91b | 2011-07-02 13:50:21 -0400 | [diff] [blame] | 168 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 169 | /**************************************************************** |
| 170 | * Roms in CBFS |
| 171 | ****************************************************************/ |
| 172 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 173 | static struct rom_header * |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 174 | deploy_romfile(struct romfile_s *file) |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 175 | { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 176 | u32 size = file->size; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 177 | struct rom_header *rom = rom_reserve(size); |
| 178 | if (!rom) { |
| 179 | warn_noalloc(); |
| 180 | return NULL; |
| 181 | } |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 182 | int ret = file->copy(file, rom, size); |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 183 | if (ret <= 0) |
| 184 | return NULL; |
| 185 | return rom; |
| 186 | } |
| 187 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 188 | // Run all roms in a given CBFS directory. |
| 189 | static void |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 190 | run_file_roms(const char *prefix, int isvga, u64 *sources) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 191 | { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 192 | struct romfile_s *file = NULL; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 193 | for (;;) { |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 194 | file = romfile_findprefix(prefix, file); |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 195 | if (!file) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 196 | break; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 197 | struct rom_header *rom = deploy_romfile(file); |
| 198 | if (rom) { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 199 | setRomSource(sources, rom, (u32)file); |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 200 | init_optionrom(rom, 0, isvga); |
| 201 | } |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /**************************************************************** |
| 207 | * PCI roms |
| 208 | ****************************************************************/ |
| 209 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 210 | // Verify device is a vga device with legacy address decoding enabled. |
Alex Williamson | 7adfd71 | 2013-03-20 10:58:47 -0600 | [diff] [blame] | 211 | int |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 212 | is_pci_vga(struct pci_device *pci) |
| 213 | { |
| 214 | if (pci->class != PCI_CLASS_DISPLAY_VGA) |
| 215 | return 0; |
| 216 | u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND); |
| 217 | if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY)) |
| 218 | return 0; |
| 219 | while (pci->parent) { |
| 220 | pci = pci->parent; |
| 221 | u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL); |
| 222 | if (!(ctrl & PCI_BRIDGE_CTL_VGA)) |
| 223 | return 0; |
| 224 | } |
| 225 | return 1; |
| 226 | } |
| 227 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 228 | // Copy a rom to its permanent location below 1MiB |
| 229 | static struct rom_header * |
| 230 | copy_rom(struct rom_header *rom) |
| 231 | { |
| 232 | u32 romsize = rom->size * 512; |
| 233 | struct rom_header *newrom = rom_reserve(romsize); |
| 234 | if (!newrom) { |
| 235 | warn_noalloc(); |
| 236 | return NULL; |
| 237 | } |
| 238 | dprintf(4, "Copying option rom (size %d) from %p to %p\n" |
| 239 | , romsize, rom, newrom); |
| 240 | iomemcpy(newrom, rom, romsize); |
| 241 | return newrom; |
| 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 | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 246 | map_pcirom(struct pci_device *pci) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 247 | { |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 248 | dprintf(6, "Attempting to map option rom on dev %pP\n", pci); |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 249 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 250 | if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) { |
| 251 | dprintf(6, "Skipping non-normal pci device (type=%x)\n" |
| 252 | , pci->header_type); |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 253 | return NULL; |
| 254 | } |
| 255 | |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 256 | u16 bdf = pci->bdf; |
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 | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 278 | dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x bdf=%pP)\n" |
| 279 | , rom, pci->vendor, pci->device, pci); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 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 | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 284 | struct pci_data *pd = get_pci_rom(rom); |
| 285 | if (! pd) { |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 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 | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 290 | if (pd->vendor == pci->vendor && pd->device == pci->device |
| 291 | && pd->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 | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 294 | dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n" |
| 295 | , pd->vendor, pd->device, pd->type); |
| 296 | if (pd->indicator & 0x80) { |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 297 | dprintf(6, "No more images left\n"); |
| 298 | goto fail; |
| 299 | } |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 300 | rom = (void*)((u32)rom + pd->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 | |
Gerd Hoffmann | 0932c20 | 2018-11-20 08:06:55 +0100 | [diff] [blame] | 312 | static int boot_irq_captured(void) |
| 313 | { |
| 314 | return GET_IVT(0x19).segoff != FUNC16(entry_19_official).segoff; |
| 315 | } |
| 316 | |
| 317 | static void boot_irq_restore(void) |
| 318 | { |
| 319 | struct segoff_s seabios; |
| 320 | |
| 321 | seabios = FUNC16(entry_19_official); |
| 322 | SET_IVT(0x19, seabios); |
| 323 | } |
| 324 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 325 | // Attempt to map and initialize the option rom on a given PCI device. |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 326 | static void |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 327 | init_pcirom(struct pci_device *pci, int isvga, u64 *sources) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 328 | { |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 329 | dprintf(4, "Attempting to init PCI bdf %pP (vd %04x:%04x)\n" |
| 330 | , pci, pci->vendor, pci->device); |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 331 | |
Rob Barnes | 27b56c6 | 2020-01-22 14:09:21 -0700 | [diff] [blame] | 332 | char fname[20]; |
| 333 | // Try to find rom file with revision included |
| 334 | snprintf(fname, sizeof(fname), "pci%04x,%04x,%02x.rom" |
| 335 | , pci->vendor, pci->device, pci->revision); |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 336 | struct romfile_s *file = romfile_find(fname); |
Rob Barnes | 27b56c6 | 2020-01-22 14:09:21 -0700 | [diff] [blame] | 337 | if (!file) { |
| 338 | // Fallback to finding rom file without revision |
| 339 | snprintf(fname, sizeof(fname), "pci%04x,%04x.rom" |
| 340 | , pci->vendor, pci->device); |
| 341 | file = romfile_find(fname); |
| 342 | } |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 343 | struct rom_header *rom = NULL; |
| 344 | if (file) |
| 345 | rom = deploy_romfile(file); |
| 346 | else if (RunPCIroms > 1 || (RunPCIroms == 1 && isvga)) |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 347 | rom = map_pcirom(pci); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 348 | if (! rom) |
| 349 | // No ROM present. |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 350 | return; |
Gerd Hoffmann | 0932c20 | 2018-11-20 08:06:55 +0100 | [diff] [blame] | 351 | int irq_was_captured = boot_irq_captured(); |
| 352 | struct pnp_data *pnp = get_pnp_rom(rom); |
Kevin O'Connor | fce9189 | 2011-07-09 14:47:47 -0400 | [diff] [blame] | 353 | setRomSource(sources, rom, RS_PCIROM | (u32)pci); |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 354 | init_optionrom(rom, pci->bdf, isvga); |
Gerd Hoffmann | 0932c20 | 2018-11-20 08:06:55 +0100 | [diff] [blame] | 355 | if (boot_irq_captured() && !irq_was_captured && |
| 356 | !file && !isvga && pnp) { |
| 357 | // This PCI rom is misbehaving - recapture the boot irqs |
| 358 | char *desc = MAKE_FLATPTR(FLATPTR_TO_SEG(rom), pnp->productname); |
| 359 | dprintf(1, "PnP optionrom \"%s\" (bdf %pP) captured int19, restoring\n", |
| 360 | desc, pci); |
| 361 | boot_irq_restore(); |
| 362 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | |
| 366 | /**************************************************************** |
| 367 | * Non-VGA option rom init |
| 368 | ****************************************************************/ |
| 369 | |
| 370 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 371 | optionrom_setup(void) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 372 | { |
| 373 | if (! CONFIG_OPTIONROMS) |
| 374 | return; |
| 375 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 376 | dprintf(1, "Scan for option roms\n"); |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 377 | u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN]; |
| 378 | memset(sources, 0, sizeof(sources)); |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 379 | u32 post_vga = rom_get_last(); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 380 | |
Kevin O'Connor | 9d691ac | 2016-04-01 14:23:36 -0400 | [diff] [blame] | 381 | // Find and deploy PCI roms. |
| 382 | struct pci_device *pci; |
| 383 | foreachpci(pci) { |
Gerd Hoffmann | e28e0bb | 2018-05-30 13:49:06 +0200 | [diff] [blame] | 384 | if (pci->class == PCI_CLASS_DISPLAY_VGA || |
| 385 | pci->class == PCI_CLASS_DISPLAY_OTHER || |
| 386 | pci->have_driver) |
Kevin O'Connor | 9d691ac | 2016-04-01 14:23:36 -0400 | [diff] [blame] | 387 | continue; |
| 388 | init_pcirom(pci, 0, sources); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 389 | } |
Kevin O'Connor | 9d691ac | 2016-04-01 14:23:36 -0400 | [diff] [blame] | 390 | |
| 391 | // Find and deploy CBFS roms not associated with a device. |
| 392 | run_file_roms("genroms/", 0, sources); |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 393 | rom_reserve(0); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 394 | |
| 395 | // All option roms found and deployed - now build BEV/BCV vectors. |
| 396 | |
| 397 | u32 pos = post_vga; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 398 | while (pos < rom_get_last()) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 399 | struct rom_header *rom = (void*)pos; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 400 | if (! is_valid_rom(rom)) { |
| 401 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 402 | continue; |
| 403 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 404 | pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 405 | struct pnp_data *pnp = get_pnp_rom(rom); |
| 406 | if (! pnp) { |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 407 | // Legacy rom. |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 408 | boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0 |
| 409 | , getRomPriority(sources, rom, 0)); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 410 | continue; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 411 | } |
Kevin O'Connor | 1e15725 | 2012-01-14 11:55:35 -0500 | [diff] [blame] | 412 | // PnP rom - check for BEV and BCV boot capabilities. |
| 413 | int instance = 0; |
| 414 | while (pnp) { |
| 415 | if (pnp->bev) |
| 416 | boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname |
| 417 | , getRomPriority(sources, rom, instance++)); |
| 418 | else if (pnp->bcv) |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 419 | boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname |
| 420 | , getRomPriority(sources, rom, instance++)); |
Kevin O'Connor | 1e15725 | 2012-01-14 11:55:35 -0500 | [diff] [blame] | 421 | else |
| 422 | break; |
| 423 | pnp = get_pnp_next(rom, pnp); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 424 | } |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 428 | |
| 429 | /**************************************************************** |
| 430 | * VGA init |
| 431 | ****************************************************************/ |
| 432 | |
Kevin O'Connor | 422263d | 2011-07-05 20:56:07 -0400 | [diff] [blame] | 433 | int ScreenAndDebug; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 434 | struct rom_header *VgaROM; |
Kevin O'Connor | 8b0c509 | 2011-07-05 20:50:32 -0400 | [diff] [blame] | 435 | |
Gerd Hoffmann | e28e0bb | 2018-05-30 13:49:06 +0200 | [diff] [blame] | 436 | static void try_setup_display_other(void) |
| 437 | { |
| 438 | struct pci_device *pci; |
| 439 | |
| 440 | dprintf(1, "No VGA found, scan for other display\n"); |
| 441 | |
| 442 | foreachpci(pci) { |
| 443 | if (pci->class != PCI_CLASS_DISPLAY_OTHER) |
| 444 | continue; |
| 445 | struct rom_header *rom = map_pcirom(pci); |
| 446 | if (!rom) |
| 447 | continue; |
| 448 | dprintf(1, "Other display found at %pP\n", pci); |
| 449 | pci_config_maskw(pci->bdf, PCI_COMMAND, 0, |
| 450 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY); |
| 451 | init_optionrom(rom, pci->bdf, 1); |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 456 | // Call into vga code to turn on console. |
| 457 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 458 | vgarom_setup(void) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 459 | { |
Gerd Hoffmann | e28e0bb | 2018-05-30 13:49:06 +0200 | [diff] [blame] | 460 | int have_vga = 0; |
| 461 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 462 | if (! CONFIG_OPTIONROMS) |
| 463 | return; |
| 464 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 465 | dprintf(1, "Scan for VGA option rom\n"); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 466 | |
Kevin O'Connor | 422263d | 2011-07-05 20:56:07 -0400 | [diff] [blame] | 467 | // Load some config settings that impact VGA. |
Kevin O'Connor | e010d85 | 2011-07-05 20:47:35 -0400 | [diff] [blame] | 468 | EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1); |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 469 | S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU); |
tpearson@raptorengineeringinc.com | d23eba6 | 2015-02-17 15:04:17 -0600 | [diff] [blame] | 470 | RunPCIroms = romfile_loadint("etc/pci-optionrom-exec", 2); |
Kevin O'Connor | 422263d | 2011-07-05 20:56:07 -0400 | [diff] [blame] | 471 | ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1); |
Kevin O'Connor | e010d85 | 2011-07-05 20:47:35 -0400 | [diff] [blame] | 472 | |
Kevin O'Connor | 9d691ac | 2016-04-01 14:23:36 -0400 | [diff] [blame] | 473 | // Clear option rom memory |
| 474 | memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START); |
Kevin O'Connor | e826465 | 2010-09-15 22:06:19 -0400 | [diff] [blame] | 475 | |
Kevin O'Connor | 9d691ac | 2016-04-01 14:23:36 -0400 | [diff] [blame] | 476 | // Find and deploy PCI VGA rom. |
| 477 | struct pci_device *pci; |
| 478 | foreachpci(pci) { |
| 479 | if (!is_pci_vga(pci)) |
| 480 | continue; |
| 481 | vgahook_setup(pci); |
| 482 | init_pcirom(pci, 1, NULL); |
Gerd Hoffmann | e28e0bb | 2018-05-30 13:49:06 +0200 | [diff] [blame] | 483 | have_vga = 1; |
Kevin O'Connor | 9d691ac | 2016-04-01 14:23:36 -0400 | [diff] [blame] | 484 | break; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 485 | } |
Gerd Hoffmann | e28e0bb | 2018-05-30 13:49:06 +0200 | [diff] [blame] | 486 | if (!have_vga) |
| 487 | try_setup_display_other(); |
Kevin O'Connor | 9d691ac | 2016-04-01 14:23:36 -0400 | [diff] [blame] | 488 | |
| 489 | // Find and deploy CBFS vga-style roms not associated with a device. |
| 490 | run_file_roms("vgaroms/", 1, NULL); |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 491 | rom_reserve(0); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 492 | |
Gerd Hoffmann | d6728f3 | 2017-09-18 10:47:23 +0200 | [diff] [blame] | 493 | if (rom_get_last() != BUILD_ROM_START) |
| 494 | // VGA rom found |
| 495 | VgaROM = (void*)BUILD_ROM_START; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 496 | } |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 497 | |
| 498 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 499 | s3_resume_vga(void) |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 500 | { |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 501 | if (!S3ResumeVga) |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 502 | return; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 503 | if (!VgaROM || ! is_valid_rom(VgaROM)) |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 504 | return; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 505 | callrom(VgaROM, 0); |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 506 | } |