hvc_vio.c 11 KB

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