sunhv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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)
  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 (port->state == 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(&port->state->port, c, TTY_NORMAL);
  91. }
  92. return saw_console_brk;
  93. }
  94. static int receive_chars_read(struct uart_port *port)
  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 (port->state == NULL)
  126. continue;
  127. port->icount.rx += bytes_read;
  128. tty_insert_flip_string(&port->state->port, con_read_page,
  129. bytes_read);
  130. }
  131. return saw_console_brk;
  132. }
  133. struct sunhv_ops {
  134. void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
  135. int (*receive_chars)(struct uart_port *port);
  136. };
  137. static struct sunhv_ops bychar_ops = {
  138. .transmit_chars = transmit_chars_putchar,
  139. .receive_chars = receive_chars_getchar,
  140. };
  141. static struct sunhv_ops bywrite_ops = {
  142. .transmit_chars = transmit_chars_write,
  143. .receive_chars = receive_chars_read,
  144. };
  145. static struct sunhv_ops *sunhv_ops = &bychar_ops;
  146. static struct tty_port *receive_chars(struct uart_port *port)
  147. {
  148. struct tty_port *tport = NULL;
  149. if (port->state != NULL) /* Unopened serial console */
  150. tport = &port->state->port;
  151. if (sunhv_ops->receive_chars(port))
  152. sun_do_break();
  153. return tport;
  154. }
  155. static void transmit_chars(struct uart_port *port)
  156. {
  157. struct circ_buf *xmit;
  158. if (!port->state)
  159. return;
  160. xmit = &port->state->xmit;
  161. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  162. return;
  163. sunhv_ops->transmit_chars(port, xmit);
  164. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  165. uart_write_wakeup(port);
  166. }
  167. static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
  168. {
  169. struct uart_port *port = dev_id;
  170. struct tty_port *tport;
  171. unsigned long flags;
  172. spin_lock_irqsave(&port->lock, flags);
  173. tport = receive_chars(port);
  174. transmit_chars(port);
  175. spin_unlock_irqrestore(&port->lock, flags);
  176. if (tport)
  177. tty_flip_buffer_push(tport);
  178. return IRQ_HANDLED;
  179. }
  180. /* port->lock is not held. */
  181. static unsigned int sunhv_tx_empty(struct uart_port *port)
  182. {
  183. /* Transmitter is always empty for us. If the circ buffer
  184. * is non-empty or there is an x_char pending, our caller
  185. * will do the right thing and ignore what we return here.
  186. */
  187. return TIOCSER_TEMT;
  188. }
  189. /* port->lock held by caller. */
  190. static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
  191. {
  192. return;
  193. }
  194. /* port->lock is held by caller and interrupts are disabled. */
  195. static unsigned int sunhv_get_mctrl(struct uart_port *port)
  196. {
  197. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  198. }
  199. /* port->lock held by caller. */
  200. static void sunhv_stop_tx(struct uart_port *port)
  201. {
  202. return;
  203. }
  204. /* port->lock held by caller. */
  205. static void sunhv_start_tx(struct uart_port *port)
  206. {
  207. transmit_chars(port);
  208. }
  209. /* port->lock is not held. */
  210. static void sunhv_send_xchar(struct uart_port *port, char ch)
  211. {
  212. unsigned long flags;
  213. int limit = 10000;
  214. spin_lock_irqsave(&port->lock, flags);
  215. while (limit-- > 0) {
  216. long status = sun4v_con_putchar(ch);
  217. if (status == HV_EOK)
  218. break;
  219. udelay(1);
  220. }
  221. spin_unlock_irqrestore(&port->lock, flags);
  222. }
  223. /* port->lock held by caller. */
  224. static void sunhv_stop_rx(struct uart_port *port)
  225. {
  226. }
  227. /* port->lock is not held. */
  228. static void sunhv_break_ctl(struct uart_port *port, int break_state)
  229. {
  230. if (break_state) {
  231. unsigned long flags;
  232. int limit = 10000;
  233. spin_lock_irqsave(&port->lock, flags);
  234. while (limit-- > 0) {
  235. long status = sun4v_con_putchar(CON_BREAK);
  236. if (status == HV_EOK)
  237. break;
  238. udelay(1);
  239. }
  240. spin_unlock_irqrestore(&port->lock, flags);
  241. }
  242. }
  243. /* port->lock is not held. */
  244. static int sunhv_startup(struct uart_port *port)
  245. {
  246. return 0;
  247. }
  248. /* port->lock is not held. */
  249. static void sunhv_shutdown(struct uart_port *port)
  250. {
  251. }
  252. /* port->lock is not held. */
  253. static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
  254. struct ktermios *old)
  255. {
  256. unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  257. unsigned int quot = uart_get_divisor(port, baud);
  258. unsigned int iflag, cflag;
  259. unsigned long flags;
  260. spin_lock_irqsave(&port->lock, flags);
  261. iflag = termios->c_iflag;
  262. cflag = termios->c_cflag;
  263. port->ignore_status_mask = 0;
  264. if (iflag & IGNBRK)
  265. port->ignore_status_mask |= IGNORE_BREAK;
  266. if ((cflag & CREAD) == 0)
  267. port->ignore_status_mask |= IGNORE_ALL;
  268. /* XXX */
  269. uart_update_timeout(port, cflag,
  270. (port->uartclk / (16 * quot)));
  271. spin_unlock_irqrestore(&port->lock, flags);
  272. }
  273. static const char *sunhv_type(struct uart_port *port)
  274. {
  275. return "SUN4V HCONS";
  276. }
  277. static void sunhv_release_port(struct uart_port *port)
  278. {
  279. }
  280. static int sunhv_request_port(struct uart_port *port)
  281. {
  282. return 0;
  283. }
  284. static void sunhv_config_port(struct uart_port *port, int flags)
  285. {
  286. }
  287. static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
  288. {
  289. return -EINVAL;
  290. }
  291. static struct uart_ops sunhv_pops = {
  292. .tx_empty = sunhv_tx_empty,
  293. .set_mctrl = sunhv_set_mctrl,
  294. .get_mctrl = sunhv_get_mctrl,
  295. .stop_tx = sunhv_stop_tx,
  296. .start_tx = sunhv_start_tx,
  297. .send_xchar = sunhv_send_xchar,
  298. .stop_rx = sunhv_stop_rx,
  299. .break_ctl = sunhv_break_ctl,
  300. .startup = sunhv_startup,
  301. .shutdown = sunhv_shutdown,
  302. .set_termios = sunhv_set_termios,
  303. .type = sunhv_type,
  304. .release_port = sunhv_release_port,
  305. .request_port = sunhv_request_port,
  306. .config_port = sunhv_config_port,
  307. .verify_port = sunhv_verify_port,
  308. };
  309. static struct uart_driver sunhv_reg = {
  310. .owner = THIS_MODULE,
  311. .driver_name = "sunhv",
  312. .dev_name = "ttyS",
  313. .major = TTY_MAJOR,
  314. };
  315. static struct uart_port *sunhv_port;
  316. /* Copy 's' into the con_write_page, decoding "\n" into
  317. * "\r\n" along the way. We have to return two lengths
  318. * because the caller needs to know how much to advance
  319. * 's' and also how many bytes to output via con_write_page.
  320. */
  321. static int fill_con_write_page(const char *s, unsigned int n,
  322. unsigned long *page_bytes)
  323. {
  324. const char *orig_s = s;
  325. char *p = con_write_page;
  326. int left = PAGE_SIZE;
  327. while (n--) {
  328. if (*s == '\n') {
  329. if (left < 2)
  330. break;
  331. *p++ = '\r';
  332. left--;
  333. } else if (left < 1)
  334. break;
  335. *p++ = *s++;
  336. left--;
  337. }
  338. *page_bytes = p - con_write_page;
  339. return s - orig_s;
  340. }
  341. static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
  342. {
  343. struct uart_port *port = sunhv_port;
  344. unsigned long flags;
  345. int locked = 1;
  346. if (port->sysrq || oops_in_progress)
  347. locked = spin_trylock_irqsave(&port->lock, flags);
  348. else
  349. spin_lock_irqsave(&port->lock, flags);
  350. while (n > 0) {
  351. unsigned long ra = __pa(con_write_page);
  352. unsigned long page_bytes;
  353. unsigned int cpy = fill_con_write_page(s, n,
  354. &page_bytes);
  355. n -= cpy;
  356. s += cpy;
  357. while (page_bytes > 0) {
  358. unsigned long written;
  359. int limit = 1000000;
  360. while (limit--) {
  361. unsigned long stat;
  362. stat = sun4v_con_write(ra, page_bytes,
  363. &written);
  364. if (stat == HV_EOK)
  365. break;
  366. udelay(1);
  367. }
  368. if (limit < 0)
  369. break;
  370. page_bytes -= written;
  371. ra += written;
  372. }
  373. }
  374. if (locked)
  375. spin_unlock_irqrestore(&port->lock, flags);
  376. }
  377. static inline void sunhv_console_putchar(struct uart_port *port, char c)
  378. {
  379. int limit = 1000000;
  380. while (limit-- > 0) {
  381. long status = sun4v_con_putchar(c);
  382. if (status == HV_EOK)
  383. break;
  384. udelay(1);
  385. }
  386. }
  387. static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
  388. {
  389. struct uart_port *port = sunhv_port;
  390. unsigned long flags;
  391. int i, locked = 1;
  392. if (port->sysrq || oops_in_progress)
  393. locked = spin_trylock_irqsave(&port->lock, flags);
  394. else
  395. spin_lock_irqsave(&port->lock, flags);
  396. if (port->sysrq) {
  397. locked = 0;
  398. } else if (oops_in_progress) {
  399. locked = spin_trylock(&port->lock);
  400. } else
  401. spin_lock(&port->lock);
  402. for (i = 0; i < n; i++) {
  403. if (*s == '\n')
  404. sunhv_console_putchar(port, '\r');
  405. sunhv_console_putchar(port, *s++);
  406. }
  407. if (locked)
  408. spin_unlock_irqrestore(&port->lock, flags);
  409. }
  410. static struct console sunhv_console = {
  411. .name = "ttyHV",
  412. .write = sunhv_console_write_bychar,
  413. .device = uart_console_device,
  414. .flags = CON_PRINTBUFFER,
  415. .index = -1,
  416. .data = &sunhv_reg,
  417. };
  418. static int hv_probe(struct platform_device *op)
  419. {
  420. struct uart_port *port;
  421. unsigned long minor;
  422. int err;
  423. if (op->archdata.irqs[0] == 0xffffffff)
  424. return -ENODEV;
  425. port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
  426. if (unlikely(!port))
  427. return -ENOMEM;
  428. minor = 1;
  429. if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
  430. minor >= 1) {
  431. err = -ENOMEM;
  432. con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  433. if (!con_write_page)
  434. goto out_free_port;
  435. con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  436. if (!con_read_page)
  437. goto out_free_con_write_page;
  438. sunhv_console.write = sunhv_console_write_paged;
  439. sunhv_ops = &bywrite_ops;
  440. }
  441. sunhv_port = port;
  442. port->line = 0;
  443. port->ops = &sunhv_pops;
  444. port->type = PORT_SUNHV;
  445. port->uartclk = ( 29491200 / 16 ); /* arbitrary */
  446. port->membase = (unsigned char __iomem *) __pa(port);
  447. port->irq = op->archdata.irqs[0];
  448. port->dev = &op->dev;
  449. err = sunserial_register_minors(&sunhv_reg, 1);
  450. if (err)
  451. goto out_free_con_read_page;
  452. sunserial_console_match(&sunhv_console, op->dev.of_node,
  453. &sunhv_reg, port->line, false);
  454. err = uart_add_one_port(&sunhv_reg, port);
  455. if (err)
  456. goto out_unregister_driver;
  457. err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
  458. if (err)
  459. goto out_remove_port;
  460. platform_set_drvdata(op, port);
  461. return 0;
  462. out_remove_port:
  463. uart_remove_one_port(&sunhv_reg, port);
  464. out_unregister_driver:
  465. sunserial_unregister_minors(&sunhv_reg, 1);
  466. out_free_con_read_page:
  467. kfree(con_read_page);
  468. out_free_con_write_page:
  469. kfree(con_write_page);
  470. out_free_port:
  471. kfree(port);
  472. sunhv_port = NULL;
  473. return err;
  474. }
  475. static int hv_remove(struct platform_device *dev)
  476. {
  477. struct uart_port *port = platform_get_drvdata(dev);
  478. free_irq(port->irq, port);
  479. uart_remove_one_port(&sunhv_reg, port);
  480. sunserial_unregister_minors(&sunhv_reg, 1);
  481. kfree(port);
  482. sunhv_port = NULL;
  483. return 0;
  484. }
  485. static const struct of_device_id hv_match[] = {
  486. {
  487. .name = "console",
  488. .compatible = "qcn",
  489. },
  490. {
  491. .name = "console",
  492. .compatible = "SUNW,sun4v-console",
  493. },
  494. {},
  495. };
  496. MODULE_DEVICE_TABLE(of, hv_match);
  497. static struct platform_driver hv_driver = {
  498. .driver = {
  499. .name = "hv",
  500. .owner = THIS_MODULE,
  501. .of_match_table = hv_match,
  502. },
  503. .probe = hv_probe,
  504. .remove = hv_remove,
  505. };
  506. static int __init sunhv_init(void)
  507. {
  508. if (tlb_type != hypervisor)
  509. return -ENODEV;
  510. return platform_driver_register(&hv_driver);
  511. }
  512. static void __exit sunhv_exit(void)
  513. {
  514. platform_driver_unregister(&hv_driver);
  515. }
  516. module_init(sunhv_init);
  517. module_exit(sunhv_exit);
  518. MODULE_AUTHOR("David S. Miller");
  519. MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
  520. MODULE_VERSION("2.0");
  521. MODULE_LICENSE("GPL");