summaryrefslogtreecommitdiff
path: root/Source/DirectFB/gfxdrivers/savage/savage.c
blob: dffb8e38a1c62bfd6fb8292a3519d9be7ecdec94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi>,
              Claudio Ciccani <klan@users.sf.net> and
              Alex Song <alexsong@comports.com>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>

#include <dfb_types.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include <sys/mman.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <fbdev/fb.h>

#include <directfb.h>

#include <direct/messages.h>
#include <direct/util.h>

#include <core/coredefs.h>
#include <core/coretypes.h>

#include <core/state.h>
#include <core/surface.h>
#include <core/gfxcard.h>

#include <gfx/convert.h>
#include <gfx/util.h>

#include <misc/conf.h>
#include <misc/util.h>

#include <core/graphics_driver.h>

DFB_GRAPHICS_DRIVER( savage )

#include "savage.h"
#include "savage3d.h"
#include "savage4.h"
#include "savage2000.h"
#include "savage_bci.h"

/* exported symbols */

static int
driver_probe( CoreGraphicsDevice *device )
{
     switch (dfb_gfxcard_get_accelerator( device )) {
          case FB_ACCEL_SAVAGE3D:       /* Savage3D series     */
          case FB_ACCEL_SAVAGE3D_MV:
          case FB_ACCEL_SAVAGE_MX_MV:
          case FB_ACCEL_SAVAGE_MX:
          case FB_ACCEL_SAVAGE_IX_MV:
          case FB_ACCEL_SAVAGE_IX:
               return 1;

          case FB_ACCEL_SAVAGE4:        /* Savage4 series      */
          case FB_ACCEL_PROSAVAGE_PM:
          case FB_ACCEL_PROSAVAGE_KM:
          case FB_ACCEL_S3TWISTER_P:
          case FB_ACCEL_S3TWISTER_K:
               return 1;

          case FB_ACCEL_SAVAGE2000:     /* Savage 2000         */
               return 1;
     }

     return 0;
}

static void
driver_get_info( CoreGraphicsDevice *device,
                 GraphicsDriverInfo *info )
{
     /* fill driver info structure */
     snprintf( info->name,
               DFB_GRAPHICS_DRIVER_INFO_NAME_LENGTH,
               "Savage Driver" );

     snprintf( info->vendor,
               DFB_GRAPHICS_DRIVER_INFO_VENDOR_LENGTH,
               "directfb.org" );

     /* some defaults, each driver has it's own version */
     info->version.major = 0;
     info->version.minor = 3;

     info->driver_data_size = sizeof (SavageDriverData);
     info->device_data_size = sizeof (SavageDeviceData);

     switch (dfb_gfxcard_get_accelerator( device )) {
          case FB_ACCEL_SAVAGE3D:       /* Savage3D series */
          case FB_ACCEL_SAVAGE3D_MV:
          case FB_ACCEL_SAVAGE_MX_MV:
          case FB_ACCEL_SAVAGE_MX:
          case FB_ACCEL_SAVAGE_IX_MV:
          case FB_ACCEL_SAVAGE_IX:
               savage3d_get_info( device, info );
               break;

          case FB_ACCEL_SAVAGE4:        /* Savage4 series  */
          case FB_ACCEL_PROSAVAGE_PM:
          case FB_ACCEL_PROSAVAGE_KM:
          case FB_ACCEL_S3TWISTER_P:
          case FB_ACCEL_S3TWISTER_K:
               savage4_get_info( device, info );
               break;

          case FB_ACCEL_SAVAGE2000:     /* Savage 2000     */
               savage2000_get_info( device, info );
               break;
     }
}


