On some EFI systems, the video BIOS is provided by the EFI firmware. The boot stub code stores the physical address of the ROM image in pdev->rom. Currently we attempt to access this pointer using phys_to_virt(), which doesn't work with CONFIG_HIGHMEM. On these systems, attempting to load the radeon module on a x86_32 kernel can result in the following: BUG: unable to handle page fault for address: 3e8ed03c #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page *pde = 00000000 Oops: 0000 [#1] PREEMPT SMP CPU: 0 PID: 317 Comm: systemd-udevd Not tainted 5.6.0-rc3-next-20200228 #2 Hardware name: Apple Computer, Inc. MacPro1,1/Mac-F4208DC8, BIOS MP11.88Z.005C.B08.0707021221 07/02/07 EIP: radeon_get_bios+0x5ed/0xe50 [radeon] Code: 00 00 84 c0 0f 85 12 fd ff ff c7 87 64 01 00 00 00 00 00 00 8b 47 08 8b 55 b0 e8 1e 83 e1 d6 85 c0 74 1a 8b 55 c0 85 d2 74 13 <80> 38 55 75 0e 80 78 01 aa 0f 84 a4 03 00 00 8d 74 26 00 68 dc 06 EAX: 3e8ed03c EBX: 00000000 ECX: 3e8ed03c EDX: 00010000 ESI: 00040000 EDI: eec04000 EBP: eef3fc60 ESP: eef3fbe0 DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010206 CR0: 80050033 CR2: 3e8ed03c CR3: 2ec77000 CR4: 000006d0 Call Trace: r520_init+0x26/0x240 [radeon] radeon_device_init+0x533/0xa50 [radeon] radeon_driver_load_kms+0x80/0x220 [radeon] drm_dev_register+0xa7/0x180 [drm] radeon_pci_probe+0x10f/0x1a0 [radeon] pci_device_probe+0xd4/0x140 Fix the issue by updating all drivers which can access a platform provided ROM. Instead of calling the helper function pci_platform_rom() which uses phys_to_virt(), call ioremap() directly on the pdev->rom. radeon_read_platform_bios() previously directly accessed an __iomem pointer. Avoid this by calling memcpy_fromio() instead of kmemdup(). pci_platform_rom() now has no remaining callers, so remove it. Link: https://lore.kernel.org/r/20200319021623.5426-1-mikel@mikelr.com Signed-off-by: Mikel Rychliski <mikel@mikelr.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Acked-by: Alex Deucher <alexander.deucher@amd.com>
		
			
				
	
	
		
			448 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			448 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 * Copyright 2008 Advanced Micro Devices, Inc.
 | 
						|
 * Copyright 2008 Red Hat Inc.
 | 
						|
 * Copyright 2009 Jerome Glisse.
 | 
						|
 *
 | 
						|
 * Permission is hereby granted, free of charge, to any person obtaining a
 | 
						|
 * copy of this software and associated documentation files (the "Software"),
 | 
						|
 * to deal in the Software without restriction, including without limitation
 | 
						|
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 | 
						|
 * and/or sell copies of the Software, and to permit persons to whom the
 | 
						|
 * Software is furnished to do so, subject to the following conditions:
 | 
						|
 *
 | 
						|
 * The above copyright notice and this permission notice shall be included in
 | 
						|
 * all copies or substantial portions of the Software.
 | 
						|
 *
 | 
						|
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
						|
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
						|
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 | 
						|
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 | 
						|
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 | 
						|
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 | 
						|
 * OTHER DEALINGS IN THE SOFTWARE.
 | 
						|
 *
 | 
						|
 * Authors: Dave Airlie
 | 
						|
 *          Alex Deucher
 | 
						|
 *          Jerome Glisse
 | 
						|
 */
 | 
						|
 | 
						|
#include "amdgpu.h"
 | 
						|
#include "atom.h"
 | 
						|
 | 
						|
#include <linux/pci.h>
 | 
						|
#include <linux/slab.h>
 | 
						|
