1// SPDX-License-Identifier: GPL-2.0
2/*
3 *  linux/arch/sparc/kernel/setup.c
4 *
5 *  Copyright (C) 1995  David S. Miller (davem@caip.rutgers.edu)
6 *  Copyright (C) 2000  Anton Blanchard (anton@samba.org)
7 */
8
9#include <linux/errno.h>
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/mm.h>
13#include <linux/stddef.h>
14#include <linux/unistd.h>
15#include <linux/ptrace.h>
16#include <linux/slab.h>
17#include <linux/initrd.h>
18#include <asm/smp.h>
19#include <linux/user.h>
20#include <linux/delay.h>
21#include <linux/fs.h>
22#include <linux/seq_file.h>
23#include <linux/syscalls.h>
24#include <linux/kdev_t.h>
25#include <linux/major.h>
26#include <linux/string.h>
27#include <linux/init.h>
28#include <linux/interrupt.h>
29#include <linux/console.h>
30#include <linux/spinlock.h>
31#include <linux/root_dev.h>
32#include <linux/cpu.h>
33#include <linux/kdebug.h>
34#include <linux/export.h>
35#include <linux/start_kernel.h>
36#include <uapi/linux/mount.h>
37
38#include <asm/io.h>
39#include <asm/processor.h>
40#include <asm/oplib.h>
41#include <asm/page.h>
42#include <asm/traps.h>
43#include <asm/vaddrs.h>
44#include <asm/mbus.h>
45#include <asm/idprom.h>
46#include <asm/cpudata.h>
47#include <asm/setup.h>
48#include <asm/cacheflush.h>
49#include <asm/sections.h>
50
51#include "kernel.h"
52
53/* Typing sync at the prom prompt calls the function pointed to by
54 * romvec->pv_synchook which I set to the following function.
55 * This should sync all filesystems and return, for now it just
56 * prints out pretty messages and returns.
57 */
58
59/* Pretty sick eh? */
60static void prom_sync_me(void)
61{
62	unsigned long prom_tbr, flags;
63
64	/* XXX Badly broken. FIX! - Anton */
65	local_irq_save(flags);
66	__asm__ __volatile__("rd %%tbr, %0\n\t" : "=r" (prom_tbr));
67	__asm__ __volatile__("wr %0, 0x0, %%tbr\n\t"
68			     "nop\n\t"
69			     "nop\n\t"
70			     "nop\n\t" : : "r" (&trapbase[0]));
71
72	prom_printf("PROM SYNC COMMAND...\n");
73	show_mem();
74	if (!is_idle_task(current)) {
75		local_irq_enable();
76		ksys_sync();
77		local_irq_disable();
78	}
79	prom_printf("Returning to prom\n");
80
81	__asm__ __volatile__("wr %0, 0x0, %%tbr\n\t"
82			     "nop\n\t"
83			     "nop\n\t"
84			     "nop\n\t" : : "r" (prom_tbr));
85	local_irq_restore(flags);
86}
87
88static unsigned int boot_flags __initdata = 0;
89#define BOOTME_DEBUG  0x1
90
91/* Exported for mm/init.c:paging_init. */
92unsigned long cmdline_memory_size __initdata = 0;
93
94/* which CPU booted us (0xff = not set) */
95unsigned char boot_cpu_id = 0xff; /* 0xff will make it into DATA section... */
96
97static void
98prom_console_write(struct console *con, const char *s, unsigned int n)
99{
100	prom_write(s, n);
101}
102
103static struct console prom_early_console = {
104	.name =		"earlyprom",
105	.write =	prom_console_write,
106	.flags =	CON_PRINTBUFFER | CON_BOOT,
107	.index =	-1,
108};
109
110/*
111 * Process kernel command line switches that are specific to the
112 * SPARC or that require special low-level processing.
113 */
114static void __init process_switch(char c)
115{
116	switch (c) {
117	case 'd':
118		boot_flags |= BOOTME_DEBUG;
119		break;
120	case 's':
121		break;
122	case 'h':
123		prom_printf("boot_flags_init: Halt!\n");
124		prom_halt();
125		break;
126	case 'p':
127		prom_early_console.flags &= ~CON_BOOT;
128		break;
129	default:
130		printk("Unknown boot switch (-%c)\n", c);
131		break;
132	}
133}
134
135static void __init boot_flags_init(char *commands)
136{
137	while (*commands) {
138		/* Move to the start of the next "argument". */
139		while (*commands == ' ')
140			commands++;
141
142		/* Process any command switches, otherwise skip it. */
143		if (*commands == '\0')
144			break;
145		if (*commands == '-') {
146			commands++;
147			while (*commands && *commands != ' ')
148				process_switch(*commands++);
149			continue;
150		}
151		if (!strncmp(commands, "mem=", 4)) {
152			/*
153			 * "mem=XXX[kKmM] overrides the PROM-reported
154			 * memory size.
155			 */
156			cmdline_memory_size = simple_strtoul(commands + 4,
157						     &commands, 0);
158			if (*commands == 'K' || *commands == 'k') {
159				cmdline_memory_size <<= 10;
160				commands++;
161			} else if (*commands=='M' || *commands=='m') {
162				cmdline_memory_size <<= 20;
163				commands++;
164			}
165		}
166		while (*commands && *commands != ' ')
167			commands++;
168	}
169}
170
171extern unsigned short root_flags;
172extern unsigned short root_dev;
173extern unsigned short ram_flags;
174#define RAMDISK_IMAGE_START_MASK	0x07FF
175#define RAMDISK_PROMPT_FLAG		0x8000
176#define RAMDISK_LOAD_FLAG		0x4000
177
178extern int root_mountflags;
179
180char reboot_command[COMMAND_LINE_SIZE];
181
182struct cpuid_patch_entry {
183	unsigned int	addr;
184	unsigned int	sun4d[3];
185	unsigned int	leon[3];
186};
187extern struct cpuid_patch_entry __cpuid_patch, __cpuid_patch_end;
188
189static void __init per_cpu_patch(void)
190{
191	struct cpuid_patch_entry *p;
192
193	if (sparc_cpu_model == sun4m) {
194		/* Nothing to do, this is what the unpatched code
195		 * targets.
196		 */
197		return;
198	}
199
200	p = &__cpuid_patch;
201	while (p < &__cpuid_patch_end) {
202		unsigned long addr = p->addr;
203		unsigned int *insns;
204
205		switch (sparc_cpu_model) {
206		case sun4d:
207			insns = &p->sun4d[0];
208			break;
209
210		case sparc_leon:
211			insns = &p->leon[0];
212			break;
213		default:
214			prom_printf("Unknown cpu type, halting.\n");
215			prom_halt();
216		}
217		*(unsigned int *) (addr + 0) = insns[0];
218		flushi(addr + 0);
219		*(unsigned int *) (addr + 4) = insns[1];
220		flushi(addr + 4);
221		*(unsigned int *) (addr + 8) = insns[2];
222		flushi(addr + 8);
223
224		p++;
225	}
226}
227
228struct leon_1insn_patch_entry {
229	unsigned int addr;
230	unsigned int insn;
231};
232
233enum sparc_cpu sparc_cpu_model;
234EXPORT_SYMBOL(sparc_cpu_model);
235
236static __init void leon_patch(void)
237{
238	struct leon_1insn_patch_entry *start = (void *)__leon_1insn_patch;
239	struct leon_1insn_patch_entry *end = (void *)__leon_1insn_patch_end;
240
241	/* Default instruction is leon - no patching */
242	if (sparc_cpu_model == sparc_leon)
243		return;
244
245	while (start < end) {
246		unsigned long addr = start->addr;
247
248		*(unsigned int *)(addr) = start->insn;
249		flushi(addr);
250
251		start++;
252	}
253}
254
255struct tt_entry *sparc_ttable;
256
257/* Called from head_32.S - before we have setup anything
258 * in the kernel. Be very careful with what you do here.
259 */
260void __init sparc32_start_kernel(struct linux_romvec *rp)
261{
262	prom_init(rp);
263
264	/* Set sparc_cpu_model */
265	sparc_cpu_model = sun_unknown;
266	if (!strcmp(&cputypval[0], "sun4m"))
267		sparc_cpu_model = sun4m;
268	if (!strcmp(&cputypval[0], "sun4s"))
269		sparc_cpu_model = sun4m; /* CP-1200 with PROM 2.30 -E */
270	if (!strcmp(&cputypval[0], "sun4d"))
271		sparc_cpu_model = sun4d;
272	if (!strcmp(&cputypval[0], "sun4e"))
273		sparc_cpu_model = sun4e;
274	if (!strcmp(&cputypval[0], "sun4u"))
275		sparc_cpu_model = sun4u;
276	if (!strncmp(&cputypval[0], "leon" , 4))
277		sparc_cpu_model = sparc_leon;
278
279	leon_patch();
280	start_kernel();
281}
282
283void __init setup_arch(char **cmdline_p)
284{
285	int i;
286	unsigned long highest_paddr;
287
288	sparc_ttable = &trapbase[0];
289
290	/* Initialize PROM console and command line. */
291	*cmdline_p = prom_getbootargs();
292	strscpy(boot_command_line, *cmdline_p, COMMAND_LINE_SIZE);
293	parse_early_param();
294
295	boot_flags_init(*cmdline_p);
296
297	register_console(&prom_early_console);
298
299	switch(sparc_cpu_model) {
300	case sun4m:
301		pr_info("ARCH: SUN4M\n");
302		break;
303	case sun4d:
304		pr_info("ARCH: SUN4D\n");
305		break;
306	case sun4e:
307		pr_info("ARCH: SUN4E\n");
308		break;
309	case sun4u:
310		pr_info("ARCH: SUN4U\n");
311		break;
312	case sparc_leon:
313		pr_info("ARCH: LEON\n");
314		break;
315	default:
316		pr_info("ARCH: UNKNOWN!\n");
317		break;
318	}
319
320	idprom_init();
321	load_mmu();
322
323	phys_base = 0xffffffffUL;
324	highest_paddr = 0UL;
325	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
326		unsigned long top;
327
328		if (sp_banks[i].base_addr < phys_base)
329			phys_base = sp_banks[i].base_addr;
330		top = sp_banks[i].base_addr +
331			sp_banks[i].num_bytes;
332		if (highest_paddr < top)
333			highest_paddr = top;
334	}
335	pfn_base = phys_base >> PAGE_SHIFT;
336
337	if (!root_flags)
338		root_mountflags &= ~MS_RDONLY;
339	ROOT_DEV = old_decode_dev(root_dev);
340#ifdef CONFIG_BLK_DEV_RAM
341	rd_image_start = ram_flags & RAMDISK_IMAGE_START_MASK;
342#endif
343
344	prom_setsync(prom_sync_me);
345
346	if((boot_flags & BOOTME_DEBUG) && (linux_dbvec != NULL) &&
347	   ((*(short *)linux_dbvec) != -1)) {
348		printk("Booted under KADB. Syncing trap table.\n");
349		(*(linux_dbvec->teach_debugger))();
350	}
351
352	/* Run-time patch instructions to match the cpu model */
353	per_cpu_patch();
354
355	paging_init();
356
357	smp_setup_cpu_possible_map();
358}
359
360extern int stop_a_enabled;
361
362void sun_do_break(void)
363{
364	if (!stop_a_enabled)
365		return;
366
367	printk("\n");
368	flush_user_windows();
369
370	prom_cmdline();
371}
372EXPORT_SYMBOL(sun_do_break);
373
374int stop_a_enabled = 1;
375
376static int __init topology_init(void)
377{
378	int i, ncpus, err;
379
380	/* Count the number of physically present processors in
381	 * the machine, even on uniprocessor, so that /proc/cpuinfo
382	 * output is consistent with 2.4.x
383	 */
384	ncpus = 0;
385	while (!cpu_find_by_instance(ncpus, NULL, NULL))
386		ncpus++;
387	ncpus_probed = ncpus;
388
389	err = 0;
390	for_each_online_cpu(i) {
391		struct cpu *p = kzalloc(sizeof(*p), GFP_KERNEL);
392		if (!p)
393			err = -ENOMEM;
394		else
395			register_cpu(p, i);
396	}
397
398	return err;
399}
400
401subsys_initcall(topology_init);
402
403#if defined(CONFIG_SPARC32) && !defined(CONFIG_SMP)
404void __init arch_cpu_finalize_init(void)
405{
406	cpu_data(0).udelay_val = loops_per_jiffy;
407}
408#endif
409