xref: /netbsd-src/sys/arch/arm/arm/psci.c (revision 9e015301d478f2ba8de08d86ce37766bb4a4949b)
1 /* $NetBSD: psci.c,v 1.8 2024/12/30 19:09:49 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: psci.c,v 1.8 2024/12/30 19:09:49 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/device.h>
36 
37 #include <arm/arm/psci.h>
38 
39 /* PSCI methods */
40 #define	PSCI_VERSION		0x84000000
41 #define	PSCI_SYSTEM_OFF		0x84000008
42 #define	PSCI_SYSTEM_RESET	0x84000009
43 #if defined(__aarch64__)
44 #define	PSCI_CPU_SUSPEND	0xc4000001
45 #define	PSCI_CPU_ON		0xc4000003
46 #else
47 #define	PSCI_CPU_SUSPEND	0x84000001
48 #define	PSCI_CPU_ON		0x84000003
49 #endif
50 #define	PSCI_FEATURES		0x8400000a
51 
52 static psci_fn psci_call_fn;
53 
54 static uint32_t psci_functions[PSCI_FUNC_MAX] = {
55         [PSCI_FUNC_VERSION] = PSCI_VERSION,
56         [PSCI_FUNC_SYSTEM_OFF] = PSCI_SYSTEM_OFF,
57 	[PSCI_FUNC_SYSTEM_RESET] = PSCI_SYSTEM_RESET,
58 	[PSCI_FUNC_CPU_SUSPEND] = PSCI_CPU_SUSPEND,
59 	[PSCI_FUNC_CPU_ON] = PSCI_CPU_ON,
60 	[PSCI_FUNC_FEATURES] = PSCI_FEATURES,
61 };
62 
63 int
64 psci_call(register_t fid, register_t arg1, register_t arg2, register_t arg3)
65 {
66 	KASSERT(psci_call_fn != NULL);
67 
68 	if (fid == 0)
69 		return PSCI_NOT_SUPPORTED;
70 
71 	return psci_call_fn(fid, arg1, arg2, arg3);
72 }
73 
74 enum psci_conduit
75 psci_conduit(void)
76 {
77 	if (psci_call_fn == psci_call_smc) {
78 		return PSCI_CONDUIT_SMC;
79 	} else if (psci_call_fn == psci_call_hvc) {
80 		return PSCI_CONDUIT_HVC;
81 	} else {
82 		return PSCI_CONDUIT_NONE;
83 	}
84 }
85 
86 uint32_t
87 psci_version(void)
88 {
89 	if (psci_functions[PSCI_FUNC_VERSION] == 0) {
90 		/* Pre-0.2 implementation */
91 		return __SHIFTIN(0, PSCI_VERSION_MAJOR) |
92 		       __SHIFTIN(1, PSCI_VERSION_MINOR);
93 	}
94 
95 	return psci_call(psci_functions[PSCI_FUNC_VERSION], 0, 0, 0);
96 }
97 
98 int
99 psci_cpu_on(register_t target_cpu, register_t entry_point_address,
100     register_t context_id)
101 {
102 	return psci_call(psci_functions[PSCI_FUNC_CPU_ON], target_cpu,
103 	    entry_point_address, context_id);
104 }
105 
106 int
107 psci_cpu_suspend(uint32_t power_state)
108 {
109 	return psci_call(psci_functions[PSCI_FUNC_CPU_SUSPEND], power_state,
110 	    0, 0);
111 }
112 
113 void
114 psci_system_off(void)
115 {
116 	psci_call(psci_functions[PSCI_FUNC_SYSTEM_OFF], 0, 0, 0);
117 }
118 
119 void
120 psci_system_reset(void)
121 {
122 	psci_call(psci_functions[PSCI_FUNC_SYSTEM_RESET], 0, 0, 0);
123 }
124 
125 int
126 psci_features(uint32_t fid)
127 {
128 	if (psci_functions[PSCI_FUNC_FEATURES] == 0) {
129 		return PSCI_NOT_SUPPORTED;
130 	}
131 	return psci_call(psci_functions[PSCI_FUNC_FEATURES], fid, 0, 0);
132 }
133 
134 void
135 psci_init(psci_fn fn)
136 {
137 	psci_call_fn = fn;
138 }
139 
140 bool
141 psci_available(void)
142 {
143 	return psci_call_fn != NULL;
144 }
145 
146 void
147 psci_clearfunc(void)
148 {
149 	for (int i = 0; i < PSCI_FUNC_MAX; i++)
150 		psci_setfunc(i, 0);
151 }
152 
153 void
154 psci_setfunc(enum psci_function func, uint32_t fid)
155 {
156 	psci_functions[func] = fid;
157 }
158