blob: 00697b24b3dcda200ed956d231863de0db30575e [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'Connor862d5fb2011-06-20 22:19:17 -040012#include "pci.h" // foreachpci
Kevin O'Connorceea03c2008-11-08 21:36:35 -050013#include "pci_regs.h" // PCI_ROM_ADDRESS
14#include "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'Connorceea03c2008-11-08 21:36:35 -050017
Kevin O'Connorceea03c2008-11-08 21:36:35 -050018
19/****************************************************************
20 * Helper functions
21 ****************************************************************/
22
Kevin O'Connor714325c2008-11-01 20:32:27 -040023// Execute a given option rom.
24static void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040025__callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040026{
Kevin O'Connor35ae7262009-01-19 15:44:44 -050027 u16 seg = FLATPTR_TO_SEG(rom);
Kevin O'Connor91b53a72009-05-05 22:52:09 -040028 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
Kevin O'Connor714325c2008-11-01 20:32:27 -040029
30 struct bregs br;
31 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040032 br.flags = F_IF;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050033 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040034 br.bx = 0xffff;
35 br.dx = 0xffff;
36 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050037 br.di = get_pnp_offset();
Kevin O'Connor9f985422009-09-09 11:34:39 -040038 br.code = SEGOFF(seg, offset);
Kevin O'Connorad901592009-12-13 11:25:25 -050039 start_preempt();
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -040040 farcall16big(&br);
Kevin O'Connorad901592009-12-13 11:25:25 -050041 finish_preempt();
Kevin O'Connor714325c2008-11-01 20:32:27 -040042
43 debug_serial_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -040044}
45
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040046// Execute a given option rom at the standard entry vector.
47static void
48callrom(struct rom_header *rom, u16 bdf)
49{
50 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
51}
52
Kevin O'Connor0a924122009-02-08 19:43:47 -050053// Execute a BCV option rom registered via add_bcv().
54void
55call_bcv(u16 seg, u16 ip)
56{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040057 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -050058}
59
Kevin O'Connore010d852011-07-05 20:47:35 -040060static int EnforceChecksum;
61
Kevin O'Connorceea03c2008-11-08 21:36:35 -050062// Verify that an option rom looks valid
63static int
64is_valid_rom(struct rom_header *rom)
65{
Kevin O'Connorc36273f2009-04-16 20:43:07 -040066 dprintf(6, "Checking rom %p (sig %x size %d)\n"
67 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050068 if (rom->signature != OPTION_ROM_SIGNATURE)
69 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -040070 if (! rom->size)
71 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050072 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050073 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050074 if (sum != 0) {
75 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
76 , rom, len, sum);
Kevin O'Connore010d852011-07-05 20:47:35 -040077 if (EnforceChecksum)
Kevin O'Connorcc975642011-03-06 19:22:46 -050078 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050079 }
80 return 1;
81}
82
83// Check if a valid option rom has a pnp struct; return it if so.
84static struct pnp_data *
85get_pnp_rom(struct rom_header *rom)
86{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040087 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050088 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -050089 return NULL;
90 return pnp;
91}
92
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -050093// Check for multiple pnp option rom headers.
94static struct pnp_data *
95get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
96{
97 if (! pnp->nextoffset)
98 return NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040099 pnp = (void*)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500100 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500101 return NULL;
102 return pnp;
103}
104
Kevin O'Connor62eea672008-12-06 11:57:45 -0500105// Check if a valid option rom has a pci struct; return it if so.
106static struct pci_data *
107get_pci_rom(struct rom_header *rom)
108{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400109 struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
110 if (pd->signature != PCI_ROM_SIGNATURE)
Kevin O'Connor62eea672008-12-06 11:57:45 -0500111 return NULL;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400112 return pd;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500113}
114
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400115// Run rom init code and note rom size.
116static int
117init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
118{
119 if (! is_valid_rom(rom))
120 return -1;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400121 struct rom_header *newrom = rom_reserve(rom->size * 512);
122 if (!newrom) {
123 warn_noalloc();
124 return -1;
125 }
126 if (newrom != rom)
127 memmove(newrom, rom, rom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400128
Kevin O'Connor5e019082012-05-20 21:11:43 -0400129 if (isvga || get_pnp_rom(newrom))
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400130 // Only init vga and PnP roms here.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400131 callrom(newrom, bdf);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400132
Kevin O'Connor5e019082012-05-20 21:11:43 -0400133 return rom_confirm(newrom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400134}
135
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500136#define RS_PCIROM (1LL<<33)
137
138static void
139setRomSource(u64 *sources, struct rom_header *rom, u64 source)
140{
141 if (sources)
142 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
143}
144
Kevin O'Connor031ef552010-12-27 19:26:57 -0500145static int
146getRomPriority(u64 *sources, struct rom_header *rom, int instance)
147{
148 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
149 if (!source)
150 return -1;
151 if (source & RS_PCIROM)
Kevin O'Connorfce91892011-07-09 14:47:47 -0400152 return bootprio_find_pci_rom((void*)(u32)source, instance);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400153 struct romfile_s *file = (void*)(u32)source;
154 return bootprio_find_named_rom(file->name, instance);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500155}
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400156
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400157
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400158/****************************************************************
159 * Roms in CBFS
160 ****************************************************************/
161
Kevin O'Connor5e019082012-05-20 21:11:43 -0400162static struct rom_header *
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400163deploy_romfile(struct romfile_s *file)
Kevin O'Connor5e019082012-05-20 21:11:43 -0400164{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400165 u32 size = file->size;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400166 struct rom_header *rom = rom_reserve(size);
167 if (!rom) {
168 warn_noalloc();
169 return NULL;
170 }
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400171 int ret = file->copy(file, rom, size);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400172 if (ret <= 0)
173 return NULL;
174 return rom;
175}
176
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400177// Check if an option rom is at a hardcoded location or in CBFS.
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500178static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400179lookup_hardcode(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500180{
Kevin O'Connore2304262010-06-13 16:05:17 -0400181 char fname[17];
182 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400183 , pci->vendor, pci->device);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400184 struct romfile_s *file = romfile_find(fname);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400185 if (file)
186 return deploy_romfile(file);
187 return NULL;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500188}
189
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400190// Run all roms in a given CBFS directory.
191static void
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500192run_file_roms(const char *prefix, int isvga, u64 *sources)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400193{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400194 struct romfile_s *file = NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400195 for (;;) {
Kevin O'Connore2304262010-06-13 16:05:17 -0400196 file = romfile_findprefix(prefix, file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400197 if (!file)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400198 break;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400199 struct rom_header *rom = deploy_romfile(file);
200 if (rom) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400201 setRomSource(sources, rom, (u32)file);
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500202 init_optionrom(rom, 0, isvga);
203 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400204 }
205}
206
207
208/****************************************************************
209 * PCI roms
210 ****************************************************************/
211
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400212// Verify device is a vga device with legacy address decoding enabled.
213static int
214is_pci_vga(struct pci_device *pci)
215{
216 if (pci->class != PCI_CLASS_DISPLAY_VGA)
217 return 0;
218 u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
219 if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
220 return 0;
221 while (pci->parent) {
222 pci = pci->parent;
223 u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
224 if (!(ctrl & PCI_BRIDGE_CTL_VGA))
225 return 0;
226 }
227 return 1;
228}
229
Kevin O'Connor5e019082012-05-20 21:11:43 -0400230// Copy a rom to its permanent location below 1MiB
231static struct rom_header *
232copy_rom(struct rom_header *rom)
233{
234 u32 romsize = rom->size * 512;
235 struct rom_header *newrom = rom_reserve(romsize);
236 if (!newrom) {
237 warn_noalloc();
238 return NULL;
239 }
240 dprintf(4, "Copying option rom (size %d) from %p to %p\n"
241 , romsize, rom, newrom);
242 iomemcpy(newrom, rom, romsize);
243 return newrom;
244}
245
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500246// Map the option rom of a given PCI device.
247static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400248map_pcirom(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500249{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400250 u16 bdf = pci->bdf;
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400251 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
252 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500253
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400254 if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
255 dprintf(6, "Skipping non-normal pci device (type=%x)\n"
256 , pci->header_type);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500257 return NULL;
258 }
259
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500260 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
261 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
262 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500263
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500264 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500265 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500266 if (!sz || sz == 0xffffffff)
267 goto fail;
268
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400269 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
270 // Don't try to map to a pci addresses at its max, in the last
271 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500272 dprintf(6, "Preset rom address doesn't look valid\n");
273 goto fail;
274 }
275
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500276 // Looks like a rom - enable it.
277 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500278
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400279 struct rom_header *rom = (void*)orig;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500280 for (;;) {
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400281 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
282 " bdf=%02x:%02x.%x)\n"
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400283 , rom, pci->vendor, pci->device
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400284 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500285 if (rom->signature != OPTION_ROM_SIGNATURE) {
286 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
287 goto fail;
288 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400289 struct pci_data *pd = get_pci_rom(rom);
290 if (! pd) {
Kevin O'Connor62eea672008-12-06 11:57:45 -0500291 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500292 goto fail;
293 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500294
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400295 if (pd->vendor == pci->vendor && pd->device == pci->device
296 && pd->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500297 // A match
298 break;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400299 dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
300 , pd->vendor, pd->device, pd->type);
301 if (pd->indicator & 0x80) {
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500302 dprintf(6, "No more images left\n");
303 goto fail;
304 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400305 rom = (void*)((u32)rom + pd->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500306 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500307
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500308 rom = copy_rom(rom);
309 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500310 return rom;
311fail:
312 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500313 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500314 return NULL;
315}
316
317// Attempt to map and initialize the option rom on a given PCI device.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400318static int
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400319init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500320{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400321 u16 bdf = pci->bdf;
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400322 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400323 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400324 , pci->vendor, pci->device);
325 struct rom_header *rom = lookup_hardcode(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500326 if (! rom)
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400327 rom = map_pcirom(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500328 if (! rom)
329 // No ROM present.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400330 return -1;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400331 setRomSource(sources, rom, RS_PCIROM | (u32)pci);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400332 return init_optionrom(rom, bdf, isvga);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500333}
334
335
336/****************************************************************
337 * Non-VGA option rom init
338 ****************************************************************/
339
340void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500341optionrom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400342{
343 if (! CONFIG_OPTIONROMS)
344 return;
345
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500346 dprintf(1, "Scan for option roms\n");
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500347 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
348 memset(sources, 0, sizeof(sources));
Kevin O'Connor5e019082012-05-20 21:11:43 -0400349 u32 post_vga = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500350
351 if (CONFIG_OPTIONROMS_DEPLOYED) {
352 // Option roms are already deployed on the system.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400353 u32 pos = post_vga;
354 while (pos < rom_get_top()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400355 int ret = init_optionrom((void*)pos, 0, 0);
356 if (ret)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500357 pos += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400358 else
Kevin O'Connor5e019082012-05-20 21:11:43 -0400359 pos = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500360 }
361 } else {
362 // Find and deploy PCI roms.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400363 struct pci_device *pci;
364 foreachpci(pci) {
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400365 if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500366 continue;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400367 init_pcirom(pci, 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500368 }
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400369
370 // Find and deploy CBFS roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500371 run_file_roms("genroms/", 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500372 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400373 rom_reserve(0);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500374
375 // All option roms found and deployed - now build BEV/BCV vectors.
376
377 u32 pos = post_vga;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400378 while (pos < rom_get_last()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400379 struct rom_header *rom = (void*)pos;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500380 if (! is_valid_rom(rom)) {
381 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400382 continue;
383 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500384 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
385 struct pnp_data *pnp = get_pnp_rom(rom);
386 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500387 // Legacy rom.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500388 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
389 , getRomPriority(sources, rom, 0));
Kevin O'Connor714325c2008-11-01 20:32:27 -0400390 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500391 }
Kevin O'Connor1e157252012-01-14 11:55:35 -0500392 // PnP rom - check for BEV and BCV boot capabilities.
393 int instance = 0;
394 while (pnp) {
395 if (pnp->bev)
396 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
397 , getRomPriority(sources, rom, instance++));
398 else if (pnp->bcv)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500399 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
400 , getRomPriority(sources, rom, instance++));
Kevin O'Connor1e157252012-01-14 11:55:35 -0500401 else
402 break;
403 pnp = get_pnp_next(rom, pnp);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500404 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400405 }
406}
407
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500408
409/****************************************************************
410 * VGA init
411 ****************************************************************/
412
Kevin O'Connor8b0c5092011-07-05 20:50:32 -0400413static int S3ResumeVgaInit;
Kevin O'Connor422263d2011-07-05 20:56:07 -0400414int ScreenAndDebug;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400415struct rom_header *VgaROM;
Kevin O'Connor8b0c5092011-07-05 20:50:32 -0400416
Kevin O'Connor714325c2008-11-01 20:32:27 -0400417// Call into vga code to turn on console.
418void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500419vga_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400420{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500421 if (! CONFIG_OPTIONROMS)
422 return;
423
Kevin O'Connor714325c2008-11-01 20:32:27 -0400424 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500425
Kevin O'Connor422263d2011-07-05 20:56:07 -0400426 // Load some config settings that impact VGA.
Kevin O'Connore010d852011-07-05 20:47:35 -0400427 EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
Gleb Natapova5abf9b2012-02-13 13:01:07 +0200428 S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", !CONFIG_COREBOOT);
Kevin O'Connor422263d2011-07-05 20:56:07 -0400429 ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
Kevin O'Connore010d852011-07-05 20:47:35 -0400430
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500431 if (CONFIG_OPTIONROMS_DEPLOYED) {
432 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400433 init_optionrom((void*)BUILD_ROM_START, 0, 1);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500434 } else {
Kevin O'Connore8264652010-09-15 22:06:19 -0400435 // Clear option rom memory
Kevin O'Connor5e019082012-05-20 21:11:43 -0400436 memset((void*)BUILD_ROM_START, 0, rom_get_top() - BUILD_ROM_START);
Kevin O'Connore8264652010-09-15 22:06:19 -0400437
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500438 // Find and deploy PCI VGA rom.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400439 struct pci_device *pci;
440 foreachpci(pci) {
441 if (!is_pci_vga(pci))
442 continue;
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400443 vgahook_setup(pci);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400444 init_pcirom(pci, 1, NULL);
445 break;
446 }
Kevin O'Connor09880da2009-06-17 20:35:41 -0400447
448 // Find and deploy CBFS vga-style roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500449 run_file_roms("vgaroms/", 1, NULL);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400450 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400451 rom_reserve(0);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400452
Kevin O'Connor5e019082012-05-20 21:11:43 -0400453 if (rom_get_last() == BUILD_ROM_START)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400454 // No VGA rom found
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400455 return;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400456
Kevin O'Connor5e019082012-05-20 21:11:43 -0400457 VgaROM = (void*)BUILD_ROM_START;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400458 enable_vga_console();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400459}
Kevin O'Connord282af72009-07-04 04:10:32 -0400460
461void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500462s3_resume_vga_init(void)
Kevin O'Connord282af72009-07-04 04:10:32 -0400463{
Kevin O'Connor8b0c5092011-07-05 20:50:32 -0400464 if (!S3ResumeVgaInit)
Kevin O'Connord282af72009-07-04 04:10:32 -0400465 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400466 if (!VgaROM || ! is_valid_rom(VgaROM))
Kevin O'Connord282af72009-07-04 04:10:32 -0400467 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400468 callrom(VgaROM, 0);
Kevin O'Connord282af72009-07-04 04:10:32 -0400469}