All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/20] hw/mips: Set CPU frequency
@ 2020-10-10 20:42 Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 01/20] util/cutils: Introduce freq_to_str() to display Hertz units Philippe Mathieu-Daudé
                   ` (19 more replies)
  0 siblings, 20 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:42 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Since v2:
- Renamed "clk" -> "clk-in"
- Renamed "cpuclk-out -> "cpu-refclk"

Missing review: patches 6-20

~~~

All the MIPS cores emulated by QEMU provides the Coproc#0
'Count' register which can be used as a free running timer.

Since it's introduction in 2005 this timer uses a fixed
frequency of 100 MHz (for a CPU freq of 200 MHz).
While this is not an issue with Linux guests, it makes
some firmwares behave incorrectly.

The Clock API allow propagating clocks. It is particularly
useful when hardware dynamicly changes clock frequencies.

To be able to model such MIPS hardware, we need to refactor
the MIPS hardware code to handle clocks.

This series is organized as follow:

1/ qdev/clock patches already reviewed but not merged

2/ refactor the CP0 timer period to allow dynamic changes

3/ MIPS CPU get an optional input clock

4/ set correct CPU frequencies to all boards

I used a MIPSsim test suggested by Thomas.
The test is available on the list:
https://mid.mail-archive.com/20200928171539.788309-17-f4bug@amsat.org

Possible follow up:
- QOM'ify the GIC
- let the GIC handle dynamic clock changes

Regards,

Phil.

Luc Michel (1):
  hw/core/clock: add the clock_new helper function

Philippe Mathieu-Daudé (19):
  util/cutils: Introduce freq_to_str() to display Hertz units
  qdev-monitor: Display frequencies scaled to SI unit
  hw/qdev-clock: Display error hint when clock is missing from device
  target/mips: Move cpu_mips_get_random() with CP0 helpers
  target/mips/cp0_timer: Explicit unit in variable name
  target/mips/cp0_timer: Document TIMER_PERIOD origin
  target/mips: Move cp0_count_ns to CPUMIPSState
  target/mips/cpu: Calculate the CP0 timer period using the CPU
    frequency
  target/mips/cpu: Make cp0_count_rate a property
  target/mips/cpu: Allow the CPU to use dynamic frequencies
  hw/mips/r4k: Explicit CPU frequency is 200 MHz
  hw/mips/fuloong2e: Set CPU frequency to 533 MHz
  hw/mips/mipssim: Correct CPU frequency
  hw/mips/jazz: Correct CPU frequencies
  hw/mips/cps: Expose input clock and connect it to CPU cores
  hw/mips/boston: Set CPU frequency to 1 GHz
  hw/mips/malta: Set CPU frequency to 320 MHz
  hw/mips/cps: Do not allow use without input clock
  target/mips/cpu: Display warning when CPU is used without input clock

 include/hw/clock.h       | 13 ++++++++++
 include/hw/mips/cps.h    |  2 ++
 include/qemu/cutils.h    | 12 ++++++++++
 target/mips/cpu.h        | 14 +++++++++++
 target/mips/internal.h   |  2 +-
 hw/core/clock.c          | 15 ++++++++++++
 hw/core/qdev-clock.c     | 11 +++++++++
 hw/mips/boston.c         | 13 ++++++++++
 hw/mips/cps.c            |  9 +++++++
 hw/mips/fuloong2e.c      |  8 ++++++-
 hw/mips/jazz.c           | 16 ++++++++++++-
 hw/mips/malta.c          | 20 +++++++++++++---
 hw/mips/mipssim.c        | 12 +++++++++-
 hw/mips/r4k.c            |  8 ++++++-
 qdev-monitor.c           |  8 ++++---
 target/mips/cp0_helper.c | 25 ++++++++++++++++++++
 target/mips/cp0_timer.c  | 51 ++++++++++------------------------------
 target/mips/cpu.c        | 42 ++++++++++++++++++++++++++++++++-
 util/cutils.c            | 14 +++++++++++
 19 files changed, 245 insertions(+), 50 deletions(-)

-- 
2.26.2



^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH v3 01/20] util/cutils: Introduce freq_to_str() to display Hertz units
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 02/20] qdev-monitor: Display frequencies scaled to SI unit Philippe Mathieu-Daudé
                   ` (18 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alistair Francis, Alex Bennée,
	Aurelien Jarno, Richard Henderson

Introduce freq_to_str() to convert frequency values in human
friendly units using the SI units for Hertz.

Suggested-by: Luc Michel <luc@lmichel.fr>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Luc Michel <luc@lmichel.fr>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/qemu/cutils.h | 12 ++++++++++++
 util/cutils.c         | 14 ++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 3a86ec0321e..4bbf4834ea5 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -158,6 +158,18 @@ int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
 
 char *size_to_str(uint64_t val);
 
+/**
+ * freq_to_str:
+ * @freq_hz: frequency to stringify
+ *
+ * Return human readable string for frequency @freq_hz.
+ * Use SI units like KHz, MHz, and so forth.
+ *
+ * The caller is responsible for releasing the value returned
+ * with g_free() after use.
+ */
+char *freq_to_str(uint64_t freq_hz);
+
 /* used to print char* safely */
 #define STR_OR_NULL(str) ((str) ? (str) : "null")
 
diff --git a/util/cutils.c b/util/cutils.c
index 8da34e04b0b..be4e43a9eff 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -885,6 +885,20 @@ char *size_to_str(uint64_t val)
     return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]);
 }
 
+char *freq_to_str(uint64_t freq_hz)
+{
+    static const char *const suffixes[] = { "", "K", "M", "G", "T", "P", "E" };
+    double freq = freq_hz;
+    size_t idx = 0;
+
+    while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
+        freq /= 1000.0;
+        idx++;
+    }
+
+    return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]);
+}
+
 int qemu_pstrcmp0(const char **str1, const char **str2)
 {
     return g_strcmp0(*str1, *str2);
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 02/20] qdev-monitor: Display frequencies scaled to SI unit
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 01/20] util/cutils: Introduce freq_to_str() to display Hertz units Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 03/20] hw/qdev-clock: Display error hint when clock is missing from device Philippe Mathieu-Daudé
                   ` (17 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alistair Francis, Alex Bennée,
	Aurelien Jarno, Richard Henderson

Since commit 9f2ff99c7f2 ("qdev-monitor: print the device's clock
with info qtree") we can display the clock frequencies in the
monitor. Use the recently introduced freq_to_str() to display
the frequencies using the closest SI unit (human friendlier).

Before:

  (qemu) info qtree
  [...]
  dev: xilinx,zynq_slcr, id ""
    clock-in "ps_clk" freq_hz=3.333333e+07
    mmio 00000000f8000000/0000000000001000

After:

  dev: xilinx,zynq_slcr, id ""
    clock-in "ps_clk" freq_hz=33.3 MHz
    mmio 00000000f8000000/0000000000001000

Reviewed-by: Luc Michel <luc@lmichel.fr>
Reviewed-by: Damien Hedde <damien.hedde@greensocs.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 qdev-monitor.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/qdev-monitor.c b/qdev-monitor.c
index e9b7228480d..a0301cfca81 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -747,11 +747,13 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
         }
     }
     QLIST_FOREACH(ncl, &dev->clocks, node) {
-        qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n",
+        g_autofree char *freq = NULL;
+
+        freq = freq_to_str(clock_get_hz(ncl->clock));
+        qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
                     ncl->output ? "out" : "in",
                     ncl->alias ? " (alias)" : "",
-                    ncl->name,
-                    CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl->clock)));
+                    ncl->name, freq);
     }
     class = object_get_class(OBJECT(dev));
     do {
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 03/20] hw/qdev-clock: Display error hint when clock is missing from device
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 01/20] util/cutils: Introduce freq_to_str() to display Hertz units Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 02/20] qdev-monitor: Display frequencies scaled to SI unit Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 04/20] hw/core/clock: add the clock_new helper function Philippe Mathieu-Daudé
                   ` (16 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Edgar E . Iglesias,
	Luc Michel, Eduardo Habkost, Paul Burton,
	Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Instead of directly aborting, display a hint to help the developer
figure out the problem (likely trying to connect a clock to a device
pre-dating the Clock API, thus not expecting clocks).

Reviewed-by: Luc Michel <luc@lmichel.fr>
Reviewed-by: Damien Hedde <damien.hedde@greensocs.com>
Reviewed-by: Edgar E. Iglesias <edgar.iglesias@xilinx.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/core/qdev-clock.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/hw/core/qdev-clock.c b/hw/core/qdev-clock.c
index 47ecb5b4fae..6a9a340d0fb 100644
--- a/hw/core/qdev-clock.c
+++ b/hw/core/qdev-clock.c
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "hw/qdev-clock.h"
 #include "hw/qdev-core.h"
 #include "qapi/error.h"
@@ -153,6 +154,11 @@ Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("Can not find clock-in '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(!ncl->output);
 
     return ncl->clock;
@@ -165,6 +171,11 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("Can not find clock-out '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(ncl->output);
 
     return ncl->clock;
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 04/20] hw/core/clock: add the clock_new helper function
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 03/20] hw/qdev-clock: Display error hint when clock is missing from device Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 05/20] target/mips: Move cpu_mips_get_random() with CP0 helpers Philippe Mathieu-Daudé
                   ` (15 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

From: Luc Michel <luc@lmichel.fr>

This function creates a clock and parents it to another object with a given
name. It calls clock_setup_canonical_path before returning the new
clock.

This function is useful to create clocks in devices when one doesn't
want to expose it at the qdev level (as an input or an output).

Suggested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Luc Michel <luc@lmichel.fr>
Message-Id: <20201010135759.437903-4-luc@lmichel.fr>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/hw/clock.h | 13 +++++++++++++
 hw/core/clock.c    | 15 +++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/hw/clock.h b/include/hw/clock.h
index d357594df99..cbc5e6ced1e 100644
--- a/include/hw/clock.h
+++ b/include/hw/clock.h
@@ -90,6 +90,19 @@ extern const VMStateDescription vmstate_clock;
  */
 void clock_setup_canonical_path(Clock *clk);
 
+/**
+ * clock_new:
+ * @parent: the clock parent
+ * @name: the clock object name
+ *
+ * Helper function to create a new clock and parent it to @parent. There is no
+ * need to call clock_setup_canonical_path on the returned clock as it is done
+ * by this function.
+ *
+ * @return the newly created clock
+ */
+Clock *clock_new(Object *parent, const char *name);
+
 /**
  * clock_set_callback:
  * @clk: the clock to register the callback into
diff --git a/hw/core/clock.c b/hw/core/clock.c
index 7066282f7b9..f866717a835 100644
--- a/hw/core/clock.c
+++ b/hw/core/clock.c
@@ -23,6 +23,21 @@ void clock_setup_canonical_path(Clock *clk)
     clk->canonical_path = object_get_canonical_path(OBJECT(clk));
 }
 
+Clock *clock_new(Object *parent, const char *name)
+{
+    Object *obj;
+    Clock *clk;
+
+    obj = object_new(TYPE_CLOCK);
+    object_property_add_child(parent, name, obj);
+    object_unref(obj);
+
+    clk = CLOCK(obj);
+    clock_setup_canonical_path(clk);
+
+    return clk;
+}
+
 void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque)
 {
     clk->callback = cb;
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 05/20] target/mips: Move cpu_mips_get_random() with CP0 helpers
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 04/20] hw/core/clock: add the clock_new helper function Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-12  7:35   ` Luc Michel
  2020-10-10 20:43 ` [PATCH v3 06/20] target/mips/cp0_timer: Explicit unit in variable name Philippe Mathieu-Daudé
                   ` (14 subsequent siblings)
  19 siblings, 1 reply; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Aleksandar Markovic, Hervé Poussineau,
	Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

