sunhv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* sunhv.c: Serial driver for SUN4V hypervisor console.
  3. *
  4. * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
  5. */
  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.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/hypervisor.h>
  22. #include <asm/spitfire.h>
  23. #include <asm/irq.h>
  24. #include <asm/setup.h>
  25. #include <linux/serial_core.h>
  26. #include <linux/sunserialcore.h>
  27. #define CON_BREAK ((long)-1)
  28. #define CON_HUP ((long)-2)
  29. #define IGNORE_BREAK 0x1
  30. #define IGNORE_ALL 0x2
  31. static char *con_write_page;
  32. static char *con_read_page;
  33. static int hung_up = 0;
  34. static void transmit_chars_putchar(struct uart_port *port,
  35. struct tty_port *tport)
  36. {
  37. unsigned char ch;
  38. while (kfifo_peek(&tport->xmit_fifo, &ch)) {
  39. long status = sun4v_con_putchar(ch);
  40. if (status != HV_EOK)
  41. break;
  42. uart_xmit_advance(port, 1);
  43. }
  44. }
  45. static void transmit_chars_write(struct uart_port *port, struct tty_port *tport)
  46. {
  47. while (!kfifo_is_empty(&tport->xmit_fifo)) {
  48. unsigned long len, ra, status, sent;
  49. unsigned char *tail;
  50. len = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail,
  51. UART_XMIT_SIZE);
  52. ra = __pa(tail);
  53. status = sun4v_con_write(ra, len, &sent);
  54. if (status != HV_EOK)
  55. break;
  56. uart_xmit_advance(port, sent);
  57. }
  58. }
  59. static int receive_chars_getchar(struct uart_port *port)
  60. {
  61. int saw_console_brk = 0;
  62. int limit = 10000;
  63. while (limit-- > 0) {
  64. long status;
  65. long c = sun4v_con_getchar(&status);
  66. if (status == HV_EWOULDBLOCK)
  67. break;
  68. if (c == CON_BREAK) {
  69. if (uart_handle_break(port))
  70. continue;
  71. saw_console_brk = 1;
  72. c = 0;
  73. }
  74. if (c == CON_HUP) {
  75. hung_up = 1;
  76. uart_handle_dcd_change(port, false);
  77. } else if (hung_up) {
  78. hung_up = 0;
  79. uart_handle_dcd_change(port, true);
  80. }
  81. if (port->state == NULL) {
  82. uart_handle_sysrq_char(port, c);
  83. continue;
  84. }
  85. port->icount.rx++;
  86. if (uart_handle_sysrq_char(port, c))
  87. continue;
  88. tty_insert_flip_char(&port->state->port, c, TTY_NORMAL);
  89. }
  90. return saw_console_brk;
  91. }
  92. static int receive_chars_read(struct uart_port *port)
  93. {
  94. static int saw_console_brk;
  95. int limit = 10000;
  96. while (limit-- > 0) {
  97. unsigned long ra = __pa(con_read_page);
  98. unsigned long bytes_read, i;
  99. long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
  100. if (stat != HV_EOK) {
  101. bytes_read = 0;
  102. if (stat == CON_BREAK) {
  103. if (saw_console_brk)
  104. sun_do_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, false);
  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, true);
  122. }
  123. if (port->sysrq != 0 && *con_read_page) {
  124. for (i = 0; i < bytes_read; i++)
  125. uart_handle_sysrq_char(port, con_read_page[i]);
  126. saw_console_brk = 0;
  127. }
  128. if (port->state == NULL)
  129. continue;
  130. port->icount.rx += bytes_read;
  131. tty_insert_flip_string(&port->state->port, con_read_page,
  132. bytes_read);
  133. }
  134. return saw_console_brk;
  135. }
  136. struct sunhv_ops {
  137. void (*transmit_chars)(struct uart_port *port, struct tty_port *tport);
  138. int (*receive_chars)(struct uart_port *port);
  139. };
  140. static const struct sunhv_ops bychar_ops = {
  141. .transmit_chars = transmit_chars_putchar,
  142. .receive_chars = receive_chars_getchar,
  143. };
  144. static const struct sunhv_ops bywrite_ops = {
  145. .transmit_chars = transmit_chars_write,
  146. .receive_chars = receive_chars_read,
  147. };
  148. static const struct sunhv_ops *sunhv_ops = &bychar_ops;
  149. static struct tty_port *receive_chars(struct uart_port *port)
  150. {
  151. struct tty_port *tport = NULL;
  152. if (port->state != NULL) /* Unopened serial console */
  153. tport = &port->state->port;
  154. if (sunhv_ops->receive_chars(port))
  155. sun_do_break();
  156. return tport;
  157. }
  158. static void transmit_chars(struct uart_port *port)
  159. {
  160. struct tty_port *tport;
  161. if (!port->state)
  162. return;
  163. tport = &port->state->port;
  164. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port))
  165. return;
  166. sunhv_ops->transmit_chars(port, tport);
  167. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  168. uart_write_wakeup(port);
  169. }
  170. static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
  171. {
  172. struct uart_port *port = dev_id;
  173. struct tty_port *tport;
  174. unsigned long flags;
  175. uart_port_lock_irqsave(port, &flags);
  176. tport = receive_chars(port);
  177. transmit_chars(port);
  178. uart_port_unlock_irqrestore(port, flags);
  179. if (tport)
  180. tty_flip_buffer_push(tport);
  181. return IRQ_HANDLED;
  182. }
  183. /* port->lock is not held. */
  184. static unsigned int sunhv_tx_empty(struct uart_port *port)
  185. {
  186. /* Transmitter is always empty for us. If the circ buffer
  187. * is non-empty or there is an x_char pending, our caller
  188. * will do the right thing and ignore what we return here.
  189. */
  190. return TIOCSER_TEMT;
  191. }
  192. /* port->lock held by caller. */
  193. static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
  194. {
  195. return;
  196. }
  197. /* port->lock is held by caller and interrupts are disabled. */
  198. static unsigned int sunhv_get_mctrl(struct uart_port *port)
  199. {
  200. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  201. }
  202. /* port->lock held by caller. */
  203. static void sunhv_stop_tx(struct uart_port *port)
  204. {
  205. return;
  206. }
  207. /* port->lock held by caller. */
  208. static void sunhv_start_tx(struct uart_port *port)
  209. {
  210. transmit_chars(port);
  211. }
  212. /* port->lock is not held. */
  213. static void sunhv_send_xchar(struct uart_port *port, char ch)
  214. {
  215. unsigned long flags;
  216. int limit = 10000;
  217. if (ch == __DISABLED_CHAR)
  218. return;
  219. uart_port_lock_irqsave(port, &flags);
  220. while (limit-- > 0) {
  221. long status = sun4v_con_putchar(ch);
  222. if (status == HV_EOK)
  223. break;
  224. udelay(1);
  225. }
  226. uart_port_unlock_irqrestore(port, flags);
  227. }
  228. /* port->lock held by caller. */
  229. static void sunhv_stop_rx(struct uart_port *port)
  230. {
  231. }
  232. /* port->lock is not held. */
  233. static void sunhv_break_ctl(struct uart_port *port, int break_state)
  234. {
  235. if (break_state) {
  236. unsigned long flags;
  237. int limit = 10000;
  238. uart_port_lock_irqsave(port, &flags);
  239. while (limit-- > 0) {
  240. long status = sun4v_con_putchar(CON_BREAK);
  241. if (status == HV_EOK)
  242. break;
  243. udelay(1);
  244. }
  245. uart_port_unlock_irqrestore(port, flags);
  246. }
  247. }
  248. /* port->lock is not held. */
  249. static int sunhv_startup(struct uart_port *port)
  250. {
  251. return 0;
  252. }
  253. /* port->lock is not held. */
  254. static void sunhv_shutdown(struct uart_port *port)
  255. {
  256. }
  257. /* port->lock is not held. */
  258. static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
  259. const struct ktermios *old)
  260. {
  261. unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  262. unsigned int quot = uart_get_divisor(port, baud);
  263. unsigned int iflag, cflag;
  264. unsigned long flags;
  265. uart_port_lock_irqsave(port, &flags);
  266. iflag = termios->c_iflag;
  267. cflag = termios->c_cflag;
  268. port->ignore_status_mask = 0;
  269. if (iflag & IGNBRK)
  270. port->ignore_status_mask |= IGNORE_BREAK;
  271. if ((cflag & CREAD) == 0)
  272. port->ignore_status_mask |= IGNORE_ALL;
  273. /* XXX */
  274. uart_update_timeout(port, cflag,
  275. (port->uartclk / (16 * quot)));
  276. uart_port_unlock_irqrestore(port, flags);
  277. }
  278. static const char *sunhv_type(struct uart_port *port)
  279. {
  280. return "SUN4V HCONS";
  281. }
  282. static void sunhv_release_port(struct uart_port *port)
  283. {
  284. }
  285. static int sunhv_request_port(struct uart_port *port)
  286. {
  287. return 0;
  288. }
  289. static void sunhv_config_port(struct uart_port *port, int flags)
  290. {
  291. }
  292. static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
  293. {
  294. return -EINVAL;
  295. }
  296. static const struct uart_ops sunhv_pops = {
  297. .tx_empty = sunhv_tx_empty,
  298. .set_mctrl = sunhv_set_mctrl,
  299. .get_mctrl = sunhv_get_mctrl,
  300. .stop_tx = sunhv_stop_tx,
  301. .start_tx = sunhv_start_tx,
  302. .send_xchar = sunhv_send_xchar,
  303. .stop_rx = sunhv_stop_rx,
  304. .break_ctl = sunhv_break_ctl,
  305. .startup = sunhv_startup,
  306. .shutdown = sunhv_shutdown,
  307. .set_termios = sunhv_set_termios,
  308. .type = sunhv_type,
  309. .release_port = sunhv_release_port,
  310. .request_port = sunhv_request_port,
  311. .config_port = sunhv_config_port,
  312. .verify_port = sunhv_verify_port,
  313. };
  314. static struct uart_driver sunhv_reg = {
  315. .owner = THIS_MODULE,
  316. .driver_name = "sunhv",
  317. .dev_name = "ttyHV",
  318. .major = TTY_MAJOR,
  319. };
  320. static struct uart_port *sunhv_port;
  321. void sunhv_migrate_hvcons_irq(int cpu)
  322. {
  323. /* Migrate hvcons irq to param cpu */
  324. irq_force_affinity(sunhv_port->irq, cpumask_of(cpu));
  325. }
  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. if (port->sysrq || oops_in_progress)
  357. locked = uart_port_trylock_irqsave(port, &flags);
  358. else
  359. uart_port_lock_irqsave(port, &flags);
  360. while (n > 0) {
  361. unsigned long ra = __pa(con_write_page);
  362. unsigned long page_bytes;
  363. unsigned int cpy = fill_con_write_page(s, n,
  364. &page_bytes);
  365. n -= cpy;
  366. s += cpy;
  367. while (page_bytes > 0) {
  368. unsigned long written;
  369. int limit = 1000000;
  370. while (limit--) {
  371. unsigned long stat;
  372. stat = sun4v_con_write(ra, page_bytes,
  373. &written);
  374. if (stat == HV_EOK)
  375. break;
  376. udelay(1);
  377. }
  378. if (limit < 0)
  379. break;
  380. page_bytes -= written;
  381. ra += written;
  382. }
  383. }
  384. if (locked)
  385. uart_port_unlock_irqrestore(port, flags);
  386. }
  387. static inline void sunhv_console_putchar(struct uart_port *port, char c)
  388. {
  389. int limit = 1000000;
  390. while (limit-- > 0) {
  391. long status = sun4v_con_putchar(c);
  392. if (status == HV_EOK)
  393. break;
  394. udelay(1);
  395. }
  396. }
  397. static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
  398. {
  399. struct uart_port *port = sunhv_port;
  400. unsigned long flags;
  401. int i, locked = 1;
  402. if (port->sysrq || oops_in_progress)
  403. locked = uart_port_trylock_irqsave(port, &flags);
  404. else
  405. uart_port_lock_irqsave(port, &flags);
  406. for (i = 0; i < n; i++) {
  407. if (*s == '\n')
  408. sunhv_console_putchar(port, '\r');
  409. sunhv_console_putchar(port, *s++);
  410. }
  411. if (locked)
  412. uart_port_unlock_irqrestore(port, flags);
  413. }
  414. static struct console sunhv_console = {
  415. .name = "ttyHV",
  416. .write = sunhv_console_write_bychar,
  417. .device = uart_console_device,
  418. .flags = CON_PRINTBUFFER,
  419. .index = -1,
  420. .data = &sunhv_reg,
  421. };
  422. static int hv_probe(struct platform_device *op)
  423. {
  424. struct uart_port *port;
  425. unsigned long minor;
  426. int err;
  427. if (op->archdata.irqs[0] == 0xffffffff)
  428. return -ENODEV;
  429. port = kzalloc_obj(struct uart_port);
  430. if (unlikely(!port))
  431. return -ENOMEM;
  432. minor = 1;
  433. if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
  434. minor >= 1) {
  435. err = -ENOMEM;
  436. con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  437. if (!con_write_page)
  438. goto out_free_port;
  439. con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  440. if (!con_read_page)
  441. goto out_free_con_write_page;
  442. sunhv_console.write = sunhv_console_write_paged;
  443. sunhv_ops = &bywrite_ops;
  444. }
  445. sunhv_port = port;
  446. port->has_sysrq = 1;
  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 void 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. }
  491. static const struct of_device_id hv_match[] = {
  492. {
  493. .name = "console",
  494. .compatible = "qcn",
  495. },
  496. {
  497. .name = "console",
  498. .compatible = "SUNW,sun4v-console",
  499. },
  500. {},
  501. };
  502. static struct platform_driver hv_driver = {
  503. .driver = {
  504. .name = "hv",
  505. .of_match_table = hv_match,
  506. },
  507. .probe = hv_probe,
  508. .remove = hv_remove,
  509. };
  510. static int __init sunhv_init(void)
  511. {
  512. if (tlb_type != hypervisor)
  513. return -ENODEV;
  514. return platform_driver_register(&hv_driver);
  515. }
  516. device_initcall(sunhv_init);
  517. #if 0 /* ...def MODULE ; never supported as such */
  518. MODULE_AUTHOR("David S. Miller");
  519. MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
  520. MODULE_VERSION("2.0");
  521. MODULE_LICENSE("GPL");
  522. #endif