File: /usr/src/linux/arch/sh/mm/init.c

1     /* $Id: init.c,v 1.18 2001/08/03 11:22:06 gniibe Exp $
2      *
3      *  linux/arch/sh/mm/init.c
4      *
5      *  Copyright (C) 1999  Niibe Yutaka
6      *
7      *  Based on linux/arch/i386/mm/init.c:
8      *   Copyright (C) 1995  Linus Torvalds
9      */
10     
11     #include <linux/config.h>
12     #include <linux/signal.h>
13     #include <linux/sched.h>
14     #include <linux/kernel.h>
15     #include <linux/errno.h>
16     #include <linux/string.h>
17     #include <linux/types.h>
18     #include <linux/ptrace.h>
19     #include <linux/mman.h>
20     #include <linux/mm.h>
21     #include <linux/swap.h>
22     #include <linux/smp.h>
23     #include <linux/init.h>
24     #ifdef CONFIG_BLK_DEV_INITRD
25     #include <linux/blk.h>
26     #endif
27     #include <linux/highmem.h>
28     #include <linux/bootmem.h>
29     
30     #include <asm/processor.h>
31     #include <asm/system.h>
32     #include <asm/uaccess.h>
33     #include <asm/pgtable.h>
34     #include <asm/pgalloc.h>
35     #include <asm/mmu_context.h>
36     #include <asm/io.h>
37     #include <asm/tlb.h>
38     
39     mmu_gather_t mmu_gathers[NR_CPUS];
40     
41     /*
42      * Cache of MMU context last used.
43      */
44     unsigned long mmu_context_cache;
45     
46     #ifdef CONFIG_DISCONTIGMEM
47     pg_data_t discontig_page_data[NR_NODES];
48     bootmem_data_t discontig_node_bdata[NR_NODES];
49     #endif
50     
51     static unsigned long totalram_pages;
52     static unsigned long totalhigh_pages;
53     
54     void show_mem(void)
55     {
56     	int i, total = 0, reserved = 0;
57     	int shared = 0, cached = 0;
58     
59     	printk("Mem-info:\n");
60     	show_free_areas();
61     	printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
62     	i = max_mapnr;
63     	while (i-- > 0) {
64     		total++;
65     		if (PageReserved(mem_map+i))
66     			reserved++;
67     		else if (PageSwapCache(mem_map+i))
68     			cached++;
69     		else if (page_count(mem_map+i))
70     			shared += page_count(mem_map+i) - 1;
71     	}
72     	printk("%d pages of RAM\n",total);
73     	printk("%d reserved pages\n",reserved);
74     	printk("%d pages shared\n",shared);
75     	printk("%d pages swap cached\n",cached);
76     	show_buffers();
77     }
78     
79     /* References to section boundaries */
80     
81     extern char _text, _etext, _edata, __bss_start, _end;
82     extern char __init_begin, __init_end;
83     
84     pgd_t swapper_pg_dir[PTRS_PER_PGD];
85     
86     /* It'd be good if these lines were in the standard header file. */
87     #define START_PFN	(NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
88     #define MAX_LOW_PFN	(NODE_DATA(0)->bdata->node_low_pfn)
89     
90     /*
91      * paging_init() sets up the page tables
92      *
93      * This routines also unmaps the page at virtual kernel address 0, so
94      * that we can trap those pesky NULL-reference errors in the kernel.
95      */
96     void __init paging_init(void)
97     {
98     	int i;
99     	pgd_t * pg_dir;
100     
101     	/* We don't need kernel mapping as hardware support that. */
102     	pg_dir = swapper_pg_dir;
103     
104     	for (i=0; i < PTRS_PER_PGD; i++)
105     		pgd_val(pg_dir[i]) = 0;
106     
107     	/* Enable MMU */
108     	ctrl_outl(MMU_CONTROL_INIT, MMUCR);
109     
110     	/* The manual suggests doing some nops after turning on the MMU */
111     	asm volatile("nop;nop;nop;nop;nop;nop;");
112     
113     	mmu_context_cache = MMU_CONTEXT_FIRST_VERSION;
114     	set_asid(mmu_context_cache & MMU_CONTEXT_ASID_MASK);
115     
116      	{
117     		unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
118     		unsigned long max_dma, low, start_pfn;
119     
120     		start_pfn = START_PFN;
121     		max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
122     		low = MAX_LOW_PFN;
123     
124     		if (low < max_dma)
125     			zones_size[ZONE_DMA] = low - start_pfn;
126     		else {
127     			zones_size[ZONE_DMA] = max_dma - start_pfn;
128     			zones_size[ZONE_NORMAL] = low - max_dma;
129     		}
130     		free_area_init_node(0, NODE_DATA(0), 0, zones_size, __MEMORY_START, 0);
131     #ifdef CONFIG_DISCONTIGMEM
132     		zones_size[ZONE_DMA] = __MEMORY_SIZE_2ND >> PAGE_SHIFT;
133     		zones_size[ZONE_NORMAL] = 0;
134     		free_area_init_node(1, NODE_DATA(1), 0, zones_size, __MEMORY_START_2ND, 0);
135     #endif
136      	}
137     }
138     
139     void __init mem_init(void)
140     {
141     	extern unsigned long empty_zero_page[1024];
142     	int codesize, reservedpages, datasize, initsize;
143     	int tmp;
144     
145     	max_mapnr = num_physpages = MAX_LOW_PFN - START_PFN;
146     	high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
147     
148     	/* clear the zero-page */
149     	memset(empty_zero_page, 0, PAGE_SIZE);
150     	__flush_wback_region(empty_zero_page, PAGE_SIZE);
151     
152     	/* this will put all low memory onto the freelists */
153     	totalram_pages += free_all_bootmem_node(NODE_DATA(0));
154     #ifdef CONFIG_DISCONTIGMEM
155     	totalram_pages += free_all_bootmem_node(NODE_DATA(1));
156     #endif
157     	reservedpages = 0;
158     	for (tmp = 0; tmp < num_physpages; tmp++)
159     		/*
160     		 * Only count reserved RAM pages
161     		 */
162     		if (PageReserved(mem_map+tmp))
163     			reservedpages++;
164     	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
165     	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
166     	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
167     
168     	printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
169     		(unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
170     		max_mapnr << (PAGE_SHIFT-10),
171     		codesize >> 10,
172     		reservedpages << (PAGE_SHIFT-10),
173     		datasize >> 10,
174     		initsize >> 10);
175     
176     	p3_cache_init();
177     }
178     
179     void free_initmem(void)
180     {
181     	unsigned long addr;
182     	
183     	addr = (unsigned long)(&__init_begin);
184     	for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
185     		ClearPageReserved(virt_to_page(addr));
186     		set_page_count(virt_to_page(addr), 1);
187     		free_page(addr);
188     		totalram_pages++;
189     	}
190     	printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
191     }
192     
193     #ifdef CONFIG_BLK_DEV_INITRD
194     void free_initrd_mem(unsigned long start, unsigned long end)
195     {
196     	unsigned long p;
197     	for (p = start; p < end; p += PAGE_SIZE) {
198     		ClearPageReserved(virt_to_page(p));
199     		set_page_count(virt_to_page(p), 1);
200     		free_page(p);
201     		totalram_pages++;
202     	}
203     	printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
204     }
205     #endif
206     
207     void si_meminfo(struct sysinfo *val)
208     {
209     	val->totalram = totalram_pages;
210     	val->sharedram = 0;
211     	val->freeram = nr_free_pages();
212     	val->bufferram = atomic_read(&buffermem_pages);
213     	val->totalhigh = totalhigh_pages;
214     	val->freehigh = nr_free_highpages();
215     	val->mem_unit = PAGE_SIZE;
216     	return;
217     }
218