blob: 029f63b59a7d29eda2a4d2ce71bb8d1fb1c9d142 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* r128_state.c -- State support for r128 -*- linux-c -*-
2 * Created: Thu Jan 27 02:53:43 2000 by gareth@valinux.com
Dave Airlief26c4732006-01-02 17:18:39 +11003 */
Dave Airlie83a9e292006-12-19 17:56:14 +11004/*
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Gareth Hughes <gareth@valinux.com>
29 */
30
31#include "drmP.h"
32#include "drm.h"
33#include "r128_drm.h"
34#include "r128_drv.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* ================================================================
37 * CCE hardware state programming functions
38 */
39
Dave Airlieb5e89ed2005-09-25 14:28:13 +100040static void r128_emit_clip_rects(drm_r128_private_t * dev_priv,
Dave Airlieeddca552007-07-11 16:09:54 +100041 struct drm_clip_rect * boxes, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 u32 aux_sc_cntl = 0x00000000;
44 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100045 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Dave Airlieb5e89ed2005-09-25 14:28:13 +100047 BEGIN_RING((count < 3 ? count : 3) * 5 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Dave Airlieb5e89ed2005-09-25 14:28:13 +100049 if (count >= 1) {
50 OUT_RING(CCE_PACKET0(R128_AUX1_SC_LEFT, 3));
51 OUT_RING(boxes[0].x1);
52 OUT_RING(boxes[0].x2 - 1);
53 OUT_RING(boxes[0].y1);
54 OUT_RING(boxes[0].y2 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 aux_sc_cntl |= (R128_AUX1_SC_EN | R128_AUX1_SC_MODE_OR);
57 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 if (count >= 2) {
59 OUT_RING(CCE_PACKET0(R128_AUX2_SC_LEFT, 3));
60 OUT_RING(boxes[1].x1);
61 OUT_RING(boxes[1].x2 - 1);
62 OUT_RING(boxes[1].y1);
63 OUT_RING(boxes[1].y2 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 aux_sc_cntl |= (R128_AUX2_SC_EN | R128_AUX2_SC_MODE_OR);
66 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100067 if (count >= 3) {
68 OUT_RING(CCE_PACKET0(R128_AUX3_SC_LEFT, 3));
69 OUT_RING(boxes[2].x1);
70 OUT_RING(boxes[2].x2 - 1);
71 OUT_RING(boxes[2].y1);
72 OUT_RING(boxes[2].y2 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 aux_sc_cntl |= (R128_AUX3_SC_EN | R128_AUX3_SC_MODE_OR);
75 }
76
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 OUT_RING(CCE_PACKET0(R128_AUX_SC_CNTL, 0));
78 OUT_RING(aux_sc_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 ADVANCE_RING();
81}
82
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083static __inline__ void r128_emit_core(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
86 drm_r128_context_regs_t *ctx = &sarea_priv->context_state;
87 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100088 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 OUT_RING(CCE_PACKET0(R128_SCALE_3D_CNTL, 0));
93 OUT_RING(ctx->scale_3d_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 ADVANCE_RING();
96}
97
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098static __inline__ void r128_emit_context(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
101 drm_r128_context_regs_t *ctx = &sarea_priv->context_state;
102 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 BEGIN_RING(13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000107 OUT_RING(CCE_PACKET0(R128_DST_PITCH_OFFSET_C, 11));
108 OUT_RING(ctx->dst_pitch_offset_c);
109 OUT_RING(ctx->dp_gui_master_cntl_c);
110 OUT_RING(ctx->sc_top_left_c);
111 OUT_RING(ctx->sc_bottom_right_c);
112 OUT_RING(ctx->z_offset_c);
113 OUT_RING(ctx->z_pitch_c);
114 OUT_RING(ctx->z_sten_cntl_c);
115 OUT_RING(ctx->tex_cntl_c);
116 OUT_RING(ctx->misc_3d_state_cntl_reg);
117 OUT_RING(ctx->texture_clr_cmp_clr_c);
118 OUT_RING(ctx->texture_clr_cmp_msk_c);
119 OUT_RING(ctx->fog_color_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 ADVANCE_RING();
122}
123
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000124static __inline__ void r128_emit_setup(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
127 drm_r128_context_regs_t *ctx = &sarea_priv->context_state;
128 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 BEGIN_RING(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 OUT_RING(CCE_PACKET1(R128_SETUP_CNTL, R128_PM4_VC_FPU_SETUP));
134 OUT_RING(ctx->setup_cntl);
135 OUT_RING(ctx->pm4_vc_fpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 ADVANCE_RING();
138}
139
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140static __inline__ void r128_emit_masks(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
143 drm_r128_context_regs_t *ctx = &sarea_priv->context_state;
144 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000145 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 BEGIN_RING(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 OUT_RING(CCE_PACKET0(R128_DP_WRITE_MASK, 0));
150 OUT_RING(ctx->dp_write_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 OUT_RING(CCE_PACKET0(R128_STEN_REF_MASK_C, 1));
153 OUT_RING(ctx->sten_ref_mask_c);
154 OUT_RING(ctx->plane_3d_mask_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 ADVANCE_RING();
157}
158
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159static __inline__ void r128_emit_window(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
162 drm_r128_context_regs_t *ctx = &sarea_priv->context_state;
163 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 OUT_RING(CCE_PACKET0(R128_WINDOW_XY_OFFSET, 0));
169 OUT_RING(ctx->window_xy_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 ADVANCE_RING();
172}
173
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174static __inline__ void r128_emit_tex0(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
177 drm_r128_context_regs_t *ctx = &sarea_priv->context_state;
178 drm_r128_texture_regs_t *tex = &sarea_priv->tex_state[0];
179 int i;
180 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183 BEGIN_RING(7 + R128_MAX_TEXTURE_LEVELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 OUT_RING(CCE_PACKET0(R128_PRIM_TEX_CNTL_C,
186 2 + R128_MAX_TEXTURE_LEVELS));
187 OUT_RING(tex->tex_cntl);
188 OUT_RING(tex->tex_combine_cntl);
189 OUT_RING(ctx->tex_size_pitch_c);
190 for (i = 0; i < R128_MAX_TEXTURE_LEVELS; i++) {
191 OUT_RING(tex->tex_offset[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 OUT_RING(CCE_PACKET0(R128_CONSTANT_COLOR_C, 1));
195 OUT_RING(ctx->constant_color_c);
196 OUT_RING(tex->tex_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 ADVANCE_RING();
199}
200
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201static __inline__ void r128_emit_tex1(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
204 drm_r128_texture_regs_t *tex = &sarea_priv->tex_state[1];
205 int i;
206 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 DRM_DEBUG(" %s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 BEGIN_RING(5 + R128_MAX_TEXTURE_LEVELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 OUT_RING(CCE_PACKET0(R128_SEC_TEX_CNTL_C, 1 + R128_MAX_TEXTURE_LEVELS));
212 OUT_RING(tex->tex_cntl);
213 OUT_RING(tex->tex_combine_cntl);
214 for (i = 0; i < R128_MAX_TEXTURE_LEVELS; i++) {
215 OUT_RING(tex->tex_offset[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 OUT_RING(CCE_PACKET0(R128_SEC_TEXTURE_BORDER_COLOR_C, 0));
219 OUT_RING(tex->tex_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 ADVANCE_RING();
222}
223
Arjan van de Ven858119e2006-01-14 13:20:43 -0800224static void r128_emit_state(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
227 unsigned int dirty = sarea_priv->dirty;
228
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 DRM_DEBUG("%s: dirty=0x%08x\n", __FUNCTION__, dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 if (dirty & R128_UPLOAD_CORE) {
232 r128_emit_core(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 sarea_priv->dirty &= ~R128_UPLOAD_CORE;
234 }
235
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 if (dirty & R128_UPLOAD_CONTEXT) {
237 r128_emit_context(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 sarea_priv->dirty &= ~R128_UPLOAD_CONTEXT;
239 }
240
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (dirty & R128_UPLOAD_SETUP) {
242 r128_emit_setup(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 sarea_priv->dirty &= ~R128_UPLOAD_SETUP;
244 }
245
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 if (dirty & R128_UPLOAD_MASKS) {
247 r128_emit_masks(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 sarea_priv->dirty &= ~R128_UPLOAD_MASKS;
249 }
250
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 if (dirty & R128_UPLOAD_WINDOW) {
252 r128_emit_window(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 sarea_priv->dirty &= ~R128_UPLOAD_WINDOW;
254 }
255
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 if (dirty & R128_UPLOAD_TEX0) {
257 r128_emit_tex0(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sarea_priv->dirty &= ~R128_UPLOAD_TEX0;
259 }
260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 if (dirty & R128_UPLOAD_TEX1) {
262 r128_emit_tex1(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 sarea_priv->dirty &= ~R128_UPLOAD_TEX1;
264 }
265
266 /* Turn off the texture cache flushing */
267 sarea_priv->context_state.tex_cntl_c &= ~R128_TEX_CACHE_FLUSH;
268
269 sarea_priv->dirty &= ~R128_REQUIRE_QUIESCENCE;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#if R128_PERFORMANCE_BOXES
273/* ================================================================
274 * Performance monitoring functions
275 */
276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277static void r128_clear_box(drm_r128_private_t * dev_priv,
278 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 u32 pitch, offset;
281 u32 fb_bpp, color;
282 RING_LOCALS;
283
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 switch (dev_priv->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 case 16:
286 fb_bpp = R128_GMC_DST_16BPP;
287 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 break;
290 case 24:
291 fb_bpp = R128_GMC_DST_24BPP;
292 color = ((r << 16) | (g << 8) | b);
293 break;
294 case 32:
295 fb_bpp = R128_GMC_DST_32BPP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
298 default:
299 return;
300 }
301
302 offset = dev_priv->back_offset;
303 pitch = dev_priv->back_pitch >> 3;
304
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000307 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
308 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
309 R128_GMC_BRUSH_SOLID_COLOR |
310 fb_bpp |
311 R128_GMC_SRC_DATATYPE_COLOR |
312 R128_ROP3_P |
313 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_AUX_CLIP_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315 OUT_RING((pitch << 21) | (offset >> 5));
316 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 OUT_RING((x << 16) | y);
319 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 ADVANCE_RING();
322}
323
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324static void r128_cce_performance_boxes(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 if (atomic_read(&dev_priv->idle_count) == 0) {
327 r128_clear_box(dev_priv, 64, 4, 8, 8, 0, 255, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 atomic_set(&dev_priv->idle_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331}
332
333#endif
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* ================================================================
336 * CCE command dispatch functions
337 */
338
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339static void r128_print_dirty(const char *msg, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 DRM_INFO("%s: (0x%x) %s%s%s%s%s%s%s%s%s\n",
342 msg,
343 flags,
344 (flags & R128_UPLOAD_CORE) ? "core, " : "",
345 (flags & R128_UPLOAD_CONTEXT) ? "context, " : "",
346 (flags & R128_UPLOAD_SETUP) ? "setup, " : "",
347 (flags & R128_UPLOAD_TEX0) ? "tex0, " : "",
348 (flags & R128_UPLOAD_TEX1) ? "tex1, " : "",
349 (flags & R128_UPLOAD_MASKS) ? "masks, " : "",
350 (flags & R128_UPLOAD_WINDOW) ? "window, " : "",
351 (flags & R128_UPLOAD_CLIPRECTS) ? "cliprects, " : "",
352 (flags & R128_REQUIRE_QUIESCENCE) ? "quiescence, " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Dave Airlieeddca552007-07-11 16:09:54 +1000355static void r128_cce_dispatch_clear(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356 drm_r128_clear_t * clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 drm_r128_private_t *dev_priv = dev->dev_private;
359 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
360 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000361 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned int flags = clear->flags;
363 int i;
364 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000367 if (dev_priv->page_flipping && dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unsigned int tmp = flags;
369
370 flags &= ~(R128_FRONT | R128_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 if (tmp & R128_FRONT)
372 flags |= R128_BACK;
373 if (tmp & R128_BACK)
374 flags |= R128_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000377 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int x = pbox[i].x1;
379 int y = pbox[i].y1;
380 int w = pbox[i].x2 - x;
381 int h = pbox[i].y2 - y;
382
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 DRM_DEBUG("dispatch clear %d,%d-%d,%d flags 0x%x\n",
384 pbox[i].x1, pbox[i].y1, pbox[i].x2,
385 pbox[i].y2, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 if (flags & (R128_FRONT | R128_BACK)) {
388 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000390 OUT_RING(CCE_PACKET0(R128_DP_WRITE_MASK, 0));
391 OUT_RING(clear->color_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 ADVANCE_RING();
394 }
395
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 if (flags & R128_FRONT) {
397 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000399 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
400 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
401 R128_GMC_BRUSH_SOLID_COLOR |
402 (dev_priv->color_fmt << 8) |
403 R128_GMC_SRC_DATATYPE_COLOR |
404 R128_ROP3_P |
405 R128_GMC_CLR_CMP_CNTL_DIS |
406 R128_GMC_AUX_CLIP_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 OUT_RING(dev_priv->front_pitch_offset_c);
409 OUT_RING(clear->clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 OUT_RING((x << 16) | y);
412 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 ADVANCE_RING();
415 }
416
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 if (flags & R128_BACK) {
418 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
421 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
422 R128_GMC_BRUSH_SOLID_COLOR |
423 (dev_priv->color_fmt << 8) |
424 R128_GMC_SRC_DATATYPE_COLOR |
425 R128_ROP3_P |
426 R128_GMC_CLR_CMP_CNTL_DIS |
427 R128_GMC_AUX_CLIP_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 OUT_RING(dev_priv->back_pitch_offset_c);
430 OUT_RING(clear->clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 OUT_RING((x << 16) | y);
433 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 ADVANCE_RING();
436 }
437
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 if (flags & R128_DEPTH) {
439 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
442 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
443 R128_GMC_BRUSH_SOLID_COLOR |
444 (dev_priv->depth_fmt << 8) |
445 R128_GMC_SRC_DATATYPE_COLOR |
446 R128_ROP3_P |
447 R128_GMC_CLR_CMP_CNTL_DIS |
448 R128_GMC_AUX_CLIP_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000450 OUT_RING(dev_priv->depth_pitch_offset_c);
451 OUT_RING(clear->clear_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000453 OUT_RING((x << 16) | y);
454 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 ADVANCE_RING();
457 }
458 }
459}
460
Dave Airlieeddca552007-07-11 16:09:54 +1000461static void r128_cce_dispatch_swap(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 drm_r128_private_t *dev_priv = dev->dev_private;
464 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
465 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000466 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int i;
468 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000469 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471#if R128_PERFORMANCE_BOXES
472 /* Do some trivial performance monitoring...
473 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 r128_cce_performance_boxes(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475#endif
476
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int x = pbox[i].x1;
479 int y = pbox[i].y1;
480 int w = pbox[i].x2 - x;
481 int h = pbox[i].y2 - y;
482
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
486 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
487 R128_GMC_DST_PITCH_OFFSET_CNTL |
488 R128_GMC_BRUSH_NONE |
489 (dev_priv->color_fmt << 8) |
490 R128_GMC_SRC_DATATYPE_COLOR |
491 R128_ROP3_S |
492 R128_DP_SRC_SOURCE_MEMORY |
493 R128_GMC_CLR_CMP_CNTL_DIS |
494 R128_GMC_AUX_CLIP_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* Make this work even if front & back are flipped:
497 */
498 if (dev_priv->current_page == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 OUT_RING(dev_priv->back_pitch_offset_c);
500 OUT_RING(dev_priv->front_pitch_offset_c);
501 } else {
502 OUT_RING(dev_priv->front_pitch_offset_c);
503 OUT_RING(dev_priv->back_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 OUT_RING((x << 16) | y);
507 OUT_RING((x << 16) | y);
508 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 ADVANCE_RING();
511 }
512
513 /* Increment the frame counter. The client-side 3D driver must
514 * throttle the framerate by waiting for this value before
515 * performing the swapbuffer ioctl.
516 */
517 dev_priv->sarea_priv->last_frame++;
518
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000521 OUT_RING(CCE_PACKET0(R128_LAST_FRAME_REG, 0));
522 OUT_RING(dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 ADVANCE_RING();
525}
526
Dave Airlieeddca552007-07-11 16:09:54 +1000527static void r128_cce_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 drm_r128_private_t *dev_priv = dev->dev_private;
530 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
532 __FUNCTION__,
533 dev_priv->current_page, dev_priv->sarea_priv->pfCurrentPage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535#if R128_PERFORMANCE_BOXES
536 /* Do some trivial performance monitoring...
537 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 r128_cce_performance_boxes(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif
540
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000541 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 R128_WAIT_UNTIL_PAGE_FLIPPED();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 OUT_RING(CCE_PACKET0(R128_CRTC_OFFSET, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 if (dev_priv->current_page == 0) {
547 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
552 ADVANCE_RING();
553
554 /* Increment the frame counter. The client-side 3D driver must
555 * throttle the framerate by waiting for this value before
556 * performing the swapbuffer ioctl.
557 */
558 dev_priv->sarea_priv->last_frame++;
559 dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 1 - dev_priv->current_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000562 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564 OUT_RING(CCE_PACKET0(R128_LAST_FRAME_REG, 0));
565 OUT_RING(dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 ADVANCE_RING();
568}
569
Dave Airlie056219e2007-07-11 16:17:42 +1000570static void r128_cce_dispatch_vertex(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 drm_r128_private_t *dev_priv = dev->dev_private;
573 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
574 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
575 int format = sarea_priv->vc_format;
576 int offset = buf->bus_address;
577 int size = buf->used;
578 int prim = buf_priv->prim;
579 int i = 0;
580 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000581 DRM_DEBUG("buf=%d nbox=%d\n", buf->idx, sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 if (0)
584 r128_print_dirty("dispatch_vertex", sarea_priv->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 if (buf->used) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 buf_priv->dispatched = 1;
588
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 if (sarea_priv->dirty & ~R128_UPLOAD_CLIPRECTS) {
590 r128_emit_state(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 do {
594 /* Emit the next set of up to three cliprects */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 if (i < sarea_priv->nbox) {
596 r128_emit_clip_rects(dev_priv,
597 &sarea_priv->boxes[i],
598 sarea_priv->nbox - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
601 /* Emit the vertex buffer rendering commands */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 BEGIN_RING(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 OUT_RING(CCE_PACKET3(R128_3D_RNDR_GEN_INDX_PRIM, 3));
605 OUT_RING(offset);
606 OUT_RING(size);
607 OUT_RING(format);
608 OUT_RING(prim | R128_CCE_VC_CNTL_PRIM_WALK_LIST |
609 (size << R128_CCE_VC_CNTL_NUM_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 ADVANCE_RING();
612
613 i += 3;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 } while (i < sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
619
620 /* Emit the vertex buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
624 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 ADVANCE_RING();
627
628 buf->pending = 1;
629 buf->used = 0;
630 /* FIXME: Check dispatched field */
631 buf_priv->dispatched = 0;
632 }
633
634 dev_priv->sarea_priv->last_dispatch++;
635
636 sarea_priv->dirty &= ~R128_UPLOAD_CLIPRECTS;
637 sarea_priv->nbox = 0;
638}
639
Dave Airlieeddca552007-07-11 16:09:54 +1000640static void r128_cce_dispatch_indirect(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000641 struct drm_buf * buf, int start, int end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 drm_r128_private_t *dev_priv = dev->dev_private;
644 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
645 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 DRM_DEBUG("indirect: buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int offset = buf->bus_address + start;
650 int dwords = (end - start + 3) / sizeof(u32);
651
652 /* Indirect buffer data must be an even number of
653 * dwords, so if we've been given an odd number we must
654 * pad the data with a Type-2 CCE packet.
655 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 if (dwords & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 u32 *data = (u32 *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 ((char *)dev->agp_buffer_map->handle
659 + buf->offset + start);
660 data[dwords++] = cpu_to_le32(R128_CCE_PACKET2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 buf_priv->dispatched = 1;
664
665 /* Fire off the indirect buffer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 BEGIN_RING(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668 OUT_RING(CCE_PACKET0(R128_PM4_IW_INDOFF, 1));
669 OUT_RING(offset);
670 OUT_RING(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 ADVANCE_RING();
673 }
674
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
677
678 /* Emit the indirect buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000679 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
682 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 ADVANCE_RING();
685
686 buf->pending = 1;
687 buf->used = 0;
688 /* FIXME: Check dispatched field */
689 buf_priv->dispatched = 0;
690 }
691
692 dev_priv->sarea_priv->last_dispatch++;
693}
694
Dave Airlieeddca552007-07-11 16:09:54 +1000695static void r128_cce_dispatch_indices(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000696 struct drm_buf * buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 int start, int end, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 drm_r128_private_t *dev_priv = dev->dev_private;
700 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
701 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
702 int format = sarea_priv->vc_format;
703 int offset = dev->agp_buffer_map->offset - dev_priv->cce_buffers_offset;
704 int prim = buf_priv->prim;
705 u32 *data;
706 int dwords;
707 int i = 0;
708 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 DRM_DEBUG("indices: s=%d e=%d c=%d\n", start, end, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 if (0)
712 r128_print_dirty("dispatch_indices", sarea_priv->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 buf_priv->dispatched = 1;
716
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 if (sarea_priv->dirty & ~R128_UPLOAD_CLIPRECTS) {
718 r128_emit_state(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
721 dwords = (end - start + 3) / sizeof(u32);
722
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 data = (u32 *) ((char *)dev->agp_buffer_map->handle
724 + buf->offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 data[0] = cpu_to_le32(CCE_PACKET3(R128_3D_RNDR_GEN_INDX_PRIM,
727 dwords - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 data[1] = cpu_to_le32(offset);
730 data[2] = cpu_to_le32(R128_MAX_VB_VERTS);
731 data[3] = cpu_to_le32(format);
732 data[4] = cpu_to_le32((prim | R128_CCE_VC_CNTL_PRIM_WALK_IND |
733 (count << 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735 if (count & 0x1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#ifdef __LITTLE_ENDIAN
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 data[dwords - 1] &= 0x0000ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 data[dwords - 1] &= 0xffff0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#endif
741 }
742
743 do {
744 /* Emit the next set of up to three cliprects */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 if (i < sarea_priv->nbox) {
746 r128_emit_clip_rects(dev_priv,
747 &sarea_priv->boxes[i],
748 sarea_priv->nbox - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 r128_cce_dispatch_indirect(dev, buf, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 i += 3;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000754 } while (i < sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
759
760 /* Emit the vertex buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
764 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 ADVANCE_RING();
767
768 buf->pending = 1;
769 /* FIXME: Check dispatched field */
770 buf_priv->dispatched = 0;
771 }
772
773 dev_priv->sarea_priv->last_dispatch++;
774
775 sarea_priv->dirty &= ~R128_UPLOAD_CLIPRECTS;
776 sarea_priv->nbox = 0;
777}
778
Eric Anholt6c340ea2007-08-25 20:23:09 +1000779static int r128_cce_dispatch_blit(struct drm_device * dev,
780 struct drm_file *file_priv,
781 drm_r128_blit_t * blit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000784 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000785 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 drm_r128_buf_priv_t *buf_priv;
787 u32 *data;
788 int dword_shift, dwords;
789 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* The compiler won't optimize away a division by a variable,
793 * even if the only legal values are powers of two. Thus, we'll
794 * use a shift instead.
795 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 switch (blit->format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case R128_DATATYPE_ARGB8888:
798 dword_shift = 0;
799 break;
800 case R128_DATATYPE_ARGB1555:
801 case R128_DATATYPE_RGB565:
802 case R128_DATATYPE_ARGB4444:
803 case R128_DATATYPE_YVYU422:
804 case R128_DATATYPE_VYUY422:
805 dword_shift = 1;
806 break;
807 case R128_DATATYPE_CI8:
808 case R128_DATATYPE_RGB8:
809 dword_shift = 2;
810 break;
811 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 DRM_ERROR("invalid blit format %d\n", blit->format);
Eric Anholt20caafa2007-08-25 19:22:43 +1000813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 /* Flush the pixel cache, and mark the contents as Read Invalid.
817 * This ensures no pixel data gets mixed up with the texture
818 * data from the host data blit, otherwise part of the texture
819 * image may be corrupted.
820 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 OUT_RING(CCE_PACKET0(R128_PC_GUI_CTLSTAT, 0));
824 OUT_RING(R128_PC_RI_GUI | R128_PC_FLUSH_GUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 ADVANCE_RING();
827
828 /* Dispatch the indirect buffer.
829 */
830 buf = dma->buflist[blit->idx];
831 buf_priv = buf->dev_private;
832
Eric Anholt6c340ea2007-08-25 20:23:09 +1000833 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +1000835 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +1000836 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 if (buf->pending) {
839 DRM_ERROR("sending pending buffer %d\n", blit->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +1000840 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
843 buf_priv->discard = 1;
844
845 dwords = (blit->width * blit->height) >> dword_shift;
846
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 data = (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 data[0] = cpu_to_le32(CCE_PACKET3(R128_CNTL_HOSTDATA_BLT, dwords + 6));
850 data[1] = cpu_to_le32((R128_GMC_DST_PITCH_OFFSET_CNTL |
851 R128_GMC_BRUSH_NONE |
852 (blit->format << 8) |
853 R128_GMC_SRC_DATATYPE_COLOR |
854 R128_ROP3_S |
855 R128_DP_SRC_SOURCE_HOST_DATA |
856 R128_GMC_CLR_CMP_CNTL_DIS |
857 R128_GMC_AUX_CLIP_DIS | R128_GMC_WR_MSK_DIS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 data[2] = cpu_to_le32((blit->pitch << 21) | (blit->offset >> 5));
860 data[3] = cpu_to_le32(0xffffffff);
861 data[4] = cpu_to_le32(0xffffffff);
862 data[5] = cpu_to_le32((blit->y << 16) | blit->x);
863 data[6] = cpu_to_le32((blit->height << 16) | blit->width);
864 data[7] = cpu_to_le32(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 buf->used = (dwords + 8) * sizeof(u32);
867
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 r128_cce_dispatch_indirect(dev, buf, 0, buf->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /* Flush the pixel cache after the blit completes. This ensures
871 * the texture data is written out to memory before rendering
872 * continues.
873 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 OUT_RING(CCE_PACKET0(R128_PC_GUI_CTLSTAT, 0));
877 OUT_RING(R128_PC_FLUSH_GUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 ADVANCE_RING();
880
881 return 0;
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/* ================================================================
885 * Tiled depth buffer management
886 *
887 * FIXME: These should all set the destination write mask for when we
888 * have hardware stencil support.
889 */
890
Dave Airlieeddca552007-07-11 16:09:54 +1000891static int r128_cce_dispatch_write_span(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 drm_r128_private_t *dev_priv = dev->dev_private;
895 int count, x, y;
896 u32 *buffer;
897 u8 *mask;
898 int i, buffer_size, mask_size;
899 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 count = depth->n;
903 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +1000904 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 if (DRM_COPY_FROM_USER(&x, depth->x, sizeof(x))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000907 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 if (DRM_COPY_FROM_USER(&y, depth->y, sizeof(y))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000910 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
913 buffer_size = depth->n * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 buffer = drm_alloc(buffer_size, DRM_MEM_BUFS);
915 if (buffer == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000916 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 if (DRM_COPY_FROM_USER(buffer, depth->buffer, buffer_size)) {
918 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +1000919 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
922 mask_size = depth->n * sizeof(u8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 if (depth->mask) {
924 mask = drm_alloc(mask_size, DRM_MEM_BUFS);
925 if (mask == NULL) {
926 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +1000927 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 if (DRM_COPY_FROM_USER(mask, depth->mask, mask_size)) {
930 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
931 drm_free(mask, mask_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +1000932 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 for (i = 0; i < count; i++, x++) {
936 if (mask[i]) {
937 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
940 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
941 R128_GMC_BRUSH_SOLID_COLOR |
942 (dev_priv->depth_fmt << 8) |
943 R128_GMC_SRC_DATATYPE_COLOR |
944 R128_ROP3_P |
945 R128_GMC_CLR_CMP_CNTL_DIS |
946 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 OUT_RING(dev_priv->depth_pitch_offset_c);
949 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 OUT_RING((x << 16) | y);
952 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 ADVANCE_RING();
955 }
956 }
957
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 drm_free(mask, mask_size, DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960 for (i = 0; i < count; i++, x++) {
961 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
964 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
965 R128_GMC_BRUSH_SOLID_COLOR |
966 (dev_priv->depth_fmt << 8) |
967 R128_GMC_SRC_DATATYPE_COLOR |
968 R128_ROP3_P |
969 R128_GMC_CLR_CMP_CNTL_DIS |
970 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 OUT_RING(dev_priv->depth_pitch_offset_c);
973 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000975 OUT_RING((x << 16) | y);
976 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 ADVANCE_RING();
979 }
980 }
981
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000982 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 return 0;
985}
986
Dave Airlieeddca552007-07-11 16:09:54 +1000987static int r128_cce_dispatch_write_pixels(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000988 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 drm_r128_private_t *dev_priv = dev->dev_private;
991 int count, *x, *y;
992 u32 *buffer;
993 u8 *mask;
994 int i, xbuf_size, ybuf_size, buffer_size, mask_size;
995 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000996 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 count = depth->n;
999 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +10001000 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 xbuf_size = count * sizeof(*x);
1003 ybuf_size = count * sizeof(*y);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 x = drm_alloc(xbuf_size, DRM_MEM_BUFS);
1005 if (x == NULL) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001006 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 y = drm_alloc(ybuf_size, DRM_MEM_BUFS);
1009 if (y == NULL) {
1010 drm_free(x, xbuf_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001011 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 if (DRM_COPY_FROM_USER(x, depth->x, xbuf_size)) {
1014 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1015 drm_free(y, ybuf_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001016 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 if (DRM_COPY_FROM_USER(y, depth->y, xbuf_size)) {
1019 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1020 drm_free(y, ybuf_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001021 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
1024 buffer_size = depth->n * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 buffer = drm_alloc(buffer_size, DRM_MEM_BUFS);
1026 if (buffer == NULL) {
1027 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1028 drm_free(y, ybuf_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001029 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 if (DRM_COPY_FROM_USER(buffer, depth->buffer, buffer_size)) {
1032 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1033 drm_free(y, ybuf_size, DRM_MEM_BUFS);
1034 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001035 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 if (depth->mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 mask_size = depth->n * sizeof(u8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 mask = drm_alloc(mask_size, DRM_MEM_BUFS);
1041 if (mask == NULL) {
1042 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1043 drm_free(y, ybuf_size, DRM_MEM_BUFS);
1044 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001045 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 if (DRM_COPY_FROM_USER(mask, depth->mask, mask_size)) {
1048 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1049 drm_free(y, ybuf_size, DRM_MEM_BUFS);
1050 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
1051 drm_free(mask, mask_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001052 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 for (i = 0; i < count; i++) {
1056 if (mask[i]) {
1057 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
1060 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
1061 R128_GMC_BRUSH_SOLID_COLOR |
1062 (dev_priv->depth_fmt << 8) |
1063 R128_GMC_SRC_DATATYPE_COLOR |
1064 R128_ROP3_P |
1065 R128_GMC_CLR_CMP_CNTL_DIS |
1066 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 OUT_RING(dev_priv->depth_pitch_offset_c);
1069 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 OUT_RING((x[i] << 16) | y[i]);
1072 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 ADVANCE_RING();
1075 }
1076 }
1077
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 drm_free(mask, mask_size, DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 for (i = 0; i < count; i++) {
1081 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
1084 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
1085 R128_GMC_BRUSH_SOLID_COLOR |
1086 (dev_priv->depth_fmt << 8) |
1087 R128_GMC_SRC_DATATYPE_COLOR |
1088 R128_ROP3_P |
1089 R128_GMC_CLR_CMP_CNTL_DIS |
1090 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 OUT_RING(dev_priv->depth_pitch_offset_c);
1093 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 OUT_RING((x[i] << 16) | y[i]);
1096 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 ADVANCE_RING();
1099 }
1100 }
1101
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1103 drm_free(y, ybuf_size, DRM_MEM_BUFS);
1104 drm_free(buffer, buffer_size, DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 return 0;
1107}
1108
Dave Airlieeddca552007-07-11 16:09:54 +10001109static int r128_cce_dispatch_read_span(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 drm_r128_private_t *dev_priv = dev->dev_private;
1113 int count, x, y;
1114 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 count = depth->n;
1118 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +10001119 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 if (DRM_COPY_FROM_USER(&x, depth->x, sizeof(x))) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001122 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 if (DRM_COPY_FROM_USER(&y, depth->y, sizeof(y))) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001125 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
1131 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
1132 R128_GMC_DST_PITCH_OFFSET_CNTL |
1133 R128_GMC_BRUSH_NONE |
1134 (dev_priv->depth_fmt << 8) |
1135 R128_GMC_SRC_DATATYPE_COLOR |
1136 R128_ROP3_S |
1137 R128_DP_SRC_SOURCE_MEMORY |
1138 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140 OUT_RING(dev_priv->depth_pitch_offset_c);
1141 OUT_RING(dev_priv->span_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001143 OUT_RING((x << 16) | y);
1144 OUT_RING((0 << 16) | 0);
1145 OUT_RING((count << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 ADVANCE_RING();
1148
1149 return 0;
1150}
1151
Dave Airlieeddca552007-07-11 16:09:54 +10001152static int r128_cce_dispatch_read_pixels(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001153 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 drm_r128_private_t *dev_priv = dev->dev_private;
1156 int count, *x, *y;
1157 int i, xbuf_size, ybuf_size;
1158 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 count = depth->n;
1162 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +10001163 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001165 if (count > dev_priv->depth_pitch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 count = dev_priv->depth_pitch;
1167 }
1168
1169 xbuf_size = count * sizeof(*x);
1170 ybuf_size = count * sizeof(*y);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 x = drm_alloc(xbuf_size, DRM_MEM_BUFS);
1172 if (x == NULL) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001173 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175 y = drm_alloc(ybuf_size, DRM_MEM_BUFS);
1176 if (y == NULL) {
1177 drm_free(x, xbuf_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001178 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 if (DRM_COPY_FROM_USER(x, depth->x, xbuf_size)) {
1181 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1182 drm_free(y, ybuf_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001183 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001185 if (DRM_COPY_FROM_USER(y, depth->y, ybuf_size)) {
1186 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1187 drm_free(y, ybuf_size, DRM_MEM_BUFS);
Eric Anholt20caafa2007-08-25 19:22:43 +10001188 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001191 for (i = 0; i < count; i++) {
1192 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001194 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
1195 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
1196 R128_GMC_DST_PITCH_OFFSET_CNTL |
1197 R128_GMC_BRUSH_NONE |
1198 (dev_priv->depth_fmt << 8) |
1199 R128_GMC_SRC_DATATYPE_COLOR |
1200 R128_ROP3_S |
1201 R128_DP_SRC_SOURCE_MEMORY |
1202 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001204 OUT_RING(dev_priv->depth_pitch_offset_c);
1205 OUT_RING(dev_priv->span_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001207 OUT_RING((x[i] << 16) | y[i]);
1208 OUT_RING((i << 16) | 0);
1209 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 ADVANCE_RING();
1212 }
1213
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001214 drm_free(x, xbuf_size, DRM_MEM_BUFS);
1215 drm_free(y, ybuf_size, DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 return 0;
1218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/* ================================================================
1221 * Polygon stipple
1222 */
1223
Dave Airlieeddca552007-07-11 16:09:54 +10001224static void r128_cce_dispatch_stipple(struct drm_device * dev, u32 * stipple)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 drm_r128_private_t *dev_priv = dev->dev_private;
1227 int i;
1228 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001229 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001231 BEGIN_RING(33);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001233 OUT_RING(CCE_PACKET0(R128_BRUSH_DATA0, 31));
1234 for (i = 0; i < 32; i++) {
1235 OUT_RING(stipple[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
1238 ADVANCE_RING();
1239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/* ================================================================
1242 * IOCTL functions
1243 */
1244
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001245static int r128_cce_clear(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 DRM_DEVICE;
1248 drm_r128_private_t *dev_priv = dev->dev_private;
1249 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
1250 drm_r128_clear_t clear;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Eric Anholt6c340ea2007-08-25 20:23:09 +10001253 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001255 DRM_COPY_FROM_USER_IOCTL(clear, (drm_r128_clear_t __user *) data,
1256 sizeof(clear));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001258 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001260 if (sarea_priv->nbox > R128_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sarea_priv->nbox = R128_NR_SAREA_CLIPRECTS;
1262
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001263 r128_cce_dispatch_clear(dev, &clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 COMMIT_RING();
1265
1266 /* Make sure we restore the 3D state next time.
1267 */
1268 dev_priv->sarea_priv->dirty |= R128_UPLOAD_CONTEXT | R128_UPLOAD_MASKS;
1269
1270 return 0;
1271}
1272
Dave Airlieeddca552007-07-11 16:09:54 +10001273static int r128_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001276 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001278 dev_priv->crtc_offset = R128_READ(R128_CRTC_OFFSET);
1279 dev_priv->crtc_offset_cntl = R128_READ(R128_CRTC_OFFSET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001281 R128_WRITE(R128_CRTC_OFFSET, dev_priv->front_offset);
1282 R128_WRITE(R128_CRTC_OFFSET_CNTL,
1283 dev_priv->crtc_offset_cntl | R128_CRTC_OFFSET_FLIP_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 dev_priv->page_flipping = 1;
1286 dev_priv->current_page = 0;
1287 dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page;
1288
1289 return 0;
1290}
1291
Dave Airlieeddca552007-07-11 16:09:54 +10001292static int r128_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001295 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001297 R128_WRITE(R128_CRTC_OFFSET, dev_priv->crtc_offset);
1298 R128_WRITE(R128_CRTC_OFFSET_CNTL, dev_priv->crtc_offset_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 if (dev_priv->current_page != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301 r128_cce_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 COMMIT_RING();
1303 }
1304
1305 dev_priv->page_flipping = 0;
1306 return 0;
1307}
1308
1309/* Swapping and flipping are different operations, need different ioctls.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001310 * They can & should be intermixed to support multiple 3d windows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
1312
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001313static int r128_cce_flip(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 DRM_DEVICE;
1316 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001317 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Eric Anholt6c340ea2007-08-25 20:23:09 +10001319 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001321 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001323 if (!dev_priv->page_flipping)
1324 r128_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001326 r128_cce_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 COMMIT_RING();
1329 return 0;
1330}
1331
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001332static int r128_cce_swap(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 DRM_DEVICE;
1335 drm_r128_private_t *dev_priv = dev->dev_private;
1336 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001337 DRM_DEBUG("%s\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Eric Anholt6c340ea2007-08-25 20:23:09 +10001339 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001341 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001343 if (sarea_priv->nbox > R128_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 sarea_priv->nbox = R128_NR_SAREA_CLIPRECTS;
1345
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001346 r128_cce_dispatch_swap(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 dev_priv->sarea_priv->dirty |= (R128_UPLOAD_CONTEXT |
1348 R128_UPLOAD_MASKS);
1349
1350 COMMIT_RING();
1351 return 0;
1352}
1353
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354static int r128_cce_vertex(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 DRM_DEVICE;
1357 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001358 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001359 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 drm_r128_buf_priv_t *buf_priv;
1361 drm_r128_vertex_t vertex;
1362
Eric Anholt6c340ea2007-08-25 20:23:09 +10001363 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 if (!dev_priv) {
1366 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Eric Anholt20caafa2007-08-25 19:22:43 +10001367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001370 DRM_COPY_FROM_USER_IOCTL(vertex, (drm_r128_vertex_t __user *) data,
1371 sizeof(vertex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001373 DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
1374 DRM_CURRENTPID, vertex.idx, vertex.count, vertex.discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001376 if (vertex.idx < 0 || vertex.idx >= dma->buf_count) {
1377 DRM_ERROR("buffer index %d (of %d max)\n",
1378 vertex.idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 if (vertex.prim < 0 ||
1382 vertex.prim > R128_CCE_VC_CNTL_PRIM_TYPE_TRI_TYPE2) {
1383 DRM_ERROR("buffer prim %d\n", vertex.prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10001384 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1388 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 buf = dma->buflist[vertex.idx];
1391 buf_priv = buf->dev_private;
1392
Eric Anholt6c340ea2007-08-25 20:23:09 +10001393 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001395 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001396 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001398 if (buf->pending) {
1399 DRM_ERROR("sending pending buffer %d\n", vertex.idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 buf->used = vertex.count;
1404 buf_priv->prim = vertex.prim;
1405 buf_priv->discard = vertex.discard;
1406
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001407 r128_cce_dispatch_vertex(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 COMMIT_RING();
1410 return 0;
1411}
1412
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001413static int r128_cce_indices(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 DRM_DEVICE;
1416 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001417 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001418 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 drm_r128_buf_priv_t *buf_priv;
1420 drm_r128_indices_t elts;
1421 int count;
1422
Eric Anholt6c340ea2007-08-25 20:23:09 +10001423 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001425 if (!dev_priv) {
1426 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Eric Anholt20caafa2007-08-25 19:22:43 +10001427 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 DRM_COPY_FROM_USER_IOCTL(elts, (drm_r128_indices_t __user *) data,
1431 sizeof(elts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 DRM_DEBUG("pid=%d buf=%d s=%d e=%d d=%d\n", DRM_CURRENTPID,
1434 elts.idx, elts.start, elts.end, elts.discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 if (elts.idx < 0 || elts.idx >= dma->buf_count) {
1437 DRM_ERROR("buffer index %d (of %d max)\n",
1438 elts.idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001441 if (elts.prim < 0 || elts.prim > R128_CCE_VC_CNTL_PRIM_TYPE_TRI_TYPE2) {
1442 DRM_ERROR("buffer prim %d\n", elts.prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10001443 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001446 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1447 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 buf = dma->buflist[elts.idx];
1450 buf_priv = buf->dev_private;
1451
Eric Anholt6c340ea2007-08-25 20:23:09 +10001452 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001453 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001454 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001457 if (buf->pending) {
1458 DRM_ERROR("sending pending buffer %d\n", elts.idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
1462 count = (elts.end - elts.start) / sizeof(u16);
1463 elts.start -= R128_INDEX_PRIM_OFFSET;
1464
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001465 if (elts.start & 0x7) {
1466 DRM_ERROR("misaligned buffer 0x%x\n", elts.start);
Eric Anholt20caafa2007-08-25 19:22:43 +10001467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001469 if (elts.start < buf->used) {
1470 DRM_ERROR("no header 0x%x - 0x%x\n", elts.start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10001471 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
1474 buf->used = elts.end;
1475 buf_priv->prim = elts.prim;
1476 buf_priv->discard = elts.discard;
1477
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001478 r128_cce_dispatch_indices(dev, buf, elts.start, elts.end, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 COMMIT_RING();
1481 return 0;
1482}
1483
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484static int r128_cce_blit(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 DRM_DEVICE;
Dave Airliecdd55a22007-07-11 16:32:08 +10001487 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 drm_r128_private_t *dev_priv = dev->dev_private;
1489 drm_r128_blit_t blit;
1490 int ret;
1491
Eric Anholt6c340ea2007-08-25 20:23:09 +10001492 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 DRM_COPY_FROM_USER_IOCTL(blit, (drm_r128_blit_t __user *) data,
1495 sizeof(blit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 DRM_DEBUG("pid=%d index=%d\n", DRM_CURRENTPID, blit.idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001499 if (blit.idx < 0 || blit.idx >= dma->buf_count) {
1500 DRM_ERROR("buffer index %d (of %d max)\n",
1501 blit.idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1506 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Eric Anholt6c340ea2007-08-25 20:23:09 +10001508 ret = r128_cce_dispatch_blit(dev, file_priv, &blit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 COMMIT_RING();
1511 return ret;
1512}
1513
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001514static int r128_cce_depth(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 DRM_DEVICE;
1517 drm_r128_private_t *dev_priv = dev->dev_private;
1518 drm_r128_depth_t depth;
1519 int ret;
1520
Eric Anholt6c340ea2007-08-25 20:23:09 +10001521 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001523 DRM_COPY_FROM_USER_IOCTL(depth, (drm_r128_depth_t __user *) data,
1524 sizeof(depth));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Eric Anholt20caafa2007-08-25 19:22:43 +10001528 ret = -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001529 switch (depth.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 case R128_WRITE_SPAN:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001531 ret = r128_cce_dispatch_write_span(dev, &depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 case R128_WRITE_PIXELS:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534 ret = r128_cce_dispatch_write_pixels(dev, &depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 case R128_READ_SPAN:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001537 ret = r128_cce_dispatch_read_span(dev, &depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 case R128_READ_PIXELS:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 ret = r128_cce_dispatch_read_pixels(dev, &depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
1544 COMMIT_RING();
1545 return ret;
1546}
1547
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001548static int r128_cce_stipple(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 DRM_DEVICE;
1551 drm_r128_private_t *dev_priv = dev->dev_private;
1552 drm_r128_stipple_t stipple;
1553 u32 mask[32];
1554
Eric Anholt6c340ea2007-08-25 20:23:09 +10001555 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 DRM_COPY_FROM_USER_IOCTL(stipple, (drm_r128_stipple_t __user *) data,
1558 sizeof(stipple));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 if (DRM_COPY_FROM_USER(&mask, stipple.mask, 32 * sizeof(u32)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001561 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 r128_cce_dispatch_stipple(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 COMMIT_RING();
1568 return 0;
1569}
1570
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571static int r128_cce_indirect(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 DRM_DEVICE;
1574 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001575 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001576 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 drm_r128_buf_priv_t *buf_priv;
1578 drm_r128_indirect_t indirect;
1579#if 0
1580 RING_LOCALS;
1581#endif
1582
Eric Anholt6c340ea2007-08-25 20:23:09 +10001583 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 if (!dev_priv) {
1586 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Eric Anholt20caafa2007-08-25 19:22:43 +10001587 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001590 DRM_COPY_FROM_USER_IOCTL(indirect, (drm_r128_indirect_t __user *) data,
1591 sizeof(indirect));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001593 DRM_DEBUG("indirect: idx=%d s=%d e=%d d=%d\n",
1594 indirect.idx, indirect.start, indirect.end, indirect.discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 if (indirect.idx < 0 || indirect.idx >= dma->buf_count) {
1597 DRM_ERROR("buffer index %d (of %d max)\n",
1598 indirect.idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001599 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
1602 buf = dma->buflist[indirect.idx];
1603 buf_priv = buf->dev_private;
1604
Eric Anholt6c340ea2007-08-25 20:23:09 +10001605 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001606 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001607 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001610 if (buf->pending) {
1611 DRM_ERROR("sending pending buffer %d\n", indirect.idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001615 if (indirect.start < buf->used) {
1616 DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
1617 indirect.start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10001618 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001621 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1622 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 buf->used = indirect.end;
1625 buf_priv->discard = indirect.discard;
1626
1627#if 0
1628 /* Wait for the 3D stream to idle before the indirect buffer
1629 * containing 2D acceleration commands is processed.
1630 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001631 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 RADEON_WAIT_UNTIL_3D_IDLE();
1633 ADVANCE_RING();
1634#endif
1635
1636 /* Dispatch the indirect buffer full of commands from the
1637 * X server. This is insecure and is thus only available to
1638 * privileged clients.
1639 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001640 r128_cce_dispatch_indirect(dev, buf, indirect.start, indirect.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 COMMIT_RING();
1643 return 0;
1644}
1645
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001646static int r128_getparam(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 DRM_DEVICE;
1649 drm_r128_private_t *dev_priv = dev->dev_private;
1650 drm_r128_getparam_t param;
1651 int value;
1652
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001653 if (!dev_priv) {
1654 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Eric Anholt20caafa2007-08-25 19:22:43 +10001655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001658 DRM_COPY_FROM_USER_IOCTL(param, (drm_r128_getparam_t __user *) data,
1659 sizeof(param));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001661 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001663 switch (param.param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 case R128_PARAM_IRQ_NR:
1665 value = dev->irq;
1666 break;
1667 default:
Eric Anholt20caafa2007-08-25 19:22:43 +10001668 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001671 if (DRM_COPY_TO_USER(param.value, &value, sizeof(int))) {
1672 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001673 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return 0;
1677}
1678
Eric Anholt6c340ea2007-08-25 20:23:09 +10001679void r128_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001681 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001683 if (dev_priv->page_flipping) {
1684 r128_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Dave Airlieeddca552007-07-11 16:09:54 +10001689void r128_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001691 r128_do_cleanup_cce(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694drm_ioctl_desc_t r128_ioctls[] = {
Dave Airliea7a2cc32006-01-02 13:54:04 +11001695 [DRM_IOCTL_NR(DRM_R128_INIT)] = {r128_cce_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1696 [DRM_IOCTL_NR(DRM_R128_CCE_START)] = {r128_cce_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1697 [DRM_IOCTL_NR(DRM_R128_CCE_STOP)] = {r128_cce_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1698 [DRM_IOCTL_NR(DRM_R128_CCE_RESET)] = {r128_cce_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1699 [DRM_IOCTL_NR(DRM_R128_CCE_IDLE)] = {r128_cce_idle, DRM_AUTH},
1700 [DRM_IOCTL_NR(DRM_R128_RESET)] = {r128_engine_reset, DRM_AUTH},
1701 [DRM_IOCTL_NR(DRM_R128_FULLSCREEN)] = {r128_fullscreen, DRM_AUTH},
1702 [DRM_IOCTL_NR(DRM_R128_SWAP)] = {r128_cce_swap, DRM_AUTH},
1703 [DRM_IOCTL_NR(DRM_R128_FLIP)] = {r128_cce_flip, DRM_AUTH},
1704 [DRM_IOCTL_NR(DRM_R128_CLEAR)] = {r128_cce_clear, DRM_AUTH},
1705 [DRM_IOCTL_NR(DRM_R128_VERTEX)] = {r128_cce_vertex, DRM_AUTH},
1706 [DRM_IOCTL_NR(DRM_R128_INDICES)] = {r128_cce_indices, DRM_AUTH},
1707 [DRM_IOCTL_NR(DRM_R128_BLIT)] = {r128_cce_blit, DRM_AUTH},
1708 [DRM_IOCTL_NR(DRM_R128_DEPTH)] = {r128_cce_depth, DRM_AUTH},
1709 [DRM_IOCTL_NR(DRM_R128_STIPPLE)] = {r128_cce_stipple, DRM_AUTH},
1710 [DRM_IOCTL_NR(DRM_R128_INDIRECT)] = {r128_cce_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
1711 [DRM_IOCTL_NR(DRM_R128_GETPARAM)] = {r128_getparam, DRM_AUTH},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712};
1713
1714int r128_max_ioctl = DRM_ARRAY_SIZE(r128_ioctls);