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