Skip to contentSkip to conversation

LoongArch: Correct the calculation logic of thread_count

PATCH Superseded Qiang Ma 2 participants 5 messages Nov 27, 2025 LoongArch

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Qiang Ma

author
#0 lore raw

PATCH v2 1/1 LoongArch: Correct the calculation logic of thread_count

For thread_count, the current calculation method has a maximum of 255, which may not be sufficient in the future. Therefore, we are correcting it now.

Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]

[1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf

---
 arch/loongarch/kernel/setup.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/loongarch/kernel/setup.c +8 -1
index 25a87378e48e..8dd25fc4bd13 100644
@@ -56,6 +56,7 @@
 #define SMBIOS_FREQLOW_MASK		0xFF
 #define SMBIOS_CORE_PACKAGE_OFFSET	0x23
 #define SMBIOS_THREAD_PACKAGE_OFFSET	0x25
+#define SMBIOS_THREAD_PACKAGE_2_OFFSET	0x2E
 #define LOONGSON_EFI_ENABLE		(1 << 3)
 
 unsigned long fw_arg0, fw_arg1, fw_arg2;
@@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
 {
 	long freq_temp = 0;
 	char *dmi_data = (char *)dm;
+	u8 thread_count;
 
 	freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
 			((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
 	cpu_clock_freq = freq_temp * 1000000;
 
 	loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
-	loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+	thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+	if (thread_count != 0)
+		loongson_sysconf.cores_per_package =
+				dm->length >= 0x30 && thread_count == 0xFF ?
+				*(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET) :
+				thread_count;
 
 	pr_info("CpuClock = %llu\n", cpu_clock_freq);
 }
Signature · 1 line
2.20.1
maintainer
#1 lore raw

Hi, Qiang,

Quoted 34 lines · Qiang Ma

For thread_count, the current calculation method has a maximum of 255, which may not be sufficient in the future. Therefore, we are correcting it now.

Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]

[1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf

---
 arch/loongarch/kernel/setup.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/loongarch/kernel/setup.c +2 -0
index 25a87378e48e..8dd25fc4bd13 100644
@@ -56,6 +56,7 @@
 #define SMBIOS_FREQLOW_MASK            0xFF
 #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
 #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
+#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
 #define LOONGSON_EFI_ENABLE            (1 << 3)
 
 unsigned long fw_arg0, fw_arg1, fw_arg2;
@@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
 {
        long freq_temp = 0;
        char *dmi_data = (char *)dm;
+       u8 thread_count;
 
        freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
                        ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));

arch/loongarch/kernel/setup.c static void __init parse_cpu_table(const struct dmi_header *dm) · line 136

Quoted 10 lines
arch/loongarch/kernel/setup.c +6 -1
        cpu_clock_freq = freq_temp * 1000000;
 
        loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
-       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       if (thread_count != 0)
+               loongson_sysconf.cores_per_package =
+                               dm->length >= 0x30 && thread_count == 0xFF ?
+                               *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET) :
+                               thread_count;

According SPEC, it uses THREAD_COUNT_2 only if dm->length >= 30 and thread_count == 0xff, so the correct and simplest way is like this:

        cpu_clock_freq = freq_temp * 1000000;

        loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);

- loongson_sysconf.cores_per_package = *(dmi_data +
SMBIOS_THREAD_PACKAGE_OFFSET);
+ if (dm->length < 0x30)
+ thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+ else if (thread_count == 0xff)
+ thread_count = *(u16 *)(dmi_data +
SMBIOS_THREAD_PACKAGE_2_OFFSET);
+ loongson_sysconf.cores_per_package = thread_count;

        pr_info("CpuClock = %llu\n", cpu_clock_freq);

Huacai

Quoted 6 lines
arch/loongarch/kernel/setup.c +0 -1
        pr_info("CpuClock = %llu\n", cpu_clock_freq);
 }
--

2.20.1

author
#2 lore raw
Quoted 34 lines · Huacai Chen

Hi, Qiang,

Quoted 31 lines · Qiang Ma

For thread_count, the current calculation method has a maximum of 255, which may not be sufficient in the future. Therefore, we are correcting it now.

Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]

