Pull button into test branch
[linux-drm-fsl-dcu.git] / arch / ia64 / kernel / crash.c
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>
19
20 #include <asm/kdebug.h>
21 #include <asm/mca.h>
22
23 int kdump_status[NR_CPUS];
24 atomic_t kdump_cpu_freezed;
25 atomic_t kdump_in_progress;
26 int kdump_on_init = 1;
27
28 static 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
44 static void
45 final_note(void *buf)
46 {
47         memset(buf, 0, sizeof(struct elf_note));
48 }
49
50 extern void ia64_dump_cpu_regs(void *);
51
52 static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
53
54 void
55 crash_save_this_cpu()
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
82 static int
83 kdump_wait_cpu_freeze(void)
84 {
85         int cpu_num = num_online_cpus() - 1;
86         int timeout = 1000;
87         while(timeout-- > 0) {
88                 if (atomic_read(&kdump_cpu_freezed) == cpu_num)
89                         return 0;
90                 udelay(1000);
91         }
92         return 1;
93 }
94
95 void
96 machine_crash_shutdown(struct pt_regs *pt)
97 {
98         /* This function is only called after the system
99          * has paniced or is otherwise in a critical state.
100          * The minimum amount of code to allow a kexec'd kernel
101          * to run successfully needs to happen here.
102          *
103          * In practice this means shooting down the other cpus in
104          * an SMP system.
105          */
106         kexec_disable_iosapic();
107 #ifdef CONFIG_SMP
108         kdump_smp_send_stop();
109         if (kdump_wait_cpu_freeze() && kdump_on_init)   {
110                 //not all cpu response to IPI, send INIT to freeze them
111                 kdump_smp_send_init();
112         }
113 #endif
114 }
115
116 static void
117 machine_kdump_on_init(void)
118 {
119         local_irq_disable();
120         kexec_disable_iosapic();
121         machine_kexec(ia64_kimage);
122 }
123
124 void
125 kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
126 {
127         int cpuid;
128         local_irq_disable();
129         cpuid = smp_processor_id();
130         crash_save_this_cpu();
131         current->thread.ksp = (__u64)info->sw - 16;
132         atomic_inc(&kdump_cpu_freezed);
133         kdump_status[cpuid] = 1;
134         mb();
135         if (cpuid == 0) {
136                 for (;;)
137                         cpu_relax();
138         } else
139                 ia64_jump_to_sal(&sal_boot_rendez_state[cpuid]);
140 }
141
142 static int
143 kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
144 {
145         struct ia64_mca_notify_die *nd;
146         struct die_args *args = data;
147
148         if (!kdump_on_init)
149                 return NOTIFY_DONE;
150
151         if (val != DIE_INIT_MONARCH_ENTER &&
152             val != DIE_INIT_SLAVE_ENTER &&
153             val != DIE_MCA_RENDZVOUS_LEAVE &&
154             val != DIE_MCA_MONARCH_LEAVE)
155                 return NOTIFY_DONE;
156
157         nd = (struct ia64_mca_notify_die *)args->err;
158         /* Reason code 1 means machine check rendezous*/
159         if ((val == DIE_INIT_MONARCH_ENTER || DIE_INIT_SLAVE_ENTER) &&
160                  nd->sos->rv_rc == 1)
161                 return NOTIFY_DONE;
162
163         switch (val) {
164                 case DIE_INIT_MONARCH_ENTER:
165                         machine_kdump_on_init();
166                         break;
167                 case DIE_INIT_SLAVE_ENTER:
168                         unw_init_running(kdump_cpu_freeze, NULL);
169                         break;
170                 case DIE_MCA_RENDZVOUS_LEAVE:
171                         if (atomic_read(&kdump_in_progress))
172                                 unw_init_running(kdump_cpu_freeze, NULL);
173                         break;
174                 case DIE_MCA_MONARCH_LEAVE:
175                      /* die_register->signr indicate if MCA is recoverable */
176                         if (!args->signr)
177                                 machine_kdump_on_init();
178                         break;
179         }
180         return NOTIFY_DONE;
181 }
182
183 #ifdef CONFIG_SYSCTL
184 static ctl_table kdump_on_init_table[] = {
185         {
186                 .ctl_name = CTL_UNNUMBERED,
187                 .procname = "kdump_on_init",
188                 .data = &kdump_on_init,
189                 .maxlen = sizeof(int),
190                 .mode = 0644,
191                 .proc_handler = &proc_dointvec,
192         },
193         { .ctl_name = 0 }
194 };
195
196 static ctl_table sys_table[] = {
197         {
198           .ctl_name = CTL_KERN,
199           .procname = "kernel",
200           .mode = 0555,
201           .child = kdump_on_init_table,
202         },
203         { .ctl_name = 0 }
204 };
205 #endif
206
207 static int
208 machine_crash_setup(void)
209 {
210         static struct notifier_block kdump_init_notifier_nb = {
211                 .notifier_call = kdump_init_notifier,
212         };
213         int ret;
214         if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
215                 return ret;
216 #ifdef CONFIG_SYSCTL
217         register_sysctl_table(sys_table, 0);
218 #endif
219         return 0;
220 }
221
222 __initcall(machine_crash_setup);
223