xref: /openbmc/linux/arch/arm/mm/mmu.c (revision 1c2f87c2)
1 /*
2  *  linux/arch/arm/mm/mmu.c
3  *
4  *  Copyright (C) 1995-2005 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/mman.h>
15 #include <linux/nodemask.h>
16 #include <linux/memblock.h>
17 #include <linux/fs.h>
18 #include <linux/vmalloc.h>
19 #include <linux/sizes.h>
20 
21 #include <asm/cp15.h>
22 #include <asm/cputype.h>
23 #include <asm/sections.h>
24 #include <asm/cachetype.h>
25 #include <asm/sections.h>
26 #include <asm/setup.h>
27 #include <asm/smp_plat.h>
28 #include <asm/tlb.h>
29 #include <asm/highmem.h>
30 #include <asm/system_info.h>
31 #include <asm/traps.h>
32 #include <asm/procinfo.h>
33 #include <asm/memory.h>
34 
35 #include <asm/mach/arch.h>
36 #include <asm/mach/map.h>
37 #include <asm/mach/pci.h>
38 #include <asm/fixmap.h>
39 
40 #include "mm.h"
41 #include "tcm.h"
42 
43 /*
44  * empty_zero_page is a special page that is used for
45  * zero-initialized data and COW.
46  */
47 struct page *empty_zero_page;
48 EXPORT_SYMBOL(empty_zero_page);
49 
50 /*
51  * The pmd table for the upper-most set of pages.
52  */
53 pmd_t *top_pmd;
54 
55 #define CPOLICY_UNCACHED	0
56 #define CPOLICY_BUFFERED	1
57 #define CPOLICY_WRITETHROUGH	2
58 #define CPOLICY_WRITEBACK	3
59 #define CPOLICY_WRITEALLOC	4
60 
61 static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
62 static unsigned int ecc_mask __initdata = 0;
63 pgprot_t pgprot_user;
64 pgprot_t pgprot_kernel;
65 pgprot_t pgprot_hyp_device;
66 pgprot_t pgprot_s2;
67 pgprot_t pgprot_s2_device;
68 
69 EXPORT_SYMBOL(pgprot_user);
70 EXPORT_SYMBOL(pgprot_kernel);
71 
72 struct cachepolicy {
73 	const char	policy[16];
74 	unsigned int	cr_mask;
75 	pmdval_t	pmd;
76 	pteval_t	pte;
77 	pteval_t	pte_s2;
78 };
79 
80 #ifdef CONFIG_ARM_LPAE
81 #define s2_policy(policy)	policy
82 #else
83 #define s2_policy(policy)	0
84 #endif
85 
86 static struct cachepolicy cache_policies[] __initdata = {
87 	{
88 		.policy		= "uncached",
89 		.cr_mask	= CR_W|CR_C,
90 		.pmd		= PMD_SECT_UNCACHED,
91 		.pte		= L_PTE_MT_UNCACHED,
92 		.pte_s2		= s2_policy(L_PTE_S2_MT_UNCACHED),
93 	}, {
94 		.policy		= "buffered",
95 		.cr_mask	= CR_C,
96 		.pmd		= PMD_SECT_BUFFERED,
97 		.pte		= L_PTE_MT_BUFFERABLE,
98 		.pte_s2		= s2_policy(L_PTE_S2_MT_UNCACHED),
99 	}, {
100 		.policy		= "writethrough",
101 		.cr_mask	= 0,
102 		.pmd		= PMD_SECT_WT,
103 		.pte		= L_PTE_MT_WRITETHROUGH,
104 		.pte_s2		= s2_policy(L_PTE_S2_MT_WRITETHROUGH),
105 	}, {
106 		.policy		= "writeback",
107 		.cr_mask	= 0,
108 		.pmd		= PMD_SECT_WB,
109 		.pte		= L_PTE_MT_WRITEBACK,
110 		.pte_s2		= s2_policy(L_PTE_S2_MT_WRITEBACK),
111 	}, {
112 		.policy		= "writealloc",
113 		.cr_mask	= 0,
114 		.pmd		= PMD_SECT_WBWA,
115 		.pte		= L_PTE_MT_WRITEALLOC,
116 		.pte_s2		= s2_policy(L_PTE_S2_MT_WRITEBACK),
117 	}
118 };
119 
120 #ifdef CONFIG_CPU_CP15
121 /*
122  * These are useful for identifying cache coherency
123  * problems by allowing the cache or the cache and
124  * writebuffer to be turned off.  (Note: the write
125  * buffer should not be on and the cache off).
126  */
127 static int __init early_cachepolicy(char *p)
128 {
129 	int i;
130 
131 	for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
132 		int len = strlen(cache_policies[i].policy);
133 
134 		if (memcmp(p, cache_policies[i].policy, len) == 0) {
135 			cachepolicy = i;
136 			cr_alignment &= ~cache_policies[i].cr_mask;
137 			cr_no_alignment &= ~cache_policies[i].cr_mask;
138 			break;
139 		}
140 	}
141 	if (i == ARRAY_SIZE(cache_policies))
142 		printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
143 	/*
144 	 * This restriction is partly to do with the way we boot; it is
145 	 * unpredictable to have memory mapped using two different sets of
146 	 * memory attributes (shared, type, and cache attribs).  We can not
147 	 * change these attributes once the initial assembly has setup the
148 	 * page tables.
149 	 */
150 	if (cpu_architecture() >= CPU_ARCH_ARMv6) {
151 		printk(KERN_WARNING "Only cachepolicy=writeback supported on ARMv6 and later\n");
152 		cachepolicy = CPOLICY_WRITEBACK;
153 	}
154 	flush_cache_all();
155 	set_cr(cr_alignment);
156 	return 0;
157 }
158 early_param("cachepolicy", early_cachepolicy);
159 
160 static int __init early_nocache(char *__unused)
161 {
162 	char *p = "buffered";
163 	printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
164 	early_cachepolicy(p);
165 	return 0;
166 }
167 early_param("nocache", early_nocache);
168 
169 static int __init early_nowrite(char *__unused)
170 {
171 	char *p = "uncached";
172 	printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
173 	early_cachepolicy(p);
174 	return 0;
175 }
176 early_param("nowb", early_nowrite);
177 
178 #ifndef CONFIG_ARM_LPAE
179 static int __init early_ecc(char *p)
180 {
181 	if (memcmp(p, "on", 2) == 0)
182 		ecc_mask = PMD_PROTECTION;
183 	else if (memcmp(p, "off", 3) == 0)
184 		ecc_mask = 0;
185 	return 0;
186 }
187 early_param("ecc", early_ecc);
188 #endif
189 
190 static int __init noalign_setup(char *__unused)
191 {
192 	cr_alignment &= ~CR_A;
193 	cr_no_alignment &= ~CR_A;
194 	set_cr(cr_alignment);
195 	return 1;
196 }
197 __setup("noalign", noalign_setup);
198 
199 #ifndef CONFIG_SMP
200 void adjust_cr(unsigned long mask, unsigned long set)
201 {
202 	unsigned long flags;
203 
204 	mask &= ~CR_A;
205 
206 	set &= mask;
207 
208 	local_irq_save(flags);
209 
210 	cr_no_alignment = (cr_no_alignment & ~mask) | set;
211 	cr_alignment = (cr_alignment & ~mask) | set;
212 
213 	set_cr((get_cr() & ~mask) | set);
214 
215 	local_irq_restore(flags);
216 }
217 #endif
218 
219 #else /* ifdef CONFIG_CPU_CP15 */
220 
221 static int __init early_cachepolicy(char *p)
222 {
223 	pr_warning("cachepolicy kernel parameter not supported without cp15\n");
224 }
225 early_param("cachepolicy", early_cachepolicy);
226 
227 static int __init noalign_setup(char *__unused)
228 {
229 	pr_warning("noalign kernel parameter not supported without cp15\n");
230 }
231 __setup("noalign", noalign_setup);
232 
233 #endif /* ifdef CONFIG_CPU_CP15 / else */
234 
235 #define PROT_PTE_DEVICE		L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_XN
236 #define PROT_PTE_S2_DEVICE	PROT_PTE_DEVICE
237 #define PROT_SECT_DEVICE	PMD_TYPE_SECT|PMD_SECT_AP_WRITE
238 
239 static struct mem_type mem_types[] = {
240 	[MT_DEVICE] = {		  /* Strongly ordered / ARMv6 shared device */
241 		.prot_pte	= PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
242 				  L_PTE_SHARED,
243 		.prot_pte_s2	= s2_policy(PROT_PTE_S2_DEVICE) |
244 				  s2_policy(L_PTE_S2_MT_DEV_SHARED) |
245 				  L_PTE_SHARED,
246 		.prot_l1	= PMD_TYPE_TABLE,
247 		.prot_sect	= PROT_SECT_DEVICE | PMD_SECT_S,
248 		.domain		= DOMAIN_IO,
249 	},
250 	[MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
251 		.prot_pte	= PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
252 		.prot_l1	= PMD_TYPE_TABLE,
253 		.prot_sect	= PROT_SECT_DEVICE,
254 		.domain		= DOMAIN_IO,
255 	},
256 	[MT_DEVICE_CACHED] = {	  /* ioremap_cached */
257 		.prot_pte	= PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
258 		.prot_l1	= PMD_TYPE_TABLE,
259 		.prot_sect	= PROT_SECT_DEVICE | PMD_SECT_WB,
260 		.domain		= DOMAIN_IO,
261 	},
262 	[MT_DEVICE_WC] = {	/* ioremap_wc */
263 		.prot_pte	= PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
264 		.prot_l1	= PMD_TYPE_TABLE,
265 		.prot_sect	= PROT_SECT_DEVICE,
266 		.domain		= DOMAIN_IO,
267 	},
268 	[MT_UNCACHED] = {
269 		.prot_pte	= PROT_PTE_DEVICE,
270 		.prot_l1	= PMD_TYPE_TABLE,
271 		.prot_sect	= PMD_TYPE_SECT | PMD_SECT_XN,
272 		.domain		= DOMAIN_IO,
273 	},
274 	[MT_CACHECLEAN] = {
275 		.prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
276 		.domain    = DOMAIN_KERNEL,
277 	},
278 #ifndef CONFIG_ARM_LPAE
279 	[MT_MINICLEAN] = {
280 		.prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
281 		.domain    = DOMAIN_KERNEL,
282 	},
283 #endif
284 	[MT_LOW_VECTORS] = {
285 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
286 				L_PTE_RDONLY,
287 		.prot_l1   = PMD_TYPE_TABLE,
288 		.domain    = DOMAIN_USER,
289 	},
290 	[MT_HIGH_VECTORS] = {
291 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
292 				L_PTE_USER | L_PTE_RDONLY,
293 		.prot_l1   = PMD_TYPE_TABLE,
294 		.domain    = DOMAIN_USER,
295 	},
296 	[MT_MEMORY_RWX] = {
297 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
298 		.prot_l1   = PMD_TYPE_TABLE,
299 		.prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
300 		.domain    = DOMAIN_KERNEL,
301 	},
302 	[MT_MEMORY_RW] = {
303 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
304 			     L_PTE_XN,
305 		.prot_l1   = PMD_TYPE_TABLE,
306 		.prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
307 		.domain    = DOMAIN_KERNEL,
308 	},
309 	[MT_ROM] = {
310 		.prot_sect = PMD_TYPE_SECT,
311 		.domain    = DOMAIN_KERNEL,
312 	},
313 	[MT_MEMORY_RWX_NONCACHED] = {
314 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
315 				L_PTE_MT_BUFFERABLE,
316 		.prot_l1   = PMD_TYPE_TABLE,
317 		.prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
318 		.domain    = DOMAIN_KERNEL,
319 	},
320 	[MT_MEMORY_RW_DTCM] = {
321 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
322 				L_PTE_XN,
323 		.prot_l1   = PMD_TYPE_TABLE,
324 		.prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
325 		.domain    = DOMAIN_KERNEL,
326 	},
327 	[MT_MEMORY_RWX_ITCM] = {
328 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
329 		.prot_l1   = PMD_TYPE_TABLE,
330 		.domain    = DOMAIN_KERNEL,
331 	},
332 	[MT_MEMORY_RW_SO] = {
333 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
334 				L_PTE_MT_UNCACHED | L_PTE_XN,
335 		.prot_l1   = PMD_TYPE_TABLE,
336 		.prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_S |
337 				PMD_SECT_UNCACHED | PMD_SECT_XN,
338 		.domain    = DOMAIN_KERNEL,
339 	},
340 	[MT_MEMORY_DMA_READY] = {
341 		.prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
342 				L_PTE_XN,
343 		.prot_l1   = PMD_TYPE_TABLE,
344 		.domain    = DOMAIN_KERNEL,
345 	},
346 };
347 
348 const struct mem_type *get_mem_type(unsigned int type)
349 {
350 	return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
351 }
352 EXPORT_SYMBOL(get_mem_type);
353 
354 #define PTE_SET_FN(_name, pteop) \
355 static int pte_set_##_name(pte_t *ptep, pgtable_t token, unsigned long addr, \
356 			void *data) \
357 { \
358 	pte_t pte = pteop(*ptep); \
359 \
360 	set_pte_ext(ptep, pte, 0); \
361 	return 0; \
362 } \
363 
364 #define SET_MEMORY_FN(_name, callback) \
365 int set_memory_##_name(unsigned long addr, int numpages) \
366 { \
367 	unsigned long start = addr; \
368 	unsigned long size = PAGE_SIZE*numpages; \
369 	unsigned end = start + size; \
370 \
371 	if (start < MODULES_VADDR || start >= MODULES_END) \
372 		return -EINVAL;\
373 \
374 	if (end < MODULES_VADDR || end >= MODULES_END) \
375 		return -EINVAL; \
376 \
377 	apply_to_page_range(&init_mm, start, size, callback, NULL); \
378 	flush_tlb_kernel_range(start, end); \
379 	return 0;\
380 }
381 
382 PTE_SET_FN(ro, pte_wrprotect)
383 PTE_SET_FN(rw, pte_mkwrite)
384 PTE_SET_FN(x, pte_mkexec)
385 PTE_SET_FN(nx, pte_mknexec)
386 
387 SET_MEMORY_FN(ro, pte_set_ro)
388 SET_MEMORY_FN(rw, pte_set_rw)
389 SET_MEMORY_FN(x, pte_set_x)
390 SET_MEMORY_FN(nx, pte_set_nx)
391 
392 /*
393  * Adjust the PMD section entries according to the CPU in use.
394  */
395 static void __init build_mem_type_table(void)
396 {
397 	struct cachepolicy *cp;
398 	unsigned int cr = get_cr();
399 	pteval_t user_pgprot, kern_pgprot, vecs_pgprot;
400 	pteval_t hyp_device_pgprot, s2_pgprot, s2_device_pgprot;
401 	int cpu_arch = cpu_architecture();
402 	int i;
403 
404 	if (cpu_arch < CPU_ARCH_ARMv6) {
405 #if defined(CONFIG_CPU_DCACHE_DISABLE)
406 		if (cachepolicy > CPOLICY_BUFFERED)
407 			cachepolicy = CPOLICY_BUFFERED;
408 #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
409 		if (cachepolicy > CPOLICY_WRITETHROUGH)
410 			cachepolicy = CPOLICY_WRITETHROUGH;
411 #endif
412 	}
413 	if (cpu_arch < CPU_ARCH_ARMv5) {
414 		if (cachepolicy >= CPOLICY_WRITEALLOC)
415 			cachepolicy = CPOLICY_WRITEBACK;
416 		ecc_mask = 0;
417 	}
418 	if (is_smp())
419 		cachepolicy = CPOLICY_WRITEALLOC;
420 
421 	/*
422 	 * Strip out features not present on earlier architectures.
423 	 * Pre-ARMv5 CPUs don't have TEX bits.  Pre-ARMv6 CPUs or those
424 	 * without extended page tables don't have the 'Shared' bit.
425 	 */
426 	if (cpu_arch < CPU_ARCH_ARMv5)
427 		for (i = 0; i < ARRAY_SIZE(mem_types); i++)
428 			mem_types[i].prot_sect &= ~PMD_SECT_TEX(7);
429 	if ((cpu_arch < CPU_ARCH_ARMv6 || !(cr & CR_XP)) && !cpu_is_xsc3())
430 		for (i = 0; i < ARRAY_SIZE(mem_types); i++)
431 			mem_types[i].prot_sect &= ~PMD_SECT_S;
432 
433 	/*
434 	 * ARMv5 and lower, bit 4 must be set for page tables (was: cache
435 	 * "update-able on write" bit on ARM610).  However, Xscale and
436 	 * Xscale3 require this bit to be cleared.
437 	 */
438 	if (cpu_is_xscale() || cpu_is_xsc3()) {
439 		for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
440 			mem_types[i].prot_sect &= ~PMD_BIT4;
441 			mem_types[i].prot_l1 &= ~PMD_BIT4;
442 		}
443 	} else if (cpu_arch < CPU_ARCH_ARMv6) {
444 		for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
445 			if (mem_types[i].prot_l1)
446 				mem_types[i].prot_l1 |= PMD_BIT4;
447 			if (mem_types[i].prot_sect)
448 				mem_types[i].prot_sect |= PMD_BIT4;
449 		}
450 	}
451 
452 	/*
453 	 * Mark the device areas according to the CPU/architecture.
454 	 */
455 	if (cpu_is_xsc3() || (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP))) {
456 		if (!cpu_is_xsc3()) {
457 			/*
458 			 * Mark device regions on ARMv6+ as execute-never
459 			 * to prevent speculative instruction fetches.
460 			 */
461 			mem_types[MT_DEVICE].prot_sect |= PMD_SECT_XN;
462 			mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_XN;
463 			mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_XN;
464 			mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_XN;
465 
466 			/* Also setup NX memory mapping */
467 			mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_XN;
468 		}
469 		if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
470 			/*
471 			 * For ARMv7 with TEX remapping,
472 			 * - shared device is SXCB=1100
473 			 * - nonshared device is SXCB=0100
474 			 * - write combine device mem is SXCB=0001
475 			 * (Uncached Normal memory)
476 			 */
477 			mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1);
478 			mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(1);
479 			mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
480 		} else if (cpu_is_xsc3()) {
481 			/*
482 			 * For Xscale3,
483 			 * - shared device is TEXCB=00101
484 			 * - nonshared device is TEXCB=01000
485 			 * - write combine device mem is TEXCB=00100
486 			 * (Inner/Outer Uncacheable in xsc3 parlance)
487 			 */
488 			mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1) | PMD_SECT_BUFFERED;
489 			mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
490 			mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
491 		} else {
492 			/*
493 			 * For ARMv6 and ARMv7 without TEX remapping,
494 			 * - shared device is TEXCB=00001
495 			 * - nonshared device is TEXCB=01000
496 			 * - write combine device mem is TEXCB=00100
497 			 * (Uncached Normal in ARMv6 parlance).
498 			 */
499 			mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
500 			mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
501 			mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
502 		}
503 	} else {
504 		/*
505 		 * On others, write combining is "Uncached/Buffered"
506 		 */
507 		mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
508 	}
509 
510 	/*
511 	 * Now deal with the memory-type mappings
512 	 */
513 	cp = &cache_policies[cachepolicy];
514 	vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
515 	s2_pgprot = cp->pte_s2;
516 	hyp_device_pgprot = mem_types[MT_DEVICE].prot_pte;
517 	s2_device_pgprot = mem_types[MT_DEVICE].prot_pte_s2;
518 
519 	/*
520 	 * We don't use domains on ARMv6 (since this causes problems with
521 	 * v6/v7 kernels), so we must use a separate memory type for user
522 	 * r/o, kernel r/w to map the vectors page.
523 	 */
524 #ifndef CONFIG_ARM_LPAE
525 	if (cpu_arch == CPU_ARCH_ARMv6)
526 		vecs_pgprot |= L_PTE_MT_VECTORS;
527 #endif
528 
529 	/*
530 	 * ARMv6 and above have extended page tables.
531 	 */
532 	if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
533 #ifndef CONFIG_ARM_LPAE
534 		/*
535 		 * Mark cache clean areas and XIP ROM read only
536 		 * from SVC mode and no access from userspace.
537 		 */
538 		mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
539 		mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
540 		mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
541 #endif
542 
543 		if (is_smp()) {
544 			/*
545 			 * Mark memory with the "shared" attribute
546 			 * for SMP systems
547 			 */
548 			user_pgprot |= L_PTE_SHARED;
549 			kern_pgprot |= L_PTE_SHARED;
550 			vecs_pgprot |= L_PTE_SHARED;
551 			s2_pgprot |= L_PTE_SHARED;
552 			mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_S;
553 			mem_types[MT_DEVICE_WC].prot_pte |= L_PTE_SHARED;
554 			mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_S;
555 			mem_types[MT_DEVICE_CACHED].prot_pte |= L_PTE_SHARED;
556 			mem_types[MT_MEMORY_RWX].prot_sect |= PMD_SECT_S;
557 			mem_types[MT_MEMORY_RWX].prot_pte |= L_PTE_SHARED;
558 			mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_S;
559 			mem_types[MT_MEMORY_RW].prot_pte |= L_PTE_SHARED;
560 			mem_types[MT_MEMORY_DMA_READY].prot_pte |= L_PTE_SHARED;
561 			mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_S;
562 			mem_types[MT_MEMORY_RWX_NONCACHED].prot_pte |= L_PTE_SHARED;
563 		}
564 	}
565 
566 	/*
567 	 * Non-cacheable Normal - intended for memory areas that must
568 	 * not cause dirty cache line writebacks when used
569 	 */
570 	if (cpu_arch >= CPU_ARCH_ARMv6) {
571 		if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
572 			/* Non-cacheable Normal is XCB = 001 */
573 			mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
574 				PMD_SECT_BUFFERED;
575 		} else {
576 			/* For both ARMv6 and non-TEX-remapping ARMv7 */
577 			mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
578 				PMD_SECT_TEX(1);
579 		}
580 	} else {
581 		mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_BUFFERABLE;
582 	}
583 
584 #ifdef CONFIG_ARM_LPAE
585 	/*
586 	 * Do not generate access flag faults for the kernel mappings.
587 	 */
588 	for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
589 		mem_types[i].prot_pte |= PTE_EXT_AF;
590 		if (mem_types[i].prot_sect)
591 			mem_types[i].prot_sect |= PMD_SECT_AF;
592 	}
593 	kern_pgprot |= PTE_EXT_AF;
594 	vecs_pgprot |= PTE_EXT_AF;
595 #endif
596 
597 	for (i = 0; i < 16; i++) {
598 		pteval_t v = pgprot_val(protection_map[i]);
599 		protection_map[i] = __pgprot(v | user_pgprot);
600 	}
601 
602 	mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
603 	mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
604 
605 	pgprot_user   = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
606 	pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
607 				 L_PTE_DIRTY | kern_pgprot);
608 	pgprot_s2  = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | s2_pgprot);
609 	pgprot_s2_device  = __pgprot(s2_device_pgprot);
610 	pgprot_hyp_device  = __pgprot(hyp_device_pgprot);
611 
612 	mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
613 	mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
614 	mem_types[MT_MEMORY_RWX].prot_sect |= ecc_mask | cp->pmd;
615 	mem_types[MT_MEMORY_RWX].prot_pte |= kern_pgprot;
616 	mem_types[MT_MEMORY_RW].prot_sect |= ecc_mask | cp->pmd;
617 	mem_types[MT_MEMORY_RW].prot_pte |= kern_pgprot;
618 	mem_types[MT_MEMORY_DMA_READY].prot_pte |= kern_pgprot;
619 	mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= ecc_mask;
620 	mem_types[MT_ROM].prot_sect |= cp->pmd;
621 
622 	switch (cp->pmd) {
623 	case PMD_SECT_WT:
624 		mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
625 		break;
626 	case PMD_SECT_WB:
627 	case PMD_SECT_WBWA:
628 		mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
629 		break;
630 	}
631 	pr_info("Memory policy: %sData cache %s\n",
632 		ecc_mask ? "ECC enabled, " : "", cp->policy);
633 
634 	for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
635 		struct mem_type *t = &mem_types[i];
636 		if (t->prot_l1)
637 			t->prot_l1 |= PMD_DOMAIN(t->domain);
638 		if (t->prot_sect)
639 			t->prot_sect |= PMD_DOMAIN(t->domain);
640 	}
641 }
642 
643 #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
644 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
645 			      unsigned long size, pgprot_t vma_prot)
646 {
647 	if (!pfn_valid(pfn))
648 		return pgprot_noncached(vma_prot);
649 	else if (file->f_flags & O_SYNC)
650 		return pgprot_writecombine(vma_prot);
651 	return vma_prot;
652 }
653 EXPORT_SYMBOL(phys_mem_access_prot);
654 #endif
655 
656 #define vectors_base()	(vectors_high() ? 0xffff0000 : 0)
657 
658 static void __init *early_alloc_aligned(unsigned long sz, unsigned long align)
659 {
660 	void *ptr = __va(memblock_alloc(sz, align));
661 	memset(ptr, 0, sz);
662 	return ptr;
663 }
664 
665 static void __init *early_alloc(unsigned long sz)
666 {
667 	return early_alloc_aligned(sz, sz);
668 }
669 
670 static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr, unsigned long prot)
671 {
672 	if (pmd_none(*pmd)) {
673 		pte_t *pte = early_alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);
674 		__pmd_populate(pmd, __pa(pte), prot);
675 	}
676 	BUG_ON(pmd_bad(*pmd));
677 	return pte_offset_kernel(pmd, addr);
678 }
679 
680 static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
681 				  unsigned long end, unsigned long pfn,
682 				  const struct mem_type *type)
683 {
684 	pte_t *pte = early_pte_alloc(pmd, addr, type->prot_l1);
685 	do {
686 		set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), 0);
687 		pfn++;
688 	} while (pte++, addr += PAGE_SIZE, addr != end);
689 }
690 
691 static void __init __map_init_section(pmd_t *pmd, unsigned long addr,
692 			unsigned long end, phys_addr_t phys,
693 			const struct mem_type *type)
694 {
695 	pmd_t *p = pmd;
696 
697 #ifndef CONFIG_ARM_LPAE
698 	/*
699 	 * In classic MMU format, puds and pmds are folded in to
700 	 * the pgds. pmd_offset gives the PGD entry. PGDs refer to a
701 	 * group of L1 entries making up one logical pointer to
702 	 * an L2 table (2MB), where as PMDs refer to the individual
703 	 * L1 entries (1MB). Hence increment to get the correct
704 	 * offset for odd 1MB sections.
705 	 * (See arch/arm/include/asm/pgtable-2level.h)
706 	 */
707 	if (addr & SECTION_SIZE)
708 		pmd++;
709 #endif
710 	do {
711 		*pmd = __pmd(phys | type->prot_sect);
712 		phys += SECTION_SIZE;
713 	} while (pmd++, addr += SECTION_SIZE, addr != end);
714 
715 	flush_pmd_entry(p);
716 }
717 
718 static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
719 				      unsigned long end, phys_addr_t phys,
720 				      const struct mem_type *type)
721 {
722 	pmd_t *pmd = pmd_offset(pud, addr);
723 	unsigned long next;
724 
725 	do {
726 		/*
727 		 * With LPAE, we must loop over to map
728 		 * all the pmds for the given range.
729 		 */
730 		next = pmd_addr_end(addr, end);
731 
732 		/*
733 		 * Try a section mapping - addr, next and phys must all be
734 		 * aligned to a section boundary.
735 		 */
736 		if (type->prot_sect &&
737 				((addr | next | phys) & ~SECTION_MASK) == 0) {
738 			__map_init_section(pmd, addr, next, phys, type);
739 		} else {
740 			alloc_init_pte(pmd, addr, next,
741 						__phys_to_pfn(phys), type);
742 		}
743 
744 		phys += next - addr;
745 
746 	} while (pmd++, addr = next, addr != end);
747 }
748 
749 static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
750 				  unsigned long end, phys_addr_t phys,
751 				  const struct mem_type *type)
752 {
753 	pud_t *pud = pud_offset(pgd, addr);
754 	unsigned long next;
755 
756 	do {
757 		next = pud_addr_end(addr, end);
758 		alloc_init_pmd(pud, addr, next, phys, type);
759 		phys += next - addr;
760 	} while (pud++, addr = next, addr != end);
761 }
762 
763 #ifndef CONFIG_ARM_LPAE
764 static void __init create_36bit_mapping(struct map_desc *md,
765 					const struct mem_type *type)
766 {
767 	unsigned long addr, length, end;
768 	phys_addr_t phys;
769 	pgd_t *pgd;
770 
771 	addr = md->virtual;
772 	phys = __pfn_to_phys(md->pfn);
773 	length = PAGE_ALIGN(md->length);
774 
775 	if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
776 		printk(KERN_ERR "MM: CPU does not support supersection "
777 		       "mapping for 0x%08llx at 0x%08lx\n",
778 		       (long long)__pfn_to_phys((u64)md->pfn), addr);
779 		return;
780 	}
781 
782 	/* N.B.	ARMv6 supersections are only defined to work with domain 0.
783 	 *	Since domain assignments can in fact be arbitrary, the
784 	 *	'domain == 0' check below is required to insure that ARMv6
785 	 *	supersections are only allocated for domain 0 regardless
786 	 *	of the actual domain assignments in use.
787 	 */
788 	if (type->domain) {
789 		printk(KERN_ERR "MM: invalid domain in supersection "
790 		       "mapping for 0x%08llx at 0x%08lx\n",
791 		       (long long)__pfn_to_phys((u64)md->pfn), addr);
792 		return;
793 	}
794 
795 	if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
796 		printk(KERN_ERR "MM: cannot create mapping for 0x%08llx"
797 		       " at 0x%08lx invalid alignment\n",
798 		       (long long)__pfn_to_phys((u64)md->pfn), addr);
799 		return;
800 	}
801 
802 	/*
803 	 * Shift bits [35:32] of address into bits [23:20] of PMD
804 	 * (See ARMv6 spec).
805 	 */
806 	phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
807 
808 	pgd = pgd_offset_k(addr);
809 	end = addr + length;
810 	do {
811 		pud_t *pud = pud_offset(pgd, addr);
812 		pmd_t *pmd = pmd_offset(pud, addr);
813 		int i;
814 
815 		for (i = 0; i < 16; i++)
816 			*pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);
817 
818 		addr += SUPERSECTION_SIZE;
819 		phys += SUPERSECTION_SIZE;
820 		pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
821 	} while (addr != end);
822 }
823 #endif	/* !CONFIG_ARM_LPAE */
824 
825 /*
826  * Create the page directory entries and any necessary
827  * page tables for the mapping specified by `md'.  We
828  * are able to cope here with varying sizes and address
829  * offsets, and we take full advantage of sections and
830  * supersections.
831  */
832 static void __init create_mapping(struct map_desc *md)
833 {
834 	unsigned long addr, length, end;
835 	phys_addr_t phys;
836 	const struct mem_type *type;
837 	pgd_t *pgd;
838 
839 	if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
840 		printk(KERN_WARNING "BUG: not creating mapping for 0x%08llx"
841 		       " at 0x%08lx in user region\n",
842 		       (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
843 		return;
844 	}
845 
846 	if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
847 	    md->virtual >= PAGE_OFFSET &&
848 	    (md->virtual < VMALLOC_START || md->virtual >= VMALLOC_END)) {
849 		printk(KERN_WARNING "BUG: mapping for 0x%08llx"
850 		       " at 0x%08lx out of vmalloc space\n",
851 		       (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
852 	}
853 
854 	type = &mem_types[md->type];
855 
856 #ifndef CONFIG_ARM_LPAE
857 	/*
858 	 * Catch 36-bit addresses
859 	 */
860 	if (md->pfn >= 0x100000) {
861 		create_36bit_mapping(md, type);
862 		return;
863 	}
864 #endif
865 
866 	addr = md->virtual & PAGE_MASK;
867 	phys = __pfn_to_phys(md->pfn);
868 	length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
869 
870 	if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
871 		printk(KERN_WARNING "BUG: map for 0x%08llx at 0x%08lx can not "
872 		       "be mapped using pages, ignoring.\n",
873 		       (long long)__pfn_to_phys(md->pfn), addr);
874 		return;
875 	}
876 
877 	pgd = pgd_offset_k(addr);
878 	end = addr + length;
879 	do {
880 		unsigned long next = pgd_addr_end(addr, end);
881 
882 		alloc_init_pud(pgd, addr, next, phys, type);
883 
884 		phys += next - addr;
885 		addr = next;
886 	} while (pgd++, addr != end);
887 }
888 
889 /*
890  * Create the architecture specific mappings
891  */
892 void __init iotable_init(struct map_desc *io_desc, int nr)
893 {
894 	struct map_desc *md;
895 	struct vm_struct *vm;
896 	struct static_vm *svm;
897 
898 	if (!nr)
899 		return;
900 
901 	svm = early_alloc_aligned(sizeof(*svm) * nr, __alignof__(*svm));
902 
903 	for (md = io_desc; nr; md++, nr--) {
904 		create_mapping(md);
905 
906 		vm = &svm->vm;
907 		vm->addr = (void *)(md->virtual & PAGE_MASK);
908 		vm->size = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
909 		vm->phys_addr = __pfn_to_phys(md->pfn);
910 		vm->flags = VM_IOREMAP | VM_ARM_STATIC_MAPPING;
911 		vm->flags |= VM_ARM_MTYPE(md->type);
912 		vm->caller = iotable_init;
913 		add_static_vm_early(svm++);
914 	}
915 }
916 
917 void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
918 				  void *caller)
919 {
920 	struct vm_struct *vm;
921 	struct static_vm *svm;
922 
923 	svm = early_alloc_aligned(sizeof(*svm), __alignof__(*svm));
924 
925 	vm = &svm->vm;
926 	vm->addr = (void *)addr;
927 	vm->size = size;
928 	vm->flags = VM_IOREMAP | VM_ARM_EMPTY_MAPPING;
929 	vm->caller = caller;
930 	add_static_vm_early(svm);
931 }
932 
933 #ifndef CONFIG_ARM_LPAE
934 
935 /*
936  * The Linux PMD is made of two consecutive section entries covering 2MB
937  * (see definition in include/asm/pgtable-2level.h).  However a call to
938  * create_mapping() may optimize static mappings by using individual
939  * 1MB section mappings.  This leaves the actual PMD potentially half
940  * initialized if the top or bottom section entry isn't used, leaving it
941  * open to problems if a subsequent ioremap() or vmalloc() tries to use
942  * the virtual space left free by that unused section entry.
943  *
944  * Let's avoid the issue by inserting dummy vm entries covering the unused
945  * PMD halves once the static mappings are in place.
946  */
947 
948 static void __init pmd_empty_section_gap(unsigned long addr)
949 {
950 	vm_reserve_area_early(addr, SECTION_SIZE, pmd_empty_section_gap);
951 }
952 
953 static void __init fill_pmd_gaps(void)
954 {
955 	struct static_vm *svm;
956 	struct vm_struct *vm;
957 	unsigned long addr, next = 0;
958 	pmd_t *pmd;
959 
960 	list_for_each_entry(svm, &static_vmlist, list) {
961 		vm = &svm->vm;
962 		addr = (unsigned long)vm->addr;
963 		if (addr < next)
964 			continue;
965 
966 		/*
967 		 * Check if this vm starts on an odd section boundary.
968 		 * If so and the first section entry for this PMD is free
969 		 * then we block the corresponding virtual address.
970 		 */
971 		if ((addr & ~PMD_MASK) == SECTION_SIZE) {
972 			pmd = pmd_off_k(addr);
973 			if (pmd_none(*pmd))
974 				pmd_empty_section_gap(addr & PMD_MASK);
975 		}
976 
977 		/*
978 		 * Then check if this vm ends on an odd section boundary.
979 		 * If so and the second section entry for this PMD is empty
980 		 * then we block the corresponding virtual address.
981 		 */
982 		addr += vm->size;
983 		if ((addr & ~PMD_MASK) == SECTION_SIZE) {
984 			pmd = pmd_off_k(addr) + 1;
985 			if (pmd_none(*pmd))
986 				pmd_empty_section_gap(addr);
987 		}
988 
989 		/* no need to look at any vm entry until we hit the next PMD */
990 		next = (addr + PMD_SIZE - 1) & PMD_MASK;
991 	}
992 }
993 
994 #else
995 #define fill_pmd_gaps() do { } while (0)
996 #endif
997 
998 #if defined(CONFIG_PCI) && !defined(CONFIG_NEED_MACH_IO_H)
999 static void __init pci_reserve_io(void)
1000 {
1001 	struct static_vm *svm;
1002 
1003 	svm = find_static_vm_vaddr((void *)PCI_IO_VIRT_BASE);
1004 	if (svm)
1005 		return;
1006 
1007 	vm_reserve_area_early(PCI_IO_VIRT_BASE, SZ_2M, pci_reserve_io);
1008 }
1009 #else
1010 #define pci_reserve_io() do { } while (0)
1011 #endif
1012 
1013 #ifdef CONFIG_DEBUG_LL
1014 void __init debug_ll_io_init(void)
1015 {
1016 	struct map_desc map;
1017 
1018 	debug_ll_addr(&map.pfn, &map.virtual);
1019 	if (!map.pfn || !map.virtual)
1020 		return;
1021 	map.pfn = __phys_to_pfn(map.pfn);
1022 	map.virtual &= PAGE_MASK;
1023 	map.length = PAGE_SIZE;
1024 	map.type = MT_DEVICE;
1025 	iotable_init(&map, 1);
1026 }
1027 #endif
1028 
1029 static void * __initdata vmalloc_min =
1030 	(void *)(VMALLOC_END - (240 << 20) - VMALLOC_OFFSET);
1031 
1032 /*
1033  * vmalloc=size forces the vmalloc area to be exactly 'size'
1034  * bytes. This can be used to increase (or decrease) the vmalloc
1035  * area - the default is 240m.
1036  */
1037 static int __init early_vmalloc(char *arg)
1038 {
1039 	unsigned long vmalloc_reserve = memparse(arg, NULL);
1040 
1041 	if (vmalloc_reserve < SZ_16M) {
1042 		vmalloc_reserve = SZ_16M;
1043 		printk(KERN_WARNING
1044 			"vmalloc area too small, limiting to %luMB\n",
1045 			vmalloc_reserve >> 20);
1046 	}
1047 
1048 	if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
1049 		vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
1050 		printk(KERN_WARNING
1051 			"vmalloc area is too big, limiting to %luMB\n",
1052 			vmalloc_reserve >> 20);
1053 	}
1054 
1055 	vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
1056 	return 0;
1057 }
1058 early_param("vmalloc", early_vmalloc);
1059 
1060 phys_addr_t arm_lowmem_limit __initdata = 0;
1061 
1062 void __init sanity_check_meminfo(void)
1063 {
1064 	phys_addr_t memblock_limit = 0;
1065 	int highmem = 0;
1066 	phys_addr_t vmalloc_limit = __pa(vmalloc_min - 1) + 1;
1067 	struct memblock_region *reg;
1068 
1069 	for_each_memblock(memory, reg) {
1070 		phys_addr_t block_start = reg->base;
1071 		phys_addr_t block_end = reg->base + reg->size;
1072 		phys_addr_t size_limit = reg->size;
1073 
1074 		if (reg->base >= vmalloc_limit)
1075 			highmem = 1;
1076 		else
1077 			size_limit = vmalloc_limit - reg->base;
1078 
1079 
1080 		if (!IS_ENABLED(CONFIG_HIGHMEM) || cache_is_vipt_aliasing()) {
1081 
1082 			if (highmem) {
1083 				pr_notice("Ignoring RAM at %pa-%pa (!CONFIG_HIGHMEM)\n",
1084 					&block_start, &block_end);
1085 				memblock_remove(reg->base, reg->size);
1086 				continue;
1087 			}
1088 
1089 			if (reg->size > size_limit) {
1090 				phys_addr_t overlap_size = reg->size - size_limit;
1091 
1092 				pr_notice("Truncating RAM at %pa-%pa to -%pa",
1093 				      &block_start, &block_end, &vmalloc_limit);
1094 				memblock_remove(vmalloc_limit, overlap_size);
1095 				block_end = vmalloc_limit;
1096 			}
1097 		}
1098 
1099 		if (!highmem) {
1100 			if (block_end > arm_lowmem_limit) {
1101 				if (reg->size > size_limit)
1102 					arm_lowmem_limit = vmalloc_limit;
1103 				else
1104 					arm_lowmem_limit = block_end;
1105 			}
1106 
1107 			/*
1108 			 * Find the first non-section-aligned page, and point
1109 			 * memblock_limit at it. This relies on rounding the
1110 			 * limit down to be section-aligned, which happens at
1111 			 * the end of this function.
1112 			 *
1113 			 * With this algorithm, the start or end of almost any
1114 			 * bank can be non-section-aligned. The only exception
1115 			 * is that the start of the bank 0 must be section-
1116 			 * aligned, since otherwise memory would need to be
1117 			 * allocated when mapping the start of bank 0, which
1118 			 * occurs before any free memory is mapped.
1119 			 */
1120 			if (!memblock_limit) {
1121 				if (!IS_ALIGNED(block_start, SECTION_SIZE))
1122 					memblock_limit = block_start;
1123 				else if (!IS_ALIGNED(block_end, SECTION_SIZE))
1124 					memblock_limit = arm_lowmem_limit;
1125 			}
1126 
1127 		}
1128 	}
1129 
1130 	high_memory = __va(arm_lowmem_limit - 1) + 1;
1131 
1132 	/*
1133 	 * Round the memblock limit down to a section size.  This
1134 	 * helps to ensure that we will allocate memory from the
1135 	 * last full section, which should be mapped.
1136 	 */
1137 	if (memblock_limit)
1138 		memblock_limit = round_down(memblock_limit, SECTION_SIZE);
1139 	if (!memblock_limit)
1140 		memblock_limit = arm_lowmem_limit;
1141 
1142 	memblock_set_current_limit(memblock_limit);
1143 }
1144 
1145 static inline void prepare_page_table(void)
1146 {
1147 	unsigned long addr;
1148 	phys_addr_t end;
1149 
1150 	/*
1151 	 * Clear out all the mappings below the kernel image.
1152 	 */
1153 	for (addr = 0; addr < MODULES_VADDR; addr += PMD_SIZE)
1154 		pmd_clear(pmd_off_k(addr));
1155 
1156 #ifdef CONFIG_XIP_KERNEL
1157 	/* The XIP kernel is mapped in the module area -- skip over it */
1158 	addr = ((unsigned long)_etext + PMD_SIZE - 1) & PMD_MASK;
1159 #endif
1160 	for ( ; addr < PAGE_OFFSET; addr += PMD_SIZE)
1161 		pmd_clear(pmd_off_k(addr));
1162 
1163 	/*
1164 	 * Find the end of the first block of lowmem.
1165 	 */
1166 	end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
1167 	if (end >= arm_lowmem_limit)
1168 		end = arm_lowmem_limit;
1169 
1170 	/*
1171 	 * Clear out all the kernel space mappings, except for the first
1172 	 * memory bank, up to the vmalloc region.
1173 	 */
1174 	for (addr = __phys_to_virt(end);
1175 	     addr < VMALLOC_START; addr += PMD_SIZE)
1176 		pmd_clear(pmd_off_k(addr));
1177 }
1178 
1179 #ifdef CONFIG_ARM_LPAE
1180 /* the first page is reserved for pgd */
1181 #define SWAPPER_PG_DIR_SIZE	(PAGE_SIZE + \
1182 				 PTRS_PER_PGD * PTRS_PER_PMD * sizeof(pmd_t))
1183 #else
1184 #define SWAPPER_PG_DIR_SIZE	(PTRS_PER_PGD * sizeof(pgd_t))
1185 #endif
1186 
1187 /*
1188  * Reserve the special regions of memory
1189  */
1190 void __init arm_mm_memblock_reserve(void)
1191 {
1192 	/*
1193 	 * Reserve the page tables.  These are already in use,
1194 	 * and can only be in node 0.
1195 	 */
1196 	memblock_reserve(__pa(swapper_pg_dir), SWAPPER_PG_DIR_SIZE);
1197 
1198 #ifdef CONFIG_SA1111
1199 	/*
1200 	 * Because of the SA1111 DMA bug, we want to preserve our
1201 	 * precious DMA-able memory...
1202 	 */
1203 	memblock_reserve(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
1204 #endif
1205 }
1206 
1207 /*
1208  * Set up the device mappings.  Since we clear out the page tables for all
1209  * mappings above VMALLOC_START, we will remove any debug device mappings.
1210  * This means you have to be careful how you debug this function, or any
1211  * called function.  This means you can't use any function or debugging
1212  * method which may touch any device, otherwise the kernel _will_ crash.
1213  */
1214 static void __init devicemaps_init(const struct machine_desc *mdesc)
1215 {
1216 	struct map_desc map;
1217 	unsigned long addr;
1218 	void *vectors;
1219 
1220 	/*
1221 	 * Allocate the vector page early.
1222 	 */
1223 	vectors = early_alloc(PAGE_SIZE * 2);
1224 
1225 	early_trap_init(vectors);
1226 
1227 	for (addr = VMALLOC_START; addr; addr += PMD_SIZE)
1228 		pmd_clear(pmd_off_k(addr));
1229 
1230 	/*
1231 	 * Map the kernel if it is XIP.
1232 	 * It is always first in the modulearea.
1233 	 */
1234 #ifdef CONFIG_XIP_KERNEL
1235 	map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
1236 	map.virtual = MODULES_VADDR;
1237 	map.length = ((unsigned long)_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
1238 	map.type = MT_ROM;
1239 	create_mapping(&map);
1240 #endif
1241 
1242 	/*
1243 	 * Map the cache flushing regions.
1244 	 */
1245 #ifdef FLUSH_BASE
1246 	map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
1247 	map.virtual = FLUSH_BASE;
1248 	map.length = SZ_1M;
1249 	map.type = MT_CACHECLEAN;
1250 	create_mapping(&map);
1251 #endif
1252 #ifdef FLUSH_BASE_MINICACHE
1253 	map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
1254 	map.virtual = FLUSH_BASE_MINICACHE;
1255 	map.length = SZ_1M;
1256 	map.type = MT_MINICLEAN;
1257 	create_mapping(&map);
1258 #endif
1259 
1260 	/*
1261 	 * Create a mapping for the machine vectors at the high-vectors
1262 	 * location (0xffff0000).  If we aren't using high-vectors, also
1263 	 * create a mapping at the low-vectors virtual address.
1264 	 */
1265 	map.pfn = __phys_to_pfn(virt_to_phys(vectors));
1266 	map.virtual = 0xffff0000;
1267 	map.length = PAGE_SIZE;
1268 #ifdef CONFIG_KUSER_HELPERS
1269 	map.type = MT_HIGH_VECTORS;
1270 #else
1271 	map.type = MT_LOW_VECTORS;
1272 #endif
1273 	create_mapping(&map);
1274 
1275 	if (!vectors_high()) {
1276 		map.virtual = 0;
1277 		map.length = PAGE_SIZE * 2;
1278 		map.type = MT_LOW_VECTORS;
1279 		create_mapping(&map);
1280 	}
1281 
1282 	/* Now create a kernel read-only mapping */
1283 	map.pfn += 1;
1284 	map.virtual = 0xffff0000 + PAGE_SIZE;
1285 	map.length = PAGE_SIZE;
1286 	map.type = MT_LOW_VECTORS;
1287 	create_mapping(&map);
1288 
1289 	/*
1290 	 * Ask the machine support to map in the statically mapped devices.
1291 	 */
1292 	if (mdesc->map_io)
1293 		mdesc->map_io();
1294 	else
1295 		debug_ll_io_init();
1296 	fill_pmd_gaps();
1297 
1298 	/* Reserve fixed i/o space in VMALLOC region */
1299 	pci_reserve_io();
1300 
1301 	/*
1302 	 * Finally flush the caches and tlb to ensure that we're in a
1303 	 * consistent state wrt the writebuffer.  This also ensures that
1304 	 * any write-allocated cache lines in the vector page are written
1305 	 * back.  After this point, we can start to touch devices again.
1306 	 */
1307 	local_flush_tlb_all();
1308 	flush_cache_all();
1309 }
1310 
1311 static void __init kmap_init(void)
1312 {
1313 #ifdef CONFIG_HIGHMEM
1314 	pkmap_page_table = early_pte_alloc(pmd_off_k(PKMAP_BASE),
1315 		PKMAP_BASE, _PAGE_KERNEL_TABLE);
1316 
1317 	fixmap_page_table = early_pte_alloc(pmd_off_k(FIXADDR_START),
1318 		FIXADDR_START, _PAGE_KERNEL_TABLE);
1319 #endif
1320 }
1321 
1322 static void __init map_lowmem(void)
1323 {
1324 	struct memblock_region *reg;
1325 	unsigned long kernel_x_start = round_down(__pa(_stext), SECTION_SIZE);
1326 	unsigned long kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
1327 
1328 	/* Map all the lowmem memory banks. */
1329 	for_each_memblock(memory, reg) {
1330 		phys_addr_t start = reg->base;
1331 		phys_addr_t end = start + reg->size;
1332 		struct map_desc map;
1333 
1334 		if (end > arm_lowmem_limit)
1335 			end = arm_lowmem_limit;
1336 		if (start >= end)
1337 			break;
1338 
1339 		if (end < kernel_x_start || start >= kernel_x_end) {
1340 			map.pfn = __phys_to_pfn(start);
1341 			map.virtual = __phys_to_virt(start);
1342 			map.length = end - start;
1343 			map.type = MT_MEMORY_RWX;
1344 
1345 			create_mapping(&map);
1346 		} else {
1347 			/* This better cover the entire kernel */
1348 			if (start < kernel_x_start) {
1349 				map.pfn = __phys_to_pfn(start);
1350 				map.virtual = __phys_to_virt(start);
1351 				map.length = kernel_x_start - start;
1352 				map.type = MT_MEMORY_RW;
1353 
1354 				create_mapping(&map);
1355 			}
1356 
1357 			map.pfn = __phys_to_pfn(kernel_x_start);
1358 			map.virtual = __phys_to_virt(kernel_x_start);
1359 			map.length = kernel_x_end - kernel_x_start;
1360 			map.type = MT_MEMORY_RWX;
1361 
1362 			create_mapping(&map);
1363 
1364 			if (kernel_x_end < end) {
1365 				map.pfn = __phys_to_pfn(kernel_x_end);
1366 				map.virtual = __phys_to_virt(kernel_x_end);
1367 				map.length = end - kernel_x_end;
1368 				map.type = MT_MEMORY_RW;
1369 
1370 				create_mapping(&map);
1371 			}
1372 		}
1373 	}
1374 }
1375 
1376 #ifdef CONFIG_ARM_LPAE
1377 /*
1378  * early_paging_init() recreates boot time page table setup, allowing machines
1379  * to switch over to a high (>4G) address space on LPAE systems
1380  */
1381 void __init early_paging_init(const struct machine_desc *mdesc,
1382 			      struct proc_info_list *procinfo)
1383 {
1384 	pmdval_t pmdprot = procinfo->__cpu_mm_mmu_flags;
1385 	unsigned long map_start, map_end;
1386 	pgd_t *pgd0, *pgdk;
1387 	pud_t *pud0, *pudk, *pud_start;
1388 	pmd_t *pmd0, *pmdk;
1389 	phys_addr_t phys;
1390 	int i;
1391 
1392 	if (!(mdesc->init_meminfo))
1393 		return;
1394 
1395 	/* remap kernel code and data */
1396 	map_start = init_mm.start_code;
1397 	map_end   = init_mm.brk;
1398 
1399 	/* get a handle on things... */
1400 	pgd0 = pgd_offset_k(0);
1401 	pud_start = pud0 = pud_offset(pgd0, 0);
1402 	pmd0 = pmd_offset(pud0, 0);
1403 
1404 	pgdk = pgd_offset_k(map_start);
1405 	pudk = pud_offset(pgdk, map_start);
1406 	pmdk = pmd_offset(pudk, map_start);
1407 
1408 	mdesc->init_meminfo();
1409 
1410 	/* Run the patch stub to update the constants */
1411 	fixup_pv_table(&__pv_table_begin,
1412 		(&__pv_table_end - &__pv_table_begin) << 2);
1413 
1414 	/*
1415 	 * Cache cleaning operations for self-modifying code
1416 	 * We should clean the entries by MVA but running a
1417 	 * for loop over every pv_table entry pointer would
1418 	 * just complicate the code.
1419 	 */
1420 	flush_cache_louis();
1421 	dsb(ishst);
1422 	isb();
1423 
1424 	/* remap level 1 table */
1425 	for (i = 0; i < PTRS_PER_PGD; pud0++, i++) {
1426 		set_pud(pud0,
1427 			__pud(__pa(pmd0) | PMD_TYPE_TABLE | L_PGD_SWAPPER));
1428 		pmd0 += PTRS_PER_PMD;
1429 	}
1430 
1431 	/* remap pmds for kernel mapping */
1432 	phys = __pa(map_start) & PMD_MASK;
1433 	do {
1434 		*pmdk++ = __pmd(phys | pmdprot);
1435 		phys += PMD_SIZE;
1436 	} while (phys < map_end);
1437 
1438 	flush_cache_all();
1439 	cpu_switch_mm(pgd0, &init_mm);
1440 	cpu_set_ttbr(1, __pa(pgd0) + TTBR1_OFFSET);
1441 	local_flush_bp_all();
1442 	local_flush_tlb_all();
1443 }
1444 
1445 #else
1446 
1447 void __init early_paging_init(const struct machine_desc *mdesc,
1448 			      struct proc_info_list *procinfo)
1449 {
1450 	if (mdesc->init_meminfo)
1451 		mdesc->init_meminfo();
1452 }
1453 
1454 #endif
1455 
1456 /*
1457  * paging_init() sets up the page tables, initialises the zone memory
1458  * maps, and sets up the zero page, bad page and bad page tables.
1459  */
1460 void __init paging_init(const struct machine_desc *mdesc)
1461 {
1462 	void *zero_page;
1463 
1464 	build_mem_type_table();
1465 	prepare_page_table();
1466 	map_lowmem();
1467 	dma_contiguous_remap();
1468 	devicemaps_init(mdesc);
1469 	kmap_init();
1470 	tcm_init();
1471 
1472 	top_pmd = pmd_off_k(0xffff0000);
1473 
1474 	/* allocate the zero page. */
1475 	zero_page = early_alloc(PAGE_SIZE);
1476 
1477 	bootmem_init();
1478 
1479 	empty_zero_page = virt_to_page(zero_page);
1480 	__flush_dcache_page(NULL, empty_zero_page);
1481 }
1482