xref: /openbmc/linux/include/linux/console.h (revision 7df45f35313c1ae083dac72c066b3aebfc7fc0cd)
1 /*
2  *  linux/include/linux/console.h
3  *
4  *  Copyright (C) 1993        Hamish Macdonald
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  *
10  * Changed:
11  * 10-Mar-94: Arno Griffioen: Conversion for vt100 emulator port from PC LINUX
12  */
13 
14 #ifndef _LINUX_CONSOLE_H_
15 #define _LINUX_CONSOLE_H_ 1
16 
17 #include <linux/atomic.h>
18 #include <linux/bits.h>
19 #include <linux/rculist.h>
20 #include <linux/types.h>
21 
22 struct vc_data;
23 struct console_font_op;
24 struct console_font;
25 struct module;
26 struct tty_struct;
27 struct notifier_block;
28 
29 enum con_scroll {
30 	SM_UP,
31 	SM_DOWN,
32 };
33 
34 enum vc_intensity;
35 
36 /**
37  * struct consw - callbacks for consoles
38  *
39  * @con_init:   initialize the console on @vc. @init is true for the very first
40  *		call on this @vc.
41  * @con_clear:  erase @count characters at [@x, @y] on @vc. @count >= 1.
42  * @con_scroll: move lines from @top to @bottom in direction @dir by @lines.
43  *		Return true if no generic handling should be done.
44  *		Invoked by csi_M and printing to the console.
45  * @con_switch: notifier about the console switch; it is supposed to return
46  *		true if a redraw is needed.
47  * @con_set_palette: sets the palette of the console to @table (optional)
48  * @con_scrolldelta: the contents of the console should be scrolled by @lines.
49  *		     Invoked by user. (optional)
50  */
51 struct consw {
52 	struct module *owner;
53 	const char *(*con_startup)(void);
54 	void	(*con_init)(struct vc_data *vc, bool init);
55 	void	(*con_deinit)(struct vc_data *vc);
56 	void	(*con_clear)(struct vc_data *vc, unsigned int y,
57 			     unsigned int x, unsigned int count);
58 	void	(*con_putc)(struct vc_data *vc, int c, int ypos, int xpos);
59 	void	(*con_putcs)(struct vc_data *vc, const unsigned short *s,
60 			int count, int ypos, int xpos);
61 	void	(*con_cursor)(struct vc_data *vc, int mode);
62 	bool	(*con_scroll)(struct vc_data *vc, unsigned int top,
63 			unsigned int bottom, enum con_scroll dir,
64 			unsigned int lines);
65 	bool	(*con_switch)(struct vc_data *vc);
66 	int	(*con_blank)(struct vc_data *vc, int blank, int mode_switch);
67 	int	(*con_font_set)(struct vc_data *vc, struct console_font *font,
68 			unsigned int vpitch, unsigned int flags);
69 	int	(*con_font_get)(struct vc_data *vc, struct console_font *font,
70 			unsigned int vpitch);
71 	int	(*con_font_default)(struct vc_data *vc,
72 			struct console_font *font, char *name);
73 	int     (*con_resize)(struct vc_data *vc, unsigned int width,
74 			unsigned int height, unsigned int user);
75 	void	(*con_set_palette)(struct vc_data *vc,
76 			const unsigned char *table);
77 	void	(*con_scrolldelta)(struct vc_data *vc, int lines);
78 	int	(*con_set_origin)(struct vc_data *vc);
79 	void	(*con_save_screen)(struct vc_data *vc);
80 	u8	(*con_build_attr)(struct vc_data *vc, u8 color,
81 			enum vc_intensity intensity,
82 			bool blink, bool underline, bool reverse, bool italic);
83 	void	(*con_invert_region)(struct vc_data *vc, u16 *p, int count);
84 	u16    *(*con_screen_pos)(const struct vc_data *vc, int offset);
85 	unsigned long (*con_getxy)(struct vc_data *vc, unsigned long position,
86 			int *px, int *py);
87 	/*
88 	 * Flush the video console driver's scrollback buffer
89 	 */
90 	void	(*con_flush_scrollback)(struct vc_data *vc);
91 	/*
92 	 * Prepare the console for the debugger.  This includes, but is not
93 	 * limited to, unblanking the console, loading an appropriate
94 	 * palette, and allowing debugger generated output.
95 	 */
96 	int	(*con_debug_enter)(struct vc_data *vc);
97 	/*
98 	 * Restore the console to its pre-debug state as closely as possible.
99 	 */
100 	int	(*con_debug_leave)(struct vc_data *vc);
101 };
102 
103 extern const struct consw *conswitchp;
104 
105 extern const struct consw dummy_con;	/* dummy console buffer */
106 extern const struct consw vga_con;	/* VGA text console */
107 extern const struct consw newport_con;	/* SGI Newport console  */
108 
109 int con_is_bound(const struct consw *csw);
110 int do_unregister_con_driver(const struct consw *csw);
111 int do_take_over_console(const struct consw *sw, int first, int last, int deflt);
112 void give_up_console(const struct consw *sw);
113 #ifdef CONFIG_HW_CONSOLE
114 int con_debug_enter(struct vc_data *vc);
115 int con_debug_leave(void);
116 #else
con_debug_enter(struct vc_data * vc)117 static inline int con_debug_enter(struct vc_data *vc)
118 {
119 	return 0;
120 }
con_debug_leave(void)121 static inline int con_debug_leave(void)
122 {
123 	return 0;
124 }
125 #endif
126 
127 /* cursor */
128 #define CM_DRAW     (1)
129 #define CM_ERASE    (2)
130 #define CM_MOVE     (3)
131 
132 /*
133  * The interface for a console, or any other device that wants to capture
134  * console messages (printer driver?)
135  */
136 
137 /**
138  * cons_flags - General console flags
139  * @CON_PRINTBUFFER:	Used by newly registered consoles to avoid duplicate
140  *			output of messages that were already shown by boot
141  *			consoles or read by userspace via syslog() syscall.
142  * @CON_CONSDEV:	Indicates that the console driver is backing
143  *			/dev/console.
144  * @CON_ENABLED:	Indicates if a console is allowed to print records. If
145  *			false, the console also will not advance to later
146  *			records.
147  * @CON_BOOT:		Marks the console driver as early console driver which
148  *			is used during boot before the real driver becomes
149  *			available. It will be automatically unregistered
150  *			when the real console driver is registered unless
151  *			"keep_bootcon" parameter is used.
152  * @CON_ANYTIME:	A misnomed historical flag which tells the core code
153  *			that the legacy @console::write callback can be invoked
154  *			on a CPU which is marked OFFLINE. That is misleading as
155  *			it suggests that there is no contextual limit for
156  *			invoking the callback. The original motivation was
157  *			readiness of the per-CPU areas.
158  * @CON_BRL:		Indicates a braille device which is exempt from
159  *			receiving the printk spam for obvious reasons.
160  * @CON_EXTENDED:	The console supports the extended output format of
161  *			/dev/kmesg which requires a larger output buffer.
162  * @CON_SUSPENDED:	Indicates if a console is suspended. If true, the
163  *			printing callbacks must not be called.
164  */
165 enum cons_flags {
166 	CON_PRINTBUFFER		= BIT(0),
167 	CON_CONSDEV		= BIT(1),
168 	CON_ENABLED		= BIT(2),
169 	CON_BOOT		= BIT(3),
170 	CON_ANYTIME		= BIT(4),
171 	CON_BRL			= BIT(5),
172 	CON_EXTENDED		= BIT(6),
173 	CON_SUSPENDED		= BIT(7),
174 };
175 
176 /**
177  * struct console - The console descriptor structure
178  * @name:		The name of the console driver
179  * @write:		Write callback to output messages (Optional)
180  * @read:		Read callback for console input (Optional)
181  * @device:		The underlying TTY device driver (Optional)
182  * @unblank:		Callback to unblank the console (Optional)
183  * @setup:		Callback for initializing the console (Optional)
184  * @exit:		Callback for teardown of the console (Optional)
185  * @match:		Callback for matching a console (Optional)
186  * @flags:		Console flags. See enum cons_flags
187  * @index:		Console index, e.g. port number
188  * @cflag:		TTY control mode flags
189  * @ispeed:		TTY input speed
190  * @ospeed:		TTY output speed
191  * @seq:		Sequence number of the next ringbuffer record to print
192  * @dropped:		Number of unreported dropped ringbuffer records
193  * @data:		Driver private data
194  * @node:		hlist node for the console list
195  */
196 struct console {
197 	char			name[16];
198 	void			(*write)(struct console *co, const char *s, unsigned int count);
199 	int			(*read)(struct console *co, char *s, unsigned int count);
200 	struct tty_driver	*(*device)(struct console *co, int *index);
201 	void			(*unblank)(void);
202 	int			(*setup)(struct console *co, char *options);
203 	int			(*exit)(struct console *co);
204 	int			(*match)(struct console *co, char *name, int idx, char *options);
205 	short			flags;
206 	short			index;
207 	int			cflag;
208 	uint			ispeed;
209 	uint			ospeed;
210 	u64			seq;
211 	unsigned long		dropped;
212 	void			*data;
213 	struct hlist_node	node;
214 };
215 
216 #ifdef CONFIG_LOCKDEP
217 extern void lockdep_assert_console_list_lock_held(void);
218 #else
lockdep_assert_console_list_lock_held(void)219 static inline void lockdep_assert_console_list_lock_held(void)
220 {
221 }
222 #endif
223 
224 #ifdef CONFIG_DEBUG_LOCK_ALLOC
225 extern bool console_srcu_read_lock_is_held(void);
226 #else
console_srcu_read_lock_is_held(void)227 static inline bool console_srcu_read_lock_is_held(void)
228 {
229 	return 1;
230 }
231 #endif
232 
233 extern int console_srcu_read_lock(void);
234 extern void console_srcu_read_unlock(int cookie);
235 
236 extern void console_list_lock(void) __acquires(console_mutex);
237 extern void console_list_unlock(void) __releases(console_mutex);
238 
239 extern struct hlist_head console_list;
240 
241 /**
242  * console_srcu_read_flags - Locklessly read the console flags
243  * @con:	struct console pointer of console to read flags from
244  *
245  * This function provides the necessary READ_ONCE() and data_race()
246  * notation for locklessly reading the console flags. The READ_ONCE()
247  * in this function matches the WRITE_ONCE() when @flags are modified
248  * for registered consoles with console_srcu_write_flags().
249  *
250  * Only use this function to read console flags when locklessly
251  * iterating the console list via srcu.
252  *
253  * Context: Any context.
254  */
console_srcu_read_flags(const struct console * con)255 static inline short console_srcu_read_flags(const struct console *con)
256 {
257 	WARN_ON_ONCE(!console_srcu_read_lock_is_held());
258 
259 	/*
260 	 * Locklessly reading console->flags provides a consistent
261 	 * read value because there is at most one CPU modifying
262 	 * console->flags and that CPU is using only read-modify-write
263 	 * operations to do so.
264 	 */
265 	return data_race(READ_ONCE(con->flags));
266 }
267 
268 /**
269  * console_srcu_write_flags - Write flags for a registered console
270  * @con:	struct console pointer of console to write flags to
271  * @flags:	new flags value to write
272  *
273  * Only use this function to write flags for registered consoles. It
274  * requires holding the console_list_lock.
275  *
276  * Context: Any context.
277  */
console_srcu_write_flags(struct console * con,short flags)278 static inline void console_srcu_write_flags(struct console *con, short flags)
279 {
280 	lockdep_assert_console_list_lock_held();
281 
282 	/* This matches the READ_ONCE() in console_srcu_read_flags(). */
283 	WRITE_ONCE(con->flags, flags);
284 }
285 
286 /* Variant of console_is_registered() when the console_list_lock is held. */
console_is_registered_locked(const struct console * con)287 static inline bool console_is_registered_locked(const struct console *con)
288 {
289 	lockdep_assert_console_list_lock_held();
290 	return !hlist_unhashed(&con->node);
291 }
292 
293 /*
294  * console_is_registered - Check if the console is registered
295  * @con:	struct console pointer of console to check
296  *
297  * Context: Process context. May sleep while acquiring console list lock.
298  * Return: true if the console is in the console list, otherwise false.
299  *
300  * If false is returned for a console that was previously registered, it
301  * can be assumed that the console's unregistration is fully completed,
302  * including the exit() callback after console list removal.
303  */
console_is_registered(const struct console * con)304 static inline bool console_is_registered(const struct console *con)
305 {
306 	bool ret;
307 
308 	console_list_lock();
309 	ret = console_is_registered_locked(con);
310 	console_list_unlock();
311 	return ret;
312 }
313 
314 /**
315  * for_each_console_srcu() - Iterator over registered consoles
316  * @con:	struct console pointer used as loop cursor
317  *
318  * Although SRCU guarantees the console list will be consistent, the
319  * struct console fields may be updated by other CPUs while iterating.
320  *
321  * Requires console_srcu_read_lock to be held. Can be invoked from
322  * any context.
323  */
324 #define for_each_console_srcu(con)					\
325 	hlist_for_each_entry_srcu(con, &console_list, node,		\
326 				  console_srcu_read_lock_is_held())
327 
328 /**
329  * for_each_console() - Iterator over registered consoles
330  * @con:	struct console pointer used as loop cursor
331  *
332  * The console list and the console->flags are immutable while iterating.
333  *
334  * Requires console_list_lock to be held.
335  */
336 #define for_each_console(con)						\
337 	lockdep_assert_console_list_lock_held();			\
338 	hlist_for_each_entry(con, &console_list, node)
339 
340 extern int console_set_on_cmdline;
341 extern struct console *early_console;
342 
343 enum con_flush_mode {
344 	CONSOLE_FLUSH_PENDING,
345 	CONSOLE_REPLAY_ALL,
346 };
347 
348 extern int add_preferred_console(char *name, int idx, char *options);
349 extern void console_force_preferred_locked(struct console *con);
350 extern void register_console(struct console *);
351 extern int unregister_console(struct console *);
352 extern void console_lock(void);
353 extern int console_trylock(void);
354 extern void console_unlock(void);
355 extern void console_conditional_schedule(void);
356 extern void console_unblank(void);
357 extern void console_flush_on_panic(enum con_flush_mode mode);
358 extern struct tty_driver *console_device(int *);
359 extern void console_stop(struct console *);
360 extern void console_start(struct console *);
361 extern int is_console_locked(void);
362 extern int braille_register_console(struct console *, int index,
363 		char *console_options, char *braille_options);
364 extern int braille_unregister_console(struct console *);
365 #ifdef CONFIG_TTY
366 extern void console_sysfs_notify(void);
367 #else
console_sysfs_notify(void)368 static inline void console_sysfs_notify(void)
369 { }
370 #endif
371 extern bool console_suspend_enabled;
372 
373 /* Suspend and resume console messages over PM events */
374 extern void suspend_console(void);
375 extern void resume_console(void);
376 
377 int mda_console_init(void);
378 
379 void vcs_make_sysfs(int index);
380 void vcs_remove_sysfs(int index);
381 
382 /* Some debug stub to catch some of the obvious races in the VT code */
383 #define WARN_CONSOLE_UNLOCKED()						\
384 	WARN_ON(!atomic_read(&ignore_console_lock_warning) &&		\
385 		!is_console_locked() && !oops_in_progress)
386 /*
387  * Increment ignore_console_lock_warning if you need to quiet
388  * WARN_CONSOLE_UNLOCKED() for debugging purposes.
389  */
390 extern atomic_t ignore_console_lock_warning;
391 
392 /* VESA Blanking Levels */
393 #define VESA_NO_BLANKING        0
394 #define VESA_VSYNC_SUSPEND      1
395 #define VESA_HSYNC_SUSPEND      2
396 #define VESA_POWERDOWN          3
397 
398 extern void console_init(void);
399 
400 /* For deferred console takeover */
401 void dummycon_register_output_notifier(struct notifier_block *nb);
402 void dummycon_unregister_output_notifier(struct notifier_block *nb);
403 
404 #endif /* _LINUX_CONSOLE_H */
405