blob: bf6fbc03ebafbfc3cb9f889cef3f3f1868fe7dc9 [file] [log] [blame]
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07001/*
2 * arch/s390/kernel/machine_kexec.c
3 *
Michael Holzheu60a0c682011-10-30 15:16:40 +01004 * Copyright IBM Corp. 2005,2011
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07005 *
Heiko Carstensc6b5b842006-12-04 15:40:33 +01006 * Author(s): Rolf Adelsberger,
7 * Heiko Carstens <heiko.carstens@de.ibm.com>
Michael Holzheu60a0c682011-10-30 15:16:40 +01008 * Michael Holzheu <holzheu@linux.vnet.ibm.com>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07009 */
10
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070011#include <linux/device.h>
12#include <linux/mm.h>
13#include <linux/kexec.h>
14#include <linux/delay.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010015#include <linux/reboot.h>
Heiko Carstens69667272011-03-15 17:08:33 +010016#include <linux/ftrace.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080017#include <asm/cio.h>
18#include <asm/setup.h>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070019#include <asm/pgtable.h>
20#include <asm/pgalloc.h>
21#include <asm/system.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080022#include <asm/smp.h>
Heiko Carstens15e9b582006-12-04 15:40:26 +010023#include <asm/reset.h>
Heiko Carstens9c9c1762007-03-05 23:35:45 +010024#include <asm/ipl.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010025#include <asm/diag.h>
26#include <asm/asm-offsets.h>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070027
Heiko Carstensc6b5b842006-12-04 15:40:33 +010028typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070029
Tobias Klauser2efe55a2006-06-26 18:57:34 +020030extern const unsigned char relocate_kernel[];
31extern const unsigned long long relocate_kernel_len;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070032
Michael Holzheu60a0c682011-10-30 15:16:40 +010033#ifdef CONFIG_CRASH_DUMP
34
35void *fill_cpu_elf_notes(void *ptr, struct save_area *sa);
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);
48 memset(ptr, 0, sizeof(struct elf_note));
49}
50
51/*
Michael Holzheu60a0c682011-10-30 15:16:40 +010052 * Initialize CPU ELF notes
53 */
54void setup_regs(void)
55{
56 unsigned long sa = S390_lowcore.prefixreg_save_area + SAVE_AREA_BASE;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040057 int cpu, this_cpu;
Michael Holzheu60a0c682011-10-30 15:16:40 +010058
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040059 this_cpu = smp_find_processor_id(stap());
60 add_elf_notes(this_cpu);
Michael Holzheu60a0c682011-10-30 15:16:40 +010061 for_each_online_cpu(cpu) {
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040062 if (cpu == this_cpu)
Michael Holzheu60a0c682011-10-30 15:16:40 +010063 continue;
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -040064 if (smp_store_status(cpu))
65 continue;
Michael Holzheu60a0c682011-10-30 15:16:40 +010066 add_elf_notes(cpu);
Michael Holzheu60a0c682011-10-30 15:16:40 +010067 }
68 /* Copy dump CPU store status info to absolute zero */
69 memcpy((void *) SAVE_AREA_BASE, (void *) sa, sizeof(struct save_area));
70}
71
72#endif
73
74/*
75 * Start kdump: We expect here that a store status has been done on our CPU
76 */
77static void __do_machine_kdump(void *image)
78{
79#ifdef CONFIG_CRASH_DUMP
80 int (*start_kdump)(int) = (void *)((struct kimage *) image)->start;
81
Martin Schwidefskyb50511e2011-10-30 15:16:50 +010082 __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
Michael Holzheu60a0c682011-10-30 15:16:40 +010083 setup_regs();
84 start_kdump(1);
85#endif
86}
87
88/*
89 * Check if kdump checksums are valid: We call purgatory with parameter "0"
90 */
91static int kdump_csum_valid(struct kimage *image)
92{
93#ifdef CONFIG_CRASH_DUMP
94 int (*start_kdump)(int) = (void *)image->start;
95 int rc;
96
97 __arch_local_irq_stnsm(0xfb); /* disable DAT */
98 rc = start_kdump(0);
99 __arch_local_irq_stosm(0x04); /* enable DAT */
100 return rc ? 0 : -EINVAL;
101#else
102 return -EINVAL;
103#endif
104}
105
106/*
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100107 * Map or unmap crashkernel memory
108 */
109static void crash_map_pages(int enable)
110{
111 unsigned long size = resource_size(&crashk_res);
112
113 BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
114 size % KEXEC_CRASH_MEM_ALIGN);
115 if (enable)
116 vmem_add_mapping(crashk_res.start, size);
117 else
118 vmem_remove_mapping(crashk_res.start, size);
119}
120
121/*
122 * Map crashkernel memory
123 */
124void crash_map_reserved_pages(void)
125{
126 crash_map_pages(1);
127}
128
129/*
130 * Unmap crashkernel memory
131 */
132void crash_unmap_reserved_pages(void)
133{
134 crash_map_pages(0);
135}
136
137/*
Michael Holzheu60a0c682011-10-30 15:16:40 +0100138 * Give back memory to hypervisor before new kdump is loaded
139 */
140static int machine_kexec_prepare_kdump(void)
141{
142#ifdef CONFIG_CRASH_DUMP
143 if (MACHINE_IS_VM)
144 diag10_range(PFN_DOWN(crashk_res.start),
145 PFN_DOWN(crashk_res.end - crashk_res.start + 1));
146 return 0;
147#else
148 return -EINVAL;
149#endif
150}
151
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100152int machine_kexec_prepare(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700153{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100154 void *reboot_code_buffer;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700155
Heiko Carstens9c9c1762007-03-05 23:35:45 +0100156 /* Can't replace kernel image since it is read-only. */
157 if (ipl_flags & IPL_NSS_VALID)
158 return -ENOSYS;
159
Michael Holzheu60a0c682011-10-30 15:16:40 +0100160 if (image->type == KEXEC_TYPE_CRASH)
161 return machine_kexec_prepare_kdump();
162
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700163 /* We don't support anything but the default image type for now. */
164 if (image->type != KEXEC_TYPE_DEFAULT)
165 return -EINVAL;
166
167 /* Get the destination where the assembler code should be copied to.*/
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100168 reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700169
170 /* Then copy it */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100171 memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700172 return 0;
173}
174
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100175void machine_kexec_cleanup(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700176{
177}
178
Michael Holzheu60a0c682011-10-30 15:16:40 +0100179void arch_crash_save_vmcoreinfo(void)
180{
181 VMCOREINFO_SYMBOL(lowcore_ptr);
Michael Holzheu7fe7a182011-12-27 11:27:24 +0100182 VMCOREINFO_SYMBOL(high_memory);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100183 VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
184}
185
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100186void machine_shutdown(void)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700187{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700188}
189
Michael Holzheu60a0c682011-10-30 15:16:40 +0100190/*
191 * Do normal kexec
192 */
193static void __do_machine_kexec(void *data)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700194{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700195 relocate_kernel_t data_mover;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100196 struct kimage *image = data;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700197
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100198 data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700199
200 /* Call the moving routine */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100201 (*data_mover)(&image->head, image->start);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700202}
Heiko Carstens2c2df112010-02-26 22:37:34 +0100203
Michael Holzheu60a0c682011-10-30 15:16:40 +0100204/*
205 * Reset system and call either kdump or normal kexec
206 */
207static void __machine_kexec(void *data)
208{
209 struct kimage *image = data;
210
211 pfault_fini();
212 if (image->type == KEXEC_TYPE_CRASH)
213 s390_reset_system(__do_machine_kdump, data);
214 else
215 s390_reset_system(__do_machine_kexec, data);
216 disabled_wait((unsigned long) __builtin_return_address(0));
217}
218
219/*
220 * Do either kdump or normal kexec. In case of kdump we first ask
221 * purgatory, if kdump checksums are valid.
222 */
Heiko Carstens2c2df112010-02-26 22:37:34 +0100223void machine_kexec(struct kimage *image)
224{
Michael Holzheu60a0c682011-10-30 15:16:40 +0100225 if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
226 return;
Heiko Carstens69667272011-03-15 17:08:33 +0100227 tracer_disable();
Heiko Carstens2c2df112010-02-26 22:37:34 +0100228 smp_send_stop();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400229 smp_call_ipl_cpu(__machine_kexec, image);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100230}