summaryrefslogtreecommitdiff
path: root/src/radeon_pm.c
blob: bf8fecfc19da87dd8a420faf6f06f5e1f38f7e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
 * Copyright 2009 Advanced Micro Devices, Inc.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR
 * AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Author: Alex Deucher <alexander.deucher@amd.com>
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

				/* Driver data structures */
#include "radeon.h"
#include "radeon_reg.h"
#include "radeon_macros.h"
#include "radeon_atombios.h"

static int calc_div(uint32_t num, uint32_t den)
{
    int div = (num + (den / 2)) / den;

    if ((div < 2) || (div > 0xff))
	return 0;
    else
	return div;

}

/* 10 khz */
static void
RADEONSetEngineClock(ScrnInfoPtr pScrn, uint32_t eng_clock)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);
    RADEONPLLPtr pll = &info->pll;
    uint32_t ref_div, fb_div;
    uint32_t m_spll_ref_fb_div;

    RADEONWaitForIdleMMIO(pScrn);

    m_spll_ref_fb_div = INPLL(pScrn, RADEON_M_SPLL_REF_FB_DIV);
    ref_div = m_spll_ref_fb_div & RADEON_M_SPLL_REF_DIV_MASK;
    fb_div = calc_div(eng_clock * ref_div, pll->reference_freq);

    if (!fb_div)
	return;

    m_spll_ref_fb_div &= ~(RADEON_SPLL_FB_DIV_MASK << RADEON_SPLL_FB_DIV_SHIFT);
    m_spll_ref_fb_div |= (fb_div & RADEON_SPLL_FB_DIV_MASK) << RADEON_SPLL_FB_DIV_SHIFT;
    OUTPLL(pScrn, RADEON_M_SPLL_REF_FB_DIV, m_spll_ref_fb_div);
    usleep(16000); /* Let the pll settle */
}

/* 10 khz */
static void
RADEONSetMemoryClock(ScrnInfoPtr pScrn, uint32_t mem_clock)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);
    RADEONPLLPtr pll = &info->pll;
    uint32_t ref_div, fb_div;
    uint32_t m_spll_ref_fb_div;

    if (info->IsIGP)
	return;

    RADEONWaitForIdleMMIO(pScrn);

    m_spll_ref_fb_div = INPLL(pScrn, RADEON_M_SPLL_REF_FB_DIV);
    ref_div = m_spll_ref_fb_div & RADEON_M_SPLL_REF_DIV_MASK;
    fb_div = calc_div(mem_clock * ref_div, pll->reference_freq);

    if (!fb_div)
	return;

    m_spll_ref_fb_div &= ~(RADEON_MPLL_FB_DIV_MASK << RADEON_MPLL_FB_DIV_SHIFT);
    m_spll_ref_fb_div |= (fb_div & RADEON_MPLL_FB_DIV_MASK) << RADEON_MPLL_FB_DIV_SHIFT;
    OUTPLL(pScrn, RADEON_M_SPLL_REF_FB_DIV, m_spll_ref_fb_div);
    usleep(16000); /* Let the pll settle */
}

