Path: blob/master/arch/powerpc/platforms/pseries/dlpar.c
10818 views
/*1* Support for dynamic reconfiguration for PCI, Memory, and CPU2* Hotplug and Dynamic Logical Partitioning on RPA platforms.3*4* Copyright (C) 2009 Nathan Fontenot5* Copyright (C) 2009 IBM Corporation6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU General Public License version9* 2 as published by the Free Software Foundation.10*/1112#include <linux/kernel.h>13#include <linux/kref.h>14#include <linux/notifier.h>15#include <linux/proc_fs.h>16#include <linux/spinlock.h>17#include <linux/cpu.h>18#include <linux/slab.h>19#include "offline_states.h"2021#include <asm/prom.h>22#include <asm/machdep.h>23#include <asm/uaccess.h>24#include <asm/rtas.h>25#include <asm/pSeries_reconfig.h>2627struct cc_workarea {28u32 drc_index;29u32 zero;30u32 name_offset;31u32 prop_length;32u32 prop_offset;33};3435void dlpar_free_cc_property(struct property *prop)36{37kfree(prop->name);38kfree(prop->value);39kfree(prop);40}4142static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)43{44struct property *prop;45char *name;46char *value;4748prop = kzalloc(sizeof(*prop), GFP_KERNEL);49if (!prop)50return NULL;5152name = (char *)ccwa + ccwa->name_offset;53prop->name = kstrdup(name, GFP_KERNEL);5455prop->length = ccwa->prop_length;56value = (char *)ccwa + ccwa->prop_offset;57prop->value = kmemdup(value, prop->length, GFP_KERNEL);58if (!prop->value) {59dlpar_free_cc_property(prop);60return NULL;61}6263return prop;64}6566static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)67{68struct device_node *dn;69char *name;7071dn = kzalloc(sizeof(*dn), GFP_KERNEL);72if (!dn)73return NULL;7475/* The configure connector reported name does not contain a76* preceding '/', so we allocate a buffer large enough to77* prepend this to the full_name.78*/79name = (char *)ccwa + ccwa->name_offset;80dn->full_name = kasprintf(GFP_KERNEL, "/%s", name);81if (!dn->full_name) {82kfree(dn);83return NULL;84}8586return dn;87}8889static void dlpar_free_one_cc_node(struct device_node *dn)90{91struct property *prop;9293while (dn->properties) {94prop = dn->properties;95dn->properties = prop->next;96dlpar_free_cc_property(prop);97}9899kfree(dn->full_name);100kfree(dn);101}102103void dlpar_free_cc_nodes(struct device_node *dn)104{105if (dn->child)106dlpar_free_cc_nodes(dn->child);107108if (dn->sibling)109dlpar_free_cc_nodes(dn->sibling);110111dlpar_free_one_cc_node(dn);112}113114#define NEXT_SIBLING 1115#define NEXT_CHILD 2116#define NEXT_PROPERTY 3117#define PREV_PARENT 4118#define MORE_MEMORY 5119#define CALL_AGAIN -2120#define ERR_CFG_USE -9003121122struct device_node *dlpar_configure_connector(u32 drc_index)123{124struct device_node *dn;125struct device_node *first_dn = NULL;126struct device_node *last_dn = NULL;127struct property *property;128struct property *last_property = NULL;129struct cc_workarea *ccwa;130char *data_buf;131int cc_token;132int rc = -1;133134cc_token = rtas_token("ibm,configure-connector");135if (cc_token == RTAS_UNKNOWN_SERVICE)136return NULL;137138data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);139if (!data_buf)140return NULL;141142ccwa = (struct cc_workarea *)&data_buf[0];143ccwa->drc_index = drc_index;144ccwa->zero = 0;145146do {147/* Since we release the rtas_data_buf lock between configure148* connector calls we want to re-populate the rtas_data_buffer149* with the contents of the previous call.150*/151spin_lock(&rtas_data_buf_lock);152153memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);154rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);155memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);156157spin_unlock(&rtas_data_buf_lock);158159switch (rc) {160case NEXT_SIBLING:161dn = dlpar_parse_cc_node(ccwa);162if (!dn)163goto cc_error;164165dn->parent = last_dn->parent;166last_dn->sibling = dn;167last_dn = dn;168break;169170case NEXT_CHILD:171dn = dlpar_parse_cc_node(ccwa);172if (!dn)173goto cc_error;174175if (!first_dn)176first_dn = dn;177else {178dn->parent = last_dn;179if (last_dn)180last_dn->child = dn;181}182183last_dn = dn;184break;185186case NEXT_PROPERTY:187property = dlpar_parse_cc_property(ccwa);188if (!property)189goto cc_error;190191if (!last_dn->properties)192last_dn->properties = property;193else194last_property->next = property;195196last_property = property;197break;198199case PREV_PARENT:200last_dn = last_dn->parent;201break;202203case CALL_AGAIN:204break;205206case MORE_MEMORY:207case ERR_CFG_USE:208default:209printk(KERN_ERR "Unexpected Error (%d) "210"returned from configure-connector\n", rc);211goto cc_error;212}213} while (rc);214215cc_error:216kfree(data_buf);217218if (rc) {219if (first_dn)220dlpar_free_cc_nodes(first_dn);221222return NULL;223}224225return first_dn;226}227228static struct device_node *derive_parent(const char *path)229{230struct device_node *parent;231char *last_slash;232233last_slash = strrchr(path, '/');234if (last_slash == path) {235parent = of_find_node_by_path("/");236} else {237char *parent_path;238int parent_path_len = last_slash - path + 1;239parent_path = kmalloc(parent_path_len, GFP_KERNEL);240if (!parent_path)241return NULL;242243strlcpy(parent_path, path, parent_path_len);244parent = of_find_node_by_path(parent_path);245kfree(parent_path);246}247248return parent;249}250251int dlpar_attach_node(struct device_node *dn)252{253#ifdef CONFIG_PROC_DEVICETREE254struct proc_dir_entry *ent;255#endif256int rc;257258of_node_set_flag(dn, OF_DYNAMIC);259kref_init(&dn->kref);260dn->parent = derive_parent(dn->full_name);261if (!dn->parent)262return -ENOMEM;263264rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,265PSERIES_RECONFIG_ADD, dn);266if (rc == NOTIFY_BAD) {267printk(KERN_ERR "Failed to add device node %s\n",268dn->full_name);269return -ENOMEM; /* For now, safe to assume kmalloc failure */270}271272of_attach_node(dn);273274#ifdef CONFIG_PROC_DEVICETREE275ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);276if (ent)277proc_device_tree_add_node(dn, ent);278#endif279280of_node_put(dn->parent);281return 0;282}283284int dlpar_detach_node(struct device_node *dn)285{286#ifdef CONFIG_PROC_DEVICETREE287struct device_node *parent = dn->parent;288struct property *prop = dn->properties;289290while (prop) {291remove_proc_entry(prop->name, dn->pde);292prop = prop->next;293}294295if (dn->pde)296remove_proc_entry(dn->pde->name, parent->pde);297#endif298299blocking_notifier_call_chain(&pSeries_reconfig_chain,300PSERIES_RECONFIG_REMOVE, dn);301of_detach_node(dn);302of_node_put(dn); /* Must decrement the refcount */303304return 0;305}306307#define DR_ENTITY_SENSE 9003308#define DR_ENTITY_PRESENT 1309#define DR_ENTITY_UNUSABLE 2310#define ALLOCATION_STATE 9003311#define ALLOC_UNUSABLE 0312#define ALLOC_USABLE 1313#define ISOLATION_STATE 9001314#define ISOLATE 0315#define UNISOLATE 1316317int dlpar_acquire_drc(u32 drc_index)318{319int dr_status, rc;320321rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,322DR_ENTITY_SENSE, drc_index);323if (rc || dr_status != DR_ENTITY_UNUSABLE)324return -1;325326rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);327if (rc)328return rc;329330rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);331if (rc) {332rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);333return rc;334}335336return 0;337}338339int dlpar_release_drc(u32 drc_index)340{341int dr_status, rc;342343rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,344DR_ENTITY_SENSE, drc_index);345if (rc || dr_status != DR_ENTITY_PRESENT)346return -1;347348rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);349if (rc)350return rc;351352rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);353if (rc) {354rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);355return rc;356}357358return 0;359}360361#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE362363static int dlpar_online_cpu(struct device_node *dn)364{365int rc = 0;366unsigned int cpu;367int len, nthreads, i;368const u32 *intserv;369370intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);371if (!intserv)372return -EINVAL;373374nthreads = len / sizeof(u32);375376cpu_maps_update_begin();377for (i = 0; i < nthreads; i++) {378for_each_present_cpu(cpu) {379if (get_hard_smp_processor_id(cpu) != intserv[i])380continue;381BUG_ON(get_cpu_current_state(cpu)382!= CPU_STATE_OFFLINE);383cpu_maps_update_done();384rc = cpu_up(cpu);385if (rc)386goto out;387cpu_maps_update_begin();388389break;390}391if (cpu == num_possible_cpus())392printk(KERN_WARNING "Could not find cpu to online "393"with physical id 0x%x\n", intserv[i]);394}395cpu_maps_update_done();396397out:398return rc;399400}401402static ssize_t dlpar_cpu_probe(const char *buf, size_t count)403{404struct device_node *dn;405unsigned long drc_index;406char *cpu_name;407int rc;408409cpu_hotplug_driver_lock();410rc = strict_strtoul(buf, 0, &drc_index);411if (rc) {412rc = -EINVAL;413goto out;414}415416dn = dlpar_configure_connector(drc_index);417if (!dn) {418rc = -EINVAL;419goto out;420}421422/* configure-connector reports cpus as living in the base423* directory of the device tree. CPUs actually live in the424* cpus directory so we need to fixup the full_name.425*/426cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);427if (!cpu_name) {428dlpar_free_cc_nodes(dn);429rc = -ENOMEM;430goto out;431}432433kfree(dn->full_name);434dn->full_name = cpu_name;435436rc = dlpar_acquire_drc(drc_index);437if (rc) {438dlpar_free_cc_nodes(dn);439rc = -EINVAL;440goto out;441}442443rc = dlpar_attach_node(dn);444if (rc) {445dlpar_release_drc(drc_index);446dlpar_free_cc_nodes(dn);447goto out;448}449450rc = dlpar_online_cpu(dn);451out:452cpu_hotplug_driver_unlock();453454return rc ? rc : count;455}456457static int dlpar_offline_cpu(struct device_node *dn)458{459int rc = 0;460unsigned int cpu;461int len, nthreads, i;462const u32 *intserv;463464intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);465if (!intserv)466return -EINVAL;467468nthreads = len / sizeof(u32);469470cpu_maps_update_begin();471for (i = 0; i < nthreads; i++) {472for_each_present_cpu(cpu) {473if (get_hard_smp_processor_id(cpu) != intserv[i])474continue;475476if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)477break;478479if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {480set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);481cpu_maps_update_done();482rc = cpu_down(cpu);483if (rc)484goto out;485cpu_maps_update_begin();486break;487488}489490/*491* The cpu is in CPU_STATE_INACTIVE.492* Upgrade it's state to CPU_STATE_OFFLINE.493*/494set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);495BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])496!= H_SUCCESS);497__cpu_die(cpu);498break;499}500if (cpu == num_possible_cpus())501printk(KERN_WARNING "Could not find cpu to offline "502"with physical id 0x%x\n", intserv[i]);503}504cpu_maps_update_done();505506out:507return rc;508509}510511static ssize_t dlpar_cpu_release(const char *buf, size_t count)512{513struct device_node *dn;514const u32 *drc_index;515int rc;516517dn = of_find_node_by_path(buf);518if (!dn)519return -EINVAL;520521drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);522if (!drc_index) {523of_node_put(dn);524return -EINVAL;525}526527cpu_hotplug_driver_lock();528rc = dlpar_offline_cpu(dn);529if (rc) {530of_node_put(dn);531rc = -EINVAL;532goto out;533}534535rc = dlpar_release_drc(*drc_index);536if (rc) {537of_node_put(dn);538goto out;539}540541rc = dlpar_detach_node(dn);542if (rc) {543dlpar_acquire_drc(*drc_index);544goto out;545}546547of_node_put(dn);548out:549cpu_hotplug_driver_unlock();550return rc ? rc : count;551}552553static int __init pseries_dlpar_init(void)554{555ppc_md.cpu_probe = dlpar_cpu_probe;556ppc_md.cpu_release = dlpar_cpu_release;557558return 0;559}560machine_device_initcall(pseries, pseries_dlpar_init);561562#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */563564565