1 /* 2 * Copyright 2016 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Author: Huang Rui 23 * 24 */ 25 26 #include <linux/firmware.h> 27 #include "amdgpu.h" 28 #include "amdgpu_psp.h" 29 #include "amdgpu_ucode.h" 30 #include "soc15_common.h" 31 #include "psp_v10_0.h" 32 33 #include "mp/mp_10_0_offset.h" 34 #include "gc/gc_9_1_offset.h" 35 #include "sdma0/sdma0_4_1_offset.h" 36 37 MODULE_FIRMWARE("amdgpu/raven_asd.bin"); 38 MODULE_FIRMWARE("amdgpu/picasso_asd.bin"); 39 MODULE_FIRMWARE("amdgpu/raven2_asd.bin"); 40 41 static int 42 psp_v10_0_get_fw_type(struct amdgpu_firmware_info *ucode, enum psp_gfx_fw_type *type) 43 { 44 switch(ucode->ucode_id) { 45 case AMDGPU_UCODE_ID_SDMA0: 46 *type = GFX_FW_TYPE_SDMA0; 47 break; 48 case AMDGPU_UCODE_ID_SDMA1: 49 *type = GFX_FW_TYPE_SDMA1; 50 break; 51 case AMDGPU_UCODE_ID_CP_CE: 52 *type = GFX_FW_TYPE_CP_CE; 53 break; 54 case AMDGPU_UCODE_ID_CP_PFP: 55 *type = GFX_FW_TYPE_CP_PFP; 56 break; 57 case AMDGPU_UCODE_ID_CP_ME: 58 *type = GFX_FW_TYPE_CP_ME; 59 break; 60 case AMDGPU_UCODE_ID_CP_MEC1: 61 *type = GFX_FW_TYPE_CP_MEC; 62 break; 63 case AMDGPU_UCODE_ID_CP_MEC1_JT: 64 *type = GFX_FW_TYPE_CP_MEC_ME1; 65 break; 66 case AMDGPU_UCODE_ID_CP_MEC2: 67 *type = GFX_FW_TYPE_CP_MEC; 68 break; 69 case AMDGPU_UCODE_ID_CP_MEC2_JT: 70 *type = GFX_FW_TYPE_CP_MEC_ME2; 71 break; 72 case AMDGPU_UCODE_ID_RLC_G: 73 *type = GFX_FW_TYPE_RLC_G; 74 break; 75 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL: 76 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_CNTL; 77 break; 78 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM: 79 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM; 80 break; 81 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM: 82 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM; 83 break; 84 case AMDGPU_UCODE_ID_SMC: 85 *type = GFX_FW_TYPE_SMU; 86 break; 87 case AMDGPU_UCODE_ID_UVD: 88 *type = GFX_FW_TYPE_UVD; 89 break; 90 case AMDGPU_UCODE_ID_VCE: 91 *type = GFX_FW_TYPE_VCE; 92 break; 93 case AMDGPU_UCODE_ID_VCN: 94 *type = GFX_FW_TYPE_VCN; 95 break; 96 case AMDGPU_UCODE_ID_DMCU_ERAM: 97 *type = GFX_FW_TYPE_DMCU_ERAM; 98 break; 99 case AMDGPU_UCODE_ID_DMCU_INTV: 100 *type = GFX_FW_TYPE_DMCU_ISR; 101 break; 102 case AMDGPU_UCODE_ID_MAXIMUM: 103 default: 104 return -EINVAL; 105 } 106 107 return 0; 108 } 109 110 static int psp_v10_0_init_microcode(struct psp_context *psp) 111 { 112 struct amdgpu_device *adev = psp->adev; 113 const char *chip_name; 114 char fw_name[30]; 115 int err = 0; 116 const struct psp_firmware_header_v1_0 *hdr; 117 118 DRM_DEBUG("\n"); 119 120 switch (adev->asic_type) { 121 case CHIP_RAVEN: 122 if (adev->rev_id >= 0x8) 123 chip_name = "raven2"; 124 else if (adev->pdev->device == 0x15d8) 125 chip_name = "picasso"; 126 else 127 chip_name = "raven"; 128 break; 129 default: BUG(); 130 } 131 132 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name); 133 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev); 134 if (err) 135 goto out; 136 137 err = amdgpu_ucode_validate(adev->psp.asd_fw); 138 if (err) 139 goto out; 140 141 hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data; 142 adev->psp.asd_fw_version = le32_to_cpu(hdr->header.ucode_version); 143 adev->psp.asd_feature_version = le32_to_cpu(hdr->ucode_feature_version); 144 adev->psp.asd_ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes); 145 adev->psp.asd_start_addr = (uint8_t *)hdr + 146 le32_to_cpu(hdr->header.ucode_array_offset_bytes); 147 148 return 0; 149 out: 150 if (err) { 151 dev_err(adev->dev, 152 "psp v10.0: Failed to load firmware \"%s\"\n", 153 fw_name); 154 release_firmware(adev->psp.asd_fw); 155 adev->psp.asd_fw = NULL; 156 } 157 158 return err; 159 } 160 161 static int psp_v10_0_prep_cmd_buf(struct amdgpu_firmware_info *ucode, 162 struct psp_gfx_cmd_resp *cmd) 163 { 164 int ret; 165 uint64_t fw_mem_mc_addr = ucode->mc_addr; 166 167 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp)); 168 169 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW; 170 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr); 171 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr); 172 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size; 173 174 ret = psp_v10_0_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type); 175 if (ret) 176 DRM_ERROR("Unknown firmware type\n"); 177 178 return ret; 179 } 180 181 static int psp_v10_0_ring_init(struct psp_context *psp, 182 enum psp_ring_type ring_type) 183 { 184 int ret = 0; 185 struct psp_ring *ring; 186 struct amdgpu_device *adev = psp->adev; 187 188 ring = &psp->km_ring; 189 190 ring->ring_type = ring_type; 191 192 /* allocate 4k Page of Local Frame Buffer memory for ring */ 193 ring->ring_size = 0x1000; 194 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE, 195 AMDGPU_GEM_DOMAIN_VRAM, 196 &adev->firmware.rbuf, 197 &ring->ring_mem_mc_addr, 198 (void **)&ring->ring_mem); 199 if (ret) { 200 ring->ring_size = 0; 201 return ret; 202 } 203 204 return 0; 205 } 206 207 static int psp_v10_0_ring_create(struct psp_context *psp, 208 enum psp_ring_type ring_type) 209 { 210 int ret = 0; 211 unsigned int psp_ring_reg = 0; 212 struct psp_ring *ring = &psp->km_ring; 213 struct amdgpu_device *adev = psp->adev; 214 215 /* Write low address of the ring to C2PMSG_69 */ 216 psp_ring_reg = lower_32_bits(ring->ring_mem_mc_addr); 217 WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_69, psp_ring_reg); 218 /* Write high address of the ring to C2PMSG_70 */ 219 psp_ring_reg = upper_32_bits(ring->ring_mem_mc_addr); 220 WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_70, psp_ring_reg); 221 /* Write size of ring to C2PMSG_71 */ 222 psp_ring_reg = ring->ring_size; 223 WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_71, psp_ring_reg); 224 /* Write the ring initialization command to C2PMSG_64 */ 225 psp_ring_reg = ring_type; 226 psp_ring_reg = psp_ring_reg << 16; 227 WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_64, psp_ring_reg); 228 229 /* There might be handshake issue with hardware which needs delay */ 230 mdelay(20); 231 232 /* Wait for response flag (bit 31) in C2PMSG_64 */ 233 ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, mmMP0_SMN_C2PMSG_64), 234 0x80000000, 0x8000FFFF, false); 235 236 return ret; 237 } 238 239 static int psp_v10_0_ring_stop(struct psp_context *psp, 240 enum psp_ring_type ring_type) 241 { 242 int ret = 0; 243 struct psp_ring *ring; 244 unsigned int psp_ring_reg = 0; 245 struct amdgpu_device *adev = psp->adev; 246 247 ring = &psp->km_ring; 248 249 /* Write the ring destroy command to C2PMSG_64 */ 250 psp_ring_reg = 3 << 16; 251 WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_64, psp_ring_reg); 252 253 /* There might be handshake issue with hardware which needs delay */ 254 mdelay(20); 255 256 /* Wait for response flag (bit 31) in C2PMSG_64 */ 257 ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, mmMP0_SMN_C2PMSG_64), 258 0x80000000, 0x80000000, false); 259 260 return ret; 261 } 262 263 static int psp_v10_0_ring_destroy(struct psp_context *psp, 264 enum psp_ring_type ring_type) 265 { 266 int ret = 0; 267 struct psp_ring *ring = &psp->km_ring; 268 struct amdgpu_device *adev = psp->adev; 269 270 ret = psp_v10_0_ring_stop(psp, ring_type); 271 if (ret) 272 DRM_ERROR("Fail to stop psp ring\n"); 273 274 amdgpu_bo_free_kernel(&adev->firmware.rbuf, 275 &ring->ring_mem_mc_addr, 276 (void **)&ring->ring_mem); 277 278 return ret; 279 } 280 281 static int psp_v10_0_cmd_submit(struct psp_context *psp, 282 struct amdgpu_firmware_info *ucode, 283 uint64_t cmd_buf_mc_addr, uint64_t fence_mc_addr, 284 int index) 285 { 286 unsigned int psp_write_ptr_reg = 0; 287 struct psp_gfx_rb_frame * write_frame = psp->km_ring.ring_mem; 288 struct psp_ring *ring = &psp->km_ring; 289 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem; 290 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start + 291 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1; 292 struct amdgpu_device *adev = psp->adev; 293 uint32_t ring_size_dw = ring->ring_size / 4; 294 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4; 295 296 /* KM (GPCOM) prepare write pointer */ 297 psp_write_ptr_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_67); 298 299 /* Update KM RB frame pointer to new frame */ 300 if ((psp_write_ptr_reg % ring_size_dw) == 0) 301 write_frame = ring_buffer_start; 302 else 303 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw); 304 /* Check invalid write_frame ptr address */ 305 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) { 306 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n", 307 ring_buffer_start, ring_buffer_end, write_frame); 308 DRM_ERROR("write_frame is pointing to address out of bounds\n"); 309 return -EINVAL; 310 } 311 312 /* Initialize KM RB frame */ 313 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame)); 314 315 /* Update KM RB frame */ 316 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr); 317 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr); 318 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr); 319 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr); 320 write_frame->fence_value = index; 321 322 /* Update the write Pointer in DWORDs */ 323 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw; 324 WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_67, psp_write_ptr_reg); 325 326 return 0; 327 } 328 329 static int 330 psp_v10_0_sram_map(struct amdgpu_device *adev, 331 unsigned int *sram_offset, unsigned int *sram_addr_reg_offset, 332 unsigned int *sram_data_reg_offset, 333 enum AMDGPU_UCODE_ID ucode_id) 334 { 335 int ret = 0; 336 337 switch(ucode_id) { 338 /* TODO: needs to confirm */ 339 #if 0 340 case AMDGPU_UCODE_ID_SMC: 341 *sram_offset = 0; 342 *sram_addr_reg_offset = 0; 343 *sram_data_reg_offset = 0; 344 break; 345 #endif 346 347 case AMDGPU_UCODE_ID_CP_CE: 348 *sram_offset = 0x0; 349 *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_CE_UCODE_ADDR); 350 *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_CE_UCODE_DATA); 351 break; 352 353 case AMDGPU_UCODE_ID_CP_PFP: 354 *sram_offset = 0x0; 355 *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_PFP_UCODE_ADDR); 356 *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_PFP_UCODE_DATA); 357 break; 358 359 case AMDGPU_UCODE_ID_CP_ME: 360 *sram_offset = 0x0; 361 *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_ME_UCODE_ADDR); 362 *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_ME_UCODE_DATA); 363 break; 364 365 case AMDGPU_UCODE_ID_CP_MEC1: 366 *sram_offset = 0x10000; 367 *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_MEC_ME1_UCODE_ADDR); 368 *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_MEC_ME1_UCODE_DATA); 369 break; 370 371 case AMDGPU_UCODE_ID_CP_MEC2: 372 *sram_offset = 0x10000; 373 *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_MEC2_UCODE_ADDR); 374 *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_MEC2_UCODE_DATA); 375 break; 376 377 case AMDGPU_UCODE_ID_RLC_G: 378 *sram_offset = 0x2000; 379 *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_UCODE_ADDR); 380 *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_UCODE_DATA); 381 break; 382 383 case AMDGPU_UCODE_ID_SDMA0: 384 *sram_offset = 0x0; 385 *sram_addr_reg_offset = SOC15_REG_OFFSET(SDMA0, 0, mmSDMA0_UCODE_ADDR); 386 *sram_data_reg_offset = SOC15_REG_OFFSET(SDMA0, 0, mmSDMA0_UCODE_DATA); 387 break; 388 389 /* TODO: needs to confirm */ 390 #if 0 391 case AMDGPU_UCODE_ID_SDMA1: 392 *sram_offset = ; 393 *sram_addr_reg_offset = ; 394 break; 395 396 case AMDGPU_UCODE_ID_UVD: 397 *sram_offset = ; 398 *sram_addr_reg_offset = ; 399 break; 400 401 case AMDGPU_UCODE_ID_VCE: 402 *sram_offset = ; 403 *sram_addr_reg_offset = ; 404 break; 405 #endif 406 407 case AMDGPU_UCODE_ID_MAXIMUM: 408 default: 409 ret = -EINVAL; 410 break; 411 } 412 413 return ret; 414 } 415 416 static bool psp_v10_0_compare_sram_data(struct psp_context *psp, 417 struct amdgpu_firmware_info *ucode, 418 enum AMDGPU_UCODE_ID ucode_type) 419 { 420 int err = 0; 421 unsigned int fw_sram_reg_val = 0; 422 unsigned int fw_sram_addr_reg_offset = 0; 423 unsigned int fw_sram_data_reg_offset = 0; 424 unsigned int ucode_size; 425 uint32_t *ucode_mem = NULL; 426 struct amdgpu_device *adev = psp->adev; 427 428 err = psp_v10_0_sram_map(adev, &fw_sram_reg_val, &fw_sram_addr_reg_offset, 429 &fw_sram_data_reg_offset, ucode_type); 430 if (err) 431 return false; 432 433 WREG32(fw_sram_addr_reg_offset, fw_sram_reg_val); 434 435 ucode_size = ucode->ucode_size; 436 ucode_mem = (uint32_t *)ucode->kaddr; 437 while (!ucode_size) { 438 fw_sram_reg_val = RREG32(fw_sram_data_reg_offset); 439 440 if (*ucode_mem != fw_sram_reg_val) 441 return false; 442 443 ucode_mem++; 444 /* 4 bytes */ 445 ucode_size -= 4; 446 } 447 448 return true; 449 } 450 451 452 static int psp_v10_0_mode1_reset(struct psp_context *psp) 453 { 454 DRM_INFO("psp mode 1 reset not supported now! \n"); 455 return -EINVAL; 456 } 457 458 static const struct psp_funcs psp_v10_0_funcs = { 459 .init_microcode = psp_v10_0_init_microcode, 460 .prep_cmd_buf = psp_v10_0_prep_cmd_buf, 461 .ring_init = psp_v10_0_ring_init, 462 .ring_create = psp_v10_0_ring_create, 463 .ring_stop = psp_v10_0_ring_stop, 464 .ring_destroy = psp_v10_0_ring_destroy, 465 .cmd_submit = psp_v10_0_cmd_submit, 466 .compare_sram_data = psp_v10_0_compare_sram_data, 467 .mode1_reset = psp_v10_0_mode1_reset, 468 }; 469 470 void psp_v10_0_set_psp_funcs(struct psp_context *psp) 471 { 472 psp->funcs = &psp_v10_0_funcs; 473 } 474