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 29 #include "amdgpu.h" 30 #include "atom.h" 31 32 #include <linux/device.h> 33 #include <linux/pci.h> 34 #include <linux/slab.h> 35 #include <linux/acpi.h> 36 37 #if defined(__amd64__) || defined(__i386__) 38 #include <dev/isa/isareg.h> 39 #include <dev/isa/isavar.h> 40 #endif 41 42 /* 43 * BIOS. 44 */ 45 46 #define AMD_VBIOS_SIGNATURE " 761295520" 47 #define AMD_VBIOS_SIGNATURE_OFFSET 0x30 48 #define AMD_VBIOS_SIGNATURE_SIZE sizeof(AMD_VBIOS_SIGNATURE) 49 #define AMD_VBIOS_SIGNATURE_END (AMD_VBIOS_SIGNATURE_OFFSET + AMD_VBIOS_SIGNATURE_SIZE) 50 #define AMD_IS_VALID_VBIOS(p) ((p)[0] == 0x55 && (p)[1] == 0xAA) 51 #define AMD_VBIOS_LENGTH(p) ((p)[2] << 9) 52 53 /* Check if current bios is an ATOM BIOS. 54 * Return true if it is ATOM BIOS. Otherwise, return false. 55 */ 56 static bool check_atom_bios(uint8_t *bios, size_t size) 57 { 58 uint16_t tmp, bios_header_start; 59 60 if (!bios || size < 0x49) { 61 DRM_INFO("vbios mem is null or mem size is wrong\n"); 62 return false; 63 } 64 65 if (!AMD_IS_VALID_VBIOS(bios)) { 66 DRM_INFO("BIOS signature incorrect %x %x\n", bios[0], bios[1]); 67 return false; 68 } 69 70 bios_header_start = bios[0x48] | (bios[0x49] << 8); 71 if (!bios_header_start) { 72 DRM_INFO("Can't locate bios header\n"); 73 return false; 74 } 75 76 tmp = bios_header_start + 4; 77 if (size < tmp) { 78 DRM_INFO("BIOS header is broken\n"); 79 return false; 80 } 81 82 if (!memcmp(bios + tmp, "ATOM", 4) || 83 !memcmp(bios + tmp, "MOTA", 4)) { 84 DRM_DEBUG("ATOMBIOS detected\n"); 85 return true; 86 } 87 88 return false; 89 } 90 91 /* If you boot an IGP board with a discrete card as the primary, 92 * the IGP rom is not accessible via the rom bar as the IGP rom is 93 * part of the system bios. On boot, the system bios puts a 94 * copy of the igp rom at the start of vram if a discrete card is 95 * present. 96 */ 97 #ifdef __linux__ 98 static bool igp_read_bios_from_vram(struct amdgpu_device *adev) 99 { 100 uint8_t __iomem *bios; 101 resource_size_t vram_base; 102 resource_size_t size = 256 * 1024; /* ??? */ 103 104 if (!(adev->flags & AMD_IS_APU)) 105 if (amdgpu_device_need_post(adev)) 106 return false; 107 108 /* FB BAR not enabled */ 109 if (pci_resource_len(adev->pdev, 0) == 0) 110 return false; 111 112 adev->bios = NULL; 113 vram_base = pci_resource_start(adev->pdev, 0); 114 bios = ioremap_wc(vram_base, size); 115 if (!bios) 116 return false; 117 118 adev->bios = kmalloc(size, GFP_KERNEL); 119 if (!adev->bios) { 120 iounmap(bios); 121 return false; 122 } 123 adev->bios_size = size; 124 memcpy_fromio(adev->bios, bios, size); 125 iounmap(bios); 126 127 if (!check_atom_bios(adev->bios, size)) { 128 kfree(adev->bios); 129 return false; 130 } 131 132 return true; 133 } 134 #else 135 static bool igp_read_bios_from_vram(struct amdgpu_device *adev) 136 { 137 uint8_t __iomem *bios; 138 resource_size_t size = 256 * 1024; /* ??? */ 139 bus_space_handle_t bsh; 140 bus_space_tag_t bst = adev->memt; 141 142 if (!(adev->flags & AMD_IS_APU)) 143 if (amdgpu_device_need_post(adev)) 144 return false; 145 146 adev->bios = NULL; 147 148 if (bus_space_map(bst, adev->fb_aper_offset, size, BUS_SPACE_MAP_LINEAR, &bsh) != 0) 149 return false; 150 151 bios = bus_space_vaddr(adev->memt, bsh); 152 if (bios == NULL) { 153 bus_space_unmap(bst, bsh, size); 154 return false; 155 } 156 157 adev->bios = kmalloc(size, GFP_KERNEL); 158 if (!adev->bios) { 159 bus_space_unmap(bst, bsh, size); 160 return false; 161 } 162 adev->bios_size = size; 163 memcpy_fromio(adev->bios, bios, size); 164 bus_space_unmap(bst, bsh, size); 165 166 if (!check_atom_bios(adev->bios, size)) { 167 kfree(adev->bios); 168 return false; 169 } 170 171 return true; 172 } 173 #endif 174 175 #ifdef __linux__ 176 bool amdgpu_read_bios(struct amdgpu_device *adev) 177 { 178 uint8_t __iomem *bios; 179 size_t size; 180 181 adev->bios = NULL; 182 /* XXX: some cards may return 0 for rom size? ddx has a workaround */ 183 bios = pci_map_rom(adev->pdev, &size); 184 if (!bios) 185 return false; 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 204 bool amdgpu_read_bios(struct amdgpu_device *adev) 205 { 206 size_t size; 207 pcireg_t address, mask; 208 bus_space_handle_t romh; 209 int rc; 210 211 adev->bios = NULL; 212 /* XXX: some cards may return 0 for rom size? ddx has a workaround */ 213 214 address = pci_conf_read(adev->pc, adev->pa_tag, PCI_ROM_REG); 215 pci_conf_write(adev->pc, adev->pa_tag, PCI_ROM_REG, ~PCI_ROM_ENABLE); 216 mask = pci_conf_read(adev->pc, adev->pa_tag, PCI_ROM_REG); 217 address |= PCI_ROM_ENABLE; 218 pci_conf_write(adev->pc, adev->pa_tag, PCI_ROM_REG, address); 219 220 size = PCI_ROM_SIZE(mask); 221 if (size == 0) 222 return false; 223 rc = bus_space_map(adev->memt, PCI_ROM_ADDR(address), size, 0, &romh); 224 if (rc != 0) { 225 printf(": can't map PCI ROM (%d)\n", rc); 226 return false; 227 } 228 229 adev->bios = kzalloc(size, GFP_KERNEL); 230 adev->bios_size = size; 231 bus_space_read_region_1(adev->memt, romh, 0, adev->bios, size); 232 bus_space_unmap(adev->memt, romh, size); 233 234 if (!check_atom_bios(adev->bios, size)) { 235 kfree(adev->bios); 236 return false; 237 } 238 239 return true; 240 } 241 #endif 242 243 static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev) 244 { 245 u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0}; 246 int len; 247 248 if (!adev->asic_funcs || !adev->asic_funcs->read_bios_from_rom) 249 return false; 250 251 /* validate VBIOS signature */ 252 if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false) 253 return false; 254 header[AMD_VBIOS_SIGNATURE_END] = 0; 255 256 if ((!AMD_IS_VALID_VBIOS(header)) || 257 memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET], 258 AMD_VBIOS_SIGNATURE, 259 strlen(AMD_VBIOS_SIGNATURE)) != 0) 260 return false; 261 262 /* valid vbios, go on */ 263 len = AMD_VBIOS_LENGTH(header); 264 len = ALIGN(len, 4); 265 adev->bios = kmalloc(len, GFP_KERNEL); 266 if (!adev->bios) { 267 DRM_ERROR("no memory to allocate for BIOS\n"); 268 return false; 269 } 270 adev->bios_size = len; 271 272 /* read complete BIOS */ 273 amdgpu_asic_read_bios_from_rom(adev, adev->bios, len); 274 275 if (!check_atom_bios(adev->bios, len)) { 276 kfree(adev->bios); 277 return false; 278 } 279 280 return true; 281 } 282 283 #ifdef __linux__ 284 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev) 285 { 286 phys_addr_t rom = adev->pdev->rom; 287 size_t romlen = adev->pdev->romlen; 288 void __iomem *bios; 289 290 adev->bios = NULL; 291 292 if (!rom || romlen == 0) 293 return false; 294 295 adev->bios = kzalloc(romlen, GFP_KERNEL); 296 if (!adev->bios) 297 return false; 298 299 bios = ioremap(rom, romlen); 300 if (!bios) 301 goto free_bios; 302 303 memcpy_fromio(adev->bios, bios, romlen); 304 iounmap(bios); 305 306 if (!check_atom_bios(adev->bios, romlen)) 307 goto free_bios; 308 309 adev->bios_size = romlen; 310 311 return true; 312 free_bios: 313 kfree(adev->bios); 314 return false; 315 } 316 #else 317 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev) 318 { 319 #if defined(__amd64__) || defined(__i386__) 320 uint8_t __iomem *bios; 321 bus_size_t size = 256 * 1024; /* ??? */ 322 323 adev->bios = NULL; 324 325 bios = (u8 *)ISA_HOLE_VADDR(0xc0000); 326 327 adev->bios = kzalloc(size, GFP_KERNEL); 328 if (adev->bios == NULL) 329 return false; 330 331 memcpy_fromio(adev->bios, bios, size); 332 333 if (!check_atom_bios(adev->bios, size)) { 334 kfree(adev->bios); 335 return false; 336 } 337 338 adev->bios_size = size; 339 340 return true; 341 #endif 342 return false; 343 } 344 #endif 345 346 #ifdef CONFIG_ACPI 347 /* ATRM is used to get the BIOS on the discrete cards in 348 * dual-gpu systems. 349 */ 350 /* retrieve the ROM in 4k blocks */ 351 #define ATRM_BIOS_PAGE 4096 352 /** 353 * amdgpu_atrm_call - fetch a chunk of the vbios 354 * 355 * @atrm_handle: acpi ATRM handle 356 * @bios: vbios image pointer 357 * @offset: offset of vbios image data to fetch 358 * @len: length of vbios image data to fetch 359 * 360 * Executes ATRM to fetch a chunk of the discrete 361 * vbios image on PX systems (all asics). 362 * Returns the length of the buffer fetched. 363 */ 364 static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios, 365 int offset, int len) 366 { 367 acpi_status status; 368 union acpi_object atrm_arg_elements[2], *obj; 369 struct acpi_object_list atrm_arg; 370 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL}; 371 372 atrm_arg.count = 2; 373 atrm_arg.pointer = &atrm_arg_elements[0]; 374 375 atrm_arg_elements[0].type = ACPI_TYPE_INTEGER; 376 atrm_arg_elements[0].integer.value = offset; 377 378 atrm_arg_elements[1].type = ACPI_TYPE_INTEGER; 379 atrm_arg_elements[1].integer.value = len; 380 381 status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer); 382 if (ACPI_FAILURE(status)) { 383 DRM_ERROR("failed to evaluate ATRM got %s\n", acpi_format_exception(status)); 384 return -ENODEV; 385 } 386 387 obj = (union acpi_object *)buffer.pointer; 388 memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length); 389 len = obj->buffer.length; 390 kfree(buffer.pointer); 391 return len; 392 } 393 394 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 395 { 396 int ret; 397 int size = 256 * 1024; 398 int i; 399 struct pci_dev *pdev = NULL; 400 acpi_handle dhandle, atrm_handle; 401 acpi_status status; 402 bool found = false; 403 404 /* ATRM is for the discrete card only */ 405 if (adev->flags & AMD_IS_APU) 406 return false; 407 408 /* ATRM is for on-platform devices only */ 409 if (dev_is_removable(&adev->pdev->dev)) 410 return false; 411 412 #ifdef notyet 413 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { 414 dhandle = ACPI_HANDLE(&pdev->dev); 415 if (!dhandle) 416 continue; 417 418 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 419 if (ACPI_SUCCESS(status)) { 420 found = true; 421 break; 422 } 423 } 424 425 if (!found) { 426 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) { 427 dhandle = ACPI_HANDLE(&pdev->dev); 428 if (!dhandle) 429 continue; 430 431 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 432 if (ACPI_SUCCESS(status)) { 433 found = true; 434 break; 435 } 436 } 437 } 438 #else 439 { 440 pdev = adev->pdev; 441 dhandle = ACPI_HANDLE(&pdev->dev); 442 443 if (dhandle) { 444 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 445 if (ACPI_SUCCESS(status)) { 446 found = true; 447 } 448 } 449 } 450 #endif 451 452 if (!found) 453 return false; 454 pci_dev_put(pdev); 455 456 adev->bios = kmalloc(size, GFP_KERNEL); 457 if (!adev->bios) { 458 dev_err(adev->dev, "Unable to allocate bios\n"); 459 return false; 460 } 461 462 for (i = 0; i < size / ATRM_BIOS_PAGE; i++) { 463 ret = amdgpu_atrm_call(atrm_handle, 464 adev->bios, 465 (i * ATRM_BIOS_PAGE), 466 ATRM_BIOS_PAGE); 467 if (ret < ATRM_BIOS_PAGE) 468 break; 469 } 470 471 if (!check_atom_bios(adev->bios, size)) { 472 kfree(adev->bios); 473 return false; 474 } 475 adev->bios_size = size; 476 return true; 477 } 478 #else 479 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 480 { 481 return false; 482 } 483 #endif 484 485 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev) 486 { 487 if (adev->flags & AMD_IS_APU) 488 return igp_read_bios_from_vram(adev); 489 else 490 return (!adev->asic_funcs || !adev->asic_funcs->read_disabled_bios) ? 491 false : amdgpu_asic_read_disabled_bios(adev); 492 } 493 494 #ifdef CONFIG_ACPI 495 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 496 { 497 struct acpi_table_header *hdr; 498 acpi_size tbl_size; 499 UEFI_ACPI_VFCT *vfct; 500 unsigned int offset; 501 502 if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr))) 503 return false; 504 tbl_size = hdr->length; 505 if (tbl_size < sizeof(UEFI_ACPI_VFCT)) { 506 dev_info(adev->dev, "ACPI VFCT table present but broken (too short #1),skipping\n"); 507 return false; 508 } 509 510 vfct = (UEFI_ACPI_VFCT *)hdr; 511 offset = vfct->VBIOSImageOffset; 512 513 while (offset < tbl_size) { 514 GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset); 515 VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader; 516 517 offset += sizeof(VFCT_IMAGE_HEADER); 518 if (offset > tbl_size) { 519 dev_info(adev->dev, "ACPI VFCT image header truncated,skipping\n"); 520 return false; 521 } 522 523 offset += vhdr->ImageLength; 524 if (offset > tbl_size) { 525 dev_info(adev->dev, "ACPI VFCT image truncated,skipping\n"); 526 return false; 527 } 528 529 if (vhdr->ImageLength && 530 vhdr->PCIBus == adev->pdev->bus->number && 531 vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) && 532 vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) && 533 vhdr->VendorID == adev->pdev->vendor && 534 vhdr->DeviceID == adev->pdev->device) { 535 adev->bios = kmemdup(&vbios->VbiosContent, 536 vhdr->ImageLength, 537 GFP_KERNEL); 538 539 if (!check_atom_bios(adev->bios, vhdr->ImageLength)) { 540 kfree(adev->bios); 541 return false; 542 } 543 adev->bios_size = vhdr->ImageLength; 544 return true; 545 } 546 } 547 548 dev_info(adev->dev, "ACPI VFCT table present but broken (too short #2),skipping\n"); 549 return false; 550 } 551 #else 552 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 553 { 554 return false; 555 } 556 #endif 557 558 bool amdgpu_get_bios(struct amdgpu_device *adev) 559 { 560 if (amdgpu_atrm_get_bios(adev)) { 561 dev_info(adev->dev, "Fetched VBIOS from ATRM\n"); 562 goto success; 563 } 564 565 if (amdgpu_acpi_vfct_bios(adev)) { 566 dev_info(adev->dev, "Fetched VBIOS from VFCT\n"); 567 goto success; 568 } 569 570 if (igp_read_bios_from_vram(adev)) { 571 dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n"); 572 goto success; 573 } 574 575 if (amdgpu_read_bios(adev)) { 576 dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n"); 577 goto success; 578 } 579 580 if (amdgpu_read_bios_from_rom(adev)) { 581 dev_info(adev->dev, "Fetched VBIOS from ROM\n"); 582 goto success; 583 } 584 585 if (amdgpu_read_disabled_bios(adev)) { 586 dev_info(adev->dev, "Fetched VBIOS from disabled ROM BAR\n"); 587 goto success; 588 } 589 590 if (amdgpu_read_platform_bios(adev)) { 591 dev_info(adev->dev, "Fetched VBIOS from platform\n"); 592 goto success; 593 } 594 595 dev_err(adev->dev, "Unable to locate a BIOS ROM\n"); 596 return false; 597 598 success: 599 adev->is_atom_fw = adev->asic_type >= CHIP_VEGA10; 600 return true; 601 } 602 603 /* helper function for soc15 and onwards to read bios from rom */ 604 bool amdgpu_soc15_read_bios_from_rom(struct amdgpu_device *adev, 605 u8 *bios, u32 length_bytes) 606 { 607 u32 *dw_ptr; 608 u32 i, length_dw; 609 u32 rom_offset; 610 u32 rom_index_offset; 611 u32 rom_data_offset; 612 613 if (bios == NULL) 614 return false; 615 if (length_bytes == 0) 616 return false; 617 /* APU vbios image is part of sbios image */ 618 if (adev->flags & AMD_IS_APU) 619 return false; 620 if (!adev->smuio.funcs || 621 !adev->smuio.funcs->get_rom_index_offset || 622 !adev->smuio.funcs->get_rom_data_offset) 623 return false; 624 625 dw_ptr = (u32 *)bios; 626 length_dw = ALIGN(length_bytes, 4) / 4; 627 628 rom_index_offset = 629 adev->smuio.funcs->get_rom_index_offset(adev); 630 rom_data_offset = 631 adev->smuio.funcs->get_rom_data_offset(adev); 632 633 if (adev->nbio.funcs && 634 adev->nbio.funcs->get_rom_offset) { 635 rom_offset = adev->nbio.funcs->get_rom_offset(adev); 636 rom_offset = rom_offset << 17; 637 } else { 638 rom_offset = 0; 639 } 640 641 /* set rom index to rom_offset */ 642 WREG32(rom_index_offset, rom_offset); 643 /* read out the rom data */ 644 for (i = 0; i < length_dw; i++) 645 dw_ptr[i] = RREG32(rom_data_offset); 646 647 return true; 648 } 649