serial_xen.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2018 NXP
  4. * (C) 2020 EPAM Systems Inc.
  5. */
  6. #include <common.h>
  7. #include <cpu_func.h>
  8. #include <dm.h>
  9. #include <serial.h>
  10. #include <watchdog.h>
  11. #include <linux/bug.h>
  12. #include <xen/hvm.h>
  13. #include <xen/events.h>
  14. #include <xen/interface/sched.h>
  15. #include <xen/interface/hvm/hvm_op.h>
  16. #include <xen/interface/hvm/params.h>
  17. #include <xen/interface/io/console.h>
  18. #include <xen/interface/io/ring.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. u32 console_evtchn;
  21. /*
  22. * struct xen_uart_priv - Structure representing a Xen UART info
  23. * @intf: Console I/O interface for Xen guest OSes
  24. * @evtchn: Console event channel
  25. */
  26. struct xen_uart_priv {
  27. struct xencons_interface *intf;
  28. u32 evtchn;
  29. };
  30. int xen_serial_setbrg(struct udevice *dev, int baudrate)
  31. {
  32. return 0;
  33. }
  34. static int xen_serial_probe(struct udevice *dev)
  35. {
  36. struct xen_uart_priv *priv = dev_get_priv(dev);
  37. u64 val = 0;
  38. unsigned long gfn;
  39. int ret;
  40. ret = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &val);
  41. if (ret < 0 || val == 0)
  42. return ret;
  43. priv->evtchn = val;
  44. console_evtchn = val;
  45. ret = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &val);
  46. if (ret < 0)
  47. return ret;
  48. if (!val)
  49. return -EINVAL;
  50. gfn = val;
  51. priv->intf = (struct xencons_interface *)(gfn << XEN_PAGE_SHIFT);
  52. return 0;
  53. }
  54. static int xen_serial_pending(struct udevice *dev, bool input)
  55. {
  56. struct xen_uart_priv *priv = dev_get_priv(dev);
  57. struct xencons_interface *intf = priv->intf;
  58. if (!input || intf->in_cons == intf->in_prod)
  59. return 0;
  60. return 1;
  61. }
  62. static int xen_serial_getc(struct udevice *dev)
  63. {
  64. struct xen_uart_priv *priv = dev_get_priv(dev);
  65. struct xencons_interface *intf = priv->intf;
  66. XENCONS_RING_IDX cons;
  67. char c;
  68. while (intf->in_cons == intf->in_prod)
  69. mb(); /* wait */
  70. cons = intf->in_cons;
  71. mb(); /* get pointers before reading ring */
  72. c = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
  73. mb(); /* read ring before consuming */
  74. intf->in_cons = cons;
  75. notify_remote_via_evtchn(priv->evtchn);
  76. return c;
  77. }
  78. static int __write_console(struct udevice *dev, const char *data, int len)
  79. {
  80. struct xen_uart_priv *priv = dev_get_priv(dev);
  81. struct xencons_interface *intf = priv->intf;
  82. XENCONS_RING_IDX cons, prod;
  83. int sent = 0;
  84. cons = intf->out_cons;
  85. prod = intf->out_prod;
  86. mb(); /* Update pointer */
  87. WARN_ON((prod - cons) > sizeof(intf->out));
  88. while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
  89. intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
  90. mb(); /* Update data before pointer */
  91. intf->out_prod = prod;
  92. if (sent)
  93. notify_remote_via_evtchn(priv->evtchn);
  94. return sent;
  95. }
  96. static int write_console(struct udevice *dev, const char *data, int len)
  97. {
  98. /*
  99. * Make sure the whole buffer is emitted, polling if
  100. * necessary. We don't ever want to rely on the hvc daemon
  101. * because the most interesting console output is when the
  102. * kernel is crippled.
  103. */
  104. while (len) {
  105. int sent = __write_console(dev, data, len);
  106. data += sent;
  107. len -= sent;
  108. if (unlikely(len))
  109. HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
  110. }
  111. return 0;
  112. }
  113. static int xen_serial_putc(struct udevice *dev, const char ch)
  114. {
  115. write_console(dev, &ch, 1);
  116. return 0;
  117. }
  118. static const struct dm_serial_ops xen_serial_ops = {
  119. .putc = xen_serial_putc,
  120. .getc = xen_serial_getc,
  121. .pending = xen_serial_pending,
  122. };
  123. #if CONFIG_IS_ENABLED(OF_CONTROL)
  124. static const struct udevice_id xen_serial_ids[] = {
  125. { .compatible = "xen,xen" },
  126. { }
  127. };
  128. #endif
  129. U_BOOT_DRIVER(serial_xen) = {
  130. .name = "serial_xen",
  131. .id = UCLASS_SERIAL,
  132. #if CONFIG_IS_ENABLED(OF_CONTROL)
  133. .of_match = xen_serial_ids,
  134. #endif
  135. .priv_auto_alloc_size = sizeof(struct xen_uart_priv),
  136. .probe = xen_serial_probe,
  137. .ops = &xen_serial_ops,
  138. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  139. .flags = DM_FLAG_PRE_RELOC,
  140. #endif
  141. };