1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "intel_batchbuffer.h"
29 #include "intel_ioctl.h"
30 #include "intel_decode.h"
31 #include "intel_reg.h"
33 /* Relocations in kernel space:
34 * - pass dma buffer seperately
35 * - memory manager knows how to patch
36 * - pass list of dependent buffers
37 * - pass relocation list
40 * - get back an offset for buffer to fire
41 * - memory manager knows how to fire buffer
43 * Really want the buffer to be AGP and pinned.
47 /* Cliprect fence: The highest fence protecting a dma buffer
48 * containing explicit cliprect information. Like the old drawable
49 * lock but irq-driven. X server must wait for this fence to expire
50 * before changing cliprects [and then doing sw rendering?]. For
51 * other dma buffers, the scheduler will grab current cliprect info
52 * and mix into buffer. X server must hold the lock while changing
53 * cliprects??? Make per-drawable. Need cliprects in shared memory
54 * -- beats storing them with every cmd buffer in the queue.
56 * ==> X server must wait for this fence to expire before touching the
57 * framebuffer with new cliprects.
59 * ==> Cliprect-dependent buffers associated with a
60 * cliprect-timestamp. All of the buffers associated with a timestamp
61 * must go to hardware before any buffer with a newer timestamp.
63 * ==> Dma should be queued per-drawable for correct X/GL
64 * synchronization. Or can fences be used for this?
66 * Applies to: Blit operations, metaops, X server operations -- X
67 * server automatically waits on its own dma to complete before
68 * modifying cliprects ???
72 intel_batchbuffer_reset(struct intel_batchbuffer
*batch
)
74 struct intel_context
*intel
= batch
->intel
;
76 if (batch
->buf
!= NULL
) {
77 dri_bo_unreference(batch
->buf
);
81 if (!batch
->buffer
&& intel
->ttm
== GL_TRUE
)
82 batch
->buffer
= malloc (intel
->maxBatchSize
);
84 batch
->buf
= dri_bo_alloc(intel
->bufmgr
, "batchbuffer",
85 intel
->maxBatchSize
, 4096,
86 DRM_BO_FLAG_MEM_LOCAL
| DRM_BO_FLAG_CACHED
| DRM_BO_FLAG_CACHED_MAPPED
);
88 batch
->map
= batch
->buffer
;
90 dri_bo_map(batch
->buf
, GL_TRUE
);
91 batch
->map
= batch
->buf
->virtual;
93 batch
->size
= intel
->maxBatchSize
;
94 batch
->ptr
= batch
->map
;
95 batch
->dirty_state
= ~0;
96 batch
->cliprect_mode
= IGNORE_CLIPRECTS
;
98 /* account batchbuffer in aperture */
99 dri_bufmgr_check_aperture_space(batch
->buf
);
103 struct intel_batchbuffer
*
104 intel_batchbuffer_alloc(struct intel_context
*intel
)
106 struct intel_batchbuffer
*batch
= calloc(sizeof(*batch
), 1);
108 batch
->intel
= intel
;
109 intel_batchbuffer_reset(batch
);
115 intel_batchbuffer_free(struct intel_batchbuffer
*batch
)
118 free (batch
->buffer
);
121 dri_bo_unmap(batch
->buf
);
125 dri_bo_unreference(batch
->buf
);
132 /* TODO: Push this whole function into bufmgr.
135 do_flush_locked(struct intel_batchbuffer
*batch
,
136 GLuint used
, GLboolean allow_unlock
)
138 struct intel_context
*intel
= batch
->intel
;
142 dri_bo_subdata (batch
->buf
, 0, used
, batch
->buffer
);
144 dri_bo_unmap(batch
->buf
);
149 /* Throw away non-effective packets. Won't work once we have
150 * hardware contexts which would preserve statechanges beyond a
154 if (!(intel
->numClipRects
== 0 &&
155 batch
->cliprect_mode
== LOOP_CLIPRECTS
)) {
156 if (intel
->ttm
== GL_TRUE
) {
157 struct drm_i915_gem_execbuffer
*execbuf
;
159 execbuf
= dri_process_relocs(batch
->buf
);
160 ret
= intel_exec_ioctl(batch
->intel
,
162 batch
->cliprect_mode
!= LOOP_CLIPRECTS
,
166 dri_process_relocs(batch
->buf
);
167 ret
= intel_batch_ioctl(batch
->intel
,
170 batch
->cliprect_mode
!= LOOP_CLIPRECTS
,
175 dri_post_submit(batch
->buf
);
177 if (intel
->numClipRects
== 0 &&
178 batch
->cliprect_mode
== LOOP_CLIPRECTS
) {
180 /* If we are not doing any actual user-visible rendering,
181 * do a sched_yield to keep the app from pegging the cpu while
184 UNLOCK_HARDWARE(intel
);
186 LOCK_HARDWARE(intel
);
190 if (INTEL_DEBUG
& DEBUG_BATCH
) {
191 dri_bo_map(batch
->buf
, GL_FALSE
);
192 intel_decode(batch
->buf
->virtual, used
/ 4, batch
->buf
->offset
,
193 intel
->intelScreen
->deviceID
);
194 dri_bo_unmap(batch
->buf
);
196 if (intel
->vtbl
.debug_batch
!= NULL
)
197 intel
->vtbl
.debug_batch(intel
);
201 UNLOCK_HARDWARE(intel
);
204 intel
->vtbl
.new_batch(intel
);
208 _intel_batchbuffer_flush(struct intel_batchbuffer
*batch
, const char *file
,
211 struct intel_context
*intel
= batch
->intel
;
212 GLuint used
= batch
->ptr
- batch
->map
;
213 GLboolean was_locked
= intel
->locked
;
218 if (INTEL_DEBUG
& DEBUG_BATCH
)
219 fprintf(stderr
, "%s:%d: Batchbuffer flush with %db used\n", file
, line
,
222 /* Emit a flush if the bufmgr doesn't do it for us. */
224 *(GLuint
*) (batch
->ptr
) = intel
->vtbl
.flush_cmd();
226 used
= batch
->ptr
- batch
->map
;
229 /* Round batchbuffer usage to 2 DWORDs. */
231 if ((used
& 4) == 0) {
232 *(GLuint
*) (batch
->ptr
) = 0; /* noop */
234 used
= batch
->ptr
- batch
->map
;
237 /* Mark the end of the buffer. */
238 *(GLuint
*) (batch
->ptr
) = MI_BATCH_BUFFER_END
; /* noop */
240 used
= batch
->ptr
- batch
->map
;
242 /* Workaround for recursive batchbuffer flushing: If the window is
243 * moved, we can get into a case where we try to flush during a
244 * flush. What happens is that when we try to grab the lock for
245 * the first flush, we detect that the window moved which then
246 * causes another flush (from the intel_draw_buffer() call in
247 * intelUpdatePageFlipping()). To work around this we reset the
248 * batchbuffer tail pointer before trying to get the lock. This
249 * prevent the nested buffer flush, but a better fix would be to
250 * avoid that in the first place. */
251 batch
->ptr
= batch
->map
;
253 /* TODO: Just pass the relocation list and dma buffer up to the
257 LOCK_HARDWARE(intel
);
259 do_flush_locked(batch
, used
, GL_FALSE
);
262 UNLOCK_HARDWARE(intel
);
264 if (INTEL_DEBUG
& DEBUG_SYNC
) {
267 fprintf(stderr
, "waiting for idle\n");
268 LOCK_HARDWARE(intel
);
269 irq
= intelEmitIrqLocked(intel
);
270 UNLOCK_HARDWARE(intel
);
271 intelWaitIrq(intel
, irq
);
276 intel_batchbuffer_reset(batch
);
280 /* This is the only way buffers get added to the validate list.
283 intel_batchbuffer_emit_reloc(struct intel_batchbuffer
*batch
,
285 uint32_t read_domains
, uint32_t write_domain
,
290 if (batch
->ptr
- batch
->map
> batch
->buf
->size
)
291 _mesa_printf ("bad relocation ptr %p map %p offset %d size %d\n",
292 batch
->ptr
, batch
->map
, batch
->ptr
- batch
->map
, batch
->buf
->size
);
293 ret
= dri_emit_reloc(batch
->buf
, read_domains
, write_domain
,
294 delta
, batch
->ptr
- batch
->map
, buffer
);
297 * Using the old buffer offset, write in what the right data would be, in case
298 * the buffer doesn't move and we can short-circuit the relocation processing
301 intel_batchbuffer_emit_dword (batch
, buffer
->offset
+ delta
);
307 intel_batchbuffer_data(struct intel_batchbuffer
*batch
,
308 const void *data
, GLuint bytes
,
309 enum cliprect_mode cliprect_mode
)
311 assert((bytes
& 3) == 0);
312 intel_batchbuffer_require_space(batch
, bytes
, cliprect_mode
);
313 __memcpy(batch
->ptr
, data
, bytes
);