static void LegacySetClockGating(ScrnInfoPtr pScrn, Bool enable)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);
    RADEONEntPtr pRADEONEnt = RADEONEntPriv(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    uint32_t tmp;

    if (enable) {
	if (!pRADEONEnt->HasCRTC2) {
	    tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
	    if ((INREG(RADEON_CONFIG_CNTL) & RADEON_CFG_ATI_REV_ID_MASK) >
		RADEON_CFG_ATI_REV_A13) {
		tmp &= ~(RADEON_SCLK_FORCE_CP | RADEON_SCLK_FORCE_RB);
	    }
	    tmp &= ~(RADEON_SCLK_FORCE_HDP  | RADEON_SCLK_FORCE_DISP1 |
		     RADEON_SCLK_FORCE_TOP  | RADEON_SCLK_FORCE_SE   |
		     RADEON_SCLK_FORCE_IDCT | RADEON_SCLK_FORCE_RE   |
		     RADEON_SCLK_FORCE_PB   | RADEON_SCLK_FORCE_TAM  |
		     RADEON_SCLK_FORCE_TDM);
	    OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);
	} else if (IS_R300_VARIANT) {
	    if ((info->ChipFamily == CHIP_FAMILY_RS400) ||
		(info->ChipFamily == CHIP_FAMILY_RS480)) {
		tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
		tmp &= ~(RADEON_SCLK_FORCE_DISP2 | RADEON_SCLK_FORCE_CP      |
			 RADEON_SCLK_FORCE_HDP   | RADEON_SCLK_FORCE_DISP1   |
			 RADEON_SCLK_FORCE_TOP   | RADEON_SCLK_FORCE_E2      |
			 R300_SCLK_FORCE_VAP     | RADEON_SCLK_FORCE_IDCT    |
			 RADEON_SCLK_FORCE_VIP   | R300_SCLK_FORCE_SR        |
			 R300_SCLK_FORCE_PX      | R300_SCLK_FORCE_TX        |
			 R300_SCLK_FORCE_US      | RADEON_SCLK_FORCE_TV_SCLK |
			 R300_SCLK_FORCE_SU      | RADEON_SCLK_FORCE_OV0);
		tmp |=  RADEON_DYN_STOP_LAT_MASK;
		tmp |= RADEON_SCLK_FORCE_TOP | RADEON_SCLK_FORCE_VIP;
		OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);

		tmp = INPLL(pScrn, RADEON_SCLK_MORE_CNTL);
		tmp &= ~RADEON_SCLK_MORE_FORCEON;
		tmp |=  RADEON_SCLK_MORE_MAX_DYN_STOP_LAT;
		OUTPLL(pScrn, RADEON_SCLK_MORE_CNTL, tmp);

		tmp = INPLL(pScrn, RADEON_VCLK_ECP_CNTL);
		tmp |= (RADEON_PIXCLK_ALWAYS_ONb |
			RADEON_PIXCLK_DAC_ALWAYS_ONb);
		OUTPLL(pScrn, RADEON_VCLK_ECP_CNTL, tmp);

		tmp = INPLL(pScrn, RADEON_PIXCLKS_CNTL);
		tmp |= (RADEON_PIX2CLK_ALWAYS_ONb         |
			RADEON_PIX2CLK_DAC_ALWAYS_ONb     |
			RADEON_DISP_TVOUT_PIXCLK_TV_ALWAYS_ONb |
			R300_DVOCLK_ALWAYS_ONb            |
			RADEON_PIXCLK_BLEND_ALWAYS_ONb    |
			RADEON_PIXCLK_GV_ALWAYS_ONb       |
			R300_PIXCLK_DVO_ALWAYS_ONb        |
			RADEON_PIXCLK_LVDS_ALWAYS_ONb     |
			RADEON_PIXCLK_TMDS_ALWAYS_ONb     |
			R300_PIXCLK_TRANS_ALWAYS_ONb      |
			R300_PIXCLK_TVO_ALWAYS_ONb        |
			R300_P2G2CLK_ALWAYS_ONb           |
			R300_P2G2CLK_ALWAYS_ONb);
		OUTPLL(pScrn, RADEON_PIXCLKS_CNTL, tmp);
	    } else if (info->ChipFamily >= CHIP_FAMILY_RV350) {
		tmp = INPLL(pScrn, R300_SCLK_CNTL2);
		tmp &= ~(R300_SCLK_FORCE_TCL |
			 R300_SCLK_FORCE_GA  |
			 R300_SCLK_FORCE_CBA);
		tmp |=  (R300_SCLK_TCL_MAX_DYN_STOP_LAT |
			 R300_SCLK_GA_MAX_DYN_STOP_LAT  |
			 R300_SCLK_CBA_MAX_DYN_STOP_LAT);
		OUTPLL(pScrn, R300_SCLK_CNTL2, tmp);

		tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
		tmp &= ~(RADEON_SCLK_FORCE_DISP2 | RADEON_SCLK_FORCE_CP      |
			 RADEON_SCLK_FORCE_HDP   | RADEON_SCLK_FORCE_DISP1   |
			 RADEON_SCLK_FORCE_TOP   | RADEON_SCLK_FORCE_E2      |
			 R300_SCLK_FORCE_VAP     | RADEON_SCLK_FORCE_IDCT    |
			 RADEON_SCLK_FORCE_VIP   | R300_SCLK_FORCE_SR        |
			 R300_SCLK_FORCE_PX      | R300_SCLK_FORCE_TX        |
			 R300_SCLK_FORCE_US      | RADEON_SCLK_FORCE_TV_SCLK |
			 R300_SCLK_FORCE_SU      | RADEON_SCLK_FORCE_OV0);
		tmp |=  RADEON_DYN_STOP_LAT_MASK;
		OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);

		tmp = INPLL(pScrn, RADEON_SCLK_MORE_CNTL);
		tmp &= ~RADEON_SCLK_MORE_FORCEON;
		tmp |=  RADEON_SCLK_MORE_MAX_DYN_STOP_LAT;
		OUTPLL(pScrn, RADEON_SCLK_MORE_CNTL, tmp);

		tmp = INPLL(pScrn, RADEON_VCLK_ECP_CNTL);
		tmp |= (RADEON_PIXCLK_ALWAYS_ONb |
			RADEON_PIXCLK_DAC_ALWAYS_ONb);
		OUTPLL(pScrn, RADEON_VCLK_ECP_CNTL, tmp);

		tmp = INPLL(pScrn, RADEON_PIXCLKS_CNTL);
		tmp |= (RADEON_PIX2CLK_ALWAYS_ONb         |
			RADEON_PIX2CLK_DAC_ALWAYS_ONb     |
			RADEON_DISP_TVOUT_PIXCLK_TV_ALWAYS_ONb |
			R300_DVOCLK_ALWAYS_ONb            |
			RADEON_PIXCLK_BLEND_ALWAYS_ONb    |
			RADEON_PIXCLK_GV_ALWAYS_ONb       |
			R300_PIXCLK_DVO_ALWAYS_ONb        |
			RADEON_PIXCLK_LVDS_ALWAYS_ONb     |
			RADEON_PIXCLK_TMDS_ALWAYS_ONb     |
			R300_PIXCLK_TRANS_ALWAYS_ONb      |
			R300_PIXCLK_TVO_ALWAYS_ONb        |
			R300_P2G2CLK_ALWAYS_ONb           |
			R300_P2G2CLK_ALWAYS_ONb);
		OUTPLL(pScrn, RADEON_PIXCLKS_CNTL, tmp);

		tmp = INPLL(pScrn, RADEON_MCLK_MISC);
		tmp |= (RADEON_MC_MCLK_DYN_ENABLE |
			RADEON_IO_MCLK_DYN_ENABLE);
		OUTPLL(pScrn, RADEON_MCLK_MISC, tmp);

		tmp = INPLL(pScrn, RADEON_MCLK_CNTL);
		tmp |= (RADEON_FORCEON_MCLKA |
			RADEON_FORCEON_MCLKB);

		tmp &= ~(RADEON_FORCEON_YCLKA  |
			 RADEON_FORCEON_YCLKB  |
			 RADEON_FORCEON_MC);

		/* Some releases of vbios have set DISABLE_MC_MCLKA
		   and DISABLE_MC_MCLKB bits in the vbios table.  Setting these
		   bits will cause H/W hang when reading video memory with dynamic clocking
		   enabled. */
		if ((tmp & R300_DISABLE_MC_MCLKA) &&
		    (tmp & R300_DISABLE_MC_MCLKB)) {
		    /* If both bits are set, then check the active channels */
		    tmp = INPLL(pScrn, RADEON_MCLK_CNTL);
		    if (info->RamWidth == 64) {
			if (INREG(RADEON_MEM_CNTL) & R300_MEM_USE_CD_CH_ONLY)
			    tmp &= ~R300_DISABLE_MC_MCLKB;
			else
			    tmp &= ~R300_DISABLE_MC_MCLKA;
		    } else {
			tmp &= ~(R300_DISABLE_MC_MCLKA |
				 R300_DISABLE_MC_MCLKB);
		    }
		}

		OUTPLL(pScrn, RADEON_MCLK_CNTL, tmp);
	    } else {
		tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
		tmp &= ~(R300_SCLK_FORCE_VAP);
		tmp |= RADEON_SCLK_FORCE_CP;
		OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);
		usleep(15000);

		tmp = INPLL(pScrn, R300_SCLK_CNTL2);
		tmp &= ~(R300_SCLK_FORCE_TCL |
			 R300_SCLK_FORCE_GA  |
			 R300_SCLK_FORCE_CBA);
		OUTPLL(pScrn, R300_SCLK_CNTL2, tmp);
	    }
	} else {
	    tmp = INPLL(pScrn, RADEON_CLK_PWRMGT_CNTL);

	    tmp &= ~(RADEON_ACTIVE_HILO_LAT_MASK     |
		     RADEON_DISP_DYN_STOP_LAT_MASK   |
		     RADEON_DYN_STOP_MODE_MASK);

	    tmp |= (RADEON_ENGIN_DYNCLK_MODE |
		    (0x01 << RADEON_ACTIVE_HILO_LAT_SHIFT));
	    OUTPLL(pScrn, RADEON_CLK_PWRMGT_CNTL, tmp);
	    usleep(15000);

	    tmp = INPLL(pScrn, RADEON_CLK_PIN_CNTL);
	    tmp |= RADEON_SCLK_DYN_START_CNTL;
	    OUTPLL(pScrn, RADEON_CLK_PIN_CNTL, tmp);
	    usleep(15000);

	    /* When DRI is enabled, setting DYN_STOP_LAT to zero can cause some R200
	       to lockup randomly, leave them as set by BIOS.
	    */
	    tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
	    /*tmp &= RADEON_SCLK_SRC_SEL_MASK;*/
	    tmp &= ~RADEON_SCLK_FORCEON_MASK;

	    /*RAGE_6::A11 A12 A12N1 A13, RV250::A11 A12, R300*/
	    if (((info->ChipFamily == CHIP_FAMILY_RV250) &&
		 ((INREG(RADEON_CONFIG_CNTL) & RADEON_CFG_ATI_REV_ID_MASK) <
		  RADEON_CFG_ATI_REV_A13)) ||
		((info->ChipFamily == CHIP_FAMILY_RV100) &&
		 ((INREG(RADEON_CONFIG_CNTL) & RADEON_CFG_ATI_REV_ID_MASK) <=
		  RADEON_CFG_ATI_REV_A13))) {
		tmp |= RADEON_SCLK_FORCE_CP;
		tmp |= RADEON_SCLK_FORCE_VIP;
	    }

	    OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);

	    if ((info->ChipFamily == CHIP_FAMILY_RV200) ||
		(info->ChipFamily == CHIP_FAMILY_RV250) ||
		(info->ChipFamily == CHIP_FAMILY_RV280)) {
		tmp = INPLL(pScrn, RADEON_SCLK_MORE_CNTL);
		tmp &= ~RADEON_SCLK_MORE_FORCEON;

		/* RV200::A11 A12 RV250::A11 A12 */
		if (((info->ChipFamily == CHIP_FAMILY_RV200) ||
		     (info->ChipFamily == CHIP_FAMILY_RV250)) &&
		    ((INREG(RADEON_CONFIG_CNTL) & RADEON_CFG_ATI_REV_ID_MASK) <
		     RADEON_CFG_ATI_REV_A13)) {
		    tmp |= RADEON_SCLK_MORE_FORCEON;
		}
		OUTPLL(pScrn, RADEON_SCLK_MORE_CNTL, tmp);
		usleep(15000);
	    }

	    /* RV200::A11 A12, RV250::A11 A12 */
	    if (((info->ChipFamily == CHIP_FAMILY_RV200) ||
		 (info->ChipFamily == CHIP_FAMILY_RV250)) &&
		((INREG(RADEON_CONFIG_CNTL) & RADEON_CFG_ATI_REV_ID_MASK) <
		 RADEON_CFG_ATI_REV_A13)) {
		tmp = INPLL(pScrn, RADEON_PLL_PWRMGT_CNTL);
		tmp |= RADEON_TCL_BYPASS_DISABLE;
		OUTPLL(pScrn, RADEON_PLL_PWRMGT_CNTL, tmp);
	    }
	    usleep(15000);

	    /*enable dynamic mode for display clocks (PIXCLK and PIX2CLK)*/
	    tmp = INPLL(pScrn, RADEON_PIXCLKS_CNTL);
	    tmp |=  (RADEON_PIX2CLK_ALWAYS_ONb         |
		     RADEON_PIX2CLK_DAC_ALWAYS_ONb     |
		     RADEON_PIXCLK_BLEND_ALWAYS_ONb    |
		     RADEON_PIXCLK_GV_ALWAYS_ONb       |
		     RADEON_PIXCLK_DIG_TMDS_ALWAYS_ONb |
		     RADEON_PIXCLK_LVDS_ALWAYS_ONb     |
		     RADEON_PIXCLK_TMDS_ALWAYS_ONb);

	    OUTPLL(pScrn, RADEON_PIXCLKS_CNTL, tmp);
	    usleep(15000);

	    tmp = INPLL(pScrn, RADEON_VCLK_ECP_CNTL);
	    tmp |= (RADEON_PIXCLK_ALWAYS_ONb  |
		    RADEON_PIXCLK_DAC_ALWAYS_ONb);

	    OUTPLL(pScrn, RADEON_VCLK_ECP_CNTL, tmp);
	    usleep(15000);
	}
    } else {
	/* Turn everything OFF (ForceON to everything)*/
	if ( !pRADEONEnt->HasCRTC2 ) {
	    tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
	    tmp |= (RADEON_SCLK_FORCE_CP   | RADEON_SCLK_FORCE_HDP |
		    RADEON_SCLK_FORCE_DISP1 | RADEON_SCLK_FORCE_TOP |
		    RADEON_SCLK_FORCE_E2   | RADEON_SCLK_FORCE_SE  |
		    RADEON_SCLK_FORCE_IDCT | RADEON_SCLK_FORCE_VIP |
		    RADEON_SCLK_FORCE_RE   | RADEON_SCLK_FORCE_PB  |
		    RADEON_SCLK_FORCE_TAM  | RADEON_SCLK_FORCE_TDM |
		    RADEON_SCLK_FORCE_RB);
	    OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);
	} else if ((info->ChipFamily == CHIP_FAMILY_RS400) ||
		   (info->ChipFamily == CHIP_FAMILY_RS480)) {
	    tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
	    tmp |= (RADEON_SCLK_FORCE_DISP2 | RADEON_SCLK_FORCE_CP      |
		    RADEON_SCLK_FORCE_HDP   | RADEON_SCLK_FORCE_DISP1   |
		    RADEON_SCLK_FORCE_TOP   | RADEON_SCLK_FORCE_E2      |
		    R300_SCLK_FORCE_VAP     | RADEON_SCLK_FORCE_IDCT    |
		    RADEON_SCLK_FORCE_VIP   | R300_SCLK_FORCE_SR        |
		    R300_SCLK_FORCE_PX      | R300_SCLK_FORCE_TX        |
		    R300_SCLK_FORCE_US      | RADEON_SCLK_FORCE_TV_SCLK |
		    R300_SCLK_FORCE_SU      | RADEON_SCLK_FORCE_OV0);
	    OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);

	    tmp = INPLL(pScrn, RADEON_SCLK_MORE_CNTL);
	    tmp |= RADEON_SCLK_MORE_FORCEON;
	    OUTPLL(pScrn, RADEON_SCLK_MORE_CNTL, tmp);

	    tmp = INPLL(pScrn, RADEON_VCLK_ECP_CNTL);
	    tmp &= ~(RADEON_PIXCLK_ALWAYS_ONb  |
		     RADEON_PIXCLK_DAC_ALWAYS_ONb |
		     R300_DISP_DAC_PIXCLK_DAC_BLANK_OFF);
	    OUTPLL(pScrn, RADEON_VCLK_ECP_CNTL, tmp);

	    tmp = INPLL(pScrn, RADEON_PIXCLKS_CNTL);
	    tmp &= ~(RADEON_PIX2CLK_ALWAYS_ONb         |
		     RADEON_PIX2CLK_DAC_ALWAYS_ONb     |
		     RADEON_DISP_TVOUT_PIXCLK_TV_ALWAYS_ONb |
		     R300_DVOCLK_ALWAYS_ONb            |
		     RADEON_PIXCLK_BLEND_ALWAYS_ONb    |
		     RADEON_PIXCLK_GV_ALWAYS_ONb       |
		     R300_PIXCLK_DVO_ALWAYS_ONb        |
		     RADEON_PIXCLK_LVDS_ALWAYS_ONb     |
		     RADEON_PIXCLK_TMDS_ALWAYS_ONb     |
		     R300_PIXCLK_TRANS_ALWAYS_ONb      |
		     R300_PIXCLK_TVO_ALWAYS_ONb        |
		     R300_P2G2CLK_ALWAYS_ONb            |
		     R300_P2G2CLK_ALWAYS_ONb           |
		     R300_DISP_DAC_PIXCLK_DAC2_BLANK_OFF);
	    OUTPLL(pScrn, RADEON_PIXCLKS_CNTL, tmp);
	} else if (info->ChipFamily >= CHIP_FAMILY_RV350) {
	    /* for RV350/M10, no delays are required. */
	    tmp = INPLL(pScrn, R300_SCLK_CNTL2);
	    tmp |= (R300_SCLK_FORCE_TCL |
		    R300_SCLK_FORCE_GA  |
		    R300_SCLK_FORCE_CBA);
	    OUTPLL(pScrn, R300_SCLK_CNTL2, tmp);

	    tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
	    tmp |= (RADEON_SCLK_FORCE_DISP2 | RADEON_SCLK_FORCE_CP      |
		    RADEON_SCLK_FORCE_HDP   | RADEON_SCLK_FORCE_DISP1   |
		    RADEON_SCLK_FORCE_TOP   | RADEON_SCLK_FORCE_E2      |
		    R300_SCLK_FORCE_VAP     | RADEON_SCLK_FORCE_IDCT    |
		    RADEON_SCLK_FORCE_VIP   | R300_SCLK_FORCE_SR        |
		    R300_SCLK_FORCE_PX      | R300_SCLK_FORCE_TX        |
		    R300_SCLK_FORCE_US      | RADEON_SCLK_FORCE_TV_SCLK |
		    R300_SCLK_FORCE_SU      | RADEON_SCLK_FORCE_OV0);
	    OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);

	    tmp = INPLL(pScrn, RADEON_SCLK_MORE_CNTL);
	    tmp |= RADEON_SCLK_MORE_FORCEON;
	    OUTPLL(pScrn, RADEON_SCLK_MORE_CNTL, tmp);

	    tmp = INPLL(pScrn, RADEON_MCLK_CNTL);
	    tmp |= (RADEON_FORCEON_MCLKA |
		    RADEON_FORCEON_MCLKB |
		    RADEON_FORCEON_YCLKA |
		    RADEON_FORCEON_YCLKB |
		    RADEON_FORCEON_MC);
	    OUTPLL(pScrn, RADEON_MCLK_CNTL, tmp);

	    tmp = INPLL(pScrn, RADEON_VCLK_ECP_CNTL);
	    tmp &= ~(RADEON_PIXCLK_ALWAYS_ONb  |
		     RADEON_PIXCLK_DAC_ALWAYS_ONb |
		     R300_DISP_DAC_PIXCLK_DAC_BLANK_OFF);
	    OUTPLL(pScrn, RADEON_VCLK_ECP_CNTL, tmp);

	    tmp = INPLL(pScrn, RADEON_PIXCLKS_CNTL);
	    tmp &= ~(RADEON_PIX2CLK_ALWAYS_ONb         |
		     RADEON_PIX2CLK_DAC_ALWAYS_ONb     |
		     RADEON_DISP_TVOUT_PIXCLK_TV_ALWAYS_ONb |
		     R300_DVOCLK_ALWAYS_ONb            |
		     RADEON_PIXCLK_BLEND_ALWAYS_ONb    |
		     RADEON_PIXCLK_GV_ALWAYS_ONb       |
		     R300_PIXCLK_DVO_ALWAYS_ONb        |
		     RADEON_PIXCLK_LVDS_ALWAYS_ONb     |
		     RADEON_PIXCLK_TMDS_ALWAYS_ONb     |
		     R300_PIXCLK_TRANS_ALWAYS_ONb      |
		     R300_PIXCLK_TVO_ALWAYS_ONb        |
		     R300_P2G2CLK_ALWAYS_ONb            |
		     R300_P2G2CLK_ALWAYS_ONb           |
		     R300_DISP_DAC_PIXCLK_DAC2_BLANK_OFF);
	    OUTPLL(pScrn, RADEON_PIXCLKS_CNTL, tmp);
	}  else {
	    tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
	    tmp |= (RADEON_SCLK_FORCE_CP | RADEON_SCLK_FORCE_E2);
	    tmp |= RADEON_SCLK_FORCE_SE;

	    if ( !pRADEONEnt->HasCRTC2 ) {
		tmp |= ( RADEON_SCLK_FORCE_RB    |
			 RADEON_SCLK_FORCE_TDM   |
			 RADEON_SCLK_FORCE_TAM   |
			 RADEON_SCLK_FORCE_PB    |
			 RADEON_SCLK_FORCE_RE    |
			 RADEON_SCLK_FORCE_VIP   |
			 RADEON_SCLK_FORCE_IDCT  |
			 RADEON_SCLK_FORCE_TOP   |
			 RADEON_SCLK_FORCE_DISP1 |
			 RADEON_SCLK_FORCE_DISP2 |
			 RADEON_SCLK_FORCE_HDP    );
	    } else if ((info->ChipFamily == CHIP_FAMILY_R300) ||
		       (info->ChipFamily == CHIP_FAMILY_R350)) {
		tmp |= ( RADEON_SCLK_FORCE_HDP   |
			 RADEON_SCLK_FORCE_DISP1 |
			 RADEON_SCLK_FORCE_DISP2 |
			 RADEON_SCLK_FORCE_TOP   |
			 RADEON_SCLK_FORCE_IDCT  |
			 RADEON_SCLK_FORCE_VIP);
	    }
	    OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);

	    usleep(16000);

	    if ((info->ChipFamily == CHIP_FAMILY_R300) ||
		(info->ChipFamily == CHIP_FAMILY_R350)) {
		tmp = INPLL(pScrn, R300_SCLK_CNTL2);
		tmp |= ( R300_SCLK_FORCE_TCL |
			 R300_SCLK_FORCE_GA  |
			 R300_SCLK_FORCE_CBA);
		OUTPLL(pScrn, R300_SCLK_CNTL2, tmp);
		usleep(16000);
	    }

	    if (info->IsIGP) {
		tmp = INPLL(pScrn, RADEON_MCLK_CNTL);
		tmp &= ~(RADEON_FORCEON_MCLKA |
			 RADEON_FORCEON_YCLKA);
		OUTPLL(pScrn, RADEON_MCLK_CNTL, tmp);
		usleep(16000);
	    }

	    if ((info->ChipFamily == CHIP_FAMILY_RV200) ||
		(info->ChipFamily == CHIP_FAMILY_RV250) ||
		(info->ChipFamily == CHIP_FAMILY_RV280)) {
		tmp = INPLL(pScrn, RADEON_SCLK_MORE_CNTL);
		tmp |= RADEON_SCLK_MORE_FORCEON;
		OUTPLL(pScrn, RADEON_SCLK_MORE_CNTL, tmp);
		usleep(16000);
	    }

	    tmp = INPLL(pScrn, RADEON_PIXCLKS_CNTL);
	    tmp &= ~(RADEON_PIX2CLK_ALWAYS_ONb         |
		     RADEON_PIX2CLK_DAC_ALWAYS_ONb     |
		     RADEON_PIXCLK_BLEND_ALWAYS_ONb    |
		     RADEON_PIXCLK_GV_ALWAYS_ONb       |
		     RADEON_PIXCLK_DIG_TMDS_ALWAYS_ONb |
		     RADEON_PIXCLK_LVDS_ALWAYS_ONb     |
		     RADEON_PIXCLK_TMDS_ALWAYS_ONb);

	    OUTPLL(pScrn, RADEON_PIXCLKS_CNTL, tmp);
	    usleep(16000);

	    tmp = INPLL(pScrn, RADEON_VCLK_ECP_CNTL);
	    tmp &= ~(RADEON_PIXCLK_ALWAYS_ONb  |
		     RADEON_PIXCLK_DAC_ALWAYS_ONb);
	    OUTPLL(pScrn, RADEON_VCLK_ECP_CNTL, tmp);
	}
    }
}

