1/* $NetBSD: ofwreal.S,v 1.3 2001/10/23 02:59:09 thorpej Exp $ */ 2 3/* 4 * Copyright (C) 1996 Wolfgang Solfrank. 5 * Copyright (C) 1996 TooLs GmbH. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34/* 35 * This file provides a real-mode client interface on machines, that 36 * (incorrectly) only implement virtual mode client interface. 37 * 38 * It assumes though, that any actual memory in the machine is 39 * mapped 1:1 even by the virtual mode OpenFirmware. 40 * Furthermore it assumes that addresses returned by OpenFirmware are not 41 * accessed by the client. 42 * 43 * TODO: handle set-callback specially 44 */ 45#include <machine/psl.h> 46#include <machine/trap.h> 47#include <machine/asm.h> 48 49#define CACHELINE 32 /* Note that this value is really 50 hardwired */ 51 52 .data 53ofentry: 54 .long 0 /* actual entry to firmware in 55 virtual mode */ 56 57#define BATSIZE (8*8) 58#define SRSIZE (16*4) 59#define SPRGSIZE (4*4) 60#define SDR1SIZE 4 61#define SI1SIZE (2*256) 62#define SI2SIZE (3*256) 63#define SVSIZE (BATSIZE+SRSIZE+SPRGSIZE+SDR1SIZE+SI1SIZE+SI2SIZE) 64 65 .local fwsave 66 .comm fwsave,SVSIZE,8 67 68 .local clsave 69 .comm clsave,SVSIZE,8 70 71ENTRY(ofwr_init) 72 mflr 31 /* save return address */ 73 74 mr 13,6 /* save args */ 75 mr 12,7 /* save argslen */ 76 lis 8,ofentry@ha 77 stw 5,ofentry@l(8) /* save virtual mode firmware entry */ 78 79 lis 3,fwsave@ha /* save the mmu values of the 80 firmware */ 81 addi 3,3,fwsave@l 82 bl savemmu 83 84 lis 5,fwentry@ha /* get new firmware entry */ 85 addi 5,5,fwentry@l 86 87 mr 6,13 /* restore args pointer */ 88 mr 7,12 /* restore args length */ 89 mtlr 31 /* restore return address */ 90 blr 91 92/* 93 * Emulated firmware entry. 94 */ 95fwentry: 96 mflr 0 /* save return address */ 97 stw 0,4(1) 98 stwu 1,-16(1) /* setup stack frame */ 99 stw 3,8(1) /* save arg */ 100 101 lis 3,clsave@ha /* save mmu values of client */ 102 addi 3,3,clsave@l 103 bl savemmu 104 105 lis 3,fwsave@ha /* restore mmu values of firmware */ 106 addi 3,3,fwsave@l 107 bl restoremmu 108 109 lis 3,ofentry@ha 110 lwz 3,ofentry@l(3) /* get actual firmware entry */ 111 mtlr 3 112 113 mfmsr 4 114 ori 4,4,PSL_IR|PSL_DR /* turn on MMU */ 115 mtmsr 4 116 isync 117 118 lwz 3,8(1) /* restore arg */ 119 blrl /* do actual firmware call */ 120 121 stw 3,8(1) /* save return value */ 122 123 mfmsr 4 124 lis 3,(PSL_IR|PSL_DR)@h /* turn off MMU */ 125 ori 3,3,(PSL_IR|PSL_DR)@l 126 andc 4,4,3 127 mtmsr 4 128 isync 129 130 lis 3,fwsave@ha /* save mmu values of firmare */ 131 addi 3,3,fwsave@l /* (might not be necessary, but... */ 132 bl savemmu 133 134 lis 3,clsave@ha /* restore mmu values of client */ 135 addi 3,3,clsave@l 136 bl restoremmu 137 138 lwz 3,8(1) /* restore return value */ 139 lwz 1,0(1) /* and return */ 140 lwz 0,4(1) 141 mtlr 0 142 blr 143 144/* 145 * Save everyting related to the mmu to the saveare pointed to by r3. 146 */ 147savemmu: 148 149 mfibatl 4,0 /* save BATs */ 150 stw 4,0(3) 151 mfibatu 4,0 152 stw 4,4(3) 153 mfibatl 4,1 154 stw 4,8(3) 155 mfibatu 4,1 156 stw 4,12(3) 157 mfibatl 4,2 158 stw 4,16(3) 159 mfibatu 4,2 160 stw 4,20(3) 161 mfibatl 4,3 162 stw 4,24(3) 163 mfibatu 4,3 164 stw 4,28(3) 165 mfdbatl 4,0 166 stw 4,32(3) 167 mfdbatu 4,0 168 stw 4,36(3) 169 mfdbatl 4,1 170 stw 4,40(3) 171 mfdbatu 4,1 172 stw 4,44(3) 173 mfdbatl 4,2 174 stw 4,48(3) 175 mfdbatu 4,2 176 stw 4,52(3) 177 mfdbatl 4,3 178 stw 4,56(3) 179 mfdbatu 4,3 180 stwu 4,60(3) 181 182 li 4,0 /* save SRs */ 1831: 184 addis 4,4,-0x10000000@ha 185 or. 4,4,4 186 mfsrin 5,4 187 stwu 5,4(3) 188 bne 1b 189 190 mfsprg 4,0 /* save SPRGs */ 191 stw 4,4(3) 192 mfsprg 4,1 193 stw 4,8(3) 194 mfsprg 4,2 195 stw 4,12(3) 196 mfsprg 4,3 197 stw 4,16(3) 198 199 mfsdr1 4 /* save SDR1 */ 200 stw 4,20(3) 201 202 addi 4,3,24 203 mflr 11 204 li 3,EXC_DSI /* save DSI/ISI trap vectors */ 205 li 5,SI1SIZE 206 bl copy 207 208 mtlr 11 209 li 3,EXC_IMISS /* save MISS trap vectors */ 210 li 5,SI2SIZE 211 212copy: 213 li 6,CACHELINE 2141: 215 lwz 7,0(3) 216 lwz 8,4(3) 217 lwz 9,8(3) 218 lwz 10,12(3) 219 stw 7,0(4) 220 stw 8,4(4) 221 stw 9,8(4) 222 stw 10,12(4) 223 lwz 7,16(3) 224 lwz 8,20(3) 225 lwz 9,24(3) 226 lwz 10,28(3) 227 stw 7,16(4) 228 stw 8,20(4) 229 stw 9,24(4) 230 stw 10,28(4) 231 dcbst 0,4 232 icbi 0,4 233 add 3,3,6 234 add 4,4,6 235 subf. 5,6,5 236 bgt 1b 237 238 dcbst 0,4 239 icbi 0,4 240 241 sync 242 isync 243 244 blr 245 246/* 247 * Restore everyting related to the mmu from the saveare pointed to by r3. 248 */ 249restoremmu: 250 251 li 4,0 /* first, invalidate BATs */ 252 mtibatu 0,4 253 mtibatu 1,4 254 mtibatu 2,4 255 mtibatu 3,4 256 mtdbatu 0,4 257 mtdbatu 1,4 258 mtdbatu 2,4 259 mtdbatu 3,4 260 261 lwz 4,0(3) 262 mtibatl 0,4 /* restore BATs */ 263 lwz 4,4(3) 264 mtibatu 0,4 265 lwz 4,8(3) 266 mtibatl 1,4 267 lwz 4,12(3) 268 mtibatu 1,4 269 lwz 4,16(3) 270 mtibatl 2,4 271 lwz 4,20(3) 272 mtibatu 2,4 273 lwz 4,24(3) 274 mtibatl 3,4 275 lwz 4,28(3) 276 mtibatu 3,4 277 lwz 4,32(3) 278 mtdbatl 0,4 279 lwz 4,36(3) 280 mtdbatu 0,4 281 lwz 4,40(3) 282 mtdbatl 1,4 283 lwz 4,44(3) 284 mtdbatu 1,4 285 lwz 4,48(3) 286 mtdbatl 2,4 287 lwz 4,52(3) 288 mtdbatu 2,4 289 lwz 4,56(3) 290 mtdbatl 3,4 291 lwzu 4,60(3) 292 mtdbatu 3,4 293 294 li 4,0 /* restore SRs */ 2951: 296 lwzu 5,4(3) 297 addis 4,4,-0x10000000@ha 298 or. 4,4,4 299 mtsrin 5,4 300 bne 1b 301 302 lwz 4,4(3) 303 mtsprg 0,4 /* restore SPRGs */ 304 lwz 4,8(3) 305 mtsprg 1,4 306 lwz 4,12(3) 307 mtsprg 2,4 308 lwz 4,16(3) 309 mtsprg 3,4 310 311 sync /* remove everything from tlb */ 312 lis 4,0x40000@ha 313 li 5,0x1000 3141: 315 subf. 4,5,4 316 tlbie 4 317 bne 1b 318 319 sync 320 tlbsync 321 sync 322 323 lwz 4,20(3) 324 sync 325 mtsdr1 4 /* restore SDR1 */ 326 327 addi 3,3,24 328 mflr 11 329 li 4,EXC_DSI /* restore DSI/ISI trap vectors */ 330 li 5,SI1SIZE 331 bl copy 332 333 li 4,EXC_IMISS /* restore MISS trap vectors */ 334 li 5,SI2SIZE 335 bl copy 336 337 /* tlbia */ 338 sync 339 li 3,0x40 340 mtctr 3 341 li 4,0 342 1: 343 tlbie 4 344 addi 4,4,0x1000 345 bdnz 1b 346 sync 347 tlbsync 348 sync 349 350 mtlr 11 351 blr 352