blob: 0e205db54e40d113ccd050319d4d7aca5550a6a2 [file] [log] [blame]
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01001/*
Kristofer Jonssonb42bc0b2023-01-04 17:09:28 +01002 * Copyright 2022-2023 Arm Limited and/or its affiliates
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01003 *
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
Kristofer Jonssonec477042023-01-20 13:38:13 +010034static inline int ethosu_network_info_send(struct ethosu_network_info *info,
35 struct ethosu_mailbox *mailbox)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010036{
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010037 /* Send network info request to firmware */
Kristofer Jonssonec477042023-01-20 13:38:13 +010038 return ethosu_mailbox_network_info_request(mailbox,
Davide Grohmann32660f92022-04-27 16:49:07 +020039 &info->msg,
40 info->net->buf,
41 info->net->index);
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
Kristofer Jonssonec477042023-01-20 13:38:13 +010086 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020087 "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:
Kristofer Jonssonec477042023-01-20 13:38:13 +0100112 dev_info(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200113 "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;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100129
Kristofer Jonssonec477042023-01-20 13:38:13 +0100130 msg = ethosu_mailbox_find(mailbox, msg_id);
Davide Grohmann32660f92022-04-27 16:49:07 +0200131 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100132 dev_warn(dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200133 "Id for network info msg not found. msg.id=0x%x\n",
Kristofer Jonssond779a082023-01-04 17:09:47 +0100134 msg_id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100135
136 return;
137 }
138
Davide Grohmann32660f92022-04-27 16:49:07 +0200139 info = container_of(msg, typeof(*info), msg);
140
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100141 if (completion_done(&info->done))
142 return;
143
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100144 info->errno = 0;
145
146 if (rsp->status != ETHOSU_CORE_STATUS_OK) {
147 info->errno = -EBADF;
148 goto signal_complete;
149 }
150
151 if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
152 info->errno = -ENFILE;
153 goto signal_complete;
154 }
155
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200156 ret = strscpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));
157 if (ret < 0) {
158 info->errno = ret;
159 goto signal_complete;
160 }
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100161
162 info->uapi->ifm_count = rsp->ifm_count;
163 for (i = 0; i < rsp->ifm_count; i++)
164 info->uapi->ifm_size[i] = rsp->ifm_size[i];
165
166 info->uapi->ofm_count = rsp->ofm_count;
167 for (i = 0; i < rsp->ofm_count; i++)
168 info->uapi->ofm_size[i] = rsp->ofm_size[i];
169
170signal_complete:
171 complete(&info->done);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100172}