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
nvbios_checksum(const u8 * data,int size)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
nvbios_findstr(const u8 * data,int size,const char * str,int len)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
nouveau_bios_shadow_of(struct nouveau_bios * bios)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
nouveau_bios_shadow_pramin(struct nouveau_bios * bios)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 = (addr & 0xffffff00) << 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
nouveau_bios_shadow_prom(struct nouveau_bios * bios)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 goto out;
188
189 for (i = 0; i < bios->size; i += 4)
190 ((u32 *)bios->data)[i/4] = nv_rd32(bios, 0x300000 + i);
191
192 /* check the PCI record header */
193 pcir = nv_ro16(bios, 0x0018);
194 if (bios->data[pcir + 0] != 'P' ||
195 bios->data[pcir + 1] != 'C' ||
196 bios->data[pcir + 2] != 'I' ||
197 bios->data[pcir + 3] != 'R') {
198 bios->size = 0;
199 kfree(bios->data);
200 }
201
202 out:
203 /* disable access to rom */
204 nv_wr32(bios, pcireg, access);
205 }
206
207 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
208 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
209 bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
210 #else
211 static inline bool
nouveau_acpi_rom_supported(struct pci_dev * pdev)212 nouveau_acpi_rom_supported(struct pci_dev *pdev) {
213 return false;
214 }
215
216 static inline int
nouveau_acpi_get_bios_chunk(uint8_t * bios,int offset,int len)217 nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) {
218 return -EINVAL;
219 }
220 #endif
221
222 static void
nouveau_bios_shadow_acpi(struct nouveau_bios * bios)223 nouveau_bios_shadow_acpi(struct nouveau_bios *bios)
224 {
225 struct pci_dev *pdev = nv_device(bios)->pdev;
226 int ret, cnt, i;
227
228 if (!nouveau_acpi_rom_supported(pdev)) {
229 bios->data = NULL;
230 return;
231 }
232
233 bios->size = 0;
234 bios->data = kmalloc(4096, GFP_KERNEL);
235 if (bios->data) {
236 if (nouveau_acpi_get_bios_chunk(bios->data, 0, 4096) == 4096)
237 bios->size = bios->data[2] * 512;
238 kfree(bios->data);
239 }
240
241 if (!bios->size)
242 return;
243
244 bios->data = kmalloc(bios->size, GFP_KERNEL);
245 if (bios->data) {
246 /* disobey the acpi spec - much faster on at least w530 ... */
247 ret = nouveau_acpi_get_bios_chunk(bios->data, 0, bios->size);
248 if (ret != bios->size ||
249 nvbios_checksum(bios->data, bios->size)) {
250 /* ... that didn't work, ok, i'll be good now */
251 for (i = 0; i < bios->size; i += cnt) {
252 cnt = min((bios->size - i), (u32)4096);
253 ret = nouveau_acpi_get_bios_chunk(bios->data, i, cnt);
254 if (ret != cnt)
255 break;
256 }
257 }
258 }
259 }
260
261 static void
nouveau_bios_shadow_pci(struct nouveau_bios * bios)262 nouveau_bios_shadow_pci(struct nouveau_bios *bios)
263 {
264 struct pci_dev *pdev = nv_device(bios)->pdev;
265 size_t size;
266
267 if (!pci_enable_rom(pdev)) {
268 void __iomem *rom = pci_map_rom(pdev, &size);
269 if (rom && size) {
270 bios->data = kmalloc(size, GFP_KERNEL);
271 if (bios->data) {
272 memcpy_fromio(bios->data, rom, size);
273 bios->size = size;
274 }
275 }
276 if (rom)
277 pci_unmap_rom(pdev, rom);
278
279 pci_disable_rom(pdev);
280 }
281 }
282
283 static void
nouveau_bios_shadow_platform(struct nouveau_bios * bios)284 nouveau_bios_shadow_platform(struct nouveau_bios *bios)
285 {
286 struct pci_dev *pdev = nv_device(bios)->pdev;
287 size_t size;
288
289 void __iomem *rom = pci_platform_rom(pdev, &size);
290 if (rom && size) {
291 bios->data = kmalloc(size, GFP_KERNEL);
292 if (bios->data) {
293 memcpy_fromio(bios->data, rom, size);
294 bios->size = size;
295 }
296 }
297 }
298
299 static int
nouveau_bios_score(struct nouveau_bios * bios,const bool writeable)300 nouveau_bios_score(struct nouveau_bios *bios, const bool writeable)
301 {
302 if (bios->size < 3 || !bios->data || bios->data[0] != 0x55 ||
303 bios->data[1] != 0xAA) {
304 nv_info(bios, "... signature not found\n");
305 return 0;
306 }
307
308 if (nvbios_checksum(bios->data,
309 min_t(u32, bios->data[2] * 512, bios->size))) {
310 nv_info(bios, "... checksum invalid\n");
311 /* if a ro image is somewhat bad, it's probably all rubbish */
312 return writeable ? 2 : 1;
313 }
314
315 nv_info(bios, "... appears to be valid\n");
316 return 3;
317 }
318
319 struct methods {
320 const char desc[16];
321 void (*shadow)(struct nouveau_bios *);
322 const bool rw;
323 int score;
324 u32 size;
325 u8 *data;
326 };
327
328 static int
nouveau_bios_shadow(struct nouveau_bios * bios)329 nouveau_bios_shadow(struct nouveau_bios *bios)
330 {
331 struct methods shadow_methods[] = {
332 #if defined(__powerpc__)
333 { "OpenFirmware", nouveau_bios_shadow_of, true, 0, 0, NULL },
334 #endif
335 { "PRAMIN", nouveau_bios_shadow_pramin, true, 0, 0, NULL },
336 { "PROM", nouveau_bios_shadow_prom, false, 0, 0, NULL },
337 { "ACPI", nouveau_bios_shadow_acpi, true, 0, 0, NULL },
338 { "PCIROM", nouveau_bios_shadow_pci, true, 0, 0, NULL },
339 { "PLATFORM", nouveau_bios_shadow_platform, true, 0, 0, NULL },
340 {}
341 };
342 struct methods *mthd, *best;
343 const struct firmware *fw;
344 const char *optarg;
345 int optlen, ret;
346 char *source;
347
348 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
349 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
350 if (source) {
351 /* try to match one of the built-in methods */
352 mthd = shadow_methods;
353 do {
354 if (strcasecmp(source, mthd->desc))
355 continue;
356 nv_info(bios, "source: %s\n", mthd->desc);
357
358 mthd->shadow(bios);
359 mthd->score = nouveau_bios_score(bios, mthd->rw);
360 if (mthd->score) {
361 kfree(source);
362 return 0;
363 }
364 } while ((++mthd)->shadow);
365
366 /* attempt to load firmware image */
367 ret = request_firmware(&fw, source, &nv_device(bios)->pdev->dev);
368 if (ret == 0) {
369 bios->size = fw->size;
370 bios->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
371 release_firmware(fw);
372
373 nv_info(bios, "image: %s\n", source);
374 if (nouveau_bios_score(bios, 1)) {
375 kfree(source);
376 return 0;
377 }
378
379 kfree(bios->data);
380 bios->data = NULL;
381 }
382
383 nv_error(bios, "source \'%s\' invalid\n", source);
384 kfree(source);
385 }
386
387 mthd = shadow_methods;
388 do {
389 nv_info(bios, "checking %s for image...\n", mthd->desc);
390 mthd->shadow(bios);
391 mthd->score = nouveau_bios_score(bios, mthd->rw);
392 mthd->size = bios->size;
393 mthd->data = bios->data;
394 bios->data = NULL;
395 } while (mthd->score != 3 && (++mthd)->shadow);
396
397 mthd = shadow_methods;
398 best = mthd;
399 do {
400 if (mthd->score > best->score) {
401 kfree(best->data);
402 best = mthd;
403 }
404 } while ((++mthd)->shadow);
405
406 if (best->score) {
407 nv_info(bios, "using image from %s\n", best->desc);
408 bios->size = best->size;
409 bios->data = best->data;
410 return 0;
411 }
412
413 nv_error(bios, "unable to locate usable image\n");
414 return -EINVAL;
415 }
416
417 static u8
nouveau_bios_rd08(struct nouveau_object * object,u64 addr)418 nouveau_bios_rd08(struct nouveau_object *object, u64 addr)
419 {
420 struct nouveau_bios *bios = (void *)object;
421 return bios->data[addr];
422 }
423
424 static u16
nouveau_bios_rd16(struct nouveau_object * object,u64 addr)425 nouveau_bios_rd16(struct nouveau_object *object, u64 addr)
426 {
427 struct nouveau_bios *bios = (void *)object;
428 return get_unaligned_le16(&bios->data[addr]);
429 }
430
431 static u32
nouveau_bios_rd32(struct nouveau_object * object,u64 addr)432 nouveau_bios_rd32(struct nouveau_object *object, u64 addr)
433 {
434 struct nouveau_bios *bios = (void *)object;
435 return get_unaligned_le32(&bios->data[addr]);
436 }
437
438 static void
nouveau_bios_wr08(struct nouveau_object * object,u64 addr,u8 data)439 nouveau_bios_wr08(struct nouveau_object *object, u64 addr, u8 data)
440 {
441 struct nouveau_bios *bios = (void *)object;
442 bios->data[addr] = data;
443 }
444
445 static void
nouveau_bios_wr16(struct nouveau_object * object,u64 addr,u16 data)446 nouveau_bios_wr16(struct nouveau_object *object, u64 addr, u16 data)
447 {
448 struct nouveau_bios *bios = (void *)object;
449 put_unaligned_le16(data, &bios->data[addr]);
450 }
451
452 static void
nouveau_bios_wr32(struct nouveau_object * object,u64 addr,u32 data)453 nouveau_bios_wr32(struct nouveau_object *object, u64 addr, u32 data)
454 {
455 struct nouveau_bios *bios = (void *)object;
456 put_unaligned_le32(data, &bios->data[addr]);
457 }
458
459 static int
nouveau_bios_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * data,u32 size,struct nouveau_object ** pobject)460 nouveau_bios_ctor(struct nouveau_object *parent,
461 struct nouveau_object *engine,
462 struct nouveau_oclass *oclass, void *data, u32 size,
463 struct nouveau_object **pobject)
464 {
465 struct nouveau_bios *bios;
466 struct bit_entry bit_i;
467 int ret;
468
469 ret = nouveau_subdev_create(parent, engine, oclass, 0,
470 "VBIOS", "bios", &bios);
471 *pobject = nv_object(bios);
472 if (ret)
473 return ret;
474
475 ret = nouveau_bios_shadow(bios);
476 if (ret)
477 return ret;
478
479 /* detect type of vbios we're dealing with */
480 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
481 "\xff\x7f""NV\0", 5);
482 if (bios->bmp_offset) {
483 nv_info(bios, "BMP version %x.%x\n",
484 bmp_version(bios) >> 8,
485 bmp_version(bios) & 0xff);
486 }
487
488 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
489 "\xff\xb8""BIT", 5);
490 if (bios->bit_offset)
491 nv_info(bios, "BIT signature found\n");
492
493 /* determine the vbios version number */
494 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
495 bios->version.major = nv_ro08(bios, bit_i.offset + 3);
496 bios->version.chip = nv_ro08(bios, bit_i.offset + 2);
497 bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
498 bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
499 bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
500 } else
501 if (bmp_version(bios)) {
502 bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
503 bios->version.chip = nv_ro08(bios, bios->bmp_offset + 12);
504 bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
505 bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
506 }
507
508 nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
509 bios->version.major, bios->version.chip,
510 bios->version.minor, bios->version.micro, bios->version.patch);
511
512 return 0;
513 }
514
515 static void
nouveau_bios_dtor(struct nouveau_object * object)516 nouveau_bios_dtor(struct nouveau_object *object)
517 {
518 struct nouveau_bios *bios = (void *)object;
519 kfree(bios->data);
520 nouveau_subdev_destroy(&bios->base);
521 }
522
523 static int
nouveau_bios_init(struct nouveau_object * object)524 nouveau_bios_init(struct nouveau_object *object)
525 {
526 struct nouveau_bios *bios = (void *)object;
527 return nouveau_subdev_init(&bios->base);
528 }
529
530 static int
nouveau_bios_fini(struct nouveau_object * object,bool suspend)531 nouveau_bios_fini(struct nouveau_object *object, bool suspend)
532 {
533 struct nouveau_bios *bios = (void *)object;
534 return nouveau_subdev_fini(&bios->base, suspend);
535 }
536
537 struct nouveau_oclass
538 nouveau_bios_oclass = {
539 .handle = NV_SUBDEV(VBIOS, 0x00),
540 .ofuncs = &(struct nouveau_ofuncs) {
541 .ctor = nouveau_bios_ctor,
542 .dtor = nouveau_bios_dtor,
543 .init = nouveau_bios_init,
544 .fini = nouveau_bios_fini,
545 .rd08 = nouveau_bios_rd08,
546 .rd16 = nouveau_bios_rd16,
547 .rd32 = nouveau_bios_rd32,
548 .wr08 = nouveau_bios_wr08,
549 .wr16 = nouveau_bios_wr16,
550 .wr32 = nouveau_bios_wr32,
551 },
552 };
553