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