9ae3a8
From 18e92ed681383c787912d0cd4b8164d8e7df26d4 Mon Sep 17 00:00:00 2001
9ae3a8
From: Fam Zheng <famz@redhat.com>
9ae3a8
Date: Thu, 18 May 2017 09:21:15 +0200
9ae3a8
Subject: [PATCH 02/18] char/serial: Use generic Fifo8
9ae3a8
9ae3a8
RH-Author: Fam Zheng <famz@redhat.com>
9ae3a8
Message-id: <20170518092131.16571-3-famz@redhat.com>
9ae3a8
Patchwork-id: 75292
9ae3a8
O-Subject: [RHEL-7.4 qemu-kvm PATCH v3 02/18] char/serial: Use generic Fifo8
9ae3a8
Bugzilla: 1451470
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
9ae3a8
9ae3a8
From: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
9ae3a8
9ae3a8
Use the generic Fifo8 helper provided by QEMU, rather than re-implement
9ae3a8
privately.
9ae3a8
9ae3a8
Signed-off-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
9ae3a8
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
9ae3a8
(cherry picked from commit 8e8638fa87ff045f5dadec7342301bf10de776ff)
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	hw/char/serial.c
9ae3a8
Conflict because in downstream we've got 4df7961faa out of order.
9ae3a8
---
9ae3a8
 hw/char/serial.c         | 98 +++++++++++++++++-------------------------------
9ae3a8
 include/hw/char/serial.h | 15 +++-----
9ae3a8
 2 files changed, 39 insertions(+), 74 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/char/serial.c b/hw/char/serial.c
9ae3a8
index 7866b0f..0d4450e 100644
9ae3a8
--- a/hw/char/serial.c
9ae3a8
+++ b/hw/char/serial.c
9ae3a8
@@ -93,8 +93,6 @@
9ae3a8
 #define UART_FCR_RFR        0x02    /* RCVR Fifo Reset */
9ae3a8
 #define UART_FCR_FE         0x01    /* FIFO Enable */
9ae3a8
 
9ae3a8
-#define XMIT_FIFO           0
9ae3a8
-#define RECV_FIFO           1
9ae3a8
 #define MAX_XMIT_RETRY      4
9ae3a8
 
9ae3a8
 #ifdef DEBUG_SERIAL
9ae3a8
@@ -107,50 +105,14 @@ do {} while (0)
9ae3a8
 
9ae3a8
 static void serial_receive1(void *opaque, const uint8_t *buf, int size);
9ae3a8
 
9ae3a8
-static void fifo_clear(SerialState *s, int fifo)
9ae3a8
+static inline void recv_fifo_put(SerialState *s, uint8_t chr)
9ae3a8
 {
9ae3a8
-    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
9ae3a8
-    memset(f->data, 0, UART_FIFO_LENGTH);
9ae3a8
-    f->count = 0;
9ae3a8
-    f->head = 0;
9ae3a8
-    f->tail = 0;
9ae3a8
-}
9ae3a8
-
9ae3a8
-static int fifo_put(SerialState *s, int fifo, uint8_t chr)
9ae3a8
-{
9ae3a8
-    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
9ae3a8
-
9ae3a8
     /* Receive overruns do not overwrite FIFO contents. */
9ae3a8
-    if (fifo == XMIT_FIFO || f->count < UART_FIFO_LENGTH) {
9ae3a8
-
9ae3a8
-        f->data[f->head++] = chr;
9ae3a8
-
9ae3a8
-        if (f->head == UART_FIFO_LENGTH)
9ae3a8
-            f->head = 0;
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    if (f->count < UART_FIFO_LENGTH)
9ae3a8
-        f->count++;
9ae3a8
-    else if (fifo == RECV_FIFO)
9ae3a8
+    if (!fifo8_is_full(&s->recv_fifo)) {
9ae3a8
+        fifo8_push(&s->recv_fifo, chr);
9ae3a8
+    } else {
9ae3a8
         s->lsr |= UART_LSR_OE;
9ae3a8
-
9ae3a8
-    return 1;
9ae3a8
-}
9ae3a8
-
9ae3a8
-static uint8_t fifo_get(SerialState *s, int fifo)
9ae3a8
-{
9ae3a8
-    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
9ae3a8
-    uint8_t c;
9ae3a8
-
9ae3a8
-    if(f->count == 0)
9ae3a8
-        return 0;
9ae3a8
-
9ae3a8
-    c = f->data[f->tail++];
9ae3a8
-    if (f->tail == UART_FIFO_LENGTH)
9ae3a8
-        f->tail = 0;
9ae3a8
-    f->count--;
9ae3a8
-
9ae3a8
-    return c;
9ae3a8
+    }
9ae3a8
 }
9ae3a8
 
9ae3a8
 static void serial_update_irq(SerialState *s)
9ae3a8
@@ -166,7 +128,7 @@ static void serial_update_irq(SerialState *s)
9ae3a8
         tmp_iir = UART_IIR_CTI;
9ae3a8
     } else if ((s->ier & UART_IER_RDI) && (s->lsr & UART_LSR_DR) &&
9ae3a8
                (!(s->fcr & UART_FCR_FE) ||
9ae3a8
-                s->recv_fifo.count >= s->recv_fifo.itl)) {
9ae3a8
+                s->recv_fifo.num >= s->recv_fifo_itl)) {
9ae3a8
         tmp_iir = UART_IIR_RDI;
9ae3a8
     } else if ((s->ier & UART_IER_THRI) && s->thr_ipending) {
9ae3a8
         tmp_iir = UART_IIR_THRI;
9ae3a8
@@ -263,8 +225,9 @@ static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque)
9ae3a8
 
