tau_6xx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * temp.c Thermal management for cpu's with Thermal Assist Units
  4. *
  5. * Written by Troy Benjegerdes <hozer@drgw.net>
  6. *
  7. * TODO:
  8. * dynamic power management to limit peak CPU temp (using ICTC)
  9. * calibration???
  10. *
  11. * Silly, crazy ideas: use cpu load (from scheduler) and ICTC to extend battery
  12. * life in portables, and add a 'performance/watt' metric somewhere in /proc
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/kernel.h>
  17. #include <linux/param.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/init.h>
  22. #include <asm/io.h>
  23. #include <asm/reg.h>
  24. #include <asm/nvram.h>
  25. #include <asm/cache.h>
  26. #include <asm/8xx_immap.h>
  27. #include <asm/machdep.h>
  28. #include <asm/asm-prototypes.h>
  29. #include "setup.h"
  30. static struct tau_temp
  31. {
  32. int interrupts;
  33. unsigned char low;
  34. unsigned char high;
  35. unsigned char grew;
  36. } tau[NR_CPUS];
  37. struct timer_list tau_timer;
  38. #undef DEBUG
  39. /* TODO: put these in a /proc interface, with some sanity checks, and maybe
  40. * dynamic adjustment to minimize # of interrupts */
  41. /* configurable values for step size and how much to expand the window when
  42. * we get an interrupt. These are based on the limit that was out of range */
  43. #define step_size 2 /* step size when temp goes out of range */
  44. #define window_expand 1 /* expand the window by this much */
  45. /* configurable values for shrinking the window */
  46. #define shrink_timer 2*HZ /* period between shrinking the window */
  47. #define min_window 2 /* minimum window size, degrees C */
  48. static void set_thresholds(unsigned long cpu)
  49. {
  50. #ifdef CONFIG_TAU_INT
  51. /*
  52. * setup THRM1,
  53. * threshold, valid bit, enable interrupts, interrupt when below threshold
  54. */
  55. mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TIE | THRM1_TID);
  56. /* setup THRM2,
  57. * threshold, valid bit, enable interrupts, interrupt when above threshold
  58. */
  59. mtspr (SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | THRM1_TIE);
  60. #else
  61. /* same thing but don't enable interrupts */
  62. mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TID);
  63. mtspr(SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V);
  64. #endif
  65. }
  66. static void TAUupdate(int cpu)
  67. {
  68. unsigned thrm;
  69. #ifdef DEBUG
  70. printk("TAUupdate ");
  71. #endif
  72. /* if both thresholds are crossed, the step_sizes cancel out
  73. * and the window winds up getting expanded twice. */
  74. if((thrm = mfspr(SPRN_THRM1)) & THRM1_TIV){ /* is valid? */
  75. if(thrm & THRM1_TIN){ /* crossed low threshold */
  76. if (tau[cpu].low >= step_size){
  77. tau[cpu].low -= step_size;
  78. tau[cpu].high -= (step_size - window_expand);
  79. }
  80. tau[cpu].grew = 1;
  81. #ifdef DEBUG
  82. printk("low threshold crossed ");
  83. #endif
  84. }
  85. }
  86. if((thrm = mfspr(SPRN_THRM2)) & THRM1_TIV){ /* is valid? */
  87. if(thrm & THRM1_TIN){ /* crossed high threshold */
  88. if (tau[cpu].high <= 127-step_size){
  89. tau[cpu].low += (step_size - window_expand);
  90. tau[cpu].high += step_size;
  91. }
  92. tau[cpu].grew = 1;
  93. #ifdef DEBUG
  94. printk("high threshold crossed ");
  95. #endif
  96. }
  97. }
  98. #ifdef DEBUG
  99. printk("grew = %d\n", tau[cpu].grew);
  100. #endif
  101. #ifndef CONFIG_TAU_INT /* tau_timeout will do this if not using interrupts */
  102. set_thresholds(cpu);
  103. #endif
  104. }
  105. #ifdef CONFIG_TAU_INT
  106. /*
  107. * TAU interrupts - called when we have a thermal assist unit interrupt
  108. * with interrupts disabled
  109. */
  110. void TAUException(struct pt_regs * regs)
  111. {
  112. int cpu = smp_processor_id();
  113. irq_enter();
  114. tau[cpu].interrupts++;
  115. TAUupdate(cpu);
  116. irq_exit();
  117. }
  118. #endif /* CONFIG_TAU_INT */
  119. static void tau_timeout(void * info)
  120. {
  121. int cpu;
  122. unsigned long flags;
  123. int size;
  124. int shrink;
  125. /* disabling interrupts *should* be okay */
  126. local_irq_save(flags);
  127. cpu = smp_processor_id();
  128. #ifndef CONFIG_TAU_INT
  129. TAUupdate(cpu);
  130. #endif
  131. size = tau[cpu].high - tau[cpu].low;
  132. if (size > min_window && ! tau[cpu].grew) {
  133. /* do an exponential shrink of half the amount currently over size */
  134. shrink = (2 + size - min_window) / 4;
  135. if (shrink) {
  136. tau[cpu].low += shrink;
  137. tau[cpu].high -= shrink;
  138. } else { /* size must have been min_window + 1 */
  139. tau[cpu].low += 1;
  140. #if 1 /* debug */
  141. if ((tau[cpu].high - tau[cpu].low) != min_window){
  142. printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
  143. }
  144. #endif
  145. }
  146. }
  147. tau[cpu].grew = 0;
  148. set_thresholds(cpu);
  149. /*
  150. * Do the enable every time, since otherwise a bunch of (relatively)
  151. * complex sleep code needs to be added. One mtspr every time
  152. * tau_timeout is called is probably not a big deal.
  153. *
  154. * Enable thermal sensor and set up sample interval timer
  155. * need 20 us to do the compare.. until a nice 'cpu_speed' function
  156. * call is implemented, just assume a 500 mhz clock. It doesn't really
  157. * matter if we take too long for a compare since it's all interrupt
  158. * driven anyway.
  159. *
  160. * use a extra long time.. (60 us @ 500 mhz)
  161. */
  162. mtspr(SPRN_THRM3, THRM3_SITV(500*60) | THRM3_E);
  163. local_irq_restore(flags);
  164. }
  165. static void tau_timeout_smp(struct timer_list *unused)
  166. {
  167. /* schedule ourselves to be run again */
  168. mod_timer(&tau_timer, jiffies + shrink_timer) ;
  169. on_each_cpu(tau_timeout, NULL, 0);
  170. }
  171. /*
  172. * setup the TAU
  173. *
  174. * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
  175. * Start off at zero
  176. */
  177. int tau_initialized = 0;
  178. static void __init TAU_init_smp(void *info)
  179. {
  180. unsigned long cpu = smp_processor_id();
  181. /* set these to a reasonable value and let the timer shrink the
  182. * window */
  183. tau[cpu].low = 5;
  184. tau[cpu].high = 120;
  185. set_thresholds(cpu);
  186. }
  187. static int __init TAU_init(void)
  188. {
  189. /* We assume in SMP that if one CPU has TAU support, they
  190. * all have it --BenH
  191. */
  192. if (!cpu_has_feature(CPU_FTR_TAU)) {
  193. printk("Thermal assist unit not available\n");
  194. tau_initialized = 0;
  195. return 1;
  196. }
  197. /* first, set up the window shrinking timer */
  198. timer_setup(&tau_timer, tau_timeout_smp, 0);
  199. tau_timer.expires = jiffies + shrink_timer;
  200. add_timer(&tau_timer);
  201. on_each_cpu(TAU_init_smp, NULL, 0);
  202. printk("Thermal assist unit ");
  203. #ifdef CONFIG_TAU_INT
  204. printk("using interrupts, ");
  205. #else
  206. printk("using timers, ");
  207. #endif
  208. printk("shrink_timer: %d jiffies\n", shrink_timer);
  209. tau_initialized = 1;
  210. return 0;
  211. }
  212. __initcall(TAU_init);
  213. /*
  214. * return current temp
  215. */
  216. u32 cpu_temp_both(unsigned long cpu)
  217. {
  218. return ((tau[cpu].high << 16) | tau[cpu].low);
  219. }
  220. u32 cpu_temp(unsigned long cpu)
  221. {
  222. return ((tau[cpu].high + tau[cpu].low) / 2);
  223. }
  224. u32 tau_interrupts(unsigned long cpu)
  225. {
  226. return (tau[cpu].interrupts);
  227. }