Skip to contentSkip to conversation

MIPS: BCM47XX: Get rid of calls to KSEG1ADDR in nvram

PATCH Superseded Rafał Miłecki 2 participants 3 messages Sep 1, 2014 – Sep 3, 2014

Subsystems (2)
lore
Share & tools
lore

Rafał Miłecki

author maintainer
#0 lore raw

PATCH 1/1 MIPS: BCM47XX: Get rid of calls to KSEG1ADDR in nvram

We should be using ioremap_nocache helper which handles remaps in a smarter way.

---
 arch/mips/bcm47xx/nvram.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
arch/mips/bcm47xx/nvram.c +25 -10
index 2bed73a..2f0a646 100644
@@ -23,13 +23,13 @@
 static char nvram_buf[NVRAM_SPACE];
 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
 
-static u32 find_nvram_size(u32 end)
+static u32 find_nvram_size(void __iomem *end)
 {
 	struct nvram_header *header;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
-		header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
+		header = (struct nvram_header *)(end - nvram_sizes[i]);
 		if (header->magic == NVRAM_HEADER)
 			return nvram_sizes[i];
 	}
@@ -38,7 +38,7 @@ static u32 find_nvram_size(u32 end)
 }
 
 /* Probe for NVRAM header */
-static int nvram_find_and_copy(u32 base, u32 lim)
+static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
 {
 	struct nvram_header *header;
 	int i;
@@ -46,27 +46,31 @@ static int nvram_find_and_copy(u32 base, u32 lim)
 	u32 *src, *dst;
 	u32 size;
 
+	if (nvram_buf[0]) {
+		pr_warn("nvram already initialized\n");
+		return -EEXIST;
+	}
+
 	/* TODO: when nvram is on nand flash check for bad blocks first. */
 	off = FLASH_MIN;
 	while (off <= lim) {
 		/* Windowed flash access */
-		size = find_nvram_size(base + off);
+		size = find_nvram_size(iobase + off);
 		if (size) {
-			header = (struct nvram_header *)KSEG1ADDR(base + off -
-								  size);
+			header = (struct nvram_header *)(iobase + off - size);
 			goto found;
 		}
 		off <<= 1;
 	}
 
 	/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
-	header = (struct nvram_header *) KSEG1ADDR(base + 4096);
+	header = (struct nvram_header *)(iobase + 4096);
 	if (header->magic == NVRAM_HEADER) {
 		size = NVRAM_SPACE;
 		goto found;
 	}
 
-	header = (struct nvram_header *) KSEG1ADDR(base + 1024);
+	header = (struct nvram_header *)(iobase + 1024);
 	if (header->magic == NVRAM_HEADER) {
 		size = NVRAM_SPACE;
 		goto found;
@@ -94,6 +98,17 @@ found:
 	return 0;
 }
 
+static int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
+{
+	void __iomem *iobase;
+
+	iobase = ioremap_nocache(base, lim);
+	if (!iobase)
+		return -ENOMEM;
+
+	return nvram_find_and_copy(iobase, lim);
+}
+
 #ifdef CONFIG_BCM47XX_SSB
 static int nvram_init_ssb(void)
 {
@@ -109,7 +124,7 @@ static int nvram_init_ssb(void)
 		return -ENXIO;
 	}
 
-	return nvram_find_and_copy(base, lim);
+	return bcm47xx_nvram_init_from_mem(base, lim);
 }
 #endif
 
@@ -139,7 +154,7 @@ static int nvram_init_bcma(void)
 		return -ENXIO;
 	}
 
-	return nvram_find_and_copy(base, lim);
+	return bcm47xx_nvram_init_from_mem(base, lim);
 }
 #endif
 
Signature · 1 line
1.8.4.5
maintainer
#1 lore raw
Quoted 2 lines · Rafał Miłecki

We should be using ioremap_nocache helper which handles remaps in a smarter way.

This is a good idea.

I just checked this with sparse and it still finds some places where you cast a var annotated with __iomem to a var without this annotation.

hauke@hauke-desktop:~/linux/linux-next$ ionice -c 3 nice -n 20 make
ARCH=mips CROSS_COMPILE=mipsel-openwrt-linux-uclibc- C=2 arch/mips/bcm47xx/
.....
  CHECK   arch/mips/bcm47xx/nvram.c
