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 by Ralf Baechle
7 * Copyright (C) 1999, 2000 by Silicon Graphics
8 */
9 #include <linux/config.h>
10 #include <linux/init.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/pagemap.h>
18 #include <linux/ptrace.h>
19 #include <linux/mman.h>
20 #include <linux/mm.h>
21 #include <linux/bootmem.h>
22 #include <linux/highmem.h>
23 #include <linux/swap.h>
24 #include <linux/swapctl.h>
25 #ifdef CONFIG_BLK_DEV_INITRD
26 #include <linux/blk.h>
27 #endif
28
29 #include <asm/bootinfo.h>
30 #include <asm/cachectl.h>
31 #include <asm/dma.h>
32 #include <asm/system.h>
33 #include <asm/pgtable.h>
34 #include <asm/pgalloc.h>
35 #include <asm/mmu_context.h>
36 #include <asm/tlb.h>
37 #include <asm/cpu.h>
38
39 mmu_gather_t mmu_gathers[NR_CPUS];
40 unsigned long totalram_pages;
41
pgd_init(unsigned long page)42 void pgd_init(unsigned long page)
43 {
44 unsigned long *p, *end;
45
46 p = (unsigned long *) page;
47 end = p + PTRS_PER_PGD;
48
49 while (p < end) {
50 p[0] = (unsigned long) invalid_pmd_table;
51 p[1] = (unsigned long) invalid_pmd_table;
52 p[2] = (unsigned long) invalid_pmd_table;
53 p[3] = (unsigned long) invalid_pmd_table;
54 p[4] = (unsigned long) invalid_pmd_table;
55 p[5] = (unsigned long) invalid_pmd_table;
56 p[6] = (unsigned long) invalid_pmd_table;
57 p[7] = (unsigned long) invalid_pmd_table;
58 p += 8;
59 }
60 }
61
get_pgd_slow(void)62 pgd_t *get_pgd_slow(void)
63 {
64 pgd_t *ret, *init;
65
66 ret = (pgd_t *) __get_free_pages(GFP_KERNEL, PGD_ORDER);
67 if (ret) {
68 init = pgd_offset(&init_mm, 0);
69 pgd_init((unsigned long)ret);
70 memcpy(ret + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
71 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
72 }
73 return ret;
74 }
75
pmd_init(unsigned long addr,unsigned long pagetable)76 void pmd_init(unsigned long addr, unsigned long pagetable)
77 {
78 unsigned long *p, *end;
79
80 p = (unsigned long *) addr;
81 end = p + PTRS_PER_PMD;
82
83 while (p < end) {
84 p[0] = (unsigned long)pagetable;
85 p[1] = (unsigned long)pagetable;
86 p[2] = (unsigned long)pagetable;
87 p[3] = (unsigned long)pagetable;
88 p[4] = (unsigned long)pagetable;
89 p[5] = (unsigned long)pagetable;
90 p[6] = (unsigned long)pagetable;
91 p[7] = (unsigned long)pagetable;
92 p += 8;
93 }
94 }
95
do_check_pgt_cache(int low,int high)96 int do_check_pgt_cache(int low, int high)
97 {
98 int freed = 0;
99
100 if (pgtable_cache_size > high) {
101 do {
102 if (pgd_quicklist)
103 free_pgd_slow(get_pgd_fast()), freed++;
104 if (pmd_quicklist)
105 free_pmd_slow(get_pmd_fast()), freed++;
106 if (pte_quicklist)
107 free_pte_slow(get_pte_fast()), freed++;
108 } while (pgtable_cache_size > low);
109 }
110 return freed;
111 }
112
113 /*
114 * We have up to 8 empty zeroed pages so we can map one of the right colour
115 * when needed. This is necessary only on R4000 / R4400 SC and MC versions
116 * where we have to avoid VCED / VECI exceptions for good performance at
117 * any price. Since page is never written to after the initialization we
118 * don't have to care about aliases on other CPUs.
119 */
120 unsigned long empty_zero_page, zero_page_mask;
121
setup_zero_pages(void)122 unsigned long setup_zero_pages(void)
123 {
124 unsigned long order, size;
125 struct page *page;
126
127 if (cpu_has_vce)
128 order = 3;
129 else
130 order = 0;
131
132 empty_zero_page = __get_free_pages(GFP_KERNEL, order);
133 if (!empty_zero_page)
134 panic("Oh boy, that early out of memory?");
135
136 page = virt_to_page(empty_zero_page);
137 while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) {
138 set_bit(PG_reserved, &page->flags);
139 set_page_count(page, 0);
140 page++;
141 }
142
143 size = PAGE_SIZE << order;
144 zero_page_mask = (size - 1) & PAGE_MASK;
145 memset((void *)empty_zero_page, 0, size);
146
147 return 1UL << order;
148 }
149
show_mem(void)150 void show_mem(void)
151 {
152 int i, free = 0, total = 0, reserved = 0;
153 int shared = 0, cached = 0;
154
155 printk("Mem-info:\n");
156 show_free_areas();
157 printk("Free swap: %6dkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
158 i = max_mapnr;
159 while (i-- > 0) {
160 total++;
161 if (PageReserved(mem_map+i))
162 reserved++;
163 else if (PageSwapCache(mem_map+i))
164 cached++;
165 else if (!page_count(mem_map + i))
166 free++;
167 else
168 shared += page_count(mem_map + i) - 1;
169 }
170 printk("%d pages of RAM\n", total);
171 printk("%d reserved pages\n", reserved);
172 printk("%d pages shared\n", shared);
173 printk("%d pages swap cached\n",cached);
174 printk("%ld pages in page table cache\n", pgtable_cache_size);
175 printk("%d free pages\n", free);
176 show_buffers();
177 }
178
179 #ifndef CONFIG_DISCONTIGMEM
180 /* References to section boundaries */
181
182 extern char _stext, _etext, _fdata, _edata;
183 extern char __init_begin, __init_end;
184
paging_init(void)185 void __init paging_init(void)
186 {
187 pmd_t *pmd = kpmdtbl;
188 pte_t *pte = kptbl;
189
190 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
191 unsigned long max_dma, low;
192 int i;
193
194 /* Initialize the entire pgd. */
195 pgd_init((unsigned long)swapper_pg_dir);
196 pmd_init((unsigned long)invalid_pmd_table, (unsigned long)invalid_pte_table);
197 memset((void *)invalid_pte_table, 0, sizeof(pte_t) * PTRS_PER_PTE);
198
199 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
200 low = max_low_pfn;
201
202 #ifdef CONFIG_ISA
203 if (low < max_dma)
204 zones_size[ZONE_DMA] = low;
205 else {
206 zones_size[ZONE_DMA] = max_dma;
207 zones_size[ZONE_NORMAL] = low - max_dma;
208 }
209 #else
210 zones_size[ZONE_DMA] = low;
211 #endif
212
213 free_area_init(zones_size);
214
215 memset((void *)kptbl, 0, PAGE_SIZE << PGD_ORDER);
216 memset((void *)kpmdtbl, 0, PAGE_SIZE);
217 pgd_set(swapper_pg_dir, kpmdtbl);
218 for (i = 0; i < (1 << PGD_ORDER); pmd++,i++,pte+=PTRS_PER_PTE)
219 pmd_val(*pmd) = (unsigned long)pte;
220 }
221
222 //extern int page_is_ram(unsigned long pagenr);
223
224 #define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
225 #define PFN_DOWN(x) ((x) >> PAGE_SHIFT)
226
page_is_ram(unsigned long pagenr)227 static inline int page_is_ram(unsigned long pagenr)
228 {
229 int i;
230
231 for (i = 0; i < boot_mem_map.nr_map; i++) {
232 unsigned long addr, end;
233
234 if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
235 /* not usable memory */
236 continue;
237
238 addr = PFN_UP(boot_mem_map.map[i].addr);
239 end = PFN_DOWN(boot_mem_map.map[i].addr +
240 boot_mem_map.map[i].size);
241
242 if (pagenr >= addr && pagenr < end)
243 return 1;
244 }
245
246 return 0;
247 }
248
mem_init(void)249 void __init mem_init(void)
250 {
251 unsigned long codesize, reservedpages, datasize, initsize;
252 unsigned long tmp, ram;
253
254 max_mapnr = num_mappedpages = num_physpages = max_low_pfn;
255 high_memory = (void *) __va(max_mapnr << PAGE_SHIFT);
256
257 totalram_pages += free_all_bootmem();
258 totalram_pages -= setup_zero_pages(); /* Setup zeroed pages. */
259
260 reservedpages = ram = 0;
261 for (tmp = 0; tmp < max_low_pfn; tmp++)
262 if (page_is_ram(tmp)) {
263 ram++;
264 if (PageReserved(mem_map+tmp))
265 reservedpages++;
266 }
267
268 codesize = (unsigned long) &_etext - (unsigned long) &_stext;
269 datasize = (unsigned long) &_edata - (unsigned long) &_fdata;
270 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
271
272 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
273 "%ldk reserved, %ldk data, %ldk init)\n",
274 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
275 ram << (PAGE_SHIFT-10),
276 codesize >> 10,
277 reservedpages << (PAGE_SHIFT-10),
278 datasize >> 10,
279 initsize >> 10);
280 }
281 #endif /* !CONFIG_DISCONTIGMEM */
282
283 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)284 void free_initrd_mem(unsigned long start, unsigned long end)
285 {
286 /* Switch from KSEG0 to XKPHYS addresses */
287 start = (unsigned long)phys_to_virt(CPHYSADDR(start));
288 end = (unsigned long)phys_to_virt(CPHYSADDR(end));
289 if (start < end)
290 printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
291 (end - start) >> 10);
292
293 for (; start < end; start += PAGE_SIZE) {
294 ClearPageReserved(virt_to_page(start));
295 set_page_count(virt_to_page(start), 1);
296 free_page(start);
297 totalram_pages++;
298 }
299 }
300 #endif
301
302 extern char __init_begin, __init_end;
303 extern void prom_free_prom_memory(void) __init;
304
305 void
free_initmem(void)306 free_initmem(void)
307 {
308 unsigned long addr, page;
309
310 prom_free_prom_memory();
311
312 addr = (unsigned long)(&__init_begin);
313 while (addr < (unsigned long)&__init_end) {
314 page = PAGE_OFFSET | CPHYSADDR(addr);
315 ClearPageReserved(virt_to_page(page));
316 set_page_count(virt_to_page(page), 1);
317 free_page(page);
318 totalram_pages++;
319 addr += PAGE_SIZE;
320 }
321 printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",
322 (&__init_end - &__init_begin) >> 10);
323 }
324
325 void
si_meminfo(struct sysinfo * val)326 si_meminfo(struct sysinfo *val)
327 {
328 val->totalram = totalram_pages;
329 val->sharedram = 0;
330 val->freeram = nr_free_pages();
331 val->bufferram = atomic_read(&buffermem_pages);
332 val->totalhigh = 0;
333 val->freehigh = nr_free_highpages();
334 val->mem_unit = PAGE_SIZE;
335
336 return;
337 }
338