blob: 8e97d9ff36d93fe280c027135b8a9bcc5af1c056 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Mengf91cf6b2015-06-23 12:18:51 +08002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 *
5 * Adapted from coreboot src/arch/x86/boot/mpspec.c
Bin Mengf91cf6b2015-06-23 12:18:51 +08006 */
7
8#include <common.h>
9#include <cpu.h>
10#include <dm.h>
Bin Mengc4f407e2015-06-23 12:18:52 +080011#include <errno.h>
12#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Bin Mengf91cf6b2015-06-23 12:18:51 +080014#include <asm/cpu.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Bin Mengc4f407e2015-06-23 12:18:52 +080016#include <asm/irq.h>
Bin Mengf91cf6b2015-06-23 12:18:51 +080017#include <asm/ioapic.h>
18#include <asm/lapic.h>
19#include <asm/mpspec.h>
20#include <asm/tables.h>
21#include <dm/uclass-internal.h>
22
Bin Mengc4f407e2015-06-23 12:18:52 +080023DECLARE_GLOBAL_DATA_PTR;
24
Bin Meng95882f02015-07-22 01:21:10 -070025static bool isa_irq_occupied[16];
26
Bin Mengf91cf6b2015-06-23 12:18:51 +080027struct mp_config_table *mp_write_floating_table(struct mp_floating_table *mf)
28{
Simon Glass39473e12017-01-16 07:03:36 -070029 ulong mc;
Bin Mengf91cf6b2015-06-23 12:18:51 +080030
31 memcpy(mf->mpf_signature, MPF_SIGNATURE, 4);
Simon Glass39473e12017-01-16 07:03:36 -070032 mf->mpf_physptr = (ulong)mf + sizeof(struct mp_floating_table);
Bin Mengf91cf6b2015-06-23 12:18:51 +080033 mf->mpf_length = 1;
34 mf->mpf_spec = MPSPEC_V14;
35 mf->mpf_checksum = 0;
36 /* We don't use the default configuration table */
37 mf->mpf_feature1 = 0;
38 /* Indicate that virtual wire mode is always implemented */
39 mf->mpf_feature2 = 0;
40 mf->mpf_feature3 = 0;
41 mf->mpf_feature4 = 0;
42 mf->mpf_feature5 = 0;
43 mf->mpf_checksum = table_compute_checksum(mf, mf->mpf_length * 16);
44
Simon Glass39473e12017-01-16 07:03:36 -070045 mc = (ulong)mf + sizeof(struct mp_floating_table);
Bin Mengf91cf6b2015-06-23 12:18:51 +080046 return (struct mp_config_table *)mc;
47}
48
49void mp_config_table_init(struct mp_config_table *mc)
50{
51 memcpy(mc->mpc_signature, MPC_SIGNATURE, 4);
52 mc->mpc_length = sizeof(struct mp_config_table);
53 mc->mpc_spec = MPSPEC_V14;
54 mc->mpc_checksum = 0;
55 mc->mpc_oemptr = 0;
56 mc->mpc_oemsize = 0;
57 mc->mpc_entry_count = 0;
58 mc->mpc_lapic = LAPIC_DEFAULT_BASE;
59 mc->mpe_length = 0;
60 mc->mpe_checksum = 0;
61 mc->reserved = 0;
62
63 /* The oem/product id fields are exactly 8/12 bytes long */
64 table_fill_string(mc->mpc_oem, CONFIG_SYS_VENDOR, 8, ' ');
65 table_fill_string(mc->mpc_product, CONFIG_SYS_BOARD, 12, ' ');
66}
67
68void mp_write_processor(struct mp_config_table *mc)
69{
70 struct mpc_config_processor *mpc;
71 struct udevice *dev;
72 u8 boot_apicid, apicver;
73 u32 cpusignature, cpufeature;
74 struct cpuid_result result;
75
76 boot_apicid = lapicid();
77 apicver = lapic_read(LAPIC_LVR) & 0xff;
78 result = cpuid(1);
79 cpusignature = result.eax;
80 cpufeature = result.edx;
81
82 for (uclass_find_first_device(UCLASS_CPU, &dev);
83 dev;
84 uclass_find_next_device(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -070085 struct cpu_plat *plat = dev_get_parent_plat(dev);
Bin Mengf91cf6b2015-06-23 12:18:51 +080086 u8 cpuflag = MPC_CPU_EN;
87
88 if (!device_active(dev))
89 continue;
90
91 mpc = (struct mpc_config_processor *)mp_next_mpc_entry(mc);
92 mpc->mpc_type = MP_PROCESSOR;
93 mpc->mpc_apicid = plat->cpu_id;
94 mpc->mpc_apicver = apicver;
95 if (boot_apicid == plat->cpu_id)
96 cpuflag |= MPC_CPU_BP;
97 mpc->mpc_cpuflag = cpuflag;
98 mpc->mpc_cpusignature = cpusignature;
99 mpc->mpc_cpufeature = cpufeature;
100 mpc->mpc_reserved[0] = 0;
101 mpc->mpc_reserved[1] = 0;
102 mp_add_mpc_entry(mc, sizeof(*mpc));
103 }
104}
105
106void mp_write_bus(struct mp_config_table *mc, int id, const char *bustype)
107{
108 struct mpc_config_bus *mpc;
109
110 mpc = (struct mpc_config_bus *)mp_next_mpc_entry(mc);
111 mpc->mpc_type = MP_BUS;
112 mpc->mpc_busid = id;
113 memcpy(mpc->mpc_bustype, bustype, 6);
114 mp_add_mpc_entry(mc, sizeof(*mpc));
115}
116
117void mp_write_ioapic(struct mp_config_table *mc, int id, int ver, u32 apicaddr)
118{
119 struct mpc_config_ioapic *mpc;
120
121 mpc = (struct mpc_config_ioapic *)mp_next_mpc_entry(mc);
122 mpc->mpc_type = MP_IOAPIC;
123 mpc->mpc_apicid = id;
124 mpc->mpc_apicver = ver;
125 mpc->mpc_flags = MPC_APIC_USABLE;
126 mpc->mpc_apicaddr = apicaddr;
127 mp_add_mpc_entry(mc, sizeof(*mpc));
128}
129
130void mp_write_intsrc(struct mp_config_table *mc, int irqtype, int irqflag,
131 int srcbus, int srcbusirq, int dstapic, int dstirq)
132{
133 struct mpc_config_intsrc *mpc;
134
135 mpc = (struct mpc_config_intsrc *)mp_next_mpc_entry(mc);
136 mpc->mpc_type = MP_INTSRC;
137 mpc->mpc_irqtype = irqtype;
138 mpc->mpc_irqflag = irqflag;
139 mpc->mpc_srcbus = srcbus;
140 mpc->mpc_srcbusirq = srcbusirq;
141 mpc->mpc_dstapic = dstapic;
142 mpc->mpc_dstirq = dstirq;
143 mp_add_mpc_entry(mc, sizeof(*mpc));
144}
145
146void mp_write_pci_intsrc(struct mp_config_table *mc, int irqtype,
147 int srcbus, int dev, int pin, int dstapic, int dstirq)
148{
149 u8 srcbusirq = (dev << 2) | (pin - 1);
150
151 mp_write_intsrc(mc, irqtype, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW,
152 srcbus, srcbusirq, dstapic, dstirq);
153}
154
155void mp_write_lintsrc(struct mp_config_table *mc, int irqtype, int irqflag,
156 int srcbus, int srcbusirq, int destapic, int destlint)
157{
158 struct mpc_config_lintsrc *mpc;
159
160 mpc = (struct mpc_config_lintsrc *)mp_next_mpc_entry(mc);
161 mpc->mpc_type = MP_LINTSRC;
162 mpc->mpc_irqtype = irqtype;
163 mpc->mpc_irqflag = irqflag;
164 mpc->mpc_srcbusid = srcbus;
165 mpc->mpc_srcbusirq = srcbusirq;
166 mpc->mpc_destapic = destapic;
167 mpc->mpc_destlint = destlint;
168 mp_add_mpc_entry(mc, sizeof(*mpc));
169}
170
171void mp_write_address_space(struct mp_config_table *mc,
172 int busid, int addr_type,
173 u32 addr_base_low, u32 addr_base_high,
174 u32 addr_length_low, u32 addr_length_high)
175{
176 struct mp_ext_system_address_space *mpe;
177
178 mpe = (struct mp_ext_system_address_space *)mp_next_mpe_entry(mc);
179 mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE;
180 mpe->mpe_length = sizeof(*mpe);
181 mpe->mpe_busid = busid;
182 mpe->mpe_addr_type = addr_type;
183 mpe->mpe_addr_base_low = addr_base_low;
184 mpe->mpe_addr_base_high = addr_base_high;
185 mpe->mpe_addr_length_low = addr_length_low;
186 mpe->mpe_addr_length_high = addr_length_high;
187 mp_add_mpe_entry(mc, (struct mp_ext_config *)mpe);
188}
189
190void mp_write_bus_hierarchy(struct mp_config_table *mc,
191 int busid, int bus_info, int parent_busid)
192{
193 struct mp_ext_bus_hierarchy *mpe;
194
195 mpe = (struct mp_ext_bus_hierarchy *)mp_next_mpe_entry(mc);
196 mpe->mpe_type = MPE_BUS_HIERARCHY;
197 mpe->mpe_length = sizeof(*mpe);
198 mpe->mpe_busid = busid;
199 mpe->mpe_bus_info = bus_info;
200 mpe->mpe_parent_busid = parent_busid;
201 mpe->reserved[0] = 0;
202 mpe->reserved[1] = 0;
203 mpe->reserved[2] = 0;
204 mp_add_mpe_entry(mc, (struct mp_ext_config *)mpe);
205}
206
207void mp_write_compat_address_space(struct mp_config_table *mc, int busid,
208 int addr_modifier, u32 range_list)
209{
210 struct mp_ext_compat_address_space *mpe;
211
212 mpe = (struct mp_ext_compat_address_space *)mp_next_mpe_entry(mc);
213 mpe->mpe_type = MPE_COMPAT_ADDRESS_SPACE;
214 mpe->mpe_length = sizeof(*mpe);
215 mpe->mpe_busid = busid;
216 mpe->mpe_addr_modifier = addr_modifier;
217 mpe->mpe_range_list = range_list;
218 mp_add_mpe_entry(mc, (struct mp_ext_config *)mpe);
219}
220
221u32 mptable_finalize(struct mp_config_table *mc)
222{
Simon Glass39473e12017-01-16 07:03:36 -0700223 ulong end;
Bin Mengf91cf6b2015-06-23 12:18:51 +0800224
225 mc->mpe_checksum = table_compute_checksum((void *)mp_next_mpc_entry(mc),
226 mc->mpe_length);
227 mc->mpc_checksum = table_compute_checksum(mc, mc->mpc_length);
228 end = mp_next_mpe_entry(mc);
229
Simon Glass39473e12017-01-16 07:03:36 -0700230 debug("Write the MP table at: %lx - %lx\n", (ulong)mc, end);
Bin Mengf91cf6b2015-06-23 12:18:51 +0800231
232 return end;
233}
Bin Mengc4f407e2015-06-23 12:18:52 +0800234
235static void mptable_add_isa_interrupts(struct mp_config_table *mc, int bus_isa,
236 int apicid, int external_int2)
237{
238 int i;
239
240 mp_write_intsrc(mc, external_int2 ? MP_INT : MP_EXTINT,
241 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
242 bus_isa, 0, apicid, 0);
243 mp_write_intsrc(mc, MP_INT, MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
244 bus_isa, 1, apicid, 1);
245 mp_write_intsrc(mc, external_int2 ? MP_EXTINT : MP_INT,
246 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
247 bus_isa, 0, apicid, 2);
248
Bin Meng95882f02015-07-22 01:21:10 -0700249 for (i = 3; i < 16; i++) {
250 /*
251 * Do not write ISA interrupt entry if it is already occupied
252 * by the platform devices.
253 */
254 if (isa_irq_occupied[i])
255 continue;
256
Bin Mengc4f407e2015-06-23 12:18:52 +0800257 mp_write_intsrc(mc, MP_INT,
258 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
259 bus_isa, i, apicid, i);
Bin Meng95882f02015-07-22 01:21:10 -0700260 }
Bin Mengc4f407e2015-06-23 12:18:52 +0800261}
262
263/*
264 * Check duplicated I/O interrupt assignment table entry, to make sure
265 * there is only one entry with the given bus, device and interrupt pin.
266 */
267static bool check_dup_entry(struct mpc_config_intsrc *intsrc_base,
268 int entry_num, int bus, int device, int pin)
269{
270 struct mpc_config_intsrc *intsrc = intsrc_base;
271 int i;
272
273 for (i = 0; i < entry_num; i++) {
274 if (intsrc->mpc_srcbus == bus &&
275 intsrc->mpc_srcbusirq == ((device << 2) | (pin - 1)))
276 break;
277 intsrc++;
278 }
279
280 return (i == entry_num) ? false : true;
281}
282
Bin Menga2def7e2015-07-22 01:21:09 -0700283/* TODO: move this to driver model */
284__weak int mp_determine_pci_dstirq(int bus, int dev, int func, int pirq)
285{
286 /* PIRQ[A-H] are connected to I/O APIC INTPIN#16-23 */
287 return pirq + 16;
288}
289
Bin Mengc4f407e2015-06-23 12:18:52 +0800290static int mptable_add_intsrc(struct mp_config_table *mc,
291 int bus_isa, int apicid)
292{
293 struct mpc_config_intsrc *intsrc_base;
294 int intsrc_entries = 0;
295 const void *blob = gd->fdt_blob;
Simon Glassddcafd62016-01-19 21:32:28 -0700296 struct udevice *dev;
Bin Mengc4f407e2015-06-23 12:18:52 +0800297 int len, count;
298 const u32 *cell;
Simon Glassddcafd62016-01-19 21:32:28 -0700299 int i, ret;
Bin Mengc4f407e2015-06-23 12:18:52 +0800300
Simon Glassc7298e72016-02-11 13:23:26 -0700301 ret = uclass_first_device_err(UCLASS_IRQ, &dev);
Simon Glassddcafd62016-01-19 21:32:28 -0700302 if (ret && ret != -ENODEV) {
Bin Mengc4f407e2015-06-23 12:18:52 +0800303 debug("%s: Cannot find irq router node\n", __func__);
Simon Glassddcafd62016-01-19 21:32:28 -0700304 return ret;
Bin Mengc4f407e2015-06-23 12:18:52 +0800305 }
306
Simon Glassddcafd62016-01-19 21:32:28 -0700307 /* Get I/O interrupt information from device tree */
Simon Glassdd79d6e2017-01-17 16:52:55 -0700308 cell = fdt_getprop(blob, dev_of_offset(dev), "intel,pirq-routing",
309 &len);
Bin Mengc4f407e2015-06-23 12:18:52 +0800310 if (!cell)
311 return -ENOENT;
312
313 if ((len % sizeof(struct pirq_routing)) == 0)
314 count = len / sizeof(struct pirq_routing);
315 else
316 return -EINVAL;
317
318 intsrc_base = (struct mpc_config_intsrc *)mp_next_mpc_entry(mc);
319
320 for (i = 0; i < count; i++) {
321 struct pirq_routing pr;
Bin Menga2def7e2015-07-22 01:21:09 -0700322 int bus, dev, func;
323 int dstirq;
Bin Mengc4f407e2015-06-23 12:18:52 +0800324
325 pr.bdf = fdt_addr_to_cpu(cell[0]);
326 pr.pin = fdt_addr_to_cpu(cell[1]);
327 pr.pirq = fdt_addr_to_cpu(cell[2]);
Bin Menga2def7e2015-07-22 01:21:09 -0700328 bus = PCI_BUS(pr.bdf);
329 dev = PCI_DEV(pr.bdf);
330 func = PCI_FUNC(pr.bdf);
Bin Mengc4f407e2015-06-23 12:18:52 +0800331
332 if (check_dup_entry(intsrc_base, intsrc_entries,
Bin Menga2def7e2015-07-22 01:21:09 -0700333 bus, dev, pr.pin)) {
Bin Mengc4f407e2015-06-23 12:18:52 +0800334 debug("found entry for bus %d device %d INT%c, skipping\n",
Bin Menga2def7e2015-07-22 01:21:09 -0700335 bus, dev, 'A' + pr.pin - 1);
Bin Mengc4f407e2015-06-23 12:18:52 +0800336 cell += sizeof(struct pirq_routing) / sizeof(u32);
337 continue;
338 }
339
Bin Menga2def7e2015-07-22 01:21:09 -0700340 dstirq = mp_determine_pci_dstirq(bus, dev, func, pr.pirq);
Bin Meng95882f02015-07-22 01:21:10 -0700341 /*
342 * For PIRQ which is connected to I/O APIC interrupt pin#0-15,
343 * mark it as occupied so that we can skip it later.
344 */
345 if (dstirq < 16)
346 isa_irq_occupied[dstirq] = true;
Bin Menga2def7e2015-07-22 01:21:09 -0700347 mp_write_pci_intsrc(mc, MP_INT, bus, dev, pr.pin,
348 apicid, dstirq);
Bin Mengc4f407e2015-06-23 12:18:52 +0800349 intsrc_entries++;
350 cell += sizeof(struct pirq_routing) / sizeof(u32);
351 }
352
Bin Meng95882f02015-07-22 01:21:10 -0700353 /* Legacy Interrupts */
354 debug("Writing ISA IRQs\n");
355 mptable_add_isa_interrupts(mc, bus_isa, apicid, 0);
356
Bin Mengc4f407e2015-06-23 12:18:52 +0800357 return 0;
358}
359
360static void mptable_add_lintsrc(struct mp_config_table *mc, int bus_isa)
361{
362 mp_write_lintsrc(mc, MP_EXTINT,
363 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
364 bus_isa, 0, MP_APIC_ALL, 0);
365 mp_write_lintsrc(mc, MP_NMI,
366 MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH,
367 bus_isa, 0, MP_APIC_ALL, 1);
368}
369
Simon Glassca37a392017-01-16 07:03:35 -0700370ulong write_mp_table(ulong addr)
Bin Mengc4f407e2015-06-23 12:18:52 +0800371{
372 struct mp_config_table *mc;
373 int ioapic_id, ioapic_ver;
374 int bus_isa = 0xff;
375 int ret;
Simon Glass39473e12017-01-16 07:03:36 -0700376 ulong end;
Bin Mengc4f407e2015-06-23 12:18:52 +0800377
378 /* 16 byte align the table address */
379 addr = ALIGN(addr, 16);
380
381 /* Write floating table */
382 mc = mp_write_floating_table((struct mp_floating_table *)addr);
383
384 /* Write configuration table header */
385 mp_config_table_init(mc);
386
387 /* Write processor entry */
388 mp_write_processor(mc);
389
390 /* Write bus entry */
391 mp_write_bus(mc, bus_isa, BUSTYPE_ISA);
392
393 /* Write I/O APIC entry */
394 ioapic_id = io_apic_read(IO_APIC_ID) >> 24;
395 ioapic_ver = io_apic_read(IO_APIC_VER) & 0xff;
396 mp_write_ioapic(mc, ioapic_id, ioapic_ver, IO_APIC_ADDR);
397
398 /* Write I/O interrupt assignment entry */
399 ret = mptable_add_intsrc(mc, bus_isa, ioapic_id);
400 if (ret)
401 debug("Failed to write I/O interrupt assignment table\n");
402
403 /* Write local interrupt assignment entry */
404 mptable_add_lintsrc(mc, bus_isa);
405
406 /* Finalize the MP table */
407 end = mptable_finalize(mc);
408
409 return end;
410}