cpufreq_spudemand.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * spu aware cpufreq governor for the cell processor
  3. *
  4. * © Copyright IBM Corporation 2006-2008
  5. *
  6. * Author: Christian Krafft <krafft@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/cpufreq.h>
  23. #include <linux/sched.h>
  24. #include <linux/timer.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/atomic.h>
  27. #include <asm/machdep.h>
  28. #include <asm/spu.h>
  29. #define POLL_TIME 100000 /* in µs */
  30. #define EXP 753 /* exp(-1) in fixed-point */
  31. struct spu_gov_info_struct {
  32. unsigned long busy_spus; /* fixed-point */
  33. struct cpufreq_policy *policy;
  34. struct delayed_work work;
  35. unsigned int poll_int; /* µs */
  36. };
  37. static DEFINE_PER_CPU(struct spu_gov_info_struct, spu_gov_info);
  38. static int calc_freq(struct spu_gov_info_struct *info)
  39. {
  40. int cpu;
  41. int busy_spus;
  42. cpu = info->policy->cpu;
  43. busy_spus = atomic_read(&cbe_spu_info[cpu_to_node(cpu)].busy_spus);
  44. CALC_LOAD(info->busy_spus, EXP, busy_spus * FIXED_1);
  45. pr_debug("cpu %d: busy_spus=%d, info->busy_spus=%ld\n",
  46. cpu, busy_spus, info->busy_spus);
  47. return info->policy->max * info->busy_spus / FIXED_1;
  48. }
  49. static void spu_gov_work(struct work_struct *work)
  50. {
  51. struct spu_gov_info_struct *info;
  52. int delay;
  53. unsigned long target_freq;
  54. info = container_of(work, struct spu_gov_info_struct, work.work);
  55. /* after cancel_delayed_work_sync we unset info->policy */
  56. BUG_ON(info->policy == NULL);
  57. target_freq = calc_freq(info);
  58. __cpufreq_driver_target(info->policy, target_freq, CPUFREQ_RELATION_H);
  59. delay = usecs_to_jiffies(info->poll_int);
  60. schedule_delayed_work_on(info->policy->cpu, &info->work, delay);
  61. }
  62. static void spu_gov_init_work(struct spu_gov_info_struct *info)
  63. {
  64. int delay = usecs_to_jiffies(info->poll_int);
  65. INIT_DELAYED_WORK_DEFERRABLE(&info->work, spu_gov_work);
  66. schedule_delayed_work_on(info->policy->cpu, &info->work, delay);
  67. }
  68. static void spu_gov_cancel_work(struct spu_gov_info_struct *info)
  69. {
  70. cancel_delayed_work_sync(&info->work);
  71. }
  72. static int spu_gov_govern(struct cpufreq_policy *policy, unsigned int event)
  73. {
  74. unsigned int cpu = policy->cpu;
  75. struct spu_gov_info_struct *info, *affected_info;
  76. int i;
  77. int ret = 0;
  78. info = &per_cpu(spu_gov_info, cpu);
  79. switch (event) {
  80. case CPUFREQ_GOV_START:
  81. if (!cpu_online(cpu)) {
  82. printk(KERN_ERR "cpu %d is not online\n", cpu);
  83. ret = -EINVAL;
  84. break;
  85. }
  86. if (!policy->cur) {
  87. printk(KERN_ERR "no cpu specified in policy\n");
  88. ret = -EINVAL;
  89. break;
  90. }
  91. /* initialize spu_gov_info for all affected cpus */
  92. for_each_cpu(i, policy->cpus) {
  93. affected_info = &per_cpu(spu_gov_info, i);
  94. affected_info->policy = policy;
  95. }
  96. info->poll_int = POLL_TIME;
  97. /* setup timer */
  98. spu_gov_init_work(info);
  99. break;
  100. case CPUFREQ_GOV_STOP:
  101. /* cancel timer */
  102. spu_gov_cancel_work(info);
  103. /* clean spu_gov_info for all affected cpus */
  104. for_each_cpu (i, policy->cpus) {
  105. info = &per_cpu(spu_gov_info, i);
  106. info->policy = NULL;
  107. }
  108. break;
  109. }
  110. return ret;
  111. }
  112. static struct cpufreq_governor spu_governor = {
  113. .name = "spudemand",
  114. .governor = spu_gov_govern,
  115. .owner = THIS_MODULE,
  116. };
  117. /*
  118. * module init and destoy
  119. */
  120. static int __init spu_gov_init(void)
  121. {
  122. int ret;
  123. ret = cpufreq_register_governor(&spu_governor);
  124. if (ret)
  125. printk(KERN_ERR "registration of governor failed\n");
  126. return ret;
  127. }
  128. static void __exit spu_gov_exit(void)
  129. {
  130. cpufreq_unregister_governor(&spu_governor);
  131. }
  132. module_init(spu_gov_init);
  133. module_exit(spu_gov_exit);
  134. MODULE_LICENSE("GPL");
  135. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");