xref: /optee_os/core/drivers/crypto/caam/hal/common/hal_cfg.c (revision 75be62aebb3981aa5e6c6b7159228af77d46374f)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2017-2019, 2021, 2024 NXP
4  *
5  * Brief   CAAM Configuration.
6  */
7 #include <caam_common.h>
8 #include <caam_hal_cfg.h>
9 #include <caam_hal_ctrl.h>
10 #include <caam_hal_jr.h>
11 #include <caam_jr.h>
12 #include <config.h>
13 #include <kernel/boot.h>
14 #include <kernel/dt.h>
15 #include <mm/core_memprot.h>
16 #include <registers/jr_regs.h>
17 
caam_hal_cfg_get_conf(struct caam_jrcfg * jrcfg)18 enum caam_status caam_hal_cfg_get_conf(struct caam_jrcfg *jrcfg)
19 {
20 	enum caam_status retstatus = CAAM_FAILURE;
21 	vaddr_t ctrl_base = 0;
22 	void *fdt = NULL;
23 
24 	fdt = get_dt();
25 
26 	/*
27 	 * First get the CAAM Controller base address from the DTB,
28 	 * if DTB present and if the CAAM Controller defined in it.
29 	 */
30 	if (fdt)
31 		caam_hal_cfg_get_ctrl_dt(fdt, &ctrl_base);
32 
33 	if (!ctrl_base) {
34 		ctrl_base = (vaddr_t)core_mmu_add_mapping(MEM_AREA_IO_SEC,
35 							  CAAM_BASE, CAAM_SIZE);
36 		if (!ctrl_base) {
37 			EMSG("Unable to map CAAM Registers");
38 			goto exit_get_conf;
39 		}
40 	}
41 
42 	jrcfg->base = ctrl_base;
43 
44 	/*
45 	 * Next get the Job Ring reserved for the Secure environment
46 	 * into the DTB. If nothing reserved use the default hard coded
47 	 * value.
48 	 */
49 	if (fdt)
50 		caam_hal_cfg_get_jobring_dt(fdt, jrcfg);
51 
52 	if (!jrcfg->offset) {
53 		jrcfg->offset = (CFG_JR_INDEX + 1) * JRX_BLOCK_SIZE;
54 		jrcfg->it_num = CFG_JR_INT;
55 
56 		if (IS_ENABLED(CFG_NXP_CAAM_RUNTIME_JR) &&
57 		    !is_embedded_dt(fdt)) {
58 			if (fdt) {
59 				/* Ensure Secure Job Ring is secure in DTB */
60 				caam_hal_cfg_disable_jobring_dt(fdt, jrcfg);
61 			}
62 		}
63 	}
64 
65 	jrcfg->nb_jobs = NB_JOBS_QUEUE;
66 
67 	retstatus = CAAM_NO_ERROR;
68 
69 	if (IS_ENABLED(CFG_NXP_CAAM_RUNTIME_JR))
70 		caam_hal_jr_prepare_backup(jrcfg->base, jrcfg->offset);
71 
72 	if (IS_ENABLED(CFG_MX8M))
73 		caam_hal_cfg_hab_jr_mgmt(jrcfg);
74 
75 exit_get_conf:
76 	HAL_TRACE("HAL CFG Get CAAM config ret (0x%x)\n", retstatus);
77 	return retstatus;
78 }
79 
caam_hal_cfg_setup_nsjobring(struct caam_jrcfg * jrcfg)80 void __weak caam_hal_cfg_setup_nsjobring(struct caam_jrcfg *jrcfg)
81 {
82 	enum caam_status status = CAAM_FAILURE;
83 	paddr_t jr_offset = 0;
84 	uint8_t jrnum = 0;
85 
86 	for (jrnum = caam_hal_ctrl_jrnum(jrcfg->base); jrnum; jrnum--) {
87 		jr_offset = jrnum * JRX_BLOCK_SIZE;
88 
89 		/*
90 		 * Skip configuration for the JR used by the HAB
91 		 */
92 		if (IS_ENABLED(CFG_MX8M)) {
93 			if (caam_hal_cfg_is_hab_jr(jr_offset))
94 				continue;
95 		}
96 
97 #ifdef CFG_NXP_CAAM_RUNTIME_JR
98 		/*
99 		 * When the Cryptographic driver is enabled, keep the
100 		 * Secure Job Ring don't release it.
101 		 * But save the configuration to restore it when
102 		 * device reset after suspend.
103 		 */
104 		if (jr_offset == jrcfg->offset) {
105 			caam_hal_jr_prepare_backup(jrcfg->base, jr_offset);
106 			continue;
107 		}
108 #endif
109 		status = caam_hal_jr_setowner(jrcfg->base, jr_offset,
110 					      JROWN_ARM_NS);
111 		if (status == CAAM_NO_ERROR)
112 			caam_hal_jr_prepare_backup(jrcfg->base, jr_offset);
113 	}
114 }
115