acpi_pad.c 12 KB

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