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/image.h>
30 struct nouveau_oclass base;
32 const struct nvbios_source *func;
39 shadow_fetch(struct nouveau_bios *bios, u32 upto)
41 struct shadow *mthd = (void *)nv_object(bios)->oclass;
42 const u32 limit = (upto + 3) & ~3;
43 const u32 start = bios->size;
44 void *data = mthd->data;
45 if (nvbios_extend(bios, limit) > 0) {
46 u32 read = mthd->func->read(data, start, limit - start, bios);
47 bios->size = start + read;
49 return bios->size >= limit;
53 shadow_rd08(struct nouveau_object *object, u64 addr)
55 struct nouveau_bios *bios = (void *)object;
56 if (shadow_fetch(bios, addr + 1))
57 return bios->data[addr];
62 shadow_rd16(struct nouveau_object *object, u64 addr)
64 struct nouveau_bios *bios = (void *)object;
65 if (shadow_fetch(bios, addr + 2))
66 return get_unaligned_le16(&bios->data[addr]);
71 shadow_rd32(struct nouveau_object *object, u64 addr)
73 struct nouveau_bios *bios = (void *)object;
74 if (shadow_fetch(bios, addr + 4))
75 return get_unaligned_le32(&bios->data[addr]);
79 static struct nouveau_oclass
81 .handle = NV_SUBDEV(VBIOS, 0x00),
82 .ofuncs = &(struct nouveau_ofuncs) {
90 shadow_image(struct nouveau_bios *bios, int idx, struct shadow *mthd)
92 struct nvbios_image image;
95 if (!nvbios_image(bios, idx, &image)) {
96 nv_debug(bios, "image %d invalid\n", idx);
99 nv_debug(bios, "%08x: type %02x, %d bytes\n",
100 image.base, image.type, image.size);
102 if (!shadow_fetch(bios, image.size)) {
103 nv_debug(bios, "%08x: fetch failed\n", image.base);
107 switch (image.type) {
109 if (nvbios_checksum(&bios->data[image.base], image.size)) {
110 nv_debug(bios, "%08x: checksum failed\n", image.base);
124 score += shadow_image(bios, idx + 1, mthd);
129 shadow_score(struct nouveau_bios *bios, struct shadow *mthd)
131 struct nouveau_oclass *oclass = nv_object(bios)->oclass;
133 nv_object(bios)->oclass = &mthd->base;
134 score = shadow_image(bios, 0, mthd);
135 nv_object(bios)->oclass = oclass;
141 shadow_method(struct nouveau_bios *bios, struct shadow *mthd, const char *name)
143 const struct nvbios_source *func = mthd->func;
145 nv_debug(bios, "trying %s...\n", name ? name : func->name);
147 mthd->data = func->init(bios, name);
148 if (IS_ERR(mthd->data)) {
153 mthd->score = shadow_score(bios, mthd);
155 func->fini(mthd->data);
156 nv_debug(bios, "scored %d\n", mthd->score);
157 mthd->data = bios->data;
158 mthd->size = bios->size;
166 shadow_fw_read(void *data, u32 offset, u32 length, struct nouveau_bios *bios)
168 const struct firmware *fw = data;
169 if (offset + length <= fw->size) {
170 memcpy(bios->data + offset, fw->data + offset, length);
177 shadow_fw_init(struct nouveau_bios *bios, const char *name)
179 struct device *dev = &nv_device(bios)->pdev->dev;
180 const struct firmware *fw;
181 int ret = request_firmware(&fw, name, dev);
183 return ERR_PTR(-ENOENT);
187 static const struct nvbios_source
190 .init = shadow_fw_init,
191 .fini = (void(*)(void *))release_firmware,
192 .read = shadow_fw_read,
197 nvbios_shadow(struct nouveau_bios *bios)
199 struct shadow mthds[] = {
200 { shadow_class, 0, &nvbios_of },
201 { shadow_class, 0, &nvbios_ramin },
202 { shadow_class, 0, &nvbios_rom },
203 { shadow_class, 0, &nvbios_acpi_fast },
204 { shadow_class, 4, &nvbios_acpi_slow },
205 { shadow_class, 1, &nvbios_pcirom },
206 { shadow_class, 1, &nvbios_platform },
208 }, *mthd = mthds, *best = NULL;
213 /* handle user-specified bios source */
214 optarg = nouveau_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
215 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
217 /* try to match one of the built-in methods */
218 for (mthd = mthds; mthd->func; mthd++) {
219 if (mthd->func->name &&
220 !strcasecmp(source, mthd->func->name)) {
222 if (shadow_method(bios, mthd, NULL))
227 /* otherwise, attempt to load as firmware */
228 if (!best && (best = mthd)) {
229 mthd->func = &shadow_fw;
230 shadow_method(bios, mthd, source);
235 nv_error(bios, "%s invalid\n", source);
241 /* scan all potential bios sources, looking for best image */
242 if (!best || !best->score) {
243 for (mthd = mthds, best = mthd; mthd->func; mthd++) {
244 if (!mthd->skip || best->score < mthd->skip) {
245 if (shadow_method(bios, mthd, NULL)) {
246 if (mthd->score > best->score)
253 /* cleanup the ones we didn't use */
254 for (mthd = mthds; mthd->func; mthd++) {
260 nv_fatal(bios, "unable to locate usable image\n");
264 nv_info(bios, "using image from %s\n", best->func ?
265 best->func->name : source);
266 bios->data = best->data;
267 bios->size = best->size;