Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
| 40 | |
| 41 | #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT |
| 42 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 43 | ACPI_MODULE_NAME("acpi_memory") |
| 44 | MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | MODULE_DESCRIPTION(ACPI_MEMORY_DEVICE_DRIVER_NAME); |
| 46 | MODULE_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 Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 58 | static int acpi_memory_device_add(struct acpi_device *device); |
| 59 | static int acpi_memory_device_remove(struct acpi_device *device, int type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | static struct acpi_driver acpi_memory_device_driver = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 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, |
| 68 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | struct acpi_memory_device { |
| 72 | acpi_handle handle; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 73 | unsigned int state; /* State of the memory device */ |
KAMEZAWA Hiroyuki | 3963f00 | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 74 | unsigned short caching; /* memory cache attribute */ |
| 75 | unsigned short write_protect; /* memory read/write attribute */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | u64 start_addr; /* Memory Range start physical addr */ |
| 77 | u64 end_addr; /* Memory Range end physical addr */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static int |
| 81 | acpi_memory_get_device_resources(struct acpi_memory_device *mem_device) |
| 82 | { |
| 83 | acpi_status status; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 84 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | struct acpi_resource *resource = NULL; |
| 86 | struct acpi_resource_address64 address64; |
| 87 | |
| 88 | ACPI_FUNCTION_TRACE("acpi_memory_get_device_resources"); |
| 89 | |
| 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); |
| 94 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 95 | resource = (struct acpi_resource *)buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
KAMEZAWA Hiroyuki | 3963f00 | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 100 | mem_device->caching = |
| 101 | address64.info.mem.caching; |
| 102 | mem_device->write_protect = |
| 103 | address64.info.mem.write_protect; |
Bob Moore | 50eca3e | 2005-09-30 19:03:00 -0400 | [diff] [blame] | 104 | mem_device->start_addr = address64.minimum; |
| 105 | mem_device->end_addr = address64.maximum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | acpi_os_free(buffer.pointer); |
| 110 | return_VALUE(0); |
| 111 | } |
| 112 | |
| 113 | static int |
| 114 | acpi_memory_get_device(acpi_handle handle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 115 | struct acpi_memory_device **mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | acpi_status status; |
| 118 | acpi_handle phandle; |
| 119 | struct acpi_device *device = NULL; |
| 120 | struct acpi_device *pdevice = NULL; |
| 121 | |
| 122 | ACPI_FUNCTION_TRACE("acpi_memory_get_device"); |
| 123 | |
| 124 | if (!acpi_bus_get_device(handle, &device) && device) |
| 125 | goto end; |
| 126 | |
| 127 | status = acpi_get_parent(handle, &phandle); |
| 128 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 129 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_get_parent\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return_VALUE(-EINVAL); |
| 131 | } |
| 132 | |
| 133 | /* Get the parent device */ |
| 134 | status = acpi_bus_get_device(phandle, &pdevice); |
| 135 | if (ACPI_FAILURE(status)) { |
| 136 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 137 | "Error in acpi_bus_get_device\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return_VALUE(-EINVAL); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Now add the notified device. This creates the acpi_device |
| 143 | * and invokes .add function |
| 144 | */ |
| 145 | status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE); |
| 146 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 147 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_bus_add\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return_VALUE(-EINVAL); |
| 149 | } |
| 150 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 151 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | *mem_device = acpi_driver_data(device); |
| 153 | if (!(*mem_device)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 154 | printk(KERN_ERR "\n driver data not found"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return_VALUE(-ENODEV); |
| 156 | } |
| 157 | |
| 158 | return_VALUE(0); |
| 159 | } |
| 160 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 161 | static int acpi_memory_check_device(struct acpi_memory_device *mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | unsigned long current_status; |
| 164 | |
| 165 | ACPI_FUNCTION_TRACE("acpi_memory_check_device"); |
| 166 | |
| 167 | /* Get device present/absent information from the _STA */ |
| 168 | if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 169 | NULL, ¤t_status))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return_VALUE(-ENODEV); |
| 171 | /* |
| 172 | * Check for device status. Device should be |
| 173 | * present/enabled/functioning. |
| 174 | */ |
| 175 | if (!((current_status & ACPI_MEMORY_STA_PRESENT) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 176 | && (current_status & ACPI_MEMORY_STA_ENABLED) |
| 177 | && (current_status & ACPI_MEMORY_STA_FUNCTIONAL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | return_VALUE(-ENODEV); |
| 179 | |
| 180 | return_VALUE(0); |
| 181 | } |
| 182 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 183 | static int acpi_memory_enable_device(struct acpi_memory_device *mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | int result; |
| 186 | |
| 187 | ACPI_FUNCTION_TRACE("acpi_memory_enable_device"); |
| 188 | |
| 189 | /* Get the range from the _CRS */ |
| 190 | result = acpi_memory_get_device_resources(mem_device); |
| 191 | if (result) { |
| 192 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 193 | "\nget_device_resources failed\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | mem_device->state = MEMORY_INVALID_STATE; |
| 195 | return result; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Tell the VM there is more memory here... |
| 200 | * Note: Assume that this function returns zero on success |
| 201 | */ |
| 202 | result = add_memory(mem_device->start_addr, |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 203 | (mem_device->end_addr - mem_device->start_addr) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (result) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 205 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "\nadd_memory failed\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | mem_device->state = MEMORY_INVALID_STATE; |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | return result; |
| 211 | } |
| 212 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 213 | static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
| 215 | acpi_status status; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 216 | struct acpi_object_list arg_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | union acpi_object arg; |
| 218 | unsigned long current_status; |
| 219 | |
| 220 | ACPI_FUNCTION_TRACE("acpi_memory_powerdown_device"); |
| 221 | |
| 222 | /* Issue the _EJ0 command */ |
| 223 | arg_list.count = 1; |
| 224 | arg_list.pointer = &arg; |
| 225 | arg.type = ACPI_TYPE_INTEGER; |
| 226 | arg.integer.value = 1; |
| 227 | status = acpi_evaluate_object(mem_device->handle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 228 | "_EJ0", &arg_list, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* Return on _EJ0 failure */ |
| 230 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 231 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_EJ0 failed.\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return_VALUE(-ENODEV); |
| 233 | } |
| 234 | |
| 235 | /* Evalute _STA to check if the device is disabled */ |
| 236 | status = acpi_evaluate_integer(mem_device->handle, "_STA", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 237 | NULL, ¤t_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (ACPI_FAILURE(status)) |
| 239 | return_VALUE(-ENODEV); |
| 240 | |
| 241 | /* Check for device status. Device should be disabled */ |
| 242 | if (current_status & ACPI_MEMORY_STA_ENABLED) |
| 243 | return_VALUE(-EINVAL); |
| 244 | |
| 245 | return_VALUE(0); |
| 246 | } |
| 247 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 248 | static int acpi_memory_disable_device(struct acpi_memory_device *mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | int result; |
| 251 | u64 start = mem_device->start_addr; |
| 252 | u64 len = mem_device->end_addr - start + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | ACPI_FUNCTION_TRACE("acpi_memory_disable_device"); |
| 255 | |
| 256 | /* |
| 257 | * Ask the VM to offline this memory range. |
| 258 | * Note: Assume that this function returns zero on success |
| 259 | */ |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 260 | result = remove_memory(start, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | if (result) { |
| 262 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Hot-Remove failed.\n")); |
| 263 | return_VALUE(result); |
| 264 | } |
| 265 | |
| 266 | /* Power-off and eject the device */ |
| 267 | result = acpi_memory_powerdown_device(mem_device); |
| 268 | if (result) { |
| 269 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 270 | "Device Power Down failed.\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | /* Set the status of the device to invalid */ |
| 272 | mem_device->state = MEMORY_INVALID_STATE; |
| 273 | return result; |
| 274 | } |
| 275 | |
| 276 | mem_device->state = MEMORY_POWER_OFF_STATE; |
| 277 | return result; |
| 278 | } |
| 279 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 280 | static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | struct acpi_memory_device *mem_device; |
| 283 | struct acpi_device *device; |
| 284 | |
| 285 | ACPI_FUNCTION_TRACE("acpi_memory_device_notify"); |
| 286 | |
| 287 | switch (event) { |
| 288 | case ACPI_NOTIFY_BUS_CHECK: |
| 289 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 290 | "\nReceived BUS CHECK notification for device\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | /* Fall Through */ |
| 292 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 293 | if (event == ACPI_NOTIFY_DEVICE_CHECK) |
| 294 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 295 | "\nReceived DEVICE CHECK notification for device\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (acpi_memory_get_device(handle, &mem_device)) { |
| 297 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 298 | "Error in finding driver data\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return_VOID; |
| 300 | } |
| 301 | |
| 302 | if (!acpi_memory_check_device(mem_device)) { |
| 303 | if (acpi_memory_enable_device(mem_device)) |
| 304 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 305 | "Error in acpi_memory_enable_device\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | break; |
| 308 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 309 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 310 | "\nReceived EJECT REQUEST notification for device\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | if (acpi_bus_get_device(handle, &device)) { |
| 313 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 314 | "Device doesn't exist\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | mem_device = acpi_driver_data(device); |
| 318 | if (!mem_device) { |
| 319 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 320 | "Driver Data is NULL\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Currently disabling memory device from kernel mode |
| 326 | * TBD: Can also be disabled from user mode scripts |
| 327 | * TBD: Can also be disabled by Callback registration |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 328 | * with generic sysfs driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | */ |
| 330 | if (acpi_memory_disable_device(mem_device)) |
| 331 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 332 | "Error in acpi_memory_disable_device\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | /* |
| 334 | * TBD: Invoke acpi_bus_remove to cleanup data structures |
| 335 | */ |
| 336 | break; |
| 337 | default: |
| 338 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 339 | "Unsupported event [0x%x]\n", event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | |
| 343 | return_VOID; |
| 344 | } |
| 345 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 346 | static int acpi_memory_device_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
| 348 | int result; |
| 349 | struct acpi_memory_device *mem_device = NULL; |
| 350 | |
| 351 | ACPI_FUNCTION_TRACE("acpi_memory_device_add"); |
| 352 | |
| 353 | if (!device) |
| 354 | return_VALUE(-EINVAL); |
| 355 | |
| 356 | mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL); |
| 357 | if (!mem_device) |
| 358 | return_VALUE(-ENOMEM); |
| 359 | memset(mem_device, 0, sizeof(struct acpi_memory_device)); |
| 360 | |
| 361 | mem_device->handle = device->handle; |
| 362 | sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME); |
| 363 | sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS); |
| 364 | acpi_driver_data(device) = mem_device; |
| 365 | |
| 366 | /* Get the range from the _CRS */ |
| 367 | result = acpi_memory_get_device_resources(mem_device); |
| 368 | if (result) { |
| 369 | kfree(mem_device); |
| 370 | return_VALUE(result); |
| 371 | } |
| 372 | |
| 373 | /* Set the device state */ |
| 374 | mem_device->state = MEMORY_POWER_ON_STATE; |
| 375 | |
| 376 | printk(KERN_INFO "%s \n", acpi_device_name(device)); |
| 377 | |
| 378 | return_VALUE(result); |
| 379 | } |
| 380 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 381 | static int acpi_memory_device_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
| 383 | struct acpi_memory_device *mem_device = NULL; |
| 384 | |
| 385 | ACPI_FUNCTION_TRACE("acpi_memory_device_remove"); |
| 386 | |
| 387 | if (!device || !acpi_driver_data(device)) |
| 388 | return_VALUE(-EINVAL); |
| 389 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 390 | mem_device = (struct acpi_memory_device *)acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | kfree(mem_device); |
| 392 | |
| 393 | return_VALUE(0); |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Helper function to check for memory device |
| 398 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 399 | static acpi_status is_memory_device(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
| 401 | char *hardware_id; |
| 402 | acpi_status status; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 403 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | struct acpi_device_info *info; |
| 405 | |
| 406 | ACPI_FUNCTION_TRACE("is_memory_device"); |
| 407 | |
| 408 | status = acpi_get_object_info(handle, &buffer); |
| 409 | if (ACPI_FAILURE(status)) |
| 410 | return_ACPI_STATUS(AE_ERROR); |
| 411 | |
| 412 | info = buffer.pointer; |
| 413 | if (!(info->valid & ACPI_VALID_HID)) { |
| 414 | acpi_os_free(buffer.pointer); |
| 415 | return_ACPI_STATUS(AE_ERROR); |
| 416 | } |
| 417 | |
| 418 | hardware_id = info->hardware_id.value; |
| 419 | if ((hardware_id == NULL) || |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 420 | (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | status = AE_ERROR; |
| 422 | |
| 423 | acpi_os_free(buffer.pointer); |
| 424 | return_ACPI_STATUS(status); |
| 425 | } |
| 426 | |
| 427 | static acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 428 | acpi_memory_register_notify_handler(acpi_handle handle, |
| 429 | u32 level, void *ctxt, void **retv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
| 431 | acpi_status status; |
| 432 | |
| 433 | ACPI_FUNCTION_TRACE("acpi_memory_register_notify_handler"); |
| 434 | |
| 435 | status = is_memory_device(handle); |
| 436 | if (ACPI_FAILURE(status)) |
| 437 | return_ACPI_STATUS(AE_OK); /* continue */ |
| 438 | |
| 439 | status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 440 | acpi_memory_device_notify, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (ACPI_FAILURE(status)) { |
| 442 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 443 | "Error installing notify handler\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | return_ACPI_STATUS(AE_OK); /* continue */ |
| 445 | } |
| 446 | |
| 447 | return_ACPI_STATUS(status); |
| 448 | } |
| 449 | |
| 450 | static acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 451 | acpi_memory_deregister_notify_handler(acpi_handle handle, |
| 452 | u32 level, void *ctxt, void **retv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
| 454 | acpi_status status; |
| 455 | |
| 456 | ACPI_FUNCTION_TRACE("acpi_memory_deregister_notify_handler"); |
| 457 | |
| 458 | status = is_memory_device(handle); |
| 459 | if (ACPI_FAILURE(status)) |
| 460 | return_ACPI_STATUS(AE_OK); /* continue */ |
| 461 | |
| 462 | status = acpi_remove_notify_handler(handle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 463 | ACPI_SYSTEM_NOTIFY, |
| 464 | acpi_memory_device_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (ACPI_FAILURE(status)) { |
| 466 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 467 | "Error removing notify handler\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return_ACPI_STATUS(AE_OK); /* continue */ |
| 469 | } |
| 470 | |
| 471 | return_ACPI_STATUS(status); |
| 472 | } |
| 473 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 474 | static int __init acpi_memory_device_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
| 476 | int result; |
| 477 | acpi_status status; |
| 478 | |
| 479 | ACPI_FUNCTION_TRACE("acpi_memory_device_init"); |
| 480 | |
| 481 | result = acpi_bus_register_driver(&acpi_memory_device_driver); |
| 482 | |
| 483 | if (result < 0) |
| 484 | return_VALUE(-ENODEV); |
| 485 | |
| 486 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 487 | ACPI_UINT32_MAX, |
| 488 | acpi_memory_register_notify_handler, |
| 489 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 491 | if (ACPI_FAILURE(status)) { |
| 492 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | acpi_bus_unregister_driver(&acpi_memory_device_driver); |
| 494 | return_VALUE(-ENODEV); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 495 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | return_VALUE(0); |
| 498 | } |
| 499 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 500 | static void __exit acpi_memory_device_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
| 502 | acpi_status status; |
| 503 | |
| 504 | ACPI_FUNCTION_TRACE("acpi_memory_device_exit"); |
| 505 | |
| 506 | /* |
| 507 | * Adding this to un-install notification handlers for all the device |
| 508 | * handles. |
| 509 | */ |
| 510 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 511 | ACPI_UINT32_MAX, |
| 512 | acpi_memory_deregister_notify_handler, |
| 513 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 515 | if (ACPI_FAILURE(status)) |
| 516 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
| 518 | acpi_bus_unregister_driver(&acpi_memory_device_driver); |
| 519 | |
| 520 | return_VOID; |
| 521 | } |
| 522 | |
| 523 | module_init(acpi_memory_device_init); |
| 524 | module_exit(acpi_memory_device_exit); |