serial_xen.c 3.7 KB

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