arch/mips/bcm47xx/nvram.c:32:27: warning: cast removes address space of
expression
arch/mips/bcm47xx/nvram.c:60:35: warning: cast removes address space of
expression
arch/mips/bcm47xx/nvram.c:67:19: warning: cast removes address space of
expression
arch/mips/bcm47xx/nvram.c:73:19: warning: cast removes address space of
expression
  CC      arch/mips/bcm47xx/nvram.o
Quoted 13 lines
---
 arch/mips/bcm47xx/nvram.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
arch/mips/bcm47xx/nvram.c +0 -0
index 2bed73a..2f0a646 100644
@@ -23,13 +23,13 @@
 static char nvram_buf[NVRAM_SPACE];
 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};

arch/mips/bcm47xx/nvram.c line 32

Quoted 10 lines
arch/mips/bcm47xx/nvram.c +2 -2
 
-static u32 find_nvram_size(u32 end)
+static u32 find_nvram_size(void __iomem *end)
 {
 	struct nvram_header *header;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
-		header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
+		header = (struct nvram_header *)(end - nvram_sizes[i]);

__iomem annotation gets lost

Quoted 21 lines
arch/mips/bcm47xx/nvram.c +6 -1
 		if (header->magic == NVRAM_HEADER)
 			return nvram_sizes[i];
 	}
@@ -38,7 +38,7 @@ static u32 find_nvram_size(u32 end)
 }
 
 /* Probe for NVRAM header */
-static int nvram_find_and_copy(u32 base, u32 lim)
+static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
 {
 	struct nvram_header *header;
 	int i;
@@ -46,27 +46,31 @@ static int nvram_find_and_copy(u32 base, u32 lim)
 	u32 *src, *dst;
 	u32 size;
 
+	if (nvram_buf[0]) {
+		pr_warn("nvram already initialized\n");
+		return -EEXIST;
+	}
+

arch/mips/bcm47xx/nvram.c static int nvram_find_and_copy(u32 base, u32 lim) · line 60

Quoted 10 lines
arch/mips/bcm47xx/nvram.c +2 -3
 	/* TODO: when nvram is on nand flash check for bad blocks first. */
 	off = FLASH_MIN;
 	while (off <= lim) {
 		/* Windowed flash access */
-		size = find_nvram_size(base + off);
+		size = find_nvram_size(iobase + off);
 		if (size) {
-			header = (struct nvram_header *)KSEG1ADDR(base + off -
-								  size);
+			header = (struct nvram_header *)(iobase + off - size);

__iomem annotation gets lost

arch/mips/bcm47xx/nvram.c static int nvram_find_and_copy(u32 base, u32 lim) · line 67

Quoted 8 lines
arch/mips/bcm47xx/nvram.c +1 -1
 			goto found;
 		}
 		off <<= 1;
 	}
 
 	/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
-	header = (struct nvram_header *) KSEG1ADDR(base + 4096);
+	header = (struct nvram_header *)(iobase + 4096);

__iomem annotation gets lost

arch/mips/bcm47xx/nvram.c static int nvram_find_and_copy(u32 base, u32 lim) · line 73

Quoted 7 lines
arch/mips/bcm47xx/nvram.c +1 -1
 	if (header->magic == NVRAM_HEADER) {
 		size = NVRAM_SPACE;
 		goto found;
 	}
 
-	header = (struct nvram_header *) KSEG1ADDR(base + 1024);
+	header = (struct nvram_header *)(iobase + 1024);

__iomem annotation gets lost

Quoted 4 lines
arch/mips/bcm47xx/nvram.c +0 -0
 	if (header->magic == NVRAM_HEADER) {
 		size = NVRAM_SPACE;
 		goto found;

arch/mips/bcm47xx/nvram.c found: · line 107

Quoted 10 lines
arch/mips/bcm47xx/nvram.c +7 -0
@@ -94,6 +98,17 @@ found:
 	return 0;
 }
 
+static int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
+{
+	void __iomem *iobase;
+
+	iobase = ioremap_nocache(base, lim);
+	if (!iobase)
+		return -ENOMEM;

You should iounmap this sometime later, because the data is copied to nvram_buf and iobase is not accsses after is was passed to nvram_find_and_copy().

Quoted 26 lines
arch/mips/bcm47xx/nvram.c +6 -2
+
+	return nvram_find_and_copy(iobase, lim);
+}
+
 #ifdef CONFIG_BCM47XX_SSB
 static int nvram_init_ssb(void)
 {
@@ -109,7 +124,7 @@ static int nvram_init_ssb(void)
 		return -ENXIO;
 	}
 
-	return nvram_find_and_copy(base, lim);
+	return bcm47xx_nvram_init_from_mem(base, lim);
 }
 #endif
 
