cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

mux.c (14938B)


      1// SPDX-License-Identifier: GPL-2.0+
      2/*
      3** mux.c:
      4**	serial driver for the Mux console found in some PA-RISC servers.
      5**
      6**	(c) Copyright 2002 Ryan Bradetich
      7**	(c) Copyright 2002 Hewlett-Packard Company
      8**
      9** This Driver currently only supports the console (port 0) on the MUX.
     10** Additional work will be needed on this driver to enable the full
     11** functionality of the MUX.
     12**
     13*/
     14
     15#include <linux/module.h>
     16#include <linux/ioport.h>
     17#include <linux/init.h>
     18#include <linux/serial.h>
     19#include <linux/tty.h>
     20#include <linux/tty_flip.h>
     21#include <linux/console.h>
     22#include <linux/delay.h> /* for udelay */
     23#include <linux/device.h>
     24#include <linux/io.h>
     25#include <asm/irq.h>
     26#include <asm/parisc-device.h>
     27
     28#include <linux/sysrq.h>
     29#include <linux/serial_core.h>
     30
     31#define MUX_OFFSET 0x800
     32#define MUX_LINE_OFFSET 0x80
     33
     34#define MUX_FIFO_SIZE 255
     35#define MUX_POLL_DELAY (30 * HZ / 1000)
     36
     37#define IO_DATA_REG_OFFSET 0x3c
     38#define IO_DCOUNT_REG_OFFSET 0x40
     39
     40#define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
     41#define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
     42#define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
     43
     44#define MUX_NR 256
     45static unsigned int port_cnt __read_mostly;
     46struct mux_port {
     47	struct uart_port port;
     48	int enabled;
     49};
     50static struct mux_port mux_ports[MUX_NR];
     51
     52static struct uart_driver mux_driver = {
     53	.owner = THIS_MODULE,
     54	.driver_name = "ttyB",
     55	.dev_name = "ttyB",
     56	.major = MUX_MAJOR,
     57	.minor = 0,
     58	.nr = MUX_NR,
     59};
     60
     61static struct timer_list mux_timer;
     62
     63#define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + IO_DATA_REG_OFFSET)
     64#define UART_GET_FIFO_CNT(p) __raw_readl((p)->membase + IO_DCOUNT_REG_OFFSET)
     65
     66/**
     67 * get_mux_port_count - Get the number of available ports on the Mux.
     68 * @dev: The parisc device.
     69 *
     70 * This function is used to determine the number of ports the Mux
     71 * supports.  The IODC data reports the number of ports the Mux
     72 * can support, but there are cases where not all the Mux ports
     73 * are connected.  This function can override the IODC and
     74 * return the true port count.
     75 */
     76static int __init get_mux_port_count(struct parisc_device *dev)
     77{
     78	int status;
     79	u8 iodc_data[32];
     80	unsigned long bytecnt;
     81
     82	/* If this is the built-in Mux for the K-Class (Eole CAP/MUX),
     83	 * we only need to allocate resources for 1 port since the
     84	 * other 7 ports are not connected.
     85	 */
     86	if(dev->id.hversion == 0x15)
     87		return 1;
     88
     89	status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32);
     90	BUG_ON(status != PDC_OK);
     91
     92	/* Return the number of ports specified in the iodc data. */
     93	return ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8;
     94}
     95
     96/**
     97 * mux_tx_empty - Check if the transmitter fifo is empty.
     98 * @port: Ptr to the uart_port.
     99 *
    100 * This function test if the transmitter fifo for the port
    101 * described by 'port' is empty.  If it is empty, this function
    102 * should return TIOCSER_TEMT, otherwise return 0.
    103 */
    104static unsigned int mux_tx_empty(struct uart_port *port)
    105{
    106	return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT;
    107} 
    108
    109/**
    110 * mux_set_mctrl - Set the current state of the modem control inputs.
    111 * @ports: Ptr to the uart_port.
    112 * @mctrl: Modem control bits.
    113 *
    114 * The Serial MUX does not support CTS, DCD or DSR so this function
    115 * is ignored.
    116 */
    117static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
    118{
    119}
    120
    121/**
    122 * mux_get_mctrl - Returns the current state of modem control inputs.
    123 * @port: Ptr to the uart_port.
    124 *
    125 * The Serial MUX does not support CTS, DCD or DSR so these lines are
    126 * treated as permanently active.
    127 */
    128static unsigned int mux_get_mctrl(struct uart_port *port)
    129{ 
    130	return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
    131}
    132
    133/**
    134 * mux_stop_tx - Stop transmitting characters.
    135 * @port: Ptr to the uart_port.
    136 *
    137 * The Serial MUX does not support this function.
    138 */
    139static void mux_stop_tx(struct uart_port *port)
    140{
    141}
    142
    143/**
    144 * mux_start_tx - Start transmitting characters.
    145 * @port: Ptr to the uart_port.
    146 *
    147 * The Serial Mux does not support this function.
    148 */
    149static void mux_start_tx(struct uart_port *port)
    150{
    151}
    152
    153/**
    154 * mux_stop_rx - Stop receiving characters.
    155 * @port: Ptr to the uart_port.
    156 *
    157 * The Serial Mux does not support this function.
    158 */
    159static void mux_stop_rx(struct uart_port *port)
    160{
    161}
    162
    163/**
    164 * mux_break_ctl - Control the transmitssion of a break signal.
    165 * @port: Ptr to the uart_port.
    166 * @break_state: Raise/Lower the break signal.
    167 *
    168 * The Serial Mux does not support this function.
    169 */
    170static void mux_break_ctl(struct uart_port *port, int break_state)
    171{
    172}
    173
    174/**
    175 * mux_write - Write chars to the mux fifo.
    176 * @port: Ptr to the uart_port.
    177 *
    178 * This function writes all the data from the uart buffer to
    179 * the mux fifo.
    180 */
    181static void mux_write(struct uart_port *port)
    182{
    183	int count;
    184	struct circ_buf *xmit = &port->state->xmit;
    185
    186	if(port->x_char) {
    187		UART_PUT_CHAR(port, port->x_char);
    188		port->icount.tx++;
    189		port->x_char = 0;
    190		return;
    191	}
    192
    193	if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
    194		mux_stop_tx(port);
    195		return;
    196	}
    197
    198	count = (port->fifosize) - UART_GET_FIFO_CNT(port);
    199	do {
    200		UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
    201		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
    202		port->icount.tx++;
    203		if(uart_circ_empty(xmit))
    204			break;
    205
    206	} while(--count > 0);
    207
    208	while(UART_GET_FIFO_CNT(port)) 
    209		udelay(1);
    210
    211	if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
    212		uart_write_wakeup(port);
    213
    214	if (uart_circ_empty(xmit))
    215		mux_stop_tx(port);
    216}
    217
    218/**
    219 * mux_read - Read chars from the mux fifo.
    220 * @port: Ptr to the uart_port.
    221 *
    222 * This reads all available data from the mux's fifo and pushes
    223 * the data to the tty layer.
    224 */
    225static void mux_read(struct uart_port *port)
    226{
    227	struct tty_port *tport = &port->state->port;
    228	int data;
    229	__u32 start_count = port->icount.rx;
    230
    231	while(1) {
    232		data = __raw_readl(port->membase + IO_DATA_REG_OFFSET);
    233
    234		if (MUX_STATUS(data))
    235			continue;
    236
    237		if (MUX_EOFIFO(data))
    238			break;
    239
    240		port->icount.rx++;
    241
    242		if (MUX_BREAK(data)) {
    243			port->icount.brk++;
    244			if(uart_handle_break(port))
    245				continue;
    246		}
    247
    248		if (uart_handle_sysrq_char(port, data & 0xffu))
    249			continue;
    250
    251		tty_insert_flip_char(tport, data & 0xFF, TTY_NORMAL);
    252	}
    253	
    254	if (start_count != port->icount.rx)
    255		tty_flip_buffer_push(tport);
    256}
    257
    258/**
    259 * mux_startup - Initialize the port.
    260 * @port: Ptr to the uart_port.
    261 *
    262 * Grab any resources needed for this port and start the
    263 * mux timer.
    264 */
    265static int mux_startup(struct uart_port *port)
    266{
    267	mux_ports[port->line].enabled = 1;
    268	return 0;
    269}
    270
    271/**
    272 * mux_shutdown - Disable the port.
    273 * @port: Ptr to the uart_port.
    274 *
    275 * Release any resources needed for the port.
    276 */
    277static void mux_shutdown(struct uart_port *port)
    278{
    279	mux_ports[port->line].enabled = 0;
    280}
    281
    282/**
    283 * mux_set_termios - Chane port parameters.
    284 * @port: Ptr to the uart_port.
    285 * @termios: new termios settings.
    286 * @old: old termios settings.
    287 *
    288 * The Serial Mux does not support this function.
    289 */
    290static void
    291mux_set_termios(struct uart_port *port, struct ktermios *termios,
    292	        struct ktermios *old)
    293{
    294}
    295
    296/**
    297 * mux_type - Describe the port.
    298 * @port: Ptr to the uart_port.
    299 *
    300 * Return a pointer to a string constant describing the
    301 * specified port.
    302 */
    303static const char *mux_type(struct uart_port *port)
    304{
    305	return "Mux";
    306}
    307
    308/**
    309 * mux_release_port - Release memory and IO regions.
    310 * @port: Ptr to the uart_port.
    311 * 
    312 * Release any memory and IO region resources currently in use by
    313 * the port.
    314 */
    315static void mux_release_port(struct uart_port *port)
    316{
    317}
    318
    319/**
    320 * mux_request_port - Request memory and IO regions.
    321 * @port: Ptr to the uart_port.
    322 *
    323 * Request any memory and IO region resources required by the port.
    324 * If any fail, no resources should be registered when this function
    325 * returns, and it should return -EBUSY on failure.
    326 */
    327static int mux_request_port(struct uart_port *port)
    328{
    329	return 0;
    330}
    331
    332/**
    333 * mux_config_port - Perform port autoconfiguration.
    334 * @port: Ptr to the uart_port.
    335 * @type: Bitmask of required configurations.
    336 *
    337 * Perform any autoconfiguration steps for the port.  This function is
    338 * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
    339 * [Note: This is required for now because of a bug in the Serial core.
    340 *  rmk has already submitted a patch to linus, should be available for
    341 *  2.5.47.]
    342 */
    343static void mux_config_port(struct uart_port *port, int type)
    344{
    345	port->type = PORT_MUX;
    346}
    347
    348/**
    349 * mux_verify_port - Verify the port information.
    350 * @port: Ptr to the uart_port.
    351 * @ser: Ptr to the serial information.
    352 *
    353 * Verify the new serial port information contained within serinfo is
    354 * suitable for this port type.
    355 */
    356static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
    357{
    358	if(port->membase == NULL)
    359		return -EINVAL;
    360
    361	return 0;
    362}
    363
    364/**
    365 * mux_drv_poll - Mux poll function.
    366 * @unused: Unused variable
    367 *
    368 * This function periodically polls the Serial MUX to check for new data.
    369 */
    370static void mux_poll(struct timer_list *unused)
    371{  
    372	int i;
    373
    374	for(i = 0; i < port_cnt; ++i) {
    375		if(!mux_ports[i].enabled)
    376			continue;
    377
    378		mux_read(&mux_ports[i].port);
    379		mux_write(&mux_ports[i].port);
    380	}
    381
    382	mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
    383}
    384
    385
    386#ifdef CONFIG_SERIAL_MUX_CONSOLE
    387static void mux_console_write(struct console *co, const char *s, unsigned count)
    388{
    389	/* Wait until the FIFO drains. */
    390	while(UART_GET_FIFO_CNT(&mux_ports[0].port))
    391		udelay(1);
    392
    393	while(count--) {
    394		if(*s == '\n') {
    395			UART_PUT_CHAR(&mux_ports[0].port, '\r');
    396		}
    397		UART_PUT_CHAR(&mux_ports[0].port, *s++);
    398	}
    399
    400}
    401
    402static int mux_console_setup(struct console *co, char *options)
    403{
    404        return 0;
    405}
    406
    407static struct console mux_console = {
    408	.name =		"ttyB",
    409	.write =	mux_console_write,
    410	.device =	uart_console_device,
    411	.setup =	mux_console_setup,
    412	.flags =	CON_ENABLED | CON_PRINTBUFFER,
    413	.index =	0,
    414	.data =		&mux_driver,
    415};
    416
    417#define MUX_CONSOLE	&mux_console
    418#else
    419#define MUX_CONSOLE	NULL
    420#endif
    421
    422static const struct uart_ops mux_pops = {
    423	.tx_empty =		mux_tx_empty,
    424	.set_mctrl =		mux_set_mctrl,
    425	.get_mctrl =		mux_get_mctrl,
    426	.stop_tx =		mux_stop_tx,
    427	.start_tx =		mux_start_tx,
    428	.stop_rx =		mux_stop_rx,
    429	.break_ctl =		mux_break_ctl,
    430	.startup =		mux_startup,
    431	.shutdown =		mux_shutdown,
    432	.set_termios =		mux_set_termios,
    433	.type =			mux_type,
    434	.release_port =		mux_release_port,
    435	.request_port =		mux_request_port,
    436	.config_port =		mux_config_port,
    437	.verify_port =		mux_verify_port,
    438};
    439
    440/**
    441 * mux_probe - Determine if the Serial Mux should claim this device.
    442 * @dev: The parisc device.
    443 *
    444 * Deterimine if the Serial Mux should claim this chip (return 0)
    445 * or not (return 1).
    446 */
    447static int __init mux_probe(struct parisc_device *dev)
    448{
    449	int i, status;
    450
    451	int port_count = get_mux_port_count(dev);
    452	printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.6\n", port_count);
    453
    454	dev_set_drvdata(&dev->dev, (void *)(long)port_count);
    455	request_mem_region(dev->hpa.start + MUX_OFFSET,
    456                           port_count * MUX_LINE_OFFSET, "Mux");
    457
    458	if(!port_cnt) {
    459		mux_driver.cons = MUX_CONSOLE;
    460
    461		status = uart_register_driver(&mux_driver);
    462		if(status) {
    463			printk(KERN_ERR "Serial mux: Unable to register driver.\n");
    464			return 1;
    465		}
    466	}
    467
    468	for(i = 0; i < port_count; ++i, ++port_cnt) {
    469		struct uart_port *port = &mux_ports[port_cnt].port;
    470		port->iobase	= 0;
    471		port->mapbase	= dev->hpa.start + MUX_OFFSET +
    472						(i * MUX_LINE_OFFSET);
    473		port->membase	= ioremap(port->mapbase, MUX_LINE_OFFSET);
    474		port->iotype	= UPIO_MEM;
    475		port->type	= PORT_MUX;
    476		port->irq	= 0;
    477		port->uartclk	= 0;
    478		port->fifosize	= MUX_FIFO_SIZE;
    479		port->ops	= &mux_pops;
    480		port->flags	= UPF_BOOT_AUTOCONF;
    481		port->line	= port_cnt;
    482		port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MUX_CONSOLE);
    483
    484		/* The port->timeout needs to match what is present in
    485		 * uart_wait_until_sent in serial_core.c.  Otherwise
    486		 * the time spent in msleep_interruptable will be very
    487		 * long, causing the appearance of a console hang.
    488		 */
    489		port->timeout   = HZ / 50;
    490		spin_lock_init(&port->lock);
    491
    492		status = uart_add_one_port(&mux_driver, port);
    493		BUG_ON(status);
    494	}
    495
    496	return 0;
    497}
    498
    499static void __exit mux_remove(struct parisc_device *dev)
    500{
    501	int i, j;
    502	int port_count = (long)dev_get_drvdata(&dev->dev);
    503
    504	/* Find Port 0 for this card in the mux_ports list. */
    505	for(i = 0; i < port_cnt; ++i) {
    506		if(mux_ports[i].port.mapbase == dev->hpa.start + MUX_OFFSET)
    507			break;
    508	}
    509	BUG_ON(i + port_count > port_cnt);
    510
    511	/* Release the resources associated with each port on the device. */
    512	for(j = 0; j < port_count; ++j, ++i) {
    513		struct uart_port *port = &mux_ports[i].port;
    514
    515		uart_remove_one_port(&mux_driver, port);
    516		if(port->membase)
    517			iounmap(port->membase);
    518	}
    519
    520	release_mem_region(dev->hpa.start + MUX_OFFSET, port_count * MUX_LINE_OFFSET);
    521}
    522
    523/* Hack.  This idea was taken from the 8250_gsc.c on how to properly order
    524 * the serial port detection in the proper order.   The idea is we always
    525 * want the builtin mux to be detected before addin mux cards, so we
    526 * specifically probe for the builtin mux cards first.
    527 *
    528 * This table only contains the parisc_device_id of known builtin mux
    529 * devices.  All other mux cards will be detected by the generic mux_tbl.
    530 */
    531static const struct parisc_device_id builtin_mux_tbl[] __initconst = {
    532	{ HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x15, 0x0000D }, /* All K-class */
    533	{ HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x44, 0x0000D }, /* E35, E45, and E55 */
    534	{ 0, }
    535};
    536
    537static const struct parisc_device_id mux_tbl[] __initconst = {
    538	{ HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
    539	{ 0, }
    540};
    541
    542MODULE_DEVICE_TABLE(parisc, builtin_mux_tbl);
    543MODULE_DEVICE_TABLE(parisc, mux_tbl);
    544
    545static struct parisc_driver builtin_serial_mux_driver __refdata = {
    546	.name =		"builtin_serial_mux",
    547	.id_table =	builtin_mux_tbl,
    548	.probe =	mux_probe,
    549	.remove =       __exit_p(mux_remove),
    550};
    551
    552static struct parisc_driver serial_mux_driver __refdata = {
    553	.name =		"serial_mux",
    554	.id_table =	mux_tbl,
    555	.probe =	mux_probe,
    556	.remove =       __exit_p(mux_remove),
    557};
    558
    559/**
    560 * mux_init - Serial MUX initialization procedure.
    561 *
    562 * Register the Serial MUX driver.
    563 */
    564static int __init mux_init(void)
    565{
    566	register_parisc_driver(&builtin_serial_mux_driver);
    567	register_parisc_driver(&serial_mux_driver);
    568
    569	if(port_cnt > 0) {
    570		/* Start the Mux timer */
    571		timer_setup(&mux_timer, mux_poll, 0);
    572		mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
    573
    574#ifdef CONFIG_SERIAL_MUX_CONSOLE
    575	        register_console(&mux_console);
    576#endif
    577	}
    578
    579	return 0;
    580}
    581
    582/**
    583 * mux_exit - Serial MUX cleanup procedure.
    584 *
    585 * Unregister the Serial MUX driver from the tty layer.
    586 */
    587static void __exit mux_exit(void)
    588{
    589	/* Delete the Mux timer. */
    590	if(port_cnt > 0) {
    591		del_timer_sync(&mux_timer);
    592#ifdef CONFIG_SERIAL_MUX_CONSOLE
    593		unregister_console(&mux_console);
    594#endif
    595	}
    596
    597	unregister_parisc_driver(&builtin_serial_mux_driver);
    598	unregister_parisc_driver(&serial_mux_driver);
    599	uart_unregister_driver(&mux_driver);
    600}
    601
    602module_init(mux_init);
    603module_exit(mux_exit);
    604
    605MODULE_AUTHOR("Ryan Bradetich");
    606MODULE_DESCRIPTION("Serial MUX driver");
    607MODULE_LICENSE("GPL");
    608MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);