Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / include / asm / assembler.h
1 /*
2  * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
3  *
4  * Copyright (C) 1996-2000 Russell King
5  * Copyright (C) 2012 ARM Ltd.
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  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef __ASSEMBLY__
20 #error "Only include this from assembly code"
21 #endif
22
23 #ifndef __ASM_ASSEMBLER_H
24 #define __ASM_ASSEMBLER_H
25
26 #include <asm/asm-offsets.h>
27 #include <asm/cpufeature.h>
28 #include <asm/page.h>
29 #include <asm/pgtable-hwdef.h>
30 #include <asm/ptrace.h>
31 #include <asm/thread_info.h>
32
33 /*
34  * Stack pushing/popping (register pairs only). Equivalent to store decrement
35  * before, load increment after.
36  */
37         .macro  push, xreg1, xreg2
38         stp     \xreg1, \xreg2, [sp, #-16]!
39         .endm
40
41         .macro  pop, xreg1, xreg2
42         ldp     \xreg1, \xreg2, [sp], #16
43         .endm
44
45 /*
46  * Enable and disable interrupts.
47  */
48         .macro  disable_irq
49         msr     daifset, #2
50         .endm
51
52         .macro  enable_irq
53         msr     daifclr, #2
54         .endm
55
56         .macro  save_and_disable_irq, flags
57         mrs     \flags, daif
58         msr     daifset, #2
59         .endm
60
61         .macro  restore_irq, flags
62         msr     daif, \flags
63         .endm
64
65 /*
66  * Enable and disable debug exceptions.
67  */
68         .macro  disable_dbg
69         msr     daifset, #8
70         .endm
71
72         .macro  enable_dbg
73         msr     daifclr, #8
74         .endm
75
76         .macro  disable_step_tsk, flgs, tmp
77         tbz     \flgs, #TIF_SINGLESTEP, 9990f
78         mrs     \tmp, mdscr_el1
79         bic     \tmp, \tmp, #1
80         msr     mdscr_el1, \tmp
81         isb     // Synchronise with enable_dbg
82 9990:
83         .endm
84
85         .macro  enable_step_tsk, flgs, tmp
86         tbz     \flgs, #TIF_SINGLESTEP, 9990f
87         disable_dbg
88         mrs     \tmp, mdscr_el1
89         orr     \tmp, \tmp, #1
90         msr     mdscr_el1, \tmp
91 9990:
92         .endm
93
94 /*
95  * Enable both debug exceptions and interrupts. This is likely to be
96  * faster than two daifclr operations, since writes to this register
97  * are self-synchronising.
98  */
99         .macro  enable_dbg_and_irq
100         msr     daifclr, #(8 | 2)
101         .endm
102
103 /*
104  * SMP data memory barrier
105  */
106         .macro  smp_dmb, opt
107         dmb     \opt
108         .endm
109
110 /*
111  * NOP sequence
112  */
113         .macro  nops, num
114         .rept   \num
115         nop
116         .endr
117         .endm
118
119 /*
120  * Emit an entry into the exception table
121  */
122         .macro          _asm_extable, from, to
123         .pushsection    __ex_table, "a"
124         .align          3
125         .long           (\from - .), (\to - .)
126         .popsection
127         .endm
128
129 #define USER(l, x...)                           \
130 9999:   x;                                      \
131         _asm_extable    9999b, l
132
133 /*
134  * Register aliases.
135  */
136 lr      .req    x30             // link register
137
138 /*
139  * Vector entry
140  */
141          .macro ventry  label
142         .align  7
143         b       \label
144         .endm
145
146 /*
147  * Select code when configured for BE.
148  */
149 #ifdef CONFIG_CPU_BIG_ENDIAN
150 #define CPU_BE(code...) code
151 #else
152 #define CPU_BE(code...)
153 #endif
154
155 /*
156  * Select code when configured for LE.
157  */
158 #ifdef CONFIG_CPU_BIG_ENDIAN
159 #define CPU_LE(code...)
160 #else
161 #define CPU_LE(code...) code
162 #endif
163
164 /*
165  * Define a macro that constructs a 64-bit value by concatenating two
166  * 32-bit registers. Note that on big endian systems the order of the
167  * registers is swapped.
168  */
169 #ifndef CONFIG_CPU_BIG_ENDIAN
170         .macro  regs_to_64, rd, lbits, hbits
171 #else
172         .macro  regs_to_64, rd, hbits, lbits
173 #endif
174         orr     \rd, \lbits, \hbits, lsl #32
175         .endm
176
177 /*
178  * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
179  * <symbol> is within the range +/- 4 GB of the PC.
180  */
181         /*
182          * @dst: destination register (64 bit wide)
183          * @sym: name of the symbol
184          * @tmp: optional scratch register to be used if <dst> == sp, which
185          *       is not allowed in an adrp instruction
186          */
187         .macro  adr_l, dst, sym, tmp=
188         .ifb    \tmp
189         adrp    \dst, \sym
190         add     \dst, \dst, :lo12:\sym
191         .else
192         adrp    \tmp, \sym
193         add     \dst, \tmp, :lo12:\sym
194         .endif
195         .endm
196
197         /*
198          * @dst: destination register (32 or 64 bit wide)
199          * @sym: name of the symbol
200          * @tmp: optional 64-bit scratch register to be used if <dst> is a
201          *       32-bit wide register, in which case it cannot be used to hold
202          *       the address
203          */
204         .macro  ldr_l, dst, sym, tmp=
205         .ifb    \tmp
206         adrp    \dst, \sym
207         ldr     \dst, [\dst, :lo12:\sym]
208         .else
209         adrp    \tmp, \sym
210         ldr     \dst, [\tmp, :lo12:\sym]
211         .endif
212         .endm
213
214         /*
215          * @src: source register (32 or 64 bit wide)
216          * @sym: name of the symbol
217          * @tmp: mandatory 64-bit scratch register to calculate the address
218          *       while <src> needs to be preserved.
219          */
220         .macro  str_l, src, sym, tmp
221         adrp    \tmp, \sym
222         str     \src, [\tmp, :lo12:\sym]
223         .endm
224
225         /*
226          * @sym: The name of the per-cpu variable
227          * @reg: Result of per_cpu(sym, smp_processor_id())
228          * @tmp: scratch register
229          */
230         .macro this_cpu_ptr, sym, reg, tmp
231         adr_l   \reg, \sym
232         mrs     \tmp, tpidr_el1
233         add     \reg, \reg, \tmp
234         .endm
235
236 /*
237  * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
238  */
239         .macro  vma_vm_mm, rd, rn
240         ldr     \rd, [\rn, #VMA_VM_MM]
241         .endm
242
243 /*
244  * mmid - get context id from mm pointer (mm->context.id)
245  */
246         .macro  mmid, rd, rn
247         ldr     \rd, [\rn, #MM_CONTEXT_ID]
248         .endm
249
250 /*
251  * dcache_line_size - get the minimum D-cache line size from the CTR register.
252  */
253         .macro  dcache_line_size, reg, tmp
254         mrs     \tmp, ctr_el0                   // read CTR
255         ubfm    \tmp, \tmp, #16, #19            // cache line size encoding
256         mov     \reg, #4                        // bytes per word
257         lsl     \reg, \reg, \tmp                // actual cache line size
258         .endm
259
260 /*
261  * icache_line_size - get the minimum I-cache line size from the CTR register.
262  */
263         .macro  icache_line_size, reg, tmp
264         mrs     \tmp, ctr_el0                   // read CTR
265         and     \tmp, \tmp, #0xf                // cache line size encoding
266         mov     \reg, #4                        // bytes per word
267         lsl     \reg, \reg, \tmp                // actual cache line size
268         .endm
269
270 /*
271  * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
272  */
273         .macro  tcr_set_idmap_t0sz, valreg, tmpreg
274 #ifndef CONFIG_ARM64_VA_BITS_48
275         ldr_l   \tmpreg, idmap_t0sz
276         bfi     \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
277 #endif
278         .endm
279
280 /*
281  * Macro to perform a data cache maintenance for the interval
282  * [kaddr, kaddr + size)
283  *
284  *      op:             operation passed to dc instruction
285  *      domain:         domain used in dsb instruciton
286  *      kaddr:          starting virtual address of the region
287  *      size:           size of the region
288  *      Corrupts:       kaddr, size, tmp1, tmp2
289  */
290         .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
291         dcache_line_size \tmp1, \tmp2
292         add     \size, \kaddr, \size
293         sub     \tmp2, \tmp1, #1
294         bic     \kaddr, \kaddr, \tmp2
295 9998:
296         .if     (\op == cvau || \op == cvac)
297 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
298         dc      \op, \kaddr
299 alternative_else
300         dc      civac, \kaddr
301 alternative_endif
302         .else
303         dc      \op, \kaddr
304         .endif
305         add     \kaddr, \kaddr, \tmp1
306         cmp     \kaddr, \size
307         b.lo    9998b
308         dsb     \domain
309         .endm
310
311 /*
312  * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
313  */
314         .macro  reset_pmuserenr_el0, tmpreg
315         mrs     \tmpreg, id_aa64dfr0_el1        // Check ID_AA64DFR0_EL1 PMUVer
316         sbfx    \tmpreg, \tmpreg, #8, #4
317         cmp     \tmpreg, #1                     // Skip if no PMU present
318         b.lt    9000f
319         msr     pmuserenr_el0, xzr              // Disable PMU access from EL0
320 9000:
321         .endm
322
323 /*
324  * copy_page - copy src to dest using temp registers t1-t8
325  */
326         .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
327 9998:   ldp     \t1, \t2, [\src]
328         ldp     \t3, \t4, [\src, #16]
329         ldp     \t5, \t6, [\src, #32]
330         ldp     \t7, \t8, [\src, #48]
331         add     \src, \src, #64
332         stnp    \t1, \t2, [\dest]
333         stnp    \t3, \t4, [\dest, #16]
334         stnp    \t5, \t6, [\dest, #32]
335         stnp    \t7, \t8, [\dest, #48]
336         add     \dest, \dest, #64
337         tst     \src, #(PAGE_SIZE - 1)
338         b.ne    9998b
339         .endm
340
341 /*
342  * Annotate a function as position independent, i.e., safe to be called before
343  * the kernel virtual mapping is activated.
344  */
345 #define ENDPIPROC(x)                    \
346         .globl  __pi_##x;               \
347         .type   __pi_##x, %function;    \
348         .set    __pi_##x, x;            \
349         .size   __pi_##x, . - x;        \
350         ENDPROC(x)
351
352         /*
353          * Emit a 64-bit absolute little endian symbol reference in a way that
354          * ensures that it will be resolved at build time, even when building a
355          * PIE binary. This requires cooperation from the linker script, which
356          * must emit the lo32/hi32 halves individually.
357          */
358         .macro  le64sym, sym
359         .long   \sym\()_lo32
360         .long   \sym\()_hi32
361         .endm
362
363         /*
364          * mov_q - move an immediate constant into a 64-bit register using
365          *         between 2 and 4 movz/movk instructions (depending on the
366          *         magnitude and sign of the operand)
367          */
368         .macro  mov_q, reg, val
369         .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
370         movz    \reg, :abs_g1_s:\val
371         .else
372         .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
373         movz    \reg, :abs_g2_s:\val
374         .else
375         movz    \reg, :abs_g3:\val
376         movk    \reg, :abs_g2_nc:\val
377         .endif
378         movk    \reg, :abs_g1_nc:\val
379         .endif
380         movk    \reg, :abs_g0_nc:\val
381         .endm
382
383 /*
384  * Return the current thread_info.
385  */
386         .macro  get_thread_info, rd
387         mrs     \rd, sp_el0
388         .endm
389
390 /*
391  * Errata workaround post TTBR0_EL1 update.
392  */
393         .macro  post_ttbr0_update_workaround
394 #ifdef CONFIG_CAVIUM_ERRATUM_27456
395 alternative_if ARM64_WORKAROUND_CAVIUM_27456
396         ic      iallu
397         dsb     nsh
398         isb
399 alternative_else_nop_endif
400 #endif
401         .endm
402
403 #endif  /* __ASM_ASSEMBLER_H */