processor_driver.c 8.4 KB

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