xref: /freebsd/sys/powerpc/aim/locore64.S (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
1/* $FreeBSD$ */
2
3/*-
4 * Copyright (C) 2010-2016 Nathan Whitehorn
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30#include "assym.inc"
31
32#include <sys/syscall.h>
33
34#include <machine/trap.h>
35#include <machine/param.h>
36#include <machine/spr.h>
37#include <machine/asm.h>
38
39#ifdef _CALL_ELF
40.abiversion _CALL_ELF
41#endif
42
43/* Glue for linker script */
44.globl  kernbase
45.set    kernbase, KERNBASE
46
47/*
48 * Globals
49 */
50	.data
51	.align 3
52GLOBAL(__startkernel)
53	.llong	begin
54GLOBAL(__endkernel)
55	.llong	end
56GLOBAL(can_wakeup)
57	.llong	0x0
58
59	.align	4
60#define	TMPSTKSZ	16384		/* 16K temporary stack */
61GLOBAL(tmpstk)
62	.space	TMPSTKSZ
63
64TOC_ENTRY(tmpstk)
65TOC_ENTRY(can_wakeup)
66
67/*
68 * Entry point for bootloaders that do not fully implement ELF and start
69 * at the beginning of the image (kexec, notably). In its own section so
70 * that it ends up before any linker-generated call stubs and actually at
71 * the beginning of the image. kexec on some systems also enters at
72 * (start of image) + 0x60, so put a spin loop there.
73 */
74	.section ".text.kboot", "x", @progbits
75kbootentry:
76	b __start
77. = kbootentry + 0x40	/* Magic address used in platform layer */
78	.global smp_spin_sem
79ap_kexec_spin_sem:
80	.long   -1
81. = kbootentry + 0x60	/* Entry point for kexec APs */
82ap_kexec_start:		/* At 0x60 past start, copied to 0x60 by kexec */
83	/* r3 set to CPU ID by kexec */
84
85	/* Invalidate icache for low-memory copy and jump there */
86	li	%r0,0x80
87	dcbst	0,%r0
88	sync
89	icbi	0,%r0
90	isync
91	ba	0x80			/* Absolute branch to next inst */
92
93. = kbootentry + 0x80			/* Aligned to cache line */
941:	or	31,31,31		/* yield */
95	sync
96	lwz	%r1,0x40(0)		/* Spin on ap_kexec_spin_sem */
97	cmpw	%r1,%r3			/* Until it equals our CPU ID */
98	bne	1b
99
100	/* Released */
101	or	2,2,2			/* unyield */
102
103	/* Make sure that it will be software reset. Clear SRR1 */
104	li	%r1,0
105	mtsrr1	%r1
106	ba	EXC_RST
107
108/*
109 * Now start the real text section
110 */
111
112	.text
113	.globl	btext
114btext:
115
116/*
117 * Main kernel entry point.
118 *
119 * Calling convention:
120 * r3: Flattened Device Tree pointer (or zero)
121 * r4: ignored
122 * r5: OF client interface pointer (or zero)
123 * r6: Loader metadata pointer (or zero)
124 * r7: Magic cookie (0xfb5d104d) to indicate that r6 has loader metadata
125 */
126	.text
127ASENTRY_NOPROF(__start)
128	/* Set 64-bit mode if not yet set before branching to C */
129	mfmsr	%r20
130	li	%r21,1
131	insrdi	%r20,%r21,1,0
132	mtmsrd	%r20
133	isync
134	nop	/* Make this block a multiple of 8 bytes */
135
136	/* Set up the TOC pointer */
137	b	0f
138	.align 3
1390:	nop
140	bl	1f
141	.llong	__tocbase + 0x8000 - .
1421:	mflr	%r2
143	ld	%r1,0(%r2)
144	add	%r2,%r1,%r2
145
146	/* Get load offset */
147	ld	%r31,-0x8000(%r2) /* First TOC entry is TOC base */
148	subf    %r31,%r31,%r2	/* Subtract from real TOC base to get base */
149
150	/* Set up the stack pointer */
151	bl	1f
152	.llong	tmpstk + TMPSTKSZ - 96 - .
1531:	mflr	%r30
154	ld	%r1,0(%r30)
155	add	%r1,%r1,%r30
156	nop
157
158	/* Relocate kernel */
159	std	%r3,48(%r1)
160	std	%r4,56(%r1)
161	std	%r5,64(%r1)
162	std	%r6,72(%r1)
163	std	%r7,80(%r1)
164
165	bl	1f
166	.llong _DYNAMIC-.
1671:	mflr	%r3
168	ld	%r4,0(%r3)
169	add	%r3,%r4,%r3
170	mr	%r4,%r31
171	bl	elf_reloc_self
172	nop
173	ld	%r3,48(%r1)
174	ld	%r4,56(%r1)
175	ld	%r5,64(%r1)
176	ld	%r6,72(%r1)
177	ld	%r7,80(%r1)
178
179	/* Begin CPU init */
180	mr	%r4,%r2 /* Replace ignored r4 with tocbase for trap handlers */
181	bl	powerpc_init
182	nop
183
184	/* Set stack pointer to new value and branch to mi_startup */
185	mr	%r1, %r3
186	li	%r3, 0
187	std	%r3, 0(%r1)
188	bl	mi_startup
189	nop
190
191	/* Unreachable */
192	b	.
193
194ASENTRY_NOPROF(__restartkernel)
195	/*
196	 * r3-r7: arguments to go to __start
197	 * r8: offset from current kernel address to apply
198	 * r9: MSR to set when (atomically) jumping to __start + r8
199	 */
200	mtsrr1	%r9
201	bl	1f
2021:	mflr	%r25
203	add	%r25,%r8,%r25
204	addi	%r25,%r25,2f-1b
205	mtsrr0	%r25
206	rfid
2072:	bl	__start
208	nop
209
210#include <powerpc/aim/trap_subr64.S>
211