hvc_vio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vio driver interface to hvc_console.c
  4. *
  5. * This code was moved here to allow the remaining code to be reused as a
  6. * generic polling mode with semi-reliable transport driver core to the
  7. * console and tty subsystems.
  8. *
  9. *
  10. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  11. * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
  12. * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  13. * Copyright (C) 2004 IBM Corporation
  14. *
  15. * Additional Author(s):
  16. * Ryan S. Arnold <rsa@us.ibm.com>
  17. *
  18. * TODO:
  19. *
  20. * - handle error in sending hvsi protocol packets
  21. * - retry nego on subsequent sends ?
  22. */
  23. #undef DEBUG
  24. #include <linux/types.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/console.h>
  29. #include <linux/of.h>
  30. #include <asm/hvconsole.h>
  31. #include <asm/vio.h>
  32. #include <asm/hvsi.h>
  33. #include <asm/udbg.h>
  34. #include <asm/machdep.h>
  35. #include "hvc_console.h"
  36. static const char hvc_driver_name[] = "hvc_console";
  37. static const struct vio_device_id hvc_driver_table[] = {
  38. {"serial", "hvterm1"},
  39. #ifndef HVC_OLD_HVSI
  40. {"serial", "hvterm-protocol"},
  41. #endif
  42. { "", "" }
  43. };
  44. typedef enum hv_protocol {
  45. HV_PROTOCOL_RAW,
  46. HV_PROTOCOL_HVSI
  47. } hv_protocol_t;
  48. struct hvterm_priv {
  49. u32 termno; /* HV term number */
  50. hv_protocol_t proto; /* Raw data or HVSI packets */
  51. struct hvsi_priv hvsi; /* HVSI specific data */
  52. spinlock_t buf_lock;
  53. u8 buf[SIZE_VIO_GET_CHARS];
  54. size_t left;
  55. size_t offset;
  56. };
  57. static struct hvterm_priv *hvterm_privs[MAX_NR_HVC_CONSOLES];
  58. /* For early boot console */
  59. static struct hvterm_priv hvterm_priv0;
  60. static ssize_t hvterm_raw_get_chars(uint32_t vtermno, u8 *buf, size_t count)
  61. {
  62. struct hvterm_priv *pv = hvterm_privs[vtermno];
  63. unsigned long i;
  64. unsigned long flags;
  65. size_t got;
  66. if (WARN_ON(!pv))
  67. return 0;
  68. spin_lock_irqsave(&pv->buf_lock, flags);
  69. if (pv->left == 0) {
  70. pv->offset = 0;
  71. pv->left = hvc_get_chars(pv->termno, pv->buf, count);
  72. /*
  73. * Work around a HV bug where it gives us a null
  74. * after every \r. -- paulus
  75. */
  76. for (i = 1; i < pv->left; ++i) {
  77. if (pv->buf[i] == 0 && pv->buf[i-1] == '\r') {
  78. --pv->left;
  79. if (i < pv->left) {
  80. memmove(&pv->buf[i], &pv->buf[i+1],
  81. pv->left - i);
  82. }
  83. }
  84. }
  85. }
  86. got = min(count, pv->left);
  87. memcpy(buf, &pv->buf[pv->offset], got);
  88. pv->offset += got;
  89. pv->left -= got;
  90. spin_unlock_irqrestore(&pv->buf_lock, flags);
  91. return got;
  92. }
  93. /**
  94. * hvterm_raw_put_chars: send characters to firmware for given vterm adapter
  95. * @vtermno: The virtual terminal number.
  96. * @buf: The characters to send. Because of the underlying hypercall in
  97. * hvc_put_chars(), this buffer must be at least 16 bytes long, even if
  98. * you are sending fewer chars.
  99. * @count: number of chars to send.
  100. */
  101. static ssize_t hvterm_raw_put_chars(uint32_t vtermno, const u8 *buf,
  102. size_t count)
  103. {
  104. struct hvterm_priv *pv = hvterm_privs[vtermno];
  105. if (WARN_ON(!pv))
  106. return 0;
  107. return hvc_put_chars(pv->termno, buf, count);
  108. }
  109. static const struct hv_ops hvterm_raw_ops = {
  110. .get_chars = hvterm_raw_get_chars,
  111. .put_chars = hvterm_raw_put_chars,
  112. .notifier_add = notifier_add_irq,
  113. .notifier_del = notifier_del_irq,
  114. .notifier_hangup = notifier_hangup_irq,
  115. };
  116. static ssize_t hvterm_hvsi_get_chars(uint32_t vtermno, u8 *buf, size_t count)
  117. {
  118. struct hvterm_priv *pv = hvterm_privs[vtermno];
  119. if (WARN_ON(!pv))
  120. return 0;
  121. return hvsilib_get_chars(&pv->hvsi, buf, count);
  122. }
  123. static ssize_t hvterm_hvsi_put_chars(uint32_t vtermno, const u8 *buf,
  124. size_t count)
  125. {
  126. struct hvterm_priv *pv = hvterm_privs[vtermno];
  127. if (WARN_ON(!pv))
  128. return 0;
  129. return hvsilib_put_chars(&pv->hvsi, buf, count);
  130. }
  131. static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
  132. {
  133. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  134. int rc;
  135. pr_devel("HVSI@%x: open !\n", pv->termno);
  136. rc = notifier_add_irq(hp, data);
  137. if (rc)
  138. return rc;
  139. return hvsilib_open(&pv->hvsi, hp);
  140. }
  141. static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
  142. {
  143. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  144. pr_devel("HVSI@%x: do close !\n", pv->termno);
  145. hvsilib_close(&pv->hvsi, hp);
  146. notifier_del_irq(hp, data);
  147. }
  148. static void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
  149. {
  150. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  151. pr_devel("HVSI@%x: do hangup !\n", pv->termno);
  152. hvsilib_close(&pv->hvsi, hp);
  153. notifier_hangup_irq(hp, data);
  154. }
  155. static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
  156. {
  157. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  158. if (!pv)
  159. return -EINVAL;
  160. return pv->hvsi.mctrl;
  161. }
  162. static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  163. unsigned int clear)
  164. {
  165. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  166. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  167. pv->termno, set, clear);
  168. if (set & TIOCM_DTR)
  169. hvsilib_write_mctrl(&pv->hvsi, 1);
  170. else if (clear & TIOCM_DTR)
  171. hvsilib_write_mctrl(&pv->hvsi, 0);
  172. return 0;
  173. }
  174. static const struct hv_ops hvterm_hvsi_ops = {
  175. .get_chars = hvterm_hvsi_get_chars,
  176. .put_chars = hvterm_hvsi_put_chars,
  177. .notifier_add = hvterm_hvsi_open,
  178. .notifier_del = hvterm_hvsi_close,
  179. .notifier_hangup = hvterm_hvsi_hangup,
  180. .tiocmget = hvterm_hvsi_tiocmget,
  181. .tiocmset = hvterm_hvsi_tiocmset,
  182. };
  183. static void udbg_hvc_putc(char c)
  184. {
  185. int count = -1;
  186. unsigned char bounce_buffer[16];
  187. if (!hvterm_privs[0])
  188. return;
  189. if (c == '\n')
  190. udbg_hvc_putc('\r');
  191. do {
  192. switch(hvterm_privs[0]->proto) {
  193. case HV_PROTOCOL_RAW:
  194. /*
  195. * hvterm_raw_put_chars requires at least a 16-byte
  196. * buffer, so go via the bounce buffer
  197. */
  198. bounce_buffer[0] = c;
  199. count = hvterm_raw_put_chars(0, bounce_buffer, 1);
  200. break;
  201. case HV_PROTOCOL_HVSI:
  202. count = hvterm_hvsi_put_chars(0, &c, 1);
  203. break;
  204. }
  205. } while (count == 0 || count == -EAGAIN);
  206. }
  207. static int udbg_hvc_getc_poll(void)
  208. {
  209. int rc = 0;
  210. char c;
  211. if (!hvterm_privs[0])
  212. return -1;
  213. switch(hvterm_privs[0]->proto) {
  214. case HV_PROTOCOL_RAW:
  215. rc = hvterm_raw_get_chars(0, &c, 1);
  216. break;
  217. case HV_PROTOCOL_HVSI:
  218. rc = hvterm_hvsi_get_chars(0, &c, 1);
  219. break;
  220. }
  221. if (!rc)
  222. return -1;
  223. return c;
  224. }
  225. static int udbg_hvc_getc(void)
  226. {
  227. int ch;
  228. if (!hvterm_privs[0])
  229. return -1;
  230. for (;;) {
  231. ch = udbg_hvc_getc_poll();
  232. if (ch == -1) {
  233. /* This shouldn't be needed...but... */
  234. volatile unsigned long delay;
  235. for (delay=0; delay < 2000000; delay++)
  236. ;
  237. } else {
  238. return ch;
  239. }
  240. }
  241. }
  242. static int hvc_vio_probe(struct vio_dev *vdev,
  243. const struct vio_device_id *id)
  244. {
  245. const struct hv_ops *ops;
  246. struct hvc_struct *hp;
  247. struct hvterm_priv *pv;
  248. hv_protocol_t proto;
  249. int i, termno = -1;
  250. /* probed with invalid parameters. */
  251. if (!vdev || !id)
  252. return -EPERM;
  253. if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
  254. proto = HV_PROTOCOL_RAW;
  255. ops = &hvterm_raw_ops;
  256. } else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
  257. proto = HV_PROTOCOL_HVSI;
  258. ops = &hvterm_hvsi_ops;
  259. } else {
  260. pr_err("hvc_vio: Unknown protocol for %pOF\n", vdev->dev.of_node);
  261. return -ENXIO;
  262. }
  263. pr_devel("hvc_vio_probe() device %pOF, using %s protocol\n",
  264. vdev->dev.of_node,
  265. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");
  266. /* Is it our boot one ? */
  267. if (hvterm_privs[0] == &hvterm_priv0 &&
  268. vdev->unit_address == hvterm_priv0.termno) {
  269. pv = hvterm_privs[0];
  270. termno = 0;
  271. pr_devel("->boot console, using termno 0\n");
  272. }
  273. /* nope, allocate a new one */
  274. else {
  275. for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
  276. if (!hvterm_privs[i])
  277. termno = i;
  278. pr_devel("->non-boot console, using termno %d\n", termno);
  279. if (termno < 0)
  280. return -ENODEV;
  281. pv = kzalloc_obj(struct hvterm_priv);
  282. if (!pv)
  283. return -ENOMEM;
  284. pv->termno = vdev->unit_address;
  285. pv->proto = proto;
  286. spin_lock_init(&pv->buf_lock);
  287. hvterm_privs[termno] = pv;
  288. hvsilib_init(&pv->hvsi, hvc_get_chars, hvc_put_chars,
  289. pv->termno, 0);
  290. }
  291. hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
  292. if (IS_ERR(hp))
  293. return PTR_ERR(hp);
  294. dev_set_drvdata(&vdev->dev, hp);
  295. /* register udbg if it's not there already for console 0 */
  296. if (hp->index == 0 && !udbg_putc) {
  297. udbg_putc = udbg_hvc_putc;
  298. udbg_getc = udbg_hvc_getc;
  299. udbg_getc_poll = udbg_hvc_getc_poll;
  300. }
  301. return 0;
  302. }
  303. static struct vio_driver hvc_vio_driver = {
  304. .id_table = hvc_driver_table,
  305. .probe = hvc_vio_probe,
  306. .name = hvc_driver_name,
  307. .driver = {
  308. .suppress_bind_attrs = true,
  309. },
  310. };
  311. static int __init hvc_vio_init(void)
  312. {
  313. int rc;
  314. /* Register as a vio device to receive callbacks */
  315. rc = vio_register_driver(&hvc_vio_driver);
  316. return rc;
  317. }
  318. device_initcall(hvc_vio_init); /* after drivers/tty/hvc/hvc_console.c */
  319. void __init hvc_vio_init_early(void)
  320. {
  321. const __be32 *termno;
  322. const struct hv_ops *ops;
  323. /* find the boot console from /chosen/stdout */
  324. /* Check if it's a virtual terminal */
  325. if (!of_node_name_prefix(of_stdout, "vty"))
  326. return;
  327. termno = of_get_property(of_stdout, "reg", NULL);
  328. if (termno == NULL)
  329. return;
  330. hvterm_priv0.termno = of_read_number(termno, 1);
  331. spin_lock_init(&hvterm_priv0.buf_lock);
  332. hvterm_privs[0] = &hvterm_priv0;
  333. /* Check the protocol */
  334. if (of_device_is_compatible(of_stdout, "hvterm1")) {
  335. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  336. ops = &hvterm_raw_ops;
  337. }
  338. else if (of_device_is_compatible(of_stdout, "hvterm-protocol")) {
  339. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  340. ops = &hvterm_hvsi_ops;
  341. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  342. hvterm_priv0.termno, 1);
  343. /* HVSI, perform the handshake now */
  344. hvsilib_establish(&hvterm_priv0.hvsi);
  345. } else
  346. return;
  347. udbg_putc = udbg_hvc_putc;
  348. udbg_getc = udbg_hvc_getc;
  349. udbg_getc_poll = udbg_hvc_getc_poll;
  350. #ifdef HVC_OLD_HVSI
  351. /* When using the old HVSI driver don't register the HVC
  352. * backend for HVSI, only do udbg
  353. */
  354. if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
  355. return;
  356. #endif
  357. /* Check whether the user has requested a different console. */
  358. if (!strstr(boot_command_line, "console="))
  359. add_preferred_console("hvc", 0, NULL);
  360. hvc_instantiate(0, 0, ops);
  361. }
  362. /* call this from early_init() for a working debug console on
  363. * vterm capable LPAR machines
  364. */
  365. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
  366. void __init udbg_init_debug_lpar(void)
  367. {
  368. /*
  369. * If we're running as a hypervisor then we definitely can't call the
  370. * hypervisor to print debug output (we *are* the hypervisor), so don't
  371. * register if we detect that MSR_HV=1.
  372. */
  373. if (mfmsr() & MSR_HV)
  374. return;
  375. hvterm_privs[0] = &hvterm_priv0;
  376. hvterm_priv0.termno = 0;
  377. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  378. spin_lock_init(&hvterm_priv0.buf_lock);
  379. udbg_putc = udbg_hvc_putc;
  380. udbg_getc = udbg_hvc_getc;
  381. udbg_getc_poll = udbg_hvc_getc_poll;
  382. }
  383. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
  384. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
  385. void __init udbg_init_debug_lpar_hvsi(void)
  386. {
  387. /* See comment above in udbg_init_debug_lpar() */
  388. if (mfmsr() & MSR_HV)
  389. return;
  390. hvterm_privs[0] = &hvterm_priv0;
  391. hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
  392. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  393. spin_lock_init(&hvterm_priv0.buf_lock);
  394. udbg_putc = udbg_hvc_putc;
  395. udbg_getc = udbg_hvc_getc;
  396. udbg_getc_poll = udbg_hvc_getc_poll;
  397. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  398. hvterm_priv0.termno, 1);
  399. hvsilib_establish(&hvterm_priv0.hvsi);
  400. }
  401. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */