xref: /freebsd/sys/powerpc/ofw/rtas.c (revision 685dc743)
117879090SNathan Whitehorn /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
417879090SNathan Whitehorn  * Copyright (c) 2011 Nathan Whitehorn
517879090SNathan Whitehorn  * All rights reserved.
617879090SNathan Whitehorn  *
717879090SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
817879090SNathan Whitehorn  * modification, are permitted provided that the following conditions
917879090SNathan Whitehorn  * are met:
1017879090SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
1117879090SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
1217879090SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
1317879090SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
1417879090SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
1517879090SNathan Whitehorn  *
1617879090SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1717879090SNathan Whitehorn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1817879090SNathan Whitehorn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1917879090SNathan Whitehorn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2017879090SNathan Whitehorn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2117879090SNathan Whitehorn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2217879090SNathan Whitehorn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2317879090SNathan Whitehorn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2417879090SNathan Whitehorn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2517879090SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2617879090SNathan Whitehorn  * SUCH DAMAGE.
2717879090SNathan Whitehorn  */
2817879090SNathan Whitehorn 
2917879090SNathan Whitehorn #include <sys/cdefs.h>
30d20d17f6SBrandon Bergren #include <sys/endian.h>
3117879090SNathan Whitehorn #include <sys/param.h>
3217879090SNathan Whitehorn #include <sys/kernel.h>
3317879090SNathan Whitehorn #include <sys/lock.h>
3417879090SNathan Whitehorn #include <sys/mutex.h>
3517879090SNathan Whitehorn #include <sys/systm.h>
369134df8bSNathan Whitehorn #include <sys/proc.h>
3717879090SNathan Whitehorn 
3817879090SNathan Whitehorn #include <vm/vm.h>
3917879090SNathan Whitehorn #include <vm/vm_page.h>
4017879090SNathan Whitehorn #include <vm/pmap.h>
4117879090SNathan Whitehorn 
4217879090SNathan Whitehorn #include <machine/bus.h>
4317879090SNathan Whitehorn #include <machine/md_var.h>
449134df8bSNathan Whitehorn #include <machine/pcb.h>
4517879090SNathan Whitehorn #include <machine/rtas.h>
4617879090SNathan Whitehorn #include <machine/stdarg.h>
4717879090SNathan Whitehorn 
4817879090SNathan Whitehorn #include <dev/ofw/openfirm.h>
4917879090SNathan Whitehorn 
50d745c852SEd Schouten static MALLOC_DEFINE(M_RTAS, "rtas", "Run Time Abstraction Service");
5117879090SNathan Whitehorn 
5217879090SNathan Whitehorn static vm_offset_t	rtas_bounce_phys;
5317879090SNathan Whitehorn static caddr_t		rtas_bounce_virt;
5417879090SNathan Whitehorn static off_t		rtas_bounce_offset;
5517879090SNathan Whitehorn static size_t		rtas_bounce_size;
5617879090SNathan Whitehorn static uintptr_t	rtas_private_data;
5717879090SNathan Whitehorn static struct mtx	rtas_mtx;
5817879090SNathan Whitehorn static phandle_t	rtas;
5917879090SNathan Whitehorn 
6017879090SNathan Whitehorn /* From ofwcall.S */
6117879090SNathan Whitehorn int rtascall(vm_offset_t callbuffer, uintptr_t rtas_privdat);
6217879090SNathan Whitehorn extern uintptr_t	rtas_entry;
6317879090SNathan Whitehorn extern register_t	rtasmsr;
6417879090SNathan Whitehorn 
6517879090SNathan Whitehorn /*
6617879090SNathan Whitehorn  * After the VM is up, allocate RTAS memory and instantiate it
6717879090SNathan Whitehorn  */
6817879090SNathan Whitehorn 
6917879090SNathan Whitehorn static void rtas_setup(void *);
7017879090SNathan Whitehorn 
7117879090SNathan Whitehorn SYSINIT(rtas_setup, SI_SUB_KMEM, SI_ORDER_ANY, rtas_setup, NULL);
7217879090SNathan Whitehorn 
7317879090SNathan Whitehorn static void
rtas_setup(void * junk)7417879090SNathan Whitehorn rtas_setup(void *junk)
7517879090SNathan Whitehorn {
7617879090SNathan Whitehorn 	ihandle_t rtasi;
7717879090SNathan Whitehorn 	cell_t rtas_size = 0, rtas_ptr;
7817879090SNathan Whitehorn 	char path[31];
7917879090SNathan Whitehorn 	int result;
8017879090SNathan Whitehorn 
8117879090SNathan Whitehorn 	rtas = OF_finddevice("/rtas");
8217879090SNathan Whitehorn 	if (rtas == -1) {
8317879090SNathan Whitehorn 		rtas = 0;
8417879090SNathan Whitehorn 		return;
8517879090SNathan Whitehorn 	}
8617879090SNathan Whitehorn 	OF_package_to_path(rtas, path, sizeof(path));
8717879090SNathan Whitehorn 
88e52f055dSNathan Whitehorn 	mtx_init(&rtas_mtx, "RTAS", NULL, MTX_SPIN);
8917879090SNathan Whitehorn 
9017879090SNathan Whitehorn 	/* RTAS must be called with everything turned off in MSR */
9117879090SNathan Whitehorn 	rtasmsr = mfmsr();
92d20d17f6SBrandon Bergren 	rtasmsr &= ~(PSL_IR | PSL_DR | PSL_EE | PSL_SE | PSL_LE);
9317879090SNathan Whitehorn 	#ifdef __powerpc64__
9417879090SNathan Whitehorn 	rtasmsr &= ~PSL_SF;
9517879090SNathan Whitehorn 	#endif
9617879090SNathan Whitehorn 
9717879090SNathan Whitehorn 	/*
9817879090SNathan Whitehorn 	 * Allocate rtas_size + one page of contiguous, wired physical memory
9917879090SNathan Whitehorn 	 * that can fit into a 32-bit address space and accessed from real mode.
10017879090SNathan Whitehorn 	 * This is used both to bounce arguments and for RTAS private data.
10117879090SNathan Whitehorn 	 *
10217879090SNathan Whitehorn 	 * It must be 4KB-aligned and not cross a 256 MB boundary.
10317879090SNathan Whitehorn 	 */
10417879090SNathan Whitehorn 
105f70dd52fSNathan Whitehorn 	OF_getencprop(rtas, "rtas-size", &rtas_size, sizeof(rtas_size));
10617879090SNathan Whitehorn 	rtas_size = round_page(rtas_size);
10717879090SNathan Whitehorn 	rtas_bounce_virt = contigmalloc(rtas_size + PAGE_SIZE, M_RTAS, 0, 0,
10817879090SNathan Whitehorn 	    ulmin(platform_real_maxaddr(), BUS_SPACE_MAXADDR_32BIT),
10917879090SNathan Whitehorn 	    4096, 256*1024*1024);
11017879090SNathan Whitehorn 
11117879090SNathan Whitehorn 	rtas_private_data = vtophys(rtas_bounce_virt);
11217879090SNathan Whitehorn 	rtas_bounce_virt += rtas_size;	/* Actual bounce area */
11317879090SNathan Whitehorn 	rtas_bounce_phys = vtophys(rtas_bounce_virt);
11417879090SNathan Whitehorn 	rtas_bounce_size = PAGE_SIZE;
11517879090SNathan Whitehorn 
11617879090SNathan Whitehorn 	/*
11717879090SNathan Whitehorn 	 * Instantiate RTAS. We always use the 32-bit version.
11817879090SNathan Whitehorn 	 */
11917879090SNathan Whitehorn 
120f70dd52fSNathan Whitehorn 	if (OF_hasprop(rtas, "linux,rtas-entry") &&
121f70dd52fSNathan Whitehorn 	    OF_hasprop(rtas, "linux,rtas-base")) {
122f70dd52fSNathan Whitehorn 		OF_getencprop(rtas, "linux,rtas-base", &rtas_ptr,
123f70dd52fSNathan Whitehorn 		    sizeof(rtas_ptr));
124f70dd52fSNathan Whitehorn 		rtas_private_data = rtas_ptr;
125f70dd52fSNathan Whitehorn 		OF_getencprop(rtas, "linux,rtas-entry", &rtas_ptr,
126f70dd52fSNathan Whitehorn 		    sizeof(rtas_ptr));
127f70dd52fSNathan Whitehorn 	} else {
128f70dd52fSNathan Whitehorn 		rtasi = OF_open(path);
129f70dd52fSNathan Whitehorn 		if (rtasi == 0) {
130f70dd52fSNathan Whitehorn 			rtas = 0;
131f70dd52fSNathan Whitehorn 			printf("Error initializing RTAS: could not open "
132f70dd52fSNathan Whitehorn 			    "node\n");
133f70dd52fSNathan Whitehorn 			return;
134f70dd52fSNathan Whitehorn 		}
135f70dd52fSNathan Whitehorn 
13617879090SNathan Whitehorn 		result = OF_call_method("instantiate-rtas", rtasi, 1, 1,
13717879090SNathan Whitehorn 		    (cell_t)rtas_private_data, &rtas_ptr);
13817879090SNathan Whitehorn 		OF_close(rtasi);
13917879090SNathan Whitehorn 
14017879090SNathan Whitehorn 		if (result != 0) {
14117879090SNathan Whitehorn 			rtas = 0;
14217879090SNathan Whitehorn 			rtas_ptr = 0;
14317879090SNathan Whitehorn 			printf("Error initializing RTAS (%d)\n", result);
14417879090SNathan Whitehorn 			return;
14517879090SNathan Whitehorn 		}
146f70dd52fSNathan Whitehorn 	}
14717879090SNathan Whitehorn 
14817879090SNathan Whitehorn 	rtas_entry = (uintptr_t)(rtas_ptr);
14917879090SNathan Whitehorn }
15017879090SNathan Whitehorn 
15117879090SNathan Whitehorn static cell_t
rtas_real_map(const void * buf,size_t len)15217879090SNathan Whitehorn rtas_real_map(const void *buf, size_t len)
15317879090SNathan Whitehorn {
15417879090SNathan Whitehorn 	cell_t phys;
15517879090SNathan Whitehorn 
15617879090SNathan Whitehorn 	mtx_assert(&rtas_mtx, MA_OWNED);
15717879090SNathan Whitehorn 
15817879090SNathan Whitehorn 	/*
15917879090SNathan Whitehorn 	 * Make sure the bounce page offset satisfies any reasonable
16017879090SNathan Whitehorn 	 * alignment constraint.
16117879090SNathan Whitehorn 	 */
16217879090SNathan Whitehorn 	rtas_bounce_offset += sizeof(register_t) -
16317879090SNathan Whitehorn 	    (rtas_bounce_offset % sizeof(register_t));
16417879090SNathan Whitehorn 
16517879090SNathan Whitehorn 	if (rtas_bounce_offset + len > rtas_bounce_size) {
16617879090SNathan Whitehorn 		panic("Oversize RTAS call!");
16717879090SNathan Whitehorn 		return 0;
16817879090SNathan Whitehorn 	}
16917879090SNathan Whitehorn 
17017879090SNathan Whitehorn 	if (buf != NULL)
17117879090SNathan Whitehorn 		memcpy(rtas_bounce_virt + rtas_bounce_offset, buf, len);
17217879090SNathan Whitehorn 	else
17317879090SNathan Whitehorn 		return (0);
17417879090SNathan Whitehorn 
17517879090SNathan Whitehorn 	phys = rtas_bounce_phys + rtas_bounce_offset;
17617879090SNathan Whitehorn 	rtas_bounce_offset += len;
17717879090SNathan Whitehorn 
17817879090SNathan Whitehorn 	return (phys);
17917879090SNathan Whitehorn }
18017879090SNathan Whitehorn 
18117879090SNathan Whitehorn static void
rtas_real_unmap(cell_t physaddr,void * buf,size_t len)18217879090SNathan Whitehorn rtas_real_unmap(cell_t physaddr, void *buf, size_t len)
18317879090SNathan Whitehorn {
18417879090SNathan Whitehorn 	mtx_assert(&rtas_mtx, MA_OWNED);
18517879090SNathan Whitehorn 
18617879090SNathan Whitehorn 	if (physaddr == 0)
18717879090SNathan Whitehorn 		return;
18817879090SNathan Whitehorn 
18917879090SNathan Whitehorn 	memcpy(buf, rtas_bounce_virt + (physaddr - rtas_bounce_phys), len);
19017879090SNathan Whitehorn }
19117879090SNathan Whitehorn 
19217879090SNathan Whitehorn /* Check if we have RTAS */
19317879090SNathan Whitehorn int
rtas_exists(void)19417879090SNathan Whitehorn rtas_exists(void)
19517879090SNathan Whitehorn {
19617879090SNathan Whitehorn 	return (rtas != 0);
19717879090SNathan Whitehorn }
19817879090SNathan Whitehorn 
19917879090SNathan Whitehorn /* Call an RTAS method by token */
20017879090SNathan Whitehorn int
rtas_call_method(cell_t token,int nargs,int nreturns,...)20117879090SNathan Whitehorn rtas_call_method(cell_t token, int nargs, int nreturns, ...)
20217879090SNathan Whitehorn {
20317879090SNathan Whitehorn 	vm_offset_t argsptr;
204a18c313eSNathan Whitehorn 	jmp_buf env, *oldfaultbuf;
20517879090SNathan Whitehorn 	va_list ap;
20617879090SNathan Whitehorn 	struct {
20717879090SNathan Whitehorn 		cell_t token;
20817879090SNathan Whitehorn 		cell_t nargs;
20917879090SNathan Whitehorn 		cell_t nreturns;
21017879090SNathan Whitehorn 		cell_t args_n_results[12];
21117879090SNathan Whitehorn 	} args;
21217879090SNathan Whitehorn 	int n, result;
21317879090SNathan Whitehorn 
21417879090SNathan Whitehorn 	if (!rtas_exists() || nargs + nreturns > 12)
21517879090SNathan Whitehorn 		return (-1);
21617879090SNathan Whitehorn 
217d20d17f6SBrandon Bergren 	args.token = htobe32(token);
21817879090SNathan Whitehorn 	va_start(ap, nreturns);
21917879090SNathan Whitehorn 
220e52f055dSNathan Whitehorn 	mtx_lock_spin(&rtas_mtx);
22117879090SNathan Whitehorn 	rtas_bounce_offset = 0;
22217879090SNathan Whitehorn 
223d20d17f6SBrandon Bergren 	args.nargs = htobe32(nargs);
224d20d17f6SBrandon Bergren 	args.nreturns = htobe32(nreturns);
22517879090SNathan Whitehorn 
22617879090SNathan Whitehorn 	for (n = 0; n < nargs; n++)
227d20d17f6SBrandon Bergren 		args.args_n_results[n] = htobe32(va_arg(ap, cell_t));
22817879090SNathan Whitehorn 
22917879090SNathan Whitehorn 	argsptr = rtas_real_map(&args, sizeof(args));
2309134df8bSNathan Whitehorn 
2319134df8bSNathan Whitehorn 	/* Get rid of any stale machine checks that have been waiting.  */
2329134df8bSNathan Whitehorn 	__asm __volatile ("sync; isync");
2331679c302SNathan Whitehorn 	oldfaultbuf = curthread->td_pcb->pcb_onfault;
234a18c313eSNathan Whitehorn 	curthread->td_pcb->pcb_onfault = &env;
235a18c313eSNathan Whitehorn 	if (!setjmp(env)) {
2369134df8bSNathan Whitehorn 		__asm __volatile ("sync");
23717879090SNathan Whitehorn 		result = rtascall(argsptr, rtas_private_data);
2389134df8bSNathan Whitehorn 		__asm __volatile ("sync; isync");
2399134df8bSNathan Whitehorn 	} else {
2409134df8bSNathan Whitehorn 		result = RTAS_HW_ERROR;
2419134df8bSNathan Whitehorn 	}
2421679c302SNathan Whitehorn 	curthread->td_pcb->pcb_onfault = oldfaultbuf;
2439134df8bSNathan Whitehorn 	__asm __volatile ("sync");
2449134df8bSNathan Whitehorn 
24517879090SNathan Whitehorn 	rtas_real_unmap(argsptr, &args, sizeof(args));
246e52f055dSNathan Whitehorn 	mtx_unlock_spin(&rtas_mtx);
24717879090SNathan Whitehorn 
24817879090SNathan Whitehorn 	if (result < 0)
24917879090SNathan Whitehorn 		return (result);
25017879090SNathan Whitehorn 
25117879090SNathan Whitehorn 	for (n = nargs; n < nargs + nreturns; n++)
252d20d17f6SBrandon Bergren 		*va_arg(ap, cell_t *) = be32toh(args.args_n_results[n]);
25317879090SNathan Whitehorn 	return (result);
25417879090SNathan Whitehorn }
25517879090SNathan Whitehorn 
25617879090SNathan Whitehorn /* Look up an RTAS token */
25717879090SNathan Whitehorn cell_t
rtas_token_lookup(const char * method)25817879090SNathan Whitehorn rtas_token_lookup(const char *method)
25917879090SNathan Whitehorn {
26017879090SNathan Whitehorn 	cell_t token;
26117879090SNathan Whitehorn 
26217879090SNathan Whitehorn 	if (!rtas_exists())
26317879090SNathan Whitehorn 		return (-1);
26417879090SNathan Whitehorn 
265f70dd52fSNathan Whitehorn 	if (OF_getencprop(rtas, method, &token, sizeof(token)) == -1)
26617879090SNathan Whitehorn 		return (-1);
26717879090SNathan Whitehorn 
26817879090SNathan Whitehorn 	return (token);
26917879090SNathan Whitehorn }
270