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