ACPI: Enable ACPI error messages w/o CONFIG_ACPI_DEBUG
[linux-2.6/linux-mips.git] / drivers / acpi / acpi_memhotplug.c
blobe49d327ccf4cd747c59dc4ddc34b088789472cf4
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>
35 #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"
41 #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
43 ACPI_MODULE_NAME("acpi_memory")
44 MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
45 MODULE_DESCRIPTION(ACPI_MEMORY_DEVICE_DRIVER_NAME);
46 MODULE_LICENSE("GPL");
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)
53 /* Memory Device States */
54 #define MEMORY_INVALID_STATE 0
55 #define MEMORY_POWER_ON_STATE 1
56 #define MEMORY_POWER_OFF_STATE 2
58 static int acpi_memory_device_add(struct acpi_device *device);
59 static int acpi_memory_device_remove(struct acpi_device *device, int type);
61 static struct acpi_driver acpi_memory_device_driver = {
62 .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,
71 struct acpi_memory_device {
72 acpi_handle handle;
73 unsigned int state; /* State of the memory device */
74 unsigned short caching; /* memory cache attribute */
75 unsigned short write_protect; /* memory read/write attribute */
76 u64 start_addr; /* Memory Range start physical addr */
77 u64 length; /* Memory Range length */
80 static int
81 acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
83 acpi_status status;
84 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85 struct acpi_resource *resource = NULL;
86 struct acpi_resource_address64 address64;
88 ACPI_FUNCTION_TRACE("acpi_memory_get_device_resources");
90 /* Get the range from the _CRS */
91 status = acpi_get_current_resources(mem_device->handle, &buffer);
92 if (ACPI_FAILURE(status))
93 return_VALUE(-EINVAL);
95 resource = (struct acpi_resource *)buffer.pointer;
96 status = acpi_resource_to_address64(resource, &address64);
97 if (ACPI_SUCCESS(status)) {
98 if (address64.resource_type == ACPI_MEMORY_RANGE) {
99 /* Populate the structure */
100 mem_device->caching = address64.info.mem.caching;
101 mem_device->write_protect =
102 address64.info.mem.write_protect;
103 mem_device->start_addr = address64.minimum;
104 mem_device->length = address64.address_length;
108 acpi_os_free(buffer.pointer);
109 return_VALUE(0);
112 static int
113 acpi_memory_get_device(acpi_handle handle,
114 struct acpi_memory_device **mem_device)
116 acpi_status status;
117 acpi_handle phandle;
118 struct acpi_device *device = NULL;
119 struct acpi_device *pdevice = NULL;
121 ACPI_FUNCTION_TRACE("acpi_memory_get_device");
123 if (!acpi_bus_get_device(handle, &device) && device)
124 goto end;
126 status = acpi_get_parent(handle, &phandle);
127 if (ACPI_FAILURE(status)) {
128 ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
129 return_VALUE(-EINVAL);
132 /* Get the parent device */
133 status = acpi_bus_get_device(phandle, &pdevice);
134 if (ACPI_FAILURE(status)) {
135 ACPI_EXCEPTION((AE_INFO, status, "Cannot get acpi bus device"));
136 return_VALUE(-EINVAL);
140 * Now add the notified device. This creates the acpi_device
141 * and invokes .add function
143 status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
144 if (ACPI_FAILURE(status)) {
145 ACPI_EXCEPTION((AE_INFO, status, "Cannot add acpi bus"));
146 return_VALUE(-EINVAL);
149 end:
150 *mem_device = acpi_driver_data(device);
151 if (!(*mem_device)) {
152 printk(KERN_ERR "\n driver data not found");
153 return_VALUE(-ENODEV);
156 return_VALUE(0);
159 static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
161 unsigned long current_status;
163 ACPI_FUNCTION_TRACE("acpi_memory_check_device");
165 /* Get device present/absent information from the _STA */
166 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA",
167 NULL, &current_status)))
168 return_VALUE(-ENODEV);
170 * Check for device status. Device should be
171 * present/enabled/functioning.
173 if (!((current_status & ACPI_MEMORY_STA_PRESENT)
174 && (current_status & ACPI_MEMORY_STA_ENABLED)
175 && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
176 return_VALUE(-ENODEV);
178 return_VALUE(0);
181 static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
183 int result;
185 ACPI_FUNCTION_TRACE("acpi_memory_enable_device");
187 /* Get the range from the _CRS */
188 result = acpi_memory_get_device_resources(mem_device);
189 if (result) {
190 ACPI_ERROR((AE_INFO, "get_device_resources failed"));
191 mem_device->state = MEMORY_INVALID_STATE;
192 return result;
196 * Tell the VM there is more memory here...
197 * Note: Assume that this function returns zero on success
199 result = add_memory(mem_device->start_addr, mem_device->length);
200 if (result) {
201 ACPI_ERROR((AE_INFO, "add_memory failed"));
202 mem_device->state = MEMORY_INVALID_STATE;
203 return result;
206 return result;
209 static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
211 acpi_status status;
212 struct acpi_object_list arg_list;
213 union acpi_object arg;
214 unsigned long current_status;
216 ACPI_FUNCTION_TRACE("acpi_memory_powerdown_device");
218 /* Issue the _EJ0 command */
219 arg_list.count = 1;
220 arg_list.pointer = &arg;
221 arg.type = ACPI_TYPE_INTEGER;
222 arg.integer.value = 1;
223 status = acpi_evaluate_object(mem_device->handle,
224 "_EJ0", &arg_list, NULL);
225 /* Return on _EJ0 failure */
226 if (ACPI_FAILURE(status)) {
227 ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
228 return_VALUE(-ENODEV);
231 /* Evalute _STA to check if the device is disabled */
232 status = acpi_evaluate_integer(mem_device->handle, "_STA",
233 NULL, &current_status);
234 if (ACPI_FAILURE(status))
235 return_VALUE(-ENODEV);
237 /* Check for device status. Device should be disabled */
238 if (current_status & ACPI_MEMORY_STA_ENABLED)
239 return_VALUE(-EINVAL);
241 return_VALUE(0);
244 static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
246 int result;
247 u64 start = mem_device->start_addr;
248 u64 len = mem_device->length;
250 ACPI_FUNCTION_TRACE("acpi_memory_disable_device");
253 * Ask the VM to offline this memory range.
254 * Note: Assume that this function returns zero on success
256 result = remove_memory(start, len);
257 if (result)
258 return_VALUE(result);
260 /* Power-off and eject the device */
261 result = acpi_memory_powerdown_device(mem_device);
262 if (result) {
263 /* Set the status of the device to invalid */
264 mem_device->state = MEMORY_INVALID_STATE;
265 return result;
268 mem_device->state = MEMORY_POWER_OFF_STATE;
269 return result;
272 static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
274 struct acpi_memory_device *mem_device;
275 struct acpi_device *device;
277 ACPI_FUNCTION_TRACE("acpi_memory_device_notify");
279 switch (event) {
280 case ACPI_NOTIFY_BUS_CHECK:
281 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
282 "\nReceived BUS CHECK notification for device\n"));
283 /* Fall Through */
284 case ACPI_NOTIFY_DEVICE_CHECK:
285 if (event == ACPI_NOTIFY_DEVICE_CHECK)
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
287 "\nReceived DEVICE CHECK notification for device\n"));
288 if (acpi_memory_get_device(handle, &mem_device)) {
289 ACPI_ERROR((AE_INFO, "Cannot find driver data"));
290 return_VOID;
293 if (!acpi_memory_check_device(mem_device)) {
294 if (acpi_memory_enable_device(mem_device))
295 ACPI_ERROR((AE_INFO,
296 "Cannot enable memory device"));
298 break;
299 case ACPI_NOTIFY_EJECT_REQUEST:
300 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
301 "\nReceived EJECT REQUEST notification for device\n"));
303 if (acpi_bus_get_device(handle, &device)) {
304 ACPI_ERROR((AE_INFO, "Device doesn't exist"));
305 break;
307 mem_device = acpi_driver_data(device);
308 if (!mem_device) {
309 ACPI_ERROR((AE_INFO, "Driver Data is NULL"));
310 break;
314 * Currently disabling memory device from kernel mode
315 * TBD: Can also be disabled from user mode scripts
316 * TBD: Can also be disabled by Callback registration
317 * with generic sysfs driver
319 if (acpi_memory_disable_device(mem_device))
320 ACPI_ERROR((AE_INFO,
321 "Disable memory device\n"));
323 * TBD: Invoke acpi_bus_remove to cleanup data structures
325 break;
326 default:
327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
328 "Unsupported event [0x%x]\n", event));
329 break;
332 return_VOID;
335 static int acpi_memory_device_add(struct acpi_device *device)
337 int result;
338 struct acpi_memory_device *mem_device = NULL;
340 ACPI_FUNCTION_TRACE("acpi_memory_device_add");
342 if (!device)
343 return_VALUE(-EINVAL);
345 mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
346 if (!mem_device)
347 return_VALUE(-ENOMEM);
348 memset(mem_device, 0, sizeof(struct acpi_memory_device));
350 mem_device->handle = device->handle;
351 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
352 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
353 acpi_driver_data(device) = mem_device;
355 /* Get the range from the _CRS */
356 result = acpi_memory_get_device_resources(mem_device);
357 if (result) {
358 kfree(mem_device);
359 return_VALUE(result);
362 /* Set the device state */
363 mem_device->state = MEMORY_POWER_ON_STATE;
365 printk(KERN_INFO "%s \n", acpi_device_name(device));
367 return_VALUE(result);
370 static int acpi_memory_device_remove(struct acpi_device *device, int type)
372 struct acpi_memory_device *mem_device = NULL;
374 ACPI_FUNCTION_TRACE("acpi_memory_device_remove");
376 if (!device || !acpi_driver_data(device))
377 return_VALUE(-EINVAL);
379 mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
380 kfree(mem_device);
382 return_VALUE(0);
386 * Helper function to check for memory device
388 static acpi_status is_memory_device(acpi_handle handle)
390 char *hardware_id;
391 acpi_status status;
392 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
393 struct acpi_device_info *info;
395 ACPI_FUNCTION_TRACE("is_memory_device");
397 status = acpi_get_object_info(handle, &buffer);
398 if (ACPI_FAILURE(status))
399 return_ACPI_STATUS(status);
401 info = buffer.pointer;
402 if (!(info->valid & ACPI_VALID_HID)) {
403 acpi_os_free(buffer.pointer);
404 return_ACPI_STATUS(AE_ERROR);
407 hardware_id = info->hardware_id.value;
408 if ((hardware_id == NULL) ||
409 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
410 status = AE_ERROR;
412 acpi_os_free(buffer.pointer);
413 return_ACPI_STATUS(status);
416 static acpi_status
417 acpi_memory_register_notify_handler(acpi_handle handle,
418 u32 level, void *ctxt, void **retv)
420 acpi_status status;
422 ACPI_FUNCTION_TRACE("acpi_memory_register_notify_handler");
424 status = is_memory_device(handle);
425 if (ACPI_FAILURE(status)){
426 ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
427 return_ACPI_STATUS(AE_OK); /* continue */
430 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
431 acpi_memory_device_notify, NULL);
432 /* continue */
433 return_ACPI_STATUS(AE_OK);
436 static acpi_status
437 acpi_memory_deregister_notify_handler(acpi_handle handle,
438 u32 level, void *ctxt, void **retv)
440 acpi_status status;
442 ACPI_FUNCTION_TRACE("acpi_memory_deregister_notify_handler");
444 status = is_memory_device(handle);
445 if (ACPI_FAILURE(status)){
446 ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
447 return_ACPI_STATUS(AE_OK); /* continue */
450 status = acpi_remove_notify_handler(handle,
451 ACPI_SYSTEM_NOTIFY,
452 acpi_memory_device_notify);
454 return_ACPI_STATUS(AE_OK); /* continue */
457 static int __init acpi_memory_device_init(void)
459 int result;
460 acpi_status status;
462 ACPI_FUNCTION_TRACE("acpi_memory_device_init");
464 result = acpi_bus_register_driver(&acpi_memory_device_driver);
466 if (result < 0)
467 return_VALUE(-ENODEV);
469 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
470 ACPI_UINT32_MAX,
471 acpi_memory_register_notify_handler,
472 NULL, NULL);
474 if (ACPI_FAILURE(status)) {
475 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
476 acpi_bus_unregister_driver(&acpi_memory_device_driver);
477 return_VALUE(-ENODEV);
480 return_VALUE(0);
483 static void __exit acpi_memory_device_exit(void)
485 acpi_status status;
487 ACPI_FUNCTION_TRACE("acpi_memory_device_exit");
490 * Adding this to un-install notification handlers for all the device
491 * handles.
493 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
494 ACPI_UINT32_MAX,
495 acpi_memory_deregister_notify_handler,
496 NULL, NULL);
498 if (ACPI_FAILURE(status))
499 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
501 acpi_bus_unregister_driver(&acpi_memory_device_driver);
503 return_VOID;
506 module_init(acpi_memory_device_init);
507 module_exit(acpi_memory_device_exit);