blob: 30a9aef5772b933f18e4ce21e1c2cd1c96d73498 [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
Mikael Olsson16be2852024-02-12 09:56:56 +010044static inline int ethosu_rpmsg_capabilities_send(
45 struct ethosu_rpmsg_capabilities *cap,
46 struct ethosu_rpmsg_mailbox *mailbox)
Davide Grohmann32660f92022-04-27 16:49:07 +020047{
Mikael Olsson16be2852024-02-12 09:56:56 +010048 return ethosu_rpmsg_mailbox_capabilities_request(mailbox,
49 &cap->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +020050}
51
Mikael Olsson16be2852024-02-12 09:56:56 +010052static void ethosu_rpmsg_capabilities_fail(struct ethosu_rpmsg_mailbox_msg *msg)
Davide Grohmann32660f92022-04-27 16:49:07 +020053{
Mikael Olsson16be2852024-02-12 09:56:56 +010054 struct ethosu_rpmsg_capabilities *cap =
Davide Grohmann32660f92022-04-27 16:49:07 +020055 container_of(msg, typeof(*cap), msg);
56
57 if (completion_done(&cap->done))
58 return;
59
60 cap->errno = -EFAULT;
61 complete(&cap->done);
62}
63
Mikael Olsson16be2852024-02-12 09:56:56 +010064void ethosu_capability_rsp(struct ethosu_rpmsg_mailbox *mailbox,
Kristofer Jonssond779a082023-01-04 17:09:47 +010065 int msg_id,
Mikael Olsson16be2852024-02-12 09:56:56 +010066 struct ethosu_rpmsg_capabilities_rsp *rsp)
Davide Grohmann32660f92022-04-27 16:49:07 +020067{
Kristofer Jonssonec477042023-01-20 13:38:13 +010068 struct device *dev = mailbox->dev;
Mikael Olsson16be2852024-02-12 09:56:56 +010069 struct ethosu_rpmsg_mailbox_msg *msg;
70 struct ethosu_rpmsg_capabilities *cap;
Davide Grohmann32660f92022-04-27 16:49:07 +020071
Mikael Olsson16be2852024-02-12 09:56:56 +010072 msg = ethosu_rpmsg_mailbox_find(mailbox, msg_id,
73 ETHOSU_RPMSG_CAPABILITIES_REQ);
Davide Grohmann32660f92022-04-27 16:49:07 +020074 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +010075 dev_warn(dev,
Mikael Olsson09965b02023-06-13 12:17:04 +020076 "Id for capabilities msg not found. Id=0x%0x: %ld\n",
77 msg_id, PTR_ERR(msg));
Davide Grohmann32660f92022-04-27 16:49:07 +020078
79 return;
80 }
81
82 cap = container_of(msg, typeof(*cap), msg);
83
84 if (completion_done(&cap->done))
85 return;
86
87 cap->uapi->hw_id.version_status = rsp->version_status;
88 cap->uapi->hw_id.version_minor = rsp->version_minor;
89 cap->uapi->hw_id.version_major = rsp->version_major;
90 cap->uapi->hw_id.product_major = rsp->product_major;
91 cap->uapi->hw_id.arch_patch_rev = rsp->arch_patch_rev;
92 cap->uapi->hw_id.arch_minor_rev = rsp->arch_minor_rev;
93 cap->uapi->hw_id.arch_major_rev = rsp->arch_major_rev;
94 cap->uapi->driver_patch_rev = rsp->driver_patch_rev;
95 cap->uapi->driver_minor_rev = rsp->driver_minor_rev;
96 cap->uapi->driver_major_rev = rsp->driver_major_rev;
97 cap->uapi->hw_cfg.macs_per_cc = rsp->macs_per_cc;
98 cap->uapi->hw_cfg.cmd_stream_version = rsp->cmd_stream_version;
99 cap->uapi->hw_cfg.custom_dma = rsp->custom_dma;
Mikael Olssondc18cea2024-02-16 11:04:46 +0100100 cap->uapi->hw_cfg.type = ETHOSU_DEV_SUBSYSTEM;
Davide Grohmann32660f92022-04-27 16:49:07 +0200101
102 cap->errno = 0;
103 complete(&cap->done);
104}
105
Mikael Olsson16be2852024-02-12 09:56:56 +0100106int ethosu_rpmsg_capabilities_request(struct device *dev,
107 struct ethosu_rpmsg_mailbox *mailbox,
108 struct ethosu_uapi_device_capabilities *uapi)
Davide Grohmann32660f92022-04-27 16:49:07 +0200109{
Mikael Olsson16be2852024-02-12 09:56:56 +0100110 struct ethosu_rpmsg_capabilities *cap;
Davide Grohmann32660f92022-04-27 16:49:07 +0200111 int ret;
112 int timeout;
113
Mikael Olsson16be2852024-02-12 09:56:56 +0100114 cap = devm_kzalloc(dev, sizeof(struct ethosu_rpmsg_capabilities),
Davide Grohmann32660f92022-04-27 16:49:07 +0200115 GFP_KERNEL);
116 if (!cap)
117 return -ENOMEM;
118
Kristofer Jonssonec477042023-01-20 13:38:13 +0100119 cap->dev = dev;
Davide Grohmann32660f92022-04-27 16:49:07 +0200120 cap->uapi = uapi;
121 init_completion(&cap->done);
Mikael Olsson16be2852024-02-12 09:56:56 +0100122 cap->msg.fail = ethosu_rpmsg_capabilities_fail;
Davide Grohmann32660f92022-04-27 16:49:07 +0200123
Mikael Olsson16be2852024-02-12 09:56:56 +0100124 ret = ethosu_rpmsg_mailbox_register(mailbox, &cap->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200125 if (ret < 0)
126 goto kfree;
127
Ledion Dajaedd25502023-10-17 09:15:32 +0200128 dev_dbg(dev, "Capabilities create. Id=%d, handle=0x%p\n",
129 cap->msg.id, cap);
Davide Grohmann32660f92022-04-27 16:49:07 +0200130
Mikael Olsson16be2852024-02-12 09:56:56 +0100131 ret = ethosu_rpmsg_capabilities_send(cap, mailbox);
Davide Grohmann32660f92022-04-27 16:49:07 +0200132 if (0 != ret)
133 goto deregister;
134
135 /* Unlock the mutex before going to block on the condition */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100136 device_unlock(dev);
Davide Grohmann32660f92022-04-27 16:49:07 +0200137
138 /* wait for response to arrive back */
139 timeout = wait_for_completion_timeout(&cap->done,
140 msecs_to_jiffies(
141 CAPABILITIES_RESP_TIMEOUT_MS));
142
143 /* take back the mutex before resuming to do anything */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100144 device_lock(dev);
Davide Grohmann32660f92022-04-27 16:49:07 +0200145
146 if (0 == timeout) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100147 dev_warn(dev, "Capabilities response timeout");
Davide Grohmann32660f92022-04-27 16:49:07 +0200148 ret = -ETIME;
149 goto deregister;
150 }
151
152 if (cap->errno) {
153 ret = cap->errno;
154 goto deregister;
155 }
156
157deregister:
Mikael Olsson16be2852024-02-12 09:56:56 +0100158 ethosu_rpmsg_mailbox_deregister(mailbox, &cap->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200159
160kfree:
Ledion Dajaedd25502023-10-17 09:15:32 +0200161 dev_dbg(dev, "Capabilities destroy. Id=%d, handle=0x%p\n",
162 cap->msg.id, cap);
Kristofer Jonssonec477042023-01-20 13:38:13 +0100163 devm_kfree(dev, cap);
Davide Grohmann32660f92022-04-27 16:49:07 +0200164
165 return ret;
166}