OSDN Git Service

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