1
2
3
4
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
22#include "mm.h"
23
24#ifdef CONFIG_ARM_MPU
25struct mpu_rgn_info mpu_rgn_info;
26
27
28static void rgnr_write(u32 v)
29{
30 asm("mcr p15, 0, %0, c6, c2, 0" : : "r" (v));
31}
32
33
34
35
36static void dracr_write(u32 v)
37{
38 asm("mcr p15, 0, %0, c6, c1, 4" : : "r" (v));
39}
40
41
42static void drsr_write(u32 v)
43{
44 asm("mcr p15, 0, %0, c6, c1, 2" : : "r" (v));
45}
46
47
48static void drbar_write(u32 v)
49{
50 asm("mcr p15, 0, %0, c6, c1, 0" : : "r" (v));
51}
52
53static u32 drbar_read(void)
54{
55 u32 v;
56 asm("mrc p15, 0, %0, c6, c1, 0" : "=r" (v));
57 return v;
58}
59
60
61
62static void iracr_write(u32 v)
63{
64 asm("mcr p15, 0, %0, c6, c1, 5" : : "r" (v));
65}
66
67
68static void irsr_write(u32 v)
69{
70 asm("mcr p15, 0, %0, c6, c1, 3" : : "r" (v));
71}
72
73
74static void irbar_write(u32 v)
75{
76 asm("mcr p15, 0, %0, c6, c1, 1" : : "r" (v));
77}
78
79static unsigned long irbar_read(void)
80{
81 unsigned long v;
82 asm("mrc p15, 0, %0, c6, c1, 1" : "=r" (v));
83 return v;
84}
85
86
87void __init sanity_check_meminfo_mpu(void)
88{
89 int i;
90 struct membank *bank = meminfo.bank;
91 phys_addr_t phys_offset = PHYS_OFFSET;
92 phys_addr_t aligned_region_size, specified_mem_size, rounded_mem_size;
93
94
95 if (bank_phys_start(&bank[0]) != phys_offset)
96 panic("First memory bank must be contiguous from PHYS_OFFSET");
97
98
99 for (i = 1; i < meminfo.nr_banks; i++) {
100 if (bank[i].start <= bank_phys_end(&bank[0]) &&
101 bank_phys_end(&bank[i]) > bank_phys_end(&bank[0])) {
102 bank[0].size = bank_phys_end(&bank[i]) - bank[0].start;
103 } else {
104 pr_notice("Ignoring RAM after 0x%.8lx. "
105 "First non-contiguous (ignored) bank start: 0x%.8lx\n",
106 (unsigned long)bank_phys_end(&bank[0]),
107 (unsigned long)bank_phys_start(&bank[i]));
108 break;
109 }
110 }
111
112 meminfo.nr_banks = 1;
113 specified_mem_size = bank[0].size;
114
115
116
117
118
119 if (phys_offset != 0)
120 pr_info("PHYS_OFFSET != 0 => MPU Region size constrained by alignment requirements\n");
121
122
123
124
125
126
127
128 aligned_region_size = (phys_offset - 1) ^ (phys_offset);
129
130 rounded_mem_size = (1 << __fls(bank[0].size)) - 1;
131
132
133 aligned_region_size = aligned_region_size < rounded_mem_size
134 ? aligned_region_size + 1
135 : rounded_mem_size + 1;
136
137 if (aligned_region_size != specified_mem_size)
138 pr_warn("Truncating memory from 0x%.8lx to 0x%.8lx (MPU region constraints)",
139 (unsigned long)specified_mem_size,
140 (unsigned long)aligned_region_size);
141
142 meminfo.bank[0].size = aligned_region_size;
143 pr_debug("MPU Region from 0x%.8lx size 0x%.8lx (end 0x%.8lx))\n",
144 (unsigned long)phys_offset,
145 (unsigned long)aligned_region_size,
146 (unsigned long)bank_phys_end(&bank[0]));
147
148}
149
150static int mpu_present(void)
151{
152 return ((read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA) == MMFR0_PMSAv7);
153}
154
155static int mpu_max_regions(void)
156{
157
158
159
160
161
162 u32 dregions, iregions, mpuir;
163 mpuir = read_cpuid(CPUID_MPUIR);
164
165 dregions = iregions = (mpuir & MPUIR_DREGION_SZMASK) >> MPUIR_DREGION;
166
167
168 if (mpuir & MPUIR_nU)
169 iregions = (mpuir & MPUIR_IREGION_SZMASK) >> MPUIR_IREGION;
170
171
172 return min(dregions, iregions);
173}
174
175static int mpu_iside_independent(void)
176{
177
178 return read_cpuid(CPUID_MPUIR) & MPUIR_nU;
179}
180
181static int mpu_min_region_order(void)
182{
183 u32 drbar_result, irbar_result;
184
185 rgnr_write(MPU_PROBE_REGION);
186 isb();
187
188
189
190
191 drbar_write(0xFFFFFFFC);
192 drbar_result = irbar_result = drbar_read();
193 drbar_write(0x0);
194
195 if (mpu_iside_independent()) {
196 irbar_write(0xFFFFFFFC);
197 irbar_result = irbar_read();
198 irbar_write(0x0);
199 }
200 isb();
201
202 return __ffs(max(drbar_result, irbar_result));
203}
204
205static int mpu_setup_region(unsigned int number, phys_addr_t start,
206 unsigned int size_order, unsigned int properties)
207{
208 u32 size_data;
209
210
211 if (number > mpu_max_regions() || number == MPU_PROBE_REGION)
212 return -ENOENT;
213
214 if (size_order > 32)
215 return -ENOMEM;
216
217 if (size_order < mpu_min_region_order())
218 return -ENOMEM;
219
220
221 size_data = ((size_order - 1) << MPU_RSR_SZ) | 1 << MPU_RSR_EN;
222
223 dsb();
224 rgnr_write(number);
225 isb();
226 drbar_write(start);
227 dracr_write(properties);
228 isb();
229 drsr_write(size_data);
230
231
232 if (mpu_iside_independent()) {
233 irbar_write(start);
234 iracr_write(properties);
235 isb();
236 irsr_write(size_data);
237 }
238 isb();
239
240
241 mpu_rgn_info.rgns[number].dracr = properties;
242 mpu_rgn_info.rgns[number].drbar = start;
243 mpu_rgn_info.rgns[number].drsr = size_data;
244 return 0;
245}
246
247
248
249
250void __init mpu_setup(void)
251{
252 int region_err;
253 if (!mpu_present())
254 return;
255
256 region_err = mpu_setup_region(MPU_RAM_REGION, PHYS_OFFSET,
257 ilog2(meminfo.bank[0].size),
258 MPU_AP_PL1RW_PL0RW | MPU_RGN_NORMAL);
259 if (region_err) {
260 panic("MPU region initialization failure! %d", region_err);
261 } else {
262 pr_info("Using ARMv7 PMSA Compliant MPU. "
263 "Region independence: %s, Max regions: %d\n",
264 mpu_iside_independent() ? "Yes" : "No",
265 mpu_max_regions());
266 }
267}
268#else
269static void sanity_check_meminfo_mpu(void) {}
270static void __init mpu_setup(void) {}
271#endif
272
273void __init arm_mm_memblock_reserve(void)
274{
275#ifndef CONFIG_CPU_V7M
276
277
278
279
280
281 memblock_reserve(CONFIG_VECTORS_BASE, PAGE_SIZE);
282#else
283
284
285
286
287#endif
288}
289
290void __init sanity_check_meminfo(void)
291{
292 phys_addr_t end;
293 sanity_check_meminfo_mpu();
294 end = bank_phys_end(&meminfo.bank[meminfo.nr_banks - 1]);
295 high_memory = __va(end - 1) + 1;
296}
297
298
299
300
301
302void __init paging_init(const struct machine_desc *mdesc)
303{
304 early_trap_init((void *)CONFIG_VECTORS_BASE);
305 mpu_setup();
306 bootmem_init();
307}
308
309
310
311
312void setup_mm_for_reboot(void)
313{
314}
315
316void flush_dcache_page(struct page *page)
317{
318 __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
319}
320EXPORT_SYMBOL(flush_dcache_page);
321
322void flush_kernel_dcache_page(struct page *page)
323{
324 __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
325}
326EXPORT_SYMBOL(flush_kernel_dcache_page);
327
328void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
329 unsigned long uaddr, void *dst, const void *src,
330 unsigned long len)
331{
332 memcpy(dst, src, len);
333 if (vma->vm_flags & VM_EXEC)
334 __cpuc_coherent_user_range(uaddr, uaddr + len);
335}
336
337void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
338 size_t size, unsigned int mtype)
339{
340 if (pfn >= (0x100000000ULL >> PAGE_SHIFT))
341 return NULL;
342 return (void __iomem *) (offset + (pfn << PAGE_SHIFT));
343}
344EXPORT_SYMBOL(__arm_ioremap_pfn);
345
346void __iomem *__arm_ioremap_pfn_caller(unsigned long pfn, unsigned long offset,
347 size_t size, unsigned int mtype, void *caller)
348{
349 return __arm_ioremap_pfn(pfn, offset, size, mtype);
350}
351
352void __iomem *__arm_ioremap(phys_addr_t phys_addr, size_t size,
353 unsigned int mtype)
354{
355 return (void __iomem *)phys_addr;
356}
357EXPORT_SYMBOL(__arm_ioremap);
358
359void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, unsigned int, void *);
360
361void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
362 unsigned int mtype, void *caller)
363{
364 return __arm_ioremap(phys_addr, size, mtype);
365}
366
367void (*arch_iounmap)(volatile void __iomem *);
368
369void __arm_iounmap(volatile void __iomem *addr)
370{
371}
372EXPORT_SYMBOL(__arm_iounmap);
373