void RADEONForceSomeClocks(ScrnInfoPtr pScrn)
{
    /* It appears from r300 and rv100 may need some clocks forced-on */
     uint32_t tmp;

     tmp = INPLL(pScrn, RADEON_SCLK_CNTL);
     tmp |= RADEON_SCLK_FORCE_CP | RADEON_SCLK_FORCE_VIP;
     OUTPLL(pScrn, RADEON_SCLK_CNTL, tmp);
}

static void
RADEONSetPCIELanes(ScrnInfoPtr pScrn, int lanes)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    uint32_t link_width_cntl, mask, target_reg;

    if (info->IsIGP)
	return;

    RADEONWaitForIdleMMIO(pScrn);

    switch (lanes) {
    case 0:
	mask = RADEON_PCIE_LC_LINK_WIDTH_X0;
	break;
    case 1:
	mask = RADEON_PCIE_LC_LINK_WIDTH_X1;
	break;
    case 2:
	mask = RADEON_PCIE_LC_LINK_WIDTH_X2;
	break;
    case 4:
	mask = RADEON_PCIE_LC_LINK_WIDTH_X4;
	break;
    case 8:
	mask = RADEON_PCIE_LC_LINK_WIDTH_X8;
	break;
    case 12:
	mask = RADEON_PCIE_LC_LINK_WIDTH_X12;
	break;
    case 16:
    default:
	mask = RADEON_PCIE_LC_LINK_WIDTH_X16;
	break;
    }

    if (info->ChipFamily >= CHIP_FAMILY_R600) {
	link_width_cntl = INPCIE_P(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL);

	if ((link_width_cntl & RADEON_PCIE_LC_LINK_WIDTH_RD_MASK) ==
	    (mask << RADEON_PCIE_LC_LINK_WIDTH_RD_SHIFT))
	    return;

	link_width_cntl &= ~(RADEON_PCIE_LC_LINK_WIDTH_MASK |
			     RADEON_PCIE_LC_RECONFIG_NOW |
			     R600_PCIE_LC_RECONFIG_ARC_MISSING_ESCAPE |
			     R600_PCIE_LC_SHORT_RECONFIG_EN |
			     R600_PCIE_LC_RENEGOTIATE_EN);
	link_width_cntl |= mask;

#if 0
	/* some northbridges can renegotiate the link rather than requiring
	 * a complete re-config.
	 * e.g., AMD 780/790 northbridges (pci ids: 0x5956, 0x5957, 0x5958, etc.)
	 */
	if (northbridge can renegotiate)
	    link_width_cntl |= R600_PCIE_LC_RENEGOTIATE_EN;
	else
#endif
	    link_width_cntl |= R600_PCIE_LC_RECONFIG_ARC_MISSING_ESCAPE;

	OUTPCIE_P(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL, link_width_cntl);
	OUTPCIE_P(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL, link_width_cntl | RADEON_PCIE_LC_RECONFIG_NOW);

	if (info->ChipFamily >= CHIP_FAMILY_RV770)
	    target_reg = R700_TARGET_AND_CURRENT_PROFILE_INDEX;
	else
	    target_reg = R600_TARGET_AND_CURRENT_PROFILE_INDEX;

	/* wait for lane set to complete */
	link_width_cntl = INREG(target_reg);
	while (link_width_cntl == 0xffffffff)
	    link_width_cntl = INREG(target_reg);

    } else {
	link_width_cntl = INPCIE(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL);

	if ((link_width_cntl & RADEON_PCIE_LC_LINK_WIDTH_RD_MASK) ==
	    (mask << RADEON_PCIE_LC_LINK_WIDTH_RD_SHIFT))
	    return;

	link_width_cntl &= ~(RADEON_PCIE_LC_LINK_WIDTH_MASK |
			     RADEON_PCIE_LC_RECONFIG_NOW |
			     RADEON_PCIE_LC_RECONFIG_LATER |
			     RADEON_PCIE_LC_SHORT_RECONFIG_EN);
	link_width_cntl |= mask;
	OUTPCIE(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL, link_width_cntl);
	OUTPCIE(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL, link_width_cntl | RADEON_PCIE_LC_RECONFIG_NOW);

	/* wait for lane set to complete */
	link_width_cntl = INPCIE(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL);
	while (link_width_cntl == 0xffffffff)
	    link_width_cntl = INPCIE(pScrn, RADEON_PCIE_LC_LINK_WIDTH_CNTL);

    }

}

