temp.c 6.3 KB

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