hvc_vio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 <asm/hvconsole.h>
  30. #include <asm/vio.h>
  31. #include <asm/prom.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. char buf[SIZE_VIO_GET_CHARS];
  54. int left;
  55. int 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 int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)
  61. {
  62. struct hvterm_priv *pv = hvterm_privs[vtermno];
  63. unsigned long i;
  64. unsigned long flags;
  65. int 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 int hvterm_raw_put_chars(uint32_t vtermno, const char *buf, int count)
  102. {
  103. struct hvterm_priv *pv = hvterm_privs[vtermno];
  104. if (WARN_ON(!pv))
  105. return 0;
  106. return hvc_put_chars(pv->termno, buf, count);
  107. }
  108. static const struct hv_ops hvterm_raw_ops = {
  109. .get_chars = hvterm_raw_get_chars,
  110. .put_chars = hvterm_raw_put_chars,
  111. .notifier_add = notifier_add_irq,
  112. .notifier_del = notifier_del_irq,
  113. .notifier_hangup = notifier_hangup_irq,
  114. };
  115. static int hvterm_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
  116. {
  117. struct hvterm_priv *pv = hvterm_privs[vtermno];
  118. if (WARN_ON(!pv))
  119. return 0;
  120. return hvsilib_get_chars(&pv->hvsi, buf, count);
  121. }
  122. static int hvterm_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
  123. {
  124. struct hvterm_priv *pv = hvterm_privs[vtermno];
  125. if (WARN_ON(!pv))
  126. return 0;
  127. return hvsilib_put_chars(&pv->hvsi, buf, count);
  128. }
  129. static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
  130. {
  131. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  132. int rc;
  133. pr_devel("HVSI@%x: open !\n", pv->termno);
  134. rc = notifier_add_irq(hp, data);
  135. if (rc)
  136. return rc;
  137. return hvsilib_open(&pv->hvsi, hp);
  138. }
  139. static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
  140. {
  141. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  142. pr_devel("HVSI@%x: do close !\n", pv->termno);
  143. hvsilib_close(&pv->hvsi, hp);
  144. notifier_del_irq(hp, data);
  145. }
  146. void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
  147. {
  148. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  149. pr_devel("HVSI@%x: do hangup !\n", pv->termno);
  150. hvsilib_close(&pv->hvsi, hp);
  151. notifier_hangup_irq(hp, data);
  152. }
  153. static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
  154. {
  155. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  156. if (!pv)
  157. return -EINVAL;
  158. return pv->hvsi.mctrl;
  159. }
  160. static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  161. unsigned int clear)
  162. {
  163. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  164. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  165. pv->termno, set, clear);
  166. if (set & TIOCM_DTR)
  167. hvsilib_write_mctrl(&pv->hvsi, 1);
  168. else if (clear & TIOCM_DTR)
  169. hvsilib_write_mctrl(&pv->hvsi, 0);
  170. return 0;
  171. }
  172. static const struct hv_ops hvterm_hvsi_ops = {
  173. .get_chars = hvterm_hvsi_get_chars,
  174. .put_chars = hvterm_hvsi_put_chars,
  175. .notifier_add = hvterm_hvsi_open,
  176. .notifier_del = hvterm_hvsi_close,
  177. .notifier_hangup = hvterm_hvsi_hangup,
  178. .tiocmget = hvterm_hvsi_tiocmget,
  179. .tiocmset = hvterm_hvsi_tiocmset,
  180. };
  181. static void udbg_hvc_putc(char c)
  182. {
  183. int count = -1;
  184. unsigned char bounce_buffer[16];
  185. if (!hvterm_privs[0])
  186. return;
  187. if (c == '\n')
  188. udbg_hvc_putc('\r');
  189. do {
  190. switch(hvterm_privs[0]->proto) {
  191. case HV_PROTOCOL_RAW:
  192. /*
  193. * hvterm_raw_put_chars requires at least a 16-byte
  194. * buffer, so go via the bounce buffer
  195. */
  196. bounce_buffer[0] = c;
  197. count = hvterm_raw_put_chars(0, bounce_buffer, 1);
  198. break;
  199. case HV_PROTOCOL_HVSI:
  200. count = hvterm_hvsi_put_chars(0, &c, 1);
  201. break;
  202. }
  203. } while(count == 0);
  204. }
  205. static int udbg_hvc_getc_poll(void)
  206. {
  207. int rc = 0;
  208. char c;
  209. if (!hvterm_privs[0])
  210. return -1;
  211. switch(hvterm_privs[0]->proto) {
  212. case HV_PROTOCOL_RAW:
  213. rc = hvterm_raw_get_chars(0, &c, 1);
  214. break;
  215. case HV_PROTOCOL_HVSI:
  216. rc = hvterm_hvsi_get_chars(0, &c, 1);
  217. break;
  218. }
  219. if (!rc)
  220. return -1;
  221. return c;
  222. }
  223. static int udbg_hvc_getc(void)
  224. {
  225. int ch;
  226. if (!hvterm_privs[0])
  227. return -1;
  228. for (;;) {
  229. ch = udbg_hvc_getc_poll();
  230. if (ch == -1) {
  231. /* This shouldn't be needed...but... */
  232. volatile unsigned long delay;
  233. for (delay=0; delay < 2000000; delay++)
  234. ;
  235. } else {
  236. return ch;
  237. }
  238. }
  239. }
  240. static int hvc_vio_probe(struct vio_dev *vdev,
  241. const struct vio_device_id *id)
  242. {
  243. const struct hv_ops *ops;
  244. struct hvc_struct *hp;
  245. struct hvterm_priv *pv;
  246. hv_protocol_t proto;
  247. int i, termno = -1;
  248. /* probed with invalid parameters. */
  249. if (!vdev || !id)
  250. return -EPERM;
  251. if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
  252. proto = HV_PROTOCOL_RAW;
  253. ops = &hvterm_raw_ops;
  254. } else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
  255. proto = HV_PROTOCOL_HVSI;
  256. ops = &hvterm_hvsi_ops;
  257. } else {
  258. pr_err("hvc_vio: Unknown protocol for %pOF\n", vdev->dev.of_node);
  259. return -ENXIO;
  260. }
  261. pr_devel("hvc_vio_probe() device %pOF, using %s protocol\n",
  262. vdev->dev.of_node,
  263. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");
  264. /* Is it our boot one ? */
  265. if (hvterm_privs[0] == &hvterm_priv0 &&
  266. vdev->unit_address == hvterm_priv0.termno) {
  267. pv = hvterm_privs[0];
  268. termno = 0;
  269. pr_devel("->boot console, using termno 0\n");
  270. }
  271. /* nope, allocate a new one */
  272. else {
  273. for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
  274. if (!hvterm_privs[i])
  275. termno = i;
  276. pr_devel("->non-boot console, using termno %d\n", termno);
  277. if (termno < 0)
  278. return -ENODEV;
  279. pv = kzalloc(sizeof(struct hvterm_priv), GFP_KERNEL);
  280. if (!pv)
  281. return -ENOMEM;
  282. pv->termno = vdev->unit_address;
  283. pv->proto = proto;
  284. spin_lock_init(&pv->buf_lock);
  285. hvterm_privs[termno] = pv;
  286. hvsilib_init(&pv->hvsi, hvc_get_chars, hvc_put_chars,
  287. pv->termno, 0);
  288. }
  289. hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
  290. if (IS_ERR(hp))
  291. return PTR_ERR(hp);
  292. dev_set_drvdata(&vdev->dev, hp);
  293. /* register udbg if it's not there already for console 0 */
  294. if (hp->index == 0 && !udbg_putc) {
  295. udbg_putc = udbg_hvc_putc;
  296. udbg_getc = udbg_hvc_getc;
  297. udbg_getc_poll = udbg_hvc_getc_poll;
  298. }
  299. return 0;
  300. }
  301. static struct vio_driver hvc_vio_driver = {
  302. .id_table = hvc_driver_table,
  303. .probe = hvc_vio_probe,
  304. .name = hvc_driver_name,
  305. .driver = {
  306. .suppress_bind_attrs = true,
  307. },
  308. };
  309. static int __init hvc_vio_init(void)
  310. {
  311. int rc;
  312. /* Register as a vio device to receive callbacks */
  313. rc = vio_register_driver(&hvc_vio_driver);
  314. return rc;
  315. }
  316. device_initcall(hvc_vio_init); /* after drivers/tty/hvc/hvc_console.c */
  317. void __init hvc_vio_init_early(void)
  318. {
  319. const __be32 *termno;
  320. const struct hv_ops *ops;
  321. /* find the boot console from /chosen/stdout */
  322. /* Check if it's a virtual terminal */
  323. if (!of_node_name_prefix(of_stdout, "vty"))
  324. return;
  325. termno = of_get_property(of_stdout, "reg", NULL);
  326. if (termno == NULL)
  327. return;
  328. hvterm_priv0.termno = of_read_number(termno, 1);
  329. spin_lock_init(&hvterm_priv0.buf_lock);
  330. hvterm_privs[0] = &hvterm_priv0;
  331. /* Check the protocol */
  332. if (of_device_is_compatible(of_stdout, "hvterm1")) {
  333. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  334. ops = &hvterm_raw_ops;
  335. }
  336. else if (of_device_is_compatible(of_stdout, "hvterm-protocol")) {
  337. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  338. ops = &hvterm_hvsi_ops;
  339. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  340. hvterm_priv0.termno, 1);
  341. /* HVSI, perform the handshake now */
  342. hvsilib_establish(&hvterm_priv0.hvsi);
  343. } else
  344. return;
  345. udbg_putc = udbg_hvc_putc;
  346. udbg_getc = udbg_hvc_getc;
  347. udbg_getc_poll = udbg_hvc_getc_poll;
  348. #ifdef HVC_OLD_HVSI
  349. /* When using the old HVSI driver don't register the HVC
  350. * backend for HVSI, only do udbg
  351. */
  352. if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
  353. return;
  354. #endif
  355. /* Check whether the user has requested a different console. */
  356. if (!strstr(boot_command_line, "console="))
  357. add_preferred_console("hvc", 0, NULL);
  358. hvc_instantiate(0, 0, ops);
  359. }
  360. /* call this from early_init() for a working debug console on
  361. * vterm capable LPAR machines
  362. */
  363. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
  364. void __init udbg_init_debug_lpar(void)
  365. {
  366. /*
  367. * If we're running as a hypervisor then we definitely can't call the
  368. * hypervisor to print debug output (we *are* the hypervisor), so don't
  369. * register if we detect that MSR_HV=1.
  370. */
  371. if (mfmsr() & MSR_HV)
  372. return;
  373. hvterm_privs[0] = &hvterm_priv0;
  374. hvterm_priv0.termno = 0;
  375. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  376. spin_lock_init(&hvterm_priv0.buf_lock);
  377. udbg_putc = udbg_hvc_putc;
  378. udbg_getc = udbg_hvc_getc;
  379. udbg_getc_poll = udbg_hvc_getc_poll;
  380. }
  381. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
  382. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
  383. void __init udbg_init_debug_lpar_hvsi(void)
  384. {
  385. /* See comment above in udbg_init_debug_lpar() */
  386. if (mfmsr() & MSR_HV)
  387. return;
  388. hvterm_privs[0] = &hvterm_priv0;
  389. hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
  390. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  391. spin_lock_init(&hvterm_priv0.buf_lock);
  392. udbg_putc = udbg_hvc_putc;
  393. udbg_getc = udbg_hvc_getc;
  394. udbg_getc_poll = udbg_hvc_getc_poll;
  395. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  396. hvterm_priv0.termno, 1);
  397. hvsilib_establish(&hvterm_priv0.hvsi);
  398. }
  399. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */