blob: aa0fbbec7f08212cd8b3106ecb57ec4ff491403b [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) {
Maarten Lankhorst791dc1432013-06-27 17:35:53 +100093 if ( device->card_type < NV_C0 ||
94 !(nv_rd32(bios, 0x022500) & 0x00000001))
95 addr = (u64)(nv_rd32(bios, 0x619f04) & 0xffffff00) << 8;
96
Ben Skeggs70c0f262012-07-10 10:49:22 +100097 if (!addr) {
98 addr = (u64)nv_rd32(bios, 0x001700) << 16;
99 addr += 0xf0000;
100 }
101
102 bar0 = nv_mask(bios, 0x001700, 0xffffffff, addr >> 16);
103 }
104
105 /* bail if no rom signature */
106 if (nv_rd08(bios, 0x700000) != 0x55 ||
107 nv_rd08(bios, 0x700001) != 0xaa)
108 goto out;
109
110 bios->size = nv_rd08(bios, 0x700002) * 512;
Marcin Slusarz00e48452012-10-22 09:59:20 +1000111 if (!bios->size)
112 goto out;
113
Ben Skeggs70c0f262012-07-10 10:49:22 +1000114 bios->data = kmalloc(bios->size, GFP_KERNEL);
115 if (bios->data) {
116 for (i = 0; i < bios->size; i++)
117 nv_wo08(bios, i, nv_rd08(bios, 0x700000 + i));
118 }
119
120out:
121 if (device->card_type >= NV_50)
122 nv_wr32(bios, 0x001700, bar0);
123}
124
125static void
126nouveau_bios_shadow_prom(struct nouveau_bios *bios)
127{
128 struct nouveau_device *device = nv_device(bios);
129 u32 pcireg, access;
130 u16 pcir;
131 int i;
132
133 /* enable access to rom */
134 if (device->card_type >= NV_50)
135 pcireg = 0x088050;
136 else
137 pcireg = 0x001850;
138 access = nv_mask(bios, pcireg, 0x00000001, 0x00000000);
139
140 /* bail if no rom signature, with a workaround for a PROM reading
141 * issue on some chipsets. the first read after a period of
142 * inactivity returns the wrong result, so retry the first header
143 * byte a few times before giving up as a workaround
144 */
145 i = 16;
146 do {
147 if (nv_rd08(bios, 0x300000) == 0x55)
148 break;
149 } while (i--);
150
151 if (!i || nv_rd08(bios, 0x300001) != 0xaa)
152 goto out;
153
154 /* additional check (see note below) - read PCI record header */
155 pcir = nv_rd08(bios, 0x300018) |
156 nv_rd08(bios, 0x300019) << 8;
157 if (nv_rd08(bios, 0x300000 + pcir) != 'P' ||
158 nv_rd08(bios, 0x300001 + pcir) != 'C' ||
159 nv_rd08(bios, 0x300002 + pcir) != 'I' ||
160 nv_rd08(bios, 0x300003 + pcir) != 'R')
161 goto out;
162
163 /* read entire bios image to system memory */
164 bios->size = nv_rd08(bios, 0x300002) * 512;
Marcin Slusarz00e48452012-10-22 09:59:20 +1000165 if (!bios->size)
166 goto out;
167
Ben Skeggs70c0f262012-07-10 10:49:22 +1000168 bios->data = kmalloc(bios->size, GFP_KERNEL);
169 if (bios->data) {
170 for (i = 0; i < bios->size; i++)
171 nv_wo08(bios, i, nv_rd08(bios, 0x300000 + i));
172 }
173
174out:
175 /* disable access to rom */
176 nv_wr32(bios, pcireg, access);
177}
178
Ben Hutchingsa91ed422013-02-20 02:57:32 +0000179#if defined(CONFIG_ACPI) && defined(CONFIG_X86)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000180int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
181bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
182#else
183static inline bool
184nouveau_acpi_rom_supported(struct pci_dev *pdev) {
185 return false;
186}
187
188static inline int
189nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
190 return -EINVAL;
191}
192#endif
193
194static void
195nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
196{
197 struct pci_dev *pdev = nv_device(bios)->pdev;
Ben Skeggs9a334cd2012-10-09 12:21:36 +1000198 int ret, cnt, i;
Ben Skeggs70c0f262012-07-10 10:49:22 +1000199
Martin Peres90e28892012-10-20 11:03:36 +0200200 if (!nouveau_acpi_rom_supported(pdev)) {
201 bios->data = NULL;
Ben Skeggs70c0f262012-07-10 10:49:22 +1000202 return;
Martin Peres90e28892012-10-20 11:03:36 +0200203 }
Ben Skeggs70c0f262012-07-10 10:49:22 +1000204
Ben Skeggs70c0f262012-07-10 10:49:22 +1000205 bios->size = 0;
Ben Skeggsd1626a92012-10-22 10:08:19 +1000206 bios->data = kmalloc(4096, GFP_KERNEL);
207 if (bios->data) {
208 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
209 bios->size = bios->data[2] * 512;
210 kfree(bios->data);
211 }
212
Marcin Slusarz00e48452012-10-22 09:59:20 +1000213 if (!bios->size)
214 return;
Ben Skeggs70c0f262012-07-10 10:49:22 +1000215
Ben Skeggs9a334cd2012-10-09 12:21:36 +1000216 bios->data = kmalloc(bios->size, GFP_KERNEL);
Ben Skeggsde2b8b82012-10-11 22:45:15 -0400217 if (bios->data) {
218 /* disobey the acpi spec - much faster on at least w530 ... */
219 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
220 if (ret != bios->size ||
221 nvbios_checksum(bios->data, bios->size)) {
222 /* ... that didn't work, ok, i'll be good now */
223 for (i = 0; i < bios->size; i += cnt) {
224 cnt = min((bios->size - i), (u32)4096);
225 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
226 if (ret != cnt)
227 break;
228 }
229 }
Ben Skeggs70c0f262012-07-10 10:49:22 +1000230 }
231}
232
233static void
234nouveau_bios_shadow_pci(struct nouveau_bios *bios)
235{
236 struct pci_dev *pdev = nv_device(bios)->pdev;
237 size_t size;
238
239 if (!pci_enable_rom(pdev)) {
240 void __iomem *rom = pci_map_rom(pdev, &size);
241 if (rom && size) {
242 bios->data = kmalloc(size, GFP_KERNEL);
243 if (bios->data) {
244 memcpy_fromio(bios->data, rom, size);
245 bios->size = size;
246 }
247 }
248 if (rom)
249 pci_unmap_rom(pdev, rom);
250
251 pci_disable_rom(pdev);
252 }
253}
254
Matthew Garrett121cdf02013-03-26 17:25:55 -0400255static void
256nouveau_bios_shadow_platform(struct nouveau_bios *bios)
257{
258 struct pci_dev *pdev = nv_device(bios)->pdev;
259 size_t size;
260
261 void __iomem *rom = pci_platform_rom(pdev, &size);
262 if (rom && size) {
263 bios->data = kmalloc(size, GFP_KERNEL);
264 if (bios->data) {
265 memcpy_fromio(bios->data, rom, size);
266 bios->size = size;
267 }
268 }
269}
270
Ben Skeggs70c0f262012-07-10 10:49:22 +1000271static int
272nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
273{
Marcin Slusarz00e48452012-10-22 09:59:20 +1000274 if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
275 bios->data[1] != 0xAA) {
Ben Skeggs70c0f262012-07-10 10:49:22 +1000276 nv_info(bios, "... signature not found\n");
277 return 0;
278 }
279
Marcin Slusarz00e48452012-10-22 09:59:20 +1000280 if (nvbios_checksum(bios->data,
281 min_t(u32, bios->data[2] * 512, bios->size))) {
Ben Skeggs70c0f262012-07-10 10:49:22 +1000282 nv_info(bios, "... checksum invalid\n");
283 /* if a ro image is somewhat bad, it's probably all rubbish */
284 return writeable ? 2 : 1;
285 }
286
287 nv_info(bios, "... appears to be valid\n");
288 return 3;
289}
290
291struct methods {
Ben Skeggs77145f12012-07-31 16:16:21 +1000292 const char desc[16];
Ben Skeggs70c0f262012-07-10 10:49:22 +1000293 void (*shadow)(struct nouveau_bios *);
294 const bool rw;
295 int score;
296 u32 size;
297 u8 *data;
298};
299
300static int
301nouveau_bios_shadow(struct nouveau_bios *bios)
302{
303 struct methods shadow_methods[] = {
Ben Skeggs77145f12012-07-31 16:16:21 +1000304#if defined(__powerpc__)
305 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
306#endif
Ben Skeggs70c0f262012-07-10 10:49:22 +1000307 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
308 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
309 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
310 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
Matthew Garrett121cdf02013-03-26 17:25:55 -0400311 { "PLATFORM", nouveau_bios_shadow_platform, true, 0, 0, NULL },
Ben Skeggs70c0f262012-07-10 10:49:22 +1000312 {}
313 };
314 struct methods *mthd, *best;
315 const struct firmware *fw;
316 const char *optarg;
317 int optlen, ret;
318 char *source;
319
320 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
321 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
322 if (source) {
323 /* try to match one of the built-in methods */
324 mthd = shadow_methods;
325 do {
326 if (strcasecmp(source, mthd->desc))
327 continue;
328 nv_info(bios, "source: %s\n", mthd->desc);
329
330 mthd->shadow(bios);
331 mthd->score = nouveau_bios_score(bios, mthd->rw);
332 if (mthd->score) {
333 kfree(source);
334 return 0;
335 }
336 } while ((++mthd)->shadow);
337
338 /* attempt to load firmware image */
339 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
340 if (ret == 0) {
341 bios->size = fw->size;
342 bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
343 release_firmware(fw);
344
345 nv_info(bios, "image: %s\n", source);
346 if (nouveau_bios_score(bios, 1)) {
347 kfree(source);
348 return 0;
349 }
350
351 kfree(bios->data);
352 bios->data = NULL;
353 }
354
355 nv_error(bios, "source \'%s\' invalid\n", source);
356 kfree(source);
357 }
358
359 mthd = shadow_methods;
360 do {
361 nv_info(bios, "checking %s for image...\n", mthd->desc);
362 mthd->shadow(bios);
363 mthd->score = nouveau_bios_score(bios, mthd->rw);
364 mthd->size = bios->size;
365 mthd->data = bios->data;
366 bios->data = NULL;
367 } while (mthd->score != 3 && (++mthd)->shadow);
368
369 mthd = shadow_methods;
370 best = mthd;
371 do {
372 if (mthd->score > best->score) {
373 kfree(best->data);
374 best = mthd;
375 }
376 } while ((++mthd)->shadow);
377
378 if (best->score) {
379 nv_info(bios, "using image from %s\n", best->desc);
380 bios->size = best->size;
381 bios->data = best->data;
382 return 0;
383 }
384
385 nv_error(bios, "unable to locate usable image\n");
386 return -EINVAL;
387}
388
389static u8
Ben Skeggs0a322412012-11-17 21:51:30 +1000390nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000391{
392 struct nouveau_bios *bios = (void *)object;
393 return bios->data[addr];
394}
395
396static u16
Ben Skeggs0a322412012-11-17 21:51:30 +1000397nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000398{
399 struct nouveau_bios *bios = (void *)object;
400 return get_unaligned_le16(&bios->data[addr]);
401}
402
403static u32
Ben Skeggs0a322412012-11-17 21:51:30 +1000404nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000405{
406 struct nouveau_bios *bios = (void *)object;
407 return get_unaligned_le32(&bios->data[addr]);
408}
409
410static void
Ben Skeggs0a322412012-11-17 21:51:30 +1000411nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000412{
413 struct nouveau_bios *bios = (void *)object;
414 bios->data[addr] = data;
415}
416
417static void
Ben Skeggs0a322412012-11-17 21:51:30 +1000418nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000419{
420 struct nouveau_bios *bios = (void *)object;
421 put_unaligned_le16(data, &bios->data[addr]);
422}
423
424static void
Ben Skeggs0a322412012-11-17 21:51:30 +1000425nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
Ben Skeggs70c0f262012-07-10 10:49:22 +1000426{
427 struct nouveau_bios *bios = (void *)object;
428 put_unaligned_le32(data, &bios->data[addr]);
429}
430
431static int
432nouveau_bios_ctor(struct nouveau_object *parent,
433 struct nouveau_object *engine,
434 struct nouveau_oclass *oclass, void *data, u32 size,
435 struct nouveau_object **pobject)
436{
437 struct nouveau_bios *bios;
438 struct bit_entry bit_i;
439 int ret;
440
441 ret = nouveau_subdev_create(parent, engine, oclass, 0,
442 "VBIOS", "bios", &bios);
443 *pobject = nv_object(bios);
444 if (ret)
445 return ret;
446
447 ret = nouveau_bios_shadow(bios);
448 if (ret)
449 return ret;
450
451 /* detect type of vbios we're dealing with */
452 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
453 "\xff\x7f""NV\0", 5);
454 if (bios->bmp_offset) {
455 nv_info(bios, "BMP version %x.%x\n",
456 bmp_version(bios) >> 8,
457 bmp_version(bios) & 0xff);
458 }
459
460 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
461 "\xff\xb8""BIT", 5);
462 if (bios->bit_offset)
463 nv_info(bios, "BIT signature found\n");
464
465 /* determine the vbios version number */
466 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
467 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
468 bios->version.chip = nv_ro08(bios, bit_i.offset + 2);
469 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
470 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
Ben Skeggs0dd660d2012-12-11 12:49:18 +1000471 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
Ben Skeggs70c0f262012-07-10 10:49:22 +1000472 } else
473 if (bmp_version(bios)) {
474 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
475 bios->version.chip = nv_ro08(bios, bios->bmp_offset + 12);
476 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
477 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
478 }
479
Ben Skeggs0dd660d2012-12-11 12:49:18 +1000480 nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
Ben Skeggs70c0f262012-07-10 10:49:22 +1000481 bios->version.major, bios->version.chip,
Ben Skeggs0dd660d2012-12-11 12:49:18 +1000482 bios->version.minor, bios->version.micro, bios->version.patch);
Ben Skeggs70c0f262012-07-10 10:49:22 +1000483
484 return 0;
485}
486
487static void
488nouveau_bios_dtor(struct nouveau_object *object)
489{
490 struct nouveau_bios *bios = (void *)object;
491 kfree(bios->data);
492 nouveau_subdev_destroy(&bios->base);
493}
494
495static int
496nouveau_bios_init(struct nouveau_object *object)
497{
498 struct nouveau_bios *bios = (void *)object;
499 return nouveau_subdev_init(&bios->base);
500}
501
502static int
503nouveau_bios_fini(struct nouveau_object *object, bool suspend)
504{
505 struct nouveau_bios *bios = (void *)object;
506 return nouveau_subdev_fini(&bios->base, suspend);
507}
508
509struct nouveau_oclass
510nouveau_bios_oclass = {
511 .handle = NV_SUBDEV(VBIOS, 0x00),
512 .ofuncs = &(struct nouveau_ofuncs) {
513 .ctor = nouveau_bios_ctor,
514 .dtor = nouveau_bios_dtor,
515 .init = nouveau_bios_init,
516 .fini = nouveau_bios_fini,
517 .rd08 = nouveau_bios_rd08,
518 .rd16 = nouveau_bios_rd16,
519 .rd32 = nouveau_bios_rd32,
520 .wr08 = nouveau_bios_wr08,
521 .wr16 = nouveau_bios_wr16,
522 .wr32 = nouveau_bios_wr32,
523 },
524};