1/*-
2 * Copyright (C) 2009-2011 Nathan Whitehorn
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD$
26 */
27
28#include <sys/syscall.h>
29
30#include <machine/trap.h>
31#include <machine/param.h>
32#include <machine/spr.h>
33#include <machine/asm.h>
34
35#define	OFWSTKSZ	4096		/* 4K Open Firmware stack */
36
37/*
38 * Globals
39 */
40	.data
41GLOBAL(ofmsr)
42	.long	0, 0, 0, 0, 0		/* msr/sprg0-3 used in Open Firmware */
43GLOBAL(rtasmsr)
44	.long	0
45GLOBAL(openfirmware_entry)
46	.long	0			/* Open Firmware entry point */
47GLOBAL(rtas_entry)
48	.long	0			/* RTAS entry point */
49
50	.align	4
51ofwstk:
52	.space	OFWSTKSZ
53rtas_regsave:
54	.space	4
55
56/*
57 * Open Firmware Entry Point. May need to enter real mode.
58 *
59 * C prototype: int ofwcall(void *callbuffer);
60 */
61
62ASENTRY(ofwcall)
63	mflr	%r0
64	stw	%r0,4(%r1)
65
66	/* Record the old MSR */
67	mfmsr	%r6
68
69	/* read client interface handler */
70	lis	%r4,openfirmware_entry@ha
71	lwz	%r4,openfirmware_entry@l(%r4)
72
73	/*
74	 * Set the MSR to the OF value. This has the side effect of disabling
75	 * exceptions, which prevents preemption later.
76	 */
77
78	lis	%r5,ofmsr@ha
79	lwz	%r5,ofmsr@l(%r5)
80	mtmsr	%r5
81	isync
82
83	/*
84	 * Set up OF stack. This needs to be potentially accessible in real mode
85	 * The pointer to the current kernel stack is placed at the very
86	 * top of the stack along with the old MSR so we can get them back
87	 * later.
88	 */
89	mr	%r5,%r1
90	lis	%r1,(ofwstk+OFWSTKSZ-32)@ha
91	addi	%r1,%r1,(ofwstk+OFWSTKSZ-32)@l
92	stw	%r5,20(%r1)	/* Save real stack pointer */
93	stw	%r2,24(%r1)	/* Save curthread */
94	stw	%r6,28(%r1)	/* Save old MSR */
95	li	%r5,0
96	stw	%r5,4(%r1)
97	stw	%r5,0(%r1)
98
99	/* Finally, branch to OF */
100	mtctr	%r4
101	bctrl
102
103	/* Reload stack pointer and MSR from the OFW stack */
104	lwz	%r6,28(%r1)
105	lwz	%r2,24(%r1)
106	lwz	%r1,20(%r1)
107
108	/* Now set the real MSR */
109	mtmsr	%r6
110	isync
111
112	/* Return */
113	lwz	%r0,4(%r1)
114	mtlr 	%r0
115	blr
116
117/*
118 * RTAS Entry Point. Similar to the OF one, but simpler (no separate stack)
119 *
120 * C prototype: int rtascall(void *callbuffer, void *rtas_privdat);
121 */
122
123ASENTRY(rtascall)
124	mflr	%r0
125	stw	%r0,4(%r1)
126
127	/* Record the old MSR to real-mode-accessible area */
128	mfmsr	%r0
129	lis	%r5,rtas_regsave@ha
130	stw	%r0,rtas_regsave@l(%r5)
131
132	/* read client interface handler */
133	lis	%r5,rtas_entry@ha
134	lwz	%r5,rtas_entry@l(%r5)
135
136	/* Set the MSR to the RTAS value */
137	lis	%r6,rtasmsr@ha
138	lwz	%r6,rtasmsr@l(%r6)
139	mtmsr	%r6
140	isync
141
142	/* Branch to RTAS */
143	mtctr	%r5
144	bctrl
145
146	/* Now set the MSR back */
147	lis	%r6,rtas_regsave@ha
148	lwz	%r6,rtas_regsave@l(%r6)
149	mtmsr	%r6
150	isync
151
152	/* And return */
153	lwz	%r0,4(%r1)
154	mtlr 	%r0
155	blr
156
157