radeon_state.c 91.7 KB
Newer Older
1 2
/* radeon_state.c -- State support for Radeon -*- linux-c -*- */
/*
L
Linus Torvalds 已提交
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
 * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
 * All Rights Reserved.
 *
 * 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
 * PRECISION INSIGHT 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:
 *    Gareth Hughes <gareth@valinux.com>
 *    Kevin E. Martin <martin@valinux.com>
 */

#include "drmP.h"
#include "drm.h"
#include "drm_sarea.h"
#include "radeon_drm.h"
#include "radeon_drv.h"

/* ================================================================
 * Helper functions for client state checking and fixup
 */

D
Dave Airlie 已提交
40 41
static __inline__ int radeon_check_and_fixup_offset(drm_radeon_private_t *
						    dev_priv,
42
						    struct drm_file * file_priv,
43
						    u32 *offset)
D
Dave Airlie 已提交
44
{
45
	u64 off = *offset;
46
	u32 fb_end = dev_priv->fb_location + dev_priv->fb_size - 1;
L
Linus Torvalds 已提交
47 48
	struct drm_radeon_driver_file_fields *radeon_priv;

49 50 51 52 53 54 55 56 57 58 59 60 61
	/* Hrm ... the story of the offset ... So this function converts
	 * the various ideas of what userland clients might have for an
	 * offset in the card address space into an offset into the card
	 * address space :) So with a sane client, it should just keep
	 * the value intact and just do some boundary checking. However,
	 * not all clients are sane. Some older clients pass us 0 based
	 * offsets relative to the start of the framebuffer and some may
	 * assume the AGP aperture it appended to the framebuffer, so we
	 * try to detect those cases and fix them up.
	 *
	 * Note: It might be a good idea here to make sure the offset lands
	 * in some "allowed" area to protect things like the PCIE GART...
	 */
L
Linus Torvalds 已提交
62

63 64 65
	/* First, the best case, the offset already lands in either the
	 * framebuffer or the GART mapped space
	 */
66
	if (radeon_check_offset(dev_priv, off))
67
		return 0;
L
Linus Torvalds 已提交
68

69 70 71 72 73
	/* Ok, that didn't happen... now check if we have a zero based
	 * offset that fits in the framebuffer + gart space, apply the
	 * magic offset we get from SETPARAM or calculated from fb_location
	 */
	if (off < (dev_priv->fb_size + dev_priv->gart_size)) {
74
		radeon_priv = file_priv->driver_priv;
75 76
		off += radeon_priv->radeon_fb_delta;
	}
L
Linus Torvalds 已提交
77

78
	/* Finally, assume we aimed at a GART offset if beyond the fb */
79
	if (off > fb_end)
80
		off = off - fb_end - 1 + dev_priv->gart_vm_start;
L
Linus Torvalds 已提交
81

82
	/* Now recheck and fail if out of bounds */
83
	if (radeon_check_offset(dev_priv, off)) {
84
		DRM_DEBUG("offset fixed up to 0x%x\n", (unsigned int)off);
85 86 87
		*offset = off;
		return 0;
	}
E
Eric Anholt 已提交
88
	return -EINVAL;
L
Linus Torvalds 已提交
89 90
}

D
Dave Airlie 已提交
91 92
static __inline__ int radeon_check_and_fixup_packets(drm_radeon_private_t *
						     dev_priv,
93
						     struct drm_file *file_priv,
94
						     int id, u32 *data)
D
Dave Airlie 已提交
95 96
{
	switch (id) {
L
Linus Torvalds 已提交
97 98

	case RADEON_EMIT_PP_MISC:
99
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
100
		    &data[(RADEON_RB3D_DEPTHOFFSET - RADEON_PP_MISC) / 4])) {
D
Dave Airlie 已提交
101
			DRM_ERROR("Invalid depth buffer offset\n");
E
Eric Anholt 已提交
102
			return -EINVAL;
L
Linus Torvalds 已提交
103 104 105 106
		}
		break;

	case RADEON_EMIT_PP_CNTL:
107
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
108
		    &data[(RADEON_RB3D_COLOROFFSET - RADEON_PP_CNTL) / 4])) {
D
Dave Airlie 已提交
109
			DRM_ERROR("Invalid colour buffer offset\n");
E
Eric Anholt 已提交
110
			return -EINVAL;
L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119
		}
		break;

	case R200_EMIT_PP_TXOFFSET_0:
	case R200_EMIT_PP_TXOFFSET_1:
	case R200_EMIT_PP_TXOFFSET_2:
	case R200_EMIT_PP_TXOFFSET_3:
	case R200_EMIT_PP_TXOFFSET_4:
	case R200_EMIT_PP_TXOFFSET_5:
120
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
D
Dave Airlie 已提交
121 122
						  &data[0])) {
			DRM_ERROR("Invalid R200 texture offset\n");
E
Eric Anholt 已提交
123
			return -EINVAL;
L
Linus Torvalds 已提交
124 125 126 127 128 129
		}
		break;

	case RADEON_EMIT_PP_TXFILTER_0:
	case RADEON_EMIT_PP_TXFILTER_1:
	case RADEON_EMIT_PP_TXFILTER_2:
130
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
131
		    &data[(RADEON_PP_TXOFFSET_0 - RADEON_PP_TXFILTER_0) / 4])) {
D
Dave Airlie 已提交
132
			DRM_ERROR("Invalid R100 texture offset\n");
E
Eric Anholt 已提交
133
			return -EINVAL;
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141
		}
		break;

	case R200_EMIT_PP_CUBIC_OFFSETS_0:
	case R200_EMIT_PP_CUBIC_OFFSETS_1:
	case R200_EMIT_PP_CUBIC_OFFSETS_2:
	case R200_EMIT_PP_CUBIC_OFFSETS_3:
	case R200_EMIT_PP_CUBIC_OFFSETS_4:
D
Dave Airlie 已提交
142 143 144
	case R200_EMIT_PP_CUBIC_OFFSETS_5:{
			int i;
			for (i = 0; i < 5; i++) {
145
				if (radeon_check_and_fixup_offset(dev_priv,
146
								  file_priv,
147
								  &data[i])) {
D
Dave Airlie 已提交
148 149
					DRM_ERROR
					    ("Invalid R200 cubic texture offset\n");
E
Eric Anholt 已提交
150
					return -EINVAL;
D
Dave Airlie 已提交
151
				}
L
Linus Torvalds 已提交
152
			}
D
Dave Airlie 已提交
153
			break;
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161
		}

	case RADEON_EMIT_PP_CUBIC_OFFSETS_T0:
	case RADEON_EMIT_PP_CUBIC_OFFSETS_T1:
	case RADEON_EMIT_PP_CUBIC_OFFSETS_T2:{
			int i;
			for (i = 0; i < 5; i++) {
				if (radeon_check_and_fixup_offset(dev_priv,
162
								  file_priv,
L
Linus Torvalds 已提交
163 164 165
								  &data[i])) {
					DRM_ERROR
					    ("Invalid R100 cubic texture offset\n");
E
Eric Anholt 已提交
166
					return -EINVAL;
L
Linus Torvalds 已提交
167 168 169 170 171
				}
			}
		}
		break;

172 173 174 175 176 177 178 179
	case R200_EMIT_VAP_CTL:{
			RING_LOCALS;
			BEGIN_RING(2);
			OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
			ADVANCE_RING();
		}
		break;

L
Linus Torvalds 已提交
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
	case RADEON_EMIT_RB3D_COLORPITCH:
	case RADEON_EMIT_RE_LINE_PATTERN:
	case RADEON_EMIT_SE_LINE_WIDTH:
	case RADEON_EMIT_PP_LUM_MATRIX:
	case RADEON_EMIT_PP_ROT_MATRIX_0:
	case RADEON_EMIT_RB3D_STENCILREFMASK:
	case RADEON_EMIT_SE_VPORT_XSCALE:
	case RADEON_EMIT_SE_CNTL:
	case RADEON_EMIT_SE_CNTL_STATUS:
	case RADEON_EMIT_RE_MISC:
	case RADEON_EMIT_PP_BORDER_COLOR_0:
	case RADEON_EMIT_PP_BORDER_COLOR_1:
	case RADEON_EMIT_PP_BORDER_COLOR_2:
	case RADEON_EMIT_SE_ZBIAS_FACTOR:
	case RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT:
	case RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED:
	case R200_EMIT_PP_TXCBLEND_0:
	case R200_EMIT_PP_TXCBLEND_1:
	case R200_EMIT_PP_TXCBLEND_2:
	case R200_EMIT_PP_TXCBLEND_3:
	case R200_EMIT_PP_TXCBLEND_4:
	case R200_EMIT_PP_TXCBLEND_5:
	case R200_EMIT_PP_TXCBLEND_6:
	case R200_EMIT_PP_TXCBLEND_7:
	case R200_EMIT_TCL_LIGHT_MODEL_CTL_0:
	case R200_EMIT_TFACTOR_0:
	case R200_EMIT_VTX_FMT_0:
	case R200_EMIT_MATRIX_SELECT_0:
	case R200_EMIT_TEX_PROC_CTL_2:
	case R200_EMIT_TCL_UCP_VERT_BLEND_CTL:
	case R200_EMIT_PP_TXFILTER_0:
	case R200_EMIT_PP_TXFILTER_1:
	case R200_EMIT_PP_TXFILTER_2:
	case R200_EMIT_PP_TXFILTER_3:
	case R200_EMIT_PP_TXFILTER_4:
	case R200_EMIT_PP_TXFILTER_5:
	case R200_EMIT_VTE_CNTL:
	case R200_EMIT_OUTPUT_VTX_COMP_SEL:
	case R200_EMIT_PP_TAM_DEBUG3:
	case R200_EMIT_PP_CNTL_X:
	case R200_EMIT_RB3D_DEPTHXY_OFFSET:
	case R200_EMIT_RE_AUX_SCISSOR_CNTL:
	case R200_EMIT_RE_SCISSOR_TL_0:
	case R200_EMIT_RE_SCISSOR_TL_1:
	case R200_EMIT_RE_SCISSOR_TL_2:
	case R200_EMIT_SE_VAP_CNTL_STATUS:
	case R200_EMIT_SE_VTX_STATE_CNTL:
	case R200_EMIT_RE_POINTSIZE:
	case R200_EMIT_TCL_INPUT_VTX_VECTOR_ADDR_0:
	case R200_EMIT_PP_CUBIC_FACES_0:
	case R200_EMIT_PP_CUBIC_FACES_1:
	case R200_EMIT_PP_CUBIC_FACES_2:
	case R200_EMIT_PP_CUBIC_FACES_3:
	case R200_EMIT_PP_CUBIC_FACES_4:
	case R200_EMIT_PP_CUBIC_FACES_5:
	case RADEON_EMIT_PP_TEX_SIZE_0:
	case RADEON_EMIT_PP_TEX_SIZE_1:
	case RADEON_EMIT_PP_TEX_SIZE_2:
	case R200_EMIT_RB3D_BLENDCOLOR:
	case R200_EMIT_TCL_POINT_SPRITE_CNTL:
	case RADEON_EMIT_PP_CUBIC_FACES_0:
	case RADEON_EMIT_PP_CUBIC_FACES_1:
	case RADEON_EMIT_PP_CUBIC_FACES_2:
	case R200_EMIT_PP_TRI_PERF_CNTL:
D
Dave Airlie 已提交
244 245 246 247 248 249 250 251 252
	case R200_EMIT_PP_AFS_0:
	case R200_EMIT_PP_AFS_1:
	case R200_EMIT_ATF_TFACTOR:
	case R200_EMIT_PP_TXCTLALL_0:
	case R200_EMIT_PP_TXCTLALL_1:
	case R200_EMIT_PP_TXCTLALL_2:
	case R200_EMIT_PP_TXCTLALL_3:
	case R200_EMIT_PP_TXCTLALL_4:
	case R200_EMIT_PP_TXCTLALL_5:
253
	case R200_EMIT_VAP_PVS_CNTL:
L
Linus Torvalds 已提交
254 255 256 257
		/* These packets don't contain memory offsets */
		break;

	default:
D
Dave Airlie 已提交
258
		DRM_ERROR("Unknown state packet ID %d\n", id);
E
Eric Anholt 已提交
259
		return -EINVAL;
L
Linus Torvalds 已提交
260 261 262 263 264
	}

	return 0;
}

D
Dave Airlie 已提交
265 266
static __inline__ int radeon_check_and_fixup_packet3(drm_radeon_private_t *
						     dev_priv,
267
						     struct drm_file *file_priv,
268 269
						     drm_radeon_kcmd_buffer_t *
						     cmdbuf,
D
Dave Airlie 已提交
270 271
						     unsigned int *cmdsz)
{
L
Linus Torvalds 已提交
272
	u32 *cmd = (u32 *) cmdbuf->buf;
273 274
	u32 offset, narrays;
	int count, i, k;
L
Linus Torvalds 已提交
275

D
Dave Airlie 已提交
276
	*cmdsz = 2 + ((cmd[0] & RADEON_CP_PACKET_COUNT_MASK) >> 16);
L
Linus Torvalds 已提交
277

D
Dave Airlie 已提交
278 279
	if ((cmd[0] & 0xc0000000) != RADEON_CP_PACKET3) {
		DRM_ERROR("Not a type 3 packet\n");
E
Eric Anholt 已提交
280
		return -EINVAL;
L
Linus Torvalds 已提交
281 282
	}

D
Dave Airlie 已提交
283 284
	if (4 * *cmdsz > cmdbuf->bufsz) {
		DRM_ERROR("Packet size larger than size of data provided\n");
E
Eric Anholt 已提交
285
		return -EINVAL;
L
Linus Torvalds 已提交
286 287
	}

288 289
	switch(cmd[0] & 0xff00) {
	/* XXX Are there old drivers needing other packets? */
L
Linus Torvalds 已提交
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	case RADEON_3D_DRAW_IMMD:
	case RADEON_3D_DRAW_VBUF:
	case RADEON_3D_DRAW_INDX:
	case RADEON_WAIT_FOR_IDLE:
	case RADEON_CP_NOP:
	case RADEON_3D_CLEAR_ZMASK:
/*	case RADEON_CP_NEXT_CHAR:
	case RADEON_CP_PLY_NEXTSCAN:
	case RADEON_CP_SET_SCISSORS: */ /* probably safe but will never need them? */
		/* these packets are safe */
		break;

	case RADEON_CP_3D_DRAW_IMMD_2:
	case RADEON_CP_3D_DRAW_VBUF_2:
	case RADEON_CP_3D_DRAW_INDX_2:
	case RADEON_3D_CLEAR_HIZ:
		/* safe but r200 only */
		if (dev_priv->microcode_version != UCODE_R200) {
			DRM_ERROR("Invalid 3d packet for r100-class chip\n");
E
Eric Anholt 已提交
310
			return -EINVAL;
311 312 313 314 315 316 317 318 319
		}
		break;

	case RADEON_3D_LOAD_VBPNTR:
		count = (cmd[0] >> 16) & 0x3fff;

		if (count > 18) { /* 12 arrays max */
			DRM_ERROR("Too large payload in 3D_LOAD_VBPNTR (count=%d)\n",
				  count);
E
Eric Anholt 已提交
320
			return -EINVAL;
321 322 323 324 325 326 327 328
		}

		/* carefully check packet contents */
		narrays = cmd[1] & ~0xc000;
		k = 0;
		i = 2;
		while ((k < narrays) && (i < (count + 2))) {
			i++;		/* skip attribute field */
329 330
			if (radeon_check_and_fixup_offset(dev_priv, file_priv,
							  &cmd[i])) {
331 332 333
				DRM_ERROR
				    ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
				     k, i);
E
Eric Anholt 已提交
334
				return -EINVAL;
335 336 337 338 339 340
			}
			k++;
			i++;
			if (k == narrays)
				break;
			/* have one more to process, they come in pairs */
341 342 343
			if (radeon_check_and_fixup_offset(dev_priv,
							  file_priv, &cmd[i]))
			{
344 345 346
				DRM_ERROR
				    ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
				     k, i);
E
Eric Anholt 已提交
347
				return -EINVAL;
348 349 350 351 352 353 354 355 356
			}
			k++;
			i++;
		}
		/* do the counts match what we expect ? */
		if ((k != narrays) || (i != (count + 2))) {
			DRM_ERROR
			    ("Malformed 3D_LOAD_VBPNTR packet (k=%d i=%d narrays=%d count+1=%d).\n",
			      k, i, narrays, count + 1);
E
Eric Anholt 已提交
357
			return -EINVAL;
358 359 360 361 362 363
		}
		break;

	case RADEON_3D_RNDR_GEN_INDX_PRIM:
		if (dev_priv->microcode_version != UCODE_R100) {
			DRM_ERROR("Invalid 3d packet for r200-class chip\n");
E
Eric Anholt 已提交
364
			return -EINVAL;
365
		}
366
		if (radeon_check_and_fixup_offset(dev_priv, file_priv, &cmd[1])) {
367
				DRM_ERROR("Invalid rndr_gen_indx offset\n");
E
Eric Anholt 已提交
368
				return -EINVAL;
369 370 371 372 373 374
		}
		break;

	case RADEON_CP_INDX_BUFFER:
		if (dev_priv->microcode_version != UCODE_R200) {
			DRM_ERROR("Invalid 3d packet for r100-class chip\n");
E
Eric Anholt 已提交
375
			return -EINVAL;
376 377 378
		}
		if ((cmd[1] & 0x8000ffff) != 0x80000810) {
			DRM_ERROR("Invalid indx_buffer reg address %08X\n", cmd[1]);
E
Eric Anholt 已提交
379
			return -EINVAL;
380
		}
381
		if (radeon_check_and_fixup_offset(dev_priv, file_priv, &cmd[2])) {
382
			DRM_ERROR("Invalid indx_buffer offset is %08X\n", cmd[2]);
E
Eric Anholt 已提交
383
			return -EINVAL;
384 385 386 387 388 389 390
		}
		break;

	case RADEON_CNTL_HOSTDATA_BLT:
	case RADEON_CNTL_PAINT_MULTI:
	case RADEON_CNTL_BITBLT_MULTI:
		/* MSB of opcode: next DWORD GUI_CNTL */
D
Dave Airlie 已提交
391 392
		if (cmd[1] & (RADEON_GMC_SRC_PITCH_OFFSET_CNTL
			      | RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
L
Linus Torvalds 已提交
393
			offset = cmd[2] << 10;
D
Dave Airlie 已提交
394
			if (radeon_check_and_fixup_offset
395
			    (dev_priv, file_priv, &offset)) {
D
Dave Airlie 已提交
396
				DRM_ERROR("Invalid first packet offset\n");
E
Eric Anholt 已提交
397
				return -EINVAL;
L
Linus Torvalds 已提交
398
			}
D
Dave Airlie 已提交
399
			cmd[2] = (cmd[2] & 0xffc00000) | offset >> 10;
L
Linus Torvalds 已提交
400 401
		}

D
Dave Airlie 已提交
402 403
		if ((cmd[1] & RADEON_GMC_SRC_PITCH_OFFSET_CNTL) &&
		    (cmd[1] & RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
L
Linus Torvalds 已提交
404
			offset = cmd[3] << 10;
D
Dave Airlie 已提交
405
			if (radeon_check_and_fixup_offset
406
			    (dev_priv, file_priv, &offset)) {
D
Dave Airlie 已提交
407
				DRM_ERROR("Invalid second packet offset\n");
E
Eric Anholt 已提交
408
				return -EINVAL;
L
Linus Torvalds 已提交
409
			}
D
Dave Airlie 已提交
410
			cmd[3] = (cmd[3] & 0xffc00000) | offset >> 10;
L
Linus Torvalds 已提交
411
		}
412 413 414 415
		break;

	default:
		DRM_ERROR("Invalid packet type %x\n", cmd[0] & 0xff00);
E
Eric Anholt 已提交
416
		return -EINVAL;
L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425
	}

	return 0;
}

/* ================================================================
 * CP hardware state programming functions
 */

