mptable: Pull cpuid_signature/features setting out of loop.
[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     // Config structure in temp area.
24     struct mptable_config_s *config = malloc_tmphigh(32*1024);
25     if (!config) {
26         dprintf(1, "No space for temp mptable\n");
27         return;
28     }
29     memset(config, 0, sizeof(*config));
30     config->signature = MPCONFIG_SIGNATURE;
31     config->spec = 4;
32     memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
33     memcpy(config->productid, "0.1         ", sizeof(config->productid));
34     config->lapic = BUILD_APIC_ADDR;
35
36     // Detect cpu info
37     u32 cpuid_signature, ebx, ecx, cpuid_features;
38     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
39     if (! cpuid_signature) {
40         // Use default values.
41         cpuid_signature = 0x600;
42         cpuid_features = 0x201;
43     }
44     int pkgcpus = 1;
45     if (cpuid_features & (1 << 28)) {
46         /* Only populate the MPS tables with the first logical CPU in
47            each package */
48         pkgcpus = (ebx >> 16) & 0xff;
49         pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
50     }
51     u8 apic_version = readl((u8*)BUILD_APIC_ADDR + 0x30) & 0xff;
52
53     // CPU definitions.
54     struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
55     int i;
56     for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
57         memset(cpu, 0, sizeof(*cpu));
58         cpu->type = MPT_TYPE_CPU;
59         cpu->apicid = i;
60         cpu->apicver = apic_version;
61         /* cpu flags: enabled, bootstrap cpu */
62         cpu->cpuflag = (i < CountCPUs) | ((i == 0) << 1);
63         cpu->cpusignature = cpuid_signature;
64         cpu->featureflag = cpuid_features;
65         cpu++;
66     }
67     int entrycount = cpu - cpus;
68
69     // PCI buses
70     struct mpt_bus *bus = (void*)cpu;
71     int bdf, max, lastbus = -1;
72     foreachpci(bdf, max) {
73         int curbus = pci_bdf_to_bus(bdf);
74         if (curbus == lastbus)
75             continue;
76         lastbus = curbus;
77         memset(bus, 0, sizeof(*bus));
78         bus->type = MPT_TYPE_BUS;
79         bus->busid = curbus;
80         memcpy(bus->bustype, "PCI   ", sizeof(bus->bustype));
81         bus++;
82         entrycount++;
83     }
84
85     /* isa bus */
86     int isabusid;
87     memset(bus, 0, sizeof(*bus));
88     bus->type = MPT_TYPE_BUS;
89     isabusid = bus->busid = lastbus + 1;
90     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
91     entrycount++;
92
93     /* ioapic */
94     u8 ioapic_id = CountCPUs;
95     struct mpt_ioapic *ioapic = (void*)&bus[1];
96     memset(ioapic, 0, sizeof(*ioapic));
97     ioapic->type = MPT_TYPE_IOAPIC;
98     ioapic->apicid = ioapic_id;
99     ioapic->apicver = 0x11;
100     ioapic->flags = 1; // enable
101     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
102     entrycount++;
103
104     /* irqs */
105     struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
106     int dev = -1;
107     unsigned short mask = 0, pinmask = 0;
108
109     foreachpci(bdf, max) {
110         int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
111         int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
112         if (pin == 0)
113             continue;
114         if (dev != pci_bdf_to_busdev(bdf)) {
115             dev = pci_bdf_to_busdev(bdf);
116             pinmask = 0;
117         }
118         if (pinmask & (1 << pin)) /* pin was seen already */
119             continue;
120         pinmask |= (1 << pin);
121         mask |= (1 << irq);
122         memset(intsrc, 0, sizeof(*intsrc));
123         intsrc->type = MPT_TYPE_INTSRC;
124         intsrc->irqtype = 0; /* INT */
125         intsrc->irqflag = 1; /* active high */
126         intsrc->srcbus = pci_bdf_to_bus(bdf); /* PCI bus */
127         intsrc->srcbusirq = (pci_bdf_to_dev(bdf) << 2) | (pin - 1);
128         intsrc->dstapic = ioapic_id;
129         intsrc->dstirq = irq;
130         intsrc++;
131     }
132
133     for (i = 0; i < 16; i++) {
134         memset(intsrc, 0, sizeof(*intsrc));
135         if (mask & (1 << i))
136             continue;
137         intsrc->type = MPT_TYPE_INTSRC;
138         intsrc->irqtype = 0; /* INT */
139         intsrc->irqflag = 0; /* conform to bus spec */
140         intsrc->srcbus = isabusid; /* ISA bus */
141         intsrc->srcbusirq = i;
142         intsrc->dstapic = ioapic_id;
143         intsrc->dstirq = i;
144         if (qemu_cfg_irq0_override()) {
145             /* Destination 2 is covered by irq0->inti2 override (i ==
146                0). Source IRQ 2 is unused */
147             if (i == 0)
148                 intsrc->dstirq = 2;
149             else if (i == 2)
150                 intsrc--;
151         }
152         intsrc++;
153     }
154     entrycount += intsrc - intsrcs;
155
156     /* Local interrupt assignment */
157     intsrc->type = MPT_TYPE_LOCAL_INT;
158     intsrc->irqtype = 3; /* ExtINT */
159     intsrc->irqflag = 0; /* PO, EL default */
160     intsrc->srcbus = isabusid; /* ISA */
161     intsrc->srcbusirq = 0;
162     intsrc->dstapic = 0; /* BSP == APIC #0 */
163     intsrc->dstirq = 0; /* LINTIN0 */
164     intsrc++;
165     entrycount++;
166
167     intsrc->type = MPT_TYPE_LOCAL_INT;
168     intsrc->irqtype = 1; /* NMI */
169     intsrc->irqflag = 0; /* PO, EL default */
170     intsrc->srcbus = isabusid; /* ISA */
171     intsrc->srcbusirq = 0;
172     intsrc->dstapic = 0; /* BSP == APIC #0 */
173     intsrc->dstirq = 1; /* LINTIN1 */
174     intsrc++;
175     entrycount++;
176
177     // Finalize config structure.
178     int length = (void*)intsrc - (void*)config;
179     config->entrycount = entrycount;
180     config->length = length;
181     config->checksum -= checksum(config, length);
182
183     // Allocate final memory locations.  (In theory the config
184     // structure can go in high memory, but Linux kernels before
185     // v2.6.30 crash with that.)
186     struct mptable_config_s *finalconfig = malloc_fseg(length);
187     struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
188     if (!finalconfig || !floating) {
189         dprintf(1, "No room for MPTABLE!\n");
190         free(config);
191         free(finalconfig);
192         free(floating);
193         return;
194     }
195     memcpy(finalconfig, config, length);
196     free(config);
197
198     /* floating pointer structure */
199     memset(floating, 0, sizeof(*floating));
200     floating->signature = MPTABLE_SIGNATURE;
201     floating->physaddr = (u32)finalconfig;
202     floating->length = 1;
203     floating->spec_rev = 4;
204     floating->checksum -= checksum(floating, sizeof(*floating));
205
206     dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
207             floating, finalconfig, length);
208 }