1 /* $NetBSD: amdgpu_bios.c,v 1.3 2018/08/27 14:04:50 riastradh Exp $ */ 2 3 /* 4 * Copyright 2008 Advanced Micro Devices, Inc. 5 * Copyright 2008 Red Hat Inc. 6 * Copyright 2009 Jerome Glisse. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 * 26 * Authors: Dave Airlie 27 * Alex Deucher 28 * Jerome Glisse 29 */ 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: amdgpu_bios.c,v 1.3 2018/08/27 14:04:50 riastradh Exp $"); 32 33 #include <drm/drmP.h> 34 #include "amdgpu.h" 35 #include "atom.h" 36 37 #include <linux/slab.h> 38 #include <linux/acpi.h> 39 /* 40 * BIOS. 41 */ 42 43 /* If you boot an IGP board with a discrete card as the primary, 44 * the IGP rom is not accessible via the rom bar as the IGP rom is 45 * part of the system bios. On boot, the system bios puts a 46 * copy of the igp rom at the start of vram if a discrete card is 47 * present. 48 */ 49 static bool igp_read_bios_from_vram(struct amdgpu_device *adev) 50 { 51 #ifdef __NetBSD__ 52 bus_space_tag_t bst; 53 bus_space_handle_t bsh; 54 bus_size_t size; 55 #else 56 uint8_t __iomem *bios; 57 resource_size_t vram_base; 58 resource_size_t size = 256 * 1024; /* ??? */ 59 #endif 60 61 if (!(adev->flags & AMD_IS_APU)) 62 if (!amdgpu_card_posted(adev)) 63 return false; 64 65 adev->bios = NULL; 66 #ifdef __NetBSD__ 67 if (pci_mapreg_map(&adev->pdev->pd_pa, PCI_BAR(0), 68 /* XXX Dunno what type to expect here; fill me in... */ 69 pci_mapreg_type(adev->pdev->pd_pa.pa_pc, 70 adev->pdev->pd_pa.pa_tag, PCI_BAR(0)), 71 0, &bst, &bsh, NULL, &size)) 72 return false; 73 if ((size == 0) || 74 (size < 256 * 1024) || 75 (bus_space_read_1(bst, bsh, 0) != 0x55) || 76 (bus_space_read_1(bst, bsh, 1) != 0xaa) || 77 ((adev->bios = kmalloc(size, GFP_KERNEL)) == NULL)) { 78 bus_space_unmap(bst, bsh, size); 79 return false; 80 } 81 bus_space_read_region_1(bst, bsh, 0, adev->bios, size); 82 bus_space_unmap(bst, bsh, size); 83 #else 84 vram_base = pci_resource_start(adev->pdev, 0); 85 bios = ioremap(vram_base, size); 86 if (!bios) { 87 return false; 88 } 89 90 if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) { 91 iounmap(bios); 92 return false; 93 } 94 adev->bios = kmalloc(size, GFP_KERNEL); 95 if (adev->bios == NULL) { 96 iounmap(bios); 97 return false; 98 } 99 memcpy_fromio(adev->bios, bios, size); 100 iounmap(bios); 101 #endif 102 return true; 103 } 104 105 #ifdef __NetBSD__ 106 # define __iomem __pci_rom_iomem 107 #endif 108 109 bool amdgpu_read_bios(struct amdgpu_device *adev) 110 { 111 uint8_t __iomem *bios, val1, val2; 112 size_t size; 113 114 adev->bios = NULL; 115 /* XXX: some cards may return 0 for rom size? ddx has a workaround */ 116 bios = pci_map_rom(adev->pdev, &size); 117 if (!bios) { 118 return false; 119 } 120 121 #ifdef __NetBSD__ 122 const bus_space_tag_t bst = adev->pdev->pd_rom_bst; 123 const bus_space_handle_t bsh = adev->pdev->pd_rom_found_bsh; 124 125 val1 = bus_space_read_1(bst, bsh, 0); 126 val2 = bus_space_read_1(bst, bsh, 1); 127 #else 128 val1 = readb(&bios[0]); 129 val2 = readb(&bios[1]); 130 #endif 131 132 if (size == 0 || val1 != 0x55 || val2 != 0xaa) { 133 pci_unmap_rom(adev->pdev, bios); 134 return false; 135 } 136 adev->bios = kzalloc(size, GFP_KERNEL); 137 if (adev->bios == NULL) { 138 pci_unmap_rom(adev->pdev, bios); 139 return false; 140 } 141 memcpy_fromio(adev->bios, bios, size); 142 pci_unmap_rom(adev->pdev, bios); 143 return true; 144 } 145 146 #ifdef __NetBSD__ 147 # undef __iomem 148 #endif 149 150 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev) 151 { 152 #ifdef __NetBSD__ /* XXX amdgpu platform bios */ 153 return false; 154 #else 155 uint8_t __iomem *bios; 156 size_t size; 157 158 adev->bios = NULL; 159 160 bios = pci_platform_rom(adev->pdev, &size); 161 if (!bios) { 162 return false; 163 } 164 165 if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) { 166 return false; 167 } 168 adev->bios = kmemdup(bios, size, GFP_KERNEL); 169 if (adev->bios == NULL) { 170 return false; 171 } 172 173 return true; 174 #endif /* __NetBSD__ */ 175 } 176 177 /* XXX amdgpu acpi */ 178 #ifdef CONFIG_ACPI 179 /* ATRM is used to get the BIOS on the discrete cards in 180 * dual-gpu systems. 181 */ 182 /* retrieve the ROM in 4k blocks */ 183 #define ATRM_BIOS_PAGE 4096 184 /** 185 * amdgpu_atrm_call - fetch a chunk of the vbios 186 * 187 * @atrm_handle: acpi ATRM handle 188 * @bios: vbios image pointer 189 * @offset: offset of vbios image data to fetch 190 * @len: length of vbios image data to fetch 191 * 192 * Executes ATRM to fetch a chunk of the discrete 193 * vbios image on PX systems (all asics). 194 * Returns the length of the buffer fetched. 195 */ 196 static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios, 197 int offset, int len) 198 { 199 acpi_status status; 200 union acpi_object atrm_arg_elements[2], *obj; 201 struct acpi_object_list atrm_arg; 202 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL}; 203 204 atrm_arg.count = 2; 205 atrm_arg.pointer = &atrm_arg_elements[0]; 206 207 atrm_arg_elements[0].type = ACPI_TYPE_INTEGER; 208 atrm_arg_elements[0].integer.value = offset; 209 210 atrm_arg_elements[1].type = ACPI_TYPE_INTEGER; 211 atrm_arg_elements[1].integer.value = len; 212 213 status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer); 214 if (ACPI_FAILURE(status)) { 215 printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status)); 216 return -ENODEV; 217 } 218 219 obj = (union acpi_object *)buffer.pointer; 220 memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length); 221 len = obj->buffer.length; 222 kfree(buffer.pointer); 223 return len; 224 } 225 226 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 227 { 228 int ret; 229 int size = 256 * 1024; 230 int i; 231 struct pci_dev *pdev = NULL; 232 acpi_handle dhandle, atrm_handle; 233 acpi_status status; 234 bool found = false; 235 236 /* ATRM is for the discrete card only */ 237 if (adev->flags & AMD_IS_APU) 238 return false; 239 240 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { 241 dhandle = ACPI_HANDLE(&pdev->dev); 242 if (!dhandle) 243 continue; 244 245 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 246 if (!ACPI_FAILURE(status)) { 247 found = true; 248 break; 249 } 250 } 251 252 if (!found) { 253 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) { 254 dhandle = ACPI_HANDLE(&pdev->dev); 255 if (!dhandle) 256 continue; 257 258 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 259 if (!ACPI_FAILURE(status)) { 260 found = true; 261 break; 262 } 263 } 264 } 265 266 if (!found) 267 return false; 268 269 adev->bios = kmalloc(size, GFP_KERNEL); 270 if (!adev->bios) { 271 DRM_ERROR("Unable to allocate bios\n"); 272 return false; 273 } 274 275 for (i = 0; i < size / ATRM_BIOS_PAGE; i++) { 276 ret = amdgpu_atrm_call(atrm_handle, 277 adev->bios, 278 (i * ATRM_BIOS_PAGE), 279 ATRM_BIOS_PAGE); 280 if (ret < ATRM_BIOS_PAGE) 281 break; 282 } 283 284 if (i == 0 || adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) { 285 kfree(adev->bios); 286 return false; 287 } 288 return true; 289 } 290 #else 291 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 292 { 293 return false; 294 } 295 #endif 296 297 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev) 298 { 299 if (adev->flags & AMD_IS_APU) 300 return igp_read_bios_from_vram(adev); 301 else 302 return amdgpu_asic_read_disabled_bios(adev); 303 } 304 305 #ifdef CONFIG_ACPI 306 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 307 { 308 bool ret = false; 309 struct acpi_table_header *hdr; 310 acpi_size tbl_size; 311 UEFI_ACPI_VFCT *vfct; 312 GOP_VBIOS_CONTENT *vbios; 313 VFCT_IMAGE_HEADER *vhdr; 314 315 if (!ACPI_SUCCESS(acpi_get_table_with_size("VFCT", 1, &hdr, &tbl_size))) 316 return false; 317 if (tbl_size < sizeof(UEFI_ACPI_VFCT)) { 318 DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n"); 319 goto out_unmap; 320 } 321 322 vfct = (UEFI_ACPI_VFCT *)hdr; 323 if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) > tbl_size) { 324 DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n"); 325 goto out_unmap; 326 } 327 328 vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + vfct->VBIOSImageOffset); 329 vhdr = &vbios->VbiosHeader; 330 DRM_INFO("ACPI VFCT contains a BIOS for %02x:%02x.%d %04x:%04x, size %d\n", 331 vhdr->PCIBus, vhdr->PCIDevice, vhdr->PCIFunction, 332 vhdr->VendorID, vhdr->DeviceID, vhdr->ImageLength); 333 334 if (vhdr->PCIBus != adev->pdev->bus->number || 335 vhdr->PCIDevice != PCI_SLOT(adev->pdev->devfn) || 336 vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn) || 337 vhdr->VendorID != adev->pdev->vendor || 338 vhdr->DeviceID != adev->pdev->device) { 339 DRM_INFO("ACPI VFCT table is not for this card\n"); 340 goto out_unmap; 341 } 342 343 if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) + vhdr->ImageLength > tbl_size) { 344 DRM_ERROR("ACPI VFCT image truncated\n"); 345 goto out_unmap; 346 } 347 348 adev->bios = kmemdup(&vbios->VbiosContent, vhdr->ImageLength, GFP_KERNEL); 349 ret = !!adev->bios; 350 351 out_unmap: 352 return ret; 353 } 354 #else 355 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 356 { 357 return false; 358 } 359 #endif 360 361 bool amdgpu_get_bios(struct amdgpu_device *adev) 362 { 363 bool r; 364 uint16_t tmp; 365 366 r = amdgpu_atrm_get_bios(adev); 367 if (r == false) 368 r = amdgpu_acpi_vfct_bios(adev); 369 if (r == false) 370 r = igp_read_bios_from_vram(adev); 371 if (r == false) 372 r = amdgpu_read_bios(adev); 373 if (r == false) { 374 r = amdgpu_read_disabled_bios(adev); 375 } 376 if (r == false) { 377 r = amdgpu_read_platform_bios(adev); 378 } 379 if (r == false || adev->bios == NULL) { 380 DRM_ERROR("Unable to locate a BIOS ROM\n"); 381 adev->bios = NULL; 382 return false; 383 } 384 if (adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) { 385 printk("BIOS signature incorrect %x %x\n", adev->bios[0], adev->bios[1]); 386 goto free_bios; 387 } 388 389 tmp = RBIOS16(0x18); 390 if (RBIOS8(tmp + 0x14) != 0x0) { 391 DRM_INFO("Not an x86 BIOS ROM, not using.\n"); 392 goto free_bios; 393 } 394 395 adev->bios_header_start = RBIOS16(0x48); 396 if (!adev->bios_header_start) { 397 goto free_bios; 398 } 399 tmp = adev->bios_header_start + 4; 400 if (!memcmp(adev->bios + tmp, "ATOM", 4) || 401 !memcmp(adev->bios + tmp, "MOTA", 4)) { 402 adev->is_atom_bios = true; 403 } else { 404 adev->is_atom_bios = false; 405 } 406 407 DRM_DEBUG("%sBIOS detected\n", adev->is_atom_bios ? "ATOM" : "COM"); 408 return true; 409 free_bios: 410 kfree(adev->bios); 411 adev->bios = NULL; 412 return false; 413 } 414