D
Dave Airlie 已提交
426
static __inline__ void radeon_emit_clip_rect(drm_radeon_private_t * dev_priv,
427
					     struct drm_clip_rect * box)
L
Linus Torvalds 已提交
428 429 430
{
	RING_LOCALS;

D
Dave Airlie 已提交
431 432
	DRM_DEBUG("   box:  x1=%d y1=%d  x2=%d y2=%d\n",
		  box->x1, box->y1, box->x2, box->y2);
L
Linus Torvalds 已提交
433

D
Dave Airlie 已提交
434 435 436 437 438
	BEGIN_RING(4);
	OUT_RING(CP_PACKET0(RADEON_RE_TOP_LEFT, 0));
	OUT_RING((box->y1 << 16) | box->x1);
	OUT_RING(CP_PACKET0(RADEON_RE_WIDTH_HEIGHT, 0));
	OUT_RING(((box->y2 - 1) << 16) | (box->x2 - 1));
L
Linus Torvalds 已提交
439 440 441 442 443
	ADVANCE_RING();
}

/* Emit 1.1 state
 */
D
Dave Airlie 已提交
444
static int radeon_emit_state(drm_radeon_private_t * dev_priv,
445
			     struct drm_file *file_priv,
D
Dave Airlie 已提交
446 447 448
			     drm_radeon_context_regs_t * ctx,
			     drm_radeon_texture_regs_t * tex,
			     unsigned int dirty)
L
Linus Torvalds 已提交
449 450
{
	RING_LOCALS;
D
Dave Airlie 已提交
451
	DRM_DEBUG("dirty=0x%08x\n", dirty);
L
Linus Torvalds 已提交
452

D
Dave Airlie 已提交
453
	if (dirty & RADEON_UPLOAD_CONTEXT) {
454
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
D
Dave Airlie 已提交
455 456
						  &ctx->rb3d_depthoffset)) {
			DRM_ERROR("Invalid depth buffer offset\n");
E
Eric Anholt 已提交
457
			return -EINVAL;
L
Linus Torvalds 已提交
458 459
		}

460
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
D
Dave Airlie 已提交
461 462
						  &ctx->rb3d_coloroffset)) {
			DRM_ERROR("Invalid depth buffer offset\n");
E
Eric Anholt 已提交
463
			return -EINVAL;
L
Linus Torvalds 已提交
464 465
		}

D
Dave Airlie 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		BEGIN_RING(14);
		OUT_RING(CP_PACKET0(RADEON_PP_MISC, 6));
		OUT_RING(ctx->pp_misc);
		OUT_RING(ctx->pp_fog_color);
		OUT_RING(ctx->re_solid_color);
		OUT_RING(ctx->rb3d_blendcntl);
		OUT_RING(ctx->rb3d_depthoffset);
		OUT_RING(ctx->rb3d_depthpitch);
		OUT_RING(ctx->rb3d_zstencilcntl);
		OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 2));
		OUT_RING(ctx->pp_cntl);
		OUT_RING(ctx->rb3d_cntl);
		OUT_RING(ctx->rb3d_coloroffset);
		OUT_RING(CP_PACKET0(RADEON_RB3D_COLORPITCH, 0));
		OUT_RING(ctx->rb3d_colorpitch);
L
Linus Torvalds 已提交
481 482 483
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
484 485 486 487
	if (dirty & RADEON_UPLOAD_VERTFMT) {
		BEGIN_RING(2);
		OUT_RING(CP_PACKET0(RADEON_SE_COORD_FMT, 0));
		OUT_RING(ctx->se_coord_fmt);
L
Linus Torvalds 已提交
488 489 490
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
491 492 493 494 495 496 497
	if (dirty & RADEON_UPLOAD_LINE) {
		BEGIN_RING(5);
		OUT_RING(CP_PACKET0(RADEON_RE_LINE_PATTERN, 1));
		OUT_RING(ctx->re_line_pattern);
		OUT_RING(ctx->re_line_state);
		OUT_RING(CP_PACKET0(RADEON_SE_LINE_WIDTH, 0));
		OUT_RING(ctx->se_line_width);
L
Linus Torvalds 已提交
498 499 500
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
501 502 503 504 505 506 507
	if (dirty & RADEON_UPLOAD_BUMPMAP) {
		BEGIN_RING(5);
		OUT_RING(CP_PACKET0(RADEON_PP_LUM_MATRIX, 0));
		OUT_RING(ctx->pp_lum_matrix);
		OUT_RING(CP_PACKET0(RADEON_PP_ROT_MATRIX_0, 1));
		OUT_RING(ctx->pp_rot_matrix_0);
		OUT_RING(ctx->pp_rot_matrix_1);
L
Linus Torvalds 已提交
508 509 510
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
511 512 513 514 515 516
	if (dirty & RADEON_UPLOAD_MASKS) {
		BEGIN_RING(4);
		OUT_RING(CP_PACKET0(RADEON_RB3D_STENCILREFMASK, 2));
		OUT_RING(ctx->rb3d_stencilrefmask);
		OUT_RING(ctx->rb3d_ropcntl);
		OUT_RING(ctx->rb3d_planemask);
L
Linus Torvalds 已提交
517 518 519
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
520 521 522 523 524 525 526 527 528
	if (dirty & RADEON_UPLOAD_VIEWPORT) {
		BEGIN_RING(7);
		OUT_RING(CP_PACKET0(RADEON_SE_VPORT_XSCALE, 5));
		OUT_RING(ctx->se_vport_xscale);
		OUT_RING(ctx->se_vport_xoffset);
		OUT_RING(ctx->se_vport_yscale);
		OUT_RING(ctx->se_vport_yoffset);
		OUT_RING(ctx->se_vport_zscale);
		OUT_RING(ctx->se_vport_zoffset);
L
Linus Torvalds 已提交
529 530 531
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
532 533 534 535 536 537
	if (dirty & RADEON_UPLOAD_SETUP) {
		BEGIN_RING(4);
		OUT_RING(CP_PACKET0(RADEON_SE_CNTL, 0));
		OUT_RING(ctx->se_cntl);
		OUT_RING(CP_PACKET0(RADEON_SE_CNTL_STATUS, 0));
		OUT_RING(ctx->se_cntl_status);
L
Linus Torvalds 已提交
538 539 540
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
541 542 543 544
	if (dirty & RADEON_UPLOAD_MISC) {
		BEGIN_RING(2);
		OUT_RING(CP_PACKET0(RADEON_RE_MISC, 0));
		OUT_RING(ctx->re_misc);
L
Linus Torvalds 已提交
545 546 547
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
548
	if (dirty & RADEON_UPLOAD_TEX0) {
549
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
D
Dave Airlie 已提交
550 551
						  &tex[0].pp_txoffset)) {
			DRM_ERROR("Invalid texture offset for unit 0\n");
E
Eric Anholt 已提交
552
			return -EINVAL;
L
Linus Torvalds 已提交
553 554
		}

D
Dave Airlie 已提交
555 556 557 558 559 560 561 562 563 564
		BEGIN_RING(9);
		OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_0, 5));
		OUT_RING(tex[0].pp_txfilter);
		OUT_RING(tex[0].pp_txformat);
		OUT_RING(tex[0].pp_txoffset);
		OUT_RING(tex[0].pp_txcblend);
		OUT_RING(tex[0].pp_txablend);
		OUT_RING(tex[0].pp_tfactor);
		OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_0, 0));
		OUT_RING(tex[0].pp_border_color);
L
Linus Torvalds 已提交
565 566 567
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
568
	if (dirty & RADEON_UPLOAD_TEX1) {
569
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
D
Dave Airlie 已提交
570 571
						  &tex[1].pp_txoffset)) {
			DRM_ERROR("Invalid texture offset for unit 1\n");
E
Eric Anholt 已提交
572
			return -EINVAL;
L
Linus Torvalds 已提交
573 574
		}

D
Dave Airlie 已提交
575 576 577 578 579 580 581 582 583 584
		BEGIN_RING(9);
		OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_1, 5));
		OUT_RING(tex[1].pp_txfilter);
		OUT_RING(tex[1].pp_txformat);
		OUT_RING(tex[1].pp_txoffset);
		OUT_RING(tex[1].pp_txcblend);
		OUT_RING(tex[1].pp_txablend);
		OUT_RING(tex[1].pp_tfactor);
		OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_1, 0));
		OUT_RING(tex[1].pp_border_color);
L
Linus Torvalds 已提交
585 586 587
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
588
	if (dirty & RADEON_UPLOAD_TEX2) {
589
		if (radeon_check_and_fixup_offset(dev_priv, file_priv,
D
Dave Airlie 已提交
590 591
						  &tex[2].pp_txoffset)) {
			DRM_ERROR("Invalid texture offset for unit 2\n");
E
Eric Anholt 已提交
592
			return -EINVAL;
L
Linus Torvalds 已提交
593 594
		}

D
Dave Airlie 已提交
595 596 597 598 599 600 601 602 603 604
		BEGIN_RING(9);
		OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_2, 5));
		OUT_RING(tex[2].pp_txfilter);
		OUT_RING(tex[2].pp_txformat);
		OUT_RING(tex[2].pp_txoffset);
		OUT_RING(tex[2].pp_txcblend);
		OUT_RING(tex[2].pp_txablend);
		OUT_RING(tex[2].pp_tfactor);
		OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_2, 0));
		OUT_RING(tex[2].pp_border_color);
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612
		ADVANCE_RING();
	}

	return 0;
}

/* Emit 1.2 state
 */
D
Dave Airlie 已提交
613
static int radeon_emit_state2(drm_radeon_private_t * dev_priv,
614
			      struct drm_file *file_priv,
D
Dave Airlie 已提交
615
			      drm_radeon_state_t * state)
L
Linus Torvalds 已提交
616 617 618 619
{
	RING_LOCALS;

	if (state->dirty & RADEON_UPLOAD_ZBIAS) {
D
Dave Airlie 已提交
620 621 622 623
		BEGIN_RING(3);
		OUT_RING(CP_PACKET0(RADEON_SE_ZBIAS_FACTOR, 1));
		OUT_RING(state->context2.se_zbias_factor);
		OUT_RING(state->context2.se_zbias_constant);
L
Linus Torvalds 已提交
624 625 626
		ADVANCE_RING();
	}

627
	return radeon_emit_state(dev_priv, file_priv, &state->context,
D
Dave Airlie 已提交
628
				 state->tex, state->dirty);
L
Linus Torvalds 已提交
629 630 631 632
}

/* New (1.3) state mechanism.  3 commands (packet, scalar, vector) in
 * 1.3 cmdbuffers allow all previous state to be updated as well as
D
Dave Airlie 已提交
633
 * the tcl scalar and vector areas.
L
Linus Torvalds 已提交
634
 */
