amdgpu_bios.c revision 1.1
1/* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28#include <drm/drmP.h> 29#include "amdgpu.h" 30#include "atom.h" 31 32#include <linux/slab.h> 33#include <linux/acpi.h> 34 35#if defined(__amd64__) || defined(__i386__) 36#include <dev/isa/isareg.h> 37#include <dev/isa/isavar.h> 38#endif 39 40#ifdef __HAVE_ACPI 41#include "acpi.h" 42#endif 43 44/* 45 * BIOS. 46 */ 47 48#define AMD_VBIOS_SIGNATURE " 761295520" 49#define AMD_VBIOS_SIGNATURE_OFFSET 0x30 50#define AMD_VBIOS_SIGNATURE_SIZE sizeof(AMD_VBIOS_SIGNATURE) 51#define AMD_VBIOS_SIGNATURE_END (AMD_VBIOS_SIGNATURE_OFFSET + AMD_VBIOS_SIGNATURE_SIZE) 52#define AMD_IS_VALID_VBIOS(p) ((p)[0] == 0x55 && (p)[1] == 0xAA) 53#define AMD_VBIOS_LENGTH(p) ((p)[2] << 9) 54 55/* Check if current bios is an ATOM BIOS. 56 * Return true if it is ATOM BIOS. Otherwise, return false. 57 */ 58static bool check_atom_bios(uint8_t *bios, size_t size) 59{ 60 uint16_t tmp, bios_header_start; 61 62 if (!bios || size < 0x49) { 63 DRM_INFO("vbios mem is null or mem size is wrong\n"); 64 return false; 65 } 66 67 if (!AMD_IS_VALID_VBIOS(bios)) { 68 DRM_INFO("BIOS signature incorrect %x %x\n", bios[0], bios[1]); 69 return false; 70 } 71 72 bios_header_start = bios[0x48] | (bios[0x49] << 8); 73 if (!bios_header_start) { 74 DRM_INFO("Can't locate bios header\n"); 75 return false; 76 } 77 78 tmp = bios_header_start + 4; 79 if (size < tmp) { 80 DRM_INFO("BIOS header is broken\n"); 81 return false; 82 } 83 84 if (!memcmp(bios + tmp, "ATOM", 4) || 85 !memcmp(bios + tmp, "MOTA", 4)) { 86 DRM_DEBUG("ATOMBIOS detected\n"); 87 return true; 88 } 89 90 return false; 91} 92 93/* 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 is 95 * part of the system bios. On boot, the system bios puts a 96 * copy of the igp rom at the start of vram if a discrete card is 97 * present. 98 */ 99#ifdef __linux__ 100static bool igp_read_bios_from_vram(struct amdgpu_device *adev) 101{ 102 uint8_t __iomem *bios; 103 resource_size_t vram_base; 104 resource_size_t size = 256 * 1024; /* ??? */ 105 106 if (!(adev->flags & AMD_IS_APU)) 107 if (amdgpu_device_need_post(adev)) 108 return false; 109 110 adev->bios = NULL; 111 vram_base = pci_resource_start(adev->pdev, 0); 112 bios = ioremap_wc(vram_base, size); 113 if (!bios) { 114 return false; 115 } 116 117 adev->bios = kmalloc(size, GFP_KERNEL); 118 if (!adev->bios) { 119 iounmap(bios); 120 return false; 121 } 122 adev->bios_size = size; 123 memcpy_fromio(adev->bios, bios, size); 124 iounmap(bios); 125 126 if (!check_atom_bios(adev->bios, size)) { 127 kfree(adev->bios); 128 return false; 129 } 130 131 return true; 132} 133#else 134static bool igp_read_bios_from_vram(struct amdgpu_device *adev) 135{ 136 uint8_t __iomem *bios; 137 resource_size_t size = 256 * 1024; /* ??? */ 138 bus_space_handle_t bsh; 139 bus_space_tag_t bst = adev->memt; 140 141 if (!(adev->flags & AMD_IS_APU)) 142 if (amdgpu_device_need_post(adev)) 143 return false; 144 145 adev->bios = NULL; 146 147 if (bus_space_map(bst, adev->fb_aper_offset, size, BUS_SPACE_MAP_LINEAR, &bsh) != 0) 148 return false; 149 150 bios = bus_space_vaddr(adev->memt, bsh); 151 if (bios == NULL) { 152 bus_space_unmap(bst, bsh, size); 153 return false; 154 } 155 156 adev->bios = kmalloc(size, GFP_KERNEL); 157 if (!adev->bios) { 158 bus_space_unmap(bst, bsh, size); 159 return false; 160 } 161 adev->bios_size = size; 162 memcpy_fromio(adev->bios, bios, size); 163 bus_space_unmap(bst, bsh, size); 164 165 if (!check_atom_bios(adev->bios, size)) { 166 kfree(adev->bios); 167 return false; 168 } 169 170 return true; 171} 172#endif 173 174#ifdef __linux__ 175bool amdgpu_read_bios(struct amdgpu_device *adev) 176{ 177 uint8_t __iomem *bios; 178 size_t size; 179 180 adev->bios = NULL; 181 /* XXX: some cards may return 0 for rom size? ddx has a workaround */ 182 bios = pci_map_rom(adev->pdev, &size); 183 if (!bios) { 184 return false; 185 } 186 187 adev->bios = kzalloc(size, GFP_KERNEL); 188 if (adev->bios == NULL) { 189 pci_unmap_rom(adev->pdev, bios); 190 return false; 191 } 192 adev->bios_size = size; 193 memcpy_fromio(adev->bios, bios, size); 194 pci_unmap_rom(adev->pdev, bios); 195 196 if (!check_atom_bios(adev->bios, size)) { 197 kfree(adev->bios); 198 return false; 199 } 200 201 return true; 202} 203#else 204bool amdgpu_read_bios(struct amdgpu_device *adev) 205{ 206 uint8_t __iomem *bios; 207 size_t size; 208 pcireg_t address, mask; 209 bus_space_handle_t romh; 210 int rc; 211 212 adev->bios = NULL; 213 /* XXX: some cards may return 0 for rom size? ddx has a workaround */ 214 215 address = pci_conf_read(adev->pc, adev->pa_tag, PCI_ROM_REG); 216 pci_conf_write(adev->pc, adev->pa_tag, PCI_ROM_REG, ~PCI_ROM_ENABLE); 217 mask = pci_conf_read(adev->pc, adev->pa_tag, PCI_ROM_REG); 218 address |= PCI_ROM_ENABLE; 219 pci_conf_write(adev->pc, adev->pa_tag, PCI_ROM_REG, address); 220 221 size = PCI_ROM_SIZE(mask); 222 if (size == 0) 223 return false; 224 rc = bus_space_map(adev->memt, PCI_ROM_ADDR(address), size, 225 BUS_SPACE_MAP_LINEAR, &romh); 226 if (rc != 0) { 227 printf(": can't map PCI ROM (%d)\n", rc); 228 return false; 229 } 230 bios = (uint8_t *)bus_space_vaddr(adev->memt, romh); 231 if (!bios) { 232 printf(": bus_space_vaddr failed\n"); 233 return false; 234 } 235 236 adev->bios = kzalloc(size, GFP_KERNEL); 237 if (adev->bios == NULL) { 238 bus_space_unmap(adev->memt, romh, size); 239 return false; 240 } 241 adev->bios_size = size; 242 memcpy_fromio(adev->bios, bios, size); 243 bus_space_unmap(adev->memt, romh, size); 244 245 if (!check_atom_bios(adev->bios, size)) { 246 kfree(adev->bios); 247 return false; 248 } 249 250 return true; 251} 252#endif 253 254static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev) 255{ 256 u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0}; 257 int len; 258 259 if (!adev->asic_funcs->read_bios_from_rom) 260 return false; 261 262 /* validate VBIOS signature */ 263 if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false) 264 return false; 265 header[AMD_VBIOS_SIGNATURE_END] = 0; 266 267 if ((!AMD_IS_VALID_VBIOS(header)) || 268 0 != memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET], 269 AMD_VBIOS_SIGNATURE, 270 strlen(AMD_VBIOS_SIGNATURE))) 271 return false; 272 273 /* valid vbios, go on */ 274 len = AMD_VBIOS_LENGTH(header); 275 len = roundup2(len, 4); 276 adev->bios = kmalloc(len, GFP_KERNEL); 277 if (!adev->bios) { 278 DRM_ERROR("no memory to allocate for BIOS\n"); 279 return false; 280 } 281 adev->bios_size = len; 282 283 /* read complete BIOS */ 284 amdgpu_asic_read_bios_from_rom(adev, adev->bios, len); 285 286 if (!check_atom_bios(adev->bios, len)) { 287 kfree(adev->bios); 288 return false; 289 } 290 291 return true; 292} 293 294#ifdef __linux__ 295static bool amdgpu_read_platform_bios(struct amdgpu_device *adev) 296{ 297 uint8_t __iomem *bios; 298 size_t size; 299 300 adev->bios = NULL; 301 302 bios = pci_platform_rom(adev->pdev, &size); 303 if (!bios) { 304 return false; 305 } 306 307 adev->bios = kzalloc(size, GFP_KERNEL); 308 if (adev->bios == NULL) 309 return false; 310 311 memcpy_fromio(adev->bios, bios, size); 312 313 if (!check_atom_bios(adev->bios, size)) { 314 kfree(adev->bios); 315 return false; 316 } 317 318 adev->bios_size = size; 319 320 return true; 321} 322#else 323static bool amdgpu_read_platform_bios(struct amdgpu_device *adev) 324{ 325#if defined(__amd64__) || defined(__i386__) 326 uint8_t __iomem *bios; 327 bus_size_t size = 256 * 1024; /* ??? */ 328 329 adev->bios = NULL; 330 331 bios = (u8 *)ISA_HOLE_VADDR(0xc0000); 332 333 adev->bios = kzalloc(size, GFP_KERNEL); 334 if (adev->bios == NULL) 335 return false; 336 337 memcpy_fromio(adev->bios, bios, size); 338 339 if (!check_atom_bios(adev->bios, size)) { 340 kfree(adev->bios); 341 return false; 342 } 343 344 adev->bios_size = size; 345 346 return true; 347#endif 348 return false; 349} 350#endif 351 352#ifdef CONFIG_ACPI 353/* ATRM is used to get the BIOS on the discrete cards in 354 * dual-gpu systems. 355 */ 356/* retrieve the ROM in 4k blocks */ 357#define ATRM_BIOS_PAGE 4096 358/** 359 * amdgpu_atrm_call - fetch a chunk of the vbios 360 * 361 * @atrm_handle: acpi ATRM handle 362 * @bios: vbios image pointer 363 * @offset: offset of vbios image data to fetch 364 * @len: length of vbios image data to fetch 365 * 366 * Executes ATRM to fetch a chunk of the discrete 367 * vbios image on PX systems (all asics). 368 * Returns the length of the buffer fetched. 369 */ 370static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios, 371 int offset, int len) 372{ 373 acpi_status status; 374 union acpi_object atrm_arg_elements[2], *obj; 375 struct acpi_object_list atrm_arg; 376 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL}; 377 378 atrm_arg.count = 2; 379 atrm_arg.pointer = &atrm_arg_elements[0]; 380 381 atrm_arg_elements[0].type = ACPI_TYPE_INTEGER; 382 atrm_arg_elements[0].integer.value = offset; 383 384 atrm_arg_elements[1].type = ACPI_TYPE_INTEGER; 385 atrm_arg_elements[1].integer.value = len; 386 387 status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer); 388 if (ACPI_FAILURE(status)) { 389 printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status)); 390 return -ENODEV; 391 } 392 393 obj = (union acpi_object *)buffer.pointer; 394 memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length); 395 len = obj->buffer.length; 396 kfree(buffer.pointer); 397 return len; 398} 399 400static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 401{ 402 int ret; 403 int size = 256 * 1024; 404 int i; 405 struct pci_dev *pdev = NULL; 406 acpi_handle dhandle, atrm_handle; 407 acpi_status status; 408 bool found = false; 409 410 /* ATRM is for the discrete card only */ 411 if (adev->flags & AMD_IS_APU) 412 return false; 413 414 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { 415 dhandle = ACPI_HANDLE(&pdev->dev); 416 if (!dhandle) 417 continue; 418 419 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 420 if (!ACPI_FAILURE(status)) { 421 found = true; 422 break; 423 } 424 } 425 426 if (!found) { 427 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) { 428 dhandle = ACPI_HANDLE(&pdev->dev); 429 if (!dhandle) 430 continue; 431 432 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 433 if (!ACPI_FAILURE(status)) { 434 found = true; 435 break; 436 } 437 } 438 } 439 440 if (!found) 441 return false; 442 443 adev->bios = kmalloc(size, GFP_KERNEL); 444 if (!adev->bios) { 445 DRM_ERROR("Unable to allocate bios\n"); 446 return false; 447 } 448 449 for (i = 0; i < size / ATRM_BIOS_PAGE; i++) { 450 ret = amdgpu_atrm_call(atrm_handle, 451 adev->bios, 452 (i * ATRM_BIOS_PAGE), 453 ATRM_BIOS_PAGE); 454 if (ret < ATRM_BIOS_PAGE) 455 break; 456 } 457 458 if (!check_atom_bios(adev->bios, size)) { 459 kfree(adev->bios); 460 return false; 461 } 462 adev->bios_size = size; 463 return true; 464} 465#else 466static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 467{ 468 return false; 469} 470#endif 471 472static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev) 473{ 474 if (adev->flags & AMD_IS_APU) 475 return igp_read_bios_from_vram(adev); 476 else 477 return amdgpu_asic_read_disabled_bios(adev); 478} 479 480#if NACPI > 0 481#define CONFIG_ACPI 482#endif 483 484#ifdef CONFIG_ACPI 485static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 486{ 487 struct acpi_table_header *hdr; 488 acpi_size tbl_size; 489 UEFI_ACPI_VFCT *vfct; 490 unsigned offset; 491 492 if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr))) 493 return false; 494 tbl_size = hdr->length; 495 if (tbl_size < sizeof(UEFI_ACPI_VFCT)) { 496 DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n"); 497 return false; 498 } 499 500 vfct = (UEFI_ACPI_VFCT *)hdr; 501 offset = vfct->VBIOSImageOffset; 502 503 while (offset < tbl_size) { 504 GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset); 505 VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader; 506 507 offset += sizeof(VFCT_IMAGE_HEADER); 508 if (offset > tbl_size) { 509 DRM_ERROR("ACPI VFCT image header truncated\n"); 510 return false; 511 } 512 513 offset += vhdr->ImageLength; 514 if (offset > tbl_size) { 515 DRM_ERROR("ACPI VFCT image truncated\n"); 516 return false; 517 } 518 519 if (vhdr->ImageLength && 520 vhdr->PCIBus == adev->pdev->bus->number && 521 vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) && 522 vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) && 523 vhdr->VendorID == adev->pdev->vendor && 524 vhdr->DeviceID == adev->pdev->device) { 525 adev->bios = kmemdup(&vbios->VbiosContent, 526 vhdr->ImageLength, 527 GFP_KERNEL); 528 529 if (!check_atom_bios(adev->bios, vhdr->ImageLength)) { 530 kfree(adev->bios); 531 return false; 532 } 533 adev->bios_size = vhdr->ImageLength; 534 return true; 535 } 536 } 537 538 DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n"); 539 return false; 540} 541#else 542static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 543{ 544 return false; 545} 546#endif 547 548bool amdgpu_get_bios(struct amdgpu_device *adev) 549{ 550 if (amdgpu_atrm_get_bios(adev)) 551 goto success; 552 553 if (amdgpu_acpi_vfct_bios(adev)) 554 goto success; 555 556 if (igp_read_bios_from_vram(adev)) 557 goto success; 558 559 if (amdgpu_read_bios(adev)) 560 goto success; 561 562 if (amdgpu_read_bios_from_rom(adev)) 563 goto success; 564 565 if (amdgpu_read_disabled_bios(adev)) 566 goto success; 567 568 if (amdgpu_read_platform_bios(adev)) 569 goto success; 570 571 DRM_ERROR("Unable to locate a BIOS ROM\n"); 572 return false; 573 574success: 575 adev->is_atom_fw = (adev->asic_type >= CHIP_VEGA10) ? true : false; 576 return true; 577} 578