processor_driver.c 7.9 KB

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