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 28*628fb898SBorislav Petkov (AMD) static __cpuidle int default_enter_idle(struct cpuidle_device *dev, 29fa86ee90SMarcelo Tosatti struct cpuidle_driver *drv, int index) 30fa86ee90SMarcelo Tosatti { 31*628fb898SBorislav Petkov (AMD) if (current_clr_polling_and_test()) 32fa86ee90SMarcelo Tosatti return index; 33*628fb898SBorislav Petkov (AMD) 34716ff71aSLi RongQing arch_cpu_idle(); 35fa86ee90SMarcelo Tosatti return index; 36fa86ee90SMarcelo Tosatti } 37fa86ee90SMarcelo Tosatti 38fa86ee90SMarcelo Tosatti static struct cpuidle_driver haltpoll_driver = { 39fa86ee90SMarcelo Tosatti .name = "haltpoll", 4073214408SJoao Martins .governor = "haltpoll", 41fa86ee90SMarcelo Tosatti .states = { 42fa86ee90SMarcelo Tosatti { /* entry 0 is for polling */ }, 43fa86ee90SMarcelo Tosatti { 44fa86ee90SMarcelo Tosatti .enter = default_enter_idle, 45fa86ee90SMarcelo Tosatti .exit_latency = 1, 46fa86ee90SMarcelo Tosatti .target_residency = 1, 47fa86ee90SMarcelo Tosatti .power_usage = -1, 48fa86ee90SMarcelo Tosatti .name = "haltpoll idle", 49fa86ee90SMarcelo Tosatti .desc = "default architecture idle", 50fa86ee90SMarcelo Tosatti }, 51fa86ee90SMarcelo Tosatti }, 52fa86ee90SMarcelo Tosatti .safe_state_index = 0, 53fa86ee90SMarcelo Tosatti .state_count = 2, 54fa86ee90SMarcelo Tosatti }; 55fa86ee90SMarcelo Tosatti 5697d3eb9dSJoao Martins static int haltpoll_cpu_online(unsigned int cpu) 5797d3eb9dSJoao Martins { 5897d3eb9dSJoao Martins struct cpuidle_device *dev; 5997d3eb9dSJoao Martins 6097d3eb9dSJoao Martins dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu); 6197d3eb9dSJoao Martins if (!dev->registered) { 6297d3eb9dSJoao Martins dev->cpu = cpu; 6397d3eb9dSJoao Martins if (cpuidle_register_device(dev)) { 6497d3eb9dSJoao Martins pr_notice("cpuidle_register_device %d failed!\n", cpu); 6597d3eb9dSJoao Martins return -EIO; 6697d3eb9dSJoao Martins } 6797d3eb9dSJoao Martins arch_haltpoll_enable(cpu); 6897d3eb9dSJoao Martins } 6997d3eb9dSJoao Martins 7097d3eb9dSJoao Martins return 0; 7197d3eb9dSJoao Martins } 7297d3eb9dSJoao Martins 7397d3eb9dSJoao Martins static int haltpoll_cpu_offline(unsigned int cpu) 7497d3eb9dSJoao Martins { 7597d3eb9dSJoao Martins struct cpuidle_device *dev; 7697d3eb9dSJoao Martins 7797d3eb9dSJoao Martins dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu); 7897d3eb9dSJoao Martins if (dev->registered) { 7997d3eb9dSJoao Martins arch_haltpoll_disable(cpu); 8097d3eb9dSJoao Martins cpuidle_unregister_device(dev); 8197d3eb9dSJoao Martins } 8297d3eb9dSJoao Martins 8397d3eb9dSJoao Martins return 0; 8497d3eb9dSJoao Martins } 8597d3eb9dSJoao Martins 8697d3eb9dSJoao Martins static void haltpoll_uninit(void) 8797d3eb9dSJoao Martins { 8897d3eb9dSJoao Martins if (haltpoll_hp_state) 8997d3eb9dSJoao Martins cpuhp_remove_state(haltpoll_hp_state); 9097d3eb9dSJoao Martins cpuidle_unregister_driver(&haltpoll_driver); 9197d3eb9dSJoao Martins 9297d3eb9dSJoao Martins free_percpu(haltpoll_cpuidle_devices); 9397d3eb9dSJoao Martins haltpoll_cpuidle_devices = NULL; 9497d3eb9dSJoao Martins } 9597d3eb9dSJoao Martins 964902f7fcSYihao Wu static bool haltpoll_want(void) 97dd52551fSMaciej S. Szmigiero { 98dd52551fSMaciej S. Szmigiero return kvm_para_has_hint(KVM_HINTS_REALTIME) || force; 99dd52551fSMaciej S. Szmigiero } 100dd52551fSMaciej S. Szmigiero 101fa86ee90SMarcelo Tosatti static int __init haltpoll_init(void) 102fa86ee90SMarcelo Tosatti { 103a1c4423bSMarcelo Tosatti int ret; 104fa86ee90SMarcelo Tosatti struct cpuidle_driver *drv = &haltpoll_driver; 105fa86ee90SMarcelo Tosatti 10631d85140SZhenzhong Duan /* Do not load haltpoll if idle= is passed */ 10731d85140SZhenzhong Duan if (boot_option_idle_override != IDLE_NO_OVERRIDE) 10831d85140SZhenzhong Duan return -ENODEV; 10931d85140SZhenzhong Duan 1104902f7fcSYihao Wu if (!kvm_para_available() || !haltpoll_want()) 1115cc59f59SJoao Martins return -ENODEV; 112fa86ee90SMarcelo Tosatti 113659b66e9SLi RongQing cpuidle_poll_state_init(drv); 114659b66e9SLi RongQing 11597d3eb9dSJoao Martins ret = cpuidle_register_driver(drv); 11697d3eb9dSJoao Martins if (ret < 0) 11797d3eb9dSJoao Martins return ret; 11897d3eb9dSJoao Martins 11997d3eb9dSJoao Martins haltpoll_cpuidle_devices = alloc_percpu(struct cpuidle_device); 12097d3eb9dSJoao Martins if (haltpoll_cpuidle_devices == NULL) { 12197d3eb9dSJoao Martins cpuidle_unregister_driver(drv); 12297d3eb9dSJoao Martins return -ENOMEM; 12397d3eb9dSJoao Martins } 12497d3eb9dSJoao Martins 12597d3eb9dSJoao Martins ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpuidle/haltpoll:online", 12697d3eb9dSJoao Martins haltpoll_cpu_online, haltpoll_cpu_offline); 12797d3eb9dSJoao Martins if (ret < 0) { 12897d3eb9dSJoao Martins haltpoll_uninit(); 12997d3eb9dSJoao Martins } else { 13097d3eb9dSJoao Martins haltpoll_hp_state = ret; 13197d3eb9dSJoao Martins ret = 0; 13297d3eb9dSJoao Martins } 133a1c4423bSMarcelo Tosatti 134a1c4423bSMarcelo Tosatti return ret; 135fa86ee90SMarcelo Tosatti } 136fa86ee90SMarcelo Tosatti 137fa86ee90SMarcelo Tosatti static void __exit haltpoll_exit(void) 138fa86ee90SMarcelo Tosatti { 13997d3eb9dSJoao Martins haltpoll_uninit(); 140fa86ee90SMarcelo Tosatti } 141fa86ee90SMarcelo Tosatti 142fa86ee90SMarcelo Tosatti module_init(haltpoll_init); 143fa86ee90SMarcelo Tosatti module_exit(haltpoll_exit); 144fa86ee90SMarcelo Tosatti MODULE_LICENSE("GPL"); 145fa86ee90SMarcelo Tosatti MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>"); 146