Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / fw / mptable.c
1 // MPTable generation (on emulators)
2 // DO NOT ADD NEW FEATURES HERE.  (See paravirt.c / biostables.c instead.)
3 //
4 // Copyright (C) 2008-2010  Kevin O'Connor <kevin@koconnor.net>
5 // Copyright (C) 2006 Fabrice Bellard
6 //
7 // This file may be distributed under the terms of the GNU LGPLv3 license.
8
9 #include "config.h" // CONFIG_*
10 #include "hw/pci.h"
11 #include "hw/pci_regs.h"
12 #include "malloc.h" // free
13 #include "output.h" // dprintf
14 #include "romfile.h" // romfile_loadint
15 #include "std/mptable.h" // MPTABLE_SIGNATURE
16 #include "string.h" // memset
17 #include "util.h" // MaxCountCPUs
18 #include "x86.h" // cpuid
19
20 void
21 mptable_setup(void)
22 {
23     if (! CONFIG_MPTABLE)
24         return;
25
26     dprintf(3, "init MPTable\n");
27
28     // Config structure in temp area.
29     struct mptable_config_s *config = malloc_tmp(32*1024);
30     if (!config) {
31         warn_noalloc();
32         return;
33     }
34     memset(config, 0, sizeof(*config));
35     config->signature = MPCONFIG_SIGNATURE;
36     config->spec = 4;
37     memcpy(config->oemid, BUILD_CPUNAME8, sizeof(config->oemid));
38     memcpy(config->productid, "0.1         ", sizeof(config->productid));
39     config->lapic = BUILD_APIC_ADDR;
40
41     // Detect cpu info
42     u32 cpuid_signature, ebx, ecx, cpuid_features;
43     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
44     if (! cpuid_signature) {
45         // Use default values.
46         cpuid_signature = 0x600;
47         cpuid_features = 0x201;
48     }
49     int pkgcpus = 1;
50     if (cpuid_features & (1 << 28)) {
51         /* Only populate the MPS tables with the first logical CPU in
52            each package */
53         pkgcpus = (ebx >> 16) & 0xff;
54         pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
55     }
56     u8 apic_version = readl((u8*)BUILD_APIC_ADDR + 0x30) & 0xff;
57
58     // CPU definitions.
59     struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
60     int i;
61     for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
62         memset(cpu, 0, sizeof(*cpu));
63         cpu->type = MPT_TYPE_CPU;
64         cpu->apicid = i;
65         cpu->apicver = apic_version;
66         /* cpu flags: enabled, bootstrap cpu */
67         cpu->cpuflag = (apic_id_is_present(i) ? 0x01 : 0x00) | ((i==0) ? 0x02 : 0x00);
68         cpu->cpusignature = cpuid_signature;
69         cpu->featureflag = cpuid_features;
70         cpu++;
71     }
72     int entrycount = cpu - cpus;
73
74     // PCI bus
75     struct mpt_bus *buses = (void*)cpu, *bus = buses;
76     if (!hlist_empty(&PCIDevices)) {
77         memset(bus, 0, sizeof(*bus));
78         bus->type = MPT_TYPE_BUS;
79         bus->busid = 0;
80         memcpy(bus->bustype, "PCI   ", sizeof(bus->bustype));
81         bus++;
82         entrycount++;
83     }
84
85     /* isa bus */
86     int isabusid = bus - buses;
87     memset(bus, 0, sizeof(*bus));
88     bus->type = MPT_TYPE_BUS;
89     bus->busid = isabusid;
90     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
91     bus++;
92     entrycount++;
93
94     /* ioapic */
95     u8 ioapic_id = BUILD_IOAPIC_ID;
96     struct mpt_ioapic *ioapic = (void*)bus;
97     memset(ioapic, 0, sizeof(*ioapic));
98     ioapic->type = MPT_TYPE_IOAPIC;
99     ioapic->apicid = ioapic_id;
100     ioapic->apicver = 0x11;
101     ioapic->flags = 1; // enable
102     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
103     entrycount++;
104
105     /* irqs */
106     struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
107     int dev = -1;
108     unsigned short pinmask = 0;
109
110     struct pci_device *pci;
111     foreachpci(pci) {
112         u16 bdf = pci->bdf;
113         if (pci_bdf_to_bus(bdf) != 0)
114             break;
115         int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
116         int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
117         if (pin == 0)
118             continue;
119         if (dev != pci_bdf_to_busdev(bdf)) {
120             dev = pci_bdf_to_busdev(bdf);
121             pinmask = 0;
122         }
123         if (pinmask & (1 << pin)) /* pin was seen already */
124             continue;
125         pinmask |= (1 << pin);
126         memset(intsrc, 0, sizeof(*intsrc));
127         intsrc->type = MPT_TYPE_INTSRC;
128         intsrc->irqtype = 0; /* INT */
129         intsrc->irqflag = 1; /* active high */
130         intsrc->srcbus = pci_bdf_to_bus(bdf); /* PCI bus */
131         intsrc->srcbusirq = (pci_bdf_to_dev(bdf) << 2) | (pin - 1);
132         intsrc->dstapic = ioapic_id;
133         intsrc->dstirq = irq;
134         intsrc++;
135     }
136
137     int irq0_override = romfile_loadint("etc/irq0-override", 0);
138     for (i = 0; i < 16; i++) {
139         memset(intsrc, 0, sizeof(*intsrc));
140         if (BUILD_PCI_IRQS & (1 << i))
141             continue;
142         intsrc->type = MPT_TYPE_INTSRC;
143         intsrc->irqtype = 0; /* INT */
144         intsrc->irqflag = 0; /* conform to bus spec */
145         intsrc->srcbus = isabusid; /* ISA bus */
146         intsrc->srcbusirq = i;
147         intsrc->dstapic = ioapic_id;
148         intsrc->dstirq = i;
149         if (irq0_override) {
150             /* Destination 2 is covered by irq0->inti2 override (i ==
151                0). Source IRQ 2 is unused */
152             if (i == 0)
153                 intsrc->dstirq = 2;
154             else if (i == 2)
155                 intsrc--;
156         }
157         intsrc++;
158     }
159
160     /* Local interrupt assignment */
161     intsrc->type = MPT_TYPE_LOCAL_INT;
162     intsrc->irqtype = 3; /* ExtINT */
163     intsrc->irqflag = 0; /* PO, EL default */
164     intsrc->srcbus = isabusid; /* ISA */
165     intsrc->srcbusirq = 0;
166     intsrc->dstapic = 0; /* BSP == APIC #0 */
167     intsrc->dstirq = 0; /* LINTIN0 */
168     intsrc++;
169
170     intsrc->type = MPT_TYPE_LOCAL_INT;
171     intsrc->irqtype = 1; /* NMI */
172     intsrc->irqflag = 0; /* PO, EL default */
173     intsrc->srcbus = isabusid; /* ISA */
174     intsrc->srcbusirq = 0;
175     intsrc->dstapic = 0xff; /* to all local APICs */
176     intsrc->dstirq = 1; /* LINTIN1 */
177     intsrc++;
178     entrycount += intsrc - intsrcs;
179
180     // Finalize config structure.
181     int length = (void*)intsrc - (void*)config;
182     config->entrycount = entrycount;
183     config->length = length;
184     config->checksum -= checksum(config, length);
185
186     // floating pointer structure
187     struct mptable_floating_s floating;
188     memset(&floating, 0, sizeof(floating));
189     floating.signature = MPTABLE_SIGNATURE;
190     floating.physaddr = (u32)config;
191     floating.length = 1;
192     floating.spec_rev = 4;
193     floating.checksum -= checksum(&floating, sizeof(floating));
194     copy_mptable(&floating);
195     free(config);
196 }