nv50_pm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 nvbios_pll *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.max_freq = 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 nv50_pm_state *info = exec->priv;
  430. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  431. if (mr <= 1) {
  432. if (nvfb_vram_rank_B(exec->dev))
  433. hwsq_wr32(hwsq, 0x1002c8 + ((mr - 0) * 4), data);
  434. hwsq_wr32(hwsq, 0x1002c0 + ((mr - 0) * 4), data);
  435. } else
  436. if (mr <= 3) {
  437. if (nvfb_vram_rank_B(exec->dev))
  438. hwsq_wr32(hwsq, 0x1002e8 + ((mr - 2) * 4), data);
  439. hwsq_wr32(hwsq, 0x1002e0 + ((mr - 2) * 4), data);
  440. }
  441. }
  442. static void
  443. mclk_clock_set(struct nouveau_mem_exec_func *exec)
  444. {
  445. struct nv50_pm_state *info = exec->priv;
  446. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  447. u32 ctrl = nv_rd32(exec->dev, 0x004008);
  448. info->mmast = nv_rd32(exec->dev, 0x00c040);
  449. info->mmast &= ~0xc0000000; /* get MCLK_2 from HREF */
  450. info->mmast |= 0x0000c000; /* use MCLK_2 as MPLL_BYPASS clock */
  451. hwsq_wr32(hwsq, 0xc040, info->mmast);
  452. hwsq_wr32(hwsq, 0x4008, ctrl | 0x00000200); /* bypass MPLL */
  453. if (info->mctrl & 0x80000000)
  454. hwsq_wr32(hwsq, 0x400c, info->mcoef);
  455. hwsq_wr32(hwsq, 0x4008, info->mctrl);
  456. }
  457. static void
  458. mclk_timing_set(struct nouveau_mem_exec_func *exec)
  459. {
  460. struct drm_device *dev = exec->dev;
  461. struct nv50_pm_state *info = exec->priv;
  462. struct nouveau_pm_level *perflvl = info->perflvl;
  463. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  464. int i;
  465. for (i = 0; i < 9; i++) {
  466. u32 reg = 0x100220 + (i * 4);
  467. u32 val = nv_rd32(dev, reg);
  468. if (val != perflvl->timing.reg[i])
  469. hwsq_wr32(hwsq, reg, perflvl->timing.reg[i]);
  470. }
  471. }
  472. static int
  473. calc_mclk(struct drm_device *dev, struct nouveau_pm_level *perflvl,
  474. struct nv50_pm_state *info)
  475. {
  476. struct drm_nouveau_private *dev_priv = dev->dev_private;
  477. u32 crtc_mask = nv50_display_active_crtcs(dev);
  478. struct nouveau_mem_exec_func exec = {
  479. .dev = dev,
  480. .precharge = mclk_precharge,
  481. .refresh = mclk_refresh,
  482. .refresh_auto = mclk_refresh_auto,
  483. .refresh_self = mclk_refresh_self,
  484. .wait = mclk_wait,
  485. .mrg = mclk_mrg,
  486. .mrs = mclk_mrs,
  487. .clock_set = mclk_clock_set,
  488. .timing_set = mclk_timing_set,
  489. .priv = info
  490. };
  491. struct hwsq_ucode *hwsq = &info->mclk_hwsq;
  492. struct nvbios_pll pll;
  493. int N, M, P;
  494. int ret;
  495. /* use pcie refclock if possible, otherwise use mpll */
  496. info->mctrl = nv_rd32(dev, 0x004008);
  497. info->mctrl &= ~0x81ff0200;
  498. if (clk_same(perflvl->memory, read_clk(dev, clk_src_href))) {
  499. info->mctrl |= 0x00000200 | (pll.bias_p << 19);
  500. } else {
  501. ret = calc_pll(dev, 0x4008, &pll, perflvl->memory, &N, &M, &P);
  502. if (ret == 0)
  503. return -EINVAL;
  504. info->mctrl |= 0x80000000 | (P << 22) | (P << 16);
  505. info->mctrl |= pll.bias_p << 19;
  506. info->mcoef = (N << 8) | M;
  507. }
  508. /* build the ucode which will reclock the memory for us */
  509. hwsq_init(hwsq);
  510. if (crtc_mask) {
  511. hwsq_op5f(hwsq, crtc_mask, 0x00); /* wait for scanout */
  512. hwsq_op5f(hwsq, crtc_mask, 0x01); /* wait for vblank */
  513. }
  514. if (dev_priv->chipset >= 0x92)
  515. hwsq_wr32(hwsq, 0x611200, 0x00003300); /* disable scanout */
  516. hwsq_setf(hwsq, 0x10, 0); /* disable bus access */
  517. hwsq_op5f(hwsq, 0x00, 0x01); /* no idea :s */
  518. ret = nouveau_mem_exec(&exec, perflvl);
  519. if (ret)
  520. return ret;
  521. hwsq_setf(hwsq, 0x10, 1); /* enable bus access */
  522. hwsq_op5f(hwsq, 0x00, 0x00); /* no idea, reverse of 0x00, 0x01? */
  523. if (dev_priv->chipset >= 0x92)
  524. hwsq_wr32(hwsq, 0x611200, 0x00003330); /* enable scanout */
  525. hwsq_fini(hwsq);
  526. return 0;
  527. }
  528. void *
  529. nv50_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  530. {
  531. struct drm_nouveau_private *dev_priv = dev->dev_private;
  532. struct nv50_pm_state *info;
  533. struct hwsq_ucode *hwsq;
  534. struct nvbios_pll pll;
  535. u32 out, mast, divs, ctrl;
  536. int clk, ret = -EINVAL;
  537. int N, M, P1, P2;
  538. if (dev_priv->chipset == 0xaa ||
  539. dev_priv->chipset == 0xac)
  540. return ERR_PTR(-ENODEV);
  541. info = kmalloc(sizeof(*info), GFP_KERNEL);
  542. if (!info)
  543. return ERR_PTR(-ENOMEM);
  544. info->perflvl = perflvl;
  545. /* memory: build hwsq ucode which we'll use to reclock memory.
  546. * use pcie refclock if possible, otherwise use mpll */
  547. info->mclk_hwsq.len = 0;
  548. if (perflvl->memory) {
  549. ret = calc_mclk(dev, perflvl, info);
  550. if (ret)
  551. goto error;
  552. info->mscript = perflvl->memscript;
  553. }
  554. divs = read_div(dev);
  555. mast = info->mmast;
  556. /* start building HWSQ script for engine reclocking */
  557. hwsq = &info->eclk_hwsq;
  558. hwsq_init(hwsq);
  559. hwsq_setf(hwsq, 0x10, 0); /* disable bus access */
  560. hwsq_op5f(hwsq, 0x00, 0x01); /* wait for access disabled? */
  561. /* vdec/dom6: switch to "safe" clocks temporarily */
  562. if (perflvl->vdec) {
  563. mast &= ~0x00000c00;
  564. divs &= ~0x00000700;
  565. }
  566. if (perflvl->dom6) {
  567. mast &= ~0x0c000000;
  568. divs &= ~0x00000007;
  569. }
  570. hwsq_wr32(hwsq, 0x00c040, mast);
  571. /* vdec: avoid modifying xpll until we know exactly how the other
  572. * clock domains work, i suspect at least some of them can also be
  573. * tied to xpll...
  574. */
  575. if (perflvl->vdec) {
  576. /* see how close we can get using nvclk as a source */
  577. clk = calc_div(perflvl->core, perflvl->vdec, &P1);
  578. /* see how close we can get using xpll/hclk as a source */
  579. if (dev_priv->chipset != 0x98)
  580. out = read_pll(dev, 0x004030);
  581. else
  582. out = read_clk(dev, clk_src_hclkm3d2);
  583. out = calc_div(out, perflvl->vdec, &P2);
  584. /* select whichever gets us closest */
  585. if (abs((int)perflvl->vdec - clk) <=
  586. abs((int)perflvl->vdec - out)) {
  587. if (dev_priv->chipset != 0x98)
  588. mast |= 0x00000c00;
  589. divs |= P1 << 8;
  590. } else {
  591. mast |= 0x00000800;
  592. divs |= P2 << 8;
  593. }
  594. }
  595. /* dom6: nfi what this is, but we're limited to various combinations
  596. * of the host clock frequency
  597. */
  598. if (perflvl->dom6) {
  599. if (clk_same(perflvl->dom6, read_clk(dev, clk_src_href))) {
  600. mast |= 0x00000000;
  601. } else
  602. if (clk_same(perflvl->dom6, read_clk(dev, clk_src_hclk))) {
  603. mast |= 0x08000000;
  604. } else {
  605. clk = read_clk(dev, clk_src_hclk) * 3;
  606. clk = calc_div(clk, perflvl->dom6, &P1);
  607. mast |= 0x0c000000;
  608. divs |= P1;
  609. }
  610. }
  611. /* vdec/dom6: complete switch to new clocks */
  612. switch (dev_priv->chipset) {
  613. case 0x92:
  614. case 0x94:
  615. case 0x96:
  616. hwsq_wr32(hwsq, 0x004800, divs);
  617. break;
  618. default:
  619. hwsq_wr32(hwsq, 0x004700, divs);
  620. break;
  621. }
  622. hwsq_wr32(hwsq, 0x00c040, mast);
  623. /* core/shader: make sure sclk/nvclk are disconnected from their
  624. * PLLs (nvclk to dom6, sclk to hclk)
  625. */
  626. if (dev_priv->chipset < 0x92)
  627. mast = (mast & ~0x001000b0) | 0x00100080;
  628. else
  629. mast = (mast & ~0x000000b3) | 0x00000081;
  630. hwsq_wr32(hwsq, 0x00c040, mast);
  631. /* core: for the moment at least, always use nvpll */
  632. clk = calc_pll(dev, 0x4028, &pll, perflvl->core, &N, &M, &P1);
  633. if (clk == 0)
  634. goto error;
  635. ctrl = nv_rd32(dev, 0x004028) & ~0xc03f0100;
  636. mast &= ~0x00100000;
  637. mast |= 3;
  638. hwsq_wr32(hwsq, 0x004028, 0x80000000 | (P1 << 19) | (P1 << 16) | ctrl);
  639. hwsq_wr32(hwsq, 0x00402c, (N << 8) | M);
  640. /* shader: tie to nvclk if possible, otherwise use spll. have to be
  641. * very careful that the shader clock is at least twice the core, or
  642. * some chipsets will be very unhappy. i expect most or all of these
  643. * cases will be handled by tying to nvclk, but it's possible there's
  644. * corners
  645. */
  646. ctrl = nv_rd32(dev, 0x004020) & ~0xc03f0100;
  647. if (P1-- && perflvl->shader == (perflvl->core << 1)) {
  648. hwsq_wr32(hwsq, 0x004020, (P1 << 19) | (P1 << 16) | ctrl);
  649. hwsq_wr32(hwsq, 0x00c040, 0x00000020 | mast);
  650. } else {
  651. clk = calc_pll(dev, 0x4020, &pll, perflvl->shader, &N, &M, &P1);
  652. if (clk == 0)
  653. goto error;
  654. ctrl |= 0x80000000;
  655. hwsq_wr32(hwsq, 0x004020, (P1 << 19) | (P1 << 16) | ctrl);
  656. hwsq_wr32(hwsq, 0x004024, (N << 8) | M);
  657. hwsq_wr32(hwsq, 0x00c040, 0x00000030 | mast);
  658. }
  659. hwsq_setf(hwsq, 0x10, 1); /* enable bus access */
  660. hwsq_op5f(hwsq, 0x00, 0x00); /* wait for access enabled? */
  661. hwsq_fini(hwsq);
  662. return info;
  663. error:
  664. kfree(info);
  665. return ERR_PTR(ret);
  666. }
  667. static int
  668. prog_hwsq(struct drm_device *dev, struct hwsq_ucode *hwsq)
  669. {
  670. struct drm_nouveau_private *dev_priv = dev->dev_private;
  671. u32 hwsq_data, hwsq_kick;
  672. int i;
  673. if (dev_priv->chipset < 0x94) {
  674. hwsq_data = 0x001400;
  675. hwsq_kick = 0x00000003;
  676. } else {
  677. hwsq_data = 0x080000;
  678. hwsq_kick = 0x00000001;
  679. }
  680. /* upload hwsq ucode */
  681. nv_mask(dev, 0x001098, 0x00000008, 0x00000000);
  682. nv_wr32(dev, 0x001304, 0x00000000);
  683. if (dev_priv->chipset >= 0x92)
  684. nv_wr32(dev, 0x001318, 0x00000000);
  685. for (i = 0; i < hwsq->len / 4; i++)
  686. nv_wr32(dev, hwsq_data + (i * 4), hwsq->ptr.u32[i]);
  687. nv_mask(dev, 0x001098, 0x00000018, 0x00000018);
  688. /* launch, and wait for completion */
  689. nv_wr32(dev, 0x00130c, hwsq_kick);
  690. if (!nv_wait(dev, 0x001308, 0x00000100, 0x00000000)) {
  691. NV_ERROR(dev, "hwsq ucode exec timed out\n");
  692. NV_ERROR(dev, "0x001308: 0x%08x\n", nv_rd32(dev, 0x001308));
  693. for (i = 0; i < hwsq->len / 4; i++) {
  694. NV_ERROR(dev, "0x%06x: 0x%08x\n", 0x1400 + (i * 4),
  695. nv_rd32(dev, 0x001400 + (i * 4)));
  696. }
  697. return -EIO;
  698. }
  699. return 0;
  700. }
  701. int
  702. nv50_pm_clocks_set(struct drm_device *dev, void *data)
  703. {
  704. struct nv50_pm_state *info = data;
  705. struct bit_entry M;
  706. int ret = -EBUSY;
  707. /* halt and idle execution engines */
  708. nv_mask(dev, 0x002504, 0x00000001, 0x00000001);
  709. if (!nv_wait(dev, 0x002504, 0x00000010, 0x00000010))
  710. goto resume;
  711. if (!nv_wait(dev, 0x00251c, 0x0000003f, 0x0000003f))
  712. goto resume;
  713. /* program memory clock, if necessary - must come before engine clock
  714. * reprogramming due to how we construct the hwsq scripts in pre()
  715. */
  716. if (info->mclk_hwsq.len) {
  717. /* execute some scripts that do ??? from the vbios.. */
  718. if (!bit_table(dev, 'M', &M) && M.version == 1) {
  719. if (M.length >= 6)
  720. nouveau_bios_init_exec(dev, ROM16(M.data[5]));
  721. if (M.length >= 8)
  722. nouveau_bios_init_exec(dev, ROM16(M.data[7]));
  723. if (M.length >= 10)
  724. nouveau_bios_init_exec(dev, ROM16(M.data[9]));
  725. nouveau_bios_init_exec(dev, info->mscript);
  726. }
  727. ret = prog_hwsq(dev, &info->mclk_hwsq);
  728. if (ret)
  729. goto resume;
  730. }
  731. /* program engine clocks */
  732. ret = prog_hwsq(dev, &info->eclk_hwsq);
  733. resume:
  734. nv_mask(dev, 0x002504, 0x00000001, 0x00000000);
  735. kfree(info);
  736. return ret;
  737. }
  738. static int
  739. pwm_info(struct drm_device *dev, int *line, int *ctrl, int *indx)
  740. {
  741. if (*line == 0x04) {
  742. *ctrl = 0x00e100;
  743. *line = 4;
  744. *indx = 0;
  745. } else
  746. if (*line == 0x09) {
  747. *ctrl = 0x00e100;
  748. *line = 9;
  749. *indx = 1;
  750. } else
  751. if (*line == 0x10) {
  752. *ctrl = 0x00e28c;
  753. *line = 0;
  754. *indx = 0;
  755. } else {
  756. NV_ERROR(dev, "unknown pwm ctrl for gpio %d\n", *line);
  757. return -ENODEV;
  758. }
  759. return 0;
  760. }
  761. int
  762. nv50_pm_pwm_get(struct drm_device *dev, int line, u32 *divs, u32 *duty)
  763. {
  764. int ctrl, id, ret = pwm_info(dev, &line, &ctrl, &id);
  765. if (ret)
  766. return ret;
  767. if (nv_rd32(dev, ctrl) & (1 << line)) {
  768. *divs = nv_rd32(dev, 0x00e114 + (id * 8));
  769. *duty = nv_rd32(dev, 0x00e118 + (id * 8));
  770. return 0;
  771. }
  772. return -EINVAL;
  773. }
  774. int
  775. nv50_pm_pwm_set(struct drm_device *dev, int line, u32 divs, u32 duty)
  776. {
  777. int ctrl, id, ret = pwm_info(dev, &line, &ctrl, &id);
  778. if (ret)
  779. return ret;
  780. nv_mask(dev, ctrl, 0x00010001 << line, 0x00000001 << line);
  781. nv_wr32(dev, 0x00e114 + (id * 8), divs);
  782. nv_wr32(dev, 0x00e118 + (id * 8), duty | 0x80000000);
  783. return 0;
  784. }