2020-06-05 07:50:27 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Intel Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2024-05-02 17:24:42 +08:00
|
|
|
#include <linux/cleanup.h>
|
2020-06-05 07:50:27 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/printk.h>
|
2024-05-02 17:24:42 +08:00
|
|
|
#include <linux/slab.h>
|
2020-06-05 07:50:27 +08:00
|
|
|
|
2020-06-11 09:41:53 +08:00
|
|
|
/* a tiny module only meant to test
|
|
|
|
*
|
|
|
|
* set/clear_bit
|
|
|
|
* get_count_order/long
|
|
|
|
*/
|
2020-06-05 07:50:27 +08:00
|
|
|
|
2021-07-08 09:07:28 +08:00
|
|
|
/* use an enum because that's the most common BITMAP usage */
|
2020-06-05 07:50:27 +08:00
|
|
|
enum bitops_fun {
|
|
|
|
BITOPS_4 = 4,
|
|
|
|
BITOPS_7 = 7,
|
|
|
|
BITOPS_11 = 11,
|
|
|
|
BITOPS_31 = 31,
|
|
|
|
BITOPS_88 = 88,
|
|
|
|
BITOPS_LAST = 255,
|
|
|
|
BITOPS_LENGTH = 256
|
|
|
|
};
|
|
|
|
|
|
|
|
static DECLARE_BITMAP(g_bitmap, BITOPS_LENGTH);
|
|
|
|
|
2020-06-11 09:41:53 +08:00
|
|
|
static unsigned int order_comb[][2] = {
|
|
|
|
{0x00000003, 2},
|
|
|
|
{0x00000004, 2},
|
|
|
|
{0x00001fff, 13},
|
|
|
|
{0x00002000, 13},
|
|
|
|
{0x50000000, 31},
|
|
|
|
{0x80000000, 31},
|
|
|
|
{0x80003000, 32},
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_64BIT
|
|
|
|
static unsigned long order_comb_long[][2] = {
|
|
|
|
{0x0000000300000000, 34},
|
|
|
|
{0x0000000400000000, 34},
|
|
|
|
{0x00001fff00000000, 45},
|
|
|
|
{0x0000200000000000, 45},
|
|
|
|
{0x5000000000000000, 63},
|
|
|
|
{0x8000000000000000, 63},
|
|
|
|
{0x8000300000000000, 64},
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2024-05-02 17:24:42 +08:00
|
|
|
static int __init test_fns(void)
|
|
|
|
{
|
|
|
|
static volatile __always_used unsigned long tmp __initdata;
|
|
|
|
unsigned long *buf __free(kfree) = NULL;
|
|
|
|
unsigned int i, n;
|
|
|
|
ktime_t time;
|
|
|
|
|
|
|
|
buf = kmalloc_array(10000, sizeof(unsigned long), GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
get_random_bytes(buf, 10000 * sizeof(unsigned long));
|
|
|
|
time = ktime_get();
|
|
|
|
|
|
|
|
for (n = 0; n < BITS_PER_LONG; n++)
|
|
|
|
for (i = 0; i < 10000; i++)
|
|
|
|
tmp = fns(buf[i], n);
|
|
|
|
|
|
|
|
time = ktime_get() - time;
|
|
|
|
pr_err("fns: %18llu ns\n", time);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:50:27 +08:00
|
|
|
static int __init test_bitops_startup(void)
|
|
|
|
{
|
2020-08-12 09:34:38 +08:00
|
|
|
int i, bit_set;
|
2020-06-11 09:41:53 +08:00
|
|
|
|
2020-08-12 09:34:38 +08:00
|
|
|
pr_info("Starting bitops test\n");
|
2020-06-05 07:50:27 +08:00
|
|
|
set_bit(BITOPS_4, g_bitmap);
|
|
|
|
set_bit(BITOPS_7, g_bitmap);
|
|
|
|
set_bit(BITOPS_11, g_bitmap);
|
|
|
|
set_bit(BITOPS_31, g_bitmap);
|
|
|
|
set_bit(BITOPS_88, g_bitmap);
|
2020-06-11 09:41:53 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
|
|
|
|
if (order_comb[i][1] != get_count_order(order_comb[i][0]))
|
|
|
|
pr_warn("get_count_order wrong for %x\n",
|
|
|
|
order_comb[i][0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
|
|
|
|
if (order_comb[i][1] != get_count_order_long(order_comb[i][0]))
|
|
|
|
pr_warn("get_count_order_long wrong for %x\n",
|
|
|
|
order_comb[i][0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_64BIT
|
|
|
|
for (i = 0; i < ARRAY_SIZE(order_comb_long); i++) {
|
|
|
|
if (order_comb_long[i][1] !=
|
|
|
|
get_count_order_long(order_comb_long[i][0]))
|
|
|
|
pr_warn("get_count_order_long wrong for %lx\n",
|
|
|
|
order_comb_long[i][0]);
|
|
|
|
}
|
|
|
|
#endif
|
2020-06-05 07:50:27 +08:00
|
|
|
|
2020-08-12 09:34:38 +08:00
|
|
|
barrier();
|
2020-06-05 07:50:27 +08:00
|
|
|
|
|
|
|
clear_bit(BITOPS_4, g_bitmap);
|
|
|
|
clear_bit(BITOPS_7, g_bitmap);
|
|
|
|
clear_bit(BITOPS_11, g_bitmap);
|
|
|
|
clear_bit(BITOPS_31, g_bitmap);
|
|
|
|
clear_bit(BITOPS_88, g_bitmap);
|
|
|
|
|
|
|
|
bit_set = find_first_bit(g_bitmap, BITOPS_LAST);
|
|
|
|
if (bit_set != BITOPS_LAST)
|
|
|
|
pr_err("ERROR: FOUND SET BIT %d\n", bit_set);
|
|
|
|
|
2024-05-02 17:24:42 +08:00
|
|
|
test_fns();
|
|
|
|
|
2020-08-12 09:34:38 +08:00
|
|
|
pr_info("Completed bitops test\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit test_bitops_unstartup(void)
|
|
|
|
{
|
2020-06-05 07:50:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(test_bitops_startup);
|
|
|
|
module_exit(test_bitops_unstartup);
|
|
|
|
|
2020-06-11 09:41:53 +08:00
|
|
|
MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>, Wei Yang <richard.weiyang@gmail.com>");
|
2020-06-05 07:50:27 +08:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("Bit testing module");
|