amdgpu_bios.c revision 1.4 1 /* $NetBSD: amdgpu_bios.c,v 1.4 2021/12/18 23:44:58 riastradh Exp $ */
2
3 /*
4 * Copyright 2008 Advanced Micro Devices, Inc.
5 * Copyright 2008 Red Hat Inc.
6 * Copyright 2009 Jerome Glisse.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors: Dave Airlie
27 * Alex Deucher
28 * Jerome Glisse
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: amdgpu_bios.c,v 1.4 2021/12/18 23:44:58 riastradh Exp $");
33
34 #include "amdgpu.h"
35 #include "atom.h"
36
37 #include <linux/pci.h>
38 #include <linux/slab.h>
39 #include <linux/acpi.h>
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 static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
96 {
97 #ifdef __NetBSD__
98 bus_space_tag_t bst;
99 bus_space_handle_t bsh;
100 bus_size_t size;
101 #else
102 uint8_t __iomem *bios;
103 resource_size_t vram_base;
104 resource_size_t size = 256 * 1024; /* ??? */
105 #endif
106
107 if (!(adev->flags & AMD_IS_APU))
108 if (amdgpu_device_need_post(adev))
109 return false;
110
111 adev->bios = NULL;
112 #ifdef __NetBSD__
113 if (pci_mapreg_map(&adev->pdev->pd_pa, PCI_BAR(0),
114 /* XXX Dunno what type to expect here; fill me in... */
115 pci_mapreg_type(adev->pdev->pd_pa.pa_pc,
116 adev->pdev->pd_pa.pa_tag, PCI_BAR(0)),
117 0, &bst, &bsh, NULL, &size))
118 return false;
119 if ((size == 0) ||
120 (size < 256 * 1024) ||
121 (bus_space_read_1(bst, bsh, 0) != 0x55) ||
122 (bus_space_read_1(bst, bsh, 1) != 0xaa) ||
123 ((adev->bios = kmalloc(size, GFP_KERNEL)) == NULL)) {
124 bus_space_unmap(bst, bsh, size);
125 return false;
126 }
127 bus_space_read_region_1(bst, bsh, 0, adev->bios, size);
128 bus_space_unmap(bst, bsh, size);
129 #else
130 vram_base = pci_resource_start(adev->pdev, 0);
131 bios = ioremap_wc(vram_base, size);
132 if (!bios) {
133 return false;
134 }
135
136 adev->bios = kmalloc(size, GFP_KERNEL);
137 if (!adev->bios) {
138 iounmap(bios);
139 return false;
140 }
141 adev->bios_size = size;
142 memcpy_fromio(adev->bios, bios, size);
143 iounmap(bios);
144 #endif
145
146 if (!check_atom_bios(adev->bios, size)) {
147 kfree(adev->bios);
148 return false;
149 }
150
151 return true;
152 }
153
154 #ifdef __NetBSD__
155 # define __iomem __pci_rom_iomem
156 #endif
157
158 bool amdgpu_read_bios(struct amdgpu_device *adev)
159 {
160 uint8_t __iomem *bios;
161 size_t size;
162
163 adev->bios = NULL;
164 /* XXX: some cards may return 0 for rom size? ddx has a workaround */
165 bios = pci_map_rom(adev->pdev, &size);
166 if (!bios) {
167 return false;
168 }
169
170 adev->bios = kzalloc(size, GFP_KERNEL);
171 if (adev->bios == NULL) {
172 pci_unmap_rom(adev->pdev, bios);
173 return false;
174 }
175 adev->bios_size = size;
176 memcpy_fromio(adev->bios, bios, size);
177 pci_unmap_rom(adev->pdev, bios);
178
179 if (!check_atom_bios(adev->bios, size)) {
180 kfree(adev->bios);
181 return false;
182 }
183
184 return true;
185 }
186
187 static bool amdgpu_read_bios_from_rom(struct amdgpu_device *adev)
188 {
189 u8 header[AMD_VBIOS_SIGNATURE_END+1] = {0};
190 int len;
191
192 if (!adev->asic_funcs->read_bios_from_rom)
193 return false;
194
195 /* validate VBIOS signature */
196 if (amdgpu_asic_read_bios_from_rom(adev, &header[0], sizeof(header)) == false)
197 return false;
198 header[AMD_VBIOS_SIGNATURE_END] = 0;
199
200 if ((!AMD_IS_VALID_VBIOS(header)) ||
201 0 != memcmp((char *)&header[AMD_VBIOS_SIGNATURE_OFFSET],
202 AMD_VBIOS_SIGNATURE,
203 strlen(AMD_VBIOS_SIGNATURE)))
204 return false;
205
206 /* valid vbios, go on */
207 len = AMD_VBIOS_LENGTH(header);
208 len = ALIGN(len, 4);
209 adev->bios = kmalloc(len, GFP_KERNEL);
210 if (!adev->bios) {
211 DRM_ERROR("no memory to allocate for BIOS\n");
212 return false;
213 }
214 adev->bios_size = len;
215
216 /* read complete BIOS */
217 amdgpu_asic_read_bios_from_rom(adev, adev->bios, len);
218
219 if (!check_atom_bios(adev->bios, len)) {
220 kfree(adev->bios);
221 return false;
222 }
223
224 return true;
225 }
226
227 #ifdef __NetBSD__
228 # undef __iomem
229 #endif
230
231 static bool amdgpu_read_platform_bios(struct amdgpu_device *adev)
232 {
233 #ifdef __NetBSD__ /* XXX amdgpu platform bios */
234 return false;
235 #else
236 uint8_t __iomem *bios;
237 size_t size;
238
239 adev->bios = NULL;
240
241 bios = pci_platform_rom(adev->pdev, &size);
242 if (!bios) {
243 return false;
244 }
245
246 adev->bios = kzalloc(size, GFP_KERNEL);
247 if (adev->bios == NULL)
248 return false;
249
250 memcpy_fromio(adev->bios, bios, size);
251
252 if (!check_atom_bios(adev->bios, size)) {
253 kfree(adev->bios);
254 return false;
255 }
256
257 adev->bios_size = size;
258
259 return true;
260 #endif /* __NetBSD__ */
261 }
262
263 /* XXX amdgpu acpi */
264 #ifdef CONFIG_ACPI
265 /* ATRM is used to get the BIOS on the discrete cards in
266 * dual-gpu systems.
267 */
268 /* retrieve the ROM in 4k blocks */
269 #define ATRM_BIOS_PAGE 4096
270 /**
271 * amdgpu_atrm_call - fetch a chunk of the vbios
272 *
273 * @atrm_handle: acpi ATRM handle
274 * @bios: vbios image pointer
275 * @offset: offset of vbios image data to fetch
276 * @len: length of vbios image data to fetch
277 *
278 * Executes ATRM to fetch a chunk of the discrete
279 * vbios image on PX systems (all asics).
280 * Returns the length of the buffer fetched.
281 */
282 static int amdgpu_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
283 int offset, int len)
284 {
285 acpi_status status;
286 union acpi_object atrm_arg_elements[2], *obj;
287 struct acpi_object_list atrm_arg;
288 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
289
290 atrm_arg.count = 2;
291 atrm_arg.pointer = &atrm_arg_elements[0];
292
293 atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
294 atrm_arg_elements[0].integer.value = offset;
295
296 atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
297 atrm_arg_elements[1].integer.value = len;
298
299 status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
300 if (ACPI_FAILURE(status)) {
301 printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
302 return -ENODEV;
303 }
304
305 obj = (union acpi_object *)buffer.pointer;
306 memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
307 len = obj->buffer.length;
308 kfree(buffer.pointer);
309 return len;
310 }
311
312 static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
313 {
314 int ret;
315 int size = 256 * 1024;
316 int i;
317 struct pci_dev *pdev = NULL;
318 acpi_handle dhandle, atrm_handle;
319 acpi_status status;
320 bool found = false;
321
322 /* ATRM is for the discrete card only */
323 if (adev->flags & AMD_IS_APU)
324 return false;
325
326 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
327 dhandle = ACPI_HANDLE(&pdev->dev);
328 if (!dhandle)
329 continue;
330
331 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
332 if (!ACPI_FAILURE(status)) {
333 found = true;
334 break;
335 }
336 }
337
338 if (!found) {
339 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
340 dhandle = ACPI_HANDLE(&pdev->dev);
341 if (!dhandle)
342 continue;
343
344 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
345 if (!ACPI_FAILURE(status)) {
346 found = true;
347 break;
348 }
349 }
350 }
351
352 if (!found)
353 return false;
354
355 adev->bios = kmalloc(size, GFP_KERNEL);
356 if (!adev->bios) {
357 DRM_ERROR("Unable to allocate bios\n");
358 return false;
359 }
360
361 for (i = 0; i < size / ATRM_BIOS_PAGE; i++) {
362 ret = amdgpu_atrm_call(atrm_handle,
363 adev->bios,
364 (i * ATRM_BIOS_PAGE),
365 ATRM_BIOS_PAGE);
366 if (ret < ATRM_BIOS_PAGE)
367 break;
368 }
369
370 if (!check_atom_bios(adev->bios, size)) {
371 kfree(adev->bios);
372 return false;
373 }
374 adev->bios_size = size;
375 return true;
376 }
377 #else
378 static inline bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
379 {
380 return false;
381 }
382 #endif
383
384 static bool amdgpu_read_disabled_bios(struct amdgpu_device *adev)
385 {
386 if (adev->flags & AMD_IS_APU)
387 return igp_read_bios_from_vram(adev);
388 else
389 return amdgpu_asic_read_disabled_bios(adev);
390 }
391
392 #ifdef CONFIG_ACPI
393 static bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
394 {
395 struct acpi_table_header *hdr;
396 acpi_size tbl_size;
397 UEFI_ACPI_VFCT *vfct;
398 unsigned offset;
399
400 if (!ACPI_SUCCESS(acpi_get_table("VFCT", 1, &hdr)))
401 return false;
402 tbl_size = hdr->length;
403 if (tbl_size < sizeof(UEFI_ACPI_VFCT)) {
404 DRM_ERROR("ACPI VFCT table present but broken (too short #1)\n");
405 return false;
406 }
407
408 vfct = (UEFI_ACPI_VFCT *)hdr;
409 offset = vfct->VBIOSImageOffset;
410
411 while (offset < tbl_size) {
412 GOP_VBIOS_CONTENT *vbios = (GOP_VBIOS_CONTENT *)((char *)hdr + offset);
413 VFCT_IMAGE_HEADER *vhdr = &vbios->VbiosHeader;
414
415 offset += sizeof(VFCT_IMAGE_HEADER);
416 if (offset > tbl_size) {
417 DRM_ERROR("ACPI VFCT image header truncated\n");
418 return false;
419 }
420
421 offset += vhdr->ImageLength;
422 if (offset > tbl_size) {
423 DRM_ERROR("ACPI VFCT image truncated\n");
424 return false;
425 }
426
427 if (vhdr->ImageLength &&
428 vhdr->PCIBus == adev->pdev->bus->number &&
429 vhdr->PCIDevice == PCI_SLOT(adev->pdev->devfn) &&
430 vhdr->PCIFunction == PCI_FUNC(adev->pdev->devfn) &&
431 vhdr->VendorID == adev->pdev->vendor &&
432 vhdr->DeviceID == adev->pdev->device) {
433 adev->bios = kmemdup(&vbios->VbiosContent,
434 vhdr->ImageLength,
435 GFP_KERNEL);
436
437 if (!check_atom_bios(adev->bios, vhdr->ImageLength)) {
438 kfree(adev->bios);
439 return false;
440 }
441 adev->bios_size = vhdr->ImageLength;
442 return true;
443 }
444 }
445
446 DRM_ERROR("ACPI VFCT table present but broken (too short #2)\n");
447 return false;
448 }
449 #else
450 static inline bool amdgpu_acpi_vfct_bios(struct amdgpu_device *adev)
451 {
452 return false;
453 }
454 #endif
455
456 bool amdgpu_get_bios(struct amdgpu_device *adev)
457 {
458 if (amdgpu_atrm_get_bios(adev))
459 goto success;
460
461 if (amdgpu_acpi_vfct_bios(adev))
462 goto success;
463
464 if (igp_read_bios_from_vram(adev))
465 goto success;
466
467 if (amdgpu_read_bios(adev))
468 goto success;
469
470 if (amdgpu_read_bios_from_rom(adev))
471 goto success;
472
473 if (amdgpu_read_disabled_bios(adev))
474 goto success;
475
476 if (amdgpu_read_platform_bios(adev))
477 goto success;
478
479 DRM_ERROR("Unable to locate a BIOS ROM\n");
480 return false;
481
482 success:
483 adev->is_atom_fw = (adev->asic_type >= CHIP_VEGA10) ? true : false;
484 return true;
485 }
486