source: mainline/boot/arch/arm32/src/mm.c@ ae7d03c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ae7d03c was ae7d03c, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Selected ccheck-proposed comment fixes.

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