hvc_dcc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2010, 2014, 2022 The Linux Foundation. All rights reserved. */
  3. #include <linux/console.h>
  4. #include <linux/cpu.h>
  5. #include <linux/cpumask.h>
  6. #include <linux/init.h>
  7. #include <linux/kfifo.h>
  8. #include <linux/serial.h>
  9. #include <linux/serial_core.h>
  10. #include <linux/smp.h>
  11. #include <linux/spinlock.h>
  12. #include <asm/dcc.h>
  13. #include <asm/processor.h>
  14. #include "hvc_console.h"
  15. /* DCC Status Bits */
  16. #define DCC_STATUS_RX (1 << 30)
  17. #define DCC_STATUS_TX (1 << 29)
  18. #define DCC_INBUF_SIZE 128
  19. #define DCC_OUTBUF_SIZE 1024
  20. /* Lock to serialize access to DCC fifo */
  21. static DEFINE_SPINLOCK(dcc_lock);
  22. static DEFINE_KFIFO(inbuf, u8, DCC_INBUF_SIZE);
  23. static DEFINE_KFIFO(outbuf, u8, DCC_OUTBUF_SIZE);
  24. static void dcc_uart_console_putchar(struct uart_port *port, u8 ch)
  25. {
  26. while (__dcc_getstatus() & DCC_STATUS_TX)
  27. cpu_relax();
  28. __dcc_putchar(ch);
  29. }
  30. static void dcc_early_write(struct console *con, const char *s, unsigned n)
  31. {
  32. struct earlycon_device *dev = con->data;
  33. uart_console_write(&dev->port, s, n, dcc_uart_console_putchar);
  34. }
  35. static int __init dcc_early_console_setup(struct earlycon_device *device,
  36. const char *opt)
  37. {
  38. unsigned int count = 0x4000000;
  39. while (--count && (__dcc_getstatus() & DCC_STATUS_TX))
  40. cpu_relax();
  41. if (__dcc_getstatus() & DCC_STATUS_TX)
  42. return -ENODEV;
  43. device->con->write = dcc_early_write;
  44. return 0;
  45. }
  46. EARLYCON_DECLARE(dcc, dcc_early_console_setup);
  47. static ssize_t hvc_dcc_put_chars(uint32_t vt, const u8 *buf, size_t count)
  48. {
  49. size_t i;
  50. for (i = 0; i < count; i++) {
  51. while (__dcc_getstatus() & DCC_STATUS_TX)
  52. cpu_relax();
  53. __dcc_putchar(buf[i]);
  54. }
  55. return count;
  56. }
  57. static ssize_t hvc_dcc_get_chars(uint32_t vt, u8 *buf, size_t count)
  58. {
  59. size_t i;
  60. for (i = 0; i < count; ++i)
  61. if (__dcc_getstatus() & DCC_STATUS_RX)
  62. buf[i] = __dcc_getchar();
  63. else
  64. break;
  65. return i;
  66. }
  67. /*
  68. * Check if the DCC is enabled. If CONFIG_HVC_DCC_SERIALIZE_SMP is enabled,
  69. * then we assume then this function will be called first on core0. That way,
  70. * dcc_core0_available will be true only if it's available on core0.
  71. */
  72. static bool hvc_dcc_check(void)
  73. {
  74. unsigned long time = jiffies + (HZ / 10);
  75. static bool dcc_core0_available;
  76. /*
  77. * If we're not on core 0, but we previously confirmed that DCC is
  78. * active, then just return true.
  79. */
  80. int cpu = get_cpu();
  81. if (IS_ENABLED(CONFIG_HVC_DCC_SERIALIZE_SMP) && cpu && dcc_core0_available) {
  82. put_cpu();
  83. return true;
  84. }
  85. put_cpu();
  86. /* Write a test character to check if it is handled */
  87. __dcc_putchar('\n');
  88. while (time_is_after_jiffies(time)) {
  89. if (!(__dcc_getstatus() & DCC_STATUS_TX)) {
  90. dcc_core0_available = true;
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /*
  97. * Workqueue function that writes the output FIFO to the DCC on core 0.
  98. */
  99. static void dcc_put_work(struct work_struct *work)
  100. {
  101. unsigned char ch;
  102. unsigned long irqflags;
  103. spin_lock_irqsave(&dcc_lock, irqflags);
  104. /* While there's data in the output FIFO, write it to the DCC */
  105. while (kfifo_get(&outbuf, &ch))
  106. hvc_dcc_put_chars(0, &ch, 1);
  107. /* While we're at it, check for any input characters */
  108. while (!kfifo_is_full(&inbuf)) {
  109. if (!hvc_dcc_get_chars(0, &ch, 1))
  110. break;
  111. kfifo_put(&inbuf, ch);
  112. }
  113. spin_unlock_irqrestore(&dcc_lock, irqflags);
  114. }
  115. static DECLARE_WORK(dcc_pwork, dcc_put_work);
  116. /*
  117. * Workqueue function that reads characters from DCC and puts them into the
  118. * input FIFO.
  119. */
  120. static void dcc_get_work(struct work_struct *work)
  121. {
  122. unsigned long irqflags;
  123. u8 ch;
  124. /*
  125. * Read characters from DCC and put them into the input FIFO, as
  126. * long as there is room and we have characters to read.
  127. */
  128. spin_lock_irqsave(&dcc_lock, irqflags);
  129. while (!kfifo_is_full(&inbuf)) {
  130. if (!hvc_dcc_get_chars(0, &ch, 1))
  131. break;
  132. kfifo_put(&inbuf, ch);
  133. }
  134. spin_unlock_irqrestore(&dcc_lock, irqflags);
  135. }
  136. static DECLARE_WORK(dcc_gwork, dcc_get_work);
  137. /*
  138. * Write characters directly to the DCC if we're on core 0 and the FIFO
  139. * is empty, or write them to the FIFO if we're not.
  140. */
  141. static ssize_t hvc_dcc0_put_chars(u32 vt, const u8 *buf, size_t count)
  142. {
  143. unsigned long irqflags;
  144. ssize_t len;
  145. if (!IS_ENABLED(CONFIG_HVC_DCC_SERIALIZE_SMP))
  146. return hvc_dcc_put_chars(vt, buf, count);
  147. spin_lock_irqsave(&dcc_lock, irqflags);
  148. if (smp_processor_id() || (!kfifo_is_empty(&outbuf))) {
  149. len = kfifo_in(&outbuf, buf, count);
  150. spin_unlock_irqrestore(&dcc_lock, irqflags);
  151. /*
  152. * We just push data to the output FIFO, so schedule the
  153. * workqueue that will actually write that data to DCC.
  154. * CPU hotplug is disabled in dcc_init so CPU0 cannot be
  155. * offlined after the cpu online check.
  156. */
  157. if (cpu_online(0))
  158. schedule_work_on(0, &dcc_pwork);
  159. return len;
  160. }
  161. /*
  162. * If we're already on core 0, and the FIFO is empty, then just
  163. * write the data to DCC.
  164. */
  165. len = hvc_dcc_put_chars(vt, buf, count);
  166. spin_unlock_irqrestore(&dcc_lock, irqflags);
  167. return len;
  168. }
  169. /*
  170. * Read characters directly from the DCC if we're on core 0 and the FIFO
  171. * is empty, or read them from the FIFO if we're not.
  172. */
  173. static ssize_t hvc_dcc0_get_chars(u32 vt, u8 *buf, size_t count)
  174. {
  175. unsigned long irqflags;
  176. ssize_t len;
  177. if (!IS_ENABLED(CONFIG_HVC_DCC_SERIALIZE_SMP))
  178. return hvc_dcc_get_chars(vt, buf, count);
  179. spin_lock_irqsave(&dcc_lock, irqflags);
  180. if (smp_processor_id() || (!kfifo_is_empty(&inbuf))) {
  181. len = kfifo_out(&inbuf, buf, count);
  182. spin_unlock_irqrestore(&dcc_lock, irqflags);
  183. /*
  184. * If the FIFO was empty, there may be characters in the DCC
  185. * that we haven't read yet. Schedule a workqueue to fill
  186. * the input FIFO, so that the next time this function is
  187. * called, we'll have data. CPU hotplug is disabled in dcc_init
  188. * so CPU0 cannot be offlined after the cpu online check.
  189. */
  190. if (!len && cpu_online(0))
  191. schedule_work_on(0, &dcc_gwork);
  192. return len;
  193. }
  194. /*
  195. * If we're already on core 0, and the FIFO is empty, then just
  196. * read the data from DCC.
  197. */
  198. len = hvc_dcc_get_chars(vt, buf, count);
  199. spin_unlock_irqrestore(&dcc_lock, irqflags);
  200. return len;
  201. }
  202. static const struct hv_ops hvc_dcc_get_put_ops = {
  203. .get_chars = hvc_dcc0_get_chars,
  204. .put_chars = hvc_dcc0_put_chars,
  205. };
  206. static int __init hvc_dcc_console_init(void)
  207. {
  208. int ret;
  209. if (!hvc_dcc_check())
  210. return -ENODEV;
  211. /* Returns -1 if error */
  212. ret = hvc_instantiate(0, 0, &hvc_dcc_get_put_ops);
  213. return ret < 0 ? -ENODEV : 0;
  214. }
  215. console_initcall(hvc_dcc_console_init);
  216. static int __init hvc_dcc_init(void)
  217. {
  218. struct hvc_struct *p;
  219. if (!hvc_dcc_check())
  220. return -ENODEV;
  221. if (IS_ENABLED(CONFIG_HVC_DCC_SERIALIZE_SMP)) {
  222. pr_warn("\n");
  223. pr_warn("********************************************************************\n");
  224. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  225. pr_warn("** **\n");
  226. pr_warn("** HVC_DCC_SERIALIZE_SMP SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n");
  227. pr_warn("** **\n");
  228. pr_warn("** This means that this is a DEBUG kernel and unsafe for **\n");
  229. pr_warn("** production use and has important feature like CPU hotplug **\n");
  230. pr_warn("** disabled. **\n");
  231. pr_warn("** **\n");
  232. pr_warn("** If you see this message and you are not debugging the **\n");
  233. pr_warn("** kernel, report this immediately to your vendor! **\n");
  234. pr_warn("** **\n");
  235. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  236. pr_warn("********************************************************************\n");
  237. cpu_hotplug_disable();
  238. }
  239. p = hvc_alloc(0, 0, &hvc_dcc_get_put_ops, 128);
  240. return PTR_ERR_OR_ZERO(p);
  241. }
  242. device_initcall(hvc_dcc_init);