Skip to contentSkip to conversation

add CPUFreq support for loongson2f

PATCH In review Wu Zhangjin 3 participants 9 messages Nov 11, 2009 – Nov 13, 2009 MIPS

lore
Share & tools
lore
#0 lore raw

cover add CPUFreq support for loongson2f

This patchset add CPUFreq support for loongson2f family machines, to untilize this support, the machines need an external timer to ensure the system timer is normal for the MIPS Timer's frequency is relative to the processor's frequency, without an external timer, the system timer will be "mussy".

Thanks & Regards,
Wu Zhangjin

Wu Zhangjin (3):
[loongson] lemote-2f: add cs5536 MFGPT timer support
MIPS: add basic options for CPUFreq support [loongson] 2f: add Cpufreq support

 arch/mips/Kconfig                                  |    3 +
 arch/mips/include/asm/clock.h                      |   64 ++++++
 .../asm/mach-loongson/cs5536/cs5536_mfgpt.h        |   35 +++
 arch/mips/include/asm/mach-loongson/loongson.h     |    6 +-
 arch/mips/kernel/Makefile                          |    2 +
 arch/mips/kernel/cpu-probe.c                       |    2 +
 arch/mips/kernel/cpufreq/Kconfig                   |   41 ++++
 arch/mips/kernel/cpufreq/Makefile                  |    5 +
 arch/mips/kernel/cpufreq/loongson2_clock.c         |  166 +++++++++++++++
 arch/mips/kernel/cpufreq/loongson2_cpufreq.c       |  200 ++++++++++++++++++
 arch/mips/loongson/Kconfig                         |   12 +-
 arch/mips/loongson/common/cs5536/Makefile          |    5 +
 arch/mips/loongson/common/cs5536/cs5536_mfgpt.c    |  217 ++++++++++++++++++++
 arch/mips/loongson/common/env.c                    |    3 +
 arch/mips/loongson/common/time.c                   |    3 +
 15 files changed, 761 insertions(+), 3 deletions(-)
 create mode 100644 arch/mips/include/asm/clock.h
 create mode 100644 arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h
 create mode 100644 arch/mips/kernel/cpufreq/Kconfig
 create mode 100644 arch/mips/kernel/cpufreq/Makefile
 create mode 100644 arch/mips/kernel/cpufreq/loongson2_clock.c
 create mode 100644 arch/mips/kernel/cpufreq/loongson2_cpufreq.c
 create mode 100644 arch/mips/loongson/common/cs5536/cs5536_mfgpt.c
#1 lore raw

PATCH 1/3 lemote-2f: add cs5536 MFGPT timer support

When Oprofile is used, it's better not to use the MIPS Timer, but use an external timer, in Lemote loongson2f family machines, there is a cs5536 mfgpt timer, this patch add support for it.

And also, this external timer is also needed by the next Cpufreq support, 'Cause the frequency of the MIPS Timer is half of the cpu frequency, if we use it with Cpufreq support, the sytem time will become not accuracy.

And we export the mfgpt0 counter disable/enable operations for the coming suspend support to suspend/resume the timer.

---
 .../asm/mach-loongson/cs5536/cs5536_mfgpt.h        |   35 +++
 arch/mips/loongson/Kconfig                         |   11 +-
 arch/mips/loongson/common/cs5536/Makefile          |    5 +
 arch/mips/loongson/common/cs5536/cs5536_mfgpt.c    |  217 ++++++++++++++++++++
 arch/mips/loongson/common/time.c                   |    3 +
 5 files changed, 269 insertions(+), 2 deletions(-)
 create mode 100644 arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h
 create mode 100644 arch/mips/loongson/common/cs5536/cs5536_mfgpt.c
arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h new file +35 -0
new file mode 100644
index 0000000..8d45945
@@ -0,0 +1,35 @@
+/*
+ * cs5536 mfgpt header file
+ */
+
+#ifndef _CS5536_MFGPT_H
+#define _CS5536_MFGPT_H
+
+#include <cs5536/cs5536.h>
+#include <cs5536/cs5536_pci.h>
+
+#ifdef CONFIG_CS5536_MFGPT
+extern void setup_mfgpt_timer(void);
+extern void disable_mfgpt0_counter(void);
+extern void enable_mfgpt0_counter(void);
+#else
+static inline void __maybe_unused setup_mfgpt0_timer(void)
+{
+}
+static inline void __maybe_unused disable_mfgpt0_counter(void)
+{
+}
+static inline void __maybe_unused enable_mfgpt0_counter(void)
+{
+}
+#endif
+
+#define MFGPT_TICK_RATE 14318000
+#define COMPARE  ((MFGPT_TICK_RATE + HZ/2) / HZ)
+
+#define MFGPT_BASE	mfgpt_base
+#define MFGPT0_CMP2	(MFGPT_BASE + 2)
+#define MFGPT0_CNT	(MFGPT_BASE + 4)
+#define MFGPT0_SETUP	(MFGPT_BASE + 6)
+
+#endif /*!_CS5536_MFGPT_H */
arch/mips/loongson/Kconfig +9 -2
index 029360f..648c47d 100644
@@ -34,10 +34,10 @@ config LEMOTE_MACH2F
 	select ARCH_SPARSEMEM_ENABLE
 	select BOARD_SCACHE
 	select BOOT_ELF32
-	select CEVT_R4K
+	select CEVT_R4K if !CS5536_MFGPT
 	select CPU_HAS_WB
 	select CS5536
-	select CSRC_R4K
+	select CSRC_R4K if !CS5536_MFGPT
 	select DMA_NONCOHERENT
 	select GENERIC_HARDIRQS_NO__DO_IRQ
 	select GENERIC_ISA_DMA_SUPPORT_BROKEN
@@ -62,6 +62,13 @@ endchoice
 config CS5536
 	bool
 
+config CS5536_MFGPT
+	bool "Using cs5536's MFGPT as system clock"
+	depends on CS5536
+	help
+	  This is needed when cpufreq or oprofile is enabled in Lemote Loongson2F
+	  family machines
+
 config LOONGSON_SUSPEND
 	bool
 	default y
arch/mips/loongson/common/cs5536/Makefile +5 -0
index 31657ee..510d4cd 100644
@@ -5,4 +5,9 @@
 obj-$(CONFIG_CS5536) += cs5536_pci.o cs5536_ide.o cs5536_acc.o cs5536_ohci.o \
 			cs5536_isa.o cs5536_ehci.o
 
+#
+# Enable cs5536 mfgpt Timer
+#
+obj-$(CONFIG_CS5536_MFGPT) += cs5536_mfgpt.o
+
 EXTRA_CFLAGS += -Werror
arch/mips/loongson/common/cs5536/cs5536_mfgpt.c new file +217 -0

217 lines not on this page · raw message

arch/mips/loongson/common/time.c +3 -0
index 6e08c82..c871b2e 100644
@@ -14,11 +14,14 @@
 #include <asm/time.h>
 
 #include <loongson.h>
+#include <cs5536/cs5536_mfgpt.h>
 
 void __init plat_time_init(void)
 {
 	/* setup mips r4k timer */
 	mips_hpt_frequency = cpu_clock_freq / 2;
+
+	setup_mfgpt_timer();
 }
 
 void read_persistent_clock(struct timespec *ts)
Signature · 1 line
1.6.2.1
#2 lore raw

PATCH 2/3 MIPS: add basic options for CPUFreq support

This patch adds basic options for MIPS CPUFreq support.

Since MIPS Timer's frequency is relative to the processor's frequency, So, MIPS CPUFreq support not only need the processor's CPUFreq support but also need an external timer. otherwise, we will make the system time "mussy".

---
 arch/mips/Kconfig                |    3 +++
 arch/mips/kernel/cpufreq/Kconfig |   27 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 0 deletions(-)
 create mode 100644 arch/mips/kernel/cpufreq/Kconfig
arch/mips/Kconfig +3 -0
index 2e39609..2228e5a 100644
@@ -2127,6 +2127,7 @@ config MMU
 
 config I8253
 	bool
+	select MIPS_EXTERNAL_TIMER
 
 config ZONE_DMA32
 	bool
@@ -2203,6 +2204,8 @@ source "kernel/power/Kconfig"
 
 endmenu
 
+source "arch/mips/kernel/cpufreq/Kconfig"
+
 source "net/Kconfig"
 
 source "drivers/Kconfig"
