blob: 6746399d028dcee8ab7ffc7ad83251eb3ff46021 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
Eric Biederman7003ba42004-10-16 06:20:29 +00002#include <device/device.h>
3#include <device/path.h>
Yinghai Luf327d9f2008-02-20 17:41:38 +00004#include <device/pci_ids.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00005#include <cpu/cpu.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>
Eric Biederman8ca8d762003-04-22 19:02:15 +000010
11unsigned char smp_compute_checksum(void *v, int len)
12{
13 unsigned char *bytes;
14 unsigned char checksum;
15 int i;
16 bytes = v;
17 checksum = 0;
18 for(i = 0; i < len; i++) {
19 checksum -= bytes[i];
20 }
21 return checksum;
22}
23
24void *smp_write_floating_table(unsigned long addr)
25{
26 struct intel_mp_floating *mf;
27 void *v;
28
29 /* 16 byte align the table address */
Yinghai Luf327d9f2008-02-20 17:41:38 +000030 addr = (addr + 0xf) & (~0xf);
Patrick Georgie9149032009-04-24 06:27:31 +000031 return smp_write_floating_table_physaddr(addr, addr + SMP_FLOATING_TABLE_LEN);
Eric Biederman8ca8d762003-04-22 19:02:15 +000032}
33
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000034void *smp_write_floating_table_physaddr(unsigned long addr, unsigned long mpf_physptr)
35{
36 struct intel_mp_floating *mf;
37 void *v;
Yinghai Luf327d9f2008-02-20 17:41:38 +000038
39 v = (void *)addr;
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000040 mf = v;
41 mf->mpf_signature[0] = '_';
42 mf->mpf_signature[1] = 'M';
43 mf->mpf_signature[2] = 'P';
44 mf->mpf_signature[3] = '_';
45 mf->mpf_physptr = mpf_physptr;
46 mf->mpf_length = 1;
47 mf->mpf_specification = 4;
48 mf->mpf_checksum = 0;
49 mf->mpf_feature1 = 0;
50 mf->mpf_feature2 = 0;
51 mf->mpf_feature3 = 0;
52 mf->mpf_feature4 = 0;
53 mf->mpf_feature5 = 0;
54 mf->mpf_checksum = smp_compute_checksum(mf, mf->mpf_length*16);
55 return v;
56}
57
Eric Biederman8ca8d762003-04-22 19:02:15 +000058void *smp_next_mpc_entry(struct mp_config_table *mc)
59{
60 void *v;
61 v = (void *)(((char *)mc) + mc->mpc_length);
62 return v;
63}
64static void smp_add_mpc_entry(struct mp_config_table *mc, unsigned length)
65{
66 mc->mpc_length += length;
67 mc->mpc_entry_count++;
68}
69
70void *smp_next_mpe_entry(struct mp_config_table *mc)
71{
72 void *v;
73 v = (void *)(((char *)mc) + mc->mpc_length + mc->mpe_length);
74 return v;
75}
76static void smp_add_mpe_entry(struct mp_config_table *mc, mpe_t mpe)
77{
78 mc->mpe_length += mpe->mpe_length;
79}
80
81void smp_write_processor(struct mp_config_table *mc,
82 unsigned char apicid, unsigned char apicver,
83 unsigned char cpuflag, unsigned int cpufeature,
84 unsigned int featureflag)
85{
86 struct mpc_config_processor *mpc;
87 mpc = smp_next_mpc_entry(mc);
88 memset(mpc, '\0', sizeof(*mpc));
89 mpc->mpc_type = MP_PROCESSOR;
90 mpc->mpc_apicid = apicid;
91 mpc->mpc_apicver = apicver;
92 mpc->mpc_cpuflag = cpuflag;
93 mpc->mpc_cpufeature = cpufeature;
94 mpc->mpc_featureflag = featureflag;
95 smp_add_mpc_entry(mc, sizeof(*mpc));
96}
97
98/* If we assume a symmetric processor configuration we can
99 * get all of the information we need to write the processor
100 * entry from the bootstrap processor.
101 * Plus I don't think linux really even cares.
102 * Having the proper apicid's in the table so the non-bootstrap
103 * processors can be woken up should be enough.
104 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000105void smp_write_processors(struct mp_config_table *mc)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000106{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000107 int boot_apic_id;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000108 unsigned apic_version;
109 unsigned cpu_features;
110 unsigned cpu_feature_flags;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000111 struct cpuid_result result;
112 device_t cpu;
Eric Biederman7003ba42004-10-16 06:20:29 +0000113
Eric Biedermanb78c1972004-10-14 20:54:17 +0000114 boot_apic_id = lapicid();
115 apic_version = lapic_read(LAPIC_LVR) & 0xff;
116 result = cpuid(1);
117 cpu_features = result.eax;
118 cpu_feature_flags = result.edx;
Eric Biederman7003ba42004-10-16 06:20:29 +0000119 for(cpu = all_devices; cpu; cpu = cpu->next) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000120 unsigned long cpu_flag;
Eric Biederman7003ba42004-10-16 06:20:29 +0000121 if ((cpu->path.type != DEVICE_PATH_APIC) ||
122 (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
123 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000124 continue;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000125 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000126 if (!cpu->enabled) {
127 continue;
128 }
129 cpu_flag = MPC_CPU_ENABLED;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000130 if (boot_apic_id == cpu->path.apic.apic_id) {
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000131 cpu_flag = MPC_CPU_ENABLED | MPC_CPU_BOOTPROCESSOR;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000132 }
133 smp_write_processor(mc,
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000134 cpu->path.apic.apic_id, apic_version,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000135 cpu_flag, cpu_features, cpu_feature_flags
136 );
Eric Biederman8ca8d762003-04-22 19:02:15 +0000137 }
138}
139
140void smp_write_bus(struct mp_config_table *mc,
Stefan Reinauer7a4f6882008-08-01 11:31:08 +0000141 unsigned char id, char *bustype)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000142{
143 struct mpc_config_bus *mpc;
144 mpc = smp_next_mpc_entry(mc);
145 memset(mpc, '\0', sizeof(*mpc));
146 mpc->mpc_type = MP_BUS;
147 mpc->mpc_busid = id;
148 memcpy(mpc->mpc_bustype, bustype, sizeof(mpc->mpc_bustype));
149 smp_add_mpc_entry(mc, sizeof(*mpc));
150}
151
152void smp_write_ioapic(struct mp_config_table *mc,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000153 unsigned char id, unsigned char ver,
154 unsigned long apicaddr)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000155{
156 struct mpc_config_ioapic *mpc;
157 mpc = smp_next_mpc_entry(mc);
158 memset(mpc, '\0', sizeof(*mpc));
159 mpc->mpc_type = MP_IOAPIC;
160 mpc->mpc_apicid = id;
161 mpc->mpc_apicver = ver;
162 mpc->mpc_flags = MPC_APIC_USABLE;
163 mpc->mpc_apicaddr = apicaddr;
164 smp_add_mpc_entry(mc, sizeof(*mpc));
165}
166
167void smp_write_intsrc(struct mp_config_table *mc,
168 unsigned char irqtype, unsigned short irqflag,
169 unsigned char srcbus, unsigned char srcbusirq,
170 unsigned char dstapic, unsigned char dstirq)
171{
172 struct mpc_config_intsrc *mpc;
173 mpc = smp_next_mpc_entry(mc);
174 memset(mpc, '\0', sizeof(*mpc));
175 mpc->mpc_type = MP_INTSRC;
176 mpc->mpc_irqtype = irqtype;
177 mpc->mpc_irqflag = irqflag;
178 mpc->mpc_srcbus = srcbus;
179 mpc->mpc_srcbusirq = srcbusirq;
180 mpc->mpc_dstapic = dstapic;
181 mpc->mpc_dstirq = dstirq;
182 smp_add_mpc_entry(mc, sizeof(*mpc));
Stefan Reinauer5bba7522009-04-21 23:00:14 +0000183#ifdef DEBUG_MPTABLE
184 printk_debug("add intsrc srcbus 0x%x srcbusirq 0x%x, dstapic 0x%x, dstirq 0x%x\n",
Eric Biederman8ca8d762003-04-22 19:02:15 +0000185 srcbus, srcbusirq, dstapic, dstirq);
Myles Watson552b3272009-02-12 21:30:06 +0000186 hexdump(__func__, mpc, sizeof(*mpc));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000187#endif
188}
189
Yinghai Luf327d9f2008-02-20 17:41:38 +0000190void smp_write_intsrc_pci_bridge(struct mp_config_table *mc,
191 unsigned char irqtype, unsigned short irqflag,
192 struct device *dev,
193 unsigned char dstapic, unsigned char *dstirq)
194{
195 struct device *child;
196
197 int linkn;
198 int i;
199 int srcbus;
200 int slot;
201
202 struct bus *link;
203 unsigned char dstirq_x[4];
204
205 for (linkn = 0; linkn < dev->links; linkn++) {
206
207 link = &dev->link[linkn];
208 child = link->children;
209 srcbus = link->secondary;
210
211 while (child) {
212 if (child->path.type != DEVICE_PATH_PCI)
213 goto next;
214
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000215 slot = (child->path.pci.devfn >> 3);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000216 /* round pins */
217 for (i = 0; i < 4; i++)
218 dstirq_x[i] = dstirq[(i + slot) % 4];
219
220 if ((child->class >> 16) != PCI_BASE_CLASS_BRIDGE) {
221 /* pci device */
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000222 printk_debug("route irq: %s\n", dev_path(child));
Yinghai Luf327d9f2008-02-20 17:41:38 +0000223 for (i = 0; i < 4; i++)
224 smp_write_intsrc(mc, irqtype, irqflag, srcbus, (slot<<2)|i, dstapic, dstirq_x[i]);
225 goto next;
226 }
227
228 switch (child->class>>8) {
229 case PCI_CLASS_BRIDGE_PCI:
230 case PCI_CLASS_BRIDGE_PCMCIA:
231 case PCI_CLASS_BRIDGE_CARDBUS:
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000232 printk_debug("route irq bridge: %s\n", dev_path(child));
Yinghai Luf327d9f2008-02-20 17:41:38 +0000233 smp_write_intsrc_pci_bridge(mc, irqtype, irqflag, child, dstapic, dstirq_x);
234 }
235
236 next:
237 child = child->sibling;
238 }
239
240 }
241}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000242
243void smp_write_lintsrc(struct mp_config_table *mc,
244 unsigned char irqtype, unsigned short irqflag,
245 unsigned char srcbusid, unsigned char srcbusirq,
246 unsigned char destapic, unsigned char destapiclint)
247{
248 struct mpc_config_lintsrc *mpc;
249 mpc = smp_next_mpc_entry(mc);
250 memset(mpc, '\0', sizeof(*mpc));
251 mpc->mpc_type = MP_LINTSRC;
252 mpc->mpc_irqtype = irqtype;
253 mpc->mpc_irqflag = irqflag;
254 mpc->mpc_srcbusid = srcbusid;
255 mpc->mpc_srcbusirq = srcbusirq;
256 mpc->mpc_destapic = destapic;
257 mpc->mpc_destapiclint = destapiclint;
258 smp_add_mpc_entry(mc, sizeof(*mpc));
259}
260
261void smp_write_address_space(struct mp_config_table *mc,
262 unsigned char busid, unsigned char address_type,
263 unsigned int address_base_low, unsigned int address_base_high,
264 unsigned int address_length_low, unsigned int address_length_high)
265{
266 struct mp_exten_system_address_space *mpe;
267 mpe = smp_next_mpe_entry(mc);
268 memset(mpe, '\0', sizeof(*mpe));
269 mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE;
270 mpe->mpe_length = sizeof(*mpe);
271 mpe->mpe_busid = busid;
272 mpe->mpe_address_type = address_type;
273 mpe->mpe_address_base_low = address_base_low;
274 mpe->mpe_address_base_high = address_base_high;
275 mpe->mpe_address_length_low = address_length_low;
276 mpe->mpe_address_length_high = address_length_high;
277 smp_add_mpe_entry(mc, (mpe_t)mpe);
278}
279
280
281void smp_write_bus_hierarchy(struct mp_config_table *mc,
282 unsigned char busid, unsigned char bus_info,
283 unsigned char parent_busid)
284{
285 struct mp_exten_bus_hierarchy *mpe;
286 mpe = smp_next_mpe_entry(mc);
287 memset(mpe, '\0', sizeof(*mpe));
288 mpe->mpe_type = MPE_BUS_HIERARCHY;
289 mpe->mpe_length = sizeof(*mpe);
290 mpe->mpe_busid = busid;
291 mpe->mpe_bus_info = bus_info;
292 mpe->mpe_parent_busid = parent_busid;
293 smp_add_mpe_entry(mc, (mpe_t)mpe);
294}
295
296void smp_write_compatibility_address_space(struct mp_config_table *mc,
297 unsigned char busid, unsigned char address_modifier,
298 unsigned int range_list)
299{
300 struct mp_exten_compatibility_address_space *mpe;
301 mpe = smp_next_mpe_entry(mc);
302 memset(mpe, '\0', sizeof(*mpe));
303 mpe->mpe_type = MPE_COMPATIBILITY_ADDRESS_SPACE;
304 mpe->mpe_length = sizeof(*mpe);
305 mpe->mpe_busid = busid;
306 mpe->mpe_address_modifier = address_modifier;
307 mpe->mpe_range_list = range_list;
308 smp_add_mpe_entry(mc, (mpe_t)mpe);
309}
310