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