drm/nouveau/device: include core/device.h automatically for subdevs/engines
[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 nvbios_image image;
94         int score = 1;
95
96         if (!nvbios_image(bios, idx, &image)) {
97                 nv_debug(bios, "image %d invalid\n", idx);
98                 return 0;
99         }
100         nv_debug(bios, "%08x: type %02x, %d bytes\n",
101                  image.base, image.type, image.size);
102
103         if (!shadow_fetch(bios, image.size)) {
104                 nv_debug(bios, "%08x: fetch failed\n", image.base);
105                 return 0;
106         }
107
108         switch (image.type) {
109         case 0x00:
110                 if (nvbios_checksum(&bios->data[image.base], image.size)) {
111                         nv_debug(bios, "%08x: checksum failed\n", image.base);
112                         if (mthd->func->rw)
113                                 score += 1;
114                         score += 1;
115                 } else {
116                         score += 3;
117                 }
118                 break;
119         default:
120                 score += 3;
121                 break;
122         }
123
124         if (!image.last)
125                 score += shadow_image(bios, idx + 1, mthd);
126         return score;
127 }
128
129 static int
130 shadow_score(struct nvkm_bios *bios, struct shadow *mthd)
131 {
132         struct nvkm_oclass *oclass = nv_object(bios)->oclass;
133         int score;
134         nv_object(bios)->oclass = &mthd->base;
135         score = shadow_image(bios, 0, mthd);
136         nv_object(bios)->oclass = oclass;
137         return score;
138
139 }
140
141 static int
142 shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
143 {
144         const struct nvbios_source *func = mthd->func;
145         if (func->name) {
146                 nv_debug(bios, "trying %s...\n", name ? name : func->name);
147                 if (func->init) {
148                         mthd->data = func->init(bios, name);
149                         if (IS_ERR(mthd->data)) {
150                                 mthd->data = NULL;
151                                 return 0;
152                         }
153                 }
154                 mthd->score = shadow_score(bios, mthd);
155                 if (func->fini)
156                         func->fini(mthd->data);
157                 nv_debug(bios, "scored %d\n", mthd->score);
158                 mthd->data = bios->data;
159                 mthd->size = bios->size;
160                 bios->data  = NULL;
161                 bios->size  = 0;
162         }
163         return mthd->score;
164 }
165
166 static u32
167 shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
168 {
169         const struct firmware *fw = data;
170         if (offset + length <= fw->size) {
171                 memcpy(bios->data + offset, fw->data + offset, length);
172                 return length;
173         }
174         return 0;
175 }
176
177 static void *
178 shadow_fw_init(struct nvkm_bios *bios, const char *name)
179 {
180         struct device *dev = &nv_device(bios)->pdev->dev;
181         const struct firmware *fw;
182         int ret = request_firmware(&fw, name, dev);
183         if (ret)
184                 return ERR_PTR(-ENOENT);
185         return (void *)fw;
186 }
187
188 static const struct nvbios_source
189 shadow_fw = {
190         .name = "firmware",
191         .init = shadow_fw_init,
192         .fini = (void(*)(void *))release_firmware,
193         .read = shadow_fw_read,
194         .rw = false,
195 };
196
197 int
198 nvbios_shadow(struct nvkm_bios *bios)
199 {
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 },
208                 { shadow_class }
209         }, *mthd = mthds, *best = NULL;
210         const char *optarg;
211         char *source;
212         int optlen;
213
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;
217         if (source) {
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)) {
222                                 best = mthd;
223                                 if (shadow_method(bios, mthd, NULL))
224                                         break;
225                         }
226                 }
227
228                 /* otherwise, attempt to load as firmware */
229                 if (!best && (best = mthd)) {
230                         mthd->func = &shadow_fw;
231                         shadow_method(bios, mthd, source);
232                         mthd->func = NULL;
233                 }
234
235                 if (!best->score) {
236                         nv_error(bios, "%s invalid\n", source);
237                         kfree(source);
238                         source = NULL;
239                 }
240         }
241
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)
248                                                 best = mthd;
249                                 }
250                         }
251                 }
252         }
253
254         /* cleanup the ones we didn't use */
255         for (mthd = mthds; mthd->func; mthd++) {
256                 if (mthd != best)
257                         kfree(mthd->data);
258         }
259
260         if (!best->score) {
261                 nv_fatal(bios, "unable to locate usable image\n");
262                 return -EINVAL;
263         }
264
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;
269         kfree(source);
270         return 0;
271 }