1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _MEMBLOCK_TEST_H
3 #define _MEMBLOCK_TEST_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>
13 #define MEM_SIZE SZ_16K
17 /* No special request. */
19 /* Perform raw allocations (no zeroing of memory). */
26 * @_expected == @_seen
27 * If false, print failed test message (if running with --verbose) and then
30 #define ASSERT_EQ(_expected, _seen) do { \
31 if ((_expected) != (_seen)) \
33 assert((_expected) == (_seen)); \
39 * @_expected != @_seen
40 * If false, print failed test message (if running with --verbose) and then
43 #define ASSERT_NE(_expected, _seen) do { \
44 if ((_expected) == (_seen)) \
46 assert((_expected) != (_seen)); \
53 * If false, print failed test message (if running with --verbose) and then
56 #define ASSERT_LT(_expected, _seen) do { \
57 if ((_expected) >= (_seen)) \
59 assert((_expected) < (_seen)); \
65 * @_expected <= @_seen
66 * If false, print failed test message (if running with --verbose) and then
69 #define ASSERT_LE(_expected, _seen) do { \
70 if ((_expected) > (_seen)) \
72 assert((_expected) <= (_seen)); \
77 * Check that the first @_size bytes of @_seen are all equal to @_expected.
78 * If false, print failed test message (if running with --verbose) and then
81 #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \
82 for (int _i = 0; _i < (_size); _i++) { \
83 ASSERT_EQ(((char *)_seen)[_i], (_expected)); \
89 * Check that none of the first @_size bytes of @_seen are equal to @_expected.
90 * If false, print failed test message (if running with --verbose) and then
93 #define ASSERT_MEM_NE(_seen, _expected, _size) do { \
94 for (int _i = 0; _i < (_size); _i++) { \
95 ASSERT_NE(((char *)_seen)[_i], (_expected)); \
99 #define PREFIX_PUSH() prefix_push(__func__)
102 * Available memory registered with memblock needs to be valid for allocs
103 * test to run. This is a convenience wrapper for memory allocated in
104 * dummy_physical_memory_init() that is later registered with memblock
105 * in setup_memblock().
116 static inline phys_addr_t __maybe_unused region_end(struct memblock_region *rgn)
118 return rgn->base + rgn->size;
121 void reset_memblock_regions(void);
122 void reset_memblock_attributes(void);
123 void setup_memblock(void);
124 void setup_numa_memblock(const unsigned int node_fracs[]);
125 void dummy_physical_memory_init(void);
126 void dummy_physical_memory_cleanup(void);
127 void parse_args(int argc, char **argv);
129 void test_fail(void);
130 void test_pass(void);
131 void test_print(const char *fmt, ...);
132 void prefix_reset(void);
133 void prefix_push(const char *prefix);
134 void prefix_pop(void);
136 static inline void test_pass_pop(void)
142 static inline void run_top_down(int (*func)())
144 memblock_set_bottom_up(false);
145 prefix_push("top-down");
150 static inline void run_bottom_up(int (*func)())
152 memblock_set_bottom_up(true);
153 prefix_push("bottom-up");
158 static inline void assert_mem_content(void *mem, int size, int flags)
160 if (flags & TEST_F_RAW)
161 ASSERT_MEM_NE(mem, 0, size);
163 ASSERT_MEM_EQ(mem, 0, size);