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