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