1*ce110ea1SWei Hu /*-
2*ce110ea1SWei Hu * SPDX-License-Identifier: BSD-2-Clause
3*ce110ea1SWei Hu *
4*ce110ea1SWei Hu * Copyright (c) 2021 Microsoft Corp.
5*ce110ea1SWei Hu * All rights reserved.
6*ce110ea1SWei Hu *
7*ce110ea1SWei Hu * Redistribution and use in source and binary forms, with or without
8*ce110ea1SWei Hu * modification, are permitted provided that the following conditions
9*ce110ea1SWei Hu * are met:
10*ce110ea1SWei Hu *
11*ce110ea1SWei Hu * 1. Redistributions of source code must retain the above copyright
12*ce110ea1SWei Hu * notice, this list of conditions and the following disclaimer.
13*ce110ea1SWei Hu *
14*ce110ea1SWei Hu * 2. Redistributions in binary form must reproduce the above copyright
15*ce110ea1SWei Hu * notice, this list of conditions and the following disclaimer in the
16*ce110ea1SWei Hu * documentation and/or other materials provided with the distribution.
17*ce110ea1SWei Hu *
18*ce110ea1SWei Hu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*ce110ea1SWei Hu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*ce110ea1SWei Hu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*ce110ea1SWei Hu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*ce110ea1SWei Hu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*ce110ea1SWei Hu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*ce110ea1SWei Hu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*ce110ea1SWei Hu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*ce110ea1SWei Hu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*ce110ea1SWei Hu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*ce110ea1SWei Hu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*ce110ea1SWei Hu *
30*ce110ea1SWei Hu */
31*ce110ea1SWei Hu
32*ce110ea1SWei Hu #ifndef _GDMA_UTIL_H_
33*ce110ea1SWei Hu #define _GDMA_UTIL_H_
34*ce110ea1SWei Hu
35*ce110ea1SWei Hu #include <sys/types.h>
36*ce110ea1SWei Hu #include <sys/param.h>
37*ce110ea1SWei Hu
38*ce110ea1SWei Hu /* Log Levels */
39*ce110ea1SWei Hu #define MANA_ALERT (1 << 0) /* Alerts are providing more error info. */
40*ce110ea1SWei Hu #define MANA_WARNING (1 << 1) /* Driver output is more error sensitive. */
41*ce110ea1SWei Hu #define MANA_INFO (1 << 2) /* Provides additional driver info. */
42*ce110ea1SWei Hu #define MANA_DBG (1 << 3) /* Driver output for debugging. */
43*ce110ea1SWei Hu
44*ce110ea1SWei Hu extern int mana_log_level;
45*ce110ea1SWei Hu
46*ce110ea1SWei Hu #define mana_trace_raw(ctx, level, fmt, args...) \
47*ce110ea1SWei Hu do { \
48*ce110ea1SWei Hu ((void)(ctx)); \
49*ce110ea1SWei Hu if (((level) & mana_log_level) != (level)) \
50*ce110ea1SWei Hu break; \
51*ce110ea1SWei Hu printf(fmt, ##args); \
52*ce110ea1SWei Hu } while (0)
53*ce110ea1SWei Hu
54*ce110ea1SWei Hu #define mana_trace(ctx, level, fmt, args...) \
55*ce110ea1SWei Hu mana_trace_raw(ctx, level, "%s() [TID:%d]: " \
56*ce110ea1SWei Hu fmt, __func__, curthread->td_tid, ##args)
57*ce110ea1SWei Hu
58*ce110ea1SWei Hu
59*ce110ea1SWei Hu #define mana_dbg(ctx, format, arg...) \
60*ce110ea1SWei Hu mana_trace(ctx, MANA_DBG, format, ##arg)
61*ce110ea1SWei Hu #define mana_info(ctx, format, arg...) \
62*ce110ea1SWei Hu mana_trace(ctx, MANA_INFO, format, ##arg)
63*ce110ea1SWei Hu #define mana_warn(ctx, format, arg...) \
64*ce110ea1SWei Hu mana_trace(ctx, MANA_WARNING, format, ##arg)
65*ce110ea1SWei Hu #define mana_err(ctx, format, arg...) \
66*ce110ea1SWei Hu mana_trace(ctx, MANA_ALERT, format, ##arg)
67*ce110ea1SWei Hu
68*ce110ea1SWei Hu #define unlikely(x) __predict_false(!!(x))
69*ce110ea1SWei Hu #define likely(x) __predict_true(!!(x))
70*ce110ea1SWei Hu
71*ce110ea1SWei Hu
72*ce110ea1SWei Hu #define BITS_PER_LONG (sizeof(long) * NBBY)
73*ce110ea1SWei Hu
74*ce110ea1SWei Hu #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
75*ce110ea1SWei Hu #define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
76*ce110ea1SWei Hu #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
77*ce110ea1SWei Hu #define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
78*ce110ea1SWei Hu #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
79*ce110ea1SWei Hu
80*ce110ea1SWei Hu #undef ALIGN
81*ce110ea1SWei Hu #define ALIGN(x, y) roundup2((x), (y))
82*ce110ea1SWei Hu #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0)
83*ce110ea1SWei Hu
84*ce110ea1SWei Hu #define BIT(n) (1ULL << (n))
85*ce110ea1SWei Hu
86*ce110ea1SWei Hu #define PHYS_PFN(x) ((unsigned long)((x) >> PAGE_SHIFT))
87*ce110ea1SWei Hu #define offset_in_page(x) ((x) & PAGE_MASK)
88*ce110ea1SWei Hu
89*ce110ea1SWei Hu #define min_t(type, _x, _y) \
90*ce110ea1SWei Hu ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
91*ce110ea1SWei Hu
92*ce110ea1SWei Hu #define test_bit(i, a) \
93*ce110ea1SWei Hu ((((volatile const unsigned long *)(a))[BIT_WORD(i)]) & BIT_MASK(i))
94*ce110ea1SWei Hu
95*ce110ea1SWei Hu typedef volatile uint32_t atomic_t;
96*ce110ea1SWei Hu
97*ce110ea1SWei Hu #define atomic_add_return(v, p) (atomic_fetchadd_int(p, v) + (v))
98*ce110ea1SWei Hu #define atomic_sub_return(v, p) (atomic_fetchadd_int(p, -(v)) - (v))
99*ce110ea1SWei Hu #define atomic_inc_return(p) atomic_add_return(1, p)
100*ce110ea1SWei Hu #define atomic_dec_return(p) atomic_sub_return(1, p)
101*ce110ea1SWei Hu #define atomic_read(p) atomic_add_return(0, p)
102*ce110ea1SWei Hu
103*ce110ea1SWei Hu #define usleep_range(_1, _2) \
104*ce110ea1SWei Hu pause_sbt("gdma-usleep-range", SBT_1US * _1, SBT_1US * 1, C_ABSOLUTE)
105*ce110ea1SWei Hu
106*ce110ea1SWei Hu static inline void
gdma_msleep(unsigned int ms)107*ce110ea1SWei Hu gdma_msleep(unsigned int ms)
108*ce110ea1SWei Hu {
109*ce110ea1SWei Hu if (ms == 0)
110*ce110ea1SWei Hu ms = 1;
111*ce110ea1SWei Hu pause_sbt("gdma-msleep", mstosbt(ms), 0, C_HARDCLOCK);
112*ce110ea1SWei Hu }
113*ce110ea1SWei Hu
114*ce110ea1SWei Hu static inline void
bitmap_set(unsigned long * map,unsigned int start,int nr)115*ce110ea1SWei Hu bitmap_set(unsigned long *map, unsigned int start, int nr)
116*ce110ea1SWei Hu {
117*ce110ea1SWei Hu const unsigned int size = start + nr;
118*ce110ea1SWei Hu int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
119*ce110ea1SWei Hu unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
120*ce110ea1SWei Hu
121*ce110ea1SWei Hu map += BIT_WORD(start);
122*ce110ea1SWei Hu
123*ce110ea1SWei Hu while (nr - bits_to_set >= 0) {
124*ce110ea1SWei Hu *map |= mask_to_set;
125*ce110ea1SWei Hu nr -= bits_to_set;
126*ce110ea1SWei Hu bits_to_set = BITS_PER_LONG;
127*ce110ea1SWei Hu mask_to_set = ~0UL;
128*ce110ea1SWei Hu map++;
129*ce110ea1SWei Hu }
130*ce110ea1SWei Hu
131*ce110ea1SWei Hu if (nr) {
132*ce110ea1SWei Hu mask_to_set &= BITMAP_LAST_WORD_MASK(size);
133*ce110ea1SWei Hu *map |= mask_to_set;
134*ce110ea1SWei Hu }
135*ce110ea1SWei Hu }
136*ce110ea1SWei Hu
137*ce110ea1SWei Hu static inline void
bitmap_clear(unsigned long * map,unsigned int start,int nr)138*ce110ea1SWei Hu bitmap_clear(unsigned long *map, unsigned int start, int nr)
139*ce110ea1SWei Hu {
140*ce110ea1SWei Hu const unsigned int size = start + nr;
141*ce110ea1SWei Hu int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
142*ce110ea1SWei Hu unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
143*ce110ea1SWei Hu
144*ce110ea1SWei Hu map += BIT_WORD(start);
145*ce110ea1SWei Hu
146*ce110ea1SWei Hu while (nr - bits_to_clear >= 0) {
147*ce110ea1SWei Hu *map &= ~mask_to_clear;
148*ce110ea1SWei Hu nr -= bits_to_clear;
149*ce110ea1SWei Hu bits_to_clear = BITS_PER_LONG;
150*ce110ea1SWei Hu mask_to_clear = ~0UL;
151*ce110ea1SWei Hu map++;
152*ce110ea1SWei Hu }
153*ce110ea1SWei Hu
154*ce110ea1SWei Hu if (nr) {
155*ce110ea1SWei Hu mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
156*ce110ea1SWei Hu *map &= ~mask_to_clear;
157*ce110ea1SWei Hu }
158*ce110ea1SWei Hu }
159*ce110ea1SWei Hu
160*ce110ea1SWei Hu static inline unsigned long
find_first_zero_bit(const unsigned long * p,unsigned long max)161*ce110ea1SWei Hu find_first_zero_bit(const unsigned long *p, unsigned long max)
162*ce110ea1SWei Hu {
163*ce110ea1SWei Hu unsigned long i, n;
164*ce110ea1SWei Hu
165*ce110ea1SWei Hu for (i = 0; i < max / BITS_PER_LONG + 1; i++) {
166*ce110ea1SWei Hu n = ~p[i];
167*ce110ea1SWei Hu if (n != 0)
168*ce110ea1SWei Hu return (i * BITS_PER_LONG + ffsl(n) - 1);
169*ce110ea1SWei Hu }
170*ce110ea1SWei Hu return (max);
171*ce110ea1SWei Hu }
172*ce110ea1SWei Hu
173*ce110ea1SWei Hu struct completion {
174*ce110ea1SWei Hu unsigned int done;
175*ce110ea1SWei Hu struct mtx lock;
176*ce110ea1SWei Hu };
177*ce110ea1SWei Hu
178*ce110ea1SWei Hu void init_completion(struct completion *c);
179*ce110ea1SWei Hu void free_completion(struct completion *c);
180*ce110ea1SWei Hu void complete(struct completion *c);
181*ce110ea1SWei Hu void wait_for_completion(struct completion *c);
182*ce110ea1SWei Hu int wait_for_completion_timeout(struct completion *c, int timeout);
183*ce110ea1SWei Hu #endif /* _GDMA_UTIL_H_ */
184