sclp_con.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <linux/gfp.h>
  17. #include "sclp.h"
  18. #include "sclp_rw.h"
  19. #include "sclp_tty.h"
  20. #define sclp_console_major 4 /* TTYAUX_MAJOR */
  21. #define sclp_console_minor 64
  22. #define sclp_console_name "ttyS"
  23. /* Lock to guard over changes to global variables */
  24. static spinlock_t sclp_con_lock;
  25. /* List of free pages that can be used for console output buffering */
  26. static struct list_head sclp_con_pages;
  27. /* List of full struct sclp_buffer structures ready for output */
  28. static struct list_head sclp_con_outqueue;
  29. /* Pointer to current console buffer */
  30. static struct sclp_buffer *sclp_conbuf;
  31. /* Timer for delayed output of console messages */
  32. static struct timer_list sclp_con_timer;
  33. /* Suspend mode flag */
  34. static int sclp_con_suspended;
  35. /* Flag that output queue is currently running */
  36. static int sclp_con_queue_running;
  37. /* Output format for console messages */
  38. static unsigned short sclp_con_columns;
  39. static unsigned short sclp_con_width_htab;
  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 || sclp_con_suspended) {
  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 || sclp_con_suspended)
  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. if (timer_pending(&sclp_con_timer))
  99. del_timer(&sclp_con_timer);
  100. while (sclp_con_queue_running) {
  101. spin_unlock_irqrestore(&sclp_con_lock, flags);
  102. sclp_sync_wait();
  103. spin_lock_irqsave(&sclp_con_lock, flags);
  104. }
  105. spin_unlock_irqrestore(&sclp_con_lock, flags);
  106. }
  107. /*
  108. * When this routine is called from the timer then we flush the
  109. * temporary write buffer without further waiting on a final new line.
  110. */
  111. static void
  112. sclp_console_timeout(unsigned long data)
  113. {
  114. sclp_conbuf_emit();
  115. }
  116. /*
  117. * Writes the given message to S390 system console
  118. */
  119. static void
  120. sclp_console_write(struct console *console, const char *message,
  121. unsigned int count)
  122. {
  123. unsigned long flags;
  124. void *page;
  125. int written;
  126. if (count == 0)
  127. return;
  128. spin_lock_irqsave(&sclp_con_lock, flags);
  129. /*
  130. * process escape characters, write message into buffer,
  131. * send buffer to SCLP
  132. */
  133. do {
  134. /* make sure we have a console output buffer */
  135. if (sclp_conbuf == NULL) {
  136. while (list_empty(&sclp_con_pages)) {
  137. if (sclp_con_suspended)
  138. goto out;
  139. spin_unlock_irqrestore(&sclp_con_lock, flags);
  140. sclp_sync_wait();
  141. spin_lock_irqsave(&sclp_con_lock, flags);
  142. }
  143. page = sclp_con_pages.next;
  144. list_del((struct list_head *) page);
  145. sclp_conbuf = sclp_make_buffer(page, sclp_con_columns,
  146. sclp_con_width_htab);
  147. }
  148. /* try to write the string to the current output buffer */
  149. written = sclp_write(sclp_conbuf, (const unsigned char *)
  150. message, count);
  151. if (written == count)
  152. break;
  153. /*
  154. * Not all characters could be written to the current
  155. * output buffer. Emit the buffer, create a new buffer
  156. * and then output the rest of the string.
  157. */
  158. spin_unlock_irqrestore(&sclp_con_lock, flags);
  159. sclp_conbuf_emit();
  160. spin_lock_irqsave(&sclp_con_lock, flags);
  161. message += written;
  162. count -= written;
  163. } while (count > 0);
  164. /* Setup timer to output current console buffer after 1/10 second */
  165. if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
  166. !timer_pending(&sclp_con_timer)) {
  167. init_timer(&sclp_con_timer);
  168. sclp_con_timer.function = sclp_console_timeout;
  169. sclp_con_timer.data = 0UL;
  170. sclp_con_timer.expires = jiffies + HZ/10;
  171. add_timer(&sclp_con_timer);
  172. }
  173. out:
  174. spin_unlock_irqrestore(&sclp_con_lock, flags);
  175. }
  176. static struct tty_driver *
  177. sclp_console_device(struct console *c, int *index)
  178. {
  179. *index = c->index;
  180. return sclp_tty_driver;
  181. }
  182. /*
  183. * Make sure that all buffers will be flushed to the SCLP.
  184. */
  185. static void
  186. sclp_console_flush(void)
  187. {
  188. sclp_conbuf_emit();
  189. sclp_console_sync_queue();
  190. }
  191. /*
  192. * Resume console: If there are cached messages, emit them.
  193. */
  194. static void sclp_console_resume(void)
  195. {
  196. unsigned long flags;
  197. spin_lock_irqsave(&sclp_con_lock, flags);
  198. sclp_con_suspended = 0;
  199. spin_unlock_irqrestore(&sclp_con_lock, flags);
  200. sclp_conbuf_emit();
  201. }
  202. /*
  203. * Suspend console: Set suspend flag and flush console
  204. */
  205. static void sclp_console_suspend(void)
  206. {
  207. unsigned long flags;
  208. spin_lock_irqsave(&sclp_con_lock, flags);
  209. sclp_con_suspended = 1;
  210. spin_unlock_irqrestore(&sclp_con_lock, flags);
  211. sclp_console_flush();
  212. }
  213. static int sclp_console_notify(struct notifier_block *self,
  214. unsigned long event, void *data)
  215. {
  216. sclp_console_flush();
  217. return NOTIFY_OK;
  218. }
  219. static struct notifier_block on_panic_nb = {
  220. .notifier_call = sclp_console_notify,
  221. .priority = SCLP_PANIC_PRIO_CLIENT,
  222. };
  223. static struct notifier_block on_reboot_nb = {
  224. .notifier_call = sclp_console_notify,
  225. .priority = 1,
  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. * This function is called for SCLP suspend and resume events.
  241. */
  242. void sclp_console_pm_event(enum sclp_pm_event sclp_pm_event)
  243. {
  244. switch (sclp_pm_event) {
  245. case SCLP_PM_EVENT_FREEZE:
  246. sclp_console_suspend();
  247. break;
  248. case SCLP_PM_EVENT_RESTORE:
  249. case SCLP_PM_EVENT_THAW:
  250. sclp_console_resume();
  251. break;
  252. }
  253. }
  254. /*
  255. * called by console_init() in drivers/char/tty_io.c at boot-time.
  256. */
  257. static int __init
  258. sclp_console_init(void)
  259. {
  260. void *page;
  261. int i;
  262. int rc;
  263. if (!CONSOLE_IS_SCLP)
  264. return 0;
  265. rc = sclp_rw_init();
  266. if (rc)
  267. return rc;
  268. /* Allocate pages for output buffering */
  269. INIT_LIST_HEAD(&sclp_con_pages);
  270. for (i = 0; i < MAX_CONSOLE_PAGES; i++) {
  271. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  272. list_add_tail(page, &sclp_con_pages);
  273. }
  274. INIT_LIST_HEAD(&sclp_con_outqueue);
  275. spin_lock_init(&sclp_con_lock);
  276. sclp_conbuf = NULL;
  277. init_timer(&sclp_con_timer);
  278. /* Set output format */
  279. if (MACHINE_IS_VM)
  280. /*
  281. * save 4 characters for the CPU number
  282. * written at start of each line by VM/CP
  283. */
  284. sclp_con_columns = 76;
  285. else
  286. sclp_con_columns = 80;
  287. sclp_con_width_htab = 8;
  288. /* enable printk-access to this driver */
  289. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  290. register_reboot_notifier(&on_reboot_nb);
  291. register_console(&sclp_console);
  292. return 0;
  293. }
  294. console_initcall(sclp_console_init);