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