1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/cache.h>
9 
10 #define SIZE	(4096)
11 
12 static ZTEST_BMEM uint8_t user_buffer[SIZE];
13 
ZTEST(cache_api,test_instr_cache_api)14 ZTEST(cache_api, test_instr_cache_api)
15 {
16 	int ret;
17 
18 #ifdef CONFIG_XTENSA_MMU
19 	/* With MMU enabled, user_buffer is not marked as executable.
20 	 * Invalidating the i-cache by region will cause an instruction
21 	 * fetch prohibited exception. So skip all i-cache tests,
22 	 * instead of just the range ones to avoid confusions of
23 	 * only running the test partially.
24 	 */
25 	ztest_test_skip();
26 #endif
27 
28 	ret = sys_cache_instr_flush_all();
29 	zassert_true((ret == 0) || (ret == -ENOTSUP));
30 
31 	ret = sys_cache_instr_invd_all();
32 	zassert_true((ret == 0) || (ret == -ENOTSUP));
33 
34 	ret = sys_cache_instr_flush_and_invd_all();
35 	zassert_true((ret == 0) || (ret == -ENOTSUP));
36 
37 	ret = sys_cache_instr_flush_range(user_buffer, SIZE);
38 	zassert_true((ret == 0) || (ret == -ENOTSUP));
39 
40 	ret = sys_cache_instr_invd_range(user_buffer, SIZE);
41 	zassert_true((ret == 0) || (ret == -ENOTSUP));
42 
43 	ret = sys_cache_instr_flush_and_invd_range(user_buffer, SIZE);
44 	zassert_true((ret == 0) || (ret == -ENOTSUP));
45 }
46 
ZTEST(cache_api,test_data_cache_api)47 ZTEST(cache_api, test_data_cache_api)
48 {
49 	int ret;
50 
51 	ret = sys_cache_data_flush_all();
52 	zassert_true((ret == 0) || (ret == -ENOTSUP));
53 
54 	ret = sys_cache_data_flush_and_invd_all();
55 	zassert_true((ret == 0) || (ret == -ENOTSUP));
56 
57 	ret = sys_cache_data_flush_range(user_buffer, SIZE);
58 	zassert_true((ret == 0) || (ret == -ENOTSUP));
59 
60 	ret = sys_cache_data_invd_range(user_buffer, SIZE);
61 	zassert_true((ret == 0) || (ret == -ENOTSUP));
62 
63 	ret = sys_cache_data_flush_and_invd_range(user_buffer, SIZE);
64 	zassert_true((ret == 0) || (ret == -ENOTSUP));
65 
66 }
67 
ZTEST_USER(cache_api,test_data_cache_api_user)68 ZTEST_USER(cache_api, test_data_cache_api_user)
69 {
70 	int ret;
71 
72 	ret = sys_cache_data_flush_range(user_buffer, SIZE);
73 	zassert_true((ret == 0) || (ret == -ENOTSUP));
74 
75 	ret = sys_cache_data_invd_range(user_buffer, SIZE);
76 	zassert_true((ret == 0) || (ret == -ENOTSUP));
77 
78 	ret = sys_cache_data_flush_and_invd_range(user_buffer, SIZE);
79 	zassert_true((ret == 0) || (ret == -ENOTSUP));
80 }
81 
cache_api_setup(void)82 static void *cache_api_setup(void)
83 {
84 	sys_cache_data_enable();
85 	sys_cache_instr_enable();
86 
87 	return NULL;
88 }
89 
cache_api_teardown(void * unused)90 static void cache_api_teardown(void *unused)
91 {
92 	sys_cache_data_disable();
93 	sys_cache_instr_disable();
94 }
95 
96 ZTEST_SUITE(cache_api, NULL, cache_api_setup, NULL, NULL, cache_api_teardown);
97