sclp_con.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * drivers/s390/char/sclp_con.c
  3. * SCLP line mode console driver
  4. *
  5. * S390 version
  6. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  8. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. */
  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/bootmem.h>
  16. #include <linux/termios.h>
  17. #include <linux/err.h>
  18. #include <linux/reboot.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 spinlock_t sclp_con_lock;
  27. /* List of free pages that can be used for console output buffering */
  28. static struct list_head sclp_con_pages;
  29. /* List of full struct sclp_buffer structures ready for output */
  30. static struct list_head sclp_con_outqueue;
  31. /* Counter how many buffers are emitted (max 1) and how many */
  32. /* are on the output queue. */
  33. static int sclp_con_buffer_count;
  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. /* 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. sclp_con_buffer_count--;
  52. list_add_tail((struct list_head *) page, &sclp_con_pages);
  53. /* Check if there is a pending buffer on the out queue. */
  54. buffer = NULL;
  55. if (!list_empty(&sclp_con_outqueue))
  56. buffer = list_entry(sclp_con_outqueue.next,
  57. struct sclp_buffer, list);
  58. spin_unlock_irqrestore(&sclp_con_lock, flags);
  59. } while (buffer && sclp_emit_buffer(buffer, sclp_conbuf_callback));
  60. }
  61. static void
  62. sclp_conbuf_emit(void)
  63. {
  64. struct sclp_buffer* buffer;
  65. unsigned long flags;
  66. int count;
  67. int rc;
  68. spin_lock_irqsave(&sclp_con_lock, flags);
  69. buffer = sclp_conbuf;
  70. sclp_conbuf = NULL;
  71. if (buffer == NULL) {
  72. spin_unlock_irqrestore(&sclp_con_lock, flags);
  73. return;
  74. }
  75. list_add_tail(&buffer->list, &sclp_con_outqueue);
  76. count = sclp_con_buffer_count++;
  77. spin_unlock_irqrestore(&sclp_con_lock, flags);
  78. if (count)
  79. return;
  80. rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
  81. if (rc)
  82. sclp_conbuf_callback(buffer, rc);
  83. }
  84. /*
  85. * When this routine is called from the timer then we flush the
  86. * temporary write buffer without further waiting on a final new line.
  87. */
  88. static void
  89. sclp_console_timeout(unsigned long data)
  90. {
  91. sclp_conbuf_emit();
  92. }
  93. /*
  94. * Writes the given message to S390 system console
  95. */
  96. static void
  97. sclp_console_write(struct console *console, const char *message,
  98. unsigned int count)
  99. {
  100. unsigned long flags;
  101. void *page;
  102. int written;
  103. if (count == 0)
  104. return;
  105. spin_lock_irqsave(&sclp_con_lock, flags);
  106. /*
  107. * process escape characters, write message into buffer,
  108. * send buffer to SCLP
  109. */
  110. do {
  111. /* make sure we have a console output buffer */
  112. if (sclp_conbuf == NULL) {
  113. while (list_empty(&sclp_con_pages)) {
  114. spin_unlock_irqrestore(&sclp_con_lock, flags);
  115. sclp_sync_wait();
  116. spin_lock_irqsave(&sclp_con_lock, flags);
  117. }
  118. page = sclp_con_pages.next;
  119. list_del((struct list_head *) page);
  120. sclp_conbuf = sclp_make_buffer(page, sclp_con_columns,
  121. sclp_con_width_htab);
  122. }
  123. /* try to write the string to the current output buffer */
  124. written = sclp_write(sclp_conbuf, (const unsigned char *)
  125. message, count);
  126. if (written == count)
  127. break;
  128. /*
  129. * Not all characters could be written to the current
  130. * output buffer. Emit the buffer, create a new buffer
  131. * and then output the rest of the string.
  132. */
  133. spin_unlock_irqrestore(&sclp_con_lock, flags);
  134. sclp_conbuf_emit();
  135. spin_lock_irqsave(&sclp_con_lock, flags);
  136. message += written;
  137. count -= written;
  138. } while (count > 0);
  139. /* Setup timer to output current console buffer after 1/10 second */
  140. if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
  141. !timer_pending(&sclp_con_timer)) {
  142. init_timer(&sclp_con_timer);
  143. sclp_con_timer.function = sclp_console_timeout;
  144. sclp_con_timer.data = 0UL;
  145. sclp_con_timer.expires = jiffies + HZ/10;
  146. add_timer(&sclp_con_timer);
  147. }
  148. spin_unlock_irqrestore(&sclp_con_lock, flags);
  149. }
  150. static struct tty_driver *
  151. sclp_console_device(struct console *c, int *index)
  152. {
  153. *index = c->index;
  154. return sclp_tty_driver;
  155. }
  156. /*
  157. * This routine is called from panic when the kernel
  158. * is going to give up. We have to make sure that all buffers
  159. * will be flushed to the SCLP.
  160. */
  161. static void
  162. sclp_console_flush(void)
  163. {
  164. unsigned long flags;
  165. sclp_conbuf_emit();
  166. spin_lock_irqsave(&sclp_con_lock, flags);
  167. if (timer_pending(&sclp_con_timer))
  168. del_timer(&sclp_con_timer);
  169. while (sclp_con_buffer_count > 0) {
  170. spin_unlock_irqrestore(&sclp_con_lock, flags);
  171. sclp_sync_wait();
  172. spin_lock_irqsave(&sclp_con_lock, flags);
  173. }
  174. spin_unlock_irqrestore(&sclp_con_lock, flags);
  175. }
  176. static int
  177. sclp_console_notify(struct notifier_block *self,
  178. unsigned long event, void *data)
  179. {
  180. sclp_console_flush();
  181. return NOTIFY_OK;
  182. }
  183. static struct notifier_block on_panic_nb = {
  184. .notifier_call = sclp_console_notify,
  185. .priority = 1,
  186. };
  187. static struct notifier_block on_reboot_nb = {
  188. .notifier_call = sclp_console_notify,
  189. .priority = 1,
  190. };
  191. /*
  192. * used to register the SCLP console to the kernel and to
  193. * give printk necessary information
  194. */
  195. static struct console sclp_console =
  196. {
  197. .name = sclp_console_name,
  198. .write = sclp_console_write,
  199. .device = sclp_console_device,
  200. .flags = CON_PRINTBUFFER,
  201. .index = 0 /* ttyS0 */
  202. };
  203. /*
  204. * called by console_init() in drivers/char/tty_io.c at boot-time.
  205. */
  206. static int __init
  207. sclp_console_init(void)
  208. {
  209. void *page;
  210. int i;
  211. int rc;
  212. if (!CONSOLE_IS_SCLP)
  213. return 0;
  214. rc = sclp_rw_init();
  215. if (rc)
  216. return rc;
  217. /* Allocate pages for output buffering */
  218. INIT_LIST_HEAD(&sclp_con_pages);
  219. for (i = 0; i < MAX_CONSOLE_PAGES; i++) {
  220. page = alloc_bootmem_low_pages(PAGE_SIZE);
  221. list_add_tail((struct list_head *) page, &sclp_con_pages);
  222. }
  223. INIT_LIST_HEAD(&sclp_con_outqueue);
  224. spin_lock_init(&sclp_con_lock);
  225. sclp_con_buffer_count = 0;
  226. sclp_conbuf = NULL;
  227. init_timer(&sclp_con_timer);
  228. /* Set output format */
  229. if (MACHINE_IS_VM)
  230. /*
  231. * save 4 characters for the CPU number
  232. * written at start of each line by VM/CP
  233. */
  234. sclp_con_columns = 76;
  235. else
  236. sclp_con_columns = 80;
  237. sclp_con_width_htab = 8;
  238. /* enable printk-access to this driver */
  239. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  240. register_reboot_notifier(&on_reboot_nb);
  241. register_console(&sclp_console);
  242. return 0;
  243. }
  244. console_initcall(sclp_console_init);