Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / arch / s390 / mm / init.c
1 /*
2  *  arch/s390/mm/init.c
3  *
4  *  S390 version
5  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Hartmut Penner (hp@de.ibm.com)
7  *
8  *  Derived from "arch/i386/mm/init.c"
9  *    Copyright (C) 1995  Linus Torvalds
10  */
11
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 #include <linux/pagemap.h>
25 #include <linux/bootmem.h>
26 #include <linux/pfn.h>
27
28 #include <asm/processor.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/pgalloc.h>
33 #include <asm/dma.h>
34 #include <asm/lowcore.h>
35 #include <asm/tlb.h>
36 #include <asm/tlbflush.h>
37 #include <asm/sections.h>
38
39 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
40
41 pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__((__aligned__(PAGE_SIZE)));
42 char  empty_zero_page[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
43
44 void diag10(unsigned long addr)
45 {
46         if (addr >= 0x7ff00000)
47                 return;
48         asm volatile(
49 #ifdef CONFIG_64BIT
50                 "       sam31\n"
51                 "       diag    %0,%0,0x10\n"
52                 "0:     sam64\n"
53 #else
54                 "       diag    %0,%0,0x10\n"
55                 "0:\n"
56 #endif
57                 EX_TABLE(0b,0b)
58                 : : "a" (addr));
59 }
60
61 void show_mem(void)
62 {
63         int i, total = 0, reserved = 0;
64         int shared = 0, cached = 0;
65         struct page *page;
66
67         printk("Mem-info:\n");
68         show_free_areas();
69         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
70         i = max_mapnr;
71         while (i-- > 0) {
72                 page = pfn_to_page(i);
73                 total++;
74                 if (PageReserved(page))
75                         reserved++;
76                 else if (PageSwapCache(page))
77                         cached++;
78                 else if (page_count(page))
79                         shared += page_count(page) - 1;
80         }
81         printk("%d pages of RAM\n",total);
82         printk("%d reserved pages\n",reserved);
83         printk("%d pages shared\n",shared);
84         printk("%d pages swap cached\n",cached);
85 }
86
87 extern unsigned long __initdata zholes_size[];
88 /*
89  * paging_init() sets up the page tables
90  */
91
92 #ifndef CONFIG_64BIT
93 void __init paging_init(void)
94 {
95         pgd_t * pg_dir;
96         pte_t * pg_table;
97         pte_t   pte;
98         int     i;
99         unsigned long tmp;
100         unsigned long pfn = 0;
101         unsigned long pgdir_k = (__pa(swapper_pg_dir) & PAGE_MASK) | _KERNSEG_TABLE;
102         static const int ssm_mask = 0x04000000L;
103         unsigned long ro_start_pfn, ro_end_pfn;
104         unsigned long zones_size[MAX_NR_ZONES];
105
106         ro_start_pfn = PFN_DOWN((unsigned long)&__start_rodata);
107         ro_end_pfn = PFN_UP((unsigned long)&__end_rodata);
108
109         memset(zones_size, 0, sizeof(zones_size));
110         zones_size[ZONE_DMA] = max_low_pfn;
111         free_area_init_node(0, &contig_page_data, zones_size,
112                             __pa(PAGE_OFFSET) >> PAGE_SHIFT,
113                             zholes_size);
114
115         /* unmap whole virtual address space */
116         
117         pg_dir = swapper_pg_dir;
118
119         for (i = 0; i < PTRS_PER_PGD; i++)
120                 pmd_clear((pmd_t *) pg_dir++);
121                 
122         /*
123          * map whole physical memory to virtual memory (identity mapping) 
124          */
125
126         pg_dir = swapper_pg_dir;
127
128         while (pfn < max_low_pfn) {
129                 /*
130                  * pg_table is physical at this point
131                  */
132                 pg_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
133
134                 pmd_populate_kernel(&init_mm, (pmd_t *) pg_dir, pg_table);
135                 pg_dir++;
136
137                 for (tmp = 0 ; tmp < PTRS_PER_PTE ; tmp++,pg_table++) {
138                         if (pfn >= ro_start_pfn && pfn < ro_end_pfn)
139                                 pte = pfn_pte(pfn, __pgprot(_PAGE_RO));
140                         else
141                                 pte = pfn_pte(pfn, PAGE_KERNEL);
142                         if (pfn >= max_low_pfn)
143                                 pte_val(pte) = _PAGE_TYPE_EMPTY;
144                         set_pte(pg_table, pte);
145                         pfn++;
146                 }
147         }
148
149         S390_lowcore.kernel_asce = pgdir_k;
150
151         /* enable virtual mapping in kernel mode */
152         __ctl_load(pgdir_k, 1, 1);
153         __ctl_load(pgdir_k, 7, 7);
154         __ctl_load(pgdir_k, 13, 13);
155         __raw_local_irq_ssm(ssm_mask);
156
157         local_flush_tlb();
158 }
159
160 #else /* CONFIG_64BIT */
161
162 void __init paging_init(void)
163 {
164         pgd_t * pg_dir;
165         pmd_t * pm_dir;
166         pte_t * pt_dir;
167         pte_t   pte;
168         int     i,j,k;
169         unsigned long pfn = 0;
170         unsigned long pgdir_k = (__pa(swapper_pg_dir) & PAGE_MASK) |
171           _KERN_REGION_TABLE;
172         static const int ssm_mask = 0x04000000L;
173         unsigned long zones_size[MAX_NR_ZONES];
174         unsigned long dma_pfn, high_pfn;
175         unsigned long ro_start_pfn, ro_end_pfn;
176
177         memset(zones_size, 0, sizeof(zones_size));
178         dma_pfn = MAX_DMA_ADDRESS >> PAGE_SHIFT;
179         high_pfn = max_low_pfn;
180         ro_start_pfn = PFN_DOWN((unsigned long)&__start_rodata);
181         ro_end_pfn = PFN_UP((unsigned long)&__end_rodata);
182
183         if (dma_pfn > high_pfn)
184                 zones_size[ZONE_DMA] = high_pfn;
185         else {
186                 zones_size[ZONE_DMA] = dma_pfn;
187                 zones_size[ZONE_NORMAL] = high_pfn - dma_pfn;
188         }
189
190         /* Initialize mem_map[].  */
191         free_area_init_node(0, &contig_page_data, zones_size,
192                             __pa(PAGE_OFFSET) >> PAGE_SHIFT, zholes_size);
193
194         /*
195          * map whole physical memory to virtual memory (identity mapping) 
196          */
197
198         pg_dir = swapper_pg_dir;
199         
200         for (i = 0 ; i < PTRS_PER_PGD ; i++,pg_dir++) {
201         
202                 if (pfn >= max_low_pfn) {
203                         pgd_clear(pg_dir);
204                         continue;
205                 }          
206         
207                 pm_dir = (pmd_t *) alloc_bootmem_pages(PAGE_SIZE * 4);
208                 pgd_populate(&init_mm, pg_dir, pm_dir);
209
210                 for (j = 0 ; j < PTRS_PER_PMD ; j++,pm_dir++) {
211                         if (pfn >= max_low_pfn) {
212                                 pmd_clear(pm_dir);
213                                 continue; 
214                         }          
215                         
216                         pt_dir = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
217                         pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
218         
219                         for (k = 0 ; k < PTRS_PER_PTE ; k++,pt_dir++) {
220                                 if (pfn >= ro_start_pfn && pfn < ro_end_pfn)
221                                         pte = pfn_pte(pfn, __pgprot(_PAGE_RO));
222                                 else
223                                         pte = pfn_pte(pfn, PAGE_KERNEL);
224                                 if (pfn >= max_low_pfn)
225                                         pte_val(pte) = _PAGE_TYPE_EMPTY;
226                                 set_pte(pt_dir, pte);
227                                 pfn++;
228                         }
229                 }
230         }
231
232         S390_lowcore.kernel_asce = pgdir_k;
233
234         /* enable virtual mapping in kernel mode */
235         __ctl_load(pgdir_k, 1, 1);
236         __ctl_load(pgdir_k, 7, 7);
237         __ctl_load(pgdir_k, 13, 13);
238         __raw_local_irq_ssm(ssm_mask);
239
240         local_flush_tlb();
241 }
242 #endif /* CONFIG_64BIT */
243
244 void __init mem_init(void)
245 {
246         unsigned long codesize, reservedpages, datasize, initsize;
247
248         max_mapnr = num_physpages = max_low_pfn;
249         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
250
251         /* clear the zero-page */
252         memset(empty_zero_page, 0, PAGE_SIZE);
253
254         /* this will put all low memory onto the freelists */
255         totalram_pages += free_all_bootmem();
256
257         reservedpages = 0;
258
259         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
260         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
261         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
262         printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, %ldk data, %ldk init)\n",
263                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
264                 max_mapnr << (PAGE_SHIFT-10),
265                 codesize >> 10,
266                 reservedpages << (PAGE_SHIFT-10),
267                 datasize >>10,
268                 initsize >> 10);
269         printk("Write protected kernel read-only data: %#lx - %#lx\n",
270                (unsigned long)&__start_rodata,
271                PFN_ALIGN((unsigned long)&__end_rodata) - 1);
272 }
273
274 void free_initmem(void)
275 {
276         unsigned long addr;
277
278         addr = (unsigned long)(&__init_begin);
279         for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
280                 ClearPageReserved(virt_to_page(addr));
281                 init_page_count(virt_to_page(addr));
282                 free_page(addr);
283                 totalram_pages++;
284         }
285         printk ("Freeing unused kernel memory: %ldk freed\n",
286                 ((unsigned long)&__init_end - (unsigned long)&__init_begin) >> 10);
287 }
288
289 #ifdef CONFIG_BLK_DEV_INITRD
290 void free_initrd_mem(unsigned long start, unsigned long end)
291 {
292         if (start < end)
293                 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
294         for (; start < end; start += PAGE_SIZE) {
295                 ClearPageReserved(virt_to_page(start));
296                 init_page_count(virt_to_page(start));
297                 free_page(start);
298                 totalram_pages++;
299         }
300 }
301 #endif