<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>+BoYuan</p>
    <p>I am not sure if we need add ENCRYPT check when allocate surface
      if is supposed to be TMZ protected . For example:<br>
    </p>
    <p>VAStatus<br>
      <b>vlVaHandleSurfaceAllocate</b>(vlVaDriver *drv, vlVaSurface
      *surface,<br>
                                struct pipe_video_buffer *templat,<br>
                                const uint64_t *modifiers,<br>
                                unsigned int modifiers_count)<br>
      {<br>
         struct pipe_surface **surfaces;<br>
         unsigned i;<br>
    </p>
    <p><b>    if (is_encrypted && </b><b>templat) {/*</b><b>is_encrypted
        is pseudocode*/<br>
      </b></p>
    <p><b>        </b><b>templat->bind |= PIPE_BIND_PROTECTED;</b><b><br>
      </b></p>
    <p><b>    }</b><br>
         if (modifiers_count > 0) {<br>
            if (!drv->pipe->create_video_buffer_with_modifiers)<br>
               return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;<br>
            surface->buffer =<br>
              
      drv->pipe->create_video_buffer_with_modifiers(drv->pipe,
      templat,<br>
                                                             modifiers,<br>
                                                            
      modifiers_count);<br>
         } else {<br>
            surface->buffer =
      drv->pipe->create_video_buffer(drv->pipe, templat);<br>
         }<br>
    </p>
    <p>Best Regards!</p>
    <p>James<br>
    </p>
    <div class="moz-cite-prefix">On 2022-03-08 11:30 a.m., Leo Liu
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:95e6f48e-fdc7-e535-1803-080a8d189d33@amd.com">
      <br>
      On 2022-03-08 11:18, Leo Liu wrote:
      <br>
      <blockquote type="cite">
        <br>
        On 2022-03-08 04:16, Christian König wrote:
        <br>
        <blockquote type="cite">Am 08.03.22 um 09:06 schrieb Lang Yu:
          <br>
          <blockquote type="cite">On 03/08/ , Christian König wrote:
            <br>
            <blockquote type="cite">Am 08.03.22 um 08:33 schrieb Lang
              Yu:
              <br>
              <blockquote type="cite">On 03/08/ , Christian König wrote:
                <br>
                <blockquote type="cite">Am 08.03.22 um 04:39 schrieb
                  Lang Yu:
                  <br>
                  <blockquote type="cite">It is a hardware issue that
                    VCN can't handle a GTT
                    <br>
                    backing stored TMZ buffer on Raven.
                    <br>
                    <br>
                    Move such a TMZ buffer to VRAM domain before command
                    <br>
                    submission.
                    <br>
                    <br>
                    v2:
                    <br>
                        - Use patch_cs_in_place callback.
                    <br>
                    <br>
                    Suggested-by: Christian König
                    <a class="moz-txt-link-rfc2396E" href="mailto:christian.koenig@amd.com"><christian.koenig@amd.com></a>
                    <br>
                    Signed-off-by: Lang Yu <a class="moz-txt-link-rfc2396E" href="mailto:Lang.Yu@amd.com"><Lang.Yu@amd.com></a>
                    <br>
                    ---
                    <br>
                        drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
                    +++++++++++++++++++++++++++
                    <br>
                        1 file changed, 68 insertions(+)
                    <br>
                    <br>
                    diff --git a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
                    b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
                    <br>
                    index 7bbb9ba6b80b..810932abd3af 100644
                    <br>
                    --- a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
                    <br>
                    +++ b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
                    <br>
                    @@ -24,6 +24,7 @@
                    <br>
                        #include <linux/firmware.h>
                    <br>
                        #include "amdgpu.h"
                    <br>
                    +#include "amdgpu_cs.h"
                    <br>
                        #include "amdgpu_vcn.h"
                    <br>
                        #include "amdgpu_pm.h"
                    <br>
                        #include "soc15.h"
                    <br>
                    @@ -1905,6 +1906,72 @@ static const struct
                    amd_ip_funcs vcn_v1_0_ip_funcs = {
                    <br>
                            .set_powergating_state =
                    vcn_v1_0_set_powergating_state,
                    <br>
                        };
                    <br>
                    +/**
                    <br>
                    + * It is a hardware issue that Raven VCN can't
                    handle a GTT TMZ buffer.
                    <br>
                    + * Move such a GTT TMZ buffer to VRAM domain before
                    command submission.
                    <br>
                    + */
                    <br>
                    +static int vcn_v1_0_validate_bo(struct
                    amdgpu_cs_parser *parser,
                    <br>
                    +                struct amdgpu_job *job,
                    <br>
                    +                uint64_t addr)
                    <br>
                    +{
                    <br>
                    +    struct ttm_operation_ctx ctx = { false, false
                    };
                    <br>
                    +    struct amdgpu_fpriv *fpriv =
                    parser->filp->driver_priv;
                    <br>
                    +    struct amdgpu_vm *vm = &fpriv->vm;
                    <br>
                    +    struct amdgpu_bo_va_mapping *mapping;
                    <br>
                    +    struct amdgpu_bo *bo;
                    <br>
                    +    int r;
                    <br>
                    +
                    <br>
                    +    addr &= AMDGPU_GMC_HOLE_MASK;
                    <br>
                    +    if (addr & 0x7) {
                    <br>
                    +        DRM_ERROR("VCN messages must be 8 byte
                    aligned!\n");
                    <br>
                    +        return -EINVAL;
                    <br>
                    +    }
                    <br>
                    +
                    <br>
                    +    mapping = amdgpu_vm_bo_lookup_mapping(vm,
                    addr/AMDGPU_GPU_PAGE_SIZE);
                    <br>
                    +    if (!mapping || !mapping->bo_va ||
                    !mapping->bo_va->base.bo)
                    <br>
                    +        return -EINVAL;
                    <br>
                    +
                    <br>
                    +    bo = mapping->bo_va->base.bo;
                    <br>
                    +    if (!(bo->flags &
                    AMDGPU_GEM_CREATE_ENCRYPTED))
                    <br>
                    +        return 0;
                    <br>
                    +
                    <br>
                    +    amdgpu_bo_placement_from_domain(bo,
                    AMDGPU_GEM_DOMAIN_VRAM);
                    <br>
                    +    r = ttm_bo_validate(&bo->tbo,
                    &bo->placement, &ctx);
                    <br>
                    +    if (r) {
                    <br>
                    +        DRM_ERROR("Failed validating the VCN
                    message BO (%d)!\n", r);
                    <br>
                    +        return r;
                    <br>
                    +    }
                    <br>
                  </blockquote>
                  Well, exactly that won't work.
                  <br>
                  <br>
                  The message structure isn't TMZ protected because
                  otherwise the driver won't
                  <br>
                  be able to stitch it together.
                  <br>
                  <br>
                  What is TMZ protected are the surfaces the message
                  structure is pointing to.
                  <br>
                  So what you would need to do is to completely parse
                  the structure and then
                  <br>
                  move on the relevant buffers into VRAM.
                  <br>
                  <br>
                  Leo or James, can you help with that?
                  <br>
                </blockquote>
                  From my observations, when decoding secure contents,
                register
                <br>
                GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ
                buffer address.
                <br>
                And this way works when allocating TMZ buffers in GTT
                domain.
                <br>
              </blockquote>
              As far as I remember that's only the case for the
              decoding, encoding works
              <br>
              by putting the addresses into the message buffer.
              <br>
              <br>
              But could be that decoding is sufficient, Leo and James
              need to comment on
              <br>
              this.
              <br>
            </blockquote>
            It seems that only decode needs TMZ buffers. Only observe
            si_vid_create_tmz_buffer()
            <br>
            was called in rvcn_dec_message_decode() in
            src/gallium/drivers/radeon/radeon_vcn_dec.c.
            <br>
          </blockquote>
          <br>
          Mhm, good point. Let's wait for Leo and James to wake up, when
          we don't need encode support than that would makes things much
          easier.
          <br>
        </blockquote>
        <br>
        For secure playback, the buffer required in TMZ are dpb, dt and
        ctx, for the rest esp. those for CPU access don't need that E.g.
        msg buffer, and bitstream buffer.
        <br>
        <br>
        From radeon_vcn_dec.c, you can see the buffer for dpb and ctx,
        and dt buffer frontend/va/surface is set to PIPE_BIND_PROTECTED.
        <br>
        <br>
        <br>
        Regards,
        <br>
        <br>
        Leo
        <br>
        <br>
      </blockquote>
      For VCN1, due to performance reason, the msg and fb buffer was
      allocated into VRAM instead of GTT(for other HW), but those are
      not TMZ in order to have CPU access.
      <br>
      <br>
      <br>
      Regards,
      <br>
      <br>
      Leo
      <br>
      <br>
      <br>
      <br>
      <blockquote type="cite">
        <br>
        <br>
        <blockquote type="cite">
          <br>
          Regards,
          <br>
          Christian.
          <br>
          <br>
          <blockquote type="cite">
            <br>
            Regards,
            <br>
            Lang
            <br>
            <br>
            <blockquote type="cite">Regards,
              <br>
              Christian.
              <br>
              <br>
              <blockquote type="cite">Regards,
                <br>
                Lang
                <br>
                <br>
                <blockquote type="cite">Regards,
                  <br>
                  Christian.
                  <br>
                  <br>
                  <blockquote type="cite">+
                    <br>
                    +    return r;
                    <br>
                    +}
                    <br>
                    +
                    <br>
                    +static int vcn_v1_0_ring_patch_cs_in_place(struct
                    amdgpu_cs_parser *p,
                    <br>
                    +                       struct amdgpu_job *job,
                    <br>
                    +                       struct amdgpu_ib *ib)
                    <br>
                    +{
                    <br>
                    +    uint32_t msg_lo = 0, msg_hi = 0;
                    <br>
                    +    int i, r;
                    <br>
                    +
                    <br>
                    +    for (i = 0; i < ib->length_dw; i += 2) {
                    <br>
                    +        uint32_t reg = amdgpu_ib_get_value(ib, i);
                    <br>
                    +        uint32_t val = amdgpu_ib_get_value(ib, i +
                    1);
                    <br>
                    +
                    <br>
                    +        if (reg ==
                    PACKET0(p->adev->vcn.internal.data0, 0)) {
                    <br>
                    +            msg_lo = val;
                    <br>
                    +        } else if (reg ==
                    PACKET0(p->adev->vcn.internal.data1, 0)) {
                    <br>
                    +            msg_hi = val;
                    <br>
                    +        } else if (reg ==
                    PACKET0(p->adev->vcn.internal.cmd, 0)) {
                    <br>
                    +            r = vcn_v1_0_validate_bo(p, job,
                    <br>
                    +                         ((u64)msg_hi) << 32
                    | msg_lo);
                    <br>
                    +            if (r)
                    <br>
                    +                return r;
                    <br>
                    +        }
                    <br>
                    +    }
                    <br>
                    +
                    <br>
                    +    return 0;
                    <br>
                    +}
                    <br>
                    +
                    <br>
                    +
                    <br>
                        static const struct amdgpu_ring_funcs
                    vcn_v1_0_dec_ring_vm_funcs = {
                    <br>
                            .type = AMDGPU_RING_TYPE_VCN_DEC,
                    <br>
                            .align_mask = 0xf,
                    <br>
                    @@ -1914,6 +1981,7 @@ static const struct
                    amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
                    <br>
                            .get_rptr = vcn_v1_0_dec_ring_get_rptr,
                    <br>
                            .get_wptr = vcn_v1_0_dec_ring_get_wptr,
                    <br>
                            .set_wptr = vcn_v1_0_dec_ring_set_wptr,
                    <br>
                    +    .patch_cs_in_place =
                    vcn_v1_0_ring_patch_cs_in_place,
                    <br>
                            .emit_frame_size =
                    <br>
                                6 + 6 + /* hdp invalidate / flush */
                    <br>
                                SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                    <br>
                  </blockquote>
                </blockquote>
              </blockquote>
            </blockquote>
          </blockquote>
          <br>
        </blockquote>
      </blockquote>
    </blockquote>
  </body>
</html>