1*5c810cedSChristophe Leroy // SPDX-License-Identifier: GPL-2.0
2*5c810cedSChristophe Leroy #include <linux/memory.h>
3*5c810cedSChristophe Leroy #include <linux/static_call.h>
4*5c810cedSChristophe Leroy 
5*5c810cedSChristophe Leroy #include <asm/code-patching.h>
6*5c810cedSChristophe Leroy 
arch_static_call_transform(void * site,void * tramp,void * func,bool tail)7*5c810cedSChristophe Leroy void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
8*5c810cedSChristophe Leroy {
9*5c810cedSChristophe Leroy 	int err;
10*5c810cedSChristophe Leroy 	bool is_ret0 = (func == __static_call_return0);
11*5c810cedSChristophe Leroy 	unsigned long target = (unsigned long)(is_ret0 ? tramp + PPC_SCT_RET0 : func);
12*5c810cedSChristophe Leroy 	bool is_short = is_offset_in_branch_range((long)target - (long)tramp);
13*5c810cedSChristophe Leroy 
14*5c810cedSChristophe Leroy 	if (!tramp)
15*5c810cedSChristophe Leroy 		return;
16*5c810cedSChristophe Leroy 
17*5c810cedSChristophe Leroy 	mutex_lock(&text_mutex);
18*5c810cedSChristophe Leroy 
19*5c810cedSChristophe Leroy 	if (func && !is_short) {
20*5c810cedSChristophe Leroy 		err = patch_instruction(tramp + PPC_SCT_DATA, ppc_inst(target));
21*5c810cedSChristophe Leroy 		if (err)
22*5c810cedSChristophe Leroy 			goto out;
23*5c810cedSChristophe Leroy 	}
24*5c810cedSChristophe Leroy 
25*5c810cedSChristophe Leroy 	if (!func)
26*5c810cedSChristophe Leroy 		err = patch_instruction(tramp, ppc_inst(PPC_RAW_BLR()));
27*5c810cedSChristophe Leroy 	else if (is_short)
28*5c810cedSChristophe Leroy 		err = patch_branch(tramp, target, 0);
29*5c810cedSChristophe Leroy 	else
30*5c810cedSChristophe Leroy 		err = patch_instruction(tramp, ppc_inst(PPC_RAW_NOP()));
31*5c810cedSChristophe Leroy out:
32*5c810cedSChristophe Leroy 	mutex_unlock(&text_mutex);
33*5c810cedSChristophe Leroy 
34*5c810cedSChristophe Leroy 	if (err)
35*5c810cedSChristophe Leroy 		panic("%s: patching failed %pS at %pS\n", __func__, func, tramp);
36*5c810cedSChristophe Leroy }
37*5c810cedSChristophe Leroy EXPORT_SYMBOL_GPL(arch_static_call_transform);
38