xen-acpi-pad.c 3.7 KB

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