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/vga_switcheroo.h> 33 #include <linux/slab.h> 34 #include <linux/acpi.h> 35 /* 36 * BIOS. 37 */ 38 39 /* If you boot an IGP board with a discrete card as the primary, 40 * the IGP rom is not accessible via the rom bar as the IGP rom is 41 * part of the system bios. On boot, the system bios puts a 42 * copy of the igp rom at the start of vram if a discrete card is 43 * present. 44 */ 45 static bool igp_read_bios_from_vram(struct amdgpu_device *adev) 46 { 47 uint8_t __iomem *bios; 48 resource_size_t vram_base; 49 resource_size_t size = 256 * 1024; /* ??? */ 50 51 if (!(adev->flags & AMDGPU_IS_APU)) 52 if (!amdgpu_card_posted(adev)) 53 return false; 54 55 adev->bios = NULL; 56 vram_base = pci_resource_start(adev->pdev, 0); 57 bios = ioremap(vram_base, size); 58 if (!bios) { 59 return false; 60 } 61 62 if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) { 63 iounmap(bios); 64 return false; 65 } 66 adev->bios = kmalloc(size, GFP_KERNEL); 67 if (adev->bios == NULL) { 68 iounmap(bios); 69 return false; 70 } 71 memcpy_fromio(adev->bios, bios, size); 72 iounmap(bios); 73 return true; 74 } 75 76 bool amdgpu_read_bios(struct amdgpu_device *adev) 77 { 78 uint8_t __iomem *bios, val1, val2; 79 size_t size; 80 81 adev->bios = NULL; 82 /* XXX: some cards may return 0 for rom size? ddx has a workaround */ 83 bios = pci_map_rom(adev->pdev, &size); 84 if (!bios) { 85 return false; 86 } 87 88 val1 = readb(&bios[0]); 89 val2 = readb(&bios[1]); 90 91 if (size == 0 || val1 != 0x55 || val2 != 0xaa) { 92 pci_unmap_rom(adev->pdev, bios); 93 return false; 94 } 95 adev->bios = kzalloc(size, GFP_KERNEL); 96 if (adev->bios == NULL) { 97 pci_unmap_rom(adev->pdev, bios); 98 return false; 99 } 100 memcpy_fromio(adev->bios, bios, size); 101 pci_unmap_rom(adev->pdev, bios); 102 return true; 103 } 104 105 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev) 106 { 107 uint8_t __iomem *bios; 108 size_t size; 109 110 adev->bios = NULL; 111 112 bios = pci_platform_rom(adev->pdev, &size); 113 if (!bios) { 114 return false; 115 } 116 117 if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) { 118 return false; 119 } 120 adev->bios = kmemdup(bios, size, GFP_KERNEL); 121 if (adev->bios == NULL) { 122 return false; 123 } 124 125 return true; 126 } 127 128 #ifdef CONFIG_ACPI 129 /* ATRM is used to get the BIOS on the discrete cards in 130 * dual-gpu systems. 131 */ 132 /* retrieve the ROM in 4k blocks */ 133 #define ATRM_BIOS_PAGE 4096 134 /** 135 * amdgpu_atrm_call - fetch a chunk of the vbios 136 * 137 * @atrm_handle: acpi ATRM handle 138 * @bios: vbios image pointer 139 * @offset: offset of vbios image data to fetch 140 * @len: length of vbios image data to fetch 141 * 142 * Executes ATRM to fetch a chunk of the discrete 143 * vbios image on PX systems (all asics). 144 * Returns the length of the buffer fetched. 145 */ 146 static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios, 147 int offset, int len) 148 { 149 acpi_status status; 150 union acpi_object atrm_arg_elements[2], *obj; 151 struct acpi_object_list atrm_arg; 152 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL}; 153 154 atrm_arg.count = 2; 155 atrm_arg.pointer = &atrm_arg_elements[0]; 156 157 atrm_arg_elements[0].type = ACPI_TYPE_INTEGER; 158 atrm_arg_elements[0].integer.value = offset; 159 160 atrm_arg_elements[1].type = ACPI_TYPE_INTEGER; 161 atrm_arg_elements[1].integer.value = len; 162 163 status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer); 164 if (ACPI_FAILURE(status)) { 165 printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status)); 166 return -ENODEV; 167 } 168 169 obj = (union acpi_object *)buffer.pointer; 170 memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length); 171 len = obj->buffer.length; 172 kfree(buffer.pointer); 173 return len; 174 } 175 176 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 177 { 178 int ret; 179 int size = 256 * 1024; 180 int i; 181 struct pci_dev *pdev = NULL; 182 acpi_handle dhandle, atrm_handle; 183 acpi_status status; 184 bool found = false; 185 186 /* ATRM is for the discrete card only */ 187 if (adev->flags & AMDGPU_IS_APU) 188 return false; 189 190 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { 191 dhandle = ACPI_HANDLE(&pdev->dev); 192 if (!dhandle) 193 continue; 194 195 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 196 if (!ACPI_FAILURE(status)) { 197 found = true; 198 break; 199 } 200 } 201 202 if (!found) { 203 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) { 204 dhandle = ACPI_HANDLE(&pdev->dev); 205 if (!dhandle) 206 continue; 207 208 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); 209 if (!ACPI_FAILURE(status)) { 210 found = true; 211 break; 212 } 213 } 214 } 215 216 if (!found) 217 return false; 218 219 adev->bios = kmalloc(size, GFP_KERNEL); 220 if (!adev->bios) { 221 DRM_ERROR("Unable to allocate bios\n"); 222 return false; 223 } 224 225 for (i = 0; i < size / ATRM_BIOS_PAGE; i++) { 226 ret = amdgpu_atrm_call(atrm_handle, 227 adev->bios, 228 (i * ATRM_BIOS_PAGE), 229 ATRM_BIOS_PAGE); 230 if (ret < ATRM_BIOS_PAGE) 231 break; 232 } 233 234 if (i == 0 || adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) { 235 kfree(adev->bios); 236 return false; 237 } 238 return true; 239 } 240 #else 241 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev) 242 { 243 return false; 244 } 245 #endif 246 247 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev) 248 { 249 if (adev->flags & AMDGPU_IS_APU) 250 return igp_read_bios_from_vram(adev); 251 else 252 return amdgpu_asic_read_disabled_bios(adev); 253 } 254 255 #ifdef CONFIG_ACPI 256 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 257 { 258 bool ret = false; 259 struct acpi_table_header *hdr; 260 acpi_size tbl_size; 261 UEFI_ACPI_VFCT *vfct; 262 GOP_VBIOS_CONTENT *vbios; 263 VFCT_IMAGE_HEADER *vhdr; 264 265 if (!ACPI_SUCCESS(acpi_get_table_with_size("VFCT", 1, &hdr, &tbl_size))) 266 return false; 267 if (tbl_size < sizeof(UEFI_ACPI_VFCT)) { 268 DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n"); 269 goto out_unmap; 270 } 271 272 vfct = (UEFI_ACPI_VFCT *)hdr; 273 if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) > tbl_size) { 274 DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n"); 275 goto out_unmap; 276 } 277 278 vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + vfct->VBIOSImageOffset); 279 vhdr = &vbios->VbiosHeader; 280 DRM_INFO("ACPI VFCT contains a BIOS for %02x:%02x.%d %04x:%04x, size %d\n", 281 vhdr->PCIBus, vhdr->PCIDevice, vhdr->PCIFunction, 282 vhdr->VendorID, vhdr->DeviceID, vhdr->ImageLength); 283 284 if (vhdr->PCIBus != adev->pdev->bus->number || 285 vhdr->PCIDevice != PCI_SLOT(adev->pdev->devfn) || 286 vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn) || 287 vhdr->VendorID != adev->pdev->vendor || 288 vhdr->DeviceID != adev->pdev->device) { 289 DRM_INFO("ACPI VFCT table is not for this card\n"); 290 goto out_unmap; 291 } 292 293 if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) + vhdr->ImageLength > tbl_size) { 294 DRM_ERROR("ACPI VFCT image truncated\n"); 295 goto out_unmap; 296 } 297 298 adev->bios = kmemdup(&vbios->VbiosContent, vhdr->ImageLength, GFP_KERNEL); 299 ret = !!adev->bios; 300 301 out_unmap: 302 return ret; 303 } 304 #else 305 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev) 306 { 307 return false; 308 } 309 #endif 310 311 bool amdgpu_get_bios(struct amdgpu_device *adev) 312 { 313 bool r; 314 uint16_t tmp; 315 316 r = amdgpu_atrm_get_bios(adev); 317 if (r == false) 318 r = amdgpu_acpi_vfct_bios(adev); 319 if (r == false) 320 r = igp_read_bios_from_vram(adev); 321 if (r == false) 322 r = amdgpu_read_bios(adev); 323 if (r == false) { 324 r = amdgpu_read_disabled_bios(adev); 325 } 326 if (r == false) { 327 r = amdgpu_read_platform_bios(adev); 328 } 329 if (r == false || adev->bios == NULL) { 330 DRM_ERROR("Unable to locate a BIOS ROM\n"); 331 adev->bios = NULL; 332 return false; 333 } 334 if (adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) { 335 printk("BIOS signature incorrect %x %x\n", adev->bios[0], adev->bios[1]); 336 goto free_bios; 337 } 338 339 tmp = RBIOS16(0x18); 340 if (RBIOS8(tmp + 0x14) != 0x0) { 341 DRM_INFO("Not an x86 BIOS ROM, not using.\n"); 342 goto free_bios; 343 } 344 345 adev->bios_header_start = RBIOS16(0x48); 346 if (!adev->bios_header_start) { 347 goto free_bios; 348 } 349 tmp = adev->bios_header_start + 4; 350 if (!memcmp(adev->bios + tmp, "ATOM", 4) || 351 !memcmp(adev->bios + tmp, "MOTA", 4)) { 352 adev->is_atom_bios = true; 353 } else { 354 adev->is_atom_bios = false; 355 } 356 357 DRM_DEBUG("%sBIOS detected\n", adev->is_atom_bios ? "ATOM" : "COM"); 358 return true; 359 free_bios: 360 kfree(adev->bios); 361 adev->bios = NULL; 362 return false; 363 } 364