1 #include <console/console.h>
2 #include <device/path.h>
3 #include <device/pci_ids.h>
5 #include <arch/smp/mpspec.h>
8 #include <cpu/x86/lapic.h>
10 /* Initialize the specified "mc" struct with initial values. */
11 void mptable_init(struct mp_config_table *mc, u32 lapic_addr)
15 memset(mc, 0, sizeof(*mc));
17 memcpy(mc->mpc_signature, MPC_SIGNATURE, 4);
19 mc->mpc_length = sizeof(*mc); /* Initially just the header size. */
20 mc->mpc_spec = 0x04; /* MultiProcessor specification 1.4 */
21 mc->mpc_checksum = 0; /* Not yet computed. */
24 mc->mpc_entry_count = 0; /* No entries yet... */
25 mc->mpc_lapic = lapic_addr;
30 strncpy(mc->mpc_oem, CONFIG_MAINBOARD_VENDOR, 8);
31 strncpy(mc->mpc_productid, CONFIG_MAINBOARD_PART_NUMBER, 12);
34 * The oem/productid fields are exactly 8/12 bytes long. If the resp.
35 * entry is shorter, the remaining bytes are filled with spaces.
37 for (i = MIN(strlen(CONFIG_MAINBOARD_VENDOR), 8); i < 8; i++)
39 for (i = MIN(strlen(CONFIG_MAINBOARD_PART_NUMBER), 12); i < 12; i++)
40 mc->mpc_productid[i] = ' ';
43 unsigned char smp_compute_checksum(void *v, int len)
46 unsigned char checksum;
50 for(i = 0; i < len; i++) {
56 void *smp_write_floating_table(unsigned long addr)
58 /* 16 byte align the table address */
59 addr = (addr + 0xf) & (~0xf);
60 return smp_write_floating_table_physaddr(addr, addr + SMP_FLOATING_TABLE_LEN);
63 void *smp_write_floating_table_physaddr(unsigned long addr, unsigned long mpf_physptr)
65 struct intel_mp_floating *mf;
70 mf->mpf_signature[0] = '_';
71 mf->mpf_signature[1] = 'M';
72 mf->mpf_signature[2] = 'P';
73 mf->mpf_signature[3] = '_';
74 mf->mpf_physptr = mpf_physptr;
76 mf->mpf_specification = 4;
83 mf->mpf_checksum = smp_compute_checksum(mf, mf->mpf_length*16);
87 void *smp_next_mpc_entry(struct mp_config_table *mc)
90 v = (void *)(((char *)mc) + mc->mpc_length);
93 static void smp_add_mpc_entry(struct mp_config_table *mc, unsigned length)
95 mc->mpc_length += length;
96 mc->mpc_entry_count++;
99 void *smp_next_mpe_entry(struct mp_config_table *mc)
102 v = (void *)(((char *)mc) + mc->mpc_length + mc->mpe_length);
105 static void smp_add_mpe_entry(struct mp_config_table *mc, mpe_t mpe)
107 mc->mpe_length += mpe->mpe_length;
110 void smp_write_processor(struct mp_config_table *mc,
111 unsigned char apicid, unsigned char apicver,
112 unsigned char cpuflag, unsigned int cpufeature,
113 unsigned int featureflag)
115 struct mpc_config_processor *mpc;
116 mpc = smp_next_mpc_entry(mc);
117 memset(mpc, '\0', sizeof(*mpc));
118 mpc->mpc_type = MP_PROCESSOR;
119 mpc->mpc_apicid = apicid;
120 mpc->mpc_apicver = apicver;
121 mpc->mpc_cpuflag = cpuflag;
122 mpc->mpc_cpufeature = cpufeature;
123 mpc->mpc_featureflag = featureflag;
124 smp_add_mpc_entry(mc, sizeof(*mpc));
127 /* If we assume a symmetric processor configuration we can
128 * get all of the information we need to write the processor
129 * entry from the bootstrap processor.
130 * Plus I don't think linux really even cares.
131 * Having the proper apicid's in the table so the non-bootstrap
132 * processors can be woken up should be enough.
134 void smp_write_processors(struct mp_config_table *mc)
137 unsigned apic_version;
138 unsigned cpu_features;
139 unsigned cpu_feature_flags;
140 struct cpuid_result result;
143 boot_apic_id = lapicid();
144 apic_version = lapic_read(LAPIC_LVR) & 0xff;
146 cpu_features = result.eax;
147 cpu_feature_flags = result.edx;
148 for(cpu = all_devices; cpu; cpu = cpu->next) {
149 unsigned long cpu_flag;
150 if ((cpu->path.type != DEVICE_PATH_APIC) ||
151 (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
158 cpu_flag = MPC_CPU_ENABLED;
159 if (boot_apic_id == cpu->path.apic.apic_id) {
160 cpu_flag = MPC_CPU_ENABLED | MPC_CPU_BOOTPROCESSOR;
162 smp_write_processor(mc,
163 cpu->path.apic.apic_id, apic_version,
164 cpu_flag, cpu_features, cpu_feature_flags
169 static void smp_write_bus(struct mp_config_table *mc,
170 unsigned char id, const char *bustype)
172 struct mpc_config_bus *mpc;
173 mpc = smp_next_mpc_entry(mc);
174 memset(mpc, '\0', sizeof(*mpc));
175 mpc->mpc_type = MP_BUS;
177 memcpy(mpc->mpc_bustype, bustype, sizeof(mpc->mpc_bustype));
178 smp_add_mpc_entry(mc, sizeof(*mpc));
181 void smp_write_ioapic(struct mp_config_table *mc,
182 unsigned char id, unsigned char ver,
183 unsigned long apicaddr)
185 struct mpc_config_ioapic *mpc;
186 mpc = smp_next_mpc_entry(mc);
187 memset(mpc, '\0', sizeof(*mpc));
188 mpc->mpc_type = MP_IOAPIC;
189 mpc->mpc_apicid = id;
190 mpc->mpc_apicver = ver;
191 mpc->mpc_flags = MPC_APIC_USABLE;
192 mpc->mpc_apicaddr = apicaddr;
193 smp_add_mpc_entry(mc, sizeof(*mpc));
196 void smp_write_intsrc(struct mp_config_table *mc,
197 unsigned char irqtype, unsigned short irqflag,
198 unsigned char srcbus, unsigned char srcbusirq,
199 unsigned char dstapic, unsigned char dstirq)
201 struct mpc_config_intsrc *mpc;
202 mpc = smp_next_mpc_entry(mc);
203 memset(mpc, '\0', sizeof(*mpc));
204 mpc->mpc_type = MP_INTSRC;
205 mpc->mpc_irqtype = irqtype;
206 mpc->mpc_irqflag = irqflag;
207 mpc->mpc_srcbus = srcbus;
208 mpc->mpc_srcbusirq = srcbusirq;
209 mpc->mpc_dstapic = dstapic;
210 mpc->mpc_dstirq = dstirq;
211 smp_add_mpc_entry(mc, sizeof(*mpc));
213 printk(BIOS_DEBUG, "add intsrc srcbus 0x%x srcbusirq 0x%x, dstapic 0x%x, dstirq 0x%x\n",
214 srcbus, srcbusirq, dstapic, dstirq);
215 hexdump(__func__, mpc, sizeof(*mpc));
219 void smp_write_intsrc_pci_bridge(struct mp_config_table *mc,
220 unsigned char irqtype, unsigned short irqflag,
222 unsigned char dstapic, unsigned char *dstirq)
224 struct device *child;
231 unsigned char dstirq_x[4];
233 for (link = dev->link_list; link; link = link->next) {
235 child = link->children;
236 srcbus = link->secondary;
239 if (child->path.type != DEVICE_PATH_PCI)
242 slot = (child->path.pci.devfn >> 3);
244 for (i = 0; i < 4; i++)
245 dstirq_x[i] = dstirq[(i + slot) % 4];
247 if ((child->class >> 16) != PCI_BASE_CLASS_BRIDGE) {
249 printk(BIOS_DEBUG, "route irq: %s\n", dev_path(child));
250 for (i = 0; i < 4; i++)
251 smp_write_intsrc(mc, irqtype, irqflag, srcbus, (slot<<2)|i, dstapic, dstirq_x[i]);
255 switch (child->class>>8) {
256 case PCI_CLASS_BRIDGE_PCI:
257 case PCI_CLASS_BRIDGE_PCMCIA:
258 case PCI_CLASS_BRIDGE_CARDBUS:
259 printk(BIOS_DEBUG, "route irq bridge: %s\n", dev_path(child));
260 smp_write_intsrc_pci_bridge(mc, irqtype, irqflag, child, dstapic, dstirq_x);
264 child = child->sibling;
270 void smp_write_lintsrc(struct mp_config_table *mc,
271 unsigned char irqtype, unsigned short irqflag,
272 unsigned char srcbusid, unsigned char srcbusirq,
273 unsigned char destapic, unsigned char destapiclint)
275 struct mpc_config_lintsrc *mpc;
276 mpc = smp_next_mpc_entry(mc);
277 memset(mpc, '\0', sizeof(*mpc));
278 mpc->mpc_type = MP_LINTSRC;
279 mpc->mpc_irqtype = irqtype;
280 mpc->mpc_irqflag = irqflag;
281 mpc->mpc_srcbusid = srcbusid;
282 mpc->mpc_srcbusirq = srcbusirq;
283 mpc->mpc_destapic = destapic;
284 mpc->mpc_destapiclint = destapiclint;
285 smp_add_mpc_entry(mc, sizeof(*mpc));
288 void smp_write_address_space(struct mp_config_table *mc,
289 unsigned char busid, unsigned char address_type,
290 unsigned int address_base_low, unsigned int address_base_high,
291 unsigned int address_length_low, unsigned int address_length_high)
293 struct mp_exten_system_address_space *mpe;
294 mpe = smp_next_mpe_entry(mc);
295 memset(mpe, '\0', sizeof(*mpe));
296 mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE;
297 mpe->mpe_length = sizeof(*mpe);
298 mpe->mpe_busid = busid;
299 mpe->mpe_address_type = address_type;
300 mpe->mpe_address_base_low = address_base_low;
301 mpe->mpe_address_base_high = address_base_high;
302 mpe->mpe_address_length_low = address_length_low;
303 mpe->mpe_address_length_high = address_length_high;
304 smp_add_mpe_entry(mc, (mpe_t)mpe);
308 void smp_write_bus_hierarchy(struct mp_config_table *mc,
309 unsigned char busid, unsigned char bus_info,
310 unsigned char parent_busid)
312 struct mp_exten_bus_hierarchy *mpe;
313 mpe = smp_next_mpe_entry(mc);
314 memset(mpe, '\0', sizeof(*mpe));
315 mpe->mpe_type = MPE_BUS_HIERARCHY;
316 mpe->mpe_length = sizeof(*mpe);
317 mpe->mpe_busid = busid;
318 mpe->mpe_bus_info = bus_info;
319 mpe->mpe_parent_busid = parent_busid;
320 smp_add_mpe_entry(mc, (mpe_t)mpe);
323 void smp_write_compatibility_address_space(struct mp_config_table *mc,
324 unsigned char busid, unsigned char address_modifier,
325 unsigned int range_list)
327 struct mp_exten_compatibility_address_space *mpe;
328 mpe = smp_next_mpe_entry(mc);
329 memset(mpe, '\0', sizeof(*mpe));
330 mpe->mpe_type = MPE_COMPATIBILITY_ADDRESS_SPACE;
331 mpe->mpe_length = sizeof(*mpe);
332 mpe->mpe_busid = busid;
333 mpe->mpe_address_modifier = address_modifier;
334 mpe->mpe_range_list = range_list;
335 smp_add_mpe_entry(mc, (mpe_t)mpe);
338 void mptable_add_isa_interrupts(struct mp_config_table *mc, unsigned long bus_isa, unsigned long apicid, int external_int2)
340 /*I/O Ints: Type Trigger Polarity Bus ID IRQ APIC ID PIN# */
341 smp_write_intsrc(mc, external_int2?mp_INT:mp_ExtINT,
342 MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, apicid, 0x0);
343 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x1, apicid, 0x1);
344 smp_write_intsrc(mc, external_int2?mp_ExtINT:mp_INT,
345 MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, apicid, 0x2);
346 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x3, apicid, 0x3);
347 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x4, apicid, 0x4);
348 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x6, apicid, 0x6);
349 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x7, apicid, 0x7);
350 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x8, apicid, 0x8);
351 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x9, apicid, 0x9);
352 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xa, apicid, 0xa);
353 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xb, apicid, 0xb);
354 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xc, apicid, 0xc);
355 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xd, apicid, 0xd);
356 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xe, apicid, 0xe);
357 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xf, apicid, 0xf);
360 void mptable_write_buses(struct mp_config_table *mc, int *max_pci_bus, int *isa_bus) {
361 int dummy, i, highest;
365 if (!max_pci_bus) max_pci_bus = &dummy;
366 if (!isa_bus) isa_bus = &dummy;
370 memset(buses, 0, sizeof(buses));
372 for (dev = all_devices; dev; dev = dev->next) {
374 for (bus = dev->link_list; bus; bus = bus->next) {
375 if (bus->secondary > 255) {
376 printk(BIOS_ERR, "A bus claims to have a bus ID > 255?!? Aborting");
379 buses[bus->secondary] = 1;
380 if (highest < bus->secondary) highest = bus->secondary;
383 for (i=0; i <= highest; i++) {
385 smp_write_bus(mc, i, "PCI ");
389 *isa_bus = *max_pci_bus + 1;
390 smp_write_bus(mc, *isa_bus, "ISA ");