Linux Audio

Check our new training course

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