static void
RADEONSetClockGating(ScrnInfoPtr pScrn, Bool enable)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);

    RADEONWaitForIdleMMIO(pScrn);

    if (info->ChipFamily >= CHIP_FAMILY_R600)
	atombios_static_pwrmgt_setup(pScrn, enable);
    else {
	if (info->IsAtomBios) {
	    atombios_static_pwrmgt_setup(pScrn, enable);
	    atombios_clk_gating_setup(pScrn, enable);
	} else if (info->IsMobility)
	    LegacySetClockGating(pScrn, enable);

	if (IS_R300_VARIANT || IS_RV100_VARIANT)
	    RADEONForceSomeClocks(pScrn);
    }

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Dynamic Clock Gating %sabled\n",
	       enable ? "En" : "Dis");
}

static void RADEONSetStaticPowerMode(ScrnInfoPtr pScrn, RADEONPMType type)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);
    int i;

    for (i = 0; i < info->pm.num_modes; i++) {
	if (info->pm.mode[i].type == type)
	    break;
    }

    if (i == info->pm.num_modes)
	return;

    if (i == info->pm.current_mode)
	return;

    RADEONWaitForIdleMMIO(pScrn);

    if (info->IsAtomBios)
	atombios_set_engine_clock(pScrn, info->pm.mode[i].sclk);
    else
	RADEONSetEngineClock(pScrn, info->pm.mode[i].sclk);

    if (info->cardType == CARD_PCIE)
	RADEONSetPCIELanes(pScrn, info->pm.mode[i].pcie_lanes);

    info->pm.current_mode = i;

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Power Mode Switch\n");
}


