Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / r600 / r600.h
1 /*
2  * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3  *
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  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
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
13  * Software.
14  *
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Jerome Glisse
25  */
26 #ifndef R600_H
27 #define R600_H
28
29 #include <assert.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <util/u_double_list.h>
33 #include <util/u_inlines.h>
34 #include <pipe/p_compiler.h>
35
36 #define RADEON_CTX_MAX_PM4      (64 * 1024 / 4)
37
38 #define R600_ERR(fmt, args...) \
39         fprintf(stderr, "EE %s:%d %s - "fmt, __FILE__, __LINE__, __func__, ##args)
40
41 typedef uint64_t                u64;
42 typedef uint32_t                u32;
43 typedef uint16_t                u16;
44 typedef uint8_t                 u8;
45
46 struct radeon;
47 struct winsys_handle;
48
49 enum radeon_family {
50         CHIP_UNKNOWN,
51         CHIP_R600,
52         CHIP_RV610,
53         CHIP_RV630,
54         CHIP_RV670,
55         CHIP_RV620,
56         CHIP_RV635,
57         CHIP_RS780,
58         CHIP_RS880,
59         CHIP_RV770,
60         CHIP_RV730,
61         CHIP_RV710,
62         CHIP_RV740,
63         CHIP_CEDAR,
64         CHIP_REDWOOD,
65         CHIP_JUNIPER,
66         CHIP_CYPRESS,
67         CHIP_HEMLOCK,
68         CHIP_PALM,
69         CHIP_SUMO,
70         CHIP_SUMO2,
71         CHIP_BARTS,
72         CHIP_TURKS,
73         CHIP_CAICOS,
74         CHIP_CAYMAN,
75         CHIP_LAST,
76 };
77
78 enum chip_class {
79         R600,
80         R700,
81         EVERGREEN,
82         CAYMAN,
83 };
84
85 struct r600_tiling_info {
86         unsigned num_channels;
87         unsigned num_banks;
88         unsigned group_bytes;
89 };
90
91 enum radeon_family r600_get_family(struct radeon *rw);
92 enum chip_class r600_get_family_class(struct radeon *radeon);
93 struct r600_tiling_info *r600_get_tiling_info(struct radeon *radeon);
94 unsigned r600_get_clock_crystal_freq(struct radeon *radeon);
95 unsigned r600_get_minor_version(struct radeon *radeon);
96 unsigned r600_get_num_backends(struct radeon *radeon);
97 unsigned r600_get_num_tile_pipes(struct radeon *radeon);
98 unsigned r600_get_backend_map(struct radeon *radeon);
99
100 /* r600_bo.c */
101 struct r600_bo;
102 struct r600_bo *r600_bo(struct radeon *radeon,
103                         unsigned size, unsigned alignment,
104                         unsigned binding, unsigned usage);
105 struct r600_bo *r600_bo_handle(struct radeon *radeon,
106                                 unsigned handle, unsigned *array_mode);
107 void *r600_bo_map(struct radeon *radeon, struct r600_bo *bo, unsigned usage, void *ctx);
108 void r600_bo_unmap(struct radeon *radeon, struct r600_bo *bo);
109 boolean r600_bo_get_winsys_handle(struct radeon *radeon, struct r600_bo *pb_bo,
110                                 unsigned stride, struct winsys_handle *whandle);
111 static INLINE unsigned r600_bo_offset(struct r600_bo *bo)
112 {
113         return 0;
114 }
115 void r600_bo_destroy(struct radeon *radeon, struct r600_bo *bo);
116
117 /* this relies on the pipe_reference being the first member of r600_bo */
118 static INLINE void r600_bo_reference(struct radeon *radeon, struct r600_bo **dst, struct r600_bo *src)
119 {
120         struct r600_bo *old = *dst;
121
122         if (pipe_reference((struct pipe_reference *)(*dst), (struct pipe_reference *)src)) {
123                 r600_bo_destroy(radeon, old);
124         }
125         *dst = src;
126 }
127
128
129 /* R600/R700 STATES */
130 #define R600_GROUP_MAX                  16
131 #define R600_BLOCK_MAX_BO               32
132 #define R600_BLOCK_MAX_REG              128
133
134 /* each range covers 9 bits of dword space = 512 dwords = 2k bytes */
135 /* there is a block entry for each register so 512 blocks */
136 /* we have no registers to read/write below 0x8000 (0x2000 in dw space) */
137 /* we use some fake offsets at 0x40000 to do evergreen sampler borders so take 0x42000 as a max bound*/
138 #define RANGE_OFFSET_START 0x8000
139 #define HASH_SHIFT 9
140 #define NUM_RANGES (0x42000 - RANGE_OFFSET_START) / (4 << HASH_SHIFT) /* 128 << 9 = 64k */
141
142 #define CTX_RANGE_ID(offset) ((((offset - RANGE_OFFSET_START) >> 2) >> HASH_SHIFT) & 255)
143 #define CTX_BLOCK_ID(offset) (((offset - RANGE_OFFSET_START) >> 2) & ((1 << HASH_SHIFT) - 1))
144
145 struct r600_pipe_reg {
146         u32                             value;
147         u32                             mask;
148         struct r600_block               *block;
149         struct r600_bo                  *bo;
150         u32                             id;
151 };
152
153 struct r600_pipe_state {
154         unsigned                        id;
155         unsigned                        nregs;
156         struct r600_pipe_reg            regs[R600_BLOCK_MAX_REG];
157 };
158
159 struct r600_pipe_resource_state {
160         unsigned                        id;
161         u32                             val[8];
162         struct r600_bo *bo[2];
163 };
164
165 #define R600_BLOCK_STATUS_ENABLED       (1 << 0)
166 #define R600_BLOCK_STATUS_DIRTY         (1 << 1)
167 #define R600_BLOCK_STATUS_RESOURCE_DIRTY        (1 << 2)
168
169 #define R600_BLOCK_STATUS_RESOURCE_VERTEX       (1 << 3)
170
171 struct r600_block_reloc {
172         struct r600_bo          *bo;
173         unsigned                flush_flags;
174         unsigned                flush_mask;
175         unsigned                bo_pm4_index;
176 };
177
178 struct r600_block {
179         struct list_head        list;
180         struct list_head        enable_list;
181         unsigned                status;
182         unsigned                flags;
183         unsigned                start_offset;
184         unsigned                pm4_ndwords;
185         unsigned                pm4_flush_ndwords;
186         unsigned                nbo;
187         u16                     nreg;
188         u16                     nreg_dirty;
189         u32                     *reg;
190         u32                     pm4[R600_BLOCK_MAX_REG];
191         unsigned                pm4_bo_index[R600_BLOCK_MAX_REG];
192         struct r600_block_reloc reloc[R600_BLOCK_MAX_BO];
193 };
194
195 struct r600_range {
196         struct r600_block       **blocks;
197 };
198
199 /*
200  * relocation
201  */
202 #pragma pack(1)
203 struct r600_reloc {
204         uint32_t        handle;
205         uint32_t        read_domain;
206         uint32_t        write_domain;
207         uint32_t        flags;
208 };
209 #pragma pack()
210
211 /*
212  * query
213  */
214 struct r600_query {
215         u64                                     result;
216         /* The kind of query */
217         unsigned                                type;
218         /* Offset of the first result for current query */
219         unsigned                                results_start;
220         /* Offset of the next free result after current query data */
221         unsigned                                results_end;
222         /* Size of the result */
223         unsigned                                result_size;
224         /* Count of new queries started in one stream without flushing */
225         unsigned                                queries_emitted;
226         /* State flags */
227         unsigned                                state;
228         /* The buffer where query results are stored. It's used as a ring,
229          * data blocks for current query are stored sequentially from
230          * results_start to results_end, with wrapping on the buffer end */
231         struct r600_bo                  *buffer;
232         unsigned                                buffer_size;
233         /* linked list of queries */
234         struct list_head                        list;
235 };
236
237 #define R600_QUERY_STATE_STARTED        (1 << 0)
238 #define R600_QUERY_STATE_ENDED          (1 << 1)
239 #define R600_QUERY_STATE_SUSPENDED      (1 << 2)
240 #define R600_QUERY_STATE_FLUSHED        (1 << 3)
241
242 #define R600_CONTEXT_DRAW_PENDING       (1 << 0)
243 #define R600_CONTEXT_DST_CACHES_DIRTY   (1 << 1)
244 #define R600_CONTEXT_CHECK_EVENT_FLUSH  (1 << 2)
245
246 struct r600_context {
247         struct radeon           *radeon;
248         struct r600_range       *range;
249         unsigned                nblocks;
250         struct r600_block       **blocks;
251         struct list_head        dirty;
252         struct list_head        resource_dirty;
253         struct list_head        enable_list;
254         unsigned                pm4_ndwords;
255         unsigned                pm4_cdwords;
256         unsigned                pm4_dirty_cdwords;
257         unsigned                ctx_pm4_ndwords;
258         unsigned                init_dwords;
259         unsigned                nreloc;
260         unsigned                creloc;
261         struct r600_reloc       *reloc;
262         struct radeon_bo        **bo;
263         u32                     *pm4;
264         struct list_head        query_list;
265         unsigned                num_query_running;
266         unsigned                backend_mask;
267         struct list_head        fenced_bo;
268         unsigned                max_db; /* for OQ */
269         unsigned                num_dest_buffers;
270         unsigned                flags;
271         boolean                 predicate_drawing;
272         struct r600_range ps_resources;
273         struct r600_range vs_resources;
274         struct r600_range fs_resources;
275         int num_ps_resources, num_vs_resources, num_fs_resources;
276         boolean                 have_depth_texture, have_depth_fb;
277 };
278
279 struct r600_draw {
280         u32                     vgt_num_indices;
281         u32                     vgt_num_instances;
282         u32                     vgt_index_type;
283         u32                     vgt_draw_initiator;
284         u32                     indices_bo_offset;
285         struct r600_bo          *indices;
286 };
287
288 void r600_get_backend_mask(struct r600_context *ctx);
289 int r600_context_init(struct r600_context *ctx, struct radeon *radeon);
290 void r600_context_fini(struct r600_context *ctx);
291 void r600_context_pipe_state_set(struct r600_context *ctx, struct r600_pipe_state *state);
292 void r600_context_pipe_state_set_ps_resource(struct r600_context *ctx, struct r600_pipe_resource_state *state, unsigned rid);
293 void r600_context_pipe_state_set_vs_resource(struct r600_context *ctx, struct r600_pipe_resource_state *state, unsigned rid);
294 void r600_context_pipe_state_set_fs_resource(struct r600_context *ctx, struct r600_pipe_resource_state *state, unsigned rid);
295 void r600_context_pipe_state_set_ps_sampler(struct r600_context *ctx, struct r600_pipe_state *state, unsigned id);
296 void r600_context_pipe_state_set_vs_sampler(struct r600_context *ctx, struct r600_pipe_state *state, unsigned id);
297 void r600_context_flush(struct r600_context *ctx);
298 void r600_context_dump_bof(struct r600_context *ctx, const char *file);
299 void r600_context_draw(struct r600_context *ctx, const struct r600_draw *draw);
300
301 struct r600_query *r600_context_query_create(struct r600_context *ctx, unsigned query_type);
302 void r600_context_query_destroy(struct r600_context *ctx, struct r600_query *query);
303 boolean r600_context_query_result(struct r600_context *ctx,
304                                 struct r600_query *query,
305                                 boolean wait, void *vresult);
306 void r600_query_begin(struct r600_context *ctx, struct r600_query *query);
307 void r600_query_end(struct r600_context *ctx, struct r600_query *query);
308 void r600_context_queries_suspend(struct r600_context *ctx);
309 void r600_context_queries_resume(struct r600_context *ctx, boolean flushed);
310 void r600_query_predication(struct r600_context *ctx, struct r600_query *query, int operation,
311                             int flag_wait);
312 void r600_context_emit_fence(struct r600_context *ctx, struct r600_bo *fence,
313                              unsigned offset, unsigned value);
314 void r600_context_flush_all(struct r600_context *ctx, unsigned flush_flags);
315 void r600_context_flush_dest_caches(struct r600_context *ctx);
316
317 int evergreen_context_init(struct r600_context *ctx, struct radeon *radeon);
318 void evergreen_context_draw(struct r600_context *ctx, const struct r600_draw *draw);
319 void evergreen_context_flush_dest_caches(struct r600_context *ctx);
320 void evergreen_context_pipe_state_set_ps_resource(struct r600_context *ctx, struct r600_pipe_resource_state *state, unsigned rid);
321 void evergreen_context_pipe_state_set_vs_resource(struct r600_context *ctx, struct r600_pipe_resource_state *state, unsigned rid);
322 void evergreen_context_pipe_state_set_fs_resource(struct r600_context *ctx, struct r600_pipe_resource_state *state, unsigned rid);
323 void evergreen_context_pipe_state_set_ps_sampler(struct r600_context *ctx, struct r600_pipe_state *state, unsigned id);
324 void evergreen_context_pipe_state_set_vs_sampler(struct r600_context *ctx, struct r600_pipe_state *state, unsigned id);
325
326 struct radeon *radeon_decref(struct radeon *radeon);
327
328 void _r600_pipe_state_add_reg(struct r600_context *ctx,
329                               struct r600_pipe_state *state,
330                               u32 offset, u32 value, u32 mask,
331                               u32 range_id, u32 block_id,
332                               struct r600_bo *bo);
333
334 void r600_pipe_state_add_reg_noblock(struct r600_pipe_state *state,
335                                      u32 offset, u32 value, u32 mask,
336                                      struct r600_bo *bo);
337 #define r600_pipe_state_add_reg(state, offset, value, mask, bo) _r600_pipe_state_add_reg(&rctx->ctx, state, offset, value, mask, CTX_RANGE_ID(offset), CTX_BLOCK_ID(offset), bo)
338
339 static inline void r600_pipe_state_mod_reg(struct r600_pipe_state *state,
340                                            u32 value)
341 {
342         state->regs[state->nregs].value = value;
343         state->nregs++;
344 }
345
346 static inline void r600_pipe_state_mod_reg_bo(struct r600_pipe_state *state,
347                                            u32 value, struct r600_bo *bo)
348 {
349         state->regs[state->nregs].value = value;
350         state->regs[state->nregs].bo = bo;
351         state->nregs++;
352 }
353
354 #endif