1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 
9 /************* Include Files ****************/
10 #include "cc_pal_init.h"
11 #include "cc_pal_dma_plat.h"
12 #include "cc_pal_log.h"
13 #include "dx_reg_base_host.h"
14 #include "cc_pal_mutex.h"
15 #include "cc_pal_mem.h"
16 #include "cc_pal_abort.h"
17 #include "cc_pal_pm.h"
18 
19 extern CC_PalMutex CCSymCryptoMutex;
20 extern CC_PalMutex CCAsymCryptoMutex;
21 extern CC_PalMutex CCRndCryptoMutex;
22 extern CC_PalMutex *pCCRndCryptoMutex;
23 
24 #ifndef CC_IOT
25 extern CC_PalMutex CCGenVecMutex;
26 extern CC_PalMutex *pCCGenVecMutex;
27 #endif
28 
29 #ifdef CC_IOT
30 extern CC_PalMutex CCApbFilteringRegMutex;
31 #endif
32 
33 #ifdef DX_PLAT_ZYNQ7000
34 /* Zynq EVBs have 1GB and we reserve the memory at offset 768M */
35 #define PAL_WORKSPACE_MEM_BASE_ADDR     0x34000000
36 #elif defined PLAT_VIRTEX5
37 /* Virtex5 platforms (PPC) have 512MB and we reserve the memory at offset 256M */
38 #define PAL_WORKSPACE_MEM_BASE_ADDR     0x10000000
39 #elif defined DX_PLAT_JUNO
40 /* Juno platforms (AARCH64)  */
41 #define PAL_WORKSPACE_MEM_BASE_ADDR     0x8A0000000
42 #endif
43 #define PAL_WORKSPACE_MEM_SIZE      0x1000000
44 
45 /**
46  * @brief   PAL layer entry point.
47  *          The function initializes customer platform sub components,
48  *           such as memory mapping used later by CRYS to get physical contiguous memory.
49  *
50  *
51  * @return Virtual start address of contiguous memory
52  */
CC_PalInit(void)53 int CC_PalInit(void)
54 {
55     int rc = 0;
56 
57     CC_PalLogInit();
58 
59     rc = CC_PalDmaInit(PAL_WORKSPACE_MEM_SIZE, PAL_WORKSPACE_MEM_BASE_ADDR);
60     if (rc != 0) {
61             return 1;
62     }
63 
64 #ifdef CC_IOT
65     /* Initialize power management module */
66     CC_PalPowerSaveModeInit();
67 #endif
68 
69     /* Initialize mutex that protects shared memory and crypto access */
70     rc = CC_PalMutexCreate(&CCSymCryptoMutex);
71     if (rc != 0) {
72             CC_PalAbort("Fail to create SYM mutex\n");
73     }
74     /* Initialize mutex that protects shared memory and crypto access */
75     rc = CC_PalMutexCreate(&CCAsymCryptoMutex);
76     if (rc != 0) {
77             CC_PalAbort("Fail to create ASYM mutex\n");
78     }
79     /* Initialize mutex that protects shared memory and crypto access */
80     rc = CC_PalMutexCreate(&CCRndCryptoMutex);
81     if (rc != 0) {
82             CC_PalAbort("Fail to create RND mutex\n");
83     }
84     pCCRndCryptoMutex = &CCRndCryptoMutex;
85 
86 #ifndef CC_IOT
87     pCCGenVecMutex = &CCRndCryptoMutex;
88 #endif
89 
90 #ifdef CC_IOT
91     /* Initialize mutex that protects APBC access */
92     rc = CC_PalMutexCreate(&CCApbFilteringRegMutex);
93     if (rc != 0) {
94             CC_PalAbort("Fail to create APBC mutex\n");
95     }
96 #endif
97 
98     return 0;
99 }
100 
101 
102 /**
103  * @brief   PAL layer entry point.
104  *          The function initializes customer platform sub components,
105  *           such as memory mapping used later by CRYS to get physical contiguous memory.
106  *
107  *
108  * @return None
109  */
CC_PalTerminate(void)110 void CC_PalTerminate(void)
111 {
112     CCError_t err = 0;
113 
114     CC_PalDmaTerminate();
115 
116     err = CC_PalMutexDestroy(&CCSymCryptoMutex);
117     if (err != 0){
118             CC_PAL_LOG_DEBUG("failed to destroy mutex CCSymCryptoMutex\n");
119     }
120     CC_PalMemSetZero(&CCSymCryptoMutex, sizeof(CC_PalMutex));
121 
122     err = CC_PalMutexDestroy(&CCAsymCryptoMutex);
123     if (err != 0){
124             CC_PAL_LOG_DEBUG("failed to destroy mutex CCAsymCryptoMutex\n");
125     }
126     CC_PalMemSetZero(&CCAsymCryptoMutex, sizeof(CC_PalMutex));
127 
128     err = CC_PalMutexDestroy(&CCRndCryptoMutex);
129     if (err != 0){
130             CC_PAL_LOG_DEBUG("failed to destroy mutex CCRndCryptoMutex\n");
131     }
132     CC_PalMemSetZero(&CCRndCryptoMutex, sizeof(CC_PalMutex));
133 
134 #ifdef CC_IOT
135     err = CC_PalMutexDestroy(&CCApbFilteringRegMutex);
136     if (err != 0){
137             CC_PAL_LOG_DEBUG("failed to destroy mutex CCApbFilteringRegMutex\n");
138     }
139     CC_PalMemSetZero(&CCApbFilteringRegMutex, sizeof(CC_PalMutex));
140 #endif
141 
142 }
143 
144