1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Cadence Design Systems Inc.
5 #include <linux/jump_label.h>
6 #include <linux/kernel.h>
7 #include <linux/memory.h>
8 #include <linux/stop_machine.h>
9 #include <linux/types.h>
11 #include <asm/cacheflush.h>
13 #ifdef HAVE_JUMP_LABEL
15 #define J_OFFSET_MASK 0x0003ffff
16 #define J_SIGN_MASK (~(J_OFFSET_MASK >> 1))
18 #if defined(__XTENSA_EL__)
20 #define NOP_INSN 0x0020f0
21 #elif defined(__XTENSA_EB__)
22 #define J_INSN 0x60000000
23 #define NOP_INSN 0x0f020000
25 #error Unsupported endianness.
35 static void local_patch_text(unsigned long addr, const void *data, size_t sz)
37 memcpy((void *)addr, data, sz);
38 local_flush_icache_range(addr, addr + sz);
41 static int patch_text_stop_machine(void *data)
43 struct patch *patch = data;
45 if (atomic_inc_return(&patch->cpu_count) == 1) {
46 local_patch_text(patch->addr, patch->data, patch->sz);
47 atomic_inc(&patch->cpu_count);
49 while (atomic_read(&patch->cpu_count) <= num_online_cpus())
51 __invalidate_icache_range(patch->addr, patch->sz);
56 static void patch_text(unsigned long addr, const void *data, size_t sz)
58 if (IS_ENABLED(CONFIG_SMP)) {
59 struct patch patch = {
60 .cpu_count = ATOMIC_INIT(0),
65 stop_machine_cpuslocked(patch_text_stop_machine,
70 local_irq_save(flags);
71 local_patch_text(addr, data, sz);
72 local_irq_restore(flags);
76 void arch_jump_label_transform(struct jump_entry *e,
77 enum jump_label_type type)
79 u32 d = (jump_entry_target(e) - (jump_entry_code(e) + 4));
82 /* Jump only works within 128K of the J instruction. */
83 BUG_ON(!((d & J_SIGN_MASK) == 0 ||
84 (d & J_SIGN_MASK) == J_SIGN_MASK));
86 if (type == JUMP_LABEL_JMP) {
87 #if defined(__XTENSA_EL__)
88 insn = ((d & J_OFFSET_MASK) << 6) | J_INSN;
89 #elif defined(__XTENSA_EB__)
90 insn = ((d & J_OFFSET_MASK) << 8) | J_INSN;
96 patch_text(jump_entry_code(e), &insn, JUMP_LABEL_NOP_SIZE);
99 #endif /* HAVE_JUMP_LABEL */