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