nv50_pm.c 22 KB

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