radeon_state.c 86.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* radeon_state.c -- State support for Radeon -*- linux-c -*-
 *
 * 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 42 43 44
static __inline__ int radeon_check_and_fixup_offset(drm_radeon_private_t *
						    dev_priv,
						    drm_file_t * filp_priv,
						    u32 * offset)
{
L
Linus Torvalds 已提交
45 46 47
	u32 off = *offset;
	struct drm_radeon_driver_file_fields *radeon_priv;

D
Dave Airlie 已提交
48 49
	if (off >= dev_priv->fb_location &&
	    off < (dev_priv->gart_vm_start + dev_priv->gart_size))
L
Linus Torvalds 已提交
50 51 52 53 54
		return 0;

	radeon_priv = filp_priv->driver_priv;
	off += radeon_priv->radeon_fb_delta;

D
Dave Airlie 已提交
55
	DRM_DEBUG("offset fixed up to 0x%x\n", off);
L
Linus Torvalds 已提交
56

D
Dave Airlie 已提交
57 58 59
	if (off < dev_priv->fb_location ||
	    off >= (dev_priv->gart_vm_start + dev_priv->gart_size))
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
60 61 62 63 64 65

	*offset = off;

	return 0;
}

D
Dave Airlie 已提交
66 67 68 69 70 71
static __inline__ int radeon_check_and_fixup_packets(drm_radeon_private_t *
						     dev_priv,
						     drm_file_t * filp_priv,
						     int id, u32 __user * data)
{
	switch (id) {
L
Linus Torvalds 已提交
72 73

	case RADEON_EMIT_PP_MISC:
D
Dave Airlie 已提交
74 75 76 77 78 79 80
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &data[(RADEON_RB3D_DEPTHOFFSET
							 -
							 RADEON_PP_MISC) /
							4])) {
			DRM_ERROR("Invalid depth buffer offset\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
81 82 83 84
		}
		break;

	case RADEON_EMIT_PP_CNTL:
D
Dave Airlie 已提交
85 86 87 88 89 90 91
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &data[(RADEON_RB3D_COLOROFFSET
							 -
							 RADEON_PP_CNTL) /
							4])) {
			DRM_ERROR("Invalid colour buffer offset\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100
		}
		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:
D
Dave Airlie 已提交
101 102 103 104
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &data[0])) {
			DRM_ERROR("Invalid R200 texture offset\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
105 106 107 108 109 110
		}
		break;

	case RADEON_EMIT_PP_TXFILTER_0:
	case RADEON_EMIT_PP_TXFILTER_1:
	case RADEON_EMIT_PP_TXFILTER_2:
D
Dave Airlie 已提交
111 112 113 114 115 116 117
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &data[(RADEON_PP_TXOFFSET_0
							 -
							 RADEON_PP_TXFILTER_0) /
							4])) {
			DRM_ERROR("Invalid R100 texture offset\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125
		}
		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 已提交
126 127 128 129 130 131 132 133 134
	case R200_EMIT_PP_CUBIC_OFFSETS_5:{
			int i;
			for (i = 0; i < 5; i++) {
				if (radeon_check_and_fixup_offset
				    (dev_priv, filp_priv, &data[i])) {
					DRM_ERROR
					    ("Invalid R200 cubic texture offset\n");
					return DRM_ERR(EINVAL);
				}
L
Linus Torvalds 已提交
135
			}
D
Dave Airlie 已提交
136
			break;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		}

	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,
								  filp_priv,
								  &data[i])) {
					DRM_ERROR
					    ("Invalid R100 cubic texture offset\n");
					return DRM_ERR(EINVAL);
				}
			}
		}
		break;

	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_VAP_CTL:
	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 已提交
220 221 222 223 224 225 226 227 228
	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:
L
Linus Torvalds 已提交
229 230 231 232
		/* These packets don't contain memory offsets */
		break;

	default:
D
Dave Airlie 已提交
233 234
		DRM_ERROR("Unknown state packet ID %d\n", id);
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
235 236 237 238 239
	}

	return 0;
}

