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/memblock.h> 9 #include <linux/sizes.h> 10 #include <linux/printk.h> 11 #include <../selftests/kselftest.h> 12 13 #define MEM_SIZE SZ_16K 14 #define NUMA_NODES 8 15 16 enum test_flags { 17 /* No special request. */ 18 TEST_F_NONE = 0x0, 19 /* Perform raw allocations (no zeroing of memory). */ 20 TEST_F_RAW = 0x1, 21 }; 22 23 /** 24 * ASSERT_EQ(): 25 * Check the condition 26 * @_expected == @_seen 27 * If false, print failed test message (if running with --verbose) and then 28 * assert. 29 */ 30 #define ASSERT_EQ(_expected, _seen) do { \ 31 if ((_expected) != (_seen)) \ 32 test_fail(); \ 33 assert((_expected) == (_seen)); \ 34 } while (0) 35 36 /** 37 * ASSERT_NE(): 38 * Check the condition 39 * @_expected != @_seen 40 * If false, print failed test message (if running with --verbose) and then 41 * assert. 42 */ 43 #define ASSERT_NE(_expected, _seen) do { \ 44 if ((_expected) == (_seen)) \ 45 test_fail(); \ 46 assert((_expected) != (_seen)); \ 47 } while (0) 48 49 /** 50 * ASSERT_LT(): 51 * Check the condition 52 * @_expected < @_seen 53 * If false, print failed test message (if running with --verbose) and then 54 * assert. 55 */ 56 #define ASSERT_LT(_expected, _seen) do { \ 57 if ((_expected) >= (_seen)) \ 58 test_fail(); \ 59 assert((_expected) < (_seen)); \ 60 } while (0) 61 62 /** 63 * ASSERT_LE(): 64 * Check the condition 65 * @_expected <= @_seen 66 * If false, print failed test message (if running with --verbose) and then 67 * assert. 68 */ 69 #define ASSERT_LE(_expected, _seen) do { \ 70 if ((_expected) > (_seen)) \ 71 test_fail(); \ 72 assert((_expected) <= (_seen)); \ 73 } while (0) 74 75 /** 76 * ASSERT_MEM_EQ(): 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 79 * assert. 80 */ 81 #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \ 82 for (int _i = 0; _i < (_size); _i++) { \ 83 ASSERT_EQ(((char *)_seen)[_i], (_expected)); \ 84 } \ 85 } while (0) 86 87 /** 88 * ASSERT_MEM_NE(): 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 91 * assert. 92 */ 93 #define ASSERT_MEM_NE(_seen, _expected, _size) do { \ 94 for (int _i = 0; _i < (_size); _i++) { \ 95 ASSERT_NE(((char *)_seen)[_i], (_expected)); \ 96 } \ 97 } while (0) 98 99 #define PREFIX_PUSH() prefix_push(__func__) 100 101 /* 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(). 106 */ 107 struct test_memory { 108 void *base; 109 }; 110 111 struct region { 112 phys_addr_t base; 113 phys_addr_t size; 114 }; 115 116 static inline phys_addr_t __maybe_unused region_end(struct memblock_region *rgn) 117 { 118 return rgn->base + rgn->size; 119 } 120 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); 128 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); 135 136 static inline void test_pass_pop(void) 137 { 138 test_pass(); 139 prefix_pop(); 140 } 141 142 static inline void run_top_down(int (*func)()) 143 { 144 memblock_set_bottom_up(false); 145 prefix_push("top-down"); 146 func(); 147 prefix_pop(); 148 } 149 150 static inline void run_bottom_up(int (*func)()) 151 { 152 memblock_set_bottom_up(true); 153 prefix_push("bottom-up"); 154 func(); 155 prefix_pop(); 156 } 157 158 static inline void assert_mem_content(void *mem, int size, int flags) 159 { 160 if (flags & TEST_F_RAW) 161 ASSERT_MEM_NE(mem, 0, size); 162 else 163 ASSERT_MEM_EQ(mem, 0, size); 164 } 165 166 #endif 167