xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_bios.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
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/slab.h>
33 #include <linux/acpi.h>
34 
35 #if defined(__amd64__) || defined(__i386__)
36 #include <dev/isa/isareg.h>
37 #include <dev/isa/isavar.h>
38 #endif
39 
40 /*
41  * BIOS.
42  */
43 
44 #define AMD_VBIOS_SIGNATURE " 761295520"
45 #define AMD_VBIOS_SIGNATURE_OFFSET 0x30
46 #define AMD_VBIOS_SIGNATURE_SIZE sizeof(AMD_VBIOS_SIGNATURE)
47 #define AMD_VBIOS_SIGNATURE_END (AMD_VBIOS_SIGNATURE_OFFSET + AMD_VBIOS_SIGNATURE_SIZE)
48 #define AMD_IS_VALID_VBIOS(p) ((p)[0] == 0x55 && (p)[1] == 0xAA)
49 #define AMD_VBIOS_LENGTH(p) ((p)[2] << 9)
50 
51 /* Check if current bios is an ATOM BIOS.
52  * Return true if it is ATOM BIOS. Otherwise, return false.
53  */
54 static bool check_atom_bios(uint8_t *bios, size_t size)
55 {
56 	uint16_t tmp, bios_header_start;
57 
58 	if (!bios || size < 0x49) {
59 		DRM_INFO("vbios mem is null or mem size is wrong\n");
60 		return false;
61 	}
62 
63 	if (!AMD_IS_VALID_VBIOS(bios)) {
64 		DRM_INFO("BIOS signature incorrect %x %x\n", bios[0], bios[1]);
65 		return false;
66 	}
67 
68 	bios_header_start = bios[0x48] | (bios[0x49] << 8);
69 	if (!bios_header_start) {
70 		DRM_INFO("Can't locate bios header\n");
71 		return false;
72 	}
73 
74 	tmp = bios_header_start + 4;
75 	if (size < tmp) {
76 		DRM_INFO("BIOS header is broken\n");
77 		return false;
78 	}
79 
80 	if (!memcmp(bios + tmp, "ATOM", 4) ||
81 	    !memcmp(bios + tmp, "MOTA", 4)) {
82 		DRM_DEBUG("ATOMBIOS detected\n");
83 		return true;
84 	}
85 
86 	return false;
87 }
88 
89 /* If you boot an IGP board with a discrete card as the primary,
90  * the IGP rom is not accessible via the rom bar as the IGP rom is
91  * part of the system bios.  On boot, the system bios puts a
92  * copy of the igp rom at the start of vram if a discrete card is
93  * present.
94  */
95 #ifdef __linux__
96 static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
97 {
98 	uint8_t __iomem *bios;
99 	resource_size_t vram_base;
100 	resource_size_t size = 256 * 1024; /* ??? */
101 
102 	if (!(adev->flags & AMD_IS_APU))
103 		if (amdgpu_device_need_post(adev))
104 			return false;
105 
106 	adev->bios = NULL;
107 	vram_base = pci_resource_start(adev->pdev, 0);
108 	bios = ioremap_wc(vram_base, size);
109 	if (!bios) {
110 		return false;
111 	}
112 
113 	adev->bios = kmalloc(size, GFP_KERNEL);
114 	if (!adev->bios) {
115 		iounmap(bios);
116 		return false;
117 	}
118 	adev->bios_size = size;
119 	memcpy_fromio(adev->bios, bios, size);
120 	iounmap(bios);
121 
122 	if (!check_atom_bios(adev->bios, size)) {
123 		kfree(adev->bios);
124 		return false;
125 	}
126 
127 	return true;
128 }
129 #else
130 static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
131 {
132 	uint8_t __iomem *bios;
133 	resource_size_t size = 256 * 1024; /* ??? */
134 	bus_space_handle_t bsh;
135 	bus_space_tag_t bst = adev->memt;
136 
137 	if (!(adev->flags & AMD_IS_APU))
138 		if (amdgpu_device_need_post(adev))
139 			return false;
140 
141 	adev->bios = NULL;
142 
143 	if (bus_space_map(bst, adev->fb_aper_offset, size, BUS_SPACE_MAP_LINEAR, &bsh) != 0)
144 		return false;
145 
146 	bios = bus_space_vaddr(adev->memt, bsh);
147 	if (bios == NULL) {
148 		bus_space_unmap(bst, bsh, size);
149 		return false;
150 	}
151 
152 	adev->bios = kmalloc(size, GFP_KERNEL);
153 	if (!adev->bios) {
154 		bus_space_unmap(bst, bsh, size);
155 		return false;
156 	}
157 	adev->bios_size = size;
158 	memcpy_fromio(adev->bios, bios, size);
159 	bus_space_unmap(bst, bsh, size);
160 
161 	if (!check_atom_bios(adev->bios, size)) {
162 		kfree(adev->bios);
163 		return false;
164 	}
165 
166 	return true;
167 }
168 #endif
169 
170 #ifdef __linux__
171 bool amdgpu_read_bios(struct amdgpu_device *adev)
172 {
173 	uint8_t __iomem *bios;
174 	size_t size;
175 
176 	adev->bios = NULL;
177 	/* XXX: some cards may return 0 for rom size? ddx has a workaround */
178 	bios = pci_map_rom(adev->pdev, &size);
179 	if (!bios) {
180 		return false;
181 	}
182 
183 	adev->bios = kzalloc(size, GFP_KERNEL);
184 	if (adev->bios == NULL) {
185 		pci_unmap_rom(adev->pdev, bios);
186 		return false;
187 	}
188 	adev->bios_size = size;
189 	memcpy_fromio(adev->bios, bios, size);
190 	pci_unmap_rom(adev->pdev, bios);
191 
192 	if (!check_atom_bios(adev->bios, size)) {
193 		kfree(adev->bios);
194 		return false;
195 	}
196 
197 	return true;
198 }
199 #else
200 bool amdgpu_read_bios(struct amdgpu_device *adev)
201 {
202 	uint8_t __iomem *bios;
203 	size_t size;
204 	pcireg_t address, mask;
205 	bus_space_handle_t romh;
206 	int rc;
207 
208 	adev->bios = NULL;
209 	/* XXX: some cards may return 0 for rom size? ddx has a workaround */
210 
211 	address = pci_conf_read(adev->pc, adev->pa_tag, PCI_ROM_REG);
212 	pci_conf_write(adev->pc, adev->pa_tag, PCI_ROM_REG, ~PCI_ROM_ENABLE);
213 	mask = pci_conf_read(adev->pc, adev->pa_tag, PCI_ROM_REG);
214 	address |= PCI_ROM_ENABLE;
215 	pci_conf_write(adev->pc, adev->pa_tag, PCI_ROM_REG, address);
216 
217 	size = PCI_ROM_SIZE(mask);
218 	if (size == 0)
219 		return false;
220 	rc = bus_space_map(adev->memt, PCI_ROM_ADDR(address), size,
221 	    BUS_SPACE_MAP_LINEAR, &romh);
222 	if (rc != 0) {
223 		printf(": can't map PCI ROM (%d)\n", rc);
224 		return false;
225 	}
226 	bios = (uint8_t *)bus_space_vaddr(adev->memt, romh);
227 	if (!bios) {
228 		printf(": bus_space_vaddr failed\n");
229 		return false;
230 	}
231 
232 	adev->bios = kzalloc(size, GFP_KERNEL);
233 	if (adev->bios == NULL) {
234 		bus_space_unmap(adev->memt, romh, size);
235 		return false;
236 	}
237 	adev->bios_size = size;
238 	memcpy_fromio(adev->bios, bios, size);
239 	bus_space_unmap(adev->memt, romh, size);
240 
241 	if (!check_atom_bios(adev->bios, size)) {
242 		kfree(adev->bios);
243 		return false;
244 	}
245 
246 	return true;
247 }
248 #endif
249 
250 static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev)
251 {
252 	u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0};
253 	int len;
254 
255 	if (!adev->asic_funcs->read_bios_from_rom)
256 		return false;
257 
258 	/* validate VBIOS signature */
259 	if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false)
260 		return false;
261 	header[AMD_VBIOS_SIGNATURE_END] = 0;
262 
263 	if ((!AMD_IS_VALID_VBIOS(header)) ||
264 	    0 != memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET],
265 			AMD_VBIOS_SIGNATURE,
266 			strlen(AMD_VBIOS_SIGNATURE)))
267 		return false;
268 
269 	/* valid vbios, go on */
270 	len = AMD_VBIOS_LENGTH(header);
271 	len = roundup2(len, 4);
272 	adev->bios = kmalloc(len, GFP_KERNEL);
273 	if (!adev->bios) {
274 		DRM_ERROR("no memory to allocate for BIOS\n");
275 		return false;
276 	}
277 	adev->bios_size = len;
278 
279 	/* read complete BIOS */
280 	amdgpu_asic_read_bios_from_rom(adev, adev->bios, len);
281 
282 	if (!check_atom_bios(adev->bios, len)) {
283 		kfree(adev->bios);
284 		return false;
285 	}
286 
287 	return true;
288 }
289 
290 #ifdef __linux__
291 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
292 {
293 	uint8_t __iomem *bios;
294 	size_t size;
295 
296 	adev->bios = NULL;
297 
298 	bios = pci_platform_rom(adev->pdev, &size);
299 	if (!bios) {
300 		return false;
301 	}
302 
303 	adev->bios = kzalloc(size, GFP_KERNEL);
304 	if (adev->bios == NULL)
305 		return false;
306 
307 	memcpy_fromio(adev->bios, bios, size);
308 
309 	if (!check_atom_bios(adev->bios, size)) {
310 		kfree(adev->bios);
311 		return false;
312 	}
313 
314 	adev->bios_size = size;
315 
316 	return true;
317 }
318 #else
319 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
320 {
321 #if defined(__amd64__) || defined(__i386__)
322 	uint8_t __iomem *bios;
323 	bus_size_t size = 256 * 1024; /* ??? */
324 
325 	adev->bios = NULL;
326 
327 	bios = (u8 *)ISA_HOLE_VADDR(0xc0000);
328 
329 	adev->bios = kzalloc(size, GFP_KERNEL);
330 	if (adev->bios == NULL)
331 		return false;
332 
333 	memcpy_fromio(adev->bios, bios, size);
334 
335 	if (!check_atom_bios(adev->bios, size)) {
336 		kfree(adev->bios);
337 		return false;
338 	}
339 
340 	adev->bios_size = size;
341 
342 	return true;
343 #endif
344 	return false;
345 }
346 #endif
347 
348 #ifdef CONFIG_ACPI
349 /* ATRM is used to get the BIOS on the discrete cards in
350  * dual-gpu systems.
351  */
352 /* retrieve the ROM in 4k blocks */
353 #define ATRM_BIOS_PAGE 4096
354 /**
355  * amdgpu_atrm_call - fetch a chunk of the vbios
356  *
357  * @atrm_handle: acpi ATRM handle
358  * @bios: vbios image pointer
359  * @offset: offset of vbios image data to fetch
360  * @len: length of vbios image data to fetch
361  *
362  * Executes ATRM to fetch a chunk of the discrete
363  * vbios image on PX systems (all asics).
364  * Returns the length of the buffer fetched.
365  */
366 static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
367 			    int offset, int len)
368 {
369 	acpi_status status;
370 	union acpi_object atrm_arg_elements[2], *obj;
371 	struct acpi_object_list atrm_arg;
372 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
373 
374 	atrm_arg.count = 2;
375 	atrm_arg.pointer = &atrm_arg_elements[0];
376 
377 	atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
378 	atrm_arg_elements[0].integer.value = offset;
379 
380 	atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
381 	atrm_arg_elements[1].integer.value = len;
382 
383 	status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
384 	if (ACPI_FAILURE(status)) {
385 		printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
386 		return -ENODEV;
387 	}
388 
389 	obj = (union acpi_object *)buffer.pointer;
390 	memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
391 	len = obj->buffer.length;
392 	kfree(buffer.pointer);
393 	return len;
394 }
395 
396 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
397 {
398 	int ret;
399 	int size = 256 * 1024;
400 	int i;
401 	struct pci_dev *pdev = NULL;
402 	acpi_handle dhandle, atrm_handle;
403 	acpi_status status;
404 	bool found = false;
405 
406 	/* ATRM is for the discrete card only */
407 	if (adev->flags & AMD_IS_APU)
408 		return false;
409 
410 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
411 		dhandle = ACPI_HANDLE(&pdev->dev);
412 		if (!dhandle)
413 			continue;
414 
415 		status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
416 		if (!ACPI_FAILURE(status)) {
417 			found = true;
418 			break;
419 		}
420 	}
421 
422 	if (!found) {
423 		while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
424 			dhandle = ACPI_HANDLE(&pdev->dev);
425 			if (!dhandle)
426 				continue;
427 
428 			status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
429 			if (!ACPI_FAILURE(status)) {
430 				found = true;
431 				break;
432 			}
433 		}
434 	}
435 
436 	if (!found)
437 		return false;
438 
439 	adev->bios = kmalloc(size, GFP_KERNEL);
440 	if (!adev->bios) {
441 		DRM_ERROR("Unable to allocate bios\n");
442 		return false;
443 	}
444 
445 	for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
446 		ret = amdgpu_atrm_call(atrm_handle,
447 				       adev->bios,
448 				       (i * ATRM_BIOS_PAGE),
449 				       ATRM_BIOS_PAGE);
450 		if (ret < ATRM_BIOS_PAGE)
451 			break;
452 	}
453 
454 	if (!check_atom_bios(adev->bios, size)) {
455 		kfree(adev->bios);
456 		return false;
457 	}
458 	adev->bios_size = size;
459 	return true;
460 }
461 #else
462 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
463 {
464 	return false;
465 }
466 #endif
467 
468 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
469 {
470 	if (adev->flags & AMD_IS_APU)
471 		return igp_read_bios_from_vram(adev);
472 	else
473 		return amdgpu_asic_read_disabled_bios(adev);
474 }
475 
476 #ifdef CONFIG_ACPI
477 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
478 {
479 	struct acpi_table_header *hdr;
480 	acpi_size tbl_size;
481 	UEFI_ACPI_VFCT *vfct;
482 	unsigned offset;
483 
484 	if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))
485 		return false;
486 	tbl_size = hdr->length;
487 	if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
488 		DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n");
489 		return false;
490 	}
491 
492 	vfct = (UEFI_ACPI_VFCT *)hdr;
493 	offset = vfct->VBIOSImageOffset;
494 
495 	while (offset < tbl_size) {
496 		GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);
497 		VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;
498 
499 		offset += sizeof(VFCT_IMAGE_HEADER);
500 		if (offset > tbl_size) {
501 			DRM_ERROR("ACPI VFCT image header truncated\n");
502 			return false;
503 		}
504 
505 		offset += vhdr->ImageLength;
506 		if (offset > tbl_size) {
507 			DRM_ERROR("ACPI VFCT image truncated\n");
508 			return false;
509 		}
510 
511 		if (vhdr->ImageLength &&
512 		    vhdr->PCIBus == adev->pdev->bus->number &&
513 		    vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&
514 		    vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) &&
515 		    vhdr->VendorID == adev->pdev->vendor &&
516 		    vhdr->DeviceID == adev->pdev->device) {
517 			adev->bios = kmemdup(&vbios->VbiosContent,
518 					     vhdr->ImageLength,
519 					     GFP_KERNEL);
520 
521 			if (!check_atom_bios(adev->bios, vhdr->ImageLength)) {
522 				kfree(adev->bios);
523 				return false;
524 			}
525 			adev->bios_size = vhdr->ImageLength;
526 			return true;
527 		}
528 	}
529 
530 	DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n");
531 	return false;
532 }
533 #else
534 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
535 {
536 	return false;
537 }
538 #endif
539 
540 bool amdgpu_get_bios(struct amdgpu_device *adev)
541 {
542 	if (amdgpu_atrm_get_bios(adev))
543 		goto success;
544 
545 	if (amdgpu_acpi_vfct_bios(adev))
546 		goto success;
547 
548 	if (igp_read_bios_from_vram(adev))
549 		goto success;
550 
551 	if (amdgpu_read_bios(adev))
552 		goto success;
553 
554 	if (amdgpu_read_bios_from_rom(adev))
555 		goto success;
556 
557 	if (amdgpu_read_disabled_bios(adev))
558 		goto success;
559 
560 	if (amdgpu_read_platform_bios(adev))
561 		goto success;
562 
563 	DRM_ERROR("Unable to locate a BIOS ROM\n");
564 	return false;
565 
566 success:
567 	adev->is_atom_fw = (adev->asic_type >= CHIP_VEGA10) ? true : false;
568 	return true;
569 }
570