1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 #include <va/va_backend.h>
34 #include "intel_batchbuffer.h"
37 intel_batchbuffer_reset(struct intel_batchbuffer *batch)
39 struct intel_driver_data *intel = batch->intel;
40 int batch_size = BATCH_SIZE;
42 assert(batch->flag == I915_EXEC_RENDER ||
43 batch->flag == I915_EXEC_BLT ||
44 batch->flag == I915_EXEC_BSD);
46 dri_bo_unreference(batch->buffer);
47 batch->buffer = dri_bo_alloc(intel->bufmgr,
51 assert(batch->buffer);
52 dri_bo_map(batch->buffer, 1);
53 assert(batch->buffer->virtual);
54 batch->map = batch->buffer->virtual;
55 batch->size = batch_size;
56 batch->ptr = batch->map;
61 intel_batchbuffer_space(struct intel_batchbuffer *batch)
63 return (batch->size - BATCH_RESERVED) - (batch->ptr - batch->map);
67 struct intel_batchbuffer *
68 intel_batchbuffer_new(struct intel_driver_data *intel, int flag)
70 struct intel_batchbuffer *batch = calloc(1, sizeof(*batch));
71 assert(flag == I915_EXEC_RENDER ||
72 flag == I915_EXEC_BSD ||
73 flag == I915_EXEC_BLT);
77 batch->run = drm_intel_bo_mrb_exec;
78 intel_batchbuffer_reset(batch);
83 void intel_batchbuffer_free(struct intel_batchbuffer *batch)
86 dri_bo_unmap(batch->buffer);
90 dri_bo_unreference(batch->buffer);
95 intel_batchbuffer_flush(struct intel_batchbuffer *batch)
97 unsigned int used = batch->ptr - batch->map;
103 if ((used & 4) == 0) {
104 *(unsigned int*)batch->ptr = 0;
108 *(unsigned int*)batch->ptr = MI_BATCH_BUFFER_END;
110 dri_bo_unmap(batch->buffer);
111 used = batch->ptr - batch->map;
112 batch->run(batch->buffer, used, 0, 0, 0, batch->flag);
113 intel_batchbuffer_reset(batch);
117 intel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, unsigned int x)
119 assert(intel_batchbuffer_space(batch) >= 4);
120 *(unsigned int *)batch->ptr = x;
125 intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch, dri_bo *bo,
126 uint32_t read_domains, uint32_t write_domains,
129 assert(batch->ptr - batch->map < batch->size);
130 dri_bo_emit_reloc(batch->buffer, read_domains, write_domains,
131 delta, batch->ptr - batch->map, bo);
132 intel_batchbuffer_emit_dword(batch, bo->offset + delta);
136 intel_batchbuffer_require_space(struct intel_batchbuffer *batch,
139 assert(size < batch->size - 8);
141 if (intel_batchbuffer_space(batch) < size) {
142 intel_batchbuffer_flush(batch);
147 intel_batchbuffer_data(struct intel_batchbuffer *batch,
151 assert((size & 3) == 0);
152 intel_batchbuffer_require_space(batch, size);
155 memcpy(batch->ptr, data, size);
160 intel_batchbuffer_emit_mi_flush(struct intel_batchbuffer *batch)
162 struct intel_driver_data *intel = batch->intel;
164 if (IS_GEN6(intel->device_id) ||
165 IS_GEN7(intel->device_id)) {
166 if (batch->flag == I915_EXEC_RENDER) {
167 BEGIN_BATCH(batch, 4);
168 OUT_BATCH(batch, CMD_PIPE_CONTROL | 0x2);
170 CMD_PIPE_CONTROL_WC_FLUSH |
171 CMD_PIPE_CONTROL_TC_FLUSH |
172 CMD_PIPE_CONTROL_NOWRITE);
175 ADVANCE_BATCH(batch);
177 if (batch->flag == I915_EXEC_BLT) {
178 BEGIN_BLT_BATCH(batch, 4);
179 OUT_BLT_BATCH(batch, MI_FLUSH_DW);
180 OUT_BLT_BATCH(batch, 0);
181 OUT_BLT_BATCH(batch, 0);
182 OUT_BLT_BATCH(batch, 0);
183 ADVANCE_BLT_BATCH(batch);
185 assert(batch->flag == I915_EXEC_BSD);
186 BEGIN_BCS_BATCH(batch, 4);
187 OUT_BCS_BATCH(batch, MI_FLUSH_DW | MI_FLUSH_DW_VIDEO_PIPELINE_CACHE_INVALIDATE);
188 OUT_BCS_BATCH(batch, 0);
189 OUT_BCS_BATCH(batch, 0);
190 OUT_BCS_BATCH(batch, 0);
191 ADVANCE_BCS_BATCH(batch);
195 if (batch->flag == I915_EXEC_RENDER) {
196 BEGIN_BATCH(batch, 1);
197 OUT_BATCH(batch, MI_FLUSH | MI_FLUSH_STATE_INSTRUCTION_CACHE_INVALIDATE);
198 ADVANCE_BATCH(batch);
200 assert(batch->flag == I915_EXEC_BSD);
201 BEGIN_BCS_BATCH(batch, 1);
202 OUT_BCS_BATCH(batch, MI_FLUSH | MI_FLUSH_STATE_INSTRUCTION_CACHE_INVALIDATE);
203 ADVANCE_BCS_BATCH(batch);
209 intel_batchbuffer_begin_batch(struct intel_batchbuffer *batch, int total)
211 batch->emit_total = total * 4;
212 batch->emit_start = batch->ptr;
216 intel_batchbuffer_advance_batch(struct intel_batchbuffer *batch)
218 assert(batch->emit_total == (batch->ptr - batch->emit_start));
222 intel_batchbuffer_check_batchbuffer_flag(struct intel_batchbuffer *batch, int flag)
224 if (flag != I915_EXEC_RENDER &&
225 flag != I915_EXEC_BLT &&
226 flag != I915_EXEC_BSD)
229 if (batch->flag == flag)
232 intel_batchbuffer_flush(batch);
237 intel_batchbuffer_check_free_space(struct intel_batchbuffer *batch, int size)
239 return intel_batchbuffer_space(batch) >= size;
243 intel_batchbuffer_start_atomic_helper(struct intel_batchbuffer *batch,
247 assert(!batch->atomic);
248 intel_batchbuffer_check_batchbuffer_flag(batch, flag);
249 intel_batchbuffer_require_space(batch, size);
254 intel_batchbuffer_start_atomic(struct intel_batchbuffer *batch, unsigned int size)
256 intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_RENDER, size);
260 intel_batchbuffer_start_atomic_blt(struct intel_batchbuffer *batch, unsigned int size)
262 intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_BLT, size);
266 intel_batchbuffer_start_atomic_bcs(struct intel_batchbuffer *batch, unsigned int size)
268 intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_BSD, size);
272 intel_batchbuffer_end_atomic(struct intel_batchbuffer *batch)
274 assert(batch->atomic);