acpi_pad.c 12 KB

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