File: /usr/src/linux/arch/cris/mm/init.c

1     /*
2      *  linux/arch/cris/mm/init.c
3      *
4      *  Copyright (C) 1995  Linus Torvalds
5      *  Copyright (C) 2000,2001  Axis Communications AB
6      *
7      *  Authors:  Bjorn Wesen (bjornw@axis.com)
8      *
9      *  $Log: init.c,v $
10      *  Revision 1.29  2001/07/25 16:09:50  bjornw
11      *  val->sharedram will stay 0
12      *
13      *  Revision 1.28  2001/06/28 16:30:17  bjornw
14      *  Oops. This needs to wait until 2.4.6 is merged
15      *
16      *  Revision 1.27  2001/06/28 14:04:07  bjornw
17      *  Fill in sharedram
18      *
19      *  Revision 1.26  2001/06/18 06:36:02  hp
20      *  Enable free_initmem of __init-type pages
21      *
22      *  Revision 1.25  2001/06/13 00:02:23  bjornw
23      *  Use a separate variable to store the current pgd to avoid races in schedule
24      *
25      *  Revision 1.24  2001/05/15 00:52:20  hp
26      *  Only map segment 0xa as seg if CONFIG_JULIETTE
27      *
28      *  Revision 1.23  2001/04/04 14:35:40  bjornw
29      *  * Removed get_pte_slow and friends (2.4.3 change)
30      *  * Removed bad_pmd handling (2.4.3 change)
31      *
32      *  Revision 1.22  2001/04/04 13:38:04  matsfg
33      *  Moved ioremap to a separate function instead
34      *
35      *  Revision 1.21  2001/03/27 09:28:33  bjornw
36      *  ioremap used too early - lets try it in mem_init instead
37      *
38      *  Revision 1.20  2001/03/23 07:39:21  starvik
39      *  Corrected according to review remarks
40      *
41      *  Revision 1.19  2001/03/15 14:25:17  bjornw
42      *  More general shadow registers and ioremaped addresses for external I/O
43      *
44      *  Revision 1.18  2001/02/23 12:46:44  bjornw
45      *  * 0xc was not CSE1; 0x8 is, same as uncached flash, so we move the uncached
46      *    flash during CRIS_LOW_MAP from 0xe to 0x8 so both the flash and the I/O
47      *    is mapped straight over (for !CRIS_LOW_MAP the uncached flash is still 0xe)
48      *
49      *  Revision 1.17  2001/02/22 15:05:21  bjornw
50      *  Map 0x9 straight over during LOW_MAP to allow for memory mapped LEDs
51      *
52      *  Revision 1.16  2001/02/22 15:02:35  bjornw
53      *  Map 0xc straight over during LOW_MAP to allow for memory mapped I/O
54      *
55      *  Revision 1.15  2001/01/10 21:12:10  bjornw
56      *  loops_per_sec -> loops_per_jiffy
57      *
58      *  Revision 1.14  2000/11/22 16:23:20  bjornw
59      *  Initialize totalhigh counters to 0 to make /proc/meminfo look nice.
60      *
61      *  Revision 1.13  2000/11/21 16:37:51  bjornw
62      *  Temporarily disable initmem freeing
63      *
64      *  Revision 1.12  2000/11/21 13:55:07  bjornw
65      *  Use CONFIG_CRIS_LOW_MAP for the low VM map instead of explicit CPU type
66      *
67      *  Revision 1.11  2000/10/06 12:38:22  bjornw
68      *  Cast empty_bad_page correctly (should really be of * type from the start..
69      *
70      *  Revision 1.10  2000/10/04 16:53:57  bjornw
71      *  Fix memory-map due to LX features
72      *
73      *  Revision 1.9  2000/09/13 15:47:49  bjornw
74      *  Wrong count in reserved-pages loop
75      *
76      *  Revision 1.8  2000/09/13 14:35:10  bjornw
77      *  2.4.0-test8 added a new arg to free_area_init_node
78      *
79      *  Revision 1.7  2000/08/17 15:35:55  bjornw
80      *  2.4.0-test6 removed MAP_NR and inserted virt_to_page
81      *
82      *
83      */
84     
85     #include <linux/config.h>
86     #include <linux/signal.h>
87     #include <linux/sched.h>
88     #include <linux/kernel.h>
89     #include <linux/errno.h>
90     #include <linux/string.h>
91     #include <linux/types.h>
92     #include <linux/ptrace.h>
93     #include <linux/mman.h>
94     #include <linux/mm.h>
95     #include <linux/swap.h>
96     #include <linux/smp.h>
97     #include <linux/bootmem.h>
98     
99     #include <asm/system.h>
100     #include <asm/segment.h>
101     #include <asm/pgalloc.h>
102     #include <asm/pgtable.h>
103     #include <asm/dma.h>
104     #include <asm/svinto.h>
105     #include <asm/io.h>
106     #include <asm/mmu_context.h>
107     
108     static unsigned long totalram_pages;
109     
110     struct pgtable_cache_struct quicklists;  /* see asm/pgalloc.h */
111     
112     const char bad_pmd_string[] = "Bad pmd in pte_alloc: %08lx\n";
113     
114     extern void die_if_kernel(char *,struct pt_regs *,long);
115     extern void show_net_buffers(void);
116     extern void tlb_init(void);
117     
118     
119     unsigned long empty_zero_page;
120     
121     /* trim the page-table cache if necessary */
122     
123     int 
124     do_check_pgt_cache(int low, int high)
125     {
126             int freed = 0;
127     
128             if(pgtable_cache_size > high) {
129                     do {
130                             if(pgd_quicklist) {
131                                     free_pgd_slow(get_pgd_fast());
132                                     freed++;
133                             }
134                             if(pmd_quicklist) {
135                                     pmd_free_slow(pmd_alloc_one_fast(NULL, 0));
136                                     freed++;
137                             }
138                             if(pte_quicklist) {
139                                     pte_free_slow(pte_alloc_one_fast(NULL, 0));
140     				freed++;
141                             }
142                     } while(pgtable_cache_size > low);
143             }
144             return freed;
145     }
146     
147     void 
148     show_mem(void)
149     {
150     	int i,free = 0,total = 0,cached = 0, reserved = 0, nonshared = 0;
151     	int shared = 0;
152     
153     	printk("\nMem-info:\n");
154     	show_free_areas();
155     	printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
156     	i = max_mapnr;
157     	while (i-- > 0) {
158     		total++;
159     		if (PageReserved(mem_map+i))
160     			reserved++;
161     		else if (PageSwapCache(mem_map+i))
162     			cached++;
163     		else if (!page_count(mem_map+i))
164     			free++;
165     		else if (page_count(mem_map+i) == 1)
166     			nonshared++;
167     		else
168     			shared += page_count(mem_map+i) - 1;
169     	}
170     	printk("%d pages of RAM\n",total);
171     	printk("%d free pages\n",free);
172     	printk("%d reserved pages\n",reserved);
173     	printk("%d pages nonshared\n",nonshared);
174     	printk("%d pages shared\n",shared);
175     	printk("%d pages swap cached\n",cached);
176     	printk("%ld pages in page table cache\n",pgtable_cache_size);
177     	show_buffers();
178     }
179     
180     /*
181      * The kernel is already mapped with a kernel segment at kseg_c so 
182      * we don't need to map it with a page table. However head.S also
183      * temporarily mapped it at kseg_4 so we should set up the ksegs again,
184      * clear the TLB and do some other paging setup stuff.
185      */
186     
187     void __init 
188     paging_init(void)
189     {
190     	int i;
191     	unsigned long zones_size[MAX_NR_ZONES];
192     
193     	printk("Setting up paging and the MMU.\n");
194     	
195     	/* clear out the init_mm.pgd that will contain the kernel's mappings */
196     
197     	for(i = 0; i < PTRS_PER_PGD; i++)
198     		swapper_pg_dir[i] = __pgd(0);
199     	
200     	/* make sure the current pgd table points to something sane
201     	 * (even if it is most probably not used until the next 
202     	 *  switch_mm)
203     	 */
204     
205     	current_pgd = init_mm.pgd;
206     
207     	/* initialise the TLB (tlb.c) */
208     
209     	tlb_init();
210     
211     	/* see README.mm for details on the KSEG setup */
212     
213     #ifndef CONFIG_CRIS_LOW_MAP
214     	/* This code is for the corrected Etrax-100 LX version 2... */
215     
216     	*R_MMU_KSEG = ( IO_STATE(R_MMU_KSEG, seg_f, seg  ) | /* cached flash */
217     			IO_STATE(R_MMU_KSEG, seg_e, seg  ) | /* uncached flash */
218     			IO_STATE(R_MMU_KSEG, seg_d, page ) | /* vmalloc area */
219     			IO_STATE(R_MMU_KSEG, seg_c, seg  ) | /* kernel area */
220     			IO_STATE(R_MMU_KSEG, seg_b, seg  ) | /* kernel reg area */
221     			IO_STATE(R_MMU_KSEG, seg_a, page ) | /* user area */
222     			IO_STATE(R_MMU_KSEG, seg_9, page ) |
223     			IO_STATE(R_MMU_KSEG, seg_8, page ) |
224     			IO_STATE(R_MMU_KSEG, seg_7, page ) |
225     			IO_STATE(R_MMU_KSEG, seg_6, page ) |
226     			IO_STATE(R_MMU_KSEG, seg_5, page ) |
227     			IO_STATE(R_MMU_KSEG, seg_4, page ) |
228     			IO_STATE(R_MMU_KSEG, seg_3, page ) |
229     			IO_STATE(R_MMU_KSEG, seg_2, page ) |
230     			IO_STATE(R_MMU_KSEG, seg_1, page ) |
231     			IO_STATE(R_MMU_KSEG, seg_0, page ) );
232     
233     	*R_MMU_KBASE_HI = ( IO_FIELD(R_MMU_KBASE_HI, base_f, 0x0 ) |
234     			    IO_FIELD(R_MMU_KBASE_HI, base_e, 0x8 ) |
235     			    IO_FIELD(R_MMU_KBASE_HI, base_d, 0x0 ) |
236     			    IO_FIELD(R_MMU_KBASE_HI, base_c, 0x4 ) |
237     			    IO_FIELD(R_MMU_KBASE_HI, base_b, 0xb ) |
238     			    IO_FIELD(R_MMU_KBASE_HI, base_a, 0x0 ) |
239     			    IO_FIELD(R_MMU_KBASE_HI, base_9, 0x0 ) |
240     			    IO_FIELD(R_MMU_KBASE_HI, base_8, 0x0 ) );
241     	
242     	*R_MMU_KBASE_LO = ( IO_FIELD(R_MMU_KBASE_LO, base_7, 0x0 ) |
243     			    IO_FIELD(R_MMU_KBASE_LO, base_6, 0x0 ) |
244     			    IO_FIELD(R_MMU_KBASE_LO, base_5, 0x0 ) |
245     			    IO_FIELD(R_MMU_KBASE_LO, base_4, 0x0 ) |
246     			    IO_FIELD(R_MMU_KBASE_LO, base_3, 0x0 ) |
247     			    IO_FIELD(R_MMU_KBASE_LO, base_2, 0x0 ) |
248     			    IO_FIELD(R_MMU_KBASE_LO, base_1, 0x0 ) |
249     			    IO_FIELD(R_MMU_KBASE_LO, base_0, 0x0 ) );
250     #else
251     	/* Etrax-100 LX version 1 has a bug so that we cannot map anything
252     	 * across the 0x80000000 boundary, so we need to shrink the user-virtual
253     	 * area to 0x50000000 instead of 0xb0000000 and map things slightly
254     	 * different. The unused areas are marked as paged so that we can catch
255     	 * freak kernel accesses there.
256     	 *
257     	 * The ARTPEC chip is mapped at 0xa so we pass that segment straight
258     	 * through. We cannot vremap it because the vmalloc area is below 0x8
259     	 * and Juliette needs an uncached area above 0x8.
260     	 *
261     	 * Same thing with 0xc and 0x9, which is memory-mapped I/O on some boards.
262     	 * We map them straight over in LOW_MAP, but use vremap in LX version 2.
263     	 */
264     
265     	*R_MMU_KSEG = ( IO_STATE(R_MMU_KSEG, seg_f, page ) | 
266     			IO_STATE(R_MMU_KSEG, seg_e, page ) |
267     			IO_STATE(R_MMU_KSEG, seg_d, page ) | 
268     			IO_STATE(R_MMU_KSEG, seg_c, page ) |   
269     			IO_STATE(R_MMU_KSEG, seg_b, seg  ) |  /* kernel reg area */
270     #ifdef CONFIG_JULIETTE
271     			IO_STATE(R_MMU_KSEG, seg_a, seg  ) |  /* ARTPEC etc. */
272     #else
273     			IO_STATE(R_MMU_KSEG, seg_a, page ) |
274     #endif
275     			IO_STATE(R_MMU_KSEG, seg_9, seg  ) |  /* LED's on some boards */
276     			IO_STATE(R_MMU_KSEG, seg_8, seg  ) |  /* CSE0/1, flash and I/O */
277     			IO_STATE(R_MMU_KSEG, seg_7, page ) |  /* kernel vmalloc area */
278     			IO_STATE(R_MMU_KSEG, seg_6, seg  ) |  /* kernel DRAM area */
279     			IO_STATE(R_MMU_KSEG, seg_5, seg  ) |  /* cached flash */
280     			IO_STATE(R_MMU_KSEG, seg_4, page ) |  /* user area */
281     			IO_STATE(R_MMU_KSEG, seg_3, page ) |  /* user area */
282     			IO_STATE(R_MMU_KSEG, seg_2, page ) |  /* user area */
283     			IO_STATE(R_MMU_KSEG, seg_1, page ) |  /* user area */
284     			IO_STATE(R_MMU_KSEG, seg_0, page ) ); /* user area */
285     
286     	*R_MMU_KBASE_HI = ( IO_FIELD(R_MMU_KBASE_HI, base_f, 0x0 ) |
287     			    IO_FIELD(R_MMU_KBASE_HI, base_e, 0x0 ) |
288     			    IO_FIELD(R_MMU_KBASE_HI, base_d, 0x0 ) |
289     			    IO_FIELD(R_MMU_KBASE_HI, base_c, 0x0 ) |
290     			    IO_FIELD(R_MMU_KBASE_HI, base_b, 0xb ) |
291     #ifdef CONFIG_JULIETTE
292     			    IO_FIELD(R_MMU_KBASE_HI, base_a, 0xa ) |
293     #else
294     			    IO_FIELD(R_MMU_KBASE_HI, base_a, 0x0 ) |
295     #endif
296     			    IO_FIELD(R_MMU_KBASE_HI, base_9, 0x9 ) |
297     			    IO_FIELD(R_MMU_KBASE_HI, base_8, 0x8 ) );
298     	
299     	*R_MMU_KBASE_LO = ( IO_FIELD(R_MMU_KBASE_LO, base_7, 0x0 ) |
300     			    IO_FIELD(R_MMU_KBASE_LO, base_6, 0x4 ) |
301     			    IO_FIELD(R_MMU_KBASE_LO, base_5, 0x0 ) |
302     			    IO_FIELD(R_MMU_KBASE_LO, base_4, 0x0 ) |
303     			    IO_FIELD(R_MMU_KBASE_LO, base_3, 0x0 ) |
304     			    IO_FIELD(R_MMU_KBASE_LO, base_2, 0x0 ) |
305     			    IO_FIELD(R_MMU_KBASE_LO, base_1, 0x0 ) |
306     			    IO_FIELD(R_MMU_KBASE_LO, base_0, 0x0 ) );
307     #endif
308     
309     	*R_MMU_CONTEXT = ( IO_FIELD(R_MMU_CONTEXT, page_id, 0 ) );
310     	
311     	/* The MMU has been enabled ever since head.S but just to make
312     	 * it totally obvious we do it here as well.
313     	 */
314     
315     	*R_MMU_CTRL = ( IO_STATE(R_MMU_CTRL, inv_excp, enable ) |
316     			IO_STATE(R_MMU_CTRL, acc_excp, enable ) |
317     			IO_STATE(R_MMU_CTRL, we_excp,  enable ) );
318     	
319     	*R_MMU_ENABLE = IO_STATE(R_MMU_ENABLE, mmu_enable, enable);
320     
321     	/*
322     	 * initialize the bad page table and bad page to point
323     	 * to a couple of allocated pages
324     	 */
325     
326     	empty_zero_page = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
327     	memset((void *)empty_zero_page, 0, PAGE_SIZE);
328     
329     	/* All pages are DMA'able in Etrax, so put all in the DMA'able zone */
330     
331     	zones_size[0] = ((unsigned long)high_memory - PAGE_OFFSET) >> PAGE_SHIFT;
332     
333     	for (i = 1; i < MAX_NR_ZONES; i++)
334     		zones_size[i] = 0;
335     
336     	/* Use free_area_init_node instead of free_area_init, because the former
337     	 * is designed for systems where the DRAM starts at an address substantially
338     	 * higher than 0, like us (we start at PAGE_OFFSET). This saves space in the
339     	 * mem_map page array.
340     	 */
341     
342     	free_area_init_node(0, 0, 0, zones_size, PAGE_OFFSET, 0);
343     
344     }
345     
346     extern unsigned long loops_per_jiffy; /* init/main.c */
347     unsigned long loops_per_usec;
348     
349     extern char _stext, _edata, _etext;
350     extern char __init_begin, __init_end;
351     
352     void __init
353     mem_init(void)
354     {
355     	int codesize, reservedpages, datasize, initsize;
356     	unsigned long tmp;
357     
358     	if(!mem_map)
359     		BUG();
360     
361     	/* max/min_low_pfn was set by setup.c
362     	 * now we just copy it to some other necessary places...
363     	 *
364     	 * high_memory was also set in setup.c
365     	 */
366     
367     	max_mapnr = num_physpages = max_low_pfn - min_low_pfn;
368      
369     	/* this will put all memory onto the freelists */
370             totalram_pages = free_all_bootmem();
371     
372     	reservedpages = 0;
373     	for (tmp = 0; tmp < max_mapnr; tmp++) {
374     		/*
375                      * Only count reserved RAM pages
376                      */
377     		if (PageReserved(mem_map + tmp))
378     			reservedpages++;
379     	}
380     
381     	codesize =  (unsigned long) &_etext - (unsigned long) &_stext;
382             datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
383             initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
384     	
385             printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, "
386     	       "%dk init)\n" ,
387     	       (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
388     	       max_mapnr << (PAGE_SHIFT-10),
389     	       codesize >> 10,
390     	       reservedpages << (PAGE_SHIFT-10),
391     	       datasize >> 10,
392     	       initsize >> 10
393                    );
394     
395     	/* HACK alert - calculate a loops_per_usec for asm/delay.h here
396     	 * since this is called just after calibrate_delay in init/main.c
397     	 * but before places which use udelay. cannot be in time.c since
398     	 * that is called _before_ calibrate_delay
399     	 */
400     
401     	loops_per_usec = (loops_per_jiffy * HZ) / 1000000;
402     
403     	return;
404     }
405     
406     /* Initialize remaps of some I/O-ports. This is designed to be callable
407      * multiple times from the drivers init-sections, because we don't know
408      * beforehand which driver will get initialized first.
409      */
410     
411     void 
412     init_ioremap(void)
413     {
414       
415     	/* Give the external I/O-port addresses their values */
416     
417             static int initialized = 0;
418       
419             if( !initialized ) {
420                     initialized++;
421                 
422     #ifdef CONFIG_CRIS_LOW_MAP
423                    /* Simply a linear map (see the KSEG map above in paging_init) */
424                    port_cse1_addr = (volatile unsigned long *)(MEM_CSE1_START | 
425                                                                MEM_NON_CACHEABLE);
426                    port_csp0_addr = (volatile unsigned long *)(MEM_CSP0_START |
427                                                                MEM_NON_CACHEABLE);
428                    port_csp4_addr = (volatile unsigned long *)(MEM_CSP4_START |
429                                                                MEM_NON_CACHEABLE);
430     #else
431                    /* Note that nothing blows up just because we do this remapping 
432                     * it's ok even if the ports are not used or connected 
433                     * to anything (or connected to a non-I/O thing) */        
434                    port_cse1_addr = (volatile unsigned long *)
435                      ioremap((unsigned long)(MEM_CSE1_START | 
436                                              MEM_NON_CACHEABLE), 16);
437                    port_csp0_addr = (volatile unsigned long *)
438                      ioremap((unsigned long)(MEM_CSP0_START |
439                                              MEM_NON_CACHEABLE), 16);
440                    port_csp4_addr = (volatile unsigned long *)
441                      ioremap((unsigned long)(MEM_CSP4_START |
442                                              MEM_NON_CACHEABLE), 16);
443     #endif	
444             }
445     }
446     
447     
448     /* free the pages occupied by initialization code */
449     
450     void 
451     free_initmem(void)
452     {
453             unsigned long addr;
454     
455             addr = (unsigned long)(&__init_begin);
456             for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
457                     ClearPageReserved(virt_to_page(addr));
458                     set_page_count(virt_to_page(addr), 1);
459                     free_page(addr);
460                     totalram_pages++;
461             }
462             printk ("Freeing unused kernel memory: %dk freed\n", 
463     		(&__init_end - &__init_begin) >> 10);
464     }
465     
466     void 
467     si_meminfo(struct sysinfo *val)
468     {
469     	int i;
470     
471     	i = max_mapnr;
472     	val->totalram = 0;
473     	val->sharedram = 0;
474     	val->freeram = nr_free_pages();
475     	val->bufferram = atomic_read(&buffermem_pages);
476     	while (i-- > 0)  {
477     		if (PageReserved(mem_map+i))
478     			continue;
479     		val->totalram++;
480     		if (!atomic_read(&mem_map[i].count))
481     			continue;
482     		val->sharedram += atomic_read(&mem_map[i].count) - 1;
483     	}
484     	val->mem_unit = PAGE_SIZE;
485     	val->totalhigh = 0;
486     	val->freehigh = 0;
487     }
488