cpufreq_spudemand.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/sched/loadavg.h>
  25. #include <linux/module.h>
  26. #include <linux/timer.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/atomic.h>
  29. #include <asm/machdep.h>
  30. #include <asm/spu.h>
  31. #define POLL_TIME 100000 /* in µs */
  32. #define EXP 753 /* exp(-1) in fixed-point */
  33. struct spu_gov_info_struct {
  34. unsigned long busy_spus; /* fixed-point */
  35. struct cpufreq_policy *policy;
  36. struct delayed_work work;
  37. unsigned int poll_int; /* µs */
  38. };
  39. static DEFINE_PER_CPU(struct spu_gov_info_struct, spu_gov_info);
  40. static int calc_freq(struct spu_gov_info_struct *info)
  41. {
  42. int cpu;
  43. int busy_spus;
  44. cpu = info->policy->cpu;
  45. busy_spus = atomic_read(&cbe_spu_info[cpu_to_node(cpu)].busy_spus);
  46. info->busy_spus = calc_load(info->busy_spus, EXP, busy_spus * FIXED_1);
  47. pr_debug("cpu %d: busy_spus=%d, info->busy_spus=%ld\n",
  48. cpu, busy_spus, info->busy_spus);
  49. return info->policy->max * info->busy_spus / FIXED_1;
  50. }
  51. static void spu_gov_work(struct work_struct *work)
  52. {
  53. struct spu_gov_info_struct *info;
  54. int delay;
  55. unsigned long target_freq;
  56. info = container_of(work, struct spu_gov_info_struct, work.work);
  57. /* after cancel_delayed_work_sync we unset info->policy */
  58. BUG_ON(info->policy == NULL);
  59. target_freq = calc_freq(info);
  60. __cpufreq_driver_target(info->policy, target_freq, CPUFREQ_RELATION_H);
  61. delay = usecs_to_jiffies(info->poll_int);
  62. schedule_delayed_work_on(info->policy->cpu, &info->work, delay);
  63. }
  64. static void spu_gov_init_work(struct spu_gov_info_struct *info)
  65. {
  66. int delay = usecs_to_jiffies(info->poll_int);
  67. INIT_DEFERRABLE_WORK(&info->work, spu_gov_work);
  68. schedule_delayed_work_on(info->policy->cpu, &info->work, delay);
  69. }
  70. static void spu_gov_cancel_work(struct spu_gov_info_struct *info)
  71. {
  72. cancel_delayed_work_sync(&info->work);
  73. }
  74. static int spu_gov_start(struct cpufreq_policy *policy)
  75. {
  76. unsigned int cpu = policy->cpu;
  77. struct spu_gov_info_struct *info = &per_cpu(spu_gov_info, cpu);
  78. struct spu_gov_info_struct *affected_info;
  79. int i;
  80. if (!cpu_online(cpu)) {
  81. printk(KERN_ERR "cpu %d is not online\n", cpu);
  82. return -EINVAL;
  83. }
  84. if (!policy->cur) {
  85. printk(KERN_ERR "no cpu specified in policy\n");
  86. return -EINVAL;
  87. }
  88. /* initialize spu_gov_info for all affected cpus */
  89. for_each_cpu(i, policy->cpus) {
  90. affected_info = &per_cpu(spu_gov_info, i);
  91. affected_info->policy = policy;
  92. }
  93. info->poll_int = POLL_TIME;
  94. /* setup timer */
  95. spu_gov_init_work(info);
  96. return 0;
  97. }
  98. static void spu_gov_stop(struct cpufreq_policy *policy)
  99. {
  100. unsigned int cpu = policy->cpu;
  101. struct spu_gov_info_struct *info = &per_cpu(spu_gov_info, cpu);
  102. int i;
  103. /* cancel timer */
  104. spu_gov_cancel_work(info);
  105. /* clean spu_gov_info for all affected cpus */
  106. for_each_cpu (i, policy->cpus) {
  107. info = &per_cpu(spu_gov_info, i);
  108. info->policy = NULL;
  109. }
  110. }
  111. static struct cpufreq_governor spu_governor = {
  112. .name = "spudemand",
  113. .start = spu_gov_start,
  114. .stop = spu_gov_stop,
  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>");