xref: /linux/drivers/xen/xen-acpi-pad.c (revision 48f76a12713253f3abaa39c4ff7606d6fed05a7e)
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 <jinsong.liu@intel.com>
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/acpi.h>
14 #include <linux/platform_device.h>
15 #include <xen/xen.h>
16 #include <xen/interface/version.h>
17 #include <xen/xen-ops.h>
18 #include <asm/xen/hypercall.h>
19 
20 #define ACPI_PROCESSOR_AGGREGATOR_CLASS	"acpi_pad"
21 #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
22 #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
23 static DEFINE_MUTEX(xen_cpu_lock);
24 
xen_acpi_pad_idle_cpus(unsigned int idle_nums)25 static int xen_acpi_pad_idle_cpus(unsigned int idle_nums)
26 {
27 	struct xen_platform_op op;
28 
29 	op.cmd = XENPF_core_parking;
30 	op.u.core_parking.type = XEN_CORE_PARKING_SET;
31 	op.u.core_parking.idle_nums = idle_nums;
32 
33 	return HYPERVISOR_platform_op(&op);
34 }
35 
xen_acpi_pad_idle_cpus_num(void)36 static int xen_acpi_pad_idle_cpus_num(void)
37 {
38 	struct xen_platform_op op;
39 
40 	op.cmd = XENPF_core_parking;
41 	op.u.core_parking.type = XEN_CORE_PARKING_GET;
42 
43 	return HYPERVISOR_platform_op(&op)
44 	       ?: op.u.core_parking.idle_nums;
45 }
46 
47 /*
48  * Query firmware how many CPUs should be idle
49  * return -1 on failure
50  */
acpi_pad_pur(acpi_handle handle)51 static int acpi_pad_pur(acpi_handle handle)
52 {
53 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
54 	union acpi_object *package;
55 	int num = -1;
56 
57 	if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
58 		return num;
59 
60 	if (!buffer.length || !buffer.pointer)
61 		return num;
62 
63 	package = buffer.pointer;
64 
65 	if (package->type == ACPI_TYPE_PACKAGE &&
66 		package->package.count == 2 &&
67 		package->package.elements[0].integer.value == 1) /* rev 1 */
68 		num = package->package.elements[1].integer.value;
69 
70 	kfree(buffer.pointer);
71 	return num;
72 }
73 
acpi_pad_handle_notify(acpi_handle handle)74 static void acpi_pad_handle_notify(acpi_handle handle)
75 {
76 	int idle_nums;
77 	struct acpi_buffer param = {
78 		.length = 4,
79 		.pointer = (void *)&idle_nums,
80 	};
81 
82 
83 	mutex_lock(&xen_cpu_lock);
84 	idle_nums = acpi_pad_pur(handle);
85 	if (idle_nums < 0) {
86 		mutex_unlock(&xen_cpu_lock);
87 		return;
88 	}
89 
90 	idle_nums = xen_acpi_pad_idle_cpus(idle_nums)
91 		    ?: xen_acpi_pad_idle_cpus_num();
92 	if (idle_nums >= 0)
93 		acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY,
94 				  0, &param);
95 	mutex_unlock(&xen_cpu_lock);
96 }
97 
acpi_pad_notify(acpi_handle handle,u32 event,void * data)98 static void acpi_pad_notify(acpi_handle handle, u32 event,
99 	void *data)
100 {
101 	switch (event) {
102 	case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
103 		acpi_pad_handle_notify(handle);
104 		break;
105 	default:
106 		pr_warn("Unsupported event [0x%x]\n", event);
107 		break;
108 	}
109 }
110 
acpi_pad_probe(struct platform_device * pdev)111 static int acpi_pad_probe(struct platform_device *pdev)
112 {
113 	struct acpi_device *device;
114 	acpi_status status;
115 
116 	device = ACPI_COMPANION(&pdev->dev);
117 	if (!device)
118 		return -ENODEV;
119 
120 	strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
121 	strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
122 
123 	status = acpi_install_notify_handler(device->handle,
124 		ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
125 	if (ACPI_FAILURE(status))
126 		return -ENODEV;
127 
128 	return 0;
129 }
130 
acpi_pad_remove(struct platform_device * pdev)131 static void acpi_pad_remove(struct platform_device *pdev)
132 {
133 	mutex_lock(&xen_cpu_lock);
134 	xen_acpi_pad_idle_cpus(0);
135 	mutex_unlock(&xen_cpu_lock);
136 
137 	acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
138 		ACPI_DEVICE_NOTIFY, acpi_pad_notify);
139 }
140 
141 static const struct acpi_device_id pad_device_ids[] = {
142 	{"ACPI000C", 0},
143 	{"", 0},
144 };
145 
146 static struct platform_driver acpi_pad_driver = {
147 	.probe = acpi_pad_probe,
148 	.remove = acpi_pad_remove,
149 	.driver = {
150 		.name = "acpi_processor_aggregator",
151 		.acpi_match_table = pad_device_ids,
152 	},
153 };
154 
xen_acpi_pad_init(void)155 static int __init xen_acpi_pad_init(void)
156 {
157 	/* Only DOM0 is responsible for Xen acpi pad */
158 	if (!xen_initial_domain())
159 		return -ENODEV;
160 
161 	/* Only Xen4.2 or later support Xen acpi pad */
162 	if (!xen_running_on_version_or_later(4, 2))
163 		return -ENODEV;
164 
165 	return platform_driver_register(&acpi_pad_driver);
166 }
167 subsys_initcall(xen_acpi_pad_init);
168