2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #ifndef FREEDRENO_UTIL_H_
28 #define FREEDRENO_UTIL_H_
30 #include "drm/freedreno_drmif.h"
31 #include "drm/freedreno_ringbuffer.h"
33 #include "pipe/p_format.h"
34 #include "pipe/p_state.h"
35 #include "util/u_debug.h"
36 #include "util/u_math.h"
37 #include "util/u_half.h"
38 #include "util/u_dynarray.h"
39 #include "util/u_pack_color.h"
42 #include "adreno_common.xml.h"
43 #include "adreno_pm4.xml.h"
45 enum adreno_rb_depth_format
fd_pipe2depth(enum pipe_format format
);
46 enum pc_di_index_size
fd_pipe2index(enum pipe_format format
);
47 enum pipe_format
fd_gmem_restore_format(enum pipe_format format
);
48 enum adreno_rb_blend_factor
fd_blend_factor(unsigned factor
);
49 enum adreno_pa_su_sc_draw
fd_polygon_mode(unsigned mode
);
50 enum adreno_stencil_op
fd_stencil_op(unsigned op
);
52 #define A3XX_MAX_MIP_LEVELS 14
53 /* TBD if it is same on a2xx, but for now: */
54 #define MAX_MIP_LEVELS A3XX_MAX_MIP_LEVELS
56 #define A2XX_MAX_RENDER_TARGETS 1
57 #define A3XX_MAX_RENDER_TARGETS 4
58 #define A4XX_MAX_RENDER_TARGETS 8
59 #define A5XX_MAX_RENDER_TARGETS 8
60 #define A6XX_MAX_RENDER_TARGETS 8
62 #define MAX_RENDER_TARGETS A6XX_MAX_RENDER_TARGETS
64 #define FD_DBG_MSGS 0x0001
65 #define FD_DBG_DISASM 0x0002
66 #define FD_DBG_DCLEAR 0x0004
67 #define FD_DBG_DDRAW 0x0008
68 #define FD_DBG_NOSCIS 0x0010
69 #define FD_DBG_DIRECT 0x0020
70 #define FD_DBG_NOBYPASS 0x0040
71 #define FD_DBG_FRAGHALF 0x0080
72 #define FD_DBG_NOBIN 0x0100
73 #define FD_DBG_NOGMEM 0x0200
74 #define FD_DBG_GLSL120 0x0400
75 #define FD_DBG_SHADERDB 0x0800
76 #define FD_DBG_FLUSH 0x1000
77 #define FD_DBG_DEQP 0x2000
78 #define FD_DBG_INORDER 0x4000
79 #define FD_DBG_BSTAT 0x8000
80 #define FD_DBG_NOGROW 0x10000
81 #define FD_DBG_LRZ 0x20000
82 #define FD_DBG_NOINDR 0x40000
83 #define FD_DBG_NOBLIT 0x80000
84 #define FD_DBG_HIPRIO 0x100000
85 #define FD_DBG_TTILE 0x200000
86 #define FD_DBG_PERFC 0x400000
87 #define FD_DBG_NOUBWC 0x800000
88 #define FD_DBG_NOLRZ 0x1000000
89 extern int fd_mesa_debug
;
90 extern bool fd_binning_enabled
;
92 #define DBG(fmt, ...) \
93 do { if (fd_mesa_debug & FD_DBG_MSGS) \
94 debug_printf("%s:%d: "fmt "\n", \
95 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
97 /* for conditionally setting boolean flag(s): */
98 #define COND(bool, val) ((bool) ? (val) : 0)
100 #define CP_REG(reg) ((0x4 << 16) | ((unsigned int)((reg) - (0x2000))))
102 static inline uint32_t DRAW(enum pc_di_primtype prim_type
,
103 enum pc_di_src_sel source_select
, enum pc_di_index_size index_size
,
104 enum pc_di_vis_cull_mode vis_cull_mode
,
107 return (prim_type
<< 0) |
108 (source_select
<< 6) |
109 ((index_size
& 1) << 11) |
110 ((index_size
>> 1) << 13) |
111 (vis_cull_mode
<< 9) |
116 static inline uint32_t DRAW_A20X(enum pc_di_primtype prim_type
,
117 enum pc_di_face_cull_sel faceness_cull_select
,
118 enum pc_di_src_sel source_select
, enum pc_di_index_size index_size
,
119 bool pre_fetch_cull_enable
,
120 bool grp_cull_enable
,
123 return (prim_type
<< 0) |
124 (source_select
<< 6) |
125 (faceness_cull_select
<< 8) |
126 ((index_size
& 1) << 11) |
127 ((index_size
>> 1) << 13) |
128 (pre_fetch_cull_enable
<< 14) |
129 (grp_cull_enable
<< 15) |
133 /* for tracking cmdstream positions that need to be patched: */
138 #define fd_patch_num_elements(buf) ((buf)->size / sizeof(struct fd_cs_patch))
139 #define fd_patch_element(buf, i) util_dynarray_element(buf, struct fd_cs_patch, i)
141 static inline enum pipe_format
142 pipe_surface_format(struct pipe_surface
*psurf
)
145 return PIPE_FORMAT_NONE
;
146 return psurf
->format
;
150 fd_surface_half_precision(const struct pipe_surface
*psurf
)
152 enum pipe_format format
;
157 format
= psurf
->format
;
159 /* colors are provided in consts, which go through cov.f32f16, which will
162 if (util_format_is_pure_integer(format
))
165 /* avoid losing precision on 32-bit float formats */
166 if (util_format_is_float(format
) &&
167 util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 0) == 32)
173 static inline unsigned
174 fd_sampler_first_level(const struct pipe_sampler_view
*view
)
176 if (view
->target
== PIPE_BUFFER
)
178 return view
->u
.tex
.first_level
;
181 static inline unsigned
182 fd_sampler_last_level(const struct pipe_sampler_view
*view
)
184 if (view
->target
== PIPE_BUFFER
)
186 return view
->u
.tex
.last_level
;
190 fd_half_precision(struct pipe_framebuffer_state
*pfb
)
194 for (i
= 0; i
< pfb
->nr_cbufs
; i
++)
195 if (!fd_surface_half_precision(pfb
->cbufs
[i
]))
201 /* Note sure if this is same on all gens, but seems to be same on the later
204 static inline unsigned
205 fd_calc_guardband(unsigned x
)
210 return 511 - ((l
- 8) * 65);
215 static inline void emit_marker(struct fd_ringbuffer
*ring
, int scratch_idx
);
218 OUT_RING(struct fd_ringbuffer
*ring
, uint32_t data
)
221 DBG("ring[%p]: OUT_RING %04x: %08x", ring
,
222 (uint32_t)(ring
->cur
- ring
->start
), data
);
224 fd_ringbuffer_emit(ring
, data
);
227 /* like OUT_RING() but appends a cmdstream patch point to 'buf' */
229 OUT_RINGP(struct fd_ringbuffer
*ring
, uint32_t data
,
230 struct util_dynarray
*buf
)
233 DBG("ring[%p]: OUT_RINGP %04x: %08x", ring
,
234 (uint32_t)(ring
->cur
- ring
->start
), data
);
236 util_dynarray_append(buf
, struct fd_cs_patch
, ((struct fd_cs_patch
){
243 * NOTE: OUT_RELOC*() is 2 dwords (64b) on a5xx+
247 __out_reloc(struct fd_ringbuffer
*ring
, struct fd_bo
*bo
,
248 uint32_t offset
, uint64_t or, int32_t shift
, uint32_t flags
)
251 DBG("ring[%p]: OUT_RELOC %04x: %p+%u << %d", ring
,
252 (uint32_t)(ring
->cur
- ring
->start
), bo
, offset
, shift
);
254 debug_assert(offset
< fd_bo_size(bo
));
255 fd_ringbuffer_reloc(ring
, &(struct fd_reloc
){
266 OUT_RELOC(struct fd_ringbuffer
*ring
, struct fd_bo
*bo
,
267 uint32_t offset
, uint64_t or, int32_t shift
)
269 __out_reloc(ring
, bo
, offset
, or, shift
, FD_RELOC_READ
);
273 OUT_RELOCW(struct fd_ringbuffer
*ring
, struct fd_bo
*bo
,
274 uint32_t offset
, uint64_t or, int32_t shift
)
276 __out_reloc(ring
, bo
, offset
, or, shift
, FD_RELOC_READ
| FD_RELOC_WRITE
);
280 OUT_RELOCD(struct fd_ringbuffer
*ring
, struct fd_bo
*bo
,
281 uint32_t offset
, uint64_t or, int32_t shift
)
283 __out_reloc(ring
, bo
, offset
, or, shift
, FD_RELOC_READ
| FD_RELOC_DUMP
);
287 OUT_RB(struct fd_ringbuffer
*ring
, struct fd_ringbuffer
*target
)
289 fd_ringbuffer_emit_reloc_ring_full(ring
, target
, 0);
292 static inline void BEGIN_RING(struct fd_ringbuffer
*ring
, uint32_t ndwords
)
294 if (ring
->cur
+ ndwords
> ring
->end
)
295 fd_ringbuffer_grow(ring
, ndwords
);
299 OUT_PKT0(struct fd_ringbuffer
*ring
, uint16_t regindx
, uint16_t cnt
)
301 BEGIN_RING(ring
, cnt
+1);
302 OUT_RING(ring
, CP_TYPE0_PKT
| ((cnt
-1) << 16) | (regindx
& 0x7FFF));
306 OUT_PKT2(struct fd_ringbuffer
*ring
)
309 OUT_RING(ring
, CP_TYPE2_PKT
);
313 OUT_PKT3(struct fd_ringbuffer
*ring
, uint8_t opcode
, uint16_t cnt
)
315 BEGIN_RING(ring
, cnt
+1);
316 OUT_RING(ring
, CP_TYPE3_PKT
| ((cnt
-1) << 16) | ((opcode
& 0xFF) << 8));
320 * Starting with a5xx, pkt4/pkt7 are used instead of pkt0/pkt3
323 static inline unsigned
324 _odd_parity_bit(unsigned val
)
326 /* See: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
327 * note that we want odd parity so 0x6996 is inverted.
333 return (~0x6996 >> val
) & 1;
337 OUT_PKT4(struct fd_ringbuffer
*ring
, uint16_t regindx
, uint16_t cnt
)
339 BEGIN_RING(ring
, cnt
+1);
340 OUT_RING(ring
, CP_TYPE4_PKT
| cnt
|
341 (_odd_parity_bit(cnt
) << 7) |
342 ((regindx
& 0x3ffff) << 8) |
343 ((_odd_parity_bit(regindx
) << 27)));
347 OUT_PKT7(struct fd_ringbuffer
*ring
, uint8_t opcode
, uint16_t cnt
)
349 BEGIN_RING(ring
, cnt
+1);
350 OUT_RING(ring
, CP_TYPE7_PKT
| cnt
|
351 (_odd_parity_bit(cnt
) << 15) |
352 ((opcode
& 0x7f) << 16) |
353 ((_odd_parity_bit(opcode
) << 23)));
357 OUT_WFI(struct fd_ringbuffer
*ring
)
359 OUT_PKT3(ring
, CP_WAIT_FOR_IDLE
, 1);
360 OUT_RING(ring
, 0x00000000);
364 OUT_WFI5(struct fd_ringbuffer
*ring
)
366 OUT_PKT7(ring
, CP_WAIT_FOR_IDLE
, 0);
370 __OUT_IB(struct fd_ringbuffer
*ring
, bool prefetch
, struct fd_ringbuffer
*target
)
372 if (target
->cur
== target
->start
)
375 unsigned count
= fd_ringbuffer_cmd_count(target
);
377 /* for debug after a lock up, write a unique counter value
378 * to scratch6 for each IB, to make it easier to match up
379 * register dumps to cmdstream. The combination of IB and
380 * DRAW (scratch7) is enough to "triangulate" the particular
381 * draw that caused lockup.
383 emit_marker(ring
, 6);
385 for (unsigned i
= 0; i
< count
; i
++) {
387 OUT_PKT3(ring
, prefetch
? CP_INDIRECT_BUFFER_PFE
: CP_INDIRECT_BUFFER_PFD
, 2);
388 dwords
= fd_ringbuffer_emit_reloc_ring_full(ring
, target
, i
) / 4;
390 OUT_RING(ring
, dwords
);
394 emit_marker(ring
, 6);
398 __OUT_IB5(struct fd_ringbuffer
*ring
, struct fd_ringbuffer
*target
)
400 if (target
->cur
== target
->start
)
403 unsigned count
= fd_ringbuffer_cmd_count(target
);
405 for (unsigned i
= 0; i
< count
; i
++) {
407 OUT_PKT7(ring
, CP_INDIRECT_BUFFER
, 3);
408 dwords
= fd_ringbuffer_emit_reloc_ring_full(ring
, target
, i
) / 4;
410 OUT_RING(ring
, dwords
);
414 /* CP_SCRATCH_REG4 is used to hold base address for query results: */
415 // XXX annoyingly scratch regs move on a5xx.. and additionally different
416 // packet types.. so freedreno_query_hw is going to need a bit of
418 #define HW_QUERY_BASE_REG REG_AXXX_CP_SCRATCH_REG4
421 emit_marker(struct fd_ringbuffer
*ring
, int scratch_idx
)
423 extern unsigned marker_cnt
;
424 unsigned reg
= REG_AXXX_CP_SCRATCH_REG0
+ scratch_idx
;
425 assert(reg
!= HW_QUERY_BASE_REG
);
426 if (reg
== HW_QUERY_BASE_REG
)
428 OUT_PKT0(ring
, reg
, 1);
429 OUT_RING(ring
, ++marker_cnt
);
432 static inline uint32_t
433 pack_rgba(enum pipe_format format
, const float *rgba
)
436 util_pack_color(rgba
, format
, &uc
);
441 * swap - swap value of @a and @b
444 do { __typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
446 #define foreach_bit(b, mask) \
447 for (uint32_t _m = (mask); _m && ({(b) = u_bit_scan(&_m); 1;});)
450 #define BIT(bit) (1u << bit)
456 static inline enum a3xx_msaa_samples
457 fd_msaa_samples(unsigned samples
)
463 case 1: return MSAA_ONE
;
464 case 2: return MSAA_TWO
;
465 case 4: return MSAA_FOUR
;
466 case 8: return MSAA_EIGHT
;
474 static inline enum a4xx_state_block
475 fd4_stage2shadersb(gl_shader_stage type
)
478 case MESA_SHADER_VERTEX
:
479 return SB4_VS_SHADER
;
480 case MESA_SHADER_FRAGMENT
:
481 return SB4_FS_SHADER
;
482 case MESA_SHADER_COMPUTE
:
483 case MESA_SHADER_KERNEL
:
484 return SB4_CS_SHADER
;
486 unreachable("bad shader type");
491 static inline enum a4xx_index_size
492 fd4_size2indextype(unsigned index_size
)
494 switch (index_size
) {
495 case 1: return INDEX4_SIZE_8_BIT
;
496 case 2: return INDEX4_SIZE_16_BIT
;
497 case 4: return INDEX4_SIZE_32_BIT
;
499 DBG("unsupported index size: %d", index_size
);
501 return INDEX4_SIZE_32_BIT
;
504 #endif /* FREEDRENO_UTIL_H_ */