arch/mips/kernel/cpufreq/Kconfig new file +27 -0
new file mode 100644
index 0000000..3969661
@@ -0,0 +1,27 @@
+#
+# CPU Frequency scaling
+#
+
+config MIPS_EXTERNAL_TIMER
+	bool
+
+config MIPS_CPUFREQ
+	bool
+	default y
+	depends on CPU_SUPPORT_CPUFREQ && MIPS_EXTERNAL_TIMER
+
+if MIPS_CPUFREQ
+
+menu "CPU Frequency scaling"
+
+source "drivers/cpufreq/Kconfig"
+
+if CPU_FREQ
+
+comment "CPUFreq processor drivers"
+
+endif	# CPU_FREQ
+
+endmenu
+
+endif	# MIPS_CPUFREQ
Signature · 1 line
1.6.2.1
#3 lore raw

PATCH 3/3 2f: add Cpufreq support

Loongson2f add a new capability to dynamicly scale cpu frequency. And when we put it into wait mode via setting the frequency as ZERO, it will wait there until an external interrupt take place, which will help saving power for us.

And as the last patch described, to enable this support, an external timer is needed to avoid getting wrong system time when using the MIPS Timer, 'Cause the MIPS Timer's frequency is half of the cpu frequency, when the cpu frequency is changed, the MIPS Timer will be not accuracy.

---
 arch/mips/include/asm/clock.h                  |   64 ++++++++
 arch/mips/include/asm/mach-loongson/loongson.h |    6 +-
 arch/mips/kernel/Makefile                      |    2 +
 arch/mips/kernel/cpu-probe.c                   |    2 +
 arch/mips/kernel/cpufreq/Kconfig               |   14 ++
 arch/mips/kernel/cpufreq/Makefile              |    5 +
 arch/mips/kernel/cpufreq/loongson2_clock.c     |  166 ++++++++++++++++++++
 arch/mips/kernel/cpufreq/loongson2_cpufreq.c   |  200 ++++++++++++++++++++++++
 arch/mips/loongson/Kconfig                     |    1 +
 arch/mips/loongson/common/env.c                |    3 +
 10 files changed, 462 insertions(+), 1 deletions(-)
 create mode 100644 arch/mips/include/asm/clock.h
 create mode 100644 arch/mips/kernel/cpufreq/Makefile
 create mode 100644 arch/mips/kernel/cpufreq/loongson2_clock.c
 create mode 100644 arch/mips/kernel/cpufreq/loongson2_cpufreq.c
arch/mips/include/asm/clock.h new file +64 -0
new file mode 100644
index 0000000..83894aa
@@ -0,0 +1,64 @@
+#ifndef __ASM_MIPS_CLOCK_H
+#define __ASM_MIPS_CLOCK_H
+
+#include <linux/kref.h>
+#include <linux/list.h>
+#include <linux/seq_file.h>
+#include <linux/clk.h>
+
+extern void (*cpu_wait) (void);
+
+struct clk;
+
+struct clk_ops {
+	void (*init) (struct clk *clk);
+	void (*enable) (struct clk *clk);
+	void (*disable) (struct clk *clk);
+	void (*recalc) (struct clk *clk);
+	int (*set_rate) (struct clk *clk, unsigned long rate, int algo_id);
+	long (*round_rate) (struct clk *clk, unsigned long rate);
+};
+
+struct clk {
+	struct list_head node;
+	const char *name;
+	int id;
+	struct module *owner;
+
+	struct clk *parent;
+	struct clk_ops *ops;
+
+	struct kref kref;
+
+	unsigned long rate;
+	unsigned long flags;
+};
+
+#define CLK_ALWAYS_ENABLED	(1 << 0)
+#define CLK_RATE_PROPAGATES	(1 << 1)
+
+/* Should be defined by processor-specific code */
+void arch_init_clk_ops(struct clk_ops **, int type);
+
+int clk_init(void);
+
+int __clk_enable(struct clk *);
+void __clk_disable(struct clk *);
+
+void clk_recalc_rate(struct clk *);
+
+int clk_register(struct clk *);
+void clk_unregister(struct clk *);
+
+/* the exported API, in addition to clk_set_rate */
+/**
+ * clk_set_rate_ex - set the clock rate for a clock source, with additional parameter
+ * @clk: clock source
+ * @rate: desired clock rate in Hz
+ * @algo_id: algorithm id to be passed down to ops->set_rate
+ *
+ * Returns success (0) or negative errno.
+ */
+int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id);
+
+#endif				/* __ASM_MIPS_CLOCK_H */
arch/mips/include/asm/mach-loongson/loongson.h +5 -1
index 008c768..926f77c 100644
@@ -226,8 +226,12 @@ extern void mach_irq_dispatch(unsigned int pending);
 #define LOONGSON_PCIMAP_WIN(WIN, ADDR)	\
 	((((ADDR)>>26) & LOONGSON_PCIMAP_PCIMAP_LO0) << ((WIN)*6))
 
