2 * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
4 * Copyright (C) 1996-2000 Russell King
5 * Copyright (C) 2012 ARM Ltd.
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.
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.
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/>.
20 #error "Only include this from assembly code"
23 #ifndef __ASM_ASSEMBLER_H
24 #define __ASM_ASSEMBLER_H
26 #include <asm/asm-offsets.h>
27 #include <asm/cpufeature.h>
28 #include <asm/debug-monitors.h>
29 #include <asm/mmu_context.h>
31 #include <asm/pgtable-hwdef.h>
32 #include <asm/ptrace.h>
33 #include <asm/thread_info.h>
35 .macro save_and_disable_daif, flags
48 .macro restore_daif, flags:req
52 /* Only on aarch64 pstate, PSR_D_BIT is different for aarch32 */
53 .macro inherit_daif, pstate:req, tmp:req
54 and \tmp, \pstate, #(PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
58 /* IRQ is the lowest priority flag, unconditionally unmask the rest. */
60 msr daifclr, #(8 | 4 | 1)
64 * Enable and disable interrupts.
74 .macro save_and_disable_irq, flags
79 .macro restore_irq, flags
87 .macro disable_step_tsk, flgs, tmp
88 tbz \flgs, #TIF_SINGLESTEP, 9990f
90 bic \tmp, \tmp, #DBG_MDSCR_SS
92 isb // Synchronise with enable_dbg
96 /* call with daif masked */
97 .macro enable_step_tsk, flgs, tmp
98 tbz \flgs, #TIF_SINGLESTEP, 9990f
100 orr \tmp, \tmp, #DBG_MDSCR_SS
106 * SMP data memory barrier
122 * Emit an entry into the exception table
124 .macro _asm_extable, from, to
125 .pushsection __ex_table, "a"
127 .long (\from - .), (\to - .)
131 #define USER(l, x...) \
133 _asm_extable 9999b, l
138 lr .req x30 // link register
149 * Select code when configured for BE.
151 #ifdef CONFIG_CPU_BIG_ENDIAN
152 #define CPU_BE(code...) code
154 #define CPU_BE(code...)
158 * Select code when configured for LE.
160 #ifdef CONFIG_CPU_BIG_ENDIAN
161 #define CPU_LE(code...)
163 #define CPU_LE(code...) code
167 * Define a macro that constructs a 64-bit value by concatenating two
168 * 32-bit registers. Note that on big endian systems the order of the
169 * registers is swapped.
171 #ifndef CONFIG_CPU_BIG_ENDIAN
172 .macro regs_to_64, rd, lbits, hbits
174 .macro regs_to_64, rd, hbits, lbits
176 orr \rd, \lbits, \hbits, lsl #32
180 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
181 * <symbol> is within the range +/- 4 GB of the PC when running
182 * in core kernel context. In module context, a movz/movk sequence
183 * is used, since modules may be loaded far away from the kernel
184 * when KASLR is in effect.
187 * @dst: destination register (64 bit wide)
188 * @sym: name of the symbol
190 .macro adr_l, dst, sym
193 add \dst, \dst, :lo12:\sym
195 movz \dst, #:abs_g3:\sym
196 movk \dst, #:abs_g2_nc:\sym
197 movk \dst, #:abs_g1_nc:\sym
198 movk \dst, #:abs_g0_nc:\sym
203 * @dst: destination register (32 or 64 bit wide)
204 * @sym: name of the symbol
205 * @tmp: optional 64-bit scratch register to be used if <dst> is a
206 * 32-bit wide register, in which case it cannot be used to hold
209 .macro ldr_l, dst, sym, tmp=
213 ldr \dst, [\dst, :lo12:\sym]
216 ldr \dst, [\tmp, :lo12:\sym]
230 * @src: source register (32 or 64 bit wide)
231 * @sym: name of the symbol
232 * @tmp: mandatory 64-bit scratch register to calculate the address
233 * while <src> needs to be preserved.
235 .macro str_l, src, sym, tmp
238 str \src, [\tmp, :lo12:\sym]
246 * @dst: Result of per_cpu(sym, smp_processor_id()), can be SP for
248 * @sym: The name of the per-cpu variable
249 * @tmp: scratch register
251 .macro adr_this_cpu, dst, sym, tmp
254 add \dst, \tmp, #:lo12:\sym
263 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
264 * @sym: The name of the per-cpu variable
265 * @tmp: scratch register
267 .macro ldr_this_cpu dst, sym, tmp
270 ldr \dst, [\dst, \tmp]
274 * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm)
276 .macro vma_vm_mm, rd, rn
277 ldr \rd, [\rn, #VMA_VM_MM]
281 * mmid - get context id from mm pointer (mm->context.id)
284 ldr \rd, [\rn, #MM_CONTEXT_ID]
287 * read_ctr - read CTR_EL0. If the system has mismatched
288 * cache line sizes, provide the system wide safe value
289 * from arm64_ftr_reg_ctrel0.sys_val
292 alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
293 mrs \reg, ctr_el0 // read CTR
296 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
302 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
303 * from the CTR register.
305 .macro raw_dcache_line_size, reg, tmp
306 mrs \tmp, ctr_el0 // read CTR
307 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
308 mov \reg, #4 // bytes per word
309 lsl \reg, \reg, \tmp // actual cache line size
313 * dcache_line_size - get the safe D-cache line size across all CPUs
315 .macro dcache_line_size, reg, tmp
317 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
318 mov \reg, #4 // bytes per word
319 lsl \reg, \reg, \tmp // actual cache line size
323 * raw_icache_line_size - get the minimum I-cache line size on this CPU
324 * from the CTR register.
326 .macro raw_icache_line_size, reg, tmp
327 mrs \tmp, ctr_el0 // read CTR
328 and \tmp, \tmp, #0xf // cache line size encoding
329 mov \reg, #4 // bytes per word
330 lsl \reg, \reg, \tmp // actual cache line size
334 * icache_line_size - get the safe I-cache line size across all CPUs
336 .macro icache_line_size, reg, tmp
338 and \tmp, \tmp, #0xf // cache line size encoding
339 mov \reg, #4 // bytes per word
340 lsl \reg, \reg, \tmp // actual cache line size
344 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
346 .macro tcr_set_idmap_t0sz, valreg, tmpreg
347 #ifndef CONFIG_ARM64_VA_BITS_48
348 ldr_l \tmpreg, idmap_t0sz
349 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
354 * Macro to perform a data cache maintenance for the interval
355 * [kaddr, kaddr + size)
357 * op: operation passed to dc instruction
358 * domain: domain used in dsb instruciton
359 * kaddr: starting virtual address of the region
360 * size: size of the region
361 * Corrupts: kaddr, size, tmp1, tmp2
363 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
364 dcache_line_size \tmp1, \tmp2
365 add \size, \kaddr, \size
367 bic \kaddr, \kaddr, \tmp2
369 .if (\op == cvau || \op == cvac)
370 alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
375 .elseif (\op == cvap)
376 alternative_if ARM64_HAS_DCPOP
377 sys 3, c7, c12, 1, \kaddr // dc cvap
384 add \kaddr, \kaddr, \tmp1
391 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
393 .macro reset_pmuserenr_el0, tmpreg
394 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
395 sbfx \tmpreg, \tmpreg, #8, #4
396 cmp \tmpreg, #1 // Skip if no PMU present
398 msr pmuserenr_el0, xzr // Disable PMU access from EL0
403 * copy_page - copy src to dest using temp registers t1-t8
405 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
406 9998: ldp \t1, \t2, [\src]
407 ldp \t3, \t4, [\src, #16]
408 ldp \t5, \t6, [\src, #32]
409 ldp \t7, \t8, [\src, #48]
411 stnp \t1, \t2, [\dest]
412 stnp \t3, \t4, [\dest, #16]
413 stnp \t5, \t6, [\dest, #32]
414 stnp \t7, \t8, [\dest, #48]
415 add \dest, \dest, #64
416 tst \src, #(PAGE_SIZE - 1)
421 * Annotate a function as position independent, i.e., safe to be called before
422 * the kernel virtual mapping is activated.
424 #define ENDPIPROC(x) \
426 .type __pi_##x, %function; \
428 .size __pi_##x, . - x; \
432 * Annotate a function as being unsuitable for kprobes.
434 #ifdef CONFIG_KPROBES
435 #define NOKPROBE(x) \
436 .pushsection "_kprobe_blacklist", "aw"; \
443 * Emit a 64-bit absolute little endian symbol reference in a way that
444 * ensures that it will be resolved at build time, even when building a
445 * PIE binary. This requires cooperation from the linker script, which
446 * must emit the lo32/hi32 halves individually.
454 * mov_q - move an immediate constant into a 64-bit register using
455 * between 2 and 4 movz/movk instructions (depending on the
456 * magnitude and sign of the operand)
458 .macro mov_q, reg, val
459 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
460 movz \reg, :abs_g1_s:\val
462 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
463 movz \reg, :abs_g2_s:\val
465 movz \reg, :abs_g3:\val
466 movk \reg, :abs_g2_nc:\val
468 movk \reg, :abs_g1_nc:\val
470 movk \reg, :abs_g0_nc:\val
474 * Return the current thread_info.
476 .macro get_thread_info, rd
481 * Errata workaround prior to TTBR0_EL1 update
483 * val: TTBR value with new BADDR, preserved
484 * tmp0: temporary register, clobbered
485 * tmp1: other temporary register, clobbered
487 .macro pre_ttbr0_update_workaround, val, tmp0, tmp1
488 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
489 alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
491 mov \tmp1, #FALKOR_RESERVED_ASID
492 bfi \tmp0, \tmp1, #48, #16 // reserved ASID + old BADDR
495 bfi \tmp0, \val, #0, #48 // reserved ASID + new BADDR
498 alternative_else_nop_endif
503 * Errata workaround post TTBR0_EL1 update.
505 .macro post_ttbr0_update_workaround
506 #ifdef CONFIG_CAVIUM_ERRATUM_27456
507 alternative_if ARM64_WORKAROUND_CAVIUM_27456
511 alternative_else_nop_endif
516 * Errata workaround prior to disable MMU. Insert an ISB immediately prior
517 * to executing the MSR that will change SCTLR_ELn[M] from a value of 1 to 0.
519 .macro pre_disable_mmu_workaround
520 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_E1041
525 #endif /* __ASM_ASSEMBLER_H */