The get_random() helper uses the CP0_Wired register, which is
unrelated to the CP0_Count register use as timer.
Commit e16fe40c872 ("Move the MIPS CPU timer in a separate file")
incorrectly moved this get_random() helper with timer specific
code. Move it back to generic CP0 helpers.

Reviewed-by: Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/internal.h   |  2 +-
 target/mips/cp0_helper.c | 25 +++++++++++++++++++++++++
 target/mips/cp0_timer.c  | 25 -------------------------
 3 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/target/mips/internal.h b/target/mips/internal.h
index 7f159a9230c..087cabaa6d4 100644
--- a/target/mips/internal.h
+++ b/target/mips/internal.h
@@ -144,6 +144,7 @@ void r4k_helper_tlbr(CPUMIPSState *env);
 void r4k_helper_tlbinv(CPUMIPSState *env);
 void r4k_helper_tlbinvf(CPUMIPSState *env);
 void r4k_invalidate_tlb(CPUMIPSState *env, int idx, int use_extra);
+uint32_t cpu_mips_get_random(CPUMIPSState *env);
 
 void mips_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
                                     vaddr addr, unsigned size,
@@ -209,7 +210,6 @@ void cpu_state_reset(CPUMIPSState *s);
 void cpu_mips_realize_env(CPUMIPSState *env);
 
 /* cp0_timer.c */
-uint32_t cpu_mips_get_random(CPUMIPSState *env);
 uint32_t cpu_mips_get_count(CPUMIPSState *env);
 void cpu_mips_store_count(CPUMIPSState *env, uint32_t value);
 void cpu_mips_store_compare(CPUMIPSState *env, uint32_t value);
diff --git a/target/mips/cp0_helper.c b/target/mips/cp0_helper.c
index de64add038b..12143ac55b9 100644
--- a/target/mips/cp0_helper.c
+++ b/target/mips/cp0_helper.c
@@ -203,6 +203,31 @@ static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
     *tcst |= asid;
 }
 
+/* XXX: do not use a global */
+uint32_t cpu_mips_get_random(CPUMIPSState *env)
+{
+    static uint32_t seed = 1;
+    static uint32_t prev_idx;
+    uint32_t idx;
+    uint32_t nb_rand_tlb = env->tlb->nb_tlb - env->CP0_Wired;
+
+    if (nb_rand_tlb == 1) {
+        return env->tlb->nb_tlb - 1;
+    }
+
+    /* Don't return same value twice, so get another value */
+    do {
+        /*
+         * Use a simple algorithm of Linear Congruential Generator
+         * from ISO/IEC 9899 standard.
+         */
+        seed = 1103515245 * seed + 12345;
+        idx = (seed >> 16) % nb_rand_tlb + env->CP0_Wired;
+    } while (idx == prev_idx);
+    prev_idx = idx;
+    return idx;
+}
+
 /* CP0 helpers */
 target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
 {
diff --git a/target/mips/cp0_timer.c b/target/mips/cp0_timer.c
index bd7efb152dd..9c38e9da1c8 100644
--- a/target/mips/cp0_timer.c
+++ b/target/mips/cp0_timer.c
@@ -29,31 +29,6 @@
 
 #define TIMER_PERIOD 10 /* 10 ns period for 100 Mhz frequency */
 
-/* XXX: do not use a global */
-uint32_t cpu_mips_get_random(CPUMIPSState *env)
-{
-    static uint32_t seed = 1;
-    static uint32_t prev_idx = 0;
-    uint32_t idx;
-    uint32_t nb_rand_tlb = env->tlb->nb_tlb - env->CP0_Wired;
-
-    if (nb_rand_tlb == 1) {
-        return env->tlb->nb_tlb - 1;
-    }
-
-    /* Don't return same value twice, so get another value */
-    do {
-        /*
-         * Use a simple algorithm of Linear Congruential Generator
-         * from ISO/IEC 9899 standard.
-         */
-        seed = 1103515245 * seed + 12345;
-        idx = (seed >> 16) % nb_rand_tlb + env->CP0_Wired;
-    } while (idx == prev_idx);
-    prev_idx = idx;
-    return idx;
-}
-
 /* MIPS R4K timer */
 static void cpu_mips_timer_update(CPUMIPSState *env)
 {
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 06/20] target/mips/cp0_timer: Explicit unit in variable name
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 05/20] target/mips: Move cpu_mips_get_random() with CP0 helpers Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 07/20] target/mips/cp0_timer: Document TIMER_PERIOD origin Philippe Mathieu-Daudé
                   ` (13 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Aleksandar Markovic, Hervé Poussineau,
	Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Name variables holding nanoseconds with the '_ns' suffix.

Reviewed-by: Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/cp0_timer.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/target/mips/cp0_timer.c b/target/mips/cp0_timer.c
index 9c38e9da1c8..5194c967ae3 100644
--- a/target/mips/cp0_timer.c
+++ b/target/mips/cp0_timer.c
@@ -32,13 +32,14 @@
 /* MIPS R4K timer */
 static void cpu_mips_timer_update(CPUMIPSState *env)
 {
-    uint64_t now, next;
+    uint64_t now_ns, next_ns;
     uint32_t wait;
 
-    now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    wait = env->CP0_Compare - env->CP0_Count - (uint32_t)(now / TIMER_PERIOD);
-    next = now + (uint64_t)wait * TIMER_PERIOD;
-    timer_mod(env->timer, next);
+    now_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+    wait = env->CP0_Compare - env->CP0_Count -
+           (uint32_t)(now_ns / TIMER_PERIOD);
+    next_ns = now_ns + (uint64_t)wait * TIMER_PERIOD;
+    timer_mod(env->timer, next_ns);
 }
 
 /* Expire the timer.  */
@@ -56,16 +57,16 @@ uint32_t cpu_mips_get_count(CPUMIPSState *env)
     if (env->CP0_Cause & (1 << CP0Ca_DC)) {
         return env->CP0_Count;
     } else {
-        uint64_t now;
+        uint64_t now_ns;
 
-        now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        now_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
         if (timer_pending(env->timer)
-            && timer_expired(env->timer, now)) {
+            && timer_expired(env->timer, now_ns)) {
             /* The timer has already expired.  */
             cpu_mips_timer_expire(env);
         }
 
-        return env->CP0_Count + (uint32_t)(now / TIMER_PERIOD);
+        return env->CP0_Count + (uint32_t)(now_ns / TIMER_PERIOD);
     }
 }
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 07/20] target/mips/cp0_timer: Document TIMER_PERIOD origin
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 06/20] target/mips/cp0_timer: Explicit unit in variable name Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 08/20] target/mips: Move cp0_count_ns to CPUMIPSState Philippe Mathieu-Daudé
                   ` (12 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

TIMER_PERIOD value of '10 ns' can be explained looking at
commit 6af0bf9c7c3doc, where the CPU frequency is 200 MHz
and CP0 default count rate is half the frequency of the
CPU. Document that.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/cp0_timer.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/target/mips/cp0_timer.c b/target/mips/cp0_timer.c
index 5194c967ae3..6fec5fe0ff7 100644
--- a/target/mips/cp0_timer.c
+++ b/target/mips/cp0_timer.c
@@ -27,7 +27,17 @@
 #include "sysemu/kvm.h"
 #include "internal.h"
 
-#define TIMER_PERIOD 10 /* 10 ns period for 100 Mhz frequency */
+/*
+ * Since commit 6af0bf9c7c3 this model assumes a CPU clocked at 200MHz
+ * and a CP0 timer running at half the clock of the CPU (cp0_count_rate = 2).
+ *
+ * TIMER_FREQ_HZ = CPU_FREQ_HZ / CP0_COUNT_RATE = 200 MHz / 2 = 100 MHz
+ *
+ * TIMER_PERIOD_NS = 1 / TIMER_FREQ_HZ = 10 ns
+ */
+#define CPU_FREQ_HZ_DEFAULT     200000000
+#define CP0_COUNT_RATE_DEFAULT  2
+#define TIMER_PERIOD            10  /* 1 / (CPU_FREQ_HZ / CP0_COUNT_RATE) */
 
 /* MIPS R4K timer */
 static void cpu_mips_timer_update(CPUMIPSState *env)
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 08/20] target/mips: Move cp0_count_ns to CPUMIPSState
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 07/20] target/mips/cp0_timer: Document TIMER_PERIOD origin Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-12  5:42   ` Jiaxun Yang
  2020-10-10 20:43 ` [PATCH v3 09/20] target/mips/cpu: Calculate the CP0 timer period using the CPU frequency Philippe Mathieu-Daudé
                   ` (11 subsequent siblings)
  19 siblings, 1 reply; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Currently the CP0 timer period is fixed at 10 ns, corresponding
to a fixed CPU frequency of 200 MHz (using half the speed of the
CPU).

In few commits we will be able to use a different CPU frequency.
In preparation, move the cp0_count_ns variable to CPUMIPSState
so we can modify it.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/cpu.h       |  1 +
 target/mips/cp0_timer.c | 23 ++++++-----------------
 target/mips/cpu.c       | 21 +++++++++++++++++++++
 3 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/target/mips/cpu.h b/target/mips/cpu.h
index 7cf7f5239f7..085a88e9550 100644
--- a/target/mips/cpu.h
+++ b/target/mips/cpu.h
@@ -1145,6 +1145,7 @@ struct CPUMIPSState {
     struct MIPSITUState *itu;
     MemoryRegion *itc_tag; /* ITC Configuration Tags */
     target_ulong exception_base; /* ExceptionBase input to the core */
+    uint64_t cp0_count_ns; /* CP0_Count clock period (in nanoseconds) */
 };
 
 /**
diff --git a/target/mips/cp0_timer.c b/target/mips/cp0_timer.c
index 6fec5fe0ff7..5ec0d6249e9 100644
--- a/target/mips/cp0_timer.c
+++ b/target/mips/cp0_timer.c
@@ -27,18 +27,6 @@
 #include "sysemu/kvm.h"
 #include "internal.h"
 
-/*
- * Since commit 6af0bf9c7c3 this model assumes a CPU clocked at 200MHz
- * and a CP0 timer running at half the clock of the CPU (cp0_count_rate = 2).
- *
- * TIMER_FREQ_HZ = CPU_FREQ_HZ / CP0_COUNT_RATE = 200 MHz / 2 = 100 MHz
- *
- * TIMER_PERIOD_NS = 1 / TIMER_FREQ_HZ = 10 ns
- */
-#define CPU_FREQ_HZ_DEFAULT     200000000
-#define CP0_COUNT_RATE_DEFAULT  2
-#define TIMER_PERIOD            10  /* 1 / (CPU_FREQ_HZ / CP0_COUNT_RATE) */
-
 /* MIPS R4K timer */
 static void cpu_mips_timer_update(CPUMIPSState *env)
 {
@@ -47,8 +35,8 @@ static void cpu_mips_timer_update(CPUMIPSState *env)
 
     now_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
     wait = env->CP0_Compare - env->CP0_Count -
-           (uint32_t)(now_ns / TIMER_PERIOD);
-    next_ns = now_ns + (uint64_t)wait * TIMER_PERIOD;
+           (uint32_t)(now_ns / env->cp0_count_ns);
+    next_ns = now_ns + (uint64_t)wait * env->cp0_count_ns;
     timer_mod(env->timer, next_ns);
 }
 
@@ -76,7 +64,7 @@ uint32_t cpu_mips_get_count(CPUMIPSState *env)
             cpu_mips_timer_expire(env);
         }
 
-        return env->CP0_Count + (uint32_t)(now_ns / TIMER_PERIOD);
+        return env->CP0_Count + (uint32_t)(now_ns / env->cp0_count_ns);
     }
 }
 
@@ -92,7 +80,8 @@ void cpu_mips_store_count(CPUMIPSState *env, uint32_t count)
     } else {
         /* Store new count register */
         env->CP0_Count = count -
-               (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / TIMER_PERIOD);
+               (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) /
+                          env->cp0_count_ns);
         /* Update timer timer */
         cpu_mips_timer_update(env);
     }
@@ -119,7 +108,7 @@ void cpu_mips_stop_count(CPUMIPSState *env)
 {
     /* Store the current value */
     env->CP0_Count += (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) /
-                                 TIMER_PERIOD);
+                                 env->cp0_count_ns);
 }
 
 static void mips_timer_cb(void *opaque)
diff --git a/target/mips/cpu.c b/target/mips/cpu.c
index e86cd065483..84b727fefa8 100644
--- a/target/mips/cpu.c
+++ b/target/mips/cpu.c
@@ -134,6 +134,25 @@ static void mips_cpu_disas_set_info(CPUState *s, disassemble_info *info)
     }
 }
 
+/*
+ * Since commit 6af0bf9c7c3 this model assumes a CPU clocked at 200MHz
+ * and a CP0 timer running at half the clock of the CPU (cp0_count_rate = 2).
+ *
+ * TIMER_FREQ_HZ = CPU_FREQ_HZ / CP0_COUNT_RATE = 200 MHz / 2 = 100 MHz
+ *
+ * TIMER_PERIOD_NS = 1 / TIMER_FREQ_HZ = 10 ns
+ */
+#define CPU_FREQ_HZ_DEFAULT     200000000
+#define CP0_COUNT_RATE_DEFAULT  2
+#define TIMER_PERIOD_DEFAULT    10  /* 1 / (CPU_FREQ_HZ / CP0_COUNT_RATE) */
+
+static void mips_cp0_period_set(MIPSCPU *cpu)
+{
+    CPUMIPSState *env = &cpu->env;
+
+    env->cp0_count_ns = TIMER_PERIOD_DEFAULT;
+}
+
 static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
@@ -141,6 +160,8 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(dev);
     Error *local_err = NULL;
 
+    mips_cp0_period_set(cpu);
+
     cpu_exec_realizefn(cs, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 09/20] target/mips/cpu: Calculate the CP0 timer period using the CPU frequency
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (7 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 08/20] target/mips: Move cp0_count_ns to CPUMIPSState Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-12  5:48   ` Jiaxun Yang
  2020-10-10 20:43 ` [PATCH v3 10/20] target/mips/cpu: Make cp0_count_rate a property Philippe Mathieu-Daudé
                   ` (10 subsequent siblings)
  19 siblings, 1 reply; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

The CP0 timer period is a function of the CPU frequency.
Start using the default values, which will be replaced by
properties in the next commits.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/cpu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/target/mips/cpu.c b/target/mips/cpu.c
index 84b727fefa8..46188139b7b 100644
--- a/target/mips/cpu.c
+++ b/target/mips/cpu.c
@@ -144,13 +144,13 @@ static void mips_cpu_disas_set_info(CPUState *s, disassemble_info *info)
  */
 #define CPU_FREQ_HZ_DEFAULT     200000000
 #define CP0_COUNT_RATE_DEFAULT  2
