nva3_pm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. u32 sctl, sdiv, sclk;
  42. /* refclk for the 0xe8xx plls always 27KHz */
  43. if (clk >= 0x40)
  44. return 27000;
  45. sctl = nv_rd32(dev, 0x4120 + (clk * 4));
  46. if (!ignore_en && !(sctl & 0x00000100))
  47. return 0;
  48. switch (sctl & 0x00003000) {
  49. case 0x00000000:
  50. return 27000;
  51. case 0x00002000:
  52. if (sctl & 0x00000040)
  53. return 108000;
  54. return 100000;
  55. case 0x00003000:
  56. sclk = read_vco(dev, clk);
  57. sdiv = ((sctl & 0x003f0000) >> 16) + 2;
  58. return (sclk * 2) / sdiv;
  59. default:
  60. return 0;
  61. }
  62. }
  63. static u32
  64. read_pll(struct drm_device *dev, int clk, u32 pll)
  65. {
  66. u32 ctrl = nv_rd32(dev, pll + 0);
  67. u32 sclk = 0, P = 1, N = 1, M = 1;
  68. if (!(ctrl & 0x00000008)) {
  69. if (ctrl & 0x00000001) {
  70. u32 coef = nv_rd32(dev, pll + 4);
  71. M = (coef & 0x000000ff) >> 0;
  72. N = (coef & 0x0000ff00) >> 8;
  73. P = (coef & 0x003f0000) >> 16;
  74. /* no post-divider on these.. */
  75. if ((pll & 0x00ff00) == 0x00e800)
  76. P = 1;
  77. sclk = read_clk(dev, 0x00 + clk, false);
  78. }
  79. } else {
  80. sclk = read_clk(dev, 0x10 + clk, false);
  81. }
  82. return sclk * N / (M * P);
  83. }
  84. struct creg {
  85. u32 clk;
  86. u32 pll;
  87. };
  88. static int
  89. calc_clk(struct drm_device *dev, int clk, u32 pll, u32 khz, struct creg *reg)
  90. {
  91. struct pll_lims limits;
  92. u32 oclk, sclk, sdiv;
  93. int P, N, M, diff;
  94. int ret;
  95. reg->pll = 0;
  96. reg->clk = 0;
  97. if (!khz) {
  98. NV_DEBUG(dev, "no clock for 0x%04x/0x%02x\n", pll, clk);
  99. return 0;
  100. }
  101. switch (khz) {
  102. case 27000:
  103. reg->clk = 0x00000100;
  104. return khz;
  105. case 100000:
  106. reg->clk = 0x00002100;
  107. return khz;
  108. case 108000:
  109. reg->clk = 0x00002140;
  110. return khz;
  111. default:
  112. sclk = read_vco(dev, clk);
  113. sdiv = min((sclk * 2) / (khz - 2999), (u32)65);
  114. /* if the clock has a PLL attached, and we can get a within
  115. * [-2, 3) MHz of a divider, we'll disable the PLL and use
  116. * the divider instead.
  117. *
  118. * divider can go as low as 2, limited here because NVIDIA
  119. * and the VBIOS on my NVA8 seem to prefer using the PLL
  120. * for 810MHz - is there a good reason?
  121. */
  122. if (sdiv > 4) {
  123. oclk = (sclk * 2) / sdiv;
  124. diff = khz - oclk;
  125. if (!pll || (diff >= -2000 && diff < 3000)) {
  126. reg->clk = (((sdiv - 2) << 16) | 0x00003100);
  127. return oclk;
  128. }
  129. }
  130. if (!pll) {
  131. NV_ERROR(dev, "bad freq %02x: %d %d\n", clk, khz, sclk);
  132. return -ERANGE;
  133. }
  134. break;
  135. }
  136. ret = get_pll_limits(dev, pll, &limits);
  137. if (ret)
  138. return ret;
  139. limits.refclk = read_clk(dev, clk - 0x10, true);
  140. if (!limits.refclk)
  141. return -EINVAL;
  142. ret = nva3_calc_pll(dev, &limits, khz, &N, NULL, &M, &P);
  143. if (ret >= 0) {
  144. reg->clk = nv_rd32(dev, 0x4120 + (clk * 4));
  145. reg->pll = (P << 16) | (N << 8) | M;
  146. }
  147. return ret;
  148. }
  149. static void
  150. prog_pll(struct drm_device *dev, int clk, u32 pll, struct creg *reg)
  151. {
  152. const u32 src0 = 0x004120 + (clk * 4);
  153. const u32 src1 = 0x004160 + (clk * 4);
  154. const u32 ctrl = pll + 0;
  155. const u32 coef = pll + 4;
  156. u32 cntl;
  157. if (!reg->clk && !reg->pll) {
  158. NV_DEBUG(dev, "no clock for %02x\n", clk);
  159. return;
  160. }
  161. cntl = nv_rd32(dev, ctrl) & 0xfffffff2;
  162. if (reg->pll) {
  163. nv_mask(dev, src0, 0x00000101, 0x00000101);
  164. nv_wr32(dev, coef, reg->pll);
  165. nv_wr32(dev, ctrl, cntl | 0x00000015);
  166. nv_mask(dev, src1, 0x00000100, 0x00000000);
  167. nv_mask(dev, src1, 0x00000001, 0x00000000);
  168. } else {
  169. nv_mask(dev, src1, 0x003f3141, 0x00000101 | reg->clk);
  170. nv_wr32(dev, ctrl, cntl | 0x0000001d);
  171. nv_mask(dev, ctrl, 0x00000001, 0x00000000);
  172. nv_mask(dev, src0, 0x00000100, 0x00000000);
  173. nv_mask(dev, src0, 0x00000001, 0x00000000);
  174. }
  175. }
  176. static void
  177. prog_clk(struct drm_device *dev, int clk, struct creg *reg)
  178. {
  179. if (!reg->clk) {
  180. NV_DEBUG(dev, "no clock for %02x\n", clk);
  181. return;
  182. }
  183. nv_mask(dev, 0x004120 + (clk * 4), 0x003f3141, 0x00000101 | reg->clk);
  184. }
  185. int
  186. nva3_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  187. {
  188. perflvl->core = read_pll(dev, 0x00, 0x4200);
  189. perflvl->shader = read_pll(dev, 0x01, 0x4220);
  190. perflvl->memory = read_pll(dev, 0x02, 0x4000);
  191. perflvl->unka0 = read_clk(dev, 0x20, false);
  192. perflvl->vdec = read_clk(dev, 0x21, false);
  193. perflvl->daemon = read_clk(dev, 0x25, false);
  194. perflvl->copy = perflvl->core;
  195. return 0;
  196. }
  197. struct nva3_pm_state {
  198. struct creg nclk;
  199. struct creg sclk;
  200. struct creg mclk;
  201. struct creg vdec;
  202. struct creg unka0;
  203. };
  204. void *
  205. nva3_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  206. {
  207. struct nva3_pm_state *info;
  208. int ret;
  209. info = kzalloc(sizeof(*info), GFP_KERNEL);
  210. if (!info)
  211. return ERR_PTR(-ENOMEM);
  212. ret = calc_clk(dev, 0x10, 0x4200, perflvl->core, &info->nclk);
  213. if (ret < 0)
  214. goto out;
  215. ret = calc_clk(dev, 0x11, 0x4220, perflvl->shader, &info->sclk);
  216. if (ret < 0)
  217. goto out;
  218. ret = calc_clk(dev, 0x12, 0x4000, perflvl->memory, &info->mclk);
  219. if (ret < 0)
  220. goto out;
  221. ret = calc_clk(dev, 0x20, 0x0000, perflvl->unka0, &info->unka0);
  222. if (ret < 0)
  223. goto out;
  224. ret = calc_clk(dev, 0x21, 0x0000, perflvl->vdec, &info->vdec);
  225. if (ret < 0)
  226. goto out;
  227. out:
  228. if (ret < 0) {
  229. kfree(info);
  230. info = ERR_PTR(ret);
  231. }
  232. return info;
  233. }
  234. static bool
  235. nva3_pm_grcp_idle(void *data)
  236. {
  237. struct drm_device *dev = data;
  238. if (!(nv_rd32(dev, 0x400304) & 0x00000001))
  239. return true;
  240. if (nv_rd32(dev, 0x400308) == 0x0050001c)
  241. return true;
  242. return false;
  243. }
  244. void
  245. nva3_pm_clocks_set(struct drm_device *dev, void *pre_state)
  246. {
  247. struct drm_nouveau_private *dev_priv = dev->dev_private;
  248. struct nva3_pm_state *info = pre_state;
  249. unsigned long flags;
  250. /* prevent any new grctx switches from starting */
  251. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  252. nv_wr32(dev, 0x400324, 0x00000000);
  253. nv_wr32(dev, 0x400328, 0x0050001c); /* wait flag 0x1c */
  254. /* wait for any pending grctx switches to complete */
  255. if (!nv_wait_cb(dev, nva3_pm_grcp_idle, dev)) {
  256. NV_ERROR(dev, "pm: ctxprog didn't go idle\n");
  257. goto cleanup;
  258. }
  259. /* freeze PFIFO */
  260. nv_mask(dev, 0x002504, 0x00000001, 0x00000001);
  261. if (!nv_wait(dev, 0x002504, 0x00000010, 0x00000010)) {
  262. NV_ERROR(dev, "pm: fifo didn't go idle\n");
  263. goto cleanup;
  264. }
  265. prog_pll(dev, 0x00, 0x004200, &info->nclk);
  266. prog_pll(dev, 0x01, 0x004220, &info->sclk);
  267. prog_clk(dev, 0x20, &info->unka0);
  268. prog_clk(dev, 0x21, &info->vdec);
  269. if (info->mclk.clk || info->mclk.pll) {
  270. nv_wr32(dev, 0x100210, 0);
  271. nv_wr32(dev, 0x1002dc, 1);
  272. nv_wr32(dev, 0x004018, 0x00001000);
  273. prog_pll(dev, 0x02, 0x004000, &info->mclk);
  274. if (nv_rd32(dev, 0x4000) & 0x00000008)
  275. nv_wr32(dev, 0x004018, 0x1000d000);
  276. else
  277. nv_wr32(dev, 0x004018, 0x10005000);
  278. nv_wr32(dev, 0x1002dc, 0);
  279. nv_wr32(dev, 0x100210, 0x80000000);
  280. }
  281. cleanup:
  282. /* unfreeze PFIFO */
  283. nv_mask(dev, 0x002504, 0x00000001, 0x00000000);
  284. /* restore ctxprog to normal */
  285. nv_wr32(dev, 0x400324, 0x00000000);
  286. nv_wr32(dev, 0x400328, 0x0070009c); /* set flag 0x1c */
  287. /* unblock it if necessary */
  288. if (nv_rd32(dev, 0x400308) == 0x0050001c)
  289. nv_mask(dev, 0x400824, 0x10000000, 0x10000000);
  290. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  291. kfree(info);
  292. }