Merge remote-tracking branch 'spi/topic/core' into spi-next
[cascardo/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 ||
94                     !(nv_rd32(bios, 0x022500) & 0x00000001))
95                         addr = (u64)(nv_rd32(bios, 0x619f04) & 0xffffff00) << 8;
96
97                 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;
111         if (!bios->size)
112                 goto out;
113
114         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
120 out:
121         if (device->card_type >= NV_50)
122                 nv_wr32(bios, 0x001700, bar0);
123 }
124
125 static void
126 nouveau_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         /* there is no prom on nv4x IGP's */
134         if (device->card_type == NV_40 && device->chipset >= 0x4c)
135                 return;
136
137         /* enable access to rom */
138         if (device->card_type >= NV_50)
139                 pcireg = 0x088050;
140         else
141                 pcireg = 0x001850;
142         access = nv_mask(bios, pcireg, 0x00000001, 0x00000000);
143
144         /* bail if no rom signature, with a workaround for a PROM reading
145          * issue on some chipsets.  the first read after a period of
146          * inactivity returns the wrong result, so retry the first header
147          * byte a few times before giving up as a workaround
148          */
149         i = 16;
150         do {
151                 if (nv_rd08(bios, 0x300000) == 0x55)
152                         break;
153         } while (i--);
154
155         if (!i || nv_rd08(bios, 0x300001) != 0xaa)
156                 goto out;
157
158         /* additional check (see note below) - read PCI record header */
159         pcir = nv_rd08(bios, 0x300018) |
160                nv_rd08(bios, 0x300019) << 8;
161         if (nv_rd08(bios, 0x300000 + pcir) != 'P' ||
162             nv_rd08(bios, 0x300001 + pcir) != 'C' ||
163             nv_rd08(bios, 0x300002 + pcir) != 'I' ||
164             nv_rd08(bios, 0x300003 + pcir) != 'R')
165                 goto out;
166
167         /* read entire bios image to system memory */
168         bios->size = nv_rd08(bios, 0x300002) * 512;
169         if (!bios->size)
170                 goto out;
171
172         bios->data = kmalloc(bios->size, GFP_KERNEL);
173         if (bios->data) {
174                 for (i = 0; i < bios->size; i++)
175                         nv_wo08(bios, i, nv_rd08(bios, 0x300000 + i));
176         }
177
178 out:
179         /* disable access to rom */
180         nv_wr32(bios, pcireg, access);
181 }
182
183 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
184 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
185 bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
186 #else
187 static inline bool
188 nouveau_acpi_rom_supported(struct pci_dev *pdev) {
189         return false;
190 }
191
192 static inline int
193 nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
194         return -EINVAL;
195 }
196 #endif
197
198 static void
199 nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
200 {
201         struct pci_dev *pdev = nv_device(bios)->pdev;
202         int ret, cnt, i;
203
204         if (!nouveau_acpi_rom_supported(pdev)) {
205                 bios->data = NULL;
206                 return;
207         }
208
209         bios->size = 0;
210         bios->data = kmalloc(4096, GFP_KERNEL);
211         if (bios->data) {
212                 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
213                         bios->size = bios->data[2] * 512;
214                 kfree(bios->data);
215         }
216
217         if (!bios->size)
218                 return;
219
220         bios->data = kmalloc(bios->size, GFP_KERNEL);
221         if (bios->data) {
222                 /* disobey the acpi spec - much faster on at least w530 ... */
223                 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
224                 if (ret != bios->size ||
225                     nvbios_checksum(bios->data, bios->size)) {
226                         /* ... that didn't work, ok, i'll be good now */
227                         for (i = 0; i < bios->size; i += cnt) {
228                                 cnt = min((bios->size - i), (u32)4096);
229                                 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
230                                 if (ret != cnt)
231                                         break;
232                         }
233                 }
234         }
235 }
236
237 static void
238 nouveau_bios_shadow_pci(struct nouveau_bios *bios)
239 {
240         struct pci_dev *pdev = nv_device(bios)->pdev;
241         size_t size;
242
243         if (!pci_enable_rom(pdev)) {
244                 void __iomem *rom = pci_map_rom(pdev, &size);
245                 if (rom && size) {
246                         bios->data = kmalloc(size, GFP_KERNEL);
247                         if (bios->data) {
248                                 memcpy_fromio(bios->data, rom, size);
249                                 bios->size = size;
250                         }
251                 }
252                 if (rom)
253                         pci_unmap_rom(pdev, rom);
254
255                 pci_disable_rom(pdev);
256         }
257 }
258
259 static void
260 nouveau_bios_shadow_platform(struct nouveau_bios *bios)
261 {
262         struct pci_dev *pdev = nv_device(bios)->pdev;
263         size_t size;
264
265         void __iomem *rom = pci_platform_rom(pdev, &size);
266         if (rom && size) {
267                 bios->data = kmalloc(size, GFP_KERNEL);
268                 if (bios->data) {
269                         memcpy_fromio(bios->data, rom, size);
270                         bios->size = size;
271                 }
272         }
273 }
274
275 static int
276 nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
277 {
278         if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
279                         bios->data[1] != 0xAA) {
280                 nv_info(bios, "... signature not found\n");
281                 return 0;
282         }
283
284         if (nvbios_checksum(bios->data,
285                         min_t(u32, bios->data[2] * 512, bios->size))) {
286                 nv_info(bios, "... checksum invalid\n");
287                 /* if a ro image is somewhat bad, it's probably all rubbish */
288                 return writeable ? 2 : 1;
289         }
290
291         nv_info(bios, "... appears to be valid\n");
292         return 3;
293 }
294
295 struct methods {
296         const char desc[16];
297         void (*shadow)(struct nouveau_bios *);
298         const bool rw;
299         int score;
300         u32 size;
301         u8 *data;
302 };
303
304 static int
305 nouveau_bios_shadow(struct nouveau_bios *bios)
306 {
307         struct methods shadow_methods[] = {
308 #if defined(__powerpc__)
309                 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
310 #endif
311                 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
312                 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
313                 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
314                 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
315                 { "PLATFORM", nouveau_bios_shadow_platform, true, 0, 0, NULL },
316                 {}
317         };
318         struct methods *mthd, *best;
319         const struct firmware *fw;
320         const char *optarg;
321         int optlen, ret;
322         char *source;
323
324         optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
325         source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
326         if (source) {
327                 /* try to match one of the built-in methods */
328                 mthd = shadow_methods;
329                 do {
330                         if (strcasecmp(source, mthd->desc))
331                                 continue;
332                         nv_info(bios, "source: %s\n", mthd->desc);
333
334                         mthd->shadow(bios);
335                         mthd->score = nouveau_bios_score(bios, mthd->rw);
336                         if (mthd->score) {
337                                 kfree(source);
338                                 return 0;
339                         }
340                 } while ((++mthd)->shadow);
341
342                 /* attempt to load firmware image */
343                 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
344                 if (ret == 0) {
345                         bios->size = fw->size;
346                         bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
347                         release_firmware(fw);
348
349                         nv_info(bios, "image: %s\n", source);
350                         if (nouveau_bios_score(bios, 1)) {
351                                 kfree(source);
352                                 return 0;
353                         }
354
355                         kfree(bios->data);
356                         bios->data = NULL;
357                 }
358
359                 nv_error(bios, "source \'%s\' invalid\n", source);
360                 kfree(source);
361         }
362
363         mthd = shadow_methods;
364         do {
365                 nv_info(bios, "checking %s for image...\n", mthd->desc);
366                 mthd->shadow(bios);
367                 mthd->score = nouveau_bios_score(bios, mthd->rw);
368                 mthd->size = bios->size;
369                 mthd->data = bios->data;
370                 bios->data = NULL;
371         } while (mthd->score != 3 && (++mthd)->shadow);
372
373         mthd = shadow_methods;
374         best = mthd;
375         do {
376                 if (mthd->score > best->score) {
377                         kfree(best->data);
378                         best = mthd;
379                 }
380         } while ((++mthd)->shadow);
381
382         if (best->score) {
383                 nv_info(bios, "using image from %s\n", best->desc);
384                 bios->size = best->size;
385                 bios->data = best->data;
386                 return 0;
387         }
388
389         nv_error(bios, "unable to locate usable image\n");
390         return -EINVAL;
391 }
392
393 static u8
394 nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
395 {
396         struct nouveau_bios *bios = (void *)object;
397         return bios->data[addr];
398 }
399
400 static u16
401 nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
402 {
403         struct nouveau_bios *bios = (void *)object;
404         return get_unaligned_le16(&bios->data[addr]);
405 }
406
407 static u32
408 nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
409 {
410         struct nouveau_bios *bios = (void *)object;
411         return get_unaligned_le32(&bios->data[addr]);
412 }
413
414 static void
415 nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
416 {
417         struct nouveau_bios *bios = (void *)object;
418         bios->data[addr] = data;
419 }
420
421 static void
422 nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
423 {
424         struct nouveau_bios *bios = (void *)object;
425         put_unaligned_le16(data, &bios->data[addr]);
426 }
427
428 static void
429 nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
430 {
431         struct nouveau_bios *bios = (void *)object;
432         put_unaligned_le32(data, &bios->data[addr]);
433 }
434
435 static int
436 nouveau_bios_ctor(struct nouveau_object *parent,
437                   struct nouveau_object *engine,
438                   struct nouveau_oclass *oclass, void *data, u32 size,
439                   struct nouveau_object **pobject)
440 {
441         struct nouveau_bios *bios;
442         struct bit_entry bit_i;
443         int ret;
444
445         ret = nouveau_subdev_create(parent, engine, oclass, 0,
446                                     "VBIOS", "bios", &bios);
447         *pobject = nv_object(bios);
448         if (ret)
449                 return ret;
450
451         ret = nouveau_bios_shadow(bios);
452         if (ret)
453                 return ret;
454
455         /* detect type of vbios we're dealing with */
456         bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
457                                           "\xff\x7f""NV\0", 5);
458         if (bios->bmp_offset) {
459                 nv_info(bios, "BMP version %x.%x\n",
460                         bmp_version(bios) >> 8,
461                         bmp_version(bios) & 0xff);
462         }
463
464         bios->bit_offset = nvbios_findstr(bios->data, bios->size,
465                                           "\xff\xb8""BIT", 5);
466         if (bios->bit_offset)
467                 nv_info(bios, "BIT signature found\n");
468
469         /* determine the vbios version number */
470         if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
471                 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
472                 bios->version.chip  = nv_ro08(bios, bit_i.offset + 2);
473                 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
474                 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
475                 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
476         } else
477         if (bmp_version(bios)) {
478                 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
479                 bios->version.chip  = nv_ro08(bios, bios->bmp_offset + 12);
480                 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
481                 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
482         }
483
484         nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
485                 bios->version.major, bios->version.chip,
486                 bios->version.minor, bios->version.micro, bios->version.patch);
487
488         return 0;
489 }
490
491 static void
492 nouveau_bios_dtor(struct nouveau_object *object)
493 {
494         struct nouveau_bios *bios = (void *)object;
495         kfree(bios->data);
496         nouveau_subdev_destroy(&bios->base);
497 }
498
499 static int
500 nouveau_bios_init(struct nouveau_object *object)
501 {
502         struct nouveau_bios *bios = (void *)object;
503         return nouveau_subdev_init(&bios->base);
504 }
505
506 static int
507 nouveau_bios_fini(struct nouveau_object *object, bool suspend)
508 {
509         struct nouveau_bios *bios = (void *)object;
510         return nouveau_subdev_fini(&bios->base, suspend);
511 }
512
513 struct nouveau_oclass
514 nouveau_bios_oclass = {
515         .handle = NV_SUBDEV(VBIOS, 0x00),
516         .ofuncs = &(struct nouveau_ofuncs) {
517                 .ctor = nouveau_bios_ctor,
518                 .dtor = nouveau_bios_dtor,
519                 .init = nouveau_bios_init,
520                 .fini = nouveau_bios_fini,
521                 .rd08 = nouveau_bios_rd08,
522                 .rd16 = nouveau_bios_rd16,
523                 .rd32 = nouveau_bios_rd32,
524                 .wr08 = nouveau_bios_wr08,
525                 .wr16 = nouveau_bios_wr16,
526                 .wr32 = nouveau_bios_wr32,
527         },
528 };