blob: 1823706ae0768bf8b060de6a3ddd2bee6d075a53 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002/*
3 * Common prep/pmac/chrp boot and setup code.
4 */
5
Paul Mackerras9b6b5632005-10-06 12:06:20 +10006#include <linux/module.h>
7#include <linux/string.h>
8#include <linux/sched.h>
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/reboot.h>
12#include <linux/delay.h>
13#include <linux/initrd.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100014#include <linux/tty.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100015#include <linux/seq_file.h>
16#include <linux/root_dev.h>
17#include <linux/cpu.h>
18#include <linux/console.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100019#include <linux/memblock.h>
Al Viro9445aa12016-01-13 23:33:46 -050020#include <linux/export.h>
Finn Thaina156c7b2019-01-15 15:18:56 +110021#include <linux/nvram.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070022#include <linux/pgtable.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100023
Paul Mackerras9b6b5632005-10-06 12:06:20 +100024#include <asm/io.h>
25#include <asm/prom.h>
26#include <asm/processor.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100027#include <asm/setup.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100028#include <asm/smp.h>
29#include <asm/elf.h>
30#include <asm/cputable.h>
31#include <asm/bootx.h>
32#include <asm/btext.h>
33#include <asm/machdep.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080034#include <linux/uaccess.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100035#include <asm/pmac_feature.h>
36#include <asm/sections.h>
37#include <asm/nvram.h>
38#include <asm/xmon.h>
Kumar Gala6d7f58b2005-10-25 23:57:33 -050039#include <asm/time.h>
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110040#include <asm/serial.h>
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +110041#include <asm/udbg.h>
LEROY Christophe1cd03892015-09-16 12:04:51 +020042#include <asm/code-patching.h>
Kevin Haob92a2262016-07-23 14:42:40 +053043#include <asm/cpu_has_feature.h>
Mathieu Malaterree82d70cf92018-03-08 22:31:59 +110044#include <asm/asm-prototypes.h>
Christophe Leroydb0a2b62018-07-05 16:24:51 +000045#include <asm/kdump.h>
Christophe Leroy2c86cd12018-07-05 16:25:01 +000046#include <asm/feature-fixups.h>
Christophe Leroy265c3492019-09-12 13:49:43 +000047#include <asm/early_ioremap.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100048
Mathieu Malaterref2c6d0d2018-06-22 21:26:53 +020049#include "setup.h"
50
Paul Mackerras03501da2005-10-26 17:11:18 +100051#define DBG(fmt...)
52
Paul Mackerras9b6b5632005-10-06 12:06:20 +100053extern void bootx_init(unsigned long r4, unsigned long phys);
54
Paul Mackerras80579e12005-10-27 22:42:04 +100055int boot_cpuid_phys;
Andrew Gabbasov9974eec2011-07-16 03:22:13 +000056EXPORT_SYMBOL_GPL(boot_cpuid_phys);
Paul Mackerras80579e12005-10-27 22:42:04 +100057
Nathan Lynch13a98012008-12-10 14:28:41 +000058int smp_hw_index[NR_CPUS];
Al Viro9445aa12016-01-13 23:33:46 -050059EXPORT_SYMBOL(smp_hw_index);
Nathan Lynch13a98012008-12-10 14:28:41 +000060
Paul Mackerras9b6b5632005-10-06 12:06:20 +100061unsigned int DMA_MODE_READ;
62unsigned int DMA_MODE_WRITE;
63
Al Viro9445aa12016-01-13 23:33:46 -050064EXPORT_SYMBOL(DMA_MODE_READ);
65EXPORT_SYMBOL(DMA_MODE_WRITE);
66
Paul Mackerras9b6b5632005-10-06 12:06:20 +100067/*
Benjamin Herrenschmidtbd7c93c2016-07-05 15:03:45 +100068 * This is run before start_kernel(), the kernel has been relocated
69 * and we are running with enough of the MMU enabled to have our
70 * proper kernel virtual addresses
71 *
Benjamin Herrenschmidtf9cc1d12016-08-10 17:32:38 +100072 * We do the initial parsing of the flat device-tree and prepares
73 * for the MMU to be fully initialized.
Paul Mackerras9b6b5632005-10-06 12:06:20 +100074 */
Scott Wood6dece0eb2011-07-25 11:29:33 +000075notrace void __init machine_init(u64 dt_ptr)
Paul Mackerras9b6b5632005-10-06 12:06:20 +100076{
Jordan Niethe94afd062020-05-06 13:40:31 +100077 struct ppc_inst *addr = (struct ppc_inst *)patch_site_addr(&patch__memset_nocache);
78 struct ppc_inst insn;
Christophe Leroyad1b0122017-08-23 16:54:38 +020079
Benjamin Herrenschmidt97f6e0c2016-08-10 17:27:34 +100080 /* Configure static keys first, now that we're relocated. */
81 setup_feature_keys();
82
Christophe Leroy925ac142020-05-19 05:48:58 +000083 early_ioremap_init();
Christophe Leroy265c3492019-09-12 13:49:43 +000084
David Gibson719c91c2007-02-13 15:54:22 +110085 /* Enable early debugging if any specified (see udbg.h) */
86 udbg_early_init();
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +110087
Jordan Niethe75346252020-05-06 13:40:26 +100088 patch_instruction_site(&patch__memcpy_nocache, ppc_inst(PPC_INST_NOP));
Christophe Leroyad1b0122017-08-23 16:54:38 +020089
Jordan Niethe7c95d882020-05-06 13:40:25 +100090 create_cond_branch(&insn, addr, branch_target(addr), 0x820000);
Christophe Leroyad1b0122017-08-23 16:54:38 +020091 patch_instruction(addr, insn); /* replace b by bne cr0 */
LEROY Christophe1cd03892015-09-16 12:04:51 +020092
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +110093 /* Do some early initialization based on the flat device tree */
Paul Mackerras9b6b5632005-10-06 12:06:20 +100094 early_init_devtree(__va(dt_ptr));
95
Dave Kleikamp91b191c2011-07-04 18:38:03 +000096 early_init_mmu();
97
Dale Farnsworthf8f50b12008-12-17 10:09:26 +000098 setup_kdump_trampoline();
Paul Mackerras9b6b5632005-10-06 12:06:20 +100099}
100
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000101/* Checks "l2cr=xxxx" command-line option */
Mathieu Malaterred15a2612018-03-07 21:32:55 +0100102static int __init ppc_setup_l2cr(char *str)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000103{
104 if (cpu_has_feature(CPU_FTR_L2CR)) {
105 unsigned long val = simple_strtoul(str, NULL, 0);
106 printk(KERN_INFO "l2cr set to %lx\n", val);
107 _set_L2CR(0); /* force invalidate by disable cache */
108 _set_L2CR(val); /* and enable it */
109 }
110 return 1;
111}
112__setup("l2cr=", ppc_setup_l2cr);
113
Robert Brosea78bfbf2008-03-29 07:20:23 +1100114/* Checks "l3cr=xxxx" command-line option */
Mathieu Malaterred15a2612018-03-07 21:32:55 +0100115static int __init ppc_setup_l3cr(char *str)
Robert Brosea78bfbf2008-03-29 07:20:23 +1100116{
117 if (cpu_has_feature(CPU_FTR_L3CR)) {
118 unsigned long val = simple_strtoul(str, NULL, 0);
119 printk(KERN_INFO "l3cr set to %lx\n", val);
120 _set_L3CR(val); /* and enable it */
121 }
122 return 1;
123}
124__setup("l3cr=", ppc_setup_l3cr);
125
Mathieu Malaterred15a2612018-03-07 21:32:55 +0100126static int __init ppc_init(void)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000127{
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000128 /* clear the progress line */
Giuliano Pochini5e417632007-03-26 21:40:28 -0800129 if (ppc_md.progress)
130 ppc_md.progress(" ", 0xffff);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000131
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000132 /* call platform init */
133 if (ppc_md.init != NULL) {
134 ppc_md.init();
135 }
136 return 0;
137}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000138arch_initcall(ppc_init);
139
Christophe Leroyc8e409a2019-01-31 10:08:44 +0000140static void *__init alloc_stack(void)
141{
Christophe Leroy63289e72019-12-21 08:32:28 +0000142 void *ptr = memblock_alloc(THREAD_SIZE, THREAD_ALIGN);
Christophe Leroyc8e409a2019-01-31 10:08:44 +0000143
144 if (!ptr)
145 panic("cannot allocate %d bytes for stack at %pS\n",
146 THREAD_SIZE, (void *)_RET_IP_);
147
148 return ptr;
149}
150
Benjamin Herrenschmidtb1923ca2016-07-05 15:07:51 +1000151void __init irqstack_early_init(void)
Kumar Gala85218822008-04-28 16:21:22 +1000152{
153 unsigned int i;
154
Christophe Leroy547db122019-12-21 08:32:30 +0000155 if (IS_ENABLED(CONFIG_VMAP_STACK))
156 return;
157
Kumar Gala85218822008-04-28 16:21:22 +1000158 /* interrupt stacks must be in lowmem, we get that for free on ppc32
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700159 * as the memblock is limited to lowmem by default */
Kumar Gala85218822008-04-28 16:21:22 +1000160 for_each_possible_cpu(i) {
Christophe Leroyc8e409a2019-01-31 10:08:44 +0000161 softirq_ctx[i] = alloc_stack();
162 hardirq_ctx[i] = alloc_stack();
Kumar Gala85218822008-04-28 16:21:22 +1000163 }
164}
Kumar Gala85218822008-04-28 16:21:22 +1000165
Christophe Leroy3978eb72019-12-21 08:32:29 +0000166#ifdef CONFIG_VMAP_STACK
167void *emergency_ctx[NR_CPUS] __ro_after_init;
168
169void __init emergency_stack_init(void)
170{
171 unsigned int i;
172
173 for_each_possible_cpu(i)
174 emergency_ctx[i] = alloc_stack();
175}
176#endif
177
Kumar Galabcf0b082008-04-30 03:49:55 -0500178#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
Benjamin Herrenschmidtb1923ca2016-07-05 15:07:51 +1000179void __init exc_lvl_early_init(void)
Kumar Galabcf0b082008-04-30 03:49:55 -0500180{
Dave Kleikamp3e7f45a2010-08-18 06:44:25 +0000181 unsigned int i, hw_cpu;
Kumar Galabcf0b082008-04-30 03:49:55 -0500182
183 /* interrupt stacks must be in lowmem, we get that for free on ppc32
Yinghai Lu95f72d12010-07-12 14:36:09 +1000184 * as the memblock is limited to lowmem by MEMBLOCK_REAL_LIMIT */
Kumar Galabcf0b082008-04-30 03:49:55 -0500185 for_each_possible_cpu(i) {
Kevin Hao04a34112014-01-29 18:24:54 +0800186#ifdef CONFIG_SMP
Dave Kleikamp3e7f45a2010-08-18 06:44:25 +0000187 hw_cpu = get_hard_smp_processor_id(i);
Kevin Hao04a34112014-01-29 18:24:54 +0800188#else
189 hw_cpu = 0;
190#endif
191
Christophe Leroyc8e409a2019-01-31 10:08:44 +0000192 critirq_ctx[hw_cpu] = alloc_stack();
Kumar Galabcf0b082008-04-30 03:49:55 -0500193#ifdef CONFIG_BOOKE
Christophe Leroyc8e409a2019-01-31 10:08:44 +0000194 dbgirq_ctx[hw_cpu] = alloc_stack();
195 mcheckirq_ctx[hw_cpu] = alloc_stack();
Kumar Galabcf0b082008-04-30 03:49:55 -0500196#endif
197 }
198}
Kumar Galabcf0b082008-04-30 03:49:55 -0500199#endif
200
Benjamin Herrenschmidtb1923ca2016-07-05 15:07:51 +1000201void __init setup_power_save(void)
Benjamin Herrenschmidt56571382016-07-05 15:04:05 +1000202{
Christophe Leroyd7cceda2018-11-17 10:24:56 +0000203#ifdef CONFIG_PPC_BOOK3S_32
Benjamin Herrenschmidt56571382016-07-05 15:04:05 +1000204 if (cpu_has_feature(CPU_FTR_CAN_DOZE) ||
205 cpu_has_feature(CPU_FTR_CAN_NAP))
206 ppc_md.power_save = ppc6xx_idle;
207#endif
208
209#ifdef CONFIG_E500
210 if (cpu_has_feature(CPU_FTR_CAN_DOZE) ||
211 cpu_has_feature(CPU_FTR_CAN_NAP))
212 ppc_md.power_save = e500_idle;
213#endif
214}
215
Benjamin Herrenschmidtb1923ca2016-07-05 15:07:51 +1000216__init void initialize_cache_info(void)
Benjamin Herrenschmidt8f212cb2016-07-05 15:04:10 +1000217{
218 /*
219 * Set cache line size based on type of cpu as a default.
220 * Systems with OF can look in the properties on the cpu node(s)
221 * for a possibly more accurate value.
222 */
223 dcache_bsize = cur_cpu_spec->dcache_bsize;
224 icache_bsize = cur_cpu_spec->icache_bsize;
225 ucache_bsize = 0;
Christophe Leroye0291f12019-08-26 15:52:18 +0000226 if (IS_ENABLED(CONFIG_PPC_BOOK3S_601) || IS_ENABLED(CONFIG_E200))
Benjamin Herrenschmidt8f212cb2016-07-05 15:04:10 +1000227 ucache_bsize = icache_bsize = dcache_bsize;
228}