1 /******************************************************************************
3 * Copyright (C) 2014 - 2015 Xilinx, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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
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.
31 ******************************************************************************/
32 /*****************************************************************************/
36 * This file provides APIs for enabling/disabling MPU and setting the memory
37 * attributes for sections, in the MPU translation table.
40 * MODIFICATION HISTORY:
42 * Ver Who Date Changes
43 * ----- ---- -------- ---------------------------------------------------
44 * 5.00 pkp 02/10/14 Initial version
51 ******************************************************************************/
53 /***************************** Include Files *********************************/
55 #include "xil_cache.h"
56 #include "xpseudo_asm.h"
57 #include "xil_types.h"
60 /***************** Macros (Inline Functions) Definitions *********************/
62 /**************************** Type Definitions *******************************/
64 /************************** Constant Definitions *****************************/
66 /************************** Variable Definitions *****************************/
70 unsigned int encoding;
74 { 0x80, REGION_128B },
75 { 0x100, REGION_256B },
76 { 0x200, REGION_512B },
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 },
102 /************************** Function Prototypes ******************************/
104 /*****************************************************************************
106 * Set the memory attributes for a section of memory with starting address addr
107 * of the region size 1MB having attributes attrib
109 * @param addr is the address for which attributes are to be set.
110 * @param attrib specifies the attributes for that memory region.
114 ******************************************************************************/
115 void Xil_SetTlbAttributes(INTPTR addr, u32 attrib)
117 INTPTR Localaddr = addr;
118 Localaddr &= (~(0xFFFFFU));
119 /* Setting the MPU region with given attribute with 1MB size */
120 Xil_SetMPURegion(Localaddr, 0x100000, attrib);
123 /*****************************************************************************
125 * Set the memory attributes for a section of memory with starting address addr
126 * of the region size size and having attributes attrib
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.
134 ******************************************************************************/
135 void Xil_SetMPURegion(INTPTR addr, u64 size, u32 attrib)
138 INTPTR Localaddr = addr;
139 u32 NextAvailableMemRegion;
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");
150 mtcp(XREG_CP15_MPU_MEMORY_REG_NUMBER,NextAvailableMemRegion);
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;
161 Localaddr &= ~(region_size[i].size - 1);
164 Regionsize |= REGION_EN;
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*/
172 /*****************************************************************************
174 * Enable MPU for Cortex R5 processor. This function invalidates I cache and
175 * flush the D Caches before enabling the MPU.
181 ******************************************************************************/
182 void Xil_EnableMPU(void)
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) {
191 if ((CtrlReg & XREG_CP15_CONTROL_I_BIT) != 0x00000000U) {
195 if(DCacheStatus != 0) {
198 if(ICacheStatus != 0){
201 Reg = mfcp(XREG_CP15_SYS_CONTROL);
204 mtcp(XREG_CP15_SYS_CONTROL, Reg);
206 /* enable caches only if they are disabled in routine*/
207 if(DCacheStatus != 0) {
210 if(ICacheStatus != 0) {
215 /*****************************************************************************
217 * Disable MPU for Cortex R5 processors. This function invalidates I cache and
218 * flush the D Caches before disabling the MPU.
224 ******************************************************************************/
225 void Xil_DisableMPU(void)
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) {
234 if ((CtrlReg & XREG_CP15_CONTROL_I_BIT) != 0x00000000U) {
238 if(DCacheStatus != 0) {
241 if(ICacheStatus != 0){
245 mtcp(XREG_CP15_INVAL_BRANCH_ARRAY, 0);
246 Reg = mfcp(XREG_CP15_SYS_CONTROL);
247 Reg &= ~(0x00000001U);
249 mtcp(XREG_CP15_SYS_CONTROL, Reg);
251 /* enable caches only if they are disabled in routine*/
252 if(DCacheStatus != 0) {
255 if(ICacheStatus != 0) {