xref: /openbmc/linux/arch/mips/mm/init.c (revision 1da177e4)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1994 - 2000 Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
9  * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
10  */
11 #include <linux/config.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/pagemap.h>
21 #include <linux/ptrace.h>
22 #include <linux/mman.h>
23 #include <linux/mm.h>
24 #include <linux/bootmem.h>
25 #include <linux/highmem.h>
26 #include <linux/swap.h>
27 
28 #include <asm/bootinfo.h>
29 #include <asm/cachectl.h>
30 #include <asm/cpu.h>
31 #include <asm/dma.h>
32 #include <asm/mmu_context.h>
33 #include <asm/sections.h>
34 #include <asm/pgtable.h>
35 #include <asm/pgalloc.h>
36 #include <asm/tlb.h>
37 
38 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
39 
40 unsigned long highstart_pfn, highend_pfn;
41 
42 /*
43  * We have up to 8 empty zeroed pages so we can map one of the right colour
44  * when needed.  This is necessary only on R4000 / R4400 SC and MC versions
45  * where we have to avoid VCED / VECI exceptions for good performance at
46  * any price.  Since page is never written to after the initialization we
47  * don't have to care about aliases on other CPUs.
48  */
49 unsigned long empty_zero_page, zero_page_mask;
50 
51 /*
52  * Not static inline because used by IP27 special magic initialization code
53  */
54 unsigned long setup_zero_pages(void)
55 {
56 	unsigned long order, size;
57 	struct page *page;
58 
59 	if (cpu_has_vce)
60 		order = 3;
61 	else
62 		order = 0;
63 
64 	empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
65 	if (!empty_zero_page)
66 		panic("Oh boy, that early out of memory?");
67 
68 	page = virt_to_page(empty_zero_page);
69 	while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
70 		set_bit(PG_reserved, &page->flags);
71 		set_page_count(page, 0);
72 		page++;
73 	}
74 
75 	size = PAGE_SIZE << order;
76 	zero_page_mask = (size - 1) & PAGE_MASK;
77 
78 	return 1UL << order;
79 }
80 
81 #ifdef CONFIG_HIGHMEM
82 pte_t *kmap_pte;
83 pgprot_t kmap_prot;
84 
85 #define kmap_get_fixmap_pte(vaddr)					\
86 	pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
87 
88 static void __init kmap_init(void)
89 {
90 	unsigned long kmap_vstart;
91 
92 	/* cache the first kmap pte */
93 	kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
94 	kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
95 
96 	kmap_prot = PAGE_KERNEL;
97 }
98 
99 #ifdef CONFIG_MIPS64
100 static void __init fixrange_init(unsigned long start, unsigned long end,
101 	pgd_t *pgd_base)
102 {
103 	pgd_t *pgd;
104 	pmd_t *pmd;
105 	pte_t *pte;
106 	int i, j;
107 	unsigned long vaddr;
108 
109 	vaddr = start;
110 	i = __pgd_offset(vaddr);
111 	j = __pmd_offset(vaddr);
112 	pgd = pgd_base + i;
113 
114 	for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
115 		pmd = (pmd_t *)pgd;
116 		for (; (j < PTRS_PER_PMD) && (vaddr != end); pmd++, j++) {
117 			if (pmd_none(*pmd)) {
118 				pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
119 				set_pmd(pmd, __pmd(pte));
120 				if (pte != pte_offset_kernel(pmd, 0))
121 					BUG();
122 			}
123 			vaddr += PMD_SIZE;
124 		}
125 		j = 0;
126 	}
127 }
128 #endif /* CONFIG_MIPS64 */
129 #endif /* CONFIG_HIGHMEM */
130 
131 #ifndef CONFIG_DISCONTIGMEM
132 extern void pagetable_init(void);
133 
134 void __init paging_init(void)
135 {
136 	unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
137 	unsigned long max_dma, high, low;
138 
139 	pagetable_init();
140 
141 #ifdef CONFIG_HIGHMEM
142 	kmap_init();
143 #endif
144 
145 	max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
146 	low = max_low_pfn;
147 	high = highend_pfn;
148 
149 #ifdef CONFIG_ISA
150 	if (low < max_dma)
151 		zones_size[ZONE_DMA] = low;
152 	else {
153 		zones_size[ZONE_DMA] = max_dma;
154 		zones_size[ZONE_NORMAL] = low - max_dma;
155 	}
156 #else
157 	zones_size[ZONE_DMA] = low;
158 #endif
159 #ifdef CONFIG_HIGHMEM
160 	if (cpu_has_dc_aliases) {
161 		printk(KERN_WARNING "This processor doesn't support highmem.");
162 		if (high - low)
163 			printk(" %ldk highmem ignored", high - low);
164 		printk("\n");
165 	} else
166 		zones_size[ZONE_HIGHMEM] = high - low;
167 #endif
168 
169 	free_area_init(zones_size);
170 }
171 
172 #define PFN_UP(x)	(((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
173 #define PFN_DOWN(x)	((x) >> PAGE_SHIFT)
174 
175 static inline int page_is_ram(unsigned long pagenr)
176 {
177 	int i;
178 
179 	for (i = 0; i < boot_mem_map.nr_map; i++) {
180 		unsigned long addr, end;
181 
182 		if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
183 			/* not usable memory */
184 			continue;
185 
186 		addr = PFN_UP(boot_mem_map.map[i].addr);
187 		end = PFN_DOWN(boot_mem_map.map[i].addr +
188 			       boot_mem_map.map[i].size);
189 
190 		if (pagenr >= addr && pagenr < end)
191 			return 1;
192 	}
193 
194 	return 0;
195 }
196 
197 void __init mem_init(void)
198 {
199 	unsigned long codesize, reservedpages, datasize, initsize;
200 	unsigned long tmp, ram;
201 
202 #ifdef CONFIG_HIGHMEM
203 #ifdef CONFIG_DISCONTIGMEM
204 #error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
205 #endif
206 	max_mapnr = num_physpages = highend_pfn;
207 #else
208 	max_mapnr = num_physpages = max_low_pfn;
209 #endif
210 	high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
211 
212 	totalram_pages += free_all_bootmem();
213 	totalram_pages -= setup_zero_pages();	/* Setup zeroed pages.  */
214 
215 	reservedpages = ram = 0;
216 	for (tmp = 0; tmp < max_low_pfn; tmp++)
217 		if (page_is_ram(tmp)) {
218 			ram++;
219 			if (PageReserved(mem_map+tmp))
220 				reservedpages++;
221 		}
222 
223 #ifdef CONFIG_HIGHMEM
224 	for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
225 		struct page *page = mem_map + tmp;
226 
227 		if (!page_is_ram(tmp)) {
228 			SetPageReserved(page);
229 			continue;
230 		}
231 		ClearPageReserved(page);
232 #ifdef CONFIG_LIMITED_DMA
233 		set_page_address(page, lowmem_page_address(page));
234 #endif
235 		set_bit(PG_highmem, &page->flags);
236 		set_page_count(page, 1);
237 		__free_page(page);
238 		totalhigh_pages++;
239 	}
240 	totalram_pages += totalhigh_pages;
241 #endif
242 
243 	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
244 	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
245 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
246 
247 	printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
248 	       "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
249 	       (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
250 	       ram << (PAGE_SHIFT-10),
251 	       codesize >> 10,
252 	       reservedpages << (PAGE_SHIFT-10),
253 	       datasize >> 10,
254 	       initsize >> 10,
255 	       (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
256 }
257 #endif /* !CONFIG_DISCONTIGMEM */
258 
259 #ifdef CONFIG_BLK_DEV_INITRD
260 void free_initrd_mem(unsigned long start, unsigned long end)
261 {
262 #ifdef CONFIG_MIPS64
263 	/* Switch from KSEG0 to XKPHYS addresses */
264 	start = (unsigned long)phys_to_virt(CPHYSADDR(start));
265 	end = (unsigned long)phys_to_virt(CPHYSADDR(end));
266 #endif
267 	if (start < end)
268 		printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
269 		       (end - start) >> 10);
270 
271 	for (; start < end; start += PAGE_SIZE) {
272 		ClearPageReserved(virt_to_page(start));
273 		set_page_count(virt_to_page(start), 1);
274 		free_page(start);
275 		totalram_pages++;
276 	}
277 }
278 #endif
279 
280 extern unsigned long prom_free_prom_memory(void);
281 
282 void free_initmem(void)
283 {
284 	unsigned long addr, page, freed;
285 
286 	freed = prom_free_prom_memory();
287 
288 	addr = (unsigned long) &__init_begin;
289 	while (addr < (unsigned long) &__init_end) {
290 #ifdef CONFIG_MIPS64
291 		page = PAGE_OFFSET | CPHYSADDR(addr);
292 #else
293 		page = addr;
294 #endif
295 		ClearPageReserved(virt_to_page(page));
296 		set_page_count(virt_to_page(page), 1);
297 		free_page(page);
298 		totalram_pages++;
299 		freed += PAGE_SIZE;
300 		addr += PAGE_SIZE;
301 	}
302 	printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",
303 	       freed >> 10);
304 }
305