rtas.c revision 227293
1139749Simp/*-
291355Simp * Copyright (c) 2011 Nathan Whitehorn
391355Simp * All rights reserved.
491355Simp *
591355Simp * Redistribution and use in source and binary forms, with or without
691355Simp * modification, are permitted provided that the following conditions
791355Simp * are met:
891355Simp * 1. Redistributions of source code must retain the above copyright
991355Simp *    notice, this list of conditions and the following disclaimer.
1091355Simp * 2. Redistributions in binary form must reproduce the above copyright
1191355Simp *    notice, this list of conditions and the following disclaimer in the
1291355Simp *    documentation and/or other materials provided with the distribution.
1391355Simp *
1491355Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1591355Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1691355Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1791355Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1891355Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1991355Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2091355Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2191355Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2291355Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2391355Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2491355Simp * SUCH DAMAGE.
2591355Simp */
2691355Simp
2791355Simp#include <sys/cdefs.h>
2891355Simp__FBSDID("$FreeBSD: head/sys/powerpc/ofw/rtas.c 227293 2011-11-07 06:44:47Z ed $");
2991355Simp
3091355Simp#include <sys/param.h>
3191355Simp#include <sys/kernel.h>
3291355Simp#include <sys/lock.h>
33165217Sjhb#include <sys/mutex.h>
3491355Simp#include <sys/systm.h>
3591355Simp#include <sys/proc.h>
3691355Simp
37154599Sjhb#include <vm/vm.h>
3891355Simp#include <vm/vm_page.h>
39154599Sjhb#include <vm/pmap.h>
40102440Sjhb
41232403Sjhb#include <machine/bus.h>
42232403Sjhb#include <machine/md_var.h>
43280970Sjhb#include <machine/pcb.h>
44280970Sjhb#include <machine/pmap.h>
45280970Sjhb#include <machine/rtas.h>
46232403Sjhb#include <machine/stdarg.h>
47232403Sjhb
48211430Sjhb#include <dev/ofw/openfirm.h>
49214110Sjkim
50211430Sjhbstatic MALLOC_DEFINE(M_RTAS, "rtas", "Run Time Abstraction Service");
51172394Smarius
52172394Smariusstatic vm_offset_t	rtas_bounce_phys;
53102440Sjhbstatic caddr_t		rtas_bounce_virt;
54153898Simpstatic off_t		rtas_bounce_offset;
55153898Simpstatic size_t		rtas_bounce_size;
56232403Sjhbstatic uintptr_t	rtas_private_data;
57128058Simpstatic struct mtx	rtas_mtx;
5891355Simpstatic phandle_t	rtas;
5991355Simp
6091355Simp/* From ofwcall.S */
6191355Simpint rtascall(vm_offset_t callbuffer, uintptr_t rtas_privdat);
6291355Simpextern uintptr_t	rtas_entry;
6391355Simpextern register_t	rtasmsr;
64169221Sjhb
65169221Sjhbint setfault(faultbuf);             /* defined in locore.S */
66169221Sjhb
67169221Sjhb/*
68169221Sjhb * After the VM is up, allocate RTAS memory and instantiate it
69163163Sjmg */
70163163Sjmg
71163163Sjmgstatic void rtas_setup(void *);
72163163Sjmg
7391355SimpSYSINIT(rtas_setup, SI_SUB_KMEM, SI_ORDER_ANY, rtas_setup, NULL);
7491355Simp
7591355Simpstatic void
76119266Simprtas_setup(void *junk)
7791355Simp{
7891355Simp	ihandle_t rtasi;
79119266Simp	cell_t rtas_size = 0, rtas_ptr;
80113544Smdodd	char path[31];
81113544Smdodd	int result;
82113544Smdodd
83113544Smdodd	rtas = OF_finddevice("/rtas");
84232472Sjhb	if (rtas == -1) {
85232472Sjhb		rtas = 0;
86153560Sjhb		return;
87153560Sjhb	}
88232472Sjhb	OF_package_to_path(rtas, path, sizeof(path));
89232472Sjhb	rtasi = OF_open(path);
90164264Sjhb	if (rtasi == 0) {
91166176Sjhb		rtas = 0;
92292907Sngie		printf("Error initializing RTAS: could not open node\n");
93292907Sngie		return;
94292907Sngie	}
95292907Sngie
96292907Sngie	mtx_init(&rtas_mtx, "RTAS", MTX_DEF, 0);
97166176Sjhb
98169221Sjhb	/* RTAS must be called with everything turned off in MSR */
99164264Sjhb	rtasmsr = mfmsr();
100164264Sjhb	rtasmsr &= ~(PSL_IR | PSL_DR | PSL_EE | PSL_SE);
101166176Sjhb	#ifdef __powerpc64__
102294340Sjhb	rtasmsr &= ~PSL_SF;
103294340Sjhb	#endif
10491355Simp
10591355Simp	/*
10691355Simp	 * Allocate rtas_size + one page of contiguous, wired physical memory
107253450Sjhb	 * that can fit into a 32-bit address space and accessed from real mode.
108253450Sjhb	 * This is used both to bounce arguments and for RTAS private data.
109189306Sjhb	 *
110189306Sjhb	 * It must be 4KB-aligned and not cross a 256 MB boundary.
111201279Sjhb	 */
112201279Sjhb
11391355Simp	OF_getprop(rtas, "rtas-size", &rtas_size, sizeof(rtas_size));
11491355Simp	rtas_size = round_page(rtas_size);
11591355Simp	rtas_bounce_virt = contigmalloc(rtas_size + PAGE_SIZE, M_RTAS, 0, 0,
116172394Smarius	    ulmin(platform_real_maxaddr(), BUS_SPACE_MAXADDR_32BIT),
11792233Simp	    4096, 256*1024*1024);
11891355Simp
11991355Simp	rtas_private_data = vtophys(rtas_bounce_virt);
120330938Sjhb	rtas_bounce_virt += rtas_size;	/* Actual bounce area */
121252315Sjhb	rtas_bounce_phys = vtophys(rtas_bounce_virt);
122111056Simp	rtas_bounce_size = PAGE_SIZE;
123111056Simp
124111056Simp	/*
125111056Simp	 * Instantiate RTAS. We always use the 32-bit version.
126117115Stmm	 */
127120155Siwasaki
128128058Simp	result = OF_call_method("instantiate-rtas", rtasi, 1, 1,
129257495Skib	    (cell_t)rtas_private_data, &rtas_ptr);
130292907Sngie	OF_close(rtasi);
131169615Simp
132169615Simp	if (result != 0) {
133169615Simp		rtas = 0;
134169615Simp		rtas_ptr = 0;
135169615Simp		printf("Error initializing RTAS (%d)\n", result);
136169615Simp		return;
137142735Simp	}
138169615Simp
139169615Simp	rtas_entry = (uintptr_t)(rtas_ptr);
140169615Simp}
141169615Simp
142142735Simpstatic cell_t
143164067Sjhbrtas_real_map(const void *buf, size_t len)
14491355Simp{
145	cell_t phys;
146
147	mtx_assert(&rtas_mtx, MA_OWNED);
148
149	/*
150	 * Make sure the bounce page offset satisfies any reasonable
151	 * alignment constraint.
152	 */
153	rtas_bounce_offset += sizeof(register_t) -
154	    (rtas_bounce_offset % sizeof(register_t));
155
156	if (rtas_bounce_offset + len > rtas_bounce_size) {
157		panic("Oversize RTAS call!");
158		return 0;
159	}
160
161	if (buf != NULL)
162		memcpy(rtas_bounce_virt + rtas_bounce_offset, buf, len);
163	else
164		return (0);
165
166	phys = rtas_bounce_phys + rtas_bounce_offset;
167	rtas_bounce_offset += len;
168
169	return (phys);
170}
171
172static void
173rtas_real_unmap(cell_t physaddr, void *buf, size_t len)
174{
175	mtx_assert(&rtas_mtx, MA_OWNED);
176
177	if (physaddr == 0)
178		return;
179
180	memcpy(buf, rtas_bounce_virt + (physaddr - rtas_bounce_phys), len);
181}
182
183/* Check if we have RTAS */
184int
185rtas_exists(void)
186{
187	return (rtas != 0);
188}
189
190/* Call an RTAS method by token */
191int
192rtas_call_method(cell_t token, int nargs, int nreturns, ...)
193{
194	vm_offset_t argsptr;
195	faultbuf env;
196	va_list ap;
197	struct {
198		cell_t token;
199		cell_t nargs;
200		cell_t nreturns;
201		cell_t args_n_results[12];
202	} args;
203	int n, result;
204
205	if (!rtas_exists() || nargs + nreturns > 12)
206		return (-1);
207
208	args.token = token;
209	va_start(ap, nreturns);
210
211	mtx_lock(&rtas_mtx);
212	rtas_bounce_offset = 0;
213
214	args.nargs = nargs;
215	args.nreturns = nreturns;
216
217	for (n = 0; n < nargs; n++)
218		args.args_n_results[n] = va_arg(ap, cell_t);
219
220	argsptr = rtas_real_map(&args, sizeof(args));
221
222	/* Get rid of any stale machine checks that have been waiting.  */
223	__asm __volatile ("sync; isync");
224        if (!setfault(env)) {
225		__asm __volatile ("sync");
226		result = rtascall(argsptr, rtas_private_data);
227		__asm __volatile ("sync; isync");
228	} else {
229		result = RTAS_HW_ERROR;
230	}
231	curthread->td_pcb->pcb_onfault = 0;
232	__asm __volatile ("sync");
233
234	rtas_real_unmap(argsptr, &args, sizeof(args));
235	mtx_unlock(&rtas_mtx);
236
237	if (result < 0)
238		return (result);
239
240	for (n = nargs; n < nargs + nreturns; n++)
241		*va_arg(ap, cell_t *) = args.args_n_results[n];
242	return (result);
243}
244
245/* Look up an RTAS token */
246cell_t
247rtas_token_lookup(const char *method)
248{
249	cell_t token;
250
251	if (!rtas_exists())
252		return (-1);
253
254	if (OF_getprop(rtas, method, &token, sizeof(token)) == -1)
255		return (-1);
256
257	return (token);
258}
259
260
261