blob: 011a0b4868fbb813b3aca3489902221c0855e891 [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.
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010018 */
19
20/****************************************************************************
21 * Includes
22 ****************************************************************************/
23
24#include "ethosu_network_info.h"
25
26#include "ethosu_device.h"
27#include "ethosu_network.h"
28#include "ethosu_mailbox.h"
29#include "uapi/ethosu.h"
30
Mikael Olssonea209b62023-08-15 17:01:01 +020031#include <linux/bug.h>
32
Davide Grohmann32660f92022-04-27 16:49:07 +020033#define NETWORK_INFO_RESP_TIMEOUT_MS 3000
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010034
Kristofer Jonssonec477042023-01-20 13:38:13 +010035static inline int ethosu_network_info_send(struct ethosu_network_info *info,
36 struct ethosu_mailbox *mailbox)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010037{
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010038 /* Send network info request to firmware */
Kristofer Jonssonec477042023-01-20 13:38:13 +010039 return ethosu_mailbox_network_info_request(mailbox,
Davide Grohmann32660f92022-04-27 16:49:07 +020040 &info->msg,
Mikael Olssonc081e592023-10-30 11:10:56 +010041 info->net);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010042}
43
44static void ethosu_network_info_fail(struct ethosu_mailbox_msg *msg)
45{
46 struct ethosu_network_info *info =
47 container_of(msg, typeof(*info), msg);
48
49 if (completion_done(&info->done))
50 return;
51
52 info->errno = -EFAULT;
53 complete(&info->done);
54}
55
Kristofer Jonssonec477042023-01-20 13:38:13 +010056int ethosu_network_info_request(struct device *dev,
57 struct ethosu_mailbox *mailbox,
58 struct ethosu_network *net,
Davide Grohmann32660f92022-04-27 16:49:07 +020059 struct ethosu_uapi_network_info *uapi)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010060{
61 struct ethosu_network_info *info;
62 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +020063 int timeout;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010064
Kristofer Jonssonec477042023-01-20 13:38:13 +010065 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010066 if (!info)
Davide Grohmann32660f92022-04-27 16:49:07 +020067 return -ENOMEM;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010068
Kristofer Jonssonec477042023-01-20 13:38:13 +010069 info->dev = dev;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010070 info->net = net;
71 info->uapi = uapi;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010072 init_completion(&info->done);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010073 info->msg.fail = ethosu_network_info_fail;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010074
Kristofer Jonssonec477042023-01-20 13:38:13 +010075 ret = ethosu_mailbox_register(mailbox, &info->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +020076 if (ret < 0)
77 goto kfree;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010078
79 /* Get reference to network */
Davide Grohmann32660f92022-04-27 16:49:07 +020080 ethosu_network_get(info->net);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010081
Kristofer Jonssonec477042023-01-20 13:38:13 +010082 ret = ethosu_network_info_send(info, mailbox);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010083 if (ret)
Davide Grohmann32660f92022-04-27 16:49:07 +020084 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010085
Ledion Dajaedd25502023-10-17 09:15:32 +020086 dev_dbg(dev,
87 "Network info create. info=0x%pK, net=0x%pK, msg.id=0x%x\n",
88 info, info->net, info->msg.id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010089
Davide Grohmann32660f92022-04-27 16:49:07 +020090 /* Unlock the device mutex and wait for completion */
Kristofer Jonssonec477042023-01-20 13:38:13 +010091 device_unlock(dev);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010092 timeout = wait_for_completion_timeout(&info->done,
Davide Grohmann32660f92022-04-27 16:49:07 +020093 msecs_to_jiffies(
94 NETWORK_INFO_RESP_TIMEOUT_MS));
Kristofer Jonssonec477042023-01-20 13:38:13 +010095 device_lock(dev);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010096
Davide Grohmann32660f92022-04-27 16:49:07 +020097 if (0 == timeout) {
Kristofer Jonssonec477042023-01-20 13:38:13 +010098 dev_warn(dev, "Network info timed out. info=0x%pK",
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020099 info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100100
Davide Grohmann32660f92022-04-27 16:49:07 +0200101 ret = -ETIME;
102 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100103 }
104
Kristofer Jonssone3e26432022-06-28 11:29:42 +0200105 ret = info->errno;
106
Davide Grohmann32660f92022-04-27 16:49:07 +0200107deregister:
Kristofer Jonssonec477042023-01-20 13:38:13 +0100108 ethosu_mailbox_deregister(mailbox, &info->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200109 ethosu_network_put(info->net);
110
111kfree:
Ledion Dajaedd25502023-10-17 09:15:32 +0200112 dev_dbg(dev,
113 "Network info destroy. info=0x%pK, msg.id=0x%x\n",
114 info, info->msg.id);
Kristofer Jonssonec477042023-01-20 13:38:13 +0100115 devm_kfree(dev, info);
Davide Grohmann32660f92022-04-27 16:49:07 +0200116
117 return ret;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100118}
119
Kristofer Jonssonec477042023-01-20 13:38:13 +0100120void ethosu_network_info_rsp(struct ethosu_mailbox *mailbox,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100121 int msg_id,
122 struct ethosu_core_msg_network_info_rsp *rsp)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100123{
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100124 int ret;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100125 struct device *dev = mailbox->dev;
Davide Grohmann32660f92022-04-27 16:49:07 +0200126 struct ethosu_mailbox_msg *msg;
127 struct ethosu_network_info *info;
128 uint32_t i;
Mikael Olssonea209b62023-08-15 17:01:01 +0200129 const size_t rsp_desc_size = sizeof(rsp->desc);
130
131 BUILD_BUG_ON(rsp_desc_size != sizeof(info->uapi->desc));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100132
Mikael Olsson09965b02023-06-13 12:17:04 +0200133 msg = ethosu_mailbox_find(mailbox, msg_id,
134 ETHOSU_CORE_MSG_NETWORK_INFO_REQ);
Davide Grohmann32660f92022-04-27 16:49:07 +0200135 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100136 dev_warn(dev,
Mikael Olsson09965b02023-06-13 12:17:04 +0200137 "Id for network info msg not found. Id=0x%x: %ld\n",
138 msg_id, PTR_ERR(msg));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100139
140 return;
141 }
142
Davide Grohmann32660f92022-04-27 16:49:07 +0200143 info = container_of(msg, typeof(*info), msg);
144
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100145 if (completion_done(&info->done))
146 return;
147
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100148 info->errno = 0;
149
150 if (rsp->status != ETHOSU_CORE_STATUS_OK) {
Mikael Olsson42699b02023-08-16 11:25:12 +0200151 dev_err(dev, "Failed to get information about the network\n");
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100152 info->errno = -EBADF;
153 goto signal_complete;
154 }
155
156 if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
Mikael Olsson42699b02023-08-16 11:25:12 +0200157 dev_err(dev,
158 "Invalid number of IFMs/OFMs in network info: IFMs=%u OFMs=%u\n",
159 rsp->ifm_count, rsp->ofm_count);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100160 info->errno = -ENFILE;
161 goto signal_complete;
162 }
163
Mikael Olssonea209b62023-08-15 17:01:01 +0200164 if (strnlen(rsp->desc, rsp_desc_size) == rsp_desc_size) {
165 dev_err(dev,
166 "Description in network info is not null-terminated\n");
167 info->errno = -EMSGSIZE;
168 goto signal_complete;
169 }
170
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200171 ret = strscpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));
172 if (ret < 0) {
Mikael Olssonea209b62023-08-15 17:01:01 +0200173 dev_err(dev, "Failed to copy network info description\n");
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200174 info->errno = ret;
175 goto signal_complete;
176 }
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100177
178 info->uapi->ifm_count = rsp->ifm_count;
179 for (i = 0; i < rsp->ifm_count; i++)
180 info->uapi->ifm_size[i] = rsp->ifm_size[i];
181
182 info->uapi->ofm_count = rsp->ofm_count;
183 for (i = 0; i < rsp->ofm_count; i++)
184 info->uapi->ofm_size[i] = rsp->ofm_size[i];
185
186signal_complete:
187 complete(&info->done);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100188}