sunhv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /* sunhv.c: Serial driver for SUN4V hypervisor console.
  2. *
  3. * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/errno.h>
  8. #include <linux/tty.h>
  9. #include <linux/tty_flip.h>
  10. #include <linux/major.h>
  11. #include <linux/circ_buf.h>
  12. #include <linux/serial.h>
  13. #include <linux/sysrq.h>
  14. #include <linux/console.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/init.h>
  19. #include <linux/of_device.h>
  20. #include <asm/hypervisor.h>
  21. #include <asm/spitfire.h>
  22. #include <asm/prom.h>
  23. #include <asm/irq.h>
  24. #include <asm/setup.h>
  25. #if defined(CONFIG_MAGIC_SYSRQ)
  26. #define SUPPORT_SYSRQ
  27. #endif
  28. #include <linux/serial_core.h>
  29. #include <linux/sunserialcore.h>
  30. #define CON_BREAK ((long)-1)
  31. #define CON_HUP ((long)-2)
  32. #define IGNORE_BREAK 0x1
  33. #define IGNORE_ALL 0x2
  34. static char *con_write_page;
  35. static char *con_read_page;
  36. static int hung_up = 0;
  37. static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit)
  38. {
  39. while (!uart_circ_empty(xmit)) {
  40. long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
  41. if (status != HV_EOK)
  42. break;
  43. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  44. port->icount.tx++;
  45. }
  46. }
  47. static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
  48. {
  49. while (!uart_circ_empty(xmit)) {
  50. unsigned long ra = __pa(xmit->buf + xmit->tail);
  51. unsigned long len, status, sent;
  52. len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
  53. UART_XMIT_SIZE);
  54. status = sun4v_con_write(ra, len, &sent);
  55. if (status != HV_EOK)
  56. break;
  57. xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
  58. port->icount.tx += sent;
  59. }
  60. }
  61. static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty)
  62. {
  63. int saw_console_brk = 0;
  64. int limit = 10000;
  65. while (limit-- > 0) {
  66. long status;
  67. long c = sun4v_con_getchar(&status);
  68. if (status == HV_EWOULDBLOCK)
  69. break;
  70. if (c == CON_BREAK) {
  71. if (uart_handle_break(port))
  72. continue;
  73. saw_console_brk = 1;
  74. c = 0;
  75. }
  76. if (c == CON_HUP) {
  77. hung_up = 1;
  78. uart_handle_dcd_change(port, 0);
  79. } else if (hung_up) {
  80. hung_up = 0;
  81. uart_handle_dcd_change(port, 1);
  82. }
  83. if (tty == NULL) {
  84. uart_handle_sysrq_char(port, c);
  85. continue;
  86. }
  87. port->icount.rx++;
  88. if (uart_handle_sysrq_char(port, c))
  89. continue;
  90. tty_insert_flip_char(tty, c, TTY_NORMAL);
  91. }
  92. return saw_console_brk;
  93. }
  94. static int receive_chars_read(struct uart_port *port, struct tty_struct *tty)
  95. {
  96. int saw_console_brk = 0;
  97. int limit = 10000;
  98. while (limit-- > 0) {
  99. unsigned long ra = __pa(con_read_page);
  100. unsigned long bytes_read, i;
  101. long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
  102. if (stat != HV_EOK) {
  103. bytes_read = 0;
  104. if (stat == CON_BREAK) {
  105. if (uart_handle_break(port))
  106. continue;
  107. saw_console_brk = 1;
  108. *con_read_page = 0;
  109. bytes_read = 1;
  110. } else if (stat == CON_HUP) {
  111. hung_up = 1;
  112. uart_handle_dcd_change(port, 0);
  113. continue;
  114. } else {
  115. /* HV_EWOULDBLOCK, etc. */
  116. break;
  117. }
  118. }
  119. if (hung_up) {
  120. hung_up = 0;
  121. uart_handle_dcd_change(port, 1);
  122. }
  123. for (i = 0; i < bytes_read; i++)
  124. uart_handle_sysrq_char(port, con_read_page[i]);
  125. if (tty == NULL)
  126. continue;
  127. port->icount.rx += bytes_read;
  128. tty_insert_flip_string(tty, con_read_page, bytes_read);
  129. }
  130. return saw_console_brk;
  131. }
  132. struct sunhv_ops {
  133. void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
  134. int (*receive_chars)(struct uart_port *port, struct tty_struct *tty);
  135. };
  136. static struct sunhv_ops bychar_ops = {
  137. .transmit_chars = transmit_chars_putchar,
  138. .receive_chars = receive_chars_getchar,
  139. };
  140. static struct sunhv_ops bywrite_ops = {
  141. .transmit_chars = transmit_chars_write,
  142. .receive_chars = receive_chars_read,
  143. };
  144. static struct sunhv_ops *sunhv_ops = &bychar_ops;
  145. static struct tty_struct *receive_chars(struct uart_port *port)
  146. {
  147. struct tty_struct *tty = NULL;
  148. if (port->state != NULL) /* Unopened serial console */
  149. tty = port->state->port.tty;
  150. if (sunhv_ops->receive_chars(port, tty))
  151. sun_do_break();
  152. return tty;
  153. }
  154. static void transmit_chars(struct uart_port *port)
  155. {
  156. struct circ_buf *xmit;
  157. if (!port->state)
  158. return;
  159. xmit = &port->state->xmit;
  160. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  161. return;
  162. sunhv_ops->transmit_chars(port, xmit);
  163. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  164. uart_write_wakeup(port);
  165. }
  166. static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
  167. {
  168. struct uart_port *port = dev_id;
  169. struct tty_struct *tty;
  170. unsigned long flags;
  171. spin_lock_irqsave(&port->lock, flags);
  172. tty = receive_chars(port);
  173. transmit_chars(port);
  174. spin_unlock_irqrestore(&port->lock, flags);
  175. if (tty)
  176. tty_flip_buffer_push(tty);
  177. return IRQ_HANDLED;
  178. }
  179. /* port->lock is not held. */
  180. static unsigned int sunhv_tx_empty(struct uart_port *port)
  181. {
  182. /* Transmitter is always empty for us. If the circ buffer
  183. * is non-empty or there is an x_char pending, our caller
  184. * will do the right thing and ignore what we return here.
  185. */
  186. return TIOCSER_TEMT;
  187. }
  188. /* port->lock held by caller. */
  189. static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
  190. {
  191. return;
  192. }
  193. /* port->lock is held by caller and interrupts are disabled. */
  194. static unsigned int sunhv_get_mctrl(struct uart_port *port)
  195. {
  196. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  197. }
  198. /* port->lock held by caller. */
  199. static void sunhv_stop_tx(struct uart_port *port)
  200. {
  201. return;
  202. }
  203. /* port->lock held by caller. */
  204. static void sunhv_start_tx(struct uart_port *port)
  205. {
  206. transmit_chars(port);
  207. }
  208. /* port->lock is not held. */
  209. static void sunhv_send_xchar(struct uart_port *port, char ch)
  210. {
  211. unsigned long flags;
  212. int limit = 10000;
  213. spin_lock_irqsave(&port->lock, flags);
  214. while (limit-- > 0) {
  215. long status = sun4v_con_putchar(ch);
  216. if (status == HV_EOK)
  217. break;
  218. udelay(1);
  219. }
  220. spin_unlock_irqrestore(&port->lock, flags);
  221. }
  222. /* port->lock held by caller. */
  223. static void sunhv_stop_rx(struct uart_port *port)
  224. {
  225. }
  226. /* port->lock held by caller. */
  227. static void sunhv_enable_ms(struct uart_port *port)
  228. {
  229. }
  230. /* port->lock is not held. */
  231. static void sunhv_break_ctl(struct uart_port *port, int break_state)
  232. {
  233. if (break_state) {
  234. unsigned long flags;
  235. int limit = 10000;
  236. spin_lock_irqsave(&port->lock, flags);
  237. while (limit-- > 0) {
  238. long status = sun4v_con_putchar(CON_BREAK);
  239. if (status == HV_EOK)
  240. break;
  241. udelay(1);
  242. }
  243. spin_unlock_irqrestore(&port->lock, flags);
  244. }
  245. }
  246. /* port->lock is not held. */
  247. static int sunhv_startup(struct uart_port *port)
  248. {
  249. return 0;
  250. }
  251. /* port->lock is not held. */
  252. static void sunhv_shutdown(struct uart_port *port)
  253. {
  254. }
  255. /* port->lock is not held. */
  256. static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
  257. struct ktermios *old)
  258. {
  259. unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  260. unsigned int quot = uart_get_divisor(port, baud);
  261. unsigned int iflag, cflag;
  262. unsigned long flags;
  263. spin_lock_irqsave(&port->lock, flags);
  264. iflag = termios->c_iflag;
  265. cflag = termios->c_cflag;
  266. port->ignore_status_mask = 0;
  267. if (iflag & IGNBRK)
  268. port->ignore_status_mask |= IGNORE_BREAK;
  269. if ((cflag & CREAD) == 0)
  270. port->ignore_status_mask |= IGNORE_ALL;
  271. /* XXX */
  272. uart_update_timeout(port, cflag,
  273. (port->uartclk / (16 * quot)));
  274. spin_unlock_irqrestore(&port->lock, flags);
  275. }
  276. static const char *sunhv_type(struct uart_port *port)
  277. {
  278. return "SUN4V HCONS";
  279. }
  280. static void sunhv_release_port(struct uart_port *port)
  281. {
  282. }
  283. static int sunhv_request_port(struct uart_port *port)
  284. {
  285. return 0;
  286. }
  287. static void sunhv_config_port(struct uart_port *port, int flags)
  288. {
  289. }
  290. static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
  291. {
  292. return -EINVAL;
  293. }
  294. static struct uart_ops sunhv_pops = {
  295. .tx_empty = sunhv_tx_empty,
  296. .set_mctrl = sunhv_set_mctrl,
  297. .get_mctrl = sunhv_get_mctrl,
  298. .stop_tx = sunhv_stop_tx,
  299. .start_tx = sunhv_start_tx,
  300. .send_xchar = sunhv_send_xchar,
  301. .stop_rx = sunhv_stop_rx,
  302. .enable_ms = sunhv_enable_ms,
  303. .break_ctl = sunhv_break_ctl,
  304. .startup = sunhv_startup,
  305. .shutdown = sunhv_shutdown,
  306. .set_termios = sunhv_set_termios,
  307. .type = sunhv_type,
  308. .release_port = sunhv_release_port,
  309. .request_port = sunhv_request_port,
  310. .config_port = sunhv_config_port,
  311. .verify_port = sunhv_verify_port,
  312. };
  313. static struct uart_driver sunhv_reg = {
  314. .owner = THIS_MODULE,
  315. .driver_name = "sunhv",
  316. .dev_name = "ttyS",
  317. .major = TTY_MAJOR,
  318. };
  319. static struct uart_port *sunhv_port;
  320. /* Copy 's' into the con_write_page, decoding "\n" into
  321. * "\r\n" along the way. We have to return two lengths
  322. * because the caller needs to know how much to advance
  323. * 's' and also how many bytes to output via con_write_page.
  324. */
  325. static int fill_con_write_page(const char *s, unsigned int n,
  326. unsigned long *page_bytes)
  327. {
  328. const char *orig_s = s;
  329. char *p = con_write_page;
  330. int left = PAGE_SIZE;
  331. while (n--) {
  332. if (*s == '\n') {
  333. if (left < 2)
  334. break;
  335. *p++ = '\r';
  336. left--;
  337. } else if (left < 1)
  338. break;
  339. *p++ = *s++;
  340. left--;
  341. }
  342. *page_bytes = p - con_write_page;
  343. return s - orig_s;
  344. }
  345. static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
  346. {
  347. struct uart_port *port = sunhv_port;
  348. unsigned long flags;
  349. int locked = 1;
  350. local_irq_save(flags);
  351. if (port->sysrq) {
  352. locked = 0;
  353. } else if (oops_in_progress) {
  354. locked = spin_trylock(&port->lock);
  355. } else
  356. spin_lock(&port->lock);
  357. while (n > 0) {
  358. unsigned long ra = __pa(con_write_page);
  359. unsigned long page_bytes;
  360. unsigned int cpy = fill_con_write_page(s, n,
  361. &page_bytes);
  362. n -= cpy;
  363. s += cpy;
  364. while (page_bytes > 0) {
  365. unsigned long written;
  366. int limit = 1000000;
  367. while (limit--) {
  368. unsigned long stat;
  369. stat = sun4v_con_write(ra, page_bytes,
  370. &written);
  371. if (stat == HV_EOK)
  372. break;
  373. udelay(1);
  374. }
  375. if (limit < 0)
  376. break;
  377. page_bytes -= written;
  378. ra += written;
  379. }
  380. }
  381. if (locked)
  382. spin_unlock(&port->lock);
  383. local_irq_restore(flags);
  384. }
  385. static inline void sunhv_console_putchar(struct uart_port *port, char c)
  386. {
  387. int limit = 1000000;
  388. while (limit-- > 0) {
  389. long status = sun4v_con_putchar(c);
  390. if (status == HV_EOK)
  391. break;
  392. udelay(1);
  393. }
  394. }
  395. static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
  396. {
  397. struct uart_port *port = sunhv_port;
  398. unsigned long flags;
  399. int i, locked = 1;
  400. local_irq_save(flags);
  401. if (port->sysrq) {
  402. locked = 0;
  403. } else if (oops_in_progress) {
  404. locked = spin_trylock(&port->lock);
  405. } else
  406. spin_lock(&port->lock);
  407. for (i = 0; i < n; i++) {
  408. if (*s == '\n')
  409. sunhv_console_putchar(port, '\r');
  410. sunhv_console_putchar(port, *s++);
  411. }
  412. if (locked)
  413. spin_unlock(&port->lock);
  414. local_irq_restore(flags);
  415. }
  416. static struct console sunhv_console = {
  417. .name = "ttyHV",
  418. .write = sunhv_console_write_bychar,
  419. .device = uart_console_device,
  420. .flags = CON_PRINTBUFFER,
  421. .index = -1,
  422. .data = &sunhv_reg,
  423. };
  424. static int __devinit hv_probe(struct platform_device *op)
  425. {
  426. struct uart_port *port;
  427. unsigned long minor;
  428. int err;
  429. if (op->archdata.irqs[0] == 0xffffffff)
  430. return -ENODEV;
  431. port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
  432. if (unlikely(!port))
  433. return -ENOMEM;
  434. minor = 1;
  435. if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
  436. minor >= 1) {
  437. err = -ENOMEM;
  438. con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  439. if (!con_write_page)
  440. goto out_free_port;
  441. con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  442. if (!con_read_page)
  443. goto out_free_con_write_page;
  444. sunhv_console.write = sunhv_console_write_paged;
  445. sunhv_ops = &bywrite_ops;
  446. }
  447. sunhv_port = port;
  448. port->line = 0;
  449. port->ops = &sunhv_pops;
  450. port->type = PORT_SUNHV;
  451. port->uartclk = ( 29491200 / 16 ); /* arbitrary */
  452. port->membase = (unsigned char __iomem *) __pa(port);
  453. port->irq = op->archdata.irqs[0];
  454. port->dev = &op->dev;
  455. err = sunserial_register_minors(&sunhv_reg, 1);
  456. if (err)
  457. goto out_free_con_read_page;
  458. sunserial_console_match(&sunhv_console, op->dev.of_node,
  459. &sunhv_reg, port->line, false);
  460. err = uart_add_one_port(&sunhv_reg, port);
  461. if (err)
  462. goto out_unregister_driver;
  463. err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
  464. if (err)
  465. goto out_remove_port;
  466. dev_set_drvdata(&op->dev, port);
  467. return 0;
  468. out_remove_port:
  469. uart_remove_one_port(&sunhv_reg, port);
  470. out_unregister_driver:
  471. sunserial_unregister_minors(&sunhv_reg, 1);
  472. out_free_con_read_page:
  473. kfree(con_read_page);
  474. out_free_con_write_page:
  475. kfree(con_write_page);
  476. out_free_port:
  477. kfree(port);
  478. sunhv_port = NULL;
  479. return err;
  480. }
  481. static int __devexit hv_remove(struct platform_device *dev)
  482. {
  483. struct uart_port *port = dev_get_drvdata(&dev->dev);
  484. free_irq(port->irq, port);
  485. uart_remove_one_port(&sunhv_reg, port);
  486. sunserial_unregister_minors(&sunhv_reg, 1);
  487. kfree(port);
  488. sunhv_port = NULL;
  489. dev_set_drvdata(&dev->dev, NULL);
  490. return 0;
  491. }
  492. static const struct of_device_id hv_match[] = {
  493. {
  494. .name = "console",
  495. .compatible = "qcn",
  496. },
  497. {
  498. .name = "console",
  499. .compatible = "SUNW,sun4v-console",
  500. },
  501. {},
  502. };
  503. MODULE_DEVICE_TABLE(of, hv_match);
  504. static struct platform_driver hv_driver = {
  505. .driver = {
  506. .name = "hv",
  507. .owner = THIS_MODULE,
  508. .of_match_table = hv_match,
  509. },
  510. .probe = hv_probe,
  511. .remove = __devexit_p(hv_remove),
  512. };
  513. static int __init sunhv_init(void)
  514. {
  515. if (tlb_type != hypervisor)
  516. return -ENODEV;
  517. return platform_driver_register(&hv_driver);
  518. }
  519. static void __exit sunhv_exit(void)
  520. {
  521. platform_driver_unregister(&hv_driver);
  522. }
  523. module_init(sunhv_init);
  524. module_exit(sunhv_exit);
  525. MODULE_AUTHOR("David S. Miller");
  526. MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
  527. MODULE_VERSION("2.0");
  528. MODULE_LICENSE("GPL");