blob: 2e65e252d522bf97ac17d77fd3e43dd0224e5b1a [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>
Kyösti Mälkki06b20492021-06-07 23:00:00 +03006#include <arch/ioapic.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00007#include <arch/smp/mpspec.h>
8#include <string.h>
Kyösti Mälkkidea42e02021-05-31 20:26:16 +03009#include <cpu/cpu.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000010#include <cpu/x86/lapic.h>
Sven Schnelle0fa50a12012-06-21 22:19:48 +020011#include <drivers/generic/ioapic/chip.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000012
Uwe Hermann55dc2232010-10-25 15:32:07 +000013/* Initialize the specified "mc" struct with initial values. */
Kyösti Mälkkidea42e02021-05-31 20:26:16 +030014void mptable_init(struct mp_config_table *mc)
Uwe Hermann55dc2232010-10-25 15:32:07 +000015{
Uwe Hermannc36d5062010-12-16 19:51:38 +000016 int i;
Kyösti Mälkkidea42e02021-05-31 20:26:16 +030017 u32 lapic_addr = cpu_get_lapic_addr();
Uwe Hermann55dc2232010-10-25 15:32:07 +000018
19 memset(mc, 0, sizeof(*mc));
Uwe Hermannc36d5062010-12-16 19:51:38 +000020
Uwe Hermann55dc2232010-10-25 15:32:07 +000021 memcpy(mc->mpc_signature, MPC_SIGNATURE, 4);
Uwe Hermannc36d5062010-12-16 19:51:38 +000022
Uwe Hermann55dc2232010-10-25 15:32:07 +000023 mc->mpc_length = sizeof(*mc); /* Initially just the header size. */
24 mc->mpc_spec = 0x04; /* MultiProcessor specification 1.4 */
25 mc->mpc_checksum = 0; /* Not yet computed. */
Uwe Hermann55dc2232010-10-25 15:32:07 +000026 mc->mpc_oemptr = 0;
27 mc->mpc_oemsize = 0;
28 mc->mpc_entry_count = 0; /* No entries yet... */
29 mc->mpc_lapic = lapic_addr;
30 mc->mpe_length = 0;
31 mc->mpe_checksum = 0;
32 mc->reserved = 0;
Uwe Hermannc36d5062010-12-16 19:51:38 +000033
34 strncpy(mc->mpc_oem, CONFIG_MAINBOARD_VENDOR, 8);
35 strncpy(mc->mpc_productid, CONFIG_MAINBOARD_PART_NUMBER, 12);
36
37 /*
38 * The oem/productid fields are exactly 8/12 bytes long. If the resp.
39 * entry is shorter, the remaining bytes are filled with spaces.
40 */
41 for (i = MIN(strlen(CONFIG_MAINBOARD_VENDOR), 8); i < 8; i++)
42 mc->mpc_oem[i] = ' ';
43 for (i = MIN(strlen(CONFIG_MAINBOARD_PART_NUMBER), 12); i < 12; i++)
44 mc->mpc_productid[i] = ' ';
Uwe Hermann55dc2232010-10-25 15:32:07 +000045}
46
Patrick Georgib0a9c5c2011-10-07 23:01:55 +020047static unsigned char smp_compute_checksum(void *v, int len)
Eric Biederman8ca8d762003-04-22 19:02:15 +000048{
49 unsigned char *bytes;
50 unsigned char checksum;
51 int i;
52 bytes = v;
53 checksum = 0;
Lee Leahy9c7c6f72017-03-16 11:24:09 -070054 for (i = 0; i < len; i++)
Eric Biederman8ca8d762003-04-22 19:02:15 +000055 checksum -= bytes[i];
Eric Biederman8ca8d762003-04-22 19:02:15 +000056 return checksum;
57}
58
Lee Leahy6f80ccc2017-03-16 15:18:22 -070059static void *smp_write_floating_table_physaddr(uintptr_t addr,
60 uintptr_t mpf_physptr, unsigned int virtualwire)
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000061{
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +100062 struct intel_mp_floating *mf;
63 void *v;
Stefan Reinauer14e22772010-04-27 06:56:47 +000064
Yinghai Luf327d9f2008-02-20 17:41:38 +000065 v = (void *)addr;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +100066 mf = v;
67 mf->mpf_signature[0] = '_';
68 mf->mpf_signature[1] = 'M';
69 mf->mpf_signature[2] = 'P';
70 mf->mpf_signature[3] = '_';
71 mf->mpf_physptr = mpf_physptr;
72 mf->mpf_length = 1;
73 mf->mpf_specification = 4;
74 mf->mpf_checksum = 0;
75 mf->mpf_feature1 = 0;
Mike Loptiend0167d32014-06-11 14:20:48 -060076 mf->mpf_feature2 = virtualwire?MP_FEATURE_PIC:MP_FEATURE_VIRTUALWIRE;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +100077 mf->mpf_feature3 = 0;
78 mf->mpf_feature4 = 0;
79 mf->mpf_feature5 = 0;
80 mf->mpf_checksum = smp_compute_checksum(mf, mf->mpf_length*16);
81 return v;
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000082}
83
Patrick Georgic75c79b2011-10-07 22:41:07 +020084void *smp_write_floating_table(unsigned long addr, unsigned int virtualwire)
85{
86 /* 16 byte align the table address */
87 addr = (addr + 0xf) & (~0xf);
Lee Leahy6f80ccc2017-03-16 15:18:22 -070088 return smp_write_floating_table_physaddr(addr, addr
89 + SMP_FLOATING_TABLE_LEN, virtualwire);
Patrick Georgic75c79b2011-10-07 22:41:07 +020090}
91
Eric Biederman8ca8d762003-04-22 19:02:15 +000092void *smp_next_mpc_entry(struct mp_config_table *mc)
93{
94 void *v;
95 v = (void *)(((char *)mc) + mc->mpc_length);
Mike Loptienbd4553b2014-06-16 10:46:56 -060096
Eric Biederman8ca8d762003-04-22 19:02:15 +000097 return v;
98}
Mike Loptienbd4553b2014-06-16 10:46:56 -060099static void smp_add_mpc_entry(struct mp_config_table *mc, u16 length)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000100{
101 mc->mpc_length += length;
102 mc->mpc_entry_count++;
103}
104
105void *smp_next_mpe_entry(struct mp_config_table *mc)
106{
107 void *v;
108 v = (void *)(((char *)mc) + mc->mpc_length + mc->mpe_length);
Mike Loptienbd4553b2014-06-16 10:46:56 -0600109
Eric Biederman8ca8d762003-04-22 19:02:15 +0000110 return v;
111}
112static void smp_add_mpe_entry(struct mp_config_table *mc, mpe_t mpe)
113{
114 mc->mpe_length += mpe->mpe_length;
115}
116
Mike Loptienbd4553b2014-06-16 10:46:56 -0600117/*
118 * Type 0: Processor Entries:
119 * Entry Type, LAPIC ID, LAPIC Version, CPU Flags EN/BP,
120 * CPU Signature (Stepping, Model, Family), Feature Flags
121 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122void smp_write_processor(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600123 u8 apicid, u8 apicver, u8 cpuflag,
124 u32 cpufeature, u32 featureflag)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000125{
126 struct mpc_config_processor *mpc;
127 mpc = smp_next_mpc_entry(mc);
128 memset(mpc, '\0', sizeof(*mpc));
129 mpc->mpc_type = MP_PROCESSOR;
130 mpc->mpc_apicid = apicid;
131 mpc->mpc_apicver = apicver;
132 mpc->mpc_cpuflag = cpuflag;
133 mpc->mpc_cpufeature = cpufeature;
134 mpc->mpc_featureflag = featureflag;
135 smp_add_mpc_entry(mc, sizeof(*mpc));
136}
137
Mike Loptienbd4553b2014-06-16 10:46:56 -0600138/*
139 * If we assume a symmetric processor configuration we can
Eric Biederman8ca8d762003-04-22 19:02:15 +0000140 * get all of the information we need to write the processor
141 * entry from the bootstrap processor.
142 * Plus I don't think linux really even cares.
143 * Having the proper apicid's in the table so the non-bootstrap
144 * processors can be woken up should be enough.
145 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000146void smp_write_processors(struct mp_config_table *mc)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000147{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000148 int boot_apic_id;
Patrick Georgi07b42152011-10-14 23:02:57 +0200149 int order_id;
Lee Leahye5f29e82017-03-16 14:16:56 -0700150 unsigned int apic_version;
151 unsigned int cpu_features;
152 unsigned int cpu_feature_flags;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100153 struct device *cpu;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000154
Eric Biedermanb78c1972004-10-14 20:54:17 +0000155 boot_apic_id = lapicid();
156 apic_version = lapic_read(LAPIC_LVR) & 0xff;
Subrata Banik53b08c32018-12-10 14:11:35 +0530157 cpu_features = cpu_get_cpuid();
158 cpu_feature_flags = cpu_get_feature_flags_edx();
Patrick Georgi07b42152011-10-14 23:02:57 +0200159 /* order the output of the cpus to fix a bug in kernel 2.6.11 */
Lee Leahy024b13d2017-03-16 13:41:11 -0700160 for (order_id = 0; order_id < 256; order_id++) {
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200161 for (cpu = all_devices; cpu; cpu = cpu->next) {
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000162 unsigned long cpu_flag;
Fabio Aiuto45aae7f2022-09-23 16:51:34 +0200163 if (!is_enabled_cpu(cpu))
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000164 continue;
165
166 cpu_flag = MPC_CPU_ENABLED;
167
168 if (boot_apic_id == cpu->path.apic.apic_id)
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700169 cpu_flag = MPC_CPU_ENABLED
170 | MPC_CPU_BOOTPROCESSOR;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000171
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200172 if (cpu->path.apic.apic_id == order_id) {
Mike Loptienbd4553b2014-06-16 10:46:56 -0600173 smp_write_processor(mc,
174 cpu->path.apic.apic_id, apic_version,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700175 cpu_flag, cpu_features,
176 cpu_feature_flags
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000177 );
178 break;
179 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181 }
182}
183
Mike Loptienbd4553b2014-06-16 10:46:56 -0600184/*
185 * Type 1: Bus Entries:
186 * Entry Type, Bus ID, Bus Type
187 */
Patrick Georgi patrick612fcc32010-11-23 07:19:54 +0000188static void smp_write_bus(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600189 u8 id, const char *bustype)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000190{
191 struct mpc_config_bus *mpc;
192 mpc = smp_next_mpc_entry(mc);
193 memset(mpc, '\0', sizeof(*mpc));
194 mpc->mpc_type = MP_BUS;
195 mpc->mpc_busid = id;
196 memcpy(mpc->mpc_bustype, bustype, sizeof(mpc->mpc_bustype));
197 smp_add_mpc_entry(mc, sizeof(*mpc));
198}
199
Mike Loptienbd4553b2014-06-16 10:46:56 -0600200/*
201 * Type 2: I/O APIC Entries:
202 * Entry Type, APIC ID, Version,
203 * APIC Flags:EN, Address
204 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000205void smp_write_ioapic(struct mp_config_table *mc,
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800206 u8 id, u8 ver, void *apicaddr)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000207{
208 struct mpc_config_ioapic *mpc;
209 mpc = smp_next_mpc_entry(mc);
210 memset(mpc, '\0', sizeof(*mpc));
211 mpc->mpc_type = MP_IOAPIC;
212 mpc->mpc_apicid = id;
213 mpc->mpc_apicver = ver;
214 mpc->mpc_flags = MPC_APIC_USABLE;
215 mpc->mpc_apicaddr = apicaddr;
216 smp_add_mpc_entry(mc, sizeof(*mpc));
217}
218
Kyösti Mälkki06b20492021-06-07 23:00:00 +0300219u8 smp_write_ioapic_from_hw(struct mp_config_table *mc, void *apicaddr)
220{
221 u8 id = get_ioapic_id(apicaddr);
222 u8 ver = get_ioapic_version(apicaddr);
223 smp_write_ioapic(mc, id, ver, apicaddr);
224 return id;
225}
226
Mike Loptienbd4553b2014-06-16 10:46:56 -0600227/*
228 * Type 3: I/O Interrupt Table Entries:
229 * Entry Type, Int Type, Int Polarity, Int Level,
230 * Source Bus ID, Source Bus IRQ, Dest APIC ID, Dest PIN#
231 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000232void smp_write_intsrc(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600233 u8 irqtype, u16 irqflag,
234 u8 srcbus, u8 srcbusirq,
235 u8 dstapic, u8 dstirq)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000236{
237 struct mpc_config_intsrc *mpc;
238 mpc = smp_next_mpc_entry(mc);
239 memset(mpc, '\0', sizeof(*mpc));
240 mpc->mpc_type = MP_INTSRC;
241 mpc->mpc_irqtype = irqtype;
242 mpc->mpc_irqflag = irqflag;
243 mpc->mpc_srcbus = srcbus;
244 mpc->mpc_srcbusirq = srcbusirq;
245 mpc->mpc_dstapic = dstapic;
246 mpc->mpc_dstirq = dstirq;
247 smp_add_mpc_entry(mc, sizeof(*mpc));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000248}
249
Mike Loptienbd4553b2014-06-16 10:46:56 -0600250/*
251 * Type 3: I/O Interrupt Table Entries for PCI Devices:
252 * This has the same fields as 'Type 3: I/O Interrupt Table Entries'
253 * but the Source Bus IRQ field has a slightly different
254 * definition:
255 * Bits 1-0: PIRQ pin: INT_A# = 0, INT_B# = 1, INT_C# = 2, INT_D# = 3
256 * Bits 2-6: Originating PCI Device Number (Not its parent bridge device number)
257 * Bit 7: Reserved
258 */
259void smp_write_pci_intsrc(struct mp_config_table *mc,
260 u8 irqtype, u8 srcbus, u8 dev, u8 pirq,
261 u8 dstapic, u8 dstirq)
262{
263 u8 srcbusirq = (dev << 2) | pirq;
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700264 printk(BIOS_SPEW,
265 "\tPCI srcbusirq = 0x%x from dev = 0x%x and pirq = %x\n",
266 srcbusirq, dev, pirq);
267 smp_write_intsrc(mc, irqtype, MP_IRQ_TRIGGER_LEVEL
268 | MP_IRQ_POLARITY_LOW, srcbus, srcbusirq, dstapic, dstirq);
Mike Loptienbd4553b2014-06-16 10:46:56 -0600269}
270
Yinghai Luf327d9f2008-02-20 17:41:38 +0000271void smp_write_intsrc_pci_bridge(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600272 u8 irqtype, u16 irqflag, struct device *dev,
Yinghai Luf327d9f2008-02-20 17:41:38 +0000273 unsigned char dstapic, unsigned char *dstirq)
274{
275 struct device *child;
276
Yinghai Luf327d9f2008-02-20 17:41:38 +0000277 int i;
278 int srcbus;
279 int slot;
280
281 struct bus *link;
282 unsigned char dstirq_x[4];
283
Myles Watson894a3472010-06-09 22:41:35 +0000284 for (link = dev->link_list; link; link = link->next) {
Yinghai Luf327d9f2008-02-20 17:41:38 +0000285
Yinghai Luf327d9f2008-02-20 17:41:38 +0000286 child = link->children;
287 srcbus = link->secondary;
288
289 while (child) {
290 if (child->path.type != DEVICE_PATH_PCI)
291 goto next;
292
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000293 slot = (child->path.pci.devfn >> 3);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000294 /* round pins */
295 for (i = 0; i < 4; i++)
296 dstirq_x[i] = dstirq[(i + slot) % 4];
297
298 if ((child->class >> 16) != PCI_BASE_CLASS_BRIDGE) {
299 /* pci device */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700300 printk(BIOS_DEBUG, "route irq: %s\n",
301 dev_path(child));
Yinghai Luf327d9f2008-02-20 17:41:38 +0000302 for (i = 0; i < 4; i++)
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700303 smp_write_intsrc(mc, irqtype, irqflag,
304 srcbus, (slot<<2)|i, dstapic,
305 dstirq_x[i]);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000306 goto next;
307 }
308
309 switch (child->class>>8) {
310 case PCI_CLASS_BRIDGE_PCI:
311 case PCI_CLASS_BRIDGE_PCMCIA:
312 case PCI_CLASS_BRIDGE_CARDBUS:
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700313 printk(BIOS_DEBUG, "route irq bridge: %s\n",
314 dev_path(child));
315 smp_write_intsrc_pci_bridge(mc, irqtype,
316 irqflag, child, dstapic, dstirq_x);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000317 }
318
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100319next:
Yinghai Luf327d9f2008-02-20 17:41:38 +0000320 child = child->sibling;
321 }
322
323 }
324}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000325
Mike Loptienbd4553b2014-06-16 10:46:56 -0600326/*
327 * Type 4: Local Interrupt Assignment Entries:
328 * Entry Type, Int Type, Int Polarity, Int Level,
329 * Source Bus ID, Source Bus IRQ, Dest LAPIC ID,
330 * Dest LAPIC LINTIN#
331 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000332void smp_write_lintsrc(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600333 u8 irqtype, u16 irqflag,
334 u8 srcbusid, u8 srcbusirq,
335 u8 destapic, u8 destapiclint)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000336{
337 struct mpc_config_lintsrc *mpc;
338 mpc = smp_next_mpc_entry(mc);
339 memset(mpc, '\0', sizeof(*mpc));
340 mpc->mpc_type = MP_LINTSRC;
341 mpc->mpc_irqtype = irqtype;
342 mpc->mpc_irqflag = irqflag;
343 mpc->mpc_srcbusid = srcbusid;
344 mpc->mpc_srcbusirq = srcbusirq;
345 mpc->mpc_destapic = destapic;
346 mpc->mpc_destapiclint = destapiclint;
347 smp_add_mpc_entry(mc, sizeof(*mpc));
348}
349
Mike Loptienbd4553b2014-06-16 10:46:56 -0600350/*
351 * Type 128: System Address Space Mapping Entries
352 * Entry Type, Entry Length, Bus ID, Address Type,
353 * Address Base Lo/Hi, Address Length Lo/Hi
354 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000355void smp_write_address_space(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600356 u8 busid, u8 address_type,
357 u32 address_base_low, u32 address_base_high,
358 u32 address_length_low, u32 address_length_high)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000359{
360 struct mp_exten_system_address_space *mpe;
361 mpe = smp_next_mpe_entry(mc);
362 memset(mpe, '\0', sizeof(*mpe));
363 mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE;
364 mpe->mpe_length = sizeof(*mpe);
365 mpe->mpe_busid = busid;
366 mpe->mpe_address_type = address_type;
367 mpe->mpe_address_base_low = address_base_low;
368 mpe->mpe_address_base_high = address_base_high;
369 mpe->mpe_address_length_low = address_length_low;
370 mpe->mpe_address_length_high = address_length_high;
371 smp_add_mpe_entry(mc, (mpe_t)mpe);
372}
373
Mike Loptienbd4553b2014-06-16 10:46:56 -0600374/*
375 * Type 129: Bus Hierarchy Descriptor Entry
376 * Entry Type, Entry Length, Bus ID, Bus Info,
377 * Parent Bus ID
378 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000379void smp_write_bus_hierarchy(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600380 u8 busid, u8 bus_info, u8 parent_busid)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000381{
382 struct mp_exten_bus_hierarchy *mpe;
383 mpe = smp_next_mpe_entry(mc);
384 memset(mpe, '\0', sizeof(*mpe));
385 mpe->mpe_type = MPE_BUS_HIERARCHY;
386 mpe->mpe_length = sizeof(*mpe);
387 mpe->mpe_busid = busid;
388 mpe->mpe_bus_info = bus_info;
389 mpe->mpe_parent_busid = parent_busid;
390 smp_add_mpe_entry(mc, (mpe_t)mpe);
391}
392
Mike Loptienbd4553b2014-06-16 10:46:56 -0600393/*
394 * Type 130: Compatibility Bus Address Space Modifier Entry
395 * Entry Type, Entry Length, Bus ID, Address Modifier
396 * Predefined Range List
397 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000398void smp_write_compatibility_address_space(struct mp_config_table *mc,
Mike Loptienbd4553b2014-06-16 10:46:56 -0600399 u8 busid, u8 address_modifier,
400 u32 range_list)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000401{
402 struct mp_exten_compatibility_address_space *mpe;
403 mpe = smp_next_mpe_entry(mc);
404 memset(mpe, '\0', sizeof(*mpe));
405 mpe->mpe_type = MPE_COMPATIBILITY_ADDRESS_SPACE;
406 mpe->mpe_length = sizeof(*mpe);
407 mpe->mpe_busid = busid;
408 mpe->mpe_address_modifier = address_modifier;
409 mpe->mpe_range_list = range_list;
410 smp_add_mpe_entry(mc, (mpe_t)mpe);
411}
412
Patrick Georgi6eb7a532011-10-07 21:42:52 +0200413void mptable_lintsrc(struct mp_config_table *mc, unsigned long bus_isa)
414{
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700415 smp_write_lintsrc(mc, mp_ExtINT, MP_IRQ_TRIGGER_EDGE
416 | MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x0);
417 smp_write_lintsrc(mc, mp_NMI, MP_IRQ_TRIGGER_EDGE
418 | MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x1);
Patrick Georgi6eb7a532011-10-07 21:42:52 +0200419}
420
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700421void mptable_add_isa_interrupts(struct mp_config_table *mc,
422 unsigned long bus_isa, unsigned long apicid, int external_int2)
Patrick Georgic5b87c82010-05-20 15:28:19 +0000423{
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700424/*I/O Ints: Type Trigger Polarity
425 * Bus ID IRQ APIC ID PIN# */
Patrick Georgic5b87c82010-05-20 15:28:19 +0000426 smp_write_intsrc(mc, external_int2?mp_INT:mp_ExtINT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700427 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
428 bus_isa, 0x0, apicid, 0x0);
429 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
430 bus_isa, 0x1, apicid, 0x1);
Patrick Georgic5b87c82010-05-20 15:28:19 +0000431 smp_write_intsrc(mc, external_int2?mp_ExtINT:mp_INT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700432 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
433 bus_isa, 0x0, apicid, 0x2);
434 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
435 bus_isa, 0x3, apicid, 0x3);
436 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
437 bus_isa, 0x4, apicid, 0x4);
438 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
439 bus_isa, 0x6, apicid, 0x6);
440 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
441 bus_isa, 0x7, apicid, 0x7);
442 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
443 bus_isa, 0x8, apicid, 0x8);
444 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
445 bus_isa, 0x9, apicid, 0x9);
446 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
447 bus_isa, 0xa, apicid, 0xa);
448 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
449 bus_isa, 0xb, apicid, 0xb);
450 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
451 bus_isa, 0xc, apicid, 0xc);
452 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
453 bus_isa, 0xd, apicid, 0xd);
454 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
455 bus_isa, 0xe, apicid, 0xe);
456 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
457 bus_isa, 0xf, apicid, 0xf);
Patrick Georgic5b87c82010-05-20 15:28:19 +0000458}
Patrick Georgiab272662010-06-25 13:43:22 +0000459
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700460void mptable_write_buses(struct mp_config_table *mc, int *max_pci_bus,
461 int *isa_bus)
462{
Patrick Georgiab272662010-06-25 13:43:22 +0000463 int dummy, i, highest;
464 char buses[256];
465 struct device *dev;
466
Lee Leahy0b5678f2017-03-16 16:01:40 -0700467 if (!max_pci_bus)
468 max_pci_bus = &dummy;
469 if (!isa_bus)
470 isa_bus = &dummy;
Patrick Georgiab272662010-06-25 13:43:22 +0000471
472 *max_pci_bus = 0;
473 highest = 0;
474 memset(buses, 0, sizeof(buses));
475
476 for (dev = all_devices; dev; dev = dev->next) {
477 struct bus *bus;
478 for (bus = dev->link_list; bus; bus = bus->next) {
479 if (bus->secondary > 255) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700480 printk(BIOS_ERR,
481 "A bus claims to have a bus ID > 255?!? Aborting");
Patrick Georgiab272662010-06-25 13:43:22 +0000482 return;
483 }
484 buses[bus->secondary] = 1;
Lee Leahy0b5678f2017-03-16 16:01:40 -0700485 if (highest < bus->secondary)
486 highest = bus->secondary;
Patrick Georgiab272662010-06-25 13:43:22 +0000487 }
488 }
Lee Leahy024b13d2017-03-16 13:41:11 -0700489 for (i = 0; i <= highest; i++) {
Patrick Georgiab272662010-06-25 13:43:22 +0000490 if (buses[i]) {
491 smp_write_bus(mc, i, "PCI ");
492 *max_pci_bus = i;
493 }
494 }
495 *isa_bus = *max_pci_bus + 1;
496 smp_write_bus(mc, *isa_bus, "ISA ");
497}
498
Patrick Georgib0a9c5c2011-10-07 23:01:55 +0200499void *mptable_finalize(struct mp_config_table *mc)
500{
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700501 mc->mpe_checksum = smp_compute_checksum(smp_next_mpc_entry(mc),
502 mc->mpe_length);
Patrick Georgib0a9c5c2011-10-07 23:01:55 +0200503 mc->mpc_checksum = smp_compute_checksum(mc, mc->mpc_length);
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700504 printk(BIOS_DEBUG, "Wrote the mp table end at: %p - %p\n",
505 mc, smp_next_mpe_entry(mc));
Patrick Georgib0a9c5c2011-10-07 23:01:55 +0200506 return smp_next_mpe_entry(mc);
507}
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200508
Lubomir Rintel294446a2018-04-14 11:48:27 +0200509static const struct device *find_next_ioapic(unsigned int last_ioapic_id)
510{
511 const struct device *dev;
512 const struct device *result = NULL;
513 unsigned int ioapic_id = MAX_APICS;
514
515 for (dev = all_devices; dev; dev = dev->next) {
516 if (dev->path.type == DEVICE_PATH_IOAPIC &&
517 dev->path.ioapic.ioapic_id > last_ioapic_id &&
518 dev->path.ioapic.ioapic_id <= ioapic_id) {
519 result = dev;
520 }
521 }
522 return result;
523}
524
Aaron Durbin64031672018-04-21 14:45:32 -0600525unsigned long __weak write_smp_table(unsigned long addr)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200526{
527 struct drivers_generic_ioapic_config *ioapic_config;
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000528 struct mp_config_table *mc;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200529 int isa_bus, pin, parentpin;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200530 const struct device *dev;
531 const struct device *parent;
532 const struct device *oldparent;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200533 void *tmp, *v;
Sven Schnelle8c027902012-08-20 11:19:00 +0200534 int isaioapic = -1, have_fixed_entries;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200535 const struct pci_irq_info *pci_irq_info;
Lubomir Rintel294446a2018-04-14 11:48:27 +0200536 unsigned int ioapic_id = 0;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200537
538 v = smp_write_floating_table(addr, 0);
Edward O'Callaghan0bb0aff2014-05-02 03:58:32 +1000539 mc = (void *)(((char *)v) + SMP_FLOATING_TABLE_LEN);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200540
Kyösti Mälkkidea42e02021-05-31 20:26:16 +0300541 mptable_init(mc);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200542
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500543 smp_write_processors(mc);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200544
545 mptable_write_buses(mc, NULL, &isa_bus);
546
Lubomir Rintel294446a2018-04-14 11:48:27 +0200547 while ((dev = find_next_ioapic(ioapic_id))) {
Lee Leahy0b5678f2017-03-16 16:01:40 -0700548 ioapic_config = dev->chip_info;
549 if (!ioapic_config) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700550 printk(BIOS_ERR, "%s has no config, ignoring\n",
551 dev_path(dev));
Lubomir Rintel294446a2018-04-14 11:48:27 +0200552 ioapic_id++;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200553 continue;
554 }
Lubomir Rintel294446a2018-04-14 11:48:27 +0200555
556 ioapic_id = dev->path.ioapic.ioapic_id;
557 smp_write_ioapic(mc, ioapic_id,
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200558 ioapic_config->version,
559 ioapic_config->base);
560
561 if (ioapic_config->have_isa_interrupts) {
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500562 if (isaioapic >= 0)
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700563 printk(BIOS_ERR,
564 "More than one IOAPIC with ISA interrupts?\n");
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200565 else
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500566 isaioapic = dev->path.ioapic.ioapic_id;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200567 }
568 }
569
570 if (isaioapic >= 0) {
571 /* Legacy Interrupts */
Alexandru Gagniuc0d5d70b2012-08-21 17:08:03 -0500572 printk(BIOS_DEBUG, "Writing ISA IRQs\n");
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200573 mptable_add_isa_interrupts(mc, isa_bus, isaioapic, 0);
574 }
575
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200576 for (dev = all_devices; dev; dev = dev->next) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200577
Sven Schnelle64c40dd2012-08-20 11:17:52 +0200578 if (dev->path.type != DEVICE_PATH_PCI || !dev->enabled)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200579 continue;
580
Sven Schnelle8c027902012-08-20 11:19:00 +0200581 have_fixed_entries = 0;
582 for (pin = 0; pin < 4; pin++) {
583 if (dev->pci_irq_info[pin].ioapic_dst_id) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700584 printk(BIOS_DEBUG,
585 "fixed IRQ entry for: %s: INT%c# -> IOAPIC %d PIN %d\n",
586 dev_path(dev),
Sven Schnelle8c027902012-08-20 11:19:00 +0200587 pin + 'A',
588 dev->pci_irq_info[pin].ioapic_dst_id,
589 dev->pci_irq_info[pin].ioapic_irq_pin);
590 smp_write_intsrc(mc, mp_INT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700591 dev->pci_irq_info[pin].ioapic_flags,
592 dev->bus->secondary,
593 ((dev->path.pci.devfn & 0xf8) >> 1)
594 | pin,
595 dev->pci_irq_info[pin].ioapic_dst_id,
596 dev->pci_irq_info[pin].ioapic_irq_pin);
Sven Schnelle8c027902012-08-20 11:19:00 +0200597 have_fixed_entries = 1;
598 }
599 }
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200600
Sven Schnelle8c027902012-08-20 11:19:00 +0200601 if (!have_fixed_entries) {
602 pin = (dev->path.pci.devfn & 7) % 4;
603 oldparent = parent = dev;
Lee Leahy024b13d2017-03-16 13:41:11 -0700604 while ((parent = parent->bus->dev)) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700605 parentpin = (oldparent->path.pci.devfn >> 3)
606 + (oldparent->path.pci.devfn & 7);
Sven Schnelle8c027902012-08-20 11:19:00 +0200607 parentpin += dev->path.pci.devfn & 7;
608 parentpin += dev->path.pci.devfn >> 3;
609 parentpin %= 4;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200610
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700611 pci_irq_info = &parent->pci_irq_info[parentpin];
612 if (pci_irq_info->ioapic_dst_id) {
613 printk(BIOS_DEBUG,
614 "automatic IRQ entry for %s: INT%c# -> IOAPIC %d PIN %d\n",
Sven Schnelle8c027902012-08-20 11:19:00 +0200615 dev_path(dev), pin + 'A',
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700616 pci_irq_info->ioapic_dst_id,
617 pci_irq_info->ioapic_irq_pin);
Sven Schnelle8c027902012-08-20 11:19:00 +0200618 smp_write_intsrc(mc, mp_INT,
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700619 pci_irq_info->ioapic_flags,
620 dev->bus->secondary,
621 ((dev->path.pci.devfn & 0xf8)
622 >> 1) | pin,
623 pci_irq_info->ioapic_dst_id,
624 pci_irq_info->ioapic_irq_pin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200625
Sven Schnelle8c027902012-08-20 11:19:00 +0200626 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200627 }
Sven Schnelle8c027902012-08-20 11:19:00 +0200628
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800629 if (parent->path.type == DEVICE_PATH_DOMAIN) {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700630 printk(BIOS_WARNING,
631 "no IRQ found for %s\n",
632 dev_path(dev));
Sven Schnelle8c027902012-08-20 11:19:00 +0200633 break;
634 }
635 oldparent = parent;
636 }
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200637 }
638 }
639
640 mptable_lintsrc(mc, isa_bus);
641 tmp = mptable_finalize(mc);
Stefan Reinauer96938852015-06-18 01:23:48 -0700642 printk(BIOS_INFO, "MPTABLE len: %d\n", (unsigned int)((uintptr_t)tmp -
643 (uintptr_t)v));
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200644 return (unsigned long)tmp;
645}