Michael Ellerman | 0332c2d | 2006-12-05 17:52:36 +1100 | [diff] [blame] | 1 | /* |
| 2 | * pseries CPU Hotplug infrastructure. |
| 3 | * |
Michael Ellerman | 413f7c4 | 2006-12-05 17:52:38 +1100 | [diff] [blame] | 4 | * Split out from arch/powerpc/platforms/pseries/setup.c |
| 5 | * arch/powerpc/kernel/rtas.c, and arch/powerpc/platforms/pseries/smp.c |
Michael Ellerman | 0332c2d | 2006-12-05 17:52:36 +1100 | [diff] [blame] | 6 | * |
| 7 | * Peter Bergner, IBM March 2001. |
| 8 | * Copyright (C) 2001 IBM. |
Michael Ellerman | 413f7c4 | 2006-12-05 17:52:38 +1100 | [diff] [blame] | 9 | * Dave Engebretsen, Peter Bergner, and |
| 10 | * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com |
| 11 | * Plus various changes from other IBM teams... |
Michael Ellerman | 0332c2d | 2006-12-05 17:52:36 +1100 | [diff] [blame] | 12 | * |
| 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" |
| 32 | |
| 33 | /* This version can't take the spinlock, because it never returns */ |
| 34 | static struct rtas_args rtas_stop_self_args = { |
| 35 | .token = RTAS_UNKNOWN_SERVICE, |
| 36 | .nargs = 0, |
| 37 | .nret = 1, |
| 38 | .rets = &rtas_stop_self_args.args[0], |
| 39 | }; |
| 40 | |
Michael Ellerman | 04da6af | 2006-12-05 17:52:37 +1100 | [diff] [blame] | 41 | static void rtas_stop_self(void) |
Michael Ellerman | 0332c2d | 2006-12-05 17:52:36 +1100 | [diff] [blame] | 42 | { |
| 43 | struct rtas_args *args = &rtas_stop_self_args; |
| 44 | |
| 45 | local_irq_disable(); |
| 46 | |
| 47 | BUG_ON(args->token == RTAS_UNKNOWN_SERVICE); |
| 48 | |
| 49 | printk("cpu %u (hwid %u) Ready to die...\n", |
| 50 | smp_processor_id(), hard_smp_processor_id()); |
| 51 | enter_rtas(__pa(args)); |
| 52 | |
| 53 | panic("Alas, I survived.\n"); |
| 54 | } |
| 55 | |
Michael Ellerman | 04da6af | 2006-12-05 17:52:37 +1100 | [diff] [blame] | 56 | static void pSeries_mach_cpu_die(void) |
| 57 | { |
| 58 | local_irq_disable(); |
| 59 | idle_task_exit(); |
| 60 | xics_teardown_cpu(0); |
| 61 | rtas_stop_self(); |
| 62 | /* Should never get here... */ |
| 63 | BUG(); |
| 64 | for(;;); |
| 65 | } |
| 66 | |
Michael Ellerman | 674fa67 | 2006-12-05 17:52:38 +1100 | [diff] [blame^] | 67 | static int qcss_tok; /* query-cpu-stopped-state token */ |
| 68 | |
Michael Ellerman | 413f7c4 | 2006-12-05 17:52:38 +1100 | [diff] [blame] | 69 | /* Get state of physical CPU. |
| 70 | * Return codes: |
| 71 | * 0 - The processor is in the RTAS stopped state |
| 72 | * 1 - stop-self is in progress |
| 73 | * 2 - The processor is not in the RTAS stopped state |
| 74 | * -1 - Hardware Error |
| 75 | * -2 - Hardware Busy, Try again later. |
| 76 | */ |
| 77 | static int query_cpu_stopped(unsigned int pcpu) |
| 78 | { |
Michael Ellerman | 674fa67 | 2006-12-05 17:52:38 +1100 | [diff] [blame^] | 79 | int cpu_status, status; |
Michael Ellerman | 413f7c4 | 2006-12-05 17:52:38 +1100 | [diff] [blame] | 80 | |
Michael Ellerman | 413f7c4 | 2006-12-05 17:52:38 +1100 | [diff] [blame] | 81 | status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu); |
| 82 | if (status != 0) { |
| 83 | printk(KERN_ERR |
| 84 | "RTAS query-cpu-stopped-state failed: %i\n", status); |
| 85 | return status; |
| 86 | } |
| 87 | |
| 88 | return cpu_status; |
| 89 | } |
| 90 | |
| 91 | static int pSeries_cpu_disable(void) |
| 92 | { |
| 93 | int cpu = smp_processor_id(); |
| 94 | |
| 95 | cpu_clear(cpu, cpu_online_map); |
| 96 | vdso_data->processorCount--; |
| 97 | |
| 98 | /*fix boot_cpuid here*/ |
| 99 | if (cpu == boot_cpuid) |
| 100 | boot_cpuid = any_online_cpu(cpu_online_map); |
| 101 | |
| 102 | /* FIXME: abstract this to not be platform specific later on */ |
| 103 | xics_migrate_irqs_away(); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void pSeries_cpu_die(unsigned int cpu) |
| 108 | { |
| 109 | int tries; |
| 110 | int cpu_status; |
| 111 | unsigned int pcpu = get_hard_smp_processor_id(cpu); |
| 112 | |
| 113 | for (tries = 0; tries < 25; tries++) { |
| 114 | cpu_status = query_cpu_stopped(pcpu); |
| 115 | if (cpu_status == 0 || cpu_status == -1) |
| 116 | break; |
| 117 | msleep(200); |
| 118 | } |
| 119 | if (cpu_status != 0) { |
| 120 | printk("Querying DEAD? cpu %i (%i) shows %i\n", |
| 121 | cpu, pcpu, cpu_status); |
| 122 | } |
| 123 | |
| 124 | /* Isolation and deallocation are definatly done by |
| 125 | * drslot_chrp_cpu. If they were not they would be |
| 126 | * done here. Change isolate state to Isolate and |
| 127 | * change allocation-state to Unusable. |
| 128 | */ |
| 129 | paca[cpu].cpu_start = 0; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Update cpu_present_map and paca(s) for a new cpu node. The wrinkle |
| 134 | * here is that a cpu device node may represent up to two logical cpus |
| 135 | * in the SMT case. We must honor the assumption in other code that |
| 136 | * the logical ids for sibling SMT threads x and y are adjacent, such |
| 137 | * that x^1 == y and y^1 == x. |
| 138 | */ |
| 139 | static int pSeries_add_processor(struct device_node *np) |
| 140 | { |
| 141 | unsigned int cpu; |
| 142 | cpumask_t candidate_map, tmp = CPU_MASK_NONE; |
| 143 | int err = -ENOSPC, len, nthreads, i; |
| 144 | const u32 *intserv; |
| 145 | |
| 146 | intserv = get_property(np, "ibm,ppc-interrupt-server#s", &len); |
| 147 | if (!intserv) |
| 148 | return 0; |
| 149 | |
| 150 | nthreads = len / sizeof(u32); |
| 151 | for (i = 0; i < nthreads; i++) |
| 152 | cpu_set(i, tmp); |
| 153 | |
| 154 | lock_cpu_hotplug(); |
| 155 | |
| 156 | BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map)); |
| 157 | |
| 158 | /* Get a bitmap of unoccupied slots. */ |
| 159 | cpus_xor(candidate_map, cpu_possible_map, cpu_present_map); |
| 160 | if (cpus_empty(candidate_map)) { |
| 161 | /* If we get here, it most likely means that NR_CPUS is |
| 162 | * less than the partition's max processors setting. |
| 163 | */ |
| 164 | printk(KERN_ERR "Cannot add cpu %s; this system configuration" |
| 165 | " supports %d logical cpus.\n", np->full_name, |
| 166 | cpus_weight(cpu_possible_map)); |
| 167 | goto out_unlock; |
| 168 | } |
| 169 | |
| 170 | while (!cpus_empty(tmp)) |
| 171 | if (cpus_subset(tmp, candidate_map)) |
| 172 | /* Found a range where we can insert the new cpu(s) */ |
| 173 | break; |
| 174 | else |
| 175 | cpus_shift_left(tmp, tmp, nthreads); |
| 176 | |
| 177 | if (cpus_empty(tmp)) { |
| 178 | printk(KERN_ERR "Unable to find space in cpu_present_map for" |
| 179 | " processor %s with %d thread(s)\n", np->name, |
| 180 | nthreads); |
| 181 | goto out_unlock; |
| 182 | } |
| 183 | |
| 184 | for_each_cpu_mask(cpu, tmp) { |
| 185 | BUG_ON(cpu_isset(cpu, cpu_present_map)); |
| 186 | cpu_set(cpu, cpu_present_map); |
| 187 | set_hard_smp_processor_id(cpu, *intserv++); |
| 188 | } |
| 189 | err = 0; |
| 190 | out_unlock: |
| 191 | unlock_cpu_hotplug(); |
| 192 | return err; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Update the present map for a cpu node which is going away, and set |
| 197 | * the hard id in the paca(s) to -1 to be consistent with boot time |
| 198 | * convention for non-present cpus. |
| 199 | */ |
| 200 | static void pSeries_remove_processor(struct device_node *np) |
| 201 | { |
| 202 | unsigned int cpu; |
| 203 | int len, nthreads, i; |
| 204 | const u32 *intserv; |
| 205 | |
| 206 | intserv = get_property(np, "ibm,ppc-interrupt-server#s", &len); |
| 207 | if (!intserv) |
| 208 | return; |
| 209 | |
| 210 | nthreads = len / sizeof(u32); |
| 211 | |
| 212 | lock_cpu_hotplug(); |
| 213 | for (i = 0; i < nthreads; i++) { |
| 214 | for_each_present_cpu(cpu) { |
| 215 | if (get_hard_smp_processor_id(cpu) != intserv[i]) |
| 216 | continue; |
| 217 | BUG_ON(cpu_online(cpu)); |
| 218 | cpu_clear(cpu, cpu_present_map); |
| 219 | set_hard_smp_processor_id(cpu, -1); |
| 220 | break; |
| 221 | } |
| 222 | if (cpu == NR_CPUS) |
| 223 | printk(KERN_WARNING "Could not find cpu to remove " |
| 224 | "with physical id 0x%x\n", intserv[i]); |
| 225 | } |
| 226 | unlock_cpu_hotplug(); |
| 227 | } |
| 228 | |
| 229 | static int pSeries_smp_notifier(struct notifier_block *nb, unsigned long action, void *node) |
| 230 | { |
| 231 | int err = NOTIFY_OK; |
| 232 | |
| 233 | switch (action) { |
| 234 | case PSERIES_RECONFIG_ADD: |
| 235 | if (pSeries_add_processor(node)) |
| 236 | err = NOTIFY_BAD; |
| 237 | break; |
| 238 | case PSERIES_RECONFIG_REMOVE: |
| 239 | pSeries_remove_processor(node); |
| 240 | break; |
| 241 | default: |
| 242 | err = NOTIFY_DONE; |
| 243 | break; |
| 244 | } |
| 245 | return err; |
| 246 | } |
| 247 | |
| 248 | static struct notifier_block pSeries_smp_nb = { |
| 249 | .notifier_call = pSeries_smp_notifier, |
| 250 | }; |
| 251 | |
Michael Ellerman | 0332c2d | 2006-12-05 17:52:36 +1100 | [diff] [blame] | 252 | static int __init pseries_cpu_hotplug_init(void) |
| 253 | { |
| 254 | rtas_stop_self_args.token = rtas_token("stop-self"); |
Michael Ellerman | 674fa67 | 2006-12-05 17:52:38 +1100 | [diff] [blame^] | 255 | qcss_tok = rtas_token("query-cpu-stopped-state"); |
| 256 | |
| 257 | if (rtas_stop_self_args.token == RTAS_UNKNOWN_SERVICE || |
| 258 | qcss_tok == RTAS_UNKNOWN_SERVICE) { |
| 259 | printk(KERN_INFO "CPU Hotplug not supported by firmware " |
| 260 | "- disabling.\n"); |
| 261 | return 0; |
| 262 | } |
Michael Ellerman | 0332c2d | 2006-12-05 17:52:36 +1100 | [diff] [blame] | 263 | |
Michael Ellerman | 04da6af | 2006-12-05 17:52:37 +1100 | [diff] [blame] | 264 | ppc_md.cpu_die = pSeries_mach_cpu_die; |
Michael Ellerman | 413f7c4 | 2006-12-05 17:52:38 +1100 | [diff] [blame] | 265 | smp_ops->cpu_disable = pSeries_cpu_disable; |
| 266 | smp_ops->cpu_die = pSeries_cpu_die; |
| 267 | |
| 268 | /* Processors can be added/removed only on LPAR */ |
| 269 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
| 270 | pSeries_reconfig_notifier_register(&pSeries_smp_nb); |
| 271 | |
Michael Ellerman | 0332c2d | 2006-12-05 17:52:36 +1100 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | arch_initcall(pseries_cpu_hotplug_init); |