radeon_crtc.c revision ad43ddac
1/*
2 * Copyright 2000 ATI Technologies Inc., Markham, Ontario, and
3 *                VA Linux Systems Inc., Fremont, California.
4 *
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation on the rights to use, copy, modify, merge,
11 * publish, distribute, sublicense, and/or sell copies of the Software,
12 * and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT.  IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR
23 * THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 */
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include <string.h>
34#include <stdio.h>
35
36/* X and server generic header files */
37#include "xf86.h"
38#include "xf86_OSproc.h"
39#include "vgaHW.h"
40#include "xf86Modes.h"
41
42/* Driver data structures */
43#include "radeon.h"
44#include "radeon_reg.h"
45#include "radeon_macros.h"
46#include "radeon_probe.h"
47#include "radeon_version.h"
48
49#ifdef XF86DRI
50#define _XF86DRI_SERVER_
51#include "radeon_drm.h"
52#include "sarea.h"
53#endif
54
55extern void atombios_crtc_mode_set(xf86CrtcPtr crtc,
56				   DisplayModePtr mode,
57				   DisplayModePtr adjusted_mode,
58				   int x, int y);
59extern void atombios_crtc_dpms(xf86CrtcPtr crtc, int mode);
60extern void
61RADEONInitDispBandwidthLegacy(ScrnInfoPtr pScrn,
62			      DisplayModePtr mode1, int pixel_bytes1,
63			      DisplayModePtr mode2, int pixel_bytes2);
64extern void
65RADEONInitDispBandwidthAVIVO(ScrnInfoPtr pScrn,
66			     DisplayModePtr mode1, int pixel_bytes1,
67			     DisplayModePtr mode2, int pixel_bytes2);
68
69void
70radeon_crtc_dpms(xf86CrtcPtr crtc, int mode)
71{
72    RADEONInfoPtr info = RADEONPTR(crtc->scrn);
73    RADEONEntPtr pRADEONEnt = RADEONEntPriv(crtc->scrn);
74    RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
75    xf86CrtcPtr crtc0 = pRADEONEnt->pCrtc[0];
76
77    if ((mode == DPMSModeOn) && radeon_crtc->enabled)
78	return;
79
80    if (mode == DPMSModeOff)
81	radeon_crtc_modeset_ioctl(crtc, FALSE);
82
83    if (IS_AVIVO_VARIANT || info->r4xx_atom) {
84	atombios_crtc_dpms(crtc, mode);
85    } else {
86
87	/* need to restore crtc1 before crtc0 or we may get a blank screen
88	 * in some cases
89	 */
90	if ((radeon_crtc->crtc_id == 1) && (mode == DPMSModeOn)) {
91	    if (crtc0->enabled)
92		legacy_crtc_dpms(crtc0,  DPMSModeOff);
93	}
94
95	legacy_crtc_dpms(crtc, mode);
96
97	if ((radeon_crtc->crtc_id == 1) && (mode == DPMSModeOn)) {
98	    if (crtc0->enabled)
99		legacy_crtc_dpms(crtc0, mode);
100	}
101    }
102
103    if (mode != DPMSModeOff) {
104	radeon_crtc_modeset_ioctl(crtc, TRUE);
105	radeon_crtc_load_lut(crtc);
106    }
107
108    if (mode == DPMSModeOn)
109	radeon_crtc->enabled = TRUE;
110    else
111	radeon_crtc->enabled = FALSE;
112}
113
114static Bool
115radeon_crtc_mode_fixup(xf86CrtcPtr crtc, DisplayModePtr mode,
116		     DisplayModePtr adjusted_mode)
117{
118    return TRUE;
119}
120
121static void
122radeon_crtc_mode_prepare(xf86CrtcPtr crtc)
123{
124    RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
125
126    if (radeon_crtc->enabled)
127	crtc->funcs->hide_cursor(crtc);
128}
129
130static uint32_t RADEONDiv(CARD64 n, uint32_t d)
131{
132    return (n + (d / 2)) / d;
133}
134
135static void
136RADEONComputePLL_old(RADEONPLLPtr pll,
137		     unsigned long freq,
138		     uint32_t *chosen_dot_clock_freq,
139		     uint32_t *chosen_feedback_div,
140		     uint32_t *chosen_frac_feedback_div,
141		     uint32_t *chosen_reference_div,
142		     uint32_t *chosen_post_div,
143		     int flags)
144{
145    uint32_t min_ref_div = pll->min_ref_div;
146    uint32_t max_ref_div = pll->max_ref_div;
147    uint32_t min_post_div = pll->min_post_div;
148    uint32_t max_post_div = pll->max_post_div;
149    uint32_t min_fractional_feed_div = 0;
150    uint32_t max_fractional_feed_div = 0;
151    uint32_t best_vco = pll->best_vco;
152    uint32_t best_post_div = 1;
153    uint32_t best_ref_div = 1;
154    uint32_t best_feedback_div = 1;
155    uint32_t best_frac_feedback_div = 0;
156    uint32_t best_freq = -1;
157    uint32_t best_error = 0xffffffff;
158    uint32_t best_vco_diff = 1;
159    uint32_t post_div;
160
161    freq = freq * 1000;
162
163    ErrorF("freq: %lu\n", freq);
164
165    if (flags & RADEON_PLL_USE_REF_DIV)
166	min_ref_div = max_ref_div = pll->reference_div;
167    else {
168	while (min_ref_div < max_ref_div-1) {
169	    uint32_t mid=(min_ref_div+max_ref_div)/2;
170	    uint32_t pll_in = pll->reference_freq / mid;
171	    if (pll_in < pll->pll_in_min)
172		max_ref_div = mid;
173	    else if (pll_in > pll->pll_in_max)
174		min_ref_div = mid;
175	    else break;
176	}
177    }
178
179    if (flags & RADEON_PLL_USE_POST_DIV)
180	min_post_div = max_post_div = pll->post_div;
181
182    if (flags & RADEON_PLL_USE_FRAC_FB_DIV) {
183	min_fractional_feed_div = pll->min_frac_feedback_div;
184	max_fractional_feed_div = pll->max_frac_feedback_div;
185    }
186
187    for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
188	uint32_t ref_div;
189
190	if ((flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
191	    continue;
192
193	/* legacy radeons only have a few post_divs */
194	if (flags & RADEON_PLL_LEGACY) {
195	    if ((post_div == 5) ||
196		(post_div == 7) ||
197		(post_div == 9) ||
198		(post_div == 10) ||
199		(post_div == 11))
200		continue;
201	}
202
203	for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
204	    uint32_t feedback_div, current_freq = 0, error, vco_diff;
205	    uint32_t pll_in = pll->reference_freq / ref_div;
206	    uint32_t min_feed_div = pll->min_feedback_div;
207	    uint32_t max_feed_div = pll->max_feedback_div+1;
208
209	    if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
210		continue;
211
212	    while (min_feed_div < max_feed_div) {
213		uint32_t vco;
214		uint32_t min_frac_feed_div = min_fractional_feed_div;
215		uint32_t max_frac_feed_div = max_fractional_feed_div+1;
216		uint32_t frac_feedback_div;
217		CARD64 tmp;
218
219		feedback_div = (min_feed_div+max_feed_div)/2;
220
221		tmp = (CARD64)pll->reference_freq * feedback_div;
222		vco = RADEONDiv(tmp, ref_div);
223
224		if (vco < pll->pll_out_min) {
225		    min_feed_div = feedback_div+1;
226		    continue;
227		} else if(vco > pll->pll_out_max) {
228		    max_feed_div = feedback_div;
229		    continue;
230		}
231
232		while (min_frac_feed_div < max_frac_feed_div) {
233		    frac_feedback_div = (min_frac_feed_div+max_frac_feed_div)/2;
234		    tmp = (CARD64)pll->reference_freq * 10000 * feedback_div;
235		    tmp += (CARD64)pll->reference_freq * 1000 * frac_feedback_div;
236		    current_freq = RADEONDiv(tmp, ref_div * post_div);
237
238		    if (flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
239			error = freq - current_freq;
240			error = error < 0 ? 0xffffffff : error;
241		    } else
242			error = abs(current_freq - freq);
243		    vco_diff = abs(vco - best_vco);
244
245		    if ((best_vco == 0 && error < best_error) ||
246			(best_vco != 0 &&
247			 (error < best_error - 100 ||
248			  (abs(error - best_error) < 100 && vco_diff < best_vco_diff )))) {
249			best_post_div = post_div;
250			best_ref_div = ref_div;
251			best_feedback_div = feedback_div;
252			best_frac_feedback_div = frac_feedback_div;
253			best_freq = current_freq;
254			best_error = error;
255			best_vco_diff = vco_diff;
256		    } else if (current_freq == freq) {
257			if (best_freq == -1) {
258			    best_post_div = post_div;
259			    best_ref_div = ref_div;
260			    best_feedback_div = feedback_div;
261			    best_frac_feedback_div = frac_feedback_div;
262			    best_freq = current_freq;
263			    best_error = error;
264			    best_vco_diff = vco_diff;
265			} else if (((flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
266				   ((flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
267				   ((flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
268				   ((flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
269				   ((flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
270				   ((flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
271			    best_post_div = post_div;
272			    best_ref_div = ref_div;
273			    best_feedback_div = feedback_div;
274			    best_frac_feedback_div = frac_feedback_div;
275			    best_freq = current_freq;
276			    best_error = error;
277			    best_vco_diff = vco_diff;
278			}
279		    }
280		    if (current_freq < freq)
281			min_frac_feed_div = frac_feedback_div+1;
282		    else
283			max_frac_feed_div = frac_feedback_div;
284		}
285		if (current_freq < freq)
286		    min_feed_div = feedback_div+1;
287		else
288		    max_feed_div = feedback_div;
289	    }
290	}
291    }
292
293    ErrorF("best_freq: %u\n", (unsigned int)best_freq);
294    ErrorF("best_feedback_div: %u\n", (unsigned int)best_feedback_div);
295    ErrorF("best_frac_feedback_div: %u\n", (unsigned int)best_frac_feedback_div);
296    ErrorF("best_ref_div: %u\n", (unsigned int)best_ref_div);
297    ErrorF("best_post_div: %u\n", (unsigned int)best_post_div);
298
299    if (best_freq == -1)
300	FatalError("Couldn't find valid PLL dividers\n");
301    *chosen_dot_clock_freq = best_freq / 10000;
302    *chosen_feedback_div = best_feedback_div;
303    *chosen_frac_feedback_div = best_frac_feedback_div;
304    *chosen_reference_div = best_ref_div;
305    *chosen_post_div = best_post_div;
306
307}
308
309static Bool
310calc_fb_div(RADEONPLLPtr pll,
311            unsigned long freq,
312            int flags,
313            int post_div,
314	    int ref_div,
315            int *fb_div,
316            int *fb_div_frac)
317{
318    float ffreq = freq / 10;
319    float vco_freq = ffreq * post_div;
320    float feedback_divider = vco_freq * ref_div / pll->reference_freq;
321
322    if (flags & RADEON_PLL_USE_FRAC_FB_DIV) {
323        feedback_divider = floor((feedback_divider * 10.0) + 0.5) * 0.1;
324
325	*fb_div = floor(feedback_divider);
326        *fb_div_frac = fmod(feedback_divider, 1.0) * 10.0;
327
328    } else {
329        *fb_div = floor(feedback_divider + 0.5);
330        *fb_div_frac = 0;
331    }
332    if ((*fb_div < pll->min_feedback_div) || (*fb_div > pll->max_feedback_div))
333        return FALSE;
334    else
335        return TRUE;
336}
337
338static Bool
339calc_fb_ref_div(RADEONPLLPtr pll,
340                unsigned long freq,
341                int flags,
342                int post_div,
343                int *fb_div,
344                int *fb_div_frac,
345                int *ref_div)
346{
347    float ffreq = freq / 10;
348    float max_error = ffreq * 0.0025;
349    float vco, error, pll_out;
350
351    for ((*ref_div) = pll->min_ref_div; (*ref_div) < pll->max_ref_div; ++(*ref_div)) {
352        if (calc_fb_div(pll, freq, flags, post_div, (*ref_div), fb_div, fb_div_frac)) {
353            vco = pll->reference_freq * ((*fb_div) + ((*fb_div_frac) * 0.1)) / (*ref_div);
354
355            if ((vco < pll->pll_out_min) || (vco > pll->pll_out_max))
356                continue;
357
358            pll_out = vco / post_div;
359
360            error = pll_out - ffreq;
361            if ((fabs(error) <= max_error) && (error >= 0))
362                return TRUE;
363        }
364    }
365    return FALSE;
366}
367
368static void
369RADEONComputePLL_new(RADEONPLLPtr pll,
370		     unsigned long freq,
371		     uint32_t *chosen_dot_clock_freq,
372		     uint32_t *chosen_feedback_div,
373		     uint32_t *chosen_frac_feedback_div,
374		     uint32_t *chosen_reference_div,
375		     uint32_t *chosen_post_div,
376		     int flags)
377{
378    float ffreq = freq / 10;
379    float vco_frequency;
380    int fb_div = 0, fb_div_frac = 0, post_div = 0, ref_div = 0;
381    uint32_t best_freq = 0;
382
383    if (flags & RADEON_PLL_USE_POST_DIV) {
384        post_div = pll->post_div;
385        if ((post_div < pll->min_post_div) || (post_div > pll->max_post_div))
386            goto done;
387        vco_frequency = ffreq * post_div;
388        if ((vco_frequency < pll->pll_out_min) || (vco_frequency > pll->pll_out_max))
389            goto done;
390
391        if (flags & RADEON_PLL_USE_REF_DIV) {
392            ref_div = pll->reference_div;
393            if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
394                goto done;
395            if (!calc_fb_div(pll, freq, flags, post_div, ref_div, &fb_div, &fb_div_frac))
396                goto done;
397        }
398    } else {
399	for (post_div = pll->max_post_div; post_div >= pll->min_post_div; --post_div) {
400	    if (flags & RADEON_PLL_LEGACY) {
401		if ((post_div == 5) ||
402		    (post_div == 7) ||
403		    (post_div == 9) ||
404		    (post_div == 10) ||
405		    (post_div == 11))
406		    continue;
407	    }
408	    if ((flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
409		continue;
410
411	    vco_frequency = ffreq * post_div;
412	    if ((vco_frequency < pll->pll_out_min) || (vco_frequency > pll->pll_out_max))
413		continue;
414	    if (flags & RADEON_PLL_USE_REF_DIV) {
415		ref_div = pll->reference_div;
416		if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
417		    goto done;
418		if (calc_fb_div(pll, freq, flags, post_div, ref_div, &fb_div, &fb_div_frac))
419		    break;
420	    } else {
421		if (calc_fb_ref_div(pll, freq, flags, post_div, &fb_div, &fb_div_frac, &ref_div))
422		    break;
423	    }
424	}
425    }
426
427    best_freq = pll->reference_freq * 10 * fb_div;
428    best_freq += pll->reference_freq * fb_div_frac;
429    best_freq = best_freq / (ref_div * post_div);
430
431    ErrorF("best_freq: %u\n", (unsigned int)best_freq);
432    ErrorF("best_feedback_div: %u\n", (unsigned int)fb_div);
433    ErrorF("best_frac_feedback_div: %u\n", (unsigned int)fb_div_frac);
434    ErrorF("best_ref_div: %u\n", (unsigned int)ref_div);
435    ErrorF("best_post_div: %u\n", (unsigned int)post_div);
436
437done:
438    if (best_freq == 0)
439	FatalError("Couldn't find valid PLL dividers\n");
440
441    *chosen_dot_clock_freq = best_freq;
442    *chosen_feedback_div = fb_div;
443    *chosen_frac_feedback_div = fb_div_frac;
444    *chosen_reference_div = ref_div;
445    *chosen_post_div = post_div;
446
447}
448
449void
450RADEONComputePLL(ScrnInfoPtr pScrn,
451		 RADEONPLLPtr pll,
452		 unsigned long freq,
453		 uint32_t *chosen_dot_clock_freq,
454		 uint32_t *chosen_feedback_div,
455		 uint32_t *chosen_frac_feedback_div,
456		 uint32_t *chosen_reference_div,
457		 uint32_t *chosen_post_div,
458		 int flags)
459{
460    RADEONInfoPtr info = RADEONPTR(pScrn);
461
462    if (IS_AVIVO_VARIANT) {
463	if (xf86ReturnOptValBool(info->Options, OPTION_NEW_PLL, TRUE)) {
464	    /* disable frac fb dividers */
465	    flags &= ~RADEON_PLL_USE_FRAC_FB_DIV;
466	    RADEONComputePLL_new(pll, freq, chosen_dot_clock_freq,
467				 chosen_feedback_div, chosen_frac_feedback_div,
468				 chosen_reference_div, chosen_post_div, flags);
469	} else
470	    RADEONComputePLL_old(pll, freq, chosen_dot_clock_freq,
471				 chosen_feedback_div, chosen_frac_feedback_div,
472				 chosen_reference_div, chosen_post_div, flags);
473    } else {
474	if (xf86ReturnOptValBool(info->Options, OPTION_NEW_PLL, FALSE))
475	    RADEONComputePLL_new(pll, freq, chosen_dot_clock_freq,
476				 chosen_feedback_div, chosen_frac_feedback_div,
477				 chosen_reference_div, chosen_post_div, flags);
478	else
479	    RADEONComputePLL_old(pll, freq, chosen_dot_clock_freq,
480				 chosen_feedback_div, chosen_frac_feedback_div,
481				 chosen_reference_div, chosen_post_div, flags);
482    }
483}
484
485static void
486radeon_crtc_mode_set(xf86CrtcPtr crtc, DisplayModePtr mode,
487		     DisplayModePtr adjusted_mode, int x, int y)
488{
489    ScrnInfoPtr pScrn = crtc->scrn;
490    RADEONInfoPtr info = RADEONPTR(pScrn);
491
492    if (IS_AVIVO_VARIANT || info->r4xx_atom) {
493	atombios_crtc_mode_set(crtc, mode, adjusted_mode, x, y);
494    } else {
495	legacy_crtc_mode_set(crtc, mode, adjusted_mode, x, y);
496    }
497}
498
499static void
500radeon_crtc_mode_commit(xf86CrtcPtr crtc)
501{
502    if (crtc->scrn->pScreen != NULL)
503	xf86_reload_cursors(crtc->scrn->pScreen);
504}
505
506void
507radeon_crtc_load_lut(xf86CrtcPtr crtc)
508{
509    ScrnInfoPtr pScrn = crtc->scrn;
510    RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
511    RADEONInfoPtr info = RADEONPTR(pScrn);
512    unsigned char *RADEONMMIO = info->MMIO;
513    int i;
514
515    if (!crtc->enabled)
516	return;
517
518    if (IS_DCE4_VARIANT) {
519	OUTREG(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
520
521	OUTREG(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
522	OUTREG(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
523	OUTREG(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
524
525	OUTREG(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0x0000ffff);
526	OUTREG(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0x0000ffff);
527	OUTREG(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0x0000ffff);
528
529	OUTREG(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
530	OUTREG(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
531
532	for (i = 0; i < 256; i++) {
533	    OUTREG(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, i);
534	    OUTREG(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
535		   (((radeon_crtc->lut_r[i]) << 20) |
536		    ((radeon_crtc->lut_g[i]) << 10) |
537		    (radeon_crtc->lut_b[i])));
538	}
539    } else {
540	if (IS_AVIVO_VARIANT) {
541	    OUTREG(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
542
543	    OUTREG(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
544	    OUTREG(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
545	    OUTREG(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
546
547	    OUTREG(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0x0000ffff);
548	    OUTREG(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0x0000ffff);
549	    OUTREG(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0x0000ffff);
550	}
551
552	PAL_SELECT(radeon_crtc->crtc_id);
553
554	if (IS_AVIVO_VARIANT) {
555	    OUTREG(AVIVO_DC_LUT_RW_MODE, 0);
556	    OUTREG(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
557	}
558
559	for (i = 0; i < 256; i++) {
560	    OUTPAL(i, radeon_crtc->lut_r[i], radeon_crtc->lut_g[i], radeon_crtc->lut_b[i]);
561	}
562
563	if (IS_AVIVO_VARIANT)
564	    OUTREG(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
565    }
566
567}
568
569static void
570radeon_crtc_gamma_set(xf86CrtcPtr crtc, uint16_t *red, uint16_t *green,
571		      uint16_t *blue, int size)
572{
573    RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
574    ScrnInfoPtr		pScrn = crtc->scrn;
575    int i, j;
576
577    if (pScrn->depth == 16) {
578	for (i = 0; i < 64; i++) {
579	    if (i <= 31) {
580		for (j = 0; j < 8; j++) {
581		    radeon_crtc->lut_r[i * 8 + j] = red[i] >> 6;
582		    radeon_crtc->lut_b[i * 8 + j] = blue[i] >> 6;
583		}
584	    }
585
586	    for (j = 0; j < 4; j++) {
587		radeon_crtc->lut_g[i * 4 + j] = green[i] >> 6;
588	    }
589	}
590    } else {
591	for (i = 0; i < 256; i++) {
592	    radeon_crtc->lut_r[i] = red[i] >> 6;
593	    radeon_crtc->lut_g[i] = green[i] >> 6;
594	    radeon_crtc->lut_b[i] = blue[i] >> 6;
595	}
596    }
597
598    radeon_crtc_load_lut(crtc);
599}
600
601static Bool
602radeon_crtc_lock(xf86CrtcPtr crtc)
603{
604    ScrnInfoPtr		pScrn = crtc->scrn;
605    RADEONInfoPtr  info = RADEONPTR(pScrn);
606
607#ifdef XF86DRI
608    if (info->cp->CPStarted && pScrn->pScreen) {
609	DRILock(pScrn->pScreen, 0);
610	if (info->accelOn)
611	    RADEON_SYNC(info, pScrn);
612	return TRUE;
613    }
614#endif
615    if (info->accelOn)
616        RADEON_SYNC(info, pScrn);
617
618    return FALSE;
619
620}
621
622static void
623radeon_crtc_unlock(xf86CrtcPtr crtc)
624{
625    ScrnInfoPtr		pScrn = crtc->scrn;
626    RADEONInfoPtr  info = RADEONPTR(pScrn);
627
628#ifdef XF86DRI
629	if (info->cp->CPStarted && pScrn->pScreen) DRIUnlock(pScrn->pScreen);
630#endif
631
632    if (info->accelOn)
633        RADEON_SYNC(info, pScrn);
634}
635
636/**
637 * Allocates memory for a locked-in-framebuffer shadow of the given
638 * width and height for this CRTC's rotated shadow framebuffer.
639 */
640
641static void *
642radeon_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
643{
644    ScrnInfoPtr pScrn = crtc->scrn;
645    RADEONInfoPtr  info = RADEONPTR(pScrn);
646    RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
647    unsigned long rotate_pitch;
648    unsigned long rotate_offset;
649    int size;
650    int cpp = pScrn->bitsPerPixel / 8;
651
652    /* No rotation without accel */
653    if (((info->ChipFamily >= CHIP_FAMILY_R600) && !info->directRenderingEnabled) ||
654	xf86ReturnOptValBool(info->Options, OPTION_NOACCEL, FALSE)) {
655	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
656		   "Acceleration required for rotation\n");
657	return NULL;
658    }
659
660    rotate_pitch = pScrn->displayWidth * cpp;
661    size = rotate_pitch * height;
662
663    /* We could get close to what we want here by just creating a pixmap like
664     * normal, but we have to lock it down in framebuffer, and there is no
665     * setter for offscreen area locking in EXA currently.  So, we just
666     * allocate offscreen memory and fake up a pixmap header for it.
667     */
668    rotate_offset = radeon_legacy_allocate_memory(pScrn, &radeon_crtc->crtc_rotate_mem,
669		    size, RADEON_GPU_PAGE_SIZE, RADEON_GEM_DOMAIN_VRAM);
670    if (rotate_offset == 0)
671	return NULL;
672
673    return info->FB + rotate_offset;
674}
675
676/**
677 * Creates a pixmap for this CRTC's rotated shadow framebuffer.
678 */
679static PixmapPtr
680radeon_crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
681{
682    ScrnInfoPtr pScrn = crtc->scrn;
683    unsigned long rotate_pitch;
684    PixmapPtr rotate_pixmap;
685    int cpp = pScrn->bitsPerPixel / 8;
686
687    if (!data)
688	data = radeon_crtc_shadow_allocate(crtc, width, height);
689
690    rotate_pitch = pScrn->displayWidth * cpp;
691
692    rotate_pixmap = GetScratchPixmapHeader(pScrn->pScreen,
693					   width, height,
694					   pScrn->depth,
695					   pScrn->bitsPerPixel,
696					   rotate_pitch,
697					   data);
698
699    if (rotate_pixmap == NULL) {
700	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
701		   "Couldn't allocate shadow pixmap for rotated CRTC\n");
702    }
703
704    return rotate_pixmap;
705}
706
707static void
708radeon_crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
709{
710    ScrnInfoPtr pScrn = crtc->scrn;
711    RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
712
713    if (rotate_pixmap)
714	FreeScratchPixmapHeader(rotate_pixmap);
715
716    if (data) {
717	radeon_legacy_free_memory(pScrn, radeon_crtc->crtc_rotate_mem);
718	radeon_crtc->crtc_rotate_mem = NULL;
719    }
720
721}
722
723#if XF86_CRTC_VERSION >= 2
724#include "radeon_atombios.h"
725
726extern AtomBiosResult
727atombios_lock_crtc(atomBiosHandlePtr atomBIOS, int crtc, int lock);
728extern void
729RADEONInitCrtcBase(xf86CrtcPtr crtc, RADEONSavePtr save,
730		   int x, int y);
731extern void
732RADEONInitCrtc2Base(xf86CrtcPtr crtc, RADEONSavePtr save,
733		    int x, int y);
734extern void
735RADEONRestoreCrtcBase(ScrnInfoPtr pScrn,
736		      RADEONSavePtr restore);
737extern void
738RADEONRestoreCrtc2Base(ScrnInfoPtr pScrn,
739		       RADEONSavePtr restore);
740
741static void
742radeon_crtc_set_origin(xf86CrtcPtr crtc, int x, int y)
743{
744    ScrnInfoPtr pScrn = crtc->scrn;
745    RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
746    RADEONInfoPtr info = RADEONPTR(pScrn);
747    unsigned char *RADEONMMIO = info->MMIO;
748
749
750    if (IS_DCE4_VARIANT) {
751	x &= ~3;
752	y &= ~1;
753	atombios_lock_crtc(info->atomBIOS, radeon_crtc->crtc_id, 1);
754	OUTREG(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset, (x << 16) | y);
755	atombios_lock_crtc(info->atomBIOS, radeon_crtc->crtc_id, 0);
756    } else if (IS_AVIVO_VARIANT) {
757	x &= ~3;
758	y &= ~1;
759	atombios_lock_crtc(info->atomBIOS, radeon_crtc->crtc_id, 1);
760	OUTREG(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset, (x << 16) | y);
761	atombios_lock_crtc(info->atomBIOS, radeon_crtc->crtc_id, 0);
762    } else {
763	switch (radeon_crtc->crtc_id) {
764	case 0:
765	    RADEONInitCrtcBase(crtc, info->ModeReg, x, y);
766	    RADEONRestoreCrtcBase(pScrn, info->ModeReg);
767	    break;
768	case 1:
769	    RADEONInitCrtc2Base(crtc, info->ModeReg, x, y);
770	    RADEONRestoreCrtc2Base(pScrn, info->ModeReg);
771	    break;
772	default:
773	    break;
774	}
775    }
776}
777#endif
778
779
780static xf86CrtcFuncsRec radeon_crtc_funcs = {
781    .dpms = radeon_crtc_dpms,
782    .save = NULL, /* XXX */
783    .restore = NULL, /* XXX */
784    .mode_fixup = radeon_crtc_mode_fixup,
785    .prepare = radeon_crtc_mode_prepare,
786    .mode_set = radeon_crtc_mode_set,
787    .commit = radeon_crtc_mode_commit,
788    .gamma_set = radeon_crtc_gamma_set,
789    .lock = radeon_crtc_lock,
790    .unlock = radeon_crtc_unlock,
791    .shadow_create = radeon_crtc_shadow_create,
792    .shadow_allocate = radeon_crtc_shadow_allocate,
793    .shadow_destroy = radeon_crtc_shadow_destroy,
794    .set_cursor_colors = radeon_crtc_set_cursor_colors,
795    .set_cursor_position = radeon_crtc_set_cursor_position,
796    .show_cursor = radeon_crtc_show_cursor,
797    .hide_cursor = radeon_crtc_hide_cursor,
798    .load_cursor_argb = radeon_crtc_load_cursor_argb,
799    .destroy = NULL, /* XXX */
800#if XF86_CRTC_VERSION >= 2
801    .set_origin = radeon_crtc_set_origin,
802#endif
803};
804
805void
806RADEONInitDispBandwidth(ScrnInfoPtr pScrn)
807{
808    RADEONInfoPtr info = RADEONPTR(pScrn);
809    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
810    DisplayModePtr mode1 = NULL, mode2 = NULL;
811    int pixel_bytes1 = info->CurrentLayout.pixel_bytes;
812    int pixel_bytes2 = info->CurrentLayout.pixel_bytes;
813
814    /* XXX fix me */
815    if (IS_DCE4_VARIANT)
816	return;
817
818    if (xf86_config->num_crtc == 2) {
819	if (xf86_config->crtc[1]->enabled &&
820	    xf86_config->crtc[0]->enabled) {
821	    mode1 = &xf86_config->crtc[0]->mode;
822	    mode2 = &xf86_config->crtc[1]->mode;
823	} else if (xf86_config->crtc[0]->enabled) {
824	    mode1 = &xf86_config->crtc[0]->mode;
825	} else if (xf86_config->crtc[1]->enabled) {
826	    mode2 = &xf86_config->crtc[1]->mode;
827	} else
828	    return;
829    } else {
830	if (info->IsPrimary)
831	    mode1 = &xf86_config->crtc[0]->mode;
832	else if (info->IsSecondary)
833	    mode2 = &xf86_config->crtc[0]->mode;
834	else if (xf86_config->crtc[0]->enabled)
835	    mode1 = &xf86_config->crtc[0]->mode;
836	else
837	    return;
838    }
839
840    if (IS_AVIVO_VARIANT)
841	RADEONInitDispBandwidthAVIVO(pScrn, mode1, pixel_bytes1, mode2, pixel_bytes2);
842    else
843	RADEONInitDispBandwidthLegacy(pScrn, mode1, pixel_bytes1, mode2, pixel_bytes2);
844}
845
846Bool RADEONAllocateControllers(ScrnInfoPtr pScrn, int mask)
847{
848    RADEONEntPtr pRADEONEnt = RADEONEntPriv(pScrn);
849    RADEONInfoPtr  info = RADEONPTR(pScrn);
850    int i;
851
852    if (!xf86ReturnOptValBool(info->Options, OPTION_NOACCEL, FALSE)) {
853	radeon_crtc_funcs.shadow_create = radeon_crtc_shadow_create;
854	radeon_crtc_funcs.shadow_allocate = radeon_crtc_shadow_allocate;
855	radeon_crtc_funcs.shadow_destroy = radeon_crtc_shadow_destroy;
856    }
857
858    if (mask & 1) {
859	if (pRADEONEnt->Controller[0])
860	    return TRUE;
861
862	pRADEONEnt->pCrtc[0] = xf86CrtcCreate(pScrn, &radeon_crtc_funcs);
863	if (!pRADEONEnt->pCrtc[0])
864	    return FALSE;
865
866	pRADEONEnt->Controller[0] = xnfcalloc(sizeof(RADEONCrtcPrivateRec), 1);
867	if (!pRADEONEnt->Controller[0])
868	    return FALSE;
869
870	pRADEONEnt->pCrtc[0]->driver_private = pRADEONEnt->Controller[0];
871	pRADEONEnt->Controller[0]->crtc_id = 0;
872	pRADEONEnt->Controller[0]->crtc_offset = 0;
873	pRADEONEnt->Controller[0]->initialized = FALSE;
874	if (info->allowColorTiling)
875	    pRADEONEnt->Controller[0]->can_tile = 1;
876	else
877	    pRADEONEnt->Controller[0]->can_tile = 0;
878	pRADEONEnt->Controller[0]->pll_id = -1;
879    }
880
881    if (mask & 2) {
882	if (!pRADEONEnt->HasCRTC2)
883	    return TRUE;
884
885	pRADEONEnt->pCrtc[1] = xf86CrtcCreate(pScrn, &radeon_crtc_funcs);
886	if (!pRADEONEnt->pCrtc[1])
887	    return FALSE;
888
889	pRADEONEnt->Controller[1] = xnfcalloc(sizeof(RADEONCrtcPrivateRec), 1);
890	if (!pRADEONEnt->Controller[1])
891	    {
892		xfree(pRADEONEnt->Controller[0]);
893		return FALSE;
894	    }
895
896	pRADEONEnt->pCrtc[1]->driver_private = pRADEONEnt->Controller[1];
897	pRADEONEnt->Controller[1]->crtc_id = 1;
898	if (IS_DCE4_VARIANT)
899	    pRADEONEnt->Controller[1]->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
900	else
901	    pRADEONEnt->Controller[1]->crtc_offset = AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
902	pRADEONEnt->Controller[1]->initialized = FALSE;
903	if (info->allowColorTiling)
904	    pRADEONEnt->Controller[1]->can_tile = 1;
905	else
906	    pRADEONEnt->Controller[1]->can_tile = 0;
907	pRADEONEnt->Controller[1]->pll_id = -1;
908    }
909
910    /* 6 crtcs on DCE4 chips */
911    if (IS_DCE4_VARIANT && ((mask & 3) == 3)) {
912	for (i = 2; i < RADEON_MAX_CRTC; i++) {
913	    pRADEONEnt->pCrtc[i] = xf86CrtcCreate(pScrn, &radeon_crtc_funcs);
914	    if (!pRADEONEnt->pCrtc[i])
915		return FALSE;
916
917	    pRADEONEnt->Controller[i] = xnfcalloc(sizeof(RADEONCrtcPrivateRec), 1);
918	    if (!pRADEONEnt->Controller[i])
919	    {
920		xfree(pRADEONEnt->Controller[i]);
921		return FALSE;
922	    }
923
924	    pRADEONEnt->pCrtc[i]->driver_private = pRADEONEnt->Controller[i];
925	    pRADEONEnt->Controller[i]->crtc_id = i;
926	    switch (i) {
927	    case 0:
928		pRADEONEnt->Controller[i]->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
929		break;
930	    case 1:
931		pRADEONEnt->Controller[i]->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
932		break;
933	    case 2:
934		pRADEONEnt->Controller[i]->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
935		break;
936	    case 3:
937		pRADEONEnt->Controller[i]->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
938		break;
939	    case 4:
940		pRADEONEnt->Controller[i]->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
941		break;
942	    case 5:
943		pRADEONEnt->Controller[i]->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
944		break;
945	    }
946	    pRADEONEnt->Controller[i]->initialized = FALSE;
947	    if (info->allowColorTiling)
948		pRADEONEnt->Controller[i]->can_tile = 1;
949	    else
950		pRADEONEnt->Controller[i]->can_tile = 0;
951	    pRADEONEnt->Controller[i]->pll_id = -1;
952	}
953    }
954
955    return TRUE;
956}
957
958/**
959 * In the current world order, there are lists of modes per output, which may
960 * or may not include the mode that was asked to be set by XFree86's mode
961 * selection.  Find the closest one, in the following preference order:
962 *
963 * - Equality
964 * - Closer in size to the requested mode, but no larger
965 * - Closer in refresh rate to the requested mode.
966 */
967DisplayModePtr
968RADEONCrtcFindClosestMode(xf86CrtcPtr crtc, DisplayModePtr pMode)
969{
970    ScrnInfoPtr	pScrn = crtc->scrn;
971    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
972    DisplayModePtr pBest = NULL, pScan = NULL;
973    int i;
974
975    /* Assume that there's only one output connected to the given CRTC. */
976    for (i = 0; i < xf86_config->num_output; i++)
977    {
978	xf86OutputPtr  output = xf86_config->output[i];
979	if (output->crtc == crtc && output->probed_modes != NULL)
980	{
981	    pScan = output->probed_modes;
982	    break;
983	}
984    }
985
986    /* If the pipe doesn't have any detected modes, just let the system try to
987     * spam the desired mode in.
988     */
989    if (pScan == NULL) {
990	RADEONCrtcPrivatePtr  radeon_crtc = crtc->driver_private;
991	xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
992		   "No crtc mode list for crtc %d,"
993		   "continuing with desired mode\n", radeon_crtc->crtc_id);
994	return pMode;
995    }
996
997    for (; pScan != NULL; pScan = pScan->next) {
998	assert(pScan->VRefresh != 0.0);
999
1000	/* If there's an exact match, we're done. */
1001	if (xf86ModesEqual(pScan, pMode)) {
1002	    pBest = pMode;
1003	    break;
1004	}
1005
1006	/* Reject if it's larger than the desired mode. */
1007	if (pScan->HDisplay > pMode->HDisplay ||
1008	    pScan->VDisplay > pMode->VDisplay)
1009	{
1010	    continue;
1011	}
1012
1013	if (pBest == NULL) {
1014	    pBest = pScan;
1015	    continue;
1016	}
1017
1018	/* Find if it's closer to the right size than the current best
1019	 * option.
1020	 */
1021	if ((pScan->HDisplay > pBest->HDisplay &&
1022	     pScan->VDisplay >= pBest->VDisplay) ||
1023	    (pScan->HDisplay >= pBest->HDisplay &&
1024	     pScan->VDisplay > pBest->VDisplay))
1025	{
1026	    pBest = pScan;
1027	    continue;
1028	}
1029
1030	/* Find if it's still closer to the right refresh than the current
1031	 * best resolution.
1032	 */
1033	if (pScan->HDisplay == pBest->HDisplay &&
1034	    pScan->VDisplay == pBest->VDisplay &&
1035	    (fabs(pScan->VRefresh - pMode->VRefresh) <
1036	     fabs(pBest->VRefresh - pMode->VRefresh))) {
1037	    pBest = pScan;
1038	}
1039    }
1040
1041    if (pBest == NULL) {
1042	xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1043		   "No suitable mode found to program for the pipe.\n"
1044		   "	continuing with desired mode %dx%d@%.1f\n",
1045		   pMode->HDisplay, pMode->VDisplay, pMode->VRefresh);
1046    } else if (!xf86ModesEqual(pBest, pMode)) {
1047      RADEONCrtcPrivatePtr  radeon_crtc = crtc->driver_private;
1048      int		    crtc = radeon_crtc->crtc_id;
1049      xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1050		   "Choosing pipe %d's mode %dx%d@%.1f instead of xf86 "
1051		   "mode %dx%d@%.1f\n", crtc,
1052		   pBest->HDisplay, pBest->VDisplay, pBest->VRefresh,
1053		   pMode->HDisplay, pMode->VDisplay, pMode->VRefresh);
1054	pMode = pBest;
1055    }
1056    return pMode;
1057}
1058
1059void
1060RADEONBlank(ScrnInfoPtr pScrn)
1061{
1062    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1063    xf86OutputPtr output;
1064    xf86CrtcPtr crtc;
1065    int o, c;
1066
1067    for (c = 0; c < xf86_config->num_crtc; c++) {
1068	crtc = xf86_config->crtc[c];
1069	for (o = 0; o < xf86_config->num_output; o++) {
1070	    output = xf86_config->output[o];
1071	    if (output->crtc != crtc)
1072		continue;
1073
1074	    output->funcs->dpms(output, DPMSModeOff);
1075	}
1076	crtc->funcs->dpms(crtc, DPMSModeOff);
1077    }
1078}
1079
1080void
1081RADEONUnblank(ScrnInfoPtr pScrn)
1082{
1083    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1084    xf86OutputPtr output;
1085    xf86CrtcPtr crtc;
1086    int o, c;
1087
1088    for (c = 0; c < xf86_config->num_crtc; c++) {
1089	crtc = xf86_config->crtc[c];
1090	if(!crtc->enabled)
1091		continue;
1092	crtc->funcs->dpms(crtc, DPMSModeOn);
1093	for (o = 0; o < xf86_config->num_output; o++) {
1094	    output = xf86_config->output[o];
1095	    if (output->crtc != crtc)
1096		continue;
1097
1098	    output->funcs->dpms(output, DPMSModeOn);
1099	}
1100    }
1101}
1102
1103Bool
1104RADEONSetTiling(ScrnInfoPtr pScrn)
1105{
1106    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1107    RADEONInfoPtr info = RADEONPTR(pScrn);
1108    RADEONCrtcPrivatePtr radeon_crtc;
1109    xf86CrtcPtr crtc;
1110    int c;
1111    int can_tile = 1;
1112    Bool changed = FALSE;
1113
1114    for (c = 0; c < xf86_config->num_crtc; c++) {
1115	crtc = xf86_config->crtc[c];
1116	radeon_crtc = crtc->driver_private;
1117
1118	if (crtc->enabled) {
1119	    if (!radeon_crtc->can_tile)
1120		can_tile = 0;
1121	}
1122    }
1123
1124    if (info->tilingEnabled != can_tile)
1125	changed = TRUE;
1126
1127#ifdef XF86DRI
1128    if (info->directRenderingEnabled && (info->tilingEnabled != can_tile)) {
1129	drm_radeon_sarea_t *pSAREAPriv;
1130	if (RADEONDRISetParam(pScrn, RADEON_SETPARAM_SWITCH_TILING, (can_tile ? 1 : 0)) < 0)
1131	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1132		       "[drm] failed changing tiling status\n");
1133	/* if this is called during ScreenInit() we don't have pScrn->pScreen yet */
1134	pSAREAPriv = DRIGetSAREAPrivate(screenInfo.screens[pScrn->scrnIndex]);
1135	info->tilingEnabled = pSAREAPriv->tiling_enabled ? TRUE : FALSE;
1136    }
1137#endif
1138
1139    return changed;
1140}
1141