73d02b573a40711c8ee53c86b3d51640fffe223a
[mesa.git] / src / gallium / drivers / iris / iris_batch.h
1 /*
2 * Copyright © 2017 Intel Corporation
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 * 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:
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 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef IRIS_BATCH_DOT_H
25 #define IRIS_BATCH_DOT_H
26
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <string.h>
30
31 #include "util/u_dynarray.h"
32
33 #include "i915_drm.h"
34 #include "common/gen_decoder.h"
35
36 /* The kernel assumes batchbuffers are smaller than 256kB. */
37 #define MAX_BATCH_SIZE (256 * 1024)
38
39 /* Our target batch size - flush approximately at this point. */
40 #define BATCH_SZ (20 * 1024)
41
42 #define IRIS_BATCH_COUNT 2
43
44 struct iris_address {
45 struct iris_bo *bo;
46 uint64_t offset;
47 bool write;
48 };
49
50 struct iris_batch {
51 struct iris_screen *screen;
52 struct iris_vtable *vtbl;
53 struct pipe_debug_callback *dbg;
54
55 /** The name of this batch for debug info (e.g. "render") */
56 const char *name;
57
58 /** Current batchbuffer being queued up. */
59 struct iris_bo *bo;
60 void *map;
61 void *map_next;
62 /** Size of the primary batch if we've moved on to a secondary. */
63 unsigned primary_batch_size;
64
65 /** Last Surface State Base Address set in this hardware context. */
66 uint64_t last_surface_base_address;
67
68 uint32_t hw_ctx_id;
69
70 /** Which engine this batch targets - a I915_EXEC_RING_MASK value */
71 uint8_t engine;
72
73 /** The validation list */
74 struct drm_i915_gem_exec_object2 *validation_list;
75 struct iris_bo **exec_bos;
76 int exec_count;
77 int exec_array_size;
78
79 /** A list of drm_i915_exec_fences to have execbuf signal or wait on */
80 struct util_dynarray exec_fences;
81
82 /** The amount of aperture space (in bytes) used by all exec_bos */
83 int aperture_space;
84
85 /** List of other batches which we might need to flush to use a BO */
86 struct iris_batch *other_batches[IRIS_BATCH_COUNT - 1];
87
88 struct {
89 /**
90 * Set of struct brw_bo * that have been rendered to within this
91 * batchbuffer and would need flushing before being used from another
92 * cache domain that isn't coherent with it (i.e. the sampler).
93 */
94 struct hash_table *render;
95
96 /**
97 * Set of struct brw_bo * that have been used as a depth buffer within
98 * this batchbuffer and would need flushing before being used from
99 * another cache domain that isn't coherent with it (i.e. the sampler).
100 */
101 struct set *depth;
102 } cache;
103
104 /** Map from batch offset to iris_alloc_state data (with DEBUG_BATCH) */
105 // XXX: unused
106 struct hash_table *state_sizes;
107 struct gen_batch_decode_ctx decoder;
108
109 /** Have we emitted any draw calls to this batch? */
110 bool contains_draw;
111 };
112
113 void iris_init_batch(struct iris_batch *batch,
114 struct iris_screen *screen,
115 struct iris_vtable *vtbl,
116 struct pipe_debug_callback *dbg,
117 struct iris_batch **other_batches,
118 const char *name,
119 uint8_t ring);
120 void iris_chain_to_new_batch(struct iris_batch *batch);
121 void iris_batch_free(struct iris_batch *batch);
122 void iris_batch_maybe_flush(struct iris_batch *batch, unsigned estimate);
123
124 void _iris_batch_flush(struct iris_batch *batch, const char *file, int line);
125 #define iris_batch_flush(batch) _iris_batch_flush((batch), __FILE__, __LINE__)
126
127 bool iris_batch_references(struct iris_batch *batch, struct iris_bo *bo);
128
129 #define RELOC_WRITE EXEC_OBJECT_WRITE
130
131 void iris_use_pinned_bo(struct iris_batch *batch, struct iris_bo *bo,
132 bool writable);
133
134 static inline unsigned
135 iris_batch_bytes_used(struct iris_batch *batch)
136 {
137 return batch->map_next - batch->map;
138 }
139
140 /**
141 * Ensure the current command buffer has \param size bytes of space
142 * remaining. If not, this creates a secondary batch buffer and emits
143 * a jump from the primary batch to the start of the secondary.
144 *
145 * Most callers want iris_get_command_space() instead.
146 */
147 static inline void
148 iris_require_command_space(struct iris_batch *batch, unsigned size)
149 {
150 const unsigned required_bytes = iris_batch_bytes_used(batch) + size;
151
152 if (required_bytes >= BATCH_SZ) {
153 iris_chain_to_new_batch(batch);
154 }
155 }
156
157 /**
158 * Allocate space in the current command buffer, and return a pointer
159 * to the mapped area so the caller can write commands there.
160 *
161 * This should be called whenever emitting commands.
162 */
163 static inline void *
164 iris_get_command_space(struct iris_batch *batch, unsigned bytes)
165 {
166 iris_require_command_space(batch, bytes);
167 void *map = batch->map_next;
168 batch->map_next += bytes;
169 return map;
170 }
171
172 /**
173 * Helper to emit GPU commands - allocates space, copies them there.
174 */
175 static inline void
176 iris_batch_emit(struct iris_batch *batch, const void *data, unsigned size)
177 {
178 void *map = iris_get_command_space(batch, size);
179 memcpy(map, data, size);
180 }
181
182 #endif