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