windfarm_cpufreq_clamp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <linux/types.h>
  2. #include <linux/errno.h>
  3. #include <linux/kernel.h>
  4. #include <linux/delay.h>
  5. #include <linux/slab.h>
  6. #include <linux/init.h>
  7. #include <linux/wait.h>
  8. #include <linux/cpufreq.h>
  9. #include <asm/prom.h>
  10. #include "windfarm.h"
  11. #define VERSION "0.3"
  12. static int clamped;
  13. static struct wf_control *clamp_control;
  14. static int clamp_notifier_call(struct notifier_block *self,
  15. unsigned long event, void *data)
  16. {
  17. struct cpufreq_policy *p = data;
  18. unsigned long max_freq;
  19. if (event != CPUFREQ_ADJUST)
  20. return 0;
  21. max_freq = clamped ? (p->cpuinfo.min_freq) : (p->cpuinfo.max_freq);
  22. cpufreq_verify_within_limits(p, 0, max_freq);
  23. return 0;
  24. }
  25. static struct notifier_block clamp_notifier = {
  26. .notifier_call = clamp_notifier_call,
  27. };
  28. static int clamp_set(struct wf_control *ct, s32 value)
  29. {
  30. if (value)
  31. printk(KERN_INFO "windfarm: Clamping CPU frequency to "
  32. "minimum !\n");
  33. else
  34. printk(KERN_INFO "windfarm: CPU frequency unclamped !\n");
  35. clamped = value;
  36. cpufreq_update_policy(0);
  37. return 0;
  38. }
  39. static int clamp_get(struct wf_control *ct, s32 *value)
  40. {
  41. *value = clamped;
  42. return 0;
  43. }
  44. static s32 clamp_min(struct wf_control *ct)
  45. {
  46. return 0;
  47. }
  48. static s32 clamp_max(struct wf_control *ct)
  49. {
  50. return 1;
  51. }
  52. static struct wf_control_ops clamp_ops = {
  53. .set_value = clamp_set,
  54. .get_value = clamp_get,
  55. .get_min = clamp_min,
  56. .get_max = clamp_max,
  57. .owner = THIS_MODULE,
  58. };
  59. static int __init wf_cpufreq_clamp_init(void)
  60. {
  61. struct wf_control *clamp;
  62. clamp = kmalloc(sizeof(struct wf_control), GFP_KERNEL);
  63. if (clamp == NULL)
  64. return -ENOMEM;
  65. cpufreq_register_notifier(&clamp_notifier, CPUFREQ_POLICY_NOTIFIER);
  66. clamp->ops = &clamp_ops;
  67. clamp->name = "cpufreq-clamp";
  68. if (wf_register_control(clamp))
  69. goto fail;
  70. clamp_control = clamp;
  71. return 0;
  72. fail:
  73. kfree(clamp);
  74. return -ENODEV;
  75. }
  76. static void __exit wf_cpufreq_clamp_exit(void)
  77. {
  78. if (clamp_control)
  79. wf_unregister_control(clamp_control);
  80. }
  81. module_init(wf_cpufreq_clamp_init);
  82. module_exit(wf_cpufreq_clamp_exit);
  83. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  84. MODULE_DESCRIPTION("CPU frequency clamp for PowerMacs thermal control");
  85. MODULE_LICENSE("GPL");