12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28e0af514SShaohua Li /*
38e0af514SShaohua Li * acpi_pad.c ACPI Processor Aggregator Driver
48e0af514SShaohua Li *
58e0af514SShaohua Li * Copyright (c) 2009, Intel Corporation.
68e0af514SShaohua Li */
78e0af514SShaohua Li
88e0af514SShaohua Li #include <linux/kernel.h>
98e0af514SShaohua Li #include <linux/cpumask.h>
108e0af514SShaohua Li #include <linux/module.h>
118e0af514SShaohua Li #include <linux/init.h>
128e0af514SShaohua Li #include <linux/types.h>
138e0af514SShaohua Li #include <linux/kthread.h>
14ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h>
158e0af514SShaohua Li #include <linux/freezer.h>
168e0af514SShaohua Li #include <linux/cpu.h>
17979081e7SThomas Gleixner #include <linux/tick.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
198b48463fSLv Zheng #include <linux/acpi.h>
202a606a18SStephane Eranian #include <linux/perf_event.h>
21bc83ccccSH. Peter Anvin #include <asm/mwait.h>
22e311404fSJuergen Gross #include <xen/xen.h>
238e0af514SShaohua Li
24a40770a9SDan Carpenter #define ACPI_PROCESSOR_AGGREGATOR_CLASS "acpi_pad"
258e0af514SShaohua Li #define ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME "Processor Aggregator"
268e0af514SShaohua Li #define ACPI_PROCESSOR_AGGREGATOR_NOTIFY 0x80
278e0af514SShaohua Li static DEFINE_MUTEX(isolated_cpus_lock);
285f160126SStuart Hayes static DEFINE_MUTEX(round_robin_lock);
298e0af514SShaohua Li
308e0af514SShaohua Li static unsigned long power_saving_mwait_eax;
310dc698b9SVenkatesh Pallipadi
320dc698b9SVenkatesh Pallipadi static unsigned char tsc_detected_unstable;
330dc698b9SVenkatesh Pallipadi static unsigned char tsc_marked_unstable;
340dc698b9SVenkatesh Pallipadi
power_saving_mwait_init(void)358e0af514SShaohua Li static void power_saving_mwait_init(void)
368e0af514SShaohua Li {
378e0af514SShaohua Li unsigned int eax, ebx, ecx, edx;
388e0af514SShaohua Li unsigned int highest_cstate = 0;
398e0af514SShaohua Li unsigned int highest_subcstate = 0;
408e0af514SShaohua Li int i;
418e0af514SShaohua Li
428e0af514SShaohua Li if (!boot_cpu_has(X86_FEATURE_MWAIT))
438e0af514SShaohua Li return;
448e0af514SShaohua Li if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
458e0af514SShaohua Li return;
468e0af514SShaohua Li
478e0af514SShaohua Li cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
488e0af514SShaohua Li
498e0af514SShaohua Li if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
508e0af514SShaohua Li !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
518e0af514SShaohua Li return;
528e0af514SShaohua Li
538e0af514SShaohua Li edx >>= MWAIT_SUBSTATE_SIZE;
548e0af514SShaohua Li for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
558e0af514SShaohua Li if (edx & MWAIT_SUBSTATE_MASK) {
568e0af514SShaohua Li highest_cstate = i;
578e0af514SShaohua Li highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
588e0af514SShaohua Li }
598e0af514SShaohua Li }
608e0af514SShaohua Li power_saving_mwait_eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
618e0af514SShaohua Li (highest_subcstate - 1);
628e0af514SShaohua Li
63592913ecSJohn Stultz #if defined(CONFIG_X86)
648e0af514SShaohua Li switch (boot_cpu_data.x86_vendor) {
657377ed4bSPu Wen case X86_VENDOR_HYGON:
668e0af514SShaohua Li case X86_VENDOR_AMD:
678e0af514SShaohua Li case X86_VENDOR_INTEL:
68773b2f30STony W Wang-oc case X86_VENDOR_ZHAOXIN:
69b72f301cSTony W Wang-oc case X86_VENDOR_CENTAUR:
708e0af514SShaohua Li /*
718e0af514SShaohua Li * AMD Fam10h TSC will tick in all
728e0af514SShaohua Li * C/P/S0/S1 states when this bit is set.
738e0af514SShaohua Li */
748aa4b14eSChen Gong if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
750dc698b9SVenkatesh Pallipadi tsc_detected_unstable = 1;
768aa4b14eSChen Gong break;
778aa4b14eSChen Gong default:
783ff70551SThomas Gleixner /* TSC could halt in idle */
798aa4b14eSChen Gong tsc_detected_unstable = 1;
808e0af514SShaohua Li }
818e0af514SShaohua Li #endif
828e0af514SShaohua Li }
838e0af514SShaohua Li
848e0af514SShaohua Li static unsigned long cpu_weight[NR_CPUS];
858e0af514SShaohua Li static int tsk_in_cpu[NR_CPUS] = {[0 ... NR_CPUS-1] = -1};
868e0af514SShaohua Li static DECLARE_BITMAP(pad_busy_cpus_bits, NR_CPUS);
round_robin_cpu(unsigned int tsk_index)878e0af514SShaohua Li static void round_robin_cpu(unsigned int tsk_index)
888e0af514SShaohua Li {
898e0af514SShaohua Li struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits);
908e0af514SShaohua Li cpumask_var_t tmp;
918e0af514SShaohua Li int cpu;
92f67538f8SAndrew Morton unsigned long min_weight = -1;
93134043cdSJason Yan unsigned long preferred_cpu;
948e0af514SShaohua Li
958e0af514SShaohua Li if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
968e0af514SShaohua Li return;
978e0af514SShaohua Li
985f160126SStuart Hayes mutex_lock(&round_robin_lock);
998e0af514SShaohua Li cpumask_clear(tmp);
1008e0af514SShaohua Li for_each_cpu(cpu, pad_busy_cpus)
10106931e62SBartosz Golaszewski cpumask_or(tmp, tmp, topology_sibling_cpumask(cpu));
1028e0af514SShaohua Li cpumask_andnot(tmp, cpu_online_mask, tmp);
1038e0af514SShaohua Li /* avoid HT sibilings if possible */
1048e0af514SShaohua Li if (cpumask_empty(tmp))
1058e0af514SShaohua Li cpumask_andnot(tmp, cpu_online_mask, pad_busy_cpus);
1068e0af514SShaohua Li if (cpumask_empty(tmp)) {
1075f160126SStuart Hayes mutex_unlock(&round_robin_lock);
1088b29d29aSLenny Szubowicz free_cpumask_var(tmp);
1098e0af514SShaohua Li return;
1108e0af514SShaohua Li }
1118e0af514SShaohua Li for_each_cpu(cpu, tmp) {
1128e0af514SShaohua Li if (cpu_weight[cpu] < min_weight) {
1138e0af514SShaohua Li min_weight = cpu_weight[cpu];
1148e0af514SShaohua Li preferred_cpu = cpu;
1158e0af514SShaohua Li }
1168e0af514SShaohua Li }
1178e0af514SShaohua Li
1188e0af514SShaohua Li if (tsk_in_cpu[tsk_index] != -1)
1198e0af514SShaohua Li cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus);
1208e0af514SShaohua Li tsk_in_cpu[tsk_index] = preferred_cpu;
1218e0af514SShaohua Li cpumask_set_cpu(preferred_cpu, pad_busy_cpus);
1228e0af514SShaohua Li cpu_weight[preferred_cpu]++;
1235f160126SStuart Hayes mutex_unlock(&round_robin_lock);
1248e0af514SShaohua Li
1258e0af514SShaohua Li set_cpus_allowed_ptr(current, cpumask_of(preferred_cpu));
1268b29d29aSLenny Szubowicz
1278b29d29aSLenny Szubowicz free_cpumask_var(tmp);
1288e0af514SShaohua Li }
1298e0af514SShaohua Li
exit_round_robin(unsigned int tsk_index)1308e0af514SShaohua Li static void exit_round_robin(unsigned int tsk_index)
1318e0af514SShaohua Li {
1328e0af514SShaohua Li struct cpumask *pad_busy_cpus = to_cpumask(pad_busy_cpus_bits);
133c8eb628cSXiaofei Tan
134*68a8e457SSeiji Nishikawa if (tsk_in_cpu[tsk_index] != -1) {
1358e0af514SShaohua Li cpumask_clear_cpu(tsk_in_cpu[tsk_index], pad_busy_cpus);
1368e0af514SShaohua Li tsk_in_cpu[tsk_index] = -1;
1378e0af514SShaohua Li }
138*68a8e457SSeiji Nishikawa }
1398e0af514SShaohua Li
1408e0af514SShaohua Li static unsigned int idle_pct = 5; /* percentage */
141fa7584e1SLen Brown static unsigned int round_robin_time = 1; /* second */
power_saving_thread(void * data)1428e0af514SShaohua Li static int power_saving_thread(void *data)
1438e0af514SShaohua Li {
1448e0af514SShaohua Li int do_sleep;
1458e0af514SShaohua Li unsigned int tsk_index = (unsigned long)data;
1468e0af514SShaohua Li u64 last_jiffies = 0;
1478e0af514SShaohua Li
1484ca6c1a0SPeter Zijlstra sched_set_fifo_low(current);
1498e0af514SShaohua Li
1508e0af514SShaohua Li while (!kthread_should_stop()) {
1514ff248f3SManuel Schölling unsigned long expire_time;
1528e0af514SShaohua Li
1538e0af514SShaohua Li /* round robin to cpus */
1544ff248f3SManuel Schölling expire_time = last_jiffies + round_robin_time * HZ;
1554ff248f3SManuel Schölling if (time_before(expire_time, jiffies)) {
1568e0af514SShaohua Li last_jiffies = jiffies;
1578e0af514SShaohua Li round_robin_cpu(tsk_index);
1588e0af514SShaohua Li }
1598e0af514SShaohua Li
1608e0af514SShaohua Li do_sleep = 0;
1618e0af514SShaohua Li
1628e0af514SShaohua Li expire_time = jiffies + HZ * (100 - idle_pct) / 100;
1638e0af514SShaohua Li
1648e0af514SShaohua Li while (!need_resched()) {
1650dc698b9SVenkatesh Pallipadi if (tsc_detected_unstable && !tsc_marked_unstable) {
1660dc698b9SVenkatesh Pallipadi /* TSC could halt in idle, so notify users */
1670dc698b9SVenkatesh Pallipadi mark_tsc_unstable("TSC halts in idle");
1680dc698b9SVenkatesh Pallipadi tsc_marked_unstable = 1;
1690dc698b9SVenkatesh Pallipadi }
1708e0af514SShaohua Li local_irq_disable();
1712a606a18SStephane Eranian
1722a606a18SStephane Eranian perf_lopwr_cb(true);
1732a606a18SStephane Eranian
174979081e7SThomas Gleixner tick_broadcast_enable();
175c7952135SThomas Gleixner tick_broadcast_enter();
1768e0af514SShaohua Li stop_critical_timings();
1778e0af514SShaohua Li
17816824255SPeter Zijlstra mwait_idle_with_hints(power_saving_mwait_eax, 1);
1798e0af514SShaohua Li
1808e0af514SShaohua Li start_critical_timings();
181c7952135SThomas Gleixner tick_broadcast_exit();
1822a606a18SStephane Eranian
1832a606a18SStephane Eranian perf_lopwr_cb(false);
1842a606a18SStephane Eranian
1858e0af514SShaohua Li local_irq_enable();
1868e0af514SShaohua Li
1874ff248f3SManuel Schölling if (time_before(expire_time, jiffies)) {
1888e0af514SShaohua Li do_sleep = 1;
1898e0af514SShaohua Li break;
1908e0af514SShaohua Li }
1918e0af514SShaohua Li }
1928e0af514SShaohua Li
1938e0af514SShaohua Li /*
1948e0af514SShaohua Li * current sched_rt has threshold for rt task running time.
1958e0af514SShaohua Li * When a rt task uses 95% CPU time, the rt thread will be
1968e0af514SShaohua Li * scheduled out for 5% CPU time to not starve other tasks. But
1978e0af514SShaohua Li * the mechanism only works when all CPUs have RT task running,
1988e0af514SShaohua Li * as if one CPU hasn't RT task, RT task from other CPUs will
1998e0af514SShaohua Li * borrow CPU time from this CPU and cause RT task use > 95%
2003b8cb427SChen Gong * CPU time. To make 'avoid starvation' work, takes a nap here.
2018e0af514SShaohua Li */
2025b59c69eSTony Camuso if (unlikely(do_sleep))
2038e0af514SShaohua Li schedule_timeout_killable(HZ * idle_pct / 100);
2045b59c69eSTony Camuso
2055b59c69eSTony Camuso /* If an external event has set the need_resched flag, then
2065b59c69eSTony Camuso * we need to deal with it, or this loop will continue to
2075b59c69eSTony Camuso * spin without calling __mwait().
2085b59c69eSTony Camuso */
2095b59c69eSTony Camuso if (unlikely(need_resched()))
2105b59c69eSTony Camuso schedule();
2118e0af514SShaohua Li }
2128e0af514SShaohua Li
2138e0af514SShaohua Li exit_round_robin(tsk_index);
2148e0af514SShaohua Li return 0;
2158e0af514SShaohua Li }
2168e0af514SShaohua Li
2178e0af514SShaohua Li static struct task_struct *ps_tsks[NR_CPUS];
2188e0af514SShaohua Li static unsigned int ps_tsk_num;
create_power_saving_task(void)2198e0af514SShaohua Li static int create_power_saving_task(void)
2208e0af514SShaohua Li {
2215d7e4386SRusty Russell int rc;
2223b8cb427SChen Gong
2238e0af514SShaohua Li ps_tsks[ps_tsk_num] = kthread_run(power_saving_thread,
2248e0af514SShaohua Li (void *)(unsigned long)ps_tsk_num,
225150ed86fSLen Brown "acpi_pad/%d", ps_tsk_num);
2265d7e4386SRusty Russell
2275d7e4386SRusty Russell if (IS_ERR(ps_tsks[ps_tsk_num])) {
2285d7e4386SRusty Russell rc = PTR_ERR(ps_tsks[ps_tsk_num]);
2293b8cb427SChen Gong ps_tsks[ps_tsk_num] = NULL;
2305d7e4386SRusty Russell } else {
2315d7e4386SRusty Russell rc = 0;
2325d7e4386SRusty Russell ps_tsk_num++;
2335d7e4386SRusty Russell }
2343b8cb427SChen Gong
2353b8cb427SChen Gong return rc;
2368e0af514SShaohua Li }
2378e0af514SShaohua Li
destroy_power_saving_task(void)2388e0af514SShaohua Li static void destroy_power_saving_task(void)
2398e0af514SShaohua Li {
2408e0af514SShaohua Li if (ps_tsk_num > 0) {
2418e0af514SShaohua Li ps_tsk_num--;
2428e0af514SShaohua Li kthread_stop(ps_tsks[ps_tsk_num]);
2433b8cb427SChen Gong ps_tsks[ps_tsk_num] = NULL;
2448e0af514SShaohua Li }
2458e0af514SShaohua Li }
2468e0af514SShaohua Li
set_power_saving_task_num(unsigned int num)2478e0af514SShaohua Li static void set_power_saving_task_num(unsigned int num)
2488e0af514SShaohua Li {
2498e0af514SShaohua Li if (num > ps_tsk_num) {
2508e0af514SShaohua Li while (ps_tsk_num < num) {
2518e0af514SShaohua Li if (create_power_saving_task())
2528e0af514SShaohua Li return;
2538e0af514SShaohua Li }
2548e0af514SShaohua Li } else if (num < ps_tsk_num) {
2558e0af514SShaohua Li while (ps_tsk_num > num)
2568e0af514SShaohua Li destroy_power_saving_task();
2578e0af514SShaohua Li }
2588e0af514SShaohua Li }
2598e0af514SShaohua Li
acpi_pad_idle_cpus(unsigned int num_cpus)2603b8cb427SChen Gong static void acpi_pad_idle_cpus(unsigned int num_cpus)
2618e0af514SShaohua Li {
26295ac7067SSebastian Andrzej Siewior cpus_read_lock();
2638e0af514SShaohua Li
2648e0af514SShaohua Li num_cpus = min_t(unsigned int, num_cpus, num_online_cpus());
2658e0af514SShaohua Li set_power_saving_task_num(num_cpus);
2668e0af514SShaohua Li
26795ac7067SSebastian Andrzej Siewior cpus_read_unlock();
2688e0af514SShaohua Li }
2698e0af514SShaohua Li
acpi_pad_idle_cpus_num(void)2708e0af514SShaohua Li static uint32_t acpi_pad_idle_cpus_num(void)
2718e0af514SShaohua Li {
2728e0af514SShaohua Li return ps_tsk_num;
2738e0af514SShaohua Li }
2748e0af514SShaohua Li
rrtime_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2750f39ee83SDwaipayan Ray static ssize_t rrtime_store(struct device *dev,
2768e0af514SShaohua Li struct device_attribute *attr, const char *buf, size_t count)
2778e0af514SShaohua Li {
2788e0af514SShaohua Li unsigned long num;
279c8eb628cSXiaofei Tan
28073d4511aSJosh if (kstrtoul(buf, 0, &num))
2818e0af514SShaohua Li return -EINVAL;
2828e0af514SShaohua Li if (num < 1 || num >= 100)
2838e0af514SShaohua Li return -EINVAL;
2848e0af514SShaohua Li mutex_lock(&isolated_cpus_lock);
2858e0af514SShaohua Li round_robin_time = num;
2868e0af514SShaohua Li mutex_unlock(&isolated_cpus_lock);
2878e0af514SShaohua Li return count;
2888e0af514SShaohua Li }
2898e0af514SShaohua Li
rrtime_show(struct device * dev,struct device_attribute * attr,char * buf)2900f39ee83SDwaipayan Ray static ssize_t rrtime_show(struct device *dev,
2918e0af514SShaohua Li struct device_attribute *attr, char *buf)
2928e0af514SShaohua Li {
29392266c65Sye xingchen return sysfs_emit(buf, "%d\n", round_robin_time);
2948e0af514SShaohua Li }
2950f39ee83SDwaipayan Ray static DEVICE_ATTR_RW(rrtime);
2968e0af514SShaohua Li
idlepct_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2970f39ee83SDwaipayan Ray static ssize_t idlepct_store(struct device *dev,
2988e0af514SShaohua Li struct device_attribute *attr, const char *buf, size_t count)
2998e0af514SShaohua Li {
3008e0af514SShaohua Li unsigned long num;
301c8eb628cSXiaofei Tan
30273d4511aSJosh if (kstrtoul(buf, 0, &num))
3038e0af514SShaohua Li return -EINVAL;
3048e0af514SShaohua Li if (num < 1 || num >= 100)
3058e0af514SShaohua Li return -EINVAL;
3068e0af514SShaohua Li mutex_lock(&isolated_cpus_lock);
3078e0af514SShaohua Li idle_pct = num;
3088e0af514SShaohua Li mutex_unlock(&isolated_cpus_lock);
3098e0af514SShaohua Li return count;
3108e0af514SShaohua Li }
3118e0af514SShaohua Li
idlepct_show(struct device * dev,struct device_attribute * attr,char * buf)3120f39ee83SDwaipayan Ray static ssize_t idlepct_show(struct device *dev,
3138e0af514SShaohua Li struct device_attribute *attr, char *buf)
3148e0af514SShaohua Li {
31592266c65Sye xingchen return sysfs_emit(buf, "%d\n", idle_pct);
3168e0af514SShaohua Li }
3170f39ee83SDwaipayan Ray static DEVICE_ATTR_RW(idlepct);
3188e0af514SShaohua Li
idlecpus_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3190f39ee83SDwaipayan Ray static ssize_t idlecpus_store(struct device *dev,
3208e0af514SShaohua Li struct device_attribute *attr, const char *buf, size_t count)
3218e0af514SShaohua Li {
3228e0af514SShaohua Li unsigned long num;
323c8eb628cSXiaofei Tan
32473d4511aSJosh if (kstrtoul(buf, 0, &num))
3258e0af514SShaohua Li return -EINVAL;
3268e0af514SShaohua Li mutex_lock(&isolated_cpus_lock);
3278e0af514SShaohua Li acpi_pad_idle_cpus(num);
3288e0af514SShaohua Li mutex_unlock(&isolated_cpus_lock);
3298e0af514SShaohua Li return count;
3308e0af514SShaohua Li }
3318e0af514SShaohua Li
idlecpus_show(struct device * dev,struct device_attribute * attr,char * buf)3320f39ee83SDwaipayan Ray static ssize_t idlecpus_show(struct device *dev,
3338e0af514SShaohua Li struct device_attribute *attr, char *buf)
3348e0af514SShaohua Li {
3355aaba363SSudeep Holla return cpumap_print_to_pagebuf(false, buf,
3365aaba363SSudeep Holla to_cpumask(pad_busy_cpus_bits));
3378e0af514SShaohua Li }
3385aaba363SSudeep Holla
3390f39ee83SDwaipayan Ray static DEVICE_ATTR_RW(idlecpus);
3408e0af514SShaohua Li
acpi_pad_add_sysfs(struct acpi_device * device)3418e0af514SShaohua Li static int acpi_pad_add_sysfs(struct acpi_device *device)
3428e0af514SShaohua Li {
3438e0af514SShaohua Li int result;
3448e0af514SShaohua Li
3458e0af514SShaohua Li result = device_create_file(&device->dev, &dev_attr_idlecpus);
3468e0af514SShaohua Li if (result)
3478e0af514SShaohua Li return -ENODEV;
3488e0af514SShaohua Li result = device_create_file(&device->dev, &dev_attr_idlepct);
3498e0af514SShaohua Li if (result) {
3508e0af514SShaohua Li device_remove_file(&device->dev, &dev_attr_idlecpus);
3518e0af514SShaohua Li return -ENODEV;
3528e0af514SShaohua Li }
3538e0af514SShaohua Li result = device_create_file(&device->dev, &dev_attr_rrtime);
3548e0af514SShaohua Li if (result) {
3558e0af514SShaohua Li device_remove_file(&device->dev, &dev_attr_idlecpus);
3568e0af514SShaohua Li device_remove_file(&device->dev, &dev_attr_idlepct);
3578e0af514SShaohua Li return -ENODEV;
3588e0af514SShaohua Li }
3598e0af514SShaohua Li return 0;
3608e0af514SShaohua Li }
3618e0af514SShaohua Li
acpi_pad_remove_sysfs(struct acpi_device * device)3628e0af514SShaohua Li static void acpi_pad_remove_sysfs(struct acpi_device *device)
3638e0af514SShaohua Li {
3648e0af514SShaohua Li device_remove_file(&device->dev, &dev_attr_idlecpus);
3658e0af514SShaohua Li device_remove_file(&device->dev, &dev_attr_idlepct);
3668e0af514SShaohua Li device_remove_file(&device->dev, &dev_attr_rrtime);
3678e0af514SShaohua Li }
3688e0af514SShaohua Li
369c9ad8e06SLen Brown /*
370c9ad8e06SLen Brown * Query firmware how many CPUs should be idle
371c9ad8e06SLen Brown * return -1 on failure
372c9ad8e06SLen Brown */
acpi_pad_pur(acpi_handle handle)373c9ad8e06SLen Brown static int acpi_pad_pur(acpi_handle handle)
3748e0af514SShaohua Li {
3758e0af514SShaohua Li struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
3768e0af514SShaohua Li union acpi_object *package;
377c9ad8e06SLen Brown int num = -1;
3788e0af514SShaohua Li
3793b8cb427SChen Gong if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PUR", NULL, &buffer)))
380c9ad8e06SLen Brown return num;
3813b8cb427SChen Gong
3823b8cb427SChen Gong if (!buffer.length || !buffer.pointer)
383c9ad8e06SLen Brown return num;
3843b8cb427SChen Gong
3858e0af514SShaohua Li package = buffer.pointer;
386c9ad8e06SLen Brown
387c9ad8e06SLen Brown if (package->type == ACPI_TYPE_PACKAGE &&
388c9ad8e06SLen Brown package->package.count == 2 &&
389c9ad8e06SLen Brown package->package.elements[0].integer.value == 1) /* rev 1 */
390c9ad8e06SLen Brown
3918e0af514SShaohua Li num = package->package.elements[1].integer.value;
392c9ad8e06SLen Brown
3938e0af514SShaohua Li kfree(buffer.pointer);
394c9ad8e06SLen Brown return num;
3958e0af514SShaohua Li }
3968e0af514SShaohua Li
acpi_pad_handle_notify(acpi_handle handle)3978e0af514SShaohua Li static void acpi_pad_handle_notify(acpi_handle handle)
3988e0af514SShaohua Li {
3993b8cb427SChen Gong int num_cpus;
4008e0af514SShaohua Li uint32_t idle_cpus;
4018b296d94SJiang Liu struct acpi_buffer param = {
4028b296d94SJiang Liu .length = 4,
4038b296d94SJiang Liu .pointer = (void *)&idle_cpus,
4048b296d94SJiang Liu };
4058e0af514SShaohua Li
4068e0af514SShaohua Li mutex_lock(&isolated_cpus_lock);
407c9ad8e06SLen Brown num_cpus = acpi_pad_pur(handle);
408c9ad8e06SLen Brown if (num_cpus < 0) {
4098e0af514SShaohua Li mutex_unlock(&isolated_cpus_lock);
4108e0af514SShaohua Li return;
4118e0af514SShaohua Li }
4123b8cb427SChen Gong acpi_pad_idle_cpus(num_cpus);
4138e0af514SShaohua Li idle_cpus = acpi_pad_idle_cpus_num();
4148b296d94SJiang Liu acpi_evaluate_ost(handle, ACPI_PROCESSOR_AGGREGATOR_NOTIFY, 0, ¶m);
4158e0af514SShaohua Li mutex_unlock(&isolated_cpus_lock);
4168e0af514SShaohua Li }
4178e0af514SShaohua Li
acpi_pad_notify(acpi_handle handle,u32 event,void * data)4188e0af514SShaohua Li static void acpi_pad_notify(acpi_handle handle, u32 event,
4198e0af514SShaohua Li void *data)
4208e0af514SShaohua Li {
4218e0af514SShaohua Li struct acpi_device *device = data;
4228e0af514SShaohua Li
4238e0af514SShaohua Li switch (event) {
4248e0af514SShaohua Li case ACPI_PROCESSOR_AGGREGATOR_NOTIFY:
4258e0af514SShaohua Li acpi_pad_handle_notify(handle);
4268e0af514SShaohua Li acpi_bus_generate_netlink_event(device->pnp.device_class,
4278e0af514SShaohua Li dev_name(&device->dev), event, 0);
4288e0af514SShaohua Li break;
4298e0af514SShaohua Li default:
43073d4511aSJosh pr_warn("Unsupported event [0x%x]\n", event);
4318e0af514SShaohua Li break;
4328e0af514SShaohua Li }
4338e0af514SShaohua Li }
4348e0af514SShaohua Li
acpi_pad_add(struct acpi_device * device)4358e0af514SShaohua Li static int acpi_pad_add(struct acpi_device *device)
4368e0af514SShaohua Li {
4378e0af514SShaohua Li acpi_status status;
4388e0af514SShaohua Li
4398e0af514SShaohua Li strcpy(acpi_device_name(device), ACPI_PROCESSOR_AGGREGATOR_DEVICE_NAME);
4408e0af514SShaohua Li strcpy(acpi_device_class(device), ACPI_PROCESSOR_AGGREGATOR_CLASS);
4418e0af514SShaohua Li
4428e0af514SShaohua Li if (acpi_pad_add_sysfs(device))
4438e0af514SShaohua Li return -ENODEV;
4448e0af514SShaohua Li
4458e0af514SShaohua Li status = acpi_install_notify_handler(device->handle,
4468e0af514SShaohua Li ACPI_DEVICE_NOTIFY, acpi_pad_notify, device);
4478e0af514SShaohua Li if (ACPI_FAILURE(status)) {
4488e0af514SShaohua Li acpi_pad_remove_sysfs(device);
4498e0af514SShaohua Li return -ENODEV;
4508e0af514SShaohua Li }
4518e0af514SShaohua Li
4528e0af514SShaohua Li return 0;
4538e0af514SShaohua Li }
4548e0af514SShaohua Li
acpi_pad_remove(struct acpi_device * device)4556c0eb5baSDawei Li static void acpi_pad_remove(struct acpi_device *device)
4568e0af514SShaohua Li {
4578e0af514SShaohua Li mutex_lock(&isolated_cpus_lock);
4588e0af514SShaohua Li acpi_pad_idle_cpus(0);
4598e0af514SShaohua Li mutex_unlock(&isolated_cpus_lock);
4608e0af514SShaohua Li
4618e0af514SShaohua Li acpi_remove_notify_handler(device->handle,
4628e0af514SShaohua Li ACPI_DEVICE_NOTIFY, acpi_pad_notify);
4638e0af514SShaohua Li acpi_pad_remove_sysfs(device);
4648e0af514SShaohua Li }
4658e0af514SShaohua Li
4668e0af514SShaohua Li static const struct acpi_device_id pad_device_ids[] = {
4678e0af514SShaohua Li {"ACPI000C", 0},
4688e0af514SShaohua Li {"", 0},
4698e0af514SShaohua Li };
4708e0af514SShaohua Li MODULE_DEVICE_TABLE(acpi, pad_device_ids);
4718e0af514SShaohua Li
4728e0af514SShaohua Li static struct acpi_driver acpi_pad_driver = {
4738e0af514SShaohua Li .name = "processor_aggregator",
4748e0af514SShaohua Li .class = ACPI_PROCESSOR_AGGREGATOR_CLASS,
4758e0af514SShaohua Li .ids = pad_device_ids,
4768e0af514SShaohua Li .ops = {
4778e0af514SShaohua Li .add = acpi_pad_add,
4788e0af514SShaohua Li .remove = acpi_pad_remove,
4798e0af514SShaohua Li },
4808e0af514SShaohua Li };
4818e0af514SShaohua Li
acpi_pad_init(void)4828e0af514SShaohua Li static int __init acpi_pad_init(void)
4838e0af514SShaohua Li {
484e311404fSJuergen Gross /* Xen ACPI PAD is used when running as Xen Dom0. */
485e311404fSJuergen Gross if (xen_initial_domain())
486e311404fSJuergen Gross return -ENODEV;
487e311404fSJuergen Gross
4888e0af514SShaohua Li power_saving_mwait_init();
4898e0af514SShaohua Li if (power_saving_mwait_eax == 0)
4908e0af514SShaohua Li return -EINVAL;
4918e0af514SShaohua Li
4928e0af514SShaohua Li return acpi_bus_register_driver(&acpi_pad_driver);
4938e0af514SShaohua Li }
4948e0af514SShaohua Li
acpi_pad_exit(void)4958e0af514SShaohua Li static void __exit acpi_pad_exit(void)
4968e0af514SShaohua Li {
4978e0af514SShaohua Li acpi_bus_unregister_driver(&acpi_pad_driver);
4988e0af514SShaohua Li }
4998e0af514SShaohua Li
5008e0af514SShaohua Li module_init(acpi_pad_init);
5018e0af514SShaohua Li module_exit(acpi_pad_exit);
5028e0af514SShaohua Li MODULE_AUTHOR("Shaohua Li<shaohua.li@intel.com>");
5038e0af514SShaohua Li MODULE_DESCRIPTION("ACPI Processor Aggregator Driver");
5048e0af514SShaohua Li MODULE_LICENSE("GPL");
505