9ae3a8
     if (s->tsr_retry <= 0) {
9ae3a8
         if (s->fcr & UART_FCR_FE) {
9ae3a8
-            s->tsr = fifo_get(s,XMIT_FIFO);
9ae3a8
-            if (!s->xmit_fifo.count) {
9ae3a8
+            s->tsr = fifo8_is_full(&s->xmit_fifo) ?
9ae3a8
+                        0 : fifo8_pop(&s->xmit_fifo);
9ae3a8
+            if (!s->xmit_fifo.num) {
9ae3a8
                 s->lsr |= UART_LSR_THRE;
9ae3a8
             }
9ae3a8
         } else if ((s->lsr & UART_LSR_THRE)) {
9ae3a8
@@ -318,7 +281,11 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,
9ae3a8
         } else {
9ae3a8
             s->thr = (uint8_t) val;
9ae3a8
             if(s->fcr & UART_FCR_FE) {
9ae3a8
-                fifo_put(s, XMIT_FIFO, s->thr);
9ae3a8
+                /* xmit overruns overwrite data, so make space if needed */
9ae3a8
+                if (fifo8_is_full(&s->xmit_fifo)) {
9ae3a8
+                    fifo8_pop(&s->xmit_fifo);
9ae3a8
+                }
9ae3a8
+                fifo8_push(&s->xmit_fifo, s->thr);
9ae3a8
                 s->thr_ipending = 0;
9ae3a8
                 s->lsr &= ~UART_LSR_TEMT;
9ae3a8
                 s->lsr &= ~UART_LSR_THRE;
9ae3a8
@@ -369,28 +336,28 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,
9ae3a8
         if (val & UART_FCR_RFR) {
9ae3a8
             qemu_del_timer(s->fifo_timeout_timer);
9ae3a8
             s->timeout_ipending=0;
9ae3a8
-            fifo_clear(s,RECV_FIFO);
9ae3a8
+            fifo8_reset(&s->recv_fifo);
9ae3a8
         }
9ae3a8
 
9ae3a8
         if (val & UART_FCR_XFR) {
9ae3a8
-            fifo_clear(s,XMIT_FIFO);
9ae3a8
+            fifo8_reset(&s->xmit_fifo);
9ae3a8
         }
9ae3a8
 
9ae3a8
         if (val & UART_FCR_FE) {
9ae3a8
             s->iir |= UART_IIR_FE;
9ae3a8
-            /* Set RECV_FIFO trigger Level */
9ae3a8
+            /* Set recv_fifo trigger Level */
9ae3a8
             switch (val & 0xC0) {
9ae3a8
             case UART_FCR_ITL_1:
9ae3a8
-                s->recv_fifo.itl = 1;
9ae3a8
+                s->recv_fifo_itl = 1;
9ae3a8
                 break;
9ae3a8
             case UART_FCR_ITL_2:
9ae3a8
-                s->recv_fifo.itl = 4;
9ae3a8
+                s->recv_fifo_itl = 4;
9ae3a8
                 break;
9ae3a8
             case UART_FCR_ITL_3:
9ae3a8
-                s->recv_fifo.itl = 8;
9ae3a8
+                s->recv_fifo_itl = 8;
9ae3a8
                 break;
9ae3a8
             case UART_FCR_ITL_4:
9ae3a8
-                s->recv_fifo.itl = 14;
9ae3a8
+                s->recv_fifo_itl = 14;
9ae3a8
                 break;
9ae3a8
             }
9ae3a8
         } else
9ae3a8
@@ -462,8 +429,9 @@ static uint64_t serial_ioport_read(void *opaque, hwaddr addr, unsigned size)
9ae3a8
             ret = s->divider & 0xff;
9ae3a8
         } else {
9ae3a8
             if(s->fcr & UART_FCR_FE) {
9ae3a8
-                ret = fifo_get(s,RECV_FIFO);
9ae3a8
-                if (s->recv_fifo.count == 0) {
9ae3a8
+                ret = fifo8_is_full(&s->recv_fifo) ?
9ae3a8
+                            0 : fifo8_pop(&s->recv_fifo);
9ae3a8
+                if (s->recv_fifo.num == 0) {
9ae3a8
                     s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
9ae3a8
                 } else {
9ae3a8
                     qemu_mod_timer(s->fifo_timeout_timer, qemu_get_clock_ns (vm_clock) + s->char_transmit_time * 4);
9ae3a8
@@ -537,7 +505,7 @@ static uint64_t serial_ioport_read(void *opaque, hwaddr addr, unsigned size)
9ae3a8
 static int serial_can_receive(SerialState *s)
9ae3a8
 {
9ae3a8
     if(s->fcr & UART_FCR_FE) {
9ae3a8
-        if (s->recv_fifo.count < UART_FIFO_LENGTH) {
9ae3a8
+        if (s->recv_fifo.num < UART_FIFO_LENGTH) {
9ae3a8
             /*
9ae3a8
              * Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1
9ae3a8
              * if above. If UART_FIFO_LENGTH - fifo.count is advertised the
9ae3a8
@@ -545,8 +513,8 @@ static int serial_can_receive(SerialState *s)
9ae3a8
              * the guest has a chance to respond, effectively overriding the ITL
9ae3a8
              * that the guest has set.
9ae3a8
              */
9ae3a8
-            return (s->recv_fifo.count <= s->recv_fifo.itl) ?
9ae3a8
-                        s->recv_fifo.itl - s->recv_fifo.count : 1;
9ae3a8
+            return (s->recv_fifo.num <= s->recv_fifo_itl) ?
9ae3a8
+                        s->recv_fifo_itl - s->recv_fifo.num : 1;
9ae3a8
         } else {
9ae3a8
             return 0;
9ae3a8
         }
9ae3a8
@@ -559,7 +527,7 @@ static void serial_receive_break(SerialState *s)
9ae3a8
 {
9ae3a8
     s->rbr = 0;
9ae3a8
     /* When the LSR_DR is set a null byte is pushed into the fifo */
9ae3a8
-    fifo_put(s, RECV_FIFO, '\0');
9ae3a8
+    recv_fifo_put(s, '\0');
9ae3a8
     s->lsr |= UART_LSR_BI | UART_LSR_DR;
9ae3a8
     serial_update_irq(s);
9ae3a8
 }
9ae3a8
@@ -567,7 +535,7 @@ static void serial_receive_break(SerialState *s)
9ae3a8
 /* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
9ae3a8
 static void fifo_timeout_int (void *opaque) {
9ae3a8
     SerialState *s = opaque;
9ae3a8
-    if (s->recv_fifo.count) {
9ae3a8
+    if (s->recv_fifo.num) {
9ae3a8
         s->timeout_ipending = 1;
9ae3a8
         serial_update_irq(s);
9ae3a8
     }
9ae3a8
@@ -589,7 +557,7 @@ static void serial_receive1(void *opaque, const uint8_t *buf, int size)
9ae3a8
     if(s->fcr & UART_FCR_FE) {
9ae3a8
         int i;
9ae3a8
         for (i = 0; i < size; i++) {
9ae3a8
-            fifo_put(s, RECV_FIFO, buf[i]);
9ae3a8
+            recv_fifo_put(s, buf[i]);
9ae3a8
         }
9ae3a8
         s->lsr |= UART_LSR_DR;
9ae3a8
         /* call the timeout receive callback in 4 char transmit time */
9ae3a8
@@ -669,8 +637,8 @@ static void serial_reset(void *opaque)
9ae3a8
     s->char_transmit_time = (get_ticks_per_sec() / 9600) * 10;
9ae3a8
     s->poll_msl = 0;
9ae3a8
 
9ae3a8
-    fifo_clear(s,RECV_FIFO);
9ae3a8
-    fifo_clear(s,XMIT_FIFO);
9ae3a8
+    fifo8_reset(&s->recv_fifo);
9ae3a8
+    fifo8_reset(&s->xmit_fifo);
9ae3a8
 
9ae3a8
     s->last_xmit_ts = qemu_get_clock_ns(vm_clock);
9ae3a8
 
9ae3a8
@@ -693,6 +661,8 @@ void serial_init_core(SerialState *s)
9ae3a8
 
9ae3a8
     qemu_chr_add_handlers(s->chr, serial_can_receive1, serial_receive1,
9ae3a8
                           serial_event, s);
9ae3a8
+    fifo8_create(&s->recv_fifo, UART_FIFO_LENGTH);
9ae3a8
+    fifo8_create(&s->xmit_fifo, UART_FIFO_LENGTH);
9ae3a8
     serial_reset(s);
9ae3a8
 }
9ae3a8
 
9ae3a8
diff --git a/include/hw/char/serial.h b/include/hw/char/serial.h
9ae3a8
index bca79f1..9ab81f6 100644
9ae3a8
--- a/include/hw/char/serial.h
9ae3a8
+++ b/include/hw/char/serial.h
9ae3a8
@@ -28,17 +28,10 @@
9ae3a8
 #include "hw/hw.h"
9ae3a8
 #include "sysemu/sysemu.h"
9ae3a8
 #include "exec/memory.h"
9ae3a8
+#include "qemu/fifo8.h"
9ae3a8
 
9ae3a8
 #define UART_FIFO_LENGTH    16      /* 16550A Fifo Length */
9ae3a8
 
9ae3a8
-typedef struct SerialFIFO {
9ae3a8
-    uint8_t data[UART_FIFO_LENGTH];
9ae3a8
-    uint8_t count;
9ae3a8
-    uint8_t itl;                        /* Interrupt Trigger Level */
9ae3a8
-    uint8_t tail;
9ae3a8
-    uint8_t head;
9ae3a8
-} SerialFIFO;
9ae3a8
-
9ae3a8
 struct SerialState {
9ae3a8
     uint16_t divider;
9ae3a8
     uint8_t rbr; /* receive register */
9ae3a8
@@ -67,8 +60,10 @@ struct SerialState {
9ae3a8
 
9ae3a8
     /* Time when the last byte was successfully sent out of the tsr */
9ae3a8
     uint64_t last_xmit_ts;
9ae3a8
-    SerialFIFO recv_fifo;
9ae3a8
-    SerialFIFO xmit_fifo;
9ae3a8
+    Fifo8 recv_fifo;
9ae3a8
+    Fifo8 xmit_fifo;
9ae3a8
+    /* Interrupt trigger level for recv_fifo */
9ae3a8
+    uint8_t recv_fifo_itl;
9ae3a8
 
9ae3a8
     struct QEMUTimer *fifo_timeout_timer;
9ae3a8
     int timeout_ipending;           /* timeout interrupt pending state */
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8