Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[pandora-kernel.git] / arch / powerpc / platforms / pseries / hotplug-memory.c
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>
13 #include <linux/lmb.h>
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;
21         const unsigned int *my_index;
22         const unsigned int *regs;
23         u64 start_pfn, start;
24         struct zone *zone;
25         int ret = -EINVAL;
26
27         /*
28          * Check to see if we are actually removing memory
29          */
30         type = of_get_property(np, "device_type", NULL);
31         if (type == NULL || strcmp(type, "memory") != 0)
32                 return 0;
33
34         /*
35          * Find the memory index and size of the removing section
36          */
37         my_index = of_get_property(np, "ibm,my-drc-index", NULL);
38         if (!my_index)
39                 return ret;
40
41         regs = of_get_property(np, "reg", NULL);
42         if (!regs)
43                 return ret;
44
45         start_pfn = section_nr_to_pfn(*my_index & 0xffff);
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          */
57         ret = __remove_pages(zone, start_pfn, regs[3] >> PAGE_SHIFT);
58         if (ret)
59                 return ret;
60
61         /*
62          * Update memory regions for memory remove
63          */
64         lmb_remove(start_pfn << PAGE_SHIFT, regs[3]);
65
66         /*
67          * Remove htab bolted mappings for this section of memory
68          */
69         start = (unsigned long)__va(start_pfn << PAGE_SHIFT);
70         ret = remove_section_mapping(start, start + regs[3]);
71         return ret;
72 }
73
74 static int pseries_add_memory(struct device_node *np)
75 {
76         const char *type;
77         const unsigned int *my_index;
78         const unsigned int *regs;
79         u64 start_pfn;
80         int ret = -EINVAL;
81
82         /*
83          * Check to see if we are actually adding memory
84          */
85         type = of_get_property(np, "device_type", NULL);
86         if (type == NULL || strcmp(type, "memory") != 0)
87                 return 0;
88
89         /*
90          * Find the memory index and size of the added section
91          */
92         my_index = of_get_property(np, "ibm,my-drc-index", NULL);
93         if (!my_index)
94                 return ret;
95
96         regs = of_get_property(np, "reg", NULL);
97         if (!regs)
98                 return ret;
99
100         start_pfn = section_nr_to_pfn(*my_index & 0xffff);
101
102         /*
103          * Update memory region to represent the memory add
104          */
105         lmb_add(start_pfn << PAGE_SHIFT, regs[3]);
106         return 0;
107 }
108
109 static int pseries_memory_notifier(struct notifier_block *nb,
110                                 unsigned long action, void *node)
111 {
112         int err = NOTIFY_OK;
113
114         switch (action) {
115         case PSERIES_RECONFIG_ADD:
116                 if (pseries_add_memory(node))
117                         err = NOTIFY_BAD;
118                 break;
119         case PSERIES_RECONFIG_REMOVE:
120                 if (pseries_remove_memory(node))
121                         err = NOTIFY_BAD;
122                 break;
123         default:
124                 err = NOTIFY_DONE;
125                 break;
126         }
127         return err;
128 }
129
130 static struct notifier_block pseries_mem_nb = {
131         .notifier_call = pseries_memory_notifier,
132 };
133
134 static int __init pseries_memory_hotplug_init(void)
135 {
136         if (firmware_has_feature(FW_FEATURE_LPAR))
137                 pSeries_reconfig_notifier_register(&pseries_mem_nb);
138
139         return 0;
140 }
141 machine_device_initcall(pseries, pseries_memory_hotplug_init);