blob: 932390b028f1df148d8f96bd6a5b1b6b36fcec72 [file] [log] [blame]
Marcelo Tosattifa86ee92019-07-03 20:51:25 -03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * cpuidle driver for haltpoll governor.
4 *
5 * Copyright 2019 Red Hat, Inc. and/or its affiliates.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
9 *
10 * Authors: Marcelo Tosatti <mtosatti@redhat.com>
11 */
12
13#include <linux/init.h>
Joao Martins97d3eb92019-09-02 11:40:31 +010014#include <linux/cpu.h>
Marcelo Tosattifa86ee92019-07-03 20:51:25 -030015#include <linux/cpuidle.h>
16#include <linux/module.h>
17#include <linux/sched/idle.h>
18#include <linux/kvm_para.h>
Marcelo Tosattia1c44232019-07-03 20:51:29 -030019#include <linux/cpuidle_haltpoll.h>
Marcelo Tosattifa86ee92019-07-03 20:51:25 -030020
Joao Martins97d3eb92019-09-02 11:40:31 +010021static struct cpuidle_device __percpu *haltpoll_cpuidle_devices;
22static enum cpuhp_state haltpoll_hp_state;
23
Marcelo Tosattifa86ee92019-07-03 20:51:25 -030024static int default_enter_idle(struct cpuidle_device *dev,
25 struct cpuidle_driver *drv, int index)
26{
27 if (current_clr_polling_and_test()) {
28 local_irq_enable();
29 return index;
30 }
31 default_idle();
32 return index;
33}
34
35static struct cpuidle_driver haltpoll_driver = {
36 .name = "haltpoll",
Joao Martins73214402019-09-08 00:45:22 +010037 .governor = "haltpoll",
Marcelo Tosattifa86ee92019-07-03 20:51:25 -030038 .states = {
39 { /* entry 0 is for polling */ },
40 {
41 .enter = default_enter_idle,
42 .exit_latency = 1,
43 .target_residency = 1,
44 .power_usage = -1,
45 .name = "haltpoll idle",
46 .desc = "default architecture idle",
47 },
48 },
49 .safe_state_index = 0,
50 .state_count = 2,
51};
52
Joao Martins97d3eb92019-09-02 11:40:31 +010053static int haltpoll_cpu_online(unsigned int cpu)
54{
55 struct cpuidle_device *dev;
56
57 dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
58 if (!dev->registered) {
59 dev->cpu = cpu;
60 if (cpuidle_register_device(dev)) {
61 pr_notice("cpuidle_register_device %d failed!\n", cpu);
62 return -EIO;
63 }
64 arch_haltpoll_enable(cpu);
65 }
66
67 return 0;
68}
69
70static int haltpoll_cpu_offline(unsigned int cpu)
71{
72 struct cpuidle_device *dev;
73
74 dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
75 if (dev->registered) {
76 arch_haltpoll_disable(cpu);
77 cpuidle_unregister_device(dev);
78 }
79
80 return 0;
81}
82
83static void haltpoll_uninit(void)
84{
85 if (haltpoll_hp_state)
86 cpuhp_remove_state(haltpoll_hp_state);
87 cpuidle_unregister_driver(&haltpoll_driver);
88
89 free_percpu(haltpoll_cpuidle_devices);
90 haltpoll_cpuidle_devices = NULL;
91}
92
Marcelo Tosattifa86ee92019-07-03 20:51:25 -030093static int __init haltpoll_init(void)
94{
Marcelo Tosattia1c44232019-07-03 20:51:29 -030095 int ret;
Marcelo Tosattifa86ee92019-07-03 20:51:25 -030096 struct cpuidle_driver *drv = &haltpoll_driver;
97
98 cpuidle_poll_state_init(drv);
99
Wanpeng Li1328edc2019-08-29 16:49:57 +0800100 if (!kvm_para_available() ||
101 !kvm_para_has_hint(KVM_HINTS_REALTIME))
Joao Martins5cc59f52019-09-08 00:45:23 +0100102 return -ENODEV;
Marcelo Tosattifa86ee92019-07-03 20:51:25 -0300103
Joao Martins97d3eb92019-09-02 11:40:31 +0100104 ret = cpuidle_register_driver(drv);
105 if (ret < 0)
106 return ret;
107
108 haltpoll_cpuidle_devices = alloc_percpu(struct cpuidle_device);
109 if (haltpoll_cpuidle_devices == NULL) {
110 cpuidle_unregister_driver(drv);
111 return -ENOMEM;
112 }
113
114 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpuidle/haltpoll:online",
115 haltpoll_cpu_online, haltpoll_cpu_offline);
116 if (ret < 0) {
117 haltpoll_uninit();
118 } else {
119 haltpoll_hp_state = ret;
120 ret = 0;
121 }
Marcelo Tosattia1c44232019-07-03 20:51:29 -0300122
123 return ret;
Marcelo Tosattifa86ee92019-07-03 20:51:25 -0300124}
125
126static void __exit haltpoll_exit(void)
127{
Joao Martins97d3eb92019-09-02 11:40:31 +0100128 haltpoll_uninit();
Marcelo Tosattifa86ee92019-07-03 20:51:25 -0300129}
130
131module_init(haltpoll_init);
132module_exit(haltpoll_exit);
133MODULE_LICENSE("GPL");
134MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");