ehv_bytechan.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* ePAPR hypervisor byte channel device driver
  3. *
  4. * Copyright 2009-2011 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Timur Tabi <timur@freescale.com>
  7. *
  8. * This driver support three distinct interfaces, all of which are related to
  9. * ePAPR hypervisor byte channels.
  10. *
  11. * 1) An early-console (udbg) driver. This provides early console output
  12. * through a byte channel. The byte channel handle must be specified in a
  13. * Kconfig option.
  14. *
  15. * 2) A normal console driver. Output is sent to the byte channel designated
  16. * for stdout in the device tree. The console driver is for handling kernel
  17. * printk calls.
  18. *
  19. * 3) A tty driver, which is used to handle user-space input and output. The
  20. * byte channel used for the console is designated as the default tty.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/fs.h>
  27. #include <linux/poll.h>
  28. #include <asm/epapr_hcalls.h>
  29. #include <linux/of.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/cdev.h>
  33. #include <linux/console.h>
  34. #include <linux/tty.h>
  35. #include <linux/tty_flip.h>
  36. #include <linux/circ_buf.h>
  37. #include <asm/udbg.h>
  38. /* The size of the transmit circular buffer. This must be a power of two. */
  39. #define BUF_SIZE 2048
  40. /* Per-byte channel private data */
  41. struct ehv_bc_data {
  42. struct device *dev;
  43. struct tty_port port;
  44. uint32_t handle;
  45. unsigned int rx_irq;
  46. unsigned int tx_irq;
  47. spinlock_t lock; /* lock for transmit buffer */
  48. u8 buf[BUF_SIZE]; /* transmit circular buffer */
  49. unsigned int head; /* circular buffer head */
  50. unsigned int tail; /* circular buffer tail */
  51. int tx_irq_enabled; /* true == TX interrupt is enabled */
  52. };
  53. /* Array of byte channel objects */
  54. static struct ehv_bc_data *bcs;
  55. /* Byte channel handle for stdout (and stdin), taken from device tree */
  56. static unsigned int stdout_bc;
  57. /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
  58. static unsigned int stdout_irq;
  59. /**************************** SUPPORT FUNCTIONS ****************************/
  60. /*
  61. * Enable the transmit interrupt
  62. *
  63. * Unlike a serial device, byte channels have no mechanism for disabling their
  64. * own receive or transmit interrupts. To emulate that feature, we toggle
  65. * the IRQ in the kernel.
  66. *
  67. * We cannot just blindly call enable_irq() or disable_irq(), because these
  68. * calls are reference counted. This means that we cannot call enable_irq()
  69. * if interrupts are already enabled. This can happen in two situations:
  70. *
  71. * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write()
  72. * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue()
  73. *
  74. * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
  75. */
  76. static void enable_tx_interrupt(struct ehv_bc_data *bc)
  77. {
  78. if (!bc->tx_irq_enabled) {
  79. enable_irq(bc->tx_irq);
  80. bc->tx_irq_enabled = 1;
  81. }
  82. }
  83. static void disable_tx_interrupt(struct ehv_bc_data *bc)
  84. {
  85. if (bc->tx_irq_enabled) {
  86. disable_irq_nosync(bc->tx_irq);
  87. bc->tx_irq_enabled = 0;
  88. }
  89. }
  90. /*
  91. * find the byte channel handle to use for the console
  92. *
  93. * The byte channel to be used for the console is specified via a "stdout"
  94. * property in the /chosen node.
  95. */
  96. static int find_console_handle(void)
  97. {
  98. struct device_node *np = of_stdout;
  99. const uint32_t *iprop;
  100. /* We don't care what the aliased node is actually called. We only
  101. * care if it's compatible with "epapr,hv-byte-channel", because that
  102. * indicates that it's a byte channel node.
  103. */
  104. if (!np || !of_device_is_compatible(np, "epapr,hv-byte-channel"))
  105. return 0;
  106. stdout_irq = irq_of_parse_and_map(np, 0);
  107. if (!stdout_irq) {
  108. pr_err("ehv-bc: no 'interrupts' property in %pOF node\n", np);
  109. return 0;
  110. }
  111. /*
  112. * The 'hv-handle' property contains the handle for this byte channel.
  113. */
  114. iprop = of_get_property(np, "hv-handle", NULL);
  115. if (!iprop) {
  116. pr_err("ehv-bc: no 'hv-handle' property in %pOFn node\n",
  117. np);
  118. return 0;
  119. }
  120. stdout_bc = be32_to_cpu(*iprop);
  121. return 1;
  122. }
  123. static unsigned int local_ev_byte_channel_send(unsigned int handle,
  124. unsigned int *count,
  125. const u8 *p)
  126. {
  127. u8 buffer[EV_BYTE_CHANNEL_MAX_BYTES];
  128. unsigned int c = *count;
  129. /*
  130. * ev_byte_channel_send() expects at least EV_BYTE_CHANNEL_MAX_BYTES
  131. * (16 B) in the buffer. Fake it using a local buffer if needed.
  132. */
  133. if (c < sizeof(buffer)) {
  134. memcpy_and_pad(buffer, sizeof(buffer), p, c, 0);
  135. p = buffer;
  136. }
  137. return ev_byte_channel_send(handle, count, p);
  138. }
  139. /*************************** EARLY CONSOLE DRIVER ***************************/
  140. #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
  141. /*
  142. * send a byte to a byte channel, wait if necessary
  143. *
  144. * This function sends a byte to a byte channel, and it waits and
  145. * retries if the byte channel is full. It returns if the character
  146. * has been sent, or if some error has occurred.
  147. *
  148. */
  149. static void byte_channel_spin_send(const u8 data)
  150. {
  151. int ret, count;
  152. do {
  153. count = 1;
  154. ret = local_ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
  155. &count, &data);
  156. } while (ret == EV_EAGAIN);
  157. }
  158. /*
  159. * The udbg subsystem calls this function to display a single character.
  160. * We convert CR to a CR/LF.
  161. */
  162. static void ehv_bc_udbg_putc(char c)
  163. {
  164. if (c == '\n')
  165. byte_channel_spin_send('\r');
  166. byte_channel_spin_send(c);
  167. }
  168. /*
  169. * early console initialization
  170. *
  171. * PowerPC kernels support an early printk console, also known as udbg.
  172. * This function must be called via the ppc_md.init_early function pointer.
  173. * At this point, the device tree has been unflattened, so we can obtain the
  174. * byte channel handle for stdout.
  175. *
  176. * We only support displaying of characters (putc). We do not support
  177. * keyboard input.
  178. */
  179. void __init udbg_init_ehv_bc(void)
  180. {
  181. unsigned int rx_count, tx_count;
  182. unsigned int ret;
  183. /* Verify the byte channel handle */
  184. ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
  185. &rx_count, &tx_count);
  186. if (ret)
  187. return;
  188. udbg_putc = ehv_bc_udbg_putc;
  189. register_early_udbg_console();
  190. udbg_printf("ehv-bc: early console using byte channel handle %u\n",
  191. CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
  192. }
  193. #endif
  194. /****************************** CONSOLE DRIVER ******************************/
  195. static struct tty_driver *ehv_bc_driver;
  196. /*
  197. * Byte channel console sending worker function.
  198. *
  199. * For consoles, if the output buffer is full, we should just spin until it
  200. * clears.
  201. */
  202. static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
  203. unsigned int count)
  204. {
  205. unsigned int len;
  206. int ret = 0;
  207. while (count) {
  208. len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
  209. do {
  210. ret = local_ev_byte_channel_send(handle, &len, s);
  211. } while (ret == EV_EAGAIN);
  212. count -= len;
  213. s += len;
  214. }
  215. return ret;
  216. }
  217. /*
  218. * write a string to the console
  219. *
  220. * This function gets called to write a string from the kernel, typically from
  221. * a printk(). This function spins until all data is written.
  222. *
  223. * We copy the data to a temporary buffer because we need to insert a \r in
  224. * front of every \n. It's more efficient to copy the data to the buffer than
  225. * it is to make multiple hcalls for each character or each newline.
  226. */
  227. static void ehv_bc_console_write(struct console *co, const char *s,
  228. unsigned int count)
  229. {
  230. char s2[EV_BYTE_CHANNEL_MAX_BYTES];
  231. unsigned int i, j = 0;
  232. char c;
  233. for (i = 0; i < count; i++) {
  234. c = *s++;
  235. if (c == '\n')
  236. s2[j++] = '\r';
  237. s2[j++] = c;
  238. if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
  239. if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
  240. return;
  241. j = 0;
  242. }
  243. }
  244. if (j)
  245. ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
  246. }
  247. /*
  248. * When /dev/console is opened, the kernel iterates the console list looking
  249. * for one with ->device and then calls that method. On success, it expects
  250. * the passed-in int* to contain the minor number to use.
  251. */
  252. static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
  253. {
  254. *index = co->index;
  255. return ehv_bc_driver;
  256. }
  257. static struct console ehv_bc_console = {
  258. .name = "ttyEHV",
  259. .write = ehv_bc_console_write,
  260. .device = ehv_bc_console_device,
  261. .flags = CON_PRINTBUFFER | CON_ENABLED,
  262. };
  263. /*
  264. * Console initialization
  265. *
  266. * This is the first function that is called after the device tree is
  267. * available, so here is where we determine the byte channel handle and IRQ for
  268. * stdout/stdin, even though that information is used by the tty and character
  269. * drivers.
  270. */
  271. static int __init ehv_bc_console_init(void)
  272. {
  273. if (!find_console_handle()) {
  274. pr_debug("ehv-bc: stdout is not a byte channel\n");
  275. return -ENODEV;
  276. }
  277. #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
  278. /* Print a friendly warning if the user chose the wrong byte channel
  279. * handle for udbg.
  280. */
  281. if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
  282. pr_warn("ehv-bc: udbg handle %u is not the stdout handle\n",
  283. CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
  284. #endif
  285. /* add_preferred_console() must be called before register_console(),
  286. otherwise it won't work. However, we don't want to enumerate all the
  287. byte channels here, either, since we only care about one. */
  288. add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
  289. register_console(&ehv_bc_console);
  290. pr_info("ehv-bc: registered console driver for byte channel %u\n",
  291. stdout_bc);
  292. return 0;
  293. }
  294. console_initcall(ehv_bc_console_init);
  295. /******************************** TTY DRIVER ********************************/
  296. /*
  297. * byte channel receive interrupt handler
  298. *
  299. * This ISR is called whenever data is available on a byte channel.
  300. */
  301. static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
  302. {
  303. struct ehv_bc_data *bc = data;
  304. unsigned int rx_count, tx_count, len;
  305. int count;
  306. char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
  307. int ret;
  308. /* Find out how much data needs to be read, and then ask the TTY layer
  309. * if it can handle that much. We want to ensure that every byte we
  310. * read from the byte channel will be accepted by the TTY layer.
  311. */
  312. ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
  313. count = tty_buffer_request_room(&bc->port, rx_count);
  314. /* 'count' is the maximum amount of data the TTY layer can accept at
  315. * this time. However, during testing, I was never able to get 'count'
  316. * to be less than 'rx_count'. I'm not sure whether I'm calling it
  317. * correctly.
  318. */
  319. while (count > 0) {
  320. len = min_t(unsigned int, count, sizeof(buffer));
  321. /* Read some data from the byte channel. This function will
  322. * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
  323. */
  324. ev_byte_channel_receive(bc->handle, &len, buffer);
  325. /* 'len' is now the amount of data that's been received. 'len'
  326. * can't be zero, and most likely it's equal to one.
  327. */
  328. /* Pass the received data to the tty layer. */
  329. ret = tty_insert_flip_string(&bc->port, buffer, len);
  330. /* 'ret' is the number of bytes that the TTY layer accepted.
  331. * If it's not equal to 'len', then it means the buffer is
  332. * full, which should never happen. If it does happen, we can
  333. * exit gracefully, but we drop the last 'len - ret' characters
  334. * that we read from the byte channel.
  335. */
  336. if (ret != len)
  337. break;
  338. count -= len;
  339. }
  340. /* Tell the tty layer that we're done. */
  341. tty_flip_buffer_push(&bc->port);
  342. return IRQ_HANDLED;
  343. }
  344. /*
  345. * dequeue the transmit buffer to the hypervisor
  346. *
  347. * This function, which can be called in interrupt context, dequeues as much
  348. * data as possible from the transmit buffer to the byte channel.
  349. */
  350. static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
  351. {
  352. unsigned int count;
  353. unsigned int len, ret;
  354. unsigned long flags;
  355. do {
  356. spin_lock_irqsave(&bc->lock, flags);
  357. len = min_t(unsigned int,
  358. CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
  359. EV_BYTE_CHANNEL_MAX_BYTES);
  360. ret = local_ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
  361. /* 'len' is valid only if the return code is 0 or EV_EAGAIN */
  362. if (!ret || (ret == EV_EAGAIN))
  363. bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
  364. count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
  365. spin_unlock_irqrestore(&bc->lock, flags);
  366. } while (count && !ret);
  367. spin_lock_irqsave(&bc->lock, flags);
  368. if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
  369. /*
  370. * If we haven't emptied the buffer, then enable the TX IRQ.
  371. * We'll get an interrupt when there's more room in the
  372. * hypervisor's output buffer.
  373. */
  374. enable_tx_interrupt(bc);
  375. else
  376. disable_tx_interrupt(bc);
  377. spin_unlock_irqrestore(&bc->lock, flags);
  378. }
  379. /*
  380. * byte channel transmit interrupt handler
  381. *
  382. * This ISR is called whenever space becomes available for transmitting
  383. * characters on a byte channel.
  384. */
  385. static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
  386. {
  387. struct ehv_bc_data *bc = data;
  388. ehv_bc_tx_dequeue(bc);
  389. tty_port_tty_wakeup(&bc->port);
  390. return IRQ_HANDLED;
  391. }
  392. /*
  393. * This function is called when the tty layer has data for us send. We store
  394. * the data first in a circular buffer, and then dequeue as much of that data
  395. * as possible.
  396. *
  397. * We don't need to worry about whether there is enough room in the buffer for
  398. * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty
  399. * layer how much data it can safely send to us. We guarantee that
  400. * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
  401. * too much data.
  402. */
  403. static ssize_t ehv_bc_tty_write(struct tty_struct *ttys, const u8 *s,
  404. size_t count)
  405. {
  406. struct ehv_bc_data *bc = ttys->driver_data;
  407. unsigned long flags;
  408. size_t len, written = 0;
  409. while (1) {
  410. spin_lock_irqsave(&bc->lock, flags);
  411. len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
  412. if (count < len)
  413. len = count;
  414. if (len) {
  415. memcpy(bc->buf + bc->head, s, len);
  416. bc->head = (bc->head + len) & (BUF_SIZE - 1);
  417. }
  418. spin_unlock_irqrestore(&bc->lock, flags);
  419. if (!len)
  420. break;
  421. s += len;
  422. count -= len;
  423. written += len;
  424. }
  425. ehv_bc_tx_dequeue(bc);
  426. return written;
  427. }
  428. /*
  429. * This function can be called multiple times for a given tty_struct, which is
  430. * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
  431. *
  432. * The tty layer will still call this function even if the device was not
  433. * registered (i.e. tty_register_device() was not called). This happens
  434. * because tty_register_device() is optional and some legacy drivers don't
  435. * use it. So we need to check for that.
  436. */
  437. static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
  438. {
  439. struct ehv_bc_data *bc = &bcs[ttys->index];
  440. if (!bc->dev)
  441. return -ENODEV;
  442. return tty_port_open(&bc->port, ttys, filp);
  443. }
  444. /*
  445. * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
  446. * still call this function to close the tty device. So we can't assume that
  447. * the tty port has been initialized.
  448. */
  449. static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
  450. {
  451. struct ehv_bc_data *bc = &bcs[ttys->index];
  452. if (bc->dev)
  453. tty_port_close(&bc->port, ttys, filp);
  454. }
  455. /*
  456. * Return the amount of space in the output buffer
  457. *
  458. * This is actually a contract between the driver and the tty layer outlining
  459. * how much write room the driver can guarantee will be sent OR BUFFERED. This
  460. * driver MUST honor the return value.
  461. */
  462. static unsigned int ehv_bc_tty_write_room(struct tty_struct *ttys)
  463. {
  464. struct ehv_bc_data *bc = ttys->driver_data;
  465. unsigned long flags;
  466. unsigned int count;
  467. spin_lock_irqsave(&bc->lock, flags);
  468. count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
  469. spin_unlock_irqrestore(&bc->lock, flags);
  470. return count;
  471. }
  472. /*
  473. * Stop sending data to the tty layer
  474. *
  475. * This function is called when the tty layer's input buffers are getting full,
  476. * so the driver should stop sending it data. The easiest way to do this is to
  477. * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
  478. * called.
  479. *
  480. * The hypervisor will continue to queue up any incoming data. If there is any
  481. * data in the queue when the RX interrupt is enabled, we'll immediately get an
  482. * RX interrupt.
  483. */
  484. static void ehv_bc_tty_throttle(struct tty_struct *ttys)
  485. {
  486. struct ehv_bc_data *bc = ttys->driver_data;
  487. disable_irq(bc->rx_irq);
  488. }
  489. /*
  490. * Resume sending data to the tty layer
  491. *
  492. * This function is called after previously calling ehv_bc_tty_throttle(). The
  493. * tty layer's input buffers now have more room, so the driver can resume
  494. * sending it data.
  495. */
  496. static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
  497. {
  498. struct ehv_bc_data *bc = ttys->driver_data;
  499. /* If there is any data in the queue when the RX interrupt is enabled,
  500. * we'll immediately get an RX interrupt.
  501. */
  502. enable_irq(bc->rx_irq);
  503. }
  504. static void ehv_bc_tty_hangup(struct tty_struct *ttys)
  505. {
  506. struct ehv_bc_data *bc = ttys->driver_data;
  507. ehv_bc_tx_dequeue(bc);
  508. tty_port_hangup(&bc->port);
  509. }
  510. /*
  511. * TTY driver operations
  512. *
  513. * If we could ask the hypervisor how much data is still in the TX buffer, or
  514. * at least how big the TX buffers are, then we could implement the
  515. * .wait_until_sent and .chars_in_buffer functions.
  516. */
  517. static const struct tty_operations ehv_bc_ops = {
  518. .open = ehv_bc_tty_open,
  519. .close = ehv_bc_tty_close,
  520. .write = ehv_bc_tty_write,
  521. .write_room = ehv_bc_tty_write_room,
  522. .throttle = ehv_bc_tty_throttle,
  523. .unthrottle = ehv_bc_tty_unthrottle,
  524. .hangup = ehv_bc_tty_hangup,
  525. };
  526. /*
  527. * initialize the TTY port
  528. *
  529. * This function will only be called once, no matter how many times
  530. * ehv_bc_tty_open() is called. That's why we register the ISR here, and also
  531. * why we initialize tty_struct-related variables here.
  532. */
  533. static int ehv_bc_tty_port_activate(struct tty_port *port,
  534. struct tty_struct *ttys)
  535. {
  536. struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
  537. int ret;
  538. ttys->driver_data = bc;
  539. ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
  540. if (ret < 0) {
  541. dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
  542. bc->rx_irq, ret);
  543. return ret;
  544. }
  545. /* request_irq also enables the IRQ */
  546. bc->tx_irq_enabled = 1;
  547. ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
  548. if (ret < 0) {
  549. dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
  550. bc->tx_irq, ret);
  551. free_irq(bc->rx_irq, bc);
  552. return ret;
  553. }
  554. /* The TX IRQ is enabled only when we can't write all the data to the
  555. * byte channel at once, so by default it's disabled.
  556. */
  557. disable_tx_interrupt(bc);
  558. return 0;
  559. }
  560. static void ehv_bc_tty_port_shutdown(struct tty_port *port)
  561. {
  562. struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
  563. free_irq(bc->tx_irq, bc);
  564. free_irq(bc->rx_irq, bc);
  565. }
  566. static const struct tty_port_operations ehv_bc_tty_port_ops = {
  567. .activate = ehv_bc_tty_port_activate,
  568. .shutdown = ehv_bc_tty_port_shutdown,
  569. };
  570. static int ehv_bc_tty_probe(struct platform_device *pdev)
  571. {
  572. struct device_node *np = pdev->dev.of_node;
  573. struct ehv_bc_data *bc;
  574. const uint32_t *iprop;
  575. unsigned int handle;
  576. int ret;
  577. static unsigned int index = 1;
  578. unsigned int i;
  579. iprop = of_get_property(np, "hv-handle", NULL);
  580. if (!iprop) {
  581. dev_err(&pdev->dev, "no 'hv-handle' property in %pOFn node\n",
  582. np);
  583. return -ENODEV;
  584. }
  585. /* We already told the console layer that the index for the console
  586. * device is zero, so we need to make sure that we use that index when
  587. * we probe the console byte channel node.
  588. */
  589. handle = be32_to_cpu(*iprop);
  590. i = (handle == stdout_bc) ? 0 : index++;
  591. bc = &bcs[i];
  592. bc->handle = handle;
  593. bc->head = 0;
  594. bc->tail = 0;
  595. spin_lock_init(&bc->lock);
  596. bc->rx_irq = irq_of_parse_and_map(np, 0);
  597. bc->tx_irq = irq_of_parse_and_map(np, 1);
  598. if (!bc->rx_irq || !bc->tx_irq) {
  599. dev_err(&pdev->dev, "no 'interrupts' property in %pOFn node\n",
  600. np);
  601. ret = -ENODEV;
  602. goto error;
  603. }
  604. tty_port_init(&bc->port);
  605. bc->port.ops = &ehv_bc_tty_port_ops;
  606. bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i,
  607. &pdev->dev);
  608. if (IS_ERR(bc->dev)) {
  609. ret = PTR_ERR(bc->dev);
  610. dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
  611. goto error;
  612. }
  613. dev_set_drvdata(&pdev->dev, bc);
  614. dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
  615. ehv_bc_driver->name, i, bc->handle);
  616. return 0;
  617. error:
  618. tty_port_destroy(&bc->port);
  619. irq_dispose_mapping(bc->tx_irq);
  620. irq_dispose_mapping(bc->rx_irq);
  621. memset(bc, 0, sizeof(struct ehv_bc_data));
  622. return ret;
  623. }
  624. static const struct of_device_id ehv_bc_tty_of_ids[] = {
  625. { .compatible = "epapr,hv-byte-channel" },
  626. {}
  627. };
  628. static struct platform_driver ehv_bc_tty_driver = {
  629. .driver = {
  630. .name = "ehv-bc",
  631. .of_match_table = ehv_bc_tty_of_ids,
  632. .suppress_bind_attrs = true,
  633. },
  634. .probe = ehv_bc_tty_probe,
  635. };
  636. /**
  637. * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
  638. *
  639. * This function is called when this driver is loaded.
  640. */
  641. static int __init ehv_bc_init(void)
  642. {
  643. struct tty_driver *driver;
  644. struct device_node *np;
  645. unsigned int count = 0; /* Number of elements in bcs[] */
  646. int ret;
  647. pr_info("ePAPR hypervisor byte channel driver\n");
  648. /* Count the number of byte channels */
  649. for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
  650. count++;
  651. if (!count)
  652. return -ENODEV;
  653. /* The array index of an element in bcs[] is the same as the tty index
  654. * for that element. If you know the address of an element in the
  655. * array, then you can use pointer math (e.g. "bc - bcs") to get its
  656. * tty index.
  657. */
  658. bcs = kcalloc(count, sizeof(struct ehv_bc_data), GFP_KERNEL);
  659. if (!bcs)
  660. return -ENOMEM;
  661. driver = tty_alloc_driver(count, TTY_DRIVER_REAL_RAW |
  662. TTY_DRIVER_DYNAMIC_DEV);
  663. if (IS_ERR(driver)) {
  664. ret = PTR_ERR(driver);
  665. goto err_free_bcs;
  666. }
  667. driver->driver_name = "ehv-bc";
  668. driver->name = ehv_bc_console.name;
  669. driver->type = TTY_DRIVER_TYPE_CONSOLE;
  670. driver->subtype = SYSTEM_TYPE_CONSOLE;
  671. driver->init_termios = tty_std_termios;
  672. tty_set_operations(driver, &ehv_bc_ops);
  673. ret = tty_register_driver(driver);
  674. if (ret) {
  675. pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
  676. goto err_tty_driver_kref_put;
  677. }
  678. ehv_bc_driver = driver;
  679. ret = platform_driver_register(&ehv_bc_tty_driver);
  680. if (ret) {
  681. pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
  682. ret);
  683. goto err_deregister_tty_driver;
  684. }
  685. return 0;
  686. err_deregister_tty_driver:
  687. ehv_bc_driver = NULL;
  688. tty_unregister_driver(driver);
  689. err_tty_driver_kref_put:
  690. tty_driver_kref_put(driver);
  691. err_free_bcs:
  692. kfree(bcs);
  693. return ret;
  694. }
  695. device_initcall(ehv_bc_init);