blob: a20ead87153db18566a2034179a9041ea6cc456d [file] [log] [blame]
Michael Ellerman0332c2d2006-12-05 17:52:36 +11001/*
2 * pseries CPU Hotplug infrastructure.
3 *
Michael Ellerman413f7c42006-12-05 17:52:38 +11004 * Split out from arch/powerpc/platforms/pseries/setup.c
5 * arch/powerpc/kernel/rtas.c, and arch/powerpc/platforms/pseries/smp.c
Michael Ellerman0332c2d2006-12-05 17:52:36 +11006 *
7 * Peter Bergner, IBM March 2001.
8 * Copyright (C) 2001 IBM.
Michael Ellerman413f7c42006-12-05 17:52:38 +11009 * Dave Engebretsen, Peter Bergner, and
10 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
11 * Plus various changes from other IBM teams...
Michael Ellerman0332c2d2006-12-05 17:52:36 +110012 *
13 * Copyright (C) 2006 Michael Ellerman, IBM Corporation
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21#include <linux/kernel.h>
22#include <linux/delay.h>
23#include <linux/cpu.h>
24#include <asm/system.h>
25#include <asm/prom.h>
26#include <asm/rtas.h>
27#include <asm/firmware.h>
28#include <asm/machdep.h>
29#include <asm/vdso_datapage.h>
30#include <asm/pSeries_reconfig.h>
31#include "xics.h"
Michael Neuling473980a2008-01-11 14:02:47 +110032#include "plpar_wrappers.h"
Michael Ellerman0332c2d2006-12-05 17:52:36 +110033
34/* This version can't take the spinlock, because it never returns */
35static struct rtas_args rtas_stop_self_args = {
36 .token = RTAS_UNKNOWN_SERVICE,
37 .nargs = 0,
38 .nret = 1,
39 .rets = &rtas_stop_self_args.args[0],
40};
41
Michael Ellerman04da6af2006-12-05 17:52:37 +110042static void rtas_stop_self(void)
Michael Ellerman0332c2d2006-12-05 17:52:36 +110043{
44 struct rtas_args *args = &rtas_stop_self_args;
45
46 local_irq_disable();
47
48 BUG_ON(args->token == RTAS_UNKNOWN_SERVICE);
49
50 printk("cpu %u (hwid %u) Ready to die...\n",
51 smp_processor_id(), hard_smp_processor_id());
52 enter_rtas(__pa(args));
53
54 panic("Alas, I survived.\n");
55}
56
Michael Ellerman06ba30b2006-12-05 17:52:39 +110057static void pseries_mach_cpu_die(void)
Michael Ellerman04da6af2006-12-05 17:52:37 +110058{
59 local_irq_disable();
60 idle_task_exit();
Nathan Fontenotc3e85062008-02-07 07:37:31 +110061 xics_teardown_cpu();
Michael Neuling473980a2008-01-11 14:02:47 +110062 unregister_slb_shadow(hard_smp_processor_id(), __pa(get_slb_shadow()));
Michael Ellerman04da6af2006-12-05 17:52:37 +110063 rtas_stop_self();
64 /* Should never get here... */
65 BUG();
66 for(;;);
67}
68
Michael Ellerman674fa672006-12-05 17:52:38 +110069static int qcss_tok; /* query-cpu-stopped-state token */
70
Michael Ellerman413f7c42006-12-05 17:52:38 +110071/* Get state of physical CPU.
72 * Return codes:
73 * 0 - The processor is in the RTAS stopped state
74 * 1 - stop-self is in progress
75 * 2 - The processor is not in the RTAS stopped state
76 * -1 - Hardware Error
77 * -2 - Hardware Busy, Try again later.
78 */
79static int query_cpu_stopped(unsigned int pcpu)
80{
Michael Ellerman674fa672006-12-05 17:52:38 +110081 int cpu_status, status;
Michael Ellerman413f7c42006-12-05 17:52:38 +110082
Michael Ellerman413f7c42006-12-05 17:52:38 +110083 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
84 if (status != 0) {
85 printk(KERN_ERR
86 "RTAS query-cpu-stopped-state failed: %i\n", status);
87 return status;
88 }
89
90 return cpu_status;
91}
92
Michael Ellerman06ba30b2006-12-05 17:52:39 +110093static int pseries_cpu_disable(void)
Michael Ellerman413f7c42006-12-05 17:52:38 +110094{
95 int cpu = smp_processor_id();
96
97 cpu_clear(cpu, cpu_online_map);
98 vdso_data->processorCount--;
99
100 /*fix boot_cpuid here*/
101 if (cpu == boot_cpuid)
102 boot_cpuid = any_online_cpu(cpu_online_map);
103
104 /* FIXME: abstract this to not be platform specific later on */
105 xics_migrate_irqs_away();
106 return 0;
107}
108
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100109static void pseries_cpu_die(unsigned int cpu)
Michael Ellerman413f7c42006-12-05 17:52:38 +1100110{
111 int tries;
112 int cpu_status;
113 unsigned int pcpu = get_hard_smp_processor_id(cpu);
114
115 for (tries = 0; tries < 25; tries++) {
116 cpu_status = query_cpu_stopped(pcpu);
117 if (cpu_status == 0 || cpu_status == -1)
118 break;
Sebastien Dugueb906cfa2008-11-27 00:59:52 +0000119 cpu_relax();
Michael Ellerman413f7c42006-12-05 17:52:38 +1100120 }
121 if (cpu_status != 0) {
122 printk("Querying DEAD? cpu %i (%i) shows %i\n",
123 cpu, pcpu, cpu_status);
124 }
125
126 /* Isolation and deallocation are definatly done by
127 * drslot_chrp_cpu. If they were not they would be
128 * done here. Change isolate state to Isolate and
129 * change allocation-state to Unusable.
130 */
131 paca[cpu].cpu_start = 0;
132}
133
134/*
135 * Update cpu_present_map and paca(s) for a new cpu node. The wrinkle
136 * here is that a cpu device node may represent up to two logical cpus
137 * in the SMT case. We must honor the assumption in other code that
138 * the logical ids for sibling SMT threads x and y are adjacent, such
139 * that x^1 == y and y^1 == x.
140 */
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100141static int pseries_add_processor(struct device_node *np)
Michael Ellerman413f7c42006-12-05 17:52:38 +1100142{
143 unsigned int cpu;
144 cpumask_t candidate_map, tmp = CPU_MASK_NONE;
145 int err = -ENOSPC, len, nthreads, i;
146 const u32 *intserv;
147
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000148 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
Michael Ellerman413f7c42006-12-05 17:52:38 +1100149 if (!intserv)
150 return 0;
151
152 nthreads = len / sizeof(u32);
153 for (i = 0; i < nthreads; i++)
154 cpu_set(i, tmp);
155
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100156 cpu_maps_update_begin();
Michael Ellerman413f7c42006-12-05 17:52:38 +1100157
158 BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
159
160 /* Get a bitmap of unoccupied slots. */
161 cpus_xor(candidate_map, cpu_possible_map, cpu_present_map);
162 if (cpus_empty(candidate_map)) {
163 /* If we get here, it most likely means that NR_CPUS is
164 * less than the partition's max processors setting.
165 */
166 printk(KERN_ERR "Cannot add cpu %s; this system configuration"
167 " supports %d logical cpus.\n", np->full_name,
168 cpus_weight(cpu_possible_map));
169 goto out_unlock;
170 }
171
172 while (!cpus_empty(tmp))
173 if (cpus_subset(tmp, candidate_map))
174 /* Found a range where we can insert the new cpu(s) */
175 break;
176 else
177 cpus_shift_left(tmp, tmp, nthreads);
178
179 if (cpus_empty(tmp)) {
180 printk(KERN_ERR "Unable to find space in cpu_present_map for"
181 " processor %s with %d thread(s)\n", np->name,
182 nthreads);
183 goto out_unlock;
184 }
185
186 for_each_cpu_mask(cpu, tmp) {
187 BUG_ON(cpu_isset(cpu, cpu_present_map));
188 cpu_set(cpu, cpu_present_map);
189 set_hard_smp_processor_id(cpu, *intserv++);
190 }
191 err = 0;
192out_unlock:
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100193 cpu_maps_update_done();
Michael Ellerman413f7c42006-12-05 17:52:38 +1100194 return err;
195}
196
197/*
198 * Update the present map for a cpu node which is going away, and set
199 * the hard id in the paca(s) to -1 to be consistent with boot time
200 * convention for non-present cpus.
201 */
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100202static void pseries_remove_processor(struct device_node *np)
Michael Ellerman413f7c42006-12-05 17:52:38 +1100203{
204 unsigned int cpu;
205 int len, nthreads, i;
206 const u32 *intserv;
207
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000208 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
Michael Ellerman413f7c42006-12-05 17:52:38 +1100209 if (!intserv)
210 return;
211
212 nthreads = len / sizeof(u32);
213
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100214 cpu_maps_update_begin();
Michael Ellerman413f7c42006-12-05 17:52:38 +1100215 for (i = 0; i < nthreads; i++) {
216 for_each_present_cpu(cpu) {
217 if (get_hard_smp_processor_id(cpu) != intserv[i])
218 continue;
219 BUG_ON(cpu_online(cpu));
220 cpu_clear(cpu, cpu_present_map);
221 set_hard_smp_processor_id(cpu, -1);
222 break;
223 }
224 if (cpu == NR_CPUS)
225 printk(KERN_WARNING "Could not find cpu to remove "
226 "with physical id 0x%x\n", intserv[i]);
227 }
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100228 cpu_maps_update_done();
Michael Ellerman413f7c42006-12-05 17:52:38 +1100229}
230
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100231static int pseries_smp_notifier(struct notifier_block *nb,
232 unsigned long action, void *node)
Michael Ellerman413f7c42006-12-05 17:52:38 +1100233{
234 int err = NOTIFY_OK;
235
236 switch (action) {
237 case PSERIES_RECONFIG_ADD:
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100238 if (pseries_add_processor(node))
Michael Ellerman413f7c42006-12-05 17:52:38 +1100239 err = NOTIFY_BAD;
240 break;
241 case PSERIES_RECONFIG_REMOVE:
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100242 pseries_remove_processor(node);
Michael Ellerman413f7c42006-12-05 17:52:38 +1100243 break;
244 default:
245 err = NOTIFY_DONE;
246 break;
247 }
248 return err;
249}
250
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100251static struct notifier_block pseries_smp_nb = {
252 .notifier_call = pseries_smp_notifier,
Michael Ellerman413f7c42006-12-05 17:52:38 +1100253};
254
Michael Ellerman0332c2d2006-12-05 17:52:36 +1100255static int __init pseries_cpu_hotplug_init(void)
256{
Olof Johansson64f27582007-10-10 10:38:24 +1000257 struct device_node *np;
258 const char *typep;
259
260 for_each_node_by_name(np, "interrupt-controller") {
261 typep = of_get_property(np, "compatible", NULL);
262 if (strstr(typep, "open-pic")) {
263 of_node_put(np);
264
265 printk(KERN_INFO "CPU Hotplug not supported on "
266 "systems using MPIC\n");
267 return 0;
268 }
269 }
270
Michael Ellerman0332c2d2006-12-05 17:52:36 +1100271 rtas_stop_self_args.token = rtas_token("stop-self");
Michael Ellerman674fa672006-12-05 17:52:38 +1100272 qcss_tok = rtas_token("query-cpu-stopped-state");
273
274 if (rtas_stop_self_args.token == RTAS_UNKNOWN_SERVICE ||
275 qcss_tok == RTAS_UNKNOWN_SERVICE) {
276 printk(KERN_INFO "CPU Hotplug not supported by firmware "
277 "- disabling.\n");
278 return 0;
279 }
Michael Ellerman0332c2d2006-12-05 17:52:36 +1100280
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100281 ppc_md.cpu_die = pseries_mach_cpu_die;
282 smp_ops->cpu_disable = pseries_cpu_disable;
283 smp_ops->cpu_die = pseries_cpu_die;
Michael Ellerman413f7c42006-12-05 17:52:38 +1100284
285 /* Processors can be added/removed only on LPAR */
286 if (firmware_has_feature(FW_FEATURE_LPAR))
Michael Ellerman06ba30b2006-12-05 17:52:39 +1100287 pSeries_reconfig_notifier_register(&pseries_smp_nb);
Michael Ellerman413f7c42006-12-05 17:52:38 +1100288
Michael Ellerman0332c2d2006-12-05 17:52:36 +1100289 return 0;
290}
291arch_initcall(pseries_cpu_hotplug_init);