EDAC: simplify total memory calculation

6 messages, 3 authors, 2017-06-14 · open the first message on its own page

EDAC: simplify total memory calculation

From: Chris Packham <chris.packham@alliedtelesis.co.nz>
Date: 2017-06-06 23:55:23

This take the approach used by cell_edac.c to obtain the total memory from
the devicetree and applies it to mv64x60_edac.c, altera_edac.c and
cpc925_edac.c which were all manually parsing the reg property. In the case
of mv64x60 this actually fixes cases where #address/size-cells != 1. For
altera and cpc925 this is just a cleanup.

Chris Packham (3):
  EDAC: mv64x60: calculate memory size correctly
  EDAC: altera: simplify calculation of total memory
  EDAC: cpc925: simplify calculation of total memory

 drivers/edac/altera_edac.c  | 24 ++++++++----------------
 drivers/edac/cpc925_edac.c  | 32 ++++++++++----------------------
 drivers/edac/mv64x60_edac.c | 17 +++++++++++------
 3 files changed, 29 insertions(+), 44 deletions(-)

-- 
2.13.0

[PATCH v2 1/3] EDAC: mv64x60: calculate memory size correctly

From: Chris Packham <chris.packham@alliedtelesis.co.nz>
Date: 2017-06-06 23:55:24

The #address-cells and #size-cells properties need to be accounted for
when dealing with the "memory" device tree node. Use
of_address_to_resource() and resource_size() to retrieve the size of the
memory node which will automatically take the #cells into account.

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
Changes in v2:
- Use of_address_to_resource() instead of manually parsing the reg property.

 drivers/edac/mv64x60_edac.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/drivers/edac/mv64x60_edac.c b/drivers/edac/mv64x60_edac.c
index 3461db3723cb..93623e704623 100644
--- a/drivers/edac/mv64x60_edac.c
+++ b/drivers/edac/mv64x60_edac.c
@@ -16,6 +16,7 @@
 #include <linux/io.h>
 #include <linux/edac.h>
 #include <linux/gfp.h>
+#include <linux/of_address.h>
 #include <linux/of_platform.h>
 #include <linux/of_device.h>
 
