1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <asm/opcodes.h>
7 __arm_gen_branch_thumb2(unsigned long pc
, unsigned long addr
, bool link
)
9 unsigned long s
, j1
, j2
, i1
, i2
, imm10
, imm11
;
10 unsigned long first
, second
;
13 offset
= (long)addr
- (long)(pc
+ 4);
14 if (offset
< -16777216 || offset
> 16777214) {
19 s
= (offset
>> 24) & 0x1;
20 i1
= (offset
>> 23) & 0x1;
21 i2
= (offset
>> 22) & 0x1;
22 imm10
= (offset
>> 12) & 0x3ff;
23 imm11
= (offset
>> 1) & 0x7ff;
28 first
= 0xf000 | (s
<< 10) | imm10
;
29 second
= 0x9000 | (j1
<< 13) | (j2
<< 11) | imm11
;
33 return __opcode_thumb32_compose(first
, second
);
37 __arm_gen_branch_arm(unsigned long pc
, unsigned long addr
, bool link
)
39 unsigned long opcode
= 0xea000000;
45 offset
= (long)addr
- (long)(pc
+ 8);
46 if (unlikely(offset
< -33554432 || offset
> 33554428)) {
51 offset
= (offset
>> 2) & 0x00ffffff;
53 return opcode
| offset
;
57 __arm_gen_branch(unsigned long pc
, unsigned long addr
, bool link
)
59 if (IS_ENABLED(CONFIG_THUMB2_KERNEL
))
60 return __arm_gen_branch_thumb2(pc
, addr
, link
);
62 return __arm_gen_branch_arm(pc
, addr
, link
);