]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/powerpc/platforms/pseries/phyp_dump.c
[POWERPC] pseries: phyp dump: Disable phyp-dump through boot-var
[mirror_ubuntu-jammy-kernel.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 #define DUMP_ERROR_FLAG 0x2000
74 #define DUMP_TRIGGERED 0x4000
75 #define DUMP_PERFORMED 0x8000
76
77
78 /**
79 * init_dump_header() - initialize the header declaring a dump
80 * Returns: length of dump save area.
81 *
82 * When the hypervisor saves crashed state, it needs to put
83 * it somewhere. The dump header tells the hypervisor where
84 * the data can be saved.
85 */
86 static unsigned long init_dump_header(struct phyp_dump_header *ph)
87 {
88 unsigned long addr_offset = 0;
89
90 /* Set up the dump header */
91 ph->version = DUMP_HEADER_VERSION;
92 ph->num_of_sections = NUM_DUMP_SECTIONS;
93 ph->status = 0;
94
95 ph->first_offset_section =
96 (u32)offsetof(struct phyp_dump_header, cpu_data);
97 ph->dump_disk_section = 0;
98 ph->block_num_dd = 0;
99 ph->num_of_blocks_dd = 0;
100 ph->offset_dd = 0;
101
102 ph->maxtime_to_auto = 0; /* disabled */
103
104 /* The first two sections are mandatory */
105 ph->cpu_data.dump_flags = DUMP_REQUEST_FLAG;
106 ph->cpu_data.source_type = DUMP_SOURCE_CPU;
107 ph->cpu_data.source_address = 0;
108 ph->cpu_data.source_length = phyp_dump_info->cpu_state_size;
109 ph->cpu_data.destination_address = addr_offset;
110 addr_offset += phyp_dump_info->cpu_state_size;
111
112 ph->hpte_data.dump_flags = DUMP_REQUEST_FLAG;
113 ph->hpte_data.source_type = DUMP_SOURCE_HPTE;
114 ph->hpte_data.source_address = 0;
115 ph->hpte_data.source_length = phyp_dump_info->hpte_region_size;
116 ph->hpte_data.destination_address = addr_offset;
117 addr_offset += phyp_dump_info->hpte_region_size;
118
119 /* This section describes the low kernel region */
120 ph->kernel_data.dump_flags = DUMP_REQUEST_FLAG;
121 ph->kernel_data.source_type = DUMP_SOURCE_RMO;
122 ph->kernel_data.source_address = PHYP_DUMP_RMR_START;
123 ph->kernel_data.source_length = PHYP_DUMP_RMR_END;
124 ph->kernel_data.destination_address = addr_offset;
125 addr_offset += ph->kernel_data.source_length;
126
127 return addr_offset;
128 }
129
130 static void print_dump_header(const struct phyp_dump_header *ph)
131 {
132 #ifdef DEBUG
133 printk(KERN_INFO "dump header:\n");
134 /* setup some ph->sections required */
135 printk(KERN_INFO "version = %d\n", ph->version);
136 printk(KERN_INFO "Sections = %d\n", ph->num_of_sections);
137 printk(KERN_INFO "Status = 0x%x\n", ph->status);
138
139 /* No ph->disk, so all should be set to 0 */
140 printk(KERN_INFO "Offset to first section 0x%x\n",
141 ph->first_offset_section);
142 printk(KERN_INFO "dump disk sections should be zero\n");
143 printk(KERN_INFO "dump disk section = %d\n", ph->dump_disk_section);
144 printk(KERN_INFO "block num = %ld\n", ph->block_num_dd);
145 printk(KERN_INFO "number of blocks = %ld\n", ph->num_of_blocks_dd);
146 printk(KERN_INFO "dump disk offset = %d\n", ph->offset_dd);
147 printk(KERN_INFO "Max auto time= %d\n", ph->maxtime_to_auto);
148
149 /*set cpu state and hpte states as well scratch pad area */
150 printk(KERN_INFO " CPU AREA \n");
151 printk(KERN_INFO "cpu dump_flags =%d\n", ph->cpu_data.dump_flags);
152 printk(KERN_INFO "cpu source_type =%d\n", ph->cpu_data.source_type);
153 printk(KERN_INFO "cpu error_flags =%d\n", ph->cpu_data.error_flags);
154 printk(KERN_INFO "cpu source_address =%lx\n",
155 ph->cpu_data.source_address);
156 printk(KERN_INFO "cpu source_length =%lx\n",
157 ph->cpu_data.source_length);
158 printk(KERN_INFO "cpu length_copied =%lx\n",
159 ph->cpu_data.length_copied);
160
161 printk(KERN_INFO " HPTE AREA \n");
162 printk(KERN_INFO "HPTE dump_flags =%d\n", ph->hpte_data.dump_flags);
163 printk(KERN_INFO "HPTE source_type =%d\n", ph->hpte_data.source_type);
164 printk(KERN_INFO "HPTE error_flags =%d\n", ph->hpte_data.error_flags);
165 printk(KERN_INFO "HPTE source_address =%lx\n",
166 ph->hpte_data.source_address);
167 printk(KERN_INFO "HPTE source_length =%lx\n",
168 ph->hpte_data.source_length);
169 printk(KERN_INFO "HPTE length_copied =%lx\n",
170 ph->hpte_data.length_copied);
171
172 printk(KERN_INFO " SRSD AREA \n");
173 printk(KERN_INFO "SRSD dump_flags =%d\n", ph->kernel_data.dump_flags);
174 printk(KERN_INFO "SRSD source_type =%d\n", ph->kernel_data.source_type);
175 printk(KERN_INFO "SRSD error_flags =%d\n", ph->kernel_data.error_flags);
176 printk(KERN_INFO "SRSD source_address =%lx\n",
177 ph->kernel_data.source_address);
178 printk(KERN_INFO "SRSD source_length =%lx\n",
179 ph->kernel_data.source_length);
180 printk(KERN_INFO "SRSD length_copied =%lx\n",
181 ph->kernel_data.length_copied);
182 #endif
183 }
184
185 static void register_dump_area(struct phyp_dump_header *ph, unsigned long addr)
186 {
187 int rc;
188
189 /* Add addr value if not initialized before */
190 if (ph->cpu_data.destination_address == 0) {
191 ph->cpu_data.destination_address += addr;
192 ph->hpte_data.destination_address += addr;
193 ph->kernel_data.destination_address += addr;
194 }
195
196 /* ToDo Invalidate kdump and free memory range. */
197
198 do {
199 rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
200 1, ph, sizeof(struct phyp_dump_header));
201 } while (rtas_busy_delay(rc));
202
203 if (rc) {
204 printk(KERN_ERR "phyp-dump: unexpected error (%d) on "
205 "register\n", rc);
206 print_dump_header(ph);
207 }
208 }
209
210 static
211 void invalidate_last_dump(struct phyp_dump_header *ph, unsigned long addr)
212 {
213 int rc;
214
215 /* Add addr value if not initialized before */
216 if (ph->cpu_data.destination_address == 0) {
217 ph->cpu_data.destination_address += addr;
218 ph->hpte_data.destination_address += addr;
219 ph->kernel_data.destination_address += addr;
220 }
221
222 do {
223 rc = rtas_call(ibm_configure_kernel_dump, 3, 1, NULL,
224 2, ph, sizeof(struct phyp_dump_header));
225 } while (rtas_busy_delay(rc));
226
227 if (rc) {
228 printk(KERN_ERR "phyp-dump: unexpected error (%d) "
229 "on invalidate\n", rc);
230 print_dump_header(ph);
231 }
232 }
233
234 /* ------------------------------------------------- */
235 /**
236 * release_memory_range -- release memory previously lmb_reserved
237 * @start_pfn: starting physical frame number
238 * @nr_pages: number of pages to free.
239 *
240 * This routine will release memory that had been previously
241 * lmb_reserved in early boot. The released memory becomes
242 * available for genreal use.
243 */
244 static void release_memory_range(unsigned long start_pfn,
245 unsigned long nr_pages)
246 {
247 struct page *rpage;
248 unsigned long end_pfn;
249 long i;
250
251 end_pfn = start_pfn + nr_pages;
252
253 for (i = start_pfn; i <= end_pfn; i++) {
254 rpage = pfn_to_page(i);
255 if (PageReserved(rpage)) {
256 ClearPageReserved(rpage);
257 init_page_count(rpage);
258 __free_page(rpage);
259 totalram_pages++;
260 }
261 }
262 }
263
264 /**
265 * track_freed_range -- Counts the range being freed.
266 * Once the counter goes to zero, it re-registers dump for
267 * future use.
268 */
269 static void
270 track_freed_range(unsigned long addr, unsigned long length)
271 {
272 static unsigned long scratch_area_size, reserved_area_size;
273
274 if (addr < phyp_dump_info->init_reserve_start)
275 return;
276
277 if ((addr >= phyp_dump_info->init_reserve_start) &&
278 (addr <= phyp_dump_info->init_reserve_start +
279 phyp_dump_info->init_reserve_size))
280 reserved_area_size += length;
281
282 if ((addr >= phyp_dump_info->reserved_scratch_addr) &&
283 (addr <= phyp_dump_info->reserved_scratch_addr +
284 phyp_dump_info->reserved_scratch_size))
285 scratch_area_size += length;
286
287 if ((reserved_area_size == phyp_dump_info->init_reserve_size) &&
288 (scratch_area_size == phyp_dump_info->reserved_scratch_size)) {
289
290 invalidate_last_dump(&phdr,
291 phyp_dump_info->reserved_scratch_addr);
292 register_dump_area(&phdr,
293 phyp_dump_info->reserved_scratch_addr);
294 }
295 }
296
297 /* ------------------------------------------------- */
298 /**
299 * sysfs_release_region -- sysfs interface to release memory range.
300 *
301 * Usage:
302 * "echo <start addr> <length> > /sys/kernel/release_region"
303 *
304 * Example:
305 * "echo 0x40000000 0x10000000 > /sys/kernel/release_region"
306 *
307 * will release 256MB starting at 1GB.
308 */
309 static ssize_t store_release_region(struct kobject *kobj,
310 struct kobj_attribute *attr,
311 const char *buf, size_t count)
312 {
313 unsigned long start_addr, length, end_addr;
314 unsigned long start_pfn, nr_pages;
315 ssize_t ret;
316
317 ret = sscanf(buf, "%lx %lx", &start_addr, &length);
318 if (ret != 2)
319 return -EINVAL;
320
321 track_freed_range(start_addr, length);
322
323 /* Range-check - don't free any reserved memory that
324 * wasn't reserved for phyp-dump */
325 if (start_addr < phyp_dump_info->init_reserve_start)
326 start_addr = phyp_dump_info->init_reserve_start;
327
328 end_addr = phyp_dump_info->init_reserve_start +
329 phyp_dump_info->init_reserve_size;
330 if (start_addr+length > end_addr)
331 length = end_addr - start_addr;
332
333 /* Release the region of memory assed in by user */
334 start_pfn = PFN_DOWN(start_addr);
335 nr_pages = PFN_DOWN(length);
336 release_memory_range(start_pfn, nr_pages);
337
338 return count;
339 }
340
341 static ssize_t show_release_region(struct kobject *kobj,
342 struct kobj_attribute *attr, char *buf)
343 {
344 u64 second_addr_range;
345
346 /* total reserved size - start of scratch area */
347 second_addr_range = phyp_dump_info->init_reserve_size -
348 phyp_dump_info->reserved_scratch_size;
349 return sprintf(buf, "CPU:0x%lx-0x%lx: HPTE:0x%lx-0x%lx:"
350 " DUMP:0x%lx-0x%lx, 0x%lx-0x%lx:\n",
351 phdr.cpu_data.destination_address,
352 phdr.cpu_data.length_copied,
353 phdr.hpte_data.destination_address,
354 phdr.hpte_data.length_copied,
355 phdr.kernel_data.destination_address,
356 phdr.kernel_data.length_copied,
357 phyp_dump_info->init_reserve_start,
358 second_addr_range);
359 }
360
361 static struct kobj_attribute rr = __ATTR(release_region, 0600,
362 show_release_region,
363 store_release_region);
364
365 static int __init phyp_dump_setup(void)
366 {
367 struct device_node *rtas;
368 const struct phyp_dump_header *dump_header = NULL;
369 unsigned long dump_area_start;
370 unsigned long dump_area_length;
371 int header_len = 0;
372 int rc;
373
374 /* If no memory was reserved in early boot, there is nothing to do */
375 if (phyp_dump_info->init_reserve_size == 0)
376 return 0;
377
378 /* Return if phyp dump not supported */
379 if (!phyp_dump_info->phyp_dump_configured)
380 return -ENOSYS;
381
382 /* Is there dump data waiting for us? If there isn't,
383 * then register a new dump area, and release all of
384 * the rest of the reserved ram.
385 *
386 * The /rtas/ibm,kernel-dump rtas node is present only
387 * if there is dump data waiting for us.
388 */
389 rtas = of_find_node_by_path("/rtas");
390 if (rtas) {
391 dump_header = of_get_property(rtas, "ibm,kernel-dump",
392 &header_len);
393 of_node_put(rtas);
394 }
395
396 print_dump_header(dump_header);
397 dump_area_length = init_dump_header(&phdr);
398 /* align down */
399 dump_area_start = phyp_dump_info->init_reserve_start & PAGE_MASK;
400
401 if (dump_header == NULL) {
402 register_dump_area(&phdr, dump_area_start);
403 return 0;
404 }
405
406 /* re-register the dump area, if old dump was invalid */
407 if ((dump_header) && (dump_header->status & DUMP_ERROR_FLAG)) {
408 invalidate_last_dump(&phdr, dump_area_start);
409 register_dump_area(&phdr, dump_area_start);
410 return 0;
411 }
412
413 if (dump_header) {
414 phyp_dump_info->reserved_scratch_addr =
415 dump_header->cpu_data.destination_address;
416 phyp_dump_info->reserved_scratch_size =
417 dump_header->cpu_data.source_length +
418 dump_header->hpte_data.source_length +
419 dump_header->kernel_data.source_length;
420 }
421
422 /* Should we create a dump_subsys, analogous to s390/ipl.c ? */
423 rc = sysfs_create_file(kernel_kobj, &rr.attr);
424 if (rc)
425 printk(KERN_ERR "phyp-dump: unable to create sysfs file (%d)\n",
426 rc);
427
428 /* ToDo: re-register the dump area, for next time. */
429 return 0;
430 }
431 machine_subsys_initcall(pseries, phyp_dump_setup);
432
433 int __init early_init_dt_scan_phyp_dump(unsigned long node,
434 const char *uname, int depth, void *data)
435 {
436 const unsigned int *sizes;
437
438 phyp_dump_info->phyp_dump_configured = 0;
439 phyp_dump_info->phyp_dump_is_active = 0;
440
441 if (depth != 1 || strcmp(uname, "rtas") != 0)
442 return 0;
443
444 if (of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL))
445 phyp_dump_info->phyp_dump_configured++;
446
447 if (of_get_flat_dt_prop(node, "ibm,dump-kernel", NULL))
448 phyp_dump_info->phyp_dump_is_active++;
449
450 sizes = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
451 NULL);
452 if (!sizes)
453 return 0;
454
455 if (sizes[0] == 1)
456 phyp_dump_info->cpu_state_size = *((unsigned long *)&sizes[1]);
457
458 if (sizes[3] == 2)
459 phyp_dump_info->hpte_region_size =
460 *((unsigned long *)&sizes[4]);
461 return 1;
462 }
463
464 /* Look for phyp_dump= cmdline option */
465 static int __init early_phyp_dump_enabled(char *p)
466 {
467 phyp_dump_info->phyp_dump_at_boot = 1;
468
469 if (!p)
470 return 0;
471
472 if (strncmp(p, "1", 1) == 0)
473 phyp_dump_info->phyp_dump_at_boot = 1;
474 else if (strncmp(p, "0", 1) == 0)
475 phyp_dump_info->phyp_dump_at_boot = 0;
476
477 return 0;
478 }
479 early_param("phyp_dump", early_phyp_dump_enabled);
480