aboutsummaryrefslogtreecommitdiff
path: root/kernel/ethosu_network_info.c
blob: c2b6caaf8863e0663afa06036d0545dc86d01209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (c) 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
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 * SPDX-License-Identifier: GPL-2.0-only
 */

/****************************************************************************
 * Includes
 ****************************************************************************/

#include "ethosu_network_info.h"

#include "ethosu_device.h"
#include "ethosu_network.h"
#include "ethosu_mailbox.h"
#include "uapi/ethosu.h"

/****************************************************************************
 * Functions
 ****************************************************************************/

static void ethosu_network_info_destroy(struct kref *kref)
{
	struct ethosu_network_info *info =
		container_of(kref, struct ethosu_network_info, kref);

	dev_info(info->edev->dev, "Network info destroy. handle=0x%pK\n", info);

	list_del(&info->msg.list);

	ethosu_network_put(info->net);

	devm_kfree(info->edev->dev, info);
}

static int ethosu_network_info_send(struct ethosu_network_info *info)
{
	int ret;

	/* Send network info request to firmware */
	ret = ethosu_mailbox_network_info_request(&info->edev->mailbox,
						  info,
						  info->net->buf,
						  info->net->index);
	if (ret)
		return ret;

	return 0;
}

static void ethosu_network_info_fail(struct ethosu_mailbox_msg *msg)
{
	struct ethosu_network_info *info =
		container_of(msg, typeof(*info), msg);

	if (completion_done(&info->done))
		return;

	info->errno = -EFAULT;
	complete(&info->done);
}

static int ethosu_network_info_resend(struct ethosu_mailbox_msg *msg)
{
	struct ethosu_network_info *info =
		container_of(msg, typeof(*info), msg);
	int ret;

	/* Don't resend request if response has already been received */
	if (completion_done(&info->done))
		return 0;

	/* Resend request */
	ret = ethosu_network_info_send(info);
	if (ret)
		return ret;

	return 0;
}

struct ethosu_network_info *ethosu_network_info_create(
	struct ethosu_device *edev,
	struct ethosu_network *net,
	struct ethosu_uapi_network_info *uapi)
{
	struct ethosu_network_info *info;
	int ret;

	info = devm_kzalloc(edev->dev, sizeof(*info), GFP_KERNEL);
	if (!info)
		return ERR_PTR(-ENOMEM);

	info->edev = edev;
	info->net = net;
	info->uapi = uapi;
	kref_init(&info->kref);
	init_completion(&info->done);
	info->msg.fail = ethosu_network_info_fail;
	info->msg.resend = ethosu_network_info_resend;

	/* Insert network info to network info list */
	list_add(&info->msg.list, &edev->mailbox.pending_list);

	/* Get reference to network */
	ethosu_network_get(net);

	ret = ethosu_network_info_send(info);
	if (ret)
		goto put_info;

	dev_info(edev->dev, "Network info create. handle=%p\n", info);

	return info;

put_info:
	ethosu_network_info_put(info);

	return ERR_PTR(ret);
}

void ethosu_network_info_get(struct ethosu_network_info *info)
{
	kref_get(&info->kref);
}

int ethosu_network_info_put(struct ethosu_network_info *info)
{
	return kref_put(&info->kref, ethosu_network_info_destroy);
}

int ethosu_network_info_wait(struct ethosu_network_info *info,
			     int timeout_ms)
{
	int timeout;

	timeout = wait_for_completion_timeout(&info->done,
					      msecs_to_jiffies(timeout_ms));

	if (!timeout) {
		dev_warn(info->edev->dev,
			 "Network info timed out.");

		return -ETIME;
	}

	return info->errno;
}

void ethosu_network_info_rsp(struct ethosu_device *edev,
			     struct ethosu_core_network_info_rsp *rsp)
{
	struct ethosu_network_info *info =
		(struct ethosu_network_info *)rsp->user_arg;
	uint32_t i;
	int ret;

	ret = ethosu_mailbox_find(&edev->mailbox, &info->msg);
	if (0 != ret) {
		dev_warn(edev->dev,
			 "Handle not found in network info list. handle=0x%p\n",
			 info);

		return;
	}

	if (completion_done(&info->done))
		return;

	info->errno = 0;

	if (rsp->status != ETHOSU_CORE_STATUS_OK) {
		info->errno = -EBADF;
		goto signal_complete;
	}

	if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
		info->errno = -ENFILE;
		goto signal_complete;
	}

	strncpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));

	info->uapi->ifm_count = rsp->ifm_count;
	for (i = 0; i < rsp->ifm_count; i++)
		info->uapi->ifm_size[i] = rsp->ifm_size[i];

	info->uapi->ofm_count = rsp->ofm_count;
	for (i = 0; i < rsp->ofm_count; i++)
		info->uapi->ofm_size[i] = rsp->ofm_size[i];

signal_complete:
	complete(&info->done);
}