acpi_pad.c 13 KB

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