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