uniphier: set buffer offset and length for io_block dynamically

Currently, the .buffer field in io_block_dev_spec is statically set,
which is not handy for PIE.

Towards the goal of making this really position-independent, set the
buffer length and length in the uniphier_io_block_setup() function.

Change-Id: I22b20d7b58d6ffd38f64f967a2820fca4bd7dade
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
diff --git a/plat/socionext/uniphier/uniphier.h b/plat/socionext/uniphier/uniphier.h
index 1d3651a..6ccd422 100644
--- a/plat/socionext/uniphier/uniphier.h
+++ b/plat/socionext/uniphier/uniphier.h
@@ -36,9 +36,11 @@
 
 void uniphier_console_setup(void);
 
-int uniphier_emmc_init(uintptr_t *block_dev_spec);
-int uniphier_nand_init(uintptr_t *block_dev_spec);
-int uniphier_usb_init(unsigned int soc, uintptr_t *block_dev_spec);
+struct io_block_dev_spec;
+int uniphier_emmc_init(struct io_block_dev_spec **block_dev_spec);
+int uniphier_nand_init(struct io_block_dev_spec **block_dev_spec);
+int uniphier_usb_init(unsigned int soc,
+		      struct io_block_dev_spec **block_dev_spec);
 
 int uniphier_io_setup(unsigned int soc);
 
@@ -74,8 +76,4 @@
 					 (UNIPHIER_BL33_MAX_SIZE))
 #define UNIPHIER_SCP_MAX_SIZE		0x00020000
 
-#define UNIPHIER_BLOCK_BUF_BASE		((UNIPHIER_SCP_BASE) + \
-					 (UNIPHIER_SCP_MAX_SIZE))
-#define UNIPHIER_BLOCK_BUF_SIZE		0x00100000
-
 #endif /* UNIPHIER_H */
diff --git a/plat/socionext/uniphier/uniphier_emmc.c b/plat/socionext/uniphier/uniphier_emmc.c
index 4ac1f51..d666ba7 100644
--- a/plat/socionext/uniphier/uniphier_emmc.c
+++ b/plat/socionext/uniphier/uniphier_emmc.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -225,11 +225,7 @@
 	return ret ? 0 : size;
 }
 
-static const struct io_block_dev_spec uniphier_emmc_dev_spec = {
-	.buffer = {
-		.offset = UNIPHIER_BLOCK_BUF_BASE,
-		.length = UNIPHIER_BLOCK_BUF_SIZE,
-	},
+static struct io_block_dev_spec uniphier_emmc_dev_spec = {
 	.ops = {
 		.read = uniphier_emmc_read,
 	},
@@ -278,7 +274,7 @@
 	return 0;
 }
 
-int uniphier_emmc_init(uintptr_t *block_dev_spec)
+int uniphier_emmc_init(struct io_block_dev_spec **block_dev_spec)
 {
 	int ret;
 
@@ -286,7 +282,7 @@
 	if (ret)
 		return ret;
 
-	*block_dev_spec = (uintptr_t)&uniphier_emmc_dev_spec;
+	*block_dev_spec = &uniphier_emmc_dev_spec;
 
 	return 0;
 }
diff --git a/plat/socionext/uniphier/uniphier_io_storage.c b/plat/socionext/uniphier/uniphier_io_storage.c
index c039acc..d151914 100644
--- a/plat/socionext/uniphier/uniphier_io_storage.c
+++ b/plat/socionext/uniphier/uniphier_io_storage.c
@@ -26,6 +26,9 @@
 #define UNIPHIER_OCM_REGION_BASE	0x30000000ULL
 #define UNIPHIER_OCM_REGION_SIZE	0x00040000ULL
 
+#define UNIPHIER_BLOCK_BUF_BASE		0x84200000UL
+#define UNIPHIER_BLOCK_BUF_SIZE		0x00100000UL
+
 static const io_dev_connector_t *uniphier_fip_dev_con;
 static uintptr_t uniphier_fip_dev_handle;
 
@@ -189,15 +192,20 @@
 #endif
 };
 
