xref: /openbmc/linux/arch/xtensa/mm/init.c (revision 0bef42e5)
1 /*
2  * arch/xtensa/mm/init.c
3  *
4  * Derived from MIPS, PPC.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  *
10  * Copyright (C) 2001 - 2005 Tensilica Inc.
11  *
12  * Chris Zankel	<chris@zankel.net>
13  * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
14  * Marc Gauthier
15  * Kevin Chea
16  */
17 
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/bootmem.h>
21 #include <linux/swap.h>
22 #include <linux/mman.h>
23 #include <linux/nodemask.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 
27 #include <asm/pgtable.h>
28 #include <asm/bootparam.h>
29 #include <asm/mmu_context.h>
30 #include <asm/tlb.h>
31 #include <asm/page.h>
32 #include <asm/pgalloc.h>
33 
34 
35 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
36 
37 /* References to section boundaries */
38 
39 extern char _ftext, _etext, _fdata, _edata, _rodata_end;
40 extern char __init_begin, __init_end;
41 
42 /*
43  * mem_reserve(start, end, must_exist)
44  *
45  * Reserve some memory from the memory pool.
46  *
47  * Parameters:
48  *  start	Start of region,
49  *  end		End of region,
50  *  must_exist	Must exist in memory pool.
51  *
52  * Returns:
53  *  0 (memory area couldn't be mapped)
54  * -1 (success)
55  */
56 
57 int __init mem_reserve(unsigned long start, unsigned long end, int must_exist)
58 {
59 	int i;
60 
61 	if (start == end)
62 		return 0;
63 
64 	start = start & PAGE_MASK;
65 	end = PAGE_ALIGN(end);
66 
67 	for (i = 0; i < sysmem.nr_banks; i++)
68 		if (start < sysmem.bank[i].end
69 		    && end >= sysmem.bank[i].start)
70 			break;
71 
72 	if (i == sysmem.nr_banks) {
73 		if (must_exist)
74 			printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) "
75 				"not in any region!\n", start, end);
76 		return 0;
77 	}
78 
79 	if (start > sysmem.bank[i].start) {
80 		if (end < sysmem.bank[i].end) {
81 			/* split entry */
82 			if (sysmem.nr_banks >= SYSMEM_BANKS_MAX)
83 				panic("meminfo overflow\n");
84 			sysmem.bank[sysmem.nr_banks].start = end;
85 			sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end;
86 			sysmem.nr_banks++;
87 		}
88 		sysmem.bank[i].end = start;
89 	} else {
90 		if (end < sysmem.bank[i].end)
91 			sysmem.bank[i].start = end;
92 		else {
93 			/* remove entry */
94 			sysmem.nr_banks--;
95 			sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start;
96 			sysmem.bank[i].end   = sysmem.bank[sysmem.nr_banks].end;
97 		}
98 	}
99 	return -1;
100 }
101 
102 
103 /*
104  * Initialize the bootmem system and give it all the memory we have available.
105  */
106 
107 void __init bootmem_init(void)
108 {
109 	unsigned long pfn;
110 	unsigned long bootmap_start, bootmap_size;
111 	int i;
112 
113 	max_low_pfn = max_pfn = 0;
114 	min_low_pfn = ~0;
115 
116 	for (i=0; i < sysmem.nr_banks; i++) {
117 		pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT;
118 		if (pfn < min_low_pfn)
119 			min_low_pfn = pfn;
120 		pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT;
121 		if (pfn > max_pfn)
122 			max_pfn = pfn;
123 	}
124 
125 	if (min_low_pfn > max_pfn)
126 		panic("No memory found!\n");
127 
128 	max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ?
129 		max_pfn : MAX_MEM_PFN >> PAGE_SHIFT;
130 
131 	/* Find an area to use for the bootmem bitmap. */
132 
133 	bootmap_size = bootmem_bootmap_pages(max_low_pfn) << PAGE_SHIFT;
134 	bootmap_start = ~0;
135 
136 	for (i=0; i<sysmem.nr_banks; i++)
137 		if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) {
138 			bootmap_start = sysmem.bank[i].start;
139 			break;
140 		}
141 
142 	if (bootmap_start == ~0UL)
143 		panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
144 
145 	/* Reserve the bootmem bitmap area */
146 
147 	mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1);
148 	bootmap_size = init_bootmem_node(NODE_DATA(0),
149 					 bootmap_start >> PAGE_SHIFT,
150 					 min_low_pfn,
151 					 max_low_pfn);
152 
153 	/* Add all remaining memory pieces into the bootmem map */
154 
155 	for (i=0; i<sysmem.nr_banks; i++)
156 		free_bootmem(sysmem.bank[i].start,
157 			     sysmem.bank[i].end - sysmem.bank[i].start);
158 
159 }
160 
161 
162 void __init paging_init(void)
163 {
164 	unsigned long zones_size[MAX_NR_ZONES];
165 	int i;
166 
167 	/* All pages are DMA-able, so we put them all in the DMA zone. */
168 
169 	zones_size[ZONE_DMA] = max_low_pfn;
170 	for (i = 1; i < MAX_NR_ZONES; i++)
171 		zones_size[i] = 0;
172 
173 #ifdef CONFIG_HIGHMEM
174 	zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;
175 #endif
176 
177 	/* Initialize the kernel's page tables. */
178 
179 	memset(swapper_pg_dir, 0, PAGE_SIZE);
180 
181 	free_area_init(zones_size);
182 }
183 
184 /*
185  * Flush the mmu and reset associated register to default values.
186  */
187 
188 void __init init_mmu (void)
189 {
190 	/* Writing zeros to the <t>TLBCFG special registers ensure
191 	 * that valid values exist in the register.  For existing
192 	 * PGSZID<w> fields, zero selects the first element of the
193 	 * page-size array.  For nonexistent PGSZID<w> fields, zero is
194 	 * the best value to write.  Also, when changing PGSZID<w>
195 	 * fields, the corresponding TLB must be flushed.
196 	 */
197 	set_itlbcfg_register (0);
198 	set_dtlbcfg_register (0);
199 	flush_tlb_all ();
200 
201 	/* Set rasid register to a known value. */
202 
203 	set_rasid_register (ASID_USER_FIRST);
204 
205 	/* Set PTEVADDR special register to the start of the page
206 	 * table, which is in kernel mappable space (ie. not
207 	 * statically mapped).  This register's value is undefined on
208 	 * reset.
209 	 */
210 	set_ptevaddr_register (PGTABLE_START);
211 }
212 
213 /*
214  * Initialize memory pages.
215  */
216 
217 void __init mem_init(void)
218 {
219 	unsigned long codesize, reservedpages, datasize, initsize;
220 	unsigned long highmemsize, tmp, ram;
221 
222 	max_mapnr = num_physpages = max_low_pfn;
223 	high_memory = (void *) __va(max_mapnr << PAGE_SHIFT);
224 	highmemsize = 0;
225 
226 #ifdef CONFIG_HIGHMEM
227 #error HIGHGMEM not implemented in init.c
228 #endif
229 
230 	totalram_pages += free_all_bootmem();
231 
232 	reservedpages = ram = 0;
233 	for (tmp = 0; tmp < max_low_pfn; tmp++) {
234 		ram++;
235 		if (PageReserved(mem_map+tmp))
236 			reservedpages++;
237 	}
238 
239 	codesize =  (unsigned long) &_etext - (unsigned long) &_ftext;
240 	datasize =  (unsigned long) &_edata - (unsigned long) &_fdata;
241 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
242 
243 	printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, "
244 	       "%ldk data, %ldk init %ldk highmem)\n",
245 	       (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
246 	       ram << (PAGE_SHIFT-10),
247 	       codesize >> 10,
248 	       reservedpages << (PAGE_SHIFT-10),
249 	       datasize >> 10,
250 	       initsize >> 10,
251 	       highmemsize >> 10);
252 }
253 
254 void
255 free_reserved_mem(void *start, void *end)
256 {
257 	for (; start < end; start += PAGE_SIZE) {
258 		ClearPageReserved(virt_to_page(start));
259 		init_page_count(virt_to_page(start));
260 		free_page((unsigned long)start);
261 		totalram_pages++;
262 	}
263 }
264 
265 #ifdef CONFIG_BLK_DEV_INITRD
266 extern int initrd_is_mapped;
267 
268 void free_initrd_mem(unsigned long start, unsigned long end)
269 {
270 	if (initrd_is_mapped) {
271 		free_reserved_mem((void*)start, (void*)end);
272 		printk ("Freeing initrd memory: %ldk freed\n",(end-start)>>10);
273 	}
274 }
275 #endif
276 
277 void free_initmem(void)
278 {
279 	free_reserved_mem(&__init_begin, &__init_end);
280 	printk("Freeing unused kernel memory: %dk freed\n",
281 	       (&__init_end - &__init_begin) >> 10);
282 }
283 
284 struct kmem_cache *pgtable_cache __read_mostly;
285 
286 static void pgd_ctor(void* addr)
287 {
288 	pte_t* ptep = (pte_t*)addr;
289 	int i;
290 
291 	for (i = 0; i < 1024; i++, ptep++)
292 		pte_clear(NULL, 0, ptep);
293 
294 }
295 
296 void __init pgtable_cache_init(void)
297 {
298 	pgtable_cache = kmem_cache_create("pgd",
299 			PAGE_SIZE, PAGE_SIZE,
300 			SLAB_HWCACHE_ALIGN,
301 			pgd_ctor);
302 }
303