xref: /linux/tools/testing/memblock/tests/common.h (revision fba3e86fb848f79c4573243affcd7d79d378958d)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _MEMBLOCK_TEST_H
3 #define _MEMBLOCK_TEST_H
4 
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <linux/types.h>
8 #include <linux/seq_file.h>
9 #include <linux/memblock.h>
10 #include <linux/sizes.h>
11 #include <linux/printk.h>
12 #include <../selftests/kselftest.h>
13 
14 #define MEM_SIZE		SZ_32K
15 #define PHYS_MEM_SIZE		SZ_16M
16 #define NUMA_NODES		8
17 
18 #define INIT_MEMBLOCK_REGIONS			128
19 #define INIT_MEMBLOCK_RESERVED_REGIONS		INIT_MEMBLOCK_REGIONS
20 
21 enum test_flags {
22 	/* No special request. */
23 	TEST_F_NONE = 0x0,
24 	/* Perform raw allocations (no zeroing of memory). */
25 	TEST_F_RAW = 0x1,
26 };
27 
28 /**
29  * ASSERT_EQ():
30  * Check the condition
31  * @_expected == @_seen
32  * If false, print failed test message (if running with --verbose) and then
33  * assert.
34  */
35 #define ASSERT_EQ(_expected, _seen) do { \
36 	if ((_expected) != (_seen)) \
37 		test_fail(); \
38 	assert((_expected) == (_seen)); \
39 } while (0)
40 
41 #define ASSERT_TRUE(_seen) ASSERT_EQ(true, _seen)
42 #define ASSERT_FALSE(_seen) ASSERT_EQ(false, _seen)
43 
44 /**
45  * ASSERT_NE():
46  * Check the condition
47  * @_expected != @_seen
48  * If false, print failed test message (if running with --verbose) and then
49  * assert.
50  */
51 #define ASSERT_NE(_expected, _seen) do { \
52 	if ((_expected) == (_seen)) \
53 		test_fail(); \
54 	assert((_expected) != (_seen)); \
55 } while (0)
56 
57 /**
58  * ASSERT_LT():
59  * Check the condition
60  * @_expected < @_seen
61  * If false, print failed test message (if running with --verbose) and then
62  * assert.
63  */
64 #define ASSERT_LT(_expected, _seen) do { \
65 	if ((_expected) >= (_seen)) \
66 		test_fail(); \
67 	assert((_expected) < (_seen)); \
68 } while (0)
69 
70 /**
71  * ASSERT_LE():
72  * Check the condition
73  * @_expected <= @_seen
74  * If false, print failed test message (if running with --verbose) and then
75  * assert.
76  */
77 #define ASSERT_LE(_expected, _seen) do { \
78 	if ((_expected) > (_seen)) \
79 		test_fail(); \
80 	assert((_expected) <= (_seen)); \
81 } while (0)
82 
83 /**
84  * ASSERT_MEM_EQ():
85  * Check that the first @_size bytes of @_seen are all equal to @_expected.
86  * If false, print failed test message (if running with --verbose) and then
87  * assert.
88  */
89 #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \
90 	for (int _i = 0; _i < (_size); _i++) { \
91 		ASSERT_EQ(((char *)_seen)[_i], (_expected)); \
92 	} \
93 } while (0)
94 
95 /**
96  * ASSERT_MEM_NE():
97  * Check that none of the first @_size bytes of @_seen are equal to @_expected.
98  * If false, print failed test message (if running with --verbose) and then
99  * assert.
100  */
101 #define ASSERT_MEM_NE(_seen, _expected, _size) do { \
102 	for (int _i = 0; _i < (_size); _i++) { \
103 		ASSERT_NE(((char *)_seen)[_i], (_expected)); \
104 	} \
105 } while (0)
106 
107 #define PREFIX_PUSH() prefix_push(__func__)
108 
109 /*
110  * Available memory registered with memblock needs to be valid for allocs
111  * test to run. This is a convenience wrapper for memory allocated in
112  * dummy_physical_memory_init() that is later registered with memblock
113  * in setup_memblock().
114  */
115 struct test_memory {
116 	void *base;
117 };
118 
119 struct region {
120 	phys_addr_t base;
121 	phys_addr_t size;
122 };
123 
124 static inline phys_addr_t __maybe_unused region_end(struct memblock_region *rgn)
125 {
126 	return rgn->base + rgn->size;
127 }
128 
129 void reset_memblock_regions(void);
130 void reset_memblock_attributes(void);
131 void setup_memblock(void);
132 void setup_numa_memblock(const unsigned int node_fracs[]);
133 void dummy_physical_memory_init(void);
134 void dummy_physical_memory_cleanup(void);
135 phys_addr_t dummy_physical_memory_base(void);
136 void parse_args(int argc, char **argv);
137 
138 void test_fail(void);
139 void test_pass(void);
140 void test_print(const char *fmt, ...);
141 void prefix_reset(void);
142 void prefix_push(const char *prefix);
143 void prefix_pop(void);
144 
145 static inline void test_pass_pop(void)
146 {
147 	test_pass();
148 	prefix_pop();
149 }
150 
151 static inline void run_top_down(int (*func)())
152 {
153 	memblock_set_bottom_up(false);
154 	prefix_push("top-down");
155 	func();
156 	prefix_pop();
157 }
158 
159 static inline void run_bottom_up(int (*func)())
160 {
161 	memblock_set_bottom_up(true);
162 	prefix_push("bottom-up");
163 	func();
164 	prefix_pop();
165 }
166 
167 static inline void assert_mem_content(void *mem, int size, int flags)
168 {
169 	if (flags & TEST_F_RAW)
170 		ASSERT_MEM_NE(mem, 0, size);
171 	else
172 		ASSERT_MEM_EQ(mem, 0, size);
173 }
174 
175 #endif
176