-static int uniphier_io_block_setup(size_t fip_offset, uintptr_t block_dev_spec)
+static int uniphier_io_block_setup(size_t fip_offset,
+				   struct io_block_dev_spec *block_dev_spec,
+				   size_t buffer_offset)
 {
 	int ret;
 
 	uniphier_fip_spec.offset = fip_offset;
 
-	ret = mmap_add_dynamic_region(UNIPHIER_BLOCK_BUF_BASE,
-				      UNIPHIER_BLOCK_BUF_BASE,
-				      UNIPHIER_BLOCK_BUF_SIZE,
+	block_dev_spec->buffer.offset = buffer_offset;
+	block_dev_spec->buffer.length = UNIPHIER_BLOCK_BUF_SIZE;
+
+	ret = mmap_add_dynamic_region(block_dev_spec->buffer.offset,
+				      block_dev_spec->buffer.offset,
+				      block_dev_spec->buffer.length,
 				      MT_MEMORY | MT_RW | MT_NS);
 	if (ret)
 		return ret;
@@ -206,7 +214,7 @@
 	if (ret)
 		return ret;
 
-	return io_dev_open(uniphier_backend_dev_con, block_dev_spec,
+	return io_dev_open(uniphier_backend_dev_con, (uintptr_t)block_dev_spec,
 			   &uniphier_backend_dev_handle);
 }
 
@@ -241,38 +249,38 @@
 	return io_dev_open(uniphier_fip_dev_con, 0, &uniphier_fip_dev_handle);
 }
 
-static int uniphier_io_emmc_setup(unsigned int soc_id)
+static int uniphier_io_emmc_setup(unsigned int soc_id, size_t buffer_offset)
 {
-	uintptr_t block_dev_spec;
+	struct io_block_dev_spec *block_dev_spec;
 	int ret;
 
 	ret = uniphier_emmc_init(&block_dev_spec);
 	if (ret)
 		return ret;
 
-	return uniphier_io_block_setup(0x20000, block_dev_spec);
+	return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
 }
 
-static int uniphier_io_nand_setup(unsigned int soc_id)
+static int uniphier_io_nand_setup(unsigned int soc_id, size_t buffer_offset)
 {
-	uintptr_t block_dev_spec;
+	struct io_block_dev_spec *block_dev_spec;
 	int ret;
 
 	ret = uniphier_nand_init(&block_dev_spec);
 	if (ret)
 		return ret;
 
-	return uniphier_io_block_setup(0x20000, block_dev_spec);
+	return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
 }
 
-static int uniphier_io_nor_setup(unsigned int soc_id)
+static int uniphier_io_nor_setup(unsigned int soc_id, size_t buffer_offset)
 {
 	return uniphier_io_memmap_setup(0x70000);
 }
 
-static int uniphier_io_usb_setup(unsigned int soc_id)
+static int uniphier_io_usb_setup(unsigned int soc_id, size_t buffer_offset)
 {
-	uintptr_t block_dev_spec;
+	struct io_block_dev_spec *block_dev_spec;
 	int ret;
 
 	/* use ROM API for loading images from USB storage */
@@ -299,10 +307,10 @@
 	if (ret)
 		return ret;
 
-	return uniphier_io_block_setup(0x20000, block_dev_spec);
+	return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
 }
 
-static int (* const uniphier_io_setup_table[])(unsigned int) = {
+static int (* const uniphier_io_setup_table[])(unsigned int, size_t) = {
 	[UNIPHIER_BOOT_DEVICE_EMMC] = uniphier_io_emmc_setup,
 	[UNIPHIER_BOOT_DEVICE_NAND] = uniphier_io_nand_setup,
 	[UNIPHIER_BOOT_DEVICE_NOR] = uniphier_io_nor_setup,
@@ -311,7 +319,7 @@
 
 int uniphier_io_setup(unsigned int soc_id)
 {
-	int (*io_setup)(unsigned int soc_id);
+	int (*io_setup)(unsigned int soc_id, size_t buffer_offset);
 	unsigned int boot_dev;
 	int ret;
 
@@ -320,7 +328,7 @@
 		return -EINVAL;
 
 	io_setup = uniphier_io_setup_table[boot_dev];
-	ret = io_setup(soc_id);
+	ret = io_setup(soc_id, UNIPHIER_BLOCK_BUF_BASE);
 	if (ret)
 		return ret;
 
diff --git a/plat/socionext/uniphier/uniphier_nand.c b/plat/socionext/uniphier/uniphier_nand.c
index 27e10e4..3925177 100644
--- a/plat/socionext/uniphier/uniphier_nand.c
+++ b/plat/socionext/uniphier/uniphier_nand.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -224,10 +224,6 @@
 }
 
 static struct io_block_dev_spec uniphier_nand_dev_spec = {
-	.buffer = {
-		.offset = UNIPHIER_BLOCK_BUF_BASE,
-		.length = UNIPHIER_BLOCK_BUF_SIZE,
-	},
 	.ops = {
 		.read = uniphier_nand_read,
 	},
@@ -259,7 +255,7 @@
 	return 0;
 }
 
-int uniphier_nand_init(uintptr_t *block_dev_spec)
+int uniphier_nand_init(struct io_block_dev_spec **block_dev_spec)
 {
 	int ret;
 
@@ -269,7 +265,7 @@
 
 	uniphier_nand_dev_spec.block_size = uniphier_nand.page_size;
 
-	*block_dev_spec = (uintptr_t)&uniphier_nand_dev_spec;
+	*block_dev_spec = &uniphier_nand_dev_spec;
 
 	return 0;
 }
diff --git a/plat/socionext/uniphier/uniphier_usb.c b/plat/socionext/uniphier/uniphier_usb.c
index ef7079a..7469ad1 100644
--- a/plat/socionext/uniphier/uniphier_usb.c
+++ b/plat/socionext/uniphier/uniphier_usb.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -158,17 +158,14 @@
 }
 
 static struct io_block_dev_spec uniphier_usb_dev_spec = {
-	.buffer = {
-		.offset = UNIPHIER_BLOCK_BUF_BASE,
-		.length = UNIPHIER_BLOCK_BUF_SIZE,
-	},
 	.ops = {
 		.read = uniphier_usb_read,
 	},
 	.block_size = 512,
 };
 
-int uniphier_usb_init(unsigned int soc, uintptr_t *block_dev_spec)
+int uniphier_usb_init(unsigned int soc,
+		      struct io_block_dev_spec **block_dev_spec)
 {
 	const struct uniphier_usb_rom_param *param;
 
@@ -180,7 +177,7 @@
 
 	__uniphier_usb_read = param->read;
 
-	*block_dev_spec = (uintptr_t)&uniphier_usb_dev_spec;
+	*block_dev_spec = &uniphier_usb_dev_spec;
 
 	return 0;
 }