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/seq_file.h>
9 #include <linux/memblock.h>
10 #include <linux/sizes.h>
11 #include <linux/printk.h>
12 #include <../selftests/kselftest.h>
14 #define MEM_SIZE SZ_32K
17 #define INIT_MEMBLOCK_REGIONS 128
18 #define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
21 /* No special request. */
23 /* Perform raw allocations (no zeroing of memory). */
25 /* Perform allocations on the exact node specified. */
32 * @_expected == @_seen
33 * If false, print failed test message (if running with --verbose) and then
36 #define ASSERT_EQ(_expected, _seen) do { \
37 if ((_expected) != (_seen)) \
39 assert((_expected) == (_seen)); \
45 * @_expected != @_seen
46 * If false, print failed test message (if running with --verbose) and then
49 #define ASSERT_NE(_expected, _seen) do { \
50 if ((_expected) == (_seen)) \
52 assert((_expected) != (_seen)); \
59 * If false, print failed test message (if running with --verbose) and then
62 #define ASSERT_LT(_expected, _seen) do { \
63 if ((_expected) >= (_seen)) \
65 assert((_expected) < (_seen)); \
71 * @_expected <= @_seen
72 * If false, print failed test message (if running with --verbose) and then
75 #define ASSERT_LE(_expected, _seen) do { \
76 if ((_expected) > (_seen)) \
78 assert((_expected) <= (_seen)); \
83 * Check that the first @_size bytes of @_seen are all equal to @_expected.
84 * If false, print failed test message (if running with --verbose) and then
87 #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \
88 for (int _i = 0; _i < (_size); _i++) { \
89 ASSERT_EQ(((char *)_seen)[_i], (_expected)); \
95 * Check that none of the first @_size bytes of @_seen are equal to @_expected.
96 * If false, print failed test message (if running with --verbose) and then
99 #define ASSERT_MEM_NE(_seen, _expected, _size) do { \
100 for (int _i = 0; _i < (_size); _i++) { \
101 ASSERT_NE(((char *)_seen)[_i], (_expected)); \
105 #define PREFIX_PUSH() prefix_push(__func__)
108 * Available memory registered with memblock needs to be valid for allocs
109 * test to run. This is a convenience wrapper for memory allocated in
110 * dummy_physical_memory_init() that is later registered with memblock
111 * in setup_memblock().
122 static inline phys_addr_t __maybe_unused region_end(struct memblock_region *rgn)
124 return rgn->base + rgn->size;
127 void reset_memblock_regions(void);
128 void reset_memblock_attributes(void);
129 void setup_memblock(void);
130 void setup_numa_memblock(const unsigned int node_fracs[]);
131 void dummy_physical_memory_init(void);
132 void dummy_physical_memory_cleanup(void);
133 phys_addr_t dummy_physical_memory_base(void);
134 void parse_args(int argc, char **argv);
136 void test_fail(void);
137 void test_pass(void);
138 void test_print(const char *fmt, ...);
139 void prefix_reset(void);
140 void prefix_push(const char *prefix);
141 void prefix_pop(void);
143 static inline void test_pass_pop(void)
149 static inline void run_top_down(int (*func)())
151 memblock_set_bottom_up(false);
152 prefix_push("top-down");
157 static inline void run_bottom_up(int (*func)())
159 memblock_set_bottom_up(true);
160 prefix_push("bottom-up");
165 static inline void assert_mem_content(void *mem, int size, int flags)
167 if (flags & TEST_F_RAW)
168 ASSERT_MEM_NE(mem, 0, size);
170 ASSERT_MEM_EQ(mem, 0, size);