nouveau_perf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. static void
  119. nouveau_perf_voltage(struct drm_device *dev, struct bit_entry *P,
  120. struct nouveau_pm_level *perflvl)
  121. {
  122. struct drm_nouveau_private *dev_priv = dev->dev_private;
  123. struct nvbios *bios = &dev_priv->vbios;
  124. u8 *vmap;
  125. int id;
  126. id = perflvl->volt_min;
  127. perflvl->volt_min = 0;
  128. /* boards using voltage table version <0x40 store the voltage
  129. * level directly in the perflvl entry as a multiple of 10mV
  130. */
  131. if (dev_priv->engine.pm.voltage.version < 0x40) {
  132. perflvl->volt_min = id * 10000;
  133. perflvl->volt_max = perflvl->volt_min;
  134. return;
  135. }
  136. /* on newer ones, the perflvl stores an index into yet another
  137. * vbios table containing a min/max voltage value for the perflvl
  138. */
  139. if (P->version != 2 || P->length < 34) {
  140. NV_DEBUG(dev, "where's our volt map table ptr? %d %d\n",
  141. P->version, P->length);
  142. return;
  143. }
  144. vmap = ROMPTR(bios, P->data[32]);
  145. if (!vmap) {
  146. NV_DEBUG(dev, "volt map table pointer invalid\n");
  147. return;
  148. }
  149. if (id < vmap[3]) {
  150. vmap += vmap[1] + (vmap[2] * id);
  151. perflvl->volt_min = ROM32(vmap[0]);
  152. perflvl->volt_max = ROM32(vmap[4]);
  153. }
  154. }
  155. void
  156. nouveau_perf_init(struct drm_device *dev)
  157. {
  158. struct drm_nouveau_private *dev_priv = dev->dev_private;
  159. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  160. struct nvbios *bios = &dev_priv->vbios;
  161. struct bit_entry P;
  162. u8 version, headerlen, recordlen, entries;
  163. u8 *perf, *entry;
  164. int vid, i;
  165. if (bios->type == NVBIOS_BIT) {
  166. if (bit_table(dev, 'P', &P))
  167. return;
  168. if (P.version != 1 && P.version != 2) {
  169. NV_WARN(dev, "unknown perf for BIT P %d\n", P.version);
  170. return;
  171. }
  172. perf = ROMPTR(bios, P.data[0]);
  173. version = perf[0];
  174. headerlen = perf[1];
  175. if (version < 0x40) {
  176. recordlen = perf[3] + (perf[4] * perf[5]);
  177. entries = perf[2];
  178. } else {
  179. recordlen = perf[2] + (perf[3] * perf[4]);
  180. entries = perf[5];
  181. }
  182. } else {
  183. if (bios->data[bios->offset + 6] < 0x25) {
  184. legacy_perf_init(dev);
  185. return;
  186. }
  187. perf = ROMPTR(bios, bios->data[bios->offset + 0x94]);
  188. if (!perf) {
  189. NV_DEBUG(dev, "perf table pointer invalid\n");
  190. return;
  191. }
  192. version = perf[1];
  193. headerlen = perf[0];
  194. recordlen = perf[3];
  195. entries = perf[2];
  196. }
  197. if (entries > NOUVEAU_PM_MAX_LEVEL) {
  198. NV_DEBUG(dev, "perf table has too many entries - buggy vbios?\n");
  199. entries = NOUVEAU_PM_MAX_LEVEL;
  200. }
  201. entry = perf + headerlen;
  202. for (i = 0; i < entries; i++) {
  203. struct nouveau_pm_level *perflvl = &pm->perflvl[pm->nr_perflvl];
  204. perflvl->timing = NULL;
  205. if (entry[0] == 0xff) {
  206. entry += recordlen;
  207. continue;
  208. }
  209. switch (version) {
  210. case 0x12:
  211. case 0x13:
  212. case 0x15:
  213. perflvl->fanspeed = entry[55];
  214. if (recordlen > 56)
  215. perflvl->volt_min = entry[56];
  216. perflvl->core = ROM32(entry[1]) * 10;
  217. perflvl->memory = ROM32(entry[5]) * 20;
  218. break;
  219. case 0x21:
  220. case 0x23:
  221. case 0x24:
  222. perflvl->fanspeed = entry[4];
  223. perflvl->volt_min = entry[5];
  224. perflvl->core = ROM16(entry[6]) * 1000;
  225. if (dev_priv->chipset == 0x49 ||
  226. dev_priv->chipset == 0x4b)
  227. perflvl->memory = ROM16(entry[11]) * 1000;
  228. else
  229. perflvl->memory = ROM16(entry[11]) * 2000;
  230. break;
  231. case 0x25:
  232. perflvl->fanspeed = entry[4];
  233. perflvl->volt_min = entry[5];
  234. perflvl->core = ROM16(entry[6]) * 1000;
  235. perflvl->shader = ROM16(entry[10]) * 1000;
  236. perflvl->memory = ROM16(entry[12]) * 1000;
  237. break;
  238. case 0x30:
  239. perflvl->memscript = ROM16(entry[2]);
  240. case 0x35:
  241. perflvl->fanspeed = entry[6];
  242. perflvl->volt_min = entry[7];
  243. perflvl->core = ROM16(entry[8]) * 1000;
  244. perflvl->shader = ROM16(entry[10]) * 1000;
  245. perflvl->memory = ROM16(entry[12]) * 1000;
  246. /*XXX: confirm on 0x35 */
  247. perflvl->unk05 = ROM16(entry[16]) * 1000;
  248. break;
  249. case 0x40:
  250. #define subent(n) (ROM16(entry[perf[2] + ((n) * perf[3])]) & 0xfff) * 1000
  251. perflvl->fanspeed = 0; /*XXX*/
  252. perflvl->volt_min = entry[2];
  253. if (dev_priv->card_type == NV_50) {
  254. perflvl->core = subent(0);
  255. perflvl->shader = subent(1);
  256. perflvl->memory = subent(2);
  257. perflvl->vdec = subent(3);
  258. perflvl->unka0 = subent(4);
  259. } else {
  260. perflvl->hub06 = subent(0);
  261. perflvl->hub01 = subent(1);
  262. perflvl->copy = subent(2);
  263. perflvl->shader = subent(3);
  264. perflvl->rop = subent(4);
  265. perflvl->memory = subent(5);
  266. perflvl->vdec = subent(6);
  267. perflvl->daemon = subent(10);
  268. perflvl->hub07 = subent(11);
  269. perflvl->core = perflvl->shader / 2;
  270. }
  271. break;
  272. }
  273. /* make sure vid is valid */
  274. nouveau_perf_voltage(dev, &P, perflvl);
  275. if (pm->voltage.supported && perflvl->volt_min) {
  276. vid = nouveau_volt_vid_lookup(dev, perflvl->volt_min);
  277. if (vid < 0) {
  278. NV_DEBUG(dev, "drop perflvl %d, bad vid\n", i);
  279. entry += recordlen;
  280. continue;
  281. }
  282. }
  283. /* get the corresponding memory timings */
  284. if (version > 0x15) {
  285. /* last 3 args are for < 0x40, ignored for >= 0x40 */
  286. perflvl->timing =
  287. nouveau_perf_timing(dev, &P,
  288. perflvl->memory / 1000,
  289. entry + perf[3],
  290. perf[5], perf[4]);
  291. }
  292. snprintf(perflvl->name, sizeof(perflvl->name),
  293. "performance_level_%d", i);
  294. perflvl->id = i;
  295. pm->nr_perflvl++;
  296. entry += recordlen;
  297. }
  298. }
  299. void
  300. nouveau_perf_fini(struct drm_device *dev)
  301. {
  302. }