]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/nvkm/subdev/bios/shadow.c
Merge remote-tracking branch 'input-current/for-linus'
[karo-tx-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         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 nvkm_bios *bios, struct shadow *mthd, u32 upto)
40 {
41         const u32 limit = (upto + 3) & ~3;
42         const u32 start = bios->size;
43         void *data = mthd->data;
44         if (nvbios_extend(bios, limit) > 0) {
45                 u32 read = mthd->func->read(data, start, limit - start, bios);
46                 bios->size = start + read;
47         }
48         return bios->size >= upto;
49 }
50
51 static int
52 shadow_image(struct nvkm_bios *bios, int idx, u32 offset, struct shadow *mthd)
53 {
54         struct nvkm_subdev *subdev = &bios->subdev;
55         struct nvbios_image image;
56         int score = 1;
57
58         if (mthd->func->no_pcir) {
59                 image.base = 0;
60                 image.type = 0;
61                 image.size = mthd->func->size(mthd->data);
62                 image.last = 1;
63         } else {
64                 if (!shadow_fetch(bios, mthd, offset + 0x1000)) {
65                         nvkm_debug(subdev, "%08x: header fetch failed\n",
66                                    offset);
67                         return 0;
68                 }
69
70                 if (!nvbios_image(bios, idx, &image)) {
71                         nvkm_debug(subdev, "image %d invalid\n", idx);
72                         return 0;
73                 }
74         }
75         nvkm_debug(subdev, "%08x: type %02x, %d bytes\n",
76                    image.base, image.type, image.size);
77
78         if (!shadow_fetch(bios, mthd, image.size)) {
79                 nvkm_debug(subdev, "%08x: fetch failed\n", image.base);
80                 return 0;
81         }
82
83         switch (image.type) {
84         case 0x00:
85                 if (!mthd->func->ignore_checksum &&
86                     nvbios_checksum(&bios->data[image.base], image.size)) {
87                         nvkm_debug(subdev, "%08x: checksum failed\n",
88                                    image.base);
89                         if (mthd->func->rw)
90                                 score += 1;
91                         score += 1;
92                 } else {
93                         score += 3;
94                 }
95                 break;
96         default:
97                 score += 3;
98                 break;
99         }
100
101         if (!image.last)
102                 score += shadow_image(bios, idx + 1, offset + image.size, mthd);
103         return score;
104 }
105
106 static int
107 shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
108 {
109         const struct nvbios_source *func = mthd->func;
110         struct nvkm_subdev *subdev = &bios->subdev;
111         if (func->name) {
112                 nvkm_debug(subdev, "trying %s...\n", name ? name : func->name);
113                 if (func->init) {
114                         mthd->data = func->init(bios, name);
115                         if (IS_ERR(mthd->data)) {
116                                 mthd->data = NULL;
117                                 return 0;
118                         }
119                 }
120                 mthd->score = shadow_image(bios, 0, 0, mthd);
121                 if (func->fini)
122                         func->fini(mthd->data);
123                 nvkm_debug(subdev, "scored %d\n", mthd->score);
124                 mthd->data = bios->data;
125                 mthd->size = bios->size;
126                 bios->data  = NULL;
127                 bios->size  = 0;
128         }
129         return mthd->score;
130 }
131
132 static u32
133 shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
134 {
135         const struct firmware *fw = data;
136         if (offset + length <= fw->size) {
137                 memcpy(bios->data + offset, fw->data + offset, length);
138                 return length;
139         }
140         return 0;
141 }
142
143 static void *
144 shadow_fw_init(struct nvkm_bios *bios, const char *name)
145 {
146         struct device *dev = bios->subdev.device->dev;
147         const struct firmware *fw;
148         int ret = request_firmware(&fw, name, dev);
149         if (ret)
150                 return ERR_PTR(-ENOENT);
151         return (void *)fw;
152 }
153
154 static const struct nvbios_source
155 shadow_fw = {
156         .name = "firmware",
157         .init = shadow_fw_init,
158         .fini = (void(*)(void *))release_firmware,
159         .read = shadow_fw_read,
160         .rw = false,
161 };
162
163 int
164 nvbios_shadow(struct nvkm_bios *bios)
165 {
166         struct nvkm_subdev *subdev = &bios->subdev;
167         struct nvkm_device *device = subdev->device;
168         struct shadow mthds[] = {
169                 { 0, &nvbios_of },
170                 { 0, &nvbios_ramin },
171                 { 0, &nvbios_rom },
172                 { 0, &nvbios_acpi_fast },
173                 { 4, &nvbios_acpi_slow },
174                 { 1, &nvbios_pcirom },
175                 { 1, &nvbios_platform },
176                 {}
177         }, *mthd, *best = NULL;
178         const char *optarg;
179         char *source;
180         int optlen;
181
182         /* handle user-specified bios source */
183         optarg = nvkm_stropt(device->cfgopt, "NvBios", &optlen);
184         source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
185         if (source) {
186                 /* try to match one of the built-in methods */
187                 for (mthd = mthds; mthd->func; mthd++) {
188                         if (mthd->func->name &&
189                             !strcasecmp(source, mthd->func->name)) {
190                                 best = mthd;
191                                 if (shadow_method(bios, mthd, NULL))
192                                         break;
193                         }
194                 }
195
196                 /* otherwise, attempt to load as firmware */
197                 if (!best && (best = mthd)) {
198                         mthd->func = &shadow_fw;
199                         shadow_method(bios, mthd, source);
200                         mthd->func = NULL;
201                 }
202
203                 if (!best->score) {
204                         nvkm_error(subdev, "%s invalid\n", source);
205                         kfree(source);
206                         source = NULL;
207                 }
208         }
209
210         /* scan all potential bios sources, looking for best image */
211         if (!best || !best->score) {
212                 for (mthd = mthds, best = mthd; mthd->func; mthd++) {
213                         if (!mthd->skip || best->score < mthd->skip) {
214                                 if (shadow_method(bios, mthd, NULL)) {
215                                         if (mthd->score > best->score)
216                                                 best = mthd;
217                                 }
218                         }
219                 }
220         }
221
222         /* cleanup the ones we didn't use */
223         for (mthd = mthds; mthd->func; mthd++) {
224                 if (mthd != best)
225                         kfree(mthd->data);
226         }
227
228         if (!best->score) {
229                 nvkm_error(subdev, "unable to locate usable image\n");
230                 return -EINVAL;
231         }
232
233         nvkm_debug(subdev, "using image from %s\n", best->func ?
234                    best->func->name : source);
235         bios->data = best->data;
236         bios->size = best->size;
237         kfree(source);
238         return 0;
239 }