-#define TIMER_PERIOD_DEFAULT    10  /* 1 / (CPU_FREQ_HZ / CP0_COUNT_RATE) */
 
 static void mips_cp0_period_set(MIPSCPU *cpu)
 {
     CPUMIPSState *env = &cpu->env;
 
-    env->cp0_count_ns = TIMER_PERIOD_DEFAULT;
+    env->cp0_count_ns = muldiv64(NANOSECONDS_PER_SECOND, CP0_COUNT_RATE_DEFAULT,
+                                 CPU_FREQ_HZ_DEFAULT);
 }
 
 static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 10/20] target/mips/cpu: Make cp0_count_rate a property
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (8 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 09/20] target/mips/cpu: Calculate the CP0 timer period using the CPU frequency Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 11/20] target/mips/cpu: Allow the CPU to use dynamic frequencies Philippe Mathieu-Daudé
                   ` (9 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Since not all CPU implementations use a cores use a CP0 timer
at half the frequency of the CPU, make this variable a property.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/cpu.h |  9 +++++++++
 target/mips/cpu.c | 19 +++++++++++--------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/target/mips/cpu.h b/target/mips/cpu.h
index 085a88e9550..baeceb892ef 100644
--- a/target/mips/cpu.h
+++ b/target/mips/cpu.h
@@ -1151,6 +1151,7 @@ struct CPUMIPSState {
 /**
  * MIPSCPU:
  * @env: #CPUMIPSState
+ * @cp0_count_rate: rate at which the coprocessor 0 counter increments
  *
  * A MIPS CPU.
  */
@@ -1161,6 +1162,14 @@ struct MIPSCPU {
 
     CPUNegativeOffsetState neg;
     CPUMIPSState env;
+    /*
+     * The Count register acts as a timer, incrementing at a constant rate,
+     * whether or not an instruction is executed, retired, or any forward
+     * progress is made through the pipeline. The rate at which the counter
+     * increments is implementation dependent, and is a function of the
+     * pipeline clock of the processor, not the issue width of the processor.
+     */
+    unsigned cp0_count_rate;
 };
 
 
diff --git a/target/mips/cpu.c b/target/mips/cpu.c
index 46188139b7b..461edfe22b7 100644
--- a/target/mips/cpu.c
+++ b/target/mips/cpu.c
@@ -26,7 +26,7 @@
 #include "qemu/module.h"
 #include "sysemu/kvm.h"
 #include "exec/exec-all.h"
-
+#include "hw/qdev-properties.h"
 
 static void mips_cpu_set_pc(CPUState *cs, vaddr value)
 {
@@ -135,12 +135,7 @@ static void mips_cpu_disas_set_info(CPUState *s, disassemble_info *info)
 }
 
 /*
- * Since commit 6af0bf9c7c3 this model assumes a CPU clocked at 200MHz
- * and a CP0 timer running at half the clock of the CPU (cp0_count_rate = 2).
- *
- * TIMER_FREQ_HZ = CPU_FREQ_HZ / CP0_COUNT_RATE = 200 MHz / 2 = 100 MHz
- *
- * TIMER_PERIOD_NS = 1 / TIMER_FREQ_HZ = 10 ns
+ * Since commit 6af0bf9c7c3 this model assumes a CPU clocked at 200MHz.
  */
 #define CPU_FREQ_HZ_DEFAULT     200000000
 #define CP0_COUNT_RATE_DEFAULT  2
@@ -149,7 +144,7 @@ static void mips_cp0_period_set(MIPSCPU *cpu)
 {
     CPUMIPSState *env = &cpu->env;
 
-    env->cp0_count_ns = muldiv64(NANOSECONDS_PER_SECOND, CP0_COUNT_RATE_DEFAULT,
+    env->cp0_count_ns = muldiv64(NANOSECONDS_PER_SECOND, cpu->cp0_count_rate,
                                  CPU_FREQ_HZ_DEFAULT);
 }
 
@@ -202,6 +197,13 @@ static ObjectClass *mips_cpu_class_by_name(const char *cpu_model)
     return oc;
 }
 
+static Property mips_cpu_properties[] = {
+    /* CP0 timer running at half the clock of the CPU */
+    DEFINE_PROP_UINT32("cp0-count-rate", MIPSCPU, cp0_count_rate,
+                       CP0_COUNT_RATE_DEFAULT),
+    DEFINE_PROP_END_OF_LIST()
+};
+
 static void mips_cpu_class_init(ObjectClass *c, void *data)
 {
     MIPSCPUClass *mcc = MIPS_CPU_CLASS(c);
@@ -211,6 +213,7 @@ static void mips_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_parent_realize(dc, mips_cpu_realizefn,
                                     &mcc->parent_realize);
     device_class_set_parent_reset(dc, mips_cpu_reset, &mcc->parent_reset);
+    device_class_set_props(dc, mips_cpu_properties);
 
     cc->class_by_name = mips_cpu_class_by_name;
     cc->has_work = mips_cpu_has_work;
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 11/20] target/mips/cpu: Allow the CPU to use dynamic frequencies
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (9 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 10/20] target/mips/cpu: Make cp0_count_rate a property Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz Philippe Mathieu-Daudé
                   ` (8 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Use the Clock API and let the CPU object have an input clock.

If no clock is connected, keep using the default frequency of
200 MHz used since the introduction of the 'r4k' machine in
commit 6af0bf9c7c3.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/cpu.h |  4 ++++
 target/mips/cpu.c | 10 ++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/target/mips/cpu.h b/target/mips/cpu.h
index baeceb892ef..062a4ba6225 100644
--- a/target/mips/cpu.h
+++ b/target/mips/cpu.h
@@ -4,6 +4,7 @@
 #include "cpu-qom.h"
 #include "exec/cpu-defs.h"
 #include "fpu/softfloat-types.h"
+#include "hw/clock.h"
 #include "mips-defs.h"
 
 #define TCG_GUEST_DEFAULT_MO (0)
@@ -1151,6 +1152,8 @@ struct CPUMIPSState {
 /**
  * MIPSCPU:
  * @env: #CPUMIPSState
+ * @clock: this CPU input clock (may be connected
+ *         to an output clock from another device).
  * @cp0_count_rate: rate at which the coprocessor 0 counter increments
  *
  * A MIPS CPU.
@@ -1160,6 +1163,7 @@ struct MIPSCPU {
     CPUState parent_obj;
     /*< public >*/
 
+    Clock *clock;
     CPUNegativeOffsetState neg;
     CPUMIPSState env;
     /*
diff --git a/target/mips/cpu.c b/target/mips/cpu.c
index 461edfe22b7..3deb0245e7c 100644
--- a/target/mips/cpu.c
+++ b/target/mips/cpu.c
@@ -27,6 +27,7 @@
 #include "sysemu/kvm.h"
 #include "exec/exec-all.h"
 #include "hw/qdev-properties.h"
+#include "hw/qdev-clock.h"
 
 static void mips_cpu_set_pc(CPUState *cs, vaddr value)
 {
@@ -144,8 +145,8 @@ static void mips_cp0_period_set(MIPSCPU *cpu)
 {
     CPUMIPSState *env = &cpu->env;
 
-    env->cp0_count_ns = muldiv64(NANOSECONDS_PER_SECOND, cpu->cp0_count_rate,
-                                 CPU_FREQ_HZ_DEFAULT);
+    env->cp0_count_ns = cpu->cp0_count_rate
+                        * clock_get_ns(MIPS_CPU(cpu)->clock);
 }
 
 static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
@@ -155,6 +156,10 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(dev);
     Error *local_err = NULL;
 
+    if (!clock_get(cpu->clock)) {
+        /* Initialize the frequency in case the clock remains unconnected. */
+        clock_set_hz(cpu->clock, CPU_FREQ_HZ_DEFAULT);
+    }
     mips_cp0_period_set(cpu);
 
     cpu_exec_realizefn(cs, &local_err);
@@ -178,6 +183,7 @@ static void mips_cpu_initfn(Object *obj)
     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(obj);
 
     cpu_set_cpustate_pointers(cpu);
+    cpu->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, cpu);
     env->cpu_model = mcc->cpu_def;
 }
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (10 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 11/20] target/mips/cpu: Allow the CPU to use dynamic frequencies Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-11  3:52   ` chen huacai
  2020-10-10 20:43 ` [PATCH v3 13/20] hw/mips/fuloong2e: Set CPU frequency to 533 MHz Philippe Mathieu-Daudé
                   ` (7 subsequent siblings)
  19 siblings, 1 reply; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Since its introduction in commit 6af0bf9c7c3,
the 'r4k' machine runs at 200 MHz.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/mips/r4k.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/hw/mips/r4k.c b/hw/mips/r4k.c
index 3487013a4a1..e64687b505a 100644
--- a/hw/mips/r4k.c
+++ b/hw/mips/r4k.c
@@ -37,6 +37,7 @@
 #include "sysemu/reset.h"
 #include "sysemu/runstate.h"
 #include "qemu/error-report.h"
+#include "hw/qdev-clock.h"
 
 #define MAX_IDE_BUS 2
 
@@ -184,6 +185,7 @@ void mips_r4k_init(MachineState *machine)
     int bios_size;
     MIPSCPU *cpu;
     CPUMIPSState *env;
+    Clock *cpuclk;
     ResetData *reset_info;
     int i;
     qemu_irq *i8259;
@@ -193,7 +195,11 @@ void mips_r4k_init(MachineState *machine)
     int be;
 
     /* init CPUs */
-    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
+    cpu = MIPS_CPU(object_new(machine->cpu_type));
+    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
+    clock_set_hz(cpuclk, 200000000); /* 200 MHz */
+    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
+    qdev_realize(DEVICE(cpu), NULL, &error_abort);
     env = &cpu->env;
 
     reset_info = g_malloc0(sizeof(ResetData));
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 13/20] hw/mips/fuloong2e: Set CPU frequency to 533 MHz
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (11 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-12  5:54   ` Jiaxun Yang
  2020-10-10 20:43 ` [PATCH v3 14/20] hw/mips/mipssim: Correct CPU frequency Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  19 siblings, 1 reply; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

The CPU frequency is normally provided by the firmware in the
"cpuclock" environment variable. The 2E board can handles up
to 660MHz, but be conservative and take the same value used
by the Linux kernel: 533 MHz.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/mips/fuloong2e.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/hw/mips/fuloong2e.c b/hw/mips/fuloong2e.c
index b000ed1d7f7..b25c3df19d0 100644
--- a/hw/mips/fuloong2e.c
+++ b/hw/mips/fuloong2e.c
@@ -28,6 +28,7 @@
 #include "hw/isa/superio.h"
 #include "net/net.h"
 #include "hw/boards.h"
+#include "hw/qdev-clock.h"
 #include "hw/i2c/smbus_eeprom.h"
 #include "hw/block/flash.h"
 #include "hw/mips/mips.h"
@@ -300,10 +301,15 @@ static void mips_fuloong2e_init(MachineState *machine)
     I2CBus *smbus;
     MIPSCPU *cpu;
     CPUMIPSState *env;
+    Clock *cpuclk;
     DeviceState *dev;
 
     /* init CPUs */
-    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
+    cpu = MIPS_CPU(object_new(machine->cpu_type));
+    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
+    clock_set_hz(cpuclk, 533080000); /* ~533 MHz */
+    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
+    qdev_realize(DEVICE(cpu), NULL, &error_abort);
     env = &cpu->env;
 
     qemu_register_reset(main_cpu_reset, cpu);
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 14/20] hw/mips/mipssim: Correct CPU frequency
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (12 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 13/20] hw/mips/fuloong2e: Set CPU frequency to 533 MHz Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 15/20] hw/mips/jazz: Correct CPU frequencies Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

The MIPSsim machine CPU frequency is too fast running at 200 MHz,
while it should be 12 MHz for the 24K and 6 MHz for the 5K core.

Ref: Linux commit c78cbf49c4ed
("Support for MIPSsim, the cycle accurate MIPS simulator.")

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/mips/mipssim.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/hw/mips/mipssim.c b/hw/mips/mipssim.c
index 5d4ad74828d..b4b3c2851da 100644
--- a/hw/mips/mipssim.c
+++ b/hw/mips/mipssim.c
@@ -40,6 +40,7 @@
 #include "hw/loader.h"
 #include "elf.h"
 #include "hw/sysbus.h"
+#include "hw/qdev-clock.h"
 #include "hw/qdev-properties.h"
 #include "exec/address-spaces.h"
 #include "qemu/error-report.h"
@@ -152,11 +153,20 @@ mips_mipssim_init(MachineState *machine)
     MemoryRegion *bios = g_new(MemoryRegion, 1);
     MIPSCPU *cpu;
     CPUMIPSState *env;
+    Clock *cpuclk;
     ResetData *reset_info;
     int bios_size;
 
     /* Init CPUs. */
-    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
+    cpu = MIPS_CPU(object_new(machine->cpu_type));
+    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
+#ifdef TARGET_MIPS64
+    clock_set_hz(cpuclk, 6000000); /* 6 MHz */
+#else
+    clock_set_hz(cpuclk, 12000000); /* 12 MHz */
+#endif
+    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
+    qdev_realize(DEVICE(cpu), NULL, &error_abort);
     env = &cpu->env;
 
     reset_info = g_malloc0(sizeof(ResetData));
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 15/20] hw/mips/jazz: Correct CPU frequencies
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (13 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 14/20] hw/mips/mipssim: Correct CPU frequency Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 16/20] hw/mips/cps: Expose input clock and connect it to CPU cores Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

The Magnum 4000PC CPU runs at 100 MHz, and the Acer PICA-61
CPU at ~134 MHz.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/mips/jazz.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/hw/mips/jazz.c b/hw/mips/jazz.c
index 47723093b63..e56e36cf3f7 100644
--- a/hw/mips/jazz.c
+++ b/hw/mips/jazz.c
@@ -45,6 +45,7 @@
 #include "hw/audio/pcspk.h"
 #include "hw/input/i8042.h"
 #include "hw/sysbus.h"
+#include "hw/qdev-clock.h"
 #include "exec/address-spaces.h"
 #include "sysemu/qtest.h"
 #include "sysemu/reset.h"
@@ -145,6 +146,7 @@ static void mips_jazz_init(MachineState *machine,
     MIPSCPU *cpu;
     CPUClass *cc;
     CPUMIPSState *env;
+    Clock *cpuclk;
     qemu_irq *i8259;
     rc4030_dma *dmas;
     IOMMUMemoryRegion *rc4030_dma_mr;
@@ -163,6 +165,13 @@ static void mips_jazz_init(MachineState *machine,
     MemoryRegion *bios2 = g_new(MemoryRegion, 1);
     SysBusESPState *sysbus_esp;
     ESPState *esp;
+    static const struct {
+        unsigned freq_hz;
+        unsigned pll_mult;
+    } ext_clk[] = {
+        [JAZZ_MAGNUM] = {50000000, 2},
+        [JAZZ_PICA61] = {33333333, 4},
+    };
 
     if (machine->ram_size > 256 * MiB) {
         error_report("RAM size more than 256Mb is not supported");
@@ -170,7 +179,12 @@ static void mips_jazz_init(MachineState *machine,
     }
 
     /* init CPUs */
-    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
+    cpu = MIPS_CPU(object_new(machine->cpu_type));
+    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
+    clock_set_hz(cpuclk,
+                 ext_clk[jazz_model].freq_hz * ext_clk[jazz_model].pll_mult);
+    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
+    qdev_realize(DEVICE(cpu), NULL, &error_abort);
     env = &cpu->env;
     qemu_register_reset(main_cpu_reset, cpu);
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 16/20] hw/mips/cps: Expose input clock and connect it to CPU cores
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (14 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 15/20] hw/mips/jazz: Correct CPU frequencies Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 17/20] hw/mips/boston: Set CPU frequency to 1 GHz Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Expose a qdev input clock named 'clk-in', and connect it to each
core to forward-propagate the clock.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/hw/mips/cps.h | 2 ++
 hw/mips/cps.c         | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/include/hw/mips/cps.h b/include/hw/mips/cps.h
index 9e35a881366..859a8d4a674 100644
--- a/include/hw/mips/cps.h
+++ b/include/hw/mips/cps.h
@@ -21,6 +21,7 @@
 #define MIPS_CPS_H
 
 #include "hw/sysbus.h"
+#include "hw/clock.h"
 #include "hw/misc/mips_cmgcr.h"
 #include "hw/intc/mips_gic.h"
 #include "hw/misc/mips_cpc.h"
@@ -43,6 +44,7 @@ struct MIPSCPSState {
     MIPSGICState gic;
     MIPSCPCState cpc;
     MIPSITUState itu;
+    Clock *clock;
 };
 
 qemu_irq get_cps_irq(MIPSCPSState *cps, int pin_number);
diff --git a/hw/mips/cps.c b/hw/mips/cps.c
index 23c0f87e41a..915655b91e0 100644
--- a/hw/mips/cps.c
+++ b/hw/mips/cps.c
@@ -22,6 +22,7 @@
 #include "qemu/module.h"
 #include "hw/mips/cps.h"
 #include "hw/mips/mips.h"
+#include "hw/qdev-clock.h"
 #include "hw/qdev-properties.h"
 #include "hw/mips/cpudevs.h"
 #include "sysemu/kvm.h"
@@ -38,6 +39,7 @@ static void mips_cps_init(Object *obj)
     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
     MIPSCPSState *s = MIPS_CPS(obj);
 
+    s->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, NULL);
     /*
      * Cover entire address space as there do not seem to be any
      * constraints for the base address of CPC and GIC.
@@ -80,6 +82,7 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
                                       errp)) {
             return;
         }
+        qdev_connect_clock_in(DEVICE(cpu), "clk-in", s->clock);
 
         if (!qdev_realize_and_unref(DEVICE(cpu), NULL, errp)) {
             return;
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 17/20] hw/mips/boston: Set CPU frequency to 1 GHz
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (15 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 16/20] hw/mips/cps: Expose input clock and connect it to CPU cores Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 18/20] hw/mips/malta: Set CPU frequency to 320 MHz Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

The I6400 can run at 1 GHz or more. Create a 'cpuclk'
output clock and connect it to the CPU input clock.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/mips/boston.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/hw/mips/boston.c b/hw/mips/boston.c
index 1b3f69e949c..cf2296f4488 100644
--- a/hw/mips/boston.c
+++ b/hw/mips/boston.c
@@ -30,6 +30,7 @@
 #include "hw/mips/cps.h"
 #include "hw/mips/cpudevs.h"
 #include "hw/pci-host/xilinx-pcie.h"
+#include "hw/qdev-clock.h"
 #include "hw/qdev-properties.h"
 #include "qapi/error.h"
 #include "qemu/error-report.h"
@@ -54,6 +55,7 @@ struct BostonState {
     MachineState *mach;
     MIPSCPSState cps;
     SerialMM *uart;
+    Clock *cpuclk;
 
     CharBackend lcd_display;
     char lcd_content[8];
@@ -251,10 +253,19 @@ static const MemoryRegionOps boston_platreg_ops = {
     .endianness = DEVICE_NATIVE_ENDIAN,
 };
 
+static void mips_boston_instance_init(Object *obj)
+{
+    BostonState *s = BOSTON(obj);
+
+    s->cpuclk = qdev_init_clock_out(DEVICE(obj), "cpu-refclk");
+    clock_set_hz(s->cpuclk, 1000000000); /* 1 GHz */
+}
+
 static const TypeInfo boston_device = {
     .name          = TYPE_MIPS_BOSTON,
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(BostonState),
+    .instance_init = mips_boston_instance_init,
 };
 
 static void boston_register_types(void)
@@ -462,6 +473,8 @@ static void boston_mach_init(MachineState *machine)
                             &error_fatal);
     object_property_set_int(OBJECT(&s->cps), "num-vp", machine->smp.cpus,
                             &error_fatal);
+    qdev_connect_clock_in(DEVICE(&s->cps), "clk-in",
+                          qdev_get_clock_out(dev, "cpu-refclk"));
     sysbus_realize(SYS_BUS_DEVICE(&s->cps), &error_fatal);
 
     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->cps), 0, 0, 1);
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 18/20] hw/mips/malta: Set CPU frequency to 320 MHz
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (16 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 17/20] hw/mips/boston: Set CPU frequency to 1 GHz Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 19/20] hw/mips/cps: Do not allow use without input clock Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 20/20] target/mips/cpu: Display warning when CPU is used " Philippe Mathieu-Daudé
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

The CoreLV card with ID 0x420's CPU clocked at 320 MHz. Create
a 'cpuclk' output clock and connect it to the CPU input clock.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/mips/malta.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/hw/mips/malta.c b/hw/mips/malta.c
index 4019c9dc1a8..357e269e088 100644
--- a/hw/mips/malta.c
+++ b/hw/mips/malta.c
@@ -57,6 +57,7 @@
 #include "sysemu/kvm.h"
 #include "hw/semihosting/semihost.h"
 #include "hw/mips/cps.h"
+#include "hw/qdev-clock.h"
 
 #define ENVP_ADDR           0x80002000l
 #define ENVP_NB_ENTRIES     16
@@ -94,6 +95,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(MaltaState, MIPS_MALTA)
 struct MaltaState {
     SysBusDevice parent_obj;
 
+    Clock *cpuclk;
     MIPSCPSState cps;
     qemu_irq i8259[ISA_NUM_IRQS];
 };
@@ -1159,7 +1161,7 @@ static void main_cpu_reset(void *opaque)
     }
 }
 
-static void create_cpu_without_cps(MachineState *ms,
+static void create_cpu_without_cps(MachineState *ms, MaltaState *s,
                                    qemu_irq *cbus_irq, qemu_irq *i8259_irq)
 {
     CPUMIPSState *env;
@@ -1167,7 +1169,9 @@ static void create_cpu_without_cps(MachineState *ms,
     int i;
 
     for (i = 0; i < ms->smp.cpus; i++) {
-        cpu = MIPS_CPU(cpu_create(ms->cpu_type));
+        cpu = MIPS_CPU(object_new(ms->cpu_type));
+        qdev_connect_clock_in(DEVICE(cpu), "clk-in", s->cpuclk);
+        qdev_realize(DEVICE(cpu), NULL, &error_abort);
 
         /* Init internal devices */
         cpu_mips_irq_init_cpu(cpu);
@@ -1189,6 +1193,7 @@ static void create_cps(MachineState *ms, MaltaState *s,
                             &error_fatal);
     object_property_set_int(OBJECT(&s->cps), "num-vp", ms->smp.cpus,
                             &error_fatal);
+    qdev_connect_clock_in(DEVICE(&s->cps), "clk-in", s->cpuclk);
     sysbus_realize(SYS_BUS_DEVICE(&s->cps), &error_fatal);
 
     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->cps), 0, 0, 1);
@@ -1203,7 +1208,7 @@ static void mips_create_cpu(MachineState *ms, MaltaState *s,
     if ((ms->smp.cpus > 1) && cpu_supports_cps_smp(ms->cpu_type)) {
         create_cps(ms, s, cbus_irq, i8259_irq);
     } else {
-        create_cpu_without_cps(ms, cbus_irq, i8259_irq);
+        create_cpu_without_cps(ms, s, cbus_irq, i8259_irq);
     }
 }
 
@@ -1421,10 +1426,19 @@ void mips_malta_init(MachineState *machine)
     pci_vga_init(pci_bus);
 }
 
+static void mips_malta_instance_init(Object *obj)
+{
+    MaltaState *s = MIPS_MALTA(obj);
+
+    s->cpuclk = qdev_init_clock_out(DEVICE(obj), "cpu-refclk");
+    clock_set_hz(s->cpuclk, 320000000); /* 320 MHz */
+}
+
 static const TypeInfo mips_malta_device = {
     .name          = TYPE_MIPS_MALTA,
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(MaltaState),
+    .instance_init = mips_malta_instance_init,
 };
 
 static void mips_malta_machine_init(MachineClass *mc)
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 19/20] hw/mips/cps: Do not allow use without input clock
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (17 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 18/20] hw/mips/malta: Set CPU frequency to 320 MHz Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  2020-10-10 20:43 ` [PATCH v3 20/20] target/mips/cpu: Display warning when CPU is used " Philippe Mathieu-Daudé
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Now than all QOM users provides the input clock, do not allow
using a CPS without input clock connected.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/mips/cps.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/hw/mips/cps.c b/hw/mips/cps.c
index 915655b91e0..c624821315a 100644
--- a/hw/mips/cps.c
+++ b/hw/mips/cps.c
@@ -74,6 +74,11 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
     bool itu_present = false;
     bool saar_present = false;
 
