]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/standalone_v6_6/src/xil_mmu.c
Update Zynq, MPSoc Cortex-A53 and MPSoc Cortex-R5 demo projects to build with the...
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / standalone_v6_6 / src / xil_mmu.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2012 - 2015 Xilinx, Inc.  All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 * @file xil_mmu.c
35 *
36 * This file provides APIs for enabling/disabling MMU and setting the memory
37 * attributes for sections, in the MMU translation table.
38 *
39 * <pre>
40 * MODIFICATION HISTORY:
41 *
42 * Ver   Who  Date     Changes
43 * ----- ---- -------- ---------------------------------------------------
44 * 1.00a sdm  01/12/12 Initial version
45 * 3.05a asa  03/10/12 Modified the Xil_EnableMMU to invalidate the caches
46 *                     before enabling back.
47 * 3.05a asa  04/15/12 Modified the Xil_SetTlbAttributes routine so that
48 *                     translation table and branch predictor arrays are
49 *                     invalidated, D-cache flushed before the attribute
50 *                     change is applied. This is done so that the user
51 *                     need not call Xil_DisableMMU before calling
52 *                     Xil_SetTlbAttributes.
53 * 3.10a  srt 04/18/13 Implemented ARM Erratas. Please refer to file
54 *                     'xil_errata.h' for errata description
55 * 3.11a  asa 09/23/13 Modified Xil_SetTlbAttributes to flush the complete
56 *                        D cache after the translation table update. Removed the
57 *                        redundant TLB invalidation in the same API at the beginning.
58 * </pre>
59 *
60 * @note
61 *
62 * None.
63 *
64 ******************************************************************************/
65
66 /***************************** Include Files *********************************/
67
68 #include "xil_cache.h"
69 #include "xpseudo_asm.h"
70 #include "xil_types.h"
71 #include "xil_mmu.h"
72 #include "xil_errata.h"
73
74 /***************** Macros (Inline Functions) Definitions *********************/
75
76 /**************************** Type Definitions *******************************/
77
78 /************************** Constant Definitions *****************************/
79
80 /************************** Variable Definitions *****************************/
81
82 extern u32 MMUTable;
83
84 /************************** Function Prototypes ******************************/
85
86 /*****************************************************************************/
87 /**
88 * @brief        This function sets the memory attributes for a section covering 1MB
89 *                       of memory in the translation table.
90 *
91 * @param        Addr: 32-bit address for which memory attributes need to be set.
92 * @param        attrib: Attribute for the given memory region. xil_mmu.h contains
93 *                       definitions of commonly used memory attributes which can be
94 *                       utilized for this function.
95 *
96 *
97 * @return       None.
98 *
99 * @note         The MMU or D-cache does not need to be disabled before changing a
100 *                       translation table entry.
101 *
102 ******************************************************************************/
103 void Xil_SetTlbAttributes(INTPTR Addr, u32 attrib)
104 {
105         u32 *ptr;
106         u32 section;
107
108         section = Addr / 0x100000U;
109         ptr = &MMUTable;
110         ptr += section;
111         if(ptr != NULL) {
112                 *ptr = (Addr & 0xFFF00000U) | attrib;
113         }
114
115         Xil_DCacheFlush();
116
117         mtcp(XREG_CP15_INVAL_UTLB_UNLOCKED, 0U);
118         /* Invalidate all branch predictors */
119         mtcp(XREG_CP15_INVAL_BRANCH_ARRAY, 0U);
120
121         dsb(); /* ensure completion of the BP and TLB invalidation */
122     isb(); /* synchronize context on this processor */
123 }
124
125 /*****************************************************************************/
126 /**
127 * @brief        Enable MMU for cortex A9 processor. This function invalidates the
128 *                       instruction and data caches, and then enables MMU.
129 *
130 * @param        None.
131 * @return       None.
132 *
133 ******************************************************************************/
134 void Xil_EnableMMU(void)
135 {
136         u32 Reg;
137         Xil_DCacheInvalidate();
138         Xil_ICacheInvalidate();
139
140 #ifdef __GNUC__
141         Reg = mfcp(XREG_CP15_SYS_CONTROL);
142 #elif defined (__ICCARM__)
143         mfcp(XREG_CP15_SYS_CONTROL, Reg);
144 #else
145         { volatile register u32 Cp15Reg __asm(XREG_CP15_SYS_CONTROL);
146           Reg = Cp15Reg; }
147 #endif
148         Reg |= (u32)0x05U;
149         mtcp(XREG_CP15_SYS_CONTROL, Reg);
150
151         dsb();
152         isb();
153 }
154
155 /*****************************************************************************/
156 /**
157 * @brief        Disable MMU for Cortex A9 processors. This function invalidates
158 *                       the TLBs, Branch Predictor Array and flushed the D Caches before
159 *                       disabling the MMU.
160 *
161 * @param        None.
162 *
163 * @return       None.
164 *
165 * @note         When the MMU is disabled, all the memory accesses are treated as
166 *                       strongly ordered.
167 ******************************************************************************/
168 void Xil_DisableMMU(void)
169 {
170         u32 Reg;
171
172         mtcp(XREG_CP15_INVAL_UTLB_UNLOCKED, 0U);
173         mtcp(XREG_CP15_INVAL_BRANCH_ARRAY, 0U);
174         Xil_DCacheFlush();
175
176 #ifdef __GNUC__
177         Reg = mfcp(XREG_CP15_SYS_CONTROL);
178 #elif defined (__ICCARM__)
179         mfcp(XREG_CP15_SYS_CONTROL, Reg);
180 #else
181         { volatile register u32 Cp15Reg __asm(XREG_CP15_SYS_CONTROL);
182           Reg = Cp15Reg; }
183 #endif
184         Reg &= (u32)(~0x05U);
185 #ifdef CONFIG_ARM_ERRATA_794073
186         /* Disable Branch Prediction */
187         Reg &= (u32)(~0x800U);
188 #endif
189         mtcp(XREG_CP15_SYS_CONTROL, Reg);
190 }