blob: 33b20c075fb3702cc52601b8246cfe38022950e6 [file] [log] [blame]
Catalin Marinas0be73202012-03-05 11:49:26 +00001/*
Geoff Levand7b7293a2016-04-27 17:47:00 +01002 * Based on arch/arm/include/asm/assembler.h, arch/arm/mm/proc-macros.S
Catalin Marinas0be73202012-03-05 11:49:26 +00003 *
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
Marc Zyngierf3e39272015-02-20 13:53:13 +000023#ifndef __ASM_ASSEMBLER_H
24#define __ASM_ASSEMBLER_H
25
Geoff Levand7b7293a2016-04-27 17:47:00 +010026#include <asm/asm-offsets.h>
Andre Przywara823066d2016-06-28 18:07:29 +010027#include <asm/cpufeature.h>
Christopher Covington38fd94b2017-02-08 15:08:37 -050028#include <asm/mmu_context.h>
Geoff Levand5003dbd2016-04-27 17:47:10 +010029#include <asm/page.h>
Geoff Levand7b7293a2016-04-27 17:47:00 +010030#include <asm/pgtable-hwdef.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000031#include <asm/ptrace.h>
Will Deacon2a283072014-04-29 19:04:06 +010032#include <asm/thread_info.h>
Catalin Marinas0be73202012-03-05 11:49:26 +000033
34/*
Catalin Marinas0be73202012-03-05 11:49:26 +000035 * Enable and disable interrupts.
36 */
37 .macro disable_irq
38 msr daifset, #2
39 .endm
40
41 .macro enable_irq
42 msr daifclr, #2
43 .endm
44
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010045 .macro save_and_disable_irq, flags
46 mrs \flags, daif
47 msr daifset, #2
48 .endm
49
50 .macro restore_irq, flags
51 msr daif, \flags
52 .endm
53
Catalin Marinas0be73202012-03-05 11:49:26 +000054/*
Catalin Marinas0be73202012-03-05 11:49:26 +000055 * Enable and disable debug exceptions.
56 */
57 .macro disable_dbg
58 msr daifset, #8
59 .endm
60
61 .macro enable_dbg
62 msr daifclr, #8
63 .endm
64
Will Deacon2a283072014-04-29 19:04:06 +010065 .macro disable_step_tsk, flgs, tmp
66 tbz \flgs, #TIF_SINGLESTEP, 9990f
Catalin Marinas0be73202012-03-05 11:49:26 +000067 mrs \tmp, mdscr_el1
68 bic \tmp, \tmp, #1
69 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +010070 isb // Synchronise with enable_dbg
719990:
Catalin Marinas0be73202012-03-05 11:49:26 +000072 .endm
73
Will Deacon2a283072014-04-29 19:04:06 +010074 .macro enable_step_tsk, flgs, tmp
75 tbz \flgs, #TIF_SINGLESTEP, 9990f
76 disable_dbg
Catalin Marinas0be73202012-03-05 11:49:26 +000077 mrs \tmp, mdscr_el1
78 orr \tmp, \tmp, #1
79 msr mdscr_el1, \tmp
Will Deacon2a283072014-04-29 19:04:06 +0100809990:
Catalin Marinas0be73202012-03-05 11:49:26 +000081 .endm
82
Will Deacon2a283072014-04-29 19:04:06 +010083/*
84 * Enable both debug exceptions and interrupts. This is likely to be
85 * faster than two daifclr operations, since writes to this register
86 * are self-synchronising.
87 */
88 .macro enable_dbg_and_irq
89 msr daifclr, #(8 | 2)
Catalin Marinas0be73202012-03-05 11:49:26 +000090 .endm
91
92/*
93 * SMP data memory barrier
94 */
95 .macro smp_dmb, opt
Catalin Marinas0be73202012-03-05 11:49:26 +000096 dmb \opt
Catalin Marinas0be73202012-03-05 11:49:26 +000097 .endm
98
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010099/*
Will Deaconf99a2502016-09-06 16:40:23 +0100100 * NOP sequence
101 */
102 .macro nops, num
103 .rept \num
104 nop
105 .endr
106 .endm
107
108/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100109 * Emit an entry into the exception table
110 */
111 .macro _asm_extable, from, to
112 .pushsection __ex_table, "a"
113 .align 3
114 .long (\from - .), (\to - .)
115 .popsection
116 .endm
117
Catalin Marinas0be73202012-03-05 11:49:26 +0000118#define USER(l, x...) \
1199999: x; \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100120 _asm_extable 9999b, l
Catalin Marinas0be73202012-03-05 11:49:26 +0000121
122/*
123 * Register aliases.
124 */
125lr .req x30 // link register
Marc Zyngierdc637f12012-10-19 17:37:35 +0100126
127/*
128 * Vector entry
129 */
130 .macro ventry label
131 .align 7
132 b \label
133 .endm
Matthew Leache68beda2013-10-11 14:52:15 +0100134
135/*
136 * Select code when configured for BE.
137 */
138#ifdef CONFIG_CPU_BIG_ENDIAN
139#define CPU_BE(code...) code
140#else
141#define CPU_BE(code...)
142#endif
143
144/*
145 * Select code when configured for LE.
146 */
147#ifdef CONFIG_CPU_BIG_ENDIAN
148#define CPU_LE(code...)
149#else
150#define CPU_LE(code...) code
151#endif
152
Matthew Leach55b89542013-10-11 14:52:13 +0100153/*
154 * Define a macro that constructs a 64-bit value by concatenating two
155 * 32-bit registers. Note that on big endian systems the order of the
156 * registers is swapped.
157 */
158#ifndef CONFIG_CPU_BIG_ENDIAN
159 .macro regs_to_64, rd, lbits, hbits
160#else
161 .macro regs_to_64, rd, hbits, lbits
162#endif
163 orr \rd, \lbits, \hbits, lsl #32
164 .endm
Marc Zyngierf3e39272015-02-20 13:53:13 +0000165
Ard Biesheuvelb784a5d2015-03-04 19:45:38 +0100166/*
167 * Pseudo-ops for PC-relative adr/ldr/str <reg>, <symbol> where
168 * <symbol> is within the range +/- 4 GB of the PC.
169 */
170 /*
171 * @dst: destination register (64 bit wide)
172 * @sym: name of the symbol
173 * @tmp: optional scratch register to be used if <dst> == sp, which
174 * is not allowed in an adrp instruction
175 */
176 .macro adr_l, dst, sym, tmp=
177 .ifb \tmp
178 adrp \dst, \sym
179 add \dst, \dst, :lo12:\sym
180 .else
181 adrp \tmp, \sym
182 add \dst, \tmp, :lo12:\sym
183 .endif
184 .endm
185
186 /*
187 * @dst: destination register (32 or 64 bit wide)
188 * @sym: name of the symbol
189 * @tmp: optional 64-bit scratch register to be used if <dst> is a
190 * 32-bit wide register, in which case it cannot be used to hold
191 * the address
192 */
193 .macro ldr_l, dst, sym, tmp=
194 .ifb \tmp
195 adrp \dst, \sym
196 ldr \dst, [\dst, :lo12:\sym]
197 .else
198 adrp \tmp, \sym
199 ldr \dst, [\tmp, :lo12:\sym]
200 .endif
201 .endm
202
203 /*
204 * @src: source register (32 or 64 bit wide)
205 * @sym: name of the symbol
206 * @tmp: mandatory 64-bit scratch register to calculate the address
207 * while <src> needs to be preserved.
208 */
209 .macro str_l, src, sym, tmp
210 adrp \tmp, \sym
211 str \src, [\tmp, :lo12:\sym]
212 .endm
213
James Morseaa4d5d32015-12-10 10:22:39 +0000214 /*
Mark Rutland1b7e2292016-11-03 20:23:12 +0000215 * @dst: Result of per_cpu(sym, smp_processor_id())
James Morseaa4d5d32015-12-10 10:22:39 +0000216 * @sym: The name of the per-cpu variable
James Morseaa4d5d32015-12-10 10:22:39 +0000217 * @tmp: scratch register
218 */
Mark Rutland1b7e2292016-11-03 20:23:12 +0000219 .macro adr_this_cpu, dst, sym, tmp
220 adr_l \dst, \sym
James Morseaa4d5d32015-12-10 10:22:39 +0000221 mrs \tmp, tpidr_el1
Mark Rutland1b7e2292016-11-03 20:23:12 +0000222 add \dst, \dst, \tmp
223 .endm
224
225 /*
226 * @dst: Result of READ_ONCE(per_cpu(sym, smp_processor_id()))
227 * @sym: The name of the per-cpu variable
228 * @tmp: scratch register
229 */
230 .macro ldr_this_cpu dst, sym, tmp
231 adr_l \dst, \sym
232 mrs \tmp, tpidr_el1
233 ldr \dst, [\dst, \tmp]
James Morseaa4d5d32015-12-10 10:22:39 +0000234 .endm
235
Ard Biesheuvel20791842015-10-08 20:02:03 +0100236/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100237 * 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
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100249/*
250 * read_ctr - read CTR_EL0. If the system has mismatched
251 * cache line sizes, provide the system wide safe value
252 * from arm64_ftr_reg_ctrel0.sys_val
253 */
254 .macro read_ctr, reg
255alternative_if_not ARM64_MISMATCHED_CACHE_LINE_SIZE
256 mrs \reg, ctr_el0 // read CTR
257 nop
258alternative_else
259 ldr_l \reg, arm64_ftr_reg_ctrel0 + ARM64_FTR_SYSVAL
260alternative_endif
261 .endm
262
Geoff Levand7b7293a2016-04-27 17:47:00 +0100263
264/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100265 * raw_dcache_line_size - get the minimum D-cache line size on this CPU
266 * from the CTR register.
Geoff Levand7b7293a2016-04-27 17:47:00 +0100267 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100268 .macro raw_dcache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100269 mrs \tmp, ctr_el0 // read CTR
270 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
271 mov \reg, #4 // bytes per word
272 lsl \reg, \reg, \tmp // actual cache line size
273 .endm
274
275/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100276 * dcache_line_size - get the safe D-cache line size across all CPUs
Geoff Levand7b7293a2016-04-27 17:47:00 +0100277 */
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100278 .macro dcache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100279 read_ctr \tmp
280 ubfm \tmp, \tmp, #16, #19 // cache line size encoding
281 mov \reg, #4 // bytes per word
282 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100283 .endm
284
285/*
286 * raw_icache_line_size - get the minimum I-cache line size on this CPU
287 * from the CTR register.
288 */
289 .macro raw_icache_line_size, reg, tmp
Geoff Levand7b7293a2016-04-27 17:47:00 +0100290 mrs \tmp, ctr_el0 // read CTR
291 and \tmp, \tmp, #0xf // cache line size encoding
292 mov \reg, #4 // bytes per word
293 lsl \reg, \reg, \tmp // actual cache line size
294 .endm
295
296/*
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100297 * icache_line_size - get the safe I-cache line size across all CPUs
298 */
299 .macro icache_line_size, reg, tmp
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100300 read_ctr \tmp
301 and \tmp, \tmp, #0xf // cache line size encoding
302 mov \reg, #4 // bytes per word
303 lsl \reg, \reg, \tmp // actual cache line size
Suzuki K Poulose072f0a62016-09-09 14:07:14 +0100304 .endm
305
306/*
Geoff Levand7b7293a2016-04-27 17:47:00 +0100307 * tcr_set_idmap_t0sz - update TCR.T0SZ so that we can load the ID map
308 */
309 .macro tcr_set_idmap_t0sz, valreg, tmpreg
310#ifndef CONFIG_ARM64_VA_BITS_48
311 ldr_l \tmpreg, idmap_t0sz
312 bfi \valreg, \tmpreg, #TCR_T0SZ_OFFSET, #TCR_TxSZ_WIDTH
313#endif
314 .endm
315
316/*
317 * Macro to perform a data cache maintenance for the interval
318 * [kaddr, kaddr + size)
319 *
320 * op: operation passed to dc instruction
321 * domain: domain used in dsb instruciton
322 * kaddr: starting virtual address of the region
323 * size: size of the region
324 * Corrupts: kaddr, size, tmp1, tmp2
325 */
326 .macro dcache_by_line_op op, domain, kaddr, size, tmp1, tmp2
327 dcache_line_size \tmp1, \tmp2
328 add \size, \kaddr, \size
329 sub \tmp2, \tmp1, #1
330 bic \kaddr, \kaddr, \tmp2
Andre Przywara823066d2016-06-28 18:07:29 +01003319998:
332 .if (\op == cvau || \op == cvac)
333alternative_if_not ARM64_WORKAROUND_CLEAN_CACHE
334 dc \op, \kaddr
335alternative_else
336 dc civac, \kaddr
337alternative_endif
338 .else
339 dc \op, \kaddr
340 .endif
Geoff Levand7b7293a2016-04-27 17:47:00 +0100341 add \kaddr, \kaddr, \tmp1
342 cmp \kaddr, \size
343 b.lo 9998b
344 dsb \domain
345 .endm
346
347/*
348 * reset_pmuserenr_el0 - reset PMUSERENR_EL0 if PMUv3 present
349 */
350 .macro reset_pmuserenr_el0, tmpreg
351 mrs \tmpreg, id_aa64dfr0_el1 // Check ID_AA64DFR0_EL1 PMUVer
352 sbfx \tmpreg, \tmpreg, #8, #4
353 cmp \tmpreg, #1 // Skip if no PMU present
354 b.lt 9000f
355 msr pmuserenr_el0, xzr // Disable PMU access from EL0
3569000:
357 .endm
358
359/*
Geoff Levand5003dbd2016-04-27 17:47:10 +0100360 * copy_page - copy src to dest using temp registers t1-t8
361 */
362 .macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
3639998: ldp \t1, \t2, [\src]
364 ldp \t3, \t4, [\src, #16]
365 ldp \t5, \t6, [\src, #32]
366 ldp \t7, \t8, [\src, #48]
367 add \src, \src, #64
368 stnp \t1, \t2, [\dest]
369 stnp \t3, \t4, [\dest, #16]
370 stnp \t5, \t6, [\dest, #32]
371 stnp \t7, \t8, [\dest, #48]
372 add \dest, \dest, #64
373 tst \src, #(PAGE_SIZE - 1)
374 b.ne 9998b
375 .endm
376
377/*
Ard Biesheuvel20791842015-10-08 20:02:03 +0100378 * Annotate a function as position independent, i.e., safe to be called before
379 * the kernel virtual mapping is activated.
380 */
381#define ENDPIPROC(x) \
382 .globl __pi_##x; \
383 .type __pi_##x, %function; \
384 .set __pi_##x, x; \
385 .size __pi_##x, . - x; \
386 ENDPROC(x)
387
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +0100388 /*
389 * Emit a 64-bit absolute little endian symbol reference in a way that
390 * ensures that it will be resolved at build time, even when building a
391 * PIE binary. This requires cooperation from the linker script, which
392 * must emit the lo32/hi32 halves individually.
393 */
394 .macro le64sym, sym
395 .long \sym\()_lo32
396 .long \sym\()_hi32
397 .endm
398
Ard Biesheuvel30b5ba52016-04-18 17:09:44 +0200399 /*
400 * mov_q - move an immediate constant into a 64-bit register using
401 * between 2 and 4 movz/movk instructions (depending on the
402 * magnitude and sign of the operand)
403 */
404 .macro mov_q, reg, val
405 .if (((\val) >> 31) == 0 || ((\val) >> 31) == 0x1ffffffff)
406 movz \reg, :abs_g1_s:\val
407 .else
408 .if (((\val) >> 47) == 0 || ((\val) >> 47) == 0x1ffff)
409 movz \reg, :abs_g2_s:\val
410 .else
411 movz \reg, :abs_g3:\val
412 movk \reg, :abs_g2_nc:\val
413 .endif
414 movk \reg, :abs_g1_nc:\val
415 .endif
416 movk \reg, :abs_g0_nc:\val
417 .endm
418
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100419/*
Catalin Marinas4b65a5d2016-07-01 16:53:00 +0100420 * Return the current thread_info.
421 */
422 .macro get_thread_info, rd
423 mrs \rd, sp_el0
424 .endm
425
426/*
Christopher Covington38fd94b2017-02-08 15:08:37 -0500427 * Errata workaround prior to TTBR0_EL1 update
428 *
429 * val: TTBR value with new BADDR, preserved
430 * tmp0: temporary register, clobbered
431 * tmp1: other temporary register, clobbered
432 */
433 .macro pre_ttbr0_update_workaround, val, tmp0, tmp1
434#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
435alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
436 mrs \tmp0, ttbr0_el1
437 mov \tmp1, #FALKOR_RESERVED_ASID
438 bfi \tmp0, \tmp1, #48, #16 // reserved ASID + old BADDR
439 msr ttbr0_el1, \tmp0
440 isb
441 bfi \tmp0, \val, #0, #48 // reserved ASID + new BADDR
442 msr ttbr0_el1, \tmp0
443 isb
444alternative_else_nop_endif
445#endif
446 .endm
447
448/*
Catalin Marinasf33bcf02016-07-01 15:48:55 +0100449 * Errata workaround post TTBR0_EL1 update.
450 */
451 .macro post_ttbr0_update_workaround
452#ifdef CONFIG_CAVIUM_ERRATUM_27456
453alternative_if ARM64_WORKAROUND_CAVIUM_27456
454 ic iallu
455 dsb nsh
456 isb
457alternative_else_nop_endif
458#endif
459 .endm
460
Marc Zyngierf3e39272015-02-20 13:53:13 +0000461#endif /* __ASM_ASSEMBLER_H */