1 /* savage_state.c -- State and drawing support for Savage
3 * Copyright 2004 Felix Kuehling
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
26 #include "dev/drm/drmP.h"
27 #include "dev/drm/savage_drm.h"
28 #include "dev/drm/savage_drv.h"
30 void savage_emit_clip_rect_s3d(drm_savage_private_t
*dev_priv
,
31 const struct drm_clip_rect
*pbox
)
33 uint32_t scstart
= dev_priv
->state
.s3d
.new_scstart
;
34 uint32_t scend
= dev_priv
->state
.s3d
.new_scend
;
35 scstart
= (scstart
& ~SAVAGE_SCISSOR_MASK_S3D
) |
36 ((uint32_t)pbox
->x1
& 0x000007ff) |
37 (((uint32_t)pbox
->y1
<< 16) & 0x07ff0000);
38 scend
= (scend
& ~SAVAGE_SCISSOR_MASK_S3D
) |
39 (((uint32_t)pbox
->x2
- 1) & 0x000007ff) |
40 ((((uint32_t)pbox
->y2
- 1) << 16) & 0x07ff0000);
41 if (scstart
!= dev_priv
->state
.s3d
.scstart
||
42 scend
!= dev_priv
->state
.s3d
.scend
) {
45 DMA_WRITE(BCI_CMD_WAIT
| BCI_CMD_WAIT_3D
);
46 DMA_SET_REGISTERS(SAVAGE_SCSTART_S3D
, 2);
49 dev_priv
->state
.s3d
.scstart
= scstart
;
50 dev_priv
->state
.s3d
.scend
= scend
;
51 dev_priv
->waiting
= 1;
56 void savage_emit_clip_rect_s4(drm_savage_private_t
*dev_priv
,
57 const struct drm_clip_rect
*pbox
)
59 uint32_t drawctrl0
= dev_priv
->state
.s4
.new_drawctrl0
;
60 uint32_t drawctrl1
= dev_priv
->state
.s4
.new_drawctrl1
;
61 drawctrl0
= (drawctrl0
& ~SAVAGE_SCISSOR_MASK_S4
) |
62 ((uint32_t)pbox
->x1
& 0x000007ff) |
63 (((uint32_t)pbox
->y1
<< 12) & 0x00fff000);
64 drawctrl1
= (drawctrl1
& ~SAVAGE_SCISSOR_MASK_S4
) |
65 (((uint32_t)pbox
->x2
- 1) & 0x000007ff) |
66 ((((uint32_t)pbox
->y2
- 1) << 12) & 0x00fff000);
67 if (drawctrl0
!= dev_priv
->state
.s4
.drawctrl0
||
68 drawctrl1
!= dev_priv
->state
.s4
.drawctrl1
) {
71 DMA_WRITE(BCI_CMD_WAIT
| BCI_CMD_WAIT_3D
);
72 DMA_SET_REGISTERS(SAVAGE_DRAWCTRL0_S4
, 2);
75 dev_priv
->state
.s4
.drawctrl0
= drawctrl0
;
76 dev_priv
->state
.s4
.drawctrl1
= drawctrl1
;
77 dev_priv
->waiting
= 1;
82 static int savage_verify_texaddr(drm_savage_private_t
*dev_priv
, int unit
,
85 if ((addr
& 6) != 2) { /* reserved bits */
86 DRM_ERROR("bad texAddr%d %08x (reserved bits)\n", unit
, addr
);
89 if (!(addr
& 1)) { /* local */
91 if (addr
< dev_priv
->texture_offset
||
92 addr
>= dev_priv
->texture_offset
+ dev_priv
->texture_size
) {
94 ("bad texAddr%d %08x (local addr out of range)\n",
99 if (!dev_priv
->agp_textures
) {
100 DRM_ERROR("bad texAddr%d %08x (AGP not available)\n",
105 if (addr
< dev_priv
->agp_textures
->offset
||
106 addr
>= (dev_priv
->agp_textures
->offset
+
107 dev_priv
->agp_textures
->size
)) {
109 ("bad texAddr%d %08x (AGP addr out of range)\n",
117 #define SAVE_STATE(reg,where) \
118 if(start <= reg && start + count > reg) \
119 dev_priv->state.where = regs[reg - start]
120 #define SAVE_STATE_MASK(reg,where,mask) do { \
121 if(start <= reg && start + count > reg) { \
123 tmp = regs[reg - start]; \
124 dev_priv->state.where = (tmp & (mask)) | \
125 (dev_priv->state.where & ~(mask)); \
128 static int savage_verify_state_s3d(drm_savage_private_t
*dev_priv
,
129 unsigned int start
, unsigned int count
,
130 const uint32_t *regs
)
132 if (start
< SAVAGE_TEXPALADDR_S3D
||
133 start
+ count
- 1 > SAVAGE_DESTTEXRWWATERMARK_S3D
) {
134 DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
135 start
, start
+ count
- 1);
139 SAVE_STATE_MASK(SAVAGE_SCSTART_S3D
, s3d
.new_scstart
,
140 ~SAVAGE_SCISSOR_MASK_S3D
);
141 SAVE_STATE_MASK(SAVAGE_SCEND_S3D
, s3d
.new_scend
,
142 ~SAVAGE_SCISSOR_MASK_S3D
);
144 /* if any texture regs were changed ... */
145 if (start
<= SAVAGE_TEXCTRL_S3D
&&
146 start
+ count
> SAVAGE_TEXPALADDR_S3D
) {
147 /* ... check texture state */
148 SAVE_STATE(SAVAGE_TEXCTRL_S3D
, s3d
.texctrl
);
149 SAVE_STATE(SAVAGE_TEXADDR_S3D
, s3d
.texaddr
);
150 if (dev_priv
->state
.s3d
.texctrl
& SAVAGE_TEXCTRL_TEXEN_MASK
)
151 return savage_verify_texaddr(dev_priv
, 0,
152 dev_priv
->state
.s3d
.texaddr
);
158 static int savage_verify_state_s4(drm_savage_private_t
*dev_priv
,
159 unsigned int start
, unsigned int count
,
160 const uint32_t *regs
)
164 if (start
< SAVAGE_DRAWLOCALCTRL_S4
||
165 start
+ count
- 1 > SAVAGE_TEXBLENDCOLOR_S4
) {
166 DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
167 start
, start
+ count
- 1);
171 SAVE_STATE_MASK(SAVAGE_DRAWCTRL0_S4
, s4
.new_drawctrl0
,
172 ~SAVAGE_SCISSOR_MASK_S4
);
173 SAVE_STATE_MASK(SAVAGE_DRAWCTRL1_S4
, s4
.new_drawctrl1
,
174 ~SAVAGE_SCISSOR_MASK_S4
);
176 /* if any texture regs were changed ... */
177 if (start
<= SAVAGE_TEXDESCR_S4
&&
178 start
+ count
> SAVAGE_TEXPALADDR_S4
) {
179 /* ... check texture state */
180 SAVE_STATE(SAVAGE_TEXDESCR_S4
, s4
.texdescr
);
181 SAVE_STATE(SAVAGE_TEXADDR0_S4
, s4
.texaddr0
);
182 SAVE_STATE(SAVAGE_TEXADDR1_S4
, s4
.texaddr1
);
183 if (dev_priv
->state
.s4
.texdescr
& SAVAGE_TEXDESCR_TEX0EN_MASK
)
184 ret
|= savage_verify_texaddr(dev_priv
, 0,
185 dev_priv
->state
.s4
.texaddr0
);
186 if (dev_priv
->state
.s4
.texdescr
& SAVAGE_TEXDESCR_TEX1EN_MASK
)
187 ret
|= savage_verify_texaddr(dev_priv
, 1,
188 dev_priv
->state
.s4
.texaddr1
);
194 #undef SAVE_STATE_MASK
196 static int savage_dispatch_state(drm_savage_private_t
*dev_priv
,
197 const drm_savage_cmd_header_t
*cmd_header
,
198 const uint32_t *regs
)
200 unsigned int count
= cmd_header
->state
.count
;
201 unsigned int start
= cmd_header
->state
.start
;
202 unsigned int count2
= 0;
203 unsigned int bci_size
;
210 if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
)) {
211 ret
= savage_verify_state_s3d(dev_priv
, start
, count
, regs
);
214 /* scissor regs are emitted in savage_dispatch_draw */
215 if (start
< SAVAGE_SCSTART_S3D
) {
216 if (start
+ count
> SAVAGE_SCEND_S3D
+ 1)
217 count2
= count
- (SAVAGE_SCEND_S3D
+ 1 - start
);
218 if (start
+ count
> SAVAGE_SCSTART_S3D
)
219 count
= SAVAGE_SCSTART_S3D
- start
;
220 } else if (start
<= SAVAGE_SCEND_S3D
) {
221 if (start
+ count
> SAVAGE_SCEND_S3D
+ 1) {
222 count
-= SAVAGE_SCEND_S3D
+ 1 - start
;
223 start
= SAVAGE_SCEND_S3D
+ 1;
228 ret
= savage_verify_state_s4(dev_priv
, start
, count
, regs
);
231 /* scissor regs are emitted in savage_dispatch_draw */
232 if (start
< SAVAGE_DRAWCTRL0_S4
) {
233 if (start
+ count
> SAVAGE_DRAWCTRL1_S4
+ 1)
235 (SAVAGE_DRAWCTRL1_S4
+ 1 - start
);
236 if (start
+ count
> SAVAGE_DRAWCTRL0_S4
)
237 count
= SAVAGE_DRAWCTRL0_S4
- start
;
238 } else if (start
<= SAVAGE_DRAWCTRL1_S4
) {
239 if (start
+ count
> SAVAGE_DRAWCTRL1_S4
+ 1) {
240 count
-= SAVAGE_DRAWCTRL1_S4
+ 1 - start
;
241 start
= SAVAGE_DRAWCTRL1_S4
+ 1;
247 bci_size
= count
+ (count
+ 254) / 255 + count2
+ (count2
+ 254) / 255;
249 if (cmd_header
->state
.global
) {
250 BEGIN_DMA(bci_size
+ 1);
251 DMA_WRITE(BCI_CMD_WAIT
| BCI_CMD_WAIT_3D
);
252 dev_priv
->waiting
= 1;
259 unsigned int n
= count
< 255 ? count
: 255;
260 DMA_SET_REGISTERS(start
, n
);
277 static int savage_dispatch_dma_prim(drm_savage_private_t
*dev_priv
,
278 const drm_savage_cmd_header_t
*cmd_header
,
279 const struct drm_buf
*dmabuf
)
281 unsigned char reorder
= 0;
282 unsigned int prim
= cmd_header
->prim
.prim
;
283 unsigned int skip
= cmd_header
->prim
.skip
;
284 unsigned int n
= cmd_header
->prim
.count
;
285 unsigned int start
= cmd_header
->prim
.start
;
290 DRM_ERROR("called without dma buffers!\n");
298 case SAVAGE_PRIM_TRILIST_201
:
300 prim
= SAVAGE_PRIM_TRILIST
;
301 case SAVAGE_PRIM_TRILIST
:
303 DRM_ERROR("wrong number of vertices %u in TRILIST\n",
308 case SAVAGE_PRIM_TRISTRIP
:
309 case SAVAGE_PRIM_TRIFAN
:
312 ("wrong number of vertices %u in TRIFAN/STRIP\n",
318 DRM_ERROR("invalid primitive type %u\n", prim
);
322 if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
)) {
324 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip
);
328 unsigned int size
= 10 - (skip
& 1) - (skip
>> 1 & 1) -
329 (skip
>> 2 & 1) - (skip
>> 3 & 1) - (skip
>> 4 & 1) -
330 (skip
>> 5 & 1) - (skip
>> 6 & 1) - (skip
>> 7 & 1);
331 if (skip
> SAVAGE_SKIP_ALL_S4
|| size
!= 8) {
332 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip
);
336 DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
341 if (start
+ n
> dmabuf
->total
/ 32) {
342 DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
343 start
, start
+ n
- 1, dmabuf
->total
/ 32);
347 /* Vertex DMA doesn't work with command DMA at the same time,
348 * so we use BCI_... to submit commands here. Flush buffered
349 * faked DMA first. */
352 if (dmabuf
->bus_address
!= dev_priv
->state
.common
.vbaddr
) {
354 BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR
, 1);
355 BCI_WRITE(dmabuf
->bus_address
| dev_priv
->dma_type
);
356 dev_priv
->state
.common
.vbaddr
= dmabuf
->bus_address
;
358 if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
) && dev_priv
->waiting
) {
359 /* Workaround for what looks like a hardware bug. If a
360 * WAIT_3D_IDLE was emitted some time before the
361 * indexed drawing command then the engine will lock
362 * up. There are two known workarounds:
363 * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
365 for (i
= 0; i
< 63; ++i
)
366 BCI_WRITE(BCI_CMD_WAIT
);
367 dev_priv
->waiting
= 0;
372 /* Can emit up to 255 indices (85 triangles) at once. */
373 unsigned int count
= n
> 255 ? 255 : n
;
375 /* Need to reorder indices for correct flat
376 * shading while preserving the clock sense
377 * for correct culling. Only on Savage3D. */
378 int reorder
[3] = { -1, -1, -1 };
379 reorder
[start
% 3] = 2;
381 BEGIN_BCI((count
+ 1 + 1) / 2);
382 BCI_DRAW_INDICES_S3D(count
, prim
, start
+ 2);
384 for (i
= start
+ 1; i
+ 1 < start
+ count
; i
+= 2)
385 BCI_WRITE((i
+ reorder
[i
% 3]) |
387 reorder
[(i
+ 1) % 3]) << 16));
388 if (i
< start
+ count
)
389 BCI_WRITE(i
+ reorder
[i
% 3]);
390 } else if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
)) {
391 BEGIN_BCI((count
+ 1 + 1) / 2);
392 BCI_DRAW_INDICES_S3D(count
, prim
, start
);
394 for (i
= start
+ 1; i
+ 1 < start
+ count
; i
+= 2)
395 BCI_WRITE(i
| ((i
+ 1) << 16));
396 if (i
< start
+ count
)
399 BEGIN_BCI((count
+ 2 + 1) / 2);
400 BCI_DRAW_INDICES_S4(count
, prim
, skip
);
402 for (i
= start
; i
+ 1 < start
+ count
; i
+= 2)
403 BCI_WRITE(i
| ((i
+ 1) << 16));
404 if (i
< start
+ count
)
411 prim
|= BCI_CMD_DRAW_CONT
;
417 static int savage_dispatch_vb_prim(drm_savage_private_t
*dev_priv
,
418 const drm_savage_cmd_header_t
*cmd_header
,
419 const uint32_t *vtxbuf
, unsigned int vb_size
,
420 unsigned int vb_stride
)
422 unsigned char reorder
= 0;
423 unsigned int prim
= cmd_header
->prim
.prim
;
424 unsigned int skip
= cmd_header
->prim
.skip
;
425 unsigned int n
= cmd_header
->prim
.count
;
426 unsigned int start
= cmd_header
->prim
.start
;
427 unsigned int vtx_size
;
435 case SAVAGE_PRIM_TRILIST_201
:
437 prim
= SAVAGE_PRIM_TRILIST
;
438 case SAVAGE_PRIM_TRILIST
:
440 DRM_ERROR("wrong number of vertices %u in TRILIST\n",
445 case SAVAGE_PRIM_TRISTRIP
:
446 case SAVAGE_PRIM_TRIFAN
:
449 ("wrong number of vertices %u in TRIFAN/STRIP\n",
455 DRM_ERROR("invalid primitive type %u\n", prim
);
459 if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
)) {
460 if (skip
> SAVAGE_SKIP_ALL_S3D
) {
461 DRM_ERROR("invalid skip flags 0x%04x\n", skip
);
464 vtx_size
= 8; /* full vertex */
466 if (skip
> SAVAGE_SKIP_ALL_S4
) {
467 DRM_ERROR("invalid skip flags 0x%04x\n", skip
);
470 vtx_size
= 10; /* full vertex */
473 vtx_size
-= (skip
& 1) + (skip
>> 1 & 1) +
474 (skip
>> 2 & 1) + (skip
>> 3 & 1) + (skip
>> 4 & 1) +
475 (skip
>> 5 & 1) + (skip
>> 6 & 1) + (skip
>> 7 & 1);
477 if (vtx_size
> vb_stride
) {
478 DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
479 vtx_size
, vb_stride
);
483 if (start
+ n
> vb_size
/ (vb_stride
* 4)) {
484 DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
485 start
, start
+ n
- 1, vb_size
/ (vb_stride
* 4));
491 /* Can emit up to 255 vertices (85 triangles) at once. */
492 unsigned int count
= n
> 255 ? 255 : n
;
494 /* Need to reorder vertices for correct flat
495 * shading while preserving the clock sense
496 * for correct culling. Only on Savage3D. */
497 int reorder
[3] = { -1, -1, -1 };
498 reorder
[start
% 3] = 2;
500 BEGIN_DMA(count
* vtx_size
+ 1);
501 DMA_DRAW_PRIMITIVE(count
, prim
, skip
);
503 for (i
= start
; i
< start
+ count
; ++i
) {
504 unsigned int j
= i
+ reorder
[i
% 3];
505 DMA_COPY(&vtxbuf
[vb_stride
* j
], vtx_size
);
510 BEGIN_DMA(count
* vtx_size
+ 1);
511 DMA_DRAW_PRIMITIVE(count
, prim
, skip
);
513 if (vb_stride
== vtx_size
) {
514 DMA_COPY(&vtxbuf
[vb_stride
* start
],
517 for (i
= start
; i
< start
+ count
; ++i
) {
518 DMA_COPY(&vtxbuf
[vb_stride
* i
],
529 prim
|= BCI_CMD_DRAW_CONT
;
535 static int savage_dispatch_dma_idx(drm_savage_private_t
*dev_priv
,
536 const drm_savage_cmd_header_t
*cmd_header
,
538 const struct drm_buf
*dmabuf
)
540 unsigned char reorder
= 0;
541 unsigned int prim
= cmd_header
->idx
.prim
;
542 unsigned int skip
= cmd_header
->idx
.skip
;
543 unsigned int n
= cmd_header
->idx
.count
;
548 DRM_ERROR("called without dma buffers!\n");
556 case SAVAGE_PRIM_TRILIST_201
:
558 prim
= SAVAGE_PRIM_TRILIST
;
559 case SAVAGE_PRIM_TRILIST
:
561 DRM_ERROR("wrong number of indices %u in TRILIST\n", n
);
565 case SAVAGE_PRIM_TRISTRIP
:
566 case SAVAGE_PRIM_TRIFAN
:
569 ("wrong number of indices %u in TRIFAN/STRIP\n", n
);
574 DRM_ERROR("invalid primitive type %u\n", prim
);
578 if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
)) {
580 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip
);
584 unsigned int size
= 10 - (skip
& 1) - (skip
>> 1 & 1) -
585 (skip
>> 2 & 1) - (skip
>> 3 & 1) - (skip
>> 4 & 1) -
586 (skip
>> 5 & 1) - (skip
>> 6 & 1) - (skip
>> 7 & 1);
587 if (skip
> SAVAGE_SKIP_ALL_S4
|| size
!= 8) {
588 DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip
);
592 DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
597 /* Vertex DMA doesn't work with command DMA at the same time,
598 * so we use BCI_... to submit commands here. Flush buffered
599 * faked DMA first. */
602 if (dmabuf
->bus_address
!= dev_priv
->state
.common
.vbaddr
) {
604 BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR
, 1);
605 BCI_WRITE(dmabuf
->bus_address
| dev_priv
->dma_type
);
606 dev_priv
->state
.common
.vbaddr
= dmabuf
->bus_address
;
608 if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
) && dev_priv
->waiting
) {
609 /* Workaround for what looks like a hardware bug. If a
610 * WAIT_3D_IDLE was emitted some time before the
611 * indexed drawing command then the engine will lock
612 * up. There are two known workarounds:
613 * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
615 for (i
= 0; i
< 63; ++i
)
616 BCI_WRITE(BCI_CMD_WAIT
);
617 dev_priv
->waiting
= 0;
622 /* Can emit up to 255 indices (85 triangles) at once. */
623 unsigned int count
= n
> 255 ? 255 : n
;
626 for (i
= 0; i
< count
; ++i
) {
627 if (idx
[i
] > dmabuf
->total
/ 32) {
628 DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
629 i
, idx
[i
], dmabuf
->total
/ 32);
635 /* Need to reorder indices for correct flat
636 * shading while preserving the clock sense
637 * for correct culling. Only on Savage3D. */
638 int reorder
[3] = { 2, -1, -1 };
640 BEGIN_BCI((count
+ 1 + 1) / 2);
641 BCI_DRAW_INDICES_S3D(count
, prim
, idx
[2]);
643 for (i
= 1; i
+ 1 < count
; i
+= 2)
644 BCI_WRITE(idx
[i
+ reorder
[i
% 3]] |
646 reorder
[(i
+ 1) % 3]] << 16));
648 BCI_WRITE(idx
[i
+ reorder
[i
% 3]]);
649 } else if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
)) {
650 BEGIN_BCI((count
+ 1 + 1) / 2);
651 BCI_DRAW_INDICES_S3D(count
, prim
, idx
[0]);
653 for (i
= 1; i
+ 1 < count
; i
+= 2)
654 BCI_WRITE(idx
[i
] | (idx
[i
+ 1] << 16));
658 BEGIN_BCI((count
+ 2 + 1) / 2);
659 BCI_DRAW_INDICES_S4(count
, prim
, skip
);
661 for (i
= 0; i
+ 1 < count
; i
+= 2)
662 BCI_WRITE(idx
[i
] | (idx
[i
+ 1] << 16));
670 prim
|= BCI_CMD_DRAW_CONT
;
676 static int savage_dispatch_vb_idx(drm_savage_private_t
*dev_priv
,
677 const drm_savage_cmd_header_t
*cmd_header
,
679 const uint32_t *vtxbuf
,
680 unsigned int vb_size
, unsigned int vb_stride
)
682 unsigned char reorder
= 0;
683 unsigned int prim
= cmd_header
->idx
.prim
;
684 unsigned int skip
= cmd_header
->idx
.skip
;
685 unsigned int n
= cmd_header
->idx
.count
;
686 unsigned int vtx_size
;
694 case SAVAGE_PRIM_TRILIST_201
:
696 prim
= SAVAGE_PRIM_TRILIST
;
697 case SAVAGE_PRIM_TRILIST
:
699 DRM_ERROR("wrong number of indices %u in TRILIST\n", n
);
703 case SAVAGE_PRIM_TRISTRIP
:
704 case SAVAGE_PRIM_TRIFAN
:
707 ("wrong number of indices %u in TRIFAN/STRIP\n", n
);
712 DRM_ERROR("invalid primitive type %u\n", prim
);
716 if (S3_SAVAGE3D_SERIES(dev_priv
->chipset
)) {
717 if (skip
> SAVAGE_SKIP_ALL_S3D
) {
718 DRM_ERROR("invalid skip flags 0x%04x\n", skip
);
721 vtx_size
= 8; /* full vertex */
723 if (skip
> SAVAGE_SKIP_ALL_S4
) {
724 DRM_ERROR("invalid skip flags 0x%04x\n", skip
);
727 vtx_size
= 10; /* full vertex */
730 vtx_size
-= (skip
& 1) + (skip
>> 1 & 1) +
731 (skip
>> 2 & 1) + (skip
>> 3 & 1) + (skip
>> 4 & 1) +
732 (skip
>> 5 & 1) + (skip
>> 6 & 1) + (skip
>> 7 & 1);
734 if (vtx_size
> vb_stride
) {
735 DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
736 vtx_size
, vb_stride
);
742 /* Can emit up to 255 vertices (85 triangles) at once. */
743 unsigned int count
= n
> 255 ? 255 : n
;
746 for (i
= 0; i
< count
; ++i
) {
747 if (idx
[i
] > vb_size
/ (vb_stride
* 4)) {
748 DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
749 i
, idx
[i
], vb_size
/ (vb_stride
* 4));
755 /* Need to reorder vertices for correct flat
756 * shading while preserving the clock sense
757 * for correct culling. Only on Savage3D. */
758 int reorder
[3] = { 2, -1, -1 };
760 BEGIN_DMA(count
* vtx_size
+ 1);
761 DMA_DRAW_PRIMITIVE(count
, prim
, skip
);
763 for (i
= 0; i
< count
; ++i
) {
764 unsigned int j
= idx
[i
+ reorder
[i
% 3]];
765 DMA_COPY(&vtxbuf
[vb_stride
* j
], vtx_size
);
770 BEGIN_DMA(count
* vtx_size
+ 1);
771 DMA_DRAW_PRIMITIVE(count
, prim
, skip
);
773 for (i
= 0; i
< count
; ++i
) {
774 unsigned int j
= idx
[i
];
775 DMA_COPY(&vtxbuf
[vb_stride
* j
], vtx_size
);
784 prim
|= BCI_CMD_DRAW_CONT
;
790 static int savage_dispatch_clear(drm_savage_private_t
*dev_priv
,
791 const drm_savage_cmd_header_t
*cmd_header
,
792 const drm_savage_cmd_header_t
*data
,
794 const struct drm_clip_rect
*boxes
)
796 unsigned int flags
= cmd_header
->clear0
.flags
;
797 unsigned int clear_cmd
;
798 unsigned int i
, nbufs
;
804 clear_cmd
= BCI_CMD_RECT
| BCI_CMD_RECT_XP
| BCI_CMD_RECT_YP
|
805 BCI_CMD_SEND_COLOR
| BCI_CMD_DEST_PBD_NEW
;
806 BCI_CMD_SET_ROP(clear_cmd
,0xCC);
808 nbufs
= ((flags
& SAVAGE_FRONT
) ? 1 : 0) +
809 ((flags
& SAVAGE_BACK
) ? 1 : 0) + ((flags
& SAVAGE_DEPTH
) ? 1 : 0);
813 if (data
->clear1
.mask
!= 0xffffffff) {
816 DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK
, 1);
817 DMA_WRITE(data
->clear1
.mask
);
820 for (i
= 0; i
< nbox
; ++i
) {
821 unsigned int x
, y
, w
, h
;
824 x
= boxes
[i
].x1
, y
= boxes
[i
].y1
;
825 w
= boxes
[i
].x2
- boxes
[i
].x1
;
826 h
= boxes
[i
].y2
- boxes
[i
].y1
;
827 BEGIN_DMA(nbufs
* 6);
828 for (buf
= SAVAGE_FRONT
; buf
<= SAVAGE_DEPTH
; buf
<<= 1) {
831 DMA_WRITE(clear_cmd
);
834 DMA_WRITE(dev_priv
->front_offset
);
835 DMA_WRITE(dev_priv
->front_bd
);
838 DMA_WRITE(dev_priv
->back_offset
);
839 DMA_WRITE(dev_priv
->back_bd
);
842 DMA_WRITE(dev_priv
->depth_offset
);
843 DMA_WRITE(dev_priv
->depth_bd
);
846 DMA_WRITE(data
->clear1
.value
);
847 DMA_WRITE(BCI_X_Y(x
, y
));
848 DMA_WRITE(BCI_W_H(w
, h
));
852 if (data
->clear1
.mask
!= 0xffffffff) {
855 DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK
, 1);
856 DMA_WRITE(0xffffffff);
863 static int savage_dispatch_swap(drm_savage_private_t
*dev_priv
,
864 unsigned int nbox
, const struct drm_clip_rect
*boxes
)
866 unsigned int swap_cmd
;
873 swap_cmd
= BCI_CMD_RECT
| BCI_CMD_RECT_XP
| BCI_CMD_RECT_YP
|
874 BCI_CMD_SRC_PBD_COLOR_NEW
| BCI_CMD_DEST_GBD
;
875 BCI_CMD_SET_ROP(swap_cmd
,0xCC);
877 for (i
= 0; i
< nbox
; ++i
) {
880 DMA_WRITE(dev_priv
->back_offset
);
881 DMA_WRITE(dev_priv
->back_bd
);
882 DMA_WRITE(BCI_X_Y(boxes
[i
].x1
, boxes
[i
].y1
));
883 DMA_WRITE(BCI_X_Y(boxes
[i
].x1
, boxes
[i
].y1
));
884 DMA_WRITE(BCI_W_H(boxes
[i
].x2
- boxes
[i
].x1
,
885 boxes
[i
].y2
- boxes
[i
].y1
));
892 static int savage_dispatch_draw(drm_savage_private_t
*dev_priv
,
893 const drm_savage_cmd_header_t
*start
,
894 const drm_savage_cmd_header_t
*end
,
895 const struct drm_buf
*dmabuf
,
896 const unsigned int *vtxbuf
,
897 unsigned int vb_size
, unsigned int vb_stride
,
899 const struct drm_clip_rect
*boxes
)
904 for (i
= 0; i
< nbox
; ++i
) {
905 const drm_savage_cmd_header_t
*cmdbuf
;
906 dev_priv
->emit_clip_rect(dev_priv
, &boxes
[i
]);
909 while (cmdbuf
< end
) {
910 drm_savage_cmd_header_t cmd_header
;
911 cmd_header
= *cmdbuf
;
913 switch (cmd_header
.cmd
.cmd
) {
914 case SAVAGE_CMD_DMA_PRIM
:
915 ret
= savage_dispatch_dma_prim(
916 dev_priv
, &cmd_header
, dmabuf
);
918 case SAVAGE_CMD_VB_PRIM
:
919 ret
= savage_dispatch_vb_prim(
920 dev_priv
, &cmd_header
,
921 vtxbuf
, vb_size
, vb_stride
);
923 case SAVAGE_CMD_DMA_IDX
:
924 j
= (cmd_header
.idx
.count
+ 3) / 4;
925 /* j was check in savage_bci_cmdbuf */
926 ret
= savage_dispatch_dma_idx(dev_priv
,
927 &cmd_header
, (const uint16_t *)cmdbuf
,
931 case SAVAGE_CMD_VB_IDX
:
932 j
= (cmd_header
.idx
.count
+ 3) / 4;
933 /* j was check in savage_bci_cmdbuf */
934 ret
= savage_dispatch_vb_idx(dev_priv
,
935 &cmd_header
, (const uint16_t *)cmdbuf
,
936 (const uint32_t *)vtxbuf
, vb_size
,
941 /* What's the best return code? EFAULT? */
942 DRM_ERROR("IMPLEMENTATION ERROR: "
943 "non-drawing-command %d\n",
956 int savage_bci_cmdbuf(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
958 drm_savage_private_t
*dev_priv
= dev
->dev_private
;
959 struct drm_device_dma
*dma
= dev
->dma
;
960 struct drm_buf
*dmabuf
;
961 drm_savage_cmdbuf_t
*cmdbuf
= data
;
962 drm_savage_cmd_header_t
*kcmd_addr
= NULL
;
963 drm_savage_cmd_header_t
*first_draw_cmd
;
964 unsigned int *kvb_addr
= NULL
;
965 struct drm_clip_rect
*kbox_addr
= NULL
;
971 LOCK_TEST_WITH_RETURN(dev
, file_priv
);
973 if (dma
&& dma
->buflist
) {
974 if (cmdbuf
->dma_idx
> dma
->buf_count
) {
976 ("vertex buffer index %u out of range (0-%u)\n",
977 cmdbuf
->dma_idx
, dma
->buf_count
- 1);
980 dmabuf
= dma
->buflist
[cmdbuf
->dma_idx
];
985 /* Copy the user buffers into kernel temporary areas. This hasn't been
986 * a performance loss compared to VERIFYAREA_READ/
987 * COPY_FROM_USER_UNCHECKED when done in other drivers, and is correct
988 * for locking on FreeBSD.
991 kcmd_addr
= drm_alloc(cmdbuf
->size
* 8, DRM_MEM_DRIVER
);
992 if (kcmd_addr
== NULL
)
995 if (DRM_COPY_FROM_USER(kcmd_addr
, cmdbuf
->cmd_addr
,
998 drm_free(kcmd_addr
, cmdbuf
->size
* 8, DRM_MEM_DRIVER
);
1001 cmdbuf
->cmd_addr
= kcmd_addr
;
1003 if (cmdbuf
->vb_size
) {
1004 kvb_addr
= drm_alloc(cmdbuf
->vb_size
, DRM_MEM_DRIVER
);
1005 if (kvb_addr
== NULL
) {
1010 if (DRM_COPY_FROM_USER(kvb_addr
, cmdbuf
->vb_addr
,
1015 cmdbuf
->vb_addr
= kvb_addr
;
1018 kbox_addr
= drm_alloc(cmdbuf
->nbox
*
1019 sizeof(struct drm_clip_rect
),
1021 if (kbox_addr
== NULL
) {
1026 if (DRM_COPY_FROM_USER(kbox_addr
, cmdbuf
->box_addr
,
1028 sizeof(struct drm_clip_rect
))) {
1032 cmdbuf
->box_addr
= kbox_addr
;
1035 /* Make sure writes to DMA buffers are finished before sending
1036 * DMA commands to the graphics hardware. */
1037 DRM_MEMORYBARRIER();
1039 /* Coming from user space. Don't know if the Xserver has
1040 * emitted wait commands. Assuming the worst. */
1041 dev_priv
->waiting
= 1;
1044 first_draw_cmd
= NULL
;
1045 while (i
< cmdbuf
->size
) {
1046 drm_savage_cmd_header_t cmd_header
;
1047 cmd_header
= *(drm_savage_cmd_header_t
*)cmdbuf
->cmd_addr
;
1051 /* Group drawing commands with same state to minimize
1052 * iterations over clip rects. */
1054 switch (cmd_header
.cmd
.cmd
) {
1055 case SAVAGE_CMD_DMA_IDX
:
1056 case SAVAGE_CMD_VB_IDX
:
1057 j
= (cmd_header
.idx
.count
+ 3) / 4;
1058 if (i
+ j
> cmdbuf
->size
) {
1059 DRM_ERROR("indexed drawing command extends "
1060 "beyond end of command buffer\n");
1065 case SAVAGE_CMD_DMA_PRIM
:
1066 case SAVAGE_CMD_VB_PRIM
:
1067 if (!first_draw_cmd
)
1068 first_draw_cmd
= cmdbuf
->cmd_addr
- 1;
1069 cmdbuf
->cmd_addr
+= j
;
1073 if (first_draw_cmd
) {
1074 ret
= savage_dispatch_draw(
1075 dev_priv
, first_draw_cmd
,
1076 cmdbuf
->cmd_addr
- 1,
1077 dmabuf
, cmdbuf
->vb_addr
,
1080 cmdbuf
->nbox
, cmdbuf
->box_addr
);
1083 first_draw_cmd
= NULL
;
1089 switch (cmd_header
.cmd
.cmd
) {
1090 case SAVAGE_CMD_STATE
:
1091 j
= (cmd_header
.state
.count
+ 1) / 2;
1092 if (i
+ j
> cmdbuf
->size
) {
1093 DRM_ERROR("command SAVAGE_CMD_STATE extends "
1094 "beyond end of command buffer\n");
1099 ret
= savage_dispatch_state(dev_priv
, &cmd_header
,
1100 (const uint32_t *)cmdbuf
->cmd_addr
);
1101 cmdbuf
->cmd_addr
+= j
;
1104 case SAVAGE_CMD_CLEAR
:
1105 if (i
+ 1 > cmdbuf
->size
) {
1106 DRM_ERROR("command SAVAGE_CMD_CLEAR extends "
1107 "beyond end of command buffer\n");
1112 ret
= savage_dispatch_clear(dev_priv
, &cmd_header
,
1119 case SAVAGE_CMD_SWAP
:
1120 ret
= savage_dispatch_swap(dev_priv
, cmdbuf
->nbox
,
1124 DRM_ERROR("invalid command 0x%x\n",
1125 cmd_header
.cmd
.cmd
);
1137 if (first_draw_cmd
) {
1138 ret
= savage_dispatch_draw(
1139 dev_priv
, first_draw_cmd
, cmdbuf
->cmd_addr
, dmabuf
,
1140 cmdbuf
->vb_addr
, cmdbuf
->vb_size
, cmdbuf
->vb_stride
,
1141 cmdbuf
->nbox
, cmdbuf
->box_addr
);
1150 if (dmabuf
&& cmdbuf
->discard
) {
1151 drm_savage_buf_priv_t
*buf_priv
= dmabuf
->dev_private
;
1153 event
= savage_bci_emit_event(dev_priv
, SAVAGE_WAIT_3D
);
1154 SET_AGE(&buf_priv
->age
, event
, dev_priv
->event_wrap
);
1155 savage_freelist_put(dev
, dmabuf
);
1159 /* If we didn't need to allocate them, these'll be NULL */
1160 drm_free(kcmd_addr
, cmdbuf
->size
* 8, DRM_MEM_DRIVER
);
1161 drm_free(kvb_addr
, cmdbuf
->vb_size
, DRM_MEM_DRIVER
);
1162 drm_free(kbox_addr
, cmdbuf
->nbox
* sizeof(struct drm_clip_rect
),