cpuidle-haltpoll.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // 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. #include <linux/init.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cpuidle.h>
  15. #include <linux/module.h>
  16. #include <linux/sched/idle.h>
  17. #include <linux/kvm_para.h>
  18. #include <linux/cpuidle_haltpoll.h>
  19. static bool force __read_mostly;
  20. module_param(force, bool, 0444);
  21. MODULE_PARM_DESC(force, "Load unconditionally");
  22. static struct cpuidle_device __percpu *haltpoll_cpuidle_devices;
  23. static enum cpuhp_state haltpoll_hp_state;
  24. static __cpuidle int default_enter_idle(struct cpuidle_device *dev,
  25. struct cpuidle_driver *drv, int index)
  26. {
  27. if (current_clr_polling_and_test())
  28. return index;
  29. arch_cpu_idle();
  30. return index;
  31. }
  32. static struct cpuidle_driver haltpoll_driver = {
  33. .name = "haltpoll",
  34. .governor = "haltpoll",
  35. .states = {
  36. { /* entry 0 is for polling */ },
  37. {
  38. .enter = default_enter_idle,
  39. .exit_latency = 1,
  40. .target_residency = 1,
  41. .power_usage = -1,
  42. .name = "haltpoll idle",
  43. .desc = "default architecture idle",
  44. },
  45. },
  46. .safe_state_index = 0,
  47. .state_count = 2,
  48. };
  49. static int haltpoll_cpu_online(unsigned int cpu)
  50. {
  51. struct cpuidle_device *dev;
  52. dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
  53. if (!dev->registered) {
  54. dev->cpu = cpu;
  55. if (cpuidle_register_device(dev)) {
  56. pr_notice("cpuidle_register_device %d failed!\n", cpu);
  57. return -EIO;
  58. }
  59. arch_haltpoll_enable(cpu);
  60. }
  61. return 0;
  62. }
  63. static int haltpoll_cpu_offline(unsigned int cpu)
  64. {
  65. struct cpuidle_device *dev;
  66. dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
  67. if (dev->registered) {
  68. arch_haltpoll_disable(cpu);
  69. cpuidle_unregister_device(dev);
  70. }
  71. return 0;
  72. }
  73. static void haltpoll_uninit(void)
  74. {
  75. if (haltpoll_hp_state)
  76. cpuhp_remove_state(haltpoll_hp_state);
  77. cpuidle_unregister_driver(&haltpoll_driver);
  78. free_percpu(haltpoll_cpuidle_devices);
  79. haltpoll_cpuidle_devices = NULL;
  80. }
  81. static bool haltpoll_want(void)
  82. {
  83. return kvm_para_has_hint(KVM_HINTS_REALTIME) || force;
  84. }
  85. static int __init haltpoll_init(void)
  86. {
  87. int ret;
  88. struct cpuidle_driver *drv = &haltpoll_driver;
  89. /* Do not load haltpoll if idle= is passed */
  90. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  91. return -ENODEV;
  92. if (!kvm_para_available() || !haltpoll_want())
  93. return -ENODEV;
  94. cpuidle_poll_state_init(drv);
  95. ret = cpuidle_register_driver(drv);
  96. if (ret < 0)
  97. return ret;
  98. haltpoll_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  99. if (haltpoll_cpuidle_devices == NULL) {
  100. cpuidle_unregister_driver(drv);
  101. return -ENOMEM;
  102. }
  103. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpuidle/haltpoll:online",
  104. haltpoll_cpu_online, haltpoll_cpu_offline);
  105. if (ret < 0) {
  106. haltpoll_uninit();
  107. } else {
  108. haltpoll_hp_state = ret;
  109. ret = 0;
  110. }
  111. return ret;
  112. }
  113. static void __exit haltpoll_exit(void)
  114. {
  115. haltpoll_uninit();
  116. }
  117. module_init(haltpoll_init);
  118. module_exit(haltpoll_exit);
  119. MODULE_DESCRIPTION("cpuidle driver for haltpoll governor");
  120. MODULE_LICENSE("GPL");
  121. MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");