@@ -139,7 +154,7 @@ static int nvram_init_bcma(void)
 		return -ENXIO;
 	}
 
-	return nvram_find_and_copy(base, lim);
+	return bcm47xx_nvram_init_from_mem(base, lim);
 }
 #endif
 
author maintainer
#2 lore raw

PATCH v2 1/1 MIPS: BCM47XX: Get rid of calls to KSEG1ADDR

In reply to Rafał Miłecki

We should be using ioremap_nocache helper which handles remaps in a smarter way.

---
V2: Use __iomem for *header
iounmap when initialization is done
Thanks Hauke!

---
 arch/mips/bcm47xx/nvram.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
arch/mips/bcm47xx/nvram.c +32 -12
index 2bed73a..e07976b 100644
@@ -23,13 +23,13 @@
 static char nvram_buf[NVRAM_SPACE];
 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
 
-static u32 find_nvram_size(u32 end)
+static u32 find_nvram_size(void __iomem *end)
 {
-	struct nvram_header *header;
+	struct nvram_header __iomem *header;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
-		header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
+		header = (struct nvram_header *)(end - nvram_sizes[i]);
 		if (header->magic == NVRAM_HEADER)
 			return nvram_sizes[i];
 	}
@@ -38,35 +38,39 @@ static u32 find_nvram_size(u32 end)
 }
 
 /* Probe for NVRAM header */
-static int nvram_find_and_copy(u32 base, u32 lim)
+static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
 {
-	struct nvram_header *header;
+	struct nvram_header __iomem *header;
 	int i;
 	u32 off;
 	u32 *src, *dst;
 	u32 size;
 
+	if (nvram_buf[0]) {
+		pr_warn("nvram already initialized\n");
+		return -EEXIST;
+	}
+
 	/* TODO: when nvram is on nand flash check for bad blocks first. */
 	off = FLASH_MIN;
 	while (off <= lim) {
 		/* Windowed flash access */
-		size = find_nvram_size(base + off);
+		size = find_nvram_size(iobase + off);
 		if (size) {
-			header = (struct nvram_header *)KSEG1ADDR(base + off -
-								  size);
+			header = (struct nvram_header *)(iobase + off - size);
 			goto found;
 		}
 		off <<= 1;
 	}
 
 	/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
-	header = (struct nvram_header *) KSEG1ADDR(base + 4096);
+	header = (struct nvram_header *)(iobase + 4096);
 	if (header->magic == NVRAM_HEADER) {
 		size = NVRAM_SPACE;
 		goto found;
 	}
 
-	header = (struct nvram_header *) KSEG1ADDR(base + 1024);
+	header = (struct nvram_header *)(iobase + 1024);
 	if (header->magic == NVRAM_HEADER) {
 		size = NVRAM_SPACE;
 		goto found;
@@ -94,6 +98,22 @@ found:
 	return 0;
 }
 
+static int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
+{
+	void __iomem *iobase;
+	int err;
+
+	iobase = ioremap_nocache(base, lim);
+	if (!iobase)
+		return -ENOMEM;
+
+	err = nvram_find_and_copy(iobase, lim);
+
+	iounmap(iobase);
+
+	return err;
+}
+
 #ifdef CONFIG_BCM47XX_SSB
 static int nvram_init_ssb(void)
 {
@@ -109,7 +129,7 @@ static int nvram_init_ssb(void)
 		return -ENXIO;
 	}
 
-	return nvram_find_and_copy(base, lim);
+	return bcm47xx_nvram_init_from_mem(base, lim);
 }
 #endif
 
@@ -139,7 +159,7 @@ static int nvram_init_bcma(void)
 		return -ENXIO;
 	}
 
-	return nvram_find_and_copy(base, lim);
+	return bcm47xx_nvram_init_from_mem(base, lim);
 }
 #endif
 
Signature · 1 line
1.8.4.5