D
Dave Airlie 已提交
635 636 637
static struct {
	int start;
	int len;
L
Linus Torvalds 已提交
638 639
	const char *name;
} packet[RADEON_MAX_STATE_PACKETS] = {
D
Dave Airlie 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
	{RADEON_PP_MISC, 7, "RADEON_PP_MISC"},
	{RADEON_PP_CNTL, 3, "RADEON_PP_CNTL"},
	{RADEON_RB3D_COLORPITCH, 1, "RADEON_RB3D_COLORPITCH"},
	{RADEON_RE_LINE_PATTERN, 2, "RADEON_RE_LINE_PATTERN"},
	{RADEON_SE_LINE_WIDTH, 1, "RADEON_SE_LINE_WIDTH"},
	{RADEON_PP_LUM_MATRIX, 1, "RADEON_PP_LUM_MATRIX"},
	{RADEON_PP_ROT_MATRIX_0, 2, "RADEON_PP_ROT_MATRIX_0"},
	{RADEON_RB3D_STENCILREFMASK, 3, "RADEON_RB3D_STENCILREFMASK"},
	{RADEON_SE_VPORT_XSCALE, 6, "RADEON_SE_VPORT_XSCALE"},
	{RADEON_SE_CNTL, 2, "RADEON_SE_CNTL"},
	{RADEON_SE_CNTL_STATUS, 1, "RADEON_SE_CNTL_STATUS"},
	{RADEON_RE_MISC, 1, "RADEON_RE_MISC"},
	{RADEON_PP_TXFILTER_0, 6, "RADEON_PP_TXFILTER_0"},
	{RADEON_PP_BORDER_COLOR_0, 1, "RADEON_PP_BORDER_COLOR_0"},
	{RADEON_PP_TXFILTER_1, 6, "RADEON_PP_TXFILTER_1"},
	{RADEON_PP_BORDER_COLOR_1, 1, "RADEON_PP_BORDER_COLOR_1"},
	{RADEON_PP_TXFILTER_2, 6, "RADEON_PP_TXFILTER_2"},
	{RADEON_PP_BORDER_COLOR_2, 1, "RADEON_PP_BORDER_COLOR_2"},
	{RADEON_SE_ZBIAS_FACTOR, 2, "RADEON_SE_ZBIAS_FACTOR"},
	{RADEON_SE_TCL_OUTPUT_VTX_FMT, 11, "RADEON_SE_TCL_OUTPUT_VTX_FMT"},
	{RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED, 17,
		    "RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED"},
	{R200_PP_TXCBLEND_0, 4, "R200_PP_TXCBLEND_0"},
	{R200_PP_TXCBLEND_1, 4, "R200_PP_TXCBLEND_1"},
	{R200_PP_TXCBLEND_2, 4, "R200_PP_TXCBLEND_2"},
	{R200_PP_TXCBLEND_3, 4, "R200_PP_TXCBLEND_3"},
	{R200_PP_TXCBLEND_4, 4, "R200_PP_TXCBLEND_4"},
	{R200_PP_TXCBLEND_5, 4, "R200_PP_TXCBLEND_5"},
	{R200_PP_TXCBLEND_6, 4, "R200_PP_TXCBLEND_6"},
	{R200_PP_TXCBLEND_7, 4, "R200_PP_TXCBLEND_7"},
	{R200_SE_TCL_LIGHT_MODEL_CTL_0, 6, "R200_SE_TCL_LIGHT_MODEL_CTL_0"},
	{R200_PP_TFACTOR_0, 6, "R200_PP_TFACTOR_0"},
	{R200_SE_VTX_FMT_0, 4, "R200_SE_VTX_FMT_0"},
	{R200_SE_VAP_CNTL, 1, "R200_SE_VAP_CNTL"},
	{R200_SE_TCL_MATRIX_SEL_0, 5, "R200_SE_TCL_MATRIX_SEL_0"},
	{R200_SE_TCL_TEX_PROC_CTL_2, 5, "R200_SE_TCL_TEX_PROC_CTL_2"},
	{R200_SE_TCL_UCP_VERT_BLEND_CTL, 1, "R200_SE_TCL_UCP_VERT_BLEND_CTL"},
	{R200_PP_TXFILTER_0, 6, "R200_PP_TXFILTER_0"},
	{R200_PP_TXFILTER_1, 6, "R200_PP_TXFILTER_1"},
	{R200_PP_TXFILTER_2, 6, "R200_PP_TXFILTER_2"},
	{R200_PP_TXFILTER_3, 6, "R200_PP_TXFILTER_3"},
	{R200_PP_TXFILTER_4, 6, "R200_PP_TXFILTER_4"},
	{R200_PP_TXFILTER_5, 6, "R200_PP_TXFILTER_5"},
	{R200_PP_TXOFFSET_0, 1, "R200_PP_TXOFFSET_0"},
	{R200_PP_TXOFFSET_1, 1, "R200_PP_TXOFFSET_1"},
	{R200_PP_TXOFFSET_2, 1, "R200_PP_TXOFFSET_2"},
	{R200_PP_TXOFFSET_3, 1, "R200_PP_TXOFFSET_3"},
	{R200_PP_TXOFFSET_4, 1, "R200_PP_TXOFFSET_4"},
	{R200_PP_TXOFFSET_5, 1, "R200_PP_TXOFFSET_5"},
	{R200_SE_VTE_CNTL, 1, "R200_SE_VTE_CNTL"},
690 691
	{R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1,
	 "R200_SE_TCL_OUTPUT_VTX_COMP_SEL"},
D
Dave Airlie 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704
	{R200_PP_TAM_DEBUG3, 1, "R200_PP_TAM_DEBUG3"},
	{R200_PP_CNTL_X, 1, "R200_PP_CNTL_X"},
	{R200_RB3D_DEPTHXY_OFFSET, 1, "R200_RB3D_DEPTHXY_OFFSET"},
	{R200_RE_AUX_SCISSOR_CNTL, 1, "R200_RE_AUX_SCISSOR_CNTL"},
	{R200_RE_SCISSOR_TL_0, 2, "R200_RE_SCISSOR_TL_0"},
	{R200_RE_SCISSOR_TL_1, 2, "R200_RE_SCISSOR_TL_1"},
	{R200_RE_SCISSOR_TL_2, 2, "R200_RE_SCISSOR_TL_2"},
	{R200_SE_VAP_CNTL_STATUS, 1, "R200_SE_VAP_CNTL_STATUS"},
	{R200_SE_VTX_STATE_CNTL, 1, "R200_SE_VTX_STATE_CNTL"},
	{R200_RE_POINTSIZE, 1, "R200_RE_POINTSIZE"},
	{R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0, 4,
		    "R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0"},
	{R200_PP_CUBIC_FACES_0, 1, "R200_PP_CUBIC_FACES_0"},	/* 61 */
705
	{R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0"}, /* 62 */
D
Dave Airlie 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	{R200_PP_CUBIC_FACES_1, 1, "R200_PP_CUBIC_FACES_1"},
	{R200_PP_CUBIC_OFFSET_F1_1, 5, "R200_PP_CUBIC_OFFSET_F1_1"},
	{R200_PP_CUBIC_FACES_2, 1, "R200_PP_CUBIC_FACES_2"},
	{R200_PP_CUBIC_OFFSET_F1_2, 5, "R200_PP_CUBIC_OFFSET_F1_2"},
	{R200_PP_CUBIC_FACES_3, 1, "R200_PP_CUBIC_FACES_3"},
	{R200_PP_CUBIC_OFFSET_F1_3, 5, "R200_PP_CUBIC_OFFSET_F1_3"},
	{R200_PP_CUBIC_FACES_4, 1, "R200_PP_CUBIC_FACES_4"},
	{R200_PP_CUBIC_OFFSET_F1_4, 5, "R200_PP_CUBIC_OFFSET_F1_4"},
	{R200_PP_CUBIC_FACES_5, 1, "R200_PP_CUBIC_FACES_5"},
	{R200_PP_CUBIC_OFFSET_F1_5, 5, "R200_PP_CUBIC_OFFSET_F1_5"},
	{RADEON_PP_TEX_SIZE_0, 2, "RADEON_PP_TEX_SIZE_0"},
	{RADEON_PP_TEX_SIZE_1, 2, "RADEON_PP_TEX_SIZE_1"},
	{RADEON_PP_TEX_SIZE_2, 2, "RADEON_PP_TEX_SIZE_2"},
	{R200_RB3D_BLENDCOLOR, 3, "R200_RB3D_BLENDCOLOR"},
	{R200_SE_TCL_POINT_SPRITE_CNTL, 1, "R200_SE_TCL_POINT_SPRITE_CNTL"},
	{RADEON_PP_CUBIC_FACES_0, 1, "RADEON_PP_CUBIC_FACES_0"},
	{RADEON_PP_CUBIC_OFFSET_T0_0, 5, "RADEON_PP_CUBIC_OFFSET_T0_0"},
	{RADEON_PP_CUBIC_FACES_1, 1, "RADEON_PP_CUBIC_FACES_1"},
	{RADEON_PP_CUBIC_OFFSET_T1_0, 5, "RADEON_PP_CUBIC_OFFSET_T1_0"},
	{RADEON_PP_CUBIC_FACES_2, 1, "RADEON_PP_CUBIC_FACES_2"},
	{RADEON_PP_CUBIC_OFFSET_T2_0, 5, "RADEON_PP_CUBIC_OFFSET_T2_0"},
	{R200_PP_TRI_PERF, 2, "R200_PP_TRI_PERF"},
728
	{R200_PP_AFS_0, 32, "R200_PP_AFS_0"},     /* 85 */
D
Dave Airlie 已提交
729 730 731 732 733 734 735 736
	{R200_PP_AFS_1, 32, "R200_PP_AFS_1"},
	{R200_PP_TFACTOR_0, 8, "R200_ATF_TFACTOR"},
	{R200_PP_TXFILTER_0, 8, "R200_PP_TXCTLALL_0"},
	{R200_PP_TXFILTER_1, 8, "R200_PP_TXCTLALL_1"},
	{R200_PP_TXFILTER_2, 8, "R200_PP_TXCTLALL_2"},
	{R200_PP_TXFILTER_3, 8, "R200_PP_TXCTLALL_3"},
	{R200_PP_TXFILTER_4, 8, "R200_PP_TXCTLALL_4"},
	{R200_PP_TXFILTER_5, 8, "R200_PP_TXCTLALL_5"},
737
	{R200_VAP_PVS_CNTL_1, 2, "R200_VAP_PVS_CNTL"},
L
Linus Torvalds 已提交
738 739 740 741 742 743
};

/* ================================================================
 * Performance monitoring functions
 */

D
Dave Airlie 已提交
744
static void radeon_clear_box(drm_radeon_private_t * dev_priv,
745
			     struct drm_radeon_master_private *master_priv,
D
Dave Airlie 已提交
746
			     int x, int y, int w, int h, int r, int g, int b)
L
Linus Torvalds 已提交
747 748 749 750
{
	u32 color;
	RING_LOCALS;

751 752
	x += master_priv->sarea_priv->boxes[0].x1;
	y += master_priv->sarea_priv->boxes[0].y1;
L
Linus Torvalds 已提交
753

D
Dave Airlie 已提交
754
	switch (dev_priv->color_fmt) {
L
Linus Torvalds 已提交
755 756
	case RADEON_COLOR_FORMAT_RGB565:
		color = (((r & 0xf8) << 8) |
D
Dave Airlie 已提交
757
			 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
L
Linus Torvalds 已提交
758 759 760
		break;
	case RADEON_COLOR_FORMAT_ARGB8888:
	default:
D
Dave Airlie 已提交
761
		color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
L
Linus Torvalds 已提交
762 763 764
		break;
	}

D
Dave Airlie 已提交
765 766 767 768
	BEGIN_RING(4);
	RADEON_WAIT_UNTIL_3D_IDLE();
	OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
	OUT_RING(0xffffffff);
L
Linus Torvalds 已提交
769 770
	ADVANCE_RING();

D
Dave Airlie 已提交
771
	BEGIN_RING(6);
L
Linus Torvalds 已提交
772

D
Dave Airlie 已提交
773 774 775 776 777 778
	OUT_RING(CP_PACKET3(RADEON_CNTL_PAINT_MULTI, 4));
	OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
		 RADEON_GMC_BRUSH_SOLID_COLOR |
		 (dev_priv->color_fmt << 8) |
		 RADEON_GMC_SRC_DATATYPE_COLOR |
		 RADEON_ROP3_P | RADEON_GMC_CLR_CMP_CNTL_DIS);
L
Linus Torvalds 已提交
779

780
	if (master_priv->sarea_priv->pfCurrentPage == 1) {
D
Dave Airlie 已提交
781 782 783 784
		OUT_RING(dev_priv->front_pitch_offset);
	} else {
		OUT_RING(dev_priv->back_pitch_offset);
	}
L
Linus Torvalds 已提交
785

D
Dave Airlie 已提交
786
	OUT_RING(color);
L
Linus Torvalds 已提交
787

D
Dave Airlie 已提交
788 789
	OUT_RING((x << 16) | y);
	OUT_RING((w << 16) | h);
L
Linus Torvalds 已提交
790 791 792 793

	ADVANCE_RING();
}

794
static void radeon_cp_performance_boxes(drm_radeon_private_t *dev_priv, struct drm_radeon_master_private *master_priv)
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
{
	/* Collapse various things into a wait flag -- trying to
	 * guess if userspase slept -- better just to have them tell us.
	 */
	if (dev_priv->stats.last_frame_reads > 1 ||
	    dev_priv->stats.last_clear_reads > dev_priv->stats.clears) {
		dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
	}

	if (dev_priv->stats.freelist_loops) {
		dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
	}

	/* Purple box for page flipping
	 */
D
Dave Airlie 已提交
810
	if (dev_priv->stats.boxes & RADEON_BOX_FLIP)
811
		radeon_clear_box(dev_priv, master_priv, 4, 4, 8, 8, 255, 0, 255);
L
Linus Torvalds 已提交
812 813 814

	/* Red box if we have to wait for idle at any point
	 */
D
Dave Airlie 已提交
815
	if (dev_priv->stats.boxes & RADEON_BOX_WAIT_IDLE)
816
		radeon_clear_box(dev_priv, master_priv, 16, 4, 8, 8, 255, 0, 0);
L
Linus Torvalds 已提交
817 818 819 820 821 822

	/* Blue box: lost context?
	 */

	/* Yellow box for texture swaps
	 */
D
Dave Airlie 已提交
823
	if (dev_priv->stats.boxes & RADEON_BOX_TEXTURE_LOAD)
824
		radeon_clear_box(dev_priv, master_priv, 40, 4, 8, 8, 255, 255, 0);
L
Linus Torvalds 已提交
825 826 827

	/* Green box if hardware never idles (as far as we can tell)
	 */
D
Dave Airlie 已提交
828
	if (!(dev_priv->stats.boxes & RADEON_BOX_DMA_IDLE))
829
		radeon_clear_box(dev_priv, master_priv, 64, 4, 8, 8, 0, 255, 0);
L
Linus Torvalds 已提交
830

D
Dave Airlie 已提交
831
	/* Draw bars indicating number of buffers allocated
L
Linus Torvalds 已提交
832 833 834 835 836 837
	 * (not a great measure, easily confused)
	 */
	if (dev_priv->stats.requested_bufs) {
		if (dev_priv->stats.requested_bufs > 100)
			dev_priv->stats.requested_bufs = 100;

838
		radeon_clear_box(dev_priv, master_priv, 4, 16,
D
Dave Airlie 已提交
839 840
				 dev_priv->stats.requested_bufs, 4,
				 196, 128, 128);
L
Linus Torvalds 已提交
841 842
	}

D
Dave Airlie 已提交
843
	memset(&dev_priv->stats, 0, sizeof(dev_priv->stats));
L
Linus Torvalds 已提交
844 845

}
D
Dave Airlie 已提交
846

L
Linus Torvalds 已提交
847 848 849 850
/* ================================================================
 * CP command dispatch functions
 */

851
static void radeon_cp_dispatch_clear(struct drm_device * dev,
852
				     struct drm_master *master,
D
Dave Airlie 已提交
853 854
				     drm_radeon_clear_t * clear,
				     drm_radeon_clear_rect_t * depth_boxes)
L
Linus Torvalds 已提交
855 856
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
857 858
	struct drm_radeon_master_private *master_priv = master->driver_priv;
	drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
L
Linus Torvalds 已提交
859 860
	drm_radeon_depth_clear_t *depth_clear = &dev_priv->depth_clear;
	int nbox = sarea_priv->nbox;
861
	struct drm_clip_rect *pbox = sarea_priv->boxes;
L
Linus Torvalds 已提交
862
	unsigned int flags = clear->flags;
D
Dave Airlie 已提交
863
	u32 rb3d_cntl = 0, rb3d_stencilrefmask = 0;
L
Linus Torvalds 已提交
864 865
	int i;
	RING_LOCALS;
D
Dave Airlie 已提交
866
	DRM_DEBUG("flags = 0x%x\n", flags);
L
Linus Torvalds 已提交
867 868 869

	dev_priv->stats.clears++;

870
	if (sarea_priv->pfCurrentPage == 1) {
L
Linus Torvalds 已提交
871 872 873
		unsigned int tmp = flags;

		flags &= ~(RADEON_FRONT | RADEON_BACK);
D
Dave Airlie 已提交
874 875 876 877
		if (tmp & RADEON_FRONT)
			flags |= RADEON_BACK;
		if (tmp & RADEON_BACK)
			flags |= RADEON_FRONT;
L
Linus Torvalds 已提交
878 879
	}

D
Dave Airlie 已提交
880
	if (flags & (RADEON_FRONT | RADEON_BACK)) {
L
Linus Torvalds 已提交
881

D
Dave Airlie 已提交
882
		BEGIN_RING(4);
L
Linus Torvalds 已提交
883 884 885 886 887

		/* Ensure the 3D stream is idle before doing a
		 * 2D fill to clear the front or back buffer.
		 */
		RADEON_WAIT_UNTIL_3D_IDLE();
D
Dave Airlie 已提交
888 889 890

		OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
		OUT_RING(clear->color_mask);
L
Linus Torvalds 已提交
891 892 893 894 895

		ADVANCE_RING();

		/* Make sure we restore the 3D state next time.
		 */
896
		sarea_priv->ctx_owner = 0;
L
Linus Torvalds 已提交
897

D
Dave Airlie 已提交
898
		for (i = 0; i < nbox; i++) {
L
Linus Torvalds 已提交
899 900 901 902 903
			int x = pbox[i].x1;
			int y = pbox[i].y1;
			int w = pbox[i].x2 - x;
			int h = pbox[i].y2 - y;

904
			DRM_DEBUG("%d,%d-%d,%d flags 0x%x\n",
D
Dave Airlie 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
				  x, y, w, h, flags);

			if (flags & RADEON_FRONT) {
				BEGIN_RING(6);

				OUT_RING(CP_PACKET3
					 (RADEON_CNTL_PAINT_MULTI, 4));
				OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
					 RADEON_GMC_BRUSH_SOLID_COLOR |
					 (dev_priv->
					  color_fmt << 8) |
					 RADEON_GMC_SRC_DATATYPE_COLOR |
					 RADEON_ROP3_P |
					 RADEON_GMC_CLR_CMP_CNTL_DIS);

				OUT_RING(dev_priv->front_pitch_offset);
				OUT_RING(clear->clear_color);

				OUT_RING((x << 16) | y);
				OUT_RING((w << 16) | h);

L
Linus Torvalds 已提交
926 927
				ADVANCE_RING();
			}
D
Dave Airlie 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

			if (flags & RADEON_BACK) {
				BEGIN_RING(6);

				OUT_RING(CP_PACKET3
					 (RADEON_CNTL_PAINT_MULTI, 4));
				OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
					 RADEON_GMC_BRUSH_SOLID_COLOR |
					 (dev_priv->
					  color_fmt << 8) |
					 RADEON_GMC_SRC_DATATYPE_COLOR |
					 RADEON_ROP3_P |
					 RADEON_GMC_CLR_CMP_CNTL_DIS);

				OUT_RING(dev_priv->back_pitch_offset);
				OUT_RING(clear->clear_color);

				OUT_RING((x << 16) | y);
				OUT_RING((w << 16) | h);
L
Linus Torvalds 已提交
947 948 949 950 951

				ADVANCE_RING();
			}
		}
	}
D
Dave Airlie 已提交
952

L
Linus Torvalds 已提交
953 954
	/* hyper z clear */
	/* no docs available, based on reverse engeneering by Stephane Marchesin */
D
Dave Airlie 已提交
955 956
	if ((flags & (RADEON_DEPTH | RADEON_STENCIL))
	    && (flags & RADEON_CLEAR_FASTZ)) {
L
Linus Torvalds 已提交
957 958

		int i;
D
Dave Airlie 已提交
959 960 961 962 963 964
		int depthpixperline =
		    dev_priv->depth_fmt ==
		    RADEON_DEPTH_FORMAT_16BIT_INT_Z ? (dev_priv->depth_pitch /
						       2) : (dev_priv->
							     depth_pitch / 4);

L
Linus Torvalds 已提交
965 966 967
		u32 clearmask;

		u32 tempRB3D_DEPTHCLEARVALUE = clear->clear_depth |
D
Dave Airlie 已提交
968 969
		    ((clear->depth_mask & 0xff) << 24);

L
Linus Torvalds 已提交
970 971 972
		/* Make sure we restore the 3D state next time.
		 * we haven't touched any "normal" state - still need this?
		 */
973
		sarea_priv->ctx_owner = 0;
L
Linus Torvalds 已提交
974

975
		if ((dev_priv->flags & RADEON_HAS_HIERZ)
D
Dave Airlie 已提交
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
		    && (flags & RADEON_USE_HIERZ)) {
			/* FIXME : reverse engineer that for Rx00 cards */
			/* FIXME : the mask supposedly contains low-res z values. So can't set
			   just to the max (0xff? or actually 0x3fff?), need to take z clear
			   value into account? */
			/* pattern seems to work for r100, though get slight
			   rendering errors with glxgears. If hierz is not enabled for r100,
			   only 4 bits which indicate clear (15,16,31,32, all zero) matter, the
			   other ones are ignored, and the same clear mask can be used. That's
			   very different behaviour than R200 which needs different clear mask
			   and different number of tiles to clear if hierz is enabled or not !?!
			 */
			clearmask = (0xff << 22) | (0xff << 6) | 0x003f003f;
		} else {
			/* clear mask : chooses the clearing pattern.
			   rv250: could be used to clear only parts of macrotiles
			   (but that would get really complicated...)?
			   bit 0 and 1 (either or both of them ?!?!) are used to
			   not clear tile (or maybe one of the bits indicates if the tile is
			   compressed or not), bit 2 and 3 to not clear tile 1,...,.
			   Pattern is as follows:
			   | 0,1 | 4,5 | 8,9 |12,13|16,17|20,21|24,25|28,29|
			   bits -------------------------------------------------
			   | 2,3 | 6,7 |10,11|14,15|18,19|22,23|26,27|30,31|
			   rv100: clearmask covers 2x8 4x1 tiles, but one clear still
			   covers 256 pixels ?!?
			 */
L
Linus Torvalds 已提交
1003 1004 1005
			clearmask = 0x0;
		}

D
Dave Airlie 已提交
1006
		BEGIN_RING(8);
L
Linus Torvalds 已提交
1007
		RADEON_WAIT_UNTIL_2D_IDLE();
D
Dave Airlie 已提交
1008 1009
		OUT_RING_REG(RADEON_RB3D_DEPTHCLEARVALUE,
			     tempRB3D_DEPTHCLEARVALUE);
L
Linus Torvalds 已提交
1010
		/* what offset is this exactly ? */
D
Dave Airlie 已提交
1011
		OUT_RING_REG(RADEON_RB3D_ZMASKOFFSET, 0);
L
Linus Torvalds 已提交
1012
		/* need ctlstat, otherwise get some strange black flickering */
D
Dave Airlie 已提交
1013 1014
		OUT_RING_REG(RADEON_RB3D_ZCACHE_CTLSTAT,
			     RADEON_RB3D_ZC_FLUSH_ALL);
L
Linus Torvalds 已提交
1015 1016 1017 1018 1019
		ADVANCE_RING();

		for (i = 0; i < nbox; i++) {
			int tileoffset, nrtilesx, nrtilesy, j;
			/* it looks like r200 needs rv-style clears, at least if hierz is not enabled? */
1020
			if ((dev_priv->flags & RADEON_HAS_HIERZ)
D
Dave Airlie 已提交
1021
			    && !(dev_priv->microcode_version == UCODE_R200)) {
L
Linus Torvalds 已提交
1022 1023 1024 1025 1026 1027 1028
				/* FIXME : figure this out for r200 (when hierz is enabled). Or
				   maybe r200 actually doesn't need to put the low-res z value into
				   the tile cache like r100, but just needs to clear the hi-level z-buffer?
				   Works for R100, both with hierz and without.
				   R100 seems to operate on 2x1 8x8 tiles, but...
				   odd: offset/nrtiles need to be 64 pix (4 block) aligned? Potentially
				   problematic with resolutions which are not 64 pix aligned? */
D
Dave Airlie 已提交
1029 1030 1031 1032 1033 1034 1035 1036
				tileoffset =
				    ((pbox[i].y1 >> 3) * depthpixperline +
				     pbox[i].x1) >> 6;
				nrtilesx =
				    ((pbox[i].x2 & ~63) -
				     (pbox[i].x1 & ~63)) >> 4;
				nrtilesy =
				    (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
L
Linus Torvalds 已提交
1037
				for (j = 0; j <= nrtilesy; j++) {
D
Dave Airlie 已提交
1038 1039 1040
					BEGIN_RING(4);
					OUT_RING(CP_PACKET3
						 (RADEON_3D_CLEAR_ZMASK, 2));
L
Linus Torvalds 已提交
1041
					/* first tile */
D
Dave Airlie 已提交
1042
					OUT_RING(tileoffset * 8);
L
Linus Torvalds 已提交
1043
					/* the number of tiles to clear */
D
Dave Airlie 已提交
1044
					OUT_RING(nrtilesx + 4);
L
Linus Torvalds 已提交
1045
					/* clear mask : chooses the clearing pattern. */
D
Dave Airlie 已提交
1046
					OUT_RING(clearmask);
L
Linus Torvalds 已提交
1047 1048 1049
					ADVANCE_RING();
					tileoffset += depthpixperline >> 6;
				}
D
Dave Airlie 已提交
1050
			} else if (dev_priv->microcode_version == UCODE_R200) {
L
Linus Torvalds 已提交
1051 1052
				/* works for rv250. */
				/* find first macro tile (8x2 4x4 z-pixels on rv250) */
D
Dave Airlie 已提交
1053 1054 1055 1056 1057 1058 1059
				tileoffset =
				    ((pbox[i].y1 >> 3) * depthpixperline +
				     pbox[i].x1) >> 5;
				nrtilesx =
				    (pbox[i].x2 >> 5) - (pbox[i].x1 >> 5);
				nrtilesy =
				    (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
L
Linus Torvalds 已提交
1060
				for (j = 0; j <= nrtilesy; j++) {
D
Dave Airlie 已提交
1061 1062 1063
					BEGIN_RING(4);
					OUT_RING(CP_PACKET3
						 (RADEON_3D_CLEAR_ZMASK, 2));
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068
					/* first tile */
					/* judging by the first tile offset needed, could possibly
					   directly address/clear 4x4 tiles instead of 8x2 * 4x4
					   macro tiles, though would still need clear mask for
					   right/bottom if truely 4x4 granularity is desired ? */
D
Dave Airlie 已提交
1069
					OUT_RING(tileoffset * 16);
L
Linus Torvalds 已提交
1070
					/* the number of tiles to clear */
D
Dave Airlie 已提交
1071
					OUT_RING(nrtilesx + 1);
L
Linus Torvalds 已提交
1072
					/* clear mask : chooses the clearing pattern. */
D
Dave Airlie 已提交
1073
					OUT_RING(clearmask);
L
Linus Torvalds 已提交
1074 1075 1076
					ADVANCE_RING();
					tileoffset += depthpixperline >> 5;
				}
D
Dave Airlie 已提交
1077
			} else {	/* rv 100 */
L
Linus Torvalds 已提交
1078 1079
				/* rv100 might not need 64 pix alignment, who knows */
				/* offsets are, hmm, weird */
D
Dave Airlie 已提交
1080 1081 1082 1083 1084 1085 1086 1087
				tileoffset =
				    ((pbox[i].y1 >> 4) * depthpixperline +
				     pbox[i].x1) >> 6;
				nrtilesx =
				    ((pbox[i].x2 & ~63) -
				     (pbox[i].x1 & ~63)) >> 4;
				nrtilesy =
				    (pbox[i].y2 >> 4) - (pbox[i].y1 >> 4);
L
Linus Torvalds 已提交
1088
				for (j = 0; j <= nrtilesy; j++) {
D
Dave Airlie 已提交
1089 1090 1091 1092
					BEGIN_RING(4);
					OUT_RING(CP_PACKET3
						 (RADEON_3D_CLEAR_ZMASK, 2));
					OUT_RING(tileoffset * 128);
L
Linus Torvalds 已提交
1093
					/* the number of tiles to clear */
D
Dave Airlie 已提交
1094
					OUT_RING(nrtilesx + 4);
L
Linus Torvalds 已提交
1095
					/* clear mask : chooses the clearing pattern. */
D
Dave Airlie 已提交
1096
					OUT_RING(clearmask);
L
Linus Torvalds 已提交
1097 1098 1099 1100 1101 1102 1103
					ADVANCE_RING();
					tileoffset += depthpixperline >> 6;
				}
			}
		}

		/* TODO don't always clear all hi-level z tiles */
1104
		if ((dev_priv->flags & RADEON_HAS_HIERZ)
D
Dave Airlie 已提交
1105 1106 1107 1108 1109 1110
		    && (dev_priv->microcode_version == UCODE_R200)
		    && (flags & RADEON_USE_HIERZ))
			/* r100 and cards without hierarchical z-buffer have no high-level z-buffer */
			/* FIXME : the mask supposedly contains low-res z values. So can't set
			   just to the max (0xff? or actually 0x3fff?), need to take z clear
			   value into account? */
L
Linus Torvalds 已提交
1111
		{
D
Dave Airlie 已提交
1112 1113 1114 1115 1116
			BEGIN_RING(4);
			OUT_RING(CP_PACKET3(RADEON_3D_CLEAR_HIZ, 2));
			OUT_RING(0x0);	/* First tile */
			OUT_RING(0x3cc0);
			OUT_RING((0xff << 22) | (0xff << 6) | 0x003f003f);
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121 1122 1123 1124
			ADVANCE_RING();
		}
	}

	/* We have to clear the depth and/or stencil buffers by
	 * rendering a quad into just those buffers.  Thus, we have to
	 * make sure the 3D engine is configured correctly.
	 */
1125 1126
	else if ((dev_priv->microcode_version == UCODE_R200) &&
		(flags & (RADEON_DEPTH | RADEON_STENCIL))) {
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

		int tempPP_CNTL;
		int tempRE_CNTL;
		int tempRB3D_CNTL;
		int tempRB3D_ZSTENCILCNTL;
		int tempRB3D_STENCILREFMASK;
		int tempRB3D_PLANEMASK;
		int tempSE_CNTL;
		int tempSE_VTE_CNTL;
		int tempSE_VTX_FMT_0;
		int tempSE_VTX_FMT_1;
		int tempSE_VAP_CNTL;
		int tempRE_AUX_SCISSOR_CNTL;

		tempPP_CNTL = 0;
		tempRE_CNTL = 0;

		tempRB3D_CNTL = depth_clear->rb3d_cntl;

		tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
		tempRB3D_STENCILREFMASK = 0x0;

		tempSE_CNTL = depth_clear->se_cntl;

		/* Disable TCL */

D
Dave Airlie 已提交
1153 1154 1155
		tempSE_VAP_CNTL = (	/* SE_VAP_CNTL__FORCE_W_TO_ONE_MASK |  */
					  (0x9 <<
					   SE_VAP_CNTL__VF_MAX_VTX_NUM__SHIFT));
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161

		tempRB3D_PLANEMASK = 0x0;

		tempRE_AUX_SCISSOR_CNTL = 0x0;

		tempSE_VTE_CNTL =
D
Dave Airlie 已提交
1162
		    SE_VTE_CNTL__VTX_XY_FMT_MASK | SE_VTE_CNTL__VTX_Z_FMT_MASK;
L
Linus Torvalds 已提交
1163

D
Dave Airlie 已提交
1164
		/* Vertex format (X, Y, Z, W) */
L
Linus Torvalds 已提交
1165
		tempSE_VTX_FMT_0 =
D
Dave Airlie 已提交
1166 1167
		    SE_VTX_FMT_0__VTX_Z0_PRESENT_MASK |
		    SE_VTX_FMT_0__VTX_W0_PRESENT_MASK;
L
Linus Torvalds 已提交
1168 1169
		tempSE_VTX_FMT_1 = 0x0;

D
Dave Airlie 已提交
1170 1171
		/*
		 * Depth buffer specific enables
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180
		 */
		if (flags & RADEON_DEPTH) {
			/* Enable depth buffer */
			tempRB3D_CNTL |= RADEON_Z_ENABLE;
		} else {
			/* Disable depth buffer */
			tempRB3D_CNTL &= ~RADEON_Z_ENABLE;
		}

D
Dave Airlie 已提交
1181
		/*
L
Linus Torvalds 已提交
1182 1183
		 * Stencil buffer specific enables
		 */
D
Dave Airlie 已提交
1184 1185 1186
		if (flags & RADEON_STENCIL) {
			tempRB3D_CNTL |= RADEON_STENCIL_ENABLE;
			tempRB3D_STENCILREFMASK = clear->depth_mask;
L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192 1193
		} else {
			tempRB3D_CNTL &= ~RADEON_STENCIL_ENABLE;
			tempRB3D_STENCILREFMASK = 0x00000000;
		}

		if (flags & RADEON_USE_COMP_ZBUF) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
D
Dave Airlie 已提交
1194
			    RADEON_Z_DECOMPRESSION_ENABLE;
L
Linus Torvalds 已提交
1195 1196 1197 1198 1199
		}
		if (flags & RADEON_USE_HIERZ) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
		}

D
Dave Airlie 已提交
1200
		BEGIN_RING(26);
L
Linus Torvalds 已提交
1201 1202
		RADEON_WAIT_UNTIL_2D_IDLE();

D
Dave Airlie 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
		OUT_RING_REG(RADEON_PP_CNTL, tempPP_CNTL);
		OUT_RING_REG(R200_RE_CNTL, tempRE_CNTL);
		OUT_RING_REG(RADEON_RB3D_CNTL, tempRB3D_CNTL);
		OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
		OUT_RING_REG(RADEON_RB3D_STENCILREFMASK,
			     tempRB3D_STENCILREFMASK);
		OUT_RING_REG(RADEON_RB3D_PLANEMASK, tempRB3D_PLANEMASK);
		OUT_RING_REG(RADEON_SE_CNTL, tempSE_CNTL);
		OUT_RING_REG(R200_SE_VTE_CNTL, tempSE_VTE_CNTL);
		OUT_RING_REG(R200_SE_VTX_FMT_0, tempSE_VTX_FMT_0);
		OUT_RING_REG(R200_SE_VTX_FMT_1, tempSE_VTX_FMT_1);
		OUT_RING_REG(R200_SE_VAP_CNTL, tempSE_VAP_CNTL);
		OUT_RING_REG(R200_RE_AUX_SCISSOR_CNTL, tempRE_AUX_SCISSOR_CNTL);
L
Linus Torvalds 已提交
1216 1217 1218 1219
		ADVANCE_RING();

		/* Make sure we restore the 3D state next time.
		 */
1220
		sarea_priv->ctx_owner = 0;
L
Linus Torvalds 已提交
1221

D
Dave Airlie 已提交
1222 1223 1224
		for (i = 0; i < nbox; i++) {

			/* Funny that this should be required --
L
Linus Torvalds 已提交
1225 1226
			 *  sets top-left?
			 */
D
Dave Airlie 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
			radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);

			BEGIN_RING(14);
			OUT_RING(CP_PACKET3(R200_3D_DRAW_IMMD_2, 12));
			OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
				  RADEON_PRIM_WALK_RING |
				  (3 << RADEON_NUM_VERTICES_SHIFT)));
			OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
			OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
			OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
			OUT_RING(0x3f800000);
			OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
			OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
			OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
			OUT_RING(0x3f800000);
			OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
			OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
			OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
			OUT_RING(0x3f800000);
