imx8: postpone moving resources to non-secure partition

With flash_uboot_cm4ddr in imx-mkimage, the m4 code will access ddr.
However after m4 core moved to non-secure partition, the ddr memory
is still in secure partition. Then m4 core will fault.

So postpone moving resources including m4 core, until other resources,
such as memory/pin moved to non-secure partition.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
(cherry picked from commit 1c8ce0ad5f583ec41026d4ab5bef622f1b45aecd)
(cherry picked from commit 5b026e05b8f71b3d86da0953c5ca196d5ba5cc66)
diff --git a/plat/imx/imx8qm/imx8qm_bl31_setup.c b/plat/imx/imx8qm/imx8qm_bl31_setup.c
index 1cee5ba..881c3c6 100644
--- a/plat/imx/imx8qm/imx8qm_bl31_setup.c
+++ b/plat/imx/imx8qm/imx8qm_bl31_setup.c
@@ -175,6 +175,57 @@
 				secure_rsrcs[i], err);
 	}
 
+	/*
+	 * sc_rm_set_peripheral_permissions
+	 * sc_rm_set_memreg_permissions
+	 * sc_rm_set_pin_movable
+	 */
+
+	for (mr = 0; mr < 64; mr++) {
+		owned = sc_rm_is_memreg_owned(ipc_handle, mr);
+		if (owned) {
+			err = sc_rm_get_memreg_info(ipc_handle, mr, &start, &end);
+			if (err)
+				ERROR("Memreg get info failed, %u\n", mr);
+			NOTICE("Memreg %u 0x%llx -- 0x%llx\n", mr, start, end);
+			if (BL31_BASE >= start && (BL31_LIMIT - 1) <= end) {
+				mr_record = mr; /* Record the mr for ATF running */
+			} else {
+				err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
+				if (err)
+					ERROR("Memreg assign failed, 0x%llx -- 0x%llx, \
+						err %d\n", start, end, err);
+			}
+		}
+	}
+
+	if (mr_record != 64) {
+		err = sc_rm_get_memreg_info(ipc_handle, mr_record, &start, &end);
+		if (err)
+			ERROR("Memreg get info failed, %u\n", mr_record);
+		if ((BL31_LIMIT - 1) < end) {
+			err = sc_rm_memreg_alloc(ipc_handle, &mr, BL31_LIMIT, end);
+			if (err)
+				ERROR("sc_rm_memreg_alloc failed, 0x%llx -- 0x%llx\n",
+					(sc_faddr_t)BL31_LIMIT, end);
+			err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
+			if (err)
+				ERROR("Memreg assign failed, 0x%llx -- 0x%llx\n",
+					(sc_faddr_t)BL31_LIMIT, end);
+		}
+
+		if (start < (BL31_BASE - 1)) {
+			err = sc_rm_memreg_alloc(ipc_handle, &mr, start, BL31_BASE - 1);
+			if (err)
+				ERROR("sc_rm_memreg_alloc failed, 0x%llx -- 0x%llx\n",
+					start, (sc_faddr_t)BL31_BASE - 1);
+			err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
+				if (err)
+					ERROR("Memreg assign failed, 0x%llx -- 0x%llx\n",
+						start, (sc_faddr_t)BL31_BASE - 1);
+		}
+	}
+
 	owned = sc_rm_is_resource_owned(ipc_handle, SC_R_M4_0_PID0);
 	if (owned) {
 		err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_0_PID0,
@@ -229,57 +280,6 @@
 				SC_R_M4_1_PID0, err);
 	}
 
-	/*
-	 * sc_rm_set_peripheral_permissions
-	 * sc_rm_set_memreg_permissions
-	 * sc_rm_set_pin_movable
-	 */
-
-	for (mr = 0; mr < 64; mr++) {
-		owned = sc_rm_is_memreg_owned(ipc_handle, mr);
-		if (owned) {
-			err = sc_rm_get_memreg_info(ipc_handle, mr, &start, &end);
-			if (err)
-				ERROR("Memreg get info failed, %u\n", mr);
-			NOTICE("Memreg %u 0x%llx -- 0x%llx\n", mr, start, end);
-			if (BL31_BASE >= start && (BL31_LIMIT - 1) <= end) {
-				mr_record = mr; /* Record the mr for ATF running */
-			} else {
-				err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
-				if (err)
-					ERROR("Memreg assign failed, 0x%llx -- 0x%llx, \
-						err %d\n", start, end, err);
-			}
-		}
-	}
-
-	if (mr_record != 64) {
-		err = sc_rm_get_memreg_info(ipc_handle, mr_record, &start, &end);
-		if (err)
-			ERROR("Memreg get info failed, %u\n", mr_record);
-		if ((BL31_LIMIT - 1) < end) {
-			err = sc_rm_memreg_alloc(ipc_handle, &mr, BL31_LIMIT, end);
-			if (err)
-				ERROR("sc_rm_memreg_alloc failed, 0x%llx -- 0x%llx\n",
-					(sc_faddr_t)BL31_LIMIT, end);
-			err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
-			if (err)
-				ERROR("Memreg assign failed, 0x%llx -- 0x%llx\n",
-					(sc_faddr_t)BL31_LIMIT, end);
-		}
-
-		if (start < (BL31_BASE - 1)) {
-			err = sc_rm_memreg_alloc(ipc_handle, &mr, start, BL31_BASE - 1);
-			if (err)
-				ERROR("sc_rm_memreg_alloc failed, 0x%llx -- 0x%llx\n",
-					start, (sc_faddr_t)BL31_BASE - 1);
-			err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
-				if (err)
-					ERROR("Memreg assign failed, 0x%llx -- 0x%llx\n",
-						start, (sc_faddr_t)BL31_BASE - 1);
-		}
-	}
-
 	if (err)
 		NOTICE("Partitioning Failed\n");
 	else
