1 // SPDX-License-Identifier: GPL-2.0
3 * cpuidle driver for haltpoll governor.
5 * Copyright 2019 Red Hat, Inc. and/or its affiliates.
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
10 * Authors: Marcelo Tosatti <mtosatti@redhat.com>
13 #include <linux/init.h>
14 #include <linux/cpu.h>
15 #include <linux/cpuidle.h>
16 #include <linux/module.h>
17 #include <linux/sched/idle.h>
18 #include <linux/kvm_para.h>
19 #include <linux/cpuidle_haltpoll.h>
21 static bool force __read_mostly
;
22 module_param(force
, bool, 0444);
23 MODULE_PARM_DESC(force
, "Load unconditionally");
25 static struct cpuidle_device __percpu
*haltpoll_cpuidle_devices
;
26 static enum cpuhp_state haltpoll_hp_state
;
28 static __cpuidle
int default_enter_idle(struct cpuidle_device
*dev
,
29 struct cpuidle_driver
*drv
, int index
)
31 if (current_clr_polling_and_test())
38 static struct cpuidle_driver haltpoll_driver
= {
40 .governor
= "haltpoll",
42 { /* entry 0 is for polling */ },
44 .enter
= default_enter_idle
,
46 .target_residency
= 1,
48 .name
= "haltpoll idle",
49 .desc
= "default architecture idle",
52 .safe_state_index
= 0,
56 static int haltpoll_cpu_online(unsigned int cpu
)
58 struct cpuidle_device
*dev
;
60 dev
= per_cpu_ptr(haltpoll_cpuidle_devices
, cpu
);
61 if (!dev
->registered
) {
63 if (cpuidle_register_device(dev
)) {
64 pr_notice("cpuidle_register_device %d failed!\n", cpu
);
67 arch_haltpoll_enable(cpu
);
73 static int haltpoll_cpu_offline(unsigned int cpu
)
75 struct cpuidle_device
*dev
;
77 dev
= per_cpu_ptr(haltpoll_cpuidle_devices
, cpu
);
78 if (dev
->registered
) {
79 arch_haltpoll_disable(cpu
);
80 cpuidle_unregister_device(dev
);
86 static void haltpoll_uninit(void)
88 if (haltpoll_hp_state
)
89 cpuhp_remove_state(haltpoll_hp_state
);
90 cpuidle_unregister_driver(&haltpoll_driver
);
92 free_percpu(haltpoll_cpuidle_devices
);
93 haltpoll_cpuidle_devices
= NULL
;
96 static bool haltpoll_want(void)
98 return kvm_para_has_hint(KVM_HINTS_REALTIME
) || force
;
101 static int __init
haltpoll_init(void)
104 struct cpuidle_driver
*drv
= &haltpoll_driver
;
106 /* Do not load haltpoll if idle= is passed */
107 if (boot_option_idle_override
!= IDLE_NO_OVERRIDE
)
110 if (!kvm_para_available() || !haltpoll_want())
113 cpuidle_poll_state_init(drv
);
115 ret
= cpuidle_register_driver(drv
);
119 haltpoll_cpuidle_devices
= alloc_percpu(struct cpuidle_device
);
120 if (haltpoll_cpuidle_devices
== NULL
) {
121 cpuidle_unregister_driver(drv
);
125 ret
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "cpuidle/haltpoll:online",
126 haltpoll_cpu_online
, haltpoll_cpu_offline
);
130 haltpoll_hp_state
= ret
;
137 static void __exit
haltpoll_exit(void)
142 module_init(haltpoll_init
);
143 module_exit(haltpoll_exit
);
144 MODULE_DESCRIPTION("cpuidle driver for haltpoll governor");
145 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");