cache.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright 2018 SiFive, Inc */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. #ifndef METAL__CACHE_H
  4. #define METAL__CACHE_H
  5. /*!
  6. * @file cache.h
  7. *
  8. * @brief API for configuring caches
  9. */
  10. #include <stdint.h>
  11. struct metal_cache;
  12. struct __metal_cache_vtable {
  13. void (*init)(struct metal_cache *cache, int ways);
  14. int (*get_enabled_ways)(struct metal_cache *cache);
  15. int (*set_enabled_ways)(struct metal_cache *cache, int ways);
  16. };
  17. /*!
  18. * @brief a handle for a cache
  19. */
  20. struct metal_cache {
  21. const struct __metal_cache_vtable *vtable;
  22. };
  23. /*!
  24. * @brief Initialize a cache
  25. * @param cache The handle for the cache to initialize
  26. * @param ways The number of ways to enable
  27. *
  28. * Initializes a cache with the requested number of ways enabled.
  29. */
  30. __inline__ void metal_cache_init(struct metal_cache *cache, int ways) {
  31. cache->vtable->init(cache, ways);
  32. }
  33. /*!
  34. * @brief Get the current number of enabled cache ways
  35. * @param cache The handle for the cache
  36. * @return The current number of enabled cache ways
  37. */
  38. __inline__ int metal_cache_get_enabled_ways(struct metal_cache *cache) {
  39. return cache->vtable->get_enabled_ways(cache);
  40. }
  41. /*!
  42. * @brief Enable the requested number of cache ways
  43. * @param cache The handle for the cache
  44. * @param ways The number of ways to enabled
  45. * @return 0 if the ways are successfully enabled
  46. */
  47. __inline__ int metal_cache_set_enabled_ways(struct metal_cache *cache, int ways) {
  48. return cache->vtable->set_enabled_ways(cache, ways);
  49. }
  50. /*!
  51. * @brief Check if dcache is supported on the core
  52. * @param hartid The core to check
  53. * @return 1 if dcache is present
  54. */
  55. int metal_dcache_l1_available(int hartid);
  56. /*!
  57. * @brief Flush dcache for L1 on the requested core with write back
  58. * @param hartid The core to flush
  59. * @param address The virtual address of cacheline to invalidate
  60. * @return None
  61. */
  62. void metal_dcache_l1_flush(int hartid, uintptr_t address);
  63. /*!
  64. * @brief Discard dcache for L1 on the requested core with no write back
  65. * @param hartid The core to discard
  66. * @param address The virtual address of cacheline to invalidate
  67. * @return None
  68. */
  69. void metal_dcache_l1_discard(int hartid, uintptr_t address);
  70. /*!
  71. * @brief Check if icache is supported on the core
  72. * @param hartid The core to check
  73. * @return 1 if icache is present
  74. */
  75. int metal_icache_l1_available(int hartid);
  76. /*!
  77. * @brief Flush icache for L1 on the requested core
  78. * @param hartid The core to flush
  79. * @return None
  80. */
  81. void metal_icache_l1_flush(int hartid);
  82. #endif