blob: b0546951384261fabb8bec4f6f1d1e002fd228da [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
23 * This driver fields notifications from firmare for memory add
24 * 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_COMPONENT 0x08000000UL
36#define ACPI_MEMORY_DEVICE_CLASS "memory"
37#define ACPI_MEMORY_DEVICE_HID "PNP0C80"
38#define ACPI_MEMORY_DEVICE_DRIVER_NAME "Hotplug Mem Driver"
39#define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
40
41#define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
42
Len Brown4be44fc2005-08-05 00:44:28 -040043ACPI_MODULE_NAME("acpi_memory")
44 MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_DESCRIPTION(ACPI_MEMORY_DEVICE_DRIVER_NAME);
46MODULE_LICENSE("GPL");
47
48/* ACPI _STA method values */
49#define ACPI_MEMORY_STA_PRESENT (0x00000001UL)
50#define ACPI_MEMORY_STA_ENABLED (0x00000002UL)
51#define ACPI_MEMORY_STA_FUNCTIONAL (0x00000008UL)
52
53/* Memory Device States */
54#define MEMORY_INVALID_STATE 0
55#define MEMORY_POWER_ON_STATE 1
56#define MEMORY_POWER_OFF_STATE 2
57
Len Brown4be44fc2005-08-05 00:44:28 -040058static int acpi_memory_device_add(struct acpi_device *device);
59static int acpi_memory_device_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static struct acpi_driver acpi_memory_device_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040062 .name = ACPI_MEMORY_DEVICE_DRIVER_NAME,
63 .class = ACPI_MEMORY_DEVICE_CLASS,
64 .ids = ACPI_MEMORY_DEVICE_HID,
65 .ops = {
66 .add = acpi_memory_device_add,
67 .remove = acpi_memory_device_remove,
68 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71struct acpi_memory_device {
72 acpi_handle handle;
Len Brown4be44fc2005-08-05 00:44:28 -040073 unsigned int state; /* State of the memory device */
KAMEZAWA Hiroyuki3963f002006-01-06 01:31:00 -050074 unsigned short caching; /* memory cache attribute */
75 unsigned short write_protect; /* memory read/write attribute */
Len Brown4be44fc2005-08-05 00:44:28 -040076 u64 start_addr; /* Memory Range start physical addr */
Bjorn Helgaas459c7262006-03-28 17:04:00 -050077 u64 length; /* Memory Range length */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int
81acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
82{
83 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -040084 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct acpi_resource *resource = NULL;
86 struct acpi_resource_address64 address64;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 /* Get the range from the _CRS */
90 status = acpi_get_current_resources(mem_device->handle, &buffer);
91 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -040092 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Len Brown4be44fc2005-08-05 00:44:28 -040094 resource = (struct acpi_resource *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 status = acpi_resource_to_address64(resource, &address64);
96 if (ACPI_SUCCESS(status)) {
97 if (address64.resource_type == ACPI_MEMORY_RANGE) {
98 /* Populate the structure */
Bjorn Helgaas459c7262006-03-28 17:04:00 -050099 mem_device->caching = address64.info.mem.caching;
KAMEZAWA Hiroyuki3963f002006-01-06 01:31:00 -0500100 mem_device->write_protect =
101 address64.info.mem.write_protect;
Bob Moore50eca3e2005-09-30 19:03:00 -0400102 mem_device->start_addr = address64.minimum;
Bjorn Helgaas459c7262006-03-28 17:04:00 -0500103 mem_device->length = address64.address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 }
106
107 acpi_os_free(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static int
112acpi_memory_get_device(acpi_handle handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400113 struct acpi_memory_device **mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 acpi_status status;
116 acpi_handle phandle;
117 struct acpi_device *device = NULL;
118 struct acpi_device *pdevice = NULL;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (!acpi_bus_get_device(handle, &device) && device)
122 goto end;
123
124 status = acpi_get_parent(handle, &phandle);
125 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400126 ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400127 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 /* Get the parent device */
131 status = acpi_bus_get_device(phandle, &pdevice);
132 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400133 ACPI_EXCEPTION((AE_INFO, status, "Cannot get acpi bus device"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400134 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
137 /*
138 * Now add the notified device. This creates the acpi_device
139 * and invokes .add function
140 */
141 status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
142 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400143 ACPI_EXCEPTION((AE_INFO, status, "Cannot add acpi bus"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Len Brown4be44fc2005-08-05 00:44:28 -0400147 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *mem_device = acpi_driver_data(device);
149 if (!(*mem_device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400150 printk(KERN_ERR "\n driver data not found");
Patrick Mocheld550d982006-06-27 00:41:40 -0400151 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
Patrick Mocheld550d982006-06-27 00:41:40 -0400154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Len Brown4be44fc2005-08-05 00:44:28 -0400157static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 unsigned long current_status;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Get device present/absent information from the _STA */
163 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA",
Len Brown4be44fc2005-08-05 00:44:28 -0400164 NULL, &current_status)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * Check for device status. Device should be
168 * present/enabled/functioning.
169 */
170 if (!((current_status & ACPI_MEMORY_STA_PRESENT)
Len Brown4be44fc2005-08-05 00:44:28 -0400171 && (current_status & ACPI_MEMORY_STA_ENABLED)
172 && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400173 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Patrick Mocheld550d982006-06-27 00:41:40 -0400175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Len Brown4be44fc2005-08-05 00:44:28 -0400178static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int result;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* Get the range from the _CRS */
184 result = acpi_memory_get_device_resources(mem_device);
185 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400186 printk(KERN_ERR PREFIX "get_device_resources failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 mem_device->state = MEMORY_INVALID_STATE;
188 return result;
189 }
190
191 /*
192 * Tell the VM there is more memory here...
193 * Note: Assume that this function returns zero on success
194 */
Bjorn Helgaas459c7262006-03-28 17:04:00 -0500195 result = add_memory(mem_device->start_addr, mem_device->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400197 printk(KERN_ERR PREFIX "add_memory failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mem_device->state = MEMORY_INVALID_STATE;
199 return result;
200 }
201
202 return result;
203}
204
Len Brown4be44fc2005-08-05 00:44:28 -0400205static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400208 struct acpi_object_list arg_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 union acpi_object arg;
210 unsigned long current_status;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Issue the _EJ0 command */
214 arg_list.count = 1;
215 arg_list.pointer = &arg;
216 arg.type = ACPI_TYPE_INTEGER;
217 arg.integer.value = 1;
218 status = acpi_evaluate_object(mem_device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400219 "_EJ0", &arg_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Return on _EJ0 failure */
221 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400222 ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400223 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 /* Evalute _STA to check if the device is disabled */
227 status = acpi_evaluate_integer(mem_device->handle, "_STA",
Len Brown4be44fc2005-08-05 00:44:28 -0400228 NULL, &current_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400230 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* Check for device status. Device should be disabled */
233 if (current_status & ACPI_MEMORY_STA_ENABLED)
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Patrick Mocheld550d982006-06-27 00:41:40 -0400236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Len Brown4be44fc2005-08-05 00:44:28 -0400239static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int result;
242 u64 start = mem_device->start_addr;
Bjorn Helgaas459c7262006-03-28 17:04:00 -0500243 u64 len = mem_device->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /*
247 * Ask the VM to offline this memory range.
248 * Note: Assume that this function returns zero on success
249 */
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700250 result = remove_memory(start, len);
Thomas Renningera6fc6722006-06-26 23:58:43 -0400251 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400252 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Power-off and eject the device */
255 result = acpi_memory_powerdown_device(mem_device);
256 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* Set the status of the device to invalid */
258 mem_device->state = MEMORY_INVALID_STATE;
259 return result;
260 }
261
262 mem_device->state = MEMORY_POWER_OFF_STATE;
263 return result;
264}
265
Len Brown4be44fc2005-08-05 00:44:28 -0400266static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct acpi_memory_device *mem_device;
269 struct acpi_device *device;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 switch (event) {
273 case ACPI_NOTIFY_BUS_CHECK:
274 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400275 "\nReceived BUS CHECK notification for device\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Fall Through */
277 case ACPI_NOTIFY_DEVICE_CHECK:
278 if (event == ACPI_NOTIFY_DEVICE_CHECK)
279 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400280 "\nReceived DEVICE CHECK notification for device\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (acpi_memory_get_device(handle, &mem_device)) {
Len Brown64684632006-06-26 23:41:38 -0400282 printk(KERN_ERR PREFIX "Cannot find driver data\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400283 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 if (!acpi_memory_check_device(mem_device)) {
287 if (acpi_memory_enable_device(mem_device))
Len Brown64684632006-06-26 23:41:38 -0400288 printk(KERN_ERR PREFIX
289 "Cannot enable memory device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 break;
292 case ACPI_NOTIFY_EJECT_REQUEST:
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400294 "\nReceived EJECT REQUEST notification for device\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (acpi_bus_get_device(handle, &device)) {
Len Brown64684632006-06-26 23:41:38 -0400297 printk(KERN_ERR PREFIX "Device doesn't exist\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
299 }
300 mem_device = acpi_driver_data(device);
301 if (!mem_device) {
Len Brown64684632006-06-26 23:41:38 -0400302 printk(KERN_ERR PREFIX "Driver Data is NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 }
305
306 /*
307 * Currently disabling memory device from kernel mode
308 * TBD: Can also be disabled from user mode scripts
309 * TBD: Can also be disabled by Callback registration
Len Brown4be44fc2005-08-05 00:44:28 -0400310 * with generic sysfs driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312 if (acpi_memory_disable_device(mem_device))
Len Brown64684632006-06-26 23:41:38 -0400313 printk(KERN_ERR PREFIX
314 "Disable memory device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * TBD: Invoke acpi_bus_remove to cleanup data structures
317 */
318 break;
319 default:
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400321 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323 }
324
Patrick Mocheld550d982006-06-27 00:41:40 -0400325 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328static int acpi_memory_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int result;
331 struct acpi_memory_device *mem_device = NULL;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
338 if (!mem_device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400339 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 memset(mem_device, 0, sizeof(struct acpi_memory_device));
341
342 mem_device->handle = device->handle;
343 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
344 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
345 acpi_driver_data(device) = mem_device;
346
347 /* Get the range from the _CRS */
348 result = acpi_memory_get_device_resources(mem_device);
349 if (result) {
350 kfree(mem_device);
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 /* Set the device state */
355 mem_device->state = MEMORY_POWER_ON_STATE;
356
357 printk(KERN_INFO "%s \n", acpi_device_name(device));
358
Patrick Mocheld550d982006-06-27 00:41:40 -0400359 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Len Brown4be44fc2005-08-05 00:44:28 -0400362static int acpi_memory_device_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct acpi_memory_device *mem_device = NULL;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400368 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Len Brown4be44fc2005-08-05 00:44:28 -0400370 mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 kfree(mem_device);
372
Patrick Mocheld550d982006-06-27 00:41:40 -0400373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/*
377 * Helper function to check for memory device
378 */
Len Brown4be44fc2005-08-05 00:44:28 -0400379static acpi_status is_memory_device(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 char *hardware_id;
382 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400383 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct acpi_device_info *info;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 status = acpi_get_object_info(handle, &buffer);
388 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400389 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 info = buffer.pointer;
392 if (!(info->valid & ACPI_VALID_HID)) {
393 acpi_os_free(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400394 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 hardware_id = info->hardware_id.value;
398 if ((hardware_id == NULL) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400399 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 status = AE_ERROR;
401
402 acpi_os_free(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400407acpi_memory_register_notify_handler(acpi_handle handle,
408 u32 level, void *ctxt, void **retv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 acpi_status status;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 status = is_memory_device(handle);
Thomas Renningera6fc6722006-06-26 23:58:43 -0400414 if (ACPI_FAILURE(status)){
415 ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400416 return AE_OK; /* continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Thomas Renningera6fc6722006-06-26 23:58:43 -0400419 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
420 acpi_memory_device_notify, NULL);
421 /* continue */
Patrick Mocheld550d982006-06-27 00:41:40 -0400422 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400426acpi_memory_deregister_notify_handler(acpi_handle handle,
427 u32 level, void *ctxt, void **retv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 acpi_status status;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 status = is_memory_device(handle);
Thomas Renningera6fc6722006-06-26 23:58:43 -0400433 if (ACPI_FAILURE(status)){
434 ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400435 return AE_OK; /* continue */
Thomas Renningera6fc6722006-06-26 23:58:43 -0400436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 status = acpi_remove_notify_handler(handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400439 ACPI_SYSTEM_NOTIFY,
440 acpi_memory_device_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Patrick Mocheld550d982006-06-27 00:41:40 -0400442 return AE_OK; /* continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Len Brown4be44fc2005-08-05 00:44:28 -0400445static int __init acpi_memory_device_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 int result;
448 acpi_status status;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 result = acpi_bus_register_driver(&acpi_memory_device_driver);
452
453 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400454 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -0400457 ACPI_UINT32_MAX,
458 acpi_memory_register_notify_handler,
459 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Len Brown4be44fc2005-08-05 00:44:28 -0400461 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400462 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 acpi_bus_unregister_driver(&acpi_memory_device_driver);
Patrick Mocheld550d982006-06-27 00:41:40 -0400464 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Patrick Mocheld550d982006-06-27 00:41:40 -0400467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Len Brown4be44fc2005-08-05 00:44:28 -0400470static void __exit acpi_memory_device_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 acpi_status status;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /*
476 * Adding this to un-install notification handlers for all the device
477 * handles.
478 */
479 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -0400480 ACPI_UINT32_MAX,
481 acpi_memory_deregister_notify_handler,
482 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Len Brown4be44fc2005-08-05 00:44:28 -0400484 if (ACPI_FAILURE(status))
Thomas Renningera6fc6722006-06-26 23:58:43 -0400485 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 acpi_bus_unregister_driver(&acpi_memory_device_driver);
488
Patrick Mocheld550d982006-06-27 00:41:40 -0400489 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492module_init(acpi_memory_device_init);
493module_exit(acpi_memory_device_exit);