sclp_con.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SCLP line mode console driver
  4. *
  5. * Copyright IBM Corp. 1999, 2009
  6. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  7. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. */
  9. #include <linux/kmod.h>
  10. #include <linux/console.h>
  11. #include <linux/init.h>
  12. #include <linux/panic_notifier.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>
  19. #include "sclp.h"
  20. #include "sclp_rw.h"
  21. #include "sclp_tty.h"
  22. #define sclp_console_major 4 /* TTYAUX_MAJOR */
  23. #define sclp_console_minor 64
  24. #define sclp_console_name "ttyS"
  25. /* Lock to guard over changes to global variables */
  26. static DEFINE_SPINLOCK(sclp_con_lock);
  27. /* List of free pages that can be used for console output buffering */
  28. static LIST_HEAD(sclp_con_pages);
  29. /* List of full struct sclp_buffer structures ready for output */
  30. static LIST_HEAD(sclp_con_outqueue);
  31. /* Pointer to current console buffer */
  32. static struct sclp_buffer *sclp_conbuf;
  33. /* Timer for delayed output of console messages */
  34. static struct timer_list sclp_con_timer;
  35. /* Flag that output queue is currently running */
  36. static int sclp_con_queue_running;
  37. /* Output format for console messages */
  38. #define SCLP_CON_COLUMNS 320
  39. #define SPACES_PER_TAB 8
  40. static void
  41. sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
  42. {
  43. unsigned long flags;
  44. void *page;
  45. do {
  46. page = sclp_unmake_buffer(buffer);
  47. spin_lock_irqsave(&sclp_con_lock, flags);
  48. /* Remove buffer from outqueue */
  49. list_del(&buffer->list);
  50. list_add_tail((struct list_head *) page, &sclp_con_pages);
  51. /* Check if there is a pending buffer on the out queue. */
  52. buffer = NULL;
  53. if (!list_empty(&sclp_con_outqueue))
  54. buffer = list_first_entry(&sclp_con_outqueue,
  55. struct sclp_buffer, list);
  56. if (!buffer) {
  57. sclp_con_queue_running = 0;
  58. spin_unlock_irqrestore(&sclp_con_lock, flags);
  59. break;
  60. }
  61. spin_unlock_irqrestore(&sclp_con_lock, flags);
  62. } while (sclp_emit_buffer(buffer, sclp_conbuf_callback));
  63. }
  64. /*
  65. * Finalize and emit first pending buffer.
  66. */
  67. static void sclp_conbuf_emit(void)
  68. {
  69. struct sclp_buffer* buffer;
  70. unsigned long flags;
  71. int rc;
  72. spin_lock_irqsave(&sclp_con_lock, flags);
  73. if (sclp_conbuf)
  74. list_add_tail(&sclp_conbuf->list, &sclp_con_outqueue);
  75. sclp_conbuf = NULL;
  76. if (sclp_con_queue_running)
  77. goto out_unlock;
  78. if (list_empty(&sclp_con_outqueue))
  79. goto out_unlock;
  80. buffer = list_first_entry(&sclp_con_outqueue, struct sclp_buffer,
  81. list);
  82. sclp_con_queue_running = 1;
  83. spin_unlock_irqrestore(&sclp_con_lock, flags);
  84. rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
  85. if (rc)
  86. sclp_conbuf_callback(buffer, rc);
  87. return;
  88. out_unlock:
  89. spin_unlock_irqrestore(&sclp_con_lock, flags);
  90. }
  91. /*
  92. * Wait until out queue is empty
  93. */
  94. static void sclp_console_sync_queue(void)
  95. {
  96. unsigned long flags;
  97. spin_lock_irqsave(&sclp_con_lock, flags);
  98. del_timer(&sclp_con_timer);
  99. while (sclp_con_queue_running) {
  100. spin_unlock_irqrestore(&sclp_con_lock, flags);
  101. sclp_sync_wait();
  102. spin_lock_irqsave(&sclp_con_lock, flags);
  103. }
  104. spin_unlock_irqrestore(&sclp_con_lock, flags);
  105. }
  106. /*
  107. * When this routine is called from the timer then we flush the
  108. * temporary write buffer without further waiting on a final new line.
  109. */
  110. static void
  111. sclp_console_timeout(struct timer_list *unused)
  112. {
  113. sclp_conbuf_emit();
  114. }
  115. /*
  116. * Drop oldest console buffer if sclp_con_drop is set
  117. */
  118. static int
  119. sclp_console_drop_buffer(void)
  120. {
  121. struct list_head *list;
  122. struct sclp_buffer *buffer;
  123. void *page;
  124. if (!sclp_console_drop)
  125. return 0;
  126. list = sclp_con_outqueue.next;
  127. if (sclp_con_queue_running)
  128. /* The first element is in I/O */
  129. list = list->next;
  130. if (list == &sclp_con_outqueue)
  131. return 0;
  132. list_del(list);
  133. buffer = list_entry(list, struct sclp_buffer, list);
  134. page = sclp_unmake_buffer(buffer);
  135. list_add_tail((struct list_head *) page, &sclp_con_pages);
  136. return 1;
  137. }
  138. /*
  139. * Writes the given message to S390 system console
  140. */
  141. static void
  142. sclp_console_write(struct console *console, const char *message,
  143. unsigned int count)
  144. {
  145. unsigned long flags;
  146. void *page;
  147. int written;
  148. if (count == 0)
  149. return;
  150. spin_lock_irqsave(&sclp_con_lock, flags);
  151. /*
  152. * process escape characters, write message into buffer,
  153. * send buffer to SCLP
  154. */
  155. do {
  156. /* make sure we have a console output buffer */
  157. if (sclp_conbuf == NULL) {
  158. if (list_empty(&sclp_con_pages))
  159. sclp_console_full++;
  160. while (list_empty(&sclp_con_pages)) {
  161. if (sclp_console_drop_buffer())
  162. break;
  163. spin_unlock_irqrestore(&sclp_con_lock, flags);
  164. sclp_sync_wait();
  165. spin_lock_irqsave(&sclp_con_lock, flags);
  166. }
  167. page = sclp_con_pages.next;
  168. list_del((struct list_head *) page);
  169. sclp_conbuf = sclp_make_buffer(page, SCLP_CON_COLUMNS,
  170. SPACES_PER_TAB);
  171. }
  172. /* try to write the string to the current output buffer */
  173. written = sclp_write(sclp_conbuf, (const unsigned char *)
  174. message, count);
  175. if (written == count)
  176. break;
  177. /*
  178. * Not all characters could be written to the current
  179. * output buffer. Emit the buffer, create a new buffer
  180. * and then output the rest of the string.
  181. */
  182. spin_unlock_irqrestore(&sclp_con_lock, flags);
  183. sclp_conbuf_emit();
  184. spin_lock_irqsave(&sclp_con_lock, flags);
  185. message += written;
  186. count -= written;
  187. } while (count > 0);
  188. /* Setup timer to output current console buffer after 1/10 second */
  189. if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
  190. !timer_pending(&sclp_con_timer)) {
  191. mod_timer(&sclp_con_timer, jiffies + HZ / 10);
  192. }
  193. spin_unlock_irqrestore(&sclp_con_lock, flags);
  194. }
  195. static struct tty_driver *
  196. sclp_console_device(struct console *c, int *index)
  197. {
  198. *index = c->index;
  199. return sclp_tty_driver;
  200. }
  201. /*
  202. * This panic/reboot notifier makes sure that all buffers
  203. * will be flushed to the SCLP.
  204. */
  205. static int sclp_console_notify(struct notifier_block *self,
  206. unsigned long event, void *data)
  207. {
  208. /*
  209. * Perform the lock check before effectively getting the
  210. * lock on sclp_conbuf_emit() / sclp_console_sync_queue()
  211. * to prevent potential lockups in atomic context.
  212. */
  213. if (spin_is_locked(&sclp_con_lock))
  214. return NOTIFY_DONE;
  215. sclp_conbuf_emit();
  216. sclp_console_sync_queue();
  217. return NOTIFY_DONE;
  218. }
  219. static struct notifier_block on_panic_nb = {
  220. .notifier_call = sclp_console_notify,
  221. .priority = INT_MIN + 1, /* run the callback late */
  222. };
  223. static struct notifier_block on_reboot_nb = {
  224. .notifier_call = sclp_console_notify,
  225. .priority = INT_MIN + 1, /* run the callback late */
  226. };
  227. /*
  228. * used to register the SCLP console to the kernel and to
  229. * give printk necessary information
  230. */
  231. static struct console sclp_console =
  232. {
  233. .name = sclp_console_name,
  234. .write = sclp_console_write,
  235. .device = sclp_console_device,
  236. .flags = CON_PRINTBUFFER,
  237. .index = 0 /* ttyS0 */
  238. };
  239. /*
  240. * called by console_init() in drivers/char/tty_io.c at boot-time.
  241. */
  242. static int __init
  243. sclp_console_init(void)
  244. {
  245. void *page;
  246. int i;
  247. int rc;
  248. /* SCLP consoles are handled together */
  249. if (!(CONSOLE_IS_SCLP || CONSOLE_IS_VT220))
  250. return 0;
  251. rc = sclp_rw_init();
  252. if (rc)
  253. return rc;
  254. /* Allocate pages for output buffering */
  255. for (i = 0; i < sclp_console_pages; i++) {
  256. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  257. list_add_tail(page, &sclp_con_pages);
  258. }
  259. sclp_conbuf = NULL;
  260. timer_setup(&sclp_con_timer, sclp_console_timeout, 0);
  261. /* enable printk-access to this driver */
  262. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  263. register_reboot_notifier(&on_reboot_nb);
  264. register_console(&sclp_console);
  265. return 0;
  266. }
  267. console_initcall(sclp_console_init);