Skip to contentSkip to conversation

board_f: show_dram_config: Print also real DRAM size

PATCH Superseded Pali Rohár 4 participants 11 messages Sep 11, 2022 – Sep 23, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 board_f: show_dram_config: Print also real DRAM size

32-bit U-Boot builds cannot use more than around 2 GB of DDR memory. But on some platforms/boards it is possible to connect also 4 GB SODIMM DDR memory. U-Boot currently prints only effective size of RAM which can use, which may be misleading as somebody would expect that this line prints total size of connected DDR modules. So change show_dram_config code to prints both real and effective DRAM size if they are different. If they are same then print just one number like before. It is possible that effective size is just few bytes smaller than the real size, so print both numbers only in case function print_size() prints formats them differently.

---
 common/board_f.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
common/board_f.c +30 -1
index 9e34fbee147e..3131a06db940 100644
@@ -54,6 +54,7 @@
 #include <asm/sections.h>
 #include <dm/root.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
 /*
  * Pointer to initial global data area
@@ -213,6 +214,30 @@ static int announce_dram_init(void)
 	return 0;
 }
 
+/*
+ * Check if the sizes in their natural units written in decimal format with
+ * one fraction number are same.
+ */
+static int sizes_near(unsigned long long size1, unsigned long long size2)
+{
+	unsigned int size1_scale = ilog2(size1) / 10 * 10;
+	unsigned int size1_val = (10 * size1 + ((1ULL << size1_scale) >> 1)) >> size1_scale;
+	unsigned int size2_scale = ilog2(size2) / 10 * 10;
+	unsigned int size2_val = (10 * size2 + ((1ULL << size2_scale) >> 1)) >> size2_scale;
+
+	if (size1_val == 10240) {
+		size1_val = 10;
+		size1_scale += 10;
+	}
+
+	if (size2_val == 10240) {
+		size2_val = 10;
+		size2_scale += 10;
+	}
+
+	return size1_scale == size2_scale && size1_val == size2_val;
+}
+
 static int show_dram_config(void)
 {
 	unsigned long long size;
@@ -229,7 +254,11 @@ static int show_dram_config(void)
 	}
 	debug("\nDRAM:  ");
 
-	print_size(size, "");
+	print_size(gd->ram_size, "");
+	if (!sizes_near(gd->ram_size, size)) {
+		printf(" (effective ");
+		print_size(size, ")");
+	}
 	board_add_ram_info(0);
 	putc('\n');
 
Signature · 1 line
2.20.1
#1 lore raw

Hi Pali,

Quoted 32 lines · Pali Rohár

32-bit U-Boot builds cannot use more than around 2 GB of DDR memory. But on some platforms/boards it is possible to connect also 4 GB SODIMM DDR memory. U-Boot currently prints only effective size of RAM which can use, which may be misleading as somebody would expect that this line prints total size of connected DDR modules. So change show_dram_config code to prints both real and effective DRAM size if they are different. If they are same then print just one number like before. It is possible that effective size is just few bytes smaller than the real size, so print both numbers only in case function print_size() prints formats them differently.

---
 common/board_f.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
common/board_f.c +1 -0
index 9e34fbee147e..3131a06db940 100644
@@ -54,6 +54,7 @@
 #include <asm/sections.h>
 #include <dm/root.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
 /*
  * Pointer to initial global data area
@@ -213,6 +214,30 @@ static int announce_dram_init(void)
        return 0;
 }
 

common/board_f.c static int announce_dram_init(void) · line 226

Quoted 10 lines
common/board_f.c +10 -0
+/*
+ * Check if the sizes in their natural units written in decimal format with
+ * one fraction number are same.
+ */
+static int sizes_near(unsigned long long size1, unsigned long long size2)
+{
+       unsigned int size1_scale = ilog2(size1) / 10 * 10;
+       unsigned int size1_val = (10 * size1 + ((1ULL << size1_scale) >> 1)) >> size1_scale;
+       unsigned int size2_scale = ilog2(size2) / 10 * 10;
+       unsigned int size2_val = (10 * size2 + ((1ULL << size2_scale) >> 1)) >> size2_scale;

Can you put that expression into a function with a comment, etc.? It is a bit hard to understand.

common/board_f.c static int announce_dram_init(void) · line 236

Quoted 10 lines
common/board_f.c +10 -0
+
+       if (size1_val == 10240) {
+               size1_val = 10;
+               size1_scale += 10;
+       }
+
+       if (size2_val == 10240) {
+               size2_val = 10;
+               size2_scale += 10;
+       }

If you are doing the same thing to each, why bother? It should not affect the expression below, should it? :

Quoted 21 lines
common/board_f.c +9 -2
+
+       return size1_scale == size2_scale && size1_val == size2_val;
+}
+
 static int show_dram_config(void)
 {
        unsigned long long size;
@@ -229,7 +254,11 @@ static int show_dram_config(void)
        }
        debug("\nDRAM:  ");
 
