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