Subversion Repositories Kolibri OS

Rev

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