Firmware resident model

Support referencing a network model by index that has been built into
the firmware binary.

Change-Id: Idd5294376ea82503dfeafe1203dcc0694d296dfe
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index 5aa47bb..6d2beb5 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -1,5 +1,5 @@
 #
-# (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+# Copyright (c) 2020-2022 Arm Limited.
 #
 # This program is free software and is provided to you under the terms of the
 # GNU General Public License version 2 as published by the Free Software
diff --git a/kernel/ethosu_core_interface.h b/kernel/ethosu_core_interface.h
index ef63c3b..cc4fca4 100644
--- a/kernel/ethosu_core_interface.h
+++ b/kernel/ethosu_core_interface.h
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020-2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
@@ -88,27 +88,61 @@
 	uint8_t                         data[];
 };
 
+/**
+ * enum ethosu_core_status - Status
+ */
 enum ethosu_core_status {
 	ETHOSU_CORE_STATUS_OK,
 	ETHOSU_CORE_STATUS_ERROR
 };
 
+/**
+ * struct ethosu_core_buffer - Buffer descriptor
+ *
+ * Pointer and size to a buffer withing the Ethos-U
+ * address space.
+ */
 struct ethosu_core_buffer {
 	uint32_t ptr;
 	uint32_t size;
 };
 
-struct ethosu_core_inference_req {
-	uint64_t                  user_arg;
-	uint32_t                  ifm_count;
-	struct ethosu_core_buffer ifm[ETHOSU_CORE_BUFFER_MAX];
-	uint32_t                  ofm_count;
-	struct ethosu_core_buffer ofm[ETHOSU_CORE_BUFFER_MAX];
-	struct ethosu_core_buffer network;
-	uint8_t                   pmu_event_config[ETHOSU_CORE_PMU_MAX];
-	uint32_t                  pmu_cycle_counter_enable;
+/**
+ * enum ethosu_core_network_type - Network buffer type
+ */
+enum ethosu_core_network_type {
+	ETHOSU_CORE_NETWORK_BUFFER = 1,
+	ETHOSU_CORE_NETWORK_INDEX
 };
 
+/**
+ * struct ethosu_core_network_buffer - Network buffer
+ */
+struct ethosu_core_network_buffer {
+	uint32_t type;
+	union {
+		struct ethosu_core_buffer buffer;
+		uint32_t                  index;
+	};
+};
+
+/**
+ * struct ethosu_core_inference_req - Inference request
+ */
+struct ethosu_core_inference_req {
+	uint64_t                          user_arg;
+	uint32_t                          ifm_count;
+	struct ethosu_core_buffer         ifm[ETHOSU_CORE_BUFFER_MAX];
+	uint32_t                          ofm_count;
+	struct ethosu_core_buffer         ofm[ETHOSU_CORE_BUFFER_MAX];
+	struct ethosu_core_network_buffer network;
+	uint8_t                           pmu_event_config[ETHOSU_CORE_PMU_MAX];
+	uint32_t                          pmu_cycle_counter_enable;
+};
+
+/**
+ * struct ethosu_core_inference_rsp - Inference response
+ */
 struct ethosu_core_inference_rsp {
 	uint64_t user_arg;
 	uint32_t ofm_count;
@@ -121,7 +155,7 @@
 };
 
 /**
- * struct ethosu_core_msg_verson - Message protocol version
+ * struct ethosu_core_msg_version - Message protocol version
  */
 struct ethosu_core_msg_version {
 	uint8_t major;
diff --git a/kernel/ethosu_device.c b/kernel/ethosu_device.c
index e6f1e80..54d70f8 100644
--- a/kernel/ethosu_device.c
+++ b/kernel/ethosu_device.c
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020-2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
diff --git a/kernel/ethosu_device.h b/kernel/ethosu_device.h
index 3afdda8..e1d7034 100644
--- a/kernel/ethosu_device.h
+++ b/kernel/ethosu_device.h
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020-2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
diff --git a/kernel/ethosu_driver.c b/kernel/ethosu_driver.c
index 9d02431..a530f95 100644
--- a/kernel/ethosu_driver.c
+++ b/kernel/ethosu_driver.c
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020-2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
diff --git a/kernel/ethosu_inference.c b/kernel/ethosu_inference.c
index 6fde92c..17beef4 100644
--- a/kernel/ethosu_inference.c
+++ b/kernel/ethosu_inference.c
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020, 2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
@@ -90,6 +90,7 @@
 				       inf->ifm_count, inf->ifm,
 				       inf->ofm_count, inf->ofm,
 				       inf->net->buf,
+				       inf->net->index,
 				       inf->pmu_event_config,
 				       ETHOSU_PMU_EVENT_MAX,
 				       inf->pmu_cycle_counter_enable);
diff --git a/kernel/ethosu_mailbox.c b/kernel/ethosu_mailbox.c
index 7f159f3..ef9a07d 100644
--- a/kernel/ethosu_mailbox.c
+++ b/kernel/ethosu_mailbox.c
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020-2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
@@ -234,6 +234,7 @@
 			     uint32_t ofm_count,
 			     struct ethosu_buffer **ofm,
 			     struct ethosu_buffer *network,
+			     uint32_t network_index,
 			     uint8_t *pmu_event_config,
 			     uint8_t pmu_event_config_count,
 			     uint8_t pmu_cycle_counter_enable)