void RADEONPMInit(ScrnInfoPtr pScrn)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);

    if (xf86ReturnOptValBool(info->Options, OPTION_CLOCK_GATING, FALSE)) {
	info->pm.clock_gating_enabled = TRUE;
	RADEONSetClockGating(pScrn, info->pm.clock_gating_enabled);
    } else
	info->pm.clock_gating_enabled = FALSE;

    info->pm.mode[0].type = POWER_DEFAULT;
    info->pm.mode[0].sclk = (uint32_t)info->sclk * 100; /* 10 khz */
    info->pm.mode[0].mclk = (uint32_t)info->mclk * 100; /* 10 khz */
    info->pm.mode[0].pcie_lanes = 16; /* XXX: read back current lane config */
    info->pm.current_mode = 0;
    info->pm.num_modes = 1;

    if (xf86ReturnOptValBool(info->Options, OPTION_DYNAMIC_PM, FALSE)) {
	info->pm.dynamic_mode_enabled = TRUE;
	info->pm.mode[1].type = POWER_LOW;
	info->pm.mode[1].sclk = info->pm.mode[0].sclk / 4;
	info->pm.mode[1].mclk = info->pm.mode[0].mclk / 4;
	info->pm.mode[1].pcie_lanes = 1;

	info->pm.mode[2].type = POWER_HIGH;
	info->pm.mode[2].sclk = info->pm.mode[0].sclk;
	info->pm.mode[2].mclk = info->pm.mode[0].mclk;
	info->pm.mode[2].pcie_lanes = 16;

	info->pm.num_modes += 2;

	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Dynamic Power Management Enabled\n");
    } else {
	info->pm.dynamic_mode_enabled = FALSE;
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Dynamic Power Management Disabled\n");
    }

    if (xf86ReturnOptValBool(info->Options, OPTION_FORCE_LOW_POWER, FALSE)) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Force Low Power Mode Enabled\n");
	info->pm.force_low_power_enabled = TRUE;
	if (info->pm.dynamic_mode_enabled) {
	    info->pm.mode[2].type = POWER_HIGH;
	    info->pm.mode[2].sclk = info->pm.mode[0].sclk / 2;
	    info->pm.mode[2].mclk = info->pm.mode[0].mclk / 2;
	    info->pm.mode[2].pcie_lanes = 4;
	} else {
	    info->pm.mode[1].type = POWER_HIGH;
	    info->pm.mode[1].sclk = info->pm.mode[0].sclk / 2;
	    info->pm.mode[1].mclk = info->pm.mode[0].mclk / 2;
	    info->pm.mode[1].pcie_lanes = 4;
	    info->pm.num_modes += 1;
	}
	RADEONSetStaticPowerMode(pScrn, POWER_HIGH);
    } else
	info->pm.force_low_power_enabled = FALSE;

}

