xref: /linux/include/linux/console.h (revision 4d38b88fd17e9989429e65420bf3c33ca53b2085)
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/irq_work.h>
20 #include <linux/rculist.h>
21 #include <linux/rcuwait.h>
22 #include <linux/smp.h>
23 #include <linux/types.h>
24 #include <linux/vesa.h>
25 
26 struct vc_data;
27 struct console_font_op;
28 struct console_font;
29 struct module;
30 struct tty_struct;
31 struct notifier_block;
32 
33 enum con_scroll {
34 	SM_UP,
35 	SM_DOWN,
36 };
37 
38 enum vc_intensity;
39 
40 /**
41  * struct consw - callbacks for consoles
42  *
43  * @owner:      the module to get references of when this console is used
44  * @con_startup: set up the console and return its name (like VGA, EGA, ...)
45  * @con_init:   initialize the console on @vc. @init is true for the very first
46  *		call on this @vc.
47  * @con_deinit: deinitialize the console from @vc.
48  * @con_clear:  erase @count characters at [@x, @y] on @vc. @count >= 1.
49  * @con_putc:   emit one character with attributes @ca to [@x, @y] on @vc.
50  *		(optional -- @con_putcs would be called instead)
51  * @con_putcs:  emit @count characters with attributes @s to [@x, @y] on @vc.
52  * @con_cursor: enable/disable cursor depending on @enable
53  * @con_scroll: move lines from @top to @bottom in direction @dir by @lines.
54  *		Return true if no generic handling should be done.
55  *		Invoked by csi_M and printing to the console.
56  * @con_switch: notifier about the console switch; it is supposed to return
57  *		true if a redraw is needed.
58  * @con_blank:  blank/unblank the console. The target mode is passed in @blank.
59  *		@mode_switch is set if changing from/to text/graphics. The hook
60  *		is supposed to return true if a redraw is needed.
61  * @con_font_set: set console @vc font to @font with height @vpitch. @flags can
62  *		be %KD_FONT_FLAG_DONT_RECALC. (optional)
63  * @con_font_get: fetch the current font on @vc of height @vpitch into @font.
64  *		(optional)
65  * @con_font_default: set default font on @vc. @name can be %NULL or font name
66  *		to search for. @font can be filled back. (optional)
67  * @con_resize:	resize the @vc console to @width x @height. @from_user is true
68  *		when this change comes from the user space.
69  * @con_set_palette: sets the palette of the console @vc to @table (optional)
70  * @con_scrolldelta: the contents of the console should be scrolled by @lines.
71  *		     Invoked by user. (optional)
72  * @con_set_origin: set origin (see &vc_data::vc_origin) of the @vc. If not
73  *		provided or returns false, the origin is set to
74  *		@vc->vc_screenbuf. (optional)
75  * @con_save_screen: save screen content into @vc->vc_screenbuf. Called e.g.
76  *		upon entering graphics. (optional)
77  * @con_build_attr: build attributes based on @color, @intensity and other
78  *		parameters. The result is used for both normal and erase
79  *		characters. (optional)
80  * @con_invert_region: invert a region of length @count on @vc starting at @p.
81  *		(optional)
82  * @con_debug_enter: prepare the console for the debugger. This includes, but
83  *		is not limited to, unblanking the console, loading an
84  *		appropriate palette, and allowing debugger generated output.
85  *		(optional)
86  * @con_debug_leave: restore the console to its pre-debug state as closely as
87  *		possible. (optional)
88  */
89 struct consw {
90 	struct module *owner;
91 	const char *(*con_startup)(void);
92 	void	(*con_init)(struct vc_data *vc, bool init);
93 	void	(*con_deinit)(struct vc_data *vc);
94 	void	(*con_clear)(struct vc_data *vc, unsigned int y,
95 			     unsigned int x, unsigned int count);
96 	void	(*con_putc)(struct vc_data *vc, u16 ca, unsigned int y,
97 			    unsigned int x);
98 	void	(*con_putcs)(struct vc_data *vc, const u16 *s,
99 			     unsigned int count, unsigned int ypos,
100 			     unsigned int xpos);
101 	void	(*con_cursor)(struct vc_data *vc, bool enable);
102 	bool	(*con_scroll)(struct vc_data *vc, unsigned int top,
103 			unsigned int bottom, enum con_scroll dir,
104 			unsigned int lines);
105 	bool	(*con_switch)(struct vc_data *vc);
106 	bool	(*con_blank)(struct vc_data *vc, enum vesa_blank_mode blank,
107 			     bool mode_switch);
108 	int	(*con_font_set)(struct vc_data *vc,
109 				const struct console_font *font,
110 				unsigned int vpitch, unsigned int flags);
111 	int	(*con_font_get)(struct vc_data *vc, struct console_font *font,
112 			unsigned int vpitch);
113 	int	(*con_font_default)(struct vc_data *vc,
114 			struct console_font *font, const char *name);
115 	int     (*con_resize)(struct vc_data *vc, unsigned int width,
116 			      unsigned int height, bool from_user);
117 	void	(*con_set_palette)(struct vc_data *vc,
118 			const unsigned char *table);
119 	void	(*con_scrolldelta)(struct vc_data *vc, int lines);
120 	bool	(*con_set_origin)(struct vc_data *vc);
121 	void	(*con_save_screen)(struct vc_data *vc);
122 	u8	(*con_build_attr)(struct vc_data *vc, u8 color,
123 			enum vc_intensity intensity,
124 			bool blink, bool underline, bool reverse, bool italic);
125 	void	(*con_invert_region)(struct vc_data *vc, u16 *p, int count);
126 	void	(*con_debug_enter)(struct vc_data *vc);
127 	void	(*con_debug_leave)(struct vc_data *vc);
128 };
129 
130 extern const struct consw *conswitchp;
131 
132 extern const struct consw dummy_con;	/* dummy console buffer */
133 extern const struct consw vga_con;	/* VGA text console */
134 extern const struct consw newport_con;	/* SGI Newport console  */
135 
136 struct screen_info;
137 #ifdef CONFIG_VGA_CONSOLE
138 void vgacon_register_screen(struct screen_info *si);
139 #else
vgacon_register_screen(struct screen_info * si)140 static inline void vgacon_register_screen(struct screen_info *si) { }
141 #endif
142 
143 int con_is_bound(const struct consw *csw);
144 int do_unregister_con_driver(const struct consw *csw);
145 int do_take_over_console(const struct consw *sw, int first, int last, int deflt);
146 void give_up_console(const struct consw *sw);
147 #ifdef CONFIG_VT
148 void con_debug_enter(struct vc_data *vc);
149 void con_debug_leave(void);
150 #else
con_debug_enter(struct vc_data * vc)151 static inline void con_debug_enter(struct vc_data *vc) { }
con_debug_leave(void)152 static inline void con_debug_leave(void) { }
153 #endif
154 
155 /*
156  * The interface for a console, or any other device that wants to capture
157  * console messages (printer driver?)
158  */
159 
160 /**
161  * enum cons_flags - General console flags
162  * @CON_PRINTBUFFER:	Used by newly registered consoles to avoid duplicate
163  *			output of messages that were already shown by boot
164  *			consoles or read by userspace via syslog() syscall.
165  * @CON_CONSDEV:	Indicates that the console driver is backing
166  *			/dev/console.
167  * @CON_ENABLED:	Indicates if a console is allowed to print records. If
168  *			false, the console also will not advance to later
169  *			records.
170  * @CON_BOOT:		Marks the console driver as early console driver which
171  *			is used during boot before the real driver becomes
172  *			available. It will be automatically unregistered
173  *			when the real console driver is registered unless
174  *			"keep_bootcon" parameter is used.
175  * @CON_ANYTIME:	A misnomed historical flag which tells the core code
176  *			that the legacy @console::write callback can be invoked
177  *			on a CPU which is marked OFFLINE. That is misleading as
178  *			it suggests that there is no contextual limit for
179  *			invoking the callback. The original motivation was
180  *			readiness of the per-CPU areas.
181  * @CON_BRL:		Indicates a braille device which is exempt from
182  *			receiving the printk spam for obvious reasons.
183  * @CON_EXTENDED:	The console supports the extended output format of
184  *			/dev/kmesg which requires a larger output buffer.
185  * @CON_SUSPENDED:	Indicates if a console is suspended. If true, the
186  *			printing callbacks must not be called.
187  * @CON_NBCON:		Console can operate outside of the legacy style console_lock
188  *			constraints.
189  * @CON_NBCON_ATOMIC_UNSAFE: The write_atomic() callback is not safe and is
190  *			therefore only used by nbcon_atomic_flush_unsafe().
191  */
192 enum cons_flags {
193 	CON_PRINTBUFFER		= BIT(0),
194 	CON_CONSDEV		= BIT(1),
195 	CON_ENABLED		= BIT(2),
196 	CON_BOOT		= BIT(3),
197 	CON_ANYTIME		= BIT(4),
198 	CON_BRL			= BIT(5),
199 	CON_EXTENDED		= BIT(6),
200 	CON_SUSPENDED		= BIT(7),
201 	CON_NBCON		= BIT(8),
202 	CON_NBCON_ATOMIC_UNSAFE	= BIT(9),
203 };
204 
205 /**
206  * struct nbcon_state - console state for nbcon consoles
207  * @atom:	Compound of the state fields for atomic operations
208  *
209  * @req_prio:		The priority of a handover request
210  * @prio:		The priority of the current owner
211  * @unsafe:		Console is busy in a non takeover region
212  * @unsafe_takeover:	A hostile takeover in an unsafe state happened in the
213  *			past. The console cannot be safe until re-initialized.
214  * @cpu:		The CPU on which the owner runs
215  *
216  * To be used for reading and preparing of the value stored in the nbcon
217  * state variable @console::nbcon_state.
218  *
219  * The @prio and @req_prio fields are particularly important to allow
220  * spin-waiting to timeout and give up without the risk of a waiter being
221  * assigned the lock after giving up.
222  */
223 struct nbcon_state {
224 	union {
225 		unsigned int	atom;
226 		struct {
227 			unsigned int prio		:  2;
228 			unsigned int req_prio		:  2;
229 			unsigned int unsafe		:  1;
230 			unsigned int unsafe_takeover	:  1;
231 			unsigned int cpu		: 24;
232 		};
233 	};
234 };
235 
236 /*
237  * The nbcon_state struct is used to easily create and interpret values that
238  * are stored in the @console::nbcon_state variable. Ensure this struct stays
239  * within the size boundaries of the atomic variable's underlying type in
240  * order to avoid any accidental truncation.
241  */
242 static_assert(sizeof(struct nbcon_state) <= sizeof(int));
243 
244 /**
245  * enum nbcon_prio - console owner priority for nbcon consoles
246  * @NBCON_PRIO_NONE:		Unused
247  * @NBCON_PRIO_NORMAL:		Normal (non-emergency) usage
248  * @NBCON_PRIO_EMERGENCY:	Emergency output (WARN/OOPS...)
249  * @NBCON_PRIO_PANIC:		Panic output
250  * @NBCON_PRIO_MAX:		The number of priority levels
251  *
252  * A higher priority context can takeover the console when it is
253  * in the safe state. The final attempt to flush consoles in panic()
254  * can be allowed to do so even in an unsafe state (Hope and pray).
255  */
256 enum nbcon_prio {
257 	NBCON_PRIO_NONE = 0,
258 	NBCON_PRIO_NORMAL,
259 	NBCON_PRIO_EMERGENCY,
260 	NBCON_PRIO_PANIC,
261 	NBCON_PRIO_MAX,
262 };
263 
264 struct console;
265 struct printk_buffers;
266 
267 /**
268  * struct nbcon_context - Context for console acquire/release
269  * @console:			The associated console
270  * @spinwait_max_us:		Limit for spin-wait acquire
271  * @prio:			Priority of the context
272  * @allow_unsafe_takeover:	Allow performing takeover even if unsafe. Can
273  *				be used only with NBCON_PRIO_PANIC @prio. It
274  *				might cause a system freeze when the console
275  *				is used later.
276  * @backlog:			Ringbuffer has pending records
277  * @pbufs:			Pointer to the text buffer for this context
278  * @seq:			The sequence number to print for this context
279  */
280 struct nbcon_context {
281 	/* members set by caller */
282 	struct console		*console;
283 	unsigned int		spinwait_max_us;
284 	enum nbcon_prio		prio;
285 	unsigned int		allow_unsafe_takeover	: 1;
286 
287 	/* members set by emit */
288 	unsigned int		backlog			: 1;
289 
290 	/* members set by acquire */
291 	struct printk_buffers	*pbufs;
292 	u64			seq;
293 };
294 
295 /**
296  * struct nbcon_write_context - Context handed to the nbcon write callbacks
297  * @ctxt:		The core console context
298  * @outbuf:		Pointer to the text buffer for output
299  * @len:		Length to write
300  * @unsafe_takeover:	If a hostile takeover in an unsafe state has occurred
301  */
302 struct nbcon_write_context {
303 	struct nbcon_context	__private ctxt;
304 	char			*outbuf;
305 	unsigned int		len;
306 	bool			unsafe_takeover;
307 };
308 
309 /**
310  * struct console - The console descriptor structure
311  * @name:		The name of the console driver
312  * @write:		Legacy write callback to output messages (Optional)
313  * @read:		Read callback for console input (Optional)
314  * @device:		The underlying TTY device driver (Optional)
315  * @unblank:		Callback to unblank the console (Optional)
316  * @setup:		Callback for initializing the console (Optional)
317  * @exit:		Callback for teardown of the console (Optional)
318  * @match:		Callback for matching a console (Optional)
319  * @flags:		Console flags. See enum cons_flags
320  * @index:		Console index, e.g. port number
321  * @cflag:		TTY control mode flags
322  * @ispeed:		TTY input speed
323  * @ospeed:		TTY output speed
324  * @seq:		Sequence number of the next ringbuffer record to print
325  * @dropped:		Number of unreported dropped ringbuffer records
326  * @data:		Driver private data
327  * @node:		hlist node for the console list
328  *
329  * @nbcon_state:	State for nbcon consoles
330  * @nbcon_seq:		Sequence number of the next record for nbcon to print
331  * @nbcon_device_ctxt:	Context available for non-printing operations
332  * @nbcon_prev_seq:	Seq num the previous nbcon owner was assigned to print
333  * @pbufs:		Pointer to nbcon private buffer
334  * @kthread:		Printer kthread for this console
335  * @rcuwait:		RCU-safe wait object for @kthread waking
336  * @irq_work:		Defer @kthread waking to IRQ work context
337  */
338 struct console {
339 	char			name[16];
340 	void			(*write)(struct console *co, const char *s, unsigned int count);
341 	int			(*read)(struct console *co, char *s, unsigned int count);
342 	struct tty_driver	*(*device)(struct console *co, int *index);
343 	void			(*unblank)(void);
344 	int			(*setup)(struct console *co, char *options);
345 	int			(*exit)(struct console *co);
346 	int			(*match)(struct console *co, char *name, int idx, char *options);
347 	short			flags;
348 	short			index;
349 	int			cflag;
350 	uint			ispeed;
351 	uint			ospeed;
352 	u64			seq;
353 	unsigned long		dropped;
354 	void			*data;
355 	struct hlist_node	node;
356 
357 	/* nbcon console specific members */
358 
359 	/**
360 	 * @write_atomic:
361 	 *
362 	 * NBCON callback to write out text in any context. (Optional)
363 	 *
364 	 * This callback is called with the console already acquired. However,
365 	 * a higher priority context is allowed to take it over by default.
366 	 *
367 	 * The callback must call nbcon_enter_unsafe() and nbcon_exit_unsafe()
368 	 * around any code where the takeover is not safe, for example, when
369 	 * manipulating the serial port registers.
370 	 *
371 	 * nbcon_enter_unsafe() will fail if the context has lost the console
372 	 * ownership in the meantime. In this case, the callback is no longer
373 	 * allowed to go forward. It must back out immediately and carefully.
374 	 * The buffer content is also no longer trusted since it no longer
375 	 * belongs to the context.
376 	 *
377 	 * The callback should allow the takeover whenever it is safe. It
378 	 * increases the chance to see messages when the system is in trouble.
379 	 * If the driver must reacquire ownership in order to finalize or
380 	 * revert hardware changes, nbcon_reacquire_nobuf() can be used.
381 	 * However, on reacquire the buffer content is no longer available. A
382 	 * reacquire cannot be used to resume printing.
383 	 *
384 	 * The callback can be called from any context (including NMI).
385 	 * Therefore it must avoid usage of any locking and instead rely
386 	 * on the console ownership for synchronization.
387 	 */
388 	void (*write_atomic)(struct console *con, struct nbcon_write_context *wctxt);
389 
390 	/**
391 	 * @write_thread:
392 	 *
393 	 * NBCON callback to write out text in task context.
394 	 *
395 	 * This callback must be called only in task context with both
396 	 * device_lock() and the nbcon console acquired with
397 	 * NBCON_PRIO_NORMAL.
398 	 *
399 	 * The same rules for console ownership verification and unsafe
400 	 * sections handling applies as with write_atomic().
401 	 *
402 	 * The console ownership handling is necessary for synchronization
403 	 * against write_atomic() which is synchronized only via the context.
404 	 *
405 	 * The device_lock() provides the primary serialization for operations
406 	 * on the device. It might be as relaxed (mutex)[*] or as tight
407 	 * (disabled preemption and interrupts) as needed. It allows
408 	 * the kthread to operate in the least restrictive mode[**].
409 	 *
410 	 * [*] Standalone nbcon_context_try_acquire() is not safe with
411 	 *     the preemption enabled, see nbcon_owner_matches(). But it
412 	 *     can be safe when always called in the preemptive context
413 	 *     under the device_lock().
414 	 *
415 	 * [**] The device_lock() makes sure that nbcon_context_try_acquire()
416 	 *      would never need to spin which is important especially with
417 	 *      PREEMPT_RT.
418 	 */
419 	void (*write_thread)(struct console *con, struct nbcon_write_context *wctxt);
420 
421 	/**
422 	 * @device_lock:
423 	 *
424 	 * NBCON callback to begin synchronization with driver code.
425 	 *
426 	 * Console drivers typically must deal with access to the hardware
427 	 * via user input/output (such as an interactive login shell) and
428 	 * output of kernel messages via printk() calls. This callback is
429 	 * called by the printk-subsystem whenever it needs to synchronize
430 	 * with hardware access by the driver. It should be implemented to
431 	 * use whatever synchronization mechanism the driver is using for
432 	 * itself (for example, the port lock for uart serial consoles).
433 	 *
434 	 * The callback is always called from task context. It may use any
435 	 * synchronization method required by the driver.
436 	 *
437 	 * IMPORTANT: The callback MUST disable migration. The console driver
438 	 *	may be using a synchronization mechanism that already takes
439 	 *	care of this (such as spinlocks). Otherwise this function must
440 	 *	explicitly call migrate_disable().
441 	 *
442 	 * The flags argument is provided as a convenience to the driver. It
443 	 * will be passed again to device_unlock(). It can be ignored if the
444 	 * driver does not need it.
445 	 */
446 	void (*device_lock)(struct console *con, unsigned long *flags);
447 
448 	/**
449 	 * @device_unlock:
450 	 *
451 	 * NBCON callback to finish synchronization with driver code.
452 	 *
453 	 * It is the counterpart to device_lock().
454 	 *
455 	 * This callback is always called from task context. It must
456 	 * appropriately re-enable migration (depending on how device_lock()
457 	 * disabled migration).
458 	 *
459 	 * The flags argument is the value of the same variable that was
460 	 * passed to device_lock().
461 	 */
462 	void (*device_unlock)(struct console *con, unsigned long flags);
463 
464 	atomic_t		__private nbcon_state;
465 	atomic_long_t		__private nbcon_seq;
466 	struct nbcon_context	__private nbcon_device_ctxt;
467 	atomic_long_t           __private nbcon_prev_seq;
468 
469 	struct printk_buffers	*pbufs;
470 	struct task_struct	*kthread;
471 	struct rcuwait		rcuwait;
472 	struct irq_work		irq_work;
473 };
474 
475 #ifdef CONFIG_LOCKDEP
476 extern void lockdep_assert_console_list_lock_held(void);
477 #else
lockdep_assert_console_list_lock_held(void)478 static inline void lockdep_assert_console_list_lock_held(void)
479 {
480 }
481 #endif
482 
483 #ifdef CONFIG_DEBUG_LOCK_ALLOC
484 extern bool console_srcu_read_lock_is_held(void);
485 #else
console_srcu_read_lock_is_held(void)486 static inline bool console_srcu_read_lock_is_held(void)
487 {
488 	return 1;
489 }
490 #endif
491 
492 extern int console_srcu_read_lock(void);
493 extern void console_srcu_read_unlock(int cookie);
494 
495 extern void console_list_lock(void) __acquires(console_mutex);
496 extern void console_list_unlock(void) __releases(console_mutex);
497 
498 extern struct hlist_head console_list;
499 
500 /**
501  * console_srcu_read_flags - Locklessly read flags of a possibly registered
502  *				console
503  * @con:	struct console pointer of console to read flags from
504  *
505  * Locklessly reading @con->flags provides a consistent read value because
506  * there is at most one CPU modifying @con->flags and that CPU is using only
507  * read-modify-write operations to do so.
508  *
509  * Requires console_srcu_read_lock to be held, which implies that @con might
510  * be a registered console. The purpose of holding console_srcu_read_lock is
511  * to guarantee that the console state is valid (CON_SUSPENDED/CON_ENABLED)
512  * and that no exit/cleanup routines will run if the console is currently
513  * undergoing unregistration.
514  *
515  * If the caller is holding the console_list_lock or it is _certain_ that
516  * @con is not and will not become registered, the caller may read
517  * @con->flags directly instead.
518  *
519  * Context: Any context.
520  * Return: The current value of the @con->flags field.
521  */
console_srcu_read_flags(const struct console * con)522 static inline short console_srcu_read_flags(const struct console *con)
523 {
524 	WARN_ON_ONCE(!console_srcu_read_lock_is_held());
525 
526 	/*
527 	 * The READ_ONCE() matches the WRITE_ONCE() when @flags are modified
528 	 * for registered consoles with console_srcu_write_flags().
529 	 */
530 	return data_race(READ_ONCE(con->flags));
531 }
532 
533 /**
534  * console_srcu_write_flags - Write flags for a registered console
535  * @con:	struct console pointer of console to write flags to
536  * @flags:	new flags value to write
537  *
538  * Only use this function to write flags for registered consoles. It
539  * requires holding the console_list_lock.
540  *
541  * Context: Any context.
542  */
console_srcu_write_flags(struct console * con,short flags)543 static inline void console_srcu_write_flags(struct console *con, short flags)
544 {
545 	lockdep_assert_console_list_lock_held();
546 
547 	/* This matches the READ_ONCE() in console_srcu_read_flags(). */
548 	WRITE_ONCE(con->flags, flags);
549 }
550 
551 /* Variant of console_is_registered() when the console_list_lock is held. */
console_is_registered_locked(const struct console * con)552 static inline bool console_is_registered_locked(const struct console *con)
553 {
554 	lockdep_assert_console_list_lock_held();
555 	return !hlist_unhashed(&con->node);
556 }
557 
558 /*
559  * console_is_registered - Check if the console is registered
560  * @con:	struct console pointer of console to check
561  *
562  * Context: Process context. May sleep while acquiring console list lock.
563  * Return: true if the console is in the console list, otherwise false.
564  *
565  * If false is returned for a console that was previously registered, it
566  * can be assumed that the console's unregistration is fully completed,
567  * including the exit() callback after console list removal.
568  */
console_is_registered(const struct console * con)569 static inline bool console_is_registered(const struct console *con)
570 {
571 	bool ret;
572 
573 	console_list_lock();
574 	ret = console_is_registered_locked(con);
575 	console_list_unlock();
576 	return ret;
577 }
578 
579 /**
580  * for_each_console_srcu() - Iterator over registered consoles
581  * @con:	struct console pointer used as loop cursor
582  *
583  * Although SRCU guarantees the console list will be consistent, the
584  * struct console fields may be updated by other CPUs while iterating.
585  *
586  * Requires console_srcu_read_lock to be held. Can be invoked from
587  * any context.
588  */
589 #define for_each_console_srcu(con)					\
590 	hlist_for_each_entry_srcu(con, &console_list, node,		\
591 				  console_srcu_read_lock_is_held())
592 
593 /**
594  * for_each_console() - Iterator over registered consoles
595  * @con:	struct console pointer used as loop cursor
596  *
597  * The console list and the &console.flags are immutable while iterating.
598  *
599  * Requires console_list_lock to be held.
600  */
601 #define for_each_console(con)						\
602 	lockdep_assert_console_list_lock_held();			\
603 	hlist_for_each_entry(con, &console_list, node)
604 
605 #ifdef CONFIG_PRINTK
606 extern void nbcon_cpu_emergency_enter(void);
607 extern void nbcon_cpu_emergency_exit(void);
608 extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt);
609 extern void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt,
610 					char *buf, unsigned int len);
611 extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt);
612 extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt);
613 extern void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt);
614 extern bool nbcon_allow_unsafe_takeover(void);
615 extern bool nbcon_kdb_try_acquire(struct console *con,
616 				  struct nbcon_write_context *wctxt);
617 extern void nbcon_kdb_release(struct nbcon_write_context *wctxt);
618 
619 /*
620  * Check if the given console is currently capable and allowed to print
621  * records. Note that this function does not consider the current context,
622  * which can also play a role in deciding if @con can be used to print
623  * records.
624  */
console_is_usable(struct console * con,short flags,bool use_atomic)625 static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
626 {
627 	if (!(flags & CON_ENABLED))
628 		return false;
629 
630 	if ((flags & CON_SUSPENDED))
631 		return false;
632 
633 	if (flags & CON_NBCON) {
634 		if (use_atomic) {
635 			/* The write_atomic() callback is optional. */
636 			if (!con->write_atomic)
637 				return false;
638 
639 			/*
640 			 * An unsafe write_atomic() callback is only usable
641 			 * when unsafe takeovers are allowed.
642 			 */
643 			if ((flags & CON_NBCON_ATOMIC_UNSAFE) && !nbcon_allow_unsafe_takeover())
644 				return false;
645 		}
646 
647 		/*
648 		 * For the !use_atomic case, @printk_kthreads_running is not
649 		 * checked because the write_thread() callback is also used
650 		 * via the legacy loop when the printer threads are not
651 		 * available.
652 		 */
653 	} else {
654 		if (!con->write)
655 			return false;
656 	}
657 
658 	/*
659 	 * Console drivers may assume that per-cpu resources have been
660 	 * allocated. So unless they're explicitly marked as being able to
661 	 * cope (CON_ANYTIME) don't call them until this CPU is officially up.
662 	 */
663 	if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME))
664 		return false;
665 
666 	return true;
667 }
668 
669 #else
nbcon_cpu_emergency_enter(void)670 static inline void nbcon_cpu_emergency_enter(void) { }
nbcon_cpu_emergency_exit(void)671 static inline void nbcon_cpu_emergency_exit(void) { }
nbcon_can_proceed(struct nbcon_write_context * wctxt)672 static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; }
nbcon_write_context_set_buf(struct nbcon_write_context * wctxt,char * buf,unsigned int len)673 static inline void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt,
674 					       char *buf, unsigned int len) { }
nbcon_enter_unsafe(struct nbcon_write_context * wctxt)675 static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; }
nbcon_exit_unsafe(struct nbcon_write_context * wctxt)676 static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; }
nbcon_reacquire_nobuf(struct nbcon_write_context * wctxt)677 static inline void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt) { }
nbcon_kdb_try_acquire(struct console * con,struct nbcon_write_context * wctxt)678 static inline bool nbcon_kdb_try_acquire(struct console *con,
679 					 struct nbcon_write_context *wctxt) { return false; }
nbcon_kdb_release(struct nbcon_write_context * wctxt)680 static inline void nbcon_kdb_release(struct nbcon_write_context *wctxt) { }
console_is_usable(struct console * con,short flags,bool use_atomic)681 static inline bool console_is_usable(struct console *con, short flags,
682 				     bool use_atomic) { return false; }
683 #endif
684 
685 extern int console_set_on_cmdline;
686 extern struct console *early_console;
687 
688 enum con_flush_mode {
689 	CONSOLE_FLUSH_PENDING,
690 	CONSOLE_REPLAY_ALL,
691 };
692 
693 extern int add_preferred_console(const char *name, const short idx, char *options);
694 extern void console_force_preferred_locked(struct console *con);
695 extern void register_console(struct console *);
696 extern int unregister_console(struct console *);
697 extern void console_lock(void);
698 extern int console_trylock(void);
699 extern void console_unlock(void);
700 extern void console_conditional_schedule(void);
701 extern void console_unblank(void);
702 extern void console_flush_on_panic(enum con_flush_mode mode);
703 extern struct tty_driver *console_device(int *);
704 extern void console_suspend(struct console *);
705 extern void console_resume(struct console *);
706 extern int is_console_locked(void);
707 extern int braille_register_console(struct console *, int index,
708 		char *console_options, char *braille_options);
709 extern int braille_unregister_console(struct console *);
710 #ifdef CONFIG_TTY
711 extern void console_sysfs_notify(void);
712 #else
console_sysfs_notify(void)713 static inline void console_sysfs_notify(void)
714 { }
715 #endif
716 extern bool console_suspend_enabled;
717 
718 /* Suspend and resume console messages over PM events */
719 extern void console_suspend_all(void);
720 extern void console_resume_all(void);
721 
722 int mda_console_init(void);
723 
724 void vcs_make_sysfs(int index);
725 void vcs_remove_sysfs(int index);
726 
727 /* Some debug stub to catch some of the obvious races in the VT code */
728 #define WARN_CONSOLE_UNLOCKED()						\
729 	WARN_ON(!atomic_read(&ignore_console_lock_warning) &&		\
730 		!is_console_locked() && !oops_in_progress)
731 /*
732  * Increment ignore_console_lock_warning if you need to quiet
733  * WARN_CONSOLE_UNLOCKED() for debugging purposes.
734  */
735 extern atomic_t ignore_console_lock_warning;
736 
737 DEFINE_LOCK_GUARD_0(console_lock, console_lock(), console_unlock());
738 
739 extern void console_init(void);
740 
741 /* For deferred console takeover */
742 void dummycon_register_output_notifier(struct notifier_block *nb);
743 void dummycon_unregister_output_notifier(struct notifier_block *nb);
744 
745 #endif /* _LINUX_CONSOLE_H */
746