Skip to contentSkip to conversation

drm/amdgpu: add workarounds for Raven VCN TMZ issue

PATCH Superseded Lang Yu 6 participants 16 messages Mar 8, 2022 – Mar 14, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lang Yu

author
#0 lore raw

PATCH v2 1/1 drm/amdgpu: add workarounds for Raven VCN TMZ issue

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
- Use patch_cs_in_place callback.

---
 drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68 +++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +68 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
 #include <linux/firmware.h>
 
 #include "amdgpu.h"
+#include "amdgpu_cs.h"
 #include "amdgpu_vcn.h"
 #include "amdgpu_pm.h"
 #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
 	.set_powergating_state = vcn_v1_0_set_powergating_state,
 };
 
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+				struct amdgpu_job *job,
+				uint64_t addr)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct amdgpu_bo_va_mapping *mapping;
+	struct amdgpu_bo *bo;
+	int r;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	if (addr & 0x7) {
+		DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+		return -EINVAL;
+	}
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr/AMDGPU_GPU_PAGE_SIZE);
+	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
+		return -EINVAL;
+
+	bo = mapping->bo_va->base.bo;
+	if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+		return 0;
+
+	amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+	if (r) {
+		DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+		return r;
+	}
+
+	return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
+					   struct amdgpu_job *job,
+					   struct amdgpu_ib *ib)
+{
+	uint32_t msg_lo = 0, msg_hi = 0;
+	int i, r;
+
+	for (i = 0; i < ib->length_dw; i += 2) {
+		uint32_t reg = amdgpu_ib_get_value(ib, i);
+		uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+		if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+			msg_lo = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.data1, 0)) {
+			msg_hi = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+			r = vcn_v1_0_validate_bo(p, job,
+					     ((u64)msg_hi) << 32 | msg_lo);
+			if (r)
+				return r;
+		}
+	}
+
+	return 0;
+}
+
+
 static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
 	.type = AMDGPU_RING_TYPE_VCN_DEC,
 	.align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
 	.get_rptr = vcn_v1_0_dec_ring_get_rptr,
 	.get_wptr = vcn_v1_0_dec_ring_get_wptr,
 	.set_wptr = vcn_v1_0_dec_ring_set_wptr,
+	.patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
 	.emit_frame_size =
 		6 + 6 + /* hdp invalidate / flush */
 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
Signature · 1 line
2.25.1
#1 lore raw

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 56 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
- Use patch_cs_in_place callback.

---
  drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68 +++++++++++++++++++++++++++
  1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +26 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
  #include <linux/firmware.h>
 
  #include "amdgpu.h"
+#include "amdgpu_cs.h"
  #include "amdgpu_vcn.h"
  #include "amdgpu_pm.h"
  #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
  	.set_powergating_state = vcn_v1_0_set_powergating_state,
  };
 
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+				struct amdgpu_job *job,
+				uint64_t addr)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct amdgpu_bo_va_mapping *mapping;
+	struct amdgpu_bo *bo;
+	int r;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	if (addr & 0x7) {
+		DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+		return -EINVAL;
+	}
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr/AMDGPU_GPU_PAGE_SIZE);
+	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
+		return -EINVAL;
+

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c static const struct amd_ip_funcs vcn_v1_0_ip_funcs = { · line 1943

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +10 -0
+	bo = mapping->bo_va->base.bo;
+	if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+		return 0;
+
+	amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+	if (r) {
+		DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+		return r;
+	}

Well, exactly that won't work.

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

Regards,
Christian.

Quoted 42 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +32 -0
+
+	return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
+					   struct amdgpu_job *job,
+					   struct amdgpu_ib *ib)
+{
+	uint32_t msg_lo = 0, msg_hi = 0;
+	int i, r;
+
+	for (i = 0; i < ib->length_dw; i += 2) {
+		uint32_t reg = amdgpu_ib_get_value(ib, i);
+		uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+		if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+			msg_lo = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.data1, 0)) {
+			msg_hi = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+			r = vcn_v1_0_validate_bo(p, job,
+					     ((u64)msg_hi) << 32 | msg_lo);
+			if (r)
+				return r;
+		}
+	}
+
+	return 0;
+}
+
+
  static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
  	.type = AMDGPU_RING_TYPE_VCN_DEC,
  	.align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
  	.get_rptr = vcn_v1_0_dec_ring_get_rptr,
  	.get_wptr = vcn_v1_0_dec_ring_get_wptr,
  	.set_wptr = vcn_v1_0_dec_ring_set_wptr,
+	.patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
  	.emit_frame_size =
  		6 + 6 + /* hdp invalidate / flush */
  		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
author
#2 lore raw
Quoted 70 lines · Christian König

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 54 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
- Use patch_cs_in_place callback.

---
  drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68 +++++++++++++++++++++++++++
  1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +26 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
  #include <linux/firmware.h>
  #include "amdgpu.h"
+#include "amdgpu_cs.h"
  #include "amdgpu_vcn.h"
  #include "amdgpu_pm.h"
  #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
  	.set_powergating_state = vcn_v1_0_set_powergating_state,
  };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+				struct amdgpu_job *job,
+				uint64_t addr)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct amdgpu_bo_va_mapping *mapping;
+	struct amdgpu_bo *bo;
+	int r;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	if (addr & 0x7) {
+		DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+		return -EINVAL;
+	}
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr/AMDGPU_GPU_PAGE_SIZE);
+	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
+		return -EINVAL;
+

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c static const struct amd_ip_funcs vcn_v1_0_ip_funcs = { · line 1942

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +10 -0
+	bo = mapping->bo_va->base.bo;
+	if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+		return 0;
+
+	amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+	if (r) {
+		DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+		return r;
+	}

Well, exactly that won't work.

Quoted 8 lines

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

Regards,
Lang

Quoted 46 lines

Regards,
Christian.

Quoted 42 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +32 -0
+
+	return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
+					   struct amdgpu_job *job,
+					   struct amdgpu_ib *ib)
+{
+	uint32_t msg_lo = 0, msg_hi = 0;
+	int i, r;
+
+	for (i = 0; i < ib->length_dw; i += 2) {
+		uint32_t reg = amdgpu_ib_get_value(ib, i);
+		uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+		if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+			msg_lo = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.data1, 0)) {
+			msg_hi = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+			r = vcn_v1_0_validate_bo(p, job,
+					     ((u64)msg_hi) << 32 | msg_lo);
+			if (r)
+				return r;
+		}
+	}
+
+	return 0;
+}
+
+
  static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
  	.type = AMDGPU_RING_TYPE_VCN_DEC,
  	.align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
  	.get_rptr = vcn_v1_0_dec_ring_get_rptr,
  	.get_wptr = vcn_v1_0_dec_ring_get_wptr,
  	.set_wptr = vcn_v1_0_dec_ring_set_wptr,
