Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / intel / 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 "intel_context.h"
29 #include "intel_batchbuffer.h"
30 #include "intel_buffer_objects.h"
31 #include "intel_decode.h"
32 #include "intel_reg.h"
33 #include "intel_bufmgr.h"
34 #include "intel_buffers.h"
35
36 struct cached_batch_item {
37    struct cached_batch_item *next;
38    uint16_t header;
39    uint16_t size;
40 };
41
42 static void clear_cache( struct intel_context *intel )
43 {
44    struct cached_batch_item *item = intel->batch.cached_items;
45
46    while (item) {
47       struct cached_batch_item *next = item->next;
48       free(item);
49       item = next;
50    }
51
52    intel->batch.cached_items = NULL;
53 }
54
55 void
56 intel_batchbuffer_init(struct intel_context *intel)
57 {
58    intel_batchbuffer_reset(intel);
59
60    if (intel->gen == 6) {
61       /* We can't just use brw_state_batch to get a chunk of space for
62        * the gen6 workaround because it involves actually writing to
63        * the buffer, and the kernel doesn't let us write to the batch.
64        */
65       intel->batch.workaround_bo = drm_intel_bo_alloc(intel->bufmgr,
66                                                       "gen6 workaround",
67                                                       4096, 4096);
68    }
69 }
70
71 void
72 intel_batchbuffer_reset(struct intel_context *intel)
73 {
74    if (intel->batch.last_bo != NULL) {
75       drm_intel_bo_unreference(intel->batch.last_bo);
76       intel->batch.last_bo = NULL;
77    }
78    intel->batch.last_bo = intel->batch.bo;
79
80    clear_cache(intel);
81
82    intel->batch.bo = drm_intel_bo_alloc(intel->bufmgr, "batchbuffer",
83                                         intel->maxBatchSize, 4096);
84
85    intel->batch.reserved_space = BATCH_RESERVED;
86    intel->batch.state_batch_offset = intel->batch.bo->size;
87    intel->batch.used = 0;
88 }
89
90 void
91 intel_batchbuffer_free(struct intel_context *intel)
92 {
93    drm_intel_bo_unreference(intel->batch.last_bo);
94    drm_intel_bo_unreference(intel->batch.bo);
95    drm_intel_bo_unreference(intel->batch.workaround_bo);
96    clear_cache(intel);
97 }
98
99
100 /* TODO: Push this whole function into bufmgr.
101  */
102 static void
103 do_flush_locked(struct intel_context *intel)
104 {
105    struct intel_batchbuffer *batch = &intel->batch;
106    int ret = 0;
107
108    if (!intel->intelScreen->no_hw) {
109       int ring;
110
111       if (intel->gen < 6 || !batch->is_blit) {
112          ring = I915_EXEC_RENDER;
113       } else {
114          ring = I915_EXEC_BLT;
115       }
116
117       ret = drm_intel_bo_subdata(batch->bo, 0, 4*batch->used, batch->map);
118       if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
119          ret = drm_intel_bo_subdata(batch->bo,
120                                     batch->state_batch_offset,
121                                     batch->bo->size - batch->state_batch_offset,
122                                     (char *)batch->map + batch->state_batch_offset);
123       }
124
125       if (ret == 0)
126          ret = drm_intel_bo_mrb_exec(batch->bo, 4*batch->used, NULL, 0, 0, ring);
127    }
128
129    if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
130       intel_decode(batch->map, batch->used,
131                    batch->bo->offset,
132                    intel->intelScreen->deviceID, GL_TRUE);
133
134       if (intel->vtbl.debug_batch != NULL)
135          intel->vtbl.debug_batch(intel);
136    }
137
138    if (ret != 0) {
139       exit(1);
140    }
141    intel->vtbl.new_batch(intel);
142 }
143
144 void
145 _intel_batchbuffer_flush(struct intel_context *intel,
146                          const char *file, int line)
147 {
148    if (intel->batch.used == 0)
149       return;
150
151    if (intel->first_post_swapbuffers_batch == NULL) {
152       intel->first_post_swapbuffers_batch = intel->batch.bo;
153       drm_intel_bo_reference(intel->first_post_swapbuffers_batch);
154    }
155
156    if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
157       fprintf(stderr, "%s:%d: Batchbuffer flush with %db used\n", file, line,
158               4*intel->batch.used);
159
160    intel->batch.reserved_space = 0;
161
162    if (intel->always_flush_cache) {
163       intel_batchbuffer_emit_mi_flush(intel);
164    }
165
166    /* Mark the end of the buffer. */
167    intel_batchbuffer_emit_dword(intel, MI_BATCH_BUFFER_END);
168    if (intel->batch.used & 1) {
169       /* Round batchbuffer usage to 2 DWORDs. */
170       intel_batchbuffer_emit_dword(intel, MI_NOOP);
171    }
172
173    if (intel->vtbl.finish_batch)
174       intel->vtbl.finish_batch(intel);
175
176    intel_upload_finish(intel);
177
178    /* Check that we didn't just wrap our batchbuffer at a bad time. */
179    assert(!intel->no_batch_wrap);
180
181    do_flush_locked(intel);
182
183    if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
184       fprintf(stderr, "waiting for idle\n");
185       drm_intel_bo_wait_rendering(intel->batch.bo);
186    }
187
188    /* Reset the buffer:
189     */
190    intel_batchbuffer_reset(intel);
191 }
192
193
194 /*  This is the only way buffers get added to the validate list.
195  */
196 GLboolean
197 intel_batchbuffer_emit_reloc(struct intel_context *intel,
198                              drm_intel_bo *buffer,
199                              uint32_t read_domains, uint32_t write_domain,
200                              uint32_t delta)
201 {
202    int ret;
203
204    ret = drm_intel_bo_emit_reloc(intel->batch.bo, 4*intel->batch.used,
205                                  buffer, delta,
206                                  read_domains, write_domain);
207    assert(ret == 0);
208    (void)ret;
209
210    /*
211     * Using the old buffer offset, write in what the right data would be, in case
212     * the buffer doesn't move and we can short-circuit the relocation processing
213     * in the kernel
214     */
215    intel_batchbuffer_emit_dword(intel, buffer->offset + delta);
216
217    return GL_TRUE;
218 }
219
220 GLboolean
221 intel_batchbuffer_emit_reloc_fenced(struct intel_context *intel,
222                                     drm_intel_bo *buffer,
223                                     uint32_t read_domains,
224                                     uint32_t write_domain,
225                                     uint32_t delta)
226 {
227    int ret;
228
229    ret = drm_intel_bo_emit_reloc_fence(intel->batch.bo, 4*intel->batch.used,
230                                        buffer, delta,
231                                        read_domains, write_domain);
232    assert(ret == 0);
233    (void)ret;
234
235    /*
236     * Using the old buffer offset, write in what the right data would
237     * be, in case the buffer doesn't move and we can short-circuit the
238     * relocation processing in the kernel
239     */
240    intel_batchbuffer_emit_dword(intel, buffer->offset + delta);
241
242    return GL_TRUE;
243 }
244
245 void
246 intel_batchbuffer_data(struct intel_context *intel,
247                        const void *data, GLuint bytes, bool is_blit)
248 {
249    assert((bytes & 3) == 0);
250    intel_batchbuffer_require_space(intel, bytes, is_blit);
251    __memcpy(intel->batch.map + intel->batch.used, data, bytes);
252    intel->batch.used += bytes >> 2;
253 }
254
255 void
256 intel_batchbuffer_cached_advance(struct intel_context *intel)
257 {
258    struct cached_batch_item **prev = &intel->batch.cached_items, *item;
259    uint32_t sz = (intel->batch.used - intel->batch.emit) * sizeof(uint32_t);
260    uint32_t *start = intel->batch.map + intel->batch.emit;
261    uint16_t op = *start >> 16;
262
263    while (*prev) {
264       uint32_t *old;
265
266       item = *prev;
267       old = intel->batch.map + item->header;
268       if (op == *old >> 16) {
269          if (item->size == sz && memcmp(old, start, sz) == 0) {
270             if (prev != &intel->batch.cached_items) {
271                *prev = item->next;
272                item->next = intel->batch.cached_items;
273                intel->batch.cached_items = item;
274             }
275             intel->batch.used = intel->batch.emit;
276             return;
277          }
278
279          goto emit;
280       }
281       prev = &item->next;
282    }
283
284    item = malloc(sizeof(struct cached_batch_item));
285    if (item == NULL)
286       return;
287
288    item->next = intel->batch.cached_items;
289    intel->batch.cached_items = item;
290
291 emit:
292    item->size = sz;
293    item->header = intel->batch.emit;
294 }
295
296 /**
297  * Restriction [DevSNB, DevIVB]:
298  *
299  * Prior to changing Depth/Stencil Buffer state (i.e. any combination of
300  * 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS, 3DSTATE_STENCIL_BUFFER,
301  * 3DSTATE_HIER_DEPTH_BUFFER) SW must first issue a pipelined depth stall
302  * (PIPE_CONTROL with Depth Stall bit set), followed by a pipelined depth
303  * cache flush (PIPE_CONTROL with Depth Flush Bit set), followed by
304  * another pipelined depth stall (PIPE_CONTROL with Depth Stall bit set),
305  * unless SW can otherwise guarantee that the pipeline from WM onwards is
306  * already flushed (e.g., via a preceding MI_FLUSH).
307  */
308 void
309 intel_emit_depth_stall_flushes(struct intel_context *intel)
310 {
311    assert(intel->gen >= 6 && intel->gen <= 7);
312
313    BEGIN_BATCH(4);
314    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
315    OUT_BATCH(PIPE_CONTROL_DEPTH_STALL);
316    OUT_BATCH(0); /* address */
317    OUT_BATCH(0); /* write data */
318    ADVANCE_BATCH()
319
320    BEGIN_BATCH(4);
321    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
322    OUT_BATCH(PIPE_CONTROL_DEPTH_CACHE_FLUSH);
323    OUT_BATCH(0); /* address */
324    OUT_BATCH(0); /* write data */
325    ADVANCE_BATCH();
326
327    BEGIN_BATCH(4);
328    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
329    OUT_BATCH(PIPE_CONTROL_DEPTH_STALL);
330    OUT_BATCH(0); /* address */
331    OUT_BATCH(0); /* write data */
332    ADVANCE_BATCH();
333 }
334
335 /**
336  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
337  * implementing two workarounds on gen6.  From section 1.4.7.1
338  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
339  *
340  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
341  * produced by non-pipelined state commands), software needs to first
342  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
343  * 0.
344  *
345  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
346  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
347  *
348  * And the workaround for these two requires this workaround first:
349  *
350  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
351  * BEFORE the pipe-control with a post-sync op and no write-cache
352  * flushes.
353  *
354  * And this last workaround is tricky because of the requirements on
355  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
356  * volume 2 part 1:
357  *
358  *     "1 of the following must also be set:
359  *      - Render Target Cache Flush Enable ([12] of DW1)
360  *      - Depth Cache Flush Enable ([0] of DW1)
361  *      - Stall at Pixel Scoreboard ([1] of DW1)
362  *      - Depth Stall ([13] of DW1)
363  *      - Post-Sync Operation ([13] of DW1)
364  *      - Notify Enable ([8] of DW1)"
365  *
366  * The cache flushes require the workaround flush that triggered this
367  * one, so we can't use it.  Depth stall would trigger the same.
368  * Post-sync nonzero is what triggered this second workaround, so we
369  * can't use that one either.  Notify enable is IRQs, which aren't
370  * really our business.  That leaves only stall at scoreboard.
371  */
372 void
373 intel_emit_post_sync_nonzero_flush(struct intel_context *intel)
374 {
375    if (!intel->batch.need_workaround_flush)
376       return;
377
378    BEGIN_BATCH(4);
379    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
380    OUT_BATCH(PIPE_CONTROL_CS_STALL |
381              PIPE_CONTROL_STALL_AT_SCOREBOARD);
382    OUT_BATCH(0); /* address */
383    OUT_BATCH(0); /* write data */
384    ADVANCE_BATCH();
385
386    BEGIN_BATCH(4);
387    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
388    OUT_BATCH(PIPE_CONTROL_WRITE_IMMEDIATE);
389    OUT_RELOC(intel->batch.workaround_bo,
390              I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION, 0);
391    OUT_BATCH(0); /* write data */
392    ADVANCE_BATCH();
393
394    intel->batch.need_workaround_flush = false;
395 }
396
397 /* Emit a pipelined flush to either flush render and texture cache for
398  * reading from a FBO-drawn texture, or flush so that frontbuffer
399  * render appears on the screen in DRI1.
400  *
401  * This is also used for the always_flush_cache driconf debug option.
402  */
403 void
404 intel_batchbuffer_emit_mi_flush(struct intel_context *intel)
405 {
406    if (intel->gen >= 6) {
407       if (intel->batch.is_blit) {
408          BEGIN_BATCH_BLT(4);
409          OUT_BATCH(MI_FLUSH_DW);
410          OUT_BATCH(0);
411          OUT_BATCH(0);
412          OUT_BATCH(0);
413          ADVANCE_BATCH();
414       } else {
415          if (intel->gen == 6) {
416             /* Hardware workaround: SNB B-Spec says:
417              *
418              * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache
419              * Flush Enable =1, a PIPE_CONTROL with any non-zero
420              * post-sync-op is required.
421              */
422             intel_emit_post_sync_nonzero_flush(intel);
423          }
424
425          BEGIN_BATCH(4);
426          OUT_BATCH(_3DSTATE_PIPE_CONTROL);
427          OUT_BATCH(PIPE_CONTROL_INSTRUCTION_FLUSH |
428                    PIPE_CONTROL_WRITE_FLUSH |
429                    PIPE_CONTROL_DEPTH_CACHE_FLUSH |
430                    PIPE_CONTROL_TC_FLUSH |
431                    PIPE_CONTROL_NO_WRITE);
432          OUT_BATCH(0); /* write address */
433          OUT_BATCH(0); /* write data */
434          ADVANCE_BATCH();
435       }
436    } else if (intel->gen >= 4) {
437       BEGIN_BATCH(4);
438       OUT_BATCH(_3DSTATE_PIPE_CONTROL |
439                 PIPE_CONTROL_WRITE_FLUSH |
440                 PIPE_CONTROL_NO_WRITE);
441       OUT_BATCH(0); /* write address */
442       OUT_BATCH(0); /* write data */
443       OUT_BATCH(0); /* write data */
444       ADVANCE_BATCH();
445    } else {
446       BEGIN_BATCH(1);
447       OUT_BATCH(MI_FLUSH);
448       ADVANCE_BATCH();
449    }
450 }