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