nvc0_pm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright 2011 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_bios.h"
  27. #include "nouveau_pm.h"
  28. static u32 read_div(struct drm_device *, int, u32, u32);
  29. static u32 read_pll(struct drm_device *, u32);
  30. static u32
  31. read_vco(struct drm_device *dev, u32 dsrc)
  32. {
  33. u32 ssrc = nv_rd32(dev, dsrc);
  34. if (!(ssrc & 0x00000100))
  35. return read_pll(dev, 0x00e800);
  36. return read_pll(dev, 0x00e820);
  37. }
  38. static u32
  39. read_pll(struct drm_device *dev, u32 pll)
  40. {
  41. u32 ctrl = nv_rd32(dev, pll + 0);
  42. u32 coef = nv_rd32(dev, pll + 4);
  43. u32 P = (coef & 0x003f0000) >> 16;
  44. u32 N = (coef & 0x0000ff00) >> 8;
  45. u32 M = (coef & 0x000000ff) >> 0;
  46. u32 sclk, doff;
  47. if (!(ctrl & 0x00000001))
  48. return 0;
  49. switch (pll & 0xfff000) {
  50. case 0x00e000:
  51. sclk = 27000;
  52. P = 1;
  53. break;
  54. case 0x137000:
  55. doff = (pll - 0x137000) / 0x20;
  56. sclk = read_div(dev, doff, 0x137120, 0x137140);
  57. break;
  58. case 0x132000:
  59. switch (pll) {
  60. case 0x132000:
  61. sclk = read_pll(dev, 0x132020);
  62. break;
  63. case 0x132020:
  64. sclk = read_div(dev, 0, 0x137320, 0x137330);
  65. break;
  66. default:
  67. return 0;
  68. }
  69. break;
  70. default:
  71. return 0;
  72. }
  73. return sclk * N / M / P;
  74. }
  75. static u32
  76. read_div(struct drm_device *dev, int doff, u32 dsrc, u32 dctl)
  77. {
  78. u32 ssrc = nv_rd32(dev, dsrc + (doff * 4));
  79. u32 sctl = nv_rd32(dev, dctl + (doff * 4));
  80. switch (ssrc & 0x00000003) {
  81. case 0:
  82. if ((ssrc & 0x00030000) != 0x00030000)
  83. return 27000;
  84. return 108000;
  85. case 2:
  86. return 100000;
  87. case 3:
  88. if (sctl & 0x80000000) {
  89. u32 sclk = read_vco(dev, dsrc + (doff * 4));
  90. u32 sdiv = (sctl & 0x0000003f) + 2;
  91. return (sclk * 2) / sdiv;
  92. }
  93. return read_vco(dev, dsrc + (doff * 4));
  94. default:
  95. return 0;
  96. }
  97. }
  98. static u32
  99. read_mem(struct drm_device *dev)
  100. {
  101. u32 ssel = nv_rd32(dev, 0x1373f0);
  102. if (ssel & 0x00000001)
  103. return read_div(dev, 0, 0x137300, 0x137310);
  104. return read_pll(dev, 0x132000);
  105. }
  106. static u32
  107. read_clk(struct drm_device *dev, int clk)
  108. {
  109. u32 sctl = nv_rd32(dev, 0x137250 + (clk * 4));
  110. u32 ssel = nv_rd32(dev, 0x137100);
  111. u32 sclk, sdiv;
  112. if (ssel & (1 << clk)) {
  113. if (clk < 7)
  114. sclk = read_pll(dev, 0x137000 + (clk * 0x20));
  115. else
  116. sclk = read_pll(dev, 0x1370e0);
  117. sdiv = ((sctl & 0x00003f00) >> 8) + 2;
  118. } else {
  119. sclk = read_div(dev, clk, 0x137160, 0x1371d0);
  120. sdiv = ((sctl & 0x0000003f) >> 0) + 2;
  121. }
  122. if (sctl & 0x80000000)
  123. return (sclk * 2) / sdiv;
  124. return sclk;
  125. }
  126. int
  127. nvc0_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  128. {
  129. perflvl->shader = read_clk(dev, 0x00);
  130. perflvl->core = perflvl->shader / 2;
  131. perflvl->memory = read_mem(dev);
  132. perflvl->rop = read_clk(dev, 0x01);
  133. perflvl->hub07 = read_clk(dev, 0x02);
  134. perflvl->hub06 = read_clk(dev, 0x07);
  135. perflvl->hub01 = read_clk(dev, 0x08);
  136. perflvl->copy = read_clk(dev, 0x09);
  137. perflvl->daemon = read_clk(dev, 0x0c);
  138. perflvl->vdec = read_clk(dev, 0x0e);
  139. return 0;
  140. }
  141. struct nvc0_pm_clock {
  142. u32 freq;
  143. u32 ssel;
  144. u32 mdiv;
  145. u32 dsrc;
  146. u32 ddiv;
  147. u32 coef;
  148. };
  149. struct nvc0_pm_state {
  150. struct nvc0_pm_clock eng[16];
  151. };
  152. static u32
  153. calc_div(struct drm_device *dev, int clk, u32 ref, u32 freq, u32 *ddiv)
  154. {
  155. u32 div = min((ref * 2) / freq, (u32)65);
  156. if (div < 2)
  157. div = 2;
  158. *ddiv = div - 2;
  159. return (ref * 2) / div;
  160. }
  161. static u32
  162. calc_src(struct drm_device *dev, int clk, u32 freq, u32 *dsrc, u32 *ddiv)
  163. {
  164. u32 sclk;
  165. /* use one of the fixed frequencies if possible */
  166. *ddiv = 0x00000000;
  167. switch (freq) {
  168. case 27000:
  169. case 108000:
  170. *dsrc = 0x00000000;
  171. if (freq == 108000)
  172. *dsrc |= 0x00030000;
  173. return freq;
  174. case 100000:
  175. *dsrc = 0x00000002;
  176. return freq;
  177. default:
  178. *dsrc = 0x00000003;
  179. break;
  180. }
  181. /* otherwise, calculate the closest divider */
  182. sclk = read_vco(dev, clk);
  183. if (clk < 7)
  184. sclk = calc_div(dev, clk, sclk, freq, ddiv);
  185. return sclk;
  186. }
  187. static u32
  188. calc_pll(struct drm_device *dev, int clk, u32 freq, u32 *coef)
  189. {
  190. struct pll_lims limits;
  191. int N, M, P, ret;
  192. ret = get_pll_limits(dev, 0x137000 + (clk * 0x20), &limits);
  193. if (ret)
  194. return 0;
  195. limits.refclk = read_div(dev, clk, 0x137120, 0x137140);
  196. if (!limits.refclk)
  197. return 0;
  198. ret = nva3_calc_pll(dev, &limits, freq, &N, NULL, &M, &P);
  199. if (ret <= 0)
  200. return 0;
  201. *coef = (P << 16) | (N << 8) | M;
  202. return ret;
  203. }
  204. /* A (likely rather simplified and incomplete) view of the clock tree
  205. *
  206. * Key:
  207. *
  208. * S: source select
  209. * D: divider
  210. * P: pll
  211. * F: switch
  212. *
  213. * Engine clocks:
  214. *
  215. * 137250(D) ---- 137100(F0) ---- 137160(S)/1371d0(D) ------------------- ref
  216. * (F1) ---- 1370X0(P) ---- 137120(S)/137140(D) ---- ref
  217. *
  218. * Not all registers exist for all clocks. For example: clocks >= 8 don't
  219. * have their own PLL (all tied to clock 7's PLL when in PLL mode), nor do
  220. * they have the divider at 1371d0, though the source selection at 137160
  221. * still exists. You must use the divider at 137250 for these instead.
  222. *
  223. * Memory clock:
  224. *
  225. * TBD, read_mem() above is likely very wrong...
  226. *
  227. */
  228. static int
  229. calc_clk(struct drm_device *dev, int clk, struct nvc0_pm_clock *info, u32 freq)
  230. {
  231. u32 src0, div0, div1D, div1P = 0;
  232. u32 clk0, clk1 = 0;
  233. /* invalid clock domain */
  234. if (!freq)
  235. return 0;
  236. /* first possible path, using only dividers */
  237. clk0 = calc_src(dev, clk, freq, &src0, &div0);
  238. clk0 = calc_div(dev, clk, clk0, freq, &div1D);
  239. /* see if we can get any closer using PLLs */
  240. if (clk0 != freq && (0x00004387 & (1 << clk))) {
  241. if (clk < 7)
  242. clk1 = calc_pll(dev, clk, freq, &info->coef);
  243. else
  244. clk1 = read_pll(dev, 0x1370e0);
  245. clk1 = calc_div(dev, clk, clk1, freq, &div1P);
  246. }
  247. /* select the method which gets closest to target freq */
  248. if (abs((int)freq - clk0) <= abs((int)freq - clk1)) {
  249. info->dsrc = src0;
  250. if (div0) {
  251. info->ddiv |= 0x80000000;
  252. info->ddiv |= div0 << 8;
  253. info->ddiv |= div0;
  254. }
  255. if (div1D) {
  256. info->mdiv |= 0x80000000;
  257. info->mdiv |= div1D;
  258. }
  259. info->ssel = 0;
  260. info->freq = clk0;
  261. } else {
  262. if (div1P) {
  263. info->mdiv |= 0x80000000;
  264. info->mdiv |= div1P << 8;
  265. }
  266. info->ssel = (1 << clk);
  267. info->freq = clk1;
  268. }
  269. return 0;
  270. }
  271. void *
  272. nvc0_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  273. {
  274. struct drm_nouveau_private *dev_priv = dev->dev_private;
  275. struct nvc0_pm_state *info;
  276. int ret;
  277. info = kzalloc(sizeof(*info), GFP_KERNEL);
  278. if (!info)
  279. return ERR_PTR(-ENOMEM);
  280. /* NFI why this is still in the performance table, the ROPCs appear
  281. * to get their clock from clock 2 ("hub07", actually hub05 on this
  282. * chip, but, anyway...) as well. nvatiming confirms hub05 and ROP
  283. * are always the same freq with the binary driver even when the
  284. * performance table says they should differ.
  285. */
  286. if (dev_priv->chipset == 0xd9)
  287. perflvl->rop = 0;
  288. if ((ret = calc_clk(dev, 0x00, &info->eng[0x00], perflvl->shader)) ||
  289. (ret = calc_clk(dev, 0x01, &info->eng[0x01], perflvl->rop)) ||
  290. (ret = calc_clk(dev, 0x02, &info->eng[0x02], perflvl->hub07)) ||
  291. (ret = calc_clk(dev, 0x07, &info->eng[0x07], perflvl->hub06)) ||
  292. (ret = calc_clk(dev, 0x08, &info->eng[0x08], perflvl->hub01)) ||
  293. (ret = calc_clk(dev, 0x09, &info->eng[0x09], perflvl->copy)) ||
  294. (ret = calc_clk(dev, 0x0c, &info->eng[0x0c], perflvl->daemon)) ||
  295. (ret = calc_clk(dev, 0x0e, &info->eng[0x0e], perflvl->vdec))) {
  296. kfree(info);
  297. return ERR_PTR(ret);
  298. }
  299. return info;
  300. }
  301. static void
  302. prog_clk(struct drm_device *dev, int clk, struct nvc0_pm_clock *info)
  303. {
  304. /* program dividers at 137160/1371d0 first */
  305. if (clk < 7 && !info->ssel) {
  306. nv_mask(dev, 0x1371d0 + (clk * 0x04), 0x80003f3f, info->ddiv);
  307. nv_wr32(dev, 0x137160 + (clk * 0x04), info->dsrc);
  308. }
  309. /* switch clock to non-pll mode */
  310. nv_mask(dev, 0x137100, (1 << clk), 0x00000000);
  311. nv_wait(dev, 0x137100, (1 << clk), 0x00000000);
  312. /* reprogram pll */
  313. if (clk < 7) {
  314. /* make sure it's disabled first... */
  315. u32 base = 0x137000 + (clk * 0x20);
  316. u32 ctrl = nv_rd32(dev, base + 0x00);
  317. if (ctrl & 0x00000001) {
  318. nv_mask(dev, base + 0x00, 0x00000004, 0x00000000);
  319. nv_mask(dev, base + 0x00, 0x00000001, 0x00000000);
  320. }
  321. /* program it to new values, if necessary */
  322. if (info->ssel) {
  323. nv_wr32(dev, base + 0x04, info->coef);
  324. nv_mask(dev, base + 0x00, 0x00000001, 0x00000001);
  325. nv_wait(dev, base + 0x00, 0x00020000, 0x00020000);
  326. nv_mask(dev, base + 0x00, 0x00020004, 0x00000004);
  327. }
  328. }
  329. /* select pll/non-pll mode, and program final clock divider */
  330. nv_mask(dev, 0x137100, (1 << clk), info->ssel);
  331. nv_wait(dev, 0x137100, (1 << clk), info->ssel);
  332. nv_mask(dev, 0x137250 + (clk * 0x04), 0x00003f3f, info->mdiv);
  333. }
  334. int
  335. nvc0_pm_clocks_set(struct drm_device *dev, void *data)
  336. {
  337. struct nvc0_pm_state *info = data;
  338. int i;
  339. for (i = 0; i < 16; i++) {
  340. if (!info->eng[i].freq)
  341. continue;
  342. prog_clk(dev, i, &info->eng[i]);
  343. }
  344. kfree(info);
  345. return 0;
  346. }