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