Linux 4.7
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / secboot / gm20b.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
25 #include <core/gpuobj.h>
26
27 /*
28  * The BL header format used by GM20B's firmware is slightly different
29  * from the one of GM200. Fix the differences here.
30  */
31 struct gm20b_flcn_bl_desc {
32         u32 reserved[4];
33         u32 signature[4];
34         u32 ctx_dma;
35         u32 code_dma_base;
36         u32 non_sec_code_off;
37         u32 non_sec_code_size;
38         u32 sec_code_off;
39         u32 sec_code_size;
40         u32 code_entry_point;
41         u32 data_dma_base;
42         u32 data_size;
43 };
44
45 /**
46  * gm20b_secboot_fixup_bl_desc - adapt BL descriptor to format used by GM20B FW
47  *
48  * There is only a slight format difference (DMA addresses being 32-bits and
49  * 256B-aligned) to address.
50  */
51 static void
52 gm20b_secboot_fixup_bl_desc(const struct gm200_flcn_bl_desc *desc, void *ret)
53 {
54         struct gm20b_flcn_bl_desc *gdesc = ret;
55         u64 addr;
56
57         memcpy(gdesc->reserved, desc->reserved, sizeof(gdesc->reserved));
58         memcpy(gdesc->signature, desc->signature, sizeof(gdesc->signature));
59         gdesc->ctx_dma = desc->ctx_dma;
60         addr = desc->code_dma_base.hi;
61         addr <<= 32;
62         addr |= desc->code_dma_base.lo;
63         gdesc->code_dma_base = lower_32_bits(addr >> 8);
64         gdesc->non_sec_code_off = desc->non_sec_code_off;
65         gdesc->non_sec_code_size = desc->non_sec_code_size;
66         gdesc->sec_code_off = desc->sec_code_off;
67         gdesc->sec_code_size = desc->sec_code_size;
68         gdesc->code_entry_point = desc->code_entry_point;
69         addr = desc->data_dma_base.hi;
70         addr <<= 32;
71         addr |= desc->data_dma_base.lo;
72         gdesc->data_dma_base = lower_32_bits(addr >> 8);
73         gdesc->data_size = desc->data_size;
74 }
75
76 static void
77 gm20b_secboot_fixup_hs_desc(struct gm200_secboot *gsb,
78                             struct hsflcn_acr_desc *desc)
79 {
80         desc->ucode_blob_base = gsb->ls_blob->addr;
81         desc->ucode_blob_size = gsb->ls_blob->size;
82
83         desc->wpr_offset = 0;
84 }
85
86 static const struct gm200_secboot_func
87 gm20b_secboot_func = {
88         .bl_desc_size = sizeof(struct gm20b_flcn_bl_desc),
89         .fixup_bl_desc = gm20b_secboot_fixup_bl_desc,
90         .fixup_hs_desc = gm20b_secboot_fixup_hs_desc,
91 };
92
93
94 #ifdef CONFIG_ARCH_TEGRA
95 #define TEGRA_MC_BASE                           0x70019000
96 #define MC_SECURITY_CARVEOUT2_CFG0              0xc58
97 #define MC_SECURITY_CARVEOUT2_BOM_0             0xc5c
98 #define MC_SECURITY_CARVEOUT2_BOM_HI_0          0xc60
99 #define MC_SECURITY_CARVEOUT2_SIZE_128K         0xc64
100 #define TEGRA_MC_SECURITY_CARVEOUT_CFG_LOCKED   (1 << 1)
101 /**
102  * sb_tegra_read_wpr() - read the WPR registers on Tegra
103  *
104  * On dGPU, we can manage the WPR region ourselves, but on Tegra the WPR region
105  * is reserved from system memory by the bootloader and irreversibly locked.
106  * This function reads the address and size of the pre-configured WPR region.
107  */
108 static int
109 gm20b_tegra_read_wpr(struct gm200_secboot *gsb)
110 {
111         struct nvkm_secboot *sb = &gsb->base;
112         void __iomem *mc;
113         u32 cfg;
114
115         mc = ioremap(TEGRA_MC_BASE, 0xd00);
116         if (!mc) {
117                 nvkm_error(&sb->subdev, "Cannot map Tegra MC registers\n");
118                 return PTR_ERR(mc);
119         }
120         gsb->wpr_addr = ioread32_native(mc + MC_SECURITY_CARVEOUT2_BOM_0) |
121               ((u64)ioread32_native(mc + MC_SECURITY_CARVEOUT2_BOM_HI_0) << 32);
122         gsb->wpr_size = ioread32_native(mc + MC_SECURITY_CARVEOUT2_SIZE_128K)
123                 << 17;
124         cfg = ioread32_native(mc + MC_SECURITY_CARVEOUT2_CFG0);
125         iounmap(mc);
126
127         /* Check that WPR settings are valid */
128         if (gsb->wpr_size == 0) {
129                 nvkm_error(&sb->subdev, "WPR region is empty\n");
130                 return -EINVAL;
131         }
132
133         if (!(cfg & TEGRA_MC_SECURITY_CARVEOUT_CFG_LOCKED)) {
134                 nvkm_error(&sb->subdev, "WPR region not locked\n");
135                 return -EINVAL;
136         }
137
138         return 0;
139 }
140 #else
141 static int
142 gm20b_tegra_read_wpr(struct gm200_secboot *gsb)
143 {
144         nvkm_error(&gsb->base.subdev, "Tegra support not compiled in\n");
145         return -EINVAL;
146 }
147 #endif
148
149 static int
150 gm20b_secboot_prepare_blobs(struct nvkm_secboot *sb)
151 {
152         struct gm200_secboot *gsb = gm200_secboot(sb);
153         int acr_size;
154         int ret;
155
156         ret = gm20x_secboot_prepare_blobs(gsb);
157         if (ret)
158                 return ret;
159
160         acr_size = gsb->acr_load_blob->size;
161         /*
162          * On Tegra the WPR region is set by the bootloader. It is illegal for
163          * the HS blob to be larger than this region.
164          */
165         if (acr_size > gsb->wpr_size) {
166                 nvkm_error(&sb->subdev, "WPR region too small for FW blob!\n");
167                 nvkm_error(&sb->subdev, "required: %dB\n", acr_size);
168                 nvkm_error(&sb->subdev, "WPR size: %dB\n", gsb->wpr_size);
169                 return -ENOSPC;
170         }
171
172         return 0;
173 }
174
175 static int
176 gm20b_secboot_init(struct nvkm_secboot *sb)
177 {
178         struct gm200_secboot *gsb = gm200_secboot(sb);
179         int ret;
180
181         ret = gm20b_tegra_read_wpr(gsb);
182         if (ret)
183                 return ret;
184
185         return gm200_secboot_init(sb);
186 }
187
188 static const struct nvkm_secboot_func
189 gm20b_secboot = {
190         .dtor = gm200_secboot_dtor,
191         .init = gm20b_secboot_init,
192         .prepare_blobs = gm20b_secboot_prepare_blobs,
193         .reset = gm200_secboot_reset,
194         .start = gm200_secboot_start,
195         .managed_falcons = BIT(NVKM_SECBOOT_FALCON_FECS),
196         .boot_falcon = NVKM_SECBOOT_FALCON_PMU,
197 };
198
199 int
200 gm20b_secboot_new(struct nvkm_device *device, int index,
201                   struct nvkm_secboot **psb)
202 {
203         int ret;
204         struct gm200_secboot *gsb;
205
206         gsb = kzalloc(sizeof(*gsb), GFP_KERNEL);
207         if (!gsb) {
208                 psb = NULL;
209                 return -ENOMEM;
210         }
211         *psb = &gsb->base;
212
213         ret = nvkm_secboot_ctor(&gm20b_secboot, device, index, &gsb->base);
214         if (ret)
215                 return ret;
216
217         gsb->func = &gm20b_secboot_func;
218
219         return 0;
220 }
221
222 MODULE_FIRMWARE("nvidia/gm20b/acr/bl.bin");
223 MODULE_FIRMWARE("nvidia/gm20b/acr/ucode_load.bin");
224 MODULE_FIRMWARE("nvidia/gm20b/gr/fecs_bl.bin");
225 MODULE_FIRMWARE("nvidia/gm20b/gr/fecs_inst.bin");
226 MODULE_FIRMWARE("nvidia/gm20b/gr/fecs_data.bin");
227 MODULE_FIRMWARE("nvidia/gm20b/gr/fecs_sig.bin");
228 MODULE_FIRMWARE("nvidia/gm20b/gr/gpccs_inst.bin");
229 MODULE_FIRMWARE("nvidia/gm20b/gr/gpccs_data.bin");
230 MODULE_FIRMWARE("nvidia/gm20b/gr/sw_ctx.bin");
231 MODULE_FIRMWARE("nvidia/gm20b/gr/sw_nonctx.bin");
232 MODULE_FIRMWARE("nvidia/gm20b/gr/sw_bundle_init.bin");
233 MODULE_FIRMWARE("nvidia/gm20b/gr/sw_method_init.bin");