L
Linus Torvalds 已提交
1246 1247
			ADVANCE_RING();
		}
D
Dave Airlie 已提交
1248
	} else if ((flags & (RADEON_DEPTH | RADEON_STENCIL))) {
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253

		int tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;

		rb3d_cntl = depth_clear->rb3d_cntl;

D
Dave Airlie 已提交
1254 1255
		if (flags & RADEON_DEPTH) {
			rb3d_cntl |= RADEON_Z_ENABLE;
L
Linus Torvalds 已提交
1256 1257 1258 1259
		} else {
			rb3d_cntl &= ~RADEON_Z_ENABLE;
		}

D
Dave Airlie 已提交
1260 1261 1262
		if (flags & RADEON_STENCIL) {
			rb3d_cntl |= RADEON_STENCIL_ENABLE;
			rb3d_stencilrefmask = clear->depth_mask;	/* misnamed field */
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269
		} else {
			rb3d_cntl &= ~RADEON_STENCIL_ENABLE;
			rb3d_stencilrefmask = 0x00000000;
		}

		if (flags & RADEON_USE_COMP_ZBUF) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
D
Dave Airlie 已提交
1270
			    RADEON_Z_DECOMPRESSION_ENABLE;
L
Linus Torvalds 已提交
1271 1272 1273 1274 1275
		}
		if (flags & RADEON_USE_HIERZ) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
		}

D
Dave Airlie 已提交
1276
		BEGIN_RING(13);
L
Linus Torvalds 已提交
1277 1278
		RADEON_WAIT_UNTIL_2D_IDLE();

D
Dave Airlie 已提交
1279 1280 1281 1282 1283 1284 1285 1286
		OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 1));
		OUT_RING(0x00000000);
		OUT_RING(rb3d_cntl);

		OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
		OUT_RING_REG(RADEON_RB3D_STENCILREFMASK, rb3d_stencilrefmask);
		OUT_RING_REG(RADEON_RB3D_PLANEMASK, 0x00000000);
		OUT_RING_REG(RADEON_SE_CNTL, depth_clear->se_cntl);
L
Linus Torvalds 已提交
1287 1288 1289 1290
		ADVANCE_RING();

		/* Make sure we restore the 3D state next time.
		 */
1291
		sarea_priv->ctx_owner = 0;
L
Linus Torvalds 已提交
1292

D
Dave Airlie 已提交
1293 1294 1295
		for (i = 0; i < nbox; i++) {

			/* Funny that this should be required --
L
Linus Torvalds 已提交
1296 1297
			 *  sets top-left?
			 */
D
Dave Airlie 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
			radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);

			BEGIN_RING(15);

			OUT_RING(CP_PACKET3(RADEON_3D_DRAW_IMMD, 13));
			OUT_RING(RADEON_VTX_Z_PRESENT |
				 RADEON_VTX_PKCOLOR_PRESENT);
			OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
				  RADEON_PRIM_WALK_RING |
				  RADEON_MAOS_ENABLE |
				  RADEON_VTX_FMT_RADEON_MODE |
				  (3 << RADEON_NUM_VERTICES_SHIFT)));

			OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
			OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
			OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
			OUT_RING(0x0);

			OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
			OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
			OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
			OUT_RING(0x0);

			OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
			OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
			OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
			OUT_RING(0x0);
L
Linus Torvalds 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333

			ADVANCE_RING();
		}
	}

	/* Increment the clear counter.  The client-side 3D driver must
	 * wait on this value before performing the clear ioctl.  We
	 * need this because the card's so damned fast...
	 */
1334
	sarea_priv->last_clear++;
L
Linus Torvalds 已提交
1335

D
Dave Airlie 已提交
1336
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1337

1338
	RADEON_CLEAR_AGE(sarea_priv->last_clear);
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343
	RADEON_WAIT_UNTIL_IDLE();

	ADVANCE_RING();
}

1344
static void radeon_cp_dispatch_swap(struct drm_device *dev, struct drm_master *master)
L
Linus Torvalds 已提交
1345 1346
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
1347 1348
	struct drm_radeon_master_private *master_priv = master->driver_priv;
	drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
L
Linus Torvalds 已提交
1349
	int nbox = sarea_priv->nbox;
1350
	struct drm_clip_rect *pbox = sarea_priv->boxes;
L
Linus Torvalds 已提交
1351 1352
	int i;
	RING_LOCALS;
D
Dave Airlie 已提交
1353
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
1354 1355 1356 1357

	/* Do some trivial performance monitoring...
	 */
	if (dev_priv->do_boxes)
1358
		radeon_cp_performance_boxes(dev_priv, master_priv);
L
Linus Torvalds 已提交
1359 1360 1361 1362

	/* Wait for the 3D stream to idle before dispatching the bitblt.
	 * This will prevent data corruption between the two streams.
	 */
D
Dave Airlie 已提交
1363
	BEGIN_RING(2);
L
Linus Torvalds 已提交
1364 1365 1366 1367 1368

	RADEON_WAIT_UNTIL_3D_IDLE();

	ADVANCE_RING();

D
Dave Airlie 已提交
1369
	for (i = 0; i < nbox; i++) {
L
Linus Torvalds 已提交
1370 1371 1372 1373 1374
		int x = pbox[i].x1;
		int y = pbox[i].y1;
		int w = pbox[i].x2 - x;
		int h = pbox[i].y2 - y;

1375
		DRM_DEBUG("%d,%d-%d,%d\n", x, y, w, h);
D
Dave Airlie 已提交
1376

1377
		BEGIN_RING(9);
D
Dave Airlie 已提交
1378

1379
		OUT_RING(CP_PACKET0(RADEON_DP_GUI_MASTER_CNTL, 0));
D
Dave Airlie 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388
		OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
			 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
			 RADEON_GMC_BRUSH_NONE |
			 (dev_priv->color_fmt << 8) |
			 RADEON_GMC_SRC_DATATYPE_COLOR |
			 RADEON_ROP3_S |
			 RADEON_DP_SRC_SOURCE_MEMORY |
			 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);

L
Linus Torvalds 已提交
1389 1390
		/* Make this work even if front & back are flipped:
		 */
1391
		OUT_RING(CP_PACKET0(RADEON_SRC_PITCH_OFFSET, 1));
1392
		if (sarea_priv->pfCurrentPage == 0) {
D
Dave Airlie 已提交
1393 1394 1395 1396 1397
			OUT_RING(dev_priv->back_pitch_offset);
			OUT_RING(dev_priv->front_pitch_offset);
		} else {
			OUT_RING(dev_priv->front_pitch_offset);
			OUT_RING(dev_priv->back_pitch_offset);
L
Linus Torvalds 已提交
1398 1399
		}

1400
		OUT_RING(CP_PACKET0(RADEON_SRC_X_Y, 2));
D
Dave Airlie 已提交
1401 1402 1403
		OUT_RING((x << 16) | y);
		OUT_RING((x << 16) | y);
		OUT_RING((w << 16) | h);
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408 1409 1410 1411

		ADVANCE_RING();
	}

	/* Increment the frame counter.  The client-side 3D driver must
	 * throttle the framerate by waiting for this value before
	 * performing the swapbuffer ioctl.
	 */
1412
	sarea_priv->last_frame++;
L
Linus Torvalds 已提交
1413

D
Dave Airlie 已提交
1414
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1415

1416
	RADEON_FRAME_AGE(sarea_priv->last_frame);
L
Linus Torvalds 已提交
1417 1418 1419 1420 1421
	RADEON_WAIT_UNTIL_2D_IDLE();

	ADVANCE_RING();
}

1422
void radeon_cp_dispatch_flip(struct drm_device *dev, struct drm_master *master)
L
Linus Torvalds 已提交
1423 1424
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
1425 1426 1427
	struct drm_radeon_master_private *master_priv = master->driver_priv;
	struct drm_sarea *sarea = (struct drm_sarea *)master_priv->sarea->handle;
	int offset = (master_priv->sarea_priv->pfCurrentPage == 1)
D
Dave Airlie 已提交
1428
	    ? dev_priv->front_offset : dev_priv->back_offset;
L
Linus Torvalds 已提交
1429
	RING_LOCALS;
1430
	DRM_DEBUG("pfCurrentPage=%d\n",
1431
		  master_priv->sarea_priv->pfCurrentPage);
L
Linus Torvalds 已提交
1432 1433 1434 1435 1436

	/* Do some trivial performance monitoring...
	 */
	if (dev_priv->do_boxes) {
		dev_priv->stats.boxes |= RADEON_BOX_FLIP;
1437
		radeon_cp_performance_boxes(dev_priv, master_priv);
L
Linus Torvalds 已提交
1438 1439 1440 1441
	}

	/* Update the frame offsets for both CRTCs
	 */
D
Dave Airlie 已提交
1442
	BEGIN_RING(6);
L
Linus Torvalds 已提交
1443 1444

	RADEON_WAIT_UNTIL_3D_IDLE();
D
Dave Airlie 已提交
1445 1446 1447 1448
	OUT_RING_REG(RADEON_CRTC_OFFSET,
		     ((sarea->frame.y * dev_priv->front_pitch +
		       sarea->frame.x * (dev_priv->color_fmt - 2)) & ~7)
		     + offset);
1449
	OUT_RING_REG(RADEON_CRTC2_OFFSET, master_priv->sarea_priv->crtc2_base
D
Dave Airlie 已提交
1450
		     + offset);
L
Linus Torvalds 已提交
1451 1452 1453 1454 1455 1456 1457

	ADVANCE_RING();

	/* Increment the frame counter.  The client-side 3D driver must
	 * throttle the framerate by waiting for this value before
	 * performing the swapbuffer ioctl.
	 */
1458 1459 1460
	master_priv->sarea_priv->last_frame++;
	master_priv->sarea_priv->pfCurrentPage =
		1 - master_priv->sarea_priv->pfCurrentPage;
L
Linus Torvalds 已提交
1461

D
Dave Airlie 已提交
1462
	BEGIN_RING(2);
L
Linus Torvalds 已提交
1463

1464
	RADEON_FRAME_AGE(master_priv->sarea_priv->last_frame);
L
Linus Torvalds 已提交
1465 1466 1467 1468

	ADVANCE_RING();
}

D
Dave Airlie 已提交
1469
static int bad_prim_vertex_nr(int primitive, int nr)
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
{
	switch (primitive & RADEON_PRIM_TYPE_MASK) {
	case RADEON_PRIM_TYPE_NONE:
	case RADEON_PRIM_TYPE_POINT:
		return nr < 1;
	case RADEON_PRIM_TYPE_LINE:
		return (nr & 1) || nr == 0;
	case RADEON_PRIM_TYPE_LINE_STRIP:
		return nr < 2;
	case RADEON_PRIM_TYPE_TRI_LIST:
	case RADEON_PRIM_TYPE_3VRT_POINT_LIST:
	case RADEON_PRIM_TYPE_3VRT_LINE_LIST:
	case RADEON_PRIM_TYPE_RECT_LIST:
		return nr % 3 || nr == 0;
	case RADEON_PRIM_TYPE_TRI_FAN:
	case RADEON_PRIM_TYPE_TRI_STRIP:
		return nr < 3;
	default:
		return 1;
D
Dave Airlie 已提交
1489
	}
L
Linus Torvalds 已提交
1490 1491 1492 1493 1494 1495 1496
}

typedef struct {
	unsigned int start;
	unsigned int finish;
	unsigned int prim;
	unsigned int numverts;
D
Dave Airlie 已提交
1497 1498
	unsigned int offset;
	unsigned int vc_format;
L
Linus Torvalds 已提交
1499 1500
} drm_radeon_tcl_prim_t;

1501
static void radeon_cp_dispatch_vertex(struct drm_device * dev,
1502
				      struct drm_file *file_priv,
D
Dave Airlie 已提交
1503
				      struct drm_buf * buf,
D
Dave Airlie 已提交
1504
				      drm_radeon_tcl_prim_t * prim)
L
Linus Torvalds 已提交
1505 1506
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
1507 1508
	struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
	drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516
	int offset = dev_priv->gart_buffers_offset + buf->offset + prim->start;
	int numverts = (int)prim->numverts;
	int nbox = sarea_priv->nbox;
	int i = 0;
	RING_LOCALS;

	DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d %d verts\n",
		  prim->prim,
D
Dave Airlie 已提交
1517
		  prim->vc_format, prim->start, prim->finish, prim->numverts);
L
Linus Torvalds 已提交
1518

