1 /*
2  * processor_driver.c - ACPI Processor Driver
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2004       Dominik Brodowski <linux@brodo.de>
7  *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8  *  			- Added processor hotplug support
9  *  Copyright (C) 2013, Intel Corporation
10  *                      Rafael J. Wysocki <rafael.j.wysocki@intel.com>
11  *
12  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2 of the License, or (at
17  *  your option) any later version.
18  *
19  *  This program is distributed in the hope that it will be useful, but
20  *  WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  *  General Public License for more details.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26 
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/cpufreq.h>
31 #include <linux/cpu.h>
32 #include <linux/cpuidle.h>
33 #include <linux/slab.h>
34 #include <linux/acpi.h>
35 
36 #include <acpi/processor.h>
37 
38 #include "internal.h"
39 
40 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
41 #define ACPI_PROCESSOR_NOTIFY_POWER	0x81
42 #define ACPI_PROCESSOR_NOTIFY_THROTTLING	0x82
43 
44 #define _COMPONENT		ACPI_PROCESSOR_COMPONENT
45 ACPI_MODULE_NAME("processor_driver");
46 
47 MODULE_AUTHOR("Paul Diefenbaugh");
48 MODULE_DESCRIPTION("ACPI Processor Driver");
49 MODULE_LICENSE("GPL");
50 
51 static int acpi_processor_start(struct device *dev);
52 static int acpi_processor_stop(struct device *dev);
53 
54 static const struct acpi_device_id processor_device_ids[] = {
55 	{ACPI_PROCESSOR_OBJECT_HID, 0},
56 	{ACPI_PROCESSOR_DEVICE_HID, 0},
57 	{"", 0},
58 };
59 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
60 
61 static struct device_driver acpi_processor_driver = {
62 	.name = "processor",
63 	.bus = &cpu_subsys,
64 	.acpi_match_table = processor_device_ids,
65 	.probe = acpi_processor_start,
66 	.remove = acpi_processor_stop,
67 };
68 
69 static void acpi_processor_notify(acpi_handle handle, u32 event, void *data)
70 {
71 	struct acpi_device *device = data;
72 	struct acpi_processor *pr;
73 	int saved;
74 
75 	if (device->handle != handle)
76 		return;
77 
78 	pr = acpi_driver_data(device);
79 	if (!pr)
80 		return;
81 
82 	switch (event) {
83 	case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
84 		saved = pr->performance_platform_limit;
85 		acpi_processor_ppc_has_changed(pr, 1);
86 		if (saved == pr->performance_platform_limit)
87 			break;
88 		acpi_bus_generate_netlink_event(device->pnp.device_class,
89 						  dev_name(&device->dev), event,
90 						  pr->performance_platform_limit);
91 		break;
92 	case ACPI_PROCESSOR_NOTIFY_POWER:
93 		acpi_processor_power_state_has_changed(pr);
94 		acpi_bus_generate_netlink_event(device->pnp.device_class,
95 						  dev_name(&device->dev), event, 0);
96 		break;
97 	case ACPI_PROCESSOR_NOTIFY_THROTTLING:
98 		acpi_processor_tstate_has_changed(pr);
99 		acpi_bus_generate_netlink_event(device->pnp.device_class,
100 						  dev_name(&device->dev), event, 0);
101 		break;
102 	default:
103 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
104 				  "Unsupported event [0x%x]\n", event));
105 		break;
106 	}
107 
108 	return;
109 }
110 
111 static int __acpi_processor_start(struct acpi_device *device);
112 
113 static int acpi_cpu_soft_notify(struct notifier_block *nfb,
114 					  unsigned long action, void *hcpu)
115 {
116 	unsigned int cpu = (unsigned long)hcpu;
117 	struct acpi_processor *pr = per_cpu(processors, cpu);
118 	struct acpi_device *device;
119 	action &= ~CPU_TASKS_FROZEN;
120 
121 	switch (action) {
122 	case CPU_ONLINE:
123 	case CPU_DEAD:
124 		break;
125 	default:
126 		return NOTIFY_DONE;
127 	}
128 
129 	if (!pr || acpi_bus_get_device(pr->handle, &device))
130 		return NOTIFY_DONE;
131 
132 	if (action == CPU_ONLINE) {
133 		/*
134 		 * CPU got physically hotplugged and onlined for the first time:
135 		 * Initialize missing things.
136 		 */
137 		if (pr->flags.need_hotplug_init) {
138 			int ret;
139 
140 			pr_info("Will online and init hotplugged CPU: %d\n",
141 				pr->id);
142 			pr->flags.need_hotplug_init = 0;
143 			ret = __acpi_processor_start(device);
144 			WARN(ret, "Failed to start CPU: %d\n", pr->id);
145 		} else {
146 			/* Normal CPU soft online event. */
147 			acpi_processor_ppc_has_changed(pr, 0);
148 			acpi_processor_hotplug(pr);
149 			acpi_processor_reevaluate_tstate(pr, action);
150 			acpi_processor_tstate_has_changed(pr);
151 		}
152 	} else if (action == CPU_DEAD) {
153 		/* Invalidate flag.throttling after the CPU is offline. */
154 		acpi_processor_reevaluate_tstate(pr, action);
155 	}
156 	return NOTIFY_OK;
157 }
158 
159 static struct notifier_block acpi_cpu_notifier = {
160 	    .notifier_call = acpi_cpu_soft_notify,
161 };
162 
163 #ifdef CONFIG_ACPI_CPU_FREQ_PSS
164 static int acpi_pss_perf_init(struct acpi_processor *pr,
165 		struct acpi_device *device)
166 {
167 	int result = 0;
168 
169 	acpi_processor_ppc_has_changed(pr, 0);
170 
171 	acpi_processor_get_throttling_info(pr);
172 
173 	if (pr->flags.throttling)
174 		pr->flags.limit = 1;
175 
176 	pr->cdev = thermal_cooling_device_register("Processor", device,
177 						   &processor_cooling_ops);
178 	if (IS_ERR(pr->cdev)) {
179 		result = PTR_ERR(pr->cdev);
180 		return result;
181 	}
182 
183 	dev_dbg(&device->dev, "registered as cooling_device%d\n",
184 		pr->cdev->id);
185 
186 	result = sysfs_create_link(&device->dev.kobj,
187 				   &pr->cdev->device.kobj,
188 				   "thermal_cooling");
189 	if (result) {
190 		dev_err(&device->dev,
191 			"Failed to create sysfs link 'thermal_cooling'\n");
192 		goto err_thermal_unregister;
193 	}
194 
195 	result = sysfs_create_link(&pr->cdev->device.kobj,
196 				   &device->dev.kobj,
197 				   "device");
198 	if (result) {
199 		dev_err(&pr->cdev->device,
200 			"Failed to create sysfs link 'device'\n");
201 		goto err_remove_sysfs_thermal;
202 	}
203 
204 	return 0;
205 
206  err_remove_sysfs_thermal:
207 	sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
208  err_thermal_unregister:
209 	thermal_cooling_device_unregister(pr->cdev);
210 
211 	return result;
212 }
213 
214 static void acpi_pss_perf_exit(struct acpi_processor *pr,
215 		struct acpi_device *device)
216 {
217 	if (pr->cdev) {
218 		sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
219 		sysfs_remove_link(&pr->cdev->device.kobj, "device");
220 		thermal_cooling_device_unregister(pr->cdev);
221 		pr->cdev = NULL;
222 	}
223 }
224 #else
225 static inline int acpi_pss_perf_init(struct acpi_processor *pr,
226 		struct acpi_device *device)
227 {
228 	return 0;
229 }
230 
231 static inline void acpi_pss_perf_exit(struct acpi_processor *pr,
232 		struct acpi_device *device) {}
233 #endif /* CONFIG_ACPI_CPU_FREQ_PSS */
234 
235 static int __acpi_processor_start(struct acpi_device *device)
236 {
237 	struct acpi_processor *pr = acpi_driver_data(device);
238 	acpi_status status;
239 	int result = 0;
240 
241 	if (!pr)
242 		return -ENODEV;
243 
244 	if (pr->flags.need_hotplug_init)
245 		return 0;
246 
247 	result = acpi_cppc_processor_probe(pr);
248 	if (result)
249 		return -ENODEV;
250 
251 	if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
252 		acpi_processor_power_init(pr);
253 
254 	result = acpi_pss_perf_init(pr, device);
255 	if (result)
256 		goto err_power_exit;
257 
258 	status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
259 					     acpi_processor_notify, device);
260 	if (ACPI_SUCCESS(status))
261 		return 0;
262 
263 err_power_exit:
264 	acpi_processor_power_exit(pr);
265 	return result;
266 }
267 
268 static int acpi_processor_start(struct device *dev)
269 {
270 	struct acpi_device *device = ACPI_COMPANION(dev);
271 
272 	if (!device)
273 		return -ENODEV;
274 
275 	return __acpi_processor_start(device);
276 }
277 
278 static int acpi_processor_stop(struct device *dev)
279 {
280 	struct acpi_device *device = ACPI_COMPANION(dev);
281 	struct acpi_processor *pr;
282 
283 	if (!device)
284 		return 0;
285 
286 	acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
287 				   acpi_processor_notify);
288 
289 	pr = acpi_driver_data(device);
290 	if (!pr)
291 		return 0;
292 	acpi_processor_power_exit(pr);
293 
294 	acpi_pss_perf_exit(pr, device);
295 
296 	acpi_cppc_processor_exit(pr);
297 
298 	return 0;
299 }
300 
301 /*
302  * We keep the driver loaded even when ACPI is not running.
303  * This is needed for the powernow-k8 driver, that works even without
304  * ACPI, but needs symbols from this driver
305  */
306 
307 static int __init acpi_processor_driver_init(void)
308 {
309 	int result = 0;
310 
311 	if (acpi_disabled)
312 		return 0;
313 
314 	result = driver_register(&acpi_processor_driver);
315 	if (result < 0)
316 		return result;
317 
318 	register_hotcpu_notifier(&acpi_cpu_notifier);
319 	acpi_thermal_cpufreq_init();
320 	acpi_processor_ppc_init();
321 	acpi_processor_throttling_init();
322 	return 0;
323 }
324 
325 static void __exit acpi_processor_driver_exit(void)
326 {
327 	if (acpi_disabled)
328 		return;
329 
330 	acpi_processor_ppc_exit();
331 	acpi_thermal_cpufreq_exit();
332 	unregister_hotcpu_notifier(&acpi_cpu_notifier);
333 	driver_unregister(&acpi_processor_driver);
334 }
335 
336 module_init(acpi_processor_driver_init);
337 module_exit(acpi_processor_driver_exit);
338 
339 MODULE_ALIAS("processor");
340