Enhance experimental option rom "threading" - enable preemption.
[seabios.git] / src / optionroms.c
1 // 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 LGPLv3 license.
7
8 #include "bregs.h" // struct bregs
9 #include "farptr.h" // FLATPTR_TO_SEG
10 #include "config.h" // CONFIG_*
11 #include "util.h" // dprintf
12 #include "pci.h" // foreachpci
13 #include "pci_regs.h" // PCI_ROM_ADDRESS
14 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
15 #include "boot.h" // IPL
16
17
18 /****************************************************************
19  * Definitions
20  ****************************************************************/
21
22 struct 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
31 struct 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
47 struct 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
67 #define OPTION_ROM_SIGNATURE 0xaa55
68 #define OPTION_ROM_ALIGN 2048
69 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
70 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
71 #define PCIROM_CODETYPE_X86 0
72
73 // The end of the last deployed rom.
74 u32 RomEnd;
75
76
77 /****************************************************************
78  * Helper functions
79  ****************************************************************/
80
81 // Execute a given option rom.
82 static void
83 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
84 {
85     u16 seg = FLATPTR_TO_SEG(rom);
86     dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
87
88     struct bregs br;
89     memset(&br, 0, sizeof(br));
90     br.flags = F_IF;
91     br.ax = bdf;
92     br.bx = 0xffff;
93     br.dx = 0xffff;
94     br.es = SEG_BIOS;
95     br.di = get_pnp_offset();
96     br.code = SEGOFF(seg, offset);
97     start_preempt();
98     call16big(&br);
99     finish_preempt();
100
101     debug_serial_setup();
102 }
103
104 // Execute a given option rom at the standard entry vector.
105 static void
106 callrom(struct rom_header *rom, u16 bdf)
107 {
108     __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
109 }
110
111 // Execute a BCV option rom registered via add_bcv().
112 void
113 call_bcv(u16 seg, u16 ip)
114 {
115     __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
116 }
117
118 // Verify that an option rom looks valid
119 static int
120 is_valid_rom(struct rom_header *rom)
121 {
122     dprintf(6, "Checking rom %p (sig %x size %d)\n"
123             , rom, rom->signature, rom->size);
124     if (rom->signature != OPTION_ROM_SIGNATURE)
125         return 0;
126     if (! rom->size)
127         return 0;
128     u32 len = rom->size * 512;
129     u8 sum = checksum(rom, len);
130     if (sum != 0) {
131         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
132                 , rom, len, sum);
133         return 0;
134     }
135     return 1;
136 }
137
138 // Check if a valid option rom has a pnp struct; return it if so.
139 static struct pnp_data *
140 get_pnp_rom(struct rom_header *rom)
141 {
142     struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
143     if (pnp->signature != PNP_SIGNATURE)
144         return NULL;
145     return pnp;
146 }
147
148 // Check for multiple pnp option rom headers.
149 static struct pnp_data *
150 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
151 {
152     if (! pnp->nextoffset)
153         return NULL;
154     pnp = (void*)((u8*)rom + pnp->nextoffset);
155     if (pnp->signature != PNP_SIGNATURE)
156         return NULL;
157     return pnp;
158 }
159
160 // Check if a valid option rom has a pci struct; return it if so.
161 static struct pci_data *
162 get_pci_rom(struct rom_header *rom)
163 {
164     struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
165     if (pci->signature != PCI_ROM_SIGNATURE)
166         return NULL;
167     return pci;
168 }
169
170 // Return the memory position up to which roms may be located.
171 static inline u32
172 max_rom()
173 {
174     extern u8 code32_start[];
175     if ((u32)code32_start > BUILD_BIOS_ADDR)
176         return BUILD_BIOS_ADDR;
177     return (u32)code32_start;
178 }
179
180 // Copy a rom to its permanent location below 1MiB
181 static struct rom_header *
182 copy_rom(struct rom_header *rom)
183 {
184     u32 romsize = rom->size * 512;
185     if (RomEnd + romsize > max_rom()) {
186         // Option rom doesn't fit.
187         dprintf(1, "Option rom %p doesn't fit.\n", rom);
188         return NULL;
189     }
190     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
191             , romsize, rom, RomEnd);
192     iomemcpy((void*)RomEnd, rom, romsize);
193     return (void*)RomEnd;
194 }
195
196 // Run rom init code and note rom size.
197 static int
198 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
199 {
200     if (! is_valid_rom(rom))
201         return -1;
202
203     if (isvga || get_pnp_rom(rom))
204         // Only init vga and PnP roms here.
205         callrom(rom, bdf);
206
207     RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
208
209     return 0;
210 }
211
212
213 /****************************************************************
214  * Roms in CBFS
215  ****************************************************************/
216
217 // Check if an option rom is at a hardcoded location or in CBFS.
218 static struct rom_header *
219 lookup_hardcode(u32 vendev)
220 {
221     if (OPTIONROM_VENDEV_1
222         && ((OPTIONROM_VENDEV_1 >> 16)
223             | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
224         return copy_rom((void*)OPTIONROM_MEM_1);
225     if (OPTIONROM_VENDEV_2
226         && ((OPTIONROM_VENDEV_2 >> 16)
227             | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
228         return copy_rom((void*)OPTIONROM_MEM_2);
229     int ret = cbfs_copy_optionrom((void*)RomEnd, max_rom() - RomEnd, vendev);
230     if (ret < 0)
231         return NULL;
232     return (void*)RomEnd;
233 }
234
235 // Run all roms in a given CBFS directory.
236 static void
237 run_cbfs_roms(const char *prefix, int isvga)
238 {
239     struct cbfs_file *file = NULL;
240     for (;;) {
241         file = cbfs_findprefix(prefix, file);
242         if (!file)
243             break;
244         int ret = cbfs_copyfile(file, (void*)RomEnd, max_rom() - RomEnd);
245         if (ret > 0)
246             init_optionrom((void*)RomEnd, 0, isvga);
247     }
248 }
249
250
251 /****************************************************************
252  * PCI roms
253  ****************************************************************/
254
255 // Map the option rom of a given PCI device.
256 static struct rom_header *
257 map_pcirom(u16 bdf, u32 vendev)
258 {
259     dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
260             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
261
262     u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
263     if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
264         dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
265         return NULL;
266     }
267
268     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
269     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
270     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
271
272     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
273     orig &= ~PCI_ROM_ADDRESS_ENABLE;
274     if (!sz || sz == 0xffffffff)
275         goto fail;
276
277     if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
278         // Don't try to map to a pci addresses at its max, in the last
279         // 4MiB of ram, or the first 16MiB of ram.
280         dprintf(6, "Preset rom address doesn't look valid\n");
281         goto fail;
282     }
283
284     // Looks like a rom - enable it.
285     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
286
287     struct rom_header *rom = (void*)orig;
288     for (;;) {
289         dprintf(5, "Inspecting possible rom at %p (dv=%08x bdf=%x)\n"
290                 , rom, vendev, bdf);
291         if (rom->signature != OPTION_ROM_SIGNATURE) {
292             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
293             goto fail;
294         }
295         struct pci_data *pci = get_pci_rom(rom);
296         if (! pci) {
297             dprintf(6, "No valid pci signature found\n");
298             goto fail;
299         }
300
301         u32 vd = (pci->device << 16) | pci->vendor;
302         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
303             // A match
304             break;
305         dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
306                 , vd, pci->type);
307         if (pci->indicator & 0x80) {
308             dprintf(6, "No more images left\n");
309             goto fail;
310         }
311         rom = (void*)((u32)rom + pci->ilen * 512);
312     }
313
314     rom = copy_rom(rom);
315     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
316     return rom;
317 fail:
318     // Not valid - restore original and exit.
319     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
320     return NULL;
321 }
322
323 // Attempt to map and initialize the option rom on a given PCI device.
324 static int
325 init_pcirom(u16 bdf, int isvga)
326 {
327     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
328     dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %08x)\n"
329             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
330             , vendev);
331     struct rom_header *rom = lookup_hardcode(vendev);
332     if (! rom)
333         rom = map_pcirom(bdf, vendev);
334     if (! rom)
335         // No ROM present.
336         return -1;
337     return init_optionrom(rom, bdf, isvga);
338 }
339
340
341 /****************************************************************
342  * Non-VGA option rom init
343  ****************************************************************/
344
345 void
346 optionrom_setup()
347 {
348     if (! CONFIG_OPTIONROMS)
349         return;
350
351     dprintf(1, "Scan for option roms\n");
352
353     u32 post_vga = RomEnd;
354
355     if (CONFIG_OPTIONROMS_DEPLOYED) {
356         // Option roms are already deployed on the system.
357         u32 pos = RomEnd;
358         while (pos < max_rom()) {
359             int ret = init_optionrom((void*)pos, 0, 0);
360             if (ret)
361                 pos += OPTION_ROM_ALIGN;
362             else
363                 pos = RomEnd;
364         }
365     } else {
366         // Find and deploy PCI roms.
367         int bdf, max;
368         foreachpci(bdf, max) {
369             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
370             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
371                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
372                 continue;
373             init_pcirom(bdf, 0);
374         }
375
376         // Find and deploy CBFS roms not associated with a device.
377         run_cbfs_roms("genroms/", 0);
378     }
379
380     // All option roms found and deployed - now build BEV/BCV vectors.
381
382     u32 pos = post_vga;
383     while (pos < RomEnd) {
384         struct rom_header *rom = (void*)pos;
385         if (! is_valid_rom(rom)) {
386             pos += OPTION_ROM_ALIGN;
387             continue;
388         }
389         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
390         struct pnp_data *pnp = get_pnp_rom(rom);
391         if (! pnp) {
392             // Legacy rom.
393             add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
394             continue;
395         }
396         // PnP rom.
397         if (pnp->bev)
398             // Can boot system - add to IPL list.
399             add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
400         else
401             // Check for BCV (there may be multiple).
402             while (pnp && pnp->bcv) {
403                 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
404                 pnp = get_pnp_next(rom, pnp);
405             }
406     }
407 }
408
409
410 /****************************************************************
411  * VGA init
412  ****************************************************************/
413
414 // Call into vga code to turn on console.
415 void
416 vga_setup()
417 {
418     VGAbdf = -1;
419     RomEnd = BUILD_ROM_START;
420
421     if (! CONFIG_OPTIONROMS)
422         return;
423
424     dprintf(1, "Scan for VGA option rom\n");
425
426     if (CONFIG_OPTIONROMS_DEPLOYED) {
427         // Option roms are already deployed on the system.
428         init_optionrom((void*)BUILD_ROM_START, 0, 1);
429     } else {
430         // Find and deploy PCI VGA rom.
431         int bdf = VGAbdf = pci_find_vga();
432         if (bdf >= 0)
433             init_pcirom(bdf, 1);
434
435         // Find and deploy CBFS vga-style roms not associated with a device.
436         run_cbfs_roms("vgaroms/", 1);
437     }
438
439     if (RomEnd == BUILD_ROM_START) {
440         // No VGA rom found
441         RomEnd += OPTION_ROM_ALIGN;
442         return;
443     }
444
445     dprintf(1, "Turning on vga console\n");
446     struct bregs br;
447     memset(&br, 0, sizeof(br));
448     br.flags = F_IF;
449     br.ax = 0x0003;
450     call16_int(0x10, &br);
451
452     // Write to screen.
453     printf("Starting SeaBIOS (version %s)\n\n", VERSION);
454 }
455
456 void
457 s3_resume_vga_init()
458 {
459     if (!CONFIG_S3_RESUME_VGA_INIT)
460         return;
461     struct rom_header *rom = (void*)BUILD_ROM_START;
462     if (! is_valid_rom(rom))
463         return;
464     callrom(rom, 0);
465 }