xref: /openbmc/linux/drivers/cpuidle/cpuidle-haltpoll.c (revision 659b66e98bb38dc6300dca3c9ebebeba194b575b)
1fa86ee90SMarcelo Tosatti // SPDX-License-Identifier: GPL-2.0
2fa86ee90SMarcelo Tosatti /*
3fa86ee90SMarcelo Tosatti  * cpuidle driver for haltpoll governor.
4fa86ee90SMarcelo Tosatti  *
5fa86ee90SMarcelo Tosatti  * Copyright 2019 Red Hat, Inc. and/or its affiliates.
6fa86ee90SMarcelo Tosatti  *
7fa86ee90SMarcelo Tosatti  * This work is licensed under the terms of the GNU GPL, version 2.  See
8fa86ee90SMarcelo Tosatti  * the COPYING file in the top-level directory.
9fa86ee90SMarcelo Tosatti  *
10fa86ee90SMarcelo Tosatti  * Authors: Marcelo Tosatti <mtosatti@redhat.com>
11fa86ee90SMarcelo Tosatti  */
12fa86ee90SMarcelo Tosatti 
13fa86ee90SMarcelo Tosatti #include <linux/init.h>
1497d3eb9dSJoao Martins #include <linux/cpu.h>
15fa86ee90SMarcelo Tosatti #include <linux/cpuidle.h>
16fa86ee90SMarcelo Tosatti #include <linux/module.h>
17fa86ee90SMarcelo Tosatti #include <linux/sched/idle.h>
18fa86ee90SMarcelo Tosatti #include <linux/kvm_para.h>
19a1c4423bSMarcelo Tosatti #include <linux/cpuidle_haltpoll.h>
20fa86ee90SMarcelo Tosatti 
21dd52551fSMaciej S. Szmigiero static bool force __read_mostly;
22dd52551fSMaciej S. Szmigiero module_param(force, bool, 0444);
23dd52551fSMaciej S. Szmigiero MODULE_PARM_DESC(force, "Load unconditionally");
24dd52551fSMaciej S. Szmigiero 
2597d3eb9dSJoao Martins static struct cpuidle_device __percpu *haltpoll_cpuidle_devices;
2697d3eb9dSJoao Martins static enum cpuhp_state haltpoll_hp_state;
2797d3eb9dSJoao Martins 
28fa86ee90SMarcelo Tosatti static int default_enter_idle(struct cpuidle_device *dev,
29fa86ee90SMarcelo Tosatti 			      struct cpuidle_driver *drv, int index)
30fa86ee90SMarcelo Tosatti {
31fa86ee90SMarcelo Tosatti 	if (current_clr_polling_and_test()) {
32fa86ee90SMarcelo Tosatti 		local_irq_enable();
33fa86ee90SMarcelo Tosatti 		return index;
34fa86ee90SMarcelo Tosatti 	}
35fa86ee90SMarcelo Tosatti 	default_idle();
36fa86ee90SMarcelo Tosatti 	return index;
37fa86ee90SMarcelo Tosatti }
38fa86ee90SMarcelo Tosatti 
39fa86ee90SMarcelo Tosatti static struct cpuidle_driver haltpoll_driver = {
40fa86ee90SMarcelo Tosatti 	.name = "haltpoll",
4173214408SJoao Martins 	.governor = "haltpoll",
42fa86ee90SMarcelo Tosatti 	.states = {
43fa86ee90SMarcelo Tosatti 		{ /* entry 0 is for polling */ },
44fa86ee90SMarcelo Tosatti 		{
45fa86ee90SMarcelo Tosatti 			.enter			= default_enter_idle,
46fa86ee90SMarcelo Tosatti 			.exit_latency		= 1,
47fa86ee90SMarcelo Tosatti 			.target_residency	= 1,
48fa86ee90SMarcelo Tosatti 			.power_usage		= -1,
49fa86ee90SMarcelo Tosatti 			.name			= "haltpoll idle",
50fa86ee90SMarcelo Tosatti 			.desc			= "default architecture idle",
51fa86ee90SMarcelo Tosatti 		},
52fa86ee90SMarcelo Tosatti 	},
53fa86ee90SMarcelo Tosatti 	.safe_state_index = 0,
54fa86ee90SMarcelo Tosatti 	.state_count = 2,
55fa86ee90SMarcelo Tosatti };
56fa86ee90SMarcelo Tosatti 
5797d3eb9dSJoao Martins static int haltpoll_cpu_online(unsigned int cpu)
5897d3eb9dSJoao Martins {
5997d3eb9dSJoao Martins 	struct cpuidle_device *dev;
6097d3eb9dSJoao Martins 
6197d3eb9dSJoao Martins 	dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
6297d3eb9dSJoao Martins 	if (!dev->registered) {
6397d3eb9dSJoao Martins 		dev->cpu = cpu;
6497d3eb9dSJoao Martins 		if (cpuidle_register_device(dev)) {
6597d3eb9dSJoao Martins 			pr_notice("cpuidle_register_device %d failed!\n", cpu);
6697d3eb9dSJoao Martins 			return -EIO;
6797d3eb9dSJoao Martins 		}
6897d3eb9dSJoao Martins 		arch_haltpoll_enable(cpu);
6997d3eb9dSJoao Martins 	}
7097d3eb9dSJoao Martins 
7197d3eb9dSJoao Martins 	return 0;
7297d3eb9dSJoao Martins }
7397d3eb9dSJoao Martins 
7497d3eb9dSJoao Martins static int haltpoll_cpu_offline(unsigned int cpu)
7597d3eb9dSJoao Martins {
7697d3eb9dSJoao Martins 	struct cpuidle_device *dev;
7797d3eb9dSJoao Martins 
7897d3eb9dSJoao Martins 	dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
7997d3eb9dSJoao Martins 	if (dev->registered) {
8097d3eb9dSJoao Martins 		arch_haltpoll_disable(cpu);
8197d3eb9dSJoao Martins 		cpuidle_unregister_device(dev);
8297d3eb9dSJoao Martins 	}
8397d3eb9dSJoao Martins 
8497d3eb9dSJoao Martins 	return 0;
8597d3eb9dSJoao Martins }
8697d3eb9dSJoao Martins 
8797d3eb9dSJoao Martins static void haltpoll_uninit(void)
8897d3eb9dSJoao Martins {
8997d3eb9dSJoao Martins 	if (haltpoll_hp_state)
9097d3eb9dSJoao Martins 		cpuhp_remove_state(haltpoll_hp_state);
9197d3eb9dSJoao Martins 	cpuidle_unregister_driver(&haltpoll_driver);
9297d3eb9dSJoao Martins 
9397d3eb9dSJoao Martins 	free_percpu(haltpoll_cpuidle_devices);
9497d3eb9dSJoao Martins 	haltpoll_cpuidle_devices = NULL;
9597d3eb9dSJoao Martins }
9697d3eb9dSJoao Martins 
974902f7fcSYihao Wu static bool haltpoll_want(void)
98dd52551fSMaciej S. Szmigiero {
99dd52551fSMaciej S. Szmigiero 	return kvm_para_has_hint(KVM_HINTS_REALTIME) || force;
100dd52551fSMaciej S. Szmigiero }
101dd52551fSMaciej S. Szmigiero 
102fa86ee90SMarcelo Tosatti static int __init haltpoll_init(void)
103fa86ee90SMarcelo Tosatti {
104a1c4423bSMarcelo Tosatti 	int ret;
105fa86ee90SMarcelo Tosatti 	struct cpuidle_driver *drv = &haltpoll_driver;
106fa86ee90SMarcelo Tosatti 
10731d85140SZhenzhong Duan 	/* Do not load haltpoll if idle= is passed */
10831d85140SZhenzhong Duan 	if (boot_option_idle_override != IDLE_NO_OVERRIDE)
10931d85140SZhenzhong Duan 		return -ENODEV;
11031d85140SZhenzhong Duan 
1114902f7fcSYihao Wu 	if (!kvm_para_available() || !haltpoll_want())
1125cc59f59SJoao Martins 		return -ENODEV;
113fa86ee90SMarcelo Tosatti 
114*659b66e9SLi RongQing 	cpuidle_poll_state_init(drv);
115*659b66e9SLi RongQing 
11697d3eb9dSJoao Martins 	ret = cpuidle_register_driver(drv);
11797d3eb9dSJoao Martins 	if (ret < 0)
11897d3eb9dSJoao Martins 		return ret;
11997d3eb9dSJoao Martins 
12097d3eb9dSJoao Martins 	haltpoll_cpuidle_devices = alloc_percpu(struct cpuidle_device);
12197d3eb9dSJoao Martins 	if (haltpoll_cpuidle_devices == NULL) {
12297d3eb9dSJoao Martins 		cpuidle_unregister_driver(drv);
12397d3eb9dSJoao Martins 		return -ENOMEM;
12497d3eb9dSJoao Martins 	}
12597d3eb9dSJoao Martins 
12697d3eb9dSJoao Martins 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpuidle/haltpoll:online",
12797d3eb9dSJoao Martins 				haltpoll_cpu_online, haltpoll_cpu_offline);
12897d3eb9dSJoao Martins 	if (ret < 0) {
12997d3eb9dSJoao Martins 		haltpoll_uninit();
13097d3eb9dSJoao Martins 	} else {
13197d3eb9dSJoao Martins 		haltpoll_hp_state = ret;
13297d3eb9dSJoao Martins 		ret = 0;
13397d3eb9dSJoao Martins 	}
134a1c4423bSMarcelo Tosatti 
135a1c4423bSMarcelo Tosatti 	return ret;
136fa86ee90SMarcelo Tosatti }
137fa86ee90SMarcelo Tosatti 
138fa86ee90SMarcelo Tosatti static void __exit haltpoll_exit(void)
139fa86ee90SMarcelo Tosatti {
14097d3eb9dSJoao Martins 	haltpoll_uninit();
141fa86ee90SMarcelo Tosatti }
142fa86ee90SMarcelo Tosatti 
143fa86ee90SMarcelo Tosatti module_init(haltpoll_init);
144fa86ee90SMarcelo Tosatti module_exit(haltpoll_exit);
145fa86ee90SMarcelo Tosatti MODULE_LICENSE("GPL");
146fa86ee90SMarcelo Tosatti MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");
147