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