summaryrefslogtreecommitdiff
path: root/bsp/imx6ul/platform/cpu/mmu.c
blob: 0cf20c9942bc484e444fcade8238609c0c95ca8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (c) 2008-2012, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file  mmu.c
 * @brief System memory arangement.
 */
#include "cortex_a.h"
#include "mmu.h"
#include "arm_cp_registers.h"

////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////

//! @brief Size in bytes of the first-level page table.
#define MMU_L1_PAGE_TABLE_SIZE (16 * 1024)

//! @brief First-level 1MB section descriptor entry.
typedef union mmu_l1_section {
    uint32_t u;
    struct {
        uint32_t id:2;  //!< ID
        uint32_t b:1;   //!< Bufferable
        uint32_t c:1;   //!< Cacheable
        uint32_t xn:1;  //!< Execute-not
        uint32_t domain:4;  //!< Domain
        uint32_t _impl_defined:1;   //!< Implementation defined, should be zero.
        uint32_t ap1_0:2;  //!< Access permissions AP[1:0]
        uint32_t tex:3; //!< TEX remap
        uint32_t ap2:1; //!< Access permissions AP[2] 
        uint32_t s:1;   //!< Shareable
        uint32_t ng:1;  //!< Not-global
        uint32_t _zero:1;   //!< Should be zero.
        uint32_t ns:1;  //!< Non-secure
        uint32_t address:12;   //!< Physical base address
    };
} mmu_l1_section_t;

enum {
    kMMU_L1_Section_ID = 2,  //!< ID value for a 1MB section first-level entry.
    kMMU_L1_Section_Address_Shift = 20  //!< Bit offset of the physical base address field.
};

////////////////////////////////////////////////////////////////////////////////
// Externs
////////////////////////////////////////////////////////////////////////////////

extern char __l1_page_table_start;

////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////

void mmu_enable()
{
    // invalidate all tlb 
    arm_unified_tlb_invalidate();

    // read SCTLR 
    uint32_t sctlr;
    _ARM_MRC(15, 0, sctlr, 1, 0, 0);
    
    // set MMU enable bit 
    sctlr |= BM_SCTLR_M;

    // write modified SCTLR
    _ARM_MCR(15, 0, sctlr, 1, 0, 0);
}

void mmu_disable()
{
    // read current SCTLR 
    uint32_t sctlr;
    _ARM_MRC(15, 0, sctlr, 1, 0, 0);
    
    // clear MMU enable bit 
    sctlr &=~ BM_SCTLR_M;

    // write modified SCTLR
    _ARM_MCR(15, 0, sctlr, 1, 0, 0);
}

void mmu_init()
{
    // Get the L1 page table base address.
    uint32_t * table = (uint32_t *)&__l1_page_table_start;
    uint32_t share_attr = kShareable;

    // write table address to TTBR0
    _ARM_MCR(15, 0, table, 2, 0, 0);

    // set Client mode for all Domains
    uint32_t dacr = 0x55555555; 
    _ARM_MCR(15, 0, dacr, 3, 0, 0); // MCR p15, 0, <Rd>, c3, c0, 0 ; Write DACR

    // Clear the L1 table.
    bzero(table, MMU_L1_PAGE_TABLE_SIZE);
    
    // Create default mappings.
    mmu_map_l1_range(0x00000000, 0x00000000, 0x00900000, kStronglyOrdered, kShareable, kRWAccess); // ROM and peripherals
    mmu_map_l1_range(0x00900000, 0x00900000, 0x00100000, kStronglyOrdered, kShareable, kRWAccess); // OCRAM
    mmu_map_l1_range(0x00a00000, 0x00a00000, 0x0f600000, kStronglyOrdered, kShareable, kRWAccess); // More peripherals
   
    // Check whether SMP is enabled. If it is not, then we don't want to make SDRAM shareable.
    uint32_t actlr = 0x0;
    _ARM_MRC(15, 0, actlr, 1, 0, 1);
    if (actlr & BM_ACTLR_SMP)
    {
        share_attr = kShareable;
    }
    else
    {
        share_attr = kNonshareable;
    }

#if defined(CHIP_MX6DQ) || defined(CHIP_MX6SDL)
    mmu_map_l1_range(0x10000000, 0x10000000, 0x80000000, kOuterInner_WB_WA, share_attr, kRWAccess); // 2GB DDR
#elif defined(CHIP_MX6SL) || defined(CHIP_MX6UL)
    mmu_map_l1_range(0x80000000, 0x80000000, 0x40000000, kOuterInner_WB_WA, share_attr, kRWAccess); // 1GB DDR
#else
#error Unknown chip type!
#endif
}

