]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5_bsp/psu_cortexr5_0/libsrc/standalone_v6_1/src/xil_mpu.c
Update BSP source files for UltraScale Cortex-A53 and Cortex-R5 and Microblaze to...
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5_bsp / psu_cortexr5_0 / libsrc / standalone_v6_1 / src / xil_mpu.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2014 - 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_mpu.c
35 *
36 * This file provides APIs for enabling/disabling MPU and setting the memory
37 * attributes for sections, in the MPU translation table.
38 *
39 * <pre>
40 * MODIFICATION HISTORY:
41 *
42 * Ver   Who  Date     Changes
43 * ----- ---- -------- ---------------------------------------------------
44 * 5.00  pkp  02/10/14 Initial version
45 * </pre>
46 *
47 * @note
48 *
49 * None.
50 *
51 ******************************************************************************/
52
53 /***************************** Include Files *********************************/
54
55 #include "xil_cache.h"
56 #include "xpseudo_asm.h"
57 #include "xil_types.h"
58 #include "xil_mpu.h"
59 #include "xdebug.h"
60 /***************** Macros (Inline Functions) Definitions *********************/
61
62 /**************************** Type Definitions *******************************/
63
64 /************************** Constant Definitions *****************************/
65
66 /************************** Variable Definitions *****************************/
67
68 static const struct {
69         u64 size;
70         unsigned int encoding;
71 }region_size[] = {
72         { 0x20, REGION_32B },
73         { 0x40, REGION_64B },
74         { 0x80, REGION_128B },
75         { 0x100, REGION_256B },
76         { 0x200, REGION_512B },
77         { 0x400, REGION_1K },
78         { 0x800, REGION_2K },
79         { 0x1000, REGION_4K },
80         { 0x2000, REGION_8K },
81         { 0x4000, REGION_16K },
82         { 0x8000, REGION_32K },
83         { 0x10000, REGION_64K },
84         { 0x20000, REGION_128K },
85         { 0x40000, REGION_256K },
86         { 0x80000, REGION_512K },
87         { 0x100000, REGION_1M },
88         { 0x200000, REGION_2M },
89         { 0x400000, REGION_4M },
90         { 0x800000, REGION_8M },
91         { 0x1000000, REGION_16M },
92         { 0x2000000, REGION_32M },
93         { 0x4000000, REGION_64M },
94         { 0x8000000, REGION_128M },
95         { 0x10000000, REGION_256M },
96         { 0x20000000, REGION_512M },
97         { 0x40000000, REGION_1G },
98         { 0x80000000, REGION_2G },
99         { 0x100000000, REGION_4G },
100 };
101
102 /************************** Function Prototypes ******************************/
103
104 /*****************************************************************************
105 *
106 * Set the memory attributes for a section of memory with starting address addr
107 * of the region size 1MB having attributes attrib
108 *
109 * @param        addr is the address for which attributes are to be set.
110 * @param        attrib specifies the attributes for that memory region.
111 * @return       None.
112 *
113 *
114 ******************************************************************************/
115 void Xil_SetTlbAttributes(INTPTR addr, u32 attrib)
116 {
117         INTPTR Localaddr = addr;
118         Localaddr &= (~(0xFFFFFU));
119         /* Setting the MPU region with given attribute with 1MB size */
120         Xil_SetMPURegion(Localaddr, 0x100000, attrib);
121 }
122
123 /*****************************************************************************
124 *
125 * Set the memory attributes for a section of memory with starting address addr
126 * of the region size size and having attributes attrib
127 *
128 * @param        addr is the address for which attributes are to be set.
129 * @param        size is the size of the region.
130 * @param        attrib specifies the attributes for that memory region.
131 * @return       None.
132 *
133 *
134 ******************************************************************************/
135 void Xil_SetMPURegion(INTPTR addr, u64 size, u32 attrib)
136 {
137         u32 Regionsize = 0;
138         INTPTR Localaddr = addr;
139         u32 NextAvailableMemRegion;
140         unsigned int i;
141
142         Xil_DCacheFlush();
143         Xil_ICacheInvalidate();
144         NextAvailableMemRegion = mfcp(XREG_CP15_MPU_MEMORY_REG_NUMBER);
145         NextAvailableMemRegion++;
146         if (NextAvailableMemRegion > 16) {
147                 xdbg_printf(DEBUG, "No regions available\r\n");
148                 return;
149         }
150         mtcp(XREG_CP15_MPU_MEMORY_REG_NUMBER,NextAvailableMemRegion);
151         isb();
152
153         /* Lookup the size.  */
154         for (i = 0; i < sizeof region_size / sizeof region_size[0]; i++) {
155                 if (size <= region_size[i].size) {
156                         Regionsize = region_size[i].encoding;
157                         break;
158                 }
159         }
160
161         Localaddr &= ~(region_size[i].size - 1);
162
163         Regionsize <<= 1;
164         Regionsize |= REGION_EN;
165         dsb();
166         mtcp(XREG_CP15_MPU_REG_BASEADDR, Localaddr);    /* Set base address of a region */
167         mtcp(XREG_CP15_MPU_REG_ACCESS_CTRL, attrib);    /* Set the control attribute */
168         mtcp(XREG_CP15_MPU_REG_SIZE_EN, Regionsize);    /* set the region size and enable it*/
169         dsb();
170         isb();
171 }
172 /*****************************************************************************
173 *
174 * Enable MPU for Cortex R5 processor. This function invalidates I cache and
175 * flush the D Caches before enabling the MPU.
176 *
177 *
178 * @param        None.
179 * @return       None.
180 *
181 ******************************************************************************/
182 void Xil_EnableMPU(void)
183 {
184         u32 CtrlReg, Reg;
185         s32 DCacheStatus=0, ICacheStatus=0;
186         /* enable caches only if they are disabled */
187         CtrlReg = mfcp(XREG_CP15_SYS_CONTROL);
188         if ((CtrlReg & XREG_CP15_CONTROL_C_BIT) != 0x00000000U) {
189                 DCacheStatus=1;
190         }
191         if ((CtrlReg & XREG_CP15_CONTROL_I_BIT) != 0x00000000U) {
192                 ICacheStatus=1;
193         }
194
195         if(DCacheStatus != 0) {
196                 Xil_DCacheDisable();
197         }
198         if(ICacheStatus != 0){
199                 Xil_ICacheDisable();
200         }
201         Reg = mfcp(XREG_CP15_SYS_CONTROL);
202         Reg |= 0x00000001U;
203         dsb();
204         mtcp(XREG_CP15_SYS_CONTROL, Reg);
205         isb();
206         /* enable caches only if they are disabled in routine*/
207         if(DCacheStatus != 0) {
208                 Xil_DCacheEnable();
209         }
210         if(ICacheStatus != 0) {
211                 Xil_ICacheEnable();
212         }
213 }
214
215 /*****************************************************************************
216 *
217 * Disable MPU for Cortex R5 processors. This function invalidates I cache and
218 * flush the D Caches before disabling the MPU.
219 *
220 * @param        None.
221 *
222 * @return       None.
223 *
224 ******************************************************************************/
225 void Xil_DisableMPU(void)
226 {
227         u32 CtrlReg, Reg;
228         s32 DCacheStatus=0, ICacheStatus=0;
229         /* enable caches only if they are disabled */
230         CtrlReg = mfcp(XREG_CP15_SYS_CONTROL);
231         if ((CtrlReg & XREG_CP15_CONTROL_C_BIT) != 0x00000000U) {
232                 DCacheStatus=1;
233         }
234         if ((CtrlReg & XREG_CP15_CONTROL_I_BIT) != 0x00000000U) {
235                 ICacheStatus=1;
236         }
237
238         if(DCacheStatus != 0) {
239                 Xil_DCacheDisable();
240         }
241         if(ICacheStatus != 0){
242                 Xil_ICacheDisable();
243         }
244
245         mtcp(XREG_CP15_INVAL_BRANCH_ARRAY, 0);
246         Reg = mfcp(XREG_CP15_SYS_CONTROL);
247         Reg &= ~(0x00000001U);
248         dsb();
249         mtcp(XREG_CP15_SYS_CONTROL, Reg);
250         isb();
251         /* enable caches only if they are disabled in routine*/
252         if(DCacheStatus != 0) {
253                 Xil_DCacheEnable();
254         }
255         if(ICacheStatus != 0) {
256                 Xil_ICacheEnable();
257         }
258 }