processor_driver.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * processor_driver.c - ACPI Processor Driver
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
  8. * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  9. * - Added processor hotplug support
  10. * Copyright (C) 2013, Intel Corporation
  11. * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/cpu.h>
  18. #include <linux/cpuidle.h>
  19. #include <linux/slab.h>
  20. #include <linux/acpi.h>
  21. #include <acpi/processor.h>
  22. #include "internal.h"
  23. #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
  24. #define ACPI_PROCESSOR_NOTIFY_POWER 0x81
  25. #define ACPI_PROCESSOR_NOTIFY_THROTTLING 0x82
  26. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  27. ACPI_MODULE_NAME("processor_driver");
  28. MODULE_AUTHOR("Paul Diefenbaugh");
  29. MODULE_DESCRIPTION("ACPI Processor Driver");
  30. MODULE_LICENSE("GPL");
  31. static int acpi_processor_start(struct device *dev);
  32. static int acpi_processor_stop(struct device *dev);
  33. static const struct acpi_device_id processor_device_ids[] = {
  34. {ACPI_PROCESSOR_OBJECT_HID, 0},
  35. {ACPI_PROCESSOR_DEVICE_HID, 0},
  36. {"", 0},
  37. };
  38. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  39. static struct device_driver acpi_processor_driver = {
  40. .name = "processor",
  41. .bus = &cpu_subsys,
  42. .acpi_match_table = processor_device_ids,
  43. .probe = acpi_processor_start,
  44. .remove = acpi_processor_stop,
  45. };
  46. static void acpi_processor_notify(acpi_handle handle, u32 event, void *data)
  47. {
  48. struct acpi_device *device = data;
  49. struct acpi_processor *pr;
  50. int saved;
  51. if (device->handle != handle)
  52. return;
  53. pr = acpi_driver_data(device);
  54. if (!pr)
  55. return;
  56. switch (event) {
  57. case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
  58. saved = pr->performance_platform_limit;
  59. acpi_processor_ppc_has_changed(pr, 1);
  60. if (saved == pr->performance_platform_limit)
  61. break;
  62. acpi_bus_generate_netlink_event(device->pnp.device_class,
  63. dev_name(&device->dev), event,
  64. pr->performance_platform_limit);
  65. break;
  66. case ACPI_PROCESSOR_NOTIFY_POWER:
  67. acpi_processor_power_state_has_changed(pr);
  68. acpi_bus_generate_netlink_event(device->pnp.device_class,
  69. dev_name(&device->dev), event, 0);
  70. break;
  71. case ACPI_PROCESSOR_NOTIFY_THROTTLING:
  72. acpi_processor_tstate_has_changed(pr);
  73. acpi_bus_generate_netlink_event(device->pnp.device_class,
  74. dev_name(&device->dev), event, 0);
  75. break;
  76. default:
  77. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  78. "Unsupported event [0x%x]\n", event));
  79. break;
  80. }
  81. return;
  82. }
  83. static int __acpi_processor_start(struct acpi_device *device);
  84. static int acpi_soft_cpu_online(unsigned int cpu)
  85. {
  86. struct acpi_processor *pr = per_cpu(processors, cpu);
  87. struct acpi_device *device;
  88. if (!pr || acpi_bus_get_device(pr->handle, &device))
  89. return 0;
  90. /*
  91. * CPU got physically hotplugged and onlined for the first time:
  92. * Initialize missing things.
  93. */
  94. if (pr->flags.need_hotplug_init) {
  95. int ret;
  96. pr_info("Will online and init hotplugged CPU: %d\n",
  97. pr->id);
  98. pr->flags.need_hotplug_init = 0;
  99. ret = __acpi_processor_start(device);
  100. WARN(ret, "Failed to start CPU: %d\n", pr->id);
  101. } else {
  102. /* Normal CPU soft online event. */
  103. acpi_processor_ppc_has_changed(pr, 0);
  104. acpi_processor_hotplug(pr);
  105. acpi_processor_reevaluate_tstate(pr, false);
  106. acpi_processor_tstate_has_changed(pr);
  107. }
  108. return 0;
  109. }
  110. static int acpi_soft_cpu_dead(unsigned int cpu)
  111. {
  112. struct acpi_processor *pr = per_cpu(processors, cpu);
  113. struct acpi_device *device;
  114. if (!pr || acpi_bus_get_device(pr->handle, &device))
  115. return 0;
  116. acpi_processor_reevaluate_tstate(pr, true);
  117. return 0;
  118. }
  119. #ifdef CONFIG_ACPI_CPU_FREQ_PSS
  120. static int acpi_pss_perf_init(struct acpi_processor *pr,
  121. struct acpi_device *device)
  122. {
  123. int result = 0;
  124. acpi_processor_ppc_has_changed(pr, 0);
  125. acpi_processor_get_throttling_info(pr);
  126. if (pr->flags.throttling)
  127. pr->flags.limit = 1;
  128. pr->cdev = thermal_cooling_device_register("Processor", device,
  129. &processor_cooling_ops);
  130. if (IS_ERR(pr->cdev)) {
  131. result = PTR_ERR(pr->cdev);
  132. return result;
  133. }
  134. dev_dbg(&device->dev, "registered as cooling_device%d\n",
  135. pr->cdev->id);
  136. result = sysfs_create_link(&device->dev.kobj,
  137. &pr->cdev->device.kobj,
  138. "thermal_cooling");
  139. if (result) {
  140. dev_err(&device->dev,
  141. "Failed to create sysfs link 'thermal_cooling'\n");
  142. goto err_thermal_unregister;
  143. }
  144. result = sysfs_create_link(&pr->cdev->device.kobj,
  145. &device->dev.kobj,
  146. "device");
  147. if (result) {
  148. dev_err(&pr->cdev->device,
  149. "Failed to create sysfs link 'device'\n");
  150. goto err_remove_sysfs_thermal;
  151. }
  152. return 0;
  153. err_remove_sysfs_thermal:
  154. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  155. err_thermal_unregister:
  156. thermal_cooling_device_unregister(pr->cdev);
  157. return result;
  158. }
  159. static void acpi_pss_perf_exit(struct acpi_processor *pr,
  160. struct acpi_device *device)
  161. {
  162. if (pr->cdev) {
  163. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  164. sysfs_remove_link(&pr->cdev->device.kobj, "device");
  165. thermal_cooling_device_unregister(pr->cdev);
  166. pr->cdev = NULL;
  167. }
  168. }
  169. #else
  170. static inline int acpi_pss_perf_init(struct acpi_processor *pr,
  171. struct acpi_device *device)
  172. {
  173. return 0;
  174. }
  175. static inline void acpi_pss_perf_exit(struct acpi_processor *pr,
  176. struct acpi_device *device) {}
  177. #endif /* CONFIG_ACPI_CPU_FREQ_PSS */
  178. static int __acpi_processor_start(struct acpi_device *device)
  179. {
  180. struct acpi_processor *pr = acpi_driver_data(device);
  181. acpi_status status;
  182. int result = 0;
  183. if (!pr)
  184. return -ENODEV;
  185. if (pr->flags.need_hotplug_init)
  186. return 0;
  187. result = acpi_cppc_processor_probe(pr);
  188. if (result && !IS_ENABLED(CONFIG_ACPI_CPU_FREQ_PSS))
  189. dev_dbg(&device->dev, "CPPC data invalid or not present\n");
  190. if (!cpuidle_get_driver() || cpuidle_get_driver() == &acpi_idle_driver)
  191. acpi_processor_power_init(pr);
  192. result = acpi_pss_perf_init(pr, device);
  193. if (result)
  194. goto err_power_exit;
  195. status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  196. acpi_processor_notify, device);
  197. if (ACPI_SUCCESS(status))
  198. return 0;
  199. result = -ENODEV;
  200. acpi_pss_perf_exit(pr, device);
  201. err_power_exit:
  202. acpi_processor_power_exit(pr);
  203. return result;
  204. }
  205. static int acpi_processor_start(struct device *dev)
  206. {
  207. struct acpi_device *device = ACPI_COMPANION(dev);
  208. int ret;
  209. if (!device)
  210. return -ENODEV;
  211. /* Protect against concurrent CPU hotplug operations */
  212. cpu_hotplug_disable();
  213. ret = __acpi_processor_start(device);
  214. cpu_hotplug_enable();
  215. return ret;
  216. }
  217. static int acpi_processor_stop(struct device *dev)
  218. {
  219. struct acpi_device *device = ACPI_COMPANION(dev);
  220. struct acpi_processor *pr;
  221. if (!device)
  222. return 0;
  223. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  224. acpi_processor_notify);
  225. pr = acpi_driver_data(device);
  226. if (!pr)
  227. return 0;
  228. acpi_processor_power_exit(pr);
  229. acpi_pss_perf_exit(pr, device);
  230. acpi_cppc_processor_exit(pr);
  231. return 0;
  232. }
  233. bool acpi_processor_cpufreq_init;
  234. static int acpi_processor_notifier(struct notifier_block *nb,
  235. unsigned long event, void *data)
  236. {
  237. struct cpufreq_policy *policy = data;
  238. if (event == CPUFREQ_CREATE_POLICY) {
  239. acpi_thermal_cpufreq_init(policy);
  240. acpi_processor_ppc_init(policy);
  241. } else if (event == CPUFREQ_REMOVE_POLICY) {
  242. acpi_processor_ppc_exit(policy);
  243. acpi_thermal_cpufreq_exit(policy);
  244. }
  245. return 0;
  246. }
  247. static struct notifier_block acpi_processor_notifier_block = {
  248. .notifier_call = acpi_processor_notifier,
  249. };
  250. /*
  251. * We keep the driver loaded even when ACPI is not running.
  252. * This is needed for the powernow-k8 driver, that works even without
  253. * ACPI, but needs symbols from this driver
  254. */
  255. static enum cpuhp_state hp_online;
  256. static int __init acpi_processor_driver_init(void)
  257. {
  258. int result = 0;
  259. if (acpi_disabled)
  260. return 0;
  261. result = driver_register(&acpi_processor_driver);
  262. if (result < 0)
  263. return result;
  264. result = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  265. "acpi/cpu-drv:online",
  266. acpi_soft_cpu_online, NULL);
  267. if (result < 0)
  268. goto err;
  269. hp_online = result;
  270. cpuhp_setup_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD, "acpi/cpu-drv:dead",
  271. NULL, acpi_soft_cpu_dead);
  272. if (!cpufreq_register_notifier(&acpi_processor_notifier_block,
  273. CPUFREQ_POLICY_NOTIFIER)) {
  274. acpi_processor_cpufreq_init = true;
  275. acpi_processor_ignore_ppc_init();
  276. }
  277. acpi_processor_throttling_init();
  278. return 0;
  279. err:
  280. driver_unregister(&acpi_processor_driver);
  281. return result;
  282. }
  283. static void __exit acpi_processor_driver_exit(void)
  284. {
  285. if (acpi_disabled)
  286. return;
  287. if (acpi_processor_cpufreq_init) {
  288. cpufreq_unregister_notifier(&acpi_processor_notifier_block,
  289. CPUFREQ_POLICY_NOTIFIER);
  290. acpi_processor_cpufreq_init = false;
  291. }
  292. cpuhp_remove_state_nocalls(hp_online);
  293. cpuhp_remove_state_nocalls(CPUHP_ACPI_CPUDRV_DEAD);
  294. driver_unregister(&acpi_processor_driver);
  295. }
  296. module_init(acpi_processor_driver_init);
  297. module_exit(acpi_processor_driver_exit);
  298. MODULE_ALIAS("processor");