acpi_pad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/kthread.h>
  26. #include <linux/freezer.h>
  27. #include <linux/cpu.h>
  28. #include <linux/tick.h>
  29. #include <linux/slab.h>
  30. #include <linux/acpi.h>
  31. #include <asm/mwait.h>
  32. #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
  33. #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
  34. #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
  35. static DEFINE_MUTEX(isolated_cpus_lock);
  36. static DEFINE_MUTEX(round_robin_lock);
  37. static unsigned long power_saving_mwait_eax;
  38. static unsigned char tsc_detected_unstable;
  39. static unsigned char tsc_marked_unstable;
  40. static void power_saving_mwait_init(void)
  41. {
  42. unsigned int eax, ebx, ecx, edx;
  43. unsigned int highest_cstate = 0;
  44. unsigned int highest_subcstate = 0;
  45. int i;
  46. if (!boot_cpu_has(X86_FEATURE_MWAIT))
  47. return;
  48. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  49. return;
  50. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  51. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  52. !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
  53. return;
  54. edx >>= MWAIT_SUBSTATE_SIZE;
  55. for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
  56. if (edx & MWAIT_SUBSTATE_MASK) {
  57. highest_cstate = i;
  58. highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
  59. }
  60. }
  61. power_saving_mwait_eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
  62. (highest_subcstate - 1);
  63. #if defined(CONFIG_X86)
  64. switch (boot_cpu_data.x86_vendor) {
  65. case X86_VENDOR_AMD:
  66. case X86_VENDOR_INTEL:
  67. /*
  68. * AMD Fam10h TSC will tick in all
  69. * C/P/S0/S1 states when this bit is set.
  70. */
  71. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  72. tsc_detected_unstable = 1;
  73. break;
  74. default:
  75. /* TSC could halt in idle */
  76. tsc_detected_unstable = 1;
  77. }
  78. #endif
  79. }
  80. static unsigned long cpu_weight[NR_CPUS];
  81. static int tsk_in_cpu[NR_CPUS] = {[0 ... NR_CPUS-1] = -1};
  82. static DECLARE_BITMAP(pad_busy_cpus_bits, NR_CPUS);
  83. static void round_robin_cpu(unsigned int tsk_index)
  84. {
  85. struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits);
  86. cpumask_var_t tmp;
  87. int cpu;
  88. unsigned long min_weight = -1;
  89. unsigned long uninitialized_var(preferred_cpu);
  90. if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
  91. return;
  92. mutex_lock(&round_robin_lock);
  93. cpumask_clear(tmp);
  94. for_each_cpu(cpu, pad_busy_cpus)
  95. cpumask_or(tmp, tmp, topology_sibling_cpumask(cpu));
  96. cpumask_andnot(tmp, cpu_online_mask, tmp);
  97. /* avoid HT sibilings if possible */
  98. if (cpumask_empty(tmp))
  99. cpumask_andnot(tmp, cpu_online_mask, pad_busy_cpus);
  100. if (cpumask_empty(tmp)) {
  101. mutex_unlock(&round_robin_lock);
  102. return;
  103. }
  104. for_each_cpu(cpu, tmp) {
  105. if (cpu_weight[cpu] < min_weight) {
  106. min_weight = cpu_weight[cpu];
  107. preferred_cpu = cpu;
  108. }
  109. }
  110. if (tsk_in_cpu[tsk_index] != -1)
  111. cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus);
  112. tsk_in_cpu[tsk_index] = preferred_cpu;
  113. cpumask_set_cpu(preferred_cpu, pad_busy_cpus);
  114. cpu_weight[preferred_cpu]++;
  115. mutex_unlock(&round_robin_lock);
  116. set_cpus_allowed_ptr(current, cpumask_of(preferred_cpu));
  117. }
  118. static void exit_round_robin(unsigned int tsk_index)
  119. {
  120. struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits);
  121. cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus);
  122. tsk_in_cpu[tsk_index] = -1;
  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. struct sched_param param = {.sched_priority = 1};
  129. int do_sleep;
  130. unsigned int tsk_index = (unsigned long)data;
  131. u64 last_jiffies = 0;
  132. sched_setscheduler(current, SCHED_RR, &param);
  133. while (!kthread_should_stop()) {
  134. unsigned long expire_time;
  135. try_to_freeze();
  136. /* round robin to cpus */
  137. expire_time = last_jiffies + round_robin_time * HZ;
  138. if (time_before(expire_time, jiffies)) {
  139. last_jiffies = jiffies;
  140. round_robin_cpu(tsk_index);
  141. }
  142. do_sleep = 0;
  143. expire_time = jiffies + HZ * (100 - idle_pct) / 100;
  144. while (!need_resched()) {
  145. if (tsc_detected_unstable && !tsc_marked_unstable) {
  146. /* TSC could halt in idle, so notify users */
  147. mark_tsc_unstable("TSC halts in idle");
  148. tsc_marked_unstable = 1;
  149. }
  150. local_irq_disable();
  151. tick_broadcast_enable();
  152. tick_broadcast_enter();
  153. stop_critical_timings();
  154. mwait_idle_with_hints(power_saving_mwait_eax, 1);
  155. start_critical_timings();
  156. tick_broadcast_exit();
  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. get_online_cpus();
  224. num_cpus = min_t(unsigned int, num_cpus, num_online_cpus());
  225. set_power_saving_task_num(num_cpus);
  226. put_online_cpus();
  227. }
  228. static uint32_t acpi_pad_idle_cpus_num(void)
  229. {
  230. return ps_tsk_num;
  231. }
  232. static ssize_t acpi_pad_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 acpi_pad_rrtime_show(struct device *dev,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. return scnprintf(buf, PAGE_SIZE, "%d\n", round_robin_time);
  249. }
  250. static DEVICE_ATTR(rrtime, S_IRUGO|S_IWUSR,
  251. acpi_pad_rrtime_show,
  252. acpi_pad_rrtime_store);
  253. static ssize_t acpi_pad_idlepct_store(struct device *dev,
  254. struct device_attribute *attr, const char *buf, size_t count)
  255. {
  256. unsigned long num;
  257. if (kstrtoul(buf, 0, &num))
  258. return -EINVAL;
  259. if (num < 1 || num >= 100)
  260. return -EINVAL;
  261. mutex_lock(&isolated_cpus_lock);
  262. idle_pct = num;
  263. mutex_unlock(&isolated_cpus_lock);
  264. return count;
  265. }
  266. static ssize_t acpi_pad_idlepct_show(struct device *dev,
  267. struct device_attribute *attr, char *buf)
  268. {
  269. return scnprintf(buf, PAGE_SIZE, "%d\n", idle_pct);
  270. }
  271. static DEVICE_ATTR(idlepct, S_IRUGO|S_IWUSR,
  272. acpi_pad_idlepct_show,
  273. acpi_pad_idlepct_store);
  274. static ssize_t acpi_pad_idlecpus_store(struct device *dev,
  275. struct device_attribute *attr, const char *buf, size_t count)
  276. {
  277. unsigned long num;
  278. if (kstrtoul(buf, 0, &num))
  279. return -EINVAL;
  280. mutex_lock(&isolated_cpus_lock);
  281. acpi_pad_idle_cpus(num);
  282. mutex_unlock(&isolated_cpus_lock);
  283. return count;
  284. }
  285. static ssize_t acpi_pad_idlecpus_show(struct device *dev,
  286. struct device_attribute *attr, char *buf)
  287. {
  288. return cpumap_print_to_pagebuf(false, buf,
  289. to_cpumask(pad_busy_cpus_bits));
  290. }
  291. static DEVICE_ATTR(idlecpus, S_IRUGO|S_IWUSR,
  292. acpi_pad_idlecpus_show,
  293. acpi_pad_idlecpus_store);
  294. static int acpi_pad_add_sysfs(struct acpi_device *device)
  295. {
  296. int result;
  297. result = device_create_file(&device->dev, &dev_attr_idlecpus);
  298. if (result)
  299. return -ENODEV;
  300. result = device_create_file(&device->dev, &dev_attr_idlepct);
  301. if (result) {
  302. device_remove_file(&device->dev, &dev_attr_idlecpus);
  303. return -ENODEV;
  304. }
  305. result = device_create_file(&device->dev, &dev_attr_rrtime);
  306. if (result) {
  307. device_remove_file(&device->dev, &dev_attr_idlecpus);
  308. device_remove_file(&device->dev, &dev_attr_idlepct);
  309. return -ENODEV;
  310. }
  311. return 0;
  312. }
  313. static void acpi_pad_remove_sysfs(struct acpi_device *device)
  314. {
  315. device_remove_file(&device->dev, &dev_attr_idlecpus);
  316. device_remove_file(&device->dev, &dev_attr_idlepct);
  317. device_remove_file(&device->dev, &dev_attr_rrtime);
  318. }
  319. /*
  320. * Query firmware how many CPUs should be idle
  321. * return -1 on failure
  322. */
  323. static int acpi_pad_pur(acpi_handle handle)
  324. {
  325. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  326. union acpi_object *package;
  327. int num = -1;
  328. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
  329. return num;
  330. if (!buffer.length || !buffer.pointer)
  331. return num;
  332. package = buffer.pointer;
  333. if (package->type == ACPI_TYPE_PACKAGE &&
  334. package->package.count == 2 &&
  335. package->package.elements[0].integer.value == 1) /* rev 1 */
  336. num = package->package.elements[1].integer.value;
  337. kfree(buffer.pointer);
  338. return num;
  339. }
  340. static void acpi_pad_handle_notify(acpi_handle handle)
  341. {
  342. int num_cpus;
  343. uint32_t idle_cpus;
  344. struct acpi_buffer param = {
  345. .length = 4,
  346. .pointer = (void *)&idle_cpus,
  347. };
  348. mutex_lock(&isolated_cpus_lock);
  349. num_cpus = acpi_pad_pur(handle);
  350. if (num_cpus < 0) {
  351. mutex_unlock(&isolated_cpus_lock);
  352. return;
  353. }
  354. acpi_pad_idle_cpus(num_cpus);
  355. idle_cpus = acpi_pad_idle_cpus_num();
  356. acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY, 0, &param);
  357. mutex_unlock(&isolated_cpus_lock);
  358. }
  359. static void acpi_pad_notify(acpi_handle handle, u32 event,
  360. void *data)
  361. {
  362. struct acpi_device *device = data;
  363. switch (event) {
  364. case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
  365. acpi_pad_handle_notify(handle);
  366. acpi_bus_generate_netlink_event(device->pnp.device_class,
  367. dev_name(&device->dev), event, 0);
  368. break;
  369. default:
  370. pr_warn("Unsupported event [0x%x]\n", event);
  371. break;
  372. }
  373. }
  374. static int acpi_pad_add(struct acpi_device *device)
  375. {
  376. acpi_status status;
  377. strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
  378. strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
  379. if (acpi_pad_add_sysfs(device))
  380. return -ENODEV;
  381. status = acpi_install_notify_handler(device->handle,
  382. ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
  383. if (ACPI_FAILURE(status)) {
  384. acpi_pad_remove_sysfs(device);
  385. return -ENODEV;
  386. }
  387. return 0;
  388. }
  389. static int acpi_pad_remove(struct acpi_device *device)
  390. {
  391. mutex_lock(&isolated_cpus_lock);
  392. acpi_pad_idle_cpus(0);
  393. mutex_unlock(&isolated_cpus_lock);
  394. acpi_remove_notify_handler(device->handle,
  395. ACPI_DEVICE_NOTIFY, acpi_pad_notify);
  396. acpi_pad_remove_sysfs(device);
  397. return 0;
  398. }
  399. static const struct acpi_device_id pad_device_ids[] = {
  400. {"ACPI000C", 0},
  401. {"", 0},
  402. };
  403. MODULE_DEVICE_TABLE(acpi, pad_device_ids);
  404. static struct acpi_driver acpi_pad_driver = {
  405. .name = "processor_aggregator",
  406. .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
  407. .ids = pad_device_ids,
  408. .ops = {
  409. .add = acpi_pad_add,
  410. .remove = acpi_pad_remove,
  411. },
  412. };
  413. static int __init acpi_pad_init(void)
  414. {
  415. power_saving_mwait_init();
  416. if (power_saving_mwait_eax == 0)
  417. return -EINVAL;
  418. return acpi_bus_register_driver(&acpi_pad_driver);
  419. }
  420. static void __exit acpi_pad_exit(void)
  421. {
  422. acpi_bus_unregister_driver(&acpi_pad_driver);
  423. }
  424. module_init(acpi_pad_init);
  425. module_exit(acpi_pad_exit);
  426. MODULE_AUTHOR("Shaohua Li<shaohua.li@intel.com>");
  427. MODULE_DESCRIPTION("ACPI Processor Aggregator Driver");
  428. MODULE_LICENSE("GPL");