1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2006 Sony Computer Entertainment Inc.
6 * Copyright 2006 Sony Corp.
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/workqueue.h>
14 #include <linux/bitops.h>
17 #include <asm/firmware.h>
18 #include <asm/lv1call.h>
22 MODULE_AUTHOR("Sony Corporation");
23 MODULE_LICENSE("GPL v2");
24 MODULE_DESCRIPTION("PS3 vuart");
27 * vuart - An inter-partition data link service.
28 * port 0: PS3 AV Settings.
29 * port 2: PS3 System Manager.
31 * The vuart provides a bi-directional byte stream data link between logical
32 * partitions. Its primary role is as a communications link between the guest
33 * OS and the system policy module. The current HV does not support any
34 * connections other than those listed.
37 enum {PORT_COUNT = 3,};
42 PARAM_INTERRUPT_MASK = 2,
43 PARAM_RX_BUF_SIZE = 3, /* read only */
44 PARAM_RX_BYTES = 4, /* read only */
45 PARAM_TX_BUF_SIZE = 5, /* read only */
46 PARAM_TX_BYTES = 6, /* read only */
47 PARAM_INTERRUPT_STATUS = 7, /* read only */
50 enum vuart_interrupt_bit {
53 INTERRUPT_BIT_DISCONNECT = 2,
56 enum vuart_interrupt_mask {
57 INTERRUPT_MASK_TX = 1,
58 INTERRUPT_MASK_RX = 2,
59 INTERRUPT_MASK_DISCONNECT = 4,
63 * struct ps3_vuart_port_priv - private vuart device data.
66 struct ps3_vuart_port_priv {
71 struct list_head head;
74 struct ps3_vuart_work work;
75 unsigned long bytes_held;
77 struct list_head head;
79 struct ps3_vuart_stats stats;
82 static struct ps3_vuart_port_priv *to_port_priv(
83 struct ps3_system_bus_device *dev)
86 BUG_ON(!dev->driver_priv);
87 return (struct ps3_vuart_port_priv *)dev->driver_priv;
91 * struct ports_bmp - bitmap indicating ports needing service.
93 * A 256 bit read only bitmap indicating ports needing service. Do not write
94 * to these bits. Must not cross a page boundary.
100 } __attribute__((aligned(32)));
102 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
103 static void __maybe_unused _dump_ports_bmp(
104 const struct ports_bmp *bmp, const char *func, int line)
106 pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
109 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
110 static void __maybe_unused _dump_port_params(unsigned int port_number,
111 const char *func, int line)
114 static const char *strings[] = {
128 for (i = 0; i < ARRAY_SIZE(strings); i++) {
129 result = lv1_get_virtual_uart_param(port_number, i, &value);
132 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
133 port_number, strings[i], ps3_result(result));
136 pr_debug("%s:%d: port_%u: %s = %lxh\n",
137 func, line, port_number, strings[i], value);
142 int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
143 struct vuart_triggers *trig)
150 result = lv1_get_virtual_uart_param(dev->port_number,
151 PARAM_TX_TRIGGER, &tx);
155 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
156 __func__, __LINE__, ps3_result(result));
160 result = lv1_get_virtual_uart_param(dev->port_number,
161 PARAM_RX_BUF_SIZE, &size);
164 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
165 __func__, __LINE__, ps3_result(result));
169 result = lv1_get_virtual_uart_param(dev->port_number,
170 PARAM_RX_TRIGGER, &val);
173 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
174 __func__, __LINE__, ps3_result(result));
178 trig->rx = size - val;
180 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
186 int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
192 result = lv1_set_virtual_uart_param(dev->port_number,
193 PARAM_TX_TRIGGER, tx);
196 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
197 __func__, __LINE__, ps3_result(result));
201 result = lv1_get_virtual_uart_param(dev->port_number,
202 PARAM_RX_BUF_SIZE, &size);
205 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
206 __func__, __LINE__, ps3_result(result));
210 result = lv1_set_virtual_uart_param(dev->port_number,
211 PARAM_RX_TRIGGER, size - rx);
214 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
215 __func__, __LINE__, ps3_result(result));
219 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
225 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
230 result = lv1_get_virtual_uart_param(dev->port_number,
231 PARAM_RX_BYTES, bytes_waiting);
234 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
235 __func__, __LINE__, ps3_result(result));
237 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
243 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
244 * @dev: The struct ps3_system_bus_device instance.
245 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
248 static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
252 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
254 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
256 priv->interrupt_mask = mask;
258 result = lv1_set_virtual_uart_param(dev->port_number,
259 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
262 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
263 __func__, __LINE__, ps3_result(result));
268 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
269 unsigned long *status)
272 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
275 result = lv1_get_virtual_uart_param(dev->port_number,
276 PARAM_INTERRUPT_STATUS, &tmp);
279 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
280 __func__, __LINE__, ps3_result(result));
282 *status = tmp & priv->interrupt_mask;
284 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
285 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
290 int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
292 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
294 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
295 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
296 | INTERRUPT_MASK_TX);
299 int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
301 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
303 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
304 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
305 | INTERRUPT_MASK_RX);
308 int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
310 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
312 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
313 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
314 | INTERRUPT_MASK_DISCONNECT);
317 int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
319 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
321 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
322 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
323 & ~INTERRUPT_MASK_TX) : 0;
326 int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
328 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
330 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
331 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
332 & ~INTERRUPT_MASK_RX) : 0;
335 int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
337 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
339 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
340 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
341 & ~INTERRUPT_MASK_DISCONNECT) : 0;
345 * ps3_vuart_raw_write - Low level write helper.
346 * @dev: The struct ps3_system_bus_device instance.
348 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
351 static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
352 const void *buf, unsigned int bytes, u64 *bytes_written)
355 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
357 result = lv1_write_virtual_uart(dev->port_number,
358 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
361 dev_warn(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
362 "%s\n", __func__, __LINE__, ps3_result(result));
366 priv->stats.bytes_written += *bytes_written;
368 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
369 *bytes_written, bytes, priv->stats.bytes_written);
375 * ps3_vuart_raw_read - Low level read helper.
376 * @dev: The struct ps3_system_bus_device instance.
378 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
381 static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
382 unsigned int bytes, u64 *bytes_read)
385 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
387 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
389 result = lv1_read_virtual_uart(dev->port_number,
390 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
393 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
394 __func__, __LINE__, ps3_result(result));
398 priv->stats.bytes_read += *bytes_read;
400 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
401 *bytes_read, bytes, priv->stats.bytes_read);
407 * ps3_vuart_clear_rx_bytes - Discard bytes received.
408 * @dev: The struct ps3_system_bus_device instance.
409 * @bytes: Max byte count to discard, zero = all pending.
411 * Used to clear pending rx interrupt source. Will not block.
414 void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
418 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
422 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
426 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
428 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
433 /* Add some extra space for recently arrived data. */
437 tmp = kmalloc(bytes, GFP_KERNEL);
442 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
446 /* Don't include these bytes in the stats. */
448 priv->stats.bytes_read -= bytes_waiting;
450 EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
453 * struct list_buffer - An element for a port device fifo buffer list.
457 struct list_head link;
458 const unsigned char *head;
459 const unsigned char *tail;
460 unsigned long dbg_number;
461 unsigned char data[];
465 * ps3_vuart_write - the entry point for writing data to a port
466 * @dev: The struct ps3_system_bus_device instance.
468 * If the port is idle on entry as much of the incoming data is written to
469 * the port as the port will accept. Otherwise a list buffer is created
470 * and any remaning incoming data is copied to that buffer. The buffer is
471 * then enqueued for transmision via the transmit interrupt.
474 int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
477 static unsigned long dbg_number;
479 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
481 struct list_buffer *lb;
483 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
486 spin_lock_irqsave(&priv->tx_list.lock, flags);
488 if (list_empty(&priv->tx_list.head)) {
491 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
493 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
497 "%s:%d: ps3_vuart_raw_write failed\n",
502 if (bytes_written == bytes) {
503 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
504 __func__, __LINE__, bytes);
508 bytes -= bytes_written;
509 buf += bytes_written;
511 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
513 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
518 memcpy(lb->data, buf, bytes);
520 lb->tail = lb->data + bytes;
521 lb->dbg_number = ++dbg_number;
523 spin_lock_irqsave(&priv->tx_list.lock, flags);
524 list_add_tail(&lb->link, &priv->tx_list.head);
525 ps3_vuart_enable_interrupt_tx(dev);
526 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
528 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
529 __func__, __LINE__, lb->dbg_number, bytes);
533 EXPORT_SYMBOL_GPL(ps3_vuart_write);
536 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
537 * @dev: The struct ps3_system_bus_device instance.
538 * @bytes_queued: Number of bytes queued to the buffer list.
540 * Must be called with priv->rx_list.lock held.
543 static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
546 static unsigned long dbg_number;
548 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
549 struct list_buffer *lb;
554 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
563 /* Add some extra space for recently arrived data. */
567 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
572 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
575 lb->tail = lb->data + bytes;
576 lb->dbg_number = ++dbg_number;
578 list_add_tail(&lb->link, &priv->rx_list.head);
579 priv->rx_list.bytes_held += bytes;
581 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
582 __func__, __LINE__, lb->dbg_number, bytes);
584 *bytes_queued = bytes;
590 * ps3_vuart_read - The entry point for reading data from a port.
592 * Queue data waiting at the port, and if enough bytes to satisfy the request
593 * are held in the buffer list those bytes are dequeued and copied to the
594 * caller's buffer. Emptied list buffers are retiered. If the request cannot
595 * be statified by bytes held in the list buffers -EAGAIN is returned.
598 int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
602 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
604 struct list_buffer *lb, *n;
605 unsigned long bytes_read;
607 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
610 spin_lock_irqsave(&priv->rx_list.lock, flags);
612 /* Queue rx bytes here for polled reads. */
614 while (priv->rx_list.bytes_held < bytes) {
617 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
618 if (result || !tmp) {
619 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
621 bytes - priv->rx_list.bytes_held);
622 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
627 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
628 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
630 memcpy(buf, lb->head, bytes_read);
633 priv->rx_list.bytes_held -= bytes_read;
635 if (bytes_read < lb->tail - lb->head) {
636 lb->head += bytes_read;
637 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
638 "bytes\n", __func__, __LINE__, lb->dbg_number,
640 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
644 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
645 "bytes\n", __func__, __LINE__, lb->dbg_number,
652 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
655 EXPORT_SYMBOL_GPL(ps3_vuart_read);
658 * ps3_vuart_work - Asynchronous read handler.
661 static void ps3_vuart_work(struct work_struct *work)
663 struct ps3_system_bus_device *dev =
664 ps3_vuart_work_to_system_bus_dev(work);
665 struct ps3_vuart_port_driver *drv =
666 ps3_system_bus_dev_to_vuart_drv(dev);
672 int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
674 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
677 if (priv->rx_list.work.trigger) {
678 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
685 spin_lock_irqsave(&priv->rx_list.lock, flags);
686 if (priv->rx_list.bytes_held >= bytes) {
687 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
688 __func__, __LINE__, bytes);
689 schedule_work(&priv->rx_list.work.work);
690 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
694 priv->rx_list.work.trigger = bytes;
695 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
697 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
698 __LINE__, bytes, bytes);
702 EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
704 void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
706 to_port_priv(dev)->rx_list.work.trigger = 0;
708 EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
711 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
713 * Services the transmit interrupt for the port. Writes as much data from the
714 * buffer list as the port will accept. Retires any emptied list buffers and
715 * adjusts the final list buffer state for a partial write.
718 static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
721 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
723 struct list_buffer *lb, *n;
724 unsigned long bytes_total = 0;
726 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
728 spin_lock_irqsave(&priv->tx_list.lock, flags);
730 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
734 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
739 "%s:%d: ps3_vuart_raw_write failed\n",
744 bytes_total += bytes_written;
746 if (bytes_written < lb->tail - lb->head) {
747 lb->head += bytes_written;
749 "%s:%d cleared buf_%lu, %llxh bytes\n",
750 __func__, __LINE__, lb->dbg_number,
755 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
762 ps3_vuart_disable_interrupt_tx(dev);
764 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
765 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
766 __func__, __LINE__, bytes_total);
771 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
773 * Services the receive interrupt for the port. Creates a list buffer and
774 * copies all waiting port data to that buffer and enqueues the buffer in the
775 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
778 static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
781 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
785 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
787 spin_lock_irqsave(&priv->rx_list.lock, flags);
788 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
791 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
795 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
796 >= priv->rx_list.work.trigger) {
797 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
798 __func__, __LINE__, priv->rx_list.work.trigger);
799 priv->rx_list.work.trigger = 0;
800 schedule_work(&priv->rx_list.work.work);
803 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
807 static int ps3_vuart_handle_interrupt_disconnect(
808 struct ps3_system_bus_device *dev)
810 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
811 BUG_ON("no support");
816 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
818 * Services any pending interrupt types for the port. Passes control to the
819 * third stage type specific interrupt handler. Returns control to the first
820 * stage handler after one iteration.
823 static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
826 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
827 unsigned long status;
829 result = ps3_vuart_get_interrupt_status(dev, &status);
834 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
837 if (status & INTERRUPT_MASK_DISCONNECT) {
838 priv->stats.disconnect_interrupts++;
839 result = ps3_vuart_handle_interrupt_disconnect(dev);
841 ps3_vuart_disable_interrupt_disconnect(dev);
844 if (status & INTERRUPT_MASK_TX) {
845 priv->stats.tx_interrupts++;
846 result = ps3_vuart_handle_interrupt_tx(dev);
848 ps3_vuart_disable_interrupt_tx(dev);
851 if (status & INTERRUPT_MASK_RX) {
852 priv->stats.rx_interrupts++;
853 result = ps3_vuart_handle_interrupt_rx(dev);
855 ps3_vuart_disable_interrupt_rx(dev);
861 static struct vuart_bus_priv {
862 struct ports_bmp *bmp;
864 struct mutex probe_mutex;
866 struct ps3_system_bus_device *devices[PORT_COUNT];
870 * ps3_vuart_irq_handler - first stage interrupt handler
872 * Loops finding any interrupting port and its associated instance data.
873 * Passes control to the second stage port specific interrupt handler. Loops
874 * until all outstanding interrupts are serviced.
877 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
879 struct vuart_bus_priv *bus_priv = _private;
886 dump_ports_bmp(bus_priv->bmp);
888 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
890 if (port == BITS_PER_LONG)
893 BUG_ON(port >= PORT_COUNT);
894 BUG_ON(!bus_priv->devices[port]);
896 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
902 static int ps3_vuart_bus_interrupt_get(void)
906 pr_debug(" -> %s:%d\n", __func__, __LINE__);
908 vuart_bus_priv.use_count++;
910 BUG_ON(vuart_bus_priv.use_count > 2);
912 if (vuart_bus_priv.use_count != 1)
915 BUG_ON(vuart_bus_priv.bmp);
917 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
919 if (!vuart_bus_priv.bmp) {
921 goto fail_bmp_malloc;
924 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
925 &vuart_bus_priv.virq);
928 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
929 __func__, __LINE__, result);
934 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
935 0, "vuart", &vuart_bus_priv);
938 pr_debug("%s:%d: request_irq failed (%d)\n",
939 __func__, __LINE__, result);
940 goto fail_request_irq;
943 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
947 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
948 vuart_bus_priv.virq = 0;
950 kfree(vuart_bus_priv.bmp);
951 vuart_bus_priv.bmp = NULL;
953 vuart_bus_priv.use_count--;
954 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
958 static int ps3_vuart_bus_interrupt_put(void)
960 pr_debug(" -> %s:%d\n", __func__, __LINE__);
962 vuart_bus_priv.use_count--;
964 BUG_ON(vuart_bus_priv.use_count < 0);
966 if (vuart_bus_priv.use_count != 0)
969 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
971 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
972 vuart_bus_priv.virq = 0;
974 kfree(vuart_bus_priv.bmp);
975 vuart_bus_priv.bmp = NULL;
977 pr_debug(" <- %s:%d\n", __func__, __LINE__);
981 static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
984 struct ps3_vuart_port_driver *drv;
985 struct ps3_vuart_port_priv *priv = NULL;
987 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
989 drv = ps3_system_bus_dev_to_vuart_drv(dev);
992 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
993 drv->core.core.name);
995 if (dev->port_number >= PORT_COUNT) {
1000 mutex_lock(&vuart_bus_priv.probe_mutex);
1002 result = ps3_vuart_bus_interrupt_get();
1005 goto fail_setup_interrupt;
1007 if (vuart_bus_priv.devices[dev->port_number]) {
1008 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
1009 __LINE__, dev->port_number);
1014 vuart_bus_priv.devices[dev->port_number] = dev;
1016 /* Setup dev->driver_priv. */
1018 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1021 if (!dev->driver_priv) {
1023 goto fail_dev_malloc;
1026 priv = to_port_priv(dev);
1028 INIT_LIST_HEAD(&priv->tx_list.head);
1029 spin_lock_init(&priv->tx_list.lock);
1031 INIT_LIST_HEAD(&priv->rx_list.head);
1032 spin_lock_init(&priv->rx_list.lock);
1034 INIT_WORK(&priv->rx_list.work.work, ps3_vuart_work);
1035 priv->rx_list.work.trigger = 0;
1036 priv->rx_list.work.dev = dev;
1038 /* clear stale pending interrupts */
1040 ps3_vuart_clear_rx_bytes(dev, 0);
1042 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
1044 ps3_vuart_set_triggers(dev, 1, 1);
1047 result = drv->probe(dev);
1050 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1055 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1056 __func__, __LINE__);
1060 mutex_unlock(&vuart_bus_priv.probe_mutex);
1065 ps3_vuart_set_interrupt_mask(dev, 0);
1066 kfree(dev->driver_priv);
1067 dev->driver_priv = NULL;
1069 vuart_bus_priv.devices[dev->port_number] = NULL;
1071 ps3_vuart_bus_interrupt_put();
1072 fail_setup_interrupt:
1073 mutex_unlock(&vuart_bus_priv.probe_mutex);
1074 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
1079 * ps3_vuart_cleanup - common cleanup helper.
1080 * @dev: The struct ps3_system_bus_device instance.
1082 * Cleans interrupts and HV resources. Must be called with
1083 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1084 * ps3_vuart_shutdown. After this call, polled reading will still work.
1087 static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
1089 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1091 ps3_vuart_cancel_async(dev);
1092 ps3_vuart_set_interrupt_mask(dev, 0);
1093 ps3_vuart_bus_interrupt_put();
1098 * ps3_vuart_remove - Completely clean the device instance.
1099 * @dev: The struct ps3_system_bus_device instance.
1101 * Cleans all memory, interrupts and HV resources. After this call the
1102 * device can no longer be used.
1105 static void ps3_vuart_remove(struct ps3_system_bus_device *dev)
1107 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1108 struct ps3_vuart_port_driver *drv;
1112 mutex_lock(&vuart_bus_priv.probe_mutex);
1114 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1117 if (!dev->core.driver) {
1118 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1120 mutex_unlock(&vuart_bus_priv.probe_mutex);
1124 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1131 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1136 ps3_vuart_cleanup(dev);
1138 vuart_bus_priv.devices[dev->port_number] = NULL;
1142 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1143 mutex_unlock(&vuart_bus_priv.probe_mutex);
1147 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1148 * @dev: The struct ps3_system_bus_device instance.
1150 * Cleans interrupts and HV resources. After this call the
1151 * device can still be used in polling mode. This behavior required
1152 * by sys-manager to be able to complete the device power operation
1156 static void ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1158 struct ps3_vuart_port_driver *drv;
1162 mutex_lock(&vuart_bus_priv.probe_mutex);
1164 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1167 if (!dev->core.driver) {
1168 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1170 mutex_unlock(&vuart_bus_priv.probe_mutex);
1174 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1180 else if (drv->remove) {
1181 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1182 __func__, __LINE__);
1185 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1190 ps3_vuart_cleanup(dev);
1192 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1194 mutex_unlock(&vuart_bus_priv.probe_mutex);
1197 static int __init ps3_vuart_bus_init(void)
1199 pr_debug("%s:%d:\n", __func__, __LINE__);
1201 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1204 mutex_init(&vuart_bus_priv.probe_mutex);
1209 static void __exit ps3_vuart_bus_exit(void)
1211 pr_debug("%s:%d:\n", __func__, __LINE__);
1214 core_initcall(ps3_vuart_bus_init);
1215 module_exit(ps3_vuart_bus_exit);
1218 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1221 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1225 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1227 BUG_ON(!drv->core.match_id);
1228 BUG_ON(!drv->core.core.name);
1230 drv->core.probe = ps3_vuart_probe;
1231 drv->core.remove = ps3_vuart_remove;
1232 drv->core.shutdown = ps3_vuart_shutdown;
1234 result = ps3_system_bus_driver_register(&drv->core);
1237 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1240 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1243 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1245 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1246 ps3_system_bus_driver_unregister(&drv->core);
1248 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);