xref: /freebsd/sys/powerpc/powernv/opalcall.S (revision 81ad6265)
1/*-
2 * Copyright (C) 2015 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 <machine/asm.h>
29
30#include "opt_platform.h"
31
32GLOBAL(opal_entrypoint)
33	.llong	0
34GLOBAL(opal_data)
35	.llong	0
36GLOBAL(opal_msr)
37	.llong	0
38
39TOC_ENTRY(opal_entrypoint)
40TOC_ENTRY(opal_data)
41TOC_ENTRY(opal_msr)
42
43ASENTRY(opal_call)
44	/* Args:
45	 * r3: opal token
46	 * r4-r10 opal arguments
47	 */
48
49	/* Save call stuff on stack */
50	mflr	%r0
51	std	%r0,16(%r1)
52	std	%r2,-16(%r1)
53	mfcr	%r0
54	std	%r0,8(%r1)
55
56	/* Load OPAL entry information */
57	mr	%r0,%r3
58	addis	%r3,%r2,TOC_REF(opal_entrypoint)@ha
59	ld	%r3,TOC_REF(opal_entrypoint)@l(%r3)
60	ld	%r3,0(%r3)
61	mtctr	%r3
62
63	/* Save MSR in non-volatile scratch register and turn off translation */
64	std	%r31,-8(%r1)
65	mfmsr	%r31
66
67	/* Load last bits from the TOC */
68	addis	%r3,%r2,TOC_REF(opal_msr)@ha
69	ld	%r3,TOC_REF(opal_msr)@l(%r3)
70	ld	%r3,0(%r3)
71	addis	%r2,%r2,TOC_REF(opal_data)@ha
72	ld	%r2,TOC_REF(opal_data)@l(%r2)
73	ld	%r2,0(%r2)
74
75
76#if defined(__LITTLE_ENDIAN__) && defined(QEMU)
77	/* QEMU hack: qemu does not emulate mtmsrd correctly! */
78	ori     %r3,%r3,1       /* Leave PSR_LE set */
79#endif
80	mtmsrd	%r3
81	isync
82#if defined(__LITTLE_ENDIAN__) && defined(QEMU)
83	/* Clean up from qemu hack */
84	xori	%r3,%r3,1
85#endif
86
87#ifdef __LITTLE_ENDIAN__
88	mtsrr1	%r3
89#endif
90
91	/* Shift registers over */
92	mr	%r3,%r4
93	mr	%r4,%r5
94	mr	%r5,%r6
95	mr	%r6,%r7
96	mr	%r7,%r8
97	mr	%r8,%r9
98	mr	%r9,%r10
99
100#ifdef __LITTLE_ENDIAN__
101	/* We need to rfid to switch endian. */
102	mfctr	%r11
103	mtsrr0	%r11
104	LOAD_LR_NIA
1051:
106	mflr	%r11
107	addi	%r11, %r11, (2f-1b)
108	mtlr	%r11
109	/* Call OPAL */
110	rfid
1112:
112	RETURN_TO_NATIVE_ENDIAN
113#else
114	/* Call OPAL */
115	bctrl
116#endif
117
118	/* Restore MSR */
119	mtmsrd	%r31
120	isync
121	ld	%r31,-8(%r1)
122
123	/* Restore call stuff from stack */
124	ld	%r0,16(%r1)
125	mtlr	%r0
126	ld	%r2,-16(%r1)
127	ld	%r0,8(%r1)
128	mtcr	%r0
129
130	/* And return */
131	blr
132ASEND(opal_call)
133