xen-acpi-pad.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/interface/version.h>
  21. #include <xen/xen-ops.h>
  22. #include <asm/xen/hypercall.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. static void acpi_pad_handle_notify(acpi_handle handle)
  65. {
  66. int idle_nums;
  67. struct acpi_buffer param = {
  68. .length = 4,
  69. .pointer = (void *)&idle_nums,
  70. };
  71. mutex_lock(&xen_cpu_lock);
  72. idle_nums = acpi_pad_pur(handle);
  73. if (idle_nums < 0) {
  74. mutex_unlock(&xen_cpu_lock);
  75. return;
  76. }
  77. idle_nums = xen_acpi_pad_idle_cpus(idle_nums)
  78. ?: xen_acpi_pad_idle_cpus_num();
  79. if (idle_nums >= 0)
  80. acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY,
  81. 0, &param);
  82. mutex_unlock(&xen_cpu_lock);
  83. }
  84. static void acpi_pad_notify(acpi_handle handle, u32 event,
  85. void *data)
  86. {
  87. switch (event) {
  88. case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
  89. acpi_pad_handle_notify(handle);
  90. break;
  91. default:
  92. pr_warn("Unsupported event [0x%x]\n", event);
  93. break;
  94. }
  95. }
  96. static int acpi_pad_add(struct acpi_device *device)
  97. {
  98. acpi_status status;
  99. strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
  100. strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
  101. status = acpi_install_notify_handler(device->handle,
  102. ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
  103. if (ACPI_FAILURE(status))
  104. return -ENODEV;
  105. return 0;
  106. }
  107. static int acpi_pad_remove(struct acpi_device *device)
  108. {
  109. mutex_lock(&xen_cpu_lock);
  110. xen_acpi_pad_idle_cpus(0);
  111. mutex_unlock(&xen_cpu_lock);
  112. acpi_remove_notify_handler(device->handle,
  113. ACPI_DEVICE_NOTIFY, acpi_pad_notify);
  114. return 0;
  115. }
  116. static const struct acpi_device_id pad_device_ids[] = {
  117. {"ACPI000C", 0},
  118. {"", 0},
  119. };
  120. static struct acpi_driver acpi_pad_driver = {
  121. .name = "processor_aggregator",
  122. .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
  123. .ids = pad_device_ids,
  124. .ops = {
  125. .add = acpi_pad_add,
  126. .remove = acpi_pad_remove,
  127. },
  128. };
  129. static int __init xen_acpi_pad_init(void)
  130. {
  131. /* Only DOM0 is responsible for Xen acpi pad */
  132. if (!xen_initial_domain())
  133. return -ENODEV;
  134. /* Only Xen4.2 or later support Xen acpi pad */
  135. if (!xen_running_on_version_or_later(4, 2))
  136. return -ENODEV;
  137. return acpi_bus_register_driver(&acpi_pad_driver);
  138. }
  139. subsys_initcall(xen_acpi_pad_init);