5758b28de5cac30fdc09777bbafe1b0c37dbc8ff
[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 "memmap.h" // malloc_fseg
10 #include "config.h" // CONFIG_*
11 #include "mptable.h" // MPTABLE_SIGNATURE
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_floating_s)
27                   + sizeof(struct mptable_config_s)
28                   + sizeof(struct mpt_cpu) * smp_cpus
29                   + sizeof(struct mpt_bus)
30                   + sizeof(struct mpt_ioapic)
31                   + sizeof(struct mpt_intsrc) * 16);
32     void *start = malloc_fseg(length);
33     if (!start) {
34         dprintf(1, "No room for MPTABLE!\n");
35         return;
36     }
37
38     /* floating pointer structure */
39     struct mptable_floating_s *floating = start;
40     memset(floating, 0, sizeof(*floating));
41     struct mptable_config_s *config = (void*)&floating[1];
42     floating->signature = MPTABLE_SIGNATURE;
43     floating->physaddr = (u32)config;
44     floating->length = 1;
45     floating->spec_rev = 4;
46     floating->checksum -= checksum(floating, sizeof(*floating));
47
48     // Config structure.
49     memset(config, 0, sizeof(*config));
50     config->signature = MPCONFIG_SIGNATURE;
51     config->length = length - sizeof(*floating);
52     config->spec = 4;
53     memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
54     memcpy(config->productid, "0.1         ", sizeof(config->productid));
55     config->entrycount = smp_cpus + 2 + 16;
56     config->lapic = BUILD_APIC_ADDR;
57
58     // CPU definitions.
59     u32 cpuid_signature, ebx, ecx, cpuid_features;
60     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
61     struct mpt_cpu *cpus = (void*)&config[1];
62     int i;
63     for (i = 0; i < smp_cpus; i++) {
64         struct mpt_cpu *cpu = &cpus[i];
65         memset(cpu, 0, sizeof(*cpu));
66         cpu->type = MPT_TYPE_CPU;
67         cpu->apicid = i;
68         cpu->apicver = 0x11;
69         /* cpu flags: enabled, bootstrap cpu */
70         cpu->cpuflag = (i == 0 ? 3 : 1);
71         if (cpuid_signature) {
72             cpu->cpusignature = cpuid_signature;
73             cpu->featureflag = cpuid_features;
74         } else {
75             cpu->cpusignature = 0x600;
76             cpu->featureflag = 0x201;
77         }
78     }
79
80     /* isa bus */
81     struct mpt_bus *bus = (void*)&cpus[smp_cpus];
82     memset(bus, 0, sizeof(*bus));
83     bus->type = MPT_TYPE_BUS;
84     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
85
86     /* ioapic */
87     u8 ioapic_id = smp_cpus;
88     struct mpt_ioapic *ioapic = (void*)&bus[1];
89     memset(ioapic, 0, sizeof(*ioapic));
90     ioapic->type = MPT_TYPE_IOAPIC;
91     ioapic->apicid = ioapic_id;
92     ioapic->apicver = 0x11;
93     ioapic->flags = 1; // enable
94     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
95
96     /* irqs */
97     struct mpt_intsrc *intsrcs = (void *)&ioapic[1];
98     for(i = 0; i < 16; i++) {
99         struct mpt_intsrc *isrc = &intsrcs[i];
100         memset(isrc, 0, sizeof(*isrc));
101         isrc->type = MPT_TYPE_INTSRC;
102         isrc->srcbusirq = i;
103         isrc->dstapic = ioapic_id;
104         isrc->dstirq = i;
105     }
106
107     // Set checksum.
108     config->checksum -= checksum(config, config->length);
109
110     dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
111             floating, config, length);
112 }