acpi_pad.c 12 KB

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