1/*-
2 * Copyright (c) 2014 Robin Randhawa
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28/*
29 * This implements support for ARM's Power State Co-ordination Interface
30 * [PSCI]. The implementation adheres to version 0.2 of the PSCI specification
31 * but also supports v0.1. PSCI standardizes operations such as system reset, CPU
32 * on/off/suspend. PSCI requires a compliant firmware implementation.
33 *
34 * The PSCI specification used for this implementation is available at:
35 *
36 * <http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0022b/index.html>.
37 *
38 * TODO:
39 * - Add support for remaining PSCI calls [this implementation only
40 *   supports get_version, system_reset and cpu_on].
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: releng/11.0/sys/dev/psci/psci.c 286630 2015-08-11 13:42:58Z andrew $");
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/bus.h>
49#include <sys/eventhandler.h>
50#include <sys/kernel.h>
51#include <sys/module.h>
52#include <sys/reboot.h>
53
54#include <machine/bus.h>
55
56#include <dev/fdt/fdt_common.h>
57#include <dev/ofw/openfirm.h>
58#include <dev/ofw/ofw_bus.h>
59#include <dev/ofw/ofw_bus_subr.h>
60
61#include <dev/psci/psci.h>
62
63struct psci_softc {
64	device_t        dev;
65
66	psci_callfn_t	psci_call;
67	uint32_t	psci_fnids[PSCI_FN_MAX];
68};
69
70static int psci_v0_1_init(device_t dev);
71static int psci_v0_2_init(device_t dev);
72
73struct psci_softc *psci_softc = NULL;
74
75static struct ofw_compat_data compat_data[] = {
76	{"arm,psci-0.2",        (uintptr_t)psci_v0_2_init},
77	{"arm,psci",            (uintptr_t)psci_v0_1_init},
78	{NULL,                  0}
79};
80
81static int psci_probe(device_t dev);
82static int psci_attach(device_t dev);
83static void psci_shutdown(void *, int);
84
85static device_method_t psci_methods[] = {
86	DEVMETHOD(device_probe,     psci_probe),
87	DEVMETHOD(device_attach,    psci_attach),
88
89	DEVMETHOD_END
90};
91
92static driver_t psci_driver = {
93	"psci",
94	psci_methods,
95	sizeof(struct psci_softc),
96};
97
98static devclass_t psci_devclass;
99
100EARLY_DRIVER_MODULE(psci, simplebus, psci_driver, psci_devclass, 0, 0,
101    BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
102EARLY_DRIVER_MODULE(psci, ofwbus, psci_driver, psci_devclass, 0, 0,
103    BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
104
105static psci_callfn_t
106psci_get_callfn(phandle_t node)
107{
108	char method[16];
109
110	if ((OF_getprop(node, "method", method, sizeof(method))) > 0) {
111		if (strcmp(method, "hvc") == 0)
112			return (psci_hvc_despatch);
113		else if (strcmp(method, "smc") == 0)
114			return (psci_smc_despatch);
115		else
116			printf("psci: PSCI conduit \"%s\" invalid\n", method);
117	} else
118		printf("psci: PSCI conduit not supplied in the device tree\n");
119
120	return (NULL);
121}
122
123static int
124psci_probe(device_t dev)
125{
126	const struct ofw_compat_data *ocd;
127
128	if (!ofw_bus_status_okay(dev))
129		return (ENXIO);
130
131	ocd = ofw_bus_search_compatible(dev, compat_data);
132	if (ocd->ocd_str == NULL)
133		return (ENXIO);
134
135	device_set_desc(dev, "ARM Power State Co-ordination Interface Driver");
136
137	return (BUS_PROBE_SPECIFIC);
138}
139
140static int
141psci_attach(device_t dev)
142{
143	struct psci_softc *sc = device_get_softc(dev);
144	const struct ofw_compat_data *ocd;
145	psci_initfn_t psci_init;
146	phandle_t node;
147
148	if (psci_softc != NULL)
149		return (ENXIO);
150
151	ocd = ofw_bus_search_compatible(dev, compat_data);
152	psci_init = (psci_initfn_t)ocd->ocd_data;
153	KASSERT(psci_init != NULL, ("PSCI init function cannot be NULL"));
154
155	node = ofw_bus_get_node(dev);
156	sc->psci_call = psci_get_callfn(node);
157	if (sc->psci_call == NULL)
158		return (ENXIO);
159
160	if (psci_init(dev))
161		return (ENXIO);
162
163	psci_softc = sc;
164
165	return (0);
166}
167
168static int
169psci_get_version(struct psci_softc *sc)
170{
171	uint32_t fnid;
172
173	/* PSCI version wasn't supported in v0.1. */
174	fnid = sc->psci_fnids[PSCI_FN_VERSION];
175	if (fnid)
176		return (sc->psci_call(fnid, 0, 0, 0));
177
178	return (PSCI_RETVAL_NOT_SUPPORTED);
179}
180
181int
182psci_cpu_on(unsigned long cpu, unsigned long entry, unsigned long context_id)
183{
184	psci_callfn_t callfn;
185	phandle_t node;
186	uint32_t fnid;
187
188	if (psci_softc == NULL) {
189		node = ofw_bus_find_compatible(OF_peer(0), "arm,psci-0.2");
190		if (node == 0)
191			/* TODO: Handle psci 0.1 */
192			return (PSCI_RETVAL_INTERNAL_FAILURE);
193
194		fnid = PSCI_FNID_CPU_ON;
195		callfn = psci_get_callfn(node);
196		if (callfn == NULL)
197			return (PSCI_RETVAL_INTERNAL_FAILURE);
198	} else {
199		callfn = psci_softc->psci_call;
200		fnid = psci_softc->psci_fnids[PSCI_FN_CPU_ON];
201	}
202
203	/* PSCI v0.1 and v0.2 both support cpu_on. */
204	return (callfn(fnid, cpu, entry, context_id));
205}
206
207static void
208psci_shutdown(void *xsc, int howto)
209{
210	uint32_t fn = 0;
211
212	/* PSCI system_off and system_reset werent't supported in v0.1. */
213	if ((howto & RB_POWEROFF) != 0)
214		fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_OFF];
215	else if ((howto & RB_HALT) == 0)
216		fn = psci_softc->psci_fnids[PSCI_FN_SYSTEM_RESET];
217
218	if (fn)
219		psci_softc->psci_call(fn, 0, 0, 0);
220
221	/* System reset and off do not return. */
222}
223
224static int
225psci_v0_1_init(device_t dev)
226{
227	struct psci_softc *sc = device_get_softc(dev);
228	int psci_fn;
229	uint32_t psci_fnid;
230	phandle_t node;
231	int len;
232
233
234	/* Zero out the function ID table - Is this needed ? */
235	for (psci_fn = PSCI_FN_VERSION, psci_fnid = PSCI_FNID_VERSION;
236	    psci_fn < PSCI_FN_MAX; psci_fn++, psci_fnid++)
237		sc->psci_fnids[psci_fn] = 0;
238
239	/* PSCI v0.1 doesn't specify function IDs. Get them from DT */
240	node = ofw_bus_get_node(dev);
241
242	if ((len = OF_getproplen(node, "cpu_suspend")) > 0) {
243		OF_getencprop(node, "cpu_suspend", &psci_fnid, len);
244		sc->psci_fnids[PSCI_FN_CPU_SUSPEND] = psci_fnid;
245	}
246
247	if ((len = OF_getproplen(node, "cpu_on")) > 0) {
248		OF_getencprop(node, "cpu_on", &psci_fnid, len);
249		sc->psci_fnids[PSCI_FN_CPU_ON] = psci_fnid;
250	}
251
252	if ((len = OF_getproplen(node, "cpu_off")) > 0) {
253		OF_getencprop(node, "cpu_off", &psci_fnid, len);
254		sc->psci_fnids[PSCI_FN_CPU_OFF] = psci_fnid;
255	}
256
257	if ((len = OF_getproplen(node, "migrate")) > 0) {
258		OF_getencprop(node, "migrate", &psci_fnid, len);
259		sc->psci_fnids[PSCI_FN_MIGRATE] = psci_fnid;
260	}
261
262	if (bootverbose)
263		device_printf(dev, "PSCI version 0.1 available\n");
264
265	return(0);
266}
267
268static int
269psci_v0_2_init(device_t dev)
270{
271	struct psci_softc *sc = device_get_softc(dev);
272	int version;
273
274	/* PSCI v0.2 specifies explicit function IDs. */
275	sc->psci_fnids[PSCI_FN_VERSION]		    = PSCI_FNID_VERSION;
276	sc->psci_fnids[PSCI_FN_CPU_SUSPEND]	    = PSCI_FNID_CPU_SUSPEND;
277	sc->psci_fnids[PSCI_FN_CPU_OFF]		    = PSCI_FNID_CPU_OFF;
278	sc->psci_fnids[PSCI_FN_CPU_ON]		    = PSCI_FNID_CPU_ON;
279	sc->psci_fnids[PSCI_FN_AFFINITY_INFO]	    = PSCI_FNID_AFFINITY_INFO;
280	sc->psci_fnids[PSCI_FN_MIGRATE]		    = PSCI_FNID_MIGRATE;
281	sc->psci_fnids[PSCI_FN_MIGRATE_INFO_TYPE]   = PSCI_FNID_MIGRATE_INFO_TYPE;
282	sc->psci_fnids[PSCI_FN_MIGRATE_INFO_UP_CPU] = PSCI_FNID_MIGRATE_INFO_UP_CPU;
283	sc->psci_fnids[PSCI_FN_SYSTEM_OFF]	    = PSCI_FNID_SYSTEM_OFF;
284	sc->psci_fnids[PSCI_FN_SYSTEM_RESET]	    = PSCI_FNID_SYSTEM_RESET;
285
286	version = psci_get_version(sc);
287
288	if (version == PSCI_RETVAL_NOT_SUPPORTED)
289		return (1);
290
291	if ((PSCI_VER_MAJOR(version) == 0 && PSCI_VER_MINOR(version) == 2) ||
292	    (PSCI_VER_MAJOR(version) == 1 && PSCI_VER_MINOR(version) == 0)) {
293		if (bootverbose)
294			device_printf(dev, "PSCI version 0.2 available\n");
295
296		/*
297		 * We only register this for v0.2 since v0.1 doesn't support
298		 * system_reset.
299		 */
300		EVENTHANDLER_REGISTER(shutdown_final, psci_shutdown, sc,
301		    SHUTDOWN_PRI_LAST);
302
303		return (0);
304	}
305
306	device_printf(dev, "PSCI version number mismatched with DT\n");
307	return (1);
308}
309