xref: /OK3568_Linux_fs/u-boot/arch/arm/lib/relocate_64.S (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun/*
2*4882a593Smuzhiyun * relocate - common relocation function for AArch64 U-Boot
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * (C) Copyright 2013
5*4882a593Smuzhiyun * Albert ARIBAUD <albert.u.boot@aribaud.net>
6*4882a593Smuzhiyun * David Feng <fenghua@phytium.com.cn>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * SPDX-License-Identifier:	GPL-2.0+
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun#include <asm-offsets.h>
12*4882a593Smuzhiyun#include <config.h>
13*4882a593Smuzhiyun#include <elf.h>
14*4882a593Smuzhiyun#include <linux/linkage.h>
15*4882a593Smuzhiyun#include <asm/macro.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun/*
18*4882a593Smuzhiyun * void relocate_code (addr_moni)
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * This function relocates the monitor code.
21*4882a593Smuzhiyun * x0 holds the destination address.
22*4882a593Smuzhiyun */
23*4882a593SmuzhiyunENTRY(relocate_code)
24*4882a593Smuzhiyun	stp	x29, x30, [sp, #-32]!	/* create a stack frame */
25*4882a593Smuzhiyun	mov	x29, sp
26*4882a593Smuzhiyun	str	x0, [sp, #16]
27*4882a593Smuzhiyun	/*
28*4882a593Smuzhiyun	 * Copy u-boot from flash to RAM
29*4882a593Smuzhiyun	 */
30*4882a593Smuzhiyun	adrp	x1, __image_copy_start		/* x1 <- address bits [31:12] */
31*4882a593Smuzhiyun	add	x1, x1, :lo12:__image_copy_start/* x1 <- address bits [11:00] */
32*4882a593Smuzhiyun	subs	x9, x0, x1			/* x9 <- Run to copy offset */
33*4882a593Smuzhiyun	b.eq	relocate_done			/* skip relocation */
34*4882a593Smuzhiyun	/*
35*4882a593Smuzhiyun	 * Don't ldr x1, __image_copy_start here, since if the code is already
36*4882a593Smuzhiyun	 * running at an address other than it was linked to, that instruction
37*4882a593Smuzhiyun	 * will load the relocated value of __image_copy_start. To
38*4882a593Smuzhiyun	 * correctly apply relocations, we need to know the linked value.
39*4882a593Smuzhiyun	 *
40*4882a593Smuzhiyun	 * Linked &__image_copy_start, which we know was at
41*4882a593Smuzhiyun	 * CONFIG_SYS_TEXT_BASE, which is stored in _TEXT_BASE, as a non-
42*4882a593Smuzhiyun	 * relocated value, since it isn't a symbol reference.
43*4882a593Smuzhiyun	 */
44*4882a593Smuzhiyun	ldr	x1, _TEXT_BASE		/* x1 <- Linked &__image_copy_start */
45*4882a593Smuzhiyun	subs	x9, x0, x1		/* x9 <- Link to copy offset */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun	adrp	x1, __image_copy_start		/* x1 <- address bits [31:12] */
48*4882a593Smuzhiyun	add	x1, x1, :lo12:__image_copy_start/* x1 <- address bits [11:00] */
49*4882a593Smuzhiyun	adrp	x2, __image_copy_end		/* x2 <- address bits [31:12] */
50*4882a593Smuzhiyun	add	x2, x2, :lo12:__image_copy_end	/* x2 <- address bits [11:00] */
51*4882a593Smuzhiyuncopy_loop:
52*4882a593Smuzhiyun	ldp	x10, x11, [x1], #16	/* copy from source address [x1] */
53*4882a593Smuzhiyun	stp	x10, x11, [x0], #16	/* copy to   target address [x0] */
54*4882a593Smuzhiyun	cmp	x1, x2			/* until source end address [x2] */
55*4882a593Smuzhiyun	b.lo	copy_loop
56*4882a593Smuzhiyun	str	x0, [sp, #24]
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun	/*
59*4882a593Smuzhiyun	 * Fix .rela.dyn relocations
60*4882a593Smuzhiyun	 */
61*4882a593Smuzhiyun	adrp	x2, __rel_dyn_start		/* x2 <- address bits [31:12] */
62*4882a593Smuzhiyun	add	x2, x2, :lo12:__rel_dyn_start	/* x2 <- address bits [11:00] */
63*4882a593Smuzhiyun	adrp	x3, __rel_dyn_end		/* x3 <- address bits [31:12] */
64*4882a593Smuzhiyun	add	x3, x3, :lo12:__rel_dyn_end	/* x3 <- address bits [11:00] */
65*4882a593Smuzhiyunfixloop:
66*4882a593Smuzhiyun	ldp	x0, x1, [x2], #16	/* (x0,x1) <- (SRC location, fixup) */
67*4882a593Smuzhiyun	ldr	x4, [x2], #8		/* x4 <- addend */
68*4882a593Smuzhiyun	and	x1, x1, #0xffffffff
69*4882a593Smuzhiyun	cmp	x1, #R_AARCH64_RELATIVE
70*4882a593Smuzhiyun	bne	fixnext
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun	/* relative fix: store addend plus offset at dest location */
73*4882a593Smuzhiyun	add	x0, x0, x9
74*4882a593Smuzhiyun	add	x4, x4, x9
75*4882a593Smuzhiyun	str	x4, [x0]
76*4882a593Smuzhiyunfixnext:
77*4882a593Smuzhiyun	cmp	x2, x3
78*4882a593Smuzhiyun	b.lo	fixloop
79*4882a593Smuzhiyun
80*4882a593Smuzhiyunrelocate_done:
81*4882a593Smuzhiyun	switch_el x1, 3f, 2f, 1f
82*4882a593Smuzhiyun	bl	hang
83*4882a593Smuzhiyun3:	mrs	x0, sctlr_el3
84*4882a593Smuzhiyun	b	0f
85*4882a593Smuzhiyun2:	mrs	x0, sctlr_el2
86*4882a593Smuzhiyun	b	0f
87*4882a593Smuzhiyun1:	mrs	x0, sctlr_el1
88*4882a593Smuzhiyun0:	tbz	w0, #2, 5f	/* skip flushing cache if disabled */
89*4882a593Smuzhiyun	tbz	w0, #12, 4f	/* skip invalidating i-cache if disabled */
90*4882a593Smuzhiyun	ic	iallu		/* i-cache invalidate all */
91*4882a593Smuzhiyun	isb	sy
92*4882a593Smuzhiyun4:	ldp	x0, x1, [sp, #16]
93*4882a593Smuzhiyun	bl	__asm_flush_dcache_range
94*4882a593Smuzhiyun5:	ldp	x29, x30, [sp],#32
95*4882a593Smuzhiyun	ret
96*4882a593SmuzhiyunENDPROC(relocate_code)
97