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