processor_driver.c 8.2 KB

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