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 batch
->buf
= dri_bo_alloc(intel
->intelScreen
->bufmgr
, "batchbuffer",
82 intel
->intelScreen
->maxBatchSize
, 4096,
83 DRM_BO_FLAG_MEM_LOCAL
| DRM_BO_FLAG_CACHED
| DRM_BO_FLAG_CACHED_MAPPED
);
84 dri_bo_map(batch
->buf
, GL_TRUE
);
85 batch
->map
= batch
->buf
->virtual;
86 batch
->size
= intel
->intelScreen
->maxBatchSize
;
87 batch
->ptr
= batch
->map
;
90 struct intel_batchbuffer
*
91 intel_batchbuffer_alloc(struct intel_context
*intel
)
93 struct intel_batchbuffer
*batch
= calloc(sizeof(*batch
), 1);
96 batch
->last_fence
= NULL
;
97 intel_batchbuffer_reset(batch
);
103 intel_batchbuffer_free(struct intel_batchbuffer
*batch
)
105 if (batch
->last_fence
) {
106 dri_fence_wait(batch
->last_fence
);
107 dri_fence_unreference(batch
->last_fence
);
108 batch
->last_fence
= NULL
;
111 dri_bo_unmap(batch
->buf
);
114 dri_bo_unreference(batch
->buf
);
121 /* TODO: Push this whole function into bufmgr.
124 do_flush_locked(struct intel_batchbuffer
*batch
,
126 GLboolean ignore_cliprects
, GLboolean allow_unlock
)
128 struct intel_context
*intel
= batch
->intel
;
132 start
= dri_process_relocs(batch
->buf
, &count
);
138 /* Throw away non-effective packets. Won't work once we have
139 * hardware contexts which would preserve statechanges beyond a
143 if (!(intel
->numClipRects
== 0 && !ignore_cliprects
)) {
144 if (intel
->intelScreen
->ttm
== GL_TRUE
) {
145 intel_exec_ioctl(batch
->intel
,
146 used
, ignore_cliprects
, allow_unlock
,
147 start
, count
, &batch
->last_fence
);
149 intel_batch_ioctl(batch
->intel
,
151 used
, ignore_cliprects
, allow_unlock
);
155 dri_post_submit(batch
->buf
, &batch
->last_fence
);
157 if (intel
->numClipRects
== 0 && !ignore_cliprects
) {
159 /* If we are not doing any actual user-visible rendering,
160 * do a sched_yield to keep the app from pegging the cpu while
163 UNLOCK_HARDWARE(intel
);
165 LOCK_HARDWARE(intel
);
167 intel
->vtbl
.lost_hardware(intel
);
170 if (INTEL_DEBUG
& DEBUG_BATCH
) {
171 dri_bo_map(batch
->buf
, GL_FALSE
);
172 intel_decode(batch
->buf
->virtual, used
/ 4, batch
->buf
->offset
,
173 intel
->intelScreen
->deviceID
);
174 dri_bo_unmap(batch
->buf
);
179 intel_batchbuffer_flush(struct intel_batchbuffer
*batch
)
181 struct intel_context
*intel
= batch
->intel
;
182 GLuint used
= batch
->ptr
- batch
->map
;
183 GLboolean was_locked
= intel
->locked
;
188 /* Add the MI_BATCH_BUFFER_END. Always add an MI_FLUSH - this is a
189 * performance drain that we would like to avoid.
192 ((int *) batch
->ptr
)[0] = intel
->vtbl
.flush_cmd();
193 ((int *) batch
->ptr
)[1] = 0;
194 ((int *) batch
->ptr
)[2] = MI_BATCH_BUFFER_END
;
198 ((int *) batch
->ptr
)[0] = intel
->vtbl
.flush_cmd();
199 ((int *) batch
->ptr
)[1] = MI_BATCH_BUFFER_END
;
203 /* TODO: Just pass the relocation list and dma buffer up to the
207 LOCK_HARDWARE(intel
);
209 do_flush_locked(batch
, used
, !(batch
->flags
& INTEL_BATCH_CLIPRECTS
),
213 UNLOCK_HARDWARE(intel
);
215 if (INTEL_DEBUG
& DEBUG_SYNC
) {
216 fprintf(stderr
, "waiting for idle\n");
217 if (batch
->last_fence
!= NULL
)
218 dri_fence_wait(batch
->last_fence
);
223 intel_batchbuffer_reset(batch
);
227 intel_batchbuffer_finish(struct intel_batchbuffer
*batch
)
229 intel_batchbuffer_flush(batch
);
230 if (batch
->last_fence
!= NULL
)
231 dri_fence_wait(batch
->last_fence
);
235 /* This is the only way buffers get added to the validate list.
238 intel_batchbuffer_emit_reloc(struct intel_batchbuffer
*batch
,
240 GLuint flags
, GLuint delta
)
242 dri_emit_reloc(batch
->buf
, flags
, delta
, batch
->ptr
- batch
->map
, buffer
);
249 intel_batchbuffer_data(struct intel_batchbuffer
*batch
,
250 const void *data
, GLuint bytes
, GLuint flags
)
252 assert((bytes
& 3) == 0);
253 intel_batchbuffer_require_space(batch
, bytes
, flags
);
254 __memcpy(batch
->ptr
, data
, bytes
);