Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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/device.h>
27 #include <core/option.h>
28 #include <subdev/bios.h>
29 #include <subdev/bios/image.h>
30
31 struct shadow {
32         struct nvkm_oclass base;
33         u32 skip;
34         const struct nvbios_source *func;
35         void *data;
36         u32 size;
37         int score;
38 };
39
40 static bool
41 shadow_fetch(struct nvkm_bios *bios, u32 upto)
42 {
43         struct shadow *mthd = (void *)nv_object(bios)->oclass;
44         const u32 limit = (upto + 3) & ~3;
45         const u32 start = bios->size;
46         void *data = mthd->data;
47         if (nvbios_extend(bios, limit) > 0) {
48                 u32 read = mthd->func->read(data, start, limit - start, bios);
49                 bios->size = start + read;
50         }
51         return bios->size >= limit;
52 }
53
54 static u8
55 shadow_rd08(struct nvkm_object *object, u64 addr)
56 {
57         struct nvkm_bios *bios = (void *)object;
58         if (shadow_fetch(bios, addr + 1))
59                 return bios->data[addr];
60         return 0x00;
61 }
62
63 static u16
64 shadow_rd16(struct nvkm_object *object, u64 addr)
65 {
66         struct nvkm_bios *bios = (void *)object;
67         if (shadow_fetch(bios, addr + 2))
68                 return get_unaligned_le16(&bios->data[addr]);
69         return 0x0000;
70 }
71
72 static u32
73 shadow_rd32(struct nvkm_object *object, u64 addr)
74 {
75         struct nvkm_bios *bios = (void *)object;
76         if (shadow_fetch(bios, addr + 4))
77                 return get_unaligned_le32(&bios->data[addr]);
78         return 0x00000000;
79 }
80
81 static struct nvkm_oclass
82 shadow_class = {
83         .handle = NV_SUBDEV(VBIOS, 0x00),
84         .ofuncs = &(struct nvkm_ofuncs) {
85                 .rd08 = shadow_rd08,
86                 .rd16 = shadow_rd16,
87                 .rd32 = shadow_rd32,
88         },
89 };
90
91 static int
92 shadow_image(struct nvkm_bios *bios, int idx, struct shadow *mthd)
93 {
94         struct nvbios_image image;
95         int score = 1;
96
97         if (!nvbios_image(bios, idx, &image)) {
98                 nv_debug(bios, "image %d invalid\n", idx);
99                 return 0;
100         }
101         nv_debug(bios, "%08x: type %02x, %d bytes\n",
102                  image.base, image.type, image.size);
103
104         if (!shadow_fetch(bios, image.size)) {
105                 nv_debug(bios, "%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                         nv_debug(bios, "%08x: checksum failed\n", image.base);
113                         if (mthd->func->rw)
114                                 score += 1;
115                         score += 1;
116                 } else {
117                         score += 3;
118                 }
119                 break;
120         default:
121                 score += 3;
122                 break;
123         }
124
125         if (!image.last)
126                 score += shadow_image(bios, idx + 1, mthd);
127         return score;
128 }
129
130 static int
131 shadow_score(struct nvkm_bios *bios, struct shadow *mthd)
132 {
133         struct nvkm_oclass *oclass = nv_object(bios)->oclass;
134         int score;
135         nv_object(bios)->oclass = &mthd->base;
136         score = shadow_image(bios, 0, mthd);
137         nv_object(bios)->oclass = oclass;
138         return score;
139
140 }
141
142 static int
143 shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
144 {
145         const struct nvbios_source *func = mthd->func;
146         if (func->name) {
147                 nv_debug(bios, "trying %s...\n", name ? name : func->name);
148                 if (func->init) {
149                         mthd->data = func->init(bios, name);
150                         if (IS_ERR(mthd->data)) {
151                                 mthd->data = NULL;
152                                 return 0;
153                         }
154                 }
155                 mthd->score = shadow_score(bios, mthd);
156                 if (func->fini)
157                         func->fini(mthd->data);
158                 nv_debug(bios, "scored %d\n", mthd->score);
159                 mthd->data = bios->data;
160                 mthd->size = bios->size;
161                 bios->data  = NULL;
162                 bios->size  = 0;
163         }
164         return mthd->score;
165 }
166
167 static u32
168 shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
169 {
170         const struct firmware *fw = data;
171         if (offset + length <= fw->size) {
172                 memcpy(bios->data + offset, fw->data + offset, length);
173                 return length;
174         }
175         return 0;
176 }
177
178 static void *
179 shadow_fw_init(struct nvkm_bios *bios, const char *name)
180 {
181         struct device *dev = &nv_device(bios)->pdev->dev;
182         const struct firmware *fw;
183         int ret = request_firmware(&fw, name, dev);
184         if (ret)
185                 return ERR_PTR(-ENOENT);
186         return (void *)fw;
187 }
188
189 static const struct nvbios_source
190 shadow_fw = {
191         .name = "firmware",
192         .init = shadow_fw_init,
193         .fini = (void(*)(void *))release_firmware,
194         .read = shadow_fw_read,
195         .rw = false,
196 };
197
198 int
199 nvbios_shadow(struct nvkm_bios *bios)
200 {
201         struct shadow mthds[] = {
202                 { shadow_class, 0, &nvbios_of },
203                 { shadow_class, 0, &nvbios_ramin },
204                 { shadow_class, 0, &nvbios_rom },
205                 { shadow_class, 0, &nvbios_acpi_fast },
206                 { shadow_class, 4, &nvbios_acpi_slow },
207                 { shadow_class, 1, &nvbios_pcirom },
208                 { shadow_class, 1, &nvbios_platform },
209                 { shadow_class }
210         }, *mthd = mthds, *best = NULL;
211         const char *optarg;
212         char *source;
213         int optlen;
214
215         /* handle user-specified bios source */
216         optarg = nvkm_stropt(nv_device(bios)->cfgopt, "NvBios", &optlen);
217         source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
218         if (source) {
219                 /* try to match one of the built-in methods */
220                 for (mthd = mthds; mthd->func; mthd++) {
221                         if (mthd->func->name &&
222                             !strcasecmp(source, mthd->func->name)) {
223                                 best = mthd;
224                                 if (shadow_method(bios, mthd, NULL))
225                                         break;
226                         }
227                 }
228
229                 /* otherwise, attempt to load as firmware */
230                 if (!best && (best = mthd)) {
231                         mthd->func = &shadow_fw;
232                         shadow_method(bios, mthd, source);
233                         mthd->func = NULL;
234                 }
235
236                 if (!best->score) {
237                         nv_error(bios, "%s invalid\n", source);
238                         kfree(source);
239                         source = NULL;
240                 }
241         }
242
243         /* scan all potential bios sources, looking for best image */
244         if (!best || !best->score) {
245                 for (mthd = mthds, best = mthd; mthd->func; mthd++) {
246                         if (!mthd->skip || best->score < mthd->skip) {
247                                 if (shadow_method(bios, mthd, NULL)) {
248                                         if (mthd->score > best->score)
249                                                 best = mthd;
250                                 }
251                         }
252                 }
253         }
254
255         /* cleanup the ones we didn't use */
256         for (mthd = mthds; mthd->func; mthd++) {
257                 if (mthd != best)
258                         kfree(mthd->data);
259         }
260
261         if (!best->score) {
262                 nv_fatal(bios, "unable to locate usable image\n");
263                 return -EINVAL;
264         }
265
266         nv_info(bios, "using image from %s\n", best->func ?
267                 best->func->name : source);
268         bios->data = best->data;
269         bios->size = best->size;
270         kfree(source);
271         return 0;
272 }