sclp_con.c 7.9 KB

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