[1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf

---
  arch/loongarch/kernel/setup.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
arch/loongarch/kernel/setup.c +2 -0
index 25a87378e48e..8dd25fc4bd13 100644
@@ -56,6 +56,7 @@
  #define SMBIOS_FREQLOW_MASK            0xFF
  #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
  #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
+#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
  #define LOONGSON_EFI_ENABLE            (1 << 3)
 
  unsigned long fw_arg0, fw_arg1, fw_arg2;
@@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
  {
         long freq_temp = 0;
         char *dmi_data = (char *)dm;
+       u8 thread_count;
 
Quoted 7 lines

arch/loongarch/kernel/setup.c static void __init parse_cpu_table(const struct dmi_header *dm) · line 131

Quoted 7 lines
arch/loongarch/kernel/setup.c +1 -1
         freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
                         ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
         cpu_clock_freq = freq_temp * 1000000;
 
         loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
-       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);

Yes, this one should need to be converted:
thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);

Quoted 5 lines

arch/loongarch/kernel/setup.c static void __init parse_cpu_table(const struct dmi_header *dm) · line 136

Quoted 5 lines
arch/loongarch/kernel/setup.c +5 -0
+       if (thread_count != 0)
+               loongson_sysconf.cores_per_package =
+                               dm->length >= 0x30 && thread_count == 0xFF ?
+                               *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET) :
+                               thread_count;

This patch method refers to dmidecode, which is the same as it, like this:

                        if (data[0x25] != 0)
                                pr_attr("Thread Count", "%u", h->length >= 0x30 && data[0x25] == 0xFF ?

                                        WORD(data + 0x2E) : data[0x25]);

Quoted 15 lines

According SPEC, it uses THREAD_COUNT_2 only if dm->length >= 30 and thread_count == 0xff, so the correct and simplest way is like this:

         cpu_clock_freq = freq_temp * 1000000;

         loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);

- loongson_sysconf.cores_per_package = *(dmi_data +
SMBIOS_THREAD_PACKAGE_OFFSET);

(quoted diff) +4 -0
+       if (dm->length < 0x30)
+               thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       else if (thread_count == 0xff)
+               thread_count = *(u16 *)(dmi_data +

SMBIOS_THREAD_PACKAGE_2_OFFSET);
+ loongson_sysconf.cores_per_package = thread_count;

Quoted 1 line
         pr_info("CpuClock = %llu\n", cpu_clock_freq);

I feel there is a problem with this method.
I saw on the 3C6000/Q machine a few days ago that the dm->lenght is 0x30,
but only one byte is used, and the thread_count is 128, not 0xFF.
Is the initialization thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET) missing? Wouldn't it be better to change it this way?

+       u8 thread_count;

        freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) + ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK)); cpu_clock_freq = freq_temp * 1000000;

        loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]); -       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET); +       thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       if (thread_count == 0xFF && dm->length >= 0x30)
+               thread_count = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET); +       loongson_sysconf.cores_per_package = thread_count;

        pr_info("CpuClock = %llu\n", cpu_clock_freq); }

Quoted 7 lines

Huacai

Quoted 5 lines
arch/loongarch/kernel/setup.c +0 -1
         pr_info("CpuClock = %llu\n", cpu_clock_freq);
  }
--

2.20.1

maintainer
#3 lore raw
Quoted 77 lines · Qiang Ma
Quoted 34 lines · Huacai Chen

Hi, Qiang,

Quoted 31 lines · Qiang Ma

For thread_count, the current calculation method has a maximum of 255, which may not be sufficient in the future. Therefore, we are correcting it now.

Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]

[1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf

---
  arch/loongarch/kernel/setup.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
arch/loongarch/kernel/setup.c +2 -0
index 25a87378e48e..8dd25fc4bd13 100644
@@ -56,6 +56,7 @@
  #define SMBIOS_FREQLOW_MASK            0xFF
  #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
  #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
+#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
  #define LOONGSON_EFI_ENABLE            (1 << 3)
 
  unsigned long fw_arg0, fw_arg1, fw_arg2;
@@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
  {
         long freq_temp = 0;
         char *dmi_data = (char *)dm;
+       u8 thread_count;
 
Quoted 7 lines

arch/loongarch/kernel/setup.c static void __init parse_cpu_table(const struct dmi_header *dm) · line 131

Quoted 7 lines
arch/loongarch/kernel/setup.c +1 -1
         freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
                         ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
         cpu_clock_freq = freq_temp * 1000000;
 
         loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
-       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);

Yes, this one should need to be converted:
thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);

