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