blob: 109e473fd5f873e946b3753b4dcf5fbc928501ff [file] [log] [blame]
Ben Skeggs02c30ca2010-09-16 16:17:35 +10001/*
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
25#include "drmP.h"
26#include "nouveau_drv.h"
Ben Skeggsaee582d2010-09-27 10:13:23 +100027#include "nouveau_bios.h"
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100028#include "nouveau_hw.h"
Ben Skeggs02c30ca2010-09-16 16:17:35 +100029#include "nouveau_pm.h"
Martin Pereseeb7a502011-11-07 23:38:50 +010030#include "nouveau_hwsq.h"
Ben Skeggse495d0d2012-01-23 13:22:58 +100031#include "nv50_display.h"
Ben Skeggs02c30ca2010-09-16 16:17:35 +100032
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100033enum clk_src {
34 clk_src_crystal,
35 clk_src_href,
36 clk_src_hclk,
37 clk_src_hclkm3,
38 clk_src_hclkm3d2,
39 clk_src_host,
40 clk_src_nvclk,
41 clk_src_sclk,
42 clk_src_mclk,
43 clk_src_vdec,
44 clk_src_dom6
Ben Skeggs02c30ca2010-09-16 16:17:35 +100045};
46
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100047static u32 read_clk(struct drm_device *, enum clk_src);
48
49static u32
50read_div(struct drm_device *dev)
Ben Skeggs02c30ca2010-09-16 16:17:35 +100051{
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100052 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs02c30ca2010-09-16 16:17:35 +100053
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100054 switch (dev_priv->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(dev, 0x004700);
61 case 0x92:
62 case 0x94:
63 case 0x96:
64 return nv_rd32(dev, 0x004800);
65 default:
66 return 0x00000000;
67 }
68}
Ben Skeggs02c30ca2010-09-16 16:17:35 +100069
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100070static u32
Ben Skeggs463464e2011-10-30 23:10:55 +100071read_pll_src(struct drm_device *dev, u32 base)
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100072{
73 struct drm_nouveau_private *dev_priv = dev->dev_private;
74 u32 coef, ref = read_clk(dev, clk_src_crystal);
75 u32 rsel = nv_rd32(dev, 0x00e18c);
76 int P, N, M, id;
Emil Velikov619d4f72011-04-11 20:43:23 +010077
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100078 switch (dev_priv->chipset) {
79 case 0x50:
80 case 0xa0:
81 switch (base) {
82 case 0x4020:
83 case 0x4028: id = !!(rsel & 0x00000004); break;
84 case 0x4008: id = !!(rsel & 0x00000008); break;
85 case 0x4030: id = 0; break;
86 default:
87 NV_ERROR(dev, "ref: bad pll 0x%06x\n", base);
88 return 0;
89 }
90
91 coef = nv_rd32(dev, 0x00e81c + (id * 0x0c));
92 ref *= (coef & 0x01000000) ? 2 : 4;
93 P = (coef & 0x00070000) >> 16;
94 N = ((coef & 0x0000ff00) >> 8) + 1;
95 M = ((coef & 0x000000ff) >> 0) + 1;
96 break;
97 case 0x84:
98 case 0x86:
99 case 0x92:
100 coef = nv_rd32(dev, 0x00e81c);
101 P = (coef & 0x00070000) >> 16;
102 N = (coef & 0x0000ff00) >> 8;
103 M = (coef & 0x000000ff) >> 0;
104 break;
105 case 0x94:
106 case 0x96:
107 case 0x98:
108 rsel = nv_rd32(dev, 0x00c050);
109 switch (base) {
110 case 0x4020: rsel = (rsel & 0x00000003) >> 0; break;
111 case 0x4008: rsel = (rsel & 0x0000000c) >> 2; break;
112 case 0x4028: rsel = (rsel & 0x00001800) >> 11; break;
113 case 0x4030: rsel = 3; break;
114 default:
115 NV_ERROR(dev, "ref: bad pll 0x%06x\n", base);
116 return 0;
117 }
118
119 switch (rsel) {
120 case 0: id = 1; break;
121 case 1: return read_clk(dev, clk_src_crystal);
122 case 2: return read_clk(dev, clk_src_href);
123 case 3: id = 0; break;
124 }
125
126 coef = nv_rd32(dev, 0x00e81c + (id * 0x28));
127 P = (nv_rd32(dev, 0x00e824 + (id * 0x28)) >> 16) & 7;
128 P += (coef & 0x00070000) >> 16;
129 N = (coef & 0x0000ff00) >> 8;
130 M = (coef & 0x000000ff) >> 0;
131 break;
132 default:
133 BUG_ON(1);
134 }
135
136 if (M)
137 return (ref * N / M) >> P;
138 return 0;
139}
140
141static u32
Ben Skeggs463464e2011-10-30 23:10:55 +1000142read_pll_ref(struct drm_device *dev, u32 base)
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000143{
Ben Skeggs463464e2011-10-30 23:10:55 +1000144 u32 src, mast = nv_rd32(dev, 0x00c040);
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000145
146 switch (base) {
147 case 0x004028:
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000148 src = !!(mast & 0x00200000);
149 break;
150 case 0x004020:
151 src = !!(mast & 0x00400000);
152 break;
153 case 0x004008:
154 src = !!(mast & 0x00010000);
155 break;
156 case 0x004030:
157 src = !!(mast & 0x02000000);
158 break;
159 case 0x00e810:
Ben Skeggs463464e2011-10-30 23:10:55 +1000160 return read_clk(dev, clk_src_crystal);
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000161 default:
162 NV_ERROR(dev, "bad pll 0x%06x\n", base);
163 return 0;
164 }
165
Ben Skeggs463464e2011-10-30 23:10:55 +1000166 if (src)
167 return read_clk(dev, clk_src_href);
168 return read_pll_src(dev, base);
169}
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000170
Ben Skeggs463464e2011-10-30 23:10:55 +1000171static u32
172read_pll(struct drm_device *dev, u32 base)
173{
174 struct drm_nouveau_private *dev_priv = dev->dev_private;
175 u32 mast = nv_rd32(dev, 0x00c040);
176 u32 ctrl = nv_rd32(dev, base + 0);
177 u32 coef = nv_rd32(dev, base + 4);
178 u32 ref = read_pll_ref(dev, base);
179 u32 clk = 0;
180 int N1, N2, M1, M2;
181
182 if (base == 0x004028 && (mast & 0x00100000)) {
183 /* wtf, appears to only disable post-divider on nva0 */
184 if (dev_priv->chipset != 0xa0)
185 return read_clk(dev, clk_src_dom6);
186 }
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000187
188 N2 = (coef & 0xff000000) >> 24;
189 M2 = (coef & 0x00ff0000) >> 16;
190 N1 = (coef & 0x0000ff00) >> 8;
191 M1 = (coef & 0x000000ff);
192 if ((ctrl & 0x80000000) && M1) {
193 clk = ref * N1 / M1;
194 if ((ctrl & 0x40000100) == 0x40000000) {
195 if (M2)
196 clk = clk * N2 / M2;
197 else
198 clk = 0;
Emil Velikov619d4f72011-04-11 20:43:23 +0100199 }
200 }
201
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000202 return clk;
203}
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000204
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000205static u32
206read_clk(struct drm_device *dev, enum clk_src src)
207{
208 struct drm_nouveau_private *dev_priv = dev->dev_private;
209 u32 mast = nv_rd32(dev, 0x00c040);
210 u32 P = 0;
211
212 switch (src) {
213 case clk_src_crystal:
214 return dev_priv->crystal;
215 case clk_src_href:
216 return 100000; /* PCIE reference clock */
217 case clk_src_hclk:
218 return read_clk(dev, clk_src_href) * 27778 / 10000;
219 case clk_src_hclkm3:
220 return read_clk(dev, clk_src_hclk) * 3;
221 case clk_src_hclkm3d2:
222 return read_clk(dev, clk_src_hclk) * 3 / 2;
223 case clk_src_host:
224 switch (mast & 0x30000000) {
225 case 0x00000000: return read_clk(dev, clk_src_href);
226 case 0x10000000: break;
227 case 0x20000000: /* !0x50 */
228 case 0x30000000: return read_clk(dev, clk_src_hclk);
229 }
230 break;
231 case clk_src_nvclk:
232 if (!(mast & 0x00100000))
233 P = (nv_rd32(dev, 0x004028) & 0x00070000) >> 16;
234 switch (mast & 0x00000003) {
235 case 0x00000000: return read_clk(dev, clk_src_crystal) >> P;
236 case 0x00000001: return read_clk(dev, clk_src_dom6);
237 case 0x00000002: return read_pll(dev, 0x004020) >> P;
238 case 0x00000003: return read_pll(dev, 0x004028) >> P;
239 }
240 break;
241 case clk_src_sclk:
242 P = (nv_rd32(dev, 0x004020) & 0x00070000) >> 16;
243 switch (mast & 0x00000030) {
244 case 0x00000000:
245 if (mast & 0x00000080)
246 return read_clk(dev, clk_src_host) >> P;
247 return read_clk(dev, clk_src_crystal) >> P;
248 case 0x00000010: break;
249 case 0x00000020: return read_pll(dev, 0x004028) >> P;
250 case 0x00000030: return read_pll(dev, 0x004020) >> P;
251 }
252 break;
253 case clk_src_mclk:
254 P = (nv_rd32(dev, 0x004008) & 0x00070000) >> 16;
255 if (nv_rd32(dev, 0x004008) & 0x00000200) {
256 switch (mast & 0x0000c000) {
257 case 0x00000000:
258 return read_clk(dev, clk_src_crystal) >> P;
259 case 0x00008000:
260 case 0x0000c000:
261 return read_clk(dev, clk_src_href) >> P;
262 }
263 } else {
264 return read_pll(dev, 0x004008) >> P;
265 }
266 break;
267 case clk_src_vdec:
268 P = (read_div(dev) & 0x00000700) >> 8;
269 switch (dev_priv->chipset) {
270 case 0x84:
271 case 0x86:
272 case 0x92:
273 case 0x94:
274 case 0x96:
275 case 0xa0:
276 switch (mast & 0x00000c00) {
277 case 0x00000000:
278 if (dev_priv->chipset == 0xa0) /* wtf?? */
279 return read_clk(dev, clk_src_nvclk) >> P;
280 return read_clk(dev, clk_src_crystal) >> P;
281 case 0x00000400:
282 return 0;
283 case 0x00000800:
284 if (mast & 0x01000000)
285 return read_pll(dev, 0x004028) >> P;
286 return read_pll(dev, 0x004030) >> P;
287 case 0x00000c00:
288 return read_clk(dev, clk_src_nvclk) >> P;
289 }
290 break;
291 case 0x98:
292 switch (mast & 0x00000c00) {
293 case 0x00000000:
294 return read_clk(dev, clk_src_nvclk) >> P;
295 case 0x00000400:
296 return 0;
297 case 0x00000800:
298 return read_clk(dev, clk_src_hclkm3d2) >> P;
299 case 0x00000c00:
Martin Peresd4676462011-11-01 11:38:16 +0100300 return read_clk(dev, clk_src_mclk) >> P;
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000301 }
302 break;
303 }
304 break;
305 case clk_src_dom6:
306 switch (dev_priv->chipset) {
307 case 0x50:
308 case 0xa0:
309 return read_pll(dev, 0x00e810) >> 2;
310 case 0x84:
311 case 0x86:
312 case 0x92:
313 case 0x94:
314 case 0x96:
315 case 0x98:
316 P = (read_div(dev) & 0x00000007) >> 0;
317 switch (mast & 0x0c000000) {
318 case 0x00000000: return read_clk(dev, clk_src_href);
319 case 0x04000000: break;
320 case 0x08000000: return read_clk(dev, clk_src_hclk);
321 case 0x0c000000:
322 return read_clk(dev, clk_src_hclkm3) >> P;
323 }
324 break;
325 default:
326 break;
327 }
328 default:
329 break;
330 }
331
332 NV_DEBUG(dev, "unknown clock source %d 0x%08x\n", src, mast);
333 return 0;
334}
335
336int
337nv50_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
338{
339 struct drm_nouveau_private *dev_priv = dev->dev_private;
340 if (dev_priv->chipset == 0xaa ||
341 dev_priv->chipset == 0xac)
342 return 0;
343
344 perflvl->core = read_clk(dev, clk_src_nvclk);
345 perflvl->shader = read_clk(dev, clk_src_sclk);
346 perflvl->memory = read_clk(dev, clk_src_mclk);
347 if (dev_priv->chipset != 0x50) {
348 perflvl->vdec = read_clk(dev, clk_src_vdec);
349 perflvl->dom6 = read_clk(dev, clk_src_dom6);
350 }
351
352 return 0;
353}
354
355struct nv50_pm_state {
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000356 struct nouveau_pm_level *perflvl;
357
Martin Pereseeb7a502011-11-07 23:38:50 +0100358 struct hwsq_ucode mclk_hwsq;
359 u32 mscript;
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000360 u32 mmast;
361 u32 mctrl;
362 u32 mcoef;
Martin Pereseeb7a502011-11-07 23:38:50 +0100363
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000364 u32 emast;
365 u32 nctrl;
366 u32 ncoef;
367 u32 sctrl;
368 u32 scoef;
369
370 u32 amast;
371 u32 pdivs;
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000372};
373
374static u32
375calc_pll(struct drm_device *dev, u32 reg, struct pll_lims *pll,
376 u32 clk, int *N1, int *M1, int *log2P)
377{
378 struct nouveau_pll_vals coef;
379 int ret;
380
381 ret = get_pll_limits(dev, reg, pll);
382 if (ret)
383 return 0;
384
385 pll->vco2.maxfreq = 0;
386 pll->refclk = read_pll_ref(dev, reg);
387 if (!pll->refclk)
388 return 0;
389
390 ret = nouveau_calc_pll_mnp(dev, pll, clk, &coef);
391 if (ret == 0)
392 return 0;
393
394 *N1 = coef.N1;
395 *M1 = coef.M1;
396 *log2P = coef.log2P;
397 return ret;
398}
399
400static inline u32
401calc_div(u32 src, u32 target, int *div)
402{
403 u32 clk0 = src, clk1 = src;
404 for (*div = 0; *div <= 7; (*div)++) {
405 if (clk0 <= target) {
406 clk1 = clk0 << (*div ? 1 : 0);
407 break;
408 }
409 clk0 >>= 1;
410 }
411
412 if (target - clk0 <= clk1 - target)
413 return clk0;
414 (*div)--;
415 return clk1;
416}
417
418static inline u32
419clk_same(u32 a, u32 b)
420{
421 return ((a / 1000) == (b / 1000));
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000422}
423
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000424static void
425mclk_precharge(struct nouveau_mem_exec_func *exec)
426{
427 struct nv50_pm_state *info = exec->priv;
428 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
429
430 hwsq_wr32(hwsq, 0x1002d4, 0x00000001);
431}
432
433static void
434mclk_refresh(struct nouveau_mem_exec_func *exec)
435{
436 struct nv50_pm_state *info = exec->priv;
437 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
438
439 hwsq_wr32(hwsq, 0x1002d0, 0x00000001);
440}
441
442static void
443mclk_refresh_auto(struct nouveau_mem_exec_func *exec, bool enable)
444{
445 struct nv50_pm_state *info = exec->priv;
446 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
447
448 hwsq_wr32(hwsq, 0x100210, enable ? 0x80000000 : 0x00000000);
449}
450
451static void
452mclk_refresh_self(struct nouveau_mem_exec_func *exec, bool enable)
453{
454 struct nv50_pm_state *info = exec->priv;
455 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
456
457 hwsq_wr32(hwsq, 0x1002dc, enable ? 0x00000001 : 0x00000000);
458}
459
460static void
461mclk_wait(struct nouveau_mem_exec_func *exec, u32 nsec)
462{
463 struct nv50_pm_state *info = exec->priv;
464 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
465
466 if (nsec > 1000)
467 hwsq_usec(hwsq, (nsec + 500) / 1000);
468}
469
470static u32
471mclk_mrg(struct nouveau_mem_exec_func *exec, int mr)
472{
473 if (mr <= 1)
474 return nv_rd32(exec->dev, 0x1002c0 + ((mr - 0) * 4));
475 if (mr <= 3)
476 return nv_rd32(exec->dev, 0x1002e0 + ((mr - 2) * 4));
477 return 0;
478}
479
480static void
481mclk_mrs(struct nouveau_mem_exec_func *exec, int mr, u32 data)
482{
483 struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
484 struct nv50_pm_state *info = exec->priv;
485 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
486
487 if (mr <= 1) {
488 if (dev_priv->vram_rank_B)
489 hwsq_wr32(hwsq, 0x1002c8 + ((mr - 0) * 4), data);
490 hwsq_wr32(hwsq, 0x1002c0 + ((mr - 0) * 4), data);
491 } else
492 if (mr <= 3) {
493 if (dev_priv->vram_rank_B)
494 hwsq_wr32(hwsq, 0x1002e8 + ((mr - 2) * 4), data);
495 hwsq_wr32(hwsq, 0x1002e0 + ((mr - 2) * 4), data);
496 }
497}
498
499static void
500mclk_clock_set(struct nouveau_mem_exec_func *exec)
501{
502 struct nv50_pm_state *info = exec->priv;
503 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
504 u32 ctrl = nv_rd32(exec->dev, 0x004008);
505
506 info->mmast = nv_rd32(exec->dev, 0x00c040);
507 info->mmast &= ~0xc0000000; /* get MCLK_2 from HREF */
508 info->mmast |= 0x0000c000; /* use MCLK_2 as MPLL_BYPASS clock */
509
510 hwsq_wr32(hwsq, 0xc040, info->mmast);
511 hwsq_wr32(hwsq, 0x4008, ctrl | 0x00000200); /* bypass MPLL */
512 if (info->mctrl & 0x80000000)
513 hwsq_wr32(hwsq, 0x400c, info->mcoef);
514 hwsq_wr32(hwsq, 0x4008, info->mctrl);
515}
516
517static void
518mclk_timing_set(struct nouveau_mem_exec_func *exec)
519{
520 struct drm_device *dev = exec->dev;
521 struct nv50_pm_state *info = exec->priv;
522 struct nouveau_pm_level *perflvl = info->perflvl;
523 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
524 int i;
525
526 for (i = 0; i < 9; i++) {
527 u32 reg = 0x100220 + (i * 4);
528 u32 val = nv_rd32(dev, reg);
529 if (val != perflvl->timing.reg[i])
530 hwsq_wr32(hwsq, reg, perflvl->timing.reg[i]);
531 }
532}
533
Martin Pereseeb7a502011-11-07 23:38:50 +0100534static int
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000535calc_mclk(struct drm_device *dev, struct nouveau_pm_level *perflvl,
536 struct nv50_pm_state *info)
Martin Pereseeb7a502011-11-07 23:38:50 +0100537{
538 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggse495d0d2012-01-23 13:22:58 +1000539 u32 crtc_mask = nv50_display_active_crtcs(dev);
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000540 struct nouveau_mem_exec_func exec = {
541 .dev = dev,
542 .precharge = mclk_precharge,
543 .refresh = mclk_refresh,
544 .refresh_auto = mclk_refresh_auto,
545 .refresh_self = mclk_refresh_self,
546 .wait = mclk_wait,
547 .mrg = mclk_mrg,
548 .mrs = mclk_mrs,
549 .clock_set = mclk_clock_set,
550 .timing_set = mclk_timing_set,
551 .priv = info
552 };
553 struct hwsq_ucode *hwsq = &info->mclk_hwsq;
Martin Pereseeb7a502011-11-07 23:38:50 +0100554 struct pll_lims pll;
Martin Pereseeb7a502011-11-07 23:38:50 +0100555 int N, M, P;
Ben Skeggse495d0d2012-01-23 13:22:58 +1000556 int ret;
Martin Pereseeb7a502011-11-07 23:38:50 +0100557
558 /* use pcie refclock if possible, otherwise use mpll */
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000559 info->mctrl = nv_rd32(dev, 0x004008);
560 info->mctrl &= ~0x81ff0200;
561 if (clk_same(perflvl->memory, read_clk(dev, clk_src_href))) {
562 info->mctrl |= 0x00000200 | (pll.log2p_bias << 19);
Martin Pereseeb7a502011-11-07 23:38:50 +0100563 } else {
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000564 ret = calc_pll(dev, 0x4008, &pll, perflvl->memory, &N, &M, &P);
Martin Pereseeb7a502011-11-07 23:38:50 +0100565 if (ret == 0)
566 return -EINVAL;
567
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000568 info->mctrl |= 0x80000000 | (P << 22) | (P << 16);
569 info->mctrl |= pll.log2p_bias << 19;
570 info->mcoef = (N << 8) | M;
Martin Pereseeb7a502011-11-07 23:38:50 +0100571 }
572
Martin Pereseeb7a502011-11-07 23:38:50 +0100573 /* build the ucode which will reclock the memory for us */
574 hwsq_init(hwsq);
575 if (crtc_mask) {
576 hwsq_op5f(hwsq, crtc_mask, 0x00); /* wait for scanout */
577 hwsq_op5f(hwsq, crtc_mask, 0x01); /* wait for vblank */
578 }
579 if (dev_priv->chipset >= 0x92)
580 hwsq_wr32(hwsq, 0x611200, 0x00003300); /* disable scanout */
Ben Skeggsc8b96412011-11-09 20:22:25 +1000581 hwsq_setf(hwsq, 0x10, 0); /* disable bus access */
Martin Pereseeb7a502011-11-07 23:38:50 +0100582 hwsq_op5f(hwsq, 0x00, 0x01); /* no idea :s */
583
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000584 ret = nouveau_mem_exec(&exec, perflvl);
585 if (ret)
586 return ret;
Martin Pereseeb7a502011-11-07 23:38:50 +0100587
Ben Skeggsc8b96412011-11-09 20:22:25 +1000588 hwsq_setf(hwsq, 0x10, 1); /* enable bus access */
Martin Pereseeb7a502011-11-07 23:38:50 +0100589 hwsq_op5f(hwsq, 0x00, 0x00); /* no idea, reverse of 0x00, 0x01? */
590 if (dev_priv->chipset >= 0x92)
591 hwsq_wr32(hwsq, 0x611200, 0x00003330); /* enable scanout */
592 hwsq_fini(hwsq);
593 return 0;
594}
595
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000596void *
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000597nv50_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000598{
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000599 struct drm_nouveau_private *dev_priv = dev->dev_private;
600 struct nv50_pm_state *info;
601 struct pll_lims pll;
Dan Carpentera9d99382012-01-04 10:20:47 +0300602 int clk, ret = -EINVAL;
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000603 int N, M, P1, P2;
Dan Carpentera9d99382012-01-04 10:20:47 +0300604 u32 out;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000605
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000606 if (dev_priv->chipset == 0xaa ||
607 dev_priv->chipset == 0xac)
608 return ERR_PTR(-ENODEV);
609
610 info = kmalloc(sizeof(*info), GFP_KERNEL);
611 if (!info)
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000612 return ERR_PTR(-ENOMEM);
Ben Skeggs6bdf68c2012-01-23 13:17:11 +1000613 info->perflvl = perflvl;
614
615 /* memory: build hwsq ucode which we'll use to reclock memory.
616 * use pcie refclock if possible, otherwise use mpll */
617 info->mclk_hwsq.len = 0;
618 if (perflvl->memory) {
619 ret = calc_mclk(dev, perflvl, info);
620 if (ret)
621 goto error;
622 info->mscript = perflvl->memscript;
623 }
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000624
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000625 /* core: for the moment at least, always use nvpll */
626 clk = calc_pll(dev, 0x4028, &pll, perflvl->core, &N, &M, &P1);
627 if (clk == 0)
628 goto error;
629
630 info->emast = 0x00000003;
631 info->nctrl = 0x80000000 | (P1 << 19) | (P1 << 16);
632 info->ncoef = (N << 8) | M;
633
634 /* shader: tie to nvclk if possible, otherwise use spll. have to be
635 * very careful that the shader clock is at least twice the core, or
636 * some chipsets will be very unhappy. i expect most or all of these
637 * cases will be handled by tying to nvclk, but it's possible there's
638 * corners
639 */
640 if (P1-- && perflvl->shader == (perflvl->core << 1)) {
641 info->emast |= 0x00000020;
642 info->sctrl = 0x00000000 | (P1 << 19) | (P1 << 16);
643 info->scoef = nv_rd32(dev, 0x004024);
644 } else {
645 clk = calc_pll(dev, 0x4020, &pll, perflvl->shader, &N, &M, &P1);
646 if (clk == 0)
647 goto error;
648
649 info->emast |= 0x00000030;
650 info->sctrl = 0x80000000 | (P1 << 19) | (P1 << 16);
651 info->scoef = (N << 8) | M;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000652 }
653
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000654 /* vdec: avoid modifying xpll until we know exactly how the other
655 * clock domains work, i suspect at least some of them can also be
656 * tied to xpll...
657 */
Ben Skeggs973e8612011-10-31 10:52:33 +1000658 info->amast = nv_rd32(dev, 0x00c040);
659 info->pdivs = read_div(dev);
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000660 if (perflvl->vdec) {
661 /* see how close we can get using nvclk as a source */
662 clk = calc_div(perflvl->core, perflvl->vdec, &P1);
663
664 /* see how close we can get using xpll/hclk as a source */
665 if (dev_priv->chipset != 0x98)
666 out = read_pll(dev, 0x004030);
667 else
668 out = read_clk(dev, clk_src_hclkm3d2);
669 out = calc_div(out, perflvl->vdec, &P2);
670
671 /* select whichever gets us closest */
Ben Skeggs973e8612011-10-31 10:52:33 +1000672 info->amast &= ~0x00000c00;
673 info->pdivs &= ~0x00000700;
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000674 if (abs((int)perflvl->vdec - clk) <=
675 abs((int)perflvl->vdec - out)) {
676 if (dev_priv->chipset != 0x98)
677 info->amast |= 0x00000c00;
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000678 info->pdivs |= P1 << 8;
679 } else {
680 info->amast |= 0x00000800;
681 info->pdivs |= P2 << 8;
682 }
683 }
684
685 /* dom6: nfi what this is, but we're limited to various combinations
686 * of the host clock frequency
687 */
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000688 if (perflvl->dom6) {
Ben Skeggs973e8612011-10-31 10:52:33 +1000689 info->amast &= ~0x0c000000;
690 if (clk_same(perflvl->dom6, read_clk(dev, clk_src_href))) {
691 info->amast |= 0x00000000;
692 } else
693 if (clk_same(perflvl->dom6, read_clk(dev, clk_src_hclk))) {
694 info->amast |= 0x08000000;
695 } else {
696 clk = read_clk(dev, clk_src_hclk) * 3;
697 clk = calc_div(clk, perflvl->dom6, &P1);
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000698
Ben Skeggs973e8612011-10-31 10:52:33 +1000699 info->amast |= 0x0c000000;
700 info->pdivs = (info->pdivs & ~0x00000007) | P1;
701 }
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000702 }
703
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000704 return info;
705error:
706 kfree(info);
707 return ERR_PTR(ret);
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000708}
709
Martin Pereseeb7a502011-11-07 23:38:50 +0100710static int
711prog_mclk(struct drm_device *dev, struct hwsq_ucode *hwsq)
712{
713 struct drm_nouveau_private *dev_priv = dev->dev_private;
714 u32 hwsq_data, hwsq_kick;
715 int i;
716
717 if (dev_priv->chipset < 0x90) {
718 hwsq_data = 0x001400;
719 hwsq_kick = 0x00000003;
720 } else {
721 hwsq_data = 0x080000;
722 hwsq_kick = 0x00000001;
723 }
724
725 /* upload hwsq ucode */
726 nv_mask(dev, 0x001098, 0x00000008, 0x00000000);
727 nv_wr32(dev, 0x001304, 0x00000000);
728 for (i = 0; i < hwsq->len / 4; i++)
729 nv_wr32(dev, hwsq_data + (i * 4), hwsq->ptr.u32[i]);
730 nv_mask(dev, 0x001098, 0x00000018, 0x00000018);
731
732 /* launch, and wait for completion */
733 nv_wr32(dev, 0x00130c, hwsq_kick);
734 if (!nv_wait(dev, 0x001308, 0x00000100, 0x00000000)) {
735 NV_ERROR(dev, "hwsq ucode exec timed out\n");
736 NV_ERROR(dev, "0x001308: 0x%08x\n", nv_rd32(dev, 0x001308));
737 for (i = 0; i < hwsq->len / 4; i++) {
738 NV_ERROR(dev, "0x%06x: 0x%08x\n", 0x1400 + (i * 4),
739 nv_rd32(dev, 0x001400 + (i * 4)));
740 }
741
742 return -EIO;
743 }
744
745 return 0;
746}
747
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000748int
749nv50_pm_clocks_set(struct drm_device *dev, void *data)
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000750{
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000751 struct drm_nouveau_private *dev_priv = dev->dev_private;
752 struct nv50_pm_state *info = data;
753 struct bit_entry M;
754 int ret = 0;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000755
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000756 /* halt and idle execution engines */
757 nv_mask(dev, 0x002504, 0x00000001, 0x00000001);
758 if (!nv_wait(dev, 0x002504, 0x00000010, 0x00000010))
759 goto error;
Ben Skeggsaee582d2010-09-27 10:13:23 +1000760
Martin Pereseeb7a502011-11-07 23:38:50 +0100761 /* memory: it is *very* important we change this first, the ucode
762 * we build in pre() now has hardcoded 0xc040 values, which can't
763 * change before we execute it or the engine clocks may end up
764 * messed up.
765 */
766 if (info->mclk_hwsq.len) {
767 /* execute some scripts that do ??? from the vbios.. */
768 if (!bit_table(dev, 'M', &M) && M.version == 1) {
769 if (M.length >= 6)
770 nouveau_bios_init_exec(dev, ROM16(M.data[5]));
771 if (M.length >= 8)
772 nouveau_bios_init_exec(dev, ROM16(M.data[7]));
773 if (M.length >= 10)
774 nouveau_bios_init_exec(dev, ROM16(M.data[9]));
775 nouveau_bios_init_exec(dev, info->mscript);
776 }
777
778 ret = prog_mclk(dev, &info->mclk_hwsq);
779 if (ret)
780 goto resume;
781 }
782
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000783 /* reclock vdec/dom6 */
784 nv_mask(dev, 0x00c040, 0x00000c00, 0x00000000);
785 switch (dev_priv->chipset) {
786 case 0x92:
787 case 0x94:
788 case 0x96:
789 nv_mask(dev, 0x004800, 0x00000707, info->pdivs);
790 break;
791 default:
792 nv_mask(dev, 0x004700, 0x00000707, info->pdivs);
793 break;
794 }
795 nv_mask(dev, 0x00c040, 0x0c000c00, info->amast);
796
Ben Skeggs68059792011-10-30 23:04:31 +1000797 /* core/shader: make sure sclk/nvclk are disconnected from their
798 * plls (nvclk to dom6, sclk to hclk), modify the plls, and
799 * reconnect sclk/nvclk to their new clock source
800 */
801 if (dev_priv->chipset < 0x92)
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000802 nv_mask(dev, 0x00c040, 0x001000b0, 0x00100080); /* grrr! */
803 else
804 nv_mask(dev, 0x00c040, 0x000000b3, 0x00000081);
805 nv_mask(dev, 0x004020, 0xc03f0100, info->sctrl);
806 nv_wr32(dev, 0x004024, info->scoef);
807 nv_mask(dev, 0x004028, 0xc03f0100, info->nctrl);
808 nv_wr32(dev, 0x00402c, info->ncoef);
809 nv_mask(dev, 0x00c040, 0x00100033, info->emast);
810
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000811 goto resume;
812error:
813 ret = -EBUSY;
814resume:
815 nv_mask(dev, 0x002504, 0x00000001, 0x00000000);
Ben Skeggs19fa2242011-10-28 22:10:15 +1000816 kfree(info);
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000817 return ret;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000818}
819
Ben Skeggscb9fa622011-08-14 12:43:47 +1000820static int
Ben Skeggs675aac02011-11-21 21:28:28 +1000821pwm_info(struct drm_device *dev, int *line, int *ctrl, int *indx)
Ben Skeggscb9fa622011-08-14 12:43:47 +1000822{
Ben Skeggs675aac02011-11-21 21:28:28 +1000823 if (*line == 0x04) {
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000824 *ctrl = 0x00e100;
825 *line = 4;
826 *indx = 0;
827 } else
Ben Skeggs675aac02011-11-21 21:28:28 +1000828 if (*line == 0x09) {
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000829 *ctrl = 0x00e100;
830 *line = 9;
831 *indx = 1;
832 } else
Ben Skeggs675aac02011-11-21 21:28:28 +1000833 if (*line == 0x10) {
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000834 *ctrl = 0x00e28c;
835 *line = 0;
836 *indx = 0;
837 } else {
Ben Skeggs675aac02011-11-21 21:28:28 +1000838 NV_ERROR(dev, "unknown pwm ctrl for gpio %d\n", *line);
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000839 return -ENODEV;
Ben Skeggscb9fa622011-08-14 12:43:47 +1000840 }
841
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000842 return 0;
Ben Skeggscb9fa622011-08-14 12:43:47 +1000843}
844
845int
Ben Skeggs675aac02011-11-21 21:28:28 +1000846nv50_pm_pwm_get(struct drm_device *dev, int line, u32 *divs, u32 *duty)
Ben Skeggscb9fa622011-08-14 12:43:47 +1000847{
Ben Skeggs675aac02011-11-21 21:28:28 +1000848 int ctrl, id, ret = pwm_info(dev, &line, &ctrl, &id);
Ben Skeggscb9fa622011-08-14 12:43:47 +1000849 if (ret)
850 return ret;
851
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000852 if (nv_rd32(dev, ctrl) & (1 << line)) {
853 *divs = nv_rd32(dev, 0x00e114 + (id * 8));
854 *duty = nv_rd32(dev, 0x00e118 + (id * 8));
Ben Skeggscb9fa622011-08-14 12:43:47 +1000855 return 0;
856 }
857
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000858 return -EINVAL;
Ben Skeggscb9fa622011-08-14 12:43:47 +1000859}
860
861int
Ben Skeggs675aac02011-11-21 21:28:28 +1000862nv50_pm_pwm_set(struct drm_device *dev, int line, u32 divs, u32 duty)
Ben Skeggscb9fa622011-08-14 12:43:47 +1000863{
Ben Skeggs675aac02011-11-21 21:28:28 +1000864 int ctrl, id, ret = pwm_info(dev, &line, &ctrl, &id);
Ben Skeggscb9fa622011-08-14 12:43:47 +1000865 if (ret)
866 return ret;
867
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000868 nv_mask(dev, ctrl, 0x00010001 << line, 0x00000001 << line);
869 nv_wr32(dev, 0x00e114 + (id * 8), divs);
870 nv_wr32(dev, 0x00e118 + (id * 8), duty | 0x80000000);
Ben Skeggscb9fa622011-08-14 12:43:47 +1000871 return 0;
872}