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
15 #define PHYS_MEM_SIZE SZ_16M
18 #define INIT_MEMBLOCK_REGIONS 128
19 #define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
22 /* No special request. */
24 /* Perform raw allocations (no zeroing of memory). */
26 /* Perform allocations on the exact node specified. */
33 * @_expected == @_seen
34 * If false, print failed test message (if running with --verbose) and then
37 #define ASSERT_EQ(_expected, _seen) do { \
38 if ((_expected) != (_seen)) \
40 assert((_expected) == (_seen)); \
43 #define ASSERT_TRUE(_seen) ASSERT_EQ(true, _seen)
44 #define ASSERT_FALSE(_seen) ASSERT_EQ(false, _seen)
49 * @_expected != @_seen
50 * If false, print failed test message (if running with --verbose) and then
53 #define ASSERT_NE(_expected, _seen) do { \
54 if ((_expected) == (_seen)) \
56 assert((_expected) != (_seen)); \
63 * If false, print failed test message (if running with --verbose) and then
66 #define ASSERT_LT(_expected, _seen) do { \
67 if ((_expected) >= (_seen)) \
69 assert((_expected) < (_seen)); \
75 * @_expected <= @_seen
76 * If false, print failed test message (if running with --verbose) and then
79 #define ASSERT_LE(_expected, _seen) do { \
80 if ((_expected) > (_seen)) \
82 assert((_expected) <= (_seen)); \
87 * Check that the first @_size bytes of @_seen are all equal to @_expected.
88 * If false, print failed test message (if running with --verbose) and then
91 #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \
92 for (int _i = 0; _i < (_size); _i++) { \
93 ASSERT_EQ(((char *)_seen)[_i], (_expected)); \
99 * Check that none of the first @_size bytes of @_seen are equal to @_expected.
100 * If false, print failed test message (if running with --verbose) and then
103 #define ASSERT_MEM_NE(_seen, _expected, _size) do { \
104 for (int _i = 0; _i < (_size); _i++) { \
105 ASSERT_NE(((char *)_seen)[_i], (_expected)); \
109 #define PREFIX_PUSH() prefix_push(__func__)
112 * Available memory registered with memblock needs to be valid for allocs
113 * test to run. This is a convenience wrapper for memory allocated in
114 * dummy_physical_memory_init() that is later registered with memblock
115 * in setup_memblock().
126 static inline phys_addr_t __maybe_unused
region_end(struct memblock_region
*rgn
)
128 return rgn
->base
+ rgn
->size
;
131 void reset_memblock_regions(void);
132 void reset_memblock_attributes(void);
133 void setup_memblock(void);
134 void setup_numa_memblock(const unsigned int node_fracs
[]);
135 void dummy_physical_memory_init(void);
136 void dummy_physical_memory_cleanup(void);
137 phys_addr_t
dummy_physical_memory_base(void);
138 void parse_args(int argc
, char **argv
);
140 void test_fail(void);
141 void test_pass(void);
142 void test_print(const char *fmt
, ...);
143 void prefix_reset(void);
144 void prefix_push(const char *prefix
);
145 void prefix_pop(void);
147 static inline void test_pass_pop(void)
153 static inline void run_top_down(int (*func
)())
155 memblock_set_bottom_up(false);
156 prefix_push("top-down");
161 static inline void run_bottom_up(int (*func
)())
163 memblock_set_bottom_up(true);
164 prefix_push("bottom-up");
169 static inline void assert_mem_content(void *mem
, int size
, int flags
)
171 if (flags
& TEST_F_RAW
)
172 ASSERT_MEM_NE(mem
, 0, size
);
174 ASSERT_MEM_EQ(mem
, 0, size
);