acpi_pad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * acpi_pad.c ACPI Processor Aggregator Driver
  3. *
  4. * Copyright (c) 2009, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/cpumask.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/types.h>
  21. #include <linux/kthread.h>
  22. #include <linux/freezer.h>
  23. #include <linux/cpu.h>
  24. #include <linux/tick.h>
  25. #include <linux/slab.h>
  26. #include <linux/acpi.h>
  27. #include <asm/mwait.h>
  28. #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
  29. #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
  30. #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
  31. static DEFINE_MUTEX(isolated_cpus_lock);
  32. static DEFINE_MUTEX(round_robin_lock);
  33. static unsigned long power_saving_mwait_eax;
  34. static unsigned char tsc_detected_unstable;
  35. static unsigned char tsc_marked_unstable;
  36. static void power_saving_mwait_init(void)
  37. {
  38. unsigned int eax, ebx, ecx, edx;
  39. unsigned int highest_cstate = 0;
  40. unsigned int highest_subcstate = 0;
  41. int i;
  42. if (!boot_cpu_has(X86_FEATURE_MWAIT))
  43. return;
  44. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  45. return;
  46. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  47. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  48. !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
  49. return;
  50. edx >>= MWAIT_SUBSTATE_SIZE;
  51. for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
  52. if (edx & MWAIT_SUBSTATE_MASK) {
  53. highest_cstate = i;
  54. highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
  55. }
  56. }
  57. power_saving_mwait_eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
  58. (highest_subcstate - 1);
  59. #if defined(CONFIG_X86)
  60. switch (boot_cpu_data.x86_vendor) {
  61. case X86_VENDOR_AMD:
  62. case X86_VENDOR_INTEL:
  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 uninitialized_var(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 sibilings 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. 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. }
  114. static void exit_round_robin(unsigned int tsk_index)
  115. {
  116. struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits);
  117. cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus);
  118. tsk_in_cpu[tsk_index] = -1;
  119. }
  120. static unsigned int idle_pct = 5; /* percentage */
  121. static unsigned int round_robin_time = 1; /* second */
  122. static int power_saving_thread(void *data)
  123. {
  124. struct sched_param param = {.sched_priority = 1};
  125. int do_sleep;
  126. unsigned int tsk_index = (unsigned long)data;
  127. u64 last_jiffies = 0;
  128. sched_setscheduler(current, SCHED_RR, &param);
  129. while (!kthread_should_stop()) {
  130. unsigned long expire_time;
  131. /* round robin to cpus */
  132. expire_time = last_jiffies + round_robin_time * HZ;
  133. if (time_before(expire_time, jiffies)) {
  134. last_jiffies = jiffies;
  135. round_robin_cpu(tsk_index);
  136. }
  137. do_sleep = 0;
  138. expire_time = jiffies + HZ * (100 - idle_pct) / 100;
  139. while (!need_resched()) {
  140. if (tsc_detected_unstable && !tsc_marked_unstable) {
  141. /* TSC could halt in idle, so notify users */
  142. mark_tsc_unstable("TSC halts in idle");
  143. tsc_marked_unstable = 1;
  144. }
  145. local_irq_disable();
  146. tick_broadcast_enable();
  147. tick_broadcast_enter();
  148. stop_critical_timings();
  149. mwait_idle_with_hints(power_saving_mwait_eax, 1);
  150. start_critical_timings();
  151. tick_broadcast_exit();
  152. local_irq_enable();
  153. if (time_before(expire_time, jiffies)) {
  154. do_sleep = 1;
  155. break;
  156. }
  157. }
  158. /*
  159. * current sched_rt has threshold for rt task running time.
  160. * When a rt task uses 95% CPU time, the rt thread will be
  161. * scheduled out for 5% CPU time to not starve other tasks. But
  162. * the mechanism only works when all CPUs have RT task running,
  163. * as if one CPU hasn't RT task, RT task from other CPUs will
  164. * borrow CPU time from this CPU and cause RT task use > 95%
  165. * CPU time. To make 'avoid starvation' work, takes a nap here.
  166. */
  167. if (unlikely(do_sleep))
  168. schedule_timeout_killable(HZ * idle_pct / 100);
  169. /* If an external event has set the need_resched flag, then
  170. * we need to deal with it, or this loop will continue to
  171. * spin without calling __mwait().
  172. */
  173. if (unlikely(need_resched()))
  174. schedule();
  175. }
  176. exit_round_robin(tsk_index);
  177. return 0;
  178. }
  179. static struct task_struct *ps_tsks[NR_CPUS];
  180. static unsigned int ps_tsk_num;
  181. static int create_power_saving_task(void)
  182. {
  183. int rc;
  184. ps_tsks[ps_tsk_num] = kthread_run(power_saving_thread,
  185. (void *)(unsigned long)ps_tsk_num,
  186. "acpi_pad/%d", ps_tsk_num);
  187. if (IS_ERR(ps_tsks[ps_tsk_num])) {
  188. rc = PTR_ERR(ps_tsks[ps_tsk_num]);
  189. ps_tsks[ps_tsk_num] = NULL;
  190. } else {
  191. rc = 0;
  192. ps_tsk_num++;
  193. }
  194. return rc;
  195. }
  196. static void destroy_power_saving_task(void)
  197. {
  198. if (ps_tsk_num > 0) {
  199. ps_tsk_num--;
  200. kthread_stop(ps_tsks[ps_tsk_num]);
  201. ps_tsks[ps_tsk_num] = NULL;
  202. }
  203. }
  204. static void set_power_saving_task_num(unsigned int num)
  205. {
  206. if (num > ps_tsk_num) {
  207. while (ps_tsk_num < num) {
  208. if (create_power_saving_task())
  209. return;
  210. }
  211. } else if (num < ps_tsk_num) {
  212. while (ps_tsk_num > num)
  213. destroy_power_saving_task();
  214. }
  215. }
  216. static void acpi_pad_idle_cpus(unsigned int num_cpus)
  217. {
  218. get_online_cpus();
  219. num_cpus = min_t(unsigned int, num_cpus, num_online_cpus());
  220. set_power_saving_task_num(num_cpus);
  221. put_online_cpus();
  222. }
  223. static uint32_t acpi_pad_idle_cpus_num(void)
  224. {
  225. return ps_tsk_num;
  226. }
  227. static ssize_t acpi_pad_rrtime_store(struct device *dev,
  228. struct device_attribute *attr, const char *buf, size_t count)
  229. {
  230. unsigned long num;
  231. if (kstrtoul(buf, 0, &num))
  232. return -EINVAL;
  233. if (num < 1 || num >= 100)
  234. return -EINVAL;
  235. mutex_lock(&isolated_cpus_lock);
  236. round_robin_time = num;
  237. mutex_unlock(&isolated_cpus_lock);
  238. return count;
  239. }
  240. static ssize_t acpi_pad_rrtime_show(struct device *dev,
  241. struct device_attribute *attr, char *buf)
  242. {
  243. return scnprintf(buf, PAGE_SIZE, "%d\n", round_robin_time);
  244. }
  245. static DEVICE_ATTR(rrtime, S_IRUGO|S_IWUSR,
  246. acpi_pad_rrtime_show,
  247. acpi_pad_rrtime_store);
  248. static ssize_t acpi_pad_idlepct_store(struct device *dev,
  249. struct device_attribute *attr, const char *buf, size_t count)
  250. {
  251. unsigned long num;
  252. if (kstrtoul(buf, 0, &num))
  253. return -EINVAL;
  254. if (num < 1 || num >= 100)
  255. return -EINVAL;
  256. mutex_lock(&isolated_cpus_lock);
  257. idle_pct = num;
  258. mutex_unlock(&isolated_cpus_lock);
  259. return count;
  260. }
  261. static ssize_t acpi_pad_idlepct_show(struct device *dev,
  262. struct device_attribute *attr, char *buf)
  263. {
  264. return scnprintf(buf, PAGE_SIZE, "%d\n", idle_pct);
  265. }
  266. static DEVICE_ATTR(idlepct, S_IRUGO|S_IWUSR,
  267. acpi_pad_idlepct_show,
  268. acpi_pad_idlepct_store);
  269. static ssize_t acpi_pad_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 acpi_pad_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(idlecpus, S_IRUGO|S_IWUSR,
  287. acpi_pad_idlecpus_show,
  288. acpi_pad_idlecpus_store);
  289. static int acpi_pad_add_sysfs(struct acpi_device *device)
  290. {
  291. int result;
  292. result = device_create_file(&device->dev, &dev_attr_idlecpus);
  293. if (result)
  294. return -ENODEV;
  295. result = device_create_file(&device->dev, &dev_attr_idlepct);
  296. if (result) {
  297. device_remove_file(&device->dev, &dev_attr_idlecpus);
  298. return -ENODEV;
  299. }
  300. result = device_create_file(&device->dev, &dev_attr_rrtime);
  301. if (result) {
  302. device_remove_file(&device->dev, &dev_attr_idlecpus);
  303. device_remove_file(&device->dev, &dev_attr_idlepct);
  304. return -ENODEV;
  305. }
  306. return 0;
  307. }
  308. static void acpi_pad_remove_sysfs(struct acpi_device *device)
  309. {
  310. device_remove_file(&device->dev, &dev_attr_idlecpus);
  311. device_remove_file(&device->dev, &dev_attr_idlepct);
  312. device_remove_file(&device->dev, &dev_attr_rrtime);
  313. }
  314. /*
  315. * Query firmware how many CPUs should be idle
  316. * return -1 on failure
  317. */
  318. static int acpi_pad_pur(acpi_handle handle)
  319. {
  320. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  321. union acpi_object *package;
  322. int num = -1;
  323. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
  324. return num;
  325. if (!buffer.length || !buffer.pointer)
  326. return num;
  327. package = buffer.pointer;
  328. if (package->type == ACPI_TYPE_PACKAGE &&
  329. package->package.count == 2 &&
  330. package->package.elements[0].integer.value == 1) /* rev 1 */
  331. num = package->package.elements[1].integer.value;
  332. kfree(buffer.pointer);
  333. return num;
  334. }
  335. static void acpi_pad_handle_notify(acpi_handle handle)
  336. {
  337. int num_cpus;
  338. uint32_t idle_cpus;
  339. struct acpi_buffer param = {
  340. .length = 4,
  341. .pointer = (void *)&idle_cpus,
  342. };
  343. mutex_lock(&isolated_cpus_lock);
  344. num_cpus = acpi_pad_pur(handle);
  345. if (num_cpus < 0) {
  346. mutex_unlock(&isolated_cpus_lock);
  347. return;
  348. }
  349. acpi_pad_idle_cpus(num_cpus);
  350. idle_cpus = acpi_pad_idle_cpus_num();
  351. acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY, 0, &param);
  352. mutex_unlock(&isolated_cpus_lock);
  353. }
  354. static void acpi_pad_notify(acpi_handle handle, u32 event,
  355. void *data)
  356. {
  357. struct acpi_device *device = data;
  358. switch (event) {
  359. case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
  360. acpi_pad_handle_notify(handle);
  361. acpi_bus_generate_netlink_event(device->pnp.device_class,
  362. dev_name(&device->dev), event, 0);
  363. break;
  364. default:
  365. pr_warn("Unsupported event [0x%x]\n", event);
  366. break;
  367. }
  368. }
  369. static int acpi_pad_add(struct acpi_device *device)
  370. {
  371. acpi_status status;
  372. strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
  373. strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
  374. if (acpi_pad_add_sysfs(device))
  375. return -ENODEV;
  376. status = acpi_install_notify_handler(device->handle,
  377. ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
  378. if (ACPI_FAILURE(status)) {
  379. acpi_pad_remove_sysfs(device);
  380. return -ENODEV;
  381. }
  382. return 0;
  383. }
  384. static int acpi_pad_remove(struct acpi_device *device)
  385. {
  386. mutex_lock(&isolated_cpus_lock);
  387. acpi_pad_idle_cpus(0);
  388. mutex_unlock(&isolated_cpus_lock);
  389. acpi_remove_notify_handler(device->handle,
  390. ACPI_DEVICE_NOTIFY, acpi_pad_notify);
  391. acpi_pad_remove_sysfs(device);
  392. return 0;
  393. }
  394. static const struct acpi_device_id pad_device_ids[] = {
  395. {"ACPI000C", 0},
  396. {"", 0},
  397. };
  398. MODULE_DEVICE_TABLE(acpi, pad_device_ids);
  399. static struct acpi_driver acpi_pad_driver = {
  400. .name = "processor_aggregator",
  401. .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
  402. .ids = pad_device_ids,
  403. .ops = {
  404. .add = acpi_pad_add,
  405. .remove = acpi_pad_remove,
  406. },
  407. };
  408. static int __init acpi_pad_init(void)
  409. {
  410. power_saving_mwait_init();
  411. if (power_saving_mwait_eax == 0)
  412. return -EINVAL;
  413. return acpi_bus_register_driver(&acpi_pad_driver);
  414. }
  415. static void __exit acpi_pad_exit(void)
  416. {
  417. acpi_bus_unregister_driver(&acpi_pad_driver);
  418. }
  419. module_init(acpi_pad_init);
  420. module_exit(acpi_pad_exit);
  421. MODULE_AUTHOR("Shaohua Li<shaohua.li@intel.com>");
  422. MODULE_DESCRIPTION("ACPI Processor Aggregator Driver");
  423. MODULE_LICENSE("GPL");