xen-acpi-pad.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <acpi/acpi_bus.h>
  19. #include <acpi/acpi_drivers.h>
  20. #include <asm/xen/hypercall.h>
  21. #include <xen/interface/version.h>
  22. #include <xen/xen-ops.h>
  23. #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
  24. #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
  25. #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
  26. static DEFINE_MUTEX(xen_cpu_lock);
  27. static int xen_acpi_pad_idle_cpus(unsigned int idle_nums)
  28. {
  29. struct xen_platform_op op;
  30. op.cmd = XENPF_core_parking;
  31. op.u.core_parking.type = XEN_CORE_PARKING_SET;
  32. op.u.core_parking.idle_nums = idle_nums;
  33. return HYPERVISOR_dom0_op(&op);
  34. }
  35. static int xen_acpi_pad_idle_cpus_num(void)
  36. {
  37. struct xen_platform_op op;
  38. op.cmd = XENPF_core_parking;
  39. op.u.core_parking.type = XEN_CORE_PARKING_GET;
  40. return HYPERVISOR_dom0_op(&op)
  41. ?: op.u.core_parking.idle_nums;
  42. }
  43. /*
  44. * Query firmware how many CPUs should be idle
  45. * return -1 on failure
  46. */
  47. static int acpi_pad_pur(acpi_handle handle)
  48. {
  49. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  50. union acpi_object *package;
  51. int num = -1;
  52. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
  53. return num;
  54. if (!buffer.length || !buffer.pointer)
  55. return num;
  56. package = buffer.pointer;
  57. if (package->type == ACPI_TYPE_PACKAGE &&
  58. package->package.count == 2 &&
  59. package->package.elements[0].integer.value == 1) /* rev 1 */
  60. num = package->package.elements[1].integer.value;
  61. kfree(buffer.pointer);
  62. return num;
  63. }
  64. /* Notify firmware how many CPUs are idle */
  65. static void acpi_pad_ost(acpi_handle handle, int stat,
  66. uint32_t idle_nums)
  67. {
  68. union acpi_object params[3] = {
  69. {.type = ACPI_TYPE_INTEGER,},
  70. {.type = ACPI_TYPE_INTEGER,},
  71. {.type = ACPI_TYPE_BUFFER,},
  72. };
  73. struct acpi_object_list arg_list = {3, params};
  74. params[0].integer.value = ACPI_PROCESSOR_AGGREGATOR_NOTIFY;
  75. params[1].integer.value = stat;
  76. params[2].buffer.length = 4;
  77. params[2].buffer.pointer = (void *)&idle_nums;
  78. acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
  79. }
  80. static void acpi_pad_handle_notify(acpi_handle handle)
  81. {
  82. int idle_nums;
  83. mutex_lock(&xen_cpu_lock);
  84. idle_nums = acpi_pad_pur(handle);
  85. if (idle_nums < 0) {
  86. mutex_unlock(&xen_cpu_lock);
  87. return;
  88. }
  89. idle_nums = xen_acpi_pad_idle_cpus(idle_nums)
  90. ?: xen_acpi_pad_idle_cpus_num();
  91. if (idle_nums >= 0)
  92. acpi_pad_ost(handle, 0, idle_nums);
  93. mutex_unlock(&xen_cpu_lock);
  94. }
  95. static void acpi_pad_notify(acpi_handle handle, u32 event,
  96. void *data)
  97. {
  98. switch (event) {
  99. case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
  100. acpi_pad_handle_notify(handle);
  101. break;
  102. default:
  103. pr_warn("Unsupported event [0x%x]\n", event);
  104. break;
  105. }
  106. }
  107. static int acpi_pad_add(struct acpi_device *device)
  108. {
  109. acpi_status status;
  110. strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
  111. strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
  112. status = acpi_install_notify_handler(device->handle,
  113. ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
  114. if (ACPI_FAILURE(status))
  115. return -ENODEV;
  116. return 0;
  117. }
  118. static int acpi_pad_remove(struct acpi_device *device)
  119. {
  120. mutex_lock(&xen_cpu_lock);
  121. xen_acpi_pad_idle_cpus(0);
  122. mutex_unlock(&xen_cpu_lock);
  123. acpi_remove_notify_handler(device->handle,
  124. ACPI_DEVICE_NOTIFY, acpi_pad_notify);
  125. return 0;
  126. }
  127. static const struct acpi_device_id pad_device_ids[] = {
  128. {"ACPI000C", 0},
  129. {"", 0},
  130. };
  131. static struct acpi_driver acpi_pad_driver = {
  132. .name = "processor_aggregator",
  133. .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
  134. .ids = pad_device_ids,
  135. .ops = {
  136. .add = acpi_pad_add,
  137. .remove = acpi_pad_remove,
  138. },
  139. };
  140. static int __init xen_acpi_pad_init(void)
  141. {
  142. /* Only DOM0 is responsible for Xen acpi pad */
  143. if (!xen_initial_domain())
  144. return -ENODEV;
  145. /* Only Xen4.2 or later support Xen acpi pad */
  146. if (!xen_running_on_version_or_later(4, 2))
  147. return -ENODEV;
  148. return acpi_bus_register_driver(&acpi_pad_driver);
  149. }
  150. subsys_initcall(xen_acpi_pad_init);