2 * Copyright (c) 2016, 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>
27 #include <core/firmware.h>
28 #include <engine/falcon.h>
29 #include <subdev/pmu.h>
30 #include <core/msgqueue.h>
31 #include <engine/sec2.h>
34 * struct acr_r352_flcn_bl_desc - DMEM bootloader descriptor
35 * @signature: 16B signature for secure code. 0s if no secure code
36 * @ctx_dma: DMA context to be used by BL while loading code/data
37 * @code_dma_base: 256B-aligned Physical FB Address where code is located
38 * (falcon's $xcbase register)
39 * @non_sec_code_off: offset from code_dma_base where the non-secure code is
40 * located. The offset must be multiple of 256 to help perf
41 * @non_sec_code_size: the size of the nonSecure code part.
42 * @sec_code_off: offset from code_dma_base where the secure code is
43 * located. The offset must be multiple of 256 to help perf
44 * @sec_code_size: offset from code_dma_base where the secure code is
45 * located. The offset must be multiple of 256 to help perf
46 * @code_entry_point: code entry point which will be invoked by BL after
48 * @data_dma_base: 256B aligned Physical FB Address where data is located.
49 * (falcon's $xdbase register)
50 * @data_size: size of data block. Should be multiple of 256B
52 * Structure used by the bootloader to load the rest of the code. This has
53 * to be filled by host and copied into DMEM at offset provided in the
54 * hsflcn_bl_desc.bl_desc_dmem_load_off.
56 struct acr_r352_flcn_bl_desc {
62 u32 non_sec_code_size;
73 * acr_r352_generate_flcn_bl_desc - generate generic BL descriptor for LS image
76 acr_r352_generate_flcn_bl_desc(const struct nvkm_acr *acr,
77 const struct ls_ucode_img *img, u64 wpr_addr,
80 struct acr_r352_flcn_bl_desc *desc = _desc;
81 const struct ls_ucode_img_desc *pdesc = &img->ucode_desc;
82 u64 base, addr_code, addr_data;
84 base = wpr_addr + img->ucode_off + pdesc->app_start_offset;
85 addr_code = (base + pdesc->app_resident_code_offset) >> 8;
86 addr_data = (base + pdesc->app_resident_data_offset) >> 8;
88 desc->ctx_dma = FALCON_DMAIDX_UCODE;
89 desc->code_dma_base = lower_32_bits(addr_code);
90 desc->code_dma_base1 = upper_32_bits(addr_code);
91 desc->non_sec_code_off = pdesc->app_resident_code_offset;
92 desc->non_sec_code_size = pdesc->app_resident_code_size;
93 desc->code_entry_point = pdesc->app_imem_entry;
94 desc->data_dma_base = lower_32_bits(addr_data);
95 desc->data_dma_base1 = upper_32_bits(addr_data);
96 desc->data_size = pdesc->app_resident_data_size;
101 * struct hsflcn_acr_desc - data section of the HS firmware
103 * This header is to be copied at the beginning of DMEM by the HS bootloader.
105 * @signature: signature of ACR ucode
106 * @wpr_region_id: region ID holding the WPR header and its details
107 * @wpr_offset: offset from the WPR region holding the wpr header
108 * @regions: region descriptors
109 * @nonwpr_ucode_blob_size: size of LS blob
110 * @nonwpr_ucode_blob_start: FB location of LS blob is
112 struct hsflcn_acr_desc {
114 u8 reserved_dmem[0x200];
116 } ucode_reserved_space;
120 #define FLCN_ACR_MAX_REGIONS 2
130 } region_props[FLCN_ACR_MAX_REGIONS];
133 u64 ucode_blob_base __aligned(8);
144 * Low-secure blob creation
148 * struct acr_r352_lsf_lsb_header - LS firmware header
149 * @signature: signature to verify the firmware against
150 * @ucode_off: offset of the ucode blob in the WPR region. The ucode
151 * blob contains the bootloader, code and data of the
153 * @ucode_size: size of the ucode blob, including bootloader
154 * @data_size: size of the ucode blob data
155 * @bl_code_size: size of the bootloader code
156 * @bl_imem_off: offset in imem of the bootloader
157 * @bl_data_off: offset of the bootloader data in WPR region
158 * @bl_data_size: size of the bootloader data
159 * @app_code_off: offset of the app code relative to ucode_off
160 * @app_code_size: size of the app code
161 * @app_data_off: offset of the app data relative to ucode_off
162 * @app_data_size: size of the app data
163 * @flags: flags for the secure bootloader
165 * This structure is written into the WPR region for each managed falcon. Each
166 * instance is referenced by the lsb_offset member of the corresponding
169 struct acr_r352_lsf_lsb_header {
171 * LS falcon signatures
172 * @prd_keys: signature to use in production mode
173 * @dgb_keys: signature to use in debug mode
174 * @b_prd_present: whether the production key is present
175 * @b_dgb_present: whether the debug key is present
176 * @falcon_id: ID of the falcon the ucode applies to
200 * struct acr_r352_lsf_wpr_header - LS blob WPR Header
201 * @falcon_id: LS falcon ID
202 * @lsb_offset: offset of the lsb_lsf_header in the WPR region
203 * @bootstrap_owner: secure falcon reponsible for bootstrapping the LS falcon
204 * @lazy_bootstrap: skip bootstrapping by ACR
205 * @status: bootstrapping status
207 * An array of these is written at the beginning of the WPR region, one for
208 * each managed falcon. The array is terminated by an instance which falcon_id
209 * is LSF_FALCON_ID_INVALID.
211 struct acr_r352_lsf_wpr_header {
217 #define LSF_IMAGE_STATUS_NONE 0
218 #define LSF_IMAGE_STATUS_COPY 1
219 #define LSF_IMAGE_STATUS_VALIDATION_CODE_FAILED 2
220 #define LSF_IMAGE_STATUS_VALIDATION_DATA_FAILED 3
221 #define LSF_IMAGE_STATUS_VALIDATION_DONE 4
222 #define LSF_IMAGE_STATUS_VALIDATION_SKIPPED 5
223 #define LSF_IMAGE_STATUS_BOOTSTRAP_READY 6
227 * struct ls_ucode_img_r352 - ucode image augmented with r352 headers
229 struct ls_ucode_img_r352 {
230 struct ls_ucode_img base;
232 struct acr_r352_lsf_wpr_header wpr_header;
233 struct acr_r352_lsf_lsb_header lsb_header;
235 #define ls_ucode_img_r352(i) container_of(i, struct ls_ucode_img_r352, base)
238 * ls_ucode_img_load() - create a lsf_ucode_img and load it
240 struct ls_ucode_img *
241 acr_r352_ls_ucode_img_load(const struct acr_r352 *acr,
242 const struct nvkm_secboot *sb,
243 enum nvkm_secboot_falcon falcon_id)
245 const struct nvkm_subdev *subdev = acr->base.subdev;
246 struct ls_ucode_img_r352 *img;
249 img = kzalloc(sizeof(*img), GFP_KERNEL);
251 return ERR_PTR(-ENOMEM);
253 img->base.falcon_id = falcon_id;
255 ret = acr->func->ls_func[falcon_id]->load(sb, &img->base);
258 kfree(img->base.ucode_data);
259 kfree(img->base.sig);
264 /* Check that the signature size matches our expectations... */
265 if (img->base.sig_size != sizeof(img->lsb_header.signature)) {
266 nvkm_error(subdev, "invalid signature size for %s falcon!\n",
267 nvkm_secboot_falcon_name[falcon_id]);
268 return ERR_PTR(-EINVAL);
271 /* Copy signature to the right place */
272 memcpy(&img->lsb_header.signature, img->base.sig, img->base.sig_size);
274 /* not needed? the signature should already have the right value */
275 img->lsb_header.signature.falcon_id = falcon_id;
280 #define LSF_LSB_HEADER_ALIGN 256
281 #define LSF_BL_DATA_ALIGN 256
282 #define LSF_BL_DATA_SIZE_ALIGN 256
283 #define LSF_BL_CODE_SIZE_ALIGN 256
284 #define LSF_UCODE_DATA_ALIGN 4096
287 * acr_r352_ls_img_fill_headers - fill the WPR and LSB headers of an image
289 * @img: image to generate for
290 * @offset: offset in the WPR region where this image starts
292 * Allocate space in the WPR area from offset and write the WPR and LSB headers
295 * Return: offset at the end of this image.
298 acr_r352_ls_img_fill_headers(struct acr_r352 *acr,
299 struct ls_ucode_img_r352 *img, u32 offset)
301 struct ls_ucode_img *_img = &img->base;
302 struct acr_r352_lsf_wpr_header *whdr = &img->wpr_header;
303 struct acr_r352_lsf_lsb_header *lhdr = &img->lsb_header;
304 struct ls_ucode_img_desc *desc = &_img->ucode_desc;
305 const struct acr_r352_ls_func *func =
306 acr->func->ls_func[_img->falcon_id];
308 /* Fill WPR header */
309 whdr->falcon_id = _img->falcon_id;
310 whdr->bootstrap_owner = acr->base.boot_falcon;
311 whdr->status = LSF_IMAGE_STATUS_COPY;
313 /* Skip bootstrapping falcons started by someone else than ACR */
314 if (acr->lazy_bootstrap & BIT(_img->falcon_id))
315 whdr->lazy_bootstrap = 1;
317 /* Align, save off, and include an LSB header size */
318 offset = ALIGN(offset, LSF_LSB_HEADER_ALIGN);
319 whdr->lsb_offset = offset;
320 offset += sizeof(*lhdr);
323 * Align, save off, and include the original (static) ucode
326 offset = ALIGN(offset, LSF_UCODE_DATA_ALIGN);
327 _img->ucode_off = lhdr->ucode_off = offset;
328 offset += _img->ucode_size;
331 * For falcons that use a boot loader (BL), we append a loader
332 * desc structure on the end of the ucode image and consider
333 * this the boot loader data. The host will then copy the loader
334 * desc args to this space within the WPR region (before locking
335 * down) and the HS bin will then copy them to DMEM 0 for the
338 lhdr->bl_code_size = ALIGN(desc->bootloader_size,
339 LSF_BL_CODE_SIZE_ALIGN);
340 lhdr->ucode_size = ALIGN(desc->app_resident_data_offset,
341 LSF_BL_CODE_SIZE_ALIGN) + lhdr->bl_code_size;
342 lhdr->data_size = ALIGN(desc->app_size, LSF_BL_CODE_SIZE_ALIGN) +
343 lhdr->bl_code_size - lhdr->ucode_size;
345 * Though the BL is located at 0th offset of the image, the VA
346 * is different to make sure that it doesn't collide the actual
349 lhdr->bl_imem_off = desc->bootloader_imem_offset;
350 lhdr->app_code_off = desc->app_start_offset +
351 desc->app_resident_code_offset;
352 lhdr->app_code_size = desc->app_resident_code_size;
353 lhdr->app_data_off = desc->app_start_offset +
354 desc->app_resident_data_offset;
355 lhdr->app_data_size = desc->app_resident_data_size;
357 lhdr->flags = func->lhdr_flags;
358 if (_img->falcon_id == acr->base.boot_falcon)
359 lhdr->flags |= LSF_FLAG_DMACTL_REQ_CTX;
361 /* Align and save off BL descriptor size */
362 lhdr->bl_data_size = ALIGN(func->bl_desc_size, LSF_BL_DATA_SIZE_ALIGN);
365 * Align, save off, and include the additional BL data
367 offset = ALIGN(offset, LSF_BL_DATA_ALIGN);
368 lhdr->bl_data_off = offset;
369 offset += lhdr->bl_data_size;
375 * acr_r352_ls_fill_headers - fill WPR and LSB headers of all managed images
378 acr_r352_ls_fill_headers(struct acr_r352 *acr, struct list_head *imgs)
380 struct ls_ucode_img_r352 *img;
385 /* Count the number of images to manage */
386 list_for_each(l, imgs)
390 * Start with an array of WPR headers at the base of the WPR.
391 * The expectation here is that the secure falcon will do a single DMA
392 * read of this array and cache it internally so it's ok to pack these.
393 * Also, we add 1 to the falcon count to indicate the end of the array.
395 offset = sizeof(img->wpr_header) * (count + 1);
398 * Walk the managed falcons, accounting for the LSB structs
399 * as well as the ucode images.
401 list_for_each_entry(img, imgs, base.node) {
402 offset = acr_r352_ls_img_fill_headers(acr, img, offset);
409 * acr_r352_ls_write_wpr - write the WPR blob contents
412 acr_r352_ls_write_wpr(struct acr_r352 *acr, struct list_head *imgs,
413 struct nvkm_gpuobj *wpr_blob, u64 wpr_addr)
415 struct ls_ucode_img *_img;
420 list_for_each_entry(_img, imgs, node) {
421 struct ls_ucode_img_r352 *img = ls_ucode_img_r352(_img);
422 const struct acr_r352_ls_func *ls_func =
423 acr->func->ls_func[_img->falcon_id];
424 u8 gdesc[ls_func->bl_desc_size];
426 nvkm_gpuobj_memcpy_to(wpr_blob, pos, &img->wpr_header,
427 sizeof(img->wpr_header));
429 nvkm_gpuobj_memcpy_to(wpr_blob, img->wpr_header.lsb_offset,
430 &img->lsb_header, sizeof(img->lsb_header));
432 /* Generate and write BL descriptor */
433 memset(gdesc, 0, ls_func->bl_desc_size);
434 ls_func->generate_bl_desc(&acr->base, _img, wpr_addr, gdesc);
436 nvkm_gpuobj_memcpy_to(wpr_blob, img->lsb_header.bl_data_off,
437 gdesc, ls_func->bl_desc_size);
440 nvkm_gpuobj_memcpy_to(wpr_blob, img->lsb_header.ucode_off,
441 _img->ucode_data, _img->ucode_size);
443 pos += sizeof(img->wpr_header);
446 nvkm_wo32(wpr_blob, pos, NVKM_SECBOOT_FALCON_INVALID);
453 /* Both size and address of WPR need to be 256K-aligned */
454 #define WPR_ALIGNMENT 0x40000
456 * acr_r352_prepare_ls_blob() - prepare the LS blob
458 * For each securely managed falcon, load the FW, signatures and bootloaders and
459 * prepare a ucode blob. Then, compute the offsets in the WPR region for each
460 * blob, and finally write the headers and ucode blobs into a GPU object that
461 * will be copied into the WPR region by the HS firmware.
464 acr_r352_prepare_ls_blob(struct acr_r352 *acr, struct nvkm_secboot *sb)
466 const struct nvkm_subdev *subdev = acr->base.subdev;
467 struct list_head imgs;
468 struct ls_ucode_img *img, *t;
469 unsigned long managed_falcons = acr->base.managed_falcons;
470 u64 wpr_addr = sb->wpr_addr;
471 u32 wpr_size = sb->wpr_size;
472 int managed_count = 0;
473 u32 image_wpr_size, ls_blob_size;
477 INIT_LIST_HEAD(&imgs);
479 /* Load all LS blobs */
480 for_each_set_bit(falcon_id, &managed_falcons, NVKM_SECBOOT_FALCON_END) {
481 struct ls_ucode_img *img;
483 img = acr->func->ls_ucode_img_load(acr, sb, falcon_id);
485 if (acr->base.optional_falcons & BIT(falcon_id)) {
486 managed_falcons &= ~BIT(falcon_id);
487 nvkm_info(subdev, "skipping %s falcon...\n",
488 nvkm_secboot_falcon_name[falcon_id]);
495 list_add_tail(&img->node, &imgs);
499 /* Commit the actual list of falcons we will manage from now on */
500 acr->base.managed_falcons = managed_falcons;
503 * If the boot falcon has a firmare, let it manage the bootstrap of other
506 if (acr->func->ls_func[acr->base.boot_falcon] &&
507 (managed_falcons & BIT(acr->base.boot_falcon))) {
508 for_each_set_bit(falcon_id, &managed_falcons,
509 NVKM_SECBOOT_FALCON_END) {
510 if (falcon_id == acr->base.boot_falcon)
513 acr->lazy_bootstrap |= BIT(falcon_id);
518 * Fill the WPR and LSF headers with the right offsets and compute
521 image_wpr_size = acr->func->ls_fill_headers(acr, &imgs);
522 image_wpr_size = ALIGN(image_wpr_size, WPR_ALIGNMENT);
524 ls_blob_size = image_wpr_size;
527 * If we need a shadow area, allocate twice the size and use the
530 if (wpr_size == 0 && acr->func->shadow_blob)
533 /* Allocate GPU object that will contain the WPR region */
534 ret = nvkm_gpuobj_new(subdev->device, ls_blob_size, WPR_ALIGNMENT,
535 false, NULL, &acr->ls_blob);
539 nvkm_debug(subdev, "%d managed LS falcons, WPR size is %d bytes\n",
540 managed_count, image_wpr_size);
542 /* If WPR address and size are not fixed, set them to fit the LS blob */
544 wpr_addr = acr->ls_blob->addr;
545 if (acr->func->shadow_blob)
546 wpr_addr += acr->ls_blob->size / 2;
548 wpr_size = image_wpr_size;
550 * But if the WPR region is set by the bootloader, it is illegal for
551 * the HS blob to be larger than this region.
553 } else if (image_wpr_size > wpr_size) {
554 nvkm_error(subdev, "WPR region too small for FW blob!\n");
555 nvkm_error(subdev, "required: %dB\n", image_wpr_size);
556 nvkm_error(subdev, "available: %dB\n", wpr_size);
562 ret = acr->func->ls_write_wpr(acr, &imgs, acr->ls_blob, wpr_addr);
564 nvkm_gpuobj_del(&acr->ls_blob);
567 list_for_each_entry_safe(img, t, &imgs, node) {
568 kfree(img->ucode_data);
580 acr_r352_fixup_hs_desc(struct acr_r352 *acr, struct nvkm_secboot *sb,
583 struct hsflcn_acr_desc *desc = _desc;
584 struct nvkm_gpuobj *ls_blob = acr->ls_blob;
586 /* WPR region information if WPR is not fixed */
587 if (sb->wpr_size == 0) {
588 u64 wpr_start = ls_blob->addr;
589 u64 wpr_end = wpr_start + ls_blob->size;
591 desc->wpr_region_id = 1;
592 desc->regions.no_regions = 2;
593 desc->regions.region_props[0].start_addr = wpr_start >> 8;
594 desc->regions.region_props[0].end_addr = wpr_end >> 8;
595 desc->regions.region_props[0].region_id = 1;
596 desc->regions.region_props[0].read_mask = 0xf;
597 desc->regions.region_props[0].write_mask = 0xc;
598 desc->regions.region_props[0].client_mask = 0x2;
600 desc->ucode_blob_base = ls_blob->addr;
601 desc->ucode_blob_size = ls_blob->size;
606 acr_r352_generate_hs_bl_desc(const struct hsf_load_header *hdr, void *_bl_desc,
609 struct acr_r352_flcn_bl_desc *bl_desc = _bl_desc;
610 u64 addr_code, addr_data;
612 addr_code = offset >> 8;
613 addr_data = (offset + hdr->data_dma_base) >> 8;
615 bl_desc->ctx_dma = FALCON_DMAIDX_VIRT;
616 bl_desc->code_dma_base = lower_32_bits(addr_code);
617 bl_desc->non_sec_code_off = hdr->non_sec_code_off;
618 bl_desc->non_sec_code_size = hdr->non_sec_code_size;
619 bl_desc->sec_code_off = hsf_load_header_app_off(hdr, 0);
620 bl_desc->sec_code_size = hsf_load_header_app_size(hdr, 0);
621 bl_desc->code_entry_point = 0;
622 bl_desc->data_dma_base = lower_32_bits(addr_data);
623 bl_desc->data_size = hdr->data_size;
627 * acr_r352_prepare_hs_blob - load and prepare a HS blob and BL descriptor
629 * @sb secure boot instance to prepare for
630 * @fw name of the HS firmware to load
631 * @blob pointer to gpuobj that will be allocated to receive the HS FW payload
632 * @bl_desc pointer to the BL descriptor to write for this firmware
633 * @patch whether we should patch the HS descriptor (only for HS loaders)
636 acr_r352_prepare_hs_blob(struct acr_r352 *acr, struct nvkm_secboot *sb,
637 const char *fw, struct nvkm_gpuobj **blob,
638 struct hsf_load_header *load_header, bool patch)
640 struct nvkm_subdev *subdev = &sb->subdev;
642 struct fw_bin_header *hsbin_hdr;
643 struct hsf_fw_header *fw_hdr;
644 struct hsf_load_header *load_hdr;
648 acr_image = hs_ucode_load_blob(subdev, sb->boot_falcon, fw);
649 if (IS_ERR(acr_image))
650 return PTR_ERR(acr_image);
652 hsbin_hdr = acr_image;
653 fw_hdr = acr_image + hsbin_hdr->header_offset;
654 load_hdr = acr_image + fw_hdr->hdr_offset;
655 acr_data = acr_image + hsbin_hdr->data_offset;
657 /* Patch descriptor with WPR information? */
659 struct hsflcn_acr_desc *desc;
661 desc = acr_data + load_hdr->data_dma_base;
662 acr->func->fixup_hs_desc(acr, sb, desc);
665 if (load_hdr->num_apps > ACR_R352_MAX_APPS) {
666 nvkm_error(subdev, "more apps (%d) than supported (%d)!",
667 load_hdr->num_apps, ACR_R352_MAX_APPS);
671 memcpy(load_header, load_hdr, sizeof(*load_header) +
672 (sizeof(load_hdr->apps[0]) * 2 * load_hdr->num_apps));
674 /* Create ACR blob and copy HS data to it */
675 ret = nvkm_gpuobj_new(subdev->device, ALIGN(hsbin_hdr->data_size, 256),
676 0x1000, false, NULL, blob);
681 nvkm_gpuobj_memcpy_to(*blob, 0, acr_data, hsbin_hdr->data_size);
691 * acr_r352_load_blobs - load blobs common to all ACR V1 versions.
693 * This includes the LS blob, HS ucode loading blob, and HS bootloader.
695 * The HS ucode unload blob is only used on dGPU if the WPR region is variable.
698 acr_r352_load_blobs(struct acr_r352 *acr, struct nvkm_secboot *sb)
700 struct nvkm_subdev *subdev = &sb->subdev;
703 /* Firmware already loaded? */
704 if (acr->firmware_ok)
707 /* Load and prepare the managed falcon's firmwares */
708 ret = acr_r352_prepare_ls_blob(acr, sb);
712 /* Load the HS firmware that will load the LS firmwares */
713 if (!acr->load_blob) {
714 ret = acr_r352_prepare_hs_blob(acr, sb, "acr/ucode_load",
716 &acr->load_bl_header, true);
721 /* If the ACR region is dynamically programmed, we need an unload FW */
722 if (sb->wpr_size == 0) {
723 ret = acr_r352_prepare_hs_blob(acr, sb, "acr/ucode_unload",
725 &acr->unload_bl_header, false);
730 /* Load the HS firmware bootloader */
731 if (!acr->hsbl_blob) {
732 acr->hsbl_blob = nvkm_acr_load_firmware(subdev, "acr/bl", 0);
733 if (IS_ERR(acr->hsbl_blob)) {
734 ret = PTR_ERR(acr->hsbl_blob);
735 acr->hsbl_blob = NULL;
739 if (acr->base.boot_falcon != NVKM_SECBOOT_FALCON_PMU) {
740 acr->hsbl_unload_blob = nvkm_acr_load_firmware(subdev,
742 if (IS_ERR(acr->hsbl_unload_blob)) {
743 ret = PTR_ERR(acr->hsbl_unload_blob);
744 acr->hsbl_unload_blob = NULL;
748 acr->hsbl_unload_blob = acr->hsbl_blob;
752 acr->firmware_ok = true;
753 nvkm_debug(&sb->subdev, "LS blob successfully created\n");
759 * acr_r352_load() - prepare HS falcon to run the specified blob, mapped.
761 * Returns the start address to use, or a negative error value.
764 acr_r352_load(struct nvkm_acr *_acr, struct nvkm_falcon *falcon,
765 struct nvkm_gpuobj *blob, u64 offset)
767 struct acr_r352 *acr = acr_r352(_acr);
768 const u32 bl_desc_size = acr->func->hs_bl_desc_size;
769 const struct hsf_load_header *load_hdr;
770 struct fw_bin_header *bl_hdr;
771 struct fw_bl_desc *hsbl_desc;
772 void *bl, *blob_data, *hsbl_code, *hsbl_data;
774 u8 bl_desc[bl_desc_size];
776 /* Find the bootloader descriptor for our blob and copy it */
777 if (blob == acr->load_blob) {
778 load_hdr = &acr->load_bl_header;
780 } else if (blob == acr->unload_blob) {
781 load_hdr = &acr->unload_bl_header;
782 bl = acr->hsbl_unload_blob;
784 nvkm_error(_acr->subdev, "invalid secure boot blob!\n");
789 hsbl_desc = bl + bl_hdr->header_offset;
790 blob_data = bl + bl_hdr->data_offset;
791 hsbl_code = blob_data + hsbl_desc->code_off;
792 hsbl_data = blob_data + hsbl_desc->data_off;
793 code_size = ALIGN(hsbl_desc->code_size, 256);
796 * Copy HS bootloader data
798 nvkm_falcon_load_dmem(falcon, hsbl_data, 0x0, hsbl_desc->data_size, 0);
800 /* Copy HS bootloader code to end of IMEM */
801 nvkm_falcon_load_imem(falcon, hsbl_code, falcon->code.limit - code_size,
802 code_size, hsbl_desc->start_tag, 0, false);
804 /* Generate the BL header */
805 memset(bl_desc, 0, bl_desc_size);
806 acr->func->generate_hs_bl_desc(load_hdr, bl_desc, offset);
809 * Copy HS BL header where the HS descriptor expects it to be
811 nvkm_falcon_load_dmem(falcon, bl_desc, hsbl_desc->dmem_load_off,
814 return hsbl_desc->start_tag << 8;
818 acr_r352_shutdown(struct acr_r352 *acr, struct nvkm_secboot *sb)
820 struct nvkm_subdev *subdev = &sb->subdev;
823 /* Run the unload blob to unprotect the WPR region */
824 if (acr->unload_blob && sb->wpr_set) {
827 nvkm_debug(subdev, "running HS unload blob\n");
828 ret = sb->func->run_blob(sb, acr->unload_blob, sb->halt_falcon);
832 * Unload blob will return this error code - it is not an error
833 * and the expected behavior on RM as well
835 if (ret && ret != 0x1d) {
836 nvkm_error(subdev, "HS unload failed, ret 0x%08x", ret);
839 nvkm_debug(subdev, "HS unload blob completed\n");
842 for (i = 0; i < NVKM_SECBOOT_FALCON_END; i++)
843 acr->falcon_state[i] = NON_SECURE;
851 * Check if the WPR region has been indeed set by the ACR firmware, and
852 * matches where it should be.
855 acr_r352_wpr_is_set(const struct acr_r352 *acr, const struct nvkm_secboot *sb)
857 const struct nvkm_subdev *subdev = &sb->subdev;
858 const struct nvkm_device *device = subdev->device;
860 u64 wpr_range_lo, wpr_range_hi;
862 nvkm_wr32(device, 0x100cd4, 0x2);
863 wpr_lo = (nvkm_rd32(device, 0x100cd4) & ~0xff);
865 nvkm_wr32(device, 0x100cd4, 0x3);
866 wpr_hi = (nvkm_rd32(device, 0x100cd4) & ~0xff);
869 if (sb->wpr_size != 0) {
870 wpr_range_lo = sb->wpr_addr;
871 wpr_range_hi = wpr_range_lo + sb->wpr_size;
873 wpr_range_lo = acr->ls_blob->addr;
874 wpr_range_hi = wpr_range_lo + acr->ls_blob->size;
877 return (wpr_lo >= wpr_range_lo && wpr_lo < wpr_range_hi &&
878 wpr_hi > wpr_range_lo && wpr_hi <= wpr_range_hi);
882 acr_r352_bootstrap(struct acr_r352 *acr, struct nvkm_secboot *sb)
884 const struct nvkm_subdev *subdev = &sb->subdev;
885 unsigned long managed_falcons = acr->base.managed_falcons;
892 /* Make sure all blobs are ready */
893 ret = acr_r352_load_blobs(acr, sb);
897 nvkm_debug(subdev, "running HS load blob\n");
898 ret = sb->func->run_blob(sb, acr->load_blob, sb->boot_falcon);
899 /* clear halt interrupt */
900 nvkm_falcon_clear_interrupt(sb->boot_falcon, 0x10);
901 sb->wpr_set = acr_r352_wpr_is_set(acr, sb);
904 } else if (ret > 0) {
905 nvkm_error(subdev, "HS load failed, ret 0x%08x", ret);
908 nvkm_debug(subdev, "HS load blob completed\n");
909 /* WPR must be set at this point */
911 nvkm_error(subdev, "ACR blob completed but WPR not set!\n");
915 /* Run LS firmwares post_run hooks */
916 for_each_set_bit(falcon_id, &managed_falcons, NVKM_SECBOOT_FALCON_END) {
917 const struct acr_r352_ls_func *func =
918 acr->func->ls_func[falcon_id];
920 if (func->post_run) {
921 ret = func->post_run(&acr->base, sb);
931 * acr_r352_reset_nopmu - dummy reset method when no PMU firmware is loaded
933 * Reset is done by re-executing secure boot from scratch, with lazy bootstrap
934 * disabled. This has the effect of making all managed falcons ready-to-run.
937 acr_r352_reset_nopmu(struct acr_r352 *acr, struct nvkm_secboot *sb,
938 unsigned long falcon_mask)
944 * Perform secure boot each time we are called on FECS. Since only FECS
945 * and GPCCS are managed and started together, this ought to be safe.
947 if (!(falcon_mask & BIT(NVKM_SECBOOT_FALCON_FECS)))
950 ret = acr_r352_shutdown(acr, sb);
954 ret = acr_r352_bootstrap(acr, sb);
959 for_each_set_bit(falcon, &falcon_mask, NVKM_SECBOOT_FALCON_END) {
960 acr->falcon_state[falcon] = RESET;
966 * acr_r352_reset() - execute secure boot from the prepared state
968 * Load the HS bootloader and ask the falcon to run it. This will in turn
969 * load the HS firmware and run it, so once the falcon stops all the managed
970 * falcons should have their LS firmware loaded and be ready to run.
973 acr_r352_reset(struct nvkm_acr *_acr, struct nvkm_secboot *sb,
974 unsigned long falcon_mask)
976 struct acr_r352 *acr = acr_r352(_acr);
977 struct nvkm_msgqueue *queue;
979 bool wpr_already_set = sb->wpr_set;
982 /* Make sure secure boot is performed */
983 ret = acr_r352_bootstrap(acr, sb);
987 /* No PMU interface? */
988 if (!nvkm_secboot_is_managed(sb, _acr->boot_falcon)) {
989 /* Redo secure boot entirely if it was already done */
991 return acr_r352_reset_nopmu(acr, sb, falcon_mask);
992 /* Else return the result of the initial invokation */
997 switch (_acr->boot_falcon) {
998 case NVKM_SECBOOT_FALCON_PMU:
999 queue = sb->subdev.device->pmu->queue;
1001 case NVKM_SECBOOT_FALCON_SEC2:
1002 queue = sb->subdev.device->sec2->queue;
1008 /* Otherwise just ask the LS firmware to reset the falcon */
1009 for_each_set_bit(falcon, &falcon_mask, NVKM_SECBOOT_FALCON_END)
1010 nvkm_debug(&sb->subdev, "resetting %s falcon\n",
1011 nvkm_secboot_falcon_name[falcon]);
1012 ret = nvkm_msgqueue_acr_boot_falcons(queue, falcon_mask);
1014 nvkm_error(&sb->subdev, "error during falcon reset: %d\n", ret);
1017 nvkm_debug(&sb->subdev, "falcon reset done\n");
1023 acr_r352_fini(struct nvkm_acr *_acr, struct nvkm_secboot *sb, bool suspend)
1025 struct acr_r352 *acr = acr_r352(_acr);
1027 return acr_r352_shutdown(acr, sb);
1031 acr_r352_dtor(struct nvkm_acr *_acr)
1033 struct acr_r352 *acr = acr_r352(_acr);
1035 nvkm_gpuobj_del(&acr->unload_blob);
1037 if (_acr->boot_falcon != NVKM_SECBOOT_FALCON_PMU)
1038 kfree(acr->hsbl_unload_blob);
1039 kfree(acr->hsbl_blob);
1040 nvkm_gpuobj_del(&acr->load_blob);
1041 nvkm_gpuobj_del(&acr->ls_blob);
1046 const struct acr_r352_ls_func
1047 acr_r352_ls_fecs_func = {
1048 .load = acr_ls_ucode_load_fecs,
1049 .generate_bl_desc = acr_r352_generate_flcn_bl_desc,
1050 .bl_desc_size = sizeof(struct acr_r352_flcn_bl_desc),
1053 const struct acr_r352_ls_func
1054 acr_r352_ls_gpccs_func = {
1055 .load = acr_ls_ucode_load_gpccs,
1056 .generate_bl_desc = acr_r352_generate_flcn_bl_desc,
1057 .bl_desc_size = sizeof(struct acr_r352_flcn_bl_desc),
1058 /* GPCCS will be loaded using PRI */
1059 .lhdr_flags = LSF_FLAG_FORCE_PRIV_LOAD,
1065 * struct acr_r352_pmu_bl_desc - PMU DMEM bootloader descriptor
1066 * @dma_idx: DMA context to be used by BL while loading code/data
1067 * @code_dma_base: 256B-aligned Physical FB Address where code is located
1068 * @total_code_size: total size of the code part in the ucode
1069 * @code_size_to_load: size of the code part to load in PMU IMEM.
1070 * @code_entry_point: entry point in the code.
1071 * @data_dma_base: Physical FB address where data part of ucode is located
1072 * @data_size: Total size of the data portion.
1073 * @overlay_dma_base: Physical Fb address for resident code present in ucode
1074 * @argc: Total number of args
1075 * @argv: offset where args are copied into PMU's DMEM.
1077 * Structure used by the PMU bootloader to load the rest of the code
1079 struct acr_r352_pmu_bl_desc {
1082 u32 code_size_total;
1083 u32 code_size_to_load;
1084 u32 code_entry_point;
1087 u32 overlay_dma_base;
1092 u16 overlay_dma_base1;
1096 * acr_r352_generate_pmu_bl_desc() - populate a DMEM BL descriptor for PMU LS image
1100 acr_r352_generate_pmu_bl_desc(const struct nvkm_acr *acr,
1101 const struct ls_ucode_img *img, u64 wpr_addr,
1104 const struct ls_ucode_img_desc *pdesc = &img->ucode_desc;
1105 const struct nvkm_pmu *pmu = acr->subdev->device->pmu;
1106 struct acr_r352_pmu_bl_desc *desc = _desc;
1112 base = wpr_addr + img->ucode_off + pdesc->app_start_offset;
1113 addr_code = (base + pdesc->app_resident_code_offset) >> 8;
1114 addr_data = (base + pdesc->app_resident_data_offset) >> 8;
1115 addr_args = pmu->falcon->data.limit;
1116 addr_args -= NVKM_MSGQUEUE_CMDLINE_SIZE;
1118 desc->dma_idx = FALCON_DMAIDX_UCODE;
1119 desc->code_dma_base = lower_32_bits(addr_code);
1120 desc->code_dma_base1 = upper_32_bits(addr_code);
1121 desc->code_size_total = pdesc->app_size;
1122 desc->code_size_to_load = pdesc->app_resident_code_size;
1123 desc->code_entry_point = pdesc->app_imem_entry;
1124 desc->data_dma_base = lower_32_bits(addr_data);
1125 desc->data_dma_base1 = upper_32_bits(addr_data);
1126 desc->data_size = pdesc->app_resident_data_size;
1127 desc->overlay_dma_base = lower_32_bits(addr_code);
1128 desc->overlay_dma_base1 = upper_32_bits(addr_code);
1130 desc->argv = addr_args;
1133 static const struct acr_r352_ls_func
1134 acr_r352_ls_pmu_func = {
1135 .load = acr_ls_ucode_load_pmu,
1136 .generate_bl_desc = acr_r352_generate_pmu_bl_desc,
1137 .bl_desc_size = sizeof(struct acr_r352_pmu_bl_desc),
1138 .post_run = acr_ls_pmu_post_run,
1141 const struct acr_r352_func
1143 .fixup_hs_desc = acr_r352_fixup_hs_desc,
1144 .generate_hs_bl_desc = acr_r352_generate_hs_bl_desc,
1145 .hs_bl_desc_size = sizeof(struct acr_r352_flcn_bl_desc),
1146 .ls_ucode_img_load = acr_r352_ls_ucode_img_load,
1147 .ls_fill_headers = acr_r352_ls_fill_headers,
1148 .ls_write_wpr = acr_r352_ls_write_wpr,
1150 [NVKM_SECBOOT_FALCON_FECS] = &acr_r352_ls_fecs_func,
1151 [NVKM_SECBOOT_FALCON_GPCCS] = &acr_r352_ls_gpccs_func,
1152 [NVKM_SECBOOT_FALCON_PMU] = &acr_r352_ls_pmu_func,
1156 static const struct nvkm_acr_func
1157 acr_r352_base_func = {
1158 .dtor = acr_r352_dtor,
1159 .fini = acr_r352_fini,
1160 .load = acr_r352_load,
1161 .reset = acr_r352_reset,
1165 acr_r352_new_(const struct acr_r352_func *func,
1166 enum nvkm_secboot_falcon boot_falcon,
1167 unsigned long managed_falcons)
1169 struct acr_r352 *acr;
1172 /* Check that all requested falcons are supported */
1173 for_each_set_bit(i, &managed_falcons, NVKM_SECBOOT_FALCON_END) {
1174 if (!func->ls_func[i])
1175 return ERR_PTR(-ENOTSUPP);
1178 acr = kzalloc(sizeof(*acr), GFP_KERNEL);
1180 return ERR_PTR(-ENOMEM);
1182 acr->base.boot_falcon = boot_falcon;
1183 acr->base.managed_falcons = managed_falcons;
1184 acr->base.func = &acr_r352_base_func;
1191 acr_r352_new(unsigned long managed_falcons)
1193 return acr_r352_new_(&acr_r352_func, NVKM_SECBOOT_FALCON_PMU,