+	.patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
  	.emit_frame_size =
  		6 + 6 + /* hdp invalidate / flush */
  		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
#3 lore raw

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 72 lines · Christian König
Quoted 71 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 54 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
- Use patch_cs_in_place callback.

---
   drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68 +++++++++++++++++++++++++++
   1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +26 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
   #include <linux/firmware.h>
   #include "amdgpu.h"
+#include "amdgpu_cs.h"
   #include "amdgpu_vcn.h"
   #include "amdgpu_pm.h"
   #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
   	.set_powergating_state = vcn_v1_0_set_powergating_state,
   };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+				struct amdgpu_job *job,
+				uint64_t addr)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct amdgpu_bo_va_mapping *mapping;
+	struct amdgpu_bo *bo;
+	int r;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	if (addr & 0x7) {
+		DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+		return -EINVAL;
+	}
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr/AMDGPU_GPU_PAGE_SIZE);
+	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
+		return -EINVAL;
+

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c static const struct amd_ip_funcs vcn_v1_0_ip_funcs = { · line 1942

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +10 -0
+	bo = mapping->bo_va->base.bo;
+	if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+		return 0;
+
+	amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+	if (r) {
+		DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+		return r;
+	}

Well, exactly that won't work.

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

Quoted 8 lines
Quoted 5 lines

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

From my observations, when decoding secure contents, register
GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on this.

Regards,
Christian.

Quoted 49 lines

Regards,
Lang

Quoted 45 lines

Regards,
Christian.

Quoted 42 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +32 -0
+
+	return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
+					   struct amdgpu_job *job,
+					   struct amdgpu_ib *ib)
+{
+	uint32_t msg_lo = 0, msg_hi = 0;
+	int i, r;
+
+	for (i = 0; i < ib->length_dw; i += 2) {
+		uint32_t reg = amdgpu_ib_get_value(ib, i);
+		uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+		if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+			msg_lo = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.data1, 0)) {
+			msg_hi = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+			r = vcn_v1_0_validate_bo(p, job,
+					     ((u64)msg_hi) << 32 | msg_lo);
+			if (r)
+				return r;
+		}
+	}
+
+	return 0;
+}
+
+
   static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
   	.type = AMDGPU_RING_TYPE_VCN_DEC,
   	.align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
   	.get_rptr = vcn_v1_0_dec_ring_get_rptr,
   	.get_wptr = vcn_v1_0_dec_ring_get_wptr,
   	.set_wptr = vcn_v1_0_dec_ring_set_wptr,
+	.patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
   	.emit_frame_size =
   		6 + 6 + /* hdp invalidate / flush */
   		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
author
#4 lore raw
Quoted 82 lines · Christian König

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 72 lines · Christian König
Quoted 71 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 54 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
- Use patch_cs_in_place callback.

---
   drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68 +++++++++++++++++++++++++++
   1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +26 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
   #include <linux/firmware.h>
   #include "amdgpu.h"
+#include "amdgpu_cs.h"
   #include "amdgpu_vcn.h"
   #include "amdgpu_pm.h"
   #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
   	.set_powergating_state = vcn_v1_0_set_powergating_state,
   };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+				struct amdgpu_job *job,
+				uint64_t addr)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct amdgpu_bo_va_mapping *mapping;
+	struct amdgpu_bo *bo;
+	int r;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	if (addr & 0x7) {
+		DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+		return -EINVAL;
+	}
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr/AMDGPU_GPU_PAGE_SIZE);
+	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
+		return -EINVAL;
+

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c static const struct amd_ip_funcs vcn_v1_0_ip_funcs = { · line 1942

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +10 -0
+	bo = mapping->bo_va->base.bo;
+	if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+		return 0;
+
+	amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+	if (r) {
+		DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+		return r;
+	}

Well, exactly that won't work.

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

Quoted 8 lines
Quoted 5 lines

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

From my observations, when decoding secure contents, register
GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

Quoted 5 lines

As far as I remember that's only the case for the decoding, encoding works by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on this.

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer() was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

Regards,
Lang

Quoted 53 lines

Regards,
Christian.

Quoted 49 lines

Regards,
Lang

Quoted 45 lines

Regards,
Christian.

Quoted 42 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +32 -0
+
+	return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
+					   struct amdgpu_job *job,
+					   struct amdgpu_ib *ib)
+{
+	uint32_t msg_lo = 0, msg_hi = 0;
+	int i, r;
+
+	for (i = 0; i < ib->length_dw; i += 2) {
+		uint32_t reg = amdgpu_ib_get_value(ib, i);
+		uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+		if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+			msg_lo = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.data1, 0)) {
+			msg_hi = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+			r = vcn_v1_0_validate_bo(p, job,
+					     ((u64)msg_hi) << 32 | msg_lo);
+			if (r)
+				return r;
+		}
+	}
+
+	return 0;
+}
+
+
   static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
   	.type = AMDGPU_RING_TYPE_VCN_DEC,
   	.align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
   	.get_rptr = vcn_v1_0_dec_ring_get_rptr,
   	.get_wptr = vcn_v1_0_dec_ring_get_wptr,
   	.set_wptr = vcn_v1_0_dec_ring_set_wptr,
+	.patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
   	.emit_frame_size =
   		6 + 6 + /* hdp invalidate / flush */
   		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
