xref: /openbmc/linux/arch/openrisc/mm/init.c (revision f519f0be)
1 /*
2  * OpenRISC idle.c
3  *
4  * Linux architectural port borrowing liberally from similar works of
5  * others.  All original copyrights apply as per the original source
6  * declaration.
7  *
8  * Modifications for the OpenRISC architecture:
9  * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10  * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17 
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/ptrace.h>
25 #include <linux/mman.h>
26 #include <linux/mm.h>
27 #include <linux/swap.h>
28 #include <linux/smp.h>
29 #include <linux/memblock.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/blkdev.h>	/* for initrd_* */
33 #include <linux/pagemap.h>
34 
35 #include <asm/pgalloc.h>
36 #include <asm/pgtable.h>
37 #include <asm/dma.h>
38 #include <asm/io.h>
39 #include <asm/tlb.h>
40 #include <asm/mmu_context.h>
41 #include <asm/kmap_types.h>
42 #include <asm/fixmap.h>
43 #include <asm/tlbflush.h>
44 #include <asm/sections.h>
45 
46 int mem_init_done;
47 
48 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
49 
50 static void __init zone_sizes_init(void)
51 {
52 	unsigned long zones_size[MAX_NR_ZONES];
53 
54 	/* Clear the zone sizes */
55 	memset(zones_size, 0, sizeof(zones_size));
56 
57 	/*
58 	 * We use only ZONE_NORMAL
59 	 */
60 	zones_size[ZONE_NORMAL] = max_low_pfn;
61 
62 	free_area_init(zones_size);
63 }
64 
65 extern const char _s_kernel_ro[], _e_kernel_ro[];
66 
67 /*
68  * Map all physical memory into kernel's address space.
69  *
70  * This is explicitly coded for two-level page tables, so if you need
71  * something else then this needs to change.
72  */
73 static void __init map_ram(void)
74 {
75 	unsigned long v, p, e;
76 	pgprot_t prot;
77 	pgd_t *pge;
78 	pud_t *pue;
79 	pmd_t *pme;
80 	pte_t *pte;
81 	/* These mark extents of read-only kernel pages...
82 	 * ...from vmlinux.lds.S
83 	 */
84 	struct memblock_region *region;
85 
86 	v = PAGE_OFFSET;
87 
88 	for_each_memblock(memory, region) {
89 		p = (u32) region->base & PAGE_MASK;
90 		e = p + (u32) region->size;
91 
92 		v = (u32) __va(p);
93 		pge = pgd_offset_k(v);
94 
95 		while (p < e) {
96 			int j;
97 			pue = pud_offset(pge, v);
98 			pme = pmd_offset(pue, v);
99 
100 			if ((u32) pue != (u32) pge || (u32) pme != (u32) pge) {
101 				panic("%s: OR1K kernel hardcoded for "
102 				      "two-level page tables",
103 				     __func__);
104 			}
105 
106 			/* Alloc one page for holding PTE's... */
107 			pte = memblock_alloc_raw(PAGE_SIZE, PAGE_SIZE);
108 			if (!pte)
109 				panic("%s: Failed to allocate page for PTEs\n",
110 				      __func__);
111 			set_pmd(pme, __pmd(_KERNPG_TABLE + __pa(pte)));
112 
113 			/* Fill the newly allocated page with PTE'S */
114 			for (j = 0; p < e && j < PTRS_PER_PTE;
115 			     v += PAGE_SIZE, p += PAGE_SIZE, j++, pte++) {
116 				if (v >= (u32) _e_kernel_ro ||
117 				    v < (u32) _s_kernel_ro)
118 					prot = PAGE_KERNEL;
119 				else
120 					prot = PAGE_KERNEL_RO;
121 
122 				set_pte(pte, mk_pte_phys(p, prot));
123 			}
124 
125 			pge++;
126 		}
127 
128 		printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
129 		       region->base, region->base + region->size);
130 	}
131 }
132 
133 void __init paging_init(void)
134 {
135 	extern void tlb_init(void);
136 
137 	unsigned long end;
138 	int i;
139 
140 	printk(KERN_INFO "Setting up paging and PTEs.\n");
141 
142 	/* clear out the init_mm.pgd that will contain the kernel's mappings */
143 
144 	for (i = 0; i < PTRS_PER_PGD; i++)
145 		swapper_pg_dir[i] = __pgd(0);
146 
147 	/* make sure the current pgd table points to something sane
148 	 * (even if it is most probably not used until the next
149 	 *  switch_mm)
150 	 */
151 	current_pgd[smp_processor_id()] = init_mm.pgd;
152 
153 	end = (unsigned long)__va(max_low_pfn * PAGE_SIZE);
154 
155 	map_ram();
156 
157 	zone_sizes_init();
158 
159 	/* self modifying code ;) */
160 	/* Since the old TLB miss handler has been running up until now,
161 	 * the kernel pages are still all RW, so we can still modify the
162 	 * text directly... after this change and a TLB flush, the kernel
163 	 * pages will become RO.
164 	 */
165 	{
166 		extern unsigned long dtlb_miss_handler;
167 		extern unsigned long itlb_miss_handler;
168 
169 		unsigned long *dtlb_vector = __va(0x900);
170 		unsigned long *itlb_vector = __va(0xa00);
171 
172 		printk(KERN_INFO "itlb_miss_handler %p\n", &itlb_miss_handler);
173 		*itlb_vector = ((unsigned long)&itlb_miss_handler -
174 				(unsigned long)itlb_vector) >> 2;
175 
176 		/* Soft ordering constraint to ensure that dtlb_vector is
177 		 * the last thing updated
178 		 */
179 		barrier();
180 
181 		printk(KERN_INFO "dtlb_miss_handler %p\n", &dtlb_miss_handler);
182 		*dtlb_vector = ((unsigned long)&dtlb_miss_handler -
183 				(unsigned long)dtlb_vector) >> 2;
184 
185 	}
186 
187 	/* Soft ordering constraint to ensure that cache invalidation and
188 	 * TLB flush really happen _after_ code has been modified.
189 	 */
190 	barrier();
191 
192 	/* Invalidate instruction caches after code modification */
193 	mtspr(SPR_ICBIR, 0x900);
194 	mtspr(SPR_ICBIR, 0xa00);
195 
196 	/* New TLB miss handlers and kernel page tables are in now place.
197 	 * Make sure that page flags get updated for all pages in TLB by
198 	 * flushing the TLB and forcing all TLB entries to be recreated
199 	 * from their page table flags.
200 	 */
201 	flush_tlb_all();
202 }
203 
204 /* References to section boundaries */
205 
206 void __init mem_init(void)
207 {
208 	BUG_ON(!mem_map);
209 
210 	max_mapnr = max_low_pfn;
211 	high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
212 
213 	/* clear the zero-page */
214 	memset((void *)empty_zero_page, 0, PAGE_SIZE);
215 
216 	/* this will put all low memory onto the freelists */
217 	memblock_free_all();
218 
219 	mem_init_print_info(NULL);
220 
221 	printk("mem_init_done ...........................................\n");
222 	mem_init_done = 1;
223 	return;
224 }
225