hvc_vio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <linux/module.h>
  43. #include <asm/hvconsole.h>
  44. #include <asm/vio.h>
  45. #include <asm/prom.h>
  46. #include <asm/hvsi.h>
  47. #include <asm/udbg.h>
  48. #include "hvc_console.h"
  49. static const char hvc_driver_name[] = "hvc_console";
  50. static struct vio_device_id hvc_driver_table[] __devinitdata = {
  51. {"serial", "hvterm1"},
  52. #ifndef HVC_OLD_HVSI
  53. {"serial", "hvterm-protocol"},
  54. #endif
  55. { "", "" }
  56. };
  57. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  58. typedef enum hv_protocol {
  59. HV_PROTOCOL_RAW,
  60. HV_PROTOCOL_HVSI
  61. } hv_protocol_t;
  62. struct hvterm_priv {
  63. u32 termno; /* HV term number */
  64. hv_protocol_t proto; /* Raw data or HVSI packets */
  65. struct hvsi_priv hvsi; /* HVSI specific data */
  66. spinlock_t buf_lock;
  67. char buf[SIZE_VIO_GET_CHARS];
  68. int left;
  69. int offset;
  70. };
  71. static struct hvterm_priv *hvterm_privs[MAX_NR_HVC_CONSOLES];
  72. /* For early boot console */
  73. static struct hvterm_priv hvterm_priv0;
  74. static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)
  75. {
  76. struct hvterm_priv *pv = hvterm_privs[vtermno];
  77. unsigned long i;
  78. unsigned long flags;
  79. int got;
  80. if (WARN_ON(!pv))
  81. return 0;
  82. spin_lock_irqsave(&pv->buf_lock, flags);
  83. if (pv->left == 0) {
  84. pv->offset = 0;
  85. pv->left = hvc_get_chars(pv->termno, pv->buf, count);
  86. /*
  87. * Work around a HV bug where it gives us a null
  88. * after every \r. -- paulus
  89. */
  90. for (i = 1; i < pv->left; ++i) {
  91. if (pv->buf[i] == 0 && pv->buf[i-1] == '\r') {
  92. --pv->left;
  93. if (i < pv->left) {
  94. memmove(&pv->buf[i], &pv->buf[i+1],
  95. pv->left - i);
  96. }
  97. }
  98. }
  99. }
  100. got = min(count, pv->left);
  101. memcpy(buf, &pv->buf[pv->offset], got);
  102. pv->offset += got;
  103. pv->left -= got;
  104. spin_unlock_irqrestore(&pv->buf_lock, flags);
  105. return got;
  106. }
  107. static int hvterm_raw_put_chars(uint32_t vtermno, const char *buf, int count)
  108. {
  109. struct hvterm_priv *pv = hvterm_privs[vtermno];
  110. if (WARN_ON(!pv))
  111. return 0;
  112. return hvc_put_chars(pv->termno, buf, count);
  113. }
  114. static const struct hv_ops hvterm_raw_ops = {
  115. .get_chars = hvterm_raw_get_chars,
  116. .put_chars = hvterm_raw_put_chars,
  117. .notifier_add = notifier_add_irq,
  118. .notifier_del = notifier_del_irq,
  119. .notifier_hangup = notifier_hangup_irq,
  120. };
  121. static int hvterm_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
  122. {
  123. struct hvterm_priv *pv = hvterm_privs[vtermno];
  124. if (WARN_ON(!pv))
  125. return 0;
  126. return hvsilib_get_chars(&pv->hvsi, buf, count);
  127. }
  128. static int hvterm_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
  129. {
  130. struct hvterm_priv *pv = hvterm_privs[vtermno];
  131. if (WARN_ON(!pv))
  132. return 0;
  133. return hvsilib_put_chars(&pv->hvsi, buf, count);
  134. }
  135. static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
  136. {
  137. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  138. int rc;
  139. pr_devel("HVSI@%x: open !\n", pv->termno);
  140. rc = notifier_add_irq(hp, data);
  141. if (rc)
  142. return rc;
  143. return hvsilib_open(&pv->hvsi, hp);
  144. }
  145. static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
  146. {
  147. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  148. pr_devel("HVSI@%x: do close !\n", pv->termno);
  149. hvsilib_close(&pv->hvsi, hp);
  150. notifier_del_irq(hp, data);
  151. }
  152. void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
  153. {
  154. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  155. pr_devel("HVSI@%x: do hangup !\n", pv->termno);
  156. hvsilib_close(&pv->hvsi, hp);
  157. notifier_hangup_irq(hp, data);
  158. }
  159. static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
  160. {
  161. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  162. if (!pv)
  163. return -EINVAL;
  164. return pv->hvsi.mctrl;
  165. }
  166. static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  167. unsigned int clear)
  168. {
  169. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  170. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  171. pv->termno, set, clear);
  172. if (set & TIOCM_DTR)
  173. hvsilib_write_mctrl(&pv->hvsi, 1);
  174. else if (clear & TIOCM_DTR)
  175. hvsilib_write_mctrl(&pv->hvsi, 0);
  176. return 0;
  177. }
  178. static const struct hv_ops hvterm_hvsi_ops = {
  179. .get_chars = hvterm_hvsi_get_chars,
  180. .put_chars = hvterm_hvsi_put_chars,
  181. .notifier_add = hvterm_hvsi_open,
  182. .notifier_del = hvterm_hvsi_close,
  183. .notifier_hangup = hvterm_hvsi_hangup,
  184. .tiocmget = hvterm_hvsi_tiocmget,
  185. .tiocmset = hvterm_hvsi_tiocmset,
  186. };
  187. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  188. const struct vio_device_id *id)
  189. {
  190. const struct hv_ops *ops;
  191. struct hvc_struct *hp;
  192. struct hvterm_priv *pv;
  193. hv_protocol_t proto;
  194. int i, termno = -1;
  195. /* probed with invalid parameters. */
  196. if (!vdev || !id)
  197. return -EPERM;
  198. if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
  199. proto = HV_PROTOCOL_RAW;
  200. ops = &hvterm_raw_ops;
  201. } else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
  202. proto = HV_PROTOCOL_HVSI;
  203. ops = &hvterm_hvsi_ops;
  204. } else {
  205. pr_err("hvc_vio: Unkown protocol for %s\n", vdev->dev.of_node->full_name);
  206. return -ENXIO;
  207. }
  208. pr_devel("hvc_vio_probe() device %s, using %s protocol\n",
  209. vdev->dev.of_node->full_name,
  210. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");
  211. /* Is it our boot one ? */
  212. if (hvterm_privs[0] == &hvterm_priv0 &&
  213. vdev->unit_address == hvterm_priv0.termno) {
  214. pv = hvterm_privs[0];
  215. termno = 0;
  216. pr_devel("->boot console, using termno 0\n");
  217. }
  218. /* nope, allocate a new one */
  219. else {
  220. for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
  221. if (!hvterm_privs[i])
  222. termno = i;
  223. pr_devel("->non-boot console, using termno %d\n", termno);
  224. if (termno < 0)
  225. return -ENODEV;
  226. pv = kzalloc(sizeof(struct hvterm_priv), GFP_KERNEL);
  227. if (!pv)
  228. return -ENOMEM;
  229. pv->termno = vdev->unit_address;
  230. pv->proto = proto;
  231. spin_lock_init(&pv->buf_lock);
  232. hvterm_privs[termno] = pv;
  233. hvsilib_init(&pv->hvsi, hvc_get_chars, hvc_put_chars,
  234. pv->termno, 0);
  235. }
  236. hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
  237. if (IS_ERR(hp))
  238. return PTR_ERR(hp);
  239. dev_set_drvdata(&vdev->dev, hp);
  240. return 0;
  241. }
  242. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  243. {
  244. struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
  245. int rc, termno;
  246. termno = hp->vtermno;
  247. rc = hvc_remove(hp);
  248. if (rc == 0) {
  249. if (hvterm_privs[termno] != &hvterm_priv0)
  250. kfree(hvterm_privs[termno]);
  251. hvterm_privs[termno] = NULL;
  252. }
  253. return rc;
  254. }
  255. static struct vio_driver hvc_vio_driver = {
  256. .id_table = hvc_driver_table,
  257. .probe = hvc_vio_probe,
  258. .remove = hvc_vio_remove,
  259. .name = hvc_driver_name,
  260. };
  261. static int __init hvc_vio_init(void)
  262. {
  263. int rc;
  264. /* Register as a vio device to receive callbacks */
  265. rc = vio_register_driver(&hvc_vio_driver);
  266. return rc;
  267. }
  268. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  269. static void __exit hvc_vio_exit(void)
  270. {
  271. vio_unregister_driver(&hvc_vio_driver);
  272. }
  273. module_exit(hvc_vio_exit);
  274. static void udbg_hvc_putc(char c)
  275. {
  276. int count = -1;
  277. if (c == '\n')
  278. udbg_hvc_putc('\r');
  279. do {
  280. switch(hvterm_priv0.proto) {
  281. case HV_PROTOCOL_RAW:
  282. count = hvterm_raw_put_chars(0, &c, 1);
  283. break;
  284. case HV_PROTOCOL_HVSI:
  285. count = hvterm_hvsi_put_chars(0, &c, 1);
  286. break;
  287. }
  288. } while(count == 0);
  289. }
  290. static int udbg_hvc_getc_poll(void)
  291. {
  292. int rc = 0;
  293. char c;
  294. switch(hvterm_priv0.proto) {
  295. case HV_PROTOCOL_RAW:
  296. rc = hvterm_raw_get_chars(0, &c, 1);
  297. break;
  298. case HV_PROTOCOL_HVSI:
  299. rc = hvterm_hvsi_get_chars(0, &c, 1);
  300. break;
  301. }
  302. if (!rc)
  303. return -1;
  304. return c;
  305. }
  306. static int udbg_hvc_getc(void)
  307. {
  308. int ch;
  309. for (;;) {
  310. ch = udbg_hvc_getc_poll();
  311. if (ch == -1) {
  312. /* This shouldn't be needed...but... */
  313. volatile unsigned long delay;
  314. for (delay=0; delay < 2000000; delay++)
  315. ;
  316. } else {
  317. return ch;
  318. }
  319. }
  320. }
  321. void __init hvc_vio_init_early(void)
  322. {
  323. struct device_node *stdout_node;
  324. const u32 *termno;
  325. const char *name;
  326. const struct hv_ops *ops;
  327. /* find the boot console from /chosen/stdout */
  328. if (!of_chosen)
  329. return;
  330. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  331. if (name == NULL)
  332. return;
  333. stdout_node = of_find_node_by_path(name);
  334. if (!stdout_node)
  335. return;
  336. name = of_get_property(stdout_node, "name", NULL);
  337. if (!name) {
  338. printk(KERN_WARNING "stdout node missing 'name' property!\n");
  339. goto out;
  340. }
  341. /* Check if it's a virtual terminal */
  342. if (strncmp(name, "vty", 3) != 0)
  343. goto out;
  344. termno = of_get_property(stdout_node, "reg", NULL);
  345. if (termno == NULL)
  346. goto out;
  347. hvterm_priv0.termno = *termno;
  348. spin_lock_init(&hvterm_priv0.buf_lock);
  349. hvterm_privs[0] = &hvterm_priv0;
  350. /* Check the protocol */
  351. if (of_device_is_compatible(stdout_node, "hvterm1")) {
  352. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  353. ops = &hvterm_raw_ops;
  354. }
  355. else if (of_device_is_compatible(stdout_node, "hvterm-protocol")) {
  356. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  357. ops = &hvterm_hvsi_ops;
  358. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  359. hvterm_priv0.termno, 1);
  360. /* HVSI, perform the handshake now */
  361. hvsilib_establish(&hvterm_priv0.hvsi);
  362. } else
  363. goto out;
  364. udbg_putc = udbg_hvc_putc;
  365. udbg_getc = udbg_hvc_getc;
  366. udbg_getc_poll = udbg_hvc_getc_poll;
  367. #ifdef HVC_OLD_HVSI
  368. /* When using the old HVSI driver don't register the HVC
  369. * backend for HVSI, only do udbg
  370. */
  371. if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
  372. goto out;
  373. #endif
  374. add_preferred_console("hvc", 0, NULL);
  375. hvc_instantiate(0, 0, ops);
  376. out:
  377. of_node_put(stdout_node);
  378. }
  379. /* call this from early_init() for a working debug console on
  380. * vterm capable LPAR machines
  381. */
  382. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
  383. void __init udbg_init_debug_lpar(void)
  384. {
  385. hvterm_privs[0] = &hvterm_priv0;
  386. hvterm_priv0.termno = 0;
  387. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  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. }
  393. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
  394. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
  395. void __init udbg_init_debug_lpar_hvsi(void)
  396. {
  397. hvterm_privs[0] = &hvterm_priv0;
  398. hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
  399. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  400. spin_lock_init(&hvterm_priv0.buf_lock);
  401. udbg_putc = udbg_hvc_putc;
  402. udbg_getc = udbg_hvc_getc;
  403. udbg_getc_poll = udbg_hvc_getc_poll;
  404. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  405. hvterm_priv0.termno, 1);
  406. hvsilib_establish(&hvterm_priv0.hvsi);
  407. }
  408. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */