nv40_pm.c 9.7 KB

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