cache.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. */
  5. #ifndef __ASM_CACHE_H
  6. #define __ASM_CACHE_H
  7. #define L1_CACHE_SHIFT (6)
  8. #define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT)
  9. #define CLIDR_LOUU_SHIFT 27
  10. #define CLIDR_LOC_SHIFT 24
  11. #define CLIDR_LOUIS_SHIFT 21
  12. #define CLIDR_LOUU(clidr) (((clidr) >> CLIDR_LOUU_SHIFT) & 0x7)
  13. #define CLIDR_LOC(clidr) (((clidr) >> CLIDR_LOC_SHIFT) & 0x7)
  14. #define CLIDR_LOUIS(clidr) (((clidr) >> CLIDR_LOUIS_SHIFT) & 0x7)
  15. /* Ctypen, bits[3(n - 1) + 2 : 3(n - 1)], for n = 1 to 7 */
  16. #define CLIDR_CTYPE_SHIFT(level) (3 * (level - 1))
  17. #define CLIDR_CTYPE_MASK(level) (7 << CLIDR_CTYPE_SHIFT(level))
  18. #define CLIDR_CTYPE(clidr, level) \
  19. (((clidr) & CLIDR_CTYPE_MASK(level)) >> CLIDR_CTYPE_SHIFT(level))
  20. /* Ttypen, bits [2(n - 1) + 34 : 2(n - 1) + 33], for n = 1 to 7 */
  21. #define CLIDR_TTYPE_SHIFT(level) (2 * ((level) - 1) + CLIDR_EL1_Ttypen_SHIFT)
  22. /*
  23. * Memory returned by kmalloc() may be used for DMA, so we must make
  24. * sure that all such allocations are cache aligned. Otherwise,
  25. * unrelated code may cause parts of the buffer to be read into the
  26. * cache before the transfer is done, causing old data to be seen by
  27. * the CPU.
  28. */
  29. #define ARCH_DMA_MINALIGN (128)
  30. #define ARCH_KMALLOC_MINALIGN (8)
  31. #if !defined(__ASSEMBLER__) && !defined(BUILD_VDSO)
  32. #include <linux/bitops.h>
  33. #include <linux/kasan-enabled.h>
  34. #include <asm/cputype.h>
  35. #include <asm/mte-def.h>
  36. #include <asm/sysreg.h>
  37. #ifdef CONFIG_KASAN_SW_TAGS
  38. #define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT)
  39. #elif defined(CONFIG_KASAN_HW_TAGS)
  40. static inline unsigned int arch_slab_minalign(void)
  41. {
  42. return kasan_hw_tags_enabled() ? MTE_GRANULE_SIZE :
  43. __alignof__(unsigned long long);
  44. }
  45. #define arch_slab_minalign() arch_slab_minalign()
  46. #endif
  47. #define CTR_L1IP(ctr) SYS_FIELD_GET(CTR_EL0, L1Ip, ctr)
  48. #define ICACHEF_ALIASING 0
  49. extern unsigned long __icache_flags;
  50. /*
  51. * Whilst the D-side always behaves as PIPT on AArch64, aliasing is
  52. * permitted in the I-cache.
  53. */
  54. static inline int icache_is_aliasing(void)
  55. {
  56. return test_bit(ICACHEF_ALIASING, &__icache_flags);
  57. }
  58. static inline u32 cache_type_cwg(void)
  59. {
  60. return SYS_FIELD_GET(CTR_EL0, CWG, read_cpuid_cachetype());
  61. }
  62. #define __read_mostly __section(".data..read_mostly")
  63. static inline int cache_line_size_of_cpu(void)
  64. {
  65. u32 cwg = cache_type_cwg();
  66. return cwg ? 4 << cwg : ARCH_DMA_MINALIGN;
  67. }
  68. int cache_line_size(void);
  69. #define dma_get_cache_alignment cache_line_size
  70. /* Compress a u64 MPIDR value into 32 bits. */
  71. static inline u64 arch_compact_of_hwid(u64 id)
  72. {
  73. u64 aff3 = MPIDR_AFFINITY_LEVEL(id, 3);
  74. /*
  75. * These bits are expected to be RES0. If not, return a value with
  76. * the upper 32 bits set to force the caller to give up on 32 bit
  77. * cache ids.
  78. */
  79. if (FIELD_GET(GENMASK_ULL(63, 40), id))
  80. return id;
  81. return (aff3 << 24) | FIELD_GET(GENMASK_ULL(23, 0), id);
  82. }
  83. #define arch_compact_of_hwid arch_compact_of_hwid
  84. /*
  85. * Read the effective value of CTR_EL0.
  86. *
  87. * According to ARM ARM for ARMv8-A (ARM DDI 0487C.a),
  88. * section D10.2.33 "CTR_EL0, Cache Type Register" :
  89. *
  90. * CTR_EL0.IDC reports the data cache clean requirements for
  91. * instruction to data coherence.
  92. *
  93. * 0 - dcache clean to PoU is required unless :
  94. * (CLIDR_EL1.LoC == 0) || (CLIDR_EL1.LoUIS == 0 && CLIDR_EL1.LoUU == 0)
  95. * 1 - dcache clean to PoU is not required for i-to-d coherence.
  96. *
  97. * This routine provides the CTR_EL0 with the IDC field updated to the
  98. * effective state.
  99. */
  100. static inline u32 __attribute_const__ read_cpuid_effective_cachetype(void)
  101. {
  102. u32 ctr = read_cpuid_cachetype();
  103. if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
  104. u64 clidr = read_sysreg(clidr_el1);
  105. if (CLIDR_LOC(clidr) == 0 ||
  106. (CLIDR_LOUIS(clidr) == 0 && CLIDR_LOUU(clidr) == 0))
  107. ctr |= BIT(CTR_EL0_IDC_SHIFT);
  108. }
  109. return ctr;
  110. }
  111. #endif /* !defined(__ASSEMBLER__) && !defined(BUILD_VDSO) */
  112. #endif