1b8e80941Smrg/*
2b8e80941Smrg * Copyright (c) 2012-2017 Etnaviv Project
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the
12b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
13b8e80941Smrg * of the Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Authors:
24b8e80941Smrg *    Wladimir J. van der Laan <laanwj@gmail.com>
25b8e80941Smrg */
26b8e80941Smrg
27b8e80941Smrg#include "etnaviv_rs.h"
28b8e80941Smrg
29b8e80941Smrg#include "etnaviv_clear_blit.h"
30b8e80941Smrg#include "etnaviv_context.h"
31b8e80941Smrg#include "etnaviv_emit.h"
32b8e80941Smrg#include "etnaviv_format.h"
33b8e80941Smrg#include "etnaviv_resource.h"
34b8e80941Smrg#include "etnaviv_screen.h"
35b8e80941Smrg#include "etnaviv_surface.h"
36b8e80941Smrg#include "etnaviv_tiling.h"
37b8e80941Smrg#include "etnaviv_translate.h"
38b8e80941Smrg#include "etnaviv_util.h"
39b8e80941Smrg
40b8e80941Smrg#include "pipe/p_defines.h"
41b8e80941Smrg#include "pipe/p_state.h"
42b8e80941Smrg#include "util/u_blitter.h"
43b8e80941Smrg#include "util/u_inlines.h"
44b8e80941Smrg#include "util/u_memory.h"
45b8e80941Smrg#include "util/u_surface.h"
46b8e80941Smrg
47b8e80941Smrg#include "hw/common.xml.h"
48b8e80941Smrg#include "hw/state.xml.h"
49b8e80941Smrg#include "hw/state_3d.xml.h"
50b8e80941Smrg
51b8e80941Smrg#include <assert.h>
52b8e80941Smrg
53b8e80941Smrgvoid
54b8e80941Smrgetna_compile_rs_state(struct etna_context *ctx, struct compiled_rs_state *cs,
55b8e80941Smrg                      const struct rs_state *rs)
56b8e80941Smrg{
57b8e80941Smrg   memset(cs, 0, sizeof(*cs));
58b8e80941Smrg
59b8e80941Smrg   /* TILED and SUPERTILED layout have their strides multiplied with 4 in RS */
60b8e80941Smrg   unsigned source_stride_shift = COND(rs->source_tiling != ETNA_LAYOUT_LINEAR, 2);
61b8e80941Smrg   unsigned dest_stride_shift = COND(rs->dest_tiling != ETNA_LAYOUT_LINEAR, 2);
62b8e80941Smrg
63b8e80941Smrg   /* tiling == ETNA_LAYOUT_MULTI_TILED or ETNA_LAYOUT_MULTI_SUPERTILED? */
64b8e80941Smrg   int source_multi = COND(rs->source_tiling & ETNA_LAYOUT_BIT_MULTI, 1);
65b8e80941Smrg   int dest_multi = COND(rs->dest_tiling & ETNA_LAYOUT_BIT_MULTI, 1);
66b8e80941Smrg
67b8e80941Smrg   /* Vivante RS needs widths to be a multiple of 16 or bad things
68b8e80941Smrg    * happen, such as scribbing over memory, or the GPU hanging,
69b8e80941Smrg    * even for non-tiled formats.  As this is serious, use abort().
70b8e80941Smrg    */
71b8e80941Smrg   if (rs->width & ETNA_RS_WIDTH_MASK)
72b8e80941Smrg      abort();
73b8e80941Smrg
74b8e80941Smrg   /* TODO could just pre-generate command buffer, would simply submit to one memcpy */
75b8e80941Smrg   cs->RS_CONFIG = VIVS_RS_CONFIG_SOURCE_FORMAT(rs->source_format) |
76b8e80941Smrg                   COND(rs->downsample_x, VIVS_RS_CONFIG_DOWNSAMPLE_X) |
77b8e80941Smrg                   COND(rs->downsample_y, VIVS_RS_CONFIG_DOWNSAMPLE_Y) |
78b8e80941Smrg                   COND(rs->source_tiling & 1, VIVS_RS_CONFIG_SOURCE_TILED) |
79b8e80941Smrg                   VIVS_RS_CONFIG_DEST_FORMAT(rs->dest_format) |
80b8e80941Smrg                   COND(rs->dest_tiling & 1, VIVS_RS_CONFIG_DEST_TILED) |
81b8e80941Smrg                   COND(rs->swap_rb, VIVS_RS_CONFIG_SWAP_RB) |
82b8e80941Smrg                   COND(rs->flip, VIVS_RS_CONFIG_FLIP);
83b8e80941Smrg
84b8e80941Smrg   cs->RS_SOURCE_STRIDE = (rs->source_stride << source_stride_shift) |
85b8e80941Smrg                          COND(rs->source_tiling & 2, VIVS_RS_SOURCE_STRIDE_TILING) |
86b8e80941Smrg                          COND(source_multi, VIVS_RS_SOURCE_STRIDE_MULTI);
87b8e80941Smrg
88b8e80941Smrg   /* Initially all pipes are set to the base address of the source and
89b8e80941Smrg    * destination buffer respectively. This will be overridden below as
90b8e80941Smrg    * necessary for the multi-pipe, multi-tiled case.
91b8e80941Smrg    */
92b8e80941Smrg   for (unsigned pipe = 0; pipe < ctx->specs.pixel_pipes; ++pipe) {
93b8e80941Smrg      cs->source[pipe].bo = rs->source;
94b8e80941Smrg      cs->source[pipe].offset = rs->source_offset;
95b8e80941Smrg      cs->source[pipe].flags = ETNA_RELOC_READ;
96b8e80941Smrg
97b8e80941Smrg      cs->dest[pipe].bo = rs->dest;
98b8e80941Smrg      cs->dest[pipe].offset = rs->dest_offset;
99b8e80941Smrg      cs->dest[pipe].flags = ETNA_RELOC_WRITE;
100b8e80941Smrg
101b8e80941Smrg      cs->RS_PIPE_OFFSET[pipe] = VIVS_RS_PIPE_OFFSET_X(0) | VIVS_RS_PIPE_OFFSET_Y(0);
102b8e80941Smrg   }
103b8e80941Smrg
104b8e80941Smrg   cs->RS_DEST_STRIDE = (rs->dest_stride << dest_stride_shift) |
105b8e80941Smrg                        COND(rs->dest_tiling & 2, VIVS_RS_DEST_STRIDE_TILING) |
106b8e80941Smrg                        COND(dest_multi, VIVS_RS_DEST_STRIDE_MULTI);
107b8e80941Smrg
108b8e80941Smrg   if (ctx->specs.pixel_pipes == 1 || ctx->specs.single_buffer) {
109b8e80941Smrg      cs->RS_WINDOW_SIZE = VIVS_RS_WINDOW_SIZE_WIDTH(rs->width) |
110b8e80941Smrg                           VIVS_RS_WINDOW_SIZE_HEIGHT(rs->height);
111b8e80941Smrg   } else if (ctx->specs.pixel_pipes == 2) {
112b8e80941Smrg      assert((rs->height & 7) == 0); /* GPU hangs happen if height not 8-aligned */
113b8e80941Smrg
114b8e80941Smrg      if (source_multi)
115b8e80941Smrg         cs->source[1].offset = rs->source_offset + rs->source_stride * rs->source_padded_height / 2;
116b8e80941Smrg
117b8e80941Smrg      if (dest_multi)
118b8e80941Smrg         cs->dest[1].offset = rs->dest_offset + rs->dest_stride * rs->dest_padded_height / 2;
119b8e80941Smrg
120b8e80941Smrg      cs->RS_WINDOW_SIZE = VIVS_RS_WINDOW_SIZE_WIDTH(rs->width) |
121b8e80941Smrg                           VIVS_RS_WINDOW_SIZE_HEIGHT(rs->height / 2);
122b8e80941Smrg      cs->RS_PIPE_OFFSET[1] = VIVS_RS_PIPE_OFFSET_X(0) | VIVS_RS_PIPE_OFFSET_Y(rs->height / 2);
123b8e80941Smrg   } else {
124b8e80941Smrg      abort();
125b8e80941Smrg   }
126b8e80941Smrg
127b8e80941Smrg   cs->RS_DITHER[0] = rs->dither[0];
128b8e80941Smrg   cs->RS_DITHER[1] = rs->dither[1];
129b8e80941Smrg   cs->RS_CLEAR_CONTROL = VIVS_RS_CLEAR_CONTROL_BITS(rs->clear_bits) | rs->clear_mode;
130b8e80941Smrg   cs->RS_FILL_VALUE[0] = rs->clear_value[0];
131b8e80941Smrg   cs->RS_FILL_VALUE[1] = rs->clear_value[1];
132b8e80941Smrg   cs->RS_FILL_VALUE[2] = rs->clear_value[2];
133b8e80941Smrg   cs->RS_FILL_VALUE[3] = rs->clear_value[3];
134b8e80941Smrg   cs->RS_EXTRA_CONFIG = VIVS_RS_EXTRA_CONFIG_AA(rs->aa) |
135b8e80941Smrg                         VIVS_RS_EXTRA_CONFIG_ENDIAN(rs->endian_mode);
136b8e80941Smrg
137b8e80941Smrg   /* If source the same as destination, and the hardware supports this,
138b8e80941Smrg    * do an in-place resolve to fill in unrendered tiles.
139b8e80941Smrg    */
140b8e80941Smrg   if (ctx->specs.single_buffer && rs->source == rs->dest &&
141b8e80941Smrg         rs->source_offset == rs->dest_offset &&
142b8e80941Smrg         rs->source_format == rs->dest_format &&
143b8e80941Smrg         rs->source_tiling == rs->dest_tiling &&
144b8e80941Smrg         (rs->source_tiling & ETNA_LAYOUT_BIT_SUPER) &&
145b8e80941Smrg         rs->source_stride == rs->dest_stride &&
146b8e80941Smrg         !rs->downsample_x && !rs->downsample_y &&
147b8e80941Smrg         !rs->swap_rb && !rs->flip &&
148b8e80941Smrg         !rs->clear_mode && rs->source_padded_width) {
149b8e80941Smrg      /* Total number of tiles (same as for autodisable) */
150b8e80941Smrg      cs->RS_KICKER_INPLACE = rs->tile_count;
151b8e80941Smrg   }
152b8e80941Smrg   cs->source_ts_valid = rs->source_ts_valid;
153b8e80941Smrg}
154b8e80941Smrg
155b8e80941Smrg/* modify the clear bits value in the compiled RS state */
156b8e80941Smrgstatic void
157b8e80941Smrgetna_modify_rs_clearbits(struct compiled_rs_state *cs, uint32_t clear_bits)
158b8e80941Smrg{
159b8e80941Smrg   cs->RS_CLEAR_CONTROL &= ~VIVS_RS_CLEAR_CONTROL_BITS__MASK;
160b8e80941Smrg   cs->RS_CLEAR_CONTROL |= VIVS_RS_CLEAR_CONTROL_BITS(clear_bits);
161b8e80941Smrg}
162b8e80941Smrg
163b8e80941Smrg#define EMIT_STATE(state_name, src_value) \
164b8e80941Smrg   etna_coalsence_emit(stream, &coalesce, VIVS_##state_name, src_value)
165b8e80941Smrg
166b8e80941Smrg#define EMIT_STATE_FIXP(state_name, src_value) \
167b8e80941Smrg   etna_coalsence_emit_fixp(stream, &coalesce, VIVS_##state_name, src_value)
168b8e80941Smrg
169b8e80941Smrg#define EMIT_STATE_RELOC(state_name, src_value) \
170b8e80941Smrg   etna_coalsence_emit_reloc(stream, &coalesce, VIVS_##state_name, src_value)
171b8e80941Smrg
172b8e80941Smrg/* submit RS state, without any processing and no dependence on context
173b8e80941Smrg * except TS if this is a source-to-destination blit. */
174b8e80941Smrgstatic void
175b8e80941Smrgetna_submit_rs_state(struct etna_context *ctx,
176b8e80941Smrg                     const struct compiled_rs_state *cs)
177b8e80941Smrg{
178b8e80941Smrg   struct etna_screen *screen = etna_screen(ctx->base.screen);
179b8e80941Smrg   struct etna_cmd_stream *stream = ctx->stream;
180b8e80941Smrg   struct etna_coalesce coalesce;
181b8e80941Smrg
182b8e80941Smrg   if (cs->RS_KICKER_INPLACE && !cs->source_ts_valid)
183b8e80941Smrg      /* Inplace resolve is no-op if TS is not configured */
184b8e80941Smrg      return;
185b8e80941Smrg
186b8e80941Smrg   ctx->stats.rs_operations++;
187b8e80941Smrg
188b8e80941Smrg   if (cs->RS_KICKER_INPLACE) {
189b8e80941Smrg      etna_cmd_stream_reserve(stream, 6);
190b8e80941Smrg      etna_coalesce_start(stream, &coalesce);
191b8e80941Smrg      /* 0/1 */ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
192b8e80941Smrg      /* 2/3 */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
193b8e80941Smrg      /* 4/5 */ EMIT_STATE(RS_KICKER_INPLACE, cs->RS_KICKER_INPLACE);
194b8e80941Smrg      etna_coalesce_end(stream, &coalesce);
195b8e80941Smrg   } else if (screen->specs.pixel_pipes == 1) {
196b8e80941Smrg      etna_cmd_stream_reserve(stream, 22);
197b8e80941Smrg      etna_coalesce_start(stream, &coalesce);
198b8e80941Smrg      /* 0/1 */ EMIT_STATE(RS_CONFIG, cs->RS_CONFIG);
199b8e80941Smrg      /* 2   */ EMIT_STATE_RELOC(RS_SOURCE_ADDR, &cs->source[0]);
200b8e80941Smrg      /* 3   */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
201b8e80941Smrg      /* 4   */ EMIT_STATE_RELOC(RS_DEST_ADDR, &cs->dest[0]);
202b8e80941Smrg      /* 5   */ EMIT_STATE(RS_DEST_STRIDE, cs->RS_DEST_STRIDE);
203b8e80941Smrg      /* 6/7 */ EMIT_STATE(RS_WINDOW_SIZE, cs->RS_WINDOW_SIZE);
204b8e80941Smrg      /* 8/9 */ EMIT_STATE(RS_DITHER(0), cs->RS_DITHER[0]);
205b8e80941Smrg      /*10   */ EMIT_STATE(RS_DITHER(1), cs->RS_DITHER[1]);
206b8e80941Smrg      /*11 - pad */
207b8e80941Smrg      /*12/13*/ EMIT_STATE(RS_CLEAR_CONTROL, cs->RS_CLEAR_CONTROL);
208b8e80941Smrg      /*14   */ EMIT_STATE(RS_FILL_VALUE(0), cs->RS_FILL_VALUE[0]);
209b8e80941Smrg      /*15   */ EMIT_STATE(RS_FILL_VALUE(1), cs->RS_FILL_VALUE[1]);
210b8e80941Smrg      /*16   */ EMIT_STATE(RS_FILL_VALUE(2), cs->RS_FILL_VALUE[2]);
211b8e80941Smrg      /*17   */ EMIT_STATE(RS_FILL_VALUE(3), cs->RS_FILL_VALUE[3]);
212b8e80941Smrg      /*18/19*/ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
213b8e80941Smrg      /*20/21*/ EMIT_STATE(RS_KICKER, 0xbeebbeeb);
214b8e80941Smrg      etna_coalesce_end(stream, &coalesce);
215b8e80941Smrg   } else if (screen->specs.pixel_pipes == 2) {
216b8e80941Smrg      etna_cmd_stream_reserve(stream, 34); /* worst case - both pipes multi=1 */
217b8e80941Smrg      etna_coalesce_start(stream, &coalesce);
218b8e80941Smrg      /* 0/1 */ EMIT_STATE(RS_CONFIG, cs->RS_CONFIG);
219b8e80941Smrg      /* 2/3 */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
220b8e80941Smrg      /* 4/5 */ EMIT_STATE(RS_DEST_STRIDE, cs->RS_DEST_STRIDE);
221b8e80941Smrg      /* 6/7 */ EMIT_STATE_RELOC(RS_PIPE_SOURCE_ADDR(0), &cs->source[0]);
222b8e80941Smrg      if (cs->RS_SOURCE_STRIDE & VIVS_RS_SOURCE_STRIDE_MULTI) {
223b8e80941Smrg         /*8 */ EMIT_STATE_RELOC(RS_PIPE_SOURCE_ADDR(1), &cs->source[1]);
224b8e80941Smrg         /*9 - pad */
225b8e80941Smrg      }
226b8e80941Smrg      /*10/11*/ EMIT_STATE_RELOC(RS_PIPE_DEST_ADDR(0), &cs->dest[0]);
227b8e80941Smrg      if (cs->RS_DEST_STRIDE & VIVS_RS_DEST_STRIDE_MULTI) {
228b8e80941Smrg         /*12*/ EMIT_STATE_RELOC(RS_PIPE_DEST_ADDR(1), &cs->dest[1]);
229b8e80941Smrg         /*13 - pad */
230b8e80941Smrg      }
231b8e80941Smrg      /*14/15*/ EMIT_STATE(RS_PIPE_OFFSET(0), cs->RS_PIPE_OFFSET[0]);
232b8e80941Smrg      /*16   */ EMIT_STATE(RS_PIPE_OFFSET(1), cs->RS_PIPE_OFFSET[1]);
233b8e80941Smrg      /*17 - pad */
234b8e80941Smrg      /*18/19*/ EMIT_STATE(RS_WINDOW_SIZE, cs->RS_WINDOW_SIZE);
235b8e80941Smrg      /*20/21*/ EMIT_STATE(RS_DITHER(0), cs->RS_DITHER[0]);
236b8e80941Smrg      /*22   */ EMIT_STATE(RS_DITHER(1), cs->RS_DITHER[1]);
237b8e80941Smrg      /*23 - pad */
238b8e80941Smrg      /*24/25*/ EMIT_STATE(RS_CLEAR_CONTROL, cs->RS_CLEAR_CONTROL);
239b8e80941Smrg      /*26   */ EMIT_STATE(RS_FILL_VALUE(0), cs->RS_FILL_VALUE[0]);
240b8e80941Smrg      /*27   */ EMIT_STATE(RS_FILL_VALUE(1), cs->RS_FILL_VALUE[1]);
241b8e80941Smrg      /*28   */ EMIT_STATE(RS_FILL_VALUE(2), cs->RS_FILL_VALUE[2]);
242b8e80941Smrg      /*29   */ EMIT_STATE(RS_FILL_VALUE(3), cs->RS_FILL_VALUE[3]);
243b8e80941Smrg      /*30/31*/ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
244b8e80941Smrg      /*32/33*/ EMIT_STATE(RS_KICKER, 0xbeebbeeb);
245b8e80941Smrg      etna_coalesce_end(stream, &coalesce);
246b8e80941Smrg   } else {
247b8e80941Smrg      abort();
248b8e80941Smrg   }
249b8e80941Smrg}
250b8e80941Smrg
251b8e80941Smrg/* Generate clear command for a surface (non-fast clear case) */
252b8e80941Smrgvoid
253b8e80941Smrgetna_rs_gen_clear_surface(struct etna_context *ctx, struct etna_surface *surf,
254b8e80941Smrg                          uint32_t clear_value)
255b8e80941Smrg{
256b8e80941Smrg   struct etna_resource *dst = etna_resource(surf->base.texture);
257b8e80941Smrg   uint32_t format = translate_rs_format(surf->base.format);
258b8e80941Smrg
259b8e80941Smrg   if (format == ETNA_NO_MATCH) {
260b8e80941Smrg      BUG("etna_rs_gen_clear_surface: Unhandled clear fmt %s", util_format_name(surf->base.format));
261b8e80941Smrg      format = RS_FORMAT_A8R8G8B8;
262b8e80941Smrg      assert(0);
263b8e80941Smrg   }
264b8e80941Smrg
265b8e80941Smrg   /* use tiled clear if width is multiple of 16 */
266b8e80941Smrg   bool tiled_clear = (surf->surf.padded_width & ETNA_RS_WIDTH_MASK) == 0 &&
267b8e80941Smrg                      (surf->surf.padded_height & ETNA_RS_HEIGHT_MASK) == 0;
268b8e80941Smrg
269b8e80941Smrg   etna_compile_rs_state( ctx, &surf->clear_command, &(struct rs_state) {
270b8e80941Smrg      .source_format = format,
271b8e80941Smrg      .dest_format = format,
272b8e80941Smrg      .dest = dst->bo,
273b8e80941Smrg      .dest_offset = surf->surf.offset,
274b8e80941Smrg      .dest_stride = surf->surf.stride,
275b8e80941Smrg      .dest_padded_height = surf->surf.padded_height,
276b8e80941Smrg      .dest_tiling = tiled_clear ? dst->layout : ETNA_LAYOUT_LINEAR,
277b8e80941Smrg      .dither = {0xffffffff, 0xffffffff},
278b8e80941Smrg      .width = surf->surf.padded_width, /* These must be padded to 16x4 if !LINEAR, otherwise RS will hang */
279b8e80941Smrg      .height = surf->surf.padded_height,
280b8e80941Smrg      .clear_value = {clear_value},
281b8e80941Smrg      .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1,
282b8e80941Smrg      .clear_bits = 0xffff
283b8e80941Smrg   });
284b8e80941Smrg}
285b8e80941Smrg
286b8e80941Smrgstatic void
287b8e80941Smrgetna_blit_clear_color_rs(struct pipe_context *pctx, struct pipe_surface *dst,
288b8e80941Smrg                      const union pipe_color_union *color)
289b8e80941Smrg{
290b8e80941Smrg   struct etna_context *ctx = etna_context(pctx);
291b8e80941Smrg   struct etna_surface *surf = etna_surface(dst);
292b8e80941Smrg   uint32_t new_clear_value = etna_clear_blit_pack_rgba(surf->base.format, color->f);
293b8e80941Smrg
294b8e80941Smrg   if (surf->surf.ts_size) { /* TS: use precompiled clear command */
295b8e80941Smrg      ctx->framebuffer.TS_COLOR_CLEAR_VALUE = new_clear_value;
296b8e80941Smrg
297b8e80941Smrg      if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
298b8e80941Smrg         /* Set number of color tiles to be filled */
299b8e80941Smrg         etna_set_state(ctx->stream, VIVS_TS_COLOR_AUTO_DISABLE_COUNT,
300b8e80941Smrg                        surf->surf.padded_width * surf->surf.padded_height / 16);
301b8e80941Smrg         ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_COLOR_AUTO_DISABLE;
302b8e80941Smrg      }
303b8e80941Smrg
304b8e80941Smrg      surf->level->ts_valid = true;
305b8e80941Smrg      ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
306b8e80941Smrg   } else if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
307b8e80941Smrg      /* If clear color changed, re-generate stored command */
308b8e80941Smrg      etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
309b8e80941Smrg   }
310b8e80941Smrg
311b8e80941Smrg   etna_submit_rs_state(ctx, &surf->clear_command);
312b8e80941Smrg
313b8e80941Smrg   surf->level->clear_value = new_clear_value;
314b8e80941Smrg   resource_written(ctx, surf->base.texture);
315b8e80941Smrg   etna_resource(surf->base.texture)->seqno++;
316b8e80941Smrg}
317b8e80941Smrg
318b8e80941Smrgstatic void
319b8e80941Smrgetna_blit_clear_zs_rs(struct pipe_context *pctx, struct pipe_surface *dst,
320b8e80941Smrg                   unsigned buffers, double depth, unsigned stencil)
321b8e80941Smrg{
322b8e80941Smrg   struct etna_context *ctx = etna_context(pctx);
323b8e80941Smrg   struct etna_surface *surf = etna_surface(dst);
324b8e80941Smrg   uint32_t new_clear_value = translate_clear_depth_stencil(surf->base.format, depth, stencil);
325b8e80941Smrg   uint32_t new_clear_bits = 0, clear_bits_depth, clear_bits_stencil;
326b8e80941Smrg
327b8e80941Smrg   /* Get the channels to clear */
328b8e80941Smrg   switch (surf->base.format) {
329b8e80941Smrg   case PIPE_FORMAT_Z16_UNORM:
330b8e80941Smrg      clear_bits_depth = 0xffff;
331b8e80941Smrg      clear_bits_stencil = 0;
332b8e80941Smrg      break;
333b8e80941Smrg   case PIPE_FORMAT_X8Z24_UNORM:
334b8e80941Smrg   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
335b8e80941Smrg      clear_bits_depth = 0xeeee;
336b8e80941Smrg      clear_bits_stencil = 0x1111;
337b8e80941Smrg      break;
338b8e80941Smrg   default:
339b8e80941Smrg      clear_bits_depth = clear_bits_stencil = 0xffff;
340b8e80941Smrg      break;
341b8e80941Smrg   }
342b8e80941Smrg
343b8e80941Smrg   if (buffers & PIPE_CLEAR_DEPTH)
344b8e80941Smrg      new_clear_bits |= clear_bits_depth;
345b8e80941Smrg   if (buffers & PIPE_CLEAR_STENCIL)
346b8e80941Smrg      new_clear_bits |= clear_bits_stencil;
347b8e80941Smrg   /* FIXME: when tile status is enabled, this becomes more complex as
348b8e80941Smrg    * we may separately clear the depth from the stencil.  In this case,
349b8e80941Smrg    * we want to resolve the surface, and avoid using the tile status.
350b8e80941Smrg    * We may be better off recording the pending clear operation,
351b8e80941Smrg    * delaying the actual clear to the first use.  This way, we can merge
352b8e80941Smrg    * consecutive clears together. */
353b8e80941Smrg   if (surf->surf.ts_size) { /* TS: use precompiled clear command */
354b8e80941Smrg      /* Set new clear depth value */
355b8e80941Smrg      ctx->framebuffer.TS_DEPTH_CLEAR_VALUE = new_clear_value;
356b8e80941Smrg      if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
357b8e80941Smrg         /* Set number of depth tiles to be filled */
358b8e80941Smrg         etna_set_state(ctx->stream, VIVS_TS_DEPTH_AUTO_DISABLE_COUNT,
359b8e80941Smrg                        surf->surf.padded_width * surf->surf.padded_height / 16);
360b8e80941Smrg         ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_DEPTH_AUTO_DISABLE;
361b8e80941Smrg      }
362b8e80941Smrg
363b8e80941Smrg      surf->level->ts_valid = true;
364b8e80941Smrg      ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
365b8e80941Smrg   } else {
366b8e80941Smrg      if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
367b8e80941Smrg         /* If clear depth value changed, re-generate stored command */
368b8e80941Smrg         etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
369b8e80941Smrg      }
370b8e80941Smrg      /* Update the channels to be cleared */
371b8e80941Smrg      etna_modify_rs_clearbits(&surf->clear_command, new_clear_bits);
372b8e80941Smrg   }
373b8e80941Smrg
374b8e80941Smrg   etna_submit_rs_state(ctx, &surf->clear_command);
375b8e80941Smrg
376b8e80941Smrg   surf->level->clear_value = new_clear_value;
377b8e80941Smrg   resource_written(ctx, surf->base.texture);
378b8e80941Smrg   etna_resource(surf->base.texture)->seqno++;
379b8e80941Smrg}
380b8e80941Smrg
381b8e80941Smrgstatic void
382b8e80941Smrgetna_clear_rs(struct pipe_context *pctx, unsigned buffers,
383b8e80941Smrg           const union pipe_color_union *color, double depth, unsigned stencil)
384b8e80941Smrg{
385b8e80941Smrg   struct etna_context *ctx = etna_context(pctx);
386b8e80941Smrg
387b8e80941Smrg   /* Flush color and depth cache before clearing anything.
388b8e80941Smrg    * This is especially important when coming from another surface, as
389b8e80941Smrg    * otherwise it may clear part of the old surface instead. */
390b8e80941Smrg   etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
391b8e80941Smrg   etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
392b8e80941Smrg
393b8e80941Smrg   /* Preparation: Flush the TS if needed. This must be done after flushing
394b8e80941Smrg    * color and depth, otherwise it can result in crashes */
395b8e80941Smrg   bool need_ts_flush = false;
396b8e80941Smrg   if ((buffers & PIPE_CLEAR_COLOR) && ctx->framebuffer_s.nr_cbufs) {
397b8e80941Smrg      struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[0]);
398b8e80941Smrg      if (surf->surf.ts_size)
399b8e80941Smrg         need_ts_flush = true;
400b8e80941Smrg   }
401b8e80941Smrg   if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL) {
402b8e80941Smrg      struct etna_surface *surf = etna_surface(ctx->framebuffer_s.zsbuf);
403b8e80941Smrg
404b8e80941Smrg      if (surf->surf.ts_size)
405b8e80941Smrg         need_ts_flush = true;
406b8e80941Smrg   }
407b8e80941Smrg
408b8e80941Smrg   if (need_ts_flush)
409b8e80941Smrg      etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
410b8e80941Smrg
411b8e80941Smrg   /* No need to set up the TS here as RS clear operations (in contrast to
412b8e80941Smrg    * resolve and copy) do not require the TS state.
413b8e80941Smrg    */
414b8e80941Smrg   if (buffers & PIPE_CLEAR_COLOR) {
415b8e80941Smrg      for (int idx = 0; idx < ctx->framebuffer_s.nr_cbufs; ++idx) {
416b8e80941Smrg         etna_blit_clear_color_rs(pctx, ctx->framebuffer_s.cbufs[idx],
417b8e80941Smrg                               &color[idx]);
418b8e80941Smrg      }
419b8e80941Smrg   }
420b8e80941Smrg
421b8e80941Smrg   /* Flush the color and depth caches before each RS clear operation
422b8e80941Smrg    * This fixes a hang on GC600. */
423b8e80941Smrg   if (buffers & PIPE_CLEAR_DEPTHSTENCIL && buffers & PIPE_CLEAR_COLOR)
424b8e80941Smrg      etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
425b8e80941Smrg                     VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
426b8e80941Smrg
427b8e80941Smrg   if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL)
428b8e80941Smrg      etna_blit_clear_zs_rs(pctx, ctx->framebuffer_s.zsbuf, buffers, depth, stencil);
429b8e80941Smrg
430b8e80941Smrg   etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
431b8e80941Smrg}
432b8e80941Smrg
433b8e80941Smrgstatic bool
434b8e80941Smrgetna_manual_blit(struct etna_resource *dst, struct etna_resource_level *dst_lev,
435b8e80941Smrg                 unsigned int dst_offset, struct etna_resource *src,
436b8e80941Smrg                 struct etna_resource_level *src_lev, unsigned int src_offset,
437b8e80941Smrg                 const struct pipe_blit_info *blit_info)
438b8e80941Smrg{
439b8e80941Smrg   void *smap, *srow, *dmap, *drow;
440b8e80941Smrg   size_t tile_size;
441b8e80941Smrg
442b8e80941Smrg   assert(src->layout == ETNA_LAYOUT_TILED);
443b8e80941Smrg   assert(dst->layout == ETNA_LAYOUT_TILED);
444b8e80941Smrg   assert(src->base.nr_samples == 0);
445b8e80941Smrg   assert(dst->base.nr_samples == 0);
446b8e80941Smrg
447b8e80941Smrg   tile_size = util_format_get_blocksize(blit_info->src.format) * 4 * 4;
448b8e80941Smrg
449b8e80941Smrg   smap = etna_bo_map(src->bo);
450b8e80941Smrg   if (!smap)
451b8e80941Smrg      return false;
452b8e80941Smrg
453b8e80941Smrg   dmap = etna_bo_map(dst->bo);
454b8e80941Smrg   if (!dmap)
455b8e80941Smrg      return false;
456b8e80941Smrg
457b8e80941Smrg   srow = smap + src_offset;
458b8e80941Smrg   drow = dmap + dst_offset;
459b8e80941Smrg
460b8e80941Smrg   etna_bo_cpu_prep(src->bo, DRM_ETNA_PREP_READ);
461b8e80941Smrg   etna_bo_cpu_prep(dst->bo, DRM_ETNA_PREP_WRITE);
462b8e80941Smrg
463b8e80941Smrg   for (int y = 0; y < blit_info->src.box.height; y += 4) {
464b8e80941Smrg      memcpy(drow, srow, tile_size * blit_info->src.box.width);
465b8e80941Smrg      srow += src_lev->stride * 4;
466b8e80941Smrg      drow += dst_lev->stride * 4;
467b8e80941Smrg   }
468b8e80941Smrg
469b8e80941Smrg   etna_bo_cpu_fini(dst->bo);
470b8e80941Smrg   etna_bo_cpu_fini(src->bo);
471b8e80941Smrg
472b8e80941Smrg   return true;
473b8e80941Smrg}
474b8e80941Smrg
475b8e80941Smrgstatic inline size_t
476b8e80941Smrgetna_compute_tileoffset(const struct pipe_box *box, enum pipe_format format,
477b8e80941Smrg                        size_t stride, enum etna_surface_layout layout)
478b8e80941Smrg{
479b8e80941Smrg   size_t offset;
480b8e80941Smrg   unsigned int x = box->x, y = box->y;
481b8e80941Smrg   unsigned int blocksize = util_format_get_blocksize(format);
482b8e80941Smrg
483b8e80941Smrg   switch (layout) {
484b8e80941Smrg   case ETNA_LAYOUT_LINEAR:
485b8e80941Smrg      offset = y * stride + x * blocksize;
486b8e80941Smrg      break;
487b8e80941Smrg   case ETNA_LAYOUT_MULTI_TILED:
488b8e80941Smrg      y >>= 1;
489b8e80941Smrg      /* fall-through */
490b8e80941Smrg   case ETNA_LAYOUT_TILED:
491b8e80941Smrg      assert(!(x & 0x03) && !(y & 0x03));
492b8e80941Smrg      offset = (y & ~0x03) * stride + blocksize * ((x & ~0x03) << 2);
493b8e80941Smrg      break;
494b8e80941Smrg   case ETNA_LAYOUT_MULTI_SUPERTILED:
495b8e80941Smrg      y >>= 1;
496b8e80941Smrg      /* fall-through */
497b8e80941Smrg   case ETNA_LAYOUT_SUPER_TILED:
498b8e80941Smrg      assert(!(x & 0x3f) && !(y & 0x3f));
499b8e80941Smrg      offset = (y & ~0x3f) * stride + blocksize * ((x & ~0x3f) << 6);
500b8e80941Smrg      break;
501b8e80941Smrg   default:
502b8e80941Smrg      unreachable("invalid resource layout");
503b8e80941Smrg   }
504b8e80941Smrg
505b8e80941Smrg   return offset;
506b8e80941Smrg}
507b8e80941Smrg
508b8e80941Smrgstatic inline void
509b8e80941Smrgetna_get_rs_alignment_mask(const struct etna_context *ctx,
510b8e80941Smrg                           const enum etna_surface_layout layout,
511b8e80941Smrg                           unsigned int *width_mask, unsigned int *height_mask)
512b8e80941Smrg{
513b8e80941Smrg   unsigned int h_align, w_align;
514b8e80941Smrg
515b8e80941Smrg   if (layout & ETNA_LAYOUT_BIT_SUPER) {
516b8e80941Smrg      w_align = h_align = 64;
517b8e80941Smrg   } else {
518b8e80941Smrg      w_align = ETNA_RS_WIDTH_MASK + 1;
519b8e80941Smrg      h_align = ETNA_RS_HEIGHT_MASK + 1;
520b8e80941Smrg   }
521b8e80941Smrg
522b8e80941Smrg   h_align *= ctx->screen->specs.pixel_pipes;
523b8e80941Smrg
524b8e80941Smrg   *width_mask = w_align - 1;
525b8e80941Smrg   *height_mask = h_align -1;
526b8e80941Smrg}
527b8e80941Smrg
528b8e80941Smrgstatic bool
529b8e80941Smrgetna_try_rs_blit(struct pipe_context *pctx,
530b8e80941Smrg                 const struct pipe_blit_info *blit_info)
531b8e80941Smrg{
532b8e80941Smrg   struct etna_context *ctx = etna_context(pctx);
533b8e80941Smrg   struct etna_resource *src = etna_resource(blit_info->src.resource);
534b8e80941Smrg   struct etna_resource *dst = etna_resource(blit_info->dst.resource);
535b8e80941Smrg   struct compiled_rs_state copy_to_screen;
536b8e80941Smrg   uint32_t ts_mem_config = 0;
537b8e80941Smrg   int msaa_xscale = 1, msaa_yscale = 1;
538b8e80941Smrg
539b8e80941Smrg   /* Ensure that the level is valid */
540b8e80941Smrg   assert(blit_info->src.level <= src->base.last_level);
541b8e80941Smrg   assert(blit_info->dst.level <= dst->base.last_level);
542b8e80941Smrg
543b8e80941Smrg   if (!translate_samples_to_xyscale(src->base.nr_samples, &msaa_xscale, &msaa_yscale, NULL))
544b8e80941Smrg      return FALSE;
545b8e80941Smrg
546b8e80941Smrg   /* The width/height are in pixels; they do not change as a result of
547b8e80941Smrg    * multi-sampling. So, when blitting from a 4x multisampled surface
548b8e80941Smrg    * to a non-multisampled surface, the width and height will be
549b8e80941Smrg    * identical. As we do not support scaling, reject different sizes. */
550b8e80941Smrg   if (blit_info->dst.box.width != blit_info->src.box.width ||
551b8e80941Smrg       blit_info->dst.box.height != blit_info->src.box.height) {
552b8e80941Smrg      DBG("scaling requested: source %dx%d destination %dx%d",
553b8e80941Smrg          blit_info->src.box.width, blit_info->src.box.height,
554b8e80941Smrg          blit_info->dst.box.width, blit_info->dst.box.height);
555b8e80941Smrg      return FALSE;
556b8e80941Smrg   }
557b8e80941Smrg
558b8e80941Smrg   /* No masks - RS can't copy specific channels */
559b8e80941Smrg   unsigned mask = util_format_get_mask(blit_info->dst.format);
560b8e80941Smrg   if ((blit_info->mask & mask) != mask) {
561b8e80941Smrg      DBG("sub-mask requested: 0x%02x vs format mask 0x%02x", blit_info->mask, mask);
562b8e80941Smrg      return FALSE;
563b8e80941Smrg   }
564b8e80941Smrg
565b8e80941Smrg   unsigned src_format = etna_compatible_rs_format(blit_info->src.format);
566b8e80941Smrg   unsigned dst_format = etna_compatible_rs_format(blit_info->dst.format);
567b8e80941Smrg   if (translate_rs_format(src_format) == ETNA_NO_MATCH ||
568b8e80941Smrg       translate_rs_format(dst_format) == ETNA_NO_MATCH ||
569b8e80941Smrg       blit_info->scissor_enable ||
570b8e80941Smrg       blit_info->dst.box.depth != blit_info->src.box.depth ||
571b8e80941Smrg       blit_info->dst.box.depth != 1) {
572b8e80941Smrg      return FALSE;
573b8e80941Smrg   }
574b8e80941Smrg
575b8e80941Smrg   unsigned w_mask, h_mask;
576b8e80941Smrg
577b8e80941Smrg   etna_get_rs_alignment_mask(ctx, src->layout, &w_mask, &h_mask);
578b8e80941Smrg   if ((blit_info->src.box.x & w_mask) || (blit_info->src.box.y & h_mask))
579b8e80941Smrg      return FALSE;
580b8e80941Smrg
581b8e80941Smrg   etna_get_rs_alignment_mask(ctx, dst->layout, &w_mask, &h_mask);
582b8e80941Smrg   if ((blit_info->dst.box.x & w_mask) || (blit_info->dst.box.y & h_mask))
583b8e80941Smrg      return FALSE;
584b8e80941Smrg
585b8e80941Smrg   /* Ensure that the Z coordinate is sane */
586b8e80941Smrg   if (dst->base.target != PIPE_TEXTURE_CUBE)
587b8e80941Smrg      assert(blit_info->dst.box.z == 0);
588b8e80941Smrg   if (src->base.target != PIPE_TEXTURE_CUBE)
589b8e80941Smrg      assert(blit_info->src.box.z == 0);
590b8e80941Smrg
591b8e80941Smrg   assert(blit_info->src.box.z < src->base.array_size);
592b8e80941Smrg   assert(blit_info->dst.box.z < dst->base.array_size);
593b8e80941Smrg
594b8e80941Smrg   struct etna_resource_level *src_lev = &src->levels[blit_info->src.level];
595b8e80941Smrg   struct etna_resource_level *dst_lev = &dst->levels[blit_info->dst.level];
596b8e80941Smrg
597b8e80941Smrg   /* we may be given coordinates up to the padded width to avoid
598b8e80941Smrg    * any alignment issues with different tiling formats */
599b8e80941Smrg   assert((blit_info->src.box.x + blit_info->src.box.width) * msaa_xscale <= src_lev->padded_width);
600b8e80941Smrg   assert((blit_info->src.box.y + blit_info->src.box.height) * msaa_yscale <= src_lev->padded_height);
601b8e80941Smrg   assert(blit_info->dst.box.x + blit_info->dst.box.width <= dst_lev->padded_width);
602b8e80941Smrg   assert(blit_info->dst.box.y + blit_info->dst.box.height <= dst_lev->padded_height);
603b8e80941Smrg
604b8e80941Smrg   unsigned src_offset = src_lev->offset +
605b8e80941Smrg                         blit_info->src.box.z * src_lev->layer_stride +
606b8e80941Smrg                         etna_compute_tileoffset(&blit_info->src.box,
607b8e80941Smrg                                                 blit_info->src.format,
608b8e80941Smrg                                                 src_lev->stride,
609b8e80941Smrg                                                 src->layout);
610b8e80941Smrg   unsigned dst_offset = dst_lev->offset +
611b8e80941Smrg                         blit_info->dst.box.z * dst_lev->layer_stride +
612b8e80941Smrg                         etna_compute_tileoffset(&blit_info->dst.box,
613b8e80941Smrg                                                 blit_info->dst.format,
614b8e80941Smrg                                                 dst_lev->stride,
615b8e80941Smrg                                                 dst->layout);
616b8e80941Smrg
617b8e80941Smrg   if (src_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
618b8e80941Smrg       dst_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
619b8e80941Smrg       src_lev->padded_height <= ETNA_RS_HEIGHT_MASK ||
620b8e80941Smrg       dst_lev->padded_height <= ETNA_RS_HEIGHT_MASK)
621b8e80941Smrg      goto manual;
622b8e80941Smrg
623b8e80941Smrg   /* If the width is not aligned to the RS width, but is within our
624b8e80941Smrg    * padding, adjust the width to suite the RS width restriction.
625b8e80941Smrg    * Note: the RS width/height are converted to source samples here. */
626b8e80941Smrg   unsigned int width = blit_info->src.box.width * msaa_xscale;
627b8e80941Smrg   unsigned int height = blit_info->src.box.height * msaa_yscale;
628b8e80941Smrg   unsigned int w_align = ETNA_RS_WIDTH_MASK + 1;
629b8e80941Smrg   unsigned int h_align = (ETNA_RS_HEIGHT_MASK + 1) * ctx->specs.pixel_pipes;
630b8e80941Smrg
631b8e80941Smrg   if (width & (w_align - 1) && width >= src_lev->width * msaa_xscale && width >= dst_lev->width)
632b8e80941Smrg      width = align(width, w_align);
633b8e80941Smrg
634b8e80941Smrg   if (height & (h_align - 1) && height >= src_lev->height * msaa_yscale && height >= dst_lev->height)
635b8e80941Smrg      height = align(height, h_align);
636b8e80941Smrg
637b8e80941Smrg   /* The padded dimensions are in samples */
638b8e80941Smrg   if (width > src_lev->padded_width ||
639b8e80941Smrg       width > dst_lev->padded_width * msaa_xscale ||
640b8e80941Smrg       height > src_lev->padded_height ||
641b8e80941Smrg       height > dst_lev->padded_height * msaa_yscale ||
642b8e80941Smrg       width & (w_align - 1) || height & (h_align - 1))
643b8e80941Smrg      goto manual;
644b8e80941Smrg
645b8e80941Smrg   if (src->base.nr_samples > 1) {
646b8e80941Smrg      uint32_t msaa_format = translate_msaa_format(src_format);
647b8e80941Smrg      assert(msaa_format != ETNA_NO_MATCH);
648b8e80941Smrg      ts_mem_config |= VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION | msaa_format;
649b8e80941Smrg   }
650b8e80941Smrg
651b8e80941Smrg   /* Always flush color and depth cache together before resolving. This works
652b8e80941Smrg    * around artifacts that appear in some cases when scanning out a texture
653b8e80941Smrg    * directly after it has been rendered to, such as rendering an animated web
654b8e80941Smrg    * page in a QtWebEngine based WebView on GC2000. The artifacts look like
655b8e80941Smrg    * the texture sampler samples zeroes instead of texture data in a small,
656b8e80941Smrg    * irregular triangle in the lower right of each browser tile quad. Other
657b8e80941Smrg    * attempts to avoid these artifacts, including a pipeline stall before the
658b8e80941Smrg    * color flush or a TS cache flush afterwards, or flushing multiple times,
659b8e80941Smrg    * with stalls before and after each flush, have shown no effect. */
660b8e80941Smrg   if (src->base.bind & PIPE_BIND_RENDER_TARGET ||
661b8e80941Smrg       src->base.bind & PIPE_BIND_DEPTH_STENCIL) {
662b8e80941Smrg      etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
663b8e80941Smrg		     VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
664b8e80941Smrg      etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
665b8e80941Smrg
666b8e80941Smrg      if (src->levels[blit_info->src.level].ts_size &&
667b8e80941Smrg          src->levels[blit_info->src.level].ts_valid)
668b8e80941Smrg         etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
669b8e80941Smrg   }
670b8e80941Smrg
671b8e80941Smrg   /* Set up color TS to source surface before blit, if needed */
672b8e80941Smrg   bool source_ts_valid = false;
673b8e80941Smrg   if (src->levels[blit_info->src.level].ts_size &&
674b8e80941Smrg       src->levels[blit_info->src.level].ts_valid) {
675b8e80941Smrg      struct etna_reloc reloc;
676b8e80941Smrg      unsigned ts_offset =
677b8e80941Smrg         src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
678b8e80941Smrg
679b8e80941Smrg      etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG,
680b8e80941Smrg                     VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR | ts_mem_config);
681b8e80941Smrg
682b8e80941Smrg      memset(&reloc, 0, sizeof(struct etna_reloc));
683b8e80941Smrg      reloc.bo = src->ts_bo;
684b8e80941Smrg      reloc.offset = ts_offset;
685b8e80941Smrg      reloc.flags = ETNA_RELOC_READ;
686b8e80941Smrg      etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_STATUS_BASE, &reloc);
687b8e80941Smrg
688b8e80941Smrg      memset(&reloc, 0, sizeof(struct etna_reloc));
689b8e80941Smrg      reloc.bo = src->bo;
690b8e80941Smrg      reloc.offset = src_lev->offset +
691b8e80941Smrg                     blit_info->src.box.z * src_lev->layer_stride;
692b8e80941Smrg      reloc.flags = ETNA_RELOC_READ;
693b8e80941Smrg      etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_SURFACE_BASE, &reloc);
694b8e80941Smrg
695b8e80941Smrg      etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE,
696b8e80941Smrg                     src->levels[blit_info->src.level].clear_value);
697b8e80941Smrg
698b8e80941Smrg      source_ts_valid = true;
699b8e80941Smrg   } else {
700b8e80941Smrg      etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG, ts_mem_config);
701b8e80941Smrg   }
702b8e80941Smrg   ctx->dirty |= ETNA_DIRTY_TS;
703b8e80941Smrg
704b8e80941Smrg   /* Kick off RS here */
705b8e80941Smrg   etna_compile_rs_state(ctx, &copy_to_screen, &(struct rs_state) {
706b8e80941Smrg      .source_format = translate_rs_format(src_format),
707b8e80941Smrg      .source_tiling = src->layout,
708b8e80941Smrg      .source = src->bo,
709b8e80941Smrg      .source_offset = src_offset,
710b8e80941Smrg      .source_stride = src_lev->stride,
711b8e80941Smrg      .source_padded_width = src_lev->padded_width,
712b8e80941Smrg      .source_padded_height = src_lev->padded_height,
713b8e80941Smrg      .source_ts_valid = source_ts_valid,
714b8e80941Smrg      .dest_format = translate_rs_format(dst_format),
715b8e80941Smrg      .dest_tiling = dst->layout,
716b8e80941Smrg      .dest = dst->bo,
717b8e80941Smrg      .dest_offset = dst_offset,
718b8e80941Smrg      .dest_stride = dst_lev->stride,
719b8e80941Smrg      .dest_padded_height = dst_lev->padded_height,
720b8e80941Smrg      .downsample_x = msaa_xscale > 1,
721b8e80941Smrg      .downsample_y = msaa_yscale > 1,
722b8e80941Smrg      .swap_rb = translate_rb_src_dst_swap(src->base.format, dst->base.format),
723b8e80941Smrg      .dither = {0xffffffff, 0xffffffff}, // XXX dither when going from 24 to 16 bit?
724b8e80941Smrg      .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_DISABLED,
725b8e80941Smrg      .width = width,
726b8e80941Smrg      .height = height,
727b8e80941Smrg      .tile_count = src_lev->layer_stride / 64
728b8e80941Smrg   });
729b8e80941Smrg
730b8e80941Smrg   etna_submit_rs_state(ctx, &copy_to_screen);
731b8e80941Smrg   resource_read(ctx, &src->base);
732b8e80941Smrg   resource_written(ctx, &dst->base);
733b8e80941Smrg   dst->seqno++;
734b8e80941Smrg   dst->levels[blit_info->dst.level].ts_valid = false;
735b8e80941Smrg   ctx->dirty |= ETNA_DIRTY_DERIVE_TS;
736b8e80941Smrg
737b8e80941Smrg   return TRUE;
738b8e80941Smrg
739b8e80941Smrgmanual:
740b8e80941Smrg   if (src->layout == ETNA_LAYOUT_TILED && dst->layout == ETNA_LAYOUT_TILED) {
741b8e80941Smrg      if ((src->status & ETNA_PENDING_WRITE) ||
742b8e80941Smrg          (dst->status & ETNA_PENDING_WRITE))
743b8e80941Smrg         pctx->flush(pctx, NULL, 0);
744b8e80941Smrg      return etna_manual_blit(dst, dst_lev, dst_offset, src, src_lev, src_offset, blit_info);
745b8e80941Smrg   }
746b8e80941Smrg
747b8e80941Smrg   return FALSE;
748b8e80941Smrg}
749b8e80941Smrg
750b8e80941Smrgstatic void
751b8e80941Smrgetna_blit_rs(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
752b8e80941Smrg{
753b8e80941Smrg   /* This is a more extended version of resource_copy_region */
754b8e80941Smrg   /* TODO Some cases can be handled by RS; if not, fall back to rendering or
755b8e80941Smrg    * even CPU copy block of pixels from info->src to info->dst
756b8e80941Smrg    * (resource, level, box, format);
757b8e80941Smrg    * function is used for scaling, flipping in x and y direction (negative
758b8e80941Smrg    * width/height), format conversion, mask and filter and even a scissor rectangle
759b8e80941Smrg    *
760b8e80941Smrg    * What can the RS do for us:
761b8e80941Smrg    *   convert between tiling formats (layouts)
762b8e80941Smrg    *   downsample 2x in x and y
763b8e80941Smrg    *   convert between a limited number of pixel formats
764b8e80941Smrg    *
765b8e80941Smrg    * For the rest, fall back to util_blitter
766b8e80941Smrg    * XXX this goes wrong when source surface is supertiled. */
767b8e80941Smrg   struct etna_context *ctx = etna_context(pctx);
768b8e80941Smrg   struct pipe_blit_info info = *blit_info;
769b8e80941Smrg
770b8e80941Smrg   if (info.src.resource->nr_samples > 1 &&
771b8e80941Smrg       info.dst.resource->nr_samples <= 1 &&
772b8e80941Smrg       !util_format_is_depth_or_stencil(info.src.resource->format) &&
773b8e80941Smrg       !util_format_is_pure_integer(info.src.resource->format)) {
774b8e80941Smrg      DBG("color resolve unimplemented");
775b8e80941Smrg      return;
776b8e80941Smrg   }
777b8e80941Smrg
778b8e80941Smrg   if (etna_try_rs_blit(pctx, blit_info))
779b8e80941Smrg      return;
780b8e80941Smrg
781b8e80941Smrg   if (util_try_blit_via_copy_region(pctx, blit_info))
782b8e80941Smrg      return;
783b8e80941Smrg
784b8e80941Smrg   if (info.mask & PIPE_MASK_S) {
785b8e80941Smrg      DBG("cannot blit stencil, skipping");
786b8e80941Smrg      info.mask &= ~PIPE_MASK_S;
787b8e80941Smrg   }
788b8e80941Smrg
789b8e80941Smrg   if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
790b8e80941Smrg      DBG("blit unsupported %s -> %s",
791b8e80941Smrg          util_format_short_name(info.src.resource->format),
792b8e80941Smrg          util_format_short_name(info.dst.resource->format));
793b8e80941Smrg      return;
794b8e80941Smrg   }
795b8e80941Smrg
796b8e80941Smrg   etna_blit_save_state(ctx);
797b8e80941Smrg   util_blitter_blit(ctx->blitter, &info);
798b8e80941Smrg}
799b8e80941Smrg
800b8e80941Smrgvoid
801b8e80941Smrgetna_clear_blit_rs_init(struct pipe_context *pctx)
802b8e80941Smrg{
803b8e80941Smrg   DBG("etnaviv: Using RS blit engine");
804b8e80941Smrg   pctx->clear = etna_clear_rs;
805b8e80941Smrg   pctx->blit = etna_blit_rs;
806b8e80941Smrg}
807