#include <linux/acpi.h>
 | 
						|
/*
 | 
						|
 * BIOS.
 | 
						|
 */
 | 
						|
 | 
						|
#define AMD_VBIOS_SIGNATURE " 761295520"
 | 
						|
#define AMD_VBIOS_SIGNATURE_OFFSET 0x30
 | 
						|
#define AMD_VBIOS_SIGNATURE_SIZE sizeof(AMD_VBIOS_SIGNATURE)
 | 
						|
#define AMD_VBIOS_SIGNATURE_END (AMD_VBIOS_SIGNATURE_OFFSET + AMD_VBIOS_SIGNATURE_SIZE)
 | 
						|
#define AMD_IS_VALID_VBIOS(p) ((p)[0] == 0x55 && (p)[1] == 0xAA)
 | 
						|
#define AMD_VBIOS_LENGTH(p) ((p)[2] << 9)
 | 
						|
 | 
						|
/* Check if current bios is an ATOM BIOS.
 | 
						|
 * Return true if it is ATOM BIOS. Otherwise, return false.
 | 
						|
 */
 | 
						|
static bool check_atom_bios(uint8_t *bios, size_t size)
 | 
						|
{
 | 
						|
	uint16_t tmp, bios_header_start;
 | 
						|
 | 
						|
	if (!bios || size < 0x49) {
 | 
						|
		DRM_INFO("vbios mem is null or mem size is wrong\n");
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	if (!AMD_IS_VALID_VBIOS(bios)) {
 | 
						|
		DRM_INFO("BIOS signature incorrect %x %x\n", bios[0], bios[1]);
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	bios_header_start = bios[0x48] | (bios[0x49] << 8);
 | 
						|
	if (!bios_header_start) {
 | 
						|
		DRM_INFO("Can't locate bios header\n");
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	tmp = bios_header_start + 4;
 | 
						|
	if (size < tmp) {
 | 
						|
		DRM_INFO("BIOS header is broken\n");
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	if (!memcmp(bios + tmp, "ATOM", 4) ||
 | 
						|
	    !memcmp(bios + tmp, "MOTA", 4)) {
 | 
						|
		DRM_DEBUG("ATOMBIOS detected\n");
 | 
						|
		return true;
 | 
						|
	}
 | 
						|
 | 
						|
	return false;
 | 
						|
}
 | 
						|
 | 
						|
/* If you boot an IGP board with a discrete card as the primary,
 | 
						|
 * the IGP rom is not accessible via the rom bar as the IGP rom is
 | 
						|
 * part of the system bios.  On boot, the system bios puts a
 | 
						|
 * copy of the igp rom at the start of vram if a discrete card is
 | 
						|
 * present.
 | 
						|
 */
 | 
						|
static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	uint8_t __iomem *bios;
 | 
						|
	resource_size_t vram_base;
 | 
						|
	resource_size_t size = 256 * 1024; /* ??? */
 | 
						|
 | 
						|
	if (!(adev->flags & AMD_IS_APU))
 | 
						|
		if (amdgpu_device_need_post(adev))
 | 
						|
			return false;
 | 
						|
 | 
						|
	adev->bios = NULL;
 | 
						|
	vram_base = pci_resource_start(adev->pdev, 0);
 | 
						|
	bios = ioremap_wc(vram_base, size);
 | 
						|
	if (!bios) {
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	adev->bios = kmalloc(size, GFP_KERNEL);
 | 
						|
	if (!adev->bios) {
 | 
						|
		iounmap(bios);
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
	adev->bios_size = size;
 | 
						|
	memcpy_fromio(adev->bios, bios, size);
 | 
						|
	iounmap(bios);
 | 
						|
 | 
						|
	if (!check_atom_bios(adev->bios, size)) {
 | 
						|
		kfree(adev->bios);
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	return true;
 | 
						|
}
 | 
						|
 | 
						|
bool amdgpu_read_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	uint8_t __iomem *bios;
 | 
						|
	size_t size;
 | 
						|
 | 
						|
	adev->bios = NULL;
 | 
						|
	/* XXX: some cards may return 0 for rom size? ddx has a workaround */
 | 
						|
	bios = pci_map_rom(adev->pdev, &size);
 | 
						|
	if (!bios) {
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	adev->bios = kzalloc(size, GFP_KERNEL);
 | 
						|
	if (adev->bios == NULL) {
 | 
						|
		pci_unmap_rom(adev->pdev, bios);
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
	adev->bios_size = size;
 | 
						|
	memcpy_fromio(adev->bios, bios, size);
 | 
						|
	pci_unmap_rom(adev->pdev, bios);
 | 
						|
 | 
						|
	if (!check_atom_bios(adev->bios, size)) {
 | 
						|
		kfree(adev->bios);
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	return true;
 | 
						|
}
 | 
						|
 | 
						|
static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0};
 | 
						|
	int len;
 | 
						|
 | 
						|
	if (!adev->asic_funcs->read_bios_from_rom)
 | 
						|
		return false;
 | 
						|
 | 
						|
	/* validate VBIOS signature */
 | 
						|
	if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false)
 | 
						|
		return false;
 | 
						|
	header[AMD_VBIOS_SIGNATURE_END] = 0;
 | 
						|
 | 
						|
	if ((!AMD_IS_VALID_VBIOS(header)) ||
 | 
						|
	    0 != memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET],
 | 
						|
			AMD_VBIOS_SIGNATURE,
 | 
						|
			strlen(AMD_VBIOS_SIGNATURE)))
 | 
						|
		return false;
 | 
						|
 | 
						|
	/* valid vbios, go on */
 | 
						|
	len = AMD_VBIOS_LENGTH(header);
 | 
						|
	len = ALIGN(len, 4);
 | 
						|
	adev->bios = kmalloc(len, GFP_KERNEL);
 | 
						|
	if (!adev->bios) {
 | 
						|
		DRM_ERROR("no memory to allocate for BIOS\n");
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
	adev->bios_size = len;
 | 
						|
 | 
						|
	/* read complete BIOS */
 | 
						|
	amdgpu_asic_read_bios_from_rom(adev, adev->bios, len);
 | 
						|
 | 
						|
	if (!check_atom_bios(adev->bios, len)) {
 | 
						|
		kfree(adev->bios);
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	return true;
 | 
						|
}
 | 
						|
 | 
						|
static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	phys_addr_t rom = adev->pdev->rom;
 | 
						|
	size_t romlen = adev->pdev->romlen;
 | 
						|
	void __iomem *bios;
 | 
						|
 | 
						|
	adev->bios = NULL;
 | 
						|
 | 
						|
	if (!rom || romlen == 0)
 | 
						|
		return false;
 | 
						|
 | 
						|
	adev->bios = kzalloc(romlen, GFP_KERNEL);
 | 
						|
	if (!adev->bios)
 | 
						|
		return false;
 | 
						|
 | 
						|
	bios = ioremap(rom, romlen);
 | 
						|
	if (!bios)
 | 
						|
		goto free_bios;
 | 
						|
 | 
						|
	memcpy_fromio(adev->bios, bios, romlen);
 | 
						|
	iounmap(bios);
 | 
						|
 | 
						|
	if (!check_atom_bios(adev->bios, romlen))
 | 
						|
		goto free_bios;
 | 
						|
 | 
						|
	adev->bios_size = romlen;
 | 
						|
 | 
						|
	return true;
 | 
						|
free_bios:
 | 
						|
	kfree(adev->bios);
 | 
						|
	return false;
 | 
						|
}
 | 
						|
 | 
						|
#ifdef CONFIG_ACPI
 | 
						|
/* ATRM is used to get the BIOS on the discrete cards in
 | 
						|
 * dual-gpu systems.
 | 
						|
 */
 | 
						|
/* retrieve the ROM in 4k blocks */
 | 
						|
#define ATRM_BIOS_PAGE 4096
 | 
						|
/**
 | 
						|
 * amdgpu_atrm_call - fetch a chunk of the vbios
 | 
						|
 *
 | 
						|
 * @atrm_handle: acpi ATRM handle
 | 
						|
 * @bios: vbios image pointer
 | 
						|
 * @offset: offset of vbios image data to fetch
 | 
						|
 * @len: length of vbios image data to fetch
 | 
						|
 *
 | 
						|
 * Executes ATRM to fetch a chunk of the discrete
 | 
						|
 * vbios image on PX systems (all asics).
 | 
						|
 * Returns the length of the buffer fetched.
 | 
						|
 */
 | 
						|
static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
 | 
						|
			    int offset, int len)
 | 
						|
{
 | 
						|
	acpi_status status;
 | 
						|
	union acpi_object atrm_arg_elements[2], *obj;
 | 
						|
	struct acpi_object_list atrm_arg;
 | 
						|
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
 | 
						|
 | 
						|
	atrm_arg.count = 2;
 | 
						|
	atrm_arg.pointer = &atrm_arg_elements[0];
 | 
						|
 | 
						|
	atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
 | 
						|
	atrm_arg_elements[0].integer.value = offset;
 | 
						|
 | 
						|
	atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
 | 
						|
	atrm_arg_elements[1].integer.value = len;
 | 
						|
 | 
						|
	status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
 | 
						|
	if (ACPI_FAILURE(status)) {
 | 
						|
		printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
 | 
						|
		return -ENODEV;
 | 
						|
	}
 | 
						|
 | 
						|
	obj = (union acpi_object *)buffer.pointer;
 | 
						|
	memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
 | 
						|
	len = obj->buffer.length;
 | 
						|
	kfree(buffer.pointer);
 | 
						|
	return len;
 | 
						|
}
 | 
						|
 | 
						|
static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	int ret;
 | 
						|
	int size = 256 * 1024;
 | 
						|
	int i;
 | 
						|
	struct pci_dev *pdev = NULL;
 | 
						|
	acpi_handle dhandle, atrm_handle;
 | 
						|
	acpi_status status;
 | 
						|
	bool found = false;
 | 
						|
 | 
						|
	/* ATRM is for the discrete card only */
 | 
						|
	if (adev->flags & AMD_IS_APU)
 | 
						|
		return false;
 | 
						|
 | 
						|
	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
 | 
						|
		dhandle = ACPI_HANDLE(&pdev->dev);
 | 
						|
		if (!dhandle)
 | 
						|
			continue;
 | 
						|
 | 
						|
		status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
 | 
						|
		if (!ACPI_FAILURE(status)) {
 | 
						|
			found = true;
 | 
						|
			break;
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	if (!found) {
 | 
						|
		while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
 | 
						|
			dhandle = ACPI_HANDLE(&pdev->dev);
 | 
						|
			if (!dhandle)
 | 
						|
				continue;
 | 
						|
 | 
						|
			status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
 | 
						|
			if (!ACPI_FAILURE(status)) {
 | 
						|
				found = true;
 | 
						|
				break;
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	if (!found)
 | 
						|
		return false;
 | 
						|
 | 
						|
	adev->bios = kmalloc(size, GFP_KERNEL);
 | 
						|
	if (!adev->bios) {
 | 
						|
		DRM_ERROR("Unable to allocate bios\n");
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
 | 
						|
		ret = amdgpu_atrm_call(atrm_handle,
 | 
						|
				       adev->bios,
 | 
						|
				       (i * ATRM_BIOS_PAGE),
 | 
						|
				       ATRM_BIOS_PAGE);
 | 
						|
		if (ret < ATRM_BIOS_PAGE)
 | 
						|
			break;
 | 
						|
	}
 | 
						|
 | 
						|
	if (!check_atom_bios(adev->bios, size)) {
 | 
						|
		kfree(adev->bios);
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
	adev->bios_size = size;
 | 
						|
	return true;
 | 
						|
}
 | 
						|
#else
 | 
						|
static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	return false;
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	if (adev->flags & AMD_IS_APU)
 | 
						|
		return igp_read_bios_from_vram(adev);
 | 
						|
	else
 | 
						|
		return amdgpu_asic_read_disabled_bios(adev);
 | 
						|
}
 | 
						|
 | 
						|
#ifdef CONFIG_ACPI
 | 
						|
static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	struct acpi_table_header *hdr;
 | 
						|
	acpi_size tbl_size;
 | 
						|
	UEFI_ACPI_VFCT *vfct;
 | 
						|
	unsigned offset;
 | 
						|
 | 
						|
	if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))
 | 
						|
		return false;
 | 
						|
	tbl_size = hdr->length;
 | 
						|
	if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
 | 
						|
		DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n");
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	vfct = (UEFI_ACPI_VFCT *)hdr;
 | 
						|
	offset = vfct->VBIOSImageOffset;
 | 
						|
 | 
						|
	while (offset < tbl_size) {
 | 
						|
		GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);
 | 
						|
		VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;
 | 
						|
 | 
						|
		offset += sizeof(VFCT_IMAGE_HEADER);
 | 
						|
		if (offset > tbl_size) {
 | 
						|
			DRM_ERROR("ACPI VFCT image header truncated\n");
 | 
						|
			return false;
 | 
						|
		}
 | 
						|
 | 
						|
		offset += vhdr->ImageLength;
 | 
						|
		if (offset > tbl_size) {
 | 
						|
			DRM_ERROR("ACPI VFCT image truncated\n");
 | 
						|
			return false;
 | 
						|
		}
 | 
						|
 | 
						|
		if (vhdr->ImageLength &&
 | 
						|
		    vhdr->PCIBus == adev->pdev->bus->number &&
 | 
						|
		    vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&
 | 
						|
		    vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) &&
 | 
						|
		    vhdr->VendorID == adev->pdev->vendor &&
 | 
						|
		    vhdr->DeviceID == adev->pdev->device) {
 | 
						|
			adev->bios = kmemdup(&vbios->VbiosContent,
 | 
						|
					     vhdr->ImageLength,
 | 
						|
					     GFP_KERNEL);
 | 
						|
 | 
						|
			if (!check_atom_bios(adev->bios, vhdr->ImageLength)) {
 | 
						|
				kfree(adev->bios);
 | 
						|
				return false;
 | 
						|
			}
 | 
						|
			adev->bios_size = vhdr->ImageLength;
 | 
						|
			return true;
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n");
 | 
						|
	return false;
 | 
						|
}
 | 
						|
#else
 | 
						|
static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	return false;
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
bool amdgpu_get_bios(struct amdgpu_device *adev)
 | 
						|
{
 | 
						|
	if (amdgpu_atrm_get_bios(adev))
 | 
						|
		goto success;
 | 
						|
 | 
						|
	if (amdgpu_acpi_vfct_bios(adev))
 | 
						|
		goto success;
 | 
						|
 | 
						|
	if (igp_read_bios_from_vram(adev))
 | 
						|
		goto success;
 | 
						|
 | 
						|
	if (amdgpu_read_bios(adev))
 | 
						|
		goto success;
 | 
						|
 | 
						|
	if (amdgpu_read_bios_from_rom(adev))
 | 
						|
		goto success;
 | 
						|
 | 
						|
	if (amdgpu_read_disabled_bios(adev))
 | 
						|
		goto success;
 | 
						|
 | 
						|
	if (amdgpu_read_platform_bios(adev))
 | 
						|
		goto success;
 | 
						|
 | 
						|
	DRM_ERROR("Unable to locate a BIOS ROM\n");
 | 
						|
	return false;
 | 
						|
 | 
						|
success:
 | 
						|
	adev->is_atom_fw = (adev->asic_type >= CHIP_VEGA10) ? true : false;
 | 
						|
	return true;
 | 
						|
}
 |