Linux v2.6.13
[linux-2.6/next.git] / drivers / acpi / acpi_memhotplug.c
blob77285ffe41c5cb0e25360fe23737da2d97fdc760
1 /*
2 * Copyright (C) 2004 Intel Corporation <naveen.b.s@intel.com>
4 * All rights reserved.
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.
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.
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.
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.
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>
36 #define ACPI_MEMORY_DEVICE_COMPONENT 0x08000000UL
37 #define ACPI_MEMORY_DEVICE_CLASS "memory"
38 #define ACPI_MEMORY_DEVICE_HID "PNP0C80"
39 #define ACPI_MEMORY_DEVICE_DRIVER_NAME "Hotplug Mem Driver"
40 #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
42 #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
44 ACPI_MODULE_NAME ("acpi_memory")
45 MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
46 MODULE_DESCRIPTION(ACPI_MEMORY_DEVICE_DRIVER_NAME);
47 MODULE_LICENSE("GPL");
49 /* ACPI _STA method values */
50 #define ACPI_MEMORY_STA_PRESENT (0x00000001UL)
51 #define ACPI_MEMORY_STA_ENABLED (0x00000002UL)
52 #define ACPI_MEMORY_STA_FUNCTIONAL (0x00000008UL)
54 /* Memory Device States */
55 #define MEMORY_INVALID_STATE 0
56 #define MEMORY_POWER_ON_STATE 1
57 #define MEMORY_POWER_OFF_STATE 2
59 static int acpi_memory_device_add (struct acpi_device *device);
60 static int acpi_memory_device_remove (struct acpi_device *device, int type);
62 static struct acpi_driver acpi_memory_device_driver = {
63 .name = ACPI_MEMORY_DEVICE_DRIVER_NAME,
64 .class = ACPI_MEMORY_DEVICE_CLASS,
65 .ids = ACPI_MEMORY_DEVICE_HID,
66 .ops = {
67 .add = acpi_memory_device_add,
68 .remove = acpi_memory_device_remove,
72 struct acpi_memory_device {
73 acpi_handle handle;
74 unsigned int state; /* State of the memory device */
75 unsigned short cache_attribute; /* memory cache attribute */
76 unsigned short read_write_attribute;/* memory read/write attribute */
77 u64 start_addr; /* Memory Range start physical addr */
78 u64 end_addr; /* Memory Range end physical addr */
82 static int
83 acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
85 acpi_status status;
86 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
87 struct acpi_resource *resource = NULL;
88 struct acpi_resource_address64 address64;
90 ACPI_FUNCTION_TRACE("acpi_memory_get_device_resources");
92 /* Get the range from the _CRS */
93 status = acpi_get_current_resources(mem_device->handle, &buffer);
94 if (ACPI_FAILURE(status))
95 return_VALUE(-EINVAL);
97 resource = (struct acpi_resource *) buffer.pointer;
98 status = acpi_resource_to_address64(resource, &address64);
99 if (ACPI_SUCCESS(status)) {
100 if (address64.resource_type == ACPI_MEMORY_RANGE) {
101 /* Populate the structure */
102 mem_device->cache_attribute =
103 address64.attribute.memory.cache_attribute;
104 mem_device->read_write_attribute =
105 address64.attribute.memory.read_write_attribute;
106 mem_device->start_addr = address64.min_address_range;
107 mem_device->end_addr = address64.max_address_range;
111 acpi_os_free(buffer.pointer);
112 return_VALUE(0);
115 static int
116 acpi_memory_get_device(acpi_handle handle,
117 struct acpi_memory_device **mem_device)
119 acpi_status status;
120 acpi_handle phandle;
121 struct acpi_device *device = NULL;
122 struct acpi_device *pdevice = NULL;
124 ACPI_FUNCTION_TRACE("acpi_memory_get_device");
126 if (!acpi_bus_get_device(handle, &device) && device)
127 goto end;
129 status = acpi_get_parent(handle, &phandle);
130 if (ACPI_FAILURE(status)) {
131 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
132 "Error in acpi_get_parent\n"));
133 return_VALUE(-EINVAL);
136 /* Get the parent device */
137 status = acpi_bus_get_device(phandle, &pdevice);
138 if (ACPI_FAILURE(status)) {
139 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
140 "Error in acpi_bus_get_device\n"));
141 return_VALUE(-EINVAL);
145 * Now add the notified device. This creates the acpi_device
146 * and invokes .add function
148 status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
149 if (ACPI_FAILURE(status)) {
150 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
151 "Error in acpi_bus_add\n"));
152 return_VALUE(-EINVAL);
155 end:
156 *mem_device = acpi_driver_data(device);
157 if (!(*mem_device)) {
158 printk(KERN_ERR "\n driver data not found" );
159 return_VALUE(-ENODEV);
162 return_VALUE(0);
165 static int
166 acpi_memory_check_device(struct acpi_memory_device *mem_device)
168 unsigned long current_status;
170 ACPI_FUNCTION_TRACE("acpi_memory_check_device");
172 /* Get device present/absent information from the _STA */
173 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA",
174 NULL, &current_status)))
175 return_VALUE(-ENODEV);
177 * Check for device status. Device should be
178 * present/enabled/functioning.
180 if (!((current_status & ACPI_MEMORY_STA_PRESENT)
181 && (current_status & ACPI_MEMORY_STA_ENABLED)
182 && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
183 return_VALUE(-ENODEV);
185 return_VALUE(0);
188 static int
189 acpi_memory_enable_device(struct acpi_memory_device *mem_device)
191 int result;
193 ACPI_FUNCTION_TRACE("acpi_memory_enable_device");
195 /* Get the range from the _CRS */
196 result = acpi_memory_get_device_resources(mem_device);
197 if (result) {
198 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
199 "\nget_device_resources failed\n"));
200 mem_device->state = MEMORY_INVALID_STATE;
201 return result;
205 * Tell the VM there is more memory here...
206 * Note: Assume that this function returns zero on success
208 result = add_memory(mem_device->start_addr,
209 (mem_device->end_addr - mem_device->start_addr) + 1,
210 mem_device->read_write_attribute);
211 if (result) {
212 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
213 "\nadd_memory failed\n"));
214 mem_device->state = MEMORY_INVALID_STATE;
215 return result;
218 return result;
221 static int
222 acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
224 acpi_status status;
225 struct acpi_object_list arg_list;
226 union acpi_object arg;
227 unsigned long current_status;
229 ACPI_FUNCTION_TRACE("acpi_memory_powerdown_device");
231 /* Issue the _EJ0 command */
232 arg_list.count = 1;
233 arg_list.pointer = &arg;
234 arg.type = ACPI_TYPE_INTEGER;
235 arg.integer.value = 1;
236 status = acpi_evaluate_object(mem_device->handle,
237 "_EJ0", &arg_list, NULL);
238 /* Return on _EJ0 failure */
239 if (ACPI_FAILURE(status)) {
240 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"_EJ0 failed.\n"));
241 return_VALUE(-ENODEV);
244 /* Evalute _STA to check if the device is disabled */
245 status = acpi_evaluate_integer(mem_device->handle, "_STA",
246 NULL, &current_status);
247 if (ACPI_FAILURE(status))
248 return_VALUE(-ENODEV);
250 /* Check for device status. Device should be disabled */
251 if (current_status & ACPI_MEMORY_STA_ENABLED)
252 return_VALUE(-EINVAL);
254 return_VALUE(0);
257 static int
258 acpi_memory_disable_device(struct acpi_memory_device *mem_device)
260 int result;
261 u64 start = mem_device->start_addr;
262 u64 len = mem_device->end_addr - start + 1;
263 unsigned long attr = mem_device->read_write_attribute;
265 ACPI_FUNCTION_TRACE("acpi_memory_disable_device");
268 * Ask the VM to offline this memory range.
269 * Note: Assume that this function returns zero on success
271 result = remove_memory(start, len, attr);
272 if (result) {
273 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Hot-Remove failed.\n"));
274 return_VALUE(result);
277 /* Power-off and eject the device */
278 result = acpi_memory_powerdown_device(mem_device);
279 if (result) {
280 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
281 "Device Power Down failed.\n"));
282 /* Set the status of the device to invalid */
283 mem_device->state = MEMORY_INVALID_STATE;
284 return result;
287 mem_device->state = MEMORY_POWER_OFF_STATE;
288 return result;
291 static void
292 acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
294 struct acpi_memory_device *mem_device;
295 struct acpi_device *device;
297 ACPI_FUNCTION_TRACE("acpi_memory_device_notify");
299 switch (event) {
300 case ACPI_NOTIFY_BUS_CHECK:
301 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
302 "\nReceived BUS CHECK notification for device\n"));
303 /* Fall Through */
304 case ACPI_NOTIFY_DEVICE_CHECK:
305 if (event == ACPI_NOTIFY_DEVICE_CHECK)
306 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
307 "\nReceived DEVICE CHECK notification for device\n"));
308 if (acpi_memory_get_device(handle, &mem_device)) {
309 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
310 "Error in finding driver data\n"));
311 return_VOID;
314 if (!acpi_memory_check_device(mem_device)) {
315 if (acpi_memory_enable_device(mem_device))
316 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
317 "Error in acpi_memory_enable_device\n"));
319 break;
320 case ACPI_NOTIFY_EJECT_REQUEST:
321 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
322 "\nReceived EJECT REQUEST notification for device\n"));
324 if (acpi_bus_get_device(handle, &device)) {
325 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
326 "Device doesn't exist\n"));
327 break;
329 mem_device = acpi_driver_data(device);
330 if (!mem_device) {
331 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
332 "Driver Data is NULL\n"));
333 break;
337 * Currently disabling memory device from kernel mode
338 * TBD: Can also be disabled from user mode scripts
339 * TBD: Can also be disabled by Callback registration
340 * with generic sysfs driver
342 if (acpi_memory_disable_device(mem_device))
343 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
344 "Error in acpi_memory_disable_device\n"));
346 * TBD: Invoke acpi_bus_remove to cleanup data structures
348 break;
349 default:
350 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
351 "Unsupported event [0x%x]\n", event));
352 break;
355 return_VOID;
358 static int
359 acpi_memory_device_add(struct acpi_device *device)
361 int result;
362 struct acpi_memory_device *mem_device = NULL;
364 ACPI_FUNCTION_TRACE("acpi_memory_device_add");
366 if (!device)
367 return_VALUE(-EINVAL);
369 mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
370 if (!mem_device)
371 return_VALUE(-ENOMEM);
372 memset(mem_device, 0, sizeof(struct acpi_memory_device));
374 mem_device->handle = device->handle;
375 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
376 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
377 acpi_driver_data(device) = mem_device;
379 /* Get the range from the _CRS */
380 result = acpi_memory_get_device_resources(mem_device);
381 if (result) {
382 kfree(mem_device);
383 return_VALUE(result);
386 /* Set the device state */
387 mem_device->state = MEMORY_POWER_ON_STATE;
389 printk(KERN_INFO "%s \n", acpi_device_name(device));
391 return_VALUE(result);
394 static int
395 acpi_memory_device_remove (struct acpi_device *device, int type)
397 struct acpi_memory_device *mem_device = NULL;
399 ACPI_FUNCTION_TRACE("acpi_memory_device_remove");
401 if (!device || !acpi_driver_data(device))
402 return_VALUE(-EINVAL);
404 mem_device = (struct acpi_memory_device *) acpi_driver_data(device);
405 kfree(mem_device);
407 return_VALUE(0);
411 * Helper function to check for memory device
413 static acpi_status
414 is_memory_device(acpi_handle handle)
416 char *hardware_id;
417 acpi_status status;
418 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
419 struct acpi_device_info *info;
421 ACPI_FUNCTION_TRACE("is_memory_device");
423 status = acpi_get_object_info(handle, &buffer);
424 if (ACPI_FAILURE(status))
425 return_ACPI_STATUS(AE_ERROR);
427 info = buffer.pointer;
428 if (!(info->valid & ACPI_VALID_HID)) {
429 acpi_os_free(buffer.pointer);
430 return_ACPI_STATUS(AE_ERROR);
433 hardware_id = info->hardware_id.value;
434 if ((hardware_id == NULL) ||
435 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
436 status = AE_ERROR;
438 acpi_os_free(buffer.pointer);
439 return_ACPI_STATUS(status);
442 static acpi_status
443 acpi_memory_register_notify_handler (acpi_handle handle,
444 u32 level, void *ctxt, void **retv)
446 acpi_status status;
448 ACPI_FUNCTION_TRACE("acpi_memory_register_notify_handler");
450 status = is_memory_device(handle);
451 if (ACPI_FAILURE(status))
452 return_ACPI_STATUS(AE_OK); /* continue */
454 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
455 acpi_memory_device_notify, NULL);
456 if (ACPI_FAILURE(status)) {
457 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
458 "Error installing notify handler\n"));
459 return_ACPI_STATUS(AE_OK); /* continue */
462 return_ACPI_STATUS(status);
465 static acpi_status
466 acpi_memory_deregister_notify_handler (acpi_handle handle,
467 u32 level, void *ctxt, void **retv)
469 acpi_status status;
471 ACPI_FUNCTION_TRACE("acpi_memory_deregister_notify_handler");
473 status = is_memory_device(handle);
474 if (ACPI_FAILURE(status))
475 return_ACPI_STATUS(AE_OK); /* continue */
477 status = acpi_remove_notify_handler(handle,
478 ACPI_SYSTEM_NOTIFY, acpi_memory_device_notify);
479 if (ACPI_FAILURE(status)) {
480 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
481 "Error removing notify handler\n"));
482 return_ACPI_STATUS(AE_OK); /* continue */
485 return_ACPI_STATUS(status);
488 static int __init
489 acpi_memory_device_init (void)
491 int result;
492 acpi_status status;
494 ACPI_FUNCTION_TRACE("acpi_memory_device_init");
496 result = acpi_bus_register_driver(&acpi_memory_device_driver);
498 if (result < 0)
499 return_VALUE(-ENODEV);
501 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
502 ACPI_UINT32_MAX,
503 acpi_memory_register_notify_handler,
504 NULL, NULL);
506 if (ACPI_FAILURE (status)) {
507 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "walk_namespace failed\n"));
508 acpi_bus_unregister_driver(&acpi_memory_device_driver);
509 return_VALUE(-ENODEV);
512 return_VALUE(0);
515 static void __exit
516 acpi_memory_device_exit (void)
518 acpi_status status;
520 ACPI_FUNCTION_TRACE("acpi_memory_device_exit");
523 * Adding this to un-install notification handlers for all the device
524 * handles.
526 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
527 ACPI_UINT32_MAX,
528 acpi_memory_deregister_notify_handler,
529 NULL, NULL);
531 if (ACPI_FAILURE (status))
532 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "walk_namespace failed\n"));
534 acpi_bus_unregister_driver(&acpi_memory_device_driver);
536 return_VOID;
539 module_init(acpi_memory_device_init);
540 module_exit(acpi_memory_device_exit);