nouveau_perf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 u8 *
  28. nouveau_perf_table(struct drm_device *dev, u8 *ver)
  29. {
  30. struct drm_nouveau_private *dev_priv = dev->dev_private;
  31. struct nvbios *bios = &dev_priv->vbios;
  32. struct bit_entry P;
  33. if (!bit_table(dev, 'P', &P) && P.version && P.version <= 2) {
  34. u8 *perf = ROMPTR(dev, P.data[0]);
  35. if (perf) {
  36. *ver = perf[0];
  37. return perf;
  38. }
  39. }
  40. if (bios->type == NVBIOS_BMP) {
  41. if (bios->data[bios->offset + 6] >= 0x25) {
  42. u8 *perf = ROMPTR(dev, bios->data[bios->offset + 0x94]);
  43. if (perf) {
  44. *ver = perf[1];
  45. return perf;
  46. }
  47. }
  48. }
  49. return NULL;
  50. }
  51. static u8 *
  52. nouveau_perf_entry(struct drm_device *dev, int idx,
  53. u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
  54. {
  55. u8 *perf = nouveau_perf_table(dev, ver);
  56. if (perf) {
  57. if (*ver >= 0x12 && *ver < 0x20 && idx < perf[2]) {
  58. *hdr = perf[3];
  59. *cnt = 0;
  60. *len = 0;
  61. return perf + perf[0] + idx * perf[3];
  62. } else
  63. if (*ver >= 0x20 && *ver < 0x40 && idx < perf[2]) {
  64. *hdr = perf[3];
  65. *cnt = perf[4];
  66. *len = perf[5];
  67. return perf + perf[1] + idx * (*hdr + (*cnt * *len));
  68. } else
  69. if (*ver >= 0x40 && *ver < 0x41 && idx < perf[5]) {
  70. *hdr = perf[2];
  71. *cnt = perf[4];
  72. *len = perf[3];
  73. return perf + perf[1] + idx * (*hdr + (*cnt * *len));
  74. }
  75. }
  76. return NULL;
  77. }
  78. static u8 *
  79. nouveau_perf_rammap(struct drm_device *dev, u32 freq,
  80. u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
  81. {
  82. struct drm_nouveau_private *dev_priv = dev->dev_private;
  83. struct bit_entry P;
  84. u8 *perf, i = 0;
  85. if (!bit_table(dev, 'P', &P) && P.version == 2) {
  86. u8 *rammap = ROMPTR(dev, P.data[4]);
  87. if (rammap) {
  88. u8 *ramcfg = rammap + rammap[1];
  89. *ver = rammap[0];
  90. *hdr = rammap[2];
  91. *cnt = rammap[4];
  92. *len = rammap[3];
  93. freq /= 1000;
  94. for (i = 0; i < rammap[5]; i++) {
  95. if (freq >= ROM16(ramcfg[0]) &&
  96. freq <= ROM16(ramcfg[2]))
  97. return ramcfg;
  98. ramcfg += *hdr + (*cnt * *len);
  99. }
  100. }
  101. return NULL;
  102. }
  103. if (dev_priv->chipset == 0x49 ||
  104. dev_priv->chipset == 0x4b)
  105. freq /= 2;
  106. while ((perf = nouveau_perf_entry(dev, i++, ver, hdr, cnt, len))) {
  107. if (*ver >= 0x20 && *ver < 0x25) {
  108. if (perf[0] != 0xff && freq <= ROM16(perf[11]) * 1000)
  109. break;
  110. } else
  111. if (*ver >= 0x25 && *ver < 0x40) {
  112. if (perf[0] != 0xff && freq <= ROM16(perf[12]) * 1000)
  113. break;
  114. }
  115. }
  116. if (perf) {
  117. u8 *ramcfg = perf + *hdr;
  118. *ver = 0x00;
  119. *hdr = 0;
  120. return ramcfg;
  121. }
  122. return NULL;
  123. }
  124. u8 *
  125. nouveau_perf_ramcfg(struct drm_device *dev, u32 freq, u8 *ver, u8 *len)
  126. {
  127. struct drm_nouveau_private *dev_priv = dev->dev_private;
  128. struct nvbios *bios = &dev_priv->vbios;
  129. u8 strap, hdr, cnt;
  130. u8 *rammap;
  131. strap = (nv_rd32(dev, 0x101000) & 0x0000003c) >> 2;
  132. if (bios->ram_restrict_tbl_ptr)
  133. strap = bios->data[bios->ram_restrict_tbl_ptr + strap];
  134. rammap = nouveau_perf_rammap(dev, freq, ver, &hdr, &cnt, len);
  135. if (rammap && strap < cnt)
  136. return rammap + hdr + (strap * *len);
  137. return NULL;
  138. }
  139. u8 *
  140. nouveau_perf_timing(struct drm_device *dev, u32 freq, u8 *ver, u8 *len)
  141. {
  142. struct drm_nouveau_private *dev_priv = dev->dev_private;
  143. struct nvbios *bios = &dev_priv->vbios;
  144. struct bit_entry P;
  145. u8 *perf, *timing = NULL;
  146. u8 i = 0, hdr, cnt;
  147. if (bios->type == NVBIOS_BMP) {
  148. while ((perf = nouveau_perf_entry(dev, i++, ver, &hdr, &cnt,
  149. len)) && *ver == 0x15) {
  150. if (freq <= ROM32(perf[5]) * 20) {
  151. *ver = 0x00;
  152. *len = 14;
  153. return perf + 41;
  154. }
  155. }
  156. return NULL;
  157. }
  158. if (!bit_table(dev, 'P', &P)) {
  159. if (P.version == 1)
  160. timing = ROMPTR(dev, P.data[4]);
  161. else
  162. if (P.version == 2)
  163. timing = ROMPTR(dev, P.data[8]);
  164. }
  165. if (timing && timing[0] == 0x10) {
  166. u8 *ramcfg = nouveau_perf_ramcfg(dev, freq, ver, len);
  167. if (ramcfg && ramcfg[1] < timing[2]) {
  168. *ver = timing[0];
  169. *len = timing[3];
  170. return timing + timing[1] + (ramcfg[1] * timing[3]);
  171. }
  172. }
  173. return NULL;
  174. }
  175. static void
  176. legacy_perf_init(struct drm_device *dev)
  177. {
  178. struct drm_nouveau_private *dev_priv = dev->dev_private;
  179. struct nvbios *bios = &dev_priv->vbios;
  180. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  181. char *perf, *entry, *bmp = &bios->data[bios->offset];
  182. int headerlen, use_straps;
  183. if (bmp[5] < 0x5 || bmp[6] < 0x14) {
  184. NV_DEBUG(dev, "BMP version too old for perf\n");
  185. return;
  186. }
  187. perf = ROMPTR(dev, bmp[0x73]);
  188. if (!perf) {
  189. NV_DEBUG(dev, "No memclock table pointer found.\n");
  190. return;
  191. }
  192. switch (perf[0]) {
  193. case 0x12:
  194. case 0x14:
  195. case 0x18:
  196. use_straps = 0;
  197. headerlen = 1;
  198. break;
  199. case 0x01:
  200. use_straps = perf[1] & 1;
  201. headerlen = (use_straps ? 8 : 2);
  202. break;
  203. default:
  204. NV_WARN(dev, "Unknown memclock table version %x.\n", perf[0]);
  205. return;
  206. }
  207. entry = perf + headerlen;
  208. if (use_straps)
  209. entry += (nv_rd32(dev, NV_PEXTDEV_BOOT_0) & 0x3c) >> 1;
  210. sprintf(pm->perflvl[0].name, "performance_level_0");
  211. pm->perflvl[0].memory = ROM16(entry[0]) * 20;
  212. pm->nr_perflvl = 1;
  213. }
  214. static void
  215. nouveau_perf_voltage(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  216. {
  217. struct drm_nouveau_private *dev_priv = dev->dev_private;
  218. struct bit_entry P;
  219. u8 *vmap;
  220. int id;
  221. id = perflvl->volt_min;
  222. perflvl->volt_min = 0;
  223. /* boards using voltage table version <0x40 store the voltage
  224. * level directly in the perflvl entry as a multiple of 10mV
  225. */
  226. if (dev_priv->engine.pm.voltage.version < 0x40) {
  227. perflvl->volt_min = id * 10000;
  228. perflvl->volt_max = perflvl->volt_min;
  229. return;
  230. }
  231. /* on newer ones, the perflvl stores an index into yet another
  232. * vbios table containing a min/max voltage value for the perflvl
  233. */
  234. if (bit_table(dev, 'P', &P) || P.version != 2 || P.length < 34) {
  235. NV_DEBUG(dev, "where's our volt map table ptr? %d %d\n",
  236. P.version, P.length);
  237. return;
  238. }
  239. vmap = ROMPTR(dev, P.data[32]);
  240. if (!vmap) {
  241. NV_DEBUG(dev, "volt map table pointer invalid\n");
  242. return;
  243. }
  244. if (id < vmap[3]) {
  245. vmap += vmap[1] + (vmap[2] * id);
  246. perflvl->volt_min = ROM32(vmap[0]);
  247. perflvl->volt_max = ROM32(vmap[4]);
  248. }
  249. }
  250. void
  251. nouveau_perf_init(struct drm_device *dev)
  252. {
  253. struct drm_nouveau_private *dev_priv = dev->dev_private;
  254. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  255. struct nvbios *bios = &dev_priv->vbios;
  256. u8 *perf, ver, hdr, cnt, len;
  257. int ret, vid, i = -1;
  258. if (bios->type == NVBIOS_BMP && bios->data[bios->offset + 6] < 0x25) {
  259. legacy_perf_init(dev);
  260. return;
  261. }
  262. perf = nouveau_perf_table(dev, &ver);
  263. if (ver >= 0x20 && ver < 0x40)
  264. pm->fan.pwm_divisor = ROM16(perf[6]);
  265. while ((perf = nouveau_perf_entry(dev, ++i, &ver, &hdr, &cnt, &len))) {
  266. struct nouveau_pm_level *perflvl = &pm->perflvl[pm->nr_perflvl];
  267. if (perf[0] == 0xff)
  268. continue;
  269. switch (ver) {
  270. case 0x12:
  271. case 0x13:
  272. case 0x15:
  273. perflvl->fanspeed = perf[55];
  274. if (hdr > 56)
  275. perflvl->volt_min = perf[56];
  276. perflvl->core = ROM32(perf[1]) * 10;
  277. perflvl->memory = ROM32(perf[5]) * 20;
  278. break;
  279. case 0x21:
  280. case 0x23:
  281. case 0x24:
  282. perflvl->fanspeed = perf[4];
  283. perflvl->volt_min = perf[5];
  284. perflvl->shader = ROM16(perf[6]) * 1000;
  285. perflvl->core = perflvl->shader;
  286. perflvl->core += (signed char)perf[8] * 1000;
  287. if (dev_priv->chipset == 0x49 ||
  288. dev_priv->chipset == 0x4b)
  289. perflvl->memory = ROM16(perf[11]) * 1000;
  290. else
  291. perflvl->memory = ROM16(perf[11]) * 2000;
  292. break;
  293. case 0x25:
  294. perflvl->fanspeed = perf[4];
  295. perflvl->volt_min = perf[5];
  296. perflvl->core = ROM16(perf[6]) * 1000;
  297. perflvl->shader = ROM16(perf[10]) * 1000;
  298. perflvl->memory = ROM16(perf[12]) * 1000;
  299. break;
  300. case 0x30:
  301. perflvl->memscript = ROM16(perf[2]);
  302. case 0x35:
  303. perflvl->fanspeed = perf[6];
  304. perflvl->volt_min = perf[7];
  305. perflvl->core = ROM16(perf[8]) * 1000;
  306. perflvl->shader = ROM16(perf[10]) * 1000;
  307. perflvl->memory = ROM16(perf[12]) * 1000;
  308. perflvl->vdec = ROM16(perf[16]) * 1000;
  309. perflvl->dom6 = ROM16(perf[20]) * 1000;
  310. break;
  311. case 0x40:
  312. #define subent(n) ((ROM16(perf[hdr + (n) * len]) & 0xfff) * 1000)
  313. perflvl->fanspeed = 0; /*XXX*/
  314. perflvl->volt_min = perf[2];
  315. if (dev_priv->card_type == NV_50) {
  316. perflvl->core = subent(0);
  317. perflvl->shader = subent(1);
  318. perflvl->memory = subent(2);
  319. perflvl->vdec = subent(3);
  320. perflvl->unka0 = subent(4);
  321. } else {
  322. perflvl->hub06 = subent(0);
  323. perflvl->hub01 = subent(1);
  324. perflvl->copy = subent(2);
  325. perflvl->shader = subent(3);
  326. perflvl->rop = subent(4);
  327. perflvl->memory = subent(5);
  328. perflvl->vdec = subent(6);
  329. perflvl->daemon = subent(10);
  330. perflvl->hub07 = subent(11);
  331. perflvl->core = perflvl->shader / 2;
  332. }
  333. break;
  334. }
  335. /* make sure vid is valid */
  336. nouveau_perf_voltage(dev, perflvl);
  337. if (pm->voltage.supported && perflvl->volt_min) {
  338. vid = nouveau_volt_vid_lookup(dev, perflvl->volt_min);
  339. if (vid < 0) {
  340. NV_DEBUG(dev, "perflvl %d, bad vid\n", i);
  341. continue;
  342. }
  343. }
  344. /* get the corresponding memory timings */
  345. ret = nouveau_mem_timing_calc(dev, perflvl->memory,
  346. &perflvl->timing);
  347. if (ret) {
  348. NV_DEBUG(dev, "perflvl %d, bad timing: %d\n", i, ret);
  349. continue;
  350. }
  351. snprintf(perflvl->name, sizeof(perflvl->name),
  352. "performance_level_%d", i);
  353. perflvl->id = i;
  354. snprintf(perflvl->profile.name, sizeof(perflvl->profile.name),
  355. "%d", perflvl->id);
  356. perflvl->profile.func = &nouveau_pm_static_profile_func;
  357. list_add_tail(&perflvl->profile.head, &pm->profiles);
  358. pm->nr_perflvl++;
  359. }
  360. }
  361. void
  362. nouveau_perf_fini(struct drm_device *dev)
  363. {
  364. }