nv40_pm.c 9.0 KB

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