blob: feabcae4a4e0578698f967824769c600e29cfbee [file] [log] [blame]
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01001/*
Ledion Dajaedd25502023-10-17 09:15:32 +02002 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
3 * SPDX-License-Identifier: GPL-2.0-only
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01004 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
18 *
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010019 */
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
Mikael Olssonea209b62023-08-15 17:01:01 +020032#include <linux/bug.h>
33
Davide Grohmann32660f92022-04-27 16:49:07 +020034#define NETWORK_INFO_RESP_TIMEOUT_MS 3000
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010035
Kristofer Jonssonec477042023-01-20 13:38:13 +010036static inline int ethosu_network_info_send(struct ethosu_network_info *info,
37 struct ethosu_mailbox *mailbox)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010038{
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010039 /* Send network info request to firmware */
Kristofer Jonssonec477042023-01-20 13:38:13 +010040 return ethosu_mailbox_network_info_request(mailbox,
Davide Grohmann32660f92022-04-27 16:49:07 +020041 &info->msg,
42 info->net->buf,
43 info->net->index);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010044}
45
46static void ethosu_network_info_fail(struct ethosu_mailbox_msg *msg)
47{
48 struct ethosu_network_info *info =
49 container_of(msg, typeof(*info), msg);
50
51 if (completion_done(&info->done))
52 return;
53
54 info->errno = -EFAULT;
55 complete(&info->done);
56}
57
Kristofer Jonssonec477042023-01-20 13:38:13 +010058int ethosu_network_info_request(struct device *dev,
59 struct ethosu_mailbox *mailbox,
60 struct ethosu_network *net,
Davide Grohmann32660f92022-04-27 16:49:07 +020061 struct ethosu_uapi_network_info *uapi)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010062{
63 struct ethosu_network_info *info;
64 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +020065 int timeout;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010066
Kristofer Jonssonec477042023-01-20 13:38:13 +010067 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010068 if (!info)
Davide Grohmann32660f92022-04-27 16:49:07 +020069 return -ENOMEM;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010070
Kristofer Jonssonec477042023-01-20 13:38:13 +010071 info->dev = dev;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010072 info->net = net;
73 info->uapi = uapi;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010074 init_completion(&info->done);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010075 info->msg.fail = ethosu_network_info_fail;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010076
Kristofer Jonssonec477042023-01-20 13:38:13 +010077 ret = ethosu_mailbox_register(mailbox, &info->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +020078 if (ret < 0)
79 goto kfree;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010080
81 /* Get reference to network */
Davide Grohmann32660f92022-04-27 16:49:07 +020082 ethosu_network_get(info->net);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010083
Kristofer Jonssonec477042023-01-20 13:38:13 +010084 ret = ethosu_network_info_send(info, mailbox);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010085 if (ret)
Davide Grohmann32660f92022-04-27 16:49:07 +020086 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010087
Ledion Dajaedd25502023-10-17 09:15:32 +020088 dev_dbg(dev,
89 "Network info create. info=0x%pK, net=0x%pK, msg.id=0x%x\n",
90 info, info->net, info->msg.id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010091
Davide Grohmann32660f92022-04-27 16:49:07 +020092 /* Unlock the device mutex and wait for completion */
Kristofer Jonssonec477042023-01-20 13:38:13 +010093 device_unlock(dev);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010094 timeout = wait_for_completion_timeout(&info->done,
Davide Grohmann32660f92022-04-27 16:49:07 +020095 msecs_to_jiffies(
96 NETWORK_INFO_RESP_TIMEOUT_MS));
Kristofer Jonssonec477042023-01-20 13:38:13 +010097 device_lock(dev);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010098
Davide Grohmann32660f92022-04-27 16:49:07 +020099 if (0 == timeout) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100100 dev_warn(dev, "Network info timed out. info=0x%pK",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200101 info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100102
Davide Grohmann32660f92022-04-27 16:49:07 +0200103 ret = -ETIME;
104 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100105 }
106
Kristofer Jonssone3e26432022-06-28 11:29:42 +0200107 ret = info->errno;
108
Davide Grohmann32660f92022-04-27 16:49:07 +0200109deregister:
Kristofer Jonssonec477042023-01-20 13:38:13 +0100110 ethosu_mailbox_deregister(mailbox, &info->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200111 ethosu_network_put(info->net);
112
113kfree:
Ledion Dajaedd25502023-10-17 09:15:32 +0200114 dev_dbg(dev,
115 "Network info destroy. info=0x%pK, msg.id=0x%x\n",
116 info, info->msg.id);
Kristofer Jonssonec477042023-01-20 13:38:13 +0100117 devm_kfree(dev, info);
Davide Grohmann32660f92022-04-27 16:49:07 +0200118
119 return ret;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100120}
121
Kristofer Jonssonec477042023-01-20 13:38:13 +0100122void ethosu_network_info_rsp(struct ethosu_mailbox *mailbox,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100123 int msg_id,
124 struct ethosu_core_msg_network_info_rsp *rsp)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100125{
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100126 int ret;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100127 struct device *dev = mailbox->dev;
Davide Grohmann32660f92022-04-27 16:49:07 +0200128 struct ethosu_mailbox_msg *msg;
129 struct ethosu_network_info *info;
130 uint32_t i;
Mikael Olssonea209b62023-08-15 17:01:01 +0200131 const size_t rsp_desc_size = sizeof(rsp->desc);
132
133 BUILD_BUG_ON(rsp_desc_size != sizeof(info->uapi->desc));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100134
Mikael Olsson09965b02023-06-13 12:17:04 +0200135 msg = ethosu_mailbox_find(mailbox, msg_id,
136 ETHOSU_CORE_MSG_NETWORK_INFO_REQ);
Davide Grohmann32660f92022-04-27 16:49:07 +0200137 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100138 dev_warn(dev,
Mikael Olsson09965b02023-06-13 12:17:04 +0200139 "Id for network info msg not found. Id=0x%x: %ld\n",
140 msg_id, PTR_ERR(msg));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100141
142 return;
143 }
144
Davide Grohmann32660f92022-04-27 16:49:07 +0200145 info = container_of(msg, typeof(*info), msg);
146
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100147 if (completion_done(&info->done))
148 return;
149
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100150 info->errno = 0;
151
152 if (rsp->status != ETHOSU_CORE_STATUS_OK) {
Mikael Olsson42699b02023-08-16 11:25:12 +0200153 dev_err(dev, "Failed to get information about the network\n");
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100154 info->errno = -EBADF;
155 goto signal_complete;
156 }
157
158 if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
Mikael Olsson42699b02023-08-16 11:25:12 +0200159 dev_err(dev,
160 "Invalid number of IFMs/OFMs in network info: IFMs=%u OFMs=%u\n",
161 rsp->ifm_count, rsp->ofm_count);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100162 info->errno = -ENFILE;
163 goto signal_complete;
164 }
165
Mikael Olssonea209b62023-08-15 17:01:01 +0200166 if (strnlen(rsp->desc, rsp_desc_size) == rsp_desc_size) {
167 dev_err(dev,
168 "Description in network info is not null-terminated\n");
169 info->errno = -EMSGSIZE;
170 goto signal_complete;
171 }
172
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200173 ret = strscpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));
174 if (ret < 0) {
Mikael Olssonea209b62023-08-15 17:01:01 +0200175 dev_err(dev, "Failed to copy network info description\n");
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200176 info->errno = ret;
177 goto signal_complete;
178 }
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100179
180 info->uapi->ifm_count = rsp->ifm_count;
181 for (i = 0; i < rsp->ifm_count; i++)
182 info->uapi->ifm_size[i] = rsp->ifm_size[i];
183
184 info->uapi->ofm_count = rsp->ofm_count;
185 for (i = 0; i < rsp->ofm_count; i++)
186 info->uapi->ofm_size[i] = rsp->ofm_size[i];
187
188signal_complete:
189 complete(&info->done);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100190}