D
Dave Airlie 已提交
1519 1520 1521
	if (bad_prim_vertex_nr(prim->prim, prim->numverts)) {
		DRM_ERROR("bad prim %x numverts %d\n",
			  prim->prim, prim->numverts);
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526
		return;
	}

	do {
		/* Emit the next cliprect */
D
Dave Airlie 已提交
1527 1528
		if (i < nbox) {
			radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
L
Linus Torvalds 已提交
1529 1530 1531
		}

		/* Emit the vertex buffer rendering commands */
D
Dave Airlie 已提交
1532
		BEGIN_RING(5);
L
Linus Torvalds 已提交
1533

D
Dave Airlie 已提交
1534 1535 1536 1537 1538 1539 1540 1541
		OUT_RING(CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, 3));
		OUT_RING(offset);
		OUT_RING(numverts);
		OUT_RING(prim->vc_format);
		OUT_RING(prim->prim | RADEON_PRIM_WALK_LIST |
			 RADEON_COLOR_ORDER_RGBA |
			 RADEON_VTX_FMT_RADEON_MODE |
			 (numverts << RADEON_NUM_VERTICES_SHIFT));
L
Linus Torvalds 已提交
1542 1543 1544 1545

		ADVANCE_RING();

		i++;
D
Dave Airlie 已提交
1546
	} while (i < nbox);
L
Linus Torvalds 已提交
1547 1548
}

1549
static void radeon_cp_discard_buffer(struct drm_device *dev, struct drm_master *master, struct drm_buf *buf)
L
Linus Torvalds 已提交
1550 1551
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
1552
	struct drm_radeon_master_private *master_priv = master->driver_priv;
L
Linus Torvalds 已提交
1553 1554 1555
	drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
	RING_LOCALS;

1556
	buf_priv->age = ++master_priv->sarea_priv->last_dispatch;
L
Linus Torvalds 已提交
1557 1558

	/* Emit the vertex buffer age */
1559 1560 1561 1562 1563 1564 1565 1566 1567
	if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
		BEGIN_RING(3);
		R600_DISPATCH_AGE(buf_priv->age);
		ADVANCE_RING();
	} else {
		BEGIN_RING(2);
		RADEON_DISPATCH_AGE(buf_priv->age);
		ADVANCE_RING();
	}
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572

	buf->pending = 1;
	buf->used = 0;
}

1573
static void radeon_cp_dispatch_indirect(struct drm_device * dev,
D
Dave Airlie 已提交
1574
					struct drm_buf * buf, int start, int end)
L
Linus Torvalds 已提交
1575 1576 1577
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	RING_LOCALS;
1578
	DRM_DEBUG("buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
L
Linus Torvalds 已提交
1579

D
Dave Airlie 已提交
1580
	if (start != end) {
L
Linus Torvalds 已提交
1581 1582 1583 1584 1585 1586 1587 1588
		int offset = (dev_priv->gart_buffers_offset
			      + buf->offset + start);
		int dwords = (end - start + 3) / sizeof(u32);

		/* Indirect buffer data must be an even number of
		 * dwords, so if we've been given an odd number we must
		 * pad the data with a Type-2 CP packet.
		 */
D
Dave Airlie 已提交
1589
		if (dwords & 1) {
L
Linus Torvalds 已提交
1590
			u32 *data = (u32 *)
D
Dave Airlie 已提交
1591 1592
			    ((char *)dev->agp_buffer_map->handle
			     + buf->offset + start);
L
Linus Torvalds 已提交
1593 1594 1595 1596
			data[dwords++] = RADEON_CP_PACKET2;
		}

		/* Fire off the indirect buffer */
D
Dave Airlie 已提交
1597
		BEGIN_RING(3);
L
Linus Torvalds 已提交
1598

D
Dave Airlie 已提交
1599 1600 1601
		OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
		OUT_RING(offset);
		OUT_RING(dwords);
L
Linus Torvalds 已提交
1602 1603 1604 1605 1606

		ADVANCE_RING();
	}
}

1607 1608
static void radeon_cp_dispatch_indices(struct drm_device *dev,
				       struct drm_master *master,
D
Dave Airlie 已提交
1609
				       struct drm_buf * elt_buf,
D
Dave Airlie 已提交
1610
				       drm_radeon_tcl_prim_t * prim)
L
Linus Torvalds 已提交
1611 1612
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
1613 1614
	struct drm_radeon_master_private *master_priv = master->driver_priv;
	drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
L
Linus Torvalds 已提交
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	int offset = dev_priv->gart_buffers_offset + prim->offset;
	u32 *data;
	int dwords;
	int i = 0;
	int start = prim->start + RADEON_INDEX_PRIM_OFFSET;
	int count = (prim->finish - start) / sizeof(u16);
	int nbox = sarea_priv->nbox;

	DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d offset: %x nr %d\n",
		  prim->prim,
		  prim->vc_format,
D
Dave Airlie 已提交
1626 1627 1628 1629
		  prim->start, prim->finish, prim->offset, prim->numverts);

	if (bad_prim_vertex_nr(prim->prim, count)) {
		DRM_ERROR("bad prim %x count %d\n", prim->prim, count);
L
Linus Torvalds 已提交
1630 1631 1632
		return;
	}

D
Dave Airlie 已提交
1633 1634
	if (start >= prim->finish || (prim->start & 0x7)) {
		DRM_ERROR("buffer prim %d\n", prim->prim);
L
Linus Torvalds 已提交
1635 1636 1637 1638 1639
		return;
	}

	dwords = (prim->finish - prim->start + 3) / sizeof(u32);

D
Dave Airlie 已提交
1640 1641
	data = (u32 *) ((char *)dev->agp_buffer_map->handle +
			elt_buf->offset + prim->start);
L
Linus Torvalds 已提交
1642

D
Dave Airlie 已提交
1643
	data[0] = CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, dwords - 2);
L
Linus Torvalds 已提交
1644 1645 1646 1647 1648 1649 1650
	data[1] = offset;
	data[2] = prim->numverts;
	data[3] = prim->vc_format;
	data[4] = (prim->prim |
		   RADEON_PRIM_WALK_IND |
		   RADEON_COLOR_ORDER_RGBA |
		   RADEON_VTX_FMT_RADEON_MODE |
D
Dave Airlie 已提交
1651
		   (count << RADEON_NUM_VERTICES_SHIFT));
L
Linus Torvalds 已提交
1652 1653

	do {
D
Dave Airlie 已提交
1654 1655
		if (i < nbox)
			radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
L
Linus Torvalds 已提交
1656

D
Dave Airlie 已提交
1657 1658
		radeon_cp_dispatch_indirect(dev, elt_buf,
					    prim->start, prim->finish);
L
Linus Torvalds 已提交
1659 1660

		i++;
D
Dave Airlie 已提交
1661
	} while (i < nbox);
L
Linus Torvalds 已提交
1662 1663 1664

}

1665
#define RADEON_MAX_TEXTURE_SIZE RADEON_BUFFER_SIZE
L
Linus Torvalds 已提交
1666

1667 1668
static int radeon_cp_dispatch_texture(struct drm_device * dev,
				      struct drm_file *file_priv,
D
Dave Airlie 已提交
1669 1670
				      drm_radeon_texture_t * tex,
				      drm_radeon_tex_image_t * image)
L
Linus Torvalds 已提交
1671 1672
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
1673
	struct drm_buf *buf;
L
Linus Torvalds 已提交
1674 1675 1676
	u32 format;
	u32 *buffer;
	const u8 __user *data;
1677
	int size, dwords, tex_width, blit_width, spitch;
L
Linus Torvalds 已提交
1678 1679 1680
	u32 height;
	int i;
	u32 texpitch, microtile;
1681
	u32 offset, byte_offset;
L
Linus Torvalds 已提交
1682 1683
	RING_LOCALS;

1684
	if (radeon_check_and_fixup_offset(dev_priv, file_priv, &tex->offset)) {
D
Dave Airlie 已提交
1685
		DRM_ERROR("Invalid destination offset\n");
E
Eric Anholt 已提交
1686
		return -EINVAL;
L
Linus Torvalds 已提交
1687 1688 1689 1690 1691 1692 1693 1694
	}

	dev_priv->stats.boxes |= RADEON_BOX_TEXTURE_LOAD;

	/* Flush the pixel cache.  This ensures no pixel data gets mixed
	 * up with the texture data from the host data blit, otherwise
	 * part of the texture image may be corrupted.
	 */
D
Dave Airlie 已提交
1695
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1696 1697 1698 1699 1700 1701 1702 1703
	RADEON_FLUSH_CACHE();
	RADEON_WAIT_UNTIL_IDLE();
	ADVANCE_RING();

	/* The compiler won't optimize away a division by a variable,
	 * even if the only legal values are powers of two.  Thus, we'll
	 * use a shift instead.
	 */
D
Dave Airlie 已提交
1704
	switch (tex->format) {
L
Linus Torvalds 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
	case RADEON_TXFORMAT_ARGB8888:
	case RADEON_TXFORMAT_RGBA8888:
		format = RADEON_COLOR_FORMAT_ARGB8888;
		tex_width = tex->width * 4;
		blit_width = image->width * 4;
		break;
	case RADEON_TXFORMAT_AI88:
	case RADEON_TXFORMAT_ARGB1555:
	case RADEON_TXFORMAT_RGB565:
	case RADEON_TXFORMAT_ARGB4444:
	case RADEON_TXFORMAT_VYUY422:
	case RADEON_TXFORMAT_YVYU422:
		format = RADEON_COLOR_FORMAT_RGB565;
		tex_width = tex->width * 2;
		blit_width = image->width * 2;
		break;
	case RADEON_TXFORMAT_I8:
	case RADEON_TXFORMAT_RGB332:
		format = RADEON_COLOR_FORMAT_CI8;
		tex_width = tex->width * 1;
		blit_width = image->width * 1;
		break;
	default:
D
Dave Airlie 已提交
1728
		DRM_ERROR("invalid texture format %d\n", tex->format);
E
Eric Anholt 已提交
1729
		return -EINVAL;
L
Linus Torvalds 已提交
1730
	}
1731 1732
	spitch = blit_width >> 6;
	if (spitch == 0 && image->height > 1)
E
Eric Anholt 已提交
1733
		return -EINVAL;
1734

L
Linus Torvalds 已提交
1735 1736 1737 1738 1739 1740 1741 1742
	texpitch = tex->pitch;
	if ((texpitch << 22) & RADEON_DST_TILE_MICRO) {
		microtile = 1;
		if (tex_width < 64) {
			texpitch &= ~(RADEON_DST_TILE_MICRO >> 22);
			/* we got tiled coordinates, untile them */
			image->x *= 2;
		}
D
Dave Airlie 已提交
1743 1744
	} else
		microtile = 0;
L
Linus Torvalds 已提交
1745

1746 1747 1748 1749 1750 1751 1752
	/* this might fail for zero-sized uploads - are those illegal? */
	if (!radeon_check_offset(dev_priv, tex->offset + image->height *
				blit_width - 1)) {
		DRM_ERROR("Invalid final destination offset\n");
		return -EINVAL;
	}

D
Dave Airlie 已提交
1753
	DRM_DEBUG("tex=%dx%d blit=%d\n", tex_width, tex->height, blit_width);
L
Linus Torvalds 已提交
1754 1755

	do {
D
Dave Airlie 已提交
1756 1757 1758
		DRM_DEBUG("tex: ofs=0x%x p=%d f=%d x=%hd y=%hd w=%hd h=%hd\n",
			  tex->offset >> 10, tex->pitch, tex->format,
			  image->x, image->y, image->width, image->height);
L
Linus Torvalds 已提交
1759 1760 1761 1762 1763 1764

		/* Make a copy of some parameters in case we have to
		 * update them for a multi-pass texture blit.
		 */
		height = image->height;
		data = (const u8 __user *)image->data;
D
Dave Airlie 已提交
1765

L
Linus Torvalds 已提交
1766 1767
		size = height * blit_width;

D
Dave Airlie 已提交
1768
		if (size > RADEON_MAX_TEXTURE_SIZE) {
L
Linus Torvalds 已提交
1769 1770
			height = RADEON_MAX_TEXTURE_SIZE / blit_width;
			size = height * blit_width;
D
Dave Airlie 已提交
1771
		} else if (size < 4 && size > 0) {
L
Linus Torvalds 已提交
1772
			size = 4;
D
Dave Airlie 已提交
1773
		} else if (size == 0) {
L
Linus Torvalds 已提交
1774 1775 1776
			return 0;
		}

D
Dave Airlie 已提交
1777 1778 1779 1780
		buf = radeon_freelist_get(dev);
		if (0 && !buf) {
			radeon_do_cp_idle(dev_priv);
			buf = radeon_freelist_get(dev);
L
Linus Torvalds 已提交
1781
		}
D
Dave Airlie 已提交
1782
		if (!buf) {
1783
			DRM_DEBUG("EAGAIN\n");
D
Dave Airlie 已提交
1784
			if (DRM_COPY_TO_USER(tex->image, image, sizeof(*image)))
E
Eric Anholt 已提交
1785 1786
				return -EFAULT;
			return -EAGAIN;
L
Linus Torvalds 已提交
1787 1788 1789 1790
		}

		/* Dispatch the indirect buffer.
		 */
D
Dave Airlie 已提交
1791 1792
		buffer =
		    (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
L
Linus Torvalds 已提交
1793 1794
		dwords = size / 4;

1795 1796 1797 1798
#define RADEON_COPY_MT(_buf, _data, _width) \
	do { \
		if (DRM_COPY_FROM_USER(_buf, _data, (_width))) {\
			DRM_ERROR("EFAULT on pad, %d bytes\n", (_width)); \
E
Eric Anholt 已提交
1799
			return -EFAULT; \
1800 1801 1802
		} \
	} while(0)

L
Linus Torvalds 已提交
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
		if (microtile) {
			/* texture micro tiling in use, minimum texture width is thus 16 bytes.
			   however, we cannot use blitter directly for texture width < 64 bytes,
			   since minimum tex pitch is 64 bytes and we need this to match
			   the texture width, otherwise the blitter will tile it wrong.
			   Thus, tiling manually in this case. Additionally, need to special
			   case tex height = 1, since our actual image will have height 2
			   and we need to ensure we don't read beyond the texture size
			   from user space. */
			if (tex->height == 1) {
				if (tex_width >= 64 || tex_width <= 16) {
1814
					RADEON_COPY_MT(buffer, data,
D
Dave Airlie 已提交
1815
						(int)(tex_width * sizeof(u32)));
L
Linus Torvalds 已提交
1816
				} else if (tex_width == 32) {
1817 1818 1819
					RADEON_COPY_MT(buffer, data, 16);
					RADEON_COPY_MT(buffer + 8,
						       data + 16, 16);
L
Linus Torvalds 已提交
1820 1821
				}
			} else if (tex_width >= 64 || tex_width == 16) {
1822
				RADEON_COPY_MT(buffer, data,
D
Dave Airlie 已提交
1823
					       (int)(dwords * sizeof(u32)));
L
Linus Torvalds 已提交
1824 1825
			} else if (tex_width < 16) {
				for (i = 0; i < tex->height; i++) {
1826
					RADEON_COPY_MT(buffer, data, tex_width);
L
Linus Torvalds 已提交
1827 1828 1829 1830 1831 1832 1833
					buffer += 4;
					data += tex_width;
				}
			} else if (tex_width == 32) {
				/* TODO: make sure this works when not fitting in one buffer
				   (i.e. 32bytes x 2048...) */
				for (i = 0; i < tex->height; i += 2) {
1834
					RADEON_COPY_MT(buffer, data, 16);
L
Linus Torvalds 已提交
1835
					data += 16;
1836
					RADEON_COPY_MT(buffer + 8, data, 16);
L
Linus Torvalds 已提交
1837
					data += 16;
1838
					RADEON_COPY_MT(buffer + 4, data, 16);
L
Linus Torvalds 已提交
1839
					data += 16;
1840
					RADEON_COPY_MT(buffer + 12, data, 16);
L
Linus Torvalds 已提交
1841 1842 1843 1844
					data += 16;
					buffer += 16;
				}
			}
D
Dave Airlie 已提交
1845
		} else {
L
Linus Torvalds 已提交
1846 1847 1848 1849
			if (tex_width >= 32) {
				/* Texture image width is larger than the minimum, so we
				 * can upload it directly.
				 */
1850
				RADEON_COPY_MT(buffer, data,
D
Dave Airlie 已提交
1851
					       (int)(dwords * sizeof(u32)));
L
Linus Torvalds 已提交
1852 1853 1854 1855 1856
			} else {
				/* Texture image width is less than the minimum, so we
				 * need to pad out each image scanline to the minimum
				 * width.
				 */
D
Dave Airlie 已提交
1857
				for (i = 0; i < tex->height; i++) {
1858
					RADEON_COPY_MT(buffer, data, tex_width);
L
Linus Torvalds 已提交
1859 1860 1861 1862 1863 1864
					buffer += 8;
					data += tex_width;
				}
			}
		}

1865
#undef RADEON_COPY_MT
1866
		byte_offset = (image->y & ~2047) * blit_width;
1867
		buf->file_priv = file_priv;
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
		buf->used = size;
		offset = dev_priv->gart_buffers_offset + buf->offset;
		BEGIN_RING(9);
		OUT_RING(CP_PACKET3(RADEON_CNTL_BITBLT_MULTI, 5));
		OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
			 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
			 RADEON_GMC_BRUSH_NONE |
			 (format << 8) |
			 RADEON_GMC_SRC_DATATYPE_COLOR |
			 RADEON_ROP3_S |
			 RADEON_DP_SRC_SOURCE_MEMORY |
D
Dave Airlie 已提交
1879
			 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
1880
		OUT_RING((spitch << 22) | (offset >> 10));
1881
		OUT_RING((texpitch << 22) | ((tex->offset >> 10) + (byte_offset >> 10)));
1882
		OUT_RING(0);
1883
		OUT_RING((image->x << 16) | (image->y % 2048));
1884 1885 1886
		OUT_RING((image->width << 16) | height);
		RADEON_WAIT_UNTIL_2D_IDLE();
		ADVANCE_RING();
1887
		COMMIT_RING();
1888

1889
		radeon_cp_discard_buffer(dev, file_priv->master, buf);
L
Linus Torvalds 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900

		/* Update the input parameters for next time */
		image->y += height;
		image->height -= height;
		image->data = (const u8 __user *)image->data + size;
	} while (image->height > 0);

	/* Flush the pixel cache after the blit completes.  This ensures
	 * the texture data is written out to memory before rendering
	 * continues.
	 */
D
Dave Airlie 已提交
1901
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1902 1903 1904
	RADEON_FLUSH_CACHE();
	RADEON_WAIT_UNTIL_2D_IDLE();
	ADVANCE_RING();
1905 1906
	COMMIT_RING();

L
Linus Torvalds 已提交
1907 1908 1909
	return 0;
}

1910
static void radeon_cp_dispatch_stipple(struct drm_device * dev, u32 * stipple)
L
Linus Torvalds 已提交
1911 1912 1913 1914
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	int i;
	RING_LOCALS;
D
Dave Airlie 已提交
1915
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
1916

D
Dave Airlie 已提交
1917
	BEGIN_RING(35);
L
Linus Torvalds 已提交
1918

D
Dave Airlie 已提交
1919 1920
	OUT_RING(CP_PACKET0(RADEON_RE_STIPPLE_ADDR, 0));
	OUT_RING(0x00000000);
L
Linus Torvalds 已提交
1921

D
Dave Airlie 已提交
1922 1923 1924
	OUT_RING(CP_PACKET0_TABLE(RADEON_RE_STIPPLE_DATA, 31));
	for (i = 0; i < 32; i++) {
		OUT_RING(stipple[i]);
L
Linus Torvalds 已提交
1925 1926 1927 1928 1929
	}

	ADVANCE_RING();
}

D
Dave Airlie 已提交
1930
static void radeon_apply_surface_regs(int surf_index,
1931
				      drm_radeon_private_t *dev_priv)
L
Linus Torvalds 已提交
1932 1933 1934 1935 1936 1937
{
	if (!dev_priv->mmio)
		return;

	radeon_do_cp_idle(dev_priv);

D
Dave Airlie 已提交
1938 1939 1940 1941 1942 1943
	RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * surf_index,
		     dev_priv->surfaces[surf_index].flags);
	RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * surf_index,
		     dev_priv->surfaces[surf_index].lower);
	RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * surf_index,
		     dev_priv->surfaces[surf_index].upper);
