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 if ((nv_rd32(bios, 0x300000) & 0xffff) == 0xaa55)
178 /* read entire bios image to system memory */
179 bios->size = ((nv_rd32(bios, 0x300000) >> 16) & 0xff) * 512;
183 bios->data = kmalloc(bios->size, GFP_KERNEL);
185 for (i = 0; i < bios->size; i+=4)
186 nv_wo32(bios, i, nv_rd32(bios, 0x300000 + i));
189 /* check the PCI record header */
190 pcir = nv_ro16(bios, 0x0018);
191 if (bios->data[pcir + 0] != 'P' ||
192 bios->data[pcir + 1] != 'C' ||
193 bios->data[pcir + 2] != 'I' ||
194 bios->data[pcir + 3] != 'R') {
200 /* disable access to rom */
201 nv_wr32(bios, pcireg, access);
204 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
205 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
206 bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
209 nouveau_acpi_rom_supported(struct pci_dev *pdev) {
214 nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
220 nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
222 struct pci_dev *pdev = nv_device(bios)->pdev;
225 if (!nouveau_acpi_rom_supported(pdev)) {
231 bios->data = kmalloc(4096, GFP_KERNEL);
233 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
234 bios->size = bios->data[2] * 512;
241 bios->data = kmalloc(bios->size, GFP_KERNEL);
243 /* disobey the acpi spec - much faster on at least w530 ... */
244 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
245 if (ret != bios->size ||
246 nvbios_checksum(bios->data, bios->size)) {
247 /* ... that didn't work, ok, i'll be good now */
248 for (i = 0; i < bios->size; i += cnt) {
249 cnt = min((bios->size - i), (u32)4096);
250 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
259 nouveau_bios_shadow_pci(struct nouveau_bios *bios)
261 struct pci_dev *pdev = nv_device(bios)->pdev;
264 if (!pci_enable_rom(pdev)) {
265 void __iomem *rom = pci_map_rom(pdev, &size);
267 bios->data = kmalloc(size, GFP_KERNEL);
269 memcpy_fromio(bios->data, rom, size);
274 pci_unmap_rom(pdev, rom);
276 pci_disable_rom(pdev);
281 nouveau_bios_shadow_platform(struct nouveau_bios *bios)
283 struct pci_dev *pdev = nv_device(bios)->pdev;
286 void __iomem *rom = pci_platform_rom(pdev, &size);
288 bios->data = kmalloc(size, GFP_KERNEL);
290 memcpy_fromio(bios->data, rom, size);
297 nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
299 if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
300 bios->data[1] != 0xAA) {
301 nv_info(bios, "... signature not found\n");
305 if (nvbios_checksum(bios->data,
306 min_t(u32, bios->data[2] * 512, bios->size))) {
307 nv_info(bios, "... checksum invalid\n");
308 /* if a ro image is somewhat bad, it's probably all rubbish */
309 return writeable ? 2 : 1;
312 nv_info(bios, "... appears to be valid\n");
318 void (*shadow)(struct nouveau_bios *);
326 nouveau_bios_shadow(struct nouveau_bios *bios)
328 struct methods shadow_methods[] = {
329 #if defined(__powerpc__)
330 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
332 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
333 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
334 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
335 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
336 { "PLATFORM", nouveau_bios_shadow_platform, true, 0, 0, NULL },
339 struct methods *mthd, *best;
340 const struct firmware *fw;
345 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
346 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
348 /* try to match one of the built-in methods */
349 mthd = shadow_methods;
351 if (strcasecmp(source, mthd->desc))
353 nv_info(bios, "source: %s\n", mthd->desc);
356 mthd->score = nouveau_bios_score(bios, mthd->rw);
361 } while ((++mthd)->shadow);
363 /* attempt to load firmware image */
364 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
366 bios->size = fw->size;
367 bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
368 release_firmware(fw);
370 nv_info(bios, "image: %s\n", source);
371 if (nouveau_bios_score(bios, 1)) {
380 nv_error(bios, "source \'%s\' invalid\n", source);
384 mthd = shadow_methods;
386 nv_info(bios, "checking %s for image...\n", mthd->desc);
388 mthd->score = nouveau_bios_score(bios, mthd->rw);
389 mthd->size = bios->size;
390 mthd->data = bios->data;
392 } while (mthd->score != 3 && (++mthd)->shadow);
394 mthd = shadow_methods;
397 if (mthd->score > best->score) {
401 } while ((++mthd)->shadow);
404 nv_info(bios, "using image from %s\n", best->desc);
405 bios->size = best->size;
406 bios->data = best->data;
410 nv_error(bios, "unable to locate usable image\n");
415 nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
417 struct nouveau_bios *bios = (void *)object;
418 return bios->data[addr];
422 nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
424 struct nouveau_bios *bios = (void *)object;
425 return get_unaligned_le16(&bios->data[addr]);
429 nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
431 struct nouveau_bios *bios = (void *)object;
432 return get_unaligned_le32(&bios->data[addr]);
436 nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
438 struct nouveau_bios *bios = (void *)object;
439 bios->data[addr] = data;
443 nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
445 struct nouveau_bios *bios = (void *)object;
446 put_unaligned_le16(data, &bios->data[addr]);
450 nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
452 struct nouveau_bios *bios = (void *)object;
453 put_unaligned_le32(data, &bios->data[addr]);
457 nouveau_bios_ctor(struct nouveau_object *parent,
458 struct nouveau_object *engine,
459 struct nouveau_oclass *oclass, void *data, u32 size,
460 struct nouveau_object **pobject)
462 struct nouveau_bios *bios;
463 struct bit_entry bit_i;
466 ret = nouveau_subdev_create(parent, engine, oclass, 0,
467 "VBIOS", "bios", &bios);
468 *pobject = nv_object(bios);
472 ret = nouveau_bios_shadow(bios);
476 /* detect type of vbios we're dealing with */
477 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
478 "\xff\x7f""NV\0", 5);
479 if (bios->bmp_offset) {
480 nv_info(bios, "BMP version %x.%x\n",
481 bmp_version(bios) >> 8,
482 bmp_version(bios) & 0xff);
485 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
487 if (bios->bit_offset)
488 nv_info(bios, "BIT signature found\n");
490 /* determine the vbios version number */
491 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
492 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
493 bios->version.chip = nv_ro08(bios, bit_i.offset + 2);
494 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
495 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
496 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
498 if (bmp_version(bios)) {
499 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
500 bios->version.chip = nv_ro08(bios, bios->bmp_offset + 12);
501 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
502 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
505 nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
506 bios->version.major, bios->version.chip,
507 bios->version.minor, bios->version.micro, bios->version.patch);
513 nouveau_bios_dtor(struct nouveau_object *object)
515 struct nouveau_bios *bios = (void *)object;
517 nouveau_subdev_destroy(&bios->base);
521 nouveau_bios_init(struct nouveau_object *object)
523 struct nouveau_bios *bios = (void *)object;
524 return nouveau_subdev_init(&bios->base);
528 nouveau_bios_fini(struct nouveau_object *object, bool suspend)
530 struct nouveau_bios *bios = (void *)object;
531 return nouveau_subdev_fini(&bios->base, suspend);
534 struct nouveau_oclass
535 nouveau_bios_oclass = {
536 .handle = NV_SUBDEV(VBIOS, 0x00),
537 .ofuncs = &(struct nouveau_ofuncs) {
538 .ctor = nouveau_bios_ctor,
539 .dtor = nouveau_bios_dtor,
540 .init = nouveau_bios_init,
541 .fini = nouveau_bios_fini,
542 .rd08 = nouveau_bios_rd08,
543 .rd16 = nouveau_bios_rd16,
544 .rd32 = nouveau_bios_rd32,
545 .wr08 = nouveau_bios_wr08,
546 .wr16 = nouveau_bios_wr16,
547 .wr32 = nouveau_bios_wr32,