4db3465f49a28d1b497914327c6a7314330c2b2f
[seabios.git] / src / rombios32.c
1 /////////////////////////////////////////////////////////////////////////
2 //
3 //  32 bit Bochs BIOS init code
4 //  Copyright (C) 2006 Fabrice Bellard
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2 of the License, or (at your option) any later version.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
19
20 #include "util.h" // BX_INFO
21 #include "cmos.h" // inb_cmos
22 #include "pci.h" // PCIDevice
23 #include "types.h" // u32
24 #include "config.h" // CONFIG_*
25
26 // Memory addresses used by this code.  (Note global variables (bss)
27 // are at 0x40000).
28 #define CPU_COUNT_ADDR    0xf000
29 #define AP_BOOT_ADDR      0x10000
30 #define BIOS_TMP_STORAGE  0x30000 /* 64 KB used to copy the BIOS to shadow RAM */
31
32 #define PM_IO_BASE        0xb000
33 #define SMB_IO_BASE       0xb100
34
35 #define cpuid(index, eax, ebx, ecx, edx) \
36   asm volatile ("cpuid" \
37                 : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) \
38                 : "0" (index))
39
40 #define wbinvd() asm volatile("wbinvd")
41
42 #define CPUID_APIC (1 << 9)
43
44 #define APIC_BASE    ((u8 *)0xfee00000)
45 #define APIC_ICR_LOW 0x300
46 #define APIC_SVR     0x0F0
47 #define APIC_ID      0x020
48 #define APIC_LVT3    0x370
49
50 #define APIC_ENABLED 0x0100
51
52 #define MPTABLE_MAX_SIZE  0x00002000
53 #define SMI_CMD_IO_ADDR   0xb2
54
55 static inline void writel(void *addr, u32 val)
56 {
57     *(volatile u32 *)addr = val;
58 }
59
60 static inline void writew(void *addr, u16 val)
61 {
62     *(volatile u16 *)addr = val;
63 }
64
65 static inline void writeb(void *addr, u8 val)
66 {
67     *(volatile u8 *)addr = val;
68 }
69
70 static inline u32 readl(const void *addr)
71 {
72     return *(volatile const u32 *)addr;
73 }
74
75 static inline u16 readw(const void *addr)
76 {
77     return *(volatile const u16 *)addr;
78 }
79
80 static inline u8 readb(const void *addr)
81 {
82     return *(volatile const u8 *)addr;
83 }
84
85 int smp_cpus;
86 u32 cpuid_signature;
87 u32 cpuid_features;
88 u32 cpuid_ext_features;
89 u8 bios_uuid[16];
90 #if (CONFIG_USE_EBDA_TABLES == 1)
91 unsigned long ebda_cur_addr;
92 #endif
93 int acpi_enabled;
94 u32 pm_io_base, smb_io_base;
95 int pm_sci_int;
96 unsigned long bios_table_cur_addr;
97 unsigned long bios_table_end_addr;
98
99 void uuid_probe(void)
100 {
101 #if (CONFIG_QEMU == 1)
102     u32 eax, ebx, ecx, edx;
103
104     // check if backdoor port exists
105     asm volatile ("outl %%eax, %%dx"
106         : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
107         : "a" (0x564d5868), "c" (0xa), "d" (0x5658));
108     if (ebx == 0x564d5868) {
109         u32 *uuid_ptr = (u32 *)bios_uuid;
110         // get uuid
111         asm volatile ("outl %%eax, %%dx"
112             : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
113             : "a" (0x564d5868), "c" (0x13), "d" (0x5658));
114         uuid_ptr[0] = eax;
115         uuid_ptr[1] = ebx;
116         uuid_ptr[2] = ecx;
117         uuid_ptr[3] = edx;
118     } else
119 #endif
120     {
121         // UUID not set
122         memset(bios_uuid, 0, 16);
123     }
124 }
125
126 void cpu_probe(void)
127 {
128     u32 eax, ebx, ecx, edx;
129     cpuid(1, eax, ebx, ecx, edx);
130     cpuid_signature = eax;
131     cpuid_features = edx;
132     cpuid_ext_features = ecx;
133 }
134
135 /****************************************************/
136 /* SMP probe */
137
138 extern u8 smp_ap_boot_code_start;
139 extern u8 smp_ap_boot_code_end;
140
141 /* find the number of CPUs by launching a SIPI to them */
142 void smp_probe(void)
143 {
144     u32 val, sipi_vector;
145
146     smp_cpus = 1;
147     if (cpuid_features & CPUID_APIC) {
148
149         /* enable local APIC */
150         val = readl(APIC_BASE + APIC_SVR);
151         val |= APIC_ENABLED;
152         writel(APIC_BASE + APIC_SVR, val);
153
154         writew((void *)CPU_COUNT_ADDR, 1);
155         /* copy AP boot code */
156         memcpy((void *)AP_BOOT_ADDR, &smp_ap_boot_code_start,
157                &smp_ap_boot_code_end - &smp_ap_boot_code_start);
158
159         /* broadcast SIPI */
160         writel(APIC_BASE + APIC_ICR_LOW, 0x000C4500);
161         sipi_vector = AP_BOOT_ADDR >> 12;
162         writel(APIC_BASE + APIC_ICR_LOW, 0x000C4600 | sipi_vector);
163
164         usleep(10*1000);
165
166         smp_cpus = readw((void *)CPU_COUNT_ADDR);
167     }
168     BX_INFO("Found %d cpu(s)\n", smp_cpus);
169 }
170
171 /****************************************************/
172 /* PCI init */
173
174 #define PCI_ADDRESS_SPACE_MEM           0x00
175 #define PCI_ADDRESS_SPACE_IO            0x01
176 #define PCI_ADDRESS_SPACE_MEM_PREFETCH  0x08
177
178 #define PCI_ROM_SLOT 6
179 #define PCI_NUM_REGIONS 7
180
181 #define PCI_DEVICES_MAX 64
182
183 #define PCI_VENDOR_ID           0x00    /* 16 bits */
184 #define PCI_DEVICE_ID           0x02    /* 16 bits */
185 #define PCI_COMMAND             0x04    /* 16 bits */
186 #define  PCI_COMMAND_IO         0x1     /* Enable response in I/O space */
187 #define  PCI_COMMAND_MEMORY     0x2     /* Enable response in Memory space */
188 #define PCI_CLASS_DEVICE        0x0a    /* Device class */
189 #define PCI_INTERRUPT_LINE      0x3c    /* 8 bits */
190 #define PCI_INTERRUPT_PIN       0x3d    /* 8 bits */
191 #define PCI_MIN_GNT             0x3e    /* 8 bits */
192 #define PCI_MAX_LAT             0x3f    /* 8 bits */
193
194 static u32 pci_bios_io_addr;
195 static u32 pci_bios_mem_addr;
196 static u32 pci_bios_bigmem_addr;
197 /* host irqs corresponding to PCI irqs A-D */
198 static u8 pci_irqs[4] = { 11, 9, 11, 9 };
199 static PCIDevice i440_pcidev;
200
201 static void pci_set_io_region_addr(PCIDevice *d, int region_num, u32 addr)
202 {
203     u16 cmd;
204     u32 ofs, old_addr;
205
206     if ( region_num == PCI_ROM_SLOT ) {
207         ofs = 0x30;
208     }else{
209         ofs = 0x10 + region_num * 4;
210     }
211
212     old_addr = pci_config_readl(d, ofs);
213
214     pci_config_writel(d, ofs, addr);
215     BX_INFO("region %d: 0x%08x\n", region_num, addr);
216
217     /* enable memory mappings */
218     cmd = pci_config_readw(d, PCI_COMMAND);
219     if ( region_num == PCI_ROM_SLOT )
220         cmd |= 2;
221     else if (old_addr & PCI_ADDRESS_SPACE_IO)
222         cmd |= 1;
223     else
224         cmd |= 2;
225     pci_config_writew(d, PCI_COMMAND, cmd);
226 }
227
228 /* return the global irq number corresponding to a given device irq
229    pin. We could also use the bus number to have a more precise
230    mapping. */
231 static int pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
232 {
233     int slot_addend;
234     slot_addend = (pci_dev->devfn >> 3) - 1;
235     return (irq_num + slot_addend) & 3;
236 }
237
238 static void
239 copy_bios(PCIDevice *d, int v)
240 {
241     pci_config_writeb(d, 0x59, v);
242     memcpy((void *)0x000f0000, (void *)BIOS_TMP_STORAGE, 0x10000);
243 }
244
245 // Test if 'addr' is in the range from 'start'..'start+size'
246 #define IN_RANGE(addr, start, size) ({   \
247             u32 __addr = (addr);         \
248             u32 __start = (start);       \
249             u32 __size = (size);         \
250             (__addr - __start < __size); \
251         })
252
253 static void bios_shadow_init(PCIDevice *d)
254 {
255     bios_table_cur_addr = 0xf0000 | OFFSET_freespace2_start;
256     bios_table_end_addr = 0xf0000 | OFFSET_freespace2_end;
257     BX_INFO("bios_table_addr: 0x%08lx end=0x%08lx\n",
258             bios_table_cur_addr, bios_table_end_addr);
259
260     /* remap the BIOS to shadow RAM an keep it read/write while we
261        are writing tables */
262     int v = pci_config_readb(d, 0x59);
263     v &= 0xcf;
264     pci_config_writeb(d, 0x59, v);
265     memcpy((void *)BIOS_TMP_STORAGE, (void *)0x000f0000, 0x10000);
266     v |= 0x30;
267
268     if (IN_RANGE((u32)copy_bios, 0xf0000, 0x10000)) {
269         // Current code is in shadowed area.  Perform the copy from
270         // the code that is in the temporary location.
271         u32 pos = (u32)copy_bios - 0xf0000 + BIOS_TMP_STORAGE;
272         void (*func)(PCIDevice *, int) = (void*)pos;
273         func(d, v);
274     } else {
275         copy_bios(d, v);
276     }
277
278     // Clear the area just copied.
279     memset((void *)BIOS_TMP_STORAGE, 0, 0x10000);
280
281     i440_pcidev = *d;
282 }
283
284 static void bios_lock_shadow_ram(void)
285 {
286     PCIDevice *d = &i440_pcidev;
287     int v;
288
289     wbinvd();
290     v = pci_config_readb(d, 0x59);
291     v = (v & 0x0f) | (0x10);
292     pci_config_writeb(d, 0x59, v);
293 }
294
295 static void pci_bios_init_bridges(PCIDevice *d)
296 {
297     u16 vendor_id, device_id;
298
299     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
300     device_id = pci_config_readw(d, PCI_DEVICE_ID);
301
302     if (vendor_id == 0x8086 && device_id == 0x7000) {
303         int i, irq;
304         u8 elcr[2];
305
306         /* PIIX3 bridge */
307
308         elcr[0] = 0x00;
309         elcr[1] = 0x00;
310         for(i = 0; i < 4; i++) {
311             irq = pci_irqs[i];
312             /* set to trigger level */
313             elcr[irq >> 3] |= (1 << (irq & 7));
314             /* activate irq remapping in PIIX */
315             pci_config_writeb(d, 0x60 + i, irq);
316         }
317         outb(elcr[0], 0x4d0);
318         outb(elcr[1], 0x4d1);
319         BX_INFO("PIIX3 init: elcr=%02x %02x\n",
320                 elcr[0], elcr[1]);
321     } else if (vendor_id == 0x8086 && device_id == 0x1237) {
322         /* i440 PCI bridge */
323         bios_shadow_init(d);
324     }
325 }
326
327 asm(
328     ".globl smp_ap_boot_code_start\n"
329     ".globl smp_ap_boot_code_end\n"
330     ".global smm_relocation_start\n"
331     ".global smm_relocation_end\n"
332     ".global smm_code_start\n"
333     ".global smm_code_end\n"
334
335     "  .code16\n"
336     "smp_ap_boot_code_start:\n"
337     "  xor %ax, %ax\n"
338     "  mov %ax, %ds\n"
339     "  incw " __stringify(CPU_COUNT_ADDR) "\n"
340     "1:\n"
341     "  hlt\n"
342     "  jmp 1b\n"
343     "smp_ap_boot_code_end:\n"
344
345     /* code to relocate SMBASE to 0xa0000 */
346     "smm_relocation_start:\n"
347     "  mov $0x38000 + 0x7efc, %ebx\n"
348     "  addr32 mov (%ebx), %al\n"  /* revision ID to see if x86_64 or x86 */
349     "  cmp $0x64, %al\n"
350     "  je 1f\n"
351     "  mov $0x38000 + 0x7ef8, %ebx\n"
352     "  jmp 2f\n"
353     "1:\n"
354     "  mov $0x38000 + 0x7f00, %ebx\n"
355     "2:\n"
356     "  movl $0xa0000, %eax\n"
357     "  addr32 movl %eax, (%ebx)\n"
358     /* indicate to the BIOS that the SMM code was executed */
359     "  mov $0x00, %al\n"
360     "  movw $0xb3, %dx\n"
361     "  outb %al, %dx\n"
362     "  rsm\n"
363     "smm_relocation_end:\n"
364
365     /* minimal SMM code to enable or disable ACPI */
366     "smm_code_start:\n"
367     "  movw $0xb2, %dx\n"
368     "  inb %dx, %al\n"
369     "  cmp $0xf0, %al\n"
370     "  jne 1f\n"
371
372     /* ACPI disable */
373     "  mov $" __stringify(PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */
374     "  inw %dx, %ax\n"
375     "  andw $~1, %ax\n"
376     "  outw %ax, %dx\n"
377
378     "  jmp 2f\n"
379
380     "1:\n"
381     "  cmp $0xf1, %al\n"
382     "  jne 2f\n"
383
384     /* ACPI enable */
385     "  mov $" __stringify(PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */
386     "  inw %dx, %ax\n"
387     "  orw $1, %ax\n"
388     "  outw %ax, %dx\n"
389
390     "2:\n"
391     "  rsm\n"
392     "smm_code_end:\n"
393     "  .code32\n"
394     );
395
396 extern u8 smm_relocation_start, smm_relocation_end;
397 extern u8 smm_code_start, smm_code_end;
398
399 #if (CONFIG_USE_SMM == 1)
400 static void smm_init(PCIDevice *d)
401 {
402     u32 value;
403
404     /* check if SMM init is already done */
405     value = pci_config_readl(d, 0x58);
406     if ((value & (1 << 25)) == 0) {
407
408         /* copy the SMM relocation code */
409         memcpy((void *)0x38000, &smm_relocation_start,
410                &smm_relocation_end - &smm_relocation_start);
411
412         /* enable SMI generation when writing to the APMC register */
413         pci_config_writel(d, 0x58, value | (1 << 25));
414
415         /* init APM status port */
416         outb(0x01, 0xb3);
417
418         /* raise an SMI interrupt */
419         outb(0x00, 0xb2);
420
421         /* wait until SMM code executed */
422         while (inb(0xb3) != 0x00)
423             ;
424
425         /* enable the SMM memory window */
426         pci_config_writeb(&i440_pcidev, 0x72, 0x02 | 0x48);
427
428         /* copy the SMM code */
429         memcpy((void *)0xa8000, &smm_code_start,
430                &smm_code_end - &smm_code_start);
431         wbinvd();
432
433         /* close the SMM memory window and enable normal SMM */
434         pci_config_writeb(&i440_pcidev, 0x72, 0x02 | 0x08);
435     }
436 }
437 #endif
438
439 static void pci_bios_init_device(PCIDevice *d)
440 {
441     int class;
442     u32 *paddr;
443     int i, pin, pic_irq, vendor_id, device_id;
444
445     class = pci_config_readw(d, PCI_CLASS_DEVICE);
446     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
447     device_id = pci_config_readw(d, PCI_DEVICE_ID);
448     BX_INFO("PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n",
449             d->bus, d->devfn, vendor_id, device_id);
450     switch(class) {
451     case 0x0101:
452         if (vendor_id == 0x8086 && device_id == 0x7010) {
453             /* PIIX3 IDE */
454             pci_config_writew(d, 0x40, 0x8000); // enable IDE0
455             pci_config_writew(d, 0x42, 0x8000); // enable IDE1
456             goto default_map;
457         } else {
458             /* IDE: we map it as in ISA mode */
459             pci_set_io_region_addr(d, 0, 0x1f0);
460             pci_set_io_region_addr(d, 1, 0x3f4);
461             pci_set_io_region_addr(d, 2, 0x170);
462             pci_set_io_region_addr(d, 3, 0x374);
463         }
464         break;
465     case 0x0300:
466         if (vendor_id != 0x1234)
467             goto default_map;
468         /* VGA: map frame buffer to default Bochs VBE address */
469         pci_set_io_region_addr(d, 0, 0xE0000000);
470         break;
471     case 0x0800:
472         /* PIC */
473         if (vendor_id == 0x1014) {
474             /* IBM */
475             if (device_id == 0x0046 || device_id == 0xFFFF) {
476                 /* MPIC & MPIC2 */
477                 pci_set_io_region_addr(d, 0, 0x80800000 + 0x00040000);
478             }
479         }
480         break;
481     case 0xff00:
482         if (vendor_id == 0x0106b &&
483             (device_id == 0x0017 || device_id == 0x0022)) {
484             /* macio bridge */
485             pci_set_io_region_addr(d, 0, 0x80800000);
486         }
487         break;
488     default:
489     default_map:
490         /* default memory mappings */
491         for(i = 0; i < PCI_NUM_REGIONS; i++) {
492             int ofs;
493             u32 val, size ;
494
495             if (i == PCI_ROM_SLOT)
496                 ofs = 0x30;
497             else
498                 ofs = 0x10 + i * 4;
499             pci_config_writel(d, ofs, 0xffffffff);
500             val = pci_config_readl(d, ofs);
501             if (val != 0) {
502                 size = (~(val & ~0xf)) + 1;
503                 if (val & PCI_ADDRESS_SPACE_IO)
504                     paddr = &pci_bios_io_addr;
505                 else if (size >= 0x04000000)
506                     paddr = &pci_bios_bigmem_addr;
507                 else
508                     paddr = &pci_bios_mem_addr;
509                 *paddr = (*paddr + size - 1) & ~(size - 1);
510                 pci_set_io_region_addr(d, i, *paddr);
511                 *paddr += size;
512             }
513         }
514         break;
515     }
516
517     /* map the interrupt */
518     pin = pci_config_readb(d, PCI_INTERRUPT_PIN);
519     if (pin != 0) {
520         pin = pci_slot_get_pirq(d, pin - 1);
521         pic_irq = pci_irqs[pin];
522         pci_config_writeb(d, PCI_INTERRUPT_LINE, pic_irq);
523     }
524
525     if (vendor_id == 0x8086 && device_id == 0x7113) {
526         /* PIIX4 Power Management device (for ACPI) */
527         pm_io_base = PM_IO_BASE;
528         pci_config_writel(d, 0x40, pm_io_base | 1);
529         pci_config_writeb(d, 0x80, 0x01); /* enable PM io space */
530         smb_io_base = SMB_IO_BASE;
531         pci_config_writel(d, 0x90, smb_io_base | 1);
532         pci_config_writeb(d, 0xd2, 0x09); /* enable SMBus io space */
533         pm_sci_int = pci_config_readb(d, PCI_INTERRUPT_LINE);
534 #if (CONFIG_USE_SMM == 1)
535         smm_init(d);
536 #endif
537         acpi_enabled = 1;
538     }
539 }
540
541 void pci_for_each_device(void (*init_func)(PCIDevice *d))
542 {
543     PCIDevice d1, *d = &d1;
544     int bus, devfn;
545     u16 vendor_id, device_id;
546
547     for(bus = 0; bus < 1; bus++) {
548         for(devfn = 0; devfn < 256; devfn++) {
549             d->bus = bus;
550             d->devfn = devfn;
551             vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
552             device_id = pci_config_readw(d, PCI_DEVICE_ID);
553             if (vendor_id != 0xffff || device_id != 0xffff) {
554                 init_func(d);
555             }
556         }
557     }
558 }
559
560 void pci_bios_init(void)
561 {
562     pci_bios_io_addr = 0xc000;
563     pci_bios_mem_addr = 0xf0000000;
564     pci_bios_bigmem_addr = GET_EBDA(ram_size);
565     if (pci_bios_bigmem_addr < 0x90000000)
566         pci_bios_bigmem_addr = 0x90000000;
567
568     pci_for_each_device(pci_bios_init_bridges);
569
570     pci_for_each_device(pci_bios_init_device);
571 }
572
573 /****************************************************/
574 /* Multi Processor table init */
575
576 static void putb(u8 **pp, int val)
577 {
578     u8 *q;
579     q = *pp;
580     *q++ = val;
581     *pp = q;
582 }
583
584 static void putstr(u8 **pp, const char *str)
585 {
586     u8 *q;
587     q = *pp;
588     while (*str)
589         *q++ = *str++;
590     *pp = q;
591 }
592
593 static void putle16(u8 **pp, int val)
594 {
595     u8 *q;
596     q = *pp;
597     *q++ = val;
598     *q++ = val >> 8;
599     *pp = q;
600 }
601
602 static void putle32(u8 **pp, int val)
603 {
604     u8 *q;
605     q = *pp;
606     *q++ = val;
607     *q++ = val >> 8;
608     *q++ = val >> 16;
609     *q++ = val >> 24;
610     *pp = q;
611 }
612
613 static unsigned long align(unsigned long addr, unsigned long v)
614 {
615     return (addr + v - 1) & ~(v - 1);
616 }
617
618 static void mptable_init(void)
619 {
620     u8 *mp_config_table, *q, *float_pointer_struct;
621     int ioapic_id, i, len;
622     int mp_config_table_size;
623
624 #if (CONFIG_QEMU == 1)
625     if (smp_cpus <= 1)
626         return;
627 #endif
628
629 #if (CONFIG_USE_EBDA_TABLES == 1)
630     mp_config_table = (u8 *)(GET_EBDA(ram_size) - CONFIG_ACPI_DATA_SIZE
631                              - MPTABLE_MAX_SIZE);
632 #else
633     bios_table_cur_addr = align(bios_table_cur_addr, 16);
634     mp_config_table = (u8 *)bios_table_cur_addr;
635 #endif
636     q = mp_config_table;
637     putstr(&q, "PCMP"); /* "PCMP signature */
638     putle16(&q, 0); /* table length (patched later) */
639     putb(&q, 4); /* spec rev */
640     putb(&q, 0); /* checksum (patched later) */
641 #if (CONFIG_QEMU == 1)
642     putstr(&q, "QEMUCPU "); /* OEM id */
643 #else
644     putstr(&q, "BOCHSCPU");
645 #endif
646     putstr(&q, "0.1         "); /* vendor id */
647     putle32(&q, 0); /* OEM table ptr */
648     putle16(&q, 0); /* OEM table size */
649     putle16(&q, smp_cpus + 18); /* entry count */
650     putle32(&q, 0xfee00000); /* local APIC addr */
651     putle16(&q, 0); /* ext table length */
652     putb(&q, 0); /* ext table checksum */
653     putb(&q, 0); /* reserved */
654
655     for(i = 0; i < smp_cpus; i++) {
656         putb(&q, 0); /* entry type = processor */
657         putb(&q, i); /* APIC id */
658         putb(&q, 0x11); /* local APIC version number */
659         if (i == 0)
660             putb(&q, 3); /* cpu flags: enabled, bootstrap cpu */
661         else
662             putb(&q, 1); /* cpu flags: enabled */
663         putb(&q, 0); /* cpu signature */
664         putb(&q, 6);
665         putb(&q, 0);
666         putb(&q, 0);
667         putle16(&q, 0x201); /* feature flags */
668         putle16(&q, 0);
669
670         putle16(&q, 0); /* reserved */
671         putle16(&q, 0);
672         putle16(&q, 0);
673         putle16(&q, 0);
674     }
675
676     /* isa bus */
677     putb(&q, 1); /* entry type = bus */
678     putb(&q, 0); /* bus ID */
679     putstr(&q, "ISA   ");
680
681     /* ioapic */
682     ioapic_id = smp_cpus;
683     putb(&q, 2); /* entry type = I/O APIC */
684     putb(&q, ioapic_id); /* apic ID */
685     putb(&q, 0x11); /* I/O APIC version number */
686     putb(&q, 1); /* enable */
687     putle32(&q, 0xfec00000); /* I/O APIC addr */
688
689     /* irqs */
690     for(i = 0; i < 16; i++) {
691         putb(&q, 3); /* entry type = I/O interrupt */
692         putb(&q, 0); /* interrupt type = vectored interrupt */
693         putb(&q, 0); /* flags: po=0, el=0 */
694         putb(&q, 0);
695         putb(&q, 0); /* source bus ID = ISA */
696         putb(&q, i); /* source bus IRQ */
697         putb(&q, ioapic_id); /* dest I/O APIC ID */
698         putb(&q, i); /* dest I/O APIC interrupt in */
699     }
700     /* patch length */
701     len = q - mp_config_table;
702     mp_config_table[4] = len;
703     mp_config_table[5] = len >> 8;
704
705     mp_config_table[7] = -checksum(mp_config_table, q - mp_config_table);
706
707     mp_config_table_size = q - mp_config_table;
708
709 #if (CONFIG_USE_EBDA_TABLES != 1)
710     bios_table_cur_addr += mp_config_table_size;
711 #endif
712
713     /* floating pointer structure */
714 #if (CONFIG_USE_EBDA_TABLES == 1)
715     ebda_cur_addr = align(ebda_cur_addr, 16);
716     float_pointer_struct = (u8 *)ebda_cur_addr;
717 #else
718     bios_table_cur_addr = align(bios_table_cur_addr, 16);
719     float_pointer_struct = (u8 *)bios_table_cur_addr;
720 #endif
721     q = float_pointer_struct;
722     putstr(&q, "_MP_");
723     /* pointer to MP config table */
724     putle32(&q, (unsigned long)mp_config_table);
725
726     putb(&q, 1); /* length in 16 byte units */
727     putb(&q, 4); /* MP spec revision */
728     putb(&q, 0); /* checksum (patched later) */
729     putb(&q, 0); /* MP feature byte 1 */
730
731     putb(&q, 0);
732     putb(&q, 0);
733     putb(&q, 0);
734     putb(&q, 0);
735     float_pointer_struct[10] = -checksum(float_pointer_struct
736                                          , q - float_pointer_struct);
737 #if (CONFIG_USE_EBDA_TABLES == 1)
738     ebda_cur_addr += (q - float_pointer_struct);
739 #else
740     bios_table_cur_addr += (q - float_pointer_struct);
741 #endif
742     BX_INFO("MP table addr=0x%08lx MPC table addr=0x%08lx size=0x%x\n",
743             (unsigned long)float_pointer_struct,
744             (unsigned long)mp_config_table,
745             mp_config_table_size);
746 }
747
748 /****************************************************/
749 /* ACPI tables init */
750
751 /* Table structure from Linux kernel (the ACPI tables are under the
752    BSD license) */
753
754 #define ACPI_TABLE_HEADER_DEF   /* ACPI common table header */ \
755         u8                            signature [4];          /* ACPI signature (4 ASCII characters) */\
756         u32                             length;                 /* Length of table, in bytes, including header */\
757         u8                              revision;               /* ACPI Specification minor version # */\
758         u8                              checksum;               /* To make sum of entire table == 0 */\
759         u8                            oem_id [6];             /* OEM identification */\
760         u8                            oem_table_id [8];       /* OEM table identification */\
761         u32                             oem_revision;           /* OEM revision number */\
762         u8                            asl_compiler_id [4];    /* ASL compiler vendor ID */\
763         u32                             asl_compiler_revision;  /* ASL compiler revision number */
764
765
766 struct acpi_table_header         /* ACPI common table header */
767 {
768         ACPI_TABLE_HEADER_DEF
769 };
770
771 struct rsdp_descriptor         /* Root System Descriptor Pointer */
772 {
773         u8                            signature [8];          /* ACPI signature, contains "RSD PTR " */
774         u8                              checksum;               /* To make sum of struct == 0 */
775         u8                            oem_id [6];             /* OEM identification */
776         u8                              revision;               /* Must be 0 for 1.0, 2 for 2.0 */
777         u32                             rsdt_physical_address;  /* 32-bit physical address of RSDT */
778         u32                             length;                 /* XSDT Length in bytes including hdr */
779         u64                             xsdt_physical_address;  /* 64-bit physical address of XSDT */
780         u8                              extended_checksum;      /* Checksum of entire table */
781         u8                            reserved [3];           /* Reserved field must be 0 */
782 };
783
784 /*
785  * ACPI 1.0 Root System Description Table (RSDT)
786  */
787 struct rsdt_descriptor_rev1
788 {
789         ACPI_TABLE_HEADER_DEF                           /* ACPI common table header */
790         u32                             table_offset_entry [3]; /* Array of pointers to other */
791                          /* ACPI tables */
792 };
793
794 /*
795  * ACPI 1.0 Firmware ACPI Control Structure (FACS)
796  */
797 struct facs_descriptor_rev1
798 {
799         u8                            signature[4];           /* ACPI Signature */
800         u32                             length;                 /* Length of structure, in bytes */
801         u32                             hardware_signature;     /* Hardware configuration signature */
802         u32                             firmware_waking_vector; /* ACPI OS waking vector */
803         u32                             global_lock;            /* Global Lock */
804         u32                             S4bios_f        : 1;    /* Indicates if S4BIOS support is present */
805         u32                             reserved1       : 31;   /* Must be 0 */
806         u8                              resverved3 [40];        /* Reserved - must be zero */
807 };
808
809
810 /*
811  * ACPI 1.0 Fixed ACPI Description Table (FADT)
812  */
813 struct fadt_descriptor_rev1
814 {
815         ACPI_TABLE_HEADER_DEF                           /* ACPI common table header */
816         u32                             firmware_ctrl;          /* Physical address of FACS */
817         u32                             dsdt;                   /* Physical address of DSDT */
818         u8                              model;                  /* System Interrupt Model */
819         u8                              reserved1;              /* Reserved */
820         u16                             sci_int;                /* System vector of SCI interrupt */
821         u32                             smi_cmd;                /* Port address of SMI command port */
822         u8                              acpi_enable;            /* Value to write to smi_cmd to enable ACPI */
823         u8                              acpi_disable;           /* Value to write to smi_cmd to disable ACPI */
824         u8                              S4bios_req;             /* Value to write to SMI CMD to enter S4BIOS state */
825         u8                              reserved2;              /* Reserved - must be zero */
826         u32                             pm1a_evt_blk;           /* Port address of Power Mgt 1a acpi_event Reg Blk */
827         u32                             pm1b_evt_blk;           /* Port address of Power Mgt 1b acpi_event Reg Blk */
828         u32                             pm1a_cnt_blk;           /* Port address of Power Mgt 1a Control Reg Blk */
829         u32                             pm1b_cnt_blk;           /* Port address of Power Mgt 1b Control Reg Blk */
830         u32                             pm2_cnt_blk;            /* Port address of Power Mgt 2 Control Reg Blk */
831         u32                             pm_tmr_blk;             /* Port address of Power Mgt Timer Ctrl Reg Blk */
832         u32                             gpe0_blk;               /* Port addr of General Purpose acpi_event 0 Reg Blk */
833         u32                             gpe1_blk;               /* Port addr of General Purpose acpi_event 1 Reg Blk */
834         u8                              pm1_evt_len;            /* Byte length of ports at pm1_x_evt_blk */
835         u8                              pm1_cnt_len;            /* Byte length of ports at pm1_x_cnt_blk */
836         u8                              pm2_cnt_len;            /* Byte Length of ports at pm2_cnt_blk */
837         u8                              pm_tmr_len;              /* Byte Length of ports at pm_tm_blk */
838         u8                              gpe0_blk_len;           /* Byte Length of ports at gpe0_blk */
839         u8                              gpe1_blk_len;           /* Byte Length of ports at gpe1_blk */
840         u8                              gpe1_base;              /* Offset in gpe model where gpe1 events start */
841         u8                              reserved3;              /* Reserved */
842         u16                             plvl2_lat;              /* Worst case HW latency to enter/exit C2 state */
843         u16                             plvl3_lat;              /* Worst case HW latency to enter/exit C3 state */
844         u16                             flush_size;             /* Size of area read to flush caches */
845         u16                             flush_stride;           /* Stride used in flushing caches */
846         u8                              duty_offset;            /* Bit location of duty cycle field in p_cnt reg */
847         u8                              duty_width;             /* Bit width of duty cycle field in p_cnt reg */
848         u8                              day_alrm;               /* Index to day-of-month alarm in RTC CMOS RAM */
849         u8                              mon_alrm;               /* Index to month-of-year alarm in RTC CMOS RAM */
850         u8                              century;                /* Index to century in RTC CMOS RAM */
851         u8                              reserved4;              /* Reserved */
852         u8                              reserved4a;             /* Reserved */
853         u8                              reserved4b;             /* Reserved */
854 #if 0
855         u32                             wb_invd         : 1;    /* The wbinvd instruction works properly */
856         u32                             wb_invd_flush   : 1;    /* The wbinvd flushes but does not invalidate */
857         u32                             proc_c1         : 1;    /* All processors support C1 state */
858         u32                             plvl2_up        : 1;    /* C2 state works on MP system */
859         u32                             pwr_button      : 1;    /* Power button is handled as a generic feature */
860         u32                             sleep_button    : 1;    /* Sleep button is handled as a generic feature, or not present */
861         u32                             fixed_rTC       : 1;    /* RTC wakeup stat not in fixed register space */
862         u32                             rtcs4           : 1;    /* RTC wakeup stat not possible from S4 */
863         u32                             tmr_val_ext     : 1;    /* The tmr_val width is 32 bits (0 = 24 bits) */
864         u32                             reserved5       : 23;   /* Reserved - must be zero */
865 #else
866         u32 flags;
867 #endif
868 };
869
870 /*
871  * MADT values and structures
872  */
873
874 /* Values for MADT PCATCompat */
875
876 #define DUAL_PIC                0
877 #define MULTIPLE_APIC           1
878
879
880 /* Master MADT */
881
882 struct multiple_apic_table
883 {
884         ACPI_TABLE_HEADER_DEF                           /* ACPI common table header */
885         u32                             local_apic_address;     /* Physical address of local APIC */
886 #if 0
887         u32                             PCATcompat      : 1;    /* A one indicates system also has dual 8259s */
888         u32                             reserved1       : 31;
889 #else
890         u32                             flags;
891 #endif
892 };
893
894
895 /* Values for Type in APIC_HEADER_DEF */
896
897 #define APIC_PROCESSOR          0
898 #define APIC_IO                 1
899 #define APIC_XRUPT_OVERRIDE     2
900 #define APIC_NMI                3
901 #define APIC_LOCAL_NMI          4
902 #define APIC_ADDRESS_OVERRIDE   5
903 #define APIC_IO_SAPIC           6
904 #define APIC_LOCAL_SAPIC        7
905 #define APIC_XRUPT_SOURCE       8
906 #define APIC_RESERVED           9           /* 9 and greater are reserved */
907
908 /*
909  * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
910  */
911 #define APIC_HEADER_DEF                     /* Common APIC sub-structure header */\
912         u8                              type; \
913         u8                              length;
914
915 /* Sub-structures for MADT */
916
917 struct madt_processor_apic
918 {
919         APIC_HEADER_DEF
920         u8                              processor_id;           /* ACPI processor id */
921         u8                              local_apic_id;          /* Processor's local APIC id */
922 #if 0
923         u32                             processor_enabled: 1;   /* Processor is usable if set */
924         u32                             reserved2       : 31;   /* Reserved, must be zero */
925 #else
926         u32 flags;
927 #endif
928 };
929
930 struct madt_io_apic
931 {
932         APIC_HEADER_DEF
933         u8                              io_apic_id;             /* I/O APIC ID */
934         u8                              reserved;               /* Reserved - must be zero */
935         u32                             address;                /* APIC physical address */
936         u32                             interrupt;              /* Global system interrupt where INTI
937                           * lines start */
938 };
939
940 #include "acpi-dsdt.hex"
941
942 static inline u16 cpu_to_le16(u16 x)
943 {
944     return x;
945 }
946
947 static inline u32 cpu_to_le32(u32 x)
948 {
949     return x;
950 }
951
952 static void acpi_build_table_header(struct acpi_table_header *h,
953                                     char *sig, int len, u8 rev)
954 {
955     memcpy(h->signature, sig, 4);
956     h->length = cpu_to_le32(len);
957     h->revision = rev;
958 #if (CONFIG_QEMU == 1)
959     memcpy(h->oem_id, "QEMU  ", 6);
960     memcpy(h->oem_table_id, "QEMU", 4);
961 #else
962     memcpy(h->oem_id, "BOCHS ", 6);
963     memcpy(h->oem_table_id, "BXPC", 4);
964 #endif
965     memcpy(h->oem_table_id + 4, sig, 4);
966     h->oem_revision = cpu_to_le32(1);
967 #if (CONFIG_QEMU == 1)
968     memcpy(h->asl_compiler_id, "QEMU", 4);
969 #else
970     memcpy(h->asl_compiler_id, "BXPC", 4);
971 #endif
972     h->asl_compiler_revision = cpu_to_le32(1);
973     h->checksum = -checksum((void *)h, len);
974 }
975
976 int acpi_build_processor_ssdt(u8 *ssdt)
977 {
978     u8 *ssdt_ptr = ssdt;
979     int i, length;
980     int acpi_cpus = smp_cpus > 0xff ? 0xff : smp_cpus;
981
982     ssdt_ptr[9] = 0; // checksum;
983     ssdt_ptr += sizeof(struct acpi_table_header);
984
985     // caluculate the length of processor block and scope block excluding PkgLength
986     length = 0x0d * acpi_cpus + 4;
987
988     // build processor scope header
989     *(ssdt_ptr++) = 0x10; // ScopeOp
990     if (length <= 0x3e) {
991         *(ssdt_ptr++) = length + 1;
992     } else {
993         *(ssdt_ptr++) = 0x7F;
994         *(ssdt_ptr++) = (length + 2) >> 6;
995     }
996     *(ssdt_ptr++) = '_'; // Name
997     *(ssdt_ptr++) = 'P';
998     *(ssdt_ptr++) = 'R';
999     *(ssdt_ptr++) = '_';
1000
1001     // build object for each processor
1002     for(i=0;i<acpi_cpus;i++) {
1003         *(ssdt_ptr++) = 0x5B; // ProcessorOp
1004         *(ssdt_ptr++) = 0x83;
1005         *(ssdt_ptr++) = 0x0B; // Length
1006         *(ssdt_ptr++) = 'C';  // Name (CPUxx)
1007         *(ssdt_ptr++) = 'P';
1008         if ((i & 0xf0) != 0)
1009             *(ssdt_ptr++) = (i >> 4) < 0xa ? (i >> 4) + '0' : (i >> 4) + 'A' - 0xa;
1010         else
1011             *(ssdt_ptr++) = 'U';
1012         *(ssdt_ptr++) = (i & 0xf) < 0xa ? (i & 0xf) + '0' : (i & 0xf) + 'A' - 0xa;
1013         *(ssdt_ptr++) = i;
1014         *(ssdt_ptr++) = 0x10; // Processor block address
1015         *(ssdt_ptr++) = 0xb0;
1016         *(ssdt_ptr++) = 0;
1017         *(ssdt_ptr++) = 0;
1018         *(ssdt_ptr++) = 6;    // Processor block length
1019     }
1020
1021     acpi_build_table_header((struct acpi_table_header *)ssdt,
1022                             "SSDT", ssdt_ptr - ssdt, 1);
1023
1024     return ssdt_ptr - ssdt;
1025 }
1026
1027 /* base_addr must be a multiple of 4KB */
1028 void acpi_bios_init(void)
1029 {
1030     struct rsdp_descriptor *rsdp;
1031     struct rsdt_descriptor_rev1 *rsdt;
1032     struct fadt_descriptor_rev1 *fadt;
1033     struct facs_descriptor_rev1 *facs;
1034     struct multiple_apic_table *madt;
1035     u8 *dsdt, *ssdt;
1036     u32 base_addr, rsdt_addr, fadt_addr, addr, facs_addr, dsdt_addr, ssdt_addr;
1037     u32 acpi_tables_size, madt_addr, madt_size;
1038     int i;
1039
1040     /* reserve memory space for tables */
1041 #if (CONFIG_USE_EBDA_TABLES == 1)
1042     ebda_cur_addr = align(ebda_cur_addr, 16);
1043     rsdp = (void *)(ebda_cur_addr);
1044     ebda_cur_addr += sizeof(*rsdp);
1045 #else
1046     bios_table_cur_addr = align(bios_table_cur_addr, 16);
1047     rsdp = (void *)(bios_table_cur_addr);
1048     bios_table_cur_addr += sizeof(*rsdp);
1049 #endif
1050
1051     addr = base_addr = GET_EBDA(ram_size) - CONFIG_ACPI_DATA_SIZE;
1052     rsdt_addr = addr;
1053     rsdt = (void *)(addr);
1054     addr += sizeof(*rsdt);
1055
1056     fadt_addr = addr;
1057     fadt = (void *)(addr);
1058     addr += sizeof(*fadt);
1059
1060     /* XXX: FACS should be in RAM */
1061     addr = (addr + 63) & ~63; /* 64 byte alignment for FACS */
1062     facs_addr = addr;
1063     facs = (void *)(addr);
1064     addr += sizeof(*facs);
1065
1066     dsdt_addr = addr;
1067     dsdt = (void *)(addr);
1068     addr += sizeof(AmlCode);
1069
1070     ssdt_addr = addr;
1071     ssdt = (void *)(addr);
1072     addr += acpi_build_processor_ssdt(ssdt);
1073
1074     addr = (addr + 7) & ~7;
1075     madt_addr = addr;
1076     madt_size = sizeof(*madt) +
1077         sizeof(struct madt_processor_apic) * smp_cpus +
1078         sizeof(struct madt_io_apic);
1079     madt = (void *)(addr);
1080     addr += madt_size;
1081
1082     acpi_tables_size = addr - base_addr;
1083
1084     BX_INFO("ACPI tables: RSDP addr=0x%08lx ACPI DATA addr=0x%08lx size=0x%x\n",
1085             (unsigned long)rsdp,
1086             (unsigned long)rsdt, acpi_tables_size);
1087
1088     /* RSDP */
1089     memset(rsdp, 0, sizeof(*rsdp));
1090     memcpy(rsdp->signature, "RSD PTR ", 8);
1091 #if (CONFIG_QEMU == 1)
1092     memcpy(rsdp->oem_id, "QEMU  ", 6);
1093 #else
1094     memcpy(rsdp->oem_id, "BOCHS ", 6);
1095 #endif
1096     rsdp->rsdt_physical_address = cpu_to_le32(rsdt_addr);
1097     rsdp->checksum = -checksum((void *)rsdp, 20);
1098
1099     /* RSDT */
1100     memset(rsdt, 0, sizeof(*rsdt));
1101     rsdt->table_offset_entry[0] = cpu_to_le32(fadt_addr);
1102     rsdt->table_offset_entry[1] = cpu_to_le32(madt_addr);
1103     rsdt->table_offset_entry[2] = cpu_to_le32(ssdt_addr);
1104     acpi_build_table_header((struct acpi_table_header *)rsdt,
1105                             "RSDT", sizeof(*rsdt), 1);
1106
1107     /* FADT */
1108     memset(fadt, 0, sizeof(*fadt));
1109     fadt->firmware_ctrl = cpu_to_le32(facs_addr);
1110     fadt->dsdt = cpu_to_le32(dsdt_addr);
1111     fadt->model = 1;
1112     fadt->reserved1 = 0;
1113     fadt->sci_int = cpu_to_le16(pm_sci_int);
1114     fadt->smi_cmd = cpu_to_le32(SMI_CMD_IO_ADDR);
1115     fadt->acpi_enable = 0xf1;
1116     fadt->acpi_disable = 0xf0;
1117     fadt->pm1a_evt_blk = cpu_to_le32(pm_io_base);
1118     fadt->pm1a_cnt_blk = cpu_to_le32(pm_io_base + 0x04);
1119     fadt->pm_tmr_blk = cpu_to_le32(pm_io_base + 0x08);
1120     fadt->pm1_evt_len = 4;
1121     fadt->pm1_cnt_len = 2;
1122     fadt->pm_tmr_len = 4;
1123     fadt->plvl2_lat = cpu_to_le16(0xfff); // C2 state not supported
1124     fadt->plvl3_lat = cpu_to_le16(0xfff); // C3 state not supported
1125     /* WBINVD + PROC_C1 + PWR_BUTTON + SLP_BUTTON + FIX_RTC */
1126     fadt->flags = cpu_to_le32((1 << 0) | (1 << 2) | (1 << 4) | (1 << 5) | (1 << 6));
1127     acpi_build_table_header((struct acpi_table_header *)fadt, "FACP",
1128                             sizeof(*fadt), 1);
1129
1130     /* FACS */
1131     memset(facs, 0, sizeof(*facs));
1132     memcpy(facs->signature, "FACS", 4);
1133     facs->length = cpu_to_le32(sizeof(*facs));
1134
1135     /* DSDT */
1136     memcpy(dsdt, AmlCode, sizeof(AmlCode));
1137
1138     /* MADT */
1139     {
1140         struct madt_processor_apic *apic;
1141         struct madt_io_apic *io_apic;
1142
1143         memset(madt, 0, madt_size);
1144         madt->local_apic_address = cpu_to_le32(0xfee00000);
1145         madt->flags = cpu_to_le32(1);
1146         apic = (void *)(madt + 1);
1147         for(i=0;i<smp_cpus;i++) {
1148             apic->type = APIC_PROCESSOR;
1149             apic->length = sizeof(*apic);
1150             apic->processor_id = i;
1151             apic->local_apic_id = i;
1152             apic->flags = cpu_to_le32(1);
1153             apic++;
1154         }
1155         io_apic = (void *)apic;
1156         io_apic->type = APIC_IO;
1157         io_apic->length = sizeof(*io_apic);
1158         io_apic->io_apic_id = smp_cpus;
1159         io_apic->address = cpu_to_le32(0xfec00000);
1160         io_apic->interrupt = cpu_to_le32(0);
1161
1162         acpi_build_table_header((struct acpi_table_header *)madt,
1163                                 "APIC", madt_size, 1);
1164     }
1165 }
1166
1167 /* SMBIOS entry point -- must be written to a 16-bit aligned address
1168    between 0xf0000 and 0xfffff.
1169  */
1170 struct smbios_entry_point {
1171         char anchor_string[4];
1172         u8 checksum;
1173         u8 length;
1174         u8 smbios_major_version;
1175         u8 smbios_minor_version;
1176         u16 max_structure_size;
1177         u8 entry_point_revision;
1178         u8 formatted_area[5];
1179         char intermediate_anchor_string[5];
1180         u8 intermediate_checksum;
1181         u16 structure_table_length;
1182         u32 structure_table_address;
1183         u16 number_of_structures;
1184         u8 smbios_bcd_revision;
1185 } __attribute__((__packed__));
1186
1187 /* This goes at the beginning of every SMBIOS structure. */
1188 struct smbios_structure_header {
1189         u8 type;
1190         u8 length;
1191         u16 handle;
1192 } __attribute__((__packed__));
1193
1194 /* SMBIOS type 0 - BIOS Information */
1195 struct smbios_type_0 {
1196         struct smbios_structure_header header;
1197         u8 vendor_str;
1198         u8 bios_version_str;
1199         u16 bios_starting_address_segment;
1200         u8 bios_release_date_str;
1201         u8 bios_rom_size;
1202         u8 bios_characteristics[8];
1203         u8 bios_characteristics_extension_bytes[2];
1204         u8 system_bios_major_release;
1205         u8 system_bios_minor_release;
1206         u8 embedded_controller_major_release;
1207         u8 embedded_controller_minor_release;
1208 } __attribute__((__packed__));
1209
1210 /* SMBIOS type 1 - System Information */
1211 struct smbios_type_1 {
1212         struct smbios_structure_header header;
1213         u8 manufacturer_str;
1214         u8 product_name_str;
1215         u8 version_str;
1216         u8 serial_number_str;
1217         u8 uuid[16];
1218         u8 wake_up_type;
1219         u8 sku_number_str;
1220         u8 family_str;
1221 } __attribute__((__packed__));
1222
1223 /* SMBIOS type 3 - System Enclosure (v2.3) */
1224 struct smbios_type_3 {
1225         struct smbios_structure_header header;
1226         u8 manufacturer_str;
1227         u8 type;
1228         u8 version_str;
1229         u8 serial_number_str;
1230         u8 asset_tag_number_str;
1231         u8 boot_up_state;
1232         u8 power_supply_state;
1233         u8 thermal_state;
1234         u8 security_status;
1235     u32 oem_defined;
1236     u8 height;
1237     u8 number_of_power_cords;
1238     u8 contained_element_count;
1239     // contained elements follow
1240 } __attribute__((__packed__));
1241
1242 /* SMBIOS type 4 - Processor Information (v2.0) */
1243 struct smbios_type_4 {
1244         struct smbios_structure_header header;
1245         u8 socket_designation_str;
1246         u8 processor_type;
1247         u8 processor_family;
1248         u8 processor_manufacturer_str;
1249         u32 processor_id[2];
1250         u8 processor_version_str;
1251         u8 voltage;
1252         u16 external_clock;
1253         u16 max_speed;
1254         u16 current_speed;
1255         u8 status;
1256         u8 processor_upgrade;
1257 } __attribute__((__packed__));
1258
1259 /* SMBIOS type 16 - Physical Memory Array
1260  *   Associated with one type 17 (Memory Device).
1261  */
1262 struct smbios_type_16 {
1263         struct smbios_structure_header header;
1264         u8 location;
1265         u8 use;
1266         u8 error_correction;
1267         u32 maximum_capacity;
1268         u16 memory_error_information_handle;
1269         u16 number_of_memory_devices;
1270 } __attribute__((__packed__));
1271
1272 /* SMBIOS type 17 - Memory Device
1273  *   Associated with one type 19
1274  */
1275 struct smbios_type_17 {
1276         struct smbios_structure_header header;
1277         u16 physical_memory_array_handle;
1278         u16 memory_error_information_handle;
1279         u16 total_width;
1280         u16 data_width;
1281         u16 size;
1282         u8 form_factor;
1283         u8 device_set;
1284         u8 device_locator_str;
1285         u8 bank_locator_str;
1286         u8 memory_type;
1287         u16 type_detail;
1288 } __attribute__((__packed__));
1289
1290 /* SMBIOS type 19 - Memory Array Mapped Address */
1291 struct smbios_type_19 {
1292         struct smbios_structure_header header;
1293         u32 starting_address;
1294         u32 ending_address;
1295         u16 memory_array_handle;
1296         u8 partition_width;
1297 } __attribute__((__packed__));
1298
1299 /* SMBIOS type 20 - Memory Device Mapped Address */
1300 struct smbios_type_20 {
1301         struct smbios_structure_header header;
1302         u32 starting_address;
1303         u32 ending_address;
1304         u16 memory_device_handle;
1305         u16 memory_array_mapped_address_handle;
1306         u8 partition_row_position;
1307         u8 interleave_position;
1308         u8 interleaved_data_depth;
1309 } __attribute__((__packed__));
1310
1311 /* SMBIOS type 32 - System Boot Information */
1312 struct smbios_type_32 {
1313         struct smbios_structure_header header;
1314         u8 reserved[6];
1315         u8 boot_status;
1316 } __attribute__((__packed__));
1317
1318 /* SMBIOS type 127 -- End-of-table */
1319 struct smbios_type_127 {
1320         struct smbios_structure_header header;
1321 } __attribute__((__packed__));
1322
1323 static void
1324 smbios_entry_point_init(void *start,
1325                         u16 max_structure_size,
1326                         u16 structure_table_length,
1327                         u32 structure_table_address,
1328                         u16 number_of_structures)
1329 {
1330     struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
1331
1332     memcpy(ep->anchor_string, "_SM_", 4);
1333     ep->length = 0x1f;
1334     ep->smbios_major_version = 2;
1335     ep->smbios_minor_version = 4;
1336     ep->max_structure_size = max_structure_size;
1337     ep->entry_point_revision = 0;
1338     memset(ep->formatted_area, 0, 5);
1339     memcpy(ep->intermediate_anchor_string, "_DMI_", 5);
1340
1341     ep->structure_table_length = structure_table_length;
1342     ep->structure_table_address = structure_table_address;
1343     ep->number_of_structures = number_of_structures;
1344     ep->smbios_bcd_revision = 0x24;
1345
1346     ep->checksum = 0;
1347     ep->intermediate_checksum = 0;
1348
1349     ep->checksum = -checksum(start, 0x10);
1350
1351     ep->intermediate_checksum = -checksum(start + 0x10, ep->length - 0x10);
1352 }
1353
1354 /* Type 0 -- BIOS Information */
1355 #define RELEASE_DATE_STR "01/01/2007"
1356 static void *
1357 smbios_type_0_init(void *start)
1358 {
1359     struct smbios_type_0 *p = (struct smbios_type_0 *)start;
1360
1361     p->header.type = 0;
1362     p->header.length = sizeof(struct smbios_type_0);
1363     p->header.handle = 0;
1364
1365     p->vendor_str = 1;
1366     p->bios_version_str = 1;
1367     p->bios_starting_address_segment = 0xe800;
1368     p->bios_release_date_str = 2;
1369     p->bios_rom_size = 0; /* FIXME */
1370
1371     memset(p->bios_characteristics, 0, 7);
1372     p->bios_characteristics[7] = 0x08; /* BIOS characteristics not supported */
1373     p->bios_characteristics_extension_bytes[0] = 0;
1374     p->bios_characteristics_extension_bytes[1] = 0;
1375
1376     p->system_bios_major_release = 1;
1377     p->system_bios_minor_release = 0;
1378     p->embedded_controller_major_release = 0xff;
1379     p->embedded_controller_minor_release = 0xff;
1380
1381     start += sizeof(struct smbios_type_0);
1382     memcpy((char *)start, CONFIG_APPNAME, sizeof(CONFIG_APPNAME));
1383     start += sizeof(CONFIG_APPNAME);
1384     memcpy((char *)start, RELEASE_DATE_STR, sizeof(RELEASE_DATE_STR));
1385     start += sizeof(RELEASE_DATE_STR);
1386     *((u8 *)start) = 0;
1387
1388     return start+1;
1389 }
1390
1391 /* Type 1 -- System Information */
1392 static void *
1393 smbios_type_1_init(void *start)
1394 {
1395     struct smbios_type_1 *p = (struct smbios_type_1 *)start;
1396     p->header.type = 1;
1397     p->header.length = sizeof(struct smbios_type_1);
1398     p->header.handle = 0x100;
1399
1400     p->manufacturer_str = 0;
1401     p->product_name_str = 0;
1402     p->version_str = 0;
1403     p->serial_number_str = 0;
1404
1405     memcpy(p->uuid, bios_uuid, 16);
1406
1407     p->wake_up_type = 0x06; /* power switch */
1408     p->sku_number_str = 0;
1409     p->family_str = 0;
1410
1411     start += sizeof(struct smbios_type_1);
1412     *((u16 *)start) = 0;
1413
1414     return start+2;
1415 }
1416
1417 /* Type 3 -- System Enclosure */
1418 static void *
1419 smbios_type_3_init(void *start)
1420 {
1421     struct smbios_type_3 *p = (struct smbios_type_3 *)start;
1422
1423     p->header.type = 3;
1424     p->header.length = sizeof(struct smbios_type_3);
1425     p->header.handle = 0x300;
1426
1427     p->manufacturer_str = 0;
1428     p->type = 0x01; /* other */
1429     p->version_str = 0;
1430     p->serial_number_str = 0;
1431     p->asset_tag_number_str = 0;
1432     p->boot_up_state = 0x03; /* safe */
1433     p->power_supply_state = 0x03; /* safe */
1434     p->thermal_state = 0x03; /* safe */
1435     p->security_status = 0x02; /* unknown */
1436     p->oem_defined = 0;
1437     p->height = 0;
1438     p->number_of_power_cords = 0;
1439     p->contained_element_count = 0;
1440
1441     start += sizeof(struct smbios_type_3);
1442     *((u16 *)start) = 0;
1443
1444     return start+2;
1445 }
1446
1447 /* Type 4 -- Processor Information */
1448 static void *
1449 smbios_type_4_init(void *start, unsigned int cpu_number)
1450 {
1451     struct smbios_type_4 *p = (struct smbios_type_4 *)start;
1452
1453     p->header.type = 4;
1454     p->header.length = sizeof(struct smbios_type_4);
1455     p->header.handle = 0x400 + cpu_number;
1456
1457     p->socket_designation_str = 1;
1458     p->processor_type = 0x03; /* CPU */
1459     p->processor_family = 0x01; /* other */
1460     p->processor_manufacturer_str = 0;
1461
1462     p->processor_id[0] = cpuid_signature;
1463     p->processor_id[1] = cpuid_features;
1464
1465     p->processor_version_str = 0;
1466     p->voltage = 0;
1467     p->external_clock = 0;
1468
1469     p->max_speed = 0; /* unknown */
1470     p->current_speed = 0; /* unknown */
1471
1472     p->status = 0x41; /* socket populated, CPU enabled */
1473     p->processor_upgrade = 0x01; /* other */
1474
1475     start += sizeof(struct smbios_type_4);
1476
1477     memcpy((char *)start, "CPU  " "\0" "" "\0" "", 7);
1478         ((char *)start)[4] = cpu_number + '0';
1479
1480     return start+7;
1481 }
1482
1483 /* Type 16 -- Physical Memory Array */
1484 static void *
1485 smbios_type_16_init(void *start, u32 memsize)
1486 {
1487     struct smbios_type_16 *p = (struct smbios_type_16*)start;
1488
1489     p->header.type = 16;
1490     p->header.length = sizeof(struct smbios_type_16);
1491     p->header.handle = 0x1000;
1492
1493     p->location = 0x01; /* other */
1494     p->use = 0x03; /* system memory */
1495     p->error_correction = 0x01; /* other */
1496     p->maximum_capacity = memsize * 1024;
1497     p->memory_error_information_handle = 0xfffe; /* none provided */
1498     p->number_of_memory_devices = 1;
1499
1500     start += sizeof(struct smbios_type_16);
1501     *((u16 *)start) = 0;
1502
1503     return start + 2;
1504 }
1505
1506 /* Type 17 -- Memory Device */
1507 static void *
1508 smbios_type_17_init(void *start, u32 memory_size_mb)
1509 {
1510     struct smbios_type_17 *p = (struct smbios_type_17 *)start;
1511
1512     p->header.type = 17;
1513     p->header.length = sizeof(struct smbios_type_17);
1514     p->header.handle = 0x1100;
1515
1516     p->physical_memory_array_handle = 0x1000;
1517     p->total_width = 64;
1518     p->data_width = 64;
1519     /* truncate memory_size_mb to 16 bits and clear most significant
1520        bit [indicates size in MB] */
1521     p->size = (u16) memory_size_mb & 0x7fff;
1522     p->form_factor = 0x09; /* DIMM */
1523     p->device_set = 0;
1524     p->device_locator_str = 1;
1525     p->bank_locator_str = 0;
1526     p->memory_type = 0x07; /* RAM */
1527     p->type_detail = 0;
1528
1529     start += sizeof(struct smbios_type_17);
1530     memcpy((char *)start, "DIMM 1", 7);
1531     start += 7;
1532     *((u8 *)start) = 0;
1533
1534     return start+1;
1535 }
1536
1537 /* Type 19 -- Memory Array Mapped Address */
1538 static void *
1539 smbios_type_19_init(void *start, u32 memory_size_mb)
1540 {
1541     struct smbios_type_19 *p = (struct smbios_type_19 *)start;
1542
1543     p->header.type = 19;
1544     p->header.length = sizeof(struct smbios_type_19);
1545     p->header.handle = 0x1300;
1546
1547     p->starting_address = 0;
1548     p->ending_address = (memory_size_mb-1) * 1024;
1549     p->memory_array_handle = 0x1000;
1550     p->partition_width = 1;
1551
1552     start += sizeof(struct smbios_type_19);
1553     *((u16 *)start) = 0;
1554
1555     return start + 2;
1556 }
1557
1558 /* Type 20 -- Memory Device Mapped Address */
1559 static void *
1560 smbios_type_20_init(void *start, u32 memory_size_mb)
1561 {
1562     struct smbios_type_20 *p = (struct smbios_type_20 *)start;
1563
1564     p->header.type = 20;
1565     p->header.length = sizeof(struct smbios_type_20);
1566     p->header.handle = 0x1400;
1567
1568     p->starting_address = 0;
1569     p->ending_address = (memory_size_mb-1)*1024;
1570     p->memory_device_handle = 0x1100;
1571     p->memory_array_mapped_address_handle = 0x1300;
1572     p->partition_row_position = 1;
1573     p->interleave_position = 0;
1574     p->interleaved_data_depth = 0;
1575
1576     start += sizeof(struct smbios_type_20);
1577
1578     *((u16 *)start) = 0;
1579     return start+2;
1580 }
1581
1582 /* Type 32 -- System Boot Information */
1583 static void *
1584 smbios_type_32_init(void *start)
1585 {
1586     struct smbios_type_32 *p = (struct smbios_type_32 *)start;
1587
1588     p->header.type = 32;
1589     p->header.length = sizeof(struct smbios_type_32);
1590     p->header.handle = 0x2000;
1591     memset(p->reserved, 0, 6);
1592     p->boot_status = 0; /* no errors detected */
1593
1594     start += sizeof(struct smbios_type_32);
1595     *((u16 *)start) = 0;
1596
1597     return start+2;
1598 }
1599
1600 /* Type 127 -- End of Table */
1601 static void *
1602 smbios_type_127_init(void *start)
1603 {
1604     struct smbios_type_127 *p = (struct smbios_type_127 *)start;
1605
1606     p->header.type = 127;
1607     p->header.length = sizeof(struct smbios_type_127);
1608     p->header.handle = 0x7f00;
1609
1610     start += sizeof(struct smbios_type_127);
1611     *((u16 *)start) = 0;
1612
1613     return start + 2;
1614 }
1615
1616 void smbios_init(void)
1617 {
1618     unsigned cpu_num, nr_structs = 0, max_struct_size = 0;
1619     char *start, *p, *q;
1620     int memsize = GET_EBDA(ram_size) / (1024 * 1024);
1621
1622 #if (CONFIG_USE_EBDA_TABLES == 1)
1623     ebda_cur_addr = align(ebda_cur_addr, 16);
1624     start = (void *)(ebda_cur_addr);
1625 #else
1626     bios_table_cur_addr = align(bios_table_cur_addr, 16);
1627     start = (void *)(bios_table_cur_addr);
1628 #endif
1629
1630     p = (char *)start + sizeof(struct smbios_entry_point);
1631
1632 #define add_struct(fn) { \
1633     q = (fn); \
1634     nr_structs++; \
1635     if ((q - p) > max_struct_size) \
1636         max_struct_size = q - p; \
1637     p = q; \
1638 }
1639
1640     add_struct(smbios_type_0_init(p));
1641     add_struct(smbios_type_1_init(p));
1642     add_struct(smbios_type_3_init(p));
1643     for (cpu_num = 1; cpu_num <= smp_cpus; cpu_num++)
1644         add_struct(smbios_type_4_init(p, cpu_num));
1645     add_struct(smbios_type_16_init(p, memsize));
1646     add_struct(smbios_type_17_init(p, memsize));
1647     add_struct(smbios_type_19_init(p, memsize));
1648     add_struct(smbios_type_20_init(p, memsize));
1649     add_struct(smbios_type_32_init(p));
1650     add_struct(smbios_type_127_init(p));
1651
1652 #undef add_struct
1653
1654     smbios_entry_point_init(
1655         start, max_struct_size,
1656         (p - (char *)start) - sizeof(struct smbios_entry_point),
1657         (u32)(start + sizeof(struct smbios_entry_point)),
1658         nr_structs);
1659
1660 #if (CONFIG_USE_EBDA_TABLES == 1)
1661     ebda_cur_addr += (p - (char *)start);
1662 #else
1663     bios_table_cur_addr += (p - (char *)start);
1664 #endif
1665
1666     BX_INFO("SMBIOS table addr=0x%08lx\n", (unsigned long)start);
1667 }
1668
1669 void rombios32_init(void)
1670 {
1671     BX_INFO("Starting rombios32\n");
1672
1673 #if (CONFIG_USE_EBDA_TABLES == 1)
1674     ebda_cur_addr = ((*(u16 *)(0x40e)) << 4) + 0x380;
1675     BX_INFO("ebda_cur_addr: 0x%08lx\n", ebda_cur_addr);
1676 #endif
1677
1678     cpu_probe();
1679
1680     smp_probe();
1681
1682     pci_bios_init();
1683
1684     if (bios_table_cur_addr != 0) {
1685
1686         mptable_init();
1687
1688         uuid_probe();
1689
1690         smbios_init();
1691
1692         if (acpi_enabled)
1693             acpi_bios_init();
1694
1695         bios_lock_shadow_ram();
1696
1697         BX_INFO("bios_table_cur_addr: 0x%08lx\n", bios_table_cur_addr);
1698         if (bios_table_cur_addr > bios_table_end_addr)
1699             BX_PANIC("bios_table_end_addr overflow!\n");
1700     }
1701 }