xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c (revision 87579b8cda9ec6ecba8327c59d8505d3b83fda98)
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 bool amdgpu_read_bios(struct amdgpu_device *adev)
156 {
157 	uint8_t __iomem *bios;
158 	size_t size;
159 
160 	adev->bios = NULL;
161 	/* XXX: some cards may return 0 for rom size? ddx has a workaround */
162 	bios = pci_map_rom(adev->pdev, &size);
163 	if (!bios)
164 		return false;
165 
166 	adev->bios = kzalloc(size, GFP_KERNEL);
167 	if (adev->bios == NULL) {
168 		pci_unmap_rom(adev->pdev, bios);
169 		return false;
170 	}
171 	adev->bios_size = size;
172 	memcpy_fromio(adev->bios, bios, size);
173 	pci_unmap_rom(adev->pdev, bios);
174 
175 	if (!check_atom_bios(adev, size)) {
176 		amdgpu_bios_release(adev);
177 		return false;
178 	}
179 
180 	return true;
181 }
182 
183 static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev)
184 {
185 	u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0};
186 	int len;
187 
188 	if (!adev->asic_funcs || !adev->asic_funcs->read_bios_from_rom)
189 		return false;
190 
191 	/* validate VBIOS signature */
192 	if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false)
193 		return false;
194 	header[AMD_VBIOS_SIGNATURE_END] = 0;
195 
196 	if ((!AMD_IS_VALID_VBIOS(header)) ||
197 		memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET],
198 		       AMD_VBIOS_SIGNATURE,
199 		       strlen(AMD_VBIOS_SIGNATURE)) != 0)
200 		return false;
201 
202 	/* valid vbios, go on */
203 	len = AMD_VBIOS_LENGTH(header);
204 	len = ALIGN(len, 4);
205 	adev->bios = kmalloc(len, GFP_KERNEL);
206 	if (!adev->bios) {
207 		DRM_ERROR("no memory to allocate for BIOS\n");
208 		return false;
209 	}
210 	adev->bios_size = len;
211 
212 	/* read complete BIOS */
213 	amdgpu_asic_read_bios_from_rom(adev, adev->bios, len);
214 
215 	if (!check_atom_bios(adev, len)) {
216 		amdgpu_bios_release(adev);
217 		return false;
218 	}
219 
220 	return true;
221 }
222 
223 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
224 {
225 	phys_addr_t rom = adev->pdev->rom;
226 	size_t romlen = adev->pdev->romlen;
227 	void __iomem *bios;
228 
229 	adev->bios = NULL;
230 
231 	if (!rom || romlen == 0)
232 		return false;
233 
234 	adev->bios = kzalloc(romlen, GFP_KERNEL);
235 	if (!adev->bios)
236 		return false;
237 
238 	bios = ioremap(rom, romlen);
239 	if (!bios)
240 		goto free_bios;
241 
242 	memcpy_fromio(adev->bios, bios, romlen);
243 	iounmap(bios);
244 
245 	if (!check_atom_bios(adev, romlen))
246 		goto free_bios;
247 
248 	adev->bios_size = romlen;
249 
250 	return true;
251 free_bios:
252 	amdgpu_bios_release(adev);
253 
254 	return false;
255 }
256 
257 #ifdef CONFIG_ACPI
258 /* ATRM is used to get the BIOS on the discrete cards in
259  * dual-gpu systems.
260  */
261 /* retrieve the ROM in 4k blocks */
262 #define ATRM_BIOS_PAGE 4096
263 /**
264  * amdgpu_atrm_call - fetch a chunk of the vbios
265  *
266  * @atrm_handle: acpi ATRM handle
267  * @bios: vbios image pointer
268  * @offset: offset of vbios image data to fetch
269  * @len: length of vbios image data to fetch
270  *
271  * Executes ATRM to fetch a chunk of the discrete
272  * vbios image on PX systems (all asics).
273  * Returns the length of the buffer fetched.
274  */
275 static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
276 			    int offset, int len)
277 {
278 	acpi_status status;
279 	union acpi_object atrm_arg_elements[2], *obj;
280 	struct acpi_object_list atrm_arg;
281 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
282 
283 	atrm_arg.count = 2;
284 	atrm_arg.pointer = &atrm_arg_elements[0];
285 
286 	atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
287 	atrm_arg_elements[0].integer.value = offset;
288 
289 	atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
290 	atrm_arg_elements[1].integer.value = len;
291 
292 	status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
293 	if (ACPI_FAILURE(status)) {
294 		DRM_ERROR("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
295 		return -ENODEV;
296 	}
297 
298 	obj = (union acpi_object *)buffer.pointer;
299 	memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
300 	len = obj->buffer.length;
301 	kfree(buffer.pointer);
302 	return len;
303 }
304 
305 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
306 {
307 	int ret;
308 	int size = 256 * 1024;
309 	int i;
310 	struct pci_dev *pdev = NULL;
311 	acpi_handle dhandle, atrm_handle;
312 	acpi_status status;
313 	bool found = false;
314 
315 	/* ATRM is for on-platform devices only */
316 	if (dev_is_removable(&adev->pdev->dev))
317 		return false;
318 
319 	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
320 		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
321 		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
322 			continue;
323 
324 		dhandle = ACPI_HANDLE(&pdev->dev);
325 		if (!dhandle)
326 			continue;
327 
328 		status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
329 		if (ACPI_SUCCESS(status)) {
330 			found = true;
331 			break;
332 		}
333 	}
334 
335 	if (!found)
336 		return false;
337 	pci_dev_put(pdev);
338 
339 	adev->bios = kmalloc(size, GFP_KERNEL);
340 	if (!adev->bios) {
341 		dev_err(adev->dev, "Unable to allocate bios\n");
342 		return false;
343 	}
344 
345 	for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
346 		ret = amdgpu_atrm_call(atrm_handle,
347 				       adev->bios,
348 				       (i * ATRM_BIOS_PAGE),
349 				       ATRM_BIOS_PAGE);
350 		if (ret < ATRM_BIOS_PAGE)
351 			break;
352 	}
353 
354 	if (!check_atom_bios(adev, size)) {
355 		amdgpu_bios_release(adev);
356 		return false;
357 	}
358 	adev->bios_size = size;
359 	return true;
360 }
361 #else
362 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
363 {
364 	return false;
365 }
366 #endif
367 
368 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
369 {
370 	return (!adev->asic_funcs || !adev->asic_funcs->read_disabled_bios) ?
371 		false : amdgpu_asic_read_disabled_bios(adev);
372 }
373 
374 #ifdef CONFIG_ACPI
375 /**
376  * amdgpu_acpi_vfct_match() - Check if a VFCT entry matches the device
377  * @adev: AMDGPU device
378  * @vhdr: VFCT image header to check
379  *
380  * VFCT entries contain the PCI bus number as recorded during BIOS POST.
381  * On systems where the kernel renumbers PCI buses (e.g. pci=realloc or
382  * resource conflicts), the runtime bus number may differ from the POST
383  * value.  Match by device identity (vendor + device + function) and use
384  * the bus number as a preference: exact bus match is preferred, but when
385  * the bus numbers disagree we accept the entry if the device identity
386  * matches.
387  *
388  * Returns: 0 on match, -ENODEV on no match
389  */
390 static int amdgpu_acpi_vfct_match(struct amdgpu_device *adev,
391 				  VFCT_IMAGE_HEADER *vhdr)
392 {
393 	/* Vendor and device IDs must always match */
394 	if (vhdr->VendorID != adev->pdev->vendor ||
395 	    vhdr->DeviceID != adev->pdev->device)
396 		return -ENODEV;
397 
398 	if (vhdr->PCIDevice != PCI_SLOT(adev->pdev->devfn) ||
399 	    vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn))
400 		return -ENODEV;
401 
402 	/* Exact bus number match - preferred */
403 	if (vhdr->PCIBus == adev->pdev->bus->number)
404 		return 0;
405 
406 	/* Bus mismatch but device identity matches (PCI renumbering case) */
407 	dev_notice(adev->dev,
408 		   "VFCT bus number mismatch: table %u != runtime %u, matching by device identity (vendor 0x%04x device 0x%04x)\n",
409 		   vhdr->PCIBus, adev->pdev->bus->number,
410 		   adev->pdev->vendor, adev->pdev->device);
411 	return 0;
412 }
413 
414 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
415 {
416 	struct acpi_table_header *hdr;
417 	acpi_size tbl_size;
418 	UEFI_ACPI_VFCT *vfct;
419 	unsigned int offset;
420 	bool r = false;
421 
422 	if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))
423 		return false;
424 	tbl_size = hdr->length;
425 	if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
426 		dev_info(adev->dev, "ACPI VFCT table present but broken (too short #1),skipping\n");
427 		goto out;
428 	}
429 
430 	vfct = (UEFI_ACPI_VFCT *)hdr;
431 	offset = vfct->VBIOSImageOffset;
432 
433 	while (offset < tbl_size) {
434 		GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);
435 		VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;
436 
437 		offset += sizeof(VFCT_IMAGE_HEADER);
438 		if (offset > tbl_size) {
439 			dev_info(adev->dev, "ACPI VFCT image header truncated,skipping\n");
440 			goto out;
441 		}
442 
443 		offset += vhdr->ImageLength;
444 		if (offset > tbl_size) {
445 			dev_info(adev->dev, "ACPI VFCT image truncated,skipping\n");
446 			goto out;
447 		}
448 
449 		if (vhdr->ImageLength &&
450 		    !amdgpu_acpi_vfct_match(adev, vhdr)) {
451 			adev->bios = kmemdup(&vbios->VbiosContent,
452 					     vhdr->ImageLength,
453 					     GFP_KERNEL);
454 
455 			if (!check_atom_bios(adev, vhdr->ImageLength)) {
456 				amdgpu_bios_release(adev);
457 				goto out;
458 			}
459 			adev->bios_size = vhdr->ImageLength;
460 			r = true;
461 			goto out;
462 		}
463 	}
464 
465 	dev_info(adev->dev, "ACPI VFCT table present but broken (too short #2),skipping\n");
466 
467 out:
468 	acpi_put_table(hdr);
469 	return r;
470 }
471 #else
472 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
473 {
474 	return false;
475 }
476 #endif
477 
478 static bool amdgpu_get_bios_apu(struct amdgpu_device *adev)
479 {
480 	if (amdgpu_acpi_vfct_bios(adev)) {
481 		dev_info(adev->dev, "Fetched VBIOS from VFCT\n");
482 		goto success;
483 	}
484 
485 	if (amdgpu_read_bios_from_vram(adev)) {
486 		dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");
487 		goto success;
488 	}
489 
490 	if (amdgpu_read_bios(adev)) {
491 		dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
492 		goto success;
493 	}
494 
495 	if (amdgpu_read_platform_bios(adev)) {
496 		dev_info(adev->dev, "Fetched VBIOS from platform\n");
497 		goto success;
498 	}
499 
500 	dev_err(adev->dev, "Unable to locate a BIOS ROM\n");
501 	return false;
502 
503 success:
504 	return true;
505 }
506 
507 static bool amdgpu_prefer_rom_resource(struct amdgpu_device *adev)
508 {
509 	struct resource *res = &adev->pdev->resource[PCI_ROM_RESOURCE];
510 
511 	return (res->flags & IORESOURCE_ROM_SHADOW) ||
512 	       adev->pdev == vga_default_device();
513 }
514 
515 static bool amdgpu_get_bios_dgpu(struct amdgpu_device *adev)
516 {
517 	if (amdgpu_atrm_get_bios(adev)) {
518 		dev_info(adev->dev, "Fetched VBIOS from ATRM\n");
519 		goto success;
520 	}
521 
522 	if (amdgpu_acpi_vfct_bios(adev)) {
523 		dev_info(adev->dev, "Fetched VBIOS from VFCT\n");
524 		goto success;
525 	}
526 
527 	/* this is required for SR-IOV */
528 	if (amdgpu_read_bios_from_vram(adev)) {
529 		dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");
530 		goto success;
531 	}
532 
533 	if (amdgpu_prefer_rom_resource(adev)) {
534 		if (amdgpu_read_bios(adev)) {
535 			dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
536 			goto success;
537 		}
538 
539 		if (amdgpu_read_platform_bios(adev)) {
540 			dev_info(adev->dev, "Fetched VBIOS from platform\n");
541 			goto success;
542 		}
543 
544 	} else {
545 		if (amdgpu_read_platform_bios(adev)) {
546 			dev_info(adev->dev, "Fetched VBIOS from platform\n");
547 			goto success;
548 		}
549 
550 		if (amdgpu_read_bios(adev)) {
551 			dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
552 			goto success;
553 		}
554 	}
555 
556 	if (amdgpu_read_bios_from_rom(adev)) {
557 		dev_info(adev->dev, "Fetched VBIOS from ROM\n");
558 		goto success;
559 	}
560 
561 	if (amdgpu_read_disabled_bios(adev)) {
562 		dev_info(adev->dev, "Fetched VBIOS from disabled ROM BAR\n");
563 		goto success;
564 	}
565 
566 	dev_err(adev->dev, "Unable to locate a BIOS ROM\n");
567 	return false;
568 
569 success:
570 	return true;
571 }
572 
573 bool amdgpu_get_bios(struct amdgpu_device *adev)
574 {
575 	bool found;
576 
577 	if (adev->flags & AMD_IS_APU)
578 		found = amdgpu_get_bios_apu(adev);
579 	else
580 		found = amdgpu_get_bios_dgpu(adev);
581 
582 	if (found)
583 		adev->is_atom_fw = adev->asic_type >= CHIP_VEGA10;
584 
585 	return found;
586 }
587 
588 /* helper function for soc15 and onwards to read bios from rom */
589 bool amdgpu_soc15_read_bios_from_rom(struct amdgpu_device *adev,
590 				     u8 *bios, u32 length_bytes)
591 {
592 	u32 *dw_ptr;
593 	u32 i, length_dw;
594 	u32 rom_offset;
595 	u32 rom_index_offset;
596 	u32 rom_data_offset;
597 
598 	if (bios == NULL)
599 		return false;
600 	if (length_bytes == 0)
601 		return false;
602 	/* APU vbios image is part of sbios image */
603 	if (adev->flags & AMD_IS_APU)
604 		return false;
605 	if (!adev->smuio.funcs ||
606 	    !adev->smuio.funcs->get_rom_index_offset ||
607 	    !adev->smuio.funcs->get_rom_data_offset)
608 		return false;
609 
610 	dw_ptr = (u32 *)bios;
611 	length_dw = ALIGN(length_bytes, 4) / 4;
612 
613 	rom_index_offset =
614 		adev->smuio.funcs->get_rom_index_offset(adev);
615 	rom_data_offset =
616 		adev->smuio.funcs->get_rom_data_offset(adev);
617 
618 	if (adev->nbio.funcs &&
619 	    adev->nbio.funcs->get_rom_offset) {
620 		rom_offset = adev->nbio.funcs->get_rom_offset(adev);
621 		rom_offset = rom_offset << 17;
622 	} else {
623 		rom_offset = 0;
624 	}
625 
626 	/* set rom index to rom_offset */
627 	WREG32(rom_index_offset, rom_offset);
628 	/* read out the rom data */
629 	for (i = 0; i < length_dw; i++)
630 		dw_ptr[i] = RREG32(rom_data_offset);
631 
632 	return true;
633 }
634