radeonsi: move PKT3_WRITE_DATA generation into a helper function
[mesa.git] / src / gallium / drivers / radeonsi / si_cp_dma.c
index b3621f794f59aa9da3db9cc869ae7ab0f425dc44..59360c0d4aa33278dca6498c1899562c339615b2 100644 (file)
 #include "si_pipe.h"
 #include "sid.h"
 
-/* Recommended maximum sizes for optimal performance.
- * Fall back to compute or SDMA if the size is greater.
- */
-#define CP_DMA_COPY_PERF_THRESHOLD     (64 * 1024) /* copied from Vulkan */
-#define CP_DMA_CLEAR_PERF_THRESHOLD    (32 * 1024) /* guess (clear is much slower) */
-
 /* Set this if you want the ME to wait until CP DMA is done.
  * It should be set on the last CP DMA packet. */
 #define CP_DMA_SYNC            (1 << 0)
  * packet. It's for preventing a read-after-write (RAW) hazard between two
  * CP DMA packets. */
 #define CP_DMA_RAW_WAIT                (1 << 1)
-#define CP_DMA_USE_L2          (1 << 2) /* CIK+ */
+#define CP_DMA_DST_IS_GDS      (1 << 2)
 #define CP_DMA_CLEAR           (1 << 3)
+#define CP_DMA_PFP_SYNC_ME     (1 << 4)
+#define CP_DMA_SRC_IS_GDS      (1 << 5)
 
 /* The max number of bytes that can be copied per packet. */
 static inline unsigned cp_dma_max_byte_count(struct si_context *sctx)
@@ -58,14 +54,14 @@ static inline unsigned cp_dma_max_byte_count(struct si_context *sctx)
  * a buffer. The size must fit in bits [20:0]. If CP_DMA_CLEAR is set, src_va is a 32-bit
  * clear value.
  */
