ehv_bytechan.c 21 KB

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