sbi_hartmask.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #ifndef __SBI_HARTMASK_H__
  10. #define __SBI_HARTMASK_H__
  11. #include <sbi/sbi_bitmap.h>
  12. /**
  13. * Maximum number of bits in a hartmask
  14. *
  15. * The hartmask is indexed using physical HART id so this define
  16. * also represents the maximum number of HART ids generic OpenSBI
  17. * can handle.
  18. */
  19. #define SBI_HARTMASK_MAX_BITS 128
  20. /** Representation of hartmask */
  21. struct sbi_hartmask {
  22. DECLARE_BITMAP(bits, SBI_HARTMASK_MAX_BITS);
  23. };
  24. /** Initialize hartmask to zero */
  25. #define SBI_HARTMASK_INIT(__m) \
  26. bitmap_zero(((__m)->bits), SBI_HARTMASK_MAX_BITS)
  27. /** Initialize hartmask to zero except a particular HART id */
  28. #define SBI_HARTMASK_INIT_EXCEPT(__m, __h) \
  29. bitmap_zero_except(((__m)->bits), (__h), SBI_HARTMASK_MAX_BITS)
  30. /**
  31. * Get underlying bitmap of hartmask
  32. * @param m the hartmask pointer
  33. */
  34. #define sbi_hartmask_bits(__m) ((__m)->bits)
  35. /**
  36. * Set a HART in hartmask
  37. * @param h HART id to set
  38. * @param m the hartmask pointer
  39. */
  40. static inline void sbi_hartmask_set_hart(u32 h, struct sbi_hartmask *m)
  41. {
  42. if (h < SBI_HARTMASK_MAX_BITS)
  43. __set_bit(h, m->bits);
  44. }
  45. /**
  46. * Clear a HART in hartmask
  47. * @param h HART id to clear
  48. * @param m the hartmask pointer
  49. */
  50. static inline void sbi_hartmask_clear_hart(u32 h, struct sbi_hartmask *m)
  51. {
  52. if (h < SBI_HARTMASK_MAX_BITS)
  53. __clear_bit(h, m->bits);
  54. }
  55. /**
  56. * Test a HART in hartmask
  57. * @param h HART id to test
  58. * @param m the hartmask pointer
  59. */
  60. static inline int sbi_hartmask_test_hart(u32 h, const struct sbi_hartmask *m)
  61. {
  62. if (h < SBI_HARTMASK_MAX_BITS)
  63. return __test_bit(h, m->bits);
  64. return 0;
  65. }
  66. /**
  67. * Set all HARTs in a hartmask
  68. * @param dstp the hartmask pointer
  69. */
  70. static inline void sbi_hartmask_set_all(struct sbi_hartmask *dstp)
  71. {
  72. bitmap_fill(sbi_hartmask_bits(dstp), SBI_HARTMASK_MAX_BITS);
  73. }
  74. /**
  75. * Clear all HARTs in a hartmask
  76. * @param dstp the hartmask pointer
  77. */
  78. static inline void sbi_hartmask_clear_all(struct sbi_hartmask *dstp)
  79. {
  80. bitmap_zero(sbi_hartmask_bits(dstp), SBI_HARTMASK_MAX_BITS);
  81. }
  82. /**
  83. * *dstp = *src1p & *src2p
  84. * @param dstp the hartmask result
  85. * @param src1p the first input
  86. * @param src2p the second input
  87. */
  88. static inline void sbi_hartmask_and(struct sbi_hartmask *dstp,
  89. const struct sbi_hartmask *src1p,
  90. const struct sbi_hartmask *src2p)
  91. {
  92. bitmap_and(sbi_hartmask_bits(dstp), sbi_hartmask_bits(src1p),
  93. sbi_hartmask_bits(src2p), SBI_HARTMASK_MAX_BITS);
  94. }
  95. /**
  96. * *dstp = *src1p | *src2p
  97. * @param dstp the hartmask result
  98. * @param src1p the first input
  99. * @param src2p the second input
  100. */
  101. static inline void sbi_hartmask_or(struct sbi_hartmask *dstp,
  102. const struct sbi_hartmask *src1p,
  103. const struct sbi_hartmask *src2p)
  104. {
  105. bitmap_or(sbi_hartmask_bits(dstp), sbi_hartmask_bits(src1p),
  106. sbi_hartmask_bits(src2p), SBI_HARTMASK_MAX_BITS);
  107. }
  108. /**
  109. * *dstp = *src1p ^ *src2p
  110. * @param dstp the hartmask result
  111. * @param src1p the first input
  112. * @param src2p the second input
  113. */
  114. static inline void sbi_hartmask_xor(struct sbi_hartmask *dstp,
  115. const struct sbi_hartmask *src1p,
  116. const struct sbi_hartmask *src2p)
  117. {
  118. bitmap_xor(sbi_hartmask_bits(dstp), sbi_hartmask_bits(src1p),
  119. sbi_hartmask_bits(src2p), SBI_HARTMASK_MAX_BITS);
  120. }
  121. /** Iterate over each HART in hartmask */
  122. #define sbi_hartmask_for_each_hart(__h, __m) \
  123. for_each_set_bit(__h, (__m)->bits, SBI_HARTMASK_MAX_BITS)
  124. #endif