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