m68k: runtime patching infrastructure
[linux-2.6.git] / arch / m68k / mm / motorola.c
1 /*
2  * linux/arch/m68k/mm/motorola.c
3  *
4  * Routines specific to the Motorola MMU, originally from:
5  * linux/arch/m68k/init.c
6  * which are Copyright (C) 1995 Hamish Macdonald
7  *
8  * Moved 8/20/1999 Sam Creasey
9  */
10
11 #include <linux/module.h>
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/swap.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/init.h>
20 #include <linux/bootmem.h>
21
22 #include <asm/setup.h>
23 #include <asm/uaccess.h>
24 #include <asm/page.h>
25 #include <asm/pgalloc.h>
26 #include <asm/system.h>
27 #include <asm/machdep.h>
28 #include <asm/io.h>
29 #include <asm/dma.h>
30 #ifdef CONFIG_ATARI
31 #include <asm/atari_stram.h>
32 #endif
33
34 #undef DEBUG
35
36 #ifndef mm_cachebits
37 /*
38  * Bits to add to page descriptors for "normal" caching mode.
39  * For 68020/030 this is 0.
40  * For 68040, this is _PAGE_CACHE040 (cachable, copyback)
41  */
42 unsigned long mm_cachebits;
43 EXPORT_SYMBOL(mm_cachebits);
44 #endif
45
46 static pte_t * __init kernel_page_table(void)
47 {
48         pte_t *ptablep;
49
50         ptablep = (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
51
52         clear_page(ptablep);
53         __flush_page_to_ram(ptablep);
54         flush_tlb_kernel_page(ptablep);
55         nocache_page(ptablep);
56
57         return ptablep;
58 }
59
60 static pmd_t *last_pgtable __initdata = NULL;
61 pmd_t *zero_pgtable __initdata = NULL;
62
63 static pmd_t * __init kernel_ptr_table(void)
64 {
65         if (!last_pgtable) {
66                 unsigned long pmd, last;
67                 int i;
68
69                 /* Find the last ptr table that was used in head.S and
70                  * reuse the remaining space in that page for further
71                  * ptr tables.
72                  */
73                 last = (unsigned long)kernel_pg_dir;
74                 for (i = 0; i < PTRS_PER_PGD; i++) {
75                         if (!pgd_present(kernel_pg_dir[i]))
76                                 continue;
77                         pmd = __pgd_page(kernel_pg_dir[i]);
78                         if (pmd > last)
79                                 last = pmd;
80                 }
81
82                 last_pgtable = (pmd_t *)last;
83 #ifdef DEBUG
84                 printk("kernel_ptr_init: %p\n", last_pgtable);
85 #endif
86         }
87
88         last_pgtable += PTRS_PER_PMD;
89         if (((unsigned long)last_pgtable & ~PAGE_MASK) == 0) {
90                 last_pgtable = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
91
92                 clear_page(last_pgtable);
93                 __flush_page_to_ram(last_pgtable);
94                 flush_tlb_kernel_page(last_pgtable);
95                 nocache_page(last_pgtable);
96         }
97
98         return last_pgtable;
99 }
100
101 static unsigned long __init
102 map_chunk (unsigned long addr, long size)
103 {
104 #define PTRTREESIZE (256*1024)
105 #define ROOTTREESIZE (32*1024*1024)
106         static unsigned long virtaddr = PAGE_OFFSET;
107         unsigned long physaddr;
108         pgd_t *pgd_dir;
109         pmd_t *pmd_dir;
110         pte_t *pte_dir;
111
112         physaddr = (addr | m68k_supervisor_cachemode |
113                     _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY);
114         if (CPU_IS_040_OR_060)
115                 physaddr |= _PAGE_GLOBAL040;
116
117         while (size > 0) {
118 #ifdef DEBUG
119                 if (!(virtaddr & (PTRTREESIZE-1)))
120                         printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
121                                 virtaddr);
122 #endif
123                 pgd_dir = pgd_offset_k(virtaddr);
124                 if (virtaddr && CPU_IS_020_OR_030) {
125                         if (!(virtaddr & (ROOTTREESIZE-1)) &&
126                             size >= ROOTTREESIZE) {
127 #ifdef DEBUG
128                                 printk ("[very early term]");
129 #endif
130                                 pgd_val(*pgd_dir) = physaddr;
131                                 size -= ROOTTREESIZE;
132                                 virtaddr += ROOTTREESIZE;
133                                 physaddr += ROOTTREESIZE;
134                                 continue;
135                         }
136                 }
137                 if (!pgd_present(*pgd_dir)) {
138                         pmd_dir = kernel_ptr_table();
139 #ifdef DEBUG
140                         printk ("[new pointer %p]", pmd_dir);
141 #endif
142                         pgd_set(pgd_dir, pmd_dir);
143                 } else
144                         pmd_dir = pmd_offset(pgd_dir, virtaddr);
145
146                 if (CPU_IS_020_OR_030) {
147                         if (virtaddr) {
148 #ifdef DEBUG
149                                 printk ("[early term]");
150 #endif
151                                 pmd_dir->pmd[(virtaddr/PTRTREESIZE) & 15] = physaddr;
152                                 physaddr += PTRTREESIZE;
153                         } else {
154                                 int i;
155 #ifdef DEBUG
156                                 printk ("[zero map]");
157 #endif
158                                 zero_pgtable = kernel_ptr_table();
159                                 pte_dir = (pte_t *)zero_pgtable;
160                                 pmd_dir->pmd[0] = virt_to_phys(pte_dir) |
161                                         _PAGE_TABLE | _PAGE_ACCESSED;
162                                 pte_val(*pte_dir++) = 0;
163                                 physaddr += PAGE_SIZE;
164                                 for (i = 1; i < 64; physaddr += PAGE_SIZE, i++)
165                                         pte_val(*pte_dir++) = physaddr;
166                         }
167                         size -= PTRTREESIZE;
168                         virtaddr += PTRTREESIZE;
169                 } else {
170                         if (!pmd_present(*pmd_dir)) {
171 #ifdef DEBUG
172                                 printk ("[new table]");
173 #endif
174                                 pte_dir = kernel_page_table();
175                                 pmd_set(pmd_dir, pte_dir);
176                         }
177                         pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
178
179                         if (virtaddr) {
180                                 if (!pte_present(*pte_dir))
181                                         pte_val(*pte_dir) = physaddr;
182                         } else
183                                 pte_val(*pte_dir) = 0;
184                         size -= PAGE_SIZE;
185                         virtaddr += PAGE_SIZE;
186                         physaddr += PAGE_SIZE;
187                 }
188
189         }
190 #ifdef DEBUG
191         printk("\n");
192 #endif
193
194         return virtaddr;
195 }
196
197 /*
198  * paging_init() continues the virtual memory environment setup which
199  * was begun by the code in arch/head.S.
200  */
201 void __init paging_init(void)
202 {
203         int chunk;
204         unsigned long mem_avail = 0;
205         unsigned long zones_size[MAX_NR_ZONES] = { 0, };
206
207 #ifdef DEBUG
208         {
209                 extern unsigned long availmem;
210                 printk ("start of paging_init (%p, %lx, %lx, %lx)\n",
211                         kernel_pg_dir, availmem, start_mem, end_mem);
212         }
213 #endif
214
215         /* Fix the cache mode in the page descriptors for the 680[46]0.  */
216         if (CPU_IS_040_OR_060) {
217                 int i;
218 #ifndef mm_cachebits
219                 mm_cachebits = _PAGE_CACHE040;
220 #endif
221                 for (i = 0; i < 16; i++)
222                         pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
223         }
224
225         module_fixup(NULL, __start_fixup, __stop_fixup);
226         flush_icache();
227
228         /*
229          * Map the physical memory available into the kernel virtual
230          * address space.  It may allocate some memory for page
231          * tables and thus modify availmem.
232          */
233
234         for (chunk = 0; chunk < m68k_num_memory; chunk++) {
235                 mem_avail = map_chunk (m68k_memory[chunk].addr,
236                                        m68k_memory[chunk].size);
237
238         }
239
240         flush_tlb_all();
241 #ifdef DEBUG
242         printk ("memory available is %ldKB\n", mem_avail >> 10);
243         printk ("start_mem is %#lx\nvirtual_end is %#lx\n",
244                 start_mem, end_mem);
245 #endif
246
247         /*
248          * initialize the bad page table and bad page to point
249          * to a couple of allocated pages
250          */
251         empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
252         memset(empty_zero_page, 0, PAGE_SIZE);
253
254         /*
255          * Set up SFC/DFC registers
256          */
257         set_fs(KERNEL_DS);
258
259 #ifdef DEBUG
260         printk ("before free_area_init\n");
261 #endif
262         zones_size[ZONE_DMA] = (mach_max_dma_address < (unsigned long)high_memory ?
263                                 (mach_max_dma_address+1) : (unsigned long)high_memory);
264         zones_size[ZONE_NORMAL] = (unsigned long)high_memory - zones_size[0];
265
266         zones_size[ZONE_DMA] = (zones_size[ZONE_DMA] - PAGE_OFFSET) >> PAGE_SHIFT;
267         zones_size[ZONE_NORMAL] >>= PAGE_SHIFT;
268
269         free_area_init(zones_size);
270 }
271
272 extern char __init_begin, __init_end;
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                 virt_to_page(addr)->flags &= ~(1 << PG_reserved);
281                 init_page_count(virt_to_page(addr));
282                 free_page(addr);
283                 totalram_pages++;
284         }
285 }
286
287