g84.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. * Martin Peres
  24. */
  25. #include "priv.h"
  26. #include <subdev/fuse.h>
  27. int
  28. g84_temp_get(struct nvkm_therm *therm)
  29. {
  30. struct nvkm_device *device = therm->subdev.device;
  31. if (nvkm_fuse_read(device->fuse, 0x1a8) == 1)
  32. return nvkm_rd32(device, 0x20400);
  33. else
  34. return -ENODEV;
  35. }
  36. void
  37. g84_sensor_setup(struct nvkm_therm *therm)
  38. {
  39. struct nvkm_device *device = therm->subdev.device;
  40. /* enable temperature reading for cards with insane defaults */
  41. if (nvkm_fuse_read(device->fuse, 0x1a8) == 1) {
  42. nvkm_mask(device, 0x20008, 0x80008000, 0x80000000);
  43. nvkm_mask(device, 0x2000c, 0x80000003, 0x00000000);
  44. mdelay(20); /* wait for the temperature to stabilize */
  45. }
  46. }
  47. static void
  48. g84_therm_program_alarms(struct nvkm_therm *therm)
  49. {
  50. struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
  51. struct nvkm_subdev *subdev = &therm->subdev;
  52. struct nvkm_device *device = subdev->device;
  53. unsigned long flags;
  54. spin_lock_irqsave(&therm->sensor.alarm_program_lock, flags);
  55. /* enable RISING and FALLING IRQs for shutdown, THRS 0, 1, 2 and 4 */
  56. nvkm_wr32(device, 0x20000, 0x000003ff);
  57. /* shutdown: The computer should be shutdown when reached */
  58. nvkm_wr32(device, 0x20484, sensor->thrs_shutdown.hysteresis);
  59. nvkm_wr32(device, 0x20480, sensor->thrs_shutdown.temp);
  60. /* THRS_1 : fan boost*/
  61. nvkm_wr32(device, 0x204c4, sensor->thrs_fan_boost.temp);
  62. /* THRS_2 : critical */
  63. nvkm_wr32(device, 0x204c0, sensor->thrs_critical.temp);
  64. /* THRS_4 : down clock */
  65. nvkm_wr32(device, 0x20414, sensor->thrs_down_clock.temp);
  66. spin_unlock_irqrestore(&therm->sensor.alarm_program_lock, flags);
  67. nvkm_debug(subdev,
  68. "Programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
  69. sensor->thrs_fan_boost.temp,
  70. sensor->thrs_fan_boost.hysteresis,
  71. sensor->thrs_down_clock.temp,
  72. sensor->thrs_down_clock.hysteresis,
  73. sensor->thrs_critical.temp,
  74. sensor->thrs_critical.hysteresis,
  75. sensor->thrs_shutdown.temp,
  76. sensor->thrs_shutdown.hysteresis);
  77. }
  78. /* must be called with alarm_program_lock taken ! */
  79. static void
  80. g84_therm_threshold_hyst_emulation(struct nvkm_therm *therm,
  81. uint32_t thrs_reg, u8 status_bit,
  82. const struct nvbios_therm_threshold *thrs,
  83. enum nvkm_therm_thrs thrs_name)
  84. {
  85. struct nvkm_device *device = therm->subdev.device;
  86. enum nvkm_therm_thrs_direction direction;
  87. enum nvkm_therm_thrs_state prev_state, new_state;
  88. int temp, cur;
  89. prev_state = nvkm_therm_sensor_get_threshold_state(therm, thrs_name);
  90. temp = nvkm_rd32(device, thrs_reg);
  91. /* program the next threshold */
  92. if (temp == thrs->temp) {
  93. nvkm_wr32(device, thrs_reg, thrs->temp - thrs->hysteresis);
  94. new_state = NVKM_THERM_THRS_HIGHER;
  95. } else {
  96. nvkm_wr32(device, thrs_reg, thrs->temp);
  97. new_state = NVKM_THERM_THRS_LOWER;
  98. }
  99. /* fix the state (in case someone reprogrammed the alarms) */
  100. cur = therm->func->temp_get(therm);
  101. if (new_state == NVKM_THERM_THRS_LOWER && cur > thrs->temp)
  102. new_state = NVKM_THERM_THRS_HIGHER;
  103. else if (new_state == NVKM_THERM_THRS_HIGHER &&
  104. cur < thrs->temp - thrs->hysteresis)
  105. new_state = NVKM_THERM_THRS_LOWER;
  106. nvkm_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
  107. /* find the direction */
  108. if (prev_state < new_state)
  109. direction = NVKM_THERM_THRS_RISING;
  110. else if (prev_state > new_state)
  111. direction = NVKM_THERM_THRS_FALLING;
  112. else
  113. return;
  114. /* advertise a change in direction */
  115. nvkm_therm_sensor_event(therm, thrs_name, direction);
  116. }
  117. static void
  118. g84_therm_intr(struct nvkm_therm *therm)
  119. {
  120. struct nvkm_subdev *subdev = &therm->subdev;
  121. struct nvkm_device *device = subdev->device;
  122. struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
  123. unsigned long flags;
  124. uint32_t intr;
  125. spin_lock_irqsave(&therm->sensor.alarm_program_lock, flags);
  126. intr = nvkm_rd32(device, 0x20100) & 0x3ff;
  127. /* THRS_4: downclock */
  128. if (intr & 0x002) {
  129. g84_therm_threshold_hyst_emulation(therm, 0x20414, 24,
  130. &sensor->thrs_down_clock,
  131. NVKM_THERM_THRS_DOWNCLOCK);
  132. intr &= ~0x002;
  133. }
  134. /* shutdown */
  135. if (intr & 0x004) {
  136. g84_therm_threshold_hyst_emulation(therm, 0x20480, 20,
  137. &sensor->thrs_shutdown,
  138. NVKM_THERM_THRS_SHUTDOWN);
  139. intr &= ~0x004;
  140. }
  141. /* THRS_1 : fan boost */
  142. if (intr & 0x008) {
  143. g84_therm_threshold_hyst_emulation(therm, 0x204c4, 21,
  144. &sensor->thrs_fan_boost,
  145. NVKM_THERM_THRS_FANBOOST);
  146. intr &= ~0x008;
  147. }
  148. /* THRS_2 : critical */
  149. if (intr & 0x010) {
  150. g84_therm_threshold_hyst_emulation(therm, 0x204c0, 22,
  151. &sensor->thrs_critical,
  152. NVKM_THERM_THRS_CRITICAL);
  153. intr &= ~0x010;
  154. }
  155. if (intr)
  156. nvkm_error(subdev, "intr %08x\n", intr);
  157. /* ACK everything */
  158. nvkm_wr32(device, 0x20100, 0xffffffff);
  159. nvkm_wr32(device, 0x1100, 0x10000); /* PBUS */
  160. spin_unlock_irqrestore(&therm->sensor.alarm_program_lock, flags);
  161. }
  162. void
  163. g84_therm_fini(struct nvkm_therm *therm)
  164. {
  165. struct nvkm_device *device = therm->subdev.device;
  166. /* Disable PTherm IRQs */
  167. nvkm_wr32(device, 0x20000, 0x00000000);
  168. /* ACK all PTherm IRQs */
  169. nvkm_wr32(device, 0x20100, 0xffffffff);
  170. nvkm_wr32(device, 0x1100, 0x10000); /* PBUS */
  171. }
  172. static void
  173. g84_therm_init(struct nvkm_therm *therm)
  174. {
  175. g84_sensor_setup(therm);
  176. }
  177. static const struct nvkm_therm_func
  178. g84_therm = {
  179. .init = g84_therm_init,
  180. .fini = g84_therm_fini,
  181. .intr = g84_therm_intr,
  182. .pwm_ctrl = nv50_fan_pwm_ctrl,
  183. .pwm_get = nv50_fan_pwm_get,
  184. .pwm_set = nv50_fan_pwm_set,
  185. .pwm_clock = nv50_fan_pwm_clock,
  186. .temp_get = g84_temp_get,
  187. .program_alarms = g84_therm_program_alarms,
  188. };
  189. int
  190. g84_therm_new(struct nvkm_device *device, int index, struct nvkm_therm **ptherm)
  191. {
  192. struct nvkm_therm *therm;
  193. int ret;
  194. ret = nvkm_therm_new_(&g84_therm, device, index, &therm);
  195. *ptherm = therm;
  196. if (ret)
  197. return ret;
  198. /* init the thresholds */
  199. nvkm_therm_sensor_set_threshold_state(therm, NVKM_THERM_THRS_SHUTDOWN,
  200. NVKM_THERM_THRS_LOWER);
  201. nvkm_therm_sensor_set_threshold_state(therm, NVKM_THERM_THRS_FANBOOST,
  202. NVKM_THERM_THRS_LOWER);
  203. nvkm_therm_sensor_set_threshold_state(therm, NVKM_THERM_THRS_CRITICAL,
  204. NVKM_THERM_THRS_LOWER);
  205. nvkm_therm_sensor_set_threshold_state(therm, NVKM_THERM_THRS_DOWNCLOCK,
  206. NVKM_THERM_THRS_LOWER);
  207. return 0;
  208. }