base.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. int
  29. nvkm_volt_get(struct nvkm_volt *volt)
  30. {
  31. int ret = volt->func->vid_get(volt), i;
  32. if (ret >= 0) {
  33. for (i = 0; i < volt->vid_nr; i++) {
  34. if (volt->vid[i].vid == ret)
  35. return volt->vid[i].uv;
  36. }
  37. ret = -EINVAL;
  38. }
  39. return ret;
  40. }
  41. static int
  42. nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
  43. {
  44. struct nvkm_subdev *subdev = &volt->subdev;
  45. int i, ret = -EINVAL;
  46. for (i = 0; i < volt->vid_nr; i++) {
  47. if (volt->vid[i].uv == uv) {
  48. ret = volt->func->vid_set(volt, volt->vid[i].vid);
  49. nvkm_debug(subdev, "set %duv: %d\n", uv, ret);
  50. break;
  51. }
  52. }
  53. return ret;
  54. }
  55. static int
  56. nvkm_volt_map(struct nvkm_volt *volt, u8 id)
  57. {
  58. struct nvkm_bios *bios = volt->subdev.device->bios;
  59. struct nvbios_vmap_entry info;
  60. u8 ver, len;
  61. u16 vmap;
  62. vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
  63. if (vmap) {
  64. if (info.link != 0xff) {
  65. int ret = nvkm_volt_map(volt, info.link);
  66. if (ret < 0)
  67. return ret;
  68. info.min += ret;
  69. }
  70. return info.min;
  71. }
  72. return id ? id * 10000 : -ENODEV;
  73. }
  74. int
  75. nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, int condition)
  76. {
  77. int ret;
  78. if (volt->func->set_id)
  79. return volt->func->set_id(volt, id, condition);
  80. ret = nvkm_volt_map(volt, id);
  81. if (ret >= 0) {
  82. int prev = nvkm_volt_get(volt);
  83. if (!condition || prev < 0 ||
  84. (condition < 0 && ret < prev) ||
  85. (condition > 0 && ret > prev)) {
  86. ret = nvkm_volt_set(volt, ret);
  87. } else {
  88. ret = 0;
  89. }
  90. }
  91. return ret;
  92. }
  93. static void
  94. nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt)
  95. {
  96. struct nvbios_volt_entry ivid;
  97. struct nvbios_volt info;
  98. u8 ver, hdr, cnt, len;
  99. u16 data;
  100. int i;
  101. data = nvbios_volt_parse(bios, &ver, &hdr, &cnt, &len, &info);
  102. if (data && info.vidmask && info.base && info.step) {
  103. for (i = 0; i < info.vidmask + 1; i++) {
  104. if (info.base >= info.min &&
  105. info.base <= info.max) {
  106. volt->vid[volt->vid_nr].uv = info.base;
  107. volt->vid[volt->vid_nr].vid = i;
  108. volt->vid_nr++;
  109. }
  110. info.base += info.step;
  111. }
  112. volt->vid_mask = info.vidmask;
  113. } else if (data && info.vidmask) {
  114. for (i = 0; i < cnt; i++) {
  115. data = nvbios_volt_entry_parse(bios, i, &ver, &hdr,
  116. &ivid);
  117. if (data) {
  118. volt->vid[volt->vid_nr].uv = ivid.voltage;
  119. volt->vid[volt->vid_nr].vid = ivid.vid;
  120. volt->vid_nr++;
  121. }
  122. }
  123. volt->vid_mask = info.vidmask;
  124. }
  125. }
  126. static int
  127. nvkm_volt_init(struct nvkm_subdev *subdev)
  128. {
  129. struct nvkm_volt *volt = nvkm_volt(subdev);
  130. int ret = nvkm_volt_get(volt);
  131. if (ret < 0) {
  132. if (ret != -ENODEV)
  133. nvkm_debug(subdev, "current voltage unknown\n");
  134. return 0;
  135. }
  136. nvkm_debug(subdev, "current voltage: %duv\n", ret);
  137. return 0;
  138. }
  139. static void *
  140. nvkm_volt_dtor(struct nvkm_subdev *subdev)
  141. {
  142. return nvkm_volt(subdev);
  143. }
  144. static const struct nvkm_subdev_func
  145. nvkm_volt = {
  146. .dtor = nvkm_volt_dtor,
  147. .init = nvkm_volt_init,
  148. };
  149. void
  150. nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device,
  151. int index, struct nvkm_volt *volt)
  152. {
  153. struct nvkm_bios *bios = device->bios;
  154. int i;
  155. nvkm_subdev_ctor(&nvkm_volt, device, index, 0, &volt->subdev);
  156. volt->func = func;
  157. /* Assuming the non-bios device should build the voltage table later */
  158. if (bios)
  159. nvkm_volt_parse_bios(bios, volt);
  160. if (volt->vid_nr) {
  161. for (i = 0; i < volt->vid_nr; i++) {
  162. nvkm_debug(&volt->subdev, "VID %02x: %duv\n",
  163. volt->vid[i].vid, volt->vid[i].uv);
  164. }
  165. }
  166. }
  167. int
  168. nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device,
  169. int index, struct nvkm_volt **pvolt)
  170. {
  171. if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL)))
  172. return -ENOMEM;
  173. nvkm_volt_ctor(func, device, index, *pvolt);
  174. return 0;
  175. }