move new files out from platform support patch
[lede.git] / target / linux / ubicom32 / files / arch / ubicom32 / mm / init.c
1 /*
2  * arch/ubicom32/mm/init.c
3  *   Ubicom32 architecture virtual memory initialization.
4  *
5  * (C) Copyright 2009, Ubicom, Inc.
6  *  Copyright (C) 1998  D. Jeff Dionne <jeff@lineo.ca>,
7  *                      Kenneth Albanowski <kjahds@kjahds.com>,
8  *  Copyright (C) 2000  Lineo, Inc.  (www.lineo.com)
9  *
10  *  Based on:
11  *
12  *  linux/arch/m68k/mm/init.c
13  *
14  *  Copyright (C) 1995  Hamish Macdonald
15  *
16  *  JAN/1999 -- hacked to support ColdFire (gerg@snapgear.com)
17  *  DEC/2000 -- linux 2.4 support <davidm@snapgear.com>
18  *
19  * This file is part of the Ubicom32 Linux Kernel Port.
20  *
21  * The Ubicom32 Linux Kernel Port is free software: you can redistribute
22  * it and/or modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation, either version 2 of the
24  * License, or (at your option) any later version.
25  *
26  * The Ubicom32 Linux Kernel Port is distributed in the hope that it
27  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
28  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
29  * the GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with the Ubicom32 Linux Kernel Port.  If not,
33  * see <http://www.gnu.org/licenses/>.
34  *
35  * Ubicom32 implementation derived from (with many thanks):
36  *   arch/m68knommu
37  *   arch/blackfin
38  *   arch/parisc
39  */
40
41 #include <linux/signal.h>
42 #include <linux/sched.h>
43 #include <linux/kernel.h>
44 #include <linux/errno.h>
45 #include <linux/string.h>
46 #include <linux/types.h>
47 #include <linux/ptrace.h>
48 #include <linux/mman.h>
49 #include <linux/mm.h>
50 #include <linux/swap.h>
51 #include <linux/init.h>
52 #include <linux/highmem.h>
53 #include <linux/pagemap.h>
54 #include <linux/bootmem.h>
55 #include <linux/slab.h>
56
57 #include <asm/setup.h>
58 #include <asm/segment.h>
59 #include <asm/page.h>
60 #include <asm/pgtable.h>
61 #include <asm/system.h>
62 #include <asm/machdep.h>
63 #include <asm/ocm-alloc.h>
64 #include <asm/processor.h>
65
66 #undef DEBUG
67
68 extern void die_if_kernel(char *,struct pt_regs *,long);
69 extern void free_initmem(void);
70
71 /*
72  * BAD_PAGE is the page that is used for page faults when linux
73  * is out-of-memory. Older versions of linux just did a
74  * do_exit(), but using this instead means there is less risk
75  * for a process dying in kernel mode, possibly leaving a inode
76  * unused etc..
77  *
78  * BAD_PAGETABLE is the accompanying page-table: it is initialized
79  * to point to BAD_PAGE entries.
80  *
81  * ZERO_PAGE is a special page that is used for zero-initialized
82  * data and COW.
83  */
84 static unsigned long empty_bad_page_table;
85
86 static unsigned long empty_bad_page;
87
88 unsigned long empty_zero_page;
89
90 void show_mem(void)
91 {
92     unsigned long i;
93     int free = 0, total = 0, reserved = 0, shared = 0;
94     int cached = 0;
95
96     printk(KERN_INFO "\nMem-info:\n");
97     show_free_areas();
98     i = max_mapnr;
99     while (i-- > 0) {
100         total++;
101         if (PageReserved(mem_map+i))
102             reserved++;
103         else if (PageSwapCache(mem_map+i))
104             cached++;
105         else if (!page_count(mem_map+i))
106             free++;
107         else
108             shared += page_count(mem_map+i) - 1;
109     }
110     printk(KERN_INFO "%d pages of RAM\n",total);
111     printk(KERN_INFO "%d free pages\n",free);
112     printk(KERN_INFO "%d reserved pages\n",reserved);
113     printk(KERN_INFO "%d pages shared\n",shared);
114     printk(KERN_INFO "%d pages swap cached\n",cached);
115 }
116
117 extern unsigned long memory_start;
118 extern unsigned long memory_end;
119 extern char __ocm_free_begin;
120 extern char __ocm_free_end;
121
122 /*
123  * paging_init() continues the virtual memory environment setup which
124  * was begun by the code in arch/head.S.
125  * The parameters are pointers to where to stick the starting and ending
126  * addresses of available kernel virtual memory.
127  */
128 void __init paging_init(void)
129 {
130         /*
131          * Make sure start_mem is page aligned, otherwise bootmem and
132          * page_alloc get different views of the world.
133          */
134 #ifdef DEBUG
135         unsigned long start_mem = PAGE_ALIGN(memory_start);
136 #endif
137         unsigned long end_mem   = memory_end & PAGE_MASK;
138
139 #ifdef DEBUG
140         printk (KERN_DEBUG "start_mem is %#lx\nvirtual_end is %#lx\n",
141                 start_mem, end_mem);
142 #endif
143
144         /*
145          * Initialize the bad page table and bad page to point
146          * to a couple of allocated pages.
147          */
148         empty_bad_page_table = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
149         empty_bad_page = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
150         empty_zero_page = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
151         memset((void *)empty_zero_page, 0, PAGE_SIZE);
152
153         /*
154          * TODO: enable setting up for user memory management interface.
155          */
156
157 #ifdef DEBUG
158         printk (KERN_DEBUG "before free_area_init\n");
159
160         printk (KERN_DEBUG "free_area_init -> start_mem is %#lx\nvirtual_end is %#lx\n",
161                 start_mem, end_mem);
162 #endif
163
164         {
165                 unsigned long zones_size[MAX_NR_ZONES] = {0, };
166 #ifdef CONFIG_ZONE_DMA
167                 zones_size[ZONE_DMA] = OCMSIZE >> PAGE_SHIFT;
168 #endif
169                 zones_size[ZONE_NORMAL] = (end_mem - PAGE_OFFSET) >> PAGE_SHIFT;
170 #ifdef CONFIG_HIGHMEM
171                 zones_size[ZONE_HIGHMEM] = 0;
172 #endif
173                 free_area_init(zones_size);
174         }
175 }
176
177 void __init mem_init(void)
178 {
179         int codek = 0, datak = 0, initk = 0;
180         unsigned long tmp, ram_start, ram_end, len;
181         extern char _etext, _stext, _sdata, _ebss, __init_begin, __init_end;
182
183         unsigned long start_mem = memory_start; /* DAVIDM - these must start at end of kernel */
184         unsigned long end_mem   = memory_end; /* DAVIDM - this must not include kernel stack at top */
185         processor_dram(&ram_start, &ram_end);
186         len = (ram_end - ram_start) + OCMSIZE;
187 #ifdef DEBUG
188         printk(KERN_DEBUG "Mem_init: start=%lx, end=%lx\n", start_mem, end_mem);
189 #endif
190
191         end_mem &= PAGE_MASK;
192         high_memory = (void *) end_mem;
193
194         start_mem = PAGE_ALIGN(start_mem);
195         max_mapnr = num_physpages = (((unsigned long) high_memory) - PAGE_OFFSET) >> PAGE_SHIFT;
196
197         /* this will put all memory onto the freelists */
198 #ifdef CONFIG_ZONE_DMA
199         {
200                 unsigned long ocm_free_begin = (unsigned long)&__ocm_free_begin;
201                 unsigned long ocm_free_end = (unsigned long)&__ocm_free_end;
202                 unsigned long zone_dma_begin = (ocm_free_begin + PAGE_SIZE - 1) & PAGE_MASK;
203                 unsigned long zone_dma_end = ocm_free_end & PAGE_MASK;
204                 if (zone_dma_end > zone_dma_begin)
205                         free_bootmem(zone_dma_begin, zone_dma_end-zone_dma_begin);
206         }
207 #endif
208         totalram_pages = free_all_bootmem();
209
210         codek = (&_etext - &_stext) >> 10;
211         datak = (&_ebss - &_sdata) >> 10;
212         initk = (&__init_begin - &__init_end) >> 10;
213
214         tmp = nr_free_pages() << PAGE_SHIFT;
215         printk(KERN_INFO "Memory available: %luk/%luk RAM, (%dk kernel code, %dk data)\n",
216                tmp >> 10,
217                len >> 10,
218                codek,
219                datak
220                );
221
222 }
223
224 #ifdef CONFIG_BLK_DEV_INITRD
225 void free_initrd_mem(unsigned long start, unsigned long end)
226 {
227         int pages = 0;
228         for (; start < end; start += PAGE_SIZE) {
229                 ClearPageReserved(virt_to_page(start));
230                 init_page_count(virt_to_page(start));
231                 free_page(start);
232                 totalram_pages++;
233                 pages++;
234         }
235         printk (KERN_NOTICE "Freeing initrd memory: %dk freed\n", pages);
236 }
237 #endif
238
239 void
240 free_initmem()
241 {
242 #ifdef CONFIG_RAMKERNEL
243         unsigned long addr;
244         extern char __init_begin, __init_end;
245         /*
246          * The following code should be cool even if these sections
247          * are not page aligned.
248          */
249         addr = PAGE_ALIGN((unsigned long)(&__init_begin));
250         /* next to check that the page we free is not a partial page */
251         for (; addr + PAGE_SIZE < (unsigned long)(&__init_end); addr +=PAGE_SIZE) {
252                 ClearPageReserved(virt_to_page(addr));
253                 init_page_count(virt_to_page(addr));
254                 free_page(addr);
255                 totalram_pages++;
256         }
257         printk(KERN_NOTICE "Freeing unused kernel memory: %ldk freed (0x%x - 0x%x)\n",
258                         (addr - PAGE_ALIGN((long) &__init_begin)) >> 10,
259                         (int)(PAGE_ALIGN((unsigned long)(&__init_begin))),
260                         (int)(addr - PAGE_SIZE));
261 #endif
262 }