summaryrefslogtreecommitdiff
path: root/sys/dev/firmware/arm/scmi_shmem.c
blob: 32c260c8a9ad52e439d9ef0abbcf79b4b1b74246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com>
 * Copyright (c) 2023 Arm Ltd
 *
 * This work was supported by Innovate UK project 105694, "Digital Security
 * by Design (DSbD) Technology Platform Prototype".
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>

#include <machine/atomic.h>

#include <dev/fdt/simplebus.h>
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus_subr.h>

#include "mmio_sram_if.h"

#include "scmi_shmem.h"
#include "scmi.h"

#define INFLIGHT_NONE	0
#define INFLIGHT_REQ	1

struct shmem_softc {
	device_t		dev;
	device_t		parent;
	int			reg;
	int			inflight;
};

static void	scmi_shmem_read(device_t, bus_size_t, void *, bus_size_t);
static void	scmi_shmem_write(device_t, bus_size_t, const void *,
				 bus_size_t);
static void	scmi_shmem_acquire_channel(struct shmem_softc *);
static void	scmi_shmem_release_channel(struct shmem_softc *);

static int	shmem_probe(device_t);
static int	shmem_attach(device_t);
static int	shmem_detach(device_t);

static int
shmem_probe(device_t dev)
{

	if (!ofw_bus_is_compatible(dev, "arm,scmi-shmem"))
		return (ENXIO);

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	device_set_desc(dev, "ARM SCMI Shared Memory driver");

	return (BUS_PROBE_DEFAULT);
}

static int
shmem_attach(device_t dev)
{
	struct shmem_softc *sc;
	phandle_t node;
	int reg;

	sc = device_get_softc(dev);
	sc->dev = dev;
	sc->parent = device_get_parent(dev);

	node = ofw_bus_get_node(dev);
	if (node == -1)
		return (ENXIO);

	OF_getencprop(node, "reg", &reg, sizeof(reg));

	sc->reg = reg;
	atomic_store_rel_int(&sc->inflight, INFLIGHT_NONE);

	OF_device_register_xref(OF_xref_from_node(node), dev);

	return (0);
}

static int
shmem_detach(device_t dev)
{

	return (0);
}

static void
scmi_shmem_read(device_t dev, bus_size_t offset, void *buf, bus_size_t len)
{
	struct shmem_softc *sc;
	uint8_t *addr;
	int i;

	sc = device_get_softc(dev);

	addr = (uint8_t *)buf;

	for (i = 0; i < len; i++)
		addr[i] = MMIO_SRAM_READ_1(sc->parent, sc->reg + offset + i);
}

static void
scmi_shmem_write(device_t dev, bus_size_t offset, const void *buf,
    bus_size_t len)
{
	struct shmem_softc *sc;
	const uint8_t *addr;
	int i;

	sc = device_get_softc(dev);

	addr = (const uint8_t *)buf;

	for (i = 0; i < len; i++)
		MMIO_SRAM_WRITE_1(sc->parent, sc->reg + offset + i, addr[i]);
}

device_t
scmi_shmem_get(device_t dev, phandle_t node, int index)
{
	phandle_t *shmems;
	device_t shmem_dev;
	size_t len;

	len = OF_getencprop_alloc_multi(node, "shmem", sizeof(*shmems),
	    (void **)&shmems);
	if (len <= 0) {
		device_printf(dev, "%s: Can't get shmem node.\n", __func__);
		return (NULL);
	}

	if (index >= len) {
		OF_prop_free(shmems);
		return (NULL);
	}

	shmem_dev = OF_device_from_xref(shmems[index]);
	if (shmem_dev == NULL)
		device_printf(dev, "%s: Can't get shmem device.\n",
		    __func__);

	OF_prop_free(shmems);

	return (shmem_dev);
}

static void
scmi_shmem_acquire_channel(struct shmem_softc *sc)
{

	 while ((atomic_cmpset_acq_int(&sc->inflight, INFLIGHT_NONE,
	     INFLIGHT_REQ)) == 0)
		DELAY(1000);
}

static void
scmi_shmem_release_channel(struct shmem_softc *sc)
{

	atomic_store_rel_int(&sc->inflight, INFLIGHT_NONE);
}

