eal: list acceptable init priorities
[dpdk.git] / lib / librte_eal / common / include / rte_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_COMMON_H_
6 #define _RTE_COMMON_H_
7
8 /**
9  * @file
10  *
11  * Generic, commonly-used macro and inline function definitions
12  * for DPDK.
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24
25 #include <rte_config.h>
26
27 #ifndef typeof
28 #define typeof __typeof__
29 #endif
30
31 #ifndef asm
32 #define asm __asm__
33 #endif
34
35 /** C extension macro for environments lacking C11 features. */
36 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
37 #define RTE_STD_C11 __extension__
38 #else
39 #define RTE_STD_C11
40 #endif
41
42 /** Define GCC_VERSION **/
43 #ifdef RTE_TOOLCHAIN_GCC
44 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
45                 __GNUC_PATCHLEVEL__)
46 #endif
47
48 #ifdef RTE_ARCH_STRICT_ALIGN
49 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
50 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
51 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
52 #else
53 typedef uint64_t unaligned_uint64_t;
54 typedef uint32_t unaligned_uint32_t;
55 typedef uint16_t unaligned_uint16_t;
56 #endif
57
58 /**
59  * Force alignment
60  */
61 #define __rte_aligned(a) __attribute__((__aligned__(a)))
62
63 /**
64  * Force a structure to be packed
65  */
66 #define __rte_packed __attribute__((__packed__))
67
68 /******* Macro to mark functions and fields scheduled for removal *****/
69 #define __rte_deprecated        __attribute__((__deprecated__))
70
71 /*********** Macros to eliminate unused variable warnings ********/
72
73 /**
74  * short definition to mark a function parameter unused
75  */
76 #define __rte_unused __attribute__((__unused__))
77
78 /**
79  * definition to mark a variable or function parameter as used so
80  * as to avoid a compiler warning
81  */
82 #define RTE_SET_USED(x) (void)(x)
83
84 #define RTE_PRIORITY_LOG 101
85 #define RTE_PRIORITY_BUS 110
86
87 #define RTE_PRIO(prio) \
88         RTE_PRIORITY_ ## prio
89
90 /**
91  * Run function before main() with low priority.
92  *
93  * The constructor will be run after prioritized constructors.
94  *
95  * @param func
96  *   Constructor function.
97  */
98 #define RTE_INIT(func) \
99 static void __attribute__((constructor, used)) func(void)
100
101 /**
102  * Run function before main() with high priority.
103  *
104  * @param func
105  *   Constructor function.
106  * @param prio
107  *   Priority number must be above 100.
108  *   Lowest number is the first to run.
109  */
110 #define RTE_INIT_PRIO(func, prio) \
111 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
112
113 /**
114  * Force a function to be inlined
115  */
116 #define __rte_always_inline inline __attribute__((always_inline))
117
118 /**
119  * Force a function to be noinlined
120  */
121 #define __rte_noinline  __attribute__((noinline))
122
123 /*********** Macros for pointer arithmetic ********/
124
125 /**
126  * add a byte-value offset from a pointer
127  */
128 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
129
130 /**
131  * subtract a byte-value offset from a pointer
132  */
133 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
134
135 /**
136  * get the difference between two pointer values, i.e. how far apart
137  * in bytes are the locations they point two. It is assumed that
138  * ptr1 is greater than ptr2.
139  */
140 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
141
142 /*********** Macros/static functions for doing alignment ********/
143
144
145 /**
146  * Macro to align a pointer to a given power-of-two. The resultant
147  * pointer will be a pointer of the same type as the first parameter, and
148  * point to an address no higher than the first parameter. Second parameter
149  * must be a power-of-two value.
150  */
151 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
152         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
153
154 /**
155  * Macro to align a value to a given power-of-two. The resultant value
156  * will be of the same type as the first parameter, and will be no
157  * bigger than the first parameter. Second parameter must be a
158  * power-of-two value.
159  */
160 #define RTE_ALIGN_FLOOR(val, align) \
161         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
162
163 /**
164  * Macro to align a pointer to a given power-of-two. The resultant
165  * pointer will be a pointer of the same type as the first parameter, and
166  * point to an address no lower than the first parameter. Second parameter
167  * must be a power-of-two value.
168  */
169 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
170         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
171
172 /**
173  * Macro to align a value to a given power-of-two. The resultant value
174  * will be of the same type as the first parameter, and will be no lower
175  * than the first parameter. Second parameter must be a power-of-two
176  * value.
177  */
178 #define RTE_ALIGN_CEIL(val, align) \
179         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
180
181 /**
182  * Macro to align a pointer to a given power-of-two. The resultant
183  * pointer will be a pointer of the same type as the first parameter, and
184  * point to an address no lower than the first parameter. Second parameter
185  * must be a power-of-two value.
186  * This function is the same as RTE_PTR_ALIGN_CEIL
187  */
188 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
189
190 /**
191  * Macro to align a value to a given power-of-two. The resultant
192  * value will be of the same type as the first parameter, and
193  * will be no lower than the first parameter. Second parameter
194  * must be a power-of-two value.
195  * This function is the same as RTE_ALIGN_CEIL
196  */
197 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
198
199 /**
200  * Macro to align a value to the multiple of given value. The resultant
201  * value will be of the same type as the first parameter and will be no lower
202  * than the first parameter.
203  */
204 #define RTE_ALIGN_MUL_CEIL(v, mul) \
205         (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
206
207 /**
208  * Macro to align a value to the multiple of given value. The resultant
209  * value will be of the same type as the first parameter and will be no higher
210  * than the first parameter.
211  */
212 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
213         ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
214
215 /**
216  * Checks if a pointer is aligned to a given power-of-two value
217  *
218  * @param ptr
219  *   The pointer whose alignment is to be checked
220  * @param align
221  *   The power-of-two value to which the ptr should be aligned
222  *
223  * @return
224  *   True(1) where the pointer is correctly aligned, false(0) otherwise
225  */
226 static inline int
227 rte_is_aligned(void *ptr, unsigned align)
228 {
229         return RTE_PTR_ALIGN(ptr, align) == ptr;
230 }
231
232 /*********** Macros for compile type checks ********/
233
234 /**
235  * Triggers an error at compilation time if the condition is true.
236  */
237 #ifndef __OPTIMIZE__
238 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
239 #else
240 extern int RTE_BUILD_BUG_ON_detected_error;
241 #define RTE_BUILD_BUG_ON(condition) do {             \
242         ((void)sizeof(char[1 - 2*!!(condition)]));   \
243         if (condition)                               \
244                 RTE_BUILD_BUG_ON_detected_error = 1; \
245 } while(0)
246 #endif
247
248 /**
249  * Combines 32b inputs most significant set bits into the least
250  * significant bits to construct a value with the same MSBs as x
251  * but all 1's under it.
252  *
253  * @param x
254  *    The integer whose MSBs need to be combined with its LSBs
255  * @return
256  *    The combined value.
257  */
258 static inline uint32_t
259 rte_combine32ms1b(register uint32_t x)
260 {
261         x |= x >> 1;
262         x |= x >> 2;
263         x |= x >> 4;
264         x |= x >> 8;
265         x |= x >> 16;
266
267         return x;
268 }
269
270 /**
271  * Combines 64b inputs most significant set bits into the least
272  * significant bits to construct a value with the same MSBs as x
273  * but all 1's under it.
274  *
275  * @param v
276  *    The integer whose MSBs need to be combined with its LSBs
277  * @return
278  *    The combined value.
279  */
280 static inline uint64_t
281 rte_combine64ms1b(register uint64_t v)
282 {
283         v |= v >> 1;
284         v |= v >> 2;
285         v |= v >> 4;
286         v |= v >> 8;
287         v |= v >> 16;
288         v |= v >> 32;
289
290         return v;
291 }
292
293 /*********** Macros to work with powers of 2 ********/
294
295 /**
296  * Returns true if n is a power of 2
297  * @param n
298  *     Number to check
299  * @return 1 if true, 0 otherwise
300  */
301 static inline int
302 rte_is_power_of_2(uint32_t n)
303 {
304         return n && !(n & (n - 1));
305 }
306
307 /**
308  * Aligns input parameter to the next power of 2
309  *
310  * @param x
311  *   The integer value to algin
312  *
313  * @return
314  *   Input parameter aligned to the next power of 2
315  */
316 static inline uint32_t
317 rte_align32pow2(uint32_t x)
318 {
319         x--;
320         x = rte_combine32ms1b(x);
321
322         return x + 1;
323 }
324
325 /**
326  * Aligns input parameter to the previous power of 2
327  *
328  * @param x
329  *   The integer value to algin
330  *
331  * @return
332  *   Input parameter aligned to the previous power of 2
333  */
334 static inline uint32_t
335 rte_align32prevpow2(uint32_t x)
336 {
337         x = rte_combine32ms1b(x);
338
339         return x - (x >> 1);
340 }
341
342 /**
343  * Aligns 64b input parameter to the next power of 2
344  *
345  * @param v
346  *   The 64b value to align
347  *
348  * @return
349  *   Input parameter aligned to the next power of 2
350  */
351 static inline uint64_t
352 rte_align64pow2(uint64_t v)
353 {
354         v--;
355         v = rte_combine64ms1b(v);
356
357         return v + 1;
358 }
359
360 /**
361  * Aligns 64b input parameter to the previous power of 2
362  *
363  * @param v
364  *   The 64b value to align
365  *
366  * @return
367  *   Input parameter aligned to the previous power of 2
368  */
369 static inline uint64_t
370 rte_align64prevpow2(uint64_t v)
371 {
372         v = rte_combine64ms1b(v);
373
374         return v - (v >> 1);
375 }
376
377 /*********** Macros for calculating min and max **********/
378
379 /**
380  * Macro to return the minimum of two numbers
381  */
382 #define RTE_MIN(a, b) \
383         __extension__ ({ \
384                 typeof (a) _a = (a); \
385                 typeof (b) _b = (b); \
386                 _a < _b ? _a : _b; \
387         })
388
389 /**
390  * Macro to return the maximum of two numbers
391  */
392 #define RTE_MAX(a, b) \
393         __extension__ ({ \
394                 typeof (a) _a = (a); \
395                 typeof (b) _b = (b); \
396                 _a > _b ? _a : _b; \
397         })
398
399 /*********** Other general functions / macros ********/
400
401 /**
402  * Searches the input parameter for the least significant set bit
403  * (starting from zero).
404  * If a least significant 1 bit is found, its bit index is returned.
405  * If the content of the input parameter is zero, then the content of the return
406  * value is undefined.
407  * @param v
408  *     input parameter, should not be zero.
409  * @return
410  *     least significant set bit in the input parameter.
411  */
412 static inline uint32_t
413 rte_bsf32(uint32_t v)
414 {
415         return __builtin_ctz(v);
416 }
417
418 /**
419  * Return the rounded-up log2 of a integer.
420  *
421  * @param v
422  *     The input parameter.
423  * @return
424  *     The rounded-up log2 of the input, or 0 if the input is 0.
425  */
426 static inline uint32_t
427 rte_log2_u32(uint32_t v)
428 {
429         if (v == 0)
430                 return 0;
431         v = rte_align32pow2(v);
432         return rte_bsf32(v);
433 }
434
435 #ifndef offsetof
436 /** Return the offset of a field in a structure. */
437 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
438 #endif
439
440 /**
441  * Return pointer to the wrapping struct instance.
442  *
443  * Example:
444  *
445  *  struct wrapper {
446  *      ...
447  *      struct child c;
448  *      ...
449  *  };
450  *
451  *  struct child *x = obtain(...);
452  *  struct wrapper *w = container_of(x, struct wrapper, c);
453  */
454 #ifndef container_of
455 #define container_of(ptr, type, member) __extension__ ({                \
456                         const typeof(((type *)0)->member) *_ptr = (ptr); \
457                         __attribute__((unused)) type *_target_ptr =     \
458                                 (type *)(ptr);                          \
459                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
460                 })
461 #endif
462
463 #define _RTE_STR(x) #x
464 /** Take a macro value and get a string version of it */
465 #define RTE_STR(x) _RTE_STR(x)
466
467 /**
468  * ISO C helpers to modify format strings using variadic macros.
469  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
470  * An empty %s argument is appended to avoid a dangling comma.
471  */
472 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
473 #define RTE_FMT_HEAD(fmt, ...) fmt
474 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
475
476 /** Mask value of type "tp" for the first "ln" bit set. */
477 #define RTE_LEN2MASK(ln, tp)    \
478         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
479
480 /** Number of elements in the array. */
481 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
482
483 /**
484  * Converts a numeric string to the equivalent uint64_t value.
485  * As well as straight number conversion, also recognises the suffixes
486  * k, m and g for kilobytes, megabytes and gigabytes respectively.
487  *
488  * If a negative number is passed in  i.e. a string with the first non-black
489  * character being "-", zero is returned. Zero is also returned in the case of
490  * an error with the strtoull call in the function.
491  *
492  * @param str
493  *     String containing number to convert.
494  * @return
495  *     Number.
496  */
497 static inline uint64_t
498 rte_str_to_size(const char *str)
499 {
500         char *endptr;
501         unsigned long long size;
502
503         while (isspace((int)*str))
504                 str++;
505         if (*str == '-')
506                 return 0;
507
508         errno = 0;
509         size = strtoull(str, &endptr, 0);
510         if (errno)
511                 return 0;
512
513         if (*endptr == ' ')
514                 endptr++; /* allow 1 space gap */
515
516         switch (*endptr){
517         case 'G': case 'g': size *= 1024; /* fall-through */
518         case 'M': case 'm': size *= 1024; /* fall-through */
519         case 'K': case 'k': size *= 1024; /* fall-through */
520         default:
521                 break;
522         }
523         return size;
524 }
525
526 /**
527  * Function to terminate the application immediately, printing an error
528  * message and returning the exit_code back to the shell.
529  *
530  * This function never returns
531  *
532  * @param exit_code
533  *     The exit code to be returned by the application
534  * @param format
535  *     The format string to be used for printing the message. This can include
536  *     printf format characters which will be expanded using any further parameters
537  *     to the function.
538  */
539 void
540 rte_exit(int exit_code, const char *format, ...)
541         __attribute__((noreturn))
542         __attribute__((format(printf, 2, 3)));
543
544 #ifdef __cplusplus
545 }
546 #endif
547
548 #endif