void RADEONPMEnterVT(ScrnInfoPtr pScrn)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);

    if (info->pm.clock_gating_enabled)
	RADEONSetClockGating(pScrn, info->pm.clock_gating_enabled);
    if (info->pm.force_low_power_enabled || info->pm.dynamic_mode_enabled)
	RADEONSetStaticPowerMode(pScrn, POWER_HIGH);
}

void RADEONPMLeaveVT(ScrnInfoPtr pScrn)
{
    RADEONInfoPtr  info       = RADEONPTR(pScrn);

    if (info->pm.clock_gating_enabled)
	RADEONSetClockGating(pScrn, FALSE);
    if (info->pm.force_low_power_enabled || info->pm.dynamic_mode_enabled)
	RADEONSetStaticPowerMode(pScrn, POWER_DEFAULT);
}

void RADEONPMFini(ScrnInfoPtr pScrn)
{
    RADEONInfoPtr info = RADEONPTR(pScrn);

    if (info->pm.clock_gating_enabled)
	RADEONSetClockGating(pScrn, FALSE);
    if (info->pm.force_low_power_enabled || info->pm.dynamic_mode_enabled)
	RADEONSetStaticPowerMode(pScrn, POWER_DEFAULT);
}

void RADEONPMBlockHandler(ScrnInfoPtr pScrn)
{
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
    int i;

    for (i = 0; i < xf86_config->num_crtc; i++) {
	xf86CrtcPtr crtc = xf86_config->crtc[i];
	RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;

	if (radeon_crtc->enabled)
	    break;
    }

    if (i == xf86_config->num_crtc)
	RADEONSetStaticPowerMode(pScrn, POWER_LOW);
    else
	RADEONSetStaticPowerMode(pScrn, POWER_HIGH);

}