blob: eb87200aa4b566645677523e0751f03bcbb06e0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King4baa9922008-08-02 10:55:55 +01002 * arch/arm/include/asm/assembler.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 */
Magnus Damm2bc58a62011-06-13 06:46:44 +010016#ifndef __ASM_ASSEMBLER_H__
17#define __ASM_ASSEMBLER_H__
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#ifndef __ASSEMBLY__
20#error "Only include this from assembly code"
21#endif
22
23#include <asm/ptrace.h>
Catalin Marinas247055a2010-09-13 16:03:21 +010024#include <asm/domain.h>
Dave Martin80c59da2012-02-09 08:47:17 -080025#include <asm/opcodes-virt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Rob Herring6f6f6a72012-03-10 10:30:31 -060027#define IOMEM(x) (x)
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * Endian independent macros for shifting bytes within registers.
31 */
32#ifndef __ARMEB__
33#define pull lsr
34#define push lsl
35#define get_byte_0 lsl #0
36#define get_byte_1 lsr #8
37#define get_byte_2 lsr #16
38#define get_byte_3 lsr #24
39#define put_byte_0 lsl #0
40#define put_byte_1 lsl #8
41#define put_byte_2 lsl #16
42#define put_byte_3 lsl #24
43#else
44#define pull lsl
45#define push lsr
46#define get_byte_0 lsr #24
47#define get_byte_1 lsr #16
48#define get_byte_2 lsr #8
49#define get_byte_3 lsl #0
50#define put_byte_0 lsl #24
51#define put_byte_1 lsl #16
52#define put_byte_2 lsl #8
53#define put_byte_3 lsl #0
54#endif
55
56/*
57 * Data preload for architectures that support it
58 */
59#if __LINUX_ARM_ARCH__ >= 5
60#define PLD(code...) code
61#else
62#define PLD(code...)
63#endif
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Nicolas Pitre2239aff2008-03-31 12:38:31 -040066 * This can be used to enable code to cacheline align the destination
67 * pointer when bulk writing to memory. Experiments on StrongARM and
68 * XScale didn't show this a worthwhile thing to do when the cache is not
69 * set to write-allocate (this would need further testing on XScale when WA
70 * is used).
71 *
72 * On Feroceon there is much to gain however, regardless of cache mode.
73 */
74#ifdef CONFIG_CPU_FEROCEON
75#define CALGN(code...) code
76#else
77#define CALGN(code...)
78#endif
79
80/*
Russell King9c429542006-03-23 16:59:37 +000081 * Enable and disable interrupts
82 */
83#if __LINUX_ARM_ARCH__ >= 6
Uwe Kleine-König0d928b02009-08-13 20:38:17 +020084 .macro disable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +000085 cpsid i
86 .endm
87
Uwe Kleine-König0d928b02009-08-13 20:38:17 +020088 .macro enable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +000089 cpsie i
90 .endm
91#else
Uwe Kleine-König0d928b02009-08-13 20:38:17 +020092 .macro disable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +000093 msr cpsr_c, #PSR_I_BIT | SVC_MODE
94 .endm
95
Uwe Kleine-König0d928b02009-08-13 20:38:17 +020096 .macro enable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +000097 msr cpsr_c, #SVC_MODE
98 .endm
99#endif
100
Uwe Kleine-König0d928b02009-08-13 20:38:17 +0200101 .macro asm_trace_hardirqs_off
102#if defined(CONFIG_TRACE_IRQFLAGS)
103 stmdb sp!, {r0-r3, ip, lr}
104 bl trace_hardirqs_off
105 ldmia sp!, {r0-r3, ip, lr}
106#endif
107 .endm
108
109 .macro asm_trace_hardirqs_on_cond, cond
110#if defined(CONFIG_TRACE_IRQFLAGS)
111 /*
112 * actually the registers should be pushed and pop'd conditionally, but
113 * after bl the flags are certainly clobbered
114 */
115 stmdb sp!, {r0-r3, ip, lr}
116 bl\cond trace_hardirqs_on
117 ldmia sp!, {r0-r3, ip, lr}
118#endif
119 .endm
120
121 .macro asm_trace_hardirqs_on
122 asm_trace_hardirqs_on_cond al
123 .endm
124
125 .macro disable_irq
126 disable_irq_notrace
127 asm_trace_hardirqs_off
128 .endm
129
130 .macro enable_irq
131 asm_trace_hardirqs_on
132 enable_irq_notrace
133 .endm
Russell King9c429542006-03-23 16:59:37 +0000134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * Save the current IRQ state and disable IRQs. Note that this macro
136 * assumes FIQs are enabled, and that the processor is in SVC mode.
137 */
Russell King59d1ff32005-11-09 15:04:22 +0000138 .macro save_and_disable_irqs, oldcpsr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 mrs \oldcpsr, cpsr
Russell King9c429542006-03-23 16:59:37 +0000140 disable_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .endm
142
Rabin Vincent8e43a902012-02-15 16:01:42 +0100143 .macro save_and_disable_irqs_notrace, oldcpsr
144 mrs \oldcpsr, cpsr
145 disable_irq_notrace
146 .endm
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * Restore interrupt state previously stored in a register. We don't
150 * guarantee that this will preserve the flags.
151 */
Uwe Kleine-König0d928b02009-08-13 20:38:17 +0200152 .macro restore_irqs_notrace, oldcpsr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 msr cpsr_c, \oldcpsr
154 .endm
155
Uwe Kleine-König0d928b02009-08-13 20:38:17 +0200156 .macro restore_irqs, oldcpsr
157 tst \oldcpsr, #PSR_I_BIT
158 asm_trace_hardirqs_on_cond eq
159 restore_irqs_notrace \oldcpsr
160 .endm
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define USER(x...) \
1639999: x; \
Russell King42604152010-04-19 10:15:03 +0100164 .pushsection __ex_table,"a"; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .align 3; \
166 .long 9999b,9001f; \
Russell King42604152010-04-19 10:15:03 +0100167 .popsection
Russell Kingbac4e962009-05-25 20:58:00 +0100168
Russell Kingf00ec482010-09-04 10:47:48 +0100169#ifdef CONFIG_SMP
170#define ALT_SMP(instr...) \
1719998: instr
Dave Martined3768a2010-12-01 15:39:23 +0100172/*
173 * Note: if you get assembler errors from ALT_UP() when building with
174 * CONFIG_THUMB2_KERNEL, you almost certainly need to use
175 * ALT_SMP( W(instr) ... )
176 */
Russell Kingf00ec482010-09-04 10:47:48 +0100177#define ALT_UP(instr...) \
178 .pushsection ".alt.smp.init", "a" ;\
179 .long 9998b ;\
Dave Martined3768a2010-12-01 15:39:23 +01001809997: instr ;\
181 .if . - 9997b != 4 ;\
182 .error "ALT_UP() content must assemble to exactly 4 bytes";\
183 .endif ;\
Russell Kingf00ec482010-09-04 10:47:48 +0100184 .popsection
185#define ALT_UP_B(label) \
186 .equ up_b_offset, label - 9998b ;\
187 .pushsection ".alt.smp.init", "a" ;\
188 .long 9998b ;\
Dave Martined3768a2010-12-01 15:39:23 +0100189 W(b) . + up_b_offset ;\
Russell Kingf00ec482010-09-04 10:47:48 +0100190 .popsection
191#else
192#define ALT_SMP(instr...)
193#define ALT_UP(instr...) instr
194#define ALT_UP_B(label) b label
195#endif
196
Russell Kingbac4e962009-05-25 20:58:00 +0100197/*
Will Deacond675d0b2011-11-22 17:30:28 +0000198 * Instruction barrier
199 */
200 .macro instr_sync
201#if __LINUX_ARM_ARCH__ >= 7
202 isb
203#elif __LINUX_ARM_ARCH__ == 6
204 mcr p15, 0, r0, c7, c5, 4
205#endif
206 .endm
207
208/*
Russell Kingbac4e962009-05-25 20:58:00 +0100209 * SMP data memory barrier
210 */
Dave Martined3768a2010-12-01 15:39:23 +0100211 .macro smp_dmb mode
Russell Kingbac4e962009-05-25 20:58:00 +0100212#ifdef CONFIG_SMP
213#if __LINUX_ARM_ARCH__ >= 7
Dave Martined3768a2010-12-01 15:39:23 +0100214 .ifeqs "\mode","arm"
Russell Kingf00ec482010-09-04 10:47:48 +0100215 ALT_SMP(dmb)
Dave Martined3768a2010-12-01 15:39:23 +0100216 .else
217 ALT_SMP(W(dmb))
218 .endif
Russell Kingbac4e962009-05-25 20:58:00 +0100219#elif __LINUX_ARM_ARCH__ == 6
Russell Kingf00ec482010-09-04 10:47:48 +0100220 ALT_SMP(mcr p15, 0, r0, c7, c10, 5) @ dmb
221#else
222#error Incompatible SMP platform
Russell Kingbac4e962009-05-25 20:58:00 +0100223#endif
Dave Martined3768a2010-12-01 15:39:23 +0100224 .ifeqs "\mode","arm"
Russell Kingf00ec482010-09-04 10:47:48 +0100225 ALT_UP(nop)
Dave Martined3768a2010-12-01 15:39:23 +0100226 .else
227 ALT_UP(W(nop))
228 .endif
Russell Kingbac4e962009-05-25 20:58:00 +0100229#endif
230 .endm
Catalin Marinasb86040a2009-07-24 12:32:54 +0100231
232#ifdef CONFIG_THUMB2_KERNEL
233 .macro setmode, mode, reg
234 mov \reg, #\mode
235 msr cpsr_c, \reg
236 .endm
237#else
238 .macro setmode, mode, reg
239 msr cpsr_c, #\mode
240 .endm
241#endif
Catalin Marinas8b592782009-07-24 12:32:57 +0100242
243/*
Dave Martin80c59da2012-02-09 08:47:17 -0800244 * Helper macro to enter SVC mode cleanly and mask interrupts. reg is
245 * a scratch register for the macro to overwrite.
246 *
247 * This macro is intended for forcing the CPU into SVC mode at boot time.
248 * you cannot return to the original mode.
249 *
250 * Beware, it also clobers LR.
251 */
252.macro safe_svcmode_maskall reg:req
Dave Martin1ecec692012-12-10 18:35:22 +0100253#if __LINUX_ARM_ARCH__ >= 6
Dave Martin80c59da2012-02-09 08:47:17 -0800254 mrs \reg , cpsr
255 mov lr , \reg
256 and lr , lr , #MODE_MASK
257 cmp lr , #HYP_MODE
Marc Zyngier2a552d52012-10-06 17:03:17 +0100258 orr \reg , \reg , #PSR_I_BIT | PSR_F_BIT
Dave Martin80c59da2012-02-09 08:47:17 -0800259 bic \reg , \reg , #MODE_MASK
260 orr \reg , \reg , #SVC_MODE
261THUMB( orr \reg , \reg , #PSR_T_BIT )
Dave Martin80c59da2012-02-09 08:47:17 -0800262 bne 1f
Marc Zyngier2a552d52012-10-06 17:03:17 +0100263 orr \reg, \reg, #PSR_A_BIT
264 adr lr, BSYM(2f)
265 msr spsr_cxsf, \reg
Dave Martin80c59da2012-02-09 08:47:17 -0800266 __MSR_ELR_HYP(14)
267 __ERET
Marc Zyngier2a552d52012-10-06 17:03:17 +01002681: msr cpsr_c, \reg
Dave Martin80c59da2012-02-09 08:47:17 -08002692:
Dave Martin1ecec692012-12-10 18:35:22 +0100270#else
271/*
272 * workaround for possibly broken pre-v6 hardware
273 * (akita, Sharp Zaurus C-1000, PXA270-based)
274 */
275 setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, \reg
276#endif
Dave Martin80c59da2012-02-09 08:47:17 -0800277.endm
278
279/*
Catalin Marinas8b592782009-07-24 12:32:57 +0100280 * STRT/LDRT access macros with ARM and Thumb-2 variants
281 */
282#ifdef CONFIG_THUMB2_KERNEL
283
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100284 .macro usraccoff, instr, reg, ptr, inc, off, cond, abort, t=TUSER()
Catalin Marinas8b592782009-07-24 12:32:57 +01002859999:
286 .if \inc == 1
Catalin Marinas247055a2010-09-13 16:03:21 +0100287 \instr\cond\()b\()\t\().w \reg, [\ptr, #\off]
Catalin Marinas8b592782009-07-24 12:32:57 +0100288 .elseif \inc == 4
Catalin Marinas247055a2010-09-13 16:03:21 +0100289 \instr\cond\()\t\().w \reg, [\ptr, #\off]
Catalin Marinas8b592782009-07-24 12:32:57 +0100290 .else
291 .error "Unsupported inc macro argument"
292 .endif
293
Russell King42604152010-04-19 10:15:03 +0100294 .pushsection __ex_table,"a"
Catalin Marinas8b592782009-07-24 12:32:57 +0100295 .align 3
296 .long 9999b, \abort
Russell King42604152010-04-19 10:15:03 +0100297 .popsection
Catalin Marinas8b592782009-07-24 12:32:57 +0100298 .endm
299
300 .macro usracc, instr, reg, ptr, inc, cond, rept, abort
301 @ explicit IT instruction needed because of the label
302 @ introduced by the USER macro
303 .ifnc \cond,al
304 .if \rept == 1
305 itt \cond
306 .elseif \rept == 2
307 ittt \cond
308 .else
309 .error "Unsupported rept macro argument"
310 .endif
311 .endif
312
313 @ Slightly optimised to avoid incrementing the pointer twice
314 usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
315 .if \rept == 2
Will Deacon1142b712010-11-19 13:18:31 +0100316 usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
Catalin Marinas8b592782009-07-24 12:32:57 +0100317 .endif
318
319 add\cond \ptr, #\rept * \inc
320 .endm
321
322#else /* !CONFIG_THUMB2_KERNEL */
323
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100324 .macro usracc, instr, reg, ptr, inc, cond, rept, abort, t=TUSER()
Catalin Marinas8b592782009-07-24 12:32:57 +0100325 .rept \rept
3269999:
327 .if \inc == 1
Catalin Marinas247055a2010-09-13 16:03:21 +0100328 \instr\cond\()b\()\t \reg, [\ptr], #\inc
Catalin Marinas8b592782009-07-24 12:32:57 +0100329 .elseif \inc == 4
Catalin Marinas247055a2010-09-13 16:03:21 +0100330 \instr\cond\()\t \reg, [\ptr], #\inc
Catalin Marinas8b592782009-07-24 12:32:57 +0100331 .else
332 .error "Unsupported inc macro argument"
333 .endif
334
Russell King42604152010-04-19 10:15:03 +0100335 .pushsection __ex_table,"a"
Catalin Marinas8b592782009-07-24 12:32:57 +0100336 .align 3
337 .long 9999b, \abort
Russell King42604152010-04-19 10:15:03 +0100338 .popsection
Catalin Marinas8b592782009-07-24 12:32:57 +0100339 .endr
340 .endm
341
342#endif /* CONFIG_THUMB2_KERNEL */
343
344 .macro strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
345 usracc str, \reg, \ptr, \inc, \cond, \rept, \abort
346 .endm
347
348 .macro ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
349 usracc ldr, \reg, \ptr, \inc, \cond, \rept, \abort
350 .endm
Dave Martin8f519652011-06-23 17:10:05 +0100351
352/* Utility macro for declaring string literals */
353 .macro string name:req, string
354 .type \name , #object
355\name:
356 .asciz "\string"
357 .size \name , . - \name
358 .endm
359
Russell King84046632012-09-07 18:22:28 +0100360 .macro check_uaccess, addr:req, size:req, limit:req, tmp:req, bad:req
361#ifndef CONFIG_CPU_USE_DOMAINS
362 adds \tmp, \addr, #\size - 1
363 sbcccs \tmp, \tmp, \limit
364 bcs \bad
365#endif
366 .endm
367
Magnus Damm2bc58a62011-06-13 06:46:44 +0100368#endif /* __ASM_ASSEMBLER_H__ */