static DFBResult
driver_init_driver( CoreGraphicsDevice  *device,
                    GraphicsDeviceFuncs *funcs,
                    void                *driver_data,
                    void                *device_data,
                    CoreDFB             *core )
{
     SavageDriverData *sdrv = (SavageDriverData*) driver_data;

     sdrv->mmio_base = (volatile u8*) dfb_gfxcard_map_mmio( device, 0, -1 );
     if (!sdrv->mmio_base)
          return DFB_IO;

     sdrv->bci_base = (volatile u32*)(sdrv->mmio_base + BCI_BUFFER_OFFSET);

     switch (dfb_gfxcard_get_accelerator( device )) {
          case FB_ACCEL_SAVAGE3D:       /* Savage3D series */
          case FB_ACCEL_SAVAGE3D_MV:
          case FB_ACCEL_SAVAGE_MX_MV:
          case FB_ACCEL_SAVAGE_MX:
          case FB_ACCEL_SAVAGE_IX_MV:
          case FB_ACCEL_SAVAGE_IX:
               return savage3d_init_driver( device, funcs, driver_data );

          case FB_ACCEL_SAVAGE4:        /* Savage4 series  */
          case FB_ACCEL_PROSAVAGE_PM:
          case FB_ACCEL_PROSAVAGE_KM:
          case FB_ACCEL_S3TWISTER_P:
          case FB_ACCEL_S3TWISTER_K:
               return savage4_init_driver( device, funcs, driver_data );

          case FB_ACCEL_SAVAGE2000:     /* Savage 2000     */
               return savage2000_init_driver( device, funcs, driver_data );
     }

     return DFB_BUG;
}

static DFBResult
driver_init_device( CoreGraphicsDevice *device,
                    GraphicsDeviceInfo *device_info,
                    void               *driver_data,
                    void               *device_data )
{
     SavageDriverData *sdrv = (SavageDriverData*) driver_data;
     SavageDeviceData *sdev = (SavageDeviceData*) device_data;
     volatile u8      *mmio = sdrv->mmio_base;

     /* use polling for syncing, artefacts occur otherwise */
     dfb_config->pollvsync_after = 1;

     sdev->accel_id = dfb_gfxcard_get_accelerator( device );

     switch (sdev->accel_id) {
          case FB_ACCEL_SAVAGE3D:       /* Savage3D series */
          case FB_ACCEL_SAVAGE3D_MV:
          case FB_ACCEL_SAVAGE_MX_MV:
          case FB_ACCEL_SAVAGE_MX:
          case FB_ACCEL_SAVAGE_IX_MV:
          case FB_ACCEL_SAVAGE_IX:
               savage3d_init_device( device, device_info,
                                     driver_data, device_data );
               break;

          case FB_ACCEL_SAVAGE4:        /* Savage4 series  */
          case FB_ACCEL_PROSAVAGE_PM:
          case FB_ACCEL_PROSAVAGE_KM:
          case FB_ACCEL_S3TWISTER_P:
          case FB_ACCEL_S3TWISTER_K:
               savage4_init_device( device, device_info,
                                    driver_data, device_data );
               break;

          case FB_ACCEL_SAVAGE2000:     /* Savage 2000     */
               savage2000_init_device( device, device_info,
                                       driver_data, device_data );
               break;
          default:
               D_BUG("unexpected accelerator id");
               return DFB_BUG;
     }

     /* Turn on 16-bit register access. */

     vga_out8( mmio, 0x3d4, 0x31);
     vga_out8( mmio, 0x3d5, 0x0c);

     /* Set stride to use GBD. */

     vga_out8( mmio, 0x3d4, 0x50);
     vga_out8( mmio, 0x3d5, vga_in8( mmio, 0x3d5 ) | 0xC1);

     /* Enable 2D engine. */

     vga_out8( mmio, 0x3d4, 0x40 );
     vga_out8( mmio, 0x3d5, 0x01 );


     savage_out32( mmio, MONO_PAT_0, ~0 );
     savage_out32( mmio, MONO_PAT_1, ~0 );

     /* Setup plane masks */
     savage_out32( mmio, 0x8128, ~0 ); /* enable all write planes */
     savage_out32( mmio, 0x812C, ~0 ); /* enable all read planes */
     savage_out16( mmio, 0x8134, 0x27 );
     savage_out16( mmio, 0x8136, 0x07 );

     return DFB_OK;
}

