blob: 7783f8467180e277b7d70ecd8cdc08243daba1d4 [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//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
8#include "bregs.h" // struct bregs
9#include "biosvar.h" // struct ipl_entry_s
10#include "util.h" // dprintf
Kevin O'Connorceea03c2008-11-08 21:36:35 -050011#include "pci.h" // pci_find_class
12#include "pci_regs.h" // PCI_ROM_ADDRESS
13#include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
14
15
16/****************************************************************
17 * Definitions
18 ****************************************************************/
Kevin O'Connor714325c2008-11-01 20:32:27 -040019
20// $PnP string with special alignment in romlayout.S
21extern char pnp_string[];
22
23struct 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_START 0xc0000
69#define OPTION_ROM_SIGNATURE 0xaa55
70#define OPTION_ROM_ALIGN 2048
71#define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
Kevin O'Connor5a1b8282008-11-29 20:39:06 -050072#define PCI_ROM_SIGNATURE 0x52494350 // PCIR
Kevin O'Connor62eea672008-12-06 11:57:45 -050073#define PCIROM_CODETYPE_X86 0
Kevin O'Connorceea03c2008-11-08 21:36:35 -050074
75// Next available position for an option rom.
76static u32 next_rom;
77
78
79/****************************************************************
80 * Helper functions
81 ****************************************************************/
82
Kevin O'Connor714325c2008-11-01 20:32:27 -040083// Execute a given option rom.
84static void
Kevin O'Connorceea03c2008-11-08 21:36:35 -050085callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040086{
Kevin O'Connorceea03c2008-11-08 21:36:35 -050087 u16 seg = FARPTR_TO_SEG(rom);
Kevin O'Connor714325c2008-11-01 20:32:27 -040088 dprintf(1, "Running option rom at %x:%x\n", seg, offset);
89
90 struct bregs br;
91 memset(&br, 0, sizeof(br));
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;
96 br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
97 br.cs = seg;
98 br.ip = offset;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -050099 // XXX - should call option rom in "big real mode".
Kevin O'Connor714325c2008-11-01 20:32:27 -0400100 call16(&br);
101
102 debug_serial_setup();
103
104 if (GET_BDA(ebda_seg) != SEG_EBDA)
105 BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
106 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
107}
108
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500109// Verify that an option rom looks valid
110static int
111is_valid_rom(struct rom_header *rom)
112{
113 if (rom->signature != OPTION_ROM_SIGNATURE)
114 return 0;
115 u32 len = rom->size * 512;
116 u8 sum = checksum((void*)rom, len);
117 if (sum != 0) {
118 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
119 , rom, len, sum);
120 return 0;
121 }
122 return 1;
123}
124
125// Check if a valid option rom has a pnp struct; return it if so.
126static struct pnp_data *
127get_pnp_rom(struct rom_header *rom)
128{
129 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
130 if (pnp->signature != *(u32*)pnp_string)
131 return NULL;
132 return pnp;
133}
134
Kevin O'Connor62eea672008-12-06 11:57:45 -0500135// Check if a valid option rom has a pci struct; return it if so.
136static struct pci_data *
137get_pci_rom(struct rom_header *rom)
138{
139 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
140 if (pci->signature != PCI_ROM_SIGNATURE)
141 return NULL;
142 return pci;
143}
144
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500145// Add a BEV vector for a given pnp compatible option rom.
146static void
147add_ipl(struct rom_header *rom, struct pnp_data *pnp)
148{
Kevin O'Connor714325c2008-11-01 20:32:27 -0400149#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
150
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500151 // Found a device that thinks it can boot the system. Record
152 // its BEV and product name string.
153
154 if (! CONFIG_BOOT)
155 return;
156
157 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
158 return;
159
160 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
161 ip->type = IPL_TYPE_BEV;
162 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
163
164 u16 desc = pnp->productname;
165 if (desc)
166 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
167
168 ebda->ipl.count++;
169}
170
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500171// Copy a rom to its permanent location below 1MiB
172static struct rom_header *
173copy_rom(struct rom_header *rom)
174{
175 u32 romsize = rom->size * 512;
176 if (next_rom + romsize > BUILD_BIOS_ADDR) {
177 // Option rom doesn't fit.
178 dprintf(1, "Option rom %p doesn't fit.\n", rom);
179 return NULL;
180 }
181 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
182 memcpy((void*)next_rom, rom, romsize);
183 return (struct rom_header *)next_rom;
184}
185
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500186// Check if an option rom is at a hardcoded location for a device.
187static struct rom_header *
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500188lookup_hardcode(u16 bdf)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500189{
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500190 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500191 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500192 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500193 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500194 // XXX - check LAR when in coreboot?
195 return NULL;
196}
197
198// Map the option rom of a given PCI device.
199static struct rom_header *
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500200map_optionrom(u16 bdf)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500201{
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500202 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
203
Kevin O'Connor62eea672008-12-06 11:57:45 -0500204 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
205 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
206 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
207 return NULL;
208 }
209
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500210 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
211 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
212 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500213
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500214 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500215 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500216 if (!sz || sz == 0xffffffff)
217 goto fail;
218
Kevin O'Connor62eea672008-12-06 11:57:45 -0500219 if (orig < 16*1024*1024) {
220 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 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
228 struct rom_header *rom = (struct rom_header *)orig;
229 for (;;) {
230 dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
231 , rom, vendev, bdf);
232 if (rom->signature != OPTION_ROM_SIGNATURE) {
233 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
234 goto fail;
235 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500236 struct pci_data *pci = get_pci_rom(rom);
237 if (! pci) {
238 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500239 goto fail;
240 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500241
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500242 u32 vd = (pci->device << 16) | pci->vendor;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500243 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500244 // A match
245 break;
246 dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
247 , vd, pci->type);
248 if (pci->indicator & 0x80) {
249 dprintf(6, "No more images left\n");
250 goto fail;
251 }
252 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500253 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500254
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500255 rom = copy_rom(rom);
256 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500257 return rom;
258fail:
259 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500260 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500261 return NULL;
262}
263
264// Attempt to map and initialize the option rom on a given PCI device.
265static struct rom_header *
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500266init_optionrom(u16 bdf)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500267{
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500268 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500269 struct rom_header *rom = lookup_hardcode(bdf);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500270 if (! rom)
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500271 rom = map_optionrom(bdf);
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) {
341 // Legacy rom - run init vector now.
342 callrom(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.
348 add_ipl(rom, pnp);
349 else if (pnp->bcv)
350 // Has BCV - run it now.
351 callrom(rom, pnp->bcv, 0);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400352 }
353}
354
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500355
356/****************************************************************
357 * VGA init
358 ****************************************************************/
359
Kevin O'Connor714325c2008-11-01 20:32:27 -0400360// Call into vga code to turn on console.
361void
362vga_setup()
363{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500364 if (! CONFIG_OPTIONROMS)
365 return;
366
Kevin O'Connor714325c2008-11-01 20:32:27 -0400367 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500368 next_rom = OPTION_ROM_START;
369
370 if (CONFIG_OPTIONROMS_DEPLOYED) {
371 // Option roms are already deployed on the system.
372 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
373 if (! is_valid_rom(rom))
374 return;
375 callrom(rom, OPTION_ROM_INITVECTOR, 0);
376 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
377 } else {
378 // Find and deploy PCI VGA rom.
Kevin O'Connor4132e022008-12-04 19:39:10 -0500379 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500380 if (bdf < 0)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500381 // Device not found
382 return;
383
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500384 struct rom_header *rom = init_optionrom(bdf);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500385 if (rom && !get_pnp_rom(rom))
386 // Call rom even if it isn't a pnp rom.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500387 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500388 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400389
390 dprintf(1, "Turning on vga console\n");
391 struct bregs br;
392 memset(&br, 0, sizeof(br));
393 br.ax = 0x0003;
394 call16_int(0x10, &br);
395
396 // Write to screen.
397 printf("Starting SeaBIOS\n\n");
398}