1 /*
2  * Copyright (c) 2016-2018 ARM Limited
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 #ifndef __DRIVER_MPC_H
17 #define __DRIVER_MPC_H
18 
19 #include "Driver_Common.h"
20 
21 /* API version */
22 #define ARM_MPC_API_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(1,0)
23 
24 /* Error code returned by the driver functions */
25 #define ARM_MPC_ERR_NOT_INIT      (ARM_DRIVER_ERROR_SPECIFIC - 1)  ///< MPC not initialized */
26 #define ARM_MPC_ERR_NOT_IN_RANGE  (ARM_DRIVER_ERROR_SPECIFIC - 2)  ///< Address does not belong to a range controlled by the MPC */
27 #define ARM_MPC_ERR_NOT_ALIGNED   (ARM_DRIVER_ERROR_SPECIFIC - 3)  ///< Address is not aligned on the block size of this MPC */
28 #define ARM_MPC_ERR_INVALID_RANGE (ARM_DRIVER_ERROR_SPECIFIC - 4)  ///< The given address range to configure is invalid
29 #define ARM_MPC_ERR_RANGE_SEC_ATTR_NON_COMPATIBLE (ARM_DRIVER_ERROR_SPECIFIC - 4)  ///< The given range cannot be accessed with the wanted security attributes */
30 #define ARM_MPC_ERR_UNSPECIFIED   (ARM_DRIVER_ERROR_SPECIFIC - 5)  ///< Unspecified error */
31 
32 /* Security attribute used in various place of the API */
33 typedef enum _ARM_MPC_SEC_ATTR {
34     ARM_MPC_ATTR_SECURE,     ///< Secure attribute
35     ARM_MPC_ATTR_NONSECURE,  ///< Non-secure attribute
36     /* Used when getting the configuration of a memory range and some blocks are
37      * secure whereas some other are non secure */
38     ARM_MPC_ATTR_MIXED,      ///< Mixed attribute
39 } ARM_MPC_SEC_ATTR;
40 
41 /* Function documentation */
42 /**
43   \fn          ARM_DRIVER_VERSION ARM_MPC_GetVersion (void)
44   \brief       Get driver version.
45   \return      \ref ARM_DRIVER_VERSION
46 
47   \fn          int32_t ARM_MPC_Initialize (void)
48   \brief       Initialize MPC Interface.
49   \return      Returns error code.
50 
51   \fn          int32_t ARM_MPC_Uninitialize (void)
52   \brief       De-initialize MPC Interface. The controlled memory region
53                should not be accessed after a call to this function, as
54                it is allowed to configure everything to be secure (to
55                prevent information leak for example).
56   \return      Returns error code.
57 
58   \fn          int32_t ARM_MPC_GetBlockSize (uint32_t* blk_size)
59   \brief       Get the block size of the MPC. All regions must be aligned
60                on this block size (base address and limit+1 address).
61   \param[out]  blk_size:  The block size in bytes.
62   \return      Returns error code.
63 
64   \fn          int32_t ARM_MPC_GetCtrlConfig (uint32_t* ctrl_val)
65   \brief       Get some information on how the MPC IP is configured.
66   \param[out]  ctrl_val:  MPC control configuration
67   \return      Returns error code.
68 
69   \fn          int32_t ARM_MPC_SetCtrlConfig (uint32_t ctrl)
70   \brief       Set new control configuration for the MPC IP.
71   \param[in]   ctrl:  New control configuration.
72   \return      Returns error code.
73 
74   \fn          int32_t ARM_MPC_ConfigRegion (uintptr_t base,
75                                              uintptr_t limit,
76                                              ARM_MPC_SEC_ATTR attr)
77   \brief       Configure a memory region (base and limit included).
78                Both base and limit addresses must belong to the same
79                memory range, and this range must be managed by this MPC.
80                Also, some ranges are only allowed to be configured as
81                secure/non-secure, because of hardware requirements
82                (security aliases), and only a relevant security attribute
83                is therefore allowed for such ranges.
84   \param[in]   base:  Base address of the region to configure. This
85                       bound is included in the configured region.
86                       This must be aligned on the block size of this MPC.
87   \param[in]   limit: Limit address of the region to configure. This
88                       bound is included in the configured region.
89                       Limit+1 must be aligned on the block size of this MPC.
90   \param[in]   attr:  Wanted security attribute of the region.
91   \return      Returns error code.
92 
93   \fn          int32_t ARM_MPC_GetRegionConfig (uintptr_t base,
94                                                 uintptr_t limit,
95                                                 ARM_MPC_SEC_ATTR *attr)
96   \brief       Gets a memory region (base and limit included).
97   \param[in]   base:  Base address of the region to poll. This
98                       bound is included. It does not need to be aligned
99                       in any way.
100   \param[in]   limit: Limit address of the region to poll. This
101                       bound is included. (limit+1) does not need to be aligned
102                       in any way.
103   \param[out]  attr:  Security attribute of the region.
104                       If the region has mixed secure/non-secure,
105                       a special value is returned (\ref ARM_MPC_SEC_ATTR).
106 
107                In case base and limit+1 addresses are not aligned on
108                the block size, the enclosing region with base and
109                limit+1 aligned on block size will be queried.
110                In case of early termination of the function (error), the
111                security attribute will be set to ARM_MPC_ATTR_MIXED.
112   \return      Returns error code.
113 
114   \fn          int32_t ARM_MPC_EnableInterrupt (void)
115   \brief       Enable MPC interrupt.
116   \return      Returns error code.
117 
118   \fn          void ARM_MPC_DisableInterrupt (void)
119   \brief       Disable MPC interrupt.
120 
121   \fn          void ARM_MPC_ClearInterrupt (void)
122   \brief       Clear MPC interrupt.
123 
124   \fn          uint32_t ARM_MPC_InterruptState (void)
125   \brief       MPC interrupt state.
126   \return      Returns 1 if the interrupt is active, 0 otherwise.
127 
128   \fn          int32_t ARM_MPC_LockDown (void)
129   \brief       Lock down the MPC configuration.
130   \return      Returns error code.
131 */
132 
133 /**
134  * \brief Access structure of the MPC Driver.
135  */
136 typedef struct _ARM_DRIVER_MPC {
137   ARM_DRIVER_VERSION (*GetVersion)       (void);                                                     ///< Pointer to \ref ARM_MPC_GetVersion    : Get driver version.
138   int32_t            (*Initialize)       (void);                                                     ///< Pointer to \ref ARM_MPC_Initialize    : Initialize the MPC Interface.
139   int32_t            (*Uninitialize)     (void);                                                     ///< Pointer to \ref ARM_MPC_Uninitialize  : De-initialize the MPC Interface.
140   int32_t            (*GetBlockSize)     (uint32_t* blk_size);                                       ///< Pointer to \ref ARM_MPC_GetBlockSize  : Get MPC block size
141   int32_t            (*GetCtrlConfig)    (uint32_t* ctrl_val);                                       ///< Pointer to \ref ARM_MPC_GetCtrlConfig : Get the MPC control configuration flags.
142   int32_t            (*SetCtrlConfig)    (uint32_t ctrl);                                            ///< Pointer to \ref ARM_MPC_SetCtrlConfig : Set the MPC control configuration flags.
143   int32_t            (*ConfigRegion)     (uintptr_t base, uintptr_t limit, ARM_MPC_SEC_ATTR attr);   ///< Pointer to \ref ARM_MPC_ConfigRegion  : Configure a region using the driver for the specific MPC.
144   int32_t            (*GetRegionConfig)  (uintptr_t base, uintptr_t limit, ARM_MPC_SEC_ATTR *attr);  ///< Pointer to \ref ARM_MPC_GetRegionConfig  : Get the configuration of a specific region on this MPC.
145   int32_t            (*EnableInterrupt)  (void);                                                     ///< Pointer to \ref ARM_MPC_EnableInterrupt  : Enable MPC interrupt.
146   void               (*DisableInterrupt) (void);                                                     ///< Pointer to \ref ARM_MPC_DisableInterrupt : Disable MPC interrupt.
147   void               (*ClearInterrupt)   (void);                                                     ///< Pointer to \ref ARM_MPC_ClearInterrupt   : Clear MPC interrupt.
148   uint32_t           (*InterruptState)   (void);                                                     ///< Pointer to \ref ARM_MPC_InterruptState   : MPC interrupt State.
149   int32_t            (*LockDown)         (void);                                                     ///< Pointer to \ref ARM_MPC_LockDown         : Lock down the MPC configuration.
150 } const ARM_DRIVER_MPC;
151 
152 #endif /* __DRIVER_MPC_H */
153 
154