irq0override provided by qemu.
[seabios.git] / src / mptable.c
1 // MPTable generation (on emulators)
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
10 #include "mptable.h" // MPTABLE_SIGNATURE
11 #include "paravirt.h"
12
13 void
14 mptable_init(void)
15 {
16     if (! CONFIG_MPTABLE)
17         return;
18
19     dprintf(3, "init MPTable\n");
20
21     int smp_cpus = CountCPUs;
22     if (smp_cpus <= 1)
23         // Building an mptable on uniprocessor machines confuses some OSes.
24         return;
25
26     int length = (sizeof(struct mptable_config_s)
27                   + sizeof(struct mpt_cpu) * smp_cpus
28                   + sizeof(struct mpt_bus)
29                   + sizeof(struct mpt_ioapic)
30                   + sizeof(struct mpt_intsrc) * 16);
31     struct mptable_config_s *config = malloc_fseg(length);
32     struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
33     if (!config || !floating) {
34         dprintf(1, "No room for MPTABLE!\n");
35         return;
36     }
37
38     /* floating pointer structure */
39     memset(floating, 0, sizeof(*floating));
40     floating->signature = MPTABLE_SIGNATURE;
41     floating->physaddr = (u32)config;
42     floating->length = 1;
43     floating->spec_rev = 4;
44     floating->checksum -= checksum(floating, sizeof(*floating));
45
46     // Config structure.
47     memset(config, 0, sizeof(*config));
48     config->signature = MPCONFIG_SIGNATURE;
49     config->spec = 4;
50     memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
51     memcpy(config->productid, "0.1         ", sizeof(config->productid));
52     config->entrycount = smp_cpus + 2 + 16;
53     config->lapic = BUILD_APIC_ADDR;
54
55     // CPU definitions.
56     u32 cpuid_signature, ebx, ecx, cpuid_features;
57     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
58     struct mpt_cpu *cpus = (void*)&config[1];
59     int i;
60     for (i = 0; i < smp_cpus; i++) {
61         struct mpt_cpu *cpu = &cpus[i];
62         memset(cpu, 0, sizeof(*cpu));
63         cpu->type = MPT_TYPE_CPU;
64         cpu->apicid = i;
65         cpu->apicver = 0x11;
66         /* cpu flags: enabled, bootstrap cpu */
67         cpu->cpuflag = (i == 0 ? 3 : 1);
68         if (cpuid_signature) {
69             cpu->cpusignature = cpuid_signature;
70             cpu->featureflag = cpuid_features;
71         } else {
72             cpu->cpusignature = 0x600;
73             cpu->featureflag = 0x201;
74         }
75     }
76
77     /* isa bus */
78     struct mpt_bus *bus = (void*)&cpus[smp_cpus];
79     memset(bus, 0, sizeof(*bus));
80     bus->type = MPT_TYPE_BUS;
81     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
82
83     /* ioapic */
84     u8 ioapic_id = smp_cpus;
85     struct mpt_ioapic *ioapic = (void*)&bus[1];
86     memset(ioapic, 0, sizeof(*ioapic));
87     ioapic->type = MPT_TYPE_IOAPIC;
88     ioapic->apicid = ioapic_id;
89     ioapic->apicver = 0x11;
90     ioapic->flags = 1; // enable
91     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
92
93     /* irqs */
94     struct mpt_intsrc *intsrc = (void*)&ioapic[1];
95     for (i = 0; i < 16; i++) {
96         memset(intsrc, 0, sizeof(*intsrc));
97         intsrc->type = MPT_TYPE_INTSRC;
98         intsrc->srcbusirq = i;
99         intsrc->dstapic = ioapic_id;
100         intsrc->dstirq = i;
101         if (qemu_cfg_irq0_override()) {
102             /* Destination 2 is covered by irq0->inti2 override (i ==
103                0). Source IRQ 2 is unused */
104             if (i == 0)
105                 intsrc->dstirq = 2;
106             else if (i == 2)
107                 intsrc--;
108         }
109         intsrc++;
110     }
111
112     // Set checksum.
113     config->length = (void*)intsrc - (void*)config;
114     config->checksum -= checksum(config, config->length);
115
116     dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
117             floating, config, config->length);
118 }