-       print_size(size, "");
+       print_size(gd->ram_size, "");
+       if (!sizes_near(gd->ram_size, size)) {
+               printf(" (effective ");
+               print_size(size, ")");
+       }
        board_add_ram_info(0);
        putc('\n');
 
--
Quoted 2 lines

2.20.1

Can we make this testable somehow? You could put the new code into a lib/ function, perhaps, and call it from a C unit test in test/lib ?

Regards,
Simon

#2 lore raw
Quoted 35 lines · On Monday 12 September 2022 07:34:47 Simon Glass wrote:

Hi Pali,

Quoted 32 lines · Pali Rohár

32-bit U-Boot builds cannot use more than around 2 GB of DDR memory. But on some platforms/boards it is possible to connect also 4 GB SODIMM DDR memory. U-Boot currently prints only effective size of RAM which can use, which may be misleading as somebody would expect that this line prints total size of connected DDR modules. So change show_dram_config code to prints both real and effective DRAM size if they are different. If they are same then print just one number like before. It is possible that effective size is just few bytes smaller than the real size, so print both numbers only in case function print_size() prints formats them differently.

---
 common/board_f.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
common/board_f.c +1 -0
index 9e34fbee147e..3131a06db940 100644
@@ -54,6 +54,7 @@
 #include <asm/sections.h>
 #include <dm/root.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
 /*
  * Pointer to initial global data area
@@ -213,6 +214,30 @@ static int announce_dram_init(void)
        return 0;
 }
 
Quoted 13 lines

common/board_f.c static int announce_dram_init(void) · line 226

Quoted 10 lines
common/board_f.c +10 -0
+/*
+ * Check if the sizes in their natural units written in decimal format with
+ * one fraction number are same.
+ */
+static int sizes_near(unsigned long long size1, unsigned long long size2)
+{
+       unsigned int size1_scale = ilog2(size1) / 10 * 10;
+       unsigned int size1_val = (10 * size1 + ((1ULL << size1_scale) >> 1)) >> size1_scale;
+       unsigned int size2_scale = ilog2(size2) / 10 * 10;
+       unsigned int size2_val = (10 * size2 + ((1ULL << size2_scale) >> 1)) >> size2_scale;

Can you put that expression into a function with a comment, etc.? It is a bit hard to understand.

Ok.

Quoted 5 lines
Quoted 5 lines
common/board_f.c +5 -0
+
+       if (size1_val == 10240) {
+               size1_val = 10;
+               size1_scale += 10;
+       }
Quoted 8 lines

common/board_f.c static int announce_dram_init(void) · line 236

Quoted 5 lines
common/board_f.c +5 -0
+
+       if (size2_val == 10240) {
+               size2_val = 10;
+               size2_scale += 10;
+       }

If you are doing the same thing to each, why bother? It should not affect the expression below, should it? :

This is interesting question, and the answer it that it is required and affects comparison expression below. For example for the case when size1 is below 1GB limit, size2 is above 1GB limit and both values are near. Imagine that size1 is approaching value 1GB from the left and size2 from the right side.

Quoted 27 lines
Quoted 21 lines
common/board_f.c +9 -2
+
+       return size1_scale == size2_scale && size1_val == size2_val;
+}
+
 static int show_dram_config(void)
 {
        unsigned long long size;
@@ -229,7 +254,11 @@ static int show_dram_config(void)
        }
        debug("\nDRAM:  ");
 
-       print_size(size, "");
+       print_size(gd->ram_size, "");
+       if (!sizes_near(gd->ram_size, size)) {
+               printf(" (effective ");
+               print_size(size, ")");
+       }
        board_add_ram_info(0);
        putc('\n');
 
--
Quoted 2 lines

2.20.1

