1 /*
2  * Copyright 2004-2009 Analog Devices Inc.
3  *
4  * Licensed under the GPL-2 or later.
5  */
6 
7 #include <linux/gfp.h>
8 #include <linux/swap.h>
9 #include <linux/bootmem.h>
10 #include <linux/uaccess.h>
11 #include <linux/export.h>
12 #include <asm/bfin-global.h>
13 #include <asm/pda.h>
14 #include <asm/cplbinit.h>
15 #include <asm/early_printk.h>
16 #include "blackfin_sram.h"
17 
18 /*
19  * ZERO_PAGE is a special page that is used for zero-initialized data and COW.
20  * Let the bss do its zero-init magic so we don't have to do it ourselves.
21  */
22 char empty_zero_page[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
23 EXPORT_SYMBOL(empty_zero_page);
24 
25 #ifndef CONFIG_EXCEPTION_L1_SCRATCH
26 #if defined CONFIG_SYSCALL_TAB_L1
27 __attribute__((l1_data))
28 #endif
29 static unsigned long exception_stack[NR_CPUS][1024];
30 #endif
31 
32 struct blackfin_pda cpu_pda[NR_CPUS];
33 EXPORT_SYMBOL(cpu_pda);
34 
35 /*
36  * paging_init() continues the virtual memory environment setup which
37  * was begun by the code in arch/head.S.
38  * The parameters are pointers to where to stick the starting and ending
39  * addresses  of available kernel virtual memory.
40  */
paging_init(void)41 void __init paging_init(void)
42 {
43 	/*
44 	 * make sure start_mem is page aligned, otherwise bootmem and
45 	 * page_alloc get different views of the world
46 	 */
47 	unsigned long end_mem = memory_end & PAGE_MASK;
48 
49 	unsigned long zones_size[MAX_NR_ZONES] = {
50 		[0] = 0,
51 		[ZONE_DMA] = (end_mem - PAGE_OFFSET) >> PAGE_SHIFT,
52 		[ZONE_NORMAL] = 0,
53 #ifdef CONFIG_HIGHMEM
54 		[ZONE_HIGHMEM] = 0,
55 #endif
56 	};
57 
58 	/* Set up SFC/DFC registers (user data space) */
59 	set_fs(KERNEL_DS);
60 
61 	pr_debug("free_area_init -> start_mem is %#lx virtual_end is %#lx\n",
62 	        PAGE_ALIGN(memory_start), end_mem);
63 	free_area_init(zones_size);
64 }
65 
init_pda(void)66 asmlinkage void __init init_pda(void)
67 {
68 	unsigned int cpu = raw_smp_processor_id();
69 
70 	early_shadow_stamp();
71 
72 	/* Initialize the PDA fields holding references to other parts
73 	   of the memory. The content of such memory is still
74 	   undefined at the time of the call, we are only setting up
75 	   valid pointers to it. */
76 	memset(&cpu_pda[cpu], 0, sizeof(cpu_pda[cpu]));
77 
78 	cpu_pda[0].next = &cpu_pda[1];
79 	cpu_pda[1].next = &cpu_pda[0];
80 
81 #ifdef CONFIG_EXCEPTION_L1_SCRATCH
82 	cpu_pda[cpu].ex_stack = (unsigned long *)(L1_SCRATCH_START + \
83 					L1_SCRATCH_LENGTH);
84 #else
85 	cpu_pda[cpu].ex_stack = exception_stack[cpu + 1];
86 #endif
87 
88 #ifdef CONFIG_SMP
89 	cpu_pda[cpu].imask = 0x1f;
90 #endif
91 }
92 
mem_init(void)93 void __init mem_init(void)
94 {
95 	unsigned int codek = 0, datak = 0, initk = 0;
96 	unsigned int reservedpages = 0, freepages = 0;
97 	unsigned long tmp;
98 	unsigned long start_mem = memory_start;
99 	unsigned long end_mem = memory_end;
100 
101 	end_mem &= PAGE_MASK;
102 	high_memory = (void *)end_mem;
103 
104 	start_mem = PAGE_ALIGN(start_mem);
105 	max_mapnr = num_physpages = MAP_NR(high_memory);
106 	printk(KERN_DEBUG "Kernel managed physical pages: %lu\n", num_physpages);
107 
108 	/* This will put all memory onto the freelists. */
109 	totalram_pages = free_all_bootmem();
110 
111 	reservedpages = 0;
112 	for (tmp = 0; tmp < max_mapnr; tmp++)
113 		if (PageReserved(pfn_to_page(tmp)))
114 			reservedpages++;
115 	freepages =  max_mapnr - reservedpages;
116 
117 	/* do not count in kernel image between _rambase and _ramstart */
118 	reservedpages -= (_ramstart - _rambase) >> PAGE_SHIFT;
119 #if (defined(CONFIG_BFIN_EXTMEM_ICACHEABLE) && ANOMALY_05000263)
120 	reservedpages += (_ramend - memory_end - DMA_UNCACHED_REGION) >> PAGE_SHIFT;
121 #endif
122 
123 	codek = (_etext - _stext) >> 10;
124 	initk = (__init_end - __init_begin) >> 10;
125 	datak = ((_ramstart - _rambase) >> 10) - codek - initk;
126 
127 	printk(KERN_INFO
128 	     "Memory available: %luk/%luk RAM, "
129 		"(%uk init code, %uk kernel code, %uk data, %uk dma, %uk reserved)\n",
130 		(unsigned long) freepages << (PAGE_SHIFT-10), _ramend >> 10,
131 		initk, codek, datak, DMA_UNCACHED_REGION >> 10, (reservedpages << (PAGE_SHIFT-10)));
132 }
133 
free_init_pages(const char * what,unsigned long begin,unsigned long end)134 static void __init free_init_pages(const char *what, unsigned long begin, unsigned long end)
135 {
136 	unsigned long addr;
137 	/* next to check that the page we free is not a partial page */
138 	for (addr = begin; addr + PAGE_SIZE <= end; addr += PAGE_SIZE) {
139 		ClearPageReserved(virt_to_page(addr));
140 		init_page_count(virt_to_page(addr));
141 		free_page(addr);
142 		totalram_pages++;
143 	}
144 	printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
145 }
146 
147 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)148 void __init free_initrd_mem(unsigned long start, unsigned long end)
149 {
150 #ifndef CONFIG_MPU
151 	free_init_pages("initrd memory", start, end);
152 #endif
153 }
154 #endif
155 
free_initmem(void)156 void __init_refok free_initmem(void)
157 {
158 #if defined CONFIG_RAMKERNEL && !defined CONFIG_MPU
159 	free_init_pages("unused kernel memory",
160 			(unsigned long)(&__init_begin),
161 			(unsigned long)(&__init_end));
162 
163 	if (memory_start == (unsigned long)(&__init_end))
164 		memory_start = (unsigned long)(&__init_begin);
165 #endif
166 }
167