Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/josef/btrfs...
[linux-2.6.git] / drivers / xen / xen-acpi-memhotplug.c
blob9083f1e474f801c07c1531551fa971b5e0e21b38
1 /*
2 * Copyright (C) 2012 Intel Corporation
3 * Author: Liu Jinsong <jinsong.liu@intel.com>
4 * Author: Jiang Yunhong <yunhong.jiang@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
15 * details.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/acpi.h>
25 #include <acpi/acpi_drivers.h>
26 #include <xen/acpi.h>
27 #include <xen/interface/platform.h>
28 #include <asm/xen/hypercall.h>
30 #define PREFIX "ACPI:xen_memory_hotplug:"
32 struct acpi_memory_info {
33 struct list_head list;
34 u64 start_addr; /* Memory Range start physical addr */
35 u64 length; /* Memory Range length */
36 unsigned short caching; /* memory cache attribute */
37 unsigned short write_protect; /* memory read/write attribute */
38 /* copied from buffer getting from _CRS */
39 unsigned int enabled:1;
42 struct acpi_memory_device {
43 struct acpi_device *device;
44 struct list_head res_list;
47 static bool acpi_hotmem_initialized __read_mostly;
49 static int xen_hotadd_memory(int pxm, struct acpi_memory_info *info)
51 int rc;
52 struct xen_platform_op op;
54 op.cmd = XENPF_mem_hotadd;
55 op.u.mem_add.spfn = info->start_addr >> PAGE_SHIFT;
56 op.u.mem_add.epfn = (info->start_addr + info->length) >> PAGE_SHIFT;
57 op.u.mem_add.pxm = pxm;
59 rc = HYPERVISOR_dom0_op(&op);
60 if (rc)
61 pr_err(PREFIX "Xen Hotplug Memory Add failed on "
62 "0x%lx -> 0x%lx, _PXM: %d, error: %d\n",
63 (unsigned long)info->start_addr,
64 (unsigned long)(info->start_addr + info->length),
65 pxm, rc);
67 return rc;
70 static int xen_acpi_memory_enable_device(struct acpi_memory_device *mem_device)
72 int pxm, result;
73 int num_enabled = 0;
74 struct acpi_memory_info *info;
76 if (!mem_device)
77 return -EINVAL;
79 pxm = xen_acpi_get_pxm(mem_device->device->handle);
80 if (pxm < 0)
81 return pxm;
83 list_for_each_entry(info, &mem_device->res_list, list) {
84 if (info->enabled) { /* just sanity check...*/
85 num_enabled++;
86 continue;
89 if (!info->length)
90 continue;
92 result = xen_hotadd_memory(pxm, info);
93 if (result)
94 continue;
95 info->enabled = 1;
96 num_enabled++;
99 if (!num_enabled)
100 return -ENODEV;
102 return 0;
105 static acpi_status
106 acpi_memory_get_resource(struct acpi_resource *resource, void *context)
108 struct acpi_memory_device *mem_device = context;
109 struct acpi_resource_address64 address64;
110 struct acpi_memory_info *info, *new;
111 acpi_status status;
113 status = acpi_resource_to_address64(resource, &address64);
114 if (ACPI_FAILURE(status) ||
115 (address64.resource_type != ACPI_MEMORY_RANGE))
116 return AE_OK;
118 list_for_each_entry(info, &mem_device->res_list, list) {
119 if ((info->caching == address64.info.mem.caching) &&
120 (info->write_protect == address64.info.mem.write_protect) &&
121 (info->start_addr + info->length == address64.minimum)) {
122 info->length += address64.address_length;
123 return AE_OK;
127 new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
128 if (!new)
129 return AE_ERROR;
131 INIT_LIST_HEAD(&new->list);
132 new->caching = address64.info.mem.caching;
133 new->write_protect = address64.info.mem.write_protect;
134 new->start_addr = address64.minimum;
135 new->length = address64.address_length;
136 list_add_tail(&new->list, &mem_device->res_list);
138 return AE_OK;
141 static int
142 acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
144 acpi_status status;
145 struct acpi_memory_info *info, *n;
147 if (!list_empty(&mem_device->res_list))
148 return 0;
150 status = acpi_walk_resources(mem_device->device->handle,
151 METHOD_NAME__CRS, acpi_memory_get_resource, mem_device);
153 if (ACPI_FAILURE(status)) {
154 list_for_each_entry_safe(info, n, &mem_device->res_list, list)
155 kfree(info);
156 INIT_LIST_HEAD(&mem_device->res_list);
157 return -EINVAL;
160 return 0;
163 static int acpi_memory_get_device(acpi_handle handle,
164 struct acpi_memory_device **mem_device)
166 struct acpi_device *device = NULL;
167 int result = 0;
169 acpi_scan_lock_acquire();
171 acpi_bus_get_device(handle, &device);
172 if (device)
173 goto end;
176 * Now add the notified device. This creates the acpi_device
177 * and invokes .add function
179 result = acpi_bus_scan(handle);
180 if (result) {
181 pr_warn(PREFIX "ACPI namespace scan failed\n");
182 result = -EINVAL;
183 goto out;
185 result = acpi_bus_get_device(handle, &device);
186 if (result) {
187 pr_warn(PREFIX "Missing device object\n");
188 result = -EINVAL;
189 goto out;
192 end:
193 *mem_device = acpi_driver_data(device);
194 if (!(*mem_device)) {
195 pr_err(PREFIX "driver data not found\n");
196 result = -ENODEV;
197 goto out;
200 out:
201 acpi_scan_lock_release();
202 return result;
205 static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
207 unsigned long long current_status;
209 /* Get device present/absent information from the _STA */
210 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
211 "_STA", NULL, &current_status)))
212 return -ENODEV;
214 * Check for device status. Device should be
215 * present/enabled/functioning.
217 if (!((current_status & ACPI_STA_DEVICE_PRESENT)
218 && (current_status & ACPI_STA_DEVICE_ENABLED)
219 && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
220 return -ENODEV;
222 return 0;
225 static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
227 pr_debug(PREFIX "Xen does not support memory hotremove\n");
229 return -ENOSYS;
232 static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
234 struct acpi_memory_device *mem_device;
235 struct acpi_device *device;
236 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
238 switch (event) {
239 case ACPI_NOTIFY_BUS_CHECK:
240 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
241 "\nReceived BUS CHECK notification for device\n"));
242 /* Fall Through */
243 case ACPI_NOTIFY_DEVICE_CHECK:
244 if (event == ACPI_NOTIFY_DEVICE_CHECK)
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
246 "\nReceived DEVICE CHECK notification for device\n"));
248 if (acpi_memory_get_device(handle, &mem_device)) {
249 pr_err(PREFIX "Cannot find driver data\n");
250 break;
253 ost_code = ACPI_OST_SC_SUCCESS;
254 break;
256 case ACPI_NOTIFY_EJECT_REQUEST:
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
258 "\nReceived EJECT REQUEST notification for device\n"));
260 acpi_scan_lock_acquire();
261 if (acpi_bus_get_device(handle, &device)) {
262 acpi_scan_lock_release();
263 pr_err(PREFIX "Device doesn't exist\n");
264 break;
266 mem_device = acpi_driver_data(device);
267 if (!mem_device) {
268 acpi_scan_lock_release();
269 pr_err(PREFIX "Driver Data is NULL\n");
270 break;
274 * TBD: implement acpi_memory_disable_device and invoke
275 * acpi_bus_remove if Xen support hotremove in the future
277 acpi_memory_disable_device(mem_device);
278 acpi_scan_lock_release();
279 break;
281 default:
282 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
283 "Unsupported event [0x%x]\n", event));
284 /* non-hotplug event; possibly handled by other handler */
285 return;
288 (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
289 return;
292 static int xen_acpi_memory_device_add(struct acpi_device *device)
294 int result;
295 struct acpi_memory_device *mem_device = NULL;
298 if (!device)
299 return -EINVAL;
301 mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
302 if (!mem_device)
303 return -ENOMEM;
305 INIT_LIST_HEAD(&mem_device->res_list);
306 mem_device->device = device;
307 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
308 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
309 device->driver_data = mem_device;
311 /* Get the range from the _CRS */
312 result = acpi_memory_get_device_resources(mem_device);
313 if (result) {
314 kfree(mem_device);
315 return result;
319 * For booting existed memory devices, early boot code has recognized
320 * memory area by EFI/E820. If DSDT shows these memory devices on boot,
321 * hotplug is not necessary for them.
322 * For hot-added memory devices during runtime, it need hypercall to
323 * Xen hypervisor to add memory.
325 if (!acpi_hotmem_initialized)
326 return 0;
328 if (!acpi_memory_check_device(mem_device))
329 result = xen_acpi_memory_enable_device(mem_device);
331 return result;
334 static int xen_acpi_memory_device_remove(struct acpi_device *device)
336 struct acpi_memory_device *mem_device = NULL;
338 if (!device || !acpi_driver_data(device))
339 return -EINVAL;
341 mem_device = acpi_driver_data(device);
342 kfree(mem_device);
344 return 0;
348 * Helper function to check for memory device
350 static acpi_status is_memory_device(acpi_handle handle)
352 char *hardware_id;
353 acpi_status status;
354 struct acpi_device_info *info;
356 status = acpi_get_object_info(handle, &info);
357 if (ACPI_FAILURE(status))
358 return status;
360 if (!(info->valid & ACPI_VALID_HID)) {
361 kfree(info);
362 return AE_ERROR;
365 hardware_id = info->hardware_id.string;
366 if ((hardware_id == NULL) ||
367 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
368 status = AE_ERROR;
370 kfree(info);
371 return status;
374 static acpi_status
375 acpi_memory_register_notify_handler(acpi_handle handle,
376 u32 level, void *ctxt, void **retv)
378 acpi_status status;
380 status = is_memory_device(handle);
381 if (ACPI_FAILURE(status))
382 return AE_OK; /* continue */
384 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
385 acpi_memory_device_notify, NULL);
386 /* continue */
387 return AE_OK;
390 static acpi_status
391 acpi_memory_deregister_notify_handler(acpi_handle handle,
392 u32 level, void *ctxt, void **retv)
394 acpi_status status;
396 status = is_memory_device(handle);
397 if (ACPI_FAILURE(status))
398 return AE_OK; /* continue */
400 status = acpi_remove_notify_handler(handle,
401 ACPI_SYSTEM_NOTIFY,
402 acpi_memory_device_notify);
404 return AE_OK; /* continue */
407 static const struct acpi_device_id memory_device_ids[] = {
408 {ACPI_MEMORY_DEVICE_HID, 0},
409 {"", 0},
411 MODULE_DEVICE_TABLE(acpi, memory_device_ids);
413 static struct acpi_driver xen_acpi_memory_device_driver = {
414 .name = "acpi_memhotplug",
415 .class = ACPI_MEMORY_DEVICE_CLASS,
416 .ids = memory_device_ids,
417 .ops = {
418 .add = xen_acpi_memory_device_add,
419 .remove = xen_acpi_memory_device_remove,
423 static int __init xen_acpi_memory_device_init(void)
425 int result;
426 acpi_status status;
428 if (!xen_initial_domain())
429 return -ENODEV;
431 /* unregister the stub which only used to reserve driver space */
432 xen_stub_memory_device_exit();
434 result = acpi_bus_register_driver(&xen_acpi_memory_device_driver);
435 if (result < 0) {
436 xen_stub_memory_device_init();
437 return -ENODEV;
440 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
441 ACPI_UINT32_MAX,
442 acpi_memory_register_notify_handler,
443 NULL, NULL, NULL);
445 if (ACPI_FAILURE(status)) {
446 pr_warn(PREFIX "walk_namespace failed\n");
447 acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
448 xen_stub_memory_device_init();
449 return -ENODEV;
452 acpi_hotmem_initialized = true;
453 return 0;
456 static void __exit xen_acpi_memory_device_exit(void)
458 acpi_status status;
460 if (!xen_initial_domain())
461 return;
463 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
464 ACPI_UINT32_MAX,
465 acpi_memory_deregister_notify_handler,
466 NULL, NULL, NULL);
467 if (ACPI_FAILURE(status))
468 pr_warn(PREFIX "walk_namespace failed\n");
470 acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
473 * stub reserve space again to prevent any chance of native
474 * driver loading.
476 xen_stub_memory_device_init();
477 return;
480 module_init(xen_acpi_memory_device_init);
481 module_exit(xen_acpi_memory_device_exit);
482 ACPI_MODULE_NAME("xen-acpi-memhotplug");
483 MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
484 MODULE_DESCRIPTION("Xen Hotplug Mem Driver");
485 MODULE_LICENSE("GPL");