@@ -262,7 +263,13 @@
 	for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++)
 		inf.pmu_event_config[i] = pmu_event_config[i];
 
-	ethosu_core_set_size(network, &inf.network);
+	if (network != NULL) {
+		inf.network.type = ETHOSU_CORE_NETWORK_BUFFER;
+		ethosu_core_set_size(network, &inf.network.buffer);
+	} else {
+		inf.network.type = ETHOSU_CORE_NETWORK_INDEX;
+		inf.network.index = network_index;
+	}
 
 	return ethosu_queue_write_msg(mbox, ETHOSU_CORE_MSG_INFERENCE_REQ,
 				      &inf, sizeof(inf));
diff --git a/kernel/ethosu_mailbox.h b/kernel/ethosu_mailbox.h
index 5cd5e62..af3d986 100644
--- a/kernel/ethosu_mailbox.h
+++ b/kernel/ethosu_mailbox.h
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020-2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
@@ -133,6 +133,7 @@
 			     uint32_t ofm_count,
 			     struct ethosu_buffer **ofm,
 			     struct ethosu_buffer *network,
+			     uint32_t network_index,
 			     uint8_t *pmu_event_config,
 			     uint8_t pmu_event_config_count,
 			     uint8_t pmu_cycle_counter_enable);
diff --git a/kernel/ethosu_network.c b/kernel/ethosu_network.c
index 4d68f05..57ccb62 100644
--- a/kernel/ethosu_network.c
+++ b/kernel/ethosu_network.c
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020, 2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
@@ -69,7 +69,9 @@
 
 	dev_info(net->edev->dev, "Network destroy. handle=0x%pK\n", net);
 
-	ethosu_buffer_put(net->buf);
+	if (net->buf != NULL)
+		ethosu_buffer_put(net->buf);
+
 	devm_kfree(net->edev->dev, net);
 }
 
@@ -128,28 +130,31 @@
 int ethosu_network_create(struct ethosu_device *edev,
 			  struct ethosu_uapi_network_create *uapi)
 {
-	struct ethosu_buffer *buf;
 	struct ethosu_network *net;
 	int ret = -ENOMEM;
 
-	buf = ethosu_buffer_get_from_fd(uapi->fd);
-	if (IS_ERR(buf))
-		return PTR_ERR(buf);
-
 	net = devm_kzalloc(edev->dev, sizeof(*net), GFP_KERNEL);
-	if (!net) {
-		ret = -ENOMEM;
-		goto put_buf;
-	}
+	if (!net)
+		return -ENOMEM;
 
 	net->edev = edev;
-	net->buf = buf;
+	net->buf = NULL;
 	kref_init(&net->kref);
 
+	if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) {
+		net->buf = ethosu_buffer_get_from_fd(uapi->fd);
+		if (IS_ERR(net->buf)) {
+			ret = PTR_ERR(net->buf);
+			goto free_net;
+		}
+	} else {
+		net->index = uapi->index;
+	}
+
 	ret = anon_inode_getfd("ethosu-network", &ethosu_network_fops, net,
 			       O_RDWR | O_CLOEXEC);
 	if (ret < 0)
-		goto free_net;
+		goto put_buf;
 
 	net->file = fget(ret);
 	fput(net->file);
@@ -159,12 +164,13 @@
 
 	return ret;
 
+put_buf:
+	if (net->buf != NULL)
+		ethosu_buffer_put(net->buf);
+
 free_net:
 	devm_kfree(edev->dev, net);
 
-put_buf:
-	ethosu_buffer_put(buf);
-
 	return ret;
 }
 
diff --git a/kernel/ethosu_network.h b/kernel/ethosu_network.h
index bb70afc..e70b46f 100644
--- a/kernel/ethosu_network.h
+++ b/kernel/ethosu_network.h
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020, 2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
@@ -43,6 +43,7 @@
 	struct file          *file;
 	struct kref          kref;
 	struct ethosu_buffer *buf;
+	uint32_t             index;
 };
 
 /****************************************************************************
diff --git a/kernel/uapi/ethosu.h b/kernel/uapi/ethosu.h
index 903316d..335c769 100644
--- a/kernel/uapi/ethosu.h
+++ b/kernel/uapi/ethosu.h
@@ -1,5 +1,5 @@
 /*
- * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
+ * Copyright (c) 2020-2022 Arm Limited.
  *
  * This program is free software and is provided to you under the terms of the
  * GNU General Public License version 2 as published by the Free Software
@@ -97,11 +97,28 @@
 };
 
 /**
+ * enum ethosu_uapi_network_create - Network buffer type.
+ * @ETHOSU_UAPI_NETWORK_BUFFER:	Network is stored in a buffer handle.
+ * @ETHOSU_UAPI_NETWORK_INDEX:	Network is built into firmware and referenced by
+ *                              index.
+ */
+enum ethosu_uapi_network_type {
+	ETHOSU_UAPI_NETWORK_BUFFER = 1,
+	ETHOSU_UAPI_NETWORK_INDEX
+};
+
+/**
  * struct ethosu_uapi_network_create - Create network request
+ * @type:	Buffer type. See @ethosu_uapi_network_type.
  * @fd:		Buffer file descriptor
+ * @index:	Buffer index compiled into firmware binary.
  */
 struct ethosu_uapi_network_create {
-	__u32 fd;
+	uint32_t type;
+	union {
+		__u32 fd;
+		__u32 index;
+	};
 };
 
 /**