aad482fe7601c9e9cbc6349ff2f018f6572720be
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / bios / shadow.c
1 /*
2  * Copyright 2014 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 <bskeggs@redhat.com>
23  */
24 #include "priv.h"
25
26 #include <core/option.h>
27 #include <subdev/bios.h>
28 #include <subdev/bios/image.h>
29
30 struct shadow {
31         struct nvkm_oclass base;
32         u32 skip;
33         const struct nvbios_source *func;
34         void *data;
35         u32 size;
36         int score;
37 };
38
39 static bool
40 shadow_fetch(struct nvkm_bios *bios, u32 upto)
41 {
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;
49         }
50         return bios->size >= limit;
51 }
52
53 static u8
54 shadow_rd08(struct nvkm_object *object, u64 addr)
55 {
56         struct nvkm_bios *bios = (void *)object;
57         if (shadow_fetch(bios, addr + 1))
58                 return bios->data[addr];
59         return 0x00;
60 }
61
62 static u16
63 shadow_rd16(struct nvkm_object *object, u64 addr)
64 {
65         struct nvkm_bios *bios = (void *)object;
66         if (shadow_fetch(bios, addr + 2))
67                 return get_unaligned_le16(&bios->data[addr]);
68         return 0x0000;
69 }
70
71 static u32
72 shadow_rd32(struct nvkm_object *object, u64 addr)
73 {
74         struct nvkm_bios *bios = (void *)object;
75         if (shadow_fetch(bios, addr + 4))
76                 return get_unaligned_le32(&bios->data[addr]);
77         return 0x00000000;
78 }
79
80 static struct nvkm_oclass
81 shadow_class = {
82         .handle = NV_SUBDEV(VBIOS, 0x00),
83         .ofuncs = &(struct nvkm_ofuncs) {
84                 .rd08 = shadow_rd08,
85                 .rd16 = shadow_rd16,
86                 .rd32 = shadow_rd32,
87         },
88 };
89
90 static int
91 shadow_image(struct nvkm_bios *bios, int idx, struct shadow *mthd)
92 {
93         struct nvkm_subdev *subdev = &bios->subdev;
94         struct nvbios_image image;
95         int score = 1;
96
97         if (!nvbios_image(bios, idx, &image)) {
98                 nvkm_debug(subdev, "image %d invalid\n", idx);
99                 return 0;
100         }
101         nvkm_debug(subdev, "%08x: type %02x, %d bytes\n",
102                    image.base, image.type, image.size);
103
104         if (!shadow_fetch(bios, image.size)) {
105                 nvkm_debug(subdev, "%08x: fetch failed\n", image.base);
106                 return 0;
107         }
108
109         switch (image.type) {
110         case 0x00:
111                 if (nvbios_checksum(&bios->data[image.base], image.size)) {
112                         nvkm_debug(subdev, "%08x: checksum failed\n",
113                                    image.base);
114                         if (mthd->func->rw)
115                                 score += 1;
116                         score += 1;
117                 } else {
118                         score += 3;
119                 }
120                 break;
121         default:
122                 score += 3;
123                 break;
124         }
125
126         if (!image.last)
127                 score += shadow_image(bios, idx + 1, mthd);
128         return score;
129 }
130
131 static int
132 shadow_score(struct nvkm_bios *bios, struct shadow *mthd)
133 {
134         struct nvkm_oclass *oclass = nv_object(bios)->oclass;
135         int score;
136         nv_object(bios)->oclass = &mthd->base;
137         score = shadow_image(bios, 0, mthd);
138         nv_object(bios)->oclass = oclass;
139         return score;
140
141 }
142
143 static int
144 shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
145 {
146         const struct nvbios_source *func = mthd->func;
147         struct nvkm_subdev *subdev = &bios->subdev;
148         if (func->name) {
149                 nvkm_debug(subdev, "trying %s...\n", name ? name : func->name);
150                 if (func->init) {
151                         mthd->data = func->init(bios, name);
152                         if (IS_ERR(mthd->data)) {
153                                 mthd->data = NULL;
154                                 return 0;
155                         }
156                 }
157                 mthd->score = shadow_score(bios, mthd);
158                 if (func->fini)
159                         func->fini(mthd->data);
160                 nvkm_debug(subdev, "scored %d\n", mthd->score);
161                 mthd->data = bios->data;
162                 mthd->size = bios->size;
163                 bios->data  = NULL;
164                 bios->size  = 0;
165         }
166         return mthd->score;
167 }
168
169 static u32
170 shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
171 {
172         const struct firmware *fw = data;
173         if (offset + length <= fw->size) {
174                 memcpy(bios->data + offset, fw->data + offset, length);
175                 return length;
176         }
177         return 0;
178 }
179
180 static void *
181 shadow_fw_init(struct nvkm_bios *bios, const char *name)
182 {
183         struct device *dev = &nv_device(bios)->pdev->dev;
184         const struct firmware *fw;
185         int ret = request_firmware(&fw, name, dev);
186         if (ret)
187                 return ERR_PTR(-ENOENT);
188         return (void *)fw;
189 }
190
191 static const struct nvbios_source
192 shadow_fw = {
193         .name = "firmware",
194         .init = shadow_fw_init,
195         .fini = (void(*)(void *))release_firmware,
196         .read = shadow_fw_read,
197         .rw = false,
198 };
199
200 int
201 nvbios_shadow(struct nvkm_bios *bios)
202 {
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 },
213                 { shadow_class }
214         }, *mthd, *best = NULL;
215         const char *optarg;
216         char *source;
217         int optlen;
218
219         /* handle user-specified bios source */
220         optarg = nvkm_stropt(device->cfgopt, "NvBios", &optlen);
221         source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
222         if (source) {
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)) {
227                                 best = mthd;
228                                 if (shadow_method(bios, mthd, NULL))
229                                         break;
230                         }
231                 }
232
233                 /* otherwise, attempt to load as firmware */
234                 if (!best && (best = mthd)) {
235                         mthd->func = &shadow_fw;
236                         shadow_method(bios, mthd, source);
237                         mthd->func = NULL;
238                 }
239
240                 if (!best->score) {
241                         nvkm_error(subdev, "%s invalid\n", source);
242                         kfree(source);
243                         source = NULL;
244                 }
245         }
246
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)
253                                                 best = mthd;
254                                 }
255                         }
256                 }
257         }
258
259         /* cleanup the ones we didn't use */
260         for (mthd = mthds; mthd->func; mthd++) {
261                 if (mthd != best)
262                         kfree(mthd->data);
263         }
264
265         if (!best->score) {
266                 nvkm_error(subdev, "unable to locate usable image\n");
267                 return -EINVAL;
268         }
269
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;
274         kfree(source);
275         return 0;
276 }