WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nvkm / subdev / bios / base.c
blobf3c30b2a788e8534fe72c23729779afd5e39d573
1 /*
2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs
24 #include "priv.h"
26 #include <subdev/bios.h>
27 #include <subdev/bios/bmp.h>
28 #include <subdev/bios/bit.h>
29 #include <subdev/bios/image.h>
31 static bool
32 nvbios_addr(struct nvkm_bios *bios, u32 *addr, u8 size)
34 u32 p = *addr;
36 if (*addr > bios->image0_size && bios->imaged_addr) {
37 *addr -= bios->image0_size;
38 *addr += bios->imaged_addr;
41 if (unlikely(*addr + size >= bios->size)) {
42 nvkm_error(&bios->subdev, "OOB %d %08x %08x\n", size, p, *addr);
43 return false;
46 return true;
50 nvbios_rd08(struct nvkm_bios *bios, u32 addr)
52 if (likely(nvbios_addr(bios, &addr, 1)))
53 return bios->data[addr];
54 return 0x00;
57 u16
58 nvbios_rd16(struct nvkm_bios *bios, u32 addr)
60 if (likely(nvbios_addr(bios, &addr, 2)))
61 return get_unaligned_le16(&bios->data[addr]);
62 return 0x0000;
65 u32
66 nvbios_rd32(struct nvkm_bios *bios, u32 addr)
68 if (likely(nvbios_addr(bios, &addr, 4)))
69 return get_unaligned_le32(&bios->data[addr]);
70 return 0x00000000;
74 nvbios_checksum(const u8 *data, int size)
76 u8 sum = 0;
77 while (size--)
78 sum += *data++;
79 return sum;
82 u16
83 nvbios_findstr(const u8 *data, int size, const char *str, int len)
85 int i, j;
87 for (i = 0; i <= (size - len); i++) {
88 for (j = 0; j < len; j++)
89 if ((char)data[i + j] != str[j])
90 break;
91 if (j == len)
92 return i;
95 return 0;
98 int
99 nvbios_memcmp(struct nvkm_bios *bios, u32 addr, const char *str, u32 len)
101 unsigned char c1, c2;
103 while (len--) {
104 c1 = nvbios_rd08(bios, addr++);
105 c2 = *(str++);
106 if (c1 != c2)
107 return c1 - c2;
109 return 0;
113 nvbios_extend(struct nvkm_bios *bios, u32 length)
115 if (bios->size < length) {
116 u8 *prev = bios->data;
117 if (!(bios->data = kmalloc(length, GFP_KERNEL))) {
118 bios->data = prev;
119 return -ENOMEM;
121 memcpy(bios->data, prev, bios->size);
122 bios->size = length;
123 kfree(prev);
124 return 1;
126 return 0;
129 static void *
130 nvkm_bios_dtor(struct nvkm_subdev *subdev)
132 struct nvkm_bios *bios = nvkm_bios(subdev);
133 kfree(bios->data);
134 return bios;
137 static const struct nvkm_subdev_func
138 nvkm_bios = {
139 .dtor = nvkm_bios_dtor,
143 nvkm_bios_new(struct nvkm_device *device, int index, struct nvkm_bios **pbios)
145 struct nvkm_bios *bios;
146 struct nvbios_image image;
147 struct bit_entry bit_i;
148 int ret, idx = 0;
150 if (!(bios = *pbios = kzalloc(sizeof(*bios), GFP_KERNEL)))
151 return -ENOMEM;
152 nvkm_subdev_ctor(&nvkm_bios, device, index, &bios->subdev);
154 ret = nvbios_shadow(bios);
155 if (ret)
156 return ret;
158 /* Some tables have weird pointers that need adjustment before
159 * they're dereferenced. I'm not entirely sure why...
161 if (nvbios_image(bios, idx++, &image)) {
162 bios->image0_size = image.size;
163 while (nvbios_image(bios, idx++, &image)) {
164 if (image.type == 0xe0) {
165 bios->imaged_addr = image.base;
166 break;
171 /* detect type of vbios we're dealing with */
172 bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
173 "\xff\x7f""NV\0", 5);
174 if (bios->bmp_offset) {
175 nvkm_debug(&bios->subdev, "BMP version %x.%x\n",
176 bmp_version(bios) >> 8,
177 bmp_version(bios) & 0xff);
180 bios->bit_offset = nvbios_findstr(bios->data, bios->size,
181 "\xff\xb8""BIT", 5);
182 if (bios->bit_offset)
183 nvkm_debug(&bios->subdev, "BIT signature found\n");
185 /* determine the vbios version number */
186 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
187 bios->version.major = nvbios_rd08(bios, bit_i.offset + 3);
188 bios->version.chip = nvbios_rd08(bios, bit_i.offset + 2);
189 bios->version.minor = nvbios_rd08(bios, bit_i.offset + 1);
190 bios->version.micro = nvbios_rd08(bios, bit_i.offset + 0);
191 bios->version.patch = nvbios_rd08(bios, bit_i.offset + 4);
192 } else
193 if (bmp_version(bios)) {
194 bios->version.major = nvbios_rd08(bios, bios->bmp_offset + 13);
195 bios->version.chip = nvbios_rd08(bios, bios->bmp_offset + 12);
196 bios->version.minor = nvbios_rd08(bios, bios->bmp_offset + 11);
197 bios->version.micro = nvbios_rd08(bios, bios->bmp_offset + 10);
200 nvkm_info(&bios->subdev, "version %02x.%02x.%02x.%02x.%02x\n",
201 bios->version.major, bios->version.chip,
202 bios->version.minor, bios->version.micro, bios->version.patch);
203 return 0;