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>
2265fddcfcSMike Rapoport #include <linux/pgtable.h>
23e6f6390aSChristophe Leroy #include <linux/of_fdt.h>
24e6f6390aSChristophe Leroy #include <linux/irq.h>
2570d64ceaSPaul Mackerras
2670d64ceaSPaul Mackerras #include <asm/io.h>
2770d64ceaSPaul Mackerras #include <asm/processor.h>
2870d64ceaSPaul Mackerras #include <asm/setup.h>
2970d64ceaSPaul Mackerras #include <asm/smp.h>
3070d64ceaSPaul Mackerras #include <asm/elf.h>
3170d64ceaSPaul Mackerras #include <asm/cputable.h>
3270d64ceaSPaul Mackerras #include <asm/bootx.h>
3370d64ceaSPaul Mackerras #include <asm/btext.h>
3470d64ceaSPaul Mackerras #include <asm/machdep.h>
357c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3670d64ceaSPaul Mackerras #include <asm/pmac_feature.h>
3770d64ceaSPaul Mackerras #include <asm/sections.h>
3870d64ceaSPaul Mackerras #include <asm/nvram.h>
3970d64ceaSPaul Mackerras #include <asm/xmon.h>
406d7f58b0SKumar Gala #include <asm/time.h>
41463ce0e1SBenjamin Herrenschmidt #include <asm/serial.h>
4251d3082fSBenjamin Herrenschmidt #include <asm/udbg.h>
431cd03890SLEROY Christophe #include <asm/code-patching.h>
44b92a226eSKevin Hao #include <asm/cpu_has_feature.h>
45e82d70cfSMathieu Malaterre #include <asm/asm-prototypes.h>
46db0a2b63SChristophe Leroy #include <asm/kdump.h>
472c86cd18SChristophe Leroy #include <asm/feature-fixups.h>
48265c3491SChristophe Leroy #include <asm/early_ioremap.h>
4970d64ceaSPaul Mackerras
50f2c6d0d1SMathieu Malaterre #include "setup.h"
51f2c6d0d1SMathieu Malaterre
5203501dabSPaul Mackerras #define DBG(fmt...)
5303501dabSPaul Mackerras
5470d64ceaSPaul Mackerras extern void bootx_init(unsigned long r4, unsigned long phys);
5570d64ceaSPaul Mackerras
5680579e1fSPaul Mackerras int boot_cpuid_phys;
579974eec2SAndrew Gabbasov EXPORT_SYMBOL_GPL(boot_cpuid_phys);
5880579e1fSPaul Mackerras
5913a9801eSNathan Lynch int smp_hw_index[NR_CPUS];
609445aa1aSAl Viro EXPORT_SYMBOL(smp_hw_index);
6113a9801eSNathan Lynch
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 */
machine_init(u64 dt_ptr)766dece0ebSScott Wood notrace void __init machine_init(u64 dt_ptr)
7770d64ceaSPaul Mackerras {
7869d4d6e5SChristophe Leroy u32 *addr = (u32 *)patch_site_addr(&patch__memset_nocache);
79c545b9f0SChristophe Leroy ppc_inst_t insn;
80ad1b0122SChristophe Leroy
8197f6e0ccSBenjamin Herrenschmidt /* Configure static keys first, now that we're relocated. */
8297f6e0ccSBenjamin Herrenschmidt setup_feature_keys();
8397f6e0ccSBenjamin Herrenschmidt
84925ac141SChristophe Leroy early_ioremap_init();
85265c3491SChristophe Leroy
86719c91ccSDavid Gibson /* Enable early debugging if any specified (see udbg.h) */
87719c91ccSDavid Gibson udbg_early_init();
8851d3082fSBenjamin Herrenschmidt
89f30becb5SChristophe Leroy patch_instruction_site(&patch__memcpy_nocache, ppc_inst(PPC_RAW_NOP()));
90ad1b0122SChristophe Leroy
917c95d889SJordan Niethe create_cond_branch(&insn, 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 */
ppc_setup_l2cr(char * str)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 */
ppc_setup_l3cr(char * str)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
ppc_init(void)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
alloc_stack(void)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
irqstack_early_init(void)152b1923caaSBenjamin Herrenschmidt void __init irqstack_early_init(void)
15385218827SKumar Gala {
15485218827SKumar Gala unsigned int i;
15585218827SKumar Gala
156547db12fSChristophe Leroy if (IS_ENABLED(CONFIG_VMAP_STACK))
157547db12fSChristophe Leroy return;
158547db12fSChristophe 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
168a4719f5bSChristophe Leroy void *emergency_ctx[NR_CPUS] __ro_after_init = {[0] = &init_stack};
1693978eb78SChristophe Leroy
emergency_stack_init(void)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
179047a6fd4SChristophe Leroy #ifdef CONFIG_BOOKE_OR_40x
exc_lvl_early_init(void)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
setup_power_save(void)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
210*688de017SChristophe Leroy #ifdef CONFIG_PPC_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
initialize_cache_info(void)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 }
227