cache.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef __ASSEMBLY__
  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. /*
  71. * Read the effective value of CTR_EL0.
  72. *
  73. * According to ARM ARM for ARMv8-A (ARM DDI 0487C.a),
  74. * section D10.2.33 "CTR_EL0, Cache Type Register" :
  75. *
  76. * CTR_EL0.IDC reports the data cache clean requirements for
  77. * instruction to data coherence.
  78. *
  79. * 0 - dcache clean to PoU is required unless :
  80. * (CLIDR_EL1.LoC == 0) || (CLIDR_EL1.LoUIS == 0 && CLIDR_EL1.LoUU == 0)
  81. * 1 - dcache clean to PoU is not required for i-to-d coherence.
  82. *
  83. * This routine provides the CTR_EL0 with the IDC field updated to the
  84. * effective state.
  85. */
  86. static inline u32 __attribute_const__ read_cpuid_effective_cachetype(void)
  87. {
  88. u32 ctr = read_cpuid_cachetype();
  89. if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
  90. u64 clidr = read_sysreg(clidr_el1);
  91. if (CLIDR_LOC(clidr) == 0 ||
  92. (CLIDR_LOUIS(clidr) == 0 && CLIDR_LOUU(clidr) == 0))
  93. ctr |= BIT(CTR_EL0_IDC_SHIFT);
  94. }
  95. return ctr;
  96. }
  97. #endif /* __ASSEMBLY__ */
  98. #endif