+    if (!clock_get(s->clock)) {
+        error_setg(errp, "CPS input clock is not connected to an output clock");
+        return;
+    }
+
     for (i = 0; i < s->num_vp; i++) {
         cpu = MIPS_CPU(object_new(s->cpu_type));
 
@@ -82,6 +87,7 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
                                       errp)) {
             return;
         }
+        /* All cores use the same clock tree */
         qdev_connect_clock_in(DEVICE(cpu), "clk-in", s->clock);
 
         if (!qdev_realize_and_unref(DEVICE(cpu), NULL, errp)) {
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH v3 20/20] target/mips/cpu: Display warning when CPU is used without input clock
  2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
                   ` (18 preceding siblings ...)
  2020-10-10 20:43 ` [PATCH v3 19/20] hw/mips/cps: Do not allow use without input clock Philippe Mathieu-Daudé
@ 2020-10-10 20:43 ` Philippe Mathieu-Daudé
  19 siblings, 0 replies; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-10 20:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Philippe Mathieu-Daudé,
	Hervé Poussineau, Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

All our QOM users provides an input clock. In order to avoid
avoid future machines added without clock, display a warning.

User-mode emulation use the CP0 timer with the RDHWR instruction
(see commit cdfcad788394) so keep using the fixed 200 MHz clock
without diplaying any warning. Only display it in system-mode
emulation.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 target/mips/cpu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/target/mips/cpu.c b/target/mips/cpu.c
index 3deb0245e7c..60011068e4d 100644
--- a/target/mips/cpu.c
+++ b/target/mips/cpu.c
@@ -19,12 +19,14 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/cutils.h"
 #include "qapi/error.h"
 #include "cpu.h"
 #include "internal.h"
 #include "kvm_mips.h"
 #include "qemu/module.h"
 #include "sysemu/kvm.h"
+#include "sysemu/qtest.h"
 #include "exec/exec-all.h"
 #include "hw/qdev-properties.h"
 #include "hw/qdev-clock.h"
@@ -157,6 +159,14 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     Error *local_err = NULL;
 
     if (!clock_get(cpu->clock)) {
+#ifndef CONFIG_USER_ONLY
+        if (!qtest_enabled()) {
+            g_autofree char *cpu_freq_str = freq_to_str(CPU_FREQ_HZ_DEFAULT);
+
+            warn_report("CPU input clock is not connected to any output clock, "
+                        "using default frequency of %s.", cpu_freq_str);
+        }
+#endif
         /* Initialize the frequency in case the clock remains unconnected. */
         clock_set_hz(cpu->clock, CPU_FREQ_HZ_DEFAULT);
     }
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 28+ messages in thread

* Re: [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz
  2020-10-10 20:43 ` [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz Philippe Mathieu-Daudé
@ 2020-10-11  3:52   ` chen huacai
  2020-10-12  8:34     ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 28+ messages in thread
From: chen huacai @ 2020-10-11  3:52 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Damien Hedde, Aleksandar Rikalo, Luc Michel, Eduardo Habkost,
	Paul Burton, qemu-level, Hervé Poussineau,
	Daniel P. Berrangé,
	Paolo Bonzini, Huacai Chen, Alex Bennée, Aurelien Jarno,
	Richard Henderson

Hi, Philippe,

On Sun, Oct 11, 2020 at 4:43 AM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>
> Since its introduction in commit 6af0bf9c7c3,
> the 'r4k' machine runs at 200 MHz.
>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  hw/mips/r4k.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/hw/mips/r4k.c b/hw/mips/r4k.c
> index 3487013a4a1..e64687b505a 100644
> --- a/hw/mips/r4k.c
> +++ b/hw/mips/r4k.c
> @@ -37,6 +37,7 @@
>  #include "sysemu/reset.h"
>  #include "sysemu/runstate.h"
>  #include "qemu/error-report.h"
> +#include "hw/qdev-clock.h"
>
>  #define MAX_IDE_BUS 2
>
> @@ -184,6 +185,7 @@ void mips_r4k_init(MachineState *machine)
>      int bios_size;
>      MIPSCPU *cpu;
>      CPUMIPSState *env;
> +    Clock *cpuclk;
>      ResetData *reset_info;
>      int i;
>      qemu_irq *i8259;
> @@ -193,7 +195,11 @@ void mips_r4k_init(MachineState *machine)
>      int be;
>
>      /* init CPUs */
> -    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
> +    cpu = MIPS_CPU(object_new(machine->cpu_type));
> +    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
> +    clock_set_hz(cpuclk, 200000000); /* 200 MHz */
> +    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
> +    qdev_realize(DEVICE(cpu), NULL, &error_abort);
Can we add a new parameter to cpu_create() and set the freq in the core code?

Huacai
>      env = &cpu->env;
>
>      reset_info = g_malloc0(sizeof(ResetData));
> --
> 2.26.2
>


-- 
Huacai Chen


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v3 08/20] target/mips: Move cp0_count_ns to CPUMIPSState
  2020-10-10 20:43 ` [PATCH v3 08/20] target/mips: Move cp0_count_ns to CPUMIPSState Philippe Mathieu-Daudé
@ 2020-10-12  5:42   ` Jiaxun Yang
  0 siblings, 0 replies; 28+ messages in thread
From: Jiaxun Yang @ 2020-10-12  5:42 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Daniel P. Berrangé,
	Hervé Poussineau, Paolo Bonzini, Huacai Chen,
	Alex Bennée, Aurelien Jarno, Richard Henderson



在 2020/10/11 上午4:43, Philippe Mathieu-Daudé 写道:
> Currently the CP0 timer period is fixed at 10 ns, corresponding
> to a fixed CPU frequency of 200 MHz (using half the speed of the
> CPU).
>
> In few commits we will be able to use a different CPU frequency.
> In preparation, move the cp0_count_ns variable to CPUMIPSState
> so we can modify it.
>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
[...]
Thanks.

- Jiauxn


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v3 09/20] target/mips/cpu: Calculate the CP0 timer period using the CPU frequency
  2020-10-10 20:43 ` [PATCH v3 09/20] target/mips/cpu: Calculate the CP0 timer period using the CPU frequency Philippe Mathieu-Daudé
@ 2020-10-12  5:48   ` Jiaxun Yang
  0 siblings, 0 replies; 28+ messages in thread
From: Jiaxun Yang @ 2020-10-12  5:48 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Daniel P. Berrangé,
	Hervé Poussineau, Paolo Bonzini, Huacai Chen,
	Alex Bennée, Aurelien Jarno, Richard Henderson



在 2020/10/11 上午4:43, Philippe Mathieu-Daudé 写道:
> The CP0 timer period is a function of the CPU frequency.
> Start using the default values, which will be replaced by
> properties in the next commits.
>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
[...]
Thanks

- Jiaxun


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v3 13/20] hw/mips/fuloong2e: Set CPU frequency to 533 MHz
  2020-10-10 20:43 ` [PATCH v3 13/20] hw/mips/fuloong2e: Set CPU frequency to 533 MHz Philippe Mathieu-Daudé
@ 2020-10-12  5:54   ` Jiaxun Yang
  0 siblings, 0 replies; 28+ messages in thread
From: Jiaxun Yang @ 2020-10-12  5:54 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, Daniel P. Berrangé,
	Hervé Poussineau, Paolo Bonzini, Huacai Chen,
	Alex Bennée, Aurelien Jarno, Richard Henderson



在 2020/10/11 上午4:43, Philippe Mathieu-Daudé 写道:
> The CPU frequency is normally provided by the firmware in the
> "cpuclock" environment variable. The 2E board can handles up
> to 660MHz, but be conservative and take the same value used
> by the Linux kernel: 533 MHz.
>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Reviewed-by: Jiaxun Yang <jiaxun.yang@flygoat.com>

Thanks

- Jiaxun
> ---
>   hw/mips/fuloong2e.c | 8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/hw/mips/fuloong2e.c b/hw/mips/fuloong2e.c
> index b000ed1d7f7..b25c3df19d0 100644
> --- a/hw/mips/fuloong2e.c
> +++ b/hw/mips/fuloong2e.c
> @@ -28,6 +28,7 @@
>   #include "hw/isa/superio.h"
>   #include "net/net.h"
>   #include "hw/boards.h"
> +#include "hw/qdev-clock.h"
>   #include "hw/i2c/smbus_eeprom.h"
>   #include "hw/block/flash.h"
>   #include "hw/mips/mips.h"
> @@ -300,10 +301,15 @@ static void mips_fuloong2e_init(MachineState *machine)
>       I2CBus *smbus;
>       MIPSCPU *cpu;
>       CPUMIPSState *env;
> +    Clock *cpuclk;
>       DeviceState *dev;
>   
>       /* init CPUs */
> -    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
> +    cpu = MIPS_CPU(object_new(machine->cpu_type));
> +    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
> +    clock_set_hz(cpuclk, 533080000); /* ~533 MHz */
> +    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
> +    qdev_realize(DEVICE(cpu), NULL, &error_abort);
>       env = &cpu->env;
>   
>       qemu_register_reset(main_cpu_reset, cpu);


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v3 05/20] target/mips: Move cpu_mips_get_random() with CP0 helpers
  2020-10-10 20:43 ` [PATCH v3 05/20] target/mips: Move cpu_mips_get_random() with CP0 helpers Philippe Mathieu-Daudé
@ 2020-10-12  7:35   ` Luc Michel
  0 siblings, 0 replies; 28+ messages in thread
From: Luc Michel @ 2020-10-12  7:35 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Damien Hedde, Huacai Chen, Aleksandar Rikalo,
	Daniel P. Berrangé,
	Eduardo Habkost, Paul Burton, qemu-devel, Aleksandar Markovic,
	Hervé Poussineau, Paolo Bonzini, Huacai Chen,
	Alex Bennée, Aurelien Jarno, Richard Henderson

On 22:43 Sat 10 Oct     , Philippe Mathieu-Daudé wrote:
> The get_random() helper uses the CP0_Wired register, which is
> unrelated to the CP0_Count register use as timer.
typo: used
> Commit e16fe40c872 ("Move the MIPS CPU timer in a separate file")
> incorrectly moved this get_random() helper with timer specific
> code. Move it back to generic CP0 helpers.
> 
> Reviewed-by: Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Reviewed-by: Luc Michel <luc@lmichel.fr>

> ---
>  target/mips/internal.h   |  2 +-
>  target/mips/cp0_helper.c | 25 +++++++++++++++++++++++++
>  target/mips/cp0_timer.c  | 25 -------------------------
>  3 files changed, 26 insertions(+), 26 deletions(-)
> 
> diff --git a/target/mips/internal.h b/target/mips/internal.h
> index 7f159a9230c..087cabaa6d4 100644
> --- a/target/mips/internal.h
> +++ b/target/mips/internal.h
> @@ -144,6 +144,7 @@ void r4k_helper_tlbr(CPUMIPSState *env);
>  void r4k_helper_tlbinv(CPUMIPSState *env);
>  void r4k_helper_tlbinvf(CPUMIPSState *env);
>  void r4k_invalidate_tlb(CPUMIPSState *env, int idx, int use_extra);
> +uint32_t cpu_mips_get_random(CPUMIPSState *env);
>  
>  void mips_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
>                                      vaddr addr, unsigned size,
> @@ -209,7 +210,6 @@ void cpu_state_reset(CPUMIPSState *s);
>  void cpu_mips_realize_env(CPUMIPSState *env);
>  
>  /* cp0_timer.c */
> -uint32_t cpu_mips_get_random(CPUMIPSState *env);
>  uint32_t cpu_mips_get_count(CPUMIPSState *env);
>  void cpu_mips_store_count(CPUMIPSState *env, uint32_t value);
>  void cpu_mips_store_compare(CPUMIPSState *env, uint32_t value);
> diff --git a/target/mips/cp0_helper.c b/target/mips/cp0_helper.c
> index de64add038b..12143ac55b9 100644
> --- a/target/mips/cp0_helper.c
> +++ b/target/mips/cp0_helper.c
> @@ -203,6 +203,31 @@ static void sync_c0_entryhi(CPUMIPSState *cpu, int tc)
>      *tcst |= asid;
>  }
>  
> +/* XXX: do not use a global */
> +uint32_t cpu_mips_get_random(CPUMIPSState *env)
> +{
> +    static uint32_t seed = 1;
> +    static uint32_t prev_idx;
> +    uint32_t idx;
> +    uint32_t nb_rand_tlb = env->tlb->nb_tlb - env->CP0_Wired;
> +
> +    if (nb_rand_tlb == 1) {
> +        return env->tlb->nb_tlb - 1;
> +    }
> +
> +    /* Don't return same value twice, so get another value */
> +    do {
> +        /*
> +         * Use a simple algorithm of Linear Congruential Generator
> +         * from ISO/IEC 9899 standard.
> +         */
> +        seed = 1103515245 * seed + 12345;
> +        idx = (seed >> 16) % nb_rand_tlb + env->CP0_Wired;
> +    } while (idx == prev_idx);
> +    prev_idx = idx;
> +    return idx;
> +}
> +
>  /* CP0 helpers */
>  target_ulong helper_mfc0_mvpcontrol(CPUMIPSState *env)
>  {
> diff --git a/target/mips/cp0_timer.c b/target/mips/cp0_timer.c
> index bd7efb152dd..9c38e9da1c8 100644
> --- a/target/mips/cp0_timer.c
> +++ b/target/mips/cp0_timer.c
> @@ -29,31 +29,6 @@
>  
>  #define TIMER_PERIOD 10 /* 10 ns period for 100 Mhz frequency */
>  
> -/* XXX: do not use a global */
> -uint32_t cpu_mips_get_random(CPUMIPSState *env)
> -{
> -    static uint32_t seed = 1;
> -    static uint32_t prev_idx = 0;
> -    uint32_t idx;
> -    uint32_t nb_rand_tlb = env->tlb->nb_tlb - env->CP0_Wired;
> -
> -    if (nb_rand_tlb == 1) {
> -        return env->tlb->nb_tlb - 1;
> -    }
> -
> -    /* Don't return same value twice, so get another value */
> -    do {
> -        /*
> -         * Use a simple algorithm of Linear Congruential Generator
> -         * from ISO/IEC 9899 standard.
> -         */
> -        seed = 1103515245 * seed + 12345;
> -        idx = (seed >> 16) % nb_rand_tlb + env->CP0_Wired;
> -    } while (idx == prev_idx);
> -    prev_idx = idx;
> -    return idx;
> -}
> -
>  /* MIPS R4K timer */
>  static void cpu_mips_timer_update(CPUMIPSState *env)
>  {
> -- 
> 2.26.2
> 

-- 


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz
  2020-10-11  3:52   ` chen huacai
@ 2020-10-12  8:34     ` Philippe Mathieu-Daudé
  2020-10-21 12:38       ` Igor Mammedov
  0 siblings, 1 reply; 28+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-10-12  8:34 UTC (permalink / raw)
  To: chen huacai
  Cc: Damien Hedde, Aleksandar Rikalo, Luc Michel, Eduardo Habkost,
	Paul Burton, Igor Mammedov, qemu-level, Hervé Poussineau,
	Paolo Bonzini, Huacai Chen, Daniel P. Berrangé,
	Alex Bennée, Aurelien Jarno, Richard Henderson

Hi Huacai,

On 10/11/20 5:52 AM, chen huacai wrote:
> Hi, Philippe,
> 
> On Sun, Oct 11, 2020 at 4:43 AM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>>
>> Since its introduction in commit 6af0bf9c7c3,
>> the 'r4k' machine runs at 200 MHz.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
>> ---
>>   hw/mips/r4k.c | 8 +++++++-
>>   1 file changed, 7 insertions(+), 1 deletion(-)
>>
>> diff --git a/hw/mips/r4k.c b/hw/mips/r4k.c
>> index 3487013a4a1..e64687b505a 100644
>> --- a/hw/mips/r4k.c
>> +++ b/hw/mips/r4k.c
>> @@ -37,6 +37,7 @@
>>   #include "sysemu/reset.h"
>>   #include "sysemu/runstate.h"
>>   #include "qemu/error-report.h"
>> +#include "hw/qdev-clock.h"
>>
>>   #define MAX_IDE_BUS 2
>>
>> @@ -184,6 +185,7 @@ void mips_r4k_init(MachineState *machine)
>>       int bios_size;
>>       MIPSCPU *cpu;
>>       CPUMIPSState *env;
>> +    Clock *cpuclk;
>>       ResetData *reset_info;
>>       int i;
>>       qemu_irq *i8259;
>> @@ -193,7 +195,11 @@ void mips_r4k_init(MachineState *machine)
>>       int be;
>>
>>       /* init CPUs */
>> -    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
>> +    cpu = MIPS_CPU(object_new(machine->cpu_type));
>> +    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
>> +    clock_set_hz(cpuclk, 200000000); /* 200 MHz */
>> +    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
>> +    qdev_realize(DEVICE(cpu), NULL, &error_abort);
 >
> Can we add a new parameter to cpu_create() and set the freq in the core code?

Adding a new parameter seems a good idea.

Both maintainers of the core code are reluctant to add
a CPU clock to the core code, see:
https://www.mail-archive.com/qemu-devel@nongnu.org/msg747589.html
https://www.mail-archive.com/qemu-devel@nongnu.org/msg747612.html
Which is why I restricted that to the MIPS CPUs.

On ARM, Damien started to use clocks on the Zynq SoC (merged):
https://www.mail-archive.com/qemu-devel@nongnu.org/msg694604.html
Luc is working on adding a clock manager to the Broadcom SoC:
https://lists.gnu.org/archive/html/qemu-devel/2020-10/msg02840.html

I also started converting one UART devices:
https://www.mail-archive.com/qemu-devel@nongnu.org/msg727972.html

So IMO the core code will soon require this. Maybe we will add it
during the next development window.

Meanwhile I can add a mips_cpu_create_with_clock() in preparation
of cpu_create_with_clock().

> 
> Huacai
>>       env = &cpu->env;
>>
>>       reset_info = g_malloc0(sizeof(ResetData));
>> --
>> 2.26.2
>>
> 
> 


^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz
  2020-10-12  8:34     ` Philippe Mathieu-Daudé
@ 2020-10-21 12:38       ` Igor Mammedov
  0 siblings, 0 replies; 28+ messages in thread
From: Igor Mammedov @ 2020-10-21 12:38 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Damien Hedde, chen huacai, Aleksandar Rikalo, Luc Michel,
	Eduardo Habkost, Paul Burton, qemu-level, Hervé Poussineau,
	Paolo Bonzini, Huacai Chen, Daniel P. Berrangé,
	Alex Bennée, Aurelien Jarno, Richard Henderson

On Mon, 12 Oct 2020 10:34:48 +0200
Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:

> Hi Huacai,
> 
> On 10/11/20 5:52 AM, chen huacai wrote:
> > Hi, Philippe,
> > 
> > On Sun, Oct 11, 2020 at 4:43 AM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:  
> >>
> >> Since its introduction in commit 6af0bf9c7c3,
> >> the 'r4k' machine runs at 200 MHz.
> >>
> >> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> >> ---
> >>   hw/mips/r4k.c | 8 +++++++-
> >>   1 file changed, 7 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/hw/mips/r4k.c b/hw/mips/r4k.c
> >> index 3487013a4a1..e64687b505a 100644
> >> --- a/hw/mips/r4k.c
> >> +++ b/hw/mips/r4k.c
> >> @@ -37,6 +37,7 @@
> >>   #include "sysemu/reset.h"
> >>   #include "sysemu/runstate.h"
> >>   #include "qemu/error-report.h"
> >> +#include "hw/qdev-clock.h"
> >>
> >>   #define MAX_IDE_BUS 2
> >>
> >> @@ -184,6 +185,7 @@ void mips_r4k_init(MachineState *machine)
> >>       int bios_size;
> >>       MIPSCPU *cpu;
> >>       CPUMIPSState *env;
> >> +    Clock *cpuclk;
> >>       ResetData *reset_info;
> >>       int i;
> >>       qemu_irq *i8259;
> >> @@ -193,7 +195,11 @@ void mips_r4k_init(MachineState *machine)
> >>       int be;
> >>
> >>       /* init CPUs */
> >> -    cpu = MIPS_CPU(cpu_create(machine->cpu_type));
> >> +    cpu = MIPS_CPU(object_new(machine->cpu_type));
> >> +    cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
> >> +    clock_set_hz(cpuclk, 200000000); /* 200 MHz */
> >> +    qdev_connect_clock_in(DEVICE(cpu), "clk-in", cpuclk);
> >> +    qdev_realize(DEVICE(cpu), NULL, &error_abort);  
>  >
> > Can we add a new parameter to cpu_create() and set the freq in the core code?  
> 
> Adding a new parameter seems a good idea.
> 
> Both maintainers of the core code are reluctant to add
> a CPU clock to the core code, see:
> https://www.mail-archive.com/qemu-devel@nongnu.org/msg747589.html
> https://www.mail-archive.com/qemu-devel@nongnu.org/msg747612.html
> Which is why I restricted that to the MIPS CPUs.
> 
the reason for that is that it's used only by a handful of devices
and not widespread.

> On ARM, Damien started to use clocks on the Zynq SoC (merged):
> https://www.mail-archive.com/qemu-devel@nongnu.org/msg694604.html
> Luc is working on adding a clock manager to the Broadcom SoC:
> https://lists.gnu.org/archive/html/qemu-devel/2020-10/msg02840.html
> 
> I also started converting one UART devices:
> https://www.mail-archive.com/qemu-devel@nongnu.org/msg727972.html
> 
> So IMO the core code will soon require this. Maybe we will add it
> during the next development window.
> 
> Meanwhile I can add a mips_cpu_create_with_clock() in preparation

boards that need property settings in between object_new() and realize()
typically open code just that and/or use plug/preplug handler(callback)
to set properties. preplug handler should work with cpu_create() just fine.

If you have multiple mips boards that will new clock mechanism, 
mips_cpu_create_with_clock() wrapper would work as well (it's a bit less
complicated compared to preplug handler but not too much)

> of cpu_create_with_clock().
> 
> > 
> > Huacai  
> >>       env = &cpu->env;
> >>
> >>       reset_info = g_malloc0(sizeof(ResetData));
> >> --
> >> 2.26.2
> >>  
> > 
> >   
> 



^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2020-10-21 12:40 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-10 20:42 [PATCH v3 00/20] hw/mips: Set CPU frequency Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 01/20] util/cutils: Introduce freq_to_str() to display Hertz units Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 02/20] qdev-monitor: Display frequencies scaled to SI unit Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 03/20] hw/qdev-clock: Display error hint when clock is missing from device Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 04/20] hw/core/clock: add the clock_new helper function Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 05/20] target/mips: Move cpu_mips_get_random() with CP0 helpers Philippe Mathieu-Daudé
2020-10-12  7:35   ` Luc Michel
2020-10-10 20:43 ` [PATCH v3 06/20] target/mips/cp0_timer: Explicit unit in variable name Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 07/20] target/mips/cp0_timer: Document TIMER_PERIOD origin Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 08/20] target/mips: Move cp0_count_ns to CPUMIPSState Philippe Mathieu-Daudé
2020-10-12  5:42   ` Jiaxun Yang
2020-10-10 20:43 ` [PATCH v3 09/20] target/mips/cpu: Calculate the CP0 timer period using the CPU frequency Philippe Mathieu-Daudé
2020-10-12  5:48   ` Jiaxun Yang
2020-10-10 20:43 ` [PATCH v3 10/20] target/mips/cpu: Make cp0_count_rate a property Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 11/20] target/mips/cpu: Allow the CPU to use dynamic frequencies Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 12/20] hw/mips/r4k: Explicit CPU frequency is 200 MHz Philippe Mathieu-Daudé
2020-10-11  3:52   ` chen huacai
2020-10-12  8:34     ` Philippe Mathieu-Daudé
2020-10-21 12:38       ` Igor Mammedov
2020-10-10 20:43 ` [PATCH v3 13/20] hw/mips/fuloong2e: Set CPU frequency to 533 MHz Philippe Mathieu-Daudé
2020-10-12  5:54   ` Jiaxun Yang
2020-10-10 20:43 ` [PATCH v3 14/20] hw/mips/mipssim: Correct CPU frequency Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 15/20] hw/mips/jazz: Correct CPU frequencies Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 16/20] hw/mips/cps: Expose input clock and connect it to CPU cores Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 17/20] hw/mips/boston: Set CPU frequency to 1 GHz Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 18/20] hw/mips/malta: Set CPU frequency to 320 MHz Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 19/20] hw/mips/cps: Do not allow use without input clock Philippe Mathieu-Daudé
2020-10-10 20:43 ` [PATCH v3 20/20] target/mips/cpu: Display warning when CPU is used " Philippe Mathieu-Daudé

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.