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