Loading...
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/sections.h>
15#include <asm/page.h>
16#include <asm/setup.h>
17#include <asm/traps.h>
18#include <asm/mach/arch.h>
19#include <asm/cputype.h>
20#include <asm/mpu.h>
21#include <asm/procinfo.h>
22
23#include "mm.h"
24
25#ifdef CONFIG_ARM_MPU
26struct mpu_rgn_info mpu_rgn_info;
27
28/* Region number */
29static void rgnr_write(u32 v)
30{
31 asm("mcr p15, 0, %0, c6, c2, 0" : : "r" (v));
32}
33
34/* Data-side / unified region attributes */
35
36/* Region access control register */
37static void dracr_write(u32 v)
38{
39 asm("mcr p15, 0, %0, c6, c1, 4" : : "r" (v));
40}
41
42/* Region size register */
43static void drsr_write(u32 v)
44{
45 asm("mcr p15, 0, %0, c6, c1, 2" : : "r" (v));
46}
47
48/* Region base address register */
49static void drbar_write(u32 v)
50{
51 asm("mcr p15, 0, %0, c6, c1, 0" : : "r" (v));
52}
53
54static u32 drbar_read(void)
55{
56 u32 v;
57 asm("mrc p15, 0, %0, c6, c1, 0" : "=r" (v));
58 return v;
59}
60/* Optional instruction-side region attributes */
61
62/* I-side Region access control register */
63static void iracr_write(u32 v)
64{
65 asm("mcr p15, 0, %0, c6, c1, 5" : : "r" (v));
66}
67
68/* I-side Region size register */
69static void irsr_write(u32 v)
70{
71 asm("mcr p15, 0, %0, c6, c1, 3" : : "r" (v));
72}
73
74/* I-side Region base address register */
75static void irbar_write(u32 v)
76{
77 asm("mcr p15, 0, %0, c6, c1, 1" : : "r" (v));
78}
79
80static unsigned long irbar_read(void)
81{
82 unsigned long v;
83 asm("mrc p15, 0, %0, c6, c1, 1" : "=r" (v));
84 return v;
85}
86
87/* MPU initialisation functions */
88void __init sanity_check_meminfo_mpu(void)
89{
90 int i;
91 struct membank *bank = meminfo.bank;
92 phys_addr_t phys_offset = PHYS_OFFSET;
93 phys_addr_t aligned_region_size, specified_mem_size, rounded_mem_size;
94
95 /* Initially only use memory continuous from PHYS_OFFSET */
96 if (bank_phys_start(&bank[0]) != phys_offset)
97 panic("First memory bank must be contiguous from PHYS_OFFSET");
98
99 /* Banks have already been sorted by start address */
100 for (i = 1; i < meminfo.nr_banks; i++) {
101 if (bank[i].start <= bank_phys_end(&bank[0]) &&
102 bank_phys_end(&bank[i]) > bank_phys_end(&bank[0])) {
103 bank[0].size = bank_phys_end(&bank[i]) - bank[0].start;
104 } else {
105 pr_notice("Ignoring RAM after 0x%.8lx. "
106 "First non-contiguous (ignored) bank start: 0x%.8lx\n",
107 (unsigned long)bank_phys_end(&bank[0]),
108 (unsigned long)bank_phys_start(&bank[i]));
109 break;
110 }
111 }
112 /* All contiguous banks are now merged in to the first bank */
113 meminfo.nr_banks = 1;
114 specified_mem_size = bank[0].size;
115
116 /*
117 * MPU has curious alignment requirements: Size must be power of 2, and
118 * region start must be aligned to the region size
119 */
120 if (phys_offset != 0)
121 pr_info("PHYS_OFFSET != 0 => MPU Region size constrained by alignment requirements\n");
122
123 /*
124 * Maximum aligned region might overflow phys_addr_t if phys_offset is
125 * 0. Hence we keep everything below 4G until we take the smaller of
126 * the aligned_region_size and rounded_mem_size, one of which is
127 * guaranteed to be smaller than the maximum physical address.
128 */
129 aligned_region_size = (phys_offset - 1) ^ (phys_offset);
130 /* Find the max power-of-two sized region that fits inside our bank */
131 rounded_mem_size = (1 << __fls(bank[0].size)) - 1;
132
133 /* The actual region size is the smaller of the two */
134 aligned_region_size = aligned_region_size < rounded_mem_size
135 ? aligned_region_size + 1
136 : rounded_mem_size + 1;
137
138 if (aligned_region_size != specified_mem_size)
139 pr_warn("Truncating memory from 0x%.8lx to 0x%.8lx (MPU region constraints)",
140 (unsigned long)specified_mem_size,
141 (unsigned long)aligned_region_size);
142
143 meminfo.bank[0].size = aligned_region_size;
144 pr_debug("MPU Region from 0x%.8lx size 0x%.8lx (end 0x%.8lx))\n",
145 (unsigned long)phys_offset,
146 (unsigned long)aligned_region_size,
147 (unsigned long)bank_phys_end(&bank[0]));
148
149}
150
151static int mpu_present(void)
152{
153 return ((read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA) == MMFR0_PMSAv7);
154}
155
156static int mpu_max_regions(void)
157{
158 /*
159 * We don't support a different number of I/D side regions so if we
160 * have separate instruction and data memory maps then return
161 * whichever side has a smaller number of supported regions.
162 */
163 u32 dregions, iregions, mpuir;
164 mpuir = read_cpuid(CPUID_MPUIR);
165
166 dregions = iregions = (mpuir & MPUIR_DREGION_SZMASK) >> MPUIR_DREGION;
167
168 /* Check for separate d-side and i-side memory maps */
169 if (mpuir & MPUIR_nU)
170 iregions = (mpuir & MPUIR_IREGION_SZMASK) >> MPUIR_IREGION;
171
172 /* Use the smallest of the two maxima */
173 return min(dregions, iregions);
174}
175
176static int mpu_iside_independent(void)
177{
178 /* MPUIR.nU specifies whether there is *not* a unified memory map */
179 return read_cpuid(CPUID_MPUIR) & MPUIR_nU;
180}
181
182static int mpu_min_region_order(void)
183{
184 u32 drbar_result, irbar_result;
185 /* We've kept a region free for this probing */
186 rgnr_write(MPU_PROBE_REGION);
187 isb();
188 /*
189 * As per ARM ARM, write 0xFFFFFFFC to DRBAR to find the minimum
190 * region order
191 */
192 drbar_write(0xFFFFFFFC);
193 drbar_result = irbar_result = drbar_read();
194 drbar_write(0x0);
195 /* If the MPU is non-unified, we use the larger of the two minima*/
196 if (mpu_iside_independent()) {
197 irbar_write(0xFFFFFFFC);
198 irbar_result = irbar_read();
199 irbar_write(0x0);
200 }
201 isb(); /* Ensure that MPU region operations have completed */
202 /* Return whichever result is larger */
203 return __ffs(max(drbar_result, irbar_result));
204}
205
206static int mpu_setup_region(unsigned int number, phys_addr_t start,
207 unsigned int size_order, unsigned int properties)
208{
209 u32 size_data;
210
211 /* We kept a region free for probing resolution of MPU regions*/
212 if (number > mpu_max_regions() || number == MPU_PROBE_REGION)
213 return -ENOENT;
214
215 if (size_order > 32)
216 return -ENOMEM;
217
218 if (size_order < mpu_min_region_order())
219 return -ENOMEM;
220
221 /* Writing N to bits 5:1 (RSR_SZ) specifies region size 2^N+1 */
222 size_data = ((size_order - 1) << MPU_RSR_SZ) | 1 << MPU_RSR_EN;
223
224 dsb(); /* Ensure all previous data accesses occur with old mappings */
225 rgnr_write(number);
226 isb();
227 drbar_write(start);
228 dracr_write(properties);
229 isb(); /* Propagate properties before enabling region */
230 drsr_write(size_data);
231
232 /* Check for independent I-side registers */
233 if (mpu_iside_independent()) {
234 irbar_write(start);
235 iracr_write(properties);
236 isb();
237 irsr_write(size_data);
238 }
239 isb();
240
241 /* Store region info (we treat i/d side the same, so only store d) */
242 mpu_rgn_info.rgns[number].dracr = properties;
243 mpu_rgn_info.rgns[number].drbar = start;
244 mpu_rgn_info.rgns[number].drsr = size_data;
245 return 0;
246}
247
248/*
249* Set up default MPU regions, doing nothing if there is no MPU
250*/
251void __init mpu_setup(void)
252{
253 int region_err;
254 if (!mpu_present())
255 return;
256
257 region_err = mpu_setup_region(MPU_RAM_REGION, PHYS_OFFSET,
258 ilog2(meminfo.bank[0].size),
259 MPU_AP_PL1RW_PL0RW | MPU_RGN_NORMAL);
260 if (region_err) {
261 panic("MPU region initialization failure! %d", region_err);
262 } else {
263 pr_info("Using ARMv7 PMSA Compliant MPU. "
264 "Region independence: %s, Max regions: %d\n",
265 mpu_iside_independent() ? "Yes" : "No",
266 mpu_max_regions());
267 }
268}
269#else
270static void sanity_check_meminfo_mpu(void) {}
271static void __init mpu_setup(void) {}
272#endif /* CONFIG_ARM_MPU */
273
274void __init arm_mm_memblock_reserve(void)
275{
276#ifndef CONFIG_CPU_V7M
277 /*
278 * Register the exception vector page.
279 * some architectures which the DRAM is the exception vector to trap,
280 * alloc_page breaks with error, although it is not NULL, but "0."
281 */
282 memblock_reserve(CONFIG_VECTORS_BASE, PAGE_SIZE);
283#else /* ifndef CONFIG_CPU_V7M */
284 /*
285 * There is no dedicated vector page on V7-M. So nothing needs to be
286 * reserved here.
287 */
288#endif
289}
290
291void __init sanity_check_meminfo(void)
292{
293 phys_addr_t end;
294 sanity_check_meminfo_mpu();
295 end = bank_phys_end(&meminfo.bank[meminfo.nr_banks - 1]);
296 high_memory = __va(end - 1) + 1;
297}
298
299/*
300 * early_paging_init() recreates boot time page table setup, allowing machines
301 * to switch over to a high (>4G) address space on LPAE systems
302 */
303void __init early_paging_init(const struct machine_desc *mdesc,
304 struct proc_info_list *procinfo)
305{
306}
307
308/*
309 * paging_init() sets up the page tables, initialises the zone memory
310 * maps, and sets up the zero page, bad page and bad page tables.
311 */
312void __init paging_init(const struct machine_desc *mdesc)
313{
314 early_trap_init((void *)CONFIG_VECTORS_BASE);
315 mpu_setup();
316 bootmem_init();
317}
318
319/*
320 * We don't need to do anything here for nommu machines.
321 */
322void setup_mm_for_reboot(void)
323{
324}
325
326void flush_dcache_page(struct page *page)
327{
328 __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
329}
330EXPORT_SYMBOL(flush_dcache_page);
331
332void flush_kernel_dcache_page(struct page *page)
333{
334 __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
335}
336EXPORT_SYMBOL(flush_kernel_dcache_page);
337
338void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
339 unsigned long uaddr, void *dst, const void *src,
340 unsigned long len)
341{
342 memcpy(dst, src, len);
343 if (vma->vm_flags & VM_EXEC)
344 __cpuc_coherent_user_range(uaddr, uaddr + len);
345}
346
347void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
348 size_t size, unsigned int mtype)
349{
350 if (pfn >= (0x100000000ULL >> PAGE_SHIFT))
351 return NULL;
352 return (void __iomem *) (offset + (pfn << PAGE_SHIFT));
353}
354EXPORT_SYMBOL(__arm_ioremap_pfn);
355
356void __iomem *__arm_ioremap_pfn_caller(unsigned long pfn, unsigned long offset,
357 size_t size, unsigned int mtype, void *caller)
358{
359 return __arm_ioremap_pfn(pfn, offset, size, mtype);
360}
361
362void __iomem *__arm_ioremap(phys_addr_t phys_addr, size_t size,
363 unsigned int mtype)
364{
365 return (void __iomem *)phys_addr;
366}
367EXPORT_SYMBOL(__arm_ioremap);
368
369void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, unsigned int, void *);
370
371void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
372 unsigned int mtype, void *caller)
373{
374 return __arm_ioremap(phys_addr, size, mtype);
375}
376
377void (*arch_iounmap)(volatile void __iomem *);
378
379void __arm_iounmap(volatile void __iomem *addr)
380{
381}
382EXPORT_SYMBOL(__arm_iounmap);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/arch/arm/mm/nommu.c
4 *
5 * ARM uCLinux supporting functions.
6 */
7#include <linux/module.h>
8#include <linux/mm.h>
9#include <linux/pagemap.h>
10#include <linux/io.h>
11#include <linux/memblock.h>
12#include <linux/kernel.h>
13
14#include <asm/cacheflush.h>
15#include <asm/cp15.h>
16#include <asm/sections.h>
17#include <asm/page.h>
18#include <asm/setup.h>
19#include <asm/traps.h>
20#include <asm/mach/arch.h>
21#include <asm/cputype.h>
22#include <asm/mpu.h>
23#include <asm/procinfo.h>
24
25#include "mm.h"
26
27unsigned long vectors_base;
28
29#ifdef CONFIG_ARM_MPU
30struct mpu_rgn_info mpu_rgn_info;
31#endif
32
33#ifdef CONFIG_CPU_CP15
34#ifdef CONFIG_CPU_HIGH_VECTOR
35unsigned long setup_vectors_base(void)
36{
37 unsigned long reg = get_cr();
38
39 set_cr(reg | CR_V);
40 return 0xffff0000;
41}
42#else /* CONFIG_CPU_HIGH_VECTOR */
43/* Write exception base address to VBAR */
44static inline void set_vbar(unsigned long val)
45{
46 asm("mcr p15, 0, %0, c12, c0, 0" : : "r" (val) : "cc");
47}
48
49/*
50 * Security extensions, bits[7:4], permitted values,
51 * 0b0000 - not implemented, 0b0001/0b0010 - implemented
52 */
53static inline bool security_extensions_enabled(void)
54{
55 /* Check CPUID Identification Scheme before ID_PFR1 read */
56 if ((read_cpuid_id() & 0x000f0000) == 0x000f0000)
57 return cpuid_feature_extract(CPUID_EXT_PFR1, 4) ||
58 cpuid_feature_extract(CPUID_EXT_PFR1, 20);
59 return 0;
60}
61
62unsigned long setup_vectors_base(void)
63{
64 unsigned long base = 0, reg = get_cr();
65
66 set_cr(reg & ~CR_V);
67 if (security_extensions_enabled()) {
68 if (IS_ENABLED(CONFIG_REMAP_VECTORS_TO_RAM))
69 base = CONFIG_DRAM_BASE;
70 set_vbar(base);
71 } else if (IS_ENABLED(CONFIG_REMAP_VECTORS_TO_RAM)) {
72 if (CONFIG_DRAM_BASE != 0)
73 pr_err("Security extensions not enabled, vectors cannot be remapped to RAM, vectors base will be 0x00000000\n");
74 }
75
76 return base;
77}
78#endif /* CONFIG_CPU_HIGH_VECTOR */
79#endif /* CONFIG_CPU_CP15 */
80
81void __init arm_mm_memblock_reserve(void)
82{
83#ifndef CONFIG_CPU_V7M
84 vectors_base = IS_ENABLED(CONFIG_CPU_CP15) ? setup_vectors_base() : 0;
85 /*
86 * Register the exception vector page.
87 * some architectures which the DRAM is the exception vector to trap,
88 * alloc_page breaks with error, although it is not NULL, but "0."
89 */
90 memblock_reserve(vectors_base, 2 * PAGE_SIZE);
91#else /* ifndef CONFIG_CPU_V7M */
92 /*
93 * There is no dedicated vector page on V7-M. So nothing needs to be
94 * reserved here.
95 */
96#endif
97 /*
98 * In any case, always ensure address 0 is never used as many things
99 * get very confused if 0 is returned as a legitimate address.
100 */
101 memblock_reserve(0, 1);
102}
103
104static void __init adjust_lowmem_bounds_mpu(void)
105{
106 unsigned long pmsa = read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA;
107
108 switch (pmsa) {
109 case MMFR0_PMSAv7:
110 pmsav7_adjust_lowmem_bounds();
111 break;
112 case MMFR0_PMSAv8:
113 pmsav8_adjust_lowmem_bounds();
114 break;
115 default:
116 break;
117 }
118}
119
120static void __init mpu_setup(void)
121{
122 unsigned long pmsa = read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA;
123
124 switch (pmsa) {
125 case MMFR0_PMSAv7:
126 pmsav7_setup();
127 break;
128 case MMFR0_PMSAv8:
129 pmsav8_setup();
130 break;
131 default:
132 break;
133 }
134}
135
136void __init adjust_lowmem_bounds(void)
137{
138 phys_addr_t end;
139 adjust_lowmem_bounds_mpu();
140 end = memblock_end_of_DRAM();
141 high_memory = __va(end - 1) + 1;
142 memblock_set_current_limit(end);
143}
144
145/*
146 * paging_init() sets up the page tables, initialises the zone memory
147 * maps, and sets up the zero page, bad page and bad page tables.
148 */
149void __init paging_init(const struct machine_desc *mdesc)
150{
151 early_trap_init((void *)vectors_base);
152 mpu_setup();
153 bootmem_init();
154}
155
156/*
157 * We don't need to do anything here for nommu machines.
158 */
159void setup_mm_for_reboot(void)
160{
161}
162
163void flush_dcache_page(struct page *page)
164{
165 __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
166}
167EXPORT_SYMBOL(flush_dcache_page);
168
169void flush_kernel_dcache_page(struct page *page)
170{
171 __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
172}
173EXPORT_SYMBOL(flush_kernel_dcache_page);
174
175void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
176 unsigned long uaddr, void *dst, const void *src,
177 unsigned long len)
178{
179 memcpy(dst, src, len);
180 if (vma->vm_flags & VM_EXEC)
181 __cpuc_coherent_user_range(uaddr, uaddr + len);
182}
183
184void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
185 size_t size, unsigned int mtype)
186{
187 if (pfn >= (0x100000000ULL >> PAGE_SHIFT))
188 return NULL;
189 return (void __iomem *) (offset + (pfn << PAGE_SHIFT));
190}
191EXPORT_SYMBOL(__arm_ioremap_pfn);
192
193void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
194 unsigned int mtype, void *caller)
195{
196 return (void __iomem *)phys_addr;
197}
198
199void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, unsigned int, void *);
200
201void __iomem *ioremap(resource_size_t res_cookie, size_t size)
202{
203 return __arm_ioremap_caller(res_cookie, size, MT_DEVICE,
204 __builtin_return_address(0));
205}
206EXPORT_SYMBOL(ioremap);
207
208void __iomem *ioremap_cache(resource_size_t res_cookie, size_t size)
209 __alias(ioremap_cached);
210
211void __iomem *ioremap_cached(resource_size_t res_cookie, size_t size)
212{
213 return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_CACHED,
214 __builtin_return_address(0));
215}
216EXPORT_SYMBOL(ioremap_cache);
217EXPORT_SYMBOL(ioremap_cached);
218
219void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size)
220{
221 return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_WC,
222 __builtin_return_address(0));
223}
224EXPORT_SYMBOL(ioremap_wc);
225
226#ifdef CONFIG_PCI
227
228#include <asm/mach/map.h>
229
230void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size)
231{
232 return arch_ioremap_caller(res_cookie, size, MT_UNCACHED,
233 __builtin_return_address(0));
234}
235EXPORT_SYMBOL_GPL(pci_remap_cfgspace);
236#endif
237
238void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
239{
240 return (void *)phys_addr;
241}
242
243void __iounmap(volatile void __iomem *addr)
244{
245}
246EXPORT_SYMBOL(__iounmap);
247
248void (*arch_iounmap)(volatile void __iomem *);
249
250void iounmap(volatile void __iomem *addr)
251{
252}
253EXPORT_SYMBOL(iounmap);