blob: a8d9d8fac5d4142201d2cc9e5abb545cdcf6e8ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 Intel Corporation <naveen.b.s@intel.com>
3 *
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 *
22 * ACPI based HotPlug driver that supports Memory Hotplug
Nick Andrewc7060d92009-01-03 18:53:39 +110023 * This driver fields notifications from firmware for memory add
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * and remove operations and alerts the VM of the affected memory
25 * ranges.
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/types.h>
32#include <linux/memory_hotplug.h>
33#include <acpi/acpi_drivers.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_MEMORY_DEVICE_CLASS "memory"
36#define ACPI_MEMORY_DEVICE_HID "PNP0C80"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
38
39#define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
40
Len Brownf52fd662007-02-12 22:42:12 -050041ACPI_MODULE_NAME("acpi_memhotplug");
42MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
Len Brown7cda93e2007-02-12 23:50:02 -050043MODULE_DESCRIPTION("Hotplug Mem Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_LICENSE("GPL");
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* Memory Device States */
47#define MEMORY_INVALID_STATE 0
48#define MEMORY_POWER_ON_STATE 1
49#define MEMORY_POWER_OFF_STATE 2
50
Len Brown4be44fc2005-08-05 00:44:28 -040051static int acpi_memory_device_add(struct acpi_device *device);
52static int acpi_memory_device_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Thomas Renninger1ba90e32007-07-23 14:44:41 +020054static const struct acpi_device_id memory_device_ids[] = {
55 {ACPI_MEMORY_DEVICE_HID, 0},
56 {"", 0},
57};
58MODULE_DEVICE_TABLE(acpi, memory_device_ids);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct acpi_driver acpi_memory_device_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050061 .name = "acpi_memhotplug",
Len Brown4be44fc2005-08-05 00:44:28 -040062 .class = ACPI_MEMORY_DEVICE_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020063 .ids = memory_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040064 .ops = {
65 .add = acpi_memory_device_add,
66 .remove = acpi_memory_device_remove,
67 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070070struct acpi_memory_info {
71 struct list_head list;
72 u64 start_addr; /* Memory Range start physical addr */
73 u64 length; /* Memory Range length */
74 unsigned short caching; /* memory cache attribute */
75 unsigned short write_protect; /* memory read/write attribute */
76 unsigned int enabled:1;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079struct acpi_memory_device {
Patrick Mochel3b748632006-05-19 16:54:38 -040080 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040081 unsigned int state; /* State of the memory device */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070082 struct list_head res_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Yasunori Goto887b9592006-10-19 23:28:31 -070085static int acpi_hotmem_initialized;
86
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070087static acpi_status
88acpi_memory_get_resource(struct acpi_resource *resource, void *context)
89{
90 struct acpi_memory_device *mem_device = context;
91 struct acpi_resource_address64 address64;
92 struct acpi_memory_info *info, *new;
93 acpi_status status;
94
95 status = acpi_resource_to_address64(resource, &address64);
96 if (ACPI_FAILURE(status) ||
97 (address64.resource_type != ACPI_MEMORY_RANGE))
98 return AE_OK;
99
100 list_for_each_entry(info, &mem_device->res_list, list) {
101 /* Can we combine the resource range information? */
102 if ((info->caching == address64.info.mem.caching) &&
103 (info->write_protect == address64.info.mem.write_protect) &&
104 (info->start_addr + info->length == address64.minimum)) {
105 info->length += address64.address_length;
106 return AE_OK;
107 }
108 }
109
110 new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
111 if (!new)
112 return AE_ERROR;
113
114 INIT_LIST_HEAD(&new->list);
115 new->caching = address64.info.mem.caching;
116 new->write_protect = address64.info.mem.write_protect;
117 new->start_addr = address64.minimum;
118 new->length = address64.address_length;
119 list_add_tail(&new->list, &mem_device->res_list);
120
121 return AE_OK;
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static int
125acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
126{
127 acpi_status status;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700128 struct acpi_memory_info *info, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
KAMEZAWA Hiroyuki5d2870f2006-08-05 12:15:04 -0700131 if (!list_empty(&mem_device->res_list))
132 return 0;
133
Patrick Mochelb8632782006-05-19 16:54:41 -0400134 status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700135 acpi_memory_get_resource, mem_device);
136 if (ACPI_FAILURE(status)) {
137 list_for_each_entry_safe(info, n, &mem_device->res_list, list)
138 kfree(info);
KAMEZAWA Hiroyuki5d2870f2006-08-05 12:15:04 -0700139 INIT_LIST_HEAD(&mem_device->res_list);
Patrick Mocheld550d982006-06-27 00:41:40 -0400140 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
Patrick Mocheld550d982006-06-27 00:41:40 -0400143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static int
147acpi_memory_get_device(acpi_handle handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400148 struct acpi_memory_device **mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 acpi_status status;
151 acpi_handle phandle;
152 struct acpi_device *device = NULL;
153 struct acpi_device *pdevice = NULL;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!acpi_bus_get_device(handle, &device) && device)
157 goto end;
158
159 status = acpi_get_parent(handle, &phandle);
160 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400161 ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400162 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
165 /* Get the parent device */
166 status = acpi_bus_get_device(phandle, &pdevice);
167 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400168 ACPI_EXCEPTION((AE_INFO, status, "Cannot get acpi bus device"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400169 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
172 /*
173 * Now add the notified device. This creates the acpi_device
174 * and invokes .add function
175 */
176 status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
177 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400178 ACPI_EXCEPTION((AE_INFO, status, "Cannot add acpi bus"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
Len Brown4be44fc2005-08-05 00:44:28 -0400182 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *mem_device = acpi_driver_data(device);
184 if (!(*mem_device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400185 printk(KERN_ERR "\n driver data not found");
Patrick Mocheld550d982006-06-27 00:41:40 -0400186 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Len Brown4be44fc2005-08-05 00:44:28 -0400192static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400194 unsigned long long current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /* Get device present/absent information from the _STA */
Patrick Mochelb8632782006-05-19 16:54:41 -0400197 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, "_STA",
Len Brown4be44fc2005-08-05 00:44:28 -0400198 NULL, &current_status)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * Check for device status. Device should be
202 * present/enabled/functioning.
203 */
Bjorn Helgaasa0bd4ac2007-04-25 14:17:39 -0400204 if (!((current_status & ACPI_STA_DEVICE_PRESENT)
205 && (current_status & ACPI_STA_DEVICE_ENABLED)
206 && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400207 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Len Brown4be44fc2005-08-05 00:44:28 -0400212static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700214 int result, num_enabled = 0;
215 struct acpi_memory_info *info;
Yasunori Goto1e3590e2006-06-27 02:53:31 -0700216 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Get the range from the _CRS */
220 result = acpi_memory_get_device_resources(mem_device);
221 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400222 printk(KERN_ERR PREFIX "get_device_resources failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 mem_device->state = MEMORY_INVALID_STATE;
224 return result;
225 }
226
Patrick Mochelb8632782006-05-19 16:54:41 -0400227 node = acpi_get_node(mem_device->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Tell the VM there is more memory here...
230 * Note: Assume that this function returns zero on success
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700231 * We don't have memory-hot-add rollback function,now.
232 * (i.e. memory-hot-remove function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700234 list_for_each_entry(info, &mem_device->res_list, list) {
KAMEZAWA Hiroyukifa25d8d2006-08-05 12:15:02 -0700235 if (info->enabled) { /* just sanity check...*/
Yasunori Gotodd56a8e2006-06-27 02:53:29 -0700236 num_enabled++;
237 continue;
238 }
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700239
240 if (node < 0)
241 node = memory_add_physaddr_to_nid(info->start_addr);
242
Yasunori Gotobc02af92006-06-27 02:53:30 -0700243 result = add_memory(node, info->start_addr, info->length);
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700244 if (result)
245 continue;
246 info->enabled = 1;
247 num_enabled++;
248 }
249 if (!num_enabled) {
akpm@osdl.org0a1f1ab2006-06-30 03:15:00 -0400250 printk(KERN_ERR PREFIX "add_memory failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 mem_device->state = MEMORY_INVALID_STATE;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 return result;
256}
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400261 struct acpi_object_list arg_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 union acpi_object arg;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400263 unsigned long long current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Issue the _EJ0 command */
267 arg_list.count = 1;
268 arg_list.pointer = &arg;
269 arg.type = ACPI_TYPE_INTEGER;
270 arg.integer.value = 1;
Patrick Mochelb8632782006-05-19 16:54:41 -0400271 status = acpi_evaluate_object(mem_device->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 "_EJ0", &arg_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Return on _EJ0 failure */
274 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400275 ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400276 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 /* Evalute _STA to check if the device is disabled */
Patrick Mochelb8632782006-05-19 16:54:41 -0400280 status = acpi_evaluate_integer(mem_device->device->handle, "_STA",
Len Brown4be44fc2005-08-05 00:44:28 -0400281 NULL, &current_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400283 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Check for device status. Device should be disabled */
Bjorn Helgaasa0bd4ac2007-04-25 14:17:39 -0400286 if (current_status & ACPI_STA_DEVICE_ENABLED)
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Patrick Mocheld550d982006-06-27 00:41:40 -0400289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Len Brown4be44fc2005-08-05 00:44:28 -0400292static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 int result;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700295 struct acpi_memory_info *info, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
299 * Ask the VM to offline this memory range.
300 * Note: Assume that this function returns zero on success
301 */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700302 list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
303 if (info->enabled) {
304 result = remove_memory(info->start_addr, info->length);
305 if (result)
306 return result;
307 }
308 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 /* Power-off and eject the device */
312 result = acpi_memory_powerdown_device(mem_device);
313 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* Set the status of the device to invalid */
315 mem_device->state = MEMORY_INVALID_STATE;
316 return result;
317 }
318
319 mem_device->state = MEMORY_POWER_OFF_STATE;
320 return result;
321}
322
Len Brown4be44fc2005-08-05 00:44:28 -0400323static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct acpi_memory_device *mem_device;
326 struct acpi_device *device;
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 switch (event) {
330 case ACPI_NOTIFY_BUS_CHECK:
331 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400332 "\nReceived BUS CHECK notification for device\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Fall Through */
334 case ACPI_NOTIFY_DEVICE_CHECK:
335 if (event == ACPI_NOTIFY_DEVICE_CHECK)
336 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400337 "\nReceived DEVICE CHECK notification for device\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (acpi_memory_get_device(handle, &mem_device)) {
Len Brown64684632006-06-26 23:41:38 -0400339 printk(KERN_ERR PREFIX "Cannot find driver data\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400340 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 if (!acpi_memory_check_device(mem_device)) {
344 if (acpi_memory_enable_device(mem_device))
Len Brown64684632006-06-26 23:41:38 -0400345 printk(KERN_ERR PREFIX
346 "Cannot enable memory device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 break;
349 case ACPI_NOTIFY_EJECT_REQUEST:
350 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400351 "\nReceived EJECT REQUEST notification for device\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (acpi_bus_get_device(handle, &device)) {
Len Brown64684632006-06-26 23:41:38 -0400354 printk(KERN_ERR PREFIX "Device doesn't exist\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 }
357 mem_device = acpi_driver_data(device);
358 if (!mem_device) {
Len Brown64684632006-06-26 23:41:38 -0400359 printk(KERN_ERR PREFIX "Driver Data is NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 }
362
363 /*
364 * Currently disabling memory device from kernel mode
365 * TBD: Can also be disabled from user mode scripts
366 * TBD: Can also be disabled by Callback registration
Len Brown4be44fc2005-08-05 00:44:28 -0400367 * with generic sysfs driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
369 if (acpi_memory_disable_device(mem_device))
Len Brown64684632006-06-26 23:41:38 -0400370 printk(KERN_ERR PREFIX
371 "Disable memory device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * TBD: Invoke acpi_bus_remove to cleanup data structures
374 */
375 break;
376 default:
377 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400378 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380 }
381
Patrick Mocheld550d982006-06-27 00:41:40 -0400382 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Len Brown4be44fc2005-08-05 00:44:28 -0400385static int acpi_memory_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 int result;
388 struct acpi_memory_device *mem_device = NULL;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400392 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Burman Yan36bcbec2006-12-19 12:56:11 -0800394 mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!mem_device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400396 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700398 INIT_LIST_HEAD(&mem_device->res_list);
Patrick Mochel3b748632006-05-19 16:54:38 -0400399 mem_device->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
401 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700402 device->driver_data = mem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* Get the range from the _CRS */
405 result = acpi_memory_get_device_resources(mem_device);
406 if (result) {
407 kfree(mem_device);
Patrick Mocheld550d982006-06-27 00:41:40 -0400408 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 /* Set the device state */
412 mem_device->state = MEMORY_POWER_ON_STATE;
413
Yasunori Goto6cbe44c2006-10-19 23:28:30 -0700414 printk(KERN_DEBUG "%s \n", acpi_device_name(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Bjorn Helgaas80f20fe2009-06-22 20:41:25 +0000416 /*
417 * Early boot code has recognized memory area by EFI/E820.
418 * If DSDT shows these memory devices on boot, hotplug is not necessary
419 * for them. So, it just returns until completion of this driver's
420 * start up.
421 */
422 if (!acpi_hotmem_initialized)
423 return 0;
424
425 if (!acpi_memory_check_device(mem_device)) {
426 /* call add_memory func */
427 result = acpi_memory_enable_device(mem_device);
428 if (result)
429 printk(KERN_ERR PREFIX
430 "Error in acpi_memory_enable_device\n");
431 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400432 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Len Brown4be44fc2005-08-05 00:44:28 -0400435static int acpi_memory_device_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct acpi_memory_device *mem_device = NULL;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200443 mem_device = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 kfree(mem_device);
445
Patrick Mocheld550d982006-06-27 00:41:40 -0400446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
450 * Helper function to check for memory device
451 */
Len Brown4be44fc2005-08-05 00:44:28 -0400452static acpi_status is_memory_device(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 char *hardware_id;
455 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400456 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct acpi_device_info *info;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 status = acpi_get_object_info(handle, &buffer);
461 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400462 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 info = buffer.pointer;
465 if (!(info->valid & ACPI_VALID_HID)) {
Len Brown02438d82006-06-30 03:19:10 -0400466 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400467 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 hardware_id = info->hardware_id.value;
471 if ((hardware_id == NULL) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400472 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 status = AE_ERROR;
474
Len Brown02438d82006-06-30 03:19:10 -0400475 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400476 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400480acpi_memory_register_notify_handler(acpi_handle handle,
481 u32 level, void *ctxt, void **retv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 acpi_status status;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 status = is_memory_device(handle);
Yasunori Goto07dd4852006-08-14 22:37:32 -0700487 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400488 return AE_OK; /* continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Thomas Renningera6fc6722006-06-26 23:58:43 -0400490 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
491 acpi_memory_device_notify, NULL);
492 /* continue */
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400497acpi_memory_deregister_notify_handler(acpi_handle handle,
498 u32 level, void *ctxt, void **retv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 acpi_status status;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 status = is_memory_device(handle);
Yasunori Goto07dd4852006-08-14 22:37:32 -0700504 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400505 return AE_OK; /* continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 status = acpi_remove_notify_handler(handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400508 ACPI_SYSTEM_NOTIFY,
509 acpi_memory_device_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Patrick Mocheld550d982006-06-27 00:41:40 -0400511 return AE_OK; /* continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Len Brown4be44fc2005-08-05 00:44:28 -0400514static int __init acpi_memory_device_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 int result;
517 acpi_status status;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 result = acpi_bus_register_driver(&acpi_memory_device_driver);
521
522 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400523 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -0400526 ACPI_UINT32_MAX,
527 acpi_memory_register_notify_handler,
528 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Len Brown4be44fc2005-08-05 00:44:28 -0400530 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400531 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 acpi_bus_unregister_driver(&acpi_memory_device_driver);
Patrick Mocheld550d982006-06-27 00:41:40 -0400533 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Yasunori Goto887b9592006-10-19 23:28:31 -0700536 acpi_hotmem_initialized = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Len Brown4be44fc2005-08-05 00:44:28 -0400540static void __exit acpi_memory_device_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 acpi_status status;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * Adding this to un-install notification handlers for all the device
547 * handles.
548 */
549 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -0400550 ACPI_UINT32_MAX,
551 acpi_memory_deregister_notify_handler,
552 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Len Brown4be44fc2005-08-05 00:44:28 -0400554 if (ACPI_FAILURE(status))
Thomas Renningera6fc6722006-06-26 23:58:43 -0400555 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 acpi_bus_unregister_driver(&acpi_memory_device_driver);
558
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562module_init(acpi_memory_device_init);
563module_exit(acpi_memory_device_exit);