nouveau_volt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright 2010 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 "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_pm.h"
  27. #include "nouveau_gpio.h"
  28. static const enum dcb_gpio_tag vidtag[] = { 0x04, 0x05, 0x06, 0x1a, 0x73 };
  29. static int nr_vidtag = sizeof(vidtag) / sizeof(vidtag[0]);
  30. int
  31. nouveau_voltage_gpio_get(struct drm_device *dev)
  32. {
  33. struct drm_nouveau_private *dev_priv = dev->dev_private;
  34. struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
  35. u8 vid = 0;
  36. int i;
  37. for (i = 0; i < nr_vidtag; i++) {
  38. if (!(volt->vid_mask & (1 << i)))
  39. continue;
  40. vid |= nouveau_gpio_func_get(dev, vidtag[i]) << i;
  41. }
  42. return nouveau_volt_lvl_lookup(dev, vid);
  43. }
  44. int
  45. nouveau_voltage_gpio_set(struct drm_device *dev, int voltage)
  46. {
  47. struct drm_nouveau_private *dev_priv = dev->dev_private;
  48. struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
  49. int vid, i;
  50. vid = nouveau_volt_vid_lookup(dev, voltage);
  51. if (vid < 0)
  52. return vid;
  53. for (i = 0; i < nr_vidtag; i++) {
  54. if (!(volt->vid_mask & (1 << i)))
  55. continue;
  56. nouveau_gpio_func_set(dev, vidtag[i], !!(vid & (1 << i)));
  57. }
  58. return 0;
  59. }
  60. int
  61. nouveau_volt_vid_lookup(struct drm_device *dev, int voltage)
  62. {
  63. struct drm_nouveau_private *dev_priv = dev->dev_private;
  64. struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
  65. int i;
  66. for (i = 0; i < volt->nr_level; i++) {
  67. if (volt->level[i].voltage == voltage)
  68. return volt->level[i].vid;
  69. }
  70. return -ENOENT;
  71. }
  72. int
  73. nouveau_volt_lvl_lookup(struct drm_device *dev, int vid)
  74. {
  75. struct drm_nouveau_private *dev_priv = dev->dev_private;
  76. struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
  77. int i;
  78. for (i = 0; i < volt->nr_level; i++) {
  79. if (volt->level[i].vid == vid)
  80. return volt->level[i].voltage;
  81. }
  82. return -ENOENT;
  83. }
  84. void
  85. nouveau_volt_init(struct drm_device *dev)
  86. {
  87. struct drm_nouveau_private *dev_priv = dev->dev_private;
  88. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  89. struct nouveau_pm_voltage *voltage = &pm->voltage;
  90. struct nvbios *bios = &dev_priv->vbios;
  91. struct bit_entry P;
  92. u8 *volt = NULL, *entry;
  93. int i, headerlen, recordlen, entries, vidmask, vidshift;
  94. if (bios->type == NVBIOS_BIT) {
  95. if (bit_table(dev, 'P', &P))
  96. return;
  97. if (P.version == 1)
  98. volt = ROMPTR(dev, P.data[16]);
  99. else
  100. if (P.version == 2)
  101. volt = ROMPTR(dev, P.data[12]);
  102. else {
  103. NV_WARN(dev, "unknown volt for BIT P %d\n", P.version);
  104. }
  105. } else {
  106. if (bios->data[bios->offset + 6] < 0x27) {
  107. NV_DEBUG(dev, "BMP version too old for voltage\n");
  108. return;
  109. }
  110. volt = ROMPTR(dev, bios->data[bios->offset + 0x98]);
  111. }
  112. if (!volt) {
  113. NV_DEBUG(dev, "voltage table pointer invalid\n");
  114. return;
  115. }
  116. switch (volt[0]) {
  117. case 0x10:
  118. case 0x11:
  119. case 0x12:
  120. headerlen = 5;
  121. recordlen = volt[1];
  122. entries = volt[2];
  123. vidshift = 0;
  124. vidmask = volt[4];
  125. break;
  126. case 0x20:
  127. headerlen = volt[1];
  128. recordlen = volt[3];
  129. entries = volt[2];
  130. vidshift = 0; /* could be vidshift like 0x30? */
  131. vidmask = volt[5];
  132. break;
  133. case 0x30:
  134. headerlen = volt[1];
  135. recordlen = volt[2];
  136. entries = volt[3];
  137. vidmask = volt[4];
  138. /* no longer certain what volt[5] is, if it's related to
  139. * the vid shift then it's definitely not a function of
  140. * how many bits are set.
  141. *
  142. * after looking at a number of nva3+ vbios images, they
  143. * all seem likely to have a static shift of 2.. lets
  144. * go with that for now until proven otherwise.
  145. */
  146. vidshift = 2;
  147. break;
  148. case 0x40:
  149. headerlen = volt[1];
  150. recordlen = volt[2];
  151. entries = volt[3]; /* not a clue what the entries are for.. */
  152. vidmask = volt[11]; /* guess.. */
  153. vidshift = 0;
  154. break;
  155. default:
  156. NV_WARN(dev, "voltage table 0x%02x unknown\n", volt[0]);
  157. return;
  158. }
  159. /* validate vid mask */
  160. voltage->vid_mask = vidmask;
  161. if (!voltage->vid_mask)
  162. return;
  163. i = 0;
  164. while (vidmask) {
  165. if (i > nr_vidtag) {
  166. NV_DEBUG(dev, "vid bit %d unknown\n", i);
  167. return;
  168. }
  169. if (!nouveau_gpio_func_valid(dev, vidtag[i])) {
  170. NV_DEBUG(dev, "vid bit %d has no gpio tag\n", i);
  171. return;
  172. }
  173. vidmask >>= 1;
  174. i++;
  175. }
  176. /* parse vbios entries into common format */
  177. voltage->version = volt[0];
  178. if (voltage->version < 0x40) {
  179. voltage->nr_level = entries;
  180. voltage->level =
  181. kcalloc(entries, sizeof(*voltage->level), GFP_KERNEL);
  182. if (!voltage->level)
  183. return;
  184. entry = volt + headerlen;
  185. for (i = 0; i < entries; i++, entry += recordlen) {
  186. voltage->level[i].voltage = entry[0] * 10000;
  187. voltage->level[i].vid = entry[1] >> vidshift;
  188. }
  189. } else {
  190. u32 volt_uv = ROM32(volt[4]);
  191. s16 step_uv = ROM16(volt[8]);
  192. u8 vid;
  193. voltage->nr_level = voltage->vid_mask + 1;
  194. voltage->level = kcalloc(voltage->nr_level,
  195. sizeof(*voltage->level), GFP_KERNEL);
  196. if (!voltage->level)
  197. return;
  198. for (vid = 0; vid <= voltage->vid_mask; vid++) {
  199. voltage->level[vid].voltage = volt_uv;
  200. voltage->level[vid].vid = vid;
  201. volt_uv += step_uv;
  202. }
  203. }
  204. voltage->supported = true;
  205. }
  206. void
  207. nouveau_volt_fini(struct drm_device *dev)
  208. {
  209. struct drm_nouveau_private *dev_priv = dev->dev_private;
  210. struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
  211. kfree(volt->level);
  212. }