blob: 44b6d66b0ab31a6d67a4c3bd5a5ed44a50767529 [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 }
108 break;
109
110 case RADEON_EMIT_PP_CNTL:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200111 data = drm_buffer_pointer_to_dword(buf,
112 (RADEON_RB3D_COLOROFFSET - RADEON_PP_CNTL) / 4);
113
114 if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000115 DRM_ERROR("Invalid colour buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000116 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 break;
119
120 case R200_EMIT_PP_TXOFFSET_0:
121 case R200_EMIT_PP_TXOFFSET_1:
122 case R200_EMIT_PP_TXOFFSET_2:
123 case R200_EMIT_PP_TXOFFSET_3:
124 case R200_EMIT_PP_TXOFFSET_4:
125 case R200_EMIT_PP_TXOFFSET_5:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200126 data = drm_buffer_pointer_to_dword(buf, 0);
127 if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 DRM_ERROR("Invalid R200 texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 break;
132
133 case RADEON_EMIT_PP_TXFILTER_0:
134 case RADEON_EMIT_PP_TXFILTER_1:
135 case RADEON_EMIT_PP_TXFILTER_2:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200136 data = drm_buffer_pointer_to_dword(buf,
137 (RADEON_PP_TXOFFSET_0 - RADEON_PP_TXFILTER_0) / 4);
138 if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 DRM_ERROR("Invalid R100 texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000140 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 break;
143
144 case R200_EMIT_PP_CUBIC_OFFSETS_0:
145 case R200_EMIT_PP_CUBIC_OFFSETS_1:
146 case R200_EMIT_PP_CUBIC_OFFSETS_2:
147 case R200_EMIT_PP_CUBIC_OFFSETS_3:
148 case R200_EMIT_PP_CUBIC_OFFSETS_4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 case R200_EMIT_PP_CUBIC_OFFSETS_5:{
150 int i;
151 for (i = 0; i < 5; i++) {
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200152 data = drm_buffer_pointer_to_dword(buf, i);
Dave Airlied985c102006-01-02 21:32:48 +1100153 if (radeon_check_and_fixup_offset(dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000154 file_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200155 data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156 DRM_ERROR
157 ("Invalid R200 cubic texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000158 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 case RADEON_EMIT_PP_CUBIC_OFFSETS_T0:
165 case RADEON_EMIT_PP_CUBIC_OFFSETS_T1:
166 case RADEON_EMIT_PP_CUBIC_OFFSETS_T2:{
167 int i;
168 for (i = 0; i < 5; i++) {
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200169 data = drm_buffer_pointer_to_dword(buf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (radeon_check_and_fixup_offset(dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000171 file_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200172 data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 DRM_ERROR
174 ("Invalid R100 cubic texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000175 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 }
178 }
179 break;
180
Roland Scheidegger18f29052006-08-30 23:17:55 +0100181 case R200_EMIT_VAP_CTL:{
182 RING_LOCALS;
183 BEGIN_RING(2);
184 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
185 ADVANCE_RING();
186 }
187 break;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 case RADEON_EMIT_RB3D_COLORPITCH:
190 case RADEON_EMIT_RE_LINE_PATTERN:
191 case RADEON_EMIT_SE_LINE_WIDTH:
192 case RADEON_EMIT_PP_LUM_MATRIX:
193 case RADEON_EMIT_PP_ROT_MATRIX_0:
194 case RADEON_EMIT_RB3D_STENCILREFMASK:
195 case RADEON_EMIT_SE_VPORT_XSCALE:
196 case RADEON_EMIT_SE_CNTL:
197 case RADEON_EMIT_SE_CNTL_STATUS:
198 case RADEON_EMIT_RE_MISC:
199 case RADEON_EMIT_PP_BORDER_COLOR_0:
200 case RADEON_EMIT_PP_BORDER_COLOR_1:
201 case RADEON_EMIT_PP_BORDER_COLOR_2:
202 case RADEON_EMIT_SE_ZBIAS_FACTOR:
203 case RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT:
204 case RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED:
205 case R200_EMIT_PP_TXCBLEND_0:
206 case R200_EMIT_PP_TXCBLEND_1:
207 case R200_EMIT_PP_TXCBLEND_2:
208 case R200_EMIT_PP_TXCBLEND_3:
209 case R200_EMIT_PP_TXCBLEND_4:
210 case R200_EMIT_PP_TXCBLEND_5:
211 case R200_EMIT_PP_TXCBLEND_6:
212 case R200_EMIT_PP_TXCBLEND_7:
213 case R200_EMIT_TCL_LIGHT_MODEL_CTL_0:
214 case R200_EMIT_TFACTOR_0:
215 case R200_EMIT_VTX_FMT_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 case R200_EMIT_MATRIX_SELECT_0:
217 case R200_EMIT_TEX_PROC_CTL_2:
218 case R200_EMIT_TCL_UCP_VERT_BLEND_CTL:
219 case R200_EMIT_PP_TXFILTER_0:
220 case R200_EMIT_PP_TXFILTER_1:
221 case R200_EMIT_PP_TXFILTER_2:
222 case R200_EMIT_PP_TXFILTER_3:
223 case R200_EMIT_PP_TXFILTER_4:
224 case R200_EMIT_PP_TXFILTER_5:
225 case R200_EMIT_VTE_CNTL:
226 case R200_EMIT_OUTPUT_VTX_COMP_SEL:
227 case R200_EMIT_PP_TAM_DEBUG3:
228 case R200_EMIT_PP_CNTL_X:
229 case R200_EMIT_RB3D_DEPTHXY_OFFSET:
230 case R200_EMIT_RE_AUX_SCISSOR_CNTL:
231 case R200_EMIT_RE_SCISSOR_TL_0:
232 case R200_EMIT_RE_SCISSOR_TL_1:
233 case R200_EMIT_RE_SCISSOR_TL_2:
234 case R200_EMIT_SE_VAP_CNTL_STATUS:
235 case R200_EMIT_SE_VTX_STATE_CNTL:
236 case R200_EMIT_RE_POINTSIZE:
237 case R200_EMIT_TCL_INPUT_VTX_VECTOR_ADDR_0:
238 case R200_EMIT_PP_CUBIC_FACES_0:
239 case R200_EMIT_PP_CUBIC_FACES_1:
240 case R200_EMIT_PP_CUBIC_FACES_2:
241 case R200_EMIT_PP_CUBIC_FACES_3:
242 case R200_EMIT_PP_CUBIC_FACES_4:
243 case R200_EMIT_PP_CUBIC_FACES_5:
244 case RADEON_EMIT_PP_TEX_SIZE_0:
245 case RADEON_EMIT_PP_TEX_SIZE_1:
246 case RADEON_EMIT_PP_TEX_SIZE_2:
247 case R200_EMIT_RB3D_BLENDCOLOR:
248 case R200_EMIT_TCL_POINT_SPRITE_CNTL:
249 case RADEON_EMIT_PP_CUBIC_FACES_0:
250 case RADEON_EMIT_PP_CUBIC_FACES_1:
251 case RADEON_EMIT_PP_CUBIC_FACES_2:
252 case R200_EMIT_PP_TRI_PERF_CNTL:
Dave Airlie9d176012005-09-11 19:55:53 +1000253 case R200_EMIT_PP_AFS_0:
254 case R200_EMIT_PP_AFS_1:
255 case R200_EMIT_ATF_TFACTOR:
256 case R200_EMIT_PP_TXCTLALL_0:
257 case R200_EMIT_PP_TXCTLALL_1:
258 case R200_EMIT_PP_TXCTLALL_2:
259 case R200_EMIT_PP_TXCTLALL_3:
260 case R200_EMIT_PP_TXCTLALL_4:
261 case R200_EMIT_PP_TXCTLALL_5:
Dave Airlied6fece02006-06-24 17:04:07 +1000262 case R200_EMIT_VAP_PVS_CNTL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* These packets don't contain memory offsets */
264 break;
265
266 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267 DRM_ERROR("Unknown state packet ID %d\n", id);
Eric Anholt20caafa2007-08-25 19:22:43 +1000268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 return 0;
272}
273
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274static __inline__ int radeon_check_and_fixup_packet3(drm_radeon_private_t *
275 dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000276 struct drm_file *file_priv,
Dave Airlied985c102006-01-02 21:32:48 +1100277 drm_radeon_kcmd_buffer_t *
278 cmdbuf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 unsigned int *cmdsz)
280{
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200281 u32 *cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 0);
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000282 u32 offset, narrays;
283 int count, i, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200285 count = ((*cmd & RADEON_CP_PACKET_COUNT_MASK) >> 16);
286 *cmdsz = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200288 if ((*cmd & 0xc0000000) != RADEON_CP_PACKET3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289 DRM_ERROR("Not a type 3 packet\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200293 if (4 * *cmdsz > drm_buffer_unprocessed(cmdbuf->buffer)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 DRM_ERROR("Packet size larger than size of data provided\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200298 switch (*cmd & 0xff00) {
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000299 /* XXX Are there old drivers needing other packets? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000301 case RADEON_3D_DRAW_IMMD:
302 case RADEON_3D_DRAW_VBUF:
303 case RADEON_3D_DRAW_INDX:
304 case RADEON_WAIT_FOR_IDLE:
305 case RADEON_CP_NOP:
306 case RADEON_3D_CLEAR_ZMASK:
307/* case RADEON_CP_NEXT_CHAR:
308 case RADEON_CP_PLY_NEXTSCAN:
309 case RADEON_CP_SET_SCISSORS: */ /* probably safe but will never need them? */
310 /* these packets are safe */
311 break;
312
313 case RADEON_CP_3D_DRAW_IMMD_2:
314 case RADEON_CP_3D_DRAW_VBUF_2:
315 case RADEON_CP_3D_DRAW_INDX_2:
316 case RADEON_3D_CLEAR_HIZ:
317 /* safe but r200 only */
318 if (dev_priv->microcode_version != UCODE_R200) {
319 DRM_ERROR("Invalid 3d packet for r100-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000320 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000321 }
322 break;
323
324 case RADEON_3D_LOAD_VBPNTR:
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000325
326 if (count > 18) { /* 12 arrays max */
327 DRM_ERROR("Too large payload in 3D_LOAD_VBPNTR (count=%d)\n",
328 count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000329 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000330 }
331
332 /* carefully check packet contents */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200333 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
334
335 narrays = *cmd & ~0xc000;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000336 k = 0;
337 i = 2;
338 while ((k < narrays) && (i < (count + 2))) {
339 i++; /* skip attribute field */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200340 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, i);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000341 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200342 cmd)) {
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000343 DRM_ERROR
344 ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
345 k, i);
Eric Anholt20caafa2007-08-25 19:22:43 +1000346 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000347 }
348 k++;
349 i++;
350 if (k == narrays)
351 break;
352 /* have one more to process, they come in pairs */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200353 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, i);
354
Eric Anholt6c340ea2007-08-25 20:23:09 +1000355 if (radeon_check_and_fixup_offset(dev_priv,
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200356 file_priv, cmd))
Eric Anholt6c340ea2007-08-25 20:23:09 +1000357 {
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000358 DRM_ERROR
359 ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
360 k, i);
Eric Anholt20caafa2007-08-25 19:22:43 +1000361 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000362 }
363 k++;
364 i++;
365 }
366 /* do the counts match what we expect ? */
367 if ((k != narrays) || (i != (count + 2))) {
368 DRM_ERROR
369 ("Malformed 3D_LOAD_VBPNTR packet (k=%d i=%d narrays=%d count+1=%d).\n",
370 k, i, narrays, count + 1);
Eric Anholt20caafa2007-08-25 19:22:43 +1000371 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000372 }
373 break;
374
375 case RADEON_3D_RNDR_GEN_INDX_PRIM:
376 if (dev_priv->microcode_version != UCODE_R100) {
377 DRM_ERROR("Invalid 3d packet for r200-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000378 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000379 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200380
381 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
382 if (radeon_check_and_fixup_offset(dev_priv, file_priv, cmd)) {
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000383 DRM_ERROR("Invalid rndr_gen_indx offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000384 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000385 }
386 break;
387
388 case RADEON_CP_INDX_BUFFER:
389 if (dev_priv->microcode_version != UCODE_R200) {
390 DRM_ERROR("Invalid 3d packet for r100-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000391 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000392 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200393
394 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
395 if ((*cmd & 0x8000ffff) != 0x80000810) {
396 DRM_ERROR("Invalid indx_buffer reg address %08X\n", *cmd);
Eric Anholt20caafa2007-08-25 19:22:43 +1000397 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000398 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200399 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 2);
400 if (radeon_check_and_fixup_offset(dev_priv, file_priv, cmd)) {
401 DRM_ERROR("Invalid indx_buffer offset is %08X\n", *cmd);
Eric Anholt20caafa2007-08-25 19:22:43 +1000402 return -EINVAL;
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000403 }
404 break;
405
406 case RADEON_CNTL_HOSTDATA_BLT:
407 case RADEON_CNTL_PAINT_MULTI:
408 case RADEON_CNTL_BITBLT_MULTI:
409 /* MSB of opcode: next DWORD GUI_CNTL */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200410 cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
411 if (*cmd & (RADEON_GMC_SRC_PITCH_OFFSET_CNTL
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 | RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200413 u32 *cmd2 = drm_buffer_pointer_to_dword(cmdbuf->buffer, 2);
414 offset = *cmd2 << 10;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000415 if (radeon_check_and_fixup_offset
Eric Anholt6c340ea2007-08-25 20:23:09 +1000416 (dev_priv, file_priv, &offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 DRM_ERROR("Invalid first packet offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000418 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200420 *cmd2 = (*cmd2 & 0xffc00000) | offset >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200423 if ((*cmd & RADEON_GMC_SRC_PITCH_OFFSET_CNTL) &&
424 (*cmd & RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
425 u32 *cmd3 = drm_buffer_pointer_to_dword(cmdbuf->buffer, 3);
426 offset = *cmd << 10;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427 if (radeon_check_and_fixup_offset
Eric Anholt6c340ea2007-08-25 20:23:09 +1000428 (dev_priv, file_priv, &offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 DRM_ERROR("Invalid second packet offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200432 *cmd3 = (*cmd3 & 0xffc00000) | offset >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Roland Scheideggera1aa2892006-10-24 21:45:00 +1000434 break;
435
436 default:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +0200437 DRM_ERROR("Invalid packet type %x\n", *cmd & 0xff00);
Eric Anholt20caafa2007-08-25 19:22:43 +1000438 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
441 return 0;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/* ================================================================
445 * CP hardware state programming functions
446 */
447
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448static __inline__ void radeon_emit_clip_rect(drm_radeon_private_t * dev_priv,
Dave Airliec60ce622007-07-11 15:27:12 +1000449 struct drm_clip_rect * box)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 RING_LOCALS;
452
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000453 DRM_DEBUG(" box: x1=%d y1=%d x2=%d y2=%d\n",
454 box->x1, box->y1, box->x2, box->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 BEGIN_RING(4);
457 OUT_RING(CP_PACKET0(RADEON_RE_TOP_LEFT, 0));
458 OUT_RING((box->y1 << 16) | box->x1);
459 OUT_RING(CP_PACKET0(RADEON_RE_WIDTH_HEIGHT, 0));
460 OUT_RING(((box->y2 - 1) << 16) | (box->x2 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 ADVANCE_RING();
462}
463
464/* Emit 1.1 state
465 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466static int radeon_emit_state(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000467 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 drm_radeon_context_regs_t * ctx,
469 drm_radeon_texture_regs_t * tex,
470 unsigned int dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 DRM_DEBUG("dirty=0x%08x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 if (dirty & RADEON_UPLOAD_CONTEXT) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000476 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 &ctx->rb3d_depthoffset)) {
478 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
Eric Anholt6c340ea2007-08-25 20:23:09 +1000482 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483 &ctx->rb3d_coloroffset)) {
484 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 BEGIN_RING(14);
489 OUT_RING(CP_PACKET0(RADEON_PP_MISC, 6));
490 OUT_RING(ctx->pp_misc);
491 OUT_RING(ctx->pp_fog_color);
492 OUT_RING(ctx->re_solid_color);
493 OUT_RING(ctx->rb3d_blendcntl);
494 OUT_RING(ctx->rb3d_depthoffset);
495 OUT_RING(ctx->rb3d_depthpitch);
496 OUT_RING(ctx->rb3d_zstencilcntl);
497 OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 2));
498 OUT_RING(ctx->pp_cntl);
499 OUT_RING(ctx->rb3d_cntl);
500 OUT_RING(ctx->rb3d_coloroffset);
501 OUT_RING(CP_PACKET0(RADEON_RB3D_COLORPITCH, 0));
502 OUT_RING(ctx->rb3d_colorpitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ADVANCE_RING();
504 }
505
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 if (dirty & RADEON_UPLOAD_VERTFMT) {
507 BEGIN_RING(2);
508 OUT_RING(CP_PACKET0(RADEON_SE_COORD_FMT, 0));
509 OUT_RING(ctx->se_coord_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ADVANCE_RING();
511 }
512
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513 if (dirty & RADEON_UPLOAD_LINE) {
514 BEGIN_RING(5);
515 OUT_RING(CP_PACKET0(RADEON_RE_LINE_PATTERN, 1));
516 OUT_RING(ctx->re_line_pattern);
517 OUT_RING(ctx->re_line_state);
518 OUT_RING(CP_PACKET0(RADEON_SE_LINE_WIDTH, 0));
519 OUT_RING(ctx->se_line_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ADVANCE_RING();
521 }
522
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000523 if (dirty & RADEON_UPLOAD_BUMPMAP) {
524 BEGIN_RING(5);
525 OUT_RING(CP_PACKET0(RADEON_PP_LUM_MATRIX, 0));
526 OUT_RING(ctx->pp_lum_matrix);
527 OUT_RING(CP_PACKET0(RADEON_PP_ROT_MATRIX_0, 1));
528 OUT_RING(ctx->pp_rot_matrix_0);
529 OUT_RING(ctx->pp_rot_matrix_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ADVANCE_RING();
531 }
532
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 if (dirty & RADEON_UPLOAD_MASKS) {
534 BEGIN_RING(4);
535 OUT_RING(CP_PACKET0(RADEON_RB3D_STENCILREFMASK, 2));
536 OUT_RING(ctx->rb3d_stencilrefmask);
537 OUT_RING(ctx->rb3d_ropcntl);
538 OUT_RING(ctx->rb3d_planemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 ADVANCE_RING();
540 }
541
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000542 if (dirty & RADEON_UPLOAD_VIEWPORT) {
543 BEGIN_RING(7);
544 OUT_RING(CP_PACKET0(RADEON_SE_VPORT_XSCALE, 5));
545 OUT_RING(ctx->se_vport_xscale);
546 OUT_RING(ctx->se_vport_xoffset);
547 OUT_RING(ctx->se_vport_yscale);
548 OUT_RING(ctx->se_vport_yoffset);
549 OUT_RING(ctx->se_vport_zscale);
550 OUT_RING(ctx->se_vport_zoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ADVANCE_RING();
552 }
553
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 if (dirty & RADEON_UPLOAD_SETUP) {
555 BEGIN_RING(4);
556 OUT_RING(CP_PACKET0(RADEON_SE_CNTL, 0));
557 OUT_RING(ctx->se_cntl);
558 OUT_RING(CP_PACKET0(RADEON_SE_CNTL_STATUS, 0));
559 OUT_RING(ctx->se_cntl_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ADVANCE_RING();
561 }
562
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 if (dirty & RADEON_UPLOAD_MISC) {
564 BEGIN_RING(2);
565 OUT_RING(CP_PACKET0(RADEON_RE_MISC, 0));
566 OUT_RING(ctx->re_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ADVANCE_RING();
568 }
569
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 if (dirty & RADEON_UPLOAD_TEX0) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000571 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 &tex[0].pp_txoffset)) {
573 DRM_ERROR("Invalid texture offset for unit 0\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 BEGIN_RING(9);
578 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_0, 5));
579 OUT_RING(tex[0].pp_txfilter);
580 OUT_RING(tex[0].pp_txformat);
581 OUT_RING(tex[0].pp_txoffset);
582 OUT_RING(tex[0].pp_txcblend);
583 OUT_RING(tex[0].pp_txablend);
584 OUT_RING(tex[0].pp_tfactor);
585 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_0, 0));
586 OUT_RING(tex[0].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ADVANCE_RING();
588 }
589
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 if (dirty & RADEON_UPLOAD_TEX1) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000591 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 &tex[1].pp_txoffset)) {
593 DRM_ERROR("Invalid texture offset for unit 1\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 BEGIN_RING(9);
598 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_1, 5));
599 OUT_RING(tex[1].pp_txfilter);
600 OUT_RING(tex[1].pp_txformat);
601 OUT_RING(tex[1].pp_txoffset);
602 OUT_RING(tex[1].pp_txcblend);
603 OUT_RING(tex[1].pp_txablend);
604 OUT_RING(tex[1].pp_tfactor);
605 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_1, 0));
606 OUT_RING(tex[1].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ADVANCE_RING();
608 }
609
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 if (dirty & RADEON_UPLOAD_TEX2) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000611 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 &tex[2].pp_txoffset)) {
613 DRM_ERROR("Invalid texture offset for unit 2\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000614 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 BEGIN_RING(9);
618 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_2, 5));
619 OUT_RING(tex[2].pp_txfilter);
620 OUT_RING(tex[2].pp_txformat);
621 OUT_RING(tex[2].pp_txoffset);
622 OUT_RING(tex[2].pp_txcblend);
623 OUT_RING(tex[2].pp_txablend);
624 OUT_RING(tex[2].pp_tfactor);
625 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_2, 0));
626 OUT_RING(tex[2].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 ADVANCE_RING();
628 }
629
630 return 0;
631}
632
633/* Emit 1.2 state
634 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635static int radeon_emit_state2(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000636 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 drm_radeon_state_t * state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 RING_LOCALS;
640
641 if (state->dirty & RADEON_UPLOAD_ZBIAS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 BEGIN_RING(3);
643 OUT_RING(CP_PACKET0(RADEON_SE_ZBIAS_FACTOR, 1));
644 OUT_RING(state->context2.se_zbias_factor);
645 OUT_RING(state->context2.se_zbias_constant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ADVANCE_RING();
647 }
648
Eric Anholt6c340ea2007-08-25 20:23:09 +1000649 return radeon_emit_state(dev_priv, file_priv, &state->context,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 state->tex, state->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/* New (1.3) state mechanism. 3 commands (packet, scalar, vector) in
654 * 1.3 cmdbuffers allow all previous state to be updated as well as
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 * the tcl scalar and vector areas.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657static struct {
658 int start;
659 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 const char *name;
661} packet[RADEON_MAX_STATE_PACKETS] = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 {RADEON_PP_MISC, 7, "RADEON_PP_MISC"},
663 {RADEON_PP_CNTL, 3, "RADEON_PP_CNTL"},
664 {RADEON_RB3D_COLORPITCH, 1, "RADEON_RB3D_COLORPITCH"},
665 {RADEON_RE_LINE_PATTERN, 2, "RADEON_RE_LINE_PATTERN"},
666 {RADEON_SE_LINE_WIDTH, 1, "RADEON_SE_LINE_WIDTH"},
667 {RADEON_PP_LUM_MATRIX, 1, "RADEON_PP_LUM_MATRIX"},
668 {RADEON_PP_ROT_MATRIX_0, 2, "RADEON_PP_ROT_MATRIX_0"},
669 {RADEON_RB3D_STENCILREFMASK, 3, "RADEON_RB3D_STENCILREFMASK"},
670 {RADEON_SE_VPORT_XSCALE, 6, "RADEON_SE_VPORT_XSCALE"},
671 {RADEON_SE_CNTL, 2, "RADEON_SE_CNTL"},
672 {RADEON_SE_CNTL_STATUS, 1, "RADEON_SE_CNTL_STATUS"},
673 {RADEON_RE_MISC, 1, "RADEON_RE_MISC"},
674 {RADEON_PP_TXFILTER_0, 6, "RADEON_PP_TXFILTER_0"},
675 {RADEON_PP_BORDER_COLOR_0, 1, "RADEON_PP_BORDER_COLOR_0"},
676 {RADEON_PP_TXFILTER_1, 6, "RADEON_PP_TXFILTER_1"},
677 {RADEON_PP_BORDER_COLOR_1, 1, "RADEON_PP_BORDER_COLOR_1"},
678 {RADEON_PP_TXFILTER_2, 6, "RADEON_PP_TXFILTER_2"},
679 {RADEON_PP_BORDER_COLOR_2, 1, "RADEON_PP_BORDER_COLOR_2"},
680 {RADEON_SE_ZBIAS_FACTOR, 2, "RADEON_SE_ZBIAS_FACTOR"},
681 {RADEON_SE_TCL_OUTPUT_VTX_FMT, 11, "RADEON_SE_TCL_OUTPUT_VTX_FMT"},
682 {RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED, 17,
683 "RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED"},
684 {R200_PP_TXCBLEND_0, 4, "R200_PP_TXCBLEND_0"},
685 {R200_PP_TXCBLEND_1, 4, "R200_PP_TXCBLEND_1"},
686 {R200_PP_TXCBLEND_2, 4, "R200_PP_TXCBLEND_2"},
687 {R200_PP_TXCBLEND_3, 4, "R200_PP_TXCBLEND_3"},
688 {R200_PP_TXCBLEND_4, 4, "R200_PP_TXCBLEND_4"},
689 {R200_PP_TXCBLEND_5, 4, "R200_PP_TXCBLEND_5"},
690 {R200_PP_TXCBLEND_6, 4, "R200_PP_TXCBLEND_6"},
691 {R200_PP_TXCBLEND_7, 4, "R200_PP_TXCBLEND_7"},
692 {R200_SE_TCL_LIGHT_MODEL_CTL_0, 6, "R200_SE_TCL_LIGHT_MODEL_CTL_0"},
693 {R200_PP_TFACTOR_0, 6, "R200_PP_TFACTOR_0"},
694 {R200_SE_VTX_FMT_0, 4, "R200_SE_VTX_FMT_0"},
695 {R200_SE_VAP_CNTL, 1, "R200_SE_VAP_CNTL"},
696 {R200_SE_TCL_MATRIX_SEL_0, 5, "R200_SE_TCL_MATRIX_SEL_0"},
697 {R200_SE_TCL_TEX_PROC_CTL_2, 5, "R200_SE_TCL_TEX_PROC_CTL_2"},
698 {R200_SE_TCL_UCP_VERT_BLEND_CTL, 1, "R200_SE_TCL_UCP_VERT_BLEND_CTL"},
699 {R200_PP_TXFILTER_0, 6, "R200_PP_TXFILTER_0"},
700 {R200_PP_TXFILTER_1, 6, "R200_PP_TXFILTER_1"},
701 {R200_PP_TXFILTER_2, 6, "R200_PP_TXFILTER_2"},
702 {R200_PP_TXFILTER_3, 6, "R200_PP_TXFILTER_3"},
703 {R200_PP_TXFILTER_4, 6, "R200_PP_TXFILTER_4"},
704 {R200_PP_TXFILTER_5, 6, "R200_PP_TXFILTER_5"},
705 {R200_PP_TXOFFSET_0, 1, "R200_PP_TXOFFSET_0"},
706 {R200_PP_TXOFFSET_1, 1, "R200_PP_TXOFFSET_1"},
707 {R200_PP_TXOFFSET_2, 1, "R200_PP_TXOFFSET_2"},
708 {R200_PP_TXOFFSET_3, 1, "R200_PP_TXOFFSET_3"},
709 {R200_PP_TXOFFSET_4, 1, "R200_PP_TXOFFSET_4"},
710 {R200_PP_TXOFFSET_5, 1, "R200_PP_TXOFFSET_5"},
711 {R200_SE_VTE_CNTL, 1, "R200_SE_VTE_CNTL"},
Dave Airlied985c102006-01-02 21:32:48 +1100712 {R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1,
713 "R200_SE_TCL_OUTPUT_VTX_COMP_SEL"},
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 {R200_PP_TAM_DEBUG3, 1, "R200_PP_TAM_DEBUG3"},
715 {R200_PP_CNTL_X, 1, "R200_PP_CNTL_X"},
716 {R200_RB3D_DEPTHXY_OFFSET, 1, "R200_RB3D_DEPTHXY_OFFSET"},
717 {R200_RE_AUX_SCISSOR_CNTL, 1, "R200_RE_AUX_SCISSOR_CNTL"},
718 {R200_RE_SCISSOR_TL_0, 2, "R200_RE_SCISSOR_TL_0"},
719 {R200_RE_SCISSOR_TL_1, 2, "R200_RE_SCISSOR_TL_1"},
720 {R200_RE_SCISSOR_TL_2, 2, "R200_RE_SCISSOR_TL_2"},
721 {R200_SE_VAP_CNTL_STATUS, 1, "R200_SE_VAP_CNTL_STATUS"},
722 {R200_SE_VTX_STATE_CNTL, 1, "R200_SE_VTX_STATE_CNTL"},
723 {R200_RE_POINTSIZE, 1, "R200_RE_POINTSIZE"},
724 {R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0, 4,
725 "R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0"},
726 {R200_PP_CUBIC_FACES_0, 1, "R200_PP_CUBIC_FACES_0"}, /* 61 */
Dave Airlied985c102006-01-02 21:32:48 +1100727 {R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0"}, /* 62 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 {R200_PP_CUBIC_FACES_1, 1, "R200_PP_CUBIC_FACES_1"},
729 {R200_PP_CUBIC_OFFSET_F1_1, 5, "R200_PP_CUBIC_OFFSET_F1_1"},
730 {R200_PP_CUBIC_FACES_2, 1, "R200_PP_CUBIC_FACES_2"},
731 {R200_PP_CUBIC_OFFSET_F1_2, 5, "R200_PP_CUBIC_OFFSET_F1_2"},
732 {R200_PP_CUBIC_FACES_3, 1, "R200_PP_CUBIC_FACES_3"},
733 {R200_PP_CUBIC_OFFSET_F1_3, 5, "R200_PP_CUBIC_OFFSET_F1_3"},
734 {R200_PP_CUBIC_FACES_4, 1, "R200_PP_CUBIC_FACES_4"},
735 {R200_PP_CUBIC_OFFSET_F1_4, 5, "R200_PP_CUBIC_OFFSET_F1_4"},
736 {R200_PP_CUBIC_FACES_5, 1, "R200_PP_CUBIC_FACES_5"},
737 {R200_PP_CUBIC_OFFSET_F1_5, 5, "R200_PP_CUBIC_OFFSET_F1_5"},
738 {RADEON_PP_TEX_SIZE_0, 2, "RADEON_PP_TEX_SIZE_0"},
739 {RADEON_PP_TEX_SIZE_1, 2, "RADEON_PP_TEX_SIZE_1"},
740 {RADEON_PP_TEX_SIZE_2, 2, "RADEON_PP_TEX_SIZE_2"},
741 {R200_RB3D_BLENDCOLOR, 3, "R200_RB3D_BLENDCOLOR"},
742 {R200_SE_TCL_POINT_SPRITE_CNTL, 1, "R200_SE_TCL_POINT_SPRITE_CNTL"},
743 {RADEON_PP_CUBIC_FACES_0, 1, "RADEON_PP_CUBIC_FACES_0"},
744 {RADEON_PP_CUBIC_OFFSET_T0_0, 5, "RADEON_PP_CUBIC_OFFSET_T0_0"},
745 {RADEON_PP_CUBIC_FACES_1, 1, "RADEON_PP_CUBIC_FACES_1"},
746 {RADEON_PP_CUBIC_OFFSET_T1_0, 5, "RADEON_PP_CUBIC_OFFSET_T1_0"},
747 {RADEON_PP_CUBIC_FACES_2, 1, "RADEON_PP_CUBIC_FACES_2"},
748 {RADEON_PP_CUBIC_OFFSET_T2_0, 5, "RADEON_PP_CUBIC_OFFSET_T2_0"},
749 {R200_PP_TRI_PERF, 2, "R200_PP_TRI_PERF"},
Dave Airlied985c102006-01-02 21:32:48 +1100750 {R200_PP_AFS_0, 32, "R200_PP_AFS_0"}, /* 85 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 {R200_PP_AFS_1, 32, "R200_PP_AFS_1"},
752 {R200_PP_TFACTOR_0, 8, "R200_ATF_TFACTOR"},
753 {R200_PP_TXFILTER_0, 8, "R200_PP_TXCTLALL_0"},
754 {R200_PP_TXFILTER_1, 8, "R200_PP_TXCTLALL_1"},
755 {R200_PP_TXFILTER_2, 8, "R200_PP_TXCTLALL_2"},
756 {R200_PP_TXFILTER_3, 8, "R200_PP_TXCTLALL_3"},
757 {R200_PP_TXFILTER_4, 8, "R200_PP_TXCTLALL_4"},
758 {R200_PP_TXFILTER_5, 8, "R200_PP_TXCTLALL_5"},
Dave Airlied6fece02006-06-24 17:04:07 +1000759 {R200_VAP_PVS_CNTL_1, 2, "R200_VAP_PVS_CNTL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760};
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/* ================================================================
763 * Performance monitoring functions
764 */
765
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000766static void radeon_clear_box(drm_radeon_private_t * dev_priv,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000767 struct drm_radeon_master_private *master_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 u32 color;
771 RING_LOCALS;
772
Dave Airlie7c1c2872008-11-28 14:22:24 +1000773 x += master_priv->sarea_priv->boxes[0].x1;
774 y += master_priv->sarea_priv->boxes[0].y1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 switch (dev_priv->color_fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case RADEON_COLOR_FORMAT_RGB565:
778 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781 case RADEON_COLOR_FORMAT_ARGB8888:
782 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 }
786
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 BEGIN_RING(4);
788 RADEON_WAIT_UNTIL_3D_IDLE();
789 OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
790 OUT_RING(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ADVANCE_RING();
792
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 OUT_RING(CP_PACKET3(RADEON_CNTL_PAINT_MULTI, 4));
796 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
797 RADEON_GMC_BRUSH_SOLID_COLOR |
798 (dev_priv->color_fmt << 8) |
799 RADEON_GMC_SRC_DATATYPE_COLOR |
800 RADEON_ROP3_P | RADEON_GMC_CLR_CMP_CNTL_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Dave Airlie7c1c2872008-11-28 14:22:24 +1000802 if (master_priv->sarea_priv->pfCurrentPage == 1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 OUT_RING(dev_priv->front_pitch_offset);
804 } else {
805 OUT_RING(dev_priv->back_pitch_offset);
806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 OUT_RING((x << 16) | y);
811 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 ADVANCE_RING();
814}
815
Dave Airlie7c1c2872008-11-28 14:22:24 +1000816static 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 -0700817{
818 /* Collapse various things into a wait flag -- trying to
819 * guess if userspase slept -- better just to have them tell us.
820 */
821 if (dev_priv->stats.last_frame_reads > 1 ||
822 dev_priv->stats.last_clear_reads > dev_priv->stats.clears) {
823 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
824 }
825
826 if (dev_priv->stats.freelist_loops) {
827 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
828 }
829
830 /* Purple box for page flipping
831 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 if (dev_priv->stats.boxes & RADEON_BOX_FLIP)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000833 radeon_clear_box(dev_priv, master_priv, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* Red box if we have to wait for idle at any point
836 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 if (dev_priv->stats.boxes & RADEON_BOX_WAIT_IDLE)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000838 radeon_clear_box(dev_priv, master_priv, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* Blue box: lost context?
841 */
842
843 /* Yellow box for texture swaps
844 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 if (dev_priv->stats.boxes & RADEON_BOX_TEXTURE_LOAD)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000846 radeon_clear_box(dev_priv, master_priv, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /* Green box if hardware never idles (as far as we can tell)
849 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 if (!(dev_priv->stats.boxes & RADEON_BOX_DMA_IDLE))
Dave Airlie7c1c2872008-11-28 14:22:24 +1000851 radeon_clear_box(dev_priv, master_priv, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * (not a great measure, easily confused)
855 */
856 if (dev_priv->stats.requested_bufs) {
857 if (dev_priv->stats.requested_bufs > 100)
858 dev_priv->stats.requested_bufs = 100;
859
Dave Airlie7c1c2872008-11-28 14:22:24 +1000860 radeon_clear_box(dev_priv, master_priv, 4, 16,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 dev_priv->stats.requested_bufs, 4,
862 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 memset(&dev_priv->stats, 0, sizeof(dev_priv->stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/* ================================================================
870 * CP command dispatch functions
871 */
872
Dave Airlie84b1fd12007-07-11 15:53:27 +1000873static void radeon_cp_dispatch_clear(struct drm_device * dev,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000874 struct drm_master *master,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 drm_radeon_clear_t * clear,
876 drm_radeon_clear_rect_t * depth_boxes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000879 struct drm_radeon_master_private *master_priv = master->driver_priv;
880 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 drm_radeon_depth_clear_t *depth_clear = &dev_priv->depth_clear;
882 int nbox = sarea_priv->nbox;
Dave Airliec60ce622007-07-11 15:27:12 +1000883 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unsigned int flags = clear->flags;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000885 u32 rb3d_cntl = 0, rb3d_stencilrefmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int i;
887 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 DRM_DEBUG("flags = 0x%x\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 dev_priv->stats.clears++;
891
Dave Airlie7c1c2872008-11-28 14:22:24 +1000892 if (sarea_priv->pfCurrentPage == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 unsigned int tmp = flags;
894
895 flags &= ~(RADEON_FRONT | RADEON_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 if (tmp & RADEON_FRONT)
897 flags |= RADEON_BACK;
898 if (tmp & RADEON_BACK)
899 flags |= RADEON_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 if (flags & (RADEON_FRONT | RADEON_BACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* Ensure the 3D stream is idle before doing a
907 * 2D fill to clear the front or back buffer.
908 */
909 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910
911 OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
912 OUT_RING(clear->color_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 ADVANCE_RING();
915
916 /* Make sure we restore the 3D state next time.
917 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000918 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int x = pbox[i].x1;
922 int y = pbox[i].y1;
923 int w = pbox[i].x2 - x;
924 int h = pbox[i].y2 - y;
925
Márton Németh3e684ea2008-01-24 15:58:57 +1000926 DRM_DEBUG("%d,%d-%d,%d flags 0x%x\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 x, y, w, h, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 if (flags & RADEON_FRONT) {
930 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000932 OUT_RING(CP_PACKET3
933 (RADEON_CNTL_PAINT_MULTI, 4));
934 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
935 RADEON_GMC_BRUSH_SOLID_COLOR |
936 (dev_priv->
937 color_fmt << 8) |
938 RADEON_GMC_SRC_DATATYPE_COLOR |
939 RADEON_ROP3_P |
940 RADEON_GMC_CLR_CMP_CNTL_DIS);
941
942 OUT_RING(dev_priv->front_pitch_offset);
943 OUT_RING(clear->clear_color);
944
945 OUT_RING((x << 16) | y);
946 OUT_RING((w << 16) | h);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ADVANCE_RING();
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 if (flags & RADEON_BACK) {
952 BEGIN_RING(6);
953
954 OUT_RING(CP_PACKET3
955 (RADEON_CNTL_PAINT_MULTI, 4));
956 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
957 RADEON_GMC_BRUSH_SOLID_COLOR |
958 (dev_priv->
959 color_fmt << 8) |
960 RADEON_GMC_SRC_DATATYPE_COLOR |
961 RADEON_ROP3_P |
962 RADEON_GMC_CLR_CMP_CNTL_DIS);
963
964 OUT_RING(dev_priv->back_pitch_offset);
965 OUT_RING(clear->clear_color);
966
967 OUT_RING((x << 16) | y);
968 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 ADVANCE_RING();
971 }
972 }
973 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* hyper z clear */
976 /* no docs available, based on reverse engeneering by Stephane Marchesin */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977 if ((flags & (RADEON_DEPTH | RADEON_STENCIL))
978 && (flags & RADEON_CLEAR_FASTZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000981 int depthpixperline =
982 dev_priv->depth_fmt ==
983 RADEON_DEPTH_FORMAT_16BIT_INT_Z ? (dev_priv->depth_pitch /
984 2) : (dev_priv->
985 depth_pitch / 4);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 u32 clearmask;
988
989 u32 tempRB3D_DEPTHCLEARVALUE = clear->clear_depth |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 ((clear->depth_mask & 0xff) << 24);
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Make sure we restore the 3D state next time.
993 * we haven't touched any "normal" state - still need this?
994 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000995 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Airlie54a56ac2006-09-22 04:25:09 +1000997 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 && (flags & RADEON_USE_HIERZ)) {
999 /* FIXME : reverse engineer that for Rx00 cards */
1000 /* FIXME : the mask supposedly contains low-res z values. So can't set
1001 just to the max (0xff? or actually 0x3fff?), need to take z clear
1002 value into account? */
1003 /* pattern seems to work for r100, though get slight
1004 rendering errors with glxgears. If hierz is not enabled for r100,
1005 only 4 bits which indicate clear (15,16,31,32, all zero) matter, the
1006 other ones are ignored, and the same clear mask can be used. That's
1007 very different behaviour than R200 which needs different clear mask
1008 and different number of tiles to clear if hierz is enabled or not !?!
1009 */
1010 clearmask = (0xff << 22) | (0xff << 6) | 0x003f003f;
1011 } else {
1012 /* clear mask : chooses the clearing pattern.
1013 rv250: could be used to clear only parts of macrotiles
1014 (but that would get really complicated...)?
1015 bit 0 and 1 (either or both of them ?!?!) are used to
1016 not clear tile (or maybe one of the bits indicates if the tile is
1017 compressed or not), bit 2 and 3 to not clear tile 1,...,.
1018 Pattern is as follows:
1019 | 0,1 | 4,5 | 8,9 |12,13|16,17|20,21|24,25|28,29|
1020 bits -------------------------------------------------
1021 | 2,3 | 6,7 |10,11|14,15|18,19|22,23|26,27|30,31|
1022 rv100: clearmask covers 2x8 4x1 tiles, but one clear still
1023 covers 256 pixels ?!?
1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 clearmask = 0x0;
1026 }
1027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 BEGIN_RING(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 RADEON_WAIT_UNTIL_2D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 OUT_RING_REG(RADEON_RB3D_DEPTHCLEARVALUE,
1031 tempRB3D_DEPTHCLEARVALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* what offset is this exactly ? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 OUT_RING_REG(RADEON_RB3D_ZMASKOFFSET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* need ctlstat, otherwise get some strange black flickering */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001035 OUT_RING_REG(RADEON_RB3D_ZCACHE_CTLSTAT,
1036 RADEON_RB3D_ZC_FLUSH_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ADVANCE_RING();
1038
1039 for (i = 0; i < nbox; i++) {
1040 int tileoffset, nrtilesx, nrtilesy, j;
1041 /* it looks like r200 needs rv-style clears, at least if hierz is not enabled? */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001042 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 && !(dev_priv->microcode_version == UCODE_R200)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* FIXME : figure this out for r200 (when hierz is enabled). Or
1045 maybe r200 actually doesn't need to put the low-res z value into
1046 the tile cache like r100, but just needs to clear the hi-level z-buffer?
1047 Works for R100, both with hierz and without.
1048 R100 seems to operate on 2x1 8x8 tiles, but...
1049 odd: offset/nrtiles need to be 64 pix (4 block) aligned? Potentially
1050 problematic with resolutions which are not 64 pix aligned? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 tileoffset =
1052 ((pbox[i].y1 >> 3) * depthpixperline +
1053 pbox[i].x1) >> 6;
1054 nrtilesx =
1055 ((pbox[i].x2 & ~63) -
1056 (pbox[i].x1 & ~63)) >> 4;
1057 nrtilesy =
1058 (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 BEGIN_RING(4);
1061 OUT_RING(CP_PACKET3
1062 (RADEON_3D_CLEAR_ZMASK, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* first tile */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 OUT_RING(tileoffset * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 OUT_RING(nrtilesx + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ADVANCE_RING();
1070 tileoffset += depthpixperline >> 6;
1071 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 } else if (dev_priv->microcode_version == UCODE_R200) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* works for rv250. */
1074 /* find first macro tile (8x2 4x4 z-pixels on rv250) */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 tileoffset =
1076 ((pbox[i].y1 >> 3) * depthpixperline +
1077 pbox[i].x1) >> 5;
1078 nrtilesx =
1079 (pbox[i].x2 >> 5) - (pbox[i].x1 >> 5);
1080 nrtilesy =
1081 (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 BEGIN_RING(4);
1084 OUT_RING(CP_PACKET3
1085 (RADEON_3D_CLEAR_ZMASK, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* first tile */
1087 /* judging by the first tile offset needed, could possibly
1088 directly address/clear 4x4 tiles instead of 8x2 * 4x4
1089 macro tiles, though would still need clear mask for
1090 right/bottom if truely 4x4 granularity is desired ? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 OUT_RING(tileoffset * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 OUT_RING(nrtilesx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 ADVANCE_RING();
1097 tileoffset += depthpixperline >> 5;
1098 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 } else { /* rv 100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* rv100 might not need 64 pix alignment, who knows */
1101 /* offsets are, hmm, weird */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 tileoffset =
1103 ((pbox[i].y1 >> 4) * depthpixperline +
1104 pbox[i].x1) >> 6;
1105 nrtilesx =
1106 ((pbox[i].x2 & ~63) -
1107 (pbox[i].x1 & ~63)) >> 4;
1108 nrtilesy =
1109 (pbox[i].y2 >> 4) - (pbox[i].y1 >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 BEGIN_RING(4);
1112 OUT_RING(CP_PACKET3
1113 (RADEON_3D_CLEAR_ZMASK, 2));
1114 OUT_RING(tileoffset * 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 OUT_RING(nrtilesx + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 ADVANCE_RING();
1120 tileoffset += depthpixperline >> 6;
1121 }
1122 }
1123 }
1124
1125 /* TODO don't always clear all hi-level z tiles */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001126 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 && (dev_priv->microcode_version == UCODE_R200)
1128 && (flags & RADEON_USE_HIERZ))
1129 /* r100 and cards without hierarchical z-buffer have no high-level z-buffer */
1130 /* FIXME : the mask supposedly contains low-res z values. So can't set
1131 just to the max (0xff? or actually 0x3fff?), need to take z clear
1132 value into account? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001134 BEGIN_RING(4);
1135 OUT_RING(CP_PACKET3(RADEON_3D_CLEAR_HIZ, 2));
1136 OUT_RING(0x0); /* First tile */
1137 OUT_RING(0x3cc0);
1138 OUT_RING((0xff << 22) | (0xff << 6) | 0x003f003f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ADVANCE_RING();
1140 }
1141 }
1142
1143 /* We have to clear the depth and/or stencil buffers by
1144 * rendering a quad into just those buffers. Thus, we have to
1145 * make sure the 3D engine is configured correctly.
1146 */
Dave Airlied985c102006-01-02 21:32:48 +11001147 else if ((dev_priv->microcode_version == UCODE_R200) &&
1148 (flags & (RADEON_DEPTH | RADEON_STENCIL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 int tempPP_CNTL;
1151 int tempRE_CNTL;
1152 int tempRB3D_CNTL;
1153 int tempRB3D_ZSTENCILCNTL;
1154 int tempRB3D_STENCILREFMASK;
1155 int tempRB3D_PLANEMASK;
1156 int tempSE_CNTL;
1157 int tempSE_VTE_CNTL;
1158 int tempSE_VTX_FMT_0;
1159 int tempSE_VTX_FMT_1;
1160 int tempSE_VAP_CNTL;
1161 int tempRE_AUX_SCISSOR_CNTL;
1162
1163 tempPP_CNTL = 0;
1164 tempRE_CNTL = 0;
1165
1166 tempRB3D_CNTL = depth_clear->rb3d_cntl;
1167
1168 tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
1169 tempRB3D_STENCILREFMASK = 0x0;
1170
1171 tempSE_CNTL = depth_clear->se_cntl;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* Disable TCL */
1174
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175 tempSE_VAP_CNTL = ( /* SE_VAP_CNTL__FORCE_W_TO_ONE_MASK | */
1176 (0x9 <<
1177 SE_VAP_CNTL__VF_MAX_VTX_NUM__SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 tempRB3D_PLANEMASK = 0x0;
1180
1181 tempRE_AUX_SCISSOR_CNTL = 0x0;
1182
1183 tempSE_VTE_CNTL =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001184 SE_VTE_CNTL__VTX_XY_FMT_MASK | SE_VTE_CNTL__VTX_Z_FMT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001186 /* Vertex format (X, Y, Z, W) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 tempSE_VTX_FMT_0 =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001188 SE_VTX_FMT_0__VTX_Z0_PRESENT_MASK |
1189 SE_VTX_FMT_0__VTX_W0_PRESENT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 tempSE_VTX_FMT_1 = 0x0;
1191
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 /*
1193 * Depth buffer specific enables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
1195 if (flags & RADEON_DEPTH) {
1196 /* Enable depth buffer */
1197 tempRB3D_CNTL |= RADEON_Z_ENABLE;
1198 } else {
1199 /* Disable depth buffer */
1200 tempRB3D_CNTL &= ~RADEON_Z_ENABLE;
1201 }
1202
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001203 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 * Stencil buffer specific enables
1205 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 if (flags & RADEON_STENCIL) {
1207 tempRB3D_CNTL |= RADEON_STENCIL_ENABLE;
1208 tempRB3D_STENCILREFMASK = clear->depth_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 } else {
1210 tempRB3D_CNTL &= ~RADEON_STENCIL_ENABLE;
1211 tempRB3D_STENCILREFMASK = 0x00000000;
1212 }
1213
1214 if (flags & RADEON_USE_COMP_ZBUF) {
1215 tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001216 RADEON_Z_DECOMPRESSION_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218 if (flags & RADEON_USE_HIERZ) {
1219 tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
1220 }
1221
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001222 BEGIN_RING(26);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 RADEON_WAIT_UNTIL_2D_IDLE();
1224
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001225 OUT_RING_REG(RADEON_PP_CNTL, tempPP_CNTL);
1226 OUT_RING_REG(R200_RE_CNTL, tempRE_CNTL);
1227 OUT_RING_REG(RADEON_RB3D_CNTL, tempRB3D_CNTL);
1228 OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
1229 OUT_RING_REG(RADEON_RB3D_STENCILREFMASK,
1230 tempRB3D_STENCILREFMASK);
1231 OUT_RING_REG(RADEON_RB3D_PLANEMASK, tempRB3D_PLANEMASK);
1232 OUT_RING_REG(RADEON_SE_CNTL, tempSE_CNTL);
1233 OUT_RING_REG(R200_SE_VTE_CNTL, tempSE_VTE_CNTL);
1234 OUT_RING_REG(R200_SE_VTX_FMT_0, tempSE_VTX_FMT_0);
1235 OUT_RING_REG(R200_SE_VTX_FMT_1, tempSE_VTX_FMT_1);
1236 OUT_RING_REG(R200_SE_VAP_CNTL, tempSE_VAP_CNTL);
1237 OUT_RING_REG(R200_RE_AUX_SCISSOR_CNTL, tempRE_AUX_SCISSOR_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ADVANCE_RING();
1239
1240 /* Make sure we restore the 3D state next time.
1241 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001242 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001244 for (i = 0; i < nbox; i++) {
1245
1246 /* Funny that this should be required --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 * sets top-left?
1248 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001249 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251 BEGIN_RING(14);
1252 OUT_RING(CP_PACKET3(R200_3D_DRAW_IMMD_2, 12));
1253 OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
1254 RADEON_PRIM_WALK_RING |
1255 (3 << RADEON_NUM_VERTICES_SHIFT)));
1256 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1257 OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
1258 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1259 OUT_RING(0x3f800000);
1260 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1261 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1262 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1263 OUT_RING(0x3f800000);
1264 OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
1265 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1266 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1267 OUT_RING(0x3f800000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ADVANCE_RING();
1269 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001270 } else if ((flags & (RADEON_DEPTH | RADEON_STENCIL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 int tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
1273
1274 rb3d_cntl = depth_clear->rb3d_cntl;
1275
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001276 if (flags & RADEON_DEPTH) {
1277 rb3d_cntl |= RADEON_Z_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } else {
1279 rb3d_cntl &= ~RADEON_Z_ENABLE;
1280 }
1281
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001282 if (flags & RADEON_STENCIL) {
1283 rb3d_cntl |= RADEON_STENCIL_ENABLE;
1284 rb3d_stencilrefmask = clear->depth_mask; /* misnamed field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 } else {
1286 rb3d_cntl &= ~RADEON_STENCIL_ENABLE;
1287 rb3d_stencilrefmask = 0x00000000;
1288 }
1289
1290 if (flags & RADEON_USE_COMP_ZBUF) {
1291 tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001292 RADEON_Z_DECOMPRESSION_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 if (flags & RADEON_USE_HIERZ) {
1295 tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
1296 }
1297
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 BEGIN_RING(13);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 RADEON_WAIT_UNTIL_2D_IDLE();
1300
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301 OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 1));
1302 OUT_RING(0x00000000);
1303 OUT_RING(rb3d_cntl);
1304
1305 OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
1306 OUT_RING_REG(RADEON_RB3D_STENCILREFMASK, rb3d_stencilrefmask);
1307 OUT_RING_REG(RADEON_RB3D_PLANEMASK, 0x00000000);
1308 OUT_RING_REG(RADEON_SE_CNTL, depth_clear->se_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ADVANCE_RING();
1310
1311 /* Make sure we restore the 3D state next time.
1312 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001313 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001315 for (i = 0; i < nbox; i++) {
1316
1317 /* Funny that this should be required --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * sets top-left?
1319 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001322 BEGIN_RING(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001324 OUT_RING(CP_PACKET3(RADEON_3D_DRAW_IMMD, 13));
1325 OUT_RING(RADEON_VTX_Z_PRESENT |
1326 RADEON_VTX_PKCOLOR_PRESENT);
1327 OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
1328 RADEON_PRIM_WALK_RING |
1329 RADEON_MAOS_ENABLE |
1330 RADEON_VTX_FMT_RADEON_MODE |
1331 (3 << RADEON_NUM_VERTICES_SHIFT)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1334 OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
1335 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1336 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001338 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1339 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1340 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1341 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001343 OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
1344 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1345 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1346 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 ADVANCE_RING();
1349 }
1350 }
1351
1352 /* Increment the clear counter. The client-side 3D driver must
1353 * wait on this value before performing the clear ioctl. We
1354 * need this because the card's so damned fast...
1355 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001356 sarea_priv->last_clear++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001358 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Dave Airlie7c1c2872008-11-28 14:22:24 +10001360 RADEON_CLEAR_AGE(sarea_priv->last_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 RADEON_WAIT_UNTIL_IDLE();
1362
1363 ADVANCE_RING();
1364}
1365
Dave Airlie7c1c2872008-11-28 14:22:24 +10001366static void radeon_cp_dispatch_swap(struct drm_device *dev, struct drm_master *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001369 struct drm_radeon_master_private *master_priv = master->driver_priv;
1370 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 int nbox = sarea_priv->nbox;
Dave Airliec60ce622007-07-11 15:27:12 +10001372 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 int i;
1374 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001375 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* Do some trivial performance monitoring...
1378 */
1379 if (dev_priv->do_boxes)
Dave Airlie7c1c2872008-11-28 14:22:24 +10001380 radeon_cp_performance_boxes(dev_priv, master_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /* Wait for the 3D stream to idle before dispatching the bitblt.
1383 * This will prevent data corruption between the two streams.
1384 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001385 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 RADEON_WAIT_UNTIL_3D_IDLE();
1388
1389 ADVANCE_RING();
1390
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001391 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int x = pbox[i].x1;
1393 int y = pbox[i].y1;
1394 int w = pbox[i].x2 - x;
1395 int h = pbox[i].y2 - y;
1396
Márton Németh3e684ea2008-01-24 15:58:57 +10001397 DRM_DEBUG("%d,%d-%d,%d\n", x, y, w, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Michel Daenzer3e14a282006-09-22 04:26:35 +10001399 BEGIN_RING(9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Michel Daenzer3e14a282006-09-22 04:26:35 +10001401 OUT_RING(CP_PACKET0(RADEON_DP_GUI_MASTER_CNTL, 0));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001402 OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
1403 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
1404 RADEON_GMC_BRUSH_NONE |
1405 (dev_priv->color_fmt << 8) |
1406 RADEON_GMC_SRC_DATATYPE_COLOR |
1407 RADEON_ROP3_S |
1408 RADEON_DP_SRC_SOURCE_MEMORY |
1409 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* Make this work even if front & back are flipped:
1412 */
Michel Daenzer3e14a282006-09-22 04:26:35 +10001413 OUT_RING(CP_PACKET0(RADEON_SRC_PITCH_OFFSET, 1));
Dave Airlie7c1c2872008-11-28 14:22:24 +10001414 if (sarea_priv->pfCurrentPage == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 OUT_RING(dev_priv->back_pitch_offset);
1416 OUT_RING(dev_priv->front_pitch_offset);
1417 } else {
1418 OUT_RING(dev_priv->front_pitch_offset);
1419 OUT_RING(dev_priv->back_pitch_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
Michel Daenzer3e14a282006-09-22 04:26:35 +10001422 OUT_RING(CP_PACKET0(RADEON_SRC_X_Y, 2));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001423 OUT_RING((x << 16) | y);
1424 OUT_RING((x << 16) | y);
1425 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 ADVANCE_RING();
1428 }
1429
1430 /* Increment the frame counter. The client-side 3D driver must
1431 * throttle the framerate by waiting for this value before
1432 * performing the swapbuffer ioctl.
1433 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001434 sarea_priv->last_frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Dave Airlie7c1c2872008-11-28 14:22:24 +10001438 RADEON_FRAME_AGE(sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 RADEON_WAIT_UNTIL_2D_IDLE();
1440
1441 ADVANCE_RING();
1442}
1443
Dave Airlie7c1c2872008-11-28 14:22:24 +10001444void radeon_cp_dispatch_flip(struct drm_device *dev, struct drm_master *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001447 struct drm_radeon_master_private *master_priv = master->driver_priv;
1448 struct drm_sarea *sarea = (struct drm_sarea *)master_priv->sarea->handle;
1449 int offset = (master_priv->sarea_priv->pfCurrentPage == 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001450 ? dev_priv->front_offset : dev_priv->back_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001452 DRM_DEBUG("pfCurrentPage=%d\n",
Dave Airlie7c1c2872008-11-28 14:22:24 +10001453 master_priv->sarea_priv->pfCurrentPage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* Do some trivial performance monitoring...
1456 */
1457 if (dev_priv->do_boxes) {
1458 dev_priv->stats.boxes |= RADEON_BOX_FLIP;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001459 radeon_cp_performance_boxes(dev_priv, master_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
1462 /* Update the frame offsets for both CRTCs
1463 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001464 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001467 OUT_RING_REG(RADEON_CRTC_OFFSET,
1468 ((sarea->frame.y * dev_priv->front_pitch +
1469 sarea->frame.x * (dev_priv->color_fmt - 2)) & ~7)
1470 + offset);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001471 OUT_RING_REG(RADEON_CRTC2_OFFSET, master_priv->sarea_priv->crtc2_base
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001472 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 ADVANCE_RING();
1475
1476 /* Increment the frame counter. The client-side 3D driver must
1477 * throttle the framerate by waiting for this value before
1478 * performing the swapbuffer ioctl.
1479 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10001480 master_priv->sarea_priv->last_frame++;
1481 master_priv->sarea_priv->pfCurrentPage =
1482 1 - master_priv->sarea_priv->pfCurrentPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Dave Airlie7c1c2872008-11-28 14:22:24 +10001486 RADEON_FRAME_AGE(master_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 ADVANCE_RING();
1489}
1490
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001491static int bad_prim_vertex_nr(int primitive, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 switch (primitive & RADEON_PRIM_TYPE_MASK) {
1494 case RADEON_PRIM_TYPE_NONE:
1495 case RADEON_PRIM_TYPE_POINT:
1496 return nr < 1;
1497 case RADEON_PRIM_TYPE_LINE:
1498 return (nr & 1) || nr == 0;
1499 case RADEON_PRIM_TYPE_LINE_STRIP:
1500 return nr < 2;
1501 case RADEON_PRIM_TYPE_TRI_LIST:
1502 case RADEON_PRIM_TYPE_3VRT_POINT_LIST:
1503 case RADEON_PRIM_TYPE_3VRT_LINE_LIST:
1504 case RADEON_PRIM_TYPE_RECT_LIST:
1505 return nr % 3 || nr == 0;
1506 case RADEON_PRIM_TYPE_TRI_FAN:
1507 case RADEON_PRIM_TYPE_TRI_STRIP:
1508 return nr < 3;
1509 default:
1510 return 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514typedef struct {
1515 unsigned int start;
1516 unsigned int finish;
1517 unsigned int prim;
1518 unsigned int numverts;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001519 unsigned int offset;
1520 unsigned int vc_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521} drm_radeon_tcl_prim_t;
1522
Dave Airlie84b1fd12007-07-11 15:53:27 +10001523static void radeon_cp_dispatch_vertex(struct drm_device * dev,
Dave Airlie7c1c2872008-11-28 14:22:24 +10001524 struct drm_file *file_priv,
Dave Airlie056219e2007-07-11 16:17:42 +10001525 struct drm_buf * buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526 drm_radeon_tcl_prim_t * prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001529 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
1530 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int offset = dev_priv->gart_buffers_offset + buf->offset + prim->start;
1532 int numverts = (int)prim->numverts;
1533 int nbox = sarea_priv->nbox;
1534 int i = 0;
1535 RING_LOCALS;
1536
1537 DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d %d verts\n",
1538 prim->prim,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001539 prim->vc_format, prim->start, prim->finish, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001541 if (bad_prim_vertex_nr(prim->prim, prim->numverts)) {
1542 DRM_ERROR("bad prim %x numverts %d\n",
1543 prim->prim, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return;
1545 }
1546
1547 do {
1548 /* Emit the next cliprect */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001549 if (i < nbox) {
1550 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552
1553 /* Emit the vertex buffer rendering commands */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001554 BEGIN_RING(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001556 OUT_RING(CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, 3));
1557 OUT_RING(offset);
1558 OUT_RING(numverts);
1559 OUT_RING(prim->vc_format);
1560 OUT_RING(prim->prim | RADEON_PRIM_WALK_LIST |
1561 RADEON_COLOR_ORDER_RGBA |
1562 RADEON_VTX_FMT_RADEON_MODE |
1563 (numverts << RADEON_NUM_VERTICES_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 ADVANCE_RING();
1566
1567 i++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001568 } while (i < nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001571void radeon_cp_discard_buffer(struct drm_device *dev, struct drm_master *master, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001574 struct drm_radeon_master_private *master_priv = master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1576 RING_LOCALS;
1577
Dave Airlie7c1c2872008-11-28 14:22:24 +10001578 buf_priv->age = ++master_priv->sarea_priv->last_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* Emit the vertex buffer age */
Alex Deucherc05ce082009-02-24 16:22:29 -05001581 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
1582 BEGIN_RING(3);
1583 R600_DISPATCH_AGE(buf_priv->age);
1584 ADVANCE_RING();
1585 } else {
1586 BEGIN_RING(2);
1587 RADEON_DISPATCH_AGE(buf_priv->age);
1588 ADVANCE_RING();
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 buf->pending = 1;
1592 buf->used = 0;
1593}
1594
Dave Airlie84b1fd12007-07-11 15:53:27 +10001595static void radeon_cp_dispatch_indirect(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001596 struct drm_buf * buf, int start, int end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 drm_radeon_private_t *dev_priv = dev->dev_private;
1599 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001600 DRM_DEBUG("buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001602 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int offset = (dev_priv->gart_buffers_offset
1604 + buf->offset + start);
1605 int dwords = (end - start + 3) / sizeof(u32);
1606
1607 /* Indirect buffer data must be an even number of
1608 * dwords, so if we've been given an odd number we must
1609 * pad the data with a Type-2 CP packet.
1610 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 if (dwords & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 u32 *data = (u32 *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001613 ((char *)dev->agp_buffer_map->handle
1614 + buf->offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 data[dwords++] = RADEON_CP_PACKET2;
1616 }
1617
1618 /* Fire off the indirect buffer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001619 BEGIN_RING(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001621 OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
1622 OUT_RING(offset);
1623 OUT_RING(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 ADVANCE_RING();
1626 }
1627}
1628
Dave Airlie7c1c2872008-11-28 14:22:24 +10001629static void radeon_cp_dispatch_indices(struct drm_device *dev,
1630 struct drm_master *master,
Dave Airlie056219e2007-07-11 16:17:42 +10001631 struct drm_buf * elt_buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001632 drm_radeon_tcl_prim_t * prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001635 struct drm_radeon_master_private *master_priv = master->driver_priv;
1636 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 int offset = dev_priv->gart_buffers_offset + prim->offset;
1638 u32 *data;
1639 int dwords;
1640 int i = 0;
1641 int start = prim->start + RADEON_INDEX_PRIM_OFFSET;
1642 int count = (prim->finish - start) / sizeof(u16);
1643 int nbox = sarea_priv->nbox;
1644
1645 DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d offset: %x nr %d\n",
1646 prim->prim,
1647 prim->vc_format,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001648 prim->start, prim->finish, prim->offset, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001650 if (bad_prim_vertex_nr(prim->prim, count)) {
1651 DRM_ERROR("bad prim %x count %d\n", prim->prim, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return;
1653 }
1654
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001655 if (start >= prim->finish || (prim->start & 0x7)) {
1656 DRM_ERROR("buffer prim %d\n", prim->prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return;
1658 }
1659
1660 dwords = (prim->finish - prim->start + 3) / sizeof(u32);
1661
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001662 data = (u32 *) ((char *)dev->agp_buffer_map->handle +
1663 elt_buf->offset + prim->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001665 data[0] = CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, dwords - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 data[1] = offset;
1667 data[2] = prim->numverts;
1668 data[3] = prim->vc_format;
1669 data[4] = (prim->prim |
1670 RADEON_PRIM_WALK_IND |
1671 RADEON_COLOR_ORDER_RGBA |
1672 RADEON_VTX_FMT_RADEON_MODE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001673 (count << RADEON_NUM_VERTICES_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001676 if (i < nbox)
1677 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001679 radeon_cp_dispatch_indirect(dev, elt_buf,
1680 prim->start, prim->finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 i++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001683 } while (i < nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685}
1686
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001687#define RADEON_MAX_TEXTURE_SIZE RADEON_BUFFER_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Eric Anholt6c340ea2007-08-25 20:23:09 +10001689static int radeon_cp_dispatch_texture(struct drm_device * dev,
1690 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001691 drm_radeon_texture_t * tex,
1692 drm_radeon_tex_image_t * image)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie056219e2007-07-11 16:17:42 +10001695 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 u32 format;
1697 u32 *buffer;
1698 const u8 __user *data;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001699 int size, dwords, tex_width, blit_width, spitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 u32 height;
1701 int i;
1702 u32 texpitch, microtile;
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001703 u32 offset, byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 RING_LOCALS;
1705
Eric Anholt6c340ea2007-08-25 20:23:09 +10001706 if (radeon_check_and_fixup_offset(dev_priv, file_priv, &tex->offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001707 DRM_ERROR("Invalid destination offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001708 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
1711 dev_priv->stats.boxes |= RADEON_BOX_TEXTURE_LOAD;
1712
1713 /* Flush the pixel cache. This ensures no pixel data gets mixed
1714 * up with the texture data from the host data blit, otherwise
1715 * part of the texture image may be corrupted.
1716 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001717 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 RADEON_FLUSH_CACHE();
1719 RADEON_WAIT_UNTIL_IDLE();
1720 ADVANCE_RING();
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* The compiler won't optimize away a division by a variable,
1723 * even if the only legal values are powers of two. Thus, we'll
1724 * use a shift instead.
1725 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001726 switch (tex->format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 case RADEON_TXFORMAT_ARGB8888:
1728 case RADEON_TXFORMAT_RGBA8888:
1729 format = RADEON_COLOR_FORMAT_ARGB8888;
1730 tex_width = tex->width * 4;
1731 blit_width = image->width * 4;
1732 break;
1733 case RADEON_TXFORMAT_AI88:
1734 case RADEON_TXFORMAT_ARGB1555:
1735 case RADEON_TXFORMAT_RGB565:
1736 case RADEON_TXFORMAT_ARGB4444:
1737 case RADEON_TXFORMAT_VYUY422:
1738 case RADEON_TXFORMAT_YVYU422:
1739 format = RADEON_COLOR_FORMAT_RGB565;
1740 tex_width = tex->width * 2;
1741 blit_width = image->width * 2;
1742 break;
1743 case RADEON_TXFORMAT_I8:
1744 case RADEON_TXFORMAT_RGB332:
1745 format = RADEON_COLOR_FORMAT_CI8;
1746 tex_width = tex->width * 1;
1747 blit_width = image->width * 1;
1748 break;
1749 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001750 DRM_ERROR("invalid texture format %d\n", tex->format);
Eric Anholt20caafa2007-08-25 19:22:43 +10001751 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001753 spitch = blit_width >> 6;
1754 if (spitch == 0 && image->height > 1)
Eric Anholt20caafa2007-08-25 19:22:43 +10001755 return -EINVAL;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 texpitch = tex->pitch;
1758 if ((texpitch << 22) & RADEON_DST_TILE_MICRO) {
1759 microtile = 1;
1760 if (tex_width < 64) {
1761 texpitch &= ~(RADEON_DST_TILE_MICRO >> 22);
1762 /* we got tiled coordinates, untile them */
1763 image->x *= 2;
1764 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001765 } else
1766 microtile = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001768 /* this might fail for zero-sized uploads - are those illegal? */
1769 if (!radeon_check_offset(dev_priv, tex->offset + image->height *
1770 blit_width - 1)) {
1771 DRM_ERROR("Invalid final destination offset\n");
1772 return -EINVAL;
1773 }
1774
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001775 DRM_DEBUG("tex=%dx%d blit=%d\n", tex_width, tex->height, blit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001778 DRM_DEBUG("tex: ofs=0x%x p=%d f=%d x=%hd y=%hd w=%hd h=%hd\n",
1779 tex->offset >> 10, tex->pitch, tex->format,
1780 image->x, image->y, image->width, image->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /* Make a copy of some parameters in case we have to
1783 * update them for a multi-pass texture blit.
1784 */
1785 height = image->height;
1786 data = (const u8 __user *)image->data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 size = height * blit_width;
1789
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001790 if (size > RADEON_MAX_TEXTURE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 height = RADEON_MAX_TEXTURE_SIZE / blit_width;
1792 size = height * blit_width;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001793 } else if (size < 4 && size > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 size = 4;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001795 } else if (size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797 }
1798
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001799 buf = radeon_freelist_get(dev);
1800 if (0 && !buf) {
1801 radeon_do_cp_idle(dev_priv);
1802 buf = radeon_freelist_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001804 if (!buf) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001805 DRM_DEBUG("EAGAIN\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001806 if (DRM_COPY_TO_USER(tex->image, image, sizeof(*image)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001807 return -EFAULT;
1808 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* Dispatch the indirect buffer.
1812 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001813 buffer =
1814 (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 dwords = size / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Dave Airlied985c102006-01-02 21:32:48 +11001817#define RADEON_COPY_MT(_buf, _data, _width) \
1818 do { \
1819 if (DRM_COPY_FROM_USER(_buf, _data, (_width))) {\
1820 DRM_ERROR("EFAULT on pad, %d bytes\n", (_width)); \
Eric Anholt20caafa2007-08-25 19:22:43 +10001821 return -EFAULT; \
Dave Airlied985c102006-01-02 21:32:48 +11001822 } \
1823 } while(0)
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (microtile) {
1826 /* texture micro tiling in use, minimum texture width is thus 16 bytes.
1827 however, we cannot use blitter directly for texture width < 64 bytes,
1828 since minimum tex pitch is 64 bytes and we need this to match
1829 the texture width, otherwise the blitter will tile it wrong.
1830 Thus, tiling manually in this case. Additionally, need to special
1831 case tex height = 1, since our actual image will have height 2
1832 and we need to ensure we don't read beyond the texture size
1833 from user space. */
1834 if (tex->height == 1) {
1835 if (tex_width >= 64 || tex_width <= 16) {
Dave Airlied985c102006-01-02 21:32:48 +11001836 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001837 (int)(tex_width * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 } else if (tex_width == 32) {
Dave Airlied985c102006-01-02 21:32:48 +11001839 RADEON_COPY_MT(buffer, data, 16);
1840 RADEON_COPY_MT(buffer + 8,
1841 data + 16, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843 } else if (tex_width >= 64 || tex_width == 16) {
Dave Airlied985c102006-01-02 21:32:48 +11001844 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001845 (int)(dwords * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 } else if (tex_width < 16) {
1847 for (i = 0; i < tex->height; i++) {
Dave Airlied985c102006-01-02 21:32:48 +11001848 RADEON_COPY_MT(buffer, data, tex_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 buffer += 4;
1850 data += tex_width;
1851 }
1852 } else if (tex_width == 32) {
1853 /* TODO: make sure this works when not fitting in one buffer
1854 (i.e. 32bytes x 2048...) */
1855 for (i = 0; i < tex->height; i += 2) {
Dave Airlied985c102006-01-02 21:32:48 +11001856 RADEON_COPY_MT(buffer, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001858 RADEON_COPY_MT(buffer + 8, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001860 RADEON_COPY_MT(buffer + 4, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001862 RADEON_COPY_MT(buffer + 12, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 data += 16;
1864 buffer += 16;
1865 }
1866 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001867 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (tex_width >= 32) {
1869 /* Texture image width is larger than the minimum, so we
1870 * can upload it directly.
1871 */
Dave Airlied985c102006-01-02 21:32:48 +11001872 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001873 (int)(dwords * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 } else {
1875 /* Texture image width is less than the minimum, so we
1876 * need to pad out each image scanline to the minimum
1877 * width.
1878 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001879 for (i = 0; i < tex->height; i++) {
Dave Airlied985c102006-01-02 21:32:48 +11001880 RADEON_COPY_MT(buffer, data, tex_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 buffer += 8;
1882 data += tex_width;
1883 }
1884 }
1885 }
1886
Dave Airlied985c102006-01-02 21:32:48 +11001887#undef RADEON_COPY_MT
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001888 byte_offset = (image->y & ~2047) * blit_width;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001889 buf->file_priv = file_priv;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001890 buf->used = size;
1891 offset = dev_priv->gart_buffers_offset + buf->offset;
1892 BEGIN_RING(9);
1893 OUT_RING(CP_PACKET3(RADEON_CNTL_BITBLT_MULTI, 5));
1894 OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
1895 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
1896 RADEON_GMC_BRUSH_NONE |
1897 (format << 8) |
1898 RADEON_GMC_SRC_DATATYPE_COLOR |
1899 RADEON_ROP3_S |
1900 RADEON_DP_SRC_SOURCE_MEMORY |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001901 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001902 OUT_RING((spitch << 22) | (offset >> 10));
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001903 OUT_RING((texpitch << 22) | ((tex->offset >> 10) + (byte_offset >> 10)));
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001904 OUT_RING(0);
Roland Scheidegger9156cf02008-06-19 11:36:04 +10001905 OUT_RING((image->x << 16) | (image->y % 2048));
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001906 OUT_RING((image->width << 16) | height);
1907 RADEON_WAIT_UNTIL_2D_IDLE();
1908 ADVANCE_RING();
chaohong guoeed0f722007-10-15 10:45:49 +10001909 COMMIT_RING();
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001910
Dave Airlie7c1c2872008-11-28 14:22:24 +10001911 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 /* Update the input parameters for next time */
1914 image->y += height;
1915 image->height -= height;
1916 image->data = (const u8 __user *)image->data + size;
1917 } while (image->height > 0);
1918
1919 /* Flush the pixel cache after the blit completes. This ensures
1920 * the texture data is written out to memory before rendering
1921 * continues.
1922 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001923 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 RADEON_FLUSH_CACHE();
1925 RADEON_WAIT_UNTIL_2D_IDLE();
1926 ADVANCE_RING();
chaohong guoeed0f722007-10-15 10:45:49 +10001927 COMMIT_RING();
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
1930}
1931
Dave Airlie84b1fd12007-07-11 15:53:27 +10001932static void radeon_cp_dispatch_stipple(struct drm_device * dev, u32 * stipple)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 drm_radeon_private_t *dev_priv = dev->dev_private;
1935 int i;
1936 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001937 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001939 BEGIN_RING(35);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001941 OUT_RING(CP_PACKET0(RADEON_RE_STIPPLE_ADDR, 0));
1942 OUT_RING(0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001944 OUT_RING(CP_PACKET0_TABLE(RADEON_RE_STIPPLE_DATA, 31));
1945 for (i = 0; i < 32; i++) {
1946 OUT_RING(stipple[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
1949 ADVANCE_RING();
1950}
1951
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001952static void radeon_apply_surface_regs(int surf_index,
Dave Airlied985c102006-01-02 21:32:48 +11001953 drm_radeon_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 if (!dev_priv->mmio)
1956 return;
1957
1958 radeon_do_cp_idle(dev_priv);
1959
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001960 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * surf_index,
1961 dev_priv->surfaces[surf_index].flags);
1962 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * surf_index,
1963 dev_priv->surfaces[surf_index].lower);
1964 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * surf_index,
1965 dev_priv->surfaces[surf_index].upper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/* Allocates a virtual surface
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001969 * doesn't always allocate a real surface, will stretch an existing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 * surface when possible.
1971 *
1972 * Note that refcount can be at most 2, since during a free refcount=3
1973 * might mean we have to allocate a new surface which might not always
1974 * be available.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001975 * For example : we allocate three contigous surfaces ABC. If B is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 * freed, we suddenly need two surfaces to store A and C, which might
1977 * not always be available.
1978 */
Dave Airlied985c102006-01-02 21:32:48 +11001979static int alloc_surface(drm_radeon_surface_alloc_t *new,
Eric Anholt6c340ea2007-08-25 20:23:09 +10001980 drm_radeon_private_t *dev_priv,
1981 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 struct radeon_virt_surface *s;
1984 int i;
1985 int virt_surface_index;
1986 uint32_t new_upper, new_lower;
1987
1988 new_lower = new->address;
1989 new_upper = new_lower + new->size - 1;
1990
1991 /* sanity check */
1992 if ((new_lower >= new_upper) || (new->flags == 0) || (new->size == 0) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001993 ((new_upper & RADEON_SURF_ADDRESS_FIXED_MASK) !=
1994 RADEON_SURF_ADDRESS_FIXED_MASK)
1995 || ((new_lower & RADEON_SURF_ADDRESS_FIXED_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return -1;
1997
1998 /* make sure there is no overlap with existing surfaces */
1999 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
2000 if ((dev_priv->surfaces[i].refcount != 0) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002001 (((new_lower >= dev_priv->surfaces[i].lower) &&
2002 (new_lower < dev_priv->surfaces[i].upper)) ||
2003 ((new_lower < dev_priv->surfaces[i].lower) &&
2004 (new_upper > dev_priv->surfaces[i].lower)))) {
2005 return -1;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
2009 /* find a virtual surface */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002010 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++)
Hannes Eder8f497aa2009-03-05 20:14:18 +01002011 if (dev_priv->virt_surfaces[i].file_priv == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002013 if (i == 2 * RADEON_MAX_SURFACES) {
2014 return -1;
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 virt_surface_index = i;
2017
2018 /* try to reuse an existing surface */
2019 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
2020 /* extend before */
2021 if ((dev_priv->surfaces[i].refcount == 1) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002022 (new->flags == dev_priv->surfaces[i].flags) &&
2023 (new_upper + 1 == dev_priv->surfaces[i].lower)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 s = &(dev_priv->virt_surfaces[virt_surface_index]);
2025 s->surface_index = i;
2026 s->lower = new_lower;
2027 s->upper = new_upper;
2028 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002029 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 dev_priv->surfaces[i].refcount++;
2031 dev_priv->surfaces[i].lower = s->lower;
2032 radeon_apply_surface_regs(s->surface_index, dev_priv);
2033 return virt_surface_index;
2034 }
2035
2036 /* extend after */
2037 if ((dev_priv->surfaces[i].refcount == 1) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002038 (new->flags == dev_priv->surfaces[i].flags) &&
2039 (new_lower == dev_priv->surfaces[i].upper + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 s = &(dev_priv->virt_surfaces[virt_surface_index]);
2041 s->surface_index = i;
2042 s->lower = new_lower;
2043 s->upper = new_upper;
2044 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002045 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 dev_priv->surfaces[i].refcount++;
2047 dev_priv->surfaces[i].upper = s->upper;
2048 radeon_apply_surface_regs(s->surface_index, dev_priv);
2049 return virt_surface_index;
2050 }
2051 }
2052
2053 /* okay, we need a new one */
2054 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
2055 if (dev_priv->surfaces[i].refcount == 0) {
2056 s = &(dev_priv->virt_surfaces[virt_surface_index]);
2057 s->surface_index = i;
2058 s->lower = new_lower;
2059 s->upper = new_upper;
2060 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002061 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 dev_priv->surfaces[i].refcount = 1;
2063 dev_priv->surfaces[i].lower = s->lower;
2064 dev_priv->surfaces[i].upper = s->upper;
2065 dev_priv->surfaces[i].flags = s->flags;
2066 radeon_apply_surface_regs(s->surface_index, dev_priv);
2067 return virt_surface_index;
2068 }
2069 }
2070
2071 /* we didn't find anything */
2072 return -1;
2073}
2074
Eric Anholt6c340ea2007-08-25 20:23:09 +10002075static int free_surface(struct drm_file *file_priv,
2076 drm_radeon_private_t * dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002077 int lower)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 struct radeon_virt_surface *s;
2080 int i;
2081 /* find the virtual surface */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002082 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 s = &(dev_priv->virt_surfaces[i]);
Eric Anholt6c340ea2007-08-25 20:23:09 +10002084 if (s->file_priv) {
2085 if ((lower == s->lower) && (file_priv == s->file_priv))
2086 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002087 if (dev_priv->surfaces[s->surface_index].
2088 lower == s->lower)
2089 dev_priv->surfaces[s->surface_index].
2090 lower = s->upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002092 if (dev_priv->surfaces[s->surface_index].
2093 upper == s->upper)
2094 dev_priv->surfaces[s->surface_index].
2095 upper = s->lower;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 dev_priv->surfaces[s->surface_index].refcount--;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002098 if (dev_priv->surfaces[s->surface_index].
2099 refcount == 0)
2100 dev_priv->surfaces[s->surface_index].
2101 flags = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002102 s->file_priv = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002103 radeon_apply_surface_regs(s->surface_index,
2104 dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
2106 }
2107 }
2108 }
2109 return 1;
2110}
2111
Eric Anholt6c340ea2007-08-25 20:23:09 +10002112static void radeon_surfaces_release(struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002113 drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002116 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002117 if (dev_priv->virt_surfaces[i].file_priv == file_priv)
2118 free_surface(file_priv, dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002119 dev_priv->virt_surfaces[i].lower);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121}
2122
2123/* ================================================================
2124 * IOCTL functions
2125 */
Eric Anholtc153f452007-09-03 12:06:45 +10002126static int radeon_surface_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002129 drm_radeon_surface_alloc_t *alloc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Eric Anholtc153f452007-09-03 12:06:45 +10002131 if (alloc_surface(alloc, dev_priv, file_priv) == -1)
Eric Anholt20caafa2007-08-25 19:22:43 +10002132 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 else
2134 return 0;
2135}
2136
Eric Anholtc153f452007-09-03 12:06:45 +10002137static int radeon_surface_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002140 drm_radeon_surface_free_t *memfree = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Eric Anholtc153f452007-09-03 12:06:45 +10002142 if (free_surface(file_priv, dev_priv, memfree->address))
Eric Anholt20caafa2007-08-25 19:22:43 +10002143 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 else
2145 return 0;
2146}
2147
Eric Anholtc153f452007-09-03 12:06:45 +10002148static int radeon_cp_clear(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002151 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2152 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10002153 drm_radeon_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 drm_radeon_clear_rect_t depth_boxes[RADEON_NR_SAREA_CLIPRECTS];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002155 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Eric Anholt6c340ea2007-08-25 20:23:09 +10002157 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002159 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002161 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
2163
Eric Anholtc153f452007-09-03 12:06:45 +10002164 if (DRM_COPY_FROM_USER(&depth_boxes, clear->depth_boxes,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002165 sarea_priv->nbox * sizeof(depth_boxes[0])))
Eric Anholt20caafa2007-08-25 19:22:43 +10002166 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Dave Airlie7c1c2872008-11-28 14:22:24 +10002168 radeon_cp_dispatch_clear(dev, file_priv->master, clear, depth_boxes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 COMMIT_RING();
2171 return 0;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002175 */
Dave Airlie7c1c2872008-11-28 14:22:24 +10002176static int radeon_do_init_pageflip(struct drm_device *dev, struct drm_master *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002179 struct drm_radeon_master_private *master_priv = master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 RING_LOCALS;
2181
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002182 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002184 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002186 OUT_RING(CP_PACKET0(RADEON_CRTC_OFFSET_CNTL, 0));
2187 OUT_RING(RADEON_READ(RADEON_CRTC_OFFSET_CNTL) |
2188 RADEON_CRTC_OFFSET_FLIP_CNTL);
2189 OUT_RING(CP_PACKET0(RADEON_CRTC2_OFFSET_CNTL, 0));
2190 OUT_RING(RADEON_READ(RADEON_CRTC2_OFFSET_CNTL) |
2191 RADEON_CRTC_OFFSET_FLIP_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 ADVANCE_RING();
2193
2194 dev_priv->page_flipping = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Dave Airlie7c1c2872008-11-28 14:22:24 +10002196 if (master_priv->sarea_priv->pfCurrentPage != 1)
2197 master_priv->sarea_priv->pfCurrentPage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200}
2201
2202/* Swapping and flipping are different operations, need different ioctls.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002203 * They can & should be intermixed to support multiple 3d windows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 */
Eric Anholtc153f452007-09-03 12:06:45 +10002205static int radeon_cp_flip(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002208 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Eric Anholt6c340ea2007-08-25 20:23:09 +10002210 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002212 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002214 if (!dev_priv->page_flipping)
Dave Airlie7c1c2872008-11-28 14:22:24 +10002215 radeon_do_init_pageflip(dev, file_priv->master);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002216
Dave Airlie7c1c2872008-11-28 14:22:24 +10002217 radeon_cp_dispatch_flip(dev, file_priv->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 COMMIT_RING();
2220 return 0;
2221}
2222
Eric Anholtc153f452007-09-03 12:06:45 +10002223static int radeon_cp_swap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002226 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2227 drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
2228
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002229 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Eric Anholt6c340ea2007-08-25 20:23:09 +10002231 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002233 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002235 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
2237
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002238 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
2239 r600_cp_dispatch_swap(dev, file_priv);
2240 else
2241 radeon_cp_dispatch_swap(dev, file_priv->master);
Dave Airlie7c1c2872008-11-28 14:22:24 +10002242 sarea_priv->ctx_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 COMMIT_RING();
2245 return 0;
2246}
2247
Eric Anholtc153f452007-09-03 12:06:45 +10002248static int radeon_cp_vertex(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002251 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2252 drm_radeon_sarea_t *sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002253 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002254 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002255 drm_radeon_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 drm_radeon_tcl_prim_t prim;
2257
Eric Anholt6c340ea2007-08-25 20:23:09 +10002258 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Dave Airlie7c1c2872008-11-28 14:22:24 +10002260 sarea_priv = master_priv->sarea_priv;
2261
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002262 DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002263 DRM_CURRENTPID, vertex->idx, vertex->count, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Eric Anholtc153f452007-09-03 12:06:45 +10002265 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002266 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002267 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
Eric Anholtc153f452007-09-03 12:06:45 +10002270 if (vertex->prim < 0 || vertex->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
2271 DRM_ERROR("buffer prim %d\n", vertex->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10002272 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002275 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2276 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Eric Anholtc153f452007-09-03 12:06:45 +10002278 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Eric Anholt6c340ea2007-08-25 20:23:09 +10002280 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002281 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002282 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002283 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002285 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002286 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289
2290 /* Build up a prim_t record:
2291 */
Eric Anholtc153f452007-09-03 12:06:45 +10002292 if (vertex->count) {
2293 buf->used = vertex->count; /* not used? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002295 if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002296 if (radeon_emit_state(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002297 &sarea_priv->context_state,
2298 sarea_priv->tex_state,
2299 sarea_priv->dirty)) {
2300 DRM_ERROR("radeon_emit_state failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002301 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
2304 sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
2305 RADEON_UPLOAD_TEX1IMAGES |
2306 RADEON_UPLOAD_TEX2IMAGES |
2307 RADEON_REQUIRE_QUIESCENCE);
2308 }
2309
2310 prim.start = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10002311 prim.finish = vertex->count; /* unused */
2312 prim.prim = vertex->prim;
2313 prim.numverts = vertex->count;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002314 prim.vc_format = sarea_priv->vc_format;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002315
Dave Airlie7c1c2872008-11-28 14:22:24 +10002316 radeon_cp_dispatch_vertex(dev, file_priv, buf, &prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318
Eric Anholtc153f452007-09-03 12:06:45 +10002319 if (vertex->discard) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10002320 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322
2323 COMMIT_RING();
2324 return 0;
2325}
2326
Eric Anholtc153f452007-09-03 12:06:45 +10002327static int radeon_cp_indices(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002330 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2331 drm_radeon_sarea_t *sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002332 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002333 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002334 drm_radeon_indices_t *elts = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 drm_radeon_tcl_prim_t prim;
2336 int count;
2337
Eric Anholt6c340ea2007-08-25 20:23:09 +10002338 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Dave Airlie7c1c2872008-11-28 14:22:24 +10002340 sarea_priv = master_priv->sarea_priv;
2341
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002342 DRM_DEBUG("pid=%d index=%d start=%d end=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002343 DRM_CURRENTPID, elts->idx, elts->start, elts->end,
2344 elts->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Eric Anholtc153f452007-09-03 12:06:45 +10002346 if (elts->idx < 0 || elts->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002347 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002348 elts->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
Eric Anholtc153f452007-09-03 12:06:45 +10002351 if (elts->prim < 0 || elts->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
2352 DRM_ERROR("buffer prim %d\n", elts->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10002353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002356 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2357 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Eric Anholtc153f452007-09-03 12:06:45 +10002359 buf = dma->buflist[elts->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Eric Anholt6c340ea2007-08-25 20:23:09 +10002361 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002362 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002363 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002366 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002367 DRM_ERROR("sending pending buffer %d\n", elts->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002368 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370
Eric Anholtc153f452007-09-03 12:06:45 +10002371 count = (elts->end - elts->start) / sizeof(u16);
2372 elts->start -= RADEON_INDEX_PRIM_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Eric Anholtc153f452007-09-03 12:06:45 +10002374 if (elts->start & 0x7) {
2375 DRM_ERROR("misaligned buffer 0x%x\n", elts->start);
Eric Anholt20caafa2007-08-25 19:22:43 +10002376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
Eric Anholtc153f452007-09-03 12:06:45 +10002378 if (elts->start < buf->used) {
2379 DRM_ERROR("no header 0x%x - 0x%x\n", elts->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10002380 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382
Eric Anholtc153f452007-09-03 12:06:45 +10002383 buf->used = elts->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002385 if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002386 if (radeon_emit_state(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002387 &sarea_priv->context_state,
2388 sarea_priv->tex_state,
2389 sarea_priv->dirty)) {
2390 DRM_ERROR("radeon_emit_state failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002391 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
2394 sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
2395 RADEON_UPLOAD_TEX1IMAGES |
2396 RADEON_UPLOAD_TEX2IMAGES |
2397 RADEON_REQUIRE_QUIESCENCE);
2398 }
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /* Build up a prim_t record:
2401 */
Eric Anholtc153f452007-09-03 12:06:45 +10002402 prim.start = elts->start;
2403 prim.finish = elts->end;
2404 prim.prim = elts->prim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 prim.offset = 0; /* offset from start of dma buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002406 prim.numverts = RADEON_MAX_VB_VERTS; /* duh */
Dave Airlie7c1c2872008-11-28 14:22:24 +10002407 prim.vc_format = sarea_priv->vc_format;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002408
Dave Airlie7c1c2872008-11-28 14:22:24 +10002409 radeon_cp_dispatch_indices(dev, file_priv->master, buf, &prim);
Eric Anholtc153f452007-09-03 12:06:45 +10002410 if (elts->discard) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10002411 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413
2414 COMMIT_RING();
2415 return 0;
2416}
2417
Eric Anholtc153f452007-09-03 12:06:45 +10002418static int radeon_cp_texture(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002421 drm_radeon_texture_t *tex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 drm_radeon_tex_image_t image;
2423 int ret;
2424
Eric Anholt6c340ea2007-08-25 20:23:09 +10002425 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Eric Anholtc153f452007-09-03 12:06:45 +10002427 if (tex->image == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002428 DRM_ERROR("null texture image!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002432 if (DRM_COPY_FROM_USER(&image,
Eric Anholtc153f452007-09-03 12:06:45 +10002433 (drm_radeon_tex_image_t __user *) tex->image,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002434 sizeof(image)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002435 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002437 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2438 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002440 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
2441 ret = r600_cp_dispatch_texture(dev, file_priv, tex, &image);
2442 else
2443 ret = radeon_cp_dispatch_texture(dev, file_priv, tex, &image);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return ret;
2446}
2447
Eric Anholtc153f452007-09-03 12:06:45 +10002448static int radeon_cp_stipple(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002451 drm_radeon_stipple_t *stipple = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 u32 mask[32];
2453
Eric Anholt6c340ea2007-08-25 20:23:09 +10002454 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Eric Anholtc153f452007-09-03 12:06:45 +10002456 if (DRM_COPY_FROM_USER(&mask, stipple->mask, 32 * sizeof(u32)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002457 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002459 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002461 radeon_cp_dispatch_stipple(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 COMMIT_RING();
2464 return 0;
2465}
2466
Eric Anholtc153f452007-09-03 12:06:45 +10002467static int radeon_cp_indirect(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10002470 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002471 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002472 drm_radeon_indirect_t *indirect = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 RING_LOCALS;
2474
Eric Anholt6c340ea2007-08-25 20:23:09 +10002475 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Márton Németh3e684ea2008-01-24 15:58:57 +10002477 DRM_DEBUG("idx=%d s=%d e=%d d=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002478 indirect->idx, indirect->start, indirect->end,
2479 indirect->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Eric Anholtc153f452007-09-03 12:06:45 +10002481 if (indirect->idx < 0 || indirect->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002482 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002483 indirect->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486
Eric Anholtc153f452007-09-03 12:06:45 +10002487 buf = dma->buflist[indirect->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Eric Anholt6c340ea2007-08-25 20:23:09 +10002489 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002490 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002491 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002494 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002495 DRM_ERROR("sending pending buffer %d\n", indirect->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498
Eric Anholtc153f452007-09-03 12:06:45 +10002499 if (indirect->start < buf->used) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002500 DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002501 indirect->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10002502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002505 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2506 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Eric Anholtc153f452007-09-03 12:06:45 +10002508 buf->used = indirect->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 /* Dispatch the indirect buffer full of commands from the
2511 * X server. This is insecure and is thus only available to
2512 * privileged clients.
2513 */
Alex Deucherc05ce082009-02-24 16:22:29 -05002514 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
2515 r600_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
2516 else {
2517 /* Wait for the 3D stream to idle before the indirect buffer
2518 * containing 2D acceleration commands is processed.
2519 */
2520 BEGIN_RING(2);
2521 RADEON_WAIT_UNTIL_3D_IDLE();
2522 ADVANCE_RING();
2523 radeon_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002526 if (indirect->discard) {
Alex Deucherc05ce082009-02-24 16:22:29 -05002527 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002528 }
Alex Deucherc05ce082009-02-24 16:22:29 -05002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 COMMIT_RING();
2531 return 0;
2532}
2533
Eric Anholtc153f452007-09-03 12:06:45 +10002534static int radeon_cp_vertex2(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10002537 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
2538 drm_radeon_sarea_t *sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002539 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002540 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002541 drm_radeon_vertex2_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 int i;
2543 unsigned char laststate;
2544
Eric Anholt6c340ea2007-08-25 20:23:09 +10002545 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Dave Airlie7c1c2872008-11-28 14:22:24 +10002547 sarea_priv = master_priv->sarea_priv;
2548
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002549 DRM_DEBUG("pid=%d index=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002550 DRM_CURRENTPID, vertex->idx, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Eric Anholtc153f452007-09-03 12:06:45 +10002552 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002553 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002554 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002558 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2559 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Eric Anholtc153f452007-09-03 12:06:45 +10002561 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Eric Anholt6c340ea2007-08-25 20:23:09 +10002563 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002564 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002565 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002566 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002569 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002570 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002571 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Eric Anholt20caafa2007-08-25 19:22:43 +10002575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Eric Anholtc153f452007-09-03 12:06:45 +10002577 for (laststate = 0xff, i = 0; i < vertex->nr_prims; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 drm_radeon_prim_t prim;
2579 drm_radeon_tcl_prim_t tclprim;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002580
Eric Anholtc153f452007-09-03 12:06:45 +10002581 if (DRM_COPY_FROM_USER(&prim, &vertex->prim[i], sizeof(prim)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002582 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002583
2584 if (prim.stateidx != laststate) {
2585 drm_radeon_state_t state;
2586
2587 if (DRM_COPY_FROM_USER(&state,
Eric Anholtc153f452007-09-03 12:06:45 +10002588 &vertex->state[prim.stateidx],
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002589 sizeof(state)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002590 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Eric Anholt6c340ea2007-08-25 20:23:09 +10002592 if (radeon_emit_state2(dev_priv, file_priv, &state)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002593 DRM_ERROR("radeon_emit_state2 failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596
2597 laststate = prim.stateidx;
2598 }
2599
2600 tclprim.start = prim.start;
2601 tclprim.finish = prim.finish;
2602 tclprim.prim = prim.prim;
2603 tclprim.vc_format = prim.vc_format;
2604
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002605 if (prim.prim & RADEON_PRIM_WALK_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 tclprim.offset = prim.numverts * 64;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002607 tclprim.numverts = RADEON_MAX_VB_VERTS; /* duh */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Dave Airlie7c1c2872008-11-28 14:22:24 +10002609 radeon_cp_dispatch_indices(dev, file_priv->master, buf, &tclprim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 } else {
2611 tclprim.numverts = prim.numverts;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002612 tclprim.offset = 0; /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Dave Airlie7c1c2872008-11-28 14:22:24 +10002614 radeon_cp_dispatch_vertex(dev, file_priv, buf, &tclprim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (sarea_priv->nbox == 1)
2618 sarea_priv->nbox = 0;
2619 }
2620
Eric Anholtc153f452007-09-03 12:06:45 +10002621 if (vertex->discard) {
Dave Airlie7c1c2872008-11-28 14:22:24 +10002622 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624
2625 COMMIT_RING();
2626 return 0;
2627}
2628
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002629static int radeon_emit_packets(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002630 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002631 drm_radeon_cmd_header_t header,
Dave Airlieb3a83632005-09-30 18:37:36 +10002632 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 int id = (int)header.packet.packet_id;
2635 int sz, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (id >= RADEON_MAX_STATE_PACKETS)
Eric Anholt20caafa2007-08-25 19:22:43 +10002639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 sz = packet[id].len;
2642 reg = packet[id].start;
2643
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002644 if (sz * sizeof(u32) > drm_buffer_unprocessed(cmdbuf->buffer)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002645 DRM_ERROR("Packet size provided larger than data provided\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002649 if (radeon_check_and_fixup_packets(dev_priv, file_priv, id,
2650 cmdbuf->buffer)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002651 DRM_ERROR("Packet verification failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002655 BEGIN_RING(sz + 1);
2656 OUT_RING(CP_PACKET0(reg, (sz - 1)));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002657 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 ADVANCE_RING();
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return 0;
2661}
2662
Dave Airlied985c102006-01-02 21:32:48 +11002663static __inline__ int radeon_emit_scalars(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002664 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002665 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
2667 int sz = header.scalars.count;
2668 int start = header.scalars.offset;
2669 int stride = header.scalars.stride;
2670 RING_LOCALS;
2671
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002672 BEGIN_RING(3 + sz);
2673 OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
2674 OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
2675 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002676 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 ADVANCE_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 0;
2679}
2680
2681/* God this is ugly
2682 */
Dave Airlied985c102006-01-02 21:32:48 +11002683static __inline__ int radeon_emit_scalars2(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002684 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002685 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 int sz = header.scalars.count;
2688 int start = ((unsigned int)header.scalars.offset) + 0x100;
2689 int stride = header.scalars.stride;
2690 RING_LOCALS;
2691
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002692 BEGIN_RING(3 + sz);
2693 OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
2694 OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
2695 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002696 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 ADVANCE_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return 0;
2699}
2700
Dave Airlied985c102006-01-02 21:32:48 +11002701static __inline__ int radeon_emit_vectors(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002702 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002703 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
2705 int sz = header.vectors.count;
2706 int start = header.vectors.offset;
2707 int stride = header.vectors.stride;
2708 RING_LOCALS;
2709
Dave Airlief2a22792006-06-24 16:55:34 +10002710 BEGIN_RING(5 + sz);
2711 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002712 OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
2713 OUT_RING(start | (stride << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
2714 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002715 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 ADVANCE_RING();
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return 0;
2719}
2720
Dave Airlied6fece02006-06-24 17:04:07 +10002721static __inline__ int radeon_emit_veclinear(drm_radeon_private_t *dev_priv,
2722 drm_radeon_cmd_header_t header,
2723 drm_radeon_kcmd_buffer_t *cmdbuf)
2724{
2725 int sz = header.veclinear.count * 4;
2726 int start = header.veclinear.addr_lo | (header.veclinear.addr_hi << 8);
2727 RING_LOCALS;
2728
2729 if (!sz)
2730 return 0;
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002731 if (sz * 4 > drm_buffer_unprocessed(cmdbuf->buffer))
Eric Anholt20caafa2007-08-25 19:22:43 +10002732 return -EINVAL;
Dave Airlied6fece02006-06-24 17:04:07 +10002733
2734 BEGIN_RING(5 + sz);
2735 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
2736 OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
2737 OUT_RING(start | (1 << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
2738 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002739 OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
Dave Airlied6fece02006-06-24 17:04:07 +10002740 ADVANCE_RING();
2741
Dave Airlied6fece02006-06-24 17:04:07 +10002742 return 0;
2743}
2744
Dave Airlie84b1fd12007-07-11 15:53:27 +10002745static int radeon_emit_packet3(struct drm_device * dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002746 struct drm_file *file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +10002747 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 drm_radeon_private_t *dev_priv = dev->dev_private;
2750 unsigned int cmdsz;
2751 int ret;
2752 RING_LOCALS;
2753
2754 DRM_DEBUG("\n");
2755
Eric Anholt6c340ea2007-08-25 20:23:09 +10002756 if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002757 cmdbuf, &cmdsz))) {
2758 DRM_ERROR("Packet verification failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return ret;
2760 }
2761
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002762 BEGIN_RING(cmdsz);
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002763 OUT_RING_DRM_BUFFER(cmdbuf->buffer, cmdsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 ADVANCE_RING();
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return 0;
2767}
2768
Dave Airlie84b1fd12007-07-11 15:53:27 +10002769static int radeon_emit_packet3_cliprect(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002770 struct drm_file *file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +10002771 drm_radeon_kcmd_buffer_t *cmdbuf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002772 int orig_nbox)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliec60ce622007-07-11 15:27:12 +10002775 struct drm_clip_rect box;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 unsigned int cmdsz;
2777 int ret;
Dave Airliec60ce622007-07-11 15:27:12 +10002778 struct drm_clip_rect __user *boxes = cmdbuf->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 int i = 0;
2780 RING_LOCALS;
2781
2782 DRM_DEBUG("\n");
2783
Eric Anholt6c340ea2007-08-25 20:23:09 +10002784 if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002785 cmdbuf, &cmdsz))) {
2786 DRM_ERROR("Packet verification failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return ret;
2788 }
2789
2790 if (!orig_nbox)
2791 goto out;
2792
2793 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002794 if (i < cmdbuf->nbox) {
2795 if (DRM_COPY_FROM_USER(&box, &boxes[i], sizeof(box)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002796 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 /* FIXME The second and subsequent times round
2798 * this loop, send a WAIT_UNTIL_3D_IDLE before
2799 * calling emit_clip_rect(). This fixes a
2800 * lockup on fast machines when sending
2801 * several cliprects with a cmdbuf, as when
2802 * waving a 2D window over a 3D
2803 * window. Something in the commands from user
2804 * space seems to hang the card when they're
2805 * sent several times in a row. That would be
2806 * the correct place to fix it but this works
2807 * around it until I can figure that out - Tim
2808 * Smith */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002809 if (i) {
2810 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 RADEON_WAIT_UNTIL_3D_IDLE();
2812 ADVANCE_RING();
2813 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002814 radeon_emit_clip_rect(dev_priv, &box);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002816
2817 BEGIN_RING(cmdsz);
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002818 OUT_RING_DRM_BUFFER(cmdbuf->buffer, cmdsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 ADVANCE_RING();
2820
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002821 } while (++i < cmdbuf->nbox);
2822 if (cmdbuf->nbox == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 cmdbuf->nbox = 0;
2824
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002825 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002826 out:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002827 drm_buffer_advance(cmdbuf->buffer, cmdsz * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return 0;
2829}
2830
Dave Airlie84b1fd12007-07-11 15:53:27 +10002831static int radeon_emit_wait(struct drm_device * dev, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 drm_radeon_private_t *dev_priv = dev->dev_private;
2834 RING_LOCALS;
2835
Márton Németh3e684ea2008-01-24 15:58:57 +10002836 DRM_DEBUG("%x\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 switch (flags) {
2838 case RADEON_WAIT_2D:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002839 BEGIN_RING(2);
2840 RADEON_WAIT_UNTIL_2D_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 ADVANCE_RING();
2842 break;
2843 case RADEON_WAIT_3D:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002844 BEGIN_RING(2);
2845 RADEON_WAIT_UNTIL_3D_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 ADVANCE_RING();
2847 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002848 case RADEON_WAIT_2D | RADEON_WAIT_3D:
2849 BEGIN_RING(2);
2850 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 ADVANCE_RING();
2852 break;
2853 default:
Eric Anholt20caafa2007-08-25 19:22:43 +10002854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856
2857 return 0;
2858}
2859
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002860static int radeon_cp_cmdbuf(struct drm_device *dev, void *data,
2861 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10002864 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002865 struct drm_buf *buf = NULL;
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002866 drm_radeon_cmd_header_t stack_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 int idx;
Eric Anholtc153f452007-09-03 12:06:45 +10002868 drm_radeon_kcmd_buffer_t *cmdbuf = data;
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002869 int orig_nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Eric Anholt6c340ea2007-08-25 20:23:09 +10002871 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002873 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2874 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Eric Anholtc153f452007-09-03 12:06:45 +10002876 if (cmdbuf->bufsz > 64 * 1024 || cmdbuf->bufsz < 0) {
Eric Anholt20caafa2007-08-25 19:22:43 +10002877 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879
2880 /* Allocate an in-kernel area and copy in the cmdbuf. Do this to avoid
2881 * races between checking values and using those values in other code,
2882 * and simply to avoid a lot of function calls to copy in data.
2883 */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002884 if (cmdbuf->bufsz != 0) {
2885 int rv;
2886 void __user *buffer = cmdbuf->buffer;
2887 rv = drm_buffer_alloc(&cmdbuf->buffer, cmdbuf->bufsz);
2888 if (rv)
2889 return rv;
2890 rv = drm_buffer_copy_from_user(cmdbuf->buffer, buffer,
2891 cmdbuf->bufsz);
2892 if (rv)
2893 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895
Eric Anholtc153f452007-09-03 12:06:45 +10002896 orig_nbox = cmdbuf->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002898 if (dev_priv->microcode_version == UCODE_R300) {
Dave Airlie414ed532005-08-16 20:43:16 +10002899 int temp;
Eric Anholtc153f452007-09-03 12:06:45 +10002900 temp = r300_do_cp_cmdbuf(dev, file_priv, cmdbuf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002901
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002902 if (cmdbuf->bufsz != 0)
2903 drm_buffer_free(cmdbuf->buffer);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002904
Dave Airlie414ed532005-08-16 20:43:16 +10002905 return temp;
2906 }
2907
2908 /* microcode_version != r300 */
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002909 while (drm_buffer_unprocessed(cmdbuf->buffer) >= sizeof(stack_header)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002911 drm_radeon_cmd_header_t *header;
2912 header = drm_buffer_read_object(cmdbuf->buffer,
2913 sizeof(stack_header), &stack_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002915 switch (header->header.cmd_type) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002916 case RADEON_CMD_PACKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 DRM_DEBUG("RADEON_CMD_PACKET\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002918 if (radeon_emit_packets
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002919 (dev_priv, file_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 DRM_ERROR("radeon_emit_packets failed\n");
2921 goto err;
2922 }
2923 break;
2924
2925 case RADEON_CMD_SCALARS:
2926 DRM_DEBUG("RADEON_CMD_SCALARS\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002927 if (radeon_emit_scalars(dev_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 DRM_ERROR("radeon_emit_scalars failed\n");
2929 goto err;
2930 }
2931 break;
2932
2933 case RADEON_CMD_VECTORS:
2934 DRM_DEBUG("RADEON_CMD_VECTORS\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002935 if (radeon_emit_vectors(dev_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 DRM_ERROR("radeon_emit_vectors failed\n");
2937 goto err;
2938 }
2939 break;
2940
2941 case RADEON_CMD_DMA_DISCARD:
2942 DRM_DEBUG("RADEON_CMD_DMA_DISCARD\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002943 idx = header->dma.buf_idx;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002944 if (idx < 0 || idx >= dma->buf_count) {
2945 DRM_ERROR("buffer index %d (of %d max)\n",
2946 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 goto err;
2948 }
2949
2950 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10002951 if (buf->file_priv != file_priv || buf->pending) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002952 DRM_ERROR("bad buffer %p %p %d\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002953 buf->file_priv, file_priv,
2954 buf->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 goto err;
2956 }
2957
Dave Airlie7c1c2872008-11-28 14:22:24 +10002958 radeon_cp_discard_buffer(dev, file_priv->master, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 break;
2960
2961 case RADEON_CMD_PACKET3:
2962 DRM_DEBUG("RADEON_CMD_PACKET3\n");
Eric Anholtc153f452007-09-03 12:06:45 +10002963 if (radeon_emit_packet3(dev, file_priv, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 DRM_ERROR("radeon_emit_packet3 failed\n");
2965 goto err;
2966 }
2967 break;
2968
2969 case RADEON_CMD_PACKET3_CLIP:
2970 DRM_DEBUG("RADEON_CMD_PACKET3_CLIP\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002971 if (radeon_emit_packet3_cliprect
Eric Anholtc153f452007-09-03 12:06:45 +10002972 (dev, file_priv, cmdbuf, orig_nbox)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 DRM_ERROR("radeon_emit_packet3_clip failed\n");
2974 goto err;
2975 }
2976 break;
2977
2978 case RADEON_CMD_SCALARS2:
2979 DRM_DEBUG("RADEON_CMD_SCALARS2\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002980 if (radeon_emit_scalars2(dev_priv, *header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 DRM_ERROR("radeon_emit_scalars2 failed\n");
2982 goto err;
2983 }
2984 break;
2985
2986 case RADEON_CMD_WAIT:
2987 DRM_DEBUG("RADEON_CMD_WAIT\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002988 if (radeon_emit_wait(dev, header->wait.flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 DRM_ERROR("radeon_emit_wait failed\n");
2990 goto err;
2991 }
2992 break;
Dave Airlied6fece02006-06-24 17:04:07 +10002993 case RADEON_CMD_VECLINEAR:
2994 DRM_DEBUG("RADEON_CMD_VECLINEAR\n");
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02002995 if (radeon_emit_veclinear(dev_priv, *header, cmdbuf)) {
Dave Airlied6fece02006-06-24 17:04:07 +10002996 DRM_ERROR("radeon_emit_veclinear failed\n");
2997 goto err;
2998 }
2999 break;
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 default:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02003002 DRM_ERROR("bad cmd_type %d at byte %d\n",
3003 header->header.cmd_type,
3004 cmdbuf->buffer->iterator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 goto err;
3006 }
3007 }
3008
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02003009 if (cmdbuf->bufsz != 0)
3010 drm_buffer_free(cmdbuf->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 DRM_DEBUG("DONE\n");
3013 COMMIT_RING();
3014 return 0;
3015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003016 err:
Pauli Nieminenb4fe9452010-02-01 19:11:16 +02003017 if (cmdbuf->bufsz != 0)
3018 drm_buffer_free(cmdbuf->buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +10003019 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Eric Anholtc153f452007-09-03 12:06:45 +10003022static int radeon_cp_getparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10003025 drm_radeon_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 int value;
3027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003028 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Eric Anholtc153f452007-09-03 12:06:45 +10003030 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 case RADEON_PARAM_GART_BUFFER_OFFSET:
3032 value = dev_priv->gart_buffers_offset;
3033 break;
3034 case RADEON_PARAM_LAST_FRAME:
3035 dev_priv->stats.last_frame_reads++;
David Millerb07fa022009-02-12 02:15:37 -08003036 value = GET_SCRATCH(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 break;
3038 case RADEON_PARAM_LAST_DISPATCH:
David Millerb07fa022009-02-12 02:15:37 -08003039 value = GET_SCRATCH(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 break;
3041 case RADEON_PARAM_LAST_CLEAR:
3042 dev_priv->stats.last_clear_reads++;
David Millerb07fa022009-02-12 02:15:37 -08003043 value = GET_SCRATCH(dev_priv, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 break;
3045 case RADEON_PARAM_IRQ_NR:
Alex Deucherb15591f2009-09-17 14:25:12 -04003046 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
3047 value = 0;
3048 else
3049 value = drm_dev_to_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 break;
3051 case RADEON_PARAM_GART_BASE:
3052 value = dev_priv->gart_vm_start;
3053 break;
3054 case RADEON_PARAM_REGISTER_HANDLE:
Dave Airlied985c102006-01-02 21:32:48 +11003055 value = dev_priv->mmio->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 break;
3057 case RADEON_PARAM_STATUS_HANDLE:
3058 value = dev_priv->ring_rptr_offset;
3059 break;
3060#if BITS_PER_LONG == 32
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003061 /*
3062 * This ioctl() doesn't work on 64-bit platforms because hw_lock is a
3063 * pointer which can't fit into an int-sized variable. According to
Jan Engelhardt96de0e22007-10-19 23:21:04 +02003064 * Michel Dänzer, the ioctl() is only used on embedded platforms, so
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003065 * not supporting it shouldn't be a problem. If the same functionality
3066 * is needed on 64-bit platforms, a new ioctl() would have to be added,
3067 * so backwards-compatibility for the embedded platforms can be
3068 * maintained. --davidm 4-Feb-2004.
3069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 case RADEON_PARAM_SAREA_HANDLE:
3071 /* The lock is the first dword in the sarea. */
Dave Airlie7c1c2872008-11-28 14:22:24 +10003072 /* no users of this parameter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 break;
3074#endif
3075 case RADEON_PARAM_GART_TEX_HANDLE:
3076 value = dev_priv->gart_textures_offset;
3077 break;
Michel Dänzer8624ecb2006-08-07 20:33:57 +10003078 case RADEON_PARAM_SCRATCH_OFFSET:
3079 if (!dev_priv->writeback_works)
Eric Anholt20caafa2007-08-25 19:22:43 +10003080 return -EINVAL;
Alex Deucherc05ce082009-02-24 16:22:29 -05003081 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
3082 value = R600_SCRATCH_REG_OFFSET;
3083 else
3084 value = RADEON_SCRATCH_REG_OFFSET;
Michel Dänzer8624ecb2006-08-07 20:33:57 +10003085 break;
Dave Airlied985c102006-01-02 21:32:48 +11003086 case RADEON_PARAM_CARD_TYPE:
Dave Airlie54a56ac2006-09-22 04:25:09 +10003087 if (dev_priv->flags & RADEON_IS_PCIE)
Dave Airlied985c102006-01-02 21:32:48 +11003088 value = RADEON_CARD_PCIE;
Dave Airlie54a56ac2006-09-22 04:25:09 +10003089 else if (dev_priv->flags & RADEON_IS_AGP)
Dave Airlied985c102006-01-02 21:32:48 +11003090 value = RADEON_CARD_AGP;
3091 else
3092 value = RADEON_CARD_PCI;
3093 break;
Dave Airlieddbee332007-07-11 12:16:01 +10003094 case RADEON_PARAM_VBLANK_CRTC:
3095 value = radeon_vblank_crtc_get(dev);
3096 break;
Dave Airlie3d5e2c12008-02-07 15:01:05 +10003097 case RADEON_PARAM_FB_LOCATION:
3098 value = radeon_read_fb_location(dev_priv);
3099 break;
Alex Deucher5b92c402008-05-28 11:57:40 +10003100 case RADEON_PARAM_NUM_GB_PIPES:
3101 value = dev_priv->num_gb_pipes;
3102 break;
Alex Deucherf779b3e2009-08-19 19:11:39 -04003103 case RADEON_PARAM_NUM_Z_PIPES:
3104 value = dev_priv->num_z_pipes;
3105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 default:
Eric Anholtc153f452007-09-03 12:06:45 +10003107 DRM_DEBUG("Invalid parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10003108 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110
Eric Anholtc153f452007-09-03 12:06:45 +10003111 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003112 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10003113 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return 0;
3117}
3118
Eric Anholtc153f452007-09-03 12:06:45 +10003119static int radeon_cp_setparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10003122 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10003123 drm_radeon_setparam_t *sp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct drm_radeon_driver_file_fields *radeon_priv;
3125
Eric Anholtc153f452007-09-03 12:06:45 +10003126 switch (sp->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 case RADEON_SETPARAM_FB_LOCATION:
Eric Anholt6c340ea2007-08-25 20:23:09 +10003128 radeon_priv = file_priv->driver_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10003129 radeon_priv->radeon_fb_delta = dev_priv->fb_location -
3130 sp->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 break;
3132 case RADEON_SETPARAM_SWITCH_TILING:
Eric Anholtc153f452007-09-03 12:06:45 +10003133 if (sp->value == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003134 DRM_DEBUG("color tiling disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 dev_priv->front_pitch_offset &= ~RADEON_DST_TILE_MACRO;
3136 dev_priv->back_pitch_offset &= ~RADEON_DST_TILE_MACRO;
Dave Airlie7c1c2872008-11-28 14:22:24 +10003137 if (master_priv->sarea_priv)
3138 master_priv->sarea_priv->tiling_enabled = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10003139 } else if (sp->value == 1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003140 DRM_DEBUG("color tiling enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 dev_priv->front_pitch_offset |= RADEON_DST_TILE_MACRO;
3142 dev_priv->back_pitch_offset |= RADEON_DST_TILE_MACRO;
Dave Airlie7c1c2872008-11-28 14:22:24 +10003143 if (master_priv->sarea_priv)
3144 master_priv->sarea_priv->tiling_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003146 break;
Dave Airlieea98a922005-09-11 20:28:11 +10003147 case RADEON_SETPARAM_PCIGART_LOCATION:
Eric Anholtc153f452007-09-03 12:06:45 +10003148 dev_priv->pcigart_offset = sp->value;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003149 dev_priv->pcigart_offset_set = 1;
Dave Airlieea98a922005-09-11 20:28:11 +10003150 break;
Dave Airlied5ea7022006-03-19 19:37:55 +11003151 case RADEON_SETPARAM_NEW_MEMMAP:
Eric Anholtc153f452007-09-03 12:06:45 +10003152 dev_priv->new_memmap = sp->value;
Dave Airlied5ea7022006-03-19 19:37:55 +11003153 break;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003154 case RADEON_SETPARAM_PCIGART_TABLE_SIZE:
Eric Anholtc153f452007-09-03 12:06:45 +10003155 dev_priv->gart_info.table_size = sp->value;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003156 if (dev_priv->gart_info.table_size < RADEON_PCIGART_TABLE_SIZE)
3157 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
3158 break;
Dave Airlieddbee332007-07-11 12:16:01 +10003159 case RADEON_SETPARAM_VBLANK_CRTC:
Eric Anholtc153f452007-09-03 12:06:45 +10003160 return radeon_vblank_crtc_set(dev, sp->value);
Dave Airlieddbee332007-07-11 12:16:01 +10003161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 default:
Eric Anholtc153f452007-09-03 12:06:45 +10003163 DRM_DEBUG("Invalid parameter %d\n", sp->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10003164 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
3166
3167 return 0;
3168}
3169
3170/* When a client dies:
3171 * - Check for and clean up flipped page state
3172 * - Free any alloced GART memory.
Dave Airlied985c102006-01-02 21:32:48 +11003173 * - Free any alloced radeon surfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 *
3175 * DRM infrastructure takes care of reclaiming dma buffers.
3176 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10003177void radeon_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003179 if (dev->dev_private) {
3180 drm_radeon_private_t *dev_priv = dev->dev_private;
Michel Dänzer453ff942007-05-08 15:21:14 +10003181 dev_priv->page_flipping = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10003182 radeon_mem_release(file_priv, dev_priv->gart_heap);
3183 radeon_mem_release(file_priv, dev_priv->fb_heap);
3184 radeon_surfaces_release(file_priv, dev_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
Dave Airlie84b1fd12007-07-11 15:53:27 +10003188void radeon_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
David Miller6abf6bb2009-02-14 01:51:07 -08003190 radeon_surfaces_release(PCIGART_FILE_PRIV, dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 radeon_do_release(dev);
3192}
3193
Eric Anholt6c340ea2007-08-25 20:23:09 +10003194int radeon_driver_open(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
3196 drm_radeon_private_t *dev_priv = dev->dev_private;
3197 struct drm_radeon_driver_file_fields *radeon_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003198
Dave Airlied985c102006-01-02 21:32:48 +11003199 DRM_DEBUG("\n");
Eric Anholt9a298b22009-03-24 12:23:04 -07003200 radeon_priv = kmalloc(sizeof(*radeon_priv), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (!radeon_priv)
3203 return -ENOMEM;
3204
Eric Anholt6c340ea2007-08-25 20:23:09 +10003205 file_priv->driver_priv = radeon_priv;
Dave Airlied985c102006-01-02 21:32:48 +11003206
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003207 if (dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 radeon_priv->radeon_fb_delta = dev_priv->fb_location;
3209 else
3210 radeon_priv->radeon_fb_delta = 0;
3211 return 0;
3212}
3213
Eric Anholt6c340ea2007-08-25 20:23:09 +10003214void radeon_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003216 struct drm_radeon_driver_file_fields *radeon_priv =
Eric Anholt6c340ea2007-08-25 20:23:09 +10003217 file_priv->driver_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003218
Eric Anholt9a298b22009-03-24 12:23:04 -07003219 kfree(radeon_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Eric Anholtc153f452007-09-03 12:06:45 +10003222struct drm_ioctl_desc radeon_ioctls[] = {
3223 DRM_IOCTL_DEF(DRM_RADEON_CP_INIT, radeon_cp_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3224 DRM_IOCTL_DEF(DRM_RADEON_CP_START, radeon_cp_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3225 DRM_IOCTL_DEF(DRM_RADEON_CP_STOP, radeon_cp_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3226 DRM_IOCTL_DEF(DRM_RADEON_CP_RESET, radeon_cp_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3227 DRM_IOCTL_DEF(DRM_RADEON_CP_IDLE, radeon_cp_idle, DRM_AUTH),
3228 DRM_IOCTL_DEF(DRM_RADEON_CP_RESUME, radeon_cp_resume, DRM_AUTH),
3229 DRM_IOCTL_DEF(DRM_RADEON_RESET, radeon_engine_reset, DRM_AUTH),
3230 DRM_IOCTL_DEF(DRM_RADEON_FULLSCREEN, radeon_fullscreen, DRM_AUTH),
3231 DRM_IOCTL_DEF(DRM_RADEON_SWAP, radeon_cp_swap, DRM_AUTH),
3232 DRM_IOCTL_DEF(DRM_RADEON_CLEAR, radeon_cp_clear, DRM_AUTH),
3233 DRM_IOCTL_DEF(DRM_RADEON_VERTEX, radeon_cp_vertex, DRM_AUTH),
3234 DRM_IOCTL_DEF(DRM_RADEON_INDICES, radeon_cp_indices, DRM_AUTH),
3235 DRM_IOCTL_DEF(DRM_RADEON_TEXTURE, radeon_cp_texture, DRM_AUTH),
3236 DRM_IOCTL_DEF(DRM_RADEON_STIPPLE, radeon_cp_stipple, DRM_AUTH),
3237 DRM_IOCTL_DEF(DRM_RADEON_INDIRECT, radeon_cp_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3238 DRM_IOCTL_DEF(DRM_RADEON_VERTEX2, radeon_cp_vertex2, DRM_AUTH),
3239 DRM_IOCTL_DEF(DRM_RADEON_CMDBUF, radeon_cp_cmdbuf, DRM_AUTH),
3240 DRM_IOCTL_DEF(DRM_RADEON_GETPARAM, radeon_cp_getparam, DRM_AUTH),
3241 DRM_IOCTL_DEF(DRM_RADEON_FLIP, radeon_cp_flip, DRM_AUTH),
3242 DRM_IOCTL_DEF(DRM_RADEON_ALLOC, radeon_mem_alloc, DRM_AUTH),
3243 DRM_IOCTL_DEF(DRM_RADEON_FREE, radeon_mem_free, DRM_AUTH),
3244 DRM_IOCTL_DEF(DRM_RADEON_INIT_HEAP, radeon_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3245 DRM_IOCTL_DEF(DRM_RADEON_IRQ_EMIT, radeon_irq_emit, DRM_AUTH),
3246 DRM_IOCTL_DEF(DRM_RADEON_IRQ_WAIT, radeon_irq_wait, DRM_AUTH),
3247 DRM_IOCTL_DEF(DRM_RADEON_SETPARAM, radeon_cp_setparam, DRM_AUTH),
3248 DRM_IOCTL_DEF(DRM_RADEON_SURF_ALLOC, radeon_surface_alloc, DRM_AUTH),
Jerome Glisse3ce0a232009-09-08 10:10:24 +10003249 DRM_IOCTL_DEF(DRM_RADEON_SURF_FREE, radeon_surface_free, DRM_AUTH),
3250 DRM_IOCTL_DEF(DRM_RADEON_CS, r600_cs_legacy_ioctl, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251};
3252
3253int radeon_max_ioctl = DRM_ARRAY_SIZE(radeon_ioctls);