1/* $NetBSD: ofwreal.S,v 1.12 2011/01/17 08:23:56 matt 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 %r31 /* save return address */ 73 74 mr %r13,%r6 /* save args */ 75 mr %r12,%r7 /* save argslen */ 76 lis %r8,ofentry@ha 77 stw %r5,ofentry@l(%r8) /* save virtual mode firmware entry */ 78 79 lis %r3,fwsave@ha /* save the mmu values of the 80 firmware */ 81 addi %r3,%r3,fwsave@l 82 bl savemmu 83 84 lis %r5,fwentry@ha /* get new firmware entry */ 85 addi %r5,%r5,fwentry@l 86 87 mr %r6,%r13 /* restore args pointer */ 88 mr %r7,%r12 /* restore args length */ 89 mtlr %r31 /* restore return address */ 90 blr 91 92/* 93 * Emulated firmware entry. 94 */ 95fwentry: 96 mflr %r0 /* save return address */ 97 stw %r0,4(%r1) 98 stwu %r1,-16(%r1) /* setup stack frame */ 99 stw %r3,8(%r1) /* save arg */ 100 101 lis %r3,clsave@ha /* save mmu values of client */ 102 addi %r3,%r3,clsave@l 103 bl savemmu 104 105 lis %r3,fwsave@ha /* restore mmu values of firmware */ 106 addi %r3,%r3,fwsave@l 107 bl restoremmu 108 109 lis %r3,ofentry@ha 110 lwz %r3,ofentry@l(%r3) /* get actual firmware entry */ 111 mtlr %r3 112 113 mfmsr %r4 114 stw %r4,12(%r1) /* save MSR */ 115 ori %r4,%r4,PSL_IR|PSL_DR /* turn on MMU */ 116 andi. %r4,%r4,~PSL_EE@l /* turn off interrupts */ 117 mtmsr %r4 118 isync 119 120 lwz %r3,8(%r1) /* restore arg */ 121 blrl /* do actual firmware call */ 122 stw %r3,8(%r1) /* save return value */ 123 124 lwz %r4,12(%r1) /* get saved MSR */ 125 mtmsr %r4 126 isync 127 128 lis %r3,fwsave@ha /* save mmu values of firmare */ 129 addi %r3,%r3,fwsave@l /* (might not be necessary, but... */ 130 bl savemmu 131 132 lis %r3,clsave@ha /* restore mmu values of client */ 133 addi %r3,%r3,clsave@l 134 bl restoremmu 135 136 lwz %r3,8(%r1) /* restore return value */ 137 lwz %r1,0(%r1) /* and return */ 138 lwz %r0,4(%r1) 139 mtlr %r0 140 blr 141 142/* 143 * Save everything related to the mmu to the saveare pointed to by r3. 144 */ 145savemmu: 146 147 mfibatl %r4,0 /* save BATs */ 148 stw %r4,0(%r3) 149 mfibatu %r4,0 150 stw %r4,4(%r3) 151 mfibatl %r4,1 152 stw %r4,8(%r3) 153 mfibatu %r4,1 154 stw %r4,12(%r3) 155 mfibatl %r4,2 156 stw %r4,16(%r3) 157 mfibatu %r4,2 158 stw %r4,20(%r3) 159 mfibatl %r4,3 160 stw %r4,24(%r3) 161 mfibatu %r4,3 162 stw %r4,28(%r3) 163 mfdbatl %r4,0 164 stw %r4,32(%r3) 165 mfdbatu %r4,0 166 stw %r4,36(%r3) 167 mfdbatl %r4,1 168 stw %r4,40(%r3) 169 mfdbatu %r4,1 170 stw %r4,44(%r3) 171 mfdbatl %r4,2 172 stw %r4,48(%r3) 173 mfdbatu %r4,2 174 stw %r4,52(%r3) 175 mfdbatl %r4,3 176 stw %r4,56(%r3) 177 mfdbatu %r4,3 178 stwu %r4,60(%r3) 179 180 li %r4,0 /* save SRs */ 1811: 182 addis %r4,%r4,-0x10000000@ha 183 or. %r4,%r4,%r4 184 mfsrin %r5,%r4 185 stwu %r5,4(%r3) 186 bne 1b 187 188 mfsprg0 %r4 /* save SPRGs */ 189 stw %r4,4(%r3) 190 mfsprg1 %r4 191 stw %r4,8(%r3) 192 mfsprg2 %r4 193 stw %r4,12(%r3) 194 mfsprg3 %r4 195 stw %r4,16(%r3) 196 197 mfsdr1 %r4 /* save SDR1 */ 198 stw %r4,20(%r3) 199 200 addi %r4,%r3,24 201 mflr %r11 202 li %r3,EXC_DSI /* save DSI/ISI trap vectors */ 203 li %r5,SI1SIZE 204 bl copy 205 206 mtlr %r11 207 li %r3,EXC_IMISS /* save MISS trap vectors */ 208 li %r5,SI2SIZE 209 210copy: 211 li %r6,CACHELINE 2121: 213 lwz %r7,0(%r3) 214 lwz %r8,4(%r3) 215 lwz %r9,8(%r3) 216 lwz %r10,12(%r3) 217 stw %r7,0(%r4) 218 stw %r8,4(%r4) 219 stw %r9,8(%r4) 220 stw %r10,12(%r4) 221 lwz %r7,16(%r3) 222 lwz %r8,20(%r3) 223 lwz %r9,24(%r3) 224 lwz %r10,28(%r3) 225 stw %r7,16(%r4) 226 stw %r8,20(%r4) 227 stw %r9,24(%r4) 228 stw %r10,28(%r4) 229 dcbst %r0,%r4 230 icbi %r0,%r4 231 add %r3,%r3,%r6 232 add %r4,%r4,%r6 233 subf. %r5,%r6,%r5 234 bgt 1b 235 236 dcbst %r0,%r4 237 icbi %r0,%r4 238 239 sync 240 isync 241 242 blr 243 244/* 245 * Restore everyting related to the mmu from the saveare pointed to by r3. 246 */ 247restoremmu: 248 mfmsr %r12 249 andi. %r4,%r12,~(PSL_IR|PSL_DR)@l 250 mtmsr %r4 /* Disable MMU */ 251 isync 252 253 li %r4,0 /* first, invalidate BATs */ 254 mtibatu 0,%r4 255 mtibatu 1,%r4 256 mtibatu 2,%r4 257 mtibatu 3,%r4 258 mtdbatu 0,%r4 259 mtdbatu 1,%r4 260 mtdbatu 2,%r4 261 mtdbatu 3,%r4 262 263 lwz %r4,0(%r3) 264 mtibatl 0,%r4 /* restore BATs */ 265 lwz %r4,4(%r3) 266 mtibatu 0,%r4 267 lwz %r4,8(%r3) 268 mtibatl 1,%r4 269 lwz %r4,12(%r3) 270 mtibatu 1,%r4 271 lwz %r4,16(%r3) 272 mtibatl 2,%r4 273 lwz %r4,20(%r3) 274 mtibatu 2,%r4 275 lwz %r4,24(%r3) 276 mtibatl 3,%r4 277 lwz %r4,28(%r3) 278 mtibatu 3,%r4 279 lwz %r4,32(%r3) 280 mtdbatl 0,%r4 281 lwz %r4,36(%r3) 282 mtdbatu 0,%r4 283 lwz %r4,40(%r3) 284 mtdbatl 1,%r4 285 lwz %r4,44(%r3) 286 mtdbatu 1,%r4 287 lwz %r4,48(%r3) 288 mtdbatl 2,%r4 289 lwz %r4,52(%r3) 290 mtdbatu 2,%r4 291 lwz %r4,56(%r3) 292 mtdbatl 3,%r4 293 lwzu %r4,60(%r3) 294 mtdbatu 3,%r4 295 296 li %r4,0 /* restore SRs */ 2971: 298 lwzu %r5,4(%r3) 299 addis %r4,%r4,-0x10000000@ha 300 or. %r4,%r4,%r4 301 mtsrin %r5,%r4 302 bne 1b 303 304 lwz %r4,4(%r3) 305 mtsprg0 %r4 /* restore SPRGs */ 306 lwz %r4,8(%r3) 307 mtsprg1 %r4 308 lwz %r4,12(%r3) 309 mtsprg2 %r4 310 lwz %r4,16(%r3) 311 mtsprg3 %r4 312 313 sync /* remove everything from tlb */ 314 lis %r4,0x40000@ha 315 li %r5,0x1000 3161: 317 subf. %r4,%r5,%r4 318 tlbie %r4 319 bne 1b 320 321 sync 322 tlbsync 323 sync 324 325 lwz %r4,20(%r3) 326 sync 327 mtsdr1 %r4 /* restore SDR1 */ 328 329 addi %r3,%r3,24 330 mflr %r11 331 li %r4,EXC_DSI /* restore DSI/ISI trap vectors */ 332 li %r5,SI1SIZE 333 bl copy 334 335 li %r4,EXC_IMISS /* restore MISS trap vectors */ 336 li %r5,SI2SIZE 337 bl copy 338 339 /* tlbia */ 340 sync 341 li %r3,0x40 342 mtctr %r3 343 li %r4,0 344 1: 345 tlbie %r4 346 addi %r4,%r4,0x1000 347 bdnz 1b 348 sync 349 tlbsync 350 sync 351 352 mtmsr %r12 /* restore MMU */ 353 mtlr %r11 354 blr 355