xref: /freebsd/sys/dev/mana/gdma_util.h (revision ce110ea12fcea71ae437d0a1d0549d3d32055b0e)
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  * $FreeBSD$
31*ce110ea1SWei Hu  *
32*ce110ea1SWei Hu  */
33*ce110ea1SWei Hu 
34*ce110ea1SWei Hu #ifndef _GDMA_UTIL_H_
35*ce110ea1SWei Hu #define _GDMA_UTIL_H_
36*ce110ea1SWei Hu 
37*ce110ea1SWei Hu #include <sys/types.h>
38*ce110ea1SWei Hu #include <sys/param.h>
39*ce110ea1SWei Hu 
40*ce110ea1SWei Hu /* Log Levels */
41*ce110ea1SWei Hu #define MANA_ALERT	(1 << 0) /* Alerts are providing more error info.     */
42*ce110ea1SWei Hu #define MANA_WARNING	(1 << 1) /* Driver output is more error sensitive.    */
43*ce110ea1SWei Hu #define MANA_INFO	(1 << 2) /* Provides additional driver info.	      */
44*ce110ea1SWei Hu #define MANA_DBG	(1 << 3) /* Driver output for debugging.	      */
45*ce110ea1SWei Hu 
46*ce110ea1SWei Hu extern int mana_log_level;
47*ce110ea1SWei Hu 
48*ce110ea1SWei Hu #define mana_trace_raw(ctx, level, fmt, args...)			\
49*ce110ea1SWei Hu 	do {							\
50*ce110ea1SWei Hu 		((void)(ctx));					\
51*ce110ea1SWei Hu 		if (((level) & mana_log_level) != (level))	\
52*ce110ea1SWei Hu 			break;					\
53*ce110ea1SWei Hu 		printf(fmt, ##args);				\
54*ce110ea1SWei Hu 	} while (0)
55*ce110ea1SWei Hu 
56*ce110ea1SWei Hu #define mana_trace(ctx, level, fmt, args...)			\
57*ce110ea1SWei Hu 	mana_trace_raw(ctx, level, "%s() [TID:%d]: "		\
58*ce110ea1SWei Hu 	    fmt, __func__, curthread->td_tid, ##args)
59*ce110ea1SWei Hu 
60*ce110ea1SWei Hu 
61*ce110ea1SWei Hu #define mana_dbg(ctx, format, arg...)		\
62*ce110ea1SWei Hu 	mana_trace(ctx, MANA_DBG, format, ##arg)
63*ce110ea1SWei Hu #define mana_info(ctx, format, arg...)		\
64*ce110ea1SWei Hu 	mana_trace(ctx, MANA_INFO, format, ##arg)
65*ce110ea1SWei Hu #define mana_warn(ctx, format, arg...)		\
66*ce110ea1SWei Hu 	mana_trace(ctx, MANA_WARNING, format, ##arg)
67*ce110ea1SWei Hu #define mana_err(ctx, format, arg...)		\
68*ce110ea1SWei Hu 	mana_trace(ctx, MANA_ALERT, format, ##arg)
69*ce110ea1SWei Hu 
70*ce110ea1SWei Hu #define unlikely(x)	__predict_false(!!(x))
71*ce110ea1SWei Hu #define likely(x)	__predict_true(!!(x))
72*ce110ea1SWei Hu 
73*ce110ea1SWei Hu 
74*ce110ea1SWei Hu #define BITS_PER_LONG			(sizeof(long) * NBBY)
75*ce110ea1SWei Hu 
76*ce110ea1SWei Hu #define BITMAP_FIRST_WORD_MASK(start)	(~0UL << ((start) % BITS_PER_LONG))
77*ce110ea1SWei Hu #define BITMAP_LAST_WORD_MASK(n)	(~0UL >> (BITS_PER_LONG - (n)))
78*ce110ea1SWei Hu #define	BITS_TO_LONGS(n)	howmany((n), BITS_PER_LONG)
79*ce110ea1SWei Hu #define	BIT_MASK(nr)		(1UL << ((nr) & (BITS_PER_LONG - 1)))
80*ce110ea1SWei Hu #define	BIT_WORD(nr)		((nr) / BITS_PER_LONG)
81*ce110ea1SWei Hu 
82*ce110ea1SWei Hu #undef	ALIGN
83*ce110ea1SWei Hu #define ALIGN(x, y)		roundup2((x), (y))
84*ce110ea1SWei Hu #define IS_ALIGNED(x, a)	(((x) & ((__typeof(x))(a) - 1)) == 0)
85*ce110ea1SWei Hu 
86*ce110ea1SWei Hu #define BIT(n)			(1ULL << (n))
87*ce110ea1SWei Hu 
88*ce110ea1SWei Hu #define PHYS_PFN(x)		((unsigned long)((x) >> PAGE_SHIFT))
89*ce110ea1SWei Hu #define offset_in_page(x)	((x) & PAGE_MASK)
90*ce110ea1SWei Hu 
91*ce110ea1SWei Hu #define min_t(type, _x, _y)						\
92*ce110ea1SWei Hu     ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
93*ce110ea1SWei Hu 
94*ce110ea1SWei Hu #define test_bit(i, a)							\
95*ce110ea1SWei Hu     ((((volatile const unsigned long *)(a))[BIT_WORD(i)]) & BIT_MASK(i))
96*ce110ea1SWei Hu 
97*ce110ea1SWei Hu typedef volatile uint32_t atomic_t;
98*ce110ea1SWei Hu 
99*ce110ea1SWei Hu #define	atomic_add_return(v, p)		(atomic_fetchadd_int(p, v) + (v))
100*ce110ea1SWei Hu #define	atomic_sub_return(v, p)		(atomic_fetchadd_int(p, -(v)) - (v))
101*ce110ea1SWei Hu #define	atomic_inc_return(p)		atomic_add_return(1, p)
102*ce110ea1SWei Hu #define	atomic_dec_return(p)		atomic_sub_return(1, p)
103*ce110ea1SWei Hu #define atomic_read(p)			atomic_add_return(0, p)
104*ce110ea1SWei Hu 
105*ce110ea1SWei Hu #define usleep_range(_1, _2)						\
106*ce110ea1SWei Hu     pause_sbt("gdma-usleep-range", SBT_1US * _1, SBT_1US * 1, C_ABSOLUTE)
107*ce110ea1SWei Hu 
108*ce110ea1SWei Hu static inline void
109*ce110ea1SWei Hu gdma_msleep(unsigned int ms)
110*ce110ea1SWei Hu {
111*ce110ea1SWei Hu 	if (ms == 0)
112*ce110ea1SWei Hu 		ms = 1;
113*ce110ea1SWei Hu 	pause_sbt("gdma-msleep", mstosbt(ms), 0, C_HARDCLOCK);
114*ce110ea1SWei Hu }
115*ce110ea1SWei Hu 
116*ce110ea1SWei Hu static inline void
117*ce110ea1SWei Hu bitmap_set(unsigned long *map, unsigned int start, int nr)
118*ce110ea1SWei Hu {
119*ce110ea1SWei Hu 	const unsigned int size = start + nr;
120*ce110ea1SWei Hu 	int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
121*ce110ea1SWei Hu 	unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
122*ce110ea1SWei Hu 
123*ce110ea1SWei Hu 	map += BIT_WORD(start);
124*ce110ea1SWei Hu 
125*ce110ea1SWei Hu 	while (nr - bits_to_set >= 0) {
126*ce110ea1SWei Hu 		*map |= mask_to_set;
127*ce110ea1SWei Hu 		nr -= bits_to_set;
128*ce110ea1SWei Hu 		bits_to_set = BITS_PER_LONG;
129*ce110ea1SWei Hu 		mask_to_set = ~0UL;
130*ce110ea1SWei Hu 		map++;
131*ce110ea1SWei Hu 	}
132*ce110ea1SWei Hu 
133*ce110ea1SWei Hu 	if (nr) {
134*ce110ea1SWei Hu 		mask_to_set &= BITMAP_LAST_WORD_MASK(size);
135*ce110ea1SWei Hu 		*map |= mask_to_set;
136*ce110ea1SWei Hu 	}
137*ce110ea1SWei Hu }
138*ce110ea1SWei Hu 
139*ce110ea1SWei Hu static inline void
140*ce110ea1SWei Hu bitmap_clear(unsigned long *map, unsigned int start, int nr)
141*ce110ea1SWei Hu {
142*ce110ea1SWei Hu 	const unsigned int size = start + nr;
143*ce110ea1SWei Hu 	int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
144*ce110ea1SWei Hu 	unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
145*ce110ea1SWei Hu 
146*ce110ea1SWei Hu 	map += BIT_WORD(start);
147*ce110ea1SWei Hu 
148*ce110ea1SWei Hu 	while (nr - bits_to_clear >= 0) {
149*ce110ea1SWei Hu 		*map &= ~mask_to_clear;
150*ce110ea1SWei Hu 		nr -= bits_to_clear;
151*ce110ea1SWei Hu 		bits_to_clear = BITS_PER_LONG;
152*ce110ea1SWei Hu 		mask_to_clear = ~0UL;
153*ce110ea1SWei Hu 		map++;
154*ce110ea1SWei Hu 	}
155*ce110ea1SWei Hu 
156*ce110ea1SWei Hu 	if (nr) {
157*ce110ea1SWei Hu 		mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
158*ce110ea1SWei Hu 		*map &= ~mask_to_clear;
159*ce110ea1SWei Hu 	}
160*ce110ea1SWei Hu }
161*ce110ea1SWei Hu 
162*ce110ea1SWei Hu static inline unsigned long
163*ce110ea1SWei Hu find_first_zero_bit(const unsigned long *p, unsigned long max)
164*ce110ea1SWei Hu {
165*ce110ea1SWei Hu 	unsigned long i, n;
166*ce110ea1SWei Hu 
167*ce110ea1SWei Hu 	for (i = 0; i < max / BITS_PER_LONG + 1; i++) {
168*ce110ea1SWei Hu 		n = ~p[i];
169*ce110ea1SWei Hu 		if (n != 0)
170*ce110ea1SWei Hu 			return (i * BITS_PER_LONG + ffsl(n) - 1);
171*ce110ea1SWei Hu 	}
172*ce110ea1SWei Hu 	return (max);
173*ce110ea1SWei Hu }
174*ce110ea1SWei Hu 
175*ce110ea1SWei Hu static inline unsigned long
176*ce110ea1SWei Hu ilog2(unsigned long x)
177*ce110ea1SWei Hu {
178*ce110ea1SWei Hu 	unsigned long log = x;
179*ce110ea1SWei Hu 	while (x >>= 1)
180*ce110ea1SWei Hu 		log++;
181*ce110ea1SWei Hu 	return (log);
182*ce110ea1SWei Hu }
183*ce110ea1SWei Hu 
184*ce110ea1SWei Hu static inline unsigned long
185*ce110ea1SWei Hu roundup_pow_of_two(unsigned long x)
186*ce110ea1SWei Hu {
187*ce110ea1SWei Hu 	return (1UL << flsl(x - 1));
188*ce110ea1SWei Hu }
189*ce110ea1SWei Hu 
190*ce110ea1SWei Hu static inline int
191*ce110ea1SWei Hu is_power_of_2(unsigned long n)
192*ce110ea1SWei Hu {
193*ce110ea1SWei Hu 	return (n == roundup_pow_of_two(n));
194*ce110ea1SWei Hu }
195*ce110ea1SWei Hu 
196*ce110ea1SWei Hu struct completion {
197*ce110ea1SWei Hu 	unsigned int done;
198*ce110ea1SWei Hu 	struct mtx lock;
199*ce110ea1SWei Hu };
200*ce110ea1SWei Hu 
201*ce110ea1SWei Hu void init_completion(struct completion *c);
202*ce110ea1SWei Hu void free_completion(struct completion *c);
203*ce110ea1SWei Hu void complete(struct completion *c);
204*ce110ea1SWei Hu void wait_for_completion(struct completion *c);
205*ce110ea1SWei Hu int wait_for_completion_timeout(struct completion *c, int timeout);
206*ce110ea1SWei Hu #endif /* _GDMA_UTIL_H_ */
207