Path: blob/master/drivers/gpu/drm/amd/amdgpu/amdgpu_cgs.c
26517 views
/*1* Copyright 2015 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21*22*/23#include <linux/list.h>24#include <linux/pci.h>25#include <linux/slab.h>2627#include <linux/firmware.h>28#include <drm/amdgpu_drm.h>29#include "amdgpu.h"30#include "atom.h"31#include "amdgpu_ucode.h"3233struct amdgpu_cgs_device {34struct cgs_device base;35struct amdgpu_device *adev;36};3738#define CGS_FUNC_ADEV \39struct amdgpu_device *adev = \40((struct amdgpu_cgs_device *)cgs_device)->adev414243static uint32_t amdgpu_cgs_read_register(struct cgs_device *cgs_device, unsigned int offset)44{45CGS_FUNC_ADEV;46return RREG32(offset);47}4849static void amdgpu_cgs_write_register(struct cgs_device *cgs_device, unsigned int offset,50uint32_t value)51{52CGS_FUNC_ADEV;53WREG32(offset, value);54}5556static uint32_t amdgpu_cgs_read_ind_register(struct cgs_device *cgs_device,57enum cgs_ind_reg space,58unsigned int index)59{60CGS_FUNC_ADEV;61switch (space) {62case CGS_IND_REG__PCIE:63return RREG32_PCIE(index);64case CGS_IND_REG__SMC:65return RREG32_SMC(index);66case CGS_IND_REG__UVD_CTX:67return RREG32_UVD_CTX(index);68case CGS_IND_REG__DIDT:69return RREG32_DIDT(index);70case CGS_IND_REG_GC_CAC:71return RREG32_GC_CAC(index);72case CGS_IND_REG_SE_CAC:73return RREG32_SE_CAC(index);74case CGS_IND_REG__AUDIO_ENDPT:75DRM_ERROR("audio endpt register access not implemented.\n");76return 0;77default:78BUG();79}80WARN(1, "Invalid indirect register space");81return 0;82}8384static void amdgpu_cgs_write_ind_register(struct cgs_device *cgs_device,85enum cgs_ind_reg space,86unsigned int index, uint32_t value)87{88CGS_FUNC_ADEV;89switch (space) {90case CGS_IND_REG__PCIE:91return WREG32_PCIE(index, value);92case CGS_IND_REG__SMC:93return WREG32_SMC(index, value);94case CGS_IND_REG__UVD_CTX:95return WREG32_UVD_CTX(index, value);96case CGS_IND_REG__DIDT:97return WREG32_DIDT(index, value);98case CGS_IND_REG_GC_CAC:99return WREG32_GC_CAC(index, value);100case CGS_IND_REG_SE_CAC:101return WREG32_SE_CAC(index, value);102case CGS_IND_REG__AUDIO_ENDPT:103DRM_ERROR("audio endpt register access not implemented.\n");104return;105default:106BUG();107}108WARN(1, "Invalid indirect register space");109}110111static uint32_t fw_type_convert(struct cgs_device *cgs_device, uint32_t fw_type)112{113CGS_FUNC_ADEV;114enum AMDGPU_UCODE_ID result = AMDGPU_UCODE_ID_MAXIMUM;115116switch (fw_type) {117case CGS_UCODE_ID_SDMA0:118result = AMDGPU_UCODE_ID_SDMA0;119break;120case CGS_UCODE_ID_SDMA1:121result = AMDGPU_UCODE_ID_SDMA1;122break;123case CGS_UCODE_ID_CP_CE:124result = AMDGPU_UCODE_ID_CP_CE;125break;126case CGS_UCODE_ID_CP_PFP:127result = AMDGPU_UCODE_ID_CP_PFP;128break;129case CGS_UCODE_ID_CP_ME:130result = AMDGPU_UCODE_ID_CP_ME;131break;132case CGS_UCODE_ID_CP_MEC:133case CGS_UCODE_ID_CP_MEC_JT1:134result = AMDGPU_UCODE_ID_CP_MEC1;135break;136case CGS_UCODE_ID_CP_MEC_JT2:137/* for VI. JT2 should be the same as JT1, because:1381, MEC2 and MEC1 use exactly same FW.1392, JT2 is not pached but JT1 is.140*/141if (adev->asic_type >= CHIP_TOPAZ)142result = AMDGPU_UCODE_ID_CP_MEC1;143else144result = AMDGPU_UCODE_ID_CP_MEC2;145break;146case CGS_UCODE_ID_RLC_G:147result = AMDGPU_UCODE_ID_RLC_G;148break;149case CGS_UCODE_ID_STORAGE:150result = AMDGPU_UCODE_ID_STORAGE;151break;152default:153DRM_ERROR("Firmware type not supported\n");154}155return result;156}157158static uint16_t amdgpu_get_firmware_version(struct cgs_device *cgs_device,159enum cgs_ucode_id type)160{161CGS_FUNC_ADEV;162uint16_t fw_version = 0;163164switch (type) {165case CGS_UCODE_ID_SDMA0:166fw_version = adev->sdma.instance[0].fw_version;167break;168case CGS_UCODE_ID_SDMA1:169fw_version = adev->sdma.instance[1].fw_version;170break;171case CGS_UCODE_ID_CP_CE:172fw_version = adev->gfx.ce_fw_version;173break;174case CGS_UCODE_ID_CP_PFP:175fw_version = adev->gfx.pfp_fw_version;176break;177case CGS_UCODE_ID_CP_ME:178fw_version = adev->gfx.me_fw_version;179break;180case CGS_UCODE_ID_CP_MEC:181fw_version = adev->gfx.mec_fw_version;182break;183case CGS_UCODE_ID_CP_MEC_JT1:184fw_version = adev->gfx.mec_fw_version;185break;186case CGS_UCODE_ID_CP_MEC_JT2:187fw_version = adev->gfx.mec_fw_version;188break;189case CGS_UCODE_ID_RLC_G:190fw_version = adev->gfx.rlc_fw_version;191break;192case CGS_UCODE_ID_STORAGE:193break;194default:195DRM_ERROR("firmware type %d do not have version\n", type);196break;197}198return fw_version;199}200201static int amdgpu_cgs_get_firmware_info(struct cgs_device *cgs_device,202enum cgs_ucode_id type,203struct cgs_firmware_info *info)204{205CGS_FUNC_ADEV;206207if (type != CGS_UCODE_ID_SMU && type != CGS_UCODE_ID_SMU_SK) {208uint64_t gpu_addr;209uint32_t data_size;210const struct gfx_firmware_header_v1_0 *header;211enum AMDGPU_UCODE_ID id;212struct amdgpu_firmware_info *ucode;213214id = fw_type_convert(cgs_device, type);215if (id >= AMDGPU_UCODE_ID_MAXIMUM)216return -EINVAL;217218ucode = &adev->firmware.ucode[id];219if (ucode->fw == NULL)220return -EINVAL;221222gpu_addr = ucode->mc_addr;223header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;224data_size = le32_to_cpu(header->header.ucode_size_bytes);225226if ((type == CGS_UCODE_ID_CP_MEC_JT1) ||227(type == CGS_UCODE_ID_CP_MEC_JT2)) {228gpu_addr += ALIGN(le32_to_cpu(header->header.ucode_size_bytes), PAGE_SIZE);229data_size = le32_to_cpu(header->jt_size) << 2;230}231232info->kptr = ucode->kaddr;233info->image_size = data_size;234info->mc_addr = gpu_addr;235info->version = (uint16_t)le32_to_cpu(header->header.ucode_version);236237if (type == CGS_UCODE_ID_CP_MEC)238info->image_size = le32_to_cpu(header->jt_offset) << 2;239240info->fw_version = amdgpu_get_firmware_version(cgs_device, type);241info->feature_version = (uint16_t)le32_to_cpu(header->ucode_feature_version);242} else {243char fw_name[30] = {0};244int err = 0;245uint32_t ucode_size;246uint32_t ucode_start_address;247const uint8_t *src;248const struct smc_firmware_header_v1_0 *hdr;249const struct common_firmware_header *header;250struct amdgpu_firmware_info *ucode = NULL;251252if (!adev->pm.fw) {253switch (adev->asic_type) {254case CHIP_BONAIRE:255if ((adev->pdev->revision == 0x80) ||256(adev->pdev->revision == 0x81) ||257(adev->pdev->device == 0x665f)) {258info->is_kicker = true;259strscpy(fw_name, "amdgpu/bonaire_k_smc.bin");260} else {261strscpy(fw_name, "amdgpu/bonaire_smc.bin");262}263break;264case CHIP_HAWAII:265if (adev->pdev->revision == 0x80) {266info->is_kicker = true;267strscpy(fw_name, "amdgpu/hawaii_k_smc.bin");268} else {269strscpy(fw_name, "amdgpu/hawaii_smc.bin");270}271break;272case CHIP_TOPAZ:273if (((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x81)) ||274((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x83)) ||275((adev->pdev->device == 0x6907) && (adev->pdev->revision == 0x87)) ||276((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0xD1)) ||277((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0xD3))) {278info->is_kicker = true;279strscpy(fw_name, "amdgpu/topaz_k_smc.bin");280} else281strscpy(fw_name, "amdgpu/topaz_smc.bin");282break;283case CHIP_TONGA:284if (((adev->pdev->device == 0x6939) && (adev->pdev->revision == 0xf1)) ||285((adev->pdev->device == 0x6938) && (adev->pdev->revision == 0xf1))) {286info->is_kicker = true;287strscpy(fw_name, "amdgpu/tonga_k_smc.bin");288} else289strscpy(fw_name, "amdgpu/tonga_smc.bin");290break;291case CHIP_FIJI:292strscpy(fw_name, "amdgpu/fiji_smc.bin");293break;294case CHIP_POLARIS11:295if (type == CGS_UCODE_ID_SMU) {296if (ASICID_IS_P21(adev->pdev->device, adev->pdev->revision)) {297info->is_kicker = true;298strscpy(fw_name, "amdgpu/polaris11_k_smc.bin");299} else if (ASICID_IS_P31(adev->pdev->device, adev->pdev->revision)) {300info->is_kicker = true;301strscpy(fw_name, "amdgpu/polaris11_k2_smc.bin");302} else {303strscpy(fw_name, "amdgpu/polaris11_smc.bin");304}305} else if (type == CGS_UCODE_ID_SMU_SK) {306strscpy(fw_name, "amdgpu/polaris11_smc_sk.bin");307}308break;309case CHIP_POLARIS10:310if (type == CGS_UCODE_ID_SMU) {311if (ASICID_IS_P20(adev->pdev->device, adev->pdev->revision)) {312info->is_kicker = true;313strscpy(fw_name, "amdgpu/polaris10_k_smc.bin");314} else if (ASICID_IS_P30(adev->pdev->device, adev->pdev->revision)) {315info->is_kicker = true;316strscpy(fw_name, "amdgpu/polaris10_k2_smc.bin");317} else {318strscpy(fw_name, "amdgpu/polaris10_smc.bin");319}320} else if (type == CGS_UCODE_ID_SMU_SK) {321strscpy(fw_name, "amdgpu/polaris10_smc_sk.bin");322}323break;324case CHIP_POLARIS12:325if (ASICID_IS_P23(adev->pdev->device, adev->pdev->revision)) {326info->is_kicker = true;327strscpy(fw_name, "amdgpu/polaris12_k_smc.bin");328} else {329strscpy(fw_name, "amdgpu/polaris12_smc.bin");330}331break;332case CHIP_VEGAM:333strscpy(fw_name, "amdgpu/vegam_smc.bin");334break;335case CHIP_VEGA10:336if ((adev->pdev->device == 0x687f) &&337((adev->pdev->revision == 0xc0) ||338(adev->pdev->revision == 0xc1) ||339(adev->pdev->revision == 0xc3)))340strscpy(fw_name, "amdgpu/vega10_acg_smc.bin");341else342strscpy(fw_name, "amdgpu/vega10_smc.bin");343break;344case CHIP_VEGA12:345strscpy(fw_name, "amdgpu/vega12_smc.bin");346break;347case CHIP_VEGA20:348strscpy(fw_name, "amdgpu/vega20_smc.bin");349break;350default:351DRM_ERROR("SMC firmware not supported\n");352return -EINVAL;353}354355err = amdgpu_ucode_request(adev, &adev->pm.fw,356AMDGPU_UCODE_REQUIRED,357"%s", fw_name);358if (err) {359DRM_ERROR("Failed to load firmware \"%s\"", fw_name);360amdgpu_ucode_release(&adev->pm.fw);361return err;362}363364if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {365ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];366ucode->ucode_id = AMDGPU_UCODE_ID_SMC;367ucode->fw = adev->pm.fw;368header = (const struct common_firmware_header *)ucode->fw->data;369adev->firmware.fw_size +=370ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);371}372}373374hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;375amdgpu_ucode_print_smc_hdr(&hdr->header);376adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);377ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);378ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);379src = (const uint8_t *)(adev->pm.fw->data +380le32_to_cpu(hdr->header.ucode_array_offset_bytes));381382info->version = adev->pm.fw_version;383info->image_size = ucode_size;384info->ucode_start_address = ucode_start_address;385info->kptr = (void *)src;386}387return 0;388}389390static const struct cgs_ops amdgpu_cgs_ops = {391.read_register = amdgpu_cgs_read_register,392.write_register = amdgpu_cgs_write_register,393.read_ind_register = amdgpu_cgs_read_ind_register,394.write_ind_register = amdgpu_cgs_write_ind_register,395.get_firmware_info = amdgpu_cgs_get_firmware_info,396};397398struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev)399{400struct amdgpu_cgs_device *cgs_device =401kmalloc(sizeof(*cgs_device), GFP_KERNEL);402403if (!cgs_device) {404DRM_ERROR("Couldn't allocate CGS device structure\n");405return NULL;406}407408cgs_device->base.ops = &amdgpu_cgs_ops;409cgs_device->adev = adev;410411return (struct cgs_device *)cgs_device;412}413414void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device)415{416kfree(cgs_device);417}418419420