aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/r300/radeon_ioctl.c
blob: f75b75518431ae2c4e40fea0896f9b52ff185f3f (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
347
348
349
350
351
352
353
/*
Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.

The Weather Channel (TM) funded Tungsten Graphics to develop the
initial release of the Radeon 8500 driver under the XFree86 license.
This notice must be preserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice (including the
next paragraph) shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*
 * Authors:
 *   Keith Whitwell <keith@tungstengraphics.com>
 */

#include <sched.h>
#include <errno.h>

#include "glheader.h"
#include "imports.h"
#include "macros.h"
#include "context.h"
#include "swrast/swrast.h"

#include "r200_context.h"
#include "r300_context.h"
#include "r200_state.h"
#include "radeon_ioctl.h"
#include "r200_ioctl.h"
#include "r300_ioctl.h"
#include "r200_tcl.h"
#include "r200_sanity.h"
#include "radeon_reg.h"

#include "vblank.h"

static void radeonWaitForIdle(radeonContextPtr radeon);

/* ================================================================
 * SwapBuffers with client-side throttling
 */

static uint32_t radeonGetLastFrame(radeonContextPtr radeon)
{
	drm_radeon_getparam_t gp;
	int ret;
	uint32_t frame;

	gp.param = RADEON_PARAM_LAST_FRAME;
	gp.value = (int *)&frame;
	ret = drmCommandWriteRead(radeon->dri.fd, DRM_RADEON_GETPARAM,
				  &gp, sizeof(gp));
	if (ret) {
		fprintf(stderr, "%s: drmRadeonGetParam: %d\n", __FUNCTION__,
			ret);
		exit(1);
	}

	return frame;
}

static void radeonEmitIrqLocked(radeonContextPtr radeon)
{
	drm_radeon_irq_emit_t ie;
	int ret;

	ie.irq_seq = &radeon->iw.irq_seq;
	ret = drmCommandWriteRead(radeon->dri.fd, DRM_RADEON_IRQ_EMIT,
				  &ie, sizeof(ie));
	if (ret) {
		fprintf(stderr, "%s: drmRadeonIrqEmit: %d\n", __FUNCTION__,
			ret);
		exit(1);
	}
}

static void radeonWaitIrq(radeonContextPtr radeon)
{
	int ret;

	do {
		ret = drmCommandWrite(radeon->dri.fd, DRM_RADEON_IRQ_WAIT,
				      &radeon->iw, sizeof(radeon->iw));
	} while (ret && (errno == EINTR || errno == EAGAIN));

	if (ret) {
		fprintf(stderr, "%s: drmRadeonIrqWait: %d\n", __FUNCTION__,
			ret);
		exit(1);
	}
}

static void radeonWaitForFrameCompletion(radeonContextPtr radeon)
{
	drm_radeon_sarea_t *sarea = radeon->sarea;

	if (radeon->do_irqs) {
		if (radeonGetLastFrame(radeon) < sarea->last_frame) {
			if (!radeon->irqsEmitted) {
				while (radeonGetLastFrame(radeon) <
				       sarea->last_frame) ;
			} else {
				UNLOCK_HARDWARE(radeon);
				radeonWaitIrq(radeon);
				LOCK_HARDWARE(radeon);
			}
			radeon->irqsEmitted = 10;
		}

		if (radeon->irqsEmitted) {
			radeonEmitIrqLocked(radeon);
			radeon->irqsEmitted--;
		}
	} else {
		while (radeonGetLastFrame(radeon) < sarea->last_frame) {
			UNLOCK_HARDWARE(radeon);
			if (radeon->do_usleeps)
				DO_USLEEP(1);
			LOCK_HARDWARE(radeon);
		}
	}
}

/* Copy the back color buffer to the front color buffer.
 */
void radeonCopyBuffer(const __DRIdrawablePrivate * dPriv)
{
	radeonContextPtr radeon;
	GLint nbox, i, ret;
	GLboolean missed_target;
	int64_t ust;

	assert(dPriv);
	assert(dPriv->driContextPriv);
	assert(dPriv->driContextPriv->driverPrivate);

	radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;

	if (RADEON_DEBUG & DEBUG_IOCTL) {
		fprintf(stderr, "\n%s( %p )\n\n", __FUNCTION__,
			(void *)radeon->glCtx);
	}

	if (IS_FAMILY_R200(radeon))
		R200_FIREVERTICES((r200ContextPtr)radeon);
	else
		r300Flush(radeon->glCtx);

	LOCK_HARDWARE(radeon);

	/* Throttle the frame rate -- only allow one pending swap buffers
	 * request at a time.
	 */
	radeonWaitForFrameCompletion(radeon);
	UNLOCK_HARDWARE(radeon);
	driWaitForVBlank(dPriv, &radeon->vbl_seq, radeon->vblank_flags,
			 &missed_target);
	LOCK_HARDWARE(radeon);

	nbox = dPriv->numClipRects;	/* must be in locked region */

	for (i = 0; i < nbox;) {
		GLint nr = MIN2(i + RADEON_NR_SAREA_CLIPRECTS, nbox);
		drm_clip_rect_t *box = dPriv->pClipRects;
		drm_clip_rect_t *b = radeon->sarea->boxes;
		GLint n = 0;

		for (; i < nr; i++) {
			*b++ = box[i];
			n++;
		}
		radeon->sarea->nbox = n;

		ret = drmCommandNone(radeon->dri.fd, DRM_RADEON_SWAP);

		if (ret) {
			fprintf(stderr, "DRM_RADEON_SWAP: return = %d\n",
				ret);
			UNLOCK_HARDWARE(radeon);
			exit(1);
		}
	}

	UNLOCK_HARDWARE(radeon);

	if (IS_FAMILY_R200(radeon))
		((r200ContextPtr)radeon)->hw.all_dirty = GL_TRUE;
	else
		((r300ContextPtr)radeon)->hw.all_dirty = GL_TRUE;

	radeon->swap_count++;
	(*radeon->get_ust) (&ust);
	if (missed_target) {
		radeon->swap_missed_count++;
		radeon->swap_missed_ust = ust - radeon->swap_ust;
	}

	radeon->swap_ust = ust;

	sched_yield();
}

void radeonPageFlip(const __DRIdrawablePrivate * dPriv)
{
	radeonContextPtr radeon;
	GLint ret;
	GLboolean missed_target;

	assert(dPriv);
	assert(dPriv->driContextPriv);
	assert(dPriv->driContextPriv->driverPrivate);

	radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;

	if (RADEON_DEBUG & DEBUG_IOCTL) {
		fprintf(stderr, "%s: pfCurrentPage: %d\n", __FUNCTION__,
			radeon->sarea->pfCurrentPage);
	}

	if (IS_FAMILY_R200(radeon))
		R200_FIREVERTICES((r200ContextPtr)radeon);
	LOCK_HARDWARE(radeon);

	if (!dPriv->numClipRects) {
		UNLOCK_HARDWARE(radeon);
		usleep(10000);	/* throttle invisible client 10ms */
		return;
	}

	/* Need to do this for the perf box placement:
	 */
	{
		drm_clip_rect_t *box = dPriv->pClipRects;
		drm_clip_rect_t *b = radeon->sarea->boxes;
		b[0] = box[0];
		radeon->sarea->nbox = 1;
	}

	/* Throttle the frame rate -- only allow a few pending swap buffers
	 * request at a time.
	 */
	radeonWaitForFrameCompletion(radeon);
	UNLOCK_HARDWARE(radeon);
	driWaitForVBlank(dPriv, &radeon->vbl_seq, radeon->vblank_flags,
			 &missed_target);
	if (missed_target) {
		radeon->swap_missed_count++;
		(void)(*radeon->get_ust) (&radeon->swap_missed_ust);
	}
	LOCK_HARDWARE(radeon);

	ret = drmCommandNone(radeon->dri.fd, DRM_RADEON_FLIP);

	UNLOCK_HARDWARE(radeon);

	if (ret) {
		fprintf(stderr, "DRM_RADEON_FLIP: return = %d\n", ret);
		exit(1);
	}

	radeon->swap_count++;
	(void)(*radeon->get_ust) (&radeon->swap_ust);

	if (radeon->sarea->pfCurrentPage == 1) {
		radeon->state.color.drawOffset = radeon->radeonScreen->frontOffset;
		radeon->state.color.drawPitch = radeon->radeonScreen->frontPitch;
	} else {
		radeon->state.color.drawOffset = radeon->radeonScreen->backOffset;
		radeon->state.color.drawPitch = radeon->radeonScreen->backPitch;
	}

	if (IS_FAMILY_R200(radeon)) {
		r200ContextPtr r200 = (r200ContextPtr)radeon;

		R200_STATECHANGE(r200, ctx);
		r200->hw.ctx.cmd[CTX_RB3D_COLOROFFSET] = radeon->state.color.drawOffset
			+ radeon->radeonScreen->fbLocation;
		r200->hw.ctx.cmd[CTX_RB3D_COLORPITCH] = radeon->state.color.drawPitch;
	}
}

void radeonWaitForIdleLocked(radeonContextPtr radeon)
{
	int ret;
	int i = 0;

	do {
		ret = drmCommandNone(radeon->dri.fd, DRM_RADEON_CP_IDLE);
		if (ret)
			DO_USLEEP(1);
	} while (ret && ++i < 100);

	if (ret < 0) {
		UNLOCK_HARDWARE(radeon);
		fprintf(stderr, "Error: R200 timed out... exiting\n");
		exit(-1);
	}
}

static void radeonWaitForIdle(radeonContextPtr radeon)
{
	LOCK_HARDWARE(radeon);
	radeonWaitForIdleLocked(radeon);
	UNLOCK_HARDWARE(radeon);
}

void radeonFlush(GLcontext * ctx)
{
	radeonContextPtr radeon = RADEON_CONTEXT(ctx);

	if (IS_FAMILY_R300(radeon))
		r300Flush(ctx);
	else
		r200Flush(ctx);
}


/* Make sure all commands have been sent to the hardware and have
 * completed processing.
 */
void radeonFinish(GLcontext * ctx)
{
	radeonContextPtr radeon = RADEON_CONTEXT(ctx);

	radeonFlush(ctx);

	if (radeon->do_irqs) {
		LOCK_HARDWARE(radeon);
		radeonEmitIrqLocked(radeon);
		UNLOCK_HARDWARE(radeon);
		radeonWaitIrq(radeon);
	} else
		radeonWaitForIdle(radeon);
}