test_bitops.c 2.5 KB

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