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/timer.h>
14 #include <linux/jiffies.h>
15 #include <linux/termios.h>
16 #include <linux/err.h>
17 #include <linux/reboot.h>
18 #include <linux/gfp.h>
24 #define sclp_console_major 4 /* TTYAUX_MAJOR */
25 #define sclp_console_minor 64
26 #define sclp_console_name "ttyS"
28 /* Lock to guard over changes to global variables */
29 static spinlock_t sclp_con_lock;
30 /* List of free pages that can be used for console output buffering */
31 static struct list_head sclp_con_pages;
32 /* List of full struct sclp_buffer structures ready for output */
33 static struct list_head sclp_con_outqueue;
34 /* Pointer to current console buffer */
35 static struct sclp_buffer *sclp_conbuf;
36 /* Timer for delayed output of console messages */
37 static struct timer_list sclp_con_timer;
38 /* Suspend mode flag */
39 static int sclp_con_suspended;
40 /* Flag that output queue is currently running */
41 static int sclp_con_queue_running;
43 /* Output format for console messages */
44 static unsigned short sclp_con_columns;
45 static unsigned short sclp_con_width_htab;
48 sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
54 page = sclp_unmake_buffer(buffer);
55 spin_lock_irqsave(&sclp_con_lock, flags);
57 /* Remove buffer from outqueue */
58 list_del(&buffer->list);
59 list_add_tail((struct list_head *) page, &sclp_con_pages);
61 /* Check if there is a pending buffer on the out queue. */
63 if (!list_empty(&sclp_con_outqueue))
64 buffer = list_first_entry(&sclp_con_outqueue,
65 struct sclp_buffer, list);
66 if (!buffer || sclp_con_suspended) {
67 sclp_con_queue_running = 0;
68 spin_unlock_irqrestore(&sclp_con_lock, flags);
71 spin_unlock_irqrestore(&sclp_con_lock, flags);
72 } while (sclp_emit_buffer(buffer, sclp_conbuf_callback));
76 * Finalize and emit first pending buffer.
78 static void sclp_conbuf_emit(void)
80 struct sclp_buffer* buffer;
84 spin_lock_irqsave(&sclp_con_lock, flags);
86 list_add_tail(&sclp_conbuf->list, &sclp_con_outqueue);
88 if (sclp_con_queue_running || sclp_con_suspended)
90 if (list_empty(&sclp_con_outqueue))
92 buffer = list_first_entry(&sclp_con_outqueue, struct sclp_buffer,
94 sclp_con_queue_running = 1;
95 spin_unlock_irqrestore(&sclp_con_lock, flags);
97 rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
99 sclp_conbuf_callback(buffer, rc);
102 spin_unlock_irqrestore(&sclp_con_lock, flags);
106 * Wait until out queue is empty
108 static void sclp_console_sync_queue(void)
112 spin_lock_irqsave(&sclp_con_lock, flags);
113 if (timer_pending(&sclp_con_timer))
114 del_timer(&sclp_con_timer);
115 while (sclp_con_queue_running) {
116 spin_unlock_irqrestore(&sclp_con_lock, flags);
118 spin_lock_irqsave(&sclp_con_lock, flags);
120 spin_unlock_irqrestore(&sclp_con_lock, flags);
124 * When this routine is called from the timer then we flush the
125 * temporary write buffer without further waiting on a final new line.
128 sclp_console_timeout(struct timer_list *unused)
134 * Drop oldest console buffer if sclp_con_drop is set
137 sclp_console_drop_buffer(void)
139 struct list_head *list;
140 struct sclp_buffer *buffer;
143 if (!sclp_console_drop)
145 list = sclp_con_outqueue.next;
146 if (sclp_con_queue_running)
147 /* The first element is in I/O */
149 if (list == &sclp_con_outqueue)
152 buffer = list_entry(list, struct sclp_buffer, list);
153 page = sclp_unmake_buffer(buffer);
154 list_add_tail((struct list_head *) page, &sclp_con_pages);
159 * Writes the given message to S390 system console
162 sclp_console_write(struct console *console, const char *message,
171 spin_lock_irqsave(&sclp_con_lock, flags);
173 * process escape characters, write message into buffer,
174 * send buffer to SCLP
177 /* make sure we have a console output buffer */
178 if (sclp_conbuf == NULL) {
179 if (list_empty(&sclp_con_pages))
181 while (list_empty(&sclp_con_pages)) {
182 if (sclp_con_suspended)
184 if (sclp_console_drop_buffer())
186 spin_unlock_irqrestore(&sclp_con_lock, flags);
188 spin_lock_irqsave(&sclp_con_lock, flags);
190 page = sclp_con_pages.next;
191 list_del((struct list_head *) page);
192 sclp_conbuf = sclp_make_buffer(page, sclp_con_columns,
193 sclp_con_width_htab);
195 /* try to write the string to the current output buffer */
196 written = sclp_write(sclp_conbuf, (const unsigned char *)
198 if (written == count)
201 * Not all characters could be written to the current
202 * output buffer. Emit the buffer, create a new buffer
203 * and then output the rest of the string.
205 spin_unlock_irqrestore(&sclp_con_lock, flags);
207 spin_lock_irqsave(&sclp_con_lock, flags);
211 /* Setup timer to output current console buffer after 1/10 second */
212 if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
213 !timer_pending(&sclp_con_timer)) {
214 mod_timer(&sclp_con_timer, jiffies + HZ / 10);
217 spin_unlock_irqrestore(&sclp_con_lock, flags);
220 static struct tty_driver *
221 sclp_console_device(struct console *c, int *index)
224 return sclp_tty_driver;
228 * Make sure that all buffers will be flushed to the SCLP.
231 sclp_console_flush(void)
234 sclp_console_sync_queue();
238 * Resume console: If there are cached messages, emit them.
240 static void sclp_console_resume(void)
244 spin_lock_irqsave(&sclp_con_lock, flags);
245 sclp_con_suspended = 0;
246 spin_unlock_irqrestore(&sclp_con_lock, flags);
251 * Suspend console: Set suspend flag and flush console
253 static void sclp_console_suspend(void)
257 spin_lock_irqsave(&sclp_con_lock, flags);
258 sclp_con_suspended = 1;
259 spin_unlock_irqrestore(&sclp_con_lock, flags);
260 sclp_console_flush();
263 static int sclp_console_notify(struct notifier_block *self,
264 unsigned long event, void *data)
266 sclp_console_flush();
270 static struct notifier_block on_panic_nb = {
271 .notifier_call = sclp_console_notify,
272 .priority = SCLP_PANIC_PRIO_CLIENT,
275 static struct notifier_block on_reboot_nb = {
276 .notifier_call = sclp_console_notify,
281 * used to register the SCLP console to the kernel and to
282 * give printk necessary information
284 static struct console sclp_console =
286 .name = sclp_console_name,
287 .write = sclp_console_write,
288 .device = sclp_console_device,
289 .flags = CON_PRINTBUFFER,
290 .index = 0 /* ttyS0 */
294 * This function is called for SCLP suspend and resume events.
296 void sclp_console_pm_event(enum sclp_pm_event sclp_pm_event)
298 switch (sclp_pm_event) {
299 case SCLP_PM_EVENT_FREEZE:
300 sclp_console_suspend();
302 case SCLP_PM_EVENT_RESTORE:
303 case SCLP_PM_EVENT_THAW:
304 sclp_console_resume();
310 * called by console_init() in drivers/char/tty_io.c at boot-time.
313 sclp_console_init(void)
319 /* SCLP consoles are handled together */
320 if (!(CONSOLE_IS_SCLP || CONSOLE_IS_VT220))
325 /* Allocate pages for output buffering */
326 INIT_LIST_HEAD(&sclp_con_pages);
327 for (i = 0; i < sclp_console_pages; i++) {
328 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
329 list_add_tail(page, &sclp_con_pages);
331 INIT_LIST_HEAD(&sclp_con_outqueue);
332 spin_lock_init(&sclp_con_lock);
334 timer_setup(&sclp_con_timer, sclp_console_timeout, 0);
336 /* Set output format */
339 * save 4 characters for the CPU number
340 * written at start of each line by VM/CP
342 sclp_con_columns = 76;
344 sclp_con_columns = 80;
345 sclp_con_width_htab = 8;
347 /* enable printk-access to this driver */
348 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
349 register_reboot_notifier(&on_reboot_nb);
350 register_console(&sclp_console);
354 console_initcall(sclp_console_init);