Merge branch 'master'
[deliverable/linux.git] / drivers / acpi / acpi_memhotplug.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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
4be44fcd
LB
43ACPI_MODULE_NAME("acpi_memory")
44 MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
1da177e4
LT
45MODULE_DESCRIPTION(ACPI_MEMORY_DEVICE_DRIVER_NAME);
46MODULE_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
4be44fcd
LB
58static int acpi_memory_device_add(struct acpi_device *device);
59static int acpi_memory_device_remove(struct acpi_device *device, int type);
1da177e4
LT
60
61static struct acpi_driver acpi_memory_device_driver = {
4be44fcd
LB
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 },
1da177e4
LT
69};
70
71struct acpi_memory_device {
72 acpi_handle handle;
4be44fcd 73 unsigned int state; /* State of the memory device */
1da177e4 74 unsigned short cache_attribute; /* memory cache attribute */
4be44fcd
LB
75 unsigned short read_write_attribute; /* memory read/write attribute */
76 u64 start_addr; /* Memory Range start physical addr */
77 u64 end_addr; /* Memory Range end physical addr */
1da177e4
LT
78};
79
1da177e4
LT
80static int
81acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
82{
83 acpi_status status;
4be44fcd 84 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4
LT
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
4be44fcd 95 resource = (struct acpi_resource *)buffer.pointer;
1da177e4
LT
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->cache_attribute =
4be44fcd 101 address64.attribute.memory.cache_attribute;
1da177e4 102 mem_device->read_write_attribute =
4be44fcd 103 address64.attribute.memory.read_write_attribute;
1da177e4
LT
104 mem_device->start_addr = address64.min_address_range;
105 mem_device->end_addr = address64.max_address_range;
106 }
107 }
108
109 acpi_os_free(buffer.pointer);
110 return_VALUE(0);
111}
112
113static int
114acpi_memory_get_device(acpi_handle handle,
4be44fcd 115 struct acpi_memory_device **mem_device)
1da177e4
LT
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)) {
4be44fcd 129 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_get_parent\n"));
1da177e4
LT
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,
4be44fcd 137 "Error in acpi_bus_get_device\n"));
1da177e4
LT
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)) {
4be44fcd 147 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_bus_add\n"));
1da177e4
LT
148 return_VALUE(-EINVAL);
149 }
150
4be44fcd 151 end:
1da177e4
LT
152 *mem_device = acpi_driver_data(device);
153 if (!(*mem_device)) {
4be44fcd 154 printk(KERN_ERR "\n driver data not found");
1da177e4
LT
155 return_VALUE(-ENODEV);
156 }
157
158 return_VALUE(0);
159}
160
4be44fcd 161static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
1da177e4
LT
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",
4be44fcd 169 NULL, &current_status)))
1da177e4
LT
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)
4be44fcd
LB
176 && (current_status & ACPI_MEMORY_STA_ENABLED)
177 && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
1da177e4
LT
178 return_VALUE(-ENODEV);
179
180 return_VALUE(0);
181}
182
4be44fcd 183static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
1da177e4
LT
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,
4be44fcd 193 "\nget_device_resources failed\n"));
1da177e4
LT
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,
0b0acbec 203 (mem_device->end_addr - mem_device->start_addr) + 1);
1da177e4 204 if (result) {
4be44fcd 205 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "\nadd_memory failed\n"));
1da177e4
LT
206 mem_device->state = MEMORY_INVALID_STATE;
207 return result;
208 }
209
210 return result;
211}
212
4be44fcd 213static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
1da177e4
LT
214{
215 acpi_status status;
4be44fcd 216 struct acpi_object_list arg_list;
1da177e4
LT
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,
4be44fcd 228 "_EJ0", &arg_list, NULL);
1da177e4
LT
229 /* Return on _EJ0 failure */
230 if (ACPI_FAILURE(status)) {
4be44fcd 231 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_EJ0 failed.\n"));
1da177e4
LT
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",
4be44fcd 237 NULL, &current_status);
1da177e4
LT
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
4be44fcd 248static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
1da177e4
LT
249{
250 int result;
251 u64 start = mem_device->start_addr;
252 u64 len = mem_device->end_addr - start + 1;
253 unsigned long attr = mem_device->read_write_attribute;
254
255 ACPI_FUNCTION_TRACE("acpi_memory_disable_device");
256
257 /*
258 * Ask the VM to offline this memory range.
259 * Note: Assume that this function returns zero on success
260 */
0b0acbec 261 result = remove_memory(start, len);
1da177e4
LT
262 if (result) {
263 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Hot-Remove failed.\n"));
264 return_VALUE(result);
265 }
266
267 /* Power-off and eject the device */
268 result = acpi_memory_powerdown_device(mem_device);
269 if (result) {
270 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 271 "Device Power Down failed.\n"));
1da177e4
LT
272 /* Set the status of the device to invalid */
273 mem_device->state = MEMORY_INVALID_STATE;
274 return result;
275 }
276
277 mem_device->state = MEMORY_POWER_OFF_STATE;
278 return result;
279}
280
4be44fcd 281static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
1da177e4
LT
282{
283 struct acpi_memory_device *mem_device;
284 struct acpi_device *device;
285
286 ACPI_FUNCTION_TRACE("acpi_memory_device_notify");
287
288 switch (event) {
289 case ACPI_NOTIFY_BUS_CHECK:
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 291 "\nReceived BUS CHECK notification for device\n"));
1da177e4
LT
292 /* Fall Through */
293 case ACPI_NOTIFY_DEVICE_CHECK:
294 if (event == ACPI_NOTIFY_DEVICE_CHECK)
295 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 296 "\nReceived DEVICE CHECK notification for device\n"));
1da177e4
LT
297 if (acpi_memory_get_device(handle, &mem_device)) {
298 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 299 "Error in finding driver data\n"));
1da177e4
LT
300 return_VOID;
301 }
302
303 if (!acpi_memory_check_device(mem_device)) {
304 if (acpi_memory_enable_device(mem_device))
305 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 306 "Error in acpi_memory_enable_device\n"));
1da177e4
LT
307 }
308 break;
309 case ACPI_NOTIFY_EJECT_REQUEST:
310 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 311 "\nReceived EJECT REQUEST notification for device\n"));
1da177e4
LT
312
313 if (acpi_bus_get_device(handle, &device)) {
314 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 315 "Device doesn't exist\n"));
1da177e4
LT
316 break;
317 }
318 mem_device = acpi_driver_data(device);
319 if (!mem_device) {
320 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 321 "Driver Data is NULL\n"));
1da177e4
LT
322 break;
323 }
324
325 /*
326 * Currently disabling memory device from kernel mode
327 * TBD: Can also be disabled from user mode scripts
328 * TBD: Can also be disabled by Callback registration
4be44fcd 329 * with generic sysfs driver
1da177e4
LT
330 */
331 if (acpi_memory_disable_device(mem_device))
332 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 333 "Error in acpi_memory_disable_device\n"));
1da177e4
LT
334 /*
335 * TBD: Invoke acpi_bus_remove to cleanup data structures
336 */
337 break;
338 default:
339 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 340 "Unsupported event [0x%x]\n", event));
1da177e4
LT
341 break;
342 }
343
344 return_VOID;
345}
346
4be44fcd 347static int acpi_memory_device_add(struct acpi_device *device)
1da177e4
LT
348{
349 int result;
350 struct acpi_memory_device *mem_device = NULL;
351
352 ACPI_FUNCTION_TRACE("acpi_memory_device_add");
353
354 if (!device)
355 return_VALUE(-EINVAL);
356
357 mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
358 if (!mem_device)
359 return_VALUE(-ENOMEM);
360 memset(mem_device, 0, sizeof(struct acpi_memory_device));
361
362 mem_device->handle = device->handle;
363 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
364 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
365 acpi_driver_data(device) = mem_device;
366
367 /* Get the range from the _CRS */
368 result = acpi_memory_get_device_resources(mem_device);
369 if (result) {
370 kfree(mem_device);
371 return_VALUE(result);
372 }
373
374 /* Set the device state */
375 mem_device->state = MEMORY_POWER_ON_STATE;
376
377 printk(KERN_INFO "%s \n", acpi_device_name(device));
378
379 return_VALUE(result);
380}
381
4be44fcd 382static int acpi_memory_device_remove(struct acpi_device *device, int type)
1da177e4
LT
383{
384 struct acpi_memory_device *mem_device = NULL;
385
386 ACPI_FUNCTION_TRACE("acpi_memory_device_remove");
387
388 if (!device || !acpi_driver_data(device))
389 return_VALUE(-EINVAL);
390
4be44fcd 391 mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
1da177e4
LT
392 kfree(mem_device);
393
394 return_VALUE(0);
395}
396
397/*
398 * Helper function to check for memory device
399 */
4be44fcd 400static acpi_status is_memory_device(acpi_handle handle)
1da177e4
LT
401{
402 char *hardware_id;
403 acpi_status status;
4be44fcd 404 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4
LT
405 struct acpi_device_info *info;
406
407 ACPI_FUNCTION_TRACE("is_memory_device");
408
409 status = acpi_get_object_info(handle, &buffer);
410 if (ACPI_FAILURE(status))
411 return_ACPI_STATUS(AE_ERROR);
412
413 info = buffer.pointer;
414 if (!(info->valid & ACPI_VALID_HID)) {
415 acpi_os_free(buffer.pointer);
416 return_ACPI_STATUS(AE_ERROR);
417 }
418
419 hardware_id = info->hardware_id.value;
420 if ((hardware_id == NULL) ||
4be44fcd 421 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
1da177e4
LT
422 status = AE_ERROR;
423
424 acpi_os_free(buffer.pointer);
425 return_ACPI_STATUS(status);
426}
427
428static acpi_status
4be44fcd
LB
429acpi_memory_register_notify_handler(acpi_handle handle,
430 u32 level, void *ctxt, void **retv)
1da177e4
LT
431{
432 acpi_status status;
433
434 ACPI_FUNCTION_TRACE("acpi_memory_register_notify_handler");
435
436 status = is_memory_device(handle);
437 if (ACPI_FAILURE(status))
438 return_ACPI_STATUS(AE_OK); /* continue */
439
440 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
4be44fcd 441 acpi_memory_device_notify, NULL);
1da177e4
LT
442 if (ACPI_FAILURE(status)) {
443 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 444 "Error installing notify handler\n"));
1da177e4
LT
445 return_ACPI_STATUS(AE_OK); /* continue */
446 }
447
448 return_ACPI_STATUS(status);
449}
450
451static acpi_status
4be44fcd
LB
452acpi_memory_deregister_notify_handler(acpi_handle handle,
453 u32 level, void *ctxt, void **retv)
1da177e4
LT
454{
455 acpi_status status;
456
457 ACPI_FUNCTION_TRACE("acpi_memory_deregister_notify_handler");
458
459 status = is_memory_device(handle);
460 if (ACPI_FAILURE(status))
461 return_ACPI_STATUS(AE_OK); /* continue */
462
463 status = acpi_remove_notify_handler(handle,
4be44fcd
LB
464 ACPI_SYSTEM_NOTIFY,
465 acpi_memory_device_notify);
1da177e4
LT
466 if (ACPI_FAILURE(status)) {
467 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
4be44fcd 468 "Error removing notify handler\n"));
1da177e4
LT
469 return_ACPI_STATUS(AE_OK); /* continue */
470 }
471
472 return_ACPI_STATUS(status);
473}
474
4be44fcd 475static int __init acpi_memory_device_init(void)
1da177e4
LT
476{
477 int result;
478 acpi_status status;
479
480 ACPI_FUNCTION_TRACE("acpi_memory_device_init");
481
482 result = acpi_bus_register_driver(&acpi_memory_device_driver);
483
484 if (result < 0)
485 return_VALUE(-ENODEV);
486
487 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
4be44fcd
LB
488 ACPI_UINT32_MAX,
489 acpi_memory_register_notify_handler,
490 NULL, NULL);
1da177e4 491
4be44fcd
LB
492 if (ACPI_FAILURE(status)) {
493 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
1da177e4
LT
494 acpi_bus_unregister_driver(&acpi_memory_device_driver);
495 return_VALUE(-ENODEV);
4be44fcd 496 }
1da177e4
LT
497
498 return_VALUE(0);
499}
500
4be44fcd 501static void __exit acpi_memory_device_exit(void)
1da177e4
LT
502{
503 acpi_status status;
504
505 ACPI_FUNCTION_TRACE("acpi_memory_device_exit");
506
507 /*
508 * Adding this to un-install notification handlers for all the device
509 * handles.
510 */
511 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
4be44fcd
LB
512 ACPI_UINT32_MAX,
513 acpi_memory_deregister_notify_handler,
514 NULL, NULL);
1da177e4 515
4be44fcd
LB
516 if (ACPI_FAILURE(status))
517 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
1da177e4
LT
518
519 acpi_bus_unregister_driver(&acpi_memory_device_driver);
520
521 return_VOID;
522}
523
524module_init(acpi_memory_device_init);
525module_exit(acpi_memory_device_exit);
This page took 0.081195 seconds and 5 git commands to generate.