sync-cache.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Machine description for AArch64 architecture.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Contributed by ARM Ltd.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #define CTR_IDC_SHIFT 28
  21. #define CTR_DIC_SHIFT 29
  22. void __aarch64_sync_cache_range (const void *, const void *);
  23. void
  24. __aarch64_sync_cache_range (const void *base, const void *end)
  25. {
  26. unsigned icache_lsize;
  27. unsigned dcache_lsize;
  28. static unsigned int cache_info = 0;
  29. const char *address;
  30. if (! cache_info)
  31. /* CTR_EL0 [3:0] contains log2 of icache line size in words.
  32. CTR_EL0 [19:16] contains log2 of dcache line size in words. */
  33. asm volatile ("mrs\t%0, ctr_el0":"=r" (cache_info));
  34. icache_lsize = 4 << (cache_info & 0xF);
  35. dcache_lsize = 4 << ((cache_info >> 16) & 0xF);
  36. /* If CTR_EL0.IDC is enabled, Data cache clean to the Point of Unification is
  37. not required for instruction to data coherence. */
  38. if (((cache_info >> CTR_IDC_SHIFT) & 0x1) == 0x0) {
  39. /* Loop over the address range, clearing one cache line at once.
  40. Data cache must be flushed to unification first to make sure the
  41. instruction cache fetches the updated data. 'end' is exclusive,
  42. as per the GNU definition of __clear_cache. */
  43. /* Make the start address of the loop cache aligned. */
  44. address = (const char*) ((__UINTPTR_TYPE__) base
  45. & ~ (__UINTPTR_TYPE__) (dcache_lsize - 1));
  46. for (; address < (const char *) end; address += dcache_lsize)
  47. asm volatile ("dc\tcvau, %0"
  48. :
  49. : "r" (address)
  50. : "memory");
  51. }
  52. asm volatile ("dsb\tish" : : : "memory");
  53. /* If CTR_EL0.DIC is enabled, Instruction cache cleaning to the Point of
  54. Unification is not required for instruction to data coherence. */
  55. if (((cache_info >> CTR_DIC_SHIFT) & 0x1) == 0x0) {
  56. /* Make the start address of the loop cache aligned. */
  57. address = (const char*) ((__UINTPTR_TYPE__) base
  58. & ~ (__UINTPTR_TYPE__) (icache_lsize - 1));
  59. for (; address < (const char *) end; address += icache_lsize)
  60. asm volatile ("ic\tivau, %0"
  61. :
  62. : "r" (address)
  63. : "memory");
  64. asm volatile ("dsb\tish" : : : "memory");
  65. }
  66. asm volatile("isb" : : : "memory");
  67. }