blob: 57db452082a16b2284d8673def6074aaeba56f2a [file] [log] [blame]
Davide Grohmann32660f92022-04-27 16:49:07 +02001/*
Kristofer Jonssonb42bc0b2023-01-04 17:09:28 +01002 * Copyright 2022-2023 Arm Limited and/or its affiliates
Davide Grohmann32660f92022-04-27 16:49:07 +02003 *
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_capabilities.h"
26
Kristofer Jonssond779a082023-01-04 17:09:47 +010027#include "ethosu_core_rpmsg.h"
Davide Grohmann32660f92022-04-27 16:49:07 +020028#include "ethosu_device.h"
29
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
44static inline int ethosu_capabilities_send(struct ethosu_capabilities *cap)
45{
46 return ethosu_mailbox_capabilities_request(&cap->edev->mailbox,
47 &cap->msg);
48}
49
50static void ethosu_capabilities_fail(struct ethosu_mailbox_msg *msg)
51{
52 struct ethosu_capabilities *cap =
53 container_of(msg, typeof(*cap), msg);
54
55 if (completion_done(&cap->done))
56 return;
57
58 cap->errno = -EFAULT;
59 complete(&cap->done);
60}
61
Davide Grohmann32660f92022-04-27 16:49:07 +020062void ethosu_capability_rsp(struct ethosu_device *edev,
Kristofer Jonssond779a082023-01-04 17:09:47 +010063 int msg_id,
Davide Grohmann32660f92022-04-27 16:49:07 +020064 struct ethosu_core_msg_capabilities_rsp *rsp)
65{
Davide Grohmann32660f92022-04-27 16:49:07 +020066 struct ethosu_mailbox_msg *msg;
67 struct ethosu_capabilities *cap;
68
Kristofer Jonssond779a082023-01-04 17:09:47 +010069 msg = ethosu_mailbox_find(&edev->mailbox, msg_id);
Davide Grohmann32660f92022-04-27 16:49:07 +020070 if (IS_ERR(msg)) {
71 dev_warn(edev->dev,
72 "Id for capabilities msg not found. id=%d\n",
Kristofer Jonssond779a082023-01-04 17:09:47 +010073 msg_id);
Davide Grohmann32660f92022-04-27 16:49:07 +020074
75 return;
76 }
77
78 cap = container_of(msg, typeof(*cap), msg);
79
80 if (completion_done(&cap->done))
81 return;
82
83 cap->uapi->hw_id.version_status = rsp->version_status;
84 cap->uapi->hw_id.version_minor = rsp->version_minor;
85 cap->uapi->hw_id.version_major = rsp->version_major;
86 cap->uapi->hw_id.product_major = rsp->product_major;
87 cap->uapi->hw_id.arch_patch_rev = rsp->arch_patch_rev;
88 cap->uapi->hw_id.arch_minor_rev = rsp->arch_minor_rev;
89 cap->uapi->hw_id.arch_major_rev = rsp->arch_major_rev;
90 cap->uapi->driver_patch_rev = rsp->driver_patch_rev;
91 cap->uapi->driver_minor_rev = rsp->driver_minor_rev;
92 cap->uapi->driver_major_rev = rsp->driver_major_rev;
93 cap->uapi->hw_cfg.macs_per_cc = rsp->macs_per_cc;
94 cap->uapi->hw_cfg.cmd_stream_version = rsp->cmd_stream_version;
95 cap->uapi->hw_cfg.custom_dma = rsp->custom_dma;
96
97 cap->errno = 0;
98 complete(&cap->done);
99}
100
101int ethosu_capabilities_request(struct ethosu_device *edev,
102 struct ethosu_uapi_device_capabilities *uapi)
103{
104 struct ethosu_capabilities *cap;
105 int ret;
106 int timeout;
107
108 cap = devm_kzalloc(edev->dev, sizeof(struct ethosu_capabilities),
109 GFP_KERNEL);
110 if (!cap)
111 return -ENOMEM;
112
113 cap->edev = edev;
114 cap->uapi = uapi;
115 init_completion(&cap->done);
116 cap->msg.fail = ethosu_capabilities_fail;
Davide Grohmann32660f92022-04-27 16:49:07 +0200117
118 ret = ethosu_mailbox_register(&cap->edev->mailbox, &cap->msg);
119 if (ret < 0)
120 goto kfree;
121
122 dev_info(edev->dev, "Capabilities create. Id=%d, handle=0x%p\n",
123 cap->msg.id, cap);
124
125 ret = ethosu_capabilities_send(cap);
126 if (0 != ret)
127 goto deregister;
128
129 /* Unlock the mutex before going to block on the condition */
130 mutex_unlock(&edev->mutex);
131
132 /* wait for response to arrive back */
133 timeout = wait_for_completion_timeout(&cap->done,
134 msecs_to_jiffies(
135 CAPABILITIES_RESP_TIMEOUT_MS));
136
137 /* take back the mutex before resuming to do anything */
138 mutex_lock(&edev->mutex);
139
140 if (0 == timeout) {
141 dev_warn(edev->dev, "Capabilities response timeout");
142 ret = -ETIME;
143 goto deregister;
144 }
145
146 if (cap->errno) {
147 ret = cap->errno;
148 goto deregister;
149 }
150
151deregister:
152 ethosu_mailbox_deregister(&cap->edev->mailbox, &cap->msg);
153
154kfree:
155 dev_info(cap->edev->dev, "Capabilities destroy. Id=%d, handle=0x%p\n",
156 cap->msg.id, cap);
157 devm_kfree(cap->edev->dev, cap);
158
159 return ret;
160}