nv40_pm.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #include "nouveau_hw.h"
  29. #define min2(a,b) ((a) < (b) ? (a) : (b))
  30. static u32
  31. read_pll_1(struct drm_device *dev, u32 reg)
  32. {
  33. u32 ctrl = nv_rd32(dev, reg + 0x00);
  34. int P = (ctrl & 0x00070000) >> 16;
  35. int N = (ctrl & 0x0000ff00) >> 8;
  36. int M = (ctrl & 0x000000ff) >> 0;
  37. u32 ref = 27000, clk = 0;
  38. if (ctrl & 0x80000000)
  39. clk = ref * N / M;
  40. return clk >> P;
  41. }
  42. static u32
  43. read_pll_2(struct drm_device *dev, u32 reg)
  44. {
  45. u32 ctrl = nv_rd32(dev, reg + 0x00);
  46. u32 coef = nv_rd32(dev, reg + 0x04);
  47. int N2 = (coef & 0xff000000) >> 24;
  48. int M2 = (coef & 0x00ff0000) >> 16;
  49. int N1 = (coef & 0x0000ff00) >> 8;
  50. int M1 = (coef & 0x000000ff) >> 0;
  51. int P = (ctrl & 0x00070000) >> 16;
  52. u32 ref = 27000, clk = 0;
  53. if (ctrl & 0x80000000)
  54. clk = ref * N1 / M1;
  55. if (!(ctrl & 0x00000100)) {
  56. if (ctrl & 0x40000000)
  57. clk = clk * N2 / M2;
  58. }
  59. return clk >> P;
  60. }
  61. static u32
  62. read_clk(struct drm_device *dev, u32 src)
  63. {
  64. switch (src) {
  65. case 3:
  66. return read_pll_2(dev, 0x004000);
  67. case 2:
  68. return read_pll_1(dev, 0x004008);
  69. default:
  70. break;
  71. }
  72. return 0;
  73. }
  74. int
  75. nv40_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  76. {
  77. u32 ctrl = nv_rd32(dev, 0x00c040);
  78. perflvl->core = read_clk(dev, (ctrl & 0x00000003) >> 0);
  79. perflvl->shader = read_clk(dev, (ctrl & 0x00000030) >> 4);
  80. perflvl->memory = read_pll_2(dev, 0x4020);
  81. return 0;
  82. }
  83. struct nv40_pm_state {
  84. u32 ctrl;
  85. u32 npll_ctrl;
  86. u32 npll_coef;
  87. u32 spll;
  88. u32 mpll_ctrl;
  89. u32 mpll_coef;
  90. };
  91. static int
  92. nv40_calc_pll(struct drm_device *dev, u32 reg, struct pll_lims *pll,
  93. u32 clk, int *N1, int *M1, int *N2, int *M2, int *log2P)
  94. {
  95. struct nouveau_pll_vals coef;
  96. int ret;
  97. ret = get_pll_limits(dev, reg, pll);
  98. if (ret)
  99. return ret;
  100. if (clk < pll->vco1.maxfreq)
  101. pll->vco2.maxfreq = 0;
  102. ret = nouveau_calc_pll_mnp(dev, pll, clk, &coef);
  103. if (ret == 0)
  104. return -ERANGE;
  105. *N1 = coef.N1;
  106. *M1 = coef.M1;
  107. if (N2 && M2) {
  108. if (pll->vco2.maxfreq) {
  109. *N2 = coef.N2;
  110. *M2 = coef.M2;
  111. } else {
  112. *N2 = 1;
  113. *M2 = 1;
  114. }
  115. }
  116. *log2P = coef.log2P;
  117. return 0;
  118. }
  119. void *
  120. nv40_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  121. {
  122. struct nv40_pm_state *info;
  123. struct pll_lims pll;
  124. int N1, N2, M1, M2, log2P;
  125. int ret;
  126. info = kmalloc(sizeof(*info), GFP_KERNEL);
  127. if (!info)
  128. return ERR_PTR(-ENOMEM);
  129. /* core/geometric clock */
  130. ret = nv40_calc_pll(dev, 0x004000, &pll, perflvl->core,
  131. &N1, &M1, &N2, &M2, &log2P);
  132. if (ret < 0)
  133. goto out;
  134. if (N2 == M2) {
  135. info->npll_ctrl = 0x80000100 | (log2P << 16);
  136. info->npll_coef = (N1 << 8) | M1;
  137. } else {
  138. info->npll_ctrl = 0xc0000000 | (log2P << 16);
  139. info->npll_coef = (N2 << 24) | (M2 << 16) | (N1 << 8) | M1;
  140. }
  141. /* use the second PLL for shader/rop clock, if it differs from core */
  142. if (perflvl->shader && perflvl->shader != perflvl->core) {
  143. ret = nv40_calc_pll(dev, 0x004008, &pll, perflvl->shader,
  144. &N1, &M1, NULL, NULL, &log2P);
  145. if (ret < 0)
  146. goto out;
  147. info->spll = 0xc0000000 | (log2P << 16) | (N1 << 8) | M1;
  148. info->ctrl = 0x00000223;
  149. } else {
  150. info->spll = 0x00000000;
  151. info->ctrl = 0x00000333;
  152. }
  153. /* memory clock */
  154. ret = nv40_calc_pll(dev, 0x004020, &pll, perflvl->memory,
  155. &N1, &M1, &N2, &M2, &log2P);
  156. if (ret < 0)
  157. goto out;
  158. info->mpll_ctrl = 0x80000000 | (log2P << 16);
  159. info->mpll_ctrl |= min2(pll.log2p_bias + log2P, pll.max_log2p) << 20;
  160. if (N2 == M2) {
  161. info->mpll_ctrl |= 0x00000100;
  162. info->mpll_coef = (N1 << 8) | M1;
  163. } else {
  164. info->mpll_ctrl |= 0x40000000;
  165. info->mpll_coef = (N2 << 24) | (M2 << 16) | (N1 << 8) | M1;
  166. }
  167. out:
  168. if (ret < 0) {
  169. kfree(info);
  170. info = ERR_PTR(ret);
  171. }
  172. return info;
  173. }
  174. static bool
  175. nv40_pm_gr_idle(void *data)
  176. {
  177. struct drm_device *dev = data;
  178. if ((nv_rd32(dev, 0x400760) & 0x000000f0) >> 4 !=
  179. (nv_rd32(dev, 0x400760) & 0x0000000f))
  180. return false;
  181. if (nv_rd32(dev, 0x400700))
  182. return false;
  183. return true;
  184. }
  185. void
  186. nv40_pm_clocks_set(struct drm_device *dev, void *pre_state)
  187. {
  188. struct drm_nouveau_private *dev_priv = dev->dev_private;
  189. struct nv40_pm_state *info = pre_state;
  190. unsigned long flags;
  191. struct bit_entry M;
  192. u32 crtc_mask = 0;
  193. u8 sr1[2];
  194. int i;
  195. /* determine which CRTCs are active, fetch VGA_SR1 for each */
  196. for (i = 0; i < 2; i++) {
  197. u32 vbl = nv_rd32(dev, 0x600808 + (i * 0x2000));
  198. u32 cnt = 0;
  199. do {
  200. if (vbl != nv_rd32(dev, 0x600808 + (i * 0x2000))) {
  201. nv_wr08(dev, 0x0c03c4 + (i * 0x2000), 0x01);
  202. sr1[i] = nv_rd08(dev, 0x0c03c5 + (i * 0x2000));
  203. if (!(sr1[i] & 0x20))
  204. crtc_mask |= (1 << i);
  205. break;
  206. }
  207. udelay(1);
  208. } while (cnt++ < 32);
  209. }
  210. /* halt and idle engines */
  211. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  212. nv_mask(dev, 0x002500, 0x00000001, 0x00000000);
  213. if (!nv_wait(dev, 0x002500, 0x00000010, 0x00000000))
  214. goto resume;
  215. nv_mask(dev, 0x003220, 0x00000001, 0x00000000);
  216. if (!nv_wait(dev, 0x003220, 0x00000010, 0x00000000))
  217. goto resume;
  218. nv_mask(dev, 0x003200, 0x00000001, 0x00000000);
  219. nv04_fifo_cache_pull(dev, false);
  220. if (!nv_wait_cb(dev, nv40_pm_gr_idle, dev))
  221. goto resume;
  222. /* set engine clocks */
  223. nv_mask(dev, 0x00c040, 0x00000333, 0x00000000);
  224. nv_wr32(dev, 0x004004, info->npll_coef);
  225. nv_mask(dev, 0x004000, 0xc0070100, info->npll_ctrl);
  226. nv_mask(dev, 0x004008, 0xc007ffff, info->spll);
  227. mdelay(5);
  228. nv_mask(dev, 0x00c040, 0x00000333, info->ctrl);
  229. /* wait for vblank start on active crtcs, disable memory access */
  230. for (i = 0; i < 2; i++) {
  231. if (!(crtc_mask & (1 << i)))
  232. continue;
  233. nv_wait(dev, 0x600808 + (i * 0x2000), 0x00010000, 0x00000000);
  234. nv_wait(dev, 0x600808 + (i * 0x2000), 0x00010000, 0x00010000);
  235. nv_wr08(dev, 0x0c03c4 + (i * 0x2000), 0x01);
  236. nv_wr08(dev, 0x0c03c5 + (i * 0x2000), sr1[i] | 0x20);
  237. }
  238. /* prepare ram for reclocking */
  239. nv_wr32(dev, 0x1002d4, 0x00000001); /* precharge */
  240. nv_wr32(dev, 0x1002d0, 0x00000001); /* refresh */
  241. nv_wr32(dev, 0x1002d0, 0x00000001); /* refresh */
  242. nv_mask(dev, 0x100210, 0x80000000, 0x00000000); /* no auto refresh */
  243. nv_wr32(dev, 0x1002dc, 0x00000001); /* enable self-refresh */
  244. /* change the PLL of each memory partition */
  245. nv_mask(dev, 0x00c040, 0x0000c000, 0x00000000);
  246. switch (dev_priv->chipset) {
  247. case 0x40:
  248. case 0x45:
  249. case 0x41:
  250. case 0x42:
  251. case 0x47:
  252. nv_mask(dev, 0x004044, 0xc0771100, info->mpll_ctrl);
  253. nv_mask(dev, 0x00402c, 0xc0771100, info->mpll_ctrl);
  254. nv_wr32(dev, 0x004048, info->mpll_coef);
  255. nv_wr32(dev, 0x004030, info->mpll_coef);
  256. case 0x43:
  257. case 0x49:
  258. case 0x4b:
  259. nv_mask(dev, 0x004038, 0xc0771100, info->mpll_ctrl);
  260. nv_wr32(dev, 0x00403c, info->mpll_coef);
  261. default:
  262. nv_mask(dev, 0x004020, 0xc0771100, info->mpll_ctrl);
  263. nv_wr32(dev, 0x004024, info->mpll_coef);
  264. break;
  265. }
  266. udelay(100);
  267. nv_mask(dev, 0x00c040, 0x0000c000, 0x0000c000);
  268. /* re-enable normal operation of memory controller */
  269. nv_wr32(dev, 0x1002dc, 0x00000000);
  270. nv_mask(dev, 0x100210, 0x80000000, 0x80000000);
  271. udelay(100);
  272. /* execute memory reset script from vbios */
  273. if (!bit_table(dev, 'M', &M))
  274. nouveau_bios_init_exec(dev, ROM16(M.data[0]));
  275. /* make sure we're in vblank (hopefully the same one as before), and
  276. * then re-enable crtc memory access
  277. */
  278. for (i = 0; i < 2; i++) {
  279. if (!(crtc_mask & (1 << i)))
  280. continue;
  281. nv_wait(dev, 0x600808 + (i * 0x2000), 0x00010000, 0x00010000);
  282. nv_wr08(dev, 0x0c03c4 + (i * 0x2000), 0x01);
  283. nv_wr08(dev, 0x0c03c5 + (i * 0x2000), sr1[i]);
  284. }
  285. /* resume engines */
  286. resume:
  287. nv_wr32(dev, 0x003250, 0x00000001);
  288. nv_mask(dev, 0x003220, 0x00000001, 0x00000001);
  289. nv_wr32(dev, 0x003200, 0x00000001);
  290. nv_wr32(dev, 0x002500, 0x00000001);
  291. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  292. kfree(info);
  293. }