blob: a9e33ce65918c98bed3c039dbc65fa8e02bac15e [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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020040static void r128_emit_clip_rects(drm_r128_private_t *dev_priv,
41 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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020083static __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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020098static __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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200124static __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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200140static __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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200159static __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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200174static __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);
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200190 for (i = 0; i < R128_MAX_TEXTURE_LEVELS; i++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 OUT_RING(tex->tex_offset[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 OUT_RING(CCE_PACKET0(R128_CONSTANT_COLOR_C, 1));
194 OUT_RING(ctx->constant_color_c);
195 OUT_RING(tex->tex_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 ADVANCE_RING();
198}
199
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200200static __inline__ void r128_emit_tex1(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
203 drm_r128_texture_regs_t *tex = &sarea_priv->tex_state[1];
204 int i;
205 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +1000206 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 BEGIN_RING(5 + R128_MAX_TEXTURE_LEVELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 OUT_RING(CCE_PACKET0(R128_SEC_TEX_CNTL_C, 1 + R128_MAX_TEXTURE_LEVELS));
211 OUT_RING(tex->tex_cntl);
212 OUT_RING(tex->tex_combine_cntl);
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200213 for (i = 0; i < R128_MAX_TEXTURE_LEVELS; i++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 OUT_RING(tex->tex_offset[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 OUT_RING(CCE_PACKET0(R128_SEC_TEXTURE_BORDER_COLOR_C, 0));
217 OUT_RING(tex->tex_border_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 ADVANCE_RING();
220}
221
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200222static void r128_emit_state(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
225 unsigned int dirty = sarea_priv->dirty;
226
Márton Németh3e684ea2008-01-24 15:58:57 +1000227 DRM_DEBUG("dirty=0x%08x\n", dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 if (dirty & R128_UPLOAD_CORE) {
230 r128_emit_core(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 sarea_priv->dirty &= ~R128_UPLOAD_CORE;
232 }
233
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234 if (dirty & R128_UPLOAD_CONTEXT) {
235 r128_emit_context(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sarea_priv->dirty &= ~R128_UPLOAD_CONTEXT;
237 }
238
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 if (dirty & R128_UPLOAD_SETUP) {
240 r128_emit_setup(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 sarea_priv->dirty &= ~R128_UPLOAD_SETUP;
242 }
243
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 if (dirty & R128_UPLOAD_MASKS) {
245 r128_emit_masks(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 sarea_priv->dirty &= ~R128_UPLOAD_MASKS;
247 }
248
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 if (dirty & R128_UPLOAD_WINDOW) {
250 r128_emit_window(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 sarea_priv->dirty &= ~R128_UPLOAD_WINDOW;
252 }
253
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 if (dirty & R128_UPLOAD_TEX0) {
255 r128_emit_tex0(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 sarea_priv->dirty &= ~R128_UPLOAD_TEX0;
257 }
258
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 if (dirty & R128_UPLOAD_TEX1) {
260 r128_emit_tex1(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 sarea_priv->dirty &= ~R128_UPLOAD_TEX1;
262 }
263
264 /* Turn off the texture cache flushing */
265 sarea_priv->context_state.tex_cntl_c &= ~R128_TEX_CACHE_FLUSH;
266
267 sarea_priv->dirty &= ~R128_REQUIRE_QUIESCENCE;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#if R128_PERFORMANCE_BOXES
271/* ================================================================
272 * Performance monitoring functions
273 */
274
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200275static void r128_clear_box(drm_r128_private_t *dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 int x, int y, int w, int h, int r, int g, int b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 u32 pitch, offset;
279 u32 fb_bpp, color;
280 RING_LOCALS;
281
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 switch (dev_priv->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 case 16:
284 fb_bpp = R128_GMC_DST_16BPP;
285 color = (((r & 0xf8) << 8) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288 case 24:
289 fb_bpp = R128_GMC_DST_24BPP;
290 color = ((r << 16) | (g << 8) | b);
291 break;
292 case 32:
293 fb_bpp = R128_GMC_DST_32BPP;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 break;
296 default:
297 return;
298 }
299
300 offset = dev_priv->back_offset;
301 pitch = dev_priv->back_pitch >> 3;
302
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
306 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
307 R128_GMC_BRUSH_SOLID_COLOR |
308 fb_bpp |
309 R128_GMC_SRC_DATATYPE_COLOR |
310 R128_ROP3_P |
311 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_AUX_CLIP_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 OUT_RING((pitch << 21) | (offset >> 5));
314 OUT_RING(color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 OUT_RING((x << 16) | y);
317 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 ADVANCE_RING();
320}
321
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200322static void r128_cce_performance_boxes(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200324 if (atomic_read(&dev_priv->idle_count) == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 r128_clear_box(dev_priv, 64, 4, 8, 8, 0, 255, 0);
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200326 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 atomic_set(&dev_priv->idle_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330#endif
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/* ================================================================
333 * CCE command dispatch functions
334 */
335
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336static void r128_print_dirty(const char *msg, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 DRM_INFO("%s: (0x%x) %s%s%s%s%s%s%s%s%s\n",
339 msg,
340 flags,
341 (flags & R128_UPLOAD_CORE) ? "core, " : "",
342 (flags & R128_UPLOAD_CONTEXT) ? "context, " : "",
343 (flags & R128_UPLOAD_SETUP) ? "setup, " : "",
344 (flags & R128_UPLOAD_TEX0) ? "tex0, " : "",
345 (flags & R128_UPLOAD_TEX1) ? "tex1, " : "",
346 (flags & R128_UPLOAD_MASKS) ? "masks, " : "",
347 (flags & R128_UPLOAD_WINDOW) ? "window, " : "",
348 (flags & R128_UPLOAD_CLIPRECTS) ? "cliprects, " : "",
349 (flags & R128_REQUIRE_QUIESCENCE) ? "quiescence, " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200352static void r128_cce_dispatch_clear(struct drm_device *dev,
353 drm_r128_clear_t *clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 drm_r128_private_t *dev_priv = dev->dev_private;
356 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
357 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000358 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned int flags = clear->flags;
360 int i;
361 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +1000362 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 if (dev_priv->page_flipping && dev_priv->current_page == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned int tmp = flags;
366
367 flags &= ~(R128_FRONT | R128_BACK);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368 if (tmp & R128_FRONT)
369 flags |= R128_BACK;
370 if (tmp & R128_BACK)
371 flags |= R128_FRONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000374 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int x = pbox[i].x1;
376 int y = pbox[i].y1;
377 int w = pbox[i].x2 - x;
378 int h = pbox[i].y2 - y;
379
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 DRM_DEBUG("dispatch clear %d,%d-%d,%d flags 0x%x\n",
381 pbox[i].x1, pbox[i].y1, pbox[i].x2,
382 pbox[i].y2, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000384 if (flags & (R128_FRONT | R128_BACK)) {
385 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 OUT_RING(CCE_PACKET0(R128_DP_WRITE_MASK, 0));
388 OUT_RING(clear->color_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 ADVANCE_RING();
391 }
392
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 if (flags & R128_FRONT) {
394 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
397 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
398 R128_GMC_BRUSH_SOLID_COLOR |
399 (dev_priv->color_fmt << 8) |
400 R128_GMC_SRC_DATATYPE_COLOR |
401 R128_ROP3_P |
402 R128_GMC_CLR_CMP_CNTL_DIS |
403 R128_GMC_AUX_CLIP_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405 OUT_RING(dev_priv->front_pitch_offset_c);
406 OUT_RING(clear->clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 OUT_RING((x << 16) | y);
409 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 ADVANCE_RING();
412 }
413
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 if (flags & R128_BACK) {
415 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
418 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
419 R128_GMC_BRUSH_SOLID_COLOR |
420 (dev_priv->color_fmt << 8) |
421 R128_GMC_SRC_DATATYPE_COLOR |
422 R128_ROP3_P |
423 R128_GMC_CLR_CMP_CNTL_DIS |
424 R128_GMC_AUX_CLIP_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 OUT_RING(dev_priv->back_pitch_offset_c);
427 OUT_RING(clear->clear_color);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 OUT_RING((x << 16) | y);
430 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 ADVANCE_RING();
433 }
434
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435 if (flags & R128_DEPTH) {
436 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
439 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
440 R128_GMC_BRUSH_SOLID_COLOR |
441 (dev_priv->depth_fmt << 8) |
442 R128_GMC_SRC_DATATYPE_COLOR |
443 R128_ROP3_P |
444 R128_GMC_CLR_CMP_CNTL_DIS |
445 R128_GMC_AUX_CLIP_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 OUT_RING(dev_priv->depth_pitch_offset_c);
448 OUT_RING(clear->clear_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000450 OUT_RING((x << 16) | y);
451 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 ADVANCE_RING();
454 }
455 }
456}
457
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200458static void r128_cce_dispatch_swap(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 drm_r128_private_t *dev_priv = dev->dev_private;
461 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
462 int nbox = sarea_priv->nbox;
Dave Airlieeddca552007-07-11 16:09:54 +1000463 struct drm_clip_rect *pbox = sarea_priv->boxes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int i;
465 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +1000466 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468#if R128_PERFORMANCE_BOXES
469 /* Do some trivial performance monitoring...
470 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000471 r128_cce_performance_boxes(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif
473
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 for (i = 0; i < nbox; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int x = pbox[i].x1;
476 int y = pbox[i].y1;
477 int w = pbox[i].x2 - x;
478 int h = pbox[i].y2 - y;
479
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000480 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
483 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
484 R128_GMC_DST_PITCH_OFFSET_CNTL |
485 R128_GMC_BRUSH_NONE |
486 (dev_priv->color_fmt << 8) |
487 R128_GMC_SRC_DATATYPE_COLOR |
488 R128_ROP3_S |
489 R128_DP_SRC_SOURCE_MEMORY |
490 R128_GMC_CLR_CMP_CNTL_DIS |
491 R128_GMC_AUX_CLIP_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* Make this work even if front & back are flipped:
494 */
495 if (dev_priv->current_page == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000496 OUT_RING(dev_priv->back_pitch_offset_c);
497 OUT_RING(dev_priv->front_pitch_offset_c);
498 } else {
499 OUT_RING(dev_priv->front_pitch_offset_c);
500 OUT_RING(dev_priv->back_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000503 OUT_RING((x << 16) | y);
504 OUT_RING((x << 16) | y);
505 OUT_RING((w << 16) | h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 ADVANCE_RING();
508 }
509
510 /* Increment the frame counter. The client-side 3D driver must
511 * throttle the framerate by waiting for this value before
512 * performing the swapbuffer ioctl.
513 */
514 dev_priv->sarea_priv->last_frame++;
515
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 OUT_RING(CCE_PACKET0(R128_LAST_FRAME_REG, 0));
519 OUT_RING(dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 ADVANCE_RING();
522}
523
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200524static void r128_cce_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 drm_r128_private_t *dev_priv = dev->dev_private;
527 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +1000528 DRM_DEBUG("page=%d pfCurrentPage=%d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529 dev_priv->current_page, dev_priv->sarea_priv->pfCurrentPage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531#if R128_PERFORMANCE_BOXES
532 /* Do some trivial performance monitoring...
533 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 r128_cce_performance_boxes(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#endif
536
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 BEGIN_RING(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 R128_WAIT_UNTIL_PAGE_FLIPPED();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 OUT_RING(CCE_PACKET0(R128_CRTC_OFFSET, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200542 if (dev_priv->current_page == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 OUT_RING(dev_priv->back_offset);
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200544 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 OUT_RING(dev_priv->front_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 ADVANCE_RING();
548
549 /* Increment the frame counter. The client-side 3D driver must
550 * throttle the framerate by waiting for this value before
551 * performing the swapbuffer ioctl.
552 */
553 dev_priv->sarea_priv->last_frame++;
554 dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 1 - dev_priv->current_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000557 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000559 OUT_RING(CCE_PACKET0(R128_LAST_FRAME_REG, 0));
560 OUT_RING(dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 ADVANCE_RING();
563}
564
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200565static void r128_cce_dispatch_vertex(struct drm_device *dev, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 drm_r128_private_t *dev_priv = dev->dev_private;
568 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
569 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
570 int format = sarea_priv->vc_format;
571 int offset = buf->bus_address;
572 int size = buf->used;
573 int prim = buf_priv->prim;
574 int i = 0;
575 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 DRM_DEBUG("buf=%d nbox=%d\n", buf->idx, sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 if (0)
579 r128_print_dirty("dispatch_vertex", sarea_priv->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000581 if (buf->used) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 buf_priv->dispatched = 1;
583
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200584 if (sarea_priv->dirty & ~R128_UPLOAD_CLIPRECTS)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000585 r128_emit_state(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 do {
588 /* Emit the next set of up to three cliprects */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 if (i < sarea_priv->nbox) {
590 r128_emit_clip_rects(dev_priv,
591 &sarea_priv->boxes[i],
592 sarea_priv->nbox - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 /* Emit the vertex buffer rendering commands */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 BEGIN_RING(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 OUT_RING(CCE_PACKET3(R128_3D_RNDR_GEN_INDX_PRIM, 3));
599 OUT_RING(offset);
600 OUT_RING(size);
601 OUT_RING(format);
602 OUT_RING(prim | R128_CCE_VC_CNTL_PRIM_WALK_LIST |
603 (size << R128_CCE_VC_CNTL_NUM_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 ADVANCE_RING();
606
607 i += 3;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 } while (i < sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
613
614 /* Emit the vertex buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
618 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 ADVANCE_RING();
621
622 buf->pending = 1;
623 buf->used = 0;
624 /* FIXME: Check dispatched field */
625 buf_priv->dispatched = 0;
626 }
627
628 dev_priv->sarea_priv->last_dispatch++;
629
630 sarea_priv->dirty &= ~R128_UPLOAD_CLIPRECTS;
631 sarea_priv->nbox = 0;
632}
633
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200634static void r128_cce_dispatch_indirect(struct drm_device *dev,
635 struct drm_buf *buf, int start, int end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 drm_r128_private_t *dev_priv = dev->dev_private;
638 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
639 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 DRM_DEBUG("indirect: buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int offset = buf->bus_address + start;
644 int dwords = (end - start + 3) / sizeof(u32);
645
646 /* Indirect buffer data must be an even number of
647 * dwords, so if we've been given an odd number we must
648 * pad the data with a Type-2 CCE packet.
649 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 if (dwords & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 u32 *data = (u32 *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 ((char *)dev->agp_buffer_map->handle
653 + buf->offset + start);
654 data[dwords++] = cpu_to_le32(R128_CCE_PACKET2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
657 buf_priv->dispatched = 1;
658
659 /* Fire off the indirect buffer */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 BEGIN_RING(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 OUT_RING(CCE_PACKET0(R128_PM4_IW_INDOFF, 1));
663 OUT_RING(offset);
664 OUT_RING(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 ADVANCE_RING();
667 }
668
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
671
672 /* Emit the indirect buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
676 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 ADVANCE_RING();
679
680 buf->pending = 1;
681 buf->used = 0;
682 /* FIXME: Check dispatched field */
683 buf_priv->dispatched = 0;
684 }
685
686 dev_priv->sarea_priv->last_dispatch++;
687}
688
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200689static void r128_cce_dispatch_indices(struct drm_device *dev,
690 struct drm_buf *buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 int start, int end, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 drm_r128_private_t *dev_priv = dev->dev_private;
694 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
695 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
696 int format = sarea_priv->vc_format;
697 int offset = dev->agp_buffer_map->offset - dev_priv->cce_buffers_offset;
698 int prim = buf_priv->prim;
699 u32 *data;
700 int dwords;
701 int i = 0;
702 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 DRM_DEBUG("indices: s=%d e=%d c=%d\n", start, end, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 if (0)
706 r128_print_dirty("dispatch_indices", sarea_priv->dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 if (start != end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 buf_priv->dispatched = 1;
710
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200711 if (sarea_priv->dirty & ~R128_UPLOAD_CLIPRECTS)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 r128_emit_state(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 dwords = (end - start + 3) / sizeof(u32);
715
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 data = (u32 *) ((char *)dev->agp_buffer_map->handle
717 + buf->offset + start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 data[0] = cpu_to_le32(CCE_PACKET3(R128_3D_RNDR_GEN_INDX_PRIM,
720 dwords - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 data[1] = cpu_to_le32(offset);
723 data[2] = cpu_to_le32(R128_MAX_VB_VERTS);
724 data[3] = cpu_to_le32(format);
725 data[4] = cpu_to_le32((prim | R128_CCE_VC_CNTL_PRIM_WALK_IND |
726 (count << 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 if (count & 0x1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729#ifdef __LITTLE_ENDIAN
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 data[dwords - 1] &= 0x0000ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 data[dwords - 1] &= 0xffff0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733#endif
734 }
735
736 do {
737 /* Emit the next set of up to three cliprects */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 if (i < sarea_priv->nbox) {
739 r128_emit_clip_rects(dev_priv,
740 &sarea_priv->boxes[i],
741 sarea_priv->nbox - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000744 r128_cce_dispatch_indirect(dev, buf, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 i += 3;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 } while (i < sarea_priv->nbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 if (buf_priv->discard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 buf_priv->age = dev_priv->sarea_priv->last_dispatch;
752
753 /* Emit the vertex buffer age */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000754 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 OUT_RING(CCE_PACKET0(R128_LAST_DISPATCH_REG, 0));
757 OUT_RING(buf_priv->age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 ADVANCE_RING();
760
761 buf->pending = 1;
762 /* FIXME: Check dispatched field */
763 buf_priv->dispatched = 0;
764 }
765
766 dev_priv->sarea_priv->last_dispatch++;
767
768 sarea_priv->dirty &= ~R128_UPLOAD_CLIPRECTS;
769 sarea_priv->nbox = 0;
770}
771
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200772static int r128_cce_dispatch_blit(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000773 struct drm_file *file_priv,
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200774 drm_r128_blit_t *blit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000777 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000778 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 drm_r128_buf_priv_t *buf_priv;
780 u32 *data;
781 int dword_shift, dwords;
782 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000783 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* The compiler won't optimize away a division by a variable,
786 * even if the only legal values are powers of two. Thus, we'll
787 * use a shift instead.
788 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 switch (blit->format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case R128_DATATYPE_ARGB8888:
791 dword_shift = 0;
792 break;
793 case R128_DATATYPE_ARGB1555:
794 case R128_DATATYPE_RGB565:
795 case R128_DATATYPE_ARGB4444:
796 case R128_DATATYPE_YVYU422:
797 case R128_DATATYPE_VYUY422:
798 dword_shift = 1;
799 break;
800 case R128_DATATYPE_CI8:
801 case R128_DATATYPE_RGB8:
802 dword_shift = 2;
803 break;
804 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000805 DRM_ERROR("invalid blit format %d\n", blit->format);
Eric Anholt20caafa2007-08-25 19:22:43 +1000806 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
809 /* Flush the pixel cache, and mark the contents as Read Invalid.
810 * This ensures no pixel data gets mixed up with the texture
811 * data from the host data blit, otherwise part of the texture
812 * image may be corrupted.
813 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 OUT_RING(CCE_PACKET0(R128_PC_GUI_CTLSTAT, 0));
817 OUT_RING(R128_PC_RI_GUI | R128_PC_FLUSH_GUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 ADVANCE_RING();
820
821 /* Dispatch the indirect buffer.
822 */
823 buf = dma->buflist[blit->idx];
824 buf_priv = buf->dev_private;
825
Eric Anholt6c340ea2007-08-25 20:23:09 +1000826 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +1000828 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +1000829 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 if (buf->pending) {
832 DRM_ERROR("sending pending buffer %d\n", blit->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +1000833 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 buf_priv->discard = 1;
837
838 dwords = (blit->width * blit->height) >> dword_shift;
839
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 data = (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 data[0] = cpu_to_le32(CCE_PACKET3(R128_CNTL_HOSTDATA_BLT, dwords + 6));
843 data[1] = cpu_to_le32((R128_GMC_DST_PITCH_OFFSET_CNTL |
844 R128_GMC_BRUSH_NONE |
845 (blit->format << 8) |
846 R128_GMC_SRC_DATATYPE_COLOR |
847 R128_ROP3_S |
848 R128_DP_SRC_SOURCE_HOST_DATA |
849 R128_GMC_CLR_CMP_CNTL_DIS |
850 R128_GMC_AUX_CLIP_DIS | R128_GMC_WR_MSK_DIS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 data[2] = cpu_to_le32((blit->pitch << 21) | (blit->offset >> 5));
853 data[3] = cpu_to_le32(0xffffffff);
854 data[4] = cpu_to_le32(0xffffffff);
855 data[5] = cpu_to_le32((blit->y << 16) | blit->x);
856 data[6] = cpu_to_le32((blit->height << 16) | blit->width);
857 data[7] = cpu_to_le32(dwords);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 buf->used = (dwords + 8) * sizeof(u32);
860
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 r128_cce_dispatch_indirect(dev, buf, 0, buf->used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* Flush the pixel cache after the blit completes. This ensures
864 * the texture data is written out to memory before rendering
865 * continues.
866 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 OUT_RING(CCE_PACKET0(R128_PC_GUI_CTLSTAT, 0));
870 OUT_RING(R128_PC_FLUSH_GUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 ADVANCE_RING();
873
874 return 0;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/* ================================================================
878 * Tiled depth buffer management
879 *
880 * FIXME: These should all set the destination write mask for when we
881 * have hardware stencil support.
882 */
883
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200884static int r128_cce_dispatch_write_span(struct drm_device *dev,
885 drm_r128_depth_t *depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 drm_r128_private_t *dev_priv = dev->dev_private;
888 int count, x, y;
889 u32 *buffer;
890 u8 *mask;
891 int i, buffer_size, mask_size;
892 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 count = depth->n;
896 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +1000897 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200899 if (DRM_COPY_FROM_USER(&x, depth->x, sizeof(x)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000900 return -EFAULT;
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200901 if (DRM_COPY_FROM_USER(&y, depth->y, sizeof(y)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000902 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 buffer_size = depth->n * sizeof(u32);
Eric Anholt9a298b22009-03-24 12:23:04 -0700905 buffer = kmalloc(buffer_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 if (buffer == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000907 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 if (DRM_COPY_FROM_USER(buffer, depth->buffer, buffer_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700909 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +1000910 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
913 mask_size = depth->n * sizeof(u8);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 if (depth->mask) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700915 mask = kmalloc(mask_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 if (mask == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700917 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +1000918 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 if (DRM_COPY_FROM_USER(mask, depth->mask, mask_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700921 kfree(buffer);
922 kfree(mask);
Eric Anholt20caafa2007-08-25 19:22:43 +1000923 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 for (i = 0; i < count; i++, x++) {
927 if (mask[i]) {
928 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
931 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
932 R128_GMC_BRUSH_SOLID_COLOR |
933 (dev_priv->depth_fmt << 8) |
934 R128_GMC_SRC_DATATYPE_COLOR |
935 R128_ROP3_P |
936 R128_GMC_CLR_CMP_CNTL_DIS |
937 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 OUT_RING(dev_priv->depth_pitch_offset_c);
940 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 OUT_RING((x << 16) | y);
943 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 ADVANCE_RING();
946 }
947 }
948
Eric Anholt9a298b22009-03-24 12:23:04 -0700949 kfree(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 for (i = 0; i < count; i++, x++) {
952 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
955 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
956 R128_GMC_BRUSH_SOLID_COLOR |
957 (dev_priv->depth_fmt << 8) |
958 R128_GMC_SRC_DATATYPE_COLOR |
959 R128_ROP3_P |
960 R128_GMC_CLR_CMP_CNTL_DIS |
961 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 OUT_RING(dev_priv->depth_pitch_offset_c);
964 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000966 OUT_RING((x << 16) | y);
967 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 ADVANCE_RING();
970 }
971 }
972
Eric Anholt9a298b22009-03-24 12:23:04 -0700973 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 return 0;
976}
977
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200978static int r128_cce_dispatch_write_pixels(struct drm_device *dev,
979 drm_r128_depth_t *depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 drm_r128_private_t *dev_priv = dev->dev_private;
982 int count, *x, *y;
983 u32 *buffer;
984 u8 *mask;
985 int i, xbuf_size, ybuf_size, buffer_size, mask_size;
986 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 count = depth->n;
990 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +1000991 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 xbuf_size = count * sizeof(*x);
994 ybuf_size = count * sizeof(*y);
Eric Anholt9a298b22009-03-24 12:23:04 -0700995 x = kmalloc(xbuf_size, GFP_KERNEL);
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200996 if (x == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000997 return -ENOMEM;
Eric Anholt9a298b22009-03-24 12:23:04 -0700998 y = kmalloc(ybuf_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 if (y == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001000 kfree(x);
Eric Anholt20caafa2007-08-25 19:22:43 +10001001 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 if (DRM_COPY_FROM_USER(x, depth->x, xbuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001004 kfree(x);
1005 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001006 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 if (DRM_COPY_FROM_USER(y, depth->y, xbuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001009 kfree(x);
1010 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001011 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
1014 buffer_size = depth->n * sizeof(u32);
Eric Anholt9a298b22009-03-24 12:23:04 -07001015 buffer = kmalloc(buffer_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 if (buffer == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001017 kfree(x);
1018 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001019 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 if (DRM_COPY_FROM_USER(buffer, depth->buffer, buffer_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001022 kfree(x);
1023 kfree(y);
1024 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +10001025 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 if (depth->mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 mask_size = depth->n * sizeof(u8);
Eric Anholt9a298b22009-03-24 12:23:04 -07001030 mask = kmalloc(mask_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 if (mask == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001032 kfree(x);
1033 kfree(y);
1034 kfree(buffer);
Eric Anholt20caafa2007-08-25 19:22:43 +10001035 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 if (DRM_COPY_FROM_USER(mask, depth->mask, mask_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001038 kfree(x);
1039 kfree(y);
1040 kfree(buffer);
1041 kfree(mask);
Eric Anholt20caafa2007-08-25 19:22:43 +10001042 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 for (i = 0; i < count; i++) {
1046 if (mask[i]) {
1047 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
1050 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
1051 R128_GMC_BRUSH_SOLID_COLOR |
1052 (dev_priv->depth_fmt << 8) |
1053 R128_GMC_SRC_DATATYPE_COLOR |
1054 R128_ROP3_P |
1055 R128_GMC_CLR_CMP_CNTL_DIS |
1056 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 OUT_RING(dev_priv->depth_pitch_offset_c);
1059 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 OUT_RING((x[i] << 16) | y[i]);
1062 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 ADVANCE_RING();
1065 }
1066 }
1067
Eric Anholt9a298b22009-03-24 12:23:04 -07001068 kfree(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 for (i = 0; i < count; i++) {
1071 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 OUT_RING(CCE_PACKET3(R128_CNTL_PAINT_MULTI, 4));
1074 OUT_RING(R128_GMC_DST_PITCH_OFFSET_CNTL |
1075 R128_GMC_BRUSH_SOLID_COLOR |
1076 (dev_priv->depth_fmt << 8) |
1077 R128_GMC_SRC_DATATYPE_COLOR |
1078 R128_ROP3_P |
1079 R128_GMC_CLR_CMP_CNTL_DIS |
1080 R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 OUT_RING(dev_priv->depth_pitch_offset_c);
1083 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 OUT_RING((x[i] << 16) | y[i]);
1086 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 ADVANCE_RING();
1089 }
1090 }
1091
Eric Anholt9a298b22009-03-24 12:23:04 -07001092 kfree(x);
1093 kfree(y);
1094 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 return 0;
1097}
1098
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001099static int r128_cce_dispatch_read_span(struct drm_device *dev,
1100 drm_r128_depth_t *depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 drm_r128_private_t *dev_priv = dev->dev_private;
1103 int count, x, y;
1104 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 count = depth->n;
1108 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +10001109 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001111 if (DRM_COPY_FROM_USER(&x, depth->x, sizeof(x)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001112 return -EFAULT;
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001113 if (DRM_COPY_FROM_USER(&y, depth->y, sizeof(y)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001114 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
1119 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
1120 R128_GMC_DST_PITCH_OFFSET_CNTL |
1121 R128_GMC_BRUSH_NONE |
1122 (dev_priv->depth_fmt << 8) |
1123 R128_GMC_SRC_DATATYPE_COLOR |
1124 R128_ROP3_S |
1125 R128_DP_SRC_SOURCE_MEMORY |
1126 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 OUT_RING(dev_priv->depth_pitch_offset_c);
1129 OUT_RING(dev_priv->span_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 OUT_RING((x << 16) | y);
1132 OUT_RING((0 << 16) | 0);
1133 OUT_RING((count << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 ADVANCE_RING();
1136
1137 return 0;
1138}
1139
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001140static int r128_cce_dispatch_read_pixels(struct drm_device *dev,
1141 drm_r128_depth_t *depth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 drm_r128_private_t *dev_priv = dev->dev_private;
1144 int count, *x, *y;
1145 int i, xbuf_size, ybuf_size;
1146 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001147 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 count = depth->n;
1150 if (count > 4096 || count <= 0)
Eric Anholt20caafa2007-08-25 19:22:43 +10001151 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001153 if (count > dev_priv->depth_pitch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 count = dev_priv->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 xbuf_size = count * sizeof(*x);
1157 ybuf_size = count * sizeof(*y);
Eric Anholt9a298b22009-03-24 12:23:04 -07001158 x = kmalloc(xbuf_size, GFP_KERNEL);
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001159 if (x == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001160 return -ENOMEM;
Eric Anholt9a298b22009-03-24 12:23:04 -07001161 y = kmalloc(ybuf_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001162 if (y == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001163 kfree(x);
Eric Anholt20caafa2007-08-25 19:22:43 +10001164 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001166 if (DRM_COPY_FROM_USER(x, depth->x, xbuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001167 kfree(x);
1168 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001169 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 if (DRM_COPY_FROM_USER(y, depth->y, ybuf_size)) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001172 kfree(x);
1173 kfree(y);
Eric Anholt20caafa2007-08-25 19:22:43 +10001174 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001177 for (i = 0; i < count; i++) {
1178 BEGIN_RING(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 OUT_RING(CCE_PACKET3(R128_CNTL_BITBLT_MULTI, 5));
1181 OUT_RING(R128_GMC_SRC_PITCH_OFFSET_CNTL |
1182 R128_GMC_DST_PITCH_OFFSET_CNTL |
1183 R128_GMC_BRUSH_NONE |
1184 (dev_priv->depth_fmt << 8) |
1185 R128_GMC_SRC_DATATYPE_COLOR |
1186 R128_ROP3_S |
1187 R128_DP_SRC_SOURCE_MEMORY |
1188 R128_GMC_CLR_CMP_CNTL_DIS | R128_GMC_WR_MSK_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 OUT_RING(dev_priv->depth_pitch_offset_c);
1191 OUT_RING(dev_priv->span_pitch_offset_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001193 OUT_RING((x[i] << 16) | y[i]);
1194 OUT_RING((i << 16) | 0);
1195 OUT_RING((1 << 16) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 ADVANCE_RING();
1198 }
1199
Eric Anholt9a298b22009-03-24 12:23:04 -07001200 kfree(x);
1201 kfree(y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 return 0;
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/* ================================================================
1207 * Polygon stipple
1208 */
1209
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001210static void r128_cce_dispatch_stipple(struct drm_device *dev, u32 *stipple)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 drm_r128_private_t *dev_priv = dev->dev_private;
1213 int i;
1214 RING_LOCALS;
Márton Németh3e684ea2008-01-24 15:58:57 +10001215 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001217 BEGIN_RING(33);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219 OUT_RING(CCE_PACKET0(R128_BRUSH_DATA0, 31));
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001220 for (i = 0; i < 32; i++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001221 OUT_RING(stipple[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 ADVANCE_RING();
1224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/* ================================================================
1227 * IOCTL functions
1228 */
1229
Eric Anholtc153f452007-09-03 12:06:45 +10001230static int r128_cce_clear(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 drm_r128_private_t *dev_priv = dev->dev_private;
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001233 drm_r128_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001234 drm_r128_clear_t *clear = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001235 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Eric Anholt6c340ea2007-08-25 20:23:09 +10001237 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001239 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1240
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001241 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001243 sarea_priv = dev_priv->sarea_priv;
1244
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001245 if (sarea_priv->nbox > R128_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 sarea_priv->nbox = R128_NR_SAREA_CLIPRECTS;
1247
Eric Anholtc153f452007-09-03 12:06:45 +10001248 r128_cce_dispatch_clear(dev, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 COMMIT_RING();
1250
1251 /* Make sure we restore the 3D state next time.
1252 */
1253 dev_priv->sarea_priv->dirty |= R128_UPLOAD_CONTEXT | R128_UPLOAD_MASKS;
1254
1255 return 0;
1256}
1257
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001258static int r128_do_init_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001261 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001263 dev_priv->crtc_offset = R128_READ(R128_CRTC_OFFSET);
1264 dev_priv->crtc_offset_cntl = R128_READ(R128_CRTC_OFFSET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001266 R128_WRITE(R128_CRTC_OFFSET, dev_priv->front_offset);
1267 R128_WRITE(R128_CRTC_OFFSET_CNTL,
1268 dev_priv->crtc_offset_cntl | R128_CRTC_OFFSET_FLIP_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 dev_priv->page_flipping = 1;
1271 dev_priv->current_page = 0;
1272 dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page;
1273
1274 return 0;
1275}
1276
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001277static int r128_do_cleanup_pageflip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001280 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001282 R128_WRITE(R128_CRTC_OFFSET, dev_priv->crtc_offset);
1283 R128_WRITE(R128_CRTC_OFFSET_CNTL, dev_priv->crtc_offset_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (dev_priv->current_page != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001286 r128_cce_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 COMMIT_RING();
1288 }
1289
1290 dev_priv->page_flipping = 0;
1291 return 0;
1292}
1293
1294/* Swapping and flipping are different operations, need different ioctls.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001295 * They can & should be intermixed to support multiple 3d windows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297
Eric Anholtc153f452007-09-03 12:06:45 +10001298static int r128_cce_flip(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 drm_r128_private_t *dev_priv = dev->dev_private;
Márton Németh3e684ea2008-01-24 15:58:57 +10001301 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Eric Anholt6c340ea2007-08-25 20:23:09 +10001303 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001305 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1306
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001309 if (!dev_priv->page_flipping)
1310 r128_do_init_pageflip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 r128_cce_dispatch_flip(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 COMMIT_RING();
1315 return 0;
1316}
1317
Eric Anholtc153f452007-09-03 12:06:45 +10001318static int r128_cce_swap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 drm_r128_private_t *dev_priv = dev->dev_private;
1321 drm_r128_sarea_t *sarea_priv = dev_priv->sarea_priv;
Márton Németh3e684ea2008-01-24 15:58:57 +10001322 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Eric Anholt6c340ea2007-08-25 20:23:09 +10001324 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001326 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1327
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001328 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330 if (sarea_priv->nbox > R128_NR_SAREA_CLIPRECTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 sarea_priv->nbox = R128_NR_SAREA_CLIPRECTS;
1332
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 r128_cce_dispatch_swap(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 dev_priv->sarea_priv->dirty |= (R128_UPLOAD_CONTEXT |
1335 R128_UPLOAD_MASKS);
1336
1337 COMMIT_RING();
1338 return 0;
1339}
1340
Eric Anholtc153f452007-09-03 12:06:45 +10001341static int r128_cce_vertex(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001344 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001345 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 drm_r128_buf_priv_t *buf_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001347 drm_r128_vertex_t *vertex = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Eric Anholt6c340ea2007-08-25 20:23:09 +10001349 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001351 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001353 DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001354 DRM_CURRENTPID, vertex->idx, vertex->count, vertex->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Eric Anholtc153f452007-09-03 12:06:45 +10001356 if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001357 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001358 vertex->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Eric Anholtc153f452007-09-03 12:06:45 +10001361 if (vertex->prim < 0 ||
1362 vertex->prim > R128_CCE_VC_CNTL_PRIM_TYPE_TRI_TYPE2) {
1363 DRM_ERROR("buffer prim %d\n", vertex->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10001364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001367 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1368 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Eric Anholtc153f452007-09-03 12:06:45 +10001370 buf = dma->buflist[vertex->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 buf_priv = buf->dev_private;
1372
Eric Anholt6c340ea2007-08-25 20:23:09 +10001373 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001374 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001375 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10001379 DRM_ERROR("sending pending buffer %d\n", vertex->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001380 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Eric Anholtc153f452007-09-03 12:06:45 +10001383 buf->used = vertex->count;
1384 buf_priv->prim = vertex->prim;
1385 buf_priv->discard = vertex->discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 r128_cce_dispatch_vertex(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 COMMIT_RING();
1390 return 0;
1391}
1392
Eric Anholtc153f452007-09-03 12:06:45 +10001393static int r128_cce_indices(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001396 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001397 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 drm_r128_buf_priv_t *buf_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001399 drm_r128_indices_t *elts = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int count;
1401
Eric Anholt6c340ea2007-08-25 20:23:09 +10001402 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001404 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 DRM_DEBUG("pid=%d buf=%d s=%d e=%d d=%d\n", DRM_CURRENTPID,
Eric Anholtc153f452007-09-03 12:06:45 +10001407 elts->idx, elts->start, elts->end, elts->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Eric Anholtc153f452007-09-03 12:06:45 +10001409 if (elts->idx < 0 || elts->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001410 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001411 elts->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Eric Anholtc153f452007-09-03 12:06:45 +10001414 if (elts->prim < 0 ||
1415 elts->prim > R128_CCE_VC_CNTL_PRIM_TYPE_TRI_TYPE2) {
1416 DRM_ERROR("buffer prim %d\n", elts->prim);
Eric Anholt20caafa2007-08-25 19:22:43 +10001417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001420 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1421 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Eric Anholtc153f452007-09-03 12:06:45 +10001423 buf = dma->buflist[elts->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 buf_priv = buf->dev_private;
1425
Eric Anholt6c340ea2007-08-25 20:23:09 +10001426 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001427 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001428 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001431 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10001432 DRM_ERROR("sending pending buffer %d\n", elts->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Eric Anholtc153f452007-09-03 12:06:45 +10001436 count = (elts->end - elts->start) / sizeof(u16);
1437 elts->start -= R128_INDEX_PRIM_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Eric Anholtc153f452007-09-03 12:06:45 +10001439 if (elts->start & 0x7) {
1440 DRM_ERROR("misaligned buffer 0x%x\n", elts->start);
Eric Anholt20caafa2007-08-25 19:22:43 +10001441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
Eric Anholtc153f452007-09-03 12:06:45 +10001443 if (elts->start < buf->used) {
1444 DRM_ERROR("no header 0x%x - 0x%x\n", elts->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10001445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
Eric Anholtc153f452007-09-03 12:06:45 +10001448 buf->used = elts->end;
1449 buf_priv->prim = elts->prim;
1450 buf_priv->discard = elts->discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Eric Anholtc153f452007-09-03 12:06:45 +10001452 r128_cce_dispatch_indices(dev, buf, elts->start, elts->end, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 COMMIT_RING();
1455 return 0;
1456}
1457
Eric Anholtc153f452007-09-03 12:06:45 +10001458static int r128_cce_blit(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Dave Airliecdd55a22007-07-11 16:32:08 +10001460 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001462 drm_r128_blit_t *blit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int ret;
1464
Eric Anholt6c340ea2007-08-25 20:23:09 +10001465 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001467 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1468
Eric Anholtc153f452007-09-03 12:06:45 +10001469 DRM_DEBUG("pid=%d index=%d\n", DRM_CURRENTPID, blit->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Eric Anholtc153f452007-09-03 12:06:45 +10001471 if (blit->idx < 0 || blit->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001472 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001473 blit->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001474 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001477 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1478 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Eric Anholtc153f452007-09-03 12:06:45 +10001480 ret = r128_cce_dispatch_blit(dev, file_priv, blit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 COMMIT_RING();
1483 return ret;
1484}
1485
Eric Anholtc153f452007-09-03 12:06:45 +10001486static int r128_cce_depth(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001489 drm_r128_depth_t *depth = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 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
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001494 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1495
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001496 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Eric Anholt20caafa2007-08-25 19:22:43 +10001498 ret = -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001499 switch (depth->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 case R128_WRITE_SPAN:
Eric Anholtc153f452007-09-03 12:06:45 +10001501 ret = r128_cce_dispatch_write_span(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 case R128_WRITE_PIXELS:
Eric Anholtc153f452007-09-03 12:06:45 +10001504 ret = r128_cce_dispatch_write_pixels(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 case R128_READ_SPAN:
Eric Anholtc153f452007-09-03 12:06:45 +10001507 ret = r128_cce_dispatch_read_span(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 case R128_READ_PIXELS:
Eric Anholtc153f452007-09-03 12:06:45 +10001510 ret = r128_cce_dispatch_read_pixels(dev, depth);
Dave Airlie41aac242005-04-16 15:24:04 -07001511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
1514 COMMIT_RING();
1515 return ret;
1516}
1517
Eric Anholtc153f452007-09-03 12:06:45 +10001518static int r128_cce_stipple(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001521 drm_r128_stipple_t *stipple = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 u32 mask[32];
1523
Eric Anholt6c340ea2007-08-25 20:23:09 +10001524 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001526 DEV_INIT_TEST_WITH_RETURN(dev_priv);
1527
Eric Anholtc153f452007-09-03 12:06:45 +10001528 if (DRM_COPY_FROM_USER(&mask, stipple->mask, 32 * sizeof(u32)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001529 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001531 RING_SPACE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 r128_cce_dispatch_stipple(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 COMMIT_RING();
1536 return 0;
1537}
1538
Eric Anholtc153f452007-09-03 12:06:45 +10001539static int r128_cce_indirect(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +10001542 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +10001543 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 drm_r128_buf_priv_t *buf_priv;
Eric Anholtc153f452007-09-03 12:06:45 +10001545 drm_r128_indirect_t *indirect = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546#if 0
1547 RING_LOCALS;
1548#endif
1549
Eric Anholt6c340ea2007-08-25 20:23:09 +10001550 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001552 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Márton Németh3e684ea2008-01-24 15:58:57 +10001554 DRM_DEBUG("idx=%d s=%d e=%d d=%d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001555 indirect->idx, indirect->start, indirect->end,
1556 indirect->discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Eric Anholtc153f452007-09-03 12:06:45 +10001558 if (indirect->idx < 0 || indirect->idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001559 DRM_ERROR("buffer index %d (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001560 indirect->idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +10001561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563
Eric Anholtc153f452007-09-03 12:06:45 +10001564 buf = dma->buflist[indirect->idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 buf_priv = buf->dev_private;
1566
Eric Anholt6c340ea2007-08-25 20:23:09 +10001567 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001568 DRM_ERROR("process %d using buffer owned by %p\n",
Eric Anholt6c340ea2007-08-25 20:23:09 +10001569 DRM_CURRENTPID, buf->file_priv);
Eric Anholt20caafa2007-08-25 19:22:43 +10001570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001572 if (buf->pending) {
Eric Anholtc153f452007-09-03 12:06:45 +10001573 DRM_ERROR("sending pending buffer %d\n", indirect->idx);
Eric Anholt20caafa2007-08-25 19:22:43 +10001574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576
Eric Anholtc153f452007-09-03 12:06:45 +10001577 if (indirect->start < buf->used) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001579 indirect->start, buf->used);
Eric Anholt20caafa2007-08-25 19:22:43 +10001580 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 RING_SPACE_TEST_WITH_RETURN(dev_priv);
1584 VB_AGE_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Eric Anholtc153f452007-09-03 12:06:45 +10001586 buf->used = indirect->end;
1587 buf_priv->discard = indirect->discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589#if 0
1590 /* Wait for the 3D stream to idle before the indirect buffer
1591 * containing 2D acceleration commands is processed.
1592 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001593 BEGIN_RING(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 RADEON_WAIT_UNTIL_3D_IDLE();
1595 ADVANCE_RING();
1596#endif
1597
1598 /* Dispatch the indirect buffer full of commands from the
1599 * X server. This is insecure and is thus only available to
1600 * privileged clients.
1601 */
Eric Anholtc153f452007-09-03 12:06:45 +10001602 r128_cce_dispatch_indirect(dev, buf, indirect->start, indirect->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 COMMIT_RING();
1605 return 0;
1606}
1607
Eric Anholtc153f452007-09-03 12:06:45 +10001608static int r128_getparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001611 drm_r128_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int value;
1613
Ben Hutchings7dc482d2009-08-23 16:59:04 +01001614 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001616 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Eric Anholtc153f452007-09-03 12:06:45 +10001618 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 case R128_PARAM_IRQ_NR:
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -07001620 value = drm_dev_to_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 break;
1622 default:
Eric Anholt20caafa2007-08-25 19:22:43 +10001623 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
Eric Anholtc153f452007-09-03 12:06:45 +10001626 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001627 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001628 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632}
1633
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001634void r128_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001636 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 drm_r128_private_t *dev_priv = dev->dev_private;
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001638 if (dev_priv->page_flipping)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001639 r128_do_cleanup_pageflip(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +02001642void r128_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001644 r128_do_cleanup_cce(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Eric Anholtc153f452007-09-03 12:06:45 +10001647struct drm_ioctl_desc r128_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001648 DRM_IOCTL_DEF_DRV(R128_INIT, r128_cce_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1649 DRM_IOCTL_DEF_DRV(R128_CCE_START, r128_cce_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1650 DRM_IOCTL_DEF_DRV(R128_CCE_STOP, r128_cce_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1651 DRM_IOCTL_DEF_DRV(R128_CCE_RESET, r128_cce_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1652 DRM_IOCTL_DEF_DRV(R128_CCE_IDLE, r128_cce_idle, DRM_AUTH),
1653 DRM_IOCTL_DEF_DRV(R128_RESET, r128_engine_reset, DRM_AUTH),
1654 DRM_IOCTL_DEF_DRV(R128_FULLSCREEN, r128_fullscreen, DRM_AUTH),
1655 DRM_IOCTL_DEF_DRV(R128_SWAP, r128_cce_swap, DRM_AUTH),
1656 DRM_IOCTL_DEF_DRV(R128_FLIP, r128_cce_flip, DRM_AUTH),
1657 DRM_IOCTL_DEF_DRV(R128_CLEAR, r128_cce_clear, DRM_AUTH),
1658 DRM_IOCTL_DEF_DRV(R128_VERTEX, r128_cce_vertex, DRM_AUTH),
1659 DRM_IOCTL_DEF_DRV(R128_INDICES, r128_cce_indices, DRM_AUTH),
1660 DRM_IOCTL_DEF_DRV(R128_BLIT, r128_cce_blit, DRM_AUTH),
1661 DRM_IOCTL_DEF_DRV(R128_DEPTH, r128_cce_depth, DRM_AUTH),
1662 DRM_IOCTL_DEF_DRV(R128_STIPPLE, r128_cce_stipple, DRM_AUTH),
1663 DRM_IOCTL_DEF_DRV(R128_INDIRECT, r128_cce_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1664 DRM_IOCTL_DEF_DRV(R128_GETPARAM, r128_getparam, DRM_AUTH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665};
1666
1667int r128_max_ioctl = DRM_ARRAY_SIZE(r128_ioctls);