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 nvbios_image image;
96 if (!nvbios_image(bios, idx, &image)) {
97 nv_debug(bios, "image %d invalid\n", idx);
100 nv_debug(bios, "%08x: type %02x, %d bytes\n",
101 image.base, image.type, image.size);
103 if (!shadow_fetch(bios, image.size)) {
104 nv_debug(bios, "%08x: fetch failed\n", image.base);
108 switch (image.type) {
110 if (nvbios_checksum(&bios->data[image.base], image.size)) {
111 nv_debug(bios, "%08x: checksum failed\n", image.base);
125 score += shadow_image(bios, idx + 1, mthd);
130 shadow_score(struct nvkm_bios *bios, struct shadow *mthd)
132 struct nvkm_oclass *oclass = nv_object(bios)->oclass;
134 nv_object(bios)->oclass = &mthd->base;
135 score = shadow_image(bios, 0, mthd);
136 nv_object(bios)->oclass = oclass;
142 shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
144 const struct nvbios_source *func = mthd->func;
146 nv_debug(bios, "trying %s...\n", name ? name : func->name);
148 mthd->data = func->init(bios, name);
149 if (IS_ERR(mthd->data)) {
154 mthd->score = shadow_score(bios, mthd);
156 func->fini(mthd->data);
157 nv_debug(bios, "scored %d\n", mthd->score);
158 mthd->data = bios->data;
159 mthd->size = bios->size;
167 shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
169 const struct firmware *fw = data;
170 if (offset + length <= fw->size) {
171 memcpy(bios->data + offset, fw->data + offset, length);
178 shadow_fw_init(struct nvkm_bios *bios, const char *name)
180 struct device *dev = &nv_device(bios)->pdev->dev;
181 const struct firmware *fw;
182 int ret = request_firmware(&fw, name, dev);
184 return ERR_PTR(-ENOENT);
188 static const struct nvbios_source
191 .init = shadow_fw_init,
192 .fini = (void(*)(void *))release_firmware,
193 .read = shadow_fw_read,
198 nvbios_shadow(struct nvkm_bios *bios)
200 struct shadow mthds[] = {
201 { shadow_class, 0, &nvbios_of },
202 { shadow_class, 0, &nvbios_ramin },
203 { shadow_class, 0, &nvbios_rom },
204 { shadow_class, 0, &nvbios_acpi_fast },
205 { shadow_class, 4, &nvbios_acpi_slow },
206 { shadow_class, 1, &nvbios_pcirom },
207 { shadow_class, 1, &nvbios_platform },
209 }, *mthd = mthds, *best = NULL;
214 /* handle user-specified bios source */
215 optarg = nvkm_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
216 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
218 /* try to match one of the built-in methods */
219 for (mthd = mthds; mthd->func; mthd++) {
220 if (mthd->func->name &&
221 !strcasecmp(source, mthd->func->name)) {
223 if (shadow_method(bios, mthd, NULL))
228 /* otherwise, attempt to load as firmware */
229 if (!best && (best = mthd)) {
230 mthd->func = &shadow_fw;
231 shadow_method(bios, mthd, source);
236 nv_error(bios, "%s invalid\n", source);
242 /* scan all potential bios sources, looking for best image */
243 if (!best || !best->score) {
244 for (mthd = mthds, best = mthd; mthd->func; mthd++) {
245 if (!mthd->skip || best->score < mthd->skip) {
246 if (shadow_method(bios, mthd, NULL)) {
247 if (mthd->score > best->score)
254 /* cleanup the ones we didn't use */
255 for (mthd = mthds; mthd->func; mthd++) {
261 nv_fatal(bios, "unable to locate usable image\n");
265 nv_info(bios, "using image from %s\n", best->func ?
266 best->func->name : source);
267 bios->data = best->data;
268 bios->size = best->size;