processor_driver.c 8.7 KB

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