1c348c163SJesse Brandeburg // SPDX-License-Identifier: GPL-2.0-only
2c348c163SJesse Brandeburg /*
3c348c163SJesse Brandeburg * Copyright (C) 2020 Intel Corporation
4c348c163SJesse Brandeburg */
5c348c163SJesse Brandeburg
6c348c163SJesse Brandeburg #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7c348c163SJesse Brandeburg
8*0a2c6664SKuan-Wei Chiu #include <linux/cleanup.h>
9c348c163SJesse Brandeburg #include <linux/init.h>
10c348c163SJesse Brandeburg #include <linux/module.h>
11c348c163SJesse Brandeburg #include <linux/printk.h>
12*0a2c6664SKuan-Wei Chiu #include <linux/slab.h>
13c348c163SJesse Brandeburg
146af132f3SWei Yang /* a tiny module only meant to test
156af132f3SWei Yang *
166af132f3SWei Yang * set/clear_bit
176af132f3SWei Yang * get_count_order/long
186af132f3SWei Yang */
19c348c163SJesse Brandeburg
2053b0fe36SZhen Lei /* use an enum because that's the most common BITMAP usage */
21c348c163SJesse Brandeburg enum bitops_fun {
22c348c163SJesse Brandeburg BITOPS_4 = 4,
23c348c163SJesse Brandeburg BITOPS_7 = 7,
24c348c163SJesse Brandeburg BITOPS_11 = 11,
25c348c163SJesse Brandeburg BITOPS_31 = 31,
26c348c163SJesse Brandeburg BITOPS_88 = 88,
27c348c163SJesse Brandeburg BITOPS_LAST = 255,
28c348c163SJesse Brandeburg BITOPS_LENGTH = 256
29c348c163SJesse Brandeburg };
30c348c163SJesse Brandeburg
31c348c163SJesse Brandeburg static DECLARE_BITMAP(g_bitmap, BITOPS_LENGTH);
32c348c163SJesse Brandeburg
336af132f3SWei Yang static unsigned int order_comb[][2] = {
346af132f3SWei Yang {0x00000003, 2},
356af132f3SWei Yang {0x00000004, 2},
366af132f3SWei Yang {0x00001fff, 13},
376af132f3SWei Yang {0x00002000, 13},
386af132f3SWei Yang {0x50000000, 31},
396af132f3SWei Yang {0x80000000, 31},
406af132f3SWei Yang {0x80003000, 32},
416af132f3SWei Yang };
426af132f3SWei Yang
436af132f3SWei Yang #ifdef CONFIG_64BIT
446af132f3SWei Yang static unsigned long order_comb_long[][2] = {
456af132f3SWei Yang {0x0000000300000000, 34},
466af132f3SWei Yang {0x0000000400000000, 34},
476af132f3SWei Yang {0x00001fff00000000, 45},
486af132f3SWei Yang {0x0000200000000000, 45},
496af132f3SWei Yang {0x5000000000000000, 63},
506af132f3SWei Yang {0x8000000000000000, 63},
516af132f3SWei Yang {0x8000300000000000, 64},
526af132f3SWei Yang };
536af132f3SWei Yang #endif
546af132f3SWei Yang
test_fns(void)55*0a2c6664SKuan-Wei Chiu static int __init test_fns(void)
56*0a2c6664SKuan-Wei Chiu {
57*0a2c6664SKuan-Wei Chiu static volatile __always_used unsigned long tmp __initdata;
58*0a2c6664SKuan-Wei Chiu unsigned long *buf __free(kfree) = NULL;
59*0a2c6664SKuan-Wei Chiu unsigned int i, n;
60*0a2c6664SKuan-Wei Chiu ktime_t time;
61*0a2c6664SKuan-Wei Chiu
62*0a2c6664SKuan-Wei Chiu buf = kmalloc_array(10000, sizeof(unsigned long), GFP_KERNEL);
63*0a2c6664SKuan-Wei Chiu if (!buf)
64*0a2c6664SKuan-Wei Chiu return -ENOMEM;
65*0a2c6664SKuan-Wei Chiu
66*0a2c6664SKuan-Wei Chiu get_random_bytes(buf, 10000 * sizeof(unsigned long));
67*0a2c6664SKuan-Wei Chiu time = ktime_get();
68*0a2c6664SKuan-Wei Chiu
69*0a2c6664SKuan-Wei Chiu for (n = 0; n < BITS_PER_LONG; n++)
70*0a2c6664SKuan-Wei Chiu for (i = 0; i < 10000; i++)
71*0a2c6664SKuan-Wei Chiu tmp = fns(buf[i], n);
72*0a2c6664SKuan-Wei Chiu
73*0a2c6664SKuan-Wei Chiu time = ktime_get() - time;
74*0a2c6664SKuan-Wei Chiu pr_err("fns: %18llu ns\n", time);
75*0a2c6664SKuan-Wei Chiu
76*0a2c6664SKuan-Wei Chiu return 0;
77*0a2c6664SKuan-Wei Chiu }
78*0a2c6664SKuan-Wei Chiu
test_bitops_startup(void)79c348c163SJesse Brandeburg static int __init test_bitops_startup(void)
80c348c163SJesse Brandeburg {
81403f1773SGeert Uytterhoeven int i, bit_set;
826af132f3SWei Yang
83403f1773SGeert Uytterhoeven pr_info("Starting bitops test\n");
84c348c163SJesse Brandeburg set_bit(BITOPS_4, g_bitmap);
85c348c163SJesse Brandeburg set_bit(BITOPS_7, g_bitmap);
86c348c163SJesse Brandeburg set_bit(BITOPS_11, g_bitmap);
87c348c163SJesse Brandeburg set_bit(BITOPS_31, g_bitmap);
88c348c163SJesse Brandeburg set_bit(BITOPS_88, g_bitmap);
896af132f3SWei Yang
906af132f3SWei Yang for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
916af132f3SWei Yang if (order_comb[i][1] != get_count_order(order_comb[i][0]))
926af132f3SWei Yang pr_warn("get_count_order wrong for %x\n",
936af132f3SWei Yang order_comb[i][0]);
946af132f3SWei Yang }
956af132f3SWei Yang
966af132f3SWei Yang for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
976af132f3SWei Yang if (order_comb[i][1] != get_count_order_long(order_comb[i][0]))
986af132f3SWei Yang pr_warn("get_count_order_long wrong for %x\n",
996af132f3SWei Yang order_comb[i][0]);
1006af132f3SWei Yang }
1016af132f3SWei Yang
1026af132f3SWei Yang #ifdef CONFIG_64BIT
1036af132f3SWei Yang for (i = 0; i < ARRAY_SIZE(order_comb_long); i++) {
1046af132f3SWei Yang if (order_comb_long[i][1] !=
1056af132f3SWei Yang get_count_order_long(order_comb_long[i][0]))
1066af132f3SWei Yang pr_warn("get_count_order_long wrong for %lx\n",
1076af132f3SWei Yang order_comb_long[i][0]);
1086af132f3SWei Yang }
1096af132f3SWei Yang #endif
110c348c163SJesse Brandeburg
111403f1773SGeert Uytterhoeven barrier();
112c348c163SJesse Brandeburg
113c348c163SJesse Brandeburg clear_bit(BITOPS_4, g_bitmap);
114c348c163SJesse Brandeburg clear_bit(BITOPS_7, g_bitmap);
115c348c163SJesse Brandeburg clear_bit(BITOPS_11, g_bitmap);
116c348c163SJesse Brandeburg clear_bit(BITOPS_31, g_bitmap);
117c348c163SJesse Brandeburg clear_bit(BITOPS_88, g_bitmap);
118c348c163SJesse Brandeburg
119c348c163SJesse Brandeburg bit_set = find_first_bit(g_bitmap, BITOPS_LAST);
120c348c163SJesse Brandeburg if (bit_set != BITOPS_LAST)
121c348c163SJesse Brandeburg pr_err("ERROR: FOUND SET BIT %d\n", bit_set);
122c348c163SJesse Brandeburg
123*0a2c6664SKuan-Wei Chiu test_fns();
124*0a2c6664SKuan-Wei Chiu
125403f1773SGeert Uytterhoeven pr_info("Completed bitops test\n");
126403f1773SGeert Uytterhoeven
127403f1773SGeert Uytterhoeven return 0;
128403f1773SGeert Uytterhoeven }
129403f1773SGeert Uytterhoeven
test_bitops_unstartup(void)130403f1773SGeert Uytterhoeven static void __exit test_bitops_unstartup(void)
131403f1773SGeert Uytterhoeven {
132c348c163SJesse Brandeburg }
133c348c163SJesse Brandeburg
134c348c163SJesse Brandeburg module_init(test_bitops_startup);
135c348c163SJesse Brandeburg module_exit(test_bitops_unstartup);
136c348c163SJesse Brandeburg
1376af132f3SWei Yang MODULE_AUTHOR("Jesse Brandeburg <[email protected]>, Wei Yang <[email protected]>");
138c348c163SJesse Brandeburg MODULE_LICENSE("GPL");
139c348c163SJesse Brandeburg MODULE_DESCRIPTION("Bit testing module");
140