test_cache_aarch64.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-03-17 WangXiaoyao cache API unit test
  9. */
  10. #ifndef __TEST_CACHE_AARCH64_H__
  11. #define __TEST_CACHE_AARCH64_H__
  12. #include "common.h"
  13. #include <cache.h>
  14. const char *platform_cache_not_guarantee = "Cannot guarantee cache operation works";
  15. /**
  16. * ==============================================================
  17. * TEST FEATURE
  18. * API under cache.h
  19. *
  20. * void rt_hw_icache_invalidate_range(unsigned long start_addr, int size);
  21. * void rt_hw_cpu_icache_invalidate(void *addr, rt_size_t size);
  22. * void rt_hw_cpu_dcache_clean_and_invalidate(void *addr, rt_size_t size);
  23. * ==============================================================
  24. */
  25. static int _get1_const(void)
  26. {
  27. return 1;
  28. }
  29. static int _get1(void)
  30. {
  31. return 1;
  32. }
  33. static int _get2(void)
  34. {
  35. return 2;
  36. }
  37. /* hot patching codes and test if the value can be seen by icache */
  38. static void _test_icache_invalidate_range(void)
  39. {
  40. /* reset _get1 */
  41. rt_memcpy(_get1, _get1_const, _get2 - _get1);
  42. rt_hw_cpu_dcache_clean(_get1, _get2 - _get1);
  43. rt_hw_cpu_icache_invalidate(_get1, _get2 - _get1);
  44. uassert_true(1 == _get1());
  45. /* now copy _get2 to _get1 */
  46. rt_memcpy(_get1, _get2, _get2 - _get1);
  47. if (1 != _get1())
  48. LOG_W(platform_cache_not_guarantee);
  49. rt_hw_cpu_dcache_clean(_get1, _get2 - _get1);
  50. rt_hw_cpu_icache_invalidate(_get1, _get2 - _get1);
  51. __asm__ volatile("isb");
  52. uassert_true(2 == _get1());
  53. LOG_I("%s ok", __func__);
  54. }
  55. /* due to hardware feature of cortex-a, we should done this on 2 separated cpu */
  56. static void _test_dcache_clean_and_invalidate(void)
  57. {
  58. const size_t padding = 1024 * 2;
  59. const size_t buf_sz = ARCH_PAGE_SIZE * 2;
  60. volatile char *remap_nocache;
  61. char *page = rt_pages_alloc(rt_page_bits(buf_sz));
  62. uassert_true(!!page);
  63. rt_memset(page, 0xab, buf_sz);
  64. rt_hw_cpu_dcache_invalidate(page, buf_sz);
  65. int _outdate_flag = 0;
  66. if (memtest(page, 0xab, buf_sz))
  67. _outdate_flag = 1;
  68. /* after ioremap, we can access system memory to verify outcome */
  69. remap_nocache = rt_ioremap(page + PV_OFFSET, buf_sz);
  70. rt_hw_cpu_dcache_clean(page + padding, ARCH_PAGE_SIZE);
  71. memtest(remap_nocache + padding, 0xab, ARCH_PAGE_SIZE);
  72. if (!_outdate_flag)
  73. LOG_W(platform_cache_not_guarantee);
  74. else
  75. LOG_I("%s ok", __func__);
  76. rt_pages_free(page, 0);
  77. rt_iounmap(remap_nocache);
  78. }
  79. static rt_err_t utest_tc_init(void)
  80. {
  81. return RT_EOK;
  82. }
  83. static rt_err_t utest_tc_cleanup(void)
  84. {
  85. return RT_EOK;
  86. }
  87. static void testcase(void)
  88. {
  89. /* todo: format API under cache.h first */
  90. UTEST_UNIT_RUN(_test_icache_invalidate_range);
  91. UTEST_UNIT_RUN(_test_dcache_clean_and_invalidate);
  92. }
  93. UTEST_TC_EXPORT(testcase, "testcases.libcpu.cache", utest_tc_init, utest_tc_cleanup, 10);
  94. #endif /* __TEST_CACHE_AARCH64_H__ */