acpi_pad.c 13 KB

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