All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xiaojuan Yang <yangxiaojuan@loongson.cn>
To: qemu-devel@nongnu.org
Cc: richard.henderson@linaro.org, gaosong@loongson.cn,
	mark.cave-ayland@ilande.co.uk, mst@redhat.com,
	imammedo@redhat.com, ani@anisinha.ca
Subject: [PATCH v4 24/43] target/loongarch: Add constant timer support
Date: Tue, 17 May 2022 19:30:04 +0800	[thread overview]
Message-ID: <20220517113023.3051143-25-yangxiaojuan@loongson.cn> (raw)
In-Reply-To: <20220517113023.3051143-1-yangxiaojuan@loongson.cn>

Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn>
Signed-off-by: Song Gao <gaosong@loongson.cn>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 target/loongarch/constant_timer.c | 64 +++++++++++++++++++++++++++++++
 target/loongarch/cpu.c            |  2 +
 target/loongarch/cpu.h            |  4 ++
 target/loongarch/internals.h      |  6 +++
 target/loongarch/meson.build      |  1 +
 5 files changed, 77 insertions(+)
 create mode 100644 target/loongarch/constant_timer.c

diff --git a/target/loongarch/constant_timer.c b/target/loongarch/constant_timer.c
new file mode 100644
index 0000000000..1851f53fd6
--- /dev/null
+++ b/target/loongarch/constant_timer.c
@@ -0,0 +1,64 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * QEMU LoongArch constant timer support
+ *
+ * Copyright (c) 2021 Loongson Technology Corporation Limited
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/timer.h"
+#include "cpu.h"
+#include "internals.h"
+#include "cpu-csr.h"
+
+#define TIMER_PERIOD                10 /* 10 ns period for 100 MHz frequency */
+#define CONSTANT_TIMER_TICK_MASK    0xfffffffffffcUL
+#define CONSTANT_TIMER_ENABLE       0x1UL
+
+uint64_t cpu_loongarch_get_constant_timer_counter(LoongArchCPU *cpu)
+{
+    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / TIMER_PERIOD;
+}
+
+uint64_t cpu_loongarch_get_constant_timer_ticks(LoongArchCPU *cpu)
+{
+    uint64_t now, expire;
+
+    now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+    expire = timer_expire_time_ns(&cpu->timer);
+
+    return (expire - now) / TIMER_PERIOD;
+}
+
+void cpu_loongarch_store_constant_timer_config(LoongArchCPU *cpu,
+                                               uint64_t value)
+{
+    CPULoongArchState *env = &cpu->env;
+    uint64_t now, next;
+
+    env->CSR_TCFG = value;
+    if (value & CONSTANT_TIMER_ENABLE) {
+        now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        next = now + (value & CONSTANT_TIMER_TICK_MASK) * TIMER_PERIOD;
+        timer_mod(&cpu->timer, next);
+    } else {
+        timer_del(&cpu->timer);
+    }
+}
+
+void loongarch_constant_timer_cb(void *opaque)
+{
+    LoongArchCPU *cpu  = opaque;
+    CPULoongArchState *env = &cpu->env;
+    uint64_t now, next;
+
+    if (FIELD_EX64(env->CSR_TCFG, CSR_TCFG, PERIODIC)) {
+        now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        next = now + (env->CSR_TCFG & CONSTANT_TIMER_TICK_MASK) * TIMER_PERIOD;
+        timer_mod(&cpu->timer, next);
+    } else {
+        env->CSR_TCFG = FIELD_DP64(env->CSR_TCFG, CSR_TCFG, EN, 0);
+    }
+
+    loongarch_cpu_set_irq(opaque, IRQ_TIMER, 1);
+}
diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
index e3b220182b..2afbd1fd3c 100644
--- a/target/loongarch/cpu.c
+++ b/target/loongarch/cpu.c
@@ -489,6 +489,8 @@ static void loongarch_cpu_init(Object *obj)
 
     cpu_set_cpustate_pointers(cpu);
     qdev_init_gpio_in(DEVICE(cpu), loongarch_cpu_set_irq, N_IRQS);
+    timer_init_ns(&cpu->timer, QEMU_CLOCK_VIRTUAL,
+                  &loongarch_constant_timer_cb, cpu);
 }
 
 static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h
index b983ce241c..2081902f2e 100644
--- a/target/loongarch/cpu.h
+++ b/target/loongarch/cpu.h
@@ -11,6 +11,7 @@
 #include "exec/cpu-defs.h"
 #include "fpu/softfloat-types.h"
 #include "hw/registerfields.h"
+#include "qemu/timer.h"
 
 #define TCG_GUEST_DEFAULT_MO (0)
 
@@ -185,6 +186,8 @@ extern const char * const regnames[32];
 extern const char * const fregnames[32];
 
 #define N_IRQS      13
+#define IRQ_TIMER   11
+#define IRQ_IPI     12
 
 #define LOONGARCH_STLB         2048 /* 2048 STLB */
 #define LOONGARCH_MTLB         64   /* 64 MTLB */
@@ -295,6 +298,7 @@ struct ArchCPU {
 
     CPUNegativeOffsetState neg;
     CPULoongArchState env;
+    QEMUTimer timer;
 };
 
 #define TYPE_LOONGARCH_CPU "loongarch-cpu"
