sunhv.c 13 KB

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