2 * Copyright (C) 2004 Intel Corporation <naveen.b.s@intel.com>
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
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
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,
66 .add = acpi_memory_device_add,
67 .remove = acpi_memory_device_remove,
71 struct acpi_memory_device {
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 */
81 acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
84 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85 struct acpi_resource *resource = NULL;
86 struct acpi_resource_address64 address64;
89 /* Get the range from the _CRS */
90 status = acpi_get_current_resources(mem_device->handle, &buffer);
91 if (ACPI_FAILURE(status))
94 resource = (struct acpi_resource *)buffer.pointer;
95 status = acpi_resource_to_address64(resource, &address64);
96 if (ACPI_SUCCESS(status)) {
97 if (address64.resource_type == ACPI_MEMORY_RANGE) {
98 /* Populate the structure */
99 mem_device->caching = address64.info.mem.caching;
100 mem_device->write_protect =
101 address64.info.mem.write_protect;
102 mem_device->start_addr = address64.minimum;
103 mem_device->length = address64.address_length;
107 acpi_os_free(buffer.pointer);
112 acpi_memory_get_device(acpi_handle handle,
113 struct acpi_memory_device **mem_device)
117 struct acpi_device *device = NULL;
118 struct acpi_device *pdevice = NULL;
121 if (!acpi_bus_get_device(handle, &device) && device)
124 status = acpi_get_parent(handle, &phandle);
125 if (ACPI_FAILURE(status)) {
126 ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
130 /* Get the parent device */
131 status = acpi_bus_get_device(phandle, &pdevice);
132 if (ACPI_FAILURE(status)) {
133 ACPI_EXCEPTION((AE_INFO, status, "Cannot get acpi bus device"));
138 * Now add the notified device. This creates the acpi_device
139 * and invokes .add function
141 status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
142 if (ACPI_FAILURE(status)) {
143 ACPI_EXCEPTION((AE_INFO, status, "Cannot add acpi bus"));
148 *mem_device = acpi_driver_data(device);
149 if (!(*mem_device)) {
150 printk(KERN_ERR "\n driver data not found");
157 static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
159 unsigned long current_status;
162 /* Get device present/absent information from the _STA */
163 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA",
164 NULL, ¤t_status)))
167 * Check for device status. Device should be
168 * present/enabled/functioning.
170 if (!((current_status & ACPI_MEMORY_STA_PRESENT)
171 && (current_status & ACPI_MEMORY_STA_ENABLED)
172 && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
178 static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
183 /* Get the range from the _CRS */
184 result = acpi_memory_get_device_resources(mem_device);
186 printk(KERN_ERR PREFIX "get_device_resources failed\n");
187 mem_device->state = MEMORY_INVALID_STATE;
192 * Tell the VM there is more memory here...
193 * Note: Assume that this function returns zero on success
195 result = add_memory(mem_device->start_addr, mem_device->length);
197 printk(KERN_ERR PREFIX "add_memory failed\n");
198 mem_device->state = MEMORY_INVALID_STATE;
205 static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
208 struct acpi_object_list arg_list;
209 union acpi_object arg;
210 unsigned long current_status;
213 /* Issue the _EJ0 command */
215 arg_list.pointer = &arg;
216 arg.type = ACPI_TYPE_INTEGER;
217 arg.integer.value = 1;
218 status = acpi_evaluate_object(mem_device->handle,
219 "_EJ0", &arg_list, NULL);
220 /* Return on _EJ0 failure */
221 if (ACPI_FAILURE(status)) {
222 ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
226 /* Evalute _STA to check if the device is disabled */
227 status = acpi_evaluate_integer(mem_device->handle, "_STA",
228 NULL, ¤t_status);
229 if (ACPI_FAILURE(status))
232 /* Check for device status. Device should be disabled */
233 if (current_status & ACPI_MEMORY_STA_ENABLED)
239 static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
242 u64 start = mem_device->start_addr;
243 u64 len = mem_device->length;
247 * Ask the VM to offline this memory range.
248 * Note: Assume that this function returns zero on success
250 result = remove_memory(start, len);
254 /* Power-off and eject the device */
255 result = acpi_memory_powerdown_device(mem_device);
257 /* Set the status of the device to invalid */
258 mem_device->state = MEMORY_INVALID_STATE;
262 mem_device->state = MEMORY_POWER_OFF_STATE;
266 static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
268 struct acpi_memory_device *mem_device;
269 struct acpi_device *device;
273 case ACPI_NOTIFY_BUS_CHECK:
274 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
275 "\nReceived BUS CHECK notification for device\n"));
277 case ACPI_NOTIFY_DEVICE_CHECK:
278 if (event == ACPI_NOTIFY_DEVICE_CHECK)
279 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
280 "\nReceived DEVICE CHECK notification for device\n"));
281 if (acpi_memory_get_device(handle, &mem_device)) {
282 printk(KERN_ERR PREFIX "Cannot find driver data\n");
286 if (!acpi_memory_check_device(mem_device)) {
287 if (acpi_memory_enable_device(mem_device))
288 printk(KERN_ERR PREFIX
289 "Cannot enable memory device\n");
292 case ACPI_NOTIFY_EJECT_REQUEST:
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
294 "\nReceived EJECT REQUEST notification for device\n"));
296 if (acpi_bus_get_device(handle, &device)) {
297 printk(KERN_ERR PREFIX "Device doesn't exist\n");
300 mem_device = acpi_driver_data(device);
302 printk(KERN_ERR PREFIX "Driver Data is NULL\n");
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
310 * with generic sysfs driver
312 if (acpi_memory_disable_device(mem_device))
313 printk(KERN_ERR PREFIX
314 "Disable memory device\n");
316 * TBD: Invoke acpi_bus_remove to cleanup data structures
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 "Unsupported event [0x%x]\n", event));
328 static int acpi_memory_device_add(struct acpi_device *device)
331 struct acpi_memory_device *mem_device = NULL;
337 mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
340 memset(mem_device, 0, sizeof(struct acpi_memory_device));
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;
347 /* Get the range from the _CRS */
348 result = acpi_memory_get_device_resources(mem_device);
354 /* Set the device state */
355 mem_device->state = MEMORY_POWER_ON_STATE;
357 printk(KERN_INFO "%s \n", acpi_device_name(device));
362 static int acpi_memory_device_remove(struct acpi_device *device, int type)
364 struct acpi_memory_device *mem_device = NULL;
367 if (!device || !acpi_driver_data(device))
370 mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
377 * Helper function to check for memory device
379 static acpi_status is_memory_device(acpi_handle handle)
383 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
384 struct acpi_device_info *info;
387 status = acpi_get_object_info(handle, &buffer);
388 if (ACPI_FAILURE(status))
391 info = buffer.pointer;
392 if (!(info->valid & ACPI_VALID_HID)) {
393 acpi_os_free(buffer.pointer);
397 hardware_id = info->hardware_id.value;
398 if ((hardware_id == NULL) ||
399 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
402 acpi_os_free(buffer.pointer);
407 acpi_memory_register_notify_handler(acpi_handle handle,
408 u32 level, void *ctxt, void **retv)
413 status = is_memory_device(handle);
414 if (ACPI_FAILURE(status)){
415 ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
416 return AE_OK; /* continue */
419 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
420 acpi_memory_device_notify, NULL);
426 acpi_memory_deregister_notify_handler(acpi_handle handle,
427 u32 level, void *ctxt, void **retv)
432 status = is_memory_device(handle);
433 if (ACPI_FAILURE(status)){
434 ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
435 return AE_OK; /* continue */
438 status = acpi_remove_notify_handler(handle,
440 acpi_memory_device_notify);
442 return AE_OK; /* continue */
445 static int __init acpi_memory_device_init(void)
451 result = acpi_bus_register_driver(&acpi_memory_device_driver);
456 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
458 acpi_memory_register_notify_handler,
461 if (ACPI_FAILURE(status)) {
462 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
463 acpi_bus_unregister_driver(&acpi_memory_device_driver);
470 static void __exit acpi_memory_device_exit(void)
476 * Adding this to un-install notification handlers for all the device
479 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
481 acpi_memory_deregister_notify_handler,
484 if (ACPI_FAILURE(status))
485 ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
487 acpi_bus_unregister_driver(&acpi_memory_device_driver);
492 module_init(acpi_memory_device_init);
493 module_exit(acpi_memory_device_exit);