drm/amdgpu/discovery: move all table parsing into amdgpu_discovery.c
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_discovery.c
index 402e125..0c359ad 100644 (file)
@@ -271,8 +271,6 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)
 {
        struct table_info *info;
        struct binary_header *bhdr;
-       struct ip_discovery_header *ihdr;
-       struct gpu_info_header *ghdr;
        uint16_t offset;
        uint16_t size;
        uint16_t checksum;
@@ -290,7 +288,7 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)
                goto out;
        }
 
-       if(!amdgpu_discovery_verify_binary_signature(adev->mman.discovery_bin)) {
+       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);
@@ -324,31 +322,110 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)
        info = &bhdr->table_list[IP_DISCOVERY];
        offset = le16_to_cpu(info->offset);
        checksum = le16_to_cpu(info->checksum);
-       ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin + offset);
 
-       if (le32_to_cpu(ihdr->signature) != DISCOVERY_TABLE_SIGNATURE) {
-               dev_err(adev->dev, "invalid ip discovery data table signature\n");
-               r = -EINVAL;
-               goto out;
-       }
+       if (offset) {
+               struct ip_discovery_header *ihdr =
+                       (struct ip_discovery_header *)(adev->mman.discovery_bin + offset);
+               if (le32_to_cpu(ihdr->signature) != DISCOVERY_TABLE_SIGNATURE) {
+                       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,
-                                             le16_to_cpu(ihdr->size), checksum)) {
-               dev_err(adev->dev, "invalid ip discovery data table checksum\n");
-               r = -EINVAL;
-               goto out;
+               if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
+                                                     le16_to_cpu(ihdr->size), checksum)) {
+                       dev_err(adev->dev, "invalid ip discovery data table checksum\n");
+                       r = -EINVAL;
+                       goto out;
+               }
        }
 
        info = &bhdr->table_list[GC];
        offset = le16_to_cpu(info->offset);
        checksum = le16_to_cpu(info->checksum);
-       ghdr = (struct gpu_info_header *)(adev->mman.discovery_bin + offset);
 
-       if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
-                                             le32_to_cpu(ghdr->size), checksum)) {
-               dev_err(adev->dev, "invalid gc data table checksum\n");
-               r = -EINVAL;
-               goto out;
+       if (offset) {
+               struct gpu_info_header *ghdr =
+                       (struct gpu_info_header *)(adev->mman.discovery_bin + offset);
+
+               if (le32_to_cpu(ghdr->table_id) != GC_TABLE_ID) {
+                       dev_err(adev->dev, "invalid ip discovery gc table id\n");
+                       r = -EINVAL;
+                       goto out;
+               }
+
+               if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
+                                                     le32_to_cpu(ghdr->size), checksum)) {
+                       dev_err(adev->dev, "invalid gc data table checksum\n");
+                       r = -EINVAL;
+                       goto out;
+               }
+       }
+
+       info = &bhdr->table_list[HARVEST_INFO];
+       offset = le16_to_cpu(info->offset);
+       checksum = le16_to_cpu(info->checksum);
+
+       if (offset) {
+               struct harvest_info_header *hhdr =
+                       (struct harvest_info_header *)(adev->mman.discovery_bin + offset);
+
+               if (le32_to_cpu(hhdr->signature) != HARVEST_TABLE_SIGNATURE) {
+                       dev_err(adev->dev, "invalid ip discovery harvest table signature\n");
+                       r = -EINVAL;
+                       goto out;
+               }
+
+               if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
+                                                     sizeof(struct harvest_table), checksum)) {
+                       dev_err(adev->dev, "invalid harvest data table checksum\n");
+                       r = -EINVAL;
+                       goto out;
+               }
+       }
+
+       info = &bhdr->table_list[VCN_INFO];
+       offset = le16_to_cpu(info->offset);
+       checksum = le16_to_cpu(info->checksum);
+
+       if (offset) {
+               struct vcn_info_header *vhdr =
+                       (struct vcn_info_header *)(adev->mman.discovery_bin + offset);
+
+               if (le32_to_cpu(vhdr->table_id) != VCN_INFO_TABLE_ID) {
+                       dev_err(adev->dev, "invalid ip discovery vcn table id\n");
+                       r = -EINVAL;
+                       goto out;
+               }
+
+               if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
+                                                     le32_to_cpu(vhdr->size_bytes), checksum)) {
+                       dev_err(adev->dev, "invalid vcn data table checksum\n");
+                       r = -EINVAL;
+                       goto out;
+               }
+       }
+
+       info = &bhdr->table_list[MALL_INFO];
+       offset = le16_to_cpu(info->offset);
+       checksum = le16_to_cpu(info->checksum);
+
+       if (0 && offset) {
+               struct mall_info_header *mhdr =
+                       (struct mall_info_header *)(adev->mman.discovery_bin + offset);
+
+               if (le32_to_cpu(mhdr->table_id) != MALL_INFO_TABLE_ID) {
+                       dev_err(adev->dev, "invalid ip discovery mall table id\n");
+                       r = -EINVAL;
+                       goto out;
+               }
+
+               if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
+                                                     le32_to_cpu(mhdr->size_bytes), checksum)) {
+                       dev_err(adev->dev, "invalid mall data table checksum\n");
+                       r = -EINVAL;
+                       goto out;
+               }
        }
 
        return 0;