Quoted 5 lines

arch/loongarch/kernel/setup.c static void __init parse_cpu_table(const struct dmi_header *dm) · line 136

Quoted 5 lines
arch/loongarch/kernel/setup.c +5 -0
+       if (thread_count != 0)
+               loongson_sysconf.cores_per_package =
+                               dm->length >= 0x30 && thread_count == 0xFF ?
+                               *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET) :
+                               thread_count;

This patch method refers to dmidecode, which is the same as it, like this:

                         if (data[0x25] != 0)
                                 pr_attr("Thread Count", "%u",
                                         h->length >= 0x30 && data[0x25]

== 0xFF ?

                                         WORD(data + 0x2E) : data[0x25]);
Quoted 15 lines

According SPEC, it uses THREAD_COUNT_2 only if dm->length >= 30 and thread_count == 0xff, so the correct and simplest way is like this:

         cpu_clock_freq = freq_temp * 1000000;

         loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);

- loongson_sysconf.cores_per_package = *(dmi_data +
SMBIOS_THREAD_PACKAGE_OFFSET);

(quoted diff) +4 -0
+       if (dm->length < 0x30)
+               thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       else if (thread_count == 0xff)
+               thread_count = *(u16 *)(dmi_data +

SMBIOS_THREAD_PACKAGE_2_OFFSET);
+ loongson_sysconf.cores_per_package = thread_count;

Quoted 6 lines
Quoted 1 line
         pr_info("CpuClock = %llu\n", cpu_clock_freq);

I feel there is a problem with this method.
I saw on the 3C6000/Q machine a few days ago that the dm->lenght is 0x30, but only one byte is used, and the thread_count is 128, not 0xFF. Is the initialization thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET) missing?

Yes, you are right. Kernel code prefers 0xff rather than 0xFF, and I want to add a comment, so I think the below is OK.
loongson_sysconf.cores_per_package = *(u8 *)(dmi_data +

SMBIOS_THREAD_PACKAGE_OFFSET);
        if (dm->length >= 0x30 && loongson_sysconf.cores_per_package == 0xff) {
                /* SMBIOS 3.0+ has ThreadCount2 for more than 255 threads */
                loongson_sysconf.cores_per_package = *(u16 *)(dmi_data
+ SMBIOS_THREAD_PACKAGE_2_OFFSET);
        }

Huacai

Quoted 29 lines

Wouldn't it be better to change it this way?

+ u8 thread_count;

         freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
                         ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) &

SMBIOS_FREQLOW_MASK));
cpu_clock_freq = freq_temp * 1000000;

loongson_sysconf.cpuname = (void *)dmi_string_parse(dm,
dmi_data[16]);
- loongson_sysconf.cores_per_package = *(dmi_data +
SMBIOS_THREAD_PACKAGE_OFFSET);

(quoted diff) +3 -0
+       thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       if (thread_count == 0xFF && dm->length >= 0x30)
+               thread_count = *(u16 *)(dmi_data +

SMBIOS_THREAD_PACKAGE_2_OFFSET);
+ loongson_sysconf.cores_per_package = thread_count;

         pr_info("CpuClock = %llu\n", cpu_clock_freq);

}

Quoted 7 lines

Huacai

Quoted 5 lines
arch/loongarch/kernel/setup.c +0 -1
         pr_info("CpuClock = %llu\n", cpu_clock_freq);
  }
--

2.20.1

author
#4 lore raw
Quoted 76 lines · Huacai Chen
Quoted 75 lines · Qiang Ma
Quoted 34 lines · Huacai Chen

Hi, Qiang,

Quoted 31 lines · Qiang Ma

For thread_count, the current calculation method has a maximum of 255, which may not be sufficient in the future. Therefore, we are correcting it now.

Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]

