1 /* 2 * Support for dynamic reconfiguration for PCI, Memory, and CPU 3 * Hotplug and Dynamic Logical Partitioning on RPA platforms. 4 * 5 * Copyright (C) 2009 Nathan Fontenot 6 * Copyright (C) 2009 IBM Corporation 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License version 10 * 2 as published by the Free Software Foundation. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/kref.h> 15 #include <linux/notifier.h> 16 #include <linux/proc_fs.h> 17 #include <linux/spinlock.h> 18 #include <linux/cpu.h> 19 #include <linux/slab.h> 20 #include "offline_states.h" 21 22 #include <asm/prom.h> 23 #include <asm/machdep.h> 24 #include <asm/uaccess.h> 25 #include <asm/rtas.h> 26 #include <asm/pSeries_reconfig.h> 27 28 struct cc_workarea { 29 u32 drc_index; 30 u32 zero; 31 u32 name_offset; 32 u32 prop_length; 33 u32 prop_offset; 34 }; 35 36 void dlpar_free_cc_property(struct property *prop) 37 { 38 kfree(prop->name); 39 kfree(prop->value); 40 kfree(prop); 41 } 42 43 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa) 44 { 45 struct property *prop; 46 char *name; 47 char *value; 48 49 prop = kzalloc(sizeof(*prop), GFP_KERNEL); 50 if (!prop) 51 return NULL; 52 53 name = (char *)ccwa + ccwa->name_offset; 54 prop->name = kstrdup(name, GFP_KERNEL); 55 56 prop->length = ccwa->prop_length; 57 value = (char *)ccwa + ccwa->prop_offset; 58 prop->value = kmemdup(value, prop->length, GFP_KERNEL); 59 if (!prop->value) { 60 dlpar_free_cc_property(prop); 61 return NULL; 62 } 63 64 return prop; 65 } 66 67 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa) 68 { 69 struct device_node *dn; 70 char *name; 71 72 dn = kzalloc(sizeof(*dn), GFP_KERNEL); 73 if (!dn) 74 return NULL; 75 76 /* The configure connector reported name does not contain a 77 * preceding '/', so we allocate a buffer large enough to 78 * prepend this to the full_name. 79 */ 80 name = (char *)ccwa + ccwa->name_offset; 81 dn->full_name = kasprintf(GFP_KERNEL, "/%s", name); 82 if (!dn->full_name) { 83 kfree(dn); 84 return NULL; 85 } 86 87 return dn; 88 } 89 90 static void dlpar_free_one_cc_node(struct device_node *dn) 91 { 92 struct property *prop; 93 94 while (dn->properties) { 95 prop = dn->properties; 96 dn->properties = prop->next; 97 dlpar_free_cc_property(prop); 98 } 99 100 kfree(dn->full_name); 101 kfree(dn); 102 } 103 104 void dlpar_free_cc_nodes(struct device_node *dn) 105 { 106 if (dn->child) 107 dlpar_free_cc_nodes(dn->child); 108 109 if (dn->sibling) 110 dlpar_free_cc_nodes(dn->sibling); 111 112 dlpar_free_one_cc_node(dn); 113 } 114 115 #define NEXT_SIBLING 1 116 #define NEXT_CHILD 2 117 #define NEXT_PROPERTY 3 118 #define PREV_PARENT 4 119 #define MORE_MEMORY 5 120 #define CALL_AGAIN -2 121 #define ERR_CFG_USE -9003 122 123 struct device_node *dlpar_configure_connector(u32 drc_index) 124 { 125 struct device_node *dn; 126 struct device_node *first_dn = NULL; 127 struct device_node *last_dn = NULL; 128 struct property *property; 129 struct property *last_property = NULL; 130 struct cc_workarea *ccwa; 131 char *data_buf; 132 int cc_token; 133 int rc = -1; 134 135 cc_token = rtas_token("ibm,configure-connector"); 136 if (cc_token == RTAS_UNKNOWN_SERVICE) 137 return NULL; 138 139 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); 140 if (!data_buf) 141 return NULL; 142 143 ccwa = (struct cc_workarea *)&data_buf[0]; 144 ccwa->drc_index = drc_index; 145 ccwa->zero = 0; 146 147 do { 148 /* Since we release the rtas_data_buf lock between configure 149 * connector calls we want to re-populate the rtas_data_buffer 150 * with the contents of the previous call. 151 */ 152 spin_lock(&rtas_data_buf_lock); 153 154 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE); 155 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL); 156 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE); 157 158 spin_unlock(&rtas_data_buf_lock); 159 160 switch (rc) { 161 case NEXT_SIBLING: 162 dn = dlpar_parse_cc_node(ccwa); 163 if (!dn) 164 goto cc_error; 165 166 dn->parent = last_dn->parent; 167 last_dn->sibling = dn; 168 last_dn = dn; 169 break; 170 171 case NEXT_CHILD: 172 dn = dlpar_parse_cc_node(ccwa); 173 if (!dn) 174 goto cc_error; 175 176 if (!first_dn) 177 first_dn = dn; 178 else { 179 dn->parent = last_dn; 180 if (last_dn) 181 last_dn->child = dn; 182 } 183 184 last_dn = dn; 185 break; 186 187 case NEXT_PROPERTY: 188 property = dlpar_parse_cc_property(ccwa); 189 if (!property) 190 goto cc_error; 191 192 if (!last_dn->properties) 193 last_dn->properties = property; 194 else 195 last_property->next = property; 196 197 last_property = property; 198 break; 199 200 case PREV_PARENT: 201 last_dn = last_dn->parent; 202 break; 203 204 case CALL_AGAIN: 205 break; 206 207 case MORE_MEMORY: 208 case ERR_CFG_USE: 209 default: 210 printk(KERN_ERR "Unexpected Error (%d) " 211 "returned from configure-connector\n", rc); 212 goto cc_error; 213 } 214 } while (rc); 215 216 cc_error: 217 kfree(data_buf); 218 219 if (rc) { 220 if (first_dn) 221 dlpar_free_cc_nodes(first_dn); 222 223 return NULL; 224 } 225 226 return first_dn; 227 } 228 229 static struct device_node *derive_parent(const char *path) 230 { 231 struct device_node *parent; 232 char *last_slash; 233 234 last_slash = strrchr(path, '/'); 235 if (last_slash == path) { 236 parent = of_find_node_by_path("/"); 237 } else { 238 char *parent_path; 239 int parent_path_len = last_slash - path + 1; 240 parent_path = kmalloc(parent_path_len, GFP_KERNEL); 241 if (!parent_path) 242 return NULL; 243 244 strlcpy(parent_path, path, parent_path_len); 245 parent = of_find_node_by_path(parent_path); 246 kfree(parent_path); 247 } 248 249 return parent; 250 } 251 252 int dlpar_attach_node(struct device_node *dn) 253 { 254 #ifdef CONFIG_PROC_DEVICETREE 255 struct proc_dir_entry *ent; 256 #endif 257 int rc; 258 259 of_node_set_flag(dn, OF_DYNAMIC); 260 kref_init(&dn->kref); 261 dn->parent = derive_parent(dn->full_name); 262 if (!dn->parent) 263 return -ENOMEM; 264 265 rc = pSeries_reconfig_notify(PSERIES_RECONFIG_ADD, dn); 266 if (rc) { 267 printk(KERN_ERR "Failed to add device node %s\n", 268 dn->full_name); 269 return rc; 270 } 271 272 of_attach_node(dn); 273 274 #ifdef CONFIG_PROC_DEVICETREE 275 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde); 276 if (ent) 277 proc_device_tree_add_node(dn, ent); 278 #endif 279 280 of_node_put(dn->parent); 281 return 0; 282 } 283 284 int dlpar_detach_node(struct device_node *dn) 285 { 286 #ifdef CONFIG_PROC_DEVICETREE 287 struct device_node *parent = dn->parent; 288 struct property *prop = dn->properties; 289 290 while (prop) { 291 remove_proc_entry(prop->name, dn->pde); 292 prop = prop->next; 293 } 294 295 if (dn->pde) 296 remove_proc_entry(dn->pde->name, parent->pde); 297 #endif 298 299 pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE, dn); 300 of_detach_node(dn); 301 of_node_put(dn); /* Must decrement the refcount */ 302 303 return 0; 304 } 305 306 #define DR_ENTITY_SENSE 9003 307 #define DR_ENTITY_PRESENT 1 308 #define DR_ENTITY_UNUSABLE 2 309 #define ALLOCATION_STATE 9003 310 #define ALLOC_UNUSABLE 0 311 #define ALLOC_USABLE 1 312 #define ISOLATION_STATE 9001 313 #define ISOLATE 0 314 #define UNISOLATE 1 315 316 int dlpar_acquire_drc(u32 drc_index) 317 { 318 int dr_status, rc; 319 320 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status, 321 DR_ENTITY_SENSE, drc_index); 322 if (rc || dr_status != DR_ENTITY_UNUSABLE) 323 return -1; 324 325 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE); 326 if (rc) 327 return rc; 328 329 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE); 330 if (rc) { 331 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE); 332 return rc; 333 } 334 335 return 0; 336 } 337 338 int dlpar_release_drc(u32 drc_index) 339 { 340 int dr_status, rc; 341 342 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status, 343 DR_ENTITY_SENSE, drc_index); 344 if (rc || dr_status != DR_ENTITY_PRESENT) 345 return -1; 346 347 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE); 348 if (rc) 349 return rc; 350 351 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE); 352 if (rc) { 353 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE); 354 return rc; 355 } 356 357 return 0; 358 } 359 360 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE 361 362 static int dlpar_online_cpu(struct device_node *dn) 363 { 364 int rc = 0; 365 unsigned int cpu; 366 int len, nthreads, i; 367 const u32 *intserv; 368 369 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len); 370 if (!intserv) 371 return -EINVAL; 372 373 nthreads = len / sizeof(u32); 374 375 cpu_maps_update_begin(); 376 for (i = 0; i < nthreads; i++) { 377 for_each_present_cpu(cpu) { 378 if (get_hard_smp_processor_id(cpu) != intserv[i]) 379 continue; 380 BUG_ON(get_cpu_current_state(cpu) 381 != CPU_STATE_OFFLINE); 382 cpu_maps_update_done(); 383 rc = cpu_up(cpu); 384 if (rc) 385 goto out; 386 cpu_maps_update_begin(); 387 388 break; 389 } 390 if (cpu == num_possible_cpus()) 391 printk(KERN_WARNING "Could not find cpu to online " 392 "with physical id 0x%x\n", intserv[i]); 393 } 394 cpu_maps_update_done(); 395 396 out: 397 return rc; 398 399 } 400 401 static ssize_t dlpar_cpu_probe(const char *buf, size_t count) 402 { 403 struct device_node *dn; 404 unsigned long drc_index; 405 char *cpu_name; 406 int rc; 407 408 cpu_hotplug_driver_lock(); 409 rc = strict_strtoul(buf, 0, &drc_index); 410 if (rc) { 411 rc = -EINVAL; 412 goto out; 413 } 414 415 dn = dlpar_configure_connector(drc_index); 416 if (!dn) { 417 rc = -EINVAL; 418 goto out; 419 } 420 421 /* configure-connector reports cpus as living in the base 422 * directory of the device tree. CPUs actually live in the 423 * cpus directory so we need to fixup the full_name. 424 */ 425 cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name); 426 if (!cpu_name) { 427 dlpar_free_cc_nodes(dn); 428 rc = -ENOMEM; 429 goto out; 430 } 431 432 kfree(dn->full_name); 433 dn->full_name = cpu_name; 434 435 rc = dlpar_acquire_drc(drc_index); 436 if (rc) { 437 dlpar_free_cc_nodes(dn); 438 rc = -EINVAL; 439 goto out; 440 } 441 442 rc = dlpar_attach_node(dn); 443 if (rc) { 444 dlpar_release_drc(drc_index); 445 dlpar_free_cc_nodes(dn); 446 goto out; 447 } 448 449 rc = dlpar_online_cpu(dn); 450 out: 451 cpu_hotplug_driver_unlock(); 452 453 return rc ? rc : count; 454 } 455 456 static int dlpar_offline_cpu(struct device_node *dn) 457 { 458 int rc = 0; 459 unsigned int cpu; 460 int len, nthreads, i; 461 const u32 *intserv; 462 463 intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len); 464 if (!intserv) 465 return -EINVAL; 466 467 nthreads = len / sizeof(u32); 468 469 cpu_maps_update_begin(); 470 for (i = 0; i < nthreads; i++) { 471 for_each_present_cpu(cpu) { 472 if (get_hard_smp_processor_id(cpu) != intserv[i]) 473 continue; 474 475 if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE) 476 break; 477 478 if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) { 479 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE); 480 cpu_maps_update_done(); 481 rc = cpu_down(cpu); 482 if (rc) 483 goto out; 484 cpu_maps_update_begin(); 485 break; 486 487 } 488 489 /* 490 * The cpu is in CPU_STATE_INACTIVE. 491 * Upgrade it's state to CPU_STATE_OFFLINE. 492 */ 493 set_preferred_offline_state(cpu, CPU_STATE_OFFLINE); 494 BUG_ON(plpar_hcall_norets(H_PROD, intserv[i]) 495 != H_SUCCESS); 496 __cpu_die(cpu); 497 break; 498 } 499 if (cpu == num_possible_cpus()) 500 printk(KERN_WARNING "Could not find cpu to offline " 501 "with physical id 0x%x\n", intserv[i]); 502 } 503 cpu_maps_update_done(); 504 505 out: 506 return rc; 507 508 } 509 510 static ssize_t dlpar_cpu_release(const char *buf, size_t count) 511 { 512 struct device_node *dn; 513 const u32 *drc_index; 514 int rc; 515 516 dn = of_find_node_by_path(buf); 517 if (!dn) 518 return -EINVAL; 519 520 drc_index = of_get_property(dn, "ibm,my-drc-index", NULL); 521 if (!drc_index) { 522 of_node_put(dn); 523 return -EINVAL; 524 } 525 526 cpu_hotplug_driver_lock(); 527 rc = dlpar_offline_cpu(dn); 528 if (rc) { 529 of_node_put(dn); 530 rc = -EINVAL; 531 goto out; 532 } 533 534 rc = dlpar_release_drc(*drc_index); 535 if (rc) { 536 of_node_put(dn); 537 goto out; 538 } 539 540 rc = dlpar_detach_node(dn); 541 if (rc) { 542 dlpar_acquire_drc(*drc_index); 543 goto out; 544 } 545 546 of_node_put(dn); 547 out: 548 cpu_hotplug_driver_unlock(); 549 return rc ? rc : count; 550 } 551 552 static int __init pseries_dlpar_init(void) 553 { 554 ppc_md.cpu_probe = dlpar_cpu_probe; 555 ppc_md.cpu_release = dlpar_cpu_release; 556 557 return 0; 558 } 559 machine_device_initcall(pseries, pseries_dlpar_init); 560 561 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */ 562