* [PATCH 0/5] cpufreq: loongson3: Fix wrong behaviors on multi-node servers
@ 2026-08-18 12:39 Huacai Chen
2026-08-18 12:39 ` [PATCH 1/5] cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64 Huacai Chen
` (4 more replies)
0 siblings, 5 replies; 14+ messages in thread
From: Huacai Chen @ 2026-08-18 12:39 UTC (permalink / raw)
To: Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
Huacai Chen, Hongliang Wang
Our server productions (e.g. Loongson-3D6000/3E6000) can have multiple
nodes in one package and SMC mailboxes are also per-node. However, the
current driver has wrong behaviors on multi-node servers, including: id
field in smc_message is too narrow, mutex is not per-node, IOCSR read/
write can only perform on the current node, etc. This series fix all the
above problems.
Hongliang Wang & Huacai Chen (5):
cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64
cpufreq: loongson3: Adjust the width of id and val in smc_message
cpufreq: loongson3: Replace per-package mutex with per-node
cpufreq: loongson3: Use global physical CPU ID in get/target callbacks
cpufreq: loongson3: Replace IOCSR read/write with MMIO ones
Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
drivers/cpufreq/Kconfig | 1 +
drivers/cpufreq/loongson3_cpufreq.c | 67 ++++++++++++++++++++++++-------------
2 files changed, 44 insertions(+), 24 deletions(-)
---
2.27.0
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH 1/5] cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64
2026-08-18 12:39 [PATCH 0/5] cpufreq: loongson3: Fix wrong behaviors on multi-node servers Huacai Chen
@ 2026-08-18 12:39 ` Huacai Chen
2026-08-20 9:23 ` Zhongqiu Han
2026-08-18 12:39 ` [PATCH 2/5] cpufreq: loongson3: Adjust the width of id and val in smc_message Huacai Chen
` (3 subsequent siblings)
4 siblings, 1 reply; 14+ messages in thread
From: Huacai Chen @ 2026-08-18 12:39 UTC (permalink / raw)
To: Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
Huacai Chen, stable
32BIT Loongson machines don't have SMC and FreqCtrl registers, so make
this drvier depend on MACH_LOONGSON64.
Cc: stable@vger.kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
drivers/cpufreq/Kconfig | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/cpufreq/Kconfig b/drivers/cpufreq/Kconfig
index db83f3365698..edc1299098d4 100644
--- a/drivers/cpufreq/Kconfig
+++ b/drivers/cpufreq/Kconfig
@@ -288,6 +288,7 @@ endif
if LOONGARCH
config LOONGSON3_CPUFREQ
tristate "Loongson3 CPUFreq Driver"
+ depends on MACH_LOONGSON64
help
This option adds a CPUFreq driver for Loongson processors which
support software configurable cpu frequency.
--
2.52.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH 2/5] cpufreq: loongson3: Adjust the width of id and val in smc_message
2026-08-18 12:39 [PATCH 0/5] cpufreq: loongson3: Fix wrong behaviors on multi-node servers Huacai Chen
2026-08-18 12:39 ` [PATCH 1/5] cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64 Huacai Chen
@ 2026-08-18 12:39 ` Huacai Chen
2026-08-20 10:03 ` Zhongqiu Han
2026-08-18 12:39 ` [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node Huacai Chen
` (2 subsequent siblings)
4 siblings, 1 reply; 14+ messages in thread
From: Huacai Chen @ 2026-08-18 12:39 UTC (permalink / raw)
To: Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
Huacai Chen, stable, Hongliang Wang
The id field of smc_message is usually stand for the CPU ID. In the past
this driver was only tested for desktop and laptop productions so 4-bits
are enough. But now we have servers that can have as many as 256 cores,
so we need 8-bits CPU ID.
On the other hand, the val field is usually stand for CPU frequency so
12-bits are enough to represent 4GHz, and for higher frequencies there
is an extra bit to extend (enough for 8GHz).
In theory, this is a incompatible change, but fortunately the old SMC
firmwares are not widely shipped and can be updated on the air, thus we
can safely adjust the widths.
Cc: stable@vger.kernel.org
Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
drivers/cpufreq/loongson3_cpufreq.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
index 1e8715ea1b77..630f679aa739 100644
--- a/drivers/cpufreq/loongson3_cpufreq.c
+++ b/drivers/cpufreq/loongson3_cpufreq.c
@@ -21,9 +21,9 @@
union smc_message {
u32 value;
struct {
- u32 id : 4;
+ u32 id : 8;
u32 info : 4;
- u32 val : 16;
+ u32 val : 12;
u32 cmd : 6;
u32 extra : 1;
u32 complete : 1;
--
2.52.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node
2026-08-18 12:39 [PATCH 0/5] cpufreq: loongson3: Fix wrong behaviors on multi-node servers Huacai Chen
2026-08-18 12:39 ` [PATCH 1/5] cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64 Huacai Chen
2026-08-18 12:39 ` [PATCH 2/5] cpufreq: loongson3: Adjust the width of id and val in smc_message Huacai Chen
@ 2026-08-18 12:39 ` Huacai Chen
2026-08-20 11:34 ` Zhongqiu Han
2026-08-18 12:39 ` [PATCH 4/5] cpufreq: loongson3: Use global physical CPU ID in get/target callbacks Huacai Chen
2026-08-18 12:39 ` [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones Huacai Chen
4 siblings, 1 reply; 14+ messages in thread
From: Huacai Chen @ 2026-08-18 12:39 UTC (permalink / raw)
To: Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
Huacai Chen, stable, Hongliang Wang
Our server productions (e.g. Loongson-3D6000/3E6000) can have multiple
nodes in one package and SMC mailboxes are also per-node. So replace the
per-package mutex with per-node one.
Cc: stable@vger.kernel.org
Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
drivers/cpufreq/loongson3_cpufreq.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
index 630f679aa739..e3cd78a5ab18 100644
--- a/drivers/cpufreq/loongson3_cpufreq.c
+++ b/drivers/cpufreq/loongson3_cpufreq.c
@@ -169,7 +169,7 @@ struct loongson3_freq_data {
struct cpufreq_frequency_table table[];
};
-static struct mutex cpufreq_mutex[MAX_PACKAGES];
+static struct mutex cpufreq_mutex[MAX_NUMNODES];
static struct cpufreq_driver loongson3_cpufreq_driver;
static DEFINE_PER_CPU(struct loongson3_freq_data *, freq_data);
@@ -177,14 +177,14 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
{
int retries;
unsigned int cpu = raw_smp_processor_id();
- unsigned int package = cpu_data[cpu].package;
+ unsigned int nid = cpu_to_node(cpu);
union smc_message msg, last;
- mutex_lock(&cpufreq_mutex[package]);
+ mutex_lock(&cpufreq_mutex[nid]);
last.value = iocsr_read32(LOONGARCH_IOCSR_SMCMBX);
if (!last.complete) {
- mutex_unlock(&cpufreq_mutex[package]);
+ mutex_unlock(&cpufreq_mutex[nid]);
return -EPERM;
}
@@ -208,11 +208,11 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
}
if (!msg.complete || msg.cmd != CMD_OK) {
- mutex_unlock(&cpufreq_mutex[package]);
+ mutex_unlock(&cpufreq_mutex[nid]);
return -EPERM;
}
- mutex_unlock(&cpufreq_mutex[package]);
+ mutex_unlock(&cpufreq_mutex[nid]);
return msg.val;
}
@@ -337,7 +337,7 @@ static int loongson3_cpufreq_probe(struct platform_device *pdev)
{
int i, ret;
- for (i = 0; i < MAX_PACKAGES; i++) {
+ for (i = 0; i < MAX_NUMNODES; i++) {
ret = devm_mutex_init(&pdev->dev, &cpufreq_mutex[i]);
if (ret)
return ret;
--
2.52.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH 4/5] cpufreq: loongson3: Use global physical CPU ID in get/target callbacks
2026-08-18 12:39 [PATCH 0/5] cpufreq: loongson3: Fix wrong behaviors on multi-node servers Huacai Chen
` (2 preceding siblings ...)
2026-08-18 12:39 ` [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node Huacai Chen
@ 2026-08-18 12:39 ` Huacai Chen
2026-08-20 12:23 ` Zhongqiu Han
2026-08-18 12:39 ` [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones Huacai Chen
4 siblings, 1 reply; 14+ messages in thread
From: Huacai Chen @ 2026-08-18 12:39 UTC (permalink / raw)
To: Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
Huacai Chen, stable, Hongliang Wang
Our server productions (e.g. Loongson-3D6000/3E6000) can have discrete
global physical CPU IDs while the core ID inside the packages are always
continuous. In these cases we should use global physical CPU IDs to get
and set frequencies.
Cc: stable@vger.kernel.org
Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
drivers/cpufreq/loongson3_cpufreq.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
index e3cd78a5ab18..c75c0e30e881 100644
--- a/drivers/cpufreq/loongson3_cpufreq.c
+++ b/drivers/cpufreq/loongson3_cpufreq.c
@@ -219,38 +219,40 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
static unsigned int loongson3_cpufreq_get(unsigned int cpu)
{
- int ret;
+ int ret, core = cpu_logical_map(cpu);
- ret = do_service_request(cpu, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_INFO, 0, 0);
+ ret = do_service_request(core, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_INFO, 0, 0);
return ret * KILO;
}
static int loongson3_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
{
- int ret;
+ int ret, core = cpu_logical_map(policy->cpu);
- ret = do_service_request(cpu_data[policy->cpu].core,
- FREQ_INFO_TYPE_LEVEL, CMD_SET_FREQ_INFO, index, 0);
+ ret = do_service_request(core, FREQ_INFO_TYPE_LEVEL, CMD_SET_FREQ_INFO,
+ index, 0);
return (ret >= 0) ? 0 : ret;
}
static int configure_freq_table(int cpu)
{
- int i, ret, boost_level, max_level, freq_level;
+ int i, ret, core, boost_level, max_level, freq_level;
struct platform_device *pdev = cpufreq_get_driver_data();
struct loongson3_freq_data *data;
if (per_cpu(freq_data, cpu))
return 0;
- ret = do_service_request(cpu, 0, CMD_GET_FREQ_LEVEL_NUM, 0, 0);
+ core = cpu_logical_map(cpu);
+
+ ret = do_service_request(core, 0, CMD_GET_FREQ_LEVEL_NUM, 0, 0);
if (ret < 0)
return ret;
max_level = ret;
- ret = do_service_request(cpu, 0, CMD_GET_FREQ_BOOST_LEVEL, 0, 0);
+ ret = do_service_request(core, 0, CMD_GET_FREQ_BOOST_LEVEL, 0, 0);
if (ret < 0)
return ret;
boost_level = ret;
@@ -263,7 +265,7 @@ static int configure_freq_table(int cpu)
data->def_freq_level = boost_level - 1;
for (i = 0; i < freq_level; i++) {
- ret = do_service_request(cpu, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_LEVEL_INFO, i, 0);
+ ret = do_service_request(core, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_LEVEL_INFO, i, 0);
if (ret < 0) {
devm_kfree(&pdev->dev, data);
return ret;
--
2.52.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones
2026-08-18 12:39 [PATCH 0/5] cpufreq: loongson3: Fix wrong behaviors on multi-node servers Huacai Chen
` (3 preceding siblings ...)
2026-08-18 12:39 ` [PATCH 4/5] cpufreq: loongson3: Use global physical CPU ID in get/target callbacks Huacai Chen
@ 2026-08-18 12:39 ` Huacai Chen
2026-08-20 12:57 ` Zhongqiu Han
2026-08-21 5:01 ` Xi Ruoyao
4 siblings, 2 replies; 14+ messages in thread
From: Huacai Chen @ 2026-08-18 12:39 UTC (permalink / raw)
To: Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
Huacai Chen, stable
Our server productions (e.g. Loongson-3D6000/3E6000) can have multiple
nodes in one package and SMC mailboxes are also per-node. However, IOCSR
read/write can only perform on the current node, while sometimes we want
to perform on other nodes (e.g. when switch governor, the get and target
callbacks are not run on target core). So replace IOCSR read/write with
MMIO ones.
Cc: stable@vger.kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
drivers/cpufreq/loongson3_cpufreq.c | 31 ++++++++++++++++++++++-------
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
index c75c0e30e881..e5062cd62390 100644
--- a/drivers/cpufreq/loongson3_cpufreq.c
+++ b/drivers/cpufreq/loongson3_cpufreq.c
@@ -164,6 +164,12 @@ union smc_message {
#define FREQ_MAX_LEVEL 16
+#define MMIO_SMCMBX(node) \
+ ((void __iomem *)(IO_BASE | (u64)(node) << NODE_ADDRSPACE_SHIFT | LOONGSON_REG_BASE | LOONGARCH_IOCSR_SMCMBX))
+
+#define MMIO_MISC_FUNC(node) \
+ ((void __iomem *)(IO_BASE | (u64)(node) << NODE_ADDRSPACE_SHIFT | LOONGSON_REG_BASE | LOONGARCH_IOCSR_MISC_FUNC))
+
struct loongson3_freq_data {
unsigned int def_freq_level;
struct cpufreq_frequency_table table[];
@@ -176,13 +182,25 @@ static DEFINE_PER_CPU(struct loongson3_freq_data *, freq_data);
static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 extra)
{
int retries;
- unsigned int cpu = raw_smp_processor_id();
- unsigned int nid = cpu_to_node(cpu);
+ unsigned int cpu, nid;
union smc_message msg, last;
+ switch (cmd) {
+ case CMD_GET_FREQ_INFO:
+ case CMD_SET_FREQ_INFO:
+ case CMD_GET_FREQ_LEVEL_NUM:
+ case CMD_GET_FREQ_LEVEL_INFO:
+ case CMD_GET_FREQ_BOOST_LEVEL:
+ cpu = cpu_number_map(id);
+ break;
+ default:
+ cpu = raw_smp_processor_id();
+ }
+ nid = cpu_to_node(cpu);
+
mutex_lock(&cpufreq_mutex[nid]);
- last.value = iocsr_read32(LOONGARCH_IOCSR_SMCMBX);
+ last.value = readl(MMIO_SMCMBX(nid));
if (!last.complete) {
mutex_unlock(&cpufreq_mutex[nid]);
return -EPERM;
@@ -195,12 +213,11 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
msg.extra = extra;
msg.complete = 0;
- iocsr_write32(msg.value, LOONGARCH_IOCSR_SMCMBX);
- iocsr_write32(iocsr_read32(LOONGARCH_IOCSR_MISC_FUNC) | IOCSR_MISC_FUNC_SOFT_INT,
- LOONGARCH_IOCSR_MISC_FUNC);
+ writel(msg.value, MMIO_SMCMBX(nid));
+ writel(readl(MMIO_MISC_FUNC(nid)) | IOCSR_MISC_FUNC_SOFT_INT, MMIO_MISC_FUNC(nid));
for (retries = 0; retries < 10000; retries++) {
- msg.value = iocsr_read32(LOONGARCH_IOCSR_SMCMBX);
+ msg.value = readl(MMIO_SMCMBX(nid));
if (msg.complete)
break;
--
2.52.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH 1/5] cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64
2026-08-18 12:39 ` [PATCH 1/5] cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64 Huacai Chen
@ 2026-08-20 9:23 ` Zhongqiu Han
0 siblings, 0 replies; 14+ messages in thread
From: Zhongqiu Han @ 2026-08-20 9:23 UTC (permalink / raw)
To: Huacai Chen, Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, zhongqiu.han
Hello Huacai,
Just share a few inline comments/questions below:
On 8/18/2026 8:39 PM, Huacai Chen wrote:
> 32BIT Loongson machines don't have SMC and FreqCtrl registers, so make
> this drvier depend on MACH_LOONGSON64.
Typo --> driver? Likewise the subject.
>
> Cc: stable@vger.kernel.org
Nit, it is better to add fixes tag? For example:
ccf51454145b ("cpufreq: Add Loongson-3 CPUFreq driver support")
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> drivers/cpufreq/Kconfig | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/cpufreq/Kconfig b/drivers/cpufreq/Kconfig
> index db83f3365698..edc1299098d4 100644
> --- a/drivers/cpufreq/Kconfig
> +++ b/drivers/cpufreq/Kconfig
> @@ -288,6 +288,7 @@ endif
> if LOONGARCH
> config LOONGSON3_CPUFREQ
> tristate "Loongson3 CPUFreq Driver"
> + depends on MACH_LOONGSON64
Small nit:
Please feel free to correct me.
There are two MACH_LOONGSON64 symbols in the tree:
arch/loongarch/Kconfig: config MACH_LOONGSON64
def_bool 64BIT
arch/mips/Kconfig: config MACH_LOONGSON64
bool "Loongson 64-bit family of
machines"
select ...
The one that actually applies here is the LoongArch one, i.e. this is
really just "depends on 64BIT". Since drivers/cpufreq/Kconfig is a
shared file that also contains an "if MIPS" block, please at least
is it better to mention it in the changelog which MACH_LOONGSON64 is
using, because the LoongArch one has no prompt and is therefore
invisible in menuconfig. Or just write "depends on 64BIT"?
> help
> This option adds a CPUFreq driver for Loongson processors which
> support software configurable cpu frequency.
--
Thx and BRs,
Zhongqiu Han
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 2/5] cpufreq: loongson3: Adjust the width of id and val in smc_message
2026-08-18 12:39 ` [PATCH 2/5] cpufreq: loongson3: Adjust the width of id and val in smc_message Huacai Chen
@ 2026-08-20 10:03 ` Zhongqiu Han
0 siblings, 0 replies; 14+ messages in thread
From: Zhongqiu Han @ 2026-08-20 10:03 UTC (permalink / raw)
To: Huacai Chen, Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, Hongliang Wang, zhongqiu.han
Hi Huacai,
Please feel free to comment on the question below, or simply ignore it
if you do not think it is worth addressing from an architectural
perspective.
On 8/18/2026 8:39 PM, Huacai Chen wrote:
> The id field of smc_message is usually stand for the CPU ID. In the past
> this driver was only tested for desktop and laptop productions so 4-bits
> are enough. But now we have servers that can have as many as 256 cores,
> so we need 8-bits CPU ID.
>
> On the other hand, the val field is usually stand for CPU frequency so
> 12-bits are enough to represent 4GHz, and for higher frequencies there
> is an extra bit to extend (enough for 8GHz).
The extra bit appears to be unused in the current implementation.
Perhaps it would be better to enforce the limit explicitly for id and
val bits, for example by using FIELD_MAX().
>
> In theory, this is a incompatible change, but fortunately the old SMC
> firmwares are not widely shipped and can be updated on the air, thus we
> can safely adjust the widths.
Not sure if using CMD_GET_VERSION check can avoid such incompatible
issue?
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> drivers/cpufreq/loongson3_cpufreq.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
> index 1e8715ea1b77..630f679aa739 100644
> --- a/drivers/cpufreq/loongson3_cpufreq.c
> +++ b/drivers/cpufreq/loongson3_cpufreq.c
> @@ -21,9 +21,9 @@
> union smc_message {
> u32 value;
> struct {
> - u32 id : 4;
> + u32 id : 8;
> u32 info : 4;
> - u32 val : 16;
> + u32 val : 12;
> u32 cmd : 6;
> u32 extra : 1;
> u32 complete : 1;
--
Thx and BRs,
Zhongqiu Han
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node
2026-08-18 12:39 ` [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node Huacai Chen
@ 2026-08-20 11:34 ` Zhongqiu Han
2026-08-20 12:35 ` Zhongqiu Han
0 siblings, 1 reply; 14+ messages in thread
From: Zhongqiu Han @ 2026-08-20 11:34 UTC (permalink / raw)
To: Huacai Chen, Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, Hongliang Wang, zhongqiu.han
On 8/18/2026 8:39 PM, Huacai Chen wrote:
> Our server productions (e.g. Loongson-3D6000/3E6000) can have multiple
> nodes in one package and SMC mailboxes are also per-node. So replace the
> per-package mutex with per-node one.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> drivers/cpufreq/loongson3_cpufreq.c | 14 +++++++-------
> 1 file changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
> index 630f679aa739..e3cd78a5ab18 100644
> --- a/drivers/cpufreq/loongson3_cpufreq.c
> +++ b/drivers/cpufreq/loongson3_cpufreq.c
> @@ -169,7 +169,7 @@ struct loongson3_freq_data {
> struct cpufreq_frequency_table table[];
> };
>
> -static struct mutex cpufreq_mutex[MAX_PACKAGES];
> +static struct mutex cpufreq_mutex[MAX_NUMNODES];
> static struct cpufreq_driver loongson3_cpufreq_driver;
> static DEFINE_PER_CPU(struct loongson3_freq_data *, freq_data);
>
> @@ -177,14 +177,14 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
> {
> int retries;
> unsigned int cpu = raw_smp_processor_id();
> - unsigned int package = cpu_data[cpu].package;
> + unsigned int nid = cpu_to_node(cpu);
> union smc_message msg, last;
>
> - mutex_lock(&cpufreq_mutex[package]);
> + mutex_lock(&cpufreq_mutex[nid]);
>
> last.value = iocsr_read32(LOONGARCH_IOCSR_SMCMBX);
> if (!last.complete) {
> - mutex_unlock(&cpufreq_mutex[package]);
> + mutex_unlock(&cpufreq_mutex[nid]);
> return -EPERM;
> }
>
> @@ -208,11 +208,11 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
> }
>
> if (!msg.complete || msg.cmd != CMD_OK) {
> - mutex_unlock(&cpufreq_mutex[package]);
> + mutex_unlock(&cpufreq_mutex[nid]);
> return -EPERM;
> }
>
> - mutex_unlock(&cpufreq_mutex[package]);
> + mutex_unlock(&cpufreq_mutex[nid]);
An optimization independent of this patch: considering to use
guard(mutex)(xxx.lock)
>
> return msg.val;
> }
> @@ -337,7 +337,7 @@ static int loongson3_cpufreq_probe(struct platform_device *pdev)
> {
> int i, ret;
>
> - for (i = 0; i < MAX_PACKAGES; i++) {
> + for (i = 0; i < MAX_NUMNODES; i++) {
> ret = devm_mutex_init(&pdev->dev, &cpufreq_mutex[i]);
> if (ret)
> return ret;
--
Thx and BRs,
Zhongqiu Han
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 4/5] cpufreq: loongson3: Use global physical CPU ID in get/target callbacks
2026-08-18 12:39 ` [PATCH 4/5] cpufreq: loongson3: Use global physical CPU ID in get/target callbacks Huacai Chen
@ 2026-08-20 12:23 ` Zhongqiu Han
0 siblings, 0 replies; 14+ messages in thread
From: Zhongqiu Han @ 2026-08-20 12:23 UTC (permalink / raw)
To: Huacai Chen, Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, Hongliang Wang, zhongqiu.han
On 8/18/2026 8:39 PM, Huacai Chen wrote:
> Our server productions (e.g. Loongson-3D6000/3E6000) can have discrete
> global physical CPU IDs while the core ID inside the packages are always
> continuous. In these cases we should use global physical CPU IDs to get
> and set frequencies.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> drivers/cpufreq/loongson3_cpufreq.c | 20 +++++++++++---------
> 1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
> index e3cd78a5ab18..c75c0e30e881 100644
> --- a/drivers/cpufreq/loongson3_cpufreq.c
> +++ b/drivers/cpufreq/loongson3_cpufreq.c
> @@ -219,38 +219,40 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
>
> static unsigned int loongson3_cpufreq_get(unsigned int cpu)
> {
> - int ret;
> + int ret, core = cpu_logical_map(cpu);
>
> - ret = do_service_request(cpu, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_INFO, 0, 0);
> + ret = do_service_request(core, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_INFO, 0, 0);
As patch 5/5 changelog:
"However, IOCSR read/write can only perform on the current node, while
sometimes we want to perform on other nodes"
However, patch 4/5 starts passing the global CPU number without updating
do_service_request(), which is only changed in patch 5/5. Doesn't that
mean patch 4/5 is broken on its own?
>
> return ret * KILO;
> }
>
A separate issue, do_service_request() can return errno such as -EPERM,
it will cause .get() func return a large unsigned integer value.
> static int loongson3_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
> {
> - int ret;
> + int ret, core = cpu_logical_map(policy->cpu);
>
> - ret = do_service_request(cpu_data[policy->cpu].core,
> - FREQ_INFO_TYPE_LEVEL, CMD_SET_FREQ_INFO, index, 0);
> + ret = do_service_request(core, FREQ_INFO_TYPE_LEVEL, CMD_SET_FREQ_INFO,
> + index, 0);
>
> return (ret >= 0) ? 0 : ret;
> }
>
> static int configure_freq_table(int cpu)
> {
> - int i, ret, boost_level, max_level, freq_level;
> + int i, ret, core, boost_level, max_level, freq_level;
> struct platform_device *pdev = cpufreq_get_driver_data();
> struct loongson3_freq_data *data;
>
> if (per_cpu(freq_data, cpu))
> return 0;
>
> - ret = do_service_request(cpu, 0, CMD_GET_FREQ_LEVEL_NUM, 0, 0);
> + core = cpu_logical_map(cpu);
> +
> + ret = do_service_request(core, 0, CMD_GET_FREQ_LEVEL_NUM, 0, 0);
> if (ret < 0)
> return ret;
> max_level = ret;
>
> - ret = do_service_request(cpu, 0, CMD_GET_FREQ_BOOST_LEVEL, 0, 0);
> + ret = do_service_request(core, 0, CMD_GET_FREQ_BOOST_LEVEL, 0, 0);
> if (ret < 0)
> return ret;
> boost_level = ret;
> @@ -263,7 +265,7 @@ static int configure_freq_table(int cpu)
> data->def_freq_level = boost_level - 1;
>
> for (i = 0; i < freq_level; i++) {
> - ret = do_service_request(cpu, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_LEVEL_INFO, i, 0);
> + ret = do_service_request(core, FREQ_INFO_TYPE_FREQ, CMD_GET_FREQ_LEVEL_INFO, i, 0);
> if (ret < 0) {
> devm_kfree(&pdev->dev, data);
> return ret;
--
Thx and BRs,
Zhongqiu Han
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node
2026-08-20 11:34 ` Zhongqiu Han
@ 2026-08-20 12:35 ` Zhongqiu Han
2026-08-21 4:54 ` Xi Ruoyao
0 siblings, 1 reply; 14+ messages in thread
From: Zhongqiu Han @ 2026-08-20 12:35 UTC (permalink / raw)
To: Huacai Chen, Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, Hongliang Wang, zhongqiu.han
On 8/20/2026 7:34 PM, Zhongqiu Han wrote:
> On 8/18/2026 8:39 PM, Huacai Chen wrote:
>> Our server productions (e.g. Loongson-3D6000/3E6000) can have multiple
>> nodes in one package and SMC mailboxes are also per-node. So replace the
>> per-package mutex with per-node one.
>>
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Hongliang Wang <wanghongliang@loongson.cn>
>> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
>> ---
>> drivers/cpufreq/loongson3_cpufreq.c | 14 +++++++-------
>> 1 file changed, 7 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/
>> loongson3_cpufreq.c
>> index 630f679aa739..e3cd78a5ab18 100644
>> --- a/drivers/cpufreq/loongson3_cpufreq.c
>> +++ b/drivers/cpufreq/loongson3_cpufreq.c
>> @@ -169,7 +169,7 @@ struct loongson3_freq_data {
>> struct cpufreq_frequency_table table[];
>> };
>> -static struct mutex cpufreq_mutex[MAX_PACKAGES];
>> +static struct mutex cpufreq_mutex[MAX_NUMNODES];
>> static struct cpufreq_driver loongson3_cpufreq_driver;
>> static DEFINE_PER_CPU(struct loongson3_freq_data *, freq_data);
>> @@ -177,14 +177,14 @@ static inline int do_service_request(u32 id, u32
>> info, u32 cmd, u32 val, u32 ext
>> {
>> int retries;
>> unsigned int cpu = raw_smp_processor_id();
>> - unsigned int package = cpu_data[cpu].package;
>> + unsigned int nid = cpu_to_node(cpu);
What if NUMA is disabled?
>> union smc_message msg, last;
>> - mutex_lock(&cpufreq_mutex[package]);
>> + mutex_lock(&cpufreq_mutex[nid]);
>> last.value = iocsr_read32(LOONGARCH_IOCSR_SMCMBX);
>> if (!last.complete) {
>> - mutex_unlock(&cpufreq_mutex[package]);
>> + mutex_unlock(&cpufreq_mutex[nid]);
>> return -EPERM;
>> }
>> @@ -208,11 +208,11 @@ static inline int do_service_request(u32 id, u32
>> info, u32 cmd, u32 val, u32 ext
>> }
>> if (!msg.complete || msg.cmd != CMD_OK) {
>> - mutex_unlock(&cpufreq_mutex[package]);
>> + mutex_unlock(&cpufreq_mutex[nid]);
>> return -EPERM;
>> }
>> - mutex_unlock(&cpufreq_mutex[package]);
>> + mutex_unlock(&cpufreq_mutex[nid]);
>
> An optimization independent of this patch: considering to use
> guard(mutex)(xxx.lock)
>
>> return msg.val;
>> }
>> @@ -337,7 +337,7 @@ static int loongson3_cpufreq_probe(struct
>> platform_device *pdev)
>> {
>> int i, ret;
>> - for (i = 0; i < MAX_PACKAGES; i++) {
>> + for (i = 0; i < MAX_NUMNODES; i++) {
>> ret = devm_mutex_init(&pdev->dev, &cpufreq_mutex[i]);
>> if (ret)
>> return ret;
>
>
--
Thx and BRs,
Zhongqiu Han
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones
2026-08-18 12:39 ` [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones Huacai Chen
@ 2026-08-20 12:57 ` Zhongqiu Han
2026-08-21 5:01 ` Xi Ruoyao
1 sibling, 0 replies; 14+ messages in thread
From: Zhongqiu Han @ 2026-08-20 12:57 UTC (permalink / raw)
To: Huacai Chen, Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, zhongqiu.han
On 8/18/2026 8:39 PM, Huacai Chen wrote:
> Our server productions (e.g. Loongson-3D6000/3E6000) can have multiple
> nodes in one package and SMC mailboxes are also per-node. However, IOCSR
> read/write can only perform on the current node, while sometimes we want
> to perform on other nodes (e.g. when switch governor, the get and target
> callbacks are not run on target core). So replace IOCSR read/write with
> MMIO ones.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> drivers/cpufreq/loongson3_cpufreq.c | 31 ++++++++++++++++++++++-------
> 1 file changed, 24 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
> index c75c0e30e881..e5062cd62390 100644
> --- a/drivers/cpufreq/loongson3_cpufreq.c
> +++ b/drivers/cpufreq/loongson3_cpufreq.c
> @@ -164,6 +164,12 @@ union smc_message {
>
> #define FREQ_MAX_LEVEL 16
>
> +#define MMIO_SMCMBX(node) \
> + ((void __iomem *)(IO_BASE | (u64)(node) << NODE_ADDRSPACE_SHIFT | LOONGSON_REG_BASE | LOONGARCH_IOCSR_SMCMBX))
> +
> +#define MMIO_MISC_FUNC(node) \
> + ((void __iomem *)(IO_BASE | (u64)(node) << NODE_ADDRSPACE_SHIFT | LOONGSON_REG_BASE | LOONGARCH_IOCSR_MISC_FUNC))
> +
> struct loongson3_freq_data {
> unsigned int def_freq_level;
> struct cpufreq_frequency_table table[];
> @@ -176,13 +182,25 @@ static DEFINE_PER_CPU(struct loongson3_freq_data *, freq_data);
> static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 extra)
> {
> int retries;
> - unsigned int cpu = raw_smp_processor_id();
> - unsigned int nid = cpu_to_node(cpu);
> + unsigned int cpu, nid;
> union smc_message msg, last;
>
> + switch (cmd) {
> + case CMD_GET_FREQ_INFO:
> + case CMD_SET_FREQ_INFO:
> + case CMD_GET_FREQ_LEVEL_NUM:
> + case CMD_GET_FREQ_LEVEL_INFO:
> + case CMD_GET_FREQ_BOOST_LEVEL:
> + cpu = cpu_number_map(id);
cpu_number_map() is undefined when CONFIG_SMP=n. Wouldn't that result in
a build failure?
> + break;
> + default:
> + cpu = raw_smp_processor_id();
> + }
> + nid = cpu_to_node(cpu);
> +
> mutex_lock(&cpufreq_mutex[nid]);
>
> - last.value = iocsr_read32(LOONGARCH_IOCSR_SMCMBX);
> + last.value = readl(MMIO_SMCMBX(nid));
> if (!last.complete) {
> mutex_unlock(&cpufreq_mutex[nid]);
> return -EPERM;
> @@ -195,12 +213,11 @@ static inline int do_service_request(u32 id, u32 info, u32 cmd, u32 val, u32 ext
> msg.extra = extra;
> msg.complete = 0;
>
> - iocsr_write32(msg.value, LOONGARCH_IOCSR_SMCMBX);
> - iocsr_write32(iocsr_read32(LOONGARCH_IOCSR_MISC_FUNC) | IOCSR_MISC_FUNC_SOFT_INT,
> - LOONGARCH_IOCSR_MISC_FUNC);
> + writel(msg.value, MMIO_SMCMBX(nid));
> + writel(readl(MMIO_MISC_FUNC(nid)) | IOCSR_MISC_FUNC_SOFT_INT, MMIO_MISC_FUNC(nid));
>
> for (retries = 0; retries < 10000; retries++) {
> - msg.value = iocsr_read32(LOONGARCH_IOCSR_SMCMBX);
> + msg.value = readl(MMIO_SMCMBX(nid));
> if (msg.complete)
> break;
>
Please ignore this comments if you do not think it is worth addressing:
A separate issue, it seems that DVFS and Boost feature is set in
loongson3_cpufreq_probe(), what if about on other nodes (non-boot cpu
node) and what if cpu hotplug?
static int loongson3_cpufreq_probe(struct platform_device *pdev)
{
int i, ret;
for (i = 0; i < MAX_PACKAGES; i++) {
ret = devm_mutex_init(&pdev->dev, &cpufreq_mutex[i]);
if (ret)
return ret;
}
ret = do_service_request(0, 0, CMD_GET_VERSION, 0, 0);
if (ret <= 0)
return -EPERM;
ret = do_service_request(FEATURE_DVFS, 0, CMD_SET_FEATURE,
FEATURE_DVFS_ENABLE | FEATURE_DVFS_BOOST, 0);
if (ret < 0)
return -EPERM;
......
}
--
Thx and BRs,
Zhongqiu Han
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node
2026-08-20 12:35 ` Zhongqiu Han
@ 2026-08-21 4:54 ` Xi Ruoyao
0 siblings, 0 replies; 14+ messages in thread
From: Xi Ruoyao @ 2026-08-21 4:54 UTC (permalink / raw)
To: Zhongqiu Han, Huacai Chen, Rafael J . Wysocki, Viresh Kumar,
Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, Hongliang Wang
在 2026/8/20 20:35, Zhongqiu Han 写道:
>>> - unsigned int package = cpu_data[cpu].package;
>>> + unsigned int nid = cpu_to_node(cpu);
>
> What if NUMA is disabled?
include/asm-generic/topology.h has a fallback:
#ifndef CONFIG_NUMA
/* Other architectures wishing to use this simple topology API should fill
in the below functions as appropriate in their own <asm/topology.h>
file. */
#ifndef cpu_to_node
#define cpu_to_node(cpu) ((void)(cpu),0)
#endif
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones
2026-08-18 12:39 ` [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones Huacai Chen
2026-08-20 12:57 ` Zhongqiu Han
@ 2026-08-21 5:01 ` Xi Ruoyao
1 sibling, 0 replies; 14+ messages in thread
From: Xi Ruoyao @ 2026-08-21 5:01 UTC (permalink / raw)
To: Huacai Chen, Rafael J . Wysocki, Viresh Kumar, Huacai Chen
Cc: loongarch, linux-pm, linux-kernel, Xuerui Wang, Jiaxun Yang,
stable, jeffbai
在 2026/8/18 20:39, Huacai Chen 写道:
> Our server productions (e.g. Loongson-3D6000/3E6000) can have multiple
> nodes in one package and SMC mailboxes are also per-node. However, IOCSR
> read/write can only perform on the current node, while sometimes we want
> to perform on other nodes (e.g. when switch governor, the get and target
> callbacks are not run on target core). So replace IOCSR read/write with
> MMIO ones.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> drivers/cpufreq/loongson3_cpufreq.c | 31 ++++++++++++++++++++++-------
> 1 file changed, 24 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/cpufreq/loongson3_cpufreq.c b/drivers/cpufreq/loongson3_cpufreq.c
> index c75c0e30e881..e5062cd62390 100644
> --- a/drivers/cpufreq/loongson3_cpufreq.c
> +++ b/drivers/cpufreq/loongson3_cpufreq.c
> @@ -164,6 +164,12 @@ union smc_message {
>
> #define FREQ_MAX_LEVEL 16
>
> +#define MMIO_SMCMBX(node) \
> + ((void __iomem *)(IO_BASE | (u64)(node) << NODE_ADDRSPACE_SHIFT | LOONGSON_REG_BASE | LOONGARCH_IOCSR_SMCMBX))
NODE_ADDRSPACE_SHIFT is defined in numa.h so that file needs to be
included, to avoid a build failure when NUMA is disabled (if NUMA is
enabled, numa.h happens to be pulled in via some other headers).
Also I'd suggest to use nid_to_addrbase(node) instead of hard coding the
shift for better readability.
With the changes:
Tested-by: Xi Ruoyao <xry111@xry111.site> # on XA61200
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-08-21 5:01 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-18 12:39 [PATCH 0/5] cpufreq: loongson3: Fix wrong behaviors on multi-node servers Huacai Chen
2026-08-18 12:39 ` [PATCH 1/5] cpufreq: loongson3: Make this drvier depend on MACH_LOONGSON64 Huacai Chen
2026-08-20 9:23 ` Zhongqiu Han
2026-08-18 12:39 ` [PATCH 2/5] cpufreq: loongson3: Adjust the width of id and val in smc_message Huacai Chen
2026-08-20 10:03 ` Zhongqiu Han
2026-08-18 12:39 ` [PATCH 3/5] cpufreq: loongson3: Replace per-package mutex with per-node Huacai Chen
2026-08-20 11:34 ` Zhongqiu Han
2026-08-20 12:35 ` Zhongqiu Han
2026-08-21 4:54 ` Xi Ruoyao
2026-08-18 12:39 ` [PATCH 4/5] cpufreq: loongson3: Use global physical CPU ID in get/target callbacks Huacai Chen
2026-08-20 12:23 ` Zhongqiu Han
2026-08-18 12:39 ` [PATCH 5/5] cpufreq: loongson3: Replace IOCSR read/write with MMIO ones Huacai Chen
2026-08-20 12:57 ` Zhongqiu Han
2026-08-21 5:01 ` Xi Ruoyao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox