sunhv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 ([email protected])
  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_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. #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, struct circ_buf *xmit)
  35. {
  36. while (!uart_circ_empty(xmit)) {
  37. long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
  38. if (status != HV_EOK)
  39. break;
  40. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  41. port->icount.tx++;
  42. }
  43. }
  44. static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
  45. {
  46. while (!uart_circ_empty(xmit)) {
  47. unsigned long ra = __pa(xmit->buf + xmit->tail);
  48. unsigned long len, status, sent;
  49. len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
  50. UART_XMIT_SIZE);
  51. status = sun4v_con_write(ra, len, &sent);
  52. if (status != HV_EOK)
  53. break;
  54. xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
  55. port->icount.tx += sent;
  56. }
  57. }
  58. static int receive_chars_getchar(struct uart_port *port)
  59. {
  60. int saw_console_brk = 0;
  61. int limit = 10000;
  62. while (limit-- > 0) {
  63. long status;
  64. long c = sun4v_con_getchar(&status);
  65. if (status == HV_EWOULDBLOCK)
  66. break;
  67. if (c == CON_BREAK) {
  68. if (uart_handle_break(port))
  69. continue;
  70. saw_console_brk = 1;
  71. c = 0;
  72. }
  73. if (c == CON_HUP) {
  74. hung_up = 1;
  75. uart_handle_dcd_change(port, 0);
  76. } else if (hung_up) {
  77. hung_up = 0;
  78. uart_handle_dcd_change(port, 1);
  79. }
  80. if (port->state == NULL) {
  81. uart_handle_sysrq_char(port, c);
  82. continue;
  83. }
  84. port->icount.rx++;
  85. if (uart_handle_sysrq_char(port, c))
  86. continue;
  87. tty_insert_flip_char(&port->state->port, c, TTY_NORMAL);
  88. }
  89. return saw_console_brk;
  90. }
  91. static int receive_chars_read(struct uart_port *port)
  92. {
  93. static int saw_console_brk;
  94. int limit = 10000;
  95. while (limit-- > 0) {
  96. unsigned long ra = __pa(con_read_page);
  97. unsigned long bytes_read, i;
  98. long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
  99. if (stat != HV_EOK) {
  100. bytes_read = 0;
  101. if (stat == CON_BREAK) {
  102. if (saw_console_brk)
  103. sun_do_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. if (port->sysrq != 0 && *con_read_page) {
  123. for (i = 0; i < bytes_read; i++)
  124. uart_handle_sysrq_char(port, con_read_page[i]);
  125. saw_console_brk = 0;
  126. }
  127. if (port->state == NULL)
  128. continue;
  129. port->icount.rx += bytes_read;
  130. tty_insert_flip_string(&port->state->port, con_read_page,
  131. bytes_read);
  132. }
  133. return saw_console_brk;
  134. }
  135. struct sunhv_ops {
  136. void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
  137. int (*receive_chars)(struct uart_port *port);
  138. };
  139. static const struct sunhv_ops bychar_ops = {
  140. .transmit_chars = transmit_chars_putchar,
  141. .receive_chars = receive_chars_getchar,
  142. };
  143. static const struct sunhv_ops bywrite_ops = {
  144. .transmit_chars = transmit_chars_write,
  145. .receive_chars = receive_chars_read,
  146. };
  147. static const struct sunhv_ops *sunhv_ops = &bychar_ops;
  148. static struct tty_port *receive_chars(struct uart_port *port)
  149. {
  150. struct tty_port *tport = NULL;
  151. if (port->state != NULL) /* Unopened serial console */
  152. tport = &port->state->port;
  153. if (sunhv_ops->receive_chars(port))
  154. sun_do_break();
  155. return tport;
  156. }
  157. static void transmit_chars(struct uart_port *port)
  158. {
  159. struct circ_buf *xmit;
  160. if (!port->state)
  161. return;
  162. xmit = &port->state->xmit;
  163. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  164. return;
  165. sunhv_ops->transmit_chars(port, xmit);
  166. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  167. uart_write_wakeup(port);
  168. }
  169. static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
  170. {
  171. struct uart_port *port = dev_id;
  172. struct tty_port *tport;
  173. unsigned long flags;
  174. spin_lock_irqsave(&port->lock, flags);
  175. tport = receive_chars(port);
  176. transmit_chars(port);
  177. spin_unlock_irqrestore(&port->lock, flags);
  178. if (tport)
  179. tty_flip_buffer_push(tport);
  180. return IRQ_HANDLED;
  181. }
  182. /* port->lock is not held. */
  183. static unsigned int sunhv_tx_empty(struct uart_port *port)
  184. {
  185. /* Transmitter is always empty for us. If the circ buffer
  186. * is non-empty or there is an x_char pending, our caller
  187. * will do the right thing and ignore what we return here.
  188. */
  189. return TIOCSER_TEMT;
  190. }
  191. /* port->lock held by caller. */
  192. static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
  193. {
  194. return;
  195. }
  196. /* port->lock is held by caller and interrupts are disabled. */
  197. static unsigned int sunhv_get_mctrl(struct uart_port *port)
  198. {
  199. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  200. }
  201. /* port->lock held by caller. */
  202. static void sunhv_stop_tx(struct uart_port *port)
  203. {
  204. return;
  205. }
  206. /* port->lock held by caller. */
  207. static void sunhv_start_tx(struct uart_port *port)
  208. {
  209. transmit_chars(port);
  210. }
  211. /* port->lock is not held. */
  212. static void sunhv_send_xchar(struct uart_port *port, char ch)
  213. {
  214. unsigned long flags;
  215. int limit = 10000;
  216. if (ch == __DISABLED_CHAR)
  217. return;
  218. spin_lock_irqsave(&port->lock, flags);
  219. while (limit-- > 0) {
  220. long status = sun4v_con_putchar(ch);
  221. if (status == HV_EOK)
  222. break;
  223. udelay(1);
  224. }
  225. spin_unlock_irqrestore(&port->lock, flags);
  226. }
  227. /* port->lock held by caller. */
  228. static void sunhv_stop_rx(struct uart_port *port)
  229. {
  230. }
  231. /* port->lock is not held. */
  232. static void sunhv_break_ctl(struct uart_port *port, int break_state)
  233. {
  234. if (break_state) {
  235. unsigned long flags;
  236. int limit = 10000;
  237. spin_lock_irqsave(&port->lock, flags);
  238. while (limit-- > 0) {
  239. long status = sun4v_con_putchar(CON_BREAK);
  240. if (status == HV_EOK)
  241. break;
  242. udelay(1);
  243. }
  244. spin_unlock_irqrestore(&port->lock, flags);
  245. }
  246. }
  247. /* port->lock is not held. */
  248. static int sunhv_startup(struct uart_port *port)
  249. {
  250. return 0;
  251. }
  252. /* port->lock is not held. */
  253. static void sunhv_shutdown(struct uart_port *port)
  254. {
  255. }
  256. /* port->lock is not held. */
  257. static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
  258. const struct ktermios *old)
  259. {
  260. unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  261. unsigned int quot = uart_get_divisor(port, baud);
  262. unsigned int iflag, cflag;
  263. unsigned long flags;
  264. spin_lock_irqsave(&port->lock, flags);
  265. iflag = termios->c_iflag;
  266. cflag = termios->c_cflag;
  267. port->ignore_status_mask = 0;
  268. if (iflag & IGNBRK)
  269. port->ignore_status_mask |= IGNORE_BREAK;
  270. if ((cflag & CREAD) == 0)
  271. port->ignore_status_mask |= IGNORE_ALL;
  272. /* XXX */
  273. uart_update_timeout(port, cflag,
  274. (port->uartclk / (16 * quot)));
  275. spin_unlock_irqrestore(&port->lock, flags);
  276. }
  277. static const char *sunhv_type(struct uart_port *port)
  278. {
  279. return "SUN4V HCONS";
  280. }
  281. static void sunhv_release_port(struct uart_port *port)
  282. {
  283. }
  284. static int sunhv_request_port(struct uart_port *port)
  285. {
  286. return 0;
  287. }
  288. static void sunhv_config_port(struct uart_port *port, int flags)
  289. {
  290. }
  291. static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
  292. {
  293. return -EINVAL;
  294. }
  295. static const struct uart_ops sunhv_pops = {
  296. .tx_empty = sunhv_tx_empty,
  297. .set_mctrl = sunhv_set_mctrl,
  298. .get_mctrl = sunhv_get_mctrl,
  299. .stop_tx = sunhv_stop_tx,
  300. .start_tx = sunhv_start_tx,
  301. .send_xchar = sunhv_send_xchar,
  302. .stop_rx = sunhv_stop_rx,
  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 = "ttyHV",
  317. .major = TTY_MAJOR,
  318. };
  319. static struct uart_port *sunhv_port;
  320. void sunhv_migrate_hvcons_irq(int cpu)
  321. {
  322. /* Migrate hvcons irq to param cpu */
  323. irq_force_affinity(sunhv_port->irq, cpumask_of(cpu));
  324. }
  325. /* Copy 's' into the con_write_page, decoding "\n" into
  326. * "\r\n" along the way. We have to return two lengths
  327. * because the caller needs to know how much to advance
  328. * 's' and also how many bytes to output via con_write_page.
  329. */
  330. static int fill_con_write_page(const char *s, unsigned int n,
  331. unsigned long *page_bytes)
  332. {
  333. const char *orig_s = s;
  334. char *p = con_write_page;
  335. int left = PAGE_SIZE;
  336. while (n--) {
  337. if (*s == '\n') {
  338. if (left < 2)
  339. break;
  340. *p++ = '\r';
  341. left--;
  342. } else if (left < 1)
  343. break;
  344. *p++ = *s++;
  345. left--;
  346. }
  347. *page_bytes = p - con_write_page;
  348. return s - orig_s;
  349. }
  350. static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
  351. {
  352. struct uart_port *port = sunhv_port;
  353. unsigned long flags;
  354. int locked = 1;
  355. if (port->sysrq || oops_in_progress)
  356. locked = spin_trylock_irqsave(&port->lock, flags);
  357. else
  358. spin_lock_irqsave(&port->lock, flags);
  359. while (n > 0) {
  360. unsigned long ra = __pa(con_write_page);
  361. unsigned long page_bytes;
  362. unsigned int cpy = fill_con_write_page(s, n,
  363. &page_bytes);
  364. n -= cpy;
  365. s += cpy;
  366. while (page_bytes > 0) {
  367. unsigned long written;
  368. int limit = 1000000;
  369. while (limit--) {
  370. unsigned long stat;
  371. stat = sun4v_con_write(ra, page_bytes,
  372. &written);
  373. if (stat == HV_EOK)
  374. break;
  375. udelay(1);
  376. }
  377. if (limit < 0)
  378. break;
  379. page_bytes -= written;
  380. ra += written;
  381. }
  382. }
  383. if (locked)
  384. spin_unlock_irqrestore(&port->lock, flags);
  385. }
  386. static inline void sunhv_console_putchar(struct uart_port *port, char c)
  387. {
  388. int limit = 1000000;
  389. while (limit-- > 0) {
  390. long status = sun4v_con_putchar(c);
  391. if (status == HV_EOK)
  392. break;
  393. udelay(1);
  394. }
  395. }
  396. static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
  397. {
  398. struct uart_port *port = sunhv_port;
  399. unsigned long flags;
  400. int i, locked = 1;
  401. if (port->sysrq || oops_in_progress)
  402. locked = spin_trylock_irqsave(&port->lock, flags);
  403. else
  404. spin_lock_irqsave(&port->lock, flags);
  405. for (i = 0; i < n; i++) {
  406. if (*s == '\n')
  407. sunhv_console_putchar(port, '\r');
  408. sunhv_console_putchar(port, *s++);
  409. }
  410. if (locked)
  411. spin_unlock_irqrestore(&port->lock, flags);
  412. }
  413. static struct console sunhv_console = {
  414. .name = "ttyHV",
  415. .write = sunhv_console_write_bychar,
  416. .device = uart_console_device,
  417. .flags = CON_PRINTBUFFER,
  418. .index = -1,
  419. .data = &sunhv_reg,
  420. };
  421. static int hv_probe(struct platform_device *op)
  422. {
  423. struct uart_port *port;
  424. unsigned long minor;
  425. int err;
  426. if (op->archdata.irqs[0] == 0xffffffff)
  427. return -ENODEV;
  428. port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
  429. if (unlikely(!port))
  430. return -ENOMEM;
  431. minor = 1;
  432. if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
  433. minor >= 1) {
  434. err = -ENOMEM;
  435. con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  436. if (!con_write_page)
  437. goto out_free_port;
  438. con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  439. if (!con_read_page)
  440. goto out_free_con_write_page;
  441. sunhv_console.write = sunhv_console_write_paged;
  442. sunhv_ops = &bywrite_ops;
  443. }
  444. sunhv_port = port;
  445. port->has_sysrq = 1;
  446. port->line = 0;
  447. port->ops = &sunhv_pops;
  448. port->type = PORT_SUNHV;
  449. port->uartclk = ( 29491200 / 16 ); /* arbitrary */
  450. port->membase = (unsigned char __iomem *) __pa(port);
  451. port->irq = op->archdata.irqs[0];
  452. port->dev = &op->dev;
  453. err = sunserial_register_minors(&sunhv_reg, 1);
  454. if (err)
  455. goto out_free_con_read_page;
  456. sunserial_console_match(&sunhv_console, op->dev.of_node,
  457. &sunhv_reg, port->line, false);
  458. err = uart_add_one_port(&sunhv_reg, port);
  459. if (err)
  460. goto out_unregister_driver;
  461. err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
  462. if (err)
  463. goto out_remove_port;
  464. platform_set_drvdata(op, port);
  465. return 0;
  466. out_remove_port:
  467. uart_remove_one_port(&sunhv_reg, port);
  468. out_unregister_driver:
  469. sunserial_unregister_minors(&sunhv_reg, 1);
  470. out_free_con_read_page:
  471. kfree(con_read_page);
  472. out_free_con_write_page:
  473. kfree(con_write_page);
  474. out_free_port:
  475. kfree(port);
  476. sunhv_port = NULL;
  477. return err;
  478. }
  479. static int hv_remove(struct platform_device *dev)
  480. {
  481. struct uart_port *port = platform_get_drvdata(dev);
  482. free_irq(port->irq, port);
  483. uart_remove_one_port(&sunhv_reg, port);
  484. sunserial_unregister_minors(&sunhv_reg, 1);
  485. kfree(con_read_page);
  486. kfree(con_write_page);
  487. kfree(port);
  488. sunhv_port = NULL;
  489. return 0;
  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