trng: Move device memory retrieve to platform code

trng_adapter_init() was checking for device memory. However not every
trng implementation needs device memory. Instead we move the device
memory retrieval to the platform specific code.

Signed-off-by: Jelle Sels <jelle.sels@arm.com>
Change-Id: Ib4ebca98152900e471711a9106682cd43510057d
diff --git a/components/service/crypto/backend/mbedcrypto/trng_adapter/platform/platform_trng_adapter.c b/components/service/crypto/backend/mbedcrypto/trng_adapter/platform/platform_trng_adapter.c
index f05fe63..c509fcf 100644
--- a/components/service/crypto/backend/mbedcrypto/trng_adapter/platform/platform_trng_adapter.c
+++ b/components/service/crypto/backend/mbedcrypto/trng_adapter/platform/platform_trng_adapter.c
@@ -1,12 +1,11 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 #include <mbedtls/entropy.h>
 #include <platform/interface/trng.h>
 #include <service/crypto/backend/mbedcrypto/trng_adapter/trng_adapter.h>
-#include <config/interface/config_store.h>
 #include <psa/error.h>
 #include <stddef.h>
 
@@ -19,17 +18,7 @@
 
 int trng_adapter_init(int instance)
 {
-	int status = PSA_STATUS_HARDWARE_FAILURE;
-	struct device_region device_region;
-
-	if (config_store_query(CONFIG_CLASSIFIER_DEVICE_REGION,
-		"trng", instance,
-		&device_region, sizeof(device_region))) {
-
-		status = platform_trng_create(&driver, &device_region);
-	}
-
-	return status;
+	return platform_trng_create(&driver,instance);
 }
 
 void trng_adapter_deinit()
diff --git a/platform/drivers/arm/juno_trng/juno_trng_adapter.c b/platform/drivers/arm/juno_trng/juno_trng_adapter.c
index 3ad8b04..69d37ce 100644
--- a/platform/drivers/arm/juno_trng/juno_trng_adapter.c
+++ b/platform/drivers/arm/juno_trng/juno_trng_adapter.c
@@ -1,10 +1,12 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
+#include <config/interface/config_store.h>
 #include <platform/interface/trng.h>
 #include <platform/interface/device_region.h>
+#include <psa/error.h>
 #include "juno_decl.h"
 
 /*
@@ -38,10 +40,10 @@
     return status;
 }
 
-int platform_trng_create(struct platform_trng_driver *driver,
-                            const struct device_region *device_region)
+int platform_trng_create(struct platform_trng_driver *driver, int instance)
 {
     static const struct platform_trng_iface iface =  { .poll = trng_poll };
+    struct device_region device_region;
 
     /*
      * Default to leaving the driver in a safe but inoperable state.
@@ -49,15 +51,17 @@
     driver->iface = &iface;
     driver->context = NULL;
 
-    if (device_region) {
+    if (!config_store_query(CONFIG_CLASSIFIER_DEVICE_REGION, "trng",
+                            instance, &device_region,
+                            sizeof(device_region)))
+        return PSA_STATUS_HARDWARE_FAILURE;
 
-        /*
-         * A device region has been provided, possibly from an external configuation.
-         */
-        juno_trng_set_base_addr(device_region->base_addr);
-    }
+   /*
+    * A device region has been provided, possibly from an external configuation.
+    */
+   juno_trng_set_base_addr(device_region.base_addr);
 
-    return 0;
+   return PSA_SUCCESS;
 }
 
 void platform_trng_destroy(struct platform_trng_driver *driver)
diff --git a/platform/drivers/arm/tztrng/tztrng_adapter.c b/platform/drivers/arm/tztrng/tztrng_adapter.c
index f52eeaa..bcabfd0 100644
--- a/platform/drivers/arm/tztrng/tztrng_adapter.c
+++ b/platform/drivers/arm/tztrng/tztrng_adapter.c
@@ -1,10 +1,12 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
+#include <config/interface/config_store.h>
 #include <platform/interface/trng.h>
 #include <platform/interface/device_region.h>
+#include <psa/error.h>
 #include <tztrng.h>
 #include <tztrng_defs.h>
 #include <stdlib.h>
@@ -44,10 +46,11 @@
     return status;
 }
 
-int platform_trng_create(struct platform_trng_driver *driver,
-                            const struct device_region *device_region)
+int platform_trng_create(struct platform_trng_driver *driver, int instance)
 {
     static const struct platform_trng_iface iface =  { .poll = trng_poll };
+    struct device_region device_region;
+    struct tztrng_instance *new_instance = NULL;
 
     /*
      * Default to leaving the driver in a safe but inoperable state.
@@ -55,22 +58,24 @@
     driver->iface = &iface;
     driver->context = NULL;
 
-    if (device_region) {
+    if (!config_store_query(CONFIG_CLASSIFIER_DEVICE_REGION, "trng",
+                            instance, &device_region,
+                            sizeof(device_region)))
+        return PSA_STATUS_HARDWARE_FAILURE;
 
-        /*
-         * A device region has been provided, possibly from an external configuation.
-         * Check that it's a sensible size to defend against a bogus configuration.
-         */
-        struct tztrng_instance *new_instance = malloc(sizeof(struct tztrng_instance));
+    /*
+     * A device region has been provided, possibly from an external configuation.
+     * Check that it's a sensible size to defend against a bogus configuration.
+     */
+    new_instance = malloc(sizeof(struct tztrng_instance));
 
-        if (new_instance) {
+    if (!new_instance)
+        return PSA_ERROR_INSUFFICIENT_MEMORY;
 
-            new_instance->trng_device_region = *device_region;
-            driver->context = new_instance;
-        }
-    }
+    new_instance->trng_device_region = device_region;
+    driver->context = new_instance;
 
-    return 0;
+    return PSA_SUCCESS;
 }
 
 void platform_trng_destroy(struct platform_trng_driver *driver)
diff --git a/platform/drivers/mock/mock_trng.c b/platform/drivers/mock/mock_trng.c
index 24b14c0..a9f79f8 100644
--- a/platform/drivers/mock/mock_trng.c
+++ b/platform/drivers/mock/mock_trng.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -24,13 +24,11 @@
     return 0;
 }
 
-int platform_trng_create(struct platform_trng_driver *driver,
-                            const struct device_region *device_region)
+int platform_trng_create(struct platform_trng_driver *driver, int instance)
 {
     static const struct platform_trng_iface iface =  { .poll = mock_poll };
 
-    (void)device_region;
-
+    (void)instance;
     driver->context = NULL;
     driver->iface = &iface;
 
diff --git a/platform/interface/trng.h b/platform/interface/trng.h
index 9c24581..0f1daf5 100644
--- a/platform/interface/trng.h
+++ b/platform/interface/trng.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -51,12 +51,11 @@
  * \brief Factory method to construct a platform specific trng driver
  *
  * \param driver    Pointer to driver structure to initialize on construction.
- * \param device_region Pointer a device region object or NULL if none.
+ * \param instance    Deployment specific trng instance.
  *
  * \return          0 if successful.
  */
-int platform_trng_create(struct platform_trng_driver *driver,
-                           const struct device_region *device_region);
+int platform_trng_create(struct platform_trng_driver *driver, int instance);
 
 /**
  * \brief Destroy a driver constructed using the factory method