xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c (revision b71a623598d319cae2acd9df1b297cc948c58bc7)
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 	if (!obj || obj->type != ACPI_TYPE_BUFFER) {
300 		DRM_ERROR("ATRM returned an invalid object\n");
301 		kfree(buffer.pointer);
302 		return -EINVAL;
303 	}
304 
305 	len = min_t(size_t, obj->buffer.length, len);
306 	memcpy(bios+offset, obj->buffer.pointer, len);
307 	kfree(buffer.pointer);
308 	return len;
309 }
310 
311 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
312 {
313 	int ret;
314 	int size = 256 * 1024;
315 	int i;
316 	struct pci_dev *pdev = NULL;
317 	acpi_handle dhandle, atrm_handle;
318 	acpi_status status;
319 	bool found = false;
320 
321 	/* ATRM is for on-platform devices only */
322 	if (dev_is_removable(&adev->pdev->dev))
323 		return false;
324 
325 	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
326 		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
327 		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
328 			continue;
329 
330 		dhandle = ACPI_HANDLE(&pdev->dev);
331 		if (!dhandle)
332 			continue;
333 
334 		status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
335 		if (ACPI_SUCCESS(status)) {
336 			found = true;
337 			break;
338 		}
339 	}
340 
341 	if (!found)
342 		return false;
343 	pci_dev_put(pdev);
344 
345 	adev->bios = kmalloc(size, GFP_KERNEL);
346 	if (!adev->bios) {
347 		dev_err(adev->dev, "Unable to allocate bios\n");
348 		return false;
349 	}
350 
351 	for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
352 		ret = amdgpu_atrm_call(atrm_handle,
353 				       adev->bios,
354 				       (i * ATRM_BIOS_PAGE),
355 				       ATRM_BIOS_PAGE);
356 		if (ret < ATRM_BIOS_PAGE)
357 			break;
358 	}
359 
360 	if (!check_atom_bios(adev, size)) {
361 		amdgpu_bios_release(adev);
362 		return false;
363 	}
364 	adev->bios_size = size;
365 	return true;
366 }
367 #else
368 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
369 {
370 	return false;
371 }
372 #endif
373 
374 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
375 {
376 	return (!adev->asic_funcs || !adev->asic_funcs->read_disabled_bios) ?
377 		false : amdgpu_asic_read_disabled_bios(adev);
378 }
379 
380 #ifdef CONFIG_ACPI
381 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
382 {
383 	struct acpi_table_header *hdr;
384 	acpi_size tbl_size;
385 	UEFI_ACPI_VFCT *vfct;
386 	unsigned int offset;
387 
388 	if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))
389 		return false;
390 	tbl_size = hdr->length;
391 	if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
392 		dev_info(adev->dev, "ACPI VFCT table present but broken (too short #1),skipping\n");
393 		return false;
394 	}
395 
396 	vfct = (UEFI_ACPI_VFCT *)hdr;
397 	offset = vfct->VBIOSImageOffset;
398 
399 	while (offset < tbl_size) {
400 		GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);
401 		VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;
402 
403 		offset += sizeof(VFCT_IMAGE_HEADER);
404 		if (offset > tbl_size) {
405 			dev_info(adev->dev, "ACPI VFCT image header truncated,skipping\n");
406 			return false;
407 		}
408 
409 		offset += vhdr->ImageLength;
410 		if (offset > tbl_size) {
411 			dev_info(adev->dev, "ACPI VFCT image truncated,skipping\n");
412 			return false;
413 		}
414 
415 		if (vhdr->ImageLength &&
416 		    vhdr->PCIBus == adev->pdev->bus->number &&
417 		    vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&
418 		    vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) &&
419 		    vhdr->VendorID == adev->pdev->vendor &&
420 		    vhdr->DeviceID == adev->pdev->device) {
421 			adev->bios = kmemdup(&vbios->VbiosContent,
422 					     vhdr->ImageLength,
423 					     GFP_KERNEL);
424 
425 			if (!check_atom_bios(adev, vhdr->ImageLength)) {
426 				amdgpu_bios_release(adev);
427 				return false;
428 			}
429 			adev->bios_size = vhdr->ImageLength;
430 			return true;
431 		}
432 	}
433 
434 	dev_info(adev->dev, "ACPI VFCT table present but broken (too short #2),skipping\n");
435 	return false;
436 }
437 #else
438 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
439 {
440 	return false;
441 }
442 #endif
443 
444 static bool amdgpu_get_bios_apu(struct amdgpu_device *adev)
445 {
446 	if (amdgpu_acpi_vfct_bios(adev)) {
447 		dev_info(adev->dev, "Fetched VBIOS from VFCT\n");
448 		goto success;
449 	}
450 
451 	if (amdgpu_read_bios_from_vram(adev)) {
452 		dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");
453 		goto success;
454 	}
455 
456 	if (amdgpu_read_bios(adev)) {
457 		dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
458 		goto success;
459 	}
460 
461 	if (amdgpu_read_platform_bios(adev)) {
462 		dev_info(adev->dev, "Fetched VBIOS from platform\n");
463 		goto success;
464 	}
465 
466 	dev_err(adev->dev, "Unable to locate a BIOS ROM\n");
467 	return false;
468 
469 success:
470 	return true;
471 }
472 
473 static bool amdgpu_prefer_rom_resource(struct amdgpu_device *adev)
474 {
475 	struct resource *res = &adev->pdev->resource[PCI_ROM_RESOURCE];
476 
477 	return (res->flags & IORESOURCE_ROM_SHADOW) ||
478 	       adev->pdev == vga_default_device();
479 }
480 
481 static bool amdgpu_get_bios_dgpu(struct amdgpu_device *adev)
482 {
483 	if (amdgpu_atrm_get_bios(adev)) {
484 		dev_info(adev->dev, "Fetched VBIOS from ATRM\n");
485 		goto success;
486 	}
487 
488 	if (amdgpu_acpi_vfct_bios(adev)) {
489 		dev_info(adev->dev, "Fetched VBIOS from VFCT\n");
490 		goto success;
491 	}
492 
493 	/* this is required for SR-IOV */
494 	if (amdgpu_read_bios_from_vram(adev)) {
495 		dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");
496 		goto success;
497 	}
498 
499 	if (amdgpu_prefer_rom_resource(adev)) {
500 		if (amdgpu_read_bios(adev)) {
501 			dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
502 			goto success;
503 		}
504 
505 		if (amdgpu_read_platform_bios(adev)) {
506 			dev_info(adev->dev, "Fetched VBIOS from platform\n");
507 			goto success;
508 		}
509 
510 	} else {
511 		if (amdgpu_read_platform_bios(adev)) {
512 			dev_info(adev->dev, "Fetched VBIOS from platform\n");
513 			goto success;
514 		}
515 
516 		if (amdgpu_read_bios(adev)) {
517 			dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
518 			goto success;
519 		}
520 	}
521 
522 	if (amdgpu_read_bios_from_rom(adev)) {
523 		dev_info(adev->dev, "Fetched VBIOS from ROM\n");
524 		goto success;
525 	}
526 
527 	if (amdgpu_read_disabled_bios(adev)) {
528 		dev_info(adev->dev, "Fetched VBIOS from disabled ROM BAR\n");
529 		goto success;
530 	}
531 
532 	dev_err(adev->dev, "Unable to locate a BIOS ROM\n");
533 	return false;
534 
535 success:
536 	return true;
537 }
538 
539 bool amdgpu_get_bios(struct amdgpu_device *adev)
540 {
541 	bool found;
542 
543 	if (adev->flags & AMD_IS_APU)
544 		found = amdgpu_get_bios_apu(adev);
545 	else
546 		found = amdgpu_get_bios_dgpu(adev);
547 
548 	if (found)
549 		adev->is_atom_fw = adev->asic_type >= CHIP_VEGA10;
550 
551 	return found;
552 }
553 
554 /* helper function for soc15 and onwards to read bios from rom */
555 bool amdgpu_soc15_read_bios_from_rom(struct amdgpu_device *adev,
556 				     u8 *bios, u32 length_bytes)
557 {
558 	u32 *dw_ptr;
559 	u32 i, length_dw;
560 	u32 rom_offset;
561 	u32 rom_index_offset;
562 	u32 rom_data_offset;
563 
564 	if (bios == NULL)
565 		return false;
566 	if (length_bytes == 0)
567 		return false;
568 	/* APU vbios image is part of sbios image */
569 	if (adev->flags & AMD_IS_APU)
570 		return false;
571 	if (!adev->smuio.funcs ||
572 	    !adev->smuio.funcs->get_rom_index_offset ||
573 	    !adev->smuio.funcs->get_rom_data_offset)
574 		return false;
575 
576 	dw_ptr = (u32 *)bios;
577 	length_dw = ALIGN(length_bytes, 4) / 4;
578 
579 	rom_index_offset =
580 		adev->smuio.funcs->get_rom_index_offset(adev);
581 	rom_data_offset =
582 		adev->smuio.funcs->get_rom_data_offset(adev);
583 
584 	if (adev->nbio.funcs &&
585 	    adev->nbio.funcs->get_rom_offset) {
586 		rom_offset = adev->nbio.funcs->get_rom_offset(adev);
587 		rom_offset = rom_offset << 17;
588 	} else {
589 		rom_offset = 0;
590 	}
591 
592 	/* set rom index to rom_offset */
593 	WREG32(rom_index_offset, rom_offset);
594 	/* read out the rom data */
595 	for (i = 0; i < length_dw; i++)
596 		dw_ptr[i] = RREG32(rom_data_offset);
597 
598 	return true;
599 }
600