hvc_tile.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  4. *
  5. * Tilera TILE Processor hypervisor console
  6. */
  7. #include <linux/console.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/types.h>
  16. #include <asm/setup.h>
  17. #include <arch/sim_def.h>
  18. #include <hv/hypervisor.h>
  19. #include "hvc_console.h"
  20. static int use_sim_console;
  21. static int __init sim_console(char *str)
  22. {
  23. use_sim_console = 1;
  24. return 0;
  25. }
  26. early_param("sim_console", sim_console);
  27. int tile_console_write(const char *buf, int count)
  28. {
  29. if (unlikely(use_sim_console)) {
  30. int i;
  31. for (i = 0; i < count; ++i)
  32. __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
  33. (buf[i] << _SIM_CONTROL_OPERATOR_BITS));
  34. __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
  35. (SIM_PUTC_FLUSH_BINARY <<
  36. _SIM_CONTROL_OPERATOR_BITS));
  37. return 0;
  38. } else {
  39. /* Translate 0 bytes written to EAGAIN for hvc_console_print. */
  40. return hv_console_write((HV_VirtAddr)buf, count) ?: -EAGAIN;
  41. }
  42. }
  43. static int hvc_tile_put_chars(uint32_t vt, const char *buf, int count)
  44. {
  45. return tile_console_write(buf, count);
  46. }
  47. static int hvc_tile_get_chars(uint32_t vt, char *buf, int count)
  48. {
  49. int i, c;
  50. for (i = 0; i < count; ++i) {
  51. c = hv_console_read_if_ready();
  52. if (c < 0)
  53. break;
  54. buf[i] = c;
  55. }
  56. return i;
  57. }
  58. #ifdef __tilegx__
  59. /*
  60. * IRQ based callbacks.
  61. */
  62. static int hvc_tile_notifier_add_irq(struct hvc_struct *hp, int irq)
  63. {
  64. int rc;
  65. int cpu = raw_smp_processor_id(); /* Choose an arbitrary cpu */
  66. HV_Coord coord = { .x = cpu_x(cpu), .y = cpu_y(cpu) };
  67. rc = notifier_add_irq(hp, irq);
  68. if (rc)
  69. return rc;
  70. /*
  71. * Request that the hypervisor start sending us interrupts.
  72. * If the hypervisor returns an error, we still return 0, so that
  73. * we can fall back to polling.
  74. */
  75. if (hv_console_set_ipi(KERNEL_PL, irq, coord) < 0)
  76. notifier_del_irq(hp, irq);
  77. return 0;
  78. }
  79. static void hvc_tile_notifier_del_irq(struct hvc_struct *hp, int irq)
  80. {
  81. HV_Coord coord = { 0, 0 };
  82. /* Tell the hypervisor to stop sending us interrupts. */
  83. hv_console_set_ipi(KERNEL_PL, -1, coord);
  84. notifier_del_irq(hp, irq);
  85. }
  86. static void hvc_tile_notifier_hangup_irq(struct hvc_struct *hp, int irq)
  87. {
  88. hvc_tile_notifier_del_irq(hp, irq);
  89. }
  90. #endif
  91. static const struct hv_ops hvc_tile_get_put_ops = {
  92. .get_chars = hvc_tile_get_chars,
  93. .put_chars = hvc_tile_put_chars,
  94. #ifdef __tilegx__
  95. .notifier_add = hvc_tile_notifier_add_irq,
  96. .notifier_del = hvc_tile_notifier_del_irq,
  97. .notifier_hangup = hvc_tile_notifier_hangup_irq,
  98. #endif
  99. };
  100. #ifdef __tilegx__
  101. static int hvc_tile_probe(struct platform_device *pdev)
  102. {
  103. struct hvc_struct *hp;
  104. int tile_hvc_irq;
  105. /* Create our IRQ and register it. */
  106. tile_hvc_irq = irq_alloc_hwirq(-1);
  107. if (!tile_hvc_irq)
  108. return -ENXIO;
  109. tile_irq_activate(tile_hvc_irq, TILE_IRQ_PERCPU);
  110. hp = hvc_alloc(0, tile_hvc_irq, &hvc_tile_get_put_ops, 128);
  111. if (IS_ERR(hp)) {
  112. irq_free_hwirq(tile_hvc_irq);
  113. return PTR_ERR(hp);
  114. }
  115. dev_set_drvdata(&pdev->dev, hp);
  116. return 0;
  117. }
  118. static int hvc_tile_remove(struct platform_device *pdev)
  119. {
  120. int rc;
  121. struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
  122. rc = hvc_remove(hp);
  123. if (rc == 0)
  124. irq_free_hwirq(hp->data);
  125. return rc;
  126. }
  127. static void hvc_tile_shutdown(struct platform_device *pdev)
  128. {
  129. struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
  130. hvc_tile_notifier_del_irq(hp, hp->data);
  131. }
  132. static struct platform_device hvc_tile_pdev = {
  133. .name = "hvc-tile",
  134. .id = 0,
  135. };
  136. static struct platform_driver hvc_tile_driver = {
  137. .probe = hvc_tile_probe,
  138. .remove = hvc_tile_remove,
  139. .shutdown = hvc_tile_shutdown,
  140. .driver = {
  141. .name = "hvc-tile",
  142. }
  143. };
  144. #endif
  145. static int __init hvc_tile_console_init(void)
  146. {
  147. hvc_instantiate(0, 0, &hvc_tile_get_put_ops);
  148. add_preferred_console("hvc", 0, NULL);
  149. return 0;
  150. }
  151. console_initcall(hvc_tile_console_init);
  152. static int __init hvc_tile_init(void)
  153. {
  154. #ifndef __tilegx__
  155. struct hvc_struct *hp;
  156. hp = hvc_alloc(0, 0, &hvc_tile_get_put_ops, 128);
  157. return PTR_ERR_OR_ZERO(hp);
  158. #else
  159. platform_device_register(&hvc_tile_pdev);
  160. return platform_driver_register(&hvc_tile_driver);
  161. #endif
  162. }
  163. device_initcall(hvc_tile_init);