Merge tag 'm68k-for-v6.4-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert...
[linux-block.git] / drivers / acpi / processor_driver.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
ac212b69 3 * processor_driver.c - ACPI Processor Driver
1da177e4
LT
4 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
8 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
9 * - Added processor hotplug support
ac212b69
RW
10 * Copyright (C) 2013, Intel Corporation
11 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1da177e4
LT
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
1da177e4
LT
17#include <linux/cpufreq.h>
18#include <linux/cpu.h>
4f86d3a8 19#include <linux/cpuidle.h>
5a0e3ad6 20#include <linux/slab.h>
47db4547 21#include <linux/acpi.h>
ac212b69 22
1da177e4
LT
23#include <acpi/processor.h>
24
ac212b69
RW
25#include "internal.h"
26
1da177e4
LT
27#define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
28#define ACPI_PROCESSOR_NOTIFY_POWER 0x81
01854e69 29#define ACPI_PROCESSOR_NOTIFY_THROTTLING 0x82
1da177e4 30
f52fd66d 31MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 32MODULE_DESCRIPTION("ACPI Processor Driver");
1da177e4
LT
33MODULE_LICENSE("GPL");
34
ac212b69
RW
35static int acpi_processor_start(struct device *dev);
36static int acpi_processor_stop(struct device *dev);
1da177e4 37
1ba90e3a 38static const struct acpi_device_id processor_device_ids[] = {
ad93a765 39 {ACPI_PROCESSOR_OBJECT_HID, 0},
9f324bda 40 {ACPI_PROCESSOR_DEVICE_HID, 0},
1ba90e3a
TR
41 {"", 0},
42};
43MODULE_DEVICE_TABLE(acpi, processor_device_ids);
44
ac212b69 45static struct device_driver acpi_processor_driver = {
c2b6705b 46 .name = "processor",
ac212b69
RW
47 .bus = &cpu_subsys,
48 .acpi_match_table = processor_device_ids,
49 .probe = acpi_processor_start,
50 .remove = acpi_processor_stop,
1da177e4
LT
51};
52
ac212b69 53static void acpi_processor_notify(acpi_handle handle, u32 event, void *data)
1da177e4 54{
ac212b69 55 struct acpi_device *device = data;
b26e9286 56 struct acpi_processor *pr;
e790cc8b 57 int saved;
1da177e4 58
ac212b69
RW
59 if (device->handle != handle)
60 return;
61
62 pr = acpi_driver_data(device);
1da177e4 63 if (!pr)
d550d98d 64 return;
1da177e4 65
1da177e4
LT
66 switch (event) {
67 case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
e790cc8b 68 saved = pr->performance_platform_limit;
d81c45e1 69 acpi_processor_ppc_has_changed(pr, 1);
e790cc8b
AS
70 if (saved == pr->performance_platform_limit)
71 break;
962ce8ca 72 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 73 dev_name(&device->dev), event,
962ce8ca 74 pr->performance_platform_limit);
1da177e4
LT
75 break;
76 case ACPI_PROCESSOR_NOTIFY_POWER:
a36a7fec 77 acpi_processor_power_state_has_changed(pr);
962ce8ca 78 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 79 dev_name(&device->dev), event, 0);
1da177e4 80 break;
01854e69
LY
81 case ACPI_PROCESSOR_NOTIFY_THROTTLING:
82 acpi_processor_tstate_has_changed(pr);
962ce8ca 83 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 84 dev_name(&device->dev), event, 0);
879dca01 85 break;
1da177e4 86 default:
52af99c3 87 acpi_handle_debug(handle, "Unsupported event [0x%x]\n", event);
1da177e4
LT
88 break;
89 }
90
d550d98d 91 return;
1da177e4
LT
92}
93
fe7bf106 94static int __acpi_processor_start(struct acpi_device *device);
ac212b69 95
64f3bf2f 96static int acpi_soft_cpu_online(unsigned int cpu)
729c6ba3 97{
706546d0 98 struct acpi_processor *pr = per_cpu(processors, cpu);
ac212b69 99 struct acpi_device *device;
8da83734 100
99ece713
RW
101 if (!pr)
102 return 0;
103
104 device = acpi_fetch_acpi_dev(pr->handle);
105 if (!device)
64f3bf2f 106 return 0;
99ece713 107
64f3bf2f
SAS
108 /*
109 * CPU got physically hotplugged and onlined for the first time:
110 * Initialize missing things.
111 */
112 if (pr->flags.need_hotplug_init) {
113 int ret;
114
115 pr_info("Will online and init hotplugged CPU: %d\n",
116 pr->id);
117 pr->flags.need_hotplug_init = 0;
118 ret = __acpi_processor_start(device);
119 WARN(ret, "Failed to start CPU: %d\n", pr->id);
120 } else {
121 /* Normal CPU soft online event. */
122 acpi_processor_ppc_has_changed(pr, 0);
123 acpi_processor_hotplug(pr);
124 acpi_processor_reevaluate_tstate(pr, false);
125 acpi_processor_tstate_has_changed(pr);
5a344a50 126 }
64f3bf2f 127 return 0;
729c6ba3
VP
128}
129
64f3bf2f
SAS
130static int acpi_soft_cpu_dead(unsigned int cpu)
131{
132 struct acpi_processor *pr = per_cpu(processors, cpu);
64f3bf2f 133
99ece713 134 if (!pr || !acpi_fetch_acpi_dev(pr->handle))
64f3bf2f
SAS
135 return 0;
136
137 acpi_processor_reevaluate_tstate(pr, true);
138 return 0;
139}
729c6ba3 140
239708a3 141#ifdef CONFIG_ACPI_CPU_FREQ_PSS
7fdc74da 142static void acpi_pss_perf_init(struct acpi_processor *pr)
54d5dcc4 143{
54d5dcc4 144 acpi_processor_ppc_has_changed(pr, 0);
239708a3 145
54d5dcc4 146 acpi_processor_get_throttling_info(pr);
22e7551e
LT
147
148 if (pr->flags.throttling)
149 pr->flags.limit = 1;
239708a3
AC
150}
151#else
7fdc74da 152static inline void acpi_pss_perf_init(struct acpi_processor *pr) {}
239708a3
AC
153#endif /* CONFIG_ACPI_CPU_FREQ_PSS */
154
155static int __acpi_processor_start(struct acpi_device *device)
156{
157 struct acpi_processor *pr = acpi_driver_data(device);
158 acpi_status status;
159 int result = 0;
160
161 if (!pr)
162 return -ENODEV;
163
164 if (pr->flags.need_hotplug_init)
165 return 0;
166
4f2f7573 167 result = acpi_cppc_processor_probe(pr);
65e95891 168 if (result && !IS_ENABLED(CONFIG_ACPI_CPU_FREQ_PSS))
512bb03f 169 dev_dbg(&device->dev, "CPPC data invalid or not present\n");
4f2f7573 170
239708a3
AC
171 if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
172 acpi_processor_power_init(pr);
173
7fdc74da
RL
174 acpi_pss_perf_init(pr);
175
176 result = acpi_processor_thermal_init(pr, device);
239708a3
AC
177 if (result)
178 goto err_power_exit;
179
180 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
181 acpi_processor_notify, device);
182 if (ACPI_SUCCESS(status))
183 return 0;
184
a5cbdf69 185 result = -ENODEV;
7fdc74da 186 acpi_processor_thermal_exit(pr, device);
a5cbdf69 187
239708a3 188err_power_exit:
38a991b6 189 acpi_processor_power_exit(pr);
54d5dcc4
TR
190 return result;
191}
192
fe7bf106 193static int acpi_processor_start(struct device *dev)
1da177e4 194{
d0a7edbb 195 struct acpi_device *device = ACPI_COMPANION(dev);
8153f9ac 196 int ret;
1da177e4 197
d0a7edbb 198 if (!device)
ac212b69 199 return -ENODEV;
1da177e4 200
8153f9ac 201 /* Protect against concurrent CPU hotplug operations */
fdaf0a51 202 cpu_hotplug_disable();
8153f9ac 203 ret = __acpi_processor_start(device);
fdaf0a51 204 cpu_hotplug_enable();
8153f9ac 205 return ret;
1da177e4
LT
206}
207
ac212b69 208static int acpi_processor_stop(struct device *dev)
1da177e4 209{
d0a7edbb 210 struct acpi_device *device = ACPI_COMPANION(dev);
ac212b69 211 struct acpi_processor *pr;
1da177e4 212
d0a7edbb 213 if (!device)
ac212b69 214 return 0;
1da177e4 215
ac212b69
RW
216 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
217 acpi_processor_notify);
1da177e4 218
50dd0969 219 pr = acpi_driver_data(device);
ac212b69
RW
220 if (!pr)
221 return 0;
38a991b6 222 acpi_processor_power_exit(pr);
1da177e4 223
4f2f7573
AC
224 acpi_cppc_processor_exit(pr);
225
7fdc74da
RL
226 acpi_processor_thermal_exit(pr, device);
227
d550d98d 228 return 0;
1da177e4
LT
229}
230
d15ce412
VK
231bool acpi_processor_cpufreq_init;
232
233static int acpi_processor_notifier(struct notifier_block *nb,
234 unsigned long event, void *data)
235{
236 struct cpufreq_policy *policy = data;
d15ce412
VK
237
238 if (event == CPUFREQ_CREATE_POLICY) {
3000ce3c
RW
239 acpi_thermal_cpufreq_init(policy);
240 acpi_processor_ppc_init(policy);
d15ce412 241 } else if (event == CPUFREQ_REMOVE_POLICY) {
3000ce3c
RW
242 acpi_processor_ppc_exit(policy);
243 acpi_thermal_cpufreq_exit(policy);
d15ce412
VK
244 }
245
246 return 0;
247}
248
249static struct notifier_block acpi_processor_notifier_block = {
250 .notifier_call = acpi_processor_notifier,
251};
252
1da177e4
LT
253/*
254 * We keep the driver loaded even when ACPI is not running.
255 * This is needed for the powernow-k8 driver, that works even without
256 * ACPI, but needs symbols from this driver
257 */
64f3bf2f 258static enum cpuhp_state hp_online;
ac212b69 259static int __init acpi_processor_driver_init(void)
1da177e4 260{
4be44fcd 261 int result = 0;
1da177e4 262
ce8442b5
YL
263 if (acpi_disabled)
264 return 0;
265
c0e0421a
RW
266 if (!cpufreq_register_notifier(&acpi_processor_notifier_block,
267 CPUFREQ_POLICY_NOTIFIER)) {
268 acpi_processor_cpufreq_init = true;
269 acpi_processor_ignore_ppc_init();
270 }
271
ac212b69 272 result = driver_register(&acpi_processor_driver);
4f86d3a8 273 if (result < 0)
46bcfad7 274 return result;
1da177e4 275
64f3bf2f
SAS
276 result = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
277 "acpi/cpu-drv:online",
278 acpi_soft_cpu_online, NULL);
279 if (result < 0)
280 goto err;
281 hp_online = result;
282 cpuhp_setup_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD, "acpi/cpu-drv:dead",
283 NULL, acpi_soft_cpu_dead);
284
1180509f 285 acpi_processor_throttling_init();
d550d98d 286 return 0;
64f3bf2f
SAS
287err:
288 driver_unregister(&acpi_processor_driver);
289 return result;
1da177e4
LT
290}
291
ac212b69 292static void __exit acpi_processor_driver_exit(void)
1da177e4 293{
ce8442b5
YL
294 if (acpi_disabled)
295 return;
296
d15ce412
VK
297 if (acpi_processor_cpufreq_init) {
298 cpufreq_unregister_notifier(&acpi_processor_notifier_block,
299 CPUFREQ_POLICY_NOTIFIER);
300 acpi_processor_cpufreq_init = false;
301 }
302
64f3bf2f
SAS
303 cpuhp_remove_state_nocalls(hp_online);
304 cpuhp_remove_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD);
ac212b69 305 driver_unregister(&acpi_processor_driver);
1da177e4
LT
306}
307
ac212b69
RW
308module_init(acpi_processor_driver_init);
309module_exit(acpi_processor_driver_exit);
1da177e4 310
1da177e4 311MODULE_ALIAS("processor");