sunhv.c 14 KB

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