xref: /openbmc/linux/arch/powerpc/kernel/setup_32.c (revision 547db12fd8a0ce753c6bafd130827f1755c93fe6)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
270d64ceaSPaul Mackerras /*
370d64ceaSPaul Mackerras  * Common prep/pmac/chrp boot and setup code.
470d64ceaSPaul Mackerras  */
570d64ceaSPaul Mackerras 
670d64ceaSPaul Mackerras #include <linux/module.h>
770d64ceaSPaul Mackerras #include <linux/string.h>
870d64ceaSPaul Mackerras #include <linux/sched.h>
970d64ceaSPaul Mackerras #include <linux/init.h>
1070d64ceaSPaul Mackerras #include <linux/kernel.h>
1170d64ceaSPaul Mackerras #include <linux/reboot.h>
1270d64ceaSPaul Mackerras #include <linux/delay.h>
1370d64ceaSPaul Mackerras #include <linux/initrd.h>
1470d64ceaSPaul Mackerras #include <linux/tty.h>
1570d64ceaSPaul Mackerras #include <linux/seq_file.h>
1670d64ceaSPaul Mackerras #include <linux/root_dev.h>
1770d64ceaSPaul Mackerras #include <linux/cpu.h>
1870d64ceaSPaul Mackerras #include <linux/console.h>
1995f72d1eSYinghai Lu #include <linux/memblock.h>
209445aa1aSAl Viro #include <linux/export.h>
21a156c7baSFinn Thain #include <linux/nvram.h>
2270d64ceaSPaul Mackerras 
2370d64ceaSPaul Mackerras #include <asm/io.h>
2470d64ceaSPaul Mackerras #include <asm/prom.h>
2570d64ceaSPaul Mackerras #include <asm/processor.h>
2670d64ceaSPaul Mackerras #include <asm/pgtable.h>
2770d64ceaSPaul Mackerras #include <asm/setup.h>
2870d64ceaSPaul Mackerras #include <asm/smp.h>
2970d64ceaSPaul Mackerras #include <asm/elf.h>
3070d64ceaSPaul Mackerras #include <asm/cputable.h>
3170d64ceaSPaul Mackerras #include <asm/bootx.h>
3270d64ceaSPaul Mackerras #include <asm/btext.h>
3370d64ceaSPaul Mackerras #include <asm/machdep.h>
347c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3570d64ceaSPaul Mackerras #include <asm/pmac_feature.h>
3670d64ceaSPaul Mackerras #include <asm/sections.h>
3770d64ceaSPaul Mackerras #include <asm/nvram.h>
3870d64ceaSPaul Mackerras #include <asm/xmon.h>
396d7f58b0SKumar Gala #include <asm/time.h>
40463ce0e1SBenjamin Herrenschmidt #include <asm/serial.h>
4151d3082fSBenjamin Herrenschmidt #include <asm/udbg.h>
421cd03890SLEROY Christophe #include <asm/code-patching.h>
43b92a226eSKevin Hao #include <asm/cpu_has_feature.h>
44e82d70cfSMathieu Malaterre #include <asm/asm-prototypes.h>
45db0a2b63SChristophe Leroy #include <asm/kdump.h>
462c86cd18SChristophe Leroy #include <asm/feature-fixups.h>
47265c3491SChristophe Leroy #include <asm/early_ioremap.h>
4870d64ceaSPaul Mackerras 
49f2c6d0d1SMathieu Malaterre #include "setup.h"
50f2c6d0d1SMathieu Malaterre 
5103501dabSPaul Mackerras #define DBG(fmt...)
5203501dabSPaul Mackerras 
5370d64ceaSPaul Mackerras extern void bootx_init(unsigned long r4, unsigned long phys);
5470d64ceaSPaul Mackerras 
5580579e1fSPaul Mackerras int boot_cpuid_phys;
569974eec2SAndrew Gabbasov EXPORT_SYMBOL_GPL(boot_cpuid_phys);
5780579e1fSPaul Mackerras 
5813a9801eSNathan Lynch int smp_hw_index[NR_CPUS];
599445aa1aSAl Viro EXPORT_SYMBOL(smp_hw_index);
6013a9801eSNathan Lynch 
6170d64ceaSPaul Mackerras unsigned long ISA_DMA_THRESHOLD;
6270d64ceaSPaul Mackerras unsigned int DMA_MODE_READ;
6370d64ceaSPaul Mackerras unsigned int DMA_MODE_WRITE;
6470d64ceaSPaul Mackerras 
659445aa1aSAl Viro EXPORT_SYMBOL(DMA_MODE_READ);
669445aa1aSAl Viro EXPORT_SYMBOL(DMA_MODE_WRITE);
679445aa1aSAl Viro 
6870d64ceaSPaul Mackerras /*
69bd7c93ccSBenjamin Herrenschmidt  * This is run before start_kernel(), the kernel has been relocated
70bd7c93ccSBenjamin Herrenschmidt  * and we are running with enough of the MMU enabled to have our
71bd7c93ccSBenjamin Herrenschmidt  * proper kernel virtual addresses
72bd7c93ccSBenjamin Herrenschmidt  *
73f9cc1d1fSBenjamin Herrenschmidt  * We do the initial parsing of the flat device-tree and prepares
74f9cc1d1fSBenjamin Herrenschmidt  * for the MMU to be fully initialized.
7570d64ceaSPaul Mackerras  */
766dece0ebSScott Wood notrace void __init machine_init(u64 dt_ptr)
7770d64ceaSPaul Mackerras {
7804b0a72fSChristophe Leroy 	unsigned int *addr = (unsigned int *)patch_site_addr(&patch__memset_nocache);
79ad1b0122SChristophe Leroy 	unsigned long insn;
80ad1b0122SChristophe Leroy 
8197f6e0ccSBenjamin Herrenschmidt 	/* Configure static keys first, now that we're relocated. */
8297f6e0ccSBenjamin Herrenschmidt 	setup_feature_keys();
8397f6e0ccSBenjamin Herrenschmidt 
84265c3491SChristophe Leroy 	early_ioremap_setup();
85265c3491SChristophe Leroy 
86719c91ccSDavid Gibson 	/* Enable early debugging if any specified (see udbg.h) */
87719c91ccSDavid Gibson 	udbg_early_init();
8851d3082fSBenjamin Herrenschmidt 
89fa54a981SChristophe Leroy 	patch_instruction_site(&patch__memcpy_nocache, PPC_INST_NOP);
90ad1b0122SChristophe Leroy 
91ad1b0122SChristophe Leroy 	insn = create_cond_branch(addr, branch_target(addr), 0x820000);
92ad1b0122SChristophe Leroy 	patch_instruction(addr, insn);	/* replace b by bne cr0 */
931cd03890SLEROY Christophe 
9451d3082fSBenjamin Herrenschmidt 	/* Do some early initialization based on the flat device tree */
9570d64ceaSPaul Mackerras 	early_init_devtree(__va(dt_ptr));
9670d64ceaSPaul Mackerras 
9791b191c7SDave Kleikamp 	early_init_mmu();
9891b191c7SDave Kleikamp 
99f8f50b1bSDale Farnsworth 	setup_kdump_trampoline();
10070d64ceaSPaul Mackerras }
10170d64ceaSPaul Mackerras 
10270d64ceaSPaul Mackerras /* Checks "l2cr=xxxx" command-line option */
103d15a261dSMathieu Malaterre static int __init ppc_setup_l2cr(char *str)
10470d64ceaSPaul Mackerras {
10570d64ceaSPaul Mackerras 	if (cpu_has_feature(CPU_FTR_L2CR)) {
10670d64ceaSPaul Mackerras 		unsigned long val = simple_strtoul(str, NULL, 0);
10770d64ceaSPaul Mackerras 		printk(KERN_INFO "l2cr set to %lx\n", val);
10870d64ceaSPaul Mackerras 		_set_L2CR(0);		/* force invalidate by disable cache */
10970d64ceaSPaul Mackerras 		_set_L2CR(val);		/* and enable it */
11070d64ceaSPaul Mackerras 	}
11170d64ceaSPaul Mackerras 	return 1;
11270d64ceaSPaul Mackerras }
11370d64ceaSPaul Mackerras __setup("l2cr=", ppc_setup_l2cr);
11470d64ceaSPaul Mackerras 
115a78bfbfcSRobert Brose /* Checks "l3cr=xxxx" command-line option */
116d15a261dSMathieu Malaterre static int __init ppc_setup_l3cr(char *str)
117a78bfbfcSRobert Brose {
118a78bfbfcSRobert Brose 	if (cpu_has_feature(CPU_FTR_L3CR)) {
119a78bfbfcSRobert Brose 		unsigned long val = simple_strtoul(str, NULL, 0);
120a78bfbfcSRobert Brose 		printk(KERN_INFO "l3cr set to %lx\n", val);
121a78bfbfcSRobert Brose 		_set_L3CR(val);		/* and enable it */
122a78bfbfcSRobert Brose 	}
123a78bfbfcSRobert Brose 	return 1;
124a78bfbfcSRobert Brose }
125a78bfbfcSRobert Brose __setup("l3cr=", ppc_setup_l3cr);
126a78bfbfcSRobert Brose 
127d15a261dSMathieu Malaterre static int __init ppc_init(void)
12870d64ceaSPaul Mackerras {
12970d64ceaSPaul Mackerras 	/* clear the progress line */
1305e41763aSGiuliano Pochini 	if (ppc_md.progress)
1315e41763aSGiuliano Pochini 		ppc_md.progress("             ", 0xffff);
13270d64ceaSPaul Mackerras 
13370d64ceaSPaul Mackerras 	/* call platform init */
13470d64ceaSPaul Mackerras 	if (ppc_md.init != NULL) {
13570d64ceaSPaul Mackerras 		ppc_md.init();
13670d64ceaSPaul Mackerras 	}
13770d64ceaSPaul Mackerras 	return 0;
13870d64ceaSPaul Mackerras }
13970d64ceaSPaul Mackerras arch_initcall(ppc_init);
14070d64ceaSPaul Mackerras 
141c8e409a3SChristophe Leroy static void *__init alloc_stack(void)
142c8e409a3SChristophe Leroy {
14363289e7dSChristophe Leroy 	void *ptr = memblock_alloc(THREAD_SIZE, THREAD_ALIGN);
144c8e409a3SChristophe Leroy 
145c8e409a3SChristophe Leroy 	if (!ptr)
146c8e409a3SChristophe Leroy 		panic("cannot allocate %d bytes for stack at %pS\n",
147c8e409a3SChristophe Leroy 		      THREAD_SIZE, (void *)_RET_IP_);
148c8e409a3SChristophe Leroy 
149c8e409a3SChristophe Leroy 	return ptr;
150c8e409a3SChristophe Leroy }
151c8e409a3SChristophe Leroy 
152b1923caaSBenjamin Herrenschmidt void __init irqstack_early_init(void)
15385218827SKumar Gala {
15485218827SKumar Gala 	unsigned int i;
15585218827SKumar Gala 
156*547db12fSChristophe Leroy 	if (IS_ENABLED(CONFIG_VMAP_STACK))
157*547db12fSChristophe Leroy 		return;
158*547db12fSChristophe Leroy 
15985218827SKumar Gala 	/* interrupt stacks must be in lowmem, we get that for free on ppc32
160e63075a3SBenjamin Herrenschmidt 	 * as the memblock is limited to lowmem by default */
16185218827SKumar Gala 	for_each_possible_cpu(i) {
162c8e409a3SChristophe Leroy 		softirq_ctx[i] = alloc_stack();
163c8e409a3SChristophe Leroy 		hardirq_ctx[i] = alloc_stack();
16485218827SKumar Gala 	}
16585218827SKumar Gala }
16685218827SKumar Gala 
1673978eb78SChristophe Leroy #ifdef CONFIG_VMAP_STACK
1683978eb78SChristophe Leroy void *emergency_ctx[NR_CPUS] __ro_after_init;
1693978eb78SChristophe Leroy 
1703978eb78SChristophe Leroy void __init emergency_stack_init(void)
1713978eb78SChristophe Leroy {
1723978eb78SChristophe Leroy 	unsigned int i;
1733978eb78SChristophe Leroy 
1743978eb78SChristophe Leroy 	for_each_possible_cpu(i)
1753978eb78SChristophe Leroy 		emergency_ctx[i] = alloc_stack();
1763978eb78SChristophe Leroy }
1773978eb78SChristophe Leroy #endif
1783978eb78SChristophe Leroy 
179bcf0b088SKumar Gala #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
180b1923caaSBenjamin Herrenschmidt void __init exc_lvl_early_init(void)
181bcf0b088SKumar Gala {
1823e7f45adSDave Kleikamp 	unsigned int i, hw_cpu;
183bcf0b088SKumar Gala 
184bcf0b088SKumar Gala 	/* interrupt stacks must be in lowmem, we get that for free on ppc32
18595f72d1eSYinghai Lu 	 * as the memblock is limited to lowmem by MEMBLOCK_REAL_LIMIT */
186bcf0b088SKumar Gala 	for_each_possible_cpu(i) {
18704a34113SKevin Hao #ifdef CONFIG_SMP
1883e7f45adSDave Kleikamp 		hw_cpu = get_hard_smp_processor_id(i);
18904a34113SKevin Hao #else
19004a34113SKevin Hao 		hw_cpu = 0;
19104a34113SKevin Hao #endif
19204a34113SKevin Hao 
193c8e409a3SChristophe Leroy 		critirq_ctx[hw_cpu] = alloc_stack();
194bcf0b088SKumar Gala #ifdef CONFIG_BOOKE
195c8e409a3SChristophe Leroy 		dbgirq_ctx[hw_cpu] = alloc_stack();
196c8e409a3SChristophe Leroy 		mcheckirq_ctx[hw_cpu] = alloc_stack();
197bcf0b088SKumar Gala #endif
198bcf0b088SKumar Gala 	}
199bcf0b088SKumar Gala }
200bcf0b088SKumar Gala #endif
201bcf0b088SKumar Gala 
202b1923caaSBenjamin Herrenschmidt void __init setup_power_save(void)
20356571384SBenjamin Herrenschmidt {
204d7cceda9SChristophe Leroy #ifdef CONFIG_PPC_BOOK3S_32
20556571384SBenjamin Herrenschmidt 	if (cpu_has_feature(CPU_FTR_CAN_DOZE) ||
20656571384SBenjamin Herrenschmidt 	    cpu_has_feature(CPU_FTR_CAN_NAP))
20756571384SBenjamin Herrenschmidt 		ppc_md.power_save = ppc6xx_idle;
20856571384SBenjamin Herrenschmidt #endif
20956571384SBenjamin Herrenschmidt 
21056571384SBenjamin Herrenschmidt #ifdef CONFIG_E500
21156571384SBenjamin Herrenschmidt 	if (cpu_has_feature(CPU_FTR_CAN_DOZE) ||
21256571384SBenjamin Herrenschmidt 	    cpu_has_feature(CPU_FTR_CAN_NAP))
21356571384SBenjamin Herrenschmidt 		ppc_md.power_save = e500_idle;
21456571384SBenjamin Herrenschmidt #endif
21556571384SBenjamin Herrenschmidt }
21656571384SBenjamin Herrenschmidt 
217b1923caaSBenjamin Herrenschmidt __init void initialize_cache_info(void)
2188f212cb2SBenjamin Herrenschmidt {
2198f212cb2SBenjamin Herrenschmidt 	/*
2208f212cb2SBenjamin Herrenschmidt 	 * Set cache line size based on type of cpu as a default.
2218f212cb2SBenjamin Herrenschmidt 	 * Systems with OF can look in the properties on the cpu node(s)
2228f212cb2SBenjamin Herrenschmidt 	 * for a possibly more accurate value.
2238f212cb2SBenjamin Herrenschmidt 	 */
2248f212cb2SBenjamin Herrenschmidt 	dcache_bsize = cur_cpu_spec->dcache_bsize;
2258f212cb2SBenjamin Herrenschmidt 	icache_bsize = cur_cpu_spec->icache_bsize;
2268f212cb2SBenjamin Herrenschmidt 	ucache_bsize = 0;
227e0291f1dSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_601) || IS_ENABLED(CONFIG_E200))
2288f212cb2SBenjamin Herrenschmidt 		ucache_bsize = icache_bsize = dcache_bsize;
2298f212cb2SBenjamin Herrenschmidt }
230