[PATCH] s390: fix memory holes and cleanup setup_arch
[linux-3.10.git] / 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/config.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/ptrace.h>
20 #include <linux/mman.h>
21 #include <linux/mm.h>
22 #include <linux/swap.h>
23 #include <linux/smp.h>
24 #include <linux/init.h>
25 #include <linux/pagemap.h>
26 #include <linux/bootmem.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
38 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
39
40 pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__((__aligned__(PAGE_SIZE)));
41 char  empty_zero_page[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
42
43 void diag10(unsigned long addr)
44 {
45         if (addr >= 0x7ff00000)
46                 return;
47 #ifdef __s390x__
48         asm volatile (
49                 "   sam31\n"
50                 "   diag %0,%0,0x10\n"
51                 "0: sam64\n"
52                 ".section __ex_table,\"a\"\n"
53                 "   .align 8\n"
54                 "   .quad 0b, 0b\n"
55                 ".previous\n"
56                 : : "a" (addr));
57 #else
58         asm volatile (
59                 "   diag %0,%0,0x10\n"
60                 "0:\n"
61                 ".section __ex_table,\"a\"\n"
62                 "   .align 4\n"
63                 "   .long 0b, 0b\n"
64                 ".previous\n"
65                 : : "a" (addr));
66 #endif
67 }
68
69 void show_mem(void)
70 {
71         int i, total = 0, reserved = 0;
72         int shared = 0, cached = 0;
73
74         printk("Mem-info:\n");
75         show_free_areas();
76         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
77         i = max_mapnr;
78         while (i-- > 0) {
79                 total++;
80                 if (PageReserved(mem_map+i))
81                         reserved++;
82                 else if (PageSwapCache(mem_map+i))
83                         cached++;
84                 else if (page_count(mem_map+i))
85                         shared += page_count(mem_map+i) - 1;
86         }
87         printk("%d pages of RAM\n",total);
88         printk("%d reserved pages\n",reserved);
89         printk("%d pages shared\n",shared);
90         printk("%d pages swap cached\n",cached);
91 }
92
93 /* References to section boundaries */
94
95 extern unsigned long _text;
96 extern unsigned long _etext;
97 extern unsigned long _edata;
98 extern unsigned long __bss_start;
99 extern unsigned long _end;
100
101 extern unsigned long __init_begin;
102 extern unsigned long __init_end;
103
104 extern unsigned long __initdata zholes_size[];
105 /*
106  * paging_init() sets up the page tables
107  */
108
109 #ifndef CONFIG_ARCH_S390X
110 void __init paging_init(void)
111 {
112         pgd_t * pg_dir;
113         pte_t * pg_table;
114         pte_t   pte;
115         int     i;
116         unsigned long tmp;
117         unsigned long pfn = 0;
118         unsigned long pgdir_k = (__pa(swapper_pg_dir) & PAGE_MASK) | _KERNSEG_TABLE;
119         static const int ssm_mask = 0x04000000L;
120
121         /* unmap whole virtual address space */
122         
123         pg_dir = swapper_pg_dir;
124
125         for (i=0;i<KERNEL_PGD_PTRS;i++) 
126                 pmd_clear((pmd_t*)pg_dir++);
127                 
128         /*
129          * map whole physical memory to virtual memory (identity mapping) 
130          */
131
132         pg_dir = swapper_pg_dir;
133
134         while (pfn < max_low_pfn) {
135                 /*
136                  * pg_table is physical at this point
137                  */
138                 pg_table = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
139
140                 pg_dir->pgd0 =  (_PAGE_TABLE | __pa(pg_table));
141                 pg_dir->pgd1 =  (_PAGE_TABLE | (__pa(pg_table)+1024));
142                 pg_dir->pgd2 =  (_PAGE_TABLE | (__pa(pg_table)+2048));
143                 pg_dir->pgd3 =  (_PAGE_TABLE | (__pa(pg_table)+3072));
144                 pg_dir++;
145
146                 for (tmp = 0 ; tmp < PTRS_PER_PTE ; tmp++,pg_table++) {
147                         pte = pfn_pte(pfn, PAGE_KERNEL);
148                         if (pfn >= max_low_pfn)
149                                 pte_clear(&init_mm, 0, &pte);
150                         set_pte(pg_table, pte);
151                         pfn++;
152                 }
153         }
154
155         S390_lowcore.kernel_asce = pgdir_k;
156
157         /* enable virtual mapping in kernel mode */
158         __asm__ __volatile__("    LCTL  1,1,%0\n"
159                              "    LCTL  7,7,%0\n"
160                              "    LCTL  13,13,%0\n"
161                              "    SSM   %1" 
162                              : : "m" (pgdir_k), "m" (ssm_mask));
163
164         local_flush_tlb();
165
166         {
167                 unsigned long zones_size[MAX_NR_ZONES];
168
169                 memset(zones_size, 0, sizeof(zones_size));
170                 zones_size[ZONE_DMA] = max_low_pfn;
171                 free_area_init_node(0, &contig_page_data, zones_size,
172                                     __pa(PAGE_OFFSET) >> PAGE_SHIFT,
173                                     zholes_size);
174         }
175         return;
176 }
177
178 #else /* CONFIG_ARCH_S390X */
179 void __init paging_init(void)
180 {
181         pgd_t * pg_dir;
182         pmd_t * pm_dir;
183         pte_t * pt_dir;
184         pte_t   pte;
185         int     i,j,k;
186         unsigned long pfn = 0;
187         unsigned long pgdir_k = (__pa(swapper_pg_dir) & PAGE_MASK) |
188           _KERN_REGION_TABLE;
189         static const int ssm_mask = 0x04000000L;
190
191         unsigned long zones_size[MAX_NR_ZONES];
192         unsigned long dma_pfn, high_pfn;
193
194         memset(zones_size, 0, sizeof(zones_size));
195         dma_pfn = MAX_DMA_ADDRESS >> PAGE_SHIFT;
196         high_pfn = max_low_pfn;
197
198         if (dma_pfn > high_pfn)
199                 zones_size[ZONE_DMA] = high_pfn;
200         else {
201                 zones_size[ZONE_DMA] = dma_pfn;
202                 zones_size[ZONE_NORMAL] = high_pfn - dma_pfn;
203         }
204
205         /* Initialize mem_map[].  */
206         free_area_init_node(0, &contig_page_data, zones_size,
207                             __pa(PAGE_OFFSET) >> PAGE_SHIFT, zholes_size);
208
209         /*
210          * map whole physical memory to virtual memory (identity mapping) 
211          */
212
213         pg_dir = swapper_pg_dir;
214         
215         for (i = 0 ; i < PTRS_PER_PGD ; i++,pg_dir++) {
216         
217                 if (pfn >= max_low_pfn) {
218                         pgd_clear(pg_dir);
219                         continue;
220                 }          
221         
222                 pm_dir = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE*4);
223                 pgd_populate(&init_mm, pg_dir, pm_dir);
224
225                 for (j = 0 ; j < PTRS_PER_PMD ; j++,pm_dir++) {
226                         if (pfn >= max_low_pfn) {
227                                 pmd_clear(pm_dir);
228                                 continue; 
229                         }          
230                         
231                         pt_dir = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
232                         pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
233         
234                         for (k = 0 ; k < PTRS_PER_PTE ; k++,pt_dir++) {
235                                 pte = pfn_pte(pfn, PAGE_KERNEL);
236                                 if (pfn >= max_low_pfn) {
237                                         pte_clear(&init_mm, 0, &pte); 
238                                         continue;
239                                 }
240                                 set_pte(pt_dir, pte);
241                                 pfn++;
242                         }
243                 }
244         }
245
246         S390_lowcore.kernel_asce = pgdir_k;
247
248         /* enable virtual mapping in kernel mode */
249         __asm__ __volatile__("lctlg 1,1,%0\n\t"
250                              "lctlg 7,7,%0\n\t"
251                              "lctlg 13,13,%0\n\t"
252                              "ssm   %1"
253                              : :"m" (pgdir_k), "m" (ssm_mask));
254
255         local_flush_tlb();
256
257         return;
258 }
259 #endif /* CONFIG_ARCH_S390X */
260
261 void __init mem_init(void)
262 {
263         unsigned long codesize, reservedpages, datasize, initsize;
264
265         max_mapnr = num_physpages = max_low_pfn;
266         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
267
268         /* clear the zero-page */
269         memset(empty_zero_page, 0, PAGE_SIZE);
270
271         /* this will put all low memory onto the freelists */
272         totalram_pages += free_all_bootmem();
273
274         reservedpages = 0;
275
276         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
277         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
278         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
279         printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, %ldk data, %ldk init)\n",
280                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
281                 max_mapnr << (PAGE_SHIFT-10),
282                 codesize >> 10,
283                 reservedpages << (PAGE_SHIFT-10),
284                 datasize >>10,
285                 initsize >> 10);
286 }
287
288 void free_initmem(void)
289 {
290         unsigned long addr;
291
292         addr = (unsigned long)(&__init_begin);
293         for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
294                 ClearPageReserved(virt_to_page(addr));
295                 set_page_count(virt_to_page(addr), 1);
296                 free_page(addr);
297                 totalram_pages++;
298         }
299         printk ("Freeing unused kernel memory: %ldk freed\n",
300                 ((unsigned long)&__init_end - (unsigned long)&__init_begin) >> 10);
301 }
302
303 #ifdef CONFIG_BLK_DEV_INITRD
304 void free_initrd_mem(unsigned long start, unsigned long end)
305 {
306         if (start < end)
307                 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
308         for (; start < end; start += PAGE_SIZE) {
309                 ClearPageReserved(virt_to_page(start));
310                 set_page_count(virt_to_page(start), 1);
311                 free_page(start);
312                 totalram_pages++;
313         }
314 }
315 #endif