1 // SPDX-License-Identifier: GPL-2.0
3 * SCLP line mode console driver
5 * Copyright IBM Corp. 1999, 2009
10 #include <linux/kmod.h>
11 #include <linux/console.h>
12 #include <linux/init.h>
13 #include <linux/panic_notifier.h>
14 #include <linux/timer.h>
15 #include <linux/jiffies.h>
16 #include <linux/termios.h>
17 #include <linux/err.h>
18 #include <linux/reboot.h>
19 #include <linux/gfp.h>
25 #define sclp_console_major 4 /* TTYAUX_MAJOR */
26 #define sclp_console_minor 64
27 #define sclp_console_name "ttyS"
29 /* Lock to guard over changes to global variables */
30 static DEFINE_SPINLOCK(sclp_con_lock);
31 /* List of free pages that can be used for console output buffering */
32 static LIST_HEAD(sclp_con_pages);
33 /* List of full struct sclp_buffer structures ready for output */
34 static LIST_HEAD(sclp_con_outqueue);
35 /* Pointer to current console buffer */
36 static struct sclp_buffer *sclp_conbuf;
37 /* Timer for delayed output of console messages */
38 static struct timer_list sclp_con_timer;
39 /* Flag that output queue is currently running */
40 static int sclp_con_queue_running;
42 /* Output format for console messages */
43 #define SCLP_CON_COLUMNS 320
44 #define SPACES_PER_TAB 8
47 sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
53 page = sclp_unmake_buffer(buffer);
54 spin_lock_irqsave(&sclp_con_lock, flags);
56 /* Remove buffer from outqueue */
57 list_del(&buffer->list);
58 list_add_tail((struct list_head *) page, &sclp_con_pages);
60 /* Check if there is a pending buffer on the out queue. */
62 if (!list_empty(&sclp_con_outqueue))
63 buffer = list_first_entry(&sclp_con_outqueue,
64 struct sclp_buffer, list);
66 sclp_con_queue_running = 0;
67 spin_unlock_irqrestore(&sclp_con_lock, flags);
70 spin_unlock_irqrestore(&sclp_con_lock, flags);
71 } while (sclp_emit_buffer(buffer, sclp_conbuf_callback));
75 * Finalize and emit first pending buffer.
77 static void sclp_conbuf_emit(void)
79 struct sclp_buffer* buffer;
83 spin_lock_irqsave(&sclp_con_lock, flags);
85 list_add_tail(&sclp_conbuf->list, &sclp_con_outqueue);
87 if (sclp_con_queue_running)
89 if (list_empty(&sclp_con_outqueue))
91 buffer = list_first_entry(&sclp_con_outqueue, struct sclp_buffer,
93 sclp_con_queue_running = 1;
94 spin_unlock_irqrestore(&sclp_con_lock, flags);
96 rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
98 sclp_conbuf_callback(buffer, rc);
101 spin_unlock_irqrestore(&sclp_con_lock, flags);
105 * Wait until out queue is empty
107 static void sclp_console_sync_queue(void)
111 spin_lock_irqsave(&sclp_con_lock, flags);
112 del_timer(&sclp_con_timer);
113 while (sclp_con_queue_running) {
114 spin_unlock_irqrestore(&sclp_con_lock, flags);
116 spin_lock_irqsave(&sclp_con_lock, flags);
118 spin_unlock_irqrestore(&sclp_con_lock, flags);
122 * When this routine is called from the timer then we flush the
123 * temporary write buffer without further waiting on a final new line.
126 sclp_console_timeout(struct timer_list *unused)
132 * Drop oldest console buffer if sclp_con_drop is set
135 sclp_console_drop_buffer(void)
137 struct list_head *list;
138 struct sclp_buffer *buffer;
141 if (!sclp_console_drop)
143 list = sclp_con_outqueue.next;
144 if (sclp_con_queue_running)
145 /* The first element is in I/O */
147 if (list == &sclp_con_outqueue)
150 buffer = list_entry(list, struct sclp_buffer, list);
151 page = sclp_unmake_buffer(buffer);
152 list_add_tail((struct list_head *) page, &sclp_con_pages);
157 * Writes the given message to S390 system console
160 sclp_console_write(struct console *console, const char *message,
169 spin_lock_irqsave(&sclp_con_lock, flags);
171 * process escape characters, write message into buffer,
172 * send buffer to SCLP
175 /* make sure we have a console output buffer */
176 if (sclp_conbuf == NULL) {
177 if (list_empty(&sclp_con_pages))
179 while (list_empty(&sclp_con_pages)) {
180 if (sclp_console_drop_buffer())
182 spin_unlock_irqrestore(&sclp_con_lock, flags);
184 spin_lock_irqsave(&sclp_con_lock, flags);
186 page = sclp_con_pages.next;
187 list_del((struct list_head *) page);
188 sclp_conbuf = sclp_make_buffer(page, SCLP_CON_COLUMNS,
191 /* try to write the string to the current output buffer */
192 written = sclp_write(sclp_conbuf, (const unsigned char *)
194 if (written == count)
197 * Not all characters could be written to the current
198 * output buffer. Emit the buffer, create a new buffer
199 * and then output the rest of the string.
201 spin_unlock_irqrestore(&sclp_con_lock, flags);
203 spin_lock_irqsave(&sclp_con_lock, flags);
207 /* Setup timer to output current console buffer after 1/10 second */
208 if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
209 !timer_pending(&sclp_con_timer)) {
210 mod_timer(&sclp_con_timer, jiffies + HZ / 10);
212 spin_unlock_irqrestore(&sclp_con_lock, flags);
215 static struct tty_driver *
216 sclp_console_device(struct console *c, int *index)
219 return sclp_tty_driver;
223 * This panic/reboot notifier makes sure that all buffers
224 * will be flushed to the SCLP.
226 static int sclp_console_notify(struct notifier_block *self,
227 unsigned long event, void *data)
230 * Perform the lock check before effectively getting the
231 * lock on sclp_conbuf_emit() / sclp_console_sync_queue()
232 * to prevent potential lockups in atomic context.
234 if (spin_is_locked(&sclp_con_lock))
238 sclp_console_sync_queue();
243 static struct notifier_block on_panic_nb = {
244 .notifier_call = sclp_console_notify,
245 .priority = INT_MIN + 1, /* run the callback late */
248 static struct notifier_block on_reboot_nb = {
249 .notifier_call = sclp_console_notify,
250 .priority = INT_MIN + 1, /* run the callback late */
254 * used to register the SCLP console to the kernel and to
255 * give printk necessary information
257 static struct console sclp_console =
259 .name = sclp_console_name,
260 .write = sclp_console_write,
261 .device = sclp_console_device,
262 .flags = CON_PRINTBUFFER,
263 .index = 0 /* ttyS0 */
267 * called by console_init() in drivers/char/tty_io.c at boot-time.
270 sclp_console_init(void)
276 /* SCLP consoles are handled together */
277 if (!(CONSOLE_IS_SCLP || CONSOLE_IS_VT220))
282 /* Allocate pages for output buffering */
283 for (i = 0; i < sclp_console_pages; i++) {
284 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
285 list_add_tail(page, &sclp_con_pages);
288 timer_setup(&sclp_con_timer, sclp_console_timeout, 0);
290 /* enable printk-access to this driver */
291 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
292 register_reboot_notifier(&on_reboot_nb);
293 register_console(&sclp_console);
297 console_initcall(sclp_console_init);