Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / 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 "config.h" // CONFIG_*
10 #include "farptr.h" // FLATPTR_TO_SEG
11 #include "hw/pci.h" // foreachpci
12 #include "hw/pci_ids.h" // PCI_CLASS_DISPLAY_VGA
13 #include "hw/pci_regs.h" // PCI_ROM_ADDRESS
14 #include "malloc.h" // rom_confirm
15 #include "output.h" // dprintf
16 #include "romfile.h" // romfile_loadint
17 #include "stacks.h" // farcall16big
18 #include "std/optionrom.h" // struct rom_header
19 #include "std/pnpbios.h" // PNP_SIGNATURE
20 #include "string.h" // memset
21 #include "util.h" // get_pnp_offset
22
23
24 /****************************************************************
25  * Helper functions
26  ****************************************************************/
27
28 // Execute a given option rom.
29 static void
30 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
31 {
32     u16 seg = FLATPTR_TO_SEG(rom);
33     dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
34
35     struct bregs br;
36     memset(&br, 0, sizeof(br));
37     br.flags = F_IF;
38     br.ax = bdf;
39     br.bx = 0xffff;
40     br.dx = 0xffff;
41     br.es = SEG_BIOS;
42     br.di = get_pnp_offset();
43     br.code = SEGOFF(seg, offset);
44     start_preempt();
45     farcall16big(&br);
46     finish_preempt();
47 }
48
49 // Execute a given option rom at the standard entry vector.
50 void
51 callrom(struct rom_header *rom, u16 bdf)
52 {
53     __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
54 }
55
56 // Execute a BCV option rom registered via add_bcv().
57 void
58 call_bcv(u16 seg, u16 ip)
59 {
60     __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
61 }
62
63 static int EnforceChecksum;
64
65 // Verify that an option rom looks valid
66 static int
67 is_valid_rom(struct rom_header *rom)
68 {
69     dprintf(6, "Checking rom %p (sig %x size %d)\n"
70             , rom, rom->signature, rom->size);
71     if (rom->signature != OPTION_ROM_SIGNATURE)
72         return 0;
73     if (! rom->size)
74         return 0;
75     u32 len = rom->size * 512;
76     u8 sum = checksum(rom, len);
77     if (sum != 0) {
78         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
79                 , rom, len, sum);
80         if (EnforceChecksum)
81             return 0;
82     }
83     return 1;
84 }
85
86 // Check if a valid option rom has a pnp struct; return it if so.
87 static struct pnp_data *
88 get_pnp_rom(struct rom_header *rom)
89 {
90     struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
91     if (pnp->signature != PNP_SIGNATURE)
92         return NULL;
93     return pnp;
94 }
95
96 // Check for multiple pnp option rom headers.
97 static struct pnp_data *
98 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
99 {
100     if (! pnp->nextoffset)
101         return NULL;
102     pnp = (void*)((u8*)rom + pnp->nextoffset);
103     if (pnp->signature != PNP_SIGNATURE)
104         return NULL;
105     return pnp;
106 }
107
108 // Check if a valid option rom has a pci struct; return it if so.
109 static struct pci_data *
110 get_pci_rom(struct rom_header *rom)
111 {
112     struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
113     if (pd->signature != PCI_ROM_SIGNATURE)
114         return NULL;
115     if (rom->pcioffset & 3)
116         dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n"
117                 , pd->vendor, pd->device);
118     return pd;
119 }
120
121 // Run rom init code and note rom size.
122 static int
123 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
124 {
125     if (! is_valid_rom(rom))
126         return -1;
127     struct rom_header *newrom = rom_reserve(rom->size * 512);
128     if (!newrom) {
129         warn_noalloc();
130         return -1;
131     }
132     if (newrom != rom)
133         memmove(newrom, rom, rom->size * 512);
134
135     if (isvga || get_pnp_rom(newrom))
136         // Only init vga and PnP roms here.
137         callrom(newrom, bdf);
138
139     return rom_confirm(newrom->size * 512);
140 }
141
142 #define RS_PCIROM (1LL<<33)
143
144 static void
145 setRomSource(u64 *sources, struct rom_header *rom, u64 source)
146 {
147     if (sources)
148         sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
149 }
150
151 static int
152 getRomPriority(u64 *sources, struct rom_header *rom, int instance)
153 {
154     u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
155     if (!source)
156         return -1;
157     if (source & RS_PCIROM)
158         return bootprio_find_pci_rom((void*)(u32)source, instance);
159     struct romfile_s *file = (void*)(u32)source;
160     return bootprio_find_named_rom(file->name, instance);
161 }
162
163
164 /****************************************************************
165  * Roms in CBFS
166  ****************************************************************/
167
168 static struct rom_header *
169 deploy_romfile(struct romfile_s *file)
170 {
171     u32 size = file->size;
172     struct rom_header *rom = rom_reserve(size);
173     if (!rom) {
174         warn_noalloc();
175         return NULL;
176     }
177     int ret = file->copy(file, rom, size);
178     if (ret <= 0)
179         return NULL;
180     return rom;
181 }
182
183 // Check if an option rom is at a hardcoded location or in CBFS.
184 static struct rom_header *
185 lookup_hardcode(struct pci_device *pci)
186 {
187     char fname[17];
188     snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
189              , pci->vendor, pci->device);
190     struct romfile_s *file = romfile_find(fname);
191     if (file)
192         return deploy_romfile(file);
193     return NULL;
194 }
195
196 // Run all roms in a given CBFS directory.
197 static void
198 run_file_roms(const char *prefix, int isvga, u64 *sources)
199 {
200     struct romfile_s *file = NULL;
201     for (;;) {
202         file = romfile_findprefix(prefix, file);
203         if (!file)
204             break;
205         struct rom_header *rom = deploy_romfile(file);
206         if (rom) {
207             setRomSource(sources, rom, (u32)file);
208             init_optionrom(rom, 0, isvga);
209         }
210     }
211 }
212
213
214 /****************************************************************
215  * PCI roms
216  ****************************************************************/
217
218 // Verify device is a vga device with legacy address decoding enabled.
219 int
220 is_pci_vga(struct pci_device *pci)
221 {
222     if (pci->class != PCI_CLASS_DISPLAY_VGA)
223         return 0;
224     u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
225     if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
226         return 0;
227     while (pci->parent) {
228         pci = pci->parent;
229         u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
230         if (!(ctrl & PCI_BRIDGE_CTL_VGA))
231             return 0;
232     }
233     return 1;
234 }
235
236 // Copy a rom to its permanent location below 1MiB
237 static struct rom_header *
238 copy_rom(struct rom_header *rom)
239 {
240     u32 romsize = rom->size * 512;
241     struct rom_header *newrom = rom_reserve(romsize);
242     if (!newrom) {
243         warn_noalloc();
244         return NULL;
245     }
246     dprintf(4, "Copying option rom (size %d) from %p to %p\n"
247             , romsize, rom, newrom);
248     iomemcpy(newrom, rom, romsize);
249     return newrom;
250 }
251
252 // Map the option rom of a given PCI device.
253 static struct rom_header *
254 map_pcirom(struct pci_device *pci)
255 {
256     u16 bdf = pci->bdf;
257     dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
258             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
259
260     if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
261         dprintf(6, "Skipping non-normal pci device (type=%x)\n"
262                 , pci->header_type);
263         return NULL;
264     }
265
266     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
267     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
268     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
269
270     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
271     orig &= ~PCI_ROM_ADDRESS_ENABLE;
272     if (!sz || sz == 0xffffffff)
273         goto fail;
274
275     if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
276         // Don't try to map to a pci addresses at its max, in the last
277         // 4MiB of ram, or the first 16MiB of ram.
278         dprintf(6, "Preset rom address doesn't look valid\n");
279         goto fail;
280     }
281
282     // Looks like a rom - enable it.
283     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
284
285     struct rom_header *rom = (void*)orig;
286     for (;;) {
287         dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
288                 " bdf=%02x:%02x.%x)\n"
289                 , rom, pci->vendor, pci->device
290                 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(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 *pd = get_pci_rom(rom);
296         if (! pd) {
297             dprintf(6, "No valid pci signature found\n");
298             goto fail;
299         }
300
301         if (pd->vendor == pci->vendor && pd->device == pci->device
302             && pd->type == PCIROM_CODETYPE_X86)
303             // A match
304             break;
305         dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
306                 , pd->vendor, pd->device, pd->type);
307         if (pd->indicator & 0x80) {
308             dprintf(6, "No more images left\n");
309             goto fail;
310         }
311         rom = (void*)((u32)rom + pd->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(struct pci_device *pci, int isvga, u64 *sources)
326 {
327     u16 bdf = pci->bdf;
328     dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
329             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
330             , pci->vendor, pci->device);
331     struct rom_header *rom = lookup_hardcode(pci);
332     if (! rom)
333         rom = map_pcirom(pci);
334     if (! rom)
335         // No ROM present.
336         return -1;
337     setRomSource(sources, rom, RS_PCIROM | (u32)pci);
338     return init_optionrom(rom, bdf, isvga);
339 }
340
341
342 /****************************************************************
343  * Non-VGA option rom init
344  ****************************************************************/
345
346 void
347 optionrom_setup(void)
348 {
349     if (! CONFIG_OPTIONROMS)
350         return;
351
352     dprintf(1, "Scan for option roms\n");
353     u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
354     memset(sources, 0, sizeof(sources));
355     u32 post_vga = rom_get_last();
356
357     if (CONFIG_OPTIONROMS_DEPLOYED) {
358         // Option roms are already deployed on the system.
359         u32 pos = post_vga;
360         while (pos < rom_get_max()) {
361             int ret = init_optionrom((void*)pos, 0, 0);
362             if (ret)
363                 pos += OPTION_ROM_ALIGN;
364             else
365                 pos = rom_get_last();
366         }
367     } else {
368         // Find and deploy PCI roms.
369         struct pci_device *pci;
370         foreachpci(pci) {
371             if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
372                 continue;
373             init_pcirom(pci, 0, sources);
374         }
375
376         // Find and deploy CBFS roms not associated with a device.
377         run_file_roms("genroms/", 0, sources);
378     }
379     rom_reserve(0);
380
381     // All option roms found and deployed - now build BEV/BCV vectors.
382
383     u32 pos = post_vga;
384     while (pos < rom_get_last()) {
385         struct rom_header *rom = (void*)pos;
386         if (! is_valid_rom(rom)) {
387             pos += OPTION_ROM_ALIGN;
388             continue;
389         }
390         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
391         struct pnp_data *pnp = get_pnp_rom(rom);
392         if (! pnp) {
393             // Legacy rom.
394             boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
395                          , getRomPriority(sources, rom, 0));
396             continue;
397         }
398         // PnP rom - check for BEV and BCV boot capabilities.
399         int instance = 0;
400         while (pnp) {
401             if (pnp->bev)
402                 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
403                              , getRomPriority(sources, rom, instance++));
404             else if (pnp->bcv)
405                 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
406                              , getRomPriority(sources, rom, instance++));
407             else
408                 break;
409             pnp = get_pnp_next(rom, pnp);
410         }
411     }
412 }
413
414
415 /****************************************************************
416  * VGA init
417  ****************************************************************/
418
419 static int S3ResumeVga;
420 int ScreenAndDebug;
421 struct rom_header *VgaROM;
422
423 // Call into vga code to turn on console.
424 void
425 vgarom_setup(void)
426 {
427     if (! CONFIG_OPTIONROMS)
428         return;
429
430     dprintf(1, "Scan for VGA option rom\n");
431
432     // Load some config settings that impact VGA.
433     EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
434     S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU);
435     ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
436
437     if (CONFIG_OPTIONROMS_DEPLOYED) {
438         // Option roms are already deployed on the system.
439         init_optionrom((void*)BUILD_ROM_START, 0, 1);
440     } else {
441         // Clear option rom memory
442         memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START);
443
444         // Find and deploy PCI VGA rom.
445         struct pci_device *pci;
446         foreachpci(pci) {
447             if (!is_pci_vga(pci))
448                 continue;
449             vgahook_setup(pci);
450             init_pcirom(pci, 1, NULL);
451             break;
452         }
453
454         // Find and deploy CBFS vga-style roms not associated with a device.
455         run_file_roms("vgaroms/", 1, NULL);
456     }
457     rom_reserve(0);
458
459     if (rom_get_last() == BUILD_ROM_START)
460         // No VGA rom found
461         return;
462
463     VgaROM = (void*)BUILD_ROM_START;
464     enable_vga_console();
465 }
466
467 void
468 s3_resume_vga(void)
469 {
470     if (!S3ResumeVga)
471         return;
472     if (!VgaROM || ! is_valid_rom(VgaROM))
473         return;
474     callrom(VgaROM, 0);
475 }