processor_driver.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. /*
  108. * CPU_STARTING and CPU_DYING must not sleep. Return here since
  109. * acpi_bus_get_device() may sleep.
  110. */
  111. if (action == CPU_STARTING || action == CPU_DYING)
  112. return NOTIFY_DONE;
  113. if (!pr || acpi_bus_get_device(pr->handle, &device))
  114. return NOTIFY_DONE;
  115. if (action == CPU_ONLINE) {
  116. /*
  117. * CPU got physically hotplugged and onlined for the first time:
  118. * Initialize missing things.
  119. */
  120. if (pr->flags.need_hotplug_init) {
  121. int ret;
  122. pr_info("Will online and init hotplugged CPU: %d\n",
  123. pr->id);
  124. pr->flags.need_hotplug_init = 0;
  125. ret = __acpi_processor_start(device);
  126. WARN(ret, "Failed to start CPU: %d\n", pr->id);
  127. } else {
  128. /* Normal CPU soft online event. */
  129. acpi_processor_ppc_has_changed(pr, 0);
  130. acpi_processor_hotplug(pr);
  131. acpi_processor_reevaluate_tstate(pr, action);
  132. acpi_processor_tstate_has_changed(pr);
  133. }
  134. } else if (action == CPU_DEAD) {
  135. /* Invalidate flag.throttling after the CPU is offline. */
  136. acpi_processor_reevaluate_tstate(pr, action);
  137. }
  138. return NOTIFY_OK;
  139. }
  140. static struct notifier_block __refdata acpi_cpu_notifier = {
  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. #endif
  155. acpi_processor_get_throttling_info(pr);
  156. if (pr->flags.throttling)
  157. pr->flags.limit = 1;
  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 = ACPI_COMPANION(dev);
  200. if (!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 = ACPI_COMPANION(dev);
  207. struct acpi_processor *pr;
  208. if (!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");