maintainer
#5 lore raw

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 81 lines · Christian König
Quoted 80 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 79 lines · Christian König
Quoted 67 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 54 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
- Use patch_cs_in_place callback.

---
    drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68 +++++++++++++++++++++++++++
    1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +26 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
    #include <linux/firmware.h>
    #include "amdgpu.h"
+#include "amdgpu_cs.h"
    #include "amdgpu_vcn.h"
    #include "amdgpu_pm.h"
    #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
    	.set_powergating_state = vcn_v1_0_set_powergating_state,
    };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+				struct amdgpu_job *job,
+				uint64_t addr)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+	struct amdgpu_vm *vm = &fpriv->vm;
+	struct amdgpu_bo_va_mapping *mapping;
+	struct amdgpu_bo *bo;
+	int r;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	if (addr & 0x7) {
+		DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+		return -EINVAL;
+	}
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr/AMDGPU_GPU_PAGE_SIZE);
+	if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
+		return -EINVAL;
+

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c static const struct amd_ip_funcs vcn_v1_0_ip_funcs = { · line 1942

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +10 -0
+	bo = mapping->bo_va->base.bo;
+	if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+		return 0;
+
+	amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+	if (r) {
+		DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+		return r;
+	}

Well, exactly that won't work.

Quoted 8 lines

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

From my observations, when decoding secure contents, register
GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

Quoted 7 lines
Quoted 5 lines

As far as I remember that's only the case for the decoding, encoding works by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on this.

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer() was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

Regards,
Christian.

Quoted 55 lines

Regards,
Lang

Quoted 51 lines

Regards,
Christian.

Quoted 48 lines

Regards,
Lang

Quoted 45 lines

Regards,
Christian.

Quoted 42 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +32 -0
+
+	return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
+					   struct amdgpu_job *job,
+					   struct amdgpu_ib *ib)
+{
+	uint32_t msg_lo = 0, msg_hi = 0;
+	int i, r;
+
+	for (i = 0; i < ib->length_dw; i += 2) {
+		uint32_t reg = amdgpu_ib_get_value(ib, i);
+		uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+		if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+			msg_lo = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.data1, 0)) {
+			msg_hi = val;
+		} else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+			r = vcn_v1_0_validate_bo(p, job,
+					     ((u64)msg_hi) << 32 | msg_lo);
+			if (r)
+				return r;
+		}
+	}
+
+	return 0;
+}
+
+
    static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
    	.type = AMDGPU_RING_TYPE_VCN_DEC,
    	.align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
    	.get_rptr = vcn_v1_0_dec_ring_get_rptr,
    	.get_wptr = vcn_v1_0_dec_ring_get_wptr,
    	.set_wptr = vcn_v1_0_dec_ring_set_wptr,
+	.patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
    	.emit_frame_size =
    		6 + 6 + /* hdp invalidate / flush */
    		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
#6 lore raw
Quoted 86 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 84 lines · Christian König
Quoted 83 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 71 lines · Christian König
Quoted 70 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 54 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
- Use patch_cs_in_place callback.

---
    drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68 +++++++++++++++++++++++++++
    1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +26 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
    #include <linux/firmware.h>
    #include "amdgpu.h"
+#include "amdgpu_cs.h"
    #include "amdgpu_vcn.h"
    #include "amdgpu_pm.h"
    #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
          .set_powergating_state = vcn_v1_0_set_powergating_state,
    };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                         struct amdgpu_job *job,
+                         uint64_t addr)
+{
+ struct ttm_operation_ctx ctx = { false, false };
+ struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+ struct amdgpu_vm *vm = &fpriv->vm;
+ struct amdgpu_bo_va_mapping *mapping;
+ struct amdgpu_bo *bo;
+ int r;
+
+ addr &= AMDGPU_GMC_HOLE_MASK;
+ if (addr & 0x7) {
+         DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+         return -EINVAL;
+ }
+
+ mapping = amdgpu_vm_bo_lookup_mapping(vm, addr/AMDGPU_GPU_PAGE_SIZE);
+ if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
+         return -EINVAL;
+

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c static const struct amd_ip_funcs vcn_v1_0_ip_funcs = { · line 1942

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +10 -0
+ bo = mapping->bo_va->base.bo;
+ if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+         return 0;
+
+ amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+ r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+ if (r) {
+         DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+         return r;
+ }

Well, exactly that won't work.

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

Quoted 8 lines
Quoted 5 lines

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

From my observations, when decoding secure contents, register
GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works by putting the addresses into the message buffer.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

But could be that decoding is sufficient, Leo and James need to comment on this.

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer() was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

If we don't support encrypted encode, we should add a check to prevent it like we do for compute.

Alex

Quoted 60 lines

Regards,
Christian.

Quoted 55 lines

Regards,
Lang

Quoted 51 lines

Regards,
Christian.

Quoted 48 lines

Regards,
Lang

Quoted 45 lines

Regards,
Christian.

Quoted 42 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +32 -0
+
+ return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
+                                    struct amdgpu_job *job,
+                                    struct amdgpu_ib *ib)
+{
+ uint32_t msg_lo = 0, msg_hi = 0;
+ int i, r;
+
+ for (i = 0; i < ib->length_dw; i += 2) {
+         uint32_t reg = amdgpu_ib_get_value(ib, i);
+         uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+         if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+                 msg_lo = val;
+         } else if (reg == PACKET0(p->adev->vcn.internal.data1, 0)) {
+                 msg_hi = val;
+         } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+                 r = vcn_v1_0_validate_bo(p, job,
+                                      ((u64)msg_hi) << 32 | msg_lo);
+                 if (r)
+                         return r;
+         }
+ }
+
+ return 0;
+}
+
+
    static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
          .type = AMDGPU_RING_TYPE_VCN_DEC,
          .align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
          .get_rptr = vcn_v1_0_dec_ring_get_rptr,
          .get_wptr = vcn_v1_0_dec_ring_get_wptr,
          .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+ .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
          .emit_frame_size =
                  6 + 6 + /* hdp invalidate / flush */
                  SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
