1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * acpi_pad.c ACPI Processor Aggregator Driver 4 * 5 * Copyright (c) 2009, Intel Corporation. 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/cpumask.h> 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/types.h> 13 #include <linux/kthread.h> 14 #include <uapi/linux/sched/types.h> 15 #include <linux/freezer.h> 16 #include <linux/cpu.h> 17 #include <linux/tick.h> 18 #include <linux/slab.h> 19 #include <linux/acpi.h> 20 #include <linux/perf_event.h> 21 #include <linux/platform_device.h> 22 #include <asm/cpuid/api.h> 23 #include <asm/mwait.h> 24 #include <xen/xen.h> 25 26 #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80 27 28 #define ACPI_PROCESSOR_AGGREGATOR_STATUS_SUCCESS 0 29 #define ACPI_PROCESSOR_AGGREGATOR_STATUS_NO_ACTION 1 30 31 static DEFINE_MUTEX(isolated_cpus_lock); 32 static DEFINE_MUTEX(round_robin_lock); 33 34 static unsigned int power_saving_mwait_eax; 35 36 static unsigned char tsc_detected_unstable; 37 static unsigned char tsc_marked_unstable; 38 39 static void power_saving_mwait_init(void) 40 { 41 unsigned int eax, ebx, ecx, edx; 42 unsigned int highest_cstate = 0; 43 unsigned int highest_subcstate = 0; 44 int i; 45 46 if (!boot_cpu_has(X86_FEATURE_MWAIT)) 47 return; 48 49 cpuid(CPUID_LEAF_MWAIT, &eax, &ebx, &ecx, &edx); 50 51 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) || 52 !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) 53 return; 54 55 edx >>= MWAIT_SUBSTATE_SIZE; 56 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) { 57 if (edx & MWAIT_SUBSTATE_MASK) { 58 highest_cstate = i; 59 highest_subcstate = edx & MWAIT_SUBSTATE_MASK; 60 } 61 } 62 power_saving_mwait_eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) | 63 (highest_subcstate - 1); 64 65 #if defined(CONFIG_X86) 66 switch (boot_cpu_data.x86_vendor) { 67 case X86_VENDOR_HYGON: 68 case X86_VENDOR_AMD: 69 case X86_VENDOR_INTEL: 70 case X86_VENDOR_ZHAOXIN: 71 case X86_VENDOR_CENTAUR: 72 /* 73 * AMD Fam10h TSC will tick in all 74 * C/P/S0/S1 states when this bit is set. 75 */ 76 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) 77 tsc_detected_unstable = 1; 78 break; 79 default: 80 /* TSC could halt in idle */ 81 tsc_detected_unstable = 1; 82 } 83 #endif 84 } 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 preferred_cpu; 96 97 if (!alloc_cpumask_var(&tmp, GFP_KERNEL)) 98 return; 99 100 mutex_lock(&round_robin_lock); 101 cpumask_clear(tmp); 102 for_each_cpu(cpu, pad_busy_cpus) 103 cpumask_or(tmp, tmp, topology_sibling_cpumask(cpu)); 104 cpumask_andnot(tmp, cpu_online_mask, tmp); 105 /* avoid HT siblings if possible */ 106 if (cpumask_empty(tmp)) 107 cpumask_andnot(tmp, cpu_online_mask, pad_busy_cpus); 108 if (cpumask_empty(tmp)) { 109 mutex_unlock(&round_robin_lock); 110 free_cpumask_var(tmp); 111 return; 112 } 113 for_each_cpu(cpu, tmp) { 114 if (cpu_weight[cpu] < min_weight) { 115 min_weight = cpu_weight[cpu]; 116 preferred_cpu = cpu; 117 } 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(&round_robin_lock); 126 127 set_cpus_allowed_ptr(current, cpumask_of(preferred_cpu)); 128 129 free_cpumask_var(tmp); 130 } 131 132 static void exit_round_robin(unsigned int tsk_index) 133 { 134 struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits); 135 136 if (tsk_in_cpu[tsk_index] != -1) { 137 cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus); 138 tsk_in_cpu[tsk_index] = -1; 139 } 140 } 141 142 static unsigned int idle_pct = 5; /* percentage */ 143 static unsigned int round_robin_time = 1; /* second */ 144 static int power_saving_thread(void *data) 145 { 146 int do_sleep; 147 unsigned int tsk_index = (unsigned long)data; 148 u64 last_jiffies = 0; 149 150 sched_set_fifo_low(current); 151 152 while (!kthread_should_stop()) { 153 unsigned long expire_time; 154 155 /* round robin to cpus */ 156 expire_time = last_jiffies + round_robin_time * HZ; 157 if (time_before(expire_time, jiffies)) { 158 last_jiffies = jiffies; 159 round_robin_cpu(tsk_index); 160 } 161 162 do_sleep = 0; 163 164 expire_time = jiffies + HZ * (100 - idle_pct) / 100; 165 166 while (!need_resched()) { 167 if (tsc_detected_unstable && !tsc_marked_unstable) { 168 /* TSC could halt in idle, so notify users */ 169 mark_tsc_unstable("TSC halts in idle"); 170 tsc_marked_unstable = 1; 171 } 172 local_irq_disable(); 173 174 perf_lopwr_cb(true); 175 176 tick_broadcast_enable(); 177 tick_broadcast_enter(); 178 stop_critical_timings(); 179 180 mwait_idle_with_hints(power_saving_mwait_eax, 1); 181 182 start_critical_timings(); 183 tick_broadcast_exit(); 184 185 perf_lopwr_cb(false); 186 187 local_irq_enable(); 188 189 if (time_before(expire_time, jiffies)) { 190 do_sleep = 1; 191 break; 192 } 193 } 194 195 /* 196 * current sched_rt has threshold for rt task running time. 197 * When a rt task uses 95% CPU time, the rt thread will be 198 * scheduled out for 5% CPU time to not starve other tasks. But 199 * the mechanism only works when all CPUs have RT task running, 200 * as if one CPU hasn't RT task, RT task from other CPUs will 201 * borrow CPU time from this CPU and cause RT task use > 95% 202 * CPU time. To make 'avoid starvation' work, takes a nap here. 203 */ 204 if (unlikely(do_sleep)) 205 schedule_timeout_killable(HZ * idle_pct / 100); 206 207 /* If an external event has set the need_resched flag, then 208 * we need to deal with it, or this loop will continue to 209 * spin without calling __mwait(). 210 */ 211 if (unlikely(need_resched())) 212 schedule(); 213 } 214 215 exit_round_robin(tsk_index); 216 return 0; 217 } 218 219 static struct task_struct *ps_tsks[NR_CPUS]; 220 static unsigned int ps_tsk_num; 221 static int create_power_saving_task(void) 222 { 223 int rc; 224 225 ps_tsks[ps_tsk_num] = kthread_run(power_saving_thread, 226 (void *)(unsigned long)ps_tsk_num, 227 "acpi_pad/%d", ps_tsk_num); 228 229 if (IS_ERR(ps_tsks[ps_tsk_num])) { 230 rc = PTR_ERR(ps_tsks[ps_tsk_num]); 231 ps_tsks[ps_tsk_num] = NULL; 232 } else { 233 rc = 0; 234 ps_tsk_num++; 235 } 236 237 return rc; 238 } 239 240 static void destroy_power_saving_task(void) 241 { 242 if (ps_tsk_num > 0) { 243 ps_tsk_num--; 244 kthread_stop(ps_tsks[ps_tsk_num]); 245 ps_tsks[ps_tsk_num] = NULL; 246 } 247 } 248 249 static void set_power_saving_task_num(unsigned int num) 250 { 251 if (num > ps_tsk_num) { 252 while (ps_tsk_num < num) { 253 if (create_power_saving_task()) 254 return; 255 } 256 } else if (num < ps_tsk_num) { 257 while (ps_tsk_num > num) 258 destroy_power_saving_task(); 259 } 260 } 261 262 static void acpi_pad_idle_cpus(unsigned int num_cpus) 263 { 264 cpus_read_lock(); 265 266 num_cpus = min_t(unsigned int, num_cpus, num_online_cpus()); 267 set_power_saving_task_num(num_cpus); 268 269 cpus_read_unlock(); 270 } 271 272 static uint32_t acpi_pad_idle_cpus_num(void) 273 { 274 return ps_tsk_num; 275 } 276 277 static ssize_t rrtime_store(struct device *dev, 278 struct device_attribute *attr, const char *buf, size_t count) 279 { 280 unsigned long num; 281 282 if (kstrtoul(buf, 0, &num)) 283 return -EINVAL; 284 if (num < 1 || num >= 100) 285 return -EINVAL; 286 mutex_lock(&isolated_cpus_lock); 287 round_robin_time = num; 288 mutex_unlock(&isolated_cpus_lock); 289 return count; 290 } 291 292 static ssize_t rrtime_show(struct device *dev, 293 struct device_attribute *attr, char *buf) 294 { 295 return sysfs_emit(buf, "%d\n", round_robin_time); 296 } 297 static DEVICE_ATTR_RW(rrtime); 298 299 static ssize_t idlepct_store(struct device *dev, 300 struct device_attribute *attr, const char *buf, size_t count) 301 { 302 unsigned long num; 303 304 if (kstrtoul(buf, 0, &num)) 305 return -EINVAL; 306 if (num < 1 || num >= 100) 307 return -EINVAL; 308 mutex_lock(&isolated_cpus_lock); 309 idle_pct = num; 310 mutex_unlock(&isolated_cpus_lock); 311 return count; 312 } 313 314 static ssize_t idlepct_show(struct device *dev, 315 struct device_attribute *attr, char *buf) 316 { 317 return sysfs_emit(buf, "%d\n", idle_pct); 318 } 319 static DEVICE_ATTR_RW(idlepct); 320 321 static ssize_t idlecpus_store(struct device *dev, 322 struct device_attribute *attr, const char *buf, size_t count) 323 { 324 unsigned long num; 325 326 if (kstrtoul(buf, 0, &num)) 327 return -EINVAL; 328 mutex_lock(&isolated_cpus_lock); 329 acpi_pad_idle_cpus(num); 330 mutex_unlock(&isolated_cpus_lock); 331 return count; 332 } 333 334 static ssize_t idlecpus_show(struct device *dev, 335 struct device_attribute *attr, char *buf) 336 { 337 return cpumap_print_to_pagebuf(false, buf, 338 to_cpumask(pad_busy_cpus_bits)); 339 } 340 341 static DEVICE_ATTR_RW(idlecpus); 342 343 static struct attribute *acpi_pad_attrs[] = { 344 &dev_attr_idlecpus.attr, 345 &dev_attr_idlepct.attr, 346 &dev_attr_rrtime.attr, 347 NULL 348 }; 349 350 ATTRIBUTE_GROUPS(acpi_pad); 351 352 /* 353 * Query firmware how many CPUs should be idle 354 * return -1 on failure 355 */ 356 static int acpi_pad_pur(acpi_handle handle) 357 { 358 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 359 union acpi_object *package; 360 int num = -1; 361 362 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer))) 363 return num; 364 365 if (!buffer.length || !buffer.pointer) 366 return num; 367 368 package = buffer.pointer; 369 370 if (package->type == ACPI_TYPE_PACKAGE && 371 package->package.count == 2 && 372 package->package.elements[0].integer.value == 1) /* rev 1 */ 373 374 num = package->package.elements[1].integer.value; 375 376 kfree(buffer.pointer); 377 return num; 378 } 379 380 static void acpi_pad_handle_notify(acpi_handle handle) 381 { 382 int num_cpus; 383 uint32_t idle_cpus; 384 struct acpi_buffer param = { 385 .length = 4, 386 .pointer = (void *)&idle_cpus, 387 }; 388 u32 status; 389 390 mutex_lock(&isolated_cpus_lock); 391 num_cpus = acpi_pad_pur(handle); 392 if (num_cpus < 0) { 393 /* The ACPI specification says that if no action was performed when 394 * processing the _PUR object, _OST should still be evaluated, albeit 395 * with a different status code. 396 */ 397 status = ACPI_PROCESSOR_AGGREGATOR_STATUS_NO_ACTION; 398 } else { 399 status = ACPI_PROCESSOR_AGGREGATOR_STATUS_SUCCESS; 400 acpi_pad_idle_cpus(num_cpus); 401 } 402 403 idle_cpus = acpi_pad_idle_cpus_num(); 404 acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY, status, ¶m); 405 mutex_unlock(&isolated_cpus_lock); 406 } 407 408 static void acpi_pad_notify(acpi_handle handle, u32 event, void *data) 409 { 410 struct acpi_device *adev = data; 411 412 switch (event) { 413 case ACPI_PROCESSOR_AGGREGATOR_NOTIFY: 414 acpi_pad_handle_notify(handle); 415 acpi_bus_generate_netlink_event("acpi_pad", 416 dev_name(&adev->dev), event, 0); 417 break; 418 default: 419 pr_warn("Unsupported event [0x%x]\n", event); 420 break; 421 } 422 } 423 424 static int acpi_pad_probe(struct platform_device *pdev) 425 { 426 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); 427 428 return acpi_dev_install_notify_handler(adev, ACPI_DEVICE_NOTIFY, 429 acpi_pad_notify, adev); 430 } 431 432 static void acpi_pad_remove(struct platform_device *pdev) 433 { 434 mutex_lock(&isolated_cpus_lock); 435 acpi_pad_idle_cpus(0); 436 mutex_unlock(&isolated_cpus_lock); 437 438 acpi_dev_remove_notify_handler(ACPI_COMPANION(&pdev->dev), 439 ACPI_DEVICE_NOTIFY, acpi_pad_notify); 440 } 441 442 static const struct acpi_device_id pad_device_ids[] = { 443 {"ACPI000C", 0}, 444 {"", 0}, 445 }; 446 MODULE_DEVICE_TABLE(acpi, pad_device_ids); 447 448 static struct platform_driver acpi_pad_driver = { 449 .probe = acpi_pad_probe, 450 .remove = acpi_pad_remove, 451 .driver = { 452 .dev_groups = acpi_pad_groups, 453 .name = "processor_aggregator", 454 .acpi_match_table = pad_device_ids, 455 }, 456 }; 457 458 static int __init acpi_pad_init(void) 459 { 460 /* Xen ACPI PAD is used when running as Xen Dom0. */ 461 if (xen_initial_domain()) 462 return -ENODEV; 463 464 power_saving_mwait_init(); 465 if (power_saving_mwait_eax == 0) 466 return -EINVAL; 467 468 return platform_driver_register(&acpi_pad_driver); 469 } 470 471 static void __exit acpi_pad_exit(void) 472 { 473 platform_driver_unregister(&acpi_pad_driver); 474 } 475 476 module_init(acpi_pad_init); 477 module_exit(acpi_pad_exit); 478 MODULE_AUTHOR("Shaohua Li<shaohua.li@intel.com>"); 479 MODULE_DESCRIPTION("ACPI Processor Aggregator Driver"); 480 MODULE_LICENSE("GPL"); 481