Rename foreachpci macro to foreachbdf.
[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" // foreachbdf
13 #include "pci_regs.h" // PCI_ROM_ADDRESS
14 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
15 #include "boot.h" // IPL
16 #include "paravirt.h" // qemu_cfg_*
17
18
19 /****************************************************************
20  * Definitions
21  ****************************************************************/
22
23 struct 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
32 struct 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
48 struct 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
68 #define OPTION_ROM_SIGNATURE 0xaa55
69 #define OPTION_ROM_ALIGN 2048
70 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
71 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
72 #define PCIROM_CODETYPE_X86 0
73
74 // The end of the last deployed rom.
75 u32 RomEnd = BUILD_ROM_START;
76
77
78 /****************************************************************
79  * Helper functions
80  ****************************************************************/
81
82 // Execute a given option rom.
83 static void
84 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
85 {
86     u16 seg = FLATPTR_TO_SEG(rom);
87     dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
88
89     struct bregs br;
90     memset(&br, 0, sizeof(br));
91     br.flags = F_IF;
92     br.ax = bdf;
93     br.bx = 0xffff;
94     br.dx = 0xffff;
95     br.es = SEG_BIOS;
96     br.di = get_pnp_offset();
97     br.code = SEGOFF(seg, offset);
98     start_preempt();
99     call16big(&br);
100     finish_preempt();
101
102     debug_serial_setup();
103 }
104
105 // Execute a given option rom at the standard entry vector.
106 static void
107 callrom(struct rom_header *rom, u16 bdf)
108 {
109     __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
110 }
111
112 // Execute a BCV option rom registered via add_bcv().
113 void
114 call_bcv(u16 seg, u16 ip)
115 {
116     __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
117 }
118
119 // Verify that an option rom looks valid
120 static int
121 is_valid_rom(struct rom_header *rom)
122 {
123     dprintf(6, "Checking rom %p (sig %x size %d)\n"
124             , rom, rom->signature, rom->size);
125     if (rom->signature != OPTION_ROM_SIGNATURE)
126         return 0;
127     if (! rom->size)
128         return 0;
129     u32 len = rom->size * 512;
130     u8 sum = checksum(rom, len);
131     if (sum != 0) {
132         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
133                 , rom, len, sum);
134         if (CONFIG_OPTIONROMS_CHECKSUM)
135             return 0;
136     }
137     return 1;
138 }
139
140 // Check if a valid option rom has a pnp struct; return it if so.
141 static struct pnp_data *
142 get_pnp_rom(struct rom_header *rom)
143 {
144     struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
145     if (pnp->signature != PNP_SIGNATURE)
146         return NULL;
147     return pnp;
148 }
149
150 // Check for multiple pnp option rom headers.
151 static struct pnp_data *
152 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
153 {
154     if (! pnp->nextoffset)
155         return NULL;
156     pnp = (void*)((u8*)rom + pnp->nextoffset);
157     if (pnp->signature != PNP_SIGNATURE)
158         return NULL;
159     return pnp;
160 }
161
162 // Check if a valid option rom has a pci struct; return it if so.
163 static struct pci_data *
164 get_pci_rom(struct rom_header *rom)
165 {
166     struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
167     if (pci->signature != PCI_ROM_SIGNATURE)
168         return NULL;
169     return pci;
170 }
171
172 // Return start of code in 0xc0000-0xf0000 space.
173 static inline u32 _max_rom(void) {
174     extern u8 code32flat_start[], code32init_end[];
175     return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start;
176 }
177 // Return the memory position up to which roms may be located.
178 static inline u32 max_rom(void) {
179     u32 end = _max_rom();
180     return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end;
181 }
182
183 // Copy a rom to its permanent location below 1MiB
184 static struct rom_header *
185 copy_rom(struct rom_header *rom)
186 {
187     u32 romsize = rom->size * 512;
188     if (RomEnd + romsize > max_rom()) {
189         // Option rom doesn't fit.
190         warn_noalloc();
191         return NULL;
192     }
193     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
194             , romsize, rom, RomEnd);
195     iomemcpy((void*)RomEnd, rom, romsize);
196     return (void*)RomEnd;
197 }
198
199 // Run rom init code and note rom size.
200 static int
201 init_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
210     RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
211
212     return 0;
213 }
214
215 #define RS_PCIROM (1LL<<33)
216
217 static void
218 setRomSource(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
224 static int
225 getRomPriority(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 }
234
235 /****************************************************************
236  * Roms in CBFS
237  ****************************************************************/
238
239 // Check if an option rom is at a hardcoded location or in CBFS.
240 static struct rom_header *
241 lookup_hardcode(u32 vendev)
242 {
243     if (OPTIONROM_VENDEV_1
244         && ((OPTIONROM_VENDEV_1 >> 16)
245             | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
246         return copy_rom((void*)OPTIONROM_MEM_1);
247     if (OPTIONROM_VENDEV_2
248         && ((OPTIONROM_VENDEV_2 >> 16)
249             | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
250         return copy_rom((void*)OPTIONROM_MEM_2);
251     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)
257         return NULL;
258     return (void*)RomEnd;
259 }
260
261 // Run all roms in a given CBFS directory.
262 static void
263 run_file_roms(const char *prefix, int isvga, u64 *sources)
264 {
265     u32 file = 0;
266     for (;;) {
267         file = romfile_findprefix(prefix, file);
268         if (!file)
269             break;
270         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         }
276     }
277 }
278
279
280 /****************************************************************
281  * PCI roms
282  ****************************************************************/
283
284 // Map the option rom of a given PCI device.
285 static struct rom_header *
286 map_pcirom(u16 bdf, u32 vendev)
287 {
288     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));
290
291     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
297     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);
300
301     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
302     orig &= ~PCI_ROM_ADDRESS_ENABLE;
303     if (!sz || sz == 0xffffffff)
304         goto fail;
305
306     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.
309         dprintf(6, "Preset rom address doesn't look valid\n");
310         goto fail;
311     }
312
313     // Looks like a rom - enable it.
314     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
315
316     struct rom_header *rom = (void*)orig;
317     for (;;) {
318         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));
322         if (rom->signature != OPTION_ROM_SIGNATURE) {
323             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
324             goto fail;
325         }
326         struct pci_data *pci = get_pci_rom(rom);
327         if (! pci) {
328             dprintf(6, "No valid pci signature found\n");
329             goto fail;
330         }
331
332         u32 vd = pci_vd(pci->vendor, pci->device);
333         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
334             // A match
335             break;
336         dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
337                 , vd, pci->type);
338         if (pci->indicator & 0x80) {
339             dprintf(6, "No more images left\n");
340             goto fail;
341         }
342         rom = (void*)((u32)rom + pci->ilen * 512);
343     }
344
345     rom = copy_rom(rom);
346     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
347     return rom;
348 fail:
349     // Not valid - restore original and exit.
350     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
351     return NULL;
352 }
353
354 // Attempt to map and initialize the option rom on a given PCI device.
355 static int
356 init_pcirom(u16 bdf, int isvga, u64 *sources)
357 {
358     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
359     dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
360             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
361             , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
362     struct rom_header *rom = lookup_hardcode(vendev);
363     if (! rom)
364         rom = map_pcirom(bdf, vendev);
365     if (! rom)
366         // No ROM present.
367         return -1;
368     setRomSource(sources, rom, RS_PCIROM | bdf);
369     return init_optionrom(rom, bdf, isvga);
370 }
371
372
373 /****************************************************************
374  * Non-VGA option rom init
375  ****************************************************************/
376
377 void
378 optionrom_setup(void)
379 {
380     if (! CONFIG_OPTIONROMS)
381         return;
382
383     dprintf(1, "Scan for option roms\n");
384     u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
385     memset(sources, 0, sizeof(sources));
386     u32 post_vga = RomEnd;
387
388     if (CONFIG_OPTIONROMS_DEPLOYED) {
389         // Option roms are already deployed on the system.
390         u32 pos = RomEnd;
391         while (pos < max_rom()) {
392             int ret = init_optionrom((void*)pos, 0, 0);
393             if (ret)
394                 pos += OPTION_ROM_ALIGN;
395             else
396                 pos = RomEnd;
397         }
398     } else {
399         // Find and deploy PCI roms.
400         int bdf, max;
401         foreachbdf(bdf, max) {
402             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
403             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
404                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
405                 continue;
406             init_pcirom(bdf, 0, sources);
407         }
408
409         // Find and deploy CBFS roms not associated with a device.
410         run_file_roms("genroms/", 0, sources);
411     }
412
413     // All option roms found and deployed - now build BEV/BCV vectors.
414
415     u32 pos = post_vga;
416     while (pos < RomEnd) {
417         struct rom_header *rom = (void*)pos;
418         if (! is_valid_rom(rom)) {
419             pos += OPTION_ROM_ALIGN;
420             continue;
421         }
422         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
423         struct pnp_data *pnp = get_pnp_rom(rom);
424         if (! pnp) {
425             // Legacy rom.
426             boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
427                          , getRomPriority(sources, rom, 0));
428             continue;
429         }
430         // PnP rom.
431         if (pnp->bev) {
432             // Can boot system - add to IPL list.
433             boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
434                          , getRomPriority(sources, rom, 0));
435         } else {
436             // Check for BCV (there may be multiple).
437             int instance = 0;
438             while (pnp && pnp->bcv) {
439                 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
440                              , getRomPriority(sources, rom, instance++));
441                 pnp = get_pnp_next(rom, pnp);
442             }
443         }
444     }
445 }
446
447
448 /****************************************************************
449  * VGA init
450  ****************************************************************/
451
452 // Call into vga code to turn on console.
453 void
454 vga_setup(void)
455 {
456     if (! CONFIG_OPTIONROMS)
457         return;
458
459     dprintf(1, "Scan for VGA option rom\n");
460
461     if (CONFIG_OPTIONROMS_DEPLOYED) {
462         // Option roms are already deployed on the system.
463         init_optionrom((void*)BUILD_ROM_START, 0, 1);
464     } else {
465         // Clear option rom memory
466         memset((void*)RomEnd, 0, _max_rom() - RomEnd);
467
468         // Find and deploy PCI VGA rom.
469         int bdf = VGAbdf = pci_find_vga();
470         if (bdf >= 0)
471             init_pcirom(bdf, 1, NULL);
472
473         // Find and deploy CBFS vga-style roms not associated with a device.
474         run_file_roms("vgaroms/", 1, NULL);
475     }
476
477     if (RomEnd == BUILD_ROM_START) {
478         // No VGA rom found
479         RomEnd += OPTION_ROM_ALIGN;
480         return;
481     }
482
483     enable_vga_console();
484 }
485
486 void
487 s3_resume_vga_init(void)
488 {
489     if (!CONFIG_S3_RESUME_VGA_INIT)
490         return;
491     struct rom_header *rom = (void*)BUILD_ROM_START;
492     if (! is_valid_rom(rom))
493         return;
494     callrom(rom, 0);
495 }