blob: def3febb457c9fa1d6cd8f5124f521a1ae169a01 [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'Connorceea03c2008-11-08 21:36:35 -050012#include "pci.h" // pci_find_class
13#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
Kevin O'Connorceea03c2008-11-08 21:36:35 -050016
17
18/****************************************************************
19 * Definitions
20 ****************************************************************/
Kevin O'Connor714325c2008-11-01 20:32:27 -040021
Kevin O'Connor714325c2008-11-01 20:32:27 -040022struct rom_header {
23 u16 signature;
24 u8 size;
25 u8 initVector[4];
26 u8 reserved[17];
27 u16 pcioffset;
28 u16 pnpoffset;
29} PACKED;
30
31struct pci_data {
32 u32 signature;
33 u16 vendor;
34 u16 device;
35 u16 vitaldata;
36 u16 dlen;
37 u8 drevision;
38 u8 class_lo;
39 u16 class_hi;
40 u16 ilen;
41 u16 irevision;
42 u8 type;
43 u8 indicator;
44 u16 reserved;
45} PACKED;
46
47struct pnp_data {
48 u32 signature;
49 u8 revision;
50 u8 len;
51 u16 nextoffset;
52 u8 reserved_08;
53 u8 checksum;
54 u32 devid;
55 u16 manufacturer;
56 u16 productname;
57 u8 type_lo;
58 u16 type_hi;
59 u8 dev_flags;
60 u16 bcv;
61 u16 dv;
62 u16 bev;
63 u16 reserved_1c;
64 u16 staticresource;
65} PACKED;
66
Kevin O'Connorceea03c2008-11-08 21:36:35 -050067#define OPTION_ROM_START 0xc0000
68#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
74// Next available position for an option rom.
75static u32 next_rom;
76
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'Connorceea03c2008-11-08 21:36:35 -050084callrom(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'Connor714325c2008-11-01 20:32:27 -040087 dprintf(1, "Running option rom at %x:%x\n", seg, offset);
88
89 struct bregs br;
90 memset(&br, 0, sizeof(br));
Kevin O'Connorceea03c2008-11-08 21:36:35 -050091 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040092 br.bx = 0xffff;
93 br.dx = 0xffff;
94 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050095 br.di = get_pnp_offset();
Kevin O'Connor714325c2008-11-01 20:32:27 -040096 br.cs = seg;
97 br.ip = offset;
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050098 call16big(&br);
Kevin O'Connor714325c2008-11-01 20:32:27 -040099
100 debug_serial_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400101}
102
Kevin O'Connor0a924122009-02-08 19:43:47 -0500103// Execute a BCV option rom registered via add_bcv().
104void
105call_bcv(u16 seg, u16 ip)
106{
107 callrom(MAKE_FLATPTR(seg, 0), ip, 0);
108}
109
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500110// Verify that an option rom looks valid
111static int
112is_valid_rom(struct rom_header *rom)
113{
Kevin O'Connorc36273f2009-04-16 20:43:07 -0400114 dprintf(6, "Checking rom %p (sig %x size %d)\n"
115 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500116 if (rom->signature != OPTION_ROM_SIGNATURE)
117 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -0400118 if (! rom->size)
119 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500120 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500121 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500122 if (sum != 0) {
123 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
124 , rom, len, sum);
125 return 0;
126 }
127 return 1;
128}
129
130// Check if a valid option rom has a pnp struct; return it if so.
131static struct pnp_data *
132get_pnp_rom(struct rom_header *rom)
133{
134 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500135 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500136 return NULL;
137 return pnp;
138}
139
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500140// Check for multiple pnp option rom headers.
141static struct pnp_data *
142get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
143{
144 if (! pnp->nextoffset)
145 return NULL;
146 pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500147 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500148 return NULL;
149 return pnp;
150}
151
Kevin O'Connor62eea672008-12-06 11:57:45 -0500152// Check if a valid option rom has a pci struct; return it if so.
153static struct pci_data *
154get_pci_rom(struct rom_header *rom)
155{
156 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
157 if (pci->signature != PCI_ROM_SIGNATURE)
158 return NULL;
159 return pci;
160}
161
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500162// Copy a rom to its permanent location below 1MiB
163static struct rom_header *
164copy_rom(struct rom_header *rom)
165{
166 u32 romsize = rom->size * 512;
167 if (next_rom + romsize > BUILD_BIOS_ADDR) {
168 // Option rom doesn't fit.
169 dprintf(1, "Option rom %p doesn't fit.\n", rom);
170 return NULL;
171 }
Kevin O'Connorc36273f2009-04-16 20:43:07 -0400172 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
173 , romsize, rom, next_rom);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500174 memcpy((void*)next_rom, rom, romsize);
175 return (struct rom_header *)next_rom;
176}
177
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500178// Check if an option rom is at a hardcoded location for a device.
179static struct rom_header *
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400180lookup_hardcode(u32 vendev)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500181{
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400182 if (OPTIONROM_VENDEV_1
183 && ((OPTIONROM_VENDEV_1 >> 16)
184 | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500185 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400186 if (OPTIONROM_VENDEV_2
187 && ((OPTIONROM_VENDEV_2 >> 16)
188 | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500189 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400190 struct rom_header *rom = cb_find_optionrom(vendev);
191 if (rom)
192 return copy_rom(rom);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500193 return NULL;
194}
195
196// Map the option rom of a given PCI device.
197static struct rom_header *
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400198map_optionrom(u16 bdf, u32 vendev)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500199{
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500200 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
201
Kevin O'Connor62eea672008-12-06 11:57:45 -0500202 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
203 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
204 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
205 return NULL;
206 }
207
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500208 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
209 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
210 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500211
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500212 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500213 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500214 if (!sz || sz == 0xffffffff)
215 goto fail;
216
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400217 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
218 // Don't try to map to a pci addresses at its max, in the last
219 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500220 dprintf(6, "Preset rom address doesn't look valid\n");
221 goto fail;
222 }
223
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500224 // Looks like a rom - enable it.
225 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500226
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500227 struct rom_header *rom = (struct rom_header *)orig;
228 for (;;) {
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400229 dprintf(5, "Inspecting possible rom at %p (dv=%x bdf=%x)\n"
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500230 , rom, vendev, bdf);
231 if (rom->signature != OPTION_ROM_SIGNATURE) {
232 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
233 goto fail;
234 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500235 struct pci_data *pci = get_pci_rom(rom);
236 if (! pci) {
237 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500238 goto fail;
239 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500240
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500241 u32 vd = (pci->device << 16) | pci->vendor;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500242 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500243 // A match
244 break;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400245 dprintf(6, "Didn't match dev/ven (got %x) or type (got %d)\n"
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500246 , vd, pci->type);
247 if (pci->indicator & 0x80) {
248 dprintf(6, "No more images left\n");
249 goto fail;
250 }
251 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500252 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500253
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500254 rom = copy_rom(rom);
255 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500256 return rom;
257fail:
258 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500259 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500260 return NULL;
261}
262
263// Attempt to map and initialize the option rom on a given PCI device.
264static struct rom_header *
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500265init_optionrom(u16 bdf)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500266{
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400267 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
268 dprintf(4, "Attempting to init PCI bdf %x (dev/ven %x)\n", bdf, vendev);
269 struct rom_header *rom = lookup_hardcode(vendev);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500270 if (! rom)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400271 rom = map_optionrom(bdf, vendev);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500272 if (! rom)
273 // No ROM present.
274 return NULL;
275
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500276 if (! is_valid_rom(rom))
277 return NULL;
278
279 if (get_pnp_rom(rom))
280 // Init the PnP rom.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500281 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500282
283 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
284
285 return rom;
286}
287
288
289/****************************************************************
290 * Non-VGA option rom init
291 ****************************************************************/
292
293void
294optionrom_setup()
Kevin O'Connor714325c2008-11-01 20:32:27 -0400295{
296 if (! CONFIG_OPTIONROMS)
297 return;
298
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500299 dprintf(1, "Scan for option roms\n");
300
301 u32 post_vga = next_rom;
302
303 if (CONFIG_OPTIONROMS_DEPLOYED) {
304 // Option roms are already deployed on the system.
305 u32 pos = next_rom;
306 while (pos < BUILD_BIOS_ADDR) {
307 struct rom_header *rom = (struct rom_header *)pos;
308 if (! is_valid_rom(rom)) {
309 pos += OPTION_ROM_ALIGN;
310 continue;
311 }
312 if (get_pnp_rom(rom))
313 callrom(rom, OPTION_ROM_INITVECTOR, 0);
314 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
315 next_rom = pos;
316 }
317 } else {
318 // Find and deploy PCI roms.
Kevin O'Connore6338322008-11-29 20:31:49 -0500319 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500320 foreachpci(bdf, max) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500321 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500322 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
323 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500324 continue;
325 init_optionrom(bdf);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500326 }
327 }
328
329 // All option roms found and deployed - now build BEV/BCV vectors.
330
331 u32 pos = post_vga;
332 while (pos < next_rom) {
333 struct rom_header *rom = (struct rom_header *)pos;
334 if (! is_valid_rom(rom)) {
335 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400336 continue;
337 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500338 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
339 struct pnp_data *pnp = get_pnp_rom(rom);
340 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500341 // Legacy rom.
342 add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400343 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500344 }
345 // PnP rom.
346 if (pnp->bev)
347 // Can boot system - add to IPL list.
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500348 add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500349 else
350 // Check for BCV (there may be multiple).
351 while (pnp && pnp->bcv) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500352 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500353 pnp = get_pnp_next(rom, pnp);
354 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400355 }
356}
357
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500358
359/****************************************************************
360 * VGA init
361 ****************************************************************/
362
Kevin O'Connor714325c2008-11-01 20:32:27 -0400363// Call into vga code to turn on console.
364void
365vga_setup()
366{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500367 if (! CONFIG_OPTIONROMS)
368 return;
369
Kevin O'Connor714325c2008-11-01 20:32:27 -0400370 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500371 next_rom = OPTION_ROM_START;
372
373 if (CONFIG_OPTIONROMS_DEPLOYED) {
374 // Option roms are already deployed on the system.
375 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
376 if (! is_valid_rom(rom))
377 return;
378 callrom(rom, OPTION_ROM_INITVECTOR, 0);
379 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
380 } else {
381 // Find and deploy PCI VGA rom.
Kevin O'Connor4132e022008-12-04 19:39:10 -0500382 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500383 if (bdf < 0)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500384 // Device not found
385 return;
386
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500387 struct rom_header *rom = init_optionrom(bdf);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500388 if (rom && !get_pnp_rom(rom))
389 // Call rom even if it isn't a pnp rom.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500390 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500391 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400392
393 dprintf(1, "Turning on vga console\n");
394 struct bregs br;
395 memset(&br, 0, sizeof(br));
396 br.ax = 0x0003;
397 call16_int(0x10, &br);
398
399 // Write to screen.
400 printf("Starting SeaBIOS\n\n");
401}