@@ -644,15 +645,19 @@ static irqreturn_t mv64x60_mc_isr(int irq, void *dev_id)
 static void get_total_mem(struct mv64x60_mc_pdata *pdata)
 {
 	struct device_node *np = NULL;
-	const unsigned int *reg;
+	struct resource res;
+	int ret;
+	unsigned long total_mem = 0;
 
-	np = of_find_node_by_type(NULL, "memory");
-	if (!np)
-		return;
+	for_each_node_by_type(np, "memory") {
+		ret = of_address_to_resource(np, 0, &res);
+		if (ret)
+			continue;
 
-	reg = of_get_property(np, "reg", NULL);
+		total_mem += resource_size(&res);
+	}
 
-	pdata->total_mem = reg[1];
+	pdata->total_mem = total_mem;
 }
 
 static void mv64x60_init_csrows(struct mem_ctl_info *mci,
-- 
2.13.0

[PATCH v2 3/3] EDAC: cpc925: simplify calculation of total memory

From: Chris Packham <chris.packham@alliedtelesis.co.nz>
Date: 2017-06-06 23:55:25

Use of_address_to_resource() and resource_size() instead of manually
parsing the "reg" property from the "memory" node(s).

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
Changes in v2:
- New

 drivers/edac/cpc925_edac.c | 32 ++++++++++----------------------
 1 file changed, 10 insertions(+), 22 deletions(-)
diff --git a/drivers/edac/cpc925_edac.c b/drivers/edac/cpc925_edac.c
index 837b62c4993d..ea347cd7eb92 100644
--- a/drivers/edac/cpc925_edac.c
+++ b/drivers/edac/cpc925_edac.c
@@ -24,6 +24,7 @@
 #include <linux/io.h>
 #include <linux/edac.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/platform_device.h>
 #include <linux/gfp.h>
 
@@ -296,30 +297,17 @@ struct cpc925_dev_info {
 static void get_total_mem(struct cpc925_mc_pdata *pdata)
 {
 	struct device_node *np = NULL;
-	const unsigned int *reg, *reg_end;
-	int len, sw, aw;
-	unsigned long start, size;
+	struct resource res;
+	int ret;
 
-	np = of_find_node_by_type(NULL, "memory");
-	if (!np)
-		return;
+	for_each_node_by_type(np, "memory") {
+		ret = of_address_to_resource(np, 0, &res);
+		if (ret)
+			continue;
+
+		pdata->total_mem += resource_size(&res);
+	}
 
-	aw = of_n_addr_cells(np);
-	sw = of_n_size_cells(np);
-	reg = (const unsigned int *)of_get_property(np, "reg", &len);
-	reg_end = reg + len/4;
-
-	pdata->total_mem = 0;
-	do {
-		start = of_read_number(reg, aw);
-		reg += aw;
-		size = of_read_number(reg, sw);
-		reg += sw;
-		edac_dbg(1, "start 0x%lx, size 0x%lx\n", start, size);
-		pdata->total_mem += size;
-	} while (reg < reg_end);
-
-	of_node_put(np);
 	edac_dbg(0, "total_mem 0x%lx\n", pdata->total_mem);
 }
 
-- 
2.13.0

[PATCH v2 2/3] EDAC: altera: simplify calculation of total memory

From: Chris Packham <chris.packham@alliedtelesis.co.nz>
Date: 2017-06-06 23:55:57

Use of_address_to_resource() and resource_size() instead of manually
parsing the "reg" property from the "memory" node(s).

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
Changes in v2:
- New

 drivers/edac/altera_edac.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)
diff --git a/drivers/edac/altera_edac.c b/drivers/edac/altera_edac.c
index 7717b094fabb..f8b623352627 100644
--- a/drivers/edac/altera_edac.c
+++ b/drivers/edac/altera_edac.c
@@ -214,24 +214,16 @@ static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
 static unsigned long get_total_mem(void)
 {
 	struct device_node *np = NULL;
-	const unsigned int *reg, *reg_end;
-	int len, sw, aw;
-	unsigned long start, size, total_mem = 0;
+	struct resource res;
+	int ret;
+	unsigned long total_mem = 0;
 
 	for_each_node_by_type(np, "memory") {
-		aw = of_n_addr_cells(np);
-		sw = of_n_size_cells(np);
-		reg = (const unsigned int *)of_get_property(np, "reg", &len);
-		reg_end = reg + (len / sizeof(u32));
-
-		total_mem = 0;
-		do {
-			start = of_read_number(reg, aw);
-			reg += aw;
-			size = of_read_number(reg, sw);
-			reg += sw;
-			total_mem += size;
-		} while (reg < reg_end);
+		ret = of_address_to_resource(np, 0, &res);
+		if (ret)
+			continue;
+
+		total_mem += resource_size(&res);
 	}
 	edac_dbg(0, "total_mem 0x%lx\n", total_mem);
 	return total_mem;
-- 
2.13.0

Re: [PATCH v2 2/3] EDAC: altera: simplify calculation of total memory

From: Thor Thayer <hidden>
Date: 2017-06-12 18:30:50

On 06/06/2017 06:54 PM, Chris Packham wrote:
quoted hunk
Use of_address_to_resource() and resource_size() instead of manually
parsing the "reg" property from the "memory" node(s).

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
Changes in v2:
- New

  drivers/edac/altera_edac.c | 24 ++++++++----------------
  1 file changed, 8 insertions(+), 16 deletions(-)
diff --git a/drivers/edac/altera_edac.c b/drivers/edac/altera_edac.c
index 7717b094fabb..f8b623352627 100644
--- a/drivers/edac/altera_edac.c
+++ b/drivers/edac/altera_edac.c
@@ -214,24 +214,16 @@ static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
  static unsigned long get_total_mem(void)
  {
  	struct device_node *np = NULL;
-	const unsigned int *reg, *reg_end;
-	int len, sw, aw;
-	unsigned long start, size, total_mem = 0;
+	struct resource res;
+	int ret;
+	unsigned long total_mem = 0;
  
  	for_each_node_by_type(np, "memory") {
-		aw = of_n_addr_cells(np);
-		sw = of_n_size_cells(np);
-		reg = (const unsigned int *)of_get_property(np, "reg", &len);
-		reg_end = reg + (len / sizeof(u32));
-
-		total_mem = 0;
-		do {
-			start = of_read_number(reg, aw);
-			reg += aw;
-			size = of_read_number(reg, sw);
-			reg += sw;
-			total_mem += size;
-		} while (reg < reg_end);
+		ret = of_address_to_resource(np, 0, &res);
+		if (ret)
+			continue;
+
+		total_mem += resource_size(&res);
  	}
  	edac_dbg(0, "total_mem 0x%lx\n", total_mem);
  	return total_mem;
Nice change! Tested on Cyclone5 DevKit & Arria10 DevKit.

Tested-by: Thor Thayer <redacted>

Re: [PATCH v2 2/3] EDAC: altera: simplify calculation of total memory

From: Borislav Petkov <bp@alien8.de>
Date: 2017-06-14 11:50:17

On Mon, Jun 12, 2017 at 01:34:05PM -0500, Thor Thayer wrote:
On 06/06/2017 06:54 PM, Chris Packham wrote:
quoted
Use of_address_to_resource() and resource_size() instead of manually
parsing the "reg" property from the "memory" node(s).

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
...
Nice change! Tested on Cyclone5 DevKit & Arria10 DevKit.

Tested-by: Thor Thayer <redacted>
Applied, thanks.

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help