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