Merge branch 'pm-cpuidle'
[linux-2.6-block.git] / drivers / acpi / processor_driver.c
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  *  You should have received a copy of the GNU General Public License along
25  *  with this program; if not, write to the Free Software Foundation, Inc.,
26  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27  *
28  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29  */
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/cpufreq.h>
35 #include <linux/cpu.h>
36 #include <linux/cpuidle.h>
37 #include <linux/slab.h>
38 #include <linux/acpi.h>
39
40 #include <acpi/processor.h>
41
42 #include "internal.h"
43
44 #define PREFIX "ACPI: "
45
46 #define ACPI_PROCESSOR_FILE_INFO        "info"
47 #define ACPI_PROCESSOR_FILE_THROTTLING  "throttling"
48 #define ACPI_PROCESSOR_FILE_LIMIT       "limit"
49 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
50 #define ACPI_PROCESSOR_NOTIFY_POWER     0x81
51 #define ACPI_PROCESSOR_NOTIFY_THROTTLING        0x82
52
53 #define ACPI_PROCESSOR_LIMIT_USER       0
54 #define ACPI_PROCESSOR_LIMIT_THERMAL    1
55
56 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
57 ACPI_MODULE_NAME("processor_driver");
58
59 MODULE_AUTHOR("Paul Diefenbaugh");
60 MODULE_DESCRIPTION("ACPI Processor Driver");
61 MODULE_LICENSE("GPL");
62
63 static int acpi_processor_start(struct device *dev);
64 static int acpi_processor_stop(struct device *dev);
65
66 static const struct acpi_device_id processor_device_ids[] = {
67         {ACPI_PROCESSOR_OBJECT_HID, 0},
68         {ACPI_PROCESSOR_DEVICE_HID, 0},
69         {"", 0},
70 };
71 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
72
73 static struct device_driver acpi_processor_driver = {
74         .name = "processor",
75         .bus = &cpu_subsys,
76         .acpi_match_table = processor_device_ids,
77         .probe = acpi_processor_start,
78         .remove = acpi_processor_stop,
79 };
80
81 static void acpi_processor_notify(acpi_handle handle, u32 event, void *data)
82 {
83         struct acpi_device *device = data;
84         struct acpi_processor *pr;
85         int saved;
86
87         if (device->handle != handle)
88                 return;
89
90         pr = acpi_driver_data(device);
91         if (!pr)
92                 return;
93
94         switch (event) {
95         case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
96                 saved = pr->performance_platform_limit;
97                 acpi_processor_ppc_has_changed(pr, 1);
98                 if (saved == pr->performance_platform_limit)
99                         break;
100                 acpi_bus_generate_proc_event(device, event,
101                                         pr->performance_platform_limit);
102                 acpi_bus_generate_netlink_event(device->pnp.device_class,
103                                                   dev_name(&device->dev), event,
104                                                   pr->performance_platform_limit);
105                 break;
106         case ACPI_PROCESSOR_NOTIFY_POWER:
107                 acpi_processor_cst_has_changed(pr);
108                 acpi_bus_generate_proc_event(device, event, 0);
109                 acpi_bus_generate_netlink_event(device->pnp.device_class,
110                                                   dev_name(&device->dev), event, 0);
111                 break;
112         case ACPI_PROCESSOR_NOTIFY_THROTTLING:
113                 acpi_processor_tstate_has_changed(pr);
114                 acpi_bus_generate_proc_event(device, event, 0);
115                 acpi_bus_generate_netlink_event(device->pnp.device_class,
116                                                   dev_name(&device->dev), event, 0);
117                 break;
118         default:
119                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
120                                   "Unsupported event [0x%x]\n", event));
121                 break;
122         }
123
124         return;
125 }
126
127 static __cpuinit int __acpi_processor_start(struct acpi_device *device);
128
129 static int __cpuinit acpi_cpu_soft_notify(struct notifier_block *nfb,
130                                           unsigned long action, void *hcpu)
131 {
132         unsigned int cpu = (unsigned long)hcpu;
133         struct acpi_processor *pr = per_cpu(processors, cpu);
134         struct acpi_device *device;
135
136         if (!pr || acpi_bus_get_device(pr->handle, &device))
137                 return NOTIFY_DONE;
138
139         if (action == CPU_ONLINE) {
140                 /*
141                  * CPU got physically hotplugged and onlined for the first time:
142                  * Initialize missing things.
143                  */
144                 if (pr->flags.need_hotplug_init) {
145                         int ret;
146
147                         pr_info("Will online and init hotplugged CPU: %d\n",
148                                 pr->id);
149                         pr->flags.need_hotplug_init = 0;
150                         ret = __acpi_processor_start(device);
151                         WARN(ret, "Failed to start CPU: %d\n", pr->id);
152                 } else {
153                         /* Normal CPU soft online event. */
154                         acpi_processor_ppc_has_changed(pr, 0);
155                         acpi_processor_hotplug(pr);
156                         acpi_processor_reevaluate_tstate(pr, action);
157                         acpi_processor_tstate_has_changed(pr);
158                 }
159         } else if (action == CPU_DEAD) {
160                 /* Invalidate flag.throttling after the CPU is offline. */
161                 acpi_processor_reevaluate_tstate(pr, action);
162         }
163         return NOTIFY_OK;
164 }
165
166 static struct notifier_block __refdata acpi_cpu_notifier =
167 {
168             .notifier_call = acpi_cpu_soft_notify,
169 };
170
171 static __cpuinit int __acpi_processor_start(struct acpi_device *device)
172 {
173         struct acpi_processor *pr = acpi_driver_data(device);
174         acpi_status status;
175         int result = 0;
176
177         if (!pr)
178                 return -ENODEV;
179
180         if (pr->flags.need_hotplug_init)
181                 return 0;
182
183 #ifdef CONFIG_CPU_FREQ
184         acpi_processor_ppc_has_changed(pr, 0);
185         acpi_processor_load_module(pr);
186 #endif
187         acpi_processor_get_throttling_info(pr);
188         acpi_processor_get_limit_info(pr);
189
190         if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
191                 acpi_processor_power_init(pr);
192
193         pr->cdev = thermal_cooling_device_register("Processor", device,
194                                                    &processor_cooling_ops);
195         if (IS_ERR(pr->cdev)) {
196                 result = PTR_ERR(pr->cdev);
197                 goto err_power_exit;
198         }
199
200         dev_dbg(&device->dev, "registered as cooling_device%d\n",
201                 pr->cdev->id);
202
203         result = sysfs_create_link(&device->dev.kobj,
204                                    &pr->cdev->device.kobj,
205                                    "thermal_cooling");
206         if (result) {
207                 dev_err(&device->dev,
208                         "Failed to create sysfs link 'thermal_cooling'\n");
209                 goto err_thermal_unregister;
210         }
211         result = sysfs_create_link(&pr->cdev->device.kobj,
212                                    &device->dev.kobj,
213                                    "device");
214         if (result) {
215                 dev_err(&pr->cdev->device,
216                         "Failed to create sysfs link 'device'\n");
217                 goto err_remove_sysfs_thermal;
218         }
219
220         status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
221                                              acpi_processor_notify, device);
222         if (ACPI_SUCCESS(status))
223                 return 0;
224
225         sysfs_remove_link(&pr->cdev->device.kobj, "device");
226  err_remove_sysfs_thermal:
227         sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
228  err_thermal_unregister:
229         thermal_cooling_device_unregister(pr->cdev);
230  err_power_exit:
231         acpi_processor_power_exit(pr);
232         return result;
233 }
234
235 static int __cpuinit acpi_processor_start(struct device *dev)
236 {
237         struct acpi_device *device;
238
239         if (acpi_bus_get_device(ACPI_HANDLE(dev), &device))
240                 return -ENODEV;
241
242         return __acpi_processor_start(device);
243 }
244
245 static int acpi_processor_stop(struct device *dev)
246 {
247         struct acpi_device *device;
248         struct acpi_processor *pr;
249
250         if (acpi_bus_get_device(ACPI_HANDLE(dev), &device))
251                 return 0;
252
253         acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
254                                    acpi_processor_notify);
255
256         pr = acpi_driver_data(device);
257         if (!pr)
258                 return 0;
259
260         acpi_processor_power_exit(pr);
261
262         if (pr->cdev) {
263                 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
264                 sysfs_remove_link(&pr->cdev->device.kobj, "device");
265                 thermal_cooling_device_unregister(pr->cdev);
266                 pr->cdev = NULL;
267         }
268         return 0;
269 }
270
271 /*
272  * We keep the driver loaded even when ACPI is not running.
273  * This is needed for the powernow-k8 driver, that works even without
274  * ACPI, but needs symbols from this driver
275  */
276
277 static int __init acpi_processor_driver_init(void)
278 {
279         int result = 0;
280
281         if (acpi_disabled)
282                 return 0;
283
284         result = driver_register(&acpi_processor_driver);
285         if (result < 0)
286                 return result;
287
288         acpi_processor_syscore_init();
289         register_hotcpu_notifier(&acpi_cpu_notifier);
290         acpi_thermal_cpufreq_init();
291         acpi_processor_ppc_init();
292         acpi_processor_throttling_init();
293         return 0;
294 }
295
296 static void __exit acpi_processor_driver_exit(void)
297 {
298         if (acpi_disabled)
299                 return;
300
301         acpi_processor_ppc_exit();
302         acpi_thermal_cpufreq_exit();
303         unregister_hotcpu_notifier(&acpi_cpu_notifier);
304         acpi_processor_syscore_exit();
305         driver_unregister(&acpi_processor_driver);
306 }
307
308 module_init(acpi_processor_driver_init);
309 module_exit(acpi_processor_driver_exit);
310
311 MODULE_ALIAS("processor");