L
Linus Torvalds 已提交
1944 1945 1946
}

/* Allocates a virtual surface
D
Dave Airlie 已提交
1947
 * doesn't always allocate a real surface, will stretch an existing
L
Linus Torvalds 已提交
1948 1949 1950 1951 1952
 * surface when possible.
 *
 * Note that refcount can be at most 2, since during a free refcount=3
 * might mean we have to allocate a new surface which might not always
 * be available.
D
Dave Airlie 已提交
1953
 * For example : we allocate three contigous surfaces ABC. If B is
L
Linus Torvalds 已提交
1954 1955 1956
 * freed, we suddenly need two surfaces to store A and C, which might
 * not always be available.
 */
1957
static int alloc_surface(drm_radeon_surface_alloc_t *new,
1958 1959
			 drm_radeon_private_t *dev_priv,
			 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
{
	struct radeon_virt_surface *s;
	int i;
	int virt_surface_index;
	uint32_t new_upper, new_lower;

	new_lower = new->address;
	new_upper = new_lower + new->size - 1;

	/* sanity check */
	if ((new_lower >= new_upper) || (new->flags == 0) || (new->size == 0) ||
D
Dave Airlie 已提交
1971 1972 1973
	    ((new_upper & RADEON_SURF_ADDRESS_FIXED_MASK) !=
	     RADEON_SURF_ADDRESS_FIXED_MASK)
	    || ((new_lower & RADEON_SURF_ADDRESS_FIXED_MASK) != 0))
L
Linus Torvalds 已提交
1974 1975 1976 1977 1978
		return -1;

	/* make sure there is no overlap with existing surfaces */
	for (i = 0; i < RADEON_MAX_SURFACES; i++) {
		if ((dev_priv->surfaces[i].refcount != 0) &&
D
Dave Airlie 已提交
1979 1980 1981 1982 1983 1984
		    (((new_lower >= dev_priv->surfaces[i].lower) &&
		      (new_lower < dev_priv->surfaces[i].upper)) ||
		     ((new_lower < dev_priv->surfaces[i].lower) &&
		      (new_upper > dev_priv->surfaces[i].lower)))) {
			return -1;
		}
L
Linus Torvalds 已提交
1985 1986 1987
	}

	/* find a virtual surface */
D
Dave Airlie 已提交
1988
	for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++)
1989
		if (dev_priv->virt_surfaces[i].file_priv == NULL)
L
Linus Torvalds 已提交
1990
			break;
D
Dave Airlie 已提交
1991 1992 1993
	if (i == 2 * RADEON_MAX_SURFACES) {
		return -1;
	}
L
Linus Torvalds 已提交
1994 1995 1996 1997 1998 1999
	virt_surface_index = i;

	/* try to reuse an existing surface */
	for (i = 0; i < RADEON_MAX_SURFACES; i++) {
		/* extend before */
		if ((dev_priv->surfaces[i].refcount == 1) &&
D
Dave Airlie 已提交
2000 2001
		    (new->flags == dev_priv->surfaces[i].flags) &&
		    (new_upper + 1 == dev_priv->surfaces[i].lower)) {
L
Linus Torvalds 已提交
2002 2003 2004 2005 2006
			s = &(dev_priv->virt_surfaces[virt_surface_index]);
			s->surface_index = i;
			s->lower = new_lower;
			s->upper = new_upper;
			s->flags = new->flags;
2007
			s->file_priv = file_priv;
L
Linus Torvalds 已提交
2008 2009 2010 2011 2012 2013 2014 2015
			dev_priv->surfaces[i].refcount++;
			dev_priv->surfaces[i].lower = s->lower;
			radeon_apply_surface_regs(s->surface_index, dev_priv);
			return virt_surface_index;
		}

		/* extend after */
		if ((dev_priv->surfaces[i].refcount == 1) &&
D
Dave Airlie 已提交
2016 2017
		    (new->flags == dev_priv->surfaces[i].flags) &&
		    (new_lower == dev_priv->surfaces[i].upper + 1)) {
L
Linus Torvalds 已提交
2018 2019 2020 2021 2022
			s = &(dev_priv->virt_surfaces[virt_surface_index]);
			s->surface_index = i;
			s->lower = new_lower;
			s->upper = new_upper;
			s->flags = new->flags;
2023
			s->file_priv = file_priv;
L
Linus Torvalds 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
			dev_priv->surfaces[i].refcount++;
			dev_priv->surfaces[i].upper = s->upper;
			radeon_apply_surface_regs(s->surface_index, dev_priv);
			return virt_surface_index;
		}
	}

	/* okay, we need a new one */
	for (i = 0; i < RADEON_MAX_SURFACES; i++) {
		if (dev_priv->surfaces[i].refcount == 0) {
			s = &(dev_priv->virt_surfaces[virt_surface_index]);
			s->surface_index = i;
			s->lower = new_lower;
			s->upper = new_upper;
			s->flags = new->flags;
2039
			s->file_priv = file_priv;
L
Linus Torvalds 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
			dev_priv->surfaces[i].refcount = 1;
			dev_priv->surfaces[i].lower = s->lower;
			dev_priv->surfaces[i].upper = s->upper;
			dev_priv->surfaces[i].flags = s->flags;
			radeon_apply_surface_regs(s->surface_index, dev_priv);
			return virt_surface_index;
		}
	}

	/* we didn't find anything */
	return -1;
}

2053 2054
static int free_surface(struct drm_file *file_priv,
			drm_radeon_private_t * dev_priv,
D
Dave Airlie 已提交
2055
			int lower)
L
Linus Torvalds 已提交
2056 2057 2058 2059
{
	struct radeon_virt_surface *s;
	int i;
	/* find the virtual surface */
D
Dave Airlie 已提交
2060
	for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
L
Linus Torvalds 已提交
2061
		s = &(dev_priv->virt_surfaces[i]);
2062 2063 2064
		if (s->file_priv) {
			if ((lower == s->lower) && (file_priv == s->file_priv))
			{
D
Dave Airlie 已提交
2065 2066 2067 2068
				if (dev_priv->surfaces[s->surface_index].
				    lower == s->lower)
					dev_priv->surfaces[s->surface_index].
					    lower = s->upper;
L
Linus Torvalds 已提交
2069

D
Dave Airlie 已提交
2070 2071 2072 2073
				if (dev_priv->surfaces[s->surface_index].
				    upper == s->upper)
					dev_priv->surfaces[s->surface_index].
					    upper = s->lower;
L
Linus Torvalds 已提交
2074 2075

				dev_priv->surfaces[s->surface_index].refcount--;
D
Dave Airlie 已提交
2076 2077 2078 2079
				if (dev_priv->surfaces[s->surface_index].
				    refcount == 0)
					dev_priv->surfaces[s->surface_index].
					    flags = 0;
2080
				s->file_priv = NULL;
D
Dave Airlie 已提交
2081 2082
				radeon_apply_surface_regs(s->surface_index,
							  dev_priv);
L
Linus Torvalds 已提交
2083 2084 2085 2086 2087 2088 2089
				return 0;
			}
		}
	}
	return 1;
}

2090
static void radeon_surfaces_release(struct drm_file *file_priv,
D
Dave Airlie 已提交
2091
				    drm_radeon_private_t * dev_priv)
L
Linus Torvalds 已提交
2092 2093
{
	int i;
D
Dave Airlie 已提交
2094
	for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
2095 2096
		if (dev_priv->virt_surfaces[i].file_priv == file_priv)
			free_surface(file_priv, dev_priv,
D
Dave Airlie 已提交
2097
				     dev_priv->virt_surfaces[i].lower);
L
Linus Torvalds 已提交
2098 2099 2100 2101 2102 2103
	}
}

/* ================================================================
 * IOCTL functions
 */
2104
static int radeon_surface_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2105 2106
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2107
	drm_radeon_surface_alloc_t *alloc = data;
L
Linus Torvalds 已提交
2108

2109
	if (alloc_surface(alloc, dev_priv, file_priv) == -1)
E
Eric Anholt 已提交
2110
		return -EINVAL;
L
Linus Torvalds 已提交
2111 2112 2113 2114
	else
		return 0;
}

2115
static int radeon_surface_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2116 2117
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2118
	drm_radeon_surface_free_t *memfree = data;
L
Linus Torvalds 已提交
2119

2120
	if (free_surface(file_priv, dev_priv, memfree->address))
E
Eric Anholt 已提交
2121
		return -EINVAL;
L
Linus Torvalds 已提交
2122 2123 2124 2125
	else
		return 0;
}

2126
static int radeon_cp_clear(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2127 2128
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2129 2130
	struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
	drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
2131
	drm_radeon_clear_t *clear = data;
L
Linus Torvalds 已提交
2132
	drm_radeon_clear_rect_t depth_boxes[RADEON_NR_SAREA_CLIPRECTS];
D
Dave Airlie 已提交
2133
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
2134

2135
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2136

D
Dave Airlie 已提交
2137
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2138

D
Dave Airlie 已提交
2139
	if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
L
Linus Torvalds 已提交
2140 2141
		sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;

2142
	if (DRM_COPY_FROM_USER(&depth_boxes, clear->depth_boxes,
D
Dave Airlie 已提交
2143
			       sarea_priv->nbox * sizeof(depth_boxes[0])))
E
Eric Anholt 已提交
2144
		return -EFAULT;
L
Linus Torvalds 已提交
2145

2146
	radeon_cp_dispatch_clear(dev, file_priv->master, clear, depth_boxes);
L
Linus Torvalds 已提交
2147 2148 2149 2150 2151 2152

	COMMIT_RING();
	return 0;
}

/* Not sure why this isn't set all the time:
D
Dave Airlie 已提交
2153
 */
2154
static int radeon_do_init_pageflip(struct drm_device *dev, struct drm_master *master)
L
Linus Torvalds 已提交
2155 2156
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2157
	struct drm_radeon_master_private *master_priv = master->driver_priv;
L
Linus Torvalds 已提交
2158 2159
	RING_LOCALS;

D
Dave Airlie 已提交
2160
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
2161

D
Dave Airlie 已提交
2162
	BEGIN_RING(6);
L
Linus Torvalds 已提交
2163
	RADEON_WAIT_UNTIL_3D_IDLE();
D
Dave Airlie 已提交
2164 2165 2166 2167 2168 2169
	OUT_RING(CP_PACKET0(RADEON_CRTC_OFFSET_CNTL, 0));
	OUT_RING(RADEON_READ(RADEON_CRTC_OFFSET_CNTL) |
		 RADEON_CRTC_OFFSET_FLIP_CNTL);
	OUT_RING(CP_PACKET0(RADEON_CRTC2_OFFSET_CNTL, 0));
	OUT_RING(RADEON_READ(RADEON_CRTC2_OFFSET_CNTL) |
		 RADEON_CRTC_OFFSET_FLIP_CNTL);
L
Linus Torvalds 已提交
2170 2171 2172 2173
	ADVANCE_RING();

	dev_priv->page_flipping = 1;

2174 2175
	if (master_priv->sarea_priv->pfCurrentPage != 1)
		master_priv->sarea_priv->pfCurrentPage = 0;
L
Linus Torvalds 已提交
2176 2177 2178 2179 2180

	return 0;
}

/* Swapping and flipping are different operations, need different ioctls.
D
Dave Airlie 已提交
2181
 * They can & should be intermixed to support multiple 3d windows.
L
Linus Torvalds 已提交
2182
 */
2183
static int radeon_cp_flip(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2184 2185
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
2186 2187
	DRM_DEBUG("\n");

2188
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2189

D
Dave Airlie 已提交
2190
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2191

D
Dave Airlie 已提交
2192
	if (!dev_priv->page_flipping)
2193
		radeon_do_init_pageflip(dev, file_priv->master);
L
Linus Torvalds 已提交
2194

2195
	radeon_cp_dispatch_flip(dev, file_priv->master);
L
Linus Torvalds 已提交
2196 2197 2198 2199 2200

	COMMIT_RING();
	return 0;
}

2201
static int radeon_cp_swap(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2202 2203
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2204 2205 2206
	struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
	drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;

D
Dave Airlie 已提交
2207
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
2208

2209
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2210

D
Dave Airlie 已提交
2211
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2212

D
Dave Airlie 已提交
2213
	if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
L
Linus Torvalds 已提交
2214 2215
		sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;

2216 2217
	radeon_cp_dispatch_swap(dev, file_priv->master);
	sarea_priv->ctx_owner = 0;
L
Linus Torvalds 已提交
2218 2219 2220 2221 2222

	COMMIT_RING();
	return 0;
}

2223
static int radeon_cp_vertex(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2224 2225
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2226 2227
	struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
	drm_radeon_sarea_t *sarea_priv;
2228
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
2229
	struct drm_buf *buf;
2230
	drm_radeon_vertex_t *vertex = data;
L
Linus Torvalds 已提交
2231 2232
	drm_radeon_tcl_prim_t prim;

2233
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2234

2235 2236
	sarea_priv = master_priv->sarea_priv;

D
Dave Airlie 已提交
2237
	DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
2238
		  DRM_CURRENTPID, vertex->idx, vertex->count, vertex->discard);
L
Linus Torvalds 已提交
2239

2240
	if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
D
Dave Airlie 已提交
2241
		DRM_ERROR("buffer index %d (of %d max)\n",
2242
			  vertex->idx, dma->buf_count - 1);
E
Eric Anholt 已提交
2243
		return -EINVAL;
L
Linus Torvalds 已提交
2244
	}
2245 2246
	if (vertex->prim < 0 || vertex->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
		DRM_ERROR("buffer prim %d\n", vertex->prim);
E
Eric Anholt 已提交
2247
		return -EINVAL;
L
Linus Torvalds 已提交
2248 2249
	}

D
Dave Airlie 已提交
2250 2251
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2252

2253
	buf = dma->buflist[vertex->idx];
L
Linus Torvalds 已提交
2254

2255
	if (buf->file_priv != file_priv) {
D
Dave Airlie 已提交
2256
		DRM_ERROR("process %d using buffer owned by %p\n",
2257
			  DRM_CURRENTPID, buf->file_priv);
E
Eric Anholt 已提交
2258
		return -EINVAL;
L
Linus Torvalds 已提交
2259
	}
D
Dave Airlie 已提交
2260
	if (buf->pending) {
2261
		DRM_ERROR("sending pending buffer %d\n", vertex->idx);
E
Eric Anholt 已提交
2262
		return -EINVAL;
L
Linus Torvalds 已提交
2263 2264 2265 2266
	}

	/* Build up a prim_t record:
	 */
2267 2268
	if (vertex->count) {
		buf->used = vertex->count;	/* not used? */
D
Dave Airlie 已提交
2269 2270

		if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
2271
			if (radeon_emit_state(dev_priv, file_priv,
D
Dave Airlie 已提交
2272 2273 2274 2275
					      &sarea_priv->context_state,
					      sarea_priv->tex_state,
					      sarea_priv->dirty)) {
				DRM_ERROR("radeon_emit_state failed\n");
E
Eric Anholt 已提交
2276
				return -EINVAL;
L
Linus Torvalds 已提交
2277 2278 2279 2280 2281 2282 2283 2284 2285
			}

			sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
					       RADEON_UPLOAD_TEX1IMAGES |
					       RADEON_UPLOAD_TEX2IMAGES |
					       RADEON_REQUIRE_QUIESCENCE);
		}

		prim.start = 0;
2286 2287 2288
		prim.finish = vertex->count;	/* unused */
		prim.prim = vertex->prim;
		prim.numverts = vertex->count;
2289
		prim.vc_format = sarea_priv->vc_format;
D
Dave Airlie 已提交
2290

2291
		radeon_cp_dispatch_vertex(dev, file_priv, buf, &prim);
L
Linus Torvalds 已提交
2292 2293
	}

2294
	if (vertex->discard) {
2295
		radeon_cp_discard_buffer(dev, file_priv->master, buf);
L
Linus Torvalds 已提交
2296 2297 2298 2299 2300 2301
	}

	COMMIT_RING();
	return 0;
}

2302
static int radeon_cp_indices(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2303 2304
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2305 2306
	struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
	drm_radeon_sarea_t *sarea_priv;
2307
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
2308
	struct drm_buf *buf;
2309
	drm_radeon_indices_t *elts = data;
L
Linus Torvalds 已提交
2310 2311 2312
	drm_radeon_tcl_prim_t prim;
	int count;

2313
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2314

2315 2316
	sarea_priv = master_priv->sarea_priv;

D
Dave Airlie 已提交
2317
	DRM_DEBUG("pid=%d index=%d start=%d end=%d discard=%d\n",
2318 2319
		  DRM_CURRENTPID, elts->idx, elts->start, elts->end,
		  elts->discard);
L
Linus Torvalds 已提交
2320

2321
	if (elts->idx < 0 || elts->idx >= dma->buf_count) {
D
Dave Airlie 已提交
2322
		DRM_ERROR("buffer index %d (of %d max)\n",
2323
			  elts->idx, dma->buf_count - 1);
E
Eric Anholt 已提交
2324
		return -EINVAL;
L
Linus Torvalds 已提交
2325
	}
2326 2327
	if (elts->prim < 0 || elts->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
		DRM_ERROR("buffer prim %d\n", elts->prim);
E
Eric Anholt 已提交
2328
		return -EINVAL;
L
Linus Torvalds 已提交
2329 2330
	}

D
Dave Airlie 已提交
2331 2332
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2333

2334
	buf = dma->buflist[elts->idx];
L
Linus Torvalds 已提交
2335

2336
	if (buf->file_priv != file_priv) {
D
Dave Airlie 已提交
2337
		DRM_ERROR("process %d using buffer owned by %p\n",
2338
			  DRM_CURRENTPID, buf->file_priv);
E
Eric Anholt 已提交
2339
		return -EINVAL;
L
Linus Torvalds 已提交
2340
	}
D
Dave Airlie 已提交
2341
	if (buf->pending) {
2342
		DRM_ERROR("sending pending buffer %d\n", elts->idx);
E
Eric Anholt 已提交
2343
		return -EINVAL;
L
Linus Torvalds 已提交
2344 2345
	}

2346 2347
	count = (elts->end - elts->start) / sizeof(u16);
	elts->start -= RADEON_INDEX_PRIM_OFFSET;
L
Linus Torvalds 已提交
2348

2349 2350
	if (elts->start & 0x7) {
		DRM_ERROR("misaligned buffer 0x%x\n", elts->start);
E
Eric Anholt 已提交
2351
		return -EINVAL;
L
Linus Torvalds 已提交
2352
	}
2353 2354
	if (elts->start < buf->used) {
		DRM_ERROR("no header 0x%x - 0x%x\n", elts->start, buf->used);
E
Eric Anholt 已提交
2355
		return -EINVAL;
L
Linus Torvalds 已提交
2356 2357
	}

2358
	buf->used = elts->end;
L
Linus Torvalds 已提交
2359

D
Dave Airlie 已提交
2360
	if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
2361
		if (radeon_emit_state(dev_priv, file_priv,
D
Dave Airlie 已提交
2362 2363 2364 2365
				      &sarea_priv->context_state,
				      sarea_priv->tex_state,
				      sarea_priv->dirty)) {
			DRM_ERROR("radeon_emit_state failed\n");
E
Eric Anholt 已提交
2366
			return -EINVAL;
L
Linus Torvalds 已提交
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
		}

		sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
				       RADEON_UPLOAD_TEX1IMAGES |
				       RADEON_UPLOAD_TEX2IMAGES |
				       RADEON_REQUIRE_QUIESCENCE);
	}

	/* Build up a prim_t record:
	 */
2377 2378 2379
	prim.start = elts->start;
	prim.finish = elts->end;
	prim.prim = elts->prim;
L
Linus Torvalds 已提交
2380
	prim.offset = 0;	/* offset from start of dma buffers */
D
Dave Airlie 已提交
2381
	prim.numverts = RADEON_MAX_VB_VERTS;	/* duh */
2382
	prim.vc_format = sarea_priv->vc_format;
