Skip to contentSkip to conversation

nios2: Add disas entries

PATCH Superseded Marek Vasut 3 participants 27 messages Jul 28, 2016 – Aug 19, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/7 nios2: Add disas entries

Add nios2 disassembler support. This patch is composed from binutils files from commit "Opcodes and assembler support for Nios II R2". The files from binutils used in this patch are:

include/opcode/nios2.h
include/opcode/nios2r1.h
include/opcode/nios2r2.h
opcodes/nios2-opc.c
opcodes/nios2-dis.c

Cc Chris Wulff <[email protected]>
Cc Jeff Da Silva <[email protected]>
Cc Ley Foon Tan <[email protected]>
Cc Sandra Loosemore <[email protected]>
Cc Yves Vandervennet <[email protected]>

---
V2: Replace the nios2.c with GPL2 licensed version

---
 disas/Makefile.objs |    1 +
 disas/nios2.c       | 3534 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/disas/bfd.h |    6 +
 3 files changed, 3541 insertions(+)
 create mode 100644 disas/nios2.c
disas/Makefile.objs +1 -0
index abeba84..62632ef 100644
@@ -15,6 +15,7 @@ common-obj-$(CONFIG_IA64_DIS) += ia64.o
 common-obj-$(CONFIG_M68K_DIS) += m68k.o
 common-obj-$(CONFIG_MICROBLAZE_DIS) += microblaze.o
 common-obj-$(CONFIG_MIPS_DIS) += mips.o
+common-obj-$(CONFIG_NIOS2_DIS) += nios2.o
 common-obj-$(CONFIG_MOXIE_DIS) += moxie.o
 common-obj-$(CONFIG_PPC_DIS) += ppc.o
 common-obj-$(CONFIG_S390_DIS) += s390.o
disas/nios2.c new file +3534 -0

2,993 lines not on this page · raw message

include/disas/bfd.h +6 -0
index 8a3488c..0435b8c 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.8.1

Part of the diff is not shown here. View the raw message.

#1 lore raw

PATCH 2/7 nios2: Add architecture emulation support

From: Chris Wulff <[email protected]>

Add support for emulating Altera NiosII R1 architecture into qemu. This patch is based on previous work by Chris Wulff from 2012 and updated to latest mainline QEMU.

Cc Chris Wulff <[email protected]>
Cc Jeff Da Silva <[email protected]>
Cc Ley Foon Tan <[email protected]>
Cc Sandra Loosemore <[email protected]>
Cc Yves Vandervennet <[email protected]>
---
 target-nios2/Makefile.objs |    4 +
 target-nios2/cpu.c         |  229 +++++++
 target-nios2/cpu.h         |  265 ++++++++
 target-nios2/helper.c      |  304 ++++++++++
 target-nios2/helper.h      |   41 ++
 target-nios2/instruction.c | 1427 ++++++++++++++++++++++++++++++++++++++++++++
 target-nios2/instruction.h |  279 +++++++++
 target-nios2/machine.c     |   38 ++
 target-nios2/mmu.c         |  292 +++++++++
 target-nios2/mmu.h         |   54 ++
 target-nios2/monitor.c     |   35 ++
 target-nios2/op_helper.c   |   86 +++
 target-nios2/translate.c   |  242 ++++++++
 13 files changed, 3296 insertions(+)
 create mode 100644 target-nios2/Makefile.objs
 create mode 100644 target-nios2/cpu.c
 create mode 100644 target-nios2/cpu.h
 create mode 100644 target-nios2/helper.c
 create mode 100644 target-nios2/helper.h
 create mode 100644 target-nios2/instruction.c
 create mode 100644 target-nios2/instruction.h
 create mode 100644 target-nios2/machine.c
 create mode 100644 target-nios2/mmu.c
 create mode 100644 target-nios2/mmu.h
 create mode 100644 target-nios2/monitor.c
 create mode 100644 target-nios2/op_helper.c
 create mode 100644 target-nios2/translate.c
target-nios2/Makefile.objs new file +4 -0
new file mode 100644
index 0000000..ea6a528
@@ -0,0 +1,4 @@
+obj-y += translate.o op_helper.o helper.o cpu.o mmu.o instruction.o
+obj-$(CONFIG_SOFTMMU) += monitor.o
+
+$(obj)/op_helper.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
target-nios2/cpu.c new file +229 -0

229 lines not on this page · raw message

target-nios2/cpu.h new file +265 -0

265 lines not on this page · raw message

target-nios2/helper.c new file +304 -0

304 lines not on this page · raw message

target-nios2/helper.h new file +41 -0

41 lines not on this page · raw message

target-nios2/instruction.c new file +1427 -0

1,427 lines not on this page · raw message

target-nios2/instruction.h new file +279 -0

279 lines not on this page · raw message

target-nios2/machine.c new file +38 -0
new file mode 100644
index 0000000..da2cb04
@@ -0,0 +1,38 @@
+/*
+ * Altera Nios II MMU emulation for qemu.
+ *
+ * Copyright (C) 2012 Chris Wulff <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/lgpl-2.1.html>
+ */
+
+/*
+ * FIXME: Convert to VMstate
+ */
+
+#include "qemu/osdep.h"
+#include "hw/hw.h"
+#include "hw/boards.h"
+
+void cpu_save(QEMUFile *f, void *opaque)
+{
+    /* TODO */
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    /* TODO */
+    return 0;
+}
target-nios2/mmu.c new file +292 -0

292 lines not on this page · raw message

target-nios2/mmu.h new file +54 -0

54 lines not on this page · raw message

target-nios2/monitor.c new file +35 -0
new file mode 100644
index 0000000..f3fa4cd
@@ -0,0 +1,35 @@
+/*
+ * QEMU monitor
+ *
+ * Copyright (c) 2003-2004 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "qemu/osdep.h"
+#include "cpu.h"
+#include "monitor/monitor.h"
+#include "monitor/hmp-target.h"
+#include "hmp.h"
+
+void hmp_info_tlb(Monitor *mon, const QDict *qdict)
+{
+    CPUArchState *env1 = mon_get_cpu_env();
+
+    dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
+}
target-nios2/op_helper.c new file +86 -0

32 lines not on this page · raw message

target-nios2/translate.c new file +242 -0

242 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.8.1

Part of the diff is not shown here. View the raw message.

#2 lore raw

PATCH 3/7 nios2: Add usermode binaries emulation

In reply to Marek Vasut

Add missing bits for qemu-user required for emulating Altera Nios2 userspace binaries.

Cc Chris Wulff <[email protected]>
Cc Jeff Da Silva <[email protected]>
Cc Ley Foon Tan <[email protected]>
Cc Sandra Loosemore <[email protected]>
Cc Yves Vandervennet <[email protected]>
---
 include/elf.h                     |   2 +
 linux-user/elfload.c              |  58 +++++++
 linux-user/main.c                 | 136 +++++++++++++++-
 linux-user/nios2/syscall_nr.h     | 330 ++++++++++++++++++++++++++++++++++++++
 linux-user/nios2/target_cpu.h     |  38 +++++
 linux-user/nios2/target_signal.h  |  26 +++
 linux-user/nios2/target_structs.h |  58 +++++++
 linux-user/nios2/target_syscall.h |  37 +++++
 linux-user/nios2/termbits.h       | 220 +++++++++++++++++++++++++
 linux-user/signal.c               | 238 ++++++++++++++++++++++++++-
 linux-user/syscall_defs.h         |   7 +-
 11 files changed, 1143 insertions(+), 7 deletions(-)
 create mode 100644 linux-user/nios2/syscall_nr.h
 create mode 100644 linux-user/nios2/target_cpu.h
 create mode 100644 linux-user/nios2/target_signal.h
 create mode 100644 linux-user/nios2/target_structs.h
 create mode 100644 linux-user/nios2/target_syscall.h
 create mode 100644 linux-user/nios2/termbits.h
include/elf.h +2 -0
index 1c2975d..0dbd3e9 100644
@@ -126,6 +126,8 @@ typedef int64_t  Elf64_Sxword;
  */
 #define EM_S390_OLD     0xA390
 
+#define EM_ALTERA_NIOS2 113     /* Altera Nios II soft-core processor */
+
 #define EM_MICROBLAZE      189
 #define EM_MICROBLAZE_OLD  0xBAAB
 
linux-user/elfload.c +58 -0

64 lines not on this page · raw message

linux-user/main.c +134 -2

154 lines not on this page · raw message

linux-user/nios2/syscall_nr.h new file +330 -0

330 lines not on this page · raw message

linux-user/nios2/target_cpu.h new file +38 -0
new file mode 100644
index 0000000..5b1e195
@@ -0,0 +1,38 @@
+/*
+ * Nios2 specific CPU ABI and functions for linux-user
+ *
+ * Copyright (c) 2016 Marek Vasut <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_CPU_H
+#define TARGET_CPU_H
+
+static inline void cpu_clone_regs(CPUNios2State *env, target_ulong newsp)
+{
+    if (newsp) {
+        env->regs[R_SP] = newsp;
+    }
+    env->regs[R_RET0] = 0;
+}
+
+static inline void cpu_set_tls(CPUNios2State *env, target_ulong newtls)
+{
+    /* Linux kernel 3.10 does not pay any attention to CLONE_SETTLS
+     * in copy_thread(), so QEMU need not do so either.
+     */
+}
+
+#endif
linux-user/nios2/target_signal.h new file +26 -0
new file mode 100644
index 0000000..23a8267
@@ -0,0 +1,26 @@
+#ifndef TARGET_SIGNAL_H
+#define TARGET_SIGNAL_H
+
+#include "cpu.h"
+
+/* this struct defines a stack used during syscall handling */
+
+typedef struct target_sigaltstack {
+    abi_long ss_sp;
+    abi_ulong ss_size;
+    abi_long ss_flags;
+} target_stack_t;
+
+/* sigaltstack controls  */
+#define TARGET_SS_ONSTACK     1
+#define TARGET_SS_DISABLE     2
+
+#define TARGET_MINSIGSTKSZ    2048
+#define TARGET_SIGSTKSZ       8192
+
+static inline abi_ulong get_sp_from_cpustate(CPUNios2State *state)
+{
+    return state->regs[R_SP];
+}
+
+#endif /* TARGET_SIGNAL_H */
linux-user/nios2/target_structs.h new file +58 -0

58 lines not on this page · raw message

