blob: f0963bb79935f6930af67ddce5b3d620243bfcad [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__
Victor Kamensky8032ebe2014-02-25 08:41:09 +010033#define lspull lsr
34#define lspush lsl
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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
Victor Kamensky8032ebe2014-02-25 08:41:09 +010044#define lspull lsl
45#define lspush lsr
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#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
Ben Dooks87d7e2f2013-02-12 18:59:57 +000056/* Select code for any configuration running in BE8 mode */
57#ifdef CONFIG_CPU_ENDIAN_BE8
58#define ARM_BE8(code...) code
59#else
60#define ARM_BE8(code...)
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Data preload for architectures that support it
65 */
66#if __LINUX_ARM_ARCH__ >= 5
67#define PLD(code...) code
68#else
69#define PLD(code...)
70#endif
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Nicolas Pitre2239aff2008-03-31 12:38:31 -040073 * This can be used to enable code to cacheline align the destination
74 * pointer when bulk writing to memory. Experiments on StrongARM and
75 * XScale didn't show this a worthwhile thing to do when the cache is not
76 * set to write-allocate (this would need further testing on XScale when WA
77 * is used).
78 *
79 * On Feroceon there is much to gain however, regardless of cache mode.
80 */
81#ifdef CONFIG_CPU_FEROCEON
82#define CALGN(code...) code
83#else
84#define CALGN(code...)
85#endif
86
87/*
Russell King9c429542006-03-23 16:59:37 +000088 * Enable and disable interrupts
89 */
90#if __LINUX_ARM_ARCH__ >= 6
Uwe Kleine-König0d928b02009-08-13 20:38:17 +020091 .macro disable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +000092 cpsid i
93 .endm
94
Uwe Kleine-König0d928b02009-08-13 20:38:17 +020095 .macro enable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +000096 cpsie i
97 .endm
98#else
Uwe Kleine-König0d928b02009-08-13 20:38:17 +020099 .macro disable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +0000100 msr cpsr_c, #PSR_I_BIT | SVC_MODE
101 .endm
102
Uwe Kleine-König0d928b02009-08-13 20:38:17 +0200103 .macro enable_irq_notrace
Russell King9c429542006-03-23 16:59:37 +0000104 msr cpsr_c, #SVC_MODE
105 .endm
106#endif
107
Uwe Kleine-König0d928b02009-08-13 20:38:17 +0200108 .macro asm_trace_hardirqs_off
109#if defined(CONFIG_TRACE_IRQFLAGS)
110 stmdb sp!, {r0-r3, ip, lr}
111 bl trace_hardirqs_off
112 ldmia sp!, {r0-r3, ip, lr}
113#endif
114 .endm
115
116 .macro asm_trace_hardirqs_on_cond, cond
117#if defined(CONFIG_TRACE_IRQFLAGS)
118 /*
119 * actually the registers should be pushed and pop'd conditionally, but
120 * after bl the flags are certainly clobbered
121 */
122 stmdb sp!, {r0-r3, ip, lr}
123 bl\cond trace_hardirqs_on
124 ldmia sp!, {r0-r3, ip, lr}
125#endif
126 .endm
127
128 .macro asm_trace_hardirqs_on
129 asm_trace_hardirqs_on_cond al
130 .endm
131
132 .macro disable_irq
133 disable_irq_notrace
134 asm_trace_hardirqs_off
135 .endm
136
137 .macro enable_irq
138 asm_trace_hardirqs_on
139 enable_irq_notrace
140 .endm
Russell King9c429542006-03-23 16:59:37 +0000141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * Save the current IRQ state and disable IRQs. Note that this macro
143 * assumes FIQs are enabled, and that the processor is in SVC mode.
144 */
Russell King59d1ff32005-11-09 15:04:22 +0000145 .macro save_and_disable_irqs, oldcpsr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 mrs \oldcpsr, cpsr
Russell King9c429542006-03-23 16:59:37 +0000147 disable_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .endm
149
Rabin Vincent8e43a902012-02-15 16:01:42 +0100150 .macro save_and_disable_irqs_notrace, oldcpsr
151 mrs \oldcpsr, cpsr
152 disable_irq_notrace
153 .endm
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * Restore interrupt state previously stored in a register. We don't
157 * guarantee that this will preserve the flags.
158 */
Uwe Kleine-König0d928b02009-08-13 20:38:17 +0200159 .macro restore_irqs_notrace, oldcpsr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 msr cpsr_c, \oldcpsr
161 .endm
162
Uwe Kleine-König0d928b02009-08-13 20:38:17 +0200163 .macro restore_irqs, oldcpsr
164 tst \oldcpsr, #PSR_I_BIT
165 asm_trace_hardirqs_on_cond eq
166 restore_irqs_notrace \oldcpsr
167 .endm
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#define USER(x...) \
1709999: x; \
Russell King42604152010-04-19 10:15:03 +0100171 .pushsection __ex_table,"a"; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .align 3; \
173 .long 9999b,9001f; \
Russell King42604152010-04-19 10:15:03 +0100174 .popsection
Russell Kingbac4e962009-05-25 20:58:00 +0100175
Russell Kingf00ec482010-09-04 10:47:48 +0100176#ifdef CONFIG_SMP
177#define ALT_SMP(instr...) \
1789998: instr
Dave Martined3768a2010-12-01 15:39:23 +0100179/*
180 * Note: if you get assembler errors from ALT_UP() when building with
181 * CONFIG_THUMB2_KERNEL, you almost certainly need to use
182 * ALT_SMP( W(instr) ... )
183 */
Russell Kingf00ec482010-09-04 10:47:48 +0100184#define ALT_UP(instr...) \
185 .pushsection ".alt.smp.init", "a" ;\
186 .long 9998b ;\
Dave Martined3768a2010-12-01 15:39:23 +01001879997: instr ;\
188 .if . - 9997b != 4 ;\
189 .error "ALT_UP() content must assemble to exactly 4 bytes";\
190 .endif ;\
Russell Kingf00ec482010-09-04 10:47:48 +0100191 .popsection
192#define ALT_UP_B(label) \
193 .equ up_b_offset, label - 9998b ;\
194 .pushsection ".alt.smp.init", "a" ;\
195 .long 9998b ;\
Dave Martined3768a2010-12-01 15:39:23 +0100196 W(b) . + up_b_offset ;\
Russell Kingf00ec482010-09-04 10:47:48 +0100197 .popsection
198#else
199#define ALT_SMP(instr...)
200#define ALT_UP(instr...) instr
201#define ALT_UP_B(label) b label
202#endif
203
Russell Kingbac4e962009-05-25 20:58:00 +0100204/*
Will Deacond675d0b2011-11-22 17:30:28 +0000205 * Instruction barrier
206 */
207 .macro instr_sync
208#if __LINUX_ARM_ARCH__ >= 7
209 isb
210#elif __LINUX_ARM_ARCH__ == 6
211 mcr p15, 0, r0, c7, c5, 4
212#endif
213 .endm
214
215/*
Russell Kingbac4e962009-05-25 20:58:00 +0100216 * SMP data memory barrier
217 */
Dave Martined3768a2010-12-01 15:39:23 +0100218 .macro smp_dmb mode
Russell Kingbac4e962009-05-25 20:58:00 +0100219#ifdef CONFIG_SMP
220#if __LINUX_ARM_ARCH__ >= 7
Dave Martined3768a2010-12-01 15:39:23 +0100221 .ifeqs "\mode","arm"
Will Deacon0db53062013-05-10 18:07:19 +0100222 ALT_SMP(dmb ish)
Dave Martined3768a2010-12-01 15:39:23 +0100223 .else
Will Deacon0db53062013-05-10 18:07:19 +0100224 ALT_SMP(W(dmb) ish)
Dave Martined3768a2010-12-01 15:39:23 +0100225 .endif
Russell Kingbac4e962009-05-25 20:58:00 +0100226#elif __LINUX_ARM_ARCH__ == 6
Russell Kingf00ec482010-09-04 10:47:48 +0100227 ALT_SMP(mcr p15, 0, r0, c7, c10, 5) @ dmb
228#else
229#error Incompatible SMP platform
Russell Kingbac4e962009-05-25 20:58:00 +0100230#endif
Dave Martined3768a2010-12-01 15:39:23 +0100231 .ifeqs "\mode","arm"
Russell Kingf00ec482010-09-04 10:47:48 +0100232 ALT_UP(nop)
Dave Martined3768a2010-12-01 15:39:23 +0100233 .else
234 ALT_UP(W(nop))
235 .endif
Russell Kingbac4e962009-05-25 20:58:00 +0100236#endif
237 .endm
Catalin Marinasb86040a2009-07-24 12:32:54 +0100238
239#ifdef CONFIG_THUMB2_KERNEL
240 .macro setmode, mode, reg
241 mov \reg, #\mode
242 msr cpsr_c, \reg
243 .endm
244#else
245 .macro setmode, mode, reg
246 msr cpsr_c, #\mode
247 .endm
248#endif
Catalin Marinas8b592782009-07-24 12:32:57 +0100249
250/*
Dave Martin80c59da2012-02-09 08:47:17 -0800251 * Helper macro to enter SVC mode cleanly and mask interrupts. reg is
252 * a scratch register for the macro to overwrite.
253 *
254 * This macro is intended for forcing the CPU into SVC mode at boot time.
255 * you cannot return to the original mode.
Dave Martin80c59da2012-02-09 08:47:17 -0800256 */
257.macro safe_svcmode_maskall reg:req
Dave Martin1ecec692012-12-10 18:35:22 +0100258#if __LINUX_ARM_ARCH__ >= 6
Dave Martin80c59da2012-02-09 08:47:17 -0800259 mrs \reg , cpsr
Russell King8e9c24a2012-12-03 15:39:43 +0000260 eor \reg, \reg, #HYP_MODE
261 tst \reg, #MODE_MASK
Dave Martin80c59da2012-02-09 08:47:17 -0800262 bic \reg , \reg , #MODE_MASK
Russell King8e9c24a2012-12-03 15:39:43 +0000263 orr \reg , \reg , #PSR_I_BIT | PSR_F_BIT | SVC_MODE
Dave Martin80c59da2012-02-09 08:47:17 -0800264THUMB( orr \reg , \reg , #PSR_T_BIT )
Dave Martin80c59da2012-02-09 08:47:17 -0800265 bne 1f
Marc Zyngier2a552d52012-10-06 17:03:17 +0100266 orr \reg, \reg, #PSR_A_BIT
267 adr lr, BSYM(2f)
268 msr spsr_cxsf, \reg
Dave Martin80c59da2012-02-09 08:47:17 -0800269 __MSR_ELR_HYP(14)
270 __ERET
Marc Zyngier2a552d52012-10-06 17:03:17 +01002711: msr cpsr_c, \reg
Dave Martin80c59da2012-02-09 08:47:17 -08002722:
Dave Martin1ecec692012-12-10 18:35:22 +0100273#else
274/*
275 * workaround for possibly broken pre-v6 hardware
276 * (akita, Sharp Zaurus C-1000, PXA270-based)
277 */
278 setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, \reg
279#endif
Dave Martin80c59da2012-02-09 08:47:17 -0800280.endm
281
282/*
Catalin Marinas8b592782009-07-24 12:32:57 +0100283 * STRT/LDRT access macros with ARM and Thumb-2 variants
284 */
285#ifdef CONFIG_THUMB2_KERNEL
286
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100287 .macro usraccoff, instr, reg, ptr, inc, off, cond, abort, t=TUSER()
Catalin Marinas8b592782009-07-24 12:32:57 +01002889999:
289 .if \inc == 1
Catalin Marinas247055a2010-09-13 16:03:21 +0100290 \instr\cond\()b\()\t\().w \reg, [\ptr, #\off]
Catalin Marinas8b592782009-07-24 12:32:57 +0100291 .elseif \inc == 4
Catalin Marinas247055a2010-09-13 16:03:21 +0100292 \instr\cond\()\t\().w \reg, [\ptr, #\off]
Catalin Marinas8b592782009-07-24 12:32:57 +0100293 .else
294 .error "Unsupported inc macro argument"
295 .endif
296
Russell King42604152010-04-19 10:15:03 +0100297 .pushsection __ex_table,"a"
Catalin Marinas8b592782009-07-24 12:32:57 +0100298 .align 3
299 .long 9999b, \abort
Russell King42604152010-04-19 10:15:03 +0100300 .popsection
Catalin Marinas8b592782009-07-24 12:32:57 +0100301 .endm
302
303 .macro usracc, instr, reg, ptr, inc, cond, rept, abort
304 @ explicit IT instruction needed because of the label
305 @ introduced by the USER macro
306 .ifnc \cond,al
307 .if \rept == 1
308 itt \cond
309 .elseif \rept == 2
310 ittt \cond
311 .else
312 .error "Unsupported rept macro argument"
313 .endif
314 .endif
315
316 @ Slightly optimised to avoid incrementing the pointer twice
317 usraccoff \instr, \reg, \ptr, \inc, 0, \cond, \abort
318 .if \rept == 2
Will Deacon1142b712010-11-19 13:18:31 +0100319 usraccoff \instr, \reg, \ptr, \inc, \inc, \cond, \abort
Catalin Marinas8b592782009-07-24 12:32:57 +0100320 .endif
321
322 add\cond \ptr, #\rept * \inc
323 .endm
324
325#else /* !CONFIG_THUMB2_KERNEL */
326
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100327 .macro usracc, instr, reg, ptr, inc, cond, rept, abort, t=TUSER()
Catalin Marinas8b592782009-07-24 12:32:57 +0100328 .rept \rept
3299999:
330 .if \inc == 1
Catalin Marinas247055a2010-09-13 16:03:21 +0100331 \instr\cond\()b\()\t \reg, [\ptr], #\inc
Catalin Marinas8b592782009-07-24 12:32:57 +0100332 .elseif \inc == 4
Catalin Marinas247055a2010-09-13 16:03:21 +0100333 \instr\cond\()\t \reg, [\ptr], #\inc
Catalin Marinas8b592782009-07-24 12:32:57 +0100334 .else
335 .error "Unsupported inc macro argument"
336 .endif
337
Russell King42604152010-04-19 10:15:03 +0100338 .pushsection __ex_table,"a"
Catalin Marinas8b592782009-07-24 12:32:57 +0100339 .align 3
340 .long 9999b, \abort
Russell King42604152010-04-19 10:15:03 +0100341 .popsection
Catalin Marinas8b592782009-07-24 12:32:57 +0100342 .endr
343 .endm
344
345#endif /* CONFIG_THUMB2_KERNEL */
346
347 .macro strusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
348 usracc str, \reg, \ptr, \inc, \cond, \rept, \abort
349 .endm
350
351 .macro ldrusr, reg, ptr, inc, cond=al, rept=1, abort=9001f
352 usracc ldr, \reg, \ptr, \inc, \cond, \rept, \abort
353 .endm
Dave Martin8f519652011-06-23 17:10:05 +0100354
355/* Utility macro for declaring string literals */
356 .macro string name:req, string
357 .type \name , #object
358\name:
359 .asciz "\string"
360 .size \name , . - \name
361 .endm
362
Russell King84046632012-09-07 18:22:28 +0100363 .macro check_uaccess, addr:req, size:req, limit:req, tmp:req, bad:req
364#ifndef CONFIG_CPU_USE_DOMAINS
365 adds \tmp, \addr, #\size - 1
366 sbcccs \tmp, \tmp, \limit
367 bcs \bad
368#endif
369 .endm
370
Magnus Damm2bc58a62011-06-13 06:46:44 +0100371#endif /* __ASM_ASSEMBLER_H__ */