blob: 13bb829f959cec50117f7024dafc3c7c49818994 [file] [log] [blame]
Ben Skeggs70c0f262012-07-10 10:49:22 +10001/*
2 * Copyright 2012 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Ben Skeggs
23 */
24
25#include <core/object.h>
26#include <core/device.h>
27#include <core/subdev.h>
28#include <core/option.h>
29
30#include <subdev/bios.h>
31#include <subdev/bios/bmp.h>
32#include <subdev/bios/bit.h>
33
34u8
35nvbios_checksum(const u8 *data, int size)
36{
37 u8 sum = 0;
38 while (size--)
39 sum += *data++;
40 return sum;
41}
42
43u16
44nvbios_findstr(const u8 *data, int size, const char *str, int len)
45{
46 int i, j;
47
48 for (i = 0; i <= (size - len); i++) {
49 for (j = 0; j < len; j++)
50 if ((char)data[i + j] != str[j])
51 break;
52 if (j == len)
53 return i;
54 }
55
56 return 0;
57}
58
Ben Skeggs77145f12012-07-31 16:16:21 +100059#if defined(__powerpc__)
60static void
61nouveau_bios_shadow_of(struct nouveau_bios *bios)
62{
63 struct pci_dev *pdev = nv_device(bios)->pdev;
64 struct device_node *dn;
65 const u32 *data;
Marcin Slusarzfced4b22012-11-11 19:57:35 +010066 int size;
Ben Skeggs77145f12012-07-31 16:16:21 +100067
68 dn = pci_device_to_OF_node(pdev);
69 if (!dn) {
70 nv_info(bios, "Unable to get the OF node\n");
71 return;
72 }
73
74 data = of_get_property(dn, "NVDA,BMP", &size);
Marcin Slusarz00e48452012-10-22 09:59:20 +100075 if (data && size) {
Ben Skeggs77145f12012-07-31 16:16:21 +100076 bios->size = size;
77 bios->data = kmalloc(bios->size, GFP_KERNEL);
78 if (bios->data)
79 memcpy(bios->data, data, size);
80 }
81}
82#endif
83
Ben Skeggs70c0f262012-07-10 10:49:22 +100084static void
85nouveau_bios_shadow_pramin(struct nouveau_bios *bios)
86{
87 struct nouveau_device *device = nv_device(bios);
Maarten Lankhorst791dc1432013-06-27 17:35:53 +100088 u64 addr = 0;
Ben Skeggs70c0f262012-07-10 10:49:22 +100089 u32 bar0 = 0;
90 int i;
91
92 if (device->card_type >= NV_50) {
Ben Skeggs13a49a12014-02-24 12:30:06 +100093 if (device->card_type >= NV_C0 && device->card_type < GM100) {
94 if (nv_rd32(bios, 0x022500) & 0x00000001)
95 return;
96 } else
97 if (device->card_type >= GM100) {
98 if (nv_rd32(bios, 0x021c04) & 0x00000001)
99 return;
100 }
Maarten Lankhorst791dc1432013-06-27 17:35:53 +1000101
Ben Skeggs13a49a12014-02-24 12:30:06 +1000102 addr = (u64)(nv_rd32(bios, 0x619f04) & 0xffffff00) << 8;
Ben Skeggs70c0f262012-07-10 10:49:22 +1000103 if (!addr) {
104 addr = (u64)nv_rd32(bios, 0x001700) << 16;
105 addr += 0xf0000;
106 }
107
108 bar0 = nv_mask(bios, 0x001700, 0xffffffff, addr >> 16);
109 }
110
111 /* bail if no rom signature */
112 if (nv_rd08(bios, 0x700000) != 0x55 ||
113 nv_rd08(bios, 0x700001) != 0xaa)
114 goto out;
115
116 bios->size = nv_rd08(bios, 0x700002) * 512;
Marcin Slusarz00e48452012-10-22 09:59:20 +1000117 if (!bios->size)
118 goto out;
119
Ben Skeggs70c0f262012-07-10 10:49:22 +1000120 bios->data = kmalloc(bios->size, GFP_KERNEL);
121 if (bios->data) {
122 for (i = 0; i < bios->size; i++)
123 nv_wo08(bios, i, nv_rd08(bios, 0x700000 + i));
124 }
125
126out:
127 if (device->card_type >= NV_50)
128 nv_wr32(bios, 0x001700, bar0);
129}
130
131static void
132nouveau_bios_shadow_prom(struct nouveau_bios *bios)
133{
134 struct nouveau_device *device = nv_device(bios);
135 u32 pcireg, access;
136 u16 pcir;
137 int i;
138
Ilia Mirkin5ac607e2014-02-05 14:33:04 -0500139 /* there is no prom on nv4x IGP's */
140 if (device->card_type == NV_40 && device->chipset >= 0x4c)
141 return;
142
Ben Skeggs70c0f262012-07-10 10:49:22 +1000143 /* enable access to rom */
144 if (device->card_type >= NV_50)
145 pcireg = 0x088050;
146 else
147 pcireg = 0x001850;
148 access = nv_mask(bios, pcireg, 0x00000001, 0x00000000);
149
150 /* bail if no rom signature, with a workaround for a PROM reading
151 * issue on some chipsets. the first read after a period of
152 * inactivity returns the wrong result, so retry the first header
153 * byte a few times before giving up as a workaround
154 */
155 i = 16;
156 do {
157 if (nv_rd08(bios, 0x300000) == 0x55)
158 break;
159 } while (i--);
160
161 if (!i || nv_rd08(bios, 0x300001) != 0xaa)
162 goto out;
163
164 /* additional check (see note below) - read PCI record header */
165 pcir = nv_rd08(bios, 0x300018) |
166 nv_rd08(bios, 0x300019) << 8;
167 if (nv_rd08(bios, 0x300000 + pcir) != 'P' ||
168 nv_rd08(bios, 0x300001 + pcir) != 'C' ||
169 nv_rd08(bios, 0x300002 + pcir) != 'I' ||
170 nv_rd08(bios, 0x300003 + pcir) != 'R')
171 goto out;
172
173 /* read entire bios image to system memory */
174 bios->size = nv_rd08(bios, 0x300002) * 512;
Marcin Slusarz00e48452012-10-22 09:59:20 +1000175 if (!bios->size)
176 goto out;
177
Ben Skeggs70c0f262012-07-10 10:49:22 +1000178 bios->data = kmalloc(bios->size, GFP_KERNEL);
179 if (bios->data) {
180 for (i = 0; i < bios->size; i++)
181 nv_wo08(bios, i, nv_rd08(bios, 0x300000 + i));
182 }
183
184out:
185 /* disable access to rom */
186 nv_wr32(bios, pcireg, access);
187}
188
Ben Hutchingsa91ed422013-02-20 02:57:32 +0000189#if defined(CONFIG_ACPI) && defined(CONFIG_X86)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000190int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
191bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
192#else
193static inline bool
194nouveau_acpi_rom_supported(struct pci_dev *pdev) {
195 return false;
196}
197
198static inline int
199nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
200 return -EINVAL;
201}
202#endif
203
204static void
205nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
206{
207 struct pci_dev *pdev = nv_device(bios)->pdev;
Ben Skeggs9a334cd2012-10-09 12:21:36 +1000208 int ret, cnt, i;
Ben Skeggs70c0f262012-07-10 10:49:22 +1000209
Martin Peres90e28892012-10-20 11:03:36 +0200210 if (!nouveau_acpi_rom_supported(pdev)) {
211 bios->data = NULL;
Ben Skeggs70c0f262012-07-10 10:49:22 +1000212 return;
Martin Peres90e28892012-10-20 11:03:36 +0200213 }
Ben Skeggs70c0f262012-07-10 10:49:22 +1000214
Ben Skeggs70c0f262012-07-10 10:49:22 +1000215 bios->size = 0;
Ben Skeggsd1626a92012-10-22 10:08:19 +1000216 bios->data = kmalloc(4096, GFP_KERNEL);
217 if (bios->data) {
218 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
219 bios->size = bios->data[2] * 512;
220 kfree(bios->data);
221 }
222
Marcin Slusarz00e48452012-10-22 09:59:20 +1000223 if (!bios->size)
224 return;
Ben Skeggs70c0f262012-07-10 10:49:22 +1000225
Ben Skeggs9a334cd2012-10-09 12:21:36 +1000226 bios->data = kmalloc(bios->size, GFP_KERNEL);
Ben Skeggsde2b8b82012-10-11 22:45:15 -0400227 if (bios->data) {
228 /* disobey the acpi spec - much faster on at least w530 ... */
229 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
230 if (ret != bios->size ||
231 nvbios_checksum(bios->data, bios->size)) {
232 /* ... that didn't work, ok, i'll be good now */
233 for (i = 0; i < bios->size; i += cnt) {
234 cnt = min((bios->size - i), (u32)4096);
235 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
236 if (ret != cnt)
237 break;
238 }
239 }
Ben Skeggs70c0f262012-07-10 10:49:22 +1000240 }
241}
242
243static void
244nouveau_bios_shadow_pci(struct nouveau_bios *bios)
245{
246 struct pci_dev *pdev = nv_device(bios)->pdev;
247 size_t size;
248
249 if (!pci_enable_rom(pdev)) {
250 void __iomem *rom = pci_map_rom(pdev, &size);
251 if (rom && size) {
252 bios->data = kmalloc(size, GFP_KERNEL);
253 if (bios->data) {
254 memcpy_fromio(bios->data, rom, size);
255 bios->size = size;
256 }
257 }
258 if (rom)
259 pci_unmap_rom(pdev, rom);
260
261 pci_disable_rom(pdev);
262 }
263}
264
Matthew Garrett121cdf02013-03-26 17:25:55 -0400265static void
266nouveau_bios_shadow_platform(struct nouveau_bios *bios)
267{
268 struct pci_dev *pdev = nv_device(bios)->pdev;
269 size_t size;
270
271 void __iomem *rom = pci_platform_rom(pdev, &size);
272 if (rom && size) {
273 bios->data = kmalloc(size, GFP_KERNEL);
274 if (bios->data) {
275 memcpy_fromio(bios->data, rom, size);
276 bios->size = size;
277 }
278 }
279}
280
Ben Skeggs70c0f262012-07-10 10:49:22 +1000281static int
282nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
283{
Marcin Slusarz00e48452012-10-22 09:59:20 +1000284 if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
285 bios->data[1] != 0xAA) {
Ben Skeggs70c0f262012-07-10 10:49:22 +1000286 nv_info(bios, "... signature not found\n");
287 return 0;
288 }
289
Marcin Slusarz00e48452012-10-22 09:59:20 +1000290 if (nvbios_checksum(bios->data,
291 min_t(u32, bios->data[2] * 512, bios->size))) {
Ben Skeggs70c0f262012-07-10 10:49:22 +1000292 nv_info(bios, "... checksum invalid\n");
293 /* if a ro image is somewhat bad, it's probably all rubbish */
294 return writeable ? 2 : 1;
295 }
296
297 nv_info(bios, "... appears to be valid\n");
298 return 3;
299}
300
301struct methods {
Ben Skeggs77145f12012-07-31 16:16:21 +1000302 const char desc[16];
Ben Skeggs70c0f262012-07-10 10:49:22 +1000303 void (*shadow)(struct nouveau_bios *);
304 const bool rw;
305 int score;
306 u32 size;
307 u8 *data;
308};
309
310static int
311nouveau_bios_shadow(struct nouveau_bios *bios)
312{
313 struct methods shadow_methods[] = {
Ben Skeggs77145f12012-07-31 16:16:21 +1000314#if defined(__powerpc__)
315 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
316#endif
Ben Skeggs70c0f262012-07-10 10:49:22 +1000317 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
318 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
319 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
320 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
Matthew Garrett121cdf02013-03-26 17:25:55 -0400321 { "PLATFORM", nouveau_bios_shadow_platform, true, 0, 0, NULL },
Ben Skeggs70c0f262012-07-10 10:49:22 +1000322 {}
323 };
324 struct methods *mthd, *best;
325 const struct firmware *fw;
326 const char *optarg;
327 int optlen, ret;
328 char *source;
329
330 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
331 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
332 if (source) {
333 /* try to match one of the built-in methods */
334 mthd = shadow_methods;
335 do {
336 if (strcasecmp(source, mthd->desc))
337 continue;
338 nv_info(bios, "source: %s\n", mthd->desc);
339
340 mthd->shadow(bios);
341 mthd->score = nouveau_bios_score(bios, mthd->rw);
342 if (mthd->score) {
343 kfree(source);
344 return 0;
345 }
346 } while ((++mthd)->shadow);
347
348 /* attempt to load firmware image */
349 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
350 if (ret == 0) {
351 bios->size = fw->size;
352 bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
353 release_firmware(fw);
354
355 nv_info(bios, "image: %s\n", source);
356 if (nouveau_bios_score(bios, 1)) {
357 kfree(source);
358 return 0;
359 }
360
361 kfree(bios->data);
362 bios->data = NULL;
363 }
364
365 nv_error(bios, "source \'%s\' invalid\n", source);
366 kfree(source);
367 }
368
369 mthd = shadow_methods;
370 do {
371 nv_info(bios, "checking %s for image...\n", mthd->desc);
372 mthd->shadow(bios);
373 mthd->score = nouveau_bios_score(bios, mthd->rw);
374 mthd->size = bios->size;
375 mthd->data = bios->data;
376 bios->data = NULL;
377 } while (mthd->score != 3 && (++mthd)->shadow);
378
379 mthd = shadow_methods;
380 best = mthd;
381 do {
382 if (mthd->score > best->score) {
383 kfree(best->data);
384 best = mthd;
385 }
386 } while ((++mthd)->shadow);
387
388 if (best->score) {
389 nv_info(bios, "using image from %s\n", best->desc);
390 bios->size = best->size;
391 bios->data = best->data;
392 return 0;
393 }
394
395 nv_error(bios, "unable to locate usable image\n");
396 return -EINVAL;
397}
398
399static u8
Ben Skeggs0a322412012-11-17 21:51:30 +1000400nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000401{
402 struct nouveau_bios *bios = (void *)object;
403 return bios->data[addr];
404}
405
406static u16
Ben Skeggs0a322412012-11-17 21:51:30 +1000407nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000408{
409 struct nouveau_bios *bios = (void *)object;
410 return get_unaligned_le16(&bios->data[addr]);
411}
412
413static u32
Ben Skeggs0a322412012-11-17 21:51:30 +1000414nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000415{
416 struct nouveau_bios *bios = (void *)object;
417 return get_unaligned_le32(&bios->data[addr]);
418}
419
420static void
Ben Skeggs0a322412012-11-17 21:51:30 +1000421nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000422{
423 struct nouveau_bios *bios = (void *)object;
424 bios->data[addr] = data;
425}
426
427static void
Ben Skeggs0a322412012-11-17 21:51:30 +1000428nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000429{
430 struct nouveau_bios *bios = (void *)object;
431 put_unaligned_le16(data, &bios->data[addr]);
432}
433
434static void
Ben Skeggs0a322412012-11-17 21:51:30 +1000435nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000436{
437 struct nouveau_bios *bios = (void *)object;
438 put_unaligned_le32(data, &bios->data[addr]);
439}
440
441static int
442nouveau_bios_ctor(struct nouveau_object *parent,
443 struct nouveau_object *engine,
444 struct nouveau_oclass *oclass, void *data, u32 size,
445 struct nouveau_object **pobject)
446{
447 struct nouveau_bios *bios;
448 struct bit_entry bit_i;
449 int ret;
450
451 ret = nouveau_subdev_create(parent, engine, oclass, 0,
452 "VBIOS", "bios", &bios);
453 *pobject = nv_object(bios);
454 if (ret)
455 return ret;
456
457 ret = nouveau_bios_shadow(bios);
458 if (ret)
459 return ret;
460
461 /* detect type of vbios we're dealing with */
462 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
463 "\xff\x7f""NV\0", 5);
464 if (bios->bmp_offset) {
465 nv_info(bios, "BMP version %x.%x\n",
466 bmp_version(bios) >> 8,
467 bmp_version(bios) & 0xff);
468 }
469
470 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
471 "\xff\xb8""BIT", 5);
472 if (bios->bit_offset)
473 nv_info(bios, "BIT signature found\n");
474
475 /* determine the vbios version number */
476 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
477 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
478 bios->version.chip = nv_ro08(bios, bit_i.offset + 2);
479 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
480 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
Ben Skeggs0dd660d2012-12-11 12:49:18 +1000481 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
Ben Skeggs70c0f262012-07-10 10:49:22 +1000482 } else
483 if (bmp_version(bios)) {
484 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
485 bios->version.chip = nv_ro08(bios, bios->bmp_offset + 12);
486 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
487 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
488 }
489
Ben Skeggs0dd660d2012-12-11 12:49:18 +1000490 nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
Ben Skeggs70c0f262012-07-10 10:49:22 +1000491 bios->version.major, bios->version.chip,
Ben Skeggs0dd660d2012-12-11 12:49:18 +1000492 bios->version.minor, bios->version.micro, bios->version.patch);
Ben Skeggs70c0f262012-07-10 10:49:22 +1000493
494 return 0;
495}
496
497static void
498nouveau_bios_dtor(struct nouveau_object *object)
499{
500 struct nouveau_bios *bios = (void *)object;
501 kfree(bios->data);
502 nouveau_subdev_destroy(&bios->base);
503}
504
505static int
506nouveau_bios_init(struct nouveau_object *object)
507{
508 struct nouveau_bios *bios = (void *)object;
509 return nouveau_subdev_init(&bios->base);
510}
511
512static int
513nouveau_bios_fini(struct nouveau_object *object, bool suspend)
514{
515 struct nouveau_bios *bios = (void *)object;
516 return nouveau_subdev_fini(&bios->base, suspend);
517}
518
519struct nouveau_oclass
520nouveau_bios_oclass = {
521 .handle = NV_SUBDEV(VBIOS, 0x00),
522 .ofuncs = &(struct nouveau_ofuncs) {
523 .ctor = nouveau_bios_ctor,
524 .dtor = nouveau_bios_dtor,
525 .init = nouveau_bios_init,
526 .fini = nouveau_bios_fini,
527 .rd08 = nouveau_bios_rd08,
528 .rd16 = nouveau_bios_rd16,
529 .rd32 = nouveau_bios_rd32,
530 .wr08 = nouveau_bios_wr08,
531 .wr16 = nouveau_bios_wr16,
532 .wr32 = nouveau_bios_wr32,
533 },
534};