cache.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /* for now: just dummy functions to satisfy the linker */
  7. #include <common.h>
  8. #include <malloc.h>
  9. /*
  10. * Flush range from all levels of d-cache/unified-cache.
  11. * Affects the range [start, start + size - 1].
  12. */
  13. __weak void flush_cache(unsigned long start, unsigned long size)
  14. {
  15. flush_dcache_range(start, start + size);
  16. }
  17. /*
  18. * Default implementation:
  19. * do a range flush for the entire range
  20. */
  21. __weak void flush_dcache_all(void)
  22. {
  23. flush_cache(0, ~0);
  24. }
  25. /*
  26. * Default implementation of enable_caches()
  27. * Real implementation should be in platform code
  28. */
  29. __weak void enable_caches(void)
  30. {
  31. puts("WARNING: Caches not enabled\n");
  32. }
  33. __weak void invalidate_dcache_range(unsigned long start, unsigned long stop)
  34. {
  35. /* An empty stub, real implementation should be in platform code */
  36. }
  37. __weak void flush_dcache_range(unsigned long start, unsigned long stop)
  38. {
  39. /* An empty stub, real implementation should be in platform code */
  40. }
  41. int check_cache_range(unsigned long start, unsigned long stop)
  42. {
  43. int ok = 1;
  44. if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
  45. ok = 0;
  46. if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
  47. ok = 0;
  48. if (!ok) {
  49. warn_non_spl("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
  50. start, stop);
  51. }
  52. return ok;
  53. }
  54. #ifdef CONFIG_SYS_NONCACHED_MEMORY
  55. /*
  56. * Reserve one MMU section worth of address space below the malloc() area that
  57. * will be mapped uncached.
  58. */
  59. static unsigned long noncached_start;
  60. static unsigned long noncached_end;
  61. static unsigned long noncached_next;
  62. void noncached_init(void)
  63. {
  64. phys_addr_t start, end;
  65. size_t size;
  66. end = ALIGN(mem_malloc_start, MMU_SECTION_SIZE) - MMU_SECTION_SIZE;
  67. size = ALIGN(CONFIG_SYS_NONCACHED_MEMORY, MMU_SECTION_SIZE);
  68. start = end - size;
  69. debug("mapping memory %pa-%pa non-cached\n", &start, &end);
  70. noncached_start = start;
  71. noncached_end = end;
  72. noncached_next = start;
  73. #ifndef CONFIG_SYS_DCACHE_OFF
  74. mmu_set_region_dcache_behaviour(noncached_start, size, DCACHE_OFF);
  75. #endif
  76. }
  77. phys_addr_t noncached_alloc(size_t size, size_t align)
  78. {
  79. phys_addr_t next = ALIGN(noncached_next, align);
  80. if (next >= noncached_end || (noncached_end - next) < size)
  81. return 0;
  82. debug("allocated %zu bytes of uncached memory @%pa\n", size, &next);
  83. noncached_next = next + size;
  84. return next;
  85. }
  86. #endif /* CONFIG_SYS_NONCACHED_MEMORY */
  87. #if CONFIG_IS_ENABLED(SYS_THUMB_BUILD)
  88. void invalidate_l2_cache(void)
  89. {
  90. unsigned int val = 0;
  91. asm volatile("mcr p15, 1, %0, c15, c11, 0 @ invl l2 cache"
  92. : : "r" (val) : "cc");
  93. isb();
  94. }
  95. #endif