15c810cedSChristophe Leroy // SPDX-License-Identifier: GPL-2.0
25c810cedSChristophe Leroy #include <linux/memory.h>
35c810cedSChristophe Leroy #include <linux/static_call.h>
45c810cedSChristophe Leroy
55c810cedSChristophe Leroy #include <asm/code-patching.h>
65c810cedSChristophe Leroy
arch_static_call_transform(void * site,void * tramp,void * func,bool tail)75c810cedSChristophe Leroy void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
85c810cedSChristophe Leroy {
95c810cedSChristophe Leroy int err;
105c810cedSChristophe Leroy bool is_ret0 = (func == __static_call_return0);
115c810cedSChristophe Leroy unsigned long target = (unsigned long)(is_ret0 ? tramp + PPC_SCT_RET0 : func);
125c810cedSChristophe Leroy bool is_short = is_offset_in_branch_range((long)target - (long)tramp);
135c810cedSChristophe Leroy
145c810cedSChristophe Leroy if (!tramp)
155c810cedSChristophe Leroy return;
165c810cedSChristophe Leroy
175c810cedSChristophe Leroy mutex_lock(&text_mutex);
185c810cedSChristophe Leroy
195c810cedSChristophe Leroy if (func && !is_short) {
20*5799cd76SBenjamin Gray err = patch_ulong(tramp + PPC_SCT_DATA, target);
215c810cedSChristophe Leroy if (err)
225c810cedSChristophe Leroy goto out;
235c810cedSChristophe Leroy }
245c810cedSChristophe Leroy
255c810cedSChristophe Leroy if (!func)
265c810cedSChristophe Leroy err = patch_instruction(tramp, ppc_inst(PPC_RAW_BLR()));
275c810cedSChristophe Leroy else if (is_short)
285c810cedSChristophe Leroy err = patch_branch(tramp, target, 0);
295c810cedSChristophe Leroy else
305c810cedSChristophe Leroy err = patch_instruction(tramp, ppc_inst(PPC_RAW_NOP()));
315c810cedSChristophe Leroy out:
325c810cedSChristophe Leroy mutex_unlock(&text_mutex);
335c810cedSChristophe Leroy
345c810cedSChristophe Leroy if (err)
355c810cedSChristophe Leroy panic("%s: patching failed %pS at %pS\n", __func__, func, tramp);
365c810cedSChristophe Leroy }
375c810cedSChristophe Leroy EXPORT_SYMBOL_GPL(arch_static_call_transform);
38