Path: blob/master/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c
26517 views
/*1* Copyright 2008 Advanced Micro Devices, Inc.2* Copyright 2008 Red Hat Inc.3* Copyright 2009 Jerome Glisse.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included in13* all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*23* Authors: Dave Airlie24* Alex Deucher25* Jerome Glisse26*/2728#include "amdgpu.h"29#include "atom.h"3031#include <linux/device.h>32#include <linux/pci.h>33#include <linux/slab.h>34#include <linux/acpi.h>35/*36* BIOS.37*/3839#define AMD_VBIOS_SIGNATURE " 761295520"40#define AMD_VBIOS_SIGNATURE_OFFSET 0x3041#define AMD_VBIOS_SIGNATURE_SIZE sizeof(AMD_VBIOS_SIGNATURE)42#define AMD_VBIOS_SIGNATURE_END (AMD_VBIOS_SIGNATURE_OFFSET + AMD_VBIOS_SIGNATURE_SIZE)43#define AMD_IS_VALID_VBIOS(p) ((p)[0] == 0x55 && (p)[1] == 0xAA)44#define AMD_VBIOS_LENGTH(p) ((p)[2] << 9)4546/* Check if current bios is an ATOM BIOS.47* Return true if it is ATOM BIOS. Otherwise, return false.48*/49static bool check_atom_bios(struct amdgpu_device *adev, size_t size)50{51uint16_t tmp, bios_header_start;52uint8_t *bios = adev->bios;5354if (!bios || size < 0x49) {55dev_dbg(adev->dev, "VBIOS mem is null or mem size is wrong\n");56return false;57}5859if (!AMD_IS_VALID_VBIOS(bios)) {60dev_dbg(adev->dev, "VBIOS signature incorrect %x %x\n", bios[0],61bios[1]);62return false;63}6465bios_header_start = bios[0x48] | (bios[0x49] << 8);66if (!bios_header_start) {67dev_dbg(adev->dev, "Can't locate VBIOS header\n");68return false;69}7071tmp = bios_header_start + 4;72if (size < tmp) {73dev_dbg(adev->dev, "VBIOS header is broken\n");74return false;75}7677if (!memcmp(bios + tmp, "ATOM", 4) ||78!memcmp(bios + tmp, "MOTA", 4)) {79dev_dbg(adev->dev, "ATOMBIOS detected\n");80return true;81}8283return false;84}8586void amdgpu_bios_release(struct amdgpu_device *adev)87{88kfree(adev->bios);89adev->bios = NULL;90adev->bios_size = 0;91}9293/* If you boot an IGP board with a discrete card as the primary,94* the IGP rom is not accessible via the rom bar as the IGP rom is95* part of the system bios. On boot, the system bios puts a96* copy of the igp rom at the start of vram if a discrete card is97* present.98* For SR-IOV, the vbios image is also put in VRAM in the VF.99*/100static bool amdgpu_read_bios_from_vram(struct amdgpu_device *adev)101{102uint8_t __iomem *bios;103resource_size_t vram_base;104resource_size_t size = 256 * 1024; /* ??? */105106if (!(adev->flags & AMD_IS_APU))107if (amdgpu_device_need_post(adev))108return false;109110/* FB BAR not enabled */111if (pci_resource_len(adev->pdev, 0) == 0)112return false;113114adev->bios = NULL;115vram_base = pci_resource_start(adev->pdev, 0);116bios = ioremap_wc(vram_base, size);117if (!bios)118return false;119120adev->bios = kmalloc(size, GFP_KERNEL);121if (!adev->bios) {122iounmap(bios);123return false;124}125adev->bios_size = size;126memcpy_fromio(adev->bios, bios, size);127iounmap(bios);128129if (!check_atom_bios(adev, size)) {130amdgpu_bios_release(adev);131return false;132}133134return true;135}136137bool amdgpu_read_bios(struct amdgpu_device *adev)138{139uint8_t __iomem *bios;140size_t size;141142adev->bios = NULL;143/* XXX: some cards may return 0 for rom size? ddx has a workaround */144bios = pci_map_rom(adev->pdev, &size);145if (!bios)146return false;147148adev->bios = kzalloc(size, GFP_KERNEL);149if (adev->bios == NULL) {150pci_unmap_rom(adev->pdev, bios);151return false;152}153adev->bios_size = size;154memcpy_fromio(adev->bios, bios, size);155pci_unmap_rom(adev->pdev, bios);156157if (!check_atom_bios(adev, size)) {158amdgpu_bios_release(adev);159return false;160}161162return true;163}164165static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev)166{167u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0};168int len;169170if (!adev->asic_funcs || !adev->asic_funcs->read_bios_from_rom)171return false;172173/* validate VBIOS signature */174if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false)175return false;176header[AMD_VBIOS_SIGNATURE_END] = 0;177178if ((!AMD_IS_VALID_VBIOS(header)) ||179memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET],180AMD_VBIOS_SIGNATURE,181strlen(AMD_VBIOS_SIGNATURE)) != 0)182return false;183184/* valid vbios, go on */185len = AMD_VBIOS_LENGTH(header);186len = ALIGN(len, 4);187adev->bios = kmalloc(len, GFP_KERNEL);188if (!adev->bios) {189DRM_ERROR("no memory to allocate for BIOS\n");190return false;191}192adev->bios_size = len;193194/* read complete BIOS */195amdgpu_asic_read_bios_from_rom(adev, adev->bios, len);196197if (!check_atom_bios(adev, len)) {198amdgpu_bios_release(adev);199return false;200}201202return true;203}204205static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)206{207phys_addr_t rom = adev->pdev->rom;208size_t romlen = adev->pdev->romlen;209void __iomem *bios;210211adev->bios = NULL;212213if (!rom || romlen == 0)214return false;215216adev->bios = kzalloc(romlen, GFP_KERNEL);217if (!adev->bios)218return false;219220bios = ioremap(rom, romlen);221if (!bios)222goto free_bios;223224memcpy_fromio(adev->bios, bios, romlen);225iounmap(bios);226227if (!check_atom_bios(adev, romlen))228goto free_bios;229230adev->bios_size = romlen;231232return true;233free_bios:234amdgpu_bios_release(adev);235236return false;237}238239#ifdef CONFIG_ACPI240/* ATRM is used to get the BIOS on the discrete cards in241* dual-gpu systems.242*/243/* retrieve the ROM in 4k blocks */244#define ATRM_BIOS_PAGE 4096245/**246* amdgpu_atrm_call - fetch a chunk of the vbios247*248* @atrm_handle: acpi ATRM handle249* @bios: vbios image pointer250* @offset: offset of vbios image data to fetch251* @len: length of vbios image data to fetch252*253* Executes ATRM to fetch a chunk of the discrete254* vbios image on PX systems (all asics).255* Returns the length of the buffer fetched.256*/257static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,258int offset, int len)259{260acpi_status status;261union acpi_object atrm_arg_elements[2], *obj;262struct acpi_object_list atrm_arg;263struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};264265atrm_arg.count = 2;266atrm_arg.pointer = &atrm_arg_elements[0];267268atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;269atrm_arg_elements[0].integer.value = offset;270271atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;272atrm_arg_elements[1].integer.value = len;273274status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);275if (ACPI_FAILURE(status)) {276DRM_ERROR("failed to evaluate ATRM got %s\n", acpi_format_exception(status));277return -ENODEV;278}279280obj = (union acpi_object *)buffer.pointer;281memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);282len = obj->buffer.length;283kfree(buffer.pointer);284return len;285}286287static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)288{289int ret;290int size = 256 * 1024;291int i;292struct pci_dev *pdev = NULL;293acpi_handle dhandle, atrm_handle;294acpi_status status;295bool found = false;296297/* ATRM is for on-platform devices only */298if (dev_is_removable(&adev->pdev->dev))299return false;300301while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {302if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&303(pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))304continue;305306dhandle = ACPI_HANDLE(&pdev->dev);307if (!dhandle)308continue;309310status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);311if (ACPI_SUCCESS(status)) {312found = true;313break;314}315}316317if (!found)318return false;319pci_dev_put(pdev);320321adev->bios = kmalloc(size, GFP_KERNEL);322if (!adev->bios) {323dev_err(adev->dev, "Unable to allocate bios\n");324return false;325}326327for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {328ret = amdgpu_atrm_call(atrm_handle,329adev->bios,330(i * ATRM_BIOS_PAGE),331ATRM_BIOS_PAGE);332if (ret < ATRM_BIOS_PAGE)333break;334}335336if (!check_atom_bios(adev, size)) {337amdgpu_bios_release(adev);338return false;339}340adev->bios_size = size;341return true;342}343#else344static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)345{346return false;347}348#endif349350static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)351{352return (!adev->asic_funcs || !adev->asic_funcs->read_disabled_bios) ?353false : amdgpu_asic_read_disabled_bios(adev);354}355356#ifdef CONFIG_ACPI357static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)358{359struct acpi_table_header *hdr;360acpi_size tbl_size;361UEFI_ACPI_VFCT *vfct;362unsigned int offset;363364if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))365return false;366tbl_size = hdr->length;367if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {368dev_info(adev->dev, "ACPI VFCT table present but broken (too short #1),skipping\n");369return false;370}371372vfct = (UEFI_ACPI_VFCT *)hdr;373offset = vfct->VBIOSImageOffset;374375while (offset < tbl_size) {376GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);377VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;378379offset += sizeof(VFCT_IMAGE_HEADER);380if (offset > tbl_size) {381dev_info(adev->dev, "ACPI VFCT image header truncated,skipping\n");382return false;383}384385offset += vhdr->ImageLength;386if (offset > tbl_size) {387dev_info(adev->dev, "ACPI VFCT image truncated,skipping\n");388return false;389}390391if (vhdr->ImageLength &&392vhdr->PCIBus == adev->pdev->bus->number &&393vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&394vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) &&395vhdr->VendorID == adev->pdev->vendor &&396vhdr->DeviceID == adev->pdev->device) {397adev->bios = kmemdup(&vbios->VbiosContent,398vhdr->ImageLength,399GFP_KERNEL);400401if (!check_atom_bios(adev, vhdr->ImageLength)) {402amdgpu_bios_release(adev);403return false;404}405adev->bios_size = vhdr->ImageLength;406return true;407}408}409410dev_info(adev->dev, "ACPI VFCT table present but broken (too short #2),skipping\n");411return false;412}413#else414static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)415{416return false;417}418#endif419420static bool amdgpu_get_bios_apu(struct amdgpu_device *adev)421{422if (amdgpu_acpi_vfct_bios(adev)) {423dev_info(adev->dev, "Fetched VBIOS from VFCT\n");424goto success;425}426427if (amdgpu_read_bios_from_vram(adev)) {428dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");429goto success;430}431432if (amdgpu_read_bios(adev)) {433dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");434goto success;435}436437if (amdgpu_read_platform_bios(adev)) {438dev_info(adev->dev, "Fetched VBIOS from platform\n");439goto success;440}441442dev_err(adev->dev, "Unable to locate a BIOS ROM\n");443return false;444445success:446return true;447}448449static bool amdgpu_prefer_rom_resource(struct amdgpu_device *adev)450{451struct resource *res = &adev->pdev->resource[PCI_ROM_RESOURCE];452453return (res->flags & IORESOURCE_ROM_SHADOW);454}455456static bool amdgpu_get_bios_dgpu(struct amdgpu_device *adev)457{458if (amdgpu_atrm_get_bios(adev)) {459dev_info(adev->dev, "Fetched VBIOS from ATRM\n");460goto success;461}462463if (amdgpu_acpi_vfct_bios(adev)) {464dev_info(adev->dev, "Fetched VBIOS from VFCT\n");465goto success;466}467468/* this is required for SR-IOV */469if (amdgpu_read_bios_from_vram(adev)) {470dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");471goto success;472}473474if (amdgpu_prefer_rom_resource(adev)) {475if (amdgpu_read_bios(adev)) {476dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");477goto success;478}479480if (amdgpu_read_platform_bios(adev)) {481dev_info(adev->dev, "Fetched VBIOS from platform\n");482goto success;483}484485} else {486if (amdgpu_read_platform_bios(adev)) {487dev_info(adev->dev, "Fetched VBIOS from platform\n");488goto success;489}490491if (amdgpu_read_bios(adev)) {492dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");493goto success;494}495}496497if (amdgpu_read_bios_from_rom(adev)) {498dev_info(adev->dev, "Fetched VBIOS from ROM\n");499goto success;500}501502if (amdgpu_read_disabled_bios(adev)) {503dev_info(adev->dev, "Fetched VBIOS from disabled ROM BAR\n");504goto success;505}506507dev_err(adev->dev, "Unable to locate a BIOS ROM\n");508return false;509510success:511return true;512}513514bool amdgpu_get_bios(struct amdgpu_device *adev)515{516bool found;517518if (adev->flags & AMD_IS_APU)519found = amdgpu_get_bios_apu(adev);520else521found = amdgpu_get_bios_dgpu(adev);522523if (found)524adev->is_atom_fw = adev->asic_type >= CHIP_VEGA10;525526return found;527}528529/* helper function for soc15 and onwards to read bios from rom */530bool amdgpu_soc15_read_bios_from_rom(struct amdgpu_device *adev,531u8 *bios, u32 length_bytes)532{533u32 *dw_ptr;534u32 i, length_dw;535u32 rom_offset;536u32 rom_index_offset;537u32 rom_data_offset;538539if (bios == NULL)540return false;541if (length_bytes == 0)542return false;543/* APU vbios image is part of sbios image */544if (adev->flags & AMD_IS_APU)545return false;546if (!adev->smuio.funcs ||547!adev->smuio.funcs->get_rom_index_offset ||548!adev->smuio.funcs->get_rom_data_offset)549return false;550551dw_ptr = (u32 *)bios;552length_dw = ALIGN(length_bytes, 4) / 4;553554rom_index_offset =555adev->smuio.funcs->get_rom_index_offset(adev);556rom_data_offset =557adev->smuio.funcs->get_rom_data_offset(adev);558559if (adev->nbio.funcs &&560adev->nbio.funcs->get_rom_offset) {561rom_offset = adev->nbio.funcs->get_rom_offset(adev);562rom_offset = rom_offset << 17;563} else {564rom_offset = 0;565}566567/* set rom index to rom_offset */568WREG32(rom_index_offset, rom_offset);569/* read out the rom data */570for (i = 0; i < length_dw; i++)571dw_ptr[i] = RREG32(rom_data_offset);572573return true;574}575576577