2 * Written by: Patricia Gaughen, IBM Corporation
4 * Copyright (C) 2002, IBM Corp.
5 * Copyright (C) 2009, Red Hat, Inc., Ingo Molnar
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.
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
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.
24 * Send feedback to <gone@us.ibm.com>
26 #include <linux/nodemask.h>
27 #include <linux/topology.h>
28 #include <linux/bootmem.h>
29 #include <linux/threads.h>
30 #include <linux/cpumask.h>
31 #include <linux/kernel.h>
32 #include <linux/mmzone.h>
33 #include <linux/module.h>
34 #include <linux/string.h>
35 #include <linux/init.h>
36 #include <linux/numa.h>
37 #include <linux/smp.h>
41 #include <asm/processor.h>
42 #include <asm/fixmap.h>
43 #include <asm/mpspec.h>
44 #include <asm/numaq.h>
45 #include <asm/setup.h>
50 #define MB_TO_PAGES(addr) ((addr) << (20 - PAGE_SHIFT))
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
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
;
69 /* x86_quirks member */
70 static int mpc_record
;
72 static struct mpc_trans
*translation_table
[MAX_MPC_ENTRY
];
74 int mp_bus_id_to_node
[MAX_MP_BUSSES
];
75 int mp_bus_id_to_local
[MAX_MP_BUSSES
];
76 int quad_local_to_mp_bus_id
[NR_CPUS
/4][4];
79 static inline void numaq_register_node(int node
, struct sys_cfg_data
*scd
)
81 struct eachquadmem
*eq
= scd
->eq
+ node
;
83 node_set_online(node
);
85 /* Convert to pages */
86 node_start_pfn
[node
] =
87 MB_TO_PAGES(eq
->hi_shrd_mem_start
- eq
->priv_mem_size
);
90 MB_TO_PAGES(eq
->hi_shrd_mem_start
+ eq
->hi_shrd_mem_size
);
92 e820_register_active_regions(node
, node_start_pfn
[node
],
95 memory_present(node
, node_start_pfn
[node
], node_end_pfn
[node
]);
97 node_remap_size
[node
] = node_memmap_size_bytes(node
,
103 * Function: smp_dump_qct()
105 * Description: gets memory layout from the quad config table. This
106 * function also updates node_online_map with the nodes (quads) present.
108 static void __init
smp_dump_qct(void)
110 struct sys_cfg_data
*scd
;
113 scd
= (void *)__va(SYS_CFG_DATA_PRIV_ADDR
);
115 nodes_clear(node_online_map
);
116 for_each_node(node
) {
117 if (scd
->quads_present31_0
& (1 << node
))
118 numaq_register_node(node
, scd
);
122 void __cpuinit
numaq_tsc_disable(void)
127 if (num_online_nodes() > 1) {
128 printk(KERN_DEBUG
"NUMAQ: disabling TSC\n");
129 setup_clear_cpu_cap(X86_FEATURE_TSC
);
133 static int __init
numaq_pre_time_init(void)
139 static inline int generate_logical_apicid(int quad
, int phys_apicid
)
141 return (quad
<< 4) + (phys_apicid
? phys_apicid
<< 1 : 1);
144 /* x86_quirks member */
145 static int mpc_apic_id(struct mpc_cpu
*m
)
147 int quad
= translation_table
[mpc_record
]->trans_quad
;
148 int logical_apicid
= generate_logical_apicid(quad
, m
->apicid
);
151 "Processor #%d %u:%u APIC version %d (quad %d, apic %d)\n",
152 m
->apicid
, (m
->cpufeature
& CPU_FAMILY_MASK
) >> 8,
153 (m
->cpufeature
& CPU_MODEL_MASK
) >> 4,
154 m
->apicver
, quad
, logical_apicid
);
156 return logical_apicid
;
159 /* x86_quirks member */
160 static void mpc_oem_bus_info(struct mpc_bus
*m
, char *name
)
162 int quad
= translation_table
[mpc_record
]->trans_quad
;
163 int local
= translation_table
[mpc_record
]->trans_local
;
165 mp_bus_id_to_node
[m
->busid
] = quad
;
166 mp_bus_id_to_local
[m
->busid
] = local
;
168 printk(KERN_INFO
"Bus #%d is %s (node %d)\n", m
->busid
, name
, quad
);
171 /* x86_quirks member */
172 static void mpc_oem_pci_bus(struct mpc_bus
*m
)
174 int quad
= translation_table
[mpc_record
]->trans_quad
;
175 int local
= translation_table
[mpc_record
]->trans_local
;
177 quad_local_to_mp_bus_id
[quad
][local
] = m
->busid
;
180 static void __init
MP_translation_info(struct mpc_trans
*m
)
183 "Translation: record %d, type %d, quad %d, global %d, local %d\n",
184 mpc_record
, m
->trans_type
, m
->trans_quad
, m
->trans_global
,
187 if (mpc_record
>= MAX_MPC_ENTRY
)
188 printk(KERN_ERR
"MAX_MPC_ENTRY exceeded!\n");
190 translation_table
[mpc_record
] = m
; /* stash this for later */
192 if (m
->trans_quad
< MAX_NUMNODES
&& !node_online(m
->trans_quad
))
193 node_set_online(m
->trans_quad
);
196 static int __init
mpf_checksum(unsigned char *mp
, int len
)
207 * Read/parse the MPC oem tables
210 smp_read_mpc_oem(struct mpc_oemtable
*oemtable
, unsigned short oemsize
)
212 int count
= sizeof(*oemtable
); /* the header size */
213 unsigned char *oemptr
= ((unsigned char *)oemtable
) + count
;
217 "Found an OEM MPC table at %8p - parsing it ... \n", oemtable
);
219 if (memcmp(oemtable
->signature
, MPC_OEM_SIGNATURE
, 4)) {
221 "SMP mpc oemtable: bad signature [%c%c%c%c]!\n",
222 oemtable
->signature
[0], oemtable
->signature
[1],
223 oemtable
->signature
[2], oemtable
->signature
[3]);
227 if (mpf_checksum((unsigned char *)oemtable
, oemtable
->length
)) {
228 printk(KERN_WARNING
"SMP oem mptable: checksum error!\n");
232 while (count
< oemtable
->length
) {
236 struct mpc_trans
*m
= (void *)oemptr
;
238 MP_translation_info(m
);
239 oemptr
+= sizeof(*m
);
246 "Unrecognised OEM table entry type! - %d\n",
253 static int __init
numaq_setup_ioapic_ids(void)
259 static struct x86_quirks numaq_x86_quirks __initdata
= {
260 .arch_pre_time_init
= numaq_pre_time_init
,
261 .arch_time_init
= NULL
,
262 .arch_pre_intr_init
= NULL
,
263 .arch_intr_init
= NULL
,
264 .arch_trap_init
= NULL
,
265 .mach_get_smp_config
= NULL
,
266 .mach_find_smp_config
= NULL
,
267 .mpc_record
= &mpc_record
,
268 .mpc_apic_id
= mpc_apic_id
,
269 .mpc_oem_bus_info
= mpc_oem_bus_info
,
270 .mpc_oem_pci_bus
= mpc_oem_pci_bus
,
271 .smp_read_mpc_oem
= smp_read_mpc_oem
,
272 .setup_ioapic_ids
= numaq_setup_ioapic_ids
,
275 static __init
void early_check_numaq(void)
278 * Find possible boot-time SMP configuration:
280 early_find_smp_config();
283 * get boot-time SMP configuration:
285 if (smp_found_config
)
286 early_get_smp_config();
289 x86_quirks
= &numaq_x86_quirks
;
292 int __init
get_memcfg_numaq(void)
302 #define NUMAQ_APIC_DFR_VALUE (APIC_DFR_CLUSTER)
304 static inline unsigned int numaq_get_apic_id(unsigned long x
)
306 return (x
>> 24) & 0x0F;
309 static inline void numaq_send_IPI_mask(const struct cpumask
*mask
, int vector
)
311 default_send_IPI_mask_sequence_logical(mask
, vector
);
314 static inline void numaq_send_IPI_allbutself(int vector
)
316 default_send_IPI_mask_allbutself_logical(cpu_online_mask
, vector
);
319 static inline void numaq_send_IPI_all(int vector
)
321 numaq_send_IPI_mask(cpu_online_mask
, vector
);
324 #define NUMAQ_TRAMPOLINE_PHYS_LOW (0x8)
325 #define NUMAQ_TRAMPOLINE_PHYS_HIGH (0xa)
328 * Because we use NMIs rather than the INIT-STARTUP sequence to
329 * bootstrap the CPUs, the APIC may be in a weird state. Kick it:
331 static inline void numaq_smp_callin_clear_local_apic(void)
336 static inline const struct cpumask
*numaq_target_cpus(void)
341 static inline unsigned long
342 numaq_check_apicid_used(physid_mask_t bitmap
, int apicid
)
344 return physid_isset(apicid
, bitmap
);
347 static inline unsigned long numaq_check_apicid_present(int bit
)
349 return physid_isset(bit
, phys_cpu_present_map
);
352 static inline int numaq_apic_id_registered(void)
357 static inline void numaq_init_apic_ldr(void)
359 /* Already done in NUMA-Q firmware */
362 static inline void numaq_setup_apic_routing(void)
365 "Enabling APIC mode: NUMA-Q. Using %d I/O APICs\n",
370 * Skip adding the timer int on secondary nodes, which causes
371 * a small but painful rift in the time-space continuum.
373 static inline int numaq_multi_timer_check(int apic
, int irq
)
375 return apic
!= 0 && irq
== 0;
378 static inline physid_mask_t
numaq_ioapic_phys_id_map(physid_mask_t phys_map
)
380 /* We don't have a good way to do this yet - hack */
381 return physids_promote(0xFUL
);
384 static inline int numaq_cpu_to_logical_apicid(int cpu
)
386 if (cpu
>= nr_cpu_ids
)
388 return cpu_2_logical_apicid
[cpu
];
392 * Supporting over 60 cpus on NUMA-Q requires a locality-dependent
393 * cpu to APIC ID relation to properly interact with the intelligent
394 * mode of the cluster controller.
396 static inline int numaq_cpu_present_to_apicid(int mps_cpu
)
399 return ((mps_cpu
>> 2) << 4) | (1 << (mps_cpu
& 0x3));
404 static inline int numaq_apicid_to_node(int logical_apicid
)
406 return logical_apicid
>> 4;
409 static inline physid_mask_t
numaq_apicid_to_cpu_present(int logical_apicid
)
411 int node
= numaq_apicid_to_node(logical_apicid
);
412 int cpu
= __ffs(logical_apicid
& 0xf);
414 return physid_mask_of_physid(cpu
+ 4*node
);
417 /* Where the IO area was mapped on multiquad, always 0 otherwise */
420 static inline int numaq_check_phys_apicid_present(int boot_cpu_physical_apicid
)
426 * We use physical apicids here, not logical, so just return the default
427 * physical broadcast to stop people from breaking us
429 static unsigned int numaq_cpu_mask_to_apicid(const struct cpumask
*cpumask
)
434 static inline unsigned int
435 numaq_cpu_mask_to_apicid_and(const struct cpumask
*cpumask
,
436 const struct cpumask
*andmask
)
441 /* No NUMA-Q box has a HT CPU, but it can't hurt to use the default code. */
442 static inline int numaq_phys_pkg_id(int cpuid_apic
, int index_msb
)
444 return cpuid_apic
>> index_msb
;
448 numaq_mps_oem_check(struct mpc_table
*mpc
, char *oem
, char *productid
)
450 if (strncmp(oem
, "IBM NUMA", 8))
451 printk(KERN_ERR
"Warning! Not a NUMA-Q system!\n");
458 static int probe_numaq(void)
460 /* already know from get_memcfg_numaq() */
464 static void numaq_vector_allocation_domain(int cpu
, struct cpumask
*retmask
)
466 /* Careful. Some cpus do not strictly honor the set of cpus
467 * specified in the interrupt destination when using lowest
468 * priority interrupt delivery mode.
470 * In particular there was a hyperthreading cpu observed to
471 * deliver interrupts to the wrong hyperthread when only one
472 * hyperthread was specified in the interrupt desitination.
474 cpumask_clear(retmask
);
475 cpumask_bits(retmask
)[0] = APIC_ALL_CPUS
;
478 static void numaq_setup_portio_remap(void)
480 int num_quads
= num_online_nodes();
486 "Remapping cross-quad port I/O for %d quads\n", num_quads
);
488 xquad_portio
= ioremap(XQUAD_PORTIO_BASE
, num_quads
*XQUAD_PORTIO_QUAD
);
491 "xquad_portio vaddr 0x%08lx, len %08lx\n",
492 (u_long
) xquad_portio
, (u_long
) num_quads
*XQUAD_PORTIO_QUAD
);
495 /* Use __refdata to keep false positive warning calm. */
496 struct apic __refdata apic_numaq
= {
499 .probe
= probe_numaq
,
500 .acpi_madt_oem_check
= NULL
,
501 .apic_id_registered
= numaq_apic_id_registered
,
503 .irq_delivery_mode
= dest_LowestPrio
,
504 /* physical delivery on LOCAL quad: */
507 .target_cpus
= numaq_target_cpus
,
509 .dest_logical
= APIC_DEST_LOGICAL
,
510 .check_apicid_used
= numaq_check_apicid_used
,
511 .check_apicid_present
= numaq_check_apicid_present
,
513 .vector_allocation_domain
= numaq_vector_allocation_domain
,
514 .init_apic_ldr
= numaq_init_apic_ldr
,
516 .ioapic_phys_id_map
= numaq_ioapic_phys_id_map
,
517 .setup_apic_routing
= numaq_setup_apic_routing
,
518 .multi_timer_check
= numaq_multi_timer_check
,
519 .apicid_to_node
= numaq_apicid_to_node
,
520 .cpu_to_logical_apicid
= numaq_cpu_to_logical_apicid
,
521 .cpu_present_to_apicid
= numaq_cpu_present_to_apicid
,
522 .apicid_to_cpu_present
= numaq_apicid_to_cpu_present
,
523 .setup_portio_remap
= numaq_setup_portio_remap
,
524 .check_phys_apicid_present
= numaq_check_phys_apicid_present
,
525 .enable_apic_mode
= NULL
,
526 .phys_pkg_id
= numaq_phys_pkg_id
,
527 .mps_oem_check
= numaq_mps_oem_check
,
529 .get_apic_id
= numaq_get_apic_id
,
531 .apic_id_mask
= 0x0F << 24,
533 .cpu_mask_to_apicid
= numaq_cpu_mask_to_apicid
,
534 .cpu_mask_to_apicid_and
= numaq_cpu_mask_to_apicid_and
,
536 .send_IPI_mask
= numaq_send_IPI_mask
,
537 .send_IPI_mask_allbutself
= NULL
,
538 .send_IPI_allbutself
= numaq_send_IPI_allbutself
,
539 .send_IPI_all
= numaq_send_IPI_all
,
540 .send_IPI_self
= default_send_IPI_self
,
542 .wakeup_secondary_cpu
= wakeup_secondary_cpu_via_nmi
,
543 .trampoline_phys_low
= NUMAQ_TRAMPOLINE_PHYS_LOW
,
544 .trampoline_phys_high
= NUMAQ_TRAMPOLINE_PHYS_HIGH
,
546 /* We don't do anything here because we use NMI's to boot instead */
547 .wait_for_init_deassert
= NULL
,
549 .smp_callin_clear_local_apic
= numaq_smp_callin_clear_local_apic
,
550 .inquire_remote_apic
= NULL
,
552 .read
= native_apic_mem_read
,
553 .write
= native_apic_mem_write
,
554 .icr_read
= native_apic_icr_read
,
555 .icr_write
= native_apic_icr_write
,
556 .wait_icr_idle
= native_apic_wait_icr_idle
,
557 .safe_wait_icr_idle
= native_safe_apic_wait_icr_idle
,