linux-user/nios2/target_syscall.h new file +37 -0
new file mode 100644
index 0000000..23da0d6
@@ -0,0 +1,37 @@
+#ifndef TARGET_SYSCALL_H
+#define TARGET_SYSCALL_H
+
+#define UNAME_MACHINE "nios2"
+#define UNAME_MINIMUM_RELEASE "3.19.0"
+
+struct target_pt_regs {
+	unsigned long  r8;	/* r8-r15 Caller-saved GP registers */
+	unsigned long  r9;
+	unsigned long  r10;
+	unsigned long  r11;
+	unsigned long  r12;
+	unsigned long  r13;
+	unsigned long  r14;
+	unsigned long  r15;
+	unsigned long  r1;	/* Assembler temporary */
+	unsigned long  r2;	/* Retval LS 32bits */
+	unsigned long  r3;	/* Retval MS 32bits */
+	unsigned long  r4;	/* r4-r7 Register arguments */
+	unsigned long  r5;
+	unsigned long  r6;
+	unsigned long  r7;
+	unsigned long  orig_r2;	/* Copy of r2 ?? */
+	unsigned long  ra;	/* Return address */
+	unsigned long  fp;	/* Frame pointer */
+	unsigned long  sp;	/* Stack pointer */
+	unsigned long  gp;	/* Global pointer */
+	unsigned long  estatus;
+	unsigned long  ea;	/* Exception return address (pc) */
+	unsigned long  orig_r7;
+};
+
+#define TARGET_MINSIGSTKSZ 2048
+#define TARGET_MLOCKALL_MCL_CURRENT 1
+#define TARGET_MLOCKALL_MCL_FUTURE  2
+
+#endif  /* TARGET_SYSCALL_H */
linux-user/nios2/termbits.h new file +220 -0

220 lines not on this page · raw message

linux-user/signal.c +236 -2

256 lines not on this page · raw message

linux-user/syscall_defs.h +4 -3
index 7835654..e4133ba 100644
@@ -68,7 +68,8 @@
 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SH4) \
     || defined(TARGET_M68K) || defined(TARGET_CRIS) \
     || defined(TARGET_UNICORE32) || defined(TARGET_S390X) \
-    || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
+    || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX) \
+    || defined(TARGET_NIOS2)
 
 #define TARGET_IOC_SIZEBITS	14
 #define TARGET_IOC_DIRBITS	2
@@ -387,7 +388,7 @@ int do_sigaction(int sig, const struct target_sigaction *act,
     || defined(TARGET_M68K) || defined(TARGET_ALPHA) || defined(TARGET_CRIS) \
     || defined(TARGET_MICROBLAZE) || defined(TARGET_UNICORE32) \
     || defined(TARGET_S390X) || defined(TARGET_OPENRISC) \
-    || defined(TARGET_TILEGX)
+    || defined(TARGET_TILEGX) || defined(TARGET_NIOS2)
 
 #if defined(TARGET_SPARC)
 #define TARGET_SA_NOCLDSTOP    8u
@@ -1934,7 +1935,7 @@ struct target_stat {
     abi_ulong  target_st_ctime_nsec;
     unsigned int __unused[2];
 };
-#elif defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
+#elif defined(TARGET_OPENRISC) || defined(TARGET_TILEGX) || defined(TARGET_NIOS2)
 
 /* These are the asm-generic versions of the stat and stat64 structures */
 
Signature · 1 line
2.8.1
#3 lore raw

PATCH 4/7 nios2: Add IIC interrupt controller emulation

In reply to Marek Vasut

From: Chris Wulff <[email protected]>

Add the Altera Nios2 internal interrupt controller model.

Cc Chris Wulff <[email protected]>
Cc Jeff Da Silva <[email protected]>
Cc Ley Foon Tan <[email protected]>
Cc Sandra Loosemore <[email protected]>
Cc Yves Vandervennet <[email protected]>
---
 hw/intc/Makefile.objs |   1 +
 hw/intc/nios2_iic.c   | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)
 create mode 100644 hw/intc/nios2_iic.c
hw/intc/Makefile.objs +1 -0
index 05ec21b..3c29fe7 100644
@@ -38,3 +38,4 @@ obj-$(CONFIG_S390_FLIC_KVM) += s390_flic_kvm.o
 obj-$(CONFIG_ASPEED_SOC) += aspeed_vic.o
 obj-$(CONFIG_ARM_GIC) += arm_gicv3_cpuif.o
 obj-$(CONFIG_MIPS_CPS) += mips_gic.o
+obj-$(CONFIG_NIOS2) += nios2_iic.o
hw/intc/nios2_iic.c new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
2.8.1
#4 lore raw

PATCH 5/7 nios2: Add periodic timer emulation

In reply to Marek Vasut

From: Chris Wulff <[email protected]>

Add the Altera timer model.

Cc Chris Wulff <[email protected]>
Cc Jeff Da Silva <[email protected]>
Cc Ley Foon Tan <[email protected]>
Cc Sandra Loosemore <[email protected]>
Cc Yves Vandervennet <[email protected]>
---
 hw/timer/Makefile.objs  |   1 +
 hw/timer/altera_timer.c | 225 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 226 insertions(+)
 create mode 100644 hw/timer/altera_timer.c
hw/timer/Makefile.objs +1 -0
index 7ba8c23..0867a64 100644
@@ -18,6 +18,7 @@ common-obj-$(CONFIG_IMX) += imx_gpt.o
 common-obj-$(CONFIG_LM32) += lm32_timer.o
 common-obj-$(CONFIG_MILKYMIST) += milkymist-sysctl.o
 
+obj-$(CONFIG_ALTERA_TIMER) += altera_timer.o
 obj-$(CONFIG_EXYNOS4) += exynos4210_mct.o
 obj-$(CONFIG_EXYNOS4) += exynos4210_pwm.o
 obj-$(CONFIG_EXYNOS4) += exynos4210_rtc.o
hw/timer/altera_timer.c new file +225 -0

225 lines not on this page · raw message

Signature · 1 line
2.8.1
#5 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Hello Marek,

Quoted 4 lines · Marek Vasut

From: Chris Wulff <[email protected]>

Add the Altera timer model.

