drm/amdgpu: add helper to load ip_discovery binary from file
Subsystems (2)
PATCH 1/4 drm/amdgpu: add helper to load ip_discovery binary from file
To be used when ip_discovery binary is not carried by vbios
--- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +30 -1
@@ -67,7 +67,8 @@#include "smuio_v11_0_6.h" #include "smuio_v13_0.h" -MODULE_FIRMWARE ("amdgpu/ip_discovery.bin" ); +#define FIRMWARE_IP_DISCOVERY "amdgpu/ip_discovery.bin" +MODULE_FIRMWARE (FIRMWARE_IP_DISCOVERY );#define mmRCC_CONFIG_MEMSIZE 0xde3#define mmMM_INDEX 0x0
@@ -187,6 +188,34 @@ static int amdgpu_discovery_read_binary(struct amdgpu_device *adev, uint8_t *binreturn 0 ; } +static int amdgpu_discovery_read_binary_from_file (struct amdgpu_device *adev,uint8_t *binary) +{ +const struct firmware *fw; +const char *fw_name; +int r; + +switch (amdgpu_discovery) { +case 2 : + fw_name =FIRMWARE_IP_DISCOVERY ; +break ; +default : +dev_warn (adev->dev,"amdgpu_discovery is not set properly\n" ); +return -EINVAL ; + } + + r =request_firmware (&fw, fw_name, adev->dev); +if (r) { +dev_err (adev->dev,"can't load firmware \"%s\"\n" , + fw_name); +return r; + } + +memcpy ((u8 *)binary, (u8 *)fw->data, adev->mman.discovery_tmr_size); +release_firmware (fw); + +return 0 ; +} +static uint16_t amdgpu_discovery_calculate_checksum (uint8_t *data,uint32_t size) {uint16_t checksum =0 ;
Signature · 1 line
2.17.1
PATCH 2/4 drm/amdgpu: rename discovery_read_binary helper
add _from_vram in the funciton name to diffrentiate the one used to read from file
--- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +2 -2
@@ -178,7 +178,7 @@ static int hw_id_map[MAX_HWIP] = { [DCI_HWIP] = DCI_HWID, }; -static int amdgpu_discovery_read_binary (struct amdgpu_device *adev,uint8_t *binary) +static int amdgpu_discovery_read_binary_from_vram (struct amdgpu_device *adev,uint8_t *binary) {uint64_t vram_size = (uint64_t )RREG32 (mmRCC_CONFIG_MEMSIZE) <<20 ;uint64_t pos = vram_size -DISCOVERY_TMR_OFFSET ;
@@ -260,7 +260,7 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)release_firmware (fw); }else { get_from_vram: - r =amdgpu_discovery_read_binary (adev, adev->mman.discovery_bin); + r =amdgpu_discovery_read_binary_from_vram (adev, adev->mman.discovery_bin);if (r) {DRM_ERROR ("failed to read ip discovery binary\n" ); goto out;
Signature · 1 line
2.17.1
PATCH 3/4 drm/amdgpu: add helper to verify ip discovery binary signature
In reply to Hawking ZhangTo be used to check ip discovery binary signature
--- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +8 -0
@@ -233,6 +233,14 @@ static inline bool amdgpu_discovery_verify_checksum(uint8_t *data, uint32_t sizereturn !!(amdgpu_discovery_calculate_checksum (data, size) == expected); } +static inline bool amdgpu_discovery_verify_binary_signature (uint8_t *binary) +{ +struct binary_header *bhdr; + bhdr = (struct binary_header *)binary; + +return (le32_to_cpu (bhdr->binary_signature) ==BINARY_SIGNATURE ); +} +static int amdgpu_discovery_init (struct amdgpu_device *adev) {struct table_info *info;
Signature · 1 line
2.17.1
PATCH 4/4 drm/amdgpu: read and authenticate ip discovery binary
In reply to Hawking Zhangread and authenticate ip discovery binary getting from vram first, if it is not valid, read and authenticate the one getting from file
--- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +20 -24
@@ -247,7 +247,6 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)struct binary_header *bhdr;struct ip_discovery_header *ihdr;struct gpu_info_header *ghdr; -const struct firmware *fw;uint16_t offset;uint16_t size;uint16_t checksum;
@@ -258,31 +257,28 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)if (!adev->mman.discovery_bin)return -ENOMEM ; -if (amdgpu_discovery ==2 ) { - r =request_firmware (&fw,"amdgpu/ip_discovery.bin" , adev->dev); -if (r) - goto get_from_vram; -dev_info (adev->dev,"Using IP discovery from file\n" ); -memcpy ((u8 *)adev->mman.discovery_bin, (u8 *)fw->data, - adev->mman.discovery_tmr_size); -release_firmware (fw); - }else { -get_from_vram: - r =amdgpu_discovery_read_binary_from_vram (adev, adev->mman.discovery_bin); + r =amdgpu_discovery_read_binary_from_vram (adev, adev->mman.discovery_bin); +if (r) { +dev_err (adev->dev,"failed to read ip discovery binary from vram\n" ); + goto out; + } + +if (!amdgpu_discovery_verify_binary_signature (adev->mman.discovery_bin)) { +dev_warn (adev->dev,"get invalid ip discovery binary signature from vram\n" ); +/* retry read ip discovery binary from file */ + r =amdgpu_discovery_read_binary_from_file (adev, adev->mman.discovery_bin);if (r) { -DRM_ERROR ("failed to read ip discovery binary\n" ); +dev_err (adev->dev,"failed to read ip discovery binary from file\n" ); + goto out; + } +/* check the ip discovery binary signature */ +if (!amdgpu_discovery_verify_binary_signature (adev->mman.discovery_bin)) { +dev_warn (adev->dev,"get invalid ip discovery binary signature from file\n" ); goto out; } } bhdr = (struct binary_header *)adev->mman.discovery_bin; - -if (le32_to_cpu (bhdr->binary_signature) !=BINARY_SIGNATURE ) { -DRM_ERROR ("invalid ip discovery binary signature\n" ); - r = -EINVAL ; - goto out; - } - offset = offsetof(struct binary_header , binary_checksum) +sizeof (bhdr->binary_checksum); size = bhdr->binary_size - offset;
@@ -290,7 +286,7 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)if (!amdgpu_discovery_verify_checksum (adev->mman.discovery_bin + offset, size, checksum)) { -DRM_ERROR ("invalid ip discovery binary checksum\n" ); +dev_err (adev->dev,"invalid ip discovery binary checksum\n" ); r = -EINVAL ; goto out; }
@@ -301,14 +297,14 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev) ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin + offset);if (le32_to_cpu (ihdr->signature) !=DISCOVERY_TABLE_SIGNATURE ) { -DRM_ERROR ("invalid ip discovery data table signature\n" ); +dev_err (adev->dev,"invalid ip discovery data table signature\n" ); r = -EINVAL ; goto out; }if (!amdgpu_discovery_verify_checksum (adev->mman.discovery_bin + offset, ihdr->size, checksum)) { -DRM_ERROR ("invalid ip discovery data table checksum\n" ); +dev_err (adev->dev,"invalid ip discovery data table checksum\n" ); r = -EINVAL ; goto out; }
@@ -320,7 +316,7 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)if (!amdgpu_discovery_verify_checksum (adev->mman.discovery_bin + offset, ghdr->size, checksum)) { -DRM_ERROR ("invalid gc data table checksum\n" ); +dev_err (adev->dev,"invalid gc data table checksum\n" ); r = -EINVAL ; goto out; }
Signature · 1 line
2.17.1
RE: [PATCH 4/4] drm/amdgpu: read and authenticate ip discovery binary
[AMD Official Use Only]
Please ignore this one (patch #4). Will send out a new one based on latest code.
Regards,
Hawking
-----Original Message-----
From: Zhang, Hawking <[email protected]>
Sent: Saturday, December 4, 2021 18:21
To: [email protected]
Cc: Zhang, Hawking <[email protected]>
Subject: [PATCH 4/4] drm/amdgpu: read and authenticate ip discovery binary
read and authenticate ip discovery binary getting from vram first, if it is not valid, read and authenticate the one getting from file
--- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +18 -20
@@ -247,7 +247,6 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)struct binary_header *bhdr;struct ip_discovery_header *ihdr;struct gpu_info_header *ghdr; -const struct firmware *fw;uint16_t offset;uint16_t size;uint16_t checksum;
@@ -258,31 +257,28 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)if (!adev->mman.discovery_bin)return -ENOMEM ; -if (amdgpu_discovery ==2 ) { - r =request_firmware (&fw,"amdgpu/ip_discovery.bin" , adev->dev); -if (r) - goto get_from_vram; -dev_info (adev->dev,"Using IP discovery from file\n" ); -memcpy ((u8 *)adev->mman.discovery_bin, (u8 *)fw->data, - adev->mman.discovery_tmr_size); -release_firmware (fw); - }else { -get_from_vram: - r =amdgpu_discovery_read_binary_from_vram (adev, adev->mman.discovery_bin); + r =amdgpu_discovery_read_binary_from_vram (adev, adev->mman.discovery_bin); +if (r) { +dev_err (adev->dev,"failed to read ip discovery binary from vram\n" ); + goto out; + } + +if (!amdgpu_discovery_verify_binary_signature (adev->mman.discovery_bin)) { +dev_warn (adev->dev,"get invalid ip discovery binary signature from vram\n" ); +/* retry read ip discovery binary from file */ + r =amdgpu_discovery_read_binary_from_file (adev, +adev->mman.discovery_bin);if (r) { -DRM_ERROR ("failed to read ip discovery binary\n" ); +dev_err (adev->dev,"failed to read ip discovery binary from file\n" ); + goto out; + } +/* check the ip discovery binary signature */ +if (!amdgpu_discovery_verify_binary_signature (adev->mman.discovery_bin)) { +dev_warn (adev->dev,"get invalid ip discovery binary signature from +file\n"); goto out; } } bhdr = (struct binary_header *)adev->mman.discovery_bin; - -if (le32_to_cpu (bhdr->binary_signature) !=BINARY_SIGNATURE ) { -DRM_ERROR ("invalid ip discovery binary signature\n" ); - r = -EINVAL ; - goto out; - } - offset = offsetof(struct binary_header, binary_checksum) +
sizeof(bhdr->binary_checksum);
size = bhdr->binary_size - offset;
@@ -290,7 +286,7 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)
if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
size, checksum)) {
- DRM_ERROR("invalid ip discovery binary checksum\n");
+ dev_err(adev->dev, "invalid ip discovery binary checksum\n");
r = -EINVAL;
goto out;
}
@@ -301,14 +297,14 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)
ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin + offset);
if (le32_to_cpu(ihdr->signature) != DISCOVERY_TABLE_SIGNATURE) {
- DRM_ERROR("invalid ip discovery data table signature\n");
+ dev_err(adev->dev, "invalid ip discovery data table signature\n");
r = -EINVAL;
goto out;
}
if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
ihdr->size, checksum)) {
- DRM_ERROR("invalid ip discovery data table checksum\n");
+ dev_err(adev->dev, "invalid ip discovery data table checksum\n");
r = -EINVAL;
goto out;
}
@@ -320,7 +316,7 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)
if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
ghdr->size, checksum)) {
- DRM_ERROR("invalid gc data table checksum\n");
+ dev_err(adev->dev, "invalid gc data table checksum\n");
r = -EINVAL;
goto out;
}
--
2.17.1
[AMD Official Use Only]
Hi Hawking,
A new function is defined in this patch, but it has not been used.
I am not sure whether this separate patch will generate a warning or whether it can be directly merged into patch-2 ?
and the "(u8 *)" is not necessary for this case, discard or using (void *) is better for this case. __visible void *memcpy(void *to, const void *from, size_t n)
thanks.
Best Regards,
Kevin
________________________________
From: amd-gfx <[email protected]> on behalf of Hawking Zhang <[email protected]>
Sent: Saturday, December 4, 2021 6:20 PM
To: [email protected] <[email protected]>
Cc: Zhang, Hawking <[email protected]>
Subject: [PATCH 1/4] drm/amdgpu: add helper to load ip_discovery binary from file
To be used when ip_discovery binary is not carried by vbios
--- drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +30 -1
@@ -67,7 +67,8 @@#include "smuio_v11_0_6.h" #include "smuio_v13_0.h" -MODULE_FIRMWARE ("amdgpu/ip_discovery.bin" ); +#define FIRMWARE_IP_DISCOVERY "amdgpu/ip_discovery.bin" +MODULE_FIRMWARE (FIRMWARE_IP_DISCOVERY );#define mmRCC_CONFIG_MEMSIZE 0xde3#define mmMM_INDEX 0x0
@@ -187,6 +188,34 @@ static int amdgpu_discovery_read_binary(struct amdgpu_device *adev, uint8_t *binreturn 0 ; } +static int amdgpu_discovery_read_binary_from_file (struct amdgpu_device *adev,uint8_t *binary) +{ +const struct firmware *fw; +const char *fw_name; +int r; + +switch (amdgpu_discovery) { +case 2 : + fw_name =FIRMWARE_IP_DISCOVERY ; +break ; +default : +dev_warn (adev->dev,"amdgpu_discovery is not set properly\n" ); +return -EINVAL ; + } + + r =request_firmware (&fw, fw_name, adev->dev); +if (r) { +dev_err (adev->dev,"can't load firmware \"%s\"\n" , + fw_name); +return r; + } + +memcpy ((u8 *)binary, (u8 *)fw->data, adev->mman.discovery_tmr_size); +release_firmware (fw); + +return 0 ; +} +static uint16_t amdgpu_discovery_calculate_checksum (uint8_t *data,uint32_t size) {uint16_t checksum =0 ;
--
2.17.1