nva3_pm.c 8.5 KB

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