2 * Copyright 2014 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 <bskeggs@redhat.com>
26 #include <core/option.h>
27 #include <subdev/bios.h>
28 #include <subdev/bios/image.h>
31 struct nvkm_oclass base;
33 const struct nvbios_source *func;
40 shadow_fetch(struct nvkm_bios *bios, u32 upto)
42 struct shadow *mthd = (void *)nv_object(bios)->oclass;
43 const u32 limit = (upto + 3) & ~3;
44 const u32 start = bios->size;
45 void *data = mthd->data;
46 if (nvbios_extend(bios, limit) > 0) {
47 u32 read = mthd->func->read(data, start, limit - start, bios);
48 bios->size = start + read;
50 return bios->size >= limit;
54 shadow_rd08(struct nvkm_object *object, u64 addr)
56 struct nvkm_bios *bios = (void *)object;
57 if (shadow_fetch(bios, addr + 1))
58 return bios->data[addr];
63 shadow_rd16(struct nvkm_object *object, u64 addr)
65 struct nvkm_bios *bios = (void *)object;
66 if (shadow_fetch(bios, addr + 2))
67 return get_unaligned_le16(&bios->data[addr]);
72 shadow_rd32(struct nvkm_object *object, u64 addr)
74 struct nvkm_bios *bios = (void *)object;
75 if (shadow_fetch(bios, addr + 4))
76 return get_unaligned_le32(&bios->data[addr]);
80 static struct nvkm_oclass
82 .handle = NV_SUBDEV(VBIOS, 0x00),
83 .ofuncs = &(struct nvkm_ofuncs) {
91 shadow_image(struct nvkm_bios *bios, int idx, struct shadow *mthd)
93 struct nvkm_subdev *subdev = &bios->subdev;
94 struct nvbios_image image;
97 if (!nvbios_image(bios, idx, &image)) {
98 nvkm_debug(subdev, "image %d invalid\n", idx);
101 nvkm_debug(subdev, "%08x: type %02x, %d bytes\n",
102 image.base, image.type, image.size);
104 if (!shadow_fetch(bios, image.size)) {
105 nvkm_debug(subdev, "%08x: fetch failed\n", image.base);
109 switch (image.type) {
111 if (nvbios_checksum(&bios->data[image.base], image.size)) {
112 nvkm_debug(subdev, "%08x: checksum failed\n",
127 score += shadow_image(bios, idx + 1, mthd);
132 shadow_score(struct nvkm_bios *bios, struct shadow *mthd)
134 struct nvkm_oclass *oclass = nv_object(bios)->oclass;
136 nv_object(bios)->oclass = &mthd->base;
137 score = shadow_image(bios, 0, mthd);
138 nv_object(bios)->oclass = oclass;
144 shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
146 const struct nvbios_source *func = mthd->func;
147 struct nvkm_subdev *subdev = &bios->subdev;
149 nvkm_debug(subdev, "trying %s...\n", name ? name : func->name);
151 mthd->data = func->init(bios, name);
152 if (IS_ERR(mthd->data)) {
157 mthd->score = shadow_score(bios, mthd);
159 func->fini(mthd->data);
160 nvkm_debug(subdev, "scored %d\n", mthd->score);
161 mthd->data = bios->data;
162 mthd->size = bios->size;
170 shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
172 const struct firmware *fw = data;
173 if (offset + length <= fw->size) {
174 memcpy(bios->data + offset, fw->data + offset, length);
181 shadow_fw_init(struct nvkm_bios *bios, const char *name)
183 struct device *dev = &nv_device(bios)->pdev->dev;
184 const struct firmware *fw;
185 int ret = request_firmware(&fw, name, dev);
187 return ERR_PTR(-ENOENT);
191 static const struct nvbios_source
194 .init = shadow_fw_init,
195 .fini = (void(*)(void *))release_firmware,
196 .read = shadow_fw_read,
201 nvbios_shadow(struct nvkm_bios *bios)
203 struct nvkm_subdev *subdev = &bios->subdev;
204 struct nvkm_device *device = subdev->device;
205 struct shadow mthds[] = {
206 { shadow_class, 0, &nvbios_of },
207 { shadow_class, 0, &nvbios_ramin },
208 { shadow_class, 0, &nvbios_rom },
209 { shadow_class, 0, &nvbios_acpi_fast },
210 { shadow_class, 4, &nvbios_acpi_slow },
211 { shadow_class, 1, &nvbios_pcirom },
212 { shadow_class, 1, &nvbios_platform },
214 }, *mthd, *best = NULL;
219 /* handle user-specified bios source */
220 optarg = nvkm_stropt(device->cfgopt, "NvBios", &optlen);
221 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
223 /* try to match one of the built-in methods */
224 for (mthd = mthds; mthd->func; mthd++) {
225 if (mthd->func->name &&
226 !strcasecmp(source, mthd->func->name)) {
228 if (shadow_method(bios, mthd, NULL))
233 /* otherwise, attempt to load as firmware */
234 if (!best && (best = mthd)) {
235 mthd->func = &shadow_fw;
236 shadow_method(bios, mthd, source);
241 nvkm_error(subdev, "%s invalid\n", source);
247 /* scan all potential bios sources, looking for best image */
248 if (!best || !best->score) {
249 for (mthd = mthds, best = mthd; mthd->func; mthd++) {
250 if (!mthd->skip || best->score < mthd->skip) {
251 if (shadow_method(bios, mthd, NULL)) {
252 if (mthd->score > best->score)
259 /* cleanup the ones we didn't use */
260 for (mthd = mthds; mthd->func; mthd++) {
266 nvkm_error(subdev, "unable to locate usable image\n");
270 nvkm_debug(subdev, "using image from %s\n", best->func ?
271 best->func->name : source);
272 bios->data = best->data;
273 bios->size = best->size;