2409fcfc6be47762f3e9af72bd85992846f76f3a
[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" // qemu_cfg_irq0_override
12 #include "pci.h"
13 #include "pci_regs.h"
14
15 void
16 mptable_init(void)
17 {
18     if (! CONFIG_MPTABLE)
19         return;
20
21     dprintf(3, "init MPTable\n");
22
23     // Allocate memory
24     int length = (sizeof(struct mptable_config_s)
25                   + sizeof(struct mpt_cpu) * MaxCountCPUs
26                   + sizeof(struct mpt_bus) * 2
27                   + sizeof(struct mpt_ioapic)
28                   + sizeof(struct mpt_intsrc) * 34);
29     struct mptable_config_s *config = malloc_fseg(length);
30     struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
31     if (!config || !floating) {
32         dprintf(1, "No room for MPTABLE!\n");
33         free(config);
34         free(floating);
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->lapic = BUILD_APIC_ADDR;
53
54     // Detect cpu info
55     u32 cpuid_signature, ebx, ecx, cpuid_features;
56     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
57     int pkgcpus = 1;
58     if (cpuid_features & (1 << 28)) {
59         /* Only populate the MPS tables with the first logical CPU in
60            each package */
61         pkgcpus = (ebx >> 16) & 0xff;
62         pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
63     }
64
65     // CPU definitions.
66     struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
67     int i;
68     for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
69         memset(cpu, 0, sizeof(*cpu));
70         cpu->type = MPT_TYPE_CPU;
71         cpu->apicid = i;
72         cpu->apicver = 0x11;
73         /* cpu flags: enabled, bootstrap cpu */
74         cpu->cpuflag = (i < CountCPUs) | ((i == 0) << 1);
75         if (cpuid_signature) {
76             cpu->cpusignature = cpuid_signature;
77             cpu->featureflag = cpuid_features;
78         } else {
79             cpu->cpusignature = 0x600;
80             cpu->featureflag = 0x201;
81         }
82         cpu++;
83     }
84     int entrycount = cpu - cpus;
85
86     /* isa bus */
87     struct mpt_bus *bus = (void*)cpu;
88     memset(bus, 0, sizeof(*bus));
89     bus->type = MPT_TYPE_BUS;
90     bus->busid = 1;
91     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
92     entrycount++;
93
94     bus++;
95     memset(bus, 0, sizeof(*bus));
96     bus->type = MPT_TYPE_BUS;
97     bus->busid = 0;
98     memcpy(bus->bustype, "PCI   ", sizeof(bus->bustype));
99     entrycount++;
100
101     /* ioapic */
102     u8 ioapic_id = CountCPUs;
103     struct mpt_ioapic *ioapic = (void*)&bus[1];
104     memset(ioapic, 0, sizeof(*ioapic));
105     ioapic->type = MPT_TYPE_IOAPIC;
106     ioapic->apicid = ioapic_id;
107     ioapic->apicver = 0x11;
108     ioapic->flags = 1; // enable
109     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
110     entrycount++;
111
112     /* irqs */
113     struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
114     int bdf, max, dev = -1;
115     unsigned short mask = 0, pinmask;
116
117     foreachpci(bdf, max) {
118         int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
119         int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
120         if (pin == 0)
121             continue;
122         if (dev != pci_bdf_to_dev(bdf)) {
123             dev = pci_bdf_to_dev(bdf);
124             pinmask = 0;
125         }
126         if (pinmask & (1 << pin)) /* pin was seen already */
127             continue;
128         pinmask |= (1 << pin);
129         mask |= (1 << irq);
130         memset(intsrc, 0, sizeof(*intsrc));
131         intsrc->type = MPT_TYPE_INTSRC;
132         intsrc->irqtype = 0; /* INT */
133         intsrc->irqflag = 1; /* active high */
134         intsrc->srcbus = 0; /* PCI bus */
135         intsrc->srcbusirq = (dev << 2) | (pin - 1);
136         intsrc->dstapic = ioapic_id;
137         intsrc->dstirq = irq;
138         intsrc++;
139     }
140
141     for (i = 0; i < 16; i++) {
142         memset(intsrc, 0, sizeof(*intsrc));
143         if (mask & (1 << i))
144             continue;
145         intsrc->type = MPT_TYPE_INTSRC;
146         intsrc->irqtype = 0; /* INT */
147         intsrc->irqflag = 0; /* conform to bus spec */
148         intsrc->srcbus = 1; /* ISA bus */
149         intsrc->srcbusirq = i;
150         intsrc->dstapic = ioapic_id;
151         intsrc->dstirq = i;
152         if (qemu_cfg_irq0_override()) {
153             /* Destination 2 is covered by irq0->inti2 override (i ==
154                0). Source IRQ 2 is unused */
155             if (i == 0)
156                 intsrc->dstirq = 2;
157             else if (i == 2)
158                 intsrc--;
159         }
160         intsrc++;
161     }
162     entrycount += intsrc - intsrcs;
163
164     /* Local interrupt assignment */
165     intsrc->type = MPT_TYPE_LOCAL_INT;
166     intsrc->irqtype = 3; /* ExtINT */
167     intsrc->irqflag = 0; /* PO, EL default */
168     intsrc->srcbus = 1; /* ISA */
169     intsrc->srcbusirq = 0;
170     intsrc->dstapic = 0; /* BSP == APIC #0 */
171     intsrc->dstirq = 0; /* LINTIN0 */
172     intsrc++;
173     entrycount++;
174
175     intsrc->type = MPT_TYPE_LOCAL_INT;
176     intsrc->irqtype = 1; /* NMI */
177     intsrc->irqflag = 0; /* PO, EL default */
178     intsrc->srcbus = 1; /* ISA */
179     intsrc->srcbusirq = 0;
180     intsrc->dstapic = 0; /* BSP == APIC #0 */
181     intsrc->dstirq = 1; /* LINTIN1 */
182     intsrc++;
183     entrycount++;
184
185     // Finalize config structure.
186     config->entrycount = entrycount;
187     config->length = (void*)intsrc - (void*)config;
188     config->checksum -= checksum(config, config->length);
189
190     dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
191             floating, config, config->length);
192 }