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_32K
16 #define INIT_MEMBLOCK_REGIONS 128
17 #define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
20 /* No special request. */
22 /* Perform raw allocations (no zeroing of memory). */
24 /* Perform allocations on the exact node specified. */
31 * @_expected == @_seen
32 * If false, print failed test message (if running with --verbose) and then
35 #define ASSERT_EQ(_expected, _seen) do { \
36 if ((_expected) != (_seen)) \
38 assert((_expected) == (_seen)); \
44 * @_expected != @_seen
45 * If false, print failed test message (if running with --verbose) and then
48 #define ASSERT_NE(_expected, _seen) do { \
49 if ((_expected) == (_seen)) \
51 assert((_expected) != (_seen)); \
58 * If false, print failed test message (if running with --verbose) and then
61 #define ASSERT_LT(_expected, _seen) do { \
62 if ((_expected) >= (_seen)) \
64 assert((_expected) < (_seen)); \
70 * @_expected <= @_seen
71 * If false, print failed test message (if running with --verbose) and then
74 #define ASSERT_LE(_expected, _seen) do { \
75 if ((_expected) > (_seen)) \
77 assert((_expected) <= (_seen)); \
82 * Check that the first @_size bytes of @_seen are all equal to @_expected.
83 * If false, print failed test message (if running with --verbose) and then
86 #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \
87 for (int _i = 0; _i < (_size); _i++) { \
88 ASSERT_EQ(((char *)_seen)[_i], (_expected)); \
94 * Check that none of the first @_size bytes of @_seen are equal to @_expected.
95 * If false, print failed test message (if running with --verbose) and then
98 #define ASSERT_MEM_NE(_seen, _expected, _size) do { \
99 for (int _i = 0; _i < (_size); _i++) { \
100 ASSERT_NE(((char *)_seen)[_i], (_expected)); \
104 #define PREFIX_PUSH() prefix_push(__func__)
107 * Available memory registered with memblock needs to be valid for allocs
108 * test to run. This is a convenience wrapper for memory allocated in
109 * dummy_physical_memory_init() that is later registered with memblock
110 * in setup_memblock().
121 static inline phys_addr_t __maybe_unused
region_end(struct memblock_region
*rgn
)
123 return rgn
->base
+ rgn
->size
;
126 void reset_memblock_regions(void);
127 void reset_memblock_attributes(void);
128 void setup_memblock(void);
129 void setup_numa_memblock(const unsigned int node_fracs
[]);
130 void dummy_physical_memory_init(void);
131 void dummy_physical_memory_cleanup(void);
132 phys_addr_t
dummy_physical_memory_base(void);
133 void parse_args(int argc
, char **argv
);
135 void test_fail(void);
136 void test_pass(void);
137 void test_print(const char *fmt
, ...);
138 void prefix_reset(void);
139 void prefix_push(const char *prefix
);
140 void prefix_pop(void);
142 static inline void test_pass_pop(void)
148 static inline void run_top_down(int (*func
)())
150 memblock_set_bottom_up(false);
151 prefix_push("top-down");
156 static inline void run_bottom_up(int (*func
)())
158 memblock_set_bottom_up(true);
159 prefix_push("bottom-up");
164 static inline void assert_mem_content(void *mem
, int size
, int flags
)
166 if (flags
& TEST_F_RAW
)
167 ASSERT_MEM_NE(mem
, 0, size
);
169 ASSERT_MEM_EQ(mem
, 0, size
);