hvc_tile.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. return hv_console_write((HV_VirtAddr)buf, count);
  49. }
  50. }
  51. static int hvc_tile_put_chars(uint32_t vt, const char *buf, int count)
  52. {
  53. return tile_console_write(buf, count);
  54. }
  55. static int hvc_tile_get_chars(uint32_t vt, char *buf, int count)
  56. {
  57. int i, c;
  58. for (i = 0; i < count; ++i) {
  59. c = hv_console_read_if_ready();
  60. if (c < 0)
  61. break;
  62. buf[i] = c;
  63. }
  64. return i;
  65. }
  66. #ifdef __tilegx__
  67. /*
  68. * IRQ based callbacks.
  69. */
  70. static int hvc_tile_notifier_add_irq(struct hvc_struct *hp, int irq)
  71. {
  72. int rc;
  73. int cpu = raw_smp_processor_id(); /* Choose an arbitrary cpu */
  74. HV_Coord coord = { .x = cpu_x(cpu), .y = cpu_y(cpu) };
  75. rc = notifier_add_irq(hp, irq);
  76. if (rc)
  77. return rc;
  78. /*
  79. * Request that the hypervisor start sending us interrupts.
  80. * If the hypervisor returns an error, we still return 0, so that
  81. * we can fall back to polling.
  82. */
  83. if (hv_console_set_ipi(KERNEL_PL, irq, coord) < 0)
  84. notifier_del_irq(hp, irq);
  85. return 0;
  86. }
  87. static void hvc_tile_notifier_del_irq(struct hvc_struct *hp, int irq)
  88. {
  89. HV_Coord coord = { 0, 0 };
  90. /* Tell the hypervisor to stop sending us interrupts. */
  91. hv_console_set_ipi(KERNEL_PL, -1, coord);
  92. notifier_del_irq(hp, irq);
  93. }
  94. static void hvc_tile_notifier_hangup_irq(struct hvc_struct *hp, int irq)
  95. {
  96. hvc_tile_notifier_del_irq(hp, irq);
  97. }
  98. #endif
  99. static const struct hv_ops hvc_tile_get_put_ops = {
  100. .get_chars = hvc_tile_get_chars,
  101. .put_chars = hvc_tile_put_chars,
  102. #ifdef __tilegx__
  103. .notifier_add = hvc_tile_notifier_add_irq,
  104. .notifier_del = hvc_tile_notifier_del_irq,
  105. .notifier_hangup = hvc_tile_notifier_hangup_irq,
  106. #endif
  107. };
  108. #ifdef __tilegx__
  109. static int hvc_tile_probe(struct platform_device *pdev)
  110. {
  111. struct hvc_struct *hp;
  112. int tile_hvc_irq;
  113. /* Create our IRQ and register it. */
  114. tile_hvc_irq = create_irq();
  115. if (tile_hvc_irq < 0)
  116. return -ENXIO;
  117. tile_irq_activate(tile_hvc_irq, TILE_IRQ_PERCPU);
  118. hp = hvc_alloc(0, tile_hvc_irq, &hvc_tile_get_put_ops, 128);
  119. if (IS_ERR(hp)) {
  120. destroy_irq(tile_hvc_irq);
  121. return PTR_ERR(hp);
  122. }
  123. dev_set_drvdata(&pdev->dev, hp);
  124. return 0;
  125. }
  126. static int hvc_tile_remove(struct platform_device *pdev)
  127. {
  128. int rc;
  129. struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
  130. rc = hvc_remove(hp);
  131. if (rc == 0)
  132. destroy_irq(hp->data);
  133. return rc;
  134. }
  135. static void hvc_tile_shutdown(struct platform_device *pdev)
  136. {
  137. struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
  138. hvc_tile_notifier_del_irq(hp, hp->data);
  139. }
  140. static struct platform_device hvc_tile_pdev = {
  141. .name = "hvc-tile",
  142. .id = 0,
  143. };
  144. static struct platform_driver hvc_tile_driver = {
  145. .probe = hvc_tile_probe,
  146. .remove = hvc_tile_remove,
  147. .shutdown = hvc_tile_shutdown,
  148. .driver = {
  149. .name = "hvc-tile",
  150. .owner = THIS_MODULE,
  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 IS_ERR(hp) ? PTR_ERR(hp) : 0;
  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);