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