[snip]

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+static void timer_write(void *opaque, hwaddr addr,
+                        uint64_t val64, unsigned int size)
+{
+    AlteraTimer *t = opaque;

hw/timer/altera_timer.c line 101

Quoted 10 lines
hw/timer/altera_timer.c new file +10 -0
+    uint64_t tvalue;
+    uint32_t value = val64;
+    uint32_t count = 0;
+    int irqState = timer_irq_state(t);
+
+    addr >>= 2;
+    addr &= 0x7;
+    switch (addr) {
+    case R_STATUS:
+        /* Writing zero clears the timeout */

Either "zero" or "clears" should be omitted here.

hw/timer/altera_timer.c line 109

Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+        t->regs[R_STATUS] &= ~STATUS_TO;
+        break;
+
+    case R_CONTROL:
+        t->regs[R_CONTROL] = value & (CONTROL_ITO | CONTROL_CONT);
+        if ((value & CONTROL_START) &&
+            !(t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_run(t->ptimer, 1);

Starting to run with counter = 0 would cause immediate ptimer trigger, is that a correct behaviour for that timer?

FYI, I'm proposing ptimer policy feature that would help handling such edge cases correctly:

https://lists.nongnu.org/archive/html/qemu-devel/2016-07/msg05044.html

According to the "Nios Timer" datasheet, at least "wraparound after one period" policy would be suited well for that timer.

Quoted 14 lines
hw/timer/altera_timer.c new file +14 -0
+            t->regs[R_STATUS] |= STATUS_RUN;
+        }
+        if ((value & CONTROL_STOP) && (t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        break;
+
+    case R_PERIODL:
+    case R_PERIODH:
+        t->regs[addr] = value & 0xFFFF;
+        if (t->regs[R_STATUS] & STATUS_RUN) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;

hw/timer/altera_timer.c line 133

Quoted 10 lines
hw/timer/altera_timer.c new file +10 -0
+        }
+        tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
+        ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+        break;
+
+    case R_SNAPL:
+    case R_SNAPH:
+        count = ptimer_get_count(t->ptimer);
+        t->regs[R_SNAPL] = count & 0xFFFF;
+        t->regs[R_SNAPH] = (count >> 16) & 0xFFFF;

"& 0xFFFF" isn't needed for the R_SNAPH.

Quoted 1 line

+ break;

[snip]

hw/timer/altera_timer.c line 162

Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
+
+    t->regs[R_STATUS] |= STATUS_TO;
+
+    ptimer_stop(t->ptimer);

Ptimer is already stopped here, that ptimer_stop() could be omitted safely.

Quoted 11 lines
hw/timer/altera_timer.c new file +11 -0
+    ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+
+    if (t->regs[R_CONTROL] & CONTROL_CONT) {
+        ptimer_run(t->ptimer, 1);
+    } else {
+        t->regs[R_STATUS] &= ~STATUS_RUN;
+    }
+
+    qemu_set_irq(t->irq, timer_irq_state(t));
+}
+

[snip]

hw/timer/altera_timer.c line 210

Quoted 7 lines
hw/timer/altera_timer.c new file +7 -0
+static void altera_timer_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = altera_timer_realize;
+    dc->props = altera_timer_properties;
+}

Why there is no "dc->reset"?

I guess VMState is planned to be added later, right?

Signature · 1 line
Dmitry
#6 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 1 line · Dmitry Osipenko

Hello Marek,

Hi!

Sorry for the late reply, I got back from vacation only recently.

I noticed that a lot of files in this patchset are under LGPLv2.1 , I believe that needs fixing too, right ? I will talk to Chris about this as he is the original author of most of these files.

Quoted 8 lines · Marek Vasut
Quoted 4 lines

From: Chris Wulff <[email protected]>

Add the Altera timer model.

[snip]

Quoted 16 lines
Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+static void timer_write(void *opaque, hwaddr addr,
+                        uint64_t val64, unsigned int size)
+{
+    AlteraTimer *t = opaque;

hw/timer/altera_timer.c line 101

Quoted 10 lines
hw/timer/altera_timer.c new file +10 -0
+    uint64_t tvalue;
+    uint32_t value = val64;
+    uint32_t count = 0;
+    int irqState = timer_irq_state(t);
+
+    addr >>= 2;
+    addr &= 0x7;
+    switch (addr) {
+    case R_STATUS:
+        /* Writing zero clears the timeout */

Either "zero" or "clears" should be omitted here.

You are really supposed to write zero into the register according to the specification. Writing anything else is undefined.

Quoted 11 lines

hw/timer/altera_timer.c line 109

Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+        t->regs[R_STATUS] &= ~STATUS_TO;
+        break;
+
+    case R_CONTROL:
+        t->regs[R_CONTROL] = value & (CONTROL_ITO | CONTROL_CONT);
+        if ((value & CONTROL_START) &&
+            !(t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_run(t->ptimer, 1);

Starting to run with counter = 0 would cause immediate ptimer trigger, is that a correct behaviour for that timer?

I believe it is. If you start the timer with countdown register = 0, it should trigger.

Quoted 7 lines

FYI, I'm proposing ptimer policy feature that would help handling such edge cases correctly:

https://lists.nongnu.org/archive/html/qemu-devel/2016-07/msg05044.html

According to the "Nios Timer" datasheet, at least "wraparound after one period" policy would be suited well for that timer.

Yes, the timer api in qemu is pretty hard to grasp, I had trouble with it so it is possible there are bugs in here.

Quoted 18 lines
Quoted 18 lines
hw/timer/altera_timer.c new file +18 -0
+            t->regs[R_STATUS] |= STATUS_RUN;
+        }
+        if ((value & CONTROL_STOP) && (t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        break;
+
+    case R_PERIODL:
+    case R_PERIODH:
+        t->regs[addr] = value & 0xFFFF;
+        if (t->regs[R_STATUS] & STATUS_RUN) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
+        ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+        break;
Quoted 8 lines

hw/timer/altera_timer.c line 133

Quoted 6 lines
hw/timer/altera_timer.c new file +6 -0
+
+    case R_SNAPL:
+    case R_SNAPH:
+        count = ptimer_get_count(t->ptimer);
+        t->regs[R_SNAPL] = count & 0xFFFF;
+        t->regs[R_SNAPH] = (count >> 16) & 0xFFFF;

"& 0xFFFF" isn't needed for the R_SNAPH.

It isn't, until someone changes the storage type to something else but uint32_t , which could happen with these sorts of systems -- the nios2 is a softcore (in an FPGA), so it can be adjusted if needed be. I can drop this if you prefer that though.

Quoted 4 lines
Quoted 1 line

+ break;

[snip]

Quoted 10 lines

hw/timer/altera_timer.c line 162

Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
+
+    t->regs[R_STATUS] |= STATUS_TO;
+
+    ptimer_stop(t->ptimer);

Ptimer is already stopped here, that ptimer_stop() could be omitted safely.

Dropped, thanks.

Quoted 24 lines
Quoted 11 lines
hw/timer/altera_timer.c new file +11 -0
+    ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+
+    if (t->regs[R_CONTROL] & CONTROL_CONT) {
+        ptimer_run(t->ptimer, 1);
+    } else {
+        t->regs[R_STATUS] &= ~STATUS_RUN;
+    }
+
+    qemu_set_irq(t->irq, timer_irq_state(t));
+}
+

[snip]

hw/timer/altera_timer.c line 210

Quoted 7 lines
hw/timer/altera_timer.c new file +7 -0
+static void altera_timer_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = altera_timer_realize;
+    dc->props = altera_timer_properties;
+}

Why there is no "dc->reset"?

Quoted 1 line

I guess VMState is planned to be added later, right?

Yes, I would like to get at least some basic version in and then extend it. The VMState was also pretty difficult concept for me to grasp.

Signature · 2 lines
Best regards,
Marek Vasut
#7 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 11 lines · Marek Vasut
Quoted 1 line · Dmitry Osipenko

Hello Marek,

Hi!

Sorry for the late reply, I got back from vacation only recently.

I noticed that a lot of files in this patchset are under LGPLv2.1 , I believe that needs fixing too, right ? I will talk to Chris about this as he is the original author of most of these files.

There are quite many files in QEMU licensed under LGPLv2.1, so it's probably not an issue. I don't know for sure.

Quoted 6 lines
Quoted 6 lines · Marek Vasut
Quoted 4 lines

From: Chris Wulff <[email protected]>

Add the Altera timer model.

Quoted 8 lines
Quoted 8 lines

[snip]

hw/timer/altera_timer.c line 93

Quoted 6 lines
hw/timer/altera_timer.c new file +6 -0
+static void timer_write(void *opaque, hwaddr addr,
+                        uint64_t val64, unsigned int size)
+{
+    AlteraTimer *t = opaque;
+    uint64_t tvalue;
+    uint32_t value = val64;

Why not to use "val64" directly? Or better to rename it to "value" and drop this definition.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+    uint32_t count = 0;
+    int irqState = timer_irq_state(t);
+
+    addr >>= 2;
+    addr &= 0x7;
+    switch (addr) {
+    case R_STATUS:
+        /* Writing zero clears the timeout */
Quoted 5 lines
Quoted 1 line

Either "zero" or "clears" should be omitted here.

You are really supposed to write zero into the register according to the specification. Writing anything else is undefined.

Okay, then is clearing TO bit on writing *any* value is a common behaviour? Mentioning it in the comment would help to avoid confusion.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+        t->regs[R_STATUS] &= ~STATUS_TO;
+        break;
+
+    case R_CONTROL:
+        t->regs[R_CONTROL] = value & (CONTROL_ITO | CONTROL_CONT);
+        if ((value & CONTROL_START) &&
+            !(t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_run(t->ptimer, 1);
Quoted 6 lines
Quoted 2 lines

Starting to run with counter = 0 would cause immediate ptimer trigger, is that a correct behaviour for that timer?

I believe it is. If you start the timer with countdown register = 0, it should trigger.

It would be good to have it verified.

Also, ptimer now supports "on the fly mode switch":

https://github.com/qemu/qemu/commit/869e92b5c392eb6b2c7b398b878c435442b8e9dd

ptimer_run(t->ptimer, !(value & CONTROL_CONT)) could be used here and "manual" re-run dropped from the timer_hit() handler.

Quoted 11 lines
Quoted 7 lines

FYI, I'm proposing ptimer policy feature that would help handling such edge cases correctly:

https://lists.nongnu.org/archive/html/qemu-devel/2016-07/msg05044.html

According to the "Nios Timer" datasheet, at least "wraparound after one period" policy would be suited well for that timer.

Yes, the timer api in qemu is pretty hard to grasp, I had trouble with it so it is possible there are bugs in here.

That would be very churny to implement with the current ptimer. Hopefully, ptimer policy would get into the QEMU and then it could be applied to this timer. So, I think it is fine for now.

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
hw/timer/altera_timer.c new file +18 -0
+            t->regs[R_STATUS] |= STATUS_RUN;
+        }
+        if ((value & CONTROL_STOP) && (t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        break;
+
+    case R_PERIODL:
+    case R_PERIODH:
+        t->regs[addr] = value & 0xFFFF;
+        if (t->regs[R_STATUS] & STATUS_RUN) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
+        ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+        break;
Quoted 8 lines

hw/timer/altera_timer.c line 133

Quoted 6 lines
hw/timer/altera_timer.c new file +6 -0
+
+    case R_SNAPL:
+    case R_SNAPH:
+        count = ptimer_get_count(t->ptimer);
+        t->regs[R_SNAPL] = count & 0xFFFF;
+        t->regs[R_SNAPH] = (count >> 16) & 0xFFFF;

"& 0xFFFF" isn't needed for the R_SNAPH.

Quoted 5 lines

It isn't, until someone changes the storage type to something else but uint32_t , which could happen with these sorts of systems -- the nios2 is a softcore (in an FPGA), so it can be adjusted if needed be. I can drop this if you prefer that though.

In case of the reduced register capacity, the ptimer limit would provide correct "high" register value. In case of the expanded register capacity, the shift value should be changed accordingly. In both cases that mask isn't needed.

If register capacity is supposed to be configurable, then QOM property knob should be provided and code changed accordingly.

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

+ break;

[snip]

hw/timer/altera_timer.c line 158

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

ptimer_get_limit() could be used here.

Quoted 33 lines
Quoted 6 lines

hw/timer/altera_timer.c line 162

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+
+    t->regs[R_STATUS] |= STATUS_TO;
+
+    ptimer_stop(t->ptimer);

Ptimer is already stopped here, that ptimer_stop() could be omitted safely.

Dropped, thanks.

Quoted 24 lines
Quoted 11 lines
hw/timer/altera_timer.c new file +11 -0
+    ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+
+    if (t->regs[R_CONTROL] & CONTROL_CONT) {
+        ptimer_run(t->ptimer, 1);
+    } else {
+        t->regs[R_STATUS] &= ~STATUS_RUN;
+    }
+
+    qemu_set_irq(t->irq, timer_irq_state(t));
+}
+

[snip]

hw/timer/altera_timer.c line 210

Quoted 7 lines
hw/timer/altera_timer.c new file +7 -0
+static void altera_timer_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = altera_timer_realize;
+    dc->props = altera_timer_properties;
+}

Why there is no "dc->reset"?

Quoted 5 lines
Quoted 1 line

I guess VMState is planned to be added later, right?

Yes, I would like to get at least some basic version in and then extend it. The VMState was also pretty difficult concept for me to grasp.

I suggest to provide "reset" function, otherwise it's likely that you would get unexpected result or crash on QEMU reset. This also applies to the "interrupt controller" patch.

Signature · 1 line
Dmitry
#8 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

On 10.08.2016 13:30, Dmitry Osipenko wrote:

[snip]

Quoted 4 lines

I suggest to provide "reset" function, otherwise it's likely that you would get unexpected result or crash on QEMU reset. This also applies to the "interrupt controller" patch.

Correction: I didn't notice that "interrupt controller" state hasn't any resettable variables, so it doesn't need reset.

Signature · 1 line
Dmitry
#9 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 4 lines · Dmitry Osipenko

On 10.08.2016 13:30, Dmitry Osipenko wrote:

[snip]

Quoted 8 lines
Quoted 4 lines

I suggest to provide "reset" function, otherwise it's likely that you would get unexpected result or crash on QEMU reset. This also applies to the "interrupt controller" patch.

Correction: I didn't notice that "interrupt controller" state hasn't any resettable variables, so it doesn't need reset.

Ah, I had a question about that anyway. Thanks !

Signature · 2 lines
Best regards,
Marek Vasut
#10 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

In reply to Dmitry Osipenko
Quoted 8 lines · Dmitry Osipenko
Quoted 7 lines · Marek Vasut
Quoted 1 line · Dmitry Osipenko

Hello Marek,

Hi!

Sorry for the late reply, I got back from vacation only recently.

Quoted 7 lines
Quoted 4 lines

I noticed that a lot of files in this patchset are under LGPLv2.1 , I believe that needs fixing too, right ? I will talk to Chris about this as he is the original author of most of these files.

There are quite many files in QEMU licensed under LGPLv2.1, so it's probably not an issue. I don't know for sure.

Looking through the code, you have a point. OK, I will leave it as-is and I'll fix it if and only if it is mandatory.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · Marek Vasut
Quoted 4 lines

From: Chris Wulff <[email protected]>

Add the Altera timer model.

[snip]

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

hw/timer/altera_timer.c line 93

Quoted 6 lines
hw/timer/altera_timer.c new file +6 -0
+static void timer_write(void *opaque, hwaddr addr,
+                        uint64_t val64, unsigned int size)
+{
+    AlteraTimer *t = opaque;
+    uint64_t tvalue;
+    uint32_t value = val64;

Why not to use "val64" directly? Or better to rename it to "value" and drop this definition.

Done, thanks for spotting this.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+    uint32_t count = 0;
+    int irqState = timer_irq_state(t);
+
+    addr >>= 2;
+    addr &= 0x7;
+    switch (addr) {
+    case R_STATUS:
+        /* Writing zero clears the timeout */
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Either "zero" or "clears" should be omitted here.

You are really supposed to write zero into the register according to the specification. Writing anything else is undefined.

Okay, then is clearing TO bit on writing *any* value is a common behaviour? Mentioning it in the comment would help to avoid confusion.

Reworded to "/* The timeout bit is cleared by writing the status register. */" .

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+        t->regs[R_STATUS] &= ~STATUS_TO;
+        break;
+
+    case R_CONTROL:
+        t->regs[R_CONTROL] = value & (CONTROL_ITO | CONTROL_CONT);
+        if ((value & CONTROL_START) &&
+            !(t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_run(t->ptimer, 1);
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Starting to run with counter = 0 would cause immediate ptimer trigger, is that a correct behaviour for that timer?

I believe it is. If you start the timer with countdown register = 0, it should trigger.

It would be good to have it verified.

I don't have access to the hardware, CCing Juro, he should have it.

Quoted 6 lines

Also, ptimer now supports "on the fly mode switch":

https://github.com/qemu/qemu/commit/869e92b5c392eb6b2c7b398b878c435442b8e9dd

ptimer_run(t->ptimer, !(value & CONTROL_CONT)) could be used here and "manual" re-run dropped from the timer_hit() handler.

So who will re-run the timer if it's configured in the continuous mode if it's not re-run in the timer_hit() ?

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

FYI, I'm proposing ptimer policy feature that would help handling such edge cases correctly:

https://lists.nongnu.org/archive/html/qemu-devel/2016-07/msg05044.html

According to the "Nios Timer" datasheet, at least "wraparound after one period" policy would be suited well for that timer.

Quoted 7 lines
Quoted 3 lines

Yes, the timer api in qemu is pretty hard to grasp, I had trouble with it so it is possible there are bugs in here.

That would be very churny to implement with the current ptimer. Hopefully, ptimer policy would get into the QEMU and then it could be applied to this timer. So, I think it is fine for now.

Thanks for pointing it out, I'll keep an eye on it.

Quoted 33 lines
Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
hw/timer/altera_timer.c new file +18 -0
+            t->regs[R_STATUS] |= STATUS_RUN;
+        }
+        if ((value & CONTROL_STOP) && (t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        break;
+
+    case R_PERIODL:
+    case R_PERIODH:
+        t->regs[addr] = value & 0xFFFF;
+        if (t->regs[R_STATUS] & STATUS_RUN) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
+        ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+        break;
Quoted 8 lines

hw/timer/altera_timer.c line 133

Quoted 6 lines
hw/timer/altera_timer.c new file +6 -0
+
+    case R_SNAPL:
+    case R_SNAPH:
+        count = ptimer_get_count(t->ptimer);
+        t->regs[R_SNAPL] = count & 0xFFFF;
+        t->regs[R_SNAPH] = (count >> 16) & 0xFFFF;

"& 0xFFFF" isn't needed for the R_SNAPH.

Quoted 5 lines

It isn't, until someone changes the storage type to something else but uint32_t , which could happen with these sorts of systems -- the nios2 is a softcore (in an FPGA), so it can be adjusted if needed be. I can drop this if you prefer that though.

Quoted 3 lines

In case of the reduced register capacity, the ptimer limit would provide correct "high" register value. In case of the expanded register capacity, the shift value should be changed accordingly. In both cases that mask isn't needed.

OK

Quoted 2 lines

If register capacity is supposed to be configurable, then QOM property knob should be provided and code changed accordingly.

This can be added later if and only if needed. I don't think it can be changed thus far. Thanks for pointing out the QOM property bit, that will come useful.

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

+ break;

[snip]

hw/timer/altera_timer.c line 158

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

ptimer_get_limit() could be used here.

You probably want to use the value in the register instead of the value in the ptimer state in case someone rewrote those registers, no ?

Quoted 35 lines
Quoted 35 lines
Quoted 6 lines

hw/timer/altera_timer.c line 162

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+
+    t->regs[R_STATUS] |= STATUS_TO;
+
+    ptimer_stop(t->ptimer);

Ptimer is already stopped here, that ptimer_stop() could be omitted safely.

Dropped, thanks.

Quoted 25 lines
Quoted 11 lines
hw/timer/altera_timer.c new file +11 -0
+    ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+
+    if (t->regs[R_CONTROL] & CONTROL_CONT) {
+        ptimer_run(t->ptimer, 1);
+    } else {
+        t->regs[R_STATUS] &= ~STATUS_RUN;
+    }
+
+    qemu_set_irq(t->irq, timer_irq_state(t));
+}
+

[snip]

hw/timer/altera_timer.c line 210

Quoted 7 lines
hw/timer/altera_timer.c new file +7 -0
+static void altera_timer_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = altera_timer_realize;
+    dc->props = altera_timer_properties;
+}

Why there is no "dc->reset"?

I guess VMState is planned to be added later, right?

Quoted 7 lines
Quoted 3 lines

Yes, I would like to get at least some basic version in and then extend it. The VMState was also pretty difficult concept for me to grasp.

I suggest to provide "reset" function, otherwise it's likely that you would get unexpected result or crash on QEMU reset. This also applies to the "interrupt controller" patch.

For the timer, that reset would look like this?

197 static void altera_timer_reset(DeviceState *dev) 198 {
199 AlteraTimer *t = ALTERA_TIMER(dev);
200
201 ptimer_stop(t->ptimer);
202 memset(t->regs, 0, ARRAY_SIZE(t->regs));
203 }

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

Thanks !

Signature · 2 lines
Best regards,
Marek Vasut
#11 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 83 lines · Marek Vasut
Quoted 8 lines · Dmitry Osipenko
Quoted 7 lines · Marek Vasut
Quoted 1 line · Dmitry Osipenko

Hello Marek,

Hi!

Sorry for the late reply, I got back from vacation only recently.

Quoted 7 lines
Quoted 4 lines

I noticed that a lot of files in this patchset are under LGPLv2.1 , I believe that needs fixing too, right ? I will talk to Chris about this as he is the original author of most of these files.

There are quite many files in QEMU licensed under LGPLv2.1, so it's probably not an issue. I don't know for sure.

Looking through the code, you have a point. OK, I will leave it as-is and I'll fix it if and only if it is mandatory.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · Marek Vasut
Quoted 4 lines

From: Chris Wulff <[email protected]>

Add the Altera timer model.

[snip]

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

hw/timer/altera_timer.c line 93

Quoted 6 lines
hw/timer/altera_timer.c new file +6 -0
+static void timer_write(void *opaque, hwaddr addr,
+                        uint64_t val64, unsigned int size)
+{
+    AlteraTimer *t = opaque;
+    uint64_t tvalue;
+    uint32_t value = val64;

Why not to use "val64" directly? Or better to rename it to "value" and drop this definition.

Done, thanks for spotting this.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+    uint32_t count = 0;
+    int irqState = timer_irq_state(t);
+
+    addr >>= 2;
+    addr &= 0x7;
+    switch (addr) {
+    case R_STATUS:
+        /* Writing zero clears the timeout */
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Either "zero" or "clears" should be omitted here.

You are really supposed to write zero into the register according to the specification. Writing anything else is undefined.

Okay, then is clearing TO bit on writing *any* value is a common behaviour? Mentioning it in the comment would help to avoid confusion.

Reworded to "/* The timeout bit is cleared by writing the status register. */" .

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
hw/timer/altera_timer.c new file +8 -0
+        t->regs[R_STATUS] &= ~STATUS_TO;
+        break;
+
+    case R_CONTROL:
+        t->regs[R_CONTROL] = value & (CONTROL_ITO | CONTROL_CONT);
+        if ((value & CONTROL_START) &&
+            !(t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_run(t->ptimer, 1);
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Starting to run with counter = 0 would cause immediate ptimer trigger, is that a correct behaviour for that timer?

I believe it is. If you start the timer with countdown register = 0, it should trigger.

It would be good to have it verified.

I don't have access to the hardware, CCing Juro, he should have it.

Quoted 2 lines

Also, ptimer now supports "on the fly mode switch":

Quoted 8 lines
Quoted 4 lines

https://github.com/qemu/qemu/commit/869e92b5c392eb6b2c7b398b878c435442b8e9dd

ptimer_run(t->ptimer, !(value & CONTROL_CONT)) could be used here and "manual" re-run dropped from the timer_hit() handler.

So who will re-run the timer if it's configured in the continuous mode if it's not re-run in the timer_hit() ?

Ptimer will, timer_hit() will only have to assert IRQ and set ptimer count to the limit tvalue if timer is in the oneshot mode.

Something like:

static void timer_hit(void *opaque)

{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}
Quoted 75 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

FYI, I'm proposing ptimer policy feature that would help handling such edge cases correctly:

https://lists.nongnu.org/archive/html/qemu-devel/2016-07/msg05044.html

According to the "Nios Timer" datasheet, at least "wraparound after one period" policy would be suited well for that timer.

Quoted 7 lines
Quoted 3 lines

Yes, the timer api in qemu is pretty hard to grasp, I had trouble with it so it is possible there are bugs in here.

That would be very churny to implement with the current ptimer. Hopefully, ptimer policy would get into the QEMU and then it could be applied to this timer. So, I think it is fine for now.

Thanks for pointing it out, I'll keep an eye on it.

Quoted 33 lines
Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
hw/timer/altera_timer.c new file +18 -0
+            t->regs[R_STATUS] |= STATUS_RUN;
+        }
+        if ((value & CONTROL_STOP) && (t->regs[R_STATUS] & STATUS_RUN)) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        break;
+
+    case R_PERIODL:
+    case R_PERIODH:
+        t->regs[addr] = value & 0xFFFF;
+        if (t->regs[R_STATUS] & STATUS_RUN) {
+            ptimer_stop(t->ptimer);
+            t->regs[R_STATUS] &= ~STATUS_RUN;
+        }
+        tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
+        ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+        break;
Quoted 8 lines

hw/timer/altera_timer.c line 133

Quoted 6 lines
hw/timer/altera_timer.c new file +6 -0
+
+    case R_SNAPL:
+    case R_SNAPH:
+        count = ptimer_get_count(t->ptimer);
+        t->regs[R_SNAPL] = count & 0xFFFF;
+        t->regs[R_SNAPH] = (count >> 16) & 0xFFFF;

"& 0xFFFF" isn't needed for the R_SNAPH.

Quoted 5 lines

It isn't, until someone changes the storage type to something else but uint32_t , which could happen with these sorts of systems -- the nios2 is a softcore (in an FPGA), so it can be adjusted if needed be. I can drop this if you prefer that though.

Quoted 3 lines

In case of the reduced register capacity, the ptimer limit would provide correct "high" register value. In case of the expanded register capacity, the shift value should be changed accordingly. In both cases that mask isn't needed.

OK

Quoted 2 lines

If register capacity is supposed to be configurable, then QOM property knob should be provided and code changed accordingly.

This can be added later if and only if needed. I don't think it can be changed thus far. Thanks for pointing out the QOM property bit, that will come useful.

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

+ break;

[snip]

hw/timer/altera_timer.c line 158

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

ptimer_get_limit() could be used here.

Quoted 3 lines

You probably want to use the value in the register instead of the value in the ptimer state in case someone rewrote those registers, no ?

In case someone rewrote the registers, the ptimer limit would be also updated. So this could be changed to:

uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

Actually, R_PERIODL and R_PERIODH regs aren't really needed and could be removed from the AlteraTimer state, since ptimer stores the same limit value + 1. The timer_read/write() would need to be changed accordingly.

Quoted 45 lines
Quoted 35 lines
Quoted 35 lines
Quoted 6 lines

hw/timer/altera_timer.c line 162

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+
+    t->regs[R_STATUS] |= STATUS_TO;
+
+    ptimer_stop(t->ptimer);

Ptimer is already stopped here, that ptimer_stop() could be omitted safely.

Dropped, thanks.

Quoted 25 lines
Quoted 11 lines
hw/timer/altera_timer.c new file +11 -0
+    ptimer_set_limit(t->ptimer, tvalue + 1, 1);
+
+    if (t->regs[R_CONTROL] & CONTROL_CONT) {
+        ptimer_run(t->ptimer, 1);
+    } else {
+        t->regs[R_STATUS] &= ~STATUS_RUN;
+    }
+
+    qemu_set_irq(t->irq, timer_irq_state(t));
+}
+

[snip]

hw/timer/altera_timer.c line 210

Quoted 7 lines
hw/timer/altera_timer.c new file +7 -0
+static void altera_timer_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = altera_timer_realize;
+    dc->props = altera_timer_properties;
+}

Why there is no "dc->reset"?

I guess VMState is planned to be added later, right?

Quoted 7 lines
Quoted 3 lines

Yes, I would like to get at least some basic version in and then extend it. The VMState was also pretty difficult concept for me to grasp.

I suggest to provide "reset" function, otherwise it's likely that you would get unexpected result or crash on QEMU reset. This also applies to the "interrupt controller" patch.

For the timer, that reset would look like this?

Quoted 8 lines

197 static void altera_timer_reset(DeviceState *dev) 198 {
199 AlteraTimer *t = ALTERA_TIMER(dev);
200
201 ptimer_stop(t->ptimer);
202 memset(t->regs, 0, ARRAY_SIZE(t->regs));
203 }

Yes, and move ptimer_set_limit(t->ptimer, 0xffffffff, 1) here from the altera_timer_realize().

hw/timer/altera_timer.c line 202

Quoted 5 lines
hw/timer/altera_timer.c new file +4 -0
+static Property altera_timer_properties[] = {
+    DEFINE_PROP_UINT32("clock-frequency", AlteraTimer, freq_hz, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};

Why not to have some sane "clock-frequency" by default?

Quoted 3 lines

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

No, IRQ's would be "deasserted" by resetting CPUClass state, of which QEMU takes care itself. No action needed by the IIC.

Quoted 2 lines

Thanks !

Signature · 1 line
Dmitry
#12 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

On 08/15/2016 01:39 PM, Dmitry Osipenko wrote:

[...]

Quoted 17 lines
Quoted 10 lines
Quoted 6 lines

Also, ptimer now supports "on the fly mode switch":

https://github.com/qemu/qemu/commit/869e92b5c392eb6b2c7b398b878c435442b8e9dd

ptimer_run(t->ptimer, !(value & CONTROL_CONT)) could be used here and "manual" re-run dropped from the timer_hit() handler.

So who will re-run the timer if it's configured in the continuous mode if it's not re-run in the timer_hit() ?

Ptimer will, timer_hit() will only have to assert IRQ and set ptimer count to the limit tvalue if timer is in the oneshot mode.

Something like:

static void timer_hit(void *opaque)

Quoted 8 lines
{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;

There should probably be } else { statement here , no ?

Quoted 5 lines
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}

Thanks for the draft.

[...]

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

hw/timer/altera_timer.c line 158

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

ptimer_get_limit() could be used here.

You probably want to use the value in the register instead of the value in the ptimer state in case someone rewrote those registers, no ?

Quoted 8 lines

In case someone rewrote the registers, the ptimer limit would be also updated. So this could be changed to:

uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

Actually, R_PERIODL and R_PERIODH regs aren't really needed and could be removed from the AlteraTimer state, since ptimer stores the same limit value + 1. The timer_read/write() would need to be changed accordingly.

Ha, so we're getting to something like the following code (based on your example + the else bit) ?

static void timer_hit(void *opaque)

{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;
    } else {
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}

[...]

Quoted 11 lines
Quoted 10 lines

For the timer, that reset would look like this?

197 static void altera_timer_reset(DeviceState *dev) 198 {
199 AlteraTimer *t = ALTERA_TIMER(dev);
200
201 ptimer_stop(t->ptimer);
202 memset(t->regs, 0, ARRAY_SIZE(t->regs));
203 }

Quoted 2 lines

Yes, and move ptimer_set_limit(t->ptimer, 0xffffffff, 1) here from the altera_timer_realize().

Got it, thanks.

Quoted 6 lines

hw/timer/altera_timer.c line 202

Quoted 4 lines
hw/timer/altera_timer.c new file +4 -0
+static Property altera_timer_properties[] = {
+    DEFINE_PROP_UINT32("clock-frequency", AlteraTimer, freq_hz, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};

Why not to have some sane "clock-frequency" by default?

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

Quoted 6 lines
Quoted 3 lines

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

No, IRQ's would be "deasserted" by resetting CPUClass state, of which QEMU takes care itself. No action needed by the IIC.

I see, thanks :)

btw any chance someone can look at the other patches too ?

Signature · 2 lines
Best regards,
Marek Vasut
#13 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 25 lines · Marek Vasut

On 08/15/2016 01:39 PM, Dmitry Osipenko wrote:

[...]

Quoted 21 lines
Quoted 10 lines
Quoted 6 lines

Also, ptimer now supports "on the fly mode switch":

https://github.com/qemu/qemu/commit/869e92b5c392eb6b2c7b398b878c435442b8e9dd

ptimer_run(t->ptimer, !(value & CONTROL_CONT)) could be used here and "manual" re-run dropped from the timer_hit() handler.

So who will re-run the timer if it's configured in the continuous mode if it's not re-run in the timer_hit() ?

Ptimer will, timer_hit() will only have to assert IRQ and set ptimer count to the limit tvalue if timer is in the oneshot mode.

Something like:

static void timer_hit(void *opaque)

{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];
Quoted 7 lines
Quoted 4 lines
    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;

There should probably be } else { statement here , no ?

No, ptimer would re-load counter when it is running in the periodic mode. In the oneshot mode, ptimer is stopped here and it's counter set to 0. According to the datasheet, counter is reloaded once timer is expired regardless of the mode, so ptimer_set_count() is needed here only for the oneshot mode.

Quoted 34 lines
Quoted 5 lines
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}

Thanks for the draft.

[...]

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

ptimer_get_limit() could be used here.

You probably want to use the value in the register instead of the value in the ptimer state in case someone rewrote those registers, no ?

Quoted 8 lines

In case someone rewrote the registers, the ptimer limit would be also updated. So this could be changed to:

uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

Actually, R_PERIODL and R_PERIODH regs aren't really needed and could be removed from the AlteraTimer state, since ptimer stores the same limit value + 1. The timer_read/write() would need to be changed accordingly.

Ha, so we're getting to something like the following code (based on your example + the else bit) ?

static void timer_hit(void *opaque)

Quoted 15 lines
{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;
    } else {
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}

The "const" could be omitted and seem "else" bit was correct in my previous sample.

Quoted 25 lines

[...]

Quoted 11 lines
Quoted 10 lines

For the timer, that reset would look like this?

197 static void altera_timer_reset(DeviceState *dev) 198 {
199 AlteraTimer *t = ALTERA_TIMER(dev);
200
201 ptimer_stop(t->ptimer);
202 memset(t->regs, 0, ARRAY_SIZE(t->regs));
203 }

Quoted 2 lines

Yes, and move ptimer_set_limit(t->ptimer, 0xffffffff, 1) here from the altera_timer_realize().

Got it, thanks.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static Property altera_timer_properties[] = {
+    DEFINE_PROP_UINT32("clock-frequency", AlteraTimer, freq_hz, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};

Why not to have some sane "clock-frequency" by default?

Quoted 3 lines

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

You could set to the one that is used by "10M50 GHRD" patch for example.

Quoted 12 lines
Quoted 6 lines
Quoted 3 lines

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

No, IRQ's would be "deasserted" by resetting CPUClass state, of which QEMU takes care itself. No action needed by the IIC.

I see, thanks :)

btw any chance someone can look at the other patches too ?

Signature · 1 line
Dmitry
#14 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 31 lines · Dmitry Osipenko
Quoted 30 lines · Marek Vasut

On 08/15/2016 01:39 PM, Dmitry Osipenko wrote:

[...]

Quoted 25 lines
Quoted 10 lines
Quoted 6 lines

Also, ptimer now supports "on the fly mode switch":

https://github.com/qemu/qemu/commit/869e92b5c392eb6b2c7b398b878c435442b8e9dd

ptimer_run(t->ptimer, !(value & CONTROL_CONT)) could be used here and "manual" re-run dropped from the timer_hit() handler.

So who will re-run the timer if it's configured in the continuous mode if it's not re-run in the timer_hit() ?

Ptimer will, timer_hit() will only have to assert IRQ and set ptimer count to the limit tvalue if timer is in the oneshot mode.

Something like:

static void timer_hit(void *opaque)

{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;
Quoted 7 lines
Quoted 2 lines

There should probably be } else { statement here , no ?

No, ptimer would re-load counter when it is running in the periodic mode. In the oneshot mode, ptimer is stopped here and it's counter set to 0. According to the datasheet, counter is reloaded once timer is expired regardless of the mode, so ptimer_set_count() is needed here only for the oneshot mode.

Ah I see, thanks!

Quoted 46 lines
Quoted 46 lines
Quoted 5 lines
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}

Thanks for the draft.

[...]

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

ptimer_get_limit() could be used here.

You probably want to use the value in the register instead of the value in the ptimer state in case someone rewrote those registers, no ?

Quoted 8 lines

In case someone rewrote the registers, the ptimer limit would be also updated. So this could be changed to:

uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

Actually, R_PERIODL and R_PERIODH regs aren't really needed and could be removed from the AlteraTimer state, since ptimer stores the same limit value + 1. The timer_read/write() would need to be changed accordingly.

Ha, so we're getting to something like the following code (based on your example + the else bit) ?

static void timer_hit(void *opaque)

{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;
    } else {
	ptimer_set_count(t->ptimer, tvalue);
    }
Quoted 6 lines
Quoted 3 lines
    qemu_set_irq(t->irq, timer_irq_state(t));
}

The "const" could be omitted and seem "else" bit was correct in my previous sample.

About the const, you'll never be assigning tvalue, so it should be const. Is qemu not strict about that or something ?

Quoted 25 lines
Quoted 25 lines

[...]

Quoted 11 lines
Quoted 10 lines

For the timer, that reset would look like this?

197 static void altera_timer_reset(DeviceState *dev) 198 {
199 AlteraTimer *t = ALTERA_TIMER(dev);
200
201 ptimer_stop(t->ptimer);
202 memset(t->regs, 0, ARRAY_SIZE(t->regs));
203 }

Quoted 2 lines

Yes, and move ptimer_set_limit(t->ptimer, 0xffffffff, 1) here from the altera_timer_realize().

Got it, thanks.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static Property altera_timer_properties[] = {
+    DEFINE_PROP_UINT32("clock-frequency", AlteraTimer, freq_hz, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};

Why not to have some sane "clock-frequency" by default?

Quoted 5 lines
Quoted 3 lines

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

You could set to the one that is used by "10M50 GHRD" patch for example.

That doesn't sound right . I can set it to 1 (Hz) , that should make it slow enough to signalize that something is broken while providing valid number.

Quoted 14 lines
Quoted 12 lines
Quoted 6 lines
Quoted 3 lines

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

No, IRQ's would be "deasserted" by resetting CPUClass state, of which QEMU takes care itself. No action needed by the IIC.

I see, thanks :)

btw any chance someone can look at the other patches too ?

Signature · 2 lines
Best regards,
Marek Vasut
#15 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 92 lines · Marek Vasut
Quoted 31 lines · Dmitry Osipenko
Quoted 30 lines · Marek Vasut

On 08/15/2016 01:39 PM, Dmitry Osipenko wrote:

[...]

Quoted 25 lines
Quoted 10 lines
Quoted 6 lines

Also, ptimer now supports "on the fly mode switch":

https://github.com/qemu/qemu/commit/869e92b5c392eb6b2c7b398b878c435442b8e9dd

ptimer_run(t->ptimer, !(value & CONTROL_CONT)) could be used here and "manual" re-run dropped from the timer_hit() handler.

So who will re-run the timer if it's configured in the continuous mode if it's not re-run in the timer_hit() ?

Ptimer will, timer_hit() will only have to assert IRQ and set ptimer count to the limit tvalue if timer is in the oneshot mode.

Something like:

static void timer_hit(void *opaque)

{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;
Quoted 7 lines
Quoted 2 lines

There should probably be } else { statement here , no ?

No, ptimer would re-load counter when it is running in the periodic mode. In the oneshot mode, ptimer is stopped here and it's counter set to 0. According to the datasheet, counter is reloaded once timer is expired regardless of the mode, so ptimer_set_count() is needed here only for the oneshot mode.

Ah I see, thanks!

Quoted 50 lines
Quoted 49 lines
Quoted 5 lines
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}

Thanks for the draft.

[...]

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static void timer_hit(void *opaque)
+{
+    AlteraTimer *t = opaque;
+    const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL];

ptimer_get_limit() could be used here.

You probably want to use the value in the register instead of the value in the ptimer state in case someone rewrote those registers, no ?

Quoted 8 lines

In case someone rewrote the registers, the ptimer limit would be also updated. So this could be changed to:

uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

Actually, R_PERIODL and R_PERIODH regs aren't really needed and could be removed from the AlteraTimer state, since ptimer stores the same limit value + 1. The timer_read/write() would need to be changed accordingly.

Ha, so we're getting to something like the following code (based on your example + the else bit) ?

static void timer_hit(void *opaque)

{
    AlteraTimer *t = opaque;
    const uint64_t tvalue = ptimer_get_limit(t->ptimer) - 1;

    t->regs[R_STATUS] |= STATUS_TO;

    if (!(t->regs[R_CONTROL] & CONTROL_CONT)) {
        t->regs[R_STATUS] &= ~STATUS_RUN;
    } else {
	ptimer_set_count(t->ptimer, tvalue);
    }

    qemu_set_irq(t->irq, timer_irq_state(t));
}
Quoted 6 lines
Quoted 2 lines

The "const" could be omitted and seem "else" bit was correct in my previous sample.

About the const, you'll never be assigning tvalue, so it should be const. Is qemu not strict about that or something ?

QEMU isn't strict about it, keeping const is fine too.

Quoted 29 lines
Quoted 29 lines
Quoted 28 lines

[...]

Quoted 11 lines
Quoted 10 lines

For the timer, that reset would look like this?

197 static void altera_timer_reset(DeviceState *dev) 198 {
199 AlteraTimer *t = ALTERA_TIMER(dev);
200
201 ptimer_stop(t->ptimer);
202 memset(t->regs, 0, ARRAY_SIZE(t->regs));
203 }

Quoted 2 lines

Yes, and move ptimer_set_limit(t->ptimer, 0xffffffff, 1) here from the altera_timer_realize().

Got it, thanks.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static Property altera_timer_properties[] = {
+    DEFINE_PROP_UINT32("clock-frequency", AlteraTimer, freq_hz, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};

Why not to have some sane "clock-frequency" by default?

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

Quoted 6 lines
Quoted 1 line

You could set to the one that is used by "10M50 GHRD" patch for example.

That doesn't sound right . I can set it to 1 (Hz) , that should make it slow enough to signalize that something is broken while providing valid number.

I'm not sure about it. Explicitly showing that something is wrong would be better.

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void altera_timer_realize(DeviceState *dev, Error **errp)
+{
+    AlteraTimer *t = ALTERA_TIMER(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    assert(t->freq_hz != 0);

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \
                     "must be provided");
    return;
}
Quoted 16 lines
Quoted 14 lines
Quoted 12 lines
Quoted 6 lines
Quoted 3 lines

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

No, IRQ's would be "deasserted" by resetting CPUClass state, of which QEMU takes care itself. No action needed by the IIC.

I see, thanks :)

btw any chance someone can look at the other patches too ?

Signature · 1 line
Dmitry
#16 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

On 08/16/2016 11:38 PM, Dmitry Osipenko wrote:

[...]

Quoted 23 lines
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

You could set to the one that is used by "10M50 GHRD" patch for example.

That doesn't sound right . I can set it to 1 (Hz) , that should make it slow enough to signalize that something is broken while providing valid number.

I'm not sure about it. Explicitly showing that something is wrong would be better.

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void altera_timer_realize(DeviceState *dev, Error **errp)
+{
+    AlteraTimer *t = ALTERA_TIMER(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    assert(t->freq_hz != 0);

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

Quoted 5 lines
if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \
                     "must be provided");
    return;
}

That's better, thanks.

btw breaking strings is frowned upon in linux/u-boot as it makes it impossible to git grep for error message. Does the same apply to qemu?

Quoted 18 lines
Quoted 16 lines
Quoted 14 lines
Quoted 12 lines
Quoted 6 lines
Quoted 3 lines

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

No, IRQ's would be "deasserted" by resetting CPUClass state, of which QEMU takes care itself. No action needed by the IIC.

I see, thanks :)

btw any chance someone can look at the other patches too ?

Signature · 2 lines
Best regards,
Marek Vasut
#17 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 6 lines · Marek Vasut
Quoted 5 lines · Dmitry Osipenko

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \

You don't need the '\' at the end of the line here.

Quoted 8 lines
Quoted 3 lines
                     "must be provided");
    return;
}

That's better, thanks.

btw breaking strings is frowned upon in linux/u-boot as it makes it impossible to git grep for error message. Does the same apply to qemu?

We tend to prefer to keep the lines below our line length limit, even if that means splitting them sometimes.

thanks
-- PMM

#18 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 14 lines · Peter Maydell
Quoted 6 lines · Marek Vasut
Quoted 5 lines · Dmitry Osipenko

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \

You don't need the '\' at the end of the line here.

Quoted 4 lines
Quoted 3 lines
                     "must be provided");
    return;
}
Quoted 7 lines
Quoted 4 lines

That's better, thanks.

btw breaking strings is frowned upon in linux/u-boot as it makes it impossible to git grep for error message. Does the same apply to qemu?

We tend to prefer to keep the lines below our line length limit, even if that means splitting them sometimes.

Is there any reason for that except for "we always did it that way" ? Linux did it that way before, but then added this exception to make it possible to grep for those strings.

Thanks

Quoted 3 lines

thanks
-- PMM

Signature · 2 lines
Best regards,
Marek Vasut
#19 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

In reply to Marek Vasut
Quoted 33 lines · Marek Vasut

On 08/16/2016 11:38 PM, Dmitry Osipenko wrote:

[...]

Quoted 23 lines
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

You could set to the one that is used by "10M50 GHRD" patch for example.

That doesn't sound right . I can set it to 1 (Hz) , that should make it slow enough to signalize that something is broken while providing valid number.

I'm not sure about it. Explicitly showing that something is wrong would be better.

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void altera_timer_realize(DeviceState *dev, Error **errp)
+{
+    AlteraTimer *t = ALTERA_TIMER(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    assert(t->freq_hz != 0);

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

Quoted 5 lines
if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \
                     "must be provided");
    return;
}
Quoted 5 lines

That's better, thanks.

btw breaking strings is frowned upon in linux/u-boot as it makes it impossible to git grep for error message. Does the same apply to qemu?

Actually, "altera_timer: " prefix isn't needed, as it should be already included in the error message produced by by the error_setg(), so that string could be squeezed into one line. And, of course, it could be rephrased more concisely.

Quoted 20 lines
Quoted 18 lines
Quoted 16 lines
Quoted 14 lines
Quoted 12 lines
Quoted 6 lines
Quoted 3 lines

For the IIC, I wonder what that'd look like -- probably qemu_set_irq(parent, 0); ?

No, IRQ's would be "deasserted" by resetting CPUClass state, of which QEMU takes care itself. No action needed by the IIC.

I see, thanks :)

btw any chance someone can look at the other patches too ?

Signature · 1 line
Dmitry
#20 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 36 lines · Dmitry Osipenko
Quoted 35 lines · Marek Vasut

On 08/16/2016 11:38 PM, Dmitry Osipenko wrote:

[...]

Quoted 23 lines
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

You could set to the one that is used by "10M50 GHRD" patch for example.

That doesn't sound right . I can set it to 1 (Hz) , that should make it slow enough to signalize that something is broken while providing valid number.

I'm not sure about it. Explicitly showing that something is wrong would be better.

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void altera_timer_realize(DeviceState *dev, Error **errp)
+{
+    AlteraTimer *t = ALTERA_TIMER(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    assert(t->freq_hz != 0);

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

Quoted 5 lines
if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \
                     "must be provided");
    return;
}

That's better, thanks.

Quoted 7 lines
Quoted 3 lines

btw breaking strings is frowned upon in linux/u-boot as it makes it impossible to git grep for error message. Does the same apply to qemu?

Actually, "altera_timer: " prefix isn't needed, as it should be already included in the error message produced by by the error_setg(), so that string could be squeezed into one line. And, of course, it could be rephrased more concisely.

Even better, prefix dropped. Thanks

So what about patches 1..5 ? Anything I should change there ?

Signature · 2 lines
Best regards,
Marek Vasut
#21 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 41 lines · Marek Vasut
Quoted 40 lines · Dmitry Osipenko
Quoted 38 lines · Marek Vasut

On 08/16/2016 11:38 PM, Dmitry Osipenko wrote:

[...]

Quoted 23 lines
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

You could set to the one that is used by "10M50 GHRD" patch for example.

That doesn't sound right . I can set it to 1 (Hz) , that should make it slow enough to signalize that something is broken while providing valid number.

I'm not sure about it. Explicitly showing that something is wrong would be better.

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void altera_timer_realize(DeviceState *dev, Error **errp)
+{
+    AlteraTimer *t = ALTERA_TIMER(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    assert(t->freq_hz != 0);

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

Quoted 5 lines
if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \
                     "must be provided");
    return;
}

That's better, thanks.

btw breaking strings is frowned upon in linux/u-boot as it makes it impossible to git grep for error message. Does the same apply to qemu?

Quoted 8 lines
Quoted 3 lines

Actually, "altera_timer: " prefix isn't needed, as it should be already included in the error message produced by by the error_setg(), so that string could be squeezed into one line. And, of course, it could be rephrased more concisely.

Even better, prefix dropped. Thanks

So what about patches 1..5 ? Anything I should change there ?

Unfortunately, I don't have a good sense of bad in those patches. I guess maintainers are currently busy with a 2.7 release, and you are probably not the only one in the review queue. Just wait for now, it could take a while.

Signature · 1 line
Dmitry
#22 lore raw

Re: [Qemu-devel] [PATCH 5/7] nios2: Add periodic timer emulation

Quoted 46 lines · Dmitry Osipenko
Quoted 45 lines · Marek Vasut
Quoted 43 lines · Dmitry Osipenko
Quoted 33 lines · Marek Vasut

On 08/16/2016 11:38 PM, Dmitry Osipenko wrote:

[...]

Quoted 23 lines
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

Well what is sane clock frequency for hardware which can have arbitrary frequency configured in ?

You could set to the one that is used by "10M50 GHRD" patch for example.

That doesn't sound right . I can set it to 1 (Hz) , that should make it slow enough to signalize that something is broken while providing valid number.

I'm not sure about it. Explicitly showing that something is wrong would be better.

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void altera_timer_realize(DeviceState *dev, Error **errp)
+{
+    AlteraTimer *t = ALTERA_TIMER(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    assert(t->freq_hz != 0);

If you would prefer to keep error'ing out, then I can suggest to add some verbose message instead of the assertion, like:

Quoted 5 lines
if (!t->freq_hz) {
    error_setg(errp, "altera_timer: \"clock-frequency\" property " \
                     "must be provided");
    return;
}
Quoted 5 lines

That's better, thanks.

btw breaking strings is frowned upon in linux/u-boot as it makes it impossible to git grep for error message. Does the same apply to qemu?

Actually, "altera_timer: " prefix isn't needed, as it should be already included in the error message produced by by the error_setg(), so that string could be squeezed into one line. And, of course, it could be rephrased more concisely.

Quoted 9 lines
Quoted 4 lines

Even better, prefix dropped. Thanks

So what about patches 1..5 ? Anything I should change there ?

Unfortunately, I don't have a good sense of bad in those patches. I guess maintainers are currently busy with a 2.7 release, and you are probably not the only one in the review queue. Just wait for now, it could take a while.

That makes sense.

Signature · 2 lines
Best regards,
Marek Vasut
#23 lore raw

PATCH 6/7 nios2: Add Altera 10M50 GHRD emulation

In reply to Marek Vasut

Add the Altera 10M50 Nios2 GHRD model. This allows emulating the 10M50 development kit with the Nios2 GHRD loaded in the FPGA. It is possible to boot Linux kernel and run userspace, thus far only from initrd as storage support is not yet implemented.

Cc Chris Wulff <[email protected]>
Cc Jeff Da Silva <[email protected]>
Cc Ley Foon Tan <[email protected]>
Cc Sandra Loosemore <[email protected]>
Cc Yves Vandervennet <[email protected]>
---
 hw/nios2/10m50_devboard.c | 125 ++++++++++++++++++++++++++
 hw/nios2/Makefile.objs    |   1 +
 hw/nios2/boot.c           | 223 ++++++++++++++++++++++++++++++++++++++++++++++
 hw/nios2/boot.h           |  11 +++
 hw/nios2/cpu_pic.c        |  74 +++++++++++++++
 5 files changed, 434 insertions(+)
 create mode 100644 hw/nios2/10m50_devboard.c
 create mode 100644 hw/nios2/Makefile.objs
 create mode 100644 hw/nios2/boot.c
 create mode 100644 hw/nios2/boot.h
 create mode 100644 hw/nios2/cpu_pic.c
hw/nios2/10m50_devboard.c new file +125 -0
new file mode 100644
index 0000000..8312fd9
@@ -0,0 +1,125 @@
+/*
+ * Altera 10M50 Nios2 GHRD
+ *
+ * Copyright (c) 2016 Marek Vasut <[email protected]>
+ *
+ * Based on LabX device code
+ *
+ * Copyright (c) 2012 Chris Wulff <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/lgpl-2.1.html>
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu-common.h"
+#include "cpu.h"
+
+#include "hw/sysbus.h"
+#include "hw/hw.h"
+#include "hw/char/serial.h"
+#include "sysemu/sysemu.h"
+#include "hw/boards.h"
+#include "exec/memory.h"
+#include "exec/address-spaces.h"
+#include "qemu/config-file.h"
+
+#include "boot.h"
+
+#define BINARY_DEVICE_TREE_FILE		"10m50-devboard.dtb"
+
+static void nios2_10m50_ghrd_init(MachineState *machine)
+{
+    Nios2CPU *cpu;
+    DeviceState *dev;
+    MemoryRegion *address_space_mem = get_system_memory();
+    MemoryRegion *phys_tcm = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_tcm_alias = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram_alias = g_new(MemoryRegion, 1);
+    ram_addr_t tcm_base = 0x0;
+    ram_addr_t tcm_size = 0x1000;    /* 1 kiB, but QEMU limit is 4 kiB */
+    ram_addr_t ram_base = 0x08000000;
+    ram_addr_t ram_size = 0x08000000;
+    qemu_irq *cpu_irq, irq[32];
+    int i;
+
+    /* Physical TCM (tb_ram_1k) with alias at 0xc0000000 */
+    memory_region_init_ram(phys_tcm, NULL, "nios2.tcm", tcm_size, &error_abort);
+    memory_region_init_alias(phys_tcm_alias, NULL, "nios2.tcm.alias",
+                             phys_tcm, 0, tcm_size);
+    vmstate_register_ram_global(phys_tcm);
+    memory_region_add_subregion(address_space_mem, tcm_base, phys_tcm);
+    memory_region_add_subregion(address_space_mem, 0xc0000000 + tcm_base,
+                                phys_tcm_alias);
+
+    /* Physical DRAM with alias at 0xc0000000 */
+    memory_region_init_ram(phys_ram, NULL, "nios2.ram", ram_size, &error_abort);
+    memory_region_init_alias(phys_ram_alias, NULL, "nios2.ram.alias",
+                             phys_ram, 0, ram_size);
+    vmstate_register_ram_global(phys_ram);
+    memory_region_add_subregion(address_space_mem, ram_base, phys_ram);
+    memory_region_add_subregion(address_space_mem, 0xc0000000 + ram_base,
+                                phys_ram_alias);
+
+    /* Create CPU -- FIXME */
+    cpu = cpu_nios2_init("nios2");
+
+    /* Register: CPU interrupt controller (PIC) */
+    cpu_irq = nios2_cpu_pic_init(cpu);
+
+    /* Register: Internal Interrupt Controller (IIC) */
+    dev = qdev_create(NULL, "altera,iic");
+    qdev_prop_set_ptr(dev, "cpu", cpu);
+    qdev_init_nofail(dev);
+    sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, cpu_irq[0]);
+    for (i = 0; i < 32; i++)
+        irq[i] = qdev_get_gpio_in(dev, i);
+
+    /* Register: Altera 16550 UART */
+    serial_mm_init(address_space_mem, 0xf8001600, 2, irq[1], 115200,
+                   serial_hds[0], DEVICE_NATIVE_ENDIAN);
+
+    /* Register: Timer sys_clk_timer  */
+    dev = qdev_create(NULL, "ALTR.timer");
+    qdev_prop_set_uint32(dev, "clock-frequency", 75 * 1000000);
+    qdev_init_nofail(dev);
+    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, 0xf8001440);
+    sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq[0]);
+
+    /* Register: Timer sys_clk_timer_1  */
+    dev = qdev_create(NULL, "ALTR.timer");
+    qdev_prop_set_uint32(dev, "clock-frequency", 75 * 1000000);
+    qdev_init_nofail(dev);
+    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, 0xe0000880);
+    sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq[5]);
+
+    /* Configure new exception vectors and reset CPU for it to take effect. */
+    cpu->env.reset_addr = 0xd4000000;
+    cpu->env.exception_addr = 0xc8000120;
+    cpu->env.fast_tlb_miss_addr = 0xc0000100;
+
+    nios2_load_kernel(cpu, ram_base, ram_size, machine->initrd_filename,
+                      BINARY_DEVICE_TREE_FILE, NULL);
+}
+
+static void nios2_10m50_ghrd_machine_init(struct MachineClass *mc)
+{
+    mc->desc = "Altera 10M50 GHRD Nios II design";
+    mc->init = nios2_10m50_ghrd_init;
+    mc->is_default = 1;
+}
+
+DEFINE_MACHINE("10m50-ghrd", nios2_10m50_ghrd_machine_init);
hw/nios2/Makefile.objs new file +1 -0
new file mode 100644
index 0000000..6b5c421
@@ -0,0 +1 @@
+obj-y = boot.o cpu_pic.o 10m50_devboard.o
hw/nios2/boot.c new file +223 -0

223 lines not on this page · raw message

hw/nios2/boot.h new file +11 -0
new file mode 100644
index 0000000..edf3046
@@ -0,0 +1,11 @@
+#ifndef __NIOS2_BOOT__
+#define __NIOS2_BOOT__
+
+#include "hw/hw.h"
+#include "cpu.h"
+
+void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base, uint32_t ramsize,
+                       const char *initrd_filename, const char *dtb_filename,
+                       void (*machine_cpu_reset)(Nios2CPU *));
+
+#endif /* __NIOS2_BOOT__ */
hw/nios2/cpu_pic.c new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.8.1
#24 lore raw

