blob: 471141ddd69137246aa2aefec271eb2ef70c072f [file] [log] [blame]
Jesse Brandeburgc348c162020-06-04 16:50:27 -07001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2020 Intel Corporation
4 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/printk.h>
11
Wei Yang6af132f2020-06-10 18:41:53 -070012/* a tiny module only meant to test
13 *
14 * set/clear_bit
15 * get_count_order/long
16 */
Jesse Brandeburgc348c162020-06-04 16:50:27 -070017
18/* use an enum because thats the most common BITMAP usage */
19enum bitops_fun {
20 BITOPS_4 = 4,
21 BITOPS_7 = 7,
22 BITOPS_11 = 11,
23 BITOPS_31 = 31,
24 BITOPS_88 = 88,
25 BITOPS_LAST = 255,
26 BITOPS_LENGTH = 256
27};
28
29static DECLARE_BITMAP(g_bitmap, BITOPS_LENGTH);
30
Wei Yang6af132f2020-06-10 18:41:53 -070031static unsigned int order_comb[][2] = {
32 {0x00000003, 2},
33 {0x00000004, 2},
34 {0x00001fff, 13},
35 {0x00002000, 13},
36 {0x50000000, 31},
37 {0x80000000, 31},
38 {0x80003000, 32},
39};
40
41#ifdef CONFIG_64BIT
42static unsigned long order_comb_long[][2] = {
43 {0x0000000300000000, 34},
44 {0x0000000400000000, 34},
45 {0x00001fff00000000, 45},
46 {0x0000200000000000, 45},
47 {0x5000000000000000, 63},
48 {0x8000000000000000, 63},
49 {0x8000300000000000, 64},
50};
51#endif
52
Jesse Brandeburgc348c162020-06-04 16:50:27 -070053static int __init test_bitops_startup(void)
54{
Geert Uytterhoeven403f1772020-08-11 18:34:38 -070055 int i, bit_set;
Wei Yang6af132f2020-06-10 18:41:53 -070056
Geert Uytterhoeven403f1772020-08-11 18:34:38 -070057 pr_info("Starting bitops test\n");
Jesse Brandeburgc348c162020-06-04 16:50:27 -070058 set_bit(BITOPS_4, g_bitmap);
59 set_bit(BITOPS_7, g_bitmap);
60 set_bit(BITOPS_11, g_bitmap);
61 set_bit(BITOPS_31, g_bitmap);
62 set_bit(BITOPS_88, g_bitmap);
Wei Yang6af132f2020-06-10 18:41:53 -070063
64 for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
65 if (order_comb[i][1] != get_count_order(order_comb[i][0]))
66 pr_warn("get_count_order wrong for %x\n",
67 order_comb[i][0]);
68 }
69
70 for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
71 if (order_comb[i][1] != get_count_order_long(order_comb[i][0]))
72 pr_warn("get_count_order_long wrong for %x\n",
73 order_comb[i][0]);
74 }
75
76#ifdef CONFIG_64BIT
77 for (i = 0; i < ARRAY_SIZE(order_comb_long); i++) {
78 if (order_comb_long[i][1] !=
79 get_count_order_long(order_comb_long[i][0]))
80 pr_warn("get_count_order_long wrong for %lx\n",
81 order_comb_long[i][0]);
82 }
83#endif
Jesse Brandeburgc348c162020-06-04 16:50:27 -070084
Geert Uytterhoeven403f1772020-08-11 18:34:38 -070085 barrier();
Jesse Brandeburgc348c162020-06-04 16:50:27 -070086
87 clear_bit(BITOPS_4, g_bitmap);
88 clear_bit(BITOPS_7, g_bitmap);
89 clear_bit(BITOPS_11, g_bitmap);
90 clear_bit(BITOPS_31, g_bitmap);
91 clear_bit(BITOPS_88, g_bitmap);
92
93 bit_set = find_first_bit(g_bitmap, BITOPS_LAST);
94 if (bit_set != BITOPS_LAST)
95 pr_err("ERROR: FOUND SET BIT %d\n", bit_set);
96
Geert Uytterhoeven403f1772020-08-11 18:34:38 -070097 pr_info("Completed bitops test\n");
98
99 return 0;
100}
101
102static void __exit test_bitops_unstartup(void)
103{
Jesse Brandeburgc348c162020-06-04 16:50:27 -0700104}
105
106module_init(test_bitops_startup);
107module_exit(test_bitops_unstartup);
108
Wei Yang6af132f2020-06-10 18:41:53 -0700109MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>, Wei Yang <richard.weiyang@gmail.com>");
Jesse Brandeburgc348c162020-06-04 16:50:27 -0700110MODULE_LICENSE("GPL");
111MODULE_DESCRIPTION("Bit testing module");