blob: 26d102544d5afc919ecc489a71b98877470bdeba [file] [log] [blame]
Bin Meng7f5df8d2015-06-23 12:18:51 +08001/*
2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3 *
4 * Adapted from coreboot src/arch/x86/boot/mpspec.c
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <cpu.h>
11#include <dm.h>
12#include <asm/cpu.h>
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>
18
19struct mp_config_table *mp_write_floating_table(struct mp_floating_table *mf)
20{
21 u32 mc;
22
23 memcpy(mf->mpf_signature, MPF_SIGNATURE, 4);
24 mf->mpf_physptr = (u32)mf + sizeof(struct mp_floating_table);
25 mf->mpf_length = 1;
26 mf->mpf_spec = MPSPEC_V14;
27 mf->mpf_checksum = 0;
28 /* We don't use the default configuration table */
29 mf->mpf_feature1 = 0;
30 /* Indicate that virtual wire mode is always implemented */
31 mf->mpf_feature2 = 0;
32 mf->mpf_feature3 = 0;
33 mf->mpf_feature4 = 0;
34 mf->mpf_feature5 = 0;
35 mf->mpf_checksum = table_compute_checksum(mf, mf->mpf_length * 16);
36
37 mc = (u32)mf + sizeof(struct mp_floating_table);
38 return (struct mp_config_table *)mc;
39}
40
41void mp_config_table_init(struct mp_config_table *mc)
42{
43 memcpy(mc->mpc_signature, MPC_SIGNATURE, 4);
44 mc->mpc_length = sizeof(struct mp_config_table);
45 mc->mpc_spec = MPSPEC_V14;
46 mc->mpc_checksum = 0;
47 mc->mpc_oemptr = 0;
48 mc->mpc_oemsize = 0;
49 mc->mpc_entry_count = 0;
50 mc->mpc_lapic = LAPIC_DEFAULT_BASE;
51 mc->mpe_length = 0;
52 mc->mpe_checksum = 0;
53 mc->reserved = 0;
54
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, ' ');
58}
59
60void mp_write_processor(struct mp_config_table *mc)
61{
62 struct mpc_config_processor *mpc;
63 struct udevice *dev;
64 u8 boot_apicid, apicver;
65 u32 cpusignature, cpufeature;
66 struct cpuid_result result;
67
68 boot_apicid = lapicid();
69 apicver = lapic_read(LAPIC_LVR) & 0xff;
70 result = cpuid(1);
71 cpusignature = result.eax;
72 cpufeature = result.edx;
73
74 for (uclass_find_first_device(UCLASS_CPU, &dev);
75 dev;
76 uclass_find_next_device(&dev)) {
77 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
78 u8 cpuflag = MPC_CPU_EN;
79
80 if (!device_active(dev))
81 continue;
82
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));
95 }
96}
97
98void mp_write_bus(struct mp_config_table *mc, int id, const char *bustype)
99{
100 struct mpc_config_bus *mpc;
101
102 mpc = (struct mpc_config_bus *)mp_next_mpc_entry(mc);
103 mpc->mpc_type = MP_BUS;
104 mpc->mpc_busid = id;
105 memcpy(mpc->mpc_bustype, bustype, 6);
106 mp_add_mpc_entry(mc, sizeof(*mpc));
107}
108
109void mp_write_ioapic(struct mp_config_table *mc, int id, int ver, u32 apicaddr)
110{
111 struct mpc_config_ioapic *mpc;
112
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));
120}
121
122void mp_write_intsrc(struct mp_config_table *mc, int irqtype, int irqflag,
123 int srcbus, int srcbusirq, int dstapic, int dstirq)
124{
125 struct mpc_config_intsrc *mpc;
126
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));
136}
137
138void mp_write_pci_intsrc(struct mp_config_table *mc, int irqtype,
139 int srcbus, int dev, int pin, int dstapic, int dstirq)
140{
141 u8 srcbusirq = (dev << 2) | (pin - 1);
142
143 mp_write_intsrc(mc, irqtype, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW,
144 srcbus, srcbusirq, dstapic, dstirq);
145}
146
147void mp_write_lintsrc(struct mp_config_table *mc, int irqtype, int irqflag,
148 int srcbus, int srcbusirq, int destapic, int destlint)
149{
150 struct mpc_config_lintsrc *mpc;
151
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));
161}
162
163void 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)
167{
168 struct mp_ext_system_address_space *mpe;
169
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);
180}
181
182void mp_write_bus_hierarchy(struct mp_config_table *mc,
183 int busid, int bus_info, int parent_busid)
184{
185 struct mp_ext_bus_hierarchy *mpe;
186
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);
197}
198
199void mp_write_compat_address_space(struct mp_config_table *mc, int busid,
200 int addr_modifier, u32 range_list)
201{
202 struct mp_ext_compat_address_space *mpe;
203
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);
211}
212
213u32 mptable_finalize(struct mp_config_table *mc)
214{
215 u32 end;
216
217 mc->mpe_checksum = table_compute_checksum((void *)mp_next_mpc_entry(mc),
218 mc->mpe_length);
219 mc->mpc_checksum = table_compute_checksum(mc, mc->mpc_length);
220 end = mp_next_mpe_entry(mc);
221
222 debug("Write the MP table at: %x - %x\n", (u32)mc, end);
223
224 return end;
225}