blob: 1c5a0e2eace0ff4a508a0fc042a0f771e1a7aef5 [file] [log] [blame]
Felix Kuehling04842e82005-01-01 20:22:58 +00001/* savage_state.c -- State and drawing support for Savage
2 *
3 * Copyright 2004 Felix Kuehling
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
22 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25#include "drmP.h"
26#include "savage_drm.h"
27#include "savage_drv.h"
28
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000029void savage_emit_clip_rect_s3d(drm_savage_private_t *dev_priv,
Dave Airlieb95ac8b2007-07-16 11:22:15 +100030 const struct drm_clip_rect *pbox)
Felix Kuehling04842e82005-01-01 20:22:58 +000031{
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000032 uint32_t scstart = dev_priv->state.s3d.new_scstart;
Dave Airlie5dc5c362007-11-22 16:10:36 +100033 uint32_t scend = dev_priv->state.s3d.new_scend;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000034 scstart = (scstart & ~SAVAGE_SCISSOR_MASK_S3D) |
Dave Airlie7f6bf842007-11-05 12:42:22 +100035 ((uint32_t)pbox->x1 & 0x000007ff) |
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000036 (((uint32_t)pbox->y1 << 16) & 0x07ff0000);
Dave Airlie5dc5c362007-11-22 16:10:36 +100037 scend = (scend & ~SAVAGE_SCISSOR_MASK_S3D) |
38 (((uint32_t)pbox->x2 - 1) & 0x000007ff) |
39 ((((uint32_t)pbox->y2 - 1) << 16) & 0x07ff0000);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000040 if (scstart != dev_priv->state.s3d.scstart ||
41 scend != dev_priv->state.s3d.scend) {
Felix Kuehling15995232005-03-06 01:41:06 +000042 DMA_LOCALS;
43 BEGIN_DMA(4);
Dave Airlie5dc5c362007-11-22 16:10:36 +100044 DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
Felix Kuehling15995232005-03-06 01:41:06 +000045 DMA_SET_REGISTERS(SAVAGE_SCSTART_S3D, 2);
46 DMA_WRITE(scstart);
47 DMA_WRITE(scend);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000048 dev_priv->state.s3d.scstart = scstart;
Dave Airlie5dc5c362007-11-22 16:10:36 +100049 dev_priv->state.s3d.scend = scend;
Felix Kuehlingd6af9022005-01-09 19:49:21 +000050 dev_priv->waiting = 1;
Felix Kuehling15995232005-03-06 01:41:06 +000051 DMA_COMMIT();
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000052 }
Felix Kuehling04842e82005-01-01 20:22:58 +000053}
54
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000055void savage_emit_clip_rect_s4(drm_savage_private_t *dev_priv,
Dave Airlieb95ac8b2007-07-16 11:22:15 +100056 const struct drm_clip_rect *pbox)
Felix Kuehling04842e82005-01-01 20:22:58 +000057{
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000058 uint32_t drawctrl0 = dev_priv->state.s4.new_drawctrl0;
59 uint32_t drawctrl1 = dev_priv->state.s4.new_drawctrl1;
60 drawctrl0 = (drawctrl0 & ~SAVAGE_SCISSOR_MASK_S4) |
61 ((uint32_t)pbox->x1 & 0x000007ff) |
62 (((uint32_t)pbox->y1 << 12) & 0x00fff000);
63 drawctrl1 = (drawctrl1 & ~SAVAGE_SCISSOR_MASK_S4) |
Dave Airlie5dc5c362007-11-22 16:10:36 +100064 (((uint32_t)pbox->x2 - 1) & 0x000007ff) |
65 ((((uint32_t)pbox->y2 - 1) << 12) & 0x00fff000);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000066 if (drawctrl0 != dev_priv->state.s4.drawctrl0 ||
67 drawctrl1 != dev_priv->state.s4.drawctrl1) {
Felix Kuehling15995232005-03-06 01:41:06 +000068 DMA_LOCALS;
69 BEGIN_DMA(4);
Dave Airlie5dc5c362007-11-22 16:10:36 +100070 DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
Felix Kuehling15995232005-03-06 01:41:06 +000071 DMA_SET_REGISTERS(SAVAGE_DRAWCTRL0_S4, 2);
72 DMA_WRITE(drawctrl0);
73 DMA_WRITE(drawctrl1);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000074 dev_priv->state.s4.drawctrl0 = drawctrl0;
75 dev_priv->state.s4.drawctrl1 = drawctrl1;
Felix Kuehlingd6af9022005-01-09 19:49:21 +000076 dev_priv->waiting = 1;
Felix Kuehling15995232005-03-06 01:41:06 +000077 DMA_COMMIT();
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +000078 }
Felix Kuehling04842e82005-01-01 20:22:58 +000079}
80
81static int savage_verify_texaddr(drm_savage_private_t *dev_priv, int unit,
82 uint32_t addr)
83{
84 if ((addr & 6) != 2) { /* reserved bits */
85 DRM_ERROR("bad texAddr%d %08x (reserved bits)\n", unit, addr);
Eric Anholte39286e2007-07-19 17:00:17 -070086 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +000087 }
88 if (!(addr & 1)) { /* local */
89 addr &= ~7;
Dave Airlie5dc5c362007-11-22 16:10:36 +100090 if (addr < dev_priv->texture_offset ||
91 addr >= dev_priv->texture_offset + dev_priv->texture_size) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +000092 DRM_ERROR
93 ("bad texAddr%d %08x (local addr out of range)\n",
94 unit, addr);
Eric Anholte39286e2007-07-19 17:00:17 -070095 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +000096 }
97 } else { /* AGP */
98 if (!dev_priv->agp_textures) {
99 DRM_ERROR("bad texAddr%d %08x (AGP not available)\n",
100 unit, addr);
Eric Anholte39286e2007-07-19 17:00:17 -0700101 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000102 }
103 addr &= ~7;
104 if (addr < dev_priv->agp_textures->offset ||
105 addr >= (dev_priv->agp_textures->offset +
106 dev_priv->agp_textures->size)) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000107 DRM_ERROR
108 ("bad texAddr%d %08x (AGP addr out of range)\n",
109 unit, addr);
Eric Anholte39286e2007-07-19 17:00:17 -0700110 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000111 }
112 }
113 return 0;
114}
115
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000116#define SAVE_STATE(reg,where) \
Dave Airlie5dc5c362007-11-22 16:10:36 +1000117 if(start <= reg && start + count > reg) \
Eric Anholt6481a2e2005-11-08 21:40:03 +0000118 dev_priv->state.where = regs[reg - start]
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000119#define SAVE_STATE_MASK(reg,where,mask) do { \
Dave Airlie5dc5c362007-11-22 16:10:36 +1000120 if(start <= reg && start + count > reg) { \
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000121 uint32_t tmp; \
Eric Anholt6481a2e2005-11-08 21:40:03 +0000122 tmp = regs[reg - start]; \
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000123 dev_priv->state.where = (tmp & (mask)) | \
124 (dev_priv->state.where & ~(mask)); \
125 } \
126} while (0)
Felix Kuehling04842e82005-01-01 20:22:58 +0000127static int savage_verify_state_s3d(drm_savage_private_t *dev_priv,
128 unsigned int start, unsigned int count,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000129 const uint32_t *regs)
Felix Kuehling04842e82005-01-01 20:22:58 +0000130{
131 if (start < SAVAGE_TEXPALADDR_S3D ||
Dave Airlie5dc5c362007-11-22 16:10:36 +1000132 start + count - 1 > SAVAGE_DESTTEXRWWATERMARK_S3D) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000133 DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
Dave Airlie5dc5c362007-11-22 16:10:36 +1000134 start, start + count - 1);
Eric Anholte39286e2007-07-19 17:00:17 -0700135 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000136 }
137
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000138 SAVE_STATE_MASK(SAVAGE_SCSTART_S3D, s3d.new_scstart,
139 ~SAVAGE_SCISSOR_MASK_S3D);
140 SAVE_STATE_MASK(SAVAGE_SCEND_S3D, s3d.new_scend,
141 ~SAVAGE_SCISSOR_MASK_S3D);
Felix Kuehling04842e82005-01-01 20:22:58 +0000142
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000143 /* if any texture regs were changed ... */
144 if (start <= SAVAGE_TEXCTRL_S3D &&
Dave Airlie5dc5c362007-11-22 16:10:36 +1000145 start + count > SAVAGE_TEXPALADDR_S3D) {
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000146 /* ... check texture state */
147 SAVE_STATE(SAVAGE_TEXCTRL_S3D, s3d.texctrl);
148 SAVE_STATE(SAVAGE_TEXADDR_S3D, s3d.texaddr);
149 if (dev_priv->state.s3d.texctrl & SAVAGE_TEXCTRL_TEXEN_MASK)
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000150 return savage_verify_texaddr(dev_priv, 0,
151 dev_priv->state.s3d.texaddr);
Felix Kuehling04842e82005-01-01 20:22:58 +0000152 }
153
154 return 0;
155}
156
157static int savage_verify_state_s4(drm_savage_private_t *dev_priv,
158 unsigned int start, unsigned int count,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000159 const uint32_t *regs)
Felix Kuehling04842e82005-01-01 20:22:58 +0000160{
161 int ret = 0;
162
163 if (start < SAVAGE_DRAWLOCALCTRL_S4 ||
Dave Airlie5dc5c362007-11-22 16:10:36 +1000164 start + count - 1 > SAVAGE_TEXBLENDCOLOR_S4) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000165 DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
Dave Airlie5dc5c362007-11-22 16:10:36 +1000166 start, start + count - 1);
Eric Anholte39286e2007-07-19 17:00:17 -0700167 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000168 }
169
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000170 SAVE_STATE_MASK(SAVAGE_DRAWCTRL0_S4, s4.new_drawctrl0,
171 ~SAVAGE_SCISSOR_MASK_S4);
172 SAVE_STATE_MASK(SAVAGE_DRAWCTRL1_S4, s4.new_drawctrl1,
173 ~SAVAGE_SCISSOR_MASK_S4);
Felix Kuehling04842e82005-01-01 20:22:58 +0000174
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000175 /* if any texture regs were changed ... */
176 if (start <= SAVAGE_TEXDESCR_S4 &&
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000177 start + count > SAVAGE_TEXPALADDR_S4) {
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000178 /* ... check texture state */
179 SAVE_STATE(SAVAGE_TEXDESCR_S4, s4.texdescr);
180 SAVE_STATE(SAVAGE_TEXADDR0_S4, s4.texaddr0);
181 SAVE_STATE(SAVAGE_TEXADDR1_S4, s4.texaddr1);
182 if (dev_priv->state.s4.texdescr & SAVAGE_TEXDESCR_TEX0EN_MASK)
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000183 ret |= savage_verify_texaddr(dev_priv, 0,
184 dev_priv->state.s4.texaddr0);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000185 if (dev_priv->state.s4.texdescr & SAVAGE_TEXDESCR_TEX1EN_MASK)
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000186 ret |= savage_verify_texaddr(dev_priv, 1,
187 dev_priv->state.s4.texaddr1);
Felix Kuehling04842e82005-01-01 20:22:58 +0000188 }
189
190 return ret;
191}
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000192#undef SAVE_STATE
193#undef SAVE_STATE_MASK
Felix Kuehling04842e82005-01-01 20:22:58 +0000194
195static int savage_dispatch_state(drm_savage_private_t *dev_priv,
196 const drm_savage_cmd_header_t *cmd_header,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000197 const uint32_t *regs)
Felix Kuehling04842e82005-01-01 20:22:58 +0000198{
Felix Kuehling04842e82005-01-01 20:22:58 +0000199 unsigned int count = cmd_header->state.count;
200 unsigned int start = cmd_header->state.start;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000201 unsigned int count2 = 0;
202 unsigned int bci_size;
Felix Kuehling04842e82005-01-01 20:22:58 +0000203 int ret;
Dave Airlie83cb1502005-06-04 06:14:58 +0000204 DMA_LOCALS;
Felix Kuehling04842e82005-01-01 20:22:58 +0000205
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000206 if (!count)
207 return 0;
208
Felix Kuehling04842e82005-01-01 20:22:58 +0000209 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
210 ret = savage_verify_state_s3d(dev_priv, start, count, regs);
211 if (ret != 0)
212 return ret;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000213 /* scissor regs are emitted in savage_dispatch_draw */
214 if (start < SAVAGE_SCSTART_S3D) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000215 if (start + count > SAVAGE_SCEND_S3D + 1)
216 count2 = count - (SAVAGE_SCEND_S3D + 1 - start);
217 if (start + count > SAVAGE_SCSTART_S3D)
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000218 count = SAVAGE_SCSTART_S3D - start;
219 } else if (start <= SAVAGE_SCEND_S3D) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000220 if (start + count > SAVAGE_SCEND_S3D + 1) {
221 count -= SAVAGE_SCEND_S3D + 1 - start;
222 start = SAVAGE_SCEND_S3D + 1;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000223 } else
224 return 0;
225 }
Felix Kuehling04842e82005-01-01 20:22:58 +0000226 } else {
227 ret = savage_verify_state_s4(dev_priv, start, count, regs);
228 if (ret != 0)
229 return ret;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000230 /* scissor regs are emitted in savage_dispatch_draw */
231 if (start < SAVAGE_DRAWCTRL0_S4) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000232 if (start + count > SAVAGE_DRAWCTRL1_S4 + 1)
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000233 count2 = count -
234 (SAVAGE_DRAWCTRL1_S4 + 1 - start);
Dave Airlie5dc5c362007-11-22 16:10:36 +1000235 if (start + count > SAVAGE_DRAWCTRL0_S4)
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000236 count = SAVAGE_DRAWCTRL0_S4 - start;
237 } else if (start <= SAVAGE_DRAWCTRL1_S4) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000238 if (start + count > SAVAGE_DRAWCTRL1_S4 + 1) {
239 count -= SAVAGE_DRAWCTRL1_S4 + 1 - start;
240 start = SAVAGE_DRAWCTRL1_S4 + 1;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000241 } else
242 return 0;
243 }
Felix Kuehling04842e82005-01-01 20:22:58 +0000244 }
245
Dave Airlie5dc5c362007-11-22 16:10:36 +1000246 bci_size = count + (count + 254) / 255 + count2 + (count2 + 254) / 255;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000247
Felix Kuehling04842e82005-01-01 20:22:58 +0000248 if (cmd_header->state.global) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000249 BEGIN_DMA(bci_size + 1);
Felix Kuehling15995232005-03-06 01:41:06 +0000250 DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
Felix Kuehlingd6af9022005-01-09 19:49:21 +0000251 dev_priv->waiting = 1;
Felix Kuehling04842e82005-01-01 20:22:58 +0000252 } else {
Felix Kuehling15995232005-03-06 01:41:06 +0000253 BEGIN_DMA(bci_size);
Felix Kuehling04842e82005-01-01 20:22:58 +0000254 }
255
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000256 do {
257 while (count > 0) {
258 unsigned int n = count < 255 ? count : 255;
Felix Kuehling15995232005-03-06 01:41:06 +0000259 DMA_SET_REGISTERS(start, n);
Eric Anholt6481a2e2005-11-08 21:40:03 +0000260 DMA_COPY(regs, n);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000261 count -= n;
262 start += n;
263 regs += n;
264 }
265 start += 2;
266 regs += 2;
267 count = count2;
268 count2 = 0;
269 } while (count);
Felix Kuehling04842e82005-01-01 20:22:58 +0000270
Felix Kuehling15995232005-03-06 01:41:06 +0000271 DMA_COMMIT();
272
Felix Kuehling04842e82005-01-01 20:22:58 +0000273 return 0;
274}
275
276static int savage_dispatch_dma_prim(drm_savage_private_t *dev_priv,
277 const drm_savage_cmd_header_t *cmd_header,
Dave Airlie24311d52007-07-16 13:42:11 +1000278 const struct drm_buf *dmabuf)
Felix Kuehling04842e82005-01-01 20:22:58 +0000279{
Felix Kuehling04842e82005-01-01 20:22:58 +0000280 unsigned char reorder = 0;
Felix Kuehling9514ee32005-01-16 01:22:09 +0000281 unsigned int prim = cmd_header->prim.prim;
282 unsigned int skip = cmd_header->prim.skip;
Felix Kuehling04842e82005-01-01 20:22:58 +0000283 unsigned int n = cmd_header->prim.count;
284 unsigned int start = cmd_header->prim.start;
285 unsigned int i;
Dave Airlie83cb1502005-06-04 06:14:58 +0000286 BCI_LOCALS;
Felix Kuehling04842e82005-01-01 20:22:58 +0000287
Felix Kuehling15995232005-03-06 01:41:06 +0000288 if (!dmabuf) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000289 DRM_ERROR("called without dma buffers!\n");
290 return -EINVAL;
Felix Kuehling15995232005-03-06 01:41:06 +0000291 }
292
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000293 if (!n)
294 return 0;
295
Felix Kuehling04842e82005-01-01 20:22:58 +0000296 switch (prim) {
297 case SAVAGE_PRIM_TRILIST_201:
298 reorder = 1;
299 prim = SAVAGE_PRIM_TRILIST;
300 case SAVAGE_PRIM_TRILIST:
301 if (n % 3 != 0) {
302 DRM_ERROR("wrong number of vertices %u in TRILIST\n",
303 n);
Eric Anholte39286e2007-07-19 17:00:17 -0700304 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000305 }
306 break;
307 case SAVAGE_PRIM_TRISTRIP:
308 case SAVAGE_PRIM_TRIFAN:
309 if (n < 3) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000310 DRM_ERROR
311 ("wrong number of vertices %u in TRIFAN/STRIP\n",
312 n);
Eric Anholte39286e2007-07-19 17:00:17 -0700313 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000314 }
315 break;
316 default:
317 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholte39286e2007-07-19 17:00:17 -0700318 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000319 }
320
321 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
322 if (skip != 0) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000323 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700324 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000325 }
326 } else {
327 unsigned int size = 10 - (skip & 1) - (skip >> 1 & 1) -
328 (skip >> 2 & 1) - (skip >> 3 & 1) - (skip >> 4 & 1) -
329 (skip >> 5 & 1) - (skip >> 6 & 1) - (skip >> 7 & 1);
330 if (skip > SAVAGE_SKIP_ALL_S4 || size != 8) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000331 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700332 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000333 }
334 if (reorder) {
335 DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
Eric Anholte39286e2007-07-19 17:00:17 -0700336 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000337 }
338 }
339
Dave Airlie5dc5c362007-11-22 16:10:36 +1000340 if (start + n > dmabuf->total / 32) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000341 DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
Dave Airlie5dc5c362007-11-22 16:10:36 +1000342 start, start + n - 1, dmabuf->total / 32);
Eric Anholte39286e2007-07-19 17:00:17 -0700343 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000344 }
345
Felix Kuehling15995232005-03-06 01:41:06 +0000346 /* Vertex DMA doesn't work with command DMA at the same time,
347 * so we use BCI_... to submit commands here. Flush buffered
348 * faked DMA first. */
349 DMA_FLUSH();
350
Felix Kuehling04842e82005-01-01 20:22:58 +0000351 if (dmabuf->bus_address != dev_priv->state.common.vbaddr) {
352 BEGIN_BCI(2);
353 BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR, 1);
354 BCI_WRITE(dmabuf->bus_address | dev_priv->dma_type);
355 dev_priv->state.common.vbaddr = dmabuf->bus_address;
356 }
Felix Kuehlingd6af9022005-01-09 19:49:21 +0000357 if (S3_SAVAGE3D_SERIES(dev_priv->chipset) && dev_priv->waiting) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000358 /* Workaround for what looks like a hardware bug. If a
359 * WAIT_3D_IDLE was emitted some time before the
360 * indexed drawing command then the engine will lock
361 * up. There are two known workarounds:
362 * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
363 BEGIN_BCI(63);
364 for (i = 0; i < 63; ++i)
365 BCI_WRITE(BCI_CMD_WAIT);
Felix Kuehlingd6af9022005-01-09 19:49:21 +0000366 dev_priv->waiting = 0;
Felix Kuehling04842e82005-01-01 20:22:58 +0000367 }
368
369 prim <<= 25;
370 while (n != 0) {
371 /* Can emit up to 255 indices (85 triangles) at once. */
372 unsigned int count = n > 255 ? 255 : n;
373 if (reorder) {
374 /* Need to reorder indices for correct flat
375 * shading while preserving the clock sense
376 * for correct culling. Only on Savage3D. */
Dave Airlie5dc5c362007-11-22 16:10:36 +1000377 int reorder[3] = { -1, -1, -1 };
378 reorder[start % 3] = 2;
Felix Kuehling04842e82005-01-01 20:22:58 +0000379
Dave Airlie5dc5c362007-11-22 16:10:36 +1000380 BEGIN_BCI((count + 1 + 1) / 2);
381 BCI_DRAW_INDICES_S3D(count, prim, start + 2);
Felix Kuehling04842e82005-01-01 20:22:58 +0000382
Dave Airlie5dc5c362007-11-22 16:10:36 +1000383 for (i = start + 1; i + 1 < start + count; i += 2)
Felix Kuehling04842e82005-01-01 20:22:58 +0000384 BCI_WRITE((i + reorder[i % 3]) |
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000385 ((i + 1 +
386 reorder[(i + 1) % 3]) << 16));
Dave Airlie5dc5c362007-11-22 16:10:36 +1000387 if (i < start + count)
388 BCI_WRITE(i + reorder[i % 3]);
Felix Kuehling04842e82005-01-01 20:22:58 +0000389 } else if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000390 BEGIN_BCI((count + 1 + 1) / 2);
Felix Kuehling04842e82005-01-01 20:22:58 +0000391 BCI_DRAW_INDICES_S3D(count, prim, start);
392
Dave Airlie5dc5c362007-11-22 16:10:36 +1000393 for (i = start + 1; i + 1 < start + count; i += 2)
394 BCI_WRITE(i | ((i + 1) << 16));
395 if (i < start + count)
Felix Kuehling04842e82005-01-01 20:22:58 +0000396 BCI_WRITE(i);
397 } else {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000398 BEGIN_BCI((count + 2 + 1) / 2);
Felix Kuehling04842e82005-01-01 20:22:58 +0000399 BCI_DRAW_INDICES_S4(count, prim, skip);
400
Dave Airlie5dc5c362007-11-22 16:10:36 +1000401 for (i = start; i + 1 < start + count; i += 2)
402 BCI_WRITE(i | ((i + 1) << 16));
403 if (i < start + count)
Felix Kuehling04842e82005-01-01 20:22:58 +0000404 BCI_WRITE(i);
405 }
406
407 start += count;
408 n -= count;
409
410 prim |= BCI_CMD_DRAW_CONT;
411 }
412
413 return 0;
414}
415
416static int savage_dispatch_vb_prim(drm_savage_private_t *dev_priv,
417 const drm_savage_cmd_header_t *cmd_header,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000418 const uint32_t *vtxbuf, unsigned int vb_size,
Felix Kuehling04842e82005-01-01 20:22:58 +0000419 unsigned int vb_stride)
420{
Felix Kuehling04842e82005-01-01 20:22:58 +0000421 unsigned char reorder = 0;
Felix Kuehling9514ee32005-01-16 01:22:09 +0000422 unsigned int prim = cmd_header->prim.prim;
423 unsigned int skip = cmd_header->prim.skip;
Felix Kuehling04842e82005-01-01 20:22:58 +0000424 unsigned int n = cmd_header->prim.count;
425 unsigned int start = cmd_header->prim.start;
426 unsigned int vtx_size;
427 unsigned int i;
Dave Airlie83cb1502005-06-04 06:14:58 +0000428 DMA_LOCALS;
Felix Kuehling04842e82005-01-01 20:22:58 +0000429
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000430 if (!n)
431 return 0;
432
Felix Kuehling04842e82005-01-01 20:22:58 +0000433 switch (prim) {
434 case SAVAGE_PRIM_TRILIST_201:
435 reorder = 1;
436 prim = SAVAGE_PRIM_TRILIST;
437 case SAVAGE_PRIM_TRILIST:
438 if (n % 3 != 0) {
439 DRM_ERROR("wrong number of vertices %u in TRILIST\n",
440 n);
Eric Anholte39286e2007-07-19 17:00:17 -0700441 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000442 }
443 break;
444 case SAVAGE_PRIM_TRISTRIP:
445 case SAVAGE_PRIM_TRIFAN:
446 if (n < 3) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000447 DRM_ERROR
448 ("wrong number of vertices %u in TRIFAN/STRIP\n",
449 n);
Eric Anholte39286e2007-07-19 17:00:17 -0700450 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000451 }
452 break;
453 default:
454 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholte39286e2007-07-19 17:00:17 -0700455 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000456 }
457
458 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
459 if (skip > SAVAGE_SKIP_ALL_S3D) {
460 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700461 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000462 }
463 vtx_size = 8; /* full vertex */
464 } else {
465 if (skip > SAVAGE_SKIP_ALL_S4) {
466 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700467 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000468 }
469 vtx_size = 10; /* full vertex */
470 }
471
472 vtx_size -= (skip & 1) + (skip >> 1 & 1) +
473 (skip >> 2 & 1) + (skip >> 3 & 1) + (skip >> 4 & 1) +
474 (skip >> 5 & 1) + (skip >> 6 & 1) + (skip >> 7 & 1);
475
476 if (vtx_size > vb_stride) {
477 DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
478 vtx_size, vb_stride);
Eric Anholte39286e2007-07-19 17:00:17 -0700479 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000480 }
481
Dave Airlie5dc5c362007-11-22 16:10:36 +1000482 if (start + n > vb_size / (vb_stride * 4)) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000483 DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
Dave Airlie5dc5c362007-11-22 16:10:36 +1000484 start, start + n - 1, vb_size / (vb_stride * 4));
Eric Anholte39286e2007-07-19 17:00:17 -0700485 return -EINVAL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000486 }
487
488 prim <<= 25;
489 while (n != 0) {
490 /* Can emit up to 255 vertices (85 triangles) at once. */
491 unsigned int count = n > 255 ? 255 : n;
492 if (reorder) {
493 /* Need to reorder vertices for correct flat
494 * shading while preserving the clock sense
495 * for correct culling. Only on Savage3D. */
Dave Airlie5dc5c362007-11-22 16:10:36 +1000496 int reorder[3] = { -1, -1, -1 };
497 reorder[start % 3] = 2;
Felix Kuehling04842e82005-01-01 20:22:58 +0000498
Dave Airlie5dc5c362007-11-22 16:10:36 +1000499 BEGIN_DMA(count * vtx_size + 1);
Felix Kuehling15995232005-03-06 01:41:06 +0000500 DMA_DRAW_PRIMITIVE(count, prim, skip);
Felix Kuehling04842e82005-01-01 20:22:58 +0000501
Dave Airlie5dc5c362007-11-22 16:10:36 +1000502 for (i = start; i < start + count; ++i) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000503 unsigned int j = i + reorder[i % 3];
Dave Airlie5dc5c362007-11-22 16:10:36 +1000504 DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
Felix Kuehling04842e82005-01-01 20:22:58 +0000505 }
Felix Kuehling15995232005-03-06 01:41:06 +0000506
507 DMA_COMMIT();
Felix Kuehling04842e82005-01-01 20:22:58 +0000508 } else {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000509 BEGIN_DMA(count * vtx_size + 1);
Felix Kuehling15995232005-03-06 01:41:06 +0000510 DMA_DRAW_PRIMITIVE(count, prim, skip);
Felix Kuehling04842e82005-01-01 20:22:58 +0000511
512 if (vb_stride == vtx_size) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000513 DMA_COPY(&vtxbuf[vb_stride * start],
514 vtx_size * count);
Felix Kuehling04842e82005-01-01 20:22:58 +0000515 } else {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000516 for (i = start; i < start + count; ++i) {
517 DMA_COPY(&vtxbuf[vb_stride * i],
Eric Anholt6481a2e2005-11-08 21:40:03 +0000518 vtx_size);
Felix Kuehling04842e82005-01-01 20:22:58 +0000519 }
520 }
Felix Kuehling15995232005-03-06 01:41:06 +0000521
522 DMA_COMMIT();
Felix Kuehling04842e82005-01-01 20:22:58 +0000523 }
524
525 start += count;
526 n -= count;
527
528 prim |= BCI_CMD_DRAW_CONT;
529 }
530
531 return 0;
532}
533
Felix Kuehling858e68f2005-01-20 12:23:25 +0000534static int savage_dispatch_dma_idx(drm_savage_private_t *dev_priv,
535 const drm_savage_cmd_header_t *cmd_header,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000536 const uint16_t *idx,
Dave Airlie24311d52007-07-16 13:42:11 +1000537 const struct drm_buf *dmabuf)
Felix Kuehling858e68f2005-01-20 12:23:25 +0000538{
Felix Kuehling858e68f2005-01-20 12:23:25 +0000539 unsigned char reorder = 0;
540 unsigned int prim = cmd_header->idx.prim;
541 unsigned int skip = cmd_header->idx.skip;
542 unsigned int n = cmd_header->idx.count;
543 unsigned int i;
Dave Airlie83cb1502005-06-04 06:14:58 +0000544 BCI_LOCALS;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000545
Felix Kuehling15995232005-03-06 01:41:06 +0000546 if (!dmabuf) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000547 DRM_ERROR("called without dma buffers!\n");
548 return -EINVAL;
Felix Kuehling15995232005-03-06 01:41:06 +0000549 }
550
Felix Kuehling858e68f2005-01-20 12:23:25 +0000551 if (!n)
552 return 0;
553
554 switch (prim) {
555 case SAVAGE_PRIM_TRILIST_201:
556 reorder = 1;
557 prim = SAVAGE_PRIM_TRILIST;
558 case SAVAGE_PRIM_TRILIST:
559 if (n % 3 != 0) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000560 DRM_ERROR("wrong number of indices %u in TRILIST\n", n);
Eric Anholte39286e2007-07-19 17:00:17 -0700561 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000562 }
563 break;
564 case SAVAGE_PRIM_TRISTRIP:
565 case SAVAGE_PRIM_TRIFAN:
566 if (n < 3) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000567 DRM_ERROR
568 ("wrong number of indices %u in TRIFAN/STRIP\n", n);
Eric Anholte39286e2007-07-19 17:00:17 -0700569 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000570 }
571 break;
572 default:
573 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholte39286e2007-07-19 17:00:17 -0700574 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000575 }
576
577 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
578 if (skip != 0) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000579 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700580 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000581 }
582 } else {
583 unsigned int size = 10 - (skip & 1) - (skip >> 1 & 1) -
584 (skip >> 2 & 1) - (skip >> 3 & 1) - (skip >> 4 & 1) -
585 (skip >> 5 & 1) - (skip >> 6 & 1) - (skip >> 7 & 1);
586 if (skip > SAVAGE_SKIP_ALL_S4 || size != 8) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000587 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700588 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000589 }
590 if (reorder) {
591 DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
Eric Anholte39286e2007-07-19 17:00:17 -0700592 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000593 }
594 }
595
Felix Kuehling15995232005-03-06 01:41:06 +0000596 /* Vertex DMA doesn't work with command DMA at the same time,
597 * so we use BCI_... to submit commands here. Flush buffered
598 * faked DMA first. */
599 DMA_FLUSH();
600
Felix Kuehling858e68f2005-01-20 12:23:25 +0000601 if (dmabuf->bus_address != dev_priv->state.common.vbaddr) {
602 BEGIN_BCI(2);
603 BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR, 1);
604 BCI_WRITE(dmabuf->bus_address | dev_priv->dma_type);
605 dev_priv->state.common.vbaddr = dmabuf->bus_address;
606 }
607 if (S3_SAVAGE3D_SERIES(dev_priv->chipset) && dev_priv->waiting) {
608 /* Workaround for what looks like a hardware bug. If a
609 * WAIT_3D_IDLE was emitted some time before the
610 * indexed drawing command then the engine will lock
611 * up. There are two known workarounds:
612 * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
613 BEGIN_BCI(63);
614 for (i = 0; i < 63; ++i)
615 BCI_WRITE(BCI_CMD_WAIT);
616 dev_priv->waiting = 0;
617 }
618
619 prim <<= 25;
620 while (n != 0) {
621 /* Can emit up to 255 indices (85 triangles) at once. */
622 unsigned int count = n > 255 ? 255 : n;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000623
Eric Anholt6481a2e2005-11-08 21:40:03 +0000624 /* check indices */
Felix Kuehling858e68f2005-01-20 12:23:25 +0000625 for (i = 0; i < count; ++i) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000626 if (idx[i] > dmabuf->total / 32) {
Felix Kuehling858e68f2005-01-20 12:23:25 +0000627 DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
Dave Airlie5dc5c362007-11-22 16:10:36 +1000628 i, idx[i], dmabuf->total / 32);
Eric Anholte39286e2007-07-19 17:00:17 -0700629 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000630 }
631 }
632
633 if (reorder) {
634 /* Need to reorder indices for correct flat
635 * shading while preserving the clock sense
636 * for correct culling. Only on Savage3D. */
Dave Airlie5dc5c362007-11-22 16:10:36 +1000637 int reorder[3] = { 2, -1, -1 };
Felix Kuehling858e68f2005-01-20 12:23:25 +0000638
Dave Airlie5dc5c362007-11-22 16:10:36 +1000639 BEGIN_BCI((count + 1 + 1) / 2);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000640 BCI_DRAW_INDICES_S3D(count, prim, idx[2]);
641
Dave Airlie5dc5c362007-11-22 16:10:36 +1000642 for (i = 1; i + 1 < count; i += 2)
Felix Kuehling858e68f2005-01-20 12:23:25 +0000643 BCI_WRITE(idx[i + reorder[i % 3]] |
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000644 (idx[i + 1 +
645 reorder[(i + 1) % 3]] << 16));
Felix Kuehling858e68f2005-01-20 12:23:25 +0000646 if (i < count)
Dave Airlie5dc5c362007-11-22 16:10:36 +1000647 BCI_WRITE(idx[i + reorder[i % 3]]);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000648 } else if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000649 BEGIN_BCI((count + 1 + 1) / 2);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000650 BCI_DRAW_INDICES_S3D(count, prim, idx[0]);
651
Dave Airlie5dc5c362007-11-22 16:10:36 +1000652 for (i = 1; i + 1 < count; i += 2)
653 BCI_WRITE(idx[i] | (idx[i + 1] << 16));
Felix Kuehling858e68f2005-01-20 12:23:25 +0000654 if (i < count)
655 BCI_WRITE(idx[i]);
656 } else {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000657 BEGIN_BCI((count + 2 + 1) / 2);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000658 BCI_DRAW_INDICES_S4(count, prim, skip);
659
Dave Airlie5dc5c362007-11-22 16:10:36 +1000660 for (i = 0; i + 1 < count; i += 2)
661 BCI_WRITE(idx[i] | (idx[i + 1] << 16));
Felix Kuehling858e68f2005-01-20 12:23:25 +0000662 if (i < count)
663 BCI_WRITE(idx[i]);
664 }
665
Eric Anholt6481a2e2005-11-08 21:40:03 +0000666 idx += count;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000667 n -= count;
668
669 prim |= BCI_CMD_DRAW_CONT;
670 }
671
672 return 0;
673}
674
675static int savage_dispatch_vb_idx(drm_savage_private_t *dev_priv,
676 const drm_savage_cmd_header_t *cmd_header,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000677 const uint16_t *idx,
678 const uint32_t *vtxbuf,
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000679 unsigned int vb_size, unsigned int vb_stride)
Felix Kuehling858e68f2005-01-20 12:23:25 +0000680{
Felix Kuehling858e68f2005-01-20 12:23:25 +0000681 unsigned char reorder = 0;
682 unsigned int prim = cmd_header->idx.prim;
683 unsigned int skip = cmd_header->idx.skip;
684 unsigned int n = cmd_header->idx.count;
685 unsigned int vtx_size;
686 unsigned int i;
Dave Airlie83cb1502005-06-04 06:14:58 +0000687 DMA_LOCALS;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000688
689 if (!n)
690 return 0;
691
692 switch (prim) {
693 case SAVAGE_PRIM_TRILIST_201:
694 reorder = 1;
695 prim = SAVAGE_PRIM_TRILIST;
696 case SAVAGE_PRIM_TRILIST:
697 if (n % 3 != 0) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000698 DRM_ERROR("wrong number of indices %u in TRILIST\n", n);
Eric Anholte39286e2007-07-19 17:00:17 -0700699 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000700 }
701 break;
702 case SAVAGE_PRIM_TRISTRIP:
703 case SAVAGE_PRIM_TRIFAN:
704 if (n < 3) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000705 DRM_ERROR
706 ("wrong number of indices %u in TRIFAN/STRIP\n", n);
Eric Anholte39286e2007-07-19 17:00:17 -0700707 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000708 }
709 break;
710 default:
711 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholte39286e2007-07-19 17:00:17 -0700712 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000713 }
714
715 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
716 if (skip > SAVAGE_SKIP_ALL_S3D) {
717 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700718 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000719 }
720 vtx_size = 8; /* full vertex */
721 } else {
722 if (skip > SAVAGE_SKIP_ALL_S4) {
723 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholte39286e2007-07-19 17:00:17 -0700724 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000725 }
726 vtx_size = 10; /* full vertex */
727 }
728
729 vtx_size -= (skip & 1) + (skip >> 1 & 1) +
730 (skip >> 2 & 1) + (skip >> 3 & 1) + (skip >> 4 & 1) +
731 (skip >> 5 & 1) + (skip >> 6 & 1) + (skip >> 7 & 1);
732
733 if (vtx_size > vb_stride) {
734 DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
735 vtx_size, vb_stride);
Eric Anholte39286e2007-07-19 17:00:17 -0700736 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000737 }
738
739 prim <<= 25;
740 while (n != 0) {
741 /* Can emit up to 255 vertices (85 triangles) at once. */
742 unsigned int count = n > 255 ? 255 : n;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000743
Eric Anholt6481a2e2005-11-08 21:40:03 +0000744 /* Check indices */
Felix Kuehling858e68f2005-01-20 12:23:25 +0000745 for (i = 0; i < count; ++i) {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000746 if (idx[i] > vb_size / (vb_stride * 4)) {
Felix Kuehling858e68f2005-01-20 12:23:25 +0000747 DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
Dave Airlie5dc5c362007-11-22 16:10:36 +1000748 i, idx[i], vb_size / (vb_stride * 4));
Eric Anholte39286e2007-07-19 17:00:17 -0700749 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000750 }
751 }
752
753 if (reorder) {
754 /* Need to reorder vertices for correct flat
755 * shading while preserving the clock sense
756 * for correct culling. Only on Savage3D. */
Dave Airlie5dc5c362007-11-22 16:10:36 +1000757 int reorder[3] = { 2, -1, -1 };
Felix Kuehling858e68f2005-01-20 12:23:25 +0000758
Dave Airlie5dc5c362007-11-22 16:10:36 +1000759 BEGIN_DMA(count * vtx_size + 1);
Felix Kuehling15995232005-03-06 01:41:06 +0000760 DMA_DRAW_PRIMITIVE(count, prim, skip);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000761
762 for (i = 0; i < count; ++i) {
763 unsigned int j = idx[i + reorder[i % 3]];
Dave Airlie5dc5c362007-11-22 16:10:36 +1000764 DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000765 }
Felix Kuehling15995232005-03-06 01:41:06 +0000766
767 DMA_COMMIT();
Felix Kuehling858e68f2005-01-20 12:23:25 +0000768 } else {
Dave Airlie5dc5c362007-11-22 16:10:36 +1000769 BEGIN_DMA(count * vtx_size + 1);
Felix Kuehling15995232005-03-06 01:41:06 +0000770 DMA_DRAW_PRIMITIVE(count, prim, skip);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000771
772 for (i = 0; i < count; ++i) {
773 unsigned int j = idx[i];
Dave Airlie5dc5c362007-11-22 16:10:36 +1000774 DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
Felix Kuehling858e68f2005-01-20 12:23:25 +0000775 }
Felix Kuehling15995232005-03-06 01:41:06 +0000776
777 DMA_COMMIT();
Felix Kuehling858e68f2005-01-20 12:23:25 +0000778 }
779
Eric Anholt6481a2e2005-11-08 21:40:03 +0000780 idx += count;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000781 n -= count;
782
783 prim |= BCI_CMD_DRAW_CONT;
784 }
785
786 return 0;
787}
788
Felix Kuehling04842e82005-01-01 20:22:58 +0000789static int savage_dispatch_clear(drm_savage_private_t *dev_priv,
790 const drm_savage_cmd_header_t *cmd_header,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000791 const drm_savage_cmd_header_t *data,
Felix Kuehling04842e82005-01-01 20:22:58 +0000792 unsigned int nbox,
Dave Airlieb95ac8b2007-07-16 11:22:15 +1000793 const struct drm_clip_rect *boxes)
Felix Kuehling04842e82005-01-01 20:22:58 +0000794{
Eric Anholt6481a2e2005-11-08 21:40:03 +0000795 unsigned int flags = cmd_header->clear0.flags;
Felix Kuehling04842e82005-01-01 20:22:58 +0000796 unsigned int clear_cmd;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000797 unsigned int i, nbufs;
Dave Airlie83cb1502005-06-04 06:14:58 +0000798 DMA_LOCALS;
Felix Kuehling04842e82005-01-01 20:22:58 +0000799
800 if (nbox == 0)
801 return 0;
802
Felix Kuehling04842e82005-01-01 20:22:58 +0000803 clear_cmd = BCI_CMD_RECT | BCI_CMD_RECT_XP | BCI_CMD_RECT_YP |
804 BCI_CMD_SEND_COLOR | BCI_CMD_DEST_PBD_NEW;
805 BCI_CMD_SET_ROP(clear_cmd,0xCC);
806
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000807 nbufs = ((flags & SAVAGE_FRONT) ? 1 : 0) +
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000808 ((flags & SAVAGE_BACK) ? 1 : 0) + ((flags & SAVAGE_DEPTH) ? 1 : 0);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000809 if (nbufs == 0)
Felix Kuehling04842e82005-01-01 20:22:58 +0000810 return 0;
811
Eric Anholt6481a2e2005-11-08 21:40:03 +0000812 if (data->clear1.mask != 0xffffffff) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000813 /* set mask */
Felix Kuehling15995232005-03-06 01:41:06 +0000814 BEGIN_DMA(2);
815 DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK, 1);
Eric Anholt6481a2e2005-11-08 21:40:03 +0000816 DMA_WRITE(data->clear1.mask);
Felix Kuehling15995232005-03-06 01:41:06 +0000817 DMA_COMMIT();
Felix Kuehling04842e82005-01-01 20:22:58 +0000818 }
819 for (i = 0; i < nbox; ++i) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000820 unsigned int x, y, w, h;
821 unsigned int buf;
Eric Anholt6481a2e2005-11-08 21:40:03 +0000822
823 x = boxes[i].x1, y = boxes[i].y1;
824 w = boxes[i].x2 - boxes[i].x1;
825 h = boxes[i].y2 - boxes[i].y1;
Dave Airlie5dc5c362007-11-22 16:10:36 +1000826 BEGIN_DMA(nbufs * 6);
Felix Kuehling04842e82005-01-01 20:22:58 +0000827 for (buf = SAVAGE_FRONT; buf <= SAVAGE_DEPTH; buf <<= 1) {
828 if (!(flags & buf))
829 continue;
Felix Kuehling15995232005-03-06 01:41:06 +0000830 DMA_WRITE(clear_cmd);
Dave Airlie5dc5c362007-11-22 16:10:36 +1000831 switch (buf) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000832 case SAVAGE_FRONT:
Felix Kuehling15995232005-03-06 01:41:06 +0000833 DMA_WRITE(dev_priv->front_offset);
834 DMA_WRITE(dev_priv->front_bd);
Felix Kuehling04842e82005-01-01 20:22:58 +0000835 break;
836 case SAVAGE_BACK:
Felix Kuehling15995232005-03-06 01:41:06 +0000837 DMA_WRITE(dev_priv->back_offset);
838 DMA_WRITE(dev_priv->back_bd);
Felix Kuehling04842e82005-01-01 20:22:58 +0000839 break;
840 case SAVAGE_DEPTH:
Felix Kuehling15995232005-03-06 01:41:06 +0000841 DMA_WRITE(dev_priv->depth_offset);
842 DMA_WRITE(dev_priv->depth_bd);
Felix Kuehling04842e82005-01-01 20:22:58 +0000843 break;
844 }
Eric Anholt6481a2e2005-11-08 21:40:03 +0000845 DMA_WRITE(data->clear1.value);
Felix Kuehling15995232005-03-06 01:41:06 +0000846 DMA_WRITE(BCI_X_Y(x, y));
847 DMA_WRITE(BCI_W_H(w, h));
Felix Kuehling04842e82005-01-01 20:22:58 +0000848 }
Felix Kuehling15995232005-03-06 01:41:06 +0000849 DMA_COMMIT();
Felix Kuehling04842e82005-01-01 20:22:58 +0000850 }
Eric Anholt6481a2e2005-11-08 21:40:03 +0000851 if (data->clear1.mask != 0xffffffff) {
Felix Kuehling04842e82005-01-01 20:22:58 +0000852 /* reset mask */
Felix Kuehling15995232005-03-06 01:41:06 +0000853 BEGIN_DMA(2);
854 DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK, 1);
855 DMA_WRITE(0xffffffff);
856 DMA_COMMIT();
Felix Kuehling04842e82005-01-01 20:22:58 +0000857 }
858
859 return 0;
860}
861
862static int savage_dispatch_swap(drm_savage_private_t *dev_priv,
Dave Airlieb95ac8b2007-07-16 11:22:15 +1000863 unsigned int nbox, const struct drm_clip_rect *boxes)
Felix Kuehling04842e82005-01-01 20:22:58 +0000864{
Felix Kuehling04842e82005-01-01 20:22:58 +0000865 unsigned int swap_cmd;
866 unsigned int i;
Dave Airlie83cb1502005-06-04 06:14:58 +0000867 DMA_LOCALS;
Felix Kuehling04842e82005-01-01 20:22:58 +0000868
869 if (nbox == 0)
870 return 0;
871
872 swap_cmd = BCI_CMD_RECT | BCI_CMD_RECT_XP | BCI_CMD_RECT_YP |
873 BCI_CMD_SRC_PBD_COLOR_NEW | BCI_CMD_DEST_GBD;
874 BCI_CMD_SET_ROP(swap_cmd,0xCC);
875
Felix Kuehling04842e82005-01-01 20:22:58 +0000876 for (i = 0; i < nbox; ++i) {
Felix Kuehling15995232005-03-06 01:41:06 +0000877 BEGIN_DMA(6);
878 DMA_WRITE(swap_cmd);
879 DMA_WRITE(dev_priv->back_offset);
880 DMA_WRITE(dev_priv->back_bd);
Eric Anholt6481a2e2005-11-08 21:40:03 +0000881 DMA_WRITE(BCI_X_Y(boxes[i].x1, boxes[i].y1));
882 DMA_WRITE(BCI_X_Y(boxes[i].x1, boxes[i].y1));
Dave Airlie5dc5c362007-11-22 16:10:36 +1000883 DMA_WRITE(BCI_W_H(boxes[i].x2 - boxes[i].x1,
884 boxes[i].y2 - boxes[i].y1));
Felix Kuehling15995232005-03-06 01:41:06 +0000885 DMA_COMMIT();
Felix Kuehling04842e82005-01-01 20:22:58 +0000886 }
887
888 return 0;
889}
890
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000891static int savage_dispatch_draw(drm_savage_private_t *dev_priv,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000892 const drm_savage_cmd_header_t *start,
893 const drm_savage_cmd_header_t *end,
Dave Airlie24311d52007-07-16 13:42:11 +1000894 const struct drm_buf *dmabuf,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000895 const unsigned int *vtxbuf,
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000896 unsigned int vb_size, unsigned int vb_stride,
897 unsigned int nbox,
Dave Airlieb95ac8b2007-07-16 11:22:15 +1000898 const struct drm_clip_rect *boxes)
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000899{
Felix Kuehling858e68f2005-01-20 12:23:25 +0000900 unsigned int i, j;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000901 int ret;
902
903 for (i = 0; i < nbox; ++i) {
Eric Anholt6481a2e2005-11-08 21:40:03 +0000904 const drm_savage_cmd_header_t *cmdbuf;
905 dev_priv->emit_clip_rect(dev_priv, &boxes[i]);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000906
Eric Anholt6481a2e2005-11-08 21:40:03 +0000907 cmdbuf = start;
908 while (cmdbuf < end) {
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000909 drm_savage_cmd_header_t cmd_header;
Eric Anholt6481a2e2005-11-08 21:40:03 +0000910 cmd_header = *cmdbuf;
911 cmdbuf++;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000912 switch (cmd_header.cmd.cmd) {
913 case SAVAGE_CMD_DMA_PRIM:
914 ret = savage_dispatch_dma_prim(
915 dev_priv, &cmd_header, dmabuf);
916 break;
917 case SAVAGE_CMD_VB_PRIM:
918 ret = savage_dispatch_vb_prim(
919 dev_priv, &cmd_header,
Eric Anholt6481a2e2005-11-08 21:40:03 +0000920 vtxbuf, vb_size, vb_stride);
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000921 break;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000922 case SAVAGE_CMD_DMA_IDX:
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000923 j = (cmd_header.idx.count + 3) / 4;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000924 /* j was check in savage_bci_cmdbuf */
Eric Anholt6481a2e2005-11-08 21:40:03 +0000925 ret = savage_dispatch_dma_idx(dev_priv,
926 &cmd_header, (const uint16_t *)cmdbuf,
Felix Kuehling858e68f2005-01-20 12:23:25 +0000927 dmabuf);
Eric Anholt6481a2e2005-11-08 21:40:03 +0000928 cmdbuf += j;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000929 break;
930 case SAVAGE_CMD_VB_IDX:
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000931 j = (cmd_header.idx.count + 3) / 4;
Felix Kuehling858e68f2005-01-20 12:23:25 +0000932 /* j was check in savage_bci_cmdbuf */
Eric Anholt6481a2e2005-11-08 21:40:03 +0000933 ret = savage_dispatch_vb_idx(dev_priv,
934 &cmd_header, (const uint16_t *)cmdbuf,
935 (const uint32_t *)vtxbuf, vb_size,
936 vb_stride);
937 cmdbuf += j;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000938 break;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000939 default:
Felix Kuehling858e68f2005-01-20 12:23:25 +0000940 /* What's the best return code? EFAULT? */
941 DRM_ERROR("IMPLEMENTATION ERROR: "
942 "non-drawing-command %d\n",
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000943 cmd_header.cmd.cmd);
Eric Anholte39286e2007-07-19 17:00:17 -0700944 return -EINVAL;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +0000945 }
946
947 if (ret != 0)
948 return ret;
949 }
950 }
951
952 return 0;
953}
954
Eric Anholt5b38e132007-07-19 17:11:11 -0700955int savage_bci_cmdbuf(struct drm_device *dev, void *data, struct drm_file *file_priv)
Felix Kuehling04842e82005-01-01 20:22:58 +0000956{
Felix Kuehling04842e82005-01-01 20:22:58 +0000957 drm_savage_private_t *dev_priv = dev->dev_private;
Dave Airlie21ee6fb2007-07-16 12:32:51 +1000958 struct drm_device_dma *dma = dev->dma;
Dave Airlie24311d52007-07-16 13:42:11 +1000959 struct drm_buf *dmabuf;
Eric Anholt5b38e132007-07-19 17:11:11 -0700960 drm_savage_cmdbuf_t *cmdbuf = data;
Eric Anholt6481a2e2005-11-08 21:40:03 +0000961 drm_savage_cmd_header_t *kcmd_addr = NULL;
962 drm_savage_cmd_header_t *first_draw_cmd;
963 unsigned int *kvb_addr = NULL;
Dave Airlieb95ac8b2007-07-16 11:22:15 +1000964 struct drm_clip_rect *kbox_addr = NULL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000965 unsigned int i, j;
966 int ret = 0;
967
968 DRM_DEBUG("\n");
Dave Airlie7f6bf842007-11-05 12:42:22 +1000969
Eric Anholtc1119b12007-07-20 06:39:25 -0700970 LOCK_TEST_WITH_RETURN(dev, file_priv);
Felix Kuehling04842e82005-01-01 20:22:58 +0000971
Felix Kuehling15995232005-03-06 01:41:06 +0000972 if (dma && dma->buflist) {
Eric Anholt5b38e132007-07-19 17:11:11 -0700973 if (cmdbuf->dma_idx > dma->buf_count) {
Dave Airlie1ad5dfc2006-01-02 05:22:50 +0000974 DRM_ERROR
975 ("vertex buffer index %u out of range (0-%u)\n",
Dave Airlie5dc5c362007-11-22 16:10:36 +1000976 cmdbuf->dma_idx, dma->buf_count - 1);
Eric Anholte39286e2007-07-19 17:00:17 -0700977 return -EINVAL;
Felix Kuehling15995232005-03-06 01:41:06 +0000978 }
Eric Anholt5b38e132007-07-19 17:11:11 -0700979 dmabuf = dma->buflist[cmdbuf->dma_idx];
Felix Kuehling15995232005-03-06 01:41:06 +0000980 } else {
981 dmabuf = NULL;
Felix Kuehling04842e82005-01-01 20:22:58 +0000982 }
Felix Kuehling04842e82005-01-01 20:22:58 +0000983
Eric Anholt6481a2e2005-11-08 21:40:03 +0000984 /* Copy the user buffers into kernel temporary areas. This hasn't been
985 * a performance loss compared to VERIFYAREA_READ/
986 * COPY_FROM_USER_UNCHECKED when done in other drivers, and is correct
987 * for locking on FreeBSD.
988 */
Eric Anholt5b38e132007-07-19 17:11:11 -0700989 if (cmdbuf->size) {
990 kcmd_addr = drm_alloc(cmdbuf->size * 8, DRM_MEM_DRIVER);
Eric Anholt6481a2e2005-11-08 21:40:03 +0000991 if (kcmd_addr == NULL)
Eric Anholte39286e2007-07-19 17:00:17 -0700992 return -ENOMEM;
Eric Anholt6481a2e2005-11-08 21:40:03 +0000993
Eric Anholt5b38e132007-07-19 17:11:11 -0700994 if (DRM_COPY_FROM_USER(kcmd_addr, cmdbuf->cmd_addr,
995 cmdbuf->size * 8))
Eric Anholt6481a2e2005-11-08 21:40:03 +0000996 {
Eric Anholt5b38e132007-07-19 17:11:11 -0700997 drm_free(kcmd_addr, cmdbuf->size * 8, DRM_MEM_DRIVER);
Eric Anholte39286e2007-07-19 17:00:17 -0700998 return -EFAULT;
Eric Anholt6481a2e2005-11-08 21:40:03 +0000999 }
Eric Anholt5b38e132007-07-19 17:11:11 -07001000 cmdbuf->cmd_addr = kcmd_addr;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001001 }
Eric Anholt5b38e132007-07-19 17:11:11 -07001002 if (cmdbuf->vb_size) {
1003 kvb_addr = drm_alloc(cmdbuf->vb_size, DRM_MEM_DRIVER);
Eric Anholt6481a2e2005-11-08 21:40:03 +00001004 if (kvb_addr == NULL) {
Eric Anholte39286e2007-07-19 17:00:17 -07001005 ret = -ENOMEM;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001006 goto done;
1007 }
1008
Eric Anholt5b38e132007-07-19 17:11:11 -07001009 if (DRM_COPY_FROM_USER(kvb_addr, cmdbuf->vb_addr,
1010 cmdbuf->vb_size)) {
Eric Anholte39286e2007-07-19 17:00:17 -07001011 ret = -EFAULT;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001012 goto done;
1013 }
Eric Anholt5b38e132007-07-19 17:11:11 -07001014 cmdbuf->vb_addr = kvb_addr;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001015 }
Eric Anholt5b38e132007-07-19 17:11:11 -07001016 if (cmdbuf->nbox) {
1017 kbox_addr = drm_alloc(cmdbuf->nbox *
1018 sizeof(struct drm_clip_rect),
1019 DRM_MEM_DRIVER);
Eric Anholt6481a2e2005-11-08 21:40:03 +00001020 if (kbox_addr == NULL) {
Eric Anholte39286e2007-07-19 17:00:17 -07001021 ret = -ENOMEM;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001022 goto done;
1023 }
1024
Eric Anholt5b38e132007-07-19 17:11:11 -07001025 if (DRM_COPY_FROM_USER(kbox_addr, cmdbuf->box_addr,
1026 cmdbuf->nbox *
1027 sizeof(struct drm_clip_rect))) {
Eric Anholte39286e2007-07-19 17:00:17 -07001028 ret = -EFAULT;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001029 goto done;
1030 }
Eric Anholt5b38e132007-07-19 17:11:11 -07001031 cmdbuf->box_addr = kbox_addr;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001032 }
Felix Kuehling04842e82005-01-01 20:22:58 +00001033
1034 /* Make sure writes to DMA buffers are finished before sending
1035 * DMA commands to the graphics hardware. */
1036 DRM_MEMORYBARRIER();
1037
Felix Kuehlingd6af9022005-01-09 19:49:21 +00001038 /* Coming from user space. Don't know if the Xserver has
1039 * emitted wait commands. Assuming the worst. */
1040 dev_priv->waiting = 1;
1041
Felix Kuehling04842e82005-01-01 20:22:58 +00001042 i = 0;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +00001043 first_draw_cmd = NULL;
Eric Anholt5b38e132007-07-19 17:11:11 -07001044 while (i < cmdbuf->size) {
Felix Kuehling04842e82005-01-01 20:22:58 +00001045 drm_savage_cmd_header_t cmd_header;
Eric Anholt5b38e132007-07-19 17:11:11 -07001046 cmd_header = *(drm_savage_cmd_header_t *)cmdbuf->cmd_addr;
1047 cmdbuf->cmd_addr++;
Felix Kuehling04842e82005-01-01 20:22:58 +00001048 i++;
1049
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +00001050 /* Group drawing commands with same state to minimize
1051 * iterations over clip rects. */
Felix Kuehling858e68f2005-01-20 12:23:25 +00001052 j = 0;
1053 switch (cmd_header.cmd.cmd) {
1054 case SAVAGE_CMD_DMA_IDX:
1055 case SAVAGE_CMD_VB_IDX:
1056 j = (cmd_header.idx.count + 3) / 4;
Eric Anholt5b38e132007-07-19 17:11:11 -07001057 if (i + j > cmdbuf->size) {
Felix Kuehling858e68f2005-01-20 12:23:25 +00001058 DRM_ERROR("indexed drawing command extends "
1059 "beyond end of command buffer\n");
Felix Kuehling15995232005-03-06 01:41:06 +00001060 DMA_FLUSH();
Eric Anholte39286e2007-07-19 17:00:17 -07001061 return -EINVAL;
Felix Kuehling858e68f2005-01-20 12:23:25 +00001062 }
1063 /* fall through */
1064 case SAVAGE_CMD_DMA_PRIM:
1065 case SAVAGE_CMD_VB_PRIM:
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +00001066 if (!first_draw_cmd)
Dave Airlie5dc5c362007-11-22 16:10:36 +10001067 first_draw_cmd = cmdbuf->cmd_addr - 1;
Eric Anholt5b38e132007-07-19 17:11:11 -07001068 cmdbuf->cmd_addr += j;
Felix Kuehling858e68f2005-01-20 12:23:25 +00001069 i += j;
1070 break;
1071 default:
1072 if (first_draw_cmd) {
Dave Airlie5dc5c362007-11-22 16:10:36 +10001073 ret = savage_dispatch_draw(
Eric Anholt6481a2e2005-11-08 21:40:03 +00001074 dev_priv, first_draw_cmd,
Dave Airlie5dc5c362007-11-22 16:10:36 +10001075 cmdbuf->cmd_addr - 1,
Eric Anholt5b38e132007-07-19 17:11:11 -07001076 dmabuf, cmdbuf->vb_addr,
1077 cmdbuf->vb_size,
1078 cmdbuf->vb_stride,
1079 cmdbuf->nbox, cmdbuf->box_addr);
Felix Kuehling858e68f2005-01-20 12:23:25 +00001080 if (ret != 0)
1081 return ret;
1082 first_draw_cmd = NULL;
1083 }
1084 }
1085 if (first_draw_cmd)
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +00001086 continue;
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +00001087
Felix Kuehling04842e82005-01-01 20:22:58 +00001088 switch (cmd_header.cmd.cmd) {
1089 case SAVAGE_CMD_STATE:
Felix Kuehling858e68f2005-01-20 12:23:25 +00001090 j = (cmd_header.state.count + 1) / 2;
Eric Anholt5b38e132007-07-19 17:11:11 -07001091 if (i + j > cmdbuf->size) {
Felix Kuehling858e68f2005-01-20 12:23:25 +00001092 DRM_ERROR("command SAVAGE_CMD_STATE extends "
1093 "beyond end of command buffer\n");
Felix Kuehling15995232005-03-06 01:41:06 +00001094 DMA_FLUSH();
Eric Anholte39286e2007-07-19 17:00:17 -07001095 ret = -EINVAL;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001096 goto done;
Felix Kuehling858e68f2005-01-20 12:23:25 +00001097 }
Eric Anholt6481a2e2005-11-08 21:40:03 +00001098 ret = savage_dispatch_state(dev_priv, &cmd_header,
Eric Anholt5b38e132007-07-19 17:11:11 -07001099 (const uint32_t *)cmdbuf->cmd_addr);
1100 cmdbuf->cmd_addr += j;
Felix Kuehling04842e82005-01-01 20:22:58 +00001101 i += j;
1102 break;
Felix Kuehling04842e82005-01-01 20:22:58 +00001103 case SAVAGE_CMD_CLEAR:
Eric Anholt5b38e132007-07-19 17:11:11 -07001104 if (i + 1 > cmdbuf->size) {
Felix Kuehling858e68f2005-01-20 12:23:25 +00001105 DRM_ERROR("command SAVAGE_CMD_CLEAR extends "
1106 "beyond end of command buffer\n");
Felix Kuehling15995232005-03-06 01:41:06 +00001107 DMA_FLUSH();
Eric Anholte39286e2007-07-19 17:00:17 -07001108 ret = -EINVAL;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001109 goto done;
Felix Kuehling858e68f2005-01-20 12:23:25 +00001110 }
Felix Kuehling04842e82005-01-01 20:22:58 +00001111 ret = savage_dispatch_clear(dev_priv, &cmd_header,
Eric Anholt5b38e132007-07-19 17:11:11 -07001112 cmdbuf->cmd_addr,
1113 cmdbuf->nbox,
1114 cmdbuf->box_addr);
1115 cmdbuf->cmd_addr++;
Felix Kuehling04842e82005-01-01 20:22:58 +00001116 i++;
1117 break;
1118 case SAVAGE_CMD_SWAP:
Eric Anholt5b38e132007-07-19 17:11:11 -07001119 ret = savage_dispatch_swap(dev_priv, cmdbuf->nbox,
1120 cmdbuf->box_addr);
Felix Kuehling04842e82005-01-01 20:22:58 +00001121 break;
1122 default:
Eric Anholt5b38e132007-07-19 17:11:11 -07001123 DRM_ERROR("invalid command 0x%x\n",
1124 cmd_header.cmd.cmd);
Felix Kuehling15995232005-03-06 01:41:06 +00001125 DMA_FLUSH();
Eric Anholte39286e2007-07-19 17:00:17 -07001126 ret = -EINVAL;
Eric Anholt6481a2e2005-11-08 21:40:03 +00001127 goto done;
Felix Kuehling04842e82005-01-01 20:22:58 +00001128 }
1129
Felix Kuehling15995232005-03-06 01:41:06 +00001130 if (ret != 0) {
1131 DMA_FLUSH();
Eric Anholt6481a2e2005-11-08 21:40:03 +00001132 goto done;
Felix Kuehling15995232005-03-06 01:41:06 +00001133 }
Felix Kuehling04842e82005-01-01 20:22:58 +00001134 }
1135
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +00001136 if (first_draw_cmd) {
Dave Airlie5dc5c362007-11-22 16:10:36 +10001137 ret = savage_dispatch_draw(
Eric Anholt5b38e132007-07-19 17:11:11 -07001138 dev_priv, first_draw_cmd, cmdbuf->cmd_addr, dmabuf,
1139 cmdbuf->vb_addr, cmdbuf->vb_size, cmdbuf->vb_stride,
1140 cmdbuf->nbox, cmdbuf->box_addr);
Felix Kuehling15995232005-03-06 01:41:06 +00001141 if (ret != 0) {
1142 DMA_FLUSH();
Eric Anholt6481a2e2005-11-08 21:40:03 +00001143 goto done;
Felix Kuehling15995232005-03-06 01:41:06 +00001144 }
Felix Kuehlingae1bc4a2005-01-05 23:45:42 +00001145 }
1146
Felix Kuehling15995232005-03-06 01:41:06 +00001147 DMA_FLUSH();
1148
Eric Anholt5b38e132007-07-19 17:11:11 -07001149 if (dmabuf && cmdbuf->discard) {
Felix Kuehling04842e82005-01-01 20:22:58 +00001150 drm_savage_buf_priv_t *buf_priv = dmabuf->dev_private;
1151 uint16_t event;
1152 event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
1153 SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
1154 savage_freelist_put(dev, dmabuf);
1155 }
1156
Eric Anholt6481a2e2005-11-08 21:40:03 +00001157done:
1158 /* If we didn't need to allocate them, these'll be NULL */
Eric Anholt5b38e132007-07-19 17:11:11 -07001159 drm_free(kcmd_addr, cmdbuf->size * 8, DRM_MEM_DRIVER);
1160 drm_free(kvb_addr, cmdbuf->vb_size, DRM_MEM_DRIVER);
1161 drm_free(kbox_addr, cmdbuf->nbox * sizeof(struct drm_clip_rect),
Eric Anholt6481a2e2005-11-08 21:40:03 +00001162 DRM_MEM_DRIVER);
1163
1164 return ret;
Felix Kuehling04842e82005-01-01 20:22:58 +00001165}