processor_driver.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * processor_driver.c - ACPI Processor Driver
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <[email protected]>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>
  6. * Copyright (C) 2004 Dominik Brodowski <[email protected]>
  7. * Copyright (C) 2004 Anil S Keshavamurthy <[email protected]>
  8. * - Added processor hotplug support
  9. * Copyright (C) 2013, Intel Corporation
  10. * Rafael J. Wysocki <[email protected]>
  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_soft_cpu_online(unsigned int cpu)
  98. {
  99. struct acpi_processor *pr = per_cpu(processors, cpu);
  100. struct acpi_device *device;
  101. if (!pr || acpi_bus_get_device(pr->handle, &device))
  102. return 0;
  103. /*
  104. * CPU got physically hotplugged and onlined for the first time:
  105. * Initialize missing things.
  106. */
  107. if (pr->flags.need_hotplug_init) {
  108. int ret;
  109. pr_info("Will online and init hotplugged CPU: %d\n",
  110. pr->id);
  111. pr->flags.need_hotplug_init = 0;
  112. ret = __acpi_processor_start(device);
  113. WARN(ret, "Failed to start CPU: %d\n", pr->id);
  114. } else {
  115. /* Normal CPU soft online event. */
  116. acpi_processor_ppc_has_changed(pr, 0);
  117. acpi_processor_hotplug(pr);
  118. acpi_processor_reevaluate_tstate(pr, false);
  119. acpi_processor_tstate_has_changed(pr);
  120. }
  121. return 0;
  122. }
  123. static int acpi_soft_cpu_dead(unsigned int cpu)
  124. {
  125. struct acpi_processor *pr = per_cpu(processors, cpu);
  126. struct acpi_device *device;
  127. if (!pr || acpi_bus_get_device(pr->handle, &device))
  128. return 0;
  129. acpi_processor_reevaluate_tstate(pr, true);
  130. return 0;
  131. }
  132. #ifdef CONFIG_ACPI_CPU_FREQ_PSS
  133. static int acpi_pss_perf_init(struct acpi_processor *pr,
  134. struct acpi_device *device)
  135. {
  136. int result = 0;
  137. acpi_processor_ppc_has_changed(pr, 0);
  138. acpi_processor_get_throttling_info(pr);
  139. if (pr->flags.throttling)
  140. pr->flags.limit = 1;
  141. pr->cdev = thermal_cooling_device_register("Processor", device,
  142. &processor_cooling_ops);
  143. if (IS_ERR(pr->cdev)) {
  144. result = PTR_ERR(pr->cdev);
  145. return result;
  146. }
  147. dev_dbg(&device->dev, "registered as cooling_device%d\n",
  148. pr->cdev->id);
  149. result = sysfs_create_link(&device->dev.kobj,
  150. &pr->cdev->device.kobj,
  151. "thermal_cooling");
  152. if (result) {
  153. dev_err(&device->dev,
  154. "Failed to create sysfs link 'thermal_cooling'\n");
  155. goto err_thermal_unregister;
  156. }
  157. result = sysfs_create_link(&pr->cdev->device.kobj,
  158. &device->dev.kobj,
  159. "device");
  160. if (result) {
  161. dev_err(&pr->cdev->device,
  162. "Failed to create sysfs link 'device'\n");
  163. goto err_remove_sysfs_thermal;
  164. }
  165. return 0;
  166. err_remove_sysfs_thermal:
  167. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  168. err_thermal_unregister:
  169. thermal_cooling_device_unregister(pr->cdev);
  170. return result;
  171. }
  172. static void acpi_pss_perf_exit(struct acpi_processor *pr,
  173. struct acpi_device *device)
  174. {
  175. if (pr->cdev) {
  176. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  177. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  178. thermal_cooling_device_unregister(pr->cdev);
  179. pr->cdev = NULL;
  180. }
  181. }
  182. #else
  183. static inline int acpi_pss_perf_init(struct acpi_processor *pr,
  184. struct acpi_device *device)
  185. {
  186. return 0;
  187. }
  188. static inline void acpi_pss_perf_exit(struct acpi_processor *pr,
  189. struct acpi_device *device) {}
  190. #endif /* CONFIG_ACPI_CPU_FREQ_PSS */
  191. static int __acpi_processor_start(struct acpi_device *device)
  192. {
  193. struct acpi_processor *pr = acpi_driver_data(device);
  194. acpi_status status;
  195. int result = 0;
  196. if (!pr)
  197. return -ENODEV;
  198. if (pr->flags.need_hotplug_init)
  199. return 0;
  200. result = acpi_cppc_processor_probe(pr);
  201. if (result && !IS_ENABLED(CONFIG_ACPI_CPU_FREQ_PSS))
  202. dev_warn(&device->dev, "CPPC data invalid or not present\n");
  203. if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
  204. acpi_processor_power_init(pr);
  205. result = acpi_pss_perf_init(pr, device);
  206. if (result)
  207. goto err_power_exit;
  208. status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  209. acpi_processor_notify, device);
  210. if (ACPI_SUCCESS(status))
  211. return 0;
  212. result = -ENODEV;
  213. acpi_pss_perf_exit(pr, device);
  214. err_power_exit:
  215. acpi_processor_power_exit(pr);
  216. return result;
  217. }
  218. static int acpi_processor_start(struct device *dev)
  219. {
  220. struct acpi_device *device = ACPI_COMPANION(dev);
  221. int ret;
  222. if (!device)
  223. return -ENODEV;
  224. /* Protect against concurrent CPU hotplug operations */
  225. get_online_cpus();
  226. ret = __acpi_processor_start(device);
  227. put_online_cpus();
  228. return ret;
  229. }
  230. static int acpi_processor_stop(struct device *dev)
  231. {
  232. struct acpi_device *device = ACPI_COMPANION(dev);
  233. struct acpi_processor *pr;
  234. if (!device)
  235. return 0;
  236. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  237. acpi_processor_notify);
  238. pr = acpi_driver_data(device);
  239. if (!pr)
  240. return 0;
  241. acpi_processor_power_exit(pr);
  242. acpi_pss_perf_exit(pr, device);
  243. acpi_cppc_processor_exit(pr);
  244. return 0;
  245. }
  246. /*
  247. * We keep the driver loaded even when ACPI is not running.
  248. * This is needed for the powernow-k8 driver, that works even without
  249. * ACPI, but needs symbols from this driver
  250. */
  251. static enum cpuhp_state hp_online;
  252. static int __init acpi_processor_driver_init(void)
  253. {
  254. int result = 0;
  255. if (acpi_disabled)
  256. return 0;
  257. result = driver_register(&acpi_processor_driver);
  258. if (result < 0)
  259. return result;
  260. result = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  261. "acpi/cpu-drv:online",
  262. acpi_soft_cpu_online, NULL);
  263. if (result < 0)
  264. goto err;
  265. hp_online = result;
  266. cpuhp_setup_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD, "acpi/cpu-drv:dead",
  267. NULL, acpi_soft_cpu_dead);
  268. acpi_thermal_cpufreq_init();
  269. acpi_processor_ppc_init();
  270. acpi_processor_throttling_init();
  271. return 0;
  272. err:
  273. driver_unregister(&acpi_processor_driver);
  274. return result;
  275. }
  276. static void __exit acpi_processor_driver_exit(void)
  277. {
  278. if (acpi_disabled)
  279. return;
  280. acpi_processor_ppc_exit();
  281. acpi_thermal_cpufreq_exit();
  282. cpuhp_remove_state_nocalls(hp_online);
  283. cpuhp_remove_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD);
  284. driver_unregister(&acpi_processor_driver);
  285. }
  286. module_init(acpi_processor_driver_init);
  287. module_exit(acpi_processor_driver_exit);
  288. MODULE_ALIAS("processor");