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;
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);
113 acpi_memory_get_device(acpi_handle handle,
114 struct acpi_memory_device **mem_device)
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)
126 status = acpi_get_parent(handle, &phandle);
127 if (ACPI_FAILURE(status)) {
128 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_get_parent\n"));
129 return_VALUE(-EINVAL);
132 /* Get the parent device */
133 status = acpi_bus_get_device(phandle, &pdevice);
134 if (ACPI_FAILURE(status)) {
135 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
136 "Error in acpi_bus_get_device\n"));
137 return_VALUE(-EINVAL);
141 * Now add the notified device. This creates the acpi_device
142 * and invokes .add function
144 status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
145 if (ACPI_FAILURE(status)) {
146 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_bus_add\n"));
147 return_VALUE(-EINVAL);
151 *mem_device = acpi_driver_data(device);
152 if (!(*mem_device)) {
153 printk(KERN_ERR "\n driver data not found");
154 return_VALUE(-ENODEV);
160 static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
162 unsigned long current_status;
164 ACPI_FUNCTION_TRACE("acpi_memory_check_device");
166 /* Get device present/absent information from the _STA */
167 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA",
168 NULL, ¤t_status)))
169 return_VALUE(-ENODEV);
171 * Check for device status. Device should be
172 * present/enabled/functioning.
174 if (!((current_status & ACPI_MEMORY_STA_PRESENT)
175 && (current_status & ACPI_MEMORY_STA_ENABLED)
176 && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
177 return_VALUE(-ENODEV);
182 static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
186 ACPI_FUNCTION_TRACE("acpi_memory_enable_device");
188 /* Get the range from the _CRS */
189 result = acpi_memory_get_device_resources(mem_device);
191 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
192 "\nget_device_resources failed\n"));
193 mem_device->state = MEMORY_INVALID_STATE;
198 * Tell the VM there is more memory here...
199 * Note: Assume that this function returns zero on success
201 result = add_memory(mem_device->start_addr, mem_device->length);
203 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "\nadd_memory failed\n"));
204 mem_device->state = MEMORY_INVALID_STATE;
211 static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
214 struct acpi_object_list arg_list;
215 union acpi_object arg;
216 unsigned long current_status;
218 ACPI_FUNCTION_TRACE("acpi_memory_powerdown_device");
220 /* Issue the _EJ0 command */
222 arg_list.pointer = &arg;
223 arg.type = ACPI_TYPE_INTEGER;
224 arg.integer.value = 1;
225 status = acpi_evaluate_object(mem_device->handle,
226 "_EJ0", &arg_list, NULL);
227 /* Return on _EJ0 failure */
228 if (ACPI_FAILURE(status)) {
229 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_EJ0 failed.\n"));
230 return_VALUE(-ENODEV);
233 /* Evalute _STA to check if the device is disabled */
234 status = acpi_evaluate_integer(mem_device->handle, "_STA",
235 NULL, ¤t_status);
236 if (ACPI_FAILURE(status))
237 return_VALUE(-ENODEV);
239 /* Check for device status. Device should be disabled */
240 if (current_status & ACPI_MEMORY_STA_ENABLED)
241 return_VALUE(-EINVAL);
246 static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
249 u64 start = mem_device->start_addr;
250 u64 len = mem_device->length;
252 ACPI_FUNCTION_TRACE("acpi_memory_disable_device");
255 * Ask the VM to offline this memory range.
256 * Note: Assume that this function returns zero on success
258 result = remove_memory(start, len);
260 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Hot-Remove failed.\n"));
261 return_VALUE(result);
264 /* Power-off and eject the device */
265 result = acpi_memory_powerdown_device(mem_device);
267 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
268 "Device Power Down failed.\n"));
269 /* Set the status of the device to invalid */
270 mem_device->state = MEMORY_INVALID_STATE;
274 mem_device->state = MEMORY_POWER_OFF_STATE;
278 static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
280 struct acpi_memory_device *mem_device;
281 struct acpi_device *device;
283 ACPI_FUNCTION_TRACE("acpi_memory_device_notify");
286 case ACPI_NOTIFY_BUS_CHECK:
287 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
288 "\nReceived BUS CHECK notification for device\n"));
290 case ACPI_NOTIFY_DEVICE_CHECK:
291 if (event == ACPI_NOTIFY_DEVICE_CHECK)
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
293 "\nReceived DEVICE CHECK notification for device\n"));
294 if (acpi_memory_get_device(handle, &mem_device)) {
295 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
296 "Error in finding driver data\n"));
300 if (!acpi_memory_check_device(mem_device)) {
301 if (acpi_memory_enable_device(mem_device))
302 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
303 "Error in acpi_memory_enable_device\n"));
306 case ACPI_NOTIFY_EJECT_REQUEST:
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308 "\nReceived EJECT REQUEST notification for device\n"));
310 if (acpi_bus_get_device(handle, &device)) {
311 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
312 "Device doesn't exist\n"));
315 mem_device = acpi_driver_data(device);
317 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
318 "Driver Data is NULL\n"));
323 * Currently disabling memory device from kernel mode
324 * TBD: Can also be disabled from user mode scripts
325 * TBD: Can also be disabled by Callback registration
326 * with generic sysfs driver
328 if (acpi_memory_disable_device(mem_device))
329 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
330 "Error in acpi_memory_disable_device\n"));
332 * TBD: Invoke acpi_bus_remove to cleanup data structures
336 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
337 "Unsupported event [0x%x]\n", event));
344 static int acpi_memory_device_add(struct acpi_device *device)
347 struct acpi_memory_device *mem_device = NULL;
349 ACPI_FUNCTION_TRACE("acpi_memory_device_add");
352 return_VALUE(-EINVAL);
354 mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
356 return_VALUE(-ENOMEM);
357 memset(mem_device, 0, sizeof(struct acpi_memory_device));
359 mem_device->handle = device->handle;
360 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
361 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
362 acpi_driver_data(device) = mem_device;
364 /* Get the range from the _CRS */
365 result = acpi_memory_get_device_resources(mem_device);
368 return_VALUE(result);
371 /* Set the device state */
372 mem_device->state = MEMORY_POWER_ON_STATE;
374 printk(KERN_INFO "%s \n", acpi_device_name(device));
376 return_VALUE(result);
379 static int acpi_memory_device_remove(struct acpi_device *device, int type)
381 struct acpi_memory_device *mem_device = NULL;
383 ACPI_FUNCTION_TRACE("acpi_memory_device_remove");
385 if (!device || !acpi_driver_data(device))
386 return_VALUE(-EINVAL);
388 mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
395 * Helper function to check for memory device
397 static acpi_status is_memory_device(acpi_handle handle)
401 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
402 struct acpi_device_info *info;
404 ACPI_FUNCTION_TRACE("is_memory_device");
406 status = acpi_get_object_info(handle, &buffer);
407 if (ACPI_FAILURE(status))
408 return_ACPI_STATUS(AE_ERROR);
410 info = buffer.pointer;
411 if (!(info->valid & ACPI_VALID_HID)) {
412 acpi_os_free(buffer.pointer);
413 return_ACPI_STATUS(AE_ERROR);
416 hardware_id = info->hardware_id.value;
417 if ((hardware_id == NULL) ||
418 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
421 acpi_os_free(buffer.pointer);
422 return_ACPI_STATUS(status);
426 acpi_memory_register_notify_handler(acpi_handle handle,
427 u32 level, void *ctxt, void **retv)
431 ACPI_FUNCTION_TRACE("acpi_memory_register_notify_handler");
433 status = is_memory_device(handle);
434 if (ACPI_FAILURE(status))
435 return_ACPI_STATUS(AE_OK); /* continue */
437 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
438 acpi_memory_device_notify, NULL);
439 if (ACPI_FAILURE(status)) {
440 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
441 "Error installing notify handler\n"));
442 return_ACPI_STATUS(AE_OK); /* continue */
445 return_ACPI_STATUS(status);
449 acpi_memory_deregister_notify_handler(acpi_handle handle,
450 u32 level, void *ctxt, void **retv)
454 ACPI_FUNCTION_TRACE("acpi_memory_deregister_notify_handler");
456 status = is_memory_device(handle);
457 if (ACPI_FAILURE(status))
458 return_ACPI_STATUS(AE_OK); /* continue */
460 status = acpi_remove_notify_handler(handle,
462 acpi_memory_device_notify);
463 if (ACPI_FAILURE(status)) {
464 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
465 "Error removing notify handler\n"));
466 return_ACPI_STATUS(AE_OK); /* continue */
469 return_ACPI_STATUS(status);
472 static int __init acpi_memory_device_init(void)
477 ACPI_FUNCTION_TRACE("acpi_memory_device_init");
479 result = acpi_bus_register_driver(&acpi_memory_device_driver);
482 return_VALUE(-ENODEV);
484 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
486 acpi_memory_register_notify_handler,
489 if (ACPI_FAILURE(status)) {
490 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
491 acpi_bus_unregister_driver(&acpi_memory_device_driver);
492 return_VALUE(-ENODEV);
498 static void __exit acpi_memory_device_exit(void)
502 ACPI_FUNCTION_TRACE("acpi_memory_device_exit");
505 * Adding this to un-install notification handlers for all the device
508 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
510 acpi_memory_deregister_notify_handler,
513 if (ACPI_FAILURE(status))
514 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
516 acpi_bus_unregister_driver(&acpi_memory_device_driver);
521 module_init(acpi_memory_device_init);
522 module_exit(acpi_memory_device_exit);