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/seq_file.h> 9 #include <linux/memblock.h> 10 #include <linux/sizes.h> 11 #include <linux/printk.h> 12 #include <../selftests/kselftest.h> 13 14 #define MEM_SIZE SZ_32K 15 #define PHYS_MEM_SIZE SZ_16M 16 #define NUMA_NODES 8 17 18 #define INIT_MEMBLOCK_REGIONS 128 19 #define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS 20 21 enum test_flags { 22 /* No special request. */ 23 TEST_F_NONE = 0x0, 24 /* Perform raw allocations (no zeroing of memory). */ 25 TEST_F_RAW = 0x1, 26 /* Perform allocations on the exact node specified. */ 27 TEST_F_EXACT = 0x2 28 }; 29 30 /** 31 * ASSERT_EQ(): 32 * Check the condition 33 * @_expected == @_seen 34 * If false, print failed test message (if running with --verbose) and then 35 * assert. 36 */ 37 #define ASSERT_EQ(_expected, _seen) do { \ 38 if ((_expected) != (_seen)) \ 39 test_fail(); \ 40 assert((_expected) == (_seen)); \ 41 } while (0) 42 43 /** 44 * ASSERT_NE(): 45 * Check the condition 46 * @_expected != @_seen 47 * If false, print failed test message (if running with --verbose) and then 48 * assert. 49 */ 50 #define ASSERT_NE(_expected, _seen) do { \ 51 if ((_expected) == (_seen)) \ 52 test_fail(); \ 53 assert((_expected) != (_seen)); \ 54 } while (0) 55 56 /** 57 * ASSERT_LT(): 58 * Check the condition 59 * @_expected < @_seen 60 * If false, print failed test message (if running with --verbose) and then 61 * assert. 62 */ 63 #define ASSERT_LT(_expected, _seen) do { \ 64 if ((_expected) >= (_seen)) \ 65 test_fail(); \ 66 assert((_expected) < (_seen)); \ 67 } while (0) 68 69 /** 70 * ASSERT_LE(): 71 * Check the condition 72 * @_expected <= @_seen 73 * If false, print failed test message (if running with --verbose) and then 74 * assert. 75 */ 76 #define ASSERT_LE(_expected, _seen) do { \ 77 if ((_expected) > (_seen)) \ 78 test_fail(); \ 79 assert((_expected) <= (_seen)); \ 80 } while (0) 81 82 /** 83 * ASSERT_MEM_EQ(): 84 * Check that the first @_size bytes of @_seen are all equal to @_expected. 85 * If false, print failed test message (if running with --verbose) and then 86 * assert. 87 */ 88 #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \ 89 for (int _i = 0; _i < (_size); _i++) { \ 90 ASSERT_EQ(((char *)_seen)[_i], (_expected)); \ 91 } \ 92 } while (0) 93 94 /** 95 * ASSERT_MEM_NE(): 96 * Check that none of the first @_size bytes of @_seen are equal to @_expected. 97 * If false, print failed test message (if running with --verbose) and then 98 * assert. 99 */ 100 #define ASSERT_MEM_NE(_seen, _expected, _size) do { \ 101 for (int _i = 0; _i < (_size); _i++) { \ 102 ASSERT_NE(((char *)_seen)[_i], (_expected)); \ 103 } \ 104 } while (0) 105 106 #define PREFIX_PUSH() prefix_push(__func__) 107 108 /* 109 * Available memory registered with memblock needs to be valid for allocs 110 * test to run. This is a convenience wrapper for memory allocated in 111 * dummy_physical_memory_init() that is later registered with memblock 112 * in setup_memblock(). 113 */ 114 struct test_memory { 115 void *base; 116 }; 117 118 struct region { 119 phys_addr_t base; 120 phys_addr_t size; 121 }; 122 123 static inline phys_addr_t __maybe_unused region_end(struct memblock_region *rgn) 124 { 125 return rgn->base + rgn->size; 126 } 127 128 void reset_memblock_regions(void); 129 void reset_memblock_attributes(void); 130 void setup_memblock(void); 131 void setup_numa_memblock(const unsigned int node_fracs[]); 132 void dummy_physical_memory_init(void); 133 void dummy_physical_memory_cleanup(void); 134 phys_addr_t dummy_physical_memory_base(void); 135 void parse_args(int argc, char **argv); 136 137 void test_fail(void); 138 void test_pass(void); 139 void test_print(const char *fmt, ...); 140 void prefix_reset(void); 141 void prefix_push(const char *prefix); 142 void prefix_pop(void); 143 144 static inline void test_pass_pop(void) 145 { 146 test_pass(); 147 prefix_pop(); 148 } 149 150 static inline void run_top_down(int (*func)()) 151 { 152 memblock_set_bottom_up(false); 153 prefix_push("top-down"); 154 func(); 155 prefix_pop(); 156 } 157 158 static inline void run_bottom_up(int (*func)()) 159 { 160 memblock_set_bottom_up(true); 161 prefix_push("bottom-up"); 162 func(); 163 prefix_pop(); 164 } 165 166 static inline void assert_mem_content(void *mem, int size, int flags) 167 { 168 if (flags & TEST_F_RAW) 169 ASSERT_MEM_NE(mem, 0, size); 170 else 171 ASSERT_MEM_EQ(mem, 0, size); 172 } 173 174 #endif 175