nouveau_perf.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. static void
  28. legacy_perf_init(struct drm_device *dev)
  29. {
  30. struct drm_nouveau_private *dev_priv = dev->dev_private;
  31. struct nvbios *bios = &dev_priv->vbios;
  32. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  33. char *perf, *entry, *bmp = &bios->data[bios->offset];
  34. int headerlen, use_straps;
  35. if (bmp[5] < 0x5 || bmp[6] < 0x14) {
  36. NV_DEBUG(dev, "BMP version too old for perf\n");
  37. return;
  38. }
  39. perf = ROMPTR(bios, bmp[0x73]);
  40. if (!perf) {
  41. NV_DEBUG(dev, "No memclock table pointer found.\n");
  42. return;
  43. }
  44. switch (perf[0]) {
  45. case 0x12:
  46. case 0x14:
  47. case 0x18:
  48. use_straps = 0;
  49. headerlen = 1;
  50. break;
  51. case 0x01:
  52. use_straps = perf[1] & 1;
  53. headerlen = (use_straps ? 8 : 2);
  54. break;
  55. default:
  56. NV_WARN(dev, "Unknown memclock table version %x.\n", perf[0]);
  57. return;
  58. }
  59. entry = perf + headerlen;
  60. if (use_straps)
  61. entry += (nv_rd32(dev, NV_PEXTDEV_BOOT_0) & 0x3c) >> 1;
  62. sprintf(pm->perflvl[0].name, "performance_level_0");
  63. pm->perflvl[0].memory = ROM16(entry[0]) * 20;
  64. pm->nr_perflvl = 1;
  65. }
  66. static struct nouveau_pm_memtiming *
  67. nouveau_perf_timing(struct drm_device *dev, struct bit_entry *P,
  68. u16 memclk, u8 *entry, u8 recordlen, u8 entries)
  69. {
  70. struct drm_nouveau_private *dev_priv = dev->dev_private;
  71. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  72. struct nvbios *bios = &dev_priv->vbios;
  73. u8 ramcfg;
  74. int i;
  75. /* perf v2 has a separate "timing map" table, we have to match
  76. * the target memory clock to a specific entry, *then* use
  77. * ramcfg to select the correct subentry
  78. */
  79. if (P->version == 2) {
  80. u8 *tmap = ROMPTR(bios, P->data[4]);
  81. if (!tmap) {
  82. NV_DEBUG(dev, "no timing map pointer\n");
  83. return NULL;
  84. }
  85. if (tmap[0] != 0x10) {
  86. NV_WARN(dev, "timing map 0x%02x unknown\n", tmap[0]);
  87. return NULL;
  88. }
  89. entry = tmap + tmap[1];
  90. recordlen = tmap[2] + (tmap[4] * tmap[3]);
  91. for (i = 0; i < tmap[5]; i++, entry += recordlen) {
  92. if (memclk >= ROM16(entry[0]) &&
  93. memclk <= ROM16(entry[2]))
  94. break;
  95. }
  96. if (i == tmap[5]) {
  97. NV_WARN(dev, "no match in timing map table\n");
  98. return NULL;
  99. }
  100. entry += tmap[2];
  101. recordlen = tmap[3];
  102. entries = tmap[4];
  103. }
  104. ramcfg = (nv_rd32(dev, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
  105. if (bios->ram_restrict_tbl_ptr)
  106. ramcfg = bios->data[bios->ram_restrict_tbl_ptr + ramcfg];
  107. if (ramcfg >= entries) {
  108. NV_WARN(dev, "ramcfg strap out of bounds!\n");
  109. return NULL;
  110. }
  111. entry += ramcfg * recordlen;
  112. if (entry[1] >= pm->memtimings.nr_timing) {
  113. NV_WARN(dev, "timingset %d does not exist\n", entry[1]);
  114. return NULL;
  115. }
  116. return &pm->memtimings.timing[entry[1]];
  117. }
  118. void
  119. nouveau_perf_init(struct drm_device *dev)
  120. {
  121. struct drm_nouveau_private *dev_priv = dev->dev_private;
  122. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  123. struct nvbios *bios = &dev_priv->vbios;
  124. struct bit_entry P;
  125. u8 version, headerlen, recordlen, entries;
  126. u8 *perf, *entry;
  127. int vid, i;
  128. if (bios->type == NVBIOS_BIT) {
  129. if (bit_table(dev, 'P', &P))
  130. return;
  131. if (P.version != 1 && P.version != 2) {
  132. NV_WARN(dev, "unknown perf for BIT P %d\n", P.version);
  133. return;
  134. }
  135. perf = ROMPTR(bios, P.data[0]);
  136. version = perf[0];
  137. headerlen = perf[1];
  138. if (version < 0x40) {
  139. recordlen = perf[3] + (perf[4] * perf[5]);
  140. entries = perf[2];
  141. } else {
  142. recordlen = perf[2] + (perf[3] * perf[4]);
  143. entries = perf[5];
  144. }
  145. } else {
  146. if (bios->data[bios->offset + 6] < 0x25) {
  147. legacy_perf_init(dev);
  148. return;
  149. }
  150. perf = ROMPTR(bios, bios->data[bios->offset + 0x94]);
  151. if (!perf) {
  152. NV_DEBUG(dev, "perf table pointer invalid\n");
  153. return;
  154. }
  155. version = perf[1];
  156. headerlen = perf[0];
  157. recordlen = perf[3];
  158. entries = perf[2];
  159. }
  160. if (entries > NOUVEAU_PM_MAX_LEVEL) {
  161. NV_DEBUG(dev, "perf table has too many entries - buggy vbios?\n");
  162. entries = NOUVEAU_PM_MAX_LEVEL;
  163. }
  164. entry = perf + headerlen;
  165. for (i = 0; i < entries; i++) {
  166. struct nouveau_pm_level *perflvl = &pm->perflvl[pm->nr_perflvl];
  167. perflvl->timing = NULL;
  168. if (entry[0] == 0xff) {
  169. entry += recordlen;
  170. continue;
  171. }
  172. switch (version) {
  173. case 0x12:
  174. case 0x13:
  175. case 0x15:
  176. perflvl->fanspeed = entry[55];
  177. perflvl->voltage = (recordlen > 56) ? entry[56] : 0;
  178. perflvl->core = ROM32(entry[1]) * 10;
  179. perflvl->memory = ROM32(entry[5]) * 20;
  180. break;
  181. case 0x21:
  182. case 0x23:
  183. case 0x24:
  184. perflvl->fanspeed = entry[4];
  185. perflvl->voltage = entry[5];
  186. perflvl->core = ROM16(entry[6]) * 1000;
  187. if (dev_priv->chipset == 0x49 ||
  188. dev_priv->chipset == 0x4b)
  189. perflvl->memory = ROM16(entry[11]) * 1000;
  190. else
  191. perflvl->memory = ROM16(entry[11]) * 2000;
  192. break;
  193. case 0x25:
  194. perflvl->fanspeed = entry[4];
  195. perflvl->voltage = entry[5];
  196. perflvl->core = ROM16(entry[6]) * 1000;
  197. perflvl->shader = ROM16(entry[10]) * 1000;
  198. perflvl->memory = ROM16(entry[12]) * 1000;
  199. break;
  200. case 0x30:
  201. perflvl->memscript = ROM16(entry[2]);
  202. case 0x35:
  203. perflvl->fanspeed = entry[6];
  204. perflvl->voltage = entry[7];
  205. perflvl->core = ROM16(entry[8]) * 1000;
  206. perflvl->shader = ROM16(entry[10]) * 1000;
  207. perflvl->memory = ROM16(entry[12]) * 1000;
  208. /*XXX: confirm on 0x35 */
  209. perflvl->unk05 = ROM16(entry[16]) * 1000;
  210. break;
  211. case 0x40:
  212. #define subent(n) entry[perf[2] + ((n) * perf[3])]
  213. perflvl->fanspeed = 0; /*XXX*/
  214. perflvl->voltage = entry[2];
  215. if (dev_priv->card_type == NV_50) {
  216. perflvl->core = ROM16(subent(0)) & 0xfff;
  217. perflvl->shader = ROM16(subent(1)) & 0xfff;
  218. perflvl->memory = ROM16(subent(2)) & 0xfff;
  219. } else {
  220. perflvl->shader = ROM16(subent(3)) & 0xfff;
  221. perflvl->core = perflvl->shader / 2;
  222. perflvl->unk0a = ROM16(subent(4)) & 0xfff;
  223. perflvl->memory = ROM16(subent(5)) & 0xfff;
  224. }
  225. perflvl->core *= 1000;
  226. perflvl->shader *= 1000;
  227. perflvl->memory *= 1000;
  228. perflvl->unk0a *= 1000;
  229. break;
  230. }
  231. /* make sure vid is valid */
  232. if (pm->voltage.supported && perflvl->voltage) {
  233. vid = nouveau_volt_vid_lookup(dev, perflvl->voltage);
  234. if (vid < 0) {
  235. NV_DEBUG(dev, "drop perflvl %d, bad vid\n", i);
  236. entry += recordlen;
  237. continue;
  238. }
  239. }
  240. /* get the corresponding memory timings */
  241. if (version > 0x15) {
  242. /* last 3 args are for < 0x40, ignored for >= 0x40 */
  243. perflvl->timing =
  244. nouveau_perf_timing(dev, &P,
  245. perflvl->memory / 1000,
  246. entry + perf[3],
  247. perf[5], perf[4]);
  248. }
  249. snprintf(perflvl->name, sizeof(perflvl->name),
  250. "performance_level_%d", i);
  251. perflvl->id = i;
  252. pm->nr_perflvl++;
  253. entry += recordlen;
  254. }
  255. }
  256. void
  257. nouveau_perf_fini(struct drm_device *dev)
  258. {
  259. }