cache.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /*
  16. * Memory returned by kmalloc() may be used for DMA, so we must make
  17. * sure that all such allocations are cache aligned. Otherwise,
  18. * unrelated code may cause parts of the buffer to be read into the
  19. * cache before the transfer is done, causing old data to be seen by
  20. * the CPU.
  21. */
  22. #define ARCH_DMA_MINALIGN (64)
  23. #ifndef __ASSEMBLY__
  24. #include <linux/bitops.h>
  25. #include <linux/kasan-enabled.h>
  26. #include <asm/cputype.h>
  27. #include <asm/mte-def.h>
  28. #include <asm/sysreg.h>
  29. #ifdef CONFIG_KASAN_SW_TAGS
  30. #define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT)
  31. #elif defined(CONFIG_KASAN_HW_TAGS)
  32. static inline unsigned int arch_slab_minalign(void)
  33. {
  34. return kasan_hw_tags_enabled() ? MTE_GRANULE_SIZE :
  35. __alignof__(unsigned long long);
  36. }
  37. #define arch_slab_minalign() arch_slab_minalign()
  38. #endif
  39. #define CTR_L1IP(ctr) SYS_FIELD_GET(CTR_EL0, L1Ip, ctr)
  40. #define ICACHEF_ALIASING 0
  41. #define ICACHEF_VPIPT 1
  42. extern unsigned long __icache_flags;
  43. /*
  44. * Whilst the D-side always behaves as PIPT on AArch64, aliasing is
  45. * permitted in the I-cache.
  46. */
  47. static inline int icache_is_aliasing(void)
  48. {
  49. return test_bit(ICACHEF_ALIASING, &__icache_flags);
  50. }
  51. static __always_inline int icache_is_vpipt(void)
  52. {
  53. return test_bit(ICACHEF_VPIPT, &__icache_flags);
  54. }
  55. static inline u32 cache_type_cwg(void)
  56. {
  57. return SYS_FIELD_GET(CTR_EL0, CWG, read_cpuid_cachetype());
  58. }
  59. #define __read_mostly __section(".data..read_mostly")
  60. static inline int cache_line_size_of_cpu(void)
  61. {
  62. u32 cwg = cache_type_cwg();
  63. return cwg ? 4 << cwg : ARCH_DMA_MINALIGN;
  64. }
  65. int cache_line_size(void);
  66. /*
  67. * Read the effective value of CTR_EL0.
  68. *
  69. * According to ARM ARM for ARMv8-A (ARM DDI 0487C.a),
  70. * section D10.2.33 "CTR_EL0, Cache Type Register" :
  71. *
  72. * CTR_EL0.IDC reports the data cache clean requirements for
  73. * instruction to data coherence.
  74. *
  75. * 0 - dcache clean to PoU is required unless :
  76. * (CLIDR_EL1.LoC == 0) || (CLIDR_EL1.LoUIS == 0 && CLIDR_EL1.LoUU == 0)
  77. * 1 - dcache clean to PoU is not required for i-to-d coherence.
  78. *
  79. * This routine provides the CTR_EL0 with the IDC field updated to the
  80. * effective state.
  81. */
  82. static inline u32 __attribute_const__ read_cpuid_effective_cachetype(void)
  83. {
  84. u32 ctr = read_cpuid_cachetype();
  85. if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
  86. u64 clidr = read_sysreg(clidr_el1);
  87. if (CLIDR_LOC(clidr) == 0 ||
  88. (CLIDR_LOUIS(clidr) == 0 && CLIDR_LOUU(clidr) == 0))
  89. ctr |= BIT(CTR_EL0_IDC_SHIFT);
  90. }
  91. return ctr;
  92. }
  93. #endif /* __ASSEMBLY__ */
  94. #endif