ehv_bytechan.c 21 KB

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