2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <core/gpuobj.h>
29 * r367 ACR: new LS signature format requires a rewrite of LS firmware and
30 * blob creation functions. Also the hsflcn_desc layout has changed slightly.
33 #define LSF_LSB_DEPMAP_SIZE 11
36 * struct acr_r367_lsf_lsb_header - LS firmware header
38 * See also struct acr_r352_lsf_lsb_header for documentation.
40 struct acr_r367_lsf_lsb_header {
42 * LS falcon signatures
43 * @prd_keys: signature to use in production mode
44 * @dgb_keys: signature to use in debug mode
45 * @b_prd_present: whether the production key is present
46 * @b_dgb_present: whether the debug key is present
47 * @falcon_id: ID of the falcon the ucode applies to
55 u32 supports_versioning;
58 u8 depmap[LSF_LSB_DEPMAP_SIZE * 2 * 4];
76 * struct acr_r367_lsf_wpr_header - LS blob WPR Header
78 * See also struct acr_r352_lsf_wpr_header for documentation.
80 struct acr_r367_lsf_wpr_header {
87 #define LSF_IMAGE_STATUS_NONE 0
88 #define LSF_IMAGE_STATUS_COPY 1
89 #define LSF_IMAGE_STATUS_VALIDATION_CODE_FAILED 2
90 #define LSF_IMAGE_STATUS_VALIDATION_DATA_FAILED 3
91 #define LSF_IMAGE_STATUS_VALIDATION_DONE 4
92 #define LSF_IMAGE_STATUS_VALIDATION_SKIPPED 5
93 #define LSF_IMAGE_STATUS_BOOTSTRAP_READY 6
94 #define LSF_IMAGE_STATUS_REVOCATION_CHECK_FAILED 7
98 * struct ls_ucode_img_r367 - ucode image augmented with r367 headers
100 struct ls_ucode_img_r367 {
101 struct ls_ucode_img base;
103 const struct acr_r352_lsf_func *func;
105 struct acr_r367_lsf_wpr_header wpr_header;
106 struct acr_r367_lsf_lsb_header lsb_header;
108 #define ls_ucode_img_r367(i) container_of(i, struct ls_ucode_img_r367, base)
110 struct ls_ucode_img *
111 acr_r367_ls_ucode_img_load(const struct acr_r352 *acr,
112 const struct nvkm_secboot *sb,
113 enum nvkm_secboot_falcon falcon_id)
115 const struct nvkm_subdev *subdev = acr->base.subdev;
116 const struct acr_r352_ls_func *func = acr->func->ls_func[falcon_id];
117 struct ls_ucode_img_r367 *img;
120 img = kzalloc(sizeof(*img), GFP_KERNEL);
122 return ERR_PTR(-ENOMEM);
124 img->base.falcon_id = falcon_id;
126 ret = func->load(sb, func->version_max, &img->base);
128 kfree(img->base.ucode_data);
129 kfree(img->base.sig);
134 img->func = func->version[ret];
136 /* Check that the signature size matches our expectations... */
137 if (img->base.sig_size != sizeof(img->lsb_header.signature)) {
138 nvkm_error(subdev, "invalid signature size for %s falcon!\n",
139 nvkm_secboot_falcon_name[falcon_id]);
140 return ERR_PTR(-EINVAL);
143 /* Copy signature to the right place */
144 memcpy(&img->lsb_header.signature, img->base.sig, img->base.sig_size);
146 /* not needed? the signature should already have the right value */
147 img->lsb_header.signature.falcon_id = falcon_id;
152 #define LSF_LSB_HEADER_ALIGN 256
153 #define LSF_BL_DATA_ALIGN 256
154 #define LSF_BL_DATA_SIZE_ALIGN 256
155 #define LSF_BL_CODE_SIZE_ALIGN 256
156 #define LSF_UCODE_DATA_ALIGN 4096
159 acr_r367_ls_img_fill_headers(struct acr_r352 *acr,
160 struct ls_ucode_img_r367 *img, u32 offset)
162 struct ls_ucode_img *_img = &img->base;
163 struct acr_r367_lsf_wpr_header *whdr = &img->wpr_header;
164 struct acr_r367_lsf_lsb_header *lhdr = &img->lsb_header;
165 struct ls_ucode_img_desc *desc = &_img->ucode_desc;
166 const struct acr_r352_lsf_func *func = img->func;
168 /* Fill WPR header */
169 whdr->falcon_id = _img->falcon_id;
170 whdr->bootstrap_owner = acr->base.boot_falcon;
171 whdr->bin_version = lhdr->signature.version;
172 whdr->status = LSF_IMAGE_STATUS_COPY;
174 /* Skip bootstrapping falcons started by someone else than ACR */
175 if (acr->lazy_bootstrap & BIT(_img->falcon_id))
176 whdr->lazy_bootstrap = 1;
178 /* Align, save off, and include an LSB header size */
179 offset = ALIGN(offset, LSF_LSB_HEADER_ALIGN);
180 whdr->lsb_offset = offset;
181 offset += sizeof(*lhdr);
184 * Align, save off, and include the original (static) ucode
187 offset = ALIGN(offset, LSF_UCODE_DATA_ALIGN);
188 _img->ucode_off = lhdr->ucode_off = offset;
189 offset += _img->ucode_size;
192 * For falcons that use a boot loader (BL), we append a loader
193 * desc structure on the end of the ucode image and consider
194 * this the boot loader data. The host will then copy the loader
195 * desc args to this space within the WPR region (before locking
196 * down) and the HS bin will then copy them to DMEM 0 for the
199 lhdr->bl_code_size = ALIGN(desc->bootloader_size,
200 LSF_BL_CODE_SIZE_ALIGN);
201 lhdr->ucode_size = ALIGN(desc->app_resident_data_offset,
202 LSF_BL_CODE_SIZE_ALIGN) + lhdr->bl_code_size;
203 lhdr->data_size = ALIGN(desc->app_size, LSF_BL_CODE_SIZE_ALIGN) +
204 lhdr->bl_code_size - lhdr->ucode_size;
206 * Though the BL is located at 0th offset of the image, the VA
207 * is different to make sure that it doesn't collide the actual
210 lhdr->bl_imem_off = desc->bootloader_imem_offset;
211 lhdr->app_code_off = desc->app_start_offset +
212 desc->app_resident_code_offset;
213 lhdr->app_code_size = desc->app_resident_code_size;
214 lhdr->app_data_off = desc->app_start_offset +
215 desc->app_resident_data_offset;
216 lhdr->app_data_size = desc->app_resident_data_size;
218 lhdr->flags = func->lhdr_flags;
219 if (_img->falcon_id == acr->base.boot_falcon)
220 lhdr->flags |= LSF_FLAG_DMACTL_REQ_CTX;
222 /* Align and save off BL descriptor size */
223 lhdr->bl_data_size = ALIGN(func->bl_desc_size, LSF_BL_DATA_SIZE_ALIGN);
226 * Align, save off, and include the additional BL data
228 offset = ALIGN(offset, LSF_BL_DATA_ALIGN);
229 lhdr->bl_data_off = offset;
230 offset += lhdr->bl_data_size;
236 acr_r367_ls_fill_headers(struct acr_r352 *acr, struct list_head *imgs)
238 struct ls_ucode_img_r367 *img;
243 /* Count the number of images to manage */
244 list_for_each(l, imgs)
248 * Start with an array of WPR headers at the base of the WPR.
249 * The expectation here is that the secure falcon will do a single DMA
250 * read of this array and cache it internally so it's ok to pack these.
251 * Also, we add 1 to the falcon count to indicate the end of the array.
253 offset = sizeof(img->wpr_header) * (count + 1);
256 * Walk the managed falcons, accounting for the LSB structs
257 * as well as the ucode images.
259 list_for_each_entry(img, imgs, base.node) {
260 offset = acr_r367_ls_img_fill_headers(acr, img, offset);
267 acr_r367_ls_write_wpr(struct acr_r352 *acr, struct list_head *imgs,
268 struct nvkm_gpuobj *wpr_blob, u64 wpr_addr)
270 struct ls_ucode_img *_img;
272 u32 max_desc_size = 0;
275 list_for_each_entry(_img, imgs, node) {
276 struct ls_ucode_img_r367 *img = ls_ucode_img_r367(_img);
277 const struct acr_r352_lsf_func *ls_func = img->func;
279 max_desc_size = max(max_desc_size, ls_func->bl_desc_size);
282 gdesc = kmalloc(max_desc_size, GFP_KERNEL);
288 list_for_each_entry(_img, imgs, node) {
289 struct ls_ucode_img_r367 *img = ls_ucode_img_r367(_img);
290 const struct acr_r352_lsf_func *ls_func = img->func;
292 nvkm_gpuobj_memcpy_to(wpr_blob, pos, &img->wpr_header,
293 sizeof(img->wpr_header));
295 nvkm_gpuobj_memcpy_to(wpr_blob, img->wpr_header.lsb_offset,
296 &img->lsb_header, sizeof(img->lsb_header));
298 /* Generate and write BL descriptor */
299 memset(gdesc, 0, ls_func->bl_desc_size);
300 ls_func->generate_bl_desc(&acr->base, _img, wpr_addr, gdesc);
302 nvkm_gpuobj_memcpy_to(wpr_blob, img->lsb_header.bl_data_off,
303 gdesc, ls_func->bl_desc_size);
306 nvkm_gpuobj_memcpy_to(wpr_blob, img->lsb_header.ucode_off,
307 _img->ucode_data, _img->ucode_size);
309 pos += sizeof(img->wpr_header);
312 nvkm_wo32(wpr_blob, pos, NVKM_SECBOOT_FALCON_INVALID);
321 struct acr_r367_hsflcn_desc {
322 u8 reserved_dmem[0x200];
326 u32 mmu_memory_range;
327 #define FLCN_ACR_MAX_REGIONS 2
337 u32 shadow_mem_start_addr;
338 } region_props[FLCN_ACR_MAX_REGIONS];
341 u64 ucode_blob_base __aligned(8);
351 acr_r367_fixup_hs_desc(struct acr_r352 *acr, struct nvkm_secboot *sb,
354 struct acr_r367_hsflcn_desc *desc = _desc;
355 struct nvkm_gpuobj *ls_blob = acr->ls_blob;
357 /* WPR region information if WPR is not fixed */
358 if (sb->wpr_size == 0) {
359 u64 wpr_start = ls_blob->addr;
360 u64 wpr_end = ls_blob->addr + ls_blob->size;
362 if (acr->func->shadow_blob)
363 wpr_start += ls_blob->size / 2;
365 desc->wpr_region_id = 1;
366 desc->regions.no_regions = 2;
367 desc->regions.region_props[0].start_addr = wpr_start >> 8;
368 desc->regions.region_props[0].end_addr = wpr_end >> 8;
369 desc->regions.region_props[0].region_id = 1;
370 desc->regions.region_props[0].read_mask = 0xf;
371 desc->regions.region_props[0].write_mask = 0xc;
372 desc->regions.region_props[0].client_mask = 0x2;
373 if (acr->func->shadow_blob)
374 desc->regions.region_props[0].shadow_mem_start_addr =
377 desc->regions.region_props[0].shadow_mem_start_addr = 0;
379 desc->ucode_blob_base = ls_blob->addr;
380 desc->ucode_blob_size = ls_blob->size;
384 const struct acr_r352_func
386 .fixup_hs_desc = acr_r367_fixup_hs_desc,
387 .generate_hs_bl_desc = acr_r361_generate_hs_bl_desc,
388 .hs_bl_desc_size = sizeof(struct acr_r361_flcn_bl_desc),
390 .ls_ucode_img_load = acr_r367_ls_ucode_img_load,
391 .ls_fill_headers = acr_r367_ls_fill_headers,
392 .ls_write_wpr = acr_r367_ls_write_wpr,
394 [NVKM_SECBOOT_FALCON_FECS] = &acr_r361_ls_fecs_func,
395 [NVKM_SECBOOT_FALCON_GPCCS] = &acr_r361_ls_gpccs_func,
396 [NVKM_SECBOOT_FALCON_PMU] = &acr_r361_ls_pmu_func,
397 [NVKM_SECBOOT_FALCON_SEC2] = &acr_r361_ls_sec2_func,
402 acr_r367_new(enum nvkm_secboot_falcon boot_falcon,
403 unsigned long managed_falcons)
405 return acr_r352_new_(&acr_r367_func, boot_falcon, managed_falcons);