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