1 /*
2 * PS3 virtual uart
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/bitops.h>
26 #include <asm/ps3.h>
27
28 #include <asm/firmware.h>
29 #include <asm/lv1call.h>
30
31 #include "vuart.h"
32
33 MODULE_AUTHOR("Sony Corporation");
34 MODULE_LICENSE("GPL v2");
35 MODULE_DESCRIPTION("PS3 vuart");
36
37 /**
38 * vuart - An inter-partition data link service.
39 * port 0: PS3 AV Settings.
40 * port 2: PS3 System Manager.
41 *
42 * The vuart provides a bi-directional byte stream data link between logical
43 * partitions. Its primary role is as a communications link between the guest
44 * OS and the system policy module. The current HV does not support any
45 * connections other than those listed.
46 */
47
48 enum {PORT_COUNT = 3,};
49
50 enum vuart_param {
51 PARAM_TX_TRIGGER = 0,
52 PARAM_RX_TRIGGER = 1,
53 PARAM_INTERRUPT_MASK = 2,
54 PARAM_RX_BUF_SIZE = 3, /* read only */
55 PARAM_RX_BYTES = 4, /* read only */
56 PARAM_TX_BUF_SIZE = 5, /* read only */
57 PARAM_TX_BYTES = 6, /* read only */
58 PARAM_INTERRUPT_STATUS = 7, /* read only */
59 };
60
61 enum vuart_interrupt_bit {
62 INTERRUPT_BIT_TX = 0,
63 INTERRUPT_BIT_RX = 1,
64 INTERRUPT_BIT_DISCONNECT = 2,
65 };
66
67 enum vuart_interrupt_mask {
68 INTERRUPT_MASK_TX = 1,
69 INTERRUPT_MASK_RX = 2,
70 INTERRUPT_MASK_DISCONNECT = 4,
71 };
72
73 /**
74 * struct ps3_vuart_port_priv - private vuart device data.
75 */
76
77 struct ps3_vuart_port_priv {
78 u64 interrupt_mask;
79
80 struct {
81 spinlock_t lock;
82 struct list_head head;
83 } tx_list;
84 struct {
85 struct ps3_vuart_work work;
86 unsigned long bytes_held;
87 spinlock_t lock;
88 struct list_head head;
89 } rx_list;
90 struct ps3_vuart_stats stats;
91 };
92
to_port_priv(struct ps3_system_bus_device * dev)93 static struct ps3_vuart_port_priv *to_port_priv(
94 struct ps3_system_bus_device *dev)
95 {
96 BUG_ON(!dev);
97 BUG_ON(!dev->driver_priv);
98 return (struct ps3_vuart_port_priv *)dev->driver_priv;
99 }
100
101 /**
102 * struct ports_bmp - bitmap indicating ports needing service.
103 *
104 * A 256 bit read only bitmap indicating ports needing service. Do not write
105 * to these bits. Must not cross a page boundary.
106 */
107
108 struct ports_bmp {
109 u64 status;
110 u64 unused[3];
111 } __attribute__((aligned(32)));
112
113 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
_dump_ports_bmp(const struct ports_bmp * bmp,const char * func,int line)114 static void __maybe_unused _dump_ports_bmp(
115 const struct ports_bmp *bmp, const char *func, int line)
116 {
117 pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
118 }
119
120 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
_dump_port_params(unsigned int port_number,const char * func,int line)121 static void __maybe_unused _dump_port_params(unsigned int port_number,
122 const char *func, int line)
123 {
124 #if defined(DEBUG)
125 static const char *strings[] = {
126 "tx_trigger ",
127 "rx_trigger ",
128 "interrupt_mask ",
129 "rx_buf_size ",
130 "rx_bytes ",
131 "tx_buf_size ",
132 "tx_bytes ",
133 "interrupt_status",
134 };
135 int result;
136 unsigned int i;
137 u64 value;
138
139 for (i = 0; i < ARRAY_SIZE(strings); i++) {
140 result = lv1_get_virtual_uart_param(port_number, i, &value);
141
142 if (result) {
143 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
144 port_number, strings[i], ps3_result(result));
145 continue;
146 }
147 pr_debug("%s:%d: port_%u: %s = %lxh\n",
148 func, line, port_number, strings[i], value);
149 }
150 #endif
151 }
152
153 struct vuart_triggers {
154 unsigned long rx;
155 unsigned long tx;
156 };
157
ps3_vuart_get_triggers(struct ps3_system_bus_device * dev,struct vuart_triggers * trig)158 int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
159 struct vuart_triggers *trig)
160 {
161 int result;
162 u64 size;
163 u64 val;
164 u64 tx;
165
166 result = lv1_get_virtual_uart_param(dev->port_number,
167 PARAM_TX_TRIGGER, &tx);
168 trig->tx = tx;
169
170 if (result) {
171 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
172 __func__, __LINE__, ps3_result(result));
173 return result;
174 }
175
176 result = lv1_get_virtual_uart_param(dev->port_number,
177 PARAM_RX_BUF_SIZE, &size);
178
179 if (result) {
180 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
181 __func__, __LINE__, ps3_result(result));
182 return result;
183 }
184
185 result = lv1_get_virtual_uart_param(dev->port_number,
186 PARAM_RX_TRIGGER, &val);
187
188 if (result) {
189 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
190 __func__, __LINE__, ps3_result(result));
191 return result;
192 }
193
194 trig->rx = size - val;
195
196 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
197 trig->tx, trig->rx);
198
199 return result;
200 }
201
ps3_vuart_set_triggers(struct ps3_system_bus_device * dev,unsigned int tx,unsigned int rx)202 int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
203 unsigned int rx)
204 {
205 int result;
206 u64 size;
207
208 result = lv1_set_virtual_uart_param(dev->port_number,
209 PARAM_TX_TRIGGER, tx);
210
211 if (result) {
212 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
213 __func__, __LINE__, ps3_result(result));
214 return result;
215 }
216
217 result = lv1_get_virtual_uart_param(dev->port_number,
218 PARAM_RX_BUF_SIZE, &size);
219
220 if (result) {
221 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
222 __func__, __LINE__, ps3_result(result));
223 return result;
224 }
225
226 result = lv1_set_virtual_uart_param(dev->port_number,
227 PARAM_RX_TRIGGER, size - rx);
228
229 if (result) {
230 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
231 __func__, __LINE__, ps3_result(result));
232 return result;
233 }
234
235 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
236 tx, rx);
237
238 return result;
239 }
240
ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device * dev,u64 * bytes_waiting)241 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
242 u64 *bytes_waiting)
243 {
244 int result;
245
246 result = lv1_get_virtual_uart_param(dev->port_number,
247 PARAM_RX_BYTES, bytes_waiting);
248
249 if (result)
250 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
251 __func__, __LINE__, ps3_result(result));
252
253 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
254 *bytes_waiting);
255 return result;
256 }
257
258 /**
259 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
260 * @dev: The struct ps3_system_bus_device instance.
261 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
262 */
263
ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device * dev,unsigned long mask)264 static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
265 unsigned long mask)
266 {
267 int result;
268 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
269
270 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
271
272 priv->interrupt_mask = mask;
273
274 result = lv1_set_virtual_uart_param(dev->port_number,
275 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
276
277 if (result)
278 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
279 __func__, __LINE__, ps3_result(result));
280
281 return result;
282 }
283
ps3_vuart_get_interrupt_status(struct ps3_system_bus_device * dev,unsigned long * status)284 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
285 unsigned long *status)
286 {
287 int result;
288 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
289 u64 tmp;
290
291 result = lv1_get_virtual_uart_param(dev->port_number,
292 PARAM_INTERRUPT_STATUS, &tmp);
293
294 if (result)
295 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
296 __func__, __LINE__, ps3_result(result));
297
298 *status = tmp & priv->interrupt_mask;
299
300 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
301 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
302
303 return result;
304 }
305
ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device * dev)306 int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
307 {
308 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
309
310 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
311 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
312 | INTERRUPT_MASK_TX);
313 }
314
ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device * dev)315 int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
316 {
317 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
318
319 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
320 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
321 | INTERRUPT_MASK_RX);
322 }
323
ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device * dev)324 int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
325 {
326 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
327
328 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
329 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
330 | INTERRUPT_MASK_DISCONNECT);
331 }
332
ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device * dev)333 int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
334 {
335 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
336
337 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
338 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
339 & ~INTERRUPT_MASK_TX) : 0;
340 }
341
ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device * dev)342 int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
343 {
344 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
345
346 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
347 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
348 & ~INTERRUPT_MASK_RX) : 0;
349 }
350
ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device * dev)351 int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
352 {
353 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
354
355 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
356 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
357 & ~INTERRUPT_MASK_DISCONNECT) : 0;
358 }
359
360 /**
361 * ps3_vuart_raw_write - Low level write helper.
362 * @dev: The struct ps3_system_bus_device instance.
363 *
364 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
365 */
366
ps3_vuart_raw_write(struct ps3_system_bus_device * dev,const void * buf,unsigned int bytes,u64 * bytes_written)367 static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
368 const void *buf, unsigned int bytes, u64 *bytes_written)
369 {
370 int result;
371 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
372
373 result = lv1_write_virtual_uart(dev->port_number,
374 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
375
376 if (result) {
377 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
378 "%s\n", __func__, __LINE__, ps3_result(result));
379 return result;
380 }
381
382 priv->stats.bytes_written += *bytes_written;
383
384 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
385 *bytes_written, bytes, priv->stats.bytes_written);
386
387 return result;
388 }
389
390 /**
391 * ps3_vuart_raw_read - Low level read helper.
392 * @dev: The struct ps3_system_bus_device instance.
393 *
394 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
395 */
396
ps3_vuart_raw_read(struct ps3_system_bus_device * dev,void * buf,unsigned int bytes,u64 * bytes_read)397 static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
398 unsigned int bytes, u64 *bytes_read)
399 {
400 int result;
401 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
402
403 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
404
405 result = lv1_read_virtual_uart(dev->port_number,
406 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
407
408 if (result) {
409 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
410 __func__, __LINE__, ps3_result(result));
411 return result;
412 }
413
414 priv->stats.bytes_read += *bytes_read;
415
416 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
417 *bytes_read, bytes, priv->stats.bytes_read);
418
419 return result;
420 }
421
422 /**
423 * ps3_vuart_clear_rx_bytes - Discard bytes received.
424 * @dev: The struct ps3_system_bus_device instance.
425 * @bytes: Max byte count to discard, zero = all pending.
426 *
427 * Used to clear pending rx interrupt source. Will not block.
428 */
429
ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device * dev,unsigned int bytes)430 void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
431 unsigned int bytes)
432 {
433 int result;
434 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
435 u64 bytes_waiting;
436 void *tmp;
437
438 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
439
440 BUG_ON(result);
441
442 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
443
444 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
445
446 if (!bytes)
447 return;
448
449 /* Add some extra space for recently arrived data. */
450
451 bytes += 128;
452
453 tmp = kmalloc(bytes, GFP_KERNEL);
454
455 if (!tmp)
456 return;
457
458 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
459
460 kfree(tmp);
461
462 /* Don't include these bytes in the stats. */
463
464 priv->stats.bytes_read -= bytes_waiting;
465 }
466 EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
467
468 /**
469 * struct list_buffer - An element for a port device fifo buffer list.
470 */
471
472 struct list_buffer {
473 struct list_head link;
474 const unsigned char *head;
475 const unsigned char *tail;
476 unsigned long dbg_number;
477 unsigned char data[];
478 };
479
480 /**
481 * ps3_vuart_write - the entry point for writing data to a port
482 * @dev: The struct ps3_system_bus_device instance.
483 *
484 * If the port is idle on entry as much of the incoming data is written to
485 * the port as the port will accept. Otherwise a list buffer is created
486 * and any remaning incoming data is copied to that buffer. The buffer is
487 * then enqueued for transmision via the transmit interrupt.
488 */
489
ps3_vuart_write(struct ps3_system_bus_device * dev,const void * buf,unsigned int bytes)490 int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
491 unsigned int bytes)
492 {
493 static unsigned long dbg_number;
494 int result;
495 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
496 unsigned long flags;
497 struct list_buffer *lb;
498
499 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
500 bytes, bytes);
501
502 spin_lock_irqsave(&priv->tx_list.lock, flags);
503
504 if (list_empty(&priv->tx_list.head)) {
505 u64 bytes_written;
506
507 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
508
509 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
510
511 if (result) {
512 dev_dbg(&dev->core,
513 "%s:%d: ps3_vuart_raw_write failed\n",
514 __func__, __LINE__);
515 return result;
516 }
517
518 if (bytes_written == bytes) {
519 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
520 __func__, __LINE__, bytes);
521 return 0;
522 }
523
524 bytes -= bytes_written;
525 buf += bytes_written;
526 } else
527 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
528
529 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
530
531 if (!lb)
532 return -ENOMEM;
533
534 memcpy(lb->data, buf, bytes);
535 lb->head = lb->data;
536 lb->tail = lb->data + bytes;
537 lb->dbg_number = ++dbg_number;
538
539 spin_lock_irqsave(&priv->tx_list.lock, flags);
540 list_add_tail(&lb->link, &priv->tx_list.head);
541 ps3_vuart_enable_interrupt_tx(dev);
542 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
543
544 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
545 __func__, __LINE__, lb->dbg_number, bytes);
546
547 return 0;
548 }
549 EXPORT_SYMBOL_GPL(ps3_vuart_write);
550
551 /**
552 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
553 * @dev: The struct ps3_system_bus_device instance.
554 * @bytes_queued: Number of bytes queued to the buffer list.
555 *
556 * Must be called with priv->rx_list.lock held.
557 */
558
ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device * dev,u64 * bytes_queued)559 static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
560 u64 *bytes_queued)
561 {
562 static unsigned long dbg_number;
563 int result;
564 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
565 struct list_buffer *lb;
566 u64 bytes;
567
568 *bytes_queued = 0;
569
570 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
571 BUG_ON(result);
572
573 if (result)
574 return -EIO;
575
576 if (!bytes)
577 return 0;
578
579 /* Add some extra space for recently arrived data. */
580
581 bytes += 128;
582
583 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
584
585 if (!lb)
586 return -ENOMEM;
587
588 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
589
590 lb->head = lb->data;
591 lb->tail = lb->data + bytes;
592 lb->dbg_number = ++dbg_number;
593
594 list_add_tail(&lb->link, &priv->rx_list.head);
595 priv->rx_list.bytes_held += bytes;
596
597 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
598 __func__, __LINE__, lb->dbg_number, bytes);
599
600 *bytes_queued = bytes;
601
602 return 0;
603 }
604
605 /**
606 * ps3_vuart_read - The entry point for reading data from a port.
607 *
608 * Queue data waiting at the port, and if enough bytes to satisfy the request
609 * are held in the buffer list those bytes are dequeued and copied to the
610 * caller's buffer. Emptied list buffers are retiered. If the request cannot
611 * be statified by bytes held in the list buffers -EAGAIN is returned.
612 */
613
ps3_vuart_read(struct ps3_system_bus_device * dev,void * buf,unsigned int bytes)614 int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
615 unsigned int bytes)
616 {
617 int result;
618 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
619 unsigned long flags;
620 struct list_buffer *lb, *n;
621 unsigned long bytes_read;
622
623 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
624 bytes, bytes);
625
626 spin_lock_irqsave(&priv->rx_list.lock, flags);
627
628 /* Queue rx bytes here for polled reads. */
629
630 while (priv->rx_list.bytes_held < bytes) {
631 u64 tmp;
632
633 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
634 if (result || !tmp) {
635 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
636 __func__, __LINE__,
637 bytes - priv->rx_list.bytes_held);
638 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
639 return -EAGAIN;
640 }
641 }
642
643 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
644 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
645
646 memcpy(buf, lb->head, bytes_read);
647 buf += bytes_read;
648 bytes -= bytes_read;
649 priv->rx_list.bytes_held -= bytes_read;
650
651 if (bytes_read < lb->tail - lb->head) {
652 lb->head += bytes_read;
653 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
654 "bytes\n", __func__, __LINE__, lb->dbg_number,
655 bytes_read);
656 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
657 return 0;
658 }
659
660 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
661 "bytes\n", __func__, __LINE__, lb->dbg_number,
662 bytes_read);
663
664 list_del(&lb->link);
665 kfree(lb);
666 }
667
668 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
669 return 0;
670 }
671 EXPORT_SYMBOL_GPL(ps3_vuart_read);
672
673 /**
674 * ps3_vuart_work - Asynchronous read handler.
675 */
676
ps3_vuart_work(struct work_struct * work)677 static void ps3_vuart_work(struct work_struct *work)
678 {
679 struct ps3_system_bus_device *dev =
680 ps3_vuart_work_to_system_bus_dev(work);
681 struct ps3_vuart_port_driver *drv =
682 ps3_system_bus_dev_to_vuart_drv(dev);
683
684 BUG_ON(!drv);
685 drv->work(dev);
686 }
687
ps3_vuart_read_async(struct ps3_system_bus_device * dev,unsigned int bytes)688 int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
689 {
690 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
691 unsigned long flags;
692
693 if (priv->rx_list.work.trigger) {
694 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
695 __func__, __LINE__);
696 return -EAGAIN;
697 }
698
699 BUG_ON(!bytes);
700
701 PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
702
703 spin_lock_irqsave(&priv->rx_list.lock, flags);
704 if (priv->rx_list.bytes_held >= bytes) {
705 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
706 __func__, __LINE__, bytes);
707 schedule_work(&priv->rx_list.work.work);
708 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
709 return 0;
710 }
711
712 priv->rx_list.work.trigger = bytes;
713 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
714
715 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
716 __LINE__, bytes, bytes);
717
718 return 0;
719 }
720 EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
721
ps3_vuart_cancel_async(struct ps3_system_bus_device * dev)722 void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
723 {
724 to_port_priv(dev)->rx_list.work.trigger = 0;
725 }
726 EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
727
728 /**
729 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
730 *
731 * Services the transmit interrupt for the port. Writes as much data from the
732 * buffer list as the port will accept. Retires any emptied list buffers and
733 * adjusts the final list buffer state for a partial write.
734 */
735
ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device * dev)736 static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
737 {
738 int result = 0;
739 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
740 unsigned long flags;
741 struct list_buffer *lb, *n;
742 unsigned long bytes_total = 0;
743
744 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
745
746 spin_lock_irqsave(&priv->tx_list.lock, flags);
747
748 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
749
750 u64 bytes_written;
751
752 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
753 &bytes_written);
754
755 if (result) {
756 dev_dbg(&dev->core,
757 "%s:%d: ps3_vuart_raw_write failed\n",
758 __func__, __LINE__);
759 break;
760 }
761
762 bytes_total += bytes_written;
763
764 if (bytes_written < lb->tail - lb->head) {
765 lb->head += bytes_written;
766 dev_dbg(&dev->core,
767 "%s:%d cleared buf_%lu, %llxh bytes\n",
768 __func__, __LINE__, lb->dbg_number,
769 bytes_written);
770 goto port_full;
771 }
772
773 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
774 lb->dbg_number);
775
776 list_del(&lb->link);
777 kfree(lb);
778 }
779
780 ps3_vuart_disable_interrupt_tx(dev);
781 port_full:
782 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
783 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
784 __func__, __LINE__, bytes_total);
785 return result;
786 }
787
788 /**
789 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
790 *
791 * Services the receive interrupt for the port. Creates a list buffer and
792 * copies all waiting port data to that buffer and enqueues the buffer in the
793 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
794 */
795
ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device * dev)796 static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
797 {
798 int result;
799 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
800 unsigned long flags;
801 u64 bytes;
802
803 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
804
805 spin_lock_irqsave(&priv->rx_list.lock, flags);
806 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
807
808 if (result) {
809 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
810 return result;
811 }
812
813 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
814 >= priv->rx_list.work.trigger) {
815 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
816 __func__, __LINE__, priv->rx_list.work.trigger);
817 priv->rx_list.work.trigger = 0;
818 schedule_work(&priv->rx_list.work.work);
819 }
820
821 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
822 return result;
823 }
824
ps3_vuart_handle_interrupt_disconnect(struct ps3_system_bus_device * dev)825 static int ps3_vuart_handle_interrupt_disconnect(
826 struct ps3_system_bus_device *dev)
827 {
828 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
829 BUG_ON("no support");
830 return -1;
831 }
832
833 /**
834 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
835 *
836 * Services any pending interrupt types for the port. Passes control to the
837 * third stage type specific interrupt handler. Returns control to the first
838 * stage handler after one iteration.
839 */
840
ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device * dev)841 static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
842 {
843 int result;
844 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
845 unsigned long status;
846
847 result = ps3_vuart_get_interrupt_status(dev, &status);
848
849 if (result)
850 return result;
851
852 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
853 status);
854
855 if (status & INTERRUPT_MASK_DISCONNECT) {
856 priv->stats.disconnect_interrupts++;
857 result = ps3_vuart_handle_interrupt_disconnect(dev);
858 if (result)
859 ps3_vuart_disable_interrupt_disconnect(dev);
860 }
861
862 if (status & INTERRUPT_MASK_TX) {
863 priv->stats.tx_interrupts++;
864 result = ps3_vuart_handle_interrupt_tx(dev);
865 if (result)
866 ps3_vuart_disable_interrupt_tx(dev);
867 }
868
869 if (status & INTERRUPT_MASK_RX) {
870 priv->stats.rx_interrupts++;
871 result = ps3_vuart_handle_interrupt_rx(dev);
872 if (result)
873 ps3_vuart_disable_interrupt_rx(dev);
874 }
875
876 return 0;
877 }
878
879 struct vuart_bus_priv {
880 struct ports_bmp *bmp;
881 unsigned int virq;
882 struct mutex probe_mutex;
883 int use_count;
884 struct ps3_system_bus_device *devices[PORT_COUNT];
885 } static vuart_bus_priv;
886
887 /**
888 * ps3_vuart_irq_handler - first stage interrupt handler
889 *
890 * Loops finding any interrupting port and its associated instance data.
891 * Passes control to the second stage port specific interrupt handler. Loops
892 * until all outstanding interrupts are serviced.
893 */
894
ps3_vuart_irq_handler(int irq,void * _private)895 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
896 {
897 struct vuart_bus_priv *bus_priv = _private;
898
899 BUG_ON(!bus_priv);
900
901 while (1) {
902 unsigned int port;
903
904 dump_ports_bmp(bus_priv->bmp);
905
906 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
907
908 if (port == BITS_PER_LONG)
909 break;
910
911 BUG_ON(port >= PORT_COUNT);
912 BUG_ON(!bus_priv->devices[port]);
913
914 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
915 }
916
917 return IRQ_HANDLED;
918 }
919
ps3_vuart_bus_interrupt_get(void)920 static int ps3_vuart_bus_interrupt_get(void)
921 {
922 int result;
923
924 pr_debug(" -> %s:%d\n", __func__, __LINE__);
925
926 vuart_bus_priv.use_count++;
927
928 BUG_ON(vuart_bus_priv.use_count > 2);
929
930 if (vuart_bus_priv.use_count != 1)
931 return 0;
932
933 BUG_ON(vuart_bus_priv.bmp);
934
935 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
936
937 if (!vuart_bus_priv.bmp) {
938 pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
939 result = -ENOMEM;
940 goto fail_bmp_malloc;
941 }
942
943 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
944 &vuart_bus_priv.virq);
945
946 if (result) {
947 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
948 __func__, __LINE__, result);
949 result = -EPERM;
950 goto fail_alloc_irq;
951 }
952
953 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
954 IRQF_DISABLED, "vuart", &vuart_bus_priv);
955
956 if (result) {
957 pr_debug("%s:%d: request_irq failed (%d)\n",
958 __func__, __LINE__, result);
959 goto fail_request_irq;
960 }
961
962 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
963 return result;
964
965 fail_request_irq:
966 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
967 vuart_bus_priv.virq = NO_IRQ;
968 fail_alloc_irq:
969 kfree(vuart_bus_priv.bmp);
970 vuart_bus_priv.bmp = NULL;
971 fail_bmp_malloc:
972 vuart_bus_priv.use_count--;
973 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
974 return result;
975 }
976
ps3_vuart_bus_interrupt_put(void)977 static int ps3_vuart_bus_interrupt_put(void)
978 {
979 pr_debug(" -> %s:%d\n", __func__, __LINE__);
980
981 vuart_bus_priv.use_count--;
982
983 BUG_ON(vuart_bus_priv.use_count < 0);
984
985 if (vuart_bus_priv.use_count != 0)
986 return 0;
987
988 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
989
990 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
991 vuart_bus_priv.virq = NO_IRQ;
992
993 kfree(vuart_bus_priv.bmp);
994 vuart_bus_priv.bmp = NULL;
995
996 pr_debug(" <- %s:%d\n", __func__, __LINE__);
997 return 0;
998 }
999
ps3_vuart_probe(struct ps3_system_bus_device * dev)1000 static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
1001 {
1002 int result;
1003 struct ps3_vuart_port_driver *drv;
1004 struct ps3_vuart_port_priv *priv = NULL;
1005
1006 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1007
1008 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1009
1010 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
1011 drv->core.core.name);
1012
1013 BUG_ON(!drv);
1014
1015 if (dev->port_number >= PORT_COUNT) {
1016 BUG();
1017 return -EINVAL;
1018 }
1019
1020 mutex_lock(&vuart_bus_priv.probe_mutex);
1021
1022 result = ps3_vuart_bus_interrupt_get();
1023
1024 if (result)
1025 goto fail_setup_interrupt;
1026
1027 if (vuart_bus_priv.devices[dev->port_number]) {
1028 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
1029 __LINE__, dev->port_number);
1030 result = -EBUSY;
1031 goto fail_busy;
1032 }
1033
1034 vuart_bus_priv.devices[dev->port_number] = dev;
1035
1036 /* Setup dev->driver_priv. */
1037
1038 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1039 GFP_KERNEL);
1040
1041 if (!dev->driver_priv) {
1042 result = -ENOMEM;
1043 goto fail_dev_malloc;
1044 }
1045
1046 priv = to_port_priv(dev);
1047
1048 INIT_LIST_HEAD(&priv->tx_list.head);
1049 spin_lock_init(&priv->tx_list.lock);
1050
1051 INIT_LIST_HEAD(&priv->rx_list.head);
1052 spin_lock_init(&priv->rx_list.lock);
1053
1054 INIT_WORK(&priv->rx_list.work.work, NULL);
1055 priv->rx_list.work.trigger = 0;
1056 priv->rx_list.work.dev = dev;
1057
1058 /* clear stale pending interrupts */
1059
1060 ps3_vuart_clear_rx_bytes(dev, 0);
1061
1062 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
1063
1064 ps3_vuart_set_triggers(dev, 1, 1);
1065
1066 if (drv->probe)
1067 result = drv->probe(dev);
1068 else {
1069 result = 0;
1070 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1071 __LINE__);
1072 }
1073
1074 if (result) {
1075 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1076 __func__, __LINE__);
1077 goto fail_probe;
1078 }
1079
1080 mutex_unlock(&vuart_bus_priv.probe_mutex);
1081
1082 return result;
1083
1084 fail_probe:
1085 ps3_vuart_set_interrupt_mask(dev, 0);
1086 kfree(dev->driver_priv);
1087 dev->driver_priv = NULL;
1088 fail_dev_malloc:
1089 vuart_bus_priv.devices[dev->port_number] = NULL;
1090 fail_busy:
1091 ps3_vuart_bus_interrupt_put();
1092 fail_setup_interrupt:
1093 mutex_unlock(&vuart_bus_priv.probe_mutex);
1094 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
1095 return result;
1096 }
1097
1098 /**
1099 * ps3_vuart_cleanup - common cleanup helper.
1100 * @dev: The struct ps3_system_bus_device instance.
1101 *
1102 * Cleans interrupts and HV resources. Must be called with
1103 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1104 * ps3_vuart_shutdown. After this call, polled reading will still work.
1105 */
1106
ps3_vuart_cleanup(struct ps3_system_bus_device * dev)1107 static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
1108 {
1109 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1110
1111 ps3_vuart_cancel_async(dev);
1112 ps3_vuart_set_interrupt_mask(dev, 0);
1113 ps3_vuart_bus_interrupt_put();
1114 return 0;
1115 }
1116
1117 /**
1118 * ps3_vuart_remove - Completely clean the device instance.
1119 * @dev: The struct ps3_system_bus_device instance.
1120 *
1121 * Cleans all memory, interrupts and HV resources. After this call the
1122 * device can no longer be used.
1123 */
1124
ps3_vuart_remove(struct ps3_system_bus_device * dev)1125 static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
1126 {
1127 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1128 struct ps3_vuart_port_driver *drv;
1129
1130 BUG_ON(!dev);
1131
1132 mutex_lock(&vuart_bus_priv.probe_mutex);
1133
1134 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1135 dev->match_id);
1136
1137 if (!dev->core.driver) {
1138 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1139 __LINE__);
1140 mutex_unlock(&vuart_bus_priv.probe_mutex);
1141 return 0;
1142 }
1143
1144 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1145
1146 BUG_ON(!drv);
1147
1148 if (drv->remove) {
1149 drv->remove(dev);
1150 } else {
1151 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1152 __LINE__);
1153 BUG();
1154 }
1155
1156 ps3_vuart_cleanup(dev);
1157
1158 vuart_bus_priv.devices[dev->port_number] = NULL;
1159 kfree(priv);
1160 priv = NULL;
1161
1162 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1163 mutex_unlock(&vuart_bus_priv.probe_mutex);
1164 return 0;
1165 }
1166
1167 /**
1168 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1169 * @dev: The struct ps3_system_bus_device instance.
1170 *
1171 * Cleans interrupts and HV resources. After this call the
1172 * device can still be used in polling mode. This behavior required
1173 * by sys-manager to be able to complete the device power operation
1174 * sequence.
1175 */
1176
ps3_vuart_shutdown(struct ps3_system_bus_device * dev)1177 static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1178 {
1179 struct ps3_vuart_port_driver *drv;
1180
1181 BUG_ON(!dev);
1182
1183 mutex_lock(&vuart_bus_priv.probe_mutex);
1184
1185 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1186 dev->match_id);
1187
1188 if (!dev->core.driver) {
1189 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1190 __LINE__);
1191 mutex_unlock(&vuart_bus_priv.probe_mutex);
1192 return 0;
1193 }
1194
1195 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1196
1197 BUG_ON(!drv);
1198
1199 if (drv->shutdown)
1200 drv->shutdown(dev);
1201 else if (drv->remove) {
1202 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1203 __func__, __LINE__);
1204 drv->remove(dev);
1205 } else {
1206 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1207 __LINE__);
1208 BUG();
1209 }
1210
1211 ps3_vuart_cleanup(dev);
1212
1213 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1214
1215 mutex_unlock(&vuart_bus_priv.probe_mutex);
1216 return 0;
1217 }
1218
ps3_vuart_bus_init(void)1219 static int __init ps3_vuart_bus_init(void)
1220 {
1221 pr_debug("%s:%d:\n", __func__, __LINE__);
1222
1223 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1224 return -ENODEV;
1225
1226 mutex_init(&vuart_bus_priv.probe_mutex);
1227
1228 return 0;
1229 }
1230
ps3_vuart_bus_exit(void)1231 static void __exit ps3_vuart_bus_exit(void)
1232 {
1233 pr_debug("%s:%d:\n", __func__, __LINE__);
1234 }
1235
1236 core_initcall(ps3_vuart_bus_init);
1237 module_exit(ps3_vuart_bus_exit);
1238
1239 /**
1240 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1241 */
1242
ps3_vuart_port_driver_register(struct ps3_vuart_port_driver * drv)1243 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1244 {
1245 int result;
1246
1247 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1248
1249 BUG_ON(!drv->core.match_id);
1250 BUG_ON(!drv->core.core.name);
1251
1252 drv->core.probe = ps3_vuart_probe;
1253 drv->core.remove = ps3_vuart_remove;
1254 drv->core.shutdown = ps3_vuart_shutdown;
1255
1256 result = ps3_system_bus_driver_register(&drv->core);
1257 return result;
1258 }
1259 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1260
1261 /**
1262 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1263 */
1264
ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver * drv)1265 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1266 {
1267 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1268 ps3_system_bus_driver_unregister(&drv->core);
1269 }
1270 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);
1271