11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * linux/include/linux/console.h
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Copyright (C) 1993 Hamish Macdonald
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * This file is subject to the terms and conditions of the GNU General Public
71da177e4SLinus Torvalds * License. See the file COPYING in the main directory of this archive
81da177e4SLinus Torvalds * for more details.
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds * Changed:
111da177e4SLinus Torvalds * 10-Mar-94: Arno Griffioen: Conversion for vt100 emulator port from PC LINUX
121da177e4SLinus Torvalds */
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds #ifndef _LINUX_CONSOLE_H_
151da177e4SLinus Torvalds #define _LINUX_CONSOLE_H_ 1
161da177e4SLinus Torvalds
1756e6c104SThomas Zimmermann #include <linux/atomic.h>
18717a5651SThomas Gleixner #include <linux/bits.h>
1976f258bfSThomas Gleixner #include <linux/irq_work.h>
206c4afa79SJohn Ogness #include <linux/rculist.h>
2176f258bfSThomas Gleixner #include <linux/rcuwait.h>
221da177e4SLinus Torvalds #include <linux/types.h>
23ace4ebf9SJiri Slaby (SUSE) #include <linux/vesa.h>
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds struct vc_data;
261da177e4SLinus Torvalds struct console_font_op;
271da177e4SLinus Torvalds struct console_font;
281da177e4SLinus Torvalds struct module;
291b135431SAdrian Bunk struct tty_struct;
3083d83bebSHans de Goede struct notifier_block;
311da177e4SLinus Torvalds
32d705ff38SJiri Slaby enum con_scroll {
33d705ff38SJiri Slaby SM_UP,
34d705ff38SJiri Slaby SM_DOWN,
35d705ff38SJiri Slaby };
36d705ff38SJiri Slaby
37b84ae3dcSJiri Slaby enum vc_intensity;
38b84ae3dcSJiri Slaby
3997293de9SJiri Slaby /**
4097293de9SJiri Slaby * struct consw - callbacks for consoles
4197293de9SJiri Slaby *
42d1e22216SJiri Slaby (SUSE) * @owner: the module to get references of when this console is used
43d1e22216SJiri Slaby (SUSE) * @con_startup: set up the console and return its name (like VGA, EGA, ...)
44dae3e6b6SJiri Slaby (SUSE) * @con_init: initialize the console on @vc. @init is true for the very first
45dae3e6b6SJiri Slaby (SUSE) * call on this @vc.
46d1e22216SJiri Slaby (SUSE) * @con_deinit: deinitialize the console from @vc.
47559f01a0SJiri Slaby (SUSE) * @con_clear: erase @count characters at [@x, @y] on @vc. @count >= 1.
48338c2810SJiri Slaby (SUSE) * @con_putc: emit one character with attributes @ca to [@x, @y] on @vc.
49338c2810SJiri Slaby (SUSE) * (optional -- @con_putcs would be called instead)
50bfd7de49SJiri Slaby (SUSE) * @con_putcs: emit @count characters with attributes @s to [@x, @y] on @vc.
51a292e3fcSJiri Slaby (SUSE) * @con_cursor: enable/disable cursor depending on @enable
52d705ff38SJiri Slaby * @con_scroll: move lines from @top to @bottom in direction @dir by @lines.
53d705ff38SJiri Slaby * Return true if no generic handling should be done.
54d705ff38SJiri Slaby * Invoked by csi_M and printing to the console.
558d5cc8eeSJiri Slaby (SUSE) * @con_switch: notifier about the console switch; it is supposed to return
568d5cc8eeSJiri Slaby (SUSE) * true if a redraw is needed.
5777e11093SJiri Slaby (SUSE) * @con_blank: blank/unblank the console. The target mode is passed in @blank.
5877e11093SJiri Slaby (SUSE) * @mode_switch is set if changing from/to text/graphics. The hook
5977e11093SJiri Slaby (SUSE) * is supposed to return true if a redraw is needed.
60d1e22216SJiri Slaby (SUSE) * @con_font_set: set console @vc font to @font with height @vpitch. @flags can
61d1e22216SJiri Slaby (SUSE) * be %KD_FONT_FLAG_DONT_RECALC. (optional)
62d1e22216SJiri Slaby (SUSE) * @con_font_get: fetch the current font on @vc of height @vpitch into @font.
63d1e22216SJiri Slaby (SUSE) * (optional)
64d1e22216SJiri Slaby (SUSE) * @con_font_default: set default font on @vc. @name can be %NULL or font name
65d1e22216SJiri Slaby (SUSE) * to search for. @font can be filled back. (optional)
66d1e22216SJiri Slaby (SUSE) * @con_resize: resize the @vc console to @width x @height. @from_user is true
67d1e22216SJiri Slaby (SUSE) * when this change comes from the user space.
68d1e22216SJiri Slaby (SUSE) * @con_set_palette: sets the palette of the console @vc to @table (optional)
6997293de9SJiri Slaby * @con_scrolldelta: the contents of the console should be scrolled by @lines.
7097293de9SJiri Slaby * Invoked by user. (optional)
7142822fabSJiri Slaby (SUSE) * @con_set_origin: set origin (see &vc_data::vc_origin) of the @vc. If not
7242822fabSJiri Slaby (SUSE) * provided or returns false, the origin is set to
7342822fabSJiri Slaby (SUSE) * @vc->vc_screenbuf. (optional)
74d1e22216SJiri Slaby (SUSE) * @con_save_screen: save screen content into @vc->vc_screenbuf. Called e.g.
75d1e22216SJiri Slaby (SUSE) * upon entering graphics. (optional)
76d1e22216SJiri Slaby (SUSE) * @con_build_attr: build attributes based on @color, @intensity and other
77d1e22216SJiri Slaby (SUSE) * parameters. The result is used for both normal and erase
78d1e22216SJiri Slaby (SUSE) * characters. (optional)
79d1e22216SJiri Slaby (SUSE) * @con_invert_region: invert a region of length @count on @vc starting at @p.
80d1e22216SJiri Slaby (SUSE) * (optional)
81d1e22216SJiri Slaby (SUSE) * @con_debug_enter: prepare the console for the debugger. This includes, but
82d1e22216SJiri Slaby (SUSE) * is not limited to, unblanking the console, loading an
83d1e22216SJiri Slaby (SUSE) * appropriate palette, and allowing debugger generated output.
84d1e22216SJiri Slaby (SUSE) * (optional)
85d1e22216SJiri Slaby (SUSE) * @con_debug_leave: restore the console to its pre-debug state as closely as
86d1e22216SJiri Slaby (SUSE) * possible. (optional)
8797293de9SJiri Slaby */
881da177e4SLinus Torvalds struct consw {
891da177e4SLinus Torvalds struct module *owner;
901da177e4SLinus Torvalds const char *(*con_startup)(void);
91dae3e6b6SJiri Slaby (SUSE) void (*con_init)(struct vc_data *vc, bool init);
92209f668cSKees Cook void (*con_deinit)(struct vc_data *vc);
93559f01a0SJiri Slaby (SUSE) void (*con_clear)(struct vc_data *vc, unsigned int y,
94559f01a0SJiri Slaby (SUSE) unsigned int x, unsigned int count);
95338c2810SJiri Slaby (SUSE) void (*con_putc)(struct vc_data *vc, u16 ca, unsigned int y,
96338c2810SJiri Slaby (SUSE) unsigned int x);
97bfd7de49SJiri Slaby (SUSE) void (*con_putcs)(struct vc_data *vc, const u16 *s,
98bfd7de49SJiri Slaby (SUSE) unsigned int count, unsigned int ypos,
99bfd7de49SJiri Slaby (SUSE) unsigned int xpos);
100a292e3fcSJiri Slaby (SUSE) void (*con_cursor)(struct vc_data *vc, bool enable);
101209f668cSKees Cook bool (*con_scroll)(struct vc_data *vc, unsigned int top,
102d705ff38SJiri Slaby unsigned int bottom, enum con_scroll dir,
103d705ff38SJiri Slaby unsigned int lines);
1048d5cc8eeSJiri Slaby (SUSE) bool (*con_switch)(struct vc_data *vc);
10577e11093SJiri Slaby (SUSE) bool (*con_blank)(struct vc_data *vc, enum vesa_blank_mode blank,
10677e11093SJiri Slaby (SUSE) bool mode_switch);
107fd0f631fSJiri Slaby (SUSE) int (*con_font_set)(struct vc_data *vc,
108fd0f631fSJiri Slaby (SUSE) const struct console_font *font,
109ffc1e089SSamuel Thibault unsigned int vpitch, unsigned int flags);
110ffc1e089SSamuel Thibault int (*con_font_get)(struct vc_data *vc, struct console_font *font,
111ffc1e089SSamuel Thibault unsigned int vpitch);
112209f668cSKees Cook int (*con_font_default)(struct vc_data *vc,
1134f596170SJiri Slaby (SUSE) struct console_font *font, const char *name);
114209f668cSKees Cook int (*con_resize)(struct vc_data *vc, unsigned int width,
115beccdcfaSJiri Slaby (SUSE) unsigned int height, bool from_user);
116209f668cSKees Cook void (*con_set_palette)(struct vc_data *vc,
117709280daSJiri Slaby const unsigned char *table);
118209f668cSKees Cook void (*con_scrolldelta)(struct vc_data *vc, int lines);
11942822fabSJiri Slaby (SUSE) bool (*con_set_origin)(struct vc_data *vc);
120209f668cSKees Cook void (*con_save_screen)(struct vc_data *vc);
121b84ae3dcSJiri Slaby u8 (*con_build_attr)(struct vc_data *vc, u8 color,
122b84ae3dcSJiri Slaby enum vc_intensity intensity,
12377bc14f2SJiri Slaby bool blink, bool underline, bool reverse, bool italic);
124209f668cSKees Cook void (*con_invert_region)(struct vc_data *vc, u16 *p, int count);
1257995c30dSJiri Slaby (SUSE) void (*con_debug_enter)(struct vc_data *vc);
1267995c30dSJiri Slaby (SUSE) void (*con_debug_leave)(struct vc_data *vc);
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds
1291da177e4SLinus Torvalds extern const struct consw *conswitchp;
1301da177e4SLinus Torvalds
1311da177e4SLinus Torvalds extern const struct consw dummy_con; /* dummy console buffer */
1321da177e4SLinus Torvalds extern const struct consw vga_con; /* VGA text console */
1331da177e4SLinus Torvalds extern const struct consw newport_con; /* SGI Newport console */
1341da177e4SLinus Torvalds
135acfc7882SArnd Bergmann struct screen_info;
136acfc7882SArnd Bergmann #ifdef CONFIG_VGA_CONSOLE
137acfc7882SArnd Bergmann void vgacon_register_screen(struct screen_info *si);
138acfc7882SArnd Bergmann #else
vgacon_register_screen(struct screen_info * si)139acfc7882SArnd Bergmann static inline void vgacon_register_screen(struct screen_info *si) { }
140acfc7882SArnd Bergmann #endif
141acfc7882SArnd Bergmann
1423e795de7SAntonino A. Daplas int con_is_bound(const struct consw *csw);
143e93a9a86STakashi Iwai int do_unregister_con_driver(const struct consw *csw);
14450e244ccSAlan Cox int do_take_over_console(const struct consw *sw, int first, int last, int deflt);
1451da177e4SLinus Torvalds void give_up_console(const struct consw *sw);
1465c49b6a4SLukas Bulwahn #ifdef CONFIG_VT
1477995c30dSJiri Slaby (SUSE) void con_debug_enter(struct vc_data *vc);
1487995c30dSJiri Slaby (SUSE) void con_debug_leave(void);
1499261ec1aSJason Wessel #else
con_debug_enter(struct vc_data * vc)1507995c30dSJiri Slaby (SUSE) static inline void con_debug_enter(struct vc_data *vc) { }
con_debug_leave(void)1517995c30dSJiri Slaby (SUSE) static inline void con_debug_leave(void) { }
1529261ec1aSJason Wessel #endif
153b45cfba4SJesse Barnes
1541da177e4SLinus Torvalds /*
1551da177e4SLinus Torvalds * The interface for a console, or any other device that wants to capture
1561da177e4SLinus Torvalds * console messages (printer driver?)
1571da177e4SLinus Torvalds */
1581da177e4SLinus Torvalds
159717a5651SThomas Gleixner /**
16060234365SJiri Slaby (SUSE) * enum cons_flags - General console flags
161717a5651SThomas Gleixner * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate
162717a5651SThomas Gleixner * output of messages that were already shown by boot
163717a5651SThomas Gleixner * consoles or read by userspace via syslog() syscall.
164717a5651SThomas Gleixner * @CON_CONSDEV: Indicates that the console driver is backing
165717a5651SThomas Gleixner * /dev/console.
166717a5651SThomas Gleixner * @CON_ENABLED: Indicates if a console is allowed to print records. If
167717a5651SThomas Gleixner * false, the console also will not advance to later
168717a5651SThomas Gleixner * records.
169717a5651SThomas Gleixner * @CON_BOOT: Marks the console driver as early console driver which
170717a5651SThomas Gleixner * is used during boot before the real driver becomes
171717a5651SThomas Gleixner * available. It will be automatically unregistered
172717a5651SThomas Gleixner * when the real console driver is registered unless
173717a5651SThomas Gleixner * "keep_bootcon" parameter is used.
174717a5651SThomas Gleixner * @CON_ANYTIME: A misnomed historical flag which tells the core code
175717a5651SThomas Gleixner * that the legacy @console::write callback can be invoked
176717a5651SThomas Gleixner * on a CPU which is marked OFFLINE. That is misleading as
177717a5651SThomas Gleixner * it suggests that there is no contextual limit for
178717a5651SThomas Gleixner * invoking the callback. The original motivation was
179717a5651SThomas Gleixner * readiness of the per-CPU areas.
180717a5651SThomas Gleixner * @CON_BRL: Indicates a braille device which is exempt from
181717a5651SThomas Gleixner * receiving the printk spam for obvious reasons.
182717a5651SThomas Gleixner * @CON_EXTENDED: The console supports the extended output format of
183717a5651SThomas Gleixner * /dev/kmesg which requires a larger output buffer.
1849e70a5e1SJohn Ogness * @CON_SUSPENDED: Indicates if a console is suspended. If true, the
1859e70a5e1SJohn Ogness * printing callbacks must not be called.
1866b93bb41SThomas Gleixner * @CON_NBCON: Console can operate outside of the legacy style console_lock
1876b93bb41SThomas Gleixner * constraints.
188717a5651SThomas Gleixner */
189717a5651SThomas Gleixner enum cons_flags {
190717a5651SThomas Gleixner CON_PRINTBUFFER = BIT(0),
191717a5651SThomas Gleixner CON_CONSDEV = BIT(1),
192717a5651SThomas Gleixner CON_ENABLED = BIT(2),
193717a5651SThomas Gleixner CON_BOOT = BIT(3),
194717a5651SThomas Gleixner CON_ANYTIME = BIT(4),
195717a5651SThomas Gleixner CON_BRL = BIT(5),
196717a5651SThomas Gleixner CON_EXTENDED = BIT(6),
1979e70a5e1SJohn Ogness CON_SUSPENDED = BIT(7),
1986b93bb41SThomas Gleixner CON_NBCON = BIT(8),
199717a5651SThomas Gleixner };
2001da177e4SLinus Torvalds
20102b2396dSThomas Gleixner /**
2026b93bb41SThomas Gleixner * struct nbcon_state - console state for nbcon consoles
2036b93bb41SThomas Gleixner * @atom: Compound of the state fields for atomic operations
2046b93bb41SThomas Gleixner *
2053a5bb251SThomas Gleixner * @req_prio: The priority of a handover request
2063a5bb251SThomas Gleixner * @prio: The priority of the current owner
2073a5bb251SThomas Gleixner * @unsafe: Console is busy in a non takeover region
2083a5bb251SThomas Gleixner * @unsafe_takeover: A hostile takeover in an unsafe state happened in the
2093a5bb251SThomas Gleixner * past. The console cannot be safe until re-initialized.
2103a5bb251SThomas Gleixner * @cpu: The CPU on which the owner runs
2113a5bb251SThomas Gleixner *
2126b93bb41SThomas Gleixner * To be used for reading and preparing of the value stored in the nbcon
2136b93bb41SThomas Gleixner * state variable @console::nbcon_state.
2143a5bb251SThomas Gleixner *
2153a5bb251SThomas Gleixner * The @prio and @req_prio fields are particularly important to allow
2163a5bb251SThomas Gleixner * spin-waiting to timeout and give up without the risk of a waiter being
2173a5bb251SThomas Gleixner * assigned the lock after giving up.
2186b93bb41SThomas Gleixner */
2196b93bb41SThomas Gleixner struct nbcon_state {
2206b93bb41SThomas Gleixner union {
2216b93bb41SThomas Gleixner unsigned int atom;
2226b93bb41SThomas Gleixner struct {
2233a5bb251SThomas Gleixner unsigned int prio : 2;
2243a5bb251SThomas Gleixner unsigned int req_prio : 2;
2253a5bb251SThomas Gleixner unsigned int unsafe : 1;
2263a5bb251SThomas Gleixner unsigned int unsafe_takeover : 1;
2273a5bb251SThomas Gleixner unsigned int cpu : 24;
2286b93bb41SThomas Gleixner };
2296b93bb41SThomas Gleixner };
2306b93bb41SThomas Gleixner };
2316b93bb41SThomas Gleixner
2326b93bb41SThomas Gleixner /*
2336b93bb41SThomas Gleixner * The nbcon_state struct is used to easily create and interpret values that
2346b93bb41SThomas Gleixner * are stored in the @console::nbcon_state variable. Ensure this struct stays
2356b93bb41SThomas Gleixner * within the size boundaries of the atomic variable's underlying type in
2366b93bb41SThomas Gleixner * order to avoid any accidental truncation.
2376b93bb41SThomas Gleixner */
2386b93bb41SThomas Gleixner static_assert(sizeof(struct nbcon_state) <= sizeof(int));
2396b93bb41SThomas Gleixner
2406b93bb41SThomas Gleixner /**
24160234365SJiri Slaby (SUSE) * enum nbcon_prio - console owner priority for nbcon consoles
2423a5bb251SThomas Gleixner * @NBCON_PRIO_NONE: Unused
2433a5bb251SThomas Gleixner * @NBCON_PRIO_NORMAL: Normal (non-emergency) usage
2443a5bb251SThomas Gleixner * @NBCON_PRIO_EMERGENCY: Emergency output (WARN/OOPS...)
2453a5bb251SThomas Gleixner * @NBCON_PRIO_PANIC: Panic output
2463a5bb251SThomas Gleixner * @NBCON_PRIO_MAX: The number of priority levels
2473a5bb251SThomas Gleixner *
2483a5bb251SThomas Gleixner * A higher priority context can takeover the console when it is
2493a5bb251SThomas Gleixner * in the safe state. The final attempt to flush consoles in panic()
2503a5bb251SThomas Gleixner * can be allowed to do so even in an unsafe state (Hope and pray).
2513a5bb251SThomas Gleixner */
2523a5bb251SThomas Gleixner enum nbcon_prio {
2533a5bb251SThomas Gleixner NBCON_PRIO_NONE = 0,
2543a5bb251SThomas Gleixner NBCON_PRIO_NORMAL,
2553a5bb251SThomas Gleixner NBCON_PRIO_EMERGENCY,
2563a5bb251SThomas Gleixner NBCON_PRIO_PANIC,
2573a5bb251SThomas Gleixner NBCON_PRIO_MAX,
2583a5bb251SThomas Gleixner };
2593a5bb251SThomas Gleixner
2603a5bb251SThomas Gleixner struct console;
2615634c90fSThomas Gleixner struct printk_buffers;
2623a5bb251SThomas Gleixner
2633a5bb251SThomas Gleixner /**
2643a5bb251SThomas Gleixner * struct nbcon_context - Context for console acquire/release
2653a5bb251SThomas Gleixner * @console: The associated console
2663a5bb251SThomas Gleixner * @spinwait_max_us: Limit for spin-wait acquire
2673a5bb251SThomas Gleixner * @prio: Priority of the context
2683a5bb251SThomas Gleixner * @allow_unsafe_takeover: Allow performing takeover even if unsafe. Can
2693a5bb251SThomas Gleixner * be used only with NBCON_PRIO_PANIC @prio. It
2703a5bb251SThomas Gleixner * might cause a system freeze when the console
2713a5bb251SThomas Gleixner * is used later.
27206653d57SThomas Gleixner * @backlog: Ringbuffer has pending records
2735634c90fSThomas Gleixner * @pbufs: Pointer to the text buffer for this context
274ad56ebd1SThomas Gleixner * @seq: The sequence number to print for this context
2753a5bb251SThomas Gleixner */
2763a5bb251SThomas Gleixner struct nbcon_context {
2773a5bb251SThomas Gleixner /* members set by caller */
2783a5bb251SThomas Gleixner struct console *console;
2793a5bb251SThomas Gleixner unsigned int spinwait_max_us;
2803a5bb251SThomas Gleixner enum nbcon_prio prio;
2813a5bb251SThomas Gleixner unsigned int allow_unsafe_takeover : 1;
2825634c90fSThomas Gleixner
28306653d57SThomas Gleixner /* members set by emit */
28406653d57SThomas Gleixner unsigned int backlog : 1;
28506653d57SThomas Gleixner
2865634c90fSThomas Gleixner /* members set by acquire */
2875634c90fSThomas Gleixner struct printk_buffers *pbufs;
288ad56ebd1SThomas Gleixner u64 seq;
2893a5bb251SThomas Gleixner };
2903a5bb251SThomas Gleixner
2913a5bb251SThomas Gleixner /**
29206653d57SThomas Gleixner * struct nbcon_write_context - Context handed to the nbcon write callbacks
29306653d57SThomas Gleixner * @ctxt: The core console context
29406653d57SThomas Gleixner * @outbuf: Pointer to the text buffer for output
29506653d57SThomas Gleixner * @len: Length to write
29606653d57SThomas Gleixner * @unsafe_takeover: If a hostile takeover in an unsafe state has occurred
29706653d57SThomas Gleixner */
29806653d57SThomas Gleixner struct nbcon_write_context {
29906653d57SThomas Gleixner struct nbcon_context __private ctxt;
30006653d57SThomas Gleixner char *outbuf;
30106653d57SThomas Gleixner unsigned int len;
30206653d57SThomas Gleixner bool unsafe_takeover;
30306653d57SThomas Gleixner };
30406653d57SThomas Gleixner
30506653d57SThomas Gleixner /**
30602b2396dSThomas Gleixner * struct console - The console descriptor structure
30702b2396dSThomas Gleixner * @name: The name of the console driver
3087d56936cSJohn Ogness * @write: Legacy write callback to output messages (Optional)
30902b2396dSThomas Gleixner * @read: Read callback for console input (Optional)
31002b2396dSThomas Gleixner * @device: The underlying TTY device driver (Optional)
31102b2396dSThomas Gleixner * @unblank: Callback to unblank the console (Optional)
31202b2396dSThomas Gleixner * @setup: Callback for initializing the console (Optional)
31302b2396dSThomas Gleixner * @exit: Callback for teardown of the console (Optional)
31402b2396dSThomas Gleixner * @match: Callback for matching a console (Optional)
31502b2396dSThomas Gleixner * @flags: Console flags. See enum cons_flags
31602b2396dSThomas Gleixner * @index: Console index, e.g. port number
31702b2396dSThomas Gleixner * @cflag: TTY control mode flags
31802b2396dSThomas Gleixner * @ispeed: TTY input speed
31902b2396dSThomas Gleixner * @ospeed: TTY output speed
32002b2396dSThomas Gleixner * @seq: Sequence number of the next ringbuffer record to print
32102b2396dSThomas Gleixner * @dropped: Number of unreported dropped ringbuffer records
32202b2396dSThomas Gleixner * @data: Driver private data
32302b2396dSThomas Gleixner * @node: hlist node for the console list
3246b93bb41SThomas Gleixner *
3256b93bb41SThomas Gleixner * @nbcon_state: State for nbcon consoles
326ad56ebd1SThomas Gleixner * @nbcon_seq: Sequence number of the next record for nbcon to print
327adf6f37dSJohn Ogness * @nbcon_device_ctxt: Context available for non-printing operations
3285102981dSJohn Ogness * @nbcon_prev_seq: Seq num the previous nbcon owner was assigned to print
3295634c90fSThomas Gleixner * @pbufs: Pointer to nbcon private buffer
33076f258bfSThomas Gleixner * @kthread: Printer kthread for this console
33176f258bfSThomas Gleixner * @rcuwait: RCU-safe wait object for @kthread waking
33276f258bfSThomas Gleixner * @irq_work: Defer @kthread waking to IRQ work context
33302b2396dSThomas Gleixner */
3346ae9200fSAndrew Morton struct console {
3356ae9200fSAndrew Morton char name[16];
33602b2396dSThomas Gleixner void (*write)(struct console *co, const char *s, unsigned int count);
33702b2396dSThomas Gleixner int (*read)(struct console *co, char *s, unsigned int count);
33802b2396dSThomas Gleixner struct tty_driver *(*device)(struct console *co, int *index);
3391da177e4SLinus Torvalds void (*unblank)(void);
34002b2396dSThomas Gleixner int (*setup)(struct console *co, char *options);
34102b2396dSThomas Gleixner int (*exit)(struct console *co);
34202b2396dSThomas Gleixner int (*match)(struct console *co, char *name, int idx, char *options);
3431da177e4SLinus Torvalds short flags;
3441da177e4SLinus Torvalds short index;
3451da177e4SLinus Torvalds int cflag;
346027b5717SPali Rohár uint ispeed;
347027b5717SPali Rohár uint ospeed;
348a699449bSJohn Ogness u64 seq;
349a699449bSJohn Ogness unsigned long dropped;
3501da177e4SLinus Torvalds void *data;
351d9a4af56SThomas Gleixner struct hlist_node node;
3526b93bb41SThomas Gleixner
3536b93bb41SThomas Gleixner /* nbcon console specific members */
3547d56936cSJohn Ogness
3557d56936cSJohn Ogness /**
3567d56936cSJohn Ogness * @write_atomic:
3577d56936cSJohn Ogness *
3587d56936cSJohn Ogness * NBCON callback to write out text in any context. (Optional)
3597d56936cSJohn Ogness *
3607d56936cSJohn Ogness * This callback is called with the console already acquired. However,
3617d56936cSJohn Ogness * a higher priority context is allowed to take it over by default.
3627d56936cSJohn Ogness *
3637d56936cSJohn Ogness * The callback must call nbcon_enter_unsafe() and nbcon_exit_unsafe()
3647d56936cSJohn Ogness * around any code where the takeover is not safe, for example, when
3657d56936cSJohn Ogness * manipulating the serial port registers.
3667d56936cSJohn Ogness *
3677d56936cSJohn Ogness * nbcon_enter_unsafe() will fail if the context has lost the console
3687d56936cSJohn Ogness * ownership in the meantime. In this case, the callback is no longer
3697d56936cSJohn Ogness * allowed to go forward. It must back out immediately and carefully.
3707d56936cSJohn Ogness * The buffer content is also no longer trusted since it no longer
3717d56936cSJohn Ogness * belongs to the context.
3727d56936cSJohn Ogness *
3737d56936cSJohn Ogness * The callback should allow the takeover whenever it is safe. It
3747d56936cSJohn Ogness * increases the chance to see messages when the system is in trouble.
375bd07d864SJohn Ogness * If the driver must reacquire ownership in order to finalize or
376bd07d864SJohn Ogness * revert hardware changes, nbcon_reacquire_nobuf() can be used.
377bd07d864SJohn Ogness * However, on reacquire the buffer content is no longer available. A
378bd07d864SJohn Ogness * reacquire cannot be used to resume printing.
3797d56936cSJohn Ogness *
3807d56936cSJohn Ogness * The callback can be called from any context (including NMI).
3817d56936cSJohn Ogness * Therefore it must avoid usage of any locking and instead rely
3827d56936cSJohn Ogness * on the console ownership for synchronization.
3837d56936cSJohn Ogness */
3847d56936cSJohn Ogness void (*write_atomic)(struct console *con, struct nbcon_write_context *wctxt);
3857d56936cSJohn Ogness
3867a16a771SJohn Ogness /**
38776f258bfSThomas Gleixner * @write_thread:
38876f258bfSThomas Gleixner *
38976f258bfSThomas Gleixner * NBCON callback to write out text in task context.
39076f258bfSThomas Gleixner *
39176f258bfSThomas Gleixner * This callback must be called only in task context with both
39276f258bfSThomas Gleixner * device_lock() and the nbcon console acquired with
39376f258bfSThomas Gleixner * NBCON_PRIO_NORMAL.
39476f258bfSThomas Gleixner *
39576f258bfSThomas Gleixner * The same rules for console ownership verification and unsafe
39676f258bfSThomas Gleixner * sections handling applies as with write_atomic().
39776f258bfSThomas Gleixner *
39876f258bfSThomas Gleixner * The console ownership handling is necessary for synchronization
39976f258bfSThomas Gleixner * against write_atomic() which is synchronized only via the context.
40076f258bfSThomas Gleixner *
40176f258bfSThomas Gleixner * The device_lock() provides the primary serialization for operations
40276f258bfSThomas Gleixner * on the device. It might be as relaxed (mutex)[*] or as tight
40376f258bfSThomas Gleixner * (disabled preemption and interrupts) as needed. It allows
40476f258bfSThomas Gleixner * the kthread to operate in the least restrictive mode[**].
40576f258bfSThomas Gleixner *
40676f258bfSThomas Gleixner * [*] Standalone nbcon_context_try_acquire() is not safe with
40776f258bfSThomas Gleixner * the preemption enabled, see nbcon_owner_matches(). But it
40876f258bfSThomas Gleixner * can be safe when always called in the preemptive context
40976f258bfSThomas Gleixner * under the device_lock().
41076f258bfSThomas Gleixner *
41176f258bfSThomas Gleixner * [**] The device_lock() makes sure that nbcon_context_try_acquire()
41276f258bfSThomas Gleixner * would never need to spin which is important especially with
41376f258bfSThomas Gleixner * PREEMPT_RT.
41476f258bfSThomas Gleixner */
41576f258bfSThomas Gleixner void (*write_thread)(struct console *con, struct nbcon_write_context *wctxt);
41676f258bfSThomas Gleixner
41776f258bfSThomas Gleixner /**
4187a16a771SJohn Ogness * @device_lock:
4197a16a771SJohn Ogness *
4207a16a771SJohn Ogness * NBCON callback to begin synchronization with driver code.
4217a16a771SJohn Ogness *
4227a16a771SJohn Ogness * Console drivers typically must deal with access to the hardware
4237a16a771SJohn Ogness * via user input/output (such as an interactive login shell) and
4247a16a771SJohn Ogness * output of kernel messages via printk() calls. This callback is
4257a16a771SJohn Ogness * called by the printk-subsystem whenever it needs to synchronize
4267a16a771SJohn Ogness * with hardware access by the driver. It should be implemented to
4277a16a771SJohn Ogness * use whatever synchronization mechanism the driver is using for
4287a16a771SJohn Ogness * itself (for example, the port lock for uart serial consoles).
4297a16a771SJohn Ogness *
4307a16a771SJohn Ogness * The callback is always called from task context. It may use any
4317a16a771SJohn Ogness * synchronization method required by the driver.
4327a16a771SJohn Ogness *
4337a16a771SJohn Ogness * IMPORTANT: The callback MUST disable migration. The console driver
4347a16a771SJohn Ogness * may be using a synchronization mechanism that already takes
4357a16a771SJohn Ogness * care of this (such as spinlocks). Otherwise this function must
4367a16a771SJohn Ogness * explicitly call migrate_disable().
4377a16a771SJohn Ogness *
4387a16a771SJohn Ogness * The flags argument is provided as a convenience to the driver. It
4397a16a771SJohn Ogness * will be passed again to device_unlock(). It can be ignored if the
4407a16a771SJohn Ogness * driver does not need it.
4417a16a771SJohn Ogness */
4427a16a771SJohn Ogness void (*device_lock)(struct console *con, unsigned long *flags);
4437a16a771SJohn Ogness
4447a16a771SJohn Ogness /**
4457a16a771SJohn Ogness * @device_unlock:
4467a16a771SJohn Ogness *
4477a16a771SJohn Ogness * NBCON callback to finish synchronization with driver code.
4487a16a771SJohn Ogness *
4497a16a771SJohn Ogness * It is the counterpart to device_lock().
4507a16a771SJohn Ogness *
4517a16a771SJohn Ogness * This callback is always called from task context. It must
4527a16a771SJohn Ogness * appropriately re-enable migration (depending on how device_lock()
4537a16a771SJohn Ogness * disabled migration).
4547a16a771SJohn Ogness *
4557a16a771SJohn Ogness * The flags argument is the value of the same variable that was
4567a16a771SJohn Ogness * passed to device_lock().
4577a16a771SJohn Ogness */
4587a16a771SJohn Ogness void (*device_unlock)(struct console *con, unsigned long flags);
4597a16a771SJohn Ogness
4606b93bb41SThomas Gleixner atomic_t __private nbcon_state;
461ad56ebd1SThomas Gleixner atomic_long_t __private nbcon_seq;
462adf6f37dSJohn Ogness struct nbcon_context __private nbcon_device_ctxt;
4635102981dSJohn Ogness atomic_long_t __private nbcon_prev_seq;
46476f258bfSThomas Gleixner
4655634c90fSThomas Gleixner struct printk_buffers *pbufs;
46676f258bfSThomas Gleixner struct task_struct *kthread;
46776f258bfSThomas Gleixner struct rcuwait rcuwait;
46876f258bfSThomas Gleixner struct irq_work irq_work;
4691da177e4SLinus Torvalds };
4701da177e4SLinus Torvalds
4714dc64682SJohn Ogness #ifdef CONFIG_LOCKDEP
4724dc64682SJohn Ogness extern void lockdep_assert_console_list_lock_held(void);
4734dc64682SJohn Ogness #else
lockdep_assert_console_list_lock_held(void)4744dc64682SJohn Ogness static inline void lockdep_assert_console_list_lock_held(void)
4754dc64682SJohn Ogness {
4764dc64682SJohn Ogness }
4774dc64682SJohn Ogness #endif
4784dc64682SJohn Ogness
4796c4afa79SJohn Ogness #ifdef CONFIG_DEBUG_LOCK_ALLOC
4806c4afa79SJohn Ogness extern bool console_srcu_read_lock_is_held(void);
4816c4afa79SJohn Ogness #else
console_srcu_read_lock_is_held(void)4826c4afa79SJohn Ogness static inline bool console_srcu_read_lock_is_held(void)
4836c4afa79SJohn Ogness {
4846c4afa79SJohn Ogness return 1;
4856c4afa79SJohn Ogness }
4866c4afa79SJohn Ogness #endif
4876c4afa79SJohn Ogness
4886c4afa79SJohn Ogness extern int console_srcu_read_lock(void);
4896c4afa79SJohn Ogness extern void console_srcu_read_unlock(int cookie);
4906c4afa79SJohn Ogness
4914dc64682SJohn Ogness extern void console_list_lock(void) __acquires(console_mutex);
4924dc64682SJohn Ogness extern void console_list_unlock(void) __releases(console_mutex);
4934dc64682SJohn Ogness
494d9a4af56SThomas Gleixner extern struct hlist_head console_list;
495d9a4af56SThomas Gleixner
4966c4afa79SJohn Ogness /**
497dc219d8dSJohn Ogness * console_srcu_read_flags - Locklessly read flags of a possibly registered
498dc219d8dSJohn Ogness * console
499100bdef2SJohn Ogness * @con: struct console pointer of console to read flags from
500100bdef2SJohn Ogness *
501dc219d8dSJohn Ogness * Locklessly reading @con->flags provides a consistent read value because
502dc219d8dSJohn Ogness * there is at most one CPU modifying @con->flags and that CPU is using only
503dc219d8dSJohn Ogness * read-modify-write operations to do so.
504100bdef2SJohn Ogness *
505dc219d8dSJohn Ogness * Requires console_srcu_read_lock to be held, which implies that @con might
506dc219d8dSJohn Ogness * be a registered console. The purpose of holding console_srcu_read_lock is
507dc219d8dSJohn Ogness * to guarantee that the console state is valid (CON_SUSPENDED/CON_ENABLED)
508dc219d8dSJohn Ogness * and that no exit/cleanup routines will run if the console is currently
509dc219d8dSJohn Ogness * undergoing unregistration.
510dc219d8dSJohn Ogness *
511dc219d8dSJohn Ogness * If the caller is holding the console_list_lock or it is _certain_ that
512dc219d8dSJohn Ogness * @con is not and will not become registered, the caller may read
513dc219d8dSJohn Ogness * @con->flags directly instead.
514100bdef2SJohn Ogness *
515100bdef2SJohn Ogness * Context: Any context.
516dc219d8dSJohn Ogness * Return: The current value of the @con->flags field.
517100bdef2SJohn Ogness */
console_srcu_read_flags(const struct console * con)518100bdef2SJohn Ogness static inline short console_srcu_read_flags(const struct console *con)
519100bdef2SJohn Ogness {
520100bdef2SJohn Ogness WARN_ON_ONCE(!console_srcu_read_lock_is_held());
521100bdef2SJohn Ogness
522100bdef2SJohn Ogness /*
523dc219d8dSJohn Ogness * The READ_ONCE() matches the WRITE_ONCE() when @flags are modified
524dc219d8dSJohn Ogness * for registered consoles with console_srcu_write_flags().
525100bdef2SJohn Ogness */
526100bdef2SJohn Ogness return data_race(READ_ONCE(con->flags));
527100bdef2SJohn Ogness }
528100bdef2SJohn Ogness
529100bdef2SJohn Ogness /**
530100bdef2SJohn Ogness * console_srcu_write_flags - Write flags for a registered console
531100bdef2SJohn Ogness * @con: struct console pointer of console to write flags to
532100bdef2SJohn Ogness * @flags: new flags value to write
533100bdef2SJohn Ogness *
534100bdef2SJohn Ogness * Only use this function to write flags for registered consoles. It
535100bdef2SJohn Ogness * requires holding the console_list_lock.
536100bdef2SJohn Ogness *
537100bdef2SJohn Ogness * Context: Any context.
538100bdef2SJohn Ogness */
console_srcu_write_flags(struct console * con,short flags)539100bdef2SJohn Ogness static inline void console_srcu_write_flags(struct console *con, short flags)
540100bdef2SJohn Ogness {
541100bdef2SJohn Ogness lockdep_assert_console_list_lock_held();
542100bdef2SJohn Ogness
543100bdef2SJohn Ogness /* This matches the READ_ONCE() in console_srcu_read_flags(). */
544100bdef2SJohn Ogness WRITE_ONCE(con->flags, flags);
545100bdef2SJohn Ogness }
546100bdef2SJohn Ogness
5471fd4224aSJohn Ogness /* Variant of console_is_registered() when the console_list_lock is held. */
console_is_registered_locked(const struct console * con)5481fd4224aSJohn Ogness static inline bool console_is_registered_locked(const struct console *con)
5491fd4224aSJohn Ogness {
5501fd4224aSJohn Ogness lockdep_assert_console_list_lock_held();
5511fd4224aSJohn Ogness return !hlist_unhashed(&con->node);
5521fd4224aSJohn Ogness }
5531fd4224aSJohn Ogness
5541fd4224aSJohn Ogness /*
5551fd4224aSJohn Ogness * console_is_registered - Check if the console is registered
5561fd4224aSJohn Ogness * @con: struct console pointer of console to check
5571fd4224aSJohn Ogness *
5581fd4224aSJohn Ogness * Context: Process context. May sleep while acquiring console list lock.
5591fd4224aSJohn Ogness * Return: true if the console is in the console list, otherwise false.
5601fd4224aSJohn Ogness *
5611fd4224aSJohn Ogness * If false is returned for a console that was previously registered, it
5621fd4224aSJohn Ogness * can be assumed that the console's unregistration is fully completed,
5631fd4224aSJohn Ogness * including the exit() callback after console list removal.
5641fd4224aSJohn Ogness */
console_is_registered(const struct console * con)5651fd4224aSJohn Ogness static inline bool console_is_registered(const struct console *con)
5661fd4224aSJohn Ogness {
5671fd4224aSJohn Ogness bool ret;
5681fd4224aSJohn Ogness
5691fd4224aSJohn Ogness console_list_lock();
5701fd4224aSJohn Ogness ret = console_is_registered_locked(con);
5711fd4224aSJohn Ogness console_list_unlock();
5721fd4224aSJohn Ogness return ret;
5731fd4224aSJohn Ogness }
5741fd4224aSJohn Ogness
575100bdef2SJohn Ogness /**
5766c4afa79SJohn Ogness * for_each_console_srcu() - Iterator over registered consoles
5776c4afa79SJohn Ogness * @con: struct console pointer used as loop cursor
5786c4afa79SJohn Ogness *
5796c4afa79SJohn Ogness * Although SRCU guarantees the console list will be consistent, the
5806c4afa79SJohn Ogness * struct console fields may be updated by other CPUs while iterating.
5816c4afa79SJohn Ogness *
5826c4afa79SJohn Ogness * Requires console_srcu_read_lock to be held. Can be invoked from
5836c4afa79SJohn Ogness * any context.
5846c4afa79SJohn Ogness */
5856c4afa79SJohn Ogness #define for_each_console_srcu(con) \
5866c4afa79SJohn Ogness hlist_for_each_entry_srcu(con, &console_list, node, \
5876c4afa79SJohn Ogness console_srcu_read_lock_is_held())
5886c4afa79SJohn Ogness
5894dc64682SJohn Ogness /**
5904dc64682SJohn Ogness * for_each_console() - Iterator over registered consoles
5914dc64682SJohn Ogness * @con: struct console pointer used as loop cursor
5924dc64682SJohn Ogness *
59360234365SJiri Slaby (SUSE) * The console list and the &console.flags are immutable while iterating.
5944dc64682SJohn Ogness *
5954dc64682SJohn Ogness * Requires console_list_lock to be held.
596a75d946fSJiri Slaby */
597a75d946fSJiri Slaby #define for_each_console(con) \
5984dc64682SJohn Ogness lockdep_assert_console_list_lock_held(); \
599d9a4af56SThomas Gleixner hlist_for_each_entry(con, &console_list, node)
600a75d946fSJiri Slaby
6019757acd0SThomas Gleixner #ifdef CONFIG_PRINTK
602ecb5e1aaSThomas Gleixner extern void nbcon_cpu_emergency_enter(void);
603ecb5e1aaSThomas Gleixner extern void nbcon_cpu_emergency_exit(void);
6049757acd0SThomas Gleixner extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt);
6059757acd0SThomas Gleixner extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt);
6069757acd0SThomas Gleixner extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt);
607bd07d864SJohn Ogness extern void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt);
6089757acd0SThomas Gleixner #else
nbcon_cpu_emergency_enter(void)609ecb5e1aaSThomas Gleixner static inline void nbcon_cpu_emergency_enter(void) { }
nbcon_cpu_emergency_exit(void)610ecb5e1aaSThomas Gleixner static inline void nbcon_cpu_emergency_exit(void) { }
nbcon_can_proceed(struct nbcon_write_context * wctxt)6119757acd0SThomas Gleixner static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; }
nbcon_enter_unsafe(struct nbcon_write_context * wctxt)6129757acd0SThomas Gleixner static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; }
nbcon_exit_unsafe(struct nbcon_write_context * wctxt)6139757acd0SThomas Gleixner static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; }
nbcon_reacquire_nobuf(struct nbcon_write_context * wctxt)614bd07d864SJohn Ogness static inline void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt) { }
6159757acd0SThomas Gleixner #endif
6169757acd0SThomas Gleixner
6179e124fe1SMarkus Armbruster extern int console_set_on_cmdline;
618d0380e6cSThomas Gleixner extern struct console *early_console;
6199e124fe1SMarkus Armbruster
620de6da1e8SFeng Tang enum con_flush_mode {
621de6da1e8SFeng Tang CONSOLE_FLUSH_PENDING,
622de6da1e8SFeng Tang CONSOLE_REPLAY_ALL,
623de6da1e8SFeng Tang };
624de6da1e8SFeng Tang
6251e3c8526STony Lindgren extern int add_preferred_console(const char *name, const short idx, char *options);
6266f883675SJohn Ogness extern void console_force_preferred_locked(struct console *con);
6271da177e4SLinus Torvalds extern void register_console(struct console *);
6281da177e4SLinus Torvalds extern int unregister_console(struct console *);
629ac751efaSTorben Hohn extern void console_lock(void);
630ac751efaSTorben Hohn extern int console_trylock(void);
631ac751efaSTorben Hohn extern void console_unlock(void);
6321da177e4SLinus Torvalds extern void console_conditional_schedule(void);
6331da177e4SLinus Torvalds extern void console_unblank(void);
634de6da1e8SFeng Tang extern void console_flush_on_panic(enum con_flush_mode mode);
6351da177e4SLinus Torvalds extern struct tty_driver *console_device(int *);
636242fafe3SMarcos Paulo de Souza extern void console_suspend(struct console *);
637*5395e09cSMarcos Paulo de Souza extern void console_resume(struct console *);
6381da177e4SLinus Torvalds extern int is_console_locked(void);
639f7511d5fSSamuel Thibault extern int braille_register_console(struct console *, int index,
640f7511d5fSSamuel Thibault char *console_options, char *braille_options);
641f7511d5fSSamuel Thibault extern int braille_unregister_console(struct console *);
6424f73bc4dSJoe Millenbach #ifdef CONFIG_TTY
643fbc92a34SKay Sievers extern void console_sysfs_notify(void);
6444f73bc4dSJoe Millenbach #else
console_sysfs_notify(void)6454f73bc4dSJoe Millenbach static inline void console_sysfs_notify(void)
6464f73bc4dSJoe Millenbach { }
6474f73bc4dSJoe Millenbach #endif
64890ab5ee9SRusty Russell extern bool console_suspend_enabled;
6498f4ce8c3SAndres Salomon
650557240b4SLinus Torvalds /* Suspend and resume console messages over PM events */
651e9cec448SMarcos Paulo de Souza extern void console_suspend_all(void);
65263830aefSMarcos Paulo de Souza extern void console_resume_all(void);
653557240b4SLinus Torvalds
6543cb340ecSAdrian Bunk int mda_console_init(void);
6553cb340ecSAdrian Bunk
6564995f8efSKay Sievers void vcs_make_sysfs(int index);
6574995f8efSKay Sievers void vcs_remove_sysfs(int index);
6581b135431SAdrian Bunk
6591da177e4SLinus Torvalds /* Some debug stub to catch some of the obvious races in the VT code */
66056e6c104SThomas Zimmermann #define WARN_CONSOLE_UNLOCKED() \
66156e6c104SThomas Zimmermann WARN_ON(!atomic_read(&ignore_console_lock_warning) && \
66256e6c104SThomas Zimmermann !is_console_locked() && !oops_in_progress)
66356e6c104SThomas Zimmermann /*
66456e6c104SThomas Zimmermann * Increment ignore_console_lock_warning if you need to quiet
66556e6c104SThomas Zimmermann * WARN_CONSOLE_UNLOCKED() for debugging purposes.
66656e6c104SThomas Zimmermann */
66756e6c104SThomas Zimmermann extern atomic_t ignore_console_lock_warning;
6681da177e4SLinus Torvalds
6690c688614SNicolas Pitre extern void console_init(void);
6700c688614SNicolas Pitre
67183d83bebSHans de Goede /* For deferred console takeover */
67283d83bebSHans de Goede void dummycon_register_output_notifier(struct notifier_block *nb);
67383d83bebSHans de Goede void dummycon_unregister_output_notifier(struct notifier_block *nb);
67483d83bebSHans de Goede
6751da177e4SLinus Torvalds #endif /* _LINUX_CONSOLE_H */
676