tau_6xx.c 6.2 KB

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