2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "radeon/r600_cs.h"
27 #include "util/os_time.h"
30 void si_need_gfx_cs_space(struct si_context
*ctx
)
32 struct radeon_winsys_cs
*cs
= ctx
->b
.gfx_cs
;
34 /* There is no need to flush the DMA IB here, because
35 * r600_need_dma_space always flushes the GFX IB if there is
36 * a conflict, which means any unflushed DMA commands automatically
37 * precede the GFX IB (= they had no dependency on the GFX IB when
38 * they were submitted).
41 /* There are two memory usage counters in the winsys for all buffers
42 * that have been added (cs_add_buffer) and two counters in the pipe
43 * driver for those that haven't been added yet.
45 if (unlikely(!radeon_cs_memory_below_limit(ctx
->b
.screen
, ctx
->b
.gfx_cs
,
46 ctx
->b
.vram
, ctx
->b
.gtt
))) {
49 si_flush_gfx_cs(ctx
, PIPE_FLUSH_ASYNC
, NULL
);
55 /* If the IB is sufficiently large, don't count the space needed
56 * and just flush if there is not enough space left.
58 * Also reserve space for stopping queries at the end of IB, because
59 * the number of active queries is mostly unlimited.
61 unsigned need_dwords
= 2048 + ctx
->b
.num_cs_dw_queries_suspend
;
62 if (!ctx
->b
.ws
->cs_check_space(cs
, need_dwords
))
63 si_flush_gfx_cs(ctx
, PIPE_FLUSH_ASYNC
, NULL
);
66 void si_flush_gfx_cs(void *context
, unsigned flags
,
67 struct pipe_fence_handle
**fence
)
69 struct si_context
*ctx
= context
;
70 struct radeon_winsys_cs
*cs
= ctx
->b
.gfx_cs
;
71 struct radeon_winsys
*ws
= ctx
->b
.ws
;
73 if (ctx
->gfx_flush_in_progress
)
76 if (!radeon_emitted(cs
, ctx
->b
.initial_gfx_cs_size
))
79 if (si_check_device_reset(&ctx
->b
))
82 if (ctx
->screen
->debug_flags
& DBG(CHECK_VM
))
83 flags
&= ~PIPE_FLUSH_ASYNC
;
85 /* If the state tracker is flushing the GFX IB, r600_flush_from_st is
86 * responsible for flushing the DMA IB and merging the fences from both.
87 * This code is only needed when the driver flushes the GFX IB
88 * internally, and it never asks for a fence handle.
90 if (radeon_emitted(ctx
->b
.dma_cs
, 0)) {
91 assert(fence
== NULL
); /* internal flushes only */
92 si_flush_dma_cs(ctx
, flags
, NULL
);
95 ctx
->gfx_flush_in_progress
= true;
97 if (!LIST_IS_EMPTY(&ctx
->b
.active_queries
))
98 si_suspend_queries(&ctx
->b
);
100 ctx
->streamout
.suspended
= false;
101 if (ctx
->streamout
.begin_emitted
) {
102 si_emit_streamout_end(ctx
);
103 ctx
->streamout
.suspended
= true;
106 ctx
->b
.flags
|= SI_CONTEXT_CS_PARTIAL_FLUSH
|
107 SI_CONTEXT_PS_PARTIAL_FLUSH
;
109 /* DRM 3.1.0 doesn't flush TC for VI correctly. */
110 if (ctx
->b
.chip_class
== VI
&& ctx
->b
.screen
->info
.drm_minor
<= 1)
111 ctx
->b
.flags
|= SI_CONTEXT_INV_GLOBAL_L2
|
112 SI_CONTEXT_INV_VMEM_L1
;
114 si_emit_cache_flush(ctx
);
116 if (ctx
->current_saved_cs
) {
118 si_log_hw_flush(ctx
);
120 /* Save the IB for debug contexts. */
121 si_save_cs(ws
, cs
, &ctx
->current_saved_cs
->gfx
, true);
122 ctx
->current_saved_cs
->flushed
= true;
123 ctx
->current_saved_cs
->time_flush
= os_time_get_nano();
127 ws
->cs_flush(cs
, flags
, &ctx
->b
.last_gfx_fence
);
129 ws
->fence_reference(fence
, ctx
->b
.last_gfx_fence
);
131 /* This must be after cs_flush returns, since the context's API
132 * thread can concurrently read this value in si_fence_finish. */
133 ctx
->b
.num_gfx_cs_flushes
++;
135 /* Check VM faults if needed. */
136 if (ctx
->screen
->debug_flags
& DBG(CHECK_VM
)) {
137 /* Use conservative timeout 800ms, after which we won't wait any
138 * longer and assume the GPU is hung.
140 ctx
->b
.ws
->fence_wait(ctx
->b
.ws
, ctx
->b
.last_gfx_fence
, 800*1000*1000);
142 si_check_vm_faults(ctx
, &ctx
->current_saved_cs
->gfx
, RING_GFX
);
145 if (ctx
->current_saved_cs
)
146 si_saved_cs_reference(&ctx
->current_saved_cs
, NULL
);
148 si_begin_new_gfx_cs(ctx
);
149 ctx
->gfx_flush_in_progress
= false;
152 static void si_begin_gfx_cs_debug(struct si_context
*ctx
)
154 static const uint32_t zeros
[1];
155 assert(!ctx
->current_saved_cs
);
157 ctx
->current_saved_cs
= calloc(1, sizeof(*ctx
->current_saved_cs
));
158 if (!ctx
->current_saved_cs
)
161 pipe_reference_init(&ctx
->current_saved_cs
->reference
, 1);
163 ctx
->current_saved_cs
->trace_buf
= (struct r600_resource
*)
164 pipe_buffer_create(ctx
->b
.b
.screen
, 0,
165 PIPE_USAGE_STAGING
, 8);
166 if (!ctx
->current_saved_cs
->trace_buf
) {
167 free(ctx
->current_saved_cs
);
168 ctx
->current_saved_cs
= NULL
;
172 pipe_buffer_write_nooverlap(&ctx
->b
.b
, &ctx
->current_saved_cs
->trace_buf
->b
.b
,
173 0, sizeof(zeros
), zeros
);
174 ctx
->current_saved_cs
->trace_id
= 0;
178 radeon_add_to_buffer_list(&ctx
->b
, ctx
->b
.gfx_cs
, ctx
->current_saved_cs
->trace_buf
,
179 RADEON_USAGE_READWRITE
, RADEON_PRIO_TRACE
);
182 void si_begin_new_gfx_cs(struct si_context
*ctx
)
185 si_begin_gfx_cs_debug(ctx
);
187 /* Flush read caches at the beginning of CS not flushed by the kernel. */
188 if (ctx
->b
.chip_class
>= CIK
)
189 ctx
->b
.flags
|= SI_CONTEXT_INV_SMEM_L1
|
190 SI_CONTEXT_INV_ICACHE
;
192 ctx
->b
.flags
|= SI_CONTEXT_START_PIPELINE_STATS
;
194 /* set all valid group as dirty so they get reemited on
197 si_pm4_reset_emitted(ctx
);
199 /* The CS initialization should be emitted before everything else. */
200 si_pm4_emit(ctx
, ctx
->init_config
);
201 if (ctx
->init_config_gs_rings
)
202 si_pm4_emit(ctx
, ctx
->init_config_gs_rings
);
204 if (ctx
->queued
.named
.ls
)
205 ctx
->prefetch_L2_mask
|= SI_PREFETCH_LS
;
206 if (ctx
->queued
.named
.hs
)
207 ctx
->prefetch_L2_mask
|= SI_PREFETCH_HS
;
208 if (ctx
->queued
.named
.es
)
209 ctx
->prefetch_L2_mask
|= SI_PREFETCH_ES
;
210 if (ctx
->queued
.named
.gs
)
211 ctx
->prefetch_L2_mask
|= SI_PREFETCH_GS
;
212 if (ctx
->queued
.named
.vs
)
213 ctx
->prefetch_L2_mask
|= SI_PREFETCH_VS
;
214 if (ctx
->queued
.named
.ps
)
215 ctx
->prefetch_L2_mask
|= SI_PREFETCH_PS
;
216 if (ctx
->vb_descriptors_buffer
&& ctx
->vertex_elements
)
217 ctx
->prefetch_L2_mask
|= SI_PREFETCH_VBO_DESCRIPTORS
;
219 /* CLEAR_STATE disables all colorbuffers, so only enable bound ones. */
220 bool has_clear_state
= ctx
->screen
->has_clear_state
;
221 if (has_clear_state
) {
222 ctx
->framebuffer
.dirty_cbufs
=
223 u_bit_consecutive(0, ctx
->framebuffer
.state
.nr_cbufs
);
224 /* CLEAR_STATE disables the zbuffer, so only enable it if it's bound. */
225 ctx
->framebuffer
.dirty_zsbuf
= ctx
->framebuffer
.state
.zsbuf
!= NULL
;
227 ctx
->framebuffer
.dirty_cbufs
= u_bit_consecutive(0, 8);
228 ctx
->framebuffer
.dirty_zsbuf
= true;
230 /* This should always be marked as dirty to set the framebuffer scissor
232 si_mark_atom_dirty(ctx
, &ctx
->framebuffer
.atom
);
234 si_mark_atom_dirty(ctx
, &ctx
->clip_regs
);
235 /* CLEAR_STATE sets zeros. */
236 if (!has_clear_state
|| ctx
->clip_state
.any_nonzeros
)
237 si_mark_atom_dirty(ctx
, &ctx
->clip_state
.atom
);
238 ctx
->msaa_sample_locs
.nr_samples
= 0;
239 si_mark_atom_dirty(ctx
, &ctx
->msaa_sample_locs
.atom
);
240 si_mark_atom_dirty(ctx
, &ctx
->msaa_config
);
241 /* CLEAR_STATE sets 0xffff. */
242 if (!has_clear_state
|| ctx
->sample_mask
.sample_mask
!= 0xffff)
243 si_mark_atom_dirty(ctx
, &ctx
->sample_mask
.atom
);
244 si_mark_atom_dirty(ctx
, &ctx
->cb_render_state
);
245 /* CLEAR_STATE sets zeros. */
246 if (!has_clear_state
|| ctx
->blend_color
.any_nonzeros
)
247 si_mark_atom_dirty(ctx
, &ctx
->blend_color
.atom
);
248 si_mark_atom_dirty(ctx
, &ctx
->db_render_state
);
249 if (ctx
->b
.chip_class
>= GFX9
)
250 si_mark_atom_dirty(ctx
, &ctx
->dpbb_state
);
251 si_mark_atom_dirty(ctx
, &ctx
->stencil_ref
.atom
);
252 si_mark_atom_dirty(ctx
, &ctx
->spi_map
);
253 si_mark_atom_dirty(ctx
, &ctx
->streamout
.enable_atom
);
254 si_mark_atom_dirty(ctx
, &ctx
->b
.render_cond_atom
);
255 si_all_descriptors_begin_new_cs(ctx
);
256 si_all_resident_buffers_begin_new_cs(ctx
);
258 ctx
->scissors
.dirty_mask
= (1 << SI_MAX_VIEWPORTS
) - 1;
259 ctx
->viewports
.dirty_mask
= (1 << SI_MAX_VIEWPORTS
) - 1;
260 ctx
->viewports
.depth_range_dirty_mask
= (1 << SI_MAX_VIEWPORTS
) - 1;
261 si_mark_atom_dirty(ctx
, &ctx
->scissors
.atom
);
262 si_mark_atom_dirty(ctx
, &ctx
->viewports
.atom
);
264 si_mark_atom_dirty(ctx
, &ctx
->scratch_state
);
265 if (ctx
->scratch_buffer
) {
266 si_context_add_resource_size(&ctx
->b
.b
,
267 &ctx
->scratch_buffer
->b
.b
);
270 if (ctx
->streamout
.suspended
) {
271 ctx
->streamout
.append_bitmask
= ctx
->streamout
.enabled_mask
;
272 si_streamout_buffers_dirty(ctx
);
275 if (!LIST_IS_EMPTY(&ctx
->b
.active_queries
))
276 si_resume_queries(&ctx
->b
);
278 assert(!ctx
->b
.gfx_cs
->prev_dw
);
279 ctx
->b
.initial_gfx_cs_size
= ctx
->b
.gfx_cs
->current
.cdw
;
281 /* Invalidate various draw states so that they are emitted before
282 * the first draw call. */
283 si_invalidate_draw_sh_constants(ctx
);
284 ctx
->last_index_size
= -1;
285 ctx
->last_primitive_restart_en
= -1;
286 ctx
->last_restart_index
= SI_RESTART_INDEX_UNKNOWN
;
287 ctx
->last_gs_out_prim
= -1;
289 ctx
->last_multi_vgt_param
= -1;
290 ctx
->last_rast_prim
= -1;
291 ctx
->last_sc_line_stipple
= ~0;
292 ctx
->last_vs_state
= ~0;
294 ctx
->last_tcs
= NULL
;
295 ctx
->last_tes_sh_base
= -1;
296 ctx
->last_num_tcs_input_cp
= -1;
298 ctx
->cs_shader_state
.initialized
= false;