void mmu_map_l1_range(uint32_t pa, uint32_t va, uint32_t length, mmu_memory_type_t memoryType, mmu_shareability_t isShareable, mmu_access_t access)
{
    register mmu_l1_section_t entry;
    entry.u = 0;
    
    // Set constant attributes.
    entry.id = kMMU_L1_Section_ID;
    entry.xn = 0; // Allow execution
    entry.domain = 0; // Domain 0
    entry.ng = 0; // Global
    entry.ns = 0; // Secure
    
    // Set attributes based on the selected memory type.
    switch (memoryType)
    {
        case kStronglyOrdered:
            entry.c = 0;
            entry.b = 0;
            entry.tex = 0;
            entry.s = 1; // Ignored
            break;
        case kDevice:
            if (isShareable)
            {
                entry.c = 0;
                entry.b = 1;
                entry.tex = 0;
                entry.s = 1; // Ignored
            }
            else
            {
                entry.c = 0;
                entry.b = 0;
                entry.tex = 2;
                entry.s = 0; // Ignored
            }
            break;
        case kOuterInner_WB_WA:
            entry.c = 1;
            entry.b = 1;
            entry.tex = 1;
            entry.s = isShareable;
            break;
        case kOuterInner_WT:
            entry.c = 1;
            entry.b = 0;
            entry.tex = 0;
            entry.s = isShareable;
            break;
        case kNoncacheable:
            entry.c = 0;
            entry.b = 0;
            entry.tex = 1;
            entry.s = isShareable;
            break;
    }
    
    // Set attributes from specified access mode.
    switch (access)
    {
        case kNoAccess:
            entry.ap2 = 0;
            entry.ap1_0 = 0;
            break;
        case kROAccess:
            entry.ap2 = 1;
            entry.ap1_0 = 3;
            break;
        case kRWAccess:
            entry.ap2 = 0;
            entry.ap1_0 = 3;
            break;
    }
    
    // Get the L1 page table base address.
    uint32_t * table = (uint32_t *)&__l1_page_table_start;
    
    // Convert addresses to 12-bit bases.
    uint32_t vbase = va >> kMMU_L1_Section_Address_Shift;
    uint32_t pbase = pa >> kMMU_L1_Section_Address_Shift;
    uint32_t entries = length >> kMMU_L1_Section_Address_Shift;

    // Fill in L1 page table entries.
    for (; entries > 0; ++pbase, ++vbase, --entries)
    {
        entry.address = pbase;
        table[vbase] = entry.u;
    }
    
    // Invalidate TLB
    arm_unified_tlb_invalidate();
}

bool mmu_virtual_to_physical(uint32_t virtualAddress, uint32_t * physicalAddress)
{
    uint32_t pa = 0;
    
    // VA to PA translation with privileged read permission check  
    _ARM_MCR(15, 0, virtualAddress & 0xfffffc00, 7, 8, 0);
    
    // Read PA register 
    _ARM_MRC(15, 0, pa, 7, 4, 0);
    
    // First bit of returned value is Result of conversion (0 is successful translation) 
    if (pa & 1)
    {
        // We can try write permission also 
        // VA to PA translation with privileged write permission check  
        _ARM_MCR(15, 0, virtualAddress & 0xfffffc00, 7, 8, 1);
        
        // Read PA register 
        _ARM_MRC(15, 0, pa, 7, 4, 0);
        
        // First bit of returned value is Result of conversion (0 is successful translation) 
        if (pa & 1)
        {
            return false;
        }
    }
    
    if (physicalAddress)
    {
        // complete address returning base + offset
        pa = (pa & 0xfffff000) | (virtualAddress & 0x00000fff);
        *physicalAddress = pa;
    }
    
    return true;
}

////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////