2 * Copyright 2011 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #include "nouveau_drm.h"
26 #include "nouveau_bios.h"
27 #include "nouveau_pm.h"
29 #include <subdev/bios/pll.h>
30 #include <subdev/bios.h>
31 #include <subdev/clock.h>
32 #include <subdev/timer.h>
33 #include <subdev/fb.h>
35 static u32
read_div(struct drm_device
*, int, u32
, u32
);
36 static u32
read_pll(struct drm_device
*, u32
);
39 read_vco(struct drm_device
*dev
, u32 dsrc
)
41 struct nouveau_device
*device
= nouveau_dev(dev
);
42 u32 ssrc
= nv_rd32(device
, dsrc
);
43 if (!(ssrc
& 0x00000100))
44 return read_pll(dev
, 0x00e800);
45 return read_pll(dev
, 0x00e820);
49 read_pll(struct drm_device
*dev
, u32 pll
)
51 struct nouveau_device
*device
= nouveau_dev(dev
);
52 u32 ctrl
= nv_rd32(device
, pll
+ 0);
53 u32 coef
= nv_rd32(device
, pll
+ 4);
54 u32 P
= (coef
& 0x003f0000) >> 16;
55 u32 N
= (coef
& 0x0000ff00) >> 8;
56 u32 M
= (coef
& 0x000000ff) >> 0;
59 if (!(ctrl
& 0x00000001))
62 switch (pll
& 0xfff000) {
68 doff
= (pll
- 0x137000) / 0x20;
69 sclk
= read_div(dev
, doff
, 0x137120, 0x137140);
74 sclk
= read_pll(dev
, 0x132020);
77 sclk
= read_div(dev
, 0, 0x137320, 0x137330);
87 return sclk
* N
/ M
/ P
;
91 read_div(struct drm_device
*dev
, int doff
, u32 dsrc
, u32 dctl
)
93 struct nouveau_device
*device
= nouveau_dev(dev
);
94 u32 ssrc
= nv_rd32(device
, dsrc
+ (doff
* 4));
95 u32 sctl
= nv_rd32(device
, dctl
+ (doff
* 4));
97 switch (ssrc
& 0x00000003) {
99 if ((ssrc
& 0x00030000) != 0x00030000)
105 if (sctl
& 0x80000000) {
106 u32 sclk
= read_vco(dev
, dsrc
+ (doff
* 4));
107 u32 sdiv
= (sctl
& 0x0000003f) + 2;
108 return (sclk
* 2) / sdiv
;
111 return read_vco(dev
, dsrc
+ (doff
* 4));
118 read_mem(struct drm_device
*dev
)
120 struct nouveau_device
*device
= nouveau_dev(dev
);
121 u32 ssel
= nv_rd32(device
, 0x1373f0);
122 if (ssel
& 0x00000001)
123 return read_div(dev
, 0, 0x137300, 0x137310);
124 return read_pll(dev
, 0x132000);
128 read_clk(struct drm_device
*dev
, int clk
)
130 struct nouveau_device
*device
= nouveau_dev(dev
);
131 u32 sctl
= nv_rd32(device
, 0x137250 + (clk
* 4));
132 u32 ssel
= nv_rd32(device
, 0x137100);
135 if (ssel
& (1 << clk
)) {
137 sclk
= read_pll(dev
, 0x137000 + (clk
* 0x20));
139 sclk
= read_pll(dev
, 0x1370e0);
140 sdiv
= ((sctl
& 0x00003f00) >> 8) + 2;
142 sclk
= read_div(dev
, clk
, 0x137160, 0x1371d0);
143 sdiv
= ((sctl
& 0x0000003f) >> 0) + 2;
146 if (sctl
& 0x80000000)
147 return (sclk
* 2) / sdiv
;
152 nvc0_pm_clocks_get(struct drm_device
*dev
, struct nouveau_pm_level
*perflvl
)
154 perflvl
->shader
= read_clk(dev
, 0x00);
155 perflvl
->core
= perflvl
->shader
/ 2;
156 perflvl
->memory
= read_mem(dev
);
157 perflvl
->rop
= read_clk(dev
, 0x01);
158 perflvl
->hub07
= read_clk(dev
, 0x02);
159 perflvl
->hub06
= read_clk(dev
, 0x07);
160 perflvl
->hub01
= read_clk(dev
, 0x08);
161 perflvl
->copy
= read_clk(dev
, 0x09);
162 perflvl
->daemon
= read_clk(dev
, 0x0c);
163 perflvl
->vdec
= read_clk(dev
, 0x0e);
167 struct nvc0_pm_clock
{
176 struct nvc0_pm_state
{
177 struct nouveau_pm_level
*perflvl
;
178 struct nvc0_pm_clock eng
[16];
179 struct nvc0_pm_clock mem
;
183 calc_div(struct drm_device
*dev
, int clk
, u32 ref
, u32 freq
, u32
*ddiv
)
185 u32 div
= min((ref
* 2) / freq
, (u32
)65);
190 return (ref
* 2) / div
;
194 calc_src(struct drm_device
*dev
, int clk
, u32 freq
, u32
*dsrc
, u32
*ddiv
)
198 /* use one of the fixed frequencies if possible */
215 /* otherwise, calculate the closest divider */
216 sclk
= read_vco(dev
, clk
);
218 sclk
= calc_div(dev
, clk
, sclk
, freq
, ddiv
);
223 calc_pll(struct drm_device
*dev
, int clk
, u32 freq
, u32
*coef
)
225 struct nouveau_device
*device
= nouveau_dev(dev
);
226 struct nouveau_bios
*bios
= nouveau_bios(device
);
227 struct nvbios_pll limits
;
230 ret
= nvbios_pll_parse(bios
, 0x137000 + (clk
* 0x20), &limits
);
234 limits
.refclk
= read_div(dev
, clk
, 0x137120, 0x137140);
238 ret
= nva3_calc_pll(dev
, &limits
, freq
, &N
, NULL
, &M
, &P
);
242 *coef
= (P
<< 16) | (N
<< 8) | M
;
246 /* A (likely rather simplified and incomplete) view of the clock tree
257 * 137250(D) ---- 137100(F0) ---- 137160(S)/1371d0(D) ------------------- ref
258 * (F1) ---- 1370X0(P) ---- 137120(S)/137140(D) ---- ref
260 * Not all registers exist for all clocks. For example: clocks >= 8 don't
261 * have their own PLL (all tied to clock 7's PLL when in PLL mode), nor do
262 * they have the divider at 1371d0, though the source selection at 137160
263 * still exists. You must use the divider at 137250 for these instead.
267 * TBD, read_mem() above is likely very wrong...
272 calc_clk(struct drm_device
*dev
, int clk
, struct nvc0_pm_clock
*info
, u32 freq
)
274 u32 src0
, div0
, div1D
, div1P
= 0;
277 /* invalid clock domain */
281 /* first possible path, using only dividers */
282 clk0
= calc_src(dev
, clk
, freq
, &src0
, &div0
);
283 clk0
= calc_div(dev
, clk
, clk0
, freq
, &div1D
);
285 /* see if we can get any closer using PLLs */
286 if (clk0
!= freq
&& (0x00004387 & (1 << clk
))) {
288 clk1
= calc_pll(dev
, clk
, freq
, &info
->coef
);
290 clk1
= read_pll(dev
, 0x1370e0);
291 clk1
= calc_div(dev
, clk
, clk1
, freq
, &div1P
);
294 /* select the method which gets closest to target freq */
295 if (abs((int)freq
- clk0
) <= abs((int)freq
- clk1
)) {
298 info
->ddiv
|= 0x80000000;
299 info
->ddiv
|= div0
<< 8;
303 info
->mdiv
|= 0x80000000;
310 info
->mdiv
|= 0x80000000;
311 info
->mdiv
|= div1P
<< 8;
313 info
->ssel
= (1 << clk
);
321 calc_mem(struct drm_device
*dev
, struct nvc0_pm_clock
*info
, u32 freq
)
323 struct nouveau_device
*device
= nouveau_dev(dev
);
324 struct nouveau_bios
*bios
= nouveau_bios(device
);
325 struct nvbios_pll pll
;
329 /* mclk pll input freq comes from another pll, make sure it's on */
330 ctrl
= nv_rd32(device
, 0x132020);
331 if (!(ctrl
& 0x00000001)) {
332 /* if not, program it to 567MHz. nfi where this value comes
333 * from - it looks like it's in the pll limits table for
334 * 132000 but the binary driver ignores all my attempts to
337 nv_wr32(device
, 0x137320, 0x00000103);
338 nv_wr32(device
, 0x137330, 0x81200606);
339 nv_wait(device
, 0x132020, 0x00010000, 0x00010000);
340 nv_wr32(device
, 0x132024, 0x0001150f);
341 nv_mask(device
, 0x132020, 0x00000001, 0x00000001);
342 nv_wait(device
, 0x137390, 0x00020000, 0x00020000);
343 nv_mask(device
, 0x132020, 0x00000004, 0x00000004);
346 /* for the moment, until the clock tree is better understood, use
347 * pll mode for all clock frequencies
349 ret
= nvbios_pll_parse(bios
, 0x132000, &pll
);
351 pll
.refclk
= read_pll(dev
, 0x132020);
353 ret
= nva3_calc_pll(dev
, &pll
, freq
, &N
, NULL
, &M
, &P
);
355 info
->coef
= (P
<< 16) | (N
<< 8) | M
;
365 nvc0_pm_clocks_pre(struct drm_device
*dev
, struct nouveau_pm_level
*perflvl
)
367 struct nouveau_device
*device
= nouveau_dev(dev
);
368 struct nvc0_pm_state
*info
;
371 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
373 return ERR_PTR(-ENOMEM
);
375 /* NFI why this is still in the performance table, the ROPCs appear
376 * to get their clock from clock 2 ("hub07", actually hub05 on this
377 * chip, but, anyway...) as well. nvatiming confirms hub05 and ROP
378 * are always the same freq with the binary driver even when the
379 * performance table says they should differ.
381 if (device
->chipset
== 0xd9)
384 if ((ret
= calc_clk(dev
, 0x00, &info
->eng
[0x00], perflvl
->shader
)) ||
385 (ret
= calc_clk(dev
, 0x01, &info
->eng
[0x01], perflvl
->rop
)) ||
386 (ret
= calc_clk(dev
, 0x02, &info
->eng
[0x02], perflvl
->hub07
)) ||
387 (ret
= calc_clk(dev
, 0x07, &info
->eng
[0x07], perflvl
->hub06
)) ||
388 (ret
= calc_clk(dev
, 0x08, &info
->eng
[0x08], perflvl
->hub01
)) ||
389 (ret
= calc_clk(dev
, 0x09, &info
->eng
[0x09], perflvl
->copy
)) ||
390 (ret
= calc_clk(dev
, 0x0c, &info
->eng
[0x0c], perflvl
->daemon
)) ||
391 (ret
= calc_clk(dev
, 0x0e, &info
->eng
[0x0e], perflvl
->vdec
))) {
396 if (perflvl
->memory
) {
397 ret
= calc_mem(dev
, &info
->mem
, perflvl
->memory
);
404 info
->perflvl
= perflvl
;
409 prog_clk(struct drm_device
*dev
, int clk
, struct nvc0_pm_clock
*info
)
411 struct nouveau_device
*device
= nouveau_dev(dev
);
413 /* program dividers at 137160/1371d0 first */
414 if (clk
< 7 && !info
->ssel
) {
415 nv_mask(device
, 0x1371d0 + (clk
* 0x04), 0x80003f3f, info
->ddiv
);
416 nv_wr32(device
, 0x137160 + (clk
* 0x04), info
->dsrc
);
419 /* switch clock to non-pll mode */
420 nv_mask(device
, 0x137100, (1 << clk
), 0x00000000);
421 nv_wait(device
, 0x137100, (1 << clk
), 0x00000000);
425 /* make sure it's disabled first... */
426 u32 base
= 0x137000 + (clk
* 0x20);
427 u32 ctrl
= nv_rd32(device
, base
+ 0x00);
428 if (ctrl
& 0x00000001) {
429 nv_mask(device
, base
+ 0x00, 0x00000004, 0x00000000);
430 nv_mask(device
, base
+ 0x00, 0x00000001, 0x00000000);
432 /* program it to new values, if necessary */
434 nv_wr32(device
, base
+ 0x04, info
->coef
);
435 nv_mask(device
, base
+ 0x00, 0x00000001, 0x00000001);
436 nv_wait(device
, base
+ 0x00, 0x00020000, 0x00020000);
437 nv_mask(device
, base
+ 0x00, 0x00020004, 0x00000004);
441 /* select pll/non-pll mode, and program final clock divider */
442 nv_mask(device
, 0x137100, (1 << clk
), info
->ssel
);
443 nv_wait(device
, 0x137100, (1 << clk
), info
->ssel
);
444 nv_mask(device
, 0x137250 + (clk
* 0x04), 0x00003f3f, info
->mdiv
);
448 mclk_precharge(struct nouveau_mem_exec_func
*exec
)
453 mclk_refresh(struct nouveau_mem_exec_func
*exec
)
458 mclk_refresh_auto(struct nouveau_mem_exec_func
*exec
, bool enable
)
460 struct nouveau_device
*device
= nouveau_dev(exec
->dev
);
461 nv_wr32(device
, 0x10f210, enable
? 0x80000000 : 0x00000000);
465 mclk_refresh_self(struct nouveau_mem_exec_func
*exec
, bool enable
)
470 mclk_wait(struct nouveau_mem_exec_func
*exec
, u32 nsec
)
472 udelay((nsec
+ 500) / 1000);
476 mclk_mrg(struct nouveau_mem_exec_func
*exec
, int mr
)
478 struct nouveau_device
*device
= nouveau_dev(exec
->dev
);
479 struct nouveau_fb
*pfb
= nouveau_fb(device
);
480 if (pfb
->ram
->type
!= NV_MEM_TYPE_GDDR5
) {
482 return nv_rd32(device
, 0x10f300 + ((mr
- 0) * 4));
483 return nv_rd32(device
, 0x10f320 + ((mr
- 2) * 4));
486 return nv_rd32(device
, 0x10f300 + (mr
* 4));
489 return nv_rd32(device
, 0x10f32c + (mr
* 4));
490 return nv_rd32(device
, 0x10f34c);
495 mclk_mrs(struct nouveau_mem_exec_func
*exec
, int mr
, u32 data
)
497 struct nouveau_device
*device
= nouveau_dev(exec
->dev
);
498 struct nouveau_fb
*pfb
= nouveau_fb(device
);
499 if (pfb
->ram
->type
!= NV_MEM_TYPE_GDDR5
) {
501 nv_wr32(device
, 0x10f300 + ((mr
- 0) * 4), data
);
502 if (pfb
->ram
->ranks
> 1)
503 nv_wr32(device
, 0x10f308 + ((mr
- 0) * 4), data
);
506 nv_wr32(device
, 0x10f320 + ((mr
- 2) * 4), data
);
507 if (pfb
->ram
->ranks
> 1)
508 nv_wr32(device
, 0x10f328 + ((mr
- 2) * 4), data
);
511 if (mr
== 0) nv_wr32(device
, 0x10f300 + (mr
* 4), data
);
512 else if (mr
<= 7) nv_wr32(device
, 0x10f32c + (mr
* 4), data
);
513 else if (mr
== 15) nv_wr32(device
, 0x10f34c, data
);
518 mclk_clock_set(struct nouveau_mem_exec_func
*exec
)
520 struct nouveau_device
*device
= nouveau_dev(exec
->dev
);
521 struct nvc0_pm_state
*info
= exec
->priv
;
522 u32 ctrl
= nv_rd32(device
, 0x132000);
524 nv_wr32(device
, 0x137360, 0x00000001);
525 nv_wr32(device
, 0x137370, 0x00000000);
526 nv_wr32(device
, 0x137380, 0x00000000);
527 if (ctrl
& 0x00000001)
528 nv_wr32(device
, 0x132000, (ctrl
&= ~0x00000001));
530 nv_wr32(device
, 0x132004, info
->mem
.coef
);
531 nv_wr32(device
, 0x132000, (ctrl
|= 0x00000001));
532 nv_wait(device
, 0x137390, 0x00000002, 0x00000002);
533 nv_wr32(device
, 0x132018, 0x00005000);
535 nv_wr32(device
, 0x137370, 0x00000001);
536 nv_wr32(device
, 0x137380, 0x00000001);
537 nv_wr32(device
, 0x137360, 0x00000000);
541 mclk_timing_set(struct nouveau_mem_exec_func
*exec
)
543 struct nouveau_device
*device
= nouveau_dev(exec
->dev
);
544 struct nvc0_pm_state
*info
= exec
->priv
;
545 struct nouveau_pm_level
*perflvl
= info
->perflvl
;
548 for (i
= 0; i
< 5; i
++)
549 nv_wr32(device
, 0x10f290 + (i
* 4), perflvl
->timing
.reg
[i
]);
553 prog_mem(struct drm_device
*dev
, struct nvc0_pm_state
*info
)
555 struct nouveau_device
*device
= nouveau_dev(dev
);
556 struct nouveau_mem_exec_func exec
= {
558 .precharge
= mclk_precharge
,
559 .refresh
= mclk_refresh
,
560 .refresh_auto
= mclk_refresh_auto
,
561 .refresh_self
= mclk_refresh_self
,
565 .clock_set
= mclk_clock_set
,
566 .timing_set
= mclk_timing_set
,
570 if (device
->chipset
< 0xd0)
571 nv_wr32(device
, 0x611200, 0x00003300);
573 nv_wr32(device
, 0x62c000, 0x03030000);
575 nouveau_mem_exec(&exec
, info
->perflvl
);
577 if (device
->chipset
< 0xd0)
578 nv_wr32(device
, 0x611200, 0x00003330);
580 nv_wr32(device
, 0x62c000, 0x03030300);
583 nvc0_pm_clocks_set(struct drm_device
*dev
, void *data
)
585 struct nvc0_pm_state
*info
= data
;
591 for (i
= 0; i
< 16; i
++) {
592 if (!info
->eng
[i
].freq
)
594 prog_clk(dev
, i
, &info
->eng
[i
]);