2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 * Adapted from coreboot src/arch/x86/boot/mpspec.c
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/ioapic.h>
14 #include <asm/lapic.h>
15 #include <asm/mpspec.h>
16 #include <asm/tables.h>
17 #include <dm/uclass-internal.h>
19 struct mp_config_table *mp_write_floating_table(struct mp_floating_table *mf)
23 memcpy(mf->mpf_signature, MPF_SIGNATURE, 4);
24 mf->mpf_physptr = (u32)mf + sizeof(struct mp_floating_table);
26 mf->mpf_spec = MPSPEC_V14;
28 /* We don't use the default configuration table */
30 /* Indicate that virtual wire mode is always implemented */
35 mf->mpf_checksum = table_compute_checksum(mf, mf->mpf_length * 16);
37 mc = (u32)mf + sizeof(struct mp_floating_table);
38 return (struct mp_config_table *)mc;
41 void mp_config_table_init(struct mp_config_table *mc)
43 memcpy(mc->mpc_signature, MPC_SIGNATURE, 4);
44 mc->mpc_length = sizeof(struct mp_config_table);
45 mc->mpc_spec = MPSPEC_V14;
49 mc->mpc_entry_count = 0;
50 mc->mpc_lapic = LAPIC_DEFAULT_BASE;
55 /* The oem/product id fields are exactly 8/12 bytes long */
56 table_fill_string(mc->mpc_oem, CONFIG_SYS_VENDOR, 8, ' ');
57 table_fill_string(mc->mpc_product, CONFIG_SYS_BOARD, 12, ' ');
60 void mp_write_processor(struct mp_config_table *mc)
62 struct mpc_config_processor *mpc;
64 u8 boot_apicid, apicver;
65 u32 cpusignature, cpufeature;
66 struct cpuid_result result;
68 boot_apicid = lapicid();
69 apicver = lapic_read(LAPIC_LVR) & 0xff;
71 cpusignature = result.eax;
72 cpufeature = result.edx;
74 for (uclass_find_first_device(UCLASS_CPU, &dev);
76 uclass_find_next_device(&dev)) {
77 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
78 u8 cpuflag = MPC_CPU_EN;
80 if (!device_active(dev))
83 mpc = (struct mpc_config_processor *)mp_next_mpc_entry(mc);
84 mpc->mpc_type = MP_PROCESSOR;
85 mpc->mpc_apicid = plat->cpu_id;
86 mpc->mpc_apicver = apicver;
87 if (boot_apicid == plat->cpu_id)
88 cpuflag |= MPC_CPU_BP;
89 mpc->mpc_cpuflag = cpuflag;
90 mpc->mpc_cpusignature = cpusignature;
91 mpc->mpc_cpufeature = cpufeature;
92 mpc->mpc_reserved[0] = 0;
93 mpc->mpc_reserved[1] = 0;
94 mp_add_mpc_entry(mc, sizeof(*mpc));
98 void mp_write_bus(struct mp_config_table *mc, int id, const char *bustype)
100 struct mpc_config_bus *mpc;
102 mpc = (struct mpc_config_bus *)mp_next_mpc_entry(mc);
103 mpc->mpc_type = MP_BUS;
105 memcpy(mpc->mpc_bustype, bustype, 6);
106 mp_add_mpc_entry(mc, sizeof(*mpc));
109 void mp_write_ioapic(struct mp_config_table *mc, int id, int ver, u32 apicaddr)
111 struct mpc_config_ioapic *mpc;
113 mpc = (struct mpc_config_ioapic *)mp_next_mpc_entry(mc);
114 mpc->mpc_type = MP_IOAPIC;
115 mpc->mpc_apicid = id;
116 mpc->mpc_apicver = ver;
117 mpc->mpc_flags = MPC_APIC_USABLE;
118 mpc->mpc_apicaddr = apicaddr;
119 mp_add_mpc_entry(mc, sizeof(*mpc));
122 void mp_write_intsrc(struct mp_config_table *mc, int irqtype, int irqflag,
123 int srcbus, int srcbusirq, int dstapic, int dstirq)
125 struct mpc_config_intsrc *mpc;
127 mpc = (struct mpc_config_intsrc *)mp_next_mpc_entry(mc);
128 mpc->mpc_type = MP_INTSRC;
129 mpc->mpc_irqtype = irqtype;
130 mpc->mpc_irqflag = irqflag;
131 mpc->mpc_srcbus = srcbus;
132 mpc->mpc_srcbusirq = srcbusirq;
133 mpc->mpc_dstapic = dstapic;
134 mpc->mpc_dstirq = dstirq;
135 mp_add_mpc_entry(mc, sizeof(*mpc));
138 void mp_write_pci_intsrc(struct mp_config_table *mc, int irqtype,
139 int srcbus, int dev, int pin, int dstapic, int dstirq)
141 u8 srcbusirq = (dev << 2) | (pin - 1);
143 mp_write_intsrc(mc, irqtype, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW,
144 srcbus, srcbusirq, dstapic, dstirq);
147 void mp_write_lintsrc(struct mp_config_table *mc, int irqtype, int irqflag,
148 int srcbus, int srcbusirq, int destapic, int destlint)
150 struct mpc_config_lintsrc *mpc;
152 mpc = (struct mpc_config_lintsrc *)mp_next_mpc_entry(mc);
153 mpc->mpc_type = MP_LINTSRC;
154 mpc->mpc_irqtype = irqtype;
155 mpc->mpc_irqflag = irqflag;
156 mpc->mpc_srcbusid = srcbus;
157 mpc->mpc_srcbusirq = srcbusirq;
158 mpc->mpc_destapic = destapic;
159 mpc->mpc_destlint = destlint;
160 mp_add_mpc_entry(mc, sizeof(*mpc));
163 void mp_write_address_space(struct mp_config_table *mc,
164 int busid, int addr_type,
165 u32 addr_base_low, u32 addr_base_high,
166 u32 addr_length_low, u32 addr_length_high)
168 struct mp_ext_system_address_space *mpe;
170 mpe = (struct mp_ext_system_address_space *)mp_next_mpe_entry(mc);
171 mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE;
172 mpe->mpe_length = sizeof(*mpe);
173 mpe->mpe_busid = busid;
174 mpe->mpe_addr_type = addr_type;
175 mpe->mpe_addr_base_low = addr_base_low;
176 mpe->mpe_addr_base_high = addr_base_high;
177 mpe->mpe_addr_length_low = addr_length_low;
178 mpe->mpe_addr_length_high = addr_length_high;
179 mp_add_mpe_entry(mc, (struct mp_ext_config *)mpe);
182 void mp_write_bus_hierarchy(struct mp_config_table *mc,
183 int busid, int bus_info, int parent_busid)
185 struct mp_ext_bus_hierarchy *mpe;
187 mpe = (struct mp_ext_bus_hierarchy *)mp_next_mpe_entry(mc);
188 mpe->mpe_type = MPE_BUS_HIERARCHY;
189 mpe->mpe_length = sizeof(*mpe);
190 mpe->mpe_busid = busid;
191 mpe->mpe_bus_info = bus_info;
192 mpe->mpe_parent_busid = parent_busid;
193 mpe->reserved[0] = 0;
194 mpe->reserved[1] = 0;
195 mpe->reserved[2] = 0;
196 mp_add_mpe_entry(mc, (struct mp_ext_config *)mpe);
199 void mp_write_compat_address_space(struct mp_config_table *mc, int busid,
200 int addr_modifier, u32 range_list)
202 struct mp_ext_compat_address_space *mpe;
204 mpe = (struct mp_ext_compat_address_space *)mp_next_mpe_entry(mc);
205 mpe->mpe_type = MPE_COMPAT_ADDRESS_SPACE;
206 mpe->mpe_length = sizeof(*mpe);
207 mpe->mpe_busid = busid;
208 mpe->mpe_addr_modifier = addr_modifier;
209 mpe->mpe_range_list = range_list;
210 mp_add_mpe_entry(mc, (struct mp_ext_config *)mpe);
213 u32 mptable_finalize(struct mp_config_table *mc)
217 mc->mpe_checksum = table_compute_checksum((void *)mp_next_mpc_entry(mc),
219 mc->mpc_checksum = table_compute_checksum(mc, mc->mpc_length);
220 end = mp_next_mpe_entry(mc);
222 debug("Write the MP table at: %x - %x\n", (u32)mc, end);