-static void si_emit_cp_dma(struct si_context *sctx, uint64_t dst_va,
-                          uint64_t src_va, unsigned size, unsigned flags,
-                          enum si_coherency coher)
+static void si_emit_cp_dma(struct si_context *sctx, struct radeon_cmdbuf *cs,
+                          uint64_t dst_va, uint64_t src_va, unsigned size,
+                          unsigned flags, enum si_cache_policy cache_policy)
 {
-       struct radeon_winsys_cs *cs = sctx->gfx_cs;
        uint32_t header = 0, command = 0;
 
        assert(size <= cp_dma_max_byte_count(sctx));
+       assert(sctx->chip_class != SI || cache_policy == L2_BYPASS);
 
        if (sctx->chip_class >= GFX9)
                command |= S_414_BYTE_COUNT_GFX9(size);
@@ -87,15 +83,29 @@ static void si_emit_cp_dma(struct si_context *sctx, uint64_t dst_va,
 
        /* Src and dst flags. */
        if (sctx->chip_class >= GFX9 && !(flags & CP_DMA_CLEAR) &&
-           src_va == dst_va)
-               header |= S_411_DSL_SEL(V_411_NOWHERE); /* prefetch only */
-       else if (flags & CP_DMA_USE_L2)
-               header |= S_411_DSL_SEL(V_411_DST_ADDR_TC_L2);
+           src_va == dst_va) {
+               header |= S_411_DST_SEL(V_411_NOWHERE); /* prefetch only */
+       } else if (flags & CP_DMA_DST_IS_GDS) {
+               header |= S_411_DST_SEL(V_411_GDS);
+               /* GDS increments the address, not CP. */
+               command |= S_414_DAS(V_414_REGISTER) |
+                          S_414_DAIC(V_414_NO_INCREMENT);
+       } else if (sctx->chip_class >= CIK && cache_policy != L2_BYPASS) {
+               header |= S_411_DST_SEL(V_411_DST_ADDR_TC_L2) |
+                         S_500_DST_CACHE_POLICY(cache_policy == L2_STREAM);
+       }
 
-       if (flags & CP_DMA_CLEAR)
+       if (flags & CP_DMA_CLEAR) {
                header |= S_411_SRC_SEL(V_411_DATA);
-       else if (flags & CP_DMA_USE_L2)
-               header |= S_411_SRC_SEL(V_411_SRC_ADDR_TC_L2);
+       } else if (flags & CP_DMA_SRC_IS_GDS) {
+               header |= S_411_SRC_SEL(V_411_GDS);
+               /* Both of these are required for GDS. It does increment the address. */
+               command |= S_414_SAS(V_414_REGISTER) |
+                          S_414_SAIC(V_414_NO_INCREMENT);
+       } else if (sctx->chip_class >= CIK && cache_policy != L2_BYPASS) {
+               header |= S_411_SRC_SEL(V_411_SRC_ADDR_TC_L2) |
+                         S_500_SRC_CACHE_POLICY(cache_policy == L2_STREAM);
+       }
 
        if (sctx->chip_class >= CIK) {
                radeon_emit(cs, PKT3(PKT3_DMA_DATA, 5, 0));
@@ -121,7 +131,7 @@ static void si_emit_cp_dma(struct si_context *sctx, uint64_t dst_va,
         * indices. If we wanted to execute CP DMA in PFP, this packet
         * should precede it.
         */
-       if (coher == SI_COHERENCY_SHADER && flags & CP_DMA_SYNC) {
+       if (flags & CP_DMA_PFP_SYNC_ME) {
                radeon_emit(cs, PKT3(PKT3_PFP_SYNC_ME, 0, 0));
                radeon_emit(cs, 0);
        }
@@ -135,37 +145,14 @@ void si_cp_dma_wait_for_idle(struct si_context *sctx)
         * DMA request, however, the CP will see the sync flag and still wait
         * for all DMAs to complete.
         */
-       si_emit_cp_dma(sctx, 0, 0, 0, CP_DMA_SYNC, SI_COHERENCY_NONE);
-}
-
-static unsigned get_flush_flags(struct si_context *sctx, enum si_coherency coher)
-{
-       switch (coher) {
-       default:
-       case SI_COHERENCY_NONE:
-               return 0;
-       case SI_COHERENCY_SHADER:
-               return SI_CONTEXT_INV_SMEM_L1 |
-                      SI_CONTEXT_INV_VMEM_L1 |
-                      (sctx->chip_class == SI ? SI_CONTEXT_INV_GLOBAL_L2 : 0);
-       case SI_COHERENCY_CB_META:
-               return SI_CONTEXT_FLUSH_AND_INV_CB;
-       }
-}
-
-static unsigned get_tc_l2_flag(struct si_context *sctx, enum si_coherency coher)
-{
-       if ((sctx->chip_class >= GFX9 && coher == SI_COHERENCY_CB_META) ||
-           (sctx->chip_class >= CIK && coher == SI_COHERENCY_SHADER))
-               return CP_DMA_USE_L2;
-
-       return 0;
+       si_emit_cp_dma(sctx, sctx->gfx_cs, 0, 0, 0, CP_DMA_SYNC, L2_BYPASS);
 }
 
 static void si_cp_dma_prepare(struct si_context *sctx, struct pipe_resource *dst,
                              struct pipe_resource *src, unsigned byte_count,
                              uint64_t remaining_size, unsigned user_flags,
-                             bool *is_first, unsigned *packet_flags)
+                             enum si_coherency coher, bool *is_first,
+                             unsigned *packet_flags)
 {
        /* Fast exit for a CPDMA prefetch. */
        if ((user_flags & SI_CPDMA_SKIP_ALL) == SI_CPDMA_SKIP_ALL) {
@@ -175,7 +162,8 @@ static void si_cp_dma_prepare(struct si_context *sctx, struct pipe_resource *dst
 
        if (!(user_flags & SI_CPDMA_SKIP_BO_LIST_UPDATE)) {
                /* Count memory usage in so that need_cs_space can take it into account. */
-               si_context_add_resource_size(sctx, dst);
+               if (dst)
+                       si_context_add_resource_size(sctx, dst);
                if (src)
                        si_context_add_resource_size(sctx, src);
        }
@@ -185,9 +173,10 @@ static void si_cp_dma_prepare(struct si_context *sctx, struct pipe_resource *dst
 
        /* This must be done after need_cs_space. */
        if (!(user_flags & SI_CPDMA_SKIP_BO_LIST_UPDATE)) {
-               radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
-                                         r600_resource(dst),
-                                         RADEON_USAGE_WRITE, RADEON_PRIO_CP_DMA);
+               if (dst)
+                       radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
+                                                 r600_resource(dst),
+                                                 RADEON_USAGE_WRITE, RADEON_PRIO_CP_DMA);
                if (src)
                        radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
                                                  r600_resource(src),
@@ -200,7 +189,8 @@ static void si_cp_dma_prepare(struct si_context *sctx, struct pipe_resource *dst
        if (!(user_flags & SI_CPDMA_SKIP_GFX_SYNC) && sctx->flags)
                si_emit_cache_flush(sctx);
 
-       if (!(user_flags & SI_CPDMA_SKIP_SYNC_BEFORE) && *is_first)
+       if (!(user_flags & SI_CPDMA_SKIP_SYNC_BEFORE) && *is_first &&
+           !(*packet_flags & CP_DMA_CLEAR))
                *packet_flags |= CP_DMA_RAW_WAIT;
 
        *is_first = false;
@@ -209,154 +199,58 @@ static void si_cp_dma_prepare(struct si_context *sctx, struct pipe_resource *dst
         * is written to memory.
         */
        if (!(user_flags & SI_CPDMA_SKIP_SYNC_AFTER) &&
-           byte_count == remaining_size)
+           byte_count == remaining_size) {
                *packet_flags |= CP_DMA_SYNC;
+
+               if (coher == SI_COHERENCY_SHADER)
+                       *packet_flags |= CP_DMA_PFP_SYNC_ME;
+       }
 }
 
-void si_clear_buffer(struct si_context *sctx, struct pipe_resource *dst,
-                    uint64_t offset, uint64_t size, unsigned value,
-                    enum si_coherency coher)
+void si_cp_dma_clear_buffer(struct si_context *sctx, struct radeon_cmdbuf *cs,
+                           struct pipe_resource *dst, uint64_t offset,
+                           uint64_t size, unsigned value, unsigned user_flags,
+                           enum si_coherency coher, enum si_cache_policy cache_policy)
 {
-       struct radeon_winsys *ws = sctx->ws;
        struct r600_resource *rdst = r600_resource(dst);
-       unsigned tc_l2_flag = get_tc_l2_flag(sctx, coher);
-       unsigned flush_flags = get_flush_flags(sctx, coher);
-       uint64_t dma_clear_size;
+       uint64_t va = (rdst ? rdst->gpu_address : 0) + offset;
        bool is_first = true;
 
-       if (!size)
-               return;
-
-       dma_clear_size = size & ~3ull;
+       assert(size && size % 4 == 0);
 
        /* Mark the buffer range of destination as valid (initialized),
         * so that transfer_map knows it should wait for the GPU when mapping
         * that range. */
-       util_range_add(&rdst->valid_buffer_range, offset,
-                      offset + dma_clear_size);
-
-       /* dma_clear_buffer can use clear_buffer on failure. Make sure that
-        * doesn't happen. We don't want an infinite recursion: */
-       if (sctx->dma_cs &&
-           !(dst->flags & PIPE_RESOURCE_FLAG_SPARSE) &&
-           (offset % 4 == 0) &&
-           /* CP DMA is very slow. Always use SDMA for big clears. This
-            * alone improves DeusEx:MD performance by 70%. */
-           (size > CP_DMA_CLEAR_PERF_THRESHOLD ||
-            /* Buffers not used by the GFX IB yet will be cleared by SDMA.
-             * This happens to move most buffer clears to SDMA, including
-             * DCC and CMASK clears, because pipe->clear clears them before
-             * si_emit_framebuffer_state (in a draw call) adds them.
-             * For example, DeusEx:MD has 21 buffer clears per frame and all
-             * of them are moved to SDMA thanks to this. */
-            !ws->cs_is_buffer_referenced(sctx->gfx_cs, rdst->buf,
-                                         RADEON_USAGE_READWRITE))) {
-               sctx->dma_clear_buffer(sctx, dst, offset, dma_clear_size, value);
-
-               offset += dma_clear_size;
-               size -= dma_clear_size;
-       } else if (dma_clear_size >= 4) {
-               uint64_t va = rdst->gpu_address + offset;
-
-               offset += dma_clear_size;
-               size -= dma_clear_size;
-
-               /* Flush the caches. */
-               sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
-                                SI_CONTEXT_CS_PARTIAL_FLUSH | flush_flags;
-
-               while (dma_clear_size) {
-                       unsigned byte_count = MIN2(dma_clear_size, cp_dma_max_byte_count(sctx));
-                       unsigned dma_flags = tc_l2_flag  | CP_DMA_CLEAR;
-
-                       si_cp_dma_prepare(sctx, dst, NULL, byte_count, dma_clear_size, 0,
-                                         &is_first, &dma_flags);
+       if (rdst)
+               util_range_add(&rdst->valid_buffer_range, offset, offset + size);
 
-                       /* Emit the clear packet. */
-                       si_emit_cp_dma(sctx, va, value, byte_count, dma_flags, coher);
-
-                       dma_clear_size -= byte_count;
-                       va += byte_count;
-               }
-
-               if (tc_l2_flag)
-                       rdst->TC_L2_dirty = true;
-
-               /* If it's not a framebuffer fast clear... */
-               if (coher == SI_COHERENCY_SHADER)
-                       sctx->num_cp_dma_calls++;
+       /* Flush the caches. */
+       if (rdst && !(user_flags & SI_CPDMA_SKIP_GFX_SYNC)) {
+               sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
+                              SI_CONTEXT_CS_PARTIAL_FLUSH |
+                              si_get_flush_flags(sctx, coher, cache_policy);
        }
 
-       if (size) {
-               /* Handle non-dword alignment.
-                *
-                * This function is called for embedded texture metadata clears,
-                * but those should always be properly aligned. */
-               assert(dst->target == PIPE_BUFFER);
-               assert(size < 4);
+       while (size) {
+               unsigned byte_count = MIN2(size, cp_dma_max_byte_count(sctx));
+               unsigned dma_flags = CP_DMA_CLEAR | (rdst ? 0 : CP_DMA_DST_IS_GDS);
 
-               pipe_buffer_write(&sctx->b, dst, offset, size, &value);
-       }
-}
+               si_cp_dma_prepare(sctx, dst, NULL, byte_count, size, user_flags,
+                                 coher, &is_first, &dma_flags);
 
-static void si_pipe_clear_buffer(struct pipe_context *ctx,
-                                struct pipe_resource *dst,
-                                unsigned offset, unsigned size,
-                                const void *clear_value_ptr,
-                                int clear_value_size)
-{
-       struct si_context *sctx = (struct si_context*)ctx;
-       uint32_t dword_value;
-       unsigned i;
-
-       assert(offset % clear_value_size == 0);
-       assert(size % clear_value_size == 0);
-
-       if (clear_value_size > 4) {
-               const uint32_t *u32 = clear_value_ptr;
-               bool clear_dword_duplicated = true;
-
-               /* See if we can lower large fills to dword fills. */
-               for (i = 1; i < clear_value_size / 4; i++)
-                       if (u32[0] != u32[i]) {
-                               clear_dword_duplicated = false;
-                               break;
-                       }
+               /* Emit the clear packet. */
+               si_emit_cp_dma(sctx, cs, va, value, byte_count, dma_flags, cache_policy);
 
-               if (!clear_dword_duplicated) {
-                       /* Use transform feedback for 64-bit, 96-bit, and
-                        * 128-bit fills.
-                        */
-                       union pipe_color_union clear_value;
-
-                       memcpy(&clear_value, clear_value_ptr, clear_value_size);
-                       si_blitter_begin(sctx, SI_DISABLE_RENDER_COND);
-                       util_blitter_clear_buffer(sctx->blitter, dst, offset,
-                                                 size, clear_value_size / 4,
-                                                 &clear_value);
-                       si_blitter_end(sctx);
-                       return;
-               }
+               size -= byte_count;
+               va += byte_count;
        }
 
-       /* Expand the clear value to a dword. */
-       switch (clear_value_size) {
-       case 1:
-               dword_value = *(uint8_t*)clear_value_ptr;
-               dword_value |= (dword_value << 8) |
-                              (dword_value << 16) |
-                              (dword_value << 24);
-               break;
-       case 2:
-               dword_value = *(uint16_t*)clear_value_ptr;
-               dword_value |= dword_value << 16;
-               break;
-       default:
-               dword_value = *(uint32_t*)clear_value_ptr;
-       }
+       if (rdst && cache_policy != L2_BYPASS)
+               rdst->TC_L2_dirty = true;
 
-       si_clear_buffer(sctx, dst, offset, size, dword_value,
-                       SI_COHERENCY_SHADER);
+       /* If it's not a framebuffer fast clear... */
+       if (coher == SI_COHERENCY_SHADER)
+               sctx->num_cp_dma_calls++;
 }
 
 /**
@@ -366,7 +260,9 @@ static void si_pipe_clear_buffer(struct pipe_context *ctx,
  * \param size  Remaining size to the CP DMA alignment.
  */
 static void si_cp_dma_realign_engine(struct si_context *sctx, unsigned size,
-                                    unsigned user_flags, bool *is_first)
+                                    unsigned user_flags, enum si_coherency coher,
+                                    enum si_cache_policy cache_policy,
+                                    bool *is_first)
 {
        uint64_t va;
        unsigned dma_flags = 0;
@@ -393,43 +289,48 @@ static void si_cp_dma_realign_engine(struct si_context *sctx, unsigned size,
 
        si_cp_dma_prepare(sctx, &sctx->scratch_buffer->b.b,
                          &sctx->scratch_buffer->b.b, size, size, user_flags,
-                         is_first, &dma_flags);
+                         coher, is_first, &dma_flags);
 
        va = sctx->scratch_buffer->gpu_address;
-       si_emit_cp_dma(sctx, va, va + SI_CPDMA_ALIGNMENT, size, dma_flags,
-                      SI_COHERENCY_SHADER);
+       si_emit_cp_dma(sctx, sctx->gfx_cs, va, va + SI_CPDMA_ALIGNMENT, size, dma_flags,
+                      cache_policy);
 }
 
 /**
  * Do memcpy between buffers using CP DMA.
+ * If src or dst is NULL, it means read or write GDS, respectively.
  *
  * \param user_flags   bitmask of SI_CPDMA_*
  */
-void si_copy_buffer(struct si_context *sctx,
-                   struct pipe_resource *dst, struct pipe_resource *src,
-                   uint64_t dst_offset, uint64_t src_offset, unsigned size,
-                   unsigned user_flags)
+void si_cp_dma_copy_buffer(struct si_context *sctx,
+                          struct pipe_resource *dst, struct pipe_resource *src,
+                          uint64_t dst_offset, uint64_t src_offset, unsigned size,
+                          unsigned user_flags, enum si_coherency coher,
+                          enum si_cache_policy cache_policy)
 {
        uint64_t main_dst_offset, main_src_offset;
        unsigned skipped_size = 0;
        unsigned realign_size = 0;
-       unsigned tc_l2_flag = get_tc_l2_flag(sctx, SI_COHERENCY_SHADER);
-       unsigned flush_flags = get_flush_flags(sctx, SI_COHERENCY_SHADER);
+       unsigned gds_flags = (dst ? 0 : CP_DMA_DST_IS_GDS) |
+                            (src ? 0 : CP_DMA_SRC_IS_GDS);
        bool is_first = true;
 
-       if (!size)
-               return;
+       assert(size);
 
-       if (dst != src || dst_offset != src_offset) {
-               /* Mark the buffer range of destination as valid (initialized),
-                * so that transfer_map knows it should wait for the GPU when mapping
-                * that range. */
-               util_range_add(&r600_resource(dst)->valid_buffer_range, dst_offset,
-                              dst_offset + size);
-       }
+       if (dst) {
+               /* Skip this for the L2 prefetch. */
+               if (dst != src || dst_offset != src_offset) {
+                       /* Mark the buffer range of destination as valid (initialized),
+                        * so that transfer_map knows it should wait for the GPU when mapping
+                        * that range. */
+                       util_range_add(&r600_resource(dst)->valid_buffer_range, dst_offset,
+                                      dst_offset + size);
+               }
 
-       dst_offset += r600_resource(dst)->gpu_address;
-       src_offset += r600_resource(src)->gpu_address;
+               dst_offset += r600_resource(dst)->gpu_address;
+       }
+       if (src)
+               src_offset += r600_resource(src)->gpu_address;
 
        /* The workarounds aren't needed on Fiji and beyond. */
        if (sctx->family <= CHIP_CARRIZO ||
@@ -444,8 +345,10 @@ void si_copy_buffer(struct si_context *sctx,
                /* If the copy begins unaligned, we must start copying from the next
                 * aligned block and the skipped part should be copied after everything
                 * else has been copied. Only the src alignment matters, not dst.
+                *
+                * GDS doesn't need the source address to be aligned.
                 */
-               if (src_offset % SI_CPDMA_ALIGNMENT) {
+               if (src && src_offset % SI_CPDMA_ALIGNMENT) {
                        skipped_size = SI_CPDMA_ALIGNMENT - (src_offset % SI_CPDMA_ALIGNMENT);
                        /* The main part will be skipped if the size is too small. */
                        skipped_size = MIN2(skipped_size, size);
@@ -454,24 +357,26 @@ void si_copy_buffer(struct si_context *sctx,
        }
 
        /* Flush the caches. */
-       if (!(user_flags & SI_CPDMA_SKIP_GFX_SYNC))
+       if ((dst || src) && !(user_flags & SI_CPDMA_SKIP_GFX_SYNC)) {
                sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
-                                SI_CONTEXT_CS_PARTIAL_FLUSH | flush_flags;
+                              SI_CONTEXT_CS_PARTIAL_FLUSH |
+                              si_get_flush_flags(sctx, coher, cache_policy);
+       }
 
        /* This is the main part doing the copying. Src is always aligned. */
        main_dst_offset = dst_offset + skipped_size;
        main_src_offset = src_offset + skipped_size;
 
        while (size) {
-               unsigned dma_flags = tc_l2_flag;
                unsigned byte_count = MIN2(size, cp_dma_max_byte_count(sctx));
+               unsigned dma_flags = gds_flags;
 
                si_cp_dma_prepare(sctx, dst, src, byte_count,
                                  size + skipped_size + realign_size,
-                                 user_flags, &is_first, &dma_flags);
+                                 user_flags, coher, &is_first, &dma_flags);
 
-               si_emit_cp_dma(sctx, main_dst_offset, main_src_offset,
-                              byte_count, dma_flags, SI_COHERENCY_SHADER);
+               si_emit_cp_dma(sctx, sctx->gfx_cs, main_dst_offset, main_src_offset,
+                              byte_count, dma_flags, cache_policy);
 
                size -= byte_count;
                main_src_offset += byte_count;
@@ -480,26 +385,27 @@ void si_copy_buffer(struct si_context *sctx,
 
        /* Copy the part we skipped because src wasn't aligned. */
        if (skipped_size) {
-               unsigned dma_flags = tc_l2_flag;
+               unsigned dma_flags = gds_flags;
 
                si_cp_dma_prepare(sctx, dst, src, skipped_size,
                                  skipped_size + realign_size, user_flags,
-                                 &is_first, &dma_flags);
+                                 coher, &is_first, &dma_flags);
 
-               si_emit_cp_dma(sctx, dst_offset, src_offset, skipped_size,
-                              dma_flags, SI_COHERENCY_SHADER);
+               si_emit_cp_dma(sctx, sctx->gfx_cs, dst_offset, src_offset, skipped_size,
+                              dma_flags, cache_policy);
        }
 
        /* Finally, realign the engine if the size wasn't aligned. */
-       if (realign_size)
-               si_cp_dma_realign_engine(sctx, realign_size, user_flags,
-                                        &is_first);
+       if (realign_size) {
+               si_cp_dma_realign_engine(sctx, realign_size, user_flags, coher,
+                                        cache_policy, &is_first);
+       }
 
-       if (tc_l2_flag)
+       if (dst && cache_policy != L2_BYPASS)
                r600_resource(dst)->TC_L2_dirty = true;
 
-       /* If it's not a prefetch... */
-       if (dst_offset != src_offset)
+       /* If it's not a prefetch or GDS copy... */
+       if (dst && src && (dst != src || dst_offset != src_offset))
                sctx->num_cp_dma_calls++;
 }
 
@@ -508,7 +414,8 @@ void cik_prefetch_TC_L2_async(struct si_context *sctx, struct pipe_resource *buf
 {
        assert(sctx->chip_class >= CIK);
 
-       si_copy_buffer(sctx, buf, buf, offset, offset, size, SI_CPDMA_SKIP_ALL);
+       si_cp_dma_copy_buffer(sctx, buf, buf, offset, offset, size,
+                             SI_CPDMA_SKIP_ALL, SI_COHERENCY_SHADER, L2_LRU);
 }
 
 static void cik_prefetch_shader_async(struct si_context *sctx,
@@ -522,7 +429,7 @@ static void cik_prefetch_shader_async(struct si_context *sctx,
 
 static void cik_prefetch_VBO_descriptors(struct si_context *sctx)
 {
-       if (!sctx->vertex_elements)
+       if (!sctx->vertex_elements || !sctx->vertex_elements->desc_list_byte_size)
                return;
 
        cik_prefetch_TC_L2_async(sctx, &sctx->vb_descriptors_buffer->b.b,
@@ -639,7 +546,63 @@ void cik_emit_prefetch_L2(struct si_context *sctx, bool vertex_stage_only)
        sctx->prefetch_L2_mask = 0;
 }
 
-void si_init_cp_dma_functions(struct si_context *sctx)
+void si_test_gds(struct si_context *sctx)
 {
-       sctx->b.clear_buffer = si_pipe_clear_buffer;
+       struct pipe_context *ctx = &sctx->b;
+       struct pipe_resource *src, *dst;
+       unsigned r[4] = {};
+       unsigned offset = debug_get_num_option("OFFSET", 16);
+
+       src = pipe_buffer_create(ctx->screen, 0, PIPE_USAGE_DEFAULT, 16);
+       dst = pipe_buffer_create(ctx->screen, 0, PIPE_USAGE_DEFAULT, 16);
+       si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, src, 0, 4, 0xabcdef01, 0, SI_COHERENCY_SHADER, L2_BYPASS);
+       si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, src, 4, 4, 0x23456789, 0, SI_COHERENCY_SHADER, L2_BYPASS);
+       si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, src, 8, 4, 0x87654321, 0, SI_COHERENCY_SHADER, L2_BYPASS);
+       si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, src, 12, 4, 0xfedcba98, 0, SI_COHERENCY_SHADER, L2_BYPASS);
+       si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, dst, 0, 16, 0xdeadbeef, 0, SI_COHERENCY_SHADER, L2_BYPASS);
+
+       si_cp_dma_copy_buffer(sctx, NULL, src, offset, 0, 16, 0, SI_COHERENCY_NONE, L2_BYPASS);
+       si_cp_dma_copy_buffer(sctx, dst, NULL, 0, offset, 16, 0, SI_COHERENCY_NONE, L2_BYPASS);
+
+       pipe_buffer_read(ctx, dst, 0, sizeof(r), r);
+       printf("GDS copy  = %08x %08x %08x %08x -> %s\n", r[0], r[1], r[2], r[3],
+                       r[0] == 0xabcdef01 && r[1] == 0x23456789 &&
+                       r[2] == 0x87654321 && r[3] == 0xfedcba98 ? "pass" : "fail");
+
+       si_cp_dma_clear_buffer(sctx, sctx->gfx_cs, NULL, offset, 16, 0xc1ea4146, 0, SI_COHERENCY_NONE, L2_BYPASS);
+       si_cp_dma_copy_buffer(sctx, dst, NULL, 0, offset, 16, 0, SI_COHERENCY_NONE, L2_BYPASS);
+
+       pipe_buffer_read(ctx, dst, 0, sizeof(r), r);
+       printf("GDS clear = %08x %08x %08x %08x -> %s\n", r[0], r[1], r[2], r[3],
+                       r[0] == 0xc1ea4146 && r[1] == 0xc1ea4146 &&
+                       r[2] == 0xc1ea4146 && r[3] == 0xc1ea4146 ? "pass" : "fail");
+
+       pipe_resource_reference(&src, NULL);
+       pipe_resource_reference(&dst, NULL);
+       exit(0);
+}
+
+void si_cp_write_data(struct si_context *sctx, struct r600_resource *buf,
+                     unsigned offset, unsigned size, unsigned dst_sel,
+                     unsigned engine, const void *data)
+{
+       struct radeon_cmdbuf *cs = sctx->gfx_cs;
+
+       assert(offset % 4 == 0);
+       assert(size % 4 == 0);
+
+       if (sctx->chip_class == SI && dst_sel == V_370_MEM)
+               dst_sel = V_370_MEM_GRBM;
+
+       radeon_add_to_buffer_list(sctx, cs, buf,
+                                 RADEON_USAGE_WRITE, RADEON_PRIO_CP_DMA);
+       uint64_t va = buf->gpu_address + offset;
+
+       radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 2 + size/4, 0));
+       radeon_emit(cs, S_370_DST_SEL(dst_sel) |
+                   S_370_WR_CONFIRM(1) |
+                   S_370_ENGINE_SEL(engine));
+       radeon_emit(cs, va);
+       radeon_emit(cs, va >> 32);
+       radeon_emit_array(cs, (const uint32_t*)data, size/4);
 }