1 // SPDX-License-Identifier: GPL-2.0-only
3 * xen-acpi-pad.c - Xen pad interface
5 * Copyright (c) 2012, Intel Corporation.
6 * Author: Liu, Jinsong <jinsong.liu@intel.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/acpi.h>
15 #include <xen/interface/version.h>
16 #include <xen/xen-ops.h>
17 #include <asm/xen/hypercall.h>
19 #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
20 #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
21 #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
22 static DEFINE_MUTEX(xen_cpu_lock);
24 static int xen_acpi_pad_idle_cpus(unsigned int idle_nums)
26 struct xen_platform_op op;
28 op.cmd = XENPF_core_parking;
29 op.u.core_parking.type = XEN_CORE_PARKING_SET;
30 op.u.core_parking.idle_nums = idle_nums;
32 return HYPERVISOR_platform_op(&op);
35 static int xen_acpi_pad_idle_cpus_num(void)
37 struct xen_platform_op op;
39 op.cmd = XENPF_core_parking;
40 op.u.core_parking.type = XEN_CORE_PARKING_GET;
42 return HYPERVISOR_platform_op(&op)
43 ?: op.u.core_parking.idle_nums;
47 * Query firmware how many CPUs should be idle
48 * return -1 on failure
50 static int acpi_pad_pur(acpi_handle handle)
52 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
53 union acpi_object *package;
56 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
59 if (!buffer.length || !buffer.pointer)
62 package = buffer.pointer;
64 if (package->type == ACPI_TYPE_PACKAGE &&
65 package->package.count == 2 &&
66 package->package.elements[0].integer.value == 1) /* rev 1 */
67 num = package->package.elements[1].integer.value;
69 kfree(buffer.pointer);
73 static void acpi_pad_handle_notify(acpi_handle handle)
76 struct acpi_buffer param = {
78 .pointer = (void *)&idle_nums,
82 mutex_lock(&xen_cpu_lock);
83 idle_nums = acpi_pad_pur(handle);
85 mutex_unlock(&xen_cpu_lock);
89 idle_nums = xen_acpi_pad_idle_cpus(idle_nums)
90 ?: xen_acpi_pad_idle_cpus_num();
92 acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY,
94 mutex_unlock(&xen_cpu_lock);
97 static void acpi_pad_notify(acpi_handle handle, u32 event,
101 case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
102 acpi_pad_handle_notify(handle);
105 pr_warn("Unsupported event [0x%x]\n", event);
110 static int acpi_pad_add(struct acpi_device *device)
114 strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
115 strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
117 status = acpi_install_notify_handler(device->handle,
118 ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
119 if (ACPI_FAILURE(status))
125 static int acpi_pad_remove(struct acpi_device *device)
127 mutex_lock(&xen_cpu_lock);
128 xen_acpi_pad_idle_cpus(0);
129 mutex_unlock(&xen_cpu_lock);
131 acpi_remove_notify_handler(device->handle,
132 ACPI_DEVICE_NOTIFY, acpi_pad_notify);
136 static const struct acpi_device_id pad_device_ids[] = {
141 static struct acpi_driver acpi_pad_driver = {
142 .name = "processor_aggregator",
143 .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
144 .ids = pad_device_ids,
147 .remove = acpi_pad_remove,
151 static int __init xen_acpi_pad_init(void)
153 /* Only DOM0 is responsible for Xen acpi pad */
154 if (!xen_initial_domain())
157 /* Only Xen4.2 or later support Xen acpi pad */
158 if (!xen_running_on_version_or_later(4, 2))
161 return acpi_bus_register_driver(&acpi_pad_driver);
163 subsys_initcall(xen_acpi_pad_init);