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 #ifdef CONFIG_BLK_DEV_INITRD
26 #include <linux/blk.h>
27 #endif
28 #include <linux/pagemap.h>
29 #include <linux/bootmem.h>
30 
31 #include <asm/processor.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
35 #include <asm/pgalloc.h>
36 #include <asm/dma.h>
37 #include <asm/lowcore.h>
38 #include <asm/tlb.h>
39 
40 mmu_gather_t mmu_gathers[NR_CPUS];
41 
42 static unsigned long totalram_pages;
43 extern unsigned long memory_size;
44 
45 pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__((__aligned__(PAGE_SIZE)));
46 char  empty_zero_page[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
47 
do_check_pgt_cache(int low,int high)48 int do_check_pgt_cache(int low, int high)
49 {
50         int freed = 0;
51         if(pgtable_cache_size > high) {
52                 do {
53                         if(pgd_quicklist) {
54                                 free_pgd_slow(get_pgd_fast());
55 				freed += 2;
56 			}
57                         if(pmd_quicklist) {
58                                 pmd_free_slow(pmd_alloc_one_fast(NULL, 0));
59 				freed++;
60 			}
61                         if(pte_quicklist) {
62                                 pte_free_slow(pte_alloc_one_fast(NULL, 0));
63 				freed++;
64 			}
65                 } while(pgtable_cache_size > low);
66         }
67         return freed;
68 }
69 
diag10(unsigned long addr)70 void diag10(unsigned long addr)
71 {
72         asm volatile ("diag %0,%0,0x10" : : "a" (addr));
73 }
74 
show_mem(void)75 void show_mem(void)
76 {
77         int i, total = 0, reserved = 0;
78         int shared = 0, cached = 0;
79 
80         printk("Mem-info:\n");
81         show_free_areas();
82         printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
83         i = max_mapnr;
84         while (i-- > 0) {
85                 total++;
86                 if (PageReserved(mem_map+i))
87                         reserved++;
88                 else if (PageSwapCache(mem_map+i))
89                         cached++;
90                 else if (page_count(mem_map+i))
91                         shared += atomic_read(&mem_map[i].count) - 1;
92         }
93         printk("%d pages of RAM\n",total);
94         printk("%d reserved pages\n",reserved);
95         printk("%d pages shared\n",shared);
96         printk("%d pages swap cached\n",cached);
97         printk("%ld pages in page table cache\n",pgtable_cache_size);
98         show_buffers();
99 }
100 
101 /* References to section boundaries */
102 
103 extern unsigned long _text;
104 extern unsigned long _etext;
105 extern unsigned long _edata;
106 extern unsigned long __bss_start;
107 extern unsigned long _end;
108 
109 extern unsigned long __init_begin;
110 extern unsigned long __init_end;
111 
112 /*
113  * paging_init() sets up the page tables - note that the first 4MB are
114  * already mapped by head.S.
115  * paging_init will erase this initial mapping
116  */
117 
118 unsigned long last_valid_pfn;
119 
paging_init(void)120 void __init paging_init(void)
121 {
122         pgd_t * pg_dir;
123         pte_t * pg_table;
124         pte_t   pte;
125 	int     i;
126         unsigned long tmp;
127         unsigned long address=0;
128         unsigned long pgdir_k = (__pa(swapper_pg_dir) & PAGE_MASK) | _KERNSEG_TABLE;
129 	unsigned long end_mem = (unsigned long) __va(max_low_pfn*PAGE_SIZE);
130         static const int ssm_mask = 0x04000000L;
131 
132 	/* unmap whole virtual address space */
133 
134         pg_dir = swapper_pg_dir;
135 
136 	for (i=0;i<KERNEL_PGD_PTRS;i++)
137 	        pmd_clear((pmd_t*)pg_dir++);
138 
139 	/*
140 	 * map whole physical memory to virtual memory (identity mapping)
141 	 */
142 
143         pg_dir = swapper_pg_dir;
144 
145         while (address < end_mem) {
146                 /*
147                  * pg_table is physical at this point
148                  */
149 		pg_table = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
150 
151                 pg_dir->pgd0 =  (_PAGE_TABLE | __pa(pg_table));
152                 pg_dir->pgd1 =  (_PAGE_TABLE | (__pa(pg_table)+1024));
153                 pg_dir->pgd2 =  (_PAGE_TABLE | (__pa(pg_table)+2048));
154                 pg_dir->pgd3 =  (_PAGE_TABLE | (__pa(pg_table)+3072));
155                 pg_dir++;
156 
157                 for (tmp = 0 ; tmp < PTRS_PER_PTE ; tmp++,pg_table++) {
158                         pte = mk_pte_phys(address, PAGE_KERNEL);
159                         if (address >= end_mem)
160                                 pte_clear(&pte);
161                         set_pte(pg_table, pte);
162                         address += PAGE_SIZE;
163                 }
164         }
165 
166         /* enable virtual mapping in kernel mode */
167         __asm__ __volatile__("    LCTL  1,1,%0\n"
168                              "    LCTL  7,7,%0\n"
169                              "    LCTL  13,13,%0\n"
170                              "    SSM   %1"
171 			     : : "m" (pgdir_k), "m" (ssm_mask));
172 
173         local_flush_tlb();
174 
175 	{
176 		unsigned long zones_size[MAX_NR_ZONES] = { 0, 0, 0};
177 
178 		zones_size[ZONE_DMA] = max_low_pfn;
179 		free_area_init(zones_size);
180 	}
181 
182         return;
183 }
184 
mem_init(void)185 void __init mem_init(void)
186 {
187 	int codesize, reservedpages, datasize, initsize;
188 
189         max_mapnr = num_physpages = max_low_pfn;
190         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
191 
192         /* clear the zero-page */
193         memset(empty_zero_page, 0, PAGE_SIZE);
194 
195 	/* this will put all low memory onto the freelists */
196 	totalram_pages += free_all_bootmem();
197 
198 	reservedpages = 0;
199 
200 	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
201 	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
202 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
203         printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
204                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
205                 max_mapnr << (PAGE_SHIFT-10),
206                 codesize >> 10,
207                 reservedpages << (PAGE_SHIFT-10),
208                 datasize >>10,
209                 initsize >> 10);
210 }
211 
free_initmem(void)212 void free_initmem(void)
213 {
214         unsigned long addr;
215 
216         addr = (unsigned long)(&__init_begin);
217         for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
218 		ClearPageReserved(virt_to_page(addr));
219 		set_page_count(virt_to_page(addr), 1);
220 		free_page(addr);
221 		totalram_pages++;
222         }
223         printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n",
224 		(&__init_end - &__init_begin) >> 10);
225 }
226 
227 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)228 void free_initrd_mem(unsigned long start, unsigned long end)
229 {
230         if (start < end)
231                 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
232         for (; start < end; start += PAGE_SIZE) {
233                 ClearPageReserved(virt_to_page(start));
234                 set_page_count(virt_to_page(start), 1);
235                 free_page(start);
236                 totalram_pages++;
237         }
238 }
239 #endif
240 
si_meminfo(struct sysinfo * val)241 void si_meminfo(struct sysinfo *val)
242 {
243 	val->totalram = totalram_pages;
244 	val->sharedram = 0;
245 	val->freeram = nr_free_pages();
246 	val->bufferram = atomic_read(&buffermem_pages);
247 	val->totalhigh = 0;
248 	val->freehigh = 0;
249 	val->mem_unit = PAGE_SIZE;
250 }
251