Skip to contentSkip to conversation

Zynq-7000 EPP platform model

PATCH Superseded Peter A. G. Crosthwaite 5 participants 21 messages Feb 20, 2012 – Feb 28, 2012

Subsystems (2)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover Zynq-7000 EPP platform model

This is a suite of Device models and a machine model for the Xilinx Zynq-7000 Extensible Processing Platform:

http://www.xilinx.com/products/silicon-devices/epp/zynq-7000/index.htm

This is an ARM based platform featuring embedded SoC peripherals. This patch series includes a minimal set of device models and a a machine model capable of emulating zynq platforms booting linux.

This first 3 patches in this series are device models for IP provided by cadence for the Zynq platform. The final patch is the initial revision of the zynq machine model.

Most of this work was originally authored by Xilinx, as indicated by (c) notices in added files.

Tree is available from:
git://developer.petalogix.com/private/peterc/qemu.git
branch: zynq-initial.5

---
changed from v5:
removed usages of arm_boot_info->bootsmp_priv_base (4/4)
changed from v4:
fixed FSF addess (1/4) (2/4) (4/4)
changed device_init -> type_init (all)
changed from v3:
fixed timer race condition issue (2/4)
changed from v2:
fixed timer prescision issue (2/4)
fixed compile warnings in zynq_arm_sysctl (4/4)
changes from v1:
formatting and style fixes
updated for QOM
removed former patch 3 (cadence WDT device model) - not required
removed former patch 5 (dtb argument) - this is currently under discussion in other patch series' removed former patch 6 (initrd parameterisation) - not required for minimal boot

Peter A. G. Crosthwaite (4):
cadence_uart: initial version of device model
cadence_ttc: initial version of device model
cadence_gem: initial version of device model
xilinx_zynq: machine model initial version

 MAINTAINERS          |    5 +
 Makefile.target      |    4 +
 hw/cadence_gem.c     | 1229 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/cadence_ttc.c     |  439 ++++++++++++++++++
 hw/cadence_uart.c    |  559 +++++++++++++++++++++++
 hw/xilinx_zynq.c     |  173 +++++++
 hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++
 7 files changed, 2941 insertions(+), 0 deletions(-)
 create mode 100644 hw/cadence_gem.c
 create mode 100644 hw/cadence_ttc.c
 create mode 100644 hw/cadence_uart.c
 create mode 100644 hw/xilinx_zynq.c
 create mode 100644 hw/zynq_arm_sysctl.c
Signature · 1 line
1.7.3.2
#1 lore raw

PATCH v6 1/4 cadence_uart: initial version of device model

Implemented cadence UART serial controller

---
---
changed from v4:
fixed FSF addess
changed device_init -> type_init
changes from v1:
converted register file to array
added vmsd state save/load support
removed read side effects from CISR register

 Makefile.target   |    1 +
 hw/cadence_uart.c |  559 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 560 insertions(+), 0 deletions(-)
 create mode 100644 hw/cadence_uart.c
Makefile.target +1 -0
index 651500e..868be15 100644
@@ -343,6 +343,7 @@ endif
 obj-arm-y = integratorcp.o versatilepb.o arm_pic.o arm_timer.o
 obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o
 obj-arm-y += versatile_pci.o
+obj-arm-y += cadence_uart.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o
 obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o
 obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o
hw/cadence_uart.c new file +559 -0

559 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#2 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] cadence_uart: initial version of device model

Quoted 1 line · Peter A. G. Crosthwaite

Implemented cadence UART serial controller

Is there any publicly available documentation for this? Google only found a minimum-info datasheet...

Quoted 56 lines

---
---
changed from v4:
fixed FSF addess
changed device_init -> type_init
changes from v1:
converted register file to array
added vmsd state save/load support
removed read side effects from CISR register

 Makefile.target   |    1 +
 hw/cadence_uart.c |  559 +++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 560 insertions(+), 0 deletions(-)

 create mode 100644 hw/cadence_uart.c

Makefile.target +0 -0
index 651500e..868be15 100644
@@ -343,6 +343,7 @@ endif

 obj-arm-y = integratorcp.o versatilepb.o arm_pic.o arm_timer.o obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o obj-arm-y += versatile_pci.o
+obj-arm-y += cadence_uart.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o

hw/cadence_uart.c new file +21 -0
new file mode 100644
index 0000000..a7e461f
@@ -0,0 +1,559 @@
+/*
+ * Device model for Cadence UART
+ *
+ * Copyright (c) 2010 Xilinx Inc.
+ * Copyright (c) 2012 Peter A.G. Crosthwaite ([email protected])
+ * Copyright (c) 2012 PetaLogix Pty Ltd.
+ * Written by Haibing Ma
+ *            M.Habib
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "sysbus.h"
+#include "qemu-char.h"
+#include "qemu-timer.h"

hw/cadence_uart.c line 31

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+
+#ifdef CADENCE_UART_ERR_DEBUG
+#define qemu_debug(...) do { \
+    fprintf(stderr,  ": %s: ", __func__); \
+    fprintf(stderr, ## __VA_ARGS__); \
+    fflush(stderr); \
+    } while (0);
+#else
+    #define qemu_debug(...)
+#endif

Don't use lowercase for macros; also qemu_debug() is a bad choice as it's liable to get used by generic code.

In fact since this is used exactly once in the whole file I think I'd just drop it completely.

(Ideally we'd support "debug register accesses to this device" by letting you interpose a MemoryRegion that traced addresses and values. Sadly not implemented yet ;-))

Quoted 163 lines
hw/cadence_uart.c new file +163 -0
+#define UART_INTR_RTRIG     0x00000001
+#define UART_INTR_REMPTY    0x00000002
+#define UART_INTR_RFUL      0x00000004
+#define UART_INTR_TEMPTY    0x00000008
+#define UART_INTR_TFUL      0x00000010
+#define UART_INTR_ROVR      0x00000020
+#define UART_INTR_FRAME     0x00000040
+#define UART_INTR_PARE      0x00000080
+#define UART_INTR_TIMEOUT   0x00000100
+#define UART_INTR_DMSI      0x00000200
+#define UART_INTR_TTRIG     0x00000400
+#define UART_INTR_TNFUL     0x00000800
+#define UART_INTR_TOVR      0x00001000
+
+#define UART_CSR_RTRIG      0x00000001
+#define UART_CSR_REMPTY     0x00000002
+#define UART_CSR_RFUL       0x00000004
+#define UART_CSR_TEMPTY     0x00000008
+#define UART_CSR_TFUL       0x00000010
+#define UART_CSR_ROVR       0x00000020
+#define UART_CSR_FRAME      0x00000040
+#define UART_CSR_PARE       0x00000080
+#define UART_CSR_TIMEOUT    0x00000100
+#define UART_CSR_DMSI       0x00000200
+#define UART_CSR_RACTIVE    0x00000400
+#define UART_CSR_TACTIVE    0x00000800
+#define UART_CSR_FDELT      0x00001000
+#define UART_CSR_TTRIG      0x00002000
+#define UART_CSR_TNFUL      0x00004000
+
+#define UART_CR_STOPBRK     0x00000100
+#define UART_CR_STARTBRK    0x00000080
+#define UART_CR_RST_TO      0x00000040
+#define UART_CR_TX_DIS      0x00000020
+#define UART_CR_TX_EN       0x00000010
+#define UART_CR_RX_DIS      0x00000008
+#define UART_CR_RX_EN       0x00000004
+#define UART_CR_TXRST       0x00000002
+#define UART_CR_RXRST       0x00000001
+
+#define UART_MR_CLKS            0x00000001
+#define UART_MR_CHRL            0x00000006
+#define UART_MR_PAR             0x00000038
+#define UART_MR_NBSTOP          0x000000C0
+#define UART_MR_CHMODE          0x00000300
+#define UART_MR_UCLKEN          0x00000400
+#define UART_MR_IRMODE          0x00000800
+
+#define UART_PARITY_ODD        0x001
+#define UART_PARITY_EVEN       0x000
+#define UART_DATA_BITS_6       0x003
+#define UART_DATA_BITS_7       0x002
+#define UART_STOP_BITS_1       0x003
+#define UART_STOP_BITS_2       0x002
+#define RX_FIFO_SIZE           16
+#define TX_FIFO_SIZE           16
+#define UARK_INPUT_CLK         50000000
+
+#define NORMAL_MODE            0
+#define ECHO_MODE              1
+#define LOCAL_LOOPBACK         2
+#define REMOTE_LOOPBACK        3
+
+#define R_CR       (0x00/4)
+#define R_MR       (0x04/4)
+#define R_IER      (0x08/4)
+#define R_IDR      (0x0C/4)
+#define R_IMR      (0x10/4)
+#define R_CISR     (0x14/4)
+#define R_BRGR     (0x18/4)
+#define R_RTOR     (0x1C/4)
+#define R_RTRIG    (0x20/4)
+#define R_MCR      (0x24/4)
+#define R_MSR      (0x28/4)
+#define R_CSR      (0x2C/4)
+#define R_TX_RX    (0x30/4)
+#define R_BDIV     (0x34/4)
+#define R_FDEL     (0x38/4)
+#define R_PMIN     (0x3C/4)
+#define R_PWID     (0x40/4)
+#define R_TTRIG    (0x44/4)
+
+#define R_MAX (R_TTRIG + 1)
+
+typedef struct {
+    SysBusDevice busdev;
+    MemoryRegion iomem;
+    uint32_t r[R_MAX];
+    uint8_t r_fifo[RX_FIFO_SIZE];
+    uint32_t rx_wpos;
+    uint32_t rx_count;
+    uint64_t char_tx_time;
+    CharDriverState *chr;
+    qemu_irq irq;
+    struct QEMUTimer *fifo_trigger_handle;
+    struct QEMUTimer *tx_time_handle;
+} UartState;
+
+static void uart_update_status(UartState *s)
+{
+    qemu_set_irq(s->irq, !!(s->r[R_IMR] & s->r[R_CISR]));
+}
+
+static void fifo_trigger_update(void *opaque)
+{
+    UartState *s = (UartState *)opaque;
+
+    s->r[R_CSR] |= UART_CSR_TIMEOUT;
+    s->r[R_CISR] |= UART_INTR_TIMEOUT;
+
+    uart_update_status(s);
+}
+
+static void uart_tx_redo(UartState *s)
+{
+    uint64_t new_tx_time = qemu_get_clock_ns(vm_clock);
+
+    qemu_mod_timer(s->tx_time_handle, new_tx_time + s->char_tx_time);
+
+    s->r[R_CSR] |= UART_CSR_TEMPTY;
+    s->r[R_CISR] |= UART_INTR_TEMPTY;
+
+    uart_update_status(s);
+}
+
+static void uart_tx_write(void *opaque)
+{
+    UartState *s = (UartState *)opaque;
+
+    uart_tx_redo(s);
+}
+
+static void uart_rx_reset(UartState *s)
+{
+    s->rx_wpos = 0;
+    s->rx_count = 0;
+
+    s->r[R_CSR] |= UART_CSR_REMPTY;
+    s->r[R_CSR] &= ~UART_CSR_RFUL;
+    s->r[R_CSR] &= ~UART_CSR_ROVR;
+    s->r[R_CSR] &= ~UART_CSR_TIMEOUT;
+
+    s->r[R_CISR] &= ~UART_INTR_REMPTY;
+    s->r[R_CISR] &= ~UART_INTR_RFUL;
+    s->r[R_CISR] &= ~UART_INTR_ROVR;
+    s->r[R_CISR] &= ~UART_INTR_TIMEOUT;
+}
+
+static void uart_tx_reset(UartState *s)
+{
+    s->r[R_CSR] |= UART_CSR_TEMPTY;
+    s->r[R_CSR] &= ~UART_CSR_TFUL;
+
+    s->r[R_CISR] &= ~UART_INTR_TEMPTY;
+    s->r[R_CISR] &= ~UART_INTR_TFUL;
+}
+
+static void uart_send_breaks(UartState *s)
+{
+    int break_enabled = 1;
+
+    qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
+                               &break_enabled);

hw/cadence_uart.c line 205

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+}
+
+static inline uint32_t mask_and_right_justify(uint32_t value, uint32_t mask)
+{
+    while (!mask & 0x1) {
+        mask >>= 1;
+        value >>= 1;
+    }
+    return value & mask;
+}

This doesn't do what you think it does. (Hint, operator precedence.)

I think that plus the fact it's not how we handle this in the rest of qemu is good enough to suggest that you just do this the way other device models do, and shift/mask by a constant amount.

Quoted 39 lines
hw/cadence_uart.c new file +39 -0
+
+static void uart_parameters_setup(UartState *s)
+{
+    QEMUSerialSetParams ssp;
+    unsigned int baud_rate, packet_size;
+
+    baud_rate = (s->r[R_MR] & UART_MR_CLKS) ?
+            UARK_INPUT_CLK / 8 : UARK_INPUT_CLK;
+
+    ssp.speed = baud_rate / (s->r[R_BRGR] * (s->r[R_BDIV] + 1));
+    packet_size = 1;
+
+    switch (mask_and_right_justify(s->r[R_MR], UART_MR_PAR)) {
+    case UART_PARITY_EVEN:
+        ssp.parity = 'E';
+        packet_size++;
+        break;
+    case UART_PARITY_ODD:
+        ssp.parity = 'O';
+        packet_size++;
+        break;
+    default:
+        ssp.parity = 'N';
+        break;
+    }
+
+    switch (mask_and_right_justify(s->r[R_MR], UART_MR_CHRL)) {
+    case UART_DATA_BITS_6:
+        ssp.data_bits = 6;
+        break;
+    case UART_DATA_BITS_7:
+        ssp.data_bits = 7;
+        break;
+    default:
+        ssp.data_bits = 8;
+        break;
+    }
+
+    switch (mask_and_right_justify(s->r[R_MR], UART_MR_NBSTOP)) {

hw/cadence_uart.c line 254

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+    case UART_STOP_BITS_1:
+        ssp.stop_bits = 1;
+        break;
+    default:
+        ssp.stop_bits = 2;
+        break;
+    }
+
+    packet_size += ssp.data_bits + ssp.stop_bits;
+    s->char_tx_time =  (get_ticks_per_sec() / ssp.speed) * packet_size;

Stray double space.

hw/cadence_uart.c line 263

Quoted 9 lines
hw/cadence_uart.c new file +9 -0
+    qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
+}
+
+static void uart_stop_breaks(UartState *s)
+{
+    int break_enabled = 0;
+    qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
+                               &break_enabled);
+}

Why not put this next to uart_send_breaks() ?

hw/cadence_uart.c line 273

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+
+static int uart_can_receive(void *opaque)
+{
+    UartState *s = (UartState *)opaque;
+
+    return RX_FIFO_SIZE - s->rx_count;
+}
+
+static void uart_ctrl_update(UartState *s, uint32_t value)
+{

When this is called by uart_write() we've already done s->r[offset] = value, but this code seems to assume that s->r[R_CR] and value are the old and new values. Who's right?

Quoted 11 lines
hw/cadence_uart.c new file +11 -0
+    if (value & UART_CR_TXRST) {
+        uart_tx_reset(s);
+    }
+
+    if (value & UART_CR_RXRST) {
+        uart_rx_reset(s);
+    }
+
+    s->r[R_CR] &= ~(UART_CR_TXRST | UART_CR_RXRST);
+
+    if ((value & UART_CR_TX_EN) && !(s->r[R_CR] & UART_CR_TX_DIS)) {

hw/cadence_uart.c line 294

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+            uart_tx_redo(s);
+    }
+
+    if (value & UART_CR_STARTBRK) {
+        if (!(s->r[R_CR] & UART_CR_STOPBRK)) {
+            uart_send_breaks(s);
+        }
+    }
+    if (value & UART_CR_STARTBRK) {
+        uart_stop_breaks(s);

...if the STARTBRK bit is set we stop sending breaks??

Why does the hardware have separate STARTBRK and STOPBRK bits? What happens if they're both set?

Quoted 41 lines
hw/cadence_uart.c new file +41 -0
+    }
+}
+
+static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size)
+{
+    UartState *s = (UartState *)opaque;
+    uint64_t new_rx_time = qemu_get_clock_ns(vm_clock);
+    int i;
+
+    if ((s->r[R_CR] & UART_CR_RX_DIS) || !(s->r[R_CR] & UART_CR_RX_EN)) {
+        return;
+    }
+
+    s->r[R_CSR] &= ~UART_CSR_REMPTY;
+    s->r[R_CISR] &= ~UART_INTR_REMPTY;
+
+    if (s->rx_count == RX_FIFO_SIZE) {
+        s->r[R_CISR] |= UART_INTR_ROVR;
+        s->r[R_CSR] |= UART_CSR_ROVR;
+    } else {
+        for (i = 0; i < size; i++) {
+            s->r_fifo[s->rx_wpos] = buf[i];
+            s->rx_wpos = (s->rx_wpos + 1) % RX_FIFO_SIZE;
+            s->rx_count++;
+
+            if (s->rx_count == RX_FIFO_SIZE) {
+                s->r[R_CSR] |= UART_CSR_RFUL;
+                s->r[R_CISR] |= UART_INTR_RFUL;
+                break;
+            }
+
+            if (s->rx_count >= s->r[R_RTRIG]) {
+                s->r[R_CISR] |= UART_INTR_RTRIG;
+                s->r[R_CSR] |= UART_CSR_RTRIG;
+            }
+        }
+        qemu_mod_timer(s->fifo_trigger_handle, new_rx_time +
+                                                (s->char_tx_time * 4));
+    }
+    uart_update_status(s);
+}

hw/cadence_uart.c line 345

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+
+static void uart_write_tx_fifo(UartState *s, unsigned  char *c)
+{
+    unsigned  char ch = *c;
+
+    if ((s->r[R_CR] & UART_CR_TX_DIS) || !(s->r[R_CR] & UART_CR_TX_EN)) {
+        return;
+    }
+
+    while (!qemu_chr_fe_write(s->chr, &ch, 1)) {

...why not just pass c?

hw/cadence_uart.c line 345

Quoted 1 line
hw/cadence_uart.c new file +1 -0
+    }

Is looping like this really a good idea? None of the other uarts seem to do it...

hw/cadence_uart.c line 359

Quoted 13 lines
hw/cadence_uart.c new file +13 -0
+}
+
+static void uart_receive(void *opaque, const uint8_t *buf, int size)
+{
+    UartState *s = (UartState *)opaque;
+    uint32_t ch_mode = mask_and_right_justify(s->r[R_MR], UART_MR_CHMODE);
+
+    if (ch_mode == NORMAL_MODE || ch_mode == ECHO_MODE) {
+        uart_write_rx_fifo(opaque, buf, size);
+    }
+    if (ch_mode == REMOTE_LOOPBACK || ch_mode == ECHO_MODE) {
+        uart_write_tx_fifo(s, (unsigned char *)buf);
+    }

Is it really correct that in loopback mode we write the whole buffer to the RX fifo but only one byte of it to the TX fifo? Why not make the write_tx_fifo prototype take a const uint8_t* like read_tx_fifo so we don't have to have a cast here?

Quoted 14 lines
hw/cadence_uart.c new file +14 -0
+}
+
+static void uart_event(void *opaque, int event)
+{
+    UartState *s = (UartState *)opaque;
+    uint8_t buf = '\0';
+
+    if (event == CHR_EVENT_BREAK) {
+        uart_write_rx_fifo(opaque, &buf, 1);
+    }
+
+    uart_update_status(s);
+}
+

hw/cadence_uart.c line 383

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+static void uart_read_rx_fifo(UartState *s, uint32_t *c)
+{
+    if ((s->r[R_CR] & UART_CR_RX_DIS) || !(s->r[R_CR] & UART_CR_RX_EN)) {
+        return;
+    }
+
+    s->r[R_CSR] &= ~UART_CSR_RFUL;
+    s->r[R_CSR] &= ~UART_CSR_ROVR;
+    s->r[R_CISR] &= ~UART_INTR_ROVR;
+    s->r[R_CISR] &= ~UART_INTR_RFUL;

Are you sure this is right? I can't tell without a data sheet, but usually if there's a status register and an interrupt status register then the status register tracks current status but the interrupt status register latches it and is only cleared by explicit interrupt acknowledge.

Quoted 56 lines
hw/cadence_uart.c new file +56 -0
+
+    if (s->rx_count) {
+        uint32_t rx_rpos =
+                (RX_FIFO_SIZE + s->rx_wpos - s->rx_count) % RX_FIFO_SIZE;
+        *c = s->r_fifo[rx_rpos];
+        s->rx_count--;
+
+        if (!s->rx_count) {
+            s->r[R_CISR] |= UART_INTR_REMPTY;
+            s->r[R_CSR] |= UART_CSR_REMPTY;
+        }
+    } else {
+        *c = 0;
+        s->r[R_CISR] |= UART_INTR_REMPTY;
+        s->r[R_CSR] |= UART_CSR_REMPTY;
+    }
+
+    if (s->rx_count < s->r[R_RTRIG]) {
+        s->r[R_CSR] &= ~UART_CSR_RTRIG;
+        s->r[R_CISR] &= ~UART_INTR_RTRIG;
+    }
+    uart_update_status(s);
+}
+
+static void uart_write(void *opaque, target_phys_addr_t offset,
+                          uint64_t value, unsigned size)
+{
+    UartState *s = (UartState *)opaque;
+
+    qemu_debug(" offset:%x data:%08x\n", offset, (unsigned)value);
+    offset >>= 2;
+    switch (offset) {
+    case R_IER: /* ier (wts imr) */
+        s->r[R_IMR] |= value;
+        break;
+    case R_IDR: /* idr (wtc imr) */
+        s->r[R_IMR] &= ~value;
+        break;
+    case R_IMR: /* imr (read only) */
+        break;
+    case R_CISR: /* cisr (wtc) */
+        s->r[R_CISR] &= ~value;
+        break;
+    case R_TX_RX: /* UARTDR */
+        switch (mask_and_right_justify(s->r[R_MR], UART_MR_CHMODE)) {
+        case NORMAL_MODE:
+            uart_write_tx_fifo(s, (unsigned  char *) &value);
+            break;
+        case LOCAL_LOOPBACK:
+            uart_write_rx_fifo(opaque, (unsigned  char *) &value, 1);
+            break;
+        }
+        break;
+    default:
+        s->r[offset] = value;
+    }

