blob: 5469b6c0618aba7f26f42c1cbad528c629a9a35d [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
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
Davide Grohmann32660f92022-04-27 16:49:07 +020055int ethosu_network_info_request(struct ethosu_network *net,
56 struct ethosu_uapi_network_info *uapi)
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010057{
58 struct ethosu_network_info *info;
59 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +020060 int timeout;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010061
Davide Grohmann32660f92022-04-27 16:49:07 +020062 info = devm_kzalloc(net->edev->dev, sizeof(*info), GFP_KERNEL);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010063 if (!info)
Davide Grohmann32660f92022-04-27 16:49:07 +020064 return -ENOMEM;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010065
Davide Grohmann32660f92022-04-27 16:49:07 +020066 info->edev = net->edev;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010067 info->net = net;
68 info->uapi = uapi;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010069 init_completion(&info->done);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010070 info->msg.fail = ethosu_network_info_fail;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010071
Davide Grohmann32660f92022-04-27 16:49:07 +020072 ret = ethosu_mailbox_register(&info->edev->mailbox, &info->msg);
73 if (ret < 0)
74 goto kfree;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010075
76 /* Get reference to network */
Davide Grohmann32660f92022-04-27 16:49:07 +020077 ethosu_network_get(info->net);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010078
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010079 ret = ethosu_network_info_send(info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010080 if (ret)
Davide Grohmann32660f92022-04-27 16:49:07 +020081 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010082
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020083 dev_info(info->edev->dev,
84 "Network info create. info=0x%pK, net=0x%pK, msg.id=0x%x\n",
85 info, info->net, info->msg.id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010086
Davide Grohmann32660f92022-04-27 16:49:07 +020087 /* Unlock the device mutex and wait for completion */
88 mutex_unlock(&info->edev->mutex);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010089 timeout = wait_for_completion_timeout(&info->done,
Davide Grohmann32660f92022-04-27 16:49:07 +020090 msecs_to_jiffies(
91 NETWORK_INFO_RESP_TIMEOUT_MS));
92 mutex_lock(&info->edev->mutex);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010093
Davide Grohmann32660f92022-04-27 16:49:07 +020094 if (0 == timeout) {
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020095 dev_warn(info->edev->dev, "Network info timed out. info=0x%pK",
96 info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010097
Davide Grohmann32660f92022-04-27 16:49:07 +020098 ret = -ETIME;
99 goto deregister;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100100 }
101
Kristofer Jonssone3e26432022-06-28 11:29:42 +0200102 ret = info->errno;
103
Davide Grohmann32660f92022-04-27 16:49:07 +0200104deregister:
105 ethosu_mailbox_deregister(&info->edev->mailbox, &info->msg);
106 ethosu_network_put(info->net);
107
108kfree:
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200109 dev_info(info->edev->dev,
110 "Network info destroy. info=0x%pK, msg.id=0x%x\n",
111 info, info->msg.id);
Davide Grohmann32660f92022-04-27 16:49:07 +0200112 devm_kfree(info->edev->dev, info);
113
114 return ret;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100115}
116
117void ethosu_network_info_rsp(struct ethosu_device *edev,
118 struct ethosu_core_network_info_rsp *rsp)
119{
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100120 int ret;
Davide Grohmann32660f92022-04-27 16:49:07 +0200121 int id = (int)rsp->user_arg;
122 struct ethosu_mailbox_msg *msg;
123 struct ethosu_network_info *info;
124 uint32_t i;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100125
Davide Grohmann32660f92022-04-27 16:49:07 +0200126 msg = ethosu_mailbox_find(&edev->mailbox, id);
127 if (IS_ERR(msg)) {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100128 dev_warn(edev->dev,
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +0200129 "Id for network info msg not found. msg.id=0x%x\n",
Davide Grohmann32660f92022-04-27 16:49:07 +0200130 id);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100131
132 return;
133 }
134
Davide Grohmann32660f92022-04-27 16:49:07 +0200135 info = container_of(msg, typeof(*info), msg);
136
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100137 if (completion_done(&info->done))
138 return;
139
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100140 info->errno = 0;
141
142 if (rsp->status != ETHOSU_CORE_STATUS_OK) {
143 info->errno = -EBADF;
144 goto signal_complete;
145 }
146
147 if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
148 info->errno = -ENFILE;
149 goto signal_complete;
150 }
151
Davide Grohmann6ab0b6b2022-05-17 16:11:14 +0200152 ret = strscpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));
153 if (ret < 0) {
154 info->errno = ret;
155 goto signal_complete;
156 }
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100157
158 info->uapi->ifm_count = rsp->ifm_count;
159 for (i = 0; i < rsp->ifm_count; i++)
160 info->uapi->ifm_size[i] = rsp->ifm_size[i];
161
162 info->uapi->ofm_count = rsp->ofm_count;
163 for (i = 0; i < rsp->ofm_count; i++)
164 info->uapi->ofm_size[i] = rsp->ofm_size[i];
165
166signal_complete:
167 complete(&info->done);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100168}