1 /*
2  * Copyright (c) 2023 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/drivers/mm/mm_drv_bank.h>
9 
10 #define BANK_PAGES  64
11 
12 #define EXPECTED(x)  ((x) * CONFIG_MM_DRV_PAGE_SIZE)
13 
14 static struct sys_mm_drv_bank bank_data = {0x123, 0x234, 0x345};
15 
test_stats(const char * error_string,struct sys_memory_stats * stats,struct sys_memory_stats * expected)16 static void test_stats(const char *error_string,
17 		       struct sys_memory_stats *stats,
18 		       struct sys_memory_stats *expected)
19 {
20 	zassert_equal(stats->free_bytes, expected->free_bytes,
21 		      "%s: [free_bytes] = %u, not %u\n", error_string,
22 		      stats->free_bytes, expected->free_bytes);
23 	zassert_equal(stats->allocated_bytes, expected->allocated_bytes,
24 		      "%s: [allocated_bytes] = %u, not %u\n", error_string,
25 		      stats->allocated_bytes, expected->allocated_bytes);
26 	zassert_equal(stats->max_allocated_bytes, expected->max_allocated_bytes,
27 		      "%s: [max_allocated_bytes] = %u, not %u\n", error_string,
28 		      stats->max_allocated_bytes,
29 		      expected->max_allocated_bytes);
30 }
31 
ZTEST(sys_mm_bank_api,test_sys_mm_drv_bank)32 ZTEST(sys_mm_bank_api, test_sys_mm_drv_bank)
33 {
34 	struct sys_memory_stats stats;
35 	struct sys_memory_stats expected;
36 	uint32_t  count;
37 	uint32_t index;
38 
39 	/* Verify that the initialization routine works as expected. */
40 	/* It set mapped state for all pages                         */
41 	sys_mm_drv_bank_init(&bank_data, BANK_PAGES);
42 	expected.free_bytes = EXPECTED(0);
43 	expected.allocated_bytes = EXPECTED(BANK_PAGES);
44 	expected.max_allocated_bytes = EXPECTED(BANK_PAGES);
45 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
46 	test_stats("MM Bank Init Error", &stats, &expected);
47 
48 	/* Now unmap all pages */
49 	for (index = 0; index < BANK_PAGES; index++) {
50 		sys_mm_drv_bank_page_unmapped(&bank_data);
51 	}
52 	sys_mm_drv_bank_stats_reset_max(&bank_data);
53 
54 	expected.free_bytes = EXPECTED(BANK_PAGES);
55 	expected.allocated_bytes = EXPECTED(0);
56 	expected.max_allocated_bytes = EXPECTED(0);
57 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
58 	test_stats("MM Bank Init Error", &stats, &expected);
59 
60 	/* Verify mapped pages are counted correctly */
61 	count = sys_mm_drv_bank_page_mapped(&bank_data);
62 	zassert_equal(count, 1,
63 		      "MM Page Mapped Error: 1st mapped = %u, not %u\n",
64 		      count, 1);
65 
66 	count = sys_mm_drv_bank_page_mapped(&bank_data);
67 	zassert_equal(count, 2,
68 		      "MM Page Mapped Error: 2nd mapped = %u, not %u\n",
69 		      count, 2);
70 
71 
72 	expected.free_bytes = EXPECTED(BANK_PAGES - 2);
73 	expected.allocated_bytes = EXPECTED(2);
74 	expected.max_allocated_bytes = EXPECTED(2);
75 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
76 	test_stats("MM Bank Mapped Error", &stats, &expected);
77 
78 	/* Verify unmapped pages are counted correctly */
79 
80 	count = sys_mm_drv_bank_page_unmapped(&bank_data);
81 	zassert_equal(count, BANK_PAGES - 1,
82 		      "MM Page Unmapped Error: Pages mapped = %u, not 1\n",
83 		      count, BANK_PAGES - 1);
84 	expected.free_bytes = EXPECTED(BANK_PAGES - 1);
85 	expected.allocated_bytes = EXPECTED(1);
86 	expected.max_allocated_bytes = EXPECTED(2);
87 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
88 	test_stats("MM Bank Unmapped Error", &stats, &expected);
89 
90 	/* Verify max mapped pages are counted correctly */
91 
92 	count = sys_mm_drv_bank_page_mapped(&bank_data);
93 	zassert_equal(count, 2,
94 		      "MM Page Mapped Error: 3rd mapped = %u, not %u\n",
95 		      count, 2);
96 	expected.free_bytes = EXPECTED(BANK_PAGES - 2);
97 	expected.allocated_bytes = EXPECTED(2);
98 	expected.max_allocated_bytes = EXPECTED(2);
99 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
100 	test_stats("MM Bank 1st Max Mapped Error", &stats, &expected);
101 
102 	count = sys_mm_drv_bank_page_mapped(&bank_data);
103 	zassert_equal(count, 3,
104 		      "MM Page Mapped Error: 3rd mapped = %u, not %u\n",
105 		      count, 3);
106 	expected.free_bytes = EXPECTED(BANK_PAGES - 3);
107 	expected.allocated_bytes = EXPECTED(3);
108 	expected.max_allocated_bytes = EXPECTED(3);
109 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
110 	test_stats("MM Bank 2nd Max Mapped Error", &stats, &expected);
111 
112 	/* Verify sys_mm_drv_bank_stats_reset_max works correctly */
113 
114 	count = sys_mm_drv_bank_page_unmapped(&bank_data);
115 	zassert_equal(count, BANK_PAGES - 2,
116 		      "MM Bank Reset Max Error: unmapped = %u, not %u\n",
117 		      count, BANK_PAGES - 2);
118 	expected.free_bytes = EXPECTED(BANK_PAGES - 2);
119 	expected.allocated_bytes = EXPECTED(2);
120 	expected.max_allocated_bytes = EXPECTED(3);
121 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
122 	test_stats("MM Bank Reset Max Error", &stats, &expected);
123 
124 	sys_mm_drv_bank_stats_reset_max(&bank_data);
125 	expected.free_bytes = EXPECTED(BANK_PAGES - 2);
126 	expected.allocated_bytes = EXPECTED(2);
127 	expected.max_allocated_bytes = EXPECTED(2);
128 	sys_mm_drv_bank_stats_get(&bank_data, &stats);
129 	test_stats("MM Bank Reset Max Error", &stats, &expected);
130 }
131 
132 ZTEST_SUITE(sys_mm_bank_api, NULL, NULL, NULL, NULL, NULL);
133