xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c (revision d639d9fa162aadec1ae9980c4dcf6e50bd2f8290)
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 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
376 {
377 	struct acpi_table_header *hdr;
378 	acpi_size tbl_size;
379 	UEFI_ACPI_VFCT *vfct;
380 	unsigned int offset;
381 
382 	if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))
383 		return false;
384 	tbl_size = hdr->length;
385 	if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
386 		dev_info(adev->dev, "ACPI VFCT table present but broken (too short #1),skipping\n");
387 		return false;
388 	}
389 
390 	vfct = (UEFI_ACPI_VFCT *)hdr;
391 	offset = vfct->VBIOSImageOffset;
392 
393 	while (offset < tbl_size) {
394 		GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);
395 		VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;
396 
397 		offset += sizeof(VFCT_IMAGE_HEADER);
398 		if (offset > tbl_size) {
399 			dev_info(adev->dev, "ACPI VFCT image header truncated,skipping\n");
400 			return false;
401 		}
402 
403 		offset += vhdr->ImageLength;
404 		if (offset > tbl_size) {
405 			dev_info(adev->dev, "ACPI VFCT image truncated,skipping\n");
406 			return false;
407 		}
408 
409 		if (vhdr->ImageLength &&
410 		    vhdr->PCIBus == adev->pdev->bus->number &&
411 		    vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&
412 		    vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) &&
413 		    vhdr->VendorID == adev->pdev->vendor &&
414 		    vhdr->DeviceID == adev->pdev->device) {
415 			adev->bios = kmemdup(&vbios->VbiosContent,
416 					     vhdr->ImageLength,
417 					     GFP_KERNEL);
418 
419 			if (!check_atom_bios(adev, vhdr->ImageLength)) {
420 				amdgpu_bios_release(adev);
421 				return false;
422 			}
423 			adev->bios_size = vhdr->ImageLength;
424 			return true;
425 		}
426 	}
427 
428 	dev_info(adev->dev, "ACPI VFCT table present but broken (too short #2),skipping\n");
429 	return false;
430 }
431 #else
432 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
433 {
434 	return false;
435 }
436 #endif
437 
438 static bool amdgpu_get_bios_apu(struct amdgpu_device *adev)
439 {
440 	if (amdgpu_acpi_vfct_bios(adev)) {
441 		dev_info(adev->dev, "Fetched VBIOS from VFCT\n");
442 		goto success;
443 	}
444 
445 	if (amdgpu_read_bios_from_vram(adev)) {
446 		dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");
447 		goto success;
448 	}
449 
450 	if (amdgpu_read_bios(adev)) {
451 		dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
452 		goto success;
453 	}
454 
455 	if (amdgpu_read_platform_bios(adev)) {
456 		dev_info(adev->dev, "Fetched VBIOS from platform\n");
457 		goto success;
458 	}
459 
460 	dev_err(adev->dev, "Unable to locate a BIOS ROM\n");
461 	return false;
462 
463 success:
464 	return true;
465 }
466 
467 static bool amdgpu_prefer_rom_resource(struct amdgpu_device *adev)
468 {
469 	struct resource *res = &adev->pdev->resource[PCI_ROM_RESOURCE];
470 
471 	return (res->flags & IORESOURCE_ROM_SHADOW) ||
472 	       adev->pdev == vga_default_device();
473 }
474 
475 static bool amdgpu_get_bios_dgpu(struct amdgpu_device *adev)
476 {
477 	if (amdgpu_atrm_get_bios(adev)) {
478 		dev_info(adev->dev, "Fetched VBIOS from ATRM\n");
479 		goto success;
480 	}
481 
482 	if (amdgpu_acpi_vfct_bios(adev)) {
483 		dev_info(adev->dev, "Fetched VBIOS from VFCT\n");
484 		goto success;
485 	}
486 
487 	/* this is required for SR-IOV */
488 	if (amdgpu_read_bios_from_vram(adev)) {
489 		dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");
490 		goto success;
491 	}
492 
493 	if (amdgpu_prefer_rom_resource(adev)) {
494 		if (amdgpu_read_bios(adev)) {
495 			dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
496 			goto success;
497 		}
498 
499 		if (amdgpu_read_platform_bios(adev)) {
500 			dev_info(adev->dev, "Fetched VBIOS from platform\n");
501 			goto success;
502 		}
503 
504 	} else {
505 		if (amdgpu_read_platform_bios(adev)) {
506 			dev_info(adev->dev, "Fetched VBIOS from platform\n");
507 			goto success;
508 		}
509 
510 		if (amdgpu_read_bios(adev)) {
511 			dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
512 			goto success;
513 		}
514 	}
515 
516 	if (amdgpu_read_bios_from_rom(adev)) {
517 		dev_info(adev->dev, "Fetched VBIOS from ROM\n");
518 		goto success;
519 	}
520 
521 	if (amdgpu_read_disabled_bios(adev)) {
522 		dev_info(adev->dev, "Fetched VBIOS from disabled ROM BAR\n");
523 		goto success;
524 	}
525 
526 	dev_err(adev->dev, "Unable to locate a BIOS ROM\n");
527 	return false;
528 
529 success:
530 	return true;
531 }
532 
533 bool amdgpu_get_bios(struct amdgpu_device *adev)
534 {
535 	bool found;
536 
537 	if (adev->flags & AMD_IS_APU)
538 		found = amdgpu_get_bios_apu(adev);
539 	else
540 		found = amdgpu_get_bios_dgpu(adev);
541 
542 	if (found)
543 		adev->is_atom_fw = adev->asic_type >= CHIP_VEGA10;
544 
545 	return found;
546 }
547 
548 /* helper function for soc15 and onwards to read bios from rom */
549 bool amdgpu_soc15_read_bios_from_rom(struct amdgpu_device *adev,
550 				     u8 *bios, u32 length_bytes)
551 {
552 	u32 *dw_ptr;
553 	u32 i, length_dw;
554 	u32 rom_offset;
555 	u32 rom_index_offset;
556 	u32 rom_data_offset;
557 
558 	if (bios == NULL)
559 		return false;
560 	if (length_bytes == 0)
561 		return false;
562 	/* APU vbios image is part of sbios image */
563 	if (adev->flags & AMD_IS_APU)
564 		return false;
565 	if (!adev->smuio.funcs ||
566 	    !adev->smuio.funcs->get_rom_index_offset ||
567 	    !adev->smuio.funcs->get_rom_data_offset)
568 		return false;
569 
570 	dw_ptr = (u32 *)bios;
571 	length_dw = ALIGN(length_bytes, 4) / 4;
572 
573 	rom_index_offset =
574 		adev->smuio.funcs->get_rom_index_offset(adev);
575 	rom_data_offset =
576 		adev->smuio.funcs->get_rom_data_offset(adev);
577 
578 	if (adev->nbio.funcs &&
579 	    adev->nbio.funcs->get_rom_offset) {
580 		rom_offset = adev->nbio.funcs->get_rom_offset(adev);
581 		rom_offset = rom_offset << 17;
582 	} else {
583 		rom_offset = 0;
584 	}
585 
586 	/* set rom index to rom_offset */
587 	WREG32(rom_index_offset, rom_offset);
588 	/* read out the rom data */
589 	for (i = 0; i < length_dw; i++)
590 		dw_ptr[i] = RREG32(rom_data_offset);
591 
592 	return true;
593 }
594