blob: 73ce2dc924e8b6f5a356f3f139a2e41c0ae5e5b9 [file] [log] [blame]
Davide Grohmann32660f92022-04-27 16:49:07 +02001/*
Mikael Olssond4ad9e52024-02-07 11:22:26 +01002 * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Ledion Dajaedd25502023-10-17 09:15:32 +02003 * SPDX-License-Identifier: GPL-2.0-only
Davide Grohmann32660f92022-04-27 16:49:07 +02004 *
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 *
Davide Grohmann32660f92022-04-27 16:49:07 +020019 */
20
21/****************************************************************************
22 * Includes
23 ****************************************************************************/
24
Mikael Olssond4ad9e52024-02-07 11:22:26 +010025#include <rpmsg/ethosu_rpmsg_capabilities.h>
Davide Grohmann32660f92022-04-27 16:49:07 +020026
Mikael Olssond4ad9e52024-02-07 11:22:26 +010027#include <common/ethosu_device.h>
28#include <rpmsg/ethosu_rpmsg.h>
Davide Grohmann32660f92022-04-27 16:49:07 +020029
30#include <linux/delay.h>
31#include <linux/dma-mapping.h>
32#include <linux/errno.h>
33
34/****************************************************************************
35 * Defines
36 ****************************************************************************/
37
38#define CAPABILITIES_RESP_TIMEOUT_MS 2000
39
40/****************************************************************************
41 * Functions
42 ****************************************************************************/
43
Kristofer Jonssonec477042023-01-20 13:38:13 +010044static inline int ethosu_capabilities_send(struct ethosu_capabilities *cap,
45 struct ethosu_mailbox *mailbox)
Davide Grohmann32660f92022-04-27 16:49:07 +020046{
Kristofer Jonssonec477042023-01-20 13:38:13 +010047 return ethosu_mailbox_capabilities_request(mailbox,
Davide Grohmann32660f92022-04-27 16:49:07 +020048 &cap->msg);
49}
50
51static void ethosu_capabilities_fail(struct ethosu_mailbox_msg *msg)
52{
53 struct ethosu_capabilities *cap =
54 container_of(msg, typeof(*cap), msg);
55
56 if (completion_done(&cap->done))
57 return;
58
59 cap->errno = -EFAULT;
60 complete(&cap->done);
61}
62
Kristofer Jonssonec477042023-01-20 13:38:13 +010063void ethosu_capability_rsp(struct ethosu_mailbox *mailbox,
Kristofer Jonssond779a082023-01-04 17:09:47 +010064 int msg_id,
Davide Grohmann32660f92022-04-27 16:49:07 +020065 struct ethosu_core_msg_capabilities_rsp *rsp)
66{
Kristofer Jonssonec477042023-01-20 13:38:13 +010067 struct device *dev = mailbox->dev;
Davide Grohmann32660f92022-04-27 16:49:07 +020068 struct ethosu_mailbox_msg *msg;
69 struct ethosu_capabilities *cap;
70
Mikael Olsson09965b02023-06-13 12:17:04 +020071 msg = ethosu_mailbox_find(mailbox, msg_id,
72 ETHOSU_CORE_MSG_CAPABILITIES_REQ);
Davide Grohmann32660f92022-04-27 16:49:07 +020073 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +010074 dev_warn(dev,
Mikael Olsson09965b02023-06-13 12:17:04 +020075 "Id for capabilities msg not found. Id=0x%0x: %ld\n",
76 msg_id, PTR_ERR(msg));
Davide Grohmann32660f92022-04-27 16:49:07 +020077
78 return;
79 }
80
81 cap = container_of(msg, typeof(*cap), msg);
82
83 if (completion_done(&cap->done))
84 return;
85
86 cap->uapi->hw_id.version_status = rsp->version_status;
87 cap->uapi->hw_id.version_minor = rsp->version_minor;
88 cap->uapi->hw_id.version_major = rsp->version_major;
89 cap->uapi->hw_id.product_major = rsp->product_major;
90 cap->uapi->hw_id.arch_patch_rev = rsp->arch_patch_rev;
91 cap->uapi->hw_id.arch_minor_rev = rsp->arch_minor_rev;
92 cap->uapi->hw_id.arch_major_rev = rsp->arch_major_rev;
93 cap->uapi->driver_patch_rev = rsp->driver_patch_rev;
94 cap->uapi->driver_minor_rev = rsp->driver_minor_rev;
95 cap->uapi->driver_major_rev = rsp->driver_major_rev;
96 cap->uapi->hw_cfg.macs_per_cc = rsp->macs_per_cc;
97 cap->uapi->hw_cfg.cmd_stream_version = rsp->cmd_stream_version;
98 cap->uapi->hw_cfg.custom_dma = rsp->custom_dma;
99
100 cap->errno = 0;
101 complete(&cap->done);
102}
103
Kristofer Jonssonec477042023-01-20 13:38:13 +0100104int ethosu_capabilities_request(struct device *dev,
105 struct ethosu_mailbox *mailbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200106 struct ethosu_uapi_device_capabilities *uapi)
107{
108 struct ethosu_capabilities *cap;
109 int ret;
110 int timeout;
111
Kristofer Jonssonec477042023-01-20 13:38:13 +0100112 cap = devm_kzalloc(dev, sizeof(struct ethosu_capabilities),
Davide Grohmann32660f92022-04-27 16:49:07 +0200113 GFP_KERNEL);
114 if (!cap)
115 return -ENOMEM;
116
Kristofer Jonssonec477042023-01-20 13:38:13 +0100117 cap->dev = dev;
Davide Grohmann32660f92022-04-27 16:49:07 +0200118 cap->uapi = uapi;
119 init_completion(&cap->done);
120 cap->msg.fail = ethosu_capabilities_fail;
Davide Grohmann32660f92022-04-27 16:49:07 +0200121
Kristofer Jonssonec477042023-01-20 13:38:13 +0100122 ret = ethosu_mailbox_register(mailbox, &cap->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200123 if (ret < 0)
124 goto kfree;
125
Ledion Dajaedd25502023-10-17 09:15:32 +0200126 dev_dbg(dev, "Capabilities create. Id=%d, handle=0x%p\n",
127 cap->msg.id, cap);
Davide Grohmann32660f92022-04-27 16:49:07 +0200128
Kristofer Jonssonec477042023-01-20 13:38:13 +0100129 ret = ethosu_capabilities_send(cap, mailbox);
Davide Grohmann32660f92022-04-27 16:49:07 +0200130 if (0 != ret)
131 goto deregister;
132
133 /* Unlock the mutex before going to block on the condition */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100134 device_unlock(dev);
Davide Grohmann32660f92022-04-27 16:49:07 +0200135
136 /* wait for response to arrive back */
137 timeout = wait_for_completion_timeout(&cap->done,
138 msecs_to_jiffies(
139 CAPABILITIES_RESP_TIMEOUT_MS));
140
141 /* take back the mutex before resuming to do anything */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100142 device_lock(dev);
Davide Grohmann32660f92022-04-27 16:49:07 +0200143
144 if (0 == timeout) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100145 dev_warn(dev, "Capabilities response timeout");
Davide Grohmann32660f92022-04-27 16:49:07 +0200146 ret = -ETIME;
147 goto deregister;
148 }
149
150 if (cap->errno) {
151 ret = cap->errno;
152 goto deregister;
153 }
154
155deregister:
Kristofer Jonssonec477042023-01-20 13:38:13 +0100156 ethosu_mailbox_deregister(mailbox, &cap->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200157
158kfree:
Ledion Dajaedd25502023-10-17 09:15:32 +0200159 dev_dbg(dev, "Capabilities destroy. Id=%d, handle=0x%p\n",
160 cap->msg.id, cap);
Kristofer Jonssonec477042023-01-20 13:38:13 +0100161 devm_kfree(dev, cap);
Davide Grohmann32660f92022-04-27 16:49:07 +0200162
163 return ret;
164}