nouveau_perf.c 8.8 KB

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