]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/pseries/phyp_dump.c
[POWERPC] pseries: phyp dump: Debugging print routines
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / pseries / phyp_dump.c
1 /*
2  * Hypervisor-assisted dump
3  *
4  * Linas Vepstas, Manish Ahuja 2008
5  * Copyright 2008 IBM Corp.
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  */
13
14 #include <linux/init.h>
15 #include <linux/kobject.h>
16 #include <linux/mm.h>
17 #include <linux/of.h>
18 #include <linux/pfn.h>
19 #include <linux/swap.h>
20 #include <linux/sysfs.h>
21
22 #include <asm/page.h>
23 #include <asm/phyp_dump.h>
24 #include <asm/machdep.h>
25 #include <asm/prom.h>
26 #include <asm/rtas.h>
27
28 /* Variables, used to communicate data between early boot and late boot */
29 static struct phyp_dump phyp_dump_vars;
30 struct phyp_dump *phyp_dump_info = &phyp_dump_vars;
31
32 static int ibm_configure_kernel_dump;
33 /* ------------------------------------------------- */
34 /* RTAS interfaces to declare the dump regions */
35
36 struct dump_section {
37         u32 dump_flags;
38         u16 source_type;
39         u16 error_flags;
40         u64 source_address;
41         u64 source_length;
42         u64 length_copied;
43         u64 destination_address;
44 };
45
46 struct phyp_dump_header {
47         u32 version;
48         u16 num_of_sections;
49         u16 status;
50
51         u32 first_offset_section;
52         u32 dump_disk_section;
53         u64 block_num_dd;
54         u64 num_of_blocks_dd;
55         u32 offset_dd;
56         u32 maxtime_to_auto;
57         /* No dump disk path string used */
58
59         struct dump_section cpu_data;
60         struct dump_section hpte_data;
61         struct dump_section kernel_data;
62 };
63
64 /* The dump header *must be* in low memory, so .bss it */
65 static struct phyp_dump_header phdr;
66
67 #define NUM_DUMP_SECTIONS       3
68 #define DUMP_HEADER_VERSION     0x1
69 #define DUMP_REQUEST_FLAG       0x1
70 #define DUMP_SOURCE_CPU         0x0001
71 #define DUMP_SOURCE_HPTE        0x0002
72 #define DUMP_SOURCE_RMO         0x0011
73
74 /**
75  * init_dump_header() - initialize the header declaring a dump
76  * Returns: length of dump save area.
77  *
78  * When the hypervisor saves crashed state, it needs to put
79  * it somewhere. The dump header tells the hypervisor where
80  * the data can be saved.
81  */
82 static unsigned long init_dump_header(struct phyp_dump_header *ph)
83 {
84         unsigned long addr_offset = 0;
85
86         /* Set up the dump header */
87         ph->version = DUMP_HEADER_VERSION;
88         ph->num_of_sections = NUM_DUMP_SECTIONS;
89         ph->status = 0;
90
91         ph->first_offset_section =
92                 (u32)offsetof(struct phyp_dump_header, cpu_data);
93         ph->dump_disk_section = 0;
94         ph->block_num_dd = 0;
95         ph->num_of_blocks_dd = 0;
96         ph->offset_dd = 0;
97
98         ph->maxtime_to_auto = 0; /* disabled */
99
100         /* The first two sections are mandatory */
101         ph->cpu_data.dump_flags = DUMP_REQUEST_FLAG;
102         ph->cpu_data.source_type = DUMP_SOURCE_CPU;
103         ph->cpu_data.source_address = 0;
104         ph->cpu_data.source_length = phyp_dump_info->cpu_state_size;
105         ph->cpu_data.destination_address = addr_offset;
106         addr_offset += phyp_dump_info->cpu_state_size;
107
108         ph->hpte_data.dump_flags = DUMP_REQUEST_FLAG;
109         ph->hpte_data.source_type = DUMP_SOURCE_HPTE;
110         ph->hpte_data.source_address = 0;
111         ph->hpte_data.source_length = phyp_dump_info->hpte_region_size;
112         ph->hpte_data.destination_address = addr_offset;
113         addr_offset += phyp_dump_info->hpte_region_size;
114
115         /* This section describes the low kernel region */
116         ph->kernel_data.dump_flags = DUMP_REQUEST_FLAG;
117         ph->kernel_data.source_type = DUMP_SOURCE_RMO;
118         ph->kernel_data.source_address = PHYP_DUMP_RMR_START;
119         ph->kernel_data.source_length = PHYP_DUMP_RMR_END;
120         ph->kernel_data.destination_address = addr_offset;
121         addr_offset += ph->kernel_data.source_length;
122
123         return addr_offset;
124 }
125
126 static void print_dump_header(const struct phyp_dump_header *ph)
127 {
128 #ifdef DEBUG
129         printk(KERN_INFO "dump header:\n");
130         /* setup some ph->sections required */
131         printk(KERN_INFO "version = %d\n", ph->version);
132         printk(KERN_INFO "Sections = %d\n", ph->num_of_sections);
133         printk(KERN_INFO "Status = 0x%x\n", ph->status);
134
135         /* No ph->disk, so all should be set to 0 */
136         printk(KERN_INFO "Offset to first section 0x%x\n",
137                 ph->first_offset_section);
138         printk(KERN_INFO "dump disk sections should be zero\n");
139         printk(KERN_INFO "dump disk section = %d\n", ph->dump_disk_section);
140         printk(KERN_INFO "block num = %ld\n", ph->block_num_dd);
141         printk(KERN_INFO "number of blocks = %ld\n", ph->num_of_blocks_dd);
142         printk(KERN_INFO "dump disk offset = %d\n", ph->offset_dd);
143         printk(KERN_INFO "Max auto time= %d\n", ph->maxtime_to_auto);
144
145         /*set cpu state and hpte states as well scratch pad area */
146         printk(KERN_INFO " CPU AREA \n");
147         printk(KERN_INFO "cpu dump_flags =%d\n", ph->cpu_data.dump_flags);
148         printk(KERN_INFO "cpu source_type =%d\n", ph->cpu_data.source_type);
149         printk(KERN_INFO "cpu error_flags =%d\n", ph->cpu_data.error_flags);
150         printk(KERN_INFO "cpu source_address =%lx\n",
151                 ph->cpu_data.source_address);
152         printk(KERN_INFO "cpu source_length =%lx\n",
153                 ph->cpu_data.source_length);
154         printk(KERN_INFO "cpu length_copied =%lx\n",
155                 ph->cpu_data.length_copied);
156
157         printk(KERN_INFO " HPTE AREA \n");
158         printk(KERN_INFO "HPTE dump_flags =%d\n", ph->hpte_data.dump_flags);
159         printk(KERN_INFO "HPTE source_type =%d\n", ph->hpte_data.source_type);
160         printk(KERN_INFO "HPTE error_flags =%d\n", ph->hpte_data.error_flags);
161         printk(KERN_INFO "HPTE source_address =%lx\n",
162                 ph->hpte_data.source_address);
163         printk(KERN_INFO "HPTE source_length =%lx\n",
164                 ph->hpte_data.source_length);
165         printk(KERN_INFO "HPTE length_copied =%lx\n",
166                 ph->hpte_data.length_copied);
167
168         printk(KERN_INFO " SRSD AREA \n");
169         printk(KERN_INFO "SRSD dump_flags =%d\n", ph->kernel_data.dump_flags);
170         printk(KERN_INFO "SRSD source_type =%d\n", ph->kernel_data.source_type);
171         printk(KERN_INFO "SRSD error_flags =%d\n", ph->kernel_data.error_flags);
172         printk(KERN_INFO "SRSD source_address =%lx\n",
173                 ph->kernel_data.source_address);
174         printk(KERN_INFO "SRSD source_length =%lx\n",
175                 ph->kernel_data.source_length);
176         printk(KERN_INFO "SRSD length_copied =%lx\n",
177                 ph->kernel_data.length_copied);
178 #endif
179 }
180
181 static void register_dump_area(struct phyp_dump_header *ph, unsigned long addr)
182 {
183         int rc;
184         ph->cpu_data.destination_address += addr;
185         ph->hpte_data.destination_address += addr;
186         ph->kernel_data.destination_address += addr;
187
188         do {
189                 rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
190                                 1, ph, sizeof(struct phyp_dump_header));
191         } while (rtas_busy_delay(rc));
192
193         if (rc) {
194                 printk(KERN_ERR "phyp-dump: unexpected error (%d) on "
195                                                 "register\n", rc);
196                 print_dump_header(ph);
197         }
198 }
199
200 /* ------------------------------------------------- */
201 /**
202  * release_memory_range -- release memory previously lmb_reserved
203  * @start_pfn: starting physical frame number
204  * @nr_pages: number of pages to free.
205  *
206  * This routine will release memory that had been previously
207  * lmb_reserved in early boot. The released memory becomes
208  * available for genreal use.
209  */
210 static void
211 release_memory_range(unsigned long start_pfn, unsigned long nr_pages)
212 {
213         struct page *rpage;
214         unsigned long end_pfn;
215         long i;
216
217         end_pfn = start_pfn + nr_pages;
218
219         for (i = start_pfn; i <= end_pfn; i++) {
220                 rpage = pfn_to_page(i);
221                 if (PageReserved(rpage)) {
222                         ClearPageReserved(rpage);
223                         init_page_count(rpage);
224                         __free_page(rpage);
225                         totalram_pages++;
226                 }
227         }
228 }
229
230 /* ------------------------------------------------- */
231 /**
232  * sysfs_release_region -- sysfs interface to release memory range.
233  *
234  * Usage:
235  *   "echo <start addr> <length> > /sys/kernel/release_region"
236  *
237  * Example:
238  *   "echo 0x40000000 0x10000000 > /sys/kernel/release_region"
239  *
240  * will release 256MB starting at 1GB.
241  */
242 static ssize_t store_release_region(struct kobject *kobj,
243                                 struct kobj_attribute *attr,
244                                 const char *buf, size_t count)
245 {
246         unsigned long start_addr, length, end_addr;
247         unsigned long start_pfn, nr_pages;
248         ssize_t ret;
249
250         ret = sscanf(buf, "%lx %lx", &start_addr, &length);
251         if (ret != 2)
252                 return -EINVAL;
253
254         /* Range-check - don't free any reserved memory that
255          * wasn't reserved for phyp-dump */
256         if (start_addr < phyp_dump_info->init_reserve_start)
257                 start_addr = phyp_dump_info->init_reserve_start;
258
259         end_addr = phyp_dump_info->init_reserve_start +
260                         phyp_dump_info->init_reserve_size;
261         if (start_addr+length > end_addr)
262                 length = end_addr - start_addr;
263
264         /* Release the region of memory assed in by user */
265         start_pfn = PFN_DOWN(start_addr);
266         nr_pages = PFN_DOWN(length);
267         release_memory_range(start_pfn, nr_pages);
268
269         return count;
270 }
271
272 static struct kobj_attribute rr = __ATTR(release_region, 0600,
273                                          NULL, store_release_region);
274
275 static int __init phyp_dump_setup(void)
276 {
277         struct device_node *rtas;
278         const struct phyp_dump_header *dump_header = NULL;
279         unsigned long dump_area_start;
280         unsigned long dump_area_length;
281         int header_len = 0;
282         int rc;
283
284         /* If no memory was reserved in early boot, there is nothing to do */
285         if (phyp_dump_info->init_reserve_size == 0)
286                 return 0;
287
288         /* Return if phyp dump not supported */
289         if (!phyp_dump_info->phyp_dump_configured)
290                 return -ENOSYS;
291
292         /* Is there dump data waiting for us? If there isn't,
293          * then register a new dump area, and release all of
294          * the rest of the reserved ram.
295          *
296          * The /rtas/ibm,kernel-dump rtas node is present only
297          * if there is dump data waiting for us.
298          */
299         rtas = of_find_node_by_path("/rtas");
300         if (rtas) {
301                 dump_header = of_get_property(rtas, "ibm,kernel-dump",
302                                                 &header_len);
303                 of_node_put(rtas);
304         }
305
306         print_dump_header(dump_header);
307         dump_area_length = init_dump_header(&phdr);
308         /* align down */
309         dump_area_start = phyp_dump_info->init_reserve_start & PAGE_MASK;
310
311         if (dump_header == NULL) {
312                 register_dump_area(&phdr, dump_area_start);
313                 return 0;
314         }
315
316         /* Should we create a dump_subsys, analogous to s390/ipl.c ? */
317         rc = sysfs_create_file(kernel_kobj, &rr.attr);
318         if (rc)
319                 printk(KERN_ERR "phyp-dump: unable to create sysfs file (%d)\n",
320                                                                         rc);
321
322         /* ToDo: re-register the dump area, for next time. */
323         return 0;
324 }
325 machine_subsys_initcall(pseries, phyp_dump_setup);
326
327 int __init early_init_dt_scan_phyp_dump(unsigned long node,
328                 const char *uname, int depth, void *data)
329 {
330         const unsigned int *sizes;
331
332         phyp_dump_info->phyp_dump_configured = 0;
333         phyp_dump_info->phyp_dump_is_active = 0;
334
335         if (depth != 1 || strcmp(uname, "rtas") != 0)
336                 return 0;
337
338         if (of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL))
339                 phyp_dump_info->phyp_dump_configured++;
340
341         if (of_get_flat_dt_prop(node, "ibm,dump-kernel", NULL))
342                 phyp_dump_info->phyp_dump_is_active++;
343
344         sizes = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
345                                     NULL);
346         if (!sizes)
347                 return 0;
348
349         if (sizes[0] == 1)
350                 phyp_dump_info->cpu_state_size = *((unsigned long *)&sizes[1]);
351
352         if (sizes[3] == 2)
353                 phyp_dump_info->hpte_region_size =
354                                                 *((unsigned long *)&sizes[4]);
355         return 1;
356 }