aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c
diff options
context:
space:
mode:
authorAlex Deucher <alexander.deucher@amd.com>2015-04-20 16:55:21 -0400
committerAlex Deucher <alexander.deucher@amd.com>2015-06-03 21:03:15 -0400
commitd38ceaf99ed015f2a0b9af3499791bd3a3daae21 (patch)
treec8e237ea218e8ed8a5f64c1654fc01fe5d2239cb /drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c
parent97b2e202fba05b87d720318a6500a337100dab4d (diff)
drm/amdgpu: add core driver (v4)
This adds the non-asic specific core driver code. v2: remove extra kconfig option v3: implement minor fixes from Fengguang Wu v4: fix cast in amdgpu_ucode.c Acked-by: Christian König <christian.koenig@amd.com> Acked-by: Jammy Zhou <Jammy.Zhou@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
Diffstat (limited to 'drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c')
-rw-r--r--drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c359
1 files changed, 359 insertions, 0 deletions
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c
new file mode 100644
index 000000000000..d7a3ab2624f0
--- /dev/null
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c
@@ -0,0 +1,359 @@
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/vga_switcheroo.h>
33#include <linux/slab.h>
34#include <linux/acpi.h>
35/*
36 * BIOS.
37 */
38
39/* If you boot an IGP board with a discrete card as the primary,
40 * the IGP rom is not accessible via the rom bar as the IGP rom is
41 * part of the system bios. On boot, the system bios puts a
42 * copy of the igp rom at the start of vram if a discrete card is
43 * present.
44 */
45static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
46{
47 uint8_t __iomem *bios;
48 resource_size_t vram_base;
49 resource_size_t size = 256 * 1024; /* ??? */
50
51 if (!(adev->flags & AMDGPU_IS_APU))
52 if (!amdgpu_card_posted(adev))
53 return false;
54
55 adev->bios = NULL;
56 vram_base = pci_resource_start(adev->pdev, 0);
57 bios = ioremap(vram_base, size);
58 if (!bios) {
59 return false;
60 }
61
62 if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
63 iounmap(bios);
64 return false;
65 }
66 adev->bios = kmalloc(size, GFP_KERNEL);
67 if (adev->bios == NULL) {
68 iounmap(bios);
69 return false;
70 }
71 memcpy_fromio(adev->bios, bios, size);
72 iounmap(bios);
73 return true;
74}
75
76bool amdgpu_read_bios(struct amdgpu_device *adev)
77{
78 uint8_t __iomem *bios;
79 size_t size;
80
81 adev->bios = NULL;
82 /* XXX: some cards may return 0 for rom size? ddx has a workaround */
83 bios = pci_map_rom(adev->pdev, &size);
84 if (!bios) {
85 return false;
86 }
87
88 if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
89 pci_unmap_rom(adev->pdev, bios);
90 return false;
91 }
92 adev->bios = kmemdup(bios, size, GFP_KERNEL);
93 if (adev->bios == NULL) {
94 pci_unmap_rom(adev->pdev, bios);
95 return false;
96 }
97 pci_unmap_rom(adev->pdev, bios);
98 return true;
99}
100
101static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
102{
103 uint8_t __iomem *bios;
104 size_t size;
105
106 adev->bios = NULL;
107
108 bios = pci_platform_rom(adev->pdev, &size);
109 if (!bios) {
110 return false;
111 }
112
113 if (size == 0 || bios[0] != 0x55 || bios[1] != 0xaa) {
114 return false;
115 }
116 adev->bios = kmemdup(bios, size, GFP_KERNEL);
117 if (adev->bios == NULL) {
118 return false;
119 }
120
121 return true;
122}
123
124#ifdef CONFIG_ACPI
125/* ATRM is used to get the BIOS on the discrete cards in
126 * dual-gpu systems.
127 */
128/* retrieve the ROM in 4k blocks */
129#define ATRM_BIOS_PAGE 4096
130/**
131 * amdgpu_atrm_call - fetch a chunk of the vbios
132 *
133 * @atrm_handle: acpi ATRM handle
134 * @bios: vbios image pointer
135 * @offset: offset of vbios image data to fetch
136 * @len: length of vbios image data to fetch
137 *
138 * Executes ATRM to fetch a chunk of the discrete
139 * vbios image on PX systems (all asics).
140 * Returns the length of the buffer fetched.
141 */
142static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
143 int offset, int len)
144{
145 acpi_status status;
146 union acpi_object atrm_arg_elements[2], *obj;
147 struct acpi_object_list atrm_arg;
148 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
149
150 atrm_arg.count = 2;
151 atrm_arg.pointer = &atrm_arg_elements[0];
152
153 atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
154 atrm_arg_elements[0].integer.value = offset;
155
156 atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
157 atrm_arg_elements[1].integer.value = len;
158
159 status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
160 if (ACPI_FAILURE(status)) {
161 printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
162 return -ENODEV;
163 }
164
165 obj = (union acpi_object *)buffer.pointer;
166 memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
167 len = obj->buffer.length;
168 kfree(buffer.pointer);
169 return len;
170}
171
172static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
173{
174 int ret;
175 int size = 256 * 1024;
176 int i;
177 struct pci_dev *pdev = NULL;
178 acpi_handle dhandle, atrm_handle;
179 acpi_status status;
180 bool found = false;
181
182 /* ATRM is for the discrete card only */
183 if (adev->flags & AMDGPU_IS_APU)
184 return false;
185
186 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
187 dhandle = ACPI_HANDLE(&pdev->dev);
188 if (!dhandle)
189 continue;
190
191 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
192 if (!ACPI_FAILURE(status)) {
193 found = true;
194 break;
195 }
196 }
197
198 if (!found) {
199 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
200 dhandle = ACPI_HANDLE(&pdev->dev);
201 if (!dhandle)
202 continue;
203
204 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
205 if (!ACPI_FAILURE(status)) {
206 found = true;
207 break;
208 }
209 }
210 }
211
212 if (!found)
213 return false;
214
215 adev->bios = kmalloc(size, GFP_KERNEL);
216 if (!adev->bios) {
217 DRM_ERROR("Unable to allocate bios\n");
218 return false;
219 }
220
221 for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
222 ret = amdgpu_atrm_call(atrm_handle,
223 adev->bios,
224 (i * ATRM_BIOS_PAGE),
225 ATRM_BIOS_PAGE);
226 if (ret < ATRM_BIOS_PAGE)
227 break;
228 }
229
230 if (i == 0 || adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) {
231 kfree(adev->bios);
232 return false;
233 }
234 return true;
235}
236#else
237static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
238{
239 return false;
240}
241#endif
242
243static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
244{
245 if (adev->flags & AMDGPU_IS_APU)
246 return igp_read_bios_from_vram(adev);
247 else
248 return amdgpu_asic_read_disabled_bios(adev);
249}
250
251#ifdef CONFIG_ACPI
252static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
253{
254 bool ret = false;
255 struct acpi_table_header *hdr;
256 acpi_size tbl_size;
257 UEFI_ACPI_VFCT *vfct;
258 GOP_VBIOS_CONTENT *vbios;
259 VFCT_IMAGE_HEADER *vhdr;
260
261 if (!ACPI_SUCCESS(acpi_get_table_with_size("VFCT", 1, &hdr, &tbl_size)))
262 return false;
263 if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
264 DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n");
265 goto out_unmap;
266 }
267
268 vfct = (UEFI_ACPI_VFCT *)hdr;
269 if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) > tbl_size) {
270 DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n");
271 goto out_unmap;
272 }
273
274 vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + vfct->VBIOSImageOffset);
275 vhdr = &vbios->VbiosHeader;
276 DRM_INFO("ACPI VFCT contains a BIOS for %02x:%02x.%d %04x:%04x, size %d\n",
277 vhdr->PCIBus, vhdr->PCIDevice, vhdr->PCIFunction,
278 vhdr->VendorID, vhdr->DeviceID, vhdr->ImageLength);
279
280 if (vhdr->PCIBus != adev->pdev->bus->number ||
281 vhdr->PCIDevice != PCI_SLOT(adev->pdev->devfn) ||
282 vhdr->PCIFunction != PCI_FUNC(adev->pdev->devfn) ||
283 vhdr->VendorID != adev->pdev->vendor ||
284 vhdr->DeviceID != adev->pdev->device) {
285 DRM_INFO("ACPI VFCT table is not for this card\n");
286 goto out_unmap;
287 }
288
289 if (vfct->VBIOSImageOffset + sizeof(VFCT_IMAGE_HEADER) + vhdr->ImageLength > tbl_size) {
290 DRM_ERROR("ACPI VFCT image truncated\n");
291 goto out_unmap;
292 }
293
294 adev->bios = kmemdup(&vbios->VbiosContent, vhdr->ImageLength, GFP_KERNEL);
295 ret = !!adev->bios;
296
297out_unmap:
298 return ret;
299}
300#else
301static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
302{
303 return false;
304}
305#endif
306
307bool amdgpu_get_bios(struct amdgpu_device *adev)
308{
309 bool r;
310 uint16_t tmp;
311
312 r = amdgpu_atrm_get_bios(adev);
313 if (r == false)
314 r = amdgpu_acpi_vfct_bios(adev);
315 if (r == false)
316 r = igp_read_bios_from_vram(adev);
317 if (r == false)
318 r = amdgpu_read_bios(adev);
319 if (r == false) {
320 r = amdgpu_read_disabled_bios(adev);
321 }
322 if (r == false) {
323 r = amdgpu_read_platform_bios(adev);
324 }
325 if (r == false || adev->bios == NULL) {
326 DRM_ERROR("Unable to locate a BIOS ROM\n");
327 adev->bios = NULL;
328 return false;
329 }
330 if (adev->bios[0] != 0x55 || adev->bios[1] != 0xaa) {
331 printk("BIOS signature incorrect %x %x\n", adev->bios[0], adev->bios[1]);
332 goto free_bios;
333 }
334
335 tmp = RBIOS16(0x18);
336 if (RBIOS8(tmp + 0x14) != 0x0) {
337 DRM_INFO("Not an x86 BIOS ROM, not using.\n");
338 goto free_bios;
339 }
340
341 adev->bios_header_start = RBIOS16(0x48);
342 if (!adev->bios_header_start) {
343 goto free_bios;
344 }
345 tmp = adev->bios_header_start + 4;
346 if (!memcmp(adev->bios + tmp, "ATOM", 4) ||
347 !memcmp(adev->bios + tmp, "MOTA", 4)) {
348 adev->is_atom_bios = true;
349 } else {
350 adev->is_atom_bios = false;
351 }
352
353 DRM_DEBUG("%sBIOS detected\n", adev->is_atom_bios ? "ATOM" : "COM");
354 return true;
355free_bios:
356 kfree(adev->bios);
357 adev->bios = NULL;
358 return false;
359}