nvc0_pm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 nouveau_pm_level *perflvl;
  151. struct nvc0_pm_clock eng[16];
  152. struct nvc0_pm_clock mem;
  153. };
  154. static u32
  155. calc_div(struct drm_device *dev, int clk, u32 ref, u32 freq, u32 *ddiv)
  156. {
  157. u32 div = min((ref * 2) / freq, (u32)65);
  158. if (div < 2)
  159. div = 2;
  160. *ddiv = div - 2;
  161. return (ref * 2) / div;
  162. }
  163. static u32
  164. calc_src(struct drm_device *dev, int clk, u32 freq, u32 *dsrc, u32 *ddiv)
  165. {
  166. u32 sclk;
  167. /* use one of the fixed frequencies if possible */
  168. *ddiv = 0x00000000;
  169. switch (freq) {
  170. case 27000:
  171. case 108000:
  172. *dsrc = 0x00000000;
  173. if (freq == 108000)
  174. *dsrc |= 0x00030000;
  175. return freq;
  176. case 100000:
  177. *dsrc = 0x00000002;
  178. return freq;
  179. default:
  180. *dsrc = 0x00000003;
  181. break;
  182. }
  183. /* otherwise, calculate the closest divider */
  184. sclk = read_vco(dev, clk);
  185. if (clk < 7)
  186. sclk = calc_div(dev, clk, sclk, freq, ddiv);
  187. return sclk;
  188. }
  189. static u32
  190. calc_pll(struct drm_device *dev, int clk, u32 freq, u32 *coef)
  191. {
  192. struct pll_lims limits;
  193. int N, M, P, ret;
  194. ret = get_pll_limits(dev, 0x137000 + (clk * 0x20), &limits);
  195. if (ret)
  196. return 0;
  197. limits.refclk = read_div(dev, clk, 0x137120, 0x137140);
  198. if (!limits.refclk)
  199. return 0;
  200. ret = nva3_calc_pll(dev, &limits, freq, &N, NULL, &M, &P);
  201. if (ret <= 0)
  202. return 0;
  203. *coef = (P << 16) | (N << 8) | M;
  204. return ret;
  205. }
  206. /* A (likely rather simplified and incomplete) view of the clock tree
  207. *
  208. * Key:
  209. *
  210. * S: source select
  211. * D: divider
  212. * P: pll
  213. * F: switch
  214. *
  215. * Engine clocks:
  216. *
  217. * 137250(D) ---- 137100(F0) ---- 137160(S)/1371d0(D) ------------------- ref
  218. * (F1) ---- 1370X0(P) ---- 137120(S)/137140(D) ---- ref
  219. *
  220. * Not all registers exist for all clocks. For example: clocks >= 8 don't
  221. * have their own PLL (all tied to clock 7's PLL when in PLL mode), nor do
  222. * they have the divider at 1371d0, though the source selection at 137160
  223. * still exists. You must use the divider at 137250 for these instead.
  224. *
  225. * Memory clock:
  226. *
  227. * TBD, read_mem() above is likely very wrong...
  228. *
  229. */
  230. static int
  231. calc_clk(struct drm_device *dev, int clk, struct nvc0_pm_clock *info, u32 freq)
  232. {
  233. u32 src0, div0, div1D, div1P = 0;
  234. u32 clk0, clk1 = 0;
  235. /* invalid clock domain */
  236. if (!freq)
  237. return 0;
  238. /* first possible path, using only dividers */
  239. clk0 = calc_src(dev, clk, freq, &src0, &div0);
  240. clk0 = calc_div(dev, clk, clk0, freq, &div1D);
  241. /* see if we can get any closer using PLLs */
  242. if (clk0 != freq && (0x00004387 & (1 << clk))) {
  243. if (clk < 7)
  244. clk1 = calc_pll(dev, clk, freq, &info->coef);
  245. else
  246. clk1 = read_pll(dev, 0x1370e0);
  247. clk1 = calc_div(dev, clk, clk1, freq, &div1P);
  248. }
  249. /* select the method which gets closest to target freq */
  250. if (abs((int)freq - clk0) <= abs((int)freq - clk1)) {
  251. info->dsrc = src0;
  252. if (div0) {
  253. info->ddiv |= 0x80000000;
  254. info->ddiv |= div0 << 8;
  255. info->ddiv |= div0;
  256. }
  257. if (div1D) {
  258. info->mdiv |= 0x80000000;
  259. info->mdiv |= div1D;
  260. }
  261. info->ssel = 0;
  262. info->freq = clk0;
  263. } else {
  264. if (div1P) {
  265. info->mdiv |= 0x80000000;
  266. info->mdiv |= div1P << 8;
  267. }
  268. info->ssel = (1 << clk);
  269. info->freq = clk1;
  270. }
  271. return 0;
  272. }
  273. static int
  274. calc_mem(struct drm_device *dev, struct nvc0_pm_clock *info, u32 freq)
  275. {
  276. struct pll_lims pll;
  277. int N, M, P, ret;
  278. u32 ctrl;
  279. /* mclk pll input freq comes from another pll, make sure it's on */
  280. ctrl = nv_rd32(dev, 0x132020);
  281. if (!(ctrl & 0x00000001)) {
  282. /* if not, program it to 567MHz. nfi where this value comes
  283. * from - it looks like it's in the pll limits table for
  284. * 132000 but the binary driver ignores all my attempts to
  285. * change this value.
  286. */
  287. nv_wr32(dev, 0x137320, 0x00000103);
  288. nv_wr32(dev, 0x137330, 0x81200606);
  289. nv_wait(dev, 0x132020, 0x00010000, 0x00010000);
  290. nv_wr32(dev, 0x132024, 0x0001150f);
  291. nv_mask(dev, 0x132020, 0x00000001, 0x00000001);
  292. nv_wait(dev, 0x137390, 0x00020000, 0x00020000);
  293. nv_mask(dev, 0x132020, 0x00000004, 0x00000004);
  294. }
  295. /* for the moment, until the clock tree is better understood, use
  296. * pll mode for all clock frequencies
  297. */
  298. ret = get_pll_limits(dev, 0x132000, &pll);
  299. if (ret == 0) {
  300. pll.refclk = read_pll(dev, 0x132020);
  301. if (pll.refclk) {
  302. ret = nva3_calc_pll(dev, &pll, freq, &N, NULL, &M, &P);
  303. if (ret > 0) {
  304. info->coef = (P << 16) | (N << 8) | M;
  305. return 0;
  306. }
  307. }
  308. }
  309. return -EINVAL;
  310. }
  311. void *
  312. nvc0_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  313. {
  314. struct drm_nouveau_private *dev_priv = dev->dev_private;
  315. struct nvc0_pm_state *info;
  316. int ret;
  317. info = kzalloc(sizeof(*info), GFP_KERNEL);
  318. if (!info)
  319. return ERR_PTR(-ENOMEM);
  320. /* NFI why this is still in the performance table, the ROPCs appear
  321. * to get their clock from clock 2 ("hub07", actually hub05 on this
  322. * chip, but, anyway...) as well. nvatiming confirms hub05 and ROP
  323. * are always the same freq with the binary driver even when the
  324. * performance table says they should differ.
  325. */
  326. if (dev_priv->chipset == 0xd9)
  327. perflvl->rop = 0;
  328. if ((ret = calc_clk(dev, 0x00, &info->eng[0x00], perflvl->shader)) ||
  329. (ret = calc_clk(dev, 0x01, &info->eng[0x01], perflvl->rop)) ||
  330. (ret = calc_clk(dev, 0x02, &info->eng[0x02], perflvl->hub07)) ||
  331. (ret = calc_clk(dev, 0x07, &info->eng[0x07], perflvl->hub06)) ||
  332. (ret = calc_clk(dev, 0x08, &info->eng[0x08], perflvl->hub01)) ||
  333. (ret = calc_clk(dev, 0x09, &info->eng[0x09], perflvl->copy)) ||
  334. (ret = calc_clk(dev, 0x0c, &info->eng[0x0c], perflvl->daemon)) ||
  335. (ret = calc_clk(dev, 0x0e, &info->eng[0x0e], perflvl->vdec))) {
  336. kfree(info);
  337. return ERR_PTR(ret);
  338. }
  339. if (perflvl->memory) {
  340. ret = calc_mem(dev, &info->mem, perflvl->memory);
  341. if (ret) {
  342. kfree(info);
  343. return ERR_PTR(ret);
  344. }
  345. }
  346. info->perflvl = perflvl;
  347. return info;
  348. }
  349. static void
  350. prog_clk(struct drm_device *dev, int clk, struct nvc0_pm_clock *info)
  351. {
  352. /* program dividers at 137160/1371d0 first */
  353. if (clk < 7 && !info->ssel) {
  354. nv_mask(dev, 0x1371d0 + (clk * 0x04), 0x80003f3f, info->ddiv);
  355. nv_wr32(dev, 0x137160 + (clk * 0x04), info->dsrc);
  356. }
  357. /* switch clock to non-pll mode */
  358. nv_mask(dev, 0x137100, (1 << clk), 0x00000000);
  359. nv_wait(dev, 0x137100, (1 << clk), 0x00000000);
  360. /* reprogram pll */
  361. if (clk < 7) {
  362. /* make sure it's disabled first... */
  363. u32 base = 0x137000 + (clk * 0x20);
  364. u32 ctrl = nv_rd32(dev, base + 0x00);
  365. if (ctrl & 0x00000001) {
  366. nv_mask(dev, base + 0x00, 0x00000004, 0x00000000);
  367. nv_mask(dev, base + 0x00, 0x00000001, 0x00000000);
  368. }
  369. /* program it to new values, if necessary */
  370. if (info->ssel) {
  371. nv_wr32(dev, base + 0x04, info->coef);
  372. nv_mask(dev, base + 0x00, 0x00000001, 0x00000001);
  373. nv_wait(dev, base + 0x00, 0x00020000, 0x00020000);
  374. nv_mask(dev, base + 0x00, 0x00020004, 0x00000004);
  375. }
  376. }
  377. /* select pll/non-pll mode, and program final clock divider */
  378. nv_mask(dev, 0x137100, (1 << clk), info->ssel);
  379. nv_wait(dev, 0x137100, (1 << clk), info->ssel);
  380. nv_mask(dev, 0x137250 + (clk * 0x04), 0x00003f3f, info->mdiv);
  381. }
  382. static void
  383. mclk_precharge(struct nouveau_mem_exec_func *exec)
  384. {
  385. }
  386. static void
  387. mclk_refresh(struct nouveau_mem_exec_func *exec)
  388. {
  389. }
  390. static void
  391. mclk_refresh_auto(struct nouveau_mem_exec_func *exec, bool enable)
  392. {
  393. nv_wr32(exec->dev, 0x10f210, enable ? 0x80000000 : 0x00000000);
  394. }
  395. static void
  396. mclk_refresh_self(struct nouveau_mem_exec_func *exec, bool enable)
  397. {
  398. }
  399. static void
  400. mclk_wait(struct nouveau_mem_exec_func *exec, u32 nsec)
  401. {
  402. udelay((nsec + 500) / 1000);
  403. }
  404. static u32
  405. mclk_mrg(struct nouveau_mem_exec_func *exec, int mr)
  406. {
  407. struct drm_device *dev = exec->dev;
  408. struct drm_nouveau_private *dev_priv = dev->dev_private;
  409. if (dev_priv->vram_type != NV_MEM_TYPE_GDDR5) {
  410. if (mr <= 1)
  411. return nv_rd32(dev, 0x10f300 + ((mr - 0) * 4));
  412. return nv_rd32(dev, 0x10f320 + ((mr - 2) * 4));
  413. } else {
  414. if (mr == 0)
  415. return nv_rd32(dev, 0x10f300 + (mr * 4));
  416. else
  417. if (mr <= 7)
  418. return nv_rd32(dev, 0x10f32c + (mr * 4));
  419. return nv_rd32(dev, 0x10f34c);
  420. }
  421. }
  422. static void
  423. mclk_mrs(struct nouveau_mem_exec_func *exec, int mr, u32 data)
  424. {
  425. struct drm_device *dev = exec->dev;
  426. struct drm_nouveau_private *dev_priv = dev->dev_private;
  427. if (dev_priv->vram_type != NV_MEM_TYPE_GDDR5) {
  428. if (mr <= 1) {
  429. nv_wr32(dev, 0x10f300 + ((mr - 0) * 4), data);
  430. if (dev_priv->vram_rank_B)
  431. nv_wr32(dev, 0x10f308 + ((mr - 0) * 4), data);
  432. } else
  433. if (mr <= 3) {
  434. nv_wr32(dev, 0x10f320 + ((mr - 2) * 4), data);
  435. if (dev_priv->vram_rank_B)
  436. nv_wr32(dev, 0x10f328 + ((mr - 2) * 4), data);
  437. }
  438. } else {
  439. if (mr == 0) nv_wr32(dev, 0x10f300 + (mr * 4), data);
  440. else if (mr <= 7) nv_wr32(dev, 0x10f32c + (mr * 4), data);
  441. else if (mr == 15) nv_wr32(dev, 0x10f34c, data);
  442. }
  443. }
  444. static void
  445. mclk_clock_set(struct nouveau_mem_exec_func *exec)
  446. {
  447. struct nvc0_pm_state *info = exec->priv;
  448. struct drm_device *dev = exec->dev;
  449. u32 ctrl = nv_rd32(dev, 0x132000);
  450. nv_wr32(dev, 0x137360, 0x00000001);
  451. nv_wr32(dev, 0x137370, 0x00000000);
  452. nv_wr32(dev, 0x137380, 0x00000000);
  453. if (ctrl & 0x00000001)
  454. nv_wr32(dev, 0x132000, (ctrl &= ~0x00000001));
  455. nv_wr32(dev, 0x132004, info->mem.coef);
  456. nv_wr32(dev, 0x132000, (ctrl |= 0x00000001));
  457. nv_wait(dev, 0x137390, 0x00000002, 0x00000002);
  458. nv_wr32(dev, 0x132018, 0x00005000);
  459. nv_wr32(dev, 0x137370, 0x00000001);
  460. nv_wr32(dev, 0x137380, 0x00000001);
  461. nv_wr32(dev, 0x137360, 0x00000000);
  462. }
  463. static void
  464. mclk_timing_set(struct nouveau_mem_exec_func *exec)
  465. {
  466. struct nvc0_pm_state *info = exec->priv;
  467. struct nouveau_pm_level *perflvl = info->perflvl;
  468. int i;
  469. for (i = 0; i < 5; i++)
  470. nv_wr32(exec->dev, 0x10f290 + (i * 4), perflvl->timing.reg[i]);
  471. }
  472. static void
  473. prog_mem(struct drm_device *dev, struct nvc0_pm_state *info)
  474. {
  475. struct drm_nouveau_private *dev_priv = dev->dev_private;
  476. struct nouveau_mem_exec_func exec = {
  477. .dev = dev,
  478. .precharge = mclk_precharge,
  479. .refresh = mclk_refresh,
  480. .refresh_auto = mclk_refresh_auto,
  481. .refresh_self = mclk_refresh_self,
  482. .wait = mclk_wait,
  483. .mrg = mclk_mrg,
  484. .mrs = mclk_mrs,
  485. .clock_set = mclk_clock_set,
  486. .timing_set = mclk_timing_set,
  487. .priv = info
  488. };
  489. if (dev_priv->chipset < 0xd0)
  490. nv_wr32(dev, 0x611200, 0x00003300);
  491. else
  492. nv_wr32(dev, 0x62c000, 0x03030000);
  493. nouveau_mem_exec(&exec, info->perflvl);
  494. if (dev_priv->chipset < 0xd0)
  495. nv_wr32(dev, 0x611200, 0x00003300);
  496. else
  497. nv_wr32(dev, 0x62c000, 0x03030300);
  498. }
  499. int
  500. nvc0_pm_clocks_set(struct drm_device *dev, void *data)
  501. {
  502. struct nvc0_pm_state *info = data;
  503. int i;
  504. if (info->mem.coef)
  505. prog_mem(dev, info);
  506. for (i = 0; i < 16; i++) {
  507. if (!info->eng[i].freq)
  508. continue;
  509. prog_clk(dev, i, &info->eng[i]);
  510. }
  511. kfree(info);
  512. return 0;
  513. }