D
Dave Airlie 已提交
240 241 242 243 244 245 246
static __inline__ int radeon_check_and_fixup_packet3(drm_radeon_private_t *
						     dev_priv,
						     drm_file_t * filp_priv,
						     drm_radeon_cmd_buffer_t *
						     cmdbuf,
						     unsigned int *cmdsz)
{
L
Linus Torvalds 已提交
247 248
	u32 *cmd = (u32 *) cmdbuf->buf;

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

D
Dave Airlie 已提交
251 252 253
	if ((cmd[0] & 0xc0000000) != RADEON_CP_PACKET3) {
		DRM_ERROR("Not a type 3 packet\n");
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
254 255
	}

D
Dave Airlie 已提交
256 257 258
	if (4 * *cmdsz > cmdbuf->bufsz) {
		DRM_ERROR("Packet size larger than size of data provided\n");
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
259 260 261
	}

	/* Check client state and fix it up if necessary */
D
Dave Airlie 已提交
262
	if (cmd[0] & 0x8000) {	/* MSB of opcode: next DWORD GUI_CNTL */
L
Linus Torvalds 已提交
263 264
		u32 offset;

D
Dave Airlie 已提交
265 266
		if (cmd[1] & (RADEON_GMC_SRC_PITCH_OFFSET_CNTL
			      | RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
L
Linus Torvalds 已提交
267
			offset = cmd[2] << 10;
D
Dave Airlie 已提交
268 269 270 271
			if (radeon_check_and_fixup_offset
			    (dev_priv, filp_priv, &offset)) {
				DRM_ERROR("Invalid first packet offset\n");
				return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
272
			}
D
Dave Airlie 已提交
273
			cmd[2] = (cmd[2] & 0xffc00000) | offset >> 10;
L
Linus Torvalds 已提交
274 275
		}

D
Dave Airlie 已提交
276 277
		if ((cmd[1] & RADEON_GMC_SRC_PITCH_OFFSET_CNTL) &&
		    (cmd[1] & RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
L
Linus Torvalds 已提交
278
			offset = cmd[3] << 10;
D
Dave Airlie 已提交
279 280 281 282
			if (radeon_check_and_fixup_offset
			    (dev_priv, filp_priv, &offset)) {
				DRM_ERROR("Invalid second packet offset\n");
				return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
283
			}
D
Dave Airlie 已提交
284
			cmd[3] = (cmd[3] & 0xffc00000) | offset >> 10;
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294
		}
	}

	return 0;
}

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

D
Dave Airlie 已提交
295 296
static __inline__ void radeon_emit_clip_rect(drm_radeon_private_t * dev_priv,
					     drm_clip_rect_t * box)
L
Linus Torvalds 已提交
297 298 299
{
	RING_LOCALS;

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

D
Dave Airlie 已提交
303 304 305 306 307
	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 已提交
308 309 310 311 312
	ADVANCE_RING();
}

/* Emit 1.1 state
 */
D
Dave Airlie 已提交
313 314 315 316 317
static int radeon_emit_state(drm_radeon_private_t * dev_priv,
			     drm_file_t * filp_priv,
			     drm_radeon_context_regs_t * ctx,
			     drm_radeon_texture_regs_t * tex,
			     unsigned int dirty)
L
Linus Torvalds 已提交
318 319
{
	RING_LOCALS;
D
Dave Airlie 已提交
320
	DRM_DEBUG("dirty=0x%08x\n", dirty);
L
Linus Torvalds 已提交
321

D
Dave Airlie 已提交
322 323 324 325 326
	if (dirty & RADEON_UPLOAD_CONTEXT) {
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &ctx->rb3d_depthoffset)) {
			DRM_ERROR("Invalid depth buffer offset\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
327 328
		}

D
Dave Airlie 已提交
329 330 331 332
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &ctx->rb3d_coloroffset)) {
			DRM_ERROR("Invalid depth buffer offset\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
333 334
		}

D
Dave Airlie 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		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 已提交
350 351 352
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
353 354 355 356
	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 已提交
357 358 359
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
360 361 362 363 364 365 366
	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 已提交
367 368 369
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
370 371 372 373 374 375 376
	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 已提交
377 378 379
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
380 381 382 383 384 385
	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 已提交
386 387 388
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
389 390 391 392 393 394 395 396 397
	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 已提交
398 399 400
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
401 402 403 404 405 406
	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 已提交
407 408 409
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
410 411 412 413
	if (dirty & RADEON_UPLOAD_MISC) {
		BEGIN_RING(2);
		OUT_RING(CP_PACKET0(RADEON_RE_MISC, 0));
		OUT_RING(ctx->re_misc);
L
Linus Torvalds 已提交
414 415 416
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
417 418 419 420 421
	if (dirty & RADEON_UPLOAD_TEX0) {
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &tex[0].pp_txoffset)) {
			DRM_ERROR("Invalid texture offset for unit 0\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
422 423
		}

D
Dave Airlie 已提交
424 425 426 427 428 429 430 431 432 433
		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 已提交
434 435 436
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
437 438 439 440 441
	if (dirty & RADEON_UPLOAD_TEX1) {
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &tex[1].pp_txoffset)) {
			DRM_ERROR("Invalid texture offset for unit 1\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
442 443
		}

D
Dave Airlie 已提交
444 445 446 447 448 449 450 451 452 453
		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 已提交
454 455 456
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
457 458 459 460 461
	if (dirty & RADEON_UPLOAD_TEX2) {
		if (radeon_check_and_fixup_offset(dev_priv, filp_priv,
						  &tex[2].pp_txoffset)) {
			DRM_ERROR("Invalid texture offset for unit 2\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
462 463
		}

D
Dave Airlie 已提交
464 465 466 467 468 469 470 471 472 473
		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 已提交
474 475 476 477 478 479 480 481
		ADVANCE_RING();
	}

	return 0;
}

/* Emit 1.2 state
 */
D
Dave Airlie 已提交
482 483 484
static int radeon_emit_state2(drm_radeon_private_t * dev_priv,
			      drm_file_t * filp_priv,
			      drm_radeon_state_t * state)
L
Linus Torvalds 已提交
485 486 487 488
{
	RING_LOCALS;

	if (state->dirty & RADEON_UPLOAD_ZBIAS) {
D
Dave Airlie 已提交
489 490 491 492
		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 已提交
493 494 495
		ADVANCE_RING();
	}

D
Dave Airlie 已提交
496 497
	return radeon_emit_state(dev_priv, filp_priv, &state->context,
				 state->tex, state->dirty);
L
Linus Torvalds 已提交
498 499 500 501
}

/* 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 已提交
502
 * the tcl scalar and vector areas.
L
Linus Torvalds 已提交
503
 */
D
Dave Airlie 已提交
504 505 506
static struct {
	int start;
	int len;
L
Linus Torvalds 已提交
507 508
	const char *name;
} packet[RADEON_MAX_STATE_PACKETS] = {
D
Dave Airlie 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	{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"},
	{R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1, "R200_SE_TCL_OUTPUT_VTX_COMP_SEL"},
	{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 */
	{R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0"},	/* 62 */
	{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"},
	{R200_PP_AFS_0, 32, "R200_PP_AFS_0"},	/* 85 */
	{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"},
L
Linus Torvalds 已提交
605 606 607 608 609 610
};

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

D
Dave Airlie 已提交
611 612
static void radeon_clear_box(drm_radeon_private_t * dev_priv,
			     int x, int y, int w, int h, int r, int g, int b)
L
Linus Torvalds 已提交
613 614 615 616 617 618 619
{
	u32 color;
	RING_LOCALS;

	x += dev_priv->sarea_priv->boxes[0].x1;
	y += dev_priv->sarea_priv->boxes[0].y1;

D
Dave Airlie 已提交
620
	switch (dev_priv->color_fmt) {
L
Linus Torvalds 已提交
621 622
	case RADEON_COLOR_FORMAT_RGB565:
		color = (((r & 0xf8) << 8) |
D
Dave Airlie 已提交
623
			 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
L
Linus Torvalds 已提交
624 625 626
		break;
	case RADEON_COLOR_FORMAT_ARGB8888:
	default:
D
Dave Airlie 已提交
627
		color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
L
Linus Torvalds 已提交
628 629 630
		break;
	}

D
Dave Airlie 已提交
631 632 633 634
	BEGIN_RING(4);
	RADEON_WAIT_UNTIL_3D_IDLE();
	OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
	OUT_RING(0xffffffff);
L
Linus Torvalds 已提交
635 636
	ADVANCE_RING();

D
Dave Airlie 已提交
637
	BEGIN_RING(6);
L
Linus Torvalds 已提交
638

D
Dave Airlie 已提交
639 640 641 642 643 644
	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 已提交
645

D
Dave Airlie 已提交
646 647 648 649 650
	if (dev_priv->page_flipping && dev_priv->current_page == 1) {
		OUT_RING(dev_priv->front_pitch_offset);
	} else {
		OUT_RING(dev_priv->back_pitch_offset);
	}
L
Linus Torvalds 已提交
651

D
Dave Airlie 已提交
652
	OUT_RING(color);
L
Linus Torvalds 已提交
653

D
Dave Airlie 已提交
654 655
	OUT_RING((x << 16) | y);
	OUT_RING((w << 16) | h);
L
Linus Torvalds 已提交
656 657 658 659

	ADVANCE_RING();
}

D
Dave Airlie 已提交
660
static void radeon_cp_performance_boxes(drm_radeon_private_t * dev_priv)
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
{
	/* 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 已提交
676 677
	if (dev_priv->stats.boxes & RADEON_BOX_FLIP)
		radeon_clear_box(dev_priv, 4, 4, 8, 8, 255, 0, 255);
L
Linus Torvalds 已提交
678 679 680

	/* Red box if we have to wait for idle at any point
	 */
D
Dave Airlie 已提交
681 682
	if (dev_priv->stats.boxes & RADEON_BOX_WAIT_IDLE)
		radeon_clear_box(dev_priv, 16, 4, 8, 8, 255, 0, 0);
L
Linus Torvalds 已提交
683 684 685 686 687 688

	/* Blue box: lost context?
	 */

	/* Yellow box for texture swaps
	 */
D
Dave Airlie 已提交
689 690
	if (dev_priv->stats.boxes & RADEON_BOX_TEXTURE_LOAD)
		radeon_clear_box(dev_priv, 40, 4, 8, 8, 255, 255, 0);
L
Linus Torvalds 已提交
691 692 693

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

D
Dave Airlie 已提交
697
	/* Draw bars indicating number of buffers allocated
L
Linus Torvalds 已提交
698 699 700 701 702 703
	 * (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;

D
Dave Airlie 已提交
704 705 706
		radeon_clear_box(dev_priv, 4, 16,
				 dev_priv->stats.requested_bufs, 4,
				 196, 128, 128);
L
Linus Torvalds 已提交
707 708
	}

D
Dave Airlie 已提交
709
	memset(&dev_priv->stats, 0, sizeof(dev_priv->stats));
L
Linus Torvalds 已提交
710 711

}
D
Dave Airlie 已提交
712

L
Linus Torvalds 已提交
713 714 715 716
/* ================================================================
 * CP command dispatch functions
 */

D
Dave Airlie 已提交
717 718 719
static void radeon_cp_dispatch_clear(drm_device_t * dev,
				     drm_radeon_clear_t * clear,
				     drm_radeon_clear_rect_t * depth_boxes)
L
Linus Torvalds 已提交
720 721 722 723 724 725 726
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	drm_radeon_depth_clear_t *depth_clear = &dev_priv->depth_clear;
	int nbox = sarea_priv->nbox;
	drm_clip_rect_t *pbox = sarea_priv->boxes;
	unsigned int flags = clear->flags;
D
Dave Airlie 已提交
727
	u32 rb3d_cntl = 0, rb3d_stencilrefmask = 0;
L
Linus Torvalds 已提交
728 729
	int i;
	RING_LOCALS;
D
Dave Airlie 已提交
730
	DRM_DEBUG("flags = 0x%x\n", flags);
L
Linus Torvalds 已提交
731 732 733

	dev_priv->stats.clears++;

D
Dave Airlie 已提交
734
	if (dev_priv->page_flipping && dev_priv->current_page == 1) {
L
Linus Torvalds 已提交
735 736 737
		unsigned int tmp = flags;

		flags &= ~(RADEON_FRONT | RADEON_BACK);
D
Dave Airlie 已提交
738 739 740 741
		if (tmp & RADEON_FRONT)
			flags |= RADEON_BACK;
		if (tmp & RADEON_BACK)
			flags |= RADEON_FRONT;
L
Linus Torvalds 已提交
742 743
	}

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

D
Dave Airlie 已提交
746
		BEGIN_RING(4);
L
Linus Torvalds 已提交
747 748 749 750 751

		/* 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 已提交
752 753 754

		OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
		OUT_RING(clear->color_mask);
L
Linus Torvalds 已提交
755 756 757 758 759 760 761

		ADVANCE_RING();

		/* Make sure we restore the 3D state next time.
		 */
		dev_priv->sarea_priv->ctx_owner = 0;

D
Dave Airlie 已提交
762
		for (i = 0; i < nbox; i++) {
L
Linus Torvalds 已提交
763 764 765 766 767
			int x = pbox[i].x1;
			int y = pbox[i].y1;
			int w = pbox[i].x2 - x;
			int h = pbox[i].y2 - y;

D
Dave Airlie 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
			DRM_DEBUG("dispatch clear %d,%d-%d,%d flags 0x%x\n",
				  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 已提交
790 791
				ADVANCE_RING();
			}
D
Dave Airlie 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

			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 已提交
811 812 813 814 815

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

L
Linus Torvalds 已提交
817 818
	/* hyper z clear */
	/* no docs available, based on reverse engeneering by Stephane Marchesin */
D
Dave Airlie 已提交
819 820
	if ((flags & (RADEON_DEPTH | RADEON_STENCIL))
	    && (flags & RADEON_CLEAR_FASTZ)) {
L
Linus Torvalds 已提交
821 822

		int i;
D
Dave Airlie 已提交
823 824 825 826 827 828
		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 已提交
829 830 831
		u32 clearmask;

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

L
Linus Torvalds 已提交
834 835 836 837 838
		/* Make sure we restore the 3D state next time.
		 * we haven't touched any "normal" state - still need this?
		 */
		dev_priv->sarea_priv->ctx_owner = 0;

D
Dave Airlie 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
		if ((dev_priv->flags & CHIP_HAS_HIERZ)
		    && (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 已提交
867 868 869
			clearmask = 0x0;
		}

D
Dave Airlie 已提交
870
		BEGIN_RING(8);
L
Linus Torvalds 已提交
871
		RADEON_WAIT_UNTIL_2D_IDLE();
D
Dave Airlie 已提交
872 873
		OUT_RING_REG(RADEON_RB3D_DEPTHCLEARVALUE,
			     tempRB3D_DEPTHCLEARVALUE);
L
Linus Torvalds 已提交
874
		/* what offset is this exactly ? */
D
Dave Airlie 已提交
875
		OUT_RING_REG(RADEON_RB3D_ZMASKOFFSET, 0);
L
Linus Torvalds 已提交
876
		/* need ctlstat, otherwise get some strange black flickering */
D
Dave Airlie 已提交
877 878
		OUT_RING_REG(RADEON_RB3D_ZCACHE_CTLSTAT,
			     RADEON_RB3D_ZC_FLUSH_ALL);
L
Linus Torvalds 已提交
879 880 881 882 883
		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? */
D
Dave Airlie 已提交
884 885
			if ((dev_priv->flags & CHIP_HAS_HIERZ)
			    && !(dev_priv->microcode_version == UCODE_R200)) {
L
Linus Torvalds 已提交
886 887 888 889 890 891 892
				/* 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 已提交
893 894 895 896 897 898 899 900
				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 已提交
901
				for (j = 0; j <= nrtilesy; j++) {
D
Dave Airlie 已提交
902 903 904
					BEGIN_RING(4);
					OUT_RING(CP_PACKET3
						 (RADEON_3D_CLEAR_ZMASK, 2));
L
Linus Torvalds 已提交
905
					/* first tile */
D
Dave Airlie 已提交
906
					OUT_RING(tileoffset * 8);
L
Linus Torvalds 已提交
907
					/* the number of tiles to clear */
D
Dave Airlie 已提交
908
					OUT_RING(nrtilesx + 4);
L
Linus Torvalds 已提交
909
					/* clear mask : chooses the clearing pattern. */
D
Dave Airlie 已提交
910
					OUT_RING(clearmask);
L
Linus Torvalds 已提交
911 912 913
					ADVANCE_RING();
					tileoffset += depthpixperline >> 6;
				}
D
Dave Airlie 已提交
914
			} else if (dev_priv->microcode_version == UCODE_R200) {
L
Linus Torvalds 已提交
915 916
				/* works for rv250. */
				/* find first macro tile (8x2 4x4 z-pixels on rv250) */
D
Dave Airlie 已提交
917 918 919 920 921 922 923
				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 已提交
924
				for (j = 0; j <= nrtilesy; j++) {
D
Dave Airlie 已提交
925 926 927
					BEGIN_RING(4);
					OUT_RING(CP_PACKET3
						 (RADEON_3D_CLEAR_ZMASK, 2));
L
Linus Torvalds 已提交
928 929 930 931 932
					/* 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 已提交
933
					OUT_RING(tileoffset * 16);
L
Linus Torvalds 已提交
934
					/* the number of tiles to clear */
D
Dave Airlie 已提交
935
					OUT_RING(nrtilesx + 1);
L
Linus Torvalds 已提交
936
					/* clear mask : chooses the clearing pattern. */
D
Dave Airlie 已提交
937
					OUT_RING(clearmask);
L
Linus Torvalds 已提交
938 939 940
					ADVANCE_RING();
					tileoffset += depthpixperline >> 5;
				}
D
Dave Airlie 已提交
941
			} else {	/* rv 100 */
L
Linus Torvalds 已提交
942 943
				/* rv100 might not need 64 pix alignment, who knows */
				/* offsets are, hmm, weird */
D
Dave Airlie 已提交
944 945 946 947 948 949 950 951
				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 已提交
952
				for (j = 0; j <= nrtilesy; j++) {
D
Dave Airlie 已提交
953 954 955 956
					BEGIN_RING(4);
					OUT_RING(CP_PACKET3
						 (RADEON_3D_CLEAR_ZMASK, 2));
					OUT_RING(tileoffset * 128);
L
Linus Torvalds 已提交
957
					/* the number of tiles to clear */
D
Dave Airlie 已提交
958
					OUT_RING(nrtilesx + 4);
L
Linus Torvalds 已提交
959
					/* clear mask : chooses the clearing pattern. */
D
Dave Airlie 已提交
960
					OUT_RING(clearmask);
L
Linus Torvalds 已提交
961 962 963 964 965 966 967
					ADVANCE_RING();
					tileoffset += depthpixperline >> 6;
				}
			}
		}

		/* TODO don't always clear all hi-level z tiles */
D
Dave Airlie 已提交
968 969 970 971 972 973 974
		if ((dev_priv->flags & CHIP_HAS_HIERZ)
		    && (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 已提交
975
		{
D
Dave Airlie 已提交
976 977 978 979 980
			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 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
			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.
	 */
	if ((dev_priv->microcode_version == UCODE_R200) &&
	    (flags & (RADEON_DEPTH | RADEON_STENCIL))) {

		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 已提交
1017 1018 1019
		tempSE_VAP_CNTL = (	/* SE_VAP_CNTL__FORCE_W_TO_ONE_MASK |  */
					  (0x9 <<
					   SE_VAP_CNTL__VF_MAX_VTX_NUM__SHIFT));
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025

		tempRB3D_PLANEMASK = 0x0;

		tempRE_AUX_SCISSOR_CNTL = 0x0;

		tempSE_VTE_CNTL =
D
Dave Airlie 已提交
1026
		    SE_VTE_CNTL__VTX_XY_FMT_MASK | SE_VTE_CNTL__VTX_Z_FMT_MASK;
L
Linus Torvalds 已提交
1027

D
Dave Airlie 已提交
1028
		/* Vertex format (X, Y, Z, W) */
L
Linus Torvalds 已提交
1029
		tempSE_VTX_FMT_0 =
D
Dave Airlie 已提交
1030 1031
		    SE_VTX_FMT_0__VTX_Z0_PRESENT_MASK |
		    SE_VTX_FMT_0__VTX_W0_PRESENT_MASK;
L
Linus Torvalds 已提交
1032 1033
		tempSE_VTX_FMT_1 = 0x0;

D
Dave Airlie 已提交
1034 1035
		/*
		 * Depth buffer specific enables
L
Linus Torvalds 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044
		 */
		if (flags & RADEON_DEPTH) {
			/* Enable depth buffer */
			tempRB3D_CNTL |= RADEON_Z_ENABLE;
		} else {
			/* Disable depth buffer */
			tempRB3D_CNTL &= ~RADEON_Z_ENABLE;
		}

D
Dave Airlie 已提交
1045
		/*
L
Linus Torvalds 已提交
1046 1047
		 * Stencil buffer specific enables
		 */
D
Dave Airlie 已提交
1048 1049 1050
		if (flags & RADEON_STENCIL) {
			tempRB3D_CNTL |= RADEON_STENCIL_ENABLE;
			tempRB3D_STENCILREFMASK = clear->depth_mask;
L
Linus Torvalds 已提交
1051 1052 1053 1054 1055 1056 1057
		} else {
			tempRB3D_CNTL &= ~RADEON_STENCIL_ENABLE;
			tempRB3D_STENCILREFMASK = 0x00000000;
		}

		if (flags & RADEON_USE_COMP_ZBUF) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
D
Dave Airlie 已提交
1058
			    RADEON_Z_DECOMPRESSION_ENABLE;
L
Linus Torvalds 已提交
1059 1060 1061 1062 1063
		}
		if (flags & RADEON_USE_HIERZ) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
		}

D
Dave Airlie 已提交
1064
		BEGIN_RING(26);
L
Linus Torvalds 已提交
1065 1066
		RADEON_WAIT_UNTIL_2D_IDLE();

D
Dave Airlie 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
		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 已提交
1080 1081 1082 1083 1084 1085
		ADVANCE_RING();

		/* Make sure we restore the 3D state next time.
		 */
		dev_priv->sarea_priv->ctx_owner = 0;

D
Dave Airlie 已提交
1086 1087 1088
		for (i = 0; i < nbox; i++) {

			/* Funny that this should be required --
L
Linus Torvalds 已提交
1089 1090
			 *  sets top-left?
			 */
D
Dave Airlie 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
			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 已提交
1110 1111
			ADVANCE_RING();
		}
D
Dave Airlie 已提交
1112
	} else if ((flags & (RADEON_DEPTH | RADEON_STENCIL))) {
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117

		int tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;

		rb3d_cntl = depth_clear->rb3d_cntl;

D
Dave Airlie 已提交
1118 1119
		if (flags & RADEON_DEPTH) {
			rb3d_cntl |= RADEON_Z_ENABLE;
L
Linus Torvalds 已提交
1120 1121 1122 1123
		} else {
			rb3d_cntl &= ~RADEON_Z_ENABLE;
		}

D
Dave Airlie 已提交
1124 1125 1126
		if (flags & RADEON_STENCIL) {
			rb3d_cntl |= RADEON_STENCIL_ENABLE;
			rb3d_stencilrefmask = clear->depth_mask;	/* misnamed field */
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133
		} else {
			rb3d_cntl &= ~RADEON_STENCIL_ENABLE;
			rb3d_stencilrefmask = 0x00000000;
		}

		if (flags & RADEON_USE_COMP_ZBUF) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
D
Dave Airlie 已提交
1134
			    RADEON_Z_DECOMPRESSION_ENABLE;
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139
		}
		if (flags & RADEON_USE_HIERZ) {
			tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
		}

D
Dave Airlie 已提交
1140
		BEGIN_RING(13);
L
Linus Torvalds 已提交
1141 1142
		RADEON_WAIT_UNTIL_2D_IDLE();

D
Dave Airlie 已提交
1143 1144 1145 1146 1147 1148 1149 1150
		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 已提交
1151 1152 1153 1154 1155 1156
		ADVANCE_RING();

		/* Make sure we restore the 3D state next time.
		 */
		dev_priv->sarea_priv->ctx_owner = 0;

D
Dave Airlie 已提交
1157 1158 1159
		for (i = 0; i < nbox; i++) {

			/* Funny that this should be required --
L
Linus Torvalds 已提交
1160 1161
			 *  sets top-left?
			 */
D
Dave Airlie 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
			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 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

			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...
	 */
	dev_priv->sarea_priv->last_clear++;

D
Dave Airlie 已提交
1200
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1201

D
Dave Airlie 已提交
1202
	RADEON_CLEAR_AGE(dev_priv->sarea_priv->last_clear);
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207
	RADEON_WAIT_UNTIL_IDLE();

	ADVANCE_RING();
}

D
Dave Airlie 已提交
1208
static void radeon_cp_dispatch_swap(drm_device_t * dev)
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214 1215
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	int nbox = sarea_priv->nbox;
	drm_clip_rect_t *pbox = sarea_priv->boxes;
	int i;
	RING_LOCALS;
D
Dave Airlie 已提交
1216
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
1217 1218 1219 1220

	/* Do some trivial performance monitoring...
	 */
	if (dev_priv->do_boxes)
D
Dave Airlie 已提交
1221
		radeon_cp_performance_boxes(dev_priv);
L
Linus Torvalds 已提交
1222 1223 1224 1225

	/* Wait for the 3D stream to idle before dispatching the bitblt.
	 * This will prevent data corruption between the two streams.
	 */
D
Dave Airlie 已提交
1226
	BEGIN_RING(2);
L
Linus Torvalds 已提交
1227 1228 1229 1230 1231

	RADEON_WAIT_UNTIL_3D_IDLE();

	ADVANCE_RING();

D
Dave Airlie 已提交
1232
	for (i = 0; i < nbox; i++) {
L
Linus Torvalds 已提交
1233 1234 1235 1236 1237
		int x = pbox[i].x1;
		int y = pbox[i].y1;
		int w = pbox[i].x2 - x;
		int h = pbox[i].y2 - y;

D
Dave Airlie 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		DRM_DEBUG("dispatch swap %d,%d-%d,%d\n", x, y, w, h);

		BEGIN_RING(7);

		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 |
			 (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 已提交
1252 1253 1254
		/* Make this work even if front & back are flipped:
		 */
		if (dev_priv->current_page == 0) {
D
Dave Airlie 已提交
1255 1256 1257 1258 1259
			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 已提交
1260 1261
		}

D
Dave Airlie 已提交
1262 1263 1264
		OUT_RING((x << 16) | y);
		OUT_RING((x << 16) | y);
		OUT_RING((w << 16) | h);
L
Linus Torvalds 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

		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.
	 */
	dev_priv->sarea_priv->last_frame++;

D
Dave Airlie 已提交
1275
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1276

D
Dave Airlie 已提交
1277
	RADEON_FRAME_AGE(dev_priv->sarea_priv->last_frame);
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282
	RADEON_WAIT_UNTIL_2D_IDLE();

	ADVANCE_RING();
}

D
Dave Airlie 已提交
1283
static void radeon_cp_dispatch_flip(drm_device_t * dev)
L
Linus Torvalds 已提交
1284 1285
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
1286
	drm_sarea_t *sarea = (drm_sarea_t *) dev_priv->sarea->handle;
L
Linus Torvalds 已提交
1287
	int offset = (dev_priv->current_page == 1)
D
Dave Airlie 已提交
1288
	    ? dev_priv->front_offset : dev_priv->back_offset;
L
Linus Torvalds 已提交
1289
	RING_LOCALS;
D
Dave Airlie 已提交
1290 1291 1292
	DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
		  __FUNCTION__,
		  dev_priv->current_page, dev_priv->sarea_priv->pfCurrentPage);
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297

	/* Do some trivial performance monitoring...
	 */
	if (dev_priv->do_boxes) {
		dev_priv->stats.boxes |= RADEON_BOX_FLIP;
D
Dave Airlie 已提交
1298
		radeon_cp_performance_boxes(dev_priv);
L
Linus Torvalds 已提交
1299 1300 1301 1302
	}

	/* Update the frame offsets for both CRTCs
	 */
D
Dave Airlie 已提交
1303
	BEGIN_RING(6);
L
Linus Torvalds 已提交
1304 1305

	RADEON_WAIT_UNTIL_3D_IDLE();
D
Dave Airlie 已提交
1306 1307 1308 1309 1310 1311
	OUT_RING_REG(RADEON_CRTC_OFFSET,
		     ((sarea->frame.y * dev_priv->front_pitch +
		       sarea->frame.x * (dev_priv->color_fmt - 2)) & ~7)
		     + offset);
	OUT_RING_REG(RADEON_CRTC2_OFFSET, dev_priv->sarea_priv->crtc2_base
		     + offset);
L
Linus Torvalds 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320

	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.
	 */
	dev_priv->sarea_priv->last_frame++;
	dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page =
D
Dave Airlie 已提交
1321
	    1 - dev_priv->current_page;
L
Linus Torvalds 已提交
1322

D
Dave Airlie 已提交
1323
	BEGIN_RING(2);
L
Linus Torvalds 已提交
1324

D
Dave Airlie 已提交
1325
	RADEON_FRAME_AGE(dev_priv->sarea_priv->last_frame);
L
Linus Torvalds 已提交
1326 1327 1328 1329

	ADVANCE_RING();
}

D
Dave Airlie 已提交
1330
static int bad_prim_vertex_nr(int primitive, int nr)
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
{
	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 已提交
1350
	}
L
Linus Torvalds 已提交
1351 1352 1353 1354 1355 1356 1357
}

typedef struct {
	unsigned int start;
	unsigned int finish;
	unsigned int prim;
	unsigned int numverts;
D
Dave Airlie 已提交
1358 1359
	unsigned int offset;
	unsigned int vc_format;
L
Linus Torvalds 已提交
1360 1361
} drm_radeon_tcl_prim_t;

D
Dave Airlie 已提交
1362 1363 1364
static void radeon_cp_dispatch_vertex(drm_device_t * dev,
				      drm_buf_t * buf,
				      drm_radeon_tcl_prim_t * prim)
L
Linus Torvalds 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	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 已提交
1376
		  prim->vc_format, prim->start, prim->finish, prim->numverts);
L
Linus Torvalds 已提交
1377

D
Dave Airlie 已提交
1378 1379 1380
	if (bad_prim_vertex_nr(prim->prim, prim->numverts)) {
		DRM_ERROR("bad prim %x numverts %d\n",
			  prim->prim, prim->numverts);
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385
		return;
	}

	do {
		/* Emit the next cliprect */
D
Dave Airlie 已提交
1386 1387
		if (i < nbox) {
			radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
L
Linus Torvalds 已提交
1388 1389 1390
		}

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

D
Dave Airlie 已提交
1393 1394 1395 1396 1397 1398 1399 1400
		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 已提交
1401 1402 1403 1404

		ADVANCE_RING();

		i++;
D
Dave Airlie 已提交
1405
	} while (i < nbox);
L
Linus Torvalds 已提交
1406 1407
}

D
Dave Airlie 已提交
1408
static void radeon_cp_discard_buffer(drm_device_t * dev, drm_buf_t * buf)
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414 1415 1416
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
	RING_LOCALS;

	buf_priv->age = ++dev_priv->sarea_priv->last_dispatch;

	/* Emit the vertex buffer age */
D
Dave Airlie 已提交
1417 1418
	BEGIN_RING(2);
	RADEON_DISPATCH_AGE(buf_priv->age);
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424
	ADVANCE_RING();

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

D
Dave Airlie 已提交
1425 1426
static void radeon_cp_dispatch_indirect(drm_device_t * dev,
					drm_buf_t * buf, int start, int end)
L
Linus Torvalds 已提交
1427 1428 1429
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	RING_LOCALS;
D
Dave Airlie 已提交
1430
	DRM_DEBUG("indirect: buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
L
Linus Torvalds 已提交
1431

D
Dave Airlie 已提交
1432
	if (start != end) {
L
Linus Torvalds 已提交
1433 1434 1435 1436 1437 1438 1439 1440
		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 已提交
1441
		if (dwords & 1) {
L
Linus Torvalds 已提交
1442
			u32 *data = (u32 *)
D
Dave Airlie 已提交
1443 1444
			    ((char *)dev->agp_buffer_map->handle
			     + buf->offset + start);
L
Linus Torvalds 已提交
1445 1446 1447 1448
			data[dwords++] = RADEON_CP_PACKET2;
		}

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

D
Dave Airlie 已提交
1451 1452 1453
		OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
		OUT_RING(offset);
		OUT_RING(dwords);
L
Linus Torvalds 已提交
1454 1455 1456 1457 1458

		ADVANCE_RING();
	}
}

D
Dave Airlie 已提交
1459 1460 1461
static void radeon_cp_dispatch_indices(drm_device_t * dev,
				       drm_buf_t * elt_buf,
				       drm_radeon_tcl_prim_t * prim)
L
Linus Torvalds 已提交
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	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 已提交
1476 1477 1478 1479
		  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 已提交
1480 1481 1482
		return;
	}

D
Dave Airlie 已提交
1483 1484
	if (start >= prim->finish || (prim->start & 0x7)) {
		DRM_ERROR("buffer prim %d\n", prim->prim);
L
Linus Torvalds 已提交
1485 1486 1487 1488 1489
		return;
	}

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

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

D
Dave Airlie 已提交
1493
	data[0] = CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, dwords - 2);
L
Linus Torvalds 已提交
1494 1495 1496 1497 1498 1499 1500
	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 已提交
1501
		   (count << RADEON_NUM_VERTICES_SHIFT));
L
Linus Torvalds 已提交
1502 1503

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

D
Dave Airlie 已提交
1507 1508
		radeon_cp_dispatch_indirect(dev, elt_buf,
					    prim->start, prim->finish);
L
Linus Torvalds 已提交
1509 1510

		i++;
D
Dave Airlie 已提交
1511
	} while (i < nbox);
L
Linus Torvalds 已提交
1512 1513 1514

}

1515
#define RADEON_MAX_TEXTURE_SIZE RADEON_BUFFER_SIZE
L
Linus Torvalds 已提交
1516

D
Dave Airlie 已提交
1517 1518 1519 1520
static int radeon_cp_dispatch_texture(DRMFILE filp,
				      drm_device_t * dev,
				      drm_radeon_texture_t * tex,
				      drm_radeon_tex_image_t * image)
L
Linus Torvalds 已提交
1521 1522 1523 1524 1525 1526 1527
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_file_t *filp_priv;
	drm_buf_t *buf;
	u32 format;
	u32 *buffer;
	const u8 __user *data;
1528
	int size, dwords, tex_width, blit_width, spitch;
L
Linus Torvalds 已提交
1529 1530 1531
	u32 height;
	int i;
	u32 texpitch, microtile;
1532
	u32 offset;
L
Linus Torvalds 已提交
1533 1534
	RING_LOCALS;

D
Dave Airlie 已提交
1535
	DRM_GET_PRIV_WITH_RETURN(filp_priv, filp);
L
Linus Torvalds 已提交
1536

D
Dave Airlie 已提交
1537 1538 1539
	if (radeon_check_and_fixup_offset(dev_priv, filp_priv, &tex->offset)) {
		DRM_ERROR("Invalid destination offset\n");
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
1540 1541 1542 1543 1544 1545 1546 1547
	}

	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 已提交
1548
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1549 1550 1551 1552 1553 1554 1555 1556
	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 已提交
1557
	switch (tex->format) {
L
Linus Torvalds 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
	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 已提交
1581
		DRM_ERROR("invalid texture format %d\n", tex->format);
L
Linus Torvalds 已提交
1582 1583
		return DRM_ERR(EINVAL);
	}
1584 1585 1586 1587
	spitch = blit_width >> 6;
	if (spitch == 0 && image->height > 1)
		return DRM_ERR(EINVAL);

L
Linus Torvalds 已提交
1588 1589 1590 1591 1592 1593 1594 1595
	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 已提交
1596 1597
	} else
		microtile = 0;
L
Linus Torvalds 已提交
1598

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

	do {
D
Dave Airlie 已提交
1602 1603 1604
		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 已提交
1605 1606 1607 1608 1609 1610

		/* 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 已提交
1611

L
Linus Torvalds 已提交
1612 1613
		size = height * blit_width;

D
Dave Airlie 已提交
1614
		if (size > RADEON_MAX_TEXTURE_SIZE) {
L
Linus Torvalds 已提交
1615 1616
			height = RADEON_MAX_TEXTURE_SIZE / blit_width;
			size = height * blit_width;
D
Dave Airlie 已提交
1617
		} else if (size < 4 && size > 0) {
L
Linus Torvalds 已提交
1618
			size = 4;
D
Dave Airlie 已提交
1619
		} else if (size == 0) {
L
Linus Torvalds 已提交
1620 1621 1622
			return 0;
		}

D
Dave Airlie 已提交
1623 1624 1625 1626
		buf = radeon_freelist_get(dev);
		if (0 && !buf) {
			radeon_do_cp_idle(dev_priv);
			buf = radeon_freelist_get(dev);
L
Linus Torvalds 已提交
1627
		}
D
Dave Airlie 已提交
1628
		if (!buf) {
L
Linus Torvalds 已提交
1629
			DRM_DEBUG("radeon_cp_dispatch_texture: EAGAIN\n");
D
Dave Airlie 已提交
1630
			if (DRM_COPY_TO_USER(tex->image, image, sizeof(*image)))
L
Linus Torvalds 已提交
1631 1632 1633 1634 1635 1636
				return DRM_ERR(EFAULT);
			return DRM_ERR(EAGAIN);
		}

		/* Dispatch the indirect buffer.
		 */
D
Dave Airlie 已提交
1637 1638
		buffer =
		    (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
L
Linus Torvalds 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
		dwords = size / 4;

		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) {
					if (DRM_COPY_FROM_USER(buffer, data,
D
Dave Airlie 已提交
1653 1654 1655 1656 1657
							       tex_width *
							       sizeof(u32))) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1658 1659 1660
						return DRM_ERR(EFAULT);
					}
				} else if (tex_width == 32) {
D
Dave Airlie 已提交
1661 1662 1663 1664 1665
					if (DRM_COPY_FROM_USER
					    (buffer, data, 16)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1666 1667
						return DRM_ERR(EFAULT);
					}
D
Dave Airlie 已提交
1668 1669 1670 1671 1672
					if (DRM_COPY_FROM_USER
					    (buffer + 8, data + 16, 16)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
						return DRM_ERR(EFAULT);
					}
				}
			} else if (tex_width >= 64 || tex_width == 16) {
				if (DRM_COPY_FROM_USER(buffer, data,
						       dwords * sizeof(u32))) {
					DRM_ERROR("EFAULT on data, %d dwords\n",
						  dwords);
					return DRM_ERR(EFAULT);
				}
			} else if (tex_width < 16) {
				for (i = 0; i < tex->height; i++) {
D
Dave Airlie 已提交
1685 1686 1687 1688 1689
					if (DRM_COPY_FROM_USER
					    (buffer, data, tex_width)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698
						return DRM_ERR(EFAULT);
					}
					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) {
D
Dave Airlie 已提交
1699 1700 1701 1702 1703
					if (DRM_COPY_FROM_USER
					    (buffer, data, 16)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1704 1705 1706
						return DRM_ERR(EFAULT);
					}
					data += 16;
D
Dave Airlie 已提交
1707 1708 1709 1710 1711
					if (DRM_COPY_FROM_USER
					    (buffer + 8, data, 16)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1712 1713 1714
						return DRM_ERR(EFAULT);
					}
					data += 16;
D
Dave Airlie 已提交
1715 1716 1717 1718 1719
					if (DRM_COPY_FROM_USER
					    (buffer + 4, data, 16)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1720 1721 1722
						return DRM_ERR(EFAULT);
					}
					data += 16;
D
Dave Airlie 已提交
1723 1724 1725 1726 1727
					if (DRM_COPY_FROM_USER
					    (buffer + 12, data, 16)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1728 1729 1730 1731 1732 1733
						return DRM_ERR(EFAULT);
					}
					data += 16;
					buffer += 16;
				}
			}
D
Dave Airlie 已提交
1734
		} else {
L
Linus Torvalds 已提交
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
			if (tex_width >= 32) {
				/* Texture image width is larger than the minimum, so we
				 * can upload it directly.
				 */
				if (DRM_COPY_FROM_USER(buffer, data,
						       dwords * sizeof(u32))) {
					DRM_ERROR("EFAULT on data, %d dwords\n",
						  dwords);
					return DRM_ERR(EFAULT);
				}
			} 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 已提交
1750 1751 1752 1753 1754 1755
				for (i = 0; i < tex->height; i++) {
					if (DRM_COPY_FROM_USER
					    (buffer, data, tex_width)) {
						DRM_ERROR
						    ("EFAULT on pad, %d bytes\n",
						     tex_width);
L
Linus Torvalds 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764
						return DRM_ERR(EFAULT);
					}
					buffer += 8;
					data += tex_width;
				}
			}
		}

		buf->filp = filp;
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
		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 已提交
