1 /*
2  * Copyright (c) 2016-2021 Arm Limited. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * \file tgu_armv8_m_drv.h
19  * \brief Generic driver for ARM TCM Gating Unit(TGU)
20  */
21 
22 #ifndef __TGU_ARMV8_M_DRV_H__
23 #define __TGU_ARMV8_M_DRV_H__
24 
25 #include <stdint.h>
26 #include <stdbool.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 enum tgu_armv8_m_error_t {
33     TGU_ERR_NONE,          /*!< No error */
34     TGU_INVALID_ARG,
35     TGU_NOT_INIT,
36     TGU_INTERN_ERR_NOT_IN_RANGE,
37     TGU_INTERN_ERR_NOT_ALIGNED,
38     TGU_INTERN_ERR_INVALID_RANGE,
39     TGU_INTERN_ERR_BLK_IDX_TOO_HIGH,
40     TGU_ERR_RANGE_SEC_ATTR_NON_COMPATIBLE
41 };
42 
43 /* Security attribute used in various place of the API */
44 enum tgu_armv8_m_sec_attr_t {
45     TGU_SEC_ATTR_SECURE,     /*!< Secure attribute */
46     TGU_SEC_ATTR_NONSECURE,  /*!< Non-secure attribute */
47     /*!< Used when getting the configuration of a memory range and some blocks
48      *   are secure whereas some other are non secure
49      */
50     TGU_SEC_ATTR_MIXED,
51 };
52 
53 /* Description of a memory range controlled by the TGU */
54 struct tgu_armv8_m_mem_range_t {
55     const uint32_t base;   /*!< Base address (included in the range) */
56     const uint32_t limit;  /*!< Limit address (included in the range) */
57     const uint32_t range_offset;
58     const enum tgu_armv8_m_sec_attr_t attr; /*!< Optional security attribute
59                                             *   needed to be matched when
60                                             *   accessing this range.
61                                             *   For example, the non-secure
62                                             *   alias of a memory region can not
63                                             *   be accessed using secure access,
64                                             *   and configuring the TGU to
65                                             *   secure using that range will not
66                                             *   be permitted by the driver.
67                                             */
68 };
69 
70 /* ARM TGU device configuration structure */
71 struct tgu_armv8_m_dev_cfg_t {
72     const uint32_t base;  /*!< TGU CTRL base address */
73 };
74 
75 /* ARM TGU device data structure */
76 struct tgu_armv8_m_dev_data_t {
77     /*!< Array of pointers to memory ranges controlled by the TGU */
78     const struct tgu_armv8_m_mem_range_t** range_list;
79     uint8_t nbr_of_ranges;  /*!< Number of memory ranges in the list */
80     bool is_initialized;
81 
82 };
83 
84 /* ARM TGU device structure */
85 struct tgu_armv8_m_dev_t {
86     const struct tgu_armv8_m_dev_cfg_t* const cfg;  /*!< TGU configuration */
87     struct tgu_armv8_m_dev_data_t* const data;      /*!< TGU data */
88 };
89 
90 /* Initialize TGU */
91 enum tgu_armv8_m_error_t tgu_armv8_m_init(struct tgu_armv8_m_dev_t* dev,
92                                           const struct tgu_armv8_m_mem_range_t** range_list,
93                                           uint8_t nbr_of_ranges);
94 
95 /* Get the block size in bytes*/
96 enum tgu_armv8_m_error_t tgu_armv8_m_get_block_size(struct tgu_armv8_m_dev_t* dev,
97                                             uint32_t* blk_size);
98 
99 /* Get number of programmable blocks*/
100 enum tgu_armv8_m_error_t tgu_armv8_m_get_number_of_prog_blocks(struct tgu_armv8_m_dev_t* dev,
101                                                         uint32_t* num_blks);
102 
103 /* Set the security configuration of the region */
104 enum tgu_armv8_m_error_t tgu_armv8_m_config_region(struct tgu_armv8_m_dev_t* dev,
105                                            const uint32_t base,
106                                            const uint32_t limit,
107                                            enum tgu_armv8_m_sec_attr_t attr);
108 
109 /* Get the security configuration of the region */
110 enum tgu_armv8_m_error_t tgu_armv8_m_get_region_config(struct tgu_armv8_m_dev_t* dev,
111                                                uint32_t base,
112                                                uint32_t limit,
113                                                enum tgu_armv8_m_sec_attr_t* attr);
114 
115 /* Get the control value from xTGU_CTRL */
116 enum tgu_armv8_m_error_t tgu_armv8_m_get_ctrl(struct tgu_armv8_m_dev_t* dev,
117                                       uint32_t* ctrl_val);
118 
119 /* Set the control val in xTGU_CTRL */
120 enum tgu_armv8_m_error_t tgu_armv8_m_set_ctrl(struct tgu_armv8_m_dev_t* dev,
121                                       uint32_t tgu_ctrl);
122 
123 /* Check if TGU is present */
124 enum tgu_armv8_m_error_t tgu_armv8_m_is_tgu_present(struct tgu_armv8_m_dev_t* dev,
125                                                bool* tgu_present);
126 
127 #endif /* __TGU_ARMV8_M_DRV_H__ */
128