blob: 5c7912145473416ce14d408dba6e3e5b29eb9a3b [file] [log] [blame]
Kevin O'Connor714325c2008-11-01 20:32:27 -04001// 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'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor714325c2008-11-01 20:32:27 -04007
8#include "bregs.h" // struct bregs
Kevin O'Connor35ae7262009-01-19 15:44:44 -05009#include "farptr.h" // FLATPTR_TO_SEG
Kevin O'Connorc659fde2008-12-28 23:43:20 -050010#include "config.h" // CONFIG_*
Kevin O'Connor714325c2008-11-01 20:32:27 -040011#include "util.h" // dprintf
Kevin O'Connor5d369d82013-09-02 20:48:46 -040012#include "hw/pci.h" // foreachpci
13#include "hw/pci_regs.h" // PCI_ROM_ADDRESS
14#include "hw/pci_ids.h" // PCI_CLASS_DISPLAY_VGA
Kevin O'Connorc659fde2008-12-28 23:43:20 -050015#include "boot.h" // IPL
Julian Pidancetbc6097b2011-12-19 05:07:54 +000016#include "optionroms.h" // struct rom_header
Kevin O'Connor41639f82013-09-14 19:37:36 -040017#include "romfile.h" // romfile_loadint
Kevin O'Connor3df600b2013-09-14 19:28:55 -040018#include "stacks.h" // farcall16big
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040019#include "string.h" // memset
Kevin O'Connorceea03c2008-11-08 21:36:35 -050020
Kevin O'Connorceea03c2008-11-08 21:36:35 -050021
22/****************************************************************
23 * Helper functions
24 ****************************************************************/
25
Kevin O'Connor714325c2008-11-01 20:32:27 -040026// Execute a given option rom.
27static void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040028__callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040029{
Kevin O'Connor35ae7262009-01-19 15:44:44 -050030 u16 seg = FLATPTR_TO_SEG(rom);
Kevin O'Connor91b53a72009-05-05 22:52:09 -040031 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
Kevin O'Connor714325c2008-11-01 20:32:27 -040032
33 struct bregs br;
34 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040035 br.flags = F_IF;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050036 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040037 br.bx = 0xffff;
38 br.dx = 0xffff;
39 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050040 br.di = get_pnp_offset();
Kevin O'Connor9f985422009-09-09 11:34:39 -040041 br.code = SEGOFF(seg, offset);
Kevin O'Connorad901592009-12-13 11:25:25 -050042 start_preempt();
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -040043 farcall16big(&br);
Kevin O'Connorad901592009-12-13 11:25:25 -050044 finish_preempt();
Kevin O'Connor714325c2008-11-01 20:32:27 -040045
Kevin O'Connord83c87b2013-01-21 01:14:12 -050046 debug_serial_preinit();
Kevin O'Connor714325c2008-11-01 20:32:27 -040047}
48
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040049// Execute a given option rom at the standard entry vector.
David Woodhouse5c8dd962013-01-25 19:31:47 -060050void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040051callrom(struct rom_header *rom, u16 bdf)
52{
53 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
54}
55
Kevin O'Connor0a924122009-02-08 19:43:47 -050056// Execute a BCV option rom registered via add_bcv().
57void
58call_bcv(u16 seg, u16 ip)
59{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040060 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -050061}
62
Kevin O'Connore010d852011-07-05 20:47:35 -040063static int EnforceChecksum;
64
Kevin O'Connorceea03c2008-11-08 21:36:35 -050065// Verify that an option rom looks valid
66static int
67is_valid_rom(struct rom_header *rom)
68{
Kevin O'Connorc36273f2009-04-16 20:43:07 -040069 dprintf(6, "Checking rom %p (sig %x size %d)\n"
70 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050071 if (rom->signature != OPTION_ROM_SIGNATURE)
72 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -040073 if (! rom->size)
74 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050075 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050076 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050077 if (sum != 0) {
78 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
79 , rom, len, sum);
Kevin O'Connore010d852011-07-05 20:47:35 -040080 if (EnforceChecksum)
Kevin O'Connorcc975642011-03-06 19:22:46 -050081 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050082 }
83 return 1;
84}
85
86// Check if a valid option rom has a pnp struct; return it if so.
87static struct pnp_data *
88get_pnp_rom(struct rom_header *rom)
89{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040090 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050091 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -050092 return NULL;
93 return pnp;
94}
95
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -050096// Check for multiple pnp option rom headers.
97static struct pnp_data *
98get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
99{
100 if (! pnp->nextoffset)
101 return NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400102 pnp = (void*)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500103 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500104 return NULL;
105 return pnp;
106}
107
Kevin O'Connor62eea672008-12-06 11:57:45 -0500108// Check if a valid option rom has a pci struct; return it if so.
109static struct pci_data *
110get_pci_rom(struct rom_header *rom)
111{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400112 struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
113 if (pd->signature != PCI_ROM_SIGNATURE)
Kevin O'Connor62eea672008-12-06 11:57:45 -0500114 return NULL;
Kevin O'Connor97dce0f2013-02-15 22:46:09 -0500115 if (rom->pcioffset & 3)
116 dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n"
117 , pd->vendor, pd->device);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400118 return pd;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500119}
120
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400121// Run rom init code and note rom size.
122static int
123init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
124{
125 if (! is_valid_rom(rom))
126 return -1;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400127 struct rom_header *newrom = rom_reserve(rom->size * 512);
128 if (!newrom) {
129 warn_noalloc();
130 return -1;
131 }
132 if (newrom != rom)
133 memmove(newrom, rom, rom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400134
Kevin O'Connor5e019082012-05-20 21:11:43 -0400135 if (isvga || get_pnp_rom(newrom))
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400136 // Only init vga and PnP roms here.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400137 callrom(newrom, bdf);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400138
Kevin O'Connor5e019082012-05-20 21:11:43 -0400139 return rom_confirm(newrom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400140}
141
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500142#define RS_PCIROM (1LL<<33)
143
144static void
145setRomSource(u64 *sources, struct rom_header *rom, u64 source)
146{
147 if (sources)
148 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
149}
150
Kevin O'Connor031ef552010-12-27 19:26:57 -0500151static int
152getRomPriority(u64 *sources, struct rom_header *rom, int instance)
153{
154 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
155 if (!source)
156 return -1;
157 if (source & RS_PCIROM)
Kevin O'Connorfce91892011-07-09 14:47:47 -0400158 return bootprio_find_pci_rom((void*)(u32)source, instance);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400159 struct romfile_s *file = (void*)(u32)source;
160 return bootprio_find_named_rom(file->name, instance);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500161}
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400162
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400163
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400164/****************************************************************
165 * Roms in CBFS
166 ****************************************************************/
167
Kevin O'Connor5e019082012-05-20 21:11:43 -0400168static struct rom_header *
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400169deploy_romfile(struct romfile_s *file)
Kevin O'Connor5e019082012-05-20 21:11:43 -0400170{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400171 u32 size = file->size;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400172 struct rom_header *rom = rom_reserve(size);
173 if (!rom) {
174 warn_noalloc();
175 return NULL;
176 }
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400177 int ret = file->copy(file, rom, size);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400178 if (ret <= 0)
179 return NULL;
180 return rom;
181}
182
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400183// Check if an option rom is at a hardcoded location or in CBFS.
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500184static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400185lookup_hardcode(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500186{
Kevin O'Connore2304262010-06-13 16:05:17 -0400187 char fname[17];
188 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400189 , pci->vendor, pci->device);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400190 struct romfile_s *file = romfile_find(fname);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400191 if (file)
192 return deploy_romfile(file);
193 return NULL;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500194}
195
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400196// Run all roms in a given CBFS directory.
197static void
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500198run_file_roms(const char *prefix, int isvga, u64 *sources)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400199{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400200 struct romfile_s *file = NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400201 for (;;) {
Kevin O'Connore2304262010-06-13 16:05:17 -0400202 file = romfile_findprefix(prefix, file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400203 if (!file)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400204 break;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400205 struct rom_header *rom = deploy_romfile(file);
206 if (rom) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400207 setRomSource(sources, rom, (u32)file);
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500208 init_optionrom(rom, 0, isvga);
209 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400210 }
211}
212
213
214/****************************************************************
215 * PCI roms
216 ****************************************************************/
217
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400218// Verify device is a vga device with legacy address decoding enabled.
Alex Williamson7adfd712013-03-20 10:58:47 -0600219int
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400220is_pci_vga(struct pci_device *pci)
221{
222 if (pci->class != PCI_CLASS_DISPLAY_VGA)
223 return 0;
224 u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
225 if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
226 return 0;
227 while (pci->parent) {
228 pci = pci->parent;
229 u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
230 if (!(ctrl & PCI_BRIDGE_CTL_VGA))
231 return 0;
232 }
233 return 1;
234}
235
Kevin O'Connor5e019082012-05-20 21:11:43 -0400236// Copy a rom to its permanent location below 1MiB
237static struct rom_header *
238copy_rom(struct rom_header *rom)
239{
240 u32 romsize = rom->size * 512;
241 struct rom_header *newrom = rom_reserve(romsize);
242 if (!newrom) {
243 warn_noalloc();
244 return NULL;
245 }
246 dprintf(4, "Copying option rom (size %d) from %p to %p\n"
247 , romsize, rom, newrom);
248 iomemcpy(newrom, rom, romsize);
249 return newrom;
250}
251
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500252// Map the option rom of a given PCI device.
253static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400254map_pcirom(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500255{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400256 u16 bdf = pci->bdf;
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400257 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
258 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500259
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400260 if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
261 dprintf(6, "Skipping non-normal pci device (type=%x)\n"
262 , pci->header_type);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500263 return NULL;
264 }
265
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500266 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
267 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
268 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500269
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500270 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500271 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500272 if (!sz || sz == 0xffffffff)
273 goto fail;
274
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400275 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
276 // Don't try to map to a pci addresses at its max, in the last
277 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500278 dprintf(6, "Preset rom address doesn't look valid\n");
279 goto fail;
280 }
281
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500282 // Looks like a rom - enable it.
283 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500284
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400285 struct rom_header *rom = (void*)orig;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500286 for (;;) {
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400287 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
288 " bdf=%02x:%02x.%x)\n"
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400289 , rom, pci->vendor, pci->device
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400290 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500291 if (rom->signature != OPTION_ROM_SIGNATURE) {
292 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
293 goto fail;
294 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400295 struct pci_data *pd = get_pci_rom(rom);
296 if (! pd) {
Kevin O'Connor62eea672008-12-06 11:57:45 -0500297 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500298 goto fail;
299 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500300
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400301 if (pd->vendor == pci->vendor && pd->device == pci->device
302 && pd->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500303 // A match
304 break;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400305 dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
306 , pd->vendor, pd->device, pd->type);
307 if (pd->indicator & 0x80) {
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500308 dprintf(6, "No more images left\n");
309 goto fail;
310 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400311 rom = (void*)((u32)rom + pd->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500312 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500313
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500314 rom = copy_rom(rom);
315 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500316 return rom;
317fail:
318 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500319 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500320 return NULL;
321}
322
323// Attempt to map and initialize the option rom on a given PCI device.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400324static int
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400325init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500326{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400327 u16 bdf = pci->bdf;
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400328 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400329 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400330 , pci->vendor, pci->device);
331 struct rom_header *rom = lookup_hardcode(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500332 if (! rom)
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400333 rom = map_pcirom(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500334 if (! rom)
335 // No ROM present.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400336 return -1;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400337 setRomSource(sources, rom, RS_PCIROM | (u32)pci);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400338 return init_optionrom(rom, bdf, isvga);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500339}
340
341
342/****************************************************************
343 * Non-VGA option rom init
344 ****************************************************************/
345
346void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500347optionrom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400348{
349 if (! CONFIG_OPTIONROMS)
350 return;
351
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500352 dprintf(1, "Scan for option roms\n");
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500353 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
354 memset(sources, 0, sizeof(sources));
Kevin O'Connor5e019082012-05-20 21:11:43 -0400355 u32 post_vga = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500356
357 if (CONFIG_OPTIONROMS_DEPLOYED) {
358 // Option roms are already deployed on the system.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400359 u32 pos = post_vga;
Kevin O'Connor3733f6f2013-02-17 12:44:23 -0500360 while (pos < rom_get_max()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400361 int ret = init_optionrom((void*)pos, 0, 0);
362 if (ret)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500363 pos += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400364 else
Kevin O'Connor5e019082012-05-20 21:11:43 -0400365 pos = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500366 }
367 } else {
368 // Find and deploy PCI roms.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400369 struct pci_device *pci;
370 foreachpci(pci) {
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400371 if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500372 continue;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400373 init_pcirom(pci, 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500374 }
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400375
376 // Find and deploy CBFS roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500377 run_file_roms("genroms/", 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500378 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400379 rom_reserve(0);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500380
381 // All option roms found and deployed - now build BEV/BCV vectors.
382
383 u32 pos = post_vga;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400384 while (pos < rom_get_last()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400385 struct rom_header *rom = (void*)pos;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500386 if (! is_valid_rom(rom)) {
387 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400388 continue;
389 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500390 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
391 struct pnp_data *pnp = get_pnp_rom(rom);
392 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500393 // Legacy rom.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500394 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
395 , getRomPriority(sources, rom, 0));
Kevin O'Connor714325c2008-11-01 20:32:27 -0400396 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500397 }
Kevin O'Connor1e157252012-01-14 11:55:35 -0500398 // PnP rom - check for BEV and BCV boot capabilities.
399 int instance = 0;
400 while (pnp) {
401 if (pnp->bev)
402 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
403 , getRomPriority(sources, rom, instance++));
404 else if (pnp->bcv)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500405 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
406 , getRomPriority(sources, rom, instance++));
Kevin O'Connor1e157252012-01-14 11:55:35 -0500407 else
408 break;
409 pnp = get_pnp_next(rom, pnp);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500410 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400411 }
412}
413
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500414
415/****************************************************************
416 * VGA init
417 ****************************************************************/
418
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500419static int S3ResumeVga;
Kevin O'Connor422263d2011-07-05 20:56:07 -0400420int ScreenAndDebug;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400421struct rom_header *VgaROM;
Kevin O'Connor8b0c5092011-07-05 20:50:32 -0400422
Kevin O'Connor714325c2008-11-01 20:32:27 -0400423// Call into vga code to turn on console.
424void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500425vgarom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400426{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500427 if (! CONFIG_OPTIONROMS)
428 return;
429
Kevin O'Connor714325c2008-11-01 20:32:27 -0400430 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500431
Kevin O'Connor422263d2011-07-05 20:56:07 -0400432 // Load some config settings that impact VGA.
Kevin O'Connore010d852011-07-05 20:47:35 -0400433 EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
Kevin O'Connor897fb112013-02-07 23:32:48 -0500434 S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU);
Kevin O'Connor422263d2011-07-05 20:56:07 -0400435 ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
Kevin O'Connore010d852011-07-05 20:47:35 -0400436
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500437 if (CONFIG_OPTIONROMS_DEPLOYED) {
438 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400439 init_optionrom((void*)BUILD_ROM_START, 0, 1);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500440 } else {
Kevin O'Connore8264652010-09-15 22:06:19 -0400441 // Clear option rom memory
Kevin O'Connor3733f6f2013-02-17 12:44:23 -0500442 memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START);
Kevin O'Connore8264652010-09-15 22:06:19 -0400443
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500444 // Find and deploy PCI VGA rom.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400445 struct pci_device *pci;
446 foreachpci(pci) {
447 if (!is_pci_vga(pci))
448 continue;
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400449 vgahook_setup(pci);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400450 init_pcirom(pci, 1, NULL);
451 break;
452 }
Kevin O'Connor09880da2009-06-17 20:35:41 -0400453
454 // Find and deploy CBFS vga-style roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500455 run_file_roms("vgaroms/", 1, NULL);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400456 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400457 rom_reserve(0);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400458
Kevin O'Connor5e019082012-05-20 21:11:43 -0400459 if (rom_get_last() == BUILD_ROM_START)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400460 // No VGA rom found
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400461 return;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400462
Kevin O'Connor5e019082012-05-20 21:11:43 -0400463 VgaROM = (void*)BUILD_ROM_START;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400464 enable_vga_console();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400465}
Kevin O'Connord282af72009-07-04 04:10:32 -0400466
467void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500468s3_resume_vga(void)
Kevin O'Connord282af72009-07-04 04:10:32 -0400469{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500470 if (!S3ResumeVga)
Kevin O'Connord282af72009-07-04 04:10:32 -0400471 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400472 if (!VgaROM || ! is_valid_rom(VgaROM))
Kevin O'Connord282af72009-07-04 04:10:32 -0400473 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400474 callrom(VgaROM, 0);
Kevin O'Connord282af72009-07-04 04:10:32 -0400475}