fadump: Register for firmware assisted dump.
[linux-2.6-block.git] / arch / powerpc / kernel / fadump.c
CommitLineData
eb39c880
MS
1/*
2 * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
3 * dump with assistance from firmware. This approach does not use kexec,
4 * instead firmware assists in booting the kdump kernel while preserving
5 * memory contents. The most of the code implementation has been adapted
6 * from phyp assisted dump implementation written by Linas Vepstas and
7 * Manish Ahuja
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * Copyright 2011 IBM Corporation
24 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
25 */
26
27#undef DEBUG
28#define pr_fmt(fmt) "fadump: " fmt
29
30#include <linux/string.h>
31#include <linux/memblock.h>
3ccc00a7
MS
32#include <linux/delay.h>
33#include <linux/debugfs.h>
34#include <linux/seq_file.h>
eb39c880
MS
35
36#include <asm/page.h>
37#include <asm/prom.h>
38#include <asm/rtas.h>
39#include <asm/fadump.h>
40
41static struct fw_dump fw_dump;
3ccc00a7
MS
42static struct fadump_mem_struct fdm;
43static const struct fadump_mem_struct *fdm_active;
44
45static DEFINE_MUTEX(fadump_mutex);
eb39c880
MS
46
47/* Scan the Firmware Assisted dump configuration details. */
48int __init early_init_dt_scan_fw_dump(unsigned long node,
49 const char *uname, int depth, void *data)
50{
51 __be32 *sections;
52 int i, num_sections;
53 unsigned long size;
54 const int *token;
55
56 if (depth != 1 || strcmp(uname, "rtas") != 0)
57 return 0;
58
59 /*
60 * Check if Firmware Assisted dump is supported. if yes, check
61 * if dump has been initiated on last reboot.
62 */
63 token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
64 if (!token)
65 return 0;
66
67 fw_dump.fadump_supported = 1;
68 fw_dump.ibm_configure_kernel_dump = *token;
69
70 /*
71 * The 'ibm,kernel-dump' rtas node is present only if there is
72 * dump data waiting for us.
73 */
3ccc00a7
MS
74 fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
75 if (fdm_active)
eb39c880
MS
76 fw_dump.dump_active = 1;
77
78 /* Get the sizes required to store dump data for the firmware provided
79 * dump sections.
80 * For each dump section type supported, a 32bit cell which defines
81 * the ID of a supported section followed by two 32 bit cells which
82 * gives teh size of the section in bytes.
83 */
84 sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
85 &size);
86
87 if (!sections)
88 return 0;
89
90 num_sections = size / (3 * sizeof(u32));
91
92 for (i = 0; i < num_sections; i++, sections += 3) {
93 u32 type = (u32)of_read_number(sections, 1);
94
95 switch (type) {
96 case FADUMP_CPU_STATE_DATA:
97 fw_dump.cpu_state_data_size =
98 of_read_ulong(&sections[1], 2);
99 break;
100 case FADUMP_HPTE_REGION:
101 fw_dump.hpte_region_size =
102 of_read_ulong(&sections[1], 2);
103 break;
104 }
105 }
106 return 1;
107}
108
3ccc00a7
MS
109int is_fadump_active(void)
110{
111 return fw_dump.dump_active;
112}
113
114/* Print firmware assisted dump configurations for debugging purpose. */
115static void fadump_show_config(void)
116{
117 pr_debug("Support for firmware-assisted dump (fadump): %s\n",
118 (fw_dump.fadump_supported ? "present" : "no support"));
119
120 if (!fw_dump.fadump_supported)
121 return;
122
123 pr_debug("Fadump enabled : %s\n",
124 (fw_dump.fadump_enabled ? "yes" : "no"));
125 pr_debug("Dump Active : %s\n",
126 (fw_dump.dump_active ? "yes" : "no"));
127 pr_debug("Dump section sizes:\n");
128 pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
129 pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
130 pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
131}
132
133static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
134 unsigned long addr)
135{
136 if (!fdm)
137 return 0;
138
139 memset(fdm, 0, sizeof(struct fadump_mem_struct));
140 addr = addr & PAGE_MASK;
141
142 fdm->header.dump_format_version = 0x00000001;
143 fdm->header.dump_num_sections = 3;
144 fdm->header.dump_status_flag = 0;
145 fdm->header.offset_first_dump_section =
146 (u32)offsetof(struct fadump_mem_struct, cpu_state_data);
147
148 /*
149 * Fields for disk dump option.
150 * We are not using disk dump option, hence set these fields to 0.
151 */
152 fdm->header.dd_block_size = 0;
153 fdm->header.dd_block_offset = 0;
154 fdm->header.dd_num_blocks = 0;
155 fdm->header.dd_offset_disk_path = 0;
156
157 /* set 0 to disable an automatic dump-reboot. */
158 fdm->header.max_time_auto = 0;
159
160 /* Kernel dump sections */
161 /* cpu state data section. */
162 fdm->cpu_state_data.request_flag = FADUMP_REQUEST_FLAG;
163 fdm->cpu_state_data.source_data_type = FADUMP_CPU_STATE_DATA;
164 fdm->cpu_state_data.source_address = 0;
165 fdm->cpu_state_data.source_len = fw_dump.cpu_state_data_size;
166 fdm->cpu_state_data.destination_address = addr;
167 addr += fw_dump.cpu_state_data_size;
168
169 /* hpte region section */
170 fdm->hpte_region.request_flag = FADUMP_REQUEST_FLAG;
171 fdm->hpte_region.source_data_type = FADUMP_HPTE_REGION;
172 fdm->hpte_region.source_address = 0;
173 fdm->hpte_region.source_len = fw_dump.hpte_region_size;
174 fdm->hpte_region.destination_address = addr;
175 addr += fw_dump.hpte_region_size;
176
177 /* RMA region section */
178 fdm->rmr_region.request_flag = FADUMP_REQUEST_FLAG;
179 fdm->rmr_region.source_data_type = FADUMP_REAL_MODE_REGION;
180 fdm->rmr_region.source_address = RMA_START;
181 fdm->rmr_region.source_len = fw_dump.boot_memory_size;
182 fdm->rmr_region.destination_address = addr;
183 addr += fw_dump.boot_memory_size;
184
185 return addr;
186}
187
eb39c880
MS
188/**
189 * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
190 *
191 * Function to find the largest memory size we need to reserve during early
192 * boot process. This will be the size of the memory that is required for a
193 * kernel to boot successfully.
194 *
195 * This function has been taken from phyp-assisted dump feature implementation.
196 *
197 * returns larger of 256MB or 5% rounded down to multiples of 256MB.
198 *
199 * TODO: Come up with better approach to find out more accurate memory size
200 * that is required for a kernel to boot successfully.
201 *
202 */
203static inline unsigned long fadump_calculate_reserve_size(void)
204{
205 unsigned long size;
206
207 /*
208 * Check if the size is specified through fadump_reserve_mem= cmdline
209 * option. If yes, then use that.
210 */
211 if (fw_dump.reserve_bootvar)
212 return fw_dump.reserve_bootvar;
213
214 /* divide by 20 to get 5% of value */
215 size = memblock_end_of_DRAM() / 20;
216
217 /* round it down in multiples of 256 */
218 size = size & ~0x0FFFFFFFUL;
219
220 /* Truncate to memory_limit. We don't want to over reserve the memory.*/
221 if (memory_limit && size > memory_limit)
222 size = memory_limit;
223
224 return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
225}
226
227/*
228 * Calculate the total memory size required to be reserved for
229 * firmware-assisted dump registration.
230 */
231static unsigned long get_fadump_area_size(void)
232{
233 unsigned long size = 0;
234
235 size += fw_dump.cpu_state_data_size;
236 size += fw_dump.hpte_region_size;
237 size += fw_dump.boot_memory_size;
238
239 size = PAGE_ALIGN(size);
240 return size;
241}
242
243int __init fadump_reserve_mem(void)
244{
245 unsigned long base, size, memory_boundary;
246
247 if (!fw_dump.fadump_enabled)
248 return 0;
249
250 if (!fw_dump.fadump_supported) {
251 printk(KERN_INFO "Firmware-assisted dump is not supported on"
252 " this hardware\n");
253 fw_dump.fadump_enabled = 0;
254 return 0;
255 }
3ccc00a7
MS
256 /*
257 * Initialize boot memory size
258 * If dump is active then we have already calculated the size during
259 * first kernel.
260 */
261 if (fdm_active)
262 fw_dump.boot_memory_size = fdm_active->rmr_region.source_len;
263 else
264 fw_dump.boot_memory_size = fadump_calculate_reserve_size();
eb39c880
MS
265
266 /*
267 * Calculate the memory boundary.
268 * If memory_limit is less than actual memory boundary then reserve
269 * the memory for fadump beyond the memory_limit and adjust the
270 * memory_limit accordingly, so that the running kernel can run with
271 * specified memory_limit.
272 */
273 if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
274 size = get_fadump_area_size();
275 if ((memory_limit + size) < memblock_end_of_DRAM())
276 memory_limit += size;
277 else
278 memory_limit = memblock_end_of_DRAM();
279 printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
280 " dump, now %#016llx\n",
281 (unsigned long long)memory_limit);
282 }
283 if (memory_limit)
284 memory_boundary = memory_limit;
285 else
286 memory_boundary = memblock_end_of_DRAM();
287
288 if (fw_dump.dump_active) {
289 printk(KERN_INFO "Firmware-assisted dump is active.\n");
290 /*
291 * If last boot has crashed then reserve all the memory
292 * above boot_memory_size so that we don't touch it until
293 * dump is written to disk by userspace tool. This memory
294 * will be released for general use once the dump is saved.
295 */
296 base = fw_dump.boot_memory_size;
297 size = memory_boundary - base;
298 memblock_reserve(base, size);
299 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
300 "for saving crash dump\n",
301 (unsigned long)(size >> 20),
302 (unsigned long)(base >> 20));
303 } else {
304 /* Reserve the memory at the top of memory. */
305 size = get_fadump_area_size();
306 base = memory_boundary - size;
307 memblock_reserve(base, size);
308 printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
309 "for firmware-assisted dump\n",
310 (unsigned long)(size >> 20),
311 (unsigned long)(base >> 20));
312 }
313 fw_dump.reserve_dump_area_start = base;
314 fw_dump.reserve_dump_area_size = size;
315 return 1;
316}
317
318/* Look for fadump= cmdline option. */
319static int __init early_fadump_param(char *p)
320{
321 if (!p)
322 return 1;
323
324 if (strncmp(p, "on", 2) == 0)
325 fw_dump.fadump_enabled = 1;
326 else if (strncmp(p, "off", 3) == 0)
327 fw_dump.fadump_enabled = 0;
328
329 return 0;
330}
331early_param("fadump", early_fadump_param);
332
333/* Look for fadump_reserve_mem= cmdline option */
334static int __init early_fadump_reserve_mem(char *p)
335{
336 if (p)
337 fw_dump.reserve_bootvar = memparse(p, &p);
338 return 0;
339}
340early_param("fadump_reserve_mem", early_fadump_reserve_mem);
3ccc00a7
MS
341
342static void register_fw_dump(struct fadump_mem_struct *fdm)
343{
344 int rc;
345 unsigned int wait_time;
346
347 pr_debug("Registering for firmware-assisted kernel dump...\n");
348
349 /* TODO: Add upper time limit for the delay */
350 do {
351 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
352 FADUMP_REGISTER, fdm,
353 sizeof(struct fadump_mem_struct));
354
355 wait_time = rtas_busy_delay_time(rc);
356 if (wait_time)
357 mdelay(wait_time);
358
359 } while (wait_time);
360
361 switch (rc) {
362 case -1:
363 printk(KERN_ERR "Failed to register firmware-assisted kernel"
364 " dump. Hardware Error(%d).\n", rc);
365 break;
366 case -3:
367 printk(KERN_ERR "Failed to register firmware-assisted kernel"
368 " dump. Parameter Error(%d).\n", rc);
369 break;
370 case -9:
371 printk(KERN_ERR "firmware-assisted kernel dump is already "
372 " registered.");
373 fw_dump.dump_registered = 1;
374 break;
375 case 0:
376 printk(KERN_INFO "firmware-assisted kernel dump registration"
377 " is successful\n");
378 fw_dump.dump_registered = 1;
379 break;
380 }
381}
382
383static void register_fadump(void)
384{
385 /*
386 * If no memory is reserved then we can not register for firmware-
387 * assisted dump.
388 */
389 if (!fw_dump.reserve_dump_area_size)
390 return;
391
392 /* register the future kernel dump with firmware. */
393 register_fw_dump(&fdm);
394}
395
396static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
397{
398 int rc = 0;
399 unsigned int wait_time;
400
401 pr_debug("Un-register firmware-assisted dump\n");
402
403 /* TODO: Add upper time limit for the delay */
404 do {
405 rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
406 FADUMP_UNREGISTER, fdm,
407 sizeof(struct fadump_mem_struct));
408
409 wait_time = rtas_busy_delay_time(rc);
410 if (wait_time)
411 mdelay(wait_time);
412 } while (wait_time);
413
414 if (rc) {
415 printk(KERN_ERR "Failed to un-register firmware-assisted dump."
416 " unexpected error(%d).\n", rc);
417 return rc;
418 }
419 fw_dump.dump_registered = 0;
420 return 0;
421}
422
423static ssize_t fadump_enabled_show(struct kobject *kobj,
424 struct kobj_attribute *attr,
425 char *buf)
426{
427 return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
428}
429
430static ssize_t fadump_register_show(struct kobject *kobj,
431 struct kobj_attribute *attr,
432 char *buf)
433{
434 return sprintf(buf, "%d\n", fw_dump.dump_registered);
435}
436
437static ssize_t fadump_register_store(struct kobject *kobj,
438 struct kobj_attribute *attr,
439 const char *buf, size_t count)
440{
441 int ret = 0;
442
443 if (!fw_dump.fadump_enabled || fdm_active)
444 return -EPERM;
445
446 mutex_lock(&fadump_mutex);
447
448 switch (buf[0]) {
449 case '0':
450 if (fw_dump.dump_registered == 0) {
451 ret = -EINVAL;
452 goto unlock_out;
453 }
454 /* Un-register Firmware-assisted dump */
455 fadump_unregister_dump(&fdm);
456 break;
457 case '1':
458 if (fw_dump.dump_registered == 1) {
459 ret = -EINVAL;
460 goto unlock_out;
461 }
462 /* Register Firmware-assisted dump */
463 register_fadump();
464 break;
465 default:
466 ret = -EINVAL;
467 break;
468 }
469
470unlock_out:
471 mutex_unlock(&fadump_mutex);
472 return ret < 0 ? ret : count;
473}
474
475static int fadump_region_show(struct seq_file *m, void *private)
476{
477 const struct fadump_mem_struct *fdm_ptr;
478
479 if (!fw_dump.fadump_enabled)
480 return 0;
481
482 if (fdm_active)
483 fdm_ptr = fdm_active;
484 else
485 fdm_ptr = &fdm;
486
487 seq_printf(m,
488 "CPU : [%#016llx-%#016llx] %#llx bytes, "
489 "Dumped: %#llx\n",
490 fdm_ptr->cpu_state_data.destination_address,
491 fdm_ptr->cpu_state_data.destination_address +
492 fdm_ptr->cpu_state_data.source_len - 1,
493 fdm_ptr->cpu_state_data.source_len,
494 fdm_ptr->cpu_state_data.bytes_dumped);
495 seq_printf(m,
496 "HPTE: [%#016llx-%#016llx] %#llx bytes, "
497 "Dumped: %#llx\n",
498 fdm_ptr->hpte_region.destination_address,
499 fdm_ptr->hpte_region.destination_address +
500 fdm_ptr->hpte_region.source_len - 1,
501 fdm_ptr->hpte_region.source_len,
502 fdm_ptr->hpte_region.bytes_dumped);
503 seq_printf(m,
504 "DUMP: [%#016llx-%#016llx] %#llx bytes, "
505 "Dumped: %#llx\n",
506 fdm_ptr->rmr_region.destination_address,
507 fdm_ptr->rmr_region.destination_address +
508 fdm_ptr->rmr_region.source_len - 1,
509 fdm_ptr->rmr_region.source_len,
510 fdm_ptr->rmr_region.bytes_dumped);
511
512 if (!fdm_active ||
513 (fw_dump.reserve_dump_area_start ==
514 fdm_ptr->cpu_state_data.destination_address))
515 return 0;
516
517 /* Dump is active. Show reserved memory region. */
518 seq_printf(m,
519 " : [%#016llx-%#016llx] %#llx bytes, "
520 "Dumped: %#llx\n",
521 (unsigned long long)fw_dump.reserve_dump_area_start,
522 fdm_ptr->cpu_state_data.destination_address - 1,
523 fdm_ptr->cpu_state_data.destination_address -
524 fw_dump.reserve_dump_area_start,
525 fdm_ptr->cpu_state_data.destination_address -
526 fw_dump.reserve_dump_area_start);
527 return 0;
528}
529
530static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
531 0444, fadump_enabled_show,
532 NULL);
533static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
534 0644, fadump_register_show,
535 fadump_register_store);
536
537static int fadump_region_open(struct inode *inode, struct file *file)
538{
539 return single_open(file, fadump_region_show, inode->i_private);
540}
541
542static const struct file_operations fadump_region_fops = {
543 .open = fadump_region_open,
544 .read = seq_read,
545 .llseek = seq_lseek,
546 .release = single_release,
547};
548
549static void fadump_init_files(void)
550{
551 struct dentry *debugfs_file;
552 int rc = 0;
553
554 rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
555 if (rc)
556 printk(KERN_ERR "fadump: unable to create sysfs file"
557 " fadump_enabled (%d)\n", rc);
558
559 rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
560 if (rc)
561 printk(KERN_ERR "fadump: unable to create sysfs file"
562 " fadump_registered (%d)\n", rc);
563
564 debugfs_file = debugfs_create_file("fadump_region", 0444,
565 powerpc_debugfs_root, NULL,
566 &fadump_region_fops);
567 if (!debugfs_file)
568 printk(KERN_ERR "fadump: unable to create debugfs file"
569 " fadump_region\n");
570 return;
571}
572
573/*
574 * Prepare for firmware-assisted dump.
575 */
576int __init setup_fadump(void)
577{
578 if (!fw_dump.fadump_enabled)
579 return 0;
580
581 if (!fw_dump.fadump_supported) {
582 printk(KERN_ERR "Firmware-assisted dump is not supported on"
583 " this hardware\n");
584 return 0;
585 }
586
587 fadump_show_config();
588 /* Initialize the kernel dump memory structure for FAD registration. */
589 if (fw_dump.reserve_dump_area_size)
590 init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
591 fadump_init_files();
592
593 return 1;
594}
595subsys_initcall(setup_fadump);