xref: /openbmc/linux/arch/arm/include/asm/assembler.h (revision ed3768a8)
1 /*
2  *  arch/arm/include/asm/assembler.h
3  *
4  *  Copyright (C) 1996-2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  This file contains arm architecture specific defines
11  *  for the different processors.
12  *
13  *  Do not include any C declarations in this file - it is included by
14  *  assembler source.
15  */
16 #ifndef __ASSEMBLY__
17 #error "Only include this from assembly code"
18 #endif
19 
20 #include <asm/ptrace.h>
21 
22 /*
23  * Endian independent macros for shifting bytes within registers.
24  */
25 #ifndef __ARMEB__
26 #define pull            lsr
27 #define push            lsl
28 #define get_byte_0      lsl #0
29 #define get_byte_1	lsr #8
30 #define get_byte_2	lsr #16
31 #define get_byte_3	lsr #24
32 #define put_byte_0      lsl #0
33 #define put_byte_1	lsl #8
34 #define put_byte_2	lsl #16
35 #define put_byte_3	lsl #24
36 #else
37 #define pull            lsl
38 #define push            lsr
39 #define get_byte_0	lsr #24
40 #define get_byte_1	lsr #16
41 #define get_byte_2	lsr #8
42 #define get_byte_3      lsl #0
43 #define put_byte_0	lsl #24
44 #define put_byte_1	lsl #16
45 #define put_byte_2	lsl #8
46 #define put_byte_3      lsl #0
47 #endif
48 
49 /*
50  * Data preload for architectures that support it
51  */
52 #if __LINUX_ARM_ARCH__ >= 5
53 #define PLD(code...)	code
54 #else
55 #define PLD(code...)
56 #endif
57 
58 /*
59  * This can be used to enable code to cacheline align the destination
60  * pointer when bulk writing to memory.  Experiments on StrongARM and
61  * XScale didn't show this a worthwhile thing to do when the cache is not
62  * set to write-allocate (this would need further testing on XScale when WA
63  * is used).
64  *
65  * On Feroceon there is much to gain however, regardless of cache mode.
66  */
67 #ifdef CONFIG_CPU_FEROCEON
68 #define CALGN(code...) code
69 #else
70 #define CALGN(code...)
71 #endif
72 
73 /*
74  * Enable and disable interrupts
75  */
76 #if __LINUX_ARM_ARCH__ >= 6
77 	.macro	disable_irq_notrace
78 	cpsid	i
79 	.endm
80 
81 	.macro	enable_irq_notrace
82 	cpsie	i
83 	.endm
84 #else
85 	.macro	disable_irq_notrace
86 	msr	cpsr_c, #PSR_I_BIT | SVC_MODE
87 	.endm
88 
89 	.macro	enable_irq_notrace
90 	msr	cpsr_c, #SVC_MODE
91 	.endm
92 #endif
93 
94 	.macro asm_trace_hardirqs_off
95 #if defined(CONFIG_TRACE_IRQFLAGS)
96 	stmdb   sp!, {r0-r3, ip, lr}
97 	bl	trace_hardirqs_off
98 	ldmia	sp!, {r0-r3, ip, lr}
99 #endif
100 	.endm
101 
102 	.macro asm_trace_hardirqs_on_cond, cond
103 #if defined(CONFIG_TRACE_IRQFLAGS)
104 	/*
105 	 * actually the registers should be pushed and pop'd conditionally, but
106 	 * after bl the flags are certainly clobbered
107 	 */
108 	stmdb   sp!, {r0-r3, ip, lr}
109 	bl\cond	trace_hardirqs_on
110 	ldmia	sp!, {r0-r3, ip, lr}
111 #endif
112 	.endm
113 
114 	.macro asm_trace_hardirqs_on
115 	asm_trace_hardirqs_on_cond al
116 	.endm
117 
118 	.macro disable_irq
119 	disable_irq_notrace
120 	asm_trace_hardirqs_off
121 	.endm
122 
123 	.macro enable_irq
124 	asm_trace_hardirqs_on
125 	enable_irq_notrace
126 	.endm
127 /*
128  * Save the current IRQ state and disable IRQs.  Note that this macro
129  * assumes FIQs are enabled, and that the processor is in SVC mode.
130  */
131 	.macro	save_and_disable_irqs, oldcpsr
132 	mrs	\oldcpsr, cpsr
133 	disable_irq
134 	.endm
135 
136 /*
137  * Restore interrupt state previously stored in a register.  We don't
138  * guarantee that this will preserve the flags.
139  */
140 	.macro	restore_irqs_notrace, oldcpsr
141 	msr	cpsr_c, \oldcpsr
142 	.endm
143 
144 	.macro restore_irqs, oldcpsr
145 	tst	\oldcpsr, #PSR_I_BIT
146 	asm_trace_hardirqs_on_cond eq
147 	restore_irqs_notrace \oldcpsr
148 	.endm
149 
150 #define USER(x...)				\
151 9999:	x;					\
152 	.pushsection __ex_table,"a";		\
153 	.align	3;				\
154 	.long	9999b,9001f;			\
155 	.popsection
156 
157 #ifdef CONFIG_SMP
158 #define ALT_SMP(instr...)					\
159 9998:	instr
160 /*
161  * Note: if you get assembler errors from ALT_UP() when building with
162  * CONFIG_THUMB2_KERNEL, you almost certainly need to use
163  * ALT_SMP( W(instr) ... )
164  */
165 #define ALT_UP(instr...)					\
166 	.pushsection ".alt.smp.init", "a"			;\
167 	.long	9998b						;\
168 9997:	instr							;\
169 	.if . - 9997b != 4					;\
170 		.error "ALT_UP() content must assemble to exactly 4 bytes";\
171 	.endif							;\
172 	.popsection
173 #define ALT_UP_B(label)					\
174 	.equ	up_b_offset, label - 9998b			;\
175 	.pushsection ".alt.smp.init", "a"			;\
176 	.long	9998b						;\
177 	W(b)	. + up_b_offset					;\
178 	.popsection
179 #else
180 #define ALT_SMP(instr...)
181 #define ALT_UP(instr...) instr
182 #define ALT_UP_B(label) b label
183 #endif
184 
185 /*
186  * SMP data memory barrier
187  */
188 	.macro	smp_dmb mode
189 #ifdef CONFIG_SMP
190 #if __LINUX_ARM_ARCH__ >= 7
191 	.ifeqs "\mode","arm"
192 	ALT_SMP(dmb)
193 	.else
194 	ALT_SMP(W(dmb))
195 	.endif
196 #elif __LINUX_ARM_ARCH__ == 6
197 	ALT_SMP(mcr	p15, 0, r0, c7, c10, 5)	@ dmb
198 #else
199 #error Incompatible SMP platform
200 #endif
201 	.ifeqs "\mode","arm"
202 	ALT_UP(nop)
203 	.else
204 	ALT_UP(W(nop))
205 	.endif
206 #endif
207 	.endm
208 
209 #ifdef CONFIG_THUMB2_KERNEL
210 	.macro	setmode, mode, reg
211 	mov	\reg, #\mode
212 	msr	cpsr_c, \reg
213 	.endm
214 #else
215 	.macro	setmode, mode, reg
216 	msr	cpsr_c, #\mode
217 	.endm
218 #endif
219 
220 /*
221  * STRT/LDRT access macros with ARM and Thumb-2 variants
222  */
223 #ifdef CONFIG_THUMB2_KERNEL
224 
225 	.macro	usraccoff, instr, reg, ptr, inc, off, cond, abort
226 9999:
227 	.if	\inc == 1
228 	\instr\cond\()bt \reg, [\ptr, #\off]
229 	.elseif	\inc == 4
230 	\instr\cond\()t \reg, [\ptr, #\off]
231 	.else
232 	.error	"Unsupported inc macro argument"
233 	.endif
234 
235 	.pushsection __ex_table,"a"
236 	.align	3
237 	.long	9999b, \abort
238 	.popsection
239 	.endm
240 
241 	.macro	usracc, instr, reg, ptr, inc, cond, rept, abort
242 	@ explicit IT instruction needed because of the label
243 	@ introduced by the USER macro
244 	.ifnc	\cond,al
245 	.if	\rept == 1
246 	itt	\cond
247 	.elseif	\rept == 2
248 	ittt	\cond
249 	.else
250 	.error	"Unsupported rept macro argument"
251 	.endif
252 	.endif
253 
254 	@ Slightly optimised to avoid incrementing the pointer twice
255 	usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
256 	.if	\rept == 2
257 	usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
258 	.endif
259 
260 	add\cond \ptr, #\rept * \inc
261 	.endm
262 
263 #else	/* !CONFIG_THUMB2_KERNEL */
264 
265 	.macro	usracc, instr, reg, ptr, inc, cond, rept, abort
266 	.rept	\rept
267 9999:
268 	.if	\inc == 1
269 	\instr\cond\()bt \reg, [\ptr], #\inc
270 	.elseif	\inc == 4
271 	\instr\cond\()t \reg, [\ptr], #\inc
272 	.else
273 	.error	"Unsupported inc macro argument"
274 	.endif
275 
276 	.pushsection __ex_table,"a"
277 	.align	3
278 	.long	9999b, \abort
279 	.popsection
280 	.endr
281 	.endm
282 
283 #endif	/* CONFIG_THUMB2_KERNEL */
284 
285 	.macro	strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
286 	usracc	str, \reg, \ptr, \inc, \cond, \rept, \abort
287 	.endm
288 
289 	.macro	ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
290 	usracc	ldr, \reg, \ptr, \inc, \cond, \rept, \abort
291 	.endm
292