Re: [Qemu-devel] [PATCH 6/7] nios2: Add Altera 10M50 GHRD emulation

Quoted 5 lines · Marek Vasut

Add the Altera 10M50 Nios2 GHRD model. This allows emulating the 10M50 development kit with the Nios2 GHRD loaded in the FPGA. It is possible to boot Linux kernel and run userspace, thus far only from initrd as storage support is not yet implemented.

[snip]

Quoted 1 line

+++ b/hw/nios2/cpu_pic.c

This file should be in the "Add architecture emulation support" patch.

Quoted 24 lines
hw/nios2/cpu_pic.c new file +23 -0
@@ -0,0 +1,74 @@
+/*
+ * Altera Nios2 CPU PIC
+ *
+ * Copyright (c) 2016 Marek Vasut <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/lgpl-2.1.html>
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu-common.h"

hw/nios2/cpu_pic.c line 33

Quoted 10 lines
hw/nios2/cpu_pic.c new file +10 -0
+#include "cpu.h"
+
+#include "qemu/config-file.h"
+
+#include "boot.h"
+
+#define BINARY_DEVICE_TREE_FILE		"10m50-devboard.dtb"
+
+uint32_t irq_pending;
+

Shouldn't "irq_pending" belong to the CPUNios2State?

Quoted 42 lines
hw/nios2/cpu_pic.c new file +41 -0
+static void nios2_pic_cpu_handler(void *opaque, int irq, int level)
+{
+    Nios2CPU *cpu = opaque;
+    CPUNios2State *env = &cpu->env;
+    CPUState *cs = CPU(cpu);
+    int type = irq ? CPU_INTERRUPT_NMI : CPU_INTERRUPT_HARD;
+
+    if (type == CPU_INTERRUPT_HARD) {
+        irq_pending = level;
+
+        if (level && (env->regs[CR_STATUS] & CR_STATUS_PIE)) {
+            irq_pending = 0;
+            cpu_interrupt(cs, type);
+        } else if (!level) {
+            irq_pending = 0;
+            cpu_reset_interrupt(cs, type);
+        }
+    } else {
+        if (level) {
+            cpu_interrupt(cs, type);
+        } else {
+            cpu_reset_interrupt(cs, type);
+        }
+    }
+}
+
+void nios2_check_interrupts(CPUNios2State *env)
+{
+    Nios2CPU *cpu = nios2_env_get_cpu(env);
+    CPUState *cs = CPU(cpu);
+
+    if (irq_pending) {
+        irq_pending = 0;
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+    }
+}
+
+qemu_irq *nios2_cpu_pic_init(Nios2CPU *cpu)
+{
+    return qemu_allocate_irqs(nios2_pic_cpu_handler, cpu, 2);
+}
Signature · 1 line
Dmitry
#25 lore raw

Re: [Qemu-devel] [PATCH 6/7] nios2: Add Altera 10M50 GHRD emulation

Quoted 7 lines · Dmitry Osipenko
Quoted 5 lines · Marek Vasut

Add the Altera 10M50 Nios2 GHRD model. This allows emulating the 10M50 development kit with the Nios2 GHRD loaded in the FPGA. It is possible to boot Linux kernel and run userspace, thus far only from initrd as storage support is not yet implemented.

Quoted 5 lines

[snip]

Quoted 1 line

+++ b/hw/nios2/cpu_pic.c

This file should be in the "Add architecture emulation support" patch.

Done

[...]

Quoted 10 lines

hw/nios2/10m50_devboard.c line 41

Quoted 10 lines
hw/nios2/10m50_devboard.c new file +10 -0
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu-common.h"
+#include "cpu.h"
+
+#include "qemu/config-file.h"
+
+#include "boot.h"
+
+#define BINARY_DEVICE_TREE_FILE		"10m50-devboard.dtb"

Uh, removed.

Quoted 4 lines

hw/nios2/cpu_pic.c line 33

Quoted 2 lines
hw/nios2/cpu_pic.c new file +2 -0
+uint32_t irq_pending;
+

Shouldn't "irq_pending" belong to the CPUNios2State?

Indeed, that makes sense in the QOM context.

btw Juro (CCed) has some patches to improve the IRQ controller handling, but that might need some further discussion.

Quoted 44 lines
Quoted 42 lines
hw/nios2/cpu_pic.c new file +41 -0
+static void nios2_pic_cpu_handler(void *opaque, int irq, int level)
+{
+    Nios2CPU *cpu = opaque;
+    CPUNios2State *env = &cpu->env;
+    CPUState *cs = CPU(cpu);
+    int type = irq ? CPU_INTERRUPT_NMI : CPU_INTERRUPT_HARD;
+
+    if (type == CPU_INTERRUPT_HARD) {
+        irq_pending = level;
+
+        if (level && (env->regs[CR_STATUS] & CR_STATUS_PIE)) {
+            irq_pending = 0;
+            cpu_interrupt(cs, type);
+        } else if (!level) {
+            irq_pending = 0;
+            cpu_reset_interrupt(cs, type);
+        }
+    } else {
+        if (level) {
+            cpu_interrupt(cs, type);
+        } else {
+            cpu_reset_interrupt(cs, type);
+        }
+    }
+}
+
+void nios2_check_interrupts(CPUNios2State *env)
+{
+    Nios2CPU *cpu = nios2_env_get_cpu(env);
+    CPUState *cs = CPU(cpu);
+
+    if (irq_pending) {
+        irq_pending = 0;
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+    }
+}
+
+qemu_irq *nios2_cpu_pic_init(Nios2CPU *cpu)
+{
+    return qemu_allocate_irqs(nios2_pic_cpu_handler, cpu, 2);
+}
Signature · 2 lines
Best regards,
Marek Vasut
#26 lore raw

PATCH 7/7 nios2: Add support for Nios-II R1

In reply to Marek Vasut

Add remaining bits of the Altera NiosII R1 support into qemu, which is documentation, MAINTAINERS file entry, configure bits, arch_init and configuration files for both linux-user (userland binaries) and softmmu (hardware emulation).

Cc Chris Wulff <[email protected]>
Cc Jeff Da Silva <[email protected]>
Cc Ley Foon Tan <[email protected]>
Cc Sandra Loosemore <[email protected]>
Cc Yves Vandervennet <[email protected]>
---
 MAINTAINERS                          | 8 ++++++++
 arch_init.c                          | 2 ++
 configure                            | 5 +++++
 default-configs/nios2-linux-user.mak | 1 +
 default-configs/nios2-softmmu.mak    | 6 ++++++
 include/sysemu/arch_init.h           | 1 +
 qemu-doc.texi                        | 3 +++
 7 files changed, 26 insertions(+)
 create mode 100644 default-configs/nios2-linux-user.mak
 create mode 100644 default-configs/nios2-softmmu.mak
MAINTAINERS +8 -0
index d1439a8..f7aa2ae 100644
@@ -157,6 +157,14 @@ S: Maintained
 F: target-moxie/
 F: disas/moxie.c
 
+NiosII
+M: Chris Wulff <[email protected]>
+M: Marek Vasut <[email protected]>
+S: Maintained
+F: target-nios2/
+F: hw/nios2/
+F: disas/nios2.c
+
 OpenRISC
 M: Jia Liu <[email protected]>
 S: Maintained
arch_init.c +2 -0
index fa05973..f63661c 100644
@@ -64,6 +64,8 @@ int graphic_depth = 32;
 #define QEMU_ARCH QEMU_ARCH_MIPS
 #elif defined(TARGET_MOXIE)
 #define QEMU_ARCH QEMU_ARCH_MOXIE
+#elif defined(TARGET_NIOS2)
+#define QEMU_ARCH QEMU_ARCH_NIOS2
 #elif defined(TARGET_OPENRISC)
 #define QEMU_ARCH QEMU_ARCH_OPENRISC
 #elif defined(TARGET_PPC)
configure +5 -0
index 879324b..d46ae63 100755
@@ -5679,6 +5679,8 @@ case "$target_name" in
   ;;
   moxie)
   ;;
+  nios2)
+  ;;
   or32)
     TARGET_ARCH=openrisc
     TARGET_BASE_ARCH=openrisc
@@ -5875,6 +5877,9 @@ for i in $ARCH $TARGET_BASE_ARCH ; do
   moxie*)
     disas_config "MOXIE"
   ;;
+  nios2)
+    disas_config "NIOS2"
+  ;;
   or32)
     disas_config "OPENRISC"
   ;;
default-configs/nios2-linux-user.mak new file +1 -0
new file mode 100644
index 0000000..5be3eb7
@@ -0,0 +1 @@
+# Default configuration for nios2-linux-user
default-configs/nios2-softmmu.mak new file +6 -0
new file mode 100644
index 0000000..74dc70c
@@ -0,0 +1,6 @@
+# Default configuration for nios2-softmmu
+
+CONFIG_NIOS2=y
+CONFIG_SERIAL=y
+CONFIG_PTIMER=y
+CONFIG_ALTERA_TIMER=y
include/sysemu/arch_init.h +1 -0
index d690dfa..cf70976 100644
@@ -23,6 +23,7 @@ enum {
     QEMU_ARCH_UNICORE32 = (1 << 14),
     QEMU_ARCH_MOXIE = (1 << 15),
     QEMU_ARCH_TRICORE = (1 << 16),
+    QEMU_ARCH_NIOS2 = (1 << 17),
 };
 
 extern const uint32_t arch_type;
qemu-doc.texi +3 -0
index f37fd31..1e40d09 100644
@@ -2856,6 +2856,9 @@ The binary format is detected automatically.
 @command{qemu-mips} TODO.
 @command{qemu-mipsel} TODO.
 
+@cindex user mode (NiosII)
+@command{qemu-nios2} TODO.
+
 @cindex user mode (PowerPC)
 @command{qemu-ppc64abi32} TODO.
 @command{qemu-ppc64} TODO.
Signature · 1 line
2.8.1