nv50_pm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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_hw.h"
  28. #include "nouveau_pm.h"
  29. #include "nouveau_hwsq.h"
  30. #include "nv50_display.h"
  31. enum clk_src {
  32. clk_src_crystal,
  33. clk_src_href,
  34. clk_src_hclk,
  35. clk_src_hclkm3,
  36. clk_src_hclkm3d2,
  37. clk_src_host,
  38. clk_src_nvclk,
  39. clk_src_sclk,
  40. clk_src_mclk,
  41. clk_src_vdec,
  42. clk_src_dom6
  43. };
  44. static u32 read_clk(struct drm_device *, enum clk_src);
  45. static u32
  46. read_div(struct drm_device *dev)
  47. {
  48. struct drm_nouveau_private *dev_priv = dev->dev_private;
  49. switch (dev_priv->chipset) {
  50. case 0x50: /* it exists, but only has bit 31, not the dividers.. */
  51. case 0x84:
  52. case 0x86:
  53. case 0x98:
  54. case 0xa0:
  55. return nv_rd32(dev, 0x004700);
  56. case 0x92:
  57. case 0x94:
  58. case 0x96:
  59. return nv_rd32(dev, 0x004800);
  60. default:
  61. return 0x00000000;
  62. }
  63. }
  64. static u32
  65. read_pll_src(struct drm_device *dev, u32 base)
  66. {
  67. struct drm_nouveau_private *dev_priv = dev->dev_private;
  68. u32 coef, ref = read_clk(dev, clk_src_crystal);
  69. u32 rsel = nv_rd32(dev, 0x00e18c);
  70. int P, N, M, id;
  71. switch (dev_priv->chipset) {
  72. case 0x50:
  73. case 0xa0:
  74. switch (base) {
  75. case 0x4020:
  76. case 0x4028: id = !!(rsel & 0x00000004); break;
  77. case 0x4008: id = !!(rsel & 0x00000008); break;
  78. case 0x4030: id = 0; break;
  79. default:
  80. NV_ERROR(dev, "ref: bad pll 0x%06x\n", base);
  81. return 0;
  82. }
  83. coef = nv_rd32(dev, 0x00e81c + (id * 0x0c));
  84. ref *= (coef & 0x01000000) ? 2 : 4;
  85. P = (coef & 0x00070000) >> 16;
  86. N = ((coef & 0x0000ff00) >> 8) + 1;
  87. M = ((coef & 0x000000ff) >> 0) + 1;
  88. break;
  89. case 0x84:
  90. case 0x86:
  91. case 0x92:
  92. coef = nv_rd32(dev, 0x00e81c);
  93. P = (coef & 0x00070000) >> 16;
  94. N = (coef & 0x0000ff00) >> 8;
  95. M = (coef & 0x000000ff) >> 0;
  96. break;
  97. case 0x94:
  98. case 0x96:
  99. case 0x98:
  100. rsel = nv_rd32(dev, 0x00c050);
  101. switch (base) {
  102. case 0x4020: rsel = (rsel & 0x00000003) >> 0; break;
  103. case 0x4008: rsel = (rsel & 0x0000000c) >> 2; break;
  104. case 0x4028: rsel = (rsel & 0x00001800) >> 11; break;
  105. case 0x4030: rsel = 3; break;
  106. default:
  107. NV_ERROR(dev, "ref: bad pll 0x%06x\n", base);
  108. return 0;
  109. }
  110. switch (rsel) {
  111. case 0: id = 1; break;
  112. case 1: return read_clk(dev, clk_src_crystal);
  113. case 2: return read_clk(dev, clk_src_href);
  114. case 3: id = 0; break;
  115. }
  116. coef = nv_rd32(dev, 0x00e81c + (id * 0x28));
  117. P = (nv_rd32(dev, 0x00e824 + (id * 0x28)) >> 16) & 7;
  118. P += (coef & 0x00070000) >> 16;
  119. N = (coef & 0x0000ff00) >> 8;
  120. M = (coef & 0x000000ff) >> 0;
  121. break;
  122. default:
  123. BUG_ON(1);
  124. }
  125. if (M)
  126. return (ref * N / M) >> P;
  127. return 0;
  128. }
  129. static u32
  130. read_pll_ref(struct drm_device *dev, u32 base)
  131. {
  132. u32 src, mast = nv_rd32(dev, 0x00c040);
  133. switch (base) {
  134. case 0x004028:
  135. src = !!(mast & 0x00200000);
  136. break;
  137. case 0x004020:
  138. src = !!(mast & 0x00400000);
  139. break;
  140. case 0x004008:
  141. src = !!(mast & 0x00010000);
  142. break;
  143. case 0x004030:
  144. src = !!(mast & 0x02000000);
  145. break;
  146. case 0x00e810:
  147. return read_clk(dev, clk_src_crystal);
  148. default:
  149. NV_ERROR(dev, "bad pll 0x%06x\n", base);
  150. return 0;
  151. }
  152. if (src)
  153. return read_clk(dev, clk_src_href);
  154. return read_pll_src(dev, base);
  155. }
  156. static u32
  157. read_pll(struct drm_device *dev, u32 base)
  158. {
  159. struct drm_nouveau_private *dev_priv = dev->dev_private;
  160. u32 mast = nv_rd32(dev, 0x00c040);
  161. u32 ctrl = nv_rd32(dev, base + 0);
  162. u32 coef = nv_rd32(dev, base + 4);
  163. u32 ref = read_pll_ref(dev, base);
  164. u32 clk = 0;
  165. int N1, N2, M1, M2;
  166. if (base == 0x004028 && (mast & 0x00100000)) {
  167. /* wtf, appears to only disable post-divider on nva0 */
  168. if (dev_priv->chipset != 0xa0)
  169. return read_clk(dev, clk_src_dom6);
  170. }
  171. N2 = (coef & 0xff000000) >> 24;
  172. M2 = (coef & 0x00ff0000) >> 16;
  173. N1 = (coef & 0x0000ff00) >> 8;
  174. M1 = (coef & 0x000000ff);
  175. if ((ctrl & 0x80000000) && M1) {
  176. clk = ref * N1 / M1;
  177. if ((ctrl & 0x40000100) == 0x40000000) {
  178. if (M2)
  179. clk = clk * N2 / M2;
  180. else
  181. clk = 0;
  182. }
  183. }
  184. return clk;
  185. }
  186. static u32
  187. read_clk(struct drm_device *dev, enum clk_src src)
  188. {
  189. struct drm_nouveau_private *dev_priv = dev->dev_private;
  190. u32 mast = nv_rd32(dev, 0x00c040);
  191. u32 P = 0;
  192. switch (src) {
  193. case clk_src_crystal:
  194. return dev_priv->crystal;
  195. case clk_src_href:
  196. return 100000; /* PCIE reference clock */
  197. case clk_src_hclk:
  198. return read_clk(dev, clk_src_href) * 27778 / 10000;
  199. case clk_src_hclkm3:
  200. return read_clk(dev, clk_src_hclk) * 3;
  201. case clk_src_hclkm3d2:
  202. return read_clk(dev, clk_src_hclk) * 3 / 2;
  203. case clk_src_host:
  204. switch (mast & 0x30000000) {
  205. case 0x00000000: return read_clk(dev, clk_src_href);
  206. case 0x10000000: break;
  207. case 0x20000000: /* !0x50 */
  208. case 0x30000000: return read_clk(dev, clk_src_hclk);
  209. }
  210. break;
  211. case clk_src_nvclk:
  212. if (!(mast & 0x00100000))
  213. P = (nv_rd32(dev, 0x004028) & 0x00070000) >> 16;
  214. switch (mast & 0x00000003) {
  215. case 0x00000000: return read_clk(dev, clk_src_crystal) >> P;
  216. case 0x00000001: return read_clk(dev, clk_src_dom6);
  217. case 0x00000002: return read_pll(dev, 0x004020) >> P;
  218. case 0x00000003: return read_pll(dev, 0x004028) >> P;
  219. }
  220. break;
  221. case clk_src_sclk:
  222. P = (nv_rd32(dev, 0x004020) & 0x00070000) >> 16;
  223. switch (mast & 0x00000030) {
  224. case 0x00000000:
  225. if (mast & 0x00000080)
  226. return read_clk(dev, clk_src_host) >> P;
  227. return read_clk(dev, clk_src_crystal) >> P;
  228. case 0x00000010: break;
  229. case 0x00000020: return read_pll(dev, 0x004028) >> P;
  230. case 0x00000030: return read_pll(dev, 0x004020) >> P;
  231. }
  232. break;
  233. case clk_src_mclk:
  234. P = (nv_rd32(dev, 0x004008) & 0x00070000) >> 16;
  235. if (nv_rd32(dev, 0x004008) & 0x00000200) {
  236. switch (mast & 0x0000c000) {
  237. case 0x00000000:
  238. return read_clk(dev, clk_src_crystal) >> P;
  239. case 0x00008000:
  240. case 0x0000c000:
  241. return read_clk(dev, clk_src_href) >> P;
  242. }
  243. } else {
  244. return read_pll(dev, 0x004008) >> P;
  245. }
  246. break;
  247. case clk_src_vdec:
  248. P = (read_div(dev) & 0x00000700) >> 8;
  249. switch (dev_priv->chipset) {
  250. case 0x84:
  251. case 0x86:
  252. case 0x92:
  253. case 0x94:
  254. case 0x96:
  255. case 0xa0:
  256. switch (mast & 0x00000c00) {
  257. case 0x00000000:
  258. if (dev_priv->chipset == 0xa0) /* wtf?? */
  259. return read_clk(dev, clk_src_nvclk) >> P;
  260. return read_clk(dev, clk_src_crystal) >> P;
  261. case 0x00000400:
  262. return 0;
  263. case 0x00000800:
  264. if (mast & 0x01000000)
  265. return read_pll(dev, 0x004028) >> P;
  266. return read_pll(dev, 0x004030) >> P;
  267. case 0x00000c00:
  268. return read_clk(dev, clk_src_nvclk) >> P;
  269. }
  270. break;
  271. case 0x98:
  272. switch (mast & 0x00000c00) {
  273. case 0x00000000:
  274. return read_clk(dev, clk_src_nvclk) >> P;
  275. case 0x00000400:
  276. return 0;
  277. case 0x00000800:
  278. return read_clk(dev, clk_src_hclkm3d2) >> P;
  279. case 0x00000c00:
  280. return read_clk(dev, clk_src_mclk) >> P;
  281. }
  282. break;
  283. }
  284. break;
  285. case clk_src_dom6:
  286. switch (dev_priv->chipset) {
  287. case 0x50:
  288. case 0xa0:
  289. return read_pll(dev, 0x00e810) >> 2;
  290. case 0x84:
  291. case 0x86:
  292. case 0x92:
  293. case 0x94:
  294. case 0x96:
  295. case 0x98:
  296. P = (read_div(dev) & 0x00000007) >> 0;
  297. switch (mast & 0x0c000000) {
  298. case 0x00000000: return read_clk(dev, clk_src_href);
  299. case 0x04000000: break;
  300. case 0x08000000: return read_clk(dev, clk_src_hclk);
  301. case 0x0c000000:
  302. return read_clk(dev, clk_src_hclkm3) >> P;
  303. }
  304. break;
  305. default:
  306. break;
  307. }
  308. default:
  309. break;
  310. }
  311. NV_DEBUG(dev, "unknown clock source %d 0x%08x\n", src, mast);
  312. return 0;
  313. }
  314. int
  315. nv50_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  316. {
  317. struct drm_nouveau_private *dev_priv = dev->dev_private;
  318. if (dev_priv->chipset == 0xaa ||
  319. dev_priv->chipset == 0xac)
  320. return 0;
  321. perflvl->core = read_clk(dev, clk_src_nvclk);
  322. perflvl->shader = read_clk(dev, clk_src_sclk);
  323. perflvl->memory = read_clk(dev, clk_src_mclk);
  324. if (dev_priv->chipset != 0x50) {
  325. perflvl->vdec = read_clk(dev, clk_src_vdec);
  326. perflvl->dom6 = read_clk(dev, clk_src_dom6);
  327. }
  328. return 0;
  329. }
  330. struct nv50_pm_state {
  331. struct nouveau_pm_level *perflvl;
  332. struct hwsq_ucode eclk_hwsq;
  333. struct hwsq_ucode mclk_hwsq;
  334. u32 mscript;
  335. u32 mmast;
  336. u32 mctrl;
  337. u32 mcoef;
  338. };
  339. static u32
  340. calc_pll(struct drm_device *dev, u32 reg, struct pll_lims *pll,
  341. u32 clk, int *N1, int *M1, int *log2P)
  342. {
  343. struct nouveau_pll_vals coef;
  344. int ret;
  345. ret = get_pll_limits(dev, reg, pll);
  346. if (ret)
  347. return 0;
  348. pll->vco2.maxfreq = 0;
  349. pll->refclk = read_pll_ref(dev, reg);
  350. if (!pll->refclk)
  351. return 0;
  352. ret = nouveau_calc_pll_mnp(dev, pll, clk, &coef);
  353. if (ret == 0)
  354. return 0;
  355. *N1 = coef.N1;
  356. *M1 = coef.M1;
  357. *log2P = coef.log2P;
  358. return ret;
  359. }
  360. static inline u32
  361. calc_div(u32 src, u32 target, int *div)
  362. {
  363. u32 clk0 = src, clk1 = src;
  364. for (*div = 0; *div <= 7; (*div)++) {
  365. if (clk0 <= target) {
  366. clk1 = clk0 << (*div ? 1 : 0);
  367. break;
  368. }
  369. clk0 >>= 1;
  370. }
  371. if (target - clk0 <= clk1 - target)
  372. return clk0;
  373. (*div)--;
  374. return clk1;
  375. }
  376. static inline u32
  377. clk_same(u32 a, u32 b)
  378. {
  379. return ((a / 1000) == (b / 1000));
  380. }
  381. static void
  382. mclk_precharge(struct nouveau_mem_exec_func *exec)
  383. {
  384. struct nv50_pm_state *info = exec->priv;
  385. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  386. hwsq_wr32(hwsq, 0x1002d4, 0x00000001);
  387. }
  388. static void
  389. mclk_refresh(struct nouveau_mem_exec_func *exec)
  390. {
  391. struct nv50_pm_state *info = exec->priv;
  392. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  393. hwsq_wr32(hwsq, 0x1002d0, 0x00000001);
  394. }
  395. static void
  396. mclk_refresh_auto(struct nouveau_mem_exec_func *exec, bool enable)
  397. {
  398. struct nv50_pm_state *info = exec->priv;
  399. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  400. hwsq_wr32(hwsq, 0x100210, enable ? 0x80000000 : 0x00000000);
  401. }
  402. static void
  403. mclk_refresh_self(struct nouveau_mem_exec_func *exec, bool enable)
  404. {
  405. struct nv50_pm_state *info = exec->priv;
  406. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  407. hwsq_wr32(hwsq, 0x1002dc, enable ? 0x00000001 : 0x00000000);
  408. }
  409. static void
  410. mclk_wait(struct nouveau_mem_exec_func *exec, u32 nsec)
  411. {
  412. struct nv50_pm_state *info = exec->priv;
  413. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  414. if (nsec > 1000)
  415. hwsq_usec(hwsq, (nsec + 500) / 1000);
  416. }
  417. static u32
  418. mclk_mrg(struct nouveau_mem_exec_func *exec, int mr)
  419. {
  420. if (mr <= 1)
  421. return nv_rd32(exec->dev, 0x1002c0 + ((mr - 0) * 4));
  422. if (mr <= 3)
  423. return nv_rd32(exec->dev, 0x1002e0 + ((mr - 2) * 4));
  424. return 0;
  425. }
  426. static void
  427. mclk_mrs(struct nouveau_mem_exec_func *exec, int mr, u32 data)
  428. {
  429. struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
  430. struct nv50_pm_state *info = exec->priv;
  431. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  432. if (mr <= 1) {
  433. if (dev_priv->vram_rank_B)
  434. hwsq_wr32(hwsq, 0x1002c8 + ((mr - 0) * 4), data);
  435. hwsq_wr32(hwsq, 0x1002c0 + ((mr - 0) * 4), data);
  436. } else
  437. if (mr <= 3) {
  438. if (dev_priv->vram_rank_B)
  439. hwsq_wr32(hwsq, 0x1002e8 + ((mr - 2) * 4), data);
  440. hwsq_wr32(hwsq, 0x1002e0 + ((mr - 2) * 4), data);
  441. }
  442. }
  443. static void
  444. mclk_clock_set(struct nouveau_mem_exec_func *exec)
  445. {
  446. struct nv50_pm_state *info = exec->priv;
  447. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  448. u32 ctrl = nv_rd32(exec->dev, 0x004008);
  449. info->mmast = nv_rd32(exec->dev, 0x00c040);
  450. info->mmast &= ~0xc0000000; /* get MCLK_2 from HREF */
  451. info->mmast |= 0x0000c000; /* use MCLK_2 as MPLL_BYPASS clock */
  452. hwsq_wr32(hwsq, 0xc040, info->mmast);
  453. hwsq_wr32(hwsq, 0x4008, ctrl | 0x00000200); /* bypass MPLL */
  454. if (info->mctrl & 0x80000000)
  455. hwsq_wr32(hwsq, 0x400c, info->mcoef);
  456. hwsq_wr32(hwsq, 0x4008, info->mctrl);
  457. }
  458. static void
  459. mclk_timing_set(struct nouveau_mem_exec_func *exec)
  460. {
  461. struct drm_device *dev = exec->dev;
  462. struct nv50_pm_state *info = exec->priv;
  463. struct nouveau_pm_level *perflvl = info->perflvl;
  464. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  465. int i;
  466. for (i = 0; i < 9; i++) {
  467. u32 reg = 0x100220 + (i * 4);
  468. u32 val = nv_rd32(dev, reg);
  469. if (val != perflvl->timing.reg[i])
  470. hwsq_wr32(hwsq, reg, perflvl->timing.reg[i]);
  471. }
  472. }
  473. static int
  474. calc_mclk(struct drm_device *dev, struct nouveau_pm_level *perflvl,
  475. struct nv50_pm_state *info)
  476. {
  477. struct drm_nouveau_private *dev_priv = dev->dev_private;
  478. u32 crtc_mask = nv50_display_active_crtcs(dev);
  479. struct nouveau_mem_exec_func exec = {
  480. .dev = dev,
  481. .precharge = mclk_precharge,
  482. .refresh = mclk_refresh,
  483. .refresh_auto = mclk_refresh_auto,
  484. .refresh_self = mclk_refresh_self,
  485. .wait = mclk_wait,
  486. .mrg = mclk_mrg,
  487. .mrs = mclk_mrs,
  488. .clock_set = mclk_clock_set,
  489. .timing_set = mclk_timing_set,
  490. .priv = info
  491. };
  492. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  493. struct pll_lims pll;
  494. int N, M, P;
  495. int ret;
  496. /* use pcie refclock if possible, otherwise use mpll */
  497. info->mctrl = nv_rd32(dev, 0x004008);
  498. info->mctrl &= ~0x81ff0200;
  499. if (clk_same(perflvl->memory, read_clk(dev, clk_src_href))) {
  500. info->mctrl |= 0x00000200 | (pll.log2p_bias << 19);
  501. } else {
  502. ret = calc_pll(dev, 0x4008, &pll, perflvl->memory, &N, &M, &P);
  503. if (ret == 0)
  504. return -EINVAL;
  505. info->mctrl |= 0x80000000 | (P << 22) | (P << 16);
  506. info->mctrl |= pll.log2p_bias << 19;
  507. info->mcoef = (N << 8) | M;
  508. }
  509. /* build the ucode which will reclock the memory for us */
  510. hwsq_init(hwsq);
  511. if (crtc_mask) {
  512. hwsq_op5f(hwsq, crtc_mask, 0x00); /* wait for scanout */
  513. hwsq_op5f(hwsq, crtc_mask, 0x01); /* wait for vblank */
  514. }
  515. if (dev_priv->chipset >= 0x92)
  516. hwsq_wr32(hwsq, 0x611200, 0x00003300); /* disable scanout */
  517. hwsq_setf(hwsq, 0x10, 0); /* disable bus access */
  518. hwsq_op5f(hwsq, 0x00, 0x01); /* no idea :s */
  519. ret = nouveau_mem_exec(&exec, perflvl);
  520. if (ret)
  521. return ret;
  522. hwsq_setf(hwsq, 0x10, 1); /* enable bus access */
  523. hwsq_op5f(hwsq, 0x00, 0x00); /* no idea, reverse of 0x00, 0x01? */
  524. if (dev_priv->chipset >= 0x92)
  525. hwsq_wr32(hwsq, 0x611200, 0x00003330); /* enable scanout */
  526. hwsq_fini(hwsq);
  527. return 0;
  528. }
  529. void *
  530. nv50_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  531. {
  532. struct drm_nouveau_private *dev_priv = dev->dev_private;
  533. struct nv50_pm_state *info;
  534. struct hwsq_ucode *hwsq;
  535. struct pll_lims pll;
  536. u32 out, mast, divs, ctrl;
  537. int clk, ret = -EINVAL;
  538. int N, M, P1, P2;
  539. if (dev_priv->chipset == 0xaa ||
  540. dev_priv->chipset == 0xac)
  541. return ERR_PTR(-ENODEV);
  542. info = kmalloc(sizeof(*info), GFP_KERNEL);
  543. if (!info)
  544. return ERR_PTR(-ENOMEM);
  545. info->perflvl = perflvl;
  546. /* memory: build hwsq ucode which we'll use to reclock memory.
  547. * use pcie refclock if possible, otherwise use mpll */
  548. info->mclk_hwsq.len = 0;
  549. if (perflvl->memory) {
  550. ret = calc_mclk(dev, perflvl, info);
  551. if (ret)
  552. goto error;
  553. info->mscript = perflvl->memscript;
  554. }
  555. divs = read_div(dev);
  556. mast = info->mmast;
  557. /* start building HWSQ script for engine reclocking */
  558. hwsq = &info->eclk_hwsq;
  559. hwsq_init(hwsq);
  560. hwsq_setf(hwsq, 0x10, 0); /* disable bus access */
  561. hwsq_op5f(hwsq, 0x00, 0x01); /* wait for access disabled? */
  562. /* vdec/dom6: switch to "safe" clocks temporarily */
  563. if (perflvl->vdec) {
  564. mast &= ~0x00000c00;
  565. divs &= ~0x00000700;
  566. }
  567. if (perflvl->dom6) {
  568. mast &= ~0x0c000000;
  569. divs &= ~0x00000007;
  570. }
  571. hwsq_wr32(hwsq, 0x00c040, mast);
  572. /* vdec: avoid modifying xpll until we know exactly how the other
  573. * clock domains work, i suspect at least some of them can also be
  574. * tied to xpll...
  575. */
  576. if (perflvl->vdec) {
  577. /* see how close we can get using nvclk as a source */
  578. clk = calc_div(perflvl->core, perflvl->vdec, &P1);
  579. /* see how close we can get using xpll/hclk as a source */
  580. if (dev_priv->chipset != 0x98)
  581. out = read_pll(dev, 0x004030);
  582. else
  583. out = read_clk(dev, clk_src_hclkm3d2);
  584. out = calc_div(out, perflvl->vdec, &P2);
  585. /* select whichever gets us closest */
  586. if (abs((int)perflvl->vdec - clk) <=
  587. abs((int)perflvl->vdec - out)) {
  588. if (dev_priv->chipset != 0x98)
  589. mast |= 0x00000c00;
  590. divs |= P1 << 8;
  591. } else {
  592. mast |= 0x00000800;
  593. divs |= P2 << 8;
  594. }
  595. }
  596. /* dom6: nfi what this is, but we're limited to various combinations
  597. * of the host clock frequency
  598. */
  599. if (perflvl->dom6) {
  600. if (clk_same(perflvl->dom6, read_clk(dev, clk_src_href))) {
  601. mast |= 0x00000000;
  602. } else
  603. if (clk_same(perflvl->dom6, read_clk(dev, clk_src_hclk))) {
  604. mast |= 0x08000000;
  605. } else {
  606. clk = read_clk(dev, clk_src_hclk) * 3;
  607. clk = calc_div(clk, perflvl->dom6, &P1);
  608. mast |= 0x0c000000;
  609. divs |= P1;
  610. }
  611. }
  612. /* vdec/dom6: complete switch to new clocks */
  613. switch (dev_priv->chipset) {
  614. case 0x92:
  615. case 0x94:
  616. case 0x96:
  617. hwsq_wr32(hwsq, 0x004800, divs);
  618. break;
  619. default:
  620. hwsq_wr32(hwsq, 0x004700, divs);
  621. break;
  622. }
  623. hwsq_wr32(hwsq, 0x00c040, mast);
  624. /* core/shader: make sure sclk/nvclk are disconnected from their
  625. * PLLs (nvclk to dom6, sclk to hclk)
  626. */
  627. if (dev_priv->chipset < 0x92)
  628. mast = (mast & ~0x001000b0) | 0x00100080;
  629. else
  630. mast = (mast & ~0x000000b3) | 0x00000081;
  631. hwsq_wr32(hwsq, 0x00c040, mast);
  632. /* core: for the moment at least, always use nvpll */
  633. clk = calc_pll(dev, 0x4028, &pll, perflvl->core, &N, &M, &P1);
  634. if (clk == 0)
  635. goto error;
  636. ctrl = nv_rd32(dev, 0x004028) & ~0xc03f0100;
  637. mast &= ~0x00100000;
  638. mast |= 3;
  639. hwsq_wr32(hwsq, 0x004028, 0x80000000 | (P1 << 19) | (P1 << 16) | ctrl);
  640. hwsq_wr32(hwsq, 0x00402c, (N << 8) | M);
  641. /* shader: tie to nvclk if possible, otherwise use spll. have to be
  642. * very careful that the shader clock is at least twice the core, or
  643. * some chipsets will be very unhappy. i expect most or all of these
  644. * cases will be handled by tying to nvclk, but it's possible there's
  645. * corners
  646. */
  647. ctrl = nv_rd32(dev, 0x004020) & ~0xc03f0100;
  648. if (P1-- && perflvl->shader == (perflvl->core << 1)) {
  649. hwsq_wr32(hwsq, 0x004020, (P1 << 19) | (P1 << 16) | ctrl);
  650. hwsq_wr32(hwsq, 0x00c040, 0x00000020 | mast);
  651. } else {
  652. clk = calc_pll(dev, 0x4020, &pll, perflvl->shader, &N, &M, &P1);
  653. if (clk == 0)
  654. goto error;
  655. ctrl |= 0x80000000;
  656. hwsq_wr32(hwsq, 0x004020, (P1 << 19) | (P1 << 16) | ctrl);
  657. hwsq_wr32(hwsq, 0x004024, (N << 8) | M);
  658. hwsq_wr32(hwsq, 0x00c040, 0x00000030 | mast);
  659. }
  660. hwsq_setf(hwsq, 0x10, 1); /* enable bus access */
  661. hwsq_op5f(hwsq, 0x00, 0x00); /* wait for access enabled? */
  662. hwsq_fini(hwsq);
  663. return info;
  664. error:
  665. kfree(info);
  666. return ERR_PTR(ret);
  667. }
  668. static int
  669. prog_hwsq(struct drm_device *dev, struct hwsq_ucode *hwsq)
  670. {
  671. struct drm_nouveau_private *dev_priv = dev->dev_private;
  672. u32 hwsq_data, hwsq_kick;
  673. int i;
  674. if (dev_priv->chipset < 0x94) {
  675. hwsq_data = 0x001400;
  676. hwsq_kick = 0x00000003;
  677. } else {
  678. hwsq_data = 0x080000;
  679. hwsq_kick = 0x00000001;
  680. }
  681. /* upload hwsq ucode */
  682. nv_mask(dev, 0x001098, 0x00000008, 0x00000000);
  683. nv_wr32(dev, 0x001304, 0x00000000);
  684. if (dev_priv->chipset >= 0x92)
  685. nv_wr32(dev, 0x001318, 0x00000000);
  686. for (i = 0; i < hwsq->len / 4; i++)
  687. nv_wr32(dev, hwsq_data + (i * 4), hwsq->ptr.u32[i]);
  688. nv_mask(dev, 0x001098, 0x00000018, 0x00000018);
  689. /* launch, and wait for completion */
  690. nv_wr32(dev, 0x00130c, hwsq_kick);
  691. if (!nv_wait(dev, 0x001308, 0x00000100, 0x00000000)) {
  692. NV_ERROR(dev, "hwsq ucode exec timed out\n");
  693. NV_ERROR(dev, "0x001308: 0x%08x\n", nv_rd32(dev, 0x001308));
  694. for (i = 0; i < hwsq->len / 4; i++) {
  695. NV_ERROR(dev, "0x%06x: 0x%08x\n", 0x1400 + (i * 4),
  696. nv_rd32(dev, 0x001400 + (i * 4)));
  697. }
  698. return -EIO;
  699. }
  700. return 0;
  701. }
  702. int
  703. nv50_pm_clocks_set(struct drm_device *dev, void *data)
  704. {
  705. struct nv50_pm_state *info = data;
  706. struct bit_entry M;
  707. int ret = -EBUSY;
  708. /* halt and idle execution engines */
  709. nv_mask(dev, 0x002504, 0x00000001, 0x00000001);
  710. if (!nv_wait(dev, 0x002504, 0x00000010, 0x00000010))
  711. goto resume;
  712. if (!nv_wait(dev, 0x00251c, 0x0000003f, 0x0000003f))
  713. goto resume;
  714. /* program memory clock, if necessary - must come before engine clock
  715. * reprogramming due to how we construct the hwsq scripts in pre()
  716. */
  717. if (info->mclk_hwsq.len) {
  718. /* execute some scripts that do ??? from the vbios.. */
  719. if (!bit_table(dev, 'M', &M) && M.version == 1) {
  720. if (M.length >= 6)
  721. nouveau_bios_init_exec(dev, ROM16(M.data[5]));
  722. if (M.length >= 8)
  723. nouveau_bios_init_exec(dev, ROM16(M.data[7]));
  724. if (M.length >= 10)
  725. nouveau_bios_init_exec(dev, ROM16(M.data[9]));
  726. nouveau_bios_init_exec(dev, info->mscript);
  727. }
  728. ret = prog_hwsq(dev, &info->mclk_hwsq);
  729. if (ret)
  730. goto resume;
  731. }
  732. /* program engine clocks */
  733. ret = prog_hwsq(dev, &info->eclk_hwsq);
  734. resume:
  735. nv_mask(dev, 0x002504, 0x00000001, 0x00000000);
  736. kfree(info);
  737. return ret;
  738. }
  739. static int
  740. pwm_info(struct drm_device *dev, int *line, int *ctrl, int *indx)
  741. {
  742. if (*line == 0x04) {
  743. *ctrl = 0x00e100;
  744. *line = 4;
  745. *indx = 0;
  746. } else
  747. if (*line == 0x09) {
  748. *ctrl = 0x00e100;
  749. *line = 9;
  750. *indx = 1;
  751. } else
  752. if (*line == 0x10) {
  753. *ctrl = 0x00e28c;
  754. *line = 0;
  755. *indx = 0;
  756. } else {
  757. NV_ERROR(dev, "unknown pwm ctrl for gpio %d\n", *line);
  758. return -ENODEV;
  759. }
  760. return 0;
  761. }
  762. int
  763. nv50_pm_pwm_get(struct drm_device *dev, int line, u32 *divs, u32 *duty)
  764. {
  765. int ctrl, id, ret = pwm_info(dev, &line, &ctrl, &id);
  766. if (ret)
  767. return ret;
  768. if (nv_rd32(dev, ctrl) & (1 << line)) {
  769. *divs = nv_rd32(dev, 0x00e114 + (id * 8));
  770. *duty = nv_rd32(dev, 0x00e118 + (id * 8));
  771. return 0;
  772. }
  773. return -EINVAL;
  774. }
  775. int
  776. nv50_pm_pwm_set(struct drm_device *dev, int line, u32 divs, u32 duty)
  777. {
  778. int ctrl, id, ret = pwm_info(dev, &line, &ctrl, &id);
  779. if (ret)
  780. return ret;
  781. nv_mask(dev, ctrl, 0x00010001 << line, 0x00000001 << line);
  782. nv_wr32(dev, 0x00e114 + (id * 8), divs);
  783. nv_wr32(dev, 0x00e118 + (id * 8), duty | 0x80000000);
  784. return 0;
  785. }