Abstract mmap() in physmap.c and only open /dev/mem on the first physmap() call

Corresponding to flashrom svn r397 and coreboot v2 svn r3903.

Signed-off-by: Stefan Reinauer <stepan@coresystems.de>
Signed-off-by: Peter Stuge <peter@stuge.se>
Acked-by: Peter Stuge <peter@stuge.se>
diff --git a/Makefile b/Makefile
index 940f346..d46600b 100644
--- a/Makefile
+++ b/Makefile
@@ -27,7 +27,7 @@
 OBJS = chipset_enable.o board_enable.o udelay.o jedec.o stm50flw0x0x.o \
 	sst28sf040.o am29f040b.o mx29f002.o sst39sf020.o m29f400bt.o \
 	w49f002u.o 82802ab.o pm49fl00x.o sst49lf040.o en29f002a.o \
-	sst49lfxxxc.o sst_fwhub.o layout.o cbtable.o flashchips.o \
+	sst49lfxxxc.o sst_fwhub.o layout.o cbtable.o flashchips.o physmap.o \
 	flashrom.o w39v080fa.o sharplhf00l04.o w29ee011.o spi.o it87spi.o \
 	ichspi.o w39v040c.o sb600spi.o
 
diff --git a/cbtable.c b/cbtable.c
index 25c3ee2..284140a 100644
--- a/cbtable.c
+++ b/cbtable.c
@@ -188,13 +188,7 @@
 	struct lb_header *lb_table;
 	struct lb_record *rec, *last;
 
-	low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem,
-		       0x00000000);
-	if (low_1MB == MAP_FAILED) {
-		perror("Can't mmap memory using " MEM_DEV);
-		mmap_errmsg();
-		exit(-2);
-	}
+	low_1MB = physmap("low megabyte", 0x0, 1024*1024);
 	lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
 	if (!lb_table)
 		lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
diff --git a/chipset_enable.c b/chipset_enable.c
index d7eb7fa..7298729 100644
--- a/chipset_enable.c
+++ b/chipset_enable.c
@@ -215,14 +215,7 @@
 
 	mmio_base = (pci_read_long(dev, 0xbc)) << 8;
 	printf_debug("MMIO base at = 0x%x\n", mmio_base);
-	spibar = mmap(NULL, 0x70, PROT_READ | PROT_WRITE, MAP_SHARED,
-		      fd_mem, mmio_base);
-
-	if (spibar == MAP_FAILED) {
-		perror("Can't mmap memory using " MEM_DEV);
-		mmap_errmsg();
-		exit(1);
-	}
+	spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
 
 	printf_debug("0x6c: 0x%04x     (CLOCK/DEBUG)\n",
 		     *(uint16_t *) (spibar + 0x6c));
@@ -252,13 +245,7 @@
 	printf_debug("\nRoot Complex Register Block address = 0x%x\n", tmp);
 
 	/* Map RCBA to virtual memory */
-	rcrb = mmap(0, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd_mem,
-		    (off_t) tmp);
-	if (rcrb == MAP_FAILED) {
-		perror("Can't mmap memory using " MEM_DEV);
-		mmap_errmsg();
-		exit(1);
-	}
+	rcrb = physmap("ICH RCRB", tmp, 0x4000);
 
 	gcs = *(volatile uint32_t *)(rcrb + 0x3410);
 	printf_debug("GCS = 0x%x: ", gcs);
@@ -679,13 +666,7 @@
 	tmp &= 0xffffc000;
 	printf_debug("SPI base address is at 0x%x\n", tmp + low_bits);
 
-	sb600_spibar = mmap(0, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED,
-			    fd_mem, (off_t)tmp);
-	if (sb600_spibar == MAP_FAILED) {
-		perror("Can't mmap memory using " MEM_DEV);
-		mmap_errmsg();
-		exit(1);
-	}
+	sb600_spibar = physmap("SB600 SPI registers", tmp, 0x4000);
 	sb600_spibar += low_bits;
 
 	/* Clear ROM protect 0-3. */
@@ -835,14 +816,7 @@
 	void *mmcr;
 
 	/* 1. Map MMCR */
-	mmcr = mmap(0, getpagesize(), PROT_WRITE | PROT_READ,
-			MAP_SHARED, fd_mem, (off_t)0xFFFEF000);
-
-	if (mmcr == MAP_FAILED) {
-		perror("Can't mmap Elan SC520 specific registers using " MEM_DEV);
-		mmap_errmsg();
-		exit(1);
-	}
+	mmcr = physmap("Elan SC520 MMCR", 0xfffef000, getpagesize());
 
 	/* 2. Scan PAR0 (0x88) - PAR15 (0xc4) for
 	 *    BOOTCS region (PARx[31:29] = 100b)e
diff --git a/flash.h b/flash.h
index a2ff912..bad3110 100644
--- a/flash.h
+++ b/flash.h
@@ -466,21 +466,15 @@
 extern flashbus_t flashbus;
 extern void *spibar;
 
-/* Physical memory mapping device */
-#if defined (__sun) && (defined(__i386) || defined(__amd64))
-#  define MEM_DEV "/dev/xsvc"
-#else
-#  define MEM_DEV "/dev/mem"
-#endif
-
-extern int fd_mem;
-
 /* debug.c */
 extern int verbose;
 #define printf_debug(x...) { if (verbose) printf(x); }
 
