drm/linux: Add kasprintf and kvasprintf()
[dragonfly.git] / sys / dev / drm / include / linux / kernel.h
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2014-2016 François Tigeot
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef _LINUX_KERNEL_H_
30 #define _LINUX_KERNEL_H_
31
32 #include <sys/systm.h>
33 #include <sys/param.h>
34 #include <sys/libkern.h>
35 #include <sys/stat.h>
36 #include <sys/endian.h>
37
38 #include <linux/bitops.h>
39 #include <linux/compiler.h>
40 #include <linux/log2.h>
41 #include <linux/types.h>
42 #include <linux/printk.h>
43
44 #define U64_MAX         ((u64)~0ULL)
45
46 #undef  ALIGN
47 #define ALIGN(x, y)             roundup2((x), (y))
48 #define IS_ALIGNED(x, y)        (((x) & ((y) - 1)) == 0)
49 #define DIV_ROUND_UP            howmany
50 #define DIV_ROUND_UP_ULL(X, N)  DIV_ROUND_UP((unsigned long long)(X), (N))
51
52 #define udelay(t)               DELAY(t)
53
54 #define container_of(ptr, type, member)                         \
55 ({                                                              \
56         __typeof(((type *)0)->member) *_p = (ptr);              \
57         (type *)((char *)_p - offsetof(type, member));          \
58 })
59   
60 #define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
61
62 #define simple_strtoul  strtoul
63 #define simple_strtol   strtol
64
65 #define min(x, y)                       ((x) < (y) ? (x) : (y))
66 #define max(x, y)                       ((x) > (y) ? (x) : (y))
67
68 #define min3(a, b, c)                   min(a, min(b,c))
69 #define max3(a, b, c)                   max(a, max(b,c))
70
71 #define min_t(type, _x, _y)             ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
72 #define max_t(type, _x, _y)             ((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y))
73
74 #define clamp_t(type, _x, min, max)     min_t(type, max_t(type, _x, min), max)
75 #define clamp(x, lo, hi)                min( max(x,lo), hi)
76
77 /*
78  * This looks more complex than it should be. But we need to
79  * get the type for the ~ right in round_down (it needs to be
80  * as wide as the result!), and we want to evaluate the macro
81  * arguments just once each.
82  */
83 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
84 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
85 #define round_down(x, y) ((x) & ~__round_mask(x, y))
86
87 #define num_possible_cpus()     mp_ncpus
88
89 typedef struct pm_message {
90         int event;
91 } pm_message_t;
92
93 /* Swap values of a and b */
94 #define swap(a, b)                      \
95 ({                                      \
96         typeof(a) _swap_tmp = a;        \
97         a = b;                          \
98         b = _swap_tmp;                  \
99 })
100
101 #define DIV_ROUND_CLOSEST(x, divisor)   (((x) + ((divisor) /2)) / (divisor))
102
103 static inline uintmax_t
104 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
105 {
106         uintmax_t q = (x / divisor);
107         uintmax_t r = (x % divisor);
108
109         return ((q * multiplier) + ((r * multiplier) / divisor));
110 }
111
112 static inline int64_t abs64(int64_t x)
113 {
114         return (x < 0 ? -x : x);
115 }
116
117 #define DIV_ROUND_CLOSEST_ULL(ll, d)    \
118  ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
119
120 #define upper_32_bits(n)        ((u32)(((n) >> 16) >> 16))
121 #define lower_32_bits(n)        ((u32)(n))
122
123 /* Byteorder compat layer */
124 #if _BYTE_ORDER == _BIG_ENDIAN
125 #define __BIG_ENDIAN 4321
126 #else
127 #define __LITTLE_ENDIAN 1234
128 #endif
129
130 #define cpu_to_le16(x)  htole16(x)
131 #define le16_to_cpu(x)  le16toh(x)
132 #define cpu_to_le32(x)  htole32(x)
133 #define le32_to_cpu(x)  le32toh(x)
134 #define le32_to_cpup(x) le32toh(*x)
135
136 #define cpu_to_be16(x)  htobe16(x)
137 #define be16_to_cpu(x)  be16toh(x)
138 #define cpu_to_be32(x)  htobe32(x)
139 #define be32_to_cpu(x)  be32toh(x)
140 #define be32_to_cpup(x) be32toh(*x)
141
142 static inline int __must_check
143 kstrtouint(const char *s, unsigned int base, unsigned int *res)
144 {
145         *(res) = strtol(s,0,base);
146
147         return 0;
148 }
149
150 #define kvasprintf drm_vasprintf
151 #define kasprintf drm_asprintf
152
153 #endif  /* _LINUX_KERNEL_H_ */