^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * cpuidle driver for haltpoll governor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2019 Red Hat, Inc. and/or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This work is licensed under the terms of the GNU GPL, version 2. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * the COPYING file in the top-level directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Authors: Marcelo Tosatti <mtosatti@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sched/idle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kvm_para.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/cpuidle_haltpoll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static bool force __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) module_param(force, bool, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_PARM_DESC(force, "Load unconditionally");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static struct cpuidle_device __percpu *haltpoll_cpuidle_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static enum cpuhp_state haltpoll_hp_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int default_enter_idle(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct cpuidle_driver *drv, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (current_clr_polling_and_test()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) default_idle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static struct cpuidle_driver haltpoll_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .name = "haltpoll",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .governor = "haltpoll",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) .states = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) { /* entry 0 is for polling */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .enter = default_enter_idle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .exit_latency = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .target_residency = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .power_usage = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .name = "haltpoll idle",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .desc = "default architecture idle",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .safe_state_index = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .state_count = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int haltpoll_cpu_online(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!dev->registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) dev->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (cpuidle_register_device(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_notice("cpuidle_register_device %d failed!\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) arch_haltpoll_enable(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int haltpoll_cpu_offline(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (dev->registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) arch_haltpoll_disable(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) cpuidle_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static void haltpoll_uninit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (haltpoll_hp_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) cpuhp_remove_state(haltpoll_hp_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) cpuidle_unregister_driver(&haltpoll_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) free_percpu(haltpoll_cpuidle_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) haltpoll_cpuidle_devices = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static bool haltpoll_want(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return kvm_para_has_hint(KVM_HINTS_REALTIME) || force;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int __init haltpoll_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct cpuidle_driver *drv = &haltpoll_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Do not load haltpoll if idle= is passed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (boot_option_idle_override != IDLE_NO_OVERRIDE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) cpuidle_poll_state_init(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!kvm_para_available() || !haltpoll_want())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ret = cpuidle_register_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) haltpoll_cpuidle_devices = alloc_percpu(struct cpuidle_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (haltpoll_cpuidle_devices == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) cpuidle_unregister_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpuidle/haltpoll:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) haltpoll_cpu_online, haltpoll_cpu_offline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) haltpoll_uninit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) haltpoll_hp_state = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void __exit haltpoll_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) haltpoll_uninit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) module_init(haltpoll_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) module_exit(haltpoll_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");