1776
			 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
1777 1778 1779 1780 1781 1782 1783 1784 1785
		OUT_RING((spitch << 22) | (offset >> 10));
		OUT_RING((texpitch << 22) | (tex->offset >> 10));
		OUT_RING(0);
		OUT_RING((image->x << 16) | image->y);
		OUT_RING((image->width << 16) | height);
		RADEON_WAIT_UNTIL_2D_IDLE();
		ADVANCE_RING();

		radeon_cp_discard_buffer(dev, buf);
L
Linus Torvalds 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

		/* 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 已提交
1797
	BEGIN_RING(4);
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803
	RADEON_FLUSH_CACHE();
	RADEON_WAIT_UNTIL_2D_IDLE();
	ADVANCE_RING();
	return 0;
}

D
Dave Airlie 已提交
1804
static void radeon_cp_dispatch_stipple(drm_device_t * dev, u32 * stipple)
L
Linus Torvalds 已提交
1805 1806 1807 1808
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	int i;
	RING_LOCALS;
D
Dave Airlie 已提交
1809
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
1810

D
Dave Airlie 已提交
1811
	BEGIN_RING(35);
L
Linus Torvalds 已提交
1812

D
Dave Airlie 已提交
1813 1814
	OUT_RING(CP_PACKET0(RADEON_RE_STIPPLE_ADDR, 0));
	OUT_RING(0x00000000);
L
Linus Torvalds 已提交
1815

D
Dave Airlie 已提交
1816 1817 1818
	OUT_RING(CP_PACKET0_TABLE(RADEON_RE_STIPPLE_DATA, 31));
	for (i = 0; i < 32; i++) {
		OUT_RING(stipple[i]);
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823
	}

	ADVANCE_RING();
}

D
Dave Airlie 已提交
1824 1825
static void radeon_apply_surface_regs(int surf_index,
				      drm_radeon_private_t * dev_priv)
L
Linus Torvalds 已提交
1826 1827 1828 1829 1830 1831
{
	if (!dev_priv->mmio)
		return;

	radeon_do_cp_idle(dev_priv);

D
Dave Airlie 已提交
1832 1833 1834 1835 1836 1837
	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 已提交
1838 1839 1840
}

/* Allocates a virtual surface
D
Dave Airlie 已提交
1841
 * doesn't always allocate a real surface, will stretch an existing
L
Linus Torvalds 已提交
1842 1843 1844 1845 1846
 * 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 已提交
1847
 * For example : we allocate three contigous surfaces ABC. If B is
L
Linus Torvalds 已提交
1848 1849 1850
 * freed, we suddenly need two surfaces to store A and C, which might
 * not always be available.
 */
