util: remove LIST_ADDTAIL macro
[mesa.git] / src / gallium / drivers / radeon / radeon_vce.c
index 0ff66f48fba695c7a47654c15dbb81b981c6853e..af46fa776a6ed37d1dfc81a20b08c8bdb84dc094 100644 (file)
  *
  **************************************************************************/
 
-/*
- * Authors:
- *      Christian König <christian.koenig@amd.com>
- *
- */
-
 #include <stdio.h>
 
 #include "pipe/p_video_codec.h"
@@ -40,7 +34,7 @@
 
 #include "vl/vl_video_buffer.h"
 
-#include "r600_pipe_common.h"
+#include "radeonsi/si_pipe.h"
 #include "radeon_video.h"
 #include "radeon_vce.h"
 
 #define FW_52_0_3 ((52 << 24) | (0 << 16) | (3 << 8))
 #define FW_52_4_3 ((52 << 24) | (4 << 16) | (3 << 8))
 #define FW_52_8_3 ((52 << 24) | (8 << 16) | (3 << 8))
+#define FW_53 (53 << 24)
 
 /**
  * flush commands to the hardware
  */
 static void flush(struct rvce_encoder *enc)
 {
-       enc->ws->cs_flush(enc->cs, RADEON_FLUSH_ASYNC, NULL);
+       enc->ws->cs_flush(enc->cs, PIPE_FLUSH_ASYNC, NULL);
        enc->task_info_idx = 0;
        enc->bs_idx = 0;
 }
@@ -96,14 +91,14 @@ static void reset_cpb(struct rvce_encoder *enc)
 {
        unsigned i;
 
-       LIST_INITHEAD(&enc->cpb_slots);
+       list_inithead(&enc->cpb_slots);
        for (i = 0; i < enc->cpb_num; ++i) {
                struct rvce_cpb_slot *slot = &enc->cpb_array[i];
                slot->index = i;
                slot->picture_type = PIPE_H264_ENC_PICTURE_TYPE_SKIP;
                slot->frame_num = 0;
                slot->pic_order_cnt = 0;
-               LIST_ADDTAIL(&slot->list, &enc->cpb_slots);
+               list_addtail(&slot->list, &enc->cpb_slots);
        }
 }
 
@@ -197,7 +192,7 @@ static unsigned get_cpb_num(struct rvce_encoder *enc)
 /**
  * Get the slot for the currently encoded frame
  */
-struct rvce_cpb_slot *current_slot(struct rvce_encoder *enc)
+struct rvce_cpb_slot *si_current_slot(struct rvce_encoder *enc)
 {
        return LIST_ENTRY(struct rvce_cpb_slot, enc->cpb_slots.prev, list);
 }
@@ -205,7 +200,7 @@ struct rvce_cpb_slot *current_slot(struct rvce_encoder *enc)
 /**
  * Get the slot for L0
  */
-struct rvce_cpb_slot *l0_slot(struct rvce_encoder *enc)
+struct rvce_cpb_slot *si_l0_slot(struct rvce_encoder *enc)
 {
        return LIST_ENTRY(struct rvce_cpb_slot, enc->cpb_slots.next, list);
 }
@@ -213,7 +208,7 @@ struct rvce_cpb_slot *l0_slot(struct rvce_encoder *enc)
 /**
  * Get the slot for L1
  */
-struct rvce_cpb_slot *l1_slot(struct rvce_encoder *enc)
+struct rvce_cpb_slot *si_l1_slot(struct rvce_encoder *enc)
 {
        return LIST_ENTRY(struct rvce_cpb_slot, enc->cpb_slots.next->next, list);
 }
@@ -221,12 +216,20 @@ struct rvce_cpb_slot *l1_slot(struct rvce_encoder *enc)
 /**
  * Calculate the offsets into the CPB
  */