@@ -441,11 +518,19 @@ static void amdgpu_discovery_read_from_harvest_table(struct amdgpu_device *adev,
 {
        struct binary_header *bhdr;
        struct harvest_table *harvest_info;
+       u16 offset;
        int i;
 
        bhdr = (struct binary_header *)adev->mman.discovery_bin;
-       harvest_info = (struct harvest_table *)(adev->mman.discovery_bin +
-                       le16_to_cpu(bhdr->table_list[HARVEST_INFO].offset));
+       offset = le16_to_cpu(bhdr->table_list[HARVEST_INFO].offset);
+
+       if (!offset) {
+               dev_err(adev->dev, "invalid harvest table offset\n");
+               return;
+       }
+
+       harvest_info = (struct harvest_table *)(adev->mman.discovery_bin + offset);
+
        for (i = 0; i < 32; i++) {
                if (le16_to_cpu(harvest_info->list[i].hw_id) == 0)
                        break;
@@ -961,7 +1046,7 @@ static void amdgpu_discovery_sysfs_fini(struct amdgpu_device *adev)
 
 /* ================================================== */
 
-int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
+static int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 {
        struct binary_header *bhdr;
        struct ip_discovery_header *ihdr;
@@ -1127,7 +1212,7 @@ int amdgpu_discovery_get_ip_version(struct amdgpu_device *adev, int hw_id, int n
        return -EINVAL;
 }
 
-void amdgpu_discovery_harvest_ip(struct amdgpu_device *adev)
+static void amdgpu_discovery_harvest_ip(struct amdgpu_device *adev)
 {
        int vcn_harvest_count = 0;
        int umc_harvest_count = 0;
@@ -1172,10 +1257,11 @@ union gc_info {
        struct gc_info_v2_0 v2;
 };
 
-int amdgpu_discovery_get_gfx_info(struct amdgpu_device *adev)
+static int amdgpu_discovery_get_gfx_info(struct amdgpu_device *adev)
 {
        struct binary_header *bhdr;
        union gc_info *gc_info;
+       u16 offset;
 
        if (!adev->mman.discovery_bin) {
                DRM_ERROR("ip discovery uninitialized\n");
@@ -1183,8 +1269,13 @@ int amdgpu_discovery_get_gfx_info(struct amdgpu_device *adev)
        }
 
        bhdr = (struct binary_header *)adev->mman.discovery_bin;
-       gc_info = (union gc_info *)(adev->mman.discovery_bin +
-                       le16_to_cpu(bhdr->table_list[GC].offset));
+       offset = le16_to_cpu(bhdr->table_list[GC].offset);
+
+       if (!offset)
+               return 0;
+
+       gc_info = (union gc_info *)(adev->mman.discovery_bin + offset);
+
        switch (le16_to_cpu(gc_info->v1.header.version_major)) {
        case 1:
                adev->gfx.config.max_shader_engines = le32_to_cpu(gc_info->v1.gc_num_se);
@@ -1260,6 +1351,7 @@ int amdgpu_discovery_get_mall_info(struct amdgpu_device *adev)
        union mall_info *mall_info;
        u32 u, mall_size_per_umc, m_s_present, half_use;
        u64 mall_size;
+       u16 offset;
 
        if (!adev->mman.discovery_bin) {
                DRM_ERROR("ip discovery uninitialized\n");
@@ -1267,8 +1359,12 @@ int amdgpu_discovery_get_mall_info(struct amdgpu_device *adev)
        }
 
        bhdr = (struct binary_header *)adev->mman.discovery_bin;
-       mall_info = (union mall_info *)(adev->mman.discovery_bin +
-                       le16_to_cpu(bhdr->table_list[MALL_INFO].offset));
+       offset = le16_to_cpu(bhdr->table_list[MALL_INFO].offset);
+
+       if (!offset)
+               return 0;
+
+       mall_info = (union mall_info *)(adev->mman.discovery_bin + offset);
 
        switch (le16_to_cpu(mall_info->v1.header.version_major)) {
        case 1:
@@ -1296,6 +1392,52 @@ int amdgpu_discovery_get_mall_info(struct amdgpu_device *adev)
        return 0;
 }
 
+union vcn_info {
+       struct vcn_info_v1_0 v1;
+};
+
+static int amdgpu_discovery_get_vcn_info(struct amdgpu_device *adev)
+{
+       struct binary_header *bhdr;
+       union vcn_info *vcn_info;
+       u16 offset;
+       int v;
+
+       if (!adev->mman.discovery_bin) {
+               DRM_ERROR("ip discovery uninitialized\n");
+               return -EINVAL;
+       }
+
+       if (adev->vcn.num_vcn_inst > VCN_INFO_TABLE_MAX_NUM_INSTANCES) {
+               dev_err(adev->dev, "invalid vcn instances\n");
+               return -EINVAL;
+       }
+
+       bhdr = (struct binary_header *)adev->mman.discovery_bin;
+       offset = le16_to_cpu(bhdr->table_list[VCN_INFO].offset);
+
+       if (!offset)
+               return 0;
+
+       vcn_info = (union vcn_info *)(adev->mman.discovery_bin + offset);
+
+       switch (le16_to_cpu(vcn_info->v1.header.version_major)) {
+       case 1:
+               for (v = 0; v < adev->vcn.num_vcn_inst; v++) {
+                       adev->vcn.vcn_codec_disable_mask[v] =
+                               le32_to_cpu(vcn_info->v1.instance_info[v].fuse_data.all_bits);
+               }
+               break;
+       default:
+               dev_err(adev->dev,
+                       "Unhandled VCN info table %d.%d\n",
+                       le16_to_cpu(vcn_info->v1.header.version_major),
+                       le16_to_cpu(vcn_info->v1.header.version_minor));
+               return -EINVAL;
+       }
+       return 0;
+}
+
 static int amdgpu_discovery_set_common_ip_blocks(struct amdgpu_device *adev)
 {
        /* what IP to use for this? */
@@ -1889,6 +2031,9 @@ int amdgpu_discovery_set_ip_blocks(struct amdgpu_device *adev)
                        return -EINVAL;
 
                amdgpu_discovery_harvest_ip(adev);
+               amdgpu_discovery_get_gfx_info(adev);
+               amdgpu_discovery_get_mall_info(adev);
+               amdgpu_discovery_get_vcn_info(adev);
                break;
        }