]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/core/subdev/bios/base.c
drm/nouveau/bios: fix shadowing from PROM on big-endian systems
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / core / subdev / bios / base.c
1 /*
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
34 u8
35 nvbios_checksum(const u8 *data, int size)
36 {
37         u8 sum = 0;
38         while (size--)
39                 sum += *data++;
40         return sum;
41 }
42
43 u16
44 nvbios_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
59 #if defined(__powerpc__)
60 static void
61 nouveau_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;
66         int size;
67
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);
75         if (data && size) {
76                 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
84 static void
85 nouveau_bios_shadow_pramin(struct nouveau_bios *bios)
86 {
87         struct nouveau_device *device = nv_device(bios);
88         u64 addr = 0;
89         u32 bar0 = 0;
90         int i;
91
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)
95                                 return;
96                 } else
97                 if (device->card_type >= GM100) {
98                         if (nv_rd32(bios, 0x021c04) & 0x00000001)
99                                 return;
100                 }
101
102                 addr = nv_rd32(bios, 0x619f04);
103                 if (!(addr & 0x00000008)) {
104                         nv_debug(bios, "... not enabled\n");
105                         return;
106                 }
107                 if ( (addr & 0x00000003) != 1) {
108                         nv_debug(bios, "... not in vram\n");
109                         return;
110                 }
111
112                 addr = (u64)(addr >> 8) << 8;
113                 if (!addr) {
114                         addr  = (u64)nv_rd32(bios, 0x001700) << 16;
115                         addr += 0xf0000;
116                 }
117
118                 bar0 = nv_mask(bios, 0x001700, 0xffffffff, addr >> 16);
119         }
120
121         /* bail if no rom signature */
122         if (nv_rd08(bios, 0x700000) != 0x55 ||
123             nv_rd08(bios, 0x700001) != 0xaa)
124                 goto out;
125
126         bios->size = nv_rd08(bios, 0x700002) * 512;
127         if (!bios->size)
128                 goto out;
129
130         bios->data = kmalloc(bios->size, GFP_KERNEL);
131         if (bios->data) {
132                 for (i = 0; i < bios->size; i++)
133                         nv_wo08(bios, i, nv_rd08(bios, 0x700000 + i));
134         }
135
136 out:
137         if (device->card_type >= NV_50)
138                 nv_wr32(bios, 0x001700, bar0);
139 }
140
141 static void
142 nouveau_bios_shadow_prom(struct nouveau_bios *bios)
143 {
144         struct nouveau_device *device = nv_device(bios);
145         u32 pcireg, access;
146         u16 pcir;
147         int i;
148
149         /* there is no prom on nv4x IGP's */
150         if (device->card_type == NV_40 && device->chipset >= 0x4c)
151                 return;
152
153         /* enable access to rom */
154         if (device->card_type >= NV_50)
155                 pcireg = 0x088050;
156         else
157                 pcireg = 0x001850;
158         access = nv_mask(bios, pcireg, 0x00000001, 0x00000000);
159
160         /* WARNING: PROM accesses should always be 32-bits aligned. Other
161          * accesses work on most chipset but do not on Kepler chipsets
162          */
163
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
168          */
169         i = 16;
170         do {
171                 u32 data = le32_to_cpu(nv_rd32(bios, 0x300000)) & 0xffff;
172                 if (data == 0xaa55)
173                         break;
174         } while (i--);
175
176         if (!i)
177                 goto out;
178
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;
182         if (!bios->size)
183                 goto out;
184
185         bios->data = kmalloc(bios->size, GFP_KERNEL);
186         if (bios->data) {
187                 for (i = 0; i < bios->size; i += 4)
188                         ((u32 *)bios->data)[i/4] = nv_rd32(bios, 0x300000 + i);
189         }
190
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') {
197                 bios->size = 0;
198                 kfree(bios->data);
199         }
200
201 out:
202         /* disable access to rom */
203         nv_wr32(bios, pcireg, access);
204 }
205
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);
209 #else
210 static inline bool
211 nouveau_acpi_rom_supported(struct pci_dev *pdev) {
212         return false;
213 }
214
215 static inline int
216 nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
217         return -EINVAL;
218 }
219 #endif
220
221 static void
222 nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
223 {
224         struct pci_dev *pdev = nv_device(bios)->pdev;
225         int ret, cnt, i;
226
227         if (!nouveau_acpi_rom_supported(pdev)) {
228                 bios->data = NULL;
229                 return;
230         }
231
232         bios->size = 0;
233         bios->data = kmalloc(4096, GFP_KERNEL);
234         if (bios->data) {
235                 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
236                         bios->size = bios->data[2] * 512;
237                 kfree(bios->data);
238         }
239
240         if (!bios->size)
241                 return;
242
243         bios->data = kmalloc(bios->size, GFP_KERNEL);
244         if (bios->data) {
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);
253                                 if (ret != cnt)
254                                         break;
255                         }
256                 }
257         }
258 }
259
260 static void
261 nouveau_bios_shadow_pci(struct nouveau_bios *bios)
262 {
263         struct pci_dev *pdev = nv_device(bios)->pdev;
264         size_t size;
265
266         if (!pci_enable_rom(pdev)) {
267                 void __iomem *rom = pci_map_rom(pdev, &size);
268                 if (rom && size) {
269                         bios->data = kmalloc(size, GFP_KERNEL);
270                         if (bios->data) {
271                                 memcpy_fromio(bios->data, rom, size);
272                                 bios->size = size;
273                         }
274                 }
275                 if (rom)
276                         pci_unmap_rom(pdev, rom);
277
278                 pci_disable_rom(pdev);
279         }
280 }
281
282 static void
283 nouveau_bios_shadow_platform(struct nouveau_bios *bios)
284 {
285         struct pci_dev *pdev = nv_device(bios)->pdev;
286         size_t size;
287
288         void __iomem *rom = pci_platform_rom(pdev, &size);
289         if (rom && size) {
290                 bios->data = kmalloc(size, GFP_KERNEL);
291                 if (bios->data) {
292                         memcpy_fromio(bios->data, rom, size);
293                         bios->size = size;
294                 }
295         }
296 }
297
298 static int
299 nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
300 {
301         if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
302                         bios->data[1] != 0xAA) {
303                 nv_info(bios, "... signature not found\n");
304                 return 0;
305         }
306
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;
312         }
313
314         nv_info(bios, "... appears to be valid\n");
315         return 3;
316 }
317
318 struct methods {
319         const char desc[16];
320         void (*shadow)(struct nouveau_bios *);
321         const bool rw;
322         int score;
323         u32 size;
324         u8 *data;
325 };
326
327 static int
328 nouveau_bios_shadow(struct nouveau_bios *bios)
329 {
330         struct methods shadow_methods[] = {
331 #if defined(__powerpc__)
332                 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
333 #endif
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 },
339                 {}
340         };
341         struct methods *mthd, *best;
342         const struct firmware *fw;
343         const char *optarg;
344         int optlen, ret;
345         char *source;
346
347         optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
348         source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
349         if (source) {
350                 /* try to match one of the built-in methods */
351                 mthd = shadow_methods;
352                 do {
353                         if (strcasecmp(source, mthd->desc))
354                                 continue;
355                         nv_info(bios, "source: %s\n", mthd->desc);
356
357                         mthd->shadow(bios);
358                         mthd->score = nouveau_bios_score(bios, mthd->rw);
359                         if (mthd->score) {
360                                 kfree(source);
361                                 return 0;
362                         }
363                 } while ((++mthd)->shadow);
364
365                 /* attempt to load firmware image */
366                 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
367                 if (ret == 0) {
368                         bios->size = fw->size;
369                         bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
370                         release_firmware(fw);
371
372                         nv_info(bios, "image: %s\n", source);
373                         if (nouveau_bios_score(bios, 1)) {
374                                 kfree(source);
375                                 return 0;
376                         }
377
378                         kfree(bios->data);
379                         bios->data = NULL;
380                 }
381
382                 nv_error(bios, "source \'%s\' invalid\n", source);
383                 kfree(source);
384         }
385
386         mthd = shadow_methods;
387         do {
388                 nv_info(bios, "checking %s for image...\n", mthd->desc);
389                 mthd->shadow(bios);
390                 mthd->score = nouveau_bios_score(bios, mthd->rw);
391                 mthd->size = bios->size;
392                 mthd->data = bios->data;
393                 bios->data = NULL;
394         } while (mthd->score != 3 && (++mthd)->shadow);
395
396         mthd = shadow_methods;
397         best = mthd;
398         do {
399                 if (mthd->score > best->score) {
400                         kfree(best->data);
401                         best = mthd;
402                 }
403         } while ((++mthd)->shadow);
404
405         if (best->score) {
406                 nv_info(bios, "using image from %s\n", best->desc);
407                 bios->size = best->size;
408                 bios->data = best->data;
409                 return 0;
410         }
411
412         nv_error(bios, "unable to locate usable image\n");
413         return -EINVAL;
414 }
415
416 static u8
417 nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
418 {
419         struct nouveau_bios *bios = (void *)object;
420         return bios->data[addr];
421 }
422
423 static u16
424 nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
425 {
426         struct nouveau_bios *bios = (void *)object;
427         return get_unaligned_le16(&bios->data[addr]);
428 }
429
430 static u32
431 nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
432 {
433         struct nouveau_bios *bios = (void *)object;
434         return get_unaligned_le32(&bios->data[addr]);
435 }
436
437 static void
438 nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
439 {
440         struct nouveau_bios *bios = (void *)object;
441         bios->data[addr] = data;
442 }
443
444 static void
445 nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
446 {
447         struct nouveau_bios *bios = (void *)object;
448         put_unaligned_le16(data, &bios->data[addr]);
449 }
450
451 static void
452 nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
453 {
454         struct nouveau_bios *bios = (void *)object;
455         put_unaligned_le32(data, &bios->data[addr]);
456 }
457
458 static int
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)
463 {
464         struct nouveau_bios *bios;
465         struct bit_entry bit_i;
466         int ret;
467
468         ret = nouveau_subdev_create(parent, engine, oclass, 0,
469                                     "VBIOS", "bios", &bios);
470         *pobject = nv_object(bios);
471         if (ret)
472                 return ret;
473
474         ret = nouveau_bios_shadow(bios);
475         if (ret)
476                 return ret;
477
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);
485         }
486
487         bios->bit_offset = nvbios_findstr(bios->data, bios->size,
488                                           "\xff\xb8""BIT", 5);
489         if (bios->bit_offset)
490                 nv_info(bios, "BIT signature found\n");
491
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);
499         } else
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);
505         }
506
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);
510
511         return 0;
512 }
513
514 static void
515 nouveau_bios_dtor(struct nouveau_object *object)
516 {
517         struct nouveau_bios *bios = (void *)object;
518         kfree(bios->data);
519         nouveau_subdev_destroy(&bios->base);
520 }
521
522 static int
523 nouveau_bios_init(struct nouveau_object *object)
524 {
525         struct nouveau_bios *bios = (void *)object;
526         return nouveau_subdev_init(&bios->base);
527 }
528
529 static int
530 nouveau_bios_fini(struct nouveau_object *object, bool suspend)
531 {
532         struct nouveau_bios *bios = (void *)object;
533         return nouveau_subdev_fini(&bios->base, suspend);
534 }
535
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,
550         },
551 };