Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * pseries Memory Hotplug infrastructure. |
| 3 | * |
| 4 | * Copyright (C) 2008 Badari Pulavarty, IBM Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/of.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 13 | #include <linux/memblock.h> |
Benjamin Herrenschmidt | b4a26be | 2010-04-06 15:03:40 +0000 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 15 | #include <asm/firmware.h> |
| 16 | #include <asm/machdep.h> |
| 17 | #include <asm/pSeries_reconfig.h> |
Michael Neuling | 0b2f828 | 2009-02-08 14:49:39 +0000 | [diff] [blame] | 18 | #include <asm/sparsemem.h> |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 19 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 20 | static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size) |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 21 | { |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 22 | unsigned long start, start_pfn; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 23 | struct zone *zone; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 24 | int ret; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 25 | |
Nathan Fontenot | 9fd3f88 | 2008-10-01 09:44:02 +0000 | [diff] [blame] | 26 | start_pfn = base >> PAGE_SHIFT; |
Nathan Fontenot | 04badfd | 2008-10-13 08:42:00 +0000 | [diff] [blame] | 27 | |
| 28 | if (!pfn_valid(start_pfn)) { |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 29 | memblock_remove(base, memblock_size); |
Nathan Fontenot | 04badfd | 2008-10-13 08:42:00 +0000 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 33 | zone = page_zone(pfn_to_page(start_pfn)); |
| 34 | |
| 35 | /* |
| 36 | * Remove section mappings and sysfs entries for the |
| 37 | * section of the memory we are removing. |
| 38 | * |
| 39 | * NOTE: Ideally, this should be done in generic code like |
| 40 | * remove_memory(). But remove_memory() gets called by writing |
| 41 | * to sysfs "state" file and we can't remove sysfs entries |
| 42 | * while writing to it. So we have to defer it to here. |
| 43 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 44 | ret = __remove_pages(zone, start_pfn, memblock_size >> PAGE_SHIFT); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 45 | if (ret) |
| 46 | return ret; |
| 47 | |
| 48 | /* |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 49 | * Update memory regions for memory remove |
| 50 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 51 | memblock_remove(base, memblock_size); |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 52 | |
| 53 | /* |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 54 | * Remove htab bolted mappings for this section of memory |
| 55 | */ |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 56 | start = (unsigned long)__va(base); |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 57 | ret = remove_section_mapping(start, start + memblock_size); |
Benjamin Herrenschmidt | b4a26be | 2010-04-06 15:03:40 +0000 | [diff] [blame] | 58 | |
| 59 | /* Ensure all vmalloc mappings are flushed in case they also |
| 60 | * hit that section of memory |
| 61 | */ |
| 62 | vm_unmap_aliases(); |
| 63 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 67 | static int pseries_remove_memory(struct device_node *np) |
| 68 | { |
| 69 | const char *type; |
| 70 | const unsigned int *regs; |
| 71 | unsigned long base; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 72 | unsigned int memblock_size; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 73 | int ret = -EINVAL; |
| 74 | |
| 75 | /* |
| 76 | * Check to see if we are actually removing memory |
| 77 | */ |
| 78 | type = of_get_property(np, "device_type", NULL); |
| 79 | if (type == NULL || strcmp(type, "memory") != 0) |
| 80 | return 0; |
| 81 | |
| 82 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 83 | * Find the bae address and size of the memblock |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 84 | */ |
| 85 | regs = of_get_property(np, "reg", NULL); |
| 86 | if (!regs) |
| 87 | return ret; |
| 88 | |
| 89 | base = *(unsigned long *)regs; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 90 | memblock_size = regs[3]; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 91 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 92 | ret = pseries_remove_memblock(base, memblock_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 93 | return ret; |
| 94 | } |
| 95 | |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 96 | static int pseries_add_memory(struct device_node *np) |
| 97 | { |
| 98 | const char *type; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 99 | const unsigned int *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 100 | unsigned long base; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 101 | unsigned int memblock_size; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 102 | int ret = -EINVAL; |
| 103 | |
| 104 | /* |
| 105 | * Check to see if we are actually adding memory |
| 106 | */ |
| 107 | type = of_get_property(np, "device_type", NULL); |
| 108 | if (type == NULL || strcmp(type, "memory") != 0) |
| 109 | return 0; |
| 110 | |
| 111 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 112 | * Find the base and size of the memblock |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 113 | */ |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 114 | regs = of_get_property(np, "reg", NULL); |
| 115 | if (!regs) |
| 116 | return ret; |
| 117 | |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 118 | base = *(unsigned long *)regs; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 119 | memblock_size = regs[3]; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Update memory region to represent the memory add |
| 123 | */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 124 | ret = memblock_add(base, memblock_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 125 | return (ret < 0) ? -EINVAL : 0; |
| 126 | } |
| 127 | |
| 128 | static int pseries_drconf_memory(unsigned long *base, unsigned int action) |
| 129 | { |
| 130 | struct device_node *np; |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 131 | const unsigned long *memblock_size; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 132 | int rc; |
| 133 | |
| 134 | np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 135 | if (!np) |
| 136 | return -EINVAL; |
| 137 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 138 | memblock_size = of_get_property(np, "ibm,memblock-size", NULL); |
| 139 | if (!memblock_size) { |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 140 | of_node_put(np); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | if (action == PSERIES_DRCONF_MEM_ADD) { |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 145 | rc = memblock_add(*base, *memblock_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 146 | rc = (rc < 0) ? -EINVAL : 0; |
| 147 | } else if (action == PSERIES_DRCONF_MEM_REMOVE) { |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 148 | rc = pseries_remove_memblock(*base, *memblock_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 149 | } else { |
| 150 | rc = -EINVAL; |
| 151 | } |
| 152 | |
| 153 | of_node_put(np); |
| 154 | return rc; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 157 | static int pseries_memory_notifier(struct notifier_block *nb, |
| 158 | unsigned long action, void *node) |
| 159 | { |
| 160 | int err = NOTIFY_OK; |
| 161 | |
| 162 | switch (action) { |
| 163 | case PSERIES_RECONFIG_ADD: |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 164 | if (pseries_add_memory(node)) |
| 165 | err = NOTIFY_BAD; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 166 | break; |
| 167 | case PSERIES_RECONFIG_REMOVE: |
| 168 | if (pseries_remove_memory(node)) |
| 169 | err = NOTIFY_BAD; |
| 170 | break; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 171 | case PSERIES_DRCONF_MEM_ADD: |
| 172 | case PSERIES_DRCONF_MEM_REMOVE: |
| 173 | if (pseries_drconf_memory(node, action)) |
| 174 | err = NOTIFY_BAD; |
| 175 | break; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 176 | default: |
| 177 | err = NOTIFY_DONE; |
| 178 | break; |
| 179 | } |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static struct notifier_block pseries_mem_nb = { |
| 184 | .notifier_call = pseries_memory_notifier, |
| 185 | }; |
| 186 | |
| 187 | static int __init pseries_memory_hotplug_init(void) |
| 188 | { |
| 189 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
| 190 | pSeries_reconfig_notifier_register(&pseries_mem_nb); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | machine_device_initcall(pseries, pseries_memory_hotplug_init); |