mem_cache.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <zephyr.h>
  7. #include <spicache.h>
  8. #include <memory/mem_cache.h>
  9. static K_MUTEX_DEFINE(cache_mutex);
  10. static void _mem_dcache_ops(uint8_t ops, const void *addr, uint32_t length)
  11. {
  12. if (buf_is_psram(addr)) {
  13. k_mutex_lock(&cache_mutex, K_FOREVER);
  14. spi1_cache_ops(ops, (void *)addr, length);
  15. k_mutex_unlock(&cache_mutex);
  16. }
  17. }
  18. void mem_dcache_sync(void)
  19. {
  20. k_mutex_lock(&cache_mutex, K_FOREVER);
  21. spi1_cache_ops_wait_finshed();
  22. k_mutex_unlock(&cache_mutex);
  23. }
  24. void * mem_addr_to_uncache(void * addr)
  25. {
  26. if (buf_is_psram(addr)) {
  27. return cache_to_uncache(addr);
  28. }
  29. return addr;
  30. }
  31. void mem_dcache_invalidate(const void *addr, uint32_t length)
  32. {
  33. _mem_dcache_ops(SPI_CACHE_INVALIDATE, addr, length);
  34. }
  35. void mem_dcache_invalidate_all(void)
  36. {
  37. #ifdef CONFIG_PSRAM_SIZE
  38. _mem_dcache_ops(SPI_CACHE_INVALID_ALL, (void *)CONFIG_PSRAM_BASE_ADDRESS, CONFIG_PSRAM_SIZE * 1024);
  39. #endif
  40. }
  41. void mem_dcache_clean(const void *addr, uint32_t length)
  42. {
  43. _mem_dcache_ops(SPI_CACHE_FLUSH, addr, length);
  44. }
  45. void mem_dcache_clean_all(void)
  46. {
  47. #ifdef CONFIG_PSRAM_SIZE
  48. _mem_dcache_ops(SPI_CACHE_FLUSH_ALL, (void *)CONFIG_PSRAM_BASE_ADDRESS, CONFIG_PSRAM_SIZE * 1024);
  49. #endif
  50. }
  51. void mem_dcache_flush(const void *addr, uint32_t length)
  52. {
  53. _mem_dcache_ops(SPI_CACHE_FLUSH_INVALID, addr, length);
  54. }
  55. void mem_dcache_flush_all(void)
  56. {
  57. #ifdef CONFIG_PSRAM_SIZE
  58. _mem_dcache_ops(SPI_CACHE_FLUSH_INVALID_ALL, (void *)CONFIG_PSRAM_BASE_ADDRESS, CONFIG_PSRAM_SIZE * 1024);
  59. #endif
  60. }
  61. bool mem_is_cacheable(const void *addr)
  62. {
  63. return buf_is_psram(addr);
  64. }
  65. void mem_writebuf_clean(const void *addr, uint32_t length)
  66. {
  67. if (buf_is_psram_un(addr)) {
  68. spi1_cache_ops(SPI_WRITEBUF_FLUSH, (void *)addr, length);
  69. }
  70. }
  71. void mem_writebuf_clean_all(void)
  72. {
  73. #ifdef CONFIG_PSRAM_SIZE
  74. mem_writebuf_clean((void *)SPI1_UNCACHE_ADDR, CONFIG_PSRAM_SIZE * 1024);
  75. #endif
  76. }
  77. bool mem_is_bufferable(const void *addr)
  78. {
  79. return buf_is_psram_un(addr);
  80. }