xref: /OK3568_Linux_fs/kernel/arch/powerpc/mm/nohash/40x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * This file contains the routines for initializing the MMU
4*4882a593Smuzhiyun  * on the 4xx series of chips.
5*4882a593Smuzhiyun  *  -- paulus
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  Derived from arch/ppc/mm/init.c:
8*4882a593Smuzhiyun  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
11*4882a593Smuzhiyun  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
12*4882a593Smuzhiyun  *    Copyright (C) 1996 Paul Mackerras
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *  Derived from "arch/i386/mm/init.c"
15*4882a593Smuzhiyun  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/signal.h>
19*4882a593Smuzhiyun #include <linux/sched.h>
20*4882a593Smuzhiyun #include <linux/kernel.h>
21*4882a593Smuzhiyun #include <linux/errno.h>
22*4882a593Smuzhiyun #include <linux/string.h>
23*4882a593Smuzhiyun #include <linux/types.h>
24*4882a593Smuzhiyun #include <linux/ptrace.h>
25*4882a593Smuzhiyun #include <linux/mman.h>
26*4882a593Smuzhiyun #include <linux/mm.h>
27*4882a593Smuzhiyun #include <linux/swap.h>
28*4882a593Smuzhiyun #include <linux/stddef.h>
29*4882a593Smuzhiyun #include <linux/vmalloc.h>
30*4882a593Smuzhiyun #include <linux/init.h>
31*4882a593Smuzhiyun #include <linux/delay.h>
32*4882a593Smuzhiyun #include <linux/highmem.h>
33*4882a593Smuzhiyun #include <linux/memblock.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <asm/prom.h>
36*4882a593Smuzhiyun #include <asm/io.h>
37*4882a593Smuzhiyun #include <asm/mmu_context.h>
38*4882a593Smuzhiyun #include <asm/mmu.h>
39*4882a593Smuzhiyun #include <linux/uaccess.h>
40*4882a593Smuzhiyun #include <asm/smp.h>
41*4882a593Smuzhiyun #include <asm/bootx.h>
42*4882a593Smuzhiyun #include <asm/machdep.h>
43*4882a593Smuzhiyun #include <asm/setup.h>
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #include <mm/mmu_decl.h>
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun extern int __map_without_ltlbs;
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun  * MMU_init_hw does the chip-specific initialization of the MMU hardware.
50*4882a593Smuzhiyun  */
MMU_init_hw(void)51*4882a593Smuzhiyun void __init MMU_init_hw(void)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	/*
54*4882a593Smuzhiyun 	 * The Zone Protection Register (ZPR) defines how protection will
55*4882a593Smuzhiyun 	 * be applied to every page which is a member of a given zone. At
56*4882a593Smuzhiyun 	 * present, we utilize only two of the 4xx's zones.
57*4882a593Smuzhiyun 	 * The zone index bits (of ZSEL) in the PTE are used for software
58*4882a593Smuzhiyun 	 * indicators, except the LSB.  For user access, zone 1 is used,
59*4882a593Smuzhiyun 	 * for kernel access, zone 0 is used.  We set all but zone 1
60*4882a593Smuzhiyun 	 * to zero, allowing only kernel access as indicated in the PTE.
61*4882a593Smuzhiyun 	 * For zone 1, we set a 01 binary (a value of 10 will not work)
62*4882a593Smuzhiyun 	 * to allow user access as indicated in the PTE.  This also allows
63*4882a593Smuzhiyun 	 * kernel access as indicated in the PTE.
64*4882a593Smuzhiyun 	 */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun         mtspr(SPRN_ZPR, 0x10000000);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	flush_instruction_cache();
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/*
71*4882a593Smuzhiyun 	 * Set up the real-mode cache parameters for the exception vector
72*4882a593Smuzhiyun 	 * handlers (which are run in real-mode).
73*4882a593Smuzhiyun 	 */
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun         mtspr(SPRN_DCWR, 0x00000000);	/* All caching is write-back */
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun         /*
78*4882a593Smuzhiyun 	 * Cache instruction and data space where the exception
79*4882a593Smuzhiyun 	 * vectors and the kernel live in real-mode.
80*4882a593Smuzhiyun 	 */
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun         mtspr(SPRN_DCCR, 0xFFFF0000);	/* 2GByte of data space at 0x0. */
83*4882a593Smuzhiyun         mtspr(SPRN_ICCR, 0xFFFF0000);	/* 2GByte of instr. space at 0x0. */
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun #define LARGE_PAGE_SIZE_16M	(1<<24)
87*4882a593Smuzhiyun #define LARGE_PAGE_SIZE_4M	(1<<22)
88*4882a593Smuzhiyun 
mmu_mapin_ram(unsigned long base,unsigned long top)89*4882a593Smuzhiyun unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	unsigned long v, s, mapped;
92*4882a593Smuzhiyun 	phys_addr_t p;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	v = KERNELBASE;
95*4882a593Smuzhiyun 	p = 0;
96*4882a593Smuzhiyun 	s = total_lowmem;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (__map_without_ltlbs)
99*4882a593Smuzhiyun 		return 0;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	while (s >= LARGE_PAGE_SIZE_16M) {
102*4882a593Smuzhiyun 		pmd_t *pmdp;
103*4882a593Smuzhiyun 		unsigned long val = p | _PMD_SIZE_16M | _PAGE_EXEC | _PAGE_RW;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 		pmdp = pmd_off_k(v);
106*4882a593Smuzhiyun 		*pmdp++ = __pmd(val);
107*4882a593Smuzhiyun 		*pmdp++ = __pmd(val);
108*4882a593Smuzhiyun 		*pmdp++ = __pmd(val);
109*4882a593Smuzhiyun 		*pmdp++ = __pmd(val);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 		v += LARGE_PAGE_SIZE_16M;
112*4882a593Smuzhiyun 		p += LARGE_PAGE_SIZE_16M;
113*4882a593Smuzhiyun 		s -= LARGE_PAGE_SIZE_16M;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	while (s >= LARGE_PAGE_SIZE_4M) {
117*4882a593Smuzhiyun 		pmd_t *pmdp;
118*4882a593Smuzhiyun 		unsigned long val = p | _PMD_SIZE_4M | _PAGE_EXEC | _PAGE_RW;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		pmdp = pmd_off_k(v);
121*4882a593Smuzhiyun 		*pmdp = __pmd(val);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 		v += LARGE_PAGE_SIZE_4M;
124*4882a593Smuzhiyun 		p += LARGE_PAGE_SIZE_4M;
125*4882a593Smuzhiyun 		s -= LARGE_PAGE_SIZE_4M;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	mapped = total_lowmem - s;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	/* If the size of RAM is not an exact power of two, we may not
131*4882a593Smuzhiyun 	 * have covered RAM in its entirety with 16 and 4 MiB
132*4882a593Smuzhiyun 	 * pages. Consequently, restrict the top end of RAM currently
133*4882a593Smuzhiyun 	 * allocable so that calls to the MEMBLOCK to allocate PTEs for "tail"
134*4882a593Smuzhiyun 	 * coverage with normal-sized pages (or other reasons) do not
135*4882a593Smuzhiyun 	 * attempt to allocate outside the allowed range.
136*4882a593Smuzhiyun 	 */
137*4882a593Smuzhiyun 	memblock_set_current_limit(mapped);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return mapped;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
setup_initial_memory_limit(phys_addr_t first_memblock_base,phys_addr_t first_memblock_size)142*4882a593Smuzhiyun void setup_initial_memory_limit(phys_addr_t first_memblock_base,
143*4882a593Smuzhiyun 				phys_addr_t first_memblock_size)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	/* We don't currently support the first MEMBLOCK not mapping 0
146*4882a593Smuzhiyun 	 * physical on those processors
147*4882a593Smuzhiyun 	 */
148*4882a593Smuzhiyun 	BUG_ON(first_memblock_base != 0);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/* 40x can only access 16MB at the moment (see head_40x.S) */
151*4882a593Smuzhiyun 	memblock_set_current_limit(min_t(u64, first_memblock_size, 0x00800000));
152*4882a593Smuzhiyun }
153