runtime-const.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_RUNTIME_CONST_H
  3. #define _ASM_RUNTIME_CONST_H
  4. #include <asm/cacheflush.h>
  5. /* Sigh. You can still run arm64 in BE mode */
  6. #include <asm/byteorder.h>
  7. #define runtime_const_ptr(sym) ({ \
  8. typeof(sym) __ret; \
  9. asm_inline("1:\t" \
  10. "movz %0, #0xcdef\n\t" \
  11. "movk %0, #0x89ab, lsl #16\n\t" \
  12. "movk %0, #0x4567, lsl #32\n\t" \
  13. "movk %0, #0x0123, lsl #48\n\t" \
  14. ".pushsection runtime_ptr_" #sym ",\"a\"\n\t" \
  15. ".long 1b - .\n\t" \
  16. ".popsection" \
  17. :"=r" (__ret)); \
  18. __ret; })
  19. #define runtime_const_shift_right_32(val, sym) ({ \
  20. unsigned long __ret; \
  21. asm_inline("1:\t" \
  22. "lsr %w0,%w1,#12\n\t" \
  23. ".pushsection runtime_shift_" #sym ",\"a\"\n\t" \
  24. ".long 1b - .\n\t" \
  25. ".popsection" \
  26. :"=r" (__ret) \
  27. :"r" (0u+(val))); \
  28. __ret; })
  29. #define runtime_const_init(type, sym) do { \
  30. extern s32 __start_runtime_##type##_##sym[]; \
  31. extern s32 __stop_runtime_##type##_##sym[]; \
  32. runtime_const_fixup(__runtime_fixup_##type, \
  33. (unsigned long)(sym), \
  34. __start_runtime_##type##_##sym, \
  35. __stop_runtime_##type##_##sym); \
  36. } while (0)
  37. /* 16-bit immediate for wide move (movz and movk) in bits 5..20 */
  38. static inline void __runtime_fixup_16(__le32 *p, unsigned int val)
  39. {
  40. u32 insn = le32_to_cpu(*p);
  41. insn &= 0xffe0001f;
  42. insn |= (val & 0xffff) << 5;
  43. *p = cpu_to_le32(insn);
  44. }
  45. static inline void __runtime_fixup_caches(void *where, unsigned int insns)
  46. {
  47. unsigned long va = (unsigned long)where;
  48. caches_clean_inval_pou(va, va + 4*insns);
  49. }
  50. static inline void __runtime_fixup_ptr(void *where, unsigned long val)
  51. {
  52. __le32 *p = lm_alias(where);
  53. __runtime_fixup_16(p, val);
  54. __runtime_fixup_16(p+1, val >> 16);
  55. __runtime_fixup_16(p+2, val >> 32);
  56. __runtime_fixup_16(p+3, val >> 48);
  57. __runtime_fixup_caches(where, 4);
  58. }
  59. /* Immediate value is 6 bits starting at bit #16 */
  60. static inline void __runtime_fixup_shift(void *where, unsigned long val)
  61. {
  62. __le32 *p = lm_alias(where);
  63. u32 insn = le32_to_cpu(*p);
  64. insn &= 0xffc0ffff;
  65. insn |= (val & 63) << 16;
  66. *p = cpu_to_le32(insn);
  67. __runtime_fixup_caches(where, 1);
  68. }
  69. static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
  70. unsigned long val, s32 *start, s32 *end)
  71. {
  72. while (start < end) {
  73. fn(*start + (void *)start, val);
  74. start++;
  75. }
  76. }
  77. #endif