acpi_pad.c 13 KB

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