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