#7 lore raw
In reply to Christian König
Quoted 91 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 90 lines · Christian König
Quoted 89 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 77 lines · Christian König
Quoted 76 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 67 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
    - Use patch_cs_in_place callback.

---
    drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++

    1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +3 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
     #include <linux/firmware.h>
     #include "amdgpu.h"
+#include "amdgpu_cs.h"
     #include "amdgpu_vcn.h"
     #include "amdgpu_pm.h"
     #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs 
 vcn_v1_0_ip_funcs = {
         .set_powergating_state = vcn_v1_0_set_powergating_state,
     };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ 

buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +18 -0
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm, 

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo) +        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +9 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+    amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+    if (r) {
+        DRM_ERROR("Failed validating the VCN message BO 
Quoted 3 lines

(%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

Quoted 8 lines
Quoted 5 lines

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

  From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works by putting the addresses into the message buffer.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

But could be that decoding is sufficient, Leo and James need to comment on this.

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer() was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

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.

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.

Regards,

Leo

Quoted 63 lines

Regards,
Christian.

Quoted 58 lines

Regards,
Lang

Quoted 54 lines

Regards,
Christian.

Quoted 51 lines

Regards,
Lang

Quoted 48 lines

Regards,
Christian.

Quoted 45 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct 

amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.data1, 

0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) { +            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+

    static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_DEC,
        .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs 
 vcn_v1_0_dec_ring_vm_funcs = {
         .get_rptr = vcn_v1_0_dec_ring_get_rptr,
         .get_wptr = vcn_v1_0_dec_ring_get_wptr,
         .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,

        .emit_frame_size =
            6 + 6 + /* hdp invalidate / flush */
            SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

#8 lore raw
Quoted 100 lines · Leo Liu
Quoted 91 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 90 lines · Christian König
Quoted 89 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 77 lines · Christian König
Quoted 76 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 67 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
    - Use patch_cs_in_place callback.

---
    drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++

    1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +3 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
     #include <linux/firmware.h>
     #include "amdgpu.h"
+#include "amdgpu_cs.h"
     #include "amdgpu_vcn.h"
     #include "amdgpu_pm.h"
     #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs 
 vcn_v1_0_ip_funcs = {
         .set_powergating_state = vcn_v1_0_set_powergating_state,
     };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT TMZ 

buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +18 -0
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm, 

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo) +        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +9 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+    amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+    if (r) {
+        DRM_ERROR("Failed validating the VCN message BO 
Quoted 3 lines

(%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

Quoted 8 lines
Quoted 5 lines

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

  From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works by putting the addresses into the message buffer.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

But could be that decoding is sufficient, Leo and James need to comment on this.

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer() was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

Quoted 9 lines

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.

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.

Regards,

Leo

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.

Regards,

Leo

Quoted 65 lines
Quoted 65 lines

Regards,
Christian.

Quoted 58 lines

Regards,
Lang

Quoted 54 lines

Regards,
Christian.

Quoted 51 lines

Regards,
Lang

Quoted 48 lines

Regards,
Christian.

Quoted 45 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct 

amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.data1, 

0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) { +            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+

    static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_DEC,
        .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs 
 vcn_v1_0_dec_ring_vm_funcs = {
         .get_rptr = vcn_v1_0_dec_ring_get_rptr,
         .get_wptr = vcn_v1_0_dec_ring_get_wptr,
         .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,

        .emit_frame_size =
            6 + 6 + /* hdp invalidate / flush */
            SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

#9 lore raw

+BoYuan

I am not sure if we need add ENCRYPT check when allocate surface if is supposed to be TMZ protected . For example:

VAStatus
*vlVaHandleSurfaceAllocate*(vlVaDriver *drv, vlVaSurface *surface, struct pipe_video_buffer *templat, const uint64_t *modifiers, unsigned int modifiers_count) {
   struct pipe_surface **surfaces;
   unsigned i;

*    if (is_encrypted && **templat) {/***is_encrypted is pseudocode*/ *

***templat->bind |= PIPE_BIND_PROTECTED;**
*

*    }*
   if (modifiers_count > 0) {
      if (!drv->pipe->create_video_buffer_with_modifiers) return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED; surface->buffer = drv->pipe->create_video_buffer_with_modifiers(drv->pipe, templat, modifiers, modifiers_count);
   } else {
      surface->buffer = drv->pipe->create_video_buffer(drv->pipe, templat); }

Best Regards!

James

Quoted 186 lines · Leo Liu
Quoted 100 lines · Leo Liu
Quoted 91 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 90 lines · Christian König
Quoted 89 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 77 lines · Christian König
Quoted 76 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 67 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
    - Use patch_cs_in_place callback.

---
    drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++

    1 file changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +3 -0
index 7bbb9ba6b80b..810932abd3af 100644
@@ -24,6 +24,7 @@
     #include <linux/firmware.h>
     #include "amdgpu.h"
+#include "amdgpu_cs.h"
     #include "amdgpu_vcn.h"
     #include "amdgpu_pm.h"
     #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct amd_ip_funcs 
 vcn_v1_0_ip_funcs = {
         .set_powergating_state = vcn_v1_0_set_powergating_state,
     };
+/**
+ * It is a hardware issue that Raven VCN can't handle a GTT 

TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +18 -0
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm, 

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo) +        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +9 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+    amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+    if (r) {
+        DRM_ERROR("Failed validating the VCN message BO 
Quoted 3 lines

(%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because otherwise the driver won't be able to stitch it together.

Quoted 8 lines
Quoted 5 lines

What is TMZ protected are the surfaces the message structure is pointing to. So what you would need to do is to completely parse the structure and then move on the relevant buffers into VRAM.

Leo or James, can you help with that?

  From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address. And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works by putting the addresses into the message buffer.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

But could be that decoding is sufficient, Leo and James need to comment on this.

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer() was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

Quoted 9 lines

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.

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.

Regards,

Leo

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.

Regards,

Leo

Quoted 66 lines
Quoted 66 lines

Regards,
Christian.

Quoted 59 lines

Regards,
Lang

Quoted 55 lines

Regards,
Christian.

Quoted 52 lines

Regards,
Lang

Quoted 49 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int vcn_v1_0_ring_patch_cs_in_place(struct 

amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.data1, 

0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+

    static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_DEC,
        .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct amdgpu_ring_funcs 
 vcn_v1_0_dec_ring_vm_funcs = {
         .get_rptr = vcn_v1_0_dec_ring_get_rptr,
         .get_wptr = vcn_v1_0_dec_ring_get_wptr,
         .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,

        .emit_frame_size =
            6 + 6 + /* hdp invalidate / flush */
            SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

author
#10 lore raw
In reply to Leo Liu

Ping.

Quoted 203 lines · Leo Liu
Quoted 115 lines · Leo Liu
Quoted 101 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 100 lines · Christian König
Quoted 99 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 86 lines · Christian König
Quoted 85 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 69 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
    - Use patch_cs_in_place callback.

---
    drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++

    1 file changed, 68 insertions(+)

diff --git
a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
index 7bbb9ba6b80b..810932abd3af 100644

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +3 -0
@@ -24,6 +24,7 @@
     #include <linux/firmware.h>
     #include "amdgpu.h"
+#include "amdgpu_cs.h"
     #include "amdgpu_vcn.h"
     #include "amdgpu_pm.h"
     #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct
 amd_ip_funcs vcn_v1_0_ip_funcs = {
         .set_powergating_state = vcn_v1_0_set_powergating_state,
     };
+/**
+ * It is a hardware issue that Raven VCN can't

handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain
before command submission.
+ */

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +18 -0
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm,

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo) +        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +9 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+    amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+    if (r) {
+        DRM_ERROR("Failed validating the VCN
Quoted 3 lines

message BO (%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because
otherwise the driver won't
be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to.
So what you would need to do is to completely parse the structure and then
move on the relevant buffers into VRAM.

Quoted 5 lines
Quoted 1 line

Leo or James, can you help with that?

  From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address.
And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works
by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on
this.

Quoted 7 lines
Quoted 4 lines

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer()
was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

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.

Quoted 8 lines

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.

Regards,

Leo

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.

Regards,

Leo

Quoted 67 lines
Quoted 67 lines

Regards,
Christian.

Quoted 60 lines

Regards,
Lang

Quoted 56 lines

Regards,
Christian.

Quoted 53 lines

Regards,
Lang

Quoted 50 lines

Regards,
Christian.

Quoted 47 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int

vcn_v1_0_ring_patch_cs_in_place(struct
amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg ==

PACKET0(p->adev->vcn.internal.data1, 0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) { +            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+

    static const struct amdgpu_ring_funcs
vcn_v1_0_dec_ring_vm_funcs = {
        .type = AMDGPU_RING_TYPE_VCN_DEC,
        .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct
 amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
         .get_rptr = vcn_v1_0_dec_ring_get_rptr,
         .get_wptr = vcn_v1_0_dec_ring_get_wptr,
         .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,

        .emit_frame_size =
            6 + 6 + /* hdp invalidate / flush */
            SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

maintainer
#11 lore raw

Leo you didn't answered the question if we need TMZ for encode as well.

Regards,
Christian.

Am 10.03.22 um 09:45 schrieb Lang Yu:

Quoted 198 lines

Ping.

Quoted 195 lines · Leo Liu
Quoted 112 lines · Leo Liu
Quoted 101 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 100 lines · Christian König
Quoted 99 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 86 lines · Christian König
Quoted 85 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 69 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
    - Use patch_cs_in_place callback.

---
     drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++
     1 file changed, 68 insertions(+)

diff --git
a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
index 7bbb9ba6b80b..810932abd3af 100644

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +3 -0
@@ -24,6 +24,7 @@
     #include <linux/firmware.h>
     #include "amdgpu.h"
+#include "amdgpu_cs.h"
     #include "amdgpu_vcn.h"
     #include "amdgpu_pm.h"
     #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct
 amd_ip_funcs vcn_v1_0_ip_funcs = {
         .set_powergating_state = vcn_v1_0_set_powergating_state,
     };
+/**
+ * It is a hardware issue that Raven VCN can't

handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain
before command submission.
+ */

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +18 -0
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm,

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo) +        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +9 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+    amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+    if (r) {
+        DRM_ERROR("Failed validating the VCN
Quoted 3 lines

message BO (%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because
otherwise the driver won't
be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to.
So what you would need to do is to completely parse the structure and then
move on the relevant buffers into VRAM.

Quoted 5 lines
Quoted 1 line

Leo or James, can you help with that?

  From my observations, when decoding secure contents, register
GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address.
And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works
by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on
this.

Quoted 6 lines
Quoted 4 lines

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer()
was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

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.

Quoted 8 lines

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.

Regards,

Leo

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.

Regards,

Leo

Quoted 63 lines
Quoted 63 lines

Regards,
Christian.

Quoted 59 lines

Regards,
Lang

Quoted 56 lines

Regards,
Christian.

Quoted 53 lines

Regards,
Lang

Quoted 50 lines

Regards,
Christian.

Quoted 47 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int

vcn_v1_0_ring_patch_cs_in_place(struct
amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg ==

PACKET0(p->adev->vcn.internal.data1, 0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) { +            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+
     static const struct amdgpu_ring_funcs

vcn_v1_0_dec_ring_vm_funcs = {
        .type = AMDGPU_RING_TYPE_VCN_DEC,
        .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct
 amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
         .get_rptr = vcn_v1_0_dec_ring_get_rptr,
         .get_wptr = vcn_v1_0_dec_ring_get_wptr,
         .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
         .emit_frame_size =
             6 + 6 + /* hdp invalidate / flush */

            SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

#12 lore raw

No need for encode. Encrypting uses TEE/TA to convert clear bitstream to encrypted bitstream, and has nothing to do with VCN encode and tmz.

Regards,

Leo

Quoted 209 lines · Christian König

Leo you didn't answered the question if we need TMZ for encode as well.

Regards,
Christian.

Am 10.03.22 um 09:45 schrieb Lang Yu:

Quoted 202 lines

Ping.

Quoted 199 lines · Leo Liu
Quoted 115 lines · Leo Liu
Quoted 104 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 103 lines · Christian König
Quoted 102 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 89 lines · Christian König
Quoted 88 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 68 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
     - Use patch_cs_in_place callback.

---
     drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++

     1 file changed, 68 insertions(+)

diff --git
a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
index 7bbb9ba6b80b..810932abd3af 100644

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +3 -0
@@ -24,6 +24,7 @@
      #include <linux/firmware.h>
      #include "amdgpu.h"
+#include "amdgpu_cs.h"
      #include "amdgpu_vcn.h"
      #include "amdgpu_pm.h"
      #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct
 amd_ip_funcs vcn_v1_0_ip_funcs = {
          .set_powergating_state = vcn_v1_0_set_powergating_state,
      };
+/**
+ * It is a hardware issue that Raven VCN can't

handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +15 -0
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm,

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping || !mapping->bo_va ||
!mapping->bo_va->base.bo)
+        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +6 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+    amdgpu_bo_placement_from_domain(bo, 
Quoted 7 lines

AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); +    if (r) {
+        DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because
otherwise the driver won't
be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to.
So what you would need to do is to completely parse the structure and then
move on the relevant buffers into VRAM.

Quoted 5 lines
Quoted 1 line

Leo or James, can you help with that?

   From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address.
And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works
by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on
this.

Quoted 6 lines
Quoted 4 lines

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer()
was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

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.

Quoted 8 lines

 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.

Regards,

Leo

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.

Regards,

Leo

Quoted 64 lines
Quoted 64 lines

Regards,
Christian.

Quoted 60 lines

Regards,
Lang

Quoted 57 lines

Regards,
Christian.

Quoted 54 lines

Regards,
Lang

Quoted 51 lines

Regards,
Christian.

Quoted 48 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int

vcn_v1_0_ring_patch_cs_in_place(struct
amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg ==

PACKET0(p->adev->vcn.internal.data1, 0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+

     static const struct amdgpu_ring_funcs
vcn_v1_0_dec_ring_vm_funcs = {
         .type = AMDGPU_RING_TYPE_VCN_DEC,
         .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct
 amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
          .get_rptr = vcn_v1_0_dec_ring_get_rptr,
          .get_wptr = vcn_v1_0_dec_ring_get_wptr,
          .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,

         .emit_frame_size =
             6 + 6 + /* hdp invalidate / flush */
             SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

maintainer
#13 lore raw

Ok, thanks.

Lang is that case your patch should work fine.

Just add another patch with a check for the encode case to reject any CS with TMZ buffers in it.

Thanks,
Christian.

Am 10.03.22 um 15:17 schrieb Leo Liu:

Quoted 217 lines

No need for encode. Encrypting uses TEE/TA to convert clear bitstream to encrypted bitstream, and has nothing to do with VCN encode and tmz.

Regards,

Leo

Quoted 209 lines · Christian König

Leo you didn't answered the question if we need TMZ for encode as well.

Regards,
Christian.

Am 10.03.22 um 09:45 schrieb Lang Yu:

Quoted 202 lines

Ping.

Quoted 199 lines · Leo Liu
Quoted 115 lines · Leo Liu
Quoted 104 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 103 lines · Christian König
Quoted 102 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 89 lines · Christian König
Quoted 88 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 68 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
     - Use patch_cs_in_place callback.

---
     drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++

     1 file changed, 68 insertions(+)

diff --git
a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
index 7bbb9ba6b80b..810932abd3af 100644

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +3 -0
@@ -24,6 +24,7 @@
      #include <linux/firmware.h>
      #include "amdgpu.h"
+#include "amdgpu_cs.h"
      #include "amdgpu_vcn.h"
      #include "amdgpu_pm.h"
      #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct
 amd_ip_funcs vcn_v1_0_ip_funcs = {
          .set_powergating_state = vcn_v1_0_set_powergating_state,
      };
+/**
+ * It is a hardware issue that Raven VCN can't

handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain before command submission.
+ */
+static int vcn_v1_0_validate_bo(struct amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +15 -0
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm,

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping || !mapping->bo_va ||
!mapping->bo_va->base.bo)
+        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +6 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+    amdgpu_bo_placement_from_domain(bo, 
Quoted 7 lines

AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); +    if (r) {
+        DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because
otherwise the driver won't
be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to.
So what you would need to do is to completely parse the structure and then
move on the relevant buffers into VRAM.

Quoted 5 lines
Quoted 1 line

Leo or James, can you help with that?

   From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address.
And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works
by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on
this.

Quoted 6 lines
Quoted 4 lines

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer()
was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

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.

Quoted 8 lines

 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.

Regards,

Leo

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.

Regards,

Leo

Quoted 64 lines
Quoted 64 lines

Regards,
Christian.

Quoted 60 lines

Regards,
Lang

Quoted 57 lines

Regards,
Christian.

Quoted 54 lines

Regards,
Lang

Quoted 51 lines

Regards,
Christian.

Quoted 48 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int

vcn_v1_0_ring_patch_cs_in_place(struct
amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg ==

PACKET0(p->adev->vcn.internal.data1, 0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd, 0)) {
+            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+

     static const struct amdgpu_ring_funcs
vcn_v1_0_dec_ring_vm_funcs = {
         .type = AMDGPU_RING_TYPE_VCN_DEC,
         .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct
 amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
          .get_rptr = vcn_v1_0_dec_ring_get_rptr,
          .get_wptr = vcn_v1_0_dec_ring_get_wptr,
          .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,

         .emit_frame_size =
             6 + 6 + /* hdp invalidate / flush */
             SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

author
#14 lore raw
Quoted 6 lines · Christian König

Ok, thanks.

Lang is that case your patch should work fine.

Just add another patch with a check for the encode case to reject any CS with TMZ buffers in it.

Only VCN decode ring is cared in this patch. For encode ring (AMDGPU_HW_IP_VCN_ENC and AMDGPU_HW_IP_VCN_JPEG), is it fine we just reject secure IBs in amdgpu_ib_schedule like compute ring?

Regards,
Lang

Quoted 234 lines

Thanks,
Christian.

Am 10.03.22 um 15:17 schrieb Leo Liu:

Quoted 229 lines

No need for encode. Encrypting uses TEE/TA to convert clear bitstream to encrypted bitstream, and has nothing to do with VCN encode and tmz.

Regards,

Leo

Quoted 220 lines · Christian König

Leo you didn't answered the question if we need TMZ for encode as well.

Regards,
Christian.

Am 10.03.22 um 09:45 schrieb Lang Yu:

Quoted 213 lines

Ping.

Quoted 210 lines · Leo Liu
Quoted 122 lines · Leo Liu
Quoted 109 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 108 lines · Christian König
Quoted 107 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 94 lines · Christian König
Quoted 93 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 72 lines

It is a hardware issue that VCN can't handle a GTT backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command submission.

v2:
     - Use patch_cs_in_place callback.

---
     drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++

     1 file changed, 68 insertions(+)

diff --git
a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
index 7bbb9ba6b80b..810932abd3af 100644

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -24,6 +24,7 @@
      #include <linux/firmware.h>
      #include "amdgpu.h"
+#include "amdgpu_cs.h"
      #include "amdgpu_vcn.h"
      #include "amdgpu_pm.h"
      #include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct
 amd_ip_funcs vcn_v1_0_ip_funcs = {

         .set_powergating_state
=
vcn_v1_0_set_powergating_state,
     };
+/**
+ * It is a hardware issue that Raven VCN can't
handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain
before command submission.
+ */
+static int
vcn_v1_0_validate_bo(struct
amdgpu_cs_parser *parser,
+                struct amdgpu_job *job,
+                uint64_t addr)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +15 -0
+{
+    struct ttm_operation_ctx ctx = { false, false };
+    struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+    struct amdgpu_vm *vm = &fpriv->vm;
+    struct amdgpu_bo_va_mapping *mapping;
+    struct amdgpu_bo *bo;
+    int r;
+
+    addr &= AMDGPU_GMC_HOLE_MASK;
+    if (addr & 0x7) {
+        DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+        return -EINVAL;
+    }
+
+    mapping = amdgpu_vm_bo_lookup_mapping(vm,

addr/AMDGPU_GPU_PAGE_SIZE);
+    if (!mapping ||
!mapping->bo_va ||
!mapping->bo_va->base.bo)
+        return -EINVAL;

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +6 -0
+
+    bo = mapping->bo_va->base.bo;
+    if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+        return 0;
+
+   
Quoted 8 lines

amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
+    r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); +    if (r) {
+        DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r);
+        return r;
+    }

Well, exactly that won't work.

The message structure isn't TMZ protected because
otherwise the driver won't
be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to.
So what you would need to do is to completely parse the structure and then
move on the relevant buffers into VRAM.

Quoted 5 lines
Quoted 1 line

Leo or James, can you help with that?

   From my observations, when decoding secure contents, register GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address.
And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works
by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on
this.

Quoted 6 lines
Quoted 4 lines

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer()
was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

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.

Quoted 9 lines

 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.

Regards,

Leo

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.

Regards,

Leo

Quoted 65 lines
Quoted 65 lines

Regards,
Christian.

Quoted 61 lines

Regards,
Lang

Quoted 58 lines

Regards,
Christian.

Quoted 55 lines

Regards,
Lang

Quoted 52 lines

Regards,
Christian.

Quoted 49 lines
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return r;
+}
+
+static int

vcn_v1_0_ring_patch_cs_in_place(struct
amdgpu_cs_parser *p,
+                       struct amdgpu_job *job,
+                       struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +11 -0
+{
+    uint32_t msg_lo = 0, msg_hi = 0;
+    int i, r;
+
+    for (i = 0; i < ib->length_dw; i += 2) {
+        uint32_t reg = amdgpu_ib_get_value(ib, i);
+        uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+        if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+            msg_lo = val;
+        } else if (reg ==

PACKET0(p->adev->vcn.internal.data1, 0)) {
+            msg_hi = val;
+        } else if (reg == PACKET0(p->adev->vcn.internal.cmd,
0)) {
+            r = vcn_v1_0_validate_bo(p, job,
+                         ((u64)msg_hi) << 32 | msg_lo); +            if (r)
+                return r;
+        }
+    }

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +5 -0
+
+    return 0;
+}
+
+

     static const struct amdgpu_ring_funcs
vcn_v1_0_dec_ring_vm_funcs = {
         .type = AMDGPU_RING_TYPE_VCN_DEC,
         .align_mask = 0xf,

drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +1 -0
@@ -1914,6 +1981,7 @@ static const struct
 amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
          .get_rptr = vcn_v1_0_dec_ring_get_rptr,
          .get_wptr = vcn_v1_0_dec_ring_get_wptr,
          .set_wptr = vcn_v1_0_dec_ring_set_wptr,
+    .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,

         .emit_frame_size =
             6 + 6 + /* hdp invalidate / flush */
             SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +

#15 lore raw
Quoted 4 lines · Lang Yu
Quoted 2 lines · Christian König

Ok, thanks.

Quoted 9 lines
Quoted 4 lines

Lang is that case your patch should work fine.

Just add another patch with a check for the encode case to reject any CS with TMZ buffers in it.

Only VCN decode ring is cared in this patch. For encode ring (AMDGPU_HW_IP_VCN_ENC and AMDGPU_HW_IP_VCN_JPEG), is it fine we just reject secure IBs in amdgpu_ib_schedule like compute ring?

Yes, correct.

Alex

Quoted 237 lines

Regards,
Lang

Quoted 234 lines

Thanks,
Christian.

Am 10.03.22 um 15:17 schrieb Leo Liu:

Quoted 229 lines

No need for encode. Encrypting uses TEE/TA to convert clear bitstream to encrypted bitstream, and has nothing to do with VCN encode and tmz.

Regards,

Leo

Quoted 220 lines · Christian König

Leo you didn't answered the question if we need TMZ for encode as well.

Regards,
Christian.

Am 10.03.22 um 09:45 schrieb Lang Yu:

Quoted 213 lines

Ping.

Quoted 210 lines · Leo Liu
Quoted 122 lines · Leo Liu
Quoted 109 lines · Christian König

Am 08.03.22 um 09:06 schrieb Lang Yu:

Quoted 108 lines · Christian König
Quoted 107 lines

Am 08.03.22 um 08:33 schrieb Lang Yu:

Quoted 94 lines · Christian König
Quoted 93 lines

Am 08.03.22 um 04:39 schrieb Lang Yu:

Quoted 80 lines

It is a hardware issue that VCN can't handle a GTT
backing stored TMZ buffer on Raven.

Move such a TMZ buffer to VRAM domain before command
submission.

v2:
- Use patch_cs_in_place callback.

Suggested-by: Christian König <[email protected]>
Signed-off-by: Lang Yu <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 68
+++++++++++++++++++++++++++
1 file changed, 68 insertions(+)

diff --git
a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
index 7bbb9ba6b80b..810932abd3af 100644
--- a/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c
@@ -24,6 +24,7 @@
#include <linux/firmware.h>
#include "amdgpu.h"
+#include "amdgpu_cs.h"
#include "amdgpu_vcn.h"
#include "amdgpu_pm.h"
#include "soc15.h"
@@ -1905,6 +1906,72 @@ static const struct
amd_ip_funcs vcn_v1_0_ip_funcs = {
.set_powergating_state
=
vcn_v1_0_set_powergating_state,
};
+/**
+ * It is a hardware issue that Raven VCN can't
handle a GTT TMZ buffer.
+ * Move such a GTT TMZ buffer to VRAM domain
before command submission.
+ */
+static int
vcn_v1_0_validate_bo(struct
amdgpu_cs_parser *parser,
+ struct amdgpu_job *job,
+ uint64_t addr)
+{
+ struct ttm_operation_ctx ctx = { false, false };
+ struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
+ struct amdgpu_vm *vm = &fpriv->vm;
+ struct amdgpu_bo_va_mapping *mapping;
+ struct amdgpu_bo *bo;
+ int r;
+
+ addr &= AMDGPU_GMC_HOLE_MASK;
+ if (addr & 0x7) {
+ DRM_ERROR("VCN messages must be 8 byte aligned!\n");
+ return -EINVAL;
+ }
+
+ mapping = amdgpu_vm_bo_lookup_mapping(vm,
addr/AMDGPU_GPU_PAGE_SIZE);
+ if (!mapping ||
!mapping->bo_va ||
!mapping->bo_va->base.bo)
+ return -EINVAL;
+
+ bo = mapping->bo_va->base.bo;
+ if (!(bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED))
+ return 0;
+
+
amdgpu_bo_placement_from_domain(bo,
AMDGPU_GEM_DOMAIN_VRAM);
+ r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+ if (r) {
+ DRM_ERROR("Failed validating the VCN
message BO (%d)!\n", r);
+ return r;
+ }

Well, exactly that won't work.

The message structure isn't TMZ protected because
otherwise the driver won't
be able to stitch it together.

What is TMZ protected are the surfaces the message structure is pointing to.
So what you would need to do is to completely parse the structure and then
move on the relevant buffers into VRAM.

Quoted 5 lines
Quoted 1 line

Leo or James, can you help with that?

From my observations, when decoding secure contents, register
GPCOM_VCPU_DATA0 and GPCOM_VCPU_DATA1 are set to a TMZ buffer address.
And this way works when allocating TMZ buffers in GTT domain.

As far as I remember that's only the case for the decoding, encoding works
by putting the addresses into the message buffer.

But could be that decoding is sufficient, Leo and James need to comment on
this.

Quoted 6 lines
Quoted 4 lines

It seems that only decode needs TMZ buffers. Only observe si_vid_create_tmz_buffer()
was called in rvcn_dec_message_decode() in src/gallium/drivers/radeon/radeon_vcn_dec.c.

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.

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.

Quoted 9 lines

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.

Regards,

Leo

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.

Regards,

Leo

Quoted 65 lines
Quoted 65 lines

Regards,
Christian.

Quoted 61 lines

Regards,
Lang

Quoted 58 lines

Regards,
Christian.

Quoted 55 lines

Regards,
Lang

Quoted 52 lines

Regards,
Christian.

Quoted 49 lines

+
+ return r;
+}
+
+static int
vcn_v1_0_ring_patch_cs_in_place(struct
amdgpu_cs_parser *p,
+ struct amdgpu_job *job,
+ struct amdgpu_ib *ib)
+{
+ uint32_t msg_lo = 0, msg_hi = 0;
+ int i, r;
+
+ for (i = 0; i < ib->length_dw; i += 2) {
+ uint32_t reg = amdgpu_ib_get_value(ib, i);
+ uint32_t val = amdgpu_ib_get_value(ib, i + 1);
+
+ if (reg == PACKET0(p->adev->vcn.internal.data0, 0)) {
+ msg_lo = val;
+ } else if (reg ==
PACKET0(p->adev->vcn.internal.data1, 0)) {
+ msg_hi = val;
+ } else if (reg ==
PACKET0(p->adev->vcn.internal.cmd,
0)) {
+ r = vcn_v1_0_validate_bo(p, job,
+ ((u64)msg_hi) << 32 | msg_lo);
+ if (r)
+ return r;
+ }
+ }
+
+ return 0;
+}
+
+
static const struct amdgpu_ring_funcs
vcn_v1_0_dec_ring_vm_funcs = {
.type = AMDGPU_RING_TYPE_VCN_DEC,
.align_mask = 0xf,
@@ -1914,6 +1981,7 @@ static const struct
amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
.get_rptr = vcn_v1_0_dec_ring_get_rptr,
.get_wptr = vcn_v1_0_dec_ring_get_wptr,
.set_wptr = vcn_v1_0_dec_ring_set_wptr,
+ .patch_cs_in_place = vcn_v1_0_ring_patch_cs_in_place,
.emit_frame_size =
6 + 6 + /* hdp invalidate / flush */
SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +