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