1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Firmware-Assisted Dump internal code.
4  *
5  * Copyright 2011, Mahesh Salgaonkar, IBM Corporation.
6  * Copyright 2019, Hari Bathini, IBM Corporation.
7  */
8 
9 #ifndef _ASM_POWERPC_FADUMP_INTERNAL_H
10 #define _ASM_POWERPC_FADUMP_INTERNAL_H
11 
12 /* Maximum number of memory regions kernel supports */
13 #define FADUMP_MAX_MEM_REGS			128
14 
15 #ifndef CONFIG_PRESERVE_FA_DUMP
16 
17 /* The upper limit percentage for user specified boot memory size (25%) */
18 #define MAX_BOOT_MEM_RATIO			4
19 
20 #define memblock_num_regions(memblock_type)	(memblock.memblock_type.cnt)
21 
22 /* FAD commands */
23 #define FADUMP_REGISTER			1
24 #define FADUMP_UNREGISTER		2
25 #define FADUMP_INVALIDATE		3
26 
27 /*
28  * Copy the ascii values for first 8 characters from a string into u64
29  * variable at their respective indexes.
30  * e.g.
31  *  The string "FADMPINF" will be converted into 0x4641444d50494e46
32  */
fadump_str_to_u64(const char * str)33 static inline u64 fadump_str_to_u64(const char *str)
34 {
35 	u64 val = 0;
36 	int i;
37 
38 	for (i = 0; i < sizeof(val); i++)
39 		val = (*str) ? (val << 8) | *str++ : val << 8;
40 	return val;
41 }
42 
43 #define FADUMP_CPU_UNKNOWN		(~((u32)0))
44 
45 /*
46  * The introduction of new fields in the fadump crash info header has
47  * led to a change in the magic key from `FADMPINF` to `FADMPSIG` for
48  * identifying a kernel crash from an old kernel.
49  *
50  * To prevent the need for further changes to the magic number in the
51  * event of future modifications to the fadump crash info header, a
52  * version field has been introduced to track the fadump crash info
53  * header version.
54  *
55  * Consider a few points before adding new members to the fadump crash info
56  * header structure:
57  *
58  *  - Append new members; avoid adding them in between.
59  *  - Non-primitive members should have a size member as well.
60  *  - For every change in the fadump header, increment the
61  *    fadump header version. This helps the updated kernel decide how to
62  *    handle kernel dumps from older kernels.
63  */
64 #define FADUMP_CRASH_INFO_MAGIC_OLD	fadump_str_to_u64("FADMPINF")
65 #define FADUMP_CRASH_INFO_MAGIC		fadump_str_to_u64("FADMPSIG")
66 #define FADUMP_HEADER_VERSION		1
67 
68 /* fadump crash info structure */
69 struct fadump_crash_info_header {
70 	u64		magic_number;
71 	u32		version;
72 	u32		crashing_cpu;
73 	u64		vmcoreinfo_raddr;
74 	u64		vmcoreinfo_size;
75 	u32		pt_regs_sz;
76 	u32		cpu_mask_sz;
77 	struct pt_regs	regs;
78 	struct cpumask	cpu_mask;
79 };
80 
81 struct fadump_memory_range {
82 	u64	base;
83 	u64	size;
84 };
85 
86 /* fadump memory ranges info */
87 #define RNG_NAME_SZ			16
88 struct fadump_mrange_info {
89 	char				name[RNG_NAME_SZ];
90 	struct fadump_memory_range	*mem_ranges;
91 	u32				mem_ranges_sz;
92 	u32				mem_range_cnt;
93 	u32				max_mem_ranges;
94 	bool				is_static;
95 };
96 
97 /* Platform specific callback functions */
98 struct fadump_ops;
99 
100 /* Firmware-assisted dump configuration details. */
101 struct fw_dump {
102 	unsigned long	reserve_dump_area_start;
103 	unsigned long	reserve_dump_area_size;
104 	/* cmd line option during boot */
105 	unsigned long	reserve_bootvar;
106 
107 	unsigned long	cpu_state_data_size;
108 	u64		cpu_state_dest_vaddr;
109 	u32		cpu_state_data_version;
110 	u32		cpu_state_entry_size;
111 
112 	unsigned long	hpte_region_size;
113 
114 	unsigned long	boot_memory_size;
115 	u64		boot_mem_dest_addr;
116 	u64		boot_mem_addr[FADUMP_MAX_MEM_REGS];
117 	u64		boot_mem_sz[FADUMP_MAX_MEM_REGS];
118 	u64		boot_mem_top;
119 	u64		boot_mem_regs_cnt;
120 
121 	unsigned long	fadumphdr_addr;
122 	u64		elfcorehdr_addr;
123 	u64		elfcorehdr_size;
124 	unsigned long	cpu_notes_buf_vaddr;
125 	unsigned long	cpu_notes_buf_size;
126 
127 	unsigned long	param_area;
128 
129 	/*
130 	 * Maximum size supported by firmware to copy from source to
131 	 * destination address per entry.
132 	 */
133 	u64		max_copy_size;
134 	u64		kernel_metadata;
135 
136 	int		ibm_configure_kernel_dump;
137 
138 	unsigned long	fadump_enabled:1;
139 	unsigned long	fadump_supported:1;
140 	unsigned long	dump_active:1;
141 	unsigned long	dump_registered:1;
142 	unsigned long	nocma:1;
143 	unsigned long	param_area_supported:1;
144 
145 	struct fadump_ops	*ops;
146 };
147 
148 struct fadump_ops {
149 	u64	(*fadump_init_mem_struct)(struct fw_dump *fadump_conf);
150 	u64	(*fadump_get_metadata_size)(void);
151 	int	(*fadump_setup_metadata)(struct fw_dump *fadump_conf);
152 	u64	(*fadump_get_bootmem_min)(void);
153 	int	(*fadump_register)(struct fw_dump *fadump_conf);
154 	int	(*fadump_unregister)(struct fw_dump *fadump_conf);
155 	int	(*fadump_invalidate)(struct fw_dump *fadump_conf);
156 	void	(*fadump_cleanup)(struct fw_dump *fadump_conf);
157 	int	(*fadump_process)(struct fw_dump *fadump_conf);
158 	void	(*fadump_region_show)(struct fw_dump *fadump_conf,
159 				      struct seq_file *m);
160 	void	(*fadump_trigger)(struct fadump_crash_info_header *fdh,
161 				  const char *msg);
162 	int	(*fadump_max_boot_mem_rgns)(void);
163 };
164 
165 /* Helper functions */
166 s32 __init fadump_setup_cpu_notes_buf(u32 num_cpus);
167 void fadump_free_cpu_notes_buf(void);
168 u32 *__init fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs);
169 void __init fadump_update_elfcore_header(char *bufp);
170 bool is_fadump_reserved_mem_contiguous(void);
171 
172 #else /* !CONFIG_PRESERVE_FA_DUMP */
173 
174 /* Firmware-assisted dump configuration details. */
175 struct fw_dump {
176 	u64	boot_mem_top;
177 	u64	dump_active;
178 };
179 
180 #endif /* CONFIG_PRESERVE_FA_DUMP */
181 
182 #ifdef CONFIG_PPC_PSERIES
183 extern void rtas_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node);
184 #else
185 static inline void
rtas_fadump_dt_scan(struct fw_dump * fadump_conf,u64 node)186 rtas_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node) { }
187 #endif
188 
189 #ifdef CONFIG_PPC_POWERNV
190 extern void opal_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node);
191 #else
192 static inline void
opal_fadump_dt_scan(struct fw_dump * fadump_conf,u64 node)193 opal_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node) { }
194 #endif
195 
196 #endif /* _ASM_POWERPC_FADUMP_INTERNAL_H */
197