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