hvc_tile.c 4.7 KB

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