1 1.8 riastrad /* $NetBSD: amdgpu_ucode.c,v 1.8 2021/12/19 12:21:29 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.8 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_ucode.c,v 1.8 2021/12/19 12:21:29 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.7 riastrad 33 1.1 riastrad #include "amdgpu.h" 34 1.1 riastrad #include "amdgpu_ucode.h" 35 1.1 riastrad 36 1.5 riastrad #include <linux/nbsd-namespace.h> 37 1.5 riastrad 38 1.1 riastrad static void amdgpu_ucode_print_common_hdr(const struct common_firmware_header *hdr) 39 1.1 riastrad { 40 1.1 riastrad DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes)); 41 1.1 riastrad DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes)); 42 1.1 riastrad DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major)); 43 1.1 riastrad DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor)); 44 1.1 riastrad DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major)); 45 1.1 riastrad DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor)); 46 1.1 riastrad DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version)); 47 1.1 riastrad DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes)); 48 1.1 riastrad DRM_DEBUG("ucode_array_offset_bytes: %u\n", 49 1.1 riastrad le32_to_cpu(hdr->ucode_array_offset_bytes)); 50 1.1 riastrad DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32)); 51 1.1 riastrad } 52 1.1 riastrad 53 1.1 riastrad void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr) 54 1.1 riastrad { 55 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 56 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 57 1.1 riastrad 58 1.1 riastrad DRM_DEBUG("MC\n"); 59 1.1 riastrad amdgpu_ucode_print_common_hdr(hdr); 60 1.1 riastrad 61 1.1 riastrad if (version_major == 1) { 62 1.1 riastrad const struct mc_firmware_header_v1_0 *mc_hdr = 63 1.3 riastrad const_container_of(hdr, struct mc_firmware_header_v1_0, header); 64 1.1 riastrad 65 1.1 riastrad DRM_DEBUG("io_debug_size_bytes: %u\n", 66 1.1 riastrad le32_to_cpu(mc_hdr->io_debug_size_bytes)); 67 1.1 riastrad DRM_DEBUG("io_debug_array_offset_bytes: %u\n", 68 1.1 riastrad le32_to_cpu(mc_hdr->io_debug_array_offset_bytes)); 69 1.1 riastrad } else { 70 1.1 riastrad DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor); 71 1.1 riastrad } 72 1.1 riastrad } 73 1.1 riastrad 74 1.1 riastrad void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr) 75 1.1 riastrad { 76 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 77 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 78 1.1 riastrad 79 1.1 riastrad DRM_DEBUG("SMC\n"); 80 1.1 riastrad amdgpu_ucode_print_common_hdr(hdr); 81 1.1 riastrad 82 1.1 riastrad if (version_major == 1) { 83 1.1 riastrad const struct smc_firmware_header_v1_0 *smc_hdr = 84 1.3 riastrad const_container_of(hdr, struct smc_firmware_header_v1_0, header); 85 1.1 riastrad 86 1.1 riastrad DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(smc_hdr->ucode_start_addr)); 87 1.7 riastrad } else if (version_major == 2) { 88 1.7 riastrad const struct smc_firmware_header_v1_0 *v1_hdr = 89 1.8 riastrad const_container_of(hdr, struct smc_firmware_header_v1_0, header); 90 1.7 riastrad const struct smc_firmware_header_v2_0 *v2_hdr = 91 1.8 riastrad const_container_of(v1_hdr, struct smc_firmware_header_v2_0, v1_0); 92 1.7 riastrad 93 1.7 riastrad DRM_DEBUG("ppt_offset_bytes: %u\n", le32_to_cpu(v2_hdr->ppt_offset_bytes)); 94 1.7 riastrad DRM_DEBUG("ppt_size_bytes: %u\n", le32_to_cpu(v2_hdr->ppt_size_bytes)); 95 1.1 riastrad } else { 96 1.1 riastrad DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor); 97 1.1 riastrad } 98 1.1 riastrad } 99 1.1 riastrad 100 1.1 riastrad void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr) 101 1.1 riastrad { 102 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 103 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 104 1.1 riastrad 105 1.1 riastrad DRM_DEBUG("GFX\n"); 106 1.1 riastrad amdgpu_ucode_print_common_hdr(hdr); 107 1.1 riastrad 108 1.1 riastrad if (version_major == 1) { 109 1.1 riastrad const struct gfx_firmware_header_v1_0 *gfx_hdr = 110 1.3 riastrad const_container_of(hdr, struct gfx_firmware_header_v1_0, header); 111 1.1 riastrad 112 1.1 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 113 1.1 riastrad le32_to_cpu(gfx_hdr->ucode_feature_version)); 114 1.1 riastrad DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset)); 115 1.1 riastrad DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size)); 116 1.1 riastrad } else { 117 1.1 riastrad DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor); 118 1.1 riastrad } 119 1.1 riastrad } 120 1.1 riastrad 121 1.1 riastrad void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr) 122 1.1 riastrad { 123 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 124 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 125 1.1 riastrad 126 1.1 riastrad DRM_DEBUG("RLC\n"); 127 1.1 riastrad amdgpu_ucode_print_common_hdr(hdr); 128 1.1 riastrad 129 1.1 riastrad if (version_major == 1) { 130 1.1 riastrad const struct rlc_firmware_header_v1_0 *rlc_hdr = 131 1.3 riastrad const_container_of(hdr, struct rlc_firmware_header_v1_0, header); 132 1.1 riastrad 133 1.1 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 134 1.1 riastrad le32_to_cpu(rlc_hdr->ucode_feature_version)); 135 1.1 riastrad DRM_DEBUG("save_and_restore_offset: %u\n", 136 1.1 riastrad le32_to_cpu(rlc_hdr->save_and_restore_offset)); 137 1.1 riastrad DRM_DEBUG("clear_state_descriptor_offset: %u\n", 138 1.1 riastrad le32_to_cpu(rlc_hdr->clear_state_descriptor_offset)); 139 1.1 riastrad DRM_DEBUG("avail_scratch_ram_locations: %u\n", 140 1.1 riastrad le32_to_cpu(rlc_hdr->avail_scratch_ram_locations)); 141 1.1 riastrad DRM_DEBUG("master_pkt_description_offset: %u\n", 142 1.1 riastrad le32_to_cpu(rlc_hdr->master_pkt_description_offset)); 143 1.1 riastrad } else if (version_major == 2) { 144 1.1 riastrad const struct rlc_firmware_header_v2_0 *rlc_hdr = 145 1.3 riastrad const_container_of(hdr, struct rlc_firmware_header_v2_0, header); 146 1.1 riastrad 147 1.1 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 148 1.1 riastrad le32_to_cpu(rlc_hdr->ucode_feature_version)); 149 1.1 riastrad DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(rlc_hdr->jt_offset)); 150 1.1 riastrad DRM_DEBUG("jt_size: %u\n", le32_to_cpu(rlc_hdr->jt_size)); 151 1.1 riastrad DRM_DEBUG("save_and_restore_offset: %u\n", 152 1.1 riastrad le32_to_cpu(rlc_hdr->save_and_restore_offset)); 153 1.1 riastrad DRM_DEBUG("clear_state_descriptor_offset: %u\n", 154 1.1 riastrad le32_to_cpu(rlc_hdr->clear_state_descriptor_offset)); 155 1.1 riastrad DRM_DEBUG("avail_scratch_ram_locations: %u\n", 156 1.1 riastrad le32_to_cpu(rlc_hdr->avail_scratch_ram_locations)); 157 1.1 riastrad DRM_DEBUG("reg_restore_list_size: %u\n", 158 1.1 riastrad le32_to_cpu(rlc_hdr->reg_restore_list_size)); 159 1.1 riastrad DRM_DEBUG("reg_list_format_start: %u\n", 160 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_format_start)); 161 1.1 riastrad DRM_DEBUG("reg_list_format_separate_start: %u\n", 162 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_format_separate_start)); 163 1.1 riastrad DRM_DEBUG("starting_offsets_start: %u\n", 164 1.1 riastrad le32_to_cpu(rlc_hdr->starting_offsets_start)); 165 1.1 riastrad DRM_DEBUG("reg_list_format_size_bytes: %u\n", 166 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_format_size_bytes)); 167 1.1 riastrad DRM_DEBUG("reg_list_format_array_offset_bytes: %u\n", 168 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes)); 169 1.1 riastrad DRM_DEBUG("reg_list_size_bytes: %u\n", 170 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_size_bytes)); 171 1.1 riastrad DRM_DEBUG("reg_list_array_offset_bytes: %u\n", 172 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes)); 173 1.1 riastrad DRM_DEBUG("reg_list_format_separate_size_bytes: %u\n", 174 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_format_separate_size_bytes)); 175 1.1 riastrad DRM_DEBUG("reg_list_format_separate_array_offset_bytes: %u\n", 176 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_format_separate_array_offset_bytes)); 177 1.1 riastrad DRM_DEBUG("reg_list_separate_size_bytes: %u\n", 178 1.1 riastrad le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes)); 179 1.7 riastrad DRM_DEBUG("reg_list_separate_array_offset_bytes: %u\n", 180 1.7 riastrad le32_to_cpu(rlc_hdr->reg_list_separate_array_offset_bytes)); 181 1.7 riastrad if (version_minor == 1) { 182 1.7 riastrad const struct rlc_firmware_header_v2_1 *v2_1 = 183 1.8 riastrad const_container_of(rlc_hdr, struct rlc_firmware_header_v2_1, v2_0); 184 1.7 riastrad DRM_DEBUG("reg_list_format_direct_reg_list_length: %u\n", 185 1.7 riastrad le32_to_cpu(v2_1->reg_list_format_direct_reg_list_length)); 186 1.7 riastrad DRM_DEBUG("save_restore_list_cntl_ucode_ver: %u\n", 187 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_cntl_ucode_ver)); 188 1.7 riastrad DRM_DEBUG("save_restore_list_cntl_feature_ver: %u\n", 189 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_cntl_feature_ver)); 190 1.7 riastrad DRM_DEBUG("save_restore_list_cntl_size_bytes %u\n", 191 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_cntl_size_bytes)); 192 1.7 riastrad DRM_DEBUG("save_restore_list_cntl_offset_bytes: %u\n", 193 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_cntl_offset_bytes)); 194 1.7 riastrad DRM_DEBUG("save_restore_list_gpm_ucode_ver: %u\n", 195 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_gpm_ucode_ver)); 196 1.7 riastrad DRM_DEBUG("save_restore_list_gpm_feature_ver: %u\n", 197 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_gpm_feature_ver)); 198 1.7 riastrad DRM_DEBUG("save_restore_list_gpm_size_bytes %u\n", 199 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_gpm_size_bytes)); 200 1.7 riastrad DRM_DEBUG("save_restore_list_gpm_offset_bytes: %u\n", 201 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_gpm_offset_bytes)); 202 1.7 riastrad DRM_DEBUG("save_restore_list_srm_ucode_ver: %u\n", 203 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_srm_ucode_ver)); 204 1.7 riastrad DRM_DEBUG("save_restore_list_srm_feature_ver: %u\n", 205 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_srm_feature_ver)); 206 1.7 riastrad DRM_DEBUG("save_restore_list_srm_size_bytes %u\n", 207 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_srm_size_bytes)); 208 1.7 riastrad DRM_DEBUG("save_restore_list_srm_offset_bytes: %u\n", 209 1.7 riastrad le32_to_cpu(v2_1->save_restore_list_srm_offset_bytes)); 210 1.7 riastrad } 211 1.1 riastrad } else { 212 1.1 riastrad DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor); 213 1.1 riastrad } 214 1.1 riastrad } 215 1.1 riastrad 216 1.1 riastrad void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr) 217 1.1 riastrad { 218 1.1 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 219 1.1 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 220 1.1 riastrad 221 1.1 riastrad DRM_DEBUG("SDMA\n"); 222 1.1 riastrad amdgpu_ucode_print_common_hdr(hdr); 223 1.1 riastrad 224 1.1 riastrad if (version_major == 1) { 225 1.1 riastrad const struct sdma_firmware_header_v1_0 *sdma_hdr = 226 1.3 riastrad const_container_of(hdr, struct sdma_firmware_header_v1_0, header); 227 1.1 riastrad 228 1.1 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 229 1.1 riastrad le32_to_cpu(sdma_hdr->ucode_feature_version)); 230 1.1 riastrad DRM_DEBUG("ucode_change_version: %u\n", 231 1.1 riastrad le32_to_cpu(sdma_hdr->ucode_change_version)); 232 1.1 riastrad DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset)); 233 1.1 riastrad DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size)); 234 1.1 riastrad if (version_minor >= 1) { 235 1.1 riastrad const struct sdma_firmware_header_v1_1 *sdma_v1_1_hdr = 236 1.3 riastrad const_container_of(sdma_hdr, struct sdma_firmware_header_v1_1, v1_0); 237 1.1 riastrad DRM_DEBUG("digest_size: %u\n", le32_to_cpu(sdma_v1_1_hdr->digest_size)); 238 1.1 riastrad } 239 1.1 riastrad } else { 240 1.1 riastrad DRM_ERROR("Unknown SDMA ucode version: %u.%u\n", 241 1.1 riastrad version_major, version_minor); 242 1.1 riastrad } 243 1.1 riastrad } 244 1.1 riastrad 245 1.7 riastrad void amdgpu_ucode_print_psp_hdr(const struct common_firmware_header *hdr) 246 1.7 riastrad { 247 1.7 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 248 1.7 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 249 1.7 riastrad 250 1.7 riastrad DRM_DEBUG("PSP\n"); 251 1.7 riastrad amdgpu_ucode_print_common_hdr(hdr); 252 1.7 riastrad 253 1.7 riastrad if (version_major == 1) { 254 1.7 riastrad const struct psp_firmware_header_v1_0 *psp_hdr = 255 1.8 riastrad const_container_of(hdr, struct psp_firmware_header_v1_0, header); 256 1.7 riastrad 257 1.7 riastrad DRM_DEBUG("ucode_feature_version: %u\n", 258 1.7 riastrad le32_to_cpu(psp_hdr->ucode_feature_version)); 259 1.7 riastrad DRM_DEBUG("sos_offset_bytes: %u\n", 260 1.7 riastrad le32_to_cpu(psp_hdr->sos_offset_bytes)); 261 1.7 riastrad DRM_DEBUG("sos_size_bytes: %u\n", 262 1.7 riastrad le32_to_cpu(psp_hdr->sos_size_bytes)); 263 1.7 riastrad if (version_minor == 1) { 264 1.7 riastrad const struct psp_firmware_header_v1_1 *psp_hdr_v1_1 = 265 1.8 riastrad const_container_of(psp_hdr, struct psp_firmware_header_v1_1, v1_0); 266 1.7 riastrad DRM_DEBUG("toc_header_version: %u\n", 267 1.7 riastrad le32_to_cpu(psp_hdr_v1_1->toc_header_version)); 268 1.7 riastrad DRM_DEBUG("toc_offset_bytes: %u\n", 269 1.7 riastrad le32_to_cpu(psp_hdr_v1_1->toc_offset_bytes)); 270 1.7 riastrad DRM_DEBUG("toc_size_bytes: %u\n", 271 1.7 riastrad le32_to_cpu(psp_hdr_v1_1->toc_size_bytes)); 272 1.7 riastrad DRM_DEBUG("kdb_header_version: %u\n", 273 1.7 riastrad le32_to_cpu(psp_hdr_v1_1->kdb_header_version)); 274 1.7 riastrad DRM_DEBUG("kdb_offset_bytes: %u\n", 275 1.7 riastrad le32_to_cpu(psp_hdr_v1_1->kdb_offset_bytes)); 276 1.7 riastrad DRM_DEBUG("kdb_size_bytes: %u\n", 277 1.7 riastrad le32_to_cpu(psp_hdr_v1_1->kdb_size_bytes)); 278 1.7 riastrad } 279 1.7 riastrad if (version_minor == 2) { 280 1.7 riastrad const struct psp_firmware_header_v1_2 *psp_hdr_v1_2 = 281 1.8 riastrad const_container_of(psp_hdr, struct psp_firmware_header_v1_2, v1_0); 282 1.7 riastrad DRM_DEBUG("kdb_header_version: %u\n", 283 1.7 riastrad le32_to_cpu(psp_hdr_v1_2->kdb_header_version)); 284 1.7 riastrad DRM_DEBUG("kdb_offset_bytes: %u\n", 285 1.7 riastrad le32_to_cpu(psp_hdr_v1_2->kdb_offset_bytes)); 286 1.7 riastrad DRM_DEBUG("kdb_size_bytes: %u\n", 287 1.7 riastrad le32_to_cpu(psp_hdr_v1_2->kdb_size_bytes)); 288 1.7 riastrad } 289 1.7 riastrad } else { 290 1.7 riastrad DRM_ERROR("Unknown PSP ucode version: %u.%u\n", 291 1.7 riastrad version_major, version_minor); 292 1.7 riastrad } 293 1.7 riastrad } 294 1.7 riastrad 295 1.7 riastrad void amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header *hdr) 296 1.7 riastrad { 297 1.7 riastrad uint16_t version_major = le16_to_cpu(hdr->header_version_major); 298 1.7 riastrad uint16_t version_minor = le16_to_cpu(hdr->header_version_minor); 299 1.7 riastrad 300 1.7 riastrad DRM_DEBUG("GPU_INFO\n"); 301 1.7 riastrad amdgpu_ucode_print_common_hdr(hdr); 302 1.7 riastrad 303 1.7 riastrad if (version_major == 1) { 304 1.7 riastrad const struct gpu_info_firmware_header_v1_0 *gpu_info_hdr = 305 1.8 riastrad const_container_of(hdr, struct gpu_info_firmware_header_v1_0, header); 306 1.7 riastrad 307 1.7 riastrad DRM_DEBUG("version_major: %u\n", 308 1.7 riastrad le16_to_cpu(gpu_info_hdr->version_major)); 309 1.7 riastrad DRM_DEBUG("version_minor: %u\n", 310 1.7 riastrad le16_to_cpu(gpu_info_hdr->version_minor)); 311 1.7 riastrad } else { 312 1.7 riastrad DRM_ERROR("Unknown gpu_info ucode version: %u.%u\n", version_major, version_minor); 313 1.7 riastrad } 314 1.7 riastrad } 315 1.7 riastrad 316 1.1 riastrad int amdgpu_ucode_validate(const struct firmware *fw) 317 1.1 riastrad { 318 1.1 riastrad const struct common_firmware_header *hdr = 319 1.1 riastrad (const struct common_firmware_header *)fw->data; 320 1.1 riastrad 321 1.1 riastrad if (fw->size == le32_to_cpu(hdr->size_bytes)) 322 1.1 riastrad return 0; 323 1.1 riastrad 324 1.1 riastrad return -EINVAL; 325 1.1 riastrad } 326 1.1 riastrad 327 1.1 riastrad bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr, 328 1.1 riastrad uint16_t hdr_major, uint16_t hdr_minor) 329 1.1 riastrad { 330 1.1 riastrad if ((hdr->common.header_version_major == hdr_major) && 331 1.1 riastrad (hdr->common.header_version_minor == hdr_minor)) 332 1.1 riastrad return false; 333 1.1 riastrad return true; 334 1.1 riastrad } 335 1.1 riastrad 336 1.7 riastrad enum amdgpu_firmware_load_type 337 1.7 riastrad amdgpu_ucode_get_load_type(struct amdgpu_device *adev, int load_type) 338 1.7 riastrad { 339 1.7 riastrad switch (adev->asic_type) { 340 1.7 riastrad #ifdef CONFIG_DRM_AMDGPU_SI 341 1.7 riastrad case CHIP_TAHITI: 342 1.7 riastrad case CHIP_PITCAIRN: 343 1.7 riastrad case CHIP_VERDE: 344 1.7 riastrad case CHIP_OLAND: 345 1.7 riastrad case CHIP_HAINAN: 346 1.7 riastrad return AMDGPU_FW_LOAD_DIRECT; 347 1.7 riastrad #endif 348 1.7 riastrad #ifdef CONFIG_DRM_AMDGPU_CIK 349 1.7 riastrad case CHIP_BONAIRE: 350 1.7 riastrad case CHIP_KAVERI: 351 1.7 riastrad case CHIP_KABINI: 352 1.7 riastrad case CHIP_HAWAII: 353 1.7 riastrad case CHIP_MULLINS: 354 1.7 riastrad return AMDGPU_FW_LOAD_DIRECT; 355 1.7 riastrad #endif 356 1.7 riastrad case CHIP_TOPAZ: 357 1.7 riastrad case CHIP_TONGA: 358 1.7 riastrad case CHIP_FIJI: 359 1.7 riastrad case CHIP_CARRIZO: 360 1.7 riastrad case CHIP_STONEY: 361 1.7 riastrad case CHIP_POLARIS10: 362 1.7 riastrad case CHIP_POLARIS11: 363 1.7 riastrad case CHIP_POLARIS12: 364 1.7 riastrad case CHIP_VEGAM: 365 1.7 riastrad return AMDGPU_FW_LOAD_SMU; 366 1.7 riastrad case CHIP_VEGA10: 367 1.7 riastrad case CHIP_RAVEN: 368 1.7 riastrad case CHIP_VEGA12: 369 1.7 riastrad case CHIP_VEGA20: 370 1.7 riastrad case CHIP_ARCTURUS: 371 1.7 riastrad case CHIP_RENOIR: 372 1.7 riastrad case CHIP_NAVI10: 373 1.7 riastrad case CHIP_NAVI14: 374 1.7 riastrad case CHIP_NAVI12: 375 1.7 riastrad if (!load_type) 376 1.7 riastrad return AMDGPU_FW_LOAD_DIRECT; 377 1.7 riastrad else 378 1.7 riastrad return AMDGPU_FW_LOAD_PSP; 379 1.7 riastrad 380 1.7 riastrad default: 381 1.7 riastrad DRM_ERROR("Unknown firmware load type\n"); 382 1.7 riastrad } 383 1.7 riastrad 384 1.7 riastrad return AMDGPU_FW_LOAD_DIRECT; 385 1.7 riastrad } 386 1.7 riastrad 387 1.8 riastrad #ifndef __NetBSD__ /* XXX amdgpu sysfs */ 388 1.8 riastrad 389 1.7 riastrad #define FW_VERSION_ATTR(name, mode, field) \ 390 1.7 riastrad static ssize_t show_##name(struct device *dev, \ 391 1.7 riastrad struct device_attribute *attr, \ 392 1.7 riastrad char *buf) \ 393 1.7 riastrad { \ 394 1.7 riastrad struct drm_device *ddev = dev_get_drvdata(dev); \ 395 1.7 riastrad struct amdgpu_device *adev = ddev->dev_private; \ 396 1.7 riastrad \ 397 1.7 riastrad return snprintf(buf, PAGE_SIZE, "0x%08x\n", adev->field); \ 398 1.7 riastrad } \ 399 1.7 riastrad static DEVICE_ATTR(name, mode, show_##name, NULL) 400 1.7 riastrad 401 1.7 riastrad FW_VERSION_ATTR(vce_fw_version, 0444, vce.fw_version); 402 1.7 riastrad FW_VERSION_ATTR(uvd_fw_version, 0444, uvd.fw_version); 403 1.7 riastrad FW_VERSION_ATTR(mc_fw_version, 0444, gmc.fw_version); 404 1.7 riastrad FW_VERSION_ATTR(me_fw_version, 0444, gfx.me_fw_version); 405 1.7 riastrad FW_VERSION_ATTR(pfp_fw_version, 0444, gfx.pfp_fw_version); 406 1.7 riastrad FW_VERSION_ATTR(ce_fw_version, 0444, gfx.ce_fw_version); 407 1.7 riastrad FW_VERSION_ATTR(rlc_fw_version, 0444, gfx.rlc_fw_version); 408 1.7 riastrad FW_VERSION_ATTR(rlc_srlc_fw_version, 0444, gfx.rlc_srlc_fw_version); 409 1.7 riastrad FW_VERSION_ATTR(rlc_srlg_fw_version, 0444, gfx.rlc_srlg_fw_version); 410 1.7 riastrad FW_VERSION_ATTR(rlc_srls_fw_version, 0444, gfx.rlc_srls_fw_version); 411 1.7 riastrad FW_VERSION_ATTR(mec_fw_version, 0444, gfx.mec_fw_version); 412 1.7 riastrad FW_VERSION_ATTR(mec2_fw_version, 0444, gfx.mec2_fw_version); 413 1.7 riastrad FW_VERSION_ATTR(sos_fw_version, 0444, psp.sos_fw_version); 414 1.7 riastrad FW_VERSION_ATTR(asd_fw_version, 0444, psp.asd_fw_version); 415 1.7 riastrad FW_VERSION_ATTR(ta_ras_fw_version, 0444, psp.ta_fw_version); 416 1.7 riastrad FW_VERSION_ATTR(ta_xgmi_fw_version, 0444, psp.ta_fw_version); 417 1.7 riastrad FW_VERSION_ATTR(smc_fw_version, 0444, pm.fw_version); 418 1.7 riastrad FW_VERSION_ATTR(sdma_fw_version, 0444, sdma.instance[0].fw_version); 419 1.7 riastrad FW_VERSION_ATTR(sdma2_fw_version, 0444, sdma.instance[1].fw_version); 420 1.7 riastrad FW_VERSION_ATTR(vcn_fw_version, 0444, vcn.fw_version); 421 1.7 riastrad FW_VERSION_ATTR(dmcu_fw_version, 0444, dm.dmcu_fw_version); 422 1.7 riastrad 423 1.7 riastrad static struct attribute *fw_attrs[] = { 424 1.7 riastrad &dev_attr_vce_fw_version.attr, &dev_attr_uvd_fw_version.attr, 425 1.7 riastrad &dev_attr_mc_fw_version.attr, &dev_attr_me_fw_version.attr, 426 1.7 riastrad &dev_attr_pfp_fw_version.attr, &dev_attr_ce_fw_version.attr, 427 1.7 riastrad &dev_attr_rlc_fw_version.attr, &dev_attr_rlc_srlc_fw_version.attr, 428 1.7 riastrad &dev_attr_rlc_srlg_fw_version.attr, &dev_attr_rlc_srls_fw_version.attr, 429 1.7 riastrad &dev_attr_mec_fw_version.attr, &dev_attr_mec2_fw_version.attr, 430 1.7 riastrad &dev_attr_sos_fw_version.attr, &dev_attr_asd_fw_version.attr, 431 1.7 riastrad &dev_attr_ta_ras_fw_version.attr, &dev_attr_ta_xgmi_fw_version.attr, 432 1.7 riastrad &dev_attr_smc_fw_version.attr, &dev_attr_sdma_fw_version.attr, 433 1.7 riastrad &dev_attr_sdma2_fw_version.attr, &dev_attr_vcn_fw_version.attr, 434 1.7 riastrad &dev_attr_dmcu_fw_version.attr, NULL 435 1.7 riastrad }; 436 1.7 riastrad 437 1.7 riastrad static const struct attribute_group fw_attr_group = { 438 1.7 riastrad .name = "fw_version", 439 1.7 riastrad .attrs = fw_attrs 440 1.7 riastrad }; 441 1.7 riastrad 442 1.8 riastrad #endif /* __NetBSD__ */ 443 1.8 riastrad 444 1.7 riastrad int amdgpu_ucode_sysfs_init(struct amdgpu_device *adev) 445 1.7 riastrad { 446 1.8 riastrad #ifdef __NetBSD__ 447 1.8 riastrad return 0; 448 1.8 riastrad #else 449 1.7 riastrad return sysfs_create_group(&adev->dev->kobj, &fw_attr_group); 450 1.8 riastrad #endif 451 1.7 riastrad } 452 1.7 riastrad 453 1.7 riastrad void amdgpu_ucode_sysfs_fini(struct amdgpu_device *adev) 454 1.7 riastrad { 455 1.8 riastrad #ifndef __NetBSD__ 456 1.7 riastrad sysfs_remove_group(&adev->dev->kobj, &fw_attr_group); 457 1.8 riastrad #endif 458 1.7 riastrad } 459 1.7 riastrad 460 1.7 riastrad static int amdgpu_ucode_init_single_fw(struct amdgpu_device *adev, 461 1.7 riastrad struct amdgpu_firmware_info *ucode, 462 1.7 riastrad uint64_t mc_addr, void *kptr) 463 1.1 riastrad { 464 1.1 riastrad const struct common_firmware_header *header = NULL; 465 1.7 riastrad const struct gfx_firmware_header_v1_0 *cp_hdr = NULL; 466 1.7 riastrad const struct dmcu_firmware_header_v1_0 *dmcu_hdr = NULL; 467 1.7 riastrad const struct dmcub_firmware_header_v1_0 *dmcub_hdr = NULL; 468 1.1 riastrad 469 1.1 riastrad if (NULL == ucode->fw) 470 1.1 riastrad return 0; 471 1.1 riastrad 472 1.1 riastrad ucode->mc_addr = mc_addr; 473 1.1 riastrad ucode->kaddr = kptr; 474 1.1 riastrad 475 1.7 riastrad if (ucode->ucode_id == AMDGPU_UCODE_ID_STORAGE) 476 1.7 riastrad return 0; 477 1.7 riastrad 478 1.1 riastrad header = (const struct common_firmware_header *)ucode->fw->data; 479 1.7 riastrad cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data; 480 1.7 riastrad dmcu_hdr = (const struct dmcu_firmware_header_v1_0 *)ucode->fw->data; 481 1.7 riastrad dmcub_hdr = (const struct dmcub_firmware_header_v1_0 *)ucode->fw->data; 482 1.7 riastrad 483 1.7 riastrad if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP || 484 1.7 riastrad (ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC1 && 485 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC2 && 486 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC1_JT && 487 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC2_JT && 488 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL && 489 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM && 490 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM && 491 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_DMCU_ERAM && 492 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_DMCU_INTV && 493 1.7 riastrad ucode->ucode_id != AMDGPU_UCODE_ID_DMCUB)) { 494 1.7 riastrad ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes); 495 1.7 riastrad 496 1.7 riastrad memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data + 497 1.7 riastrad le32_to_cpu(header->ucode_array_offset_bytes)), 498 1.7 riastrad ucode->ucode_size); 499 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1 || 500 1.7 riastrad ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2) { 501 1.7 riastrad ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) - 502 1.7 riastrad le32_to_cpu(cp_hdr->jt_size) * 4; 503 1.7 riastrad 504 1.7 riastrad memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data + 505 1.7 riastrad le32_to_cpu(header->ucode_array_offset_bytes)), 506 1.7 riastrad ucode->ucode_size); 507 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT || 508 1.7 riastrad ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT) { 509 1.7 riastrad ucode->ucode_size = le32_to_cpu(cp_hdr->jt_size) * 4; 510 1.7 riastrad 511 1.7 riastrad memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data + 512 1.7 riastrad le32_to_cpu(header->ucode_array_offset_bytes) + 513 1.7 riastrad le32_to_cpu(cp_hdr->jt_offset) * 4), 514 1.7 riastrad ucode->ucode_size); 515 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_DMCU_ERAM) { 516 1.7 riastrad ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) - 517 1.7 riastrad le32_to_cpu(dmcu_hdr->intv_size_bytes); 518 1.7 riastrad 519 1.7 riastrad memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data + 520 1.7 riastrad le32_to_cpu(header->ucode_array_offset_bytes)), 521 1.7 riastrad ucode->ucode_size); 522 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_DMCU_INTV) { 523 1.7 riastrad ucode->ucode_size = le32_to_cpu(dmcu_hdr->intv_size_bytes); 524 1.7 riastrad 525 1.7 riastrad memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data + 526 1.7 riastrad le32_to_cpu(header->ucode_array_offset_bytes) + 527 1.7 riastrad le32_to_cpu(dmcu_hdr->intv_offset_bytes)), 528 1.7 riastrad ucode->ucode_size); 529 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_DMCUB) { 530 1.7 riastrad ucode->ucode_size = le32_to_cpu(dmcub_hdr->inst_const_bytes); 531 1.7 riastrad memcpy(ucode->kaddr, 532 1.7 riastrad (void *)((uint8_t *)ucode->fw->data + 533 1.7 riastrad le32_to_cpu(header->ucode_array_offset_bytes)), 534 1.7 riastrad ucode->ucode_size); 535 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL) { 536 1.7 riastrad ucode->ucode_size = adev->gfx.rlc.save_restore_list_cntl_size_bytes; 537 1.7 riastrad memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_cntl, 538 1.7 riastrad ucode->ucode_size); 539 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM) { 540 1.7 riastrad ucode->ucode_size = adev->gfx.rlc.save_restore_list_gpm_size_bytes; 541 1.7 riastrad memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_gpm, 542 1.7 riastrad ucode->ucode_size); 543 1.7 riastrad } else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM) { 544 1.7 riastrad ucode->ucode_size = adev->gfx.rlc.save_restore_list_srm_size_bytes; 545 1.7 riastrad memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_srm, 546 1.7 riastrad ucode->ucode_size); 547 1.7 riastrad } 548 1.1 riastrad 549 1.1 riastrad return 0; 550 1.1 riastrad } 551 1.1 riastrad 552 1.7 riastrad static int amdgpu_ucode_patch_jt(struct amdgpu_firmware_info *ucode, 553 1.7 riastrad uint64_t mc_addr, void *kptr) 554 1.1 riastrad { 555 1.7 riastrad const struct gfx_firmware_header_v1_0 *header = NULL; 556 1.7 riastrad const struct common_firmware_header *comm_hdr = NULL; 557 1.7 riastrad uint8_t* src_addr = NULL; 558 1.7 riastrad uint8_t* dst_addr = NULL; 559 1.7 riastrad 560 1.7 riastrad if (NULL == ucode->fw) 561 1.7 riastrad return 0; 562 1.1 riastrad 563 1.7 riastrad comm_hdr = (const struct common_firmware_header *)ucode->fw->data; 564 1.7 riastrad header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data; 565 1.7 riastrad dst_addr = ucode->kaddr + 566 1.7 riastrad ALIGN(le32_to_cpu(comm_hdr->ucode_size_bytes), 567 1.7 riastrad PAGE_SIZE); 568 1.7 riastrad src_addr = (uint8_t *)ucode->fw->data + 569 1.7 riastrad le32_to_cpu(comm_hdr->ucode_array_offset_bytes) + 570 1.7 riastrad (le32_to_cpu(header->jt_offset) * 4); 571 1.7 riastrad memcpy(dst_addr, src_addr, le32_to_cpu(header->jt_size) * 4); 572 1.1 riastrad 573 1.7 riastrad return 0; 574 1.7 riastrad } 575 1.1 riastrad 576 1.7 riastrad int amdgpu_ucode_create_bo(struct amdgpu_device *adev) 577 1.7 riastrad { 578 1.7 riastrad if (adev->firmware.load_type != AMDGPU_FW_LOAD_DIRECT) { 579 1.7 riastrad amdgpu_bo_create_kernel(adev, adev->firmware.fw_size, PAGE_SIZE, 580 1.7 riastrad amdgpu_sriov_vf(adev) ? AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT, 581 1.7 riastrad &adev->firmware.fw_buf, 582 1.7 riastrad &adev->firmware.fw_buf_mc, 583 1.7 riastrad &adev->firmware.fw_buf_ptr); 584 1.7 riastrad if (!adev->firmware.fw_buf) { 585 1.7 riastrad dev_err(adev->dev, "failed to create kernel buffer for firmware.fw_buf\n"); 586 1.7 riastrad return -ENOMEM; 587 1.7 riastrad } else if (amdgpu_sriov_vf(adev)) { 588 1.7 riastrad memset(adev->firmware.fw_buf_ptr, 0, adev->firmware.fw_size); 589 1.1 riastrad } 590 1.1 riastrad } 591 1.7 riastrad return 0; 592 1.7 riastrad } 593 1.1 riastrad 594 1.7 riastrad void amdgpu_ucode_free_bo(struct amdgpu_device *adev) 595 1.7 riastrad { 596 1.7 riastrad if (adev->firmware.load_type != AMDGPU_FW_LOAD_DIRECT) 597 1.7 riastrad amdgpu_bo_free_kernel(&adev->firmware.fw_buf, 598 1.7 riastrad &adev->firmware.fw_buf_mc, 599 1.7 riastrad &adev->firmware.fw_buf_ptr); 600 1.1 riastrad } 601 1.1 riastrad 602 1.7 riastrad int amdgpu_ucode_init_bo(struct amdgpu_device *adev) 603 1.1 riastrad { 604 1.7 riastrad uint64_t fw_offset = 0; 605 1.1 riastrad int i; 606 1.1 riastrad struct amdgpu_firmware_info *ucode = NULL; 607 1.1 riastrad 608 1.7 riastrad /* for baremetal, the ucode is allocated in gtt, so don't need to fill the bo when reset/suspend */ 609 1.7 riastrad if (!amdgpu_sriov_vf(adev) && (adev->in_gpu_reset || adev->in_suspend)) 610 1.7 riastrad return 0; 611 1.7 riastrad /* 612 1.7 riastrad * if SMU loaded firmware, it needn't add SMC, UVD, and VCE 613 1.7 riastrad * ucode info here 614 1.7 riastrad */ 615 1.7 riastrad if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 616 1.7 riastrad if (amdgpu_sriov_vf(adev)) 617 1.7 riastrad adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 3; 618 1.7 riastrad else 619 1.7 riastrad adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 4; 620 1.7 riastrad } else { 621 1.7 riastrad adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM; 622 1.7 riastrad } 623 1.7 riastrad 624 1.7 riastrad for (i = 0; i < adev->firmware.max_ucodes; i++) { 625 1.1 riastrad ucode = &adev->firmware.ucode[i]; 626 1.1 riastrad if (ucode->fw) { 627 1.7 riastrad amdgpu_ucode_init_single_fw(adev, ucode, adev->firmware.fw_buf_mc + fw_offset, 628 1.7 riastrad adev->firmware.fw_buf_ptr + fw_offset); 629 1.7 riastrad if (i == AMDGPU_UCODE_ID_CP_MEC1 && 630 1.7 riastrad adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 631 1.7 riastrad const struct gfx_firmware_header_v1_0 *cp_hdr; 632 1.7 riastrad cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data; 633 1.7 riastrad amdgpu_ucode_patch_jt(ucode, adev->firmware.fw_buf_mc + fw_offset, 634 1.7 riastrad adev->firmware.fw_buf_ptr + fw_offset); 635 1.7 riastrad fw_offset += ALIGN(le32_to_cpu(cp_hdr->jt_size) << 2, PAGE_SIZE); 636 1.7 riastrad } 637 1.7 riastrad fw_offset += ALIGN(ucode->ucode_size, PAGE_SIZE); 638 1.1 riastrad } 639 1.1 riastrad } 640 1.1 riastrad return 0; 641 1.1 riastrad } 642