Subversion Repositories Kolibri OS

Rev

Rev 4292 | Rev 5056 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1408 serge 1
#ifndef _LINUX_KERNEL_H
2
#define _LINUX_KERNEL_H
3
 
4
/*
5
 * 'kernel.h' contains some often-used function prototypes etc
6
 */
7
 
8
#ifdef __KERNEL__
9
 
10
#include 
11
#include 
12
#include 
13
#include 
1970 serge 14
#include 
4110 Serge 15
#include 
2967 Serge 16
#include 
17
 
18
#define __init
19
 
1964 serge 20
#define USHRT_MAX	((u16)(~0U))
21
#define SHRT_MAX	((s16)(USHRT_MAX>>1))
22
#define SHRT_MIN	((s16)(-SHRT_MAX - 1))
1408 serge 23
#define INT_MAX     ((int)(~0U>>1))
24
#define INT_MIN     (-INT_MAX - 1)
25
#define UINT_MAX    (~0U)
26
#define LONG_MAX    ((long)(~0UL>>1))
27
#define LONG_MIN    (-LONG_MAX - 1)
28
#define ULONG_MAX   (~0UL)
29
#define LLONG_MAX   ((long long)(~0ULL>>1))
30
#define LLONG_MIN   (-LLONG_MAX - 1)
31
#define ULLONG_MAX  (~0ULL)
3031 serge 32
#define SIZE_MAX	(~(size_t)0)
1408 serge 33
 
34
#define ALIGN(x,a)      __ALIGN_MASK(x,(typeof(x))(a)-1)
35
#define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
36
#define PTR_ALIGN(p, a)     ((typeof(p))ALIGN((unsigned long)(p), (a)))
37
#define IS_ALIGNED(x, a)        (((x) & ((typeof(x))(a) - 1)) == 0)
38
 
4559 Serge 39
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
3747 Serge 40
 
4559 Serge 41
/*
42
 * This looks more complex than it should be. But we need to
43
 * get the type for the ~ right in round_down (it needs to be
44
 * as wide as the result!), and we want to evaluate the macro
45
 * arguments just once each.
46
 */
3747 Serge 47
#define __round_mask(x, y) ((__typeof__(x))((y)-1))
48
#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
4559 Serge 49
#define round_down(x, y) ((x) & ~__round_mask(x, y))
3747 Serge 50
 
4559 Serge 51
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
52
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
53
#define DIV_ROUND_UP_ULL(ll,d) \
54
	({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; })
55
 
56
#if BITS_PER_LONG == 32
57
# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
58
#else
59
# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
60
#endif
61
 
3039 serge 62
/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
63
#define roundup(x, y) (                                 \
64
{                                                       \
65
        const typeof(y) __y = y;                        \
66
        (((x) + (__y - 1)) / __y) * __y;                \
67
}                                                       \
68
)
4559 Serge 69
#define rounddown(x, y) (				\
70
{							\
71
	typeof(x) __x = (x);				\
72
	__x - (__x % (y));				\
73
}							\
74
)
2967 Serge 75
 
4559 Serge 76
/*
77
 * Divide positive or negative dividend by positive divisor and round
78
 * to closest integer. Result is undefined for negative divisors and
79
 * for negative dividends if the divisor variable type is unsigned.
80
 */
2967 Serge 81
#define DIV_ROUND_CLOSEST(x, divisor)(                  \
82
{                                                       \
4559 Serge 83
	typeof(x) __x = x;				\
84
	typeof(divisor) __d = divisor;			\
85
	(((typeof(x))-1) > 0 ||				\
86
	 ((typeof(divisor))-1) > 0 || (__x) > 0) ?	\
87
		(((__x) + ((__d) / 2)) / (__d)) :	\
88
		(((__x) - ((__d) / 2)) / (__d));	\
2967 Serge 89
}                                                       \
90
)
91
 
4559 Serge 92
/*
93
 * Multiplies an integer by a fraction, while avoiding unnecessary
94
 * overflow or loss of precision.
95
 */
96
#define mult_frac(x, numer, denom)(			\
97
{							\
98
	typeof(x) quot = (x) / (denom);			\
99
	typeof(x) rem  = (x) % (denom);			\
100
	(quot * (numer)) + ((rem * (numer)) / (denom));	\
101
}							\
102
)
4103 Serge 103
 
104
#define clamp_t(type, val, min, max) ({         \
105
        type __val = (val);                     \
106
        type __min = (min);                     \
107
        type __max = (max);                     \
108
        __val = __val < __min ? __min: __val;   \
109
        __val > __max ? __max: __val; })
110
 
111
 
112
 
1408 serge 113
/**
114
 * upper_32_bits - return bits 32-63 of a number
115
 * @n: the number we're accessing
116
 *
117
 * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
118
 * the "right shift count >= width of type" warning when that quantity is
119
 * 32-bits.
120
 */
