GNU Linux-libre 4.19.314-gnu1
[releases.git] / arch / arm / mm / nommu.c
1 /*
2  *  linux/arch/arm/mm/nommu.c
3  *
4  * ARM uCLinux supporting functions.
5  */
6 #include <linux/module.h>
7 #include <linux/mm.h>
8 #include <linux/pagemap.h>
9 #include <linux/io.h>
10 #include <linux/memblock.h>
11 #include <linux/kernel.h>
12
13 #include <asm/cacheflush.h>
14 #include <asm/cp15.h>
15 #include <asm/sections.h>
16 #include <asm/page.h>
17 #include <asm/setup.h>
18 #include <asm/traps.h>
19 #include <asm/mach/arch.h>
20 #include <asm/cputype.h>
21 #include <asm/mpu.h>
22 #include <asm/procinfo.h>
23
24 #include "mm.h"
25
26 unsigned long vectors_base;
27
28 /*
29  * empty_zero_page is a special page that is used for
30  * zero-initialized data and COW.
31  */
32 struct page *empty_zero_page;
33 EXPORT_SYMBOL(empty_zero_page);
34
35 #ifdef CONFIG_ARM_MPU
36 struct mpu_rgn_info mpu_rgn_info;
37 #endif
38
39 #ifdef CONFIG_CPU_CP15
40 #ifdef CONFIG_CPU_HIGH_VECTOR
41 unsigned long setup_vectors_base(void)
42 {
43         unsigned long reg = get_cr();
44
45         set_cr(reg | CR_V);
46         return 0xffff0000;
47 }
48 #else /* CONFIG_CPU_HIGH_VECTOR */
49 /* Write exception base address to VBAR */
50 static inline void set_vbar(unsigned long val)
51 {
52         asm("mcr p15, 0, %0, c12, c0, 0" : : "r" (val) : "cc");
53 }
54
55 /*
56  * Security extensions, bits[7:4], permitted values,
57  * 0b0000 - not implemented, 0b0001/0b0010 - implemented
58  */
59 static inline bool security_extensions_enabled(void)
60 {
61         /* Check CPUID Identification Scheme before ID_PFR1 read */
62         if ((read_cpuid_id() & 0x000f0000) == 0x000f0000)
63                 return cpuid_feature_extract(CPUID_EXT_PFR1, 4) ||
64                         cpuid_feature_extract(CPUID_EXT_PFR1, 20);
65         return 0;
66 }
67
68 unsigned long setup_vectors_base(void)
69 {
70         unsigned long base = 0, reg = get_cr();
71
72         set_cr(reg & ~CR_V);
73         if (security_extensions_enabled()) {
74                 if (IS_ENABLED(CONFIG_REMAP_VECTORS_TO_RAM))
75                         base = CONFIG_DRAM_BASE;
76                 set_vbar(base);
77         } else if (IS_ENABLED(CONFIG_REMAP_VECTORS_TO_RAM)) {
78                 if (CONFIG_DRAM_BASE != 0)
79                         pr_err("Security extensions not enabled, vectors cannot be remapped to RAM, vectors base will be 0x00000000\n");
80         }
81
82         return base;
83 }
84 #endif /* CONFIG_CPU_HIGH_VECTOR */
85 #endif /* CONFIG_CPU_CP15 */
86
87 void __init arm_mm_memblock_reserve(void)
88 {
89 #ifndef CONFIG_CPU_V7M
90         vectors_base = IS_ENABLED(CONFIG_CPU_CP15) ? setup_vectors_base() : 0;
91         /*
92          * Register the exception vector page.
93          * some architectures which the DRAM is the exception vector to trap,
94          * alloc_page breaks with error, although it is not NULL, but "0."
95          */
96         memblock_reserve(vectors_base, 2 * PAGE_SIZE);
97 #else /* ifndef CONFIG_CPU_V7M */
98         /*
99          * There is no dedicated vector page on V7-M. So nothing needs to be
100          * reserved here.
101          */
102 #endif
103         /*
104          * In any case, always ensure address 0 is never used as many things
105          * get very confused if 0 is returned as a legitimate address.
106          */
107         memblock_reserve(0, 1);
108 }
109
110 static void __init adjust_lowmem_bounds_mpu(void)
111 {
112         unsigned long pmsa = read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA;
113
114         switch (pmsa) {
115         case MMFR0_PMSAv7:
116                 pmsav7_adjust_lowmem_bounds();
117                 break;
118         case MMFR0_PMSAv8:
119                 pmsav8_adjust_lowmem_bounds();
120                 break;
121         default:
122                 break;
123         }
124 }
125
126 static void __init mpu_setup(void)
127 {
128         unsigned long pmsa = read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA;
129
130         switch (pmsa) {
131         case MMFR0_PMSAv7:
132                 pmsav7_setup();
133                 break;
134         case MMFR0_PMSAv8:
135                 pmsav8_setup();
136                 break;
137         default:
138                 break;
139         }
140 }
141
142 void __init adjust_lowmem_bounds(void)
143 {
144         phys_addr_t end;
145         adjust_lowmem_bounds_mpu();
146         end = memblock_end_of_DRAM();
147         high_memory = __va(end - 1) + 1;
148         memblock_set_current_limit(end);
149 }
150
151 /*
152  * paging_init() sets up the page tables, initialises the zone memory
153  * maps, and sets up the zero page, bad page and bad page tables.
154  */
155 void __init paging_init(const struct machine_desc *mdesc)
156 {
157         void *zero_page;
158
159         early_trap_init((void *)vectors_base);
160         mpu_setup();
161
162         /* allocate the zero page. */
163         zero_page = (void *)memblock_alloc(PAGE_SIZE, PAGE_SIZE);
164         if (!zero_page)
165                 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
166                       __func__, PAGE_SIZE, PAGE_SIZE);
167
168         bootmem_init();
169
170         empty_zero_page = virt_to_page(zero_page);
171         flush_dcache_page(empty_zero_page);
172 }
173
174 /*
175  * We don't need to do anything here for nommu machines.
176  */
177 void setup_mm_for_reboot(void)
178 {
179 }
180
181 void flush_dcache_page(struct page *page)
182 {
183         __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
184 }
185 EXPORT_SYMBOL(flush_dcache_page);
186
187 void flush_kernel_dcache_page(struct page *page)
188 {
189         __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
190 }
191 EXPORT_SYMBOL(flush_kernel_dcache_page);
192
193 void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
194                        unsigned long uaddr, void *dst, const void *src,
195                        unsigned long len)
196 {
197         memcpy(dst, src, len);
198         if (vma->vm_flags & VM_EXEC)
199                 __cpuc_coherent_user_range(uaddr, uaddr + len);
200 }
201
202 void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
203                                 size_t size, unsigned int mtype)
204 {
205         if (pfn >= (0x100000000ULL >> PAGE_SHIFT))
206                 return NULL;
207         return (void __iomem *) (offset + (pfn << PAGE_SHIFT));
208 }
209 EXPORT_SYMBOL(__arm_ioremap_pfn);
210
211 void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
212                                    unsigned int mtype, void *caller)
213 {
214         return (void __iomem *)phys_addr;
215 }
216
217 void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, unsigned int, void *);
218
219 void __iomem *ioremap(resource_size_t res_cookie, size_t size)
220 {
221         return __arm_ioremap_caller(res_cookie, size, MT_DEVICE,
222                                     __builtin_return_address(0));
223 }
224 EXPORT_SYMBOL(ioremap);
225
226 void __iomem *ioremap_cache(resource_size_t res_cookie, size_t size)
227         __alias(ioremap_cached);
228
229 void __iomem *ioremap_cached(resource_size_t res_cookie, size_t size)
230 {
231         return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_CACHED,
232                                     __builtin_return_address(0));
233 }
234 EXPORT_SYMBOL(ioremap_cache);
235 EXPORT_SYMBOL(ioremap_cached);
236
237 void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size)
238 {
239         return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_WC,
240                                     __builtin_return_address(0));
241 }
242 EXPORT_SYMBOL(ioremap_wc);
243
244 #ifdef CONFIG_PCI
245
246 #include <asm/mach/map.h>
247
248 void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size)
249 {
250         return arch_ioremap_caller(res_cookie, size, MT_UNCACHED,
251                                    __builtin_return_address(0));
252 }
253 EXPORT_SYMBOL_GPL(pci_remap_cfgspace);
254 #endif
255
256 void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
257 {
258         return (void *)phys_addr;
259 }
260
261 void __iounmap(volatile void __iomem *addr)
262 {
263 }
264 EXPORT_SYMBOL(__iounmap);
265
266 void (*arch_iounmap)(volatile void __iomem *);
267
268 void iounmap(volatile void __iomem *addr)
269 {
270 }
271 EXPORT_SYMBOL(iounmap);