Merge tag 'printk-for-6.7' of git://git.kernel.org/pub/scm/linux/kernel/git/printk...
[linux-2.6-block.git] / include / linux / console.h
1 /*
2  *  linux/include/linux/console.h
3  *
4  *  Copyright (C) 1993        Hamish Macdonald
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  *
10  * Changed:
11  * 10-Mar-94: Arno Griffioen: Conversion for vt100 emulator port from PC LINUX
12  */
13
14 #ifndef _LINUX_CONSOLE_H_
15 #define _LINUX_CONSOLE_H_ 1
16
17 #include <linux/atomic.h>
18 #include <linux/bits.h>
19 #include <linux/rculist.h>
20 #include <linux/types.h>
21
22 struct vc_data;
23 struct console_font_op;
24 struct console_font;
25 struct module;
26 struct tty_struct;
27 struct notifier_block;
28
29 enum con_scroll {
30         SM_UP,
31         SM_DOWN,
32 };
33
34 enum vc_intensity;
35
36 /**
37  * struct consw - callbacks for consoles
38  *
39  * @con_scroll: move lines from @top to @bottom in direction @dir by @lines.
40  *              Return true if no generic handling should be done.
41  *              Invoked by csi_M and printing to the console.
42  * @con_set_palette: sets the palette of the console to @table (optional)
43  * @con_scrolldelta: the contents of the console should be scrolled by @lines.
44  *                   Invoked by user. (optional)
45  */
46 struct consw {
47         struct module *owner;
48         const char *(*con_startup)(void);
49         void    (*con_init)(struct vc_data *vc, int init);
50         void    (*con_deinit)(struct vc_data *vc);
51         void    (*con_clear)(struct vc_data *vc, int sy, int sx, int height,
52                         int width);
53         void    (*con_putc)(struct vc_data *vc, int c, int ypos, int xpos);
54         void    (*con_putcs)(struct vc_data *vc, const unsigned short *s,
55                         int count, int ypos, int xpos);
56         void    (*con_cursor)(struct vc_data *vc, int mode);
57         bool    (*con_scroll)(struct vc_data *vc, unsigned int top,
58                         unsigned int bottom, enum con_scroll dir,
59                         unsigned int lines);
60         int     (*con_switch)(struct vc_data *vc);
61         int     (*con_blank)(struct vc_data *vc, int blank, int mode_switch);
62         int     (*con_font_set)(struct vc_data *vc, struct console_font *font,
63                         unsigned int vpitch, unsigned int flags);
64         int     (*con_font_get)(struct vc_data *vc, struct console_font *font,
65                         unsigned int vpitch);
66         int     (*con_font_default)(struct vc_data *vc,
67                         struct console_font *font, char *name);
68         int     (*con_resize)(struct vc_data *vc, unsigned int width,
69                         unsigned int height, unsigned int user);
70         void    (*con_set_palette)(struct vc_data *vc,
71                         const unsigned char *table);
72         void    (*con_scrolldelta)(struct vc_data *vc, int lines);
73         int     (*con_set_origin)(struct vc_data *vc);
74         void    (*con_save_screen)(struct vc_data *vc);
75         u8      (*con_build_attr)(struct vc_data *vc, u8 color,
76                         enum vc_intensity intensity,
77                         bool blink, bool underline, bool reverse, bool italic);
78         void    (*con_invert_region)(struct vc_data *vc, u16 *p, int count);
79         u16    *(*con_screen_pos)(const struct vc_data *vc, int offset);
80         unsigned long (*con_getxy)(struct vc_data *vc, unsigned long position,
81                         int *px, int *py);
82         /*
83          * Flush the video console driver's scrollback buffer
84          */
85         void    (*con_flush_scrollback)(struct vc_data *vc);
86         /*
87          * Prepare the console for the debugger.  This includes, but is not
88          * limited to, unblanking the console, loading an appropriate
89          * palette, and allowing debugger generated output.
90          */
91         int     (*con_debug_enter)(struct vc_data *vc);
92         /*
93          * Restore the console to its pre-debug state as closely as possible.
94          */
95         int     (*con_debug_leave)(struct vc_data *vc);
96 };
97
98 extern const struct consw *conswitchp;
99
100 extern const struct consw dummy_con;    /* dummy console buffer */
101 extern const struct consw vga_con;      /* VGA text console */
102 extern const struct consw newport_con;  /* SGI Newport console  */
103
104 int con_is_bound(const struct consw *csw);
105 int do_unregister_con_driver(const struct consw *csw);
106 int do_take_over_console(const struct consw *sw, int first, int last, int deflt);
107 void give_up_console(const struct consw *sw);
108 #ifdef CONFIG_HW_CONSOLE
109 int con_debug_enter(struct vc_data *vc);
110 int con_debug_leave(void);
111 #else
112 static inline int con_debug_enter(struct vc_data *vc)
113 {
114         return 0;
115 }
116 static inline int con_debug_leave(void)
117 {
118         return 0;
119 }
120 #endif
121
122 /* cursor */
123 #define CM_DRAW     (1)
124 #define CM_ERASE    (2)
125 #define CM_MOVE     (3)
126
127 /*
128  * The interface for a console, or any other device that wants to capture
129  * console messages (printer driver?)
130  */
131
132 /**
133  * cons_flags - General console flags
134  * @CON_PRINTBUFFER:    Used by newly registered consoles to avoid duplicate
135  *                      output of messages that were already shown by boot
136  *                      consoles or read by userspace via syslog() syscall.
137  * @CON_CONSDEV:        Indicates that the console driver is backing
138  *                      /dev/console.
139  * @CON_ENABLED:        Indicates if a console is allowed to print records. If
140  *                      false, the console also will not advance to later
141  *                      records.
142  * @CON_BOOT:           Marks the console driver as early console driver which
143  *                      is used during boot before the real driver becomes
144  *                      available. It will be automatically unregistered
145  *                      when the real console driver is registered unless
146  *                      "keep_bootcon" parameter is used.
147  * @CON_ANYTIME:        A misnomed historical flag which tells the core code
148  *                      that the legacy @console::write callback can be invoked
149  *                      on a CPU which is marked OFFLINE. That is misleading as
150  *                      it suggests that there is no contextual limit for
151  *                      invoking the callback. The original motivation was
152  *                      readiness of the per-CPU areas.
153  * @CON_BRL:            Indicates a braille device which is exempt from
154  *                      receiving the printk spam for obvious reasons.
155  * @CON_EXTENDED:       The console supports the extended output format of
156  *                      /dev/kmesg which requires a larger output buffer.
157  * @CON_SUSPENDED:      Indicates if a console is suspended. If true, the
158  *                      printing callbacks must not be called.
159  * @CON_NBCON:          Console can operate outside of the legacy style console_lock
160  *                      constraints.
161  */
162 enum cons_flags {
163         CON_PRINTBUFFER         = BIT(0),
164         CON_CONSDEV             = BIT(1),
165         CON_ENABLED             = BIT(2),
166         CON_BOOT                = BIT(3),
167         CON_ANYTIME             = BIT(4),
168         CON_BRL                 = BIT(5),
169         CON_EXTENDED            = BIT(6),
170         CON_SUSPENDED           = BIT(7),
171         CON_NBCON               = BIT(8),
172 };
173
174 /**
175  * struct nbcon_state - console state for nbcon consoles
176  * @atom:       Compound of the state fields for atomic operations
177  *
178  * @req_prio:           The priority of a handover request
179  * @prio:               The priority of the current owner
180  * @unsafe:             Console is busy in a non takeover region
181  * @unsafe_takeover:    A hostile takeover in an unsafe state happened in the
182  *                      past. The console cannot be safe until re-initialized.
183  * @cpu:                The CPU on which the owner runs
184  *
185  * To be used for reading and preparing of the value stored in the nbcon
186  * state variable @console::nbcon_state.
187  *
188  * The @prio and @req_prio fields are particularly important to allow
189  * spin-waiting to timeout and give up without the risk of a waiter being
190  * assigned the lock after giving up.
191  */
192 struct nbcon_state {
193         union {
194                 unsigned int    atom;
195                 struct {
196                         unsigned int prio               :  2;
197                         unsigned int req_prio           :  2;
198                         unsigned int unsafe             :  1;
199                         unsigned int unsafe_takeover    :  1;
200                         unsigned int cpu                : 24;
201                 };
202         };
203 };
204
205 /*
206  * The nbcon_state struct is used to easily create and interpret values that
207  * are stored in the @console::nbcon_state variable. Ensure this struct stays
208  * within the size boundaries of the atomic variable's underlying type in
209  * order to avoid any accidental truncation.
210  */
211 static_assert(sizeof(struct nbcon_state) <= sizeof(int));
212
213 /**
214  * nbcon_prio - console owner priority for nbcon consoles
215  * @NBCON_PRIO_NONE:            Unused
216  * @NBCON_PRIO_NORMAL:          Normal (non-emergency) usage
217  * @NBCON_PRIO_EMERGENCY:       Emergency output (WARN/OOPS...)
218  * @NBCON_PRIO_PANIC:           Panic output
219  * @NBCON_PRIO_MAX:             The number of priority levels
220  *
221  * A higher priority context can takeover the console when it is
222  * in the safe state. The final attempt to flush consoles in panic()
223  * can be allowed to do so even in an unsafe state (Hope and pray).
224  */
225 enum nbcon_prio {
226         NBCON_PRIO_NONE = 0,
227         NBCON_PRIO_NORMAL,
228         NBCON_PRIO_EMERGENCY,
229         NBCON_PRIO_PANIC,
230         NBCON_PRIO_MAX,
231 };
232
233 struct console;
234 struct printk_buffers;
235
236 /**
237  * struct nbcon_context - Context for console acquire/release
238  * @console:                    The associated console
239  * @spinwait_max_us:            Limit for spin-wait acquire
240  * @prio:                       Priority of the context
241  * @allow_unsafe_takeover:      Allow performing takeover even if unsafe. Can
242  *                              be used only with NBCON_PRIO_PANIC @prio. It
243  *                              might cause a system freeze when the console
244  *                              is used later.
245  * @backlog:                    Ringbuffer has pending records
246  * @pbufs:                      Pointer to the text buffer for this context
247  * @seq:                        The sequence number to print for this context
248  */
249 struct nbcon_context {
250         /* members set by caller */
251         struct console          *console;
252         unsigned int            spinwait_max_us;
253         enum nbcon_prio         prio;
254         unsigned int            allow_unsafe_takeover   : 1;
255
256         /* members set by emit */
257         unsigned int            backlog                 : 1;
258
259         /* members set by acquire */
260         struct printk_buffers   *pbufs;
261         u64                     seq;
262 };
263
264 /**
265  * struct nbcon_write_context - Context handed to the nbcon write callbacks
266  * @ctxt:               The core console context
267  * @outbuf:             Pointer to the text buffer for output
268  * @len:                Length to write
269  * @unsafe_takeover:    If a hostile takeover in an unsafe state has occurred
270  */
271 struct nbcon_write_context {
272         struct nbcon_context    __private ctxt;
273         char                    *outbuf;
274         unsigned int            len;
275         bool                    unsafe_takeover;
276 };
277
278 /**
279  * struct console - The console descriptor structure
280  * @name:               The name of the console driver
281  * @write:              Write callback to output messages (Optional)
282  * @read:               Read callback for console input (Optional)
283  * @device:             The underlying TTY device driver (Optional)
284  * @unblank:            Callback to unblank the console (Optional)
285  * @setup:              Callback for initializing the console (Optional)
286  * @exit:               Callback for teardown of the console (Optional)
287  * @match:              Callback for matching a console (Optional)
288  * @flags:              Console flags. See enum cons_flags
289  * @index:              Console index, e.g. port number
290  * @cflag:              TTY control mode flags
291  * @ispeed:             TTY input speed
292  * @ospeed:             TTY output speed
293  * @seq:                Sequence number of the next ringbuffer record to print
294  * @dropped:            Number of unreported dropped ringbuffer records
295  * @data:               Driver private data
296  * @node:               hlist node for the console list
297  *
298  * @write_atomic:       Write callback for atomic context
299  * @nbcon_state:        State for nbcon consoles
300  * @nbcon_seq:          Sequence number of the next record for nbcon to print
301  * @pbufs:              Pointer to nbcon private buffer
302  */
303 struct console {
304         char                    name[16];
305         void                    (*write)(struct console *co, const char *s, unsigned int count);
306         int                     (*read)(struct console *co, char *s, unsigned int count);
307         struct tty_driver       *(*device)(struct console *co, int *index);
308         void                    (*unblank)(void);
309         int                     (*setup)(struct console *co, char *options);
310         int                     (*exit)(struct console *co);
311         int                     (*match)(struct console *co, char *name, int idx, char *options);
312         short                   flags;
313         short                   index;
314         int                     cflag;
315         uint                    ispeed;
316         uint                    ospeed;
317         u64                     seq;
318         unsigned long           dropped;
319         void                    *data;
320         struct hlist_node       node;
321
322         /* nbcon console specific members */
323         bool                    (*write_atomic)(struct console *con,
324                                                 struct nbcon_write_context *wctxt);
325         atomic_t                __private nbcon_state;
326         atomic_long_t           __private nbcon_seq;
327         struct printk_buffers   *pbufs;
328 };
329
330 #ifdef CONFIG_LOCKDEP
331 extern void lockdep_assert_console_list_lock_held(void);
332 #else
333 static inline void lockdep_assert_console_list_lock_held(void)
334 {
335 }
336 #endif
337
338 #ifdef CONFIG_DEBUG_LOCK_ALLOC
339 extern bool console_srcu_read_lock_is_held(void);
340 #else
341 static inline bool console_srcu_read_lock_is_held(void)
342 {
343         return 1;
344 }
345 #endif
346
347 extern int console_srcu_read_lock(void);
348 extern void console_srcu_read_unlock(int cookie);
349
350 extern void console_list_lock(void) __acquires(console_mutex);
351 extern void console_list_unlock(void) __releases(console_mutex);
352
353 extern struct hlist_head console_list;
354
355 /**
356  * console_srcu_read_flags - Locklessly read the console flags
357  * @con:        struct console pointer of console to read flags from
358  *
359  * This function provides the necessary READ_ONCE() and data_race()
360  * notation for locklessly reading the console flags. The READ_ONCE()
361  * in this function matches the WRITE_ONCE() when @flags are modified
362  * for registered consoles with console_srcu_write_flags().
363  *
364  * Only use this function to read console flags when locklessly
365  * iterating the console list via srcu.
366  *
367  * Context: Any context.
368  */
369 static inline short console_srcu_read_flags(const struct console *con)
370 {
371         WARN_ON_ONCE(!console_srcu_read_lock_is_held());
372
373         /*
374          * Locklessly reading console->flags provides a consistent
375          * read value because there is at most one CPU modifying
376          * console->flags and that CPU is using only read-modify-write
377          * operations to do so.
378          */
379         return data_race(READ_ONCE(con->flags));
380 }
381
382 /**
383  * console_srcu_write_flags - Write flags for a registered console
384  * @con:        struct console pointer of console to write flags to
385  * @flags:      new flags value to write
386  *
387  * Only use this function to write flags for registered consoles. It
388  * requires holding the console_list_lock.
389  *
390  * Context: Any context.
391  */
392 static inline void console_srcu_write_flags(struct console *con, short flags)
393 {
394         lockdep_assert_console_list_lock_held();
395
396         /* This matches the READ_ONCE() in console_srcu_read_flags(). */
397         WRITE_ONCE(con->flags, flags);
398 }
399
400 /* Variant of console_is_registered() when the console_list_lock is held. */
401 static inline bool console_is_registered_locked(const struct console *con)
402 {
403         lockdep_assert_console_list_lock_held();
404         return !hlist_unhashed(&con->node);
405 }
406
407 /*
408  * console_is_registered - Check if the console is registered
409  * @con:        struct console pointer of console to check
410  *
411  * Context: Process context. May sleep while acquiring console list lock.
412  * Return: true if the console is in the console list, otherwise false.
413  *
414  * If false is returned for a console that was previously registered, it
415  * can be assumed that the console's unregistration is fully completed,
416  * including the exit() callback after console list removal.
417  */
418 static inline bool console_is_registered(const struct console *con)
419 {
420         bool ret;
421
422         console_list_lock();
423         ret = console_is_registered_locked(con);
424         console_list_unlock();
425         return ret;
426 }
427
428 /**
429  * for_each_console_srcu() - Iterator over registered consoles
430  * @con:        struct console pointer used as loop cursor
431  *
432  * Although SRCU guarantees the console list will be consistent, the
433  * struct console fields may be updated by other CPUs while iterating.
434  *
435  * Requires console_srcu_read_lock to be held. Can be invoked from
436  * any context.
437  */
438 #define for_each_console_srcu(con)                                      \
439         hlist_for_each_entry_srcu(con, &console_list, node,             \
440                                   console_srcu_read_lock_is_held())
441
442 /**
443  * for_each_console() - Iterator over registered consoles
444  * @con:        struct console pointer used as loop cursor
445  *
446  * The console list and the console->flags are immutable while iterating.
447  *
448  * Requires console_list_lock to be held.
449  */
450 #define for_each_console(con)                                           \
451         lockdep_assert_console_list_lock_held();                        \
452         hlist_for_each_entry(con, &console_list, node)
453
454 #ifdef CONFIG_PRINTK
455 extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt);
456 extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt);
457 extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt);
458 #else
459 static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; }
460 static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; }
461 static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; }
462 #endif
463
464 extern int console_set_on_cmdline;
465 extern struct console *early_console;
466
467 enum con_flush_mode {
468         CONSOLE_FLUSH_PENDING,
469         CONSOLE_REPLAY_ALL,
470 };
471
472 extern int add_preferred_console(char *name, int idx, char *options);
473 extern void console_force_preferred_locked(struct console *con);
474 extern void register_console(struct console *);
475 extern int unregister_console(struct console *);
476 extern void console_lock(void);
477 extern int console_trylock(void);
478 extern void console_unlock(void);
479 extern void console_conditional_schedule(void);
480 extern void console_unblank(void);
481 extern void console_flush_on_panic(enum con_flush_mode mode);
482 extern struct tty_driver *console_device(int *);
483 extern void console_stop(struct console *);
484 extern void console_start(struct console *);
485 extern int is_console_locked(void);
486 extern int braille_register_console(struct console *, int index,
487                 char *console_options, char *braille_options);
488 extern int braille_unregister_console(struct console *);
489 #ifdef CONFIG_TTY
490 extern void console_sysfs_notify(void);
491 #else
492 static inline void console_sysfs_notify(void)
493 { }
494 #endif
495 extern bool console_suspend_enabled;
496
497 /* Suspend and resume console messages over PM events */
498 extern void suspend_console(void);
499 extern void resume_console(void);
500
501 int mda_console_init(void);
502
503 void vcs_make_sysfs(int index);
504 void vcs_remove_sysfs(int index);
505
506 /* Some debug stub to catch some of the obvious races in the VT code */
507 #define WARN_CONSOLE_UNLOCKED()                                         \
508         WARN_ON(!atomic_read(&ignore_console_lock_warning) &&           \
509                 !is_console_locked() && !oops_in_progress)
510 /*
511  * Increment ignore_console_lock_warning if you need to quiet
512  * WARN_CONSOLE_UNLOCKED() for debugging purposes.
513  */
514 extern atomic_t ignore_console_lock_warning;
515
516 /* VESA Blanking Levels */
517 #define VESA_NO_BLANKING        0
518 #define VESA_VSYNC_SUSPEND      1
519 #define VESA_HSYNC_SUSPEND      2
520 #define VESA_POWERDOWN          3
521
522 extern void console_init(void);
523
524 /* For deferred console takeover */
525 void dummycon_register_output_notifier(struct notifier_block *nb);
526 void dummycon_unregister_output_notifier(struct notifier_block *nb);
527
528 #endif /* _LINUX_CONSOLE_H */