blob: 15c3c0e1eac8d5f1167cc81cfc234511929ce905 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Mike Loptien91597342014-06-12 10:22:27 -06002
Eric Biederman8ca8d762003-04-22 19:02:15 +00003#include <console/console.h>
Eric Biederman7003ba42004-10-16 06:20:29 +00004#include <device/path.h>
Yinghai Luf327d9f2008-02-20 17:41:38 +00005#include <device/pci_ids.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00006#include <arch/smp/mpspec.h>
7#include <string.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +00008#include <arch/cpu.h>
9#include <cpu/x86/lapic.h>
Sven Schnelle0fa50a12012-06-21 22:19:48 +020010#include <drivers/generic/ioapic/chip.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000011
Uwe Hermann55dc2232010-10-25 15:32:07 +000012/* Initialize the specified "mc" struct with initial values. */
Uwe Hermannc36d5062010-12-16 19:51:38 +000013void mptable_init(struct mp_config_table *mc, u32 lapic_addr)
Uwe Hermann55dc2232010-10-25 15:32:07 +000014{
Uwe Hermannc36d5062010-12-16 19:51:38 +000015 int i;
Uwe Hermann55dc2232010-10-25 15:32:07 +000016
17 memset(mc, 0, sizeof(*mc));
Uwe Hermannc36d5062010-12-16 19:51:38 +000018
Uwe Hermann55dc2232010-10-25 15:32:07 +000019 memcpy(mc->mpc_signature, MPC_SIGNATURE, 4);
Uwe Hermannc36d5062010-12-16 19:51:38 +000020
Uwe Hermann55dc2232010-10-25 15:32:07 +000021 mc->mpc_length = sizeof(*mc); /* Initially just the header size. */
22 mc->mpc_spec = 0x04; /* MultiProcessor specification 1.4 */
23 mc->mpc_checksum = 0; /* Not yet computed. */
Uwe Hermann55dc2232010-10-25 15:32:07 +000024 mc->mpc_oemptr = 0;
25 mc->mpc_oemsize = 0;
26 mc->mpc_entry_count = 0; /* No entries yet... */
27 mc->mpc_lapic = lapic_addr;
28 mc->mpe_length = 0;
29 mc->mpe_checksum = 0;
30 mc->reserved = 0;
Uwe Hermannc36d5062010-12-16 19:51:38 +000031
32 strncpy(mc->mpc_oem, CONFIG_MAINBOARD_VENDOR, 8);
33 strncpy(mc->mpc_productid, CONFIG_MAINBOARD_PART_NUMBER, 12);
34
35 /*
36 * The oem/productid fields are exactly 8/12 bytes long. If the resp.
37 * entry is shorter, the remaining bytes are filled with spaces.
38 */
39 for (i = MIN(strlen(CONFIG_MAINBOARD_VENDOR), 8); i < 8; i++)
40 mc->mpc_oem[i] = ' ';
41 for (i = MIN(strlen(CONFIG_MAINBOARD_PART_NUMBER), 12); i < 12; i++)
42 mc->mpc_productid[i] = ' ';
Uwe Hermann55dc2232010-10-25 15:32:07 +000043}
44
Patrick Georgib0a9c5c2011-10-07 23:01:55 +020045static unsigned char smp_compute_checksum(void *v, int len)
Eric Biederman8ca8d762003-04-22 19:02:15 +000046{
47 unsigned char *bytes;
48 unsigned char checksum;
49 int i;
50 bytes = v;
51 checksum = 0;
Lee Leahy9c7c6f72017-03-16 11:24:09 -070052 for (i = 0; i < len; i++)
Eric Biederman8ca8d762003-04-22 19:02:15 +000053 checksum -= bytes[i];
Eric Biederman8ca8d762003-04-22 19:02:15 +000054 return checksum;
55}
56
Lee Leahy6f80ccc2017-03-16 15:18:22 -070057static void *smp_write_floating_table_physaddr(uintptr_t addr,
58 uintptr_t mpf_physptr, unsigned int virtualwire)
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000059{
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +100060 struct intel_mp_floating *mf;
61 void *v;
Stefan Reinauer14e22772010-04-27 06:56:47 +000062
Yinghai Luf327d9f2008-02-20 17:41:38 +000063 v = (void *)addr;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +100064 mf = v;
65 mf->mpf_signature[0] = '_';
66 mf->mpf_signature[1] = 'M';
67 mf->mpf_signature[2] = 'P';
68 mf->mpf_signature[3] = '_';
69 mf->mpf_physptr = mpf_physptr;
70 mf->mpf_length = 1;
71 mf->mpf_specification = 4;
72 mf->mpf_checksum = 0;
73 mf->mpf_feature1 = 0;
Mike Loptiend0167d32014-06-11 14:20:48 -060074 mf->mpf_feature2 = virtualwire?MP_FEATURE_PIC:MP_FEATURE_VIRTUALWIRE;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +100075 mf->mpf_feature3 = 0;
76 mf->mpf_feature4 = 0;
77 mf->mpf_feature5 = 0;
78 mf->mpf_checksum = smp_compute_checksum(mf, mf->mpf_length*16);
79 return v;
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000080}
81
Patrick Georgic75c79b2011-10-07 22:41:07 +020082void *smp_write_floating_table(unsigned long addr, unsigned int virtualwire)
83{
84 /* 16 byte align the table address */
85 addr = (addr + 0xf) & (~0xf);
Lee Leahy6f80ccc2017-03-16 15:18:22 -070086 return smp_write_floating_table_physaddr(addr, addr
87 + SMP_FLOATING_TABLE_LEN, virtualwire);
Patrick Georgic75c79b2011-10-07 22:41:07 +020088}
89
Eric Biederman8ca8d762003-04-22 19:02:15 +000090void *smp_next_mpc_entry(struct mp_config_table *mc)
91{
92 void *v;
93 v = (void *)(((char *)mc) + mc->mpc_length);
Mike Loptienbd4553b2014-06-16 10:46:56 -060094
Eric Biederman8ca8d762003-04-22 19:02:15 +000095 return v;
96}
Mike Loptienbd4553b2014-06-16 10:46:56 -060097static void smp_add_mpc_entry(struct mp_config_table *mc, u16 length)
Eric Biederman8ca8d762003-04-22 19:02:15 +000098{
99 mc->mpc_length += length;
100 mc->mpc_entry_count++;
101}
102
103void *smp_next_mpe_entry(struct mp_config_table *mc)
104{
105 void *v;
106 v = (void *)(((char *)mc) + mc->mpc_length + mc->mpe_length);
Mike Loptienbd4553b2014-06-16 10:46:56 -0600107
Eric Biederman8ca8d762003-04-22 19:02:15 +0000108 return v;
109}
110static void smp_add_mpe_entry(struct mp_config_table *mc, mpe_t mpe)
111{
112 mc->mpe_length += mpe->mpe_length;
113}
114
Mike Loptienbd4553b2014-06-16 10:46:56 -0600115/*
116 * Type 0: Processor Entries:
117 * Entry Type, LAPIC ID, LAPIC Version, CPU Flags EN/BP,
118 * CPU Signature (Stepping, Model, Family), Feature Flags
119 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000120void smp_write_processor(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600121 u8 apicid, u8 apicver, u8 cpuflag,
122 u32 cpufeature, u32 featureflag)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000123{
124 struct mpc_config_processor *mpc;
125 mpc = smp_next_mpc_entry(mc);
126 memset(mpc, '\0', sizeof(*mpc));
127 mpc->mpc_type = MP_PROCESSOR;
128 mpc->mpc_apicid = apicid;
129 mpc->mpc_apicver = apicver;
130 mpc->mpc_cpuflag = cpuflag;
131 mpc->mpc_cpufeature = cpufeature;
132 mpc->mpc_featureflag = featureflag;
133 smp_add_mpc_entry(mc, sizeof(*mpc));
134}
135
Mike Loptienbd4553b2014-06-16 10:46:56 -0600136/*
137 * If we assume a symmetric processor configuration we can
Eric Biederman8ca8d762003-04-22 19:02:15 +0000138 * get all of the information we need to write the processor
139 * entry from the bootstrap processor.
140 * Plus I don't think linux really even cares.
141 * Having the proper apicid's in the table so the non-bootstrap
142 * processors can be woken up should be enough.
143 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000144void smp_write_processors(struct mp_config_table *mc)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000145{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000146 int boot_apic_id;
Patrick Georgi07b42152011-10-14 23:02:57 +0200147 int order_id;
Lee Leahye5f29e82017-03-16 14:16:56 -0700148 unsigned int apic_version;
149 unsigned int cpu_features;
150 unsigned int cpu_feature_flags;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100151 struct device *cpu;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000152
Eric Biedermanb78c1972004-10-14 20:54:17 +0000153 boot_apic_id = lapicid();
154 apic_version = lapic_read(LAPIC_LVR) & 0xff;
Subrata Banik53b08c32018-12-10 14:11:35 +0530155 cpu_features = cpu_get_cpuid();
156 cpu_feature_flags = cpu_get_feature_flags_edx();
Patrick Georgi07b42152011-10-14 23:02:57 +0200157 /* order the output of the cpus to fix a bug in kernel 2.6.11 */
Lee Leahy024b13d2017-03-16 13:41:11 -0700158 for (order_id = 0; order_id < 256; order_id++) {
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200159 for (cpu = all_devices; cpu; cpu = cpu->next) {
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000160 unsigned long cpu_flag;
161 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700162 (cpu->bus->dev->path.type !=
163 DEVICE_PATH_CPU_CLUSTER))
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000164 continue;
165
166 if (!cpu->enabled)
167 continue;
168
169 cpu_flag = MPC_CPU_ENABLED;
170
171 if (boot_apic_id == cpu->path.apic.apic_id)
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700172 cpu_flag = MPC_CPU_ENABLED
173 | MPC_CPU_BOOTPROCESSOR;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000174
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200175 if (cpu->path.apic.apic_id == order_id) {
Mike Loptienbd4553b2014-06-16 10:46:56 -0600176 smp_write_processor(mc,
177 cpu->path.apic.apic_id, apic_version,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700178 cpu_flag, cpu_features,
179 cpu_feature_flags
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000180 );
181 break;
182 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184 }
185}
186
Mike Loptienbd4553b2014-06-16 10:46:56 -0600187/*
188 * Type 1: Bus Entries:
189 * Entry Type, Bus ID, Bus Type
190 */
Patrick Georgi patrick612fcc32010-11-23 07:19:54 +0000191static void smp_write_bus(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600192 u8 id, const char *bustype)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000193{
194 struct mpc_config_bus *mpc;
195 mpc = smp_next_mpc_entry(mc);
196 memset(mpc, '\0', sizeof(*mpc));
197 mpc->mpc_type = MP_BUS;
198 mpc->mpc_busid = id;
199 memcpy(mpc->mpc_bustype, bustype, sizeof(mpc->mpc_bustype));
200 smp_add_mpc_entry(mc, sizeof(*mpc));
201}
202
Mike Loptienbd4553b2014-06-16 10:46:56 -0600203/*
204 * Type 2: I/O APIC Entries:
205 * Entry Type, APIC ID, Version,
206 * APIC Flags:EN, Address
207 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000208void smp_write_ioapic(struct mp_config_table *mc,
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800209 u8 id, u8 ver, void *apicaddr)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000210{
211 struct mpc_config_ioapic *mpc;
212 mpc = smp_next_mpc_entry(mc);
213 memset(mpc, '\0', sizeof(*mpc));
214 mpc->mpc_type = MP_IOAPIC;
215 mpc->mpc_apicid = id;
216 mpc->mpc_apicver = ver;
217 mpc->mpc_flags = MPC_APIC_USABLE;
218 mpc->mpc_apicaddr = apicaddr;
219 smp_add_mpc_entry(mc, sizeof(*mpc));
220}
221
Mike Loptienbd4553b2014-06-16 10:46:56 -0600222/*
223 * Type 3: I/O Interrupt Table Entries:
224 * Entry Type, Int Type, Int Polarity, Int Level,
225 * Source Bus ID, Source Bus IRQ, Dest APIC ID, Dest PIN#
226 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000227void smp_write_intsrc(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600228 u8 irqtype, u16 irqflag,
229 u8 srcbus, u8 srcbusirq,
230 u8 dstapic, u8 dstirq)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231{
232 struct mpc_config_intsrc *mpc;
233 mpc = smp_next_mpc_entry(mc);
234 memset(mpc, '\0', sizeof(*mpc));
235 mpc->mpc_type = MP_INTSRC;
236 mpc->mpc_irqtype = irqtype;
237 mpc->mpc_irqflag = irqflag;
238 mpc->mpc_srcbus = srcbus;
239 mpc->mpc_srcbusirq = srcbusirq;
240 mpc->mpc_dstapic = dstapic;
241 mpc->mpc_dstirq = dstirq;
242 smp_add_mpc_entry(mc, sizeof(*mpc));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000243}
244
Mike Loptienbd4553b2014-06-16 10:46:56 -0600245/*
246 * Type 3: I/O Interrupt Table Entries for PCI Devices:
247 * This has the same fields as 'Type 3: I/O Interrupt Table Entries'
248 * but the Source Bus IRQ field has a slightly different
249 * definition:
250 * Bits 1-0: PIRQ pin: INT_A# = 0, INT_B# = 1, INT_C# = 2, INT_D# = 3
251 * Bits 2-6: Originating PCI Device Number (Not its parent bridge device number)
252 * Bit 7: Reserved
253 */
254void smp_write_pci_intsrc(struct mp_config_table *mc,
255 u8 irqtype, u8 srcbus, u8 dev, u8 pirq,
256 u8 dstapic, u8 dstirq)
257{
258 u8 srcbusirq = (dev << 2) | pirq;
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700259 printk(BIOS_SPEW,
260 "\tPCI srcbusirq = 0x%x from dev = 0x%x and pirq = %x\n",
261 srcbusirq, dev, pirq);
262 smp_write_intsrc(mc, irqtype, MP_IRQ_TRIGGER_LEVEL
263 | MP_IRQ_POLARITY_LOW, srcbus, srcbusirq, dstapic, dstirq);
Mike Loptienbd4553b2014-06-16 10:46:56 -0600264}
265
Yinghai Luf327d9f2008-02-20 17:41:38 +0000266void smp_write_intsrc_pci_bridge(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600267 u8 irqtype, u16 irqflag, struct device *dev,
Yinghai Luf327d9f2008-02-20 17:41:38 +0000268 unsigned char dstapic, unsigned char *dstirq)
269{
270 struct device *child;
271
Yinghai Luf327d9f2008-02-20 17:41:38 +0000272 int i;
273 int srcbus;
274 int slot;
275
276 struct bus *link;
277 unsigned char dstirq_x[4];
278
Myles Watson894a3472010-06-09 22:41:35 +0000279 for (link = dev->link_list; link; link = link->next) {
Yinghai Luf327d9f2008-02-20 17:41:38 +0000280
Yinghai Luf327d9f2008-02-20 17:41:38 +0000281 child = link->children;
282 srcbus = link->secondary;
283
284 while (child) {
285 if (child->path.type != DEVICE_PATH_PCI)
286 goto next;
287
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000288 slot = (child->path.pci.devfn >> 3);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000289 /* round pins */
290 for (i = 0; i < 4; i++)
291 dstirq_x[i] = dstirq[(i + slot) % 4];
292
293 if ((child->class >> 16) != PCI_BASE_CLASS_BRIDGE) {
294 /* pci device */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700295 printk(BIOS_DEBUG, "route irq: %s\n",
296 dev_path(child));
Yinghai Luf327d9f2008-02-20 17:41:38 +0000297 for (i = 0; i < 4; i++)
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700298 smp_write_intsrc(mc, irqtype, irqflag,
299 srcbus, (slot<<2)|i, dstapic,
300 dstirq_x[i]);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000301 goto next;
302 }
303
304 switch (child->class>>8) {
305 case PCI_CLASS_BRIDGE_PCI:
306 case PCI_CLASS_BRIDGE_PCMCIA:
307 case PCI_CLASS_BRIDGE_CARDBUS:
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700308 printk(BIOS_DEBUG, "route irq bridge: %s\n",
309 dev_path(child));
310 smp_write_intsrc_pci_bridge(mc, irqtype,
311 irqflag, child, dstapic, dstirq_x);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000312 }
313
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100314next:
Yinghai Luf327d9f2008-02-20 17:41:38 +0000315 child = child->sibling;
316 }
317
318 }
319}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000320
Mike Loptienbd4553b2014-06-16 10:46:56 -0600321/*
322 * Type 4: Local Interrupt Assignment Entries:
323 * Entry Type, Int Type, Int Polarity, Int Level,
324 * Source Bus ID, Source Bus IRQ, Dest LAPIC ID,
325 * Dest LAPIC LINTIN#
326 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000327void smp_write_lintsrc(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600328 u8 irqtype, u16 irqflag,
329 u8 srcbusid, u8 srcbusirq,
330 u8 destapic, u8 destapiclint)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000331{
332 struct mpc_config_lintsrc *mpc;
333 mpc = smp_next_mpc_entry(mc);
334 memset(mpc, '\0', sizeof(*mpc));
335 mpc->mpc_type = MP_LINTSRC;
336 mpc->mpc_irqtype = irqtype;
337 mpc->mpc_irqflag = irqflag;
338 mpc->mpc_srcbusid = srcbusid;
339 mpc->mpc_srcbusirq = srcbusirq;
340 mpc->mpc_destapic = destapic;
341 mpc->mpc_destapiclint = destapiclint;
342 smp_add_mpc_entry(mc, sizeof(*mpc));
343}
344
Mike Loptienbd4553b2014-06-16 10:46:56 -0600345/*
346 * Type 128: System Address Space Mapping Entries
347 * Entry Type, Entry Length, Bus ID, Address Type,
348 * Address Base Lo/Hi, Address Length Lo/Hi
349 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000350void smp_write_address_space(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600351 u8 busid, u8 address_type,
352 u32 address_base_low, u32 address_base_high,
353 u32 address_length_low, u32 address_length_high)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000354{
355 struct mp_exten_system_address_space *mpe;
356 mpe = smp_next_mpe_entry(mc);
357 memset(mpe, '\0', sizeof(*mpe));
358 mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE;
359 mpe->mpe_length = sizeof(*mpe);
360 mpe->mpe_busid = busid;
361 mpe->mpe_address_type = address_type;
362 mpe->mpe_address_base_low = address_base_low;
363 mpe->mpe_address_base_high = address_base_high;
364 mpe->mpe_address_length_low = address_length_low;
365 mpe->mpe_address_length_high = address_length_high;
366 smp_add_mpe_entry(mc, (mpe_t)mpe);
367}
368
Mike Loptienbd4553b2014-06-16 10:46:56 -0600369/*
370 * Type 129: Bus Hierarchy Descriptor Entry
371 * Entry Type, Entry Length, Bus ID, Bus Info,
372 * Parent Bus ID
373 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000374void smp_write_bus_hierarchy(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600375 u8 busid, u8 bus_info, u8 parent_busid)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000376{
377 struct mp_exten_bus_hierarchy *mpe;
378 mpe = smp_next_mpe_entry(mc);
379 memset(mpe, '\0', sizeof(*mpe));
380 mpe->mpe_type = MPE_BUS_HIERARCHY;
381 mpe->mpe_length = sizeof(*mpe);
382 mpe->mpe_busid = busid;
383 mpe->mpe_bus_info = bus_info;
384 mpe->mpe_parent_busid = parent_busid;
385 smp_add_mpe_entry(mc, (mpe_t)mpe);
386}
387
Mike Loptienbd4553b2014-06-16 10:46:56 -0600388/*
389 * Type 130: Compatibility Bus Address Space Modifier Entry
390 * Entry Type, Entry Length, Bus ID, Address Modifier
391 * Predefined Range List
392 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000393void smp_write_compatibility_address_space(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600394 u8 busid, u8 address_modifier,
395 u32 range_list)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000396{
397 struct mp_exten_compatibility_address_space *mpe;
398 mpe = smp_next_mpe_entry(mc);
399 memset(mpe, '\0', sizeof(*mpe));
400 mpe->mpe_type = MPE_COMPATIBILITY_ADDRESS_SPACE;
401 mpe->mpe_length = sizeof(*mpe);
402 mpe->mpe_busid = busid;
403 mpe->mpe_address_modifier = address_modifier;
404 mpe->mpe_range_list = range_list;
405 smp_add_mpe_entry(mc, (mpe_t)mpe);
406}
407
Patrick Georgi6eb7a532011-10-07 21:42:52 +0200408void mptable_lintsrc(struct mp_config_table *mc, unsigned long bus_isa)
409{
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700410 smp_write_lintsrc(mc, mp_ExtINT, MP_IRQ_TRIGGER_EDGE
411 | MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x0);
412 smp_write_lintsrc(mc, mp_NMI, MP_IRQ_TRIGGER_EDGE
413 | MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x1);
Patrick Georgi6eb7a532011-10-07 21:42:52 +0200414}
415
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700416void mptable_add_isa_interrupts(struct mp_config_table *mc,
417 unsigned long bus_isa, unsigned long apicid, int external_int2)
Patrick Georgic5b87c82010-05-20 15:28:19 +0000418{
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700419/*I/O Ints: Type Trigger Polarity
420 * Bus ID IRQ APIC ID PIN# */
Patrick Georgic5b87c82010-05-20 15:28:19 +0000421 smp_write_intsrc(mc, external_int2?mp_INT:mp_ExtINT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700422 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
423 bus_isa, 0x0, apicid, 0x0);
424 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
425 bus_isa, 0x1, apicid, 0x1);
Patrick Georgic5b87c82010-05-20 15:28:19 +0000426 smp_write_intsrc(mc, external_int2?mp_ExtINT:mp_INT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700427 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
428 bus_isa, 0x0, apicid, 0x2);
429 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
430 bus_isa, 0x3, apicid, 0x3);
431 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
432 bus_isa, 0x4, apicid, 0x4);
433 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
434 bus_isa, 0x6, apicid, 0x6);
435 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
436 bus_isa, 0x7, apicid, 0x7);
437 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
438 bus_isa, 0x8, apicid, 0x8);
439 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
440 bus_isa, 0x9, apicid, 0x9);
441 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
442 bus_isa, 0xa, apicid, 0xa);
443 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
444 bus_isa, 0xb, apicid, 0xb);
445 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
446 bus_isa, 0xc, apicid, 0xc);
447 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
448 bus_isa, 0xd, apicid, 0xd);
449 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
450 bus_isa, 0xe, apicid, 0xe);
451 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
452 bus_isa, 0xf, apicid, 0xf);
Patrick Georgic5b87c82010-05-20 15:28:19 +0000453}
Patrick Georgiab272662010-06-25 13:43:22 +0000454
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700455void mptable_write_buses(struct mp_config_table *mc, int *max_pci_bus,
456 int *isa_bus)
457{
Patrick Georgiab272662010-06-25 13:43:22 +0000458 int dummy, i, highest;
459 char buses[256];
460 struct device *dev;
461
Lee Leahy0b5678f2017-03-16 16:01:40 -0700462 if (!max_pci_bus)
463 max_pci_bus = &dummy;
464 if (!isa_bus)
465 isa_bus = &dummy;
Patrick Georgiab272662010-06-25 13:43:22 +0000466
467 *max_pci_bus = 0;
468 highest = 0;
469 memset(buses, 0, sizeof(buses));
470
471 for (dev = all_devices; dev; dev = dev->next) {
472 struct bus *bus;
473 for (bus = dev->link_list; bus; bus = bus->next) {
474 if (bus->secondary > 255) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700475 printk(BIOS_ERR,
476 "A bus claims to have a bus ID > 255?!? Aborting");
Patrick Georgiab272662010-06-25 13:43:22 +0000477 return;
478 }
479 buses[bus->secondary] = 1;
Lee Leahy0b5678f2017-03-16 16:01:40 -0700480 if (highest < bus->secondary)
481 highest = bus->secondary;
Patrick Georgiab272662010-06-25 13:43:22 +0000482 }
483 }
Lee Leahy024b13d2017-03-16 13:41:11 -0700484 for (i = 0; i <= highest; i++) {
Patrick Georgiab272662010-06-25 13:43:22 +0000485 if (buses[i]) {
486 smp_write_bus(mc, i, "PCI ");
487 *max_pci_bus = i;
488 }
489 }
490 *isa_bus = *max_pci_bus + 1;
491 smp_write_bus(mc, *isa_bus, "ISA ");
492}
493
Patrick Georgib0a9c5c2011-10-07 23:01:55 +0200494void *mptable_finalize(struct mp_config_table *mc)
495{
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700496 mc->mpe_checksum = smp_compute_checksum(smp_next_mpc_entry(mc),
497 mc->mpe_length);
Patrick Georgib0a9c5c2011-10-07 23:01:55 +0200498 mc->mpc_checksum = smp_compute_checksum(mc, mc->mpc_length);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700499 printk(BIOS_DEBUG, "Wrote the mp table end at: %p - %p\n",
500 mc, smp_next_mpe_entry(mc));
Patrick Georgib0a9c5c2011-10-07 23:01:55 +0200501 return smp_next_mpe_entry(mc);
502}
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200503
Lubomir Rintel294446a2018-04-14 11:48:27 +0200504static const struct device *find_next_ioapic(unsigned int last_ioapic_id)
505{
506 const struct device *dev;
507 const struct device *result = NULL;
508 unsigned int ioapic_id = MAX_APICS;
509
510 for (dev = all_devices; dev; dev = dev->next) {
511 if (dev->path.type == DEVICE_PATH_IOAPIC &&
512 dev->path.ioapic.ioapic_id > last_ioapic_id &&
513 dev->path.ioapic.ioapic_id <= ioapic_id) {
514 result = dev;
515 }
516 }
517 return result;
518}
519
Aaron Durbin64031672018-04-21 14:45:32 -0600520unsigned long __weak write_smp_table(unsigned long addr)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200521{
522 struct drivers_generic_ioapic_config *ioapic_config;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000523 struct mp_config_table *mc;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200524 int isa_bus, pin, parentpin;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200525 const struct device *dev;
526 const struct device *parent;
527 const struct device *oldparent;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200528 void *tmp, *v;
Sven Schnelle8c027902012-08-20 11:19:00 +0200529 int isaioapic = -1, have_fixed_entries;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200530 const struct pci_irq_info *pci_irq_info;
Lubomir Rintel294446a2018-04-14 11:48:27 +0200531 unsigned int ioapic_id = 0;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200532
533 v = smp_write_floating_table(addr, 0);
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000534 mc = (void *)(((char *)v) + SMP_FLOATING_TABLE_LEN);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200535
536 mptable_init(mc, LOCAL_APIC_ADDR);
537
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500538 smp_write_processors(mc);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200539
540 mptable_write_buses(mc, NULL, &isa_bus);
541
Lubomir Rintel294446a2018-04-14 11:48:27 +0200542 while ((dev = find_next_ioapic(ioapic_id))) {
Lee Leahy0b5678f2017-03-16 16:01:40 -0700543 ioapic_config = dev->chip_info;
544 if (!ioapic_config) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700545 printk(BIOS_ERR, "%s has no config, ignoring\n",
546 dev_path(dev));
Lubomir Rintel294446a2018-04-14 11:48:27 +0200547 ioapic_id++;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200548 continue;
549 }
Lubomir Rintel294446a2018-04-14 11:48:27 +0200550
551 ioapic_id = dev->path.ioapic.ioapic_id;
552 smp_write_ioapic(mc, ioapic_id,
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200553 ioapic_config->version,
554 ioapic_config->base);
555
556 if (ioapic_config->have_isa_interrupts) {
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500557 if (isaioapic >= 0)
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700558 printk(BIOS_ERR,
559 "More than one IOAPIC with ISA interrupts?\n");
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200560 else
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500561 isaioapic = dev->path.ioapic.ioapic_id;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200562 }
563 }
564
565 if (isaioapic >= 0) {
566 /* Legacy Interrupts */
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500567 printk(BIOS_DEBUG, "Writing ISA IRQs\n");
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200568 mptable_add_isa_interrupts(mc, isa_bus, isaioapic, 0);
569 }
570
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200571 for (dev = all_devices; dev; dev = dev->next) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200572
Sven Schnelle64c40dd2012-08-20 11:17:52 +0200573 if (dev->path.type != DEVICE_PATH_PCI || !dev->enabled)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200574 continue;
575
Sven Schnelle8c027902012-08-20 11:19:00 +0200576 have_fixed_entries = 0;
577 for (pin = 0; pin < 4; pin++) {
578 if (dev->pci_irq_info[pin].ioapic_dst_id) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700579 printk(BIOS_DEBUG,
580 "fixed IRQ entry for: %s: INT%c# -> IOAPIC %d PIN %d\n",
581 dev_path(dev),
Sven Schnelle8c027902012-08-20 11:19:00 +0200582 pin + 'A',
583 dev->pci_irq_info[pin].ioapic_dst_id,
584 dev->pci_irq_info[pin].ioapic_irq_pin);
585 smp_write_intsrc(mc, mp_INT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700586 dev->pci_irq_info[pin].ioapic_flags,
587 dev->bus->secondary,
588 ((dev->path.pci.devfn & 0xf8) >> 1)
589 | pin,
590 dev->pci_irq_info[pin].ioapic_dst_id,
591 dev->pci_irq_info[pin].ioapic_irq_pin);
Sven Schnelle8c027902012-08-20 11:19:00 +0200592 have_fixed_entries = 1;
593 }
594 }
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200595
Sven Schnelle8c027902012-08-20 11:19:00 +0200596 if (!have_fixed_entries) {
597 pin = (dev->path.pci.devfn & 7) % 4;
598 oldparent = parent = dev;
Lee Leahy024b13d2017-03-16 13:41:11 -0700599 while ((parent = parent->bus->dev)) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700600 parentpin = (oldparent->path.pci.devfn >> 3)
601 + (oldparent->path.pci.devfn & 7);
Sven Schnelle8c027902012-08-20 11:19:00 +0200602 parentpin += dev->path.pci.devfn & 7;
603 parentpin += dev->path.pci.devfn >> 3;
604 parentpin %= 4;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200605
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700606 pci_irq_info = &parent->pci_irq_info[parentpin];
607 if (pci_irq_info->ioapic_dst_id) {
608 printk(BIOS_DEBUG,
609 "automatic IRQ entry for %s: INT%c# -> IOAPIC %d PIN %d\n",
Sven Schnelle8c027902012-08-20 11:19:00 +0200610 dev_path(dev), pin + 'A',
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700611 pci_irq_info->ioapic_dst_id,
612 pci_irq_info->ioapic_irq_pin);
Sven Schnelle8c027902012-08-20 11:19:00 +0200613 smp_write_intsrc(mc, mp_INT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700614 pci_irq_info->ioapic_flags,
615 dev->bus->secondary,
616 ((dev->path.pci.devfn & 0xf8)
617 >> 1) | pin,
618 pci_irq_info->ioapic_dst_id,
619 pci_irq_info->ioapic_irq_pin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200620
Sven Schnelle8c027902012-08-20 11:19:00 +0200621 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200622 }
Sven Schnelle8c027902012-08-20 11:19:00 +0200623
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800624 if (parent->path.type == DEVICE_PATH_DOMAIN) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700625 printk(BIOS_WARNING,
626 "no IRQ found for %s\n",
627 dev_path(dev));
Sven Schnelle8c027902012-08-20 11:19:00 +0200628 break;
629 }
630 oldparent = parent;
631 }
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200632 }
633 }
634
635 mptable_lintsrc(mc, isa_bus);
636 tmp = mptable_finalize(mc);
Stefan Reinauer96938852015-06-18 01:23:48 -0700637 printk(BIOS_INFO, "MPTABLE len: %d\n", (unsigned int)((uintptr_t)tmp -
638 (uintptr_t)v));
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200639 return (unsigned long)tmp;
640}