-void rvce_frame_offset(struct rvce_encoder *enc, struct rvce_cpb_slot *slot,
-                      signed *luma_offset, signed *chroma_offset)
+void si_vce_frame_offset(struct rvce_encoder *enc, struct rvce_cpb_slot *slot,
+                        signed *luma_offset, signed *chroma_offset)
 {
-       unsigned pitch = align(enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe, 128);
-       unsigned vpitch = align(enc->luma->u.legacy.level[0].nblk_y, 16);
-       unsigned fsize = pitch * (vpitch + vpitch / 2);
+       struct si_screen *sscreen = (struct si_screen *)enc->screen;
+       unsigned pitch, vpitch, fsize;
+
+       if (sscreen->info.chip_class < GFX9) {
+               pitch = align(enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe, 128);
+               vpitch = align(enc->luma->u.legacy.level[0].nblk_y, 16);
+       } else {
+               pitch = align(enc->luma->u.gfx9.surf_pitch * enc->luma->bpe, 256);
+               vpitch = align(enc->luma->u.gfx9.surf_height, 16);
+       }
+       fsize = pitch * (vpitch + vpitch / 2);
 
        *luma_offset = slot->index * fsize;
        *chroma_offset = *luma_offset + pitch * vpitch;
@@ -240,15 +243,14 @@ static void rvce_destroy(struct pipe_video_codec *encoder)
        struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
        if (enc->stream_handle) {
                struct rvid_buffer fb;
-               rvid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
+               si_vid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
                enc->fb = &fb;
                enc->session(enc);
-               enc->feedback(enc);
                enc->destroy(enc);
                flush(enc);
-               rvid_destroy_buffer(&fb);
+               si_vid_destroy_buffer(&fb);
        }
-       rvid_destroy_buffer(&enc->cpb);
+       si_vid_destroy_buffer(&enc->cpb);
        enc->ws->cs_destroy(enc->cs);
        FREE(enc->cpb_array);
        FREE(enc);
@@ -269,7 +271,7 @@ static void rvce_begin_frame(struct pipe_video_codec *encoder,
                enc->pic.quant_b_frames != pic->quant_b_frames;
 
        enc->pic = *pic;
-       get_pic_param(enc, pic);
+       si_get_pic_param(enc, pic);
 
        enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
        enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma);
@@ -282,8 +284,8 @@ static void rvce_begin_frame(struct pipe_video_codec *encoder,
        
        if (!enc->stream_handle) {
                struct rvid_buffer fb;
-               enc->stream_handle = rvid_alloc_stream_handle();
-               rvid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
+               enc->stream_handle = si_vid_alloc_stream_handle();
+               si_vid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
                enc->fb = &fb;
                enc->session(enc);
                enc->create(enc);
@@ -291,7 +293,7 @@ static void rvce_begin_frame(struct pipe_video_codec *encoder,
                enc->feedback(enc);
                flush(enc);
                //dump_feedback(enc, &fb);
-               rvid_destroy_buffer(&fb);
+               si_vid_destroy_buffer(&fb);
                need_rate_control = false;
        }
 
@@ -312,7 +314,7 @@ static void rvce_encode_bitstream(struct pipe_video_codec *encoder,
        enc->bs_size = destination->width0;
 
        *fb = enc->fb = CALLOC_STRUCT(rvid_buffer);
-       if (!rvid_create_buffer(enc->screen, enc->fb, 512, PIPE_USAGE_STAGING)) {
+       if (!si_vid_create_buffer(enc->screen, enc->fb, 512, PIPE_USAGE_STAGING)) {
                RVID_ERR("Can't create feedback buffer.\n");
                return;
        }
@@ -350,7 +352,9 @@ static void rvce_get_feedback(struct pipe_video_codec *encoder,
        struct rvid_buffer *fb = feedback;
 
        if (size) {
-               uint32_t *ptr = enc->ws->buffer_map(fb->res->buf, enc->cs, PIPE_TRANSFER_READ_WRITE);
+               uint32_t *ptr = enc->ws->buffer_map(
+                       fb->res->buf, enc->cs,
+                       PIPE_TRANSFER_READ_WRITE | RADEON_TRANSFER_TEMPORARY);
 
                if (ptr[1]) {
                        *size = ptr[4] - ptr[9];
@@ -361,7 +365,7 @@ static void rvce_get_feedback(struct pipe_video_codec *encoder,
                enc->ws->buffer_unmap(fb->res->buf);
        }
        //dump_feedback(enc, fb);
-       rvid_destroy_buffer(fb);
+       si_vid_destroy_buffer(fb);
        FREE(fb);
 }
 
@@ -381,23 +385,23 @@ static void rvce_cs_flush(void *ctx, unsigned flags,
        // just ignored
 }
 
-struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
-                                            const struct pipe_video_codec *templ,
-                                            struct radeon_winsys* ws,
-                                            rvce_get_buffer get_buffer)
+struct pipe_video_codec *si_vce_create_encoder(struct pipe_context *context,
+                                              const struct pipe_video_codec *templ,
+                                              struct radeon_winsys* ws,
+                                              rvce_get_buffer get_buffer)
 {
-       struct r600_common_screen *rscreen = (struct r600_common_screen *)context->screen;
-       struct r600_common_context *rctx = (struct r600_common_context*)context;
+       struct si_screen *sscreen = (struct si_screen *)context->screen;
+       struct si_context *sctx = (struct si_context*)context;
        struct rvce_encoder *enc;
        struct pipe_video_buffer *tmp_buf, templat = {};
        struct radeon_surf *tmp_surf;
        unsigned cpb_size;
 
-       if (!rscreen->info.vce_fw_version) {
+       if (!sscreen->info.vce_fw_version) {
                RVID_ERR("Kernel doesn't supports VCE!\n");
                return NULL;
 
-       } else if (!rvce_is_fw_version_supported(rscreen)) {
+       } else if (!si_vce_is_fw_version_supported(sscreen)) {
                RVID_ERR("Unsupported VCE fw version loaded!\n");
                return NULL;
        }
@@ -406,20 +410,21 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
        if (!enc)
                return NULL;
 
-       if (rscreen->info.drm_major == 3)
+       if (sscreen->info.is_amdgpu)
                enc->use_vm = true;
-       if ((rscreen->info.drm_major == 2 && rscreen->info.drm_minor >= 42) ||
-            rscreen->info.drm_major == 3)
+       if ((!sscreen->info.is_amdgpu && sscreen->info.drm_minor >= 42) ||
+            sscreen->info.is_amdgpu)
                enc->use_vui = true;
-       if (rscreen->info.family >= CHIP_TONGA &&
-           rscreen->info.family != CHIP_STONEY &&
-           rscreen->info.family != CHIP_POLARIS11 &&
-           rscreen->info.family != CHIP_POLARIS12)
+       if (sscreen->info.family >= CHIP_TONGA &&
+           sscreen->info.family != CHIP_STONEY &&
+           sscreen->info.family != CHIP_POLARIS11 &&
+           sscreen->info.family != CHIP_POLARIS12 &&
+           sscreen->info.family != CHIP_VEGAM)
                enc->dual_pipe = true;
        /* TODO enable B frame with dual instance */
-       if ((rscreen->info.family >= CHIP_TONGA) &&
+       if ((sscreen->info.family >= CHIP_TONGA) &&
                (templ->max_references == 1) &&
-               (rscreen->info.vce_harvest_config == 0))
+               (sscreen->info.vce_harvest_config == 0))
                enc->dual_inst = true;
 
        enc->base = *templ;
@@ -435,7 +440,7 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
 
        enc->screen = context->screen;
        enc->ws = ws;
-       enc->cs = ws->cs_create(rctx->ctx, RING_VCE, rvce_cs_flush, enc);
+       enc->cs = ws->cs_create(sctx->ctx, RING_VCE, rvce_cs_flush, enc, false);
        if (!enc->cs) {
                RVID_ERR("Can't get command submission context.\n");
                goto error;
@@ -456,15 +461,21 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
                goto error;
 
        get_buffer(((struct vl_video_buffer *)tmp_buf)->resources[0], NULL, &tmp_surf);
-       cpb_size = align(tmp_surf->u.legacy.level[0].nblk_x * tmp_surf->bpe, 128);
-       cpb_size = cpb_size * align(tmp_surf->u.legacy.level[0].nblk_y, 32);
+
+       cpb_size = (sscreen->info.chip_class < GFX9) ?
+               align(tmp_surf->u.legacy.level[0].nblk_x * tmp_surf->bpe, 128) *
+               align(tmp_surf->u.legacy.level[0].nblk_y, 32) :
+
+               align(tmp_surf->u.gfx9.surf_pitch * tmp_surf->bpe, 256) *
+               align(tmp_surf->u.gfx9.surf_height, 32);
+
        cpb_size = cpb_size * 3 / 2;
        cpb_size = cpb_size * enc->cpb_num;
        if (enc->dual_pipe)
                cpb_size +=  RVCE_MAX_AUX_BUFFER_NUM *
                        RVCE_MAX_BITSTREAM_OUTPUT_ROW_SIZE * 2;
        tmp_buf->destroy(tmp_buf);
-       if (!rvid_create_buffer(enc->screen, &enc->cpb, cpb_size, PIPE_USAGE_DEFAULT)) {
+       if (!si_vid_create_buffer(enc->screen, &enc->cpb, cpb_size, PIPE_USAGE_DEFAULT)) {
                RVID_ERR("Can't create CPB buffer.\n");
                goto error;
        }
@@ -475,29 +486,33 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
 
        reset_cpb(enc);
 
-       switch (rscreen->info.vce_fw_version) {
+       switch (sscreen->info.vce_fw_version) {
        case FW_40_2_2:
-               radeon_vce_40_2_2_init(enc);
-               get_pic_param = radeon_vce_40_2_2_get_param;
+               si_vce_40_2_2_init(enc);
+               si_get_pic_param = si_vce_40_2_2_get_param;
                break;
 
        case FW_50_0_1:
        case FW_50_1_2:
        case FW_50_10_2:
        case FW_50_17_3:
-               radeon_vce_50_init(enc);
-               get_pic_param = radeon_vce_50_get_param;
+               si_vce_50_init(enc);
+               si_get_pic_param = si_vce_50_get_param;
                break;
 
        case FW_52_0_3:
        case FW_52_4_3:
        case FW_52_8_3:
-               radeon_vce_52_init(enc);
-               get_pic_param = radeon_vce_52_get_param;
+               si_vce_52_init(enc);
+               si_get_pic_param = si_vce_52_get_param;
                break;
 
        default:
-               goto error;
+               if ((sscreen->info.vce_fw_version & (0xff << 24)) >= FW_53) {
+                       si_vce_52_init(enc);
+                       si_get_pic_param = si_vce_52_get_param;
+               } else
+                       goto error;
        }
 
        return &enc->base;
@@ -506,7 +521,7 @@ error:
        if (enc->cs)
                enc->ws->cs_destroy(enc->cs);
 
-       rvid_destroy_buffer(&enc->cpb);
+       si_vid_destroy_buffer(&enc->cpb);
 
        FREE(enc->cpb_array);
        FREE(enc);
@@ -516,9 +531,9 @@ error:
 /**
  * check if kernel has the right fw version loaded
  */
-bool rvce_is_fw_version_supported(struct r600_common_screen *rscreen)
+bool si_vce_is_fw_version_supported(struct si_screen *sscreen)
 {
-       switch (rscreen->info.vce_fw_version) {
+       switch (sscreen->info.vce_fw_version) {
        case FW_40_2_2:
        case FW_50_0_1:
        case FW_50_1_2:
@@ -529,21 +544,24 @@ bool rvce_is_fw_version_supported(struct r600_common_screen *rscreen)
        case FW_52_8_3:
                return true;
        default:
-               return false;
+               if ((sscreen->info.vce_fw_version & (0xff << 24)) >= FW_53)
+                       return true;
+               else
+                       return false;
        }
 }
 
 /**
  * Add the buffer as relocation to the current command submission
  */
-void rvce_add_buffer(struct rvce_encoder *enc, struct pb_buffer *buf,
-                     enum radeon_bo_usage usage, enum radeon_bo_domain domain,
-                     signed offset)
+void si_vce_add_buffer(struct rvce_encoder *enc, struct pb_buffer *buf,
+                      enum radeon_bo_usage usage, enum radeon_bo_domain domain,
+                      signed offset)
 {
        int reloc_idx;
 
        reloc_idx = enc->ws->cs_add_buffer(enc->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED,
-                                          domain, RADEON_PRIO_VCE);
+                                          domain, 0);
        if (enc->use_vm) {
                uint64_t addr;
                addr = enc->ws->buffer_get_virtual_address(buf);