hw/cadence_uart.c line 449

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+
+    switch (offset) {
+    case R_CR:
+        uart_ctrl_update(s, value);
+        break;
+    case R_MR:
+        uart_parameters_setup(s);
+        break;
+    }
+}

As noted earlier, the handling of R_CR is suspect here. That would only leave R_MR in this second switch, so if I were you I'd just fold it all into the first switch (ie set s->[R_MR] by hand rather than trying to reuse the default: case).

Quoted 43 lines
hw/cadence_uart.c new file +43 -0
+
+static uint64_t uart_read(void *opaque, target_phys_addr_t offset,
+        unsigned size)
+{
+    UartState *s = (UartState *)opaque;
+    uint32_t c = 0;
+
+    offset >>= 2;
+    if (offset > R_MAX) {
+        return 0;
+    } else if (offset == R_TX_RX) {
+        uart_read_rx_fifo(s, &c);
+        return c;
+    }
+    return s->r[offset];
+}
+
+static const MemoryRegionOps uart_ops = {
+    .read = uart_read,
+    .write = uart_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static int cadence_uart_init(SysBusDevice *dev)
+{
+    UartState *s = FROM_SYSBUS(UartState, dev);
+
+    memory_region_init_io(&s->iomem, &uart_ops, s, "uart", 0x1000);
+    sysbus_init_mmio(dev, &s->iomem);
+    sysbus_init_irq(dev, &s->irq);
+
+    s->fifo_trigger_handle = qemu_new_timer_ns(vm_clock,
+            (QEMUTimerCB *)fifo_trigger_update, s);
+
+    s->tx_time_handle = qemu_new_timer_ns(vm_clock,
+            (QEMUTimerCB *)uart_tx_write, s);
+
+    s->char_tx_time = (get_ticks_per_sec() / 9600) * 10;
+
+    s->chr = qemu_char_get_next_serial();
+
+    s->r[R_CR] = 0x00000128;
+    s->r[R_IMR] = 0;

hw/cadence_uart.c line 502

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+    s->r[R_CISR] = 0;
+    s->r[R_RTRIG] = 0x00000020;
+    s->r[R_BRGR] = 0x0000000F;
+    s->r[R_TTRIG] = 0x00000020;
+
+    uart_rx_reset(s);
+    uart_tx_reset(s);
+
+    s->rx_count = 0;
+    s->rx_wpos = 0;

This sort of reset code should go in a reset function, not the init function.

Quoted 13 lines
hw/cadence_uart.c new file +13 -0
+
+    if (s->chr) {
+        qemu_chr_add_handlers(s->chr, uart_can_receive, uart_receive,
+                              uart_event, s);
+    }
+
+    return 0;
+}
+
+static int cadence_uart_post_load(void *opaque, int version_id)
+{
+    UartState *s = opaque;
+

hw/cadence_uart.c line 525

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+    uart_parameters_setup(s);
+    uart_update_status(s);
+    return 0;
+}
+
+static const VMStateDescription vmstate_cadence_uart = {
+    .name = "cadence_uart",
+    .version_id = 3,
+    .minimum_version_id = 2,
+    .minimum_version_id_old = 2,

...who are we trying to be backward compatible with here?

Quoted 35 lines
hw/cadence_uart.c new file +34 -1
+    .post_load = cadence_uart_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_ARRAY(r, UartState, R_MAX),
+        VMSTATE_UINT8_ARRAY(r_fifo, UartState, RX_FIFO_SIZE),
+        VMSTATE_UINT32(rx_count, UartState),
+        VMSTATE_UINT32(rx_wpos, UartState),
+        VMSTATE_TIMER(fifo_trigger_handle, UartState),
+        VMSTATE_TIMER(tx_time_handle, UartState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void cadence_uart_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
+
+    sdc->init = cadence_uart_init;
+    dc->vmsd = &vmstate_cadence_uart;
+}
+
+static TypeInfo cadence_uart_info = {
+    .name          = "cadence_uart",
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(UartState),
+    .class_init    = cadence_uart_class_init,
+};
+
+static void cadence_uart_register_types(void)
+{
+    type_register_static(&cadence_uart_info);
+}
+
+type_init(cadence_uart_register_types)
--
Quoted 1 line

1.7.3.2

-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] cadence_uart: initial version of device model

Quoted 6 lines · Peter Maydell
Quoted 1 line · Peter A. G. Crosthwaite

Implemented cadence UART serial controller

Is there any publicly available documentation for this? Google only found a minimum-info datasheet...

No, Silicon vendor still has it under NDA. We are pushing for the release.

Quoted 62 lines
Quoted 62 lines

---
---
changed from v4:
fixed FSF addess
changed device_init -> type_init
changes from v1:
converted register file to array
added vmsd state save/load support
removed read side effects from CISR register

 Makefile.target   |    1 +
 hw/cadence_uart.c |  559 +++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 560 insertions(+), 0 deletions(-)

 create mode 100644 hw/cadence_uart.c

Makefile.target +0 -0
index 651500e..868be15 100644
@@ -343,6 +343,7 @@ endif

 obj-arm-y = integratorcp.o versatilepb.o arm_pic.o arm_timer.o obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o obj-arm-y += versatile_pci.o
+obj-arm-y += cadence_uart.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o

hw/cadence_uart.c new file +26 -0
new file mode 100644
index 0000000..a7e461f
@@ -0,0 +1,559 @@
+/*
+ * Device model for Cadence UART
+ *
+ * Copyright (c) 2010 Xilinx Inc.
+ * Copyright (c) 2012 Peter A.G. Crosthwaite ([email protected])
+ * Copyright (c) 2012 PetaLogix Pty Ltd.
+ * Written by Haibing Ma
+ *            M.Habib
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "sysbus.h"
+#include "qemu-char.h"
+#include "qemu-timer.h"
+
+#ifdef CADENCE_UART_ERR_DEBUG
+#define qemu_debug(...) do { \
+    fprintf(stderr,  ": %s: ", __func__); \
+    fprintf(stderr, ## __VA_ARGS__); \
Quoted 8 lines

hw/cadence_uart.c line 31

Quoted 5 lines
hw/cadence_uart.c new file +5 -0
+    fflush(stderr); \
+    } while (0);
+#else
+    #define qemu_debug(...)
+#endif

Don't use lowercase for macros; also qemu_debug() is a bad choice as it's liable to get used by generic code.

OK

Quoted 3 lines

In fact since this is used exactly once in the whole file I think I'd just drop it completely.

Keeps it consistent with how I handle it in other device models

Quoted 180 lines

(Ideally we'd support "debug register accesses to this device" by letting you interpose a MemoryRegion that traced addresses and values. Sadly not implemented yet ;-))

Quoted 163 lines
hw/cadence_uart.c new file +163 -0
+#define UART_INTR_RTRIG     0x00000001
+#define UART_INTR_REMPTY    0x00000002
+#define UART_INTR_RFUL      0x00000004
+#define UART_INTR_TEMPTY    0x00000008
+#define UART_INTR_TFUL      0x00000010
+#define UART_INTR_ROVR      0x00000020
+#define UART_INTR_FRAME     0x00000040
+#define UART_INTR_PARE      0x00000080
+#define UART_INTR_TIMEOUT   0x00000100
+#define UART_INTR_DMSI      0x00000200
+#define UART_INTR_TTRIG     0x00000400
+#define UART_INTR_TNFUL     0x00000800
+#define UART_INTR_TOVR      0x00001000
+
+#define UART_CSR_RTRIG      0x00000001
+#define UART_CSR_REMPTY     0x00000002
+#define UART_CSR_RFUL       0x00000004
+#define UART_CSR_TEMPTY     0x00000008
+#define UART_CSR_TFUL       0x00000010
+#define UART_CSR_ROVR       0x00000020
+#define UART_CSR_FRAME      0x00000040
+#define UART_CSR_PARE       0x00000080
+#define UART_CSR_TIMEOUT    0x00000100
+#define UART_CSR_DMSI       0x00000200
+#define UART_CSR_RACTIVE    0x00000400
+#define UART_CSR_TACTIVE    0x00000800
+#define UART_CSR_FDELT      0x00001000
+#define UART_CSR_TTRIG      0x00002000
+#define UART_CSR_TNFUL      0x00004000
+
+#define UART_CR_STOPBRK     0x00000100
+#define UART_CR_STARTBRK    0x00000080
+#define UART_CR_RST_TO      0x00000040
+#define UART_CR_TX_DIS      0x00000020
+#define UART_CR_TX_EN       0x00000010
+#define UART_CR_RX_DIS      0x00000008
+#define UART_CR_RX_EN       0x00000004
+#define UART_CR_TXRST       0x00000002
+#define UART_CR_RXRST       0x00000001
+
+#define UART_MR_CLKS            0x00000001
+#define UART_MR_CHRL            0x00000006
+#define UART_MR_PAR             0x00000038
+#define UART_MR_NBSTOP          0x000000C0
+#define UART_MR_CHMODE          0x00000300
+#define UART_MR_UCLKEN          0x00000400
+#define UART_MR_IRMODE          0x00000800
+
+#define UART_PARITY_ODD        0x001
+#define UART_PARITY_EVEN       0x000
+#define UART_DATA_BITS_6       0x003
+#define UART_DATA_BITS_7       0x002
+#define UART_STOP_BITS_1       0x003
+#define UART_STOP_BITS_2       0x002
+#define RX_FIFO_SIZE           16
+#define TX_FIFO_SIZE           16
+#define UARK_INPUT_CLK         50000000
+
+#define NORMAL_MODE            0
+#define ECHO_MODE              1
+#define LOCAL_LOOPBACK         2
+#define REMOTE_LOOPBACK        3
+
+#define R_CR       (0x00/4)
+#define R_MR       (0x04/4)
+#define R_IER      (0x08/4)
+#define R_IDR      (0x0C/4)
+#define R_IMR      (0x10/4)
+#define R_CISR     (0x14/4)
+#define R_BRGR     (0x18/4)
+#define R_RTOR     (0x1C/4)
+#define R_RTRIG    (0x20/4)
+#define R_MCR      (0x24/4)
+#define R_MSR      (0x28/4)
+#define R_CSR      (0x2C/4)
+#define R_TX_RX    (0x30/4)
+#define R_BDIV     (0x34/4)
+#define R_FDEL     (0x38/4)
+#define R_PMIN     (0x3C/4)
+#define R_PWID     (0x40/4)
+#define R_TTRIG    (0x44/4)
+
+#define R_MAX (R_TTRIG + 1)
+
+typedef struct {
+    SysBusDevice busdev;
+    MemoryRegion iomem;
+    uint32_t r[R_MAX];
+    uint8_t r_fifo[RX_FIFO_SIZE];
+    uint32_t rx_wpos;
+    uint32_t rx_count;
+    uint64_t char_tx_time;
+    CharDriverState *chr;
+    qemu_irq irq;
+    struct QEMUTimer *fifo_trigger_handle;
+    struct QEMUTimer *tx_time_handle;
+} UartState;
+
+static void uart_update_status(UartState *s)
+{
+    qemu_set_irq(s->irq, !!(s->r[R_IMR] & s->r[R_CISR]));
+}
+
+static void fifo_trigger_update(void *opaque)
+{
+    UartState *s = (UartState *)opaque;
+
+    s->r[R_CSR] |= UART_CSR_TIMEOUT;
+    s->r[R_CISR] |= UART_INTR_TIMEOUT;
+
+    uart_update_status(s);
+}
+
+static void uart_tx_redo(UartState *s)
+{
+    uint64_t new_tx_time = qemu_get_clock_ns(vm_clock);
+
+    qemu_mod_timer(s->tx_time_handle, new_tx_time + s->char_tx_time);
+
+    s->r[R_CSR] |= UART_CSR_TEMPTY;
+    s->r[R_CISR] |= UART_INTR_TEMPTY;
+
+    uart_update_status(s);
+}
+
+static void uart_tx_write(void *opaque)
+{
+    UartState *s = (UartState *)opaque;
+
+    uart_tx_redo(s);
+}
+
+static void uart_rx_reset(UartState *s)
+{
+    s->rx_wpos = 0;
+    s->rx_count = 0;
+
+    s->r[R_CSR] |= UART_CSR_REMPTY;
+    s->r[R_CSR] &= ~UART_CSR_RFUL;
+    s->r[R_CSR] &= ~UART_CSR_ROVR;
+    s->r[R_CSR] &= ~UART_CSR_TIMEOUT;
+
+    s->r[R_CISR] &= ~UART_INTR_REMPTY;
+    s->r[R_CISR] &= ~UART_INTR_RFUL;
+    s->r[R_CISR] &= ~UART_INTR_ROVR;
+    s->r[R_CISR] &= ~UART_INTR_TIMEOUT;
+}
+
+static void uart_tx_reset(UartState *s)
+{
+    s->r[R_CSR] |= UART_CSR_TEMPTY;
+    s->r[R_CSR] &= ~UART_CSR_TFUL;
+
+    s->r[R_CISR] &= ~UART_INTR_TEMPTY;
+    s->r[R_CISR] &= ~UART_INTR_TFUL;
+}
+
+static void uart_send_breaks(UartState *s)
+{
+    int break_enabled = 1;
+
+    qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
+                               &break_enabled);

hw/cadence_uart.c line 205

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+}
+
+static inline uint32_t mask_and_right_justify(uint32_t value, uint32_t mask)
+{
+    while (!mask & 0x1) {
+        mask >>= 1;
+        value >>= 1;
+    }
+    return value & mask;
+}

This doesn't do what you think it does. (Hint, operator precedence.)

Quoted 4 lines

I think that plus the fact it's not how we handle this in the rest of qemu is good enough to suggest that you just do this the way other device models do, and shift/mask by a constant amount.

Fixed

Quoted 52 lines
Quoted 39 lines
hw/cadence_uart.c new file +39 -0
+
+static void uart_parameters_setup(UartState *s)
+{
+    QEMUSerialSetParams ssp;
+    unsigned int baud_rate, packet_size;
+
+    baud_rate = (s->r[R_MR] & UART_MR_CLKS) ?
+            UARK_INPUT_CLK / 8 : UARK_INPUT_CLK;
+
+    ssp.speed = baud_rate / (s->r[R_BRGR] * (s->r[R_BDIV] + 1));
+    packet_size = 1;
+
+    switch (mask_and_right_justify(s->r[R_MR], UART_MR_PAR)) {
+    case UART_PARITY_EVEN:
+        ssp.parity = 'E';
+        packet_size++;
+        break;
+    case UART_PARITY_ODD:
+        ssp.parity = 'O';
+        packet_size++;
+        break;
+    default:
+        ssp.parity = 'N';
+        break;
+    }
+
+    switch (mask_and_right_justify(s->r[R_MR], UART_MR_CHRL)) {
+    case UART_DATA_BITS_6:
+        ssp.data_bits = 6;
+        break;
+    case UART_DATA_BITS_7:
+        ssp.data_bits = 7;
+        break;
+    default:
+        ssp.data_bits = 8;
+        break;
+    }
+
+    switch (mask_and_right_justify(s->r[R_MR], UART_MR_NBSTOP)) {

hw/cadence_uart.c line 254

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+    case UART_STOP_BITS_1:
+        ssp.stop_bits = 1;
+        break;
+    default:
+        ssp.stop_bits = 2;
+        break;
+    }
+
+    packet_size += ssp.data_bits + ssp.stop_bits;
+    s->char_tx_time =  (get_ticks_per_sec() / ssp.speed) * packet_size;

Stray double space.

Quoted 12 lines

hw/cadence_uart.c line 263

Quoted 9 lines
hw/cadence_uart.c new file +9 -0
+    qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
+}
+
+static void uart_stop_breaks(UartState *s)
+{
+    int break_enabled = 0;
+    qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
+                               &break_enabled);
+}

Why not put this next to uart_send_breaks() ?

Deleted (see below)

Quoted 6 lines
Quoted 6 lines
hw/cadence_uart.c new file +6 -0
+
+static int uart_can_receive(void *opaque)
+{
+    UartState *s = (UartState *)opaque;
+
+    return RX_FIFO_SIZE - s->rx_count;
Quoted 9 lines

hw/cadence_uart.c line 273

Quoted 4 lines
hw/cadence_uart.c new file +4 -0
+}
+
+static void uart_ctrl_update(UartState *s, uint32_t value)
+{

When this is called by uart_write() we've already done s->r[offset] = value, but this code seems to assume that s->r[R_CR] and value are the old and new values. Who's right?

Removed value argument altogether

Quoted 24 lines
Quoted 11 lines
hw/cadence_uart.c new file +11 -0
+    if (value & UART_CR_TXRST) {
+        uart_tx_reset(s);
+    }
+
+    if (value & UART_CR_RXRST) {
+        uart_rx_reset(s);
+    }
+
+    s->r[R_CR] &= ~(UART_CR_TXRST | UART_CR_RXRST);
+
+    if ((value & UART_CR_TX_EN) && !(s->r[R_CR] & UART_CR_TX_DIS)) {

hw/cadence_uart.c line 294

Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+            uart_tx_redo(s);
+    }
+
+    if (value & UART_CR_STARTBRK) {
+        if (!(s->r[R_CR] & UART_CR_STOPBRK)) {
+            uart_send_breaks(s);
+        }
+    }
+    if (value & UART_CR_STARTBRK) {
+        uart_stop_breaks(s);

...if the STARTBRK bit is set we stop sending breaks??

Quoted 3 lines

Why does the hardware have separate STARTBRK and STOPBRK bits? What happens if they're both set?

Yeh this is bizarre. I traced it and it turns out the stop_breaks path is just a giant nop so i removed it completely.

Quoted 47 lines
Quoted 47 lines
hw/cadence_uart.c new file +45 -0
+    }
+}
+
+static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size)
+{
+    UartState *s = (UartState *)opaque;
+    uint64_t new_rx_time = qemu_get_clock_ns(vm_clock);
+    int i;
+
+    if ((s->r[R_CR] & UART_CR_RX_DIS) || !(s->r[R_CR] & UART_CR_RX_EN)) {
+        return;
+    }
+
+    s->r[R_CSR] &= ~UART_CSR_REMPTY;
+    s->r[R_CISR] &= ~UART_INTR_REMPTY;
+
+    if (s->rx_count == RX_FIFO_SIZE) {
+        s->r[R_CISR] |= UART_INTR_ROVR;
+        s->r[R_CSR] |= UART_CSR_ROVR;
+    } else {
+        for (i = 0; i < size; i++) {
+            s->r_fifo[s->rx_wpos] = buf[i];
+            s->rx_wpos = (s->rx_wpos + 1) % RX_FIFO_SIZE;
+            s->rx_count++;
+
+            if (s->rx_count == RX_FIFO_SIZE) {
+                s->r[R_CSR] |= UART_CSR_RFUL;
+                s->r[R_CISR] |= UART_INTR_RFUL;
+                break;
+            }
+
+            if (s->rx_count >= s->r[R_RTRIG]) {
+                s->r[R_CISR] |= UART_INTR_RTRIG;
+                s->r[R_CSR] |= UART_CSR_RTRIG;
+            }
+        }
+        qemu_mod_timer(s->fifo_trigger_handle, new_rx_time +
+                                                (s->char_tx_time * 4));
+    }
+    uart_update_status(s);
+}
+
+static void uart_write_tx_fifo(UartState *s, unsigned  char *c)
+{
+    unsigned  char ch = *c;
Quoted 9 lines

hw/cadence_uart.c line 345

Quoted 6 lines
hw/cadence_uart.c new file +6 -0
+
+    if ((s->r[R_CR] & UART_CR_TX_DIS) || !(s->r[R_CR] & UART_CR_TX_EN)) {
+        return;
+    }
+
+    while (!qemu_chr_fe_write(s->chr, &ch, 1)) {

...why not just pass c?

Done

Quoted 5 lines

hw/cadence_uart.c line 345

Quoted 1 line
hw/cadence_uart.c new file +1 -0
+    }

Is looping like this really a good idea? None of the other uarts seem to do it...

Wont this potentially back onto an write syscall where it can return short of the specified write length? Or have we got that wrong?

Quoted 10 lines
Quoted 10 lines
hw/cadence_uart.c new file +10 -0
+}
+
+static void uart_receive(void *opaque, const uint8_t *buf, int size)
+{
+    UartState *s = (UartState *)opaque;
+    uint32_t ch_mode = mask_and_right_justify(s->r[R_MR], UART_MR_CHMODE);
+
+    if (ch_mode == NORMAL_MODE || ch_mode == ECHO_MODE) {
+        uart_write_rx_fifo(opaque, buf, size);
+    }
Quoted 9 lines

hw/cadence_uart.c line 359

Quoted 3 lines
hw/cadence_uart.c new file +3 -0
+    if (ch_mode == REMOTE_LOOPBACK || ch_mode == ECHO_MODE) {
+        uart_write_tx_fifo(s, (unsigned char *)buf);
+    }

Is it really correct that in loopback mode we write the whole buffer to the RX fifo but only one byte of it to the TX fifo? Why not make the write_tx_fifo prototype take a const uint8_t* like read_tx_fifo so we don't have to have a cast here?

Fixed

Quoted 21 lines
Quoted 21 lines
hw/cadence_uart.c new file +21 -0
+}
+
+static void uart_event(void *opaque, int event)
+{
+    UartState *s = (UartState *)opaque;
+    uint8_t buf = '\0';
+
+    if (event == CHR_EVENT_BREAK) {
+        uart_write_rx_fifo(opaque, &buf, 1);
+    }
+
+    uart_update_status(s);
+}
+
+static void uart_read_rx_fifo(UartState *s, uint32_t *c)
+{
+    if ((s->r[R_CR] & UART_CR_RX_DIS) || !(s->r[R_CR] & UART_CR_RX_EN)) {
+        return;
+    }
+
+    s->r[R_CSR] &= ~UART_CSR_RFUL;
Quoted 9 lines

hw/cadence_uart.c line 383

Quoted 3 lines
hw/cadence_uart.c new file +3 -0
+    s->r[R_CSR] &= ~UART_CSR_ROVR;
+    s->r[R_CISR] &= ~UART_INTR_ROVR;
+    s->r[R_CISR] &= ~UART_INTR_RFUL;

Are you sure this is right? I can't tell without a data sheet, but usually if there's a status register and an interrupt status register then the status register tracks current status but the interrupt status register latches it and is only cleared by explicit interrupt acknowledge.

TRM is vague at best, but you are probably right. Fixed

Quoted 63 lines
Quoted 63 lines
hw/cadence_uart.c new file +63 -0
+
+    if (s->rx_count) {
+        uint32_t rx_rpos =
+                (RX_FIFO_SIZE + s->rx_wpos - s->rx_count) % RX_FIFO_SIZE;
+        *c = s->r_fifo[rx_rpos];
+        s->rx_count--;
+
+        if (!s->rx_count) {
+            s->r[R_CISR] |= UART_INTR_REMPTY;
+            s->r[R_CSR] |= UART_CSR_REMPTY;
+        }
+    } else {
+        *c = 0;
+        s->r[R_CISR] |= UART_INTR_REMPTY;
+        s->r[R_CSR] |= UART_CSR_REMPTY;
+    }
+
+    if (s->rx_count < s->r[R_RTRIG]) {
+        s->r[R_CSR] &= ~UART_CSR_RTRIG;
+        s->r[R_CISR] &= ~UART_INTR_RTRIG;
+    }
+    uart_update_status(s);
+}
+
+static void uart_write(void *opaque, target_phys_addr_t offset,
+                          uint64_t value, unsigned size)
+{
+    UartState *s = (UartState *)opaque;
+
+    qemu_debug(" offset:%x data:%08x\n", offset, (unsigned)value);
+    offset >>= 2;
+    switch (offset) {
+    case R_IER: /* ier (wts imr) */
+        s->r[R_IMR] |= value;
+        break;
+    case R_IDR: /* idr (wtc imr) */
+        s->r[R_IMR] &= ~value;
+        break;
+    case R_IMR: /* imr (read only) */
+        break;
+    case R_CISR: /* cisr (wtc) */
+        s->r[R_CISR] &= ~value;
+        break;
+    case R_TX_RX: /* UARTDR */
+        switch (mask_and_right_justify(s->r[R_MR], UART_MR_CHMODE)) {
+        case NORMAL_MODE:
+            uart_write_tx_fifo(s, (unsigned  char *) &value);
+            break;
+        case LOCAL_LOOPBACK:
+            uart_write_rx_fifo(opaque, (unsigned  char *) &value, 1);
+            break;
+        }
+        break;
+    default:
+        s->r[offset] = value;
+    }
+
+    switch (offset) {
+    case R_CR:
+        uart_ctrl_update(s, value);
+        break;
+    case R_MR:
+        uart_parameters_setup(s);
Quoted 9 lines

hw/cadence_uart.c line 242

Quoted 3 lines
hw/cadence_uart.c new file +3 -0
+        break;
+    }
+}