D
Dave Airlie 已提交
2383

2384
	radeon_cp_dispatch_indices(dev, file_priv->master, buf, &prim);
2385
	if (elts->discard) {
2386
		radeon_cp_discard_buffer(dev, file_priv->master, buf);
L
Linus Torvalds 已提交
2387 2388 2389 2390 2391 2392
	}

	COMMIT_RING();
	return 0;
}

2393
static int radeon_cp_texture(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2394 2395
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2396
	drm_radeon_texture_t *tex = data;
L
Linus Torvalds 已提交
2397 2398 2399
	drm_radeon_tex_image_t image;
	int ret;

2400
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2401

2402
	if (tex->image == NULL) {
D
Dave Airlie 已提交
2403
		DRM_ERROR("null texture image!\n");
E
Eric Anholt 已提交
2404
		return -EINVAL;
L
Linus Torvalds 已提交
2405 2406
	}

D
Dave Airlie 已提交
2407
	if (DRM_COPY_FROM_USER(&image,
2408
			       (drm_radeon_tex_image_t __user *) tex->image,
D
Dave Airlie 已提交
2409
			       sizeof(image)))
E
Eric Anholt 已提交
2410
		return -EFAULT;
L
Linus Torvalds 已提交
2411

D
Dave Airlie 已提交
2412 2413
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2414

2415
	ret = radeon_cp_dispatch_texture(dev, file_priv, tex, &image);
L
Linus Torvalds 已提交
2416 2417 2418 2419

	return ret;
}

2420
static int radeon_cp_stipple(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2421 2422
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2423
	drm_radeon_stipple_t *stipple = data;
L
Linus Torvalds 已提交
2424 2425
	u32 mask[32];

2426
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2427

2428
	if (DRM_COPY_FROM_USER(&mask, stipple->mask, 32 * sizeof(u32)))
E
Eric Anholt 已提交
2429
		return -EFAULT;
L
Linus Torvalds 已提交
2430

D
Dave Airlie 已提交
2431
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2432

D
Dave Airlie 已提交
2433
	radeon_cp_dispatch_stipple(dev, mask);
L
Linus Torvalds 已提交
2434 2435 2436 2437 2438

	COMMIT_RING();
	return 0;
}

2439
static int radeon_cp_indirect(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2440 2441
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2442
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
2443
	struct drm_buf *buf;
2444
	drm_radeon_indirect_t *indirect = data;
L
Linus Torvalds 已提交
2445 2446
	RING_LOCALS;

2447
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2448

2449
	DRM_DEBUG("idx=%d s=%d e=%d d=%d\n",
2450 2451
		  indirect->idx, indirect->start, indirect->end,
		  indirect->discard);
L
Linus Torvalds 已提交
2452

2453
	if (indirect->idx < 0 || indirect->idx >= dma->buf_count) {
D
Dave Airlie 已提交
2454
		DRM_ERROR("buffer index %d (of %d max)\n",
2455
			  indirect->idx, dma->buf_count - 1);
E
Eric Anholt 已提交
2456
		return -EINVAL;
L
Linus Torvalds 已提交
2457 2458
	}

2459
	buf = dma->buflist[indirect->idx];
L
Linus Torvalds 已提交
2460

2461
	if (buf->file_priv != file_priv) {
D
Dave Airlie 已提交
2462
		DRM_ERROR("process %d using buffer owned by %p\n",
2463
			  DRM_CURRENTPID, buf->file_priv);
E
Eric Anholt 已提交
2464
		return -EINVAL;
L
Linus Torvalds 已提交
2465
	}
D
Dave Airlie 已提交
2466
	if (buf->pending) {
2467
		DRM_ERROR("sending pending buffer %d\n", indirect->idx);
E
Eric Anholt 已提交
2468
		return -EINVAL;
L
Linus Torvalds 已提交
2469 2470
	}

2471
	if (indirect->start < buf->used) {
D
Dave Airlie 已提交
2472
		DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
2473
			  indirect->start, buf->used);
E
Eric Anholt 已提交
2474
		return -EINVAL;
L
Linus Torvalds 已提交
2475 2476
	}

D
Dave Airlie 已提交
2477 2478
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2479

2480
	buf->used = indirect->end;
L
Linus Torvalds 已提交
2481 2482 2483 2484 2485

	/* Dispatch the indirect buffer full of commands from the
	 * X server.  This is insecure and is thus only available to
	 * privileged clients.
	 */
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
	if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
		r600_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
	else {
		/* Wait for the 3D stream to idle before the indirect buffer
		 * containing 2D acceleration commands is processed.
		 */
		BEGIN_RING(2);
		RADEON_WAIT_UNTIL_3D_IDLE();
		ADVANCE_RING();
		radeon_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
L
Linus Torvalds 已提交
2496 2497
	}

2498 2499 2500
	if (indirect->discard)
		radeon_cp_discard_buffer(dev, file_priv->master, buf);

L
Linus Torvalds 已提交
2501 2502 2503 2504
	COMMIT_RING();
	return 0;
}

2505
static int radeon_cp_vertex2(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2506 2507
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2508 2509
	struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
	drm_radeon_sarea_t *sarea_priv;
2510
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
2511
	struct drm_buf *buf;
2512
	drm_radeon_vertex2_t *vertex = data;
L
Linus Torvalds 已提交
2513 2514 2515
	int i;
	unsigned char laststate;

2516
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2517

2518 2519
	sarea_priv = master_priv->sarea_priv;

D
Dave Airlie 已提交
2520
	DRM_DEBUG("pid=%d index=%d discard=%d\n",
2521
		  DRM_CURRENTPID, vertex->idx, vertex->discard);
L
Linus Torvalds 已提交
2522

2523
	if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
D
Dave Airlie 已提交
2524
		DRM_ERROR("buffer index %d (of %d max)\n",
2525
			  vertex->idx, dma->buf_count - 1);
E
Eric Anholt 已提交
2526
		return -EINVAL;
L
Linus Torvalds 已提交
2527 2528
	}

D
Dave Airlie 已提交
2529 2530
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2531

2532
	buf = dma->buflist[vertex->idx];
L
Linus Torvalds 已提交
2533

2534
	if (buf->file_priv != file_priv) {
D
Dave Airlie 已提交
2535
		DRM_ERROR("process %d using buffer owned by %p\n",
2536
			  DRM_CURRENTPID, buf->file_priv);
E
Eric Anholt 已提交
2537
		return -EINVAL;
L
Linus Torvalds 已提交
2538 2539
	}

D
Dave Airlie 已提交
2540
	if (buf->pending) {
2541
		DRM_ERROR("sending pending buffer %d\n", vertex->idx);
E
Eric Anholt 已提交
2542
		return -EINVAL;
L
Linus Torvalds 已提交
2543
	}
D
Dave Airlie 已提交
2544

L
Linus Torvalds 已提交
2545
	if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
E
Eric Anholt 已提交
2546
		return -EINVAL;
L
Linus Torvalds 已提交
2547

2548
	for (laststate = 0xff, i = 0; i < vertex->nr_prims; i++) {
L
Linus Torvalds 已提交
2549 2550
		drm_radeon_prim_t prim;
		drm_radeon_tcl_prim_t tclprim;
D
Dave Airlie 已提交
2551

2552
		if (DRM_COPY_FROM_USER(&prim, &vertex->prim[i], sizeof(prim)))
E
Eric Anholt 已提交
2553
			return -EFAULT;
D
Dave Airlie 已提交
2554 2555 2556 2557 2558

		if (prim.stateidx != laststate) {
			drm_radeon_state_t state;

			if (DRM_COPY_FROM_USER(&state,
2559
					       &vertex->state[prim.stateidx],
D
Dave Airlie 已提交
2560
					       sizeof(state)))
E
Eric Anholt 已提交
2561
				return -EFAULT;
L
Linus Torvalds 已提交
2562

2563
			if (radeon_emit_state2(dev_priv, file_priv, &state)) {
D
Dave Airlie 已提交
2564
				DRM_ERROR("radeon_emit_state2 failed\n");
E
Eric Anholt 已提交
2565
				return -EINVAL;
L
Linus Torvalds 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
			}

			laststate = prim.stateidx;
		}

		tclprim.start = prim.start;
		tclprim.finish = prim.finish;
		tclprim.prim = prim.prim;
		tclprim.vc_format = prim.vc_format;

D
Dave Airlie 已提交
2576
		if (prim.prim & RADEON_PRIM_WALK_IND) {
L
Linus Torvalds 已提交
2577
			tclprim.offset = prim.numverts * 64;
D
Dave Airlie 已提交
2578
			tclprim.numverts = RADEON_MAX_VB_VERTS;	/* duh */
L
Linus Torvalds 已提交
2579

2580
			radeon_cp_dispatch_indices(dev, file_priv->master, buf, &tclprim);
L
Linus Torvalds 已提交
2581 2582
		} else {
			tclprim.numverts = prim.numverts;
D
Dave Airlie 已提交
2583
			tclprim.offset = 0;	/* not used */
L
Linus Torvalds 已提交
2584

2585
			radeon_cp_dispatch_vertex(dev, file_priv, buf, &tclprim);
L
Linus Torvalds 已提交
2586
		}
D
Dave Airlie 已提交
2587

L
Linus Torvalds 已提交
2588 2589 2590 2591
		if (sarea_priv->nbox == 1)
			sarea_priv->nbox = 0;
	}

2592
	if (vertex->discard) {
2593
		radeon_cp_discard_buffer(dev, file_priv->master, buf);
L
Linus Torvalds 已提交
2594 2595 2596 2597 2598 2599
	}

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2600
static int radeon_emit_packets(drm_radeon_private_t * dev_priv,
2601
			       struct drm_file *file_priv,
D
Dave Airlie 已提交
2602
			       drm_radeon_cmd_header_t header,
2603
			       drm_radeon_kcmd_buffer_t *cmdbuf)
L
Linus Torvalds 已提交
2604 2605 2606 2607 2608
{
	int id = (int)header.packet.packet_id;
	int sz, reg;
	int *data = (int *)cmdbuf->buf;
	RING_LOCALS;
D
Dave Airlie 已提交
2609

L
Linus Torvalds 已提交
2610
	if (id >= RADEON_MAX_STATE_PACKETS)
E
Eric Anholt 已提交
2611
		return -EINVAL;
L
Linus Torvalds 已提交
2612 2613 2614 2615 2616

	sz = packet[id].len;
	reg = packet[id].start;

	if (sz * sizeof(int) > cmdbuf->bufsz) {
D
Dave Airlie 已提交
2617
		DRM_ERROR("Packet size provided larger than data provided\n");
E
Eric Anholt 已提交
2618
		return -EINVAL;
L
Linus Torvalds 已提交
2619 2620
	}

2621
	if (radeon_check_and_fixup_packets(dev_priv, file_priv, id, data)) {
D
Dave Airlie 已提交
2622
		DRM_ERROR("Packet verification failed\n");
E
Eric Anholt 已提交
2623
		return -EINVAL;
L
Linus Torvalds 已提交
2624 2625
	}

D
Dave Airlie 已提交
2626 2627 2628
	BEGIN_RING(sz + 1);
	OUT_RING(CP_PACKET0(reg, (sz - 1)));
	OUT_RING_TABLE(data, sz);
L
Linus Torvalds 已提交
2629 2630 2631 2632 2633 2634 2635
	ADVANCE_RING();

	cmdbuf->buf += sz * sizeof(int);
	cmdbuf->bufsz -= sz * sizeof(int);
	return 0;
}

2636
static __inline__ int radeon_emit_scalars(drm_radeon_private_t *dev_priv,
D
Dave Airlie 已提交
2637
					  drm_radeon_cmd_header_t header,
2638
					  drm_radeon_kcmd_buffer_t *cmdbuf)
L
Linus Torvalds 已提交
2639 2640 2641 2642 2643 2644
{
	int sz = header.scalars.count;
	int start = header.scalars.offset;
	int stride = header.scalars.stride;
	RING_LOCALS;

D
Dave Airlie 已提交
2645 2646 2647 2648 2649
	BEGIN_RING(3 + sz);
	OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
	OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
	OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
	OUT_RING_TABLE(cmdbuf->buf, sz);
L
Linus Torvalds 已提交
2650 2651 2652 2653 2654 2655 2656 2657
	ADVANCE_RING();
	cmdbuf->buf += sz * sizeof(int);
	cmdbuf->bufsz -= sz * sizeof(int);
	return 0;
}

/* God this is ugly
 */
2658
static __inline__ int radeon_emit_scalars2(drm_radeon_private_t *dev_priv,
D
Dave Airlie 已提交
2659
					   drm_radeon_cmd_header_t header,
2660
					   drm_radeon_kcmd_buffer_t *cmdbuf)
L
Linus Torvalds 已提交
2661 2662 2663 2664 2665 2666
{
	int sz = header.scalars.count;
	int start = ((unsigned int)header.scalars.offset) + 0x100;
	int stride = header.scalars.stride;
	RING_LOCALS;

D
Dave Airlie 已提交
2667 2668 2669 2670 2671
	BEGIN_RING(3 + sz);
	OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
	OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
	OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
	OUT_RING_TABLE(cmdbuf->buf, sz);
L
Linus Torvalds 已提交
2672 2673 2674 2675 2676 2677
	ADVANCE_RING();
	cmdbuf->buf += sz * sizeof(int);
	cmdbuf->bufsz -= sz * sizeof(int);
	return 0;
}

2678
static __inline__ int radeon_emit_vectors(drm_radeon_private_t *dev_priv,
D
Dave Airlie 已提交
2679
					  drm_radeon_cmd_header_t header,
2680
					  drm_radeon_kcmd_buffer_t *cmdbuf)
L
Linus Torvalds 已提交
2681 2682 2683 2684 2685 2686
{
	int sz = header.vectors.count;
	int start = header.vectors.offset;
	int stride = header.vectors.stride;
	RING_LOCALS;

2687 2688
	BEGIN_RING(5 + sz);
	OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
D
Dave Airlie 已提交
2689 2690 2691 2692
	OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
	OUT_RING(start | (stride << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
	OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
	OUT_RING_TABLE(cmdbuf->buf, sz);
L
Linus Torvalds 已提交
2693 2694 2695 2696 2697 2698 2699
	ADVANCE_RING();

	cmdbuf->buf += sz * sizeof(int);
	cmdbuf->bufsz -= sz * sizeof(int);
	return 0;
}

2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
static __inline__ int radeon_emit_veclinear(drm_radeon_private_t *dev_priv,
					  drm_radeon_cmd_header_t header,
					  drm_radeon_kcmd_buffer_t *cmdbuf)
{
	int sz = header.veclinear.count * 4;
	int start = header.veclinear.addr_lo | (header.veclinear.addr_hi << 8);
	RING_LOCALS;

        if (!sz)
                return 0;
        if (sz * 4 > cmdbuf->bufsz)
E
Eric Anholt 已提交
2711
                return -EINVAL;
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725

	BEGIN_RING(5 + sz);
	OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
	OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
	OUT_RING(start | (1 << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
	OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
	OUT_RING_TABLE(cmdbuf->buf, sz);
	ADVANCE_RING();

	cmdbuf->buf += sz * sizeof(int);
	cmdbuf->bufsz -= sz * sizeof(int);
	return 0;
}

2726
static int radeon_emit_packet3(struct drm_device * dev,
2727
			       struct drm_file *file_priv,
2728
			       drm_radeon_kcmd_buffer_t *cmdbuf)
L
Linus Torvalds 已提交
2729 2730 2731 2732 2733 2734 2735 2736
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	unsigned int cmdsz;
	int ret;
	RING_LOCALS;

	DRM_DEBUG("\n");

2737
	if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
D
Dave Airlie 已提交
2738 2739
						  cmdbuf, &cmdsz))) {
		DRM_ERROR("Packet verification failed\n");
L
Linus Torvalds 已提交
2740 2741 2742
		return ret;
	}

D
Dave Airlie 已提交
2743 2744
	BEGIN_RING(cmdsz);
	OUT_RING_TABLE(cmdbuf->buf, cmdsz);
L
Linus Torvalds 已提交
2745 2746 2747 2748 2749 2750 2751
	ADVANCE_RING();

	cmdbuf->buf += cmdsz * 4;
	cmdbuf->bufsz -= cmdsz * 4;
	return 0;
}

2752
static int radeon_emit_packet3_cliprect(struct drm_device *dev,
2753
					struct drm_file *file_priv,
2754
					drm_radeon_kcmd_buffer_t *cmdbuf,
D
Dave Airlie 已提交
2755
					int orig_nbox)
L
Linus Torvalds 已提交
2756 2757
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2758
	struct drm_clip_rect box;
L
Linus Torvalds 已提交
2759 2760
	unsigned int cmdsz;
	int ret;
2761
	struct drm_clip_rect __user *boxes = cmdbuf->boxes;
L
Linus Torvalds 已提交
2762 2763 2764 2765 2766
	int i = 0;
	RING_LOCALS;

	DRM_DEBUG("\n");

2767
	if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
D
Dave Airlie 已提交
2768 2769
						  cmdbuf, &cmdsz))) {
		DRM_ERROR("Packet verification failed\n");
L
Linus Torvalds 已提交
2770 2771 2772 2773 2774 2775 2776
		return ret;
	}

	if (!orig_nbox)
		goto out;

	do {
D
Dave Airlie 已提交
2777 2778
		if (i < cmdbuf->nbox) {
			if (DRM_COPY_FROM_USER(&box, &boxes[i], sizeof(box)))
E
Eric Anholt 已提交
2779
				return -EFAULT;
L
Linus Torvalds 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
			/* FIXME The second and subsequent times round
			 * this loop, send a WAIT_UNTIL_3D_IDLE before
			 * calling emit_clip_rect(). This fixes a
			 * lockup on fast machines when sending
			 * several cliprects with a cmdbuf, as when
			 * waving a 2D window over a 3D
			 * window. Something in the commands from user
			 * space seems to hang the card when they're
			 * sent several times in a row. That would be
			 * the correct place to fix it but this works
			 * around it until I can figure that out - Tim
			 * Smith */
D
Dave Airlie 已提交
2792 2793
			if (i) {
				BEGIN_RING(2);
L
Linus Torvalds 已提交
2794 2795 2796
				RADEON_WAIT_UNTIL_3D_IDLE();
				ADVANCE_RING();
			}
D
Dave Airlie 已提交
2797
			radeon_emit_clip_rect(dev_priv, &box);
L
Linus Torvalds 已提交
2798
		}
D
Dave Airlie 已提交
2799 2800 2801

		BEGIN_RING(cmdsz);
		OUT_RING_TABLE(cmdbuf->buf, cmdsz);
L
Linus Torvalds 已提交
2802 2803
		ADVANCE_RING();

D
Dave Airlie 已提交
2804 2805
	} while (++i < cmdbuf->nbox);
	if (cmdbuf->nbox == 1)
L
Linus Torvalds 已提交
2806 2807
		cmdbuf->nbox = 0;

D
Dave Airlie 已提交
2808
      out:
L
Linus Torvalds 已提交
2809 2810 2811 2812 2813
	cmdbuf->buf += cmdsz * 4;
	cmdbuf->bufsz -= cmdsz * 4;
	return 0;
}

2814
static int radeon_emit_wait(struct drm_device * dev, int flags)
L
Linus Torvalds 已提交
2815 2816 2817 2818
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	RING_LOCALS;

2819
	DRM_DEBUG("%x\n", flags);
L
Linus Torvalds 已提交
2820 2821
	switch (flags) {
	case RADEON_WAIT_2D:
D
Dave Airlie 已提交
2822 2823
		BEGIN_RING(2);
		RADEON_WAIT_UNTIL_2D_IDLE();
L
Linus Torvalds 已提交
2824 2825 2826
		ADVANCE_RING();
		break;
	case RADEON_WAIT_3D:
D
Dave Airlie 已提交
2827 2828
		BEGIN_RING(2);
		RADEON_WAIT_UNTIL_3D_IDLE();
L
Linus Torvalds 已提交
2829 2830
		ADVANCE_RING();
		break;
D
Dave Airlie 已提交
2831 2832 2833
	case RADEON_WAIT_2D | RADEON_WAIT_3D:
		BEGIN_RING(2);
		RADEON_WAIT_UNTIL_IDLE();
L
Linus Torvalds 已提交
2834 2835 2836
		ADVANCE_RING();
		break;
	default:
E
Eric Anholt 已提交
2837
		return -EINVAL;
L
Linus Torvalds 已提交
2838 2839 2840 2841 2842
	}

	return 0;
}

