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 struct cpuidle_device __percpu
*haltpoll_cpuidle_devices
;
22 static enum cpuhp_state haltpoll_hp_state
;
24 static int default_enter_idle(struct cpuidle_device
*dev
,
25 struct cpuidle_driver
*drv
, int index
)
27 if (current_clr_polling_and_test()) {
35 static struct cpuidle_driver haltpoll_driver
= {
37 .governor
= "haltpoll",
39 { /* entry 0 is for polling */ },
41 .enter
= default_enter_idle
,
43 .target_residency
= 1,
45 .name
= "haltpoll idle",
46 .desc
= "default architecture idle",
49 .safe_state_index
= 0,
53 static int haltpoll_cpu_online(unsigned int cpu
)
55 struct cpuidle_device
*dev
;
57 dev
= per_cpu_ptr(haltpoll_cpuidle_devices
, cpu
);
58 if (!dev
->registered
) {
60 if (cpuidle_register_device(dev
)) {
61 pr_notice("cpuidle_register_device %d failed!\n", cpu
);
64 arch_haltpoll_enable(cpu
);
70 static int haltpoll_cpu_offline(unsigned int cpu
)
72 struct cpuidle_device
*dev
;
74 dev
= per_cpu_ptr(haltpoll_cpuidle_devices
, cpu
);
75 if (dev
->registered
) {
76 arch_haltpoll_disable(cpu
);
77 cpuidle_unregister_device(dev
);
83 static void haltpoll_uninit(void)
85 if (haltpoll_hp_state
)
86 cpuhp_remove_state(haltpoll_hp_state
);
87 cpuidle_unregister_driver(&haltpoll_driver
);
89 free_percpu(haltpoll_cpuidle_devices
);
90 haltpoll_cpuidle_devices
= NULL
;
93 static int __init
haltpoll_init(void)
96 struct cpuidle_driver
*drv
= &haltpoll_driver
;
98 /* Do not load haltpoll if idle= is passed */
99 if (boot_option_idle_override
!= IDLE_NO_OVERRIDE
)
102 cpuidle_poll_state_init(drv
);
104 if (!kvm_para_available() ||
105 !kvm_para_has_hint(KVM_HINTS_REALTIME
))
108 ret
= cpuidle_register_driver(drv
);
112 haltpoll_cpuidle_devices
= alloc_percpu(struct cpuidle_device
);
113 if (haltpoll_cpuidle_devices
== NULL
) {
114 cpuidle_unregister_driver(drv
);
118 ret
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "cpuidle/haltpoll:online",
119 haltpoll_cpu_online
, haltpoll_cpu_offline
);
123 haltpoll_hp_state
= ret
;
130 static void __exit
haltpoll_exit(void)
135 module_init(haltpoll_init
);
136 module_exit(haltpoll_exit
);
137 MODULE_LICENSE("GPL");
138 MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");