sclp_con.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/timer.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/termios.h>
  15. #include <linux/err.h>
  16. #include <linux/reboot.h>
  17. #include <linux/gfp.h>
  18. #include "sclp.h"
  19. #include "sclp_rw.h"
  20. #include "sclp_tty.h"
  21. #define sclp_console_major 4 /* TTYAUX_MAJOR */
  22. #define sclp_console_minor 64
  23. #define sclp_console_name "ttyS"
  24. /* Lock to guard over changes to global variables */
  25. static spinlock_t sclp_con_lock;
  26. /* List of free pages that can be used for console output buffering */
  27. static struct list_head sclp_con_pages;
  28. /* List of full struct sclp_buffer structures ready for output */
  29. static struct list_head sclp_con_outqueue;
  30. /* Pointer to current console buffer */
  31. static struct sclp_buffer *sclp_conbuf;
  32. /* Timer for delayed output of console messages */
  33. static struct timer_list sclp_con_timer;
  34. /* Suspend mode flag */
  35. static int sclp_con_suspended;
  36. /* Flag that output queue is currently running */
  37. static int sclp_con_queue_running;
  38. /* Output format for console messages */
  39. static unsigned short sclp_con_columns;
  40. static unsigned short sclp_con_width_htab;
  41. static void
  42. sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
  43. {
  44. unsigned long flags;
  45. void *page;
  46. do {
  47. page = sclp_unmake_buffer(buffer);
  48. spin_lock_irqsave(&sclp_con_lock, flags);
  49. /* Remove buffer from outqueue */
  50. list_del(&buffer->list);
  51. list_add_tail((struct list_head *) page, &sclp_con_pages);
  52. /* Check if there is a pending buffer on the out queue. */
  53. buffer = NULL;
  54. if (!list_empty(&sclp_con_outqueue))
  55. buffer = list_first_entry(&sclp_con_outqueue,
  56. struct sclp_buffer, list);
  57. if (!buffer || sclp_con_suspended) {
  58. sclp_con_queue_running = 0;
  59. spin_unlock_irqrestore(&sclp_con_lock, flags);
  60. break;
  61. }
  62. spin_unlock_irqrestore(&sclp_con_lock, flags);
  63. } while (sclp_emit_buffer(buffer, sclp_conbuf_callback));
  64. }
  65. /*
  66. * Finalize and emit first pending buffer.
  67. */
  68. static void sclp_conbuf_emit(void)
  69. {
  70. struct sclp_buffer* buffer;
  71. unsigned long flags;
  72. int rc;
  73. spin_lock_irqsave(&sclp_con_lock, flags);
  74. if (sclp_conbuf)
  75. list_add_tail(&sclp_conbuf->list, &sclp_con_outqueue);
  76. sclp_conbuf = NULL;
  77. if (sclp_con_queue_running || sclp_con_suspended)
  78. goto out_unlock;
  79. if (list_empty(&sclp_con_outqueue))
  80. goto out_unlock;
  81. buffer = list_first_entry(&sclp_con_outqueue, struct sclp_buffer,
  82. list);
  83. sclp_con_queue_running = 1;
  84. spin_unlock_irqrestore(&sclp_con_lock, flags);
  85. rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
  86. if (rc)
  87. sclp_conbuf_callback(buffer, rc);
  88. return;
  89. out_unlock:
  90. spin_unlock_irqrestore(&sclp_con_lock, flags);
  91. }
  92. /*
  93. * Wait until out queue is empty
  94. */
  95. static void sclp_console_sync_queue(void)
  96. {
  97. unsigned long flags;
  98. spin_lock_irqsave(&sclp_con_lock, flags);
  99. if (timer_pending(&sclp_con_timer))
  100. del_timer(&sclp_con_timer);
  101. while (sclp_con_queue_running) {
  102. spin_unlock_irqrestore(&sclp_con_lock, flags);
  103. sclp_sync_wait();
  104. spin_lock_irqsave(&sclp_con_lock, flags);
  105. }
  106. spin_unlock_irqrestore(&sclp_con_lock, flags);
  107. }
  108. /*
  109. * When this routine is called from the timer then we flush the
  110. * temporary write buffer without further waiting on a final new line.
  111. */
  112. static void
  113. sclp_console_timeout(struct timer_list *unused)
  114. {
  115. sclp_conbuf_emit();
  116. }
  117. /*
  118. * Drop oldest console buffer if sclp_con_drop is set
  119. */
  120. static int
  121. sclp_console_drop_buffer(void)
  122. {
  123. struct list_head *list;
  124. struct sclp_buffer *buffer;
  125. void *page;
  126. if (!sclp_console_drop)
  127. return 0;
  128. list = sclp_con_outqueue.next;
  129. if (sclp_con_queue_running)
  130. /* The first element is in I/O */
  131. list = list->next;
  132. if (list == &sclp_con_outqueue)
  133. return 0;
  134. list_del(list);
  135. buffer = list_entry(list, struct sclp_buffer, list);
  136. page = sclp_unmake_buffer(buffer);
  137. list_add_tail((struct list_head *) page, &sclp_con_pages);
  138. return 1;
  139. }
  140. /*
  141. * Writes the given message to S390 system console
  142. */
  143. static void
  144. sclp_console_write(struct console *console, const char *message,
  145. unsigned int count)
  146. {
  147. unsigned long flags;
  148. void *page;
  149. int written;
  150. if (count == 0)
  151. return;
  152. spin_lock_irqsave(&sclp_con_lock, flags);
  153. /*
  154. * process escape characters, write message into buffer,
  155. * send buffer to SCLP
  156. */
  157. do {
  158. /* make sure we have a console output buffer */
  159. if (sclp_conbuf == NULL) {
  160. if (list_empty(&sclp_con_pages))
  161. sclp_console_full++;
  162. while (list_empty(&sclp_con_pages)) {
  163. if (sclp_con_suspended)
  164. goto out;
  165. if (sclp_console_drop_buffer())
  166. break;
  167. spin_unlock_irqrestore(&sclp_con_lock, flags);
  168. sclp_sync_wait();
  169. spin_lock_irqsave(&sclp_con_lock, flags);
  170. }
  171. page = sclp_con_pages.next;
  172. list_del((struct list_head *) page);
  173. sclp_conbuf = sclp_make_buffer(page, sclp_con_columns,
  174. sclp_con_width_htab);
  175. }
  176. /* try to write the string to the current output buffer */
  177. written = sclp_write(sclp_conbuf, (const unsigned char *)
  178. message, count);
  179. if (written == count)
  180. break;
  181. /*
  182. * Not all characters could be written to the current
  183. * output buffer. Emit the buffer, create a new buffer
  184. * and then output the rest of the string.
  185. */
  186. spin_unlock_irqrestore(&sclp_con_lock, flags);
  187. sclp_conbuf_emit();
  188. spin_lock_irqsave(&sclp_con_lock, flags);
  189. message += written;
  190. count -= written;
  191. } while (count > 0);
  192. /* Setup timer to output current console buffer after 1/10 second */
  193. if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
  194. !timer_pending(&sclp_con_timer)) {
  195. mod_timer(&sclp_con_timer, jiffies + HZ / 10);
  196. }
  197. out:
  198. spin_unlock_irqrestore(&sclp_con_lock, flags);
  199. }
  200. static struct tty_driver *
  201. sclp_console_device(struct console *c, int *index)
  202. {
  203. *index = c->index;
  204. return sclp_tty_driver;
  205. }
  206. /*
  207. * Make sure that all buffers will be flushed to the SCLP.
  208. */
  209. static void
  210. sclp_console_flush(void)
  211. {
  212. sclp_conbuf_emit();
  213. sclp_console_sync_queue();
  214. }
  215. /*
  216. * Resume console: If there are cached messages, emit them.
  217. */
  218. static void sclp_console_resume(void)
  219. {
  220. unsigned long flags;
  221. spin_lock_irqsave(&sclp_con_lock, flags);
  222. sclp_con_suspended = 0;
  223. spin_unlock_irqrestore(&sclp_con_lock, flags);
  224. sclp_conbuf_emit();
  225. }
  226. /*
  227. * Suspend console: Set suspend flag and flush console
  228. */
  229. static void sclp_console_suspend(void)
  230. {
  231. unsigned long flags;
  232. spin_lock_irqsave(&sclp_con_lock, flags);
  233. sclp_con_suspended = 1;
  234. spin_unlock_irqrestore(&sclp_con_lock, flags);
  235. sclp_console_flush();
  236. }
  237. static int sclp_console_notify(struct notifier_block *self,
  238. unsigned long event, void *data)
  239. {
  240. sclp_console_flush();
  241. return NOTIFY_OK;
  242. }
  243. static struct notifier_block on_panic_nb = {
  244. .notifier_call = sclp_console_notify,
  245. .priority = SCLP_PANIC_PRIO_CLIENT,
  246. };
  247. static struct notifier_block on_reboot_nb = {
  248. .notifier_call = sclp_console_notify,
  249. .priority = 1,
  250. };
  251. /*
  252. * used to register the SCLP console to the kernel and to
  253. * give printk necessary information
  254. */
  255. static struct console sclp_console =
  256. {
  257. .name = sclp_console_name,
  258. .write = sclp_console_write,
  259. .device = sclp_console_device,
  260. .flags = CON_PRINTBUFFER,
  261. .index = 0 /* ttyS0 */
  262. };
  263. /*
  264. * This function is called for SCLP suspend and resume events.
  265. */
  266. void sclp_console_pm_event(enum sclp_pm_event sclp_pm_event)
  267. {
  268. switch (sclp_pm_event) {
  269. case SCLP_PM_EVENT_FREEZE:
  270. sclp_console_suspend();
  271. break;
  272. case SCLP_PM_EVENT_RESTORE:
  273. case SCLP_PM_EVENT_THAW:
  274. sclp_console_resume();
  275. break;
  276. }
  277. }
  278. /*
  279. * called by console_init() in drivers/char/tty_io.c at boot-time.
  280. */
  281. static int __init
  282. sclp_console_init(void)
  283. {
  284. void *page;
  285. int i;
  286. int rc;
  287. /* SCLP consoles are handled together */
  288. if (!(CONSOLE_IS_SCLP || CONSOLE_IS_VT220))
  289. return 0;
  290. rc = sclp_rw_init();
  291. if (rc)
  292. return rc;
  293. /* Allocate pages for output buffering */
  294. INIT_LIST_HEAD(&sclp_con_pages);
  295. for (i = 0; i < sclp_console_pages; i++) {
  296. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  297. list_add_tail(page, &sclp_con_pages);
  298. }
  299. INIT_LIST_HEAD(&sclp_con_outqueue);
  300. spin_lock_init(&sclp_con_lock);
  301. sclp_conbuf = NULL;
  302. timer_setup(&sclp_con_timer, sclp_console_timeout, 0);
  303. /* Set output format */
  304. if (MACHINE_IS_VM)
  305. /*
  306. * save 4 characters for the CPU number
  307. * written at start of each line by VM/CP
  308. */
  309. sclp_con_columns = 76;
  310. else
  311. sclp_con_columns = 80;
  312. sclp_con_width_htab = 8;
  313. /* enable printk-access to this driver */
  314. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  315. register_reboot_notifier(&on_reboot_nb);
  316. register_console(&sclp_console);
  317. return 0;
  318. }
  319. console_initcall(sclp_console_init);