1c39f472eSBen Skeggs /*
2c39f472eSBen Skeggs * Copyright 2014 Red Hat Inc.
3c39f472eSBen Skeggs *
4c39f472eSBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a
5c39f472eSBen Skeggs * copy of this software and associated documentation files (the "Software"),
6c39f472eSBen Skeggs * to deal in the Software without restriction, including without limitation
7c39f472eSBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c39f472eSBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the
9c39f472eSBen Skeggs * Software is furnished to do so, subject to the following conditions:
10c39f472eSBen Skeggs *
11c39f472eSBen Skeggs * The above copyright notice and this permission notice shall be included in
12c39f472eSBen Skeggs * all copies or substantial portions of the Software.
13c39f472eSBen Skeggs *
14c39f472eSBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c39f472eSBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c39f472eSBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17c39f472eSBen Skeggs * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18c39f472eSBen Skeggs * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19c39f472eSBen Skeggs * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20c39f472eSBen Skeggs * OTHER DEALINGS IN THE SOFTWARE.
21c39f472eSBen Skeggs *
22c39f472eSBen Skeggs * Authors: Ben Skeggs <bskeggs@redhat.com>
23c39f472eSBen Skeggs */
24c39f472eSBen Skeggs #include "priv.h"
25d390b480SBen Skeggs
26c39f472eSBen Skeggs #include <core/option.h>
27d390b480SBen Skeggs #include <subdev/bios.h>
28c39f472eSBen Skeggs #include <subdev/bios/image.h>
29c39f472eSBen Skeggs
30c39f472eSBen Skeggs struct shadow {
31c39f472eSBen Skeggs u32 skip;
32c39f472eSBen Skeggs const struct nvbios_source *func;
33c39f472eSBen Skeggs void *data;
34c39f472eSBen Skeggs u32 size;
35c39f472eSBen Skeggs int score;
36c39f472eSBen Skeggs };
37c39f472eSBen Skeggs
38c39f472eSBen Skeggs static bool
shadow_fetch(struct nvkm_bios * bios,struct shadow * mthd,u32 upto)397f5f518fSBen Skeggs shadow_fetch(struct nvkm_bios *bios, struct shadow *mthd, u32 upto)
40c39f472eSBen Skeggs {
41c39f472eSBen Skeggs const u32 limit = (upto + 3) & ~3;
42c39f472eSBen Skeggs const u32 start = bios->size;
43c39f472eSBen Skeggs void *data = mthd->data;
44c39f472eSBen Skeggs if (nvbios_extend(bios, limit) > 0) {
45c39f472eSBen Skeggs u32 read = mthd->func->read(data, start, limit - start, bios);
46c39f472eSBen Skeggs bios->size = start + read;
47c39f472eSBen Skeggs }
4825d29588SIlia Mirkin return bios->size >= upto;
49c39f472eSBen Skeggs }
50c39f472eSBen Skeggs
51c39f472eSBen Skeggs static int
shadow_image(struct nvkm_bios * bios,int idx,u32 offset,struct shadow * mthd)527f5f518fSBen Skeggs shadow_image(struct nvkm_bios *bios, int idx, u32 offset, struct shadow *mthd)
53c39f472eSBen Skeggs {
5460b29d20SBen Skeggs struct nvkm_subdev *subdev = &bios->subdev;
55c39f472eSBen Skeggs struct nvbios_image image;
56c39f472eSBen Skeggs int score = 1;
57c39f472eSBen Skeggs
5825d29588SIlia Mirkin if (mthd->func->no_pcir) {
5925d29588SIlia Mirkin image.base = 0;
6025d29588SIlia Mirkin image.type = 0;
6125d29588SIlia Mirkin image.size = mthd->func->size(mthd->data);
6225d29588SIlia Mirkin image.last = 1;
6325d29588SIlia Mirkin } else {
647f5f518fSBen Skeggs if (!shadow_fetch(bios, mthd, offset + 0x1000)) {
6525d29588SIlia Mirkin nvkm_debug(subdev, "%08x: header fetch failed\n",
6625d29588SIlia Mirkin offset);
677f5f518fSBen Skeggs return 0;
687f5f518fSBen Skeggs }
697f5f518fSBen Skeggs
70c39f472eSBen Skeggs if (!nvbios_image(bios, idx, &image)) {
7160b29d20SBen Skeggs nvkm_debug(subdev, "image %d invalid\n", idx);
72c39f472eSBen Skeggs return 0;
73c39f472eSBen Skeggs }
7425d29588SIlia Mirkin }
7560b29d20SBen Skeggs nvkm_debug(subdev, "%08x: type %02x, %d bytes\n",
76c39f472eSBen Skeggs image.base, image.type, image.size);
77c39f472eSBen Skeggs
78402a8966SBen Skeggs if (!shadow_fetch(bios, mthd, image.base + image.size)) {
7960b29d20SBen Skeggs nvkm_debug(subdev, "%08x: fetch failed\n", image.base);
80c39f472eSBen Skeggs return 0;
81c39f472eSBen Skeggs }
82c39f472eSBen Skeggs
83c39f472eSBen Skeggs switch (image.type) {
84c39f472eSBen Skeggs case 0x00:
8525d29588SIlia Mirkin if (!mthd->func->ignore_checksum &&
8625d29588SIlia Mirkin nvbios_checksum(&bios->data[image.base], image.size)) {
8760b29d20SBen Skeggs nvkm_debug(subdev, "%08x: checksum failed\n",
8860b29d20SBen Skeggs image.base);
895dc7f4aaSBen Skeggs if (!mthd->func->require_checksum) {
90c39f472eSBen Skeggs if (mthd->func->rw)
91c39f472eSBen Skeggs score += 1;
92c39f472eSBen Skeggs score += 1;
935dc7f4aaSBen Skeggs } else
945dc7f4aaSBen Skeggs return 0;
95c39f472eSBen Skeggs } else {
96c39f472eSBen Skeggs score += 3;
97c39f472eSBen Skeggs }
98c39f472eSBen Skeggs break;
99c39f472eSBen Skeggs default:
100c39f472eSBen Skeggs score += 3;
101c39f472eSBen Skeggs break;
102c39f472eSBen Skeggs }
103c39f472eSBen Skeggs
104c39f472eSBen Skeggs if (!image.last)
1057f5f518fSBen Skeggs score += shadow_image(bios, idx + 1, offset + image.size, mthd);
106c39f472eSBen Skeggs return score;
107c39f472eSBen Skeggs }
108c39f472eSBen Skeggs
109c39f472eSBen Skeggs static int
shadow_method(struct nvkm_bios * bios,struct shadow * mthd,const char * name)110d390b480SBen Skeggs shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
111c39f472eSBen Skeggs {
112c39f472eSBen Skeggs const struct nvbios_source *func = mthd->func;
11360b29d20SBen Skeggs struct nvkm_subdev *subdev = &bios->subdev;
114c39f472eSBen Skeggs if (func->name) {
11560b29d20SBen Skeggs nvkm_debug(subdev, "trying %s...\n", name ? name : func->name);
116c39f472eSBen Skeggs if (func->init) {
117c39f472eSBen Skeggs mthd->data = func->init(bios, name);
118c39f472eSBen Skeggs if (IS_ERR(mthd->data)) {
119c39f472eSBen Skeggs mthd->data = NULL;
120c39f472eSBen Skeggs return 0;
121c39f472eSBen Skeggs }
122c39f472eSBen Skeggs }
1237f5f518fSBen Skeggs mthd->score = shadow_image(bios, 0, 0, mthd);
124c39f472eSBen Skeggs if (func->fini)
125c39f472eSBen Skeggs func->fini(mthd->data);
12660b29d20SBen Skeggs nvkm_debug(subdev, "scored %d\n", mthd->score);
127c39f472eSBen Skeggs mthd->data = bios->data;
128c39f472eSBen Skeggs mthd->size = bios->size;
129c39f472eSBen Skeggs bios->data = NULL;
130c39f472eSBen Skeggs bios->size = 0;
131c39f472eSBen Skeggs }
132c39f472eSBen Skeggs return mthd->score;
133c39f472eSBen Skeggs }
134c39f472eSBen Skeggs
135c39f472eSBen Skeggs static u32
shadow_fw_read(void * data,u32 offset,u32 length,struct nvkm_bios * bios)136d390b480SBen Skeggs shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
137c39f472eSBen Skeggs {
138c39f472eSBen Skeggs const struct firmware *fw = data;
139c39f472eSBen Skeggs if (offset + length <= fw->size) {
140c39f472eSBen Skeggs memcpy(bios->data + offset, fw->data + offset, length);
141c39f472eSBen Skeggs return length;
142c39f472eSBen Skeggs }
143c39f472eSBen Skeggs return 0;
144c39f472eSBen Skeggs }
145c39f472eSBen Skeggs
146c39f472eSBen Skeggs static void *
shadow_fw_init(struct nvkm_bios * bios,const char * name)147d390b480SBen Skeggs shadow_fw_init(struct nvkm_bios *bios, const char *name)
148c39f472eSBen Skeggs {
14926c9e8efSBen Skeggs struct device *dev = bios->subdev.device->dev;
150c39f472eSBen Skeggs const struct firmware *fw;
151c39f472eSBen Skeggs int ret = request_firmware(&fw, name, dev);
152c39f472eSBen Skeggs if (ret)
153c39f472eSBen Skeggs return ERR_PTR(-ENOENT);
154c39f472eSBen Skeggs return (void *)fw;
155c39f472eSBen Skeggs }
156c39f472eSBen Skeggs
157*b6979032SArnd Bergmann static void
shadow_fw_release(void * fw)158*b6979032SArnd Bergmann shadow_fw_release(void *fw)
159*b6979032SArnd Bergmann {
160*b6979032SArnd Bergmann release_firmware(fw);
161*b6979032SArnd Bergmann }
162*b6979032SArnd Bergmann
163c39f472eSBen Skeggs static const struct nvbios_source
164c39f472eSBen Skeggs shadow_fw = {
165c39f472eSBen Skeggs .name = "firmware",
166c39f472eSBen Skeggs .init = shadow_fw_init,
167*b6979032SArnd Bergmann .fini = shadow_fw_release,
168c39f472eSBen Skeggs .read = shadow_fw_read,
169c39f472eSBen Skeggs .rw = false,
170c39f472eSBen Skeggs };
171c39f472eSBen Skeggs
172c39f472eSBen Skeggs int
nvbios_shadow(struct nvkm_bios * bios)173d390b480SBen Skeggs nvbios_shadow(struct nvkm_bios *bios)
174c39f472eSBen Skeggs {
17560b29d20SBen Skeggs struct nvkm_subdev *subdev = &bios->subdev;
17660b29d20SBen Skeggs struct nvkm_device *device = subdev->device;
177c39f472eSBen Skeggs struct shadow mthds[] = {
1787f5f518fSBen Skeggs { 0, &nvbios_of },
1797f5f518fSBen Skeggs { 0, &nvbios_ramin },
1806aed665fSChristophe Leroy { 0, &nvbios_prom },
1817f5f518fSBen Skeggs { 0, &nvbios_acpi_fast },
1827f5f518fSBen Skeggs { 4, &nvbios_acpi_slow },
1837f5f518fSBen Skeggs { 1, &nvbios_pcirom },
1847f5f518fSBen Skeggs { 1, &nvbios_platform },
1857f5f518fSBen Skeggs {}
186a00014e3SBen Skeggs }, *mthd, *best = NULL;
187c39f472eSBen Skeggs const char *optarg;
188c39f472eSBen Skeggs char *source;
189c39f472eSBen Skeggs int optlen;
190c39f472eSBen Skeggs
191c39f472eSBen Skeggs /* handle user-specified bios source */
19260b29d20SBen Skeggs optarg = nvkm_stropt(device->cfgopt, "NvBios", &optlen);
193c39f472eSBen Skeggs source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
194c39f472eSBen Skeggs if (source) {
195c39f472eSBen Skeggs /* try to match one of the built-in methods */
196c39f472eSBen Skeggs for (mthd = mthds; mthd->func; mthd++) {
197c39f472eSBen Skeggs if (mthd->func->name &&
198c39f472eSBen Skeggs !strcasecmp(source, mthd->func->name)) {
199c39f472eSBen Skeggs best = mthd;
200c39f472eSBen Skeggs if (shadow_method(bios, mthd, NULL))
201c39f472eSBen Skeggs break;
202c39f472eSBen Skeggs }
203c39f472eSBen Skeggs }
204c39f472eSBen Skeggs
205c39f472eSBen Skeggs /* otherwise, attempt to load as firmware */
206c39f472eSBen Skeggs if (!best && (best = mthd)) {
207c39f472eSBen Skeggs mthd->func = &shadow_fw;
208c39f472eSBen Skeggs shadow_method(bios, mthd, source);
209c39f472eSBen Skeggs mthd->func = NULL;
210c39f472eSBen Skeggs }
211c39f472eSBen Skeggs
212c39f472eSBen Skeggs if (!best->score) {
21360b29d20SBen Skeggs nvkm_error(subdev, "%s invalid\n", source);
214c39f472eSBen Skeggs kfree(source);
215c39f472eSBen Skeggs source = NULL;
216c39f472eSBen Skeggs }
217c39f472eSBen Skeggs }
218c39f472eSBen Skeggs
219c39f472eSBen Skeggs /* scan all potential bios sources, looking for best image */
220c39f472eSBen Skeggs if (!best || !best->score) {
221c39f472eSBen Skeggs for (mthd = mthds, best = mthd; mthd->func; mthd++) {
222c39f472eSBen Skeggs if (!mthd->skip || best->score < mthd->skip) {
223c39f472eSBen Skeggs if (shadow_method(bios, mthd, NULL)) {
224c39f472eSBen Skeggs if (mthd->score > best->score)
225c39f472eSBen Skeggs best = mthd;
226c39f472eSBen Skeggs }
227c39f472eSBen Skeggs }
228c39f472eSBen Skeggs }
229c39f472eSBen Skeggs }
230c39f472eSBen Skeggs
231c39f472eSBen Skeggs /* cleanup the ones we didn't use */
232c39f472eSBen Skeggs for (mthd = mthds; mthd->func; mthd++) {
233c39f472eSBen Skeggs if (mthd != best)
234c39f472eSBen Skeggs kfree(mthd->data);
235c39f472eSBen Skeggs }
236c39f472eSBen Skeggs
237c39f472eSBen Skeggs if (!best->score) {
23860b29d20SBen Skeggs nvkm_error(subdev, "unable to locate usable image\n");
239c39f472eSBen Skeggs return -EINVAL;
240c39f472eSBen Skeggs }
241c39f472eSBen Skeggs
24260b29d20SBen Skeggs nvkm_debug(subdev, "using image from %s\n", best->func ?
243c39f472eSBen Skeggs best->func->name : source);
244c39f472eSBen Skeggs bios->data = best->data;
245c39f472eSBen Skeggs bios->size = best->size;
246c39f472eSBen Skeggs kfree(source);
247c39f472eSBen Skeggs return 0;
248c39f472eSBen Skeggs }
249