diff --git a/target/loongarch/internals.h b/target/loongarch/internals.h
index 69183e8bb5..4b1bcd7c0f 100644
--- a/target/loongarch/internals.h
+++ b/target/loongarch/internals.h
@@ -32,6 +32,12 @@ extern const VMStateDescription vmstate_loongarch_cpu;
 
 void loongarch_cpu_set_irq(void *opaque, int irq, int level);
 
+void loongarch_constant_timer_cb(void *opaque);
+uint64_t cpu_loongarch_get_constant_timer_counter(LoongArchCPU *cpu);
+uint64_t cpu_loongarch_get_constant_timer_ticks(LoongArchCPU *cpu);
+void cpu_loongarch_store_constant_timer_config(LoongArchCPU *cpu,
+                                               uint64_t value);
+
 bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                             MMUAccessType access_type, int mmu_idx,
                             bool probe, uintptr_t retaddr);
diff --git a/target/loongarch/meson.build b/target/loongarch/meson.build
index 435cc75999..04e15ba1e3 100644
--- a/target/loongarch/meson.build
+++ b/target/loongarch/meson.build
@@ -18,6 +18,7 @@ loongarch_softmmu_ss = ss.source_set()
 loongarch_softmmu_ss.add(files(
   'machine.c',
   'tlb_helper.c',
+  'constant_timer.c',
 ))
 
 loongarch_ss.add_all(when: 'CONFIG_TCG', if_true: [loongarch_tcg_ss])
-- 
2.31.1



  parent reply	other threads:[~2022-05-17 13:05 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-17 11:29 [PATCH v4 00/43] Add LoongArch softmmu support Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 01/43] target/loongarch: Add README Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 02/43] target/loongarch: Add core definition Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 03/43] target/loongarch: Add main translation routines Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 04/43] target/loongarch: Add fixed point arithmetic instruction translation Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 05/43] target/loongarch: Add fixed point shift " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 06/43] target/loongarch: Add fixed point bit " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 07/43] target/loongarch: Add fixed point load/store " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 08/43] target/loongarch: Add fixed point atomic " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 09/43] target/loongarch: Add fixed point extra " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 10/43] target/loongarch: Add floating point arithmetic " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 11/43] target/loongarch: Add floating point comparison " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 12/43] target/loongarch: Add floating point conversion " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 13/43] target/loongarch: Add floating point move " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 14/43] target/loongarch: Add floating point load/store " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 15/43] target/loongarch: Add branch " Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 16/43] target/loongarch: Add disassembler Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 17/43] target/loongarch: Add target build suport Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 18/43] target/loongarch: Add system emulation introduction Xiaojuan Yang
2022-05-17 11:29 ` [PATCH v4 19/43] target/loongarch: Add CSRs definition Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 20/43] target/loongarch: Add basic vmstate description of CPU Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 21/43] target/loongarch: Implement qmp_query_cpu_definitions() Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 22/43] target/loongarch: Add MMU support for LoongArch CPU Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 23/43] target/loongarch: Add LoongArch interrupt and exception handle Xiaojuan Yang
2022-05-17 11:30 ` Xiaojuan Yang [this message]
2022-05-17 11:30 ` [PATCH v4 25/43] target/loongarch: Add LoongArch CSR instruction Xiaojuan Yang
2022-05-18 17:18   ` Richard Henderson
2022-05-17 11:30 ` [PATCH v4 26/43] target/loongarch: Add LoongArch IOCSR instruction Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 27/43] target/loongarch: Add TLB instruction support Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 28/43] target/loongarch: Add other core instructions support Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 29/43] target/loongarch: Add timer related " Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 30/43] hw/loongarch: Add support loongson3 virt machine type Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 31/43] hw/loongarch: Add LoongArch ipi interrupt support(IPI) Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 32/43] hw/intc: Add LoongArch ls7a interrupt controller support(PCH-PIC) Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 33/43] hw/intc: Add LoongArch ls7a msi interrupt controller support(PCH-MSI) Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 34/43] hw/intc: Add LoongArch extioi interrupt controller(EIOINTC) Xiaojuan Yang
2022-05-18 18:04   ` Richard Henderson
2022-05-19  3:03     ` yangxiaojuan
2022-05-17 11:30 ` [PATCH v4 35/43] hw/loongarch: Add irq hierarchy for the system Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 36/43] Enable common virtio pci support for LoongArch Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 37/43] hw/loongarch: Add some devices support for 3A5000 Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 38/43] hw/loongarch: Add LoongArch ls7a rtc device support Xiaojuan Yang
2022-05-18 19:59   ` Richard Henderson
2022-05-19 13:04     ` yangxiaojuan
2022-05-19 15:24       ` Richard Henderson
2022-05-20 10:07         ` yangxiaojuan
2022-05-17 11:30 ` [PATCH v4 39/43] hw/loongarch: Add LoongArch load elf function Xiaojuan Yang
2022-05-18 20:01   ` Richard Henderson
2022-05-17 11:30 ` [PATCH v4 40/43] hw/loongarch: Add LoongArch ls7a acpi device support Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 41/43] target/loongarch: Add gdb support Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 42/43] tests/tcg/loongarch64: Add hello/memory test in loongarch64 system Xiaojuan Yang
2022-05-17 11:30 ` [PATCH v4 43/43] target/loongarch: 'make check-tcg' support Xiaojuan Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220517113023.3051143-25-yangxiaojuan@loongson.cn \
    --to=yangxiaojuan@loongson.cn \
    --cc=ani@anisinha.ca \
    --cc=gaosong@loongson.cn \
    --cc=imammedo@redhat.com \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.