2843
static int radeon_cp_cmdbuf(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
2844 2845
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
2846
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
2847
	struct drm_buf *buf = NULL;
L
Linus Torvalds 已提交
2848
	int idx;
2849
	drm_radeon_kcmd_buffer_t *cmdbuf = data;
L
Linus Torvalds 已提交
2850 2851
	drm_radeon_cmd_header_t header;
	int orig_nbox, orig_bufsz;
D
Dave Airlie 已提交
2852
	char *kbuf = NULL;
L
Linus Torvalds 已提交
2853

2854
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
2855

D
Dave Airlie 已提交
2856 2857
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2858

2859
	if (cmdbuf->bufsz > 64 * 1024 || cmdbuf->bufsz < 0) {
E
Eric Anholt 已提交
2860
		return -EINVAL;
L
Linus Torvalds 已提交
2861 2862 2863 2864 2865 2866
	}

	/* Allocate an in-kernel area and copy in the cmdbuf.  Do this to avoid
	 * races between checking values and using those values in other code,
	 * and simply to avoid a lot of function calls to copy in data.
	 */
2867
	orig_bufsz = cmdbuf->bufsz;
L
Linus Torvalds 已提交
2868
	if (orig_bufsz != 0) {
2869
		kbuf = kmalloc(cmdbuf->bufsz, GFP_KERNEL);
L
Linus Torvalds 已提交
2870
		if (kbuf == NULL)
E
Eric Anholt 已提交
2871
			return -ENOMEM;
2872 2873
		if (DRM_COPY_FROM_USER(kbuf, (void __user *)cmdbuf->buf,
				       cmdbuf->bufsz)) {
2874
			kfree(kbuf);
E
Eric Anholt 已提交
2875
			return -EFAULT;
L
Linus Torvalds 已提交
2876
		}
2877
		cmdbuf->buf = kbuf;
L
Linus Torvalds 已提交
2878 2879
	}

2880
	orig_nbox = cmdbuf->nbox;
L
Linus Torvalds 已提交
2881

D
Dave Airlie 已提交
2882
	if (dev_priv->microcode_version == UCODE_R300) {
D
Dave Airlie 已提交
2883
		int temp;
2884
		temp = r300_do_cp_cmdbuf(dev, file_priv, cmdbuf);
D
Dave Airlie 已提交
2885

D
Dave Airlie 已提交
2886
		if (orig_bufsz != 0)
2887
			kfree(kbuf);
D
Dave Airlie 已提交
2888

D
Dave Airlie 已提交
2889 2890 2891 2892
		return temp;
	}

	/* microcode_version != r300 */
2893
	while (cmdbuf->bufsz >= sizeof(header)) {
L
Linus Torvalds 已提交
2894

2895 2896 2897
		header.i = *(int *)cmdbuf->buf;
		cmdbuf->buf += sizeof(header);
		cmdbuf->bufsz -= sizeof(header);
L
Linus Torvalds 已提交
2898 2899

		switch (header.header.cmd_type) {
D
Dave Airlie 已提交
2900
		case RADEON_CMD_PACKET:
L
Linus Torvalds 已提交
2901
			DRM_DEBUG("RADEON_CMD_PACKET\n");
D
Dave Airlie 已提交
2902
			if (radeon_emit_packets
2903
			    (dev_priv, file_priv, header, cmdbuf)) {
L
Linus Torvalds 已提交
2904 2905 2906 2907 2908 2909 2910
				DRM_ERROR("radeon_emit_packets failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_SCALARS:
			DRM_DEBUG("RADEON_CMD_SCALARS\n");
2911
			if (radeon_emit_scalars(dev_priv, header, cmdbuf)) {
L
Linus Torvalds 已提交
2912 2913 2914 2915 2916 2917 2918
				DRM_ERROR("radeon_emit_scalars failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_VECTORS:
			DRM_DEBUG("RADEON_CMD_VECTORS\n");
2919
			if (radeon_emit_vectors(dev_priv, header, cmdbuf)) {
L
Linus Torvalds 已提交
2920 2921 2922 2923 2924 2925 2926 2927
				DRM_ERROR("radeon_emit_vectors failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_DMA_DISCARD:
			DRM_DEBUG("RADEON_CMD_DMA_DISCARD\n");
			idx = header.dma.buf_idx;
D
Dave Airlie 已提交
2928 2929 2930
			if (idx < 0 || idx >= dma->buf_count) {
				DRM_ERROR("buffer index %d (of %d max)\n",
					  idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
2931 2932 2933 2934
				goto err;
			}

			buf = dma->buflist[idx];
2935
			if (buf->file_priv != file_priv || buf->pending) {
D
Dave Airlie 已提交
2936
				DRM_ERROR("bad buffer %p %p %d\n",
2937 2938
					  buf->file_priv, file_priv,
					  buf->pending);
L
Linus Torvalds 已提交
2939 2940 2941
				goto err;
			}

2942
			radeon_cp_discard_buffer(dev, file_priv->master, buf);
L
Linus Torvalds 已提交
2943 2944 2945 2946
			break;

		case RADEON_CMD_PACKET3:
			DRM_DEBUG("RADEON_CMD_PACKET3\n");
2947
			if (radeon_emit_packet3(dev, file_priv, cmdbuf)) {
L
Linus Torvalds 已提交
2948 2949 2950 2951 2952 2953 2954
				DRM_ERROR("radeon_emit_packet3 failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_PACKET3_CLIP:
			DRM_DEBUG("RADEON_CMD_PACKET3_CLIP\n");
D
Dave Airlie 已提交
2955
			if (radeon_emit_packet3_cliprect
2956
			    (dev, file_priv, cmdbuf, orig_nbox)) {
L
Linus Torvalds 已提交
2957 2958 2959 2960 2961 2962 2963
				DRM_ERROR("radeon_emit_packet3_clip failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_SCALARS2:
			DRM_DEBUG("RADEON_CMD_SCALARS2\n");
2964
			if (radeon_emit_scalars2(dev_priv, header, cmdbuf)) {
L
Linus Torvalds 已提交
2965 2966 2967 2968 2969 2970 2971
				DRM_ERROR("radeon_emit_scalars2 failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_WAIT:
			DRM_DEBUG("RADEON_CMD_WAIT\n");
D
Dave Airlie 已提交
2972
			if (radeon_emit_wait(dev, header.wait.flags)) {
L
Linus Torvalds 已提交
2973 2974 2975 2976
				DRM_ERROR("radeon_emit_wait failed\n");
				goto err;
			}
			break;
2977 2978
		case RADEON_CMD_VECLINEAR:
			DRM_DEBUG("RADEON_CMD_VECLINEAR\n");
2979
			if (radeon_emit_veclinear(dev_priv, header, cmdbuf)) {
2980 2981 2982 2983 2984
				DRM_ERROR("radeon_emit_veclinear failed\n");
				goto err;
			}
			break;

L
Linus Torvalds 已提交
2985
		default:
D
Dave Airlie 已提交
2986
			DRM_ERROR("bad cmd_type %d at %p\n",
L
Linus Torvalds 已提交
2987
				  header.header.cmd_type,
2988
				  cmdbuf->buf - sizeof(header));
L
Linus Torvalds 已提交
2989 2990 2991 2992 2993
			goto err;
		}
	}

	if (orig_bufsz != 0)
2994
		kfree(kbuf);
L
Linus Torvalds 已提交
2995 2996 2997 2998 2999

	DRM_DEBUG("DONE\n");
	COMMIT_RING();
	return 0;

D
Dave Airlie 已提交
3000
      err:
L
Linus Torvalds 已提交
3001
	if (orig_bufsz != 0)
3002
		kfree(kbuf);
E
Eric Anholt 已提交
3003
	return -EINVAL;
L
Linus Torvalds 已提交
3004 3005
}

3006
static int radeon_cp_getparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
L
Linus Torvalds 已提交
3007 3008
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
3009
	drm_radeon_getparam_t *param = data;
L
Linus Torvalds 已提交
3010 3011
	int value;

D
Dave Airlie 已提交
3012
	DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
L
Linus Torvalds 已提交
3013

3014
	switch (param->param) {
L
Linus Torvalds 已提交
3015 3016 3017 3018 3019
	case RADEON_PARAM_GART_BUFFER_OFFSET:
		value = dev_priv->gart_buffers_offset;
		break;
	case RADEON_PARAM_LAST_FRAME:
		dev_priv->stats.last_frame_reads++;
3020
		value = GET_SCRATCH(dev_priv, 0);
L
Linus Torvalds 已提交
3021 3022
		break;
	case RADEON_PARAM_LAST_DISPATCH:
3023
		value = GET_SCRATCH(dev_priv, 1);
L
Linus Torvalds 已提交
3024 3025 3026
		break;
	case RADEON_PARAM_LAST_CLEAR:
		dev_priv->stats.last_clear_reads++;
3027
		value = GET_SCRATCH(dev_priv, 2);
L
Linus Torvalds 已提交
3028 3029
		break;
	case RADEON_PARAM_IRQ_NR:
J
Jesse Barnes 已提交
3030
		value = drm_dev_to_irq(dev);
L
Linus Torvalds 已提交
3031 3032 3033 3034 3035
		break;
	case RADEON_PARAM_GART_BASE:
		value = dev_priv->gart_vm_start;
		break;
	case RADEON_PARAM_REGISTER_HANDLE:
3036
		value = dev_priv->mmio->offset;
L
Linus Torvalds 已提交
3037 3038 3039 3040 3041
		break;
	case RADEON_PARAM_STATUS_HANDLE:
		value = dev_priv->ring_rptr_offset;
		break;
#if BITS_PER_LONG == 32
D
Dave Airlie 已提交
3042 3043 3044
		/*
		 * This ioctl() doesn't work on 64-bit platforms because hw_lock is a
		 * pointer which can't fit into an int-sized variable.  According to
3045
		 * Michel Dänzer, the ioctl() is only used on embedded platforms, so
D
Dave Airlie 已提交
3046 3047 3048 3049 3050
		 * not supporting it shouldn't be a problem.  If the same functionality
		 * is needed on 64-bit platforms, a new ioctl() would have to be added,
		 * so backwards-compatibility for the embedded platforms can be
		 * maintained.  --davidm 4-Feb-2004.
		 */
L
Linus Torvalds 已提交
3051 3052
	case RADEON_PARAM_SAREA_HANDLE:
		/* The lock is the first dword in the sarea. */
3053
		/* no users of this parameter */
L
Linus Torvalds 已提交
3054 3055 3056 3057 3058
		break;
#endif
	case RADEON_PARAM_GART_TEX_HANDLE:
		value = dev_priv->gart_textures_offset;
		break;
3059 3060
	case RADEON_PARAM_SCRATCH_OFFSET:
		if (!dev_priv->writeback_works)
E
Eric Anholt 已提交
3061
			return -EINVAL;
3062 3063 3064 3065
		if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
			value = R600_SCRATCH_REG_OFFSET;
		else
			value = RADEON_SCRATCH_REG_OFFSET;
3066
		break;
3067
	case RADEON_PARAM_CARD_TYPE:
3068
		if (dev_priv->flags & RADEON_IS_PCIE)
3069
			value = RADEON_CARD_PCIE;
3070
		else if (dev_priv->flags & RADEON_IS_AGP)
3071 3072 3073 3074
			value = RADEON_CARD_AGP;
		else
			value = RADEON_CARD_PCI;
		break;
3075 3076 3077
	case RADEON_PARAM_VBLANK_CRTC:
		value = radeon_vblank_crtc_get(dev);
		break;
D
Dave Airlie 已提交
3078 3079 3080
	case RADEON_PARAM_FB_LOCATION:
		value = radeon_read_fb_location(dev_priv);
		break;
3081 3082 3083
	case RADEON_PARAM_NUM_GB_PIPES:
		value = dev_priv->num_gb_pipes;
		break;
3084 3085 3086
	case RADEON_PARAM_NUM_Z_PIPES:
		value = dev_priv->num_z_pipes;
		break;
L
Linus Torvalds 已提交
3087
	default:
3088
		DRM_DEBUG("Invalid parameter %d\n", param->param);
E
Eric Anholt 已提交
3089
		return -EINVAL;
L
Linus Torvalds 已提交
3090 3091
	}

3092
	if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
D
Dave Airlie 已提交
3093
		DRM_ERROR("copy_to_user\n");
E
Eric Anholt 已提交
3094
		return -EFAULT;
L
Linus Torvalds 已提交
3095
	}
D
Dave Airlie 已提交
3096

L
Linus Torvalds 已提交
3097 3098 3099
	return 0;
}

3100
static int radeon_cp_setparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
3101
{
L
Linus Torvalds 已提交
3102
	drm_radeon_private_t *dev_priv = dev->dev_private;
3103
	struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
3104
	drm_radeon_setparam_t *sp = data;
L
Linus Torvalds 已提交
3105 3106
	struct drm_radeon_driver_file_fields *radeon_priv;

3107
	switch (sp->param) {
L
Linus Torvalds 已提交
3108
	case RADEON_SETPARAM_FB_LOCATION:
3109
		radeon_priv = file_priv->driver_priv;
3110 3111
		radeon_priv->radeon_fb_delta = dev_priv->fb_location -
		    sp->value;
L
Linus Torvalds 已提交
3112 3113
		break;
	case RADEON_SETPARAM_SWITCH_TILING:
3114
		if (sp->value == 0) {
D
Dave Airlie 已提交
3115
			DRM_DEBUG("color tiling disabled\n");
L
Linus Torvalds 已提交
3116 3117
			dev_priv->front_pitch_offset &= ~RADEON_DST_TILE_MACRO;
			dev_priv->back_pitch_offset &= ~RADEON_DST_TILE_MACRO;
3118 3119
			if (master_priv->sarea_priv)
				master_priv->sarea_priv->tiling_enabled = 0;
3120
		} else if (sp->value == 1) {
D
Dave Airlie 已提交
3121
			DRM_DEBUG("color tiling enabled\n");
L
Linus Torvalds 已提交
3122 3123
			dev_priv->front_pitch_offset |= RADEON_DST_TILE_MACRO;
			dev_priv->back_pitch_offset |= RADEON_DST_TILE_MACRO;
3124 3125
			if (master_priv->sarea_priv)
				master_priv->sarea_priv->tiling_enabled = 1;
L
Linus Torvalds 已提交
3126
		}
D
Dave Airlie 已提交
3127
		break;
3128
	case RADEON_SETPARAM_PCIGART_LOCATION:
3129
		dev_priv->pcigart_offset = sp->value;
3130
		dev_priv->pcigart_offset_set = 1;
3131
		break;
3132
	case RADEON_SETPARAM_NEW_MEMMAP:
3133
		dev_priv->new_memmap = sp->value;
3134
		break;
3135
	case RADEON_SETPARAM_PCIGART_TABLE_SIZE:
3136
		dev_priv->gart_info.table_size = sp->value;
3137 3138 3139
		if (dev_priv->gart_info.table_size < RADEON_PCIGART_TABLE_SIZE)
			dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
		break;
3140
	case RADEON_SETPARAM_VBLANK_CRTC:
3141
		return radeon_vblank_crtc_set(dev, sp->value);
3142
		break;
L
Linus Torvalds 已提交
3143
	default:
3144
		DRM_DEBUG("Invalid parameter %d\n", sp->param);
E
Eric Anholt 已提交
3145
		return -EINVAL;
L
Linus Torvalds 已提交
3146 3147 3148 3149 3150 3151 3152 3153
	}

	return 0;
}

/* When a client dies:
 *    - Check for and clean up flipped page state
 *    - Free any alloced GART memory.
3154
 *    - Free any alloced radeon surfaces.
L
Linus Torvalds 已提交
3155 3156 3157
 *
 * DRM infrastructure takes care of reclaiming dma buffers.
 */
3158
void radeon_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
L
Linus Torvalds 已提交
3159
{
D
Dave Airlie 已提交
3160 3161
	if (dev->dev_private) {
		drm_radeon_private_t *dev_priv = dev->dev_private;
3162
		dev_priv->page_flipping = 0;
3163 3164 3165
		radeon_mem_release(file_priv, dev_priv->gart_heap);
		radeon_mem_release(file_priv, dev_priv->fb_heap);
		radeon_surfaces_release(file_priv, dev_priv);
D
Dave Airlie 已提交
3166
	}
L
Linus Torvalds 已提交
3167 3168
}

3169
void radeon_driver_lastclose(struct drm_device *dev)
L
Linus Torvalds 已提交
3170
{
3171
	radeon_surfaces_release(PCIGART_FILE_PRIV, dev->dev_private);
L
Linus Torvalds 已提交
3172 3173 3174
	radeon_do_release(dev);
}

3175
int radeon_driver_open(struct drm_device *dev, struct drm_file *file_priv)
L
Linus Torvalds 已提交
3176 3177 3178
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	struct drm_radeon_driver_file_fields *radeon_priv;
D
Dave Airlie 已提交
3179

3180
	DRM_DEBUG("\n");
3181
	radeon_priv = kmalloc(sizeof(*radeon_priv), GFP_KERNEL);
D
Dave Airlie 已提交
3182

L
Linus Torvalds 已提交
3183 3184 3185
	if (!radeon_priv)
		return -ENOMEM;

3186
	file_priv->driver_priv = radeon_priv;
3187

D
Dave Airlie 已提交
3188
	if (dev_priv)
L
Linus Torvalds 已提交
3189 3190 3191 3192 3193 3194
		radeon_priv->radeon_fb_delta = dev_priv->fb_location;
	else
		radeon_priv->radeon_fb_delta = 0;
	return 0;
}

3195
void radeon_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
L
Linus Torvalds 已提交
3196
{
D
Dave Airlie 已提交
3197
	struct drm_radeon_driver_file_fields *radeon_priv =
3198
	    file_priv->driver_priv;
D
Dave Airlie 已提交
3199

3200
	kfree(radeon_priv);
L
Linus Torvalds 已提交
3201 3202
}

3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230
struct drm_ioctl_desc radeon_ioctls[] = {
	DRM_IOCTL_DEF(DRM_RADEON_CP_INIT, radeon_cp_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_RADEON_CP_START, radeon_cp_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_RADEON_CP_STOP, radeon_cp_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_RADEON_CP_RESET, radeon_cp_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_RADEON_CP_IDLE, radeon_cp_idle, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_CP_RESUME, radeon_cp_resume, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_RESET, radeon_engine_reset, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_FULLSCREEN, radeon_fullscreen, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_SWAP, radeon_cp_swap, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_CLEAR, radeon_cp_clear, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_VERTEX, radeon_cp_vertex, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_INDICES, radeon_cp_indices, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_TEXTURE, radeon_cp_texture, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_STIPPLE, radeon_cp_stipple, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_INDIRECT, radeon_cp_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_RADEON_VERTEX2, radeon_cp_vertex2, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_CMDBUF, radeon_cp_cmdbuf, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_GETPARAM, radeon_cp_getparam, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_FLIP, radeon_cp_flip, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_ALLOC, radeon_mem_alloc, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_FREE, radeon_mem_free, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_INIT_HEAP, radeon_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF(DRM_RADEON_IRQ_EMIT, radeon_irq_emit, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_IRQ_WAIT, radeon_irq_wait, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_SETPARAM, radeon_cp_setparam, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_SURF_ALLOC, radeon_surface_alloc, DRM_AUTH),
	DRM_IOCTL_DEF(DRM_RADEON_SURF_FREE, radeon_surface_free, DRM_AUTH)
L
Linus Torvalds 已提交
3231 3232 3233
};

int radeon_max_ioctl = DRM_ARRAY_SIZE(radeon_ioctls);