blob: a2ac25c11c90384ad73d377ec4ed314b2ada0467 [file] [log] [blame]
Dave Airlie282a1672005-08-07 15:43:54 +10001/* 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 */
Sam Ravnborgac0ac212019-06-05 16:03:14 +020025
26#include <linux/slab.h>
27#include <linux/uaccess.h>
28
29#include <drm/drm_device.h>
30#include <drm/drm_file.h>
31#include <drm/drm_print.h>
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/savage_drm.h>
Sam Ravnborgac0ac212019-06-05 16:03:14 +020033
Dave Airlie282a1672005-08-07 15:43:54 +100034#include "savage_drv.h"
35
Dave Airlieb5e89ed2005-09-25 14:28:13 +100036void savage_emit_clip_rect_s3d(drm_savage_private_t * dev_priv,
Dave Airlieeddca552007-07-11 16:09:54 +100037 const struct drm_clip_rect * pbox)
Dave Airlie282a1672005-08-07 15:43:54 +100038{
39 uint32_t scstart = dev_priv->state.s3d.new_scstart;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100040 uint32_t scend = dev_priv->state.s3d.new_scend;
Dave Airlie282a1672005-08-07 15:43:54 +100041 scstart = (scstart & ~SAVAGE_SCISSOR_MASK_S3D) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +100042 ((uint32_t) pbox->x1 & 0x000007ff) |
43 (((uint32_t) pbox->y1 << 16) & 0x07ff0000);
44 scend = (scend & ~SAVAGE_SCISSOR_MASK_S3D) |
45 (((uint32_t) pbox->x2 - 1) & 0x000007ff) |
46 ((((uint32_t) pbox->y2 - 1) << 16) & 0x07ff0000);
Dave Airlie282a1672005-08-07 15:43:54 +100047 if (scstart != dev_priv->state.s3d.scstart ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +100048 scend != dev_priv->state.s3d.scend) {
Dave Airlie282a1672005-08-07 15:43:54 +100049 DMA_LOCALS;
50 BEGIN_DMA(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051 DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
Dave Airlie282a1672005-08-07 15:43:54 +100052 DMA_SET_REGISTERS(SAVAGE_SCSTART_S3D, 2);
53 DMA_WRITE(scstart);
54 DMA_WRITE(scend);
55 dev_priv->state.s3d.scstart = scstart;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100056 dev_priv->state.s3d.scend = scend;
Dave Airlie282a1672005-08-07 15:43:54 +100057 dev_priv->waiting = 1;
58 DMA_COMMIT();
59 }
60}
61
Dave Airlieb5e89ed2005-09-25 14:28:13 +100062void savage_emit_clip_rect_s4(drm_savage_private_t * dev_priv,
Dave Airlieeddca552007-07-11 16:09:54 +100063 const struct drm_clip_rect * pbox)
Dave Airlie282a1672005-08-07 15:43:54 +100064{
65 uint32_t drawctrl0 = dev_priv->state.s4.new_drawctrl0;
66 uint32_t drawctrl1 = dev_priv->state.s4.new_drawctrl1;
67 drawctrl0 = (drawctrl0 & ~SAVAGE_SCISSOR_MASK_S4) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 ((uint32_t) pbox->x1 & 0x000007ff) |
69 (((uint32_t) pbox->y1 << 12) & 0x00fff000);
Dave Airlie282a1672005-08-07 15:43:54 +100070 drawctrl1 = (drawctrl1 & ~SAVAGE_SCISSOR_MASK_S4) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +100071 (((uint32_t) pbox->x2 - 1) & 0x000007ff) |
72 ((((uint32_t) pbox->y2 - 1) << 12) & 0x00fff000);
Dave Airlie282a1672005-08-07 15:43:54 +100073 if (drawctrl0 != dev_priv->state.s4.drawctrl0 ||
74 drawctrl1 != dev_priv->state.s4.drawctrl1) {
75 DMA_LOCALS;
76 BEGIN_DMA(4);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
Dave Airlie282a1672005-08-07 15:43:54 +100078 DMA_SET_REGISTERS(SAVAGE_DRAWCTRL0_S4, 2);
79 DMA_WRITE(drawctrl0);
80 DMA_WRITE(drawctrl1);
81 dev_priv->state.s4.drawctrl0 = drawctrl0;
82 dev_priv->state.s4.drawctrl1 = drawctrl1;
83 dev_priv->waiting = 1;
84 DMA_COMMIT();
85 }
86}
87
Dave Airlieb5e89ed2005-09-25 14:28:13 +100088static int savage_verify_texaddr(drm_savage_private_t * dev_priv, int unit,
Dave Airlie282a1672005-08-07 15:43:54 +100089 uint32_t addr)
90{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091 if ((addr & 6) != 2) { /* reserved bits */
Dave Airlie282a1672005-08-07 15:43:54 +100092 DRM_ERROR("bad texAddr%d %08x (reserved bits)\n", unit, addr);
Eric Anholt20caafa2007-08-25 19:22:43 +100093 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +100094 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 if (!(addr & 1)) { /* local */
Dave Airlie282a1672005-08-07 15:43:54 +100096 addr &= ~7;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097 if (addr < dev_priv->texture_offset ||
98 addr >= dev_priv->texture_offset + dev_priv->texture_size) {
99 DRM_ERROR
100 ("bad texAddr%d %08x (local addr out of range)\n",
101 unit, addr);
Eric Anholt20caafa2007-08-25 19:22:43 +1000102 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000103 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 } else { /* AGP */
Dave Airlie282a1672005-08-07 15:43:54 +1000105 if (!dev_priv->agp_textures) {
106 DRM_ERROR("bad texAddr%d %08x (AGP not available)\n",
107 unit, addr);
Eric Anholt20caafa2007-08-25 19:22:43 +1000108 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000109 }
110 addr &= ~7;
111 if (addr < dev_priv->agp_textures->offset ||
112 addr >= (dev_priv->agp_textures->offset +
113 dev_priv->agp_textures->size)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114 DRM_ERROR
115 ("bad texAddr%d %08x (AGP addr out of range)\n",
116 unit, addr);
Eric Anholt20caafa2007-08-25 19:22:43 +1000117 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000118 }
119 }
120 return 0;
121}
122
123#define SAVE_STATE(reg,where) \
124 if(start <= reg && start+count > reg) \
Dave Airlie3528af12006-01-02 16:11:44 +1100125 dev_priv->state.where = regs[reg - start]
Dave Airlie282a1672005-08-07 15:43:54 +1000126#define SAVE_STATE_MASK(reg,where,mask) do { \
127 if(start <= reg && start+count > reg) { \
128 uint32_t tmp; \
Dave Airlie3528af12006-01-02 16:11:44 +1100129 tmp = regs[reg - start]; \
Dave Airlie282a1672005-08-07 15:43:54 +1000130 dev_priv->state.where = (tmp & (mask)) | \
131 (dev_priv->state.where & ~(mask)); \
132 } \
133} while (0)
Dave Airlie3528af12006-01-02 16:11:44 +1100134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135static int savage_verify_state_s3d(drm_savage_private_t * dev_priv,
Dave Airlie282a1672005-08-07 15:43:54 +1000136 unsigned int start, unsigned int count,
Dave Airlie3528af12006-01-02 16:11:44 +1100137 const uint32_t *regs)
Dave Airlie282a1672005-08-07 15:43:54 +1000138{
139 if (start < SAVAGE_TEXPALADDR_S3D ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 start + count - 1 > SAVAGE_DESTTEXRWWATERMARK_S3D) {
Dave Airlie282a1672005-08-07 15:43:54 +1000141 DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142 start, start + count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +1000143 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000144 }
145
146 SAVE_STATE_MASK(SAVAGE_SCSTART_S3D, s3d.new_scstart,
147 ~SAVAGE_SCISSOR_MASK_S3D);
148 SAVE_STATE_MASK(SAVAGE_SCEND_S3D, s3d.new_scend,
149 ~SAVAGE_SCISSOR_MASK_S3D);
150
151 /* if any texture regs were changed ... */
152 if (start <= SAVAGE_TEXCTRL_S3D &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 start + count > SAVAGE_TEXPALADDR_S3D) {
Dave Airlie282a1672005-08-07 15:43:54 +1000154 /* ... check texture state */
155 SAVE_STATE(SAVAGE_TEXCTRL_S3D, s3d.texctrl);
156 SAVE_STATE(SAVAGE_TEXADDR_S3D, s3d.texaddr);
157 if (dev_priv->state.s3d.texctrl & SAVAGE_TEXCTRL_TEXEN_MASK)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 return savage_verify_texaddr(dev_priv, 0,
Dave Airlie3528af12006-01-02 16:11:44 +1100159 dev_priv->state.s3d.texaddr);
Dave Airlie282a1672005-08-07 15:43:54 +1000160 }
161
162 return 0;
163}
164
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000165static int savage_verify_state_s4(drm_savage_private_t * dev_priv,
Dave Airlie282a1672005-08-07 15:43:54 +1000166 unsigned int start, unsigned int count,
Dave Airlie3528af12006-01-02 16:11:44 +1100167 const uint32_t *regs)
Dave Airlie282a1672005-08-07 15:43:54 +1000168{
169 int ret = 0;
170
171 if (start < SAVAGE_DRAWLOCALCTRL_S4 ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 start + count - 1 > SAVAGE_TEXBLENDCOLOR_S4) {
Dave Airlie282a1672005-08-07 15:43:54 +1000173 DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 start, start + count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +1000175 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000176 }
177
178 SAVE_STATE_MASK(SAVAGE_DRAWCTRL0_S4, s4.new_drawctrl0,
179 ~SAVAGE_SCISSOR_MASK_S4);
180 SAVE_STATE_MASK(SAVAGE_DRAWCTRL1_S4, s4.new_drawctrl1,
181 ~SAVAGE_SCISSOR_MASK_S4);
182
183 /* if any texture regs were changed ... */
Dave Airlie3528af12006-01-02 16:11:44 +1100184 if (start <= SAVAGE_TEXDESCR_S4 &&
185 start + count > SAVAGE_TEXPALADDR_S4) {
Dave Airlie282a1672005-08-07 15:43:54 +1000186 /* ... check texture state */
187 SAVE_STATE(SAVAGE_TEXDESCR_S4, s4.texdescr);
188 SAVE_STATE(SAVAGE_TEXADDR0_S4, s4.texaddr0);
189 SAVE_STATE(SAVAGE_TEXADDR1_S4, s4.texaddr1);
190 if (dev_priv->state.s4.texdescr & SAVAGE_TEXDESCR_TEX0EN_MASK)
Dave Airlie3528af12006-01-02 16:11:44 +1100191 ret |= savage_verify_texaddr(dev_priv, 0,
192 dev_priv->state.s4.texaddr0);
Dave Airlie282a1672005-08-07 15:43:54 +1000193 if (dev_priv->state.s4.texdescr & SAVAGE_TEXDESCR_TEX1EN_MASK)
Dave Airlie3528af12006-01-02 16:11:44 +1100194 ret |= savage_verify_texaddr(dev_priv, 1,
195 dev_priv->state.s4.texaddr1);
Dave Airlie282a1672005-08-07 15:43:54 +1000196 }
197
198 return ret;
199}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200
Dave Airlie282a1672005-08-07 15:43:54 +1000201#undef SAVE_STATE
202#undef SAVE_STATE_MASK
203
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204static int savage_dispatch_state(drm_savage_private_t * dev_priv,
205 const drm_savage_cmd_header_t * cmd_header,
Dave Airlie3528af12006-01-02 16:11:44 +1100206 const uint32_t *regs)
Dave Airlie282a1672005-08-07 15:43:54 +1000207{
208 unsigned int count = cmd_header->state.count;
209 unsigned int start = cmd_header->state.start;
210 unsigned int count2 = 0;
211 unsigned int bci_size;
212 int ret;
213 DMA_LOCALS;
214
215 if (!count)
216 return 0;
217
Dave Airlie282a1672005-08-07 15:43:54 +1000218 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
219 ret = savage_verify_state_s3d(dev_priv, start, count, regs);
220 if (ret != 0)
221 return ret;
222 /* scissor regs are emitted in savage_dispatch_draw */
223 if (start < SAVAGE_SCSTART_S3D) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 if (start + count > SAVAGE_SCEND_S3D + 1)
225 count2 = count - (SAVAGE_SCEND_S3D + 1 - start);
226 if (start + count > SAVAGE_SCSTART_S3D)
Dave Airlie282a1672005-08-07 15:43:54 +1000227 count = SAVAGE_SCSTART_S3D - start;
228 } else if (start <= SAVAGE_SCEND_S3D) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 if (start + count > SAVAGE_SCEND_S3D + 1) {
230 count -= SAVAGE_SCEND_S3D + 1 - start;
231 start = SAVAGE_SCEND_S3D + 1;
Dave Airlie282a1672005-08-07 15:43:54 +1000232 } else
233 return 0;
234 }
235 } else {
236 ret = savage_verify_state_s4(dev_priv, start, count, regs);
237 if (ret != 0)
238 return ret;
239 /* scissor regs are emitted in savage_dispatch_draw */
240 if (start < SAVAGE_DRAWCTRL0_S4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (start + count > SAVAGE_DRAWCTRL1_S4 + 1)
Dave Airlie3528af12006-01-02 16:11:44 +1100242 count2 = count -
243 (SAVAGE_DRAWCTRL1_S4 + 1 - start);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 if (start + count > SAVAGE_DRAWCTRL0_S4)
Dave Airlie282a1672005-08-07 15:43:54 +1000245 count = SAVAGE_DRAWCTRL0_S4 - start;
246 } else if (start <= SAVAGE_DRAWCTRL1_S4) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 if (start + count > SAVAGE_DRAWCTRL1_S4 + 1) {
248 count -= SAVAGE_DRAWCTRL1_S4 + 1 - start;
249 start = SAVAGE_DRAWCTRL1_S4 + 1;
Dave Airlie282a1672005-08-07 15:43:54 +1000250 } else
251 return 0;
252 }
253 }
254
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000255 bci_size = count + (count + 254) / 255 + count2 + (count2 + 254) / 255;
Dave Airlie282a1672005-08-07 15:43:54 +1000256
257 if (cmd_header->state.global) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 BEGIN_DMA(bci_size + 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000259 DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
260 dev_priv->waiting = 1;
261 } else {
262 BEGIN_DMA(bci_size);
263 }
264
265 do {
266 while (count > 0) {
267 unsigned int n = count < 255 ? count : 255;
268 DMA_SET_REGISTERS(start, n);
Dave Airlie3528af12006-01-02 16:11:44 +1100269 DMA_COPY(regs, n);
Dave Airlie282a1672005-08-07 15:43:54 +1000270 count -= n;
271 start += n;
272 regs += n;
273 }
274 start += 2;
275 regs += 2;
276 count = count2;
277 count2 = 0;
278 } while (count);
279
280 DMA_COMMIT();
281
282 return 0;
283}
284
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285static int savage_dispatch_dma_prim(drm_savage_private_t * dev_priv,
286 const drm_savage_cmd_header_t * cmd_header,
Dave Airlie056219e2007-07-11 16:17:42 +1000287 const struct drm_buf * dmabuf)
Dave Airlie282a1672005-08-07 15:43:54 +1000288{
289 unsigned char reorder = 0;
290 unsigned int prim = cmd_header->prim.prim;
291 unsigned int skip = cmd_header->prim.skip;
292 unsigned int n = cmd_header->prim.count;
293 unsigned int start = cmd_header->prim.start;
294 unsigned int i;
295 BCI_LOCALS;
296
297 if (!dmabuf) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000298 DRM_ERROR("called without dma buffers!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000299 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000300 }
301
302 if (!n)
303 return 0;
304
305 switch (prim) {
306 case SAVAGE_PRIM_TRILIST_201:
307 reorder = 1;
308 prim = SAVAGE_PRIM_TRILIST;
Gustavo A. R. Silvabd4d73a2019-01-29 14:20:06 -0600309 /* fall through */
Dave Airlie282a1672005-08-07 15:43:54 +1000310 case SAVAGE_PRIM_TRILIST:
311 if (n % 3 != 0) {
312 DRM_ERROR("wrong number of vertices %u in TRILIST\n",
313 n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000314 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000315 }
316 break;
317 case SAVAGE_PRIM_TRISTRIP:
318 case SAVAGE_PRIM_TRIFAN:
319 if (n < 3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 DRM_ERROR
321 ("wrong number of vertices %u in TRIFAN/STRIP\n",
322 n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000323 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000324 }
325 break;
326 default:
327 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholt20caafa2007-08-25 19:22:43 +1000328 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000329 }
330
331 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
332 if (skip != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000334 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000335 }
336 } else {
337 unsigned int size = 10 - (skip & 1) - (skip >> 1 & 1) -
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 (skip >> 2 & 1) - (skip >> 3 & 1) - (skip >> 4 & 1) -
339 (skip >> 5 & 1) - (skip >> 6 & 1) - (skip >> 7 & 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000340 if (skip > SAVAGE_SKIP_ALL_S4 || size != 8) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000342 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000343 }
344 if (reorder) {
345 DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000346 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000347 }
348 }
349
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 if (start + n > dmabuf->total / 32) {
Dave Airlie282a1672005-08-07 15:43:54 +1000351 DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352 start, start + n - 1, dmabuf->total / 32);
Eric Anholt20caafa2007-08-25 19:22:43 +1000353 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000354 }
355
356 /* Vertex DMA doesn't work with command DMA at the same time,
357 * so we use BCI_... to submit commands here. Flush buffered
358 * faked DMA first. */
359 DMA_FLUSH();
360
361 if (dmabuf->bus_address != dev_priv->state.common.vbaddr) {
362 BEGIN_BCI(2);
363 BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR, 1);
364 BCI_WRITE(dmabuf->bus_address | dev_priv->dma_type);
365 dev_priv->state.common.vbaddr = dmabuf->bus_address;
366 }
367 if (S3_SAVAGE3D_SERIES(dev_priv->chipset) && dev_priv->waiting) {
368 /* Workaround for what looks like a hardware bug. If a
369 * WAIT_3D_IDLE was emitted some time before the
370 * indexed drawing command then the engine will lock
371 * up. There are two known workarounds:
372 * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
373 BEGIN_BCI(63);
374 for (i = 0; i < 63; ++i)
375 BCI_WRITE(BCI_CMD_WAIT);
376 dev_priv->waiting = 0;
377 }
378
379 prim <<= 25;
380 while (n != 0) {
381 /* Can emit up to 255 indices (85 triangles) at once. */
382 unsigned int count = n > 255 ? 255 : n;
383 if (reorder) {
384 /* Need to reorder indices for correct flat
385 * shading while preserving the clock sense
386 * for correct culling. Only on Savage3D. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 int reorder[3] = { -1, -1, -1 };
388 reorder[start % 3] = 2;
Dave Airlie282a1672005-08-07 15:43:54 +1000389
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000390 BEGIN_BCI((count + 1 + 1) / 2);
391 BCI_DRAW_INDICES_S3D(count, prim, start + 2);
Dave Airlie282a1672005-08-07 15:43:54 +1000392
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 for (i = start + 1; i + 1 < start + count; i += 2)
Dave Airlie282a1672005-08-07 15:43:54 +1000394 BCI_WRITE((i + reorder[i % 3]) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 ((i + 1 +
396 reorder[(i + 1) % 3]) << 16));
397 if (i < start + count)
398 BCI_WRITE(i + reorder[i % 3]);
Dave Airlie282a1672005-08-07 15:43:54 +1000399 } else if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000400 BEGIN_BCI((count + 1 + 1) / 2);
Dave Airlie282a1672005-08-07 15:43:54 +1000401 BCI_DRAW_INDICES_S3D(count, prim, start);
402
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 for (i = start + 1; i + 1 < start + count; i += 2)
404 BCI_WRITE(i | ((i + 1) << 16));
405 if (i < start + count)
Dave Airlie282a1672005-08-07 15:43:54 +1000406 BCI_WRITE(i);
407 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408 BEGIN_BCI((count + 2 + 1) / 2);
Dave Airlie282a1672005-08-07 15:43:54 +1000409 BCI_DRAW_INDICES_S4(count, prim, skip);
410
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 for (i = start; i + 1 < start + count; i += 2)
412 BCI_WRITE(i | ((i + 1) << 16));
413 if (i < start + count)
Dave Airlie282a1672005-08-07 15:43:54 +1000414 BCI_WRITE(i);
415 }
416
417 start += count;
418 n -= count;
419
420 prim |= BCI_CMD_DRAW_CONT;
421 }
422
423 return 0;
424}
425
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426static int savage_dispatch_vb_prim(drm_savage_private_t * dev_priv,
427 const drm_savage_cmd_header_t * cmd_header,
Dave Airlie3528af12006-01-02 16:11:44 +1100428 const uint32_t *vtxbuf, unsigned int vb_size,
429 unsigned int vb_stride)
Dave Airlie282a1672005-08-07 15:43:54 +1000430{
431 unsigned char reorder = 0;
432 unsigned int prim = cmd_header->prim.prim;
433 unsigned int skip = cmd_header->prim.skip;
434 unsigned int n = cmd_header->prim.count;
435 unsigned int start = cmd_header->prim.start;
436 unsigned int vtx_size;
437 unsigned int i;
438 DMA_LOCALS;
439
440 if (!n)
441 return 0;
442
443 switch (prim) {
444 case SAVAGE_PRIM_TRILIST_201:
445 reorder = 1;
446 prim = SAVAGE_PRIM_TRILIST;
Gustavo A. R. Silvabd4d73a2019-01-29 14:20:06 -0600447 /* fall through */
Dave Airlie282a1672005-08-07 15:43:54 +1000448 case SAVAGE_PRIM_TRILIST:
449 if (n % 3 != 0) {
450 DRM_ERROR("wrong number of vertices %u in TRILIST\n",
451 n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000452 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000453 }
454 break;
455 case SAVAGE_PRIM_TRISTRIP:
456 case SAVAGE_PRIM_TRIFAN:
457 if (n < 3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 DRM_ERROR
459 ("wrong number of vertices %u in TRIFAN/STRIP\n",
460 n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000461 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000462 }
463 break;
464 default:
465 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholt20caafa2007-08-25 19:22:43 +1000466 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000467 }
468
469 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
470 if (skip > SAVAGE_SKIP_ALL_S3D) {
471 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000472 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000473 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 vtx_size = 8; /* full vertex */
Dave Airlie282a1672005-08-07 15:43:54 +1000475 } else {
476 if (skip > SAVAGE_SKIP_ALL_S4) {
477 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000478 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000479 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000480 vtx_size = 10; /* full vertex */
Dave Airlie282a1672005-08-07 15:43:54 +1000481 }
482
483 vtx_size -= (skip & 1) + (skip >> 1 & 1) +
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 (skip >> 2 & 1) + (skip >> 3 & 1) + (skip >> 4 & 1) +
485 (skip >> 5 & 1) + (skip >> 6 & 1) + (skip >> 7 & 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000486
487 if (vtx_size > vb_stride) {
488 DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
489 vtx_size, vb_stride);
Eric Anholt20caafa2007-08-25 19:22:43 +1000490 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000491 }
492
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 if (start + n > vb_size / (vb_stride * 4)) {
Dave Airlie282a1672005-08-07 15:43:54 +1000494 DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 start, start + n - 1, vb_size / (vb_stride * 4));
Eric Anholt20caafa2007-08-25 19:22:43 +1000496 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000497 }
498
499 prim <<= 25;
500 while (n != 0) {
501 /* Can emit up to 255 vertices (85 triangles) at once. */
502 unsigned int count = n > 255 ? 255 : n;
503 if (reorder) {
504 /* Need to reorder vertices for correct flat
505 * shading while preserving the clock sense
506 * for correct culling. Only on Savage3D. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 int reorder[3] = { -1, -1, -1 };
508 reorder[start % 3] = 2;
Dave Airlie282a1672005-08-07 15:43:54 +1000509
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000510 BEGIN_DMA(count * vtx_size + 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000511 DMA_DRAW_PRIMITIVE(count, prim, skip);
512
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513 for (i = start; i < start + count; ++i) {
Dave Airlie282a1672005-08-07 15:43:54 +1000514 unsigned int j = i + reorder[i % 3];
Dave Airlie3528af12006-01-02 16:11:44 +1100515 DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
Dave Airlie282a1672005-08-07 15:43:54 +1000516 }
517
518 DMA_COMMIT();
519 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 BEGIN_DMA(count * vtx_size + 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000521 DMA_DRAW_PRIMITIVE(count, prim, skip);
522
523 if (vb_stride == vtx_size) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000524 DMA_COPY(&vtxbuf[vb_stride * start],
Dave Airlie3528af12006-01-02 16:11:44 +1100525 vtx_size * count);
Dave Airlie282a1672005-08-07 15:43:54 +1000526 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000527 for (i = start; i < start + count; ++i) {
Dave Airlie3528af12006-01-02 16:11:44 +1100528 DMA_COPY(&vtxbuf [vb_stride * i],
529 vtx_size);
Dave Airlie282a1672005-08-07 15:43:54 +1000530 }
531 }
532
533 DMA_COMMIT();
534 }
535
536 start += count;
537 n -= count;
538
539 prim |= BCI_CMD_DRAW_CONT;
540 }
541
542 return 0;
543}
544
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545static int savage_dispatch_dma_idx(drm_savage_private_t * dev_priv,
546 const drm_savage_cmd_header_t * cmd_header,
Dave Airlie3528af12006-01-02 16:11:44 +1100547 const uint16_t *idx,
Dave Airlie056219e2007-07-11 16:17:42 +1000548 const struct drm_buf * dmabuf)
Dave Airlie282a1672005-08-07 15:43:54 +1000549{
550 unsigned char reorder = 0;
551 unsigned int prim = cmd_header->idx.prim;
552 unsigned int skip = cmd_header->idx.skip;
553 unsigned int n = cmd_header->idx.count;
554 unsigned int i;
555 BCI_LOCALS;
556
557 if (!dmabuf) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558 DRM_ERROR("called without dma buffers!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000559 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000560 }
561
562 if (!n)
563 return 0;
564
565 switch (prim) {
566 case SAVAGE_PRIM_TRILIST_201:
567 reorder = 1;
568 prim = SAVAGE_PRIM_TRILIST;
Gustavo A. R. Silvabd4d73a2019-01-29 14:20:06 -0600569 /* fall through */
Dave Airlie282a1672005-08-07 15:43:54 +1000570 case SAVAGE_PRIM_TRILIST:
571 if (n % 3 != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 DRM_ERROR("wrong number of indices %u in TRILIST\n", n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000573 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000574 }
575 break;
576 case SAVAGE_PRIM_TRISTRIP:
577 case SAVAGE_PRIM_TRIFAN:
578 if (n < 3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 DRM_ERROR
580 ("wrong number of indices %u in TRIFAN/STRIP\n", n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000581 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000582 }
583 break;
584 default:
585 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholt20caafa2007-08-25 19:22:43 +1000586 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000587 }
588
589 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
590 if (skip != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000592 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000593 }
594 } else {
595 unsigned int size = 10 - (skip & 1) - (skip >> 1 & 1) -
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 (skip >> 2 & 1) - (skip >> 3 & 1) - (skip >> 4 & 1) -
597 (skip >> 5 & 1) - (skip >> 6 & 1) - (skip >> 7 & 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000598 if (skip > SAVAGE_SKIP_ALL_S4 || size != 8) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000600 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000601 }
602 if (reorder) {
603 DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000604 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000605 }
606 }
607
608 /* Vertex DMA doesn't work with command DMA at the same time,
609 * so we use BCI_... to submit commands here. Flush buffered
610 * faked DMA first. */
611 DMA_FLUSH();
612
613 if (dmabuf->bus_address != dev_priv->state.common.vbaddr) {
614 BEGIN_BCI(2);
615 BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR, 1);
616 BCI_WRITE(dmabuf->bus_address | dev_priv->dma_type);
617 dev_priv->state.common.vbaddr = dmabuf->bus_address;
618 }
619 if (S3_SAVAGE3D_SERIES(dev_priv->chipset) && dev_priv->waiting) {
620 /* Workaround for what looks like a hardware bug. If a
621 * WAIT_3D_IDLE was emitted some time before the
622 * indexed drawing command then the engine will lock
623 * up. There are two known workarounds:
624 * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
625 BEGIN_BCI(63);
626 for (i = 0; i < 63; ++i)
627 BCI_WRITE(BCI_CMD_WAIT);
628 dev_priv->waiting = 0;
629 }
630
631 prim <<= 25;
632 while (n != 0) {
633 /* Can emit up to 255 indices (85 triangles) at once. */
634 unsigned int count = n > 255 ? 255 : n;
Dave Airlie282a1672005-08-07 15:43:54 +1000635
Dave Airlie3528af12006-01-02 16:11:44 +1100636 /* check indices */
Dave Airlie282a1672005-08-07 15:43:54 +1000637 for (i = 0; i < count; ++i) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 if (idx[i] > dmabuf->total / 32) {
Dave Airlie282a1672005-08-07 15:43:54 +1000639 DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 i, idx[i], dmabuf->total / 32);
Eric Anholt20caafa2007-08-25 19:22:43 +1000641 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000642 }
643 }
644
645 if (reorder) {
646 /* Need to reorder indices for correct flat
647 * shading while preserving the clock sense
648 * for correct culling. Only on Savage3D. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 int reorder[3] = { 2, -1, -1 };
Dave Airlie282a1672005-08-07 15:43:54 +1000650
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651 BEGIN_BCI((count + 1 + 1) / 2);
Dave Airlie282a1672005-08-07 15:43:54 +1000652 BCI_DRAW_INDICES_S3D(count, prim, idx[2]);
653
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 for (i = 1; i + 1 < count; i += 2)
Dave Airlie282a1672005-08-07 15:43:54 +1000655 BCI_WRITE(idx[i + reorder[i % 3]] |
Dave Airlie3528af12006-01-02 16:11:44 +1100656 (idx[i + 1 +
657 reorder[(i + 1) % 3]] << 16));
Dave Airlie282a1672005-08-07 15:43:54 +1000658 if (i < count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 BCI_WRITE(idx[i + reorder[i % 3]]);
Dave Airlie282a1672005-08-07 15:43:54 +1000660 } else if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 BEGIN_BCI((count + 1 + 1) / 2);
Dave Airlie282a1672005-08-07 15:43:54 +1000662 BCI_DRAW_INDICES_S3D(count, prim, idx[0]);
663
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000664 for (i = 1; i + 1 < count; i += 2)
665 BCI_WRITE(idx[i] | (idx[i + 1] << 16));
Dave Airlie282a1672005-08-07 15:43:54 +1000666 if (i < count)
667 BCI_WRITE(idx[i]);
668 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 BEGIN_BCI((count + 2 + 1) / 2);
Dave Airlie282a1672005-08-07 15:43:54 +1000670 BCI_DRAW_INDICES_S4(count, prim, skip);
671
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000672 for (i = 0; i + 1 < count; i += 2)
673 BCI_WRITE(idx[i] | (idx[i + 1] << 16));
Dave Airlie282a1672005-08-07 15:43:54 +1000674 if (i < count)
675 BCI_WRITE(idx[i]);
676 }
677
Dave Airlie3528af12006-01-02 16:11:44 +1100678 idx += count;
Dave Airlie282a1672005-08-07 15:43:54 +1000679 n -= count;
680
681 prim |= BCI_CMD_DRAW_CONT;
682 }
683
684 return 0;
685}
686
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687static int savage_dispatch_vb_idx(drm_savage_private_t * dev_priv,
688 const drm_savage_cmd_header_t * cmd_header,
Dave Airlie3528af12006-01-02 16:11:44 +1100689 const uint16_t *idx,
690 const uint32_t *vtxbuf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 unsigned int vb_size, unsigned int vb_stride)
Dave Airlie282a1672005-08-07 15:43:54 +1000692{
693 unsigned char reorder = 0;
694 unsigned int prim = cmd_header->idx.prim;
695 unsigned int skip = cmd_header->idx.skip;
696 unsigned int n = cmd_header->idx.count;
697 unsigned int vtx_size;
698 unsigned int i;
699 DMA_LOCALS;
700
701 if (!n)
702 return 0;
703
704 switch (prim) {
705 case SAVAGE_PRIM_TRILIST_201:
706 reorder = 1;
707 prim = SAVAGE_PRIM_TRILIST;
Gustavo A. R. Silvabd4d73a2019-01-29 14:20:06 -0600708 /* fall through */
Dave Airlie282a1672005-08-07 15:43:54 +1000709 case SAVAGE_PRIM_TRILIST:
710 if (n % 3 != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 DRM_ERROR("wrong number of indices %u in TRILIST\n", n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000712 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000713 }
714 break;
715 case SAVAGE_PRIM_TRISTRIP:
716 case SAVAGE_PRIM_TRIFAN:
717 if (n < 3) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 DRM_ERROR
719 ("wrong number of indices %u in TRIFAN/STRIP\n", n);
Eric Anholt20caafa2007-08-25 19:22:43 +1000720 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000721 }
722 break;
723 default:
724 DRM_ERROR("invalid primitive type %u\n", prim);
Eric Anholt20caafa2007-08-25 19:22:43 +1000725 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000726 }
727
728 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
729 if (skip > SAVAGE_SKIP_ALL_S3D) {
730 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000731 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000732 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 vtx_size = 8; /* full vertex */
Dave Airlie282a1672005-08-07 15:43:54 +1000734 } else {
735 if (skip > SAVAGE_SKIP_ALL_S4) {
736 DRM_ERROR("invalid skip flags 0x%04x\n", skip);
Eric Anholt20caafa2007-08-25 19:22:43 +1000737 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000738 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 vtx_size = 10; /* full vertex */
Dave Airlie282a1672005-08-07 15:43:54 +1000740 }
741
742 vtx_size -= (skip & 1) + (skip >> 1 & 1) +
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 (skip >> 2 & 1) + (skip >> 3 & 1) + (skip >> 4 & 1) +
744 (skip >> 5 & 1) + (skip >> 6 & 1) + (skip >> 7 & 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000745
746 if (vtx_size > vb_stride) {
747 DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
748 vtx_size, vb_stride);
Eric Anholt20caafa2007-08-25 19:22:43 +1000749 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000750 }
751
752 prim <<= 25;
753 while (n != 0) {
754 /* Can emit up to 255 vertices (85 triangles) at once. */
755 unsigned int count = n > 255 ? 255 : n;
Dave Airliebc5f4522007-11-05 12:50:58 +1000756
Dave Airlie3528af12006-01-02 16:11:44 +1100757 /* Check indices */
Dave Airlie282a1672005-08-07 15:43:54 +1000758 for (i = 0; i < count; ++i) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 if (idx[i] > vb_size / (vb_stride * 4)) {
Dave Airlie282a1672005-08-07 15:43:54 +1000760 DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 i, idx[i], vb_size / (vb_stride * 4));
Eric Anholt20caafa2007-08-25 19:22:43 +1000762 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000763 }
764 }
765
766 if (reorder) {
767 /* Need to reorder vertices for correct flat
768 * shading while preserving the clock sense
769 * for correct culling. Only on Savage3D. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 int reorder[3] = { 2, -1, -1 };
Dave Airlie282a1672005-08-07 15:43:54 +1000771
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000772 BEGIN_DMA(count * vtx_size + 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000773 DMA_DRAW_PRIMITIVE(count, prim, skip);
774
775 for (i = 0; i < count; ++i) {
776 unsigned int j = idx[i + reorder[i % 3]];
Dave Airlie3528af12006-01-02 16:11:44 +1100777 DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
Dave Airlie282a1672005-08-07 15:43:54 +1000778 }
779
780 DMA_COMMIT();
781 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 BEGIN_DMA(count * vtx_size + 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000783 DMA_DRAW_PRIMITIVE(count, prim, skip);
784
785 for (i = 0; i < count; ++i) {
786 unsigned int j = idx[i];
Dave Airlie3528af12006-01-02 16:11:44 +1100787 DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
Dave Airlie282a1672005-08-07 15:43:54 +1000788 }
789
790 DMA_COMMIT();
791 }
792
Dave Airlie3528af12006-01-02 16:11:44 +1100793 idx += count;
Dave Airlie282a1672005-08-07 15:43:54 +1000794 n -= count;
795
796 prim |= BCI_CMD_DRAW_CONT;
797 }
798
799 return 0;
800}
801
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802static int savage_dispatch_clear(drm_savage_private_t * dev_priv,
803 const drm_savage_cmd_header_t * cmd_header,
Dave Airlie3528af12006-01-02 16:11:44 +1100804 const drm_savage_cmd_header_t *data,
Dave Airlie282a1672005-08-07 15:43:54 +1000805 unsigned int nbox,
Dave Airlieeddca552007-07-11 16:09:54 +1000806 const struct drm_clip_rect *boxes)
Dave Airlie282a1672005-08-07 15:43:54 +1000807{
Dave Airlie3528af12006-01-02 16:11:44 +1100808 unsigned int flags = cmd_header->clear0.flags;
Dave Airlie282a1672005-08-07 15:43:54 +1000809 unsigned int clear_cmd;
810 unsigned int i, nbufs;
811 DMA_LOCALS;
812
813 if (nbox == 0)
814 return 0;
815
Dave Airlie282a1672005-08-07 15:43:54 +1000816 clear_cmd = BCI_CMD_RECT | BCI_CMD_RECT_XP | BCI_CMD_RECT_YP |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 BCI_CMD_SEND_COLOR | BCI_CMD_DEST_PBD_NEW;
818 BCI_CMD_SET_ROP(clear_cmd, 0xCC);
Dave Airlie282a1672005-08-07 15:43:54 +1000819
820 nbufs = ((flags & SAVAGE_FRONT) ? 1 : 0) +
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 ((flags & SAVAGE_BACK) ? 1 : 0) + ((flags & SAVAGE_DEPTH) ? 1 : 0);
Dave Airlie282a1672005-08-07 15:43:54 +1000822 if (nbufs == 0)
823 return 0;
824
Dave Airlie3528af12006-01-02 16:11:44 +1100825 if (data->clear1.mask != 0xffffffff) {
Dave Airlie282a1672005-08-07 15:43:54 +1000826 /* set mask */
827 BEGIN_DMA(2);
828 DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK, 1);
Dave Airlie3528af12006-01-02 16:11:44 +1100829 DMA_WRITE(data->clear1.mask);
Dave Airlie282a1672005-08-07 15:43:54 +1000830 DMA_COMMIT();
831 }
832 for (i = 0; i < nbox; ++i) {
Dave Airlie282a1672005-08-07 15:43:54 +1000833 unsigned int x, y, w, h;
834 unsigned int buf;
Dave Airlie3528af12006-01-02 16:11:44 +1100835 x = boxes[i].x1, y = boxes[i].y1;
836 w = boxes[i].x2 - boxes[i].x1;
837 h = boxes[i].y2 - boxes[i].y1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 BEGIN_DMA(nbufs * 6);
Dave Airlie282a1672005-08-07 15:43:54 +1000839 for (buf = SAVAGE_FRONT; buf <= SAVAGE_DEPTH; buf <<= 1) {
840 if (!(flags & buf))
841 continue;
842 DMA_WRITE(clear_cmd);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 switch (buf) {
Dave Airlie282a1672005-08-07 15:43:54 +1000844 case SAVAGE_FRONT:
845 DMA_WRITE(dev_priv->front_offset);
846 DMA_WRITE(dev_priv->front_bd);
847 break;
848 case SAVAGE_BACK:
849 DMA_WRITE(dev_priv->back_offset);
850 DMA_WRITE(dev_priv->back_bd);
851 break;
852 case SAVAGE_DEPTH:
853 DMA_WRITE(dev_priv->depth_offset);
854 DMA_WRITE(dev_priv->depth_bd);
855 break;
856 }
Dave Airlie3528af12006-01-02 16:11:44 +1100857 DMA_WRITE(data->clear1.value);
Dave Airlie282a1672005-08-07 15:43:54 +1000858 DMA_WRITE(BCI_X_Y(x, y));
859 DMA_WRITE(BCI_W_H(w, h));
860 }
861 DMA_COMMIT();
862 }
Dave Airlie3528af12006-01-02 16:11:44 +1100863 if (data->clear1.mask != 0xffffffff) {
Dave Airlie282a1672005-08-07 15:43:54 +1000864 /* reset mask */
865 BEGIN_DMA(2);
866 DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK, 1);
867 DMA_WRITE(0xffffffff);
868 DMA_COMMIT();
869 }
870
871 return 0;
872}
873
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874static int savage_dispatch_swap(drm_savage_private_t * dev_priv,
Dave Airlieeddca552007-07-11 16:09:54 +1000875 unsigned int nbox, const struct drm_clip_rect *boxes)
Dave Airlie282a1672005-08-07 15:43:54 +1000876{
877 unsigned int swap_cmd;
878 unsigned int i;
879 DMA_LOCALS;
880
881 if (nbox == 0)
882 return 0;
883
884 swap_cmd = BCI_CMD_RECT | BCI_CMD_RECT_XP | BCI_CMD_RECT_YP |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000885 BCI_CMD_SRC_PBD_COLOR_NEW | BCI_CMD_DEST_GBD;
886 BCI_CMD_SET_ROP(swap_cmd, 0xCC);
Dave Airlie282a1672005-08-07 15:43:54 +1000887
888 for (i = 0; i < nbox; ++i) {
Dave Airlie282a1672005-08-07 15:43:54 +1000889 BEGIN_DMA(6);
890 DMA_WRITE(swap_cmd);
891 DMA_WRITE(dev_priv->back_offset);
892 DMA_WRITE(dev_priv->back_bd);
Dave Airlie3528af12006-01-02 16:11:44 +1100893 DMA_WRITE(BCI_X_Y(boxes[i].x1, boxes[i].y1));
894 DMA_WRITE(BCI_X_Y(boxes[i].x1, boxes[i].y1));
895 DMA_WRITE(BCI_W_H(boxes[i].x2 - boxes[i].x1,
896 boxes[i].y2 - boxes[i].y1));
Dave Airlie282a1672005-08-07 15:43:54 +1000897 DMA_COMMIT();
898 }
899
900 return 0;
901}
902
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903static int savage_dispatch_draw(drm_savage_private_t * dev_priv,
Dave Airlie3528af12006-01-02 16:11:44 +1100904 const drm_savage_cmd_header_t *start,
905 const drm_savage_cmd_header_t *end,
Dave Airlie056219e2007-07-11 16:17:42 +1000906 const struct drm_buf * dmabuf,
Dave Airlie3528af12006-01-02 16:11:44 +1100907 const unsigned int *vtxbuf,
Dave Airlie282a1672005-08-07 15:43:54 +1000908 unsigned int vb_size, unsigned int vb_stride,
909 unsigned int nbox,
Dave Airlieeddca552007-07-11 16:09:54 +1000910 const struct drm_clip_rect *boxes)
Dave Airlie282a1672005-08-07 15:43:54 +1000911{
912 unsigned int i, j;
913 int ret;
914
915 for (i = 0; i < nbox; ++i) {
Dave Airlie3528af12006-01-02 16:11:44 +1100916 const drm_savage_cmd_header_t *cmdbuf;
917 dev_priv->emit_clip_rect(dev_priv, &boxes[i]);
Dave Airlie282a1672005-08-07 15:43:54 +1000918
Dave Airlie3528af12006-01-02 16:11:44 +1100919 cmdbuf = start;
920 while (cmdbuf < end) {
Dave Airlie282a1672005-08-07 15:43:54 +1000921 drm_savage_cmd_header_t cmd_header;
Dave Airlie3528af12006-01-02 16:11:44 +1100922 cmd_header = *cmdbuf;
923 cmdbuf++;
Dave Airlie282a1672005-08-07 15:43:54 +1000924 switch (cmd_header.cmd.cmd) {
925 case SAVAGE_CMD_DMA_PRIM:
Dave Airlie3528af12006-01-02 16:11:44 +1100926 ret = savage_dispatch_dma_prim(
927 dev_priv, &cmd_header, dmabuf);
Dave Airlie282a1672005-08-07 15:43:54 +1000928 break;
929 case SAVAGE_CMD_VB_PRIM:
Dave Airlie3528af12006-01-02 16:11:44 +1100930 ret = savage_dispatch_vb_prim(
931 dev_priv, &cmd_header,
932 vtxbuf, vb_size, vb_stride);
Dave Airlie282a1672005-08-07 15:43:54 +1000933 break;
934 case SAVAGE_CMD_DMA_IDX:
935 j = (cmd_header.idx.count + 3) / 4;
936 /* j was check in savage_bci_cmdbuf */
Dave Airlie3528af12006-01-02 16:11:44 +1100937 ret = savage_dispatch_dma_idx(dev_priv,
938 &cmd_header, (const uint16_t *)cmdbuf,
939 dmabuf);
940 cmdbuf += j;
Dave Airlie282a1672005-08-07 15:43:54 +1000941 break;
942 case SAVAGE_CMD_VB_IDX:
943 j = (cmd_header.idx.count + 3) / 4;
944 /* j was check in savage_bci_cmdbuf */
Dave Airlie3528af12006-01-02 16:11:44 +1100945 ret = savage_dispatch_vb_idx(dev_priv,
946 &cmd_header, (const uint16_t *)cmdbuf,
Dave Airliebc5f4522007-11-05 12:50:58 +1000947 (const uint32_t *)vtxbuf, vb_size,
Dave Airlie3528af12006-01-02 16:11:44 +1100948 vb_stride);
949 cmdbuf += j;
Dave Airlie282a1672005-08-07 15:43:54 +1000950 break;
951 default:
952 /* What's the best return code? EFAULT? */
953 DRM_ERROR("IMPLEMENTATION ERROR: "
954 "non-drawing-command %d\n",
955 cmd_header.cmd.cmd);
Eric Anholt20caafa2007-08-25 19:22:43 +1000956 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000957 }
958
959 if (ret != 0)
960 return ret;
961 }
962 }
963
964 return 0;
965}
966
Eric Anholtc153f452007-09-03 12:06:45 +1000967int savage_bci_cmdbuf(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000968{
Dave Airlie282a1672005-08-07 15:43:54 +1000969 drm_savage_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000970 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000971 struct drm_buf *dmabuf;
Eric Anholtc153f452007-09-03 12:06:45 +1000972 drm_savage_cmdbuf_t *cmdbuf = data;
Dave Airlie3528af12006-01-02 16:11:44 +1100973 drm_savage_cmd_header_t *kcmd_addr = NULL;
974 drm_savage_cmd_header_t *first_draw_cmd;
975 unsigned int *kvb_addr = NULL;
Dave Airlieeddca552007-07-11 16:09:54 +1000976 struct drm_clip_rect *kbox_addr = NULL;
Dave Airlie282a1672005-08-07 15:43:54 +1000977 unsigned int i, j;
978 int ret = 0;
979
980 DRM_DEBUG("\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000981
Eric Anholt6c340ea2007-08-25 20:23:09 +1000982 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlie282a1672005-08-07 15:43:54 +1000983
Dave Airlie282a1672005-08-07 15:43:54 +1000984 if (dma && dma->buflist) {
Dan Carpenter968d72e2018-07-04 12:48:10 +0300985 if (cmdbuf->dma_idx >= dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 DRM_ERROR
987 ("vertex buffer index %u out of range (0-%u)\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000988 cmdbuf->dma_idx, dma->buf_count - 1);
Eric Anholt20caafa2007-08-25 19:22:43 +1000989 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000990 }
Eric Anholtc153f452007-09-03 12:06:45 +1000991 dmabuf = dma->buflist[cmdbuf->dma_idx];
Dave Airlie282a1672005-08-07 15:43:54 +1000992 } else {
993 dmabuf = NULL;
994 }
995
Dave Airlie3528af12006-01-02 16:11:44 +1100996 /* Copy the user buffers into kernel temporary areas. This hasn't been
997 * a performance loss compared to VERIFYAREA_READ/
998 * COPY_FROM_USER_UNCHECKED when done in other drivers, and is correct
999 * for locking on FreeBSD.
1000 */
Eric Anholtc153f452007-09-03 12:06:45 +10001001 if (cmdbuf->size) {
Xi Wang6587eb82012-04-06 17:38:24 -04001002 kcmd_addr = kmalloc_array(cmdbuf->size, 8, GFP_KERNEL);
Dave Airlie3528af12006-01-02 16:11:44 +11001003 if (kcmd_addr == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001004 return -ENOMEM;
Dave Airlie3528af12006-01-02 16:11:44 +11001005
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001006 if (copy_from_user(kcmd_addr, cmdbuf->cmd_addr,
Eric Anholtc153f452007-09-03 12:06:45 +10001007 cmdbuf->size * 8))
Dave Airlie3528af12006-01-02 16:11:44 +11001008 {
Eric Anholt9a298b22009-03-24 12:23:04 -07001009 kfree(kcmd_addr);
Eric Anholt20caafa2007-08-25 19:22:43 +10001010 return -EFAULT;
Dave Airlie3528af12006-01-02 16:11:44 +11001011 }
Eric Anholtc153f452007-09-03 12:06:45 +10001012 cmdbuf->cmd_addr = kcmd_addr;
Dave Airlie3528af12006-01-02 16:11:44 +11001013 }
Eric Anholtc153f452007-09-03 12:06:45 +10001014 if (cmdbuf->vb_size) {
Markus Elfringa6e39182016-08-18 18:12:03 +02001015 kvb_addr = memdup_user(cmdbuf->vb_addr, cmdbuf->vb_size);
1016 if (IS_ERR(kvb_addr)) {
1017 ret = PTR_ERR(kvb_addr);
Dan Carpenterf7741aa2016-10-12 09:22:27 +03001018 kvb_addr = NULL;
Dave Airlie3528af12006-01-02 16:11:44 +11001019 goto done;
1020 }
Eric Anholtc153f452007-09-03 12:06:45 +10001021 cmdbuf->vb_addr = kvb_addr;
Dave Airlie3528af12006-01-02 16:11:44 +11001022 }
Eric Anholtc153f452007-09-03 12:06:45 +10001023 if (cmdbuf->nbox) {
Xi Wang6587eb82012-04-06 17:38:24 -04001024 kbox_addr = kmalloc_array(cmdbuf->nbox, sizeof(struct drm_clip_rect),
1025 GFP_KERNEL);
Dave Airlie3528af12006-01-02 16:11:44 +11001026 if (kbox_addr == NULL) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001027 ret = -ENOMEM;
Dave Airlie3528af12006-01-02 16:11:44 +11001028 goto done;
1029 }
1030
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001031 if (copy_from_user(kbox_addr, cmdbuf->box_addr,
Eric Anholtc153f452007-09-03 12:06:45 +10001032 cmdbuf->nbox * sizeof(struct drm_clip_rect))) {
Eric Anholt20caafa2007-08-25 19:22:43 +10001033 ret = -EFAULT;
Dave Airlie3528af12006-01-02 16:11:44 +11001034 goto done;
1035 }
Eric Anholtc153f452007-09-03 12:06:45 +10001036 cmdbuf->box_addr = kbox_addr;
Dave Airlie3528af12006-01-02 16:11:44 +11001037 }
Dave Airlie282a1672005-08-07 15:43:54 +10001038
1039 /* Make sure writes to DMA buffers are finished before sending
1040 * DMA commands to the graphics hardware. */
Daniel Vetter85b23312013-12-11 11:34:45 +01001041 mb();
Dave Airlie282a1672005-08-07 15:43:54 +10001042
1043 /* Coming from user space. Don't know if the Xserver has
1044 * emitted wait commands. Assuming the worst. */
1045 dev_priv->waiting = 1;
1046
1047 i = 0;
1048 first_draw_cmd = NULL;
Eric Anholtc153f452007-09-03 12:06:45 +10001049 while (i < cmdbuf->size) {
Dave Airlie282a1672005-08-07 15:43:54 +10001050 drm_savage_cmd_header_t cmd_header;
Eric Anholtc153f452007-09-03 12:06:45 +10001051 cmd_header = *(drm_savage_cmd_header_t *)cmdbuf->cmd_addr;
1052 cmdbuf->cmd_addr++;
Dave Airlie282a1672005-08-07 15:43:54 +10001053 i++;
1054
1055 /* Group drawing commands with same state to minimize
1056 * iterations over clip rects. */
1057 j = 0;
1058 switch (cmd_header.cmd.cmd) {
1059 case SAVAGE_CMD_DMA_IDX:
1060 case SAVAGE_CMD_VB_IDX:
1061 j = (cmd_header.idx.count + 3) / 4;
Eric Anholtc153f452007-09-03 12:06:45 +10001062 if (i + j > cmdbuf->size) {
Dave Airlie282a1672005-08-07 15:43:54 +10001063 DRM_ERROR("indexed drawing command extends "
1064 "beyond end of command buffer\n");
1065 DMA_FLUSH();
Julia Lawallf1048762012-03-17 18:57:02 +01001066 ret = -EINVAL;
1067 goto done;
Dave Airlie282a1672005-08-07 15:43:54 +10001068 }
1069 /* fall through */
1070 case SAVAGE_CMD_DMA_PRIM:
1071 case SAVAGE_CMD_VB_PRIM:
1072 if (!first_draw_cmd)
Eric Anholtc153f452007-09-03 12:06:45 +10001073 first_draw_cmd = cmdbuf->cmd_addr - 1;
1074 cmdbuf->cmd_addr += j;
Dave Airlie282a1672005-08-07 15:43:54 +10001075 i += j;
1076 break;
1077 default:
1078 if (first_draw_cmd) {
Dave Airlie3528af12006-01-02 16:11:44 +11001079 ret = savage_dispatch_draw(
1080 dev_priv, first_draw_cmd,
Eric Anholtc153f452007-09-03 12:06:45 +10001081 cmdbuf->cmd_addr - 1,
1082 dmabuf, cmdbuf->vb_addr, cmdbuf->vb_size,
1083 cmdbuf->vb_stride,
1084 cmdbuf->nbox, cmdbuf->box_addr);
Dave Airlie282a1672005-08-07 15:43:54 +10001085 if (ret != 0)
Julia Lawallf1048762012-03-17 18:57:02 +01001086 goto done;
Dave Airlie282a1672005-08-07 15:43:54 +10001087 first_draw_cmd = NULL;
1088 }
1089 }
1090 if (first_draw_cmd)
1091 continue;
1092
1093 switch (cmd_header.cmd.cmd) {
1094 case SAVAGE_CMD_STATE:
1095 j = (cmd_header.state.count + 1) / 2;
Eric Anholtc153f452007-09-03 12:06:45 +10001096 if (i + j > cmdbuf->size) {
Dave Airlie282a1672005-08-07 15:43:54 +10001097 DRM_ERROR("command SAVAGE_CMD_STATE extends "
1098 "beyond end of command buffer\n");
1099 DMA_FLUSH();
Eric Anholt20caafa2007-08-25 19:22:43 +10001100 ret = -EINVAL;
Dave Airlie3528af12006-01-02 16:11:44 +11001101 goto done;
Dave Airlie282a1672005-08-07 15:43:54 +10001102 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 ret = savage_dispatch_state(dev_priv, &cmd_header,
Eric Anholtc153f452007-09-03 12:06:45 +10001104 (const uint32_t *)cmdbuf->cmd_addr);
1105 cmdbuf->cmd_addr += j;
Dave Airlie282a1672005-08-07 15:43:54 +10001106 i += j;
1107 break;
1108 case SAVAGE_CMD_CLEAR:
Eric Anholtc153f452007-09-03 12:06:45 +10001109 if (i + 1 > cmdbuf->size) {
Dave Airlie282a1672005-08-07 15:43:54 +10001110 DRM_ERROR("command SAVAGE_CMD_CLEAR extends "
1111 "beyond end of command buffer\n");
1112 DMA_FLUSH();
Eric Anholt20caafa2007-08-25 19:22:43 +10001113 ret = -EINVAL;
Dave Airlie3528af12006-01-02 16:11:44 +11001114 goto done;
Dave Airlie282a1672005-08-07 15:43:54 +10001115 }
1116 ret = savage_dispatch_clear(dev_priv, &cmd_header,
Eric Anholtc153f452007-09-03 12:06:45 +10001117 cmdbuf->cmd_addr,
1118 cmdbuf->nbox,
1119 cmdbuf->box_addr);
1120 cmdbuf->cmd_addr++;
Dave Airlie282a1672005-08-07 15:43:54 +10001121 i++;
1122 break;
1123 case SAVAGE_CMD_SWAP:
Eric Anholtc153f452007-09-03 12:06:45 +10001124 ret = savage_dispatch_swap(dev_priv, cmdbuf->nbox,
1125 cmdbuf->box_addr);
Dave Airlie282a1672005-08-07 15:43:54 +10001126 break;
1127 default:
Eric Anholtc153f452007-09-03 12:06:45 +10001128 DRM_ERROR("invalid command 0x%x\n",
1129 cmd_header.cmd.cmd);
Dave Airlie282a1672005-08-07 15:43:54 +10001130 DMA_FLUSH();
Eric Anholt20caafa2007-08-25 19:22:43 +10001131 ret = -EINVAL;
Dave Airlie3528af12006-01-02 16:11:44 +11001132 goto done;
Dave Airlie282a1672005-08-07 15:43:54 +10001133 }
1134
1135 if (ret != 0) {
1136 DMA_FLUSH();
Dave Airlie3528af12006-01-02 16:11:44 +11001137 goto done;
Dave Airlie282a1672005-08-07 15:43:54 +10001138 }
1139 }
1140
1141 if (first_draw_cmd) {
Dave Airlie3528af12006-01-02 16:11:44 +11001142 ret = savage_dispatch_draw (
Eric Anholtc153f452007-09-03 12:06:45 +10001143 dev_priv, first_draw_cmd, cmdbuf->cmd_addr, dmabuf,
1144 cmdbuf->vb_addr, cmdbuf->vb_size, cmdbuf->vb_stride,
1145 cmdbuf->nbox, cmdbuf->box_addr);
Dave Airlie282a1672005-08-07 15:43:54 +10001146 if (ret != 0) {
1147 DMA_FLUSH();
Dave Airlie3528af12006-01-02 16:11:44 +11001148 goto done;
Dave Airlie282a1672005-08-07 15:43:54 +10001149 }
1150 }
1151
1152 DMA_FLUSH();
1153
Eric Anholtc153f452007-09-03 12:06:45 +10001154 if (dmabuf && cmdbuf->discard) {
Dave Airlie282a1672005-08-07 15:43:54 +10001155 drm_savage_buf_priv_t *buf_priv = dmabuf->dev_private;
1156 uint16_t event;
1157 event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
1158 SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
1159 savage_freelist_put(dev, dmabuf);
1160 }
1161
Dave Airlie3528af12006-01-02 16:11:44 +11001162done:
1163 /* If we didn't need to allocate them, these'll be NULL */
Eric Anholt9a298b22009-03-24 12:23:04 -07001164 kfree(kcmd_addr);
1165 kfree(kvb_addr);
1166 kfree(kbox_addr);
Dave Airlie3528af12006-01-02 16:11:44 +11001167
1168 return ret;
Dave Airlie282a1672005-08-07 15:43:54 +10001169}