blob: 9735ee2a5bba424f4c7c8322c8402a95f1785181 [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
27#include "ethosu_device.h"
28
29#include <linux/delay.h>
30#include <linux/dma-mapping.h>
31#include <linux/errno.h>
32
33/****************************************************************************
34 * Defines
35 ****************************************************************************/
36
37#define CAPABILITIES_RESP_TIMEOUT_MS 2000
38
39/****************************************************************************
40 * Functions
41 ****************************************************************************/
42
43static inline int ethosu_capabilities_send(struct ethosu_capabilities *cap)
44{
45 return ethosu_mailbox_capabilities_request(&cap->edev->mailbox,
46 &cap->msg);
47}
48
49static void ethosu_capabilities_fail(struct ethosu_mailbox_msg *msg)
50{
51 struct ethosu_capabilities *cap =
52 container_of(msg, typeof(*cap), msg);
53
54 if (completion_done(&cap->done))
55 return;
56
57 cap->errno = -EFAULT;
58 complete(&cap->done);
59}
60
Davide Grohmann32660f92022-04-27 16:49:07 +020061void ethosu_capability_rsp(struct ethosu_device *edev,
62 struct ethosu_core_msg_capabilities_rsp *rsp)
63{
64 int id = (int)rsp->user_arg;
65 struct ethosu_mailbox_msg *msg;
66 struct ethosu_capabilities *cap;
67
68 msg = ethosu_mailbox_find(&edev->mailbox, id);
69 if (IS_ERR(msg)) {
70 dev_warn(edev->dev,
71 "Id for capabilities msg not found. id=%d\n",
72 id);
73
74 return;
75 }
76
77 cap = container_of(msg, typeof(*cap), msg);
78
79 if (completion_done(&cap->done))
80 return;
81
82 cap->uapi->hw_id.version_status = rsp->version_status;
83 cap->uapi->hw_id.version_minor = rsp->version_minor;
84 cap->uapi->hw_id.version_major = rsp->version_major;
85 cap->uapi->hw_id.product_major = rsp->product_major;
86 cap->uapi->hw_id.arch_patch_rev = rsp->arch_patch_rev;
87 cap->uapi->hw_id.arch_minor_rev = rsp->arch_minor_rev;
88 cap->uapi->hw_id.arch_major_rev = rsp->arch_major_rev;
89 cap->uapi->driver_patch_rev = rsp->driver_patch_rev;
90 cap->uapi->driver_minor_rev = rsp->driver_minor_rev;
91 cap->uapi->driver_major_rev = rsp->driver_major_rev;
92 cap->uapi->hw_cfg.macs_per_cc = rsp->macs_per_cc;
93 cap->uapi->hw_cfg.cmd_stream_version = rsp->cmd_stream_version;
94 cap->uapi->hw_cfg.custom_dma = rsp->custom_dma;
95
96 cap->errno = 0;
97 complete(&cap->done);
98}
99
100int ethosu_capabilities_request(struct ethosu_device *edev,
101 struct ethosu_uapi_device_capabilities *uapi)
102{
103 struct ethosu_capabilities *cap;
104 int ret;
105 int timeout;
106
107 cap = devm_kzalloc(edev->dev, sizeof(struct ethosu_capabilities),
108 GFP_KERNEL);
109 if (!cap)
110 return -ENOMEM;
111
112 cap->edev = edev;
113 cap->uapi = uapi;
114 init_completion(&cap->done);
115 cap->msg.fail = ethosu_capabilities_fail;
Davide Grohmann32660f92022-04-27 16:49:07 +0200116
117 ret = ethosu_mailbox_register(&cap->edev->mailbox, &cap->msg);
118 if (ret < 0)
119 goto kfree;
120
121 dev_info(edev->dev, "Capabilities create. Id=%d, handle=0x%p\n",
122 cap->msg.id, cap);
123
124 ret = ethosu_capabilities_send(cap);
125 if (0 != ret)
126 goto deregister;
127
128 /* Unlock the mutex before going to block on the condition */
129 mutex_unlock(&edev->mutex);
130
131 /* wait for response to arrive back */
132 timeout = wait_for_completion_timeout(&cap->done,
133 msecs_to_jiffies(
134 CAPABILITIES_RESP_TIMEOUT_MS));
135
136 /* take back the mutex before resuming to do anything */
137 mutex_lock(&edev->mutex);
138
139 if (0 == timeout) {
140 dev_warn(edev->dev, "Capabilities response timeout");
141 ret = -ETIME;
142 goto deregister;
143 }
144
145 if (cap->errno) {
146 ret = cap->errno;
147 goto deregister;
148 }
149
150deregister:
151 ethosu_mailbox_deregister(&cap->edev->mailbox, &cap->msg);
152
153kfree:
154 dev_info(cap->edev->dev, "Capabilities destroy. Id=%d, handle=0x%p\n",
155 cap->msg.id, cap);
156 devm_kfree(cap->edev->dev, cap);
157
158 return ret;
159}