30d782aad4ea947323b4a34d18accfc18883eaa9
[platform/upstream/libva.git] / i965_drv_video / intel_batchbuffer.c
1 /**************************************************************************                                                                                  
2  *                                                                                                                                                           
3  * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.                                                                                                
4  * All Rights Reserved.                                                                                                                                      
5  *                                                                                                                                                           
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:                                                                                                                                 
13  *                                                                                                                                                           
14  * The above copyright notice and this permission notice (including the                                                                                      
15  * next paragraph) shall be included in all copies or substantial portions                                                                                   
16  * of the Software.                                                                                                                                          
17  *                                                                                                                                                           
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.                                                                                                    
25  *                                                                                                                                                           
26  **************************************************************************/      
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31
32 #include <va/va_backend.h>
33
34 #include "intel_batchbuffer.h"
35
36 static void 
37 intel_batchbuffer_reset(struct intel_batchbuffer *batch)
38 {
39     struct intel_driver_data *intel = batch->intel; 
40     int batch_size = BATCH_SIZE;
41
42     assert(batch->flag == I915_EXEC_RENDER ||
43            batch->flag == I915_EXEC_BLT ||
44            batch->flag == I915_EXEC_BSD);
45
46     dri_bo_unreference(batch->buffer);
47     batch->buffer = dri_bo_alloc(intel->bufmgr, 
48                                  "batch buffer",
49                                  batch_size,
50                                  0x1000);
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;
57     batch->atomic = 0;
58 }
59
60 static unsigned int
61 intel_batchbuffer_space(struct intel_batchbuffer *batch)
62 {
63     return (batch->size - BATCH_RESERVED) - (batch->ptr - batch->map);
64 }
65
66
67 struct intel_batchbuffer * 
68 intel_batchbuffer_new(struct intel_driver_data *intel, int flag)
69 {
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);
74
75     batch->intel = intel;
76     batch->flag = flag;
77     batch->run = drm_intel_bo_mrb_exec;
78     intel_batchbuffer_reset(batch);
79
80     return batch;
81 }
82
83 void intel_batchbuffer_free(struct intel_batchbuffer *batch)
84 {
85     if (batch->map) {
86         dri_bo_unmap(batch->buffer);
87         batch->map = NULL;
88     }
89
90     dri_bo_unreference(batch->buffer);
91     free(batch);
92 }
93
94 void 
95 intel_batchbuffer_flush(struct intel_batchbuffer *batch)
96 {
97     unsigned int used = batch->ptr - batch->map;
98
99     if (used == 0) {
100         return;
101     }
102
103     if ((used & 4) == 0) {
104         *(unsigned int*)batch->ptr = 0;
105         batch->ptr += 4;
106     }
107
108     *(unsigned int*)batch->ptr = MI_BATCH_BUFFER_END;
109     batch->ptr += 4;
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);
114 }
115
116 void 
117 intel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, unsigned int x)
118 {
119     assert(intel_batchbuffer_space(batch) >= 4);
120     *(unsigned int *)batch->ptr = x;
121     batch->ptr += 4;
122 }
123
124 void 
125 intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch, dri_bo *bo, 
126                                 uint32_t read_domains, uint32_t write_domains, 
127                                 uint32_t delta)
128 {
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);
133 }
134
135 void 
136 intel_batchbuffer_require_space(struct intel_batchbuffer *batch,
137                                    unsigned int size)
138 {
139     assert(size < batch->size - 8);
140
141     if (intel_batchbuffer_space(batch) < size) {
142         intel_batchbuffer_flush(batch);
143     }
144 }
145
146 void 
147 intel_batchbuffer_data(struct intel_batchbuffer *batch,
148                           void *data,
149                           unsigned int size)
150 {
151     assert((size & 3) == 0);
152     intel_batchbuffer_require_space(batch, size);
153
154     assert(batch->ptr);
155     memcpy(batch->ptr, data, size);
156     batch->ptr += size;
157 }
158
159 void
160 intel_batchbuffer_emit_mi_flush(struct intel_batchbuffer *batch)
161 {
162     struct intel_driver_data *intel = batch->intel; 
163
164     if (IS_GEN6(intel->device_id)) {
165         if (batch->flag == I915_EXEC_RENDER) {
166             BEGIN_BATCH(batch, 4);
167             OUT_BATCH(batch, CMD_PIPE_CONTROL | 0x2);
168             OUT_BATCH(batch, 
169                       CMD_PIPE_CONTROL_WC_FLUSH |
170                       CMD_PIPE_CONTROL_TC_FLUSH |
171                       CMD_PIPE_CONTROL_NOWRITE);
172             OUT_BATCH(batch, 0);
173             OUT_BATCH(batch, 0);
174             ADVANCE_BATCH(batch);
175         } else {
176             if (batch->flag == I915_EXEC_BLT) {
177                 BEGIN_BLT_BATCH(batch, 4);
178                 OUT_BLT_BATCH(batch, MI_FLUSH_DW);
179                 OUT_BLT_BATCH(batch, 0);
180                 OUT_BLT_BATCH(batch, 0);
181                 OUT_BLT_BATCH(batch, 0);
182                 ADVANCE_BLT_BATCH(batch);
183             } else {
184                 assert(batch->flag == I915_EXEC_BSD);
185                 BEGIN_BCS_BATCH(batch, 4);
186                 OUT_BCS_BATCH(batch, MI_FLUSH_DW | MI_FLUSH_DW_VIDEO_PIPELINE_CACHE_INVALIDATE);
187                 OUT_BCS_BATCH(batch, 0);
188                 OUT_BCS_BATCH(batch, 0);
189                 OUT_BCS_BATCH(batch, 0);
190                 ADVANCE_BCS_BATCH(batch);
191             }
192         }
193     } else {
194         if (batch->flag == I915_EXEC_RENDER) {
195             BEGIN_BATCH(batch, 1);
196             OUT_BATCH(batch, MI_FLUSH | MI_FLUSH_STATE_INSTRUCTION_CACHE_INVALIDATE);
197             ADVANCE_BATCH(batch);
198         } else {
199             assert(batch->flag == I915_EXEC_BSD);
200             BEGIN_BCS_BATCH(batch, 1);
201             OUT_BCS_BATCH(batch, MI_FLUSH | MI_FLUSH_STATE_INSTRUCTION_CACHE_INVALIDATE);
202             ADVANCE_BCS_BATCH(batch);
203         }
204     }
205 }
206
207 void
208 intel_batchbuffer_begin_batch(struct intel_batchbuffer *batch, int total)
209 {
210     batch->emit_total = total * 4;
211     batch->emit_start = batch->ptr;
212 }
213
214 void
215 intel_batchbuffer_advance_batch(struct intel_batchbuffer *batch)
216 {
217     assert(batch->emit_total == (batch->ptr - batch->emit_start));
218 }
219
220 void
221 intel_batchbuffer_check_batchbuffer_flag(struct intel_batchbuffer *batch, int flag)
222 {
223     if (flag != I915_EXEC_RENDER &&
224         flag != I915_EXEC_BLT &&
225         flag != I915_EXEC_BSD)
226         return;
227
228     if (batch->flag == flag)
229         return;
230
231     intel_batchbuffer_flush(batch);
232     batch->flag = flag;
233 }
234
235 int
236 intel_batchbuffer_check_free_space(struct intel_batchbuffer *batch, int size)
237 {
238     return intel_batchbuffer_space(batch) >= size;
239 }
240
241 static void
242 intel_batchbuffer_start_atomic_helper(struct intel_batchbuffer *batch,
243                                       int flag,
244                                       unsigned int size)
245 {
246     assert(!batch->atomic);
247     intel_batchbuffer_check_batchbuffer_flag(batch, flag);
248     intel_batchbuffer_require_space(batch, size);
249     batch->atomic = 1;
250 }
251
252 void
253 intel_batchbuffer_start_atomic(struct intel_batchbuffer *batch, unsigned int size)
254 {
255     intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_RENDER, size);
256 }
257
258 void
259 intel_batchbuffer_start_atomic_blt(struct intel_batchbuffer *batch, unsigned int size)
260 {
261     intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_BLT, size);
262 }
263
264 void
265 intel_batchbuffer_start_atomic_bcs(struct intel_batchbuffer *batch, unsigned int size)
266 {
267     intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_BSD, size);
268 }
269
270 void
271 intel_batchbuffer_end_atomic(struct intel_batchbuffer *batch)
272 {
273     assert(batch->atomic);
274     batch->atomic = 0;
275 }