/* SPDX-License-Identifier: GPL-2.0-only */1/*2* arch/arm/include/asm/fncpy.h - helper macros for function body copying3*4* Copyright (C) 2011 Linaro Limited5*/67/*8* These macros are intended for use when there is a need to copy a low-level9* function body into special memory.10*11* For example, when reconfiguring the SDRAM controller, the code doing the12* reconfiguration may need to run from SRAM.13*14* NOTE: that the copied function body must be entirely self-contained and15* position-independent in order for this to work properly.16*17* NOTE: in order for embedded literals and data to get referenced correctly,18* the alignment of functions must be preserved when copying. To ensure this,19* the source and destination addresses for fncpy() must be aligned to a20* multiple of 8 bytes: you will be get a BUG() if this condition is not met.21* You will typically need a ".align 3" directive in the assembler where the22* function to be copied is defined, and ensure that your allocator for the23* destination buffer returns 8-byte-aligned pointers.24*25* Typical usage example:26*27* extern int f(args);28* extern uint32_t size_of_f;29* int (*copied_f)(args);30* void *sram_buffer;31*32* copied_f = fncpy(sram_buffer, &f, size_of_f);33*34* ... later, call the function: ...35*36* copied_f(args);37*38* The size of the function to be copied can't be determined from C:39* this must be determined by other means, such as adding assmbler directives40* in the file where f is defined.41*/4243#ifndef __ASM_FNCPY_H44#define __ASM_FNCPY_H4546#include <linux/types.h>47#include <linux/string.h>4849#include <asm/bug.h>50#include <asm/cacheflush.h>5152/*53* Minimum alignment requirement for the source and destination addresses54* for function copying.55*/56#define FNCPY_ALIGN 85758#define fncpy(dest_buf, funcp, size) ({ \59uintptr_t __funcp_address; \60typeof(funcp) __result; \61\62asm("" : "=r" (__funcp_address) : "0" (funcp)); \63\64/* \65* Ensure alignment of source and destination addresses, \66* disregarding the function's Thumb bit: \67*/ \68BUG_ON((uintptr_t)(dest_buf) & (FNCPY_ALIGN - 1) || \69(__funcp_address & ~(uintptr_t)1 & (FNCPY_ALIGN - 1))); \70\71memcpy(dest_buf, (void const *)(__funcp_address & ~1), size); \72flush_icache_range((unsigned long)(dest_buf), \73(unsigned long)(dest_buf) + (size)); \74\75asm("" : "=r" (__result) \76: "0" ((uintptr_t)(dest_buf) | (__funcp_address & 1))); \77\78__result; \79})8081#endif /* !__ASM_FNCPY_H */828384