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