Path: blob/master/arch/powerpc/platforms/pseries/hotplug-memory.c
10818 views
/*1* pseries Memory Hotplug infrastructure.2*3* Copyright (C) 2008 Badari Pulavarty, IBM Corporation4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public License7* as published by the Free Software Foundation; either version8* 2 of the License, or (at your option) any later version.9*/1011#include <linux/of.h>12#include <linux/memblock.h>13#include <linux/vmalloc.h>14#include <linux/memory.h>1516#include <asm/firmware.h>17#include <asm/machdep.h>18#include <asm/pSeries_reconfig.h>19#include <asm/sparsemem.h>2021static unsigned long get_memblock_size(void)22{23struct device_node *np;24unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;25struct resource r;2627np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");28if (np) {29const __be64 *size;3031size = of_get_property(np, "ibm,lmb-size", NULL);32if (size)33memblock_size = be64_to_cpup(size);34of_node_put(np);35} else if (machine_is(pseries)) {36/* This fallback really only applies to pseries */37unsigned int memzero_size = 0;3839np = of_find_node_by_path("/memory@0");40if (np) {41if (!of_address_to_resource(np, 0, &r))42memzero_size = resource_size(&r);43of_node_put(np);44}4546if (memzero_size) {47/* We now know the size of memory@0, use this to find48* the first memoryblock and get its size.49*/50char buf[64];5152sprintf(buf, "/memory@%x", memzero_size);53np = of_find_node_by_path(buf);54if (np) {55if (!of_address_to_resource(np, 0, &r))56memblock_size = resource_size(&r);57of_node_put(np);58}59}60}61return memblock_size;62}6364/* WARNING: This is going to override the generic definition whenever65* pseries is built-in regardless of what platform is active at boot66* time. This is fine for now as this is the only "option" and it67* should work everywhere. If not, we'll have to turn this into a68* ppc_md. callback69*/70unsigned long memory_block_size_bytes(void)71{72return get_memblock_size();73}7475static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)76{77unsigned long start, start_pfn;78struct zone *zone;79int ret;8081start_pfn = base >> PAGE_SHIFT;8283if (!pfn_valid(start_pfn)) {84memblock_remove(base, memblock_size);85return 0;86}8788zone = page_zone(pfn_to_page(start_pfn));8990/*91* Remove section mappings and sysfs entries for the92* section of the memory we are removing.93*94* NOTE: Ideally, this should be done in generic code like95* remove_memory(). But remove_memory() gets called by writing96* to sysfs "state" file and we can't remove sysfs entries97* while writing to it. So we have to defer it to here.98*/99ret = __remove_pages(zone, start_pfn, memblock_size >> PAGE_SHIFT);100if (ret)101return ret;102103/*104* Update memory regions for memory remove105*/106memblock_remove(base, memblock_size);107108/*109* Remove htab bolted mappings for this section of memory110*/111start = (unsigned long)__va(base);112ret = remove_section_mapping(start, start + memblock_size);113114/* Ensure all vmalloc mappings are flushed in case they also115* hit that section of memory116*/117vm_unmap_aliases();118119return ret;120}121122static int pseries_remove_memory(struct device_node *np)123{124const char *type;125const unsigned int *regs;126unsigned long base;127unsigned int lmb_size;128int ret = -EINVAL;129130/*131* Check to see if we are actually removing memory132*/133type = of_get_property(np, "device_type", NULL);134if (type == NULL || strcmp(type, "memory") != 0)135return 0;136137/*138* Find the bae address and size of the memblock139*/140regs = of_get_property(np, "reg", NULL);141if (!regs)142return ret;143144base = *(unsigned long *)regs;145lmb_size = regs[3];146147ret = pseries_remove_memblock(base, lmb_size);148return ret;149}150151static int pseries_add_memory(struct device_node *np)152{153const char *type;154const unsigned int *regs;155unsigned long base;156unsigned int lmb_size;157int ret = -EINVAL;158159/*160* Check to see if we are actually adding memory161*/162type = of_get_property(np, "device_type", NULL);163if (type == NULL || strcmp(type, "memory") != 0)164return 0;165166/*167* Find the base and size of the memblock168*/169regs = of_get_property(np, "reg", NULL);170if (!regs)171return ret;172173base = *(unsigned long *)regs;174lmb_size = regs[3];175176/*177* Update memory region to represent the memory add178*/179ret = memblock_add(base, lmb_size);180return (ret < 0) ? -EINVAL : 0;181}182183static int pseries_drconf_memory(unsigned long *base, unsigned int action)184{185unsigned long memblock_size;186int rc;187188memblock_size = get_memblock_size();189if (!memblock_size)190return -EINVAL;191192if (action == PSERIES_DRCONF_MEM_ADD) {193rc = memblock_add(*base, memblock_size);194rc = (rc < 0) ? -EINVAL : 0;195} else if (action == PSERIES_DRCONF_MEM_REMOVE) {196rc = pseries_remove_memblock(*base, memblock_size);197} else {198rc = -EINVAL;199}200201return rc;202}203204static int pseries_memory_notifier(struct notifier_block *nb,205unsigned long action, void *node)206{207int err = NOTIFY_OK;208209switch (action) {210case PSERIES_RECONFIG_ADD:211if (pseries_add_memory(node))212err = NOTIFY_BAD;213break;214case PSERIES_RECONFIG_REMOVE:215if (pseries_remove_memory(node))216err = NOTIFY_BAD;217break;218case PSERIES_DRCONF_MEM_ADD:219case PSERIES_DRCONF_MEM_REMOVE:220if (pseries_drconf_memory(node, action))221err = NOTIFY_BAD;222break;223default:224err = NOTIFY_DONE;225break;226}227return err;228}229230static struct notifier_block pseries_mem_nb = {231.notifier_call = pseries_memory_notifier,232};233234static int __init pseries_memory_hotplug_init(void)235{236if (firmware_has_feature(FW_FEATURE_LPAR))237pSeries_reconfig_notifier_register(&pseries_mem_nb);238239return 0;240}241machine_device_initcall(pseries, pseries_memory_hotplug_init);242243244