121
#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
122
 
123
/**
124
 * lower_32_bits - return bits 0-31 of a number
125
 * @n: the number we're accessing
126
 */
127
#define lower_32_bits(n) ((u32)(n))
128
 
129
#define KERN_EMERG      "<0>"   /* system is unusable                   */
130
#define KERN_ALERT      "<1>"   /* action must be taken immediately     */
131
#define KERN_CRIT       "<2>"   /* critical conditions                  */
132
#define KERN_ERR        "<3>"   /* error conditions                     */
133
#define KERN_WARNING    "<4>"   /* warning conditions                   */
134
#define KERN_NOTICE     "<5>"   /* normal but significant condition     */
135
#define KERN_INFO       "<6>"   /* informational                        */
136
#define KERN_DEBUG      "<7>"   /* debug-level messages                 */
1970 serge 137
extern const char hex_asc[];
138
#define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
139
#define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
1408 serge 140
 
1970 serge 141
static inline char *pack_hex_byte(char *buf, u8 byte)
142
{
143
	*buf++ = hex_asc_hi(byte);
144
	*buf++ = hex_asc_lo(byte);
145
	return buf;
146
}
147
 
3480 Serge 148
enum {
149
    DUMP_PREFIX_NONE,
150
    DUMP_PREFIX_ADDRESS,
151
    DUMP_PREFIX_OFFSET
152
};
1970 serge 153
 
3480 Serge 154
int hex_to_bin(char ch);
155
int hex2bin(u8 *dst, const char *src, size_t count);
1970 serge 156
 
3480 Serge 157
 
1408 serge 158
//int printk(const char *fmt, ...);
159
 
160
#define printk(fmt, arg...)    dbgprintf(fmt , ##arg)
161
 
162
 
163
/*
164
 * min()/max()/clamp() macros that also do
165
 * strict type-checking.. See the
166
 * "unnecessary" pointer comparison.
167
 */
168
#define min(x, y) ({                \
169
    typeof(x) _min1 = (x);          \
170
    typeof(y) _min2 = (y);          \
171
    (void) (&_min1 == &_min2);      \
172
    _min1 < _min2 ? _min1 : _min2; })
173
 
174
#define max(x, y) ({                \
175
    typeof(x) _max1 = (x);          \
176
    typeof(y) _max2 = (y);          \
177
    (void) (&_max1 == &_max2);      \
178
    _max1 > _max2 ? _max1 : _max2; })
179
 
1970 serge 180
#define min3(x, y, z) ({			\
181
	typeof(x) _min1 = (x);			\
182
	typeof(y) _min2 = (y);			\
183
	typeof(z) _min3 = (z);			\
184
	(void) (&_min1 == &_min2);		\
185
	(void) (&_min1 == &_min3);		\
186
	_min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
187
		(_min2 < _min3 ? _min2 : _min3); })
188
 
189
#define max3(x, y, z) ({			\
190
	typeof(x) _max1 = (x);			\
191
	typeof(y) _max2 = (y);			\
192
	typeof(z) _max3 = (z);			\
193
	(void) (&_max1 == &_max2);		\
194
	(void) (&_max1 == &_max3);		\
195
	_max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
196
		(_max2 > _max3 ? _max2 : _max3); })
197
 
198
/**
199
 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
200
 * @x: value1
201
 * @y: value2
202
 */
203
#define min_not_zero(x, y) ({			\
204
	typeof(x) __x = (x);			\
205
	typeof(y) __y = (y);			\
206
	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
207
 
208
/**
209
 * clamp - return a value clamped to a given range with strict typechecking
210
 * @val: current value
211
 * @min: minimum allowable value
212
 * @max: maximum allowable value
213
 *
214
 * This macro does strict typechecking of min/max to make sure they are of the
215
 * same type as val.  See the unnecessary pointer comparisons.
216
 */
217
#define clamp(val, min, max) ({			\
218
	typeof(val) __val = (val);		\
219
	typeof(min) __min = (min);		\
220
	typeof(max) __max = (max);		\
221
	(void) (&__val == &__min);		\
222
	(void) (&__val == &__max);		\
223
	__val = __val < __min ? __min: __val;	\
224
	__val > __max ? __max: __val; })
225
 
1408 serge 226
/*
227
 * ..and if you can't take the strict
228
 * types, you can specify one yourself.
229
 *
230
 * Or not use min/max/clamp at all, of course.
231
 */
232
#define min_t(type, x, y) ({            \
233
    type __min1 = (x);          \
234
    type __min2 = (y);          \
235
    __min1 < __min2 ? __min1: __min2; })
236
 
