Fix drivers build.
[profile/ivi/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
41     if (batch->buffer != NULL) {
42         dri_bo_unreference(batch->buffer);
43         batch->buffer = NULL;
44     }
45
46     batch->buffer = dri_bo_alloc(intel->bufmgr, "batch buffer", 
47                                  BATCH_SIZE, 0x1000);
48
49     assert(batch->buffer);
50     dri_bo_map(batch->buffer, 1);
51     batch->map = batch->buffer->virtual;
52     batch->size = BATCH_SIZE;
53     batch->ptr = batch->map;
54     batch->atomic = 0;
55 }
56
57 Bool 
58 intel_batchbuffer_init(struct intel_driver_data *intel)
59 {
60     intel->batch = calloc(1, sizeof(*(intel->batch)));
61
62     assert(intel->batch);
63     intel->batch->intel = intel;
64     intel_batchbuffer_reset(intel->batch);
65
66     return True;
67 }
68
69 Bool
70 intel_batchbuffer_terminate(struct intel_driver_data *intel)
71 {
72     if (intel->batch) {
73         if (intel->batch->map) {
74             dri_bo_unmap(intel->batch->buffer);
75             intel->batch->map = NULL;
76         }
77
78         dri_bo_unreference(intel->batch->buffer);
79         free(intel->batch);
80         intel->batch = NULL;
81     }
82
83     return True;
84 }
85
86 Bool 
87 intel_batchbuffer_flush(VADriverContextP ctx)
88 {
89     struct intel_driver_data *intel = intel_driver_data(ctx);
90     struct intel_batchbuffer *batch = intel->batch;
91     unsigned int used = batch->ptr - batch->map;
92     int is_locked = intel->locked;
93
94     if (used == 0) {
95         return True;
96     }
97
98     if ((used & 4) == 0) {
99         *(unsigned int*)batch->ptr = 0;
100         batch->ptr += 4;
101     }
102
103     *(unsigned int*)batch->ptr = MI_BATCH_BUFFER_END;
104     batch->ptr += 4;
105     dri_bo_unmap(batch->buffer);
106     used = batch->ptr - batch->map;
107
108     if (!is_locked)
109         intel_lock_hardware(ctx);
110
111     dri_bo_exec(batch->buffer, used, 0, 0, 0);
112
113     if (!is_locked)
114         intel_unlock_hardware(ctx);
115
116     intel_batchbuffer_reset(intel->batch);
117
118     return True;
119 }
120
121 static unsigned int
122 intel_batchbuffer_space(struct intel_batchbuffer *batch)
123 {
124     return (batch->size - BATCH_RESERVED) - (batch->ptr - batch->map);
125 }
126
127 void 
128 intel_batchbuffer_emit_dword(VADriverContextP ctx, unsigned int x)
129 {
130     struct intel_driver_data *intel = intel_driver_data(ctx);
131     struct intel_batchbuffer *batch = intel->batch;
132
133     assert(intel_batchbuffer_space(batch) >= 4);
134     *(unsigned int*)batch->ptr = x;
135     batch->ptr += 4;
136 }
137
138 void 
139 intel_batchbuffer_emit_reloc(VADriverContextP ctx, dri_bo *bo, 
140                              uint32_t read_domains, uint32_t write_domains, 
141                              uint32_t delta)
142 {
143     struct intel_driver_data *intel = intel_driver_data(ctx);
144     struct intel_batchbuffer *batch = intel->batch;
145
146     assert(batch->ptr - batch->map < batch->size);
147     dri_bo_emit_reloc(batch->buffer, read_domains, write_domains,
148                       delta, batch->ptr - batch->map, bo);
149     intel_batchbuffer_emit_dword(ctx, bo->offset + delta);
150 }
151
152 void 
153 intel_batchbuffer_require_space(VADriverContextP ctx, unsigned int size)
154 {
155     struct intel_driver_data *intel = intel_driver_data(ctx);
156     struct intel_batchbuffer *batch = intel->batch;
157
158     assert(size < batch->size - 8);
159
160     if (intel_batchbuffer_space(batch) < size) {
161         intel_batchbuffer_flush(ctx);
162     }
163 }
164
165 void 
166 intel_batchbuffer_data(VADriverContextP ctx, void *data, unsigned int size)
167 {
168     struct intel_driver_data *intel = intel_driver_data(ctx);
169     struct intel_batchbuffer *batch = intel->batch;
170
171     assert((size & 3) == 0);
172     intel_batchbuffer_require_space(ctx, size);
173
174     assert(batch->ptr);
175     memcpy(batch->ptr, data, size);
176     batch->ptr += size;
177 }
178
179 void
180 intel_batchbuffer_emit_mi_flush(VADriverContextP ctx)
181 {
182     intel_batchbuffer_require_space(ctx, 4);
183     intel_batchbuffer_emit_dword(ctx, MI_FLUSH | STATE_INSTRUCTION_CACHE_INVALIDATE);
184 }
185
186 void
187 intel_batchbuffer_start_atomic(VADriverContextP ctx, unsigned int size)
188 {
189     struct intel_driver_data *intel = intel_driver_data(ctx);
190     struct intel_batchbuffer *batch = intel->batch;
191
192     assert(!batch->atomic);
193     intel_batchbuffer_require_space(ctx, size);
194     batch->atomic = 1;
195 }
196
197 void
198 intel_batchbuffer_end_atomic(VADriverContextP ctx)
199 {
200     struct intel_driver_data *intel = intel_driver_data(ctx);
201     struct intel_batchbuffer *batch = intel->batch;
202
203     assert(batch->atomic);
204     batch->atomic = 0;
205 }