]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/nvkm/subdev/secboot/base.c
drm/nouveau/secboot: use nvkm_mc_intr_mask/unmask()
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / secboot / base.c
1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 #include "priv.h"
23
24 #include <subdev/mc.h>
25 #include <subdev/timer.h>
26
27 static const char *
28 managed_falcons_names[] = {
29         [NVKM_SECBOOT_FALCON_PMU] = "PMU",
30         [NVKM_SECBOOT_FALCON_RESERVED] = "<reserved>",
31         [NVKM_SECBOOT_FALCON_FECS] = "FECS",
32         [NVKM_SECBOOT_FALCON_GPCCS] = "GPCCS",
33         [NVKM_SECBOOT_FALCON_END] = "<invalid>",
34 };
35
36 /*
37  * Helper falcon functions
38  */
39
40 static int
41 falcon_clear_halt_interrupt(struct nvkm_device *device, u32 base)
42 {
43         int ret;
44
45         /* clear halt interrupt */
46         nvkm_mask(device, base + 0x004, 0x10, 0x10);
47         /* wait until halt interrupt is cleared */
48         ret = nvkm_wait_msec(device, 10, base + 0x008, 0x10, 0x0);
49         if (ret < 0)
50                 return ret;
51
52         return 0;
53 }
54
55 static int
56 falcon_wait_idle(struct nvkm_device *device, u32 base)
57 {
58         int ret;
59
60         ret = nvkm_wait_msec(device, 10, base + 0x04c, 0xffff, 0x0);
61         if (ret < 0)
62                 return ret;
63
64         return 0;
65 }
66
67 static int
68 nvkm_secboot_falcon_enable(struct nvkm_secboot *sb)
69 {
70         struct nvkm_device *device = sb->subdev.device;
71         int ret;
72
73         /* enable engine */
74         nvkm_mask(device, 0x200, sb->enable_mask, sb->enable_mask);
75         nvkm_rd32(device, 0x200);
76         ret = nvkm_wait_msec(device, 10, sb->base + 0x10c, 0x6, 0x0);
77         if (ret < 0) {
78                 nvkm_mask(device, 0x200, sb->enable_mask, 0x0);
79                 nvkm_error(&sb->subdev, "Falcon mem scrubbing timeout\n");
80                 return ret;
81         }
82
83         ret = falcon_wait_idle(device, sb->base);
84         if (ret)
85                 return ret;
86
87         /* enable IRQs */
88         nvkm_wr32(device, sb->base + 0x010, 0xff);
89         nvkm_mc_intr_mask(device, sb->devidx, true);
90
91         return 0;
92 }
93
94 static int
95 nvkm_secboot_falcon_disable(struct nvkm_secboot *sb)
96 {
97         struct nvkm_device *device = sb->subdev.device;
98
99         /* disable IRQs and wait for any previous code to complete */
100         nvkm_mc_intr_mask(device, sb->devidx, false);
101         nvkm_wr32(device, sb->base + 0x014, 0xff);
102
103         falcon_wait_idle(device, sb->base);
104
105         /* disable engine */
106         nvkm_mask(device, 0x200, sb->enable_mask, 0x0);
107
108         return 0;
109 }
110
111 int
112 nvkm_secboot_falcon_reset(struct nvkm_secboot *sb)
113 {
114         int ret;
115
116         ret = nvkm_secboot_falcon_disable(sb);
117         if (ret)
118                 return ret;
119
120         ret = nvkm_secboot_falcon_enable(sb);
121         if (ret)
122                 return ret;
123
124         return 0;
125 }
126
127 /**
128  * nvkm_secboot_falcon_run - run the falcon that will perform secure boot
129  *
130  * This function is to be called after all chip-specific preparations have
131  * been completed. It will start the falcon to perform secure boot, wait for
132  * it to halt, and report if an error occurred.
133  */
134 int
135 nvkm_secboot_falcon_run(struct nvkm_secboot *sb)
136 {
137         struct nvkm_device *device = sb->subdev.device;
138         int ret;
139
140         /* Start falcon */
141         nvkm_wr32(device, sb->base + 0x100, 0x2);
142
143         /* Wait for falcon halt */
144         ret = nvkm_wait_msec(device, 100, sb->base + 0x100, 0x10, 0x10);
145         if (ret < 0)
146                 return ret;
147
148         /* If mailbox register contains an error code, then ACR has failed */
149         ret = nvkm_rd32(device, sb->base + 0x040);
150         if (ret) {
151                 nvkm_error(&sb->subdev, "ACR boot failed, ret 0x%08x", ret);
152                 falcon_clear_halt_interrupt(device, sb->base);
153                 return -EINVAL;
154         }
155
156         return 0;
157 }
158
159
160 /**
161  * nvkm_secboot_reset() - reset specified falcon
162  */
163 int
164 nvkm_secboot_reset(struct nvkm_secboot *sb, u32 falcon)
165 {
166         /* Unmanaged falcon? */
167         if (!(BIT(falcon) & sb->func->managed_falcons)) {
168                 nvkm_error(&sb->subdev, "cannot reset unmanaged falcon!\n");
169                 return -EINVAL;
170         }
171
172         return sb->func->reset(sb, falcon);
173 }
174
175 /**
176  * nvkm_secboot_start() - start specified falcon
177  */
178 int
179 nvkm_secboot_start(struct nvkm_secboot *sb, u32 falcon)
180 {
181         /* Unmanaged falcon? */
182         if (!(BIT(falcon) & sb->func->managed_falcons)) {
183                 nvkm_error(&sb->subdev, "cannot start unmanaged falcon!\n");
184                 return -EINVAL;
185         }
186
187         return sb->func->start(sb, falcon);
188 }
189
190 /**
191  * nvkm_secboot_is_managed() - check whether a given falcon is securely-managed
192  */
193 bool
194 nvkm_secboot_is_managed(struct nvkm_secboot *secboot,
195                         enum nvkm_secboot_falcon fid)
196 {
197         if (!secboot)
198                 return false;
199
200         return secboot->func->managed_falcons & BIT(fid);
201 }
202
203 static int
204 nvkm_secboot_oneinit(struct nvkm_subdev *subdev)
205 {
206         struct nvkm_secboot *sb = nvkm_secboot(subdev);
207         int ret = 0;
208
209         /* Call chip-specific init function */
210         if (sb->func->init)
211                 ret = sb->func->init(sb);
212         if (ret) {
213                 nvkm_error(subdev, "Secure Boot initialization failed: %d\n",
214                            ret);
215                 return ret;
216         }
217
218         /*
219          * Build all blobs - the same blobs can be used to perform secure boot
220          * multiple times
221          */
222         if (sb->func->prepare_blobs)
223                 ret = sb->func->prepare_blobs(sb);
224
225         return ret;
226 }
227
228 static int
229 nvkm_secboot_fini(struct nvkm_subdev *subdev, bool suspend)
230 {
231         struct nvkm_secboot *sb = nvkm_secboot(subdev);
232         int ret = 0;
233
234         if (sb->func->fini)
235                 ret = sb->func->fini(sb, suspend);
236
237         return ret;
238 }
239
240 static void *
241 nvkm_secboot_dtor(struct nvkm_subdev *subdev)
242 {
243         struct nvkm_secboot *sb = nvkm_secboot(subdev);
244         void *ret = NULL;
245
246         if (sb->func->dtor)
247                 ret = sb->func->dtor(sb);
248
249         return ret;
250 }
251
252 static const struct nvkm_subdev_func
253 nvkm_secboot = {
254         .oneinit = nvkm_secboot_oneinit,
255         .fini = nvkm_secboot_fini,
256         .dtor = nvkm_secboot_dtor,
257 };
258
259 int
260 nvkm_secboot_ctor(const struct nvkm_secboot_func *func,
261                   struct nvkm_device *device, int index,
262                   struct nvkm_secboot *sb)
263 {
264         unsigned long fid;
265
266         nvkm_subdev_ctor(&nvkm_secboot, device, index, &sb->subdev);
267         sb->func = func;
268
269         /* setup the performing falcon's base address and masks */
270         switch (func->boot_falcon) {
271         case NVKM_SECBOOT_FALCON_PMU:
272                 sb->devidx = NVKM_SUBDEV_PMU;
273                 sb->base = 0x10a000;
274                 sb->enable_mask = 0x2000;
275                 break;
276         default:
277                 nvkm_error(&sb->subdev, "invalid secure boot falcon\n");
278                 return -EINVAL;
279         };
280
281         nvkm_debug(&sb->subdev, "securely managed falcons:\n");
282         for_each_set_bit(fid, &sb->func->managed_falcons,
283                          NVKM_SECBOOT_FALCON_END)
284                 nvkm_debug(&sb->subdev, "- %s\n", managed_falcons_names[fid]);
285
286         return 0;
287 }