2 * xen-acpi-pad.c - Xen pad interface
4 * Copyright (c) 2012, Intel Corporation.
5 * Author: Liu, Jinsong <jinsong.liu@intel.com>
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.
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
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/acpi.h>
22 #include <xen/interface/version.h>
23 #include <xen/xen-ops.h>
24 #include <asm/xen/hypercall.h>
26 #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
27 #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
28 #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
29 static DEFINE_MUTEX(xen_cpu_lock
);
31 static int xen_acpi_pad_idle_cpus(unsigned int idle_nums
)
33 struct xen_platform_op op
;
35 op
.cmd
= XENPF_core_parking
;
36 op
.u
.core_parking
.type
= XEN_CORE_PARKING_SET
;
37 op
.u
.core_parking
.idle_nums
= idle_nums
;
39 return HYPERVISOR_dom0_op(&op
);
42 static int xen_acpi_pad_idle_cpus_num(void)
44 struct xen_platform_op op
;
46 op
.cmd
= XENPF_core_parking
;
47 op
.u
.core_parking
.type
= XEN_CORE_PARKING_GET
;
49 return HYPERVISOR_dom0_op(&op
)
50 ?: op
.u
.core_parking
.idle_nums
;
54 * Query firmware how many CPUs should be idle
55 * return -1 on failure
57 static int acpi_pad_pur(acpi_handle handle
)
59 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
60 union acpi_object
*package
;
63 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_PUR", NULL
, &buffer
)))
66 if (!buffer
.length
|| !buffer
.pointer
)
69 package
= buffer
.pointer
;
71 if (package
->type
== ACPI_TYPE_PACKAGE
&&
72 package
->package
.count
== 2 &&
73 package
->package
.elements
[0].integer
.value
== 1) /* rev 1 */
74 num
= package
->package
.elements
[1].integer
.value
;
76 kfree(buffer
.pointer
);
80 /* Notify firmware how many CPUs are idle */
81 static void acpi_pad_ost(acpi_handle handle
, int stat
,
84 union acpi_object params
[3] = {
85 {.type
= ACPI_TYPE_INTEGER
,},
86 {.type
= ACPI_TYPE_INTEGER
,},
87 {.type
= ACPI_TYPE_BUFFER
,},
89 struct acpi_object_list arg_list
= {3, params
};
91 params
[0].integer
.value
= ACPI_PROCESSOR_AGGREGATOR_NOTIFY
;
92 params
[1].integer
.value
= stat
;
93 params
[2].buffer
.length
= 4;
94 params
[2].buffer
.pointer
= (void *)&idle_nums
;
95 acpi_evaluate_object(handle
, "_OST", &arg_list
, NULL
);
98 static void acpi_pad_handle_notify(acpi_handle handle
)
102 mutex_lock(&xen_cpu_lock
);
103 idle_nums
= acpi_pad_pur(handle
);
105 mutex_unlock(&xen_cpu_lock
);
109 idle_nums
= xen_acpi_pad_idle_cpus(idle_nums
)
110 ?: xen_acpi_pad_idle_cpus_num();
112 acpi_pad_ost(handle
, 0, idle_nums
);
113 mutex_unlock(&xen_cpu_lock
);
116 static void acpi_pad_notify(acpi_handle handle
, u32 event
,
120 case ACPI_PROCESSOR_AGGREGATOR_NOTIFY
:
121 acpi_pad_handle_notify(handle
);
124 pr_warn("Unsupported event [0x%x]\n", event
);
129 static int acpi_pad_add(struct acpi_device
*device
)
133 strcpy(acpi_device_name(device
), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME
);
134 strcpy(acpi_device_class(device
), ACPI_PROCESSOR_AGGREGATOR_CLASS
);
136 status
= acpi_install_notify_handler(device
->handle
,
137 ACPI_DEVICE_NOTIFY
, acpi_pad_notify
, device
);
138 if (ACPI_FAILURE(status
))
144 static int acpi_pad_remove(struct acpi_device
*device
)
146 mutex_lock(&xen_cpu_lock
);
147 xen_acpi_pad_idle_cpus(0);
148 mutex_unlock(&xen_cpu_lock
);
150 acpi_remove_notify_handler(device
->handle
,
151 ACPI_DEVICE_NOTIFY
, acpi_pad_notify
);
155 static const struct acpi_device_id pad_device_ids
[] = {
160 static struct acpi_driver acpi_pad_driver
= {
161 .name
= "processor_aggregator",
162 .class = ACPI_PROCESSOR_AGGREGATOR_CLASS
,
163 .ids
= pad_device_ids
,
166 .remove
= acpi_pad_remove
,
170 static int __init
xen_acpi_pad_init(void)
172 /* Only DOM0 is responsible for Xen acpi pad */
173 if (!xen_initial_domain())
176 /* Only Xen4.2 or later support Xen acpi pad */
177 if (!xen_running_on_version_or_later(4, 2))
180 return acpi_bus_register_driver(&acpi_pad_driver
);
182 subsys_initcall(xen_acpi_pad_init
);