blob: 69c9f2febf43a9b42bb05dada49ac9df8e572c46 [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"
32#include "drm_sarea.h"
33#include "radeon_drm.h"
34#include "radeon_drv.h"
35
36/* ================================================================
37 * Helper functions for client state checking and fixup
38 */
39
Dave Airlieb5e89ed2005-09-25 14:28:13 +100040static __inline__ int radeon_check_and_fixup_offset(drm_radeon_private_t *
41 dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +100042 struct drm_file * file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +100043 u32 *offset)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100044{
Michel Daenzer214ff132006-09-22 04:12:11 +100045 u64 off = *offset;
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110046 u32 fb_end = dev_priv->fb_location + dev_priv->fb_size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct drm_radeon_driver_file_fields *radeon_priv;
48
Dave Airlied5ea7022006-03-19 19:37:55 +110049 /* Hrm ... the story of the offset ... So this function converts
50 * the various ideas of what userland clients might have for an
51 * offset in the card address space into an offset into the card
52 * address space :) So with a sane client, it should just keep
53 * the value intact and just do some boundary checking. However,
54 * not all clients are sane. Some older clients pass us 0 based
55 * offsets relative to the start of the framebuffer and some may
56 * assume the AGP aperture it appended to the framebuffer, so we
57 * try to detect those cases and fix them up.
58 *
59 * Note: It might be a good idea here to make sure the offset lands
60 * in some "allowed" area to protect things like the PCIE GART...
61 */
62
63 /* First, the best case, the offset already lands in either the
64 * framebuffer or the GART mapped space
65 */
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110066 if (radeon_check_offset(dev_priv, off))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return 0;
68
Dave Airlied5ea7022006-03-19 19:37:55 +110069 /* Ok, that didn't happen... now check if we have a zero based
70 * offset that fits in the framebuffer + gart space, apply the
71 * magic offset we get from SETPARAM or calculated from fb_location
72 */
73 if (off < (dev_priv->fb_size + dev_priv->gart_size)) {
Eric Anholt6c340ea2007-08-25 20:23:09 +100074 radeon_priv = file_priv->driver_priv;
Dave Airlied5ea7022006-03-19 19:37:55 +110075 off += radeon_priv->radeon_fb_delta;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Dave Airlied5ea7022006-03-19 19:37:55 +110078 /* Finally, assume we aimed at a GART offset if beyond the fb */
Michel Daenzer214ff132006-09-22 04:12:11 +100079 if (off > fb_end)
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110080 off = off - fb_end - 1 + dev_priv->gart_vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Dave Airlied5ea7022006-03-19 19:37:55 +110082 /* Now recheck and fail if out of bounds */
=?utf-8?q?Michel_D=C3=A4nzer?=1d6bb8e2006-12-15 18:54:35 +110083 if (radeon_check_offset(dev_priv, off)) {
Michel Daenzer214ff132006-09-22 04:12:11 +100084 DRM_DEBUG("offset fixed up to 0x%x\n", (unsigned int)off);
Dave Airlied5ea7022006-03-19 19:37:55 +110085 *offset = off;
86 return 0;
87 }
Eric Anholt20caafa2007-08-25 19:22:43 +100088 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091static __inline__ int radeon_check_and_fixup_packets(drm_radeon_private_t *
92 dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +100093 struct drm_file *file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +100094 int id, u32 *data)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095{
96 switch (id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 case RADEON_EMIT_PP_MISC:
Eric Anholt6c340ea2007-08-25 20:23:09 +100099 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlied985c102006-01-02 21:32:48 +1100100 &data[(RADEON_RB3D_DEPTHOFFSET - RADEON_PP_MISC) / 4])) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000101 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000102 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 break;
105
106 case RADEON_EMIT_PP_CNTL:
Eric Anholt6c340ea2007-08-25 20:23:09 +1000107 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlied985c102006-01-02 21:32:48 +1100108 &data[(RADEON_RB3D_COLOROFFSET - RADEON_PP_CNTL) / 4])) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 DRM_ERROR("Invalid colour buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000110 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 break;
113
114 case R200_EMIT_PP_TXOFFSET_0:
115 case R200_EMIT_PP_TXOFFSET_1:
116 case R200_EMIT_PP_TXOFFSET_2:
117 case R200_EMIT_PP_TXOFFSET_3:
118 case R200_EMIT_PP_TXOFFSET_4:
119 case R200_EMIT_PP_TXOFFSET_5:
Eric Anholt6c340ea2007-08-25 20:23:09 +1000120 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000121 &data[0])) {
122 DRM_ERROR("Invalid R200 texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000123 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 break;
126
127 case RADEON_EMIT_PP_TXFILTER_0:
128 case RADEON_EMIT_PP_TXFILTER_1:
129 case RADEON_EMIT_PP_TXFILTER_2:
Eric Anholt6c340ea2007-08-25 20:23:09 +1000130 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlied985c102006-01-02 21:32:48 +1100131 &data[(RADEON_PP_TXOFFSET_0 - RADEON_PP_TXFILTER_0) / 4])) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 DRM_ERROR("Invalid R100 texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000133 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 break;
136
137 case R200_EMIT_PP_CUBIC_OFFSETS_0:
138 case R200_EMIT_PP_CUBIC_OFFSETS_1:
139 case R200_EMIT_PP_CUBIC_OFFSETS_2:
140 case R200_EMIT_PP_CUBIC_OFFSETS_3:
141 case R200_EMIT_PP_CUBIC_OFFSETS_4:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142 case R200_EMIT_PP_CUBIC_OFFSETS_5:{
143 int i;
144 for (i = 0; i < 5; i++) {
Dave Airlied985c102006-01-02 21:32:48 +1100145 if (radeon_check_and_fixup_offset(dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000146 file_priv,
Dave Airlied985c102006-01-02 21:32:48 +1100147 &data[i])) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 DRM_ERROR
149 ("Invalid R200 cubic texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000150 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 case RADEON_EMIT_PP_CUBIC_OFFSETS_T0:
157 case RADEON_EMIT_PP_CUBIC_OFFSETS_T1:
158 case RADEON_EMIT_PP_CUBIC_OFFSETS_T2:{
159 int i;
160 for (i = 0; i < 5; i++) {
161 if (radeon_check_and_fixup_offset(dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000162 file_priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 &data[i])) {
164 DRM_ERROR
165 ("Invalid R100 cubic texture offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 }
169 }
170 break;
171
Roland Scheidegger18f29052006-08-30 23:17:55 +0100172 case R200_EMIT_VAP_CTL:{
173 RING_LOCALS;
174 BEGIN_RING(2);
175 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
176 ADVANCE_RING();
177 }
178 break;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 case RADEON_EMIT_RB3D_COLORPITCH:
181 case RADEON_EMIT_RE_LINE_PATTERN:
182 case RADEON_EMIT_SE_LINE_WIDTH:
183 case RADEON_EMIT_PP_LUM_MATRIX:
184 case RADEON_EMIT_PP_ROT_MATRIX_0:
185 case RADEON_EMIT_RB3D_STENCILREFMASK:
186 case RADEON_EMIT_SE_VPORT_XSCALE:
187 case RADEON_EMIT_SE_CNTL:
188 case RADEON_EMIT_SE_CNTL_STATUS:
189 case RADEON_EMIT_RE_MISC:
190 case RADEON_EMIT_PP_BORDER_COLOR_0:
191 case RADEON_EMIT_PP_BORDER_COLOR_1:
192 case RADEON_EMIT_PP_BORDER_COLOR_2:
193 case RADEON_EMIT_SE_ZBIAS_FACTOR:
194 case RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT:
195 case RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED:
196 case R200_EMIT_PP_TXCBLEND_0:
197 case R200_EMIT_PP_TXCBLEND_1:
198 case R200_EMIT_PP_TXCBLEND_2:
199 case R200_EMIT_PP_TXCBLEND_3:
200 case R200_EMIT_PP_TXCBLEND_4:
201 case R200_EMIT_PP_TXCBLEND_5:
202 case R200_EMIT_PP_TXCBLEND_6:
203 case R200_EMIT_PP_TXCBLEND_7:
204 case R200_EMIT_TCL_LIGHT_MODEL_CTL_0:
205 case R200_EMIT_TFACTOR_0:
206 case R200_EMIT_VTX_FMT_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 case R200_EMIT_MATRIX_SELECT_0:
208 case R200_EMIT_TEX_PROC_CTL_2:
209 case R200_EMIT_TCL_UCP_VERT_BLEND_CTL:
210 case R200_EMIT_PP_TXFILTER_0:
211 case R200_EMIT_PP_TXFILTER_1:
212 case R200_EMIT_PP_TXFILTER_2:
213 case R200_EMIT_PP_TXFILTER_3:
214 case R200_EMIT_PP_TXFILTER_4:
215 case R200_EMIT_PP_TXFILTER_5:
216 case R200_EMIT_VTE_CNTL:
217 case R200_EMIT_OUTPUT_VTX_COMP_SEL:
218 case R200_EMIT_PP_TAM_DEBUG3:
219 case R200_EMIT_PP_CNTL_X:
220 case R200_EMIT_RB3D_DEPTHXY_OFFSET:
221 case R200_EMIT_RE_AUX_SCISSOR_CNTL:
222 case R200_EMIT_RE_SCISSOR_TL_0:
223 case R200_EMIT_RE_SCISSOR_TL_1:
224 case R200_EMIT_RE_SCISSOR_TL_2:
225 case R200_EMIT_SE_VAP_CNTL_STATUS:
226 case R200_EMIT_SE_VTX_STATE_CNTL:
227 case R200_EMIT_RE_POINTSIZE:
228 case R200_EMIT_TCL_INPUT_VTX_VECTOR_ADDR_0:
229 case R200_EMIT_PP_CUBIC_FACES_0:
230 case R200_EMIT_PP_CUBIC_FACES_1:
231 case R200_EMIT_PP_CUBIC_FACES_2:
232 case R200_EMIT_PP_CUBIC_FACES_3:
233 case R200_EMIT_PP_CUBIC_FACES_4:
234 case R200_EMIT_PP_CUBIC_FACES_5:
235 case RADEON_EMIT_PP_TEX_SIZE_0:
236 case RADEON_EMIT_PP_TEX_SIZE_1:
237 case RADEON_EMIT_PP_TEX_SIZE_2:
238 case R200_EMIT_RB3D_BLENDCOLOR:
239 case R200_EMIT_TCL_POINT_SPRITE_CNTL:
240 case RADEON_EMIT_PP_CUBIC_FACES_0:
241 case RADEON_EMIT_PP_CUBIC_FACES_1:
242 case RADEON_EMIT_PP_CUBIC_FACES_2:
243 case R200_EMIT_PP_TRI_PERF_CNTL:
Dave Airlie9d176012005-09-11 19:55:53 +1000244 case R200_EMIT_PP_AFS_0:
245 case R200_EMIT_PP_AFS_1:
246 case R200_EMIT_ATF_TFACTOR:
247 case R200_EMIT_PP_TXCTLALL_0:
248 case R200_EMIT_PP_TXCTLALL_1:
249 case R200_EMIT_PP_TXCTLALL_2:
250 case R200_EMIT_PP_TXCTLALL_3:
251 case R200_EMIT_PP_TXCTLALL_4:
252 case R200_EMIT_PP_TXCTLALL_5:
Dave Airlied6fece02006-06-24 17:04:07 +1000253 case R200_EMIT_VAP_PVS_CNTL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* These packets don't contain memory offsets */
255 break;
256
257 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 DRM_ERROR("Unknown state packet ID %d\n", id);
Eric Anholt20caafa2007-08-25 19:22:43 +1000259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 return 0;
263}
264
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000265static __inline__ int radeon_check_and_fixup_packet3(drm_radeon_private_t *
266 dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000267 struct drm_file *file_priv,
Dave Airlied985c102006-01-02 21:32:48 +1100268 drm_radeon_kcmd_buffer_t *
269 cmdbuf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 unsigned int *cmdsz)
271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 u32 *cmd = (u32 *) cmdbuf->buf;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000273 u32 offset, narrays;
274 int count, i, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 *cmdsz = 2 + ((cmd[0] & RADEON_CP_PACKET_COUNT_MASK) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 if ((cmd[0] & 0xc0000000) != RADEON_CP_PACKET3) {
279 DRM_ERROR("Not a type 3 packet\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000280 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 if (4 * *cmdsz > cmdbuf->bufsz) {
284 DRM_ERROR("Packet size larger than size of data provided\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000288 switch(cmd[0] & 0xff00) {
289 /* XXX Are there old drivers needing other packets? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000291 case RADEON_3D_DRAW_IMMD:
292 case RADEON_3D_DRAW_VBUF:
293 case RADEON_3D_DRAW_INDX:
294 case RADEON_WAIT_FOR_IDLE:
295 case RADEON_CP_NOP:
296 case RADEON_3D_CLEAR_ZMASK:
297/* case RADEON_CP_NEXT_CHAR:
298 case RADEON_CP_PLY_NEXTSCAN:
299 case RADEON_CP_SET_SCISSORS: */ /* probably safe but will never need them? */
300 /* these packets are safe */
301 break;
302
303 case RADEON_CP_3D_DRAW_IMMD_2:
304 case RADEON_CP_3D_DRAW_VBUF_2:
305 case RADEON_CP_3D_DRAW_INDX_2:
306 case RADEON_3D_CLEAR_HIZ:
307 /* safe but r200 only */
308 if (dev_priv->microcode_version != UCODE_R200) {
309 DRM_ERROR("Invalid 3d packet for r100-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000310 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000311 }
312 break;
313
314 case RADEON_3D_LOAD_VBPNTR:
315 count = (cmd[0] >> 16) & 0x3fff;
316
317 if (count > 18) { /* 12 arrays max */
318 DRM_ERROR("Too large payload in 3D_LOAD_VBPNTR (count=%d)\n",
319 count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000320 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000321 }
322
323 /* carefully check packet contents */
324 narrays = cmd[1] & ~0xc000;
325 k = 0;
326 i = 2;
327 while ((k < narrays) && (i < (count + 2))) {
328 i++; /* skip attribute field */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000329 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
330 &cmd[i])) {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000331 DRM_ERROR
332 ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
333 k, i);
Eric Anholt20caafa2007-08-25 19:22:43 +1000334 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000335 }
336 k++;
337 i++;
338 if (k == narrays)
339 break;
340 /* have one more to process, they come in pairs */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000341 if (radeon_check_and_fixup_offset(dev_priv,
342 file_priv, &cmd[i]))
343 {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000344 DRM_ERROR
345 ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
346 k, i);
Eric Anholt20caafa2007-08-25 19:22:43 +1000347 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000348 }
349 k++;
350 i++;
351 }
352 /* do the counts match what we expect ? */
353 if ((k != narrays) || (i != (count + 2))) {
354 DRM_ERROR
355 ("Malformed 3D_LOAD_VBPNTR packet (k=%d i=%d narrays=%d count+1=%d).\n",
356 k, i, narrays, count + 1);
Eric Anholt20caafa2007-08-25 19:22:43 +1000357 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000358 }
359 break;
360
361 case RADEON_3D_RNDR_GEN_INDX_PRIM:
362 if (dev_priv->microcode_version != UCODE_R100) {
363 DRM_ERROR("Invalid 3d packet for r200-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000364 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000365 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000366 if (radeon_check_and_fixup_offset(dev_priv, file_priv, &cmd[1])) {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000367 DRM_ERROR("Invalid rndr_gen_indx offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000368 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000369 }
370 break;
371
372 case RADEON_CP_INDX_BUFFER:
373 if (dev_priv->microcode_version != UCODE_R200) {
374 DRM_ERROR("Invalid 3d packet for r100-class chip\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000375 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000376 }
377 if ((cmd[1] & 0x8000ffff) != 0x80000810) {
378 DRM_ERROR("Invalid indx_buffer reg address %08X\n", cmd[1]);
Eric Anholt20caafa2007-08-25 19:22:43 +1000379 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000380 }
Eric Anholt6c340ea2007-08-25 20:23:09 +1000381 if (radeon_check_and_fixup_offset(dev_priv, file_priv, &cmd[2])) {
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000382 DRM_ERROR("Invalid indx_buffer offset is %08X\n", cmd[2]);
Eric Anholt20caafa2007-08-25 19:22:43 +1000383 return -EINVAL;
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000384 }
385 break;
386
387 case RADEON_CNTL_HOSTDATA_BLT:
388 case RADEON_CNTL_PAINT_MULTI:
389 case RADEON_CNTL_BITBLT_MULTI:
390 /* MSB of opcode: next DWORD GUI_CNTL */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000391 if (cmd[1] & (RADEON_GMC_SRC_PITCH_OFFSET_CNTL
392 | RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 offset = cmd[2] << 10;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394 if (radeon_check_and_fixup_offset
Eric Anholt6c340ea2007-08-25 20:23:09 +1000395 (dev_priv, file_priv, &offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 DRM_ERROR("Invalid first packet offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000399 cmd[2] = (cmd[2] & 0xffc00000) | offset >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402 if ((cmd[1] & RADEON_GMC_SRC_PITCH_OFFSET_CNTL) &&
403 (cmd[1] & RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 offset = cmd[3] << 10;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405 if (radeon_check_and_fixup_offset
Eric Anholt6c340ea2007-08-25 20:23:09 +1000406 (dev_priv, file_priv, &offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 DRM_ERROR("Invalid second packet offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000408 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 cmd[3] = (cmd[3] & 0xffc00000) | offset >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Roland Scheideggera1aa28972006-10-24 21:45:00 +1000412 break;
413
414 default:
415 DRM_ERROR("Invalid packet type %x\n", cmd[0] & 0xff00);
Eric Anholt20caafa2007-08-25 19:22:43 +1000416 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 return 0;
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/* ================================================================
423 * CP hardware state programming functions
424 */
425
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426static __inline__ void radeon_emit_clip_rect(drm_radeon_private_t * dev_priv,
Dave Airliec60ce622007-07-11 15:27:12 +1000427 struct drm_clip_rect * box)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 RING_LOCALS;
430
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 DRM_DEBUG(" box: x1=%d y1=%d x2=%d y2=%d\n",
432 box->x1, box->y1, box->x2, box->y2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 BEGIN_RING(4);
435 OUT_RING(CP_PACKET0(RADEON_RE_TOP_LEFT, 0));
436 OUT_RING((box->y1 << 16) | box->x1);
437 OUT_RING(CP_PACKET0(RADEON_RE_WIDTH_HEIGHT, 0));
438 OUT_RING(((box->y2 - 1) << 16) | (box->x2 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ADVANCE_RING();
440}
441
442/* Emit 1.1 state
443 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444static int radeon_emit_state(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000445 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 drm_radeon_context_regs_t * ctx,
447 drm_radeon_texture_regs_t * tex,
448 unsigned int dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 DRM_DEBUG("dirty=0x%08x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000453 if (dirty & RADEON_UPLOAD_CONTEXT) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000454 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 &ctx->rb3d_depthoffset)) {
456 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Eric Anholt6c340ea2007-08-25 20:23:09 +1000460 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 &ctx->rb3d_coloroffset)) {
462 DRM_ERROR("Invalid depth buffer offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 BEGIN_RING(14);
467 OUT_RING(CP_PACKET0(RADEON_PP_MISC, 6));
468 OUT_RING(ctx->pp_misc);
469 OUT_RING(ctx->pp_fog_color);
470 OUT_RING(ctx->re_solid_color);
471 OUT_RING(ctx->rb3d_blendcntl);
472 OUT_RING(ctx->rb3d_depthoffset);
473 OUT_RING(ctx->rb3d_depthpitch);
474 OUT_RING(ctx->rb3d_zstencilcntl);
475 OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 2));
476 OUT_RING(ctx->pp_cntl);
477 OUT_RING(ctx->rb3d_cntl);
478 OUT_RING(ctx->rb3d_coloroffset);
479 OUT_RING(CP_PACKET0(RADEON_RB3D_COLORPITCH, 0));
480 OUT_RING(ctx->rb3d_colorpitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ADVANCE_RING();
482 }
483
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 if (dirty & RADEON_UPLOAD_VERTFMT) {
485 BEGIN_RING(2);
486 OUT_RING(CP_PACKET0(RADEON_SE_COORD_FMT, 0));
487 OUT_RING(ctx->se_coord_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 ADVANCE_RING();
489 }
490
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000491 if (dirty & RADEON_UPLOAD_LINE) {
492 BEGIN_RING(5);
493 OUT_RING(CP_PACKET0(RADEON_RE_LINE_PATTERN, 1));
494 OUT_RING(ctx->re_line_pattern);
495 OUT_RING(ctx->re_line_state);
496 OUT_RING(CP_PACKET0(RADEON_SE_LINE_WIDTH, 0));
497 OUT_RING(ctx->se_line_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 ADVANCE_RING();
499 }
500
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501 if (dirty & RADEON_UPLOAD_BUMPMAP) {
502 BEGIN_RING(5);
503 OUT_RING(CP_PACKET0(RADEON_PP_LUM_MATRIX, 0));
504 OUT_RING(ctx->pp_lum_matrix);
505 OUT_RING(CP_PACKET0(RADEON_PP_ROT_MATRIX_0, 1));
506 OUT_RING(ctx->pp_rot_matrix_0);
507 OUT_RING(ctx->pp_rot_matrix_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ADVANCE_RING();
509 }
510
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000511 if (dirty & RADEON_UPLOAD_MASKS) {
512 BEGIN_RING(4);
513 OUT_RING(CP_PACKET0(RADEON_RB3D_STENCILREFMASK, 2));
514 OUT_RING(ctx->rb3d_stencilrefmask);
515 OUT_RING(ctx->rb3d_ropcntl);
516 OUT_RING(ctx->rb3d_planemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 ADVANCE_RING();
518 }
519
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 if (dirty & RADEON_UPLOAD_VIEWPORT) {
521 BEGIN_RING(7);
522 OUT_RING(CP_PACKET0(RADEON_SE_VPORT_XSCALE, 5));
523 OUT_RING(ctx->se_vport_xscale);
524 OUT_RING(ctx->se_vport_xoffset);
525 OUT_RING(ctx->se_vport_yscale);
526 OUT_RING(ctx->se_vport_yoffset);
527 OUT_RING(ctx->se_vport_zscale);
528 OUT_RING(ctx->se_vport_zoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ADVANCE_RING();
530 }
531
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 if (dirty & RADEON_UPLOAD_SETUP) {
533 BEGIN_RING(4);
534 OUT_RING(CP_PACKET0(RADEON_SE_CNTL, 0));
535 OUT_RING(ctx->se_cntl);
536 OUT_RING(CP_PACKET0(RADEON_SE_CNTL_STATUS, 0));
537 OUT_RING(ctx->se_cntl_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ADVANCE_RING();
539 }
540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 if (dirty & RADEON_UPLOAD_MISC) {
542 BEGIN_RING(2);
543 OUT_RING(CP_PACKET0(RADEON_RE_MISC, 0));
544 OUT_RING(ctx->re_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 ADVANCE_RING();
546 }
547
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 if (dirty & RADEON_UPLOAD_TEX0) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000549 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 &tex[0].pp_txoffset)) {
551 DRM_ERROR("Invalid texture offset for unit 0\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 BEGIN_RING(9);
556 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_0, 5));
557 OUT_RING(tex[0].pp_txfilter);
558 OUT_RING(tex[0].pp_txformat);
559 OUT_RING(tex[0].pp_txoffset);
560 OUT_RING(tex[0].pp_txcblend);
561 OUT_RING(tex[0].pp_txablend);
562 OUT_RING(tex[0].pp_tfactor);
563 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_0, 0));
564 OUT_RING(tex[0].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ADVANCE_RING();
566 }
567
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 if (dirty & RADEON_UPLOAD_TEX1) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000569 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 &tex[1].pp_txoffset)) {
571 DRM_ERROR("Invalid texture offset for unit 1\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 BEGIN_RING(9);
576 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_1, 5));
577 OUT_RING(tex[1].pp_txfilter);
578 OUT_RING(tex[1].pp_txformat);
579 OUT_RING(tex[1].pp_txoffset);
580 OUT_RING(tex[1].pp_txcblend);
581 OUT_RING(tex[1].pp_txablend);
582 OUT_RING(tex[1].pp_tfactor);
583 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_1, 0));
584 OUT_RING(tex[1].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ADVANCE_RING();
586 }
587
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 if (dirty & RADEON_UPLOAD_TEX2) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000589 if (radeon_check_and_fixup_offset(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 &tex[2].pp_txoffset)) {
591 DRM_ERROR("Invalid texture offset for unit 2\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 BEGIN_RING(9);
596 OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_2, 5));
597 OUT_RING(tex[2].pp_txfilter);
598 OUT_RING(tex[2].pp_txformat);
599 OUT_RING(tex[2].pp_txoffset);
600 OUT_RING(tex[2].pp_txcblend);
601 OUT_RING(tex[2].pp_txablend);
602 OUT_RING(tex[2].pp_tfactor);
603 OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_2, 0));
604 OUT_RING(tex[2].pp_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ADVANCE_RING();
606 }
607
608 return 0;
609}
610
611/* Emit 1.2 state
612 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613static int radeon_emit_state2(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000614 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 drm_radeon_state_t * state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 RING_LOCALS;
618
619 if (state->dirty & RADEON_UPLOAD_ZBIAS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 BEGIN_RING(3);
621 OUT_RING(CP_PACKET0(RADEON_SE_ZBIAS_FACTOR, 1));
622 OUT_RING(state->context2.se_zbias_factor);
623 OUT_RING(state->context2.se_zbias_constant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ADVANCE_RING();
625 }
626
Eric Anholt6c340ea2007-08-25 20:23:09 +1000627 return radeon_emit_state(dev_priv, file_priv, &state->context,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000628 state->tex, state->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/* New (1.3) state mechanism. 3 commands (packet, scalar, vector) in
632 * 1.3 cmdbuffers allow all previous state to be updated as well as
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 * the tcl scalar and vector areas.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635static struct {
636 int start;
637 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 const char *name;
639} packet[RADEON_MAX_STATE_PACKETS] = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 {RADEON_PP_MISC, 7, "RADEON_PP_MISC"},
641 {RADEON_PP_CNTL, 3, "RADEON_PP_CNTL"},
642 {RADEON_RB3D_COLORPITCH, 1, "RADEON_RB3D_COLORPITCH"},
643 {RADEON_RE_LINE_PATTERN, 2, "RADEON_RE_LINE_PATTERN"},
644 {RADEON_SE_LINE_WIDTH, 1, "RADEON_SE_LINE_WIDTH"},
645 {RADEON_PP_LUM_MATRIX, 1, "RADEON_PP_LUM_MATRIX"},
646 {RADEON_PP_ROT_MATRIX_0, 2, "RADEON_PP_ROT_MATRIX_0"},
647 {RADEON_RB3D_STENCILREFMASK, 3, "RADEON_RB3D_STENCILREFMASK"},
648 {RADEON_SE_VPORT_XSCALE, 6, "RADEON_SE_VPORT_XSCALE"},
649 {RADEON_SE_CNTL, 2, "RADEON_SE_CNTL"},
650 {RADEON_SE_CNTL_STATUS, 1, "RADEON_SE_CNTL_STATUS"},
651 {RADEON_RE_MISC, 1, "RADEON_RE_MISC"},
652 {RADEON_PP_TXFILTER_0, 6, "RADEON_PP_TXFILTER_0"},
653 {RADEON_PP_BORDER_COLOR_0, 1, "RADEON_PP_BORDER_COLOR_0"},
654 {RADEON_PP_TXFILTER_1, 6, "RADEON_PP_TXFILTER_1"},
655 {RADEON_PP_BORDER_COLOR_1, 1, "RADEON_PP_BORDER_COLOR_1"},
656 {RADEON_PP_TXFILTER_2, 6, "RADEON_PP_TXFILTER_2"},
657 {RADEON_PP_BORDER_COLOR_2, 1, "RADEON_PP_BORDER_COLOR_2"},
658 {RADEON_SE_ZBIAS_FACTOR, 2, "RADEON_SE_ZBIAS_FACTOR"},
659 {RADEON_SE_TCL_OUTPUT_VTX_FMT, 11, "RADEON_SE_TCL_OUTPUT_VTX_FMT"},
660 {RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED, 17,
661 "RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED"},
662 {R200_PP_TXCBLEND_0, 4, "R200_PP_TXCBLEND_0"},
663 {R200_PP_TXCBLEND_1, 4, "R200_PP_TXCBLEND_1"},
664 {R200_PP_TXCBLEND_2, 4, "R200_PP_TXCBLEND_2"},
665 {R200_PP_TXCBLEND_3, 4, "R200_PP_TXCBLEND_3"},
666 {R200_PP_TXCBLEND_4, 4, "R200_PP_TXCBLEND_4"},
667 {R200_PP_TXCBLEND_5, 4, "R200_PP_TXCBLEND_5"},
668 {R200_PP_TXCBLEND_6, 4, "R200_PP_TXCBLEND_6"},
669 {R200_PP_TXCBLEND_7, 4, "R200_PP_TXCBLEND_7"},
670 {R200_SE_TCL_LIGHT_MODEL_CTL_0, 6, "R200_SE_TCL_LIGHT_MODEL_CTL_0"},
671 {R200_PP_TFACTOR_0, 6, "R200_PP_TFACTOR_0"},
672 {R200_SE_VTX_FMT_0, 4, "R200_SE_VTX_FMT_0"},
673 {R200_SE_VAP_CNTL, 1, "R200_SE_VAP_CNTL"},
674 {R200_SE_TCL_MATRIX_SEL_0, 5, "R200_SE_TCL_MATRIX_SEL_0"},
675 {R200_SE_TCL_TEX_PROC_CTL_2, 5, "R200_SE_TCL_TEX_PROC_CTL_2"},
676 {R200_SE_TCL_UCP_VERT_BLEND_CTL, 1, "R200_SE_TCL_UCP_VERT_BLEND_CTL"},
677 {R200_PP_TXFILTER_0, 6, "R200_PP_TXFILTER_0"},
678 {R200_PP_TXFILTER_1, 6, "R200_PP_TXFILTER_1"},
679 {R200_PP_TXFILTER_2, 6, "R200_PP_TXFILTER_2"},
680 {R200_PP_TXFILTER_3, 6, "R200_PP_TXFILTER_3"},
681 {R200_PP_TXFILTER_4, 6, "R200_PP_TXFILTER_4"},
682 {R200_PP_TXFILTER_5, 6, "R200_PP_TXFILTER_5"},
683 {R200_PP_TXOFFSET_0, 1, "R200_PP_TXOFFSET_0"},
684 {R200_PP_TXOFFSET_1, 1, "R200_PP_TXOFFSET_1"},
685 {R200_PP_TXOFFSET_2, 1, "R200_PP_TXOFFSET_2"},
686 {R200_PP_TXOFFSET_3, 1, "R200_PP_TXOFFSET_3"},
687 {R200_PP_TXOFFSET_4, 1, "R200_PP_TXOFFSET_4"},
688 {R200_PP_TXOFFSET_5, 1, "R200_PP_TXOFFSET_5"},
689 {R200_SE_VTE_CNTL, 1, "R200_SE_VTE_CNTL"},
Dave Airlied985c102006-01-02 21:32:48 +1100690 {R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1,
691 "R200_SE_TCL_OUTPUT_VTX_COMP_SEL"},
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 {R200_PP_TAM_DEBUG3, 1, "R200_PP_TAM_DEBUG3"},
693 {R200_PP_CNTL_X, 1, "R200_PP_CNTL_X"},
694 {R200_RB3D_DEPTHXY_OFFSET, 1, "R200_RB3D_DEPTHXY_OFFSET"},
695 {R200_RE_AUX_SCISSOR_CNTL, 1, "R200_RE_AUX_SCISSOR_CNTL"},
696 {R200_RE_SCISSOR_TL_0, 2, "R200_RE_SCISSOR_TL_0"},
697 {R200_RE_SCISSOR_TL_1, 2, "R200_RE_SCISSOR_TL_1"},
698 {R200_RE_SCISSOR_TL_2, 2, "R200_RE_SCISSOR_TL_2"},
699 {R200_SE_VAP_CNTL_STATUS, 1, "R200_SE_VAP_CNTL_STATUS"},
700 {R200_SE_VTX_STATE_CNTL, 1, "R200_SE_VTX_STATE_CNTL"},
701 {R200_RE_POINTSIZE, 1, "R200_RE_POINTSIZE"},
702 {R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0, 4,
703 "R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0"},
704 {R200_PP_CUBIC_FACES_0, 1, "R200_PP_CUBIC_FACES_0"}, /* 61 */
Dave Airlied985c102006-01-02 21:32:48 +1100705 {R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0"}, /* 62 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 {R200_PP_CUBIC_FACES_1, 1, "R200_PP_CUBIC_FACES_1"},
707 {R200_PP_CUBIC_OFFSET_F1_1, 5, "R200_PP_CUBIC_OFFSET_F1_1"},
708 {R200_PP_CUBIC_FACES_2, 1, "R200_PP_CUBIC_FACES_2"},
709 {R200_PP_CUBIC_OFFSET_F1_2, 5, "R200_PP_CUBIC_OFFSET_F1_2"},
710 {R200_PP_CUBIC_FACES_3, 1, "R200_PP_CUBIC_FACES_3"},
711 {R200_PP_CUBIC_OFFSET_F1_3, 5, "R200_PP_CUBIC_OFFSET_F1_3"},
712 {R200_PP_CUBIC_FACES_4, 1, "R200_PP_CUBIC_FACES_4"},
713 {R200_PP_CUBIC_OFFSET_F1_4, 5, "R200_PP_CUBIC_OFFSET_F1_4"},
714 {R200_PP_CUBIC_FACES_5, 1, "R200_PP_CUBIC_FACES_5"},
715 {R200_PP_CUBIC_OFFSET_F1_5, 5, "R200_PP_CUBIC_OFFSET_F1_5"},
716 {RADEON_PP_TEX_SIZE_0, 2, "RADEON_PP_TEX_SIZE_0"},
717 {RADEON_PP_TEX_SIZE_1, 2, "RADEON_PP_TEX_SIZE_1"},
718 {RADEON_PP_TEX_SIZE_2, 2, "RADEON_PP_TEX_SIZE_2"},
719 {R200_RB3D_BLENDCOLOR, 3, "R200_RB3D_BLENDCOLOR"},
720 {R200_SE_TCL_POINT_SPRITE_CNTL, 1, "R200_SE_TCL_POINT_SPRITE_CNTL"},
721 {RADEON_PP_CUBIC_FACES_0, 1, "RADEON_PP_CUBIC_FACES_0"},
722 {RADEON_PP_CUBIC_OFFSET_T0_0, 5, "RADEON_PP_CUBIC_OFFSET_T0_0"},
723 {RADEON_PP_CUBIC_FACES_1, 1, "RADEON_PP_CUBIC_FACES_1"},
724 {RADEON_PP_CUBIC_OFFSET_T1_0, 5, "RADEON_PP_CUBIC_OFFSET_T1_0"},
725 {RADEON_PP_CUBIC_FACES_2, 1, "RADEON_PP_CUBIC_FACES_2"},
726 {RADEON_PP_CUBIC_OFFSET_T2_0, 5, "RADEON_PP_CUBIC_OFFSET_T2_0"},
727 {R200_PP_TRI_PERF, 2, "R200_PP_TRI_PERF"},
Dave Airlied985c102006-01-02 21:32:48 +1100728 {R200_PP_AFS_0, 32, "R200_PP_AFS_0"}, /* 85 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 {R200_PP_AFS_1, 32, "R200_PP_AFS_1"},
730 {R200_PP_TFACTOR_0, 8, "R200_ATF_TFACTOR"},
731 {R200_PP_TXFILTER_0, 8, "R200_PP_TXCTLALL_0"},
732 {R200_PP_TXFILTER_1, 8, "R200_PP_TXCTLALL_1"},
733 {R200_PP_TXFILTER_2, 8, "R200_PP_TXCTLALL_2"},
734 {R200_PP_TXFILTER_3, 8, "R200_PP_TXCTLALL_3"},
735 {R200_PP_TXFILTER_4, 8, "R200_PP_TXCTLALL_4"},
736 {R200_PP_TXFILTER_5, 8, "R200_PP_TXCTLALL_5"},
Dave Airlied6fece02006-06-24 17:04:07 +1000737 {R200_VAP_PVS_CNTL_1, 2, "R200_VAP_PVS_CNTL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738};
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/* ================================================================
741 * Performance monitoring functions
742 */
743
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744static void radeon_clear_box(drm_radeon_private_t * dev_priv,
745 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 u32 color;
748 RING_LOCALS;
749
750 x += dev_priv->sarea_priv->boxes[0].x1;
751 y += dev_priv->sarea_priv->boxes[0].y1;
752
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 switch (dev_priv->color_fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 case RADEON_COLOR_FORMAT_RGB565:
755 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 case RADEON_COLOR_FORMAT_ARGB8888:
759 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 }
763
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 BEGIN_RING(4);
765 RADEON_WAIT_UNTIL_3D_IDLE();
766 OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
767 OUT_RING(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 ADVANCE_RING();
769
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 OUT_RING(CP_PACKET3(RADEON_CNTL_PAINT_MULTI, 4));
773 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
774 RADEON_GMC_BRUSH_SOLID_COLOR |
775 (dev_priv->color_fmt << 8) |
776 RADEON_GMC_SRC_DATATYPE_COLOR |
777 RADEON_ROP3_P | RADEON_GMC_CLR_CMP_CNTL_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Michel Dänzer453ff942007-05-08 15:21:14 +1000779 if (dev_priv->sarea_priv->pfCurrentPage == 1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 OUT_RING(dev_priv->front_pitch_offset);
781 } else {
782 OUT_RING(dev_priv->back_pitch_offset);
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000785 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 OUT_RING((x << 16) | y);
788 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 ADVANCE_RING();
791}
792
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793static void radeon_cp_performance_boxes(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 /* Collapse various things into a wait flag -- trying to
796 * guess if userspase slept -- better just to have them tell us.
797 */
798 if (dev_priv->stats.last_frame_reads > 1 ||
799 dev_priv->stats.last_clear_reads > dev_priv->stats.clears) {
800 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
801 }
802
803 if (dev_priv->stats.freelist_loops) {
804 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
805 }
806
807 /* Purple box for page flipping
808 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 if (dev_priv->stats.boxes & RADEON_BOX_FLIP)
810 radeon_clear_box(dev_priv, 4, 4, 8, 8, 255, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* Red box if we have to wait for idle at any point
813 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 if (dev_priv->stats.boxes & RADEON_BOX_WAIT_IDLE)
815 radeon_clear_box(dev_priv, 16, 4, 8, 8, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Blue box: lost context?
818 */
819
820 /* Yellow box for texture swaps
821 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 if (dev_priv->stats.boxes & RADEON_BOX_TEXTURE_LOAD)
823 radeon_clear_box(dev_priv, 40, 4, 8, 8, 255, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* Green box if hardware never idles (as far as we can tell)
826 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 if (!(dev_priv->stats.boxes & RADEON_BOX_DMA_IDLE))
828 radeon_clear_box(dev_priv, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 /* Draw bars indicating number of buffers allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * (not a great measure, easily confused)
832 */
833 if (dev_priv->stats.requested_bufs) {
834 if (dev_priv->stats.requested_bufs > 100)
835 dev_priv->stats.requested_bufs = 100;
836
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 radeon_clear_box(dev_priv, 4, 16,
838 dev_priv->stats.requested_bufs, 4,
839 196, 128, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 memset(&dev_priv->stats, 0, sizeof(dev_priv->stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846/* ================================================================
847 * CP command dispatch functions
848 */
849
Dave Airlie84b1fd12007-07-11 15:53:27 +1000850static void radeon_cp_dispatch_clear(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 drm_radeon_clear_t * clear,
852 drm_radeon_clear_rect_t * depth_boxes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 drm_radeon_private_t *dev_priv = dev->dev_private;
855 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
856 drm_radeon_depth_clear_t *depth_clear = &dev_priv->depth_clear;
857 int nbox = sarea_priv->nbox;
Dave Airliec60ce622007-07-11 15:27:12 +1000858 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 unsigned int flags = clear->flags;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 u32 rb3d_cntl = 0, rb3d_stencilrefmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 int i;
862 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 DRM_DEBUG("flags = 0x%x\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 dev_priv->stats.clears++;
866
Michel Dänzer453ff942007-05-08 15:21:14 +1000867 if (dev_priv->sarea_priv->pfCurrentPage == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 unsigned int tmp = flags;
869
870 flags &= ~(RADEON_FRONT | RADEON_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 if (tmp & RADEON_FRONT)
872 flags |= RADEON_BACK;
873 if (tmp & RADEON_BACK)
874 flags |= RADEON_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 if (flags & (RADEON_FRONT | RADEON_BACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* Ensure the 3D stream is idle before doing a
882 * 2D fill to clear the front or back buffer.
883 */
884 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000885
886 OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
887 OUT_RING(clear->color_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 ADVANCE_RING();
890
891 /* Make sure we restore the 3D state next time.
892 */
893 dev_priv->sarea_priv->ctx_owner = 0;
894
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int x = pbox[i].x1;
897 int y = pbox[i].y1;
898 int w = pbox[i].x2 - x;
899 int h = pbox[i].y2 - y;
900
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 DRM_DEBUG("dispatch clear %d,%d-%d,%d flags 0x%x\n",
902 x, y, w, h, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 if (flags & RADEON_FRONT) {
905 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 OUT_RING(CP_PACKET3
908 (RADEON_CNTL_PAINT_MULTI, 4));
909 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
910 RADEON_GMC_BRUSH_SOLID_COLOR |
911 (dev_priv->
912 color_fmt << 8) |
913 RADEON_GMC_SRC_DATATYPE_COLOR |
914 RADEON_ROP3_P |
915 RADEON_GMC_CLR_CMP_CNTL_DIS);
916
917 OUT_RING(dev_priv->front_pitch_offset);
918 OUT_RING(clear->clear_color);
919
920 OUT_RING((x << 16) | y);
921 OUT_RING((w << 16) | h);
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 ADVANCE_RING();
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 if (flags & RADEON_BACK) {
927 BEGIN_RING(6);
928
929 OUT_RING(CP_PACKET3
930 (RADEON_CNTL_PAINT_MULTI, 4));
931 OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
932 RADEON_GMC_BRUSH_SOLID_COLOR |
933 (dev_priv->
934 color_fmt << 8) |
935 RADEON_GMC_SRC_DATATYPE_COLOR |
936 RADEON_ROP3_P |
937 RADEON_GMC_CLR_CMP_CNTL_DIS);
938
939 OUT_RING(dev_priv->back_pitch_offset);
940 OUT_RING(clear->clear_color);
941
942 OUT_RING((x << 16) | y);
943 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 ADVANCE_RING();
946 }
947 }
948 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* hyper z clear */
951 /* no docs available, based on reverse engeneering by Stephane Marchesin */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 if ((flags & (RADEON_DEPTH | RADEON_STENCIL))
953 && (flags & RADEON_CLEAR_FASTZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 int depthpixperline =
957 dev_priv->depth_fmt ==
958 RADEON_DEPTH_FORMAT_16BIT_INT_Z ? (dev_priv->depth_pitch /
959 2) : (dev_priv->
960 depth_pitch / 4);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 u32 clearmask;
963
964 u32 tempRB3D_DEPTHCLEARVALUE = clear->clear_depth |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 ((clear->depth_mask & 0xff) << 24);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* Make sure we restore the 3D state next time.
968 * we haven't touched any "normal" state - still need this?
969 */
970 dev_priv->sarea_priv->ctx_owner = 0;
971
Dave Airlie54a56ac2006-09-22 04:25:09 +1000972 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000973 && (flags & RADEON_USE_HIERZ)) {
974 /* FIXME : reverse engineer that for Rx00 cards */
975 /* FIXME : the mask supposedly contains low-res z values. So can't set
976 just to the max (0xff? or actually 0x3fff?), need to take z clear
977 value into account? */
978 /* pattern seems to work for r100, though get slight
979 rendering errors with glxgears. If hierz is not enabled for r100,
980 only 4 bits which indicate clear (15,16,31,32, all zero) matter, the
981 other ones are ignored, and the same clear mask can be used. That's
982 very different behaviour than R200 which needs different clear mask
983 and different number of tiles to clear if hierz is enabled or not !?!
984 */
985 clearmask = (0xff << 22) | (0xff << 6) | 0x003f003f;
986 } else {
987 /* clear mask : chooses the clearing pattern.
988 rv250: could be used to clear only parts of macrotiles
989 (but that would get really complicated...)?
990 bit 0 and 1 (either or both of them ?!?!) are used to
991 not clear tile (or maybe one of the bits indicates if the tile is
992 compressed or not), bit 2 and 3 to not clear tile 1,...,.
993 Pattern is as follows:
994 | 0,1 | 4,5 | 8,9 |12,13|16,17|20,21|24,25|28,29|
995 bits -------------------------------------------------
996 | 2,3 | 6,7 |10,11|14,15|18,19|22,23|26,27|30,31|
997 rv100: clearmask covers 2x8 4x1 tiles, but one clear still
998 covers 256 pixels ?!?
999 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 clearmask = 0x0;
1001 }
1002
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 BEGIN_RING(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 RADEON_WAIT_UNTIL_2D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 OUT_RING_REG(RADEON_RB3D_DEPTHCLEARVALUE,
1006 tempRB3D_DEPTHCLEARVALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /* what offset is this exactly ? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 OUT_RING_REG(RADEON_RB3D_ZMASKOFFSET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* need ctlstat, otherwise get some strange black flickering */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 OUT_RING_REG(RADEON_RB3D_ZCACHE_CTLSTAT,
1011 RADEON_RB3D_ZC_FLUSH_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ADVANCE_RING();
1013
1014 for (i = 0; i < nbox; i++) {
1015 int tileoffset, nrtilesx, nrtilesy, j;
1016 /* it looks like r200 needs rv-style clears, at least if hierz is not enabled? */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001017 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 && !(dev_priv->microcode_version == UCODE_R200)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /* FIXME : figure this out for r200 (when hierz is enabled). Or
1020 maybe r200 actually doesn't need to put the low-res z value into
1021 the tile cache like r100, but just needs to clear the hi-level z-buffer?
1022 Works for R100, both with hierz and without.
1023 R100 seems to operate on 2x1 8x8 tiles, but...
1024 odd: offset/nrtiles need to be 64 pix (4 block) aligned? Potentially
1025 problematic with resolutions which are not 64 pix aligned? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 tileoffset =
1027 ((pbox[i].y1 >> 3) * depthpixperline +
1028 pbox[i].x1) >> 6;
1029 nrtilesx =
1030 ((pbox[i].x2 & ~63) -
1031 (pbox[i].x1 & ~63)) >> 4;
1032 nrtilesy =
1033 (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001035 BEGIN_RING(4);
1036 OUT_RING(CP_PACKET3
1037 (RADEON_3D_CLEAR_ZMASK, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* first tile */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 OUT_RING(tileoffset * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 OUT_RING(nrtilesx + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ADVANCE_RING();
1045 tileoffset += depthpixperline >> 6;
1046 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 } else if (dev_priv->microcode_version == UCODE_R200) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* works for rv250. */
1049 /* find first macro tile (8x2 4x4 z-pixels on rv250) */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 tileoffset =
1051 ((pbox[i].y1 >> 3) * depthpixperline +
1052 pbox[i].x1) >> 5;
1053 nrtilesx =
1054 (pbox[i].x2 >> 5) - (pbox[i].x1 >> 5);
1055 nrtilesy =
1056 (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 BEGIN_RING(4);
1059 OUT_RING(CP_PACKET3
1060 (RADEON_3D_CLEAR_ZMASK, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* first tile */
1062 /* judging by the first tile offset needed, could possibly
1063 directly address/clear 4x4 tiles instead of 8x2 * 4x4
1064 macro tiles, though would still need clear mask for
1065 right/bottom if truely 4x4 granularity is desired ? */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 OUT_RING(tileoffset * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 OUT_RING(nrtilesx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ADVANCE_RING();
1072 tileoffset += depthpixperline >> 5;
1073 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 } else { /* rv 100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* rv100 might not need 64 pix alignment, who knows */
1076 /* offsets are, hmm, weird */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 tileoffset =
1078 ((pbox[i].y1 >> 4) * depthpixperline +
1079 pbox[i].x1) >> 6;
1080 nrtilesx =
1081 ((pbox[i].x2 & ~63) -
1082 (pbox[i].x1 & ~63)) >> 4;
1083 nrtilesy =
1084 (pbox[i].y2 >> 4) - (pbox[i].y1 >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 for (j = 0; j <= nrtilesy; j++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 BEGIN_RING(4);
1087 OUT_RING(CP_PACKET3
1088 (RADEON_3D_CLEAR_ZMASK, 2));
1089 OUT_RING(tileoffset * 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /* the number of tiles to clear */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 OUT_RING(nrtilesx + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* clear mask : chooses the clearing pattern. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 OUT_RING(clearmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ADVANCE_RING();
1095 tileoffset += depthpixperline >> 6;
1096 }
1097 }
1098 }
1099
1100 /* TODO don't always clear all hi-level z tiles */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001101 if ((dev_priv->flags & RADEON_HAS_HIERZ)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 && (dev_priv->microcode_version == UCODE_R200)
1103 && (flags & RADEON_USE_HIERZ))
1104 /* r100 and cards without hierarchical z-buffer have no high-level z-buffer */
1105 /* FIXME : the mask supposedly contains low-res z values. So can't set
1106 just to the max (0xff? or actually 0x3fff?), need to take z clear
1107 value into account? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 BEGIN_RING(4);
1110 OUT_RING(CP_PACKET3(RADEON_3D_CLEAR_HIZ, 2));
1111 OUT_RING(0x0); /* First tile */
1112 OUT_RING(0x3cc0);
1113 OUT_RING((0xff << 22) | (0xff << 6) | 0x003f003f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ADVANCE_RING();
1115 }
1116 }
1117
1118 /* We have to clear the depth and/or stencil buffers by
1119 * rendering a quad into just those buffers. Thus, we have to
1120 * make sure the 3D engine is configured correctly.
1121 */
Dave Airlied985c102006-01-02 21:32:48 +11001122 else if ((dev_priv->microcode_version == UCODE_R200) &&
1123 (flags & (RADEON_DEPTH | RADEON_STENCIL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 int tempPP_CNTL;
1126 int tempRE_CNTL;
1127 int tempRB3D_CNTL;
1128 int tempRB3D_ZSTENCILCNTL;
1129 int tempRB3D_STENCILREFMASK;
1130 int tempRB3D_PLANEMASK;
1131 int tempSE_CNTL;
1132 int tempSE_VTE_CNTL;
1133 int tempSE_VTX_FMT_0;
1134 int tempSE_VTX_FMT_1;
1135 int tempSE_VAP_CNTL;
1136 int tempRE_AUX_SCISSOR_CNTL;
1137
1138 tempPP_CNTL = 0;
1139 tempRE_CNTL = 0;
1140
1141 tempRB3D_CNTL = depth_clear->rb3d_cntl;
1142
1143 tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
1144 tempRB3D_STENCILREFMASK = 0x0;
1145
1146 tempSE_CNTL = depth_clear->se_cntl;
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Disable TCL */
1149
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150 tempSE_VAP_CNTL = ( /* SE_VAP_CNTL__FORCE_W_TO_ONE_MASK | */
1151 (0x9 <<
1152 SE_VAP_CNTL__VF_MAX_VTX_NUM__SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 tempRB3D_PLANEMASK = 0x0;
1155
1156 tempRE_AUX_SCISSOR_CNTL = 0x0;
1157
1158 tempSE_VTE_CNTL =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 SE_VTE_CNTL__VTX_XY_FMT_MASK | SE_VTE_CNTL__VTX_Z_FMT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001161 /* Vertex format (X, Y, Z, W) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 tempSE_VTX_FMT_0 =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 SE_VTX_FMT_0__VTX_Z0_PRESENT_MASK |
1164 SE_VTX_FMT_0__VTX_W0_PRESENT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 tempSE_VTX_FMT_1 = 0x0;
1166
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167 /*
1168 * Depth buffer specific enables
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170 if (flags & RADEON_DEPTH) {
1171 /* Enable depth buffer */
1172 tempRB3D_CNTL |= RADEON_Z_ENABLE;
1173 } else {
1174 /* Disable depth buffer */
1175 tempRB3D_CNTL &= ~RADEON_Z_ENABLE;
1176 }
1177
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001178 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * Stencil buffer specific enables
1180 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001181 if (flags & RADEON_STENCIL) {
1182 tempRB3D_CNTL |= RADEON_STENCIL_ENABLE;
1183 tempRB3D_STENCILREFMASK = clear->depth_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 } else {
1185 tempRB3D_CNTL &= ~RADEON_STENCIL_ENABLE;
1186 tempRB3D_STENCILREFMASK = 0x00000000;
1187 }
1188
1189 if (flags & RADEON_USE_COMP_ZBUF) {
1190 tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001191 RADEON_Z_DECOMPRESSION_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 if (flags & RADEON_USE_HIERZ) {
1194 tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
1195 }
1196
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001197 BEGIN_RING(26);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 RADEON_WAIT_UNTIL_2D_IDLE();
1199
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001200 OUT_RING_REG(RADEON_PP_CNTL, tempPP_CNTL);
1201 OUT_RING_REG(R200_RE_CNTL, tempRE_CNTL);
1202 OUT_RING_REG(RADEON_RB3D_CNTL, tempRB3D_CNTL);
1203 OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
1204 OUT_RING_REG(RADEON_RB3D_STENCILREFMASK,
1205 tempRB3D_STENCILREFMASK);
1206 OUT_RING_REG(RADEON_RB3D_PLANEMASK, tempRB3D_PLANEMASK);
1207 OUT_RING_REG(RADEON_SE_CNTL, tempSE_CNTL);
1208 OUT_RING_REG(R200_SE_VTE_CNTL, tempSE_VTE_CNTL);
1209 OUT_RING_REG(R200_SE_VTX_FMT_0, tempSE_VTX_FMT_0);
1210 OUT_RING_REG(R200_SE_VTX_FMT_1, tempSE_VTX_FMT_1);
1211 OUT_RING_REG(R200_SE_VAP_CNTL, tempSE_VAP_CNTL);
1212 OUT_RING_REG(R200_RE_AUX_SCISSOR_CNTL, tempRE_AUX_SCISSOR_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 ADVANCE_RING();
1214
1215 /* Make sure we restore the 3D state next time.
1216 */
1217 dev_priv->sarea_priv->ctx_owner = 0;
1218
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219 for (i = 0; i < nbox; i++) {
1220
1221 /* Funny that this should be required --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * sets top-left?
1223 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001224 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001226 BEGIN_RING(14);
1227 OUT_RING(CP_PACKET3(R200_3D_DRAW_IMMD_2, 12));
1228 OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
1229 RADEON_PRIM_WALK_RING |
1230 (3 << RADEON_NUM_VERTICES_SHIFT)));
1231 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1232 OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
1233 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1234 OUT_RING(0x3f800000);
1235 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1236 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1237 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1238 OUT_RING(0x3f800000);
1239 OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
1240 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1241 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1242 OUT_RING(0x3f800000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 ADVANCE_RING();
1244 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001245 } else if ((flags & (RADEON_DEPTH | RADEON_STENCIL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 int tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
1248
1249 rb3d_cntl = depth_clear->rb3d_cntl;
1250
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251 if (flags & RADEON_DEPTH) {
1252 rb3d_cntl |= RADEON_Z_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 } else {
1254 rb3d_cntl &= ~RADEON_Z_ENABLE;
1255 }
1256
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257 if (flags & RADEON_STENCIL) {
1258 rb3d_cntl |= RADEON_STENCIL_ENABLE;
1259 rb3d_stencilrefmask = clear->depth_mask; /* misnamed field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else {
1261 rb3d_cntl &= ~RADEON_STENCIL_ENABLE;
1262 rb3d_stencilrefmask = 0x00000000;
1263 }
1264
1265 if (flags & RADEON_USE_COMP_ZBUF) {
1266 tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001267 RADEON_Z_DECOMPRESSION_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 if (flags & RADEON_USE_HIERZ) {
1270 tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
1271 }
1272
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001273 BEGIN_RING(13);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 RADEON_WAIT_UNTIL_2D_IDLE();
1275
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001276 OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 1));
1277 OUT_RING(0x00000000);
1278 OUT_RING(rb3d_cntl);
1279
1280 OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
1281 OUT_RING_REG(RADEON_RB3D_STENCILREFMASK, rb3d_stencilrefmask);
1282 OUT_RING_REG(RADEON_RB3D_PLANEMASK, 0x00000000);
1283 OUT_RING_REG(RADEON_SE_CNTL, depth_clear->se_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 ADVANCE_RING();
1285
1286 /* Make sure we restore the 3D state next time.
1287 */
1288 dev_priv->sarea_priv->ctx_owner = 0;
1289
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001290 for (i = 0; i < nbox; i++) {
1291
1292 /* Funny that this should be required --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * sets top-left?
1294 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001295 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001297 BEGIN_RING(15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001299 OUT_RING(CP_PACKET3(RADEON_3D_DRAW_IMMD, 13));
1300 OUT_RING(RADEON_VTX_Z_PRESENT |
1301 RADEON_VTX_PKCOLOR_PRESENT);
1302 OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
1303 RADEON_PRIM_WALK_RING |
1304 RADEON_MAOS_ENABLE |
1305 RADEON_VTX_FMT_RADEON_MODE |
1306 (3 << RADEON_NUM_VERTICES_SHIFT)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001308 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1309 OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
1310 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1311 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001313 OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
1314 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1315 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1316 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001318 OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
1319 OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
1320 OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
1321 OUT_RING(0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 ADVANCE_RING();
1324 }
1325 }
1326
1327 /* Increment the clear counter. The client-side 3D driver must
1328 * wait on this value before performing the clear ioctl. We
1329 * need this because the card's so damned fast...
1330 */
1331 dev_priv->sarea_priv->last_clear++;
1332
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001335 RADEON_CLEAR_AGE(dev_priv->sarea_priv->last_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 RADEON_WAIT_UNTIL_IDLE();
1337
1338 ADVANCE_RING();
1339}
1340
Dave Airlie84b1fd12007-07-11 15:53:27 +10001341static void radeon_cp_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 drm_radeon_private_t *dev_priv = dev->dev_private;
1344 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
1345 int nbox = sarea_priv->nbox;
Dave Airliec60ce622007-07-11 15:27:12 +10001346 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int i;
1348 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001349 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* Do some trivial performance monitoring...
1352 */
1353 if (dev_priv->do_boxes)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354 radeon_cp_performance_boxes(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* Wait for the 3D stream to idle before dispatching the bitblt.
1357 * This will prevent data corruption between the two streams.
1358 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001359 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 RADEON_WAIT_UNTIL_3D_IDLE();
1362
1363 ADVANCE_RING();
1364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int x = pbox[i].x1;
1367 int y = pbox[i].y1;
1368 int w = pbox[i].x2 - x;
1369 int h = pbox[i].y2 - y;
1370
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001371 DRM_DEBUG("dispatch swap %d,%d-%d,%d\n", x, y, w, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Michel Daenzer3e14a282006-09-22 04:26:35 +10001373 BEGIN_RING(9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Michel Daenzer3e14a282006-09-22 04:26:35 +10001375 OUT_RING(CP_PACKET0(RADEON_DP_GUI_MASTER_CNTL, 0));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001376 OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
1377 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
1378 RADEON_GMC_BRUSH_NONE |
1379 (dev_priv->color_fmt << 8) |
1380 RADEON_GMC_SRC_DATATYPE_COLOR |
1381 RADEON_ROP3_S |
1382 RADEON_DP_SRC_SOURCE_MEMORY |
1383 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* Make this work even if front & back are flipped:
1386 */
Michel Daenzer3e14a282006-09-22 04:26:35 +10001387 OUT_RING(CP_PACKET0(RADEON_SRC_PITCH_OFFSET, 1));
Michel Dänzer453ff942007-05-08 15:21:14 +10001388 if (dev_priv->sarea_priv->pfCurrentPage == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 OUT_RING(dev_priv->back_pitch_offset);
1390 OUT_RING(dev_priv->front_pitch_offset);
1391 } else {
1392 OUT_RING(dev_priv->front_pitch_offset);
1393 OUT_RING(dev_priv->back_pitch_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
Michel Daenzer3e14a282006-09-22 04:26:35 +10001396 OUT_RING(CP_PACKET0(RADEON_SRC_X_Y, 2));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001397 OUT_RING((x << 16) | y);
1398 OUT_RING((x << 16) | y);
1399 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 ADVANCE_RING();
1402 }
1403
1404 /* Increment the frame counter. The client-side 3D driver must
1405 * throttle the framerate by waiting for this value before
1406 * performing the swapbuffer ioctl.
1407 */
1408 dev_priv->sarea_priv->last_frame++;
1409
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001410 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 RADEON_FRAME_AGE(dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 RADEON_WAIT_UNTIL_2D_IDLE();
1414
1415 ADVANCE_RING();
1416}
1417
Dave Airlie84b1fd12007-07-11 15:53:27 +10001418static void radeon_cp_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliebd63cb52007-07-12 10:35:02 +10001421 struct drm_sarea *sarea = (struct drm_sarea *) dev_priv->sarea->handle;
Michel Dänzer453ff942007-05-08 15:21:14 +10001422 int offset = (dev_priv->sarea_priv->pfCurrentPage == 1)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001423 ? dev_priv->front_offset : dev_priv->back_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 RING_LOCALS;
Michel Dänzer453ff942007-05-08 15:21:14 +10001425 DRM_DEBUG("%s: pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001426 __FUNCTION__,
Michel Dänzer453ff942007-05-08 15:21:14 +10001427 dev_priv->sarea_priv->pfCurrentPage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 /* Do some trivial performance monitoring...
1430 */
1431 if (dev_priv->do_boxes) {
1432 dev_priv->stats.boxes |= RADEON_BOX_FLIP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 radeon_cp_performance_boxes(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
1436 /* Update the frame offsets for both CRTCs
1437 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001438 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001441 OUT_RING_REG(RADEON_CRTC_OFFSET,
1442 ((sarea->frame.y * dev_priv->front_pitch +
1443 sarea->frame.x * (dev_priv->color_fmt - 2)) & ~7)
1444 + offset);
1445 OUT_RING_REG(RADEON_CRTC2_OFFSET, dev_priv->sarea_priv->crtc2_base
1446 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 ADVANCE_RING();
1449
1450 /* Increment the frame counter. The client-side 3D driver must
1451 * throttle the framerate by waiting for this value before
1452 * performing the swapbuffer ioctl.
1453 */
1454 dev_priv->sarea_priv->last_frame++;
Michel Dänzer453ff942007-05-08 15:21:14 +10001455 dev_priv->sarea_priv->pfCurrentPage =
1456 1 - dev_priv->sarea_priv->pfCurrentPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001458 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001460 RADEON_FRAME_AGE(dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 ADVANCE_RING();
1463}
1464
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001465static int bad_prim_vertex_nr(int primitive, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 switch (primitive & RADEON_PRIM_TYPE_MASK) {
1468 case RADEON_PRIM_TYPE_NONE:
1469 case RADEON_PRIM_TYPE_POINT:
1470 return nr < 1;
1471 case RADEON_PRIM_TYPE_LINE:
1472 return (nr & 1) || nr == 0;
1473 case RADEON_PRIM_TYPE_LINE_STRIP:
1474 return nr < 2;
1475 case RADEON_PRIM_TYPE_TRI_LIST:
1476 case RADEON_PRIM_TYPE_3VRT_POINT_LIST:
1477 case RADEON_PRIM_TYPE_3VRT_LINE_LIST:
1478 case RADEON_PRIM_TYPE_RECT_LIST:
1479 return nr % 3 || nr == 0;
1480 case RADEON_PRIM_TYPE_TRI_FAN:
1481 case RADEON_PRIM_TYPE_TRI_STRIP:
1482 return nr < 3;
1483 default:
1484 return 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488typedef struct {
1489 unsigned int start;
1490 unsigned int finish;
1491 unsigned int prim;
1492 unsigned int numverts;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 unsigned int offset;
1494 unsigned int vc_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495} drm_radeon_tcl_prim_t;
1496
Dave Airlie84b1fd12007-07-11 15:53:27 +10001497static void radeon_cp_dispatch_vertex(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001498 struct drm_buf * buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001499 drm_radeon_tcl_prim_t * prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 drm_radeon_private_t *dev_priv = dev->dev_private;
1502 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
1503 int offset = dev_priv->gart_buffers_offset + buf->offset + prim->start;
1504 int numverts = (int)prim->numverts;
1505 int nbox = sarea_priv->nbox;
1506 int i = 0;
1507 RING_LOCALS;
1508
1509 DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d %d verts\n",
1510 prim->prim,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001511 prim->vc_format, prim->start, prim->finish, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001513 if (bad_prim_vertex_nr(prim->prim, prim->numverts)) {
1514 DRM_ERROR("bad prim %x numverts %d\n",
1515 prim->prim, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return;
1517 }
1518
1519 do {
1520 /* Emit the next cliprect */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001521 if (i < nbox) {
1522 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
1525 /* Emit the vertex buffer rendering commands */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526 BEGIN_RING(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001528 OUT_RING(CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, 3));
1529 OUT_RING(offset);
1530 OUT_RING(numverts);
1531 OUT_RING(prim->vc_format);
1532 OUT_RING(prim->prim | RADEON_PRIM_WALK_LIST |
1533 RADEON_COLOR_ORDER_RGBA |
1534 RADEON_VTX_FMT_RADEON_MODE |
1535 (numverts << RADEON_NUM_VERTICES_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 ADVANCE_RING();
1538
1539 i++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 } while (i < nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Dave Airlie056219e2007-07-11 16:17:42 +10001543static void radeon_cp_discard_buffer(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 drm_radeon_private_t *dev_priv = dev->dev_private;
1546 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1547 RING_LOCALS;
1548
1549 buf_priv->age = ++dev_priv->sarea_priv->last_dispatch;
1550
1551 /* Emit the vertex buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001552 BEGIN_RING(2);
1553 RADEON_DISPATCH_AGE(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 ADVANCE_RING();
1555
1556 buf->pending = 1;
1557 buf->used = 0;
1558}
1559
Dave Airlie84b1fd12007-07-11 15:53:27 +10001560static void radeon_cp_dispatch_indirect(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001561 struct drm_buf * buf, int start, int end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 drm_radeon_private_t *dev_priv = dev->dev_private;
1564 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 DRM_DEBUG("indirect: buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001567 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 int offset = (dev_priv->gart_buffers_offset
1569 + buf->offset + start);
1570 int dwords = (end - start + 3) / sizeof(u32);
1571
1572 /* Indirect buffer data must be an even number of
1573 * dwords, so if we've been given an odd number we must
1574 * pad the data with a Type-2 CP packet.
1575 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001576 if (dwords & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 u32 *data = (u32 *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 ((char *)dev->agp_buffer_map->handle
1579 + buf->offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 data[dwords++] = RADEON_CP_PACKET2;
1581 }
1582
1583 /* Fire off the indirect buffer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584 BEGIN_RING(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001586 OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
1587 OUT_RING(offset);
1588 OUT_RING(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 ADVANCE_RING();
1591 }
1592}
1593
Dave Airlie84b1fd12007-07-11 15:53:27 +10001594static void radeon_cp_dispatch_indices(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +10001595 struct drm_buf * elt_buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 drm_radeon_tcl_prim_t * prim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 drm_radeon_private_t *dev_priv = dev->dev_private;
1599 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
1600 int offset = dev_priv->gart_buffers_offset + prim->offset;
1601 u32 *data;
1602 int dwords;
1603 int i = 0;
1604 int start = prim->start + RADEON_INDEX_PRIM_OFFSET;
1605 int count = (prim->finish - start) / sizeof(u16);
1606 int nbox = sarea_priv->nbox;
1607
1608 DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d offset: %x nr %d\n",
1609 prim->prim,
1610 prim->vc_format,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 prim->start, prim->finish, prim->offset, prim->numverts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001613 if (bad_prim_vertex_nr(prim->prim, count)) {
1614 DRM_ERROR("bad prim %x count %d\n", prim->prim, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return;
1616 }
1617
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001618 if (start >= prim->finish || (prim->start & 0x7)) {
1619 DRM_ERROR("buffer prim %d\n", prim->prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return;
1621 }
1622
1623 dwords = (prim->finish - prim->start + 3) / sizeof(u32);
1624
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001625 data = (u32 *) ((char *)dev->agp_buffer_map->handle +
1626 elt_buf->offset + prim->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001628 data[0] = CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, dwords - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 data[1] = offset;
1630 data[2] = prim->numverts;
1631 data[3] = prim->vc_format;
1632 data[4] = (prim->prim |
1633 RADEON_PRIM_WALK_IND |
1634 RADEON_COLOR_ORDER_RGBA |
1635 RADEON_VTX_FMT_RADEON_MODE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001636 (count << RADEON_NUM_VERTICES_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001639 if (i < nbox)
1640 radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001642 radeon_cp_dispatch_indirect(dev, elt_buf,
1643 prim->start, prim->finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 i++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001646 } while (i < nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648}
1649
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001650#define RADEON_MAX_TEXTURE_SIZE RADEON_BUFFER_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Eric Anholt6c340ea2007-08-25 20:23:09 +10001652static int radeon_cp_dispatch_texture(struct drm_device * dev,
1653 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001654 drm_radeon_texture_t * tex,
1655 drm_radeon_tex_image_t * image)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie056219e2007-07-11 16:17:42 +10001658 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 u32 format;
1660 u32 *buffer;
1661 const u8 __user *data;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001662 int size, dwords, tex_width, blit_width, spitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 u32 height;
1664 int i;
1665 u32 texpitch, microtile;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001666 u32 offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 RING_LOCALS;
1668
Eric Anholt6c340ea2007-08-25 20:23:09 +10001669 if (radeon_check_and_fixup_offset(dev_priv, file_priv, &tex->offset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001670 DRM_ERROR("Invalid destination offset\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001671 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
1674 dev_priv->stats.boxes |= RADEON_BOX_TEXTURE_LOAD;
1675
1676 /* Flush the pixel cache. This ensures no pixel data gets mixed
1677 * up with the texture data from the host data blit, otherwise
1678 * part of the texture image may be corrupted.
1679 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001680 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 RADEON_FLUSH_CACHE();
1682 RADEON_WAIT_UNTIL_IDLE();
1683 ADVANCE_RING();
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* The compiler won't optimize away a division by a variable,
1686 * even if the only legal values are powers of two. Thus, we'll
1687 * use a shift instead.
1688 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001689 switch (tex->format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 case RADEON_TXFORMAT_ARGB8888:
1691 case RADEON_TXFORMAT_RGBA8888:
1692 format = RADEON_COLOR_FORMAT_ARGB8888;
1693 tex_width = tex->width * 4;
1694 blit_width = image->width * 4;
1695 break;
1696 case RADEON_TXFORMAT_AI88:
1697 case RADEON_TXFORMAT_ARGB1555:
1698 case RADEON_TXFORMAT_RGB565:
1699 case RADEON_TXFORMAT_ARGB4444:
1700 case RADEON_TXFORMAT_VYUY422:
1701 case RADEON_TXFORMAT_YVYU422:
1702 format = RADEON_COLOR_FORMAT_RGB565;
1703 tex_width = tex->width * 2;
1704 blit_width = image->width * 2;
1705 break;
1706 case RADEON_TXFORMAT_I8:
1707 case RADEON_TXFORMAT_RGB332:
1708 format = RADEON_COLOR_FORMAT_CI8;
1709 tex_width = tex->width * 1;
1710 blit_width = image->width * 1;
1711 break;
1712 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001713 DRM_ERROR("invalid texture format %d\n", tex->format);
Eric Anholt20caafa2007-08-25 19:22:43 +10001714 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001716 spitch = blit_width >> 6;
1717 if (spitch == 0 && image->height > 1)
Eric Anholt20caafa2007-08-25 19:22:43 +10001718 return -EINVAL;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 texpitch = tex->pitch;
1721 if ((texpitch << 22) & RADEON_DST_TILE_MICRO) {
1722 microtile = 1;
1723 if (tex_width < 64) {
1724 texpitch &= ~(RADEON_DST_TILE_MICRO >> 22);
1725 /* we got tiled coordinates, untile them */
1726 image->x *= 2;
1727 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001728 } else
1729 microtile = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001731 DRM_DEBUG("tex=%dx%d blit=%d\n", tex_width, tex->height, blit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001734 DRM_DEBUG("tex: ofs=0x%x p=%d f=%d x=%hd y=%hd w=%hd h=%hd\n",
1735 tex->offset >> 10, tex->pitch, tex->format,
1736 image->x, image->y, image->width, image->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /* Make a copy of some parameters in case we have to
1739 * update them for a multi-pass texture blit.
1740 */
1741 height = image->height;
1742 data = (const u8 __user *)image->data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 size = height * blit_width;
1745
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001746 if (size > RADEON_MAX_TEXTURE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 height = RADEON_MAX_TEXTURE_SIZE / blit_width;
1748 size = height * blit_width;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001749 } else if (size < 4 && size > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 size = 4;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001751 } else if (size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753 }
1754
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001755 buf = radeon_freelist_get(dev);
1756 if (0 && !buf) {
1757 radeon_do_cp_idle(dev_priv);
1758 buf = radeon_freelist_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001760 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 DRM_DEBUG("radeon_cp_dispatch_texture: EAGAIN\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001762 if (DRM_COPY_TO_USER(tex->image, image, sizeof(*image)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001763 return -EFAULT;
1764 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /* Dispatch the indirect buffer.
1768 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001769 buffer =
1770 (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 dwords = size / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Dave Airlied985c102006-01-02 21:32:48 +11001773#define RADEON_COPY_MT(_buf, _data, _width) \
1774 do { \
1775 if (DRM_COPY_FROM_USER(_buf, _data, (_width))) {\
1776 DRM_ERROR("EFAULT on pad, %d bytes\n", (_width)); \
Eric Anholt20caafa2007-08-25 19:22:43 +10001777 return -EFAULT; \
Dave Airlied985c102006-01-02 21:32:48 +11001778 } \
1779 } while(0)
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (microtile) {
1782 /* texture micro tiling in use, minimum texture width is thus 16 bytes.
1783 however, we cannot use blitter directly for texture width < 64 bytes,
1784 since minimum tex pitch is 64 bytes and we need this to match
1785 the texture width, otherwise the blitter will tile it wrong.
1786 Thus, tiling manually in this case. Additionally, need to special
1787 case tex height = 1, since our actual image will have height 2
1788 and we need to ensure we don't read beyond the texture size
1789 from user space. */
1790 if (tex->height == 1) {
1791 if (tex_width >= 64 || tex_width <= 16) {
Dave Airlied985c102006-01-02 21:32:48 +11001792 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001793 (int)(tex_width * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 } else if (tex_width == 32) {
Dave Airlied985c102006-01-02 21:32:48 +11001795 RADEON_COPY_MT(buffer, data, 16);
1796 RADEON_COPY_MT(buffer + 8,
1797 data + 16, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 } else if (tex_width >= 64 || tex_width == 16) {
Dave Airlied985c102006-01-02 21:32:48 +11001800 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001801 (int)(dwords * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 } else if (tex_width < 16) {
1803 for (i = 0; i < tex->height; i++) {
Dave Airlied985c102006-01-02 21:32:48 +11001804 RADEON_COPY_MT(buffer, data, tex_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 buffer += 4;
1806 data += tex_width;
1807 }
1808 } else if (tex_width == 32) {
1809 /* TODO: make sure this works when not fitting in one buffer
1810 (i.e. 32bytes x 2048...) */
1811 for (i = 0; i < tex->height; i += 2) {
Dave Airlied985c102006-01-02 21:32:48 +11001812 RADEON_COPY_MT(buffer, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001814 RADEON_COPY_MT(buffer + 8, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001816 RADEON_COPY_MT(buffer + 4, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 data += 16;
Dave Airlied985c102006-01-02 21:32:48 +11001818 RADEON_COPY_MT(buffer + 12, data, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 data += 16;
1820 buffer += 16;
1821 }
1822 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001823 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (tex_width >= 32) {
1825 /* Texture image width is larger than the minimum, so we
1826 * can upload it directly.
1827 */
Dave Airlied985c102006-01-02 21:32:48 +11001828 RADEON_COPY_MT(buffer, data,
Dave Airlief8e0f292006-01-10 19:56:17 +11001829 (int)(dwords * sizeof(u32)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 } else {
1831 /* Texture image width is less than the minimum, so we
1832 * need to pad out each image scanline to the minimum
1833 * width.
1834 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001835 for (i = 0; i < tex->height; i++) {
Dave Airlied985c102006-01-02 21:32:48 +11001836 RADEON_COPY_MT(buffer, data, tex_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 buffer += 8;
1838 data += tex_width;
1839 }
1840 }
1841 }
1842
Dave Airlied985c102006-01-02 21:32:48 +11001843#undef RADEON_COPY_MT
Eric Anholt6c340ea2007-08-25 20:23:09 +10001844 buf->file_priv = file_priv;
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001845 buf->used = size;
1846 offset = dev_priv->gart_buffers_offset + buf->offset;
1847 BEGIN_RING(9);
1848 OUT_RING(CP_PACKET3(RADEON_CNTL_BITBLT_MULTI, 5));
1849 OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
1850 RADEON_GMC_DST_PITCH_OFFSET_CNTL |
1851 RADEON_GMC_BRUSH_NONE |
1852 (format << 8) |
1853 RADEON_GMC_SRC_DATATYPE_COLOR |
1854 RADEON_ROP3_S |
1855 RADEON_DP_SRC_SOURCE_MEMORY |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001856 RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001857 OUT_RING((spitch << 22) | (offset >> 10));
1858 OUT_RING((texpitch << 22) | (tex->offset >> 10));
1859 OUT_RING(0);
1860 OUT_RING((image->x << 16) | image->y);
1861 OUT_RING((image->width << 16) | height);
1862 RADEON_WAIT_UNTIL_2D_IDLE();
1863 ADVANCE_RING();
chaohong guoeed0f722007-10-15 10:45:49 +10001864 COMMIT_RING();
Dave Airlieffbbf7a2005-08-20 17:40:04 +10001865
1866 radeon_cp_discard_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* Update the input parameters for next time */
1869 image->y += height;
1870 image->height -= height;
1871 image->data = (const u8 __user *)image->data + size;
1872 } while (image->height > 0);
1873
1874 /* Flush the pixel cache after the blit completes. This ensures
1875 * the texture data is written out to memory before rendering
1876 * continues.
1877 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001878 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 RADEON_FLUSH_CACHE();
1880 RADEON_WAIT_UNTIL_2D_IDLE();
1881 ADVANCE_RING();
chaohong guoeed0f722007-10-15 10:45:49 +10001882 COMMIT_RING();
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 0;
1885}
1886
Dave Airlie84b1fd12007-07-11 15:53:27 +10001887static void radeon_cp_dispatch_stipple(struct drm_device * dev, u32 * stipple)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 drm_radeon_private_t *dev_priv = dev->dev_private;
1890 int i;
1891 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001892 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001894 BEGIN_RING(35);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001896 OUT_RING(CP_PACKET0(RADEON_RE_STIPPLE_ADDR, 0));
1897 OUT_RING(0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001899 OUT_RING(CP_PACKET0_TABLE(RADEON_RE_STIPPLE_DATA, 31));
1900 for (i = 0; i < 32; i++) {
1901 OUT_RING(stipple[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
1904 ADVANCE_RING();
1905}
1906
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001907static void radeon_apply_surface_regs(int surf_index,
Dave Airlied985c102006-01-02 21:32:48 +11001908 drm_radeon_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 if (!dev_priv->mmio)
1911 return;
1912
1913 radeon_do_cp_idle(dev_priv);
1914
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001915 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * surf_index,
1916 dev_priv->surfaces[surf_index].flags);
1917 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * surf_index,
1918 dev_priv->surfaces[surf_index].lower);
1919 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * surf_index,
1920 dev_priv->surfaces[surf_index].upper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/* Allocates a virtual surface
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001924 * doesn't always allocate a real surface, will stretch an existing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * surface when possible.
1926 *
1927 * Note that refcount can be at most 2, since during a free refcount=3
1928 * might mean we have to allocate a new surface which might not always
1929 * be available.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001930 * For example : we allocate three contigous surfaces ABC. If B is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * freed, we suddenly need two surfaces to store A and C, which might
1932 * not always be available.
1933 */
Dave Airlied985c102006-01-02 21:32:48 +11001934static int alloc_surface(drm_radeon_surface_alloc_t *new,
Eric Anholt6c340ea2007-08-25 20:23:09 +10001935 drm_radeon_private_t *dev_priv,
1936 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 struct radeon_virt_surface *s;
1939 int i;
1940 int virt_surface_index;
1941 uint32_t new_upper, new_lower;
1942
1943 new_lower = new->address;
1944 new_upper = new_lower + new->size - 1;
1945
1946 /* sanity check */
1947 if ((new_lower >= new_upper) || (new->flags == 0) || (new->size == 0) ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001948 ((new_upper & RADEON_SURF_ADDRESS_FIXED_MASK) !=
1949 RADEON_SURF_ADDRESS_FIXED_MASK)
1950 || ((new_lower & RADEON_SURF_ADDRESS_FIXED_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return -1;
1952
1953 /* make sure there is no overlap with existing surfaces */
1954 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
1955 if ((dev_priv->surfaces[i].refcount != 0) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001956 (((new_lower >= dev_priv->surfaces[i].lower) &&
1957 (new_lower < dev_priv->surfaces[i].upper)) ||
1958 ((new_lower < dev_priv->surfaces[i].lower) &&
1959 (new_upper > dev_priv->surfaces[i].lower)))) {
1960 return -1;
1961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963
1964 /* find a virtual surface */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001965 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++)
Eric Anholt6c340ea2007-08-25 20:23:09 +10001966 if (dev_priv->virt_surfaces[i].file_priv == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001968 if (i == 2 * RADEON_MAX_SURFACES) {
1969 return -1;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 virt_surface_index = i;
1972
1973 /* try to reuse an existing surface */
1974 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
1975 /* extend before */
1976 if ((dev_priv->surfaces[i].refcount == 1) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001977 (new->flags == dev_priv->surfaces[i].flags) &&
1978 (new_upper + 1 == dev_priv->surfaces[i].lower)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 s = &(dev_priv->virt_surfaces[virt_surface_index]);
1980 s->surface_index = i;
1981 s->lower = new_lower;
1982 s->upper = new_upper;
1983 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001984 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 dev_priv->surfaces[i].refcount++;
1986 dev_priv->surfaces[i].lower = s->lower;
1987 radeon_apply_surface_regs(s->surface_index, dev_priv);
1988 return virt_surface_index;
1989 }
1990
1991 /* extend after */
1992 if ((dev_priv->surfaces[i].refcount == 1) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001993 (new->flags == dev_priv->surfaces[i].flags) &&
1994 (new_lower == dev_priv->surfaces[i].upper + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 s = &(dev_priv->virt_surfaces[virt_surface_index]);
1996 s->surface_index = i;
1997 s->lower = new_lower;
1998 s->upper = new_upper;
1999 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002000 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 dev_priv->surfaces[i].refcount++;
2002 dev_priv->surfaces[i].upper = s->upper;
2003 radeon_apply_surface_regs(s->surface_index, dev_priv);
2004 return virt_surface_index;
2005 }
2006 }
2007
2008 /* okay, we need a new one */
2009 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
2010 if (dev_priv->surfaces[i].refcount == 0) {
2011 s = &(dev_priv->virt_surfaces[virt_surface_index]);
2012 s->surface_index = i;
2013 s->lower = new_lower;
2014 s->upper = new_upper;
2015 s->flags = new->flags;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002016 s->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 dev_priv->surfaces[i].refcount = 1;
2018 dev_priv->surfaces[i].lower = s->lower;
2019 dev_priv->surfaces[i].upper = s->upper;
2020 dev_priv->surfaces[i].flags = s->flags;
2021 radeon_apply_surface_regs(s->surface_index, dev_priv);
2022 return virt_surface_index;
2023 }
2024 }
2025
2026 /* we didn't find anything */
2027 return -1;
2028}
2029
Eric Anholt6c340ea2007-08-25 20:23:09 +10002030static int free_surface(struct drm_file *file_priv,
2031 drm_radeon_private_t * dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002032 int lower)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 struct radeon_virt_surface *s;
2035 int i;
2036 /* find the virtual surface */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002037 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 s = &(dev_priv->virt_surfaces[i]);
Eric Anholt6c340ea2007-08-25 20:23:09 +10002039 if (s->file_priv) {
2040 if ((lower == s->lower) && (file_priv == s->file_priv))
2041 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002042 if (dev_priv->surfaces[s->surface_index].
2043 lower == s->lower)
2044 dev_priv->surfaces[s->surface_index].
2045 lower = s->upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002047 if (dev_priv->surfaces[s->surface_index].
2048 upper == s->upper)
2049 dev_priv->surfaces[s->surface_index].
2050 upper = s->lower;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 dev_priv->surfaces[s->surface_index].refcount--;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002053 if (dev_priv->surfaces[s->surface_index].
2054 refcount == 0)
2055 dev_priv->surfaces[s->surface_index].
2056 flags = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10002057 s->file_priv = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002058 radeon_apply_surface_regs(s->surface_index,
2059 dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return 0;
2061 }
2062 }
2063 }
2064 return 1;
2065}
2066
Eric Anholt6c340ea2007-08-25 20:23:09 +10002067static void radeon_surfaces_release(struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002068 drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002071 for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002072 if (dev_priv->virt_surfaces[i].file_priv == file_priv)
2073 free_surface(file_priv, dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002074 dev_priv->virt_surfaces[i].lower);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076}
2077
2078/* ================================================================
2079 * IOCTL functions
2080 */
Eric Anholtc153f452007-09-03 12:06:45 +10002081static int radeon_surface_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002084 drm_radeon_surface_alloc_t *alloc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Eric Anholtc153f452007-09-03 12:06:45 +10002086 if (alloc_surface(alloc, dev_priv, file_priv) == -1)
Eric Anholt20caafa2007-08-25 19:22:43 +10002087 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 else
2089 return 0;
2090}
2091
Eric Anholtc153f452007-09-03 12:06:45 +10002092static int radeon_surface_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002095 drm_radeon_surface_free_t *memfree = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Eric Anholtc153f452007-09-03 12:06:45 +10002097 if (free_surface(file_priv, dev_priv, memfree->address))
Eric Anholt20caafa2007-08-25 19:22:43 +10002098 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 else
2100 return 0;
2101}
2102
Eric Anholtc153f452007-09-03 12:06:45 +10002103static int radeon_cp_clear(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 drm_radeon_private_t *dev_priv = dev->dev_private;
2106 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10002107 drm_radeon_clear_t *clear = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 drm_radeon_clear_rect_t depth_boxes[RADEON_NR_SAREA_CLIPRECTS];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002109 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Eric Anholt6c340ea2007-08-25 20:23:09 +10002111 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002113 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002115 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
2117
Eric Anholtc153f452007-09-03 12:06:45 +10002118 if (DRM_COPY_FROM_USER(&depth_boxes, clear->depth_boxes,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002119 sarea_priv->nbox * sizeof(depth_boxes[0])))
Eric Anholt20caafa2007-08-25 19:22:43 +10002120 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Eric Anholtc153f452007-09-03 12:06:45 +10002122 radeon_cp_dispatch_clear(dev, clear, depth_boxes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 COMMIT_RING();
2125 return 0;
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128/* Not sure why this isn't set all the time:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002129 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002130static int radeon_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 drm_radeon_private_t *dev_priv = dev->dev_private;
2133 RING_LOCALS;
2134
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002135 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002137 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 RADEON_WAIT_UNTIL_3D_IDLE();
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002139 OUT_RING(CP_PACKET0(RADEON_CRTC_OFFSET_CNTL, 0));
2140 OUT_RING(RADEON_READ(RADEON_CRTC_OFFSET_CNTL) |
2141 RADEON_CRTC_OFFSET_FLIP_CNTL);
2142 OUT_RING(CP_PACKET0(RADEON_CRTC2_OFFSET_CNTL, 0));
2143 OUT_RING(RADEON_READ(RADEON_CRTC2_OFFSET_CNTL) |
2144 RADEON_CRTC_OFFSET_FLIP_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 ADVANCE_RING();
2146
2147 dev_priv->page_flipping = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Michel Dänzer453ff942007-05-08 15:21:14 +10002149 if (dev_priv->sarea_priv->pfCurrentPage != 1)
2150 dev_priv->sarea_priv->pfCurrentPage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return 0;
2153}
2154
2155/* Swapping and flipping are different operations, need different ioctls.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002156 * They can & should be intermixed to support multiple 3d windows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 */
Eric Anholtc153f452007-09-03 12:06:45 +10002158static int radeon_cp_flip(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002161 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Eric Anholt6c340ea2007-08-25 20:23:09 +10002163 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002165 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002167 if (!dev_priv->page_flipping)
2168 radeon_do_init_pageflip(dev);
2169
2170 radeon_cp_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 COMMIT_RING();
2173 return 0;
2174}
2175
Eric Anholtc153f452007-09-03 12:06:45 +10002176static int radeon_cp_swap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 drm_radeon_private_t *dev_priv = dev->dev_private;
2179 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002180 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Eric Anholt6c340ea2007-08-25 20:23:09 +10002182 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002184 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002186 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
2188
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002189 radeon_cp_dispatch_swap(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 dev_priv->sarea_priv->ctx_owner = 0;
2191
2192 COMMIT_RING();
2193 return 0;
2194}
2195
Eric Anholtc153f452007-09-03 12:06:45 +10002196static int radeon_cp_vertex(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 drm_radeon_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002200 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002201 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002202 drm_radeon_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 drm_radeon_tcl_prim_t prim;
2204
Eric Anholt6c340ea2007-08-25 20:23:09 +10002205 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002207 DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002208 DRM_CURRENTPID, vertex->idx, vertex->count, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Eric Anholtc153f452007-09-03 12:06:45 +10002210 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002211 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002212 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Eric Anholtc153f452007-09-03 12:06:45 +10002215 if (vertex->prim < 0 || vertex->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
2216 DRM_ERROR("buffer prim %d\n", vertex->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10002217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002220 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2221 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Eric Anholtc153f452007-09-03 12:06:45 +10002223 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Eric Anholt6c340ea2007-08-25 20:23:09 +10002225 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002226 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002227 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002230 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002231 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234
2235 /* Build up a prim_t record:
2236 */
Eric Anholtc153f452007-09-03 12:06:45 +10002237 if (vertex->count) {
2238 buf->used = vertex->count; /* not used? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002240 if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002241 if (radeon_emit_state(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002242 &sarea_priv->context_state,
2243 sarea_priv->tex_state,
2244 sarea_priv->dirty)) {
2245 DRM_ERROR("radeon_emit_state failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002246 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
2249 sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
2250 RADEON_UPLOAD_TEX1IMAGES |
2251 RADEON_UPLOAD_TEX2IMAGES |
2252 RADEON_REQUIRE_QUIESCENCE);
2253 }
2254
2255 prim.start = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10002256 prim.finish = vertex->count; /* unused */
2257 prim.prim = vertex->prim;
2258 prim.numverts = vertex->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 prim.vc_format = dev_priv->sarea_priv->vc_format;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002260
2261 radeon_cp_dispatch_vertex(dev, buf, &prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263
Eric Anholtc153f452007-09-03 12:06:45 +10002264 if (vertex->discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002265 radeon_cp_discard_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267
2268 COMMIT_RING();
2269 return 0;
2270}
2271
Eric Anholtc153f452007-09-03 12:06:45 +10002272static int radeon_cp_indices(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 drm_radeon_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002276 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002277 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002278 drm_radeon_indices_t *elts = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 drm_radeon_tcl_prim_t prim;
2280 int count;
2281
Eric Anholt6c340ea2007-08-25 20:23:09 +10002282 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002284 DRM_DEBUG("pid=%d index=%d start=%d end=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002285 DRM_CURRENTPID, elts->idx, elts->start, elts->end,
2286 elts->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Eric Anholtc153f452007-09-03 12:06:45 +10002288 if (elts->idx < 0 || elts->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002289 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002290 elts->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
Eric Anholtc153f452007-09-03 12:06:45 +10002293 if (elts->prim < 0 || elts->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
2294 DRM_ERROR("buffer prim %d\n", elts->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10002295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002298 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2299 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Eric Anholtc153f452007-09-03 12:06:45 +10002301 buf = dma->buflist[elts->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Eric Anholt6c340ea2007-08-25 20:23:09 +10002303 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002304 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002305 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002308 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002309 DRM_ERROR("sending pending buffer %d\n", elts->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
Eric Anholtc153f452007-09-03 12:06:45 +10002313 count = (elts->end - elts->start) / sizeof(u16);
2314 elts->start -= RADEON_INDEX_PRIM_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Eric Anholtc153f452007-09-03 12:06:45 +10002316 if (elts->start & 0x7) {
2317 DRM_ERROR("misaligned buffer 0x%x\n", elts->start);
Eric Anholt20caafa2007-08-25 19:22:43 +10002318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
Eric Anholtc153f452007-09-03 12:06:45 +10002320 if (elts->start < buf->used) {
2321 DRM_ERROR("no header 0x%x - 0x%x\n", elts->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10002322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
Eric Anholtc153f452007-09-03 12:06:45 +10002325 buf->used = elts->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002327 if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
Eric Anholt6c340ea2007-08-25 20:23:09 +10002328 if (radeon_emit_state(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002329 &sarea_priv->context_state,
2330 sarea_priv->tex_state,
2331 sarea_priv->dirty)) {
2332 DRM_ERROR("radeon_emit_state failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002333 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
2336 sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
2337 RADEON_UPLOAD_TEX1IMAGES |
2338 RADEON_UPLOAD_TEX2IMAGES |
2339 RADEON_REQUIRE_QUIESCENCE);
2340 }
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* Build up a prim_t record:
2343 */
Eric Anholtc153f452007-09-03 12:06:45 +10002344 prim.start = elts->start;
2345 prim.finish = elts->end;
2346 prim.prim = elts->prim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 prim.offset = 0; /* offset from start of dma buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002348 prim.numverts = RADEON_MAX_VB_VERTS; /* duh */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 prim.vc_format = dev_priv->sarea_priv->vc_format;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002350
2351 radeon_cp_dispatch_indices(dev, buf, &prim);
Eric Anholtc153f452007-09-03 12:06:45 +10002352 if (elts->discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002353 radeon_cp_discard_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355
2356 COMMIT_RING();
2357 return 0;
2358}
2359
Eric Anholtc153f452007-09-03 12:06:45 +10002360static int radeon_cp_texture(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002363 drm_radeon_texture_t *tex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 drm_radeon_tex_image_t image;
2365 int ret;
2366
Eric Anholt6c340ea2007-08-25 20:23:09 +10002367 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Eric Anholtc153f452007-09-03 12:06:45 +10002369 if (tex->image == NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002370 DRM_ERROR("null texture image!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002371 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002374 if (DRM_COPY_FROM_USER(&image,
Eric Anholtc153f452007-09-03 12:06:45 +10002375 (drm_radeon_tex_image_t __user *) tex->image,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002376 sizeof(image)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002377 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002379 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2380 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Eric Anholtc153f452007-09-03 12:06:45 +10002382 ret = radeon_cp_dispatch_texture(dev, file_priv, tex, &image);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return ret;
2385}
2386
Eric Anholtc153f452007-09-03 12:06:45 +10002387static int radeon_cp_stipple(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002390 drm_radeon_stipple_t *stipple = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 u32 mask[32];
2392
Eric Anholt6c340ea2007-08-25 20:23:09 +10002393 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Eric Anholtc153f452007-09-03 12:06:45 +10002395 if (DRM_COPY_FROM_USER(&mask, stipple->mask, 32 * sizeof(u32)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002396 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002398 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002400 radeon_cp_dispatch_stipple(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 COMMIT_RING();
2403 return 0;
2404}
2405
Eric Anholtc153f452007-09-03 12:06:45 +10002406static int radeon_cp_indirect(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10002409 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002410 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002411 drm_radeon_indirect_t *indirect = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 RING_LOCALS;
2413
Eric Anholt6c340ea2007-08-25 20:23:09 +10002414 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002416 DRM_DEBUG("indirect: idx=%d s=%d e=%d d=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002417 indirect->idx, indirect->start, indirect->end,
2418 indirect->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Eric Anholtc153f452007-09-03 12:06:45 +10002420 if (indirect->idx < 0 || indirect->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002421 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002422 indirect->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425
Eric Anholtc153f452007-09-03 12:06:45 +10002426 buf = dma->buflist[indirect->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Eric Anholt6c340ea2007-08-25 20:23:09 +10002428 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002429 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002430 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002433 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002434 DRM_ERROR("sending pending buffer %d\n", indirect->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437
Eric Anholtc153f452007-09-03 12:06:45 +10002438 if (indirect->start < buf->used) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002439 DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002440 indirect->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10002441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002444 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2445 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Eric Anholtc153f452007-09-03 12:06:45 +10002447 buf->used = indirect->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 /* Wait for the 3D stream to idle before the indirect buffer
2450 * containing 2D acceleration commands is processed.
2451 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002452 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 RADEON_WAIT_UNTIL_3D_IDLE();
2455
2456 ADVANCE_RING();
2457
2458 /* Dispatch the indirect buffer full of commands from the
2459 * X server. This is insecure and is thus only available to
2460 * privileged clients.
2461 */
Eric Anholtc153f452007-09-03 12:06:45 +10002462 radeon_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
2463 if (indirect->discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002464 radeon_cp_discard_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 COMMIT_RING();
2468 return 0;
2469}
2470
Eric Anholtc153f452007-09-03 12:06:45 +10002471static int radeon_cp_vertex2(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 drm_radeon_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airliecdd55a22007-07-11 16:32:08 +10002475 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002476 struct drm_buf *buf;
Eric Anholtc153f452007-09-03 12:06:45 +10002477 drm_radeon_vertex2_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 int i;
2479 unsigned char laststate;
2480
Eric Anholt6c340ea2007-08-25 20:23:09 +10002481 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002483 DRM_DEBUG("pid=%d index=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002484 DRM_CURRENTPID, vertex->idx, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Eric Anholtc153f452007-09-03 12:06:45 +10002486 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002487 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002488 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10002489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002492 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2493 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Eric Anholtc153f452007-09-03 12:06:45 +10002495 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Eric Anholt6c340ea2007-08-25 20:23:09 +10002497 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002498 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002499 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10002500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002503 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10002504 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10002505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
Eric Anholt20caafa2007-08-25 19:22:43 +10002509 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Eric Anholtc153f452007-09-03 12:06:45 +10002511 for (laststate = 0xff, i = 0; i < vertex->nr_prims; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 drm_radeon_prim_t prim;
2513 drm_radeon_tcl_prim_t tclprim;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002514
Eric Anholtc153f452007-09-03 12:06:45 +10002515 if (DRM_COPY_FROM_USER(&prim, &vertex->prim[i], sizeof(prim)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002516 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002517
2518 if (prim.stateidx != laststate) {
2519 drm_radeon_state_t state;
2520
2521 if (DRM_COPY_FROM_USER(&state,
Eric Anholtc153f452007-09-03 12:06:45 +10002522 &vertex->state[prim.stateidx],
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002523 sizeof(state)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002524 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Eric Anholt6c340ea2007-08-25 20:23:09 +10002526 if (radeon_emit_state2(dev_priv, file_priv, &state)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002527 DRM_ERROR("radeon_emit_state2 failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002528 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
2531 laststate = prim.stateidx;
2532 }
2533
2534 tclprim.start = prim.start;
2535 tclprim.finish = prim.finish;
2536 tclprim.prim = prim.prim;
2537 tclprim.vc_format = prim.vc_format;
2538
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002539 if (prim.prim & RADEON_PRIM_WALK_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 tclprim.offset = prim.numverts * 64;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002541 tclprim.numverts = RADEON_MAX_VB_VERTS; /* duh */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002543 radeon_cp_dispatch_indices(dev, buf, &tclprim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 } else {
2545 tclprim.numverts = prim.numverts;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002546 tclprim.offset = 0; /* not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002548 radeon_cp_dispatch_vertex(dev, buf, &tclprim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (sarea_priv->nbox == 1)
2552 sarea_priv->nbox = 0;
2553 }
2554
Eric Anholtc153f452007-09-03 12:06:45 +10002555 if (vertex->discard) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002556 radeon_cp_discard_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558
2559 COMMIT_RING();
2560 return 0;
2561}
2562
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002563static int radeon_emit_packets(drm_radeon_private_t * dev_priv,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002564 struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002565 drm_radeon_cmd_header_t header,
Dave Airlieb3a83632005-09-30 18:37:36 +10002566 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 int id = (int)header.packet.packet_id;
2569 int sz, reg;
2570 int *data = (int *)cmdbuf->buf;
2571 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (id >= RADEON_MAX_STATE_PACKETS)
Eric Anholt20caafa2007-08-25 19:22:43 +10002574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 sz = packet[id].len;
2577 reg = packet[id].start;
2578
2579 if (sz * sizeof(int) > cmdbuf->bufsz) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002580 DRM_ERROR("Packet size provided larger than data provided\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002581 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583
Eric Anholt6c340ea2007-08-25 20:23:09 +10002584 if (radeon_check_and_fixup_packets(dev_priv, file_priv, id, data)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002585 DRM_ERROR("Packet verification failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10002586 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002589 BEGIN_RING(sz + 1);
2590 OUT_RING(CP_PACKET0(reg, (sz - 1)));
2591 OUT_RING_TABLE(data, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 ADVANCE_RING();
2593
2594 cmdbuf->buf += sz * sizeof(int);
2595 cmdbuf->bufsz -= sz * sizeof(int);
2596 return 0;
2597}
2598
Dave Airlied985c102006-01-02 21:32:48 +11002599static __inline__ int radeon_emit_scalars(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002600 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002601 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 int sz = header.scalars.count;
2604 int start = header.scalars.offset;
2605 int stride = header.scalars.stride;
2606 RING_LOCALS;
2607
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002608 BEGIN_RING(3 + sz);
2609 OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
2610 OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
2611 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
2612 OUT_RING_TABLE(cmdbuf->buf, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 ADVANCE_RING();
2614 cmdbuf->buf += sz * sizeof(int);
2615 cmdbuf->bufsz -= sz * sizeof(int);
2616 return 0;
2617}
2618
2619/* God this is ugly
2620 */
Dave Airlied985c102006-01-02 21:32:48 +11002621static __inline__ int radeon_emit_scalars2(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002622 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002623 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 int sz = header.scalars.count;
2626 int start = ((unsigned int)header.scalars.offset) + 0x100;
2627 int stride = header.scalars.stride;
2628 RING_LOCALS;
2629
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002630 BEGIN_RING(3 + sz);
2631 OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
2632 OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
2633 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
2634 OUT_RING_TABLE(cmdbuf->buf, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 ADVANCE_RING();
2636 cmdbuf->buf += sz * sizeof(int);
2637 cmdbuf->bufsz -= sz * sizeof(int);
2638 return 0;
2639}
2640
Dave Airlied985c102006-01-02 21:32:48 +11002641static __inline__ int radeon_emit_vectors(drm_radeon_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002642 drm_radeon_cmd_header_t header,
Dave Airlied985c102006-01-02 21:32:48 +11002643 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 int sz = header.vectors.count;
2646 int start = header.vectors.offset;
2647 int stride = header.vectors.stride;
2648 RING_LOCALS;
2649
Dave Airlief2a22792006-06-24 16:55:34 +10002650 BEGIN_RING(5 + sz);
2651 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002652 OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
2653 OUT_RING(start | (stride << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
2654 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
2655 OUT_RING_TABLE(cmdbuf->buf, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 ADVANCE_RING();
2657
2658 cmdbuf->buf += sz * sizeof(int);
2659 cmdbuf->bufsz -= sz * sizeof(int);
2660 return 0;
2661}
2662
Dave Airlied6fece02006-06-24 17:04:07 +10002663static __inline__ int radeon_emit_veclinear(drm_radeon_private_t *dev_priv,
2664 drm_radeon_cmd_header_t header,
2665 drm_radeon_kcmd_buffer_t *cmdbuf)
2666{
2667 int sz = header.veclinear.count * 4;
2668 int start = header.veclinear.addr_lo | (header.veclinear.addr_hi << 8);
2669 RING_LOCALS;
2670
2671 if (!sz)
2672 return 0;
2673 if (sz * 4 > cmdbuf->bufsz)
Eric Anholt20caafa2007-08-25 19:22:43 +10002674 return -EINVAL;
Dave Airlied6fece02006-06-24 17:04:07 +10002675
2676 BEGIN_RING(5 + sz);
2677 OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
2678 OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
2679 OUT_RING(start | (1 << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
2680 OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
2681 OUT_RING_TABLE(cmdbuf->buf, sz);
2682 ADVANCE_RING();
2683
2684 cmdbuf->buf += sz * sizeof(int);
2685 cmdbuf->bufsz -= sz * sizeof(int);
2686 return 0;
2687}
2688
Dave Airlie84b1fd12007-07-11 15:53:27 +10002689static int radeon_emit_packet3(struct drm_device * dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002690 struct drm_file *file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +10002691 drm_radeon_kcmd_buffer_t *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 drm_radeon_private_t *dev_priv = dev->dev_private;
2694 unsigned int cmdsz;
2695 int ret;
2696 RING_LOCALS;
2697
2698 DRM_DEBUG("\n");
2699
Eric Anholt6c340ea2007-08-25 20:23:09 +10002700 if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002701 cmdbuf, &cmdsz))) {
2702 DRM_ERROR("Packet verification failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return ret;
2704 }
2705
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002706 BEGIN_RING(cmdsz);
2707 OUT_RING_TABLE(cmdbuf->buf, cmdsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 ADVANCE_RING();
2709
2710 cmdbuf->buf += cmdsz * 4;
2711 cmdbuf->bufsz -= cmdsz * 4;
2712 return 0;
2713}
2714
Dave Airlie84b1fd12007-07-11 15:53:27 +10002715static int radeon_emit_packet3_cliprect(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +10002716 struct drm_file *file_priv,
Dave Airlieb3a83632005-09-30 18:37:36 +10002717 drm_radeon_kcmd_buffer_t *cmdbuf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002718 int orig_nbox)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliec60ce622007-07-11 15:27:12 +10002721 struct drm_clip_rect box;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 unsigned int cmdsz;
2723 int ret;
Dave Airliec60ce622007-07-11 15:27:12 +10002724 struct drm_clip_rect __user *boxes = cmdbuf->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 int i = 0;
2726 RING_LOCALS;
2727
2728 DRM_DEBUG("\n");
2729
Eric Anholt6c340ea2007-08-25 20:23:09 +10002730 if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002731 cmdbuf, &cmdsz))) {
2732 DRM_ERROR("Packet verification failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return ret;
2734 }
2735
2736 if (!orig_nbox)
2737 goto out;
2738
2739 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002740 if (i < cmdbuf->nbox) {
2741 if (DRM_COPY_FROM_USER(&box, &boxes[i], sizeof(box)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002742 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* FIXME The second and subsequent times round
2744 * this loop, send a WAIT_UNTIL_3D_IDLE before
2745 * calling emit_clip_rect(). This fixes a
2746 * lockup on fast machines when sending
2747 * several cliprects with a cmdbuf, as when
2748 * waving a 2D window over a 3D
2749 * window. Something in the commands from user
2750 * space seems to hang the card when they're
2751 * sent several times in a row. That would be
2752 * the correct place to fix it but this works
2753 * around it until I can figure that out - Tim
2754 * Smith */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002755 if (i) {
2756 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 RADEON_WAIT_UNTIL_3D_IDLE();
2758 ADVANCE_RING();
2759 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002760 radeon_emit_clip_rect(dev_priv, &box);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002762
2763 BEGIN_RING(cmdsz);
2764 OUT_RING_TABLE(cmdbuf->buf, cmdsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 ADVANCE_RING();
2766
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002767 } while (++i < cmdbuf->nbox);
2768 if (cmdbuf->nbox == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 cmdbuf->nbox = 0;
2770
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002771 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 cmdbuf->buf += cmdsz * 4;
2773 cmdbuf->bufsz -= cmdsz * 4;
2774 return 0;
2775}
2776
Dave Airlie84b1fd12007-07-11 15:53:27 +10002777static int radeon_emit_wait(struct drm_device * dev, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 drm_radeon_private_t *dev_priv = dev->dev_private;
2780 RING_LOCALS;
2781
2782 DRM_DEBUG("%s: %x\n", __FUNCTION__, flags);
2783 switch (flags) {
2784 case RADEON_WAIT_2D:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002785 BEGIN_RING(2);
2786 RADEON_WAIT_UNTIL_2D_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 ADVANCE_RING();
2788 break;
2789 case RADEON_WAIT_3D:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002790 BEGIN_RING(2);
2791 RADEON_WAIT_UNTIL_3D_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 ADVANCE_RING();
2793 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002794 case RADEON_WAIT_2D | RADEON_WAIT_3D:
2795 BEGIN_RING(2);
2796 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 ADVANCE_RING();
2798 break;
2799 default:
Eric Anholt20caafa2007-08-25 19:22:43 +10002800 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802
2803 return 0;
2804}
2805
Eric Anholtc153f452007-09-03 12:06:45 +10002806static int radeon_cp_cmdbuf(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10002809 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10002810 struct drm_buf *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 int idx;
Eric Anholtc153f452007-09-03 12:06:45 +10002812 drm_radeon_kcmd_buffer_t *cmdbuf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 drm_radeon_cmd_header_t header;
2814 int orig_nbox, orig_bufsz;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002815 char *kbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Eric Anholt6c340ea2007-08-25 20:23:09 +10002817 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002819 RING_SPACE_TEST_WITH_RETURN(dev_priv);
2820 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Eric Anholtc153f452007-09-03 12:06:45 +10002822 if (cmdbuf->bufsz > 64 * 1024 || cmdbuf->bufsz < 0) {
Eric Anholt20caafa2007-08-25 19:22:43 +10002823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
2825
2826 /* Allocate an in-kernel area and copy in the cmdbuf. Do this to avoid
2827 * races between checking values and using those values in other code,
2828 * and simply to avoid a lot of function calls to copy in data.
2829 */
Eric Anholtc153f452007-09-03 12:06:45 +10002830 orig_bufsz = cmdbuf->bufsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (orig_bufsz != 0) {
Eric Anholtc153f452007-09-03 12:06:45 +10002832 kbuf = drm_alloc(cmdbuf->bufsz, DRM_MEM_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (kbuf == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10002834 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +10002835 if (DRM_COPY_FROM_USER(kbuf, (void __user *)cmdbuf->buf,
2836 cmdbuf->bufsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
Eric Anholt20caafa2007-08-25 19:22:43 +10002838 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
Eric Anholtc153f452007-09-03 12:06:45 +10002840 cmdbuf->buf = kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842
Eric Anholtc153f452007-09-03 12:06:45 +10002843 orig_nbox = cmdbuf->nbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002845 if (dev_priv->microcode_version == UCODE_R300) {
Dave Airlie414ed532005-08-16 20:43:16 +10002846 int temp;
Eric Anholtc153f452007-09-03 12:06:45 +10002847 temp = r300_do_cp_cmdbuf(dev, file_priv, cmdbuf);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002848
Dave Airlie414ed532005-08-16 20:43:16 +10002849 if (orig_bufsz != 0)
2850 drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002851
Dave Airlie414ed532005-08-16 20:43:16 +10002852 return temp;
2853 }
2854
2855 /* microcode_version != r300 */
Eric Anholtc153f452007-09-03 12:06:45 +10002856 while (cmdbuf->bufsz >= sizeof(header)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Eric Anholtc153f452007-09-03 12:06:45 +10002858 header.i = *(int *)cmdbuf->buf;
2859 cmdbuf->buf += sizeof(header);
2860 cmdbuf->bufsz -= sizeof(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 switch (header.header.cmd_type) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002863 case RADEON_CMD_PACKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 DRM_DEBUG("RADEON_CMD_PACKET\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002865 if (radeon_emit_packets
Eric Anholtc153f452007-09-03 12:06:45 +10002866 (dev_priv, file_priv, header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 DRM_ERROR("radeon_emit_packets failed\n");
2868 goto err;
2869 }
2870 break;
2871
2872 case RADEON_CMD_SCALARS:
2873 DRM_DEBUG("RADEON_CMD_SCALARS\n");
Eric Anholtc153f452007-09-03 12:06:45 +10002874 if (radeon_emit_scalars(dev_priv, header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 DRM_ERROR("radeon_emit_scalars failed\n");
2876 goto err;
2877 }
2878 break;
2879
2880 case RADEON_CMD_VECTORS:
2881 DRM_DEBUG("RADEON_CMD_VECTORS\n");
Eric Anholtc153f452007-09-03 12:06:45 +10002882 if (radeon_emit_vectors(dev_priv, header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 DRM_ERROR("radeon_emit_vectors failed\n");
2884 goto err;
2885 }
2886 break;
2887
2888 case RADEON_CMD_DMA_DISCARD:
2889 DRM_DEBUG("RADEON_CMD_DMA_DISCARD\n");
2890 idx = header.dma.buf_idx;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002891 if (idx < 0 || idx >= dma->buf_count) {
2892 DRM_ERROR("buffer index %d (of %d max)\n",
2893 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 goto err;
2895 }
2896
2897 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10002898 if (buf->file_priv != file_priv || buf->pending) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002899 DRM_ERROR("bad buffer %p %p %d\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10002900 buf->file_priv, file_priv,
2901 buf->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 goto err;
2903 }
2904
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002905 radeon_cp_discard_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 break;
2907
2908 case RADEON_CMD_PACKET3:
2909 DRM_DEBUG("RADEON_CMD_PACKET3\n");
Eric Anholtc153f452007-09-03 12:06:45 +10002910 if (radeon_emit_packet3(dev, file_priv, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 DRM_ERROR("radeon_emit_packet3 failed\n");
2912 goto err;
2913 }
2914 break;
2915
2916 case RADEON_CMD_PACKET3_CLIP:
2917 DRM_DEBUG("RADEON_CMD_PACKET3_CLIP\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002918 if (radeon_emit_packet3_cliprect
Eric Anholtc153f452007-09-03 12:06:45 +10002919 (dev, file_priv, cmdbuf, orig_nbox)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 DRM_ERROR("radeon_emit_packet3_clip failed\n");
2921 goto err;
2922 }
2923 break;
2924
2925 case RADEON_CMD_SCALARS2:
2926 DRM_DEBUG("RADEON_CMD_SCALARS2\n");
Eric Anholtc153f452007-09-03 12:06:45 +10002927 if (radeon_emit_scalars2(dev_priv, header, cmdbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 DRM_ERROR("radeon_emit_scalars2 failed\n");
2929 goto err;
2930 }
2931 break;
2932
2933 case RADEON_CMD_WAIT:
2934 DRM_DEBUG("RADEON_CMD_WAIT\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002935 if (radeon_emit_wait(dev, header.wait.flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 DRM_ERROR("radeon_emit_wait failed\n");
2937 goto err;
2938 }
2939 break;
Dave Airlied6fece02006-06-24 17:04:07 +10002940 case RADEON_CMD_VECLINEAR:
2941 DRM_DEBUG("RADEON_CMD_VECLINEAR\n");
Eric Anholtc153f452007-09-03 12:06:45 +10002942 if (radeon_emit_veclinear(dev_priv, header, cmdbuf)) {
Dave Airlied6fece02006-06-24 17:04:07 +10002943 DRM_ERROR("radeon_emit_veclinear failed\n");
2944 goto err;
2945 }
2946 break;
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002949 DRM_ERROR("bad cmd_type %d at %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 header.header.cmd_type,
Eric Anholtc153f452007-09-03 12:06:45 +10002951 cmdbuf->buf - sizeof(header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 goto err;
2953 }
2954 }
2955
2956 if (orig_bufsz != 0)
2957 drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
2958
2959 DRM_DEBUG("DONE\n");
2960 COMMIT_RING();
2961 return 0;
2962
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002963 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (orig_bufsz != 0)
2965 drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
Eric Anholt20caafa2007-08-25 19:22:43 +10002966 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
Eric Anholtc153f452007-09-03 12:06:45 +10002969static int radeon_cp_getparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10002972 drm_radeon_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 int value;
2974
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002975 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Eric Anholtc153f452007-09-03 12:06:45 +10002977 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 case RADEON_PARAM_GART_BUFFER_OFFSET:
2979 value = dev_priv->gart_buffers_offset;
2980 break;
2981 case RADEON_PARAM_LAST_FRAME:
2982 dev_priv->stats.last_frame_reads++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002983 value = GET_SCRATCH(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 break;
2985 case RADEON_PARAM_LAST_DISPATCH:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002986 value = GET_SCRATCH(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 break;
2988 case RADEON_PARAM_LAST_CLEAR:
2989 dev_priv->stats.last_clear_reads++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002990 value = GET_SCRATCH(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 break;
2992 case RADEON_PARAM_IRQ_NR:
2993 value = dev->irq;
2994 break;
2995 case RADEON_PARAM_GART_BASE:
2996 value = dev_priv->gart_vm_start;
2997 break;
2998 case RADEON_PARAM_REGISTER_HANDLE:
Dave Airlied985c102006-01-02 21:32:48 +11002999 value = dev_priv->mmio->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 break;
3001 case RADEON_PARAM_STATUS_HANDLE:
3002 value = dev_priv->ring_rptr_offset;
3003 break;
3004#if BITS_PER_LONG == 32
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003005 /*
3006 * This ioctl() doesn't work on 64-bit platforms because hw_lock is a
3007 * pointer which can't fit into an int-sized variable. According to
3008 * Michel Dänzer, the ioctl() is only used on embedded platforms, so
3009 * not supporting it shouldn't be a problem. If the same functionality
3010 * is needed on 64-bit platforms, a new ioctl() would have to be added,
3011 * so backwards-compatibility for the embedded platforms can be
3012 * maintained. --davidm 4-Feb-2004.
3013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 case RADEON_PARAM_SAREA_HANDLE:
3015 /* The lock is the first dword in the sarea. */
3016 value = (long)dev->lock.hw_lock;
3017 break;
3018#endif
3019 case RADEON_PARAM_GART_TEX_HANDLE:
3020 value = dev_priv->gart_textures_offset;
3021 break;
Michel Dänzer8624ecb2006-08-07 20:33:57 +10003022 case RADEON_PARAM_SCRATCH_OFFSET:
3023 if (!dev_priv->writeback_works)
Eric Anholt20caafa2007-08-25 19:22:43 +10003024 return -EINVAL;
Michel Dänzer8624ecb2006-08-07 20:33:57 +10003025 value = RADEON_SCRATCH_REG_OFFSET;
3026 break;
Dave Airlied985c102006-01-02 21:32:48 +11003027 case RADEON_PARAM_CARD_TYPE:
Dave Airlie54a56ac2006-09-22 04:25:09 +10003028 if (dev_priv->flags & RADEON_IS_PCIE)
Dave Airlied985c102006-01-02 21:32:48 +11003029 value = RADEON_CARD_PCIE;
Dave Airlie54a56ac2006-09-22 04:25:09 +10003030 else if (dev_priv->flags & RADEON_IS_AGP)
Dave Airlied985c102006-01-02 21:32:48 +11003031 value = RADEON_CARD_AGP;
3032 else
3033 value = RADEON_CARD_PCI;
3034 break;
Dave Airlieddbee332007-07-11 12:16:01 +10003035 case RADEON_PARAM_VBLANK_CRTC:
3036 value = radeon_vblank_crtc_get(dev);
3037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 default:
Eric Anholtc153f452007-09-03 12:06:45 +10003039 DRM_DEBUG("Invalid parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10003040 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042
Eric Anholtc153f452007-09-03 12:06:45 +10003043 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003044 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10003045 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return 0;
3049}
3050
Eric Anholtc153f452007-09-03 12:06:45 +10003051static int radeon_cp_setparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10003054 drm_radeon_setparam_t *sp = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 struct drm_radeon_driver_file_fields *radeon_priv;
3056
Eric Anholtc153f452007-09-03 12:06:45 +10003057 switch (sp->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 case RADEON_SETPARAM_FB_LOCATION:
Eric Anholt6c340ea2007-08-25 20:23:09 +10003059 radeon_priv = file_priv->driver_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10003060 radeon_priv->radeon_fb_delta = dev_priv->fb_location -
3061 sp->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 break;
3063 case RADEON_SETPARAM_SWITCH_TILING:
Eric Anholtc153f452007-09-03 12:06:45 +10003064 if (sp->value == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003065 DRM_DEBUG("color tiling disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 dev_priv->front_pitch_offset &= ~RADEON_DST_TILE_MACRO;
3067 dev_priv->back_pitch_offset &= ~RADEON_DST_TILE_MACRO;
3068 dev_priv->sarea_priv->tiling_enabled = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10003069 } else if (sp->value == 1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003070 DRM_DEBUG("color tiling enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 dev_priv->front_pitch_offset |= RADEON_DST_TILE_MACRO;
3072 dev_priv->back_pitch_offset |= RADEON_DST_TILE_MACRO;
3073 dev_priv->sarea_priv->tiling_enabled = 1;
3074 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003075 break;
Dave Airlieea98a922005-09-11 20:28:11 +10003076 case RADEON_SETPARAM_PCIGART_LOCATION:
Eric Anholtc153f452007-09-03 12:06:45 +10003077 dev_priv->pcigart_offset = sp->value;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003078 dev_priv->pcigart_offset_set = 1;
Dave Airlieea98a922005-09-11 20:28:11 +10003079 break;
Dave Airlied5ea7022006-03-19 19:37:55 +11003080 case RADEON_SETPARAM_NEW_MEMMAP:
Eric Anholtc153f452007-09-03 12:06:45 +10003081 dev_priv->new_memmap = sp->value;
Dave Airlied5ea7022006-03-19 19:37:55 +11003082 break;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003083 case RADEON_SETPARAM_PCIGART_TABLE_SIZE:
Eric Anholtc153f452007-09-03 12:06:45 +10003084 dev_priv->gart_info.table_size = sp->value;
Dave Airlief2b04cd2007-05-08 15:19:23 +10003085 if (dev_priv->gart_info.table_size < RADEON_PCIGART_TABLE_SIZE)
3086 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
3087 break;
Dave Airlieddbee332007-07-11 12:16:01 +10003088 case RADEON_SETPARAM_VBLANK_CRTC:
Eric Anholtc153f452007-09-03 12:06:45 +10003089 return radeon_vblank_crtc_set(dev, sp->value);
Dave Airlieddbee332007-07-11 12:16:01 +10003090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 default:
Eric Anholtc153f452007-09-03 12:06:45 +10003092 DRM_DEBUG("Invalid parameter %d\n", sp->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10003093 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095
3096 return 0;
3097}
3098
3099/* When a client dies:
3100 * - Check for and clean up flipped page state
3101 * - Free any alloced GART memory.
Dave Airlied985c102006-01-02 21:32:48 +11003102 * - Free any alloced radeon surfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 *
3104 * DRM infrastructure takes care of reclaiming dma buffers.
3105 */
Eric Anholt6c340ea2007-08-25 20:23:09 +10003106void radeon_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003108 if (dev->dev_private) {
3109 drm_radeon_private_t *dev_priv = dev->dev_private;
Michel Dänzer453ff942007-05-08 15:21:14 +10003110 dev_priv->page_flipping = 0;
Eric Anholt6c340ea2007-08-25 20:23:09 +10003111 radeon_mem_release(file_priv, dev_priv->gart_heap);
3112 radeon_mem_release(file_priv, dev_priv->fb_heap);
3113 radeon_surfaces_release(file_priv, dev_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
Dave Airlie84b1fd12007-07-11 15:53:27 +10003117void radeon_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Michel Dänzer453ff942007-05-08 15:21:14 +10003119 if (dev->dev_private) {
3120 drm_radeon_private_t *dev_priv = dev->dev_private;
3121
3122 if (dev_priv->sarea_priv &&
3123 dev_priv->sarea_priv->pfCurrentPage != 0)
3124 radeon_cp_dispatch_flip(dev);
3125 }
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 radeon_do_release(dev);
3128}
3129
Eric Anholt6c340ea2007-08-25 20:23:09 +10003130int radeon_driver_open(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
3132 drm_radeon_private_t *dev_priv = dev->dev_private;
3133 struct drm_radeon_driver_file_fields *radeon_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003134
Dave Airlied985c102006-01-02 21:32:48 +11003135 DRM_DEBUG("\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003136 radeon_priv =
3137 (struct drm_radeon_driver_file_fields *)
3138 drm_alloc(sizeof(*radeon_priv), DRM_MEM_FILES);
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (!radeon_priv)
3141 return -ENOMEM;
3142
Eric Anholt6c340ea2007-08-25 20:23:09 +10003143 file_priv->driver_priv = radeon_priv;
Dave Airlied985c102006-01-02 21:32:48 +11003144
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003145 if (dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 radeon_priv->radeon_fb_delta = dev_priv->fb_location;
3147 else
3148 radeon_priv->radeon_fb_delta = 0;
3149 return 0;
3150}
3151
Eric Anholt6c340ea2007-08-25 20:23:09 +10003152void radeon_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003154 struct drm_radeon_driver_file_fields *radeon_priv =
Eric Anholt6c340ea2007-08-25 20:23:09 +10003155 file_priv->driver_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10003156
3157 drm_free(radeon_priv, sizeof(*radeon_priv), DRM_MEM_FILES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Eric Anholtc153f452007-09-03 12:06:45 +10003160struct drm_ioctl_desc radeon_ioctls[] = {
3161 DRM_IOCTL_DEF(DRM_RADEON_CP_INIT, radeon_cp_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3162 DRM_IOCTL_DEF(DRM_RADEON_CP_START, radeon_cp_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3163 DRM_IOCTL_DEF(DRM_RADEON_CP_STOP, radeon_cp_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3164 DRM_IOCTL_DEF(DRM_RADEON_CP_RESET, radeon_cp_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3165 DRM_IOCTL_DEF(DRM_RADEON_CP_IDLE, radeon_cp_idle, DRM_AUTH),
3166 DRM_IOCTL_DEF(DRM_RADEON_CP_RESUME, radeon_cp_resume, DRM_AUTH),
3167 DRM_IOCTL_DEF(DRM_RADEON_RESET, radeon_engine_reset, DRM_AUTH),
3168 DRM_IOCTL_DEF(DRM_RADEON_FULLSCREEN, radeon_fullscreen, DRM_AUTH),
3169 DRM_IOCTL_DEF(DRM_RADEON_SWAP, radeon_cp_swap, DRM_AUTH),
3170 DRM_IOCTL_DEF(DRM_RADEON_CLEAR, radeon_cp_clear, DRM_AUTH),
3171 DRM_IOCTL_DEF(DRM_RADEON_VERTEX, radeon_cp_vertex, DRM_AUTH),
3172 DRM_IOCTL_DEF(DRM_RADEON_INDICES, radeon_cp_indices, DRM_AUTH),
3173 DRM_IOCTL_DEF(DRM_RADEON_TEXTURE, radeon_cp_texture, DRM_AUTH),
3174 DRM_IOCTL_DEF(DRM_RADEON_STIPPLE, radeon_cp_stipple, DRM_AUTH),
3175 DRM_IOCTL_DEF(DRM_RADEON_INDIRECT, radeon_cp_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3176 DRM_IOCTL_DEF(DRM_RADEON_VERTEX2, radeon_cp_vertex2, DRM_AUTH),
3177 DRM_IOCTL_DEF(DRM_RADEON_CMDBUF, radeon_cp_cmdbuf, DRM_AUTH),
3178 DRM_IOCTL_DEF(DRM_RADEON_GETPARAM, radeon_cp_getparam, DRM_AUTH),
3179 DRM_IOCTL_DEF(DRM_RADEON_FLIP, radeon_cp_flip, DRM_AUTH),
3180 DRM_IOCTL_DEF(DRM_RADEON_ALLOC, radeon_mem_alloc, DRM_AUTH),
3181 DRM_IOCTL_DEF(DRM_RADEON_FREE, radeon_mem_free, DRM_AUTH),
3182 DRM_IOCTL_DEF(DRM_RADEON_INIT_HEAP, radeon_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
3183 DRM_IOCTL_DEF(DRM_RADEON_IRQ_EMIT, radeon_irq_emit, DRM_AUTH),
3184 DRM_IOCTL_DEF(DRM_RADEON_IRQ_WAIT, radeon_irq_wait, DRM_AUTH),
3185 DRM_IOCTL_DEF(DRM_RADEON_SETPARAM, radeon_cp_setparam, DRM_AUTH),
3186 DRM_IOCTL_DEF(DRM_RADEON_SURF_ALLOC, radeon_surface_alloc, DRM_AUTH),
3187 DRM_IOCTL_DEF(DRM_RADEON_SURF_FREE, radeon_surface_free, DRM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188};
3189
3190int radeon_max_ioctl = DRM_ARRAY_SIZE(radeon_ioctls);