237
#define max_t(type, x, y) ({            \
238
    type __max1 = (x);          \
239
    type __max2 = (y);          \
240
    __max1 > __max2 ? __max1: __max2; })
241
 
242
/**
243
 * container_of - cast a member of a structure out to the containing structure
244
 * @ptr:    the pointer to the member.
245
 * @type:   the type of the container struct this is embedded in.
246
 * @member: the name of the member within the struct.
247
 *
248
 */
249
#define container_of(ptr, type, member) ({          \
250
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
251
    (type *)( (char *)__mptr - offsetof(type,member) );})
252
 
253
 
254
static inline void *kcalloc(size_t n, size_t size, uint32_t flags)
255
{
256
        if (n != 0 && size > ULONG_MAX / n)
257
                return NULL;
258
        return kzalloc(n * size, 0);
259
}
260
 
1968 serge 261
 
1412 serge 262
void free (void *ptr);
263
 
1408 serge 264
#endif /* __KERNEL__ */
265
 
266
typedef unsigned long   pgprotval_t;
267
 
268
typedef struct pgprot { pgprotval_t pgprot; } pgprot_t;
269
 
3262 Serge 270
struct file
271
{
272
    struct page  **pages;         /* physical memory backend */
273
    unsigned int   count;
274
    unsigned int   allocated;
275
    void           *vma;
276
};
277
 
1408 serge 278
struct vm_area_struct {};
279
struct address_space {};
280
 
1430 serge 281
struct device
282
{
283
    struct device   *parent;
284
    void            *driver_data;
285
};
286
 
287
static inline void dev_set_drvdata(struct device *dev, void *data)
288
{
289
    dev->driver_data = data;
290
}
291
 
292
static inline void *dev_get_drvdata(struct device *dev)
293
{
294
    return dev->driver_data;
295
}
296
 
1408 serge 297
#define preempt_disable()       do { } while (0)
298
#define preempt_enable_no_resched() do { } while (0)
299
#define preempt_enable()        do { } while (0)
300
#define preempt_check_resched()     do { } while (0)
301
 
302
#define preempt_disable_notrace()       do { } while (0)
303
#define preempt_enable_no_resched_notrace() do { } while (0)
304
#define preempt_enable_notrace()        do { } while (0)
305
 
1964 serge 306
#define in_dbg_master() (0)
1408 serge 307
 
2005 serge 308
#define HZ 100
309
 
2967 Serge 310
struct tvec_base;
2005 serge 311
 
2967 Serge 312
struct timer_list {
313
         struct list_head entry;
314
         unsigned long expires;
315
 
316
         void (*function)(unsigned long);
317
         unsigned long data;
4125 Serge 318
         u32  handle;
2967 Serge 319
};
320
 
4125 Serge 321
#define setup_timer(_timer, _fn, _data)                                 \
322
        do {                                                            \
323
                (_timer)->function = (_fn);                             \
324
                (_timer)->data = (_data);                               \
325
                (_timer)->handle = 0;                                   \
326
        } while (0)
327
 
4292 Serge 328
int del_timer(struct timer_list *timer);
4125 Serge 329
 
4292 Serge 330
# define del_timer_sync(t)              del_timer(t)
331
 
2967 Serge 332
struct timespec {
333
    long tv_sec;                 /* seconds */
334
    long tv_nsec;                /* nanoseconds */
335
};
336
 
337
 
4292 Serge 338
#define mb()    asm volatile("mfence" : : : "memory")
339
#define rmb()   asm volatile("lfence" : : : "memory")
340
#define wmb()   asm volatile("sfence" : : : "memory")
341
 
342
 
2967 Serge 343
#define build_mmio_read(name, size, type, reg, barrier)     \
344
static inline type name(const volatile void __iomem *addr)  \
345
{ type ret; asm volatile("mov" size " %1,%0":reg (ret)      \
346
:"m" (*(volatile type __force *)addr) barrier); return ret; }
347
 
348
#define build_mmio_write(name, size, type, reg, barrier) \
349
static inline void name(type val, volatile void __iomem *addr) \
350
{ asm volatile("mov" size " %0,%1": :reg (val), \
351
"m" (*(volatile type __force *)addr) barrier); }
352
 
353
build_mmio_read(readb, "b", unsigned char, "=q", :"memory")
354
build_mmio_read(readw, "w", unsigned short, "=r", :"memory")
355
build_mmio_read(readl, "l", unsigned int, "=r", :"memory")
356
 
357
build_mmio_read(__readb, "b", unsigned char, "=q", )
358
build_mmio_read(__readw, "w", unsigned short, "=r", )
359
build_mmio_read(__readl, "l", unsigned int, "=r", )
360
 
