hvc_gunyah.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "hvc_gunyah: " fmt
  7. #include <linux/console.h>
  8. #include <linux/init.h>
  9. #include <linux/kfifo.h>
  10. #include <linux/module.h>
  11. #include <linux/notifier.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/printk.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/gunyah/gh_msgq.h>
  16. #include <linux/gunyah/gh_common.h>
  17. #include <linux/gunyah/gh_rm_drv.h>
  18. #include "hvc_console.h"
  19. /*
  20. * Note: hvc_alloc follows first-come, first-served for assigning
  21. * numbers to registered hvc instances. Thus, the following assignments occur
  22. * when both DCC and GUNYAH consoles are compiled:
  23. * | DCC connected | DCC not connected
  24. * (dcc) | hvc0 | (not present)
  25. * SELF | hvc1 | hvc0
  26. * PRIMARY_VM | hvc2 | hvc1
  27. * TRUSTED_VM | hvc3 | hvc2
  28. * "DCC connected" means a DCC terminal is open with device
  29. */
  30. #define HVC_GH_VTERM_COOKIE 0x474E5948
  31. /* # of payload bytes that can fit in a 1-fragment CONSOLE_WRITE message */
  32. #define GH_HVC_WRITE_MSG_SIZE ((1 * (GH_MSGQ_MAX_MSG_SIZE_BYTES - 8)) - 4)
  33. struct gh_hvc_prv {
  34. struct hvc_struct *hvc;
  35. enum gh_vm_names vm_name;
  36. DECLARE_KFIFO(get_fifo, char, 1024);
  37. DECLARE_KFIFO(put_fifo, char, 1024);
  38. struct work_struct put_work;
  39. };
  40. static DEFINE_SPINLOCK(fifo_lock);
  41. static struct gh_hvc_prv gh_hvc_data[GH_VM_MAX];
  42. static inline int gh_vm_name_to_vtermno(enum gh_vm_names vmname)
  43. {
  44. return vmname + HVC_GH_VTERM_COOKIE;
  45. }
  46. static inline int vtermno_to_gh_vm_name(int vtermno)
  47. {
  48. return vtermno - HVC_GH_VTERM_COOKIE;
  49. }
  50. static int gh_hvc_notify_console_chars(struct notifier_block *this,
  51. unsigned long cmd, void *data)
  52. {
  53. struct gh_rm_notif_vm_console_chars *msg = data;
  54. enum gh_vm_names vm_name;
  55. int ret;
  56. if (cmd != GH_RM_NOTIF_VM_CONSOLE_CHARS)
  57. return NOTIFY_DONE;
  58. ret = gh_rm_get_vm_name(msg->vmid, &vm_name);
  59. if (ret) {
  60. pr_warn_ratelimited("don't know VMID %d ret: %d\n", msg->vmid,
  61. ret);
  62. return NOTIFY_OK;
  63. }
  64. ret = kfifo_in_spinlocked(&gh_hvc_data[vm_name].get_fifo,
  65. msg->bytes, msg->num_bytes,
  66. &fifo_lock);
  67. if (ret < 0)
  68. pr_warn_ratelimited("dropped %d bytes from VM%d - error %d\n",
  69. msg->num_bytes, vm_name, ret);
  70. else if (ret < msg->num_bytes)
  71. pr_warn_ratelimited("dropped %d bytes from VM%d - full fifo\n",
  72. msg->num_bytes - ret, vm_name);
  73. if (hvc_poll(gh_hvc_data[vm_name].hvc))
  74. hvc_kick();
  75. return NOTIFY_OK;
  76. }
  77. static void gh_hvc_put_work_fn(struct work_struct *ws)
  78. {
  79. gh_vmid_t vmid;
  80. char buf[GH_HVC_WRITE_MSG_SIZE];
  81. int count, ret;
  82. struct gh_hvc_prv *prv = container_of(ws, struct gh_hvc_prv, put_work);
  83. ret = ghd_rm_get_vmid(prv->vm_name, &vmid);
  84. if (ret) {
  85. pr_warn_once("%s: gh_rm_get_vmid failed for %d: %d\n",
  86. __func__, prv->vm_name, ret);
  87. return;
  88. }
  89. while (!kfifo_is_empty(&prv->put_fifo)) {
  90. count = kfifo_out_spinlocked(&prv->put_fifo, buf, sizeof(buf),
  91. &fifo_lock);
  92. if (count <= 0)
  93. continue;
  94. ret = gh_rm_console_write(vmid, buf, count);
  95. if (ret) {
  96. pr_warn_once("%s gh_rm_console_write failed for %d: %d\n",
  97. __func__, prv->vm_name, ret);
  98. break;
  99. }
  100. }
  101. }
  102. static int gh_hvc_get_chars(uint32_t vtermno, char *buf, int count)
  103. {
  104. int vm_name = vtermno_to_gh_vm_name(vtermno);
  105. if (vm_name < 0 || vm_name >= GH_VM_MAX)
  106. return -EINVAL;
  107. return kfifo_out_spinlocked(&gh_hvc_data[vm_name].get_fifo,
  108. buf, count, &fifo_lock);
  109. }
  110. static int gh_hvc_put_chars(uint32_t vtermno, const char *buf, int count)
  111. {
  112. int ret, vm_name = vtermno_to_gh_vm_name(vtermno);
  113. if (vm_name < 0 || vm_name >= GH_VM_MAX)
  114. return -EINVAL;
  115. ret = kfifo_in_spinlocked(&gh_hvc_data[vm_name].put_fifo,
  116. buf, count, &fifo_lock);
  117. if (ret > 0)
  118. schedule_work(&gh_hvc_data[vm_name].put_work);
  119. return ret;
  120. }
  121. static int gh_hvc_flush(uint32_t vtermno, bool wait)
  122. {
  123. int ret, vm_name = vtermno_to_gh_vm_name(vtermno);
  124. gh_vmid_t vmid;
  125. /* RM calls will all sleep. A flush without waiting isn't possible */
  126. if (!wait)
  127. return 0;
  128. might_sleep();
  129. if (vm_name < 0 || vm_name >= GH_VM_MAX)
  130. return -EINVAL;
  131. ret = ghd_rm_get_vmid(vm_name, &vmid);
  132. if (ret)
  133. return ret;
  134. if (cancel_work_sync(&gh_hvc_data[vm_name].put_work)) {
  135. /* flush the fifo */
  136. gh_hvc_put_work_fn(&gh_hvc_data[vm_name].put_work);
  137. }
  138. return gh_rm_console_flush(vmid);
  139. }
  140. static int gh_hvc_notify_add(struct hvc_struct *hp, int vm_name)
  141. {
  142. int ret;
  143. gh_vmid_t vmid;
  144. #ifdef CONFIG_HVC_GUNYAH_CONSOLE
  145. /* tty layer is opening, but kernel has already opened for printk */
  146. if (vm_name == GH_SELF_VM)
  147. return 0;
  148. #endif /* CONFIG_HVC_GUNYAH_CONSOLE */
  149. ret = ghd_rm_get_vmid(vm_name, &vmid);
  150. if (ret) {
  151. pr_err("%s: gh_rm_get_vmid failed for %d: %d\n", __func__,
  152. vm_name, ret);
  153. return ret;
  154. }
  155. return gh_rm_console_open(vmid);
  156. }
  157. static void gh_hvc_notify_del(struct hvc_struct *hp, int vm_name)
  158. {
  159. int ret;
  160. gh_vmid_t vmid;
  161. if (vm_name < 0 || vm_name >= GH_VM_MAX)
  162. return;
  163. #ifdef CONFIG_HVC_GUNYAH_CONSOLE
  164. /* tty layer is closing, but kernel is still using for printk. */
  165. if (vm_name == GH_SELF_VM)
  166. return;
  167. #endif /* CONFIG_HVC_GUNYAH_CONSOLE */
  168. if (cancel_work_sync(&gh_hvc_data[vm_name].put_work)) {
  169. /* flush the fifo */
  170. gh_hvc_put_work_fn(&gh_hvc_data[vm_name].put_work);
  171. }
  172. ret = ghd_rm_get_vmid(vm_name, &vmid);
  173. if (ret)
  174. return;
  175. ret = gh_rm_console_close(vmid);
  176. if (ret)
  177. pr_err("%s: failed close VM%d console - %d\n", __func__,
  178. vm_name, ret);
  179. kfifo_reset(&gh_hvc_data[vm_name].get_fifo);
  180. }
  181. static struct notifier_block gh_hvc_nb = {
  182. .notifier_call = gh_hvc_notify_console_chars,
  183. };
  184. static const struct hv_ops gh_hv_ops = {
  185. .get_chars = gh_hvc_get_chars,
  186. .put_chars = gh_hvc_put_chars,
  187. .flush = gh_hvc_flush,
  188. .notifier_add = gh_hvc_notify_add,
  189. .notifier_del = gh_hvc_notify_del,
  190. };
  191. #ifdef CONFIG_HVC_GUNYAH_CONSOLE
  192. static int __init hvc_gh_console_init(void)
  193. {
  194. int ret;
  195. /* Need to call RM CONSOLE_OPEN before console can be used */
  196. ret = gh_rm_console_open(0);
  197. if (ret)
  198. return ret;
  199. ret = hvc_instantiate(gh_vm_name_to_vtermno(GH_SELF_VM), 0,
  200. &gh_hv_ops);
  201. return ret < 0 ? -ENODEV : 0;
  202. }
  203. #else
  204. static int __init hvc_gh_console_init(void)
  205. {
  206. return 0;
  207. }
  208. #endif /* CONFIG_HVC_GUNYAH_CONSOLE */
  209. static int __init hvc_gh_init(void)
  210. {
  211. int i, ret = 0;
  212. struct gh_hvc_prv *prv;
  213. /* Must initialize fifos and work before calling hvc_gh_console_init */
  214. for (i = 0; i < GH_VM_MAX; i++) {
  215. prv = &gh_hvc_data[i];
  216. prv->vm_name = i;
  217. INIT_KFIFO(prv->get_fifo);
  218. INIT_KFIFO(prv->put_fifo);
  219. INIT_WORK(&prv->put_work, gh_hvc_put_work_fn);
  220. }
  221. /* Must instantiate console before calling hvc_alloc */
  222. hvc_gh_console_init();
  223. for (i = 0; i < GH_VM_MAX; i++) {
  224. prv = &gh_hvc_data[i];
  225. prv->hvc = hvc_alloc(gh_vm_name_to_vtermno(i), i, &gh_hv_ops,
  226. 256);
  227. ret = PTR_ERR_OR_ZERO(prv->hvc);
  228. if (ret)
  229. goto bail;
  230. }
  231. ret = gh_rm_register_notifier(&gh_hvc_nb);
  232. if (ret)
  233. goto bail;
  234. return 0;
  235. bail:
  236. for (--i; i >= 0; i--) {
  237. hvc_remove(gh_hvc_data[i].hvc);
  238. gh_hvc_data[i].hvc = NULL;
  239. }
  240. return ret;
  241. }
  242. late_initcall(hvc_gh_init);
  243. static __exit void hvc_gh_exit(void)
  244. {
  245. int i;
  246. gh_rm_unregister_notifier(&gh_hvc_nb);
  247. for (i = 0; i < GH_VM_MAX; i++)
  248. if (gh_hvc_data[i].hvc) {
  249. hvc_remove(gh_hvc_data[i].hvc);
  250. gh_hvc_data[i].hvc = NULL;
  251. }
  252. }
  253. module_exit(hvc_gh_exit);
  254. MODULE_LICENSE("GPL");
  255. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Gunyah Hypervisor Console Driver");