acpi_pad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * acpi_pad.c ACPI Processor Aggregator Driver
  4. *
  5. * Copyright (c) 2009, Intel Corporation.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/cpumask.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/kthread.h>
  13. #include <uapi/linux/sched/types.h>
  14. #include <linux/freezer.h>
  15. #include <linux/cpu.h>
  16. #include <linux/tick.h>
  17. #include <linux/slab.h>
  18. #include <linux/acpi.h>
  19. #include <linux/perf_event.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/cpuid/api.h>
  22. #include <asm/mwait.h>
  23. #include <xen/xen.h>
  24. #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
  25. #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
  26. #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
  27. #define ACPI_PROCESSOR_AGGREGATOR_STATUS_SUCCESS 0
  28. #define ACPI_PROCESSOR_AGGREGATOR_STATUS_NO_ACTION 1
  29. static DEFINE_MUTEX(isolated_cpus_lock);
  30. static DEFINE_MUTEX(round_robin_lock);
  31. static unsigned int power_saving_mwait_eax;
  32. static unsigned char tsc_detected_unstable;
  33. static unsigned char tsc_marked_unstable;
  34. static void power_saving_mwait_init(void)
  35. {
  36. unsigned int eax, ebx, ecx, edx;
  37. unsigned int highest_cstate = 0;
  38. unsigned int highest_subcstate = 0;
  39. int i;
  40. if (!boot_cpu_has(X86_FEATURE_MWAIT))
  41. return;
  42. cpuid(CPUID_LEAF_MWAIT, &eax, &ebx, &ecx, &edx);
  43. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  44. !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
  45. return;
  46. edx >>= MWAIT_SUBSTATE_SIZE;
  47. for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
  48. if (edx & MWAIT_SUBSTATE_MASK) {
  49. highest_cstate = i;
  50. highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
  51. }
  52. }
  53. power_saving_mwait_eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
  54. (highest_subcstate - 1);
  55. #if defined(CONFIG_X86)
  56. switch (boot_cpu_data.x86_vendor) {
  57. case X86_VENDOR_HYGON:
  58. case X86_VENDOR_AMD:
  59. case X86_VENDOR_INTEL:
  60. case X86_VENDOR_ZHAOXIN:
  61. case X86_VENDOR_CENTAUR:
  62. /*
  63. * AMD Fam10h TSC will tick in all
  64. * C/P/S0/S1 states when this bit is set.
  65. */
  66. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  67. tsc_detected_unstable = 1;
  68. break;
  69. default:
  70. /* TSC could halt in idle */
  71. tsc_detected_unstable = 1;
  72. }
  73. #endif
  74. }
  75. static unsigned long cpu_weight[NR_CPUS];
  76. static int tsk_in_cpu[NR_CPUS] = {[0 ... NR_CPUS-1] = -1};
  77. static DECLARE_BITMAP(pad_busy_cpus_bits, NR_CPUS);
  78. static void round_robin_cpu(unsigned int tsk_index)
  79. {
  80. struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits);
  81. cpumask_var_t tmp;
  82. int cpu;
  83. unsigned long min_weight = -1;
  84. unsigned long preferred_cpu;
  85. if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
  86. return;
  87. mutex_lock(&round_robin_lock);
  88. cpumask_clear(tmp);
  89. for_each_cpu(cpu, pad_busy_cpus)
  90. cpumask_or(tmp, tmp, topology_sibling_cpumask(cpu));
  91. cpumask_andnot(tmp, cpu_online_mask, tmp);
  92. /* avoid HT siblings if possible */
  93. if (cpumask_empty(tmp))
  94. cpumask_andnot(tmp, cpu_online_mask, pad_busy_cpus);
  95. if (cpumask_empty(tmp)) {
  96. mutex_unlock(&round_robin_lock);
  97. free_cpumask_var(tmp);
  98. return;
  99. }
  100. for_each_cpu(cpu, tmp) {
  101. if (cpu_weight[cpu] < min_weight) {
  102. min_weight = cpu_weight[cpu];
  103. preferred_cpu = cpu;
  104. }
  105. }
  106. if (tsk_in_cpu[tsk_index] != -1)
  107. cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus);
  108. tsk_in_cpu[tsk_index] = preferred_cpu;
  109. cpumask_set_cpu(preferred_cpu, pad_busy_cpus);
  110. cpu_weight[preferred_cpu]++;
  111. mutex_unlock(&round_robin_lock);
  112. set_cpus_allowed_ptr(current, cpumask_of(preferred_cpu));
  113. free_cpumask_var(tmp);
  114. }
  115. static void exit_round_robin(unsigned int tsk_index)
  116. {
  117. struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits);
  118. if (tsk_in_cpu[tsk_index] != -1) {
  119. cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus);
  120. tsk_in_cpu[tsk_index] = -1;
  121. }
  122. }
  123. static unsigned int idle_pct = 5; /* percentage */
  124. static unsigned int round_robin_time = 1; /* second */
  125. static int power_saving_thread(void *data)
  126. {
  127. int do_sleep;
  128. unsigned int tsk_index = (unsigned long)data;
  129. u64 last_jiffies = 0;
  130. sched_set_fifo_low(current);
  131. while (!kthread_should_stop()) {
  132. unsigned long expire_time;
  133. /* round robin to cpus */
  134. expire_time = last_jiffies + round_robin_time * HZ;
  135. if (time_before(expire_time, jiffies)) {
  136. last_jiffies = jiffies;
  137. round_robin_cpu(tsk_index);
  138. }
  139. do_sleep = 0;
  140. expire_time = jiffies + HZ * (100 - idle_pct) / 100;
  141. while (!need_resched()) {
  142. if (tsc_detected_unstable && !tsc_marked_unstable) {
  143. /* TSC could halt in idle, so notify users */
  144. mark_tsc_unstable("TSC halts in idle");
  145. tsc_marked_unstable = 1;
  146. }
  147. local_irq_disable();
  148. perf_lopwr_cb(true);
  149. tick_broadcast_enable();
  150. tick_broadcast_enter();
  151. stop_critical_timings();
  152. mwait_idle_with_hints(power_saving_mwait_eax, 1);
  153. start_critical_timings();
  154. tick_broadcast_exit();
  155. perf_lopwr_cb(false);
  156. local_irq_enable();
  157. if (time_before(expire_time, jiffies)) {
  158. do_sleep = 1;
  159. break;
  160. }
  161. }
  162. /*
  163. * current sched_rt has threshold for rt task running time.
  164. * When a rt task uses 95% CPU time, the rt thread will be
  165. * scheduled out for 5% CPU time to not starve other tasks. But
  166. * the mechanism only works when all CPUs have RT task running,
  167. * as if one CPU hasn't RT task, RT task from other CPUs will
  168. * borrow CPU time from this CPU and cause RT task use > 95%
  169. * CPU time. To make 'avoid starvation' work, takes a nap here.
  170. */
  171. if (unlikely(do_sleep))
  172. schedule_timeout_killable(HZ * idle_pct / 100);
  173. /* If an external event has set the need_resched flag, then
  174. * we need to deal with it, or this loop will continue to
  175. * spin without calling __mwait().
  176. */
  177. if (unlikely(need_resched()))
  178. schedule();
  179. }
  180. exit_round_robin(tsk_index);
  181. return 0;
  182. }
  183. static struct task_struct *ps_tsks[NR_CPUS];
  184. static unsigned int ps_tsk_num;
  185. static int create_power_saving_task(void)
  186. {
  187. int rc;
  188. ps_tsks[ps_tsk_num] = kthread_run(power_saving_thread,
  189. (void *)(unsigned long)ps_tsk_num,
  190. "acpi_pad/%d", ps_tsk_num);
  191. if (IS_ERR(ps_tsks[ps_tsk_num])) {
  192. rc = PTR_ERR(ps_tsks[ps_tsk_num]);
  193. ps_tsks[ps_tsk_num] = NULL;
  194. } else {
  195. rc = 0;
  196. ps_tsk_num++;
  197. }
  198. return rc;
  199. }
  200. static void destroy_power_saving_task(void)
  201. {
  202. if (ps_tsk_num > 0) {
  203. ps_tsk_num--;
  204. kthread_stop(ps_tsks[ps_tsk_num]);
  205. ps_tsks[ps_tsk_num] = NULL;
  206. }
  207. }
  208. static void set_power_saving_task_num(unsigned int num)
  209. {
  210. if (num > ps_tsk_num) {
  211. while (ps_tsk_num < num) {
  212. if (create_power_saving_task())
  213. return;
  214. }
  215. } else if (num < ps_tsk_num) {
  216. while (ps_tsk_num > num)
  217. destroy_power_saving_task();
  218. }
  219. }
  220. static void acpi_pad_idle_cpus(unsigned int num_cpus)
  221. {
  222. cpus_read_lock();
  223. num_cpus = min_t(unsigned int, num_cpus, num_online_cpus());
  224. set_power_saving_task_num(num_cpus);
  225. cpus_read_unlock();
  226. }
  227. static uint32_t acpi_pad_idle_cpus_num(void)
  228. {
  229. return ps_tsk_num;
  230. }
  231. static ssize_t rrtime_store(struct device *dev,
  232. struct device_attribute *attr, const char *buf, size_t count)
  233. {
  234. unsigned long num;
  235. if (kstrtoul(buf, 0, &num))
  236. return -EINVAL;
  237. if (num < 1 || num >= 100)
  238. return -EINVAL;
  239. mutex_lock(&isolated_cpus_lock);
  240. round_robin_time = num;
  241. mutex_unlock(&isolated_cpus_lock);
  242. return count;
  243. }
  244. static ssize_t rrtime_show(struct device *dev,
  245. struct device_attribute *attr, char *buf)
  246. {
  247. return sysfs_emit(buf, "%d\n", round_robin_time);
  248. }
  249. static DEVICE_ATTR_RW(rrtime);
  250. static ssize_t idlepct_store(struct device *dev,
  251. struct device_attribute *attr, const char *buf, size_t count)
  252. {
  253. unsigned long num;
  254. if (kstrtoul(buf, 0, &num))
  255. return -EINVAL;
  256. if (num < 1 || num >= 100)
  257. return -EINVAL;
  258. mutex_lock(&isolated_cpus_lock);
  259. idle_pct = num;
  260. mutex_unlock(&isolated_cpus_lock);
  261. return count;
  262. }
  263. static ssize_t idlepct_show(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. return sysfs_emit(buf, "%d\n", idle_pct);
  267. }
  268. static DEVICE_ATTR_RW(idlepct);
  269. static ssize_t idlecpus_store(struct device *dev,
  270. struct device_attribute *attr, const char *buf, size_t count)
  271. {
  272. unsigned long num;
  273. if (kstrtoul(buf, 0, &num))
  274. return -EINVAL;
  275. mutex_lock(&isolated_cpus_lock);
  276. acpi_pad_idle_cpus(num);
  277. mutex_unlock(&isolated_cpus_lock);
  278. return count;
  279. }
  280. static ssize_t idlecpus_show(struct device *dev,
  281. struct device_attribute *attr, char *buf)
  282. {
  283. return cpumap_print_to_pagebuf(false, buf,
  284. to_cpumask(pad_busy_cpus_bits));
  285. }
  286. static DEVICE_ATTR_RW(idlecpus);
  287. static struct attribute *acpi_pad_attrs[] = {
  288. &dev_attr_idlecpus.attr,
  289. &dev_attr_idlepct.attr,
  290. &dev_attr_rrtime.attr,
  291. NULL
  292. };
  293. ATTRIBUTE_GROUPS(acpi_pad);
  294. /*
  295. * Query firmware how many CPUs should be idle
  296. * return -1 on failure
  297. */
  298. static int acpi_pad_pur(acpi_handle handle)
  299. {
  300. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  301. union acpi_object *package;
  302. int num = -1;
  303. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
  304. return num;
  305. if (!buffer.length || !buffer.pointer)
  306. return num;
  307. package = buffer.pointer;
  308. if (package->type == ACPI_TYPE_PACKAGE &&
  309. package->package.count == 2 &&
  310. package->package.elements[0].integer.value == 1) /* rev 1 */
  311. num = package->package.elements[1].integer.value;
  312. kfree(buffer.pointer);
  313. return num;
  314. }
  315. static void acpi_pad_handle_notify(acpi_handle handle)
  316. {
  317. int num_cpus;
  318. uint32_t idle_cpus;
  319. struct acpi_buffer param = {
  320. .length = 4,
  321. .pointer = (void *)&idle_cpus,
  322. };
  323. u32 status;
  324. mutex_lock(&isolated_cpus_lock);
  325. num_cpus = acpi_pad_pur(handle);
  326. if (num_cpus < 0) {
  327. /* The ACPI specification says that if no action was performed when
  328. * processing the _PUR object, _OST should still be evaluated, albeit
  329. * with a different status code.
  330. */
  331. status = ACPI_PROCESSOR_AGGREGATOR_STATUS_NO_ACTION;
  332. } else {
  333. status = ACPI_PROCESSOR_AGGREGATOR_STATUS_SUCCESS;
  334. acpi_pad_idle_cpus(num_cpus);
  335. }
  336. idle_cpus = acpi_pad_idle_cpus_num();
  337. acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY, status, &param);
  338. mutex_unlock(&isolated_cpus_lock);
  339. }
  340. static void acpi_pad_notify(acpi_handle handle, u32 event,
  341. void *data)
  342. {
  343. struct acpi_device *adev = data;
  344. switch (event) {
  345. case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
  346. acpi_pad_handle_notify(handle);
  347. acpi_bus_generate_netlink_event(adev->pnp.device_class,
  348. dev_name(&adev->dev), event, 0);
  349. break;
  350. default:
  351. pr_warn("Unsupported event [0x%x]\n", event);
  352. break;
  353. }
  354. }
  355. static int acpi_pad_probe(struct platform_device *pdev)
  356. {
  357. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  358. acpi_status status;
  359. strscpy(acpi_device_name(adev), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
  360. strscpy(acpi_device_class(adev), ACPI_PROCESSOR_AGGREGATOR_CLASS);
  361. status = acpi_install_notify_handler(adev->handle,
  362. ACPI_DEVICE_NOTIFY, acpi_pad_notify, adev);
  363. if (ACPI_FAILURE(status))
  364. return -ENODEV;
  365. return 0;
  366. }
  367. static void acpi_pad_remove(struct platform_device *pdev)
  368. {
  369. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  370. mutex_lock(&isolated_cpus_lock);
  371. acpi_pad_idle_cpus(0);
  372. mutex_unlock(&isolated_cpus_lock);
  373. acpi_remove_notify_handler(adev->handle,
  374. ACPI_DEVICE_NOTIFY, acpi_pad_notify);
  375. }
  376. static const struct acpi_device_id pad_device_ids[] = {
  377. {"ACPI000C", 0},
  378. {"", 0},
  379. };
  380. MODULE_DEVICE_TABLE(acpi, pad_device_ids);
  381. static struct platform_driver acpi_pad_driver = {
  382. .probe = acpi_pad_probe,
  383. .remove = acpi_pad_remove,
  384. .driver = {
  385. .dev_groups = acpi_pad_groups,
  386. .name = "processor_aggregator",
  387. .acpi_match_table = pad_device_ids,
  388. },
  389. };
  390. static int __init acpi_pad_init(void)
  391. {
  392. /* Xen ACPI PAD is used when running as Xen Dom0. */
  393. if (xen_initial_domain())
  394. return -ENODEV;
  395. power_saving_mwait_init();
  396. if (power_saving_mwait_eax == 0)
  397. return -EINVAL;
  398. return platform_driver_register(&acpi_pad_driver);
  399. }
  400. static void __exit acpi_pad_exit(void)
  401. {
  402. platform_driver_unregister(&acpi_pad_driver);
  403. }
  404. module_init(acpi_pad_init);
  405. module_exit(acpi_pad_exit);
  406. MODULE_AUTHOR("Shaohua Li<shaohua.li@intel.com>");
  407. MODULE_DESCRIPTION("ACPI Processor Aggregator Driver");
  408. MODULE_LICENSE("GPL");