1de2362d3Smrg/*
2de2362d3Smrg * Copyright 2000 ATI Technologies Inc., Markham, Ontario, and
3de2362d3Smrg *                VA Linux Systems Inc., Fremont, California.
4de2362d3Smrg *
5de2362d3Smrg * All Rights Reserved.
6de2362d3Smrg *
7de2362d3Smrg * Permission is hereby granted, free of charge, to any person obtaining
8de2362d3Smrg * a copy of this software and associated documentation files (the
9de2362d3Smrg * "Software"), to deal in the Software without restriction, including
10de2362d3Smrg * without limitation on the rights to use, copy, modify, merge,
11de2362d3Smrg * publish, distribute, sublicense, and/or sell copies of the Software,
12de2362d3Smrg * and to permit persons to whom the Software is furnished to do so,
13de2362d3Smrg * subject to the following conditions:
14de2362d3Smrg *
15de2362d3Smrg * The above copyright notice and this permission notice (including the
16de2362d3Smrg * next paragraph) shall be included in all copies or substantial
17de2362d3Smrg * portions of the Software.
18de2362d3Smrg *
19de2362d3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20de2362d3Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21de2362d3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22de2362d3Smrg * NON-INFRINGEMENT.  IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR
23de2362d3Smrg * THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24de2362d3Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25de2362d3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26de2362d3Smrg * DEALINGS IN THE SOFTWARE.
27de2362d3Smrg */
28de2362d3Smrg
29de2362d3Smrg/*
30de2362d3Smrg * Authors:
31de2362d3Smrg *   Kevin E. Martin <martin@xfree86.org>
32de2362d3Smrg *   Rickard E. Faith <faith@valinux.com>
33de2362d3Smrg *   Alan Hourihane <alanh@fairlite.demon.co.uk>
34de2362d3Smrg *
35de2362d3Smrg */
36de2362d3Smrg
37de2362d3Smrg#ifndef _RADEON_H_
38de2362d3Smrg#define _RADEON_H_
39de2362d3Smrg
40de2362d3Smrg#include <stdlib.h>		/* For abs() */
41de2362d3Smrg#include <unistd.h>		/* For usleep() */
42de2362d3Smrg#include <sys/time.h>		/* For gettimeofday() */
43de2362d3Smrg
44de2362d3Smrg#include "config.h"
45de2362d3Smrg
46de2362d3Smrg#include "xf86str.h"
47de2362d3Smrg#include "compiler.h"
48de2362d3Smrg
49de2362d3Smrg				/* PCI support */
50de2362d3Smrg#include "xf86Pci.h"
51de2362d3Smrg
52de2362d3Smrg#include "exa.h"
53de2362d3Smrg
54de2362d3Smrg				/* Exa and Cursor Support */
55de2362d3Smrg#include "xf86Cursor.h"
56de2362d3Smrg
57de2362d3Smrg				/* DDC support */
58de2362d3Smrg#include "xf86DDC.h"
59de2362d3Smrg
60de2362d3Smrg				/* Xv support */
61de2362d3Smrg#include "xf86xv.h"
62de2362d3Smrg
63de2362d3Smrg#include "radeon_probe.h"
64de2362d3Smrg
65de2362d3Smrg				/* DRI support */
66de2362d3Smrg#include "xf86drm.h"
67de2362d3Smrg#include "radeon_drm.h"
68de2362d3Smrg
6918781e08Smrg#ifndef RADEON_GEM_NO_CPU_ACCESS
7018781e08Smrg#define RADEON_GEM_NO_CPU_ACCESS	(1 << 4)
7118781e08Smrg#endif
7218781e08Smrg
73de2362d3Smrg#ifdef DAMAGE
74de2362d3Smrg#include "damage.h"
75de2362d3Smrg#include "globals.h"
76de2362d3Smrg#endif
77de2362d3Smrg
78de2362d3Smrg#include "xf86Crtc.h"
79de2362d3Smrg#include "X11/Xatom.h"
80de2362d3Smrg
81de2362d3Smrg#include "radeon_bo.h"
82de2362d3Smrg#include "radeon_cs.h"
83de2362d3Smrg#include "radeon_dri2.h"
84de2362d3Smrg#include "drmmode_display.h"
85de2362d3Smrg#include "radeon_surface.h"
8639413783Smrg#include "radeon_bo_helper.h"
87de2362d3Smrg
88de2362d3Smrg				/* Render support */
89de2362d3Smrg#ifdef RENDER
90de2362d3Smrg#include "picturestr.h"
91de2362d3Smrg#endif
92de2362d3Smrg
93de2362d3Smrg#include "compat-api.h"
94de2362d3Smrg
95de2362d3Smrg#include "simple_list.h"
96de2362d3Smrg#include "atipcirename.h"
97de2362d3Smrg
9818781e08Smrgstruct _SyncFence;
9918781e08Smrg
10018781e08Smrg#ifndef HAVE_REGIONDUPLICATE
10118781e08Smrg
10218781e08Smrgstatic inline RegionPtr
10318781e08SmrgRegionDuplicate(RegionPtr pOld)
10418781e08Smrg{
10518781e08Smrg    RegionPtr pNew;
10618781e08Smrg
10718781e08Smrg    pNew = RegionCreate(&pOld->extents, 0);
10818781e08Smrg    if (!pNew)
10918781e08Smrg	return NULL;
11018781e08Smrg    if (!RegionCopy(pNew, pOld)) {
11118781e08Smrg	RegionDestroy(pNew);
11218781e08Smrg	return NULL;
11318781e08Smrg    }
11418781e08Smrg    return pNew;
11518781e08Smrg}
11618781e08Smrg
11718781e08Smrg#endif
11818781e08Smrg
119de2362d3Smrg#ifndef MAX
120de2362d3Smrg#define MAX(a,b) ((a)>(b)?(a):(b))
121de2362d3Smrg#endif
122de2362d3Smrg#ifndef MIN
123de2362d3Smrg#define MIN(a,b) ((a)>(b)?(b):(a))
124de2362d3Smrg#endif
125de2362d3Smrg
126de2362d3Smrg#if HAVE_BYTESWAP_H
127de2362d3Smrg#include <byteswap.h>
128de2362d3Smrg#elif defined(USE_SYS_ENDIAN_H)
129de2362d3Smrg#include <sys/endian.h>
130de2362d3Smrg#else
131de2362d3Smrg#define bswap_16(value)  \
132de2362d3Smrg        ((((value) & 0xff) << 8) | ((value) >> 8))
133de2362d3Smrg
134de2362d3Smrg#define bswap_32(value) \
135de2362d3Smrg        (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
136de2362d3Smrg        (uint32_t)bswap_16((uint16_t)((value) >> 16)))
137de2362d3Smrg
138de2362d3Smrg#define bswap_64(value) \
139de2362d3Smrg        (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
140de2362d3Smrg            << 32) | \
141de2362d3Smrg        (uint64_t)bswap_32((uint32_t)((value) >> 32)))
142de2362d3Smrg#endif
143de2362d3Smrg
144de2362d3Smrg#if X_BYTE_ORDER == X_BIG_ENDIAN
145de2362d3Smrg#define le32_to_cpu(x) bswap_32(x)
146de2362d3Smrg#define le16_to_cpu(x) bswap_16(x)
147de2362d3Smrg#define cpu_to_le32(x) bswap_32(x)
148de2362d3Smrg#define cpu_to_le16(x) bswap_16(x)
149de2362d3Smrg#else
150de2362d3Smrg#define le32_to_cpu(x) (x)
151de2362d3Smrg#define le16_to_cpu(x) (x)
152de2362d3Smrg#define cpu_to_le32(x) (x)
153de2362d3Smrg#define cpu_to_le16(x) (x)
154de2362d3Smrg#endif
155de2362d3Smrg
156de2362d3Smrg/* Provide substitutes for gcc's __FUNCTION__ on other compilers */
157de2362d3Smrg#if !defined(__GNUC__) && !defined(__FUNCTION__)
158de2362d3Smrg# define __FUNCTION__ __func__		/* C99 */
159de2362d3Smrg#endif
160de2362d3Smrg
161de2362d3Smrgtypedef enum {
16218781e08Smrg    OPTION_ACCEL,
163de2362d3Smrg    OPTION_SW_CURSOR,
164de2362d3Smrg    OPTION_PAGE_FLIP,
165de2362d3Smrg    OPTION_EXA_PIXMAPS,
166de2362d3Smrg    OPTION_COLOR_TILING,
167de2362d3Smrg    OPTION_COLOR_TILING_2D,
168de2362d3Smrg#ifdef RENDER
169de2362d3Smrg    OPTION_RENDER_ACCEL,
170de2362d3Smrg    OPTION_SUBPIXEL_ORDER,
171de2362d3Smrg#endif
172de2362d3Smrg    OPTION_ACCELMETHOD,
173de2362d3Smrg    OPTION_EXA_VSYNC,
174de2362d3Smrg    OPTION_ZAPHOD_HEADS,
17518781e08Smrg    OPTION_SWAPBUFFERS_WAIT,
17618781e08Smrg    OPTION_DELETE_DP12,
17718781e08Smrg    OPTION_DRI3,
17818781e08Smrg    OPTION_DRI,
17918781e08Smrg    OPTION_SHADOW_PRIMARY,
18018781e08Smrg    OPTION_TEAR_FREE,
181de2362d3Smrg} RADEONOpts;
182de2362d3Smrg
183de2362d3Smrg
1848bf5c682Smrgstatic inline ScreenPtr
185cd2eb4f7Smrgradeon_primary_screen(ScreenPtr screen)
1868bf5c682Smrg{
187cd2eb4f7Smrg    if (screen->current_primary)
188cd2eb4f7Smrg	return screen->current_primary;
1898bf5c682Smrg
1908bf5c682Smrg    return screen;
1918bf5c682Smrg}
1928bf5c682Smrg
193a7f02474Smrg#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) >= 25
194a7f02474Smrg#define slave_dst secondary_dst
195a7f02474Smrg#define master_pixmap primary_pixmap
196a7f02474Smrg#endif
1978bf5c682Smrgstatic inline ScreenPtr
198cd2eb4f7Smrgradeon_dirty_primary(PixmapDirtyUpdatePtr dirty)
1998bf5c682Smrg{
200cd2eb4f7Smrg    return radeon_primary_screen(dirty->secondary_dst->drawable.pScreen);
2018bf5c682Smrg}
2028bf5c682Smrg
2038bf5c682Smrgstatic inline DrawablePtr
2048bf5c682Smrgradeon_dirty_src_drawable(PixmapDirtyUpdatePtr dirty)
2058bf5c682Smrg{
2068bf5c682Smrg#ifdef HAS_DIRTYTRACKING_DRAWABLE_SRC
2078bf5c682Smrg    return dirty->src;
2083ed65abbSmrg#else
2098bf5c682Smrg    return &dirty->src->drawable;
21018781e08Smrg#endif
2118bf5c682Smrg}
2128bf5c682Smrg
2138bf5c682Smrgstatic inline Bool
2148bf5c682Smrgradeon_dirty_src_equals(PixmapDirtyUpdatePtr dirty, PixmapPtr pixmap)
2158bf5c682Smrg{
2168bf5c682Smrg    return radeon_dirty_src_drawable(dirty) == &pixmap->drawable;
2178bf5c682Smrg}
2188bf5c682Smrg
2190d16fef4Smrg
220de2362d3Smrg#define RADEON_VSYNC_TIMEOUT	20000 /* Maximum wait for VSYNC (in usecs) */
221de2362d3Smrg
222de2362d3Smrg/* Buffer are aligned on 4096 byte boundaries */
223de2362d3Smrg#define RADEON_GPU_PAGE_SIZE 4096
224de2362d3Smrg#define RADEON_BUFFER_ALIGN (RADEON_GPU_PAGE_SIZE - 1)
22518781e08Smrg
226de2362d3Smrg
227de2362d3Smrg#define xFixedToFloat(f) (((float) (f)) / 65536)
228de2362d3Smrg
229de2362d3Smrg#define RADEON_LOGLEVEL_DEBUG 4
230de2362d3Smrg
231de2362d3Smrg/* for Xv, outputs */
232de2362d3Smrg#define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
233de2362d3Smrg
234de2362d3Smrg/* Other macros */
235de2362d3Smrg#define RADEON_ALIGN(x,bytes) (((x) + ((bytes) - 1)) & ~((bytes) - 1))
236de2362d3Smrg#define RADEONPTR(pScrn)      ((RADEONInfoPtr)(pScrn)->driverPrivate)
237de2362d3Smrg
238de2362d3Smrg#define IS_RV100_VARIANT ((info->ChipFamily == CHIP_FAMILY_RV100)  ||  \
239de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RV200)  ||  \
240de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RS100)  ||  \
241de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RS200)  ||  \
242de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RV250)  ||  \
243de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RV280)  ||  \
244de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RS300))
245de2362d3Smrg
246de2362d3Smrg
247de2362d3Smrg#define IS_R300_VARIANT ((info->ChipFamily == CHIP_FAMILY_R300)  ||  \
248de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RV350) ||  \
249de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_R350)  ||  \
250de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RV380) ||  \
251de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_R420)  ||  \
252de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RV410) ||  \
253de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RS400) ||  \
254de2362d3Smrg        (info->ChipFamily == CHIP_FAMILY_RS480))
255de2362d3Smrg
256de2362d3Smrg#define IS_AVIVO_VARIANT ((info->ChipFamily >= CHIP_FAMILY_RV515))
257de2362d3Smrg
258de2362d3Smrg#define IS_DCE3_VARIANT ((info->ChipFamily >= CHIP_FAMILY_RV620))
259de2362d3Smrg
260de2362d3Smrg#define IS_DCE32_VARIANT ((info->ChipFamily >= CHIP_FAMILY_RV730))
261de2362d3Smrg
262de2362d3Smrg#define IS_DCE4_VARIANT ((info->ChipFamily >= CHIP_FAMILY_CEDAR))
263de2362d3Smrg
264de2362d3Smrg#define IS_DCE41_VARIANT ((info->ChipFamily >= CHIP_FAMILY_PALM))
265de2362d3Smrg
266de2362d3Smrg#define IS_DCE5_VARIANT ((info->ChipFamily >= CHIP_FAMILY_BARTS))
267de2362d3Smrg
268de2362d3Smrg#define IS_EVERGREEN_3D (info->ChipFamily >= CHIP_FAMILY_CEDAR)
269de2362d3Smrg
270de2362d3Smrg#define IS_R600_3D (info->ChipFamily >= CHIP_FAMILY_R600)
271de2362d3Smrg
272de2362d3Smrg#define IS_R500_3D ((info->ChipFamily == CHIP_FAMILY_RV515)  ||  \
273de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_R520)   ||  \
274de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RV530)  ||  \
275de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_R580)   ||  \
276de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RV560)  ||  \
277de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RV570))
278de2362d3Smrg
27918781e08Smrg/* RS6xx, RS740 are technically R4xx as well, but the
28018781e08Smrg * clipping hardware seems to follow the r3xx restrictions
28118781e08Smrg */
282de2362d3Smrg#define IS_R400_3D ((info->ChipFamily == CHIP_FAMILY_R420)  ||  \
28318781e08Smrg	(info->ChipFamily == CHIP_FAMILY_RV410))
284de2362d3Smrg
285de2362d3Smrg#define IS_R300_3D ((info->ChipFamily == CHIP_FAMILY_R300)  ||  \
286de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RV350) ||  \
287de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_R350)  ||  \
288de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RV380) ||  \
289de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_R420)  ||  \
290de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RV410) ||  \
291de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RS690) ||  \
292de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RS600) ||  \
293de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RS740) ||  \
294de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RS400) ||  \
295de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RS480))
296de2362d3Smrg
297de2362d3Smrg#define IS_R200_3D ((info->ChipFamily == CHIP_FAMILY_RV250) || \
298de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RV280) || \
299de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_RS300) || \
300de2362d3Smrg	(info->ChipFamily == CHIP_FAMILY_R200))
301de2362d3Smrg
30218781e08Smrg#define CURSOR_WIDTH	64
30318781e08Smrg#define CURSOR_HEIGHT	64
3040d16fef4Smrg
30518781e08Smrg#define CURSOR_WIDTH_CIK	128
30618781e08Smrg#define CURSOR_HEIGHT_CIK	128
3070d16fef4Smrg
30818781e08Smrg#ifdef USE_GLAMOR
3090d16fef4Smrg
31018781e08Smrgstruct radeon_pixmap {
31118781e08Smrg	uint_fast32_t gpu_read;
31218781e08Smrg	uint_fast32_t gpu_write;
3130d16fef4Smrg
31439413783Smrg	struct radeon_buffer *bo;
3158bf5c682Smrg	struct drmmode_fb *fb;
3160d2a5547Smrg	Bool fb_failed;
31718781e08Smrg
31818781e08Smrg	uint32_t tiling_flags;
31918781e08Smrg
32018781e08Smrg	/* GEM handle for glamor-only pixmaps shared via DRI3 */
32118781e08Smrg	Bool handle_valid;
32218781e08Smrg	uint32_t handle;
32318781e08Smrg};
32418781e08Smrg
32518781e08Smrgextern DevPrivateKeyRec glamor_pixmap_index;
32618781e08Smrg
32718781e08Smrgstatic inline struct radeon_pixmap *radeon_get_pixmap_private(PixmapPtr pixmap)
32818781e08Smrg{
32918781e08Smrg	return dixGetPrivate(&pixmap->devPrivates, &glamor_pixmap_index);
33018781e08Smrg}
33118781e08Smrg
33218781e08Smrgstatic inline void radeon_set_pixmap_private(PixmapPtr pixmap, struct radeon_pixmap *priv)
33318781e08Smrg{
33418781e08Smrg	dixSetPrivate(&pixmap->devPrivates, &glamor_pixmap_index, priv);
33518781e08Smrg}
3360d16fef4Smrg
33718781e08Smrg#endif /* USE_GLAMOR */
3380d16fef4Smrg
3390d16fef4Smrg
340de2362d3Smrgstruct radeon_exa_pixmap_priv {
34139413783Smrg    struct radeon_buffer *bo;
3428bf5c682Smrg    struct drmmode_fb *fb;
343de2362d3Smrg    uint32_t tiling_flags;
344de2362d3Smrg    struct radeon_surface surface;
345de2362d3Smrg    Bool bo_mapped;
34618781e08Smrg    Bool shared;
347de2362d3Smrg};
348de2362d3Smrg
349de2362d3Smrg#define RADEON_2D_EXA_COPY 1
350de2362d3Smrg#define RADEON_2D_EXA_SOLID 2
351de2362d3Smrg
352de2362d3Smrgstruct radeon_2d_state {
353de2362d3Smrg    int op; //
354de2362d3Smrg    uint32_t dst_pitch_offset;
355de2362d3Smrg    uint32_t src_pitch_offset;
356de2362d3Smrg    uint32_t dp_gui_master_cntl;
357de2362d3Smrg    uint32_t dp_cntl;
358de2362d3Smrg    uint32_t dp_write_mask;
359de2362d3Smrg    uint32_t dp_brush_frgd_clr;
360de2362d3Smrg    uint32_t dp_brush_bkgd_clr;
361de2362d3Smrg    uint32_t dp_src_frgd_clr;
362de2362d3Smrg    uint32_t dp_src_bkgd_clr;
363de2362d3Smrg    uint32_t default_sc_bottom_right;
36418781e08Smrg    uint32_t dst_domain;
365de2362d3Smrg    struct radeon_bo *dst_bo;
366de2362d3Smrg    struct radeon_bo *src_bo;
367de2362d3Smrg};
368de2362d3Smrg
369de2362d3Smrg#define DMA_BO_FREE_TIME 1000
370de2362d3Smrg
371de2362d3Smrgstruct radeon_dma_bo {
372de2362d3Smrg    struct radeon_dma_bo *next, *prev;
373de2362d3Smrg    struct radeon_bo  *bo;
374de2362d3Smrg    int expire_counter;
375de2362d3Smrg};
376de2362d3Smrg
377de2362d3Smrgstruct r600_accel_object {
378de2362d3Smrg    uint32_t pitch;
379de2362d3Smrg    uint32_t width;
380de2362d3Smrg    uint32_t height;
381de2362d3Smrg    int bpp;
382de2362d3Smrg    uint32_t domain;
383de2362d3Smrg    struct radeon_bo *bo;
384de2362d3Smrg    uint32_t tiling_flags;
385de2362d3Smrg    struct radeon_surface *surface;
386de2362d3Smrg};
387de2362d3Smrg
388de2362d3Smrgstruct radeon_vbo_object {
389de2362d3Smrg    int               vb_offset;
390de2362d3Smrg    int               vb_total;
391de2362d3Smrg    uint32_t          vb_size;
392de2362d3Smrg    uint32_t          vb_op_vert_size;
393de2362d3Smrg    int32_t           vb_start_op;
394de2362d3Smrg    struct radeon_bo *vb_bo;
395de2362d3Smrg    unsigned          verts_per_op;
396de2362d3Smrg};
397de2362d3Smrg
398de2362d3Smrgstruct radeon_accel_state {
39918781e08Smrg
400de2362d3Smrg				/* Saved values for ScreenToScreenCopy */
401de2362d3Smrg    int               xdir;
402de2362d3Smrg    int               ydir;
403de2362d3Smrg
404de2362d3Smrg    /* render accel */
405de2362d3Smrg    unsigned short    texW[2];
406de2362d3Smrg    unsigned short    texH[2];
407de2362d3Smrg    Bool              XInited3D; /* X itself has the 3D context */
408de2362d3Smrg    int               num_gb_pipes;
409de2362d3Smrg    Bool              has_tcl;
410de2362d3Smrg    Bool              allowHWDFS;
411de2362d3Smrg
412de2362d3Smrg    /* EXA */
413de2362d3Smrg    ExaDriverPtr      exa;
414de2362d3Smrg    int               exaSyncMarker;
415de2362d3Smrg    int               exaMarkerSynced;
416de2362d3Smrg    int               engineMode;
417de2362d3Smrg#define EXA_ENGINEMODE_UNKNOWN 0
418de2362d3Smrg#define EXA_ENGINEMODE_2D      1
419de2362d3Smrg#define EXA_ENGINEMODE_3D      2
420de2362d3Smrg
421de2362d3Smrg    int               composite_op;
422de2362d3Smrg    PicturePtr        dst_pic;
423de2362d3Smrg    PicturePtr        msk_pic;
424de2362d3Smrg    PicturePtr        src_pic;
425de2362d3Smrg    PixmapPtr         dst_pix;
426de2362d3Smrg    PixmapPtr         msk_pix;
427de2362d3Smrg    PixmapPtr         src_pix;
428de2362d3Smrg    Bool              is_transform[2];
429de2362d3Smrg    PictTransform     *transform[2];
430de2362d3Smrg    /* Whether we are tiling horizontally and vertically */
431de2362d3Smrg    Bool              need_src_tile_x;
432de2362d3Smrg    Bool              need_src_tile_y;
433de2362d3Smrg    /* Size of tiles ... set to 65536x65536 if not tiling in that direction */
434de2362d3Smrg    Bool              src_tile_width;
435de2362d3Smrg    Bool              src_tile_height;
436de2362d3Smrg    uint32_t          *draw_header;
437de2362d3Smrg    unsigned          vtx_count;
438de2362d3Smrg    unsigned          num_vtx;
439de2362d3Smrg    Bool              vsync;
440de2362d3Smrg
441de2362d3Smrg    struct radeon_vbo_object vbo;
442de2362d3Smrg    struct radeon_vbo_object cbuf;
443de2362d3Smrg
444de2362d3Smrg    /* where to discard IB from if we cancel operation */
445de2362d3Smrg    uint32_t          ib_reset_op;
446de2362d3Smrg    struct radeon_dma_bo bo_free;
447de2362d3Smrg    struct radeon_dma_bo bo_wait;
448de2362d3Smrg    struct radeon_dma_bo bo_reserved;
449de2362d3Smrg    Bool use_vbos;
450de2362d3Smrg    void (*finish_op)(ScrnInfoPtr, int);
451de2362d3Smrg    // shader storage
452de2362d3Smrg    struct radeon_bo  *shaders_bo;
453de2362d3Smrg    uint32_t          solid_vs_offset;
454de2362d3Smrg    uint32_t          solid_ps_offset;
455de2362d3Smrg    uint32_t          copy_vs_offset;
456de2362d3Smrg    uint32_t          copy_ps_offset;
457de2362d3Smrg    uint32_t          comp_vs_offset;
458de2362d3Smrg    uint32_t          comp_ps_offset;
459de2362d3Smrg    uint32_t          xv_vs_offset;
460de2362d3Smrg    uint32_t          xv_ps_offset;
461de2362d3Smrg    // shader consts
462de2362d3Smrg    uint32_t          solid_vs_const_offset;
463de2362d3Smrg    uint32_t          solid_ps_const_offset;
464de2362d3Smrg    uint32_t          copy_vs_const_offset;
465de2362d3Smrg    uint32_t          copy_ps_const_offset;
466de2362d3Smrg    uint32_t          comp_vs_const_offset;
467de2362d3Smrg    uint32_t          comp_ps_const_offset;
468de2362d3Smrg    uint32_t          comp_mask_ps_const_offset;
469de2362d3Smrg    uint32_t          xv_vs_const_offset;
470de2362d3Smrg    uint32_t          xv_ps_const_offset;
471de2362d3Smrg
472de2362d3Smrg    //size/addr stuff
473de2362d3Smrg    struct r600_accel_object src_obj[2];
474de2362d3Smrg    struct r600_accel_object dst_obj;
475de2362d3Smrg    uint32_t          src_size[2];
476de2362d3Smrg    uint32_t          dst_size;
477de2362d3Smrg
478de2362d3Smrg    uint32_t          vs_size;
479de2362d3Smrg    uint64_t          vs_mc_addr;
480de2362d3Smrg    uint32_t          ps_size;
481de2362d3Smrg    uint64_t          ps_mc_addr;
482de2362d3Smrg
483de2362d3Smrg    // solid/copy
48418781e08Smrg    void *copy_area;
485de2362d3Smrg    struct radeon_bo  *copy_area_bo;
486de2362d3Smrg    Bool              same_surface;
487de2362d3Smrg    int               rop;
488de2362d3Smrg    uint32_t          planemask;
489de2362d3Smrg    uint32_t          fg;
490de2362d3Smrg
491de2362d3Smrg    // composite
492de2362d3Smrg    Bool              component_alpha;
493de2362d3Smrg    Bool              src_alpha;
494de2362d3Smrg    // vline
495de2362d3Smrg    xf86CrtcPtr       vline_crtc;
496de2362d3Smrg    int               vline_y1;
497de2362d3Smrg    int               vline_y2;
498de2362d3Smrg
49918781e08Smrg    Bool              force;
50018781e08Smrg};
501de2362d3Smrg
50218781e08Smrgstruct radeon_client_priv {
50318781e08Smrg    uint_fast32_t     needs_flush;
5047314432eSmrg};
5057314432eSmrg
5068bf5c682Smrgstruct radeon_device_priv {
5078bf5c682Smrg    CursorPtr cursor;
5088bf5c682Smrg    Bool sprite_visible;
5098bf5c682Smrg};
5108bf5c682Smrg
5118bf5c682Smrgextern DevScreenPrivateKeyRec radeon_device_private_key;
5128bf5c682Smrg
513de2362d3Smrgtypedef struct {
514de2362d3Smrg    EntityInfoPtr     pEnt;
515de2362d3Smrg    pciVideoPtr       PciInfo;
516de2362d3Smrg    int               Chipset;
517de2362d3Smrg    RADEONChipFamily  ChipFamily;
51818781e08Smrg
5198bf5c682Smrg    Bool              (*CloseScreen)(ScreenPtr pScreen);
520de2362d3Smrg
521de2362d3Smrg    void              (*BlockHandler)(BLOCKHANDLER_ARGS_DECL);
522de2362d3Smrg
52318781e08Smrg    void              (*CreateFence) (ScreenPtr pScreen, struct _SyncFence *pFence,
52418781e08Smrg				      Bool initially_triggered);
5250d16fef4Smrg
526de2362d3Smrg    int               pix24bpp;         /* Depth of pixmap for 24bpp fb      */
527de2362d3Smrg    Bool              dac6bits;         /* Use 6 bit DAC?                    */
528de2362d3Smrg
52918781e08Smrg    int               pixel_bytes;
530de2362d3Smrg
531de2362d3Smrg    Bool              directRenderingEnabled;
532de2362d3Smrg    struct radeon_dri2  dri2;
533de2362d3Smrg
534de2362d3Smrg    /* accel */
535de2362d3Smrg    Bool              RenderAccel; /* Render */
536de2362d3Smrg    Bool              allowColorTiling;
537de2362d3Smrg    Bool              allowColorTiling2D;
53818781e08Smrg    int               callback_event_type;
53918781e08Smrg    uint_fast32_t     gpu_flushed;
54018781e08Smrg    uint_fast32_t     gpu_synced;
541de2362d3Smrg    struct radeon_accel_state *accel_state;
54218781e08Smrg    PixmapPtr         fbcon_pixmap;
543de2362d3Smrg    Bool              accelOn;
54418781e08Smrg    Bool              use_glamor;
54518781e08Smrg    Bool              shadow_primary;
5463ed65abbSmrg    int               tear_free;
547de2362d3Smrg    Bool	      exa_pixmaps;
548de2362d3Smrg    Bool              exa_force_create;
549de2362d3Smrg    XF86ModReqInfo    exaReq;
55018781e08Smrg    Bool              is_fast_fb; /* use direct mapping for fast fb access */
551de2362d3Smrg
552de2362d3Smrg    unsigned int xv_max_width;
553de2362d3Smrg    unsigned int xv_max_height;
554de2362d3Smrg
555de2362d3Smrg    /* general */
556de2362d3Smrg    OptionInfoPtr     Options;
557de2362d3Smrg
55818781e08Smrg    DisplayModePtr currentMode;
559de2362d3Smrg
560de2362d3Smrg    CreateScreenResourcesProcPtr CreateScreenResources;
56118781e08Smrg    CreateWindowProcPtr CreateWindow;
5623ed65abbSmrg    WindowExposuresProcPtr WindowExposures;
56339413783Smrg    miPointerSpriteFuncPtr SpriteFuncs;
5648bf5c682Smrg
5658bf5c682Smrg    /* Number of SW cursors currently visible on this screen */
5668bf5c682Smrg    int sprites_visible;
567de2362d3Smrg
568446f62d6Smrg    int instance_id;
569de2362d3Smrg
570de2362d3Smrg    Bool              r600_shadow_fb;
571de2362d3Smrg    void *fb_shadow;
572de2362d3Smrg
573de2362d3Smrg    void (*reemit_current2d)(ScrnInfoPtr pScrn, int op); // emit the current 2D state into the IB
574de2362d3Smrg    struct radeon_2d_state state_2d;
57539413783Smrg    struct radeon_buffer *front_buffer;
576de2362d3Smrg    struct radeon_bo_manager *bufmgr;
577de2362d3Smrg    struct radeon_cs_manager *csm;
578de2362d3Smrg    struct radeon_cs *cs;
579de2362d3Smrg
580de2362d3Smrg    uint64_t vram_size;
581de2362d3Smrg    uint64_t gart_size;
582de2362d3Smrg    drmmode_rec drmmode;
58318781e08Smrg    Bool drmmode_inited;
584de2362d3Smrg    /* r6xx+ tile config */
585de2362d3Smrg    Bool have_tiling_info;
586de2362d3Smrg    uint32_t tile_config;
587de2362d3Smrg    int group_bytes;
588de2362d3Smrg    int num_channels;
589de2362d3Smrg    int num_banks;
590de2362d3Smrg    int r7xx_bank_op;
591de2362d3Smrg    struct radeon_surface_manager *surf_man;
592de2362d3Smrg    struct radeon_surface front_surface;
593de2362d3Smrg
594de2362d3Smrg    /* Xv bicubic filtering */
595de2362d3Smrg    struct radeon_bo *bicubic_bo;
59618781e08Smrg
597de2362d3Smrg    /* kms pageflipping */
598de2362d3Smrg    Bool allowPageFlip;
599de2362d3Smrg
600de2362d3Smrg    /* Perform vsync'ed SwapBuffers? */
601de2362d3Smrg    Bool swapBuffersWait;
602de2362d3Smrg
60318781e08Smrg    /* cursor size */
60418781e08Smrg    int cursor_w;
60518781e08Smrg    int cursor_h;
6060d16fef4Smrg
60718781e08Smrg    /* If bit n of this field is set, xf86_config->crtc[n] currently can't
60818781e08Smrg     * use the HW cursor
60918781e08Smrg     */
61018781e08Smrg    unsigned hwcursor_disabled;
61118781e08Smrg
61218781e08Smrg#ifdef USE_GLAMOR
61339413783Smrg    struct gbm_device *gbm;
61439413783Smrg
61518781e08Smrg    struct {
61618781e08Smrg	CreateGCProcPtr SavedCreateGC;
61718781e08Smrg	RegionPtr (*SavedCopyArea)(DrawablePtr, DrawablePtr, GCPtr, int, int,
61818781e08Smrg				   int, int, int, int);
61918781e08Smrg	void (*SavedPolyFillRect)(DrawablePtr, GCPtr, int, xRectangle*);
62018781e08Smrg	CloseScreenProcPtr SavedCloseScreen;
62118781e08Smrg	GetImageProcPtr SavedGetImage;
62218781e08Smrg	GetSpansProcPtr SavedGetSpans;
62318781e08Smrg	CreatePixmapProcPtr SavedCreatePixmap;
62418781e08Smrg	DestroyPixmapProcPtr SavedDestroyPixmap;
62518781e08Smrg	CopyWindowProcPtr SavedCopyWindow;
62618781e08Smrg	ChangeWindowAttributesProcPtr SavedChangeWindowAttributes;
62718781e08Smrg	BitmapToRegionProcPtr SavedBitmapToRegion;
62818781e08Smrg#ifdef RENDER
62918781e08Smrg	CompositeProcPtr SavedComposite;
63018781e08Smrg	TrianglesProcPtr SavedTriangles;
63118781e08Smrg	GlyphsProcPtr SavedGlyphs;
63218781e08Smrg	TrapezoidsProcPtr SavedTrapezoids;
63318781e08Smrg	AddTrapsProcPtr SavedAddTraps;
63418781e08Smrg	UnrealizeGlyphProcPtr SavedUnrealizeGlyph;
63518781e08Smrg#endif
63618781e08Smrg	SharePixmapBackingProcPtr SavedSharePixmapBacking;
63718781e08Smrg	SetSharedPixmapBackingProcPtr SavedSetSharedPixmapBacking;
63818781e08Smrg    } glamor;
63918781e08Smrg#endif /* USE_GLAMOR */
6408bf5c682Smrg
6418bf5c682Smrg    xf86CrtcFuncsRec drmmode_crtc_funcs;
64218781e08Smrg} RADEONInfoRec, *RADEONInfoPtr;
643de2362d3Smrg
644de2362d3Smrg/* radeon_accel.c */
645de2362d3Smrgextern Bool RADEONAccelInit(ScreenPtr pScreen);
646de2362d3Smrgextern void RADEONEngineInit(ScrnInfoPtr pScrn);
647de2362d3Smrgextern void  RADEONCopySwap(uint8_t *dst, uint8_t *src, unsigned int size, int swap);
648de2362d3Smrgextern void RADEONInit3DEngine(ScrnInfoPtr pScrn);
649de2362d3Smrgextern int radeon_cs_space_remaining(ScrnInfoPtr pScrn);
650de2362d3Smrg
6518bf5c682Smrg/* radeon_bo_helper.c */
65239413783Smrgextern Bool
65339413783Smrgradeon_surface_initialize(RADEONInfoPtr info, struct radeon_surface *surface,
65439413783Smrg			  int width, int height, int cpp, uint32_t tiling_flags,
65539413783Smrg			  int usage_hint);
65639413783Smrg
6578bf5c682Smrgextern Bool radeon_get_pixmap_handle(PixmapPtr pixmap, uint32_t *handle);
6588bf5c682Smrg
6597821949aSmrg/* radeon_commonfuncs.c */
66018781e08Smrgextern void RADEONWaitForVLine(ScrnInfoPtr pScrn, PixmapPtr pPix,
66118781e08Smrg			       xf86CrtcPtr crtc, int start, int stop);
66218781e08Smrg
663de2362d3Smrg
664de2362d3Smrg/* radeon_exa.c */
665de2362d3Smrgextern unsigned eg_tile_split(unsigned tile_split);
666de2362d3Smrgextern Bool radeon_transform_is_affine_or_scaled(PictTransformPtr t);
667de2362d3Smrg
668de2362d3Smrg/* radeon_exa_funcs.c */
66918781e08Smrgextern Bool RADEONDrawInit(ScreenPtr pScreen);
670de2362d3Smrgextern Bool R600DrawInit(ScreenPtr pScreen);
671de2362d3Smrgextern Bool R600LoadShaders(ScrnInfoPtr pScrn);
672de2362d3Smrgextern Bool EVERGREENDrawInit(ScreenPtr pScreen);
673de2362d3Smrg
674de2362d3Smrg/* radeon_exa.c */
675de2362d3Smrgextern Bool RADEONGetDatatypeBpp(int bpp, uint32_t *type);
676de2362d3Smrgextern Bool RADEONGetPixmapOffsetPitch(PixmapPtr pPix,
677de2362d3Smrg				       uint32_t *pitch_offset);
678de2362d3Smrg
67918781e08Smrg/* radeon_dri3.c */
68018781e08SmrgBool radeon_dri3_screen_init(ScreenPtr screen);
68118781e08Smrg
68218781e08Smrg/* radeon_kms.c */
6838bf5c682SmrgBool radeon_scanout_do_update(xf86CrtcPtr xf86_crtc, int scanout_id,
68439413783Smrg			      PixmapPtr src_pix, BoxRec extents);
6858bf5c682Smrgvoid RADEONWindowExposures_oneshot(WindowPtr pWin, RegionPtr pRegion
6868bf5c682Smrg#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,16,99,901,0)
6878bf5c682Smrg				   , RegionPtr pBSRegion
6888bf5c682Smrg#endif
6898bf5c682Smrg				   );
69018781e08Smrg
69118781e08Smrg/* radeon_present.c */
69218781e08SmrgBool radeon_present_screen_init(ScreenPtr screen);
69318781e08Smrg
69418781e08Smrg/* radeon_sync.c */
69518781e08Smrgextern Bool radeon_sync_init(ScreenPtr screen);
69618781e08Smrgextern void radeon_sync_close(ScreenPtr screen);
6970d16fef4Smrg
698de2362d3Smrg/* radeon_video.c */
699de2362d3Smrgextern void RADEONInitVideo(ScreenPtr pScreen);
700de2362d3Smrgextern void RADEONResetVideo(ScrnInfoPtr pScrn);
701de2362d3Smrgextern Bool radeon_load_bicubic_texture(ScrnInfoPtr pScrn);
702de2362d3Smrgextern xf86CrtcPtr radeon_pick_best_crtc(ScrnInfoPtr pScrn,
70318781e08Smrg					 Bool consider_disabled,
704de2362d3Smrg					 int x1, int x2, int y1, int y2);
705de2362d3Smrg
706de2362d3Smrgextern void radeon_cs_flush_indirect(ScrnInfoPtr pScrn);
707de2362d3Smrgextern void radeon_ddx_cs_start(ScrnInfoPtr pScrn,
708de2362d3Smrg				int num, const char *file,
709de2362d3Smrg				const char *func, int line);
71018781e08Smrgvoid radeon_kms_update_vram_limit(ScrnInfoPtr pScrn, uint32_t new_fb_size);
71118781e08Smrgextern RADEONEntPtr RADEONEntPriv(ScrnInfoPtr pScrn);
71218781e08Smrg
71318781e08Smrgstatic inline struct radeon_surface *radeon_get_pixmap_surface(PixmapPtr pPix)
71418781e08Smrg{
71539413783Smrg    struct radeon_exa_pixmap_priv *driver_priv = exaGetPixmapDriverPrivate(pPix);
71618781e08Smrg
71739413783Smrg    return &driver_priv->surface;
71818781e08Smrg}
71918781e08Smrg
720de2362d3Smrguint32_t radeon_get_pixmap_tiling(PixmapPtr pPix);
721de2362d3Smrg
72239413783Smrgstatic inline Bool radeon_set_pixmap_bo(PixmapPtr pPix, struct radeon_buffer *bo)
72318781e08Smrg{
7248bf5c682Smrg    ScrnInfoPtr scrn = xf86ScreenToScrn(pPix->drawable.pScreen);
7258bf5c682Smrg    RADEONEntPtr pRADEONEnt = RADEONEntPriv(scrn);
72618781e08Smrg#ifdef USE_GLAMOR
7278bf5c682Smrg    RADEONInfoPtr info = RADEONPTR(scrn);
728de2362d3Smrg
72918781e08Smrg    if (info->use_glamor) {
73018781e08Smrg	struct radeon_pixmap *priv;
731de2362d3Smrg
73218781e08Smrg	priv = radeon_get_pixmap_private(pPix);
73339413783Smrg	if (!priv && !bo)
73418781e08Smrg	    return TRUE;
735de2362d3Smrg
73618781e08Smrg	if (priv) {
73718781e08Smrg	    if (priv->bo) {
73818781e08Smrg		if (priv->bo == bo)
73918781e08Smrg		    return TRUE;
7407314432eSmrg
74139413783Smrg		radeon_buffer_unref(&priv->bo);
74239413783Smrg		priv->handle_valid = FALSE;
74318781e08Smrg	    }
744de2362d3Smrg
7458bf5c682Smrg	    drmmode_fb_reference(pRADEONEnt->fd, &priv->fb, NULL);
7468bf5c682Smrg
74718781e08Smrg	    if (!bo) {
74818781e08Smrg		free(priv);
74918781e08Smrg		priv = NULL;
75018781e08Smrg	    }
75118781e08Smrg	}
75218781e08Smrg
75318781e08Smrg	if (bo) {
75418781e08Smrg	    if (!priv) {
75518781e08Smrg		priv = calloc(1, sizeof (struct radeon_pixmap));
75618781e08Smrg		if (!priv)
75718781e08Smrg		    return FALSE;
75818781e08Smrg	    }
759de2362d3Smrg
76039413783Smrg	    radeon_buffer_ref(bo);
76118781e08Smrg	    priv->bo = bo;
76218781e08Smrg	}
76318781e08Smrg
76418781e08Smrg	radeon_set_pixmap_private(pPix, priv);
76539413783Smrg	radeon_get_pixmap_tiling_flags(pPix);
76618781e08Smrg	return TRUE;
76718781e08Smrg    } else
76818781e08Smrg#endif /* USE_GLAMOR */
76918781e08Smrg    {
77018781e08Smrg	struct radeon_exa_pixmap_priv *driver_priv;
77118781e08Smrg
77218781e08Smrg	driver_priv = exaGetPixmapDriverPrivate(pPix);
77318781e08Smrg	if (driver_priv) {
77418781e08Smrg	    uint32_t pitch;
77518781e08Smrg
77639413783Smrg	    radeon_buffer_unref(&driver_priv->bo);
7778bf5c682Smrg	    drmmode_fb_reference(pRADEONEnt->fd, &driver_priv->fb, NULL);
7788bf5c682Smrg
77918781e08Smrg	    driver_priv->bo = bo;
78018781e08Smrg
78139413783Smrg	    if (bo) {
78239413783Smrg		radeon_buffer_ref(bo);
78339413783Smrg		radeon_bo_get_tiling(bo->bo.radeon, &driver_priv->tiling_flags,
78439413783Smrg				     &pitch);
78539413783Smrg	    } else
78639413783Smrg		driver_priv->tiling_flags = 0;
78739413783Smrg
78818781e08Smrg	    return TRUE;
78918781e08Smrg	}
79018781e08Smrg
79118781e08Smrg	return FALSE;
79218781e08Smrg    }
79318781e08Smrg}
79418781e08Smrg
79539413783Smrgstatic inline struct radeon_buffer *radeon_get_pixmap_bo(PixmapPtr pPix)
79618781e08Smrg{
79718781e08Smrg    RADEONInfoPtr info = RADEONPTR(xf86ScreenToScrn(pPix->drawable.pScreen));
7987d032622Smaya#ifdef USE_GLAMOR
79918781e08Smrg
80018781e08Smrg    if (info->use_glamor) {
80118781e08Smrg	struct radeon_pixmap *priv;
80218781e08Smrg	priv = radeon_get_pixmap_private(pPix);
80318781e08Smrg	return priv ? priv->bo : NULL;
80418781e08Smrg    } else
80518781e08Smrg#endif
8067d032622Smaya    if (info->accelOn) {
80718781e08Smrg	struct radeon_exa_pixmap_priv *driver_priv;
80818781e08Smrg	driver_priv = exaGetPixmapDriverPrivate(pPix);
80918781e08Smrg	return driver_priv ? driver_priv->bo : NULL;
81018781e08Smrg    }
81118781e08Smrg
81218781e08Smrg    return NULL;
81318781e08Smrg}
81418781e08Smrg
81518781e08Smrgstatic inline Bool radeon_get_pixmap_shared(PixmapPtr pPix)
81618781e08Smrg{
81718781e08Smrg#ifdef USE_GLAMOR
81818781e08Smrg    RADEONInfoPtr info = RADEONPTR(xf86ScreenToScrn(pPix->drawable.pScreen));
81918781e08Smrg
82018781e08Smrg    if (info->use_glamor) {
82118781e08Smrg        ErrorF("glamor sharing todo\n");
82218781e08Smrg	return FALSE;
82318781e08Smrg    } else
82418781e08Smrg#endif
82518781e08Smrg    {
82618781e08Smrg	struct radeon_exa_pixmap_priv *driver_priv;
82718781e08Smrg	driver_priv = exaGetPixmapDriverPrivate(pPix);
82818781e08Smrg	return driver_priv->shared;
82918781e08Smrg    }
83018781e08Smrg    return FALSE;
83118781e08Smrg}
832de2362d3Smrg
8338bf5c682Smrgstatic inline struct drmmode_fb*
8348bf5c682Smrgradeon_fb_create(ScrnInfoPtr scrn, int drm_fd, uint32_t width, uint32_t height,
8358bf5c682Smrg		 uint32_t pitch, uint32_t handle)
8368bf5c682Smrg{
8378bf5c682Smrg    struct drmmode_fb *fb  = malloc(sizeof(*fb));
8388bf5c682Smrg
8398bf5c682Smrg    if (!fb)
8408bf5c682Smrg	return NULL;
8418bf5c682Smrg
8428bf5c682Smrg    fb->refcnt = 1;
8438bf5c682Smrg    if (drmModeAddFB(drm_fd, width, height, scrn->depth, scrn->bitsPerPixel,
8448bf5c682Smrg		     pitch, handle, &fb->handle) == 0)
8458bf5c682Smrg	return fb;
8468bf5c682Smrg
8478bf5c682Smrg    free(fb);
8488bf5c682Smrg    return NULL;
8498bf5c682Smrg}
8508bf5c682Smrg
8518bf5c682Smrgstatic inline struct drmmode_fb**
8528bf5c682Smrgradeon_pixmap_get_fb_ptr(PixmapPtr pix)
8538bf5c682Smrg{
8548bf5c682Smrg    ScrnInfoPtr scrn = xf86ScreenToScrn(pix->drawable.pScreen);
8558bf5c682Smrg    RADEONInfoPtr info = RADEONPTR(scrn);
8568bf5c682Smrg
8578bf5c682Smrg#ifdef USE_GLAMOR
8588bf5c682Smrg    if (info->use_glamor) {
8598bf5c682Smrg	struct radeon_pixmap *priv = radeon_get_pixmap_private(pix);
8608bf5c682Smrg
8618bf5c682Smrg	if (!priv)
8628bf5c682Smrg	    return NULL;
8638bf5c682Smrg
8648bf5c682Smrg	return &priv->fb;
8658bf5c682Smrg    } else
8668bf5c682Smrg#endif
8678bf5c682Smrg    if (info->accelOn)
8688bf5c682Smrg    {
8698bf5c682Smrg	struct radeon_exa_pixmap_priv *driver_priv =
8708bf5c682Smrg	    exaGetPixmapDriverPrivate(pix);
8718bf5c682Smrg
8728bf5c682Smrg	if (!driver_priv)
8738bf5c682Smrg	    return NULL;
8748bf5c682Smrg
8758bf5c682Smrg	return &driver_priv->fb;
8768bf5c682Smrg    }
8778bf5c682Smrg
8788bf5c682Smrg    return NULL;
8798bf5c682Smrg}
8808bf5c682Smrg
8818bf5c682Smrgstatic inline struct drmmode_fb*
8828bf5c682Smrgradeon_pixmap_get_fb(PixmapPtr pix)
8838bf5c682Smrg{
8848bf5c682Smrg    struct drmmode_fb **fb_ptr = radeon_pixmap_get_fb_ptr(pix);
8850d2a5547Smrg    uint32_t handle;
8860d2a5547Smrg
8870d2a5547Smrg    if (fb_ptr && *fb_ptr)
8880d2a5547Smrg	return *fb_ptr;
8890d2a5547Smrg
8900d2a5547Smrg    if (radeon_get_pixmap_handle(pix, &handle)) {
8910d2a5547Smrg	ScrnInfoPtr scrn = xf86ScreenToScrn(pix->drawable.pScreen);
8920d2a5547Smrg	RADEONEntPtr pRADEONEnt = RADEONEntPriv(scrn);
8930d2a5547Smrg
8940d2a5547Smrg	if (!fb_ptr)
8950d2a5547Smrg	    fb_ptr = radeon_pixmap_get_fb_ptr(pix);
8960d2a5547Smrg
8970d2a5547Smrg	*fb_ptr = radeon_fb_create(scrn, pRADEONEnt->fd,
8980d2a5547Smrg				   pix->drawable.width,
8990d2a5547Smrg				   pix->drawable.height, pix->devKind,
9000d2a5547Smrg				   handle);
9018bf5c682Smrg    }
9028bf5c682Smrg
9037d032622Smaya    return fb_ptr ? *fb_ptr : NULL;
9048bf5c682Smrg}
9058bf5c682Smrg
90639413783Smrg
907de2362d3Smrg#define CP_PACKET0(reg, n)						\
908de2362d3Smrg	(RADEON_CP_PACKET0 | ((n) << 16) | ((reg) >> 2))
909de2362d3Smrg#define CP_PACKET1(reg0, reg1)						\
910de2362d3Smrg	(RADEON_CP_PACKET1 | (((reg1) >> 2) << 11) | ((reg0) >> 2))
911de2362d3Smrg#define CP_PACKET2()							\
912de2362d3Smrg	(RADEON_CP_PACKET2)
913de2362d3Smrg#define CP_PACKET3(pkt, n)						\
914de2362d3Smrg	(RADEON_CP_PACKET3 | (pkt) | ((n) << 16))
915de2362d3Smrg
916de2362d3Smrg
917de2362d3Smrg#define RADEON_VERBOSE	0
918de2362d3Smrg
919de2362d3Smrg#define BEGIN_RING(n) do {						\
920de2362d3Smrg    if (RADEON_VERBOSE) {						\
921de2362d3Smrg	xf86DrvMsg(pScrn->scrnIndex, X_INFO,				\
922de2362d3Smrg		   "BEGIN_RING(%d) in %s\n", (unsigned int)n, __FUNCTION__);\
923de2362d3Smrg    }									\
92418781e08Smrg    radeon_ddx_cs_start(pScrn, n, __FILE__, __func__, __LINE__);   \
925de2362d3Smrg} while (0)
926de2362d3Smrg
927de2362d3Smrg#define ADVANCE_RING() do {						\
92818781e08Smrg    radeon_cs_end(info->cs, __FILE__, __func__, __LINE__); \
929de2362d3Smrg  } while (0)
930de2362d3Smrg
931de2362d3Smrg#define OUT_RING(x) do {						\
932de2362d3Smrg    if (RADEON_VERBOSE) {						\
933de2362d3Smrg	xf86DrvMsg(pScrn->scrnIndex, X_INFO,				\
934de2362d3Smrg		   "   OUT_RING(0x%08x)\n", (unsigned int)(x));		\
935de2362d3Smrg    }									\
93618781e08Smrg    radeon_cs_write_dword(info->cs, (x));		\
937de2362d3Smrg} while (0)
938de2362d3Smrg
939de2362d3Smrg#define OUT_RING_REG(reg, val)						\
940de2362d3Smrgdo {									\
941de2362d3Smrg    OUT_RING(CP_PACKET0(reg, 0));					\
942de2362d3Smrg    OUT_RING(val);							\
943de2362d3Smrg} while (0)
944de2362d3Smrg
945de2362d3Smrg#define OUT_RING_RELOC(x, read_domains, write_domain)			\
946de2362d3Smrg  do {									\
947de2362d3Smrg	int _ret; \
948de2362d3Smrg    _ret = radeon_cs_write_reloc(info->cs, x, read_domains, write_domain, 0); \
949de2362d3Smrg	if (_ret) ErrorF("reloc emit failure %d\n", _ret); \
950de2362d3Smrg  } while(0)
951de2362d3Smrg
952de2362d3Smrg
953de2362d3Smrg#define FLUSH_RING()							\
954de2362d3Smrgdo {									\
955de2362d3Smrg    if (RADEON_VERBOSE)							\
956de2362d3Smrg	xf86DrvMsg(pScrn->scrnIndex, X_INFO,				\
957de2362d3Smrg		   "FLUSH_RING in %s\n", __FUNCTION__);			\
95818781e08Smrg    radeon_cs_flush_indirect(pScrn); 				\
959de2362d3Smrg} while (0)
960de2362d3Smrg
961de2362d3Smrg#define CS_FULL(cs) ((cs)->cdw > 15 * 1024)
962de2362d3Smrg
963de2362d3Smrg#define RADEON_SWITCH_TO_2D()						\
964de2362d3Smrgdo {									\
965de2362d3Smrg	uint32_t flush = 0;                                             \
966de2362d3Smrg	switch (info->accel_state->engineMode) {			\
967de2362d3Smrg	case EXA_ENGINEMODE_UNKNOWN:					\
968de2362d3Smrg	    flush = 1;                                                  \
969de2362d3Smrg	    break;							\
970de2362d3Smrg	case EXA_ENGINEMODE_3D:						\
97118781e08Smrg	    flush = CS_FULL(info->cs);			\
972de2362d3Smrg	    break;							\
973de2362d3Smrg	case EXA_ENGINEMODE_2D:						\
97418781e08Smrg	    flush = CS_FULL(info->cs);			\
975de2362d3Smrg	    break;							\
976de2362d3Smrg	}								\
977de2362d3Smrg	if (flush) {							\
97818781e08Smrg	    radeon_cs_flush_indirect(pScrn);			\
979de2362d3Smrg	}								\
980de2362d3Smrg        info->accel_state->engineMode = EXA_ENGINEMODE_2D;              \
981de2362d3Smrg} while (0);
982de2362d3Smrg
983de2362d3Smrg#define RADEON_SWITCH_TO_3D()						\
984de2362d3Smrgdo {									\
985de2362d3Smrg	uint32_t flush = 0;						\
986de2362d3Smrg	switch (info->accel_state->engineMode) {			\
987de2362d3Smrg	case EXA_ENGINEMODE_UNKNOWN:					\
988de2362d3Smrg	    flush = 1;                                                  \
989de2362d3Smrg	    break;							\
990de2362d3Smrg	case EXA_ENGINEMODE_2D:						\
99118781e08Smrg	    flush = CS_FULL(info->cs);	 		\
992de2362d3Smrg	    break;							\
993de2362d3Smrg	case EXA_ENGINEMODE_3D:						\
99418781e08Smrg	    flush = CS_FULL(info->cs);			\
995de2362d3Smrg	    break;							\
996de2362d3Smrg	}								\
997de2362d3Smrg	if (flush) {							\
99818781e08Smrg	    radeon_cs_flush_indirect(pScrn);			\
999de2362d3Smrg	}                                                               \
1000de2362d3Smrg	if (!info->accel_state->XInited3D)				\
1001de2362d3Smrg	    RADEONInit3DEngine(pScrn);                                  \
1002de2362d3Smrg        info->accel_state->engineMode = EXA_ENGINEMODE_3D;              \
1003de2362d3Smrg} while (0);
1004de2362d3Smrg
100518781e08Smrg				/* Memory mapped register access macros */
100618781e08Smrg
100718781e08Smrg#define BEGIN_ACCEL_RELOC(n, r) do {		\
100818781e08Smrg	int _nqw = (n) + (r);	\
100918781e08Smrg	BEGIN_RING(2*_nqw);			\
101018781e08Smrg    } while (0)
101118781e08Smrg
101218781e08Smrg#define EMIT_OFFSET(reg, value, pPix, rd, wd) do {		\
101318781e08Smrg    driver_priv = exaGetPixmapDriverPrivate(pPix);		\
101418781e08Smrg    OUT_RING_REG((reg), (value));				\
101539413783Smrg    OUT_RING_RELOC(driver_priv->bo->bo.radeon, (rd), (wd));	\
101618781e08Smrg    } while(0)
101718781e08Smrg
101818781e08Smrg#define EMIT_READ_OFFSET(reg, value, pPix) EMIT_OFFSET(reg, value, pPix, (RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT), 0)
101918781e08Smrg#define EMIT_WRITE_OFFSET(reg, value, pPix) EMIT_OFFSET(reg, value, pPix, 0, RADEON_GEM_DOMAIN_VRAM)
102018781e08Smrg
102118781e08Smrg#define OUT_TEXTURE_REG(reg, offset, bo) do {   \
102218781e08Smrg    OUT_RING_REG((reg), (offset));                                   \
102318781e08Smrg    OUT_RING_RELOC((bo), RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT, 0); \
102418781e08Smrg  } while(0)
102518781e08Smrg
102618781e08Smrg#define EMIT_COLORPITCH(reg, value, pPix) do {			\
102718781e08Smrg    driver_priv = exaGetPixmapDriverPrivate(pPix);			\
102818781e08Smrg    OUT_RING_REG((reg), value);					\
102939413783Smrg    OUT_RING_RELOC(driver_priv->bo->bo.radeon, 0, RADEON_GEM_DOMAIN_VRAM);	\
103018781e08Smrg} while(0)
1031de2362d3Smrg
1032de2362d3Smrgstatic __inline__ void RADEON_SYNC(RADEONInfoPtr info, ScrnInfoPtr pScrn)
1033de2362d3Smrg{
103418781e08Smrg    if (pScrn->pScreen)
1035de2362d3Smrg	exaWaitSync(pScrn->pScreen);
1036de2362d3Smrg}
1037de2362d3Smrg
103818781e08Smrgenum {
103918781e08Smrg    RADEON_CREATE_PIXMAP_SCANOUT		= 0x02000000,
104018781e08Smrg    RADEON_CREATE_PIXMAP_DRI2			= 0x04000000,
104118781e08Smrg    RADEON_CREATE_PIXMAP_TILING_MICRO_SQUARE	= 0x08000000,
104218781e08Smrg    RADEON_CREATE_PIXMAP_TILING_MACRO		= 0x10000000,
104318781e08Smrg    RADEON_CREATE_PIXMAP_TILING_MICRO		= 0x20000000,
104418781e08Smrg    RADEON_CREATE_PIXMAP_DEPTH			= 0x40000000, /* for r200 */
104518781e08Smrg    RADEON_CREATE_PIXMAP_SZBUFFER		= 0x80000000, /* for eg */
104618781e08Smrg};
1047de2362d3Smrg
104818781e08Smrg#define RADEON_CREATE_PIXMAP_TILING_FLAGS	\
104918781e08Smrg    (RADEON_CREATE_PIXMAP_TILING_MICRO_SQUARE |	\
105018781e08Smrg     RADEON_CREATE_PIXMAP_TILING_MACRO |	\
105118781e08Smrg     RADEON_CREATE_PIXMAP_TILING_MICRO |	\
105218781e08Smrg     RADEON_CREATE_PIXMAP_DEPTH |		\
105318781e08Smrg     RADEON_CREATE_PIXMAP_SZBUFFER)
105418781e08Smrg
105518781e08Smrg
105618781e08Smrg/* Compute log base 2 of val. */
105718781e08Smrgstatic __inline__ int
105818781e08SmrgRADEONLog2(int val)
1059de2362d3Smrg{
10603ed65abbSmrg	return 31 - __builtin_clz(val);
1061de2362d3Smrg}
1062de2362d3Smrg
106318781e08Smrg#define RADEON_TILING_MASK				0xff
106418781e08Smrg#define RADEON_TILING_LINEAR				0x0
1065de2362d3Smrg
1066de2362d3Smrg#endif /* _RADEON_H_ */
1067