[1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf

---
   arch/loongarch/kernel/setup.c | 9 ++++++++-
   1 file changed, 8 insertions(+), 1 deletion(-)
arch/loongarch/kernel/setup.c +2 -0
index 25a87378e48e..8dd25fc4bd13 100644
@@ -56,6 +56,7 @@
   #define SMBIOS_FREQLOW_MASK            0xFF
   #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
   #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
+#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
   #define LOONGSON_EFI_ENABLE            (1 << 3)
 
   unsigned long fw_arg0, fw_arg1, fw_arg2;
@@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
   {
          long freq_temp = 0;
          char *dmi_data = (char *)dm;
+       u8 thread_count;
 
Quoted 7 lines

arch/loongarch/kernel/setup.c static void __init parse_cpu_table(const struct dmi_header *dm) · line 131

Quoted 7 lines
arch/loongarch/kernel/setup.c +1 -1
          freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
                          ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
          cpu_clock_freq = freq_temp * 1000000;
 
          loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
-       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);

Yes, this one should need to be converted:
thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);

Quoted 5 lines

arch/loongarch/kernel/setup.c static void __init parse_cpu_table(const struct dmi_header *dm) · line 136

Quoted 5 lines
arch/loongarch/kernel/setup.c +5 -0
+       if (thread_count != 0)
+               loongson_sysconf.cores_per_package =
+                               dm->length >= 0x30 && thread_count == 0xFF ?
+                               *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET) :
+                               thread_count;

This patch method refers to dmidecode, which is the same as it, like this:

                          if (data[0x25] != 0)
                                  pr_attr("Thread Count", "%u",
                                          h->length >= 0x30 && data[0x25]

== 0xFF ?

                                          WORD(data + 0x2E) : data[0x25]);
Quoted 15 lines

According SPEC, it uses THREAD_COUNT_2 only if dm->length >= 30 and thread_count == 0xff, so the correct and simplest way is like this:

          cpu_clock_freq = freq_temp * 1000000;

          loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);

- loongson_sysconf.cores_per_package = *(dmi_data +
SMBIOS_THREAD_PACKAGE_OFFSET);

(quoted diff) +4 -0
+       if (dm->length < 0x30)
+               thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       else if (thread_count == 0xff)
+               thread_count = *(u16 *)(dmi_data +

SMBIOS_THREAD_PACKAGE_2_OFFSET);
+ loongson_sysconf.cores_per_package = thread_count;

Quoted 7 lines
Quoted 6 lines
Quoted 1 line
          pr_info("CpuClock = %llu\n", cpu_clock_freq);

I feel there is a problem with this method.
I saw on the 3C6000/Q machine a few days ago that the dm->lenght is 0x30, but only one byte is used, and the thread_count is 128, not 0xFF. Is the initialization thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET) missing?

Yes, you are right. Kernel code prefers 0xff rather than 0xFF, and I

I see.

Quoted 1 line

want to add a comment, so I think the below is OK.

Yes, I also think it's okay. I will update it to v3.

Quoted 37 lines

loongson_sysconf.cores_per_package = *(u8 *)(dmi_data +

SMBIOS_THREAD_PACKAGE_OFFSET);
         if (dm->length >= 0x30 && loongson_sysconf.cores_per_package == 0xff) {
                 /* SMBIOS 3.0+ has ThreadCount2 for more than 255 threads */
                 loongson_sysconf.cores_per_package = *(u16 *)(dmi_data
+ SMBIOS_THREAD_PACKAGE_2_OFFSET);
         }

Huacai

Quoted 28 lines

Wouldn't it be better to change it this way?

+ u8 thread_count;

          freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
                          ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) &

SMBIOS_FREQLOW_MASK));
cpu_clock_freq = freq_temp * 1000000;

loongson_sysconf.cpuname = (void *)dmi_string_parse(dm,
dmi_data[16]);
- loongson_sysconf.cores_per_package = *(dmi_data +
SMBIOS_THREAD_PACKAGE_OFFSET);

(quoted diff) +3 -0
+       thread_count = *(u8 *)(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+       if (thread_count == 0xFF && dm->length >= 0x30)
+               thread_count = *(u16 *)(dmi_data +

SMBIOS_THREAD_PACKAGE_2_OFFSET);
+ loongson_sysconf.cores_per_package = thread_count;

          pr_info("CpuClock = %llu\n", cpu_clock_freq);

}

Quoted 7 lines

Huacai

Quoted 5 lines
arch/loongarch/kernel/setup.c +0 -1
          pr_info("CpuClock = %llu\n", cpu_clock_freq);
   }
--

2.20.1