blob: aa6f8bb85da9e0a070dffc897bab0a737fd6681d [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 OPTIONROM_BDF_1 0x0000
69#define OPTIONROM_MEM_1 0x00000000
70#define OPTIONROM_BDF_2 0x0000
71#define OPTIONROM_MEM_2 0x00000000
72
73#define OPTION_ROM_START 0xc0000
74#define OPTION_ROM_SIGNATURE 0xaa55
75#define OPTION_ROM_ALIGN 2048
76#define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
77
78// Next available position for an option rom.
79static u32 next_rom;
80
81
82/****************************************************************
83 * Helper functions
84 ****************************************************************/
85
Kevin O'Connor714325c2008-11-01 20:32:27 -040086// Execute a given option rom.
87static void
Kevin O'Connorceea03c2008-11-08 21:36:35 -050088callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040089{
Kevin O'Connorceea03c2008-11-08 21:36:35 -050090 u16 seg = FARPTR_TO_SEG(rom);
Kevin O'Connor714325c2008-11-01 20:32:27 -040091 dprintf(1, "Running option rom at %x:%x\n", seg, offset);
92
93 struct bregs br;
94 memset(&br, 0, sizeof(br));
Kevin O'Connorceea03c2008-11-08 21:36:35 -050095 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040096 br.bx = 0xffff;
97 br.dx = 0xffff;
98 br.es = SEG_BIOS;
99 br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
100 br.cs = seg;
101 br.ip = offset;
102 call16(&br);
103
104 debug_serial_setup();
105
106 if (GET_BDA(ebda_seg) != SEG_EBDA)
107 BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
108 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
109}
110
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500111// Verify that an option rom looks valid
112static int
113is_valid_rom(struct rom_header *rom)
114{
115 if (rom->signature != OPTION_ROM_SIGNATURE)
116 return 0;
117 u32 len = rom->size * 512;
118 u8 sum = checksum((void*)rom, len);
119 if (sum != 0) {
120 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
121 , rom, len, sum);
122 return 0;
123 }
124 return 1;
125}
126
127// Check if a valid option rom has a pnp struct; return it if so.
128static struct pnp_data *
129get_pnp_rom(struct rom_header *rom)
130{
131 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
132 if (pnp->signature != *(u32*)pnp_string)
133 return NULL;
134 return pnp;
135}
136
137// Add a BEV vector for a given pnp compatible option rom.
138static void
139add_ipl(struct rom_header *rom, struct pnp_data *pnp)
140{
Kevin O'Connor714325c2008-11-01 20:32:27 -0400141#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
142
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500143 // Found a device that thinks it can boot the system. Record
144 // its BEV and product name string.
145
146 if (! CONFIG_BOOT)
147 return;
148
149 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
150 return;
151
152 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
153 ip->type = IPL_TYPE_BEV;
154 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
155
156 u16 desc = pnp->productname;
157 if (desc)
158 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
159
160 ebda->ipl.count++;
161}
162
163// Check if an option rom is at a hardcoded location for a device.
164static struct rom_header *
165lookup_hardcode(PCIDevice d)
166{
167 if (OPTIONROM_BDF_1
168 && OPTIONROM_BDF_1 == pci_to_bdf(d))
169 return (struct rom_header *)OPTIONROM_MEM_1;
170 else if (OPTIONROM_BDF_2
171 && OPTIONROM_BDF_2 == pci_to_bdf(d))
172 return (struct rom_header *)OPTIONROM_MEM_2;
173 // XXX - check LAR when in coreboot?
174 return NULL;
175}
176
177// Map the option rom of a given PCI device.
178static struct rom_header *
179map_optionrom(PCIDevice d)
180{
181 u32 orig = pci_config_readl(d, PCI_ROM_ADDRESS);
182 pci_config_writel(d, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
183 u32 sz = pci_config_readl(d, PCI_ROM_ADDRESS);
184
185 if (!sz || sz == 0xffffffff)
186 goto fail;
187
188 // Looks like a rom - map it to just above end of memory.
189 u32 mappos = ALIGN(GET_EBDA(ram_size), OPTION_ROM_ALIGN);
190 pci_config_writel(d, PCI_ROM_ADDRESS, mappos | PCI_ROM_ADDRESS_ENABLE);
191
192 struct rom_header *rom = (struct rom_header *)mappos;
193 if (rom->signature != OPTION_ROM_SIGNATURE)
194 goto fail;
195
196 return rom;
197fail:
198 // Not valid - restore original and exit.
199 pci_config_writel(d, PCI_ROM_ADDRESS, orig);
200 return NULL;
201}
202
203// Attempt to map and initialize the option rom on a given PCI device.
204static struct rom_header *
205init_optionrom(PCIDevice d)
206{
207 struct rom_header *rom = lookup_hardcode(d);
208 if (! rom)
209 rom = map_optionrom(d);
210 if (! rom)
211 // No ROM present.
212 return NULL;
213
214 u32 romsize = rom->size * 512;
215 if (next_rom + romsize > BUILD_BIOS_ADDR) {
216 // Option rom doesn't fit.
217 dprintf(1, "Option rom %x doesn't fit.", pci_to_bdf(d));
218 pci_config_writel(d, PCI_ROM_ADDRESS, next_rom);
219 return NULL;
220 }
221 memcpy((void*)next_rom, rom, romsize);
222 pci_config_writel(d, PCI_ROM_ADDRESS, next_rom);
223 rom = (struct rom_header *)next_rom;
224
225 if (! is_valid_rom(rom))
226 return NULL;
227
228 if (get_pnp_rom(rom))
229 // Init the PnP rom.
230 callrom(rom, OPTION_ROM_INITVECTOR, pci_to_bdf(d));
231
232 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
233
234 return rom;
235}
236
237
238/****************************************************************
239 * Non-VGA option rom init
240 ****************************************************************/
241
242void
243optionrom_setup()
Kevin O'Connor714325c2008-11-01 20:32:27 -0400244{
245 if (! CONFIG_OPTIONROMS)
246 return;
247
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500248 dprintf(1, "Scan for option roms\n");
249
250 u32 post_vga = next_rom;
251
252 if (CONFIG_OPTIONROMS_DEPLOYED) {
253 // Option roms are already deployed on the system.
254 u32 pos = next_rom;
255 while (pos < BUILD_BIOS_ADDR) {
256 struct rom_header *rom = (struct rom_header *)pos;
257 if (! is_valid_rom(rom)) {
258 pos += OPTION_ROM_ALIGN;
259 continue;
260 }
261 if (get_pnp_rom(rom))
262 callrom(rom, OPTION_ROM_INITVECTOR, 0);
263 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
264 next_rom = pos;
265 }
266 } else {
267 // Find and deploy PCI roms.
268 int devfn, bus;
269 for (bus=0; bus < CONFIG_PCI_BUS_COUNT; bus++) {
270 for (devfn=0; devfn<0x100; devfn++) {
271 PCIDevice d = pci_bd(bus, devfn);
272 u16 v = pci_config_readw(d, PCI_CLASS_DEVICE);
273 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA)
274 continue;
275 init_optionrom(d);
276 }
277 }
278 }
279
280 // All option roms found and deployed - now build BEV/BCV vectors.
281
282 u32 pos = post_vga;
283 while (pos < next_rom) {
284 struct rom_header *rom = (struct rom_header *)pos;
285 if (! is_valid_rom(rom)) {
286 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400287 continue;
288 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500289 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
290 struct pnp_data *pnp = get_pnp_rom(rom);
291 if (! pnp) {
292 // Legacy rom - run init vector now.
293 callrom(rom, OPTION_ROM_INITVECTOR, 0);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400294 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500295 }
296 // PnP rom.
297 if (pnp->bev)
298 // Can boot system - add to IPL list.
299 add_ipl(rom, pnp);
300 else if (pnp->bcv)
301 // Has BCV - run it now.
302 callrom(rom, pnp->bcv, 0);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400303 }
304}
305
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500306
307/****************************************************************
308 * VGA init
309 ****************************************************************/
310
Kevin O'Connor714325c2008-11-01 20:32:27 -0400311// Call into vga code to turn on console.
312void
313vga_setup()
314{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500315 if (! CONFIG_OPTIONROMS)
316 return;
317
Kevin O'Connor714325c2008-11-01 20:32:27 -0400318 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500319 next_rom = OPTION_ROM_START;
320
321 if (CONFIG_OPTIONROMS_DEPLOYED) {
322 // Option roms are already deployed on the system.
323 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
324 if (! is_valid_rom(rom))
325 return;
326 callrom(rom, OPTION_ROM_INITVECTOR, 0);
327 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
328 } else {
329 // Find and deploy PCI VGA rom.
330 PCIDevice d;
331 int ret = pci_find_class(PCI_CLASS_DISPLAY_VGA, 0, &d);
332 if (ret)
333 // Device not found
334 return;
335
336 struct rom_header *rom = init_optionrom(d);
337 if (rom && !get_pnp_rom(rom))
338 // Call rom even if it isn't a pnp rom.
339 callrom(rom, OPTION_ROM_INITVECTOR, pci_to_bdf(d));
340 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400341
342 dprintf(1, "Turning on vga console\n");
343 struct bregs br;
344 memset(&br, 0, sizeof(br));
345 br.ax = 0x0003;
346 call16_int(0x10, &br);
347
348 // Write to screen.
349 printf("Starting SeaBIOS\n\n");
350}