361
build_mmio_write(writeb, "b", unsigned char, "q", :"memory")
362
build_mmio_write(writew, "w", unsigned short, "r", :"memory")
363
build_mmio_write(writel, "l", unsigned int, "r", :"memory")
364
 
365
build_mmio_write(__writeb, "b", unsigned char, "q", )
366
build_mmio_write(__writew, "w", unsigned short, "r", )
367
build_mmio_write(__writel, "l", unsigned int, "r", )
368
 
369
#define readb_relaxed(a) __readb(a)
370
#define readw_relaxed(a) __readw(a)
371
#define readl_relaxed(a) __readl(a)
372
#define __raw_readb __readb
373
#define __raw_readw __readw
374
#define __raw_readl __readl
375
 
376
#define __raw_writeb __writeb
377
#define __raw_writew __writew
378
#define __raw_writel __writel
379
 
380
static inline __u64 readq(const volatile void __iomem *addr)
381
{
382
        const volatile u32 __iomem *p = addr;
383
        u32 low, high;
384
 
385
        low = readl(p);
386
        high = readl(p + 1);
387
 
388
        return low + ((u64)high << 32);
389
}
390
 
391
static inline void writeq(__u64 val, volatile void __iomem *addr)
392
{
393
        writel(val, addr);
394
        writel(val >> 32, addr+4);
395
}
396
 
3031 serge 397
#define swap(a, b) \
398
        do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
2967 Serge 399
 
3031 serge 400
 
2967 Serge 401
#define mmiowb() barrier()
402
 
403
#define dev_err(dev, format, arg...)            \
404
        printk("Error %s " format, __func__ , ## arg)
405
 
406
#define dev_warn(dev, format, arg...)            \
407
        printk("Warning %s " format, __func__ , ## arg)
408
 
409
#define dev_info(dev, format, arg...)       \
410
        printk("Info %s " format , __func__, ## arg)
411
 
3480 Serge 412
//#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
413
#define BUILD_BUG_ON(condition)
2967 Serge 414
 
415
struct page
416
{
417
    unsigned int addr;
418
};
419
 
3243 Serge 420
#define page_to_phys(page)    ((dma_addr_t)(page))
2967 Serge 421
 
422
struct vm_fault {
423
    unsigned int flags;             /* FAULT_FLAG_xxx flags */
424
    pgoff_t pgoff;                  /* Logical page offset based on vma */
425
    void __user *virtual_address;   /* Faulting virtual address */
426
 
427
    struct page *page;              /* ->fault handlers should return a
428
                                     * page here, unless VM_FAULT_NOPAGE
429
                                     * is set (which is also implied by
430
                                     * VM_FAULT_ERROR).
431
                                     */
432
};
433
 
3031 serge 434
struct pagelist {
435
    dma_addr_t    *page;
436
    unsigned int   nents;
437
};
2967 Serge 438
 
3391 Serge 439
#define page_cache_release(page)        FreePage(page_to_phys(page))
3243 Serge 440
 
441
#define alloc_page(gfp_mask) (struct page*)AllocPage()
442
 
3391 Serge 443
#define __free_page(page) FreePage(page_to_phys(page))
444
 
445
#define get_page(a)
446
#define put_page(a)
447
#define set_pages_uc(a,b)
448
#define set_pages_wb(a,b)
449
 
450
#define pci_map_page(dev, page, offset, size, direction) \
451
        (dma_addr_t)( (offset)+page_to_phys(page))
452
 
453
#define pci_unmap_page(dev, dma_address, size, direction)
454
 
455
#define GFP_TEMPORARY  0
456
#define __GFP_NOWARN   0
457
#define __GFP_NORETRY  0
458
#define GFP_NOWAIT     0
459
 
460
#define IS_ENABLED(a)  0
461
 
462
 
463
#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
464
 
465
#define RCU_INIT_POINTER(p, v) \
466
        do { \
467
                p = (typeof(*v) __force __rcu *)(v); \
468
        } while (0)
469
 
470
 
471
#define rcu_dereference_raw(p)  ({ \
472
                                typeof(p) _________p1 = ACCESS_ONCE(p); \
473
                                (_________p1); \
474
                                })
475
#define rcu_assign_pointer(p, v) \
476
        ({ \
477
                if (!__builtin_constant_p(v) || \
478
                    ((v) != NULL)) \
479
                (p) = (v); \
480
        })
481
 
3482 Serge 482
 
483
unsigned int hweight16(unsigned int w);
484
 
485
#define cpufreq_quick_get_max(x) GetCpuFreq()
486
 
487
extern unsigned int tsc_khz;
488
 
3747 Serge 489
#define on_each_cpu(func,info,wait)             \
490
        ({                                      \
491
                func(info);                     \
492
                0;                              \
493
        })
494
 
495
 
1408 serge 496
#endif
497