1 /////////////////////////////////////////////////////////////////////////
2 // $Id: rombios32.c,v 1.11 2007/08/03 13:56:13 vruppert Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // 32 bit Bochs BIOS init code
6 // Copyright (C) 2006 Fabrice Bellard
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 typedef signed char int8_t;
27 typedef short int16_t;
29 typedef long long int64_t;
30 typedef unsigned char uint8_t;
31 typedef unsigned short uint16_t;
32 typedef unsigned int uint32_t;
33 typedef unsigned long long uint64_t;
35 /* if true, put the MP float table and ACPI RSDT in EBDA and the MP
36 table in RAM. Unfortunately, Linux has bugs with that, so we prefer
37 to modify the BIOS in shadow RAM */
38 //#define BX_USE_EBDA_TABLES
40 /* define it if the (emulated) hardware supports SMM mode */
43 #define cpuid(index, eax, ebx, ecx, edx) \
44 asm volatile ("cpuid" \
45 : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) \
48 #define wbinvd() asm volatile("wbinvd")
50 #define CPUID_APIC (1 << 9)
52 #define APIC_BASE ((uint8_t *)0xfee00000)
53 #define APIC_ICR_LOW 0x300
54 #define APIC_SVR 0x0F0
56 #define APIC_LVT3 0x370
59 #define PCI_ISA_IRQ_MASK 0x0e20U
61 #define APIC_ENABLED 0x0100
63 #define AP_BOOT_ADDR 0x9f000
65 #define MPTABLE_MAX_SIZE 0x00002000
66 #define SMI_CMD_IO_ADDR 0xb2
68 #define BIOS_TMP_STORAGE 0x00030000 /* 64 KB used to copy the BIOS to shadow RAM */
70 #define MSR_MTRRcap 0x000000fe
71 #define MSR_MTRRfix64K_00000 0x00000250
72 #define MSR_MTRRfix16K_80000 0x00000258
73 #define MSR_MTRRfix16K_A0000 0x00000259
74 #define MSR_MTRRfix4K_C0000 0x00000268
75 #define MSR_MTRRfix4K_C8000 0x00000269
76 #define MSR_MTRRfix4K_D0000 0x0000026a
77 #define MSR_MTRRfix4K_D8000 0x0000026b
78 #define MSR_MTRRfix4K_E0000 0x0000026c
79 #define MSR_MTRRfix4K_E8000 0x0000026d
80 #define MSR_MTRRfix4K_F0000 0x0000026e
81 #define MSR_MTRRfix4K_F8000 0x0000026f
82 #define MSR_MTRRdefType 0x000002ff
84 #define MTRRphysBase_MSR(reg) (0x200 + 2 * (reg))
85 #define MTRRphysMask_MSR(reg) (0x200 + 2 * (reg) + 1)
87 static inline void outl(int addr
, int val
)
89 asm volatile ("outl %1, %w0" : : "d" (addr
), "a" (val
));
92 static inline void outw(int addr
, int val
)
94 asm volatile ("outw %w1, %w0" : : "d" (addr
), "a" (val
));
97 static inline void outb(int addr
, int val
)
99 asm volatile ("outb %b1, %w0" : : "d" (addr
), "a" (val
));
102 static inline uint32_t inl(int addr
)
105 asm volatile ("inl %w1, %0" : "=a" (val
) : "d" (addr
));
109 static inline uint16_t inw(int addr
)
112 asm volatile ("inw %w1, %w0" : "=a" (val
) : "d" (addr
));
116 static inline uint8_t inb(int addr
)
119 asm volatile ("inb %w1, %b0" : "=a" (val
) : "d" (addr
));
123 static inline void writel(void *addr
, uint32_t val
)
125 *(volatile uint32_t *)addr
= val
;
128 static inline void writew(void *addr
, uint16_t val
)
130 *(volatile uint16_t *)addr
= val
;
133 static inline void writeb(void *addr
, uint8_t val
)
135 *(volatile uint8_t *)addr
= val
;
138 static inline uint32_t readl(const void *addr
)
140 return *(volatile const uint32_t *)addr
;
143 static inline uint16_t readw(const void *addr
)
145 return *(volatile const uint16_t *)addr
;
148 static inline uint8_t readb(const void *addr
)
150 return *(volatile const uint8_t *)addr
;
153 static inline void putc(int c
)
158 static uint64_t rdmsr(unsigned index
)
160 unsigned long long ret
;
162 asm ("rdmsr" : "=A"(ret
) : "c"(index
));
166 static void wrmsr(unsigned index
, uint64_t val
)
168 asm volatile ("wrmsr" : : "c"(index
), "A"(val
));
171 static inline int isdigit(int c
)
173 return c
>= '0' && c
<= '9';
176 void *memset(void *d1
, int val
, size_t len
)
186 void *memcpy(void *d1
, const void *s1
, size_t len
)
189 const uint8_t *s
= s1
;
197 void *memmove(void *d1
, const void *s1
, size_t len
)
200 const uint8_t *s
= s1
;
216 int memcmp(const void *s1
, const void *s2
, size_t len
)
218 const int8_t *p1
= s1
;
219 const int8_t *p2
= s2
;
222 int r
= *p1
++ - *p2
++;
230 size_t strlen(const char *s
)
233 for(s1
= s
; *s1
!= '\0'; s1
++);
237 /* from BSD ppp sources */
238 int vsnprintf(char *buf
, int buflen
, const char *fmt
, va_list args
)
241 int width
, prec
, fillch
;
243 unsigned long val
= 0;
247 static const char hexchars
[] = "0123456789abcdef";
252 for (f
= fmt
; *f
!= '%' && *f
!= 0; ++f
)
258 memcpy(buf
, fmt
, len
);
273 width
= va_arg(args
, int);
277 width
= width
* 10 + c
- '0';
284 prec
= va_arg(args
, int);
288 prec
= prec
* 10 + c
- '0';
307 i
= va_arg(args
, int);
316 val
= va_arg(args
, unsigned int);
321 val
= va_arg(args
, unsigned int);
325 val
= (unsigned long) va_arg(args
, void *);
330 str
= va_arg(args
, char *);
333 num
[0] = va_arg(args
, int);
340 --fmt
; /* so %z outputs %z etc. */
345 str
= num
+ sizeof(num
);
347 while (str
> num
+ neg
) {
348 *--str
= hexchars
[val
% base
];
350 if (--prec
<= 0 && val
== 0)
362 len
= num
+ sizeof(num
) - 1 - str
;
365 if (prec
> 0 && len
> prec
)
371 if ((n
= width
- len
) > 0) {
379 memcpy(buf
, str
, len
);
387 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
393 i
=vsnprintf(buf
,size
,fmt
,args
);
398 void bios_printf(int flags
, const char *fmt
, ...)
404 if ((flags
& BIOS_PRINTF_DEBHALT
) == BIOS_PRINTF_DEBHALT
)
405 outb(PANIC_PORT2
, 0x00);
408 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
418 for(i
= 0; i
< n
; i
++) {
420 /* approximative ! */
421 for(j
= 0; j
< 1000000; j
++);
426 r1
= inb(0x61) & 0x10;
428 r2
= inb(0x61) & 0x10;
440 uint32_t cpuid_signature
;
441 uint32_t cpuid_features
;
442 uint32_t cpuid_ext_features
;
443 unsigned long ram_size
;
445 uint8_t bios_uuid
[16];
446 #ifdef BX_USE_EBDA_TABLES
447 unsigned long ebda_cur_addr
;
450 uint32_t pm_io_base
, smb_io_base
;
452 unsigned long bios_table_cur_addr
;
453 unsigned long bios_table_end_addr
;
455 void init_smp_msrs(void)
457 *(uint32_t *)SMP_MSR_ADDR
= 0;
460 void wrmsr_smp(uint32_t index
, uint64_t val
)
462 static struct { uint32_t ecx
, eax
, edx
; } *p
= (void *)SMP_MSR_ADDR
;
473 #define QEMU_CFG_CTL_PORT 0x510
474 #define QEMU_CFG_DATA_PORT 0x511
475 #define QEMU_CFG_SIGNATURE 0x00
476 #define QEMU_CFG_ID 0x01
477 #define QEMU_CFG_UUID 0x02
481 void qemu_cfg_select(int f
)
483 outw(QEMU_CFG_CTL_PORT
, f
);
486 int qemu_cfg_port_probe()
491 qemu_cfg_select(QEMU_CFG_SIGNATURE
);
493 for (i
= 0; i
< 4; i
++)
494 if (inb(QEMU_CFG_DATA_PORT
) != sig
[i
])
500 void qemu_cfg_read(uint8_t *buf
, int len
)
503 *(buf
++) = inb(QEMU_CFG_DATA_PORT
);
507 void uuid_probe(void)
511 qemu_cfg_select(QEMU_CFG_UUID
);
512 qemu_cfg_read(bios_uuid
, 16);
516 memset(bios_uuid
, 0, 16);
521 uint32_t eax
, ebx
, ecx
, edx
;
522 cpuid(1, eax
, ebx
, ecx
, edx
);
523 cpuid_signature
= eax
;
524 cpuid_features
= edx
;
525 cpuid_ext_features
= ecx
;
528 static int cmos_readb(int addr
)
534 void setup_mtrr(void)
536 int i
, vcnt
, fix
, wc
;
543 mtrr_cap
= rdmsr(MSR_MTRRcap
);
544 vcnt
= mtrr_cap
& 0xff;
545 fix
= mtrr_cap
& 0x100;
546 wc
= mtrr_cap
& 0x400;
550 for (i
= 0; i
< 8; ++i
)
551 if (ram_size
>= 65536 * (i
+ 1))
553 wrmsr_smp(MSR_MTRRfix64K_00000
, u
.val
);
555 for (i
= 0; i
< 8; ++i
)
556 if (ram_size
>= 65536 * 8 + 16384 * (i
+ 1))
558 wrmsr_smp(MSR_MTRRfix16K_80000
, u
.val
);
559 wrmsr_smp(MSR_MTRRfix16K_A0000
, 0);
560 wrmsr_smp(MSR_MTRRfix4K_C0000
, 0);
561 wrmsr_smp(MSR_MTRRfix4K_C8000
, 0);
562 wrmsr_smp(MSR_MTRRfix4K_D0000
, 0);
563 wrmsr_smp(MSR_MTRRfix4K_D8000
, 0);
564 wrmsr_smp(MSR_MTRRfix4K_E0000
, 0);
565 wrmsr_smp(MSR_MTRRfix4K_E8000
, 0);
566 wrmsr_smp(MSR_MTRRfix4K_F0000
, 0);
567 wrmsr_smp(MSR_MTRRfix4K_F8000
, 0);
568 /* Mark 3.5-4GB as UC, anything not specified defaults to WB */
569 wrmsr_smp(MTRRphysBase_MSR(0), 0xe0000000ull
| 0);
570 wrmsr_smp(MTRRphysMask_MSR(0), ~(0x20000000ull
- 1) | 0x800);
571 wrmsr_smp(MSR_MTRRdefType
, 0xc06);
576 if (cmos_readb(0x34) | cmos_readb(0x35))
577 ram_size
= (cmos_readb(0x34) | (cmos_readb(0x35) << 8)) * 65536 +
580 ram_size
= (cmos_readb(0x17) | (cmos_readb(0x18) << 8)) * 1024;
582 if (cmos_readb(0x5b) | cmos_readb(0x5c) | cmos_readb(0x5d))
583 ram_end
= (((uint64_t)cmos_readb(0x5b) << 16) |
584 ((uint64_t)cmos_readb(0x5c) << 24) |
585 ((uint64_t)cmos_readb(0x5d) << 32)) + (1ull << 32);
589 BX_INFO("end of ram=%ldMB\n", ram_end
>> 20);
591 BX_INFO("ram_size=0x%08lx\n", ram_size
);
592 #ifdef BX_USE_EBDA_TABLES
593 ebda_cur_addr
= ((*(uint16_t *)(0x40e)) << 4) + 0x380;
594 BX_INFO("ebda_cur_addr: 0x%08lx\n", ebda_cur_addr
);
599 /****************************************************/
602 extern uint8_t smp_ap_boot_code_start
;
603 extern uint8_t smp_ap_boot_code_end
;
605 /* find the number of CPUs by launching a SIPI to them */
608 uint32_t val
, sipi_vector
;
610 writew(&smp_cpus
, 1);
611 if (cpuid_features
& CPUID_APIC
) {
613 /* enable local APIC */
614 val
= readl(APIC_BASE
+ APIC_SVR
);
616 writel(APIC_BASE
+ APIC_SVR
, val
);
618 /* copy AP boot code */
619 memcpy((void *)AP_BOOT_ADDR
, &smp_ap_boot_code_start
,
620 &smp_ap_boot_code_end
- &smp_ap_boot_code_start
);
623 writel(APIC_BASE
+ APIC_ICR_LOW
, 0x000C4500);
624 sipi_vector
= AP_BOOT_ADDR
>> 12;
625 writel(APIC_BASE
+ APIC_ICR_LOW
, 0x000C4600 | sipi_vector
);
630 while (cmos_readb(0x5f) + 1 != readw(&smp_cpus
))
634 BX_INFO("Found %d cpu(s)\n", readw(&smp_cpus
));
637 /****************************************************/
640 #define PCI_ADDRESS_SPACE_MEM 0x00
641 #define PCI_ADDRESS_SPACE_IO 0x01
642 #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0x08
644 #define PCI_ROM_SLOT 6
645 #define PCI_NUM_REGIONS 7
647 #define PCI_DEVICES_MAX 64
649 #define PCI_VENDOR_ID 0x00 /* 16 bits */
650 #define PCI_DEVICE_ID 0x02 /* 16 bits */
651 #define PCI_COMMAND 0x04 /* 16 bits */
652 #define PCI_COMMAND_IO 0x1 /* Enable response in I/O space */
653 #define PCI_COMMAND_MEMORY 0x2 /* Enable response in Memory space */
654 #define PCI_CLASS_DEVICE 0x0a /* Device class */
655 #define PCI_INTERRUPT_LINE 0x3c /* 8 bits */
656 #define PCI_INTERRUPT_PIN 0x3d /* 8 bits */
657 #define PCI_MIN_GNT 0x3e /* 8 bits */
658 #define PCI_MAX_LAT 0x3f /* 8 bits */
660 #define PCI_VENDOR_ID_INTEL 0x8086
661 #define PCI_DEVICE_ID_INTEL_82441 0x1237
662 #define PCI_DEVICE_ID_INTEL_82371SB_0 0x7000
663 #define PCI_DEVICE_ID_INTEL_82371SB_1 0x7010
664 #define PCI_DEVICE_ID_INTEL_82371AB_0 0x7110
665 #define PCI_DEVICE_ID_INTEL_82371AB 0x7111
666 #define PCI_DEVICE_ID_INTEL_82371AB_3 0x7113
668 #define PCI_VENDOR_ID_IBM 0x1014
669 #define PCI_VENDOR_ID_APPLE 0x106b
671 typedef struct PCIDevice
{
676 static uint32_t pci_bios_io_addr
;
677 static uint32_t pci_bios_mem_addr
;
678 static uint32_t pci_bios_bigmem_addr
;
679 /* host irqs corresponding to PCI irqs A-D */
680 static uint8_t pci_irqs
[4] = { 10, 10, 11, 11 };
681 static PCIDevice i440_pcidev
;
683 static void pci_config_writel(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
685 outl(0xcf8, 0x80000000 | (d
->bus
<< 16) | (d
->devfn
<< 8) | (addr
& 0xfc));
689 static void pci_config_writew(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
691 outl(0xcf8, 0x80000000 | (d
->bus
<< 16) | (d
->devfn
<< 8) | (addr
& 0xfc));
692 outw(0xcfc + (addr
& 2), val
);
695 static void pci_config_writeb(PCIDevice
*d
, uint32_t addr
, uint32_t val
)
697 outl(0xcf8, 0x80000000 | (d
->bus
<< 16) | (d
->devfn
<< 8) | (addr
& 0xfc));
698 outb(0xcfc + (addr
& 3), val
);
701 static uint32_t pci_config_readl(PCIDevice
*d
, uint32_t addr
)
703 outl(0xcf8, 0x80000000 | (d
->bus
<< 16) | (d
->devfn
<< 8) | (addr
& 0xfc));
707 static uint32_t pci_config_readw(PCIDevice
*d
, uint32_t addr
)
709 outl(0xcf8, 0x80000000 | (d
->bus
<< 16) | (d
->devfn
<< 8) | (addr
& 0xfc));
710 return inw(0xcfc + (addr
& 2));
713 static uint32_t pci_config_readb(PCIDevice
*d
, uint32_t addr
)
715 outl(0xcf8, 0x80000000 | (d
->bus
<< 16) | (d
->devfn
<< 8) | (addr
& 0xfc));
716 return inb(0xcfc + (addr
& 3));
719 static void pci_set_io_region_addr(PCIDevice
*d
, int region_num
, uint32_t addr
)
722 uint32_t ofs
, old_addr
;
724 if ( region_num
== PCI_ROM_SLOT
) {
727 ofs
= 0x10 + region_num
* 4;
730 old_addr
= pci_config_readl(d
, ofs
);
732 pci_config_writel(d
, ofs
, addr
);
733 BX_INFO("region %d: 0x%08x\n", region_num
, addr
);
735 /* enable memory mappings */
736 cmd
= pci_config_readw(d
, PCI_COMMAND
);
737 if ( region_num
== PCI_ROM_SLOT
)
739 else if (old_addr
& PCI_ADDRESS_SPACE_IO
)
743 pci_config_writew(d
, PCI_COMMAND
, cmd
);
746 /* return the global irq number corresponding to a given device irq
747 pin. We could also use the bus number to have a more precise
749 static int pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
752 slot_addend
= (pci_dev
->devfn
>> 3) - 1;
753 return (irq_num
+ slot_addend
) & 3;
756 static void find_bios_table_area(void)
759 for(addr
= 0xf0000; addr
< 0x100000; addr
+= 16) {
760 if (*(uint32_t *)addr
== 0xaafb4442) {
761 bios_table_cur_addr
= addr
+ 8;
762 bios_table_end_addr
= bios_table_cur_addr
+ *(uint32_t *)(addr
+ 4);
763 BX_INFO("bios_table_addr: 0x%08lx end=0x%08lx\n",
764 bios_table_cur_addr
, bios_table_end_addr
);
771 static void bios_shadow_init(PCIDevice
*d
)
775 if (bios_table_cur_addr
== 0)
778 /* remap the BIOS to shadow RAM an keep it read/write while we
779 are writing tables */
780 v
= pci_config_readb(d
, 0x59);
782 pci_config_writeb(d
, 0x59, v
);
783 memcpy((void *)BIOS_TMP_STORAGE
, (void *)0x000f0000, 0x10000);
785 pci_config_writeb(d
, 0x59, v
);
786 memcpy((void *)0x000f0000, (void *)BIOS_TMP_STORAGE
, 0x10000);
791 static void bios_lock_shadow_ram(void)
793 PCIDevice
*d
= &i440_pcidev
;
797 v
= pci_config_readb(d
, 0x59);
798 v
= (v
& 0x0f) | (0x10);
799 pci_config_writeb(d
, 0x59, v
);
802 static void pci_bios_init_bridges(PCIDevice
*d
)
804 uint16_t vendor_id
, device_id
;
806 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
807 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
809 if (vendor_id
== PCI_VENDOR_ID_INTEL
&&
810 (device_id
== PCI_DEVICE_ID_INTEL_82371SB_0
||
811 device_id
== PCI_DEVICE_ID_INTEL_82371AB_0
)) {
815 /* PIIX3/PIIX4 PCI to ISA bridge */
819 for(i
= 0; i
< 4; i
++) {
821 /* set to trigger level */
822 elcr
[irq
>> 3] |= (1 << (irq
& 7));
823 /* activate irq remapping in PIIX */
824 pci_config_writeb(d
, 0x60 + i
, irq
);
826 outb(0x4d0, elcr
[0]);
827 outb(0x4d1, elcr
[1]);
828 BX_INFO("PIIX3/PIIX4 init: elcr=%02x %02x\n",
830 } else if (vendor_id
== PCI_VENDOR_ID_INTEL
&& device_id
== PCI_DEVICE_ID_INTEL_82441
) {
831 /* i440 PCI bridge */
836 extern uint8_t smm_relocation_start
, smm_relocation_end
;
837 extern uint8_t smm_code_start
, smm_code_end
;
840 static void smm_init(PCIDevice
*d
)
844 /* check if SMM init is already done */
845 value
= pci_config_readl(d
, 0x58);
846 if ((value
& (1 << 25)) == 0) {
848 /* enable the SMM memory window */
849 pci_config_writeb(&i440_pcidev
, 0x72, 0x02 | 0x48);
851 /* save original memory content */
852 memcpy((void *)0xa8000, (void *)0x38000, 0x8000);
854 /* copy the SMM relocation code */
855 memcpy((void *)0x38000, &smm_relocation_start
,
856 &smm_relocation_end
- &smm_relocation_start
);
858 /* enable SMI generation when writing to the APMC register */
859 pci_config_writel(d
, 0x58, value
| (1 << 25));
861 /* init APM status port */
864 /* raise an SMI interrupt */
867 /* wait until SMM code executed */
868 while (inb(0xb3) != 0x00);
870 /* restore original memory content */
871 memcpy((void *)0x38000, (void *)0xa8000, 0x8000);
873 /* copy the SMM code */
874 memcpy((void *)0xa8000, &smm_code_start
,
875 &smm_code_end
- &smm_code_start
);
878 /* close the SMM memory window and enable normal SMM */
879 pci_config_writeb(&i440_pcidev
, 0x72, 0x02 | 0x08);
884 static void piix4_pm_enable(PCIDevice
*d
)
886 /* PIIX4 Power Management device (for ACPI) */
887 pci_config_writel(d
, 0x40, PM_IO_BASE
| 1);
888 pci_config_writeb(d
, 0x80, 0x01); /* enable PM io space */
889 pci_config_writel(d
, 0x90, SMB_IO_BASE
| 1);
890 pci_config_writeb(d
, 0xd2, 0x09); /* enable SMBus io space */
896 static void pci_bios_init_device(PCIDevice
*d
)
900 int i
, pin
, pic_irq
, vendor_id
, device_id
;
902 class = pci_config_readw(d
, PCI_CLASS_DEVICE
);
903 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
904 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
905 BX_INFO("PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x class=0x%04x\n",
906 d
->bus
, d
->devfn
, vendor_id
, device_id
, class);
908 case 0x0101: /* Mass storage controller - IDE interface */
909 if (vendor_id
== PCI_VENDOR_ID_INTEL
&&
910 (device_id
== PCI_DEVICE_ID_INTEL_82371SB_1
||
911 device_id
== PCI_DEVICE_ID_INTEL_82371AB
)) {
912 /* PIIX3/PIIX4 IDE */
913 pci_config_writew(d
, 0x40, 0x8000); // enable IDE0
914 pci_config_writew(d
, 0x42, 0x8000); // enable IDE1
917 /* IDE: we map it as in ISA mode */
918 pci_set_io_region_addr(d
, 0, 0x1f0);
919 pci_set_io_region_addr(d
, 1, 0x3f4);
920 pci_set_io_region_addr(d
, 2, 0x170);
921 pci_set_io_region_addr(d
, 3, 0x374);
924 case 0x0300: /* Display controller - VGA compatible controller */
925 if (vendor_id
!= 0x1234)
927 /* VGA: map frame buffer to default Bochs VBE address */
928 pci_set_io_region_addr(d
, 0, 0xE0000000);
930 case 0x0800: /* Generic system peripheral - PIC */
931 if (vendor_id
== PCI_VENDOR_ID_IBM
) {
933 if (device_id
== 0x0046 || device_id
== 0xFFFF) {
935 pci_set_io_region_addr(d
, 0, 0x80800000 + 0x00040000);
940 if (vendor_id
== PCI_VENDOR_ID_APPLE
&&
941 (device_id
== 0x0017 || device_id
== 0x0022)) {
943 pci_set_io_region_addr(d
, 0, 0x80800000);
948 /* default memory mappings */
949 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
953 if (i
== PCI_ROM_SLOT
)
957 pci_config_writel(d
, ofs
, 0xffffffff);
958 val
= pci_config_readl(d
, ofs
);
960 size
= (~(val
& ~0xf)) + 1;
961 if (val
& PCI_ADDRESS_SPACE_IO
)
962 paddr
= &pci_bios_io_addr
;
963 else if (size
>= 0x04000000)
964 paddr
= &pci_bios_bigmem_addr
;
966 paddr
= &pci_bios_mem_addr
;
967 *paddr
= (*paddr
+ size
- 1) & ~(size
- 1);
968 pci_set_io_region_addr(d
, i
, *paddr
);
970 /* make memory address page aligned */
971 if (!(val
& PCI_ADDRESS_SPACE_IO
))
972 *paddr
= (*paddr
+ 0xfff) & 0xfffff000;
978 /* map the interrupt */
979 pin
= pci_config_readb(d
, PCI_INTERRUPT_PIN
);
981 pin
= pci_slot_get_pirq(d
, pin
- 1);
982 pic_irq
= pci_irqs
[pin
];
983 pci_config_writeb(d
, PCI_INTERRUPT_LINE
, pic_irq
);
986 if (vendor_id
== PCI_VENDOR_ID_INTEL
&& device_id
== PCI_DEVICE_ID_INTEL_82371AB_3
) {
987 /* PIIX4 Power Management device (for ACPI) */
989 // acpi sci is hardwired to 9
990 pci_config_writeb(d
, PCI_INTERRUPT_LINE
, 9);
992 pm_io_base
= PM_IO_BASE
;
993 smb_io_base
= SMB_IO_BASE
;
994 pm_sci_int
= pci_config_readb(d
, PCI_INTERRUPT_LINE
);
1000 void pci_for_each_device(void (*init_func
)(PCIDevice
*d
))
1002 PCIDevice d1
, *d
= &d1
;
1004 uint16_t vendor_id
, device_id
;
1006 for(bus
= 0; bus
< 1; bus
++) {
1007 for(devfn
= 0; devfn
< 256; devfn
++) {
1010 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
1011 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
1012 if (vendor_id
!= 0xffff || device_id
!= 0xffff) {
1019 void pci_bios_init(void)
1021 pci_bios_io_addr
= 0xc000;
1022 pci_bios_mem_addr
= 0xf0000000;
1023 pci_bios_bigmem_addr
= ram_size
;
1024 if (pci_bios_bigmem_addr
< 0x90000000)
1025 pci_bios_bigmem_addr
= 0x90000000;
1027 pci_for_each_device(pci_bios_init_bridges
);
1029 pci_for_each_device(pci_bios_init_device
);
1032 /****************************************************/
1033 /* Multi Processor table init */
1035 static void putb(uint8_t **pp
, int val
)
1043 static void putstr(uint8_t **pp
, const char *str
)
1052 static void putle16(uint8_t **pp
, int val
)
1061 static void putle32(uint8_t **pp
, int val
)
1072 static int mpf_checksum(const uint8_t *data
, int len
)
1076 for(i
= 0; i
< len
; i
++)
1081 static unsigned long align(unsigned long addr
, unsigned long v
)
1083 return (addr
+ v
- 1) & ~(v
- 1);
1086 static void mptable_init(void)
1088 uint8_t *mp_config_table
, *q
, *float_pointer_struct
;
1089 int ioapic_id
, i
, len
;
1090 int mp_config_table_size
;
1092 #ifdef BX_USE_EBDA_TABLES
1093 mp_config_table
= (uint8_t *)(ram_size
- ACPI_DATA_SIZE
- MPTABLE_MAX_SIZE
);
1095 bios_table_cur_addr
= align(bios_table_cur_addr
, 16);
1096 mp_config_table
= (uint8_t *)bios_table_cur_addr
;
1098 q
= mp_config_table
;
1099 putstr(&q
, "PCMP"); /* "PCMP signature */
1100 putle16(&q
, 0); /* table length (patched later) */
1101 putb(&q
, 4); /* spec rev */
1102 putb(&q
, 0); /* checksum (patched later) */
1104 putstr(&q
, "QEMUCPU "); /* OEM id */
1106 putstr(&q
, "BOCHSCPU");
1108 putstr(&q
, "0.1 "); /* vendor id */
1109 putle32(&q
, 0); /* OEM table ptr */
1110 putle16(&q
, 0); /* OEM table size */
1111 putle16(&q
, MAX_CPUS
+ 18); /* entry count */
1112 putle32(&q
, 0xfee00000); /* local APIC addr */
1113 putle16(&q
, 0); /* ext table length */
1114 putb(&q
, 0); /* ext table checksum */
1115 putb(&q
, 0); /* reserved */
1117 for(i
= 0; i
< MAX_CPUS
; i
++) {
1118 putb(&q
, 0); /* entry type = processor */
1119 putb(&q
, i
); /* APIC id */
1120 putb(&q
, 0x11); /* local APIC version number */
1122 putb(&q
, 3); /* cpu flags: enabled, bootstrap cpu */
1123 else if ( i
< smp_cpus
)
1124 putb(&q
, 1); /* cpu flags: enabled */
1126 putb(&q
, 0); /* cpu flags: disabled */
1127 putb(&q
, 0); /* cpu signature */
1131 putle16(&q
, 0x201); /* feature flags */
1134 putle16(&q
, 0); /* reserved */
1141 putb(&q
, 1); /* entry type = bus */
1142 putb(&q
, 0); /* bus ID */
1146 ioapic_id
= smp_cpus
;
1147 putb(&q
, 2); /* entry type = I/O APIC */
1148 putb(&q
, ioapic_id
); /* apic ID */
1149 putb(&q
, 0x11); /* I/O APIC version number */
1150 putb(&q
, 1); /* enable */
1151 putle32(&q
, 0xfec00000); /* I/O APIC addr */
1154 for(i
= 0; i
< 16; i
++) {
1155 putb(&q
, 3); /* entry type = I/O interrupt */
1156 putb(&q
, 0); /* interrupt type = vectored interrupt */
1157 putb(&q
, 0); /* flags: po=0, el=0 */
1159 putb(&q
, 0); /* source bus ID = ISA */
1160 putb(&q
, i
); /* source bus IRQ */
1161 putb(&q
, ioapic_id
); /* dest I/O APIC ID */
1162 putb(&q
, i
); /* dest I/O APIC interrupt in */
1165 len
= q
- mp_config_table
;
1166 mp_config_table
[4] = len
;
1167 mp_config_table
[5] = len
>> 8;
1169 mp_config_table
[7] = -mpf_checksum(mp_config_table
, q
- mp_config_table
);
1171 mp_config_table_size
= q
- mp_config_table
;
1173 #ifndef BX_USE_EBDA_TABLES
1174 bios_table_cur_addr
+= mp_config_table_size
;
1177 /* floating pointer structure */
1178 #ifdef BX_USE_EBDA_TABLES
1179 ebda_cur_addr
= align(ebda_cur_addr
, 16);
1180 float_pointer_struct
= (uint8_t *)ebda_cur_addr
;
1182 bios_table_cur_addr
= align(bios_table_cur_addr
, 16);
1183 float_pointer_struct
= (uint8_t *)bios_table_cur_addr
;
1185 q
= float_pointer_struct
;
1187 /* pointer to MP config table */
1188 putle32(&q
, (unsigned long)mp_config_table
);
1190 putb(&q
, 1); /* length in 16 byte units */
1191 putb(&q
, 4); /* MP spec revision */
1192 putb(&q
, 0); /* checksum (patched later) */
1193 putb(&q
, 0); /* MP feature byte 1 */
1199 float_pointer_struct
[10] =
1200 -mpf_checksum(float_pointer_struct
, q
- float_pointer_struct
);
1201 #ifdef BX_USE_EBDA_TABLES
1202 ebda_cur_addr
+= (q
- float_pointer_struct
);
1204 bios_table_cur_addr
+= (q
- float_pointer_struct
);
1206 BX_INFO("MP table addr=0x%08lx MPC table addr=0x%08lx size=0x%x\n",
1207 (unsigned long)float_pointer_struct
,
1208 (unsigned long)mp_config_table
,
1209 mp_config_table_size
);
1212 /****************************************************/
1213 /* ACPI tables init */
1215 /* Table structure from Linux kernel (the ACPI tables are under the
1219 * All tables must be byte-packed to match the ACPI specification, since
1220 * the tables are provided by the system BIOS.
1223 #define ACPI_TABLE_HEADER_DEF /* ACPI common table header */ \
1224 uint8_t signature [4]; /* ACPI signature (4 ASCII characters) */\
1225 uint32_t length; /* Length of table, in bytes, including header */\
1226 uint8_t revision; /* ACPI Specification minor version # */\
1227 uint8_t checksum; /* To make sum of entire table == 0 */\
1228 uint8_t oem_id [6]; /* OEM identification */\
1229 uint8_t oem_table_id [8]; /* OEM table identification */\
1230 uint32_t oem_revision; /* OEM revision number */\
1231 uint8_t asl_compiler_id [4]; /* ASL compiler vendor ID */\
1232 uint32_t asl_compiler_revision; /* ASL compiler revision number */
1235 struct acpi_table_header
/* ACPI common table header */
1237 ACPI_TABLE_HEADER_DEF
1238 } __attribute__((__packed__
));
1240 struct rsdp_descriptor
/* Root System Descriptor Pointer */
1242 uint8_t signature
[8]; /* ACPI signature, contains "RSD PTR " */
1243 uint8_t checksum
; /* To make sum of struct == 0 */
1244 uint8_t oem_id
[6]; /* OEM identification */
1245 uint8_t revision
; /* Must be 0 for 1.0, 2 for 2.0 */
1246 uint32_t rsdt_physical_address
; /* 32-bit physical address of RSDT */
1247 uint32_t length
; /* XSDT Length in bytes including hdr */
1248 uint64_t xsdt_physical_address
; /* 64-bit physical address of XSDT */
1249 uint8_t extended_checksum
; /* Checksum of entire table */
1250 uint8_t reserved
[3]; /* Reserved field must be 0 */
1251 } __attribute__((__packed__
));
1254 * ACPI 1.0 Root System Description Table (RSDT)
1256 struct rsdt_descriptor_rev1
1258 ACPI_TABLE_HEADER_DEF
/* ACPI common table header */
1259 uint32_t table_offset_entry
[2]; /* Array of pointers to other */
1261 } __attribute__((__packed__
));
1264 * ACPI 1.0 Firmware ACPI Control Structure (FACS)
1266 struct facs_descriptor_rev1
1268 uint8_t signature
[4]; /* ACPI Signature */
1269 uint32_t length
; /* Length of structure, in bytes */
1270 uint32_t hardware_signature
; /* Hardware configuration signature */
1271 uint32_t firmware_waking_vector
; /* ACPI OS waking vector */
1272 uint32_t global_lock
; /* Global Lock */
1273 uint32_t S4bios_f
: 1; /* Indicates if S4BIOS support is present */
1274 uint32_t reserved1
: 31; /* Must be 0 */
1275 uint8_t resverved3
[40]; /* Reserved - must be zero */
1276 } __attribute__((__packed__
));
1280 * ACPI 1.0 Fixed ACPI Description Table (FADT)
1282 struct fadt_descriptor_rev1
1284 ACPI_TABLE_HEADER_DEF
/* ACPI common table header */
1285 uint32_t firmware_ctrl
; /* Physical address of FACS */
1286 uint32_t dsdt
; /* Physical address of DSDT */
1287 uint8_t model
; /* System Interrupt Model */
1288 uint8_t reserved1
; /* Reserved */
1289 uint16_t sci_int
; /* System vector of SCI interrupt */
1290 uint32_t smi_cmd
; /* Port address of SMI command port */
1291 uint8_t acpi_enable
; /* Value to write to smi_cmd to enable ACPI */
1292 uint8_t acpi_disable
; /* Value to write to smi_cmd to disable ACPI */
1293 uint8_t S4bios_req
; /* Value to write to SMI CMD to enter S4BIOS state */
1294 uint8_t reserved2
; /* Reserved - must be zero */
1295 uint32_t pm1a_evt_blk
; /* Port address of Power Mgt 1a acpi_event Reg Blk */
1296 uint32_t pm1b_evt_blk
; /* Port address of Power Mgt 1b acpi_event Reg Blk */
1297 uint32_t pm1a_cnt_blk
; /* Port address of Power Mgt 1a Control Reg Blk */
1298 uint32_t pm1b_cnt_blk
; /* Port address of Power Mgt 1b Control Reg Blk */
1299 uint32_t pm2_cnt_blk
; /* Port address of Power Mgt 2 Control Reg Blk */
1300 uint32_t pm_tmr_blk
; /* Port address of Power Mgt Timer Ctrl Reg Blk */
1301 uint32_t gpe0_blk
; /* Port addr of General Purpose acpi_event 0 Reg Blk */
1302 uint32_t gpe1_blk
; /* Port addr of General Purpose acpi_event 1 Reg Blk */
1303 uint8_t pm1_evt_len
; /* Byte length of ports at pm1_x_evt_blk */
1304 uint8_t pm1_cnt_len
; /* Byte length of ports at pm1_x_cnt_blk */
1305 uint8_t pm2_cnt_len
; /* Byte Length of ports at pm2_cnt_blk */
1306 uint8_t pm_tmr_len
; /* Byte Length of ports at pm_tm_blk */
1307 uint8_t gpe0_blk_len
; /* Byte Length of ports at gpe0_blk */
1308 uint8_t gpe1_blk_len
; /* Byte Length of ports at gpe1_blk */
1309 uint8_t gpe1_base
; /* Offset in gpe model where gpe1 events start */
1310 uint8_t reserved3
; /* Reserved */
1311 uint16_t plvl2_lat
; /* Worst case HW latency to enter/exit C2 state */
1312 uint16_t plvl3_lat
; /* Worst case HW latency to enter/exit C3 state */
1313 uint16_t flush_size
; /* Size of area read to flush caches */
1314 uint16_t flush_stride
; /* Stride used in flushing caches */
1315 uint8_t duty_offset
; /* Bit location of duty cycle field in p_cnt reg */
1316 uint8_t duty_width
; /* Bit width of duty cycle field in p_cnt reg */
1317 uint8_t day_alrm
; /* Index to day-of-month alarm in RTC CMOS RAM */
1318 uint8_t mon_alrm
; /* Index to month-of-year alarm in RTC CMOS RAM */
1319 uint8_t century
; /* Index to century in RTC CMOS RAM */
1320 uint8_t reserved4
; /* Reserved */
1321 uint8_t reserved4a
; /* Reserved */
1322 uint8_t reserved4b
; /* Reserved */
1324 uint32_t wb_invd
: 1; /* The wbinvd instruction works properly */
1325 uint32_t wb_invd_flush
: 1; /* The wbinvd flushes but does not invalidate */
1326 uint32_t proc_c1
: 1; /* All processors support C1 state */
1327 uint32_t plvl2_up
: 1; /* C2 state works on MP system */
1328 uint32_t pwr_button
: 1; /* Power button is handled as a generic feature */
1329 uint32_t sleep_button
: 1; /* Sleep button is handled as a generic feature, or not present */
1330 uint32_t fixed_rTC
: 1; /* RTC wakeup stat not in fixed register space */
1331 uint32_t rtcs4
: 1; /* RTC wakeup stat not possible from S4 */
1332 uint32_t tmr_val_ext
: 1; /* The tmr_val width is 32 bits (0 = 24 bits) */
1333 uint32_t reserved5
: 23; /* Reserved - must be zero */
1337 } __attribute__((__packed__
));
1340 * MADT values and structures
1343 /* Values for MADT PCATCompat */
1346 #define MULTIPLE_APIC 1
1351 struct multiple_apic_table
1353 ACPI_TABLE_HEADER_DEF
/* ACPI common table header */
1354 uint32_t local_apic_address
; /* Physical address of local APIC */
1356 uint32_t PCATcompat
: 1; /* A one indicates system also has dual 8259s */
1357 uint32_t reserved1
: 31;
1361 } __attribute__((__packed__
));
1364 /* Values for Type in APIC_HEADER_DEF */
1366 #define APIC_PROCESSOR 0
1368 #define APIC_XRUPT_OVERRIDE 2
1370 #define APIC_LOCAL_NMI 4
1371 #define APIC_ADDRESS_OVERRIDE 5
1372 #define APIC_IO_SAPIC 6
1373 #define APIC_LOCAL_SAPIC 7
1374 #define APIC_XRUPT_SOURCE 8
1375 #define APIC_RESERVED 9 /* 9 and greater are reserved */
1378 * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
1380 #define APIC_HEADER_DEF /* Common APIC sub-structure header */\
1384 /* Sub-structures for MADT */
1386 struct madt_processor_apic
1389 uint8_t processor_id
; /* ACPI processor id */
1390 uint8_t local_apic_id
; /* Processor's local APIC id */
1392 uint32_t processor_enabled
: 1; /* Processor is usable if set */
1393 uint32_t reserved2
: 31; /* Reserved, must be zero */
1397 } __attribute__((__packed__
));
1402 uint8_t io_apic_id
; /* I/O APIC ID */
1403 uint8_t reserved
; /* Reserved - must be zero */
1404 uint32_t address
; /* APIC physical address */
1405 uint32_t interrupt
; /* Global system interrupt where INTI
1407 } __attribute__((__packed__
));
1409 struct madt_intsrcovr
{
1415 } __attribute__((packed
));
1417 #include "acpi-dsdt.hex"
1419 static inline uint16_t cpu_to_le16(uint16_t x
)
1424 static inline uint32_t cpu_to_le32(uint32_t x
)
1429 static int acpi_checksum(const uint8_t *data
, int len
)
1433 for(i
= 0; i
< len
; i
++)
1435 return (-sum
) & 0xff;
1438 static void acpi_build_table_header(struct acpi_table_header
*h
,
1439 char *sig
, int len
, uint8_t rev
)
1441 memcpy(h
->signature
, sig
, 4);
1442 h
->length
= cpu_to_le32(len
);
1445 memcpy(h
->oem_id
, "QEMU ", 6);
1446 memcpy(h
->oem_table_id
, "QEMU", 4);
1448 memcpy(h
->oem_id
, "BOCHS ", 6);
1449 memcpy(h
->oem_table_id
, "BXPC", 4);
1451 memcpy(h
->oem_table_id
+ 4, sig
, 4);
1452 h
->oem_revision
= cpu_to_le32(1);
1454 memcpy(h
->asl_compiler_id
, "QEMU", 4);
1456 memcpy(h
->asl_compiler_id
, "BXPC", 4);
1458 h
->asl_compiler_revision
= cpu_to_le32(1);
1459 h
->checksum
= acpi_checksum((void *)h
, len
);
1462 /* base_addr must be a multiple of 4KB */
1463 void acpi_bios_init(void)
1465 struct rsdp_descriptor
*rsdp
;
1466 struct rsdt_descriptor_rev1
*rsdt
;
1467 struct fadt_descriptor_rev1
*fadt
;
1468 struct facs_descriptor_rev1
*facs
;
1469 struct multiple_apic_table
*madt
;
1471 uint32_t base_addr
, rsdt_addr
, fadt_addr
, addr
, facs_addr
, dsdt_addr
;
1472 uint32_t acpi_tables_size
, madt_addr
, madt_size
;
1475 /* reserve memory space for tables */
1476 #ifdef BX_USE_EBDA_TABLES
1477 ebda_cur_addr
= align(ebda_cur_addr
, 16);
1478 rsdp
= (void *)(ebda_cur_addr
);
1479 ebda_cur_addr
+= sizeof(*rsdp
);
1481 bios_table_cur_addr
= align(bios_table_cur_addr
, 16);
1482 rsdp
= (void *)(bios_table_cur_addr
);
1483 bios_table_cur_addr
+= sizeof(*rsdp
);
1486 addr
= base_addr
= ram_size
- ACPI_DATA_SIZE
;
1488 rsdt
= (void *)(addr
);
1489 addr
+= sizeof(*rsdt
);
1492 fadt
= (void *)(addr
);
1493 addr
+= sizeof(*fadt
);
1495 /* XXX: FACS should be in RAM */
1496 addr
= (addr
+ 63) & ~63; /* 64 byte alignment for FACS */
1498 facs
= (void *)(addr
);
1499 addr
+= sizeof(*facs
);
1502 dsdt
= (void *)(addr
);
1503 addr
+= sizeof(AmlCode
);
1505 addr
= (addr
+ 7) & ~7;
1507 madt_size
= sizeof(*madt
) +
1508 sizeof(struct madt_processor_apic
) * MAX_CPUS
+
1509 sizeof(struct madt_io_apic
);
1510 madt
= (void *)(addr
);
1513 acpi_tables_size
= addr
- base_addr
;
1515 BX_INFO("ACPI tables: RSDP addr=0x%08lx ACPI DATA addr=0x%08lx size=0x%x\n",
1516 (unsigned long)rsdp
,
1517 (unsigned long)rsdt
, acpi_tables_size
);
1520 memset(rsdp
, 0, sizeof(*rsdp
));
1521 memcpy(rsdp
->signature
, "RSD PTR ", 8);
1523 memcpy(rsdp
->oem_id
, "QEMU ", 6);
1525 memcpy(rsdp
->oem_id
, "BOCHS ", 6);
1527 rsdp
->rsdt_physical_address
= cpu_to_le32(rsdt_addr
);
1528 rsdp
->checksum
= acpi_checksum((void *)rsdp
, 20);
1531 memset(rsdt
, 0, sizeof(*rsdt
));
1532 rsdt
->table_offset_entry
[0] = cpu_to_le32(fadt_addr
);
1533 rsdt
->table_offset_entry
[1] = cpu_to_le32(madt_addr
);
1534 acpi_build_table_header((struct acpi_table_header
*)rsdt
,
1535 "RSDT", sizeof(*rsdt
), 1);
1538 memset(fadt
, 0, sizeof(*fadt
));
1539 fadt
->firmware_ctrl
= cpu_to_le32(facs_addr
);
1540 fadt
->dsdt
= cpu_to_le32(dsdt_addr
);
1542 fadt
->reserved1
= 0;
1543 fadt
->sci_int
= cpu_to_le16(pm_sci_int
);
1544 fadt
->smi_cmd
= cpu_to_le32(SMI_CMD_IO_ADDR
);
1545 fadt
->acpi_enable
= 0xf1;
1546 fadt
->acpi_disable
= 0xf0;
1547 fadt
->pm1a_evt_blk
= cpu_to_le32(pm_io_base
);
1548 fadt
->pm1a_cnt_blk
= cpu_to_le32(pm_io_base
+ 0x04);
1549 fadt
->pm_tmr_blk
= cpu_to_le32(pm_io_base
+ 0x08);
1550 fadt
->pm1_evt_len
= 4;
1551 fadt
->pm1_cnt_len
= 2;
1552 fadt
->pm_tmr_len
= 4;
1553 fadt
->plvl2_lat
= cpu_to_le16(0xfff); // C2 state not supported
1554 fadt
->plvl3_lat
= cpu_to_le16(0xfff); // C3 state not supported
1555 fadt
->gpe0_blk
= cpu_to_le32(0xafe0);
1556 fadt
->gpe0_blk_len
= 4;
1557 /* WBINVD + PROC_C1 + SLP_BUTTON + FIX_RTC */
1558 fadt
->flags
= cpu_to_le32((1 << 0) | (1 << 2) | (1 << 5) | (1 << 6));
1559 acpi_build_table_header((struct acpi_table_header
*)fadt
, "FACP",
1563 memset(facs
, 0, sizeof(*facs
));
1564 memcpy(facs
->signature
, "FACS", 4);
1565 facs
->length
= cpu_to_le32(sizeof(*facs
));
1566 BX_INFO("Firmware waking vector %p\n", &facs
->firmware_waking_vector
);
1569 memcpy(dsdt
, AmlCode
, sizeof(AmlCode
));
1573 struct madt_processor_apic
*apic
;
1574 struct madt_io_apic
*io_apic
;
1575 struct madt_intsrcovr
*intsrcovr
;
1577 memset(madt
, 0, madt_size
);
1578 madt
->local_apic_address
= cpu_to_le32(0xfee00000);
1579 madt
->flags
= cpu_to_le32(1);
1580 apic
= (void *)(madt
+ 1);
1581 for(i
=0;i
<MAX_CPUS
;i
++) {
1582 apic
->type
= APIC_PROCESSOR
;
1583 apic
->length
= sizeof(*apic
);
1584 apic
->processor_id
= i
;
1585 apic
->local_apic_id
= i
;
1587 apic
->flags
= cpu_to_le32(1);
1592 io_apic
= (void *)apic
;
1593 io_apic
->type
= APIC_IO
;
1594 io_apic
->length
= sizeof(*io_apic
);
1595 io_apic
->io_apic_id
= smp_cpus
;
1596 io_apic
->address
= cpu_to_le32(0xfec00000);
1597 io_apic
->interrupt
= cpu_to_le32(0);
1599 intsrcovr
= (struct madt_intsrcovr
*)(io_apic
+ 1);
1600 for ( i
= 0; i
< 16; i
++ ) {
1601 if ( PCI_ISA_IRQ_MASK
& (1U << i
) ) {
1602 memset(intsrcovr
, 0, sizeof(*intsrcovr
));
1603 intsrcovr
->type
= APIC_XRUPT_OVERRIDE
;
1604 intsrcovr
->length
= sizeof(*intsrcovr
);
1605 intsrcovr
->source
= i
;
1607 intsrcovr
->flags
= 0xd; /* active high, level triggered */
1609 /* No need for a INT source override structure. */
1613 madt_size
+= sizeof(struct madt_intsrcovr
);
1615 acpi_build_table_header((struct acpi_table_header
*)madt
,
1616 "APIC", madt_size
, 1);
1620 /* SMBIOS entry point -- must be written to a 16-bit aligned address
1621 between 0xf0000 and 0xfffff.
1623 struct smbios_entry_point
{
1624 char anchor_string
[4];
1627 uint8_t smbios_major_version
;
1628 uint8_t smbios_minor_version
;
1629 uint16_t max_structure_size
;
1630 uint8_t entry_point_revision
;
1631 uint8_t formatted_area
[5];
1632 char intermediate_anchor_string
[5];
1633 uint8_t intermediate_checksum
;
1634 uint16_t structure_table_length
;
1635 uint32_t structure_table_address
;
1636 uint16_t number_of_structures
;
1637 uint8_t smbios_bcd_revision
;
1638 } __attribute__((__packed__
));
1640 /* This goes at the beginning of every SMBIOS structure. */
1641 struct smbios_structure_header
{
1645 } __attribute__((__packed__
));
1647 /* SMBIOS type 0 - BIOS Information */
1648 struct smbios_type_0
{
1649 struct smbios_structure_header header
;
1651 uint8_t bios_version_str
;
1652 uint16_t bios_starting_address_segment
;
1653 uint8_t bios_release_date_str
;
1654 uint8_t bios_rom_size
;
1655 uint8_t bios_characteristics
[8];
1656 uint8_t bios_characteristics_extension_bytes
[2];
1657 uint8_t system_bios_major_release
;
1658 uint8_t system_bios_minor_release
;
1659 uint8_t embedded_controller_major_release
;
1660 uint8_t embedded_controller_minor_release
;
1661 } __attribute__((__packed__
));
1663 /* SMBIOS type 1 - System Information */
1664 struct smbios_type_1
{
1665 struct smbios_structure_header header
;
1666 uint8_t manufacturer_str
;
1667 uint8_t product_name_str
;
1668 uint8_t version_str
;
1669 uint8_t serial_number_str
;
1671 uint8_t wake_up_type
;
1672 uint8_t sku_number_str
;
1674 } __attribute__((__packed__
));
1676 /* SMBIOS type 3 - System Enclosure (v2.3) */
1677 struct smbios_type_3
{
1678 struct smbios_structure_header header
;
1679 uint8_t manufacturer_str
;
1681 uint8_t version_str
;
1682 uint8_t serial_number_str
;
1683 uint8_t asset_tag_number_str
;
1684 uint8_t boot_up_state
;
1685 uint8_t power_supply_state
;
1686 uint8_t thermal_state
;
1687 uint8_t security_status
;
1688 uint32_t oem_defined
;
1690 uint8_t number_of_power_cords
;
1691 uint8_t contained_element_count
;
1692 // contained elements follow
1693 } __attribute__((__packed__
));
1695 /* SMBIOS type 4 - Processor Information (v2.0) */
1696 struct smbios_type_4
{
1697 struct smbios_structure_header header
;
1698 uint8_t socket_designation_str
;
1699 uint8_t processor_type
;
1700 uint8_t processor_family
;
1701 uint8_t processor_manufacturer_str
;
1702 uint32_t processor_id
[2];
1703 uint8_t processor_version_str
;
1705 uint16_t external_clock
;
1707 uint16_t current_speed
;
1709 uint8_t processor_upgrade
;
1710 uint16_t l1_cache_handle
;
1711 uint16_t l2_cache_handle
;
1712 uint16_t l3_cache_handle
;
1713 } __attribute__((__packed__
));
1715 /* SMBIOS type 16 - Physical Memory Array
1716 * Associated with one type 17 (Memory Device).
1718 struct smbios_type_16
{
1719 struct smbios_structure_header header
;
1722 uint8_t error_correction
;
1723 uint32_t maximum_capacity
;
1724 uint16_t memory_error_information_handle
;
1725 uint16_t number_of_memory_devices
;
1726 } __attribute__((__packed__
));
1728 /* SMBIOS type 17 - Memory Device
1729 * Associated with one type 19
1731 struct smbios_type_17
{
1732 struct smbios_structure_header header
;
1733 uint16_t physical_memory_array_handle
;
1734 uint16_t memory_error_information_handle
;
1735 uint16_t total_width
;
1736 uint16_t data_width
;
1738 uint8_t form_factor
;
1740 uint8_t device_locator_str
;
1741 uint8_t bank_locator_str
;
1742 uint8_t memory_type
;
1743 uint16_t type_detail
;
1744 } __attribute__((__packed__
));
1746 /* SMBIOS type 19 - Memory Array Mapped Address */
1747 struct smbios_type_19
{
1748 struct smbios_structure_header header
;
1749 uint32_t starting_address
;
1750 uint32_t ending_address
;
1751 uint16_t memory_array_handle
;
1752 uint8_t partition_width
;
1753 } __attribute__((__packed__
));
1755 /* SMBIOS type 20 - Memory Device Mapped Address */
1756 struct smbios_type_20
{
1757 struct smbios_structure_header header
;
1758 uint32_t starting_address
;
1759 uint32_t ending_address
;
1760 uint16_t memory_device_handle
;
1761 uint16_t memory_array_mapped_address_handle
;
1762 uint8_t partition_row_position
;
1763 uint8_t interleave_position
;
1764 uint8_t interleaved_data_depth
;
1765 } __attribute__((__packed__
));
1767 /* SMBIOS type 32 - System Boot Information */
1768 struct smbios_type_32
{
1769 struct smbios_structure_header header
;
1770 uint8_t reserved
[6];
1771 uint8_t boot_status
;
1772 } __attribute__((__packed__
));
1774 /* SMBIOS type 127 -- End-of-table */
1775 struct smbios_type_127
{
1776 struct smbios_structure_header header
;
1777 } __attribute__((__packed__
));
1780 smbios_entry_point_init(void *start
,
1781 uint16_t max_structure_size
,
1782 uint16_t structure_table_length
,
1783 uint32_t structure_table_address
,
1784 uint16_t number_of_structures
)
1788 struct smbios_entry_point
*ep
= (struct smbios_entry_point
*)start
;
1790 memcpy(ep
->anchor_string
, "_SM_", 4);
1792 ep
->smbios_major_version
= 2;
1793 ep
->smbios_minor_version
= 4;
1794 ep
->max_structure_size
= max_structure_size
;
1795 ep
->entry_point_revision
= 0;
1796 memset(ep
->formatted_area
, 0, 5);
1797 memcpy(ep
->intermediate_anchor_string
, "_DMI_", 5);
1799 ep
->structure_table_length
= structure_table_length
;
1800 ep
->structure_table_address
= structure_table_address
;
1801 ep
->number_of_structures
= number_of_structures
;
1802 ep
->smbios_bcd_revision
= 0x24;
1805 ep
->intermediate_checksum
= 0;
1808 for (i
= 0; i
< 0x10; i
++)
1809 sum
+= ((int8_t *)start
)[i
];
1810 ep
->checksum
= -sum
;
1813 for (i
= 0x10; i
< ep
->length
; i
++)
1814 sum
+= ((int8_t *)start
)[i
];
1815 ep
->intermediate_checksum
= -sum
;
1818 /* Type 0 -- BIOS Information */
1819 #define RELEASE_DATE_STR "01/01/2007"
1821 smbios_type_0_init(void *start
)
1823 struct smbios_type_0
*p
= (struct smbios_type_0
*)start
;
1826 p
->header
.length
= sizeof(struct smbios_type_0
);
1827 p
->header
.handle
= 0;
1830 p
->bios_version_str
= 1;
1831 p
->bios_starting_address_segment
= 0xe800;
1832 p
->bios_release_date_str
= 2;
1833 p
->bios_rom_size
= 0; /* FIXME */
1835 memset(p
->bios_characteristics
, 0, 8);
1836 p
->bios_characteristics
[0] = 0x08; /* BIOS characteristics not supported */
1837 p
->bios_characteristics_extension_bytes
[0] = 0;
1838 p
->bios_characteristics_extension_bytes
[1] = 0;
1840 p
->system_bios_major_release
= 1;
1841 p
->system_bios_minor_release
= 0;
1842 p
->embedded_controller_major_release
= 0xff;
1843 p
->embedded_controller_minor_release
= 0xff;
1845 start
+= sizeof(struct smbios_type_0
);
1846 memcpy((char *)start
, BX_APPNAME
, sizeof(BX_APPNAME
));
1847 start
+= sizeof(BX_APPNAME
);
1848 memcpy((char *)start
, RELEASE_DATE_STR
, sizeof(RELEASE_DATE_STR
));
1849 start
+= sizeof(RELEASE_DATE_STR
);
1850 *((uint8_t *)start
) = 0;
1855 /* Type 1 -- System Information */
1857 smbios_type_1_init(void *start
)
1859 struct smbios_type_1
*p
= (struct smbios_type_1
*)start
;
1861 p
->header
.length
= sizeof(struct smbios_type_1
);
1862 p
->header
.handle
= 0x100;
1864 p
->manufacturer_str
= 0;
1865 p
->product_name_str
= 0;
1867 p
->serial_number_str
= 0;
1869 memcpy(p
->uuid
, bios_uuid
, 16);
1871 p
->wake_up_type
= 0x06; /* power switch */
1872 p
->sku_number_str
= 0;
1875 start
+= sizeof(struct smbios_type_1
);
1876 *((uint16_t *)start
) = 0;
1881 /* Type 3 -- System Enclosure */
1883 smbios_type_3_init(void *start
)
1885 struct smbios_type_3
*p
= (struct smbios_type_3
*)start
;
1888 p
->header
.length
= sizeof(struct smbios_type_3
);
1889 p
->header
.handle
= 0x300;
1891 p
->manufacturer_str
= 0;
1892 p
->type
= 0x01; /* other */
1894 p
->serial_number_str
= 0;
1895 p
->asset_tag_number_str
= 0;
1896 p
->boot_up_state
= 0x03; /* safe */
1897 p
->power_supply_state
= 0x03; /* safe */
1898 p
->thermal_state
= 0x03; /* safe */
1899 p
->security_status
= 0x02; /* unknown */
1902 p
->number_of_power_cords
= 0;
1903 p
->contained_element_count
= 0;
1905 start
+= sizeof(struct smbios_type_3
);
1906 *((uint16_t *)start
) = 0;
1911 /* Type 4 -- Processor Information */
1913 smbios_type_4_init(void *start
, unsigned int cpu_number
)
1915 struct smbios_type_4
*p
= (struct smbios_type_4
*)start
;
1918 p
->header
.length
= sizeof(struct smbios_type_4
);
1919 p
->header
.handle
= 0x400 + cpu_number
;
1921 p
->socket_designation_str
= 1;
1922 p
->processor_type
= 0x03; /* CPU */
1923 p
->processor_family
= 0x01; /* other */
1924 p
->processor_manufacturer_str
= 0;
1926 p
->processor_id
[0] = cpuid_signature
;
1927 p
->processor_id
[1] = cpuid_features
;
1929 p
->processor_version_str
= 0;
1931 p
->external_clock
= 0;
1933 p
->max_speed
= 0; /* unknown */
1934 p
->current_speed
= 0; /* unknown */
1936 p
->status
= 0x41; /* socket populated, CPU enabled */
1937 p
->processor_upgrade
= 0x01; /* other */
1939 p
->l1_cache_handle
= 0xffff; /* cache information structure not provided */
1940 p
->l2_cache_handle
= 0xffff;
1941 p
->l3_cache_handle
= 0xffff;
1943 start
+= sizeof(struct smbios_type_4
);
1945 memcpy((char *)start
, "CPU " "\0" "" "\0" "", 7);
1946 ((char *)start
)[4] = cpu_number
+ '0';
1951 /* Type 16 -- Physical Memory Array */
1953 smbios_type_16_init(void *start
, uint32_t memsize
, int nr_mem_devs
)
1955 struct smbios_type_16
*p
= (struct smbios_type_16
*)start
;
1957 p
->header
.type
= 16;
1958 p
->header
.length
= sizeof(struct smbios_type_16
);
1959 p
->header
.handle
= 0x1000;
1961 p
->location
= 0x01; /* other */
1962 p
->use
= 0x03; /* system memory */
1963 p
->error_correction
= 0x01; /* other */
1964 p
->maximum_capacity
= memsize
* 1024;
1965 p
->memory_error_information_handle
= 0xfffe; /* none provided */
1966 p
->number_of_memory_devices
= nr_mem_devs
;
1968 start
+= sizeof(struct smbios_type_16
);
1969 *((uint16_t *)start
) = 0;
1974 /* Type 17 -- Memory Device */
1976 smbios_type_17_init(void *start
, uint32_t memory_size_mb
, int instance
)
1978 struct smbios_type_17
*p
= (struct smbios_type_17
*)start
;
1980 p
->header
.type
= 17;
1981 p
->header
.length
= sizeof(struct smbios_type_17
);
1982 p
->header
.handle
= 0x1100 + instance
;
1984 p
->physical_memory_array_handle
= 0x1000;
1985 p
->total_width
= 64;
1987 /* TODO: should assert in case something is wrong ASSERT((memory_size_mb & ~0x7fff) == 0); */
1988 p
->size
= memory_size_mb
;
1989 p
->form_factor
= 0x09; /* DIMM */
1991 p
->device_locator_str
= 1;
1992 p
->bank_locator_str
= 0;
1993 p
->memory_type
= 0x07; /* RAM */
1996 start
+= sizeof(struct smbios_type_17
);
1997 snprintf(start
, 8, "DIMM %d", instance
);
1998 start
+= strlen(start
) + 1;
1999 *((uint8_t *)start
) = 0;
2004 /* Type 19 -- Memory Array Mapped Address */
2006 smbios_type_19_init(void *start
, uint32_t memory_size_mb
, int instance
)
2008 struct smbios_type_19
*p
= (struct smbios_type_19
*)start
;
2010 p
->header
.type
= 19;
2011 p
->header
.length
= sizeof(struct smbios_type_19
);
2012 p
->header
.handle
= 0x1300 + instance
;
2014 p
->starting_address
= instance
<< 24;
2015 p
->ending_address
= p
->starting_address
+ (memory_size_mb
<< 10) - 1;
2016 p
->memory_array_handle
= 0x1000;
2017 p
->partition_width
= 1;
2019 start
+= sizeof(struct smbios_type_19
);
2020 *((uint16_t *)start
) = 0;
2025 /* Type 20 -- Memory Device Mapped Address */
2027 smbios_type_20_init(void *start
, uint32_t memory_size_mb
, int instance
)
2029 struct smbios_type_20
*p
= (struct smbios_type_20
*)start
;
2031 p
->header
.type
= 20;
2032 p
->header
.length
= sizeof(struct smbios_type_20
);
2033 p
->header
.handle
= 0x1400 + instance
;
2035 p
->starting_address
= instance
<< 24;
2036 p
->ending_address
= p
->starting_address
+ (memory_size_mb
<< 10) - 1;
2037 p
->memory_device_handle
= 0x1100 + instance
;
2038 p
->memory_array_mapped_address_handle
= 0x1300 + instance
;
2039 p
->partition_row_position
= 1;
2040 p
->interleave_position
= 0;
2041 p
->interleaved_data_depth
= 0;
2043 start
+= sizeof(struct smbios_type_20
);
2045 *((uint16_t *)start
) = 0;
2049 /* Type 32 -- System Boot Information */
2051 smbios_type_32_init(void *start
)
2053 struct smbios_type_32
*p
= (struct smbios_type_32
*)start
;
2055 p
->header
.type
= 32;
2056 p
->header
.length
= sizeof(struct smbios_type_32
);
2057 p
->header
.handle
= 0x2000;
2058 memset(p
->reserved
, 0, 6);
2059 p
->boot_status
= 0; /* no errors detected */
2061 start
+= sizeof(struct smbios_type_32
);
2062 *((uint16_t *)start
) = 0;
2067 /* Type 127 -- End of Table */
2069 smbios_type_127_init(void *start
)
2071 struct smbios_type_127
*p
= (struct smbios_type_127
*)start
;
2073 p
->header
.type
= 127;
2074 p
->header
.length
= sizeof(struct smbios_type_127
);
2075 p
->header
.handle
= 0x7f00;
2077 start
+= sizeof(struct smbios_type_127
);
2078 *((uint16_t *)start
) = 0;
2083 void smbios_init(void)
2085 unsigned cpu_num
, nr_structs
= 0, max_struct_size
= 0;
2086 char *start
, *p
, *q
;
2087 int memsize
= (ram_end
== ram_size
) ? ram_size
/ (1024 * 1024) :
2088 (ram_end
- (1ull << 32) + ram_size
) / (1024 * 1024);
2091 #ifdef BX_USE_EBDA_TABLES
2092 ebda_cur_addr
= align(ebda_cur_addr
, 16);
2093 start
= (void *)(ebda_cur_addr
);
2095 bios_table_cur_addr
= align(bios_table_cur_addr
, 16);
2096 start
= (void *)(bios_table_cur_addr
);
2099 p
= (char *)start
+ sizeof(struct smbios_entry_point
);
2101 #define add_struct(fn) do{ \
2104 if ((q - p) > max_struct_size) \
2105 max_struct_size = q - p; \
2109 add_struct(smbios_type_0_init(p
));
2110 add_struct(smbios_type_1_init(p
));
2111 add_struct(smbios_type_3_init(p
));
2112 for (cpu_num
= 1; cpu_num
<= smp_cpus
; cpu_num
++)
2113 add_struct(smbios_type_4_init(p
, cpu_num
));
2115 /* Each 'memory device' covers up to 16GB of address space. */
2116 nr_mem_devs
= (memsize
+ 0x3fff) >> 14;
2117 add_struct(smbios_type_16_init(p
, memsize
, nr_mem_devs
));
2118 for ( i
= 0; i
< nr_mem_devs
; i
++ )
2120 uint32_t dev_memsize
= ((i
== (nr_mem_devs
- 1))
2121 ? (((memsize
-1) & 0x3fff)+1) : 0x4000);
2122 add_struct(smbios_type_17_init(p
, dev_memsize
, i
));
2123 add_struct(smbios_type_19_init(p
, dev_memsize
, i
));
2124 add_struct(smbios_type_20_init(p
, dev_memsize
, i
));
2127 add_struct(smbios_type_32_init(p
));
2128 add_struct(smbios_type_127_init(p
));
2132 smbios_entry_point_init(
2133 start
, max_struct_size
,
2134 (p
- (char *)start
) - sizeof(struct smbios_entry_point
),
2135 (uint32_t)(start
+ sizeof(struct smbios_entry_point
)),
2138 #ifdef BX_USE_EBDA_TABLES
2139 ebda_cur_addr
+= (p
- (char *)start
);
2141 bios_table_cur_addr
+= (p
- (char *)start
);
2144 BX_INFO("SMBIOS table addr=0x%08lx\n", (unsigned long)start
);
2147 static uint32_t find_resume_vector(void)
2149 unsigned long addr
, start
, end
;
2151 #ifdef BX_USE_EBDA_TABLES
2152 start
= align(ebda_cur_addr
, 16);
2155 if (bios_table_cur_addr
== 0)
2157 start
= align(bios_table_cur_addr
, 16);
2158 end
= bios_table_end_addr
;
2161 for (addr
= start
; addr
< end
; addr
+= 16) {
2162 if (!memcmp((void*)addr
, "RSD PTR ", 8)) {
2163 struct rsdp_descriptor
*rsdp
= (void*)addr
;
2164 struct rsdt_descriptor_rev1
*rsdt
= (void*)rsdp
->rsdt_physical_address
;
2165 struct fadt_descriptor_rev1
*fadt
= (void*)rsdt
->table_offset_entry
[0];
2166 struct facs_descriptor_rev1
*facs
= (void*)fadt
->firmware_ctrl
;
2167 return facs
->firmware_waking_vector
;
2174 static void find_440fx(PCIDevice
*d
)
2176 uint16_t vendor_id
, device_id
;
2178 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
2179 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
2181 if (vendor_id
== PCI_VENDOR_ID_INTEL
&& device_id
== PCI_DEVICE_ID_INTEL_82441
)
2185 static void reinit_piix4_pm(PCIDevice
*d
)
2187 uint16_t vendor_id
, device_id
;
2189 vendor_id
= pci_config_readw(d
, PCI_VENDOR_ID
);
2190 device_id
= pci_config_readw(d
, PCI_DEVICE_ID
);
2192 if (vendor_id
== PCI_VENDOR_ID_INTEL
&& device_id
== PCI_DEVICE_ID_INTEL_82371AB_3
)
2196 void rombios32_init(uint32_t *s3_resume_vector
, uint8_t *shutdown_flag
)
2198 BX_INFO("Starting rombios32\n");
2199 BX_INFO("Shutdown flag %x\n", *shutdown_flag
);
2202 qemu_cfg_port
= qemu_cfg_port_probe();
2213 find_bios_table_area();
2215 if (*shutdown_flag
== 0xfe) {
2216 /* redirect bios read access to RAM */
2217 pci_for_each_device(find_440fx
);
2218 bios_lock_shadow_ram(); /* bios is already copied */
2219 *s3_resume_vector
= find_resume_vector();
2220 if (!*s3_resume_vector
) {
2221 BX_INFO("This is S3 resume but wakeup vector is NULL\n");
2223 BX_INFO("S3 resume vector %p\n", *s3_resume_vector
);
2224 pci_for_each_device(reinit_piix4_pm
);
2231 if (bios_table_cur_addr
!= 0) {
2242 bios_lock_shadow_ram();
2244 BX_INFO("bios_table_cur_addr: 0x%08lx\n", bios_table_cur_addr
);
2245 if (bios_table_cur_addr
> bios_table_end_addr
)
2246 BX_PANIC("bios_table_end_addr overflow!\n");
2247 #ifdef BX_USE_EBDA_TABLES
2248 BX_INFO("ebda_cur_addr: 0x%08lx\n", ebda_cur_addr
);
2249 if (ebda_cur_addr
> 0xA0000)
2250 BX_PANIC("ebda_cur_addr overflow!\n");