move die notifier handling to common code
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / ia64 / kernel / crash.c
CommitLineData
a7956113
ZN
1/*
2 * arch/ia64/kernel/crash.c
3 *
4 * Architecture specific (ia64) functions for kexec based crash dumps.
5 *
6 * Created by: Khalid Aziz <khalid.aziz@hp.com>
7 * Copyright (C) 2005 Hewlett-Packard Development Company, L.P.
8 * Copyright (C) 2005 Intel Corp Zou Nan hai <nanhai.zou@intel.com>
9 *
10 */
11#include <linux/smp.h>
12#include <linux/delay.h>
13#include <linux/crash_dump.h>
14#include <linux/bootmem.h>
15#include <linux/kexec.h>
16#include <linux/elfcore.h>
17#include <linux/sysctl.h>
18#include <linux/init.h>
1eeb66a1 19#include <linux/kdebug.h>
a7956113 20
a7956113 21#include <asm/mca.h>
a7956113
ZN
22
23int kdump_status[NR_CPUS];
0ac1faca 24static atomic_t kdump_cpu_frozen;
a7956113 25atomic_t kdump_in_progress;
0ac1faca 26static int kdump_on_init = 1;
a7956113
ZN
27
28static inline Elf64_Word
29*append_elf_note(Elf64_Word *buf, char *name, unsigned type, void *data,
30 size_t data_len)
31{
32 struct elf_note *note = (struct elf_note *)buf;
33 note->n_namesz = strlen(name) + 1;
34 note->n_descsz = data_len;
35 note->n_type = type;
36 buf += (sizeof(*note) + 3)/4;
37 memcpy(buf, name, note->n_namesz);
38 buf += (note->n_namesz + 3)/4;
39 memcpy(buf, data, data_len);
40 buf += (data_len + 3)/4;
41 return buf;
42}
43
44static void
45final_note(void *buf)
46{
47 memset(buf, 0, sizeof(struct elf_note));
48}
49
50extern void ia64_dump_cpu_regs(void *);
51
52static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
53
54void
ccbebdac 55crash_save_this_cpu(void)
a7956113
ZN
56{
57 void *buf;
58 unsigned long cfm, sof, sol;
59
60 int cpu = smp_processor_id();
61 struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu);
62
63 elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg);
64 memset(prstatus, 0, sizeof(*prstatus));
65 prstatus->pr_pid = current->pid;
66
67 ia64_dump_cpu_regs(dst);
68 cfm = dst[43];
69 sol = (cfm >> 7) & 0x7f;
70 sof = cfm & 0x7f;
71 dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46],
72 sof - sol);
73
74 buf = (u64 *) per_cpu_ptr(crash_notes, cpu);
75 if (!buf)
76 return;
77 buf = append_elf_note(buf, "CORE", NT_PRSTATUS, prstatus,
78 sizeof(*prstatus));
79 final_note(buf);
80}
81
bcb9b99d 82#ifdef CONFIG_SMP
a7956113
ZN
83static int
84kdump_wait_cpu_freeze(void)
85{
86 int cpu_num = num_online_cpus() - 1;
87 int timeout = 1000;
88 while(timeout-- > 0) {
0ac1faca 89 if (atomic_read(&kdump_cpu_frozen) == cpu_num)
a7956113
ZN
90 return 0;
91 udelay(1000);
92 }
93 return 1;
94}
bcb9b99d 95#endif
a7956113
ZN
96
97void
98machine_crash_shutdown(struct pt_regs *pt)
99{
100 /* This function is only called after the system
101 * has paniced or is otherwise in a critical state.
102 * The minimum amount of code to allow a kexec'd kernel
103 * to run successfully needs to happen here.
104 *
105 * In practice this means shooting down the other cpus in
106 * an SMP system.
107 */
108 kexec_disable_iosapic();
109#ifdef CONFIG_SMP
110 kdump_smp_send_stop();
0ac1faca 111 /* not all cpu response to IPI, send INIT to freeze them */
a7956113 112 if (kdump_wait_cpu_freeze() && kdump_on_init) {
a7956113
ZN
113 kdump_smp_send_init();
114 }
115#endif
116}
117
118static void
119machine_kdump_on_init(void)
120{
c2c77fe8
BW
121 if (!ia64_kimage) {
122 printk(KERN_NOTICE "machine_kdump_on_init(): "
123 "kdump not configured\n");
124 return;
125 }
a7956113
ZN
126 local_irq_disable();
127 kexec_disable_iosapic();
128 machine_kexec(ia64_kimage);
129}
130
131void
132kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
133{
134 int cpuid;
135 local_irq_disable();
136 cpuid = smp_processor_id();
137 crash_save_this_cpu();
138 current->thread.ksp = (__u64)info->sw - 16;
0ac1faca 139 atomic_inc(&kdump_cpu_frozen);
a7956113
ZN
140 kdump_status[cpuid] = 1;
141 mb();
bcb9b99d
MD
142#ifdef CONFIG_HOTPLUG_CPU
143 if (cpuid != 0)
a7956113 144 ia64_jump_to_sal(&sal_boot_rendez_state[cpuid]);
bcb9b99d
MD
145#endif
146 for (;;)
147 cpu_relax();
a7956113
ZN
148}
149
150static int
151kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
152{
153 struct ia64_mca_notify_die *nd;
154 struct die_args *args = data;
155
156 if (!kdump_on_init)
157 return NOTIFY_DONE;
158
159 if (val != DIE_INIT_MONARCH_ENTER &&
160 val != DIE_INIT_SLAVE_ENTER &&
161 val != DIE_MCA_RENDZVOUS_LEAVE &&
162 val != DIE_MCA_MONARCH_LEAVE)
163 return NOTIFY_DONE;
164
165 nd = (struct ia64_mca_notify_die *)args->err;
166 /* Reason code 1 means machine check rendezous*/
60b548df 167 if ((val == DIE_INIT_MONARCH_ENTER || val == DIE_INIT_SLAVE_ENTER) &&
a7956113
ZN
168 nd->sos->rv_rc == 1)
169 return NOTIFY_DONE;
170
171 switch (val) {
172 case DIE_INIT_MONARCH_ENTER:
173 machine_kdump_on_init();
174 break;
175 case DIE_INIT_SLAVE_ENTER:
176 unw_init_running(kdump_cpu_freeze, NULL);
177 break;
178 case DIE_MCA_RENDZVOUS_LEAVE:
179 if (atomic_read(&kdump_in_progress))
180 unw_init_running(kdump_cpu_freeze, NULL);
181 break;
182 case DIE_MCA_MONARCH_LEAVE:
183 /* die_register->signr indicate if MCA is recoverable */
184 if (!args->signr)
185 machine_kdump_on_init();
186 break;
187 }
188 return NOTIFY_DONE;
189}
190
191#ifdef CONFIG_SYSCTL
192static ctl_table kdump_on_init_table[] = {
193 {
194 .ctl_name = CTL_UNNUMBERED,
195 .procname = "kdump_on_init",
196 .data = &kdump_on_init,
197 .maxlen = sizeof(int),
198 .mode = 0644,
199 .proc_handler = &proc_dointvec,
200 },
201 { .ctl_name = 0 }
202};
203
204static ctl_table sys_table[] = {
205 {
206 .ctl_name = CTL_KERN,
207 .procname = "kernel",
208 .mode = 0555,
209 .child = kdump_on_init_table,
210 },
211 { .ctl_name = 0 }
212};
213#endif
214
215static int
216machine_crash_setup(void)
217{
a7956113
ZN
218 static struct notifier_block kdump_init_notifier_nb = {
219 .notifier_call = kdump_init_notifier,
220 };
221 int ret;
a7956113
ZN
222 if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
223 return ret;
224#ifdef CONFIG_SYSCTL
0b4d4147 225 register_sysctl_table(sys_table);
a7956113
ZN
226#endif
227 return 0;
228}
229
230__initcall(machine_crash_setup);
231