[4872160] | 1 | /*
|
---|
[999efa9] | 2 | * Copyright (c) 2007 Pavel Jancik
|
---|
| 3 | * Copyright (c) 2007 Michal Kebrt
|
---|
[4872160] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup arm32boot
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | * @brief Memory management used while booting the kernel.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[7a99507] | 37 | #include <stdint.h>
|
---|
[4872160] | 38 | #include <arch/asm.h>
|
---|
| 39 | #include <arch/mm.h>
|
---|
[67d02bb] | 40 | #include <arch/cp15.h>
|
---|
| 41 |
|
---|
| 42 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 43 | static unsigned log2(unsigned val)
|
---|
| 44 | {
|
---|
| 45 | unsigned log = 0;
|
---|
| 46 | while (val >> log++);
|
---|
| 47 | return log - 2;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | static void dcache_invalidate_level(unsigned level)
|
---|
| 51 | {
|
---|
| 52 | CSSELR_write(level << 1);
|
---|
| 53 | const uint32_t ccsidr = CCSIDR_read();
|
---|
| 54 | const unsigned sets = CCSIDR_SETS(ccsidr);
|
---|
| 55 | const unsigned ways = CCSIDR_WAYS(ccsidr);
|
---|
| 56 | const unsigned line_log = CCSIDR_LINESIZE_LOG(ccsidr);
|
---|
| 57 | const unsigned set_shift = line_log;
|
---|
| 58 | const unsigned way_shift = 32 - log2(ways);
|
---|
| 59 |
|
---|
| 60 | for (unsigned k = 0; k < ways; ++k)
|
---|
| 61 | for (unsigned j = 0; j < sets; ++j) {
|
---|
| 62 | const uint32_t val = (level << 1) |
|
---|
| 63 | (j << set_shift) | (k << way_shift);
|
---|
| 64 | DCISW_write(val);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /** invalidate all dcaches -- armv7 */
|
---|
| 69 | static void cache_invalidate(void)
|
---|
| 70 | {
|
---|
| 71 | const uint32_t cinfo = CLIDR_read();
|
---|
| 72 | for (unsigned i = 0; i < 7; ++i) {
|
---|
| 73 | switch (CLIDR_CACHE(i, cinfo))
|
---|
| 74 | {
|
---|
| 75 | case CLIDR_DCACHE_ONLY:
|
---|
| 76 | case CLIDR_SEP_CACHE:
|
---|
| 77 | case CLIDR_UNI_CACHE:
|
---|
| 78 | dcache_invalidate_level(i);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | asm volatile ( "dsb\n" );
|
---|
| 82 | ICIALLU_write(0);
|
---|
| 83 | asm volatile ( "isb\n" );
|
---|
| 84 | }
|
---|
| 85 | #endif
|
---|
[4872160] | 86 |
|
---|
[5e761f3] | 87 | /** Disable the MMU */
|
---|
| 88 | static void disable_paging(void)
|
---|
| 89 | {
|
---|
| 90 | asm volatile (
|
---|
| 91 | "mrc p15, 0, r0, c1, c0, 0\n"
|
---|
| 92 | "bic r0, r0, #1\n"
|
---|
| 93 | "mcr p15, 0, r0, c1, c0, 0\n"
|
---|
[378757f] | 94 | ::: "r0"
|
---|
[5e761f3] | 95 | );
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[b5a3b50] | 98 | /** Check if caching can be enabled for a given memory section.
|
---|
| 99 | *
|
---|
| 100 | * Memory areas used for I/O are excluded from caching.
|
---|
| 101 | * At the moment caching is enabled only on GTA02.
|
---|
| 102 | *
|
---|
| 103 | * @param section The section number.
|
---|
| 104 | *
|
---|
| 105 | * @return 1 if the given section can be mapped as cacheable, 0 otherwise.
|
---|
| 106 | */
|
---|
| 107 | static inline int section_cacheable(pfn_t section)
|
---|
| 108 | {
|
---|
[9120b69] | 109 | const unsigned long address = section << PTE_SECTION_SHIFT;
|
---|
[b5a3b50] | 110 | #ifdef MACHINE_gta02
|
---|
[9120b69] | 111 | if (address < GTA02_IOMEM_START || address >= GTA02_IOMEM_END)
|
---|
[b5a3b50] | 112 | return 1;
|
---|
[0acd339] | 113 | #elif defined MACHINE_beagleboardxm
|
---|
| 114 | if (address >= BBXM_RAM_START && address < BBXM_RAM_END)
|
---|
[bfb6576] | 115 | return 1;
|
---|
[6968948] | 116 | #elif defined MACHINE_beaglebone
|
---|
| 117 | if (address >= AM335x_RAM_START && address < AM335x_RAM_END)
|
---|
| 118 | return 1;
|
---|
[8f9d70b] | 119 | #elif defined MACHINE_raspberrypi
|
---|
| 120 | if (address < BCM2835_RAM_END)
|
---|
| 121 | return 1;
|
---|
[b5a3b50] | 122 | #endif
|
---|
[9120b69] | 123 | return address * 0;
|
---|
[b5a3b50] | 124 | }
|
---|
| 125 |
|
---|
[4872160] | 126 | /** Initialize "section" page table entry.
|
---|
| 127 | *
|
---|
| 128 | * Will be readable/writable by kernel with no access from user mode.
|
---|
| 129 | * Will belong to domain 0. No cache or buffering is enabled.
|
---|
| 130 | *
|
---|
| 131 | * @param pte Section entry to initialize.
|
---|
| 132 | * @param frame First frame in the section (frame number).
|
---|
| 133 | *
|
---|
| 134 | * @note If frame is not 1 MB aligned, first lower 1 MB aligned frame will be
|
---|
| 135 | * used.
|
---|
| 136 | *
|
---|
| 137 | */
|
---|
| 138 | static void init_ptl0_section(pte_level0_section_t* pte,
|
---|
| 139 | pfn_t frame)
|
---|
| 140 | {
|
---|
| 141 | pte->descriptor_type = PTE_DESCRIPTOR_SECTION;
|
---|
[4d02595] | 142 | pte->xn = 0;
|
---|
[4872160] | 143 | pte->domain = 0;
|
---|
| 144 | pte->should_be_zero_1 = 0;
|
---|
[4d02595] | 145 | pte->access_permission_0 = PTE_AP_USER_NO_KERNEL_RW;
|
---|
[93d8022] | 146 | #if defined(PROCESSOR_ARCH_armv6) || defined(PROCESSOR_ARCH_armv7_a)
|
---|
[ae5fb7c8] | 147 | /*
|
---|
| 148 | * Keeps this setting in sync with memory type attributes in:
|
---|
| 149 | * init_boot_pt (boot/arch/arm32/src/mm.c)
|
---|
| 150 | * set_pt_level1_flags (kernel/arch/arm32/include/arch/mm/page_armv6.h)
|
---|
| 151 | * set_ptl0_addr (kernel/arch/arm32/include/arch/mm/page.h)
|
---|
| 152 | */
|
---|
[7bf9217] | 153 | pte->tex = section_cacheable(frame) ? 5 : 0;
|
---|
| 154 | pte->cacheable = section_cacheable(frame) ? 0 : 0;
|
---|
[93d8022] | 155 | pte->bufferable = section_cacheable(frame) ? 1 : 1;
|
---|
[f9f758e] | 156 | #else
|
---|
[93d8022] | 157 | pte->bufferable = section_cacheable(frame);
|
---|
[f9f758e] | 158 | pte->cacheable = section_cacheable(frame);
|
---|
[4d02595] | 159 | pte->tex = 0;
|
---|
[f9f758e] | 160 | #endif
|
---|
[4d02595] | 161 | pte->access_permission_1 = 0;
|
---|
[2e55443] | 162 | pte->shareable = 0;
|
---|
[4d02595] | 163 | pte->non_global = 0;
|
---|
[4872160] | 164 | pte->should_be_zero_2 = 0;
|
---|
[4d02595] | 165 | pte->non_secure = 0;
|
---|
[4872160] | 166 | pte->section_base_addr = frame;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** Initialize page table used while booting the kernel. */
|
---|
| 170 | static void init_boot_pt(void)
|
---|
| 171 | {
|
---|
[ae5fb7c8] | 172 | /*
|
---|
| 173 | * Create 1:1 virtual-physical mapping.
|
---|
| 174 | * Physical memory on BBxM a BBone starts at 2GB
|
---|
[e93bb24] | 175 | * boundary, icp has a memory mirror at 2GB.
|
---|
| 176 | * (ARM Integrator Core Module User guide ch. 6.3, p. 6-7)
|
---|
| 177 | * gta02 somehow works (probably due to limited address size),
|
---|
| 178 | * s3c2442b manual ch. 5, p.5-1:
|
---|
| 179 | * "Address space: 128Mbytes per bank (total 1GB/8 banks)"
|
---|
[ae5fb7c8] | 180 | */
|
---|
[e93bb24] | 181 | for (pfn_t page = 0; page < PTL0_ENTRIES; ++page)
|
---|
[4872160] | 182 | init_ptl0_section(&boot_pt[page], page);
|
---|
[8f9d70b] | 183 |
|
---|
[ae5fb7c8] | 184 | /*
|
---|
| 185 | * Tell MMU page might be cached. Keeps this setting in sync
|
---|
| 186 | * with memory type attributes in:
|
---|
| 187 | * init_ptl0_section (boot/arch/arm32/src/mm.c)
|
---|
| 188 | * set_pt_level1_flags (kernel/arch/arm32/include/arch/mm/page_armv6.h)
|
---|
| 189 | * set_ptl0_addr (kernel/arch/arm32/include/arch/mm/page.h)
|
---|
| 190 | */
|
---|
| 191 | uint32_t val = (uint32_t)boot_pt & TTBR_ADDR_MASK;
|
---|
[a1d636e] | 192 | #if defined(PROCESSOR_ARCH_armv6) || defined(PROCESSOR_ARCH_armv7_a)
|
---|
| 193 | // FIXME: TTBR_RGN_WBWA_CACHE is unpredictable on ARMv6
|
---|
[7bf9217] | 194 | val |= TTBR_RGN_WBWA_CACHE | TTBR_C_FLAG;
|
---|
[a1d636e] | 195 | #endif
|
---|
[ae5fb7c8] | 196 | TTBR0_write(val);
|
---|
[4872160] | 197 | }
|
---|
| 198 |
|
---|
[193d280c] | 199 | static void enable_paging(void)
|
---|
[4872160] | 200 | {
|
---|
[193d280c] | 201 | /*
|
---|
| 202 | * c3 - each two bits controls access to the one of domains (16)
|
---|
[4872160] | 203 | * 0b01 - behave as a client (user) of a domain
|
---|
| 204 | */
|
---|
| 205 | asm volatile (
|
---|
| 206 | /* Behave as a client of domains */
|
---|
| 207 | "ldr r0, =0x55555555\n"
|
---|
[6b3ee0c5] | 208 | "mcr p15, 0, r0, c3, c0, 0\n"
|
---|
[5e761f3] | 209 |
|
---|
[4872160] | 210 | /* Current settings */
|
---|
| 211 | "mrc p15, 0, r0, c1, c0, 0\n"
|
---|
| 212 |
|
---|
[df334ca] | 213 | /* Enable ICache, DCache, BPredictors and MMU,
|
---|
| 214 | * we disable caches before jumping to kernel
|
---|
| 215 | * so this is safe for all archs.
|
---|
[4b27f5f] | 216 | * Enable VMSAv6 the bit (23) is only writable on ARMv6.
|
---|
[f7fba727] | 217 | * (and QEMU)
|
---|
[df334ca] | 218 | */
|
---|
[f7fba727] | 219 | #ifdef PROCESSOR_ARCH_armv6
|
---|
[4b27f5f] | 220 | "ldr r1, =0x00801805\n"
|
---|
[f7fba727] | 221 | #else
|
---|
[df334ca] | 222 | "ldr r1, =0x00001805\n"
|
---|
[f7fba727] | 223 | #endif
|
---|
[df334ca] | 224 |
|
---|
[4872160] | 225 | "orr r0, r0, r1\n"
|
---|
[5e761f3] | 226 |
|
---|
[f69ac6c] | 227 | /* Invalidate the TLB content before turning on the MMU.
|
---|
| 228 | * ARMv7-A Reference manual, B3.10.3
|
---|
| 229 | */
|
---|
[5e761f3] | 230 | "mcr p15, 0, r0, c8, c7, 0\n"
|
---|
[4872160] | 231 |
|
---|
[5e761f3] | 232 | /* Store settings, enable the MMU */
|
---|
[4872160] | 233 | "mcr p15, 0, r0, c1, c0, 0\n"
|
---|
| 234 | ::: "r0", "r1"
|
---|
| 235 | );
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /** Start the MMU - initialize page table and enable paging. */
|
---|
[193d280c] | 239 | void mmu_start(void)
|
---|
| 240 | {
|
---|
[5e761f3] | 241 | disable_paging();
|
---|
[67d02bb] | 242 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 243 | /* Make sure we run in memory code when caches are enabled,
|
---|
| 244 | * make sure we read memory data too. This part is ARMv7 specific as
|
---|
| 245 | * ARMv7 no longer invalidates caches on restart.
|
---|
| 246 | * See chapter B2.2.2 of ARM Architecture Reference Manual p. B2-1263*/
|
---|
| 247 | cache_invalidate();
|
---|
| 248 | #endif
|
---|
[4872160] | 249 | init_boot_pt();
|
---|
| 250 | enable_paging();
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /** @}
|
---|
| 254 | */
|
---|