tau_6xx.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <[email protected]>
  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/interrupt.h>
  24. #include <asm/io.h>
  25. #include <asm/reg.h>
  26. #include <asm/nvram.h>
  27. #include <asm/cache.h>
  28. #include <asm/8xx_immap.h>
  29. #include <asm/machdep.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. DEFINE_INTERRUPT_HANDLER_ASYNC(TAUException)
  89. {
  90. int cpu = smp_processor_id();
  91. tau[cpu].interrupts++;
  92. TAUupdate(cpu);
  93. }
  94. #endif /* CONFIG_TAU_INT */
  95. static void tau_timeout(void * info)
  96. {
  97. int cpu;
  98. int size;
  99. int shrink;
  100. cpu = smp_processor_id();
  101. if (!tau_int_enable)
  102. TAUupdate(cpu);
  103. /* Stop thermal sensor comparisons and interrupts */
  104. mtspr(SPRN_THRM3, 0);
  105. size = tau[cpu].high - tau[cpu].low;
  106. if (size > min_window && ! tau[cpu].grew) {
  107. /* do an exponential shrink of half the amount currently over size */
  108. shrink = (2 + size - min_window) / 4;
  109. if (shrink) {
  110. tau[cpu].low += shrink;
  111. tau[cpu].high -= shrink;
  112. } else { /* size must have been min_window + 1 */
  113. tau[cpu].low += 1;
  114. #if 1 /* debug */
  115. if ((tau[cpu].high - tau[cpu].low) != min_window){
  116. printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
  117. }
  118. #endif
  119. }
  120. }
  121. tau[cpu].grew = 0;
  122. set_thresholds(cpu);
  123. /* Restart thermal sensor comparisons and interrupts.
  124. * The "PowerPC 740 and PowerPC 750 Microprocessor Datasheet"
  125. * recommends that "the maximum value be set in THRM3 under all
  126. * conditions."
  127. */
  128. mtspr(SPRN_THRM3, THRM3_SITV(0x1fff) | THRM3_E);
  129. }
  130. static struct workqueue_struct *tau_workq;
  131. static void tau_work_func(struct work_struct *work)
  132. {
  133. msleep(shrink_timer);
  134. on_each_cpu(tau_timeout, NULL, 0);
  135. /* schedule ourselves to be run again */
  136. queue_work(tau_workq, work);
  137. }
  138. static DECLARE_WORK(tau_work, tau_work_func);
  139. /*
  140. * setup the TAU
  141. *
  142. * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
  143. * Start off at zero
  144. */
  145. int tau_initialized = 0;
  146. static void __init TAU_init_smp(void *info)
  147. {
  148. unsigned long cpu = smp_processor_id();
  149. /* set these to a reasonable value and let the timer shrink the
  150. * window */
  151. tau[cpu].low = 5;
  152. tau[cpu].high = 120;
  153. set_thresholds(cpu);
  154. }
  155. static int __init TAU_init(void)
  156. {
  157. /* We assume in SMP that if one CPU has TAU support, they
  158. * all have it --BenH
  159. */
  160. if (!cpu_has_feature(CPU_FTR_TAU)) {
  161. printk("Thermal assist unit not available\n");
  162. tau_initialized = 0;
  163. return 1;
  164. }
  165. tau_int_enable = IS_ENABLED(CONFIG_TAU_INT) &&
  166. !strcmp(cur_cpu_spec->platform, "ppc750");
  167. tau_workq = alloc_workqueue("tau", WQ_UNBOUND, 1);
  168. if (!tau_workq)
  169. return -ENOMEM;
  170. on_each_cpu(TAU_init_smp, NULL, 0);
  171. queue_work(tau_workq, &tau_work);
  172. pr_info("Thermal assist unit using %s, shrink_timer: %d ms\n",
  173. tau_int_enable ? "interrupts" : "workqueue", shrink_timer);
  174. tau_initialized = 1;
  175. return 0;
  176. }
  177. __initcall(TAU_init);
  178. /*
  179. * return current temp
  180. */
  181. u32 cpu_temp_both(unsigned long cpu)
  182. {
  183. return ((tau[cpu].high << 16) | tau[cpu].low);
  184. }
  185. u32 cpu_temp(unsigned long cpu)
  186. {
  187. return ((tau[cpu].high + tau[cpu].low) / 2);
  188. }
  189. u32 tau_interrupts(unsigned long cpu)
  190. {
  191. return (tau[cpu].interrupts);
  192. }