]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mana/gdma_util.h
sdhci: add a missing newline
[FreeBSD/FreeBSD.git] / sys / dev / mana / gdma_util.h
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  * $FreeBSD$
31  *
32  */
33
34 #ifndef _GDMA_UTIL_H_
35 #define _GDMA_UTIL_H_
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39
40 /* Log Levels */
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.              */
45
46 extern int mana_log_level;
47
48 #define mana_trace_raw(ctx, level, fmt, args...)                        \
49         do {                                                    \
50                 ((void)(ctx));                                  \
51                 if (((level) & mana_log_level) != (level))      \
52                         break;                                  \
53                 printf(fmt, ##args);                            \
54         } while (0)
55
56 #define mana_trace(ctx, level, fmt, args...)                    \
57         mana_trace_raw(ctx, level, "%s() [TID:%d]: "            \
58             fmt, __func__, curthread->td_tid, ##args)
59
60
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)
69
70 #define unlikely(x)     __predict_false(!!(x))
71 #define likely(x)       __predict_true(!!(x))
72
73
74 #define BITS_PER_LONG                   (sizeof(long) * NBBY)
75
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)
81
82 #undef  ALIGN
83 #define ALIGN(x, y)             roundup2((x), (y))
84 #define IS_ALIGNED(x, a)        (((x) & ((__typeof(x))(a) - 1)) == 0)
85
86 #define BIT(n)                  (1ULL << (n))
87
88 #define PHYS_PFN(x)             ((unsigned long)((x) >> PAGE_SHIFT))
89 #define offset_in_page(x)       ((x) & PAGE_MASK)
90
91 #define min_t(type, _x, _y)                                             \
92     ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
93
94 #define test_bit(i, a)                                                  \
95     ((((volatile const unsigned long *)(a))[BIT_WORD(i)]) & BIT_MASK(i))
96
97 typedef volatile uint32_t atomic_t;
98
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)
104
105 #define usleep_range(_1, _2)                                            \
106     pause_sbt("gdma-usleep-range", SBT_1US * _1, SBT_1US * 1, C_ABSOLUTE)
107
108 static inline void
109 gdma_msleep(unsigned int ms)
110 {
111         if (ms == 0)
112                 ms = 1;
113         pause_sbt("gdma-msleep", mstosbt(ms), 0, C_HARDCLOCK);
114 }
115
116 static inline void
117 bitmap_set(unsigned long *map, unsigned int start, int nr)
118 {
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);
122
123         map += BIT_WORD(start);
124
125         while (nr - bits_to_set >= 0) {
126                 *map |= mask_to_set;
127                 nr -= bits_to_set;
128                 bits_to_set = BITS_PER_LONG;
129                 mask_to_set = ~0UL;
130                 map++;
131         }
132
133         if (nr) {
134                 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
135                 *map |= mask_to_set;
136         }
137 }
138
139 static inline void
140 bitmap_clear(unsigned long *map, unsigned int start, int nr)
141 {
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);
145
146         map += BIT_WORD(start);
147
148         while (nr - bits_to_clear >= 0) {
149                 *map &= ~mask_to_clear;
150                 nr -= bits_to_clear;
151                 bits_to_clear = BITS_PER_LONG;
152                 mask_to_clear = ~0UL;
153                 map++;
154         }
155
156         if (nr) {
157                 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
158                 *map &= ~mask_to_clear;
159         }
160 }
161
162 static inline unsigned long
163 find_first_zero_bit(const unsigned long *p, unsigned long max)
164 {
165         unsigned long i, n;
166
167         for (i = 0; i < max / BITS_PER_LONG + 1; i++) {
168                 n = ~p[i];
169                 if (n != 0)
170                         return (i * BITS_PER_LONG + ffsl(n) - 1);
171         }
172         return (max);
173 }
174
175 static inline unsigned long
176 ilog2(unsigned long x)
177 {
178         unsigned long log = x;
179         while (x >>= 1)
180                 log++;
181         return (log);
182 }
183
184 static inline unsigned long
185 roundup_pow_of_two(unsigned long x)
186 {
187         return (1UL << flsl(x - 1));
188 }
189
190 static inline int
191 is_power_of_2(unsigned long n)
192 {
193         return (n == roundup_pow_of_two(n));
194 }
195
196 struct completion {
197         unsigned int done;
198         struct mtx lock;
199 };
200
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_ */