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);
91 if (device->card_type >= NV_50) {
92 u64 addr = (u64)(nv_rd32(bios, 0x619f04) & 0xffffff00) << 8;
94 addr = (u64)nv_rd32(bios, 0x001700) << 16;
98 bar0 = nv_mask(bios, 0x001700, 0xffffffff, addr >> 16);
101 /* bail if no rom signature */
102 if (nv_rd08(bios, 0x700000) != 0x55 ||
103 nv_rd08(bios, 0x700001) != 0xaa)
106 bios->size = nv_rd08(bios, 0x700002) * 512;
110 bios->data = kmalloc(bios->size, GFP_KERNEL);
112 for (i = 0; i < bios->size; i++)
113 nv_wo08(bios, i, nv_rd08(bios, 0x700000 + i));
117 if (device->card_type >= NV_50)
118 nv_wr32(bios, 0x001700, bar0);
122 nouveau_bios_shadow_prom(struct nouveau_bios *bios)
124 struct nouveau_device *device = nv_device(bios);
129 /* enable access to rom */
130 if (device->card_type >= NV_50)
134 access = nv_mask(bios, pcireg, 0x00000001, 0x00000000);
136 /* bail if no rom signature, with a workaround for a PROM reading
137 * issue on some chipsets. the first read after a period of
138 * inactivity returns the wrong result, so retry the first header
139 * byte a few times before giving up as a workaround
143 if (nv_rd08(bios, 0x300000) == 0x55)
147 if (!i || nv_rd08(bios, 0x300001) != 0xaa)
150 /* additional check (see note below) - read PCI record header */
151 pcir = nv_rd08(bios, 0x300018) |
152 nv_rd08(bios, 0x300019) << 8;
153 if (nv_rd08(bios, 0x300000 + pcir) != 'P' ||
154 nv_rd08(bios, 0x300001 + pcir) != 'C' ||
155 nv_rd08(bios, 0x300002 + pcir) != 'I' ||
156 nv_rd08(bios, 0x300003 + pcir) != 'R')
159 /* read entire bios image to system memory */
160 bios->size = nv_rd08(bios, 0x300002) * 512;
164 bios->data = kmalloc(bios->size, GFP_KERNEL);
166 for (i = 0; i < bios->size; i++)
167 nv_wo08(bios, i, nv_rd08(bios, 0x300000 + i));
171 /* disable access to rom */
172 nv_wr32(bios, pcireg, access);
175 #if defined(CONFIG_ACPI)
176 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
177 bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
180 nouveau_acpi_rom_supported(struct pci_dev *pdev) {
185 nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
191 nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
193 struct pci_dev *pdev = nv_device(bios)->pdev;
196 if (!nouveau_acpi_rom_supported(pdev)) {
202 bios->data = kmalloc(4096, GFP_KERNEL);
204 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
205 bios->size = bios->data[2] * 512;
212 bios->data = kmalloc(bios->size, GFP_KERNEL);
214 /* disobey the acpi spec - much faster on at least w530 ... */
215 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
216 if (ret != bios->size ||
217 nvbios_checksum(bios->data, bios->size)) {
218 /* ... that didn't work, ok, i'll be good now */
219 for (i = 0; i < bios->size; i += cnt) {
220 cnt = min((bios->size - i), (u32)4096);
221 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
230 nouveau_bios_shadow_pci(struct nouveau_bios *bios)
232 struct pci_dev *pdev = nv_device(bios)->pdev;
235 if (!pci_enable_rom(pdev)) {
236 void __iomem *rom = pci_map_rom(pdev, &size);
238 bios->data = kmalloc(size, GFP_KERNEL);
240 memcpy_fromio(bios->data, rom, size);
245 pci_unmap_rom(pdev, rom);
247 pci_disable_rom(pdev);
252 nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
254 if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
255 bios->data[1] != 0xAA) {
256 nv_info(bios, "... signature not found\n");
260 if (nvbios_checksum(bios->data,
261 min_t(u32, bios->data[2] * 512, bios->size))) {
262 nv_info(bios, "... checksum invalid\n");
263 /* if a ro image is somewhat bad, it's probably all rubbish */
264 return writeable ? 2 : 1;
267 nv_info(bios, "... appears to be valid\n");
273 void (*shadow)(struct nouveau_bios *);
281 nouveau_bios_shadow(struct nouveau_bios *bios)
283 struct methods shadow_methods[] = {
284 #if defined(__powerpc__)
285 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
287 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
288 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
289 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
290 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
293 struct methods *mthd, *best;
294 const struct firmware *fw;
299 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
300 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
302 /* try to match one of the built-in methods */
303 mthd = shadow_methods;
305 if (strcasecmp(source, mthd->desc))
307 nv_info(bios, "source: %s\n", mthd->desc);
310 mthd->score = nouveau_bios_score(bios, mthd->rw);
315 } while ((++mthd)->shadow);
317 /* attempt to load firmware image */
318 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
320 bios->size = fw->size;
321 bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
322 release_firmware(fw);
324 nv_info(bios, "image: %s\n", source);
325 if (nouveau_bios_score(bios, 1)) {
334 nv_error(bios, "source \'%s\' invalid\n", source);
338 mthd = shadow_methods;
340 nv_info(bios, "checking %s for image...\n", mthd->desc);
342 mthd->score = nouveau_bios_score(bios, mthd->rw);
343 mthd->size = bios->size;
344 mthd->data = bios->data;
346 } while (mthd->score != 3 && (++mthd)->shadow);
348 mthd = shadow_methods;
351 if (mthd->score > best->score) {
355 } while ((++mthd)->shadow);
358 nv_info(bios, "using image from %s\n", best->desc);
359 bios->size = best->size;
360 bios->data = best->data;
364 nv_error(bios, "unable to locate usable image\n");
369 nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
371 struct nouveau_bios *bios = (void *)object;
372 return bios->data[addr];
376 nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
378 struct nouveau_bios *bios = (void *)object;
379 return get_unaligned_le16(&bios->data[addr]);
383 nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
385 struct nouveau_bios *bios = (void *)object;
386 return get_unaligned_le32(&bios->data[addr]);
390 nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
392 struct nouveau_bios *bios = (void *)object;
393 bios->data[addr] = data;
397 nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
399 struct nouveau_bios *bios = (void *)object;
400 put_unaligned_le16(data, &bios->data[addr]);
404 nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
406 struct nouveau_bios *bios = (void *)object;
407 put_unaligned_le32(data, &bios->data[addr]);
411 nouveau_bios_ctor(struct nouveau_object *parent,
412 struct nouveau_object *engine,
413 struct nouveau_oclass *oclass, void *data, u32 size,
414 struct nouveau_object **pobject)
416 struct nouveau_bios *bios;
417 struct bit_entry bit_i;
420 ret = nouveau_subdev_create(parent, engine, oclass, 0,
421 "VBIOS", "bios", &bios);
422 *pobject = nv_object(bios);
426 ret = nouveau_bios_shadow(bios);
430 /* detect type of vbios we're dealing with */
431 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
432 "\xff\x7f""NV\0", 5);
433 if (bios->bmp_offset) {
434 nv_info(bios, "BMP version %x.%x\n",
435 bmp_version(bios) >> 8,
436 bmp_version(bios) & 0xff);
439 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
441 if (bios->bit_offset)
442 nv_info(bios, "BIT signature found\n");
444 /* determine the vbios version number */
445 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
446 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
447 bios->version.chip = nv_ro08(bios, bit_i.offset + 2);
448 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
449 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
450 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
452 if (bmp_version(bios)) {
453 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
454 bios->version.chip = nv_ro08(bios, bios->bmp_offset + 12);
455 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
456 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
459 nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
460 bios->version.major, bios->version.chip,
461 bios->version.minor, bios->version.micro, bios->version.patch);
467 nouveau_bios_dtor(struct nouveau_object *object)
469 struct nouveau_bios *bios = (void *)object;
471 nouveau_subdev_destroy(&bios->base);
475 nouveau_bios_init(struct nouveau_object *object)
477 struct nouveau_bios *bios = (void *)object;
478 return nouveau_subdev_init(&bios->base);
482 nouveau_bios_fini(struct nouveau_object *object, bool suspend)
484 struct nouveau_bios *bios = (void *)object;
485 return nouveau_subdev_fini(&bios->base, suspend);
488 struct nouveau_oclass
489 nouveau_bios_oclass = {
490 .handle = NV_SUBDEV(VBIOS, 0x00),
491 .ofuncs = &(struct nouveau_ofuncs) {
492 .ctor = nouveau_bios_ctor,
493 .dtor = nouveau_bios_dtor,
494 .init = nouveau_bios_init,
495 .fini = nouveau_bios_fini,
496 .rd08 = nouveau_bios_rd08,
497 .rd16 = nouveau_bios_rd16,
498 .rd32 = nouveau_bios_rd32,
499 .wr08 = nouveau_bios_wr08,
500 .wr16 = nouveau_bios_wr16,
501 .wr32 = nouveau_bios_wr32,