blob: 24be6770f5cb6c19424aba1b90ab94b3dd3c6ee9 [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
32/****************************************************************************
33 * Functions
34 ****************************************************************************/
35
36static void ethosu_network_info_destroy(struct kref *kref)
37{
38 struct ethosu_network_info *info =
39 container_of(kref, struct ethosu_network_info, kref);
40
41 dev_info(info->edev->dev, "Network info destroy. handle=0x%pK\n", info);
42
43 list_del(&info->list);
44
45 ethosu_network_put(info->net);
46
47 devm_kfree(info->edev->dev, info);
48}
49
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010050static int ethosu_network_info_send(struct ethosu_network_info *info)
51{
52 int ret;
53
54 /* Send network info request to firmware */
55 ret = ethosu_mailbox_network_info_request(&info->edev->mailbox,
56 info,
57 info->net->buf,
58 info->net->index);
59 if (ret)
60 return ret;
61
62 /* Increase reference count for the pending network info response */
63 ethosu_network_info_get(info);
64
65 return 0;
66}
67
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010068struct ethosu_network_info *ethosu_network_info_create(
69 struct ethosu_device *edev,
70 struct ethosu_network *net,
71 struct ethosu_uapi_network_info *uapi)
72{
73 struct ethosu_network_info *info;
74 int ret;
75
76 info = devm_kzalloc(edev->dev, sizeof(*info), GFP_KERNEL);
77 if (!info)
78 return ERR_PTR(-ENOMEM);
79
80 info->edev = edev;
81 info->net = net;
82 info->uapi = uapi;
83 kref_init(&info->kref);
84 init_completion(&info->done);
85
86 /* Insert network info to network info list */
87 list_add(&info->list, &edev->network_info_list);
88
89 /* Get reference to network */
90 ethosu_network_get(net);
91
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010092 ret = ethosu_network_info_send(info);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010093 if (ret)
94 goto put_info;
95
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +010096 dev_info(edev->dev, "Network info create. handle=%p\n", info);
97
98 return info;
99
100put_info:
101 ethosu_network_info_put(info);
102
103 return ERR_PTR(ret);
104}
105
106static int ethosu_network_info_find(struct ethosu_network_info *info,
107 struct list_head *network_info_list)
108{
109 struct ethosu_network_info *cur;
110
111 list_for_each_entry(cur, network_info_list, list) {
112 if (cur == info)
113 return 0;
114 }
115
116 return -EINVAL;
117}
118
119void ethosu_network_info_get(struct ethosu_network_info *info)
120{
121 kref_get(&info->kref);
122}
123
124void ethosu_network_info_put(struct ethosu_network_info *info)
125{
126 kref_put(&info->kref, ethosu_network_info_destroy);
127}
128
129int ethosu_network_info_wait(struct ethosu_network_info *info,
130 int timeout_ms)
131{
132 int timeout;
133
134 timeout = wait_for_completion_timeout(&info->done,
135 msecs_to_jiffies(timeout_ms));
136
137 if (!timeout) {
138 dev_warn(info->edev->dev,
139 "Network info timed out.");
140
141 return -ETIME;
142 }
143
144 return info->errno;
145}
146
147void ethosu_network_info_rsp(struct ethosu_device *edev,
148 struct ethosu_core_network_info_rsp *rsp)
149{
150 struct ethosu_network_info *info =
151 (struct ethosu_network_info *)rsp->user_arg;
152 uint32_t i;
153 int ret;
154
155 ret = ethosu_network_info_find(info, &edev->network_info_list);
156 if (0 != ret) {
157 dev_warn(edev->dev,
158 "Handle not found in network info list. handle=0x%p\n",
159 info);
160
161 return;
162 }
163
164 info->errno = 0;
165
166 if (rsp->status != ETHOSU_CORE_STATUS_OK) {
167 info->errno = -EBADF;
168 goto signal_complete;
169 }
170
171 if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) {
172 info->errno = -ENFILE;
173 goto signal_complete;
174 }
175
176 strncpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc));
177
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);
188
189 ethosu_network_info_put(info);
190}