Can we make this testable somehow? You could put the new code into a lib/ function, perhaps, and call it from a C unit test in test/lib ?

Quoted 2 lines

Regards,
Simon

Meh... I do not know how to test such code. Due to size / optimization requirements it is not a good idea to make function outside of board_f.c file.

#3 lore raw
Quoted 99 lines · Pali Rohár
Quoted 35 lines · On Monday 12 September 2022 07:34:47 Simon Glass wrote:

Hi Pali,

Quoted 32 lines · Pali Rohár

32-bit U-Boot builds cannot use more than around 2 GB of DDR memory. But on some platforms/boards it is possible to connect also 4 GB SODIMM DDR memory. U-Boot currently prints only effective size of RAM which can use, which may be misleading as somebody would expect that this line prints total size of connected DDR modules. So change show_dram_config code to prints both real and effective DRAM size if they are different. If they are same then print just one number like before. It is possible that effective size is just few bytes smaller than the real size, so print both numbers only in case function print_size() prints formats them differently.

---
 common/board_f.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
common/board_f.c +1 -0
index 9e34fbee147e..3131a06db940 100644
@@ -54,6 +54,7 @@
 #include <asm/sections.h>
 #include <dm/root.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
 /*
  * Pointer to initial global data area
@@ -213,6 +214,30 @@ static int announce_dram_init(void)
        return 0;
 }
 
Quoted 13 lines

common/board_f.c static int announce_dram_init(void) · line 226

Quoted 10 lines
common/board_f.c +10 -0
+/*
+ * Check if the sizes in their natural units written in decimal format with
+ * one fraction number are same.
+ */
+static int sizes_near(unsigned long long size1, unsigned long long size2)
+{
+       unsigned int size1_scale = ilog2(size1) / 10 * 10;
+       unsigned int size1_val = (10 * size1 + ((1ULL << size1_scale) >> 1)) >> size1_scale;
+       unsigned int size2_scale = ilog2(size2) / 10 * 10;
+       unsigned int size2_val = (10 * size2 + ((1ULL << size2_scale) >> 1)) >> size2_scale;

Can you put that expression into a function with a comment, etc.? It is a bit hard to understand.

Ok.

Quoted 5 lines
Quoted 5 lines
common/board_f.c +5 -0
+
+       if (size1_val == 10240) {
+               size1_val = 10;
+               size1_scale += 10;
+       }
Quoted 8 lines

common/board_f.c static int announce_dram_init(void) · line 236

Quoted 5 lines
common/board_f.c +5 -0
+
+       if (size2_val == 10240) {
+               size2_val = 10;
+               size2_scale += 10;
+       }

If you are doing the same thing to each, why bother? It should not affect the expression below, should it? :

This is interesting question, and the answer it that it is required and affects comparison expression below. For example for the case when size1 is below 1GB limit, size2 is above 1GB limit and both values are near. Imagine that size1 is approaching value 1GB from the left and size2 from the right side.

Quoted 27 lines
Quoted 21 lines
common/board_f.c +9 -2
+
+       return size1_scale == size2_scale && size1_val == size2_val;
+}
+
 static int show_dram_config(void)
 {
        unsigned long long size;
@@ -229,7 +254,11 @@ static int show_dram_config(void)
        }
        debug("\nDRAM:  ");
 
-       print_size(size, "");
+       print_size(gd->ram_size, "");
+       if (!sizes_near(gd->ram_size, size)) {
+               printf(" (effective ");
+               print_size(size, ")");
+       }
        board_add_ram_info(0);
        putc('\n');
 
--
Quoted 2 lines

2.20.1

Can we make this testable somehow? You could put the new code into a lib/ function, perhaps, and call it from a C unit test in test/lib ?

Quoted 7 lines
Quoted 2 lines

Regards,
Simon

Meh... I do not know how to test such code. Due to size / optimization requirements it is not a good idea to make function outside of board_f.c file.

You can use TEST_STATIC from test/export.h this case.

--Sean

#4 lore raw

Hi Pali,

Quoted 106 lines · Sean Anderson
Quoted 102 lines · Pali Rohár
Quoted 35 lines · On Monday 12 September 2022 07:34:47 Simon Glass wrote:

Hi Pali,

Quoted 32 lines · Pali Rohár

