summaryrefslogtreecommitdiff
path: root/sys/dev/firmware/arm/scmi_smc.c
blob: 81c66ad7bb46eb34722cc6716fd1fa9483e9d6cc (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
/*-
 * 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/cdefs.h>

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

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

#include <dev/psci/smccc.h>

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

struct scmi_smc_softc {
	struct scmi_softc	base;
	uint32_t		smc_id;
	device_t		a2p_dev;
};

static int	scmi_smc_transport_init(device_t);
static int	scmi_smc_xfer_msg(device_t, struct scmi_msg *);
static int	scmi_smc_poll_msg(device_t, struct scmi_msg *, unsigned int);
static int	scmi_smc_collect_reply(device_t, struct scmi_msg *);
static void	scmi_smc_tx_complete(device_t, void *);

static int	scmi_smc_probe(device_t);

static int
scmi_smc_transport_init(device_t dev)
{
	struct scmi_smc_softc *sc;
	phandle_t node;
	ssize_t len;

	sc = device_get_softc(dev);

	node = ofw_bus_get_node(dev);
	len = OF_getencprop(node, "arm,smc-id", &sc->smc_id,
	    sizeof(sc->smc_id));
	if (len <= 0) {
		device_printf(dev, "No SMC ID found\n");
		return (EINVAL);
	}

	device_printf(dev, "smc id %x\n", sc->smc_id);

	sc->a2p_dev = scmi_shmem_get(dev, node, SCMI_CHAN_A2P);
	if (sc->a2p_dev == NULL) {
		device_printf(dev, "A2P shmem dev not found.\n");
		return (ENXIO);
	}

	sc->base.trs_desc.no_completion_irq = true;
	sc->base.trs_desc.reply_timo_ms = 30;

	return (0);
}

static int
scmi_smc_xfer_msg(device_t dev, struct scmi_msg *msg)
{
	struct scmi_smc_softc *sc;
	int ret;

	sc = device_get_softc(dev);

	ret = scmi_shmem_prepare_msg(sc->a2p_dev, (uint8_t *)&msg->hdr,
	    msg->tx_len, msg->polling);
	if (ret != 0)
		return (ret);

	arm_smccc_invoke_smc(sc->smc_id, NULL);

	return (0);
}

static int
scmi_smc_poll_msg(device_t dev, struct scmi_msg *msg, unsigned int tmo)
{
	struct scmi_smc_softc *sc;

	sc = device_get_softc(dev);

	/*
	 * Nothing to poll since commands are completed as soon as smc
	 * returns ... but did we get back what we were poling for ?
	 */
	scmi_shmem_read_msg_header(sc->a2p_dev, &msg->hdr, &msg->rx_len);

	return (0);
}

static int
scmi_smc_collect_reply(device_t dev, struct scmi_msg *msg)
{
	struct scmi_smc_softc *sc;
	int ret;

	sc = device_get_softc(dev);

	ret = scmi_shmem_read_msg_payload(sc->a2p_dev,
	    msg->payld, msg->rx_len - SCMI_MSG_HDR_SIZE, msg->rx_len);

	return (ret);
}

static void
scmi_smc_tx_complete(device_t dev, void *chan)
{
	struct scmi_smc_softc *sc;

	sc = device_get_softc(dev);
	scmi_shmem_tx_complete(sc->a2p_dev);
}

static int
scmi_smc_probe(device_t dev)
{

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

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

	device_set_desc(dev, "ARM SCMI SMC Transport driver");

	return (BUS_PROBE_DEFAULT);
}

static device_method_t scmi_smc_methods[] = {
	DEVMETHOD(device_probe,		scmi_smc_probe),

	/* SCMI interface */
	DEVMETHOD(scmi_transport_init,		scmi_smc_transport_init),
	DEVMETHOD(scmi_xfer_msg,		scmi_smc_xfer_msg),
	DEVMETHOD(scmi_poll_msg,		scmi_smc_poll_msg),
	DEVMETHOD(scmi_collect_reply,		scmi_smc_collect_reply),
	DEVMETHOD(scmi_tx_complete,		scmi_smc_tx_complete),

	DEVMETHOD_END
};

DEFINE_CLASS_1(scmi_smc, scmi_smc_driver, scmi_smc_methods,
    sizeof(struct scmi_smc_softc), scmi_driver);

/* Needs to be after the mmio_sram driver */
EARLY_DRIVER_MODULE(scmi_smc, simplebus, scmi_smc_driver, 0, 0,
    BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_LATE);
MODULE_VERSION(scmi_smc, 1);