therm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2012 Nouveau Community
  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: Martin Peres
  23. */
  24. #include <subdev/bios.h>
  25. #include <subdev/bios/bit.h>
  26. #include <subdev/bios/therm.h>
  27. static u32
  28. therm_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *len, u8 *cnt)
  29. {
  30. struct bit_entry bit_P;
  31. u32 therm = 0;
  32. if (!bit_entry(bios, 'P', &bit_P)) {
  33. if (bit_P.version == 1)
  34. therm = nvbios_rd32(bios, bit_P.offset + 12);
  35. else if (bit_P.version == 2)
  36. therm = nvbios_rd32(bios, bit_P.offset + 16);
  37. else
  38. nvkm_error(&bios->subdev,
  39. "unknown offset for thermal in BIT P %d\n",
  40. bit_P.version);
  41. }
  42. /* exit now if we haven't found the thermal table */
  43. if (!therm)
  44. return 0;
  45. *ver = nvbios_rd08(bios, therm + 0);
  46. *hdr = nvbios_rd08(bios, therm + 1);
  47. *len = nvbios_rd08(bios, therm + 2);
  48. *cnt = nvbios_rd08(bios, therm + 3);
  49. return therm + nvbios_rd08(bios, therm + 1);
  50. }
  51. static u32
  52. nvbios_therm_entry(struct nvkm_bios *bios, int idx, u8 *ver, u8 *len)
  53. {
  54. u8 hdr, cnt;
  55. u32 therm = therm_table(bios, ver, &hdr, len, &cnt);
  56. if (therm && idx < cnt)
  57. return therm + idx * *len;
  58. return 0;
  59. }
  60. int
  61. nvbios_therm_sensor_parse(struct nvkm_bios *bios,
  62. enum nvbios_therm_domain domain,
  63. struct nvbios_therm_sensor *sensor)
  64. {
  65. s8 thrs_section, sensor_section, offset;
  66. u8 ver, len, i;
  67. u32 entry;
  68. /* we only support the core domain for now */
  69. if (domain != NVBIOS_THERM_DOMAIN_CORE)
  70. return -EINVAL;
  71. /* Read the entries from the table */
  72. thrs_section = 0;
  73. sensor_section = -1;
  74. i = 0;
  75. while ((entry = nvbios_therm_entry(bios, i++, &ver, &len))) {
  76. s16 value = nvbios_rd16(bios, entry + 1);
  77. switch (nvbios_rd08(bios, entry + 0)) {
  78. case 0x0:
  79. thrs_section = value;
  80. if (value > 0)
  81. return 0; /* we do not try to support ambient */
  82. break;
  83. case 0x01:
  84. sensor_section++;
  85. if (sensor_section == 0) {
  86. offset = ((s8) nvbios_rd08(bios, entry + 2)) / 2;
  87. sensor->offset_constant = offset;
  88. }
  89. break;
  90. case 0x04:
  91. if (thrs_section == 0) {
  92. sensor->thrs_critical.temp = (value & 0xff0) >> 4;
  93. sensor->thrs_critical.hysteresis = value & 0xf;
  94. }
  95. break;
  96. case 0x07:
  97. if (thrs_section == 0) {
  98. sensor->thrs_down_clock.temp = (value & 0xff0) >> 4;
  99. sensor->thrs_down_clock.hysteresis = value & 0xf;
  100. }
  101. break;
  102. case 0x08:
  103. if (thrs_section == 0) {
  104. sensor->thrs_fan_boost.temp = (value & 0xff0) >> 4;
  105. sensor->thrs_fan_boost.hysteresis = value & 0xf;
  106. }
  107. break;
  108. case 0x10:
  109. if (sensor_section == 0)
  110. sensor->offset_num = value;
  111. break;
  112. case 0x11:
  113. if (sensor_section == 0)
  114. sensor->offset_den = value;
  115. break;
  116. case 0x12:
  117. if (sensor_section == 0)
  118. sensor->slope_mult = value;
  119. break;
  120. case 0x13:
  121. if (sensor_section == 0)
  122. sensor->slope_div = value;
  123. break;
  124. case 0x32:
  125. if (thrs_section == 0) {
  126. sensor->thrs_shutdown.temp = (value & 0xff0) >> 4;
  127. sensor->thrs_shutdown.hysteresis = value & 0xf;
  128. }
  129. break;
  130. }
  131. }
  132. return 0;
  133. }
  134. int
  135. nvbios_therm_fan_parse(struct nvkm_bios *bios, struct nvbios_therm_fan *fan)
  136. {
  137. struct nvbios_therm_trip_point *cur_trip = NULL;
  138. u8 ver, len, i;
  139. u32 entry;
  140. uint8_t duty_lut[] = { 0, 0, 25, 0, 40, 0, 50, 0,
  141. 75, 0, 85, 0, 100, 0, 100, 0 };
  142. i = 0;
  143. fan->nr_fan_trip = 0;
  144. fan->fan_mode = NVBIOS_THERM_FAN_OTHER;
  145. while ((entry = nvbios_therm_entry(bios, i++, &ver, &len))) {
  146. s16 value = nvbios_rd16(bios, entry + 1);
  147. switch (nvbios_rd08(bios, entry + 0)) {
  148. case 0x22:
  149. fan->min_duty = value & 0xff;
  150. fan->max_duty = (value & 0xff00) >> 8;
  151. break;
  152. case 0x24:
  153. fan->nr_fan_trip++;
  154. if (fan->fan_mode > NVBIOS_THERM_FAN_TRIP)
  155. fan->fan_mode = NVBIOS_THERM_FAN_TRIP;
  156. cur_trip = &fan->trip[fan->nr_fan_trip - 1];
  157. cur_trip->hysteresis = value & 0xf;
  158. cur_trip->temp = (value & 0xff0) >> 4;
  159. cur_trip->fan_duty = duty_lut[(value & 0xf000) >> 12];
  160. break;
  161. case 0x25:
  162. cur_trip = &fan->trip[fan->nr_fan_trip - 1];
  163. cur_trip->fan_duty = value;
  164. break;
  165. case 0x26:
  166. if (!fan->pwm_freq)
  167. fan->pwm_freq = value;
  168. break;
  169. case 0x3b:
  170. fan->bump_period = value;
  171. break;
  172. case 0x3c:
  173. fan->slow_down_period = value;
  174. break;
  175. case 0x46:
  176. if (fan->fan_mode > NVBIOS_THERM_FAN_LINEAR)
  177. fan->fan_mode = NVBIOS_THERM_FAN_LINEAR;
  178. fan->linear_min_temp = nvbios_rd08(bios, entry + 1);
  179. fan->linear_max_temp = nvbios_rd08(bios, entry + 2);
  180. break;
  181. }
  182. }
  183. /* starting from fermi, fan management is always linear */
  184. if (bios->subdev.device->card_type >= NV_C0 &&
  185. fan->fan_mode == NVBIOS_THERM_FAN_OTHER) {
  186. fan->fan_mode = NVBIOS_THERM_FAN_LINEAR;
  187. }
  188. return 0;
  189. }