2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
25 #include <core/object.h>
26 #include <core/device.h>
27 #include <core/subdev.h>
28 #include <core/option.h>
30 #include <subdev/bios.h>
31 #include <subdev/bios/bmp.h>
32 #include <subdev/bios/bit.h>
35 nvbios_checksum(const u8 *data, int size)
44 nvbios_findstr(const u8 *data, int size, const char *str, int len)
48 for (i = 0; i <= (size - len); i++) {
49 for (j = 0; j < len; j++)
50 if ((char)data[i + j] != str[j])
59 #if defined(__powerpc__)
61 nouveau_bios_shadow_of(struct nouveau_bios *bios)
63 struct pci_dev *pdev = nv_device(bios)->pdev;
64 struct device_node *dn;
68 dn = pci_device_to_OF_node(pdev);
70 nv_info(bios, "Unable to get the OF node\n");
74 data = of_get_property(dn, "NVDA,BMP", &size);
77 bios->data = kmalloc(bios->size, GFP_KERNEL);
79 memcpy(bios->data, data, size);
85 nouveau_bios_shadow_pramin(struct nouveau_bios *bios)
87 struct nouveau_device *device = nv_device(bios);
92 if (device->card_type >= NV_50) {
93 if (device->card_type >= NV_C0 && device->card_type < GM100) {
94 if (nv_rd32(bios, 0x022500) & 0x00000001)
97 if (device->card_type >= GM100) {
98 if (nv_rd32(bios, 0x021c04) & 0x00000001)
102 addr = nv_rd32(bios, 0x619f04);
103 if (!(addr & 0x00000008)) {
104 nv_debug(bios, "... not enabled\n");
107 if ( (addr & 0x00000003) != 1) {
108 nv_debug(bios, "... not in vram\n");
112 addr = (u64)(addr >> 8) << 8;
114 addr = (u64)nv_rd32(bios, 0x001700) << 16;
118 bar0 = nv_mask(bios, 0x001700, 0xffffffff, addr >> 16);
121 /* bail if no rom signature */
122 if (nv_rd08(bios, 0x700000) != 0x55 ||
123 nv_rd08(bios, 0x700001) != 0xaa)
126 bios->size = nv_rd08(bios, 0x700002) * 512;
130 bios->data = kmalloc(bios->size, GFP_KERNEL);
132 for (i = 0; i < bios->size; i++)
133 nv_wo08(bios, i, nv_rd08(bios, 0x700000 + i));
137 if (device->card_type >= NV_50)
138 nv_wr32(bios, 0x001700, bar0);
142 nouveau_bios_shadow_prom(struct nouveau_bios *bios)
144 struct nouveau_device *device = nv_device(bios);
149 /* there is no prom on nv4x IGP's */
150 if (device->card_type == NV_40 && device->chipset >= 0x4c)
153 /* enable access to rom */
154 if (device->card_type >= NV_50)
158 access = nv_mask(bios, pcireg, 0x00000001, 0x00000000);
160 /* WARNING: PROM accesses should always be 32-bits aligned. Other
161 * accesses work on most chipset but do not on Kepler chipsets
164 /* bail if no rom signature, with a workaround for a PROM reading
165 * issue on some chipsets. the first read after a period of
166 * inactivity returns the wrong result, so retry the first header
167 * byte a few times before giving up as a workaround
171 u32 data = le32_to_cpu(nv_rd32(bios, 0x300000)) & 0xffff;
179 /* read entire bios image to system memory */
180 bios->size = (le32_to_cpu(nv_rd32(bios, 0x300000)) >> 16) & 0xff;
181 bios->size = bios->size * 512;
185 bios->data = kmalloc(bios->size, GFP_KERNEL);
187 for (i = 0; i < bios->size; i += 4)
188 ((u32 *)bios->data)[i/4] = nv_rd32(bios, 0x300000 + i);
191 /* check the PCI record header */
192 pcir = nv_ro16(bios, 0x0018);
193 if (bios->data[pcir + 0] != 'P' ||
194 bios->data[pcir + 1] != 'C' ||
195 bios->data[pcir + 2] != 'I' ||
196 bios->data[pcir + 3] != 'R') {
202 /* disable access to rom */
203 nv_wr32(bios, pcireg, access);
206 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
207 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
208 bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
211 nouveau_acpi_rom_supported(struct pci_dev *pdev) {
216 nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
222 nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
224 struct pci_dev *pdev = nv_device(bios)->pdev;
227 if (!nouveau_acpi_rom_supported(pdev)) {
233 bios->data = kmalloc(4096, GFP_KERNEL);
235 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
236 bios->size = bios->data[2] * 512;
243 bios->data = kmalloc(bios->size, GFP_KERNEL);
245 /* disobey the acpi spec - much faster on at least w530 ... */
246 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
247 if (ret != bios->size ||
248 nvbios_checksum(bios->data, bios->size)) {
249 /* ... that didn't work, ok, i'll be good now */
250 for (i = 0; i < bios->size; i += cnt) {
251 cnt = min((bios->size - i), (u32)4096);
252 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
261 nouveau_bios_shadow_pci(struct nouveau_bios *bios)
263 struct pci_dev *pdev = nv_device(bios)->pdev;
266 if (!pci_enable_rom(pdev)) {
267 void __iomem *rom = pci_map_rom(pdev, &size);
269 bios->data = kmalloc(size, GFP_KERNEL);
271 memcpy_fromio(bios->data, rom, size);
276 pci_unmap_rom(pdev, rom);
278 pci_disable_rom(pdev);
283 nouveau_bios_shadow_platform(struct nouveau_bios *bios)
285 struct pci_dev *pdev = nv_device(bios)->pdev;
288 void __iomem *rom = pci_platform_rom(pdev, &size);
290 bios->data = kmalloc(size, GFP_KERNEL);
292 memcpy_fromio(bios->data, rom, size);
299 nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
301 if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
302 bios->data[1] != 0xAA) {
303 nv_info(bios, "... signature not found\n");
307 if (nvbios_checksum(bios->data,
308 min_t(u32, bios->data[2] * 512, bios->size))) {
309 nv_info(bios, "... checksum invalid\n");
310 /* if a ro image is somewhat bad, it's probably all rubbish */
311 return writeable ? 2 : 1;
314 nv_info(bios, "... appears to be valid\n");
320 void (*shadow)(struct nouveau_bios *);
328 nouveau_bios_shadow(struct nouveau_bios *bios)
330 struct methods shadow_methods[] = {
331 #if defined(__powerpc__)
332 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
334 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
335 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
336 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
337 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
338 { "PLATFORM", nouveau_bios_shadow_platform, true, 0, 0, NULL },
341 struct methods *mthd, *best;
342 const struct firmware *fw;
347 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
348 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
350 /* try to match one of the built-in methods */
351 mthd = shadow_methods;
353 if (strcasecmp(source, mthd->desc))
355 nv_info(bios, "source: %s\n", mthd->desc);
358 mthd->score = nouveau_bios_score(bios, mthd->rw);
363 } while ((++mthd)->shadow);
365 /* attempt to load firmware image */
366 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
368 bios->size = fw->size;
369 bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
370 release_firmware(fw);
372 nv_info(bios, "image: %s\n", source);
373 if (nouveau_bios_score(bios, 1)) {
382 nv_error(bios, "source \'%s\' invalid\n", source);
386 mthd = shadow_methods;
388 nv_info(bios, "checking %s for image...\n", mthd->desc);
390 mthd->score = nouveau_bios_score(bios, mthd->rw);
391 mthd->size = bios->size;
392 mthd->data = bios->data;
394 } while (mthd->score != 3 && (++mthd)->shadow);
396 mthd = shadow_methods;
399 if (mthd->score > best->score) {
403 } while ((++mthd)->shadow);
406 nv_info(bios, "using image from %s\n", best->desc);
407 bios->size = best->size;
408 bios->data = best->data;
412 nv_error(bios, "unable to locate usable image\n");
417 nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
419 struct nouveau_bios *bios = (void *)object;
420 return bios->data[addr];
424 nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
426 struct nouveau_bios *bios = (void *)object;
427 return get_unaligned_le16(&bios->data[addr]);
431 nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
433 struct nouveau_bios *bios = (void *)object;
434 return get_unaligned_le32(&bios->data[addr]);
438 nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
440 struct nouveau_bios *bios = (void *)object;
441 bios->data[addr] = data;
445 nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
447 struct nouveau_bios *bios = (void *)object;
448 put_unaligned_le16(data, &bios->data[addr]);
452 nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
454 struct nouveau_bios *bios = (void *)object;
455 put_unaligned_le32(data, &bios->data[addr]);
459 nouveau_bios_ctor(struct nouveau_object *parent,
460 struct nouveau_object *engine,
461 struct nouveau_oclass *oclass, void *data, u32 size,
462 struct nouveau_object **pobject)
464 struct nouveau_bios *bios;
465 struct bit_entry bit_i;
468 ret = nouveau_subdev_create(parent, engine, oclass, 0,
469 "VBIOS", "bios", &bios);
470 *pobject = nv_object(bios);
474 ret = nouveau_bios_shadow(bios);
478 /* detect type of vbios we're dealing with */
479 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
480 "\xff\x7f""NV\0", 5);
481 if (bios->bmp_offset) {
482 nv_info(bios, "BMP version %x.%x\n",
483 bmp_version(bios) >> 8,
484 bmp_version(bios) & 0xff);
487 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
489 if (bios->bit_offset)
490 nv_info(bios, "BIT signature found\n");
492 /* determine the vbios version number */
493 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
494 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
495 bios->version.chip = nv_ro08(bios, bit_i.offset + 2);
496 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
497 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
498 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
500 if (bmp_version(bios)) {
501 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
502 bios->version.chip = nv_ro08(bios, bios->bmp_offset + 12);
503 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
504 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
507 nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
508 bios->version.major, bios->version.chip,
509 bios->version.minor, bios->version.micro, bios->version.patch);
515 nouveau_bios_dtor(struct nouveau_object *object)
517 struct nouveau_bios *bios = (void *)object;
519 nouveau_subdev_destroy(&bios->base);
523 nouveau_bios_init(struct nouveau_object *object)
525 struct nouveau_bios *bios = (void *)object;
526 return nouveau_subdev_init(&bios->base);
530 nouveau_bios_fini(struct nouveau_object *object, bool suspend)
532 struct nouveau_bios *bios = (void *)object;
533 return nouveau_subdev_fini(&bios->base, suspend);
536 struct nouveau_oclass
537 nouveau_bios_oclass = {
538 .handle = NV_SUBDEV(VBIOS, 0x00),
539 .ofuncs = &(struct nouveau_ofuncs) {
540 .ctor = nouveau_bios_ctor,
541 .dtor = nouveau_bios_dtor,
542 .init = nouveau_bios_init,
543 .fini = nouveau_bios_fini,
544 .rd08 = nouveau_bios_rd08,
545 .rd16 = nouveau_bios_rd16,
546 .rd32 = nouveau_bios_rd32,
547 .wr08 = nouveau_bios_wr08,
548 .wr16 = nouveau_bios_wr16,
549 .wr32 = nouveau_bios_wr32,