-/* Chip Config */
 #ifdef CONFIG_CPU_SUPPORT_CPUFREQ
+#include <linux/cpufreq.h>
+extern void loongson2_cpu_wait(void);
+extern struct cpufreq_frequency_table loongson2_clockmod_table[];
+
+/* Chip Config */
 #define LOONGSON_CHIPCFG0		LOONGSON_REG(LOONGSON_REGBASE + 0x80)
 #endif
 
arch/mips/kernel/Makefile +2 -0
index 8e26e9c..1d2ffc3 100644
@@ -90,6 +90,8 @@ CFLAGS_cpu-bugs64.o	= $(shell if $(CC) $(KBUILD_CFLAGS) -Wa,-mdaddi -c -o /dev/n
 
 obj-$(CONFIG_HAVE_STD_PC_SERIAL_PORT)	+= 8250-platform.o
 
+obj-$(CONFIG_MIPS_CPUFREQ)	+= cpufreq/
+
 EXTRA_CFLAGS += -Werror
 
 CPPFLAGS_vmlinux.lds		:= $(KBUILD_CFLAGS)
arch/mips/kernel/cpu-probe.c +2 -0
index 7a51866..80e202e 100644
@@ -16,6 +16,7 @@
 #include <linux/ptrace.h>
 #include <linux/smp.h>
 #include <linux/stddef.h>
+#include <linux/module.h>
 
 #include <asm/bugs.h>
 #include <asm/cpu.h>
@@ -32,6 +33,7 @@
  * the CPU very much.
  */
 void (*cpu_wait)(void);
+EXPORT_SYMBOL(cpu_wait);
 
 static void r3081_wait(void)
 {
arch/mips/kernel/cpufreq/Kconfig +14 -0
index 3969661..d3a7e93 100644
@@ -20,6 +20,20 @@ if CPU_FREQ
 
 comment "CPUFreq processor drivers"
 
+config LOONGSON2_CPUFREQ
+	tristate "Loongson2 CPUFreq Driver"
+	select CPU_FREQ_TABLE
+	depends on MIPS_CPUFREQ
+	help
+	  This option adds a CPUFreq driver for loongson processors which
+	  support software configurable cpu frequency.
+
+	  Loongson2F and it's successors support this feature.
+
+	  For details, take a look at <file:Documentation/cpu-freq/>.
+
+	  If in doubt, say N.
+
 endif	# CPU_FREQ
 
 endmenu
arch/mips/kernel/cpufreq/Makefile new file +5 -0
new file mode 100644
index 0000000..c3479a4
@@ -0,0 +1,5 @@
+#
+# Makefile for the Linux/MIPS cpufreq.
+#
+
+obj-$(CONFIG_LOONGSON2_CPUFREQ) += loongson2_cpufreq.o loongson2_clock.o
arch/mips/kernel/cpufreq/loongson2_clock.c new file +166 -0

166 lines not on this page · raw message

arch/mips/kernel/cpufreq/loongson2_cpufreq.c new file +200 -0

200 lines not on this page · raw message

arch/mips/loongson/Kconfig +1 -0
index 648c47d..9e66942 100644
@@ -65,6 +65,7 @@ config CS5536
 config CS5536_MFGPT
 	bool "Using cs5536's MFGPT as system clock"
 	depends on CS5536
+	select MIPS_EXTERNAL_TIMER
 	help
 	  This is needed when cpufreq or oprofile is enabled in Lemote Loongson2F
 	  family machines
arch/mips/loongson/common/env.c +3 -0
index b9ef503..196d947 100644
@@ -17,11 +17,14 @@
  * Free Software Foundation;  either version 2 of the  License, or (at your
  * option) any later version.
  */
+#include <linux/module.h>
+
 #include <asm/bootinfo.h>
 
 #include <loongson.h>
 
 unsigned long bus_clock, cpu_clock_freq;
+EXPORT_SYMBOL(cpu_clock_freq);
 unsigned long memsize, highmemsize;
 
 /* pmon passes arguments in 32bit pointers */
Signature · 1 line
1.6.2.1
#4 lore raw

Re: [PATCH -queue 3/3] [loongson] 2f: add Cpufreq support

Hey,

arch/mips/kernel/cpufreq/loongson2_cpufreq.c line 179

Quoted 10 lines · Wu Zhangjin
arch/mips/kernel/cpufreq/loongson2_cpufreq.c new file +10 -0
+	printk(KERN_INFO "cpufreq: Loongson-2F CPU frequency driver.\n");
+	result = cpufreq_register_driver(&loongson2_cpufreq_driver);
+
+	if (!result && !nowait) {
+		saved_cpu_wait = cpu_wait;
+		cpu_wait = loongson2_cpu_wait;
+	}
+
+	cpufreq_register_notifier(&loongson2_cpufreq_notifier_block,
+				  CPUFREQ_TRANSITION_NOTIFIER);

IMO you should register the transition notifier before register_driver(), else there's a small window where a change may be initiate which is not notified...

Best,
Dominik

#5 lore raw

Re: [PATCH -queue 3/3] [loongson] 2f: add Cpufreq support

Hi,

On Wed, 2009-11-11 at 19:21 +0100, Dominik Brodowski wrote:
[...]

Quoted 5 lines
Quoted 5 lines
arch/mips/kernel/cpufreq/loongson2_cpufreq.c new file +5 -0
+	result = cpufreq_register_driver(&loongson2_cpufreq_driver);
+
+	if (!result && !nowait) {
+		saved_cpu_wait = cpu_wait;
+		cpu_wait = loongson2_cpu_wait;
Quoted 9 lines

arch/mips/kernel/cpufreq/loongson2_cpufreq.c line 179

Quoted 4 lines
arch/mips/kernel/cpufreq/loongson2_cpufreq.c new file +4 -0
+	}
+
+	cpufreq_register_notifier(&loongson2_cpufreq_notifier_block,
+				  CPUFREQ_TRANSITION_NOTIFIER);

IMO you should register the transition notifier before register_driver(), else there's a small window where a change may be initiate which is not notified...

Will adjust them later, thanks!

Regards,
Wu Zhangjin

#6 lore raw

Re: [PATCH -queue 1/3] [loongson] lemote-2f: add cs5536 MFGPT timer support

In reply to Wu Zhangjin

Hi,

On Wed, 2009-11-11 at 15:09 +0800, Wu Zhangjin wrote:
[...]

Quoted 1 line
arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h +0 -0

[...]

arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h line 16

Quoted 7 lines
arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h +7 -0
+
+#ifdef CONFIG_CS5536_MFGPT
+extern void setup_mfgpt_timer(void);
+extern void disable_mfgpt0_counter(void);
+extern void enable_mfgpt0_counter(void);
+#else
+static inline void __maybe_unused setup_mfgpt0_timer(void)

Sorry, the above line should be:

+static inline void __maybe_unused setup_mfgpt_timer(void)

No 0 there.

Regards,
Wu Zhangjin

#7 lore raw

Re: [PATCH -queue 1/3] [loongson] lemote-2f: add cs5536 MFGPT timer support

In reply to Wu Zhangjin
Quoted 68 lines · Wu Zhangjin

When Oprofile is used, it's better not to use the MIPS Timer, but use an external timer, in Lemote loongson2f family machines, there is a cs5536 mfgpt timer, this patch add support for it.

And also, this external timer is also needed by the next Cpufreq support, 'Cause the frequency of the MIPS Timer is half of the cpu frequency, if we use it with Cpufreq support, the sytem time will become not accuracy.

And we export the mfgpt0 counter disable/enable operations for the coming suspend support to suspend/resume the timer.

---
 .../asm/mach-loongson/cs5536/cs5536_mfgpt.h        |   35 +++
 arch/mips/loongson/Kconfig                         |   11 +-
 arch/mips/loongson/common/cs5536/Makefile          |    5 +
 arch/mips/loongson/common/cs5536/cs5536_mfgpt.c    |  217 ++++++++++++++++++++
 arch/mips/loongson/common/time.c                   |    3 +
 5 files changed, 269 insertions(+), 2 deletions(-)
 create mode 100644 arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h
 create mode 100644 arch/mips/loongson/common/cs5536/cs5536_mfgpt.c
arch/mips/include/asm/mach-loongson/cs5536/cs5536_mfgpt.h new file +35 -0
new file mode 100644
index 0000000..8d45945
@@ -0,0 +1,35 @@
+/*
+ * cs5536 mfgpt header file
+ */
+
+#ifndef _CS5536_MFGPT_H
+#define _CS5536_MFGPT_H
+
+#include <cs5536/cs5536.h>
+#include <cs5536/cs5536_pci.h>
+
+#ifdef CONFIG_CS5536_MFGPT
+extern void setup_mfgpt_timer(void);
+extern void disable_mfgpt0_counter(void);
+extern void enable_mfgpt0_counter(void);
+#else
+static inline void __maybe_unused setup_mfgpt0_timer(void)
+{
+}
+static inline void __maybe_unused disable_mfgpt0_counter(void)
+{
+}
+static inline void __maybe_unused enable_mfgpt0_counter(void)
+{
+}
+#endif
+
+#define MFGPT_TICK_RATE 14318000
+#define COMPARE  ((MFGPT_TICK_RATE + HZ/2) / HZ)
+
+#define MFGPT_BASE	mfgpt_base
+#define MFGPT0_CMP2	(MFGPT_BASE + 2)
+#define MFGPT0_CNT	(MFGPT_BASE + 4)
+#define MFGPT0_SETUP	(MFGPT_BASE + 6)
+
+#endif /*!_CS5536_MFGPT_H */
arch/mips/loongson/Kconfig +0 -0
index 029360f..648c47d 100644

arch/mips/loongson/Kconfig config LEMOTE_MACH2F · line 40

Quoted 9 lines
arch/mips/loongson/Kconfig +2 -2
@@ -34,10 +34,10 @@ config LEMOTE_MACH2F
 	select ARCH_SPARSEMEM_ENABLE
 	select BOARD_SCACHE
 	select BOOT_ELF32
-	select CEVT_R4K
+	select CEVT_R4K if !CS5536_MFGPT
 	select CPU_HAS_WB
 	select CS5536
-	select CSRC_R4K
+	select CSRC_R4K if !CS5536_MFGPT

A bit inelegant to trust on a user to pick the right value for CS5536_MFGPT.

A non-fixed clock for c0_count is an obvious reason to avoid using CEVT_R4K and CSRC_R4K; we probably need something like cpu_has_fixed_c0_count_clock to make that decission at runtime.

arch/mips/loongson/Kconfig endchoice · line 70

Quoted 13 lines
arch/mips/loongson/Kconfig +6 -0
 	select DMA_NONCOHERENT
 	select GENERIC_HARDIRQS_NO__DO_IRQ
 	select GENERIC_ISA_DMA_SUPPORT_BROKEN
@@ -62,6 +62,13 @@ endchoice
 config CS5536
 	bool
 
+config CS5536_MFGPT
+	bool "Using cs5536's MFGPT as system clock"
+	depends on CS5536
+	help
+	  This is needed when cpufreq or oprofile is enabled in Lemote Loongson2F
+	  family machines

A patch to deal with interaction between oprofile and the cp0 count / compare was queued recently. So is there any remaining issue or is the comment here just outdated?

Ralf

#8 lore raw

Re: [PATCH -queue 1/3] [loongson] lemote-2f: add cs5536 MFGPT timer support

Hi,

On Thu, 2009-11-12 at 21:17 +0100, Ralf Baechle wrote:
[...]

Quoted 3 lines
Quoted 3 lines

And also, this external timer is also needed by the next Cpufreq support, 'Cause the frequency of the MIPS Timer is half of the cpu frequency, if we use it with Cpufreq support, the sytem time will become not accuracy.

[...]

Quoted 18 lines
Quoted 6 lines
arch/mips/loongson/Kconfig +0 -0
index 029360f..648c47d 100644

arch/mips/loongson/Kconfig config LEMOTE_MACH2F · line 40

Quoted 9 lines
arch/mips/loongson/Kconfig +2 -2
@@ -34,10 +34,10 @@ config LEMOTE_MACH2F
 	select ARCH_SPARSEMEM_ENABLE
 	select BOARD_SCACHE
 	select BOOT_ELF32
-	select CEVT_R4K
+	select CEVT_R4K if !CS5536_MFGPT
 	select CPU_HAS_WB
 	select CS5536
-	select CSRC_R4K
+	select CSRC_R4K if !CS5536_MFGPT

A bit inelegant to trust on a user to pick the right value for CS5536_MFGPT.

Quoted 3 lines

A non-fixed clock for c0_count is an obvious reason to avoid using CEVT_R4K and CSRC_R4K; we probably need something like cpu_has_fixed_c0_count_clock to make that decission at runtime.

Do you mean enable both of them when compiling, and at runtime select the external one when something like cpu_has_fixed_c0_count_clock is undefined and the CPUFreq Driver is loaded?

This maybe a good idea to allow users to choose their need in userspace via /sys/devices/system/clocksource/clocksource0/current_clocksource, and hide the details from the users.

But we may meet some problems:

1. At first, we must ensure the external one is used when the CPUFreq
Driver is loaded.

Just have a look at the source code in kernel/time/clocksource.c,
Seems we can finish this job via clocksource_change_rating().

we can try to increase the rating of the MFGPT Timer(external timer,
in Gdium, they use I8253), but perhaps we need to add a flag to this external timer to mask them, otherwise, we will not know which one is the external one, what about these flags in include/linux/clocksource.h:

/*
* Clock source flags bits:
*/
#define CLOCK_SOURCE_IS_CONTINUOUS 0x01
#define CLOCK_SOURCE_MUST_VERIFY 0x02

#define CLOCK_SOURCE_WATCHDOG                   0x10
#define CLOCK_SOURCE_VALID_FOR_HRES             0x20
#define CLOCK_SOURCE_UNSTABLE                   0x40

Seems no suitable flag to mask them, what about 0? we just set the
flag as 0, 'Cause the flag of MIPS Timer is set as CLOCK_SOURCE_IS_CONTINUOUS, we will be able to distinguish them by this flag. so, this problem will be fixed.

Of course, if there is no external timer found, we have to exit from
the CPUFreq Driver. and also, we need to resume the Timer to the MIPS when we unload the CPUFreq Driver.

2. And another problem is: people will choose their own
current_clocksource when they want, if they choose MIPS, we must disable the CPUFreq Driver asap, otherwise, the system time will be broken.

It will be hard to detect when will people change the current clock
source, and just checked the clocksource_select() in kernel/time/clocksource.c, seems there is a timekeeping_notify() called:

  timekeeping_notify()
     --> tick_clock_notify()
      --> set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);

perhaps we can check the check_clocks bit in the CPUFreq Driver, for
example we do it in loongson2_cpufreq_target(), but we can not protect, which cpu frequency currently we are, if we are not in the normal level(normally it is 797MHz, the mult/shift of MIPS timer is calculated with mips_hpt_frequency when initialization), we will also break the system time. So, if we really want to fix this problem, perhaps we need to touch the source code of the clocksource subsystem,

kernel/time/timekeeping.c:

/**
* timekeeping_notify - Install a new clock source
* @clock: pointer to the clock source
*
* This function is called from clocksource.c after a new, better clock
* source has been registered. The caller holds the clocksource_mutex. */
void timekeeping_notify(struct clocksource *clock)

{
        if (timekeeper.clock == clock)
                return;
        stop_machine(change_clocksource, clock, NULL);
        tick_clock_notify();
}

We can add a early-notify before changing the clocksource to tell the
relative drivers to do some necessary preparation.

void timekeeping_notify(struct clocksource *clock)

{
        if (timekeeper.clock == clock)
                return;
+	 tick_clock_early_notify(clock);
        stop_machine(change_clocksource, clock, NULL);
        tick_clock_notify();
}

we must ensure the drivers can register a notifier to that tick_clock_early_notify can called by it.

and then, in the CPUFreq driver, we can check who is the target clock, if it is MIPS, the CPUFreq driver must change it's Frequency to a normal level and then the whole problem will be fixed.

Seems this will make the problem be more complex.

Quoted 7 lines
Quoted 7 lines
arch/mips/loongson/Kconfig +0 -0
 	select DMA_NONCOHERENT
 	select GENERIC_HARDIRQS_NO__DO_IRQ
 	select GENERIC_ISA_DMA_SUPPORT_BROKEN
@@ -62,6 +62,13 @@ endchoice
 config CS5536
 	bool
 
Quoted 11 lines

arch/mips/loongson/Kconfig endchoice · line 70

Quoted 6 lines
arch/mips/loongson/Kconfig +6 -0
+config CS5536_MFGPT
+	bool "Using cs5536's MFGPT as system clock"
+	depends on CS5536
+	help
+	  This is needed when cpufreq or oprofile is enabled in Lemote Loongson2F
+	  family machines

A patch to deal with interaction between oprofile and the cp0 count / compare was queued recently. So is there any remaining issue or is the comment here just outdated?

Okay, Will remove the out-of-date part.

Thanks & Regards,
Wu Zhangjin