blob: 62114a03a51aa134dbf5254f105392c60c27a550 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02003 * processor_driver.c - ACPI Processor Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020010 * Copyright (C) 2013, Intel Corporation
11 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/cpufreq.h>
18#include <linux/cpu.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040019#include <linux/cpuidle.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Toshi Kani47db4542012-11-20 23:42:27 +000021#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <acpi/processor.h>
24
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020025#include "internal.h"
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
28#define ACPI_PROCESSOR_NOTIFY_POWER 0x81
Luming Yu01854e62007-05-26 22:49:58 +080029#define ACPI_PROCESSOR_NOTIFY_THROTTLING 0x82
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Alex Chiang0131aa32010-02-22 12:11:08 -070032ACPI_MODULE_NAME("processor_driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Len Brownf52fd662007-02-12 22:42:12 -050034MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050035MODULE_DESCRIPTION("ACPI Processor Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036MODULE_LICENSE("GPL");
37
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020038static int acpi_processor_start(struct device *dev);
39static int acpi_processor_stop(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Thomas Renninger1ba90e32007-07-23 14:44:41 +020041static const struct acpi_device_id processor_device_ids[] = {
Myron Stowead93a762008-11-04 14:52:55 -070042 {ACPI_PROCESSOR_OBJECT_HID, 0},
Toshi Kani9f324bd2012-03-19 13:08:02 -060043 {ACPI_PROCESSOR_DEVICE_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +020044 {"", 0},
45};
46MODULE_DEVICE_TABLE(acpi, processor_device_ids);
47
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020048static struct device_driver acpi_processor_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050049 .name = "processor",
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020050 .bus = &cpu_subsys,
51 .acpi_match_table = processor_device_ids,
52 .probe = acpi_processor_start,
53 .remove = acpi_processor_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020056static void acpi_processor_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020058 struct acpi_device *device = data;
Myron Stoweb26e9282008-11-04 14:53:00 -070059 struct acpi_processor *pr;
Alexey Starikovskiye790cc82007-11-21 03:23:32 +030060 int saved;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020062 if (device->handle != handle)
63 return;
64
65 pr = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -040067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 switch (event) {
70 case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
Alexey Starikovskiye790cc82007-11-21 03:23:32 +030071 saved = pr->performance_platform_limit;
Zhao Yakuid81c45e12009-10-16 09:20:41 +080072 acpi_processor_ppc_has_changed(pr, 1);
Alexey Starikovskiye790cc82007-11-21 03:23:32 +030073 if (saved == pr->performance_platform_limit)
74 break;
Zhang Rui962ce8c2007-08-23 01:24:31 +080075 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +010076 dev_name(&device->dev), event,
Zhang Rui962ce8c2007-08-23 01:24:31 +080077 pr->performance_platform_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 case ACPI_PROCESSOR_NOTIFY_POWER:
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +010080 acpi_processor_power_state_has_changed(pr);
Zhang Rui962ce8c2007-08-23 01:24:31 +080081 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +010082 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 break;
Luming Yu01854e62007-05-26 22:49:58 +080084 case ACPI_PROCESSOR_NOTIFY_THROTTLING:
85 acpi_processor_tstate_has_changed(pr);
Zhang Rui962ce8c2007-08-23 01:24:31 +080086 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +010087 dev_name(&device->dev), event, 0);
Alan Cox879dca02012-10-26 01:05:56 +020088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 default:
90 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -040091 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 break;
93 }
94
Patrick Mocheld550d982006-06-27 00:41:40 -040095 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Paul Gortmakerfe7bf102013-06-19 14:30:58 -040098static int __acpi_processor_start(struct acpi_device *device);
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020099
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200100static int acpi_soft_cpu_online(unsigned int cpu)
Venkatesh Pallipadi729c6ba2007-09-16 15:36:43 +0200101{
Mike Travis706546d2008-06-09 16:22:23 -0700102 struct acpi_processor *pr = per_cpu(processors, cpu);
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200103 struct acpi_device *device;
Toshi Kani8da83732014-05-08 07:58:59 -0600104
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200105 if (!pr || acpi_bus_get_device(pr->handle, &device))
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200106 return 0;
107 /*
108 * CPU got physically hotplugged and onlined for the first time:
109 * Initialize missing things.
110 */
111 if (pr->flags.need_hotplug_init) {
112 int ret;
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200113
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200114 pr_info("Will online and init hotplugged CPU: %d\n",
115 pr->id);
116 pr->flags.need_hotplug_init = 0;
117 ret = __acpi_processor_start(device);
118 WARN(ret, "Failed to start CPU: %d\n", pr->id);
119 } else {
120 /* Normal CPU soft online event. */
121 acpi_processor_ppc_has_changed(pr, 0);
122 acpi_processor_hotplug(pr);
123 acpi_processor_reevaluate_tstate(pr, false);
124 acpi_processor_tstate_has_changed(pr);
Zhao Yakui5a344a52011-01-10 16:35:45 +0800125 }
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200126 return 0;
Venkatesh Pallipadi729c6ba2007-09-16 15:36:43 +0200127}
128
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200129static int acpi_soft_cpu_dead(unsigned int cpu)
130{
131 struct acpi_processor *pr = per_cpu(processors, cpu);
132 struct acpi_device *device;
133
134 if (!pr || acpi_bus_get_device(pr->handle, &device))
135 return 0;
136
137 acpi_processor_reevaluate_tstate(pr, true);
138 return 0;
139}
Venkatesh Pallipadi729c6ba2007-09-16 15:36:43 +0200140
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400141#ifdef CONFIG_ACPI_CPU_FREQ_PSS
142static int acpi_pss_perf_init(struct acpi_processor *pr,
143 struct acpi_device *device)
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100144{
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100145 int result = 0;
146
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100147 acpi_processor_ppc_has_changed(pr, 0);
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400148
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100149 acpi_processor_get_throttling_info(pr);
Lan Tianyu22e75512013-08-13 12:11:22 +0200150
151 if (pr->flags.throttling)
152 pr->flags.limit = 1;
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100153
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100154 pr->cdev = thermal_cooling_device_register("Processor", device,
155 &processor_cooling_ops);
156 if (IS_ERR(pr->cdev)) {
157 result = PTR_ERR(pr->cdev);
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400158 return result;
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100159 }
160
161 dev_dbg(&device->dev, "registered as cooling_device%d\n",
162 pr->cdev->id);
163
164 result = sysfs_create_link(&device->dev.kobj,
165 &pr->cdev->device.kobj,
166 "thermal_cooling");
167 if (result) {
Toshi Kani47db4542012-11-20 23:42:27 +0000168 dev_err(&device->dev,
169 "Failed to create sysfs link 'thermal_cooling'\n");
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100170 goto err_thermal_unregister;
171 }
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400172
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100173 result = sysfs_create_link(&pr->cdev->device.kobj,
174 &device->dev.kobj,
175 "device");
176 if (result) {
Toshi Kani47db4542012-11-20 23:42:27 +0000177 dev_err(&pr->cdev->device,
178 "Failed to create sysfs link 'device'\n");
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100179 goto err_remove_sysfs_thermal;
180 }
181
Srinivas Pandruvadace360db2015-12-16 20:32:23 -0800182 return 0;
183
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200184 err_remove_sysfs_thermal:
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100185 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200186 err_thermal_unregister:
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100187 thermal_cooling_device_unregister(pr->cdev);
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400188
189 return result;
190}
191
192static void acpi_pss_perf_exit(struct acpi_processor *pr,
193 struct acpi_device *device)
194{
195 if (pr->cdev) {
196 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
197 sysfs_remove_link(&pr->cdev->device.kobj, "device");
198 thermal_cooling_device_unregister(pr->cdev);
199 pr->cdev = NULL;
200 }
201}
202#else
203static inline int acpi_pss_perf_init(struct acpi_processor *pr,
204 struct acpi_device *device)
205{
206 return 0;
207}
208
209static inline void acpi_pss_perf_exit(struct acpi_processor *pr,
210 struct acpi_device *device) {}
211#endif /* CONFIG_ACPI_CPU_FREQ_PSS */
212
213static int __acpi_processor_start(struct acpi_device *device)
214{
215 struct acpi_processor *pr = acpi_driver_data(device);
216 acpi_status status;
217 int result = 0;
218
219 if (!pr)
220 return -ENODEV;
221
222 if (pr->flags.need_hotplug_init)
223 return 0;
224
Ashwin Chaugule4f2f7572015-09-09 16:27:07 -0400225 result = acpi_cppc_processor_probe(pr);
Srinivas Pandruvada65e95892016-09-01 13:37:08 -0700226 if (result && !IS_ENABLED(CONFIG_ACPI_CPU_FREQ_PSS))
Hanjun Guo512bb032017-07-26 18:40:12 +0800227 dev_dbg(&device->dev, "CPPC data invalid or not present\n");
Ashwin Chaugule4f2f7572015-09-09 16:27:07 -0400228
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400229 if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
230 acpi_processor_power_init(pr);
231
232 result = acpi_pss_perf_init(pr, device);
233 if (result)
234 goto err_power_exit;
235
236 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
237 acpi_processor_notify, device);
238 if (ACPI_SUCCESS(status))
239 return 0;
240
Thomas Gleixnera5cbdf62017-04-12 22:07:33 +0200241 result = -ENODEV;
242 acpi_pss_perf_exit(pr, device);
243
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400244err_power_exit:
Daniel Lezcano38a991b2012-09-15 22:42:54 +0200245 acpi_processor_power_exit(pr);
Thomas Renninger54d5dcc2012-01-19 18:18:42 +0100246 return result;
247}
248
Paul Gortmakerfe7bf102013-06-19 14:30:58 -0400249static int acpi_processor_start(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Lan Tianyud0a7edb2013-12-06 15:36:26 +0800251 struct acpi_device *device = ACPI_COMPANION(dev);
Thomas Gleixner8153f9a2017-04-12 22:07:34 +0200252 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Lan Tianyud0a7edb2013-12-06 15:36:26 +0800254 if (!device)
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200255 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Thomas Gleixner8153f9a2017-04-12 22:07:34 +0200257 /* Protect against concurrent CPU hotplug operations */
Thomas Gleixnerfdaf0a52017-05-24 10:15:33 +0200258 cpu_hotplug_disable();
Thomas Gleixner8153f9a2017-04-12 22:07:34 +0200259 ret = __acpi_processor_start(device);
Thomas Gleixnerfdaf0a52017-05-24 10:15:33 +0200260 cpu_hotplug_enable();
Thomas Gleixner8153f9a2017-04-12 22:07:34 +0200261 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200264static int acpi_processor_stop(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Lan Tianyud0a7edb2013-12-06 15:36:26 +0800266 struct acpi_device *device = ACPI_COMPANION(dev);
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200267 struct acpi_processor *pr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Lan Tianyud0a7edb2013-12-06 15:36:26 +0800269 if (!device)
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200272 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
273 acpi_processor_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200275 pr = acpi_driver_data(device);
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200276 if (!pr)
277 return 0;
Daniel Lezcano38a991b2012-09-15 22:42:54 +0200278 acpi_processor_power_exit(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Ashwin Chaugule239708a2015-08-05 09:40:25 -0400280 acpi_pss_perf_exit(pr, device);
281
Ashwin Chaugule4f2f7572015-09-09 16:27:07 -0400282 acpi_cppc_processor_exit(pr);
283
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Viresh Kumard15ce412019-08-28 14:20:13 +0530287bool acpi_processor_cpufreq_init;
288
289static int acpi_processor_notifier(struct notifier_block *nb,
290 unsigned long event, void *data)
291{
292 struct cpufreq_policy *policy = data;
Viresh Kumard15ce412019-08-28 14:20:13 +0530293
294 if (event == CPUFREQ_CREATE_POLICY) {
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200295 acpi_thermal_cpufreq_init(policy);
296 acpi_processor_ppc_init(policy);
Viresh Kumard15ce412019-08-28 14:20:13 +0530297 } else if (event == CPUFREQ_REMOVE_POLICY) {
Rafael J. Wysocki3000ce32019-10-16 12:47:06 +0200298 acpi_processor_ppc_exit(policy);
299 acpi_thermal_cpufreq_exit(policy);
Viresh Kumard15ce412019-08-28 14:20:13 +0530300 }
301
302 return 0;
303}
304
305static struct notifier_block acpi_processor_notifier_block = {
306 .notifier_call = acpi_processor_notifier,
307};
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * We keep the driver loaded even when ACPI is not running.
311 * This is needed for the powernow-k8 driver, that works even without
312 * ACPI, but needs symbols from this driver
313 */
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200314static enum cpuhp_state hp_online;
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200315static int __init acpi_processor_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Len Brown4be44fc2005-08-05 00:44:28 -0400317 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Yinghai Luce8442b2009-08-26 14:29:26 -0700319 if (acpi_disabled)
320 return 0;
321
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200322 result = driver_register(&acpi_processor_driver);
Len Brown4f86d3a2007-10-03 18:58:00 -0400323 if (result < 0)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530324 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200326 result = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
327 "acpi/cpu-drv:online",
328 acpi_soft_cpu_online, NULL);
329 if (result < 0)
330 goto err;
331 hp_online = result;
332 cpuhp_setup_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD, "acpi/cpu-drv:dead",
333 NULL, acpi_soft_cpu_dead);
334
Viresh Kumard15ce412019-08-28 14:20:13 +0530335 if (!cpufreq_register_notifier(&acpi_processor_notifier_block,
336 CPUFREQ_POLICY_NOTIFIER)) {
337 acpi_processor_cpufreq_init = true;
338 acpi_processor_ignore_ppc_init();
339 }
340
Zhao Yakui11805092008-01-28 13:53:42 +0800341 acpi_processor_throttling_init();
Patrick Mocheld550d982006-06-27 00:41:40 -0400342 return 0;
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200343err:
344 driver_unregister(&acpi_processor_driver);
345 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200348static void __exit acpi_processor_driver_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Yinghai Luce8442b2009-08-26 14:29:26 -0700350 if (acpi_disabled)
351 return;
352
Viresh Kumard15ce412019-08-28 14:20:13 +0530353 if (acpi_processor_cpufreq_init) {
354 cpufreq_unregister_notifier(&acpi_processor_notifier_block,
355 CPUFREQ_POLICY_NOTIFIER);
356 acpi_processor_cpufreq_init = false;
357 }
358
Sebastian Andrzej Siewior64f3bf22016-09-06 19:04:47 +0200359 cpuhp_remove_state_nocalls(hp_online);
360 cpuhp_remove_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD);
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200361 driver_unregister(&acpi_processor_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200364module_init(acpi_processor_driver_init);
365module_exit(acpi_processor_driver_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367MODULE_ALIAS("processor");