1 1.1 riastrad /* $NetBSD: radeon_ucode.c,v 1.4 2021/12/18 23:45:43 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2014 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad */ 25 1.1 riastrad 26 1.1 riastrad #include <sys/cdefs.h> 27 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_ucode.c,v 1.4 2021/12/18 23:45:43 riastradh Exp $"); 28 1.1 riastrad 29 1.1 riastrad #include <linux/firmware.h> 30 1.1 riastrad #include <linux/slab.h> 31 1.1 riastrad #include <linux/module.h> 32 1.4 riastrad 33 1.1 riastrad #include "radeon.h" 34 1.1 riastrad #include "radeon_ucode.h" 35 1.1 riastrad 36 1.1 riastrad static void radeon_ucode_print_common_hdr(const struct common_firmware_header *hdr) 37 1.1 riastrad { 38 1.1 riastrad DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes)); 39 1.1 riastrad DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes)); 40 1.1 riastrad DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major)); 41 1.1 riastrad DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor)); 42 1.1 riastrad DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major)); 43 1.1 riastrad DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor)); 44 1.1 riastrad DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version)); 45 1.1 riastrad DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes)); 46 1.1 riastrad DRM_DEBUG("ucode_array_offset_bytes: %u\n", 47 1.1 riastrad le32_to_cpu(hdr->ucode_array_offset_bytes)); 48 1.1 riastrad DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32)); 49 1.1 riastrad } 50 1.1 riastrad 51 1.1 riastrad void radeon_ucode_print_mc_hdr(const struct common_firmware_header *hdr) 52 1.1 riastrad { 53 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 54 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 55 1.1 riastrad 56 1.1 riastrad DRM_DEBUG("MC\n"); 57 1.1 riastrad radeon_ucode_print_common_hdr(hdr); 58 1.1 riastrad 59 1.1 riastrad if (version_major == 1) { 60 1.1 riastrad const struct mc_firmware_header_v1_0 *mc_hdr = 61 1.3 riastrad const_container_of(hdr, struct mc_firmware_header_v1_0, header); 62 1.1 riastrad 63 1.1 riastrad DRM_DEBUG("io_debug_size_bytes: %u\n", 64 1.1 riastrad le32_to_cpu(mc_hdr->io_debug_size_bytes)); 65 1.1 riastrad DRM_DEBUG("io_debug_array_offset_bytes: %u\n", 66 1.1 riastrad le32_to_cpu(mc_hdr->io_debug_array_offset_bytes)); 67 1.1 riastrad } else { 68 1.1 riastrad DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor); 69 1.1 riastrad } 70 1.1 riastrad } 71 1.1 riastrad 72 1.1 riastrad void radeon_ucode_print_smc_hdr(const struct common_firmware_header *hdr) 73 1.1 riastrad { 74 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 75 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 76 1.1 riastrad 77 1.1 riastrad DRM_DEBUG("SMC\n"); 78 1.1 riastrad radeon_ucode_print_common_hdr(hdr); 79 1.1 riastrad 80 1.1 riastrad if (version_major == 1) { 81 1.1 riastrad const struct smc_firmware_header_v1_0 *smc_hdr = 82 1.3 riastrad const_container_of(hdr, struct smc_firmware_header_v1_0, header); 83 1.1 riastrad 84 1.1 riastrad DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(smc_hdr->ucode_start_addr)); 85 1.1 riastrad } else { 86 1.1 riastrad DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor); 87 1.1 riastrad } 88 1.1 riastrad } 89 1.1 riastrad 90 1.1 riastrad void radeon_ucode_print_gfx_hdr(const struct common_firmware_header *hdr) 91 1.1 riastrad { 92 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 93 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 94 1.1 riastrad 95 1.1 riastrad DRM_DEBUG("GFX\n"); 96 1.1 riastrad radeon_ucode_print_common_hdr(hdr); 97 1.1 riastrad 98 1.1 riastrad if (version_major == 1) { 99 1.1 riastrad const struct gfx_firmware_header_v1_0 *gfx_hdr = 100 1.3 riastrad const_container_of(hdr, struct gfx_firmware_header_v1_0, header); 101 1.1 riastrad 102 1.1 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 103 1.1 riastrad le32_to_cpu(gfx_hdr->ucode_feature_version)); 104 1.1 riastrad DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset)); 105 1.1 riastrad DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size)); 106 1.1 riastrad } else { 107 1.1 riastrad DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor); 108 1.1 riastrad } 109 1.1 riastrad } 110 1.1 riastrad 111 1.1 riastrad void radeon_ucode_print_rlc_hdr(const struct common_firmware_header *hdr) 112 1.1 riastrad { 113 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 114 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 115 1.1 riastrad 116 1.1 riastrad DRM_DEBUG("RLC\n"); 117 1.1 riastrad radeon_ucode_print_common_hdr(hdr); 118 1.1 riastrad 119 1.1 riastrad if (version_major == 1) { 120 1.1 riastrad const struct rlc_firmware_header_v1_0 *rlc_hdr = 121 1.3 riastrad const_container_of(hdr, struct rlc_firmware_header_v1_0, header); 122 1.1 riastrad 123 1.1 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 124 1.1 riastrad le32_to_cpu(rlc_hdr->ucode_feature_version)); 125 1.1 riastrad DRM_DEBUG("save_and_restore_offset: %u\n", 126 1.1 riastrad le32_to_cpu(rlc_hdr->save_and_restore_offset)); 127 1.1 riastrad DRM_DEBUG("clear_state_descriptor_offset: %u\n", 128 1.1 riastrad le32_to_cpu(rlc_hdr->clear_state_descriptor_offset)); 129 1.1 riastrad DRM_DEBUG("avail_scratch_ram_locations: %u\n", 130 1.1 riastrad le32_to_cpu(rlc_hdr->avail_scratch_ram_locations)); 131 1.1 riastrad DRM_DEBUG("master_pkt_description_offset: %u\n", 132 1.1 riastrad le32_to_cpu(rlc_hdr->master_pkt_description_offset)); 133 1.1 riastrad } else { 134 1.1 riastrad DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor); 135 1.1 riastrad } 136 1.1 riastrad } 137 1.1 riastrad 138 1.1 riastrad void radeon_ucode_print_sdma_hdr(const struct common_firmware_header *hdr) 139 1.1 riastrad { 140 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 141 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 142 1.1 riastrad 143 1.1 riastrad DRM_DEBUG("SDMA\n"); 144 1.1 riastrad radeon_ucode_print_common_hdr(hdr); 145 1.1 riastrad 146 1.1 riastrad if (version_major == 1) { 147 1.1 riastrad const struct sdma_firmware_header_v1_0 *sdma_hdr = 148 1.3 riastrad const_container_of(hdr, struct sdma_firmware_header_v1_0, header); 149 1.1 riastrad 150 1.1 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 151 1.1 riastrad le32_to_cpu(sdma_hdr->ucode_feature_version)); 152 1.1 riastrad DRM_DEBUG("ucode_change_version: %u\n", 153 1.1 riastrad le32_to_cpu(sdma_hdr->ucode_change_version)); 154 1.1 riastrad DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset)); 155 1.1 riastrad DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size)); 156 1.1 riastrad } else { 157 1.1 riastrad DRM_ERROR("Unknown SDMA ucode version: %u.%u\n", 158 1.1 riastrad version_major, version_minor); 159 1.1 riastrad } 160 1.1 riastrad } 161 1.1 riastrad 162 1.1 riastrad int radeon_ucode_validate(const struct firmware *fw) 163 1.1 riastrad { 164 1.1 riastrad const struct common_firmware_header *hdr = 165 1.1 riastrad (const struct common_firmware_header *)fw->data; 166 1.1 riastrad 167 1.1 riastrad if (fw->size == le32_to_cpu(hdr->size_bytes)) 168 1.1 riastrad return 0; 169 1.1 riastrad 170 1.1 riastrad return -EINVAL; 171 1.1 riastrad } 172 1.1 riastrad 173