As noted earlier, the handling of R_CR is suspect here. That would only leave R_MR in this second switch, so if I were you I'd just fold it all into the first switch (ie set s->[R_MR] by hand rather than trying to reuse the default: case).

Fixed elsewhere

Quoted 48 lines
Quoted 48 lines
hw/cadence_uart.c new file +48 -0
+
+static uint64_t uart_read(void *opaque, target_phys_addr_t offset,
+        unsigned size)
+{
+    UartState *s = (UartState *)opaque;
+    uint32_t c = 0;
+
+    offset >>= 2;
+    if (offset > R_MAX) {
+        return 0;
+    } else if (offset == R_TX_RX) {
+        uart_read_rx_fifo(s, &c);
+        return c;
+    }
+    return s->r[offset];
+}
+
+static const MemoryRegionOps uart_ops = {
+    .read = uart_read,
+    .write = uart_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static int cadence_uart_init(SysBusDevice *dev)
+{
+    UartState *s = FROM_SYSBUS(UartState, dev);
+
+    memory_region_init_io(&s->iomem, &uart_ops, s, "uart", 0x1000);
+    sysbus_init_mmio(dev, &s->iomem);
+    sysbus_init_irq(dev, &s->irq);
+
+    s->fifo_trigger_handle = qemu_new_timer_ns(vm_clock,
+            (QEMUTimerCB *)fifo_trigger_update, s);
+
+    s->tx_time_handle = qemu_new_timer_ns(vm_clock,
+            (QEMUTimerCB *)uart_tx_write, s);
+
+    s->char_tx_time = (get_ticks_per_sec() / 9600) * 10;
+
+    s->chr = qemu_char_get_next_serial();
+
+    s->r[R_CR] = 0x00000128;
+    s->r[R_IMR] = 0;
+    s->r[R_CISR] = 0;
+    s->r[R_RTRIG] = 0x00000020;
+    s->r[R_BRGR] = 0x0000000F;
+    s->r[R_TTRIG] = 0x00000020;
+
Quoted 9 lines

hw/cadence_uart.c line 502

Quoted 5 lines
hw/cadence_uart.c new file +5 -0
+    uart_rx_reset(s);
+    uart_tx_reset(s);
+
+    s->rx_count = 0;
+    s->rx_wpos = 0;

This sort of reset code should go in a reset function, not the init function.

Fixed

Quoted 17 lines
Quoted 17 lines
hw/cadence_uart.c new file +17 -0
+
+    if (s->chr) {
+        qemu_chr_add_handlers(s->chr, uart_can_receive, uart_receive,
+                              uart_event, s);
+    }
+
+    return 0;
+}
+
+static int cadence_uart_post_load(void *opaque, int version_id)
+{
+    UartState *s = opaque;
+
+    uart_parameters_setup(s);
+    uart_update_status(s);
+    return 0;
+}
Quoted 9 lines

hw/cadence_uart.c line 525

Quoted 6 lines
hw/cadence_uart.c new file +6 -0
+
+static const VMStateDescription vmstate_cadence_uart = {
+    .name = "cadence_uart",
+    .version_id = 3,
+    .minimum_version_id = 2,
+    .minimum_version_id_old = 2,

...who are we trying to be backward compatible with here?

I kinda just cloned some other random device for these fields. Is this VMSD support a blocker on the series, cos to be honest, nobody wants it and I cant effectively test it, so id rather just mark everything unmigratable and come back to it.

Quoted 38 lines
Quoted 35 lines
hw/cadence_uart.c new file +34 -1
+    .post_load = cadence_uart_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_ARRAY(r, UartState, R_MAX),
+        VMSTATE_UINT8_ARRAY(r_fifo, UartState, RX_FIFO_SIZE),
+        VMSTATE_UINT32(rx_count, UartState),
+        VMSTATE_UINT32(rx_wpos, UartState),
+        VMSTATE_TIMER(fifo_trigger_handle, UartState),
+        VMSTATE_TIMER(tx_time_handle, UartState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void cadence_uart_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
+
+    sdc->init = cadence_uart_init;
+    dc->vmsd = &vmstate_cadence_uart;
+}
+
+static TypeInfo cadence_uart_info = {
+    .name          = "cadence_uart",
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(UartState),
+    .class_init    = cadence_uart_class_init,
+};
+
+static void cadence_uart_register_types(void)
+{
+    type_register_static(&cadence_uart_info);
+}
+
+type_init(cadence_uart_register_types)
--
Quoted 1 line

1.7.3.2

-- PMM

#4 lore raw

PATCH v6 2/4 cadence_ttc: initial version of device model

In reply to Peter A. G. Crosthwaite

Implemented cadence Triple Timer Counter (TCC)

---
changed from v4:
fixed FSF addess
changed device_init -> type_init
changed from v3:
Fixed race condition where timer could miss match events on wrap around changed from v2:
changed ptimer to QEMUTimer (Fixed skew/drift issue in timer delays) changes from v1:
refactored event driven code
marked vmsd as unmigratable

 Makefile.target  |    1 +
 hw/cadence_ttc.c |  439 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 440 insertions(+), 0 deletions(-)
 create mode 100644 hw/cadence_ttc.c
Makefile.target +1 -0
index 868be15..e96b37e 100644
@@ -344,6 +344,7 @@ obj-arm-y = integratorcp.o versatilepb.o arm_pic.o arm_timer.o
 obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o
 obj-arm-y += versatile_pci.o
 obj-arm-y += cadence_uart.o
+obj-arm-y += cadence_ttc.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o
 obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o
 obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o
hw/cadence_ttc.c new file +439 -0

439 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#5 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

Quoted 101 lines · Peter A. G. Crosthwaite

Implemented cadence Triple Timer Counter (TCC)

---
changed from v4:
fixed FSF addess
changed device_init -> type_init
changed from v3:
Fixed race condition where timer could miss match events on wrap around changed from v2:
changed ptimer to QEMUTimer (Fixed skew/drift issue in timer delays) changes from v1:
refactored event driven code
marked vmsd as unmigratable

 Makefile.target  |    1 +
 hw/cadence_ttc.c |  439 ++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 440 insertions(+), 0 deletions(-)

 create mode 100644 hw/cadence_ttc.c

Makefile.target +0 -0
index 868be15..e96b37e 100644
@@ -344,6 +344,7 @@ obj-arm-y = integratorcp.o versatilepb.o arm_pic.o arm_timer.o

 obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o obj-arm-y += versatile_pci.o
 obj-arm-y += cadence_uart.o
+obj-arm-y += cadence_ttc.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o

hw/cadence_ttc.c new file +62 -0
new file mode 100644
index 0000000..14c3ba7
@@ -0,0 +1,439 @@
+/*
+ * Xilinx Zynq cadence TTC model
+ *
+ * Copyright (c) 2011 Xilinx Inc.
+ * Copyright (c) 2012 Peter A.G. Crosthwaite ([email protected])
+ * Copyright (c) 2012 PetaLogix Pty Ltd.
+ * Written By Haibing Ma
+ *            M. Habib
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "sysbus.h"
+#include "qemu-timer.h"
+
+#ifdef CADENCE_TTC_ERR_DEBUG
+#define qemu_debug(...) do { \
+    fprintf(stderr,  ": %s: ", __func__); \
+    fprintf(stderr, ## __VA_ARGS__); \
+    fflush(stderr); \
+    } while (0);
+#else
+    #define qemu_debug(...)
+#endif
+
+#define COUNTER_INTR_IV     0x00000001
+#define COUNTER_INTR_M1     0x00000002
+#define COUNTER_INTR_M2     0x00000004
+#define COUNTER_INTR_M3     0x00000008
+#define COUNTER_INTR_OV     0x00000010
+#define COUNTER_INTR_EV     0x00000020
+
+#define COUNTER_CTRL_DIS    0x00000001
+#define COUNTER_CTRL_INT    0x00000002
+#define COUNTER_CTRL_DEC    0x00000004
+#define COUNTER_CTRL_MATCH  0x00000008
+#define COUNTER_CTRL_RST    0x00000010
+
+#define CLOCK_CTRL_PS_EN    0x00000001
+#define CLOCK_CTRL_PS_V     0x0000001e
+
+typedef struct {
+    QEMUTimer *timer;
+    int freq;
+
+    uint32_t reg_clock;
+    uint32_t reg_count;
+    uint32_t reg_value;
+    uint16_t reg_interval;
+    uint16_t reg_match[3];
+    uint32_t reg_intr;
+    uint32_t reg_intr_en;
+    uint32_t reg_event_ctrl;
+    uint32_t reg_event;
+
+    uint64_t cpu_time;

hw/cadence_ttc.c line 72

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    unsigned int cpu_time_valid;
+
+    qemu_irq irq;
+} CadenceTimerState;
+
+typedef struct {
+    SysBusDevice busdev;
+    MemoryRegion iomem;
+    CadenceTimerState * timer[3];
+} cadence_ttc_state;

This type name is in breach of the coding style, which demands camelcase.

hw/cadence_ttc.c line 85

Quoted 13 lines
hw/cadence_ttc.c new file +13 -0
+
+static void cadence_timer_update(CadenceTimerState *s)
+{
+    qemu_set_irq(s->irq, !!(s->reg_intr & s->reg_intr_en));
+}
+
+static CadenceTimerState *cadence_timer_from_addr(void *opaque,
+                                        target_phys_addr_t offset)
+{
+    unsigned int index;
+    cadence_ttc_state *s = (cadence_ttc_state *)opaque;
+
+    index = (offset >> 2) % 3;

Really % 3 ? This must be a pain to implement in hardware...

Quoted 17 lines
hw/cadence_ttc.c new file +17 -0
+    return s->timer[index];
+}
+
+static uint64_t cadence_timer_get_ns(CadenceTimerState *s, uint64_t timer_steps)
+{
+    /* timer_steps has max value of 0x100000000. double check it
+     * (or overflow can happen below) */
+    assert(timer_steps <= 1ULL << 32);
+
+    uint64_t r = timer_steps * 1000000000ULL;
+    if (s->reg_clock & CLOCK_CTRL_PS_EN) {
+        r >>= 16 - (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
+    } else {
+        r >>= 16;
+    }
+    r /= (uint64_t)s->freq;
+    return r;

hw/cadence_ttc.c line 113

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+}
+
+static uint64_t cadence_timer_get_steps(CadenceTimerState *s, uint64_t ns)
+{
+    uint64_t to_divide = 1000000000ULL;
+
+    uint64_t r = ns;
+     /* for very large intervals (> 8s) do some division first to stop
+      * overflow (costs some prescision) */
+    while (r >= 8ULL << 30 && to_divide >  1) {

Stray double spaces here and below.

Quoted 15 lines
hw/cadence_ttc.c new file +15 -0
+        r /= 1000;
+        to_divide /= 1000;
+    }
+    r <<= 16;
+    /* keep early-dividing as needed */
+    while (r >= 8ULL << 30 && to_divide >  1) {
+        r /= 1000;
+        to_divide /= 1000;
+    }
+    r *= (uint64_t)s->freq;
+    if (s->reg_clock & CLOCK_CTRL_PS_EN) {
+        r /= 1 << (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
+    }
+
+    r /= to_divide;

hw/cadence_ttc.c line 138

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    return r;
+}
+
+static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
+{
+    if (a < b) {
+        return x > a && x <= b;
+    }
+    return x < a && x >= b;
+}

This looks slightly odd -- should the boundary condition for whether a value equal to the max/min really change depending on whether a or b is greater?

Quoted 171 lines
hw/cadence_ttc.c new file +171 -0
+
+static void cadence_timer_run(CadenceTimerState *s)
+{
+    int i;
+    int64_t event_interval, next_value;
+
+    assert(s->cpu_time_valid); /* cadence_timer_sync must be called first */
+
+    if (s->reg_count & COUNTER_CTRL_DIS) {
+        s->cpu_time_valid = 0;
+        return;
+    }
+
+    { /* figure out whats going to happen next (rollover or match) */
+        int64_t interval = (uint64_t)((s->reg_count & COUNTER_CTRL_INT) ?
+                (int64_t)s->reg_interval + 1 : 0x10000ULL) << 16;
+        next_value = (s->reg_count & COUNTER_CTRL_DEC) ? -1ULL : interval;
+        for (i = 0; i < 3; ++i) {
+            int64_t cand = (uint64_t)s->reg_match[i] << 16;
+            if (is_between(cand, (uint64_t)s->reg_value, next_value)) {
+                next_value = cand;
+            }
+        }
+    }
+    qemu_debug("next timer event value: %09llx\n",
+            (unsigned long long)next_value);
+
+    event_interval = next_value - (int64_t)s->reg_value;
+    event_interval = (event_interval < 0) ? -event_interval : event_interval;
+
+    qemu_mod_timer(s->timer, s->cpu_time +
+                cadence_timer_get_ns(s, event_interval));
+}
+
+static void cadence_timer_sync(CadenceTimerState *s)
+{
+    int i;
+    int64_t r, x;
+    int64_t interval = ((s->reg_count & COUNTER_CTRL_INT) ?
+            (int64_t)s->reg_interval + 1 : 0x10000ULL) << 16;
+    uint64_t old_time = s->cpu_time;
+
+    s->cpu_time = qemu_get_clock_ns(vm_clock);
+    qemu_debug("cpu time: %lld ns\n", (long long)old_time);
+
+    if (!s->cpu_time_valid || old_time == s->cpu_time) {
+        s->cpu_time_valid = 1;
+        return;
+    }
+
+    r = (int64_t)cadence_timer_get_steps(s, s->cpu_time - old_time);
+    x = (int64_t)s->reg_value + ((s->reg_count & COUNTER_CTRL_DEC) ? -r : r);
+
+    for (i = 0; i < 3; ++i) {
+        int64_t m = (int64_t)s->reg_match[i] << 16;
+        if (m > interval) {
+            continue;
+        }
+        if (is_between(m, s->reg_value, x) ||
+            is_between(m + interval, s->reg_value, x) ||
+            is_between(m - interval, s->reg_value, x)) {
+            s->reg_intr |= (2 << i);
+        }
+    }
+
+    s->reg_value = (uint32_t)((x + interval) % interval);
+
+    if (s->reg_value != x) {
+        s->reg_intr |= (s->reg_count & COUNTER_CTRL_INT) ?
+            COUNTER_INTR_IV : COUNTER_INTR_OV;
+    }
+    cadence_timer_update(s);
+}
+
+static void cadence_timer_tick(void *opaque)
+{
+    CadenceTimerState *s = opaque;
+
+    qemu_debug("\n");
+    cadence_timer_sync(s);
+    cadence_timer_run(s);
+}
+
+static uint32_t cadence_ttc_read_imp(void *opaque, target_phys_addr_t offset)
+{
+    CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
+    uint32_t value;
+
+    cadence_timer_sync(s);
+    cadence_timer_run(s);
+
+    switch (offset) {
+    case 0x00: /* clock control */
+    case 0x04:
+    case 0x08:
+        return s->reg_clock;
+
+    case 0x0c: /* counter control */
+    case 0x10:
+    case 0x14:
+        return s->reg_count;
+
+    case 0x18: /* counter value */
+    case 0x1c:
+    case 0x20:
+        return (uint16_t)(s->reg_value >> 16);
+
+    case 0x24: /* reg_interval counter */
+    case 0x28:
+    case 0x2c:
+        return s->reg_interval;
+
+    case 0x30: /* match 1 counter */
+    case 0x34:
+    case 0x38:
+        return s->reg_match[0];
+
+    case 0x3c: /* match 2 counter */
+    case 0x40:
+    case 0x44:
+        return s->reg_match[1];
+
+    case 0x48: /* match 3 counter */
+    case 0x4c:
+    case 0x50:
+        return s->reg_match[2];
+
+    case 0x54: /* interrupt register */
+    case 0x58:
+    case 0x5c:
+        /* cleared after read */
+        value = s->reg_intr;
+        s->reg_intr = 0;
+        return value;
+
+    case 0x60: /* interrupt enable */
+    case 0x64:
+    case 0x68:
+        return s->reg_intr_en;
+
+    case 0x6c:
+    case 0x70:
+    case 0x74:
+        return s->reg_event_ctrl;
+
+    case 0x78:
+    case 0x7c:
+    case 0x80:
+        return s->reg_event;
+
+    default:
+        return 0;
+    }
+}
+
+static uint64_t cadence_ttc_read(void *opaque, target_phys_addr_t offset,
+    unsigned size)
+{
+    uint32_t ret = cadence_ttc_read_imp(opaque, offset);
+
+    qemu_debug("addr: %08x data: %08x\n", offset, ret);
+    return ret;
+}
+
+static void cadence_ttc_write(void *opaque, target_phys_addr_t offset,
+        uint64_t value, unsigned size)
+{
+    CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
+
+    qemu_debug("addr: %08x data %08x\n", offset, (unsigned)value);
+

hw/cadence_ttc.c line 319

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    cadence_timer_sync(s);
+
+    switch (offset) {
+    case 0x00: /* clock control */
+    case 0x04:
+    case 0x08:
+        s->reg_clock = value & 0x3F;
+        break;
+
+    case 0x0c: /* conter control */

"counter"

Quoted 59 lines
hw/cadence_ttc.c new file +59 -0
+    case 0x10:
+    case 0x14:
+        if (value & COUNTER_CTRL_RST) {
+            s->reg_value = 0;
+        }
+        s->reg_count = value & 0x3f & ~COUNTER_CTRL_RST;
+        break;
+
+    case 0x24: /* interval register */
+    case 0x28:
+    case 0x2c:
+        s->reg_interval = value & 0xffff;
+        break;
+
+    case 0x30: /* match register */
+    case 0x34:
+    case 0x38:
+        s->reg_match[0] = value & 0xffff;
+
+    case 0x3c: /* match register */
+    case 0x40:
+    case 0x44:
+        s->reg_match[1] = value & 0xffff;
+
+    case 0x48: /* match register */
+    case 0x4c:
+    case 0x50:
+        s->reg_match[2] = value & 0xffff;
+        break;
+
+    case 0x54: /* interrupt register */
+    case 0x58:
+    case 0x5c:
+        s->reg_intr &= (~value & 0xfff);
+        break;
+
+    case 0x60: /* interrupt enable */
+    case 0x64:
+    case 0x68:
+        s->reg_intr_en = value & 0x3f;
+        break;
+
+    case 0x6c: /* event control */
+    case 0x70:
+    case 0x74:
+        s->reg_event_ctrl = value & 0x07;
+        break;
+
+    default:
+        return;
+    }
+
+    cadence_timer_run(s);
+    cadence_timer_update(s);
+}
+
+static const MemoryRegionOps cadence_ttc_ops = {
+    .read = cadence_ttc_read,
+    .write = cadence_ttc_write,

hw/cadence_ttc.c line 388

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static CadenceTimerState *cadence_timer_init(uint32_t freq)
+{
+    CadenceTimerState *s;
+
+    s = (CadenceTimerState *)g_malloc0(sizeof(CadenceTimerState));
+    s->freq = freq;
+    s->reg_count = 0x21;

Reset function.

Quoted 13 lines
hw/cadence_ttc.c new file +13 -0
+
+    s->timer = qemu_new_timer_ns(vm_clock, cadence_timer_tick, s);
+
+    return s;
+}
+
+static int cadence_ttc_init(SysBusDevice *dev)
+{
+    cadence_ttc_state *s = FROM_SYSBUS(cadence_ttc_state, dev);
+    int i;
+
+    for (i = 0; i < 3; ++i) {
+        s->timer[i] = cadence_timer_init(2500000);

hw/cadence_ttc.c line 411

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+        sysbus_init_irq(dev, &s->timer[i]->irq);
+    }
+
+    memory_region_init_io(&s->iomem, &cadence_ttc_ops, s, "timer", 0x1000);
+    sysbus_init_mmio(dev, &s->iomem);
+
+    return 0;
+}
+
+/* FIMXE: add vmsd support */

Leaving FIXME comments in patches is an invitation to being asked to fix them :-) vmsd shouldn't be very hard.

Quoted 18 lines
hw/cadence_ttc.c new file +18 -0
+
+static const VMStateDescription vmstate_cadence_ttc = {
+    .name = "cadence_TTC",
+    .unmigratable = 1,
+};
+
+static void cadence_ttc_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
+
+    sdc->init = cadence_ttc_init;
+    dc->vmsd = &vmstate_cadence_ttc;
+}
+
+static TypeInfo cadence_ttc_info = {
+    .name  = "cadence_ttc",
+    .parent = TYPE_SYS_BUS_DEVICE,

hw/cadence_ttc.c line 439

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    .instance_size  = sizeof(cadence_ttc_state),
+    .class_init = cadence_ttc_class_init,
+};
+
+static void cadence_ttc_register_types(void)
+{
+    type_register_static(&cadence_ttc_info);
+}
+
+type_init(cadence_ttc_register_types)

-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

Quoted 11 lines

hw/cadence_ttc.c line 138

Quoted 7 lines
hw/cadence_ttc.c new file +7 -0
+static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
+{
+    if (a < b) {
+        return x > a && x <= b;
+    }
+    return x < a && x >= b;
+}

This looks slightly odd -- should the boundary condition for whether a value equal to the max/min really change depending on :whether a or b is greater?

This is a ugly hack. Instead of figuring out whether we have a count-up or count-down timer the code checks for both, and have the "in_between" function magically DTRT. I haven't followed the paths through in enough detail to figure out whether it gets all the corner cases right.

Paul

#7 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

Quoted 11 lines · Paul Brook
Quoted 11 lines

hw/cadence_ttc.c line 138

Quoted 7 lines
hw/cadence_ttc.c new file +7 -0
+static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
+{
+    if (a < b) {
+        return x > a && x <= b;
+    }
+    return x < a && x >= b;
+}

This looks slightly odd -- should the boundary condition for whether a value equal to the max/min really change depending on :whether a or b is greater?

The function determines whether x is in-between a and b exclusive of a, inclusive of b, so it is consistent with itself in that regard.

Quoted 6 lines

This is a ugly hack.  Instead of figuring out whether we have a count-up or count-down timer the code checks for both, and have the "in_between" function magically DTRT.  I haven't followed the paths through in enough detail to figure out whether it gets all the corner cases right.

Is it really a "hack"?? For count up b will always be greater than a, and for count down the reverse. I suppose I could assert these conditions at the call site for peace of mind? The invocation from cadence_timer_run doesn't care whether it is count up of count down, it really does just only care if the match value is in-between the current timer value and the next timer value, which is exactly what this function determines.

Quoted 1 line

Paul

#8 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

Quoted 21 lines · Paul Brook
Quoted 11 lines
Quoted 11 lines

hw/cadence_ttc.c line 138

Quoted 7 lines
hw/cadence_ttc.c new file +7 -0
+static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
+{
+    if (a < b) {
+        return x > a && x <= b;
+    }
+    return x < a && x >= b;
+}

This looks slightly odd -- should the boundary condition for whether a value equal to the max/min really change depending on :whether a or b is greater?

The function determines whether x is in-between a and b exclusive of a, inclusive of b, so it is consistent with itself in that regard.

Quoted 4 lines

This is a ugly hack. Instead of figuring out whether we have a count-up or count-down timer the code checks for both, and have the "in_between" function magically DTRT. I haven't followed the paths through in enough detail to figure out whether it gets all the corner cases right.

Quoted 7 lines

Is it really a "hack"?? For count up b will always be greater than a, and for count down the reverse. I suppose I could assert these conditions at the call site for peace of mind? The invocation from cadence_timer_run doesn't care whether it is count up of count down, it really does just only care if the match value is in-between the current timer value and the next timer value, which is exactly what this function determines.

When you explain it like this, it makes a more sense. But this isn't immediately obvious from the code. It took me at least a couple of readings to figure out what was going on. This is exactly the sort of thing that should be described in comments. A function with a very generic name is used in a way that has fairly subtle implications. There's a good chance someone[1] will come along in a few months/years, reuse this function and "fix" the wierdness at the same time.

Annother non-obvious detail is the way you handle overflow. Specifically you check a range both plus and minus the wrap value before wrapping the final count. This is certainly confusing/surprising when you first encounter it. Very large steps result in overlapping ranges, which triggers [in this case harmless] warning bells.

Thinking about that, I realised why I don't like the following line:

Quoted 1 line

+ s->reg_value = (uint32_t)((x + interval) % interval);

This assumes x > -interval, which is not always true.

Paul

[1] "someone" includes me. After I've forgotten this obscure detail.

#9 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

Quoted 29 lines · Paul Brook
Quoted 21 lines · Paul Brook
Quoted 11 lines
Quoted 11 lines

hw/cadence_ttc.c line 138

Quoted 7 lines
hw/cadence_ttc.c new file +7 -0
+static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
+{
+    if (a < b) {
+        return x > a && x <= b;
+    }
+    return x < a && x >= b;
+}

This looks slightly odd -- should the boundary condition for whether a value equal to the max/min really change depending on :whether a or b is greater?

The function determines whether x is in-between a and b exclusive of a, inclusive of b, so it is consistent with itself in that regard.

Quoted 4 lines

This is a ugly hack.  Instead of figuring out whether we have a count-up or count-down timer the code checks for both, and have the "in_between" function magically DTRT.  I haven't followed the paths through in enough detail to figure out whether it gets all the corner cases right.

Quoted 7 lines

Is it really a "hack"?? For count up b will always be greater than a, and for count down the reverse. I suppose I could assert these conditions at the call site for peace of mind? The invocation from cadence_timer_run doesn't care whether it is count up of count down, it really does just only care if the match value is in-between the current timer value and the next timer value, which is exactly what this function determines.

Quoted 4 lines

When you explain it like this, it makes a more sense.  But this isn't immediately obvious from the code.  It took me at least a couple of readings to figure out what was going on. This is exactly the sort of thing that should be described in comments.

Ok, ill be a little more descriptive :)

A function with a very generic name

Perhaps clarify the whole inclusive a exclusive b in comment?

is used in a

Quoted 10 lines

way that has fairly subtle implications.  There's a good chance someone[1] will come along in a few months/years, reuse this function and "fix" the wierdness at the same time.

Annother non-obvious detail is the way you handle overflow.  Specifically you check a range both plus and minus the wrap value before wrapping the final count.  This is certainly confusing/surprising when you first encounter it. Very large steps result in overlapping ranges, which triggers [in this case harmless] warning bells.

Quoted 5 lines

Thinking about that, I realised why I don't like the following line:

Quoted 1 line

+    s->reg_value = (uint32_t)((x + interval) % interval);

This assumes x > -interval, which is not always true.

This would mean you have wrapped twice or more in one time step, which I am assuming is a fatal error condition, as It means your software has missed interrupts and all sort of race conditions would occur. I would personally prefer to assert !(x < -interval) and have qemu hw_error or something, as in these cases QEMU can just not handle your super quick timer wrap around.

Quoted 4 lines

Paul

[1] "someone" includes me.  After I've forgotten this obscure detail.

#10 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

Heres the diff for proposed comments:

@@ -129,6 +129,8 @@ static uint64_t cadence_timer_get_steps(CadenceTimerState *s, uint64_t ns)

     return r;

}

