xref: /openbmc/linux/arch/powerpc/kexec/core_64.c (revision d10e3c39)
1793b08e2SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-only
2793b08e2SChristophe Leroy /*
3793b08e2SChristophe Leroy  * PPC64 code to handle Linux booting another kernel.
4793b08e2SChristophe Leroy  *
5793b08e2SChristophe Leroy  * Copyright (C) 2004-2005, IBM Corp.
6793b08e2SChristophe Leroy  *
7793b08e2SChristophe Leroy  * Created by: Milton D Miller II
8793b08e2SChristophe Leroy  */
9793b08e2SChristophe Leroy 
10793b08e2SChristophe Leroy 
11793b08e2SChristophe Leroy #include <linux/kexec.h>
12793b08e2SChristophe Leroy #include <linux/smp.h>
13793b08e2SChristophe Leroy #include <linux/thread_info.h>
14793b08e2SChristophe Leroy #include <linux/init_task.h>
15793b08e2SChristophe Leroy #include <linux/errno.h>
16793b08e2SChristophe Leroy #include <linux/kernel.h>
17793b08e2SChristophe Leroy #include <linux/cpu.h>
18793b08e2SChristophe Leroy #include <linux/hardirq.h>
19e6f6390aSChristophe Leroy #include <linux/of.h>
20793b08e2SChristophe Leroy 
21793b08e2SChristophe Leroy #include <asm/page.h>
22793b08e2SChristophe Leroy #include <asm/current.h>
23793b08e2SChristophe Leroy #include <asm/machdep.h>
24793b08e2SChristophe Leroy #include <asm/cacheflush.h>
25793b08e2SChristophe Leroy #include <asm/firmware.h>
26793b08e2SChristophe Leroy #include <asm/paca.h>
27793b08e2SChristophe Leroy #include <asm/mmu.h>
28793b08e2SChristophe Leroy #include <asm/sections.h>	/* _end */
29*d10e3c39SNicholas Piggin #include <asm/setup.h>
30793b08e2SChristophe Leroy #include <asm/smp.h>
31793b08e2SChristophe Leroy #include <asm/hw_breakpoint.h>
32793b08e2SChristophe Leroy #include <asm/svm.h>
33793b08e2SChristophe Leroy #include <asm/ultravisor.h>
34793b08e2SChristophe Leroy 
machine_kexec_prepare(struct kimage * image)358f7fadb4SChristophe Leroy int machine_kexec_prepare(struct kimage *image)
36793b08e2SChristophe Leroy {
37793b08e2SChristophe Leroy 	int i;
38793b08e2SChristophe Leroy 	unsigned long begin, end;	/* limits of segment */
39793b08e2SChristophe Leroy 	unsigned long low, high;	/* limits of blocked memory range */
40793b08e2SChristophe Leroy 	struct device_node *node;
41793b08e2SChristophe Leroy 	const unsigned long *basep;
42793b08e2SChristophe Leroy 	const unsigned int *sizep;
43793b08e2SChristophe Leroy 
44793b08e2SChristophe Leroy 	/*
45793b08e2SChristophe Leroy 	 * Since we use the kernel fault handlers and paging code to
46793b08e2SChristophe Leroy 	 * handle the virtual mode, we must make sure no destination
47793b08e2SChristophe Leroy 	 * overlaps kernel static data or bss.
48793b08e2SChristophe Leroy 	 */
49793b08e2SChristophe Leroy 	for (i = 0; i < image->nr_segments; i++)
50793b08e2SChristophe Leroy 		if (image->segment[i].mem < __pa(_end))
51793b08e2SChristophe Leroy 			return -ETXTBSY;
52793b08e2SChristophe Leroy 
53793b08e2SChristophe Leroy 	/* We also should not overwrite the tce tables */
54793b08e2SChristophe Leroy 	for_each_node_by_type(node, "pci") {
55793b08e2SChristophe Leroy 		basep = of_get_property(node, "linux,tce-base", NULL);
56793b08e2SChristophe Leroy 		sizep = of_get_property(node, "linux,tce-size", NULL);
57793b08e2SChristophe Leroy 		if (basep == NULL || sizep == NULL)
58793b08e2SChristophe Leroy 			continue;
59793b08e2SChristophe Leroy 
60793b08e2SChristophe Leroy 		low = *basep;
61793b08e2SChristophe Leroy 		high = low + (*sizep);
62793b08e2SChristophe Leroy 
63793b08e2SChristophe Leroy 		for (i = 0; i < image->nr_segments; i++) {
64793b08e2SChristophe Leroy 			begin = image->segment[i].mem;
65793b08e2SChristophe Leroy 			end = begin + image->segment[i].memsz;
66793b08e2SChristophe Leroy 
67c00103abSkernel test robot 			if ((begin < high) && (end > low)) {
68c00103abSkernel test robot 				of_node_put(node);
69793b08e2SChristophe Leroy 				return -ETXTBSY;
70793b08e2SChristophe Leroy 			}
71793b08e2SChristophe Leroy 		}
72c00103abSkernel test robot 	}
73793b08e2SChristophe Leroy 
74793b08e2SChristophe Leroy 	return 0;
75793b08e2SChristophe Leroy }
76793b08e2SChristophe Leroy 
778119cefdSHari Bathini /* Called during kexec sequence with MMU off */
copy_segments(unsigned long ind)788119cefdSHari Bathini static notrace void copy_segments(unsigned long ind)
79793b08e2SChristophe Leroy {
80793b08e2SChristophe Leroy 	unsigned long entry;
81793b08e2SChristophe Leroy 	unsigned long *ptr;
82793b08e2SChristophe Leroy 	void *dest;
83793b08e2SChristophe Leroy 	void *addr;
84793b08e2SChristophe Leroy 
85793b08e2SChristophe Leroy 	/*
86793b08e2SChristophe Leroy 	 * We rely on kexec_load to create a lists that properly
87793b08e2SChristophe Leroy 	 * initializes these pointers before they are used.
88793b08e2SChristophe Leroy 	 * We will still crash if the list is wrong, but at least
89793b08e2SChristophe Leroy 	 * the compiler will be quiet.
90793b08e2SChristophe Leroy 	 */
91793b08e2SChristophe Leroy 	ptr = NULL;
92793b08e2SChristophe Leroy 	dest = NULL;
93793b08e2SChristophe Leroy 
94793b08e2SChristophe Leroy 	for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {
95793b08e2SChristophe Leroy 		addr = __va(entry & PAGE_MASK);
96793b08e2SChristophe Leroy 
97793b08e2SChristophe Leroy 		switch (entry & IND_FLAGS) {
98793b08e2SChristophe Leroy 		case IND_DESTINATION:
99793b08e2SChristophe Leroy 			dest = addr;
100793b08e2SChristophe Leroy 			break;
101793b08e2SChristophe Leroy 		case IND_INDIRECTION:
102793b08e2SChristophe Leroy 			ptr = addr;
103793b08e2SChristophe Leroy 			break;
104793b08e2SChristophe Leroy 		case IND_SOURCE:
105793b08e2SChristophe Leroy 			copy_page(dest, addr);
106793b08e2SChristophe Leroy 			dest += PAGE_SIZE;
107793b08e2SChristophe Leroy 		}
108793b08e2SChristophe Leroy 	}
109793b08e2SChristophe Leroy }
110793b08e2SChristophe Leroy 
1118119cefdSHari Bathini /* Called during kexec sequence with MMU off */
kexec_copy_flush(struct kimage * image)1128119cefdSHari Bathini notrace void kexec_copy_flush(struct kimage *image)
113793b08e2SChristophe Leroy {
114793b08e2SChristophe Leroy 	long i, nr_segments = image->nr_segments;
115793b08e2SChristophe Leroy 	struct  kexec_segment ranges[KEXEC_SEGMENT_MAX];
116793b08e2SChristophe Leroy 
117793b08e2SChristophe Leroy 	/* save the ranges on the stack to efficiently flush the icache */
118793b08e2SChristophe Leroy 	memcpy(ranges, image->segment, sizeof(ranges));
119793b08e2SChristophe Leroy 
120793b08e2SChristophe Leroy 	/*
121793b08e2SChristophe Leroy 	 * After this call we may not use anything allocated in dynamic
122793b08e2SChristophe Leroy 	 * memory, including *image.
123793b08e2SChristophe Leroy 	 *
124793b08e2SChristophe Leroy 	 * Only globals and the stack are allowed.
125793b08e2SChristophe Leroy 	 */
126793b08e2SChristophe Leroy 	copy_segments(image->head);
127793b08e2SChristophe Leroy 
128793b08e2SChristophe Leroy 	/*
129793b08e2SChristophe Leroy 	 * we need to clear the icache for all dest pages sometime,
130793b08e2SChristophe Leroy 	 * including ones that were in place on the original copy
131793b08e2SChristophe Leroy 	 */
132793b08e2SChristophe Leroy 	for (i = 0; i < nr_segments; i++)
133793b08e2SChristophe Leroy 		flush_icache_range((unsigned long)__va(ranges[i].mem),
134793b08e2SChristophe Leroy 			(unsigned long)__va(ranges[i].mem + ranges[i].memsz));
135793b08e2SChristophe Leroy }
136793b08e2SChristophe Leroy 
137793b08e2SChristophe Leroy #ifdef CONFIG_SMP
138793b08e2SChristophe Leroy 
139793b08e2SChristophe Leroy static int kexec_all_irq_disabled = 0;
140793b08e2SChristophe Leroy 
kexec_smp_down(void * arg)141793b08e2SChristophe Leroy static void kexec_smp_down(void *arg)
142793b08e2SChristophe Leroy {
143793b08e2SChristophe Leroy 	local_irq_disable();
144793b08e2SChristophe Leroy 	hard_irq_disable();
145793b08e2SChristophe Leroy 
146793b08e2SChristophe Leroy 	mb(); /* make sure our irqs are disabled before we say they are */
147793b08e2SChristophe Leroy 	get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
148793b08e2SChristophe Leroy 	while(kexec_all_irq_disabled == 0)
149793b08e2SChristophe Leroy 		cpu_relax();
150793b08e2SChristophe Leroy 	mb(); /* make sure all irqs are disabled before this */
151793b08e2SChristophe Leroy 	hw_breakpoint_disable();
152793b08e2SChristophe Leroy 	/*
153793b08e2SChristophe Leroy 	 * Now every CPU has IRQs off, we can clear out any pending
154793b08e2SChristophe Leroy 	 * IPIs and be sure that no more will come in after this.
155793b08e2SChristophe Leroy 	 */
156793b08e2SChristophe Leroy 	if (ppc_md.kexec_cpu_down)
157793b08e2SChristophe Leroy 		ppc_md.kexec_cpu_down(0, 1);
158793b08e2SChristophe Leroy 
159000a42b3SAneesh Kumar K.V 	reset_sprs();
160000a42b3SAneesh Kumar K.V 
161793b08e2SChristophe Leroy 	kexec_smp_wait();
162793b08e2SChristophe Leroy 	/* NOTREACHED */
163793b08e2SChristophe Leroy }
164793b08e2SChristophe Leroy 
kexec_prepare_cpus_wait(int wait_state)165793b08e2SChristophe Leroy static void kexec_prepare_cpus_wait(int wait_state)
166793b08e2SChristophe Leroy {
167793b08e2SChristophe Leroy 	int my_cpu, i, notified=-1;
168793b08e2SChristophe Leroy 
169793b08e2SChristophe Leroy 	hw_breakpoint_disable();
170793b08e2SChristophe Leroy 	my_cpu = get_cpu();
171793b08e2SChristophe Leroy 	/* Make sure each CPU has at least made it to the state we need.
172793b08e2SChristophe Leroy 	 *
173793b08e2SChristophe Leroy 	 * FIXME: There is a (slim) chance of a problem if not all of the CPUs
174793b08e2SChristophe Leroy 	 * are correctly onlined.  If somehow we start a CPU on boot with RTAS
175793b08e2SChristophe Leroy 	 * start-cpu, but somehow that CPU doesn't write callin_cpu_map[] in
176793b08e2SChristophe Leroy 	 * time, the boot CPU will timeout.  If it does eventually execute
177793b08e2SChristophe Leroy 	 * stuff, the secondary will start up (paca_ptrs[]->cpu_start was
178793b08e2SChristophe Leroy 	 * written) and get into a peculiar state.
179793b08e2SChristophe Leroy 	 * If the platform supports smp_ops->take_timebase(), the secondary CPU
180793b08e2SChristophe Leroy 	 * will probably be spinning in there.  If not (i.e. pseries), the
181793b08e2SChristophe Leroy 	 * secondary will continue on and try to online itself/idle/etc. If it
182793b08e2SChristophe Leroy 	 * survives that, we need to find these
183793b08e2SChristophe Leroy 	 * possible-but-not-online-but-should-be CPUs and chaperone them into
184793b08e2SChristophe Leroy 	 * kexec_smp_wait().
185793b08e2SChristophe Leroy 	 */
186793b08e2SChristophe Leroy 	for_each_online_cpu(i) {
187793b08e2SChristophe Leroy 		if (i == my_cpu)
188793b08e2SChristophe Leroy 			continue;
189793b08e2SChristophe Leroy 
190793b08e2SChristophe Leroy 		while (paca_ptrs[i]->kexec_state < wait_state) {
191793b08e2SChristophe Leroy 			barrier();
192793b08e2SChristophe Leroy 			if (i != notified) {
193793b08e2SChristophe Leroy 				printk(KERN_INFO "kexec: waiting for cpu %d "
194793b08e2SChristophe Leroy 				       "(physical %d) to enter %i state\n",
195793b08e2SChristophe Leroy 				       i, paca_ptrs[i]->hw_cpu_id, wait_state);
196793b08e2SChristophe Leroy 				notified = i;
197793b08e2SChristophe Leroy 			}
198793b08e2SChristophe Leroy 		}
199793b08e2SChristophe Leroy 	}
200793b08e2SChristophe Leroy 	mb();
201793b08e2SChristophe Leroy }
202793b08e2SChristophe Leroy 
203793b08e2SChristophe Leroy /*
204793b08e2SChristophe Leroy  * We need to make sure each present CPU is online.  The next kernel will scan
205793b08e2SChristophe Leroy  * the device tree and assume primary threads are online and query secondary
206793b08e2SChristophe Leroy  * threads via RTAS to online them if required.  If we don't online primary
207793b08e2SChristophe Leroy  * threads, they will be stuck.  However, we also online secondary threads as we
208793b08e2SChristophe Leroy  * may be using 'cede offline'.  In this case RTAS doesn't see the secondary
209793b08e2SChristophe Leroy  * threads as offline -- and again, these CPUs will be stuck.
210793b08e2SChristophe Leroy  *
211793b08e2SChristophe Leroy  * So, we online all CPUs that should be running, including secondary threads.
212793b08e2SChristophe Leroy  */
wake_offline_cpus(void)213793b08e2SChristophe Leroy static void wake_offline_cpus(void)
214793b08e2SChristophe Leroy {
215793b08e2SChristophe Leroy 	int cpu = 0;
216793b08e2SChristophe Leroy 
217793b08e2SChristophe Leroy 	for_each_present_cpu(cpu) {
218793b08e2SChristophe Leroy 		if (!cpu_online(cpu)) {
219793b08e2SChristophe Leroy 			printk(KERN_INFO "kexec: Waking offline cpu %d.\n",
220793b08e2SChristophe Leroy 			       cpu);
2214d37cc2dSQais Yousef 			WARN_ON(add_cpu(cpu));
222793b08e2SChristophe Leroy 		}
223793b08e2SChristophe Leroy 	}
224793b08e2SChristophe Leroy }
225793b08e2SChristophe Leroy 
kexec_prepare_cpus(void)226793b08e2SChristophe Leroy static void kexec_prepare_cpus(void)
227793b08e2SChristophe Leroy {
228793b08e2SChristophe Leroy 	wake_offline_cpus();
229793b08e2SChristophe Leroy 	smp_call_function(kexec_smp_down, NULL, /* wait */0);
230793b08e2SChristophe Leroy 	local_irq_disable();
231793b08e2SChristophe Leroy 	hard_irq_disable();
232793b08e2SChristophe Leroy 
233793b08e2SChristophe Leroy 	mb(); /* make sure IRQs are disabled before we say they are */
234793b08e2SChristophe Leroy 	get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
235793b08e2SChristophe Leroy 
236793b08e2SChristophe Leroy 	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
237793b08e2SChristophe Leroy 	/* we are sure every CPU has IRQs off at this point */
238793b08e2SChristophe Leroy 	kexec_all_irq_disabled = 1;
239793b08e2SChristophe Leroy 
240793b08e2SChristophe Leroy 	/*
241793b08e2SChristophe Leroy 	 * Before removing MMU mappings make sure all CPUs have entered real
242793b08e2SChristophe Leroy 	 * mode:
243793b08e2SChristophe Leroy 	 */
244793b08e2SChristophe Leroy 	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
245793b08e2SChristophe Leroy 
246793b08e2SChristophe Leroy 	/* after we tell the others to go down */
247793b08e2SChristophe Leroy 	if (ppc_md.kexec_cpu_down)
248793b08e2SChristophe Leroy 		ppc_md.kexec_cpu_down(0, 0);
249793b08e2SChristophe Leroy 
250793b08e2SChristophe Leroy 	put_cpu();
251793b08e2SChristophe Leroy }
252793b08e2SChristophe Leroy 
253793b08e2SChristophe Leroy #else /* ! SMP */
254793b08e2SChristophe Leroy 
kexec_prepare_cpus(void)255793b08e2SChristophe Leroy static void kexec_prepare_cpus(void)
256793b08e2SChristophe Leroy {
257793b08e2SChristophe Leroy 	/*
258793b08e2SChristophe Leroy 	 * move the secondarys to us so that we can copy
259793b08e2SChristophe Leroy 	 * the new kernel 0-0x100 safely
260793b08e2SChristophe Leroy 	 *
261793b08e2SChristophe Leroy 	 * do this if kexec in setup.c ?
262793b08e2SChristophe Leroy 	 *
263793b08e2SChristophe Leroy 	 * We need to release the cpus if we are ever going from an
264793b08e2SChristophe Leroy 	 * UP to an SMP kernel.
265793b08e2SChristophe Leroy 	 */
266793b08e2SChristophe Leroy 	smp_release_cpus();
267793b08e2SChristophe Leroy 	if (ppc_md.kexec_cpu_down)
268793b08e2SChristophe Leroy 		ppc_md.kexec_cpu_down(0, 0);
269793b08e2SChristophe Leroy 	local_irq_disable();
270793b08e2SChristophe Leroy 	hard_irq_disable();
271793b08e2SChristophe Leroy }
272793b08e2SChristophe Leroy 
273793b08e2SChristophe Leroy #endif /* SMP */
274793b08e2SChristophe Leroy 
275793b08e2SChristophe Leroy /*
276793b08e2SChristophe Leroy  * kexec thread structure and stack.
277793b08e2SChristophe Leroy  *
278793b08e2SChristophe Leroy  * We need to make sure that this is 16384-byte aligned due to the
279793b08e2SChristophe Leroy  * way process stacks are handled.  It also must be statically allocated
280793b08e2SChristophe Leroy  * or allocated as part of the kimage, because everything else may be
281793b08e2SChristophe Leroy  * overwritten when we copy the kexec image.  We piggyback on the
282793b08e2SChristophe Leroy  * "init_task" linker section here to statically allocate a stack.
283793b08e2SChristophe Leroy  *
284793b08e2SChristophe Leroy  * We could use a smaller stack if we don't care about anything using
285793b08e2SChristophe Leroy  * current, but that audit has not been performed.
286793b08e2SChristophe Leroy  */
287793b08e2SChristophe Leroy static union thread_union kexec_stack __init_task_data =
288793b08e2SChristophe Leroy 	{ };
289793b08e2SChristophe Leroy 
290793b08e2SChristophe Leroy /*
291793b08e2SChristophe Leroy  * For similar reasons to the stack above, the kexecing CPU needs to be on a
292793b08e2SChristophe Leroy  * static PACA; we switch to kexec_paca.
293793b08e2SChristophe Leroy  */
294a4abd55aSChristophe Leroy static struct paca_struct kexec_paca;
295793b08e2SChristophe Leroy 
296793b08e2SChristophe Leroy /* Our assembly helper, in misc_64.S */
297793b08e2SChristophe Leroy extern void kexec_sequence(void *newstack, unsigned long start,
298793b08e2SChristophe Leroy 			   void *image, void *control,
299793b08e2SChristophe Leroy 			   void (*clear_all)(void),
300793b08e2SChristophe Leroy 			   bool copy_with_mmu_off) __noreturn;
301793b08e2SChristophe Leroy 
302793b08e2SChristophe Leroy /* too late to fail here */
default_machine_kexec(struct kimage * image)303793b08e2SChristophe Leroy void default_machine_kexec(struct kimage *image)
304793b08e2SChristophe Leroy {
305793b08e2SChristophe Leroy 	bool copy_with_mmu_off;
306793b08e2SChristophe Leroy 
307793b08e2SChristophe Leroy 	/* prepare control code if any */
308793b08e2SChristophe Leroy 
309793b08e2SChristophe Leroy 	/*
310793b08e2SChristophe Leroy         * If the kexec boot is the normal one, need to shutdown other cpus
311793b08e2SChristophe Leroy         * into our wait loop and quiesce interrupts.
312793b08e2SChristophe Leroy         * Otherwise, in the case of crashed mode (crashing_cpu >= 0),
313793b08e2SChristophe Leroy         * stopping other CPUs and collecting their pt_regs is done before
314793b08e2SChristophe Leroy         * using debugger IPI.
315793b08e2SChristophe Leroy         */
316793b08e2SChristophe Leroy 
317793b08e2SChristophe Leroy 	if (!kdump_in_progress())
318793b08e2SChristophe Leroy 		kexec_prepare_cpus();
319793b08e2SChristophe Leroy 
320*d10e3c39SNicholas Piggin #ifdef CONFIG_PPC_PSERIES
321*d10e3c39SNicholas Piggin 	/*
322*d10e3c39SNicholas Piggin 	 * This must be done after other CPUs have shut down, otherwise they
323*d10e3c39SNicholas Piggin 	 * could execute the 'scv' instruction, which is not supported with
324*d10e3c39SNicholas Piggin 	 * reloc disabled (see configure_exceptions()).
325*d10e3c39SNicholas Piggin 	 */
326*d10e3c39SNicholas Piggin 	if (firmware_has_feature(FW_FEATURE_SET_MODE))
327*d10e3c39SNicholas Piggin 		pseries_disable_reloc_on_exc();
328*d10e3c39SNicholas Piggin #endif
329*d10e3c39SNicholas Piggin 
330793b08e2SChristophe Leroy 	printk("kexec: Starting switchover sequence.\n");
331793b08e2SChristophe Leroy 
332793b08e2SChristophe Leroy 	/* switch to a staticly allocated stack.  Based on irq stack code.
333793b08e2SChristophe Leroy 	 * We setup preempt_count to avoid using VMX in memcpy.
334793b08e2SChristophe Leroy 	 * XXX: the task struct will likely be invalid once we do the copy!
335793b08e2SChristophe Leroy 	 */
336793b08e2SChristophe Leroy 	current_thread_info()->flags = 0;
337793b08e2SChristophe Leroy 	current_thread_info()->preempt_count = HARDIRQ_OFFSET;
338793b08e2SChristophe Leroy 
339793b08e2SChristophe Leroy 	/* We need a static PACA, too; copy this CPU's PACA over and switch to
340793b08e2SChristophe Leroy 	 * it. Also poison per_cpu_offset and NULL lppaca to catch anyone using
341793b08e2SChristophe Leroy 	 * non-static data.
342793b08e2SChristophe Leroy 	 */
343793b08e2SChristophe Leroy 	memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct));
344793b08e2SChristophe Leroy 	kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL;
345793b08e2SChristophe Leroy #ifdef CONFIG_PPC_PSERIES
346793b08e2SChristophe Leroy 	kexec_paca.lppaca_ptr = NULL;
347793b08e2SChristophe Leroy #endif
348793b08e2SChristophe Leroy 
349793b08e2SChristophe Leroy 	if (is_secure_guest() && !(image->preserve_context ||
350793b08e2SChristophe Leroy 				   image->type == KEXEC_TYPE_CRASH)) {
351793b08e2SChristophe Leroy 		uv_unshare_all_pages();
352793b08e2SChristophe Leroy 		printk("kexec: Unshared all shared pages.\n");
353793b08e2SChristophe Leroy 	}
354793b08e2SChristophe Leroy 
355793b08e2SChristophe Leroy 	paca_ptrs[kexec_paca.paca_index] = &kexec_paca;
356793b08e2SChristophe Leroy 
357793b08e2SChristophe Leroy 	setup_paca(&kexec_paca);
358793b08e2SChristophe Leroy 
359793b08e2SChristophe Leroy 	/*
360793b08e2SChristophe Leroy 	 * The lppaca should be unregistered at this point so the HV won't
361793b08e2SChristophe Leroy 	 * touch it. In the case of a crash, none of the lppacas are
362793b08e2SChristophe Leroy 	 * unregistered so there is not much we can do about it here.
363793b08e2SChristophe Leroy 	 */
364793b08e2SChristophe Leroy 
365793b08e2SChristophe Leroy 	/*
366793b08e2SChristophe Leroy 	 * On Book3S, the copy must happen with the MMU off if we are either
367793b08e2SChristophe Leroy 	 * using Radix page tables or we are not in an LPAR since we can
368793b08e2SChristophe Leroy 	 * overwrite the page tables while copying.
369793b08e2SChristophe Leroy 	 *
370793b08e2SChristophe Leroy 	 * In an LPAR, we keep the MMU on otherwise we can't access beyond
371793b08e2SChristophe Leroy 	 * the RMA. On BookE there is no real MMU off mode, so we have to
372793b08e2SChristophe Leroy 	 * keep it enabled as well (but then we have bolted TLB entries).
373793b08e2SChristophe Leroy 	 */
374e0d68273SChristophe Leroy #ifdef CONFIG_PPC_BOOK3E_64
375793b08e2SChristophe Leroy 	copy_with_mmu_off = false;
376793b08e2SChristophe Leroy #else
377793b08e2SChristophe Leroy 	copy_with_mmu_off = radix_enabled() ||
378793b08e2SChristophe Leroy 		!(firmware_has_feature(FW_FEATURE_LPAR) ||
379793b08e2SChristophe Leroy 		  firmware_has_feature(FW_FEATURE_PS3_LV1));
380793b08e2SChristophe Leroy #endif
381793b08e2SChristophe Leroy 
382793b08e2SChristophe Leroy 	/* Some things are best done in assembly.  Finding globals with
383793b08e2SChristophe Leroy 	 * a toc is easier in C, so pass in what we can.
384793b08e2SChristophe Leroy 	 */
385793b08e2SChristophe Leroy 	kexec_sequence(&kexec_stack, image->start, image,
386793b08e2SChristophe Leroy 		       page_address(image->control_code_page),
387793b08e2SChristophe Leroy 		       mmu_cleanup_all, copy_with_mmu_off);
388793b08e2SChristophe Leroy 	/* NOTREACHED */
389793b08e2SChristophe Leroy }
390793b08e2SChristophe Leroy 
391387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
392793b08e2SChristophe Leroy /* Values we need to export to the second kernel via the device tree. */
393793b08e2SChristophe Leroy static unsigned long htab_base;
394793b08e2SChristophe Leroy static unsigned long htab_size;
395793b08e2SChristophe Leroy 
396793b08e2SChristophe Leroy static struct property htab_base_prop = {
397793b08e2SChristophe Leroy 	.name = "linux,htab-base",
398793b08e2SChristophe Leroy 	.length = sizeof(unsigned long),
399793b08e2SChristophe Leroy 	.value = &htab_base,
400793b08e2SChristophe Leroy };
401793b08e2SChristophe Leroy 
402793b08e2SChristophe Leroy static struct property htab_size_prop = {
403793b08e2SChristophe Leroy 	.name = "linux,htab-size",
404793b08e2SChristophe Leroy 	.length = sizeof(unsigned long),
405793b08e2SChristophe Leroy 	.value = &htab_size,
406793b08e2SChristophe Leroy };
407793b08e2SChristophe Leroy 
export_htab_values(void)408793b08e2SChristophe Leroy static int __init export_htab_values(void)
409793b08e2SChristophe Leroy {
410793b08e2SChristophe Leroy 	struct device_node *node;
411793b08e2SChristophe Leroy 
412793b08e2SChristophe Leroy 	/* On machines with no htab htab_address is NULL */
413793b08e2SChristophe Leroy 	if (!htab_address)
414793b08e2SChristophe Leroy 		return -ENODEV;
415793b08e2SChristophe Leroy 
416793b08e2SChristophe Leroy 	node = of_find_node_by_path("/chosen");
417793b08e2SChristophe Leroy 	if (!node)
418793b08e2SChristophe Leroy 		return -ENODEV;
419793b08e2SChristophe Leroy 
4201fd02f66SJulia Lawall 	/* remove any stale properties so ours can be found */
421793b08e2SChristophe Leroy 	of_remove_property(node, of_find_property(node, htab_base_prop.name, NULL));
422793b08e2SChristophe Leroy 	of_remove_property(node, of_find_property(node, htab_size_prop.name, NULL));
423793b08e2SChristophe Leroy 
424793b08e2SChristophe Leroy 	htab_base = cpu_to_be64(__pa(htab_address));
425793b08e2SChristophe Leroy 	of_add_property(node, &htab_base_prop);
426793b08e2SChristophe Leroy 	htab_size = cpu_to_be64(htab_size_bytes);
427793b08e2SChristophe Leroy 	of_add_property(node, &htab_size_prop);
428793b08e2SChristophe Leroy 
429793b08e2SChristophe Leroy 	of_node_put(node);
430793b08e2SChristophe Leroy 	return 0;
431793b08e2SChristophe Leroy }
432793b08e2SChristophe Leroy late_initcall(export_htab_values);
433387e220aSNicholas Piggin #endif /* CONFIG_PPC_64S_HASH_MMU */
434