hvc_vio.c 11 KB

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