acpi_pad.c 12 KB

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