i965_drv_video: improved MV quality for VME
[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         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);
169             OUT_BATCH(batch, 
170                       CMD_PIPE_CONTROL_WC_FLUSH |
171                       CMD_PIPE_CONTROL_TC_FLUSH |
172                       CMD_PIPE_CONTROL_NOWRITE);
173             OUT_BATCH(batch, 0);
174             OUT_BATCH(batch, 0);
175             ADVANCE_BATCH(batch);
176         } else {
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);
184             } else {
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);
192             }
193         }
194     } else {
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);
199         } else {
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);
204         }
205     }
206 }
207
208 void
209 intel_batchbuffer_begin_batch(struct intel_batchbuffer *batch, int total)
210 {
211     batch->emit_total = total * 4;
212     batch->emit_start = batch->ptr;
213 }
214
215 void
216 intel_batchbuffer_advance_batch(struct intel_batchbuffer *batch)
217 {
218     assert(batch->emit_total == (batch->ptr - batch->emit_start));
219 }
220
221 void
222 intel_batchbuffer_check_batchbuffer_flag(struct intel_batchbuffer *batch, int flag)
223 {
224     if (flag != I915_EXEC_RENDER &&
225         flag != I915_EXEC_BLT &&
226         flag != I915_EXEC_BSD)
227         return;
228
229     if (batch->flag == flag)
230         return;
231
232     intel_batchbuffer_flush(batch);
233     batch->flag = flag;
234 }
235
236 int
237 intel_batchbuffer_check_free_space(struct intel_batchbuffer *batch, int size)
238 {
239     return intel_batchbuffer_space(batch) >= size;
240 }
241
242 static void
243 intel_batchbuffer_start_atomic_helper(struct intel_batchbuffer *batch,
244                                       int flag,
245                                       unsigned int size)
246 {
247     assert(!batch->atomic);
248     intel_batchbuffer_check_batchbuffer_flag(batch, flag);
249     intel_batchbuffer_require_space(batch, size);
250     batch->atomic = 1;
251 }
252
253 void
254 intel_batchbuffer_start_atomic(struct intel_batchbuffer *batch, unsigned int size)
255 {
256     intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_RENDER, size);
257 }
258
259 void
260 intel_batchbuffer_start_atomic_blt(struct intel_batchbuffer *batch, unsigned int size)
261 {
262     intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_BLT, size);
263 }
264
265 void
266 intel_batchbuffer_start_atomic_bcs(struct intel_batchbuffer *batch, unsigned int size)
267 {
268     intel_batchbuffer_start_atomic_helper(batch, I915_EXEC_BSD, size);
269 }
270
271 void
272 intel_batchbuffer_end_atomic(struct intel_batchbuffer *batch)
273 {
274     assert(batch->atomic);
275     batch->atomic = 0;
276 }