32-bit U-Boot builds cannot use more than around 2 GB of DDR memory. But on some platforms/boards it is possible to connect also 4 GB SODIMM DDR memory. U-Boot currently prints only effective size of RAM which can use, which may be misleading as somebody would expect that this line prints total size of connected DDR modules. So change show_dram_config code to prints both real and effective DRAM size if they are different. If they are same then print just one number like before. It is possible that effective size is just few bytes smaller than the real size, so print both numbers only in case function print_size() prints formats them differently.

---
 common/board_f.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
common/board_f.c +1 -0
index 9e34fbee147e..3131a06db940 100644
@@ -54,6 +54,7 @@
 #include <asm/sections.h>
 #include <dm/root.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
 /*
  * Pointer to initial global data area
@@ -213,6 +214,30 @@ static int announce_dram_init(void)
        return 0;
 }
 
Quoted 13 lines

common/board_f.c static int announce_dram_init(void) · line 226

Quoted 10 lines
common/board_f.c +10 -0
+/*
+ * Check if the sizes in their natural units written in decimal format with
+ * one fraction number are same.
+ */
+static int sizes_near(unsigned long long size1, unsigned long long size2)
+{
+       unsigned int size1_scale = ilog2(size1) / 10 * 10;
+       unsigned int size1_val = (10 * size1 + ((1ULL << size1_scale) >> 1)) >> size1_scale;
+       unsigned int size2_scale = ilog2(size2) / 10 * 10;
+       unsigned int size2_val = (10 * size2 + ((1ULL << size2_scale) >> 1)) >> size2_scale;

Can you put that expression into a function with a comment, etc.? It is a bit hard to understand.

Ok.

Quoted 5 lines
Quoted 5 lines
common/board_f.c +5 -0
+
+       if (size1_val == 10240) {
+               size1_val = 10;
+               size1_scale += 10;
+       }
Quoted 8 lines

common/board_f.c static int announce_dram_init(void) · line 236

Quoted 5 lines
common/board_f.c +5 -0
+
+       if (size2_val == 10240) {
+               size2_val = 10;
+               size2_scale += 10;
+       }

If you are doing the same thing to each, why bother? It should not affect the expression below, should it? :

This is interesting question, and the answer it that it is required and affects comparison expression below. For example for the case when size1 is below 1GB limit, size2 is above 1GB limit and both values are near. Imagine that size1 is approaching value 1GB from the left and size2 from the right side.

Quoted 29 lines
Quoted 21 lines
common/board_f.c +9 -2
+
+       return size1_scale == size2_scale && size1_val == size2_val;
+}
+
 static int show_dram_config(void)
 {
        unsigned long long size;
@@ -229,7 +254,11 @@ static int show_dram_config(void)
        }
        debug("\nDRAM:  ");
 
-       print_size(size, "");
+       print_size(gd->ram_size, "");
+       if (!sizes_near(gd->ram_size, size)) {
+               printf(" (effective ");
+               print_size(size, ")");
+       }
        board_add_ram_info(0);
        putc('\n');
 
--
Quoted 2 lines

2.20.1

Can we make this testable somehow? You could put the new code into a lib/ function, perhaps, and call it from a C unit test in test/lib ?

Regards,
Simon

Quoted 6 lines
Quoted 4 lines

Meh... I do not know how to test such code. Due to size / optimization requirements it is not a good idea to make function outside of board_f.c file.

You can use TEST_STATIC from test/export.h this case.

Good point, Sean.

Also LTO take away most//all of the cost of making a static function global.

Regards,
Simon

#5 lore raw
Quoted 1 line · On Wednesday 14 September 2022 11:10:20 Simon Glass wrote:

LTO take away most//all of the cost of making a static function global.

This is not solution as LTO does not work on powerpc and as I figured today, it is broken also on some 32-bit ARM platforms (e.g. mvebu).

#6 lore raw

Hi Pali,

Quoted 6 lines · Pali Rohár
Quoted 1 line · On Wednesday 14 September 2022 11:10:20 Simon Glass wrote:

LTO take away most//all of the cost of making a static function global.

This is not solution as LTO does not work on powerpc and as I figured today, it is broken also on some 32-bit ARM platforms (e.g. mvebu).

Yes, various platforms need debugging of particular issues, as I understand it.

Anyway, Sean's solution should work fine and doesn't need LTO.

- Simon

#7 lore raw
In reply to Sean Anderson
Quoted 1 line · On Monday 12 September 2022 17:58:09 Sean Anderson wrote:

