xen-acpi-pad.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * xen-acpi-pad.c - Xen pad interface
  3. *
  4. * Copyright (c) 2012, Intel Corporation.
  5. * Author: Liu, Jinsong <jinsong.liu@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/acpi.h>
  20. #include <xen/xen.h>
  21. #include <xen/interface/version.h>
  22. #include <xen/xen-ops.h>
  23. #include <asm/xen/hypercall.h>
  24. #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
  25. #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
  26. #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
  27. static DEFINE_MUTEX(xen_cpu_lock);
  28. static int xen_acpi_pad_idle_cpus(unsigned int idle_nums)
  29. {
  30. struct xen_platform_op op;
  31. op.cmd = XENPF_core_parking;
  32. op.u.core_parking.type = XEN_CORE_PARKING_SET;
  33. op.u.core_parking.idle_nums = idle_nums;
  34. return HYPERVISOR_platform_op(&op);
  35. }
  36. static int xen_acpi_pad_idle_cpus_num(void)
  37. {
  38. struct xen_platform_op op;
  39. op.cmd = XENPF_core_parking;
  40. op.u.core_parking.type = XEN_CORE_PARKING_GET;
  41. return HYPERVISOR_platform_op(&op)
  42. ?: op.u.core_parking.idle_nums;
  43. }
  44. /*
  45. * Query firmware how many CPUs should be idle
  46. * return -1 on failure
  47. */
  48. static int acpi_pad_pur(acpi_handle handle)
  49. {
  50. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  51. union acpi_object *package;
  52. int num = -1;
  53. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
  54. return num;
  55. if (!buffer.length || !buffer.pointer)
  56. return num;
  57. package = buffer.pointer;
  58. if (package->type == ACPI_TYPE_PACKAGE &&
  59. package->package.count == 2 &&
  60. package->package.elements[0].integer.value == 1) /* rev 1 */
  61. num = package->package.elements[1].integer.value;
  62. kfree(buffer.pointer);
  63. return num;
  64. }
  65. static void acpi_pad_handle_notify(acpi_handle handle)
  66. {
  67. int idle_nums;
  68. struct acpi_buffer param = {
  69. .length = 4,
  70. .pointer = (void *)&idle_nums,
  71. };
  72. mutex_lock(&xen_cpu_lock);
  73. idle_nums = acpi_pad_pur(handle);
  74. if (idle_nums < 0) {
  75. mutex_unlock(&xen_cpu_lock);
  76. return;
  77. }
  78. idle_nums = xen_acpi_pad_idle_cpus(idle_nums)
  79. ?: xen_acpi_pad_idle_cpus_num();
  80. if (idle_nums >= 0)
  81. acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY,
  82. 0, &param);
  83. mutex_unlock(&xen_cpu_lock);
  84. }
  85. static void acpi_pad_notify(acpi_handle handle, u32 event,
  86. void *data)
  87. {
  88. switch (event) {
  89. case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
  90. acpi_pad_handle_notify(handle);
  91. break;
  92. default:
  93. pr_warn("Unsupported event [0x%x]\n", event);
  94. break;
  95. }
  96. }
  97. static int acpi_pad_add(struct acpi_device *device)
  98. {
  99. acpi_status status;
  100. strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
  101. strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
  102. status = acpi_install_notify_handler(device->handle,
  103. ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
  104. if (ACPI_FAILURE(status))
  105. return -ENODEV;
  106. return 0;
  107. }
  108. static int acpi_pad_remove(struct acpi_device *device)
  109. {
  110. mutex_lock(&xen_cpu_lock);
  111. xen_acpi_pad_idle_cpus(0);
  112. mutex_unlock(&xen_cpu_lock);
  113. acpi_remove_notify_handler(device->handle,
  114. ACPI_DEVICE_NOTIFY, acpi_pad_notify);
  115. return 0;
  116. }
  117. static const struct acpi_device_id pad_device_ids[] = {
  118. {"ACPI000C", 0},
  119. {"", 0},
  120. };
  121. static struct acpi_driver acpi_pad_driver = {
  122. .name = "processor_aggregator",
  123. .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
  124. .ids = pad_device_ids,
  125. .ops = {
  126. .add = acpi_pad_add,
  127. .remove = acpi_pad_remove,
  128. },
  129. };
  130. static int __init xen_acpi_pad_init(void)
  131. {
  132. /* Only DOM0 is responsible for Xen acpi pad */
  133. if (!xen_initial_domain())
  134. return -ENODEV;
  135. /* Only Xen4.2 or later support Xen acpi pad */
  136. if (!xen_running_on_version_or_later(4, 2))
  137. return -ENODEV;
  138. return acpi_bus_register_driver(&acpi_pad_driver);
  139. }
  140. subsys_initcall(xen_acpi_pad_init);