nouveau_perf.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
  164. struct nouveau_pm_tbl_header mt_hdr;
  165. u8 version, headerlen, recordlen, entries;
  166. u8 *perf, *entry;
  167. int vid, i;
  168. if (bios->type == NVBIOS_BIT) {
  169. if (bit_table(dev, 'P', &P))
  170. return;
  171. if (P.version != 1 && P.version != 2) {
  172. NV_WARN(dev, "unknown perf for BIT P %d\n", P.version);
  173. return;
  174. }
  175. perf = ROMPTR(bios, P.data[0]);
  176. version = perf[0];
  177. headerlen = perf[1];
  178. if (version < 0x40) {
  179. recordlen = perf[3] + (perf[4] * perf[5]);
  180. entries = perf[2];
  181. } else {
  182. recordlen = perf[2] + (perf[3] * perf[4]);
  183. entries = perf[5];
  184. }
  185. } else {
  186. if (bios->data[bios->offset + 6] < 0x25) {
  187. legacy_perf_init(dev);
  188. return;
  189. }
  190. perf = ROMPTR(bios, bios->data[bios->offset + 0x94]);
  191. if (!perf) {
  192. NV_DEBUG(dev, "perf table pointer invalid\n");
  193. return;
  194. }
  195. version = perf[1];
  196. headerlen = perf[0];
  197. recordlen = perf[3];
  198. entries = perf[2];
  199. }
  200. if (entries > NOUVEAU_PM_MAX_LEVEL) {
  201. NV_DEBUG(dev, "perf table has too many entries - buggy vbios?\n");
  202. entries = NOUVEAU_PM_MAX_LEVEL;
  203. }
  204. entry = perf + headerlen;
  205. /* For version 0x15, initialize memtiming table */
  206. if(version == 0x15) {
  207. memtimings->timing =
  208. kcalloc(entries, sizeof(*memtimings->timing), GFP_KERNEL);
  209. if(!memtimings) {
  210. NV_WARN(dev,"Could not allocate memtiming table\n");
  211. return;
  212. }
  213. mt_hdr.entry_cnt = entries;
  214. mt_hdr.entry_len = 14;
  215. mt_hdr.version = version;
  216. mt_hdr.header_len = 4;
  217. }
  218. for (i = 0; i < entries; i++) {
  219. struct nouveau_pm_level *perflvl = &pm->perflvl[pm->nr_perflvl];
  220. perflvl->timing = NULL;
  221. if (entry[0] == 0xff) {
  222. entry += recordlen;
  223. continue;
  224. }
  225. switch (version) {
  226. case 0x12:
  227. case 0x13:
  228. case 0x15:
  229. perflvl->fanspeed = entry[55];
  230. if (recordlen > 56)
  231. perflvl->volt_min = entry[56];
  232. perflvl->core = ROM32(entry[1]) * 10;
  233. perflvl->memory = ROM32(entry[5]) * 20;
  234. break;
  235. case 0x21:
  236. case 0x23:
  237. case 0x24:
  238. perflvl->fanspeed = entry[4];
  239. perflvl->volt_min = entry[5];
  240. perflvl->shader = ROM16(entry[6]) * 1000;
  241. perflvl->core = perflvl->shader;
  242. perflvl->core += (signed char)entry[8] * 1000;
  243. if (dev_priv->chipset == 0x49 ||
  244. dev_priv->chipset == 0x4b)
  245. perflvl->memory = ROM16(entry[11]) * 1000;
  246. else
  247. perflvl->memory = ROM16(entry[11]) * 2000;
  248. break;
  249. case 0x25:
  250. perflvl->fanspeed = entry[4];
  251. perflvl->volt_min = entry[5];
  252. perflvl->core = ROM16(entry[6]) * 1000;
  253. perflvl->shader = ROM16(entry[10]) * 1000;
  254. perflvl->memory = ROM16(entry[12]) * 1000;
  255. break;
  256. case 0x30:
  257. perflvl->memscript = ROM16(entry[2]);
  258. case 0x35:
  259. perflvl->fanspeed = entry[6];
  260. perflvl->volt_min = entry[7];
  261. perflvl->core = ROM16(entry[8]) * 1000;
  262. perflvl->shader = ROM16(entry[10]) * 1000;
  263. perflvl->memory = ROM16(entry[12]) * 1000;
  264. /*XXX: confirm on 0x35 */
  265. perflvl->unk05 = ROM16(entry[16]) * 1000;
  266. break;
  267. case 0x40:
  268. #define subent(n) (ROM16(entry[perf[2] + ((n) * perf[3])]) & 0xfff) * 1000
  269. perflvl->fanspeed = 0; /*XXX*/
  270. perflvl->volt_min = entry[2];
  271. if (dev_priv->card_type == NV_50) {
  272. perflvl->core = subent(0);
  273. perflvl->shader = subent(1);
  274. perflvl->memory = subent(2);
  275. perflvl->vdec = subent(3);
  276. perflvl->unka0 = subent(4);
  277. } else {
  278. perflvl->hub06 = subent(0);
  279. perflvl->hub01 = subent(1);
  280. perflvl->copy = subent(2);
  281. perflvl->shader = subent(3);
  282. perflvl->rop = subent(4);
  283. perflvl->memory = subent(5);
  284. perflvl->vdec = subent(6);
  285. perflvl->daemon = subent(10);
  286. perflvl->hub07 = subent(11);
  287. perflvl->core = perflvl->shader / 2;
  288. }
  289. break;
  290. }
  291. /* make sure vid is valid */
  292. nouveau_perf_voltage(dev, &P, perflvl);
  293. if (pm->voltage.supported && perflvl->volt_min) {
  294. vid = nouveau_volt_vid_lookup(dev, perflvl->volt_min);
  295. if (vid < 0) {
  296. NV_DEBUG(dev, "drop perflvl %d, bad vid\n", i);
  297. entry += recordlen;
  298. continue;
  299. }
  300. }
  301. /* get the corresponding memory timings */
  302. if (version == 0x15) {
  303. memtimings->timing[i].id = i;
  304. nv30_mem_timing_entry(dev,&mt_hdr,(struct nouveau_pm_tbl_entry*) &entry[41],0,&memtimings->timing[i]);
  305. perflvl->timing = &memtimings->timing[i];
  306. } else if (version > 0x15) {
  307. /* last 3 args are for < 0x40, ignored for >= 0x40 */
  308. perflvl->timing =
  309. nouveau_perf_timing(dev, &P,
  310. perflvl->memory / 1000,
  311. entry + perf[3],
  312. perf[5], perf[4]);
  313. }
  314. snprintf(perflvl->name, sizeof(perflvl->name),
  315. "performance_level_%d", i);
  316. perflvl->id = i;
  317. pm->nr_perflvl++;
  318. entry += recordlen;
  319. }
  320. }
  321. void
  322. nouveau_perf_fini(struct drm_device *dev)
  323. {
  324. }