xref: /linux/include/linux/pstore.h (revision fbccdeb8d77d6830556bc4079eeed80298cc97dc)
1ca01d6ddSTony Luck /*
2ca01d6ddSTony Luck  * Persistent Storage - pstore.h
3ca01d6ddSTony Luck  *
4ca01d6ddSTony Luck  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
5ca01d6ddSTony Luck  *
6ca01d6ddSTony Luck  * This code is the generic layer to export data records from platform
7ca01d6ddSTony Luck  * level persistent storage via a file system.
8ca01d6ddSTony Luck  *
9ca01d6ddSTony Luck  *  This program is free software; you can redistribute it and/or modify
10ca01d6ddSTony Luck  *  it under the terms of the GNU General Public License version 2 as
11ca01d6ddSTony Luck  *  published by the Free Software Foundation.
12ca01d6ddSTony Luck  *
13ca01d6ddSTony Luck  *  This program is distributed in the hope that it will be useful,
14ca01d6ddSTony Luck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15ca01d6ddSTony Luck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16ca01d6ddSTony Luck  *  GNU General Public License for more details.
17ca01d6ddSTony Luck  *
18ca01d6ddSTony Luck  *  You should have received a copy of the GNU General Public License
19ca01d6ddSTony Luck  *  along with this program; if not, write to the Free Software
20ca01d6ddSTony Luck  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21ca01d6ddSTony Luck  */
22ca01d6ddSTony Luck #ifndef _LINUX_PSTORE_H
23ca01d6ddSTony Luck #define _LINUX_PSTORE_H
24ca01d6ddSTony Luck 
255bf6d1b9SMark Salyzyn #include <linux/compiler.h>
265bf6d1b9SMark Salyzyn #include <linux/errno.h>
273d6d8d20SKees Cook #include <linux/kmsg_dump.h>
2867a101f5SAnton Vorontsov #include <linux/mutex.h>
2967a101f5SAnton Vorontsov #include <linux/spinlock.h>
305bf6d1b9SMark Salyzyn #include <linux/time.h>
315bf6d1b9SMark Salyzyn #include <linux/types.h>
323d6d8d20SKees Cook 
33ca01d6ddSTony Luck /* types */
34ca01d6ddSTony Luck enum pstore_type_id {
35ca01d6ddSTony Luck 	PSTORE_TYPE_DMESG	= 0,
36ca01d6ddSTony Luck 	PSTORE_TYPE_MCE		= 1,
37f29e5956SAnton Vorontsov 	PSTORE_TYPE_CONSOLE	= 2,
38060287b8SAnton Vorontsov 	PSTORE_TYPE_FTRACE	= 3,
3969020eeaSAruna Balakrishnaiah 	/* PPC64 partition types */
4069020eeaSAruna Balakrishnaiah 	PSTORE_TYPE_PPC_RTAS	= 4,
41f33f748cSAruna Balakrishnaiah 	PSTORE_TYPE_PPC_OF	= 5,
42a5e4797bSAruna Balakrishnaiah 	PSTORE_TYPE_PPC_COMMON	= 6,
439d5438f4SMark Salyzyn 	PSTORE_TYPE_PMSG	= 7,
44ae011d2eSHari Bathini 	PSTORE_TYPE_PPC_OPAL	= 8,
45ca01d6ddSTony Luck 	PSTORE_TYPE_UNKNOWN	= 255
46ca01d6ddSTony Luck };
47ca01d6ddSTony Luck 
4867a101f5SAnton Vorontsov struct module;
4967a101f5SAnton Vorontsov 
50ca01d6ddSTony Luck struct pstore_info {
51ca01d6ddSTony Luck 	struct module	*owner;
52ca01d6ddSTony Luck 	char		*name;
53abd4d558SDon Zickus 	spinlock_t	buf_lock;	/* serialize access to 'buf' */
54ca01d6ddSTony Luck 	char		*buf;
55ca01d6ddSTony Luck 	size_t		bufsize;
56f6f82851SKees Cook 	struct mutex	read_mutex;	/* serialize open/read/close */
57df36ac1bSLuck, Tony 	int		flags;
5806cf91b4SChen Gong 	int		(*open)(struct pstore_info *psi);
5906cf91b4SChen Gong 	int		(*close)(struct pstore_info *psi);
608d38d74bSChen Gong 	ssize_t		(*read)(u64 *id, enum pstore_type_id *type,
61755d4fe4SSeiji Aguchi 			int *count, struct timespec *time, char **buf,
628cfc8ddcSGeliang Tang 			bool *compressed, ssize_t *ecc_notice_size,
638cfc8ddcSGeliang Tang 			struct pstore_info *psi);
643d6d8d20SKees Cook 	int		(*write)(enum pstore_type_id type,
653d6d8d20SKees Cook 			enum kmsg_dump_reason reason, u64 *id,
66b3b515bbSAruna Balakrishnaiah 			unsigned int part, int count, bool compressed,
676bbbca73SAruna Balakrishnaiah 			size_t size, struct pstore_info *psi);
68897dba02SAnton Vorontsov 	int		(*write_buf)(enum pstore_type_id type,
69897dba02SAnton Vorontsov 			enum kmsg_dump_reason reason, u64 *id,
70b3b515bbSAruna Balakrishnaiah 			unsigned int part, const char *buf, bool compressed,
716bbbca73SAruna Balakrishnaiah 			size_t size, struct pstore_info *psi);
725bf6d1b9SMark Salyzyn 	int		(*write_buf_user)(enum pstore_type_id type,
735bf6d1b9SMark Salyzyn 			enum kmsg_dump_reason reason, u64 *id,
745bf6d1b9SMark Salyzyn 			unsigned int part, const char __user *buf,
755bf6d1b9SMark Salyzyn 			bool compressed, size_t size, struct pstore_info *psi);
7656280682SMatthew Garrett 	int		(*erase)(enum pstore_type_id type, u64 id,
77755d4fe4SSeiji Aguchi 			int count, struct timespec time,
78755d4fe4SSeiji Aguchi 			struct pstore_info *psi);
79638c1fd3SMatthew Garrett 	void		*data;
80ca01d6ddSTony Luck };
81ca01d6ddSTony Luck 
82c950fd6fSNamhyung Kim #define PSTORE_FLAGS_DMESG	(1 << 0)
83c950fd6fSNamhyung Kim #define PSTORE_FLAGS_FRAGILE	PSTORE_FLAGS_DMESG
84c950fd6fSNamhyung Kim #define PSTORE_FLAGS_CONSOLE	(1 << 1)
85c950fd6fSNamhyung Kim #define PSTORE_FLAGS_FTRACE	(1 << 2)
86c950fd6fSNamhyung Kim #define PSTORE_FLAGS_PMSG	(1 << 3)
87c950fd6fSNamhyung Kim 
88ca01d6ddSTony Luck extern int pstore_register(struct pstore_info *);
89ee1d2674SGeliang Tang extern void pstore_unregister(struct pstore_info *);
909f244e9cSSeiji Aguchi extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason);
91ca01d6ddSTony Luck 
92*fbccdeb8SJoel Fernandes struct pstore_ftrace_record {
93*fbccdeb8SJoel Fernandes 	unsigned long ip;
94*fbccdeb8SJoel Fernandes 	unsigned long parent_ip;
95*fbccdeb8SJoel Fernandes 	u64 ts;
96*fbccdeb8SJoel Fernandes };
97*fbccdeb8SJoel Fernandes 
98*fbccdeb8SJoel Fernandes /*
99*fbccdeb8SJoel Fernandes  * ftrace related stuff: Both backends and frontends need these so expose
100*fbccdeb8SJoel Fernandes  * them here.
101*fbccdeb8SJoel Fernandes  */
102*fbccdeb8SJoel Fernandes 
103*fbccdeb8SJoel Fernandes #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB)
104*fbccdeb8SJoel Fernandes #define PSTORE_CPU_IN_IP 0x1
105*fbccdeb8SJoel Fernandes #elif NR_CPUS <= 4 && defined(CONFIG_ARM)
106*fbccdeb8SJoel Fernandes #define PSTORE_CPU_IN_IP 0x3
107*fbccdeb8SJoel Fernandes #endif
108*fbccdeb8SJoel Fernandes 
109*fbccdeb8SJoel Fernandes #define TS_CPU_SHIFT 8
110*fbccdeb8SJoel Fernandes #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1)
111*fbccdeb8SJoel Fernandes 
112*fbccdeb8SJoel Fernandes /*
113*fbccdeb8SJoel Fernandes  * If CPU number can be stored in IP, store it there, otherwise store it in
114*fbccdeb8SJoel Fernandes  * the time stamp. This means more timestamp resolution is available when
115*fbccdeb8SJoel Fernandes  * the CPU can be stored in the IP.
116*fbccdeb8SJoel Fernandes  */
117*fbccdeb8SJoel Fernandes #ifdef PSTORE_CPU_IN_IP
118*fbccdeb8SJoel Fernandes static inline void
119*fbccdeb8SJoel Fernandes pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
120*fbccdeb8SJoel Fernandes {
121*fbccdeb8SJoel Fernandes 	rec->ip |= cpu;
122*fbccdeb8SJoel Fernandes }
123*fbccdeb8SJoel Fernandes 
124*fbccdeb8SJoel Fernandes static inline unsigned int
125*fbccdeb8SJoel Fernandes pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
126*fbccdeb8SJoel Fernandes {
127*fbccdeb8SJoel Fernandes 	return rec->ip & PSTORE_CPU_IN_IP;
128*fbccdeb8SJoel Fernandes }
129*fbccdeb8SJoel Fernandes 
130*fbccdeb8SJoel Fernandes static inline u64
131*fbccdeb8SJoel Fernandes pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
132*fbccdeb8SJoel Fernandes {
133*fbccdeb8SJoel Fernandes 	return rec->ts;
134*fbccdeb8SJoel Fernandes }
135*fbccdeb8SJoel Fernandes 
136*fbccdeb8SJoel Fernandes static inline void
137*fbccdeb8SJoel Fernandes pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
138*fbccdeb8SJoel Fernandes {
139*fbccdeb8SJoel Fernandes 	rec->ts = val;
140*fbccdeb8SJoel Fernandes }
141*fbccdeb8SJoel Fernandes #else
142*fbccdeb8SJoel Fernandes static inline void
143*fbccdeb8SJoel Fernandes pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
144*fbccdeb8SJoel Fernandes {
145*fbccdeb8SJoel Fernandes 	rec->ts &= ~(TS_CPU_MASK);
146*fbccdeb8SJoel Fernandes 	rec->ts |= cpu;
147*fbccdeb8SJoel Fernandes }
148*fbccdeb8SJoel Fernandes 
149*fbccdeb8SJoel Fernandes static inline unsigned int
150*fbccdeb8SJoel Fernandes pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
151*fbccdeb8SJoel Fernandes {
152*fbccdeb8SJoel Fernandes 	return rec->ts & TS_CPU_MASK;
153*fbccdeb8SJoel Fernandes }
154*fbccdeb8SJoel Fernandes 
155*fbccdeb8SJoel Fernandes static inline u64
156*fbccdeb8SJoel Fernandes pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
157*fbccdeb8SJoel Fernandes {
158*fbccdeb8SJoel Fernandes 	return rec->ts >> TS_CPU_SHIFT;
159*fbccdeb8SJoel Fernandes }
160*fbccdeb8SJoel Fernandes 
161*fbccdeb8SJoel Fernandes static inline void
162*fbccdeb8SJoel Fernandes pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
163*fbccdeb8SJoel Fernandes {
164*fbccdeb8SJoel Fernandes 	rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT);
165*fbccdeb8SJoel Fernandes }
166*fbccdeb8SJoel Fernandes #endif
167*fbccdeb8SJoel Fernandes 
168ca01d6ddSTony Luck #endif /*_LINUX_PSTORE_H*/
169