base.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright 2013 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. */
  24. #include "priv.h"
  25. #include <subdev/bios.h>
  26. #include <subdev/bios/vmap.h>
  27. #include <subdev/bios/volt.h>
  28. #include <subdev/therm.h>
  29. int
  30. nvkm_volt_get(struct nvkm_volt *volt)
  31. {
  32. int ret, i;
  33. if (volt->func->volt_get)
  34. return volt->func->volt_get(volt);
  35. ret = volt->func->vid_get(volt);
  36. if (ret >= 0) {
  37. for (i = 0; i < volt->vid_nr; i++) {
  38. if (volt->vid[i].vid == ret)
  39. return volt->vid[i].uv;
  40. }
  41. ret = -EINVAL;
  42. }
  43. return ret;
  44. }
  45. static int
  46. nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
  47. {
  48. struct nvkm_subdev *subdev = &volt->subdev;
  49. int i, ret = -EINVAL, best_err = volt->max_uv, best = -1;
  50. if (volt->func->volt_set)
  51. return volt->func->volt_set(volt, uv);
  52. for (i = 0; i < volt->vid_nr; i++) {
  53. int err = volt->vid[i].uv - uv;
  54. if (err < 0 || err > best_err)
  55. continue;
  56. best_err = err;
  57. best = i;
  58. if (best_err == 0)
  59. break;
  60. }
  61. if (best == -1) {
  62. nvkm_error(subdev, "couldn't set %iuv\n", uv);
  63. return ret;
  64. }
  65. ret = volt->func->vid_set(volt, volt->vid[best].vid);
  66. nvkm_debug(subdev, "set req %duv to %duv: %d\n", uv,
  67. volt->vid[best].uv, ret);
  68. return ret;
  69. }
  70. int
  71. nvkm_volt_map_min(struct nvkm_volt *volt, u8 id)
  72. {
  73. struct nvkm_bios *bios = volt->subdev.device->bios;
  74. struct nvbios_vmap_entry info;
  75. u8 ver, len;
  76. u32 vmap;
  77. vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
  78. if (vmap) {
  79. if (info.link != 0xff) {
  80. int ret = nvkm_volt_map_min(volt, info.link);
  81. if (ret < 0)
  82. return ret;
  83. info.min += ret;
  84. }
  85. return info.min;
  86. }
  87. return id ? id * 10000 : -ENODEV;
  88. }
  89. int
  90. nvkm_volt_map(struct nvkm_volt *volt, u8 id, u8 temp)
  91. {
  92. struct nvkm_bios *bios = volt->subdev.device->bios;
  93. struct nvbios_vmap_entry info;
  94. u8 ver, len;
  95. u32 vmap;
  96. vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
  97. if (vmap) {
  98. s64 result;
  99. if (volt->speedo < 0)
  100. return volt->speedo;
  101. if (ver == 0x10 || (ver == 0x20 && info.mode == 0)) {
  102. result = div64_s64((s64)info.arg[0], 10);
  103. result += div64_s64((s64)info.arg[1] * volt->speedo, 10);
  104. result += div64_s64((s64)info.arg[2] * volt->speedo * volt->speedo, 100000);
  105. } else if (ver == 0x20) {
  106. switch (info.mode) {
  107. /* 0x0 handled above! */
  108. case 0x1:
  109. result = ((s64)info.arg[0] * 15625) >> 18;
  110. result += ((s64)info.arg[1] * volt->speedo * 15625) >> 18;
  111. result += ((s64)info.arg[2] * temp * 15625) >> 10;
  112. result += ((s64)info.arg[3] * volt->speedo * temp * 15625) >> 18;
  113. result += ((s64)info.arg[4] * volt->speedo * volt->speedo * 15625) >> 30;
  114. result += ((s64)info.arg[5] * temp * temp * 15625) >> 18;
  115. break;
  116. case 0x3:
  117. result = (info.min + info.max) / 2;
  118. break;
  119. case 0x2:
  120. default:
  121. result = info.min;
  122. break;
  123. }
  124. } else {
  125. return -ENODEV;
  126. }
  127. result = min(max(result, (s64)info.min), (s64)info.max);
  128. if (info.link != 0xff) {
  129. int ret = nvkm_volt_map(volt, info.link, temp);
  130. if (ret < 0)
  131. return ret;
  132. result += ret;
  133. }
  134. return result;
  135. }
  136. return id ? id * 10000 : -ENODEV;
  137. }
  138. int
  139. nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, u8 min_id, u8 temp,
  140. int condition)
  141. {
  142. int ret;
  143. if (volt->func->set_id)
  144. return volt->func->set_id(volt, id, condition);
  145. ret = nvkm_volt_map(volt, id, temp);
  146. if (ret >= 0) {
  147. int prev = nvkm_volt_get(volt);
  148. if (!condition || prev < 0 ||
  149. (condition < 0 && ret < prev) ||
  150. (condition > 0 && ret > prev)) {
  151. int min = nvkm_volt_map(volt, min_id, temp);
  152. if (min >= 0)
  153. ret = max(min, ret);
  154. ret = nvkm_volt_set(volt, ret);
  155. } else {
  156. ret = 0;
  157. }
  158. }
  159. return ret;
  160. }
  161. static void
  162. nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt)
  163. {
  164. struct nvkm_subdev *subdev = &bios->subdev;
  165. struct nvbios_volt_entry ivid;
  166. struct nvbios_volt info;
  167. u8 ver, hdr, cnt, len;
  168. u32 data;
  169. int i;
  170. data = nvbios_volt_parse(bios, &ver, &hdr, &cnt, &len, &info);
  171. if (data && info.vidmask && info.base && info.step && info.ranged) {
  172. nvkm_debug(subdev, "found ranged based VIDs\n");
  173. volt->min_uv = info.min;
  174. volt->max_uv = info.max;
  175. for (i = 0; i < info.vidmask + 1; i++) {
  176. if (info.base >= info.min &&
  177. info.base <= info.max) {
  178. volt->vid[volt->vid_nr].uv = info.base;
  179. volt->vid[volt->vid_nr].vid = i;
  180. volt->vid_nr++;
  181. }
  182. info.base += info.step;
  183. }
  184. volt->vid_mask = info.vidmask;
  185. } else if (data && info.vidmask && !info.ranged) {
  186. nvkm_debug(subdev, "found entry based VIDs\n");
  187. volt->min_uv = 0xffffffff;
  188. volt->max_uv = 0;
  189. for (i = 0; i < cnt; i++) {
  190. data = nvbios_volt_entry_parse(bios, i, &ver, &hdr,
  191. &ivid);
  192. if (data) {
  193. volt->vid[volt->vid_nr].uv = ivid.voltage;
  194. volt->vid[volt->vid_nr].vid = ivid.vid;
  195. volt->vid_nr++;
  196. volt->min_uv = min(volt->min_uv, ivid.voltage);
  197. volt->max_uv = max(volt->max_uv, ivid.voltage);
  198. }
  199. }
  200. volt->vid_mask = info.vidmask;
  201. } else if (data && info.type == NVBIOS_VOLT_PWM) {
  202. volt->min_uv = info.base;
  203. volt->max_uv = info.base + info.pwm_range;
  204. }
  205. }
  206. static int
  207. nvkm_volt_speedo_read(struct nvkm_volt *volt)
  208. {
  209. if (volt->func->speedo_read)
  210. return volt->func->speedo_read(volt);
  211. return -EINVAL;
  212. }
  213. static int
  214. nvkm_volt_init(struct nvkm_subdev *subdev)
  215. {
  216. struct nvkm_volt *volt = nvkm_volt(subdev);
  217. int ret = nvkm_volt_get(volt);
  218. if (ret < 0) {
  219. if (ret != -ENODEV)
  220. nvkm_debug(subdev, "current voltage unknown\n");
  221. return 0;
  222. }
  223. nvkm_debug(subdev, "current voltage: %duv\n", ret);
  224. return 0;
  225. }
  226. static int
  227. nvkm_volt_oneinit(struct nvkm_subdev *subdev)
  228. {
  229. struct nvkm_volt *volt = nvkm_volt(subdev);
  230. volt->speedo = nvkm_volt_speedo_read(volt);
  231. if (volt->speedo > 0)
  232. nvkm_debug(&volt->subdev, "speedo %x\n", volt->speedo);
  233. if (volt->func->oneinit)
  234. return volt->func->oneinit(volt);
  235. return 0;
  236. }
  237. static void *
  238. nvkm_volt_dtor(struct nvkm_subdev *subdev)
  239. {
  240. return nvkm_volt(subdev);
  241. }
  242. static const struct nvkm_subdev_func
  243. nvkm_volt = {
  244. .dtor = nvkm_volt_dtor,
  245. .init = nvkm_volt_init,
  246. .oneinit = nvkm_volt_oneinit,
  247. };
  248. void
  249. nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device,
  250. int index, struct nvkm_volt *volt)
  251. {
  252. struct nvkm_bios *bios = device->bios;
  253. int i;
  254. nvkm_subdev_ctor(&nvkm_volt, device, index, &volt->subdev);
  255. volt->func = func;
  256. /* Assuming the non-bios device should build the voltage table later */
  257. if (bios) {
  258. u8 ver, hdr, cnt, len;
  259. struct nvbios_vmap vmap;
  260. nvkm_volt_parse_bios(bios, volt);
  261. nvkm_debug(&volt->subdev, "min: %iuv max: %iuv\n",
  262. volt->min_uv, volt->max_uv);
  263. if (nvbios_vmap_parse(bios, &ver, &hdr, &cnt, &len, &vmap)) {
  264. volt->max0_id = vmap.max0;
  265. volt->max1_id = vmap.max1;
  266. volt->max2_id = vmap.max2;
  267. } else {
  268. volt->max0_id = 0xff;
  269. volt->max1_id = 0xff;
  270. volt->max2_id = 0xff;
  271. }
  272. }
  273. if (volt->vid_nr) {
  274. for (i = 0; i < volt->vid_nr; i++) {
  275. nvkm_debug(&volt->subdev, "VID %02x: %duv\n",
  276. volt->vid[i].vid, volt->vid[i].uv);
  277. }
  278. }
  279. }
  280. int
  281. nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device,
  282. int index, struct nvkm_volt **pvolt)
  283. {
  284. if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL)))
  285. return -ENOMEM;
  286. nvkm_volt_ctor(func, device, index, *pvolt);
  287. return 0;
  288. }