xref: /linux/arch/openrisc/mm/init.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
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/segment.h>
36 #include <asm/pgalloc.h>
37 #include <asm/pgtable.h>
38 #include <asm/dma.h>
39 #include <asm/io.h>
40 #include <asm/tlb.h>
41 #include <asm/mmu_context.h>
42 #include <asm/kmap_types.h>
43 #include <asm/fixmap.h>
44 #include <asm/tlbflush.h>
45 #include <asm/sections.h>
46 
47 int mem_init_done;
48 
49 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
50 
51 static void __init zone_sizes_init(void)
52 {
53 	unsigned long zones_size[MAX_NR_ZONES];
54 
55 	/* Clear the zone sizes */
56 	memset(zones_size, 0, sizeof(zones_size));
57 
58 	/*
59 	 * We use only ZONE_NORMAL
60 	 */
61 	zones_size[ZONE_NORMAL] = max_low_pfn;
62 
63 	free_area_init(zones_size);
64 }
65 
66 extern const char _s_kernel_ro[], _e_kernel_ro[];
67 
68 /*
69  * Map all physical memory into kernel's address space.
70  *
71  * This is explicitly coded for two-level page tables, so if you need
72  * something else then this needs to change.
73  */
74 static void __init map_ram(void)
75 {
76 	unsigned long v, p, e;
77 	pgprot_t prot;
78 	pgd_t *pge;
79 	pud_t *pue;
80 	pmd_t *pme;
81 	pte_t *pte;
82 	/* These mark extents of read-only kernel pages...
83 	 * ...from vmlinux.lds.S
84 	 */
85 	struct memblock_region *region;
86 
87 	v = PAGE_OFFSET;
88 
89 	for_each_memblock(memory, region) {
90 		p = (u32) region->base & PAGE_MASK;
91 		e = p + (u32) region->size;
92 
93 		v = (u32) __va(p);
94 		pge = pgd_offset_k(v);
95 
96 		while (p < e) {
97 			int j;
98 			pue = pud_offset(pge, v);
99 			pme = pmd_offset(pue, v);
100 
101 			if ((u32) pue != (u32) pge || (u32) pme != (u32) pge) {
102 				panic("%s: OR1K kernel hardcoded for "
103 				      "two-level page tables",
104 				     __func__);
105 			}
106 
107 			/* Alloc one page for holding PTE's... */
108 			pte = memblock_alloc_raw(PAGE_SIZE, PAGE_SIZE);
109 			if (!pte)
110 				panic("%s: Failed to allocate page for PTEs\n",
111 				      __func__);
112 			set_pmd(pme, __pmd(_KERNPG_TABLE + __pa(pte)));
113 
114 			/* Fill the newly allocated page with PTE'S */
115 			for (j = 0; p < e && j < PTRS_PER_PTE;
116 			     v += PAGE_SIZE, p += PAGE_SIZE, j++, pte++) {
117 				if (v >= (u32) _e_kernel_ro ||
118 				    v < (u32) _s_kernel_ro)
119 					prot = PAGE_KERNEL;
120 				else
121 					prot = PAGE_KERNEL_RO;
122 
123 				set_pte(pte, mk_pte_phys(p, prot));
124 			}
125 
126 			pge++;
127 		}
128 
129 		printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
130 		       region->base, region->base + region->size);
131 	}
132 }
133 
134 void __init paging_init(void)
135 {
136 	extern void tlb_init(void);
137 
138 	unsigned long end;
139 	int i;
140 
141 	printk(KERN_INFO "Setting up paging and PTEs.\n");
142 
143 	/* clear out the init_mm.pgd that will contain the kernel's mappings */
144 
145 	for (i = 0; i < PTRS_PER_PGD; i++)
146 		swapper_pg_dir[i] = __pgd(0);
147 
148 	/* make sure the current pgd table points to something sane
149 	 * (even if it is most probably not used until the next
150 	 *  switch_mm)
151 	 */
152 	current_pgd[smp_processor_id()] = init_mm.pgd;
153 
154 	end = (unsigned long)__va(max_low_pfn * PAGE_SIZE);
155 
156 	map_ram();
157 
158 	zone_sizes_init();
159 
160 	/* self modifying code ;) */
161 	/* Since the old TLB miss handler has been running up until now,
162 	 * the kernel pages are still all RW, so we can still modify the
163 	 * text directly... after this change and a TLB flush, the kernel
164 	 * pages will become RO.
165 	 */
166 	{
167 		extern unsigned long dtlb_miss_handler;
168 		extern unsigned long itlb_miss_handler;
169 
170 		unsigned long *dtlb_vector = __va(0x900);
171 		unsigned long *itlb_vector = __va(0xa00);
172 
173 		printk(KERN_INFO "itlb_miss_handler %p\n", &itlb_miss_handler);
174 		*itlb_vector = ((unsigned long)&itlb_miss_handler -
175 				(unsigned long)itlb_vector) >> 2;
176 
177 		/* Soft ordering constraint to ensure that dtlb_vector is
178 		 * the last thing updated
179 		 */
180 		barrier();
181 
182 		printk(KERN_INFO "dtlb_miss_handler %p\n", &dtlb_miss_handler);
183 		*dtlb_vector = ((unsigned long)&dtlb_miss_handler -
184 				(unsigned long)dtlb_vector) >> 2;
185 
186 	}
187 
188 	/* Soft ordering constraint to ensure that cache invalidation and
189 	 * TLB flush really happen _after_ code has been modified.
190 	 */
191 	barrier();
192 
193 	/* Invalidate instruction caches after code modification */
194 	mtspr(SPR_ICBIR, 0x900);
195 	mtspr(SPR_ICBIR, 0xa00);
196 
197 	/* New TLB miss handlers and kernel page tables are in now place.
198 	 * Make sure that page flags get updated for all pages in TLB by
199 	 * flushing the TLB and forcing all TLB entries to be recreated
200 	 * from their page table flags.
201 	 */
202 	flush_tlb_all();
203 }
204 
205 /* References to section boundaries */
206 
207 void __init mem_init(void)
208 {
209 	BUG_ON(!mem_map);
210 
211 	max_mapnr = max_low_pfn;
212 	high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
213 
214 	/* clear the zero-page */
215 	memset((void *)empty_zero_page, 0, PAGE_SIZE);
216 
217 	/* this will put all low memory onto the freelists */
218 	memblock_free_all();
219 
220 	mem_init_print_info(NULL);
221 
222 	printk("mem_init_done ...........................................\n");
223 	mem_init_done = 1;
224 	return;
225 }
226 
227 #ifdef CONFIG_BLK_DEV_INITRD
228 void free_initrd_mem(unsigned long start, unsigned long end)
229 {
230 	free_reserved_area((void *)start, (void *)end, -1, "initrd");
231 }
232 #endif
233 
234 void free_initmem(void)
235 {
236 	free_initmem_default(-1);
237 }
238