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