You can use TEST_STATIC from test/export.h this case.

Now I sent new patch version where I extended comment.

I have still issue with test framework. Could you help me how to write that unit test and run it? Scenario should really simple, just check like this:

  sizes_near(87654321, 87654320) == true;
  sizes_near(87654321, 1000) == false;
#8 lore raw

Hi Pali,

Quoted 4 lines · Pali Rohár
Quoted 1 line · On Monday 12 September 2022 17:58:09 Sean Anderson wrote:

You can use TEST_STATIC from test/export.h this case.

Quoted 8 lines

Now I sent new patch version where I extended comment.

I have still issue with test framework. Could you help me how to write that unit test and run it? Scenario should really simple, just check like this:

  sizes_near(87654321, 87654320) == true;
  sizes_near(87654321, 1000) == false;

I suggest copying something like test/lib/sscanf.c and then you can run it in sandbox with 'ut lib <testname>'

Regards,
Simon

#9 lore raw

PATCH v2 1/1 board_f: show_dram_config: Print also real DRAM size

In reply to Pali Rohár

32-bit U-Boot builds cannot use more than around 2 GB of DDR memory. But on some platforms/boards it is possible to connect also 4 GB SODIMM DDR memory. U-Boot currently prints only effective size of RAM which can use, which may be misleading as somebody would expect that this line prints total size of connected DDR modules. So change show_dram_config code to prints both real and effective DRAM size if they are different. If they are same then print just one number like before. It is possible that effective size is just few bytes smaller than the real size, so print both numbers only in case function print_size() prints formats them differently.

---
Changes in v2:
* Move calculation code into separate macro and add description of it

---
 common/board_f.c | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
common/board_f.c +36 -1
index 9e34fbee147e..88a6dfff03fc 100644
@@ -54,6 +54,7 @@
 #include <asm/sections.h>
 #include <dm/root.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
 /*
  * Pointer to initial global data area
@@ -213,6 +214,36 @@ static int announce_dram_init(void)
 	return 0;
 }
 
+/*
+ * From input size calculate its nearest rounded unit scale (multiply of 2^10)
+ * and value in calculated unit scale multiplied by 10 (as fractional fixed
+ * point number with one decimal digit), which is human natural format,
+ * same what uses print_size() function for displaying. Mathematically it is:
+ * round_nearest(val * 2^scale) = size * 10; where: 10 <= val < 10240.
+ *
+ * For example for size=87654321 we calculate scale=20 and val=836 which means
+ * that input has natural human format 83.6 M (mega = 2^20).
+ */
+#define compute_size_scale_val(size, scale, val) do { \
+	scale = ilog2(size) / 10 * 10; \
+	val = (10 * size + ((1ULL << scale) >> 1)) >> scale; \
+	if (val == 10240) { val = 10; scale += 10; } \
+} while (0)
+
+/*
+ * Check if the sizes in their natural units written in decimal format with
+ * one fraction number are same.
+ */
+static int sizes_near(unsigned long long size1, unsigned long long size2)
+{
+	unsigned int size1_scale, size1_val, size2_scale, size2_val;
+
+	compute_size_scale_val(size1, size1_scale, size1_val);
+	compute_size_scale_val(size2, size2_scale, size2_val);
+
+	return size1_scale == size2_scale && size1_val == size2_val;
+}
+
 static int show_dram_config(void)
 {
 	unsigned long long size;
@@ -229,7 +260,11 @@ static int show_dram_config(void)
 	}
 	debug("\nDRAM:  ");
 
-	print_size(size, "");
+	print_size(gd->ram_size, "");
+	if (!sizes_near(gd->ram_size, size)) {
+		printf(" (effective ");
+		print_size(size, ")");
+	}
 	board_add_ram_info(0);
 	putc('\n');
 
Signature · 1 line
2.20.1
#10 lore raw
Quoted 11 lines · Pali Rohár

32-bit U-Boot builds cannot use more than around 2 GB of DDR memory. But on some platforms/boards it is possible to connect also 4 GB SODIMM DDR memory. U-Boot currently prints only effective size of RAM which can use, which may be misleading as somebody would expect that this line prints total size of connected DDR modules. So change show_dram_config code to prints both real and effective DRAM size if they are different. If they are same then print just one number like before. It is possible that effective size is just few bytes smaller than the real size, so print both numbers only in case function print_size() prints formats them differently.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom