1/*
2 * Copyright IBM Corp. 2005, 2011
3 *
4 * Author(s): Rolf Adelsberger,
5 *	      Heiko Carstens <heiko.carstens@de.ibm.com>
6 *	      Michael Holzheu <holzheu@linux.vnet.ibm.com>
7 */
8
9#include <linux/device.h>
10#include <linux/mm.h>
11#include <linux/kexec.h>
12#include <linux/delay.h>
13#include <linux/reboot.h>
14#include <linux/ftrace.h>
15#include <linux/debug_locks.h>
16#include <linux/suspend.h>
17#include <asm/cio.h>
18#include <asm/setup.h>
19#include <asm/pgtable.h>
20#include <asm/pgalloc.h>
21#include <asm/smp.h>
22#include <asm/reset.h>
23#include <asm/ipl.h>
24#include <asm/diag.h>
25#include <asm/elf.h>
26#include <asm/asm-offsets.h>
27#include <asm/os_info.h>
28#include <asm/switch_to.h>
29
30typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
31
32extern const unsigned char relocate_kernel[];
33extern const unsigned long long relocate_kernel_len;
34
35#ifdef CONFIG_CRASH_DUMP
36
37/*
38 * Create ELF notes for one CPU
39 */
40static void add_elf_notes(int cpu)
41{
42	struct save_area *sa = (void *) 4608 + store_prefix();
43	void *ptr;
44
45	memcpy((void *) (4608UL + sa->pref_reg), sa, sizeof(*sa));
46	ptr = (u64 *) per_cpu_ptr(crash_notes, cpu);
47	ptr = fill_cpu_elf_notes(ptr, sa, NULL);
48	memset(ptr, 0, sizeof(struct elf_note));
49}
50
51/*
52 * Initialize CPU ELF notes
53 */
54static void setup_regs(void)
55{
56	unsigned long sa = S390_lowcore.prefixreg_save_area + SAVE_AREA_BASE;
57	struct _lowcore *lc;
58	int cpu, this_cpu;
59
60	/* Get lowcore pointer from store status of this CPU (absolute zero) */
61	lc = (struct _lowcore *)(unsigned long)S390_lowcore.prefixreg_save_area;
62	this_cpu = smp_find_processor_id(stap());
63	add_elf_notes(this_cpu);
64	for_each_online_cpu(cpu) {
65		if (cpu == this_cpu)
66			continue;
67		if (smp_store_status(cpu))
68			continue;
69		add_elf_notes(cpu);
70	}
71	if (MACHINE_HAS_VX)
72		save_vx_regs_safe((void *) lc->vector_save_area_addr);
73	/* Copy dump CPU store status info to absolute zero */
74	memcpy((void *) SAVE_AREA_BASE, (void *) sa, sizeof(struct save_area));
75}
76
77/*
78 * PM notifier callback for kdump
79 */
80static int machine_kdump_pm_cb(struct notifier_block *nb, unsigned long action,
81			       void *ptr)
82{
83	switch (action) {
84	case PM_SUSPEND_PREPARE:
85	case PM_HIBERNATION_PREPARE:
86		if (crashk_res.start)
87			crash_map_reserved_pages();
88		break;
89	case PM_POST_SUSPEND:
90	case PM_POST_HIBERNATION:
91		if (crashk_res.start)
92			crash_unmap_reserved_pages();
93		break;
94	default:
95		return NOTIFY_DONE;
96	}
97	return NOTIFY_OK;
98}
99
100static int __init machine_kdump_pm_init(void)
101{
102	pm_notifier(machine_kdump_pm_cb, 0);
103	return 0;
104}
105arch_initcall(machine_kdump_pm_init);
106#endif
107
108/*
109 * Start kdump: We expect here that a store status has been done on our CPU
110 */
111static void __do_machine_kdump(void *image)
112{
113#ifdef CONFIG_CRASH_DUMP
114	int (*start_kdump)(int) = (void *)((struct kimage *) image)->start;
115
116	setup_regs();
117	__load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
118	start_kdump(1);
119#endif
120}
121
122/*
123 * Check if kdump checksums are valid: We call purgatory with parameter "0"
124 */
125static int kdump_csum_valid(struct kimage *image)
126{
127#ifdef CONFIG_CRASH_DUMP
128	int (*start_kdump)(int) = (void *)image->start;
129	int rc;
130
131	__arch_local_irq_stnsm(0xfb); /* disable DAT */
132	rc = start_kdump(0);
133	__arch_local_irq_stosm(0x04); /* enable DAT */
134	return rc ? 0 : -EINVAL;
135#else
136	return -EINVAL;
137#endif
138}
139
140/*
141 * Map or unmap crashkernel memory
142 */
143static void crash_map_pages(int enable)
144{
145	unsigned long size = resource_size(&crashk_res);
146
147	BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
148	       size % KEXEC_CRASH_MEM_ALIGN);
149	if (enable)
150		vmem_add_mapping(crashk_res.start, size);
151	else {
152		vmem_remove_mapping(crashk_res.start, size);
153		if (size)
154			os_info_crashkernel_add(crashk_res.start, size);
155		else
156			os_info_crashkernel_add(0, 0);
157	}
158}
159
160/*
161 * Map crashkernel memory
162 */
163void crash_map_reserved_pages(void)
164{
165	crash_map_pages(1);
166}
167
168/*
169 * Unmap crashkernel memory
170 */
171void crash_unmap_reserved_pages(void)
172{
173	crash_map_pages(0);
174}
175
176/*
177 * Give back memory to hypervisor before new kdump is loaded
178 */
179static int machine_kexec_prepare_kdump(void)
180{
181#ifdef CONFIG_CRASH_DUMP
182	if (MACHINE_IS_VM)
183		diag10_range(PFN_DOWN(crashk_res.start),
184			     PFN_DOWN(crashk_res.end - crashk_res.start + 1));
185	return 0;
186#else
187	return -EINVAL;
188#endif
189}
190
191int machine_kexec_prepare(struct kimage *image)
192{
193	void *reboot_code_buffer;
194
195	/* Can't replace kernel image since it is read-only. */
196	if (ipl_flags & IPL_NSS_VALID)
197		return -EOPNOTSUPP;
198
199	if (image->type == KEXEC_TYPE_CRASH)
200		return machine_kexec_prepare_kdump();
201
202	/* We don't support anything but the default image type for now. */
203	if (image->type != KEXEC_TYPE_DEFAULT)
204		return -EINVAL;
205
206	/* Get the destination where the assembler code should be copied to.*/
207	reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
208
209	/* Then copy it */
210	memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
211	return 0;
212}
213
214void machine_kexec_cleanup(struct kimage *image)
215{
216}
217
218void arch_crash_save_vmcoreinfo(void)
219{
220	VMCOREINFO_SYMBOL(lowcore_ptr);
221	VMCOREINFO_SYMBOL(high_memory);
222	VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
223}
224
225void machine_shutdown(void)
226{
227}
228
229void machine_crash_shutdown(struct pt_regs *regs)
230{
231}
232
233/*
234 * Do normal kexec
235 */
236static void __do_machine_kexec(void *data)
237{
238	relocate_kernel_t data_mover;
239	struct kimage *image = data;
240
241	data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
242
243	/* Call the moving routine */
244	(*data_mover)(&image->head, image->start);
245}
246
247/*
248 * Reset system and call either kdump or normal kexec
249 */
250static void __machine_kexec(void *data)
251{
252	struct kimage *image = data;
253
254	__arch_local_irq_stosm(0x04); /* enable DAT */
255	pfault_fini();
256	tracing_off();
257	debug_locks_off();
258	if (image->type == KEXEC_TYPE_CRASH) {
259		lgr_info_log();
260		s390_reset_system(__do_machine_kdump, data);
261	} else {
262		s390_reset_system(__do_machine_kexec, data);
263	}
264	disabled_wait((unsigned long) __builtin_return_address(0));
265}
266
267/*
268 * Do either kdump or normal kexec. In case of kdump we first ask
269 * purgatory, if kdump checksums are valid.
270 */
271void machine_kexec(struct kimage *image)
272{
273	if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
274		return;
275	tracer_disable();
276	smp_send_stop();
277	smp_call_ipl_cpu(__machine_kexec, image);
278}
279