blob: af2665cf4718ca8b92c56130e9a5aec0f80845a6 [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;
Márton Németh3e684ea2008-01-24 15:58:57 +100045 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +100088 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000103 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000129 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000145 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000164 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000181 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000207 DRM_DEBUG("\n");
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
Márton Németh3e684ea2008-01-24 15:58:57 +1000229 DRM_DEBUG("dirty=0x%08x\n", 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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000365 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000469 DRM_DEBUG("\n");
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;
Márton Németh3e684ea2008-01-24 15:58:57 +1000531 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 dev_priv->current_page, dev_priv->sarea_priv->pfCurrentPage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534#if R128_PERFORMANCE_BOXES
535 /* Do some trivial performance monitoring...
536 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 r128_cce_performance_boxes(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#endif
539
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 R128_WAIT_UNTIL_PAGE_FLIPPED();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 OUT_RING(CCE_PACKET0(R128_CRTC_OFFSET, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 if (dev_priv->current_page == 0) {
546 OUT_RING(dev_priv->back_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
551 ADVANCE_RING();
552
553 /* Increment the frame counter. The client-side 3D driver must
554 * throttle the framerate by waiting for this value before
555 * performing the swapbuffer ioctl.
556 */
557 dev_priv->sarea_priv->last_frame++;
558 dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000559 1 - dev_priv->current_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 OUT_RING(CCE_PACKET0(R128_LAST_FRAME_REG, 0));
564 OUT_RING(dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 ADVANCE_RING();
567}
568
Dave Airlie056219e2007-07-11 16:17:42 +1000569static void r128_cce_dispatch_vertex(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 drm_r128_private_t *dev_priv = dev->dev_private;
572 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
573 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
574 int format = sarea_priv->vc_format;
575 int offset = buf->bus_address;
576 int size = buf->used;
577 int prim = buf_priv->prim;
578 int i = 0;
579 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 DRM_DEBUG("buf=%d nbox=%d\n", buf->idx, sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 if (0)
583 r128_print_dirty("dispatch_vertex", sarea_priv->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 if (buf->used) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 buf_priv->dispatched = 1;
587
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 if (sarea_priv->dirty & ~R128_UPLOAD_CLIPRECTS) {
589 r128_emit_state(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 do {
593 /* Emit the next set of up to three cliprects */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 if (i < sarea_priv->nbox) {
595 r128_emit_clip_rects(dev_priv,
596 &sarea_priv->boxes[i],
597 sarea_priv->nbox - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
600 /* Emit the vertex buffer rendering commands */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 BEGIN_RING(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000603 OUT_RING(CCE_PACKET3(R128_3D_RNDR_GEN_INDX_PRIM, 3));
604 OUT_RING(offset);
605 OUT_RING(size);
606 OUT_RING(format);
607 OUT_RING(prim | R128_CCE_VC_CNTL_PRIM_WALK_LIST |
608 (size << R128_CCE_VC_CNTL_NUM_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 ADVANCE_RING();
611
612 i += 3;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 } while (i < sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000616 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
618
619 /* Emit the vertex buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
623 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 ADVANCE_RING();
626
627 buf->pending = 1;
628 buf->used = 0;
629 /* FIXME: Check dispatched field */
630 buf_priv->dispatched = 0;
631 }
632
633 dev_priv->sarea_priv->last_dispatch++;
634
635 sarea_priv->dirty &= ~R128_UPLOAD_CLIPRECTS;
636 sarea_priv->nbox = 0;
637}
638
Dave Airlieeddca552007-07-11 16:09:54 +1000639static void r128_cce_dispatch_indirect(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000640 struct drm_buf * buf, int start, int end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 drm_r128_private_t *dev_priv = dev->dev_private;
643 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
644 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 DRM_DEBUG("indirect: buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int offset = buf->bus_address + start;
649 int dwords = (end - start + 3) / sizeof(u32);
650
651 /* Indirect buffer data must be an even number of
652 * dwords, so if we've been given an odd number we must
653 * pad the data with a Type-2 CCE packet.
654 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 if (dwords & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u32 *data = (u32 *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 ((char *)dev->agp_buffer_map->handle
658 + buf->offset + start);
659 data[dwords++] = cpu_to_le32(R128_CCE_PACKET2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 buf_priv->dispatched = 1;
663
664 /* Fire off the indirect buffer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 BEGIN_RING(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 OUT_RING(CCE_PACKET0(R128_PM4_IW_INDOFF, 1));
668 OUT_RING(offset);
669 OUT_RING(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 ADVANCE_RING();
672 }
673
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
676
677 /* Emit the indirect buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
681 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 ADVANCE_RING();
684
685 buf->pending = 1;
686 buf->used = 0;
687 /* FIXME: Check dispatched field */
688 buf_priv->dispatched = 0;
689 }
690
691 dev_priv->sarea_priv->last_dispatch++;
692}
693
Dave Airlieeddca552007-07-11 16:09:54 +1000694static void r128_cce_dispatch_indices(struct drm_device * dev,
Dave Airlie056219e2007-07-11 16:17:42 +1000695 struct drm_buf * buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 int start, int end, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 drm_r128_private_t *dev_priv = dev->dev_private;
699 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
700 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
701 int format = sarea_priv->vc_format;
702 int offset = dev->agp_buffer_map->offset - dev_priv->cce_buffers_offset;
703 int prim = buf_priv->prim;
704 u32 *data;
705 int dwords;
706 int i = 0;
707 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 DRM_DEBUG("indices: s=%d e=%d c=%d\n", start, end, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 if (0)
711 r128_print_dirty("dispatch_indices", sarea_priv->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000713 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 buf_priv->dispatched = 1;
715
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 if (sarea_priv->dirty & ~R128_UPLOAD_CLIPRECTS) {
717 r128_emit_state(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 dwords = (end - start + 3) / sizeof(u32);
721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 data = (u32 *) ((char *)dev->agp_buffer_map->handle
723 + buf->offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 data[0] = cpu_to_le32(CCE_PACKET3(R128_3D_RNDR_GEN_INDX_PRIM,
726 dwords - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 data[1] = cpu_to_le32(offset);
729 data[2] = cpu_to_le32(R128_MAX_VB_VERTS);
730 data[3] = cpu_to_le32(format);
731 data[4] = cpu_to_le32((prim | R128_CCE_VC_CNTL_PRIM_WALK_IND |
732 (count << 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 if (count & 0x1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735#ifdef __LITTLE_ENDIAN
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 data[dwords - 1] &= 0x0000ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 data[dwords - 1] &= 0xffff0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739#endif
740 }
741
742 do {
743 /* Emit the next set of up to three cliprects */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 if (i < sarea_priv->nbox) {
745 r128_emit_clip_rects(dev_priv,
746 &sarea_priv->boxes[i],
747 sarea_priv->nbox - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 r128_cce_dispatch_indirect(dev, buf, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 i += 3;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 } while (i < sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
758
759 /* Emit the vertex buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
763 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 ADVANCE_RING();
766
767 buf->pending = 1;
768 /* FIXME: Check dispatched field */
769 buf_priv->dispatched = 0;
770 }
771
772 dev_priv->sarea_priv->last_dispatch++;
773
774 sarea_priv->dirty &= ~R128_UPLOAD_CLIPRECTS;
775 sarea_priv->nbox = 0;
776}
777
Eric Anholt6c340ea2007-08-25 20:23:09 +1000778static int r128_cce_dispatch_blit(struct drm_device * dev,
779 struct drm_file *file_priv,
780 drm_r128_blit_t * blit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000783 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000784 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 drm_r128_buf_priv_t *buf_priv;
786 u32 *data;
787 int dword_shift, dwords;
788 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* The compiler won't optimize away a division by a variable,
792 * even if the only legal values are powers of two. Thus, we'll
793 * use a shift instead.
794 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 switch (blit->format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case R128_DATATYPE_ARGB8888:
797 dword_shift = 0;
798 break;
799 case R128_DATATYPE_ARGB1555:
800 case R128_DATATYPE_RGB565:
801 case R128_DATATYPE_ARGB4444:
802 case R128_DATATYPE_YVYU422:
803 case R128_DATATYPE_VYUY422:
804 dword_shift = 1;
805 break;
806 case R128_DATATYPE_CI8:
807 case R128_DATATYPE_RGB8:
808 dword_shift = 2;
809 break;
810 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 DRM_ERROR("invalid blit format %d\n", blit->format);
Eric Anholt20caafa2007-08-25 19:22:43 +1000812 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815 /* Flush the pixel cache, and mark the contents as Read Invalid.
816 * This ensures no pixel data gets mixed up with the texture
817 * data from the host data blit, otherwise part of the texture
818 * image may be corrupted.
819 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 OUT_RING(CCE_PACKET0(R128_PC_GUI_CTLSTAT, 0));
823 OUT_RING(R128_PC_RI_GUI | R128_PC_FLUSH_GUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 ADVANCE_RING();
826
827 /* Dispatch the indirect buffer.
828 */
829 buf = dma->buflist[blit->idx];
830 buf_priv = buf->dev_private;
831
Eric Anholt6c340ea2007-08-25 20:23:09 +1000832 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +1000834 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +1000835 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 if (buf->pending) {
838 DRM_ERROR("sending pending buffer %d\n", blit->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +1000839 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 buf_priv->discard = 1;
843
844 dwords = (blit->width * blit->height) >> dword_shift;
845
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 data = (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 data[0] = cpu_to_le32(CCE_PACKET3(R128_CNTL_HOSTDATA_BLT, dwords + 6));
849 data[1] = cpu_to_le32((R128_GMC_DST_PITCH_OFFSET_CNTL |
850 R128_GMC_BRUSH_NONE |
851 (blit->format << 8) |
852 R128_GMC_SRC_DATATYPE_COLOR |
853 R128_ROP3_S |
854 R128_DP_SRC_SOURCE_HOST_DATA |
855 R128_GMC_CLR_CMP_CNTL_DIS |
856 R128_GMC_AUX_CLIP_DIS | R128_GMC_WR_MSK_DIS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 data[2] = cpu_to_le32((blit->pitch << 21) | (blit->offset >> 5));
859 data[3] = cpu_to_le32(0xffffffff);
860 data[4] = cpu_to_le32(0xffffffff);
861 data[5] = cpu_to_le32((blit->y << 16) | blit->x);
862 data[6] = cpu_to_le32((blit->height << 16) | blit->width);
863 data[7] = cpu_to_le32(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 buf->used = (dwords + 8) * sizeof(u32);
866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 r128_cce_dispatch_indirect(dev, buf, 0, buf->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /* Flush the pixel cache after the blit completes. This ensures
870 * the texture data is written out to memory before rendering
871 * continues.
872 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000873 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 OUT_RING(CCE_PACKET0(R128_PC_GUI_CTLSTAT, 0));
876 OUT_RING(R128_PC_FLUSH_GUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 ADVANCE_RING();
879
880 return 0;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/* ================================================================
884 * Tiled depth buffer management
885 *
886 * FIXME: These should all set the destination write mask for when we
887 * have hardware stencil support.
888 */
889
Dave Airlieeddca552007-07-11 16:09:54 +1000890static int r128_cce_dispatch_write_span(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000891 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 drm_r128_private_t *dev_priv = dev->dev_private;
894 int count, x, y;
895 u32 *buffer;
896 u8 *mask;
897 int i, buffer_size, mask_size;
898 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 count = depth->n;
902 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +1000903 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 if (DRM_COPY_FROM_USER(&x, depth->x, sizeof(x))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000906 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 if (DRM_COPY_FROM_USER(&y, depth->y, sizeof(y))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000909 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 buffer_size = depth->n * sizeof(u32);
Eric Anholt9a298b22009-03-24 12:23:04 -0700913 buffer = kmalloc(buffer_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 if (buffer == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000915 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 if (DRM_COPY_FROM_USER(buffer, depth->buffer, buffer_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700917 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +1000918 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921 mask_size = depth->n * sizeof(u8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 if (depth->mask) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700923 mask = kmalloc(mask_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 if (mask == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700925 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +1000926 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 if (DRM_COPY_FROM_USER(mask, depth->mask, mask_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700929 kfree(buffer);
930 kfree(mask);
Eric Anholt20caafa2007-08-25 19:22:43 +1000931 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000934 for (i = 0; i < count; i++, x++) {
935 if (mask[i]) {
936 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
939 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
940 R128_GMC_BRUSH_SOLID_COLOR |
941 (dev_priv->depth_fmt << 8) |
942 R128_GMC_SRC_DATATYPE_COLOR |
943 R128_ROP3_P |
944 R128_GMC_CLR_CMP_CNTL_DIS |
945 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 OUT_RING(dev_priv->depth_pitch_offset_c);
948 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 OUT_RING((x << 16) | y);
951 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 ADVANCE_RING();
954 }
955 }
956
Eric Anholt9a298b22009-03-24 12:23:04 -0700957 kfree(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000959 for (i = 0; i < count; i++, x++) {
960 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000962 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
963 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
964 R128_GMC_BRUSH_SOLID_COLOR |
965 (dev_priv->depth_fmt << 8) |
966 R128_GMC_SRC_DATATYPE_COLOR |
967 R128_ROP3_P |
968 R128_GMC_CLR_CMP_CNTL_DIS |
969 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 OUT_RING(dev_priv->depth_pitch_offset_c);
972 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 OUT_RING((x << 16) | y);
975 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 ADVANCE_RING();
978 }
979 }
980
Eric Anholt9a298b22009-03-24 12:23:04 -0700981 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 return 0;
984}
985
Dave Airlieeddca552007-07-11 16:09:54 +1000986static int r128_cce_dispatch_write_pixels(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 drm_r128_private_t *dev_priv = dev->dev_private;
990 int count, *x, *y;
991 u32 *buffer;
992 u8 *mask;
993 int i, xbuf_size, ybuf_size, buffer_size, mask_size;
994 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 count = depth->n;
998 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +1000999 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 xbuf_size = count * sizeof(*x);
1002 ybuf_size = count * sizeof(*y);
Eric Anholt9a298b22009-03-24 12:23:04 -07001003 x = kmalloc(xbuf_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001004 if (x == NULL) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Eric Anholt9a298b22009-03-24 12:23:04 -07001007 y = kmalloc(ybuf_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 if (y == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001009 kfree(x);
Eric Anholt20caafa2007-08-25 19:22:43 +10001010 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 if (DRM_COPY_FROM_USER(x, depth->x, xbuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001013 kfree(x);
1014 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001015 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001017 if (DRM_COPY_FROM_USER(y, depth->y, xbuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001018 kfree(x);
1019 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001020 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022
1023 buffer_size = depth->n * sizeof(u32);
Eric Anholt9a298b22009-03-24 12:23:04 -07001024 buffer = kmalloc(buffer_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 if (buffer == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001026 kfree(x);
1027 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001028 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 if (DRM_COPY_FROM_USER(buffer, depth->buffer, buffer_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001031 kfree(x);
1032 kfree(y);
1033 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +10001034 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 if (depth->mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 mask_size = depth->n * sizeof(u8);
Eric Anholt9a298b22009-03-24 12:23:04 -07001039 mask = kmalloc(mask_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 if (mask == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001041 kfree(x);
1042 kfree(y);
1043 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +10001044 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 if (DRM_COPY_FROM_USER(mask, depth->mask, mask_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001047 kfree(x);
1048 kfree(y);
1049 kfree(buffer);
1050 kfree(mask);
Eric Anholt20caafa2007-08-25 19:22:43 +10001051 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001054 for (i = 0; i < count; i++) {
1055 if (mask[i]) {
1056 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
1059 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
1060 R128_GMC_BRUSH_SOLID_COLOR |
1061 (dev_priv->depth_fmt << 8) |
1062 R128_GMC_SRC_DATATYPE_COLOR |
1063 R128_ROP3_P |
1064 R128_GMC_CLR_CMP_CNTL_DIS |
1065 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 OUT_RING(dev_priv->depth_pitch_offset_c);
1068 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 OUT_RING((x[i] << 16) | y[i]);
1071 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 ADVANCE_RING();
1074 }
1075 }
1076
Eric Anholt9a298b22009-03-24 12:23:04 -07001077 kfree(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 for (i = 0; i < count; i++) {
1080 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
1083 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
1084 R128_GMC_BRUSH_SOLID_COLOR |
1085 (dev_priv->depth_fmt << 8) |
1086 R128_GMC_SRC_DATATYPE_COLOR |
1087 R128_ROP3_P |
1088 R128_GMC_CLR_CMP_CNTL_DIS |
1089 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 OUT_RING(dev_priv->depth_pitch_offset_c);
1092 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 OUT_RING((x[i] << 16) | y[i]);
1095 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 ADVANCE_RING();
1098 }
1099 }
1100
Eric Anholt9a298b22009-03-24 12:23:04 -07001101 kfree(x);
1102 kfree(y);
1103 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 return 0;
1106}
1107
Dave Airlieeddca552007-07-11 16:09:54 +10001108static int r128_cce_dispatch_read_span(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 drm_r128_private_t *dev_priv = dev->dev_private;
1112 int count, x, y;
1113 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 count = depth->n;
1117 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +10001118 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 if (DRM_COPY_FROM_USER(&x, depth->x, sizeof(x))) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001121 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 if (DRM_COPY_FROM_USER(&y, depth->y, sizeof(y))) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001124 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
1130 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
1131 R128_GMC_DST_PITCH_OFFSET_CNTL |
1132 R128_GMC_BRUSH_NONE |
1133 (dev_priv->depth_fmt << 8) |
1134 R128_GMC_SRC_DATATYPE_COLOR |
1135 R128_ROP3_S |
1136 R128_DP_SRC_SOURCE_MEMORY |
1137 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 OUT_RING(dev_priv->depth_pitch_offset_c);
1140 OUT_RING(dev_priv->span_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 OUT_RING((x << 16) | y);
1143 OUT_RING((0 << 16) | 0);
1144 OUT_RING((count << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 ADVANCE_RING();
1147
1148 return 0;
1149}
1150
Dave Airlieeddca552007-07-11 16:09:54 +10001151static int r128_cce_dispatch_read_pixels(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001152 drm_r128_depth_t * depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 drm_r128_private_t *dev_priv = dev->dev_private;
1155 int count, *x, *y;
1156 int i, xbuf_size, ybuf_size;
1157 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001158 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 count = depth->n;
1161 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +10001162 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001164 if (count > dev_priv->depth_pitch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 count = dev_priv->depth_pitch;
1166 }
1167
1168 xbuf_size = count * sizeof(*x);
1169 ybuf_size = count * sizeof(*y);
Eric Anholt9a298b22009-03-24 12:23:04 -07001170 x = kmalloc(xbuf_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 if (x == NULL) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001172 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Eric Anholt9a298b22009-03-24 12:23:04 -07001174 y = kmalloc(ybuf_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175 if (y == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001176 kfree(x);
Eric Anholt20caafa2007-08-25 19:22:43 +10001177 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001179 if (DRM_COPY_FROM_USER(x, depth->x, xbuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001180 kfree(x);
1181 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001182 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001184 if (DRM_COPY_FROM_USER(y, depth->y, ybuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001185 kfree(x);
1186 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001187 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 for (i = 0; i < count; i++) {
1191 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001193 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
1194 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
1195 R128_GMC_DST_PITCH_OFFSET_CNTL |
1196 R128_GMC_BRUSH_NONE |
1197 (dev_priv->depth_fmt << 8) |
1198 R128_GMC_SRC_DATATYPE_COLOR |
1199 R128_ROP3_S |
1200 R128_DP_SRC_SOURCE_MEMORY |
1201 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001203 OUT_RING(dev_priv->depth_pitch_offset_c);
1204 OUT_RING(dev_priv->span_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 OUT_RING((x[i] << 16) | y[i]);
1207 OUT_RING((i << 16) | 0);
1208 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 ADVANCE_RING();
1211 }
1212
Eric Anholt9a298b22009-03-24 12:23:04 -07001213 kfree(x);
1214 kfree(y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 return 0;
1217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/* ================================================================
1220 * Polygon stipple
1221 */
1222
Dave Airlieeddca552007-07-11 16:09:54 +10001223static void r128_cce_dispatch_stipple(struct drm_device * dev, u32 * stipple)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 drm_r128_private_t *dev_priv = dev->dev_private;
1226 int i;
1227 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001228 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001230 BEGIN_RING(33);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001232 OUT_RING(CCE_PACKET0(R128_BRUSH_DATA0, 31));
1233 for (i = 0; i < 32; i++) {
1234 OUT_RING(stipple[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
1237 ADVANCE_RING();
1238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/* ================================================================
1241 * IOCTL functions
1242 */
1243
Eric Anholtc153f452007-09-03 12:06:45 +10001244static int r128_cce_clear(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 drm_r128_private_t *dev_priv = dev->dev_private;
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001247 drm_r128_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001248 drm_r128_clear_t *clear = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001249 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Eric Anholt6c340ea2007-08-25 20:23:09 +10001251 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001253 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1254
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001255 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001257 sarea_priv = dev_priv->sarea_priv;
1258
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001259 if (sarea_priv->nbox > R128_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 sarea_priv->nbox = R128_NR_SAREA_CLIPRECTS;
1261
Eric Anholtc153f452007-09-03 12:06:45 +10001262 r128_cce_dispatch_clear(dev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 COMMIT_RING();
1264
1265 /* Make sure we restore the 3D state next time.
1266 */
1267 dev_priv->sarea_priv->dirty |= R128_UPLOAD_CONTEXT | R128_UPLOAD_MASKS;
1268
1269 return 0;
1270}
1271
Dave Airlieeddca552007-07-11 16:09:54 +10001272static int r128_do_init_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001277 dev_priv->crtc_offset = R128_READ(R128_CRTC_OFFSET);
1278 dev_priv->crtc_offset_cntl = R128_READ(R128_CRTC_OFFSET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001280 R128_WRITE(R128_CRTC_OFFSET, dev_priv->front_offset);
1281 R128_WRITE(R128_CRTC_OFFSET_CNTL,
1282 dev_priv->crtc_offset_cntl | R128_CRTC_OFFSET_FLIP_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 dev_priv->page_flipping = 1;
1285 dev_priv->current_page = 0;
1286 dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page;
1287
1288 return 0;
1289}
1290
Dave Airlieeddca552007-07-11 16:09:54 +10001291static int r128_do_cleanup_pageflip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001296 R128_WRITE(R128_CRTC_OFFSET, dev_priv->crtc_offset);
1297 R128_WRITE(R128_CRTC_OFFSET_CNTL, dev_priv->crtc_offset_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (dev_priv->current_page != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001300 r128_cce_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 COMMIT_RING();
1302 }
1303
1304 dev_priv->page_flipping = 0;
1305 return 0;
1306}
1307
1308/* Swapping and flipping are different operations, need different ioctls.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001309 * They can & should be intermixed to support multiple 3d windows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
1311
Eric Anholtc153f452007-09-03 12:06:45 +10001312static int r128_cce_flip(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 drm_r128_private_t *dev_priv = dev->dev_private;
Márton Németh3e684ea2008-01-24 15:58:57 +10001315 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Eric Anholt6c340ea2007-08-25 20:23:09 +10001317 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001319 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1320
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
Eric Anholtc153f452007-09-03 12:06:45 +10001332static int r128_cce_swap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 drm_r128_private_t *dev_priv = dev->dev_private;
1335 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
Márton Németh3e684ea2008-01-24 15:58:57 +10001336 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Eric Anholt6c340ea2007-08-25 20:23:09 +10001338 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001340 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1341
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001342 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001344 if (sarea_priv->nbox > R128_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 sarea_priv->nbox = R128_NR_SAREA_CLIPRECTS;
1346
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001347 r128_cce_dispatch_swap(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 dev_priv->sarea_priv->dirty |= (R128_UPLOAD_CONTEXT |
1349 R128_UPLOAD_MASKS);
1350
1351 COMMIT_RING();
1352 return 0;
1353}
1354
Eric Anholtc153f452007-09-03 12:06:45 +10001355static int r128_cce_vertex(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 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;
Eric Anholtc153f452007-09-03 12:06:45 +10001361 drm_r128_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Eric Anholt6c340ea2007-08-25 20:23:09 +10001363 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001365 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001367 DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001368 DRM_CURRENTPID, vertex->idx, vertex->count, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Eric Anholtc153f452007-09-03 12:06:45 +10001370 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001371 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001372 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001373 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Eric Anholtc153f452007-09-03 12:06:45 +10001375 if (vertex->prim < 0 ||
1376 vertex->prim > R128_CCE_VC_CNTL_PRIM_TYPE_TRI_TYPE2) {
1377 DRM_ERROR("buffer prim %d\n", vertex->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10001378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1382 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Eric Anholtc153f452007-09-03 12:06:45 +10001384 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 buf_priv = buf->dev_private;
1386
Eric Anholt6c340ea2007-08-25 20:23:09 +10001387 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001388 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001389 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001390 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001392 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10001393 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
Eric Anholtc153f452007-09-03 12:06:45 +10001397 buf->used = vertex->count;
1398 buf_priv->prim = vertex->prim;
1399 buf_priv->discard = vertex->discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 r128_cce_dispatch_vertex(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 COMMIT_RING();
1404 return 0;
1405}
1406
Eric Anholtc153f452007-09-03 12:06:45 +10001407static int r128_cce_indices(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001410 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001411 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 drm_r128_buf_priv_t *buf_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001413 drm_r128_indices_t *elts = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 int count;
1415
Eric Anholt6c340ea2007-08-25 20:23:09 +10001416 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001418 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001420 DRM_DEBUG("pid=%d buf=%d s=%d e=%d d=%d\n", DRM_CURRENTPID,
Eric Anholtc153f452007-09-03 12:06:45 +10001421 elts->idx, elts->start, elts->end, elts->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Eric Anholtc153f452007-09-03 12:06:45 +10001423 if (elts->idx < 0 || elts->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001424 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001425 elts->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001426 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Eric Anholtc153f452007-09-03 12:06:45 +10001428 if (elts->prim < 0 ||
1429 elts->prim > R128_CCE_VC_CNTL_PRIM_TYPE_TRI_TYPE2) {
1430 DRM_ERROR("buffer prim %d\n", elts->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10001431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1435 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Eric Anholtc153f452007-09-03 12:06:45 +10001437 buf = dma->buflist[elts->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 buf_priv = buf->dev_private;
1439
Eric Anholt6c340ea2007-08-25 20:23:09 +10001440 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001441 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001442 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001443 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001445 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10001446 DRM_ERROR("sending pending buffer %d\n", elts->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001447 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
Eric Anholtc153f452007-09-03 12:06:45 +10001450 count = (elts->end - elts->start) / sizeof(u16);
1451 elts->start -= R128_INDEX_PRIM_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Eric Anholtc153f452007-09-03 12:06:45 +10001453 if (elts->start & 0x7) {
1454 DRM_ERROR("misaligned buffer 0x%x\n", elts->start);
Eric Anholt20caafa2007-08-25 19:22:43 +10001455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Eric Anholtc153f452007-09-03 12:06:45 +10001457 if (elts->start < buf->used) {
1458 DRM_ERROR("no header 0x%x - 0x%x\n", elts->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10001459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
Eric Anholtc153f452007-09-03 12:06:45 +10001462 buf->used = elts->end;
1463 buf_priv->prim = elts->prim;
1464 buf_priv->discard = elts->discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Eric Anholtc153f452007-09-03 12:06:45 +10001466 r128_cce_dispatch_indices(dev, buf, elts->start, elts->end, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 COMMIT_RING();
1469 return 0;
1470}
1471
Eric Anholtc153f452007-09-03 12:06:45 +10001472static int r128_cce_blit(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Dave Airliecdd55a22007-07-11 16:32:08 +10001474 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001476 drm_r128_blit_t *blit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int ret;
1478
Eric Anholt6c340ea2007-08-25 20:23:09 +10001479 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001481 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1482
Eric Anholtc153f452007-09-03 12:06:45 +10001483 DRM_DEBUG("pid=%d index=%d\n", DRM_CURRENTPID, blit->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Eric Anholtc153f452007-09-03 12:06:45 +10001485 if (blit->idx < 0 || blit->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001487 blit->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001488 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001491 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1492 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Eric Anholtc153f452007-09-03 12:06:45 +10001494 ret = r128_cce_dispatch_blit(dev, file_priv, blit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 COMMIT_RING();
1497 return ret;
1498}
1499
Eric Anholtc153f452007-09-03 12:06:45 +10001500static int r128_cce_depth(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001503 drm_r128_depth_t *depth = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int ret;
1505
Eric Anholt6c340ea2007-08-25 20:23:09 +10001506 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001508 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1509
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001510 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Eric Anholt20caafa2007-08-25 19:22:43 +10001512 ret = -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001513 switch (depth->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 case R128_WRITE_SPAN:
Eric Anholtc153f452007-09-03 12:06:45 +10001515 ret = r128_cce_dispatch_write_span(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 case R128_WRITE_PIXELS:
Eric Anholtc153f452007-09-03 12:06:45 +10001518 ret = r128_cce_dispatch_write_pixels(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 case R128_READ_SPAN:
Eric Anholtc153f452007-09-03 12:06:45 +10001521 ret = r128_cce_dispatch_read_span(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 case R128_READ_PIXELS:
Eric Anholtc153f452007-09-03 12:06:45 +10001524 ret = r128_cce_dispatch_read_pixels(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
1528 COMMIT_RING();
1529 return ret;
1530}
1531
Eric Anholtc153f452007-09-03 12:06:45 +10001532static int r128_cce_stipple(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001535 drm_r128_stipple_t *stipple = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 u32 mask[32];
1537
Eric Anholt6c340ea2007-08-25 20:23:09 +10001538 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001540 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1541
Eric Anholtc153f452007-09-03 12:06:45 +10001542 if (DRM_COPY_FROM_USER(&mask, stipple->mask, 32 * sizeof(u32)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001543 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001545 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001547 r128_cce_dispatch_stipple(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 COMMIT_RING();
1550 return 0;
1551}
1552
Eric Anholtc153f452007-09-03 12:06:45 +10001553static int r128_cce_indirect(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001556 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001557 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 drm_r128_buf_priv_t *buf_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001559 drm_r128_indirect_t *indirect = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560#if 0
1561 RING_LOCALS;
1562#endif
1563
Eric Anholt6c340ea2007-08-25 20:23:09 +10001564 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001566 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Márton Németh3e684ea2008-01-24 15:58:57 +10001568 DRM_DEBUG("idx=%d s=%d e=%d d=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001569 indirect->idx, indirect->start, indirect->end,
1570 indirect->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Eric Anholtc153f452007-09-03 12:06:45 +10001572 if (indirect->idx < 0 || indirect->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001573 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001574 indirect->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Eric Anholtc153f452007-09-03 12:06:45 +10001578 buf = dma->buflist[indirect->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 buf_priv = buf->dev_private;
1580
Eric Anholt6c340ea2007-08-25 20:23:09 +10001581 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001582 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001583 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001586 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10001587 DRM_ERROR("sending pending buffer %d\n", indirect->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001588 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590
Eric Anholtc153f452007-09-03 12:06:45 +10001591 if (indirect->start < buf->used) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001592 DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001593 indirect->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10001594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001597 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1598 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Eric Anholtc153f452007-09-03 12:06:45 +10001600 buf->used = indirect->end;
1601 buf_priv->discard = indirect->discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603#if 0
1604 /* Wait for the 3D stream to idle before the indirect buffer
1605 * containing 2D acceleration commands is processed.
1606 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001607 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 RADEON_WAIT_UNTIL_3D_IDLE();
1609 ADVANCE_RING();
1610#endif
1611
1612 /* Dispatch the indirect buffer full of commands from the
1613 * X server. This is insecure and is thus only available to
1614 * privileged clients.
1615 */
Eric Anholtc153f452007-09-03 12:06:45 +10001616 r128_cce_dispatch_indirect(dev, buf, indirect->start, indirect->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 COMMIT_RING();
1619 return 0;
1620}
1621
Eric Anholtc153f452007-09-03 12:06:45 +10001622static int r128_getparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001625 drm_r128_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int value;
1627
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001628 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001630 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Eric Anholtc153f452007-09-03 12:06:45 +10001632 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 case R128_PARAM_IRQ_NR:
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -07001634 value = drm_dev_to_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 break;
1636 default:
Eric Anholt20caafa2007-08-25 19:22:43 +10001637 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
Eric Anholtc153f452007-09-03 12:06:45 +10001640 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001641 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001642 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return 0;
1646}
1647
Eric Anholt6c340ea2007-08-25 20:23:09 +10001648void r128_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001650 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001652 if (dev_priv->page_flipping) {
1653 r128_do_cleanup_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Dave Airlieeddca552007-07-11 16:09:54 +10001658void r128_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001660 r128_do_cleanup_cce(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Eric Anholtc153f452007-09-03 12:06:45 +10001663struct drm_ioctl_desc r128_ioctls[] = {
1664 DRM_IOCTL_DEF(DRM_R128_INIT, r128_cce_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1665 DRM_IOCTL_DEF(DRM_R128_CCE_START, r128_cce_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1666 DRM_IOCTL_DEF(DRM_R128_CCE_STOP, r128_cce_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1667 DRM_IOCTL_DEF(DRM_R128_CCE_RESET, r128_cce_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1668 DRM_IOCTL_DEF(DRM_R128_CCE_IDLE, r128_cce_idle, DRM_AUTH),
1669 DRM_IOCTL_DEF(DRM_R128_RESET, r128_engine_reset, DRM_AUTH),
1670 DRM_IOCTL_DEF(DRM_R128_FULLSCREEN, r128_fullscreen, DRM_AUTH),
1671 DRM_IOCTL_DEF(DRM_R128_SWAP, r128_cce_swap, DRM_AUTH),
1672 DRM_IOCTL_DEF(DRM_R128_FLIP, r128_cce_flip, DRM_AUTH),
1673 DRM_IOCTL_DEF(DRM_R128_CLEAR, r128_cce_clear, DRM_AUTH),
1674 DRM_IOCTL_DEF(DRM_R128_VERTEX, r128_cce_vertex, DRM_AUTH),
1675 DRM_IOCTL_DEF(DRM_R128_INDICES, r128_cce_indices, DRM_AUTH),
1676 DRM_IOCTL_DEF(DRM_R128_BLIT, r128_cce_blit, DRM_AUTH),
1677 DRM_IOCTL_DEF(DRM_R128_DEPTH, r128_cce_depth, DRM_AUTH),
1678 DRM_IOCTL_DEF(DRM_R128_STIPPLE, r128_cce_stipple, DRM_AUTH),
1679 DRM_IOCTL_DEF(DRM_R128_INDIRECT, r128_cce_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1680 DRM_IOCTL_DEF(DRM_R128_GETPARAM, r128_getparam, DRM_AUTH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681};
1682
1683int r128_max_ioctl = DRM_ARRAY_SIZE(r128_ioctls);