tau_6xx.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/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 <linux/delay.h>
  22. #include <linux/workqueue.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. #include <asm/asm-prototypes.h>
  30. #include "setup.h"
  31. static struct tau_temp
  32. {
  33. int interrupts;
  34. unsigned char low;
  35. unsigned char high;
  36. unsigned char grew;
  37. } tau[NR_CPUS];
  38. static bool tau_int_enable;
  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 2000 /* 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. u32 maybe_tie = tau_int_enable ? THRM1_TIE : 0;
  51. /* setup THRM1, threshold, valid bit, interrupt when below threshold */
  52. mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | maybe_tie | THRM1_TID);
  53. /* setup THRM2, threshold, valid bit, interrupt when above threshold */
  54. mtspr(SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | maybe_tie);
  55. }
  56. static void TAUupdate(int cpu)
  57. {
  58. u32 thrm;
  59. u32 bits = THRM1_TIV | THRM1_TIN | THRM1_V;
  60. /* if both thresholds are crossed, the step_sizes cancel out
  61. * and the window winds up getting expanded twice. */
  62. thrm = mfspr(SPRN_THRM1);
  63. if ((thrm & bits) == bits) {
  64. mtspr(SPRN_THRM1, 0);
  65. if (tau[cpu].low >= step_size) {
  66. tau[cpu].low -= step_size;
  67. tau[cpu].high -= (step_size - window_expand);
  68. }
  69. tau[cpu].grew = 1;
  70. pr_debug("%s: low threshold crossed\n", __func__);
  71. }
  72. thrm = mfspr(SPRN_THRM2);
  73. if ((thrm & bits) == bits) {
  74. mtspr(SPRN_THRM2, 0);
  75. if (tau[cpu].high <= 127 - step_size) {
  76. tau[cpu].low += (step_size - window_expand);
  77. tau[cpu].high += step_size;
  78. }
  79. tau[cpu].grew = 1;
  80. pr_debug("%s: high threshold crossed\n", __func__);
  81. }
  82. }
  83. #ifdef CONFIG_TAU_INT
  84. /*
  85. * TAU interrupts - called when we have a thermal assist unit interrupt
  86. * with interrupts disabled
  87. */
  88. void TAUException(struct pt_regs * regs)
  89. {
  90. int cpu = smp_processor_id();
  91. irq_enter();
  92. tau[cpu].interrupts++;
  93. TAUupdate(cpu);
  94. irq_exit();
  95. }
  96. #endif /* CONFIG_TAU_INT */
  97. static void tau_timeout(void * info)
  98. {
  99. int cpu;
  100. int size;
  101. int shrink;
  102. cpu = smp_processor_id();
  103. if (!tau_int_enable)
  104. TAUupdate(cpu);
  105. /* Stop thermal sensor comparisons and interrupts */
  106. mtspr(SPRN_THRM3, 0);
  107. size = tau[cpu].high - tau[cpu].low;
  108. if (size > min_window && ! tau[cpu].grew) {
  109. /* do an exponential shrink of half the amount currently over size */
  110. shrink = (2 + size - min_window) / 4;
  111. if (shrink) {
  112. tau[cpu].low += shrink;
  113. tau[cpu].high -= shrink;
  114. } else { /* size must have been min_window + 1 */
  115. tau[cpu].low += 1;
  116. #if 1 /* debug */
  117. if ((tau[cpu].high - tau[cpu].low) != min_window){
  118. printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
  119. }
  120. #endif
  121. }
  122. }
  123. tau[cpu].grew = 0;
  124. set_thresholds(cpu);
  125. /* Restart thermal sensor comparisons and interrupts.
  126. * The "PowerPC 740 and PowerPC 750 Microprocessor Datasheet"
  127. * recommends that "the maximum value be set in THRM3 under all
  128. * conditions."
  129. */
  130. mtspr(SPRN_THRM3, THRM3_SITV(0x1fff) | THRM3_E);
  131. }
  132. static struct workqueue_struct *tau_workq;
  133. static void tau_work_func(struct work_struct *work)
  134. {
  135. msleep(shrink_timer);
  136. on_each_cpu(tau_timeout, NULL, 0);
  137. /* schedule ourselves to be run again */
  138. queue_work(tau_workq, work);
  139. }
  140. DECLARE_WORK(tau_work, tau_work_func);
  141. /*
  142. * setup the TAU
  143. *
  144. * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
  145. * Start off at zero
  146. */
  147. int tau_initialized = 0;
  148. static void __init TAU_init_smp(void *info)
  149. {
  150. unsigned long cpu = smp_processor_id();
  151. /* set these to a reasonable value and let the timer shrink the
  152. * window */
  153. tau[cpu].low = 5;
  154. tau[cpu].high = 120;
  155. set_thresholds(cpu);
  156. }
  157. static int __init TAU_init(void)
  158. {
  159. /* We assume in SMP that if one CPU has TAU support, they
  160. * all have it --BenH
  161. */
  162. if (!cpu_has_feature(CPU_FTR_TAU)) {
  163. printk("Thermal assist unit not available\n");
  164. tau_initialized = 0;
  165. return 1;
  166. }
  167. tau_int_enable = IS_ENABLED(CONFIG_TAU_INT) &&
  168. !strcmp(cur_cpu_spec->platform, "ppc750");
  169. tau_workq = alloc_workqueue("tau", WQ_UNBOUND, 1, 0);
  170. if (!tau_workq)
  171. return -ENOMEM;
  172. on_each_cpu(TAU_init_smp, NULL, 0);
  173. queue_work(tau_workq, &tau_work);
  174. pr_info("Thermal assist unit using %s, shrink_timer: %d ms\n",
  175. tau_int_enable ? "interrupts" : "workqueue", shrink_timer);
  176. tau_initialized = 1;
  177. return 0;
  178. }
  179. __initcall(TAU_init);
  180. /*
  181. * return current temp
  182. */
  183. u32 cpu_temp_both(unsigned long cpu)
  184. {
  185. return ((tau[cpu].high << 16) | tau[cpu].low);
  186. }
  187. u32 cpu_temp(unsigned long cpu)
  188. {
  189. return ((tau[cpu].high + tau[cpu].low) / 2);
  190. }
  191. u32 tau_interrupts(unsigned long cpu)
  192. {
  193. return (tau[cpu].interrupts);
  194. }