int
scmi_shmem_prepare_msg(device_t dev, uint8_t *msg, uint32_t tx_len,
		bool polling)
{
	struct shmem_softc *sc;
	struct scmi_smt_header hdr = {};
	uint32_t channel_status;

	sc = device_get_softc(dev);

	/* Get exclusive write access to channel */
	scmi_shmem_acquire_channel(sc);

	/* Read channel status */
	scmi_shmem_read(dev, SMT_OFFSET_CHAN_STATUS, &channel_status,
	    SMT_SIZE_CHAN_STATUS);
	if ((channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE) == 0) {
		scmi_shmem_release_channel(sc);
		device_printf(dev, "Shmem channel busy. Abort !.\n");
		return (1);
	}

	/* Update header */
	hdr.channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
	hdr.msg_header = htole32(*((uint32_t *)msg));
	hdr.length = htole32(tx_len);
	if (!polling)
		hdr.flags |= SCMI_SHMEM_FLAG_INTR_ENABLED;
	else
		hdr.flags &= ~SCMI_SHMEM_FLAG_INTR_ENABLED;

	/* Write header */
	scmi_shmem_write(dev, 0, &hdr, SMT_SIZE_HEADER);

	/* Write request payload if any */
	if (tx_len > SCMI_MSG_HDR_SIZE)
		scmi_shmem_write(dev, SMT_SIZE_HEADER,
		    &msg[SCMI_MSG_HDR_SIZE], tx_len - SCMI_MSG_HDR_SIZE);

	return (0);
}

void
scmi_shmem_clear_channel(device_t dev)
{
	uint32_t channel_status = 0;

	if (dev == NULL)
		return;

	channel_status |= SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
	scmi_shmem_write(dev, SMT_OFFSET_CHAN_STATUS, &channel_status,
	    SMT_SIZE_CHAN_STATUS);
}

int
scmi_shmem_read_msg_header(device_t dev, uint32_t *msg_header, unsigned int *rx_len)
{
	uint32_t length, header;

	/* Read and check length. */
	scmi_shmem_read(dev, SMT_OFFSET_LENGTH, &length, SMT_SIZE_LENGTH);
	if (le32toh(length) < sizeof(header))
		return (EINVAL);

	*rx_len = le32toh(length);
	/* Read header. */
	scmi_shmem_read(dev, SMT_OFFSET_MSG_HEADER, &header,
	    SMT_SIZE_MSG_HEADER);

	*msg_header = le32toh(header);

	return (0);
}

int
scmi_shmem_read_msg_payload(device_t dev, uint8_t *buf, uint32_t buf_len, uint32_t rx_len)
{
	uint32_t payld_len;

	payld_len = rx_len - SCMI_MSG_HDR_SIZE;
	if (payld_len > buf_len) {
		device_printf(dev,
		    "RX payload %dbytes exceeds buflen %dbytes. Truncate.\n",
		    payld_len, buf_len);
		payld_len = buf_len;
	}

	/* Read response payload */
	scmi_shmem_read(dev, SMT_SIZE_HEADER, buf, payld_len);

	return (0);
}

void
scmi_shmem_tx_complete(device_t dev)
{
	struct shmem_softc *sc;

	sc = device_get_softc(dev);
	scmi_shmem_release_channel(sc);
}

bool scmi_shmem_poll_msg(device_t dev, uint32_t *msg_header, uint32_t *rx_len)
{
	uint32_t status;
	bool ret;

	scmi_shmem_read(dev, SMT_OFFSET_CHAN_STATUS, &status,
	    SMT_SIZE_CHAN_STATUS);

	ret = (status & (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
	    SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE));
	if (ret)
		scmi_shmem_read_msg_header(dev, msg_header, rx_len);

	return (ret);
}

static device_method_t shmem_methods[] = {
	DEVMETHOD(device_probe,		shmem_probe),
	DEVMETHOD(device_attach,	shmem_attach),
	DEVMETHOD(device_detach,	shmem_detach),
	DEVMETHOD_END
};

DEFINE_CLASS_1(shmem, shmem_driver, shmem_methods, sizeof(struct shmem_softc),
    simplebus_driver);

EARLY_DRIVER_MODULE(shmem, mmio_sram, shmem_driver, 0, 0,
    BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
MODULE_VERSION(scmi_shmem, 1);