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> |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 13 | #include <linux/lmb.h> |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 14 | #include <asm/firmware.h> |
| 15 | #include <asm/machdep.h> |
| 16 | #include <asm/pSeries_reconfig.h> |
| 17 | |
| 18 | static int pseries_remove_memory(struct device_node *np) |
| 19 | { |
| 20 | const char *type; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 21 | const unsigned int *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 22 | unsigned long base; |
| 23 | unsigned int lmb_size; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 24 | u64 start_pfn, start; |
| 25 | struct zone *zone; |
| 26 | int ret = -EINVAL; |
| 27 | |
| 28 | /* |
| 29 | * Check to see if we are actually removing memory |
| 30 | */ |
| 31 | type = of_get_property(np, "device_type", NULL); |
| 32 | if (type == NULL || strcmp(type, "memory") != 0) |
| 33 | return 0; |
| 34 | |
| 35 | /* |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 36 | * Find the bae address and size of the lmb |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 37 | */ |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 38 | regs = of_get_property(np, "reg", NULL); |
| 39 | if (!regs) |
| 40 | return ret; |
| 41 | |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 42 | base = *(unsigned long *)regs; |
| 43 | lmb_size = regs[3]; |
| 44 | |
| 45 | start_pfn = base >> PFN_SECTION_SHIFT; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 46 | zone = page_zone(pfn_to_page(start_pfn)); |
| 47 | |
| 48 | /* |
| 49 | * Remove section mappings and sysfs entries for the |
| 50 | * section of the memory we are removing. |
| 51 | * |
| 52 | * NOTE: Ideally, this should be done in generic code like |
| 53 | * remove_memory(). But remove_memory() gets called by writing |
| 54 | * to sysfs "state" file and we can't remove sysfs entries |
| 55 | * while writing to it. So we have to defer it to here. |
| 56 | */ |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 57 | ret = __remove_pages(zone, start_pfn, lmb_size >> PAGE_SHIFT); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 58 | if (ret) |
| 59 | return ret; |
| 60 | |
| 61 | /* |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 62 | * Update memory regions for memory remove |
| 63 | */ |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 64 | lmb_remove(base, lmb_size); |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 67 | * Remove htab bolted mappings for this section of memory |
| 68 | */ |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 69 | start = (unsigned long)__va(base); |
| 70 | ret = remove_section_mapping(start, start + lmb_size); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 71 | return ret; |
| 72 | } |
| 73 | |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 74 | static int pseries_add_memory(struct device_node *np) |
| 75 | { |
| 76 | const char *type; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 77 | const unsigned int *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 78 | unsigned long base; |
| 79 | unsigned int lmb_size; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 80 | u64 start_pfn; |
| 81 | int ret = -EINVAL; |
| 82 | |
| 83 | /* |
| 84 | * Check to see if we are actually adding memory |
| 85 | */ |
| 86 | type = of_get_property(np, "device_type", NULL); |
| 87 | if (type == NULL || strcmp(type, "memory") != 0) |
| 88 | return 0; |
| 89 | |
| 90 | /* |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 91 | * Find the base and size of the lmb |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 92 | */ |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 93 | regs = of_get_property(np, "reg", NULL); |
| 94 | if (!regs) |
| 95 | return ret; |
| 96 | |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 97 | base = *(unsigned long *)regs; |
| 98 | lmb_size = regs[3]; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Update memory region to represent the memory add |
| 102 | */ |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame^] | 103 | lmb_add(base, lmb_size); |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 107 | static int pseries_memory_notifier(struct notifier_block *nb, |
| 108 | unsigned long action, void *node) |
| 109 | { |
| 110 | int err = NOTIFY_OK; |
| 111 | |
| 112 | switch (action) { |
| 113 | case PSERIES_RECONFIG_ADD: |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 114 | if (pseries_add_memory(node)) |
| 115 | err = NOTIFY_BAD; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 116 | break; |
| 117 | case PSERIES_RECONFIG_REMOVE: |
| 118 | if (pseries_remove_memory(node)) |
| 119 | err = NOTIFY_BAD; |
| 120 | break; |
| 121 | default: |
| 122 | err = NOTIFY_DONE; |
| 123 | break; |
| 124 | } |
| 125 | return err; |
| 126 | } |
| 127 | |
| 128 | static struct notifier_block pseries_mem_nb = { |
| 129 | .notifier_call = pseries_memory_notifier, |
| 130 | }; |
| 131 | |
| 132 | static int __init pseries_memory_hotplug_init(void) |
| 133 | { |
| 134 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
| 135 | pSeries_reconfig_notifier_register(&pseries_mem_nb); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | machine_device_initcall(pseries, pseries_memory_hotplug_init); |