0946840ac3cdd042c464d6a63797e1ab6593309c
[muen/linux.git] / arch / nios2 / kernel / setup.c
1 /*
2  * Nios2-specific parts of system setup
3  *
4  * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
5  * Copyright (C) 2004 Microtronix Datacom Ltd.
6  * Copyright (C) 2001 Vic Phillips <vic@microtronix.com>
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License. See the file "COPYING" in the main directory of this archive
10  * for more details.
11  */
12
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/sched/task.h>
18 #include <linux/console.h>
19 #include <linux/bootmem.h>
20 #include <linux/memblock.h>
21 #include <linux/initrd.h>
22 #include <linux/of_fdt.h>
23 #include <linux/screen_info.h>
24
25 #include <asm/mmu_context.h>
26 #include <asm/sections.h>
27 #include <asm/setup.h>
28 #include <asm/cpuinfo.h>
29
30 unsigned long memory_start;
31 EXPORT_SYMBOL(memory_start);
32
33 unsigned long memory_end;
34 EXPORT_SYMBOL(memory_end);
35
36 unsigned long memory_size;
37
38 static struct pt_regs fake_regs = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39                                         0, 0, 0, 0, 0, 0,
40                                         0};
41
42 #ifdef CONFIG_VT
43 struct screen_info screen_info;
44 #endif
45
46 /* Copy a short hook instruction sequence to the exception address */
47 static inline void copy_exception_handler(unsigned int addr)
48 {
49         unsigned int start = (unsigned int) exception_handler_hook;
50         volatile unsigned int tmp = 0;
51
52         if (start == addr) {
53                 /* The CPU exception address already points to the handler. */
54                 return;
55         }
56
57         __asm__ __volatile__ (
58                 "ldw    %2,0(%0)\n"
59                 "stw    %2,0(%1)\n"
60                 "ldw    %2,4(%0)\n"
61                 "stw    %2,4(%1)\n"
62                 "ldw    %2,8(%0)\n"
63                 "stw    %2,8(%1)\n"
64                 "flushd 0(%1)\n"
65                 "flushd 4(%1)\n"
66                 "flushd 8(%1)\n"
67                 "flushi %1\n"
68                 "addi   %1,%1,4\n"
69                 "flushi %1\n"
70                 "addi   %1,%1,4\n"
71                 "flushi %1\n"
72                 "flushp\n"
73                 : /* no output registers */
74                 : "r" (start), "r" (addr), "r" (tmp)
75                 : "memory"
76         );
77 }
78
79 /* Copy the fast TLB miss handler */
80 static inline void copy_fast_tlb_miss_handler(unsigned int addr)
81 {
82         unsigned int start = (unsigned int) fast_handler;
83         unsigned int end = (unsigned int) fast_handler_end;
84         volatile unsigned int tmp = 0;
85
86         __asm__ __volatile__ (
87                 "1:\n"
88                 "       ldw     %3,0(%0)\n"
89                 "       stw     %3,0(%1)\n"
90                 "       flushd  0(%1)\n"
91                 "       flushi  %1\n"
92                 "       flushp\n"
93                 "       addi    %0,%0,4\n"
94                 "       addi    %1,%1,4\n"
95                 "       bne     %0,%2,1b\n"
96                 : /* no output registers */
97                 : "r" (start), "r" (addr), "r" (end), "r" (tmp)
98                 : "memory"
99         );
100 }
101
102 /*
103  * save args passed from u-boot, called from head.S
104  *
105  * @r4: NIOS magic
106  * @r5: initrd start
107  * @r6: initrd end or fdt
108  * @r7: kernel command line
109  */
110 asmlinkage void __init nios2_boot_init(unsigned r4, unsigned r5, unsigned r6,
111                                        unsigned r7)
112 {
113         unsigned dtb_passed = 0;
114         char cmdline_passed[COMMAND_LINE_SIZE] __maybe_unused = { 0, };
115
116 #if defined(CONFIG_NIOS2_PASS_CMDLINE)
117         if (r4 == 0x534f494e) { /* r4 is magic NIOS */
118 #if defined(CONFIG_BLK_DEV_INITRD)
119                 if (r5) { /* initramfs */
120                         initrd_start = r5;
121                         initrd_end = r6;
122                 }
123 #endif /* CONFIG_BLK_DEV_INITRD */
124                 dtb_passed = r6;
125
126                 if (r7)
127                         strncpy(cmdline_passed, (char *)r7, COMMAND_LINE_SIZE);
128         }
129 #endif
130
131         early_init_devtree((void *)dtb_passed);
132
133 #ifndef CONFIG_CMDLINE_FORCE
134         if (cmdline_passed[0])
135                 strncpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
136 #ifdef CONFIG_NIOS2_CMDLINE_IGNORE_DTB
137         else
138                 strncpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
139 #endif
140 #endif
141
142         parse_early_param();
143 }
144
145 void __init setup_arch(char **cmdline_p)
146 {
147         int bootmap_size;
148
149         console_verbose();
150
151         memory_size = memblock_phys_mem_size();
152         memory_start = PAGE_ALIGN((unsigned long)__pa(_end));
153         memory_end = (unsigned long) CONFIG_NIOS2_MEM_BASE + memory_size;
154
155         init_mm.start_code = (unsigned long) _stext;
156         init_mm.end_code = (unsigned long) _etext;
157         init_mm.end_data = (unsigned long) _edata;
158         init_mm.brk = (unsigned long) _end;
159         init_task.thread.kregs = &fake_regs;
160
161         /* Keep a copy of command line */
162         *cmdline_p = boot_command_line;
163
164         min_low_pfn = PFN_UP(memory_start);
165         max_low_pfn = PFN_DOWN(memory_end);
166         max_mapnr = max_low_pfn;
167
168         /*
169          * give all the memory to the bootmap allocator,  tell it to put the
170          * boot mem_map at the start of memory
171          */
172         pr_debug("init_bootmem_node(?,%#lx, %#x, %#lx)\n",
173                 min_low_pfn, PFN_DOWN(PHYS_OFFSET), max_low_pfn);
174         bootmap_size = init_bootmem_node(NODE_DATA(0),
175                                         min_low_pfn, PFN_DOWN(PHYS_OFFSET),
176                                         max_low_pfn);
177
178         /*
179          * free the usable memory,  we have to make sure we do not free
180          * the bootmem bitmap so we then reserve it after freeing it :-)
181          */
182         pr_debug("free_bootmem(%#lx, %#lx)\n",
183                 memory_start, memory_end - memory_start);
184         free_bootmem(memory_start, memory_end - memory_start);
185
186         /*
187          * Reserve the bootmem bitmap itself as well. We do this in two
188          * steps (first step was init_bootmem()) because this catches
189          * the (very unlikely) case of us accidentally initializing the
190          * bootmem allocator with an invalid RAM area.
191          *
192          * Arguments are start, size
193          */
194         pr_debug("reserve_bootmem(%#lx, %#x)\n", memory_start, bootmap_size);
195         reserve_bootmem(memory_start, bootmap_size, BOOTMEM_DEFAULT);
196
197 #ifdef CONFIG_BLK_DEV_INITRD
198         if (initrd_start) {
199                 reserve_bootmem(virt_to_phys((void *)initrd_start),
200                                 initrd_end - initrd_start, BOOTMEM_DEFAULT);
201         }
202 #endif /* CONFIG_BLK_DEV_INITRD */
203
204         early_init_fdt_reserve_self();
205         early_init_fdt_scan_reserved_mem();
206
207         unflatten_and_copy_device_tree();
208
209         setup_cpuinfo();
210
211         copy_exception_handler(cpuinfo.exception_addr);
212
213         mmu_init();
214
215         copy_fast_tlb_miss_handler(cpuinfo.fast_tlb_miss_exc_addr);
216
217         /*
218          * Initialize MMU context handling here because data from cpuinfo is
219          * needed for this.
220          */
221         mmu_context_init();
222
223         /*
224          * get kmalloc into gear
225          */
226         paging_init();
227
228 #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
229         conswitchp = &dummy_con;
230 #endif
231 }