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