+/* physmap.c */
+void *physmap(const char *descr, unsigned long phys_addr, size_t len);
+void physunmap(void *virt_addr, size_t len);
+
 /* flashrom.c */
-void mmap_errmsg();
 void map_flash_registers(struct flashchip *flash);
 
 /* layout.c */
diff --git a/flashrom.c b/flashrom.c
index eb0d8e0..5d0f1ed 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -22,7 +22,6 @@
 
 #include <errno.h>
 #include <fcntl.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -44,7 +43,6 @@
 struct pci_access *pacc;	/* For board and chipset_enable */
 int exclude_start_page, exclude_end_page;
 int verbose = 0;
-int fd_mem;
 
 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
 {
@@ -84,31 +82,10 @@
 	return NULL;
 }
 
-void mmap_errmsg()
-{
-	if (EINVAL == errno) {
-		fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
-		fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
-		fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
-		fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
-		fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
-	}
-}
-
 void map_flash_registers(struct flashchip *flash)
 {
-	volatile uint8_t *registers;
 	size_t size = flash->total_size * 1024;
-
-	registers = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
-			 fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
-
-	if (registers == MAP_FAILED) {
-		perror("Can't mmap registers using " MEM_DEV);
-		mmap_errmsg();
-		exit(1);
-	}
-	flash->virtual_registers = registers;
+	flash->virtual_registers = physmap("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
 }
 
 struct flashchip *probe_flash(struct flashchip *first_flash, int force)
@@ -145,14 +122,7 @@
 		}
 
 		base = flashbase ? flashbase : (0xffffffff - size + 1);
-		bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
-			    fd_mem, (off_t) base);
-		if (bios == MAP_FAILED) {
-			perror("Can't mmap memory using " MEM_DEV);
-			mmap_errmsg();
-			exit(1);
-		}
-		flash->virtual_memory = bios;
+		flash->virtual_memory = bios = physmap("flash chip", base, size);
 
 		if (force)
 			break;
@@ -165,7 +135,7 @@
 			break;
 
 notfound:
-		munmap((void *)bios, size);
+		physunmap((void *)bios, size);
 	}
 
 	if (!flash || !flash->name)
@@ -451,13 +421,6 @@
 	pci_init(pacc);		/* Initialize the PCI library */
 	pci_scan_bus(pacc);	/* We want to get the list of devices */
 
-	/* Open the memory device UNCACHED. That's important for MMIO. */
-	if ((fd_mem = open(MEM_DEV, O_RDWR | O_SYNC)) < 0) {
-		perror("Error: Can not access memory using " MEM_DEV
-		       ". You need to be root.");
-		exit(1);
-	}
-
 	myusec_calibrate_delay();
 
 	/* We look at the lbtable first to see if we need a
diff --git a/physmap.c b/physmap.c
new file mode 100644
index 0000000..65d2569
--- /dev/null
+++ b/physmap.c
@@ -0,0 +1,58 @@
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "flash.h"
+
+#if defined (__sun) && (defined(__i386) || defined(__amd64))
+#  define MEM_DEV "/dev/xsvc"
+#else
+#  define MEM_DEV "/dev/mem"
+#endif
+
+static int fd_mem = -1;
+
+void *sys_physmap(unsigned long phys_addr, size_t len)
+{
+	void *virt_addr;
+
+	if (-1 == fd_mem) {
+		/* Open the memory device UNCACHED. Important for MMIO. */
+		if (-1 == (fd_mem = open(MEM_DEV, O_RDWR|O_SYNC))) {
+			perror("Critical error: open(" MEM_DEV ")");
+			exit(1);
+		}
+	}
+
+	virt_addr = mmap(0, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr);
+	return MAP_FAILED == virt_addr ? NULL : virt_addr;
+}
+
+void physunmap(void *virt_addr, size_t len)
+{
+	munmap(virt_addr, len);
+}
+
+void *physmap(const char *descr, unsigned long phys_addr, size_t len)
+{
+	void *virt_addr = sys_physmap(phys_addr, len);
+
+	if (NULL == virt_addr) {
+		if (NULL == descr)
+			descr = "memory";
+		fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
+		perror(MEM_DEV " mmap failed");
+		if (EINVAL == errno) {
+			fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
+			fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
+			fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
+			fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
+			fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
+		}
+		exit(1);
+	}
+
+	return virt_addr;
+}