static void
driver_close_device( CoreGraphicsDevice *device,
                     void               *driver_data,
                     void               *device_data )
{
     SavageDeviceData *sdev = (SavageDeviceData*) device_data;

     (void) sdev;

     switch (dfb_gfxcard_get_accelerator( device )) {
          case FB_ACCEL_SAVAGE3D:       /* Savage3D series */
          case FB_ACCEL_SAVAGE3D_MV:
          case FB_ACCEL_SAVAGE_MX_MV:
          case FB_ACCEL_SAVAGE_MX:
          case FB_ACCEL_SAVAGE_IX_MV:
          case FB_ACCEL_SAVAGE_IX:
               savage3d_close_device( device, driver_data, device_data );
               break;

          case FB_ACCEL_SAVAGE4:        /* Savage4 series  */
          case FB_ACCEL_PROSAVAGE_PM:
          case FB_ACCEL_PROSAVAGE_KM:
          case FB_ACCEL_S3TWISTER_P:
          case FB_ACCEL_S3TWISTER_K:
               savage4_close_device( device, driver_data, device_data );
               break;

          case FB_ACCEL_SAVAGE2000:     /* Savage 2000     */
               savage2000_close_device( device, driver_data, device_data );
               break;
     }

     D_DEBUG( "DirectFBSavage: FIFO Performance Monitoring:\n" );
     D_DEBUG( "DirectFBSavage:  %9d savage_waitfifo calls\n",
               sdev->waitfifo_calls );
     D_DEBUG( "DirectFBSavage:  %9d savage_waitidle calls\n",
               sdev->waitidle_calls );
     D_DEBUG( "DirectFBSavage:  %9d register writes (savage_waitfifo sum)\n",
               sdev->waitfifo_sum );
     D_DEBUG( "DirectFBSavage:  %9d FIFO wait cycles (depends on CPU)\n",
               sdev->fifo_waitcycles );
     D_DEBUG( "DirectFBSavage:  %9d IDLE wait cycles (depends on CPU)\n",
               sdev->idle_waitcycles );
     D_DEBUG( "DirectFBSavage:  %9d FIFO space cache hits(depends on CPU)\n",
               sdev->fifo_cache_hits );
     D_DEBUG( "DirectFBSavage: Conclusion:\n" );
     D_DEBUG( "DirectFBSavage:  Average register writes/savage_waitfifo "
               "call: %.2f\n",
               sdev->waitfifo_sum/(float)(sdev->waitfifo_calls) );
     D_DEBUG( "DirectFBSavage:  Average wait cycles/savage_waitfifo call:"
               " %.2f\n",
               sdev->fifo_waitcycles/(float)(sdev->waitfifo_calls) );
     D_DEBUG( "DirectFBSavage:  Average wait cycles/savage_waitidle call:"
               " %.2f\n",
               sdev->idle_waitcycles/(float)(sdev->waitidle_calls) );
     D_DEBUG( "DirectFBSavage:  Average fifo space cache hits: %02d%%\n",
               (int)(100 * sdev->fifo_cache_hits/
               (float)(sdev->waitfifo_calls)) );
}

static void
driver_close_driver( CoreGraphicsDevice *device,
                     void               *driver_data )
{
     SavageDriverData *sdrv = (SavageDriverData*) driver_data;

     switch (dfb_gfxcard_get_accelerator( device )) {
          case FB_ACCEL_SAVAGE3D:       /* Savage3D series */
          case FB_ACCEL_SAVAGE3D_MV:
          case FB_ACCEL_SAVAGE_MX_MV:
          case FB_ACCEL_SAVAGE_MX:
          case FB_ACCEL_SAVAGE_IX_MV:
          case FB_ACCEL_SAVAGE_IX:
               savage3d_close_driver( device, driver_data );
               break;

          case FB_ACCEL_SAVAGE4:        /* Savage4 series  */
          case FB_ACCEL_PROSAVAGE_PM:
          case FB_ACCEL_PROSAVAGE_KM:
          case FB_ACCEL_S3TWISTER_P:
          case FB_ACCEL_S3TWISTER_K:
               savage4_close_driver( device, driver_data );
               break;

          case FB_ACCEL_SAVAGE2000:     /* Savage 2000     */
               savage2000_close_driver( device, driver_data );
               break;
     }

     dfb_gfxcard_unmap_mmio( device, sdrv->mmio_base, -1 );
}