• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
37  * BIOS.
38  */
39 
40 #define AMD_VBIOS_SIGNATURE " 761295520"
41 #define AMD_VBIOS_SIGNATURE_OFFSET 0x30
42 #define AMD_VBIOS_SIGNATURE_SIZE sizeof(AMD_VBIOS_SIGNATURE)
43 #define AMD_VBIOS_SIGNATURE_END (AMD_VBIOS_SIGNATURE_OFFSET + AMD_VBIOS_SIGNATURE_SIZE)
44 #define AMD_IS_VALID_VBIOS(p) ((p)[0] == 0x55 && (p)[1] == 0xAA)
45 #define AMD_VBIOS_LENGTH(p) ((p)[2] << 9)
46 
47 /* Check if current bios is an ATOM BIOS.
48  * Return true if it is ATOM BIOS. Otherwise, return false.
49  */
check_atom_bios(uint8_t * bios,size_t size)50 static bool check_atom_bios(uint8_t *bios, size_t size)
51 {
52 	uint16_t tmp, bios_header_start;
53 
54 	if (!bios || size < 0x49) {
55 		DRM_INFO("vbios mem is null or mem size is wrong\n");
56 		return false;
57 	}
58 
59 	if (!AMD_IS_VALID_VBIOS(bios)) {
60 		DRM_INFO("BIOS signature incorrect %x %x\n", bios[0], bios[1]);
61 		return false;
62 	}
63 
64 	bios_header_start = bios[0x48] | (bios[0x49] << 8);
65 	if (!bios_header_start) {
66 		DRM_INFO("Can't locate bios header\n");
67 		return false;
68 	}
69 
70 	tmp = bios_header_start + 4;
71 	if (size < tmp) {
72 		DRM_INFO("BIOS header is broken\n");
73 		return false;
74 	}
75 
76 	if (!memcmp(bios + tmp, "ATOM", 4) ||
77 	    !memcmp(bios + tmp, "MOTA", 4)) {
78 		DRM_DEBUG("ATOMBIOS detected\n");
79 		return true;
80 	}
81 
82 	return false;
83 }
84 
85 /* If you boot an IGP board with a discrete card as the primary,
86  * the IGP rom is not accessible via the rom bar as the IGP rom is
87  * part of the system bios.  On boot, the system bios puts a
88  * copy of the igp rom at the start of vram if a discrete card is
89  * present.
90  */
igp_read_bios_from_vram(struct amdgpu_device * adev)91 static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
92 {
93 	uint8_t __iomem *bios;
94 	resource_size_t vram_base;
95 	resource_size_t size = 256 * 1024; /* ??? */
96 
97 	if (!(adev->flags & AMD_IS_APU))
98 		if (amdgpu_device_need_post(adev))
99 			return false;
100 
101 	adev->bios = NULL;
102 	vram_base = pci_resource_start(adev->pdev, 0);
103 	bios = ioremap_wc(vram_base, size);
104 	if (!bios) {
105 		return false;
106 	}
107 
108 	adev->bios = kmalloc(size, GFP_KERNEL);
109 	if (!adev->bios) {
110 		iounmap(bios);
111 		return false;
112 	}
113 	adev->bios_size = size;
114 	memcpy_fromio(adev->bios, bios, size);
115 	iounmap(bios);
116 
117 	if (!check_atom_bios(adev->bios, size)) {
118 		kfree(adev->bios);
119 		return false;
120 	}
121 
122 	return true;
123 }
124 
amdgpu_read_bios(struct amdgpu_device * adev)125 bool amdgpu_read_bios(struct amdgpu_device *adev)
126 {
127 	uint8_t __iomem *bios;
128 	size_t size;
129 
130 	adev->bios = NULL;
131 	/* XXX: some cards may return 0 for rom size? ddx has a workaround */
132 	bios = pci_map_rom(adev->pdev, &size);
133 	if (!bios) {
134 		return false;
135 	}
136 
137 	adev->bios = kzalloc(size, GFP_KERNEL);
138 	if (adev->bios == NULL) {
139 		pci_unmap_rom(adev->pdev, bios);
140 		return false;
141 	}
142 	adev->bios_size = size;
143 	memcpy_fromio(adev->bios, bios, size);
144 	pci_unmap_rom(adev->pdev, bios);
145 
146 	if (!check_atom_bios(adev->bios, size)) {
147 		kfree(adev->bios);
148 		return false;
149 	}
150 
151 	return true;
152 }
153 
amdgpu_read_bios_from_rom(struct amdgpu_device * adev)154 static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev)
155 {
156 	u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0};
157 	int len;
158 
159 	if (!adev->asic_funcs->read_bios_from_rom)
160 		return false;
161 
162 	/* validate VBIOS signature */
163 	if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false)
164 		return false;
165 	header[AMD_VBIOS_SIGNATURE_END] = 0;
166 
167 	if ((!AMD_IS_VALID_VBIOS(header)) ||
168 	    0 != memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET],
169 			AMD_VBIOS_SIGNATURE,
170 			strlen(AMD_VBIOS_SIGNATURE)))
171 		return false;
172 
173 	/* valid vbios, go on */
174 	len = AMD_VBIOS_LENGTH(header);
175 	len = ALIGN(len, 4);
176 	adev->bios = kmalloc(len, GFP_KERNEL);
177 	if (!adev->bios) {
178 		DRM_ERROR("no memory to allocate for BIOS\n");
179 		return false;
180 	}
181 	adev->bios_size = len;
182 
183 	/* read complete BIOS */
184 	amdgpu_asic_read_bios_from_rom(adev, adev->bios, len);
185 
186 	if (!check_atom_bios(adev->bios, len)) {
187 		kfree(adev->bios);
188 		return false;
189 	}
190 
191 	return true;
192 }
193 
amdgpu_read_platform_bios(struct amdgpu_device * adev)194 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
195 {
196 	phys_addr_t rom = adev->pdev->rom;
197 	size_t romlen = adev->pdev->romlen;
198 	void __iomem *bios;
199 
200 	adev->bios = NULL;
201 
202 	if (!rom || romlen == 0)
203 		return false;
204 
205 	adev->bios = kzalloc(romlen, GFP_KERNEL);
206 	if (!adev->bios)
207 		return false;
208 
209 	bios = ioremap(rom, romlen);
210 	if (!bios)
211 		goto free_bios;
212 
213 	memcpy_fromio(adev->bios, bios, romlen);
214 	iounmap(bios);
215 
216 	if (!check_atom_bios(adev->bios, romlen))
217 		goto free_bios;
218 
219 	adev->bios_size = romlen;
220 
221 	return true;
222 free_bios:
223 	kfree(adev->bios);
224 	return false;
225 }
226 
227 #ifdef CONFIG_ACPI
228 /* ATRM is used to get the BIOS on the discrete cards in
229  * dual-gpu systems.
230  */
231 /* retrieve the ROM in 4k blocks */
232 #define ATRM_BIOS_PAGE 4096
233 /**
234  * amdgpu_atrm_call - fetch a chunk of the vbios
235  *
236  * @atrm_handle: acpi ATRM handle
237  * @bios: vbios image pointer
238  * @offset: offset of vbios image data to fetch
239  * @len: length of vbios image data to fetch
240  *
241  * Executes ATRM to fetch a chunk of the discrete
242  * vbios image on PX systems (all asics).
243  * Returns the length of the buffer fetched.
244  */
amdgpu_atrm_call(acpi_handle atrm_handle,uint8_t * bios,int offset,int len)245 static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
246 			    int offset, int len)
247 {
248 	acpi_status status;
249 	union acpi_object atrm_arg_elements[2], *obj;
250 	struct acpi_object_list atrm_arg;
251 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
252 
253 	atrm_arg.count = 2;
254 	atrm_arg.pointer = &atrm_arg_elements[0];
255 
256 	atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
257 	atrm_arg_elements[0].integer.value = offset;
258 
259 	atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
260 	atrm_arg_elements[1].integer.value = len;
261 
262 	status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
263 	if (ACPI_FAILURE(status)) {
264 		printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
265 		return -ENODEV;
266 	}
267 
268 	obj = (union acpi_object *)buffer.pointer;
269 	memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
270 	len = obj->buffer.length;
271 	kfree(buffer.pointer);
272 	return len;
273 }
274 
amdgpu_atrm_get_bios(struct amdgpu_device * adev)275 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
276 {
277 	int ret;
278 	int size = 256 * 1024;
279 	int i;
280 	struct pci_dev *pdev = NULL;
281 	acpi_handle dhandle, atrm_handle;
282 	acpi_status status;
283 	bool found = false;
284 
285 	/* ATRM is for the discrete card only */
286 	if (adev->flags & AMD_IS_APU)
287 		return false;
288 
289 	/* ATRM is for on-platform devices only */
290 	if (dev_is_removable(&adev->pdev->dev))
291 		return false;
292 
293 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
294 		dhandle = ACPI_HANDLE(&pdev->dev);
295 		if (!dhandle)
296 			continue;
297 
298 		status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
299 		if (!ACPI_FAILURE(status)) {
300 			found = true;
301 			break;
302 		}
303 	}
304 
305 	if (!found) {
306 		while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
307 			dhandle = ACPI_HANDLE(&pdev->dev);
308 			if (!dhandle)
309 				continue;
310 
311 			status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
312 			if (!ACPI_FAILURE(status)) {
313 				found = true;
314 				break;
315 			}
316 		}
317 	}
318 
319 	if (!found)
320 		return false;
321 	pci_dev_put(pdev);
322 
323 	adev->bios = kmalloc(size, GFP_KERNEL);
324 	if (!adev->bios) {
325 		DRM_ERROR("Unable to allocate bios\n");
326 		return false;
327 	}
328 
329 	for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
330 		ret = amdgpu_atrm_call(atrm_handle,
331 				       adev->bios,
332 				       (i * ATRM_BIOS_PAGE),
333 				       ATRM_BIOS_PAGE);
334 		if (ret < ATRM_BIOS_PAGE)
335 			break;
336 	}
337 
338 	if (!check_atom_bios(adev->bios, size)) {
339 		kfree(adev->bios);
340 		return false;
341 	}
342 	adev->bios_size = size;
343 	return true;
344 }
345 #else
amdgpu_atrm_get_bios(struct amdgpu_device * adev)346 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
347 {
348 	return false;
349 }
350 #endif
351 
amdgpu_read_disabled_bios(struct amdgpu_device * adev)352 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
353 {
354 	if (adev->flags & AMD_IS_APU)
355 		return igp_read_bios_from_vram(adev);
356 	else
357 		return amdgpu_asic_read_disabled_bios(adev);
358 }
359 
360 #ifdef CONFIG_ACPI
amdgpu_acpi_vfct_bios(struct amdgpu_device * adev)361 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
362 {
363 	struct acpi_table_header *hdr;
364 	acpi_size tbl_size;
365 	UEFI_ACPI_VFCT *vfct;
366 	unsigned offset;
367 
368 	if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))
369 		return false;
370 	tbl_size = hdr->length;
371 	if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
372 		DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n");
373 		return false;
374 	}
375 
376 	vfct = (UEFI_ACPI_VFCT *)hdr;
377 	offset = vfct->VBIOSImageOffset;
378 
379 	while (offset < tbl_size) {
380 		GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);
381 		VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;
382 
383 		offset += sizeof(VFCT_IMAGE_HEADER);
384 		if (offset > tbl_size) {
385 			DRM_ERROR("ACPI VFCT image header truncated\n");
386 			return false;
387 		}
388 
389 		offset += vhdr->ImageLength;
390 		if (offset > tbl_size) {
391 			DRM_ERROR("ACPI VFCT image truncated\n");
392 			return false;
393 		}
394 
395 		if (vhdr->ImageLength &&
396 		    vhdr->PCIBus == adev->pdev->bus->number &&
397 		    vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&
398 		    vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) &&
399 		    vhdr->VendorID == adev->pdev->vendor &&
400 		    vhdr->DeviceID == adev->pdev->device) {
401 			adev->bios = kmemdup(&vbios->VbiosContent,
402 					     vhdr->ImageLength,
403 					     GFP_KERNEL);
404 
405 			if (!check_atom_bios(adev->bios, vhdr->ImageLength)) {
406 				kfree(adev->bios);
407 				return false;
408 			}
409 			adev->bios_size = vhdr->ImageLength;
410 			return true;
411 		}
412 	}
413 
414 	DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n");
415 	return false;
416 }
417 #else
amdgpu_acpi_vfct_bios(struct amdgpu_device * adev)418 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
419 {
420 	return false;
421 }
422 #endif
423 
amdgpu_get_bios(struct amdgpu_device * adev)424 bool amdgpu_get_bios(struct amdgpu_device *adev)
425 {
426 	if (amdgpu_atrm_get_bios(adev)) {
427 		dev_info(adev->dev, "Fetched VBIOS from ATRM\n");
428 		goto success;
429 	}
430 
431 	if (amdgpu_acpi_vfct_bios(adev)) {
432 		dev_info(adev->dev, "Fetched VBIOS from VFCT\n");
433 		goto success;
434 	}
435 
436 	if (igp_read_bios_from_vram(adev)) {
437 		dev_info(adev->dev, "Fetched VBIOS from VRAM BAR\n");
438 		goto success;
439 	}
440 
441 	if (amdgpu_read_bios(adev)) {
442 		dev_info(adev->dev, "Fetched VBIOS from ROM BAR\n");
443 		goto success;
444 	}
445 
446 	if (amdgpu_read_bios_from_rom(adev)) {
447 		dev_info(adev->dev, "Fetched VBIOS from ROM\n");
448 		goto success;
449 	}
450 
451 	if (amdgpu_read_disabled_bios(adev)) {
452 		dev_info(adev->dev, "Fetched VBIOS from disabled ROM BAR\n");
453 		goto success;
454 	}
455 
456 	if (amdgpu_read_platform_bios(adev)) {
457 		dev_info(adev->dev, "Fetched VBIOS from platform\n");
458 		goto success;
459 	}
460 
461 	DRM_ERROR("Unable to locate a BIOS ROM\n");
462 	return false;
463 
464 success:
465 	adev->is_atom_fw = (adev->asic_type >= CHIP_VEGA10) ? true : false;
466 	return true;
467 }
468