Tizen 2.1 base
[sdk/emulator/qemu.git] / gl / mesa / 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                                                       "pipe_control 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    intel->batch.needs_sol_reset = false;
89 }
90
91 void
92 intel_batchbuffer_save_state(struct intel_context *intel)
93 {
94    intel->batch.saved.used = intel->batch.used;
95    intel->batch.saved.reloc_count =
96       drm_intel_gem_bo_get_reloc_count(intel->batch.bo);
97 }
98
99 void
100 intel_batchbuffer_reset_to_saved(struct intel_context *intel)
101 {
102    drm_intel_gem_bo_clear_relocs(intel->batch.bo, intel->batch.saved.reloc_count);
103
104    intel->batch.used = intel->batch.saved.used;
105
106    /* Cached batch state is dead, since we just cleared some unknown part of the
107     * batchbuffer.  Assume that the caller resets any other state necessary.
108     */
109    clear_cache(intel);
110 }
111
112 void
113 intel_batchbuffer_free(struct intel_context *intel)
114 {
115    drm_intel_bo_unreference(intel->batch.last_bo);
116    drm_intel_bo_unreference(intel->batch.bo);
117    drm_intel_bo_unreference(intel->batch.workaround_bo);
118    clear_cache(intel);
119 }
120
121
122 /* TODO: Push this whole function into bufmgr.
123  */
124 static int
125 do_flush_locked(struct intel_context *intel)
126 {
127    struct intel_batchbuffer *batch = &intel->batch;
128    int ret = 0;
129
130    ret = drm_intel_bo_subdata(batch->bo, 0, 4*batch->used, batch->map);
131    if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
132       ret = drm_intel_bo_subdata(batch->bo,
133                                  batch->state_batch_offset,
134                                  batch->bo->size - batch->state_batch_offset,
135                                  (char *)batch->map + batch->state_batch_offset);
136    }
137
138    if (!intel->intelScreen->no_hw) {
139       int flags;
140
141       if (intel->gen < 6 || !batch->is_blit) {
142          flags = I915_EXEC_RENDER;
143       } else {
144          flags = I915_EXEC_BLT;
145       }
146
147       if (batch->needs_sol_reset)
148          flags |= I915_EXEC_GEN7_SOL_RESET;
149
150       if (ret == 0)
151          ret = drm_intel_bo_mrb_exec(batch->bo, 4*batch->used, NULL, 0, 0,
152                                      flags);
153    }
154
155    if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
156       drm_intel_bo_map(batch->bo, false);
157       intel_decode(batch->bo->virtual, batch->used,
158                    batch->bo->offset,
159                    intel->intelScreen->deviceID, true);
160       drm_intel_bo_unmap(batch->bo);
161
162       if (intel->vtbl.debug_batch != NULL)
163          intel->vtbl.debug_batch(intel);
164    }
165
166    if (ret != 0) {
167       fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
168       exit(1);
169    }
170    intel->vtbl.new_batch(intel);
171
172    return ret;
173 }
174
175 int
176 _intel_batchbuffer_flush(struct intel_context *intel,
177                          const char *file, int line)
178 {
179    int ret;
180
181    if (intel->batch.used == 0)
182       return 0;
183
184    if (intel->first_post_swapbuffers_batch == NULL) {
185       intel->first_post_swapbuffers_batch = intel->batch.bo;
186       drm_intel_bo_reference(intel->first_post_swapbuffers_batch);
187    }
188
189    if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
190       fprintf(stderr, "%s:%d: Batchbuffer flush with %db used\n", file, line,
191               4*intel->batch.used);
192
193    intel->batch.reserved_space = 0;
194
195    /* Mark the end of the buffer. */
196    intel_batchbuffer_emit_dword(intel, MI_BATCH_BUFFER_END);
197    if (intel->batch.used & 1) {
198       /* Round batchbuffer usage to 2 DWORDs. */
199       intel_batchbuffer_emit_dword(intel, MI_NOOP);
200    }
201
202    if (intel->vtbl.finish_batch)
203       intel->vtbl.finish_batch(intel);
204
205    intel_upload_finish(intel);
206
207    /* Check that we didn't just wrap our batchbuffer at a bad time. */
208    assert(!intel->no_batch_wrap);
209
210    ret = do_flush_locked(intel);
211
212    if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
213       fprintf(stderr, "waiting for idle\n");
214       drm_intel_bo_wait_rendering(intel->batch.bo);
215    }
216
217    /* Reset the buffer:
218     */
219    intel_batchbuffer_reset(intel);
220
221    return ret;
222 }
223
224
225 /*  This is the only way buffers get added to the validate list.
226  */
227 bool
228 intel_batchbuffer_emit_reloc(struct intel_context *intel,
229                              drm_intel_bo *buffer,
230                              uint32_t read_domains, uint32_t write_domain,
231                              uint32_t delta)
232 {
233    int ret;
234
235    ret = drm_intel_bo_emit_reloc(intel->batch.bo, 4*intel->batch.used,
236                                  buffer, delta,
237                                  read_domains, write_domain);
238    assert(ret == 0);
239    (void)ret;
240
241    /*
242     * Using the old buffer offset, write in what the right data would be, in case
243     * the buffer doesn't move and we can short-circuit the relocation processing
244     * in the kernel
245     */
246    intel_batchbuffer_emit_dword(intel, buffer->offset + delta);
247
248    return true;
249 }
250
251 bool
252 intel_batchbuffer_emit_reloc_fenced(struct intel_context *intel,
253                                     drm_intel_bo *buffer,
254                                     uint32_t read_domains,
255                                     uint32_t write_domain,
256                                     uint32_t delta)
257 {
258    int ret;
259
260    ret = drm_intel_bo_emit_reloc_fence(intel->batch.bo, 4*intel->batch.used,
261                                        buffer, delta,
262                                        read_domains, write_domain);
263    assert(ret == 0);
264    (void)ret;
265
266    /*
267     * Using the old buffer offset, write in what the right data would
268     * be, in case the buffer doesn't move and we can short-circuit the
269     * relocation processing in the kernel
270     */
271    intel_batchbuffer_emit_dword(intel, buffer->offset + delta);
272
273    return true;
274 }
275
276 void
277 intel_batchbuffer_data(struct intel_context *intel,
278                        const void *data, GLuint bytes, bool is_blit)
279 {
280    assert((bytes & 3) == 0);
281    intel_batchbuffer_require_space(intel, bytes, is_blit);
282    __memcpy(intel->batch.map + intel->batch.used, data, bytes);
283    intel->batch.used += bytes >> 2;
284 }
285
286 void
287 intel_batchbuffer_cached_advance(struct intel_context *intel)
288 {
289    struct cached_batch_item **prev = &intel->batch.cached_items, *item;
290    uint32_t sz = (intel->batch.used - intel->batch.emit) * sizeof(uint32_t);
291    uint32_t *start = intel->batch.map + intel->batch.emit;
292    uint16_t op = *start >> 16;
293
294    while (*prev) {
295       uint32_t *old;
296
297       item = *prev;
298       old = intel->batch.map + item->header;
299       if (op == *old >> 16) {
300          if (item->size == sz && memcmp(old, start, sz) == 0) {
301             if (prev != &intel->batch.cached_items) {
302                *prev = item->next;
303                item->next = intel->batch.cached_items;
304                intel->batch.cached_items = item;
305             }
306             intel->batch.used = intel->batch.emit;
307             return;
308          }
309
310          goto emit;
311       }
312       prev = &item->next;
313    }
314
315    item = malloc(sizeof(struct cached_batch_item));
316    if (item == NULL)
317       return;
318
319    item->next = intel->batch.cached_items;
320    intel->batch.cached_items = item;
321
322 emit:
323    item->size = sz;
324    item->header = intel->batch.emit;
325 }
326
327 /**
328  * Restriction [DevSNB, DevIVB]:
329  *
330  * Prior to changing Depth/Stencil Buffer state (i.e. any combination of
331  * 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS, 3DSTATE_STENCIL_BUFFER,
332  * 3DSTATE_HIER_DEPTH_BUFFER) SW must first issue a pipelined depth stall
333  * (PIPE_CONTROL with Depth Stall bit set), followed by a pipelined depth
334  * cache flush (PIPE_CONTROL with Depth Flush Bit set), followed by
335  * another pipelined depth stall (PIPE_CONTROL with Depth Stall bit set),
336  * unless SW can otherwise guarantee that the pipeline from WM onwards is
337  * already flushed (e.g., via a preceding MI_FLUSH).
338  */
339 void
340 intel_emit_depth_stall_flushes(struct intel_context *intel)
341 {
342    assert(intel->gen >= 6 && intel->gen <= 7);
343
344    BEGIN_BATCH(4);
345    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
346    OUT_BATCH(PIPE_CONTROL_DEPTH_STALL);
347    OUT_BATCH(0); /* address */
348    OUT_BATCH(0); /* write data */
349    ADVANCE_BATCH()
350
351    BEGIN_BATCH(4);
352    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
353    OUT_BATCH(PIPE_CONTROL_DEPTH_CACHE_FLUSH);
354    OUT_BATCH(0); /* address */
355    OUT_BATCH(0); /* write data */
356    ADVANCE_BATCH();
357
358    BEGIN_BATCH(4);
359    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
360    OUT_BATCH(PIPE_CONTROL_DEPTH_STALL);
361    OUT_BATCH(0); /* address */
362    OUT_BATCH(0); /* write data */
363    ADVANCE_BATCH();
364 }
365
366 /**
367  * From the BSpec, volume 2a.03: VS Stage Input / State:
368  * "[DevIVB] A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
369  *  stall needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
370  *  3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
371  *  3DSTATE_SAMPLER_STATE_POINTER_VS command.  Only one PIPE_CONTROL needs
372  *  to be sent before any combination of VS associated 3DSTATE."
373  */
374 void
375 gen7_emit_vs_workaround_flush(struct intel_context *intel)
376 {
377    assert(intel->gen == 7);
378
379    BEGIN_BATCH(4);
380    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
381    OUT_BATCH(PIPE_CONTROL_DEPTH_STALL | PIPE_CONTROL_WRITE_IMMEDIATE);
382    OUT_RELOC(intel->batch.workaround_bo,
383              I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION, 0);
384    OUT_BATCH(0); /* write data */
385    ADVANCE_BATCH();
386 }
387
388 /**
389  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
390  * implementing two workarounds on gen6.  From section 1.4.7.1
391  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
392  *
393  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
394  * produced by non-pipelined state commands), software needs to first
395  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
396  * 0.
397  *
398  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
399  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
400  *
401  * And the workaround for these two requires this workaround first:
402  *
403  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
404  * BEFORE the pipe-control with a post-sync op and no write-cache
405  * flushes.
406  *
407  * And this last workaround is tricky because of the requirements on
408  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
409  * volume 2 part 1:
410  *
411  *     "1 of the following must also be set:
412  *      - Render Target Cache Flush Enable ([12] of DW1)
413  *      - Depth Cache Flush Enable ([0] of DW1)
414  *      - Stall at Pixel Scoreboard ([1] of DW1)
415  *      - Depth Stall ([13] of DW1)
416  *      - Post-Sync Operation ([13] of DW1)
417  *      - Notify Enable ([8] of DW1)"
418  *
419  * The cache flushes require the workaround flush that triggered this
420  * one, so we can't use it.  Depth stall would trigger the same.
421  * Post-sync nonzero is what triggered this second workaround, so we
422  * can't use that one either.  Notify enable is IRQs, which aren't
423  * really our business.  That leaves only stall at scoreboard.
424  */
425 void
426 intel_emit_post_sync_nonzero_flush(struct intel_context *intel)
427 {
428    if (!intel->batch.need_workaround_flush)
429       return;
430
431    BEGIN_BATCH(4);
432    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
433    OUT_BATCH(PIPE_CONTROL_CS_STALL |
434              PIPE_CONTROL_STALL_AT_SCOREBOARD);
435    OUT_BATCH(0); /* address */
436    OUT_BATCH(0); /* write data */
437    ADVANCE_BATCH();
438
439    BEGIN_BATCH(4);
440    OUT_BATCH(_3DSTATE_PIPE_CONTROL);
441    OUT_BATCH(PIPE_CONTROL_WRITE_IMMEDIATE);
442    OUT_RELOC(intel->batch.workaround_bo,
443              I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION, 0);
444    OUT_BATCH(0); /* write data */
445    ADVANCE_BATCH();
446
447    intel->batch.need_workaround_flush = false;
448 }
449
450 /* Emit a pipelined flush to either flush render and texture cache for
451  * reading from a FBO-drawn texture, or flush so that frontbuffer
452  * render appears on the screen in DRI1.
453  *
454  * This is also used for the always_flush_cache driconf debug option.
455  */
456 void
457 intel_batchbuffer_emit_mi_flush(struct intel_context *intel)
458 {
459    if (intel->gen >= 6) {
460       if (intel->batch.is_blit) {
461          BEGIN_BATCH_BLT(4);
462          OUT_BATCH(MI_FLUSH_DW);
463          OUT_BATCH(0);
464          OUT_BATCH(0);
465          OUT_BATCH(0);
466          ADVANCE_BATCH();
467       } else {
468          if (intel->gen == 6) {
469             /* Hardware workaround: SNB B-Spec says:
470              *
471              * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache
472              * Flush Enable =1, a PIPE_CONTROL with any non-zero
473              * post-sync-op is required.
474              */
475             intel_emit_post_sync_nonzero_flush(intel);
476          }
477
478          BEGIN_BATCH(4);
479          OUT_BATCH(_3DSTATE_PIPE_CONTROL);
480          OUT_BATCH(PIPE_CONTROL_INSTRUCTION_FLUSH |
481                    PIPE_CONTROL_WRITE_FLUSH |
482                    PIPE_CONTROL_DEPTH_CACHE_FLUSH |
483                    PIPE_CONTROL_VF_CACHE_INVALIDATE |
484                    PIPE_CONTROL_TC_FLUSH |
485                    PIPE_CONTROL_NO_WRITE |
486                    PIPE_CONTROL_CS_STALL);
487          OUT_BATCH(0); /* write address */
488          OUT_BATCH(0); /* write data */
489          ADVANCE_BATCH();
490       }
491    } else if (intel->gen >= 4) {
492       BEGIN_BATCH(4);
493       OUT_BATCH(_3DSTATE_PIPE_CONTROL |
494                 PIPE_CONTROL_WRITE_FLUSH |
495                 PIPE_CONTROL_NO_WRITE);
496       OUT_BATCH(0); /* write address */
497       OUT_BATCH(0); /* write data */
498       OUT_BATCH(0); /* write data */
499       ADVANCE_BATCH();
500    } else {
501       BEGIN_BATCH(1);
502       OUT_BATCH(MI_FLUSH);
503       ADVANCE_BATCH();
504    }
505 }