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 #include "priv.h"
25 
26 #include <subdev/bios.h>
27 #include <subdev/bios/bmp.h>
28 #include <subdev/bios/bit.h>
29 
30 u8
31 nvbios_checksum(const u8 *data, int size)
32 {
33 	u8 sum = 0;
34 	while (size--)
35 		sum += *data++;
36 	return sum;
37 }
38 
39 u16
40 nvbios_findstr(const u8 *data, int size, const char *str, int len)
41 {
42 	int i, j;
43 
44 	for (i = 0; i <= (size - len); i++) {
45 		for (j = 0; j < len; j++)
46 			if ((char)data[i + j] != str[j])
47 				break;
48 		if (j == len)
49 			return i;
50 	}
51 
52 	return 0;
53 }
54 
55 int
56 nvbios_extend(struct nvkm_bios *bios, u32 length)
57 {
58 	if (bios->size < length) {
59 		u8 *prev = bios->data;
60 		if (!(bios->data = kmalloc(length, GFP_KERNEL))) {
61 			bios->data = prev;
62 			return -ENOMEM;
63 		}
64 		memcpy(bios->data, prev, bios->size);
65 		bios->size = length;
66 		kfree(prev);
67 		return 1;
68 	}
69 	return 0;
70 }
71 
72 static u8
73 nvkm_bios_rd08(struct nvkm_object *object, u64 addr)
74 {
75 	struct nvkm_bios *bios = (void *)object;
76 	return bios->data[addr];
77 }
78 
79 static u16
80 nvkm_bios_rd16(struct nvkm_object *object, u64 addr)
81 {
82 	struct nvkm_bios *bios = (void *)object;
83 	return get_unaligned_le16(&bios->data[addr]);
84 }
85 
86 static u32
87 nvkm_bios_rd32(struct nvkm_object *object, u64 addr)
88 {
89 	struct nvkm_bios *bios = (void *)object;
90 	return get_unaligned_le32(&bios->data[addr]);
91 }
92 
93 static void
94 nvkm_bios_wr08(struct nvkm_object *object, u64 addr, u8 data)
95 {
96 	struct nvkm_bios *bios = (void *)object;
97 	bios->data[addr] = data;
98 }
99 
100 static void
101 nvkm_bios_wr16(struct nvkm_object *object, u64 addr, u16 data)
102 {
103 	struct nvkm_bios *bios = (void *)object;
104 	put_unaligned_le16(data, &bios->data[addr]);
105 }
106 
107 static void
108 nvkm_bios_wr32(struct nvkm_object *object, u64 addr, u32 data)
109 {
110 	struct nvkm_bios *bios = (void *)object;
111 	put_unaligned_le32(data, &bios->data[addr]);
112 }
113 
114 static int
115 nvkm_bios_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
116 	       struct nvkm_oclass *oclass, void *data, u32 size,
117 	       struct nvkm_object **pobject)
118 {
119 	struct nvkm_bios *bios;
120 	struct bit_entry bit_i;
121 	int ret;
122 
123 	ret = nvkm_subdev_create(parent, engine, oclass, 0,
124 				 "VBIOS", "bios", &bios);
125 	*pobject = nv_object(bios);
126 	if (ret)
127 		return ret;
128 
129 	ret = nvbios_shadow(bios);
130 	if (ret)
131 		return ret;
132 
133 	/* detect type of vbios we're dealing with */
134 	bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
135 					  "\xff\x7f""NV\0", 5);
136 	if (bios->bmp_offset) {
137 		nv_info(bios, "BMP version %x.%x\n",
138 			bmp_version(bios) >> 8,
139 			bmp_version(bios) & 0xff);
140 	}
141 
142 	bios->bit_offset = nvbios_findstr(bios->data, bios->size,
143 					  "\xff\xb8""BIT", 5);
144 	if (bios->bit_offset)
145 		nv_info(bios, "BIT signature found\n");
146 
147 	/* determine the vbios version number */
148 	if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
149 		bios->version.major = nv_ro08(bios, bit_i.offset + 3);
150 		bios->version.chip  = nv_ro08(bios, bit_i.offset + 2);
151 		bios->version.minor = nv_ro08(bios, bit_i.offset + 1);
152 		bios->version.micro = nv_ro08(bios, bit_i.offset + 0);
153 		bios->version.patch = nv_ro08(bios, bit_i.offset + 4);
154 	} else
155 	if (bmp_version(bios)) {
156 		bios->version.major = nv_ro08(bios, bios->bmp_offset + 13);
157 		bios->version.chip  = nv_ro08(bios, bios->bmp_offset + 12);
158 		bios->version.minor = nv_ro08(bios, bios->bmp_offset + 11);
159 		bios->version.micro = nv_ro08(bios, bios->bmp_offset + 10);
160 	}
161 
162 	nv_info(bios, "version %02x.%02x.%02x.%02x.%02x\n",
163 		bios->version.major, bios->version.chip,
164 		bios->version.minor, bios->version.micro, bios->version.patch);
165 
166 	return 0;
167 }
168 
169 static void
170 nvkm_bios_dtor(struct nvkm_object *object)
171 {
172 	struct nvkm_bios *bios = (void *)object;
173 	kfree(bios->data);
174 	nvkm_subdev_destroy(&bios->base);
175 }
176 
177 static int
178 nvkm_bios_init(struct nvkm_object *object)
179 {
180 	struct nvkm_bios *bios = (void *)object;
181 	return nvkm_subdev_init(&bios->base);
182 }
183 
184 static int
185 nvkm_bios_fini(struct nvkm_object *object, bool suspend)
186 {
187 	struct nvkm_bios *bios = (void *)object;
188 	return nvkm_subdev_fini(&bios->base, suspend);
189 }
190 
191 struct nvkm_oclass
192 nvkm_bios_oclass = {
193 	.handle = NV_SUBDEV(VBIOS, 0x00),
194 	.ofuncs = &(struct nvkm_ofuncs) {
195 		.ctor = nvkm_bios_ctor,
196 		.dtor = nvkm_bios_dtor,
197 		.init = nvkm_bios_init,
198 		.fini = nvkm_bios_fini,
199 		.rd08 = nvkm_bios_rd08,
200 		.rd16 = nvkm_bios_rd16,
201 		.rd32 = nvkm_bios_rd32,
202 		.wr08 = nvkm_bios_wr08,
203 		.wr16 = nvkm_bios_wr16,
204 		.wr32 = nvkm_bios_wr32,
205 	},
206 };
207