blob: 52adf9ca407ab9d2ea0307a235fa5c28df626cf6 [file] [log] [blame]
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01001/*
2 * Copyright (c) 2022 ARM Limited.
3 *
4 * This program is free software and is provided to you under the terms of the
5 * GNU General Public License version 2 as published by the Free Software
6 * Foundation, and any use by you of this program is subject to the terms
7 * of such GNU licence.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * SPDX-License-Identifier: GPL-2.0-only
19 */
20
21/****************************************************************************
22 * Includes
23 ****************************************************************************/
24
25#include "ethosu_network_info.h"
26
27#include "ethosu_device.h"
28#include "ethosu_network.h"
29#include "ethosu_mailbox.h"
30#include "uapi/ethosu.h"
31
Davide Grohmann32660f92022-04-27 16:49:07 +020032#define NETWORK_INFO_RESP_TIMEOUT_MS 3000
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010033
Davide Grohmann32660f92022-04-27 16:49:07 +020034static inline int ethosu_network_info_send(struct ethosu_network_info *info)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010035{
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010036 /* Send network info request to firmware */
Davide Grohmann32660f92022-04-27 16:49:07 +020037 return ethosu_mailbox_network_info_request(&info->edev->mailbox,
38 &info->msg,
39 info->net->buf,
40 info->net->index);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010041}
42
43static void ethosu_network_info_fail(struct ethosu_mailbox_msg *msg)
44{
45 struct ethosu_network_info *info =
46 container_of(msg, typeof(*info), msg);
47
48 if (completion_done(&info->done))
49 return;
50
51 info->errno = -EFAULT;
52 complete(&info->done);
53}
54
55static int ethosu_network_info_resend(struct ethosu_mailbox_msg *msg)
56{
57 struct ethosu_network_info *info =
58 container_of(msg, typeof(*info), msg);
59 int ret;
60
61 /* Don't resend request if response has already been received */
62 if (completion_done(&info->done))
63 return 0;
64
65 /* Resend request */
66 ret = ethosu_network_info_send(info);
67 if (ret)
68 return ret;
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010069
70 return 0;
71}
72
Davide Grohmann32660f92022-04-27 16:49:07 +020073int ethosu_network_info_request(struct ethosu_network *net,
74 struct ethosu_uapi_network_info *uapi)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010075{
76 struct ethosu_network_info *info;
77 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +020078 int timeout;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010079
Davide Grohmann32660f92022-04-27 16:49:07 +020080 info = devm_kzalloc(net->edev->dev, sizeof(*info), GFP_KERNEL);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010081 if (!info)
Davide Grohmann32660f92022-04-27 16:49:07 +020082 return -ENOMEM;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010083
Davide Grohmann32660f92022-04-27 16:49:07 +020084 info->edev = net->edev;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010085 info->net = net;
86 info->uapi = uapi;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010087 init_completion(&info->done);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010088 info->msg.fail = ethosu_network_info_fail;
89 info->msg.resend = ethosu_network_info_resend;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010090
Davide Grohmann32660f92022-04-27 16:49:07 +020091 ret = ethosu_mailbox_register(&info->edev->mailbox, &info->msg);
92 if (ret < 0)
93 goto kfree;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010094
95 /* Get reference to network */
Davide Grohmann32660f92022-04-27 16:49:07 +020096 ethosu_network_get(info->net);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010097
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010098 ret = ethosu_network_info_send(info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010099 if (ret)
Davide Grohmann32660f92022-04-27 16:49:07 +0200100 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100101
Davide Grohmann32660f92022-04-27 16:49:07 +0200102 dev_info(info->edev->dev, "Network info create. Id=%d, handle=0x%p\n\n",
103 info->msg.id, info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100104
Davide Grohmann32660f92022-04-27 16:49:07 +0200105 /* Unlock the device mutex and wait for completion */
106 mutex_unlock(&info->edev->mutex);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100107 timeout = wait_for_completion_timeout(&info->done,
Davide Grohmann32660f92022-04-27 16:49:07 +0200108 msecs_to_jiffies(
109 NETWORK_INFO_RESP_TIMEOUT_MS));
110 mutex_lock(&info->edev->mutex);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100111
Davide Grohmann32660f92022-04-27 16:49:07 +0200112 if (0 == timeout) {
113 dev_warn(info->edev->dev, "Network info timed out.");
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100114
Davide Grohmann32660f92022-04-27 16:49:07 +0200115 ret = -ETIME;
116 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100117 }
118
Kristofer Jonssone3e26432022-06-28 11:29:42 +0200119 ret = info->errno;
120
Davide Grohmann32660f92022-04-27 16:49:07 +0200121deregister:
122 ethosu_mailbox_deregister(&info->edev->mailbox, &info->msg);
123 ethosu_network_put(info->net);
124
125kfree:
126 dev_info(info->edev->dev, "Network info destroy. Id=%d, handle=0x%p\n",
127 info->msg.id, info);
128 devm_kfree(info->edev->dev, info);
129
130 return ret;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100131}
132
133void ethosu_network_info_rsp(struct ethosu_device *edev,
134 struct ethosu_core_network_info_rsp *rsp)
135{
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100136 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +0200137 int id = (int)rsp->user_arg;
138 struct ethosu_mailbox_msg *msg;
139 struct ethosu_network_info *info;
140 uint32_t i;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100141
Davide Grohmann32660f92022-04-27 16:49:07 +0200142 msg = ethosu_mailbox_find(&edev->mailbox, id);
143 if (IS_ERR(msg)) {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100144 dev_warn(edev->dev,
Davide Grohmann32660f92022-04-27 16:49:07 +0200145 "Id for network info msg not found. Id=%d\n",
146 id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100147
148 return;
149 }
150
Davide Grohmann32660f92022-04-27 16:49:07 +0200151 info = container_of(msg, typeof(*info), msg);
152
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100153 if (completion_done(&info->done))
154 return;
155
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100156 info->errno = 0;
157
158 if (rsp->status != ETHOSU_CORE_STATUS_OK) {
159 info->errno = -EBADF;
160 goto signal_complete;
161 }
162
163 if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
164 info->errno = -ENFILE;
165 goto signal_complete;
166 }
167
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200168 ret = strscpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));
169 if (ret < 0) {
170 info->errno = ret;
171 goto signal_complete;
172 }
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100173
174 info->uapi->ifm_count = rsp->ifm_count;
175 for (i = 0; i < rsp->ifm_count; i++)
176 info->uapi->ifm_size[i] = rsp->ifm_size[i];
177
178 info->uapi->ofm_count = rsp->ofm_count;
179 for (i = 0; i < rsp->ofm_count; i++)
180 info->uapi->ofm_size[i] = rsp->ofm_size[i];
181
182signal_complete:
183 complete(&info->done);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100184}