xref: /freebsd/sys/powerpc/ofw/rtas.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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 #include <sys/endian.h>
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 
38 #include <vm/vm.h>
39 #include <vm/vm_page.h>
40 #include <vm/pmap.h>
41 
42 #include <machine/bus.h>
43 #include <machine/md_var.h>
44 #include <machine/pcb.h>
45 #include <machine/rtas.h>
46 #include <machine/stdarg.h>
47 
48 #include <dev/ofw/openfirm.h>
49 
50 static MALLOC_DEFINE(M_RTAS, "rtas", "Run Time Abstraction Service");
51 
52 static vm_offset_t	rtas_bounce_phys;
53 static caddr_t		rtas_bounce_virt;
54 static off_t		rtas_bounce_offset;
55 static size_t		rtas_bounce_size;
56 static uintptr_t	rtas_private_data;
57 static struct mtx	rtas_mtx;
58 static phandle_t	rtas;
59 
60 /* From ofwcall.S */
61 int rtascall(vm_offset_t callbuffer, uintptr_t rtas_privdat);
62 extern uintptr_t	rtas_entry;
63 extern register_t	rtasmsr;
64 
65 /*
66  * After the VM is up, allocate RTAS memory and instantiate it
67  */
68 
69 static void rtas_setup(void *);
70 
71 SYSINIT(rtas_setup, SI_SUB_KMEM, SI_ORDER_ANY, rtas_setup, NULL);
72 
73 static void
74 rtas_setup(void *junk)
75 {
76 	ihandle_t rtasi;
77 	cell_t rtas_size = 0, rtas_ptr;
78 	char path[31];
79 	int result;
80 
81 	rtas = OF_finddevice("/rtas");
82 	if (rtas == -1) {
83 		rtas = 0;
84 		return;
85 	}
86 	OF_package_to_path(rtas, path, sizeof(path));
87 
88 	mtx_init(&rtas_mtx, "RTAS", NULL, MTX_SPIN);
89 
90 	/* RTAS must be called with everything turned off in MSR */
91 	rtasmsr = mfmsr();
92 	rtasmsr &= ~(PSL_IR | PSL_DR | PSL_EE | PSL_SE | PSL_LE);
93 	#ifdef __powerpc64__
94 	rtasmsr &= ~PSL_SF;
95 	#endif
96 
97 	/*
98 	 * Allocate rtas_size + one page of contiguous, wired physical memory
99 	 * that can fit into a 32-bit address space and accessed from real mode.
100 	 * This is used both to bounce arguments and for RTAS private data.
101 	 *
102 	 * It must be 4KB-aligned and not cross a 256 MB boundary.
103 	 */
104 
105 	OF_getencprop(rtas, "rtas-size", &rtas_size, sizeof(rtas_size));
106 	rtas_size = round_page(rtas_size);
107 	rtas_bounce_virt = contigmalloc(rtas_size + PAGE_SIZE, M_RTAS, 0, 0,
108 	    ulmin(platform_real_maxaddr(), BUS_SPACE_MAXADDR_32BIT),
109 	    4096, 256*1024*1024);
110 
111 	rtas_private_data = vtophys(rtas_bounce_virt);
112 	rtas_bounce_virt += rtas_size;	/* Actual bounce area */
113 	rtas_bounce_phys = vtophys(rtas_bounce_virt);
114 	rtas_bounce_size = PAGE_SIZE;
115 
116 	/*
117 	 * Instantiate RTAS. We always use the 32-bit version.
118 	 */
119 
120 	if (OF_hasprop(rtas, "linux,rtas-entry") &&
121 	    OF_hasprop(rtas, "linux,rtas-base")) {
122 		OF_getencprop(rtas, "linux,rtas-base", &rtas_ptr,
123 		    sizeof(rtas_ptr));
124 		rtas_private_data = rtas_ptr;
125 		OF_getencprop(rtas, "linux,rtas-entry", &rtas_ptr,
126 		    sizeof(rtas_ptr));
127 	} else {
128 		rtasi = OF_open(path);
129 		if (rtasi == 0) {
130 			rtas = 0;
131 			printf("Error initializing RTAS: could not open "
132 			    "node\n");
133 			return;
134 		}
135 
136 		result = OF_call_method("instantiate-rtas", rtasi, 1, 1,
137 		    (cell_t)rtas_private_data, &rtas_ptr);
138 		OF_close(rtasi);
139 
140 		if (result != 0) {
141 			rtas = 0;
142 			rtas_ptr = 0;
143 			printf("Error initializing RTAS (%d)\n", result);
144 			return;
145 		}
146 	}
147 
148 	rtas_entry = (uintptr_t)(rtas_ptr);
149 }
150 
151 static cell_t
152 rtas_real_map(const void *buf, size_t len)
153 {
154 	cell_t phys;
155 
156 	mtx_assert(&rtas_mtx, MA_OWNED);
157 
158 	/*
159 	 * Make sure the bounce page offset satisfies any reasonable
160 	 * alignment constraint.
161 	 */
162 	rtas_bounce_offset += sizeof(register_t) -
163 	    (rtas_bounce_offset % sizeof(register_t));
164 
165 	if (rtas_bounce_offset + len > rtas_bounce_size) {
166 		panic("Oversize RTAS call!");
167 		return 0;
168 	}
169 
170 	if (buf != NULL)
171 		memcpy(rtas_bounce_virt + rtas_bounce_offset, buf, len);
172 	else
173 		return (0);
174 
175 	phys = rtas_bounce_phys + rtas_bounce_offset;
176 	rtas_bounce_offset += len;
177 
178 	return (phys);
179 }
180 
181 static void
182 rtas_real_unmap(cell_t physaddr, void *buf, size_t len)
183 {
184 	mtx_assert(&rtas_mtx, MA_OWNED);
185 
186 	if (physaddr == 0)
187 		return;
188 
189 	memcpy(buf, rtas_bounce_virt + (physaddr - rtas_bounce_phys), len);
190 }
191 
192 /* Check if we have RTAS */
193 int
194 rtas_exists(void)
195 {
196 	return (rtas != 0);
197 }
198 
199 /* Call an RTAS method by token */
200 int
201 rtas_call_method(cell_t token, int nargs, int nreturns, ...)
202 {
203 	vm_offset_t argsptr;
204 	jmp_buf env, *oldfaultbuf;
205 	va_list ap;
206 	struct {
207 		cell_t token;
208 		cell_t nargs;
209 		cell_t nreturns;
210 		cell_t args_n_results[12];
211 	} args;
212 	int n, result;
213 
214 	if (!rtas_exists() || nargs + nreturns > 12)
215 		return (-1);
216 
217 	args.token = htobe32(token);
218 	va_start(ap, nreturns);
219 
220 	mtx_lock_spin(&rtas_mtx);
221 	rtas_bounce_offset = 0;
222 
223 	args.nargs = htobe32(nargs);
224 	args.nreturns = htobe32(nreturns);
225 
226 	for (n = 0; n < nargs; n++)
227 		args.args_n_results[n] = htobe32(va_arg(ap, cell_t));
228 
229 	argsptr = rtas_real_map(&args, sizeof(args));
230 
231 	/* Get rid of any stale machine checks that have been waiting.  */
232 	__asm __volatile ("sync; isync");
233 	oldfaultbuf = curthread->td_pcb->pcb_onfault;
234 	curthread->td_pcb->pcb_onfault = &env;
235 	if (!setjmp(env)) {
236 		__asm __volatile ("sync");
237 		result = rtascall(argsptr, rtas_private_data);
238 		__asm __volatile ("sync; isync");
239 	} else {
240 		result = RTAS_HW_ERROR;
241 	}
242 	curthread->td_pcb->pcb_onfault = oldfaultbuf;
243 	__asm __volatile ("sync");
244 
245 	rtas_real_unmap(argsptr, &args, sizeof(args));
246 	mtx_unlock_spin(&rtas_mtx);
247 
248 	if (result < 0)
249 		return (result);
250 
251 	for (n = nargs; n < nargs + nreturns; n++)
252 		*va_arg(ap, cell_t *) = be32toh(args.args_n_results[n]);
253 	return (result);
254 }
255 
256 /* Look up an RTAS token */
257 cell_t
258 rtas_token_lookup(const char *method)
259 {
260 	cell_t token;
261 
262 	if (!rtas_exists())
263 		return (-1);
264 
265 	if (OF_getencprop(rtas, method, &token, sizeof(token)) == -1)
266 		return (-1);
267 
268 	return (token);
269 }
270