hwcache.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-25 GuEe-GUI the first version
  9. */
  10. #include <drivers/hwcache.h>
  11. #define DBG_TAG "rtdm.hwcache"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. struct rt_hwcache_ops *rt_dm_cpu_dcache_ops = RT_NULL;
  15. struct rt_hwcache_ops *rt_dm_cpu_icache_ops = RT_NULL;
  16. static void hwcache_enable(struct rt_hwcache_ops *ops)
  17. {
  18. if (ops && ops->enable)
  19. {
  20. ops->enable();
  21. }
  22. }
  23. static void hwcache_disable(struct rt_hwcache_ops *ops)
  24. {
  25. if (ops && ops->enable)
  26. {
  27. ops->enable();
  28. }
  29. }
  30. static rt_base_t hwcache_status(struct rt_hwcache_ops *ops)
  31. {
  32. if (ops && ops->status)
  33. {
  34. return ops->status();
  35. }
  36. return 0;
  37. }
  38. static void hwcache_ops(struct rt_hwcache_ops *ops, int op, void *addr, int size)
  39. {
  40. if (ops)
  41. {
  42. if (op == RT_HW_CACHE_FLUSH)
  43. {
  44. if (ops->flush)
  45. {
  46. ops->flush(addr, size);
  47. }
  48. }
  49. else if (op == RT_HW_CACHE_INVALIDATE)
  50. {
  51. if (ops->invalidate)
  52. {
  53. ops->invalidate(addr, size);
  54. }
  55. }
  56. }
  57. }
  58. void rt_hwcache_icache_enable(void)
  59. {
  60. hwcache_enable(rt_dm_cpu_icache_ops);
  61. }
  62. void rt_hwcache_icache_disable(void)
  63. {
  64. hwcache_disable(rt_dm_cpu_icache_ops);
  65. }
  66. rt_base_t rt_hwcache_icache_status(void)
  67. {
  68. return hwcache_status(rt_dm_cpu_icache_ops);
  69. }
  70. void rt_hwcache_icache_ops(int ops, void *addr, int size)
  71. {
  72. hwcache_ops(rt_dm_cpu_icache_ops, ops, addr, size);
  73. }
  74. void rt_hwcache_dcache_enable(void)
  75. {
  76. hwcache_enable(rt_dm_cpu_dcache_ops);
  77. }
  78. void rt_hwcache_dcache_disable(void)
  79. {
  80. hwcache_disable(rt_dm_cpu_dcache_ops);
  81. }
  82. rt_base_t rt_hwcache_dcache_status(void)
  83. {
  84. return hwcache_status(rt_dm_cpu_dcache_ops);
  85. }
  86. void rt_hwcache_dcache_ops(int ops, void *addr, int size)
  87. {
  88. hwcache_ops(rt_dm_cpu_dcache_ops, ops, addr, size);
  89. }
  90. #ifdef RT_USING_OFW
  91. RT_OFW_STUB_RANGE_EXPORT(hwcache, _hwcache_ofw_start, _hwcache_ofw_end);
  92. static rt_err_t ofw_hwcache_init(void)
  93. {
  94. struct rt_ofw_node *cache_np;
  95. rt_ofw_foreach_allnodes(cache_np)
  96. {
  97. rt_ofw_stub_probe_range(cache_np, &_hwcache_ofw_start, &_hwcache_ofw_end);
  98. }
  99. return RT_EOK;
  100. }
  101. #else
  102. static rt_err_t ofw_hwcache_init(void)
  103. {
  104. return RT_EOK;
  105. }
  106. #endif /* !RT_USING_OFW */
  107. rt_err_t rt_hwcache_init(void)
  108. {
  109. rt_err_t err;
  110. LOG_D("init start");
  111. err = ofw_hwcache_init();
  112. LOG_D("init end");
  113. return err;
  114. }