hvc_vio.c 12 KB

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