2004-03-14 13:20:30 +01:00
|
|
|
/*
|
2008-08-11 16:17:04 +02:00
|
|
|
* QEMU 16550A UART emulation
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-03-14 13:20:30 +01:00
|
|
|
* Copyright (c) 2003-2004 Fabrice Bellard
|
2008-08-11 16:17:04 +02:00
|
|
|
* Copyright (c) 2008 Citrix Systems, Inc.
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-03-14 13:20:30 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2012-10-17 09:54:19 +02:00
|
|
|
|
2016-01-26 19:17:03 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/char/serial.h"
|
2019-08-12 07:23:42 +02:00
|
|
|
#include "hw/irq.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2017-01-26 14:33:39 +01:00
|
|
|
#include "chardev/char-serial.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2019-08-12 07:23:38 +02:00
|
|
|
#include "sysemu/reset.h"
|
2019-08-12 07:23:59 +02:00
|
|
|
#include "sysemu/runstate.h"
|
2013-08-05 21:40:44 +02:00
|
|
|
#include "qemu/error-report.h"
|
2018-06-21 19:12:49 +02:00
|
|
|
#include "trace.h"
|
2004-03-14 13:20:30 +01:00
|
|
|
|
|
|
|
//#define DEBUG_SERIAL
|
|
|
|
|
|
|
|
#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
|
|
|
|
|
|
|
|
#define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
|
|
|
|
#define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
|
|
|
|
#define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
|
|
|
|
#define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
|
|
|
|
|
|
|
|
#define UART_IIR_NO_INT 0x01 /* No interrupts pending */
|
|
|
|
#define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
|
|
|
|
|
|
|
|
#define UART_IIR_MSI 0x00 /* Modem status interrupt */
|
|
|
|
#define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
|
|
|
|
#define UART_IIR_RDI 0x04 /* Receiver data interrupt */
|
|
|
|
#define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
|
2008-08-11 16:17:04 +02:00
|
|
|
#define UART_IIR_CTI 0x0C /* Character Timeout Indication */
|
|
|
|
|
|
|
|
#define UART_IIR_FENF 0x80 /* Fifo enabled, but not functionning */
|
|
|
|
#define UART_IIR_FE 0xC0 /* Fifo enabled */
|
2004-03-14 13:20:30 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the definitions for the Modem Control Register
|
|
|
|
*/
|
|
|
|
#define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
|
|
|
|
#define UART_MCR_OUT2 0x08 /* Out2 complement */
|
|
|
|
#define UART_MCR_OUT1 0x04 /* Out1 complement */
|
|
|
|
#define UART_MCR_RTS 0x02 /* RTS complement */
|
|
|
|
#define UART_MCR_DTR 0x01 /* DTR complement */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the definitions for the Modem Status Register
|
|
|
|
*/
|
|
|
|
#define UART_MSR_DCD 0x80 /* Data Carrier Detect */
|
|
|
|
#define UART_MSR_RI 0x40 /* Ring Indicator */
|
|
|
|
#define UART_MSR_DSR 0x20 /* Data Set Ready */
|
|
|
|
#define UART_MSR_CTS 0x10 /* Clear to Send */
|
|
|
|
#define UART_MSR_DDCD 0x08 /* Delta DCD */
|
|
|
|
#define UART_MSR_TERI 0x04 /* Trailing edge ring indicator */
|
|
|
|
#define UART_MSR_DDSR 0x02 /* Delta DSR */
|
|
|
|
#define UART_MSR_DCTS 0x01 /* Delta CTS */
|
|
|
|
#define UART_MSR_ANY_DELTA 0x0F /* Any of the delta bits! */
|
|
|
|
|
|
|
|
#define UART_LSR_TEMT 0x40 /* Transmitter empty */
|
|
|
|
#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
|
|
|
|
#define UART_LSR_BI 0x10 /* Break interrupt indicator */
|
|
|
|
#define UART_LSR_FE 0x08 /* Frame error indicator */
|
|
|
|
#define UART_LSR_PE 0x04 /* Parity error indicator */
|
|
|
|
#define UART_LSR_OE 0x02 /* Overrun error indicator */
|
|
|
|
#define UART_LSR_DR 0x01 /* Receiver data ready */
|
2008-08-11 16:17:04 +02:00
|
|
|
#define UART_LSR_INT_ANY 0x1E /* Any of the lsr-interrupt-triggering status bits */
|
2004-03-14 13:20:30 +01:00
|
|
|
|
2008-08-11 16:17:04 +02:00
|
|
|
/* Interrupt trigger levels. The byte-counts are for 16550A - in newer UARTs the byte-count for each ITL is higher. */
|
|
|
|
|
|
|
|
#define UART_FCR_ITL_1 0x00 /* 1 byte ITL */
|
|
|
|
#define UART_FCR_ITL_2 0x40 /* 4 bytes ITL */
|
|
|
|
#define UART_FCR_ITL_3 0x80 /* 8 bytes ITL */
|
|
|
|
#define UART_FCR_ITL_4 0xC0 /* 14 bytes ITL */
|
|
|
|
|
|
|
|
#define UART_FCR_DMS 0x08 /* DMA Mode Select */
|
|
|
|
#define UART_FCR_XFR 0x04 /* XMIT Fifo Reset */
|
|
|
|
#define UART_FCR_RFR 0x02 /* RCVR Fifo Reset */
|
|
|
|
#define UART_FCR_FE 0x01 /* FIFO Enable */
|
|
|
|
|
|
|
|
#define MAX_XMIT_RETRY 4
|
|
|
|
|
2010-09-13 14:32:32 +02:00
|
|
|
#ifdef DEBUG_SERIAL
|
|
|
|
#define DPRINTF(fmt, ...) \
|
2010-09-13 21:21:57 +02:00
|
|
|
do { fprintf(stderr, "serial: " fmt , ## __VA_ARGS__); } while (0)
|
2010-09-13 14:32:32 +02:00
|
|
|
#else
|
|
|
|
#define DPRINTF(fmt, ...) \
|
2010-09-13 21:21:57 +02:00
|
|
|
do {} while (0)
|
2010-09-13 14:32:32 +02:00
|
|
|
#endif
|
|
|
|
|
2008-08-11 16:17:04 +02:00
|
|
|
static void serial_receive1(void *opaque, const uint8_t *buf, int size);
|
2016-06-20 15:08:20 +02:00
|
|
|
static void serial_xmit(SerialState *s);
|
2008-02-10 14:40:52 +01:00
|
|
|
|
2013-06-03 07:13:27 +02:00
|
|
|
static inline void recv_fifo_put(SerialState *s, uint8_t chr)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2010-02-10 22:35:54 +01:00
|
|
|
/* Receive overruns do not overwrite FIFO contents. */
|
2013-06-03 07:13:27 +02:00
|
|
|
if (!fifo8_is_full(&s->recv_fifo)) {
|
|
|
|
fifo8_push(&s->recv_fifo, chr);
|
|
|
|
} else {
|
2010-02-10 22:35:54 +01:00
|
|
|
s->lsr |= UART_LSR_OE;
|
2013-06-03 07:13:27 +02:00
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
2008-05-04 23:42:00 +02:00
|
|
|
|
2008-08-11 16:17:04 +02:00
|
|
|
static void serial_update_irq(SerialState *s)
|
|
|
|
{
|
|
|
|
uint8_t tmp_iir = UART_IIR_NO_INT;
|
|
|
|
|
|
|
|
if ((s->ier & UART_IER_RLSI) && (s->lsr & UART_LSR_INT_ANY)) {
|
|
|
|
tmp_iir = UART_IIR_RLSI;
|
2008-09-17 02:21:05 +02:00
|
|
|
} else if ((s->ier & UART_IER_RDI) && s->timeout_ipending) {
|
2008-09-20 03:15:04 +02:00
|
|
|
/* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
|
|
|
|
* this is not in the specification but is observed on existing
|
|
|
|
* hardware. */
|
2008-08-11 16:17:04 +02:00
|
|
|
tmp_iir = UART_IIR_CTI;
|
2009-09-12 18:52:22 +02:00
|
|
|
} else if ((s->ier & UART_IER_RDI) && (s->lsr & UART_LSR_DR) &&
|
|
|
|
(!(s->fcr & UART_FCR_FE) ||
|
2013-06-03 07:13:27 +02:00
|
|
|
s->recv_fifo.num >= s->recv_fifo_itl)) {
|
2009-09-12 18:52:22 +02:00
|
|
|
tmp_iir = UART_IIR_RDI;
|
2008-08-11 16:17:04 +02:00
|
|
|
} else if ((s->ier & UART_IER_THRI) && s->thr_ipending) {
|
|
|
|
tmp_iir = UART_IIR_THRI;
|
|
|
|
} else if ((s->ier & UART_IER_MSI) && (s->msr & UART_MSR_ANY_DELTA)) {
|
|
|
|
tmp_iir = UART_IIR_MSI;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->iir = tmp_iir | (s->iir & 0xF0);
|
|
|
|
|
|
|
|
if (tmp_iir != UART_IIR_NO_INT) {
|
|
|
|
qemu_irq_raise(s->irq);
|
|
|
|
} else {
|
|
|
|
qemu_irq_lower(s->irq);
|
2008-05-04 23:42:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-08 23:30:36 +01:00
|
|
|
static void serial_update_parameters(SerialState *s)
|
|
|
|
{
|
2018-05-12 02:05:44 +02:00
|
|
|
float speed;
|
|
|
|
int parity, data_bits, stop_bits, frame_size;
|
2005-11-11 00:58:33 +01:00
|
|
|
QEMUSerialSetParams ssp;
|
2005-11-08 23:30:36 +01:00
|
|
|
|
2009-10-26 21:51:41 +01:00
|
|
|
/* Start bit. */
|
2008-08-11 16:17:04 +02:00
|
|
|
frame_size = 1;
|
2005-11-08 23:30:36 +01:00
|
|
|
if (s->lcr & 0x08) {
|
2009-10-26 21:51:41 +01:00
|
|
|
/* Parity bit. */
|
|
|
|
frame_size++;
|
2005-11-08 23:30:36 +01:00
|
|
|
if (s->lcr & 0x10)
|
|
|
|
parity = 'E';
|
|
|
|
else
|
|
|
|
parity = 'O';
|
|
|
|
} else {
|
|
|
|
parity = 'N';
|
|
|
|
}
|
2018-05-12 02:05:44 +02:00
|
|
|
if (s->lcr & 0x04) {
|
2005-11-08 23:30:36 +01:00
|
|
|
stop_bits = 2;
|
2018-05-12 02:05:44 +02:00
|
|
|
} else {
|
2005-11-08 23:30:36 +01:00
|
|
|
stop_bits = 1;
|
2018-05-12 02:05:44 +02:00
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2005-11-08 23:30:36 +01:00
|
|
|
data_bits = (s->lcr & 0x03) + 5;
|
2008-08-11 16:17:04 +02:00
|
|
|
frame_size += data_bits + stop_bits;
|
2018-05-12 02:05:44 +02:00
|
|
|
/* Zero divisor should give about 3500 baud */
|
|
|
|
speed = (s->divider == 0) ? 3500 : (float) s->baudbase / s->divider;
|
2005-11-11 00:58:33 +01:00
|
|
|
ssp.speed = speed;
|
|
|
|
ssp.parity = parity;
|
|
|
|
ssp.data_bits = data_bits;
|
|
|
|
ssp.stop_bits = stop_bits;
|
2016-03-21 17:02:30 +01:00
|
|
|
s->char_transmit_time = (NANOSECONDS_PER_SECOND / speed) * frame_size;
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
|
2010-09-13 14:32:32 +02:00
|
|
|
|
2018-05-12 02:05:44 +02:00
|
|
|
DPRINTF("speed=%.2f parity=%c data=%d stop=%d\n",
|
2005-11-08 23:30:36 +01:00
|
|
|
speed, parity, data_bits, stop_bits);
|
|
|
|
}
|
|
|
|
|
2008-08-11 16:17:04 +02:00
|
|
|
static void serial_update_msl(SerialState *s)
|
|
|
|
{
|
|
|
|
uint8_t omsr;
|
|
|
|
int flags;
|
|
|
|
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->modem_status_poll);
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2016-10-22 11:52:55 +02:00
|
|
|
if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_GET_TIOCM,
|
2016-10-22 11:52:51 +02:00
|
|
|
&flags) == -ENOTSUP) {
|
2008-08-11 16:17:04 +02:00
|
|
|
s->poll_msl = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
omsr = s->msr;
|
|
|
|
|
|
|
|
s->msr = (flags & CHR_TIOCM_CTS) ? s->msr | UART_MSR_CTS : s->msr & ~UART_MSR_CTS;
|
|
|
|
s->msr = (flags & CHR_TIOCM_DSR) ? s->msr | UART_MSR_DSR : s->msr & ~UART_MSR_DSR;
|
|
|
|
s->msr = (flags & CHR_TIOCM_CAR) ? s->msr | UART_MSR_DCD : s->msr & ~UART_MSR_DCD;
|
|
|
|
s->msr = (flags & CHR_TIOCM_RI) ? s->msr | UART_MSR_RI : s->msr & ~UART_MSR_RI;
|
|
|
|
|
|
|
|
if (s->msr != omsr) {
|
|
|
|
/* Set delta bits */
|
|
|
|
s->msr = s->msr | ((s->msr >> 4) ^ (omsr >> 4));
|
|
|
|
/* UART_MSR_TERI only if change was from 1 -> 0 */
|
|
|
|
if ((s->msr & UART_MSR_TERI) && !(omsr & UART_MSR_RI))
|
|
|
|
s->msr &= ~UART_MSR_TERI;
|
|
|
|
serial_update_irq(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The real 16550A apparently has a 250ns response latency to line status changes.
|
|
|
|
We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */
|
|
|
|
|
2016-03-21 17:02:30 +01:00
|
|
|
if (s->poll_msl) {
|
|
|
|
timer_mod(s->modem_status_poll, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
|
|
|
|
NANOSECONDS_PER_SECOND / 100);
|
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 15:08:20 +02:00
|
|
|
static gboolean serial_watch_cb(GIOChannel *chan, GIOCondition cond,
|
|
|
|
void *opaque)
|
2008-08-11 16:17:04 +02:00
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
2016-06-14 14:35:20 +02:00
|
|
|
s->watch_tag = 0;
|
2016-06-20 15:08:20 +02:00
|
|
|
serial_xmit(s);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2016-06-20 15:08:20 +02:00
|
|
|
static void serial_xmit(SerialState *s)
|
|
|
|
{
|
2014-07-11 11:41:08 +02:00
|
|
|
do {
|
2014-12-11 17:01:39 +01:00
|
|
|
assert(!(s->lsr & UART_LSR_TEMT));
|
2016-06-14 14:17:16 +02:00
|
|
|
if (s->tsr_retry == 0) {
|
2014-12-11 17:01:39 +01:00
|
|
|
assert(!(s->lsr & UART_LSR_THRE));
|
|
|
|
|
2014-07-11 11:41:08 +02:00
|
|
|
if (s->fcr & UART_FCR_FE) {
|
2014-12-11 17:01:39 +01:00
|
|
|
assert(!fifo8_is_empty(&s->xmit_fifo));
|
2014-07-11 11:41:08 +02:00
|
|
|
s->tsr = fifo8_pop(&s->xmit_fifo);
|
|
|
|
if (!s->xmit_fifo.num) {
|
|
|
|
s->lsr |= UART_LSR_THRE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s->tsr = s->thr;
|
2008-08-11 16:17:04 +02:00
|
|
|
s->lsr |= UART_LSR_THRE;
|
2014-12-11 17:01:39 +01:00
|
|
|
}
|
|
|
|
if ((s->lsr & UART_LSR_THRE) && !s->thr_ipending) {
|
|
|
|
s->thr_ipending = 1;
|
|
|
|
serial_update_irq(s);
|
2013-06-03 07:12:09 +02:00
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 11:41:08 +02:00
|
|
|
if (s->mcr & UART_MCR_LOOP) {
|
|
|
|
/* in loopback mode, say that we just received a char */
|
|
|
|
serial_receive1(s, &s->tsr, 1);
|
2018-07-16 13:07:55 +02:00
|
|
|
} else {
|
|
|
|
int rc = qemu_chr_fe_write(&s->chr, &s->tsr, 1);
|
|
|
|
|
|
|
|
if ((rc == 0 ||
|
|
|
|
(rc == -1 && errno == EAGAIN)) &&
|
|
|
|
s->tsr_retry < MAX_XMIT_RETRY) {
|
|
|
|
assert(s->watch_tag == 0);
|
|
|
|
s->watch_tag =
|
|
|
|
qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
|
|
|
|
serial_watch_cb, s);
|
|
|
|
if (s->watch_tag > 0) {
|
|
|
|
s->tsr_retry++;
|
|
|
|
return;
|
|
|
|
}
|
2014-07-11 11:41:08 +02:00
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
2016-06-14 14:20:50 +02:00
|
|
|
s->tsr_retry = 0;
|
2014-12-11 17:01:39 +01:00
|
|
|
|
2014-07-11 11:41:08 +02:00
|
|
|
/* Transmit another byte if it is already available. It is only
|
|
|
|
possible when FIFO is enabled and not empty. */
|
2014-12-11 17:01:39 +01:00
|
|
|
} while (!(s->lsr & UART_LSR_THRE));
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2013-08-21 17:03:08 +02:00
|
|
|
s->last_xmit_ts = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2014-12-11 17:01:39 +01:00
|
|
|
s->lsr |= UART_LSR_TEMT;
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:52 +02:00
|
|
|
/* Setter for FCR.
|
|
|
|
is_load flag means, that value is set while loading VM state
|
|
|
|
and interrupt should not be invoked */
|
|
|
|
static void serial_write_fcr(SerialState *s, uint8_t val)
|
|
|
|
{
|
|
|
|
/* Set fcr - val only has the bits that are supposed to "stick" */
|
|
|
|
s->fcr = val;
|
|
|
|
|
|
|
|
if (val & UART_FCR_FE) {
|
|
|
|
s->iir |= UART_IIR_FE;
|
|
|
|
/* Set recv_fifo trigger Level */
|
|
|
|
switch (val & 0xC0) {
|
|
|
|
case UART_FCR_ITL_1:
|
|
|
|
s->recv_fifo_itl = 1;
|
|
|
|
break;
|
|
|
|
case UART_FCR_ITL_2:
|
|
|
|
s->recv_fifo_itl = 4;
|
|
|
|
break;
|
|
|
|
case UART_FCR_ITL_3:
|
|
|
|
s->recv_fifo_itl = 8;
|
|
|
|
break;
|
|
|
|
case UART_FCR_ITL_4:
|
|
|
|
s->recv_fifo_itl = 14;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s->iir &= ~UART_IIR_FE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:08:59 +02:00
|
|
|
static void serial_update_tiocm(SerialState *s)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_GET_TIOCM, &flags);
|
|
|
|
|
|
|
|
flags &= ~(CHR_TIOCM_RTS | CHR_TIOCM_DTR);
|
|
|
|
|
|
|
|
if (s->mcr & UART_MCR_RTS) {
|
|
|
|
flags |= CHR_TIOCM_RTS;
|
|
|
|
}
|
|
|
|
if (s->mcr & UART_MCR_DTR) {
|
|
|
|
flags |= CHR_TIOCM_DTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_TIOCM, &flags);
|
|
|
|
}
|
|
|
|
|
2012-10-08 13:40:29 +02:00
|
|
|
static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned size)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2004-03-14 22:46:48 +01:00
|
|
|
SerialState *s = opaque;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-03-14 13:20:30 +01:00
|
|
|
addr &= 7;
|
2018-06-21 19:12:49 +02:00
|
|
|
trace_serial_ioport_write(addr, val);
|
2004-03-14 13:20:30 +01:00
|
|
|
switch(addr) {
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
if (s->lcr & UART_LCR_DLAB) {
|
2018-09-11 15:16:58 +02:00
|
|
|
if (size == 1) {
|
2018-05-12 02:05:44 +02:00
|
|
|
s->divider = (s->divider & 0xff00) | val;
|
2018-09-11 15:16:58 +02:00
|
|
|
} else {
|
2018-05-12 02:05:44 +02:00
|
|
|
s->divider = val;
|
|
|
|
}
|
2005-11-08 23:30:36 +01:00
|
|
|
serial_update_parameters(s);
|
2004-03-14 13:20:30 +01:00
|
|
|
} else {
|
2008-08-11 16:17:04 +02:00
|
|
|
s->thr = (uint8_t) val;
|
|
|
|
if(s->fcr & UART_FCR_FE) {
|
2013-06-03 07:13:27 +02:00
|
|
|
/* xmit overruns overwrite data, so make space if needed */
|
|
|
|
if (fifo8_is_full(&s->xmit_fifo)) {
|
|
|
|
fifo8_pop(&s->xmit_fifo);
|
|
|
|
}
|
|
|
|
fifo8_push(&s->xmit_fifo, s->thr);
|
2008-05-04 23:42:00 +02:00
|
|
|
}
|
2013-06-03 07:14:48 +02:00
|
|
|
s->thr_ipending = 0;
|
|
|
|
s->lsr &= ~UART_LSR_THRE;
|
2014-12-11 17:01:39 +01:00
|
|
|
s->lsr &= ~UART_LSR_TEMT;
|
2013-06-03 07:14:48 +02:00
|
|
|
serial_update_irq(s);
|
2016-06-14 14:17:16 +02:00
|
|
|
if (s->tsr_retry == 0) {
|
2016-06-20 15:08:20 +02:00
|
|
|
serial_xmit(s);
|
2014-07-11 11:41:08 +02:00
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (s->lcr & UART_LCR_DLAB) {
|
|
|
|
s->divider = (s->divider & 0x00ff) | (val << 8);
|
2005-11-08 23:30:36 +01:00
|
|
|
serial_update_parameters(s);
|
2004-03-14 13:20:30 +01:00
|
|
|
} else {
|
2014-12-12 11:54:42 +01:00
|
|
|
uint8_t changed = (s->ier ^ val) & 0x0f;
|
2004-08-24 23:55:28 +02:00
|
|
|
s->ier = val & 0x0f;
|
2008-08-11 16:17:04 +02:00
|
|
|
/* If the backend device is a real serial port, turn polling of the modem
|
2014-12-12 11:54:42 +01:00
|
|
|
* status lines on physical port on or off depending on UART_IER_MSI state.
|
|
|
|
*/
|
|
|
|
if ((changed & UART_IER_MSI) && s->poll_msl >= 0) {
|
2008-08-11 16:17:04 +02:00
|
|
|
if (s->ier & UART_IER_MSI) {
|
|
|
|
s->poll_msl = 1;
|
|
|
|
serial_update_msl(s);
|
|
|
|
} else {
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->modem_status_poll);
|
2008-08-11 16:17:04 +02:00
|
|
|
s->poll_msl = 0;
|
|
|
|
}
|
|
|
|
}
|
serial: reset thri_pending on IER writes with THRI=0
This is responsible for failure of migration from 2.2 to 2.1, because
thr_ipending is always one in practice.
serial.c is setting thr_ipending unconditionally. However, thr_ipending
is not used at all if THRI=0, and it will be overwritten again the next
time THRE or THRI changes. For that reason, we can set thr_ipending to
zero every time THRI is reset.
There is disagreement on whether LSR.THRE should be resampled when IER.THRI
goes from 1 to 1. This patch does not touch the code, leaving that for
QEMU 2.3+.
This has no semantic change and is enough to fix migration in the common
case where the interrupt is not pending or is reported in IIR. It does not
change the migration format, so 2.2.0 -> 2.1 will remain broken but we
can fix 2.2.1 -> 2.1 without breaking 2.2.1 <-> 2.2.0.
The case that remains broken (the one in which the subsection is strictly
necessary) is when THRE=1, the THRI interrupt has *not* been acknowledged
yet, and a higher-priority interrupt comes. In this case, you need the
subsection to tell the source that the lower-priority THRI interrupt is
pending. The subsection's breakage of migration, in this case, prevents
continuing the VM on the destination with an invalid state.
Cc: qemu-stable@nongnu.org
Reported-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2014-12-12 10:17:08 +01:00
|
|
|
|
|
|
|
/* Turning on the THRE interrupt on IER can trigger the interrupt
|
|
|
|
* if LSR.THRE=1, even if it had been masked before by reading IIR.
|
|
|
|
* This is not in the datasheet, but Windows relies on it. It is
|
|
|
|
* unclear if THRE has to be resampled every time THRI becomes
|
|
|
|
* 1, or only on the rising edge. Bochs does the latter, and Windows
|
2014-12-12 11:54:42 +01:00
|
|
|
* always toggles IER to all zeroes and back to all ones, so do the
|
|
|
|
* same.
|
serial: reset thri_pending on IER writes with THRI=0
This is responsible for failure of migration from 2.2 to 2.1, because
thr_ipending is always one in practice.
serial.c is setting thr_ipending unconditionally. However, thr_ipending
is not used at all if THRI=0, and it will be overwritten again the next
time THRE or THRI changes. For that reason, we can set thr_ipending to
zero every time THRI is reset.
There is disagreement on whether LSR.THRE should be resampled when IER.THRI
goes from 1 to 1. This patch does not touch the code, leaving that for
QEMU 2.3+.
This has no semantic change and is enough to fix migration in the common
case where the interrupt is not pending or is reported in IIR. It does not
change the migration format, so 2.2.0 -> 2.1 will remain broken but we
can fix 2.2.1 -> 2.1 without breaking 2.2.1 <-> 2.2.0.
The case that remains broken (the one in which the subsection is strictly
necessary) is when THRE=1, the THRI interrupt has *not* been acknowledged
yet, and a higher-priority interrupt comes. In this case, you need the
subsection to tell the source that the lower-priority THRI interrupt is
pending. The subsection's breakage of migration, in this case, prevents
continuing the VM on the destination with an invalid state.
Cc: qemu-stable@nongnu.org
Reported-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2014-12-12 10:17:08 +01:00
|
|
|
*
|
|
|
|
* If IER.THRI is zero, thr_ipending is not used. Set it to zero
|
|
|
|
* so that the thr_ipending subsection is not migrated.
|
|
|
|
*/
|
2014-12-12 11:54:42 +01:00
|
|
|
if (changed & UART_IER_THRI) {
|
|
|
|
if ((s->ier & UART_IER_THRI) && (s->lsr & UART_LSR_THRE)) {
|
|
|
|
s->thr_ipending = 1;
|
|
|
|
} else {
|
|
|
|
s->thr_ipending = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
serial_update_irq(s);
|
2004-08-24 23:55:28 +02:00
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2008-08-11 16:17:04 +02:00
|
|
|
/* Did the enable/disable flag change? If so, make sure FIFOs get flushed */
|
2014-08-28 13:18:52 +02:00
|
|
|
if ((val ^ s->fcr) & UART_FCR_FE) {
|
2008-08-11 16:17:04 +02:00
|
|
|
val |= UART_FCR_XFR | UART_FCR_RFR;
|
2014-08-28 13:18:52 +02:00
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
|
|
|
|
/* FIFO clear */
|
|
|
|
|
|
|
|
if (val & UART_FCR_RFR) {
|
2014-12-11 19:08:14 +01:00
|
|
|
s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->fifo_timeout_timer);
|
2014-08-28 13:18:52 +02:00
|
|
|
s->timeout_ipending = 0;
|
2013-06-03 07:13:27 +02:00
|
|
|
fifo8_reset(&s->recv_fifo);
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (val & UART_FCR_XFR) {
|
2014-12-11 19:08:14 +01:00
|
|
|
s->lsr |= UART_LSR_THRE;
|
|
|
|
s->thr_ipending = 1;
|
2013-06-03 07:13:27 +02:00
|
|
|
fifo8_reset(&s->xmit_fifo);
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:52 +02:00
|
|
|
serial_write_fcr(s, val & 0xC9);
|
2008-08-11 16:17:04 +02:00
|
|
|
serial_update_irq(s);
|
2004-03-14 13:20:30 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2005-11-08 23:30:36 +01:00
|
|
|
{
|
|
|
|
int break_enable;
|
|
|
|
s->lcr = val;
|
|
|
|
serial_update_parameters(s);
|
|
|
|
break_enable = (val >> 6) & 1;
|
|
|
|
if (break_enable != s->last_break_enable) {
|
|
|
|
s->last_break_enable = break_enable;
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
|
|
|
|
&break_enable);
|
2005-11-08 23:30:36 +01:00
|
|
|
}
|
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
2008-08-11 16:17:04 +02:00
|
|
|
{
|
|
|
|
int old_mcr = s->mcr;
|
|
|
|
s->mcr = val & 0x1f;
|
|
|
|
if (val & UART_MCR_LOOP)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (s->poll_msl >= 0 && old_mcr != s->mcr) {
|
2017-07-06 14:08:59 +02:00
|
|
|
serial_update_tiocm(s);
|
2008-08-11 16:17:04 +02:00
|
|
|
/* Update the modem status after a one-character-send wait-time, since there may be a response
|
|
|
|
from the device/computer at the other end of the serial line */
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->modem_status_poll, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time);
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
s->scr = val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 13:40:29 +02:00
|
|
|
static uint64_t serial_ioport_read(void *opaque, hwaddr addr, unsigned size)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2004-03-14 22:46:48 +01:00
|
|
|
SerialState *s = opaque;
|
2004-03-14 13:20:30 +01:00
|
|
|
uint32_t ret;
|
|
|
|
|
|
|
|
addr &= 7;
|
|
|
|
switch(addr) {
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
if (s->lcr & UART_LCR_DLAB) {
|
2007-09-16 23:08:06 +02:00
|
|
|
ret = s->divider & 0xff;
|
2004-03-14 13:20:30 +01:00
|
|
|
} else {
|
2008-08-11 16:17:04 +02:00
|
|
|
if(s->fcr & UART_FCR_FE) {
|
2013-06-17 02:30:52 +02:00
|
|
|
ret = fifo8_is_empty(&s->recv_fifo) ?
|
2013-06-03 07:13:27 +02:00
|
|
|
0 : fifo8_pop(&s->recv_fifo);
|
|
|
|
if (s->recv_fifo.num == 0) {
|
2008-08-11 16:17:04 +02:00
|
|
|
s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
|
2013-06-03 07:12:09 +02:00
|
|
|
} else {
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->fifo_timeout_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time * 4);
|
2013-06-03 07:12:09 +02:00
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
s->timeout_ipending = 0;
|
|
|
|
} else {
|
|
|
|
ret = s->rbr;
|
|
|
|
s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
|
|
|
|
}
|
2004-03-14 22:46:48 +01:00
|
|
|
serial_update_irq(s);
|
2008-02-10 14:40:52 +01:00
|
|
|
if (!(s->mcr & UART_MCR_LOOP)) {
|
|
|
|
/* in loopback mode, don't receive any data */
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_accept_input(&s->chr);
|
2008-02-10 14:40:52 +01:00
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (s->lcr & UART_LCR_DLAB) {
|
|
|
|
ret = (s->divider >> 8) & 0xff;
|
|
|
|
} else {
|
|
|
|
ret = s->ier;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ret = s->iir;
|
2010-03-06 22:19:53 +01:00
|
|
|
if ((ret & UART_IIR_ID) == UART_IIR_THRI) {
|
2004-03-14 13:20:30 +01:00
|
|
|
s->thr_ipending = 0;
|
2010-02-10 22:35:54 +01:00
|
|
|
serial_update_irq(s);
|
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ret = s->lcr;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
ret = s->mcr;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
ret = s->lsr;
|
2010-02-10 22:35:54 +01:00
|
|
|
/* Clear break and overrun interrupts */
|
|
|
|
if (s->lsr & (UART_LSR_BI|UART_LSR_OE)) {
|
|
|
|
s->lsr &= ~(UART_LSR_BI|UART_LSR_OE);
|
2008-08-11 16:17:04 +02:00
|
|
|
serial_update_irq(s);
|
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (s->mcr & UART_MCR_LOOP) {
|
|
|
|
/* in loopback, the modem output pins are connected to the
|
|
|
|
inputs */
|
|
|
|
ret = (s->mcr & 0x0c) << 4;
|
|
|
|
ret |= (s->mcr & 0x02) << 3;
|
|
|
|
ret |= (s->mcr & 0x01) << 5;
|
|
|
|
} else {
|
2008-08-11 16:17:04 +02:00
|
|
|
if (s->poll_msl >= 0)
|
|
|
|
serial_update_msl(s);
|
2004-03-14 13:20:30 +01:00
|
|
|
ret = s->msr;
|
2008-08-11 16:17:04 +02:00
|
|
|
/* Clear delta bits & msr int after read, if they were set */
|
|
|
|
if (s->msr & UART_MSR_ANY_DELTA) {
|
|
|
|
s->msr &= 0xF0;
|
|
|
|
serial_update_irq(s);
|
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
ret = s->scr;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-21 19:12:49 +02:00
|
|
|
trace_serial_ioport_read(addr, ret);
|
2004-03-14 13:20:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-07-14 19:28:13 +02:00
|
|
|
static int serial_can_receive(SerialState *s)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2008-08-11 16:17:04 +02:00
|
|
|
if(s->fcr & UART_FCR_FE) {
|
2013-06-03 07:13:27 +02:00
|
|
|
if (s->recv_fifo.num < UART_FIFO_LENGTH) {
|
2013-06-03 07:12:09 +02:00
|
|
|
/*
|
|
|
|
* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1
|
|
|
|
* if above. If UART_FIFO_LENGTH - fifo.count is advertised the
|
|
|
|
* effect will be to almost always fill the fifo completely before
|
|
|
|
* the guest has a chance to respond, effectively overriding the ITL
|
|
|
|
* that the guest has set.
|
|
|
|
*/
|
2013-06-03 07:13:27 +02:00
|
|
|
return (s->recv_fifo.num <= s->recv_fifo_itl) ?
|
|
|
|
s->recv_fifo_itl - s->recv_fifo.num : 1;
|
2013-06-03 07:12:09 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
} else {
|
2013-06-03 07:12:09 +02:00
|
|
|
return !(s->lsr & UART_LSR_DR);
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
|
2004-07-14 19:28:13 +02:00
|
|
|
static void serial_receive_break(SerialState *s)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
|
|
|
s->rbr = 0;
|
2009-05-18 17:00:27 +02:00
|
|
|
/* When the LSR_DR is set a null byte is pushed into the fifo */
|
2013-06-03 07:13:27 +02:00
|
|
|
recv_fifo_put(s, '\0');
|
2004-03-14 13:20:30 +01:00
|
|
|
s->lsr |= UART_LSR_BI | UART_LSR_DR;
|
2004-03-14 22:46:48 +01:00
|
|
|
serial_update_irq(s);
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
|
2008-08-11 16:17:04 +02:00
|
|
|
/* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
|
|
|
|
static void fifo_timeout_int (void *opaque) {
|
|
|
|
SerialState *s = opaque;
|
2013-06-03 07:13:27 +02:00
|
|
|
if (s->recv_fifo.num) {
|
2008-08-11 16:17:04 +02:00
|
|
|
s->timeout_ipending = 1;
|
|
|
|
serial_update_irq(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:46:48 +01:00
|
|
|
static int serial_can_receive1(void *opaque)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2004-03-14 22:46:48 +01:00
|
|
|
SerialState *s = opaque;
|
|
|
|
return serial_can_receive(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_receive1(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
2012-02-23 13:45:23 +01:00
|
|
|
|
|
|
|
if (s->wakeup) {
|
qmp hmp: Make system_wakeup check wake-up support and run state
The qmp/hmp command 'system_wakeup' is simply a direct call to
'qemu_system_wakeup_request' from vl.c. This function verifies if
runstate is SUSPENDED and if the wake up reason is valid before
proceeding. However, no error or warning is thrown if any of those
pre-requirements isn't met. There is no way for the caller to
differentiate between a successful wakeup or an error state caused
when trying to wake up a guest that wasn't suspended.
This means that system_wakeup is silently failing, which can be
considered a bug. Adding error handling isn't an API break in this
case - applications that didn't check the result will remain broken,
the ones that check it will have a chance to deal with it.
Adding to that, the commit before previous created a new QMP API called
query-current-machine, with a new flag called wakeup-suspend-support,
that indicates if the guest has the capability of waking up from suspended
state. Although such guest will never reach SUSPENDED state and erroring
it out in this scenario would suffice, it is more informative for the user
to differentiate between a failure because the guest isn't suspended versus
a failure because the guest does not have support for wake up at all.
All this considered, this patch changes qmp_system_wakeup to check if
the guest is capable of waking up from suspend, and if it is suspended.
After this patch, this is the output of system_wakeup in a guest that
does not have wake-up from suspend support (ppc64):
(qemu) system_wakeup
wake-up from suspend is not supported by this guest
(qemu)
And this is the output of system_wakeup in a x86 guest that has the
support but isn't suspended:
(qemu) system_wakeup
Unable to wake up: guest is not in suspended state
(qemu)
Reported-by: Balamuruhan S <bala24@linux.vnet.ibm.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Message-Id: <20181205194701.17836-4-danielhb413@gmail.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2018-12-05 20:47:01 +01:00
|
|
|
qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
|
2012-02-23 13:45:23 +01:00
|
|
|
}
|
2008-08-11 16:17:04 +02:00
|
|
|
if(s->fcr & UART_FCR_FE) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < size; i++) {
|
2013-06-03 07:13:27 +02:00
|
|
|
recv_fifo_put(s, buf[i]);
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
s->lsr |= UART_LSR_DR;
|
|
|
|
/* call the timeout receive callback in 4 char transmit time */
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->fifo_timeout_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time * 4);
|
2008-08-11 16:17:04 +02:00
|
|
|
} else {
|
2010-02-10 22:35:54 +01:00
|
|
|
if (s->lsr & UART_LSR_DR)
|
|
|
|
s->lsr |= UART_LSR_OE;
|
2008-08-11 16:17:04 +02:00
|
|
|
s->rbr = buf[0];
|
|
|
|
s->lsr |= UART_LSR_DR;
|
|
|
|
}
|
|
|
|
serial_update_irq(s);
|
2004-03-14 22:46:48 +01:00
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
|
2004-07-14 19:28:13 +02:00
|
|
|
static void serial_event(void *opaque, int event)
|
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
2010-09-13 14:32:32 +02:00
|
|
|
DPRINTF("event %x\n", event);
|
2004-07-14 19:28:13 +02:00
|
|
|
if (event == CHR_EVENT_BREAK)
|
|
|
|
serial_receive_break(s);
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:29:12 +02:00
|
|
|
static int serial_pre_save(void *opaque)
|
2005-11-06 16:48:04 +01:00
|
|
|
{
|
2009-09-29 22:48:22 +02:00
|
|
|
SerialState *s = opaque;
|
2009-09-10 03:04:46 +02:00
|
|
|
s->fcr_vmstate = s->fcr;
|
2017-09-25 13:29:12 +02:00
|
|
|
|
|
|
|
return 0;
|
2005-11-06 16:48:04 +01:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:52 +02:00
|
|
|
static int serial_pre_load(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
|
|
|
s->thr_ipending = -1;
|
|
|
|
s->poll_msl = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-29 22:48:21 +02:00
|
|
|
static int serial_post_load(void *opaque, int version_id)
|
2009-09-10 03:04:46 +02:00
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2009-10-16 15:39:58 +02:00
|
|
|
if (version_id < 3) {
|
|
|
|
s->fcr_vmstate = 0;
|
|
|
|
}
|
2014-08-28 13:18:52 +02:00
|
|
|
if (s->thr_ipending == -1) {
|
|
|
|
s->thr_ipending = ((s->iir & UART_IIR_ID) == UART_IIR_THRI);
|
|
|
|
}
|
2016-06-14 14:46:51 +02:00
|
|
|
|
|
|
|
if (s->tsr_retry > 0) {
|
|
|
|
/* tsr_retry > 0 implies LSR.TEMT = 0 (transmitter not empty). */
|
|
|
|
if (s->lsr & UART_LSR_TEMT) {
|
|
|
|
error_report("inconsistent state in serial device "
|
|
|
|
"(tsr empty, tsr_retry=%d", s->tsr_retry);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->tsr_retry > MAX_XMIT_RETRY) {
|
|
|
|
s->tsr_retry = MAX_XMIT_RETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(s->watch_tag == 0);
|
2016-10-22 11:52:55 +02:00
|
|
|
s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
|
2016-06-14 14:46:51 +02:00
|
|
|
serial_watch_cb, s);
|
|
|
|
} else {
|
|
|
|
/* tsr_retry == 0 implies LSR.TEMT = 1 (transmitter empty). */
|
|
|
|
if (!(s->lsr & UART_LSR_TEMT)) {
|
|
|
|
error_report("inconsistent state in serial device "
|
|
|
|
"(tsr not empty, tsr_retry=0");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-14 14:17:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:52 +02:00
|
|
|
s->last_break_enable = (s->lcr >> 6) & 1;
|
2008-08-11 16:17:04 +02:00
|
|
|
/* Initialize fcr via setter to perform essential side-effects */
|
2014-08-28 13:18:52 +02:00
|
|
|
serial_write_fcr(s, s->fcr_vmstate);
|
2010-09-15 15:35:53 +02:00
|
|
|
serial_update_parameters(s);
|
2005-11-06 16:48:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:52 +02:00
|
|
|
static bool serial_thr_ipending_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
2014-12-22 08:51:57 +01:00
|
|
|
|
|
|
|
if (s->ier & UART_IER_THRI) {
|
|
|
|
bool expected_value = ((s->iir & UART_IIR_ID) == UART_IIR_THRI);
|
|
|
|
return s->thr_ipending != expected_value;
|
|
|
|
} else {
|
|
|
|
/* LSR.THRE will be sampled again when the interrupt is
|
|
|
|
* enabled. thr_ipending is not used in this case, do
|
|
|
|
* not migrate it.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-28 13:18:52 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 22:43:13 +01:00
|
|
|
static const VMStateDescription vmstate_serial_thr_ipending = {
|
2014-08-28 13:18:52 +02:00
|
|
|
.name = "serial/thr_ipending",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-09-23 14:09:54 +02:00
|
|
|
.needed = serial_thr_ipending_needed,
|
2014-08-28 13:18:52 +02:00
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_INT32(thr_ipending, SerialState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool serial_tsr_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = (SerialState *)opaque;
|
|
|
|
return s->tsr_retry != 0;
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:43:13 +01:00
|
|
|
static const VMStateDescription vmstate_serial_tsr = {
|
2014-08-28 13:18:52 +02:00
|
|
|
.name = "serial/tsr",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-09-23 14:09:54 +02:00
|
|
|
.needed = serial_tsr_needed,
|
2014-08-28 13:18:52 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2016-06-14 14:17:16 +02:00
|
|
|
VMSTATE_UINT32(tsr_retry, SerialState),
|
2014-08-28 13:18:52 +02:00
|
|
|
VMSTATE_UINT8(thr, SerialState),
|
|
|
|
VMSTATE_UINT8(tsr, SerialState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool serial_recv_fifo_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = (SerialState *)opaque;
|
|
|
|
return !fifo8_is_empty(&s->recv_fifo);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:43:13 +01:00
|
|
|
static const VMStateDescription vmstate_serial_recv_fifo = {
|
2014-08-28 13:18:52 +02:00
|
|
|
.name = "serial/recv_fifo",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-09-23 14:09:54 +02:00
|
|
|
.needed = serial_recv_fifo_needed,
|
2014-08-28 13:18:52 +02:00
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_STRUCT(recv_fifo, SerialState, 1, vmstate_fifo8, Fifo8),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool serial_xmit_fifo_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = (SerialState *)opaque;
|
|
|
|
return !fifo8_is_empty(&s->xmit_fifo);
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:43:13 +01:00
|
|
|
static const VMStateDescription vmstate_serial_xmit_fifo = {
|
2014-08-28 13:18:52 +02:00
|
|
|
.name = "serial/xmit_fifo",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-09-23 14:09:54 +02:00
|
|
|
.needed = serial_xmit_fifo_needed,
|
2014-08-28 13:18:52 +02:00
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_STRUCT(xmit_fifo, SerialState, 1, vmstate_fifo8, Fifo8),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool serial_fifo_timeout_timer_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = (SerialState *)opaque;
|
|
|
|
return timer_pending(s->fifo_timeout_timer);
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:43:13 +01:00
|
|
|
static const VMStateDescription vmstate_serial_fifo_timeout_timer = {
|
2014-08-28 13:18:52 +02:00
|
|
|
.name = "serial/fifo_timeout_timer",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-09-23 14:09:54 +02:00
|
|
|
.needed = serial_fifo_timeout_timer_needed,
|
2014-08-28 13:18:52 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2015-01-08 10:18:59 +01:00
|
|
|
VMSTATE_TIMER_PTR(fifo_timeout_timer, SerialState),
|
2014-08-28 13:18:52 +02:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool serial_timeout_ipending_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = (SerialState *)opaque;
|
|
|
|
return s->timeout_ipending != 0;
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:43:13 +01:00
|
|
|
static const VMStateDescription vmstate_serial_timeout_ipending = {
|
2014-08-28 13:18:52 +02:00
|
|
|
.name = "serial/timeout_ipending",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-09-23 14:09:54 +02:00
|
|
|
.needed = serial_timeout_ipending_needed,
|
2014-08-28 13:18:52 +02:00
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_INT32(timeout_ipending, SerialState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool serial_poll_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = (SerialState *)opaque;
|
|
|
|
return s->poll_msl >= 0;
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:43:13 +01:00
|
|
|
static const VMStateDescription vmstate_serial_poll = {
|
2014-08-28 13:18:52 +02:00
|
|
|
.name = "serial/poll",
|
|
|
|
.version_id = 1,
|
2014-09-23 14:09:54 +02:00
|
|
|
.needed = serial_poll_needed,
|
2014-08-28 13:18:52 +02:00
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_INT32(poll_msl, SerialState),
|
2015-01-08 10:18:59 +01:00
|
|
|
VMSTATE_TIMER_PTR(modem_status_poll, SerialState),
|
2014-08-28 13:18:52 +02:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-17 09:54:19 +02:00
|
|
|
const VMStateDescription vmstate_serial = {
|
2009-09-10 03:04:46 +02:00
|
|
|
.name = "serial",
|
|
|
|
.version_id = 3,
|
|
|
|
.minimum_version_id = 2,
|
|
|
|
.pre_save = serial_pre_save,
|
2014-08-28 13:18:52 +02:00
|
|
|
.pre_load = serial_pre_load,
|
2009-09-10 03:04:46 +02:00
|
|
|
.post_load = serial_post_load,
|
2014-04-16 15:32:32 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2009-09-10 03:04:46 +02:00
|
|
|
VMSTATE_UINT16_V(divider, SerialState, 2),
|
|
|
|
VMSTATE_UINT8(rbr, SerialState),
|
|
|
|
VMSTATE_UINT8(ier, SerialState),
|
|
|
|
VMSTATE_UINT8(iir, SerialState),
|
|
|
|
VMSTATE_UINT8(lcr, SerialState),
|
|
|
|
VMSTATE_UINT8(mcr, SerialState),
|
|
|
|
VMSTATE_UINT8(lsr, SerialState),
|
|
|
|
VMSTATE_UINT8(msr, SerialState),
|
|
|
|
VMSTATE_UINT8(scr, SerialState),
|
|
|
|
VMSTATE_UINT8_V(fcr_vmstate, SerialState, 3),
|
|
|
|
VMSTATE_END_OF_LIST()
|
2014-08-28 13:18:52 +02:00
|
|
|
},
|
2014-09-23 14:09:54 +02:00
|
|
|
.subsections = (const VMStateDescription*[]) {
|
|
|
|
&vmstate_serial_thr_ipending,
|
|
|
|
&vmstate_serial_tsr,
|
|
|
|
&vmstate_serial_recv_fifo,
|
|
|
|
&vmstate_serial_xmit_fifo,
|
|
|
|
&vmstate_serial_fifo_timeout_timer,
|
|
|
|
&vmstate_serial_timeout_ipending,
|
|
|
|
&vmstate_serial_poll,
|
|
|
|
NULL
|
2009-09-10 03:04:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-02-10 14:40:52 +01:00
|
|
|
static void serial_reset(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
|
|
|
|
2016-06-14 14:35:20 +02:00
|
|
|
if (s->watch_tag > 0) {
|
|
|
|
g_source_remove(s->watch_tag);
|
|
|
|
s->watch_tag = 0;
|
|
|
|
}
|
|
|
|
|
2008-02-10 14:40:52 +01:00
|
|
|
s->rbr = 0;
|
|
|
|
s->ier = 0;
|
|
|
|
s->iir = UART_IIR_NO_INT;
|
|
|
|
s->lcr = 0;
|
|
|
|
s->lsr = UART_LSR_TEMT | UART_LSR_THRE;
|
|
|
|
s->msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS;
|
2009-10-26 21:51:41 +01:00
|
|
|
/* Default to 9600 baud, 1 start bit, 8 data bits, 1 stop bit, no parity. */
|
2008-08-11 16:17:04 +02:00
|
|
|
s->divider = 0x0C;
|
|
|
|
s->mcr = UART_MCR_OUT2;
|
2008-02-10 14:40:52 +01:00
|
|
|
s->scr = 0;
|
2008-08-11 16:17:04 +02:00
|
|
|
s->tsr_retry = 0;
|
2016-03-21 17:02:30 +01:00
|
|
|
s->char_transmit_time = (NANOSECONDS_PER_SECOND / 9600) * 10;
|
2008-08-11 16:17:04 +02:00
|
|
|
s->poll_msl = 0;
|
|
|
|
|
2014-08-28 13:18:52 +02:00
|
|
|
s->timeout_ipending = 0;
|
|
|
|
timer_del(s->fifo_timeout_timer);
|
|
|
|
timer_del(s->modem_status_poll);
|
|
|
|
|
2013-06-03 07:13:27 +02:00
|
|
|
fifo8_reset(&s->recv_fifo);
|
|
|
|
fifo8_reset(&s->xmit_fifo);
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2013-08-21 17:03:08 +02:00
|
|
|
s->last_xmit_ts = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2008-02-10 14:40:52 +01:00
|
|
|
|
|
|
|
s->thr_ipending = 0;
|
|
|
|
s->last_break_enable = 0;
|
|
|
|
qemu_irq_lower(s->irq);
|
2014-08-26 12:16:08 +02:00
|
|
|
|
|
|
|
serial_update_msl(s);
|
|
|
|
s->msr &= ~UART_MSR_ANY_DELTA;
|
2008-02-10 14:40:52 +01:00
|
|
|
}
|
|
|
|
|
serial: chardev hotswap support
This allows to change the port's backend runtime, e.g. change it from
file to a socket making it possible to establish a debug session with
WinDbg
> qemu-system [..] -chardev file,id=charchannel2,path=/tmp/charchannel2 \
-device isa-serial,chardev=charchannel2,id=channel2
QEMU 2.9.50 monitor - type 'help' for more information
(qemu) chardev-change charchannel2 \
socket,host=127.0.0.1,port=4242,server,nowait
For a backend change, a number of ioctls has to be replayed to sync
the current setup of a frontend to a backend tty. This is hopefully
enough so we don't have to track, store and replay the whole original
control byte sequence.
Signed-off-by: Anton Nefedov <anton.nefedov@virtuozzo.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <1499342940-56739-14-git-send-email-anton.nefedov@virtuozzo.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2017-07-06 14:09:00 +02:00
|
|
|
static int serial_be_change(void *opaque)
|
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
|
|
|
|
|
|
|
qemu_chr_fe_set_handlers(&s->chr, serial_can_receive1, serial_receive1,
|
|
|
|
serial_event, serial_be_change, s, NULL, true);
|
|
|
|
|
|
|
|
serial_update_parameters(s);
|
|
|
|
|
|
|
|
qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
|
|
|
|
&s->last_break_enable);
|
|
|
|
|
|
|
|
s->poll_msl = (s->ier & UART_IER_MSI) ? 1 : 0;
|
|
|
|
serial_update_msl(s);
|
|
|
|
|
|
|
|
if (s->poll_msl >= 0 && !(s->mcr & UART_MCR_LOOP)) {
|
|
|
|
serial_update_tiocm(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->watch_tag > 0) {
|
|
|
|
g_source_remove(s->watch_tag);
|
|
|
|
s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
|
|
|
|
serial_watch_cb, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-25 02:37:14 +01:00
|
|
|
void serial_realize_core(SerialState *s, Error **errp)
|
2008-08-11 16:17:04 +02:00
|
|
|
{
|
2013-08-21 17:03:08 +02:00
|
|
|
s->modem_status_poll = timer_new_ns(QEMU_CLOCK_VIRTUAL, (QEMUTimerCB *) serial_update_msl, s);
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2013-08-21 17:03:08 +02:00
|
|
|
s->fifo_timeout_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, (QEMUTimerCB *) fifo_timeout_int, s);
|
2009-06-27 09:25:07 +02:00
|
|
|
qemu_register_reset(serial_reset, s);
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_set_handlers(&s->chr, serial_can_receive1, serial_receive1,
|
serial: chardev hotswap support
This allows to change the port's backend runtime, e.g. change it from
file to a socket making it possible to establish a debug session with
WinDbg
> qemu-system [..] -chardev file,id=charchannel2,path=/tmp/charchannel2 \
-device isa-serial,chardev=charchannel2,id=channel2
QEMU 2.9.50 monitor - type 'help' for more information
(qemu) chardev-change charchannel2 \
socket,host=127.0.0.1,port=4242,server,nowait
For a backend change, a number of ioctls has to be replayed to sync
the current setup of a frontend to a backend tty. This is hopefully
enough so we don't have to track, store and replay the whole original
control byte sequence.
Signed-off-by: Anton Nefedov <anton.nefedov@virtuozzo.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <1499342940-56739-14-git-send-email-anton.nefedov@virtuozzo.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2017-07-06 14:09:00 +02:00
|
|
|
serial_event, serial_be_change, s, NULL, true);
|
2013-06-03 07:13:27 +02:00
|
|
|
fifo8_create(&s->recv_fifo, UART_FIFO_LENGTH);
|
|
|
|
fifo8_create(&s->xmit_fifo, UART_FIFO_LENGTH);
|
2014-08-26 12:16:57 +02:00
|
|
|
serial_reset(s);
|
2008-08-11 16:17:04 +02:00
|
|
|
}
|
|
|
|
|
2012-10-17 09:54:20 +02:00
|
|
|
void serial_exit_core(SerialState *s)
|
|
|
|
{
|
2017-01-26 21:49:13 +01:00
|
|
|
qemu_chr_fe_deinit(&s->chr, false);
|
2017-01-04 09:43:16 +01:00
|
|
|
|
|
|
|
timer_del(s->modem_status_poll);
|
|
|
|
timer_free(s->modem_status_poll);
|
|
|
|
|
|
|
|
timer_del(s->fifo_timeout_timer);
|
|
|
|
timer_free(s->fifo_timeout_timer);
|
|
|
|
|
|
|
|
fifo8_destroy(&s->recv_fifo);
|
|
|
|
fifo8_destroy(&s->xmit_fifo);
|
|
|
|
|
2012-10-17 09:54:20 +02:00
|
|
|
qemu_unregister_reset(serial_reset, s);
|
|
|
|
}
|
|
|
|
|
2009-10-31 11:28:11 +01:00
|
|
|
/* Change the main reference oscillator frequency. */
|
|
|
|
void serial_set_frequency(SerialState *s, uint32_t frequency)
|
|
|
|
{
|
|
|
|
s->baudbase = frequency;
|
|
|
|
serial_update_parameters(s);
|
|
|
|
}
|
|
|
|
|
2012-10-17 09:54:19 +02:00
|
|
|
const MemoryRegionOps serial_io_ops = {
|
2012-10-08 13:40:29 +02:00
|
|
|
.read = serial_ioport_read,
|
|
|
|
.write = serial_ioport_write,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
2011-08-11 00:28:18 +02:00
|
|
|
};
|
|
|
|
|
2008-05-04 23:42:11 +02:00
|
|
|
SerialState *serial_init(int base, qemu_irq irq, int baudbase,
|
2016-12-07 14:20:22 +01:00
|
|
|
Chardev *chr, MemoryRegion *system_io)
|
2004-03-14 22:46:48 +01:00
|
|
|
{
|
|
|
|
SerialState *s;
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
s = g_malloc0(sizeof(SerialState));
|
2008-05-04 23:42:00 +02:00
|
|
|
|
2009-09-22 13:53:21 +02:00
|
|
|
s->irq = irq;
|
|
|
|
s->baudbase = baudbase;
|
2016-10-22 11:52:51 +02:00
|
|
|
qemu_chr_fe_init(&s->chr, chr, &error_abort);
|
2015-09-11 15:04:45 +02:00
|
|
|
serial_realize_core(s, &error_fatal);
|
2004-03-14 22:46:48 +01:00
|
|
|
|
2010-06-25 19:09:07 +02:00
|
|
|
vmstate_register(NULL, base, &vmstate_serial, s);
|
2005-11-06 16:48:04 +01:00
|
|
|
|
2013-06-06 11:41:28 +02:00
|
|
|
memory_region_init_io(&s->io, NULL, &serial_io_ops, s, "serial", 8);
|
2012-09-19 13:50:07 +02:00
|
|
|
memory_region_add_subregion(system_io, base, &s->io);
|
2012-10-08 13:40:29 +02:00
|
|
|
|
2004-03-14 22:46:48 +01:00
|
|
|
return s;
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
2005-11-23 22:11:49 +01:00
|
|
|
|
|
|
|
/* Memory mapped interface */
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint64_t serial_mm_read(void *opaque, hwaddr addr,
|
2011-08-12 01:18:59 +02:00
|
|
|
unsigned size)
|
2005-11-23 22:11:49 +01:00
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
2012-10-08 13:40:29 +02:00
|
|
|
return serial_ioport_read(s, addr >> s->it_shift, 1);
|
2005-11-23 22:11:49 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void serial_mm_write(void *opaque, hwaddr addr,
|
2011-08-12 01:18:59 +02:00
|
|
|
uint64_t value, unsigned size)
|
2010-03-21 20:47:11 +01:00
|
|
|
{
|
|
|
|
SerialState *s = opaque;
|
2017-11-06 17:10:39 +01:00
|
|
|
value &= 255;
|
2012-10-08 13:40:29 +02:00
|
|
|
serial_ioport_write(s, addr >> s->it_shift, value, 1);
|
2010-03-21 20:47:11 +01:00
|
|
|
}
|
|
|
|
|
2011-08-12 01:18:59 +02:00
|
|
|
static const MemoryRegionOps serial_mm_ops[3] = {
|
|
|
|
[DEVICE_NATIVE_ENDIAN] = {
|
|
|
|
.read = serial_mm_read,
|
|
|
|
.write = serial_mm_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2017-11-06 17:10:39 +01:00
|
|
|
.valid.max_access_size = 8,
|
|
|
|
.impl.max_access_size = 8,
|
2011-08-12 01:18:59 +02:00
|
|
|
},
|
|
|
|
[DEVICE_LITTLE_ENDIAN] = {
|
|
|
|
.read = serial_mm_read,
|
|
|
|
.write = serial_mm_write,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
2017-11-06 17:10:39 +01:00
|
|
|
.valid.max_access_size = 8,
|
|
|
|
.impl.max_access_size = 8,
|
2011-08-12 01:18:59 +02:00
|
|
|
},
|
|
|
|
[DEVICE_BIG_ENDIAN] = {
|
|
|
|
.read = serial_mm_read,
|
|
|
|
.write = serial_mm_write,
|
|
|
|
.endianness = DEVICE_BIG_ENDIAN,
|
2017-11-06 17:10:39 +01:00
|
|
|
.valid.max_access_size = 8,
|
|
|
|
.impl.max_access_size = 8,
|
2011-08-12 01:18:59 +02:00
|
|
|
},
|
2005-11-23 22:11:49 +01:00
|
|
|
};
|
|
|
|
|
2011-08-12 01:07:16 +02:00
|
|
|
SerialState *serial_mm_init(MemoryRegion *address_space,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr base, int it_shift,
|
2011-08-12 01:07:16 +02:00
|
|
|
qemu_irq irq, int baudbase,
|
2016-12-07 14:20:22 +01:00
|
|
|
Chardev *chr, enum device_endian end)
|
2005-11-23 22:11:49 +01:00
|
|
|
{
|
|
|
|
SerialState *s;
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
s = g_malloc0(sizeof(SerialState));
|
2008-08-11 16:17:04 +02:00
|
|
|
|
2005-11-23 22:11:49 +01:00
|
|
|
s->it_shift = it_shift;
|
2009-09-22 13:53:21 +02:00
|
|
|
s->irq = irq;
|
|
|
|
s->baudbase = baudbase;
|
2016-10-22 11:52:51 +02:00
|
|
|
qemu_chr_fe_init(&s->chr, chr, &error_abort);
|
2005-11-23 22:11:49 +01:00
|
|
|
|
2015-09-11 15:04:45 +02:00
|
|
|
serial_realize_core(s, &error_fatal);
|
2010-06-25 19:09:07 +02:00
|
|
|
vmstate_register(NULL, base, &vmstate_serial, s);
|
2005-11-23 22:11:49 +01:00
|
|
|
|
2013-06-06 11:41:28 +02:00
|
|
|
memory_region_init_io(&s->io, NULL, &serial_mm_ops[end], s,
|
2011-08-12 01:18:59 +02:00
|
|
|
"serial", 8 << it_shift);
|
2011-08-12 01:07:16 +02:00
|
|
|
memory_region_add_subregion(address_space, base, &s->io);
|
2005-11-23 22:11:49 +01:00
|
|
|
return s;
|
|
|
|
}
|