D
Dave Airlie 已提交
1851 1852
static int alloc_surface(drm_radeon_surface_alloc_t * new,
			 drm_radeon_private_t * dev_priv, DRMFILE filp)
L
Linus Torvalds 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
{
	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 已提交
1864 1865 1866
	    ((new_upper & RADEON_SURF_ADDRESS_FIXED_MASK) !=
	     RADEON_SURF_ADDRESS_FIXED_MASK)
	    || ((new_lower & RADEON_SURF_ADDRESS_FIXED_MASK) != 0))
L
Linus Torvalds 已提交
1867 1868 1869 1870 1871
		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 已提交
1872 1873 1874 1875 1876 1877
		    (((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 已提交
1878 1879 1880
	}

	/* find a virtual surface */
D
Dave Airlie 已提交
1881
	for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++)
L
Linus Torvalds 已提交
1882 1883
		if (dev_priv->virt_surfaces[i].filp == 0)
			break;
D
Dave Airlie 已提交
1884 1885 1886
	if (i == 2 * RADEON_MAX_SURFACES) {
		return -1;
	}
L
Linus Torvalds 已提交
1887 1888 1889 1890 1891 1892
	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 已提交
1893 1894
		    (new->flags == dev_priv->surfaces[i].flags) &&
		    (new_upper + 1 == dev_priv->surfaces[i].lower)) {
L
Linus Torvalds 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
			s = &(dev_priv->virt_surfaces[virt_surface_index]);
			s->surface_index = i;
			s->lower = new_lower;
			s->upper = new_upper;
			s->flags = new->flags;
			s->filp = filp;
			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 已提交
1909 1910
		    (new->flags == dev_priv->surfaces[i].flags) &&
		    (new_lower == dev_priv->surfaces[i].upper + 1)) {
L
Linus Torvalds 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
			s = &(dev_priv->virt_surfaces[virt_surface_index]);
			s->surface_index = i;
			s->lower = new_lower;
			s->upper = new_upper;
			s->flags = new->flags;
			s->filp = filp;
			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;
			s->filp = filp;
			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;
}

D
Dave Airlie 已提交
1946 1947
static int free_surface(DRMFILE filp, drm_radeon_private_t * dev_priv,
			int lower)
L
Linus Torvalds 已提交
1948 1949 1950 1951
{
	struct radeon_virt_surface *s;
	int i;
	/* find the virtual surface */
D
Dave Airlie 已提交
1952
	for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
L
Linus Torvalds 已提交
1953 1954 1955
		s = &(dev_priv->virt_surfaces[i]);
		if (s->filp) {
			if ((lower == s->lower) && (filp == s->filp)) {
D
Dave Airlie 已提交
1956 1957 1958 1959
				if (dev_priv->surfaces[s->surface_index].
				    lower == s->lower)
					dev_priv->surfaces[s->surface_index].
					    lower = s->upper;
L
Linus Torvalds 已提交
1960

D
Dave Airlie 已提交
1961 1962 1963 1964
				if (dev_priv->surfaces[s->surface_index].
				    upper == s->upper)
					dev_priv->surfaces[s->surface_index].
					    upper = s->lower;
L
Linus Torvalds 已提交
1965 1966

				dev_priv->surfaces[s->surface_index].refcount--;
D
Dave Airlie 已提交
1967 1968 1969 1970
				if (dev_priv->surfaces[s->surface_index].
				    refcount == 0)
					dev_priv->surfaces[s->surface_index].
					    flags = 0;
L
Linus Torvalds 已提交
1971
				s->filp = NULL;
D
Dave Airlie 已提交
1972 1973
				radeon_apply_surface_regs(s->surface_index,
							  dev_priv);
L
Linus Torvalds 已提交
1974 1975 1976 1977 1978 1979 1980
				return 0;
			}
		}
	}
	return 1;
}

D
Dave Airlie 已提交
1981 1982
static void radeon_surfaces_release(DRMFILE filp,
				    drm_radeon_private_t * dev_priv)
L
Linus Torvalds 已提交
1983 1984
{
	int i;
D
Dave Airlie 已提交
1985
	for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
L
Linus Torvalds 已提交
1986
		if (dev_priv->virt_surfaces[i].filp == filp)
D
Dave Airlie 已提交
1987 1988
			free_surface(filp, dev_priv,
				     dev_priv->virt_surfaces[i].lower);
L
Linus Torvalds 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
	}
}

/* ================================================================
 * IOCTL functions
 */
static int radeon_surface_alloc(DRM_IOCTL_ARGS)
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_surface_alloc_t alloc;

	if (!dev_priv) {
D
Dave Airlie 已提交
2002
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
2003 2004 2005
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2006 2007 2008
	DRM_COPY_FROM_USER_IOCTL(alloc,
				 (drm_radeon_surface_alloc_t __user *) data,
				 sizeof(alloc));
L
Linus Torvalds 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022

	if (alloc_surface(&alloc, dev_priv, filp) == -1)
		return DRM_ERR(EINVAL);
	else
		return 0;
}

static int radeon_surface_free(DRM_IOCTL_ARGS)
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_surface_free_t memfree;

	if (!dev_priv) {
D
Dave Airlie 已提交
2023
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
2024 2025 2026
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2027 2028
	DRM_COPY_FROM_USER_IOCTL(memfree, (drm_radeon_mem_free_t __user *) data,
				 sizeof(memfree));
L
Linus Torvalds 已提交
2029 2030 2031 2032 2033 2034 2035

	if (free_surface(filp, dev_priv, memfree.address))
		return DRM_ERR(EINVAL);
	else
		return 0;
}

D
Dave Airlie 已提交
2036
static int radeon_cp_clear(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2037 2038 2039 2040 2041 2042
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	drm_radeon_clear_t clear;
	drm_radeon_clear_rect_t depth_boxes[RADEON_NR_SAREA_CLIPRECTS];
D
Dave Airlie 已提交
2043
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
2044

D
Dave Airlie 已提交
2045
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2046

D
Dave Airlie 已提交
2047 2048
	DRM_COPY_FROM_USER_IOCTL(clear, (drm_radeon_clear_t __user *) data,
				 sizeof(clear));
L
Linus Torvalds 已提交
2049

D
Dave Airlie 已提交
2050
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2051

D
Dave Airlie 已提交
2052
	if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
L
Linus Torvalds 已提交
2053 2054
		sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;

D
Dave Airlie 已提交
2055 2056
	if (DRM_COPY_FROM_USER(&depth_boxes, clear.depth_boxes,
			       sarea_priv->nbox * sizeof(depth_boxes[0])))
L
Linus Torvalds 已提交
2057 2058
		return DRM_ERR(EFAULT);

D
Dave Airlie 已提交
2059
	radeon_cp_dispatch_clear(dev, &clear, depth_boxes);
L
Linus Torvalds 已提交
2060 2061 2062 2063 2064 2065

	COMMIT_RING();
	return 0;
}

/* Not sure why this isn't set all the time:
D
Dave Airlie 已提交
2066 2067
 */
static int radeon_do_init_pageflip(drm_device_t * dev)
L
Linus Torvalds 已提交
2068 2069 2070 2071
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	RING_LOCALS;

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

D
Dave Airlie 已提交
2074
	BEGIN_RING(6);
L
Linus Torvalds 已提交
2075
	RADEON_WAIT_UNTIL_3D_IDLE();
D
Dave Airlie 已提交
2076 2077 2078 2079 2080 2081
	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 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
	ADVANCE_RING();

	dev_priv->page_flipping = 1;
	dev_priv->current_page = 0;
	dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page;

	return 0;
}

/* Called whenever a client dies, from drm_release.
 * NOTE:  Lock isn't necessarily held when this is called!
 */
D
Dave Airlie 已提交
2094
static int radeon_do_cleanup_pageflip(drm_device_t * dev)
L
Linus Torvalds 已提交
2095 2096
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
2097
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
2098 2099

	if (dev_priv->current_page != 0)
D
Dave Airlie 已提交
2100
		radeon_cp_dispatch_flip(dev);
L
Linus Torvalds 已提交
2101 2102 2103 2104 2105 2106

	dev_priv->page_flipping = 0;
	return 0;
}

/* Swapping and flipping are different operations, need different ioctls.
D
Dave Airlie 已提交
2107
 * They can & should be intermixed to support multiple 3d windows.
L
Linus Torvalds 已提交
2108
 */
D
Dave Airlie 已提交
2109
static int radeon_cp_flip(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2110 2111 2112
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
2113 2114 2115
	DRM_DEBUG("\n");

	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2116

D
Dave Airlie 已提交
2117
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2118

D
Dave Airlie 已提交
2119 2120
	if (!dev_priv->page_flipping)
		radeon_do_init_pageflip(dev);
L
Linus Torvalds 已提交
2121

D
Dave Airlie 已提交
2122
	radeon_cp_dispatch_flip(dev);
L
Linus Torvalds 已提交
2123 2124 2125 2126 2127

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2128
static int radeon_cp_swap(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2129 2130 2131 2132
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
D
Dave Airlie 已提交
2133
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
2134

D
Dave Airlie 已提交
2135
	LOCK_TEST_WITH_RETURN(dev, filp);
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;

D
Dave Airlie 已提交
2142
	radeon_cp_dispatch_swap(dev);
L
Linus Torvalds 已提交
2143 2144 2145 2146 2147 2148
	dev_priv->sarea_priv->ctx_owner = 0;

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2149
static int radeon_cp_vertex(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_file_t *filp_priv;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	drm_device_dma_t *dma = dev->dma;
	drm_buf_t *buf;
	drm_radeon_vertex_t vertex;
	drm_radeon_tcl_prim_t prim;

D
Dave Airlie 已提交
2160
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2161

D
Dave Airlie 已提交
2162
	DRM_GET_PRIV_WITH_RETURN(filp_priv, filp);
L
Linus Torvalds 已提交
2163

D
Dave Airlie 已提交
2164 2165
	DRM_COPY_FROM_USER_IOCTL(vertex, (drm_radeon_vertex_t __user *) data,
				 sizeof(vertex));
L
Linus Torvalds 已提交
2166

D
Dave Airlie 已提交
2167 2168
	DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
		  DRM_CURRENTPID, vertex.idx, vertex.count, vertex.discard);
L
Linus Torvalds 已提交
2169

D
Dave Airlie 已提交
2170 2171 2172
	if (vertex.idx < 0 || vertex.idx >= dma->buf_count) {
		DRM_ERROR("buffer index %d (of %d max)\n",
			  vertex.idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
2173 2174
		return DRM_ERR(EINVAL);
	}
D
Dave Airlie 已提交
2175 2176
	if (vertex.prim < 0 || vertex.prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
		DRM_ERROR("buffer prim %d\n", vertex.prim);
L
Linus Torvalds 已提交
2177 2178 2179
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2180 2181
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2182 2183 2184

	buf = dma->buflist[vertex.idx];

D
Dave Airlie 已提交
2185 2186 2187
	if (buf->filp != filp) {
		DRM_ERROR("process %d using buffer owned by %p\n",
			  DRM_CURRENTPID, buf->filp);
L
Linus Torvalds 已提交
2188 2189
		return DRM_ERR(EINVAL);
	}
D
Dave Airlie 已提交
2190 2191
	if (buf->pending) {
		DRM_ERROR("sending pending buffer %d\n", vertex.idx);
L
Linus Torvalds 已提交
2192 2193 2194 2195 2196 2197
		return DRM_ERR(EINVAL);
	}

	/* Build up a prim_t record:
	 */
	if (vertex.count) {
D
Dave Airlie 已提交
2198 2199 2200 2201 2202 2203 2204 2205 2206
		buf->used = vertex.count;	/* not used? */

		if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
			if (radeon_emit_state(dev_priv, filp_priv,
					      &sarea_priv->context_state,
					      sarea_priv->tex_state,
					      sarea_priv->dirty)) {
				DRM_ERROR("radeon_emit_state failed\n");
				return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
2207 2208 2209 2210 2211 2212 2213 2214 2215
			}

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

		prim.start = 0;
D
Dave Airlie 已提交
2216
		prim.finish = vertex.count;	/* unused */
L
Linus Torvalds 已提交
2217 2218 2219
		prim.prim = vertex.prim;
		prim.numverts = vertex.count;
		prim.vc_format = dev_priv->sarea_priv->vc_format;
D
Dave Airlie 已提交
2220 2221

		radeon_cp_dispatch_vertex(dev, buf, &prim);
L
Linus Torvalds 已提交
2222 2223 2224
	}

	if (vertex.discard) {
D
Dave Airlie 已提交
2225
		radeon_cp_discard_buffer(dev, buf);
L
Linus Torvalds 已提交
2226 2227 2228 2229 2230 2231
	}

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2232
static int radeon_cp_indices(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_file_t *filp_priv;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	drm_device_dma_t *dma = dev->dma;
	drm_buf_t *buf;
	drm_radeon_indices_t elts;
	drm_radeon_tcl_prim_t prim;
	int count;

D
Dave Airlie 已提交
2244
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2245

D
Dave Airlie 已提交
2246 2247
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
2248 2249 2250
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2251
	DRM_GET_PRIV_WITH_RETURN(filp_priv, filp);
L
Linus Torvalds 已提交
2252

D
Dave Airlie 已提交
2253 2254
	DRM_COPY_FROM_USER_IOCTL(elts, (drm_radeon_indices_t __user *) data,
				 sizeof(elts));
L
Linus Torvalds 已提交
2255

D
Dave Airlie 已提交
2256 2257
	DRM_DEBUG("pid=%d index=%d start=%d end=%d discard=%d\n",
		  DRM_CURRENTPID, elts.idx, elts.start, elts.end, elts.discard);
L
Linus Torvalds 已提交
2258

D
Dave Airlie 已提交
2259 2260 2261
	if (elts.idx < 0 || elts.idx >= dma->buf_count) {
		DRM_ERROR("buffer index %d (of %d max)\n",
			  elts.idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
2262 2263
		return DRM_ERR(EINVAL);
	}
D
Dave Airlie 已提交
2264 2265
	if (elts.prim < 0 || elts.prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
		DRM_ERROR("buffer prim %d\n", elts.prim);
L
Linus Torvalds 已提交
2266 2267 2268
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2269 2270
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2271 2272 2273

	buf = dma->buflist[elts.idx];

D
Dave Airlie 已提交
2274 2275 2276
	if (buf->filp != filp) {
		DRM_ERROR("process %d using buffer owned by %p\n",
			  DRM_CURRENTPID, buf->filp);
L
Linus Torvalds 已提交
2277 2278
		return DRM_ERR(EINVAL);
	}
D
Dave Airlie 已提交
2279 2280
	if (buf->pending) {
		DRM_ERROR("sending pending buffer %d\n", elts.idx);
L
Linus Torvalds 已提交
2281 2282 2283 2284 2285 2286
		return DRM_ERR(EINVAL);
	}

	count = (elts.end - elts.start) / sizeof(u16);
	elts.start -= RADEON_INDEX_PRIM_OFFSET;

D
Dave Airlie 已提交
2287 2288
	if (elts.start & 0x7) {
		DRM_ERROR("misaligned buffer 0x%x\n", elts.start);
L
Linus Torvalds 已提交
2289 2290
		return DRM_ERR(EINVAL);
	}
D
Dave Airlie 已提交
2291 2292
	if (elts.start < buf->used) {
		DRM_ERROR("no header 0x%x - 0x%x\n", elts.start, buf->used);
L
Linus Torvalds 已提交
2293 2294 2295 2296 2297
		return DRM_ERR(EINVAL);
	}

	buf->used = elts.end;

D
Dave Airlie 已提交
2298 2299 2300 2301 2302 2303 2304
	if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
		if (radeon_emit_state(dev_priv, filp_priv,
				      &sarea_priv->context_state,
				      sarea_priv->tex_state,
				      sarea_priv->dirty)) {
			DRM_ERROR("radeon_emit_state failed\n");
			return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
		}

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

	/* Build up a prim_t record:
	 */
	prim.start = elts.start;
D
Dave Airlie 已提交
2316
	prim.finish = elts.end;
L
Linus Torvalds 已提交
2317 2318
	prim.prim = elts.prim;
	prim.offset = 0;	/* offset from start of dma buffers */
D
Dave Airlie 已提交
2319
	prim.numverts = RADEON_MAX_VB_VERTS;	/* duh */
L
Linus Torvalds 已提交
2320
	prim.vc_format = dev_priv->sarea_priv->vc_format;
D
Dave Airlie 已提交
2321 2322

	radeon_cp_dispatch_indices(dev, buf, &prim);
L
Linus Torvalds 已提交
2323
	if (elts.discard) {
D
Dave Airlie 已提交
2324
		radeon_cp_discard_buffer(dev, buf);
L
Linus Torvalds 已提交
2325 2326 2327 2328 2329 2330
	}

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2331
static int radeon_cp_texture(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2332 2333 2334 2335 2336 2337 2338
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_texture_t tex;
	drm_radeon_tex_image_t image;
	int ret;

D
Dave Airlie 已提交
2339
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2340

D
Dave Airlie 已提交
2341 2342
	DRM_COPY_FROM_USER_IOCTL(tex, (drm_radeon_texture_t __user *) data,
				 sizeof(tex));
L
Linus Torvalds 已提交
2343

D
Dave Airlie 已提交
2344 2345
	if (tex.image == NULL) {
		DRM_ERROR("null texture image!\n");
L
Linus Torvalds 已提交
2346 2347 2348
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2349 2350 2351
	if (DRM_COPY_FROM_USER(&image,
			       (drm_radeon_tex_image_t __user *) tex.image,
			       sizeof(image)))
L
Linus Torvalds 已提交
2352 2353
		return DRM_ERR(EFAULT);

D
Dave Airlie 已提交
2354 2355
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2356

D
Dave Airlie 已提交
2357
	ret = radeon_cp_dispatch_texture(filp, dev, &tex, &image);
L
Linus Torvalds 已提交
2358 2359 2360 2361 2362

	COMMIT_RING();
	return ret;
}

D
Dave Airlie 已提交
2363
static int radeon_cp_stipple(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2364 2365 2366 2367 2368 2369
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_stipple_t stipple;
	u32 mask[32];

D
Dave Airlie 已提交
2370
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2371

D
Dave Airlie 已提交
2372 2373
	DRM_COPY_FROM_USER_IOCTL(stipple, (drm_radeon_stipple_t __user *) data,
				 sizeof(stipple));
L
Linus Torvalds 已提交
2374

D
Dave Airlie 已提交
2375
	if (DRM_COPY_FROM_USER(&mask, stipple.mask, 32 * sizeof(u32)))
L
Linus Torvalds 已提交
2376 2377
		return DRM_ERR(EFAULT);

D
Dave Airlie 已提交
2378
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2379

D
Dave Airlie 已提交
2380
	radeon_cp_dispatch_stipple(dev, mask);
L
Linus Torvalds 已提交
2381 2382 2383 2384 2385

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2386
static int radeon_cp_indirect(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2387 2388 2389 2390 2391 2392 2393 2394
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_device_dma_t *dma = dev->dma;
	drm_buf_t *buf;
	drm_radeon_indirect_t indirect;
	RING_LOCALS;

D
Dave Airlie 已提交
2395
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2396

D
Dave Airlie 已提交
2397 2398
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
2399 2400 2401
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2402 2403 2404
	DRM_COPY_FROM_USER_IOCTL(indirect,
				 (drm_radeon_indirect_t __user *) data,
				 sizeof(indirect));
L
Linus Torvalds 已提交
2405

D
Dave Airlie 已提交
2406 2407
	DRM_DEBUG("indirect: idx=%d s=%d e=%d d=%d\n",
		  indirect.idx, indirect.start, indirect.end, indirect.discard);
L
Linus Torvalds 已提交
2408

D
Dave Airlie 已提交
2409 2410 2411
	if (indirect.idx < 0 || indirect.idx >= dma->buf_count) {
		DRM_ERROR("buffer index %d (of %d max)\n",
			  indirect.idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
2412 2413 2414 2415 2416
		return DRM_ERR(EINVAL);
	}

	buf = dma->buflist[indirect.idx];

D
Dave Airlie 已提交
2417 2418 2419
	if (buf->filp != filp) {
		DRM_ERROR("process %d using buffer owned by %p\n",
			  DRM_CURRENTPID, buf->filp);
L
Linus Torvalds 已提交
2420 2421
		return DRM_ERR(EINVAL);
	}
D
Dave Airlie 已提交
2422 2423
	if (buf->pending) {
		DRM_ERROR("sending pending buffer %d\n", indirect.idx);
L
Linus Torvalds 已提交
2424 2425 2426
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2427 2428 2429
	if (indirect.start < buf->used) {
		DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
			  indirect.start, buf->used);
L
Linus Torvalds 已提交
2430 2431 2432
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2433 2434
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2435 2436 2437 2438 2439 2440

	buf->used = indirect.end;

	/* Wait for the 3D stream to idle before the indirect buffer
	 * containing 2D acceleration commands is processed.
	 */
D
Dave Airlie 已提交
2441
	BEGIN_RING(2);
L
Linus Torvalds 已提交
2442 2443 2444 2445 2446 2447 2448 2449 2450

	RADEON_WAIT_UNTIL_3D_IDLE();

	ADVANCE_RING();

	/* Dispatch the indirect buffer full of commands from the
	 * X server.  This is insecure and is thus only available to
	 * privileged clients.
	 */
D
Dave Airlie 已提交
2451
	radeon_cp_dispatch_indirect(dev, buf, indirect.start, indirect.end);
L
Linus Torvalds 已提交
2452
	if (indirect.discard) {
D
Dave Airlie 已提交
2453
		radeon_cp_discard_buffer(dev, buf);
L
Linus Torvalds 已提交
2454 2455 2456 2457 2458 2459
	}

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2460
static int radeon_cp_vertex2(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_file_t *filp_priv;
	drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
	drm_device_dma_t *dma = dev->dma;
	drm_buf_t *buf;
	drm_radeon_vertex2_t vertex;
	int i;
	unsigned char laststate;

D
Dave Airlie 已提交
2472
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2473

D
Dave Airlie 已提交
2474 2475
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
2476 2477 2478
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2479
	DRM_GET_PRIV_WITH_RETURN(filp_priv, filp);
L
Linus Torvalds 已提交
2480

D
Dave Airlie 已提交
2481 2482
	DRM_COPY_FROM_USER_IOCTL(vertex, (drm_radeon_vertex2_t __user *) data,
				 sizeof(vertex));
L
Linus Torvalds 已提交
2483

D
Dave Airlie 已提交
2484 2485
	DRM_DEBUG("pid=%d index=%d discard=%d\n",
		  DRM_CURRENTPID, vertex.idx, vertex.discard);
L
Linus Torvalds 已提交
2486

D
Dave Airlie 已提交
2487 2488 2489
	if (vertex.idx < 0 || vertex.idx >= dma->buf_count) {
		DRM_ERROR("buffer index %d (of %d max)\n",
			  vertex.idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
2490 2491 2492
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2493 2494
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2495 2496 2497

	buf = dma->buflist[vertex.idx];

D
Dave Airlie 已提交
2498 2499 2500
	if (buf->filp != filp) {
		DRM_ERROR("process %d using buffer owned by %p\n",
			  DRM_CURRENTPID, buf->filp);
L
Linus Torvalds 已提交
2501 2502 2503
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2504 2505
	if (buf->pending) {
		DRM_ERROR("sending pending buffer %d\n", vertex.idx);
L
Linus Torvalds 已提交
2506 2507
		return DRM_ERR(EINVAL);
	}
D
Dave Airlie 已提交
2508

L
Linus Torvalds 已提交
2509 2510 2511
	if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
		return DRM_ERR(EINVAL);

D
Dave Airlie 已提交
2512
	for (laststate = 0xff, i = 0; i < vertex.nr_prims; i++) {
L
Linus Torvalds 已提交
2513 2514
		drm_radeon_prim_t prim;
		drm_radeon_tcl_prim_t tclprim;
D
Dave Airlie 已提交
2515 2516

		if (DRM_COPY_FROM_USER(&prim, &vertex.prim[i], sizeof(prim)))
L
Linus Torvalds 已提交
2517
			return DRM_ERR(EFAULT);
D
Dave Airlie 已提交
2518 2519 2520 2521 2522 2523 2524

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

			if (DRM_COPY_FROM_USER(&state,
					       &vertex.state[prim.stateidx],
					       sizeof(state)))
L
Linus Torvalds 已提交
2525 2526
				return DRM_ERR(EFAULT);

D
Dave Airlie 已提交
2527 2528 2529
			if (radeon_emit_state2(dev_priv, filp_priv, &state)) {
				DRM_ERROR("radeon_emit_state2 failed\n");
				return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
			}

			laststate = prim.stateidx;
		}

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

D
Dave Airlie 已提交
2540
		if (prim.prim & RADEON_PRIM_WALK_IND) {
L
Linus Torvalds 已提交
2541
			tclprim.offset = prim.numverts * 64;
D
Dave Airlie 已提交
2542
			tclprim.numverts = RADEON_MAX_VB_VERTS;	/* duh */
L
Linus Torvalds 已提交
2543

D
Dave Airlie 已提交
2544
			radeon_cp_dispatch_indices(dev, buf, &tclprim);
L
Linus Torvalds 已提交
2545 2546
		} else {
			tclprim.numverts = prim.numverts;
D
Dave Airlie 已提交
2547
			tclprim.offset = 0;	/* not used */
L
Linus Torvalds 已提交
2548

D
Dave Airlie 已提交
2549
			radeon_cp_dispatch_vertex(dev, buf, &tclprim);
L
Linus Torvalds 已提交
2550
		}
D
Dave Airlie 已提交
2551

L
Linus Torvalds 已提交
2552 2553 2554 2555
		if (sarea_priv->nbox == 1)
			sarea_priv->nbox = 0;
	}

D
Dave Airlie 已提交
2556 2557
	if (vertex.discard) {
		radeon_cp_discard_buffer(dev, buf);
L
Linus Torvalds 已提交
2558 2559 2560 2561 2562 2563
	}

	COMMIT_RING();
	return 0;
}

D
Dave Airlie 已提交
2564 2565 2566 2567
static int radeon_emit_packets(drm_radeon_private_t * dev_priv,
			       drm_file_t * filp_priv,
			       drm_radeon_cmd_header_t header,
			       drm_radeon_cmd_buffer_t * cmdbuf)
L
Linus Torvalds 已提交
2568 2569 2570 2571 2572
{
	int id = (int)header.packet.packet_id;
	int sz, reg;
	int *data = (int *)cmdbuf->buf;
	RING_LOCALS;
D
Dave Airlie 已提交
2573

L
Linus Torvalds 已提交
2574 2575 2576 2577 2578 2579 2580
	if (id >= RADEON_MAX_STATE_PACKETS)
		return DRM_ERR(EINVAL);

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

	if (sz * sizeof(int) > cmdbuf->bufsz) {
D
Dave Airlie 已提交
2581
		DRM_ERROR("Packet size provided larger than data provided\n");
L
Linus Torvalds 已提交
2582 2583 2584
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2585 2586 2587
	if (radeon_check_and_fixup_packets(dev_priv, filp_priv, id, data)) {
		DRM_ERROR("Packet verification failed\n");
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
2588 2589
	}

D
Dave Airlie 已提交
2590 2591 2592
	BEGIN_RING(sz + 1);
	OUT_RING(CP_PACKET0(reg, (sz - 1)));
	OUT_RING_TABLE(data, sz);
L
Linus Torvalds 已提交
2593 2594 2595 2596 2597 2598 2599
	ADVANCE_RING();

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

D
Dave Airlie 已提交
2600 2601 2602
static __inline__ int radeon_emit_scalars(drm_radeon_private_t * dev_priv,
					  drm_radeon_cmd_header_t header,
					  drm_radeon_cmd_buffer_t * cmdbuf)
L
Linus Torvalds 已提交
2603 2604 2605 2606 2607 2608
{
	int sz = header.scalars.count;
	int start = header.scalars.offset;
	int stride = header.scalars.stride;
	RING_LOCALS;

D
Dave Airlie 已提交
2609 2610 2611 2612 2613
	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 已提交
2614 2615 2616 2617 2618 2619 2620 2621
	ADVANCE_RING();
	cmdbuf->buf += sz * sizeof(int);
	cmdbuf->bufsz -= sz * sizeof(int);
	return 0;
}

/* God this is ugly
 */
D
Dave Airlie 已提交
2622 2623 2624
static __inline__ int radeon_emit_scalars2(drm_radeon_private_t * dev_priv,
					   drm_radeon_cmd_header_t header,
					   drm_radeon_cmd_buffer_t * cmdbuf)
L
Linus Torvalds 已提交
2625 2626 2627 2628 2629 2630
{
	int sz = header.scalars.count;
	int start = ((unsigned int)header.scalars.offset) + 0x100;
	int stride = header.scalars.stride;
	RING_LOCALS;

D
Dave Airlie 已提交
2631 2632 2633 2634 2635
	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 已提交
2636 2637 2638 2639 2640 2641
	ADVANCE_RING();
	cmdbuf->buf += sz * sizeof(int);
	cmdbuf->bufsz -= sz * sizeof(int);
	return 0;
}

D
Dave Airlie 已提交
2642 2643 2644
static __inline__ int radeon_emit_vectors(drm_radeon_private_t * dev_priv,
					  drm_radeon_cmd_header_t header,
					  drm_radeon_cmd_buffer_t * cmdbuf)
L
Linus Torvalds 已提交
2645 2646 2647 2648 2649 2650
{
	int sz = header.vectors.count;
	int start = header.vectors.offset;
	int stride = header.vectors.stride;
	RING_LOCALS;

D
Dave Airlie 已提交
2651 2652 2653 2654 2655
	BEGIN_RING(3 + sz);
	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 已提交
2656 2657 2658 2659 2660 2661 2662
	ADVANCE_RING();

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

D
Dave Airlie 已提交
2663 2664 2665
static int radeon_emit_packet3(drm_device_t * dev,
			       drm_file_t * filp_priv,
			       drm_radeon_cmd_buffer_t * cmdbuf)
L
Linus Torvalds 已提交
2666 2667 2668 2669 2670 2671 2672 2673
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	unsigned int cmdsz;
	int ret;
	RING_LOCALS;

	DRM_DEBUG("\n");

D
Dave Airlie 已提交
2674 2675 2676
	if ((ret = radeon_check_and_fixup_packet3(dev_priv, filp_priv,
						  cmdbuf, &cmdsz))) {
		DRM_ERROR("Packet verification failed\n");
L
Linus Torvalds 已提交
2677 2678 2679
		return ret;
	}

D
Dave Airlie 已提交
2680 2681
	BEGIN_RING(cmdsz);
	OUT_RING_TABLE(cmdbuf->buf, cmdsz);
L
Linus Torvalds 已提交
2682 2683 2684 2685 2686 2687 2688
	ADVANCE_RING();

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

D
Dave Airlie 已提交
2689 2690 2691 2692
static int radeon_emit_packet3_cliprect(drm_device_t * dev,
					drm_file_t * filp_priv,
					drm_radeon_cmd_buffer_t * cmdbuf,
					int orig_nbox)
L
Linus Torvalds 已提交
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_clip_rect_t box;
	unsigned int cmdsz;
	int ret;
	drm_clip_rect_t __user *boxes = cmdbuf->boxes;
	int i = 0;
	RING_LOCALS;

	DRM_DEBUG("\n");

D
Dave Airlie 已提交
2704 2705 2706
	if ((ret = radeon_check_and_fixup_packet3(dev_priv, filp_priv,
						  cmdbuf, &cmdsz))) {
		DRM_ERROR("Packet verification failed\n");
L
Linus Torvalds 已提交
2707 2708 2709 2710 2711 2712 2713
		return ret;
	}

	if (!orig_nbox)
		goto out;

	do {
D
Dave Airlie 已提交
2714 2715
		if (i < cmdbuf->nbox) {
			if (DRM_COPY_FROM_USER(&box, &boxes[i], sizeof(box)))
L
Linus Torvalds 已提交
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
				return DRM_ERR(EFAULT);
			/* 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 已提交
2729 2730
			if (i) {
				BEGIN_RING(2);
L
Linus Torvalds 已提交
2731 2732 2733
				RADEON_WAIT_UNTIL_3D_IDLE();
				ADVANCE_RING();
			}
D
Dave Airlie 已提交
2734
			radeon_emit_clip_rect(dev_priv, &box);
L
Linus Torvalds 已提交
2735
		}
D
Dave Airlie 已提交
2736 2737 2738

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

D
Dave Airlie 已提交
2741 2742
	} while (++i < cmdbuf->nbox);
	if (cmdbuf->nbox == 1)
L
Linus Torvalds 已提交
2743 2744
		cmdbuf->nbox = 0;

D
Dave Airlie 已提交
2745
      out:
L
Linus Torvalds 已提交
2746 2747 2748 2749 2750
	cmdbuf->buf += cmdsz * 4;
	cmdbuf->bufsz -= cmdsz * 4;
	return 0;
}

D
Dave Airlie 已提交
2751
static int radeon_emit_wait(drm_device_t * dev, int flags)
L
Linus Torvalds 已提交
2752 2753 2754 2755 2756 2757 2758
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	RING_LOCALS;

	DRM_DEBUG("%s: %x\n", __FUNCTION__, flags);
	switch (flags) {
	case RADEON_WAIT_2D:
D
Dave Airlie 已提交
2759 2760
		BEGIN_RING(2);
		RADEON_WAIT_UNTIL_2D_IDLE();
L
Linus Torvalds 已提交
2761 2762 2763
		ADVANCE_RING();
		break;
	case RADEON_WAIT_3D:
D
Dave Airlie 已提交
2764 2765
		BEGIN_RING(2);
		RADEON_WAIT_UNTIL_3D_IDLE();
L
Linus Torvalds 已提交
2766 2767
		ADVANCE_RING();
		break;
D
Dave Airlie 已提交
2768 2769 2770
	case RADEON_WAIT_2D | RADEON_WAIT_3D:
		BEGIN_RING(2);
		RADEON_WAIT_UNTIL_IDLE();
L
Linus Torvalds 已提交
2771 2772 2773 2774 2775 2776 2777 2778 2779
		ADVANCE_RING();
		break;
	default:
		return DRM_ERR(EINVAL);
	}

	return 0;
}

D
Dave Airlie 已提交
2780
static int radeon_cp_cmdbuf(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_file_t *filp_priv;
	drm_device_dma_t *dma = dev->dma;
	drm_buf_t *buf = NULL;
	int idx;
	drm_radeon_cmd_buffer_t cmdbuf;
	drm_radeon_cmd_header_t header;
	int orig_nbox, orig_bufsz;
D
Dave Airlie 已提交
2791
	char *kbuf = NULL;
L
Linus Torvalds 已提交
2792

D
Dave Airlie 已提交
2793
	LOCK_TEST_WITH_RETURN(dev, filp);
L
Linus Torvalds 已提交
2794

D
Dave Airlie 已提交
2795 2796
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
2797 2798 2799
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2800
	DRM_GET_PRIV_WITH_RETURN(filp_priv, filp);
L
Linus Torvalds 已提交
2801

D
Dave Airlie 已提交
2802 2803 2804
	DRM_COPY_FROM_USER_IOCTL(cmdbuf,
				 (drm_radeon_cmd_buffer_t __user *) data,
				 sizeof(cmdbuf));
L
Linus Torvalds 已提交
2805

D
Dave Airlie 已提交
2806 2807
	RING_SPACE_TEST_WITH_RETURN(dev_priv);
	VB_AGE_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
2808

D
Dave Airlie 已提交
2809
	if (cmdbuf.bufsz > 64 * 1024 || cmdbuf.bufsz < 0) {
L
Linus Torvalds 已提交
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
		return DRM_ERR(EINVAL);
	}

	/* 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.
	 */
	orig_bufsz = cmdbuf.bufsz;
	if (orig_bufsz != 0) {
		kbuf = drm_alloc(cmdbuf.bufsz, DRM_MEM_DRIVER);
		if (kbuf == NULL)
			return DRM_ERR(ENOMEM);
		if (DRM_COPY_FROM_USER(kbuf, cmdbuf.buf, cmdbuf.bufsz)) {
			drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
			return DRM_ERR(EFAULT);
		}
		cmdbuf.buf = kbuf;
	}

	orig_nbox = cmdbuf.nbox;

D
Dave Airlie 已提交
2831
	if (dev_priv->microcode_version == UCODE_R300) {
D
Dave Airlie 已提交
2832
		int temp;
D
Dave Airlie 已提交
2833 2834
		temp = r300_do_cp_cmdbuf(dev, filp, filp_priv, &cmdbuf);

D
Dave Airlie 已提交
2835 2836
		if (orig_bufsz != 0)
			drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
D
Dave Airlie 已提交
2837

D
Dave Airlie 已提交
2838 2839 2840 2841
		return temp;
	}

	/* microcode_version != r300 */
D
Dave Airlie 已提交
2842
	while (cmdbuf.bufsz >= sizeof(header)) {
L
Linus Torvalds 已提交
2843 2844 2845 2846 2847 2848

		header.i = *(int *)cmdbuf.buf;
		cmdbuf.buf += sizeof(header);
		cmdbuf.bufsz -= sizeof(header);

		switch (header.header.cmd_type) {
D
Dave Airlie 已提交
2849
		case RADEON_CMD_PACKET:
L
Linus Torvalds 已提交
2850
			DRM_DEBUG("RADEON_CMD_PACKET\n");
D
Dave Airlie 已提交
2851 2852
			if (radeon_emit_packets
			    (dev_priv, filp_priv, header, &cmdbuf)) {
L
Linus Torvalds 已提交
2853 2854 2855 2856 2857 2858 2859
				DRM_ERROR("radeon_emit_packets failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_SCALARS:
			DRM_DEBUG("RADEON_CMD_SCALARS\n");
D
Dave Airlie 已提交
2860
			if (radeon_emit_scalars(dev_priv, header, &cmdbuf)) {
L
Linus Torvalds 已提交
2861 2862 2863 2864 2865 2866 2867
				DRM_ERROR("radeon_emit_scalars failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_VECTORS:
			DRM_DEBUG("RADEON_CMD_VECTORS\n");
D
Dave Airlie 已提交
2868
			if (radeon_emit_vectors(dev_priv, header, &cmdbuf)) {
L
Linus Torvalds 已提交
2869 2870 2871 2872 2873 2874 2875 2876
				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 已提交
2877 2878 2879
			if (idx < 0 || idx >= dma->buf_count) {
				DRM_ERROR("buffer index %d (of %d max)\n",
					  idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
2880 2881 2882 2883
				goto err;
			}

			buf = dma->buflist[idx];
D
Dave Airlie 已提交
2884 2885 2886
			if (buf->filp != filp || buf->pending) {
				DRM_ERROR("bad buffer %p %p %d\n",
					  buf->filp, filp, buf->pending);
L
Linus Torvalds 已提交
2887 2888 2889
				goto err;
			}

D
Dave Airlie 已提交
2890
			radeon_cp_discard_buffer(dev, buf);
L
Linus Torvalds 已提交
2891 2892 2893 2894
			break;

		case RADEON_CMD_PACKET3:
			DRM_DEBUG("RADEON_CMD_PACKET3\n");
D
Dave Airlie 已提交
2895
			if (radeon_emit_packet3(dev, filp_priv, &cmdbuf)) {
L
Linus Torvalds 已提交
2896 2897 2898 2899 2900 2901 2902
				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 已提交
2903 2904
			if (radeon_emit_packet3_cliprect
			    (dev, filp_priv, &cmdbuf, orig_nbox)) {
L
Linus Torvalds 已提交
2905 2906 2907 2908 2909 2910 2911
				DRM_ERROR("radeon_emit_packet3_clip failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_SCALARS2:
			DRM_DEBUG("RADEON_CMD_SCALARS2\n");
D
Dave Airlie 已提交
2912
			if (radeon_emit_scalars2(dev_priv, header, &cmdbuf)) {
L
Linus Torvalds 已提交
2913 2914 2915 2916 2917 2918 2919
				DRM_ERROR("radeon_emit_scalars2 failed\n");
				goto err;
			}
			break;

		case RADEON_CMD_WAIT:
			DRM_DEBUG("RADEON_CMD_WAIT\n");
D
Dave Airlie 已提交
2920
			if (radeon_emit_wait(dev, header.wait.flags)) {
L
Linus Torvalds 已提交
2921 2922 2923 2924 2925
				DRM_ERROR("radeon_emit_wait failed\n");
				goto err;
			}
			break;
		default:
D
Dave Airlie 已提交
2926
			DRM_ERROR("bad cmd_type %d at %p\n",
L
Linus Torvalds 已提交
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
				  header.header.cmd_type,
				  cmdbuf.buf - sizeof(header));
			goto err;
		}
	}

	if (orig_bufsz != 0)
		drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);

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

D
Dave Airlie 已提交
2940
      err:
L
Linus Torvalds 已提交
2941 2942 2943 2944 2945
	if (orig_bufsz != 0)
		drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
	return DRM_ERR(EINVAL);
}

D
Dave Airlie 已提交
2946
static int radeon_cp_getparam(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
2947 2948 2949 2950 2951 2952
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_getparam_t param;
	int value;

D
Dave Airlie 已提交
2953 2954
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
2955 2956 2957
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
2958 2959
	DRM_COPY_FROM_USER_IOCTL(param, (drm_radeon_getparam_t __user *) data,
				 sizeof(param));
L
Linus Torvalds 已提交
2960

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

D
Dave Airlie 已提交
2963
	switch (param.param) {
L
Linus Torvalds 已提交
2964 2965 2966 2967 2968
	case RADEON_PARAM_GART_BUFFER_OFFSET:
		value = dev_priv->gart_buffers_offset;
		break;
	case RADEON_PARAM_LAST_FRAME:
		dev_priv->stats.last_frame_reads++;
D
Dave Airlie 已提交
2969
		value = GET_SCRATCH(0);
L
Linus Torvalds 已提交
2970 2971
		break;
	case RADEON_PARAM_LAST_DISPATCH:
D
Dave Airlie 已提交
2972
		value = GET_SCRATCH(1);
L
Linus Torvalds 已提交
2973 2974 2975
		break;
	case RADEON_PARAM_LAST_CLEAR:
		dev_priv->stats.last_clear_reads++;
D
Dave Airlie 已提交
2976
		value = GET_SCRATCH(2);
L
Linus Torvalds 已提交
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
		break;
	case RADEON_PARAM_IRQ_NR:
		value = dev->irq;
		break;
	case RADEON_PARAM_GART_BASE:
		value = dev_priv->gart_vm_start;
		break;
	case RADEON_PARAM_REGISTER_HANDLE:
		value = dev_priv->mmio_offset;
		break;
	case RADEON_PARAM_STATUS_HANDLE:
		value = dev_priv->ring_rptr_offset;
		break;
#if BITS_PER_LONG == 32
D
Dave Airlie 已提交
2991 2992 2993 2994 2995 2996 2997 2998 2999
		/*
		 * 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
		 * Michel Dnzer, the ioctl() is only used on embedded platforms, so
		 * 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 已提交
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
	case RADEON_PARAM_SAREA_HANDLE:
		/* The lock is the first dword in the sarea. */
		value = (long)dev->lock.hw_lock;
		break;
#endif
	case RADEON_PARAM_GART_TEX_HANDLE:
		value = dev_priv->gart_textures_offset;
		break;
	default:
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
3012 3013
	if (DRM_COPY_TO_USER(param.value, &value, sizeof(int))) {
		DRM_ERROR("copy_to_user\n");
L
Linus Torvalds 已提交
3014 3015
		return DRM_ERR(EFAULT);
	}
D
Dave Airlie 已提交
3016

L
Linus Torvalds 已提交
3017 3018 3019
	return 0;
}

D
Dave Airlie 已提交
3020 3021
static int radeon_cp_setparam(DRM_IOCTL_ARGS)
{
L
Linus Torvalds 已提交
3022 3023 3024 3025 3026 3027
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_file_t *filp_priv;
	drm_radeon_setparam_t sp;
	struct drm_radeon_driver_file_fields *radeon_priv;

D
Dave Airlie 已提交
3028 3029 3030
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
3031 3032
	}

D
Dave Airlie 已提交
3033
	DRM_GET_PRIV_WITH_RETURN(filp_priv, filp);
L
Linus Torvalds 已提交
3034

D
Dave Airlie 已提交
3035 3036
	DRM_COPY_FROM_USER_IOCTL(sp, (drm_radeon_setparam_t __user *) data,
				 sizeof(sp));
L
Linus Torvalds 已提交
3037

D
Dave Airlie 已提交
3038
	switch (sp.param) {
L
Linus Torvalds 已提交
3039 3040 3041 3042 3043 3044
	case RADEON_SETPARAM_FB_LOCATION:
		radeon_priv = filp_priv->driver_priv;
		radeon_priv->radeon_fb_delta = dev_priv->fb_location - sp.value;
		break;
	case RADEON_SETPARAM_SWITCH_TILING:
		if (sp.value == 0) {
D
Dave Airlie 已提交
3045
			DRM_DEBUG("color tiling disabled\n");
L
Linus Torvalds 已提交
3046 3047 3048
			dev_priv->front_pitch_offset &= ~RADEON_DST_TILE_MACRO;
			dev_priv->back_pitch_offset &= ~RADEON_DST_TILE_MACRO;
			dev_priv->sarea_priv->tiling_enabled = 0;
D
Dave Airlie 已提交
3049 3050
		} else if (sp.value == 1) {
			DRM_DEBUG("color tiling enabled\n");
L
Linus Torvalds 已提交
3051 3052 3053 3054
			dev_priv->front_pitch_offset |= RADEON_DST_TILE_MACRO;
			dev_priv->back_pitch_offset |= RADEON_DST_TILE_MACRO;
			dev_priv->sarea_priv->tiling_enabled = 1;
		}
D
Dave Airlie 已提交
3055
		break;
3056 3057 3058
	case RADEON_SETPARAM_PCIGART_LOCATION:
		dev_priv->pcigart_offset = sp.value;
		break;
L
Linus Torvalds 已提交
3059
	default:
D
Dave Airlie 已提交
3060 3061
		DRM_DEBUG("Invalid parameter %d\n", sp.param);
		return DRM_ERR(EINVAL);
L
Linus Torvalds 已提交
3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
	}

	return 0;
}

/* When a client dies:
 *    - Check for and clean up flipped page state
 *    - Free any alloced GART memory.
 *
 * DRM infrastructure takes care of reclaiming dma buffers.
 */
D
Dave Airlie 已提交
3073
void radeon_driver_prerelease(drm_device_t * dev, DRMFILE filp)
L
Linus Torvalds 已提交
3074
{
D
Dave Airlie 已提交
3075 3076 3077 3078 3079 3080 3081
	if (dev->dev_private) {
		drm_radeon_private_t *dev_priv = dev->dev_private;
		if (dev_priv->page_flipping) {
			radeon_do_cleanup_pageflip(dev);
		}
		radeon_mem_release(filp, dev_priv->gart_heap);
		radeon_mem_release(filp, dev_priv->fb_heap);
L
Linus Torvalds 已提交
3082
		radeon_surfaces_release(filp, dev_priv);
D
Dave Airlie 已提交
3083
	}
L
Linus Torvalds 已提交
3084 3085
}

D
Dave Airlie 已提交
3086
void radeon_driver_pretakedown(drm_device_t * dev)
L
Linus Torvalds 已提交
3087 3088 3089 3090
{
	radeon_do_release(dev);
}

D
Dave Airlie 已提交
3091
int radeon_driver_open_helper(drm_device_t * dev, drm_file_t * filp_priv)
L
Linus Torvalds 已提交
3092 3093 3094
{
	drm_radeon_private_t *dev_priv = dev->dev_private;
	struct drm_radeon_driver_file_fields *radeon_priv;
D
Dave Airlie 已提交
3095 3096 3097 3098 3099

	radeon_priv =
	    (struct drm_radeon_driver_file_fields *)
	    drm_alloc(sizeof(*radeon_priv), DRM_MEM_FILES);

L
Linus Torvalds 已提交
3100 3101 3102 3103
	if (!radeon_priv)
		return -ENOMEM;

	filp_priv->driver_priv = radeon_priv;
D
Dave Airlie 已提交
3104
	if (dev_priv)
L
Linus Torvalds 已提交
3105 3106 3107 3108 3109 3110
		radeon_priv->radeon_fb_delta = dev_priv->fb_location;
	else
		radeon_priv->radeon_fb_delta = 0;
	return 0;
}

D
Dave Airlie 已提交
3111
void radeon_driver_free_filp_priv(drm_device_t * dev, drm_file_t * filp_priv)
L
Linus Torvalds 已提交
3112
{
D
Dave Airlie 已提交
3113 3114 3115 3116
	struct drm_radeon_driver_file_fields *radeon_priv =
	    filp_priv->driver_priv;

	drm_free(radeon_priv, sizeof(*radeon_priv), DRM_MEM_FILES);
L
Linus Torvalds 已提交
3117 3118 3119
}

drm_ioctl_desc_t radeon_ioctls[] = {
D
Dave Airlie 已提交
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
	[DRM_IOCTL_NR(DRM_RADEON_CP_INIT)] = {radeon_cp_init, 1, 1},
	[DRM_IOCTL_NR(DRM_RADEON_CP_START)] = {radeon_cp_start, 1, 1},
	[DRM_IOCTL_NR(DRM_RADEON_CP_STOP)] = {radeon_cp_stop, 1, 1},
	[DRM_IOCTL_NR(DRM_RADEON_CP_RESET)] = {radeon_cp_reset, 1, 1},
	[DRM_IOCTL_NR(DRM_RADEON_CP_IDLE)] = {radeon_cp_idle, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_CP_RESUME)] = {radeon_cp_resume, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_RESET)] = {radeon_engine_reset, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_FULLSCREEN)] = {radeon_fullscreen, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_SWAP)] = {radeon_cp_swap, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_CLEAR)] = {radeon_cp_clear, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_VERTEX)] = {radeon_cp_vertex, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_INDICES)] = {radeon_cp_indices, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_TEXTURE)] = {radeon_cp_texture, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_STIPPLE)] = {radeon_cp_stipple, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_INDIRECT)] = {radeon_cp_indirect, 1, 1},
	[DRM_IOCTL_NR(DRM_RADEON_VERTEX2)] = {radeon_cp_vertex2, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_CMDBUF)] = {radeon_cp_cmdbuf, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_GETPARAM)] = {radeon_cp_getparam, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_FLIP)] = {radeon_cp_flip, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_ALLOC)] = {radeon_mem_alloc, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_FREE)] = {radeon_mem_free, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_INIT_HEAP)] = {radeon_mem_init_heap, 1, 1},
	[DRM_IOCTL_NR(DRM_RADEON_IRQ_EMIT)] = {radeon_irq_emit, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_IRQ_WAIT)] = {radeon_irq_wait, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_SETPARAM)] = {radeon_cp_setparam, 1, 0},
	[DRM_IOCTL_NR(DRM_RADEON_SURF_ALLOC)] = {radeon_surface_alloc, 1, 0},
D
Dave Airlie 已提交
3146
	[DRM_IOCTL_NR(DRM_RADEON_SURF_FREE)] = {radeon_surface_free, 1, 0}
L
Linus Torvalds 已提交
3147 3148 3149
};

int radeon_max_ioctl = DRM_ARRAY_SIZE(radeon_ioctls);