posix file: Let the OS decide addresses to map to

Reverse the direction of the `Addr` parameter of Map(). We let mmap()
decide which address to use and return that. Given that we can set the
address for `MMIO_Range` instances during runtime, we shouldn't ever
have to force static addresses.

Change-Id: I35bfd65e7b471daae4f43a9233ce613c7f45891e
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/20552
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/ada/posix/hw-file.adb b/ada/posix/hw-file.adb
index 40b445d..d9b15ba 100644
--- a/ada/posix/hw-file.adb
+++ b/ada/posix/hw-file.adb
@@ -26,8 +26,8 @@
    WRITE : constant := 16#02#;
 
    function c_map
-     (path  : chars_ptr;
-      addr  : Word64;
+     (addr  : out Word64;
+      path  : chars_ptr;
       len   : Word32;
       mode  : Word32;
       copy  : int)
@@ -35,8 +35,8 @@
    pragma Import (C, c_map, "hw_file_map");
 
    procedure Map
-     (Path     : in     String;
-      Addr     : in     Word64;
+     (Addr     :    out Word64;
+      Path     : in     String;
       Len      : in     Natural;
       Readable : in     Boolean := False;
       Writable : in     Boolean := False;
@@ -47,8 +47,8 @@
 
       cpath : chars_ptr := New_String (Path);
       ret : constant int := c_map
-        (path  => cpath,
-         addr  => Addr,
+        (addr  => Addr,
+         path  => cpath,
          len   => Word32 (Len),
          mode  => (if Readable then READ else 0) or
                   (if Writable then WRITE else 0),
diff --git a/c/hw-file.c b/c/hw-file.c
index 4d756ea..7a9478d 100644
--- a/c/hw-file.c
+++ b/c/hw-file.c
@@ -21,8 +21,8 @@
 #define HW_FILE_READ	0x01
 #define HW_FILE_WRITE	0x02
 
-static int map_file(const char *const path,
-		    const uint64_t addr,
+static int map_file(uint64_t *const addr,
+		    const char *const path,
 		    const uint32_t len,
 		    const uint32_t mode)
 {
@@ -37,17 +37,17 @@
 	if (fd < 0)
 		return errno;
 
-	void *const mapped = mmap((void *)(uintptr_t)addr, len, prot,
-				  MAP_SHARED | MAP_FIXED, fd, (off_t)0);
+	void *const mapped = mmap(NULL, len, prot, MAP_SHARED, fd, (off_t)0);
 	close(fd);
 	if (mapped == MAP_FAILED)
 		return errno;
 
+	*addr = (uint64_t)(uintptr_t)mapped;
 	return 0;
 }
 
-static int map_fill_from_file(const char *const path,
-			      const uint64_t addr,
+static int map_fill_from_file(uint64_t *const addr,
+			      const char *const path,
 			      const uint32_t len,
 			      const uint32_t mode)
 {
@@ -57,10 +57,8 @@
 	if (mode != HW_FILE_READ)
 		return EINVAL;
 
-	void *const mapped = mmap((void *)(uintptr_t)addr, len,
-				  PROT_READ | PROT_WRITE,
-				  MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED,
-				  -1, (off_t)0);
+	void *const mapped = mmap(NULL, len, PROT_READ | PROT_WRITE,
+				  MAP_ANONYMOUS | MAP_PRIVATE, -1, (off_t)0);
 	if (mapped == MAP_FAILED)
 		return errno;
 
@@ -83,6 +81,7 @@
 	if (mprotect(mapped, len, PROT_READ))
 		goto _munmap;
 
+	*addr = (uint64_t)(uintptr_t)mapped;
 	return 0;
 
 _munmap:
@@ -90,14 +89,14 @@
 	return errno;
 }
 
-int hw_file_map(const char *const path,
-		const uint64_t addr,
+int hw_file_map(uint64_t *const addr,
+		const char *const path,
 		const uint32_t len,
 		const uint32_t mode,
 		const int copy)
 {
 	if (copy)
-		return map_fill_from_file(path, addr, len, mode);
+		return map_fill_from_file(addr, path, len, mode);
 	else
-		return map_file(path, addr, len, mode);
+		return map_file(addr, path, len, mode);
 }
diff --git a/common/hw-file.ads b/common/hw-file.ads
index 9f5e452..57cf429 100644
--- a/common/hw-file.ads
+++ b/common/hw-file.ads
@@ -14,16 +14,19 @@
 
 package HW.File is
 
+   --
    -- Map a file's content into our address space
    --
    -- If `Map_Copy` is `False`, `Len` bytes from the start of the file
    -- given by `Path` shall be mapped into the application's address
-   -- space at `Addr` using mmap(). If `Map_Copy` is `True`, anonymous
-   -- memory should be mapped instead and be filled with a copy of the
-   -- file's content using read().
+   -- space using mmap().
+   --
+   -- If `Map_Copy` is `True`, anonymous memory should be mapped instead
+   -- and be filled with a copy of the file's content using read().
+   --
    procedure Map
-     (Path     : in     String;
-      Addr     : in     Word64;
+     (Addr     :    out Word64;
+      Path     : in     String;
       Len      : in     Natural;
       Readable : in     Boolean := False;
       Writable : in     Boolean := False;