radeon_exa.c revision b13dfe66
1/*
2 * Copyright 2005 Eric Anholt
3 * Copyright 2005 Benjamin Herrenschmidt
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Eric Anholt <anholt@FreeBSD.org>
27 *    Zack Rusin <zrusin@trolltech.com>
28 *    Benjamin Herrenschmidt <benh@kernel.crashing.org>
29 *
30 */
31
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#include "radeon.h"
37#include "radeon_reg.h"
38#include "r600_reg.h"
39#ifdef XF86DRI
40#include "radeon_drm.h"
41#endif
42#include "radeon_macros.h"
43#include "radeon_probe.h"
44#include "radeon_version.h"
45#include "radeon_exa_shared.h"
46
47#include "xf86.h"
48
49
50/***********************************************************************/
51#define RINFO_FROM_SCREEN(pScr) ScrnInfoPtr pScrn =  xf86Screens[pScr->myNum]; \
52    RADEONInfoPtr info   = RADEONPTR(pScrn)
53
54static struct {
55    int rop;
56    int pattern;
57} RADEON_ROP[] = {
58    { RADEON_ROP3_ZERO, RADEON_ROP3_ZERO }, /* GXclear        */
59    { RADEON_ROP3_DSa,  RADEON_ROP3_DPa  }, /* Gxand          */
60    { RADEON_ROP3_SDna, RADEON_ROP3_PDna }, /* GXandReverse   */
61    { RADEON_ROP3_S,    RADEON_ROP3_P    }, /* GXcopy         */
62    { RADEON_ROP3_DSna, RADEON_ROP3_DPna }, /* GXandInverted  */
63    { RADEON_ROP3_D,    RADEON_ROP3_D    }, /* GXnoop         */
64    { RADEON_ROP3_DSx,  RADEON_ROP3_DPx  }, /* GXxor          */
65    { RADEON_ROP3_DSo,  RADEON_ROP3_DPo  }, /* GXor           */
66    { RADEON_ROP3_DSon, RADEON_ROP3_DPon }, /* GXnor          */
67    { RADEON_ROP3_DSxn, RADEON_ROP3_PDxn }, /* GXequiv        */
68    { RADEON_ROP3_Dn,   RADEON_ROP3_Dn   }, /* GXinvert       */
69    { RADEON_ROP3_SDno, RADEON_ROP3_PDno }, /* GXorReverse    */
70    { RADEON_ROP3_Sn,   RADEON_ROP3_Pn   }, /* GXcopyInverted */
71    { RADEON_ROP3_DSno, RADEON_ROP3_DPno }, /* GXorInverted   */
72    { RADEON_ROP3_DSan, RADEON_ROP3_DPan }, /* GXnand         */
73    { RADEON_ROP3_ONE,  RADEON_ROP3_ONE  }  /* GXset          */
74};
75
76/* Compute log base 2 of val. */
77static __inline__ int
78RADEONLog2(int val)
79{
80	int bits;
81#if (defined __i386__ || defined __x86_64__) && (defined __GNUC__)
82	__asm volatile("bsrl	%1, %0"
83		: "=r" (bits)
84		: "c" (val)
85	);
86	return bits;
87#else
88	for (bits = 0; val != 0; val >>= 1, ++bits)
89		;
90	return bits - 1;
91#endif
92}
93
94static __inline__ uint32_t F_TO_DW(float val)
95{
96    union {
97	float f;
98	uint32_t l;
99    } tmp;
100    tmp.f = val;
101    return tmp.l;
102}
103
104
105/* Assumes that depth 15 and 16 can be used as depth 16, which is okay since we
106 * require src and dest datatypes to be equal.
107 */
108Bool RADEONGetDatatypeBpp(int bpp, uint32_t *type)
109{
110	switch (bpp) {
111	case 8:
112		*type = ATI_DATATYPE_CI8;
113		return TRUE;
114	case 16:
115		*type = ATI_DATATYPE_RGB565;
116		return TRUE;
117	case 24:
118		*type = ATI_DATATYPE_CI8;
119		return TRUE;
120	case 32:
121		*type = ATI_DATATYPE_ARGB8888;
122		return TRUE;
123	default:
124		RADEON_FALLBACK(("Unsupported bpp: %d\n", bpp));
125		return FALSE;
126	}
127}
128
129static Bool RADEONPixmapIsColortiled(PixmapPtr pPix)
130{
131    RINFO_FROM_SCREEN(pPix->drawable.pScreen);
132
133    /* This doesn't account for the back buffer, which we may want to wrap in
134     * a pixmap at some point for the purposes of DRI buffer moves.
135     */
136    if (info->tilingEnabled && exaGetPixmapOffset(pPix) == 0)
137	return TRUE;
138    else
139	return FALSE;
140}
141
142static Bool RADEONGetOffsetPitch(PixmapPtr pPix, int bpp, uint32_t *pitch_offset,
143				 unsigned int offset, unsigned int pitch)
144{
145	RINFO_FROM_SCREEN(pPix->drawable.pScreen);
146
147	if (pitch > 16320 || pitch % info->accel_state->exa->pixmapPitchAlign != 0)
148		RADEON_FALLBACK(("Bad pitch 0x%08x\n", pitch));
149
150	if (offset % info->accel_state->exa->pixmapOffsetAlign != 0)
151		RADEON_FALLBACK(("Bad offset 0x%08x\n", offset));
152
153	pitch = pitch >> 6;
154	*pitch_offset = (pitch << 22) | (offset >> 10);
155
156	/* If it's the front buffer, we've got to note that it's tiled? */
157	if (RADEONPixmapIsColortiled(pPix))
158		*pitch_offset |= RADEON_DST_TILE_MACRO;
159	return TRUE;
160}
161
162Bool RADEONGetPixmapOffsetPitch(PixmapPtr pPix, uint32_t *pitch_offset)
163{
164	uint32_t pitch, offset;
165	int bpp;
166
167	bpp = pPix->drawable.bitsPerPixel;
168	if (bpp == 24)
169		bpp = 8;
170
171	offset = radeonGetPixmapOffset(pPix);
172	pitch = exaGetPixmapPitch(pPix);
173
174	return RADEONGetOffsetPitch(pPix, bpp, pitch_offset, offset, pitch);
175}
176
177/**
178 * Returns whether the provided transform is affine.
179 *
180 * transform may be null.
181 */
182Bool radeon_transform_is_affine_or_scaled(PictTransformPtr t)
183{
184	if (t == NULL)
185		return TRUE;
186	/* the shaders don't handle scaling either */
187	return t->matrix[2][0] == 0 && t->matrix[2][1] == 0 && t->matrix[2][2] == IntToxFixed(1);
188}
189
190#if X_BYTE_ORDER == X_BIG_ENDIAN
191
192static unsigned long swapper_surfaces[6];
193
194static Bool RADEONPrepareAccess_BE(PixmapPtr pPix, int index)
195{
196    RINFO_FROM_SCREEN(pPix->drawable.pScreen);
197    unsigned char *RADEONMMIO = info->MMIO;
198    uint32_t offset = exaGetPixmapOffset(pPix);
199    int bpp, soff;
200    uint32_t size, flags;
201
202    /* Front buffer is always set with proper swappers */
203    if (offset == 0)
204        return TRUE;
205
206    /* If same bpp as front buffer, just do nothing as the main
207     * swappers will apply
208     */
209    bpp = pPix->drawable.bitsPerPixel;
210    if (bpp == pScrn->bitsPerPixel)
211        return TRUE;
212
213    /* We need to setup a separate swapper, let's request a
214     * surface. We need to align the size first
215     */
216    size = exaGetPixmapSize(pPix);
217    size = RADEON_ALIGN(size, RADEON_GPU_PAGE_SIZE);
218
219    /* Set surface to tiling disabled with appropriate swapper */
220    switch (bpp) {
221    case 16:
222        flags = RADEON_SURF_AP0_SWP_16BPP | RADEON_SURF_AP1_SWP_16BPP;
223	break;
224    case 32:
225        flags = RADEON_SURF_AP0_SWP_32BPP | RADEON_SURF_AP1_SWP_32BPP;
226	break;
227    default:
228        flags = 0;
229    }
230#if defined(XF86DRI)
231    if (info->directRenderingEnabled && info->allowColorTiling) {
232	struct drm_radeon_surface_alloc drmsurfalloc;
233	int rc;
234
235        drmsurfalloc.address = offset;
236        drmsurfalloc.size = size;
237	drmsurfalloc.flags = flags | 1; /* bogus pitch to please DRM */
238
239        rc = drmCommandWrite(info->dri->drmFD, DRM_RADEON_SURF_ALLOC,
240			     &drmsurfalloc, sizeof(drmsurfalloc));
241	if (rc < 0) {
242	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
243		       "drm: could not allocate surface for access"
244		       " swapper, err: %d!\n", rc);
245	    return FALSE;
246	}
247	swapper_surfaces[index] = offset;
248
249	return TRUE;
250    }
251#endif
252    soff = (index + 1) * 0x10;
253    OUTREG(RADEON_SURFACE0_INFO + soff, flags);
254    OUTREG(RADEON_SURFACE0_LOWER_BOUND + soff, offset);
255    OUTREG(RADEON_SURFACE0_UPPER_BOUND + soff, offset + size - 1);
256    swapper_surfaces[index] = offset;
257    return TRUE;
258}
259
260static void RADEONFinishAccess_BE(PixmapPtr pPix, int index)
261{
262    RINFO_FROM_SCREEN(pPix->drawable.pScreen);
263    unsigned char *RADEONMMIO = info->MMIO;
264    uint32_t offset = exaGetPixmapOffset(pPix);
265    int soff;
266
267    /* Front buffer is always set with proper swappers */
268    if (offset == 0)
269        return;
270
271    if (swapper_surfaces[index] == 0)
272        return;
273#if defined(XF86DRI)
274    if (info->directRenderingEnabled && info->allowColorTiling) {
275	struct drm_radeon_surface_free drmsurffree;
276
277	drmsurffree.address = offset;
278	drmCommandWrite(info->dri->drmFD, DRM_RADEON_SURF_FREE,
279			&drmsurffree, sizeof(drmsurffree));
280	swapper_surfaces[index] = 0;
281	return;
282    }
283#endif
284    soff = (index + 1) * 0x10;
285    OUTREG(RADEON_SURFACE0_INFO + soff, 0);
286    OUTREG(RADEON_SURFACE0_LOWER_BOUND + soff, 0);
287    OUTREG(RADEON_SURFACE0_UPPER_BOUND + soff, 0);
288    swapper_surfaces[index] = 0;
289}
290
291#endif /* X_BYTE_ORDER == X_BIG_ENDIAN */
292
293#ifdef XF86DRM_MODE
294Bool RADEONPrepareAccess_CS(PixmapPtr pPix, int index)
295{
296    ScreenPtr pScreen = pPix->drawable.pScreen;
297    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
298    RADEONInfoPtr info = RADEONPTR(pScrn);
299    struct radeon_exa_pixmap_priv *driver_priv;
300    uint32_t possible_domains = ~0U;
301    uint32_t current_domain = 0;
302#ifdef EXA_MIXED_PIXMAPS
303    Bool can_fail = !(pPix->drawable.bitsPerPixel < 8) &&
304	pPix != pScreen->GetScreenPixmap(pScreen) &&
305        (info->accel_state->exa->flags & EXA_MIXED_PIXMAPS);
306#else
307    Bool can_fail = FALSE;
308#endif
309    Bool flush = FALSE;
310    int ret;
311    uint32_t tiling_flags = 0, pitch = 0;
312
313#if X_BYTE_ORDER == X_BIG_ENDIAN
314    /* May need to handle byte swapping in DownloadFrom/UploadToScreen */
315    if (can_fail && pPix->drawable.bitsPerPixel > 8)
316	return FALSE;
317#endif
318
319    driver_priv = exaGetPixmapDriverPrivate(pPix);
320    if (!driver_priv)
321      return FALSE;
322
323    /* check if we are tiled */
324    ret = radeon_bo_get_tiling(driver_priv->bo, &tiling_flags, &pitch);
325    if (ret)
326	return FALSE;
327    /* untile in DFS/UTS */
328    if (tiling_flags & (RADEON_TILING_MACRO | RADEON_TILING_MICRO))
329	return FALSE;
330
331    /* if we have more refs than just the BO then flush */
332    if (radeon_bo_is_referenced_by_cs(driver_priv->bo, info->cs)) {
333	flush = TRUE;
334
335	if (can_fail) {
336	    possible_domains = radeon_bo_get_src_domain(driver_priv->bo);
337	    if (possible_domains == RADEON_GEM_DOMAIN_VRAM)
338		return FALSE; /* use DownloadFromScreen */
339	}
340    }
341
342    /* if the BO might end up in VRAM, prefer DownloadFromScreen */
343    if (can_fail && (possible_domains & RADEON_GEM_DOMAIN_VRAM)) {
344	radeon_bo_is_busy(driver_priv->bo, &current_domain);
345
346	if (current_domain & possible_domains) {
347	    if (current_domain == RADEON_GEM_DOMAIN_VRAM)
348		return FALSE;
349	} else if (possible_domains & RADEON_GEM_DOMAIN_VRAM)
350	    return FALSE;
351    }
352
353    if (flush)
354        radeon_cs_flush_indirect(pScrn);
355
356    /* flush IB */
357    ret = radeon_bo_map(driver_priv->bo, 1);
358    if (ret) {
359      FatalError("failed to map pixmap %d\n", ret);
360      return FALSE;
361    }
362    driver_priv->bo_mapped = TRUE;
363
364    pPix->devPrivate.ptr = driver_priv->bo->ptr;
365
366    return TRUE;
367}
368
369void RADEONFinishAccess_CS(PixmapPtr pPix, int index)
370{
371    struct radeon_exa_pixmap_priv *driver_priv;
372
373    driver_priv = exaGetPixmapDriverPrivate(pPix);
374    if (!driver_priv || !driver_priv->bo_mapped)
375        return;
376
377    radeon_bo_unmap(driver_priv->bo);
378    driver_priv->bo_mapped = FALSE;
379    pPix->devPrivate.ptr = NULL;
380}
381
382
383void *RADEONEXACreatePixmap(ScreenPtr pScreen, int size, int align)
384{
385    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
386    RADEONInfoPtr info = RADEONPTR(pScrn);
387    struct radeon_exa_pixmap_priv *new_priv;
388
389#ifdef EXA_MIXED_PIXMAPS
390    if (info->accel_state->exa->flags & EXA_MIXED_PIXMAPS) {
391        if (size != 0 && !info->exa_force_create &&
392	    info->exa_pixmaps == FALSE)
393            return NULL;
394    }
395#endif
396
397    new_priv = calloc(1, sizeof(struct radeon_exa_pixmap_priv));
398    if (!new_priv)
399	return NULL;
400
401    if (size == 0)
402	return new_priv;
403
404    new_priv->bo = radeon_bo_open(info->bufmgr, 0, size, align,
405				  RADEON_GEM_DOMAIN_VRAM, 0);
406    if (!new_priv->bo) {
407	free(new_priv);
408	ErrorF("Failed to alloc memory\n");
409	return NULL;
410    }
411
412    return new_priv;
413
414}
415
416static const unsigned MicroBlockTable[5][3][2] = {
417    /*linear  tiled   square-tiled */
418    {{32, 1}, {8, 4}, {0, 0}}, /*   8 bits per pixel */
419    {{16, 1}, {8, 2}, {4, 4}}, /*  16 bits per pixel */
420    {{ 8, 1}, {4, 2}, {0, 0}}, /*  32 bits per pixel */
421    {{ 4, 1}, {0, 0}, {2, 2}}, /*  64 bits per pixel */
422    {{ 2, 1}, {0, 0}, {0, 0}}  /* 128 bits per pixel */
423};
424
425/* Return true if macrotiling can be enabled */
426static Bool RADEONMacroSwitch(int width, int height, int bpp,
427                              uint32_t flags, Bool rv350_mode)
428{
429    unsigned tilew, tileh, microtiled, logbpp;
430
431    logbpp = RADEONLog2(bpp / 8);
432    if (logbpp > 4)
433        return 0;
434
435    microtiled = !!(flags & RADEON_TILING_MICRO);
436    tilew = MicroBlockTable[logbpp][microtiled][0] * 8;
437    tileh = MicroBlockTable[logbpp][microtiled][1] * 8;
438
439    /* See TX_FILTER1_n.MACRO_SWITCH. */
440    if (rv350_mode) {
441        return width >= tilew && height >= tileh;
442    } else {
443        return width > tilew && height > tileh;
444    }
445}
446
447void *RADEONEXACreatePixmap2(ScreenPtr pScreen, int width, int height,
448			     int depth, int usage_hint, int bitsPerPixel,
449			     int *new_pitch)
450{
451    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
452    RADEONInfoPtr info = RADEONPTR(pScrn);
453    struct radeon_exa_pixmap_priv *new_priv;
454    int pitch, base_align;
455    uint32_t size;
456    uint32_t tiling = 0;
457    int cpp = bitsPerPixel / 8;
458
459#ifdef EXA_MIXED_PIXMAPS
460    if (info->accel_state->exa->flags & EXA_MIXED_PIXMAPS) {
461	if (width != 0 && height != 0 && !info->exa_force_create &&
462	    info->exa_pixmaps == FALSE)
463            return NULL;
464    }
465#endif
466
467    if (usage_hint) {
468	if (info->allowColorTiling) {
469    	    if (usage_hint & RADEON_CREATE_PIXMAP_TILING_MACRO)
470 	   	tiling |= RADEON_TILING_MACRO;
471    	    if (usage_hint & RADEON_CREATE_PIXMAP_TILING_MICRO)
472                tiling |= RADEON_TILING_MICRO;
473	}
474    }
475
476    /* Small pixmaps must not be macrotiled on R300, hw cannot sample them
477     * correctly because samplers automatically switch to macrolinear. */
478    if (info->ChipFamily >= CHIP_FAMILY_R300 &&
479        info->ChipFamily <= CHIP_FAMILY_RS740 &&
480        (tiling & RADEON_TILING_MACRO) &&
481        !RADEONMacroSwitch(width, height, bitsPerPixel, tiling,
482                           info->ChipFamily >= CHIP_FAMILY_RV350)) {
483        tiling &= ~RADEON_TILING_MACRO;
484    }
485
486    height = RADEON_ALIGN(height, drmmode_get_height_align(pScrn, tiling));
487    pitch = RADEON_ALIGN(width, drmmode_get_pitch_align(pScrn, cpp, tiling)) * cpp;
488    base_align = drmmode_get_base_align(pScrn, cpp, tiling);
489    size = RADEON_ALIGN(height * pitch, RADEON_GPU_PAGE_SIZE);
490
491    new_priv = calloc(1, sizeof(struct radeon_exa_pixmap_priv));
492    if (!new_priv)
493	return NULL;
494
495    if (size == 0)
496	return new_priv;
497
498    *new_pitch = pitch;
499
500    new_priv->bo = radeon_bo_open(info->bufmgr, 0, size, base_align,
501				  RADEON_GEM_DOMAIN_VRAM, 0);
502    if (!new_priv->bo) {
503	free(new_priv);
504	ErrorF("Failed to alloc memory\n");
505	return NULL;
506    }
507
508    if (tiling)
509	radeon_bo_set_tiling(new_priv->bo, tiling, *new_pitch);
510
511    return new_priv;
512}
513
514void RADEONEXADestroyPixmap(ScreenPtr pScreen, void *driverPriv)
515{
516    struct radeon_exa_pixmap_priv *driver_priv = driverPriv;
517
518    if (!driverPriv)
519      return;
520
521    if (driver_priv->bo)
522	radeon_bo_unref(driver_priv->bo);
523    free(driverPriv);
524}
525
526struct radeon_bo *radeon_get_pixmap_bo(PixmapPtr pPix)
527{
528    struct radeon_exa_pixmap_priv *driver_priv;
529    driver_priv = exaGetPixmapDriverPrivate(pPix);
530    return driver_priv->bo;
531}
532
533void radeon_set_pixmap_bo(PixmapPtr pPix, struct radeon_bo *bo)
534{
535    struct radeon_exa_pixmap_priv *driver_priv;
536
537    driver_priv = exaGetPixmapDriverPrivate(pPix);
538    if (driver_priv) {
539	if (driver_priv->bo)
540	    radeon_bo_unref(driver_priv->bo);
541
542	radeon_bo_ref(bo);
543	driver_priv->bo = bo;
544    }
545}
546
547Bool RADEONEXAPixmapIsOffscreen(PixmapPtr pPix)
548{
549    struct radeon_exa_pixmap_priv *driver_priv;
550
551    driver_priv = exaGetPixmapDriverPrivate(pPix);
552
553    if (!driver_priv)
554       return FALSE;
555    if (driver_priv->bo)
556       return TRUE;
557    return FALSE;
558}
559#endif
560
561#define ENTER_DRAW(x) TRACE
562#define LEAVE_DRAW(x) TRACE
563/***********************************************************************/
564
565#define ACCEL_MMIO
566#define ACCEL_PREAMBLE()	unsigned char *RADEONMMIO = info->MMIO
567#define BEGIN_ACCEL(n)		RADEONWaitForFifo(pScrn, (n))
568#define OUT_ACCEL_REG(reg, val)	OUTREG(reg, val)
569#define OUT_ACCEL_REG_F(reg, val) OUTREG(reg, F_TO_DW(val))
570#define OUT_RELOC(x, read, write)            do {} while(0)
571#define FINISH_ACCEL()
572
573#ifdef RENDER
574#include "radeon_exa_render.c"
575#endif
576#include "radeon_exa_funcs.c"
577
578#undef ACCEL_MMIO
579#undef ACCEL_PREAMBLE
580#undef BEGIN_ACCEL
581#undef OUT_ACCEL_REG
582#undef OUT_ACCEL_REG_F
583#undef FINISH_ACCEL
584#undef OUT_RELOC
585
586#ifdef XF86DRI
587
588#define ACCEL_CP
589#define ACCEL_PREAMBLE()						\
590    RING_LOCALS;							\
591    RADEONCP_REFRESH(pScrn, info)
592#define BEGIN_ACCEL(n)		BEGIN_RING(2*(n))
593#define OUT_ACCEL_REG(reg, val)	OUT_RING_REG(reg, val)
594#define FINISH_ACCEL()		ADVANCE_RING()
595#define OUT_RELOC(x, read, write) OUT_RING_RELOC(x, read, write)
596
597#define OUT_RING_F(x) OUT_RING(F_TO_DW(x))
598
599#ifdef RENDER
600#include "radeon_exa_render.c"
601#endif
602#include "radeon_exa_funcs.c"
603
604#undef ACCEL_CP
605#undef ACCEL_PREAMBLE
606#undef BEGIN_ACCEL
607#undef OUT_ACCEL_REG
608#undef FINISH_ACCEL
609#undef OUT_RING_F
610
611#endif /* XF86DRI */
612
613/*
614 * Once screen->off_screen_base is set, this function
615 * allocates the remaining memory appropriately
616 */
617Bool RADEONSetupMemEXA (ScreenPtr pScreen)
618{
619    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
620    RADEONInfoPtr info = RADEONPTR(pScrn);
621    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
622    int cpp = info->CurrentLayout.pixel_bytes;
623    int screen_size;
624    int byteStride = pScrn->displayWidth * cpp;
625
626    if (info->accel_state->exa != NULL) {
627	xf86DrvMsg(pScreen->myNum, X_ERROR, "Memory map already initialized\n");
628	return FALSE;
629    }
630    info->accel_state->exa = exaDriverAlloc();
631    if (info->accel_state->exa == NULL)
632	return FALSE;
633
634    /* Need to adjust screen size for 16 line tiles, and then make it align to.
635     * the buffer alignment requirement.
636     */
637    if (info->allowColorTiling)
638	screen_size = RADEON_ALIGN(pScrn->virtualY, 16) * byteStride;
639    else
640	screen_size = pScrn->virtualY * byteStride;
641
642    info->accel_state->exa->memoryBase = info->FB;
643    info->accel_state->exa->memorySize = info->FbMapSize - info->FbSecureSize;
644    info->accel_state->exa->offScreenBase = screen_size;
645
646    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Allocating from a screen of %ld kb\n",
647	       info->accel_state->exa->memorySize / 1024);
648
649    /* Reserve static area for hardware cursor */
650    if (!xf86ReturnOptValBool(info->Options, OPTION_SW_CURSOR, FALSE)) {
651        int cursor_size = 64 * 4 * 64;
652        int align = IS_AVIVO_VARIANT ? 4096 : 256;
653        int c;
654
655        for (c = 0; c < xf86_config->num_crtc; c++) {
656            xf86CrtcPtr crtc = xf86_config->crtc[c];
657            RADEONCrtcPrivatePtr radeon_crtc = crtc->driver_private;
658
659            radeon_crtc->cursor_offset =
660                RADEON_ALIGN(info->accel_state->exa->offScreenBase, align);
661            info->accel_state->exa->offScreenBase = radeon_crtc->cursor_offset + cursor_size;
662
663            xf86DrvMsg(pScrn->scrnIndex, X_INFO,
664                       "Will use %d kb for hardware cursor %d at offset 0x%08x\n",
665                       (cursor_size * xf86_config->num_crtc) / 1024,
666                       c,
667                       (unsigned int)radeon_crtc->cursor_offset);
668        }
669    }
670
671#if defined(XF86DRI)
672    if (info->directRenderingEnabled) {
673	int depthCpp = (info->dri->depthBits - 8) / 4, l, next, depth_size;
674
675	info->dri->frontOffset = 0;
676	info->dri->frontPitch = pScrn->displayWidth;
677
678        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
679	       "Will use %d kb for front buffer at offset 0x%08x\n",
680	       screen_size / 1024, info->dri->frontOffset);
681	RADEONDRIAllocatePCIGARTTable(pScreen);
682
683	if (info->cardType==CARD_PCIE)
684	  xf86DrvMsg(pScrn->scrnIndex, X_INFO,
685		     "Will use %d kb for PCI GART at offset 0x%08x\n",
686		     info->dri->pciGartSize / 1024,
687		     (int)info->dri->pciGartOffset);
688
689	/* Reserve a static area for the back buffer the same size as the
690	 * visible screen.  XXX: This would be better initialized in ati_dri.c
691	 * when GLX is set up, but the offscreen memory manager's allocations
692	 * don't last through VT switches, while the kernel's understanding of
693	 * offscreen locations does.
694	 */
695	info->dri->backPitch = pScrn->displayWidth;
696	next = RADEON_ALIGN(info->accel_state->exa->offScreenBase, RADEON_GPU_PAGE_SIZE);
697	if (!info->dri->noBackBuffer &&
698	    next + screen_size <= info->accel_state->exa->memorySize)
699	{
700	    info->dri->backOffset = next;
701	    info->accel_state->exa->offScreenBase = next + screen_size;
702	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
703		       "Will use %d kb for back buffer at offset 0x%08x\n",
704		       screen_size / 1024, info->dri->backOffset);
705	}
706
707	/* Reserve the static depth buffer, and adjust pitch and height to
708	 * handle tiling.
709	 */
710	info->dri->depthPitch = RADEON_ALIGN(pScrn->displayWidth, 32);
711	depth_size = RADEON_ALIGN(pScrn->virtualY, 16) * info->dri->depthPitch * depthCpp;
712	next = RADEON_ALIGN(info->accel_state->exa->offScreenBase, RADEON_GPU_PAGE_SIZE);
713	if (next + depth_size <= info->accel_state->exa->memorySize)
714	{
715	    info->dri->depthOffset = next;
716	    info->accel_state->exa->offScreenBase = next + depth_size;
717	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
718		       "Will use %d kb for depth buffer at offset 0x%08x\n",
719		       depth_size / 1024, info->dri->depthOffset);
720	}
721
722	info->dri->textureSize *= (info->accel_state->exa->memorySize -
723				   info->accel_state->exa->offScreenBase) / 100;
724
725	l = RADEONLog2(info->dri->textureSize / RADEON_NR_TEX_REGIONS);
726	if (l < RADEON_LOG_TEX_GRANULARITY)
727	    l = RADEON_LOG_TEX_GRANULARITY;
728	info->dri->textureSize = (info->dri->textureSize >> l) << l;
729	if (info->dri->textureSize >= 512 * 1024) {
730	    info->dri->textureOffset = info->accel_state->exa->offScreenBase;
731	    info->accel_state->exa->offScreenBase += info->dri->textureSize;
732	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
733		       "Will use %d kb for textures at offset 0x%08x\n",
734		       info->dri->textureSize / 1024, info->dri->textureOffset);
735	} else {
736	    /* Minimum texture size is for 2 256x256x32bpp textures */
737	    info->dri->textureSize = 0;
738	}
739    } else
740#endif /* XF86DRI */
741    	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
742		       "Will use %d kb for front buffer at offset 0x%08x\n",
743		       screen_size / 1024, 0);
744
745    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
746	       "Will use %ld kb for X Server offscreen at offset 0x%08lx\n",
747	       (info->accel_state->exa->memorySize - info->accel_state->exa->offScreenBase) /
748	       1024, info->accel_state->exa->offScreenBase);
749
750    return TRUE;
751}
752
753#ifdef XF86DRI
754
755#ifndef ExaOffscreenMarkUsed
756extern void ExaOffscreenMarkUsed(PixmapPtr);
757#endif
758
759unsigned long long
760RADEONTexOffsetStart(PixmapPtr pPix)
761{
762    RINFO_FROM_SCREEN(pPix->drawable.pScreen);
763    unsigned long long offset;
764
765    if (exaGetPixmapDriverPrivate(pPix))
766	return -1;
767
768    exaMoveInPixmap(pPix);
769    ExaOffscreenMarkUsed(pPix);
770
771    offset = exaGetPixmapOffset(pPix);
772
773    if (offset > info->FbMapSize)
774	return ~0ULL;
775    else
776	return info->fbLocation + offset;
777}
778#endif
779