+/* determine if x is inbetween a and b, exclusive of a, inclusive of b */ +
static inline int64_t is_between(int64_t x, int64_t a, int64_t b)

 {
     if (a < b) {

@@ -188,12 +190,18 @@ static void cadence_timer_sync(CadenceTimerState *s)

     r = (int64_t)cadence_timer_get_steps(s, s->cpu_time - old_time);
     x = (int64_t)s->reg_value + ((s->reg_count & COUNTER_CTRL_DEC) ? -r : r);

+ if (x > 2 * interval || x < -interval) {
+ hw_error("Timer wrapped around twice in one I/O event "
+ "interval (wrap interval set too low / frequency too high)\n");
+ }

     for (i = 0; i < 3; ++i) {
         int64_t m = (int64_t)s->reg_match[i] << 16;
         if (m > interval) {
             continue;
         }
+        /* check to see if match event has occured. check m +/- interval
+         * to account for match events in wrap around cases */
         if (is_between(m, s->reg_value, x) ||
             is_between(m + interval, s->reg_value, x) ||
             is_between(m - interval, s->reg_value, x)) {
Quoted 69 lines · Peter Crosthwaite
Quoted 29 lines · Paul Brook
Quoted 21 lines · Paul Brook
Quoted 11 lines
Quoted 11 lines

hw/cadence_ttc.c line 138

Quoted 7 lines
hw/cadence_ttc.c new file +7 -0
+static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
+{
+    if (a < b) {
+        return x > a && x <= b;
+    }
+    return x < a && x >= b;
+}

This looks slightly odd -- should the boundary condition for whether a value equal to the max/min really change depending on :whether a or b is greater?

The function determines whether x is in-between a and b exclusive of a, inclusive of b, so it is consistent with itself in that regard.

Quoted 4 lines

This is a ugly hack.  Instead of figuring out whether we have a count-up or count-down timer the code checks for both, and have the "in_between" function magically DTRT.  I haven't followed the paths through in enough detail to figure out whether it gets all the corner cases right.

Quoted 7 lines

Is it really a "hack"?? For count up b will always be greater than a, and for count down the reverse. I suppose I could assert these conditions at the call site for peace of mind? The invocation from cadence_timer_run doesn't care whether it is count up of count down, it really does just only care if the match value is in-between the current timer value and the next timer value, which is exactly what this function determines.

Quoted 4 lines

When you explain it like this, it makes a more sense.  But this isn't immediately obvious from the code.  It took me at least a couple of readings to figure out what was going on. This is exactly the sort of thing that should be described in comments.

Ok, ill be a little more descriptive :)

A function with a very generic name

Perhaps clarify the whole inclusive a exclusive b in comment?

is used in a

Quoted 10 lines

way that has fairly subtle implications.  There's a good chance someone[1] will come along in a few months/years, reuse this function and "fix" the wierdness at the same time.

Annother non-obvious detail is the way you handle overflow.  Specifically you check a range both plus and minus the wrap value before wrapping the final count.  This is certainly confusing/surprising when you first encounter it. Very large steps result in overlapping ranges, which triggers [in this case harmless] warning bells.

Quoted 5 lines

Thinking about that, I realised why I don't like the following line:

Quoted 1 line

+    s->reg_value = (uint32_t)((x + interval) % interval);

This assumes x > -interval, which is not always true.

This would mean you have wrapped twice or more in one time step, which I am assuming is a fatal error condition, as It means your software has missed interrupts and all sort of race conditions would occur. I would personally prefer to assert !(x < -interval) and have qemu hw_error or something, as in these cases QEMU can just not handle your super quick timer wrap around.

Quoted 4 lines

Paul

[1] "someone" includes me.  After I've forgotten this obscure detail.

#11 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

In reply to Peter Crosthwaite
Quoted 11 lines
Quoted 4 lines

Thinking about that, I realised why I don't like the following line:

Quoted 1 line

+ s->reg_value = (uint32_t)((x + interval) % interval);

This assumes x > -interval, which is not always true.

This would mean you have wrapped twice or more in one time step, which I am assuming is a fatal error condition, as It means your software has missed interrupts and all sort of race conditions would occur. I would personally prefer to assert !(x < -interval) and have qemu hw_error or something, as in these cases QEMU can just not handle your super quick timer wrap around.

No, not fatal at all. On a heavily loaded host it's normal for qemu[1] to stall for tens of miliseconds at a time. In extreme cases several seconds at a time, though we've fixed most of those. This is greater than the interval of many guest periodic events. A linux guest with HZ=1000 (even HZ=100) will routinely loose timer ticks. By my reading your timers have a configurable limit, so the obvious configuration is a limit of 25000 (2.5MHz / 100), and trigger the jiffy tick off the timer wrap/reload.

If your guest OS assumes their ISR will complete before timer triggers again then it will break. That's their problem. Any guest that relies on consistent realtime performance/behavior is going to malfunction when run inside qemu. This is only one of several ways that qemu behavior can differ from that of real hardware.

You can workaround some of these issues with -icount, though that has its own set of problems. One of which is that is doesn't support KVM or SMP[2]. guests.

Paul

[1] The same applies for KVM.
[2] SMP may be fixable. KVM probably not.

#12 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

Quoted 12 lines · Paul Brook
Quoted 11 lines
Quoted 4 lines

Thinking about that, I realised why I don't like the following line:

Quoted 1 line

+    s->reg_value = (uint32_t)((x + interval) % interval);

This assumes x > -interval, which is not always true.

This would mean you have wrapped twice or more in one time step, which I am assuming is a fatal error condition, as It means your software has missed interrupts and all sort of race conditions would occur. I would personally prefer to assert !(x < -interval) and have qemu hw_error or something, as in these cases QEMU can just not handle your super quick timer wrap around.

Quoted 8 lines

No, not fatal at all.  On a heavily loaded host it's normal for qemu[1] to stall for tens of miliseconds at a time.  In extreme cases several seconds at a time, though we've fixed most of those.  This is greater than the interval of many guest periodic events.  A linux guest with HZ=1000 (even HZ=100) will routinely loose timer ticks.  By my reading your timers have a configurable limit, so the obvious configuration is a limit of 25000 (2.5MHz / 100), and trigger the jiffy tick off the timer wrap/reload.

Yeh i discovered that in testing :) my hw_error did trigger under linux. Fixed that % issue you raised (in v8) such this it will not % a -ve number when the timer multi-wraps.

Quoted 6 lines

If your guest OS assumes their ISR will complete before timer triggers again then it will break.  That's their problem.  Any guest that relies on consistent realtime performance/behavior is going to malfunction when run inside qemu.  This is only one of several ways that qemu behavior can differ from that of real hardware.

Quoted 8 lines

You can workaround some of these issues with -icount, though that has its own set of problems.  One of which is that is doesn't support KVM or SMP[2]. guests.

Paul

[1] The same applies for KVM.
[2] SMP may be fixable.  KVM probably not.

Peter

#13 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model

In reply to Peter Maydell
Quoted 154 lines · Peter Maydell
Quoted 101 lines · Peter A. G. Crosthwaite

Implemented cadence Triple Timer Counter (TCC)

---
changed from v4:
fixed FSF addess
changed device_init -> type_init
changed from v3:
Fixed race condition where timer could miss match events on wrap around changed from v2:
changed ptimer to QEMUTimer (Fixed skew/drift issue in timer delays) changes from v1:
refactored event driven code
marked vmsd as unmigratable

 Makefile.target  |    1 +
 hw/cadence_ttc.c |  439 ++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 440 insertions(+), 0 deletions(-)

 create mode 100644 hw/cadence_ttc.c

Makefile.target +0 -0
index 868be15..e96b37e 100644
@@ -344,6 +344,7 @@ obj-arm-y = integratorcp.o versatilepb.o arm_pic.o arm_timer.o

 obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o obj-arm-y += versatile_pci.o
 obj-arm-y += cadence_uart.o
+obj-arm-y += cadence_ttc.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o

hw/cadence_ttc.c new file +62 -0
new file mode 100644
index 0000000..14c3ba7
@@ -0,0 +1,439 @@
+/*
+ * Xilinx Zynq cadence TTC model
+ *
+ * Copyright (c) 2011 Xilinx Inc.
+ * Copyright (c) 2012 Peter A.G. Crosthwaite ([email protected])
+ * Copyright (c) 2012 PetaLogix Pty Ltd.
+ * Written By Haibing Ma
+ *            M. Habib
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "sysbus.h"
+#include "qemu-timer.h"
+
+#ifdef CADENCE_TTC_ERR_DEBUG
+#define qemu_debug(...) do { \
+    fprintf(stderr,  ": %s: ", __func__); \
+    fprintf(stderr, ## __VA_ARGS__); \
+    fflush(stderr); \
+    } while (0);
+#else
+    #define qemu_debug(...)
+#endif
+
+#define COUNTER_INTR_IV     0x00000001
+#define COUNTER_INTR_M1     0x00000002
+#define COUNTER_INTR_M2     0x00000004
+#define COUNTER_INTR_M3     0x00000008
+#define COUNTER_INTR_OV     0x00000010
+#define COUNTER_INTR_EV     0x00000020
+
+#define COUNTER_CTRL_DIS    0x00000001
+#define COUNTER_CTRL_INT    0x00000002
+#define COUNTER_CTRL_DEC    0x00000004
+#define COUNTER_CTRL_MATCH  0x00000008
+#define COUNTER_CTRL_RST    0x00000010
+
+#define CLOCK_CTRL_PS_EN    0x00000001
+#define CLOCK_CTRL_PS_V     0x0000001e
+
+typedef struct {
+    QEMUTimer *timer;
+    int freq;
+
+    uint32_t reg_clock;
+    uint32_t reg_count;
+    uint32_t reg_value;
+    uint16_t reg_interval;
+    uint16_t reg_match[3];
+    uint32_t reg_intr;
+    uint32_t reg_intr_en;
+    uint32_t reg_event_ctrl;
+    uint32_t reg_event;
+
+    uint64_t cpu_time;

hw/cadence_ttc.c line 72

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    unsigned int cpu_time_valid;
+
+    qemu_irq irq;
+} CadenceTimerState;
+
+typedef struct {
+    SysBusDevice busdev;
+    MemoryRegion iomem;
+    CadenceTimerState * timer[3];
+} cadence_ttc_state;

This type name is in breach of the coding style, which demands camelcase.

hw/cadence_ttc.c line 85

Quoted 13 lines
hw/cadence_ttc.c new file +13 -0
+
+static void cadence_timer_update(CadenceTimerState *s)
+{
+    qemu_set_irq(s->irq, !!(s->reg_intr & s->reg_intr_en));
+}
+
+static CadenceTimerState *cadence_timer_from_addr(void *opaque,
+                                        target_phys_addr_t offset)
+{
+    unsigned int index;
+    cadence_ttc_state *s = (cadence_ttc_state *)opaque;
+
+    index = (offset >> 2) % 3;

Really % 3 ? This must be a pain to implement in hardware...

Quoted 21 lines
hw/cadence_ttc.c new file +21 -0
+    return s->timer[index];
+}
+
+static uint64_t cadence_timer_get_ns(CadenceTimerState *s, uint64_t timer_steps)
+{
+    /* timer_steps has max value of 0x100000000. double check it
+     * (or overflow can happen below) */
+    assert(timer_steps <= 1ULL << 32);
+
+    uint64_t r = timer_steps * 1000000000ULL;
+    if (s->reg_clock & CLOCK_CTRL_PS_EN) {
+        r >>= 16 - (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
+    } else {
+        r >>= 16;
+    }
+    r /= (uint64_t)s->freq;
+    return r;
+}
+
+static uint64_t cadence_timer_get_steps(CadenceTimerState *s, uint64_t ns)
+{
Quoted 9 lines

hw/cadence_ttc.c line 113

Quoted 6 lines
hw/cadence_ttc.c new file +6 -0
+    uint64_t to_divide = 1000000000ULL;
+
+    uint64_t r = ns;
+     /* for very large intervals (> 8s) do some division first to stop
+      * overflow (costs some prescision) */
+    while (r >= 8ULL << 30 && to_divide >  1) {

Stray double spaces here and below.

Fixed

Quoted 205 lines
Quoted 15 lines
hw/cadence_ttc.c new file +15 -0
+        r /= 1000;
+        to_divide /= 1000;
+    }
+    r <<= 16;
+    /* keep early-dividing as needed */
+    while (r >= 8ULL << 30 && to_divide >  1) {
+        r /= 1000;
+        to_divide /= 1000;
+    }
+    r *= (uint64_t)s->freq;
+    if (s->reg_clock & CLOCK_CTRL_PS_EN) {
+        r /= 1 << (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
+    }
+
+    r /= to_divide;

hw/cadence_ttc.c line 138

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    return r;
+}
+
+static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
+{
+    if (a < b) {
+        return x > a && x <= b;
+    }
+    return x < a && x >= b;
+}

This looks slightly odd -- should the boundary condition for whether a value equal to the max/min really change depending on whether a or b is greater?

Quoted 175 lines
hw/cadence_ttc.c new file +175 -0
+
+static void cadence_timer_run(CadenceTimerState *s)
+{
+    int i;
+    int64_t event_interval, next_value;
+
+    assert(s->cpu_time_valid); /* cadence_timer_sync must be called first */
+
+    if (s->reg_count & COUNTER_CTRL_DIS) {
+        s->cpu_time_valid = 0;
+        return;
+    }
+
+    { /* figure out whats going to happen next (rollover or match) */
+        int64_t interval = (uint64_t)((s->reg_count & COUNTER_CTRL_INT) ?
+                (int64_t)s->reg_interval + 1 : 0x10000ULL) << 16;
+        next_value = (s->reg_count & COUNTER_CTRL_DEC) ? -1ULL : interval;
+        for (i = 0; i < 3; ++i) {
+            int64_t cand = (uint64_t)s->reg_match[i] << 16;
+            if (is_between(cand, (uint64_t)s->reg_value, next_value)) {
+                next_value = cand;
+            }
+        }
+    }
+    qemu_debug("next timer event value: %09llx\n",
+            (unsigned long long)next_value);
+
+    event_interval = next_value - (int64_t)s->reg_value;
+    event_interval = (event_interval < 0) ? -event_interval : event_interval;
+
+    qemu_mod_timer(s->timer, s->cpu_time +
+                cadence_timer_get_ns(s, event_interval));
+}
+
+static void cadence_timer_sync(CadenceTimerState *s)
+{
+    int i;
+    int64_t r, x;
+    int64_t interval = ((s->reg_count & COUNTER_CTRL_INT) ?
+            (int64_t)s->reg_interval + 1 : 0x10000ULL) << 16;
+    uint64_t old_time = s->cpu_time;
+
+    s->cpu_time = qemu_get_clock_ns(vm_clock);
+    qemu_debug("cpu time: %lld ns\n", (long long)old_time);
+
+    if (!s->cpu_time_valid || old_time == s->cpu_time) {
+        s->cpu_time_valid = 1;
+        return;
+    }
+
+    r = (int64_t)cadence_timer_get_steps(s, s->cpu_time - old_time);
+    x = (int64_t)s->reg_value + ((s->reg_count & COUNTER_CTRL_DEC) ? -r : r);
+
+    for (i = 0; i < 3; ++i) {
+        int64_t m = (int64_t)s->reg_match[i] << 16;
+        if (m > interval) {
+            continue;
+        }
+        if (is_between(m, s->reg_value, x) ||
+            is_between(m + interval, s->reg_value, x) ||
+            is_between(m - interval, s->reg_value, x)) {
+            s->reg_intr |= (2 << i);
+        }
+    }
+
+    s->reg_value = (uint32_t)((x + interval) % interval);
+
+    if (s->reg_value != x) {
+        s->reg_intr |= (s->reg_count & COUNTER_CTRL_INT) ?
+            COUNTER_INTR_IV : COUNTER_INTR_OV;
+    }
+    cadence_timer_update(s);
+}
+
+static void cadence_timer_tick(void *opaque)
+{
+    CadenceTimerState *s = opaque;
+
+    qemu_debug("\n");
+    cadence_timer_sync(s);
+    cadence_timer_run(s);
+}
+
+static uint32_t cadence_ttc_read_imp(void *opaque, target_phys_addr_t offset)
+{
+    CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
+    uint32_t value;
+
+    cadence_timer_sync(s);
+    cadence_timer_run(s);
+
+    switch (offset) {
+    case 0x00: /* clock control */
+    case 0x04:
+    case 0x08:
+        return s->reg_clock;
+
+    case 0x0c: /* counter control */
+    case 0x10:
+    case 0x14:
+        return s->reg_count;
+
+    case 0x18: /* counter value */
+    case 0x1c:
+    case 0x20:
+        return (uint16_t)(s->reg_value >> 16);
+
+    case 0x24: /* reg_interval counter */
+    case 0x28:
+    case 0x2c:
+        return s->reg_interval;
+
+    case 0x30: /* match 1 counter */
+    case 0x34:
+    case 0x38:
+        return s->reg_match[0];
+
+    case 0x3c: /* match 2 counter */
+    case 0x40:
+    case 0x44:
+        return s->reg_match[1];
+
+    case 0x48: /* match 3 counter */
+    case 0x4c:
+    case 0x50:
+        return s->reg_match[2];
+
+    case 0x54: /* interrupt register */
+    case 0x58:
+    case 0x5c:
+        /* cleared after read */
+        value = s->reg_intr;
+        s->reg_intr = 0;
+        return value;
+
+    case 0x60: /* interrupt enable */
+    case 0x64:
+    case 0x68:
+        return s->reg_intr_en;
+
+    case 0x6c:
+    case 0x70:
+    case 0x74:
+        return s->reg_event_ctrl;
+
+    case 0x78:
+    case 0x7c:
+    case 0x80:
+        return s->reg_event;
+
+    default:
+        return 0;
+    }
+}
+
+static uint64_t cadence_ttc_read(void *opaque, target_phys_addr_t offset,
+    unsigned size)
+{
+    uint32_t ret = cadence_ttc_read_imp(opaque, offset);
+
+    qemu_debug("addr: %08x data: %08x\n", offset, ret);
+    return ret;
+}
+
+static void cadence_ttc_write(void *opaque, target_phys_addr_t offset,
+        uint64_t value, unsigned size)
+{
+    CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
+
+    qemu_debug("addr: %08x data %08x\n", offset, (unsigned)value);
+
+    cadence_timer_sync(s);
+
+    switch (offset) {
+    case 0x00: /* clock control */
Quoted 9 lines

hw/cadence_ttc.c line 319

Quoted 6 lines
hw/cadence_ttc.c new file +6 -0
+    case 0x04:
+    case 0x08:
+        s->reg_clock = value & 0x3F;
+        break;
+
+    case 0x0c: /* conter control */

"counter"

Fixed

Quoted 63 lines
Quoted 63 lines
hw/cadence_ttc.c new file +63 -0
+    case 0x10:
+    case 0x14:
+        if (value & COUNTER_CTRL_RST) {
+            s->reg_value = 0;
+        }
+        s->reg_count = value & 0x3f & ~COUNTER_CTRL_RST;
+        break;
+
+    case 0x24: /* interval register */
+    case 0x28:
+    case 0x2c:
+        s->reg_interval = value & 0xffff;
+        break;
+
+    case 0x30: /* match register */
+    case 0x34:
+    case 0x38:
+        s->reg_match[0] = value & 0xffff;
+
+    case 0x3c: /* match register */
+    case 0x40:
+    case 0x44:
+        s->reg_match[1] = value & 0xffff;
+
+    case 0x48: /* match register */
+    case 0x4c:
+    case 0x50:
+        s->reg_match[2] = value & 0xffff;
+        break;
+
+    case 0x54: /* interrupt register */
+    case 0x58:
+    case 0x5c:
+        s->reg_intr &= (~value & 0xfff);
+        break;
+
+    case 0x60: /* interrupt enable */
+    case 0x64:
+    case 0x68:
+        s->reg_intr_en = value & 0x3f;
+        break;
+
+    case 0x6c: /* event control */
+    case 0x70:
+    case 0x74:
+        s->reg_event_ctrl = value & 0x07;
+        break;
+
+    default:
+        return;
+    }
+
+    cadence_timer_run(s);
+    cadence_timer_update(s);
+}
+
+static const MemoryRegionOps cadence_ttc_ops = {
+    .read = cadence_ttc_read,
+    .write = cadence_ttc_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static CadenceTimerState *cadence_timer_init(uint32_t freq)
Quoted 9 lines

hw/cadence_ttc.c line 388

Quoted 6 lines
hw/cadence_ttc.c new file +6 -0
+{
+    CadenceTimerState *s;
+
+    s = (CadenceTimerState *)g_malloc0(sizeof(CadenceTimerState));
+    s->freq = freq;
+    s->reg_count = 0x21;

Reset function.

Fixed

Quoted 57 lines
Quoted 13 lines
hw/cadence_ttc.c new file +13 -0
+
+    s->timer = qemu_new_timer_ns(vm_clock, cadence_timer_tick, s);
+
+    return s;
+}
+
+static int cadence_ttc_init(SysBusDevice *dev)
+{
+    cadence_ttc_state *s = FROM_SYSBUS(cadence_ttc_state, dev);
+    int i;
+
+    for (i = 0; i < 3; ++i) {
+        s->timer[i] = cadence_timer_init(2500000);

hw/cadence_ttc.c line 411

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+        sysbus_init_irq(dev, &s->timer[i]->irq);
+    }
+
+    memory_region_init_io(&s->iomem, &cadence_ttc_ops, s, "timer", 0x1000);
+    sysbus_init_mmio(dev, &s->iomem);
+
+    return 0;
+}
+
+/* FIMXE: add vmsd support */

Leaving FIXME comments in patches is an invitation to being asked to fix them :-) vmsd shouldn't be very hard.

Quoted 18 lines
hw/cadence_ttc.c new file +18 -0
+
+static const VMStateDescription vmstate_cadence_ttc = {
+    .name = "cadence_TTC",
+    .unmigratable = 1,
+};
+
+static void cadence_ttc_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
+
+    sdc->init = cadence_ttc_init;
+    dc->vmsd = &vmstate_cadence_ttc;
+}
+
+static TypeInfo cadence_ttc_info = {
+    .name  = "cadence_ttc",
+    .parent = TYPE_SYS_BUS_DEVICE,

hw/cadence_ttc.c line 439

Quoted 10 lines
hw/cadence_ttc.c new file +10 -0
+    .instance_size  = sizeof(cadence_ttc_state),
+    .class_init = cadence_ttc_class_init,
+};
+
+static void cadence_ttc_register_types(void)
+{
+    type_register_static(&cadence_ttc_info);
+}
+
+type_init(cadence_ttc_register_types)

-- PMM

#14 lore raw

PATCH v6 3/4 cadence_gem: initial version of device model

In reply to Peter A. G. Crosthwaite

Device model for cadence gem ethernet controller.

---
changed from v4:
changed device_init -> type_init
changes from v1:
removed global init function
marked vmsd as unmigratable
cleaned up debug messages

 Makefile.target  |    1 +
 hw/cadence_gem.c | 1229 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1230 insertions(+), 0 deletions(-)
 create mode 100644 hw/cadence_gem.c
Makefile.target +1 -0
index e96b37e..df13f49 100644
@@ -345,6 +345,7 @@ obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o
 obj-arm-y += versatile_pci.o
 obj-arm-y += cadence_uart.o
 obj-arm-y += cadence_ttc.o
+obj-arm-y += cadence_gem.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o
 obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o
 obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o
hw/cadence_gem.c new file +1229 -0

1,229 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#15 lore raw

PATCH v6 4/4 xilinx_zynq: machine model initial version

In reply to Peter A. G. Crosthwaite

Xilinx zynq-7000 machine model. Also includes device model for the zynq-specific system level control register (SLCR) module.

---
changed from v5:
removed usages of arm_boot_info->bootsmp_priv_base changed from v4:
fixed FSF addess
changed device_init -> type_init
changes since v2:
removed 2 compile warnings from zynq_arm_sysctl.c changes since v1:
Added gem init function
remowed WDT instantiation
Added maintainers information
removed dead sys_id and proc_id variables

 MAINTAINERS          |    5 +
 Makefile.target      |    1 +
 hw/xilinx_zynq.c     |  173 ++++++++++++++++
 hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 711 insertions(+), 0 deletions(-)
 create mode 100644 hw/xilinx_zynq.c
 create mode 100644 hw/zynq_arm_sysctl.c
MAINTAINERS +5 -0
index 647c413..ede335c 100644
@@ -251,6 +251,11 @@ M: Peter Maydell <[email protected]>
 S: Maintained
 F: hw/versatilepb.c
 
+Xilinx Zynq
+M: Peter Crosthwaite <[email protected]>
+S: Maintained
+F: hw/xilinx_zynq.c
+
 CRIS Machines
 -------------
 Axis Dev88
Makefile.target +1 -0
index df13f49..fd2eb55 100644
@@ -346,6 +346,7 @@ obj-arm-y += versatile_pci.o
 obj-arm-y += cadence_uart.o
 obj-arm-y += cadence_ttc.o
 obj-arm-y += cadence_gem.o
+obj-arm-y += xilinx_zynq.o zynq_arm_sysctl.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o
 obj-arm-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o
 obj-arm-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o
hw/xilinx_zynq.c new file +173 -0

173 lines not on this page · raw message

hw/zynq_arm_sysctl.c new file +532 -0

532 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#16 lore raw
In reply to Peter A. G. Crosthwaite

Forgot to update branch-name, should read:

Tree is available from:
git://developer.petalogix.com/private/peterc/qemu.git
branch: zynq-initial.6

Quoted 67 lines · Peter A. G. Crosthwaite

This is a suite of Device models and a machine model for the Xilinx Zynq-7000 Extensible Processing Platform:

http://www.xilinx.com/products/silicon-devices/epp/zynq-7000/index.htm

This is an ARM based platform featuring embedded SoC peripherals. This patch series includes a minimal set of device models and a a machine model capable of emulating zynq platforms booting linux.

This first 3 patches in this series are device models for IP provided by cadence for the Zynq platform. The final patch is the initial revision of the zynq machine model.

Most of this work was originally authored by Xilinx, as indicated by (c) notices in added files.

Tree is available from:
git://developer.petalogix.com/private/peterc/qemu.git
branch: zynq-initial.5

---
changed from v5:
removed usages of arm_boot_info->bootsmp_priv_base (4/4) changed from v4:
fixed FSF addess (1/4) (2/4) (4/4)
changed device_init -> type_init (all)
changed from v3:
fixed timer race condition issue (2/4)
changed from v2:
fixed timer prescision issue (2/4)
fixed compile warnings in zynq_arm_sysctl (4/4)
changes from v1:
formatting and style fixes
updated for QOM
removed former patch 3 (cadence WDT device model) - not required removed former patch 5 (dtb argument) - this is currently under discussion in other patch series'
removed former patch 6 (initrd parameterisation) - not required for minimal boot

Peter A. G. Crosthwaite (4):
cadence_uart: initial version of device model
cadence_ttc: initial version of device model
cadence_gem: initial version of device model
xilinx_zynq: machine model initial version

 MAINTAINERS          |    5 +
 Makefile.target      |    4 +
 hw/cadence_gem.c     | 1229
++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/cadence_ttc.c     |  439 ++++++++++++++++++
 hw/cadence_uart.c    |  559 +++++++++++++++++++++++
 hw/xilinx_zynq.c     |  173 +++++++
 hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++
 7 files changed, 2941 insertions(+), 0 deletions(-)
 create mode 100644 hw/cadence_gem.c
 create mode 100644 hw/cadence_ttc.c
 create mode 100644 hw/cadence_uart.c
 create mode 100644 hw/xilinx_zynq.c
 create mode 100644 hw/zynq_arm_sysctl.c

--
1.7.3.2

#17 lore raw
In reply to Peter A. G. Crosthwaite

Pull Request for Zynq-7000 platform model initial support.

The following changes since commit 99c7f87826337fa81f2f0f9baa9ca0a44faf90e9:

input: send kbd+mouse events only to running guests. (2012-02-17
11:02:55 -0600)

are available in the git repository at:
git://developer.petalogix.com/private/peterc/qemu.git zynq-initial.6

Peter A. G. Crosthwaite (4):
cadence_uart: initial version of device model
cadence_ttc: initial version of device model
cadence_gem: initial version of device model
xilinx_zynq: machine model initial version

 MAINTAINERS          |    5 +
 Makefile.target      |    4 +
 hw/cadence_gem.c     | 1229 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/cadence_ttc.c     |  439 ++++++++++++++++++
 hw/cadence_uart.c    |  559 +++++++++++++++++++++++
 hw/xilinx_zynq.c     |  173 +++++++
 hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++
Quoted 57 lines · Peter A. G. Crosthwaite

This is a suite of Device models and a machine model for the Xilinx Zynq-7000 Extensible Processing Platform:

http://www.xilinx.com/products/silicon-devices/epp/zynq-7000/index.htm

This is an ARM based platform featuring embedded SoC peripherals. This patch series includes a minimal set of device models and a a machine model capable of emulating zynq platforms booting linux.

This first 3 patches in this series are device models for IP provided by cadence for the Zynq platform. The final patch is the initial revision of the zynq machine model.

Most of this work was originally authored by Xilinx, as indicated by (c) notices in added files.

Tree is available from:
git://developer.petalogix.com/private/peterc/qemu.git
branch: zynq-initial.5

---
changed from v5:
removed usages of arm_boot_info->bootsmp_priv_base (4/4)
changed from v4:
fixed FSF addess (1/4) (2/4) (4/4)
changed device_init -> type_init (all)
changed from v3:
fixed timer race condition issue (2/4)
changed from v2:
fixed timer prescision issue (2/4)
fixed compile warnings in zynq_arm_sysctl (4/4)
changes from v1:
formatting and style fixes
updated for QOM
removed former patch 3 (cadence WDT device model) - not required
removed former patch 5 (dtb argument) - this is currently under discussion in other patch series' removed former patch 6 (initrd parameterisation) - not required for minimal boot

Peter A. G. Crosthwaite (4):
 cadence_uart: initial version of device model
 cadence_ttc: initial version of device model
 cadence_gem: initial version of device model
 xilinx_zynq: machine model initial version

 MAINTAINERS          |    5 +
 Makefile.target      |    4 +

 hw/cadence_gem.c     | 1229 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/cadence_ttc.c     |  439 ++++++++++++++++++
 hw/cadence_uart.c    |  559 +++++++++++++++++++++++
 hw/xilinx_zynq.c     |  173 +++++++
 hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++
 7 files changed, 2941 insertions(+), 0 deletions(-)

 create mode 100644 hw/cadence_gem.c
 create mode 100644 hw/cadence_ttc.c
 create mode 100644 hw/cadence_uart.c
 create mode 100644 hw/xilinx_zynq.c
 create mode 100644 hw/zynq_arm_sysctl.c

--
1.7.3.2

#18 lore raw

Am 20.02.2012 07:25, schrieb Peter Crosthwaite:

Quoted 9 lines

Pull Request for Zynq-7000 platform model initial support.

The following changes since commit 99c7f87826337fa81f2f0f9baa9ca0a44faf90e9:

input: send kbd+mouse events only to running guests. (2012-02-17
11:02:55 -0600)

are available in the git repository at:
git://developer.petalogix.com/private/peterc/qemu.git zynq-initial.6

Five hours between v6 and PULL are not really much for review, especially when Europe is sleeping. There's no Acked-by or Reviewed-by tag at all, not even Edgar's from v5.
Usually the ARM patches get picked up by Peter Maydell and then he sends the PULL.

Me, I'm still missing a MAINTAINERS entry for hw/zynq_arm_sysctl.c and hw/cadence_*. But that could be added as a follow-up patch.

/-F

Quoted 14 lines

Peter A. G. Crosthwaite (4):
cadence_uart: initial version of device model
cadence_ttc: initial version of device model
cadence_gem: initial version of device model
xilinx_zynq: machine model initial version

 MAINTAINERS          |    5 +
 Makefile.target      |    4 +
 hw/cadence_gem.c     | 1229 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/cadence_ttc.c     |  439 ++++++++++++++++++
 hw/cadence_uart.c    |  559 +++++++++++++++++++++++
 hw/xilinx_zynq.c     |  173 +++++++
 hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#19 lore raw
In reply to Peter Crosthwaite
Quoted 16 lines · Peter Crosthwaite

Pull Request for Zynq-7000 platform model initial support.

The following changes since commit 99c7f87826337fa81f2f0f9baa9ca0a44faf90e9:

input: send kbd+mouse events only to running guests. (2012-02-17
11:02:55 -0600)

are available in the git repository at:
git://developer.petalogix.com/private/peterc/qemu.git zynq-initial.6

Peter A. G. Crosthwaite (4):
cadence_uart: initial version of device model
cadence_ttc: initial version of device model
cadence_gem: initial version of device model
xilinx_zynq: machine model initial version

Quoted 7 lines
  MAINTAINERS          |    5 +
  Makefile.target      |    4 +
  hw/cadence_gem.c     | 1229 ++++++++++++++++++++++++++++++++++++++++++++++++++
  hw/cadence_ttc.c     |  439 ++++++++++++++++++
  hw/cadence_uart.c    |  559 +++++++++++++++++++++++
  hw/xilinx_zynq.c     |  173 +++++++
  hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++

I'd prefer this comes through Peter's tree for now. If Peter wants to pull from you into his tree, I'm okay with that.

Regards,

Anthony Liguori

#20 lore raw
Quoted 29 lines · Anthony Liguori
Quoted 18 lines · Peter Crosthwaite

Pull Request for Zynq-7000 platform model initial support.

The following changes since commit
99c7f87826337fa81f2f0f9baa9ca0a44faf90e9:

  input: send kbd+mouse events only to running guests. (2012-02-17 11:02:55 -0600)

are available in the git repository at:
  git://developer.petalogix.com/private/peterc/qemu.git zynq-initial.6

Peter A. G. Crosthwaite (4):
      cadence_uart: initial version of device model
      cadence_ttc: initial version of device model
      cadence_gem: initial version of device model
      xilinx_zynq: machine model initial version

Quoted 8 lines

 MAINTAINERS          |    5 +
 Makefile.target      |    4 +
 hw/cadence_gem.c     | 1229
++++++++++++++++++++++++++++++++++++++++++++++++++

 hw/cadence_ttc.c     |  439 ++++++++++++++++++
 hw/cadence_uart.c    |  559 +++++++++++++++++++++++
 hw/xilinx_zynq.c     |  173 +++++++

 hw/zynq_arm_sysctl.c |  532 ++++++++++++++++++++++

Quoted 3 lines

I'd prefer this comes through Peter's tree for now.  If Peter wants to pull from you into his tree, I'm okay with that.

Ok. v7 is up on the mailing list and addresses the bulk of Peters comments re v6. Awaiting further review.

Quoted 3 lines

Regards,

Anthony Liguori

Regards,
Peter