blob: ff74c4fdc54dfbfb84419409d5a2fcd9580174f0 [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'Connorbaac6b62011-06-19 10:46:28 -040012#include "pci.h" // foreachbdf
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
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +010016#include "paravirt.h" // qemu_cfg_*
Kevin O'Connorceea03c2008-11-08 21:36:35 -050017
18
19/****************************************************************
20 * Definitions
21 ****************************************************************/
Kevin O'Connor714325c2008-11-01 20:32:27 -040022
Kevin O'Connor714325c2008-11-01 20:32:27 -040023struct 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
32struct 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
48struct 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'Connorceea03c2008-11-08 21:36:35 -050068#define OPTION_ROM_SIGNATURE 0xaa55
69#define OPTION_ROM_ALIGN 2048
70#define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
Kevin O'Connor5a1b8282008-11-29 20:39:06 -050071#define PCI_ROM_SIGNATURE 0x52494350 // PCIR
Kevin O'Connor62eea672008-12-06 11:57:45 -050072#define PCIROM_CODETYPE_X86 0
Kevin O'Connorceea03c2008-11-08 21:36:35 -050073
Kevin O'Connore7739302009-07-26 19:16:09 -040074// The end of the last deployed rom.
Kevin O'Connor4d96edc2010-09-25 14:53:15 -040075u32 RomEnd = BUILD_ROM_START;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050076
77
78/****************************************************************
79 * Helper functions
80 ****************************************************************/
81
Kevin O'Connor714325c2008-11-01 20:32:27 -040082// Execute a given option rom.
83static void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040084__callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040085{
Kevin O'Connor35ae7262009-01-19 15:44:44 -050086 u16 seg = FLATPTR_TO_SEG(rom);
Kevin O'Connor91b53a72009-05-05 22:52:09 -040087 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
Kevin O'Connor714325c2008-11-01 20:32:27 -040088
89 struct bregs br;
90 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040091 br.flags = F_IF;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050092 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040093 br.bx = 0xffff;
94 br.dx = 0xffff;
95 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050096 br.di = get_pnp_offset();
Kevin O'Connor9f985422009-09-09 11:34:39 -040097 br.code = SEGOFF(seg, offset);
Kevin O'Connorad901592009-12-13 11:25:25 -050098 start_preempt();
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050099 call16big(&br);
Kevin O'Connorad901592009-12-13 11:25:25 -0500100 finish_preempt();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400101
102 debug_serial_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400103}
104
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400105// Execute a given option rom at the standard entry vector.
106static void
107callrom(struct rom_header *rom, u16 bdf)
108{
109 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
110}
111
Kevin O'Connor0a924122009-02-08 19:43:47 -0500112// Execute a BCV option rom registered via add_bcv().
113void
114call_bcv(u16 seg, u16 ip)
115{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400116 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500117}
118
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500119// Verify that an option rom looks valid
120static int
121is_valid_rom(struct rom_header *rom)
122{
Kevin O'Connorc36273f2009-04-16 20:43:07 -0400123 dprintf(6, "Checking rom %p (sig %x size %d)\n"
124 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500125 if (rom->signature != OPTION_ROM_SIGNATURE)
126 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -0400127 if (! rom->size)
128 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500129 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500130 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500131 if (sum != 0) {
132 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
133 , rom, len, sum);
Kevin O'Connorcc975642011-03-06 19:22:46 -0500134 if (CONFIG_OPTIONROMS_CHECKSUM)
135 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500136 }
137 return 1;
138}
139
140// Check if a valid option rom has a pnp struct; return it if so.
141static struct pnp_data *
142get_pnp_rom(struct rom_header *rom)
143{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400144 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500145 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500146 return NULL;
147 return pnp;
148}
149
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500150// Check for multiple pnp option rom headers.
151static struct pnp_data *
152get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
153{
154 if (! pnp->nextoffset)
155 return NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400156 pnp = (void*)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500157 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500158 return NULL;
159 return pnp;
160}
161
Kevin O'Connor62eea672008-12-06 11:57:45 -0500162// Check if a valid option rom has a pci struct; return it if so.
163static struct pci_data *
164get_pci_rom(struct rom_header *rom)
165{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400166 struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500167 if (pci->signature != PCI_ROM_SIGNATURE)
168 return NULL;
169 return pci;
170}
171
Kevin O'Connore8264652010-09-15 22:06:19 -0400172// Return start of code in 0xc0000-0xf0000 space.
173static inline u32 _max_rom(void) {
174 extern u8 code32flat_start[], code32init_end[];
Kevin O'Connor4a446d72010-09-25 12:46:38 -0400175 return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start;
Kevin O'Connore8264652010-09-15 22:06:19 -0400176}
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400177// Return the memory position up to which roms may be located.
Kevin O'Connore8264652010-09-15 22:06:19 -0400178static inline u32 max_rom(void) {
179 u32 end = _max_rom();
180 return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end;
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400181}
182
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500183// Copy a rom to its permanent location below 1MiB
184static struct rom_header *
185copy_rom(struct rom_header *rom)
186{
187 u32 romsize = rom->size * 512;
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400188 if (RomEnd + romsize > max_rom()) {
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500189 // Option rom doesn't fit.
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500190 warn_noalloc();
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500191 return NULL;
192 }
Kevin O'Connorc36273f2009-04-16 20:43:07 -0400193 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
Kevin O'Connore7739302009-07-26 19:16:09 -0400194 , romsize, rom, RomEnd);
Kevin O'Connor34036962009-12-05 18:51:53 -0500195 iomemcpy((void*)RomEnd, rom, romsize);
Kevin O'Connore7739302009-07-26 19:16:09 -0400196 return (void*)RomEnd;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500197}
198
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400199// Run rom init code and note rom size.
200static int
201init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
202{
203 if (! is_valid_rom(rom))
204 return -1;
205
206 if (isvga || get_pnp_rom(rom))
207 // Only init vga and PnP roms here.
208 callrom(rom, bdf);
209
Kevin O'Connore7739302009-07-26 19:16:09 -0400210 RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400211
212 return 0;
213}
214
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500215#define RS_PCIROM (1LL<<33)
216
217static void
218setRomSource(u64 *sources, struct rom_header *rom, u64 source)
219{
220 if (sources)
221 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
222}
223
Kevin O'Connor031ef552010-12-27 19:26:57 -0500224static int
225getRomPriority(u64 *sources, struct rom_header *rom, int instance)
226{
227 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
228 if (!source)
229 return -1;
230 if (source & RS_PCIROM)
231 return bootprio_find_pci_rom(source, instance);
232 return bootprio_find_named_rom(romfile_name(source), instance);
233}
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400234
235/****************************************************************
236 * Roms in CBFS
237 ****************************************************************/
238
239// Check if an option rom is at a hardcoded location or in CBFS.
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500240static struct rom_header *
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400241lookup_hardcode(u32 vendev)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500242{
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400243 if (OPTIONROM_VENDEV_1
244 && ((OPTIONROM_VENDEV_1 >> 16)
245 | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400246 return copy_rom((void*)OPTIONROM_MEM_1);
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400247 if (OPTIONROM_VENDEV_2
248 && ((OPTIONROM_VENDEV_2 >> 16)
249 | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400250 return copy_rom((void*)OPTIONROM_MEM_2);
Kevin O'Connore2304262010-06-13 16:05:17 -0400251 char fname[17];
252 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
253 , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
254 int ret = romfile_copy(romfile_find(fname), (void*)RomEnd
255 , max_rom() - RomEnd);
256 if (ret <= 0)
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400257 return NULL;
Kevin O'Connore7739302009-07-26 19:16:09 -0400258 return (void*)RomEnd;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500259}
260
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400261// Run all roms in a given CBFS directory.
262static void
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500263run_file_roms(const char *prefix, int isvga, u64 *sources)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400264{
Kevin O'Connore2304262010-06-13 16:05:17 -0400265 u32 file = 0;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400266 for (;;) {
Kevin O'Connore2304262010-06-13 16:05:17 -0400267 file = romfile_findprefix(prefix, file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400268 if (!file)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400269 break;
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500270 struct rom_header *rom = (void*)RomEnd;
271 int ret = romfile_copy(file, rom, max_rom() - RomEnd);
272 if (ret > 0) {
273 setRomSource(sources, rom, file);
274 init_optionrom(rom, 0, isvga);
275 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400276 }
277}
278
279
280/****************************************************************
281 * PCI roms
282 ****************************************************************/
283
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500284// Map the option rom of a given PCI device.
285static struct rom_header *
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400286map_pcirom(u16 bdf, u32 vendev)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500287{
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400288 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
289 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500290
Kevin O'Connor62eea672008-12-06 11:57:45 -0500291 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
292 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
293 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
294 return NULL;
295 }
296
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500297 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
298 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
299 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500300
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500301 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500302 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500303 if (!sz || sz == 0xffffffff)
304 goto fail;
305
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400306 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
307 // Don't try to map to a pci addresses at its max, in the last
308 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500309 dprintf(6, "Preset rom address doesn't look valid\n");
310 goto fail;
311 }
312
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500313 // Looks like a rom - enable it.
314 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500315
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400316 struct rom_header *rom = (void*)orig;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500317 for (;;) {
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400318 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
319 " bdf=%02x:%02x.%x)\n"
320 , rom, pci_vd_to_ven(vendev), pci_vd_to_dev(vendev)
321 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500322 if (rom->signature != OPTION_ROM_SIGNATURE) {
323 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
324 goto fail;
325 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500326 struct pci_data *pci = get_pci_rom(rom);
327 if (! pci) {
328 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500329 goto fail;
330 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500331
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400332 u32 vd = pci_vd(pci->vendor, pci->device);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500333 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500334 // A match
335 break;
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400336 dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500337 , vd, pci->type);
338 if (pci->indicator & 0x80) {
339 dprintf(6, "No more images left\n");
340 goto fail;
341 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400342 rom = (void*)((u32)rom + pci->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500343 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500344
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500345 rom = copy_rom(rom);
346 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500347 return rom;
348fail:
349 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500350 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500351 return NULL;
352}
353
354// Attempt to map and initialize the option rom on a given PCI device.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400355static int
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500356init_pcirom(u16 bdf, int isvga, u64 *sources)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500357{
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400358 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400359 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400360 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400361 , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400362 struct rom_header *rom = lookup_hardcode(vendev);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500363 if (! rom)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400364 rom = map_pcirom(bdf, vendev);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500365 if (! rom)
366 // No ROM present.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400367 return -1;
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500368 setRomSource(sources, rom, RS_PCIROM | bdf);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400369 return init_optionrom(rom, bdf, isvga);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500370}
371
372
373/****************************************************************
374 * Non-VGA option rom init
375 ****************************************************************/
376
377void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500378optionrom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400379{
380 if (! CONFIG_OPTIONROMS)
381 return;
382
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500383 dprintf(1, "Scan for option roms\n");
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500384 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
385 memset(sources, 0, sizeof(sources));
Kevin O'Connore7739302009-07-26 19:16:09 -0400386 u32 post_vga = RomEnd;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500387
388 if (CONFIG_OPTIONROMS_DEPLOYED) {
389 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400390 u32 pos = RomEnd;
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400391 while (pos < max_rom()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400392 int ret = init_optionrom((void*)pos, 0, 0);
393 if (ret)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500394 pos += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400395 else
Kevin O'Connore7739302009-07-26 19:16:09 -0400396 pos = RomEnd;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500397 }
398 } else {
399 // Find and deploy PCI roms.
Kevin O'Connore6338322008-11-29 20:31:49 -0500400 int bdf, max;
Kevin O'Connorbaac6b62011-06-19 10:46:28 -0400401 foreachbdf(bdf, max) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500402 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500403 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
404 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500405 continue;
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500406 init_pcirom(bdf, 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500407 }
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400408
409 // Find and deploy CBFS roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500410 run_file_roms("genroms/", 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500411 }
412
413 // All option roms found and deployed - now build BEV/BCV vectors.
414
415 u32 pos = post_vga;
Kevin O'Connore7739302009-07-26 19:16:09 -0400416 while (pos < RomEnd) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400417 struct rom_header *rom = (void*)pos;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500418 if (! is_valid_rom(rom)) {
419 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400420 continue;
421 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500422 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
423 struct pnp_data *pnp = get_pnp_rom(rom);
424 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500425 // Legacy rom.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500426 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
427 , getRomPriority(sources, rom, 0));
Kevin O'Connor714325c2008-11-01 20:32:27 -0400428 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500429 }
430 // PnP rom.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500431 if (pnp->bev) {
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500432 // Can boot system - add to IPL list.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500433 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
434 , getRomPriority(sources, rom, 0));
435 } else {
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500436 // Check for BCV (there may be multiple).
Kevin O'Connor031ef552010-12-27 19:26:57 -0500437 int instance = 0;
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500438 while (pnp && pnp->bcv) {
Kevin O'Connor031ef552010-12-27 19:26:57 -0500439 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
440 , getRomPriority(sources, rom, instance++));
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500441 pnp = get_pnp_next(rom, pnp);
442 }
Kevin O'Connor031ef552010-12-27 19:26:57 -0500443 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400444 }
445}
446
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500447
448/****************************************************************
449 * VGA init
450 ****************************************************************/
451
Kevin O'Connor714325c2008-11-01 20:32:27 -0400452// Call into vga code to turn on console.
453void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500454vga_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400455{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500456 if (! CONFIG_OPTIONROMS)
457 return;
458
Kevin O'Connor714325c2008-11-01 20:32:27 -0400459 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500460
461 if (CONFIG_OPTIONROMS_DEPLOYED) {
462 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400463 init_optionrom((void*)BUILD_ROM_START, 0, 1);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500464 } else {
Kevin O'Connore8264652010-09-15 22:06:19 -0400465 // Clear option rom memory
466 memset((void*)RomEnd, 0, _max_rom() - RomEnd);
467
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500468 // Find and deploy PCI VGA rom.
Kevin O'Connor04eece22009-07-19 19:05:30 -0400469 int bdf = VGAbdf = pci_find_vga();
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400470 if (bdf >= 0)
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500471 init_pcirom(bdf, 1, NULL);
Kevin O'Connor09880da2009-06-17 20:35:41 -0400472
473 // Find and deploy CBFS vga-style roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500474 run_file_roms("vgaroms/", 1, NULL);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400475 }
476
Kevin O'Connore7739302009-07-26 19:16:09 -0400477 if (RomEnd == BUILD_ROM_START) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400478 // No VGA rom found
Kevin O'Connore7739302009-07-26 19:16:09 -0400479 RomEnd += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400480 return;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500481 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400482
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400483 enable_vga_console();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400484}
Kevin O'Connord282af72009-07-04 04:10:32 -0400485
486void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500487s3_resume_vga_init(void)
Kevin O'Connord282af72009-07-04 04:10:32 -0400488{
489 if (!CONFIG_S3_RESUME_VGA_INIT)
490 return;
Kevin O'Connore7739302009-07-26 19:16:09 -0400491 struct rom_header *rom = (void*)BUILD_ROM_START;
Kevin O'Connord282af72009-07-04 04:10:32 -0400492 if (! is_valid_rom(rom))
493 return;
494 callrom(rom, 0);
495}