1/*
2 * kexec for arm64
3 *
4 * Copyright (C) Linaro.
5 * Copyright (C) Huawei Futurewei Technologies.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/kexec.h>
13#include <linux/linkage.h>
14
15#include <asm/assembler.h>
16#include <asm/kexec.h>
17#include <asm/page.h>
18#include <asm/sysreg.h>
19
20/*
21 * arm64_relocate_new_kernel - Put a 2nd stage image in place and boot it.
22 *
23 * The memory that the old kernel occupies may be overwritten when coping the
24 * new image to its final location.  To assure that the
25 * arm64_relocate_new_kernel routine which does that copy is not overwritten,
26 * all code and data needed by arm64_relocate_new_kernel must be between the
27 * symbols arm64_relocate_new_kernel and arm64_relocate_new_kernel_end.  The
28 * machine_kexec() routine will copy arm64_relocate_new_kernel to the kexec
29 * control_code_page, a special page which has been set up to be preserved
30 * during the copy operation.
31 */
32ENTRY(arm64_relocate_new_kernel)
33
34	/* Setup the list loop variables. */
35	mov	x17, x1				/* x17 = kimage_start */
36	mov	x16, x0				/* x16 = kimage_head */
37	raw_dcache_line_size x15, x0		/* x15 = dcache line size */
38	mov	x14, xzr			/* x14 = entry ptr */
39	mov	x13, xzr			/* x13 = copy dest */
40
41	/* Clear the sctlr_el2 flags. */
42	mrs	x0, CurrentEL
43	cmp	x0, #CurrentEL_EL2
44	b.ne	1f
45	mrs	x0, sctlr_el2
46	ldr	x1, =SCTLR_ELx_FLAGS
47	bic	x0, x0, x1
48	pre_disable_mmu_workaround
49	msr	sctlr_el2, x0
50	isb
511:
52
53	/* Check if the new image needs relocation. */
54	tbnz	x16, IND_DONE_BIT, .Ldone
55
56.Lloop:
57	and	x12, x16, PAGE_MASK		/* x12 = addr */
58
59	/* Test the entry flags. */
60.Ltest_source:
61	tbz	x16, IND_SOURCE_BIT, .Ltest_indirection
62
63	/* Invalidate dest page to PoC. */
64	mov     x0, x13
65	add     x20, x0, #PAGE_SIZE
66	sub     x1, x15, #1
67	bic     x0, x0, x1
682:	dc      ivac, x0
69	add     x0, x0, x15
70	cmp     x0, x20
71	b.lo    2b
72	dsb     sy
73
74	mov x20, x13
75	mov x21, x12
76	copy_page x20, x21, x0, x1, x2, x3, x4, x5, x6, x7
77
78	/* dest += PAGE_SIZE */
79	add	x13, x13, PAGE_SIZE
80	b	.Lnext
81
82.Ltest_indirection:
83	tbz	x16, IND_INDIRECTION_BIT, .Ltest_destination
84
85	/* ptr = addr */
86	mov	x14, x12
87	b	.Lnext
88
89.Ltest_destination:
90	tbz	x16, IND_DESTINATION_BIT, .Lnext
91
92	/* dest = addr */
93	mov	x13, x12
94
95.Lnext:
96	/* entry = *ptr++ */
97	ldr	x16, [x14], #8
98
99	/* while (!(entry & DONE)) */
100	tbz	x16, IND_DONE_BIT, .Lloop
101
102.Ldone:
103	/* wait for writes from copy_page to finish */
104	dsb	nsh
105	ic	iallu
106	dsb	nsh
107	isb
108
109	/* Start new image. */
110	mov	x0, xzr
111	mov	x1, xzr
112	mov	x2, xzr
113	mov	x3, xzr
114	br	x17
115
116ENDPROC(arm64_relocate_new_kernel)
117
118.ltorg
119
120.align 3	/* To keep the 64-bit values below naturally aligned. */
121
122.Lcopy_end:
123.org	KEXEC_CONTROL_PAGE_SIZE
124
125/*
126 * arm64_relocate_new_kernel_size - Number of bytes to copy to the
127 * control_code_page.
128 */
129.globl arm64_relocate_new_kernel_size
130arm64_relocate_new_kernel_size:
131	.quad	.Lcopy_end - arm64_relocate_new_kernel
132