1/* $NetBSD: nouveau_subdev_bios_base.c,v 1.3 2015/10/27 13:13:47 riastradh Exp $ */
2
3/*
4 * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_bios_base.c,v 1.3 2015/10/27 13:13:47 riastradh Exp $");
29
30#include <core/object.h>
31#include <core/device.h>
32#include <core/subdev.h>
33#include <core/option.h>
34
35#include <subdev/bios.h>
36#include <subdev/bios/bmp.h>
37#include <subdev/bios/bit.h>
38
39u8
40nvbios_checksum(const u8 *data, int size)
41{
42 u8 sum = 0;
43 while (size--)
44 sum += *data++;
45 return sum;
46}
47
48u16
49nvbios_findstr(const u8 *data, int size, const char *str, int len)
50{
51 int i, j;
52
53 for (i = 0; i <= (size - len); i++) {
54 for (j = 0; j < len; j++)
55 if ((char)data[i + j] != str[j])
56 break;
57 if (j == len)
58 return i;
59 }
60
61 return 0;
62}
63
64#if defined(__powerpc__)
65static void
66nouveau_bios_shadow_of(struct nouveau_bios *bios)
67{
68 struct pci_dev *pdev = nv_device(bios)->pdev;
69 struct device_node *dn;
70 const u32 *data;
71 int size;
72
73 dn = pci_device_to_OF_node(pdev);
74 if (!dn) {
75 nv_info(bios, "Unable to get the OF node\n");
76 return;
77 }
78
79 data = of_get_property(dn, "NVDA,BMP", &size);
80 if (data && size) {
81 bios->size = size;
82 bios->data = kmalloc(bios->size, GFP_KERNEL);
83 if (bios->data)
84 memcpy(bios->data, data, size);
85 }
86}
87#endif
88
89static void
90nouveau_bios_shadow_pramin(struct nouveau_bios *bios)
91{
92 struct nouveau_device *device = nv_device(bios);
93 u64 addr = 0;
94 u32 bar0 = 0;
95 int i;
96
97 if (device->card_type >= NV_50) {
98 if (device->card_type >= NV_C0 && device->card_type < GM100) {
99 if (nv_rd32(bios, 0x022500) & 0x00000001)
100 return;
101 } else
102 if (device->card_type >= GM100) {
103 if (nv_rd32(bios, 0x021c04) & 0x00000001)
104 return;
105 }
106
107 addr = nv_rd32(bios, 0x619f04);
108 if (!(addr & 0x00000008)) {
109 nv_debug(bios, "... not enabled\n");
110 return;
111 }
112 if ( (addr & 0x00000003) != 1) {
113 nv_debug(bios, "... not in vram\n");
114 return;
115 }
116
117 addr = (addr & 0xffffff00) << 8;
118 if (!addr) {
119 addr = (u64)nv_rd32(bios, 0x001700) << 16;
120 addr += 0xf0000;
121 }
122
123 bar0 = nv_mask(bios, 0x001700, 0xffffffff, addr >> 16);
124 }
125
126 /* bail if no rom signature */
127 if (nv_rd08(bios, 0x700000) != 0x55 ||
128 nv_rd08(bios, 0x700001) != 0xaa)
129 goto out;
130
131 bios->size = nv_rd08(bios, 0x700002) * 512;
132 if (!bios->size)
133 goto out;
134
135 bios->data = kmalloc(bios->size, GFP_KERNEL);
136 if (bios->data) {
137 for (i = 0; i < bios->size; i++)
138 nv_wo08(bios, i, nv_rd08(bios, 0x700000 + i));
139 }
140
141out:
142 if (device->card_type >= NV_50)
143 nv_wr32(bios, 0x001700, bar0);
144}
145
146static void
147nouveau_bios_shadow_prom(struct nouveau_bios *bios)
148{
149 struct nouveau_device *device = nv_device(bios);
150 u32 pcireg, access;
151 u16 pcir;
152 int i;
153
154 /* there is no prom on nv4x IGP's */
155 if (device->card_type == NV_40 && device->chipset >= 0x4c)
156 return;
157
158 /* enable access to rom */
159 if (device->card_type >= NV_50)
160 pcireg = 0x088050;
161 else
162 pcireg = 0x001850;
163 access = nv_mask(bios, pcireg, 0x00000001, 0x00000000);
164
165 /* WARNING: PROM accesses should always be 32-bits aligned. Other
166 * accesses work on most chipset but do not on Kepler chipsets
167 */
168
169 /* bail if no rom signature, with a workaround for a PROM reading
170 * issue on some chipsets. the first read after a period of
171 * inactivity returns the wrong result, so retry the first header
172 * byte a few times before giving up as a workaround
173 */
174 i = 16;
175 do {
176 u32 data = le32_to_cpu(nv_rd32(bios, 0x300000)) & 0xffff;
177 if (data == 0xaa55)
178 break;
179 } while (i--);
180
181 if (!i)
182 goto out;
183
184 /* read entire bios image to system memory */
185 bios->size = (le32_to_cpu(nv_rd32(bios, 0x300000)) >> 16) & 0xff;
186 bios->size = bios->size * 512;
187 if (!bios->size)
188 goto out;
189
190 bios->data = kmalloc(bios->size, GFP_KERNEL);
191 if (bios->data) {
192 for (i = 0; i < bios->size; i += 4)
193 ((u32 *)bios->data)[i/4] = nv_rd32(bios, 0x300000 + i);
194 }
195
196 /* check the PCI record header */
197 pcir = nv_ro16(bios, 0x0018);
198 if (bios->data[pcir + 0] != 'P' ||
199 bios->data[pcir + 1] != 'C' ||
200 bios->data[pcir + 2] != 'I' ||
201 bios->data[pcir + 3] != 'R') {
202 bios->size = 0;
203 kfree(bios->data);
204 }
205
206out:
207 /* disable access to rom */
208 nv_wr32(bios, pcireg, access);
209}
210
211#if defined(CONFIG_ACPI) && defined(CONFIG_X86)
212int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
213bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
214#else
215static inline bool
216nouveau_acpi_rom_supported(struct pci_dev *pdev) {
217 return false;
218}
219
220static inline int
221nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
222 return -EINVAL;
223}
224#endif
225
226static void
227nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
228{
229 struct pci_dev *pdev = nv_device(bios)->pdev;
230 int ret, cnt, i;
231
232 if (!nouveau_acpi_rom_supported(pdev)) {
233 bios->data = NULL;
234 return;
235 }
236
237 bios->size = 0;
238 bios->data = kmalloc(4096, GFP_KERNEL);
239 if (bios->data) {
240 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
241 bios->size = bios->data[2] * 512;
242 kfree(bios->data);
243 }
244
245 if (!bios->size)
246 return;
247
248 bios->data = kmalloc(bios->size, GFP_KERNEL);
249 if (bios->data) {
250 /* disobey the acpi spec - much faster on at least w530 ... */
251 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
252 if (ret != bios->size ||
253 nvbios_checksum(bios->data, bios->size)) {
254 /* ... that didn't work, ok, i'll be good now */
255 for (i = 0; i < bios->size; i += cnt) {
256 cnt = min((bios->size - i), (u32)4096);
257 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
258 if (ret != cnt)
259 break;
260 }
261 }
262 }
263}
264
265#ifdef __NetBSD__
266# define __iomem __pci_rom_iomem
267#endif
268
269static void
270nouveau_bios_shadow_pci(struct nouveau_bios *bios)
271{
272 struct pci_dev *pdev = nv_device(bios)->pdev;
273 size_t size;
274
275 if (!pci_enable_rom(pdev)) {
276 void __iomem *rom = pci_map_rom(pdev, &size);
277 if (rom && size) {
278 bios->data = kmalloc(size, GFP_KERNEL);
279 if (bios->data) {
280 memcpy_fromio(bios->data, rom, size);
281 bios->size = size;
282 }
283 }
284 if (rom)
285 pci_unmap_rom(pdev, rom);
286
287 pci_disable_rom(pdev);
288 }
289}
290
291static void
292nouveau_bios_shadow_platform(struct nouveau_bios *bios)
293{
294 struct pci_dev *pdev = nv_device(bios)->pdev;
295 size_t size;
296
297 void __iomem *rom = pci_platform_rom(pdev, &size);
298 if (rom && size) {
299 bios->data = kmalloc(size, GFP_KERNEL);
300 if (bios->data) {
301 memcpy_fromio(bios->data, rom, size);
302 bios->size = size;
303 }
304 }
305}
306
307#ifdef __NetBSD__
308# undef __iomem
309#endif
310
311static int
312nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
313{
314 if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
315 bios->data[1] != 0xAA) {
316 nv_info(bios, "... signature not found\n");
317 return 0;
318 }
319
320 if (nvbios_checksum(bios->data,
321 min_t(u32, bios->data[2] * 512, bios->size))) {
322 nv_info(bios, "... checksum invalid\n");
323 /* if a ro image is somewhat bad, it's probably all rubbish */
324 return writeable ? 2 : 1;
325 }
326
327 nv_info(bios, "... appears to be valid\n");
328 return 3;
329}
330
331struct methods {
332 const char desc[16];
333 void (*shadow)(struct nouveau_bios *);
334 const bool rw;
335 int score;
336 u32 size;
337 u8 *data;
338};
339
340static int
341nouveau_bios_shadow(struct nouveau_bios *bios)
342{
343 struct methods shadow_methods[] = {
344#if defined(__powerpc__)
345 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
346#endif
347 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
348 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
349 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
350 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
351 { "PLATFORM", nouveau_bios_shadow_platform, true, 0, 0, NULL },
352 {}
353 };
354 struct methods *mthd, *best;
355 const struct firmware *fw;
356 const char *optarg;
357 int optlen, ret;
358 char *source;
359
360 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
361 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
362 if (source) {
363 /* try to match one of the built-in methods */
364 mthd = shadow_methods;
365 do {
366 if (strcasecmp(source, mthd->desc))
367 continue;
368 nv_info(bios, "source: %s\n", mthd->desc);
369
370 mthd->shadow(bios);
371 mthd->score = nouveau_bios_score(bios, mthd->rw);
372 if (mthd->score) {
373 kfree(source);
374 return 0;
375 }
376 } while ((++mthd)->shadow);
377
378 /* attempt to load firmware image */
379 ret = request_firmware(&fw, source,
380 nv_device_base(nv_device(bios)));
381 if (ret == 0) {
382 bios->size = fw->size;
383 bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
384 release_firmware(fw);
385
386 nv_info(bios, "image: %s\n", source);
387 if (nouveau_bios_score(bios, 1)) {
388 kfree(source);
389 return 0;
390 }
391
392 kfree(bios->data);
393 bios->data = NULL;
394 }
395
396 nv_error(bios, "source \'%s\' invalid\n", source);
397 kfree(source);
398 }
399
400 mthd = shadow_methods;
401 do {
402 nv_info(bios, "checking %s for image...\n", mthd->desc);
403 mthd->shadow(bios);
404 mthd->score = nouveau_bios_score(bios, mthd->rw);
405 mthd->size = bios->size;
406 mthd->data = bios->data;
407 bios->data = NULL;
408 } while (mthd->score != 3 && (++mthd)->shadow);
409
410 mthd = shadow_methods;
411 best = mthd;
412 do {
413 if (mthd->score > best->score) {
414 kfree(best->data);
415 best = mthd;
416 }
417 } while ((++mthd)->shadow);
418
419 if (best->score) {
420 nv_info(bios, "using image from %s\n", best->desc);
421 bios->size = best->size;
422 bios->data = best->data;
423 return 0;
424 }
425
426 nv_error(bios, "unable to locate usable image\n");
427 return -EINVAL;
428}
429
430static u8
431nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
432{
433 struct nouveau_bios *bios = (void *)object;
434 return bios->data[addr];
435}
436
437static u16
438nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
439{
440 struct nouveau_bios *bios = (void *)object;
441 return get_unaligned_le16(&bios->data[addr]);
442}
443
444static u32
445nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
446{
447 struct nouveau_bios *bios = (void *)object;
448 return get_unaligned_le32(&bios->data[addr]);
449}
450
451static void
452nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
453{
454 struct nouveau_bios *bios = (void *)object;
455 bios->data[addr] = data;
456}
457
458static void
459nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
460{
461 struct nouveau_bios *bios = (void *)object;
462 put_unaligned_le16(data, &bios->data[addr]);
463}
464
465static void
466nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
467{
468 struct nouveau_bios *bios = (void *)object;
469 put_unaligned_le32(data, &bios->data[addr]);
470}
471
472static int
473nouveau_bios_ctor(struct nouveau_object *parent,
474 struct nouveau_object *engine,
475 struct nouveau_oclass *oclass, void *data, u32 size,
476 struct nouveau_object **pobject)
477{
478 struct nouveau_bios *bios;
479 struct bit_entry bit_i;
480 int ret;
481
482 ret = nouveau_subdev_create(parent, engine, oclass, 0,
483 "VBIOS", "bios", &bios);
484 *pobject = nv_object(bios);
485 if (ret)
486 return ret;
487
488 ret = nouveau_bios_shadow(bios);
489 if (ret)
490 return ret;
491
492 /* detect type of vbios we're dealing with */
493 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
494 "\xff\x7f""NV\0", 5);
495 if (bios->bmp_offset) {
496 nv_info(bios, "BMP version %x.%x\n",
497 bmp_version(bios) >> 8,
498 bmp_version(bios) & 0xff);
499 }
500
501 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
502 "\xff\xb8""BIT", 5);
503 if (bios->bit_offset)
504 nv_info(bios, "BIT signature found\n");
505
506 /* determine the vbios version number */
507 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
508 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
509 bios->version.chip = nv_ro08(bios, bit_i.offset + 2);
510 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
511 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
512 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
513 } else
514 if (bmp_version(bios)) {
515 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
516 bios->version.chip = nv_ro08(bios, bios->bmp_offset + 12);
517 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
518 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
519 }
520
521 nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
522 bios->version.major, bios->version.chip,
523 bios->version.minor, bios->version.micro, bios->version.patch);
524
525 return 0;
526}
527
528static void
529nouveau_bios_dtor(struct nouveau_object *object)
530{
531 struct nouveau_bios *bios = (void *)object;
532 kfree(bios->data);
533 nouveau_subdev_destroy(&bios->base);
534}
535
536static int
537nouveau_bios_init(struct nouveau_object *object)
538{
539 struct nouveau_bios *bios = (void *)object;
540 return nouveau_subdev_init(&bios->base);
541}
542
543static int
544nouveau_bios_fini(struct nouveau_object *object, bool suspend)
545{
546 struct nouveau_bios *bios = (void *)object;
547 return nouveau_subdev_fini(&bios->base, suspend);
548}
549
550struct nouveau_oclass
551nouveau_bios_oclass = {
552 .handle = NV_SUBDEV(VBIOS, 0x00),
553 .ofuncs = &(struct nouveau_ofuncs) {
554 .ctor = nouveau_bios_ctor,
555 .dtor = nouveau_bios_dtor,
556 .init = nouveau_bios_init,
557 .fini = nouveau_bios_fini,
558 .rd08 = nouveau_bios_rd08,
559 .rd16 = nouveau_bios_rd16,
560 .rd32 = nouveau_bios_rd32,
561 .wr08 = nouveau_bios_wr08,
562 .wr16 = nouveau_bios_wr16,
563 .wr32 = nouveau_bios_wr32,
564 },
565};
566