Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 1 | /* |
| 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 Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/cpu.h> |
| 25 | #include <linux/acpi.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <acpi/acpi_bus.h> |
| 28 | #include <acpi/acpi_drivers.h> |
| 29 | #include <acpi/processor.h> |
| 30 | |
| 31 | #include <xen/acpi.h> |
| 32 | #include <xen/interface/platform.h> |
| 33 | #include <asm/xen/hypercall.h> |
| 34 | |
| 35 | #define PREFIX "ACPI:xen_cpu_hotplug:" |
| 36 | |
| 37 | #define INSTALL_NOTIFY_HANDLER 0 |
| 38 | #define UNINSTALL_NOTIFY_HANDLER 1 |
| 39 | |
| 40 | static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr); |
| 41 | |
| 42 | /* -------------------------------------------------------------------------- |
| 43 | Driver Interface |
| 44 | -------------------------------------------------------------------------- */ |
| 45 | |
| 46 | static int xen_acpi_processor_enable(struct acpi_device *device) |
| 47 | { |
| 48 | acpi_status status = 0; |
| 49 | unsigned long long value; |
| 50 | union acpi_object object = { 0 }; |
| 51 | struct acpi_buffer buffer = { sizeof(union acpi_object), &object }; |
| 52 | struct acpi_processor *pr; |
| 53 | |
| 54 | pr = acpi_driver_data(device); |
| 55 | if (!pr) { |
| 56 | pr_err(PREFIX "Cannot find driver data\n"); |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
| 60 | if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) { |
| 61 | /* Declared with "Processor" statement; match ProcessorID */ |
| 62 | status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer); |
| 63 | if (ACPI_FAILURE(status)) { |
| 64 | pr_err(PREFIX "Evaluating processor object\n"); |
| 65 | return -ENODEV; |
| 66 | } |
| 67 | |
| 68 | pr->acpi_id = object.processor.proc_id; |
| 69 | } else { |
| 70 | /* Declared with "Device" statement; match _UID */ |
| 71 | status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID, |
| 72 | NULL, &value); |
| 73 | if (ACPI_FAILURE(status)) { |
| 74 | pr_err(PREFIX "Evaluating processor _UID\n"); |
| 75 | return -ENODEV; |
| 76 | } |
| 77 | |
| 78 | pr->acpi_id = value; |
| 79 | } |
| 80 | |
| 81 | pr->id = xen_pcpu_id(pr->acpi_id); |
| 82 | |
| 83 | if ((int)pr->id < 0) |
| 84 | /* This cpu is not presented at hypervisor, try to hotadd it */ |
| 85 | if (ACPI_FAILURE(xen_acpi_cpu_hotadd(pr))) { |
| 86 | pr_err(PREFIX "Hotadd CPU (acpi_id = %d) failed.\n", |
| 87 | pr->acpi_id); |
| 88 | return -ENODEV; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Paul Gortmaker | a83048e | 2013-06-19 15:22:41 -0400 | [diff] [blame] | 94 | static int xen_acpi_processor_add(struct acpi_device *device) |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 95 | { |
| 96 | int ret; |
| 97 | struct acpi_processor *pr; |
| 98 | |
| 99 | if (!device) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL); |
| 103 | if (!pr) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | pr->handle = device->handle; |
| 107 | strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME); |
| 108 | strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS); |
| 109 | device->driver_data = pr; |
| 110 | |
| 111 | ret = xen_acpi_processor_enable(device); |
| 112 | if (ret) |
| 113 | pr_err(PREFIX "Error when enabling Xen processor\n"); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Linus Torvalds | 77be36d | 2013-02-24 16:06:13 -0800 | [diff] [blame] | 118 | static int xen_acpi_processor_remove(struct acpi_device *device) |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 119 | { |
| 120 | struct acpi_processor *pr; |
| 121 | |
| 122 | if (!device) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | pr = acpi_driver_data(device); |
| 126 | if (!pr) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | kfree(pr); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /*-------------------------------------------------------------- |
| 134 | Acpi processor hotplug support |
| 135 | --------------------------------------------------------------*/ |
| 136 | |
| 137 | static int is_processor_present(acpi_handle handle) |
| 138 | { |
| 139 | acpi_status status; |
| 140 | unsigned long long sta = 0; |
| 141 | |
| 142 | |
| 143 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
| 144 | |
| 145 | if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT)) |
| 146 | return 1; |
| 147 | |
| 148 | /* |
| 149 | * _STA is mandatory for a processor that supports hot plug |
| 150 | */ |
| 151 | if (status == AE_NOT_FOUND) |
| 152 | pr_info(PREFIX "Processor does not support hot plug\n"); |
| 153 | else |
| 154 | pr_info(PREFIX "Processor Device is not present"); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int xen_apic_id(acpi_handle handle) |
| 159 | { |
| 160 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 161 | union acpi_object *obj; |
| 162 | struct acpi_madt_local_apic *lapic; |
| 163 | int apic_id; |
| 164 | |
| 165 | if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer))) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | if (!buffer.length || !buffer.pointer) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | obj = buffer.pointer; |
| 172 | if (obj->type != ACPI_TYPE_BUFFER || |
| 173 | obj->buffer.length < sizeof(*lapic)) { |
| 174 | kfree(buffer.pointer); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | |
| 178 | lapic = (struct acpi_madt_local_apic *)obj->buffer.pointer; |
| 179 | |
| 180 | if (lapic->header.type != ACPI_MADT_TYPE_LOCAL_APIC || |
| 181 | !(lapic->lapic_flags & ACPI_MADT_ENABLED)) { |
| 182 | kfree(buffer.pointer); |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | apic_id = (uint32_t)lapic->id; |
| 187 | kfree(buffer.pointer); |
| 188 | buffer.length = ACPI_ALLOCATE_BUFFER; |
| 189 | buffer.pointer = NULL; |
| 190 | |
| 191 | return apic_id; |
| 192 | } |
| 193 | |
| 194 | static int xen_hotadd_cpu(struct acpi_processor *pr) |
| 195 | { |
| 196 | int cpu_id, apic_id, pxm; |
| 197 | struct xen_platform_op op; |
| 198 | |
| 199 | apic_id = xen_apic_id(pr->handle); |
| 200 | if (apic_id < 0) { |
| 201 | pr_err(PREFIX "Failed to get apic_id for acpi_id %d\n", |
| 202 | pr->acpi_id); |
| 203 | return -ENODEV; |
| 204 | } |
| 205 | |
| 206 | pxm = xen_acpi_get_pxm(pr->handle); |
| 207 | if (pxm < 0) { |
| 208 | pr_err(PREFIX "Failed to get _PXM for acpi_id %d\n", |
| 209 | pr->acpi_id); |
| 210 | return pxm; |
| 211 | } |
| 212 | |
| 213 | op.cmd = XENPF_cpu_hotadd; |
| 214 | op.u.cpu_add.apic_id = apic_id; |
| 215 | op.u.cpu_add.acpi_id = pr->acpi_id; |
| 216 | op.u.cpu_add.pxm = pxm; |
| 217 | |
| 218 | cpu_id = HYPERVISOR_dom0_op(&op); |
| 219 | if (cpu_id < 0) |
| 220 | pr_err(PREFIX "Failed to hotadd CPU for acpi_id %d\n", |
| 221 | pr->acpi_id); |
| 222 | |
| 223 | return cpu_id; |
| 224 | } |
| 225 | |
| 226 | static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr) |
| 227 | { |
| 228 | if (!is_processor_present(pr->handle)) |
| 229 | return AE_ERROR; |
| 230 | |
| 231 | pr->id = xen_hotadd_cpu(pr); |
| 232 | if ((int)pr->id < 0) |
| 233 | return AE_ERROR; |
| 234 | |
| 235 | /* |
| 236 | * Sync with Xen hypervisor, providing new /sys/.../xen_cpuX |
| 237 | * interface after cpu hotadded. |
| 238 | */ |
| 239 | xen_pcpu_hotplug_sync(); |
| 240 | |
| 241 | return AE_OK; |
| 242 | } |
| 243 | |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 244 | static int acpi_processor_device_remove(struct acpi_device *device) |
| 245 | { |
| 246 | pr_debug(PREFIX "Xen does not support CPU hotremove\n"); |
| 247 | |
| 248 | return -ENOSYS; |
| 249 | } |
| 250 | |
| 251 | static void acpi_processor_hotplug_notify(acpi_handle handle, |
| 252 | u32 event, void *data) |
| 253 | { |
| 254 | struct acpi_processor *pr; |
| 255 | struct acpi_device *device = NULL; |
| 256 | u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */ |
| 257 | int result; |
| 258 | |
Liu Jinsong | 1b37d6e | 2013-02-17 11:47:24 +0800 | [diff] [blame] | 259 | acpi_scan_lock_acquire(); |
| 260 | |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 261 | switch (event) { |
| 262 | case ACPI_NOTIFY_BUS_CHECK: |
| 263 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 264 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 265 | "Processor driver received %s event\n", |
| 266 | (event == ACPI_NOTIFY_BUS_CHECK) ? |
| 267 | "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK")); |
| 268 | |
| 269 | if (!is_processor_present(handle)) |
| 270 | break; |
| 271 | |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame^] | 272 | acpi_bus_get_device(handle, &device); |
| 273 | if (acpi_device_enumerated(device)) |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 274 | break; |
| 275 | |
Liu Jinsong | 1b37d6e | 2013-02-17 11:47:24 +0800 | [diff] [blame] | 276 | result = acpi_bus_scan(handle); |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 277 | if (result) { |
| 278 | pr_err(PREFIX "Unable to add the device\n"); |
| 279 | break; |
| 280 | } |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame^] | 281 | device = NULL; |
| 282 | acpi_bus_get_device(handle, &device); |
| 283 | if (!acpi_device_enumerated(device)) { |
Liu Jinsong | 1b37d6e | 2013-02-17 11:47:24 +0800 | [diff] [blame] | 284 | pr_err(PREFIX "Missing device object\n"); |
| 285 | break; |
| 286 | } |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 287 | ost_code = ACPI_OST_SC_SUCCESS; |
| 288 | break; |
| 289 | |
| 290 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 291 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 292 | "received ACPI_NOTIFY_EJECT_REQUEST\n")); |
| 293 | |
| 294 | if (acpi_bus_get_device(handle, &device)) { |
| 295 | pr_err(PREFIX "Device don't exist, dropping EJECT\n"); |
| 296 | break; |
| 297 | } |
| 298 | pr = acpi_driver_data(device); |
| 299 | if (!pr) { |
| 300 | pr_err(PREFIX "Driver data is NULL, dropping EJECT\n"); |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * TBD: implement acpi_processor_device_remove if Xen support |
| 306 | * CPU hotremove in the future. |
| 307 | */ |
| 308 | acpi_processor_device_remove(device); |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 313 | "Unsupported event [0x%x]\n", event)); |
| 314 | |
| 315 | /* non-hotplug event; possibly handled by other handler */ |
Liu Jinsong | 1b37d6e | 2013-02-17 11:47:24 +0800 | [diff] [blame] | 316 | goto out; |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL); |
Liu Jinsong | 1b37d6e | 2013-02-17 11:47:24 +0800 | [diff] [blame] | 320 | |
| 321 | out: |
| 322 | acpi_scan_lock_release(); |
Liu Jinsong | 39adc48 | 2013-01-25 15:43:34 +0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static acpi_status is_processor_device(acpi_handle handle) |
| 326 | { |
| 327 | struct acpi_device_info *info; |
| 328 | char *hid; |
| 329 | acpi_status status; |
| 330 | |
| 331 | status = acpi_get_object_info(handle, &info); |
| 332 | if (ACPI_FAILURE(status)) |
| 333 | return status; |
| 334 | |
| 335 | if (info->type == ACPI_TYPE_PROCESSOR) { |
| 336 | kfree(info); |
| 337 | return AE_OK; /* found a processor object */ |
| 338 | } |
| 339 | |
| 340 | if (!(info->valid & ACPI_VALID_HID)) { |
| 341 | kfree(info); |
| 342 | return AE_ERROR; |
| 343 | } |
| 344 | |
| 345 | hid = info->hardware_id.string; |
| 346 | if ((hid == NULL) || strcmp(hid, ACPI_PROCESSOR_DEVICE_HID)) { |
| 347 | kfree(info); |
| 348 | return AE_ERROR; |
| 349 | } |
| 350 | |
| 351 | kfree(info); |
| 352 | return AE_OK; /* found a processor device object */ |
| 353 | } |
| 354 | |
| 355 | static acpi_status |
| 356 | processor_walk_namespace_cb(acpi_handle handle, |
| 357 | u32 lvl, void *context, void **rv) |
| 358 | { |
| 359 | acpi_status status; |
| 360 | int *action = context; |
| 361 | |
| 362 | status = is_processor_device(handle); |
| 363 | if (ACPI_FAILURE(status)) |
| 364 | return AE_OK; /* not a processor; continue to walk */ |
| 365 | |
| 366 | switch (*action) { |
| 367 | case INSTALL_NOTIFY_HANDLER: |
| 368 | acpi_install_notify_handler(handle, |
| 369 | ACPI_SYSTEM_NOTIFY, |
| 370 | acpi_processor_hotplug_notify, |
| 371 | NULL); |
| 372 | break; |
| 373 | case UNINSTALL_NOTIFY_HANDLER: |
| 374 | acpi_remove_notify_handler(handle, |
| 375 | ACPI_SYSTEM_NOTIFY, |
| 376 | acpi_processor_hotplug_notify); |
| 377 | break; |
| 378 | default: |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | /* found a processor; skip walking underneath */ |
| 383 | return AE_CTRL_DEPTH; |
| 384 | } |
| 385 | |
| 386 | static |
| 387 | void acpi_processor_install_hotplug_notify(void) |
| 388 | { |
| 389 | int action = INSTALL_NOTIFY_HANDLER; |
| 390 | acpi_walk_namespace(ACPI_TYPE_ANY, |
| 391 | ACPI_ROOT_OBJECT, |
| 392 | ACPI_UINT32_MAX, |
| 393 | processor_walk_namespace_cb, NULL, &action, NULL); |
| 394 | } |
| 395 | |
| 396 | static |
| 397 | void acpi_processor_uninstall_hotplug_notify(void) |
| 398 | { |
| 399 | int action = UNINSTALL_NOTIFY_HANDLER; |
| 400 | acpi_walk_namespace(ACPI_TYPE_ANY, |
| 401 | ACPI_ROOT_OBJECT, |
| 402 | ACPI_UINT32_MAX, |
| 403 | processor_walk_namespace_cb, NULL, &action, NULL); |
| 404 | } |
| 405 | |
| 406 | static const struct acpi_device_id processor_device_ids[] = { |
| 407 | {ACPI_PROCESSOR_OBJECT_HID, 0}, |
| 408 | {ACPI_PROCESSOR_DEVICE_HID, 0}, |
| 409 | {"", 0}, |
| 410 | }; |
| 411 | MODULE_DEVICE_TABLE(acpi, processor_device_ids); |
| 412 | |
| 413 | static struct acpi_driver xen_acpi_processor_driver = { |
| 414 | .name = "processor", |
| 415 | .class = ACPI_PROCESSOR_CLASS, |
| 416 | .ids = processor_device_ids, |
| 417 | .ops = { |
| 418 | .add = xen_acpi_processor_add, |
| 419 | .remove = xen_acpi_processor_remove, |
| 420 | }, |
| 421 | }; |
| 422 | |
| 423 | static int __init xen_acpi_processor_init(void) |
| 424 | { |
| 425 | int result = 0; |
| 426 | |
| 427 | if (!xen_initial_domain()) |
| 428 | return -ENODEV; |
| 429 | |
| 430 | /* unregister the stub which only used to reserve driver space */ |
| 431 | xen_stub_processor_exit(); |
| 432 | |
| 433 | result = acpi_bus_register_driver(&xen_acpi_processor_driver); |
| 434 | if (result < 0) { |
| 435 | xen_stub_processor_init(); |
| 436 | return result; |
| 437 | } |
| 438 | |
| 439 | acpi_processor_install_hotplug_notify(); |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static void __exit xen_acpi_processor_exit(void) |
| 444 | { |
| 445 | if (!xen_initial_domain()) |
| 446 | return; |
| 447 | |
| 448 | acpi_processor_uninstall_hotplug_notify(); |
| 449 | |
| 450 | acpi_bus_unregister_driver(&xen_acpi_processor_driver); |
| 451 | |
| 452 | /* |
| 453 | * stub reserve space again to prevent any chance of native |
| 454 | * driver loading. |
| 455 | */ |
| 456 | xen_stub_processor_init(); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | module_init(xen_acpi_processor_init); |
| 461 | module_exit(xen_acpi_processor_exit); |
| 462 | ACPI_MODULE_NAME("xen-acpi-cpuhotplug"); |
| 463 | MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>"); |
| 464 | MODULE_DESCRIPTION("Xen Hotplug CPU Driver"); |
| 465 | MODULE_LICENSE("GPL"); |