nva3_pm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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_bios.h"
  27. #include "nouveau_pm.h"
  28. static u32 read_clk(struct drm_device *, int, bool);
  29. static u32 read_pll(struct drm_device *, int, u32);
  30. static u32
  31. read_vco(struct drm_device *dev, int clk)
  32. {
  33. u32 sctl = nv_rd32(dev, 0x4120 + (clk * 4));
  34. if ((sctl & 0x00000030) != 0x00000030)
  35. return read_pll(dev, 0x41, 0x00e820);
  36. return read_pll(dev, 0x42, 0x00e8a0);
  37. }
  38. static u32
  39. read_clk(struct drm_device *dev, int clk, bool ignore_en)
  40. {
  41. struct drm_nouveau_private *dev_priv = dev->dev_private;
  42. u32 sctl, sdiv, sclk;
  43. /* refclk for the 0xe8xx plls is a fixed frequency */
  44. if (clk >= 0x40) {
  45. if (dev_priv->chipset == 0xaf) {
  46. /* no joke.. seriously.. sigh.. */
  47. return nv_rd32(dev, 0x00471c) * 1000;
  48. }
  49. return dev_priv->crystal;
  50. }
  51. sctl = nv_rd32(dev, 0x4120 + (clk * 4));
  52. if (!ignore_en && !(sctl & 0x00000100))
  53. return 0;
  54. switch (sctl & 0x00003000) {
  55. case 0x00000000:
  56. return dev_priv->crystal;
  57. case 0x00002000:
  58. if (sctl & 0x00000040)
  59. return 108000;
  60. return 100000;
  61. case 0x00003000:
  62. sclk = read_vco(dev, clk);
  63. sdiv = ((sctl & 0x003f0000) >> 16) + 2;
  64. return (sclk * 2) / sdiv;
  65. default:
  66. return 0;
  67. }
  68. }
  69. static u32
  70. read_pll(struct drm_device *dev, int clk, u32 pll)
  71. {
  72. u32 ctrl = nv_rd32(dev, pll + 0);
  73. u32 sclk = 0, P = 1, N = 1, M = 1;
  74. if (!(ctrl & 0x00000008)) {
  75. if (ctrl & 0x00000001) {
  76. u32 coef = nv_rd32(dev, pll + 4);
  77. M = (coef & 0x000000ff) >> 0;
  78. N = (coef & 0x0000ff00) >> 8;
  79. P = (coef & 0x003f0000) >> 16;
  80. /* no post-divider on these.. */
  81. if ((pll & 0x00ff00) == 0x00e800)
  82. P = 1;
  83. sclk = read_clk(dev, 0x00 + clk, false);
  84. }
  85. } else {
  86. sclk = read_clk(dev, 0x10 + clk, false);
  87. }
  88. if (M * P)
  89. return sclk * N / (M * P);
  90. return 0;
  91. }
  92. struct creg {
  93. u32 clk;
  94. u32 pll;
  95. };
  96. static int
  97. calc_clk(struct drm_device *dev, int clk, u32 pll, u32 khz, struct creg *reg)
  98. {
  99. struct pll_lims limits;
  100. u32 oclk, sclk, sdiv;
  101. int P, N, M, diff;
  102. int ret;
  103. reg->pll = 0;
  104. reg->clk = 0;
  105. if (!khz) {
  106. NV_DEBUG(dev, "no clock for 0x%04x/0x%02x\n", pll, clk);
  107. return 0;
  108. }
  109. switch (khz) {
  110. case 27000:
  111. reg->clk = 0x00000100;
  112. return khz;
  113. case 100000:
  114. reg->clk = 0x00002100;
  115. return khz;
  116. case 108000:
  117. reg->clk = 0x00002140;
  118. return khz;
  119. default:
  120. sclk = read_vco(dev, clk);
  121. sdiv = min((sclk * 2) / (khz - 2999), (u32)65);
  122. /* if the clock has a PLL attached, and we can get a within
  123. * [-2, 3) MHz of a divider, we'll disable the PLL and use
  124. * the divider instead.
  125. *
  126. * divider can go as low as 2, limited here because NVIDIA
  127. * and the VBIOS on my NVA8 seem to prefer using the PLL
  128. * for 810MHz - is there a good reason?
  129. */
  130. if (sdiv > 4) {
  131. oclk = (sclk * 2) / sdiv;
  132. diff = khz - oclk;
  133. if (!pll || (diff >= -2000 && diff < 3000)) {
  134. reg->clk = (((sdiv - 2) << 16) | 0x00003100);
  135. return oclk;
  136. }
  137. }
  138. if (!pll) {
  139. NV_ERROR(dev, "bad freq %02x: %d %d\n", clk, khz, sclk);
  140. return -ERANGE;
  141. }
  142. break;
  143. }
  144. ret = get_pll_limits(dev, pll, &limits);
  145. if (ret)
  146. return ret;
  147. limits.refclk = read_clk(dev, clk - 0x10, true);
  148. if (!limits.refclk)
  149. return -EINVAL;
  150. ret = nva3_calc_pll(dev, &limits, khz, &N, NULL, &M, &P);
  151. if (ret >= 0) {
  152. reg->clk = nv_rd32(dev, 0x4120 + (clk * 4));
  153. reg->pll = (P << 16) | (N << 8) | M;
  154. }
  155. return ret;
  156. }
  157. static void
  158. prog_pll(struct drm_device *dev, int clk, u32 pll, struct creg *reg)
  159. {
  160. const u32 src0 = 0x004120 + (clk * 4);
  161. const u32 src1 = 0x004160 + (clk * 4);
  162. const u32 ctrl = pll + 0;
  163. const u32 coef = pll + 4;
  164. if (!reg->clk && !reg->pll) {
  165. NV_DEBUG(dev, "no clock for %02x\n", clk);
  166. return;
  167. }
  168. if (reg->pll) {
  169. nv_mask(dev, src0, 0x00000101, 0x00000101);
  170. nv_wr32(dev, coef, reg->pll);
  171. nv_mask(dev, ctrl, 0x00000015, 0x00000015);
  172. nv_mask(dev, ctrl, 0x00000010, 0x00000000);
  173. nv_wait(dev, ctrl, 0x00020000, 0x00020000);
  174. nv_mask(dev, ctrl, 0x00000010, 0x00000010);
  175. nv_mask(dev, ctrl, 0x00000008, 0x00000000);
  176. nv_mask(dev, src1, 0x00000100, 0x00000000);
  177. nv_mask(dev, src1, 0x00000001, 0x00000000);
  178. } else {
  179. nv_mask(dev, src1, 0x003f3141, 0x00000101 | reg->clk);
  180. nv_mask(dev, ctrl, 0x00000018, 0x00000018);
  181. udelay(20);
  182. nv_mask(dev, ctrl, 0x00000001, 0x00000000);
  183. nv_mask(dev, src0, 0x00000100, 0x00000000);
  184. nv_mask(dev, src0, 0x00000001, 0x00000000);
  185. }
  186. }
  187. static void
  188. prog_clk(struct drm_device *dev, int clk, struct creg *reg)
  189. {
  190. if (!reg->clk) {
  191. NV_DEBUG(dev, "no clock for %02x\n", clk);
  192. return;
  193. }
  194. nv_mask(dev, 0x004120 + (clk * 4), 0x003f3141, 0x00000101 | reg->clk);
  195. }
  196. int
  197. nva3_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  198. {
  199. perflvl->core = read_pll(dev, 0x00, 0x4200);
  200. perflvl->shader = read_pll(dev, 0x01, 0x4220);
  201. perflvl->memory = read_pll(dev, 0x02, 0x4000);
  202. perflvl->unka0 = read_clk(dev, 0x20, false);
  203. perflvl->vdec = read_clk(dev, 0x21, false);
  204. perflvl->daemon = read_clk(dev, 0x25, false);
  205. perflvl->copy = perflvl->core;
  206. return 0;
  207. }
  208. struct nva3_pm_state {
  209. struct nouveau_pm_level *perflvl;
  210. struct creg nclk;
  211. struct creg sclk;
  212. struct creg mclk;
  213. struct creg vdec;
  214. struct creg unka0;
  215. };
  216. void *
  217. nva3_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  218. {
  219. struct nva3_pm_state *info;
  220. int ret;
  221. info = kzalloc(sizeof(*info), GFP_KERNEL);
  222. if (!info)
  223. return ERR_PTR(-ENOMEM);
  224. ret = calc_clk(dev, 0x10, 0x4200, perflvl->core, &info->nclk);
  225. if (ret < 0)
  226. goto out;
  227. ret = calc_clk(dev, 0x11, 0x4220, perflvl->shader, &info->sclk);
  228. if (ret < 0)
  229. goto out;
  230. ret = calc_clk(dev, 0x12, 0x4000, perflvl->memory, &info->mclk);
  231. if (ret < 0)
  232. goto out;
  233. ret = calc_clk(dev, 0x20, 0x0000, perflvl->unka0, &info->unka0);
  234. if (ret < 0)
  235. goto out;
  236. ret = calc_clk(dev, 0x21, 0x0000, perflvl->vdec, &info->vdec);
  237. if (ret < 0)
  238. goto out;
  239. info->perflvl = perflvl;
  240. out:
  241. if (ret < 0) {
  242. kfree(info);
  243. info = ERR_PTR(ret);
  244. }
  245. return info;
  246. }
  247. static bool
  248. nva3_pm_grcp_idle(void *data)
  249. {
  250. struct drm_device *dev = data;
  251. if (!(nv_rd32(dev, 0x400304) & 0x00000001))
  252. return true;
  253. if (nv_rd32(dev, 0x400308) == 0x0050001c)
  254. return true;
  255. return false;
  256. }
  257. static void
  258. mclk_precharge(struct nouveau_mem_exec_func *exec)
  259. {
  260. nv_wr32(exec->dev, 0x1002d4, 0x00000001);
  261. }
  262. static void
  263. mclk_refresh(struct nouveau_mem_exec_func *exec)
  264. {
  265. nv_wr32(exec->dev, 0x1002d0, 0x00000001);
  266. }
  267. static void
  268. mclk_refresh_auto(struct nouveau_mem_exec_func *exec, bool enable)
  269. {
  270. nv_wr32(exec->dev, 0x100210, enable ? 0x80000000 : 0x00000000);
  271. }
  272. static void
  273. mclk_refresh_self(struct nouveau_mem_exec_func *exec, bool enable)
  274. {
  275. nv_wr32(exec->dev, 0x1002dc, enable ? 0x00000001 : 0x00000000);
  276. }
  277. static void
  278. mclk_wait(struct nouveau_mem_exec_func *exec, u32 nsec)
  279. {
  280. udelay((nsec + 500) / 1000);
  281. }
  282. static u32
  283. mclk_mrg(struct nouveau_mem_exec_func *exec, int mr)
  284. {
  285. if (mr <= 1)
  286. return nv_rd32(exec->dev, 0x1002c0 + ((mr - 0) * 4));
  287. if (mr <= 3)
  288. return nv_rd32(exec->dev, 0x1002e0 + ((mr - 2) * 4));
  289. return 0;
  290. }
  291. static void
  292. mclk_mrs(struct nouveau_mem_exec_func *exec, int mr, u32 data)
  293. {
  294. struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
  295. if (mr <= 1) {
  296. if (dev_priv->vram_rank_B)
  297. nv_wr32(exec->dev, 0x1002c8 + ((mr - 0) * 4), data);
  298. nv_wr32(exec->dev, 0x1002c0 + ((mr - 0) * 4), data);
  299. } else
  300. if (mr <= 3) {
  301. if (dev_priv->vram_rank_B)
  302. nv_wr32(exec->dev, 0x1002e8 + ((mr - 2) * 4), data);
  303. nv_wr32(exec->dev, 0x1002e0 + ((mr - 2) * 4), data);
  304. }
  305. }
  306. static void
  307. mclk_clock_set(struct nouveau_mem_exec_func *exec)
  308. {
  309. struct nva3_pm_state *info = exec->priv;
  310. struct drm_device *dev = exec->dev;
  311. nv_wr32(dev, 0x004018, 0x00001000);
  312. prog_pll(dev, 0x02, 0x004000, &info->mclk);
  313. if (nv_rd32(dev, 0x4000) & 0x00000008)
  314. nv_wr32(dev, 0x004018, 0x1000d000);
  315. else
  316. nv_wr32(dev, 0x004018, 0x10005000);
  317. }
  318. static void
  319. mclk_timing_set(struct nouveau_mem_exec_func *exec)
  320. {
  321. struct nva3_pm_state *info = exec->priv;
  322. struct nouveau_pm_level *perflvl = info->perflvl;
  323. int i;
  324. for (i = 0; i < 9; i++)
  325. nv_wr32(exec->dev, 0x100220 + (i * 4), perflvl->timing.reg[i]);
  326. }
  327. static void
  328. prog_mem(struct drm_device *dev, struct nva3_pm_state *info)
  329. {
  330. struct nouveau_mem_exec_func exec = {
  331. .dev = dev,
  332. .precharge = mclk_precharge,
  333. .refresh = mclk_refresh,
  334. .refresh_auto = mclk_refresh_auto,
  335. .refresh_self = mclk_refresh_self,
  336. .wait = mclk_wait,
  337. .mrg = mclk_mrg,
  338. .mrs = mclk_mrs,
  339. .clock_set = mclk_clock_set,
  340. .timing_set = mclk_timing_set,
  341. .priv = info
  342. };
  343. nv_wr32(dev, 0x611200, 0x00003300);
  344. nouveau_mem_exec(&exec, info->perflvl);
  345. nv_wr32(dev, 0x611200, 0x00003330);
  346. }
  347. int
  348. nva3_pm_clocks_set(struct drm_device *dev, void *pre_state)
  349. {
  350. struct drm_nouveau_private *dev_priv = dev->dev_private;
  351. struct nva3_pm_state *info = pre_state;
  352. unsigned long flags;
  353. int ret = -EAGAIN;
  354. /* prevent any new grctx switches from starting */
  355. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  356. nv_wr32(dev, 0x400324, 0x00000000);
  357. nv_wr32(dev, 0x400328, 0x0050001c); /* wait flag 0x1c */
  358. /* wait for any pending grctx switches to complete */
  359. if (!nv_wait_cb(dev, nva3_pm_grcp_idle, dev)) {
  360. NV_ERROR(dev, "pm: ctxprog didn't go idle\n");
  361. goto cleanup;
  362. }
  363. /* freeze PFIFO */
  364. nv_mask(dev, 0x002504, 0x00000001, 0x00000001);
  365. if (!nv_wait(dev, 0x002504, 0x00000010, 0x00000010)) {
  366. NV_ERROR(dev, "pm: fifo didn't go idle\n");
  367. goto cleanup;
  368. }
  369. prog_pll(dev, 0x00, 0x004200, &info->nclk);
  370. prog_pll(dev, 0x01, 0x004220, &info->sclk);
  371. prog_clk(dev, 0x20, &info->unka0);
  372. prog_clk(dev, 0x21, &info->vdec);
  373. if (info->mclk.clk || info->mclk.pll)
  374. prog_mem(dev, info);
  375. ret = 0;
  376. cleanup:
  377. /* unfreeze PFIFO */
  378. nv_mask(dev, 0x002504, 0x00000001, 0x00000000);
  379. /* restore ctxprog to normal */
  380. nv_wr32(dev, 0x400324, 0x00000000);
  381. nv_wr32(dev, 0x400328, 0x0070009c); /* set flag 0x1c */
  382. /* unblock it if necessary */
  383. if (nv_rd32(dev, 0x400308) == 0x0050001c)
  384. nv_mask(dev, 0x400824, 0x10000000, 0x10000000);
  385. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  386. kfree(info);
  387. return ret;
  388. }