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.
28 #include "radeon/r600_cs.h"
30 static unsigned si_descriptor_list_cs_space(unsigned count
, unsigned element_size
)
32 /* Ensure we have enough space to start a new range in a hole */
33 assert(element_size
>= 3);
35 /* 5 dwords for write to L2 + 3 bytes for the packet header of
36 * every disjoint range written to CE RAM.
38 return 5 + (3 * count
/ 2) + count
* element_size
;
41 static unsigned si_ce_needed_cs_space(void)
45 space
+= si_descriptor_list_cs_space(SI_NUM_SHADER_BUFFERS
+
46 SI_NUM_CONST_BUFFERS
, 4);
47 /* two 8-byte images share one 16-byte slot */
48 space
+= si_descriptor_list_cs_space(SI_NUM_IMAGES
/ 2 +
50 space
*= SI_NUM_SHADERS
;
52 space
+= si_descriptor_list_cs_space(SI_NUM_RW_BUFFERS
, 4);
54 /* Increment CE counter packet */
61 void si_need_cs_space(struct si_context
*ctx
)
63 struct radeon_winsys_cs
*cs
= ctx
->b
.gfx
.cs
;
64 struct radeon_winsys_cs
*ce_ib
= ctx
->ce_ib
;
66 /* There is no need to flush the DMA IB here, because
67 * r600_need_dma_space always flushes the GFX IB if there is
68 * a conflict, which means any unflushed DMA commands automatically
69 * precede the GFX IB (= they had no dependency on the GFX IB when
70 * they were submitted).
73 /* There are two memory usage counters in the winsys for all buffers
74 * that have been added (cs_add_buffer) and two counters in the pipe
75 * driver for those that haven't been added yet.
77 if (unlikely(!radeon_cs_memory_below_limit(ctx
->b
.screen
, ctx
->b
.gfx
.cs
,
78 ctx
->b
.vram
, ctx
->b
.gtt
))) {
81 ctx
->b
.gfx
.flush(ctx
, RADEON_FLUSH_ASYNC
, NULL
);
87 /* If the CS is sufficiently large, don't count the space needed
88 * and just flush if there is not enough space left.
90 if (!ctx
->b
.ws
->cs_check_space(cs
, 2048) ||
91 (ce_ib
&& !ctx
->b
.ws
->cs_check_space(ce_ib
, si_ce_needed_cs_space())))
92 ctx
->b
.gfx
.flush(ctx
, RADEON_FLUSH_ASYNC
, NULL
);
95 void si_context_gfx_flush(void *context
, unsigned flags
,
96 struct pipe_fence_handle
**fence
)
98 struct si_context
*ctx
= context
;
99 struct radeon_winsys_cs
*cs
= ctx
->b
.gfx
.cs
;
100 struct radeon_winsys
*ws
= ctx
->b
.ws
;
102 if (ctx
->gfx_flush_in_progress
)
105 if (!radeon_emitted(cs
, ctx
->b
.initial_gfx_cs_size
))
108 if (r600_check_device_reset(&ctx
->b
))
111 if (ctx
->screen
->b
.debug_flags
& DBG_CHECK_VM
)
112 flags
&= ~RADEON_FLUSH_ASYNC
;
114 /* If the state tracker is flushing the GFX IB, r600_flush_from_st is
115 * responsible for flushing the DMA IB and merging the fences from both.
116 * This code is only needed when the driver flushes the GFX IB
117 * internally, and it never asks for a fence handle.
119 if (radeon_emitted(ctx
->b
.dma
.cs
, 0)) {
120 assert(fence
== NULL
); /* internal flushes only */
121 ctx
->b
.dma
.flush(ctx
, flags
, NULL
);
124 ctx
->gfx_flush_in_progress
= true;
126 /* This CE dump should be done in parallel with the last draw. */
128 si_ce_save_all_descriptors_at_ib_end(ctx
);
130 r600_preflush_suspend_features(&ctx
->b
);
132 /* DRM 3.1.0 doesn't flush TC for VI correctly. */
133 if (ctx
->b
.chip_class
== VI
&& ctx
->b
.screen
->info
.drm_minor
<= 1) {
134 ctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
135 SI_CONTEXT_CS_PARTIAL_FLUSH
|
136 SI_CONTEXT_INV_GLOBAL_L2
|
137 SI_CONTEXT_INV_VMEM_L1
;
138 } else if (ctx
->b
.chip_class
== SI
) {
139 /* The kernel doesn't wait for idle before doing SURFACE_SYNC. */
140 ctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
141 SI_CONTEXT_CS_PARTIAL_FLUSH
;
144 si_emit_cache_flush(ctx
);
150 /* Save the IB for debug contexts. */
151 radeon_clear_saved_cs(&ctx
->last_gfx
);
152 radeon_save_cs(ws
, cs
, &ctx
->last_gfx
);
153 r600_resource_reference(&ctx
->last_trace_buf
, ctx
->trace_buf
);
154 r600_resource_reference(&ctx
->trace_buf
, NULL
);
158 ws
->cs_flush(cs
, flags
, &ctx
->b
.last_gfx_fence
);
160 ws
->fence_reference(fence
, ctx
->b
.last_gfx_fence
);
161 ctx
->b
.num_gfx_cs_flushes
++;
163 /* Check VM faults if needed. */
164 if (ctx
->screen
->b
.debug_flags
& DBG_CHECK_VM
) {
165 /* Use conservative timeout 800ms, after which we won't wait any
166 * longer and assume the GPU is hung.
168 ctx
->b
.ws
->fence_wait(ctx
->b
.ws
, ctx
->b
.last_gfx_fence
, 800*1000*1000);
170 si_check_vm_faults(&ctx
->b
, &ctx
->last_gfx
, RING_GFX
);
173 si_begin_new_cs(ctx
);
174 ctx
->gfx_flush_in_progress
= false;
177 void si_begin_new_cs(struct si_context
*ctx
)
182 /* Create a buffer used for writing trace IDs and initialize it to 0. */
183 assert(!ctx
->trace_buf
);
184 ctx
->trace_buf
= (struct r600_resource
*)
185 pipe_buffer_create(ctx
->b
.b
.screen
, 0,
186 PIPE_USAGE_STAGING
, 4);
188 pipe_buffer_write_nooverlap(&ctx
->b
.b
, &ctx
->trace_buf
->b
.b
,
189 0, sizeof(zero
), &zero
);
196 /* Flush read caches at the beginning of CS not flushed by the kernel. */
197 if (ctx
->b
.chip_class
>= CIK
)
198 ctx
->b
.flags
|= SI_CONTEXT_INV_SMEM_L1
|
199 SI_CONTEXT_INV_ICACHE
;
201 ctx
->b
.flags
|= R600_CONTEXT_START_PIPELINE_STATS
;
203 /* set all valid group as dirty so they get reemited on
206 si_pm4_reset_emitted(ctx
);
208 /* The CS initialization should be emitted before everything else. */
209 si_pm4_emit(ctx
, ctx
->init_config
);
210 if (ctx
->init_config_gs_rings
)
211 si_pm4_emit(ctx
, ctx
->init_config_gs_rings
);
213 if (ctx
->ce_preamble_ib
)
214 si_ce_enable_loads(ctx
->ce_preamble_ib
);
216 si_ce_enable_loads(ctx
->ce_ib
);
219 si_ce_restore_all_descriptors_at_ib_start(ctx
);
221 if (ctx
->b
.chip_class
>= CIK
)
222 si_mark_atom_dirty(ctx
, &ctx
->prefetch_L2
);
224 ctx
->framebuffer
.dirty_cbufs
= (1 << 8) - 1;
225 ctx
->framebuffer
.dirty_zsbuf
= true;
226 si_mark_atom_dirty(ctx
, &ctx
->framebuffer
.atom
);
228 si_mark_atom_dirty(ctx
, &ctx
->clip_regs
);
229 si_mark_atom_dirty(ctx
, &ctx
->clip_state
.atom
);
230 ctx
->msaa_sample_locs
.nr_samples
= 0;
231 si_mark_atom_dirty(ctx
, &ctx
->msaa_sample_locs
.atom
);
232 si_mark_atom_dirty(ctx
, &ctx
->msaa_config
);
233 si_mark_atom_dirty(ctx
, &ctx
->sample_mask
.atom
);
234 si_mark_atom_dirty(ctx
, &ctx
->cb_render_state
);
235 si_mark_atom_dirty(ctx
, &ctx
->blend_color
.atom
);
236 si_mark_atom_dirty(ctx
, &ctx
->db_render_state
);
237 si_mark_atom_dirty(ctx
, &ctx
->stencil_ref
.atom
);
238 si_mark_atom_dirty(ctx
, &ctx
->spi_map
);
239 si_mark_atom_dirty(ctx
, &ctx
->b
.streamout
.enable_atom
);
240 si_mark_atom_dirty(ctx
, &ctx
->b
.render_cond_atom
);
241 si_all_descriptors_begin_new_cs(ctx
);
242 si_all_resident_buffers_begin_new_cs(ctx
);
244 ctx
->b
.scissors
.dirty_mask
= (1 << R600_MAX_VIEWPORTS
) - 1;
245 ctx
->b
.viewports
.dirty_mask
= (1 << R600_MAX_VIEWPORTS
) - 1;
246 ctx
->b
.viewports
.depth_range_dirty_mask
= (1 << R600_MAX_VIEWPORTS
) - 1;
247 si_mark_atom_dirty(ctx
, &ctx
->b
.scissors
.atom
);
248 si_mark_atom_dirty(ctx
, &ctx
->b
.viewports
.atom
);
250 si_mark_atom_dirty(ctx
, &ctx
->scratch_state
);
251 if (ctx
->scratch_buffer
) {
252 r600_context_add_resource_size(&ctx
->b
.b
,
253 &ctx
->scratch_buffer
->b
.b
);
256 r600_postflush_resume_features(&ctx
->b
);
258 assert(!ctx
->b
.gfx
.cs
->prev_dw
);
259 ctx
->b
.initial_gfx_cs_size
= ctx
->b
.gfx
.cs
->current
.cdw
;
261 /* Invalidate various draw states so that they are emitted before
262 * the first draw call. */
263 si_invalidate_draw_sh_constants(ctx
);
264 ctx
->last_index_size
= -1;
265 ctx
->last_primitive_restart_en
= -1;
266 ctx
->last_restart_index
= SI_RESTART_INDEX_UNKNOWN
;
267 ctx
->last_gs_out_prim
= -1;
269 ctx
->last_multi_vgt_param
= -1;
270 ctx
->last_rast_prim
= -1;
271 ctx
->last_sc_line_stipple
= ~0;
272 ctx
->last_vs_state
= ~0;
274 ctx
->last_tcs
= NULL
;
275 ctx
->last_tes_sh_base
= -1;
276 ctx
->last_num_tcs_input_cp
= -1;
278 ctx
->cs_shader_state
.initialized
= false;