xref: /linux/include/linux/console.h (revision 8e9bf8b9e8c0a3e1ef16dd48260a113f65ed01d2)
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  */
83 struct consw {
84 	struct module *owner;
85 	const char *(*con_startup)(void);
86 	void	(*con_init)(struct vc_data *vc, bool init);
87 	void	(*con_deinit)(struct vc_data *vc);
88 	void	(*con_clear)(struct vc_data *vc, unsigned int y,
89 			     unsigned int x, unsigned int count);
90 	void	(*con_putc)(struct vc_data *vc, u16 ca, unsigned int y,
91 			    unsigned int x);
92 	void	(*con_putcs)(struct vc_data *vc, const u16 *s,
93 			     unsigned int count, unsigned int ypos,
94 			     unsigned int xpos);
95 	void	(*con_cursor)(struct vc_data *vc, bool enable);
96 	bool	(*con_scroll)(struct vc_data *vc, unsigned int top,
97 			unsigned int bottom, enum con_scroll dir,
98 			unsigned int lines);
99 	bool	(*con_switch)(struct vc_data *vc);
100 	bool	(*con_blank)(struct vc_data *vc, enum vesa_blank_mode blank,
101 			     bool mode_switch);
102 	int	(*con_font_set)(struct vc_data *vc,
103 				const struct console_font *font,
104 				unsigned int vpitch, unsigned int flags);
105 	int	(*con_font_get)(struct vc_data *vc, struct console_font *font,
106 			unsigned int vpitch);
107 	int	(*con_font_default)(struct vc_data *vc,
108 			struct console_font *font, const char *name);
109 	int     (*con_resize)(struct vc_data *vc, unsigned int width,
110 			      unsigned int height, bool from_user);
111 	void	(*con_set_palette)(struct vc_data *vc,
112 			const unsigned char *table);
113 	void	(*con_scrolldelta)(struct vc_data *vc, int lines);
114 	bool	(*con_set_origin)(struct vc_data *vc);
115 	void	(*con_save_screen)(struct vc_data *vc);
116 	u8	(*con_build_attr)(struct vc_data *vc, u8 color,
117 			enum vc_intensity intensity,
118 			bool blink, bool underline, bool reverse, bool italic);
119 	void	(*con_invert_region)(struct vc_data *vc, u16 *p, int count);
120 };
121 
122 extern const struct consw *conswitchp;
123 
124 extern const struct consw dummy_con;	/* dummy console buffer */
125 extern const struct consw vga_con;	/* VGA text console */
126 extern const struct consw newport_con;	/* SGI Newport console  */
127 
128 struct screen_info;
129 #ifdef CONFIG_VGA_CONSOLE
130 void vgacon_register_screen(struct screen_info *si);
131 #else
132 static inline void vgacon_register_screen(struct screen_info *si) { }
133 #endif
134 
135 int con_is_bound(const struct consw *csw);
136 int do_unregister_con_driver(const struct consw *csw);
137 int do_take_over_console(const struct consw *sw, int first, int last, int deflt);
138 void give_up_console(const struct consw *sw);
139 #ifdef CONFIG_VT
140 void con_debug_enter(struct vc_data *vc);
141 void con_debug_leave(void);
142 #else
143 static inline void con_debug_enter(struct vc_data *vc) { }
144 static inline void con_debug_leave(void) { }
145 #endif
146 
147 /*
148  * The interface for a console, or any other device that wants to capture
149  * console messages (printer driver?)
150  */
151 
152 /**
153  * enum cons_flags - General console flags
154  * @CON_PRINTBUFFER:	Used by newly registered consoles to avoid duplicate
155  *			output of messages that were already shown by boot
156  *			consoles or read by userspace via syslog() syscall.
157  * @CON_CONSDEV:	Indicates that the console driver is backing
158  *			/dev/console.
159  * @CON_ENABLED:	Indicates if a console is allowed to print records. If
160  *			false, the console also will not advance to later
161  *			records.
162  * @CON_BOOT:		Marks the console driver as early console driver which
163  *			is used during boot before the real driver becomes
164  *			available. It will be automatically unregistered
165  *			when the real console driver is registered unless
166  *			"keep_bootcon" parameter is used.
167  * @CON_ANYTIME:	A misnomed historical flag which tells the core code
168  *			that the legacy @console::write callback can be invoked
169  *			on a CPU which is marked OFFLINE. That is misleading as
170  *			it suggests that there is no contextual limit for
171  *			invoking the callback. The original motivation was
172  *			readiness of the per-CPU areas.
173  * @CON_BRL:		Indicates a braille device which is exempt from
174  *			receiving the printk spam for obvious reasons.
175  * @CON_EXTENDED:	The console supports the extended output format of
176  *			/dev/kmesg which requires a larger output buffer.
177  * @CON_SUSPENDED:	Indicates if a console is suspended. If true, the
178  *			printing callbacks must not be called.
179  * @CON_NBCON:		Console can operate outside of the legacy style console_lock
180  *			constraints.
181  * @CON_NBCON_ATOMIC_UNSAFE: The write_atomic() callback is not safe and is
182  *			therefore only used by nbcon_atomic_flush_unsafe().
183  */
184 enum cons_flags {
185 	CON_PRINTBUFFER		= BIT(0),
186 	CON_CONSDEV		= BIT(1),
187 	CON_ENABLED		= BIT(2),
188 	CON_BOOT		= BIT(3),
189 	CON_ANYTIME		= BIT(4),
190 	CON_BRL			= BIT(5),
191 	CON_EXTENDED		= BIT(6),
192 	CON_SUSPENDED		= BIT(7),
193 	CON_NBCON		= BIT(8),
194 	CON_NBCON_ATOMIC_UNSAFE	= BIT(9),
195 };
196 
197 /**
198  * struct nbcon_state - console state for nbcon consoles
199  * @atom:	Compound of the state fields for atomic operations
200  *
201  * @req_prio:		The priority of a handover request
202  * @prio:		The priority of the current owner
203  * @unsafe:		Console is busy in a non takeover region
204  * @unsafe_takeover:	A hostile takeover in an unsafe state happened in the
205  *			past. The console cannot be safe until re-initialized.
206  * @cpu:		The CPU on which the owner runs
207  *
208  * To be used for reading and preparing of the value stored in the nbcon
209  * state variable @console::nbcon_state.
210  *
211  * The @prio and @req_prio fields are particularly important to allow
212  * spin-waiting to timeout and give up without the risk of a waiter being
213  * assigned the lock after giving up.
214  */
215 struct nbcon_state {
216 	union {
217 		unsigned int	atom;
218 		struct {
219 			unsigned int prio		:  2;
220 			unsigned int req_prio		:  2;
221 			unsigned int unsafe		:  1;
222 			unsigned int unsafe_takeover	:  1;
223 			unsigned int cpu		: 24;
224 		};
225 	};
226 };
227 
228 /*
229  * The nbcon_state struct is used to easily create and interpret values that
230  * are stored in the @console::nbcon_state variable. Ensure this struct stays
231  * within the size boundaries of the atomic variable's underlying type in
232  * order to avoid any accidental truncation.
233  */
234 static_assert(sizeof(struct nbcon_state) <= sizeof(int));
235 
236 /**
237  * enum nbcon_prio - console owner priority for nbcon consoles
238  * @NBCON_PRIO_NONE:		Unused
239  * @NBCON_PRIO_NORMAL:		Normal (non-emergency) usage
240  * @NBCON_PRIO_EMERGENCY:	Emergency output (WARN/OOPS...)
241  * @NBCON_PRIO_PANIC:		Panic output
242  * @NBCON_PRIO_MAX:		The number of priority levels
243  *
244  * A higher priority context can takeover the console when it is
245  * in the safe state. The final attempt to flush consoles in panic()
246  * can be allowed to do so even in an unsafe state (Hope and pray).
247  */
248 enum nbcon_prio {
249 	NBCON_PRIO_NONE = 0,
250 	NBCON_PRIO_NORMAL,
251 	NBCON_PRIO_EMERGENCY,
252 	NBCON_PRIO_PANIC,
253 	NBCON_PRIO_MAX,
254 };
255 
256 struct console;
257 struct printk_buffers;
258 
259 /**
260  * struct nbcon_context - Context for console acquire/release
261  * @console:			The associated console
262  * @spinwait_max_us:		Limit for spin-wait acquire
263  * @prio:			Priority of the context
264  * @allow_unsafe_takeover:	Allow performing takeover even if unsafe. Can
265  *				be used only with NBCON_PRIO_PANIC @prio. It
266  *				might cause a system freeze when the console
267  *				is used later.
268  * @backlog:			Ringbuffer has pending records
269  * @pbufs:			Pointer to the text buffer for this context
270  * @seq:			The sequence number to print for this context
271  */
272 struct nbcon_context {
273 	/* members set by caller */
274 	struct console		*console;
275 	unsigned int		spinwait_max_us;
276 	enum nbcon_prio		prio;
277 	unsigned int		allow_unsafe_takeover	: 1;
278 
279 	/* members set by emit */
280 	unsigned int		backlog			: 1;
281 
282 	/* members set by acquire */
283 	struct printk_buffers	*pbufs;
284 	u64			seq;
285 };
286 
287 /**
288  * struct nbcon_write_context - Context handed to the nbcon write callbacks
289  * @ctxt:		The core console context
290  * @outbuf:		Pointer to the text buffer for output
291  * @len:		Length to write
292  * @unsafe_takeover:	If a hostile takeover in an unsafe state has occurred
293  * @cpu:		CPU on which the message was generated
294  * @pid:		PID of the task that generated the message
295  * @comm:		Name of the task that generated the message
296  */
297 struct nbcon_write_context {
298 	struct nbcon_context	__private ctxt;
299 	char			*outbuf;
300 	unsigned int		len;
301 	bool			unsafe_takeover;
302 #ifdef CONFIG_PRINTK_EXECUTION_CTX
303 	int			cpu;
304 	pid_t			pid;
305 	char			comm[TASK_COMM_LEN];
306 #endif
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
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
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);
496 extern void console_list_unlock(void);
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  */
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  */
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. */
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  */
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  */
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
670 static inline void nbcon_cpu_emergency_enter(void) { }
671 static inline void nbcon_cpu_emergency_exit(void) { }
672 static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; }
673 static inline void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt,
674 					       char *buf, unsigned int len) { }
675 static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; }
676 static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; }
677 static inline void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt) { }
678 static inline bool nbcon_kdb_try_acquire(struct console *con,
679 					 struct nbcon_write_context *wctxt) { return false; }
680 static inline void nbcon_kdb_release(struct nbcon_write_context *wctxt) { }
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_unblank(void);
701 extern void console_flush_on_panic(enum con_flush_mode mode);
702 extern struct tty_driver *console_device(int *);
703 extern void console_suspend(struct console *);
704 extern void console_resume(struct console *);
705 extern int is_console_locked(void);
706 extern int braille_register_console(struct console *, int index,
707 		char *console_options, char *braille_options);
708 extern int braille_unregister_console(struct console *);
709 #ifdef CONFIG_TTY
710 extern void console_sysfs_notify(void);
711 #else
712 static inline void console_sysfs_notify(void)
713 { }
714 #endif
715 extern bool console_suspend_enabled;
716 
717 /* Suspend and resume console messages over PM events */
718 extern void console_suspend_all(void);
719 extern void console_resume_all(void);
720 
721 int mda_console_init(void);
722 
723 void vcs_make_sysfs(int index);
724 void vcs_remove_sysfs(int index);
725 
726 /* Some debug stub to catch some of the obvious races in the VT code */
727 #define WARN_CONSOLE_UNLOCKED()						\
728 	WARN_ON(!atomic_read(&ignore_console_lock_warning) &&		\
729 		!is_console_locked() && !oops_in_progress)
730 /*
731  * Increment ignore_console_lock_warning if you need to quiet
732  * WARN_CONSOLE_UNLOCKED() for debugging purposes.
733  */
734 extern atomic_t ignore_console_lock_warning;
735 
736 DEFINE_LOCK_GUARD_0(console_lock, console_lock(), console_unlock());
737 
738 extern void console_init(void);
739 
740 /* For deferred console takeover */
741 void dummycon_register_output_notifier(struct notifier_block *nb);
742 void dummycon_unregister_output_notifier(struct notifier_block *nb);
743 
744 #endif /* _LINUX_CONSOLE_H */
745