processor_driver.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. action &= ~CPU_TASKS_FROZEN;
  108. /*
  109. * CPU_STARTING and CPU_DYING must not sleep. Return here since
  110. * acpi_bus_get_device() may sleep.
  111. */
  112. if (action == CPU_STARTING || action == CPU_DYING)
  113. return NOTIFY_DONE;
  114. if (!pr || acpi_bus_get_device(pr->handle, &device))
  115. return NOTIFY_DONE;
  116. if (action == CPU_ONLINE) {
  117. /*
  118. * CPU got physically hotplugged and onlined for the first time:
  119. * Initialize missing things.
  120. */
  121. if (pr->flags.need_hotplug_init) {
  122. int ret;
  123. pr_info("Will online and init hotplugged CPU: %d\n",
  124. pr->id);
  125. pr->flags.need_hotplug_init = 0;
  126. ret = __acpi_processor_start(device);
  127. WARN(ret, "Failed to start CPU: %d\n", pr->id);
  128. } else {
  129. /* Normal CPU soft online event. */
  130. acpi_processor_ppc_has_changed(pr, 0);
  131. acpi_processor_hotplug(pr);
  132. acpi_processor_reevaluate_tstate(pr, action);
  133. acpi_processor_tstate_has_changed(pr);
  134. }
  135. } else if (action == CPU_DEAD) {
  136. /* Invalidate flag.throttling after the CPU is offline. */
  137. acpi_processor_reevaluate_tstate(pr, action);
  138. }
  139. return NOTIFY_OK;
  140. }
  141. static struct notifier_block __refdata acpi_cpu_notifier = {
  142. .notifier_call = acpi_cpu_soft_notify,
  143. };
  144. static int __acpi_processor_start(struct acpi_device *device)
  145. {
  146. struct acpi_processor *pr = acpi_driver_data(device);
  147. acpi_status status;
  148. int result = 0;
  149. if (!pr)
  150. return -ENODEV;
  151. if (pr->flags.need_hotplug_init)
  152. return 0;
  153. #ifdef CONFIG_CPU_FREQ
  154. acpi_processor_ppc_has_changed(pr, 0);
  155. #endif
  156. acpi_processor_get_throttling_info(pr);
  157. if (pr->flags.throttling)
  158. pr->flags.limit = 1;
  159. if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
  160. acpi_processor_power_init(pr);
  161. pr->cdev = thermal_cooling_device_register("Processor", device,
  162. &processor_cooling_ops);
  163. if (IS_ERR(pr->cdev)) {
  164. result = PTR_ERR(pr->cdev);
  165. goto err_power_exit;
  166. }
  167. dev_dbg(&device->dev, "registered as cooling_device%d\n",
  168. pr->cdev->id);
  169. result = sysfs_create_link(&device->dev.kobj,
  170. &pr->cdev->device.kobj,
  171. "thermal_cooling");
  172. if (result) {
  173. dev_err(&device->dev,
  174. "Failed to create sysfs link 'thermal_cooling'\n");
  175. goto err_thermal_unregister;
  176. }
  177. result = sysfs_create_link(&pr->cdev->device.kobj,
  178. &device->dev.kobj,
  179. "device");
  180. if (result) {
  181. dev_err(&pr->cdev->device,
  182. "Failed to create sysfs link 'device'\n");
  183. goto err_remove_sysfs_thermal;
  184. }
  185. status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  186. acpi_processor_notify, device);
  187. if (ACPI_SUCCESS(status))
  188. return 0;
  189. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  190. err_remove_sysfs_thermal:
  191. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  192. err_thermal_unregister:
  193. thermal_cooling_device_unregister(pr->cdev);
  194. err_power_exit:
  195. acpi_processor_power_exit(pr);
  196. return result;
  197. }
  198. static int acpi_processor_start(struct device *dev)
  199. {
  200. struct acpi_device *device = ACPI_COMPANION(dev);
  201. if (!device)
  202. return -ENODEV;
  203. return __acpi_processor_start(device);
  204. }
  205. static int acpi_processor_stop(struct device *dev)
  206. {
  207. struct acpi_device *device = ACPI_COMPANION(dev);
  208. struct acpi_processor *pr;
  209. if (!device)
  210. return 0;
  211. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  212. acpi_processor_notify);
  213. pr = acpi_driver_data(device);
  214. if (!pr)
  215. return 0;
  216. acpi_processor_power_exit(pr);
  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. return 0;
  224. }
  225. /*
  226. * We keep the driver loaded even when ACPI is not running.
  227. * This is needed for the powernow-k8 driver, that works even without
  228. * ACPI, but needs symbols from this driver
  229. */
  230. static int __init acpi_processor_driver_init(void)
  231. {
  232. int result = 0;
  233. if (acpi_disabled)
  234. return 0;
  235. result = driver_register(&acpi_processor_driver);
  236. if (result < 0)
  237. return result;
  238. acpi_processor_syscore_init();
  239. register_hotcpu_notifier(&acpi_cpu_notifier);
  240. acpi_thermal_cpufreq_init();
  241. acpi_processor_ppc_init();
  242. acpi_processor_throttling_init();
  243. return 0;
  244. }
  245. static void __exit acpi_processor_driver_exit(void)
  246. {
  247. if (acpi_disabled)
  248. return;
  249. acpi_processor_ppc_exit();
  250. acpi_thermal_cpufreq_exit();
  251. unregister_hotcpu_notifier(&acpi_cpu_notifier);
  252. acpi_processor_syscore_exit();
  253. driver_unregister(&acpi_processor_driver);
  254. }
  255. module_init(acpi_processor_driver_init);
  256. module_exit(acpi_processor_driver_exit);
  257. MODULE_ALIAS("processor");