blob: fdf220bd25e0a9f7fdd2eec9013355974e19f420 [file] [log] [blame]
Gareth Hughese15a24e2000-12-02 06:14:18 +00001/* r128_cce.c -- ATI Rage 128 driver -*- linux-c -*-
2 * Created: Wed Apr 5 19:24:19 2000 by kevin@precisioninsight.com
3 *
4 * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
Gareth Hughes01a14782001-02-16 05:24:06 +000028 * Gareth Hughes <gareth@valinux.com>
Gareth Hughese15a24e2000-12-02 06:14:18 +000029 */
30
Gareth Hughes36047532001-02-15 08:12:14 +000031#include "r128.h"
Gareth Hughese15a24e2000-12-02 06:14:18 +000032#include "drmP.h"
Jens Owen3903e5a2002-04-09 21:54:56 +000033#include "drm.h"
34#include "r128_drm.h"
Gareth Hughese15a24e2000-12-02 06:14:18 +000035#include "r128_drv.h"
36
Kevin E Martin0994e632001-01-05 22:57:55 +000037#define R128_FIFO_DEBUG 0
Gareth Hughese15a24e2000-12-02 06:14:18 +000038
Gareth Hughese15a24e2000-12-02 06:14:18 +000039/* CCE microcode (from ATI) */
40static u32 r128_cce_microcode[] = {
41 0, 276838400, 0, 268449792, 2, 142, 2, 145, 0, 1076765731, 0,
42 1617039951, 0, 774592877, 0, 1987540286, 0, 2307490946U, 0,
43 599558925, 0, 589505315, 0, 596487092, 0, 589505315, 1,
44 11544576, 1, 206848, 1, 311296, 1, 198656, 2, 912273422, 11,
45 262144, 0, 0, 1, 33559837, 1, 7438, 1, 14809, 1, 6615, 12, 28,
46 1, 6614, 12, 28, 2, 23, 11, 18874368, 0, 16790922, 1, 409600, 9,
47 30, 1, 147854772, 16, 420483072, 3, 8192, 0, 10240, 1, 198656,
48 1, 15630, 1, 51200, 10, 34858, 9, 42, 1, 33559823, 2, 10276, 1,
49 15717, 1, 15718, 2, 43, 1, 15936948, 1, 570480831, 1, 14715071,
50 12, 322123831, 1, 33953125, 12, 55, 1, 33559908, 1, 15718, 2,
51 46, 4, 2099258, 1, 526336, 1, 442623, 4, 4194365, 1, 509952, 1,
52 459007, 3, 0, 12, 92, 2, 46, 12, 176, 1, 15734, 1, 206848, 1,
53 18432, 1, 133120, 1, 100670734, 1, 149504, 1, 165888, 1,
54 15975928, 1, 1048576, 6, 3145806, 1, 15715, 16, 2150645232U, 2,
55 268449859, 2, 10307, 12, 176, 1, 15734, 1, 15735, 1, 15630, 1,
56 15631, 1, 5253120, 6, 3145810, 16, 2150645232U, 1, 15864, 2, 82,
57 1, 343310, 1, 1064207, 2, 3145813, 1, 15728, 1, 7817, 1, 15729,
58 3, 15730, 12, 92, 2, 98, 1, 16168, 1, 16167, 1, 16002, 1, 16008,
59 1, 15974, 1, 15975, 1, 15990, 1, 15976, 1, 15977, 1, 15980, 0,
60 15981, 1, 10240, 1, 5253120, 1, 15720, 1, 198656, 6, 110, 1,
61 180224, 1, 103824738, 2, 112, 2, 3145839, 0, 536885440, 1,
62 114880, 14, 125, 12, 206975, 1, 33559995, 12, 198784, 0,
63 33570236, 1, 15803, 0, 15804, 3, 294912, 1, 294912, 3, 442370,
64 1, 11544576, 0, 811612160, 1, 12593152, 1, 11536384, 1,
65 14024704, 7, 310382726, 0, 10240, 1, 14796, 1, 14797, 1, 14793,
66 1, 14794, 0, 14795, 1, 268679168, 1, 9437184, 1, 268449792, 1,
67 198656, 1, 9452827, 1, 1075854602, 1, 1075854603, 1, 557056, 1,
68 114880, 14, 159, 12, 198784, 1, 1109409213, 12, 198783, 1,
69 1107312059, 12, 198784, 1, 1109409212, 2, 162, 1, 1075854781, 1,
70 1073757627, 1, 1075854780, 1, 540672, 1, 10485760, 6, 3145894,
71 16, 274741248, 9, 168, 3, 4194304, 3, 4209949, 0, 0, 0, 256, 14,
72 174, 1, 114857, 1, 33560007, 12, 176, 0, 10240, 1, 114858, 1,
73 33560018, 1, 114857, 3, 33560007, 1, 16008, 1, 114874, 1,
74 33560360, 1, 114875, 1, 33560154, 0, 15963, 0, 256, 0, 4096, 1,
75 409611, 9, 188, 0, 10240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
82};
83
Gareth Hughese15a24e2000-12-02 06:14:18 +000084int R128_READ_PLL(drm_device_t *dev, int addr)
85{
86 drm_r128_private_t *dev_priv = dev->dev_private;
87
88 R128_WRITE8(R128_CLOCK_CNTL_INDEX, addr & 0x1f);
89 return R128_READ(R128_CLOCK_CNTL_DATA);
90}
91
Kevin E Martin0994e632001-01-05 22:57:55 +000092#if R128_FIFO_DEBUG
Gareth Hughese15a24e2000-12-02 06:14:18 +000093static void r128_status( drm_r128_private_t *dev_priv )
94{
95 printk( "GUI_STAT = 0x%08x\n",
96 (unsigned int)R128_READ( R128_GUI_STAT ) );
97 printk( "PM4_STAT = 0x%08x\n",
98 (unsigned int)R128_READ( R128_PM4_STAT ) );
99 printk( "PM4_BUFFER_DL_WPTR = 0x%08x\n",
100 (unsigned int)R128_READ( R128_PM4_BUFFER_DL_WPTR ) );
101 printk( "PM4_BUFFER_DL_RPTR = 0x%08x\n",
102 (unsigned int)R128_READ( R128_PM4_BUFFER_DL_RPTR ) );
103 printk( "PM4_MICRO_CNTL = 0x%08x\n",
104 (unsigned int)R128_READ( R128_PM4_MICRO_CNTL ) );
105 printk( "PM4_BUFFER_CNTL = 0x%08x\n",
106 (unsigned int)R128_READ( R128_PM4_BUFFER_CNTL ) );
107}
Gareth Hughes87258282000-12-12 14:50:50 +0000108#endif
Gareth Hughese15a24e2000-12-02 06:14:18 +0000109
110
111/* ================================================================
112 * Engine, FIFO control
113 */
114
115static int r128_do_pixcache_flush( drm_r128_private_t *dev_priv )
116{
117 u32 tmp;
118 int i;
119
120 tmp = R128_READ( R128_PC_NGUI_CTLSTAT ) | R128_PC_FLUSH_ALL;
121 R128_WRITE( R128_PC_NGUI_CTLSTAT, tmp );
122
123 for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
124 if ( !(R128_READ( R128_PC_NGUI_CTLSTAT ) & R128_PC_BUSY) ) {
125 return 0;
126 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000127 DRM_UDELAY( 1 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000128 }
129
David Dawesd87c8732001-06-14 22:23:44 +0000130#if R128_FIFO_DEBUG
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000131 DRM_ERROR( "failed!\n" );
David Dawesd87c8732001-06-14 22:23:44 +0000132#endif
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000133 return DRM_ERR(EBUSY);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000134}
135
136static int r128_do_wait_for_fifo( drm_r128_private_t *dev_priv, int entries )
137{
138 int i;
139
140 for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
141 int slots = R128_READ( R128_GUI_STAT ) & R128_GUI_FIFOCNT_MASK;
142 if ( slots >= entries ) return 0;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000143 DRM_UDELAY( 1 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000144 }
145
David Dawesd87c8732001-06-14 22:23:44 +0000146#if R128_FIFO_DEBUG
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000147 DRM_ERROR( "failed!\n" );
David Dawesd87c8732001-06-14 22:23:44 +0000148#endif
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000149 return DRM_ERR(EBUSY);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000150}
151
Eric Anholt0f094c32003-08-18 23:42:16 +0000152static int r128_do_wait_for_idle( drm_r128_private_t *dev_priv )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000153{
154 int i, ret;
155
156 ret = r128_do_wait_for_fifo( dev_priv, 64 );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000157 if ( ret ) return ret;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000158
159 for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
160 if ( !(R128_READ( R128_GUI_STAT ) & R128_GUI_ACTIVE) ) {
161 r128_do_pixcache_flush( dev_priv );
162 return 0;
163 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000164 DRM_UDELAY( 1 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000165 }
166
David Dawesd87c8732001-06-14 22:23:44 +0000167#if R128_FIFO_DEBUG
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000168 DRM_ERROR( "failed!\n" );
David Dawesd87c8732001-06-14 22:23:44 +0000169#endif
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000170 return DRM_ERR(EBUSY);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000171}
172
173
174/* ================================================================
175 * CCE control, initialization
176 */
177
178/* Load the microcode for the CCE */
179static void r128_cce_load_microcode( drm_r128_private_t *dev_priv )
180{
181 int i;
182
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000183 DRM_DEBUG( "\n" );
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000184
Gareth Hughese15a24e2000-12-02 06:14:18 +0000185 r128_do_wait_for_idle( dev_priv );
186
187 R128_WRITE( R128_PM4_MICROCODE_ADDR, 0 );
188 for ( i = 0 ; i < 256 ; i++ ) {
189 R128_WRITE( R128_PM4_MICROCODE_DATAH,
190 r128_cce_microcode[i * 2] );
191 R128_WRITE( R128_PM4_MICROCODE_DATAL,
192 r128_cce_microcode[i * 2 + 1] );
193 }
194}
195
196/* Flush any pending commands to the CCE. This should only be used just
197 * prior to a wait for idle, as it informs the engine that the command
198 * stream is ending.
199 */
200static void r128_do_cce_flush( drm_r128_private_t *dev_priv )
201{
202 u32 tmp;
203
204 tmp = R128_READ( R128_PM4_BUFFER_DL_WPTR ) | R128_PM4_BUFFER_DL_DONE;
205 R128_WRITE( R128_PM4_BUFFER_DL_WPTR, tmp );
206}
207
208/* Wait for the CCE to go idle.
209 */
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000210int r128_do_cce_idle( drm_r128_private_t *dev_priv )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000211{
212 int i;
213
214 for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
Michel Daenzer355b2042003-10-16 14:18:52 +0000215 if ( GET_RING_HEAD( dev_priv ) == dev_priv->ring.tail ) {
Gareth Hughese15a24e2000-12-02 06:14:18 +0000216 int pm4stat = R128_READ( R128_PM4_STAT );
217 if ( ( (pm4stat & R128_PM4_FIFOCNT_MASK) >=
218 dev_priv->cce_fifo_size ) &&
219 !(pm4stat & (R128_PM4_BUSY |
220 R128_PM4_GUI_ACTIVE)) ) {
221 return r128_do_pixcache_flush( dev_priv );
222 }
223 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000224 DRM_UDELAY( 1 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000225 }
226
Kevin E Martin0994e632001-01-05 22:57:55 +0000227#if R128_FIFO_DEBUG
Gareth Hughese15a24e2000-12-02 06:14:18 +0000228 DRM_ERROR( "failed!\n" );
229 r128_status( dev_priv );
230#endif
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000231 return DRM_ERR(EBUSY);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000232}
233
234/* Start the Concurrent Command Engine.
235 */
236static void r128_do_cce_start( drm_r128_private_t *dev_priv )
237{
238 r128_do_wait_for_idle( dev_priv );
239
240 R128_WRITE( R128_PM4_BUFFER_CNTL,
Michel Daenzer355b2042003-10-16 14:18:52 +0000241 dev_priv->cce_mode | dev_priv->ring.size_l2qw
242 | R128_PM4_BUFFER_CNTL_NOUPDATE );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000243 R128_READ( R128_PM4_BUFFER_ADDR ); /* as per the sample code */
244 R128_WRITE( R128_PM4_MICRO_CNTL, R128_PM4_MICRO_FREERUN );
245
246 dev_priv->cce_running = 1;
247}
248
249/* Reset the Concurrent Command Engine. This will not flush any pending
Kevin E Martin0994e632001-01-05 22:57:55 +0000250 * commands, so you must wait for the CCE command stream to complete
Gareth Hughese15a24e2000-12-02 06:14:18 +0000251 * before calling this routine.
252 */
253static void r128_do_cce_reset( drm_r128_private_t *dev_priv )
254{
255 R128_WRITE( R128_PM4_BUFFER_DL_WPTR, 0 );
256 R128_WRITE( R128_PM4_BUFFER_DL_RPTR, 0 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000257 dev_priv->ring.tail = 0;
258}
259
260/* Stop the Concurrent Command Engine. This will not flush any pending
Kevin E Martin0994e632001-01-05 22:57:55 +0000261 * commands, so you must flush the command stream and wait for the CCE
Gareth Hughese15a24e2000-12-02 06:14:18 +0000262 * to go idle before calling this routine.
263 */
264static void r128_do_cce_stop( drm_r128_private_t *dev_priv )
265{
266 R128_WRITE( R128_PM4_MICRO_CNTL, 0 );
Michel Daenzer355b2042003-10-16 14:18:52 +0000267 R128_WRITE( R128_PM4_BUFFER_CNTL,
268 R128_PM4_NONPM4 | R128_PM4_BUFFER_CNTL_NOUPDATE );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000269
270 dev_priv->cce_running = 0;
271}
272
273/* Reset the engine. This will stop the CCE if it is running.
274 */
275static int r128_do_engine_reset( drm_device_t *dev )
276{
277 drm_r128_private_t *dev_priv = dev->dev_private;
278 u32 clock_cntl_index, mclk_cntl, gen_reset_cntl;
279
280 r128_do_pixcache_flush( dev_priv );
281
282 clock_cntl_index = R128_READ( R128_CLOCK_CNTL_INDEX );
283 mclk_cntl = R128_READ_PLL( dev, R128_MCLK_CNTL );
284
285 R128_WRITE_PLL( R128_MCLK_CNTL,
286 mclk_cntl | R128_FORCE_GCP | R128_FORCE_PIPE3D_CP );
287
288 gen_reset_cntl = R128_READ( R128_GEN_RESET_CNTL );
289
290 /* Taken from the sample code - do not change */
291 R128_WRITE( R128_GEN_RESET_CNTL,
292 gen_reset_cntl | R128_SOFT_RESET_GUI );
293 R128_READ( R128_GEN_RESET_CNTL );
294 R128_WRITE( R128_GEN_RESET_CNTL,
295 gen_reset_cntl & ~R128_SOFT_RESET_GUI );
296 R128_READ( R128_GEN_RESET_CNTL );
297
298 R128_WRITE_PLL( R128_MCLK_CNTL, mclk_cntl );
299 R128_WRITE( R128_CLOCK_CNTL_INDEX, clock_cntl_index );
300 R128_WRITE( R128_GEN_RESET_CNTL, gen_reset_cntl );
301
302 /* Reset the CCE ring */
303 r128_do_cce_reset( dev_priv );
304
305 /* The CCE is no longer running after an engine reset */
306 dev_priv->cce_running = 0;
307
308 /* Reset any pending vertex, indirect buffers */
309 r128_freelist_reset( dev );
310
311 return 0;
312}
313
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000314static void r128_cce_init_ring_buffer( drm_device_t *dev,
315 drm_r128_private_t *dev_priv )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000316{
Gareth Hughese15a24e2000-12-02 06:14:18 +0000317 u32 ring_start;
318 u32 tmp;
319
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000320 DRM_DEBUG( "\n" );
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000321
Gareth Hughese15a24e2000-12-02 06:14:18 +0000322 /* The manual (p. 2) says this address is in "VM space". This
323 * means it's an offset from the start of AGP space.
324 */
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000325#if __REALLY_HAVE_AGP
326 if ( !dev_priv->is_pci )
327 ring_start = dev_priv->cce_ring->offset - dev->agp->base;
328 else
329#endif
330 ring_start = dev_priv->cce_ring->offset - dev->sg->handle;
331
Gareth Hughese15a24e2000-12-02 06:14:18 +0000332 R128_WRITE( R128_PM4_BUFFER_OFFSET, ring_start | R128_AGP_OFFSET );
333
334 R128_WRITE( R128_PM4_BUFFER_DL_WPTR, 0 );
335 R128_WRITE( R128_PM4_BUFFER_DL_RPTR, 0 );
336
Gareth Hughese15a24e2000-12-02 06:14:18 +0000337 /* Set watermark control */
338 R128_WRITE( R128_PM4_BUFFER_WM_CNTL,
339 ((R128_WATERMARK_L/4) << R128_WMA_SHIFT)
340 | ((R128_WATERMARK_M/4) << R128_WMB_SHIFT)
341 | ((R128_WATERMARK_N/4) << R128_WMC_SHIFT)
342 | ((R128_WATERMARK_K/64) << R128_WB_WM_SHIFT) );
343
344 /* Force read. Why? Because it's in the examples... */
345 R128_READ( R128_PM4_BUFFER_ADDR );
346
347 /* Turn on bus mastering */
348 tmp = R128_READ( R128_BUS_CNTL ) & ~R128_BUS_MASTER_DIS;
349 R128_WRITE( R128_BUS_CNTL, tmp );
350}
351
352static int r128_do_init_cce( drm_device_t *dev, drm_r128_init_t *init )
353{
354 drm_r128_private_t *dev_priv;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000355
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000356 DRM_DEBUG( "\n" );
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000357
Gareth Hughes36047532001-02-15 08:12:14 +0000358 dev_priv = DRM(alloc)( sizeof(drm_r128_private_t), DRM_MEM_DRIVER );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000359 if ( dev_priv == NULL )
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000360 return DRM_ERR(ENOMEM);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000361
362 memset( dev_priv, 0, sizeof(drm_r128_private_t) );
363
364 dev_priv->is_pci = init->is_pci;
365
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000366 if ( dev_priv->is_pci && !dev->sg ) {
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000367 DRM_ERROR( "PCI GART memory not allocated!\n" );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000368 dev->dev_private = (void *)dev_priv;
369 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000370 return DRM_ERR(EINVAL);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000371 }
372
373 dev_priv->usec_timeout = init->usec_timeout;
374 if ( dev_priv->usec_timeout < 1 ||
375 dev_priv->usec_timeout > R128_MAX_USEC_TIMEOUT ) {
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000376 DRM_DEBUG( "TIMEOUT problem!\n" );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000377 dev->dev_private = (void *)dev_priv;
378 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000379 return DRM_ERR(EINVAL);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000380 }
381
382 dev_priv->cce_mode = init->cce_mode;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000383
384 /* GH: Simple idle check.
385 */
386 atomic_set( &dev_priv->idle_count, 0 );
387
388 /* We don't support anything other than bus-mastering ring mode,
389 * but the ring can be in either AGP or PCI space for the ring
390 * read pointer.
391 */
392 if ( ( init->cce_mode != R128_PM4_192BM ) &&
393 ( init->cce_mode != R128_PM4_128BM_64INDBM ) &&
394 ( init->cce_mode != R128_PM4_64BM_128INDBM ) &&
395 ( init->cce_mode != R128_PM4_64BM_64VCBM_64INDBM ) ) {
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000396 DRM_DEBUG( "Bad cce_mode!\n" );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000397 dev->dev_private = (void *)dev_priv;
398 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000399 return DRM_ERR(EINVAL);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000400 }
401
402 switch ( init->cce_mode ) {
403 case R128_PM4_NONPM4:
404 dev_priv->cce_fifo_size = 0;
405 break;
406 case R128_PM4_192PIO:
407 case R128_PM4_192BM:
408 dev_priv->cce_fifo_size = 192;
409 break;
410 case R128_PM4_128PIO_64INDBM:
411 case R128_PM4_128BM_64INDBM:
412 dev_priv->cce_fifo_size = 128;
413 break;
414 case R128_PM4_64PIO_128INDBM:
415 case R128_PM4_64BM_128INDBM:
416 case R128_PM4_64PIO_64VCBM_64INDBM:
417 case R128_PM4_64BM_64VCBM_64INDBM:
418 case R128_PM4_64PIO_64VCPIO_64INDPIO:
419 dev_priv->cce_fifo_size = 64;
420 break;
421 }
422
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000423 switch ( init->fb_bpp ) {
424 case 16:
425 dev_priv->color_fmt = R128_DATATYPE_RGB565;
426 break;
427 case 32:
428 default:
429 dev_priv->color_fmt = R128_DATATYPE_ARGB8888;
430 break;
431 }
Gareth Hughese15a24e2000-12-02 06:14:18 +0000432 dev_priv->front_offset = init->front_offset;
433 dev_priv->front_pitch = init->front_pitch;
434 dev_priv->back_offset = init->back_offset;
435 dev_priv->back_pitch = init->back_pitch;
436
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000437 switch ( init->depth_bpp ) {
438 case 16:
439 dev_priv->depth_fmt = R128_DATATYPE_RGB565;
440 break;
441 case 24:
442 case 32:
443 default:
444 dev_priv->depth_fmt = R128_DATATYPE_ARGB8888;
445 break;
446 }
Gareth Hughese15a24e2000-12-02 06:14:18 +0000447 dev_priv->depth_offset = init->depth_offset;
448 dev_priv->depth_pitch = init->depth_pitch;
449 dev_priv->span_offset = init->span_offset;
450
451 dev_priv->front_pitch_offset_c = (((dev_priv->front_pitch/8) << 21) |
452 (dev_priv->front_offset >> 5));
453 dev_priv->back_pitch_offset_c = (((dev_priv->back_pitch/8) << 21) |
454 (dev_priv->back_offset >> 5));
455 dev_priv->depth_pitch_offset_c = (((dev_priv->depth_pitch/8) << 21) |
456 (dev_priv->depth_offset >> 5) |
457 R128_DST_TILE);
458 dev_priv->span_pitch_offset_c = (((dev_priv->depth_pitch/8) << 21) |
459 (dev_priv->span_offset >> 5));
460
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000461 DRM_GETSAREA();
462
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000463 if(!dev_priv->sarea) {
464 DRM_ERROR("could not find sarea!\n");
465 dev->dev_private = (void *)dev_priv;
466 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000467 return DRM_ERR(EINVAL);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000468 }
Gareth Hughese15a24e2000-12-02 06:14:18 +0000469
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000470 DRM_FIND_MAP( dev_priv->fb, init->fb_offset );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000471 if(!dev_priv->fb) {
472 DRM_ERROR("could not find framebuffer!\n");
473 dev->dev_private = (void *)dev_priv;
474 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000475 return DRM_ERR(EINVAL);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000476 }
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000477 DRM_FIND_MAP( dev_priv->mmio, init->mmio_offset );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000478 if(!dev_priv->mmio) {
479 DRM_ERROR("could not find mmio region!\n");
480 dev->dev_private = (void *)dev_priv;
481 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000482 return DRM_ERR(EINVAL);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000483 }
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000484 DRM_FIND_MAP( dev_priv->cce_ring, init->ring_offset );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000485 if(!dev_priv->cce_ring) {
486 DRM_ERROR("could not find cce ring region!\n");
487 dev->dev_private = (void *)dev_priv;
488 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000489 return DRM_ERR(EINVAL);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000490 }
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000491 DRM_FIND_MAP( dev_priv->ring_rptr, init->ring_rptr_offset );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000492 if(!dev_priv->ring_rptr) {
493 DRM_ERROR("could not find ring read pointer!\n");
494 dev->dev_private = (void *)dev_priv;
495 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000496 return DRM_ERR(EINVAL);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000497 }
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000498 DRM_FIND_MAP( dev_priv->buffers, init->buffers_offset );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000499 if(!dev_priv->buffers) {
500 DRM_ERROR("could not find dma buffer region!\n");
501 dev->dev_private = (void *)dev_priv;
502 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000503 return DRM_ERR(EINVAL);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000504 }
Gareth Hughese15a24e2000-12-02 06:14:18 +0000505
506 if ( !dev_priv->is_pci ) {
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000507 DRM_FIND_MAP( dev_priv->agp_textures,
508 init->agp_textures_offset );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000509 if(!dev_priv->agp_textures) {
510 DRM_ERROR("could not find agp texture region!\n");
511 dev->dev_private = (void *)dev_priv;
512 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000513 return DRM_ERR(EINVAL);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000514 }
Gareth Hughese15a24e2000-12-02 06:14:18 +0000515 }
516
517 dev_priv->sarea_priv =
518 (drm_r128_sarea_t *)((u8 *)dev_priv->sarea->handle +
519 init->sarea_priv_offset);
520
Michel Daenzere5d3c7f2003-05-16 23:41:27 +0000521#if __REALLY_HAVE_AGP
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000522 if ( !dev_priv->is_pci ) {
Michel Daenzere5d3c7f2003-05-16 23:41:27 +0000523 DRM_IOREMAP( dev_priv->cce_ring, dev );
524 DRM_IOREMAP( dev_priv->ring_rptr, dev );
525 DRM_IOREMAP( dev_priv->buffers, dev );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000526 if(!dev_priv->cce_ring->handle ||
527 !dev_priv->ring_rptr->handle ||
528 !dev_priv->buffers->handle) {
529 DRM_ERROR("Could not ioremap agp regions!\n");
530 dev->dev_private = (void *)dev_priv;
531 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000532 return DRM_ERR(ENOMEM);
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000533 }
Michel Daenzere5d3c7f2003-05-16 23:41:27 +0000534 } else
535#endif
536 {
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000537 dev_priv->cce_ring->handle =
538 (void *)dev_priv->cce_ring->offset;
539 dev_priv->ring_rptr->handle =
540 (void *)dev_priv->ring_rptr->offset;
541 dev_priv->buffers->handle = (void *)dev_priv->buffers->offset;
542 }
543
544#if __REALLY_HAVE_AGP
545 if ( !dev_priv->is_pci )
546 dev_priv->cce_buffers_offset = dev->agp->base;
547 else
548#endif
549 dev_priv->cce_buffers_offset = dev->sg->handle;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000550
Gareth Hughese15a24e2000-12-02 06:14:18 +0000551 dev_priv->ring.start = (u32 *)dev_priv->cce_ring->handle;
552 dev_priv->ring.end = ((u32 *)dev_priv->cce_ring->handle
553 + init->ring_size / sizeof(u32));
554 dev_priv->ring.size = init->ring_size;
Gareth Hughes36047532001-02-15 08:12:14 +0000555 dev_priv->ring.size_l2qw = DRM(order)( init->ring_size / 8 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000556
557 dev_priv->ring.tail_mask =
558 (dev_priv->ring.size / sizeof(u32)) - 1;
559
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000560 dev_priv->ring.high_mark = 128;
561
Gareth Hughese15a24e2000-12-02 06:14:18 +0000562 dev_priv->sarea_priv->last_frame = 0;
563 R128_WRITE( R128_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame );
564
565 dev_priv->sarea_priv->last_dispatch = 0;
566 R128_WRITE( R128_LAST_DISPATCH_REG,
567 dev_priv->sarea_priv->last_dispatch );
568
Michel Daenzer355b2042003-10-16 14:18:52 +0000569#if __REALLY_HAVE_AGP
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000570 if ( dev_priv->is_pci ) {
Michel Daenzer355b2042003-10-16 14:18:52 +0000571#endif
Jeff Hartmann97b8aa52001-08-10 16:29:21 +0000572 if (!DRM(ati_pcigart_init)( dev, &dev_priv->phys_pci_gart,
573 &dev_priv->bus_pci_gart) ) {
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000574 DRM_ERROR( "failed to init PCI GART!\n" );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000575 dev->dev_private = (void *)dev_priv;
576 r128_do_cleanup_cce( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000577 return DRM_ERR(ENOMEM);
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000578 }
Jeff Hartmann97b8aa52001-08-10 16:29:21 +0000579 R128_WRITE( R128_PCI_GART_PAGE, dev_priv->bus_pci_gart );
Michel Daenzer355b2042003-10-16 14:18:52 +0000580#if __REALLY_HAVE_AGP
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000581 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000582#endif
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000583
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000584 r128_cce_init_ring_buffer( dev, dev_priv );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000585 r128_cce_load_microcode( dev_priv );
Jeff Hartmann51e38d92001-08-07 18:15:10 +0000586
587 dev->dev_private = (void *)dev_priv;
588
Gareth Hughese15a24e2000-12-02 06:14:18 +0000589 r128_do_engine_reset( dev );
590
591 return 0;
592}
593
Gareth Hughes36047532001-02-15 08:12:14 +0000594int r128_do_cleanup_cce( drm_device_t *dev )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000595{
Leif Delgassf2a0c542003-04-26 22:28:56 +0000596
Eric Anholt2950f9e2003-10-17 05:13:48 +0000597#if __HAVE_IRQ
Leif Delgassf2a0c542003-04-26 22:28:56 +0000598 /* Make sure interrupts are disabled here because the uninstall ioctl
599 * may not have been called from userspace and after dev_private
600 * is freed, it's too late.
601 */
602 if ( dev->irq ) DRM(irq_uninstall)(dev);
603#endif
604
Gareth Hughese15a24e2000-12-02 06:14:18 +0000605 if ( dev->dev_private ) {
606 drm_r128_private_t *dev_priv = dev->dev_private;
607
Michel Daenzere5d3c7f2003-05-16 23:41:27 +0000608#if __REALLY_HAVE_AGP
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000609 if ( !dev_priv->is_pci ) {
Leif Delgass46e06192003-04-21 16:07:17 +0000610 if ( dev_priv->cce_ring != NULL )
Michel Daenzere5d3c7f2003-05-16 23:41:27 +0000611 DRM_IOREMAPFREE( dev_priv->cce_ring, dev );
Leif Delgass46e06192003-04-21 16:07:17 +0000612 if ( dev_priv->ring_rptr != NULL )
Michel Daenzere5d3c7f2003-05-16 23:41:27 +0000613 DRM_IOREMAPFREE( dev_priv->ring_rptr, dev );
Leif Delgass46e06192003-04-21 16:07:17 +0000614 if ( dev_priv->buffers != NULL )
Michel Daenzere5d3c7f2003-05-16 23:41:27 +0000615 DRM_IOREMAPFREE( dev_priv->buffers, dev );
616 } else
617#endif
618 {
Jeff Hartmann97b8aa52001-08-10 16:29:21 +0000619 if (!DRM(ati_pcigart_cleanup)( dev,
620 dev_priv->phys_pci_gart,
621 dev_priv->bus_pci_gart ))
622 DRM_ERROR( "failed to cleanup PCI GART!\n" );
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000623 }
Gareth Hughese15a24e2000-12-02 06:14:18 +0000624
Gareth Hughes36047532001-02-15 08:12:14 +0000625 DRM(free)( dev->dev_private, sizeof(drm_r128_private_t),
626 DRM_MEM_DRIVER );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000627 dev->dev_private = NULL;
628 }
629
630 return 0;
631}
632
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000633int r128_cce_init( DRM_IOCTL_ARGS )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000634{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000635 DRM_DEVICE;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000636 drm_r128_init_t init;
637
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000638 DRM_DEBUG( "\n" );
Kevin E Martin5d6ddbc2001-04-05 22:16:12 +0000639
Leif Delgassf2a0c542003-04-26 22:28:56 +0000640 LOCK_TEST_WITH_RETURN( dev, filp );
641
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000642 DRM_COPY_FROM_USER_IOCTL( init, (drm_r128_init_t *)data, sizeof(init) );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000643
644 switch ( init.func ) {
645 case R128_INIT_CCE:
646 return r128_do_init_cce( dev, &init );
647 case R128_CLEANUP_CCE:
648 return r128_do_cleanup_cce( dev );
649 }
650
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000651 return DRM_ERR(EINVAL);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000652}
653
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000654int r128_cce_start( DRM_IOCTL_ARGS )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000655{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000656 DRM_DEVICE;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000657 drm_r128_private_t *dev_priv = dev->dev_private;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000658 DRM_DEBUG( "\n" );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000659
Keith Whitwell1728bc62003-03-28 14:27:37 +0000660 LOCK_TEST_WITH_RETURN( dev, filp );
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000661
Gareth Hughese15a24e2000-12-02 06:14:18 +0000662 if ( dev_priv->cce_running || dev_priv->cce_mode == R128_PM4_NONPM4 ) {
Keith Whitwell4fcde1e2002-08-29 07:34:49 +0000663 DRM_DEBUG( "%s while CCE running\n", __FUNCTION__ );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000664 return 0;
665 }
666
667 r128_do_cce_start( dev_priv );
668
669 return 0;
670}
671
672/* Stop the CCE. The engine must have been idled before calling this
673 * routine.
674 */
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000675int r128_cce_stop( DRM_IOCTL_ARGS )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000676{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000677 DRM_DEVICE;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000678 drm_r128_private_t *dev_priv = dev->dev_private;
679 drm_r128_cce_stop_t stop;
680 int ret;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000681 DRM_DEBUG( "\n" );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000682
Keith Whitwell1728bc62003-03-28 14:27:37 +0000683 LOCK_TEST_WITH_RETURN( dev, filp );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000684
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000685 DRM_COPY_FROM_USER_IOCTL(stop, (drm_r128_cce_stop_t *)data, sizeof(stop) );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000686
687 /* Flush any pending CCE commands. This ensures any outstanding
688 * commands are exectuted by the engine before we turn it off.
689 */
690 if ( stop.flush ) {
691 r128_do_cce_flush( dev_priv );
692 }
693
694 /* If we fail to make the engine go idle, we return an error
695 * code so that the DRM ioctl wrapper can try again.
696 */
697 if ( stop.idle ) {
698 ret = r128_do_cce_idle( dev_priv );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000699 if ( ret ) return ret;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000700 }
701
702 /* Finally, we can turn off the CCE. If the engine isn't idle,
703 * we will get some dropped triangles as they won't be fully
704 * rendered before the CCE is shut down.
705 */
706 r128_do_cce_stop( dev_priv );
707
708 /* Reset the engine */
709 r128_do_engine_reset( dev );
710
711 return 0;
712}
713
714/* Just reset the CCE ring. Called as part of an X Server engine reset.
715 */
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000716int r128_cce_reset( DRM_IOCTL_ARGS )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000717{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000718 DRM_DEVICE;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000719 drm_r128_private_t *dev_priv = dev->dev_private;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000720 DRM_DEBUG( "\n" );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000721
Keith Whitwell1728bc62003-03-28 14:27:37 +0000722 LOCK_TEST_WITH_RETURN( dev, filp );
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000723
Gareth Hughese15a24e2000-12-02 06:14:18 +0000724 if ( !dev_priv ) {
Keith Whitwell4fcde1e2002-08-29 07:34:49 +0000725 DRM_DEBUG( "%s called before init done\n", __FUNCTION__ );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000726 return DRM_ERR(EINVAL);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000727 }
728
729 r128_do_cce_reset( dev_priv );
730
731 /* The CCE is no longer running after an engine reset */
732 dev_priv->cce_running = 0;
733
734 return 0;
735}
736
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000737int r128_cce_idle( DRM_IOCTL_ARGS )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000738{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000739 DRM_DEVICE;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000740 drm_r128_private_t *dev_priv = dev->dev_private;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000741 DRM_DEBUG( "\n" );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000742
Keith Whitwell1728bc62003-03-28 14:27:37 +0000743 LOCK_TEST_WITH_RETURN( dev, filp );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000744
745 if ( dev_priv->cce_running ) {
746 r128_do_cce_flush( dev_priv );
747 }
748
749 return r128_do_cce_idle( dev_priv );
750}
751
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000752int r128_engine_reset( DRM_IOCTL_ARGS )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000753{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000754 DRM_DEVICE;
755 DRM_DEBUG( "\n" );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000756
Keith Whitwell1728bc62003-03-28 14:27:37 +0000757 LOCK_TEST_WITH_RETURN( dev, filp );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000758
759 return r128_do_engine_reset( dev );
760}
761
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000762int r128_fullscreen( DRM_IOCTL_ARGS )
Kevin E Martin0994e632001-01-05 22:57:55 +0000763{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000764 return DRM_ERR(EINVAL);
Kevin E Martin0994e632001-01-05 22:57:55 +0000765}
766
767
768/* ================================================================
Gareth Hughese15a24e2000-12-02 06:14:18 +0000769 * Freelist management
770 */
771#define R128_BUFFER_USED 0xffffffff
772#define R128_BUFFER_FREE 0
773
Gareth Hughes87258282000-12-12 14:50:50 +0000774#if 0
Gareth Hughese15a24e2000-12-02 06:14:18 +0000775static int r128_freelist_init( drm_device_t *dev )
776{
777 drm_device_dma_t *dma = dev->dma;
778 drm_r128_private_t *dev_priv = dev->dev_private;
779 drm_buf_t *buf;
780 drm_r128_buf_priv_t *buf_priv;
781 drm_r128_freelist_t *entry;
782 int i;
783
Gareth Hughes36047532001-02-15 08:12:14 +0000784 dev_priv->head = DRM(alloc)( sizeof(drm_r128_freelist_t),
785 DRM_MEM_DRIVER );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000786 if ( dev_priv->head == NULL )
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000787 return DRM_ERR(ENOMEM);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000788
789 memset( dev_priv->head, 0, sizeof(drm_r128_freelist_t) );
790 dev_priv->head->age = R128_BUFFER_USED;
791
792 for ( i = 0 ; i < dma->buf_count ; i++ ) {
793 buf = dma->buflist[i];
794 buf_priv = buf->dev_private;
795
Gareth Hughes36047532001-02-15 08:12:14 +0000796 entry = DRM(alloc)( sizeof(drm_r128_freelist_t),
797 DRM_MEM_DRIVER );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000798 if ( !entry ) return DRM_ERR(ENOMEM);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000799
800 entry->age = R128_BUFFER_FREE;
801 entry->buf = buf;
802 entry->prev = dev_priv->head;
803 entry->next = dev_priv->head->next;
804 if ( !entry->next )
805 dev_priv->tail = entry;
806
807 buf_priv->discard = 0;
808 buf_priv->dispatched = 0;
809 buf_priv->list_entry = entry;
810
811 dev_priv->head->next = entry;
812
813 if ( dev_priv->head->next )
814 dev_priv->head->next->prev = entry;
815 }
816
817 return 0;
818
819}
Gareth Hughes87258282000-12-12 14:50:50 +0000820#endif
Gareth Hughese15a24e2000-12-02 06:14:18 +0000821
822drm_buf_t *r128_freelist_get( drm_device_t *dev )
823{
824 drm_device_dma_t *dma = dev->dma;
825 drm_r128_private_t *dev_priv = dev->dev_private;
826 drm_r128_buf_priv_t *buf_priv;
827 drm_buf_t *buf;
828 int i, t;
829
830 /* FIXME: Optimize -- use freelist code */
831
832 for ( i = 0 ; i < dma->buf_count ; i++ ) {
833 buf = dma->buflist[i];
834 buf_priv = buf->dev_private;
Keith Whitwell1728bc62003-03-28 14:27:37 +0000835 if ( buf->filp == 0 )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000836 return buf;
837 }
838
839 for ( t = 0 ; t < dev_priv->usec_timeout ; t++ ) {
840 u32 done_age = R128_READ( R128_LAST_DISPATCH_REG );
841
842 for ( i = 0 ; i < dma->buf_count ; i++ ) {
843 buf = dma->buflist[i];
844 buf_priv = buf->dev_private;
845 if ( buf->pending && buf_priv->age <= done_age ) {
846 /* The buffer has been processed, so it
847 * can now be used.
848 */
849 buf->pending = 0;
850 return buf;
851 }
852 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000853 DRM_UDELAY( 1 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000854 }
855
Michel Daenzerc26ffea2003-07-26 15:59:09 +0000856 DRM_DEBUG( "returning NULL!\n" );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000857 return NULL;
858}
859
860void r128_freelist_reset( drm_device_t *dev )
861{
862 drm_device_dma_t *dma = dev->dma;
863 int i;
864
865 for ( i = 0 ; i < dma->buf_count ; i++ ) {
866 drm_buf_t *buf = dma->buflist[i];
867 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
868 buf_priv->age = 0;
869 }
870}
871
872
873/* ================================================================
Kevin E Martin0994e632001-01-05 22:57:55 +0000874 * CCE command submission
Gareth Hughese15a24e2000-12-02 06:14:18 +0000875 */
876
877int r128_wait_ring( drm_r128_private_t *dev_priv, int n )
878{
879 drm_r128_ring_buffer_t *ring = &dev_priv->ring;
880 int i;
881
882 for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
Michel Daenzer355b2042003-10-16 14:18:52 +0000883 r128_update_ring_snapshot( dev_priv );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000884 if ( ring->space >= n )
885 return 0;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000886 DRM_UDELAY( 1 );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000887 }
888
Kevin E Martin0994e632001-01-05 22:57:55 +0000889 /* FIXME: This is being ignored... */
890 DRM_ERROR( "failed!\n" );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000891 return DRM_ERR(EBUSY);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000892}
893
Keith Whitwell1728bc62003-03-28 14:27:37 +0000894static int r128_cce_get_buffers( DRMFILE filp, drm_device_t *dev, drm_dma_t *d )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000895{
896 int i;
897 drm_buf_t *buf;
898
899 for ( i = d->granted_count ; i < d->request_count ; i++ ) {
900 buf = r128_freelist_get( dev );
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000901 if ( !buf ) return DRM_ERR(EAGAIN);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000902
Keith Whitwell1728bc62003-03-28 14:27:37 +0000903 buf->filp = filp;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000904
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000905 if ( DRM_COPY_TO_USER( &d->request_indices[i], &buf->idx,
Gareth Hughese15a24e2000-12-02 06:14:18 +0000906 sizeof(buf->idx) ) )
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000907 return DRM_ERR(EFAULT);
908 if ( DRM_COPY_TO_USER( &d->request_sizes[i], &buf->total,
Gareth Hughese15a24e2000-12-02 06:14:18 +0000909 sizeof(buf->total) ) )
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000910 return DRM_ERR(EFAULT);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000911
912 d->granted_count++;
913 }
914 return 0;
915}
916
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000917int r128_cce_buffers( DRM_IOCTL_ARGS )
Gareth Hughese15a24e2000-12-02 06:14:18 +0000918{
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000919 DRM_DEVICE;
Gareth Hughese15a24e2000-12-02 06:14:18 +0000920 drm_device_dma_t *dma = dev->dma;
921 int ret = 0;
922 drm_dma_t d;
923
Keith Whitwell1728bc62003-03-28 14:27:37 +0000924 LOCK_TEST_WITH_RETURN( dev, filp );
Gareth Hughes4d2a4452001-01-24 15:34:46 +0000925
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000926 DRM_COPY_FROM_USER_IOCTL( d, (drm_dma_t *) data, sizeof(d) );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000927
Gareth Hughese15a24e2000-12-02 06:14:18 +0000928 /* Please don't send us buffers.
929 */
930 if ( d.send_count != 0 ) {
931 DRM_ERROR( "Process %d trying to send %d buffers via drmDMA\n",
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000932 DRM_CURRENTPID, d.send_count );
933 return DRM_ERR(EINVAL);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000934 }
935
936 /* We'll send you buffers.
937 */
938 if ( d.request_count < 0 || d.request_count > dma->buf_count ) {
939 DRM_ERROR( "Process %d trying to get %d buffers (of %d max)\n",
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000940 DRM_CURRENTPID, d.request_count, dma->buf_count );
941 return DRM_ERR(EINVAL);
Gareth Hughese15a24e2000-12-02 06:14:18 +0000942 }
943
944 d.granted_count = 0;
945
946 if ( d.request_count ) {
Keith Whitwell1728bc62003-03-28 14:27:37 +0000947 ret = r128_cce_get_buffers( filp, dev, &d );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000948 }
949
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000950 DRM_COPY_TO_USER_IOCTL((drm_dma_t *) data, d, sizeof(d) );
Gareth Hughese15a24e2000-12-02 06:14:18 +0000951
952 return ret;
953}