Software APIs
epmp.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_EPMP_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_EPMP_H_
7 
8 #include <stdint.h>
9 
10 #include "sw/device/silicon_creator/lib/epmp_state.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif // __cplusplus
15 
16 /**
17  * Enhanced Physical Memory Protection (ePMP) library.
18  *
19  * The ePMP configuration is managed in two parts:
20  *
21  * 1. The actual hardware configuration held in CSRs
22  * 2. The in-memory copy of register values in `epmp_state_t` that is used
23  * to verify the CSRs
24  *
25  * Every time the hardware configuration is updated the in-memory copy
26  * must also be updated. The hardware configuration is usually interacted
27  * with directly using the CSR library or assembly whereas the in-memory
28  * copy of the state should normally be modified using configuration functions
29  * from the silicon creator ePMP library.
30  *
31  * This separation of concerns allows the hardware configuration to be
32  * updated efficiently as needed (including before the C runtime is
33  * initialized) with the in-memory copy of the state used to double check the
34  * configuration as required.
35  */
36 
37 /**
38  * Clear an ePMP entry.
39  *
40  * Sets the PMPADDR and PMPCFG entries to zero.
41  *
42  * @param entry The ePMP entry to clear.
43  */
44 void epmp_clear(uint8_t entry);
45 
46 /**
47  * Clear the lock bit in all ePMP entries.
48  */
49 void epmp_clear_lock_bits(void);
50 
51 /**
52  * Encode a start/end address pair to NAPOT address.
53  *
54  * The region start must have an alignment consistent with the region size. The
55  * region size must be a power of two. If either of these conditions is not
56  * met, this function will fault.
57  *
58  * @param region The address region to configure.
59  * @return The encoded NAPOT address.
60  */
61 uint32_t epmp_encode_napot(epmp_region_t region);
62 
63 /**
64  * Decode a NAPOT address back to start/end address pair.
65  *
66  * @param pmpaddr The encoded NAPOT address.
67  * @return region The decoded start/end address pair.
68  */
69 epmp_region_t epmp_decode_napot(uint32_t pmpaddr);
70 
71 /**
72  * Configures an ePMP entry for a NAPOT or NA4 region.
73  *
74  * The region start must have an alignment consistent with the region size. The
75  * region size must be a power of two. If either of these conditions is not
76  * met, this function will fault.
77  *
78  * From a configuration perspective, an NA4 region is just a special case of a
79  * NAPOT region.
80  *
81  * @param entry The ePMP entry to configure.
82  * @param region The address region to configure.
83  * @param perm The ePMP permissions for the region.
84  */
85 void epmp_set_napot(uint8_t entry, epmp_region_t region, epmp_perm_t perm);
86 
87 /**
88  * Configures an ePMP entry for a TOR region.
89  *
90  * The region start and end may be abitrary addresses. The start will be
91  * rounded down to a 4-byte address. The end will be rounded up to a 4-byte
92  * address.
93  *
94  * @param entry The ePMP entry to configure.
95  * @param region The address region to configure.
96  * @param perm The ePMP permissions for the region.
97  */
98 void epmp_set_tor(uint8_t entry, epmp_region_t region, epmp_perm_t perm);
99 
100 /**
101  * Clear the rule-locking-bypass (RLB) bit.
102  *
103  * Clearing RLB causes the Lock bit in the ePMP to be enforced.
104  */
105 void epmp_clear_rlb(void);
106 
107 #ifdef __cplusplus
108 } // extern "C"
109 #endif // __cplusplus
110 
111 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_EPMP_H_