radeon_screen.c revision 5a34ace3e6f507b4a7bd0f99051b64167549672c
1/**************************************************************************
2
3Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
4                     VA Linux Systems Inc., Fremont, California.
5
6All Rights Reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice (including the
17next paragraph) shall be included in all copies or substantial
18portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28**************************************************************************/
29
30/**
31 * \file radeon_screen.c
32 * Screen initialization functions for the Radeon driver.
33 *
34 * \author Kevin E. Martin <martin@valinux.com>
35 * \author  Gareth Hughes <gareth@valinux.com>
36 */
37
38#include <errno.h>
39#include "main/glheader.h"
40#include "main/imports.h"
41#include "main/mtypes.h"
42#include "main/framebuffer.h"
43#include "main/renderbuffer.h"
44
45#define STANDALONE_MMIO
46#include "radeon_chipset.h"
47#include "radeon_macros.h"
48#include "radeon_screen.h"
49#include "radeon_common.h"
50#include "radeon_span.h"
51#if !RADEON_COMMON
52#include "radeon_context.h"
53#include "radeon_tex.h"
54#elif RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
55#include "r200_context.h"
56#include "r200_ioctl.h"
57#include "r200_tex.h"
58#elif RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
59#include "r300_context.h"
60#include "r300_fragprog.h"
61#include "r300_tex.h"
62#endif
63
64#include "utils.h"
65#include "vblank.h"
66#include "drirenderbuffer.h"
67
68#include "radeon_bocs_wrapper.h"
69
70#include "GL/internal/dri_interface.h"
71
72/* Radeon configuration
73 */
74#include "xmlpool.h"
75
76#define DRI_CONF_COMMAND_BUFFER_SIZE(def,min,max) \
77DRI_CONF_OPT_BEGIN_V(command_buffer_size,int,def, # min ":" # max ) \
78        DRI_CONF_DESC(en,"Size of command buffer (in KB)") \
79        DRI_CONF_DESC(de,"Grösse des Befehlspuffers (in KB)") \
80DRI_CONF_OPT_END
81
82#if !RADEON_COMMON	/* R100 */
83PUBLIC const char __driConfigOptions[] =
84DRI_CONF_BEGIN
85    DRI_CONF_SECTION_PERFORMANCE
86        DRI_CONF_TCL_MODE(DRI_CONF_TCL_CODEGEN)
87        DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
88        DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
89        DRI_CONF_MAX_TEXTURE_UNITS(3,2,3)
90        DRI_CONF_HYPERZ(false)
91        DRI_CONF_COMMAND_BUFFER_SIZE(8, 8, 32)
92    DRI_CONF_SECTION_END
93    DRI_CONF_SECTION_QUALITY
94        DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
95        DRI_CONF_DEF_MAX_ANISOTROPY(1.0,"1.0,2.0,4.0,8.0,16.0")
96        DRI_CONF_NO_NEG_LOD_BIAS(false)
97        DRI_CONF_FORCE_S3TC_ENABLE(false)
98        DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER)
99        DRI_CONF_ROUND_MODE(DRI_CONF_ROUND_TRUNC)
100        DRI_CONF_DITHER_MODE(DRI_CONF_DITHER_XERRORDIFF)
101        DRI_CONF_ALLOW_LARGE_TEXTURES(2)
102    DRI_CONF_SECTION_END
103    DRI_CONF_SECTION_DEBUG
104        DRI_CONF_NO_RAST(false)
105    DRI_CONF_SECTION_END
106DRI_CONF_END;
107static const GLuint __driNConfigOptions = 15;
108
109#elif RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
110
111PUBLIC const char __driConfigOptions[] =
112DRI_CONF_BEGIN
113    DRI_CONF_SECTION_PERFORMANCE
114        DRI_CONF_TCL_MODE(DRI_CONF_TCL_CODEGEN)
115        DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
116        DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
117        DRI_CONF_MAX_TEXTURE_UNITS(6,2,6)
118        DRI_CONF_HYPERZ(false)
119        DRI_CONF_COMMAND_BUFFER_SIZE(8, 8, 32)
120    DRI_CONF_SECTION_END
121    DRI_CONF_SECTION_QUALITY
122        DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
123        DRI_CONF_DEF_MAX_ANISOTROPY(1.0,"1.0,2.0,4.0,8.0,16.0")
124        DRI_CONF_NO_NEG_LOD_BIAS(false)
125        DRI_CONF_FORCE_S3TC_ENABLE(false)
126        DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER)
127        DRI_CONF_ROUND_MODE(DRI_CONF_ROUND_TRUNC)
128        DRI_CONF_DITHER_MODE(DRI_CONF_DITHER_XERRORDIFF)
129        DRI_CONF_ALLOW_LARGE_TEXTURES(2)
130        DRI_CONF_TEXTURE_BLEND_QUALITY(1.0,"0.0:1.0")
131    DRI_CONF_SECTION_END
132    DRI_CONF_SECTION_DEBUG
133        DRI_CONF_NO_RAST(false)
134    DRI_CONF_SECTION_END
135    DRI_CONF_SECTION_SOFTWARE
136        DRI_CONF_NV_VERTEX_PROGRAM(false)
137    DRI_CONF_SECTION_END
138DRI_CONF_END;
139static const GLuint __driNConfigOptions = 17;
140
141extern const struct dri_extension blend_extensions[];
142extern const struct dri_extension ARB_vp_extension[];
143extern const struct dri_extension NV_vp_extension[];
144extern const struct dri_extension ATI_fs_extension[];
145extern const struct dri_extension point_extensions[];
146
147#elif RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
148
149/* TODO: integrate these into xmlpool.h! */
150#define DRI_CONF_MAX_TEXTURE_IMAGE_UNITS(def,min,max) \
151DRI_CONF_OPT_BEGIN_V(texture_image_units,int,def, # min ":" # max ) \
152        DRI_CONF_DESC(en,"Number of texture image units") \
153        DRI_CONF_DESC(de,"Anzahl der Textureinheiten") \
154DRI_CONF_OPT_END
155
156#define DRI_CONF_MAX_TEXTURE_COORD_UNITS(def,min,max) \
157DRI_CONF_OPT_BEGIN_V(texture_coord_units,int,def, # min ":" # max ) \
158        DRI_CONF_DESC(en,"Number of texture coordinate units") \
159        DRI_CONF_DESC(de,"Anzahl der Texturkoordinateneinheiten") \
160DRI_CONF_OPT_END
161
162
163
164#define DRI_CONF_DISABLE_S3TC(def) \
165DRI_CONF_OPT_BEGIN(disable_s3tc,bool,def) \
166        DRI_CONF_DESC(en,"Disable S3TC compression") \
167DRI_CONF_OPT_END
168
169#define DRI_CONF_DISABLE_FALLBACK(def) \
170DRI_CONF_OPT_BEGIN(disable_lowimpact_fallback,bool,def) \
171        DRI_CONF_DESC(en,"Disable Low-impact fallback") \
172DRI_CONF_OPT_END
173
174#define DRI_CONF_DISABLE_DOUBLE_SIDE_STENCIL(def) \
175DRI_CONF_OPT_BEGIN(disable_stencil_two_side,bool,def) \
176        DRI_CONF_DESC(en,"Disable GL_EXT_stencil_two_side") \
177DRI_CONF_OPT_END
178
179#define DRI_CONF_FP_OPTIMIZATION(def) \
180DRI_CONF_OPT_BEGIN_V(fp_optimization,enum,def,"0:1") \
181	DRI_CONF_DESC_BEGIN(en,"Fragment Program optimization") \
182                DRI_CONF_ENUM(0,"Optimize for Speed") \
183                DRI_CONF_ENUM(1,"Optimize for Quality") \
184        DRI_CONF_DESC_END \
185DRI_CONF_OPT_END
186
187PUBLIC const char __driConfigOptions[] =
188DRI_CONF_BEGIN
189	DRI_CONF_SECTION_PERFORMANCE
190		DRI_CONF_TCL_MODE(DRI_CONF_TCL_CODEGEN)
191		DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
192		DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
193		DRI_CONF_MAX_TEXTURE_IMAGE_UNITS(8, 2, 8)
194		DRI_CONF_MAX_TEXTURE_COORD_UNITS(8, 2, 8)
195		DRI_CONF_COMMAND_BUFFER_SIZE(8, 8, 32)
196		DRI_CONF_DISABLE_FALLBACK(true)
197		DRI_CONF_DISABLE_DOUBLE_SIDE_STENCIL(false)
198	DRI_CONF_SECTION_END
199	DRI_CONF_SECTION_QUALITY
200		DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
201		DRI_CONF_DEF_MAX_ANISOTROPY(1.0, "1.0,2.0,4.0,8.0,16.0")
202		DRI_CONF_FORCE_S3TC_ENABLE(false)
203		DRI_CONF_DISABLE_S3TC(false)
204		DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER)
205		DRI_CONF_ROUND_MODE(DRI_CONF_ROUND_TRUNC)
206		DRI_CONF_DITHER_MODE(DRI_CONF_DITHER_XERRORDIFF)
207		DRI_CONF_FP_OPTIMIZATION(DRI_CONF_FP_OPTIMIZATION_SPEED)
208	DRI_CONF_SECTION_END
209	DRI_CONF_SECTION_DEBUG
210		DRI_CONF_NO_RAST(false)
211	DRI_CONF_SECTION_END
212DRI_CONF_END;
213static const GLuint __driNConfigOptions = 17;
214
215extern const struct dri_extension gl_20_extension[];
216
217#ifndef RADEON_DEBUG
218
219static const struct dri_debug_control debug_control[] = {
220	{"fall", DEBUG_FALLBACKS},
221	{"tex", DEBUG_TEXTURE},
222	{"ioctl", DEBUG_IOCTL},
223	{"prim", DEBUG_PRIMS},
224	{"vert", DEBUG_VERTS},
225	{"state", DEBUG_STATE},
226	{"code", DEBUG_CODEGEN},
227	{"vfmt", DEBUG_VFMT},
228	{"vtxf", DEBUG_VFMT},
229	{"verb", DEBUG_VERBOSE},
230	{"dri", DEBUG_DRI},
231	{"dma", DEBUG_DMA},
232	{"san", DEBUG_SANITY},
233	{"sync", DEBUG_SYNC},
234	{"pix", DEBUG_PIXEL},
235	{"mem", DEBUG_MEMORY},
236	{"allmsg", ~DEBUG_SYNC}, /* avoid the term "sync" because the parser uses strstr */
237	{NULL, 0}
238};
239#endif /* RADEON_DEBUG */
240
241#endif /* RADEON_COMMON && defined(RADEON_COMMON_FOR_R300) */
242
243extern const struct dri_extension card_extensions[];
244
245static int getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo );
246
247static int
248radeonGetParam(int fd, int param, void *value)
249{
250  int ret;
251  drm_radeon_getparam_t gp;
252
253  gp.param = param;
254  gp.value = value;
255
256  ret = drmCommandWriteRead( fd, DRM_RADEON_GETPARAM, &gp, sizeof(gp));
257  return ret;
258}
259
260static const __DRIconfig **
261radeonFillInModes( __DRIscreenPrivate *psp,
262		   unsigned pixel_bits, unsigned depth_bits,
263		   unsigned stencil_bits, GLboolean have_back_buffer )
264{
265    __DRIconfig **configs;
266    __GLcontextModes *m;
267    unsigned depth_buffer_factor;
268    unsigned back_buffer_factor;
269    GLenum fb_format;
270    GLenum fb_type;
271    int i;
272
273    /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
274     * enough to add support.  Basically, if a context is created with an
275     * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
276     * will never be used.
277     */
278    static const GLenum back_buffer_modes[] = {
279	GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
280    };
281
282    uint8_t depth_bits_array[2];
283    uint8_t stencil_bits_array[2];
284    uint8_t msaa_samples_array[1];
285
286    depth_bits_array[0] = depth_bits;
287    depth_bits_array[1] = depth_bits;
288
289    /* Just like with the accumulation buffer, always provide some modes
290     * with a stencil buffer.  It will be a sw fallback, but some apps won't
291     * care about that.
292     */
293    stencil_bits_array[0] = 0;
294    stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
295
296    msaa_samples_array[0] = 0;
297
298    depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
299    back_buffer_factor  = (have_back_buffer) ? 2 : 1;
300
301    if ( pixel_bits == 16 ) {
302        fb_format = GL_RGB;
303        fb_type = GL_UNSIGNED_SHORT_5_6_5;
304    }
305    else {
306        fb_format = GL_BGRA;
307        fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
308    }
309
310    configs = driCreateConfigs(fb_format, fb_type,
311			       depth_bits_array, stencil_bits_array,
312			       depth_buffer_factor,
313			       back_buffer_modes, back_buffer_factor,
314			       msaa_samples_array, 1);
315    if (configs == NULL) {
316	fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
317		 __func__, __LINE__ );
318	return NULL;
319    }
320
321    /* Mark the visual as slow if there are "fake" stencil bits.
322     */
323    for (i = 0; configs[i]; i++) {
324	m = &configs[i]->modes;
325	if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
326	    m->visualRating = GLX_SLOW_CONFIG;
327	}
328    }
329
330    return (const __DRIconfig **) configs;
331}
332
333#if !RADEON_COMMON
334static const __DRItexOffsetExtension radeonTexOffsetExtension = {
335    { __DRI_TEX_OFFSET, __DRI_TEX_OFFSET_VERSION },
336    radeonSetTexOffset,
337};
338#endif
339
340#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
341static const __DRIallocateExtension r200AllocateExtension = {
342    { __DRI_ALLOCATE, __DRI_ALLOCATE_VERSION },
343    r200AllocateMemoryMESA,
344    r200FreeMemoryMESA,
345    r200GetMemoryOffsetMESA
346};
347
348static const __DRItexOffsetExtension r200texOffsetExtension = {
349    { __DRI_TEX_OFFSET, __DRI_TEX_OFFSET_VERSION },
350   r200SetTexOffset,
351};
352#endif
353
354#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
355static const __DRItexOffsetExtension r300texOffsetExtension = {
356    { __DRI_TEX_OFFSET, __DRI_TEX_OFFSET_VERSION },
357   r300SetTexOffset,
358};
359
360static const __DRItexBufferExtension r300TexBufferExtension = {
361    { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
362   r300SetTexBuffer,
363};
364#endif
365
366static int radeon_set_screen_flags(radeonScreenPtr screen, int device_id)
367{
368   screen->chip_flags = 0;
369   switch ( device_id ) {
370   case PCI_CHIP_RADEON_LY:
371   case PCI_CHIP_RADEON_LZ:
372   case PCI_CHIP_RADEON_QY:
373   case PCI_CHIP_RADEON_QZ:
374   case PCI_CHIP_RN50_515E:
375   case PCI_CHIP_RN50_5969:
376      screen->chip_family = CHIP_FAMILY_RV100;
377      break;
378
379   case PCI_CHIP_RS100_4136:
380   case PCI_CHIP_RS100_4336:
381      screen->chip_family = CHIP_FAMILY_RS100;
382      break;
383
384   case PCI_CHIP_RS200_4137:
385   case PCI_CHIP_RS200_4337:
386   case PCI_CHIP_RS250_4237:
387   case PCI_CHIP_RS250_4437:
388      screen->chip_family = CHIP_FAMILY_RS200;
389      break;
390
391   case PCI_CHIP_RADEON_QD:
392   case PCI_CHIP_RADEON_QE:
393   case PCI_CHIP_RADEON_QF:
394   case PCI_CHIP_RADEON_QG:
395      /* all original radeons (7200) presumably have a stencil op bug */
396      screen->chip_family = CHIP_FAMILY_R100;
397      screen->chip_flags = RADEON_CHIPSET_TCL | RADEON_CHIPSET_BROKEN_STENCIL;
398      break;
399
400   case PCI_CHIP_RV200_QW:
401   case PCI_CHIP_RV200_QX:
402   case PCI_CHIP_RADEON_LW:
403   case PCI_CHIP_RADEON_LX:
404      screen->chip_family = CHIP_FAMILY_RV200;
405      screen->chip_flags = RADEON_CHIPSET_TCL;
406      break;
407
408   case PCI_CHIP_R200_BB:
409   case PCI_CHIP_R200_BC:
410   case PCI_CHIP_R200_QH:
411   case PCI_CHIP_R200_QL:
412   case PCI_CHIP_R200_QM:
413      screen->chip_family = CHIP_FAMILY_R200;
414      screen->chip_flags = RADEON_CHIPSET_TCL;
415      break;
416
417   case PCI_CHIP_RV250_If:
418   case PCI_CHIP_RV250_Ig:
419   case PCI_CHIP_RV250_Ld:
420   case PCI_CHIP_RV250_Lf:
421   case PCI_CHIP_RV250_Lg:
422      screen->chip_family = CHIP_FAMILY_RV250;
423      screen->chip_flags = R200_CHIPSET_YCBCR_BROKEN | RADEON_CHIPSET_TCL;
424      break;
425
426   case PCI_CHIP_RV280_5960:
427   case PCI_CHIP_RV280_5961:
428   case PCI_CHIP_RV280_5962:
429   case PCI_CHIP_RV280_5964:
430   case PCI_CHIP_RV280_5965:
431   case PCI_CHIP_RV280_5C61:
432   case PCI_CHIP_RV280_5C63:
433      screen->chip_family = CHIP_FAMILY_RV280;
434      screen->chip_flags = RADEON_CHIPSET_TCL;
435      break;
436
437   case PCI_CHIP_RS300_5834:
438   case PCI_CHIP_RS300_5835:
439   case PCI_CHIP_RS350_7834:
440   case PCI_CHIP_RS350_7835:
441      screen->chip_family = CHIP_FAMILY_RS300;
442      break;
443
444      /* 9500 with 1 pipe verified by: Reid Linnemann <lreid@cs.okstate.edu> */
445   case PCI_CHIP_R300_AD:
446      screen->chip_family = CHIP_FAMILY_RV350;
447      screen->chip_flags = RADEON_CHIPSET_TCL;
448      break;
449   case PCI_CHIP_R300_AE:
450   case PCI_CHIP_R300_AF:
451   case PCI_CHIP_R300_AG:
452   case PCI_CHIP_R300_ND:
453   case PCI_CHIP_R300_NE:
454   case PCI_CHIP_R300_NF:
455   case PCI_CHIP_R300_NG:
456      screen->chip_family = CHIP_FAMILY_R300;
457      screen->chip_flags = RADEON_CHIPSET_TCL;
458      break;
459
460   case PCI_CHIP_RV350_AP:
461   case PCI_CHIP_RV350_AQ:
462   case PCI_CHIP_RV350_AR:
463   case PCI_CHIP_RV350_AS:
464   case PCI_CHIP_RV350_AT:
465   case PCI_CHIP_RV350_AV:
466   case PCI_CHIP_RV350_AU:
467   case PCI_CHIP_RV350_NP:
468   case PCI_CHIP_RV350_NQ:
469   case PCI_CHIP_RV350_NR:
470   case PCI_CHIP_RV350_NS:
471   case PCI_CHIP_RV350_NT:
472   case PCI_CHIP_RV350_NV:
473      screen->chip_family = CHIP_FAMILY_RV350;
474      screen->chip_flags = RADEON_CHIPSET_TCL;
475      break;
476
477   case PCI_CHIP_R350_AH:
478   case PCI_CHIP_R350_AI:
479   case PCI_CHIP_R350_AJ:
480   case PCI_CHIP_R350_AK:
481   case PCI_CHIP_R350_NH:
482   case PCI_CHIP_R350_NI:
483   case PCI_CHIP_R360_NJ:
484   case PCI_CHIP_R350_NK:
485      screen->chip_family = CHIP_FAMILY_R350;
486      screen->chip_flags = RADEON_CHIPSET_TCL;
487      break;
488
489   case PCI_CHIP_RV370_5460:
490   case PCI_CHIP_RV370_5462:
491   case PCI_CHIP_RV370_5464:
492   case PCI_CHIP_RV370_5B60:
493   case PCI_CHIP_RV370_5B62:
494   case PCI_CHIP_RV370_5B63:
495   case PCI_CHIP_RV370_5B64:
496   case PCI_CHIP_RV370_5B65:
497   case PCI_CHIP_RV380_3150:
498   case PCI_CHIP_RV380_3152:
499   case PCI_CHIP_RV380_3154:
500   case PCI_CHIP_RV380_3E50:
501   case PCI_CHIP_RV380_3E54:
502      screen->chip_family = CHIP_FAMILY_RV380;
503      screen->chip_flags = RADEON_CHIPSET_TCL;
504      break;
505
506   case PCI_CHIP_R420_JN:
507   case PCI_CHIP_R420_JH:
508   case PCI_CHIP_R420_JI:
509   case PCI_CHIP_R420_JJ:
510   case PCI_CHIP_R420_JK:
511   case PCI_CHIP_R420_JL:
512   case PCI_CHIP_R420_JM:
513   case PCI_CHIP_R420_JO:
514   case PCI_CHIP_R420_JP:
515   case PCI_CHIP_R420_JT:
516   case PCI_CHIP_R481_4B49:
517   case PCI_CHIP_R481_4B4A:
518   case PCI_CHIP_R481_4B4B:
519   case PCI_CHIP_R481_4B4C:
520   case PCI_CHIP_R423_UH:
521   case PCI_CHIP_R423_UI:
522   case PCI_CHIP_R423_UJ:
523   case PCI_CHIP_R423_UK:
524   case PCI_CHIP_R430_554C:
525   case PCI_CHIP_R430_554D:
526   case PCI_CHIP_R430_554E:
527   case PCI_CHIP_R430_554F:
528   case PCI_CHIP_R423_5550:
529   case PCI_CHIP_R423_UQ:
530   case PCI_CHIP_R423_UR:
531   case PCI_CHIP_R423_UT:
532   case PCI_CHIP_R430_5D48:
533   case PCI_CHIP_R430_5D49:
534   case PCI_CHIP_R430_5D4A:
535   case PCI_CHIP_R480_5D4C:
536   case PCI_CHIP_R480_5D4D:
537   case PCI_CHIP_R480_5D4E:
538   case PCI_CHIP_R480_5D4F:
539   case PCI_CHIP_R480_5D50:
540   case PCI_CHIP_R480_5D52:
541   case PCI_CHIP_R423_5D57:
542      screen->chip_family = CHIP_FAMILY_R420;
543      screen->chip_flags = RADEON_CHIPSET_TCL;
544      break;
545
546   case PCI_CHIP_RV410_5E4C:
547   case PCI_CHIP_RV410_5E4F:
548   case PCI_CHIP_RV410_564A:
549   case PCI_CHIP_RV410_564B:
550   case PCI_CHIP_RV410_564F:
551   case PCI_CHIP_RV410_5652:
552   case PCI_CHIP_RV410_5653:
553   case PCI_CHIP_RV410_5657:
554   case PCI_CHIP_RV410_5E48:
555   case PCI_CHIP_RV410_5E4A:
556   case PCI_CHIP_RV410_5E4B:
557   case PCI_CHIP_RV410_5E4D:
558      screen->chip_family = CHIP_FAMILY_RV410;
559      screen->chip_flags = RADEON_CHIPSET_TCL;
560      break;
561
562   case PCI_CHIP_RS480_5954:
563   case PCI_CHIP_RS480_5955:
564   case PCI_CHIP_RS482_5974:
565   case PCI_CHIP_RS482_5975:
566   case PCI_CHIP_RS400_5A41:
567   case PCI_CHIP_RS400_5A42:
568   case PCI_CHIP_RC410_5A61:
569   case PCI_CHIP_RC410_5A62:
570      screen->chip_family = CHIP_FAMILY_RS400;
571      break;
572
573   case PCI_CHIP_RS690_791E:
574   case PCI_CHIP_RS690_791F:
575      screen->chip_family = CHIP_FAMILY_RS690;
576      break;
577   case PCI_CHIP_RS740_796C:
578   case PCI_CHIP_RS740_796D:
579   case PCI_CHIP_RS740_796E:
580   case PCI_CHIP_RS740_796F:
581      screen->chip_family = CHIP_FAMILY_RS740;
582      break;
583
584   case PCI_CHIP_R520_7100:
585   case PCI_CHIP_R520_7101:
586   case PCI_CHIP_R520_7102:
587   case PCI_CHIP_R520_7103:
588   case PCI_CHIP_R520_7104:
589   case PCI_CHIP_R520_7105:
590   case PCI_CHIP_R520_7106:
591   case PCI_CHIP_R520_7108:
592   case PCI_CHIP_R520_7109:
593   case PCI_CHIP_R520_710A:
594   case PCI_CHIP_R520_710B:
595   case PCI_CHIP_R520_710C:
596   case PCI_CHIP_R520_710E:
597   case PCI_CHIP_R520_710F:
598      screen->chip_family = CHIP_FAMILY_R520;
599      screen->chip_flags = RADEON_CHIPSET_TCL;
600      break;
601
602   case PCI_CHIP_RV515_7140:
603   case PCI_CHIP_RV515_7141:
604   case PCI_CHIP_RV515_7142:
605   case PCI_CHIP_RV515_7143:
606   case PCI_CHIP_RV515_7144:
607   case PCI_CHIP_RV515_7145:
608   case PCI_CHIP_RV515_7146:
609   case PCI_CHIP_RV515_7147:
610   case PCI_CHIP_RV515_7149:
611   case PCI_CHIP_RV515_714A:
612   case PCI_CHIP_RV515_714B:
613   case PCI_CHIP_RV515_714C:
614   case PCI_CHIP_RV515_714D:
615   case PCI_CHIP_RV515_714E:
616   case PCI_CHIP_RV515_714F:
617   case PCI_CHIP_RV515_7151:
618   case PCI_CHIP_RV515_7152:
619   case PCI_CHIP_RV515_7153:
620   case PCI_CHIP_RV515_715E:
621   case PCI_CHIP_RV515_715F:
622   case PCI_CHIP_RV515_7180:
623   case PCI_CHIP_RV515_7181:
624   case PCI_CHIP_RV515_7183:
625   case PCI_CHIP_RV515_7186:
626   case PCI_CHIP_RV515_7187:
627   case PCI_CHIP_RV515_7188:
628   case PCI_CHIP_RV515_718A:
629   case PCI_CHIP_RV515_718B:
630   case PCI_CHIP_RV515_718C:
631   case PCI_CHIP_RV515_718D:
632   case PCI_CHIP_RV515_718F:
633   case PCI_CHIP_RV515_7193:
634   case PCI_CHIP_RV515_7196:
635   case PCI_CHIP_RV515_719B:
636   case PCI_CHIP_RV515_719F:
637   case PCI_CHIP_RV515_7200:
638   case PCI_CHIP_RV515_7210:
639   case PCI_CHIP_RV515_7211:
640      screen->chip_family = CHIP_FAMILY_RV515;
641      screen->chip_flags = RADEON_CHIPSET_TCL;
642      break;
643
644   case PCI_CHIP_RV530_71C0:
645   case PCI_CHIP_RV530_71C1:
646   case PCI_CHIP_RV530_71C2:
647   case PCI_CHIP_RV530_71C3:
648   case PCI_CHIP_RV530_71C4:
649   case PCI_CHIP_RV530_71C5:
650   case PCI_CHIP_RV530_71C6:
651   case PCI_CHIP_RV530_71C7:
652   case PCI_CHIP_RV530_71CD:
653   case PCI_CHIP_RV530_71CE:
654   case PCI_CHIP_RV530_71D2:
655   case PCI_CHIP_RV530_71D4:
656   case PCI_CHIP_RV530_71D5:
657   case PCI_CHIP_RV530_71D6:
658   case PCI_CHIP_RV530_71DA:
659   case PCI_CHIP_RV530_71DE:
660      screen->chip_family = CHIP_FAMILY_RV530;
661      screen->chip_flags = RADEON_CHIPSET_TCL;
662      break;
663
664   case PCI_CHIP_R580_7240:
665   case PCI_CHIP_R580_7243:
666   case PCI_CHIP_R580_7244:
667   case PCI_CHIP_R580_7245:
668   case PCI_CHIP_R580_7246:
669   case PCI_CHIP_R580_7247:
670   case PCI_CHIP_R580_7248:
671   case PCI_CHIP_R580_7249:
672   case PCI_CHIP_R580_724A:
673   case PCI_CHIP_R580_724B:
674   case PCI_CHIP_R580_724C:
675   case PCI_CHIP_R580_724D:
676   case PCI_CHIP_R580_724E:
677   case PCI_CHIP_R580_724F:
678   case PCI_CHIP_R580_7284:
679      screen->chip_family = CHIP_FAMILY_R580;
680      screen->chip_flags = RADEON_CHIPSET_TCL;
681      break;
682
683   case PCI_CHIP_RV570_7280:
684   case PCI_CHIP_RV560_7281:
685   case PCI_CHIP_RV560_7283:
686   case PCI_CHIP_RV560_7287:
687   case PCI_CHIP_RV570_7288:
688   case PCI_CHIP_RV570_7289:
689   case PCI_CHIP_RV570_728B:
690   case PCI_CHIP_RV570_728C:
691   case PCI_CHIP_RV560_7290:
692   case PCI_CHIP_RV560_7291:
693   case PCI_CHIP_RV560_7293:
694   case PCI_CHIP_RV560_7297:
695      screen->chip_family = CHIP_FAMILY_RV560;
696      screen->chip_flags = RADEON_CHIPSET_TCL;
697      break;
698
699   default:
700      fprintf(stderr, "unknown chip id 0x%x, can't guess.\n",
701	      device_id);
702      return -1;
703   }
704
705   return 0;
706}
707
708
709/* Create the device specific screen private data struct.
710 */
711static radeonScreenPtr
712radeonCreateScreen( __DRIscreenPrivate *sPriv )
713{
714   radeonScreenPtr screen;
715   RADEONDRIPtr dri_priv = (RADEONDRIPtr)sPriv->pDevPriv;
716   unsigned char *RADEONMMIO = NULL;
717   int i;
718   int ret;
719   uint32_t temp;
720
721   if (sPriv->devPrivSize != sizeof(RADEONDRIRec)) {
722      fprintf(stderr,"\nERROR!  sizeof(RADEONDRIRec) does not match passed size from device driver\n");
723      return GL_FALSE;
724   }
725
726   /* Allocate the private area */
727   screen = (radeonScreenPtr) CALLOC( sizeof(*screen) );
728   if ( !screen ) {
729      __driUtilMessage("%s: Could not allocate memory for screen structure",
730		       __FUNCTION__);
731      return NULL;
732   }
733
734#if DO_DEBUG && RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
735	RADEON_DEBUG = driParseDebugString(getenv("RADEON_DEBUG"), debug_control);
736#endif
737
738   /* parse information in __driConfigOptions */
739   driParseOptionInfo (&screen->optionCache,
740		       __driConfigOptions, __driNConfigOptions);
741
742   /* This is first since which regions we map depends on whether or
743    * not we are using a PCI card.
744    */
745   screen->card_type = (dri_priv->IsPCI ? RADEON_CARD_PCI : RADEON_CARD_AGP);
746   {
747      int ret;
748
749#ifdef RADEON_PARAM_KERNEL_MM
750     ret = radeonGetParam( sPriv->fd, RADEON_PARAM_KERNEL_MM,
751                            &screen->kernel_mm);
752
753      if (ret && ret != -EINVAL) {
754         FREE( screen );
755         fprintf(stderr, "drm_radeon_getparam_t (RADEON_OFFSET): %d\n", ret);
756         return NULL;
757      }
758
759      if (ret == -EINVAL)
760          screen->kernel_mm = 0;
761#endif
762
763      ret = radeonGetParam( sPriv->fd, RADEON_PARAM_GART_BUFFER_OFFSET,
764			    &screen->gart_buffer_offset);
765
766      if (ret) {
767	 FREE( screen );
768	 fprintf(stderr, "drm_radeon_getparam_t (RADEON_PARAM_GART_BUFFER_OFFSET): %d\n", ret);
769	 return NULL;
770      }
771
772      ret = radeonGetParam( sPriv->fd, RADEON_PARAM_GART_BASE,
773			    &screen->gart_base);
774      if (ret) {
775	 FREE( screen );
776	 fprintf(stderr, "drm_radeon_getparam_t (RADEON_PARAM_GART_BASE): %d\n", ret);
777	 return NULL;
778      }
779
780      ret = radeonGetParam( sPriv->fd, RADEON_PARAM_IRQ_NR,
781			    &screen->irq);
782      if (ret) {
783	 FREE( screen );
784	 fprintf(stderr, "drm_radeon_getparam_t (RADEON_PARAM_IRQ_NR): %d\n", ret);
785	 return NULL;
786      }
787      screen->drmSupportsCubeMapsR200 = (sPriv->drm_version.minor >= 7);
788      screen->drmSupportsBlendColor = (sPriv->drm_version.minor >= 11);
789      screen->drmSupportsTriPerf = (sPriv->drm_version.minor >= 16);
790      screen->drmSupportsFragShader = (sPriv->drm_version.minor >= 18);
791      screen->drmSupportsPointSprites = (sPriv->drm_version.minor >= 13);
792      screen->drmSupportsCubeMapsR100 = (sPriv->drm_version.minor >= 15);
793      screen->drmSupportsVertexProgram = (sPriv->drm_version.minor >= 25);
794   }
795
796   if (!screen->kernel_mm) {
797     screen->mmio.handle = dri_priv->registerHandle;
798     screen->mmio.size   = dri_priv->registerSize;
799     if ( drmMap( sPriv->fd,
800		  screen->mmio.handle,
801		  screen->mmio.size,
802		  &screen->mmio.map ) ) {
803       FREE( screen );
804       __driUtilMessage("%s: drmMap failed\n", __FUNCTION__ );
805       return NULL;
806     }
807
808     RADEONMMIO = screen->mmio.map;
809
810     screen->status.handle = dri_priv->statusHandle;
811     screen->status.size   = dri_priv->statusSize;
812     if ( drmMap( sPriv->fd,
813		  screen->status.handle,
814		  screen->status.size,
815		  &screen->status.map ) ) {
816       drmUnmap( screen->mmio.map, screen->mmio.size );
817       FREE( screen );
818       __driUtilMessage("%s: drmMap (2) failed\n", __FUNCTION__ );
819       return NULL;
820     }
821     screen->scratch = (__volatile__ uint32_t *)
822       ((GLubyte *)screen->status.map + RADEON_SCRATCH_REG_OFFSET);
823
824     screen->buffers = drmMapBufs( sPriv->fd );
825     if ( !screen->buffers ) {
826       drmUnmap( screen->status.map, screen->status.size );
827       drmUnmap( screen->mmio.map, screen->mmio.size );
828       FREE( screen );
829       __driUtilMessage("%s: drmMapBufs failed\n", __FUNCTION__ );
830       return NULL;
831     }
832
833     if ( dri_priv->gartTexHandle && dri_priv->gartTexMapSize ) {
834       screen->gartTextures.handle = dri_priv->gartTexHandle;
835       screen->gartTextures.size   = dri_priv->gartTexMapSize;
836       if ( drmMap( sPriv->fd,
837		    screen->gartTextures.handle,
838		    screen->gartTextures.size,
839		    (drmAddressPtr)&screen->gartTextures.map ) ) {
840	 drmUnmapBufs( screen->buffers );
841	 drmUnmap( screen->status.map, screen->status.size );
842	 drmUnmap( screen->mmio.map, screen->mmio.size );
843	 FREE( screen );
844	 __driUtilMessage("%s: drmMap failed for GART texture area\n", __FUNCTION__);
845	 return NULL;
846       }
847
848       screen->gart_texture_offset = dri_priv->gartTexOffset + screen->gart_base;
849     }
850   }
851
852
853   ret = radeon_set_screen_flags(screen, dri_priv->deviceID);
854   if (ret == -1)
855     return NULL;
856
857   if ((screen->chip_family == CHIP_FAMILY_R350 || screen->chip_family == CHIP_FAMILY_R300) &&
858       sPriv->ddx_version.minor < 2) {
859      fprintf(stderr, "xf86-video-ati-6.6.2 or newer needed for Radeon 9500/9700/9800 cards.\n");
860      return NULL;
861   }
862
863   if ((sPriv->drm_version.minor < 29) && (screen->chip_family >= CHIP_FAMILY_RV515)) {
864      fprintf(stderr, "R500 support requires a newer drm.\n");
865      return NULL;
866   }
867
868   if (getenv("R300_NO_TCL"))
869     screen->chip_flags &= ~RADEON_CHIPSET_TCL;
870
871   if (screen->chip_family <= CHIP_FAMILY_RS200)
872      screen->chip_flags |= RADEON_CLASS_R100;
873   else if (screen->chip_family <= CHIP_FAMILY_RV280)
874      screen->chip_flags |= RADEON_CLASS_R200;
875   else
876      screen->chip_flags |= RADEON_CLASS_R300;
877
878   screen->cpp = dri_priv->bpp / 8;
879   screen->AGPMode = dri_priv->AGPMode;
880
881   ret = radeonGetParam( sPriv->fd, RADEON_PARAM_FB_LOCATION,
882                         &temp);
883   if (ret) {
884       if (screen->chip_family < CHIP_FAMILY_RS690 && !screen->kernel_mm)
885	   screen->fbLocation      = ( INREG( RADEON_MC_FB_LOCATION ) & 0xffff) << 16;
886       else {
887           FREE( screen );
888           fprintf(stderr, "Unable to get fb location need newer drm\n");
889           return NULL;
890       }
891   } else {
892       screen->fbLocation = (temp & 0xffff) << 16;
893   }
894
895   if (screen->chip_family >= CHIP_FAMILY_R300) {
896       ret = radeonGetParam( sPriv->fd, RADEON_PARAM_NUM_GB_PIPES,
897			     &temp);
898       if (ret) {
899	   fprintf(stderr, "Unable to get num_pipes, need newer drm\n");
900	   switch (screen->chip_family) {
901	   case CHIP_FAMILY_R300:
902	   case CHIP_FAMILY_R350:
903	       screen->num_gb_pipes = 2;
904	       break;
905	   case CHIP_FAMILY_R420:
906	   case CHIP_FAMILY_R520:
907	   case CHIP_FAMILY_R580:
908	   case CHIP_FAMILY_RV560:
909	   case CHIP_FAMILY_RV570:
910	       screen->num_gb_pipes = 4;
911	       break;
912	   case CHIP_FAMILY_RV350:
913	   case CHIP_FAMILY_RV515:
914	   case CHIP_FAMILY_RV530:
915	   case CHIP_FAMILY_RV410:
916	   default:
917	       screen->num_gb_pipes = 1;
918	       break;
919	   }
920       } else {
921	   screen->num_gb_pipes = temp;
922       }
923   }
924
925   if ( sPriv->drm_version.minor >= 10 ) {
926      drm_radeon_setparam_t sp;
927
928      sp.param = RADEON_SETPARAM_FB_LOCATION;
929      sp.value = screen->fbLocation;
930
931      drmCommandWrite( sPriv->fd, DRM_RADEON_SETPARAM,
932		       &sp, sizeof( sp ) );
933   }
934
935   screen->frontOffset	= dri_priv->frontOffset;
936   screen->frontPitch	= dri_priv->frontPitch;
937   screen->backOffset	= dri_priv->backOffset;
938   screen->backPitch	= dri_priv->backPitch;
939   screen->depthOffset	= dri_priv->depthOffset;
940   screen->depthPitch	= dri_priv->depthPitch;
941
942   /* Check if ddx has set up a surface reg to cover depth buffer */
943   screen->depthHasSurface = (sPriv->ddx_version.major > 4) ||
944      /* these chips don't use tiled z without hyperz. So always pretend
945         we have set up a surface which will cause linear reads/writes */
946      (IS_R100_CLASS(screen) &&
947      !(screen->chip_flags & RADEON_CHIPSET_TCL));
948
949   if ( dri_priv->textureSize == 0 ) {
950      screen->texOffset[RADEON_LOCAL_TEX_HEAP] = screen->gart_texture_offset;
951      screen->texSize[RADEON_LOCAL_TEX_HEAP] = dri_priv->gartTexMapSize;
952      screen->logTexGranularity[RADEON_LOCAL_TEX_HEAP] =
953	 dri_priv->log2GARTTexGran;
954   } else {
955      screen->texOffset[RADEON_LOCAL_TEX_HEAP] = dri_priv->textureOffset
956				               + screen->fbLocation;
957      screen->texSize[RADEON_LOCAL_TEX_HEAP] = dri_priv->textureSize;
958      screen->logTexGranularity[RADEON_LOCAL_TEX_HEAP] =
959	 dri_priv->log2TexGran;
960   }
961
962   if ( !screen->gartTextures.map || dri_priv->textureSize == 0
963	|| getenv( "RADEON_GARTTEXTURING_FORCE_DISABLE" ) ) {
964      screen->numTexHeaps = RADEON_NR_TEX_HEAPS - 1;
965      screen->texOffset[RADEON_GART_TEX_HEAP] = 0;
966      screen->texSize[RADEON_GART_TEX_HEAP] = 0;
967      screen->logTexGranularity[RADEON_GART_TEX_HEAP] = 0;
968   } else {
969      screen->numTexHeaps = RADEON_NR_TEX_HEAPS;
970      screen->texOffset[RADEON_GART_TEX_HEAP] = screen->gart_texture_offset;
971      screen->texSize[RADEON_GART_TEX_HEAP] = dri_priv->gartTexMapSize;
972      screen->logTexGranularity[RADEON_GART_TEX_HEAP] =
973	 dri_priv->log2GARTTexGran;
974   }
975
976   i = 0;
977   screen->extensions[i++] = &driCopySubBufferExtension.base;
978   screen->extensions[i++] = &driFrameTrackingExtension.base;
979   screen->extensions[i++] = &driReadDrawableExtension;
980
981   if ( screen->irq != 0 ) {
982       screen->extensions[i++] = &driSwapControlExtension.base;
983       screen->extensions[i++] = &driMediaStreamCounterExtension.base;
984   }
985
986   if (!screen->kernel_mm) {
987#if !RADEON_COMMON
988   	screen->extensions[i++] = &radeonTexOffsetExtension.base;
989#endif
990
991#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
992        if (IS_R200_CLASS(screen))
993            screen->extensions[i++] = &r200AllocateExtension.base;
994
995        screen->extensions[i++] = &r200texOffsetExtension.base;
996#endif
997
998#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
999        screen->extensions[i++] = &r300texOffsetExtension.base;
1000#endif
1001   }
1002
1003   screen->extensions[i++] = NULL;
1004   sPriv->extensions = screen->extensions;
1005
1006   screen->driScreen = sPriv;
1007   screen->sarea_priv_offset = dri_priv->sarea_priv_offset;
1008   screen->sarea = (drm_radeon_sarea_t *) ((GLubyte *) sPriv->pSAREA +
1009					       screen->sarea_priv_offset);
1010
1011   if (screen->kernel_mm)
1012     screen->bom = radeon_bo_manager_gem_ctor(sPriv->fd);
1013   else
1014     screen->bom = radeon_bo_manager_legacy_ctor(screen);
1015   if (screen->bom == NULL) {
1016     free(screen);
1017     return NULL;
1018   }
1019
1020   return screen;
1021}
1022
1023static radeonScreenPtr
1024radeonCreateScreen2(__DRIscreenPrivate *sPriv)
1025{
1026   radeonScreenPtr screen;
1027   int i;
1028   int ret;
1029   uint32_t device_id;
1030
1031   /* Allocate the private area */
1032   screen = (radeonScreenPtr) CALLOC( sizeof(*screen) );
1033   if ( !screen ) {
1034      __driUtilMessage("%s: Could not allocate memory for screen structure",
1035		       __FUNCTION__);
1036      fprintf(stderr, "leaving here\n");
1037      return NULL;
1038   }
1039
1040#if DO_DEBUG && RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
1041	RADEON_DEBUG = driParseDebugString(getenv("RADEON_DEBUG"), debug_control);
1042#endif
1043
1044   /* parse information in __driConfigOptions */
1045   driParseOptionInfo (&screen->optionCache,
1046		       __driConfigOptions, __driNConfigOptions);
1047
1048   screen->kernel_mm = 1;
1049   screen->chip_flags = 0;
1050
1051   ret = radeonGetParam( sPriv->fd, RADEON_PARAM_IRQ_NR,
1052			 &screen->irq);
1053
1054   ret = radeonGetParam( sPriv->fd, RADEON_PARAM_DEVICE_ID,
1055			 &device_id);
1056   if (ret) {
1057     FREE( screen );
1058     fprintf(stderr, "drm_radeon_getparam_t (RADEON_PARAM_DEVICE_ID): %d\n", ret);
1059     return NULL;
1060   }
1061
1062   ret = radeon_set_screen_flags(screen, device_id);
1063   if (ret == -1)
1064     return NULL;
1065
1066   if (screen->chip_family <= CHIP_FAMILY_RS200)
1067      screen->chip_flags |= RADEON_CLASS_R100;
1068   else if (screen->chip_family <= CHIP_FAMILY_RV280)
1069      screen->chip_flags |= RADEON_CLASS_R200;
1070   else
1071      screen->chip_flags |= RADEON_CLASS_R300;
1072
1073   i = 0;
1074   screen->extensions[i++] = &driCopySubBufferExtension.base;
1075   screen->extensions[i++] = &driFrameTrackingExtension.base;
1076   screen->extensions[i++] = &driReadDrawableExtension;
1077
1078   if ( screen->irq != 0 ) {
1079       screen->extensions[i++] = &driSwapControlExtension.base;
1080       screen->extensions[i++] = &driMediaStreamCounterExtension.base;
1081   }
1082
1083#if !RADEON_COMMON
1084   screen->extensions[i++] = &radeonTexOffsetExtension.base;
1085#endif
1086
1087#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
1088   if (IS_R200_CLASS(screen))
1089       screen->extensions[i++] = &r200AllocateExtension.base;
1090
1091   screen->extensions[i++] = &r200texOffsetExtension.base;
1092#endif
1093
1094#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
1095   //screen->extensions[i++] = &r300texOffsetExtension.base;
1096   screen->extensions[i++] = &r300TexBufferExtension.base;
1097#endif
1098
1099   screen->extensions[i++] = NULL;
1100   sPriv->extensions = screen->extensions;
1101
1102   screen->driScreen = sPriv;
1103   screen->bom = radeon_bo_manager_gem_ctor(sPriv->fd);
1104   if (screen->bom == NULL) {
1105       free(screen);
1106       return NULL;
1107   }
1108   return screen;
1109}
1110
1111/* Destroy the device specific screen private data struct.
1112 */
1113static void
1114radeonDestroyScreen( __DRIscreenPrivate *sPriv )
1115{
1116    radeonScreenPtr screen = (radeonScreenPtr)sPriv->private;
1117
1118    if (!screen)
1119        return;
1120
1121    if (screen->kernel_mm) {
1122#ifdef RADEON_BO_TRACK
1123        radeon_tracker_print(&screen->bom->tracker, stderr);
1124#endif
1125        radeon_bo_manager_gem_dtor(screen->bom);
1126    } else {
1127        radeon_bo_manager_legacy_dtor(screen->bom);
1128
1129        if ( screen->gartTextures.map ) {
1130            drmUnmap( screen->gartTextures.map, screen->gartTextures.size );
1131        }
1132        drmUnmapBufs( screen->buffers );
1133        drmUnmap( screen->status.map, screen->status.size );
1134        drmUnmap( screen->mmio.map, screen->mmio.size );
1135    }
1136
1137    /* free all option information */
1138    driDestroyOptionInfo (&screen->optionCache);
1139
1140    FREE( screen );
1141    sPriv->private = NULL;
1142}
1143
1144
1145/* Initialize the driver specific screen private data.
1146 */
1147static GLboolean
1148radeonInitDriver( __DRIscreenPrivate *sPriv )
1149{
1150    if (sPriv->dri2.enabled) {
1151        sPriv->private = (void *) radeonCreateScreen2( sPriv );
1152    } else {
1153        sPriv->private = (void *) radeonCreateScreen( sPriv );
1154    }
1155    if ( !sPriv->private ) {
1156        radeonDestroyScreen( sPriv );
1157        return GL_FALSE;
1158    }
1159
1160    return GL_TRUE;
1161}
1162
1163static GLboolean
1164radeon_alloc_window_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
1165			    GLenum intFormat, GLuint w, GLuint h)
1166{
1167    rb->Width = w;
1168    rb->Height = h;
1169    rb->_ActualFormat = intFormat;
1170
1171    return GL_TRUE;
1172}
1173
1174
1175static struct radeon_renderbuffer *
1176radeon_create_renderbuffer(GLenum format, __DRIdrawablePrivate *driDrawPriv)
1177{
1178    struct radeon_renderbuffer *ret;
1179
1180    ret = CALLOC_STRUCT(radeon_renderbuffer);
1181    if (!ret)
1182	return NULL;
1183
1184    _mesa_init_renderbuffer(&ret->base, 0);
1185
1186    /* XXX format junk */
1187    switch (format) {
1188	case GL_RGB5:
1189	    ret->base._ActualFormat = GL_RGB5;
1190	    ret->base._BaseFormat = GL_RGBA;
1191	    ret->base.RedBits = 5;
1192	    ret->base.GreenBits = 6;
1193	    ret->base.BlueBits = 5;
1194	    ret->base.DataType = GL_UNSIGNED_BYTE;
1195	    break;
1196	case GL_RGBA8:
1197	    ret->base._ActualFormat = GL_RGBA8;
1198	    ret->base._BaseFormat = GL_RGBA;
1199	    ret->base.RedBits = 8;
1200	    ret->base.GreenBits = 8;
1201	    ret->base.BlueBits = 8;
1202	    ret->base.AlphaBits = 8;
1203	    ret->base.DataType = GL_UNSIGNED_BYTE;
1204	    break;
1205	case GL_STENCIL_INDEX8_EXT:
1206	    ret->base._ActualFormat = GL_STENCIL_INDEX8_EXT;
1207	    ret->base._BaseFormat = GL_STENCIL_INDEX;
1208	    ret->base.StencilBits = 8;
1209	    ret->base.DataType = GL_UNSIGNED_BYTE;
1210	    break;
1211	case GL_DEPTH_COMPONENT16:
1212	    ret->base._ActualFormat = GL_DEPTH_COMPONENT16;
1213	    ret->base._BaseFormat = GL_DEPTH_COMPONENT;
1214	    ret->base.DepthBits = 16;
1215	    ret->base.DataType = GL_UNSIGNED_SHORT;
1216	    break;
1217	case GL_DEPTH_COMPONENT24:
1218	    ret->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
1219	    ret->base._BaseFormat = GL_DEPTH_COMPONENT;
1220	    ret->base.DepthBits = 24;
1221	    ret->base.DataType = GL_UNSIGNED_INT;
1222	    break;
1223	case GL_DEPTH24_STENCIL8_EXT:
1224	    ret->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
1225	    ret->base._BaseFormat = GL_DEPTH_STENCIL_EXT;
1226	    ret->base.DepthBits = 24;
1227	    ret->base.StencilBits = 8;
1228	    ret->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
1229	    break;
1230	default:
1231	    fprintf(stderr, "%s: Unknown format 0x%04x\n", __FUNCTION__, format);
1232	    _mesa_delete_renderbuffer(&ret->base);
1233	    return NULL;
1234    }
1235
1236    ret->dPriv = driDrawPriv;
1237    ret->base.InternalFormat = format;
1238
1239    ret->base.AllocStorage = radeon_alloc_window_storage;
1240
1241    radeonSetSpanFunctions(ret);
1242
1243    ret->bo = NULL;
1244    return ret;
1245}
1246
1247/**
1248 * Create the Mesa framebuffer and renderbuffers for a given window/drawable.
1249 *
1250 * \todo This function (and its interface) will need to be updated to support
1251 * pbuffers.
1252 */
1253static GLboolean
1254radeonCreateBuffer( __DRIscreenPrivate *driScrnPriv,
1255                    __DRIdrawablePrivate *driDrawPriv,
1256                    const __GLcontextModes *mesaVis,
1257                    GLboolean isPixmap )
1258{
1259   radeonScreenPtr screen = (radeonScreenPtr) driScrnPriv->private;
1260
1261    const GLboolean swDepth = GL_FALSE;
1262    const GLboolean swAlpha = GL_FALSE;
1263    const GLboolean swAccum = mesaVis->accumRedBits > 0;
1264    const GLboolean swStencil = mesaVis->stencilBits > 0 &&
1265	mesaVis->depthBits != 24;
1266    GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
1267    GLenum depthFormat = GL_NONE;
1268    struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
1269
1270    if (mesaVis->depthBits == 16)
1271	depthFormat = GL_DEPTH_COMPONENT16;
1272    else if (mesaVis->depthBits == 24)
1273	depthFormat = GL_DEPTH_COMPONENT24;
1274
1275    /* front color renderbuffer */
1276    {
1277	struct radeon_renderbuffer *front =
1278	    radeon_create_renderbuffer(rgbFormat, driDrawPriv);
1279	_mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &front->base);
1280	front->has_surface = 1;
1281    }
1282
1283    /* back color renderbuffer */
1284    if (mesaVis->doubleBufferMode) {
1285	struct radeon_renderbuffer *back =
1286	    radeon_create_renderbuffer(rgbFormat, driDrawPriv);
1287	_mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &back->base);
1288	back->has_surface = 1;
1289    }
1290
1291    /* depth renderbuffer */
1292    if (depthFormat != GL_NONE) {
1293	struct radeon_renderbuffer *depth =
1294	    radeon_create_renderbuffer(depthFormat, driDrawPriv);
1295	_mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depth->base);
1296	depth->has_surface = screen->depthHasSurface;
1297    }
1298
1299    /* stencil renderbuffer */
1300    if (mesaVis->stencilBits > 0 && !swStencil) {
1301	struct radeon_renderbuffer *stencil =
1302	    radeon_create_renderbuffer(GL_STENCIL_INDEX8_EXT, driDrawPriv);
1303	_mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencil->base);
1304	stencil->has_surface = screen->depthHasSurface;
1305    }
1306
1307    _mesa_add_soft_renderbuffers(fb,
1308	    GL_FALSE, /* color */
1309	    swDepth,
1310	    swStencil,
1311	    swAccum,
1312	    swAlpha,
1313	    GL_FALSE /* aux */);
1314    driDrawPriv->driverPrivate = (void *) fb;
1315
1316    return (driDrawPriv->driverPrivate != NULL);
1317}
1318
1319static void
1320radeonDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
1321{
1322	struct radeon_renderbuffer *rb;
1323	GLframebuffer *fb;
1324
1325    fb = (void*)driDrawPriv->driverPrivate;
1326    rb = (void *)fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
1327    if (rb && rb->bo) {
1328        radeon_bo_unref(rb->bo);
1329        rb->bo = NULL;
1330    }
1331    rb = (void *)fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
1332    if (rb && rb->bo) {
1333        radeon_bo_unref(rb->bo);
1334        rb->bo = NULL;
1335    }
1336    rb = (void *)fb->Attachment[BUFFER_DEPTH].Renderbuffer;
1337    if (rb && rb->bo) {
1338        radeon_bo_unref(rb->bo);
1339        rb->bo = NULL;
1340    }
1341   _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
1342}
1343
1344#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
1345/**
1346 * Choose the appropriate CreateContext function based on the chipset.
1347 * Eventually, all drivers will go through this process.
1348 */
1349static GLboolean radeonCreateContext(const __GLcontextModes * glVisual,
1350				     __DRIcontextPrivate * driContextPriv,
1351				     void *sharedContextPriv)
1352{
1353	__DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
1354	radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
1355
1356	if (IS_R300_CLASS(screen))
1357		return r300CreateContext(glVisual, driContextPriv, sharedContextPriv);
1358        return GL_FALSE;
1359}
1360
1361/**
1362 * Choose the appropriate DestroyContext function based on the chipset.
1363 */
1364static void radeonDestroyContext(__DRIcontextPrivate * driContextPriv)
1365{
1366	radeonContextPtr radeon = (radeonContextPtr) driContextPriv->driverPrivate;
1367
1368	if (IS_R300_CLASS(radeon->radeonScreen))
1369		return r300DestroyContext(driContextPriv);
1370}
1371
1372
1373#endif
1374
1375
1376/**
1377 * This is the driver specific part of the createNewScreen entry point.
1378 *
1379 * \todo maybe fold this into intelInitDriver
1380 *
1381 * \return the __GLcontextModes supported by this driver
1382 */
1383static const __DRIconfig **
1384radeonInitScreen(__DRIscreenPrivate *psp)
1385{
1386#if !RADEON_COMMON
1387   static const char *driver_name = "Radeon";
1388   static const __DRIutilversion2 ddx_expected = { 4, 5, 0, 0 };
1389   static const __DRIversion dri_expected = { 4, 0, 0 };
1390   static const __DRIversion drm_expected = { 1, 6, 0 };
1391#elif RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
1392   static const char *driver_name = "R200";
1393   static const __DRIutilversion2 ddx_expected = { 4, 5, 0, 0 };
1394   static const __DRIversion dri_expected = { 4, 0, 0 };
1395   static const __DRIversion drm_expected = { 1, 6, 0 };
1396#elif RADEON_COMMON && defined(RADEON_COMMON_FOR_R300)
1397   static const char *driver_name = "R300";
1398   static const __DRIutilversion2 ddx_expected = { 4, 5, 0, 0 };
1399   static const __DRIversion dri_expected = { 4, 0, 0 };
1400   static const __DRIversion drm_expected = { 1, 24, 0 };
1401#endif
1402   RADEONDRIPtr dri_priv = (RADEONDRIPtr) psp->pDevPriv;
1403
1404   if ( ! driCheckDriDdxDrmVersions3( driver_name,
1405				      &psp->dri_version, & dri_expected,
1406				      &psp->ddx_version, & ddx_expected,
1407				      &psp->drm_version, & drm_expected ) ) {
1408      return NULL;
1409   }
1410
1411   /* Calling driInitExtensions here, with a NULL context pointer,
1412    * does not actually enable the extensions.  It just makes sure
1413    * that all the dispatch offsets for all the extensions that
1414    * *might* be enables are known.  This is needed because the
1415    * dispatch offsets need to be known when _mesa_context_create
1416    * is called, but we can't enable the extensions until we have a
1417    * context pointer.
1418    *
1419    * Hello chicken.  Hello egg.  How are you two today?
1420    */
1421   driInitExtensions( NULL, card_extensions, GL_FALSE );
1422#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
1423   driInitExtensions( NULL, blend_extensions, GL_FALSE );
1424   driInitSingleExtension( NULL, ARB_vp_extension );
1425   driInitSingleExtension( NULL, NV_vp_extension );
1426   driInitSingleExtension( NULL, ATI_fs_extension );
1427   driInitExtensions( NULL, point_extensions, GL_FALSE );
1428#elif defined(RADEON_COMMON_FOR_R300)
1429   driInitSingleExtension( NULL, gl_20_extension );
1430#endif
1431
1432   if (!radeonInitDriver(psp))
1433       return NULL;
1434
1435   /* for now fill in all modes */
1436   return radeonFillInModes( psp,
1437			     dri_priv->bpp,
1438			     (dri_priv->bpp == 16) ? 16 : 24,
1439			     (dri_priv->bpp == 16) ? 0  : 8, 1);
1440}
1441#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
1442
1443/**
1444 * This is the driver specific part of the createNewScreen entry point.
1445 * Called when using DRI2.
1446 *
1447 * \return the __GLcontextModes supported by this driver
1448 */
1449static const
1450__DRIconfig **radeonInitScreen2(__DRIscreenPrivate *psp)
1451{
1452   GLenum fb_format[3];
1453   GLenum fb_type[3];
1454   /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
1455    * support pageflipping at all.
1456    */
1457   static const GLenum back_buffer_modes[] = {
1458     GLX_NONE, GLX_SWAP_UNDEFINED_OML, /*, GLX_SWAP_COPY_OML*/
1459   };
1460   uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
1461   int color;
1462   __DRIconfig **configs = NULL;
1463
1464   /* Calling driInitExtensions here, with a NULL context pointer,
1465    * does not actually enable the extensions.  It just makes sure
1466    * that all the dispatch offsets for all the extensions that
1467    * *might* be enables are known.  This is needed because the
1468    * dispatch offsets need to be known when _mesa_context_create
1469    * is called, but we can't enable the extensions until we have a
1470    * context pointer.
1471    *
1472    * Hello chicken.  Hello egg.  How are you two today?
1473    */
1474   driInitExtensions( NULL, card_extensions, GL_FALSE );
1475#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R200)
1476   driInitExtensions( NULL, blend_extensions, GL_FALSE );
1477   driInitSingleExtension( NULL, ARB_vp_extension );
1478   driInitSingleExtension( NULL, NV_vp_extension );
1479   driInitSingleExtension( NULL, ATI_fs_extension );
1480   driInitExtensions( NULL, point_extensions, GL_FALSE );
1481#endif
1482
1483   if (!radeonInitDriver(psp)) {
1484       return NULL;
1485    }
1486   depth_bits[0] = 0;
1487   stencil_bits[0] = 0;
1488   depth_bits[1] = 16;
1489   stencil_bits[1] = 0;
1490   depth_bits[2] = 24;
1491   stencil_bits[2] = 0;
1492   depth_bits[3] = 24;
1493   stencil_bits[3] = 8;
1494
1495   msaa_samples_array[0] = 0;
1496
1497   fb_format[0] = GL_RGB;
1498   fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
1499
1500   fb_format[1] = GL_BGR;
1501   fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
1502
1503   fb_format[2] = GL_BGRA;
1504   fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
1505
1506   for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
1507      __DRIconfig **new_configs;
1508
1509      new_configs = driCreateConfigs(fb_format[color], fb_type[color],
1510				     depth_bits,
1511				     stencil_bits,
1512				     ARRAY_SIZE(depth_bits),
1513				     back_buffer_modes,
1514				     ARRAY_SIZE(back_buffer_modes),
1515				     msaa_samples_array,
1516				     ARRAY_SIZE(msaa_samples_array));
1517      if (configs == NULL)
1518	 configs = new_configs;
1519      else
1520	 configs = driConcatConfigs(configs, new_configs);
1521   }
1522
1523   if (configs == NULL) {
1524      fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1525              __LINE__);
1526      return NULL;
1527   }
1528
1529   return (const __DRIconfig **)configs;
1530}
1531
1532/**
1533 * Get information about previous buffer swaps.
1534 */
1535static int
1536getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
1537{
1538   radeonContextPtr  rmesa;
1539
1540   if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
1541	|| (dPriv->driContextPriv->driverPrivate == NULL)
1542	|| (sInfo == NULL) ) {
1543      return -1;
1544   }
1545
1546   rmesa = dPriv->driContextPriv->driverPrivate;
1547   sInfo->swap_count = rmesa->swap_count;
1548   sInfo->swap_ust = rmesa->swap_ust;
1549   sInfo->swap_missed_count = rmesa->swap_missed_count;
1550
1551   sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
1552       ? driCalculateSwapUsage( dPriv, 0, rmesa->swap_missed_ust )
1553       : 0.0;
1554
1555   return 0;
1556}
1557
1558#if !RADEON_COMMON || (RADEON_COMMON && defined(RADEON_COMMON_FOR_R300))
1559const struct __DriverAPIRec driDriverAPI = {
1560   .InitScreen      = radeonInitScreen,
1561   .DestroyScreen   = radeonDestroyScreen,
1562   .CreateContext   = radeonCreateContext,
1563   .DestroyContext  = radeonDestroyContext,
1564   .CreateBuffer    = radeonCreateBuffer,
1565   .DestroyBuffer   = radeonDestroyBuffer,
1566   .SwapBuffers     = radeonSwapBuffers,
1567   .MakeCurrent     = radeonMakeCurrent,
1568   .UnbindContext   = radeonUnbindContext,
1569   .GetSwapInfo     = getSwapInfo,
1570   .GetDrawableMSC  = driDrawableGetMSC32,
1571   .WaitForMSC      = driWaitForMSC32,
1572   .WaitForSBC      = NULL,
1573   .SwapBuffersMSC  = NULL,
1574   .CopySubBuffer   = radeonCopySubBuffer,
1575    /* DRI2 */
1576   .InitScreen2     = radeonInitScreen2,
1577};
1578#else
1579const struct __DriverAPIRec driDriverAPI = {
1580   .InitScreen      = radeonInitScreen,
1581   .DestroyScreen   = radeonDestroyScreen,
1582   .CreateContext   = r200CreateContext,
1583   .DestroyContext  = r200DestroyContext,
1584   .CreateBuffer    = radeonCreateBuffer,
1585   .DestroyBuffer   = radeonDestroyBuffer,
1586   .SwapBuffers     = radeonSwapBuffers,
1587   .MakeCurrent     = radeonMakeCurrent,
1588   .UnbindContext   = radeonUnbindContext,
1589   .GetSwapInfo     = getSwapInfo,
1590   .GetDrawableMSC  = driDrawableGetMSC32,
1591   .WaitForMSC      = driWaitForMSC32,
1592   .WaitForSBC      = NULL,
1593   .SwapBuffersMSC  = NULL,
1594   .CopySubBuffer   = radeonCopySubBuffer,
1595   .InitScreen2     = radeonInitScreen2,
1596};
1597#endif
1598
1599