diff --git a/plat/imx/imx8qx/imx8qx_bl31_setup.c b/plat/imx/imx8qx/imx8qx_bl31_setup.c
index d5bd59b..ad2a1b6 100644
--- a/plat/imx/imx8qx/imx8qx_bl31_setup.c
+++ b/plat/imx/imx8qx/imx8qx_bl31_setup.c
@@ -176,40 +176,6 @@
 				secure_rsrcs[i], err);
 	}
 
-	owned = sc_rm_is_resource_owned(ipc_handle, SC_R_M4_0_PID0);
-	if (owned) {
-		err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_0_PID0,
-				SC_R_M4_0_PID0, false);
-		if (err)
-			ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
-				SC_R_M4_0_PID0, err);
-	}
-
-	/* move all movable resources and pins to non-secure partition */
-	err = sc_rm_move_all(ipc_handle, secure_part, os_part, true, true);
-	if (err)
-		ERROR("sc_rm_move_all: %u\n", err);
-	if (owned) {
-		err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_0_PID0,
-				SC_R_M4_0_PID0, true);
-		if (err)
-			ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
-				SC_R_M4_0_PID0, err);
-		err = sc_rm_assign_resource(ipc_handle, os_part, SC_R_M4_0_PID0);
-		if (err)
-			ERROR("sc_rm_assign_resource: rsrc %u, ret %u\n",
-				SC_R_M4_0_PID0, err);
-	}
-
-	/* iterate through peripherals to give NS OS part access */
-	for (i = 0; i < ARRAY_SIZE(ns_access_allowed); i++) {
-		err = sc_rm_set_peripheral_permissions(ipc_handle,
-			ns_access_allowed[i], os_part, SC_RM_PERM_FULL);
-		if (err)
-			ERROR("sc_rm_set_peripheral_permissions: rsrc %u, \
-				ret %u\n", ns_access_allowed[i], err);
-	}
-
 	/*
 	 * sc_rm_set_peripheral_permissions
 	 * sc_rm_set_memreg_permissions
@@ -261,6 +227,40 @@
 		}
 	}
 
+	owned = sc_rm_is_resource_owned(ipc_handle, SC_R_M4_0_PID0);
+	if (owned) {
+		err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_0_PID0,
+				SC_R_M4_0_PID0, false);
+		if (err)
+			ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
+				SC_R_M4_0_PID0, err);
+	}
+
+	/* move all movable resources and pins to non-secure partition */
+	err = sc_rm_move_all(ipc_handle, secure_part, os_part, true, true);
+	if (err)
+		ERROR("sc_rm_move_all: %u\n", err);
+	if (owned) {
+		err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_0_PID0,
+				SC_R_M4_0_PID0, true);
+		if (err)
+			ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
+				SC_R_M4_0_PID0, err);
+		err = sc_rm_assign_resource(ipc_handle, os_part, SC_R_M4_0_PID0);
+		if (err)
+			ERROR("sc_rm_assign_resource: rsrc %u, ret %u\n",
+				SC_R_M4_0_PID0, err);
+	}
+
+	/* iterate through peripherals to give NS OS part access */
+	for (i = 0; i < ARRAY_SIZE(ns_access_allowed); i++) {
+		err = sc_rm_set_peripheral_permissions(ipc_handle,
+			ns_access_allowed[i], os_part, SC_RM_PERM_FULL);
+		if (err)
+			ERROR("sc_rm_set_peripheral_permissions: rsrc %u, \
+				ret %u\n", ns_access_allowed[i], err);
+	}
+
 	if (err)
 		NOTICE("Partitioning Failed\n");
 	else