1247 lines · 29.8 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* *  PS3 virtual uart * *  Copyright (C) 2006 Sony Computer Entertainment Inc. *  Copyright 2006 Sony Corp. */ #include <linux/kernel.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/interrupt.h>#include <linux/workqueue.h>#include <linux/bitops.h>#include <asm/ps3.h> #include <asm/firmware.h>#include <asm/lv1call.h> #include "vuart.h" MODULE_AUTHOR("Sony Corporation");MODULE_LICENSE("GPL v2");MODULE_DESCRIPTION("PS3 vuart"); /** * vuart - An inter-partition data link service. *  port 0: PS3 AV Settings. *  port 2: PS3 System Manager. * * The vuart provides a bi-directional byte stream data link between logical * partitions.  Its primary role is as a communications link between the guest * OS and the system policy module.  The current HV does not support any * connections other than those listed. */ enum {PORT_COUNT = 3,}; enum vuart_param {	PARAM_TX_TRIGGER = 0,	PARAM_RX_TRIGGER = 1,	PARAM_INTERRUPT_MASK = 2,	PARAM_RX_BUF_SIZE = 3, /* read only */	PARAM_RX_BYTES = 4, /* read only */	PARAM_TX_BUF_SIZE = 5, /* read only */	PARAM_TX_BYTES = 6, /* read only */	PARAM_INTERRUPT_STATUS = 7, /* read only */}; enum vuart_interrupt_bit {	INTERRUPT_BIT_TX = 0,	INTERRUPT_BIT_RX = 1,	INTERRUPT_BIT_DISCONNECT = 2,}; enum vuart_interrupt_mask {	INTERRUPT_MASK_TX = 1,	INTERRUPT_MASK_RX = 2,	INTERRUPT_MASK_DISCONNECT = 4,}; /** * struct ps3_vuart_port_priv - private vuart device data. */ struct ps3_vuart_port_priv {	u64 interrupt_mask; 	struct {		spinlock_t lock;		struct list_head head;	} tx_list;	struct {		struct ps3_vuart_work work;		unsigned long bytes_held;		spinlock_t lock;		struct list_head head;	} rx_list;	struct ps3_vuart_stats stats;}; static struct ps3_vuart_port_priv *to_port_priv(	struct ps3_system_bus_device *dev){	BUG_ON(!dev);	BUG_ON(!dev->driver_priv);	return (struct ps3_vuart_port_priv *)dev->driver_priv;} /** * struct ports_bmp - bitmap indicating ports needing service. * * A 256 bit read only bitmap indicating ports needing service.  Do not write * to these bits.  Must not cross a page boundary. */ struct ports_bmp {	u64 status;	u64 unused[3];} __attribute__((aligned(32))); #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)static void __maybe_unused _dump_ports_bmp(	const struct ports_bmp *bmp, const char *func, int line){	pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);} #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)static void __maybe_unused _dump_port_params(unsigned int port_number,	const char *func, int line){#if defined(DEBUG)	static const char *strings[] = {		"tx_trigger      ",		"rx_trigger      ",		"interrupt_mask  ",		"rx_buf_size     ",		"rx_bytes        ",		"tx_buf_size     ",		"tx_bytes        ",		"interrupt_status",	};	int result;	unsigned int i;	u64 value; 	for (i = 0; i < ARRAY_SIZE(strings); i++) {		result = lv1_get_virtual_uart_param(port_number, i, &value); 		if (result) {			pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,				port_number, strings[i], ps3_result(result));			continue;		}		pr_debug("%s:%d: port_%u: %s = %lxh\n",			func, line, port_number, strings[i], value);	}#endif} int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,	struct vuart_triggers *trig){	int result;	u64 size;	u64 val;	u64 tx; 	result = lv1_get_virtual_uart_param(dev->port_number,		PARAM_TX_TRIGGER, &tx);	trig->tx = tx; 	if (result) {		dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",			__func__, __LINE__, ps3_result(result));		return result;	} 	result = lv1_get_virtual_uart_param(dev->port_number,		PARAM_RX_BUF_SIZE, &size); 	if (result) {		dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",			__func__, __LINE__, ps3_result(result));		return result;	} 	result = lv1_get_virtual_uart_param(dev->port_number,		PARAM_RX_TRIGGER, &val); 	if (result) {		dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",			__func__, __LINE__, ps3_result(result));		return result;	} 	trig->rx = size - val; 	dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,		trig->tx, trig->rx); 	return result;} int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,	unsigned int rx){	int result;	u64 size; 	result = lv1_set_virtual_uart_param(dev->port_number,		PARAM_TX_TRIGGER, tx); 	if (result) {		dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",			__func__, __LINE__, ps3_result(result));		return result;	} 	result = lv1_get_virtual_uart_param(dev->port_number,		PARAM_RX_BUF_SIZE, &size); 	if (result) {		dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",			__func__, __LINE__, ps3_result(result));		return result;	} 	result = lv1_set_virtual_uart_param(dev->port_number,		PARAM_RX_TRIGGER, size - rx); 	if (result) {		dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",			__func__, __LINE__, ps3_result(result));		return result;	} 	dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,		tx, rx); 	return result;} static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,	u64 *bytes_waiting){	int result; 	result = lv1_get_virtual_uart_param(dev->port_number,		PARAM_RX_BYTES, bytes_waiting); 	if (result)		dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",			__func__, __LINE__, ps3_result(result)); 	dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,		*bytes_waiting);	return result;} /** * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources. * @dev: The struct ps3_system_bus_device instance. * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables. */ static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,	unsigned long mask){	int result;	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask); 	priv->interrupt_mask = mask; 	result = lv1_set_virtual_uart_param(dev->port_number,		PARAM_INTERRUPT_MASK, priv->interrupt_mask); 	if (result)		dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",			__func__, __LINE__, ps3_result(result)); 	return result;} static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,	unsigned long *status){	int result;	struct ps3_vuart_port_priv *priv = to_port_priv(dev);	u64 tmp; 	result = lv1_get_virtual_uart_param(dev->port_number,		PARAM_INTERRUPT_STATUS, &tmp); 	if (result)		dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",			__func__, __LINE__, ps3_result(result)); 	*status = tmp & priv->interrupt_mask; 	dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",		__func__, __LINE__, priv->interrupt_mask, tmp, *status); 	return result;} int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev){	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0		: ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask		| INTERRUPT_MASK_TX);} int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev){	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0		: ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask		| INTERRUPT_MASK_RX);} int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev){	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0		: ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask		| INTERRUPT_MASK_DISCONNECT);} int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev){	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	return (priv->interrupt_mask & INTERRUPT_MASK_TX)		? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask		& ~INTERRUPT_MASK_TX) : 0;} int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev){	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	return (priv->interrupt_mask & INTERRUPT_MASK_RX)		? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask		& ~INTERRUPT_MASK_RX) : 0;} int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev){	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)		? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask		& ~INTERRUPT_MASK_DISCONNECT) : 0;} /** * ps3_vuart_raw_write - Low level write helper. * @dev: The struct ps3_system_bus_device instance. * * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write. */ static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,	const void *buf, unsigned int bytes, u64 *bytes_written){	int result;	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	result = lv1_write_virtual_uart(dev->port_number,		ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written); 	if (result) {		dev_warn(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "			"%s\n", __func__, __LINE__, ps3_result(result));		return result;	} 	priv->stats.bytes_written += *bytes_written; 	dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,		*bytes_written, bytes, priv->stats.bytes_written); 	return result;} /** * ps3_vuart_raw_read - Low level read helper. * @dev: The struct ps3_system_bus_device instance. * * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read. */ static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,	unsigned int bytes, u64 *bytes_read){	int result;	struct ps3_vuart_port_priv *priv = to_port_priv(dev); 	dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes); 	result = lv1_read_virtual_uart(dev->port_number,		ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read); 	if (result) {		dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",			__func__, __LINE__, ps3_result(result));		return result;	} 	priv->stats.bytes_read += *bytes_read; 	dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,		*bytes_read, bytes, priv->stats.bytes_read); 	return result;} /** * ps3_vuart_clear_rx_bytes - Discard bytes received. * @dev: The struct ps3_system_bus_device instance. * @bytes: Max byte count to discard, zero = all pending. * * Used to clear pending rx interrupt source.  Will not block. */ void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,	unsigned int bytes){	int result;	struct ps3_vuart_port_priv *priv = to_port_priv(dev);	u64 bytes_waiting;	void *tmp; 	result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting); 	BUG_ON(result); 	bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting; 	dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes); 	if (!bytes)		return; 	/* Add some extra space for recently arrived data. */ 	bytes += 128; 	tmp = kmalloc(bytes, GFP_KERNEL); 	if (!tmp)		return; 	ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting); 	kfree(tmp); 	/* Don't include these bytes in the stats. */ 	priv->stats.bytes_read -= bytes_waiting;}EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes); /** * struct list_buffer - An element for a port device fifo buffer list. */ struct list_buffer {	struct list_head link;	const unsigned char *head;	const unsigned char *tail;	unsigned long dbg_number;	unsigned char data[];}; /** * ps3_vuart_write - the entry point for writing data to a port * @dev: The struct ps3_system_bus_device instance. * * If the port is idle on entry as much of the incoming data is written to * the port as the port will accept.  Otherwise a list buffer is created * and any remaining incoming data is copied to that buffer.  The buffer is * then enqueued for transmission via the transmit interrupt. */ int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,	unsigned int bytes){	static unsigned long dbg_number;	int result;	struct ps3_vuart_port_priv *priv = to_port_priv(dev);	unsigned long flags;	struct list_buffer *lb; 	dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,		bytes, bytes); 	spin_lock_irqsave(&priv->tx_list.lock, flags); 	if (list_empty(&priv->tx_list.head)) {		u64 bytes_written; 		result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written); 		spin_unlock_irqrestore(&priv->tx_list.lock, flags); 		if (result) {			dev_dbg(&dev->core,				"%s:%d: ps3_vuart_raw_write failed\n",				__func__, __LINE__);			return result;		}

Lines 1–500 of 1247.