2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Microsoft Corp.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
38 #include <sys/param.h>
41 #define MANA_ALERT (1 << 0) /* Alerts are providing more error info. */
42 #define MANA_WARNING (1 << 1) /* Driver output is more error sensitive. */
43 #define MANA_INFO (1 << 2) /* Provides additional driver info. */
44 #define MANA_DBG (1 << 3) /* Driver output for debugging. */
46 extern int mana_log_level;
48 #define mana_trace_raw(ctx, level, fmt, args...) \
51 if (((level) & mana_log_level) != (level)) \
53 printf(fmt, ##args); \
56 #define mana_trace(ctx, level, fmt, args...) \
57 mana_trace_raw(ctx, level, "%s() [TID:%d]: " \
58 fmt, __func__, curthread->td_tid, ##args)
61 #define mana_dbg(ctx, format, arg...) \
62 mana_trace(ctx, MANA_DBG, format, ##arg)
63 #define mana_info(ctx, format, arg...) \
64 mana_trace(ctx, MANA_INFO, format, ##arg)
65 #define mana_warn(ctx, format, arg...) \
66 mana_trace(ctx, MANA_WARNING, format, ##arg)
67 #define mana_err(ctx, format, arg...) \
68 mana_trace(ctx, MANA_ALERT, format, ##arg)
70 #define unlikely(x) __predict_false(!!(x))
71 #define likely(x) __predict_true(!!(x))
74 #define BITS_PER_LONG (sizeof(long) * NBBY)
76 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
77 #define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
78 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
79 #define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
80 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
83 #define ALIGN(x, y) roundup2((x), (y))
84 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0)
86 #define BIT(n) (1ULL << (n))
88 #define PHYS_PFN(x) ((unsigned long)((x) >> PAGE_SHIFT))
89 #define offset_in_page(x) ((x) & PAGE_MASK)
91 #define min_t(type, _x, _y) \
92 ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
94 #define test_bit(i, a) \
95 ((((volatile const unsigned long *)(a))[BIT_WORD(i)]) & BIT_MASK(i))
97 typedef volatile uint32_t atomic_t;
99 #define atomic_add_return(v, p) (atomic_fetchadd_int(p, v) + (v))
100 #define atomic_sub_return(v, p) (atomic_fetchadd_int(p, -(v)) - (v))
101 #define atomic_inc_return(p) atomic_add_return(1, p)
102 #define atomic_dec_return(p) atomic_sub_return(1, p)
103 #define atomic_read(p) atomic_add_return(0, p)
105 #define usleep_range(_1, _2) \
106 pause_sbt("gdma-usleep-range", SBT_1US * _1, SBT_1US * 1, C_ABSOLUTE)
109 gdma_msleep(unsigned int ms)
113 pause_sbt("gdma-msleep", mstosbt(ms), 0, C_HARDCLOCK);
117 bitmap_set(unsigned long *map, unsigned int start, int nr)
119 const unsigned int size = start + nr;
120 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
121 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
123 map += BIT_WORD(start);
125 while (nr - bits_to_set >= 0) {
128 bits_to_set = BITS_PER_LONG;
134 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
140 bitmap_clear(unsigned long *map, unsigned int start, int nr)
142 const unsigned int size = start + nr;
143 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
144 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
146 map += BIT_WORD(start);
148 while (nr - bits_to_clear >= 0) {
149 *map &= ~mask_to_clear;
151 bits_to_clear = BITS_PER_LONG;
152 mask_to_clear = ~0UL;
157 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
158 *map &= ~mask_to_clear;
162 static inline unsigned long
163 find_first_zero_bit(const unsigned long *p, unsigned long max)
167 for (i = 0; i < max / BITS_PER_LONG + 1; i++) {
170 return (i * BITS_PER_LONG + ffsl(n) - 1);
175 static inline unsigned long
176 ilog2(unsigned long x)
178 unsigned long log = x;
184 static inline unsigned long
185 roundup_pow_of_two(unsigned long x)
187 return (1UL << flsl(x - 1));
191 is_power_of_2(unsigned long n)
193 return (n == roundup_pow_of_two(n));
201 void init_completion(struct completion *c);
202 void free_completion(struct completion *c);
203 void complete(struct completion *c);
204 void wait_for_completion(struct completion *c);
205 int wait_for_completion_timeout(struct completion *c, int timeout);
206 #endif /* _GDMA_UTIL_H_ */