x86: Use memblock to replace early_res
[linux-2.6-block.git] / arch / x86 / kernel / apic / numaq_32.c
CommitLineData
1da177e4
LT
1/*
2 * Written by: Patricia Gaughen, IBM Corporation
3 *
4 * Copyright (C) 2002, IBM Corp.
cb81eaed 5 * Copyright (C) 2009, Red Hat, Inc., Ingo Molnar
1da177e4 6 *
4f179d12 7 * All rights reserved.
1da177e4
LT
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17 * NON INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * Send feedback to <gone@us.ibm.com>
25 */
4f179d12 26#include <linux/nodemask.h>
cb81eaed 27#include <linux/topology.h>
1da177e4 28#include <linux/bootmem.h>
36afc3af
IM
29#include <linux/threads.h>
30#include <linux/cpumask.h>
31#include <linux/kernel.h>
1da177e4
LT
32#include <linux/mmzone.h>
33#include <linux/module.h>
36afc3af
IM
34#include <linux/string.h>
35#include <linux/init.h>
36#include <linux/numa.h>
37#include <linux/smp.h>
cb81eaed 38#include <linux/io.h>
4f179d12
IM
39#include <linux/mm.h>
40
e1474e2d 41#include <asm/processor.h>
36afc3af
IM
42#include <asm/fixmap.h>
43#include <asm/mpspec.h>
4f179d12 44#include <asm/numaq.h>
64898a8b 45#include <asm/setup.h>
36afc3af 46#include <asm/apic.h>
4f179d12 47#include <asm/e820.h>
36afc3af 48#include <asm/ipi.h>
1da177e4 49
36afc3af
IM
50#define MB_TO_PAGES(addr) ((addr) << (20 - PAGE_SHIFT))
51
cb81eaed
IM
52int found_numaq;
53
54/*
55 * Have to match translation table entries to main table entries by counter
56 * hence the mpc_record variable .... can't see a less disgusting way of
57 * doing this ....
58 */
59struct mpc_trans {
60 unsigned char mpc_type;
61 unsigned char trans_len;
62 unsigned char trans_type;
63 unsigned char trans_quad;
64 unsigned char trans_global;
65 unsigned char trans_local;
66 unsigned short trans_reserved;
67};
68
cb81eaed
IM
69static int mpc_record;
70
b6122b38 71static struct mpc_trans *translation_table[MAX_MPC_ENTRY];
cb81eaed
IM
72
73int mp_bus_id_to_node[MAX_MP_BUSSES];
74int mp_bus_id_to_local[MAX_MP_BUSSES];
75int quad_local_to_mp_bus_id[NR_CPUS/4][4];
76
77
36afc3af
IM
78static inline void numaq_register_node(int node, struct sys_cfg_data *scd)
79{
80 struct eachquadmem *eq = scd->eq + node;
81
82 node_set_online(node);
83
84 /* Convert to pages */
85 node_start_pfn[node] =
86 MB_TO_PAGES(eq->hi_shrd_mem_start - eq->priv_mem_size);
87
88 node_end_pfn[node] =
89 MB_TO_PAGES(eq->hi_shrd_mem_start + eq->hi_shrd_mem_size);
90
91 e820_register_active_regions(node, node_start_pfn[node],
92 node_end_pfn[node]);
93
94 memory_present(node, node_start_pfn[node], node_end_pfn[node]);
95
96 node_remap_size[node] = node_memmap_size_bytes(node,
97 node_start_pfn[node],
98 node_end_pfn[node]);
99}
1da177e4
LT
100
101/*
102 * Function: smp_dump_qct()
103 *
104 * Description: gets memory layout from the quad config table. This
105 * function also updates node_online_map with the nodes (quads) present.
106 */
107static void __init smp_dump_qct(void)
108{
36afc3af 109 struct sys_cfg_data *scd;
1da177e4 110 int node;
36afc3af
IM
111
112 scd = (void *)__va(SYS_CFG_DATA_PRIV_ADDR);
1da177e4
LT
113
114 nodes_clear(node_online_map);
115 for_each_node(node) {
36afc3af
IM
116 if (scd->quads_present31_0 & (1 << node))
117 numaq_register_node(node, scd);
1da177e4
LT
118 }
119}
120
b2a6a58c 121void __cpuinit numaq_tsc_disable(void)
64898a8b
YL
122{
123 if (!found_numaq)
124 return;
125
126 if (num_online_nodes() > 1) {
127 printk(KERN_DEBUG "NUMAQ: disabling TSC\n");
128 setup_clear_cpu_cap(X86_FEATURE_TSC);
129 }
130}
131
845b3944 132static void __init numaq_tsc_init(void)
63b5d7af
YL
133{
134 numaq_tsc_disable();
63b5d7af
YL
135}
136
64898a8b
YL
137static inline int generate_logical_apicid(int quad, int phys_apicid)
138{
139 return (quad << 4) + (phys_apicid ? phys_apicid << 1 : 1);
140}
141
142/* x86_quirks member */
f4f21b71 143static int mpc_apic_id(struct mpc_cpu *m)
64898a8b
YL
144{
145 int quad = translation_table[mpc_record]->trans_quad;
c4563826 146 int logical_apicid = generate_logical_apicid(quad, m->apicid);
64898a8b 147
36afc3af
IM
148 printk(KERN_DEBUG
149 "Processor #%d %u:%u APIC version %d (quad %d, apic %d)\n",
150 m->apicid, (m->cpufeature & CPU_FAMILY_MASK) >> 8,
151 (m->cpufeature & CPU_MODEL_MASK) >> 4,
152 m->apicver, quad, logical_apicid);
153
64898a8b
YL
154 return logical_apicid;
155}
156
64898a8b 157/* x86_quirks member */
00fb8606 158static void mpc_oem_bus_info(struct mpc_bus *m, char *name)
64898a8b
YL
159{
160 int quad = translation_table[mpc_record]->trans_quad;
161 int local = translation_table[mpc_record]->trans_local;
162
d4c715fa
JSR
163 mp_bus_id_to_node[m->busid] = quad;
164 mp_bus_id_to_local[m->busid] = local;
36afc3af
IM
165
166 printk(KERN_INFO "Bus #%d is %s (node %d)\n", m->busid, name, quad);
64898a8b
YL
167}
168
64898a8b 169/* x86_quirks member */
00fb8606 170static void mpc_oem_pci_bus(struct mpc_bus *m)
64898a8b
YL
171{
172 int quad = translation_table[mpc_record]->trans_quad;
173 int local = translation_table[mpc_record]->trans_local;
174
d4c715fa 175 quad_local_to_mp_bus_id[quad][local] = m->busid;
64898a8b
YL
176}
177
f4848472
TG
178/*
179 * Called from mpparse code.
180 * mode = 0: prescan
181 * mode = 1: one mpc entry scanned
182 */
183static void numaq_mpc_record(unsigned int mode)
184{
185 if (!mode)
186 mpc_record = 0;
187 else
188 mpc_record++;
189}
190
36afc3af 191static void __init MP_translation_info(struct mpc_trans *m)
64898a8b
YL
192{
193 printk(KERN_INFO
cb81eaed 194 "Translation: record %d, type %d, quad %d, global %d, local %d\n",
64898a8b
YL
195 mpc_record, m->trans_type, m->trans_quad, m->trans_global,
196 m->trans_local);
197
198 if (mpc_record >= MAX_MPC_ENTRY)
199 printk(KERN_ERR "MAX_MPC_ENTRY exceeded!\n");
200 else
36afc3af
IM
201 translation_table[mpc_record] = m; /* stash this for later */
202
64898a8b
YL
203 if (m->trans_quad < MAX_NUMNODES && !node_online(m->trans_quad))
204 node_set_online(m->trans_quad);
205}
206
207static int __init mpf_checksum(unsigned char *mp, int len)
208{
209 int sum = 0;
210
211 while (len--)
212 sum += *mp++;
213
214 return sum & 0xFF;
215}
216
217/*
218 * Read/parse the MPC oem tables
219 */
72302142 220static void __init smp_read_mpc_oem(struct mpc_table *mpc)
64898a8b 221{
72302142 222 struct mpc_oemtable *oemtable = (void *)(long)mpc->oemptr;
64898a8b
YL
223 int count = sizeof(*oemtable); /* the header size */
224 unsigned char *oemptr = ((unsigned char *)oemtable) + count;
225
226 mpc_record = 0;
36afc3af 227 printk(KERN_INFO
3235dc3f 228 "Found an OEM MPC table at %8p - parsing it...\n", oemtable);
36afc3af 229
a1d0272a 230 if (memcmp(oemtable->signature, MPC_OEM_SIGNATURE, 4)) {
64898a8b
YL
231 printk(KERN_WARNING
232 "SMP mpc oemtable: bad signature [%c%c%c%c]!\n",
a1d0272a
JSR
233 oemtable->signature[0], oemtable->signature[1],
234 oemtable->signature[2], oemtable->signature[3]);
64898a8b
YL
235 return;
236 }
36afc3af 237
a1d0272a 238 if (mpf_checksum((unsigned char *)oemtable, oemtable->length)) {
64898a8b
YL
239 printk(KERN_WARNING "SMP oem mptable: checksum error!\n");
240 return;
241 }
36afc3af 242
a1d0272a 243 while (count < oemtable->length) {
64898a8b
YL
244 switch (*oemptr) {
245 case MP_TRANSLATION:
246 {
36afc3af
IM
247 struct mpc_trans *m = (void *)oemptr;
248
64898a8b
YL
249 MP_translation_info(m);
250 oemptr += sizeof(*m);
251 count += sizeof(*m);
252 ++mpc_record;
253 break;
254 }
255 default:
36afc3af
IM
256 printk(KERN_WARNING
257 "Unrecognised OEM table entry type! - %d\n",
258 (int)*oemptr);
259 return;
64898a8b
YL
260 }
261 }
262}
263
ab530e1f
YL
264static __init void early_check_numaq(void)
265{
ab530e1f
YL
266 /*
267 * get boot-time SMP configuration:
268 */
269 if (smp_found_config)
270 early_get_smp_config();
64898a8b 271
f4848472 272 if (found_numaq) {
f4848472 273 x86_init.mpparse.mpc_record = numaq_mpc_record;
de934103 274 x86_init.mpparse.setup_ioapic_ids = x86_init_noop;
fd6c6661 275 x86_init.mpparse.mpc_apic_id = mpc_apic_id;
72302142 276 x86_init.mpparse.smp_read_mpc_oem = smp_read_mpc_oem;
52fdb568 277 x86_init.mpparse.mpc_oem_pci_bus = mpc_oem_pci_bus;
90e1c696 278 x86_init.mpparse.mpc_oem_bus_info = mpc_oem_bus_info;
845b3944 279 x86_init.timers.tsc_pre_init = numaq_tsc_init;
b72d0db9 280 x86_init.pci.init = pci_numaq_init;
f4848472 281 }
ab530e1f
YL
282}
283
1da177e4
LT
284int __init get_memcfg_numaq(void)
285{
ab530e1f
YL
286 early_check_numaq();
287 if (!found_numaq)
288 return 0;
1da177e4 289 smp_dump_qct();
36afc3af 290
1da177e4
LT
291 return 1;
292}
61b90b7c 293
61b90b7c
IM
294#define NUMAQ_APIC_DFR_VALUE (APIC_DFR_CLUSTER)
295
296static inline unsigned int numaq_get_apic_id(unsigned long x)
297{
298 return (x >> 24) & 0x0F;
299}
300
61b90b7c
IM
301static inline void numaq_send_IPI_mask(const struct cpumask *mask, int vector)
302{
43f39890 303 default_send_IPI_mask_sequence_logical(mask, vector);
61b90b7c
IM
304}
305
306static inline void numaq_send_IPI_allbutself(int vector)
307{
43f39890 308 default_send_IPI_mask_allbutself_logical(cpu_online_mask, vector);
61b90b7c
IM
309}
310
311static inline void numaq_send_IPI_all(int vector)
312{
313 numaq_send_IPI_mask(cpu_online_mask, vector);
314}
315
36afc3af
IM
316#define NUMAQ_TRAMPOLINE_PHYS_LOW (0x8)
317#define NUMAQ_TRAMPOLINE_PHYS_HIGH (0xa)
61b90b7c
IM
318
319/*
320 * Because we use NMIs rather than the INIT-STARTUP sequence to
321 * bootstrap the CPUs, the APIC may be in a weird state. Kick it:
322 */
323static inline void numaq_smp_callin_clear_local_apic(void)
324{
325 clear_local_APIC();
326}
327
73e907de 328static inline const struct cpumask *numaq_target_cpus(void)
61b90b7c 329{
101aaca1 330 return cpu_all_mask;
61b90b7c
IM
331}
332
7abc0753 333static unsigned long numaq_check_apicid_used(physid_mask_t *map, int apicid)
61b90b7c 334{
7abc0753 335 return physid_isset(apicid, *map);
61b90b7c
IM
336}
337
338static inline unsigned long numaq_check_apicid_present(int bit)
339{
340 return physid_isset(bit, phys_cpu_present_map);
341}
342
61b90b7c
IM
343static inline int numaq_apic_id_registered(void)
344{
345 return 1;
346}
347
348static inline void numaq_init_apic_ldr(void)
349{
350 /* Already done in NUMA-Q firmware */
351}
352
353static inline void numaq_setup_apic_routing(void)
354{
cb81eaed
IM
355 printk(KERN_INFO
356 "Enabling APIC mode: NUMA-Q. Using %d I/O APICs\n",
357 nr_ioapics);
61b90b7c
IM
358}
359
360/*
361 * Skip adding the timer int on secondary nodes, which causes
362 * a small but painful rift in the time-space continuum.
363 */
364static inline int numaq_multi_timer_check(int apic, int irq)
365{
366 return apic != 0 && irq == 0;
367}
368
7abc0753 369static inline void numaq_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
61b90b7c
IM
370{
371 /* We don't have a good way to do this yet - hack */
7abc0753 372 return physids_promote(0xFUL, retmap);
61b90b7c
IM
373}
374
61b90b7c
IM
375static inline int numaq_cpu_to_logical_apicid(int cpu)
376{
377 if (cpu >= nr_cpu_ids)
378 return BAD_APICID;
2f205bc4 379 return cpu_2_logical_apicid[cpu];
61b90b7c
IM
380}
381
382/*
383 * Supporting over 60 cpus on NUMA-Q requires a locality-dependent
384 * cpu to APIC ID relation to properly interact with the intelligent
385 * mode of the cluster controller.
386 */
387static inline int numaq_cpu_present_to_apicid(int mps_cpu)
388{
389 if (mps_cpu < 60)
390 return ((mps_cpu >> 2) << 4) | (1 << (mps_cpu & 0x3));
391 else
392 return BAD_APICID;
393}
394
36afc3af 395static inline int numaq_apicid_to_node(int logical_apicid)
61b90b7c
IM
396{
397 return logical_apicid >> 4;
398}
399
7abc0753 400static void numaq_apicid_to_cpu_present(int logical_apicid, physid_mask_t *retmap)
61b90b7c
IM
401{
402 int node = numaq_apicid_to_node(logical_apicid);
403 int cpu = __ffs(logical_apicid & 0xf);
404
7abc0753 405 physid_set_mask_of_physid(cpu + 4*node, retmap);
61b90b7c
IM
406}
407
4f179d12
IM
408/* Where the IO area was mapped on multiquad, always 0 otherwise */
409void *xquad_portio;
61b90b7c 410
e11dadab 411static inline int numaq_check_phys_apicid_present(int phys_apicid)
61b90b7c
IM
412{
413 return 1;
414}
415
416/*
417 * We use physical apicids here, not logical, so just return the default
418 * physical broadcast to stop people from breaking us
419 */
73e907de 420static unsigned int numaq_cpu_mask_to_apicid(const struct cpumask *cpumask)
61b90b7c
IM
421{
422 return 0x0F;
423}
424
425static inline unsigned int
426numaq_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
427 const struct cpumask *andmask)
428{
429 return 0x0F;
430}
431
432/* No NUMA-Q box has a HT CPU, but it can't hurt to use the default code. */
433static inline int numaq_phys_pkg_id(int cpuid_apic, int index_msb)
434{
435 return cpuid_apic >> index_msb;
436}
36afc3af
IM
437
438static int
cb81eaed 439numaq_mps_oem_check(struct mpc_table *mpc, char *oem, char *productid)
61b90b7c 440{
cb81eaed
IM
441 if (strncmp(oem, "IBM NUMA", 8))
442 printk(KERN_ERR "Warning! Not a NUMA-Q system!\n");
443 else
444 found_numaq = 1;
445
61b90b7c
IM
446 return found_numaq;
447}
448
449static int probe_numaq(void)
450{
451 /* already know from get_memcfg_numaq() */
452 return found_numaq;
453}
454
73e907de 455static void numaq_vector_allocation_domain(int cpu, struct cpumask *retmask)
61b90b7c
IM
456{
457 /* Careful. Some cpus do not strictly honor the set of cpus
458 * specified in the interrupt destination when using lowest
459 * priority interrupt delivery mode.
460 *
461 * In particular there was a hyperthreading cpu observed to
462 * deliver interrupts to the wrong hyperthread when only one
463 * hyperthread was specified in the interrupt desitination.
464 */
5c6cb5e2
RR
465 cpumask_clear(retmask);
466 cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
61b90b7c
IM
467}
468
469static void numaq_setup_portio_remap(void)
470{
471 int num_quads = num_online_nodes();
472
473 if (num_quads <= 1)
4f179d12 474 return;
61b90b7c 475
cb81eaed
IM
476 printk(KERN_INFO
477 "Remapping cross-quad port I/O for %d quads\n", num_quads);
478
61b90b7c 479 xquad_portio = ioremap(XQUAD_PORTIO_BASE, num_quads*XQUAD_PORTIO_QUAD);
cb81eaed
IM
480
481 printk(KERN_INFO
482 "xquad_portio vaddr 0x%08lx, len %08lx\n",
61b90b7c
IM
483 (u_long) xquad_portio, (u_long) num_quads*XQUAD_PORTIO_QUAD);
484}
485
7473727b
RM
486/* Use __refdata to keep false positive warning calm. */
487struct apic __refdata apic_numaq = {
61b90b7c
IM
488
489 .name = "NUMAQ",
490 .probe = probe_numaq,
491 .acpi_madt_oem_check = NULL,
492 .apic_id_registered = numaq_apic_id_registered,
493
494 .irq_delivery_mode = dest_LowestPrio,
495 /* physical delivery on LOCAL quad: */
496 .irq_dest_mode = 0,
497
498 .target_cpus = numaq_target_cpus,
499 .disable_esr = 1,
500 .dest_logical = APIC_DEST_LOGICAL,
501 .check_apicid_used = numaq_check_apicid_used,
502 .check_apicid_present = numaq_check_apicid_present,
503
504 .vector_allocation_domain = numaq_vector_allocation_domain,
505 .init_apic_ldr = numaq_init_apic_ldr,
506
507 .ioapic_phys_id_map = numaq_ioapic_phys_id_map,
508 .setup_apic_routing = numaq_setup_apic_routing,
509 .multi_timer_check = numaq_multi_timer_check,
510 .apicid_to_node = numaq_apicid_to_node,
511 .cpu_to_logical_apicid = numaq_cpu_to_logical_apicid,
512 .cpu_present_to_apicid = numaq_cpu_present_to_apicid,
513 .apicid_to_cpu_present = numaq_apicid_to_cpu_present,
514 .setup_portio_remap = numaq_setup_portio_remap,
515 .check_phys_apicid_present = numaq_check_phys_apicid_present,
516 .enable_apic_mode = NULL,
517 .phys_pkg_id = numaq_phys_pkg_id,
cb81eaed 518 .mps_oem_check = numaq_mps_oem_check,
61b90b7c
IM
519
520 .get_apic_id = numaq_get_apic_id,
521 .set_apic_id = NULL,
522 .apic_id_mask = 0x0F << 24,
523
524 .cpu_mask_to_apicid = numaq_cpu_mask_to_apicid,
525 .cpu_mask_to_apicid_and = numaq_cpu_mask_to_apicid_and,
526
527 .send_IPI_mask = numaq_send_IPI_mask,
528 .send_IPI_mask_allbutself = NULL,
529 .send_IPI_allbutself = numaq_send_IPI_allbutself,
530 .send_IPI_all = numaq_send_IPI_all,
6b64ee02 531 .send_IPI_self = default_send_IPI_self,
61b90b7c 532
1f5bcabf 533 .wakeup_secondary_cpu = wakeup_secondary_cpu_via_nmi,
61b90b7c
IM
534 .trampoline_phys_low = NUMAQ_TRAMPOLINE_PHYS_LOW,
535 .trampoline_phys_high = NUMAQ_TRAMPOLINE_PHYS_HIGH,
536
537 /* We don't do anything here because we use NMI's to boot instead */
538 .wait_for_init_deassert = NULL,
539
540 .smp_callin_clear_local_apic = numaq_smp_callin_clear_local_apic,
61b90b7c 541 .inquire_remote_apic = NULL,
c1eeb2de
YL
542
543 .read = native_apic_mem_read,
544 .write = native_apic_mem_write,
545 .icr_read = native_apic_icr_read,
546 .icr_write = native_apic_icr_write,
547 .wait_icr_idle = native_apic_wait_icr_idle,
548 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
61b90b7c 549};