blob: 9b056f06691fd08e20619c4d2226f161251a5d0f [file] [log] [blame]
Liu Jinsongef92e7c2013-01-24 20:19:47 +08001/*
2 * Copyright (C) 2012 Intel Corporation
3 * Author: Liu Jinsong <jinsong.liu@intel.com>
4 * Author: Jiang Yunhong <yunhong.jiang@intel.com>
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
Joe Perches283c0972013-06-28 03:21:41 -070018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Liu Jinsongef92e7c2013-01-24 20:19:47 +080020#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>
29
30#define PREFIX "ACPI:xen_memory_hotplug:"
31
32struct 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;
40};
41
42struct acpi_memory_device {
43 struct acpi_device *device;
44 struct list_head res_list;
45};
46
47static bool acpi_hotmem_initialized __read_mostly;
48
49static int xen_hotadd_memory(int pxm, struct acpi_memory_info *info)
50{
51 int rc;
52 struct xen_platform_op op;
53
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;
58
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);
66
67 return rc;
68}
69
Liu Jinsongef92e7c2013-01-24 20:19:47 +080070static int xen_acpi_memory_enable_device(struct acpi_memory_device *mem_device)
71{
72 int pxm, result;
73 int num_enabled = 0;
74 struct acpi_memory_info *info;
75
76 if (!mem_device)
77 return -EINVAL;
78
79 pxm = xen_acpi_get_pxm(mem_device->device->handle);
80 if (pxm < 0)
81 return pxm;
82
83 list_for_each_entry(info, &mem_device->res_list, list) {
84 if (info->enabled) { /* just sanity check...*/
85 num_enabled++;
86 continue;
87 }
88
89 if (!info->length)
90 continue;
91
92 result = xen_hotadd_memory(pxm, info);
93 if (result)
94 continue;
95 info->enabled = 1;
96 num_enabled++;
97 }
98
99 if (!num_enabled)
100 return -ENODEV;
101
102 return 0;
103}
104
105static acpi_status
106acpi_memory_get_resource(struct acpi_resource *resource, void *context)
107{
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;
112
113 status = acpi_resource_to_address64(resource, &address64);
114 if (ACPI_FAILURE(status) ||
115 (address64.resource_type != ACPI_MEMORY_RANGE))
116 return AE_OK;
117
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;
124 }
125 }
126
127 new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
128 if (!new)
129 return AE_ERROR;
130
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);
137
138 return AE_OK;
139}
140
141static int
142acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
143{
144 acpi_status status;
145 struct acpi_memory_info *info, *n;
146
147 if (!list_empty(&mem_device->res_list))
148 return 0;
149
150 status = acpi_walk_resources(mem_device->device->handle,
151 METHOD_NAME__CRS, acpi_memory_get_resource, mem_device);
152
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;
158 }
159
160 return 0;
161}
162
Liu Jinsong484400f2013-02-16 16:59:03 +0800163static int acpi_memory_get_device(acpi_handle handle,
164 struct acpi_memory_device **mem_device)
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800165{
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800166 struct acpi_device *device = NULL;
Liu Jinsong484400f2013-02-16 16:59:03 +0800167 int result = 0;
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800168
Liu Jinsong484400f2013-02-16 16:59:03 +0800169 acpi_scan_lock_acquire();
170
171 acpi_bus_get_device(handle, &device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +0100172 if (acpi_device_enumerated(device))
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800173 goto end;
174
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800175 /*
176 * Now add the notified device. This creates the acpi_device
177 * and invokes .add function
178 */
Linus Torvalds77be36d2013-02-24 16:06:13 -0800179 result = acpi_bus_scan(handle);
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800180 if (result) {
Liu Jinsong484400f2013-02-16 16:59:03 +0800181 pr_warn(PREFIX "ACPI namespace scan failed\n");
182 result = -EINVAL;
183 goto out;
184 }
Rafael J. Wysocki202317a2013-11-22 21:54:37 +0100185 device = NULL;
186 acpi_bus_get_device(handle, &device);
187 if (!acpi_device_enumerated(device)) {
Liu Jinsong484400f2013-02-16 16:59:03 +0800188 pr_warn(PREFIX "Missing device object\n");
189 result = -EINVAL;
190 goto out;
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800191 }
192
193end:
194 *mem_device = acpi_driver_data(device);
195 if (!(*mem_device)) {
Liu Jinsong484400f2013-02-16 16:59:03 +0800196 pr_err(PREFIX "driver data not found\n");
197 result = -ENODEV;
198 goto out;
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800199 }
200
Liu Jinsong484400f2013-02-16 16:59:03 +0800201out:
202 acpi_scan_lock_release();
203 return result;
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800204}
205
206static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
207{
208 unsigned long long current_status;
209
210 /* Get device present/absent information from the _STA */
211 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
212 "_STA", NULL, &current_status)))
213 return -ENODEV;
214 /*
215 * Check for device status. Device should be
216 * present/enabled/functioning.
217 */
218 if (!((current_status & ACPI_STA_DEVICE_PRESENT)
219 && (current_status & ACPI_STA_DEVICE_ENABLED)
220 && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
221 return -ENODEV;
222
223 return 0;
224}
225
226static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
227{
228 pr_debug(PREFIX "Xen does not support memory hotremove\n");
229
230 return -ENOSYS;
231}
232
233static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
234{
235 struct acpi_memory_device *mem_device;
236 struct acpi_device *device;
237 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
238
239 switch (event) {
240 case ACPI_NOTIFY_BUS_CHECK:
241 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
242 "\nReceived BUS CHECK notification for device\n"));
243 /* Fall Through */
244 case ACPI_NOTIFY_DEVICE_CHECK:
245 if (event == ACPI_NOTIFY_DEVICE_CHECK)
246 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
247 "\nReceived DEVICE CHECK notification for device\n"));
248
249 if (acpi_memory_get_device(handle, &mem_device)) {
250 pr_err(PREFIX "Cannot find driver data\n");
251 break;
252 }
253
254 ost_code = ACPI_OST_SC_SUCCESS;
255 break;
256
257 case ACPI_NOTIFY_EJECT_REQUEST:
258 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
259 "\nReceived EJECT REQUEST notification for device\n"));
260
Liu Jinsong484400f2013-02-16 16:59:03 +0800261 acpi_scan_lock_acquire();
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800262 if (acpi_bus_get_device(handle, &device)) {
Liu Jinsong484400f2013-02-16 16:59:03 +0800263 acpi_scan_lock_release();
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800264 pr_err(PREFIX "Device doesn't exist\n");
265 break;
266 }
267 mem_device = acpi_driver_data(device);
268 if (!mem_device) {
Liu Jinsong484400f2013-02-16 16:59:03 +0800269 acpi_scan_lock_release();
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800270 pr_err(PREFIX "Driver Data is NULL\n");
271 break;
272 }
273
274 /*
275 * TBD: implement acpi_memory_disable_device and invoke
276 * acpi_bus_remove if Xen support hotremove in the future
277 */
278 acpi_memory_disable_device(mem_device);
Liu Jinsong484400f2013-02-16 16:59:03 +0800279 acpi_scan_lock_release();
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800280 break;
281
282 default:
283 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
284 "Unsupported event [0x%x]\n", event));
285 /* non-hotplug event; possibly handled by other handler */
286 return;
287 }
288
289 (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
290 return;
291}
292
293static int xen_acpi_memory_device_add(struct acpi_device *device)
294{
295 int result;
296 struct acpi_memory_device *mem_device = NULL;
297
298
299 if (!device)
300 return -EINVAL;
301
302 mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
303 if (!mem_device)
304 return -ENOMEM;
305
306 INIT_LIST_HEAD(&mem_device->res_list);
307 mem_device->device = device;
308 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
309 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
310 device->driver_data = mem_device;
311
312 /* Get the range from the _CRS */
313 result = acpi_memory_get_device_resources(mem_device);
314 if (result) {
315 kfree(mem_device);
316 return result;
317 }
318
319 /*
320 * For booting existed memory devices, early boot code has recognized
321 * memory area by EFI/E820. If DSDT shows these memory devices on boot,
322 * hotplug is not necessary for them.
323 * For hot-added memory devices during runtime, it need hypercall to
324 * Xen hypervisor to add memory.
325 */
326 if (!acpi_hotmem_initialized)
327 return 0;
328
329 if (!acpi_memory_check_device(mem_device))
330 result = xen_acpi_memory_enable_device(mem_device);
331
332 return result;
333}
334
Linus Torvalds77be36d2013-02-24 16:06:13 -0800335static int xen_acpi_memory_device_remove(struct acpi_device *device)
Liu Jinsongef92e7c2013-01-24 20:19:47 +0800336{
337 struct acpi_memory_device *mem_device = NULL;
338
339 if (!device || !acpi_driver_data(device))
340 return -EINVAL;
341
342 mem_device = acpi_driver_data(device);
343 kfree(mem_device);
344
345 return 0;
346}
347
348/*
349 * Helper function to check for memory device
350 */
351static acpi_status is_memory_device(acpi_handle handle)
352{
353 char *hardware_id;
354 acpi_status status;
355 struct acpi_device_info *info;
356
357 status = acpi_get_object_info(handle, &info);
358 if (ACPI_FAILURE(status))
359 return status;
360
361 if (!(info->valid & ACPI_VALID_HID)) {
362 kfree(info);
363 return AE_ERROR;
364 }
365
366 hardware_id = info->hardware_id.string;
367 if ((hardware_id == NULL) ||
368 (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
369 status = AE_ERROR;
370
371 kfree(info);
372 return status;
373}
374
375static acpi_status
376acpi_memory_register_notify_handler(acpi_handle handle,
377 u32 level, void *ctxt, void **retv)
378{
379 acpi_status status;
380
381 status = is_memory_device(handle);
382 if (ACPI_FAILURE(status))
383 return AE_OK; /* continue */
384
385 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
386 acpi_memory_device_notify, NULL);
387 /* continue */
388 return AE_OK;
389}
390
391static acpi_status
392acpi_memory_deregister_notify_handler(acpi_handle handle,
393 u32 level, void *ctxt, void **retv)
394{
395 acpi_status status;
396
397 status = is_memory_device(handle);
398 if (ACPI_FAILURE(status))
399 return AE_OK; /* continue */
400
401 status = acpi_remove_notify_handler(handle,
402 ACPI_SYSTEM_NOTIFY,
403 acpi_memory_device_notify);
404
405 return AE_OK; /* continue */
406}
407
408static const struct acpi_device_id memory_device_ids[] = {
409 {ACPI_MEMORY_DEVICE_HID, 0},
410 {"", 0},
411};
412MODULE_DEVICE_TABLE(acpi, memory_device_ids);
413
414static struct acpi_driver xen_acpi_memory_device_driver = {
415 .name = "acpi_memhotplug",
416 .class = ACPI_MEMORY_DEVICE_CLASS,
417 .ids = memory_device_ids,
418 .ops = {
419 .add = xen_acpi_memory_device_add,
420 .remove = xen_acpi_memory_device_remove,
421 },
422};
423
424static int __init xen_acpi_memory_device_init(void)
425{
426 int result;
427 acpi_status status;
428
429 if (!xen_initial_domain())
430 return -ENODEV;
431
432 /* unregister the stub which only used to reserve driver space */
433 xen_stub_memory_device_exit();
434
435 result = acpi_bus_register_driver(&xen_acpi_memory_device_driver);
436 if (result < 0) {
437 xen_stub_memory_device_init();
438 return -ENODEV;
439 }
440
441 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
442 ACPI_UINT32_MAX,
443 acpi_memory_register_notify_handler,
444 NULL, NULL, NULL);
445
446 if (ACPI_FAILURE(status)) {
447 pr_warn(PREFIX "walk_namespace failed\n");
448 acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
449 xen_stub_memory_device_init();
450 return -ENODEV;
451 }
452
453 acpi_hotmem_initialized = true;
454 return 0;
455}
456
457static void __exit xen_acpi_memory_device_exit(void)
458{
459 acpi_status status;
460
461 if (!xen_initial_domain())
462 return;
463
464 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
465 ACPI_UINT32_MAX,
466 acpi_memory_deregister_notify_handler,
467 NULL, NULL, NULL);
468 if (ACPI_FAILURE(status))
469 pr_warn(PREFIX "walk_namespace failed\n");
470
471 acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
472
473 /*
474 * stub reserve space again to prevent any chance of native
475 * driver loading.
476 */
477 xen_stub_memory_device_init();
478 return;
479}
480
481module_init(xen_acpi_memory_device_init);
482module_exit(xen_acpi_memory_device_exit);
483ACPI_MODULE_NAME("xen-acpi-memhotplug");
484MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
485MODULE_DESCRIPTION("Xen Hotplug Mem Driver");
486MODULE_LICENSE("GPL");