blob: cc5316dcf5802e602567ab21c52f688e322a4bea [file] [log] [blame]
Dave Airlied985c102006-01-02 21:32:48 +11001/* radeon_state.c -- State support for Radeon -*- linux-c -*- */
2/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Gareth Hughes <gareth@valinux.com>
27 * Kevin E. Martin <martin@valinux.com>
28 */
29
30#include "drmP.h"
31#include "drm.h"
Pauli Nieminenb4fe9452010-02-01 19:11:16 +020032#include "drm_buffer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "drm_sarea.h"
34#include "radeon_drm.h"
35#include "radeon_drv.h"
36
37/* ================================================================
38 * Helper functions for client state checking and fixup
39 */
40
Dave Airlieb5e89ed2005-09-25 14:28:13 +100041static __inline__ int radeon_check_and_fixup_offset(drm_radeon_private_t *
42 dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +100043 struct drm_file * file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +100044 u32 *offset)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100045{
Michel Daenzer214ff132006-09-22 04:12:11 +100046 u64 off = *offset;
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110047 u32 fb_end = dev_priv->fb_location + dev_priv->fb_size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct drm_radeon_driver_file_fields *radeon_priv;
49
Dave Airlied5ea7022006-03-19 19:37:55 +110050 /* Hrm ... the story of the offset ... So this function converts
51 * the various ideas of what userland clients might have for an
52 * offset in the card address space into an offset into the card
53 * address space :) So with a sane client, it should just keep
54 * the value intact and just do some boundary checking. However,
55 * not all clients are sane. Some older clients pass us 0 based
56 * offsets relative to the start of the framebuffer and some may
57 * assume the AGP aperture it appended to the framebuffer, so we
58 * try to detect those cases and fix them up.
59 *
60 * Note: It might be a good idea here to make sure the offset lands
61 * in some "allowed" area to protect things like the PCIE GART...
62 */
63
64 /* First, the best case, the offset already lands in either the
65 * framebuffer or the GART mapped space
66 */
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110067 if (radeon_check_offset(dev_priv, off))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return 0;
69
Dave Airlied5ea7022006-03-19 19:37:55 +110070 /* Ok, that didn't happen... now check if we have a zero based
71 * offset that fits in the framebuffer + gart space, apply the
72 * magic offset we get from SETPARAM or calculated from fb_location
73 */
74 if (off < (dev_priv->fb_size + dev_priv->gart_size)) {
Eric Anholt6c340ea2007-08-25 20:23:09 +100075 radeon_priv = file_priv->driver_priv;
Dave Airlied5ea7022006-03-19 19:37:55 +110076 off += radeon_priv->radeon_fb_delta;
77 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Dave Airlied5ea7022006-03-19 19:37:55 +110079 /* Finally, assume we aimed at a GART offset if beyond the fb */
Michel Daenzer214ff132006-09-22 04:12:11 +100080 if (off > fb_end)
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110081 off = off - fb_end - 1 + dev_priv->gart_vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Dave Airlied5ea7022006-03-19 19:37:55 +110083 /* Now recheck and fail if out of bounds */
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110084 if (radeon_check_offset(dev_priv, off)) {
Michel Daenzer214ff132006-09-22 04:12:11 +100085 DRM_DEBUG("offset fixed up to 0x%x\n", (unsigned int)off);
Dave Airlied5ea7022006-03-19 19:37:55 +110086 *offset = off;
87 return 0;
88 }
Eric Anholt20caafa2007-08-25 19:22:43 +100089 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092static __inline__ int radeon_check_and_fixup_packets(drm_radeon_private_t *
93 dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +100094 struct drm_file *file_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +020095 int id, struct drm_buffer *buf)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100096{
Pauli Nieminenb4fe9452010-02-01 19:11:16 +020097 u32 *data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 switch (id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 case RADEON_EMIT_PP_MISC:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200101 data = drm_buffer_pointer_to_dword(buf,
102 (RADEON_RB3D_DEPTHOFFSET - RADEON_PP_MISC) / 4);
103
104 if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000106 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Dave Airlie566d84d2010-02-24 17:17:13 +1000108 dev_priv->have_z_offset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
110
111 case RADEON_EMIT_PP_CNTL:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200112 data = drm_buffer_pointer_to_dword(buf,
113 (RADEON_RB3D_COLOROFFSET - RADEON_PP_CNTL) / 4);
114
115 if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 DRM_ERROR("Invalid colour buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 break;
120
121 case R200_EMIT_PP_TXOFFSET_0:
122 case R200_EMIT_PP_TXOFFSET_1:
123 case R200_EMIT_PP_TXOFFSET_2:
124 case R200_EMIT_PP_TXOFFSET_3:
125 case R200_EMIT_PP_TXOFFSET_4:
126 case R200_EMIT_PP_TXOFFSET_5:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200127 data = drm_buffer_pointer_to_dword(buf, 0);
128 if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 DRM_ERROR("Invalid R200 texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 break;
133
134 case RADEON_EMIT_PP_TXFILTER_0:
135 case RADEON_EMIT_PP_TXFILTER_1:
136 case RADEON_EMIT_PP_TXFILTER_2:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200137 data = drm_buffer_pointer_to_dword(buf,
138 (RADEON_PP_TXOFFSET_0 - RADEON_PP_TXFILTER_0) / 4);
139 if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 DRM_ERROR("Invalid R100 texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 break;
144
145 case R200_EMIT_PP_CUBIC_OFFSETS_0:
146 case R200_EMIT_PP_CUBIC_OFFSETS_1:
147 case R200_EMIT_PP_CUBIC_OFFSETS_2:
148 case R200_EMIT_PP_CUBIC_OFFSETS_3:
149 case R200_EMIT_PP_CUBIC_OFFSETS_4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 case R200_EMIT_PP_CUBIC_OFFSETS_5:{
151 int i;
152 for (i = 0; i < 5; i++) {
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200153 data = drm_buffer_pointer_to_dword(buf, i);
Dave Airlied985c102006-01-02 21:32:48 +1100154 if (radeon_check_and_fixup_offset(dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000155 file_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200156 data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157 DRM_ERROR
158 ("Invalid R200 cubic texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000159 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 case RADEON_EMIT_PP_CUBIC_OFFSETS_T0:
166 case RADEON_EMIT_PP_CUBIC_OFFSETS_T1:
167 case RADEON_EMIT_PP_CUBIC_OFFSETS_T2:{
168 int i;
169 for (i = 0; i < 5; i++) {
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200170 data = drm_buffer_pointer_to_dword(buf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (radeon_check_and_fixup_offset(dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000172 file_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200173 data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 DRM_ERROR
175 ("Invalid R100 cubic texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 }
179 }
180 break;
181
Roland Scheidegger18f29052006-08-30 23:17:55 +0100182 case R200_EMIT_VAP_CTL:{
183 RING_LOCALS;
184 BEGIN_RING(2);
185 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
186 ADVANCE_RING();
187 }
188 break;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 case RADEON_EMIT_RB3D_COLORPITCH:
191 case RADEON_EMIT_RE_LINE_PATTERN:
192 case RADEON_EMIT_SE_LINE_WIDTH:
193 case RADEON_EMIT_PP_LUM_MATRIX:
194 case RADEON_EMIT_PP_ROT_MATRIX_0:
195 case RADEON_EMIT_RB3D_STENCILREFMASK:
196 case RADEON_EMIT_SE_VPORT_XSCALE:
197 case RADEON_EMIT_SE_CNTL:
198 case RADEON_EMIT_SE_CNTL_STATUS:
199 case RADEON_EMIT_RE_MISC:
200 case RADEON_EMIT_PP_BORDER_COLOR_0:
201 case RADEON_EMIT_PP_BORDER_COLOR_1:
202 case RADEON_EMIT_PP_BORDER_COLOR_2:
203 case RADEON_EMIT_SE_ZBIAS_FACTOR:
204 case RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT:
205 case RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED:
206 case R200_EMIT_PP_TXCBLEND_0:
207 case R200_EMIT_PP_TXCBLEND_1:
208 case R200_EMIT_PP_TXCBLEND_2:
209 case R200_EMIT_PP_TXCBLEND_3:
210 case R200_EMIT_PP_TXCBLEND_4:
211 case R200_EMIT_PP_TXCBLEND_5:
212 case R200_EMIT_PP_TXCBLEND_6:
213 case R200_EMIT_PP_TXCBLEND_7:
214 case R200_EMIT_TCL_LIGHT_MODEL_CTL_0:
215 case R200_EMIT_TFACTOR_0:
216 case R200_EMIT_VTX_FMT_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 case R200_EMIT_MATRIX_SELECT_0:
218 case R200_EMIT_TEX_PROC_CTL_2:
219 case R200_EMIT_TCL_UCP_VERT_BLEND_CTL:
220 case R200_EMIT_PP_TXFILTER_0:
221 case R200_EMIT_PP_TXFILTER_1:
222 case R200_EMIT_PP_TXFILTER_2:
223 case R200_EMIT_PP_TXFILTER_3:
224 case R200_EMIT_PP_TXFILTER_4:
225 case R200_EMIT_PP_TXFILTER_5:
226 case R200_EMIT_VTE_CNTL:
227 case R200_EMIT_OUTPUT_VTX_COMP_SEL:
228 case R200_EMIT_PP_TAM_DEBUG3:
229 case R200_EMIT_PP_CNTL_X:
230 case R200_EMIT_RB3D_DEPTHXY_OFFSET:
231 case R200_EMIT_RE_AUX_SCISSOR_CNTL:
232 case R200_EMIT_RE_SCISSOR_TL_0:
233 case R200_EMIT_RE_SCISSOR_TL_1:
234 case R200_EMIT_RE_SCISSOR_TL_2:
235 case R200_EMIT_SE_VAP_CNTL_STATUS:
236 case R200_EMIT_SE_VTX_STATE_CNTL:
237 case R200_EMIT_RE_POINTSIZE:
238 case R200_EMIT_TCL_INPUT_VTX_VECTOR_ADDR_0:
239 case R200_EMIT_PP_CUBIC_FACES_0:
240 case R200_EMIT_PP_CUBIC_FACES_1:
241 case R200_EMIT_PP_CUBIC_FACES_2:
242 case R200_EMIT_PP_CUBIC_FACES_3:
243 case R200_EMIT_PP_CUBIC_FACES_4:
244 case R200_EMIT_PP_CUBIC_FACES_5:
245 case RADEON_EMIT_PP_TEX_SIZE_0:
246 case RADEON_EMIT_PP_TEX_SIZE_1:
247 case RADEON_EMIT_PP_TEX_SIZE_2:
248 case R200_EMIT_RB3D_BLENDCOLOR:
249 case R200_EMIT_TCL_POINT_SPRITE_CNTL:
250 case RADEON_EMIT_PP_CUBIC_FACES_0:
251 case RADEON_EMIT_PP_CUBIC_FACES_1:
252 case RADEON_EMIT_PP_CUBIC_FACES_2:
253 case R200_EMIT_PP_TRI_PERF_CNTL:
Dave Airlie9d176012005-09-11 19:55:53 +1000254 case R200_EMIT_PP_AFS_0:
255 case R200_EMIT_PP_AFS_1:
256 case R200_EMIT_ATF_TFACTOR:
257 case R200_EMIT_PP_TXCTLALL_0:
258 case R200_EMIT_PP_TXCTLALL_1:
259 case R200_EMIT_PP_TXCTLALL_2:
260 case R200_EMIT_PP_TXCTLALL_3:
261 case R200_EMIT_PP_TXCTLALL_4:
262 case R200_EMIT_PP_TXCTLALL_5:
Dave Airlied6fece02006-06-24 17:04:07 +1000263 case R200_EMIT_VAP_PVS_CNTL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* These packets don't contain memory offsets */
265 break;
266
267 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 DRM_ERROR("Unknown state packet ID %d\n", id);
Eric Anholt20caafa2007-08-25 19:22:43 +1000269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
272 return 0;
273}
274
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275static __inline__ int radeon_check_and_fixup_packet3(drm_radeon_private_t *
276 dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000277 struct drm_file *file_priv,
Dave Airlied985c102006-01-02 21:32:48 +1100278 drm_radeon_kcmd_buffer_t *
279 cmdbuf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280 unsigned int *cmdsz)
281{
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200282 u32 *cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 0);
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000283 u32 offset, narrays;
284 int count, i, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200286 count = ((*cmd & RADEON_CP_PACKET_COUNT_MASK) >> 16);
287 *cmdsz = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200289 if ((*cmd & 0xc0000000) != RADEON_CP_PACKET3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 DRM_ERROR("Not a type 3 packet\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200294 if (4 * *cmdsz > drm_buffer_unprocessed(cmdbuf->buffer)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295 DRM_ERROR("Packet size larger than size of data provided\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000296 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200299 switch (*cmd & 0xff00) {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000300 /* XXX Are there old drivers needing other packets? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000302 case RADEON_3D_DRAW_IMMD:
303 case RADEON_3D_DRAW_VBUF:
304 case RADEON_3D_DRAW_INDX:
305 case RADEON_WAIT_FOR_IDLE:
306 case RADEON_CP_NOP:
307 case RADEON_3D_CLEAR_ZMASK:
308/* case RADEON_CP_NEXT_CHAR:
309 case RADEON_CP_PLY_NEXTSCAN:
310 case RADEON_CP_SET_SCISSORS: */ /* probably safe but will never need them? */
311 /* these packets are safe */
312 break;
313
314 case RADEON_CP_3D_DRAW_IMMD_2:
315 case RADEON_CP_3D_DRAW_VBUF_2:
316 case RADEON_CP_3D_DRAW_INDX_2:
317 case RADEON_3D_CLEAR_HIZ:
318 /* safe but r200 only */
319 if (dev_priv->microcode_version != UCODE_R200) {
320 DRM_ERROR("Invalid 3d packet for r100-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000321 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000322 }
323 break;
324
325 case RADEON_3D_LOAD_VBPNTR:
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000326
327 if (count > 18) { /* 12 arrays max */
328 DRM_ERROR("Too large payload in 3D_LOAD_VBPNTR (count=%d)\n",
329 count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000330 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000331 }
332
333 /* carefully check packet contents */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200334 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
335
336 narrays = *cmd & ~0xc000;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000337 k = 0;
338 i = 2;
339 while ((k < narrays) && (i < (count + 2))) {
340 i++; /* skip attribute field */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200341 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, i);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000342 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200343 cmd)) {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000344 DRM_ERROR
345 ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
346 k, i);
Eric Anholt20caafa2007-08-25 19:22:43 +1000347 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000348 }
349 k++;
350 i++;
351 if (k == narrays)
352 break;
353 /* have one more to process, they come in pairs */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200354 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, i);
355
Eric Anholt6c340ea2007-08-25 20:23:09 +1000356 if (radeon_check_and_fixup_offset(dev_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200357 file_priv, cmd))
Eric Anholt6c340ea2007-08-25 20:23:09 +1000358 {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000359 DRM_ERROR
360 ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
361 k, i);
Eric Anholt20caafa2007-08-25 19:22:43 +1000362 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000363 }
364 k++;
365 i++;
366 }
367 /* do the counts match what we expect ? */
368 if ((k != narrays) || (i != (count + 2))) {
369 DRM_ERROR
370 ("Malformed 3D_LOAD_VBPNTR packet (k=%d i=%d narrays=%d count+1=%d).\n",
371 k, i, narrays, count + 1);
Eric Anholt20caafa2007-08-25 19:22:43 +1000372 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000373 }
374 break;
375
376 case RADEON_3D_RNDR_GEN_INDX_PRIM:
377 if (dev_priv->microcode_version != UCODE_R100) {
378 DRM_ERROR("Invalid 3d packet for r200-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000379 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000380 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200381
382 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
383 if (radeon_check_and_fixup_offset(dev_priv, file_priv, cmd)) {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000384 DRM_ERROR("Invalid rndr_gen_indx offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000385 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000386 }
387 break;
388
389 case RADEON_CP_INDX_BUFFER:
390 if (dev_priv->microcode_version != UCODE_R200) {
391 DRM_ERROR("Invalid 3d packet for r100-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000392 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000393 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200394
395 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
396 if ((*cmd & 0x8000ffff) != 0x80000810) {
397 DRM_ERROR("Invalid indx_buffer reg address %08X\n", *cmd);
Eric Anholt20caafa2007-08-25 19:22:43 +1000398 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000399 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200400 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 2);
401 if (radeon_check_and_fixup_offset(dev_priv, file_priv, cmd)) {
402 DRM_ERROR("Invalid indx_buffer offset is %08X\n", *cmd);
Eric Anholt20caafa2007-08-25 19:22:43 +1000403 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000404 }
405 break;
406
407 case RADEON_CNTL_HOSTDATA_BLT:
408 case RADEON_CNTL_PAINT_MULTI:
409 case RADEON_CNTL_BITBLT_MULTI:
410 /* MSB of opcode: next DWORD GUI_CNTL */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200411 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
412 if (*cmd & (RADEON_GMC_SRC_PITCH_OFFSET_CNTL
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000413 | RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200414 u32 *cmd2 = drm_buffer_pointer_to_dword(cmdbuf->buffer, 2);
415 offset = *cmd2 << 10;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 if (radeon_check_and_fixup_offset
Eric Anholt6c340ea2007-08-25 20:23:09 +1000417 (dev_priv, file_priv, &offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 DRM_ERROR("Invalid first packet offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000419 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200421 *cmd2 = (*cmd2 & 0xffc00000) | offset >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200424 if ((*cmd & RADEON_GMC_SRC_PITCH_OFFSET_CNTL) &&
425 (*cmd & RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
426 u32 *cmd3 = drm_buffer_pointer_to_dword(cmdbuf->buffer, 3);
Jean Delvarec9ff04c2010-05-11 14:01:45 +1000427 offset = *cmd3 << 10;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000428 if (radeon_check_and_fixup_offset
Eric Anholt6c340ea2007-08-25 20:23:09 +1000429 (dev_priv, file_priv, &offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000430 DRM_ERROR("Invalid second packet offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200433 *cmd3 = (*cmd3 & 0xffc00000) | offset >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000435 break;
436
437 default:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200438 DRM_ERROR("Invalid packet type %x\n", *cmd & 0xff00);
Eric Anholt20caafa2007-08-25 19:22:43 +1000439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 return 0;
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/* ================================================================
446 * CP hardware state programming functions
447 */
448
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449static __inline__ void radeon_emit_clip_rect(drm_radeon_private_t * dev_priv,
Dave Airliec60ce622007-07-11 15:27:12 +1000450 struct drm_clip_rect * box)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 RING_LOCALS;
453
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000454 DRM_DEBUG(" box: x1=%d y1=%d x2=%d y2=%d\n",
455 box->x1, box->y1, box->x2, box->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 BEGIN_RING(4);
458 OUT_RING(CP_PACKET0(RADEON_RE_TOP_LEFT, 0));
459 OUT_RING((box->y1 << 16) | box->x1);
460 OUT_RING(CP_PACKET0(RADEON_RE_WIDTH_HEIGHT, 0));
461 OUT_RING(((box->y2 - 1) << 16) | (box->x2 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 ADVANCE_RING();
463}
464
465/* Emit 1.1 state
466 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467static int radeon_emit_state(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000468 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000469 drm_radeon_context_regs_t * ctx,
470 drm_radeon_texture_regs_t * tex,
471 unsigned int dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 DRM_DEBUG("dirty=0x%08x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 if (dirty & RADEON_UPLOAD_CONTEXT) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000477 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 &ctx->rb3d_depthoffset)) {
479 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
Eric Anholt6c340ea2007-08-25 20:23:09 +1000483 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 &ctx->rb3d_coloroffset)) {
485 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000486 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 BEGIN_RING(14);
490 OUT_RING(CP_PACKET0(RADEON_PP_MISC, 6));
491 OUT_RING(ctx->pp_misc);
492 OUT_RING(ctx->pp_fog_color);
493 OUT_RING(ctx->re_solid_color);
494 OUT_RING(ctx->rb3d_blendcntl);
495 OUT_RING(ctx->rb3d_depthoffset);
496 OUT_RING(ctx->rb3d_depthpitch);
497 OUT_RING(ctx->rb3d_zstencilcntl);
498 OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 2));
499 OUT_RING(ctx->pp_cntl);
500 OUT_RING(ctx->rb3d_cntl);
501 OUT_RING(ctx->rb3d_coloroffset);
502 OUT_RING(CP_PACKET0(RADEON_RB3D_COLORPITCH, 0));
503 OUT_RING(ctx->rb3d_colorpitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 ADVANCE_RING();
505 }
506
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 if (dirty & RADEON_UPLOAD_VERTFMT) {
508 BEGIN_RING(2);
509 OUT_RING(CP_PACKET0(RADEON_SE_COORD_FMT, 0));
510 OUT_RING(ctx->se_coord_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 ADVANCE_RING();
512 }
513
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514 if (dirty & RADEON_UPLOAD_LINE) {
515 BEGIN_RING(5);
516 OUT_RING(CP_PACKET0(RADEON_RE_LINE_PATTERN, 1));
517 OUT_RING(ctx->re_line_pattern);
518 OUT_RING(ctx->re_line_state);
519 OUT_RING(CP_PACKET0(RADEON_SE_LINE_WIDTH, 0));
520 OUT_RING(ctx->se_line_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 ADVANCE_RING();
522 }
523
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 if (dirty & RADEON_UPLOAD_BUMPMAP) {
525 BEGIN_RING(5);
526 OUT_RING(CP_PACKET0(RADEON_PP_LUM_MATRIX, 0));
527 OUT_RING(ctx->pp_lum_matrix);
528 OUT_RING(CP_PACKET0(RADEON_PP_ROT_MATRIX_0, 1));
529 OUT_RING(ctx->pp_rot_matrix_0);
530 OUT_RING(ctx->pp_rot_matrix_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ADVANCE_RING();
532 }
533
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 if (dirty & RADEON_UPLOAD_MASKS) {
535 BEGIN_RING(4);
536 OUT_RING(CP_PACKET0(RADEON_RB3D_STENCILREFMASK, 2));
537 OUT_RING(ctx->rb3d_stencilrefmask);
538 OUT_RING(ctx->rb3d_ropcntl);
539 OUT_RING(ctx->rb3d_planemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ADVANCE_RING();
541 }
542
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 if (dirty & RADEON_UPLOAD_VIEWPORT) {
544 BEGIN_RING(7);
545 OUT_RING(CP_PACKET0(RADEON_SE_VPORT_XSCALE, 5));
546 OUT_RING(ctx->se_vport_xscale);
547 OUT_RING(ctx->se_vport_xoffset);
548 OUT_RING(ctx->se_vport_yscale);
549 OUT_RING(ctx->se_vport_yoffset);
550 OUT_RING(ctx->se_vport_zscale);
551 OUT_RING(ctx->se_vport_zoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ADVANCE_RING();
553 }
554
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 if (dirty & RADEON_UPLOAD_SETUP) {
556 BEGIN_RING(4);
557 OUT_RING(CP_PACKET0(RADEON_SE_CNTL, 0));
558 OUT_RING(ctx->se_cntl);
559 OUT_RING(CP_PACKET0(RADEON_SE_CNTL_STATUS, 0));
560 OUT_RING(ctx->se_cntl_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ADVANCE_RING();
562 }
563
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564 if (dirty & RADEON_UPLOAD_MISC) {
565 BEGIN_RING(2);
566 OUT_RING(CP_PACKET0(RADEON_RE_MISC, 0));
567 OUT_RING(ctx->re_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ADVANCE_RING();
569 }
570
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000571 if (dirty & RADEON_UPLOAD_TEX0) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000572 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000573 &tex[0].pp_txoffset)) {
574 DRM_ERROR("Invalid texture offset for unit 0\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 BEGIN_RING(9);
579 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_0, 5));
580 OUT_RING(tex[0].pp_txfilter);
581 OUT_RING(tex[0].pp_txformat);
582 OUT_RING(tex[0].pp_txoffset);
583 OUT_RING(tex[0].pp_txcblend);
584 OUT_RING(tex[0].pp_txablend);
585 OUT_RING(tex[0].pp_tfactor);
586 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_0, 0));
587 OUT_RING(tex[0].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ADVANCE_RING();
589 }
590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 if (dirty & RADEON_UPLOAD_TEX1) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000592 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000593 &tex[1].pp_txoffset)) {
594 DRM_ERROR("Invalid texture offset for unit 1\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000595 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 BEGIN_RING(9);
599 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_1, 5));
600 OUT_RING(tex[1].pp_txfilter);
601 OUT_RING(tex[1].pp_txformat);
602 OUT_RING(tex[1].pp_txoffset);
603 OUT_RING(tex[1].pp_txcblend);
604 OUT_RING(tex[1].pp_txablend);
605 OUT_RING(tex[1].pp_tfactor);
606 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_1, 0));
607 OUT_RING(tex[1].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 ADVANCE_RING();
609 }
610
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 if (dirty & RADEON_UPLOAD_TEX2) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000612 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 &tex[2].pp_txoffset)) {
614 DRM_ERROR("Invalid texture offset for unit 2\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000615 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 BEGIN_RING(9);
619 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_2, 5));
620 OUT_RING(tex[2].pp_txfilter);
621 OUT_RING(tex[2].pp_txformat);
622 OUT_RING(tex[2].pp_txoffset);
623 OUT_RING(tex[2].pp_txcblend);
624 OUT_RING(tex[2].pp_txablend);
625 OUT_RING(tex[2].pp_tfactor);
626 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_2, 0));
627 OUT_RING(tex[2].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ADVANCE_RING();
629 }
630
631 return 0;
632}
633
634/* Emit 1.2 state
635 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636static int radeon_emit_state2(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000637 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 drm_radeon_state_t * state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 RING_LOCALS;
641
642 if (state->dirty & RADEON_UPLOAD_ZBIAS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 BEGIN_RING(3);
644 OUT_RING(CP_PACKET0(RADEON_SE_ZBIAS_FACTOR, 1));
645 OUT_RING(state->context2.se_zbias_factor);
646 OUT_RING(state->context2.se_zbias_constant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ADVANCE_RING();
648 }
649
Eric Anholt6c340ea2007-08-25 20:23:09 +1000650 return radeon_emit_state(dev_priv, file_priv, &state->context,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651 state->tex, state->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/* New (1.3) state mechanism. 3 commands (packet, scalar, vector) in
655 * 1.3 cmdbuffers allow all previous state to be updated as well as
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 * the tcl scalar and vector areas.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658static struct {
659 int start;
660 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 const char *name;
662} packet[RADEON_MAX_STATE_PACKETS] = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 {RADEON_PP_MISC, 7, "RADEON_PP_MISC"},
664 {RADEON_PP_CNTL, 3, "RADEON_PP_CNTL"},
665 {RADEON_RB3D_COLORPITCH, 1, "RADEON_RB3D_COLORPITCH"},
666 {RADEON_RE_LINE_PATTERN, 2, "RADEON_RE_LINE_PATTERN"},
667 {RADEON_SE_LINE_WIDTH, 1, "RADEON_SE_LINE_WIDTH"},
668 {RADEON_PP_LUM_MATRIX, 1, "RADEON_PP_LUM_MATRIX"},
669 {RADEON_PP_ROT_MATRIX_0, 2, "RADEON_PP_ROT_MATRIX_0"},
670 {RADEON_RB3D_STENCILREFMASK, 3, "RADEON_RB3D_STENCILREFMASK"},
671 {RADEON_SE_VPORT_XSCALE, 6, "RADEON_SE_VPORT_XSCALE"},
672 {RADEON_SE_CNTL, 2, "RADEON_SE_CNTL"},
673 {RADEON_SE_CNTL_STATUS, 1, "RADEON_SE_CNTL_STATUS"},
674 {RADEON_RE_MISC, 1, "RADEON_RE_MISC"},
675 {RADEON_PP_TXFILTER_0, 6, "RADEON_PP_TXFILTER_0"},
676 {RADEON_PP_BORDER_COLOR_0, 1, "RADEON_PP_BORDER_COLOR_0"},
677 {RADEON_PP_TXFILTER_1, 6, "RADEON_PP_TXFILTER_1"},
678 {RADEON_PP_BORDER_COLOR_1, 1, "RADEON_PP_BORDER_COLOR_1"},
679 {RADEON_PP_TXFILTER_2, 6, "RADEON_PP_TXFILTER_2"},
680 {RADEON_PP_BORDER_COLOR_2, 1, "RADEON_PP_BORDER_COLOR_2"},
681 {RADEON_SE_ZBIAS_FACTOR, 2, "RADEON_SE_ZBIAS_FACTOR"},
682 {RADEON_SE_TCL_OUTPUT_VTX_FMT, 11, "RADEON_SE_TCL_OUTPUT_VTX_FMT"},
683 {RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED, 17,
684 "RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED"},
685 {R200_PP_TXCBLEND_0, 4, "R200_PP_TXCBLEND_0"},
686 {R200_PP_TXCBLEND_1, 4, "R200_PP_TXCBLEND_1"},
687 {R200_PP_TXCBLEND_2, 4, "R200_PP_TXCBLEND_2"},
688 {R200_PP_TXCBLEND_3, 4, "R200_PP_TXCBLEND_3"},
689 {R200_PP_TXCBLEND_4, 4, "R200_PP_TXCBLEND_4"},
690 {R200_PP_TXCBLEND_5, 4, "R200_PP_TXCBLEND_5"},
691 {R200_PP_TXCBLEND_6, 4, "R200_PP_TXCBLEND_6"},
692 {R200_PP_TXCBLEND_7, 4, "R200_PP_TXCBLEND_7"},
693 {R200_SE_TCL_LIGHT_MODEL_CTL_0, 6, "R200_SE_TCL_LIGHT_MODEL_CTL_0"},
694 {R200_PP_TFACTOR_0, 6, "R200_PP_TFACTOR_0"},
695 {R200_SE_VTX_FMT_0, 4, "R200_SE_VTX_FMT_0"},
696 {R200_SE_VAP_CNTL, 1, "R200_SE_VAP_CNTL"},
697 {R200_SE_TCL_MATRIX_SEL_0, 5, "R200_SE_TCL_MATRIX_SEL_0"},
698 {R200_SE_TCL_TEX_PROC_CTL_2, 5, "R200_SE_TCL_TEX_PROC_CTL_2"},
699 {R200_SE_TCL_UCP_VERT_BLEND_CTL, 1, "R200_SE_TCL_UCP_VERT_BLEND_CTL"},
700 {R200_PP_TXFILTER_0, 6, "R200_PP_TXFILTER_0"},
701 {R200_PP_TXFILTER_1, 6, "R200_PP_TXFILTER_1"},
702 {R200_PP_TXFILTER_2, 6, "R200_PP_TXFILTER_2"},
703 {R200_PP_TXFILTER_3, 6, "R200_PP_TXFILTER_3"},
704 {R200_PP_TXFILTER_4, 6, "R200_PP_TXFILTER_4"},
705 {R200_PP_TXFILTER_5, 6, "R200_PP_TXFILTER_5"},
706 {R200_PP_TXOFFSET_0, 1, "R200_PP_TXOFFSET_0"},
707 {R200_PP_TXOFFSET_1, 1, "R200_PP_TXOFFSET_1"},
708 {R200_PP_TXOFFSET_2, 1, "R200_PP_TXOFFSET_2"},
709 {R200_PP_TXOFFSET_3, 1, "R200_PP_TXOFFSET_3"},
710 {R200_PP_TXOFFSET_4, 1, "R200_PP_TXOFFSET_4"},
711 {R200_PP_TXOFFSET_5, 1, "R200_PP_TXOFFSET_5"},
712 {R200_SE_VTE_CNTL, 1, "R200_SE_VTE_CNTL"},
Dave Airlied985c102006-01-02 21:32:48 +1100713 {R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1,
714 "R200_SE_TCL_OUTPUT_VTX_COMP_SEL"},
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 {R200_PP_TAM_DEBUG3, 1, "R200_PP_TAM_DEBUG3"},
716 {R200_PP_CNTL_X, 1, "R200_PP_CNTL_X"},
717 {R200_RB3D_DEPTHXY_OFFSET, 1, "R200_RB3D_DEPTHXY_OFFSET"},
718 {R200_RE_AUX_SCISSOR_CNTL, 1, "R200_RE_AUX_SCISSOR_CNTL"},
719 {R200_RE_SCISSOR_TL_0, 2, "R200_RE_SCISSOR_TL_0"},
720 {R200_RE_SCISSOR_TL_1, 2, "R200_RE_SCISSOR_TL_1"},
721 {R200_RE_SCISSOR_TL_2, 2, "R200_RE_SCISSOR_TL_2"},
722 {R200_SE_VAP_CNTL_STATUS, 1, "R200_SE_VAP_CNTL_STATUS"},
723 {R200_SE_VTX_STATE_CNTL, 1, "R200_SE_VTX_STATE_CNTL"},
724 {R200_RE_POINTSIZE, 1, "R200_RE_POINTSIZE"},
725 {R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0, 4,
726 "R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0"},
727 {R200_PP_CUBIC_FACES_0, 1, "R200_PP_CUBIC_FACES_0"}, /* 61 */
Dave Airlied985c102006-01-02 21:32:48 +1100728 {R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0"}, /* 62 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 {R200_PP_CUBIC_FACES_1, 1, "R200_PP_CUBIC_FACES_1"},
730 {R200_PP_CUBIC_OFFSET_F1_1, 5, "R200_PP_CUBIC_OFFSET_F1_1"},
731 {R200_PP_CUBIC_FACES_2, 1, "R200_PP_CUBIC_FACES_2"},
732 {R200_PP_CUBIC_OFFSET_F1_2, 5, "R200_PP_CUBIC_OFFSET_F1_2"},
733 {R200_PP_CUBIC_FACES_3, 1, "R200_PP_CUBIC_FACES_3"},
734 {R200_PP_CUBIC_OFFSET_F1_3, 5, "R200_PP_CUBIC_OFFSET_F1_3"},
735 {R200_PP_CUBIC_FACES_4, 1, "R200_PP_CUBIC_FACES_4"},
736 {R200_PP_CUBIC_OFFSET_F1_4, 5, "R200_PP_CUBIC_OFFSET_F1_4"},
737 {R200_PP_CUBIC_FACES_5, 1, "R200_PP_CUBIC_FACES_5"},
738 {R200_PP_CUBIC_OFFSET_F1_5, 5, "R200_PP_CUBIC_OFFSET_F1_5"},
739 {RADEON_PP_TEX_SIZE_0, 2, "RADEON_PP_TEX_SIZE_0"},
740 {RADEON_PP_TEX_SIZE_1, 2, "RADEON_PP_TEX_SIZE_1"},
741 {RADEON_PP_TEX_SIZE_2, 2, "RADEON_PP_TEX_SIZE_2"},
742 {R200_RB3D_BLENDCOLOR, 3, "R200_RB3D_BLENDCOLOR"},
743 {R200_SE_TCL_POINT_SPRITE_CNTL, 1, "R200_SE_TCL_POINT_SPRITE_CNTL"},
744 {RADEON_PP_CUBIC_FACES_0, 1, "RADEON_PP_CUBIC_FACES_0"},
745 {RADEON_PP_CUBIC_OFFSET_T0_0, 5, "RADEON_PP_CUBIC_OFFSET_T0_0"},
746 {RADEON_PP_CUBIC_FACES_1, 1, "RADEON_PP_CUBIC_FACES_1"},
747 {RADEON_PP_CUBIC_OFFSET_T1_0, 5, "RADEON_PP_CUBIC_OFFSET_T1_0"},
748 {RADEON_PP_CUBIC_FACES_2, 1, "RADEON_PP_CUBIC_FACES_2"},
749 {RADEON_PP_CUBIC_OFFSET_T2_0, 5, "RADEON_PP_CUBIC_OFFSET_T2_0"},
750 {R200_PP_TRI_PERF, 2, "R200_PP_TRI_PERF"},
Dave Airlied985c102006-01-02 21:32:48 +1100751 {R200_PP_AFS_0, 32, "R200_PP_AFS_0"}, /* 85 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000752 {R200_PP_AFS_1, 32, "R200_PP_AFS_1"},
753 {R200_PP_TFACTOR_0, 8, "R200_ATF_TFACTOR"},
754 {R200_PP_TXFILTER_0, 8, "R200_PP_TXCTLALL_0"},
755 {R200_PP_TXFILTER_1, 8, "R200_PP_TXCTLALL_1"},
756 {R200_PP_TXFILTER_2, 8, "R200_PP_TXCTLALL_2"},
757 {R200_PP_TXFILTER_3, 8, "R200_PP_TXCTLALL_3"},
758 {R200_PP_TXFILTER_4, 8, "R200_PP_TXCTLALL_4"},
759 {R200_PP_TXFILTER_5, 8, "R200_PP_TXCTLALL_5"},
Dave Airlied6fece02006-06-24 17:04:07 +1000760 {R200_VAP_PVS_CNTL_1, 2, "R200_VAP_PVS_CNTL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761};
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/* ================================================================
764 * Performance monitoring functions
765 */
766
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767static void radeon_clear_box(drm_radeon_private_t * dev_priv,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000768 struct drm_radeon_master_private *master_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 u32 color;
772 RING_LOCALS;
773
Dave Airlie7c1c2872008-11-28 14:22:24 +1000774 x += master_priv->sarea_priv->boxes[0].x1;
775 y += master_priv->sarea_priv->boxes[0].y1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 switch (dev_priv->color_fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 case RADEON_COLOR_FORMAT_RGB565:
779 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 break;
782 case RADEON_COLOR_FORMAT_ARGB8888:
783 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
786 }
787
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 BEGIN_RING(4);
789 RADEON_WAIT_UNTIL_3D_IDLE();
790 OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
791 OUT_RING(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ADVANCE_RING();
793
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 OUT_RING(CP_PACKET3(RADEON_CNTL_PAINT_MULTI, 4));
797 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
798 RADEON_GMC_BRUSH_SOLID_COLOR |
799 (dev_priv->color_fmt << 8) |
800 RADEON_GMC_SRC_DATATYPE_COLOR |
801 RADEON_ROP3_P | RADEON_GMC_CLR_CMP_CNTL_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dave Airlie7c1c2872008-11-28 14:22:24 +1000803 if (master_priv->sarea_priv->pfCurrentPage == 1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 OUT_RING(dev_priv->front_pitch_offset);
805 } else {
806 OUT_RING(dev_priv->back_pitch_offset);
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 OUT_RING((x << 16) | y);
812 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 ADVANCE_RING();
815}
816
Dave Airlie7c1c2872008-11-28 14:22:24 +1000817static void radeon_cp_performance_boxes(drm_radeon_private_t *dev_priv, struct drm_radeon_master_private *master_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 /* Collapse various things into a wait flag -- trying to
820 * guess if userspase slept -- better just to have them tell us.
821 */
822 if (dev_priv->stats.last_frame_reads > 1 ||
823 dev_priv->stats.last_clear_reads > dev_priv->stats.clears) {
824 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
825 }
826
827 if (dev_priv->stats.freelist_loops) {
828 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
829 }
830
831 /* Purple box for page flipping
832 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 if (dev_priv->stats.boxes & RADEON_BOX_FLIP)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000834 radeon_clear_box(dev_priv, master_priv, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* Red box if we have to wait for idle at any point
837 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 if (dev_priv->stats.boxes & RADEON_BOX_WAIT_IDLE)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000839 radeon_clear_box(dev_priv, master_priv, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Blue box: lost context?
842 */
843
844 /* Yellow box for texture swaps
845 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 if (dev_priv->stats.boxes & RADEON_BOX_TEXTURE_LOAD)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000847 radeon_clear_box(dev_priv, master_priv, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* Green box if hardware never idles (as far as we can tell)
850 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 if (!(dev_priv->stats.boxes & RADEON_BOX_DMA_IDLE))
Dave Airlie7c1c2872008-11-28 14:22:24 +1000852 radeon_clear_box(dev_priv, master_priv, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * (not a great measure, easily confused)
856 */
857 if (dev_priv->stats.requested_bufs) {
858 if (dev_priv->stats.requested_bufs > 100)
859 dev_priv->stats.requested_bufs = 100;
860
Dave Airlie7c1c2872008-11-28 14:22:24 +1000861 radeon_clear_box(dev_priv, master_priv, 4, 16,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 dev_priv->stats.requested_bufs, 4,
863 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 memset(&dev_priv->stats, 0, sizeof(dev_priv->stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/* ================================================================
871 * CP command dispatch functions
872 */
873
Dave Airlie84b1fd12007-07-11 15:53:27 +1000874static void radeon_cp_dispatch_clear(struct drm_device * dev,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000875 struct drm_master *master,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 drm_radeon_clear_t * clear,
877 drm_radeon_clear_rect_t * depth_boxes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000880 struct drm_radeon_master_private *master_priv = master->driver_priv;
881 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 drm_radeon_depth_clear_t *depth_clear = &dev_priv->depth_clear;
883 int nbox = sarea_priv->nbox;
Dave Airliec60ce622007-07-11 15:27:12 +1000884 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned int flags = clear->flags;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 u32 rb3d_cntl = 0, rb3d_stencilrefmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int i;
888 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 DRM_DEBUG("flags = 0x%x\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 dev_priv->stats.clears++;
892
Dave Airlie7c1c2872008-11-28 14:22:24 +1000893 if (sarea_priv->pfCurrentPage == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 unsigned int tmp = flags;
895
896 flags &= ~(RADEON_FRONT | RADEON_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 if (tmp & RADEON_FRONT)
898 flags |= RADEON_BACK;
899 if (tmp & RADEON_BACK)
900 flags |= RADEON_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Dave Airlie566d84d2010-02-24 17:17:13 +1000902 if (flags & (RADEON_DEPTH|RADEON_STENCIL)) {
903 if (!dev_priv->have_z_offset)
904 printk_once(KERN_ERR "radeon: illegal depth clear request. Buggy mesa detected - please update.\n");
905 flags &= ~(RADEON_DEPTH | RADEON_STENCIL);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 if (flags & (RADEON_FRONT | RADEON_BACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* Ensure the 3D stream is idle before doing a
913 * 2D fill to clear the front or back buffer.
914 */
915 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916
917 OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
918 OUT_RING(clear->color_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 ADVANCE_RING();
921
922 /* Make sure we restore the 3D state next time.
923 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000924 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int x = pbox[i].x1;
928 int y = pbox[i].y1;
929 int w = pbox[i].x2 - x;
930 int h = pbox[i].y2 - y;
931
Márton Németh3e684ea2008-01-24 15:58:57 +1000932 DRM_DEBUG("%d,%d-%d,%d flags 0x%x\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 x, y, w, h, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 if (flags & RADEON_FRONT) {
936 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 OUT_RING(CP_PACKET3
939 (RADEON_CNTL_PAINT_MULTI, 4));
940 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
941 RADEON_GMC_BRUSH_SOLID_COLOR |
942 (dev_priv->
943 color_fmt << 8) |
944 RADEON_GMC_SRC_DATATYPE_COLOR |
945 RADEON_ROP3_P |
946 RADEON_GMC_CLR_CMP_CNTL_DIS);
947
948 OUT_RING(dev_priv->front_pitch_offset);
949 OUT_RING(clear->clear_color);
950
951 OUT_RING((x << 16) | y);
952 OUT_RING((w << 16) | h);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 ADVANCE_RING();
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000957 if (flags & RADEON_BACK) {
958 BEGIN_RING(6);
959
960 OUT_RING(CP_PACKET3
961 (RADEON_CNTL_PAINT_MULTI, 4));
962 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
963 RADEON_GMC_BRUSH_SOLID_COLOR |
964 (dev_priv->
965 color_fmt << 8) |
966 RADEON_GMC_SRC_DATATYPE_COLOR |
967 RADEON_ROP3_P |
968 RADEON_GMC_CLR_CMP_CNTL_DIS);
969
970 OUT_RING(dev_priv->back_pitch_offset);
971 OUT_RING(clear->clear_color);
972
973 OUT_RING((x << 16) | y);
974 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 ADVANCE_RING();
977 }
978 }
979 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* hyper z clear */
982 /* no docs available, based on reverse engeneering by Stephane Marchesin */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000983 if ((flags & (RADEON_DEPTH | RADEON_STENCIL))
984 && (flags & RADEON_CLEAR_FASTZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 int depthpixperline =
988 dev_priv->depth_fmt ==
989 RADEON_DEPTH_FORMAT_16BIT_INT_Z ? (dev_priv->depth_pitch /
990 2) : (dev_priv->
991 depth_pitch / 4);
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 u32 clearmask;
994
995 u32 tempRB3D_DEPTHCLEARVALUE = clear->clear_depth |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 ((clear->depth_mask & 0xff) << 24);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Make sure we restore the 3D state next time.
999 * we haven't touched any "normal" state - still need this?
1000 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001001 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dave Airlie54a56ac2006-09-22 04:25:09 +10001003 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 && (flags & RADEON_USE_HIERZ)) {
1005 /* FIXME : reverse engineer that for Rx00 cards */
1006 /* FIXME : the mask supposedly contains low-res z values. So can't set
1007 just to the max (0xff? or actually 0x3fff?), need to take z clear
1008 value into account? */
1009 /* pattern seems to work for r100, though get slight
1010 rendering errors with glxgears. If hierz is not enabled for r100,
1011 only 4 bits which indicate clear (15,16,31,32, all zero) matter, the
1012 other ones are ignored, and the same clear mask can be used. That's
1013 very different behaviour than R200 which needs different clear mask
1014 and different number of tiles to clear if hierz is enabled or not !?!
1015 */
1016 clearmask = (0xff << 22) | (0xff << 6) | 0x003f003f;
1017 } else {
1018 /* clear mask : chooses the clearing pattern.
1019 rv250: could be used to clear only parts of macrotiles
1020 (but that would get really complicated...)?
1021 bit 0 and 1 (either or both of them ?!?!) are used to
1022 not clear tile (or maybe one of the bits indicates if the tile is
1023 compressed or not), bit 2 and 3 to not clear tile 1,...,.
1024 Pattern is as follows:
1025 | 0,1 | 4,5 | 8,9 |12,13|16,17|20,21|24,25|28,29|
1026 bits -------------------------------------------------
1027 | 2,3 | 6,7 |10,11|14,15|18,19|22,23|26,27|30,31|
1028 rv100: clearmask covers 2x8 4x1 tiles, but one clear still
1029 covers 256 pixels ?!?
1030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 clearmask = 0x0;
1032 }
1033
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 BEGIN_RING(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 RADEON_WAIT_UNTIL_2D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 OUT_RING_REG(RADEON_RB3D_DEPTHCLEARVALUE,
1037 tempRB3D_DEPTHCLEARVALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* what offset is this exactly ? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 OUT_RING_REG(RADEON_RB3D_ZMASKOFFSET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* need ctlstat, otherwise get some strange black flickering */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 OUT_RING_REG(RADEON_RB3D_ZCACHE_CTLSTAT,
1042 RADEON_RB3D_ZC_FLUSH_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ADVANCE_RING();
1044
1045 for (i = 0; i < nbox; i++) {
1046 int tileoffset, nrtilesx, nrtilesy, j;
1047 /* it looks like r200 needs rv-style clears, at least if hierz is not enabled? */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001048 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 && !(dev_priv->microcode_version == UCODE_R200)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* FIXME : figure this out for r200 (when hierz is enabled). Or
1051 maybe r200 actually doesn't need to put the low-res z value into
1052 the tile cache like r100, but just needs to clear the hi-level z-buffer?
1053 Works for R100, both with hierz and without.
1054 R100 seems to operate on 2x1 8x8 tiles, but...
1055 odd: offset/nrtiles need to be 64 pix (4 block) aligned? Potentially
1056 problematic with resolutions which are not 64 pix aligned? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 tileoffset =
1058 ((pbox[i].y1 >> 3) * depthpixperline +
1059 pbox[i].x1) >> 6;
1060 nrtilesx =
1061 ((pbox[i].x2 & ~63) -
1062 (pbox[i].x1 & ~63)) >> 4;
1063 nrtilesy =
1064 (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 BEGIN_RING(4);
1067 OUT_RING(CP_PACKET3
1068 (RADEON_3D_CLEAR_ZMASK, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* first tile */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 OUT_RING(tileoffset * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 OUT_RING(nrtilesx + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ADVANCE_RING();
1076 tileoffset += depthpixperline >> 6;
1077 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 } else if (dev_priv->microcode_version == UCODE_R200) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* works for rv250. */
1080 /* find first macro tile (8x2 4x4 z-pixels on rv250) */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 tileoffset =
1082 ((pbox[i].y1 >> 3) * depthpixperline +
1083 pbox[i].x1) >> 5;
1084 nrtilesx =
1085 (pbox[i].x2 >> 5) - (pbox[i].x1 >> 5);
1086 nrtilesy =
1087 (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 BEGIN_RING(4);
1090 OUT_RING(CP_PACKET3
1091 (RADEON_3D_CLEAR_ZMASK, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* first tile */
1093 /* judging by the first tile offset needed, could possibly
1094 directly address/clear 4x4 tiles instead of 8x2 * 4x4
1095 macro tiles, though would still need clear mask for
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05001096 right/bottom if truly 4x4 granularity is desired ? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 OUT_RING(tileoffset * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 OUT_RING(nrtilesx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ADVANCE_RING();
1103 tileoffset += depthpixperline >> 5;
1104 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 } else { /* rv 100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* rv100 might not need 64 pix alignment, who knows */
1107 /* offsets are, hmm, weird */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 tileoffset =
1109 ((pbox[i].y1 >> 4) * depthpixperline +
1110 pbox[i].x1) >> 6;
1111 nrtilesx =
1112 ((pbox[i].x2 & ~63) -
1113 (pbox[i].x1 & ~63)) >> 4;
1114 nrtilesy =
1115 (pbox[i].y2 >> 4) - (pbox[i].y1 >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 BEGIN_RING(4);
1118 OUT_RING(CP_PACKET3
1119 (RADEON_3D_CLEAR_ZMASK, 2));
1120 OUT_RING(tileoffset * 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 OUT_RING(nrtilesx + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ADVANCE_RING();
1126 tileoffset += depthpixperline >> 6;
1127 }
1128 }
1129 }
1130
1131 /* TODO don't always clear all hi-level z tiles */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001132 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 && (dev_priv->microcode_version == UCODE_R200)
1134 && (flags & RADEON_USE_HIERZ))
1135 /* r100 and cards without hierarchical z-buffer have no high-level z-buffer */
1136 /* FIXME : the mask supposedly contains low-res z values. So can't set
1137 just to the max (0xff? or actually 0x3fff?), need to take z clear
1138 value into account? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140 BEGIN_RING(4);
1141 OUT_RING(CP_PACKET3(RADEON_3D_CLEAR_HIZ, 2));
1142 OUT_RING(0x0); /* First tile */
1143 OUT_RING(0x3cc0);
1144 OUT_RING((0xff << 22) | (0xff << 6) | 0x003f003f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ADVANCE_RING();
1146 }
1147 }
1148
1149 /* We have to clear the depth and/or stencil buffers by
1150 * rendering a quad into just those buffers. Thus, we have to
1151 * make sure the 3D engine is configured correctly.
1152 */
Dave Airlied985c102006-01-02 21:32:48 +11001153 else if ((dev_priv->microcode_version == UCODE_R200) &&
1154 (flags & (RADEON_DEPTH | RADEON_STENCIL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 int tempPP_CNTL;
1157 int tempRE_CNTL;
1158 int tempRB3D_CNTL;
1159 int tempRB3D_ZSTENCILCNTL;
1160 int tempRB3D_STENCILREFMASK;
1161 int tempRB3D_PLANEMASK;
1162 int tempSE_CNTL;
1163 int tempSE_VTE_CNTL;
1164 int tempSE_VTX_FMT_0;
1165 int tempSE_VTX_FMT_1;
1166 int tempSE_VAP_CNTL;
1167 int tempRE_AUX_SCISSOR_CNTL;
1168
1169 tempPP_CNTL = 0;
1170 tempRE_CNTL = 0;
1171
1172 tempRB3D_CNTL = depth_clear->rb3d_cntl;
1173
1174 tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
1175 tempRB3D_STENCILREFMASK = 0x0;
1176
1177 tempSE_CNTL = depth_clear->se_cntl;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* Disable TCL */
1180
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001181 tempSE_VAP_CNTL = ( /* SE_VAP_CNTL__FORCE_W_TO_ONE_MASK | */
1182 (0x9 <<
1183 SE_VAP_CNTL__VF_MAX_VTX_NUM__SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 tempRB3D_PLANEMASK = 0x0;
1186
1187 tempRE_AUX_SCISSOR_CNTL = 0x0;
1188
1189 tempSE_VTE_CNTL =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 SE_VTE_CNTL__VTX_XY_FMT_MASK | SE_VTE_CNTL__VTX_Z_FMT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 /* Vertex format (X, Y, Z, W) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 tempSE_VTX_FMT_0 =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001194 SE_VTX_FMT_0__VTX_Z0_PRESENT_MASK |
1195 SE_VTX_FMT_0__VTX_W0_PRESENT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 tempSE_VTX_FMT_1 = 0x0;
1197
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001198 /*
1199 * Depth buffer specific enables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
1201 if (flags & RADEON_DEPTH) {
1202 /* Enable depth buffer */
1203 tempRB3D_CNTL |= RADEON_Z_ENABLE;
1204 } else {
1205 /* Disable depth buffer */
1206 tempRB3D_CNTL &= ~RADEON_Z_ENABLE;
1207 }
1208
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * Stencil buffer specific enables
1211 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001212 if (flags & RADEON_STENCIL) {
1213 tempRB3D_CNTL |= RADEON_STENCIL_ENABLE;
1214 tempRB3D_STENCILREFMASK = clear->depth_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 } else {
1216 tempRB3D_CNTL &= ~RADEON_STENCIL_ENABLE;
1217 tempRB3D_STENCILREFMASK = 0x00000000;
1218 }
1219
1220 if (flags & RADEON_USE_COMP_ZBUF) {
1221 tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001222 RADEON_Z_DECOMPRESSION_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 if (flags & RADEON_USE_HIERZ) {
1225 tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
1226 }
1227
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001228 BEGIN_RING(26);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 RADEON_WAIT_UNTIL_2D_IDLE();
1230
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001231 OUT_RING_REG(RADEON_PP_CNTL, tempPP_CNTL);
1232 OUT_RING_REG(R200_RE_CNTL, tempRE_CNTL);
1233 OUT_RING_REG(RADEON_RB3D_CNTL, tempRB3D_CNTL);
1234 OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
1235 OUT_RING_REG(RADEON_RB3D_STENCILREFMASK,
1236 tempRB3D_STENCILREFMASK);
1237 OUT_RING_REG(RADEON_RB3D_PLANEMASK, tempRB3D_PLANEMASK);
1238 OUT_RING_REG(RADEON_SE_CNTL, tempSE_CNTL);
1239 OUT_RING_REG(R200_SE_VTE_CNTL, tempSE_VTE_CNTL);
1240 OUT_RING_REG(R200_SE_VTX_FMT_0, tempSE_VTX_FMT_0);
1241 OUT_RING_REG(R200_SE_VTX_FMT_1, tempSE_VTX_FMT_1);
1242 OUT_RING_REG(R200_SE_VAP_CNTL, tempSE_VAP_CNTL);
1243 OUT_RING_REG(R200_RE_AUX_SCISSOR_CNTL, tempRE_AUX_SCISSOR_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ADVANCE_RING();
1245
1246 /* Make sure we restore the 3D state next time.
1247 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001248 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001250 for (i = 0; i < nbox; i++) {
1251
1252 /* Funny that this should be required --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * sets top-left?
1254 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001255 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257 BEGIN_RING(14);
1258 OUT_RING(CP_PACKET3(R200_3D_DRAW_IMMD_2, 12));
1259 OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
1260 RADEON_PRIM_WALK_RING |
1261 (3 << RADEON_NUM_VERTICES_SHIFT)));
1262 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1263 OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
1264 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1265 OUT_RING(0x3f800000);
1266 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1267 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1268 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1269 OUT_RING(0x3f800000);
1270 OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
1271 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1272 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1273 OUT_RING(0x3f800000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 ADVANCE_RING();
1275 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001276 } else if ((flags & (RADEON_DEPTH | RADEON_STENCIL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 int tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
1279
1280 rb3d_cntl = depth_clear->rb3d_cntl;
1281
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001282 if (flags & RADEON_DEPTH) {
1283 rb3d_cntl |= RADEON_Z_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } else {
1285 rb3d_cntl &= ~RADEON_Z_ENABLE;
1286 }
1287
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001288 if (flags & RADEON_STENCIL) {
1289 rb3d_cntl |= RADEON_STENCIL_ENABLE;
1290 rb3d_stencilrefmask = clear->depth_mask; /* misnamed field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 } else {
1292 rb3d_cntl &= ~RADEON_STENCIL_ENABLE;
1293 rb3d_stencilrefmask = 0x00000000;
1294 }
1295
1296 if (flags & RADEON_USE_COMP_ZBUF) {
1297 tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 RADEON_Z_DECOMPRESSION_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 if (flags & RADEON_USE_HIERZ) {
1301 tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
1302 }
1303
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304 BEGIN_RING(13);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 RADEON_WAIT_UNTIL_2D_IDLE();
1306
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 1));
1308 OUT_RING(0x00000000);
1309 OUT_RING(rb3d_cntl);
1310
1311 OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
1312 OUT_RING_REG(RADEON_RB3D_STENCILREFMASK, rb3d_stencilrefmask);
1313 OUT_RING_REG(RADEON_RB3D_PLANEMASK, 0x00000000);
1314 OUT_RING_REG(RADEON_SE_CNTL, depth_clear->se_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ADVANCE_RING();
1316
1317 /* Make sure we restore the 3D state next time.
1318 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001319 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 for (i = 0; i < nbox; i++) {
1322
1323 /* Funny that this should be required --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * sets top-left?
1325 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001326 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001328 BEGIN_RING(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330 OUT_RING(CP_PACKET3(RADEON_3D_DRAW_IMMD, 13));
1331 OUT_RING(RADEON_VTX_Z_PRESENT |
1332 RADEON_VTX_PKCOLOR_PRESENT);
1333 OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
1334 RADEON_PRIM_WALK_RING |
1335 RADEON_MAOS_ENABLE |
1336 RADEON_VTX_FMT_RADEON_MODE |
1337 (3 << RADEON_NUM_VERTICES_SHIFT)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001339 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1340 OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
1341 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1342 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001344 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1345 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1346 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1347 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001349 OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
1350 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1351 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1352 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 ADVANCE_RING();
1355 }
1356 }
1357
1358 /* Increment the clear counter. The client-side 3D driver must
1359 * wait on this value before performing the clear ioctl. We
1360 * need this because the card's so damned fast...
1361 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001362 sarea_priv->last_clear++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001364 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Dave Airlie7c1c2872008-11-28 14:22:24 +10001366 RADEON_CLEAR_AGE(sarea_priv->last_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 RADEON_WAIT_UNTIL_IDLE();
1368
1369 ADVANCE_RING();
1370}
1371
Dave Airlie7c1c2872008-11-28 14:22:24 +10001372static void radeon_cp_dispatch_swap(struct drm_device *dev, struct drm_master *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001375 struct drm_radeon_master_private *master_priv = master->driver_priv;
1376 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int nbox = sarea_priv->nbox;
Dave Airliec60ce622007-07-11 15:27:12 +10001378 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int i;
1380 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /* Do some trivial performance monitoring...
1384 */
1385 if (dev_priv->do_boxes)
Dave Airlie7c1c2872008-11-28 14:22:24 +10001386 radeon_cp_performance_boxes(dev_priv, master_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* Wait for the 3D stream to idle before dispatching the bitblt.
1389 * This will prevent data corruption between the two streams.
1390 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001391 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 RADEON_WAIT_UNTIL_3D_IDLE();
1394
1395 ADVANCE_RING();
1396
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001397 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int x = pbox[i].x1;
1399 int y = pbox[i].y1;
1400 int w = pbox[i].x2 - x;
1401 int h = pbox[i].y2 - y;
1402
Márton Németh3e684ea2008-01-24 15:58:57 +10001403 DRM_DEBUG("%d,%d-%d,%d\n", x, y, w, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Michel Daenzer3e14a282006-09-22 04:26:35 +10001405 BEGIN_RING(9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Michel Daenzer3e14a282006-09-22 04:26:35 +10001407 OUT_RING(CP_PACKET0(RADEON_DP_GUI_MASTER_CNTL, 0));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
1409 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
1410 RADEON_GMC_BRUSH_NONE |
1411 (dev_priv->color_fmt << 8) |
1412 RADEON_GMC_SRC_DATATYPE_COLOR |
1413 RADEON_ROP3_S |
1414 RADEON_DP_SRC_SOURCE_MEMORY |
1415 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Make this work even if front & back are flipped:
1418 */
Michel Daenzer3e14a282006-09-22 04:26:35 +10001419 OUT_RING(CP_PACKET0(RADEON_SRC_PITCH_OFFSET, 1));
Dave Airlie7c1c2872008-11-28 14:22:24 +10001420 if (sarea_priv->pfCurrentPage == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001421 OUT_RING(dev_priv->back_pitch_offset);
1422 OUT_RING(dev_priv->front_pitch_offset);
1423 } else {
1424 OUT_RING(dev_priv->front_pitch_offset);
1425 OUT_RING(dev_priv->back_pitch_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Michel Daenzer3e14a282006-09-22 04:26:35 +10001428 OUT_RING(CP_PACKET0(RADEON_SRC_X_Y, 2));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001429 OUT_RING((x << 16) | y);
1430 OUT_RING((x << 16) | y);
1431 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 ADVANCE_RING();
1434 }
1435
1436 /* Increment the frame counter. The client-side 3D driver must
1437 * throttle the framerate by waiting for this value before
1438 * performing the swapbuffer ioctl.
1439 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001440 sarea_priv->last_frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001442 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Dave Airlie7c1c2872008-11-28 14:22:24 +10001444 RADEON_FRAME_AGE(sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 RADEON_WAIT_UNTIL_2D_IDLE();
1446
1447 ADVANCE_RING();
1448}
1449
Dave Airlie7c1c2872008-11-28 14:22:24 +10001450void radeon_cp_dispatch_flip(struct drm_device *dev, struct drm_master *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001453 struct drm_radeon_master_private *master_priv = master->driver_priv;
1454 struct drm_sarea *sarea = (struct drm_sarea *)master_priv->sarea->handle;
1455 int offset = (master_priv->sarea_priv->pfCurrentPage == 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001456 ? dev_priv->front_offset : dev_priv->back_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001458 DRM_DEBUG("pfCurrentPage=%d\n",
Dave Airlie7c1c2872008-11-28 14:22:24 +10001459 master_priv->sarea_priv->pfCurrentPage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* Do some trivial performance monitoring...
1462 */
1463 if (dev_priv->do_boxes) {
1464 dev_priv->stats.boxes |= RADEON_BOX_FLIP;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001465 radeon_cp_performance_boxes(dev_priv, master_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
1468 /* Update the frame offsets for both CRTCs
1469 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001470 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001473 OUT_RING_REG(RADEON_CRTC_OFFSET,
1474 ((sarea->frame.y * dev_priv->front_pitch +
1475 sarea->frame.x * (dev_priv->color_fmt - 2)) & ~7)
1476 + offset);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001477 OUT_RING_REG(RADEON_CRTC2_OFFSET, master_priv->sarea_priv->crtc2_base
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001478 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 ADVANCE_RING();
1481
1482 /* Increment the frame counter. The client-side 3D driver must
1483 * throttle the framerate by waiting for this value before
1484 * performing the swapbuffer ioctl.
1485 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001486 master_priv->sarea_priv->last_frame++;
1487 master_priv->sarea_priv->pfCurrentPage =
1488 1 - master_priv->sarea_priv->pfCurrentPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001490 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Dave Airlie7c1c2872008-11-28 14:22:24 +10001492 RADEON_FRAME_AGE(master_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 ADVANCE_RING();
1495}
1496
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497static int bad_prim_vertex_nr(int primitive, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 switch (primitive & RADEON_PRIM_TYPE_MASK) {
1500 case RADEON_PRIM_TYPE_NONE:
1501 case RADEON_PRIM_TYPE_POINT:
1502 return nr < 1;
1503 case RADEON_PRIM_TYPE_LINE:
1504 return (nr & 1) || nr == 0;
1505 case RADEON_PRIM_TYPE_LINE_STRIP:
1506 return nr < 2;
1507 case RADEON_PRIM_TYPE_TRI_LIST:
1508 case RADEON_PRIM_TYPE_3VRT_POINT_LIST:
1509 case RADEON_PRIM_TYPE_3VRT_LINE_LIST:
1510 case RADEON_PRIM_TYPE_RECT_LIST:
1511 return nr % 3 || nr == 0;
1512 case RADEON_PRIM_TYPE_TRI_FAN:
1513 case RADEON_PRIM_TYPE_TRI_STRIP:
1514 return nr < 3;
1515 default:
1516 return 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520typedef struct {
1521 unsigned int start;
1522 unsigned int finish;
1523 unsigned int prim;
1524 unsigned int numverts;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001525 unsigned int offset;
1526 unsigned int vc_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527} drm_radeon_tcl_prim_t;
1528
Dave Airlie84b1fd12007-07-11 15:53:27 +10001529static void radeon_cp_dispatch_vertex(struct drm_device * dev,
Dave Airlie7c1c2872008-11-28 14:22:24 +10001530 struct drm_file *file_priv,
Dave Airlie056219e2007-07-11 16:17:42 +10001531 struct drm_buf * buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001532 drm_radeon_tcl_prim_t * prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001535 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
1536 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int offset = dev_priv->gart_buffers_offset + buf->offset + prim->start;
1538 int numverts = (int)prim->numverts;
1539 int nbox = sarea_priv->nbox;
1540 int i = 0;
1541 RING_LOCALS;
1542
1543 DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d %d verts\n",
1544 prim->prim,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001545 prim->vc_format, prim->start, prim->finish, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001547 if (bad_prim_vertex_nr(prim->prim, prim->numverts)) {
1548 DRM_ERROR("bad prim %x numverts %d\n",
1549 prim->prim, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return;
1551 }
1552
1553 do {
1554 /* Emit the next cliprect */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001555 if (i < nbox) {
1556 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
1559 /* Emit the vertex buffer rendering commands */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 BEGIN_RING(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001562 OUT_RING(CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, 3));
1563 OUT_RING(offset);
1564 OUT_RING(numverts);
1565 OUT_RING(prim->vc_format);
1566 OUT_RING(prim->prim | RADEON_PRIM_WALK_LIST |
1567 RADEON_COLOR_ORDER_RGBA |
1568 RADEON_VTX_FMT_RADEON_MODE |
1569 (numverts << RADEON_NUM_VERTICES_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 ADVANCE_RING();
1572
1573 i++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001574 } while (i < nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001577void radeon_cp_discard_buffer(struct drm_device *dev, struct drm_master *master, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001580 struct drm_radeon_master_private *master_priv = master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1582 RING_LOCALS;
1583
Dave Airlie7c1c2872008-11-28 14:22:24 +10001584 buf_priv->age = ++master_priv->sarea_priv->last_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* Emit the vertex buffer age */
Alex Deucherc05ce082009-02-24 16:22:29 -05001587 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
1588 BEGIN_RING(3);
1589 R600_DISPATCH_AGE(buf_priv->age);
1590 ADVANCE_RING();
1591 } else {
1592 BEGIN_RING(2);
1593 RADEON_DISPATCH_AGE(buf_priv->age);
1594 ADVANCE_RING();
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 buf->pending = 1;
1598 buf->used = 0;
1599}
1600
Dave Airlie84b1fd12007-07-11 15:53:27 +10001601static void radeon_cp_dispatch_indirect(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001602 struct drm_buf * buf, int start, int end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 drm_radeon_private_t *dev_priv = dev->dev_private;
1605 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001606 DRM_DEBUG("buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001608 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int offset = (dev_priv->gart_buffers_offset
1610 + buf->offset + start);
1611 int dwords = (end - start + 3) / sizeof(u32);
1612
1613 /* Indirect buffer data must be an even number of
1614 * dwords, so if we've been given an odd number we must
1615 * pad the data with a Type-2 CP packet.
1616 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001617 if (dwords & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 u32 *data = (u32 *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001619 ((char *)dev->agp_buffer_map->handle
1620 + buf->offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 data[dwords++] = RADEON_CP_PACKET2;
1622 }
1623
1624 /* Fire off the indirect buffer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001625 BEGIN_RING(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001627 OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
1628 OUT_RING(offset);
1629 OUT_RING(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 ADVANCE_RING();
1632 }
1633}
1634
Dave Airlie7c1c2872008-11-28 14:22:24 +10001635static void radeon_cp_dispatch_indices(struct drm_device *dev,
1636 struct drm_master *master,
Dave Airlie056219e2007-07-11 16:17:42 +10001637 struct drm_buf * elt_buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001638 drm_radeon_tcl_prim_t * prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001641 struct drm_radeon_master_private *master_priv = master->driver_priv;
1642 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int offset = dev_priv->gart_buffers_offset + prim->offset;
1644 u32 *data;
1645 int dwords;
1646 int i = 0;
1647 int start = prim->start + RADEON_INDEX_PRIM_OFFSET;
1648 int count = (prim->finish - start) / sizeof(u16);
1649 int nbox = sarea_priv->nbox;
1650
1651 DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d offset: %x nr %d\n",
1652 prim->prim,
1653 prim->vc_format,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001654 prim->start, prim->finish, prim->offset, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001656 if (bad_prim_vertex_nr(prim->prim, count)) {
1657 DRM_ERROR("bad prim %x count %d\n", prim->prim, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return;
1659 }
1660
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001661 if (start >= prim->finish || (prim->start & 0x7)) {
1662 DRM_ERROR("buffer prim %d\n", prim->prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
1664 }
1665
1666 dwords = (prim->finish - prim->start + 3) / sizeof(u32);
1667
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001668 data = (u32 *) ((char *)dev->agp_buffer_map->handle +
1669 elt_buf->offset + prim->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001671 data[0] = CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, dwords - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 data[1] = offset;
1673 data[2] = prim->numverts;
1674 data[3] = prim->vc_format;
1675 data[4] = (prim->prim |
1676 RADEON_PRIM_WALK_IND |
1677 RADEON_COLOR_ORDER_RGBA |
1678 RADEON_VTX_FMT_RADEON_MODE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001679 (count << RADEON_NUM_VERTICES_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001682 if (i < nbox)
1683 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001685 radeon_cp_dispatch_indirect(dev, elt_buf,
1686 prim->start, prim->finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 i++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001689 } while (i < nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691}
1692
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001693#define RADEON_MAX_TEXTURE_SIZE RADEON_BUFFER_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Eric Anholt6c340ea2007-08-25 20:23:09 +10001695static int radeon_cp_dispatch_texture(struct drm_device * dev,
1696 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001697 drm_radeon_texture_t * tex,
1698 drm_radeon_tex_image_t * image)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie056219e2007-07-11 16:17:42 +10001701 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 u32 format;
1703 u32 *buffer;
1704 const u8 __user *data;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001705 int size, dwords, tex_width, blit_width, spitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 u32 height;
1707 int i;
1708 u32 texpitch, microtile;
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001709 u32 offset, byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 RING_LOCALS;
1711
Eric Anholt6c340ea2007-08-25 20:23:09 +10001712 if (radeon_check_and_fixup_offset(dev_priv, file_priv, &tex->offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001713 DRM_ERROR("Invalid destination offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001714 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
1717 dev_priv->stats.boxes |= RADEON_BOX_TEXTURE_LOAD;
1718
1719 /* Flush the pixel cache. This ensures no pixel data gets mixed
1720 * up with the texture data from the host data blit, otherwise
1721 * part of the texture image may be corrupted.
1722 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001723 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 RADEON_FLUSH_CACHE();
1725 RADEON_WAIT_UNTIL_IDLE();
1726 ADVANCE_RING();
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* The compiler won't optimize away a division by a variable,
1729 * even if the only legal values are powers of two. Thus, we'll
1730 * use a shift instead.
1731 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001732 switch (tex->format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 case RADEON_TXFORMAT_ARGB8888:
1734 case RADEON_TXFORMAT_RGBA8888:
1735 format = RADEON_COLOR_FORMAT_ARGB8888;
1736 tex_width = tex->width * 4;
1737 blit_width = image->width * 4;
1738 break;
1739 case RADEON_TXFORMAT_AI88:
1740 case RADEON_TXFORMAT_ARGB1555:
1741 case RADEON_TXFORMAT_RGB565:
1742 case RADEON_TXFORMAT_ARGB4444:
1743 case RADEON_TXFORMAT_VYUY422:
1744 case RADEON_TXFORMAT_YVYU422:
1745 format = RADEON_COLOR_FORMAT_RGB565;
1746 tex_width = tex->width * 2;
1747 blit_width = image->width * 2;
1748 break;
1749 case RADEON_TXFORMAT_I8:
1750 case RADEON_TXFORMAT_RGB332:
1751 format = RADEON_COLOR_FORMAT_CI8;
1752 tex_width = tex->width * 1;
1753 blit_width = image->width * 1;
1754 break;
1755 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001756 DRM_ERROR("invalid texture format %d\n", tex->format);
Eric Anholt20caafa2007-08-25 19:22:43 +10001757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001759 spitch = blit_width >> 6;
1760 if (spitch == 0 && image->height > 1)
Eric Anholt20caafa2007-08-25 19:22:43 +10001761 return -EINVAL;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 texpitch = tex->pitch;
1764 if ((texpitch << 22) & RADEON_DST_TILE_MICRO) {
1765 microtile = 1;
1766 if (tex_width < 64) {
1767 texpitch &= ~(RADEON_DST_TILE_MICRO >> 22);
1768 /* we got tiled coordinates, untile them */
1769 image->x *= 2;
1770 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001771 } else
1772 microtile = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001774 /* this might fail for zero-sized uploads - are those illegal? */
1775 if (!radeon_check_offset(dev_priv, tex->offset + image->height *
1776 blit_width - 1)) {
1777 DRM_ERROR("Invalid final destination offset\n");
1778 return -EINVAL;
1779 }
1780
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001781 DRM_DEBUG("tex=%dx%d blit=%d\n", tex_width, tex->height, blit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001784 DRM_DEBUG("tex: ofs=0x%x p=%d f=%d x=%hd y=%hd w=%hd h=%hd\n",
1785 tex->offset >> 10, tex->pitch, tex->format,
1786 image->x, image->y, image->width, image->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 /* Make a copy of some parameters in case we have to
1789 * update them for a multi-pass texture blit.
1790 */
1791 height = image->height;
1792 data = (const u8 __user *)image->data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 size = height * blit_width;
1795
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001796 if (size > RADEON_MAX_TEXTURE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 height = RADEON_MAX_TEXTURE_SIZE / blit_width;
1798 size = height * blit_width;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001799 } else if (size < 4 && size > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 size = 4;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001801 } else if (size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
1803 }
1804
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001805 buf = radeon_freelist_get(dev);
1806 if (0 && !buf) {
1807 radeon_do_cp_idle(dev_priv);
1808 buf = radeon_freelist_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001810 if (!buf) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001811 DRM_DEBUG("EAGAIN\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001812 if (DRM_COPY_TO_USER(tex->image, image, sizeof(*image)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001813 return -EFAULT;
1814 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /* Dispatch the indirect buffer.
1818 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001819 buffer =
1820 (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 dwords = size / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Dave Airlied985c102006-01-02 21:32:48 +11001823#define RADEON_COPY_MT(_buf, _data, _width) \
1824 do { \
1825 if (DRM_COPY_FROM_USER(_buf, _data, (_width))) {\
1826 DRM_ERROR("EFAULT on pad, %d bytes\n", (_width)); \
Eric Anholt20caafa2007-08-25 19:22:43 +10001827 return -EFAULT; \
Dave Airlied985c102006-01-02 21:32:48 +11001828 } \
1829 } while(0)
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (microtile) {
1832 /* texture micro tiling in use, minimum texture width is thus 16 bytes.
1833 however, we cannot use blitter directly for texture width < 64 bytes,
1834 since minimum tex pitch is 64 bytes and we need this to match
1835 the texture width, otherwise the blitter will tile it wrong.
1836 Thus, tiling manually in this case. Additionally, need to special
1837 case tex height = 1, since our actual image will have height 2
1838 and we need to ensure we don't read beyond the texture size
1839 from user space. */
1840 if (tex->height == 1) {
1841 if (tex_width >= 64 || tex_width <= 16) {
Dave Airlied985c102006-01-02 21:32:48 +11001842 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001843 (int)(tex_width * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 } else if (tex_width == 32) {
Dave Airlied985c102006-01-02 21:32:48 +11001845 RADEON_COPY_MT(buffer, data, 16);
1846 RADEON_COPY_MT(buffer + 8,
1847 data + 16, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849 } else if (tex_width >= 64 || tex_width == 16) {
Dave Airlied985c102006-01-02 21:32:48 +11001850 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001851 (int)(dwords * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 } else if (tex_width < 16) {
1853 for (i = 0; i < tex->height; i++) {
Dave Airlied985c102006-01-02 21:32:48 +11001854 RADEON_COPY_MT(buffer, data, tex_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 buffer += 4;
1856 data += tex_width;
1857 }
1858 } else if (tex_width == 32) {
1859 /* TODO: make sure this works when not fitting in one buffer
1860 (i.e. 32bytes x 2048...) */
1861 for (i = 0; i < tex->height; i += 2) {
Dave Airlied985c102006-01-02 21:32:48 +11001862 RADEON_COPY_MT(buffer, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001864 RADEON_COPY_MT(buffer + 8, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001866 RADEON_COPY_MT(buffer + 4, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001868 RADEON_COPY_MT(buffer + 12, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 data += 16;
1870 buffer += 16;
1871 }
1872 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001873 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (tex_width >= 32) {
1875 /* Texture image width is larger than the minimum, so we
1876 * can upload it directly.
1877 */
Dave Airlied985c102006-01-02 21:32:48 +11001878 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001879 (int)(dwords * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 } else {
1881 /* Texture image width is less than the minimum, so we
1882 * need to pad out each image scanline to the minimum
1883 * width.
1884 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001885 for (i = 0; i < tex->height; i++) {
Dave Airlied985c102006-01-02 21:32:48 +11001886 RADEON_COPY_MT(buffer, data, tex_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 buffer += 8;
1888 data += tex_width;
1889 }
1890 }
1891 }
1892
Dave Airlied985c102006-01-02 21:32:48 +11001893#undef RADEON_COPY_MT
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001894 byte_offset = (image->y & ~2047) * blit_width;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001895 buf->file_priv = file_priv;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001896 buf->used = size;
1897 offset = dev_priv->gart_buffers_offset + buf->offset;
1898 BEGIN_RING(9);
1899 OUT_RING(CP_PACKET3(RADEON_CNTL_BITBLT_MULTI, 5));
1900 OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
1901 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
1902 RADEON_GMC_BRUSH_NONE |
1903 (format << 8) |
1904 RADEON_GMC_SRC_DATATYPE_COLOR |
1905 RADEON_ROP3_S |
1906 RADEON_DP_SRC_SOURCE_MEMORY |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001907 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001908 OUT_RING((spitch << 22) | (offset >> 10));
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001909 OUT_RING((texpitch << 22) | ((tex->offset >> 10) + (byte_offset >> 10)));
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001910 OUT_RING(0);
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001911 OUT_RING((image->x << 16) | (image->y % 2048));
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001912 OUT_RING((image->width << 16) | height);
1913 RADEON_WAIT_UNTIL_2D_IDLE();
1914 ADVANCE_RING();
chaohong guoeed0f722007-10-15 10:45:49 +10001915 COMMIT_RING();
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001916
Dave Airlie7c1c2872008-11-28 14:22:24 +10001917 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /* Update the input parameters for next time */
1920 image->y += height;
1921 image->height -= height;
1922 image->data = (const u8 __user *)image->data + size;
1923 } while (image->height > 0);
1924
1925 /* Flush the pixel cache after the blit completes. This ensures
1926 * the texture data is written out to memory before rendering
1927 * continues.
1928 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001929 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 RADEON_FLUSH_CACHE();
1931 RADEON_WAIT_UNTIL_2D_IDLE();
1932 ADVANCE_RING();
chaohong guoeed0f722007-10-15 10:45:49 +10001933 COMMIT_RING();
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 0;
1936}
1937
Dave Airlie84b1fd12007-07-11 15:53:27 +10001938static void radeon_cp_dispatch_stipple(struct drm_device * dev, u32 * stipple)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 drm_radeon_private_t *dev_priv = dev->dev_private;
1941 int i;
1942 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001943 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001945 BEGIN_RING(35);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001947 OUT_RING(CP_PACKET0(RADEON_RE_STIPPLE_ADDR, 0));
1948 OUT_RING(0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001950 OUT_RING(CP_PACKET0_TABLE(RADEON_RE_STIPPLE_DATA, 31));
1951 for (i = 0; i < 32; i++) {
1952 OUT_RING(stipple[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
1955 ADVANCE_RING();
1956}
1957
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001958static void radeon_apply_surface_regs(int surf_index,
Dave Airlied985c102006-01-02 21:32:48 +11001959 drm_radeon_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 if (!dev_priv->mmio)
1962 return;
1963
1964 radeon_do_cp_idle(dev_priv);
1965
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001966 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * surf_index,
1967 dev_priv->surfaces[surf_index].flags);
1968 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * surf_index,
1969 dev_priv->surfaces[surf_index].lower);
1970 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * surf_index,
1971 dev_priv->surfaces[surf_index].upper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/* Allocates a virtual surface
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001975 * doesn't always allocate a real surface, will stretch an existing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 * surface when possible.
1977 *
1978 * Note that refcount can be at most 2, since during a free refcount=3
1979 * might mean we have to allocate a new surface which might not always
1980 * be available.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001981 * For example : we allocate three contiguous surfaces ABC. If B is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 * freed, we suddenly need two surfaces to store A and C, which might
1983 * not always be available.
1984 */
Dave Airlied985c102006-01-02 21:32:48 +11001985static int alloc_surface(drm_radeon_surface_alloc_t *new,
Eric Anholt6c340ea2007-08-25 20:23:09 +10001986 drm_radeon_private_t *dev_priv,
1987 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct radeon_virt_surface *s;
1990 int i;
1991 int virt_surface_index;
1992 uint32_t new_upper, new_lower;
1993
1994 new_lower = new->address;
1995 new_upper = new_lower + new->size - 1;
1996
1997 /* sanity check */
1998 if ((new_lower >= new_upper) || (new->flags == 0) || (new->size == 0) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001999 ((new_upper & RADEON_SURF_ADDRESS_FIXED_MASK) !=
2000 RADEON_SURF_ADDRESS_FIXED_MASK)
2001 || ((new_lower & RADEON_SURF_ADDRESS_FIXED_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return -1;
2003
2004 /* make sure there is no overlap with existing surfaces */
2005 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
2006 if ((dev_priv->surfaces[i].refcount != 0) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002007 (((new_lower >= dev_priv->surfaces[i].lower) &&
2008 (new_lower < dev_priv->surfaces[i].upper)) ||
2009 ((new_lower < dev_priv->surfaces[i].lower) &&
2010 (new_upper > dev_priv->surfaces[i].lower)))) {
2011 return -1;
2012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014
2015 /* find a virtual surface */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002016 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++)
Hannes Eder8f497aa2009-03-05 20:14:18 +01002017 if (dev_priv->virt_surfaces[i].file_priv == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002019 if (i == 2 * RADEON_MAX_SURFACES) {
2020 return -1;
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 virt_surface_index = i;
2023
2024 /* try to reuse an existing surface */
2025 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
2026 /* extend before */
2027 if ((dev_priv->surfaces[i].refcount == 1) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002028 (new->flags == dev_priv->surfaces[i].flags) &&
2029 (new_upper + 1 == dev_priv->surfaces[i].lower)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 s = &(dev_priv->virt_surfaces[virt_surface_index]);
2031 s->surface_index = i;
2032 s->lower = new_lower;
2033 s->upper = new_upper;
2034 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002035 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 dev_priv->surfaces[i].refcount++;
2037 dev_priv->surfaces[i].lower = s->lower;
2038 radeon_apply_surface_regs(s->surface_index, dev_priv);
2039 return virt_surface_index;
2040 }
2041
2042 /* extend after */
2043 if ((dev_priv->surfaces[i].refcount == 1) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002044 (new->flags == dev_priv->surfaces[i].flags) &&
2045 (new_lower == dev_priv->surfaces[i].upper + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 s = &(dev_priv->virt_surfaces[virt_surface_index]);
2047 s->surface_index = i;
2048 s->lower = new_lower;
2049 s->upper = new_upper;
2050 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002051 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 dev_priv->surfaces[i].refcount++;
2053 dev_priv->surfaces[i].upper = s->upper;
2054 radeon_apply_surface_regs(s->surface_index, dev_priv);
2055 return virt_surface_index;
2056 }
2057 }
2058
2059 /* okay, we need a new one */
2060 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
2061 if (dev_priv->surfaces[i].refcount == 0) {
2062 s = &(dev_priv->virt_surfaces[virt_surface_index]);
2063 s->surface_index = i;
2064 s->lower = new_lower;
2065 s->upper = new_upper;
2066 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002067 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 dev_priv->surfaces[i].refcount = 1;
2069 dev_priv->surfaces[i].lower = s->lower;
2070 dev_priv->surfaces[i].upper = s->upper;
2071 dev_priv->surfaces[i].flags = s->flags;
2072 radeon_apply_surface_regs(s->surface_index, dev_priv);
2073 return virt_surface_index;
2074 }
2075 }
2076
2077 /* we didn't find anything */
2078 return -1;
2079}
2080
Eric Anholt6c340ea2007-08-25 20:23:09 +10002081static int free_surface(struct drm_file *file_priv,
2082 drm_radeon_private_t * dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002083 int lower)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct radeon_virt_surface *s;
2086 int i;
2087 /* find the virtual surface */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002088 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 s = &(dev_priv->virt_surfaces[i]);
Eric Anholt6c340ea2007-08-25 20:23:09 +10002090 if (s->file_priv) {
2091 if ((lower == s->lower) && (file_priv == s->file_priv))
2092 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002093 if (dev_priv->surfaces[s->surface_index].
2094 lower == s->lower)
2095 dev_priv->surfaces[s->surface_index].
2096 lower = s->upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002098 if (dev_priv->surfaces[s->surface_index].
2099 upper == s->upper)
2100 dev_priv->surfaces[s->surface_index].
2101 upper = s->lower;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 dev_priv->surfaces[s->surface_index].refcount--;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002104 if (dev_priv->surfaces[s->surface_index].
2105 refcount == 0)
2106 dev_priv->surfaces[s->surface_index].
2107 flags = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002108 s->file_priv = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002109 radeon_apply_surface_regs(s->surface_index,
2110 dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return 0;
2112 }
2113 }
2114 }
2115 return 1;
2116}
2117
Eric Anholt6c340ea2007-08-25 20:23:09 +10002118static void radeon_surfaces_release(struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002119 drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002122 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002123 if (dev_priv->virt_surfaces[i].file_priv == file_priv)
2124 free_surface(file_priv, dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002125 dev_priv->virt_surfaces[i].lower);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127}
2128
2129/* ================================================================
2130 * IOCTL functions
2131 */
Eric Anholtc153f452007-09-03 12:06:45 +10002132static int radeon_surface_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002135 drm_radeon_surface_alloc_t *alloc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Eric Anholtc153f452007-09-03 12:06:45 +10002137 if (alloc_surface(alloc, dev_priv, file_priv) == -1)
Eric Anholt20caafa2007-08-25 19:22:43 +10002138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 else
2140 return 0;
2141}
2142
Eric Anholtc153f452007-09-03 12:06:45 +10002143static int radeon_surface_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002146 drm_radeon_surface_free_t *memfree = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Eric Anholtc153f452007-09-03 12:06:45 +10002148 if (free_surface(file_priv, dev_priv, memfree->address))
Eric Anholt20caafa2007-08-25 19:22:43 +10002149 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 else
2151 return 0;
2152}
2153
Eric Anholtc153f452007-09-03 12:06:45 +10002154static int radeon_cp_clear(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002157 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2158 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10002159 drm_radeon_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 drm_radeon_clear_rect_t depth_boxes[RADEON_NR_SAREA_CLIPRECTS];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002161 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Eric Anholt6c340ea2007-08-25 20:23:09 +10002163 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002165 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002167 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
2169
Eric Anholtc153f452007-09-03 12:06:45 +10002170 if (DRM_COPY_FROM_USER(&depth_boxes, clear->depth_boxes,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002171 sarea_priv->nbox * sizeof(depth_boxes[0])))
Eric Anholt20caafa2007-08-25 19:22:43 +10002172 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Dave Airlie7c1c2872008-11-28 14:22:24 +10002174 radeon_cp_dispatch_clear(dev, file_priv->master, clear, depth_boxes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 COMMIT_RING();
2177 return 0;
2178}
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002181 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10002182static int radeon_do_init_pageflip(struct drm_device *dev, struct drm_master *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002185 struct drm_radeon_master_private *master_priv = master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 RING_LOCALS;
2187
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002188 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002190 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002192 OUT_RING(CP_PACKET0(RADEON_CRTC_OFFSET_CNTL, 0));
2193 OUT_RING(RADEON_READ(RADEON_CRTC_OFFSET_CNTL) |
2194 RADEON_CRTC_OFFSET_FLIP_CNTL);
2195 OUT_RING(CP_PACKET0(RADEON_CRTC2_OFFSET_CNTL, 0));
2196 OUT_RING(RADEON_READ(RADEON_CRTC2_OFFSET_CNTL) |
2197 RADEON_CRTC_OFFSET_FLIP_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 ADVANCE_RING();
2199
2200 dev_priv->page_flipping = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Dave Airlie7c1c2872008-11-28 14:22:24 +10002202 if (master_priv->sarea_priv->pfCurrentPage != 1)
2203 master_priv->sarea_priv->pfCurrentPage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return 0;
2206}
2207
2208/* Swapping and flipping are different operations, need different ioctls.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002209 * They can & should be intermixed to support multiple 3d windows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 */
Eric Anholtc153f452007-09-03 12:06:45 +10002211static int radeon_cp_flip(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002214 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Eric Anholt6c340ea2007-08-25 20:23:09 +10002216 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002218 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002220 if (!dev_priv->page_flipping)
Dave Airlie7c1c2872008-11-28 14:22:24 +10002221 radeon_do_init_pageflip(dev, file_priv->master);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002222
Dave Airlie7c1c2872008-11-28 14:22:24 +10002223 radeon_cp_dispatch_flip(dev, file_priv->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 COMMIT_RING();
2226 return 0;
2227}
2228
Eric Anholtc153f452007-09-03 12:06:45 +10002229static int radeon_cp_swap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002232 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2233 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
2234
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002235 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Eric Anholt6c340ea2007-08-25 20:23:09 +10002237 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002239 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002241 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
2243
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002244 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
2245 r600_cp_dispatch_swap(dev, file_priv);
2246 else
2247 radeon_cp_dispatch_swap(dev, file_priv->master);
Dave Airlie7c1c2872008-11-28 14:22:24 +10002248 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 COMMIT_RING();
2251 return 0;
2252}
2253
Eric Anholtc153f452007-09-03 12:06:45 +10002254static int radeon_cp_vertex(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002257 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2258 drm_radeon_sarea_t *sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002259 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002260 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002261 drm_radeon_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 drm_radeon_tcl_prim_t prim;
2263
Eric Anholt6c340ea2007-08-25 20:23:09 +10002264 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Dave Airlie7c1c2872008-11-28 14:22:24 +10002266 sarea_priv = master_priv->sarea_priv;
2267
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002268 DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002269 DRM_CURRENTPID, vertex->idx, vertex->count, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Eric Anholtc153f452007-09-03 12:06:45 +10002271 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002272 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002273 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
Eric Anholtc153f452007-09-03 12:06:45 +10002276 if (vertex->prim < 0 || vertex->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
2277 DRM_ERROR("buffer prim %d\n", vertex->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10002278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002281 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2282 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Eric Anholtc153f452007-09-03 12:06:45 +10002284 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Eric Anholt6c340ea2007-08-25 20:23:09 +10002286 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002287 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002288 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002291 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002292 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
2296 /* Build up a prim_t record:
2297 */
Eric Anholtc153f452007-09-03 12:06:45 +10002298 if (vertex->count) {
2299 buf->used = vertex->count; /* not used? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002301 if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002302 if (radeon_emit_state(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002303 &sarea_priv->context_state,
2304 sarea_priv->tex_state,
2305 sarea_priv->dirty)) {
2306 DRM_ERROR("radeon_emit_state failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309
2310 sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
2311 RADEON_UPLOAD_TEX1IMAGES |
2312 RADEON_UPLOAD_TEX2IMAGES |
2313 RADEON_REQUIRE_QUIESCENCE);
2314 }
2315
2316 prim.start = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10002317 prim.finish = vertex->count; /* unused */
2318 prim.prim = vertex->prim;
2319 prim.numverts = vertex->count;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002320 prim.vc_format = sarea_priv->vc_format;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002321
Dave Airlie7c1c2872008-11-28 14:22:24 +10002322 radeon_cp_dispatch_vertex(dev, file_priv, buf, &prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
Eric Anholtc153f452007-09-03 12:06:45 +10002325 if (vertex->discard) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10002326 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
2328
2329 COMMIT_RING();
2330 return 0;
2331}
2332
Eric Anholtc153f452007-09-03 12:06:45 +10002333static int radeon_cp_indices(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002336 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2337 drm_radeon_sarea_t *sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002338 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002339 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002340 drm_radeon_indices_t *elts = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 drm_radeon_tcl_prim_t prim;
2342 int count;
2343
Eric Anholt6c340ea2007-08-25 20:23:09 +10002344 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Dave Airlie7c1c2872008-11-28 14:22:24 +10002346 sarea_priv = master_priv->sarea_priv;
2347
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002348 DRM_DEBUG("pid=%d index=%d start=%d end=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002349 DRM_CURRENTPID, elts->idx, elts->start, elts->end,
2350 elts->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Eric Anholtc153f452007-09-03 12:06:45 +10002352 if (elts->idx < 0 || elts->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002353 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002354 elts->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Eric Anholtc153f452007-09-03 12:06:45 +10002357 if (elts->prim < 0 || elts->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
2358 DRM_ERROR("buffer prim %d\n", elts->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10002359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002362 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2363 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Eric Anholtc153f452007-09-03 12:06:45 +10002365 buf = dma->buflist[elts->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Eric Anholt6c340ea2007-08-25 20:23:09 +10002367 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002368 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002369 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002372 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002373 DRM_ERROR("sending pending buffer %d\n", elts->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
Eric Anholtc153f452007-09-03 12:06:45 +10002377 count = (elts->end - elts->start) / sizeof(u16);
2378 elts->start -= RADEON_INDEX_PRIM_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Eric Anholtc153f452007-09-03 12:06:45 +10002380 if (elts->start & 0x7) {
2381 DRM_ERROR("misaligned buffer 0x%x\n", elts->start);
Eric Anholt20caafa2007-08-25 19:22:43 +10002382 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
Eric Anholtc153f452007-09-03 12:06:45 +10002384 if (elts->start < buf->used) {
2385 DRM_ERROR("no header 0x%x - 0x%x\n", elts->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10002386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388
Eric Anholtc153f452007-09-03 12:06:45 +10002389 buf->used = elts->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002391 if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002392 if (radeon_emit_state(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002393 &sarea_priv->context_state,
2394 sarea_priv->tex_state,
2395 sarea_priv->dirty)) {
2396 DRM_ERROR("radeon_emit_state failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399
2400 sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
2401 RADEON_UPLOAD_TEX1IMAGES |
2402 RADEON_UPLOAD_TEX2IMAGES |
2403 RADEON_REQUIRE_QUIESCENCE);
2404 }
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* Build up a prim_t record:
2407 */
Eric Anholtc153f452007-09-03 12:06:45 +10002408 prim.start = elts->start;
2409 prim.finish = elts->end;
2410 prim.prim = elts->prim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 prim.offset = 0; /* offset from start of dma buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002412 prim.numverts = RADEON_MAX_VB_VERTS; /* duh */
Dave Airlie7c1c2872008-11-28 14:22:24 +10002413 prim.vc_format = sarea_priv->vc_format;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002414
Dave Airlie7c1c2872008-11-28 14:22:24 +10002415 radeon_cp_dispatch_indices(dev, file_priv->master, buf, &prim);
Eric Anholtc153f452007-09-03 12:06:45 +10002416 if (elts->discard) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10002417 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
2419
2420 COMMIT_RING();
2421 return 0;
2422}
2423
Eric Anholtc153f452007-09-03 12:06:45 +10002424static int radeon_cp_texture(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002427 drm_radeon_texture_t *tex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 drm_radeon_tex_image_t image;
2429 int ret;
2430
Eric Anholt6c340ea2007-08-25 20:23:09 +10002431 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Eric Anholtc153f452007-09-03 12:06:45 +10002433 if (tex->image == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002434 DRM_ERROR("null texture image!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002438 if (DRM_COPY_FROM_USER(&image,
Eric Anholtc153f452007-09-03 12:06:45 +10002439 (drm_radeon_tex_image_t __user *) tex->image,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002440 sizeof(image)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002441 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002443 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2444 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002446 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
2447 ret = r600_cp_dispatch_texture(dev, file_priv, tex, &image);
2448 else
2449 ret = radeon_cp_dispatch_texture(dev, file_priv, tex, &image);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return ret;
2452}
2453
Eric Anholtc153f452007-09-03 12:06:45 +10002454static int radeon_cp_stipple(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002457 drm_radeon_stipple_t *stipple = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 u32 mask[32];
2459
Eric Anholt6c340ea2007-08-25 20:23:09 +10002460 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Eric Anholtc153f452007-09-03 12:06:45 +10002462 if (DRM_COPY_FROM_USER(&mask, stipple->mask, 32 * sizeof(u32)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002463 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002465 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002467 radeon_cp_dispatch_stipple(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 COMMIT_RING();
2470 return 0;
2471}
2472
Eric Anholtc153f452007-09-03 12:06:45 +10002473static int radeon_cp_indirect(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10002476 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002477 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002478 drm_radeon_indirect_t *indirect = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 RING_LOCALS;
2480
Eric Anholt6c340ea2007-08-25 20:23:09 +10002481 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Márton Németh3e684ea2008-01-24 15:58:57 +10002483 DRM_DEBUG("idx=%d s=%d e=%d d=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002484 indirect->idx, indirect->start, indirect->end,
2485 indirect->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Eric Anholtc153f452007-09-03 12:06:45 +10002487 if (indirect->idx < 0 || indirect->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002488 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002489 indirect->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492
Eric Anholtc153f452007-09-03 12:06:45 +10002493 buf = dma->buflist[indirect->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Eric Anholt6c340ea2007-08-25 20:23:09 +10002495 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002496 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002497 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002500 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002501 DRM_ERROR("sending pending buffer %d\n", indirect->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504
Eric Anholtc153f452007-09-03 12:06:45 +10002505 if (indirect->start < buf->used) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002506 DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002507 indirect->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10002508 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
2510
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002511 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2512 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Eric Anholtc153f452007-09-03 12:06:45 +10002514 buf->used = indirect->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 /* Dispatch the indirect buffer full of commands from the
2517 * X server. This is insecure and is thus only available to
2518 * privileged clients.
2519 */
Alex Deucherc05ce082009-02-24 16:22:29 -05002520 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
2521 r600_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
2522 else {
2523 /* Wait for the 3D stream to idle before the indirect buffer
2524 * containing 2D acceleration commands is processed.
2525 */
2526 BEGIN_RING(2);
2527 RADEON_WAIT_UNTIL_3D_IDLE();
2528 ADVANCE_RING();
2529 radeon_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002532 if (indirect->discard) {
Alex Deucherc05ce082009-02-24 16:22:29 -05002533 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002534 }
Alex Deucherc05ce082009-02-24 16:22:29 -05002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 COMMIT_RING();
2537 return 0;
2538}
2539
Eric Anholtc153f452007-09-03 12:06:45 +10002540static int radeon_cp_vertex2(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002543 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2544 drm_radeon_sarea_t *sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002545 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002546 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002547 drm_radeon_vertex2_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int i;
2549 unsigned char laststate;
2550
Eric Anholt6c340ea2007-08-25 20:23:09 +10002551 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Dave Airlie7c1c2872008-11-28 14:22:24 +10002553 sarea_priv = master_priv->sarea_priv;
2554
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002555 DRM_DEBUG("pid=%d index=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002556 DRM_CURRENTPID, vertex->idx, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Eric Anholtc153f452007-09-03 12:06:45 +10002558 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002559 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002560 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002564 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2565 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Eric Anholtc153f452007-09-03 12:06:45 +10002567 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Eric Anholt6c340ea2007-08-25 20:23:09 +10002569 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002570 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002571 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002575 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002576 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002577 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Eric Anholt20caafa2007-08-25 19:22:43 +10002581 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Eric Anholtc153f452007-09-03 12:06:45 +10002583 for (laststate = 0xff, i = 0; i < vertex->nr_prims; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 drm_radeon_prim_t prim;
2585 drm_radeon_tcl_prim_t tclprim;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002586
Eric Anholtc153f452007-09-03 12:06:45 +10002587 if (DRM_COPY_FROM_USER(&prim, &vertex->prim[i], sizeof(prim)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002588 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002589
2590 if (prim.stateidx != laststate) {
2591 drm_radeon_state_t state;
2592
2593 if (DRM_COPY_FROM_USER(&state,
Eric Anholtc153f452007-09-03 12:06:45 +10002594 &vertex->state[prim.stateidx],
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002595 sizeof(state)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002596 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Eric Anholt6c340ea2007-08-25 20:23:09 +10002598 if (radeon_emit_state2(dev_priv, file_priv, &state)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002599 DRM_ERROR("radeon_emit_state2 failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002600 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602
2603 laststate = prim.stateidx;
2604 }
2605
2606 tclprim.start = prim.start;
2607 tclprim.finish = prim.finish;
2608 tclprim.prim = prim.prim;
2609 tclprim.vc_format = prim.vc_format;
2610
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002611 if (prim.prim & RADEON_PRIM_WALK_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 tclprim.offset = prim.numverts * 64;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002613 tclprim.numverts = RADEON_MAX_VB_VERTS; /* duh */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Dave Airlie7c1c2872008-11-28 14:22:24 +10002615 radeon_cp_dispatch_indices(dev, file_priv->master, buf, &tclprim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 } else {
2617 tclprim.numverts = prim.numverts;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002618 tclprim.offset = 0; /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Dave Airlie7c1c2872008-11-28 14:22:24 +10002620 radeon_cp_dispatch_vertex(dev, file_priv, buf, &tclprim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (sarea_priv->nbox == 1)
2624 sarea_priv->nbox = 0;
2625 }
2626
Eric Anholtc153f452007-09-03 12:06:45 +10002627 if (vertex->discard) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10002628 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
2630
2631 COMMIT_RING();
2632 return 0;
2633}
2634
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002635static int radeon_emit_packets(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002636 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002637 drm_radeon_cmd_header_t header,
Dave Airlieb3a83632005-09-30 18:37:36 +10002638 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 int id = (int)header.packet.packet_id;
2641 int sz, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (id >= RADEON_MAX_STATE_PACKETS)
Eric Anholt20caafa2007-08-25 19:22:43 +10002645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 sz = packet[id].len;
2648 reg = packet[id].start;
2649
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002650 if (sz * sizeof(u32) > drm_buffer_unprocessed(cmdbuf->buffer)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002651 DRM_ERROR("Packet size provided larger than data provided\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002655 if (radeon_check_and_fixup_packets(dev_priv, file_priv, id,
2656 cmdbuf->buffer)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002657 DRM_ERROR("Packet verification failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002661 BEGIN_RING(sz + 1);
2662 OUT_RING(CP_PACKET0(reg, (sz - 1)));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002663 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 ADVANCE_RING();
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return 0;
2667}
2668
Dave Airlied985c102006-01-02 21:32:48 +11002669static __inline__ int radeon_emit_scalars(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002670 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002671 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 int sz = header.scalars.count;
2674 int start = header.scalars.offset;
2675 int stride = header.scalars.stride;
2676 RING_LOCALS;
2677
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002678 BEGIN_RING(3 + sz);
2679 OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
2680 OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
2681 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002682 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 ADVANCE_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return 0;
2685}
2686
2687/* God this is ugly
2688 */
Dave Airlied985c102006-01-02 21:32:48 +11002689static __inline__ int radeon_emit_scalars2(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002690 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002691 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 int sz = header.scalars.count;
2694 int start = ((unsigned int)header.scalars.offset) + 0x100;
2695 int stride = header.scalars.stride;
2696 RING_LOCALS;
2697
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002698 BEGIN_RING(3 + sz);
2699 OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
2700 OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
2701 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002702 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 ADVANCE_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return 0;
2705}
2706
Dave Airlied985c102006-01-02 21:32:48 +11002707static __inline__ int radeon_emit_vectors(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002708 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002709 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 int sz = header.vectors.count;
2712 int start = header.vectors.offset;
2713 int stride = header.vectors.stride;
2714 RING_LOCALS;
2715
Dave Airlief2a22792006-06-24 16:55:34 +10002716 BEGIN_RING(5 + sz);
2717 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002718 OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
2719 OUT_RING(start | (stride << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
2720 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002721 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 ADVANCE_RING();
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return 0;
2725}
2726
Dave Airlied6fece02006-06-24 17:04:07 +10002727static __inline__ int radeon_emit_veclinear(drm_radeon_private_t *dev_priv,
2728 drm_radeon_cmd_header_t header,
2729 drm_radeon_kcmd_buffer_t *cmdbuf)
2730{
2731 int sz = header.veclinear.count * 4;
2732 int start = header.veclinear.addr_lo | (header.veclinear.addr_hi << 8);
2733 RING_LOCALS;
2734
2735 if (!sz)
2736 return 0;
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002737 if (sz * 4 > drm_buffer_unprocessed(cmdbuf->buffer))
Eric Anholt20caafa2007-08-25 19:22:43 +10002738 return -EINVAL;
Dave Airlied6fece02006-06-24 17:04:07 +10002739
2740 BEGIN_RING(5 + sz);
2741 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
2742 OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
2743 OUT_RING(start | (1 << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
2744 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002745 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Dave Airlied6fece02006-06-24 17:04:07 +10002746 ADVANCE_RING();
2747
Dave Airlied6fece02006-06-24 17:04:07 +10002748 return 0;
2749}
2750
Dave Airlie84b1fd12007-07-11 15:53:27 +10002751static int radeon_emit_packet3(struct drm_device * dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002752 struct drm_file *file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +10002753 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 drm_radeon_private_t *dev_priv = dev->dev_private;
2756 unsigned int cmdsz;
2757 int ret;
2758 RING_LOCALS;
2759
2760 DRM_DEBUG("\n");
2761
Eric Anholt6c340ea2007-08-25 20:23:09 +10002762 if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002763 cmdbuf, &cmdsz))) {
2764 DRM_ERROR("Packet verification failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return ret;
2766 }
2767
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002768 BEGIN_RING(cmdsz);
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002769 OUT_RING_DRM_BUFFER(cmdbuf->buffer, cmdsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 ADVANCE_RING();
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return 0;
2773}
2774
Dave Airlie84b1fd12007-07-11 15:53:27 +10002775static int radeon_emit_packet3_cliprect(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002776 struct drm_file *file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +10002777 drm_radeon_kcmd_buffer_t *cmdbuf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002778 int orig_nbox)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliec60ce622007-07-11 15:27:12 +10002781 struct drm_clip_rect box;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 unsigned int cmdsz;
2783 int ret;
Dave Airliec60ce622007-07-11 15:27:12 +10002784 struct drm_clip_rect __user *boxes = cmdbuf->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 int i = 0;
2786 RING_LOCALS;
2787
2788 DRM_DEBUG("\n");
2789
Eric Anholt6c340ea2007-08-25 20:23:09 +10002790 if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002791 cmdbuf, &cmdsz))) {
2792 DRM_ERROR("Packet verification failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return ret;
2794 }
2795
2796 if (!orig_nbox)
2797 goto out;
2798
2799 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002800 if (i < cmdbuf->nbox) {
2801 if (DRM_COPY_FROM_USER(&box, &boxes[i], sizeof(box)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002802 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /* FIXME The second and subsequent times round
2804 * this loop, send a WAIT_UNTIL_3D_IDLE before
2805 * calling emit_clip_rect(). This fixes a
2806 * lockup on fast machines when sending
2807 * several cliprects with a cmdbuf, as when
2808 * waving a 2D window over a 3D
2809 * window. Something in the commands from user
2810 * space seems to hang the card when they're
2811 * sent several times in a row. That would be
2812 * the correct place to fix it but this works
2813 * around it until I can figure that out - Tim
2814 * Smith */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002815 if (i) {
2816 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 RADEON_WAIT_UNTIL_3D_IDLE();
2818 ADVANCE_RING();
2819 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002820 radeon_emit_clip_rect(dev_priv, &box);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002822
2823 BEGIN_RING(cmdsz);
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002824 OUT_RING_DRM_BUFFER(cmdbuf->buffer, cmdsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 ADVANCE_RING();
2826
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002827 } while (++i < cmdbuf->nbox);
2828 if (cmdbuf->nbox == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 cmdbuf->nbox = 0;
2830
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002831 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002832 out:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002833 drm_buffer_advance(cmdbuf->buffer, cmdsz * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return 0;
2835}
2836
Dave Airlie84b1fd12007-07-11 15:53:27 +10002837static int radeon_emit_wait(struct drm_device * dev, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
2839 drm_radeon_private_t *dev_priv = dev->dev_private;
2840 RING_LOCALS;
2841
Márton Németh3e684ea2008-01-24 15:58:57 +10002842 DRM_DEBUG("%x\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 switch (flags) {
2844 case RADEON_WAIT_2D:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002845 BEGIN_RING(2);
2846 RADEON_WAIT_UNTIL_2D_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 ADVANCE_RING();
2848 break;
2849 case RADEON_WAIT_3D:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002850 BEGIN_RING(2);
2851 RADEON_WAIT_UNTIL_3D_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 ADVANCE_RING();
2853 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002854 case RADEON_WAIT_2D | RADEON_WAIT_3D:
2855 BEGIN_RING(2);
2856 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 ADVANCE_RING();
2858 break;
2859 default:
Eric Anholt20caafa2007-08-25 19:22:43 +10002860 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
2862
2863 return 0;
2864}
2865
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002866static int radeon_cp_cmdbuf(struct drm_device *dev, void *data,
2867 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10002870 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002871 struct drm_buf *buf = NULL;
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002872 drm_radeon_cmd_header_t stack_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 int idx;
Eric Anholtc153f452007-09-03 12:06:45 +10002874 drm_radeon_kcmd_buffer_t *cmdbuf = data;
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002875 int orig_nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Eric Anholt6c340ea2007-08-25 20:23:09 +10002877 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002879 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2880 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Eric Anholtc153f452007-09-03 12:06:45 +10002882 if (cmdbuf->bufsz > 64 * 1024 || cmdbuf->bufsz < 0) {
Eric Anholt20caafa2007-08-25 19:22:43 +10002883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885
2886 /* Allocate an in-kernel area and copy in the cmdbuf. Do this to avoid
2887 * races between checking values and using those values in other code,
2888 * and simply to avoid a lot of function calls to copy in data.
2889 */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002890 if (cmdbuf->bufsz != 0) {
2891 int rv;
2892 void __user *buffer = cmdbuf->buffer;
2893 rv = drm_buffer_alloc(&cmdbuf->buffer, cmdbuf->bufsz);
2894 if (rv)
2895 return rv;
2896 rv = drm_buffer_copy_from_user(cmdbuf->buffer, buffer,
2897 cmdbuf->bufsz);
Jean Delvarec9ff04c2010-05-11 14:01:45 +10002898 if (rv) {
2899 drm_buffer_free(cmdbuf->buffer);
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002900 return rv;
Jean Delvarec9ff04c2010-05-11 14:01:45 +10002901 }
2902 } else
2903 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Eric Anholtc153f452007-09-03 12:06:45 +10002905 orig_nbox = cmdbuf->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002907 if (dev_priv->microcode_version == UCODE_R300) {
Dave Airlie414ed532005-08-16 20:43:16 +10002908 int temp;
Eric Anholtc153f452007-09-03 12:06:45 +10002909 temp = r300_do_cp_cmdbuf(dev, file_priv, cmdbuf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002910
Jean Delvarec9ff04c2010-05-11 14:01:45 +10002911 drm_buffer_free(cmdbuf->buffer);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002912
Dave Airlie414ed532005-08-16 20:43:16 +10002913 return temp;
2914 }
2915
2916 /* microcode_version != r300 */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002917 while (drm_buffer_unprocessed(cmdbuf->buffer) >= sizeof(stack_header)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002919 drm_radeon_cmd_header_t *header;
2920 header = drm_buffer_read_object(cmdbuf->buffer,
2921 sizeof(stack_header), &stack_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002923 switch (header->header.cmd_type) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002924 case RADEON_CMD_PACKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 DRM_DEBUG("RADEON_CMD_PACKET\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002926 if (radeon_emit_packets
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002927 (dev_priv, file_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 DRM_ERROR("radeon_emit_packets failed\n");
2929 goto err;
2930 }
2931 break;
2932
2933 case RADEON_CMD_SCALARS:
2934 DRM_DEBUG("RADEON_CMD_SCALARS\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002935 if (radeon_emit_scalars(dev_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 DRM_ERROR("radeon_emit_scalars failed\n");
2937 goto err;
2938 }
2939 break;
2940
2941 case RADEON_CMD_VECTORS:
2942 DRM_DEBUG("RADEON_CMD_VECTORS\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002943 if (radeon_emit_vectors(dev_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 DRM_ERROR("radeon_emit_vectors failed\n");
2945 goto err;
2946 }
2947 break;
2948
2949 case RADEON_CMD_DMA_DISCARD:
2950 DRM_DEBUG("RADEON_CMD_DMA_DISCARD\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002951 idx = header->dma.buf_idx;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002952 if (idx < 0 || idx >= dma->buf_count) {
2953 DRM_ERROR("buffer index %d (of %d max)\n",
2954 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 goto err;
2956 }
2957
2958 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10002959 if (buf->file_priv != file_priv || buf->pending) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002960 DRM_ERROR("bad buffer %p %p %d\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002961 buf->file_priv, file_priv,
2962 buf->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 goto err;
2964 }
2965
Dave Airlie7c1c2872008-11-28 14:22:24 +10002966 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 break;
2968
2969 case RADEON_CMD_PACKET3:
2970 DRM_DEBUG("RADEON_CMD_PACKET3\n");
Eric Anholtc153f452007-09-03 12:06:45 +10002971 if (radeon_emit_packet3(dev, file_priv, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 DRM_ERROR("radeon_emit_packet3 failed\n");
2973 goto err;
2974 }
2975 break;
2976
2977 case RADEON_CMD_PACKET3_CLIP:
2978 DRM_DEBUG("RADEON_CMD_PACKET3_CLIP\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002979 if (radeon_emit_packet3_cliprect
Eric Anholtc153f452007-09-03 12:06:45 +10002980 (dev, file_priv, cmdbuf, orig_nbox)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 DRM_ERROR("radeon_emit_packet3_clip failed\n");
2982 goto err;
2983 }
2984 break;
2985
2986 case RADEON_CMD_SCALARS2:
2987 DRM_DEBUG("RADEON_CMD_SCALARS2\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002988 if (radeon_emit_scalars2(dev_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 DRM_ERROR("radeon_emit_scalars2 failed\n");
2990 goto err;
2991 }
2992 break;
2993
2994 case RADEON_CMD_WAIT:
2995 DRM_DEBUG("RADEON_CMD_WAIT\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002996 if (radeon_emit_wait(dev, header->wait.flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 DRM_ERROR("radeon_emit_wait failed\n");
2998 goto err;
2999 }
3000 break;
Dave Airlied6fece02006-06-24 17:04:07 +10003001 case RADEON_CMD_VECLINEAR:
3002 DRM_DEBUG("RADEON_CMD_VECLINEAR\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02003003 if (radeon_emit_veclinear(dev_priv, *header, cmdbuf)) {
Dave Airlied6fece02006-06-24 17:04:07 +10003004 DRM_ERROR("radeon_emit_veclinear failed\n");
3005 goto err;
3006 }
3007 break;
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 default:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02003010 DRM_ERROR("bad cmd_type %d at byte %d\n",
3011 header->header.cmd_type,
3012 cmdbuf->buffer->iterator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 goto err;
3014 }
3015 }
3016
Jean Delvarec9ff04c2010-05-11 14:01:45 +10003017 drm_buffer_free(cmdbuf->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Jean Delvarec9ff04c2010-05-11 14:01:45 +10003019 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 DRM_DEBUG("DONE\n");
3021 COMMIT_RING();
3022 return 0;
3023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003024 err:
Jean Delvarec9ff04c2010-05-11 14:01:45 +10003025 drm_buffer_free(cmdbuf->buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +10003026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027}
3028
Eric Anholtc153f452007-09-03 12:06:45 +10003029static int radeon_cp_getparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10003032 drm_radeon_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 int value;
3034
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003035 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Eric Anholtc153f452007-09-03 12:06:45 +10003037 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 case RADEON_PARAM_GART_BUFFER_OFFSET:
3039 value = dev_priv->gart_buffers_offset;
3040 break;
3041 case RADEON_PARAM_LAST_FRAME:
3042 dev_priv->stats.last_frame_reads++;
David Millerb07fa022009-02-12 02:15:37 -08003043 value = GET_SCRATCH(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 break;
3045 case RADEON_PARAM_LAST_DISPATCH:
David Millerb07fa022009-02-12 02:15:37 -08003046 value = GET_SCRATCH(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 break;
3048 case RADEON_PARAM_LAST_CLEAR:
3049 dev_priv->stats.last_clear_reads++;
David Millerb07fa022009-02-12 02:15:37 -08003050 value = GET_SCRATCH(dev_priv, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 break;
3052 case RADEON_PARAM_IRQ_NR:
Alex Deucherb15591f2009-09-17 14:25:12 -04003053 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
3054 value = 0;
3055 else
3056 value = drm_dev_to_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 break;
3058 case RADEON_PARAM_GART_BASE:
3059 value = dev_priv->gart_vm_start;
3060 break;
3061 case RADEON_PARAM_REGISTER_HANDLE:
Dave Airlied985c102006-01-02 21:32:48 +11003062 value = dev_priv->mmio->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 break;
3064 case RADEON_PARAM_STATUS_HANDLE:
3065 value = dev_priv->ring_rptr_offset;
3066 break;
3067#if BITS_PER_LONG == 32
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003068 /*
3069 * This ioctl() doesn't work on 64-bit platforms because hw_lock is a
3070 * pointer which can't fit into an int-sized variable. According to
Jan Engelhardt96de0e22007-10-19 23:21:04 +02003071 * Michel Dänzer, the ioctl() is only used on embedded platforms, so
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003072 * not supporting it shouldn't be a problem. If the same functionality
3073 * is needed on 64-bit platforms, a new ioctl() would have to be added,
3074 * so backwards-compatibility for the embedded platforms can be
3075 * maintained. --davidm 4-Feb-2004.
3076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 case RADEON_PARAM_SAREA_HANDLE:
3078 /* The lock is the first dword in the sarea. */
Dave Airlie7c1c2872008-11-28 14:22:24 +10003079 /* no users of this parameter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 break;
3081#endif
3082 case RADEON_PARAM_GART_TEX_HANDLE:
3083 value = dev_priv->gart_textures_offset;
3084 break;
Michel Dänzer8624ecb2006-08-07 20:33:57 +10003085 case RADEON_PARAM_SCRATCH_OFFSET:
3086 if (!dev_priv->writeback_works)
Eric Anholt20caafa2007-08-25 19:22:43 +10003087 return -EINVAL;
Alex Deucherc05ce082009-02-24 16:22:29 -05003088 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
3089 value = R600_SCRATCH_REG_OFFSET;
3090 else
3091 value = RADEON_SCRATCH_REG_OFFSET;
Michel Dänzer8624ecb2006-08-07 20:33:57 +10003092 break;
Dave Airlied985c102006-01-02 21:32:48 +11003093 case RADEON_PARAM_CARD_TYPE:
Dave Airlie54a56ac2006-09-22 04:25:09 +10003094 if (dev_priv->flags & RADEON_IS_PCIE)
Dave Airlied985c102006-01-02 21:32:48 +11003095 value = RADEON_CARD_PCIE;
Dave Airlie54a56ac2006-09-22 04:25:09 +10003096 else if (dev_priv->flags & RADEON_IS_AGP)
Dave Airlied985c102006-01-02 21:32:48 +11003097 value = RADEON_CARD_AGP;
3098 else
3099 value = RADEON_CARD_PCI;
3100 break;
Dave Airlieddbee332007-07-11 12:16:01 +10003101 case RADEON_PARAM_VBLANK_CRTC:
3102 value = radeon_vblank_crtc_get(dev);
3103 break;
Dave Airlie3d5e2c12008-02-07 15:01:05 +10003104 case RADEON_PARAM_FB_LOCATION:
3105 value = radeon_read_fb_location(dev_priv);
3106 break;
Alex Deucher5b92c402008-05-28 11:57:40 +10003107 case RADEON_PARAM_NUM_GB_PIPES:
3108 value = dev_priv->num_gb_pipes;
3109 break;
Alex Deucherf779b3e2009-08-19 19:11:39 -04003110 case RADEON_PARAM_NUM_Z_PIPES:
3111 value = dev_priv->num_z_pipes;
3112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 default:
Eric Anholtc153f452007-09-03 12:06:45 +10003114 DRM_DEBUG("Invalid parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10003115 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
3117
Eric Anholtc153f452007-09-03 12:06:45 +10003118 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003119 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10003120 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return 0;
3124}
3125
Eric Anholtc153f452007-09-03 12:06:45 +10003126static int radeon_cp_setparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10003129 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10003130 drm_radeon_setparam_t *sp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 struct drm_radeon_driver_file_fields *radeon_priv;
3132
Eric Anholtc153f452007-09-03 12:06:45 +10003133 switch (sp->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 case RADEON_SETPARAM_FB_LOCATION:
Eric Anholt6c340ea2007-08-25 20:23:09 +10003135 radeon_priv = file_priv->driver_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10003136 radeon_priv->radeon_fb_delta = dev_priv->fb_location -
3137 sp->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 break;
3139 case RADEON_SETPARAM_SWITCH_TILING:
Eric Anholtc153f452007-09-03 12:06:45 +10003140 if (sp->value == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003141 DRM_DEBUG("color tiling disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 dev_priv->front_pitch_offset &= ~RADEON_DST_TILE_MACRO;
3143 dev_priv->back_pitch_offset &= ~RADEON_DST_TILE_MACRO;
Dave Airlie7c1c2872008-11-28 14:22:24 +10003144 if (master_priv->sarea_priv)
3145 master_priv->sarea_priv->tiling_enabled = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10003146 } else if (sp->value == 1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003147 DRM_DEBUG("color tiling enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 dev_priv->front_pitch_offset |= RADEON_DST_TILE_MACRO;
3149 dev_priv->back_pitch_offset |= RADEON_DST_TILE_MACRO;
Dave Airlie7c1c2872008-11-28 14:22:24 +10003150 if (master_priv->sarea_priv)
3151 master_priv->sarea_priv->tiling_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003153 break;
Dave Airlieea98a922005-09-11 20:28:11 +10003154 case RADEON_SETPARAM_PCIGART_LOCATION:
Eric Anholtc153f452007-09-03 12:06:45 +10003155 dev_priv->pcigart_offset = sp->value;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003156 dev_priv->pcigart_offset_set = 1;
Dave Airlieea98a922005-09-11 20:28:11 +10003157 break;
Dave Airlied5ea7022006-03-19 19:37:55 +11003158 case RADEON_SETPARAM_NEW_MEMMAP:
Eric Anholtc153f452007-09-03 12:06:45 +10003159 dev_priv->new_memmap = sp->value;
Dave Airlied5ea7022006-03-19 19:37:55 +11003160 break;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003161 case RADEON_SETPARAM_PCIGART_TABLE_SIZE:
Eric Anholtc153f452007-09-03 12:06:45 +10003162 dev_priv->gart_info.table_size = sp->value;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003163 if (dev_priv->gart_info.table_size < RADEON_PCIGART_TABLE_SIZE)
3164 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
3165 break;
Dave Airlieddbee332007-07-11 12:16:01 +10003166 case RADEON_SETPARAM_VBLANK_CRTC:
Eric Anholtc153f452007-09-03 12:06:45 +10003167 return radeon_vblank_crtc_set(dev, sp->value);
Dave Airlieddbee332007-07-11 12:16:01 +10003168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 default:
Eric Anholtc153f452007-09-03 12:06:45 +10003170 DRM_DEBUG("Invalid parameter %d\n", sp->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10003171 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
3173
3174 return 0;
3175}
3176
3177/* When a client dies:
3178 * - Check for and clean up flipped page state
3179 * - Free any alloced GART memory.
Dave Airlied985c102006-01-02 21:32:48 +11003180 * - Free any alloced radeon surfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 *
3182 * DRM infrastructure takes care of reclaiming dma buffers.
3183 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10003184void radeon_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003186 if (dev->dev_private) {
3187 drm_radeon_private_t *dev_priv = dev->dev_private;
Michel Dänzer453ff942007-05-08 15:21:14 +10003188 dev_priv->page_flipping = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10003189 radeon_mem_release(file_priv, dev_priv->gart_heap);
3190 radeon_mem_release(file_priv, dev_priv->fb_heap);
3191 radeon_surfaces_release(file_priv, dev_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Dave Airlie84b1fd12007-07-11 15:53:27 +10003195void radeon_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
David Miller6abf6bb2009-02-14 01:51:07 -08003197 radeon_surfaces_release(PCIGART_FILE_PRIV, dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 radeon_do_release(dev);
3199}
3200
Eric Anholt6c340ea2007-08-25 20:23:09 +10003201int radeon_driver_open(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
3203 drm_radeon_private_t *dev_priv = dev->dev_private;
3204 struct drm_radeon_driver_file_fields *radeon_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003205
Dave Airlied985c102006-01-02 21:32:48 +11003206 DRM_DEBUG("\n");
Eric Anholt9a298b22009-03-24 12:23:04 -07003207 radeon_priv = kmalloc(sizeof(*radeon_priv), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if (!radeon_priv)
3210 return -ENOMEM;
3211
Eric Anholt6c340ea2007-08-25 20:23:09 +10003212 file_priv->driver_priv = radeon_priv;
Dave Airlied985c102006-01-02 21:32:48 +11003213
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003214 if (dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 radeon_priv->radeon_fb_delta = dev_priv->fb_location;
3216 else
3217 radeon_priv->radeon_fb_delta = 0;
3218 return 0;
3219}
3220
Eric Anholt6c340ea2007-08-25 20:23:09 +10003221void radeon_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003223 struct drm_radeon_driver_file_fields *radeon_priv =
Eric Anholt6c340ea2007-08-25 20:23:09 +10003224 file_priv->driver_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003225
Eric Anholt9a298b22009-03-24 12:23:04 -07003226 kfree(radeon_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
Eric Anholtc153f452007-09-03 12:06:45 +10003229struct drm_ioctl_desc radeon_ioctls[] = {
3230 DRM_IOCTL_DEF(DRM_RADEON_CP_INIT, radeon_cp_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3231 DRM_IOCTL_DEF(DRM_RADEON_CP_START, radeon_cp_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3232 DRM_IOCTL_DEF(DRM_RADEON_CP_STOP, radeon_cp_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3233 DRM_IOCTL_DEF(DRM_RADEON_CP_RESET, radeon_cp_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3234 DRM_IOCTL_DEF(DRM_RADEON_CP_IDLE, radeon_cp_idle, DRM_AUTH),
3235 DRM_IOCTL_DEF(DRM_RADEON_CP_RESUME, radeon_cp_resume, DRM_AUTH),
3236 DRM_IOCTL_DEF(DRM_RADEON_RESET, radeon_engine_reset, DRM_AUTH),
3237 DRM_IOCTL_DEF(DRM_RADEON_FULLSCREEN, radeon_fullscreen, DRM_AUTH),
3238 DRM_IOCTL_DEF(DRM_RADEON_SWAP, radeon_cp_swap, DRM_AUTH),
3239 DRM_IOCTL_DEF(DRM_RADEON_CLEAR, radeon_cp_clear, DRM_AUTH),
3240 DRM_IOCTL_DEF(DRM_RADEON_VERTEX, radeon_cp_vertex, DRM_AUTH),
3241 DRM_IOCTL_DEF(DRM_RADEON_INDICES, radeon_cp_indices, DRM_AUTH),
3242 DRM_IOCTL_DEF(DRM_RADEON_TEXTURE, radeon_cp_texture, DRM_AUTH),
3243 DRM_IOCTL_DEF(DRM_RADEON_STIPPLE, radeon_cp_stipple, DRM_AUTH),
3244 DRM_IOCTL_DEF(DRM_RADEON_INDIRECT, radeon_cp_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3245 DRM_IOCTL_DEF(DRM_RADEON_VERTEX2, radeon_cp_vertex2, DRM_AUTH),
3246 DRM_IOCTL_DEF(DRM_RADEON_CMDBUF, radeon_cp_cmdbuf, DRM_AUTH),
3247 DRM_IOCTL_DEF(DRM_RADEON_GETPARAM, radeon_cp_getparam, DRM_AUTH),
3248 DRM_IOCTL_DEF(DRM_RADEON_FLIP, radeon_cp_flip, DRM_AUTH),
3249 DRM_IOCTL_DEF(DRM_RADEON_ALLOC, radeon_mem_alloc, DRM_AUTH),
3250 DRM_IOCTL_DEF(DRM_RADEON_FREE, radeon_mem_free, DRM_AUTH),
3251 DRM_IOCTL_DEF(DRM_RADEON_INIT_HEAP, radeon_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3252 DRM_IOCTL_DEF(DRM_RADEON_IRQ_EMIT, radeon_irq_emit, DRM_AUTH),
3253 DRM_IOCTL_DEF(DRM_RADEON_IRQ_WAIT, radeon_irq_wait, DRM_AUTH),
3254 DRM_IOCTL_DEF(DRM_RADEON_SETPARAM, radeon_cp_setparam, DRM_AUTH),
3255 DRM_IOCTL_DEF(DRM_RADEON_SURF_ALLOC, radeon_surface_alloc, DRM_AUTH),
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003256 DRM_IOCTL_DEF(DRM_RADEON_SURF_FREE, radeon_surface_free, DRM_AUTH),
3257 DRM_IOCTL_DEF(DRM_RADEON_CS, r600_cs_legacy_ioctl, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258};
3259
3260int radeon_max_ioctl = DRM_ARRAY_SIZE(radeon_ioctls);