nva3_pm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 *, u32, int);
  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, 0x00e820, 0x41);
  36. return read_pll(dev, 0x00e8a0, 0x42);
  37. }
  38. static u32
  39. read_clk(struct drm_device *dev, int clk, bool ignore_en)
  40. {
  41. u32 sctl, sdiv, sclk;
  42. if (clk >= 0x40)
  43. return 27000;
  44. sctl = nv_rd32(dev, 0x4120 + (clk * 4));
  45. if (!ignore_en && !(sctl & 0x00000100))
  46. return 0;
  47. switch (sctl & 0x00003000) {
  48. case 0x00000000:
  49. return 27000;
  50. case 0x00002000:
  51. if (sctl & 0x00000040)
  52. return 108000;
  53. return 100000;
  54. case 0x00003000:
  55. sclk = read_vco(dev, clk);
  56. sdiv = ((sctl & 0x003f0000) >> 16) + 2;
  57. return (sclk * 2) / sdiv;
  58. default:
  59. return 0;
  60. }
  61. }
  62. static u32
  63. read_pll(struct drm_device *dev, u32 pll, int clk)
  64. {
  65. u32 ctrl = nv_rd32(dev, pll + 0);
  66. u32 sclk, P = 1, N = 1, M = 1;
  67. if (!(ctrl & 0x00000008)) {
  68. u32 coef = nv_rd32(dev, pll + 4);
  69. M = (coef & 0x000000ff) >> 0;
  70. N = (coef & 0x0000ff00) >> 8;
  71. P = (coef & 0x003f0000) >> 16;
  72. if ((pll & 0x00ff00) == 0x00e800)
  73. P = 1;
  74. sclk = read_clk(dev, 0x00 + clk, false);
  75. } else {
  76. sclk = read_clk(dev, 0x10 + clk, false);
  77. }
  78. return sclk * N / (M * P);
  79. }
  80. struct creg {
  81. u32 clk;
  82. u32 pll;
  83. };
  84. static int
  85. calc_clk(struct drm_device *dev, u32 pll, int clk, u32 khz, struct creg *reg)
  86. {
  87. struct pll_lims limits;
  88. u32 oclk, sclk, sdiv;
  89. int P, N, M, diff;
  90. int ret;
  91. reg->pll = 0;
  92. reg->clk = 0;
  93. switch (khz) {
  94. case 27000:
  95. reg->clk = 0x00000100;
  96. return khz;
  97. case 100000:
  98. reg->clk = 0x00002100;
  99. return khz;
  100. case 108000:
  101. reg->clk = 0x00002140;
  102. return khz;
  103. default:
  104. sclk = read_vco(dev, clk);
  105. sdiv = min((sclk * 2) / (khz - 2999), (u32)65);
  106. if (sdiv > 4) {
  107. oclk = (sclk * 2) / sdiv;
  108. diff = khz - oclk;
  109. if (!pll || (diff >= -2000 && diff < 3000)) {
  110. reg->clk = (((sdiv - 2) << 16) | 0x00003100);
  111. return oclk;
  112. }
  113. }
  114. break;
  115. }
  116. ret = get_pll_limits(dev, pll, &limits);
  117. if (ret)
  118. return ret;
  119. limits.refclk = read_clk(dev, clk - 0x10, true);
  120. if (!limits.refclk)
  121. return -EINVAL;
  122. ret = nva3_calc_pll(dev, &limits, khz, &N, NULL, &M, &P);
  123. if (ret >= 0) {
  124. reg->clk = nv_rd32(dev, 0x4120 + (clk * 4));
  125. reg->pll = (P << 16) | (N << 8) | M;
  126. }
  127. return ret;
  128. }
  129. int
  130. nva3_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  131. {
  132. perflvl->core = read_pll(dev, 0x4200, 0);
  133. perflvl->shader = read_pll(dev, 0x4220, 1);
  134. perflvl->memory = read_pll(dev, 0x4000, 2);
  135. return 0;
  136. }
  137. struct nva3_pm_state {
  138. struct creg nclk;
  139. struct creg sclk;
  140. struct creg mclk;
  141. };
  142. void *
  143. nva3_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  144. {
  145. struct nva3_pm_state *info;
  146. int ret;
  147. info = kzalloc(sizeof(*info), GFP_KERNEL);
  148. if (!info)
  149. return ERR_PTR(-ENOMEM);
  150. ret = calc_clk(dev, 0x4200, 0x10, perflvl->core, &info->nclk);
  151. if (ret < 0)
  152. goto out;
  153. ret = calc_clk(dev, 0x4220, 0x11, perflvl->shader, &info->sclk);
  154. if (ret < 0)
  155. goto out;
  156. ret = calc_clk(dev, 0x4000, 0x12, perflvl->memory, &info->mclk);
  157. if (ret < 0)
  158. goto out;
  159. out:
  160. if (ret < 0) {
  161. kfree(info);
  162. info = ERR_PTR(ret);
  163. }
  164. return info;
  165. }
  166. static void
  167. prog_pll(struct drm_device *dev, u32 pll, int clk, struct creg *reg)
  168. {
  169. const u32 src0 = 0x004120 + (clk * 4);
  170. const u32 src1 = 0x004160 + (clk * 4);
  171. const u32 ctrl = pll + 0;
  172. const u32 coef = pll + 4;
  173. u32 cntl;
  174. cntl = nv_rd32(dev, ctrl) & 0xfffffff2;
  175. if (reg->pll) {
  176. nv_mask(dev, src0, 0x00000101, 0x00000101);
  177. nv_wr32(dev, coef, reg->pll);
  178. nv_wr32(dev, ctrl, cntl | 0x00000015);
  179. nv_mask(dev, src1, 0x00000100, 0x00000000);
  180. nv_mask(dev, src1, 0x00000001, 0x00000000);
  181. } else {
  182. nv_mask(dev, src1, 0x003f3141, 0x00000101 | reg->clk);
  183. nv_wr32(dev, ctrl, cntl | 0x0000001d);
  184. nv_mask(dev, ctrl, 0x00000001, 0x00000000);
  185. nv_mask(dev, src0, 0x00000100, 0x00000000);
  186. nv_mask(dev, src0, 0x00000001, 0x00000000);
  187. }
  188. }
  189. void
  190. nva3_pm_clocks_set(struct drm_device *dev, void *pre_state)
  191. {
  192. struct nva3_pm_state *info = pre_state;
  193. prog_pll(dev, 0x004200, 0, &info->nclk);
  194. prog_pll(dev, 0x004220, 1, &info->sclk);
  195. nv_wr32(dev, 0x100210, 0);
  196. nv_wr32(dev, 0x1002dc, 1);
  197. nv_wr32(dev, 0x004018, 0x00001000);
  198. prog_pll(dev, 0x004000, 2, &info->mclk);
  199. if (nv_rd32(dev, 0x4000) & 0x00000008)
  200. nv_wr32(dev, 0x004018, 0x1000d000);
  201. else
  202. nv_wr32(dev, 0x004018, 0x10005000);
  203. nv_wr32(dev, 0x1002dc, 0);
  204. nv_wr32(dev, 0x100210, 0x80000000);
  205. kfree(info);
  206. }