summaryrefslogtreecommitdiff
path: root/lib/include/nvif/os.h
blob: d3581ef04aaa3b12bc6df69faf9fed92ded9ebe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
/*
 * Copyright 2012 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */

#ifndef __NOUVEAU_OS_H__
#define __NOUVEAU_OS_H__

/******************************************************************************
 * types
 *****************************************************************************/
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <ctype.h>

__extension__ typedef unsigned long long u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;

__extension__ typedef long long s64;
typedef int32_t s32;
typedef int16_t s16;
typedef int8_t s8;

#ifndef _ASM_GENERIC_INT_LL64_H
__extension__ typedef unsigned long long __u64;
typedef uint32_t __u32;
typedef uint16_t __u16;
typedef uint8_t __u8;
__extension__ typedef long long __s64;
typedef int32_t __s32;
typedef int16_t __s16;
typedef int8_t __s8;
#endif

typedef u64 dma_addr_t;
typedef dma_addr_t resource_size_t;

/******************************************************************************
 * various random macros
 *****************************************************************************/
#define noinline __attribute__ ((noinline))
#define __packed __attribute__ ((packed))
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))
#define max_t(t,a,b) max((t)(a), (t)(b))
#define min_t(t,a,b) min((t)(a), (t)(b))
#define clamp(a,b,c) min(max((a), (b)), (c))
#define roundup(a,b) ((((a) + ((b) - 1)) / (b)) * (b))
#define round_up(a,b) roundup((a), (b))
#define rounddown(a,b) ((a) / (b) * (b))
#define upper_32_bits(a) ((a) >> 32)
#define lower_32_bits(a) ((a) & 0xffffffff)
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define DIV_ROUND_CLOSEST(x, divisor)(			\
{							\
	typeof(x) __x = x;				\
	typeof(divisor) __d = divisor;			\
	(((typeof(x))-1) > 0 ||				\
	 ((typeof(divisor))-1) > 0 || (__x) > 0) ?	\
		(((__x) + ((__d) / 2)) / (__d)) :	\
		(((__x) - ((__d) / 2)) / (__d));	\
}							\
)
#define do_div(a,b) (a) = (a) / (b)
#define div_u64(a,b) (a) / (b)
#define likely(a) (a)
#define unlikely(a) (a)
#define BIT(a) (1UL << (a))

#define ERR_PTR(err) ((void *)(long)(err))
#define PTR_ERR(ptr) ((long)(ptr))
#define IS_ERR(ptr)  ((unsigned long)(ptr) >= (unsigned long)-4095)
#define IS_ERR_OR_NULL(ptr) ({                                                 \
	void *_ptr = (ptr);                                                    \
	(!_ptr || IS_ERR(_ptr));                                               \
})

#define jiffies (ktime_to_ns(ktime_get()))
#define nsecs_to_jiffies(a) (a)
#define usecs_to_jiffies(a) nsecs_to_jiffies((a) * 1000)
#define msecs_to_jiffies(a) usecs_to_jiffies((a) * 1000)
#define HZ 1000000

#define time_before(a,b) ((a) < (b))
#define time_after_eq(a,b) ((a) >= (b))

#define THIS_MODULE 0
#define request_module(a,b,c)

#define PAGE_SHIFT 12
#define PAGE_SIZE (1 << PAGE_SHIFT)
#define PAGE_MASK (PAGE_SHIFT - 1)

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define __acquires(a)
#define __releases(a)
#define __printf(a,b)
#define __user

#if defined(CONFIG_ARM)
#define IS_ENABLED_CONFIG_ARM 1
#else
#define IS_ENABLED_CONFIG_ARM 0
#endif

#if defined(CONFIG_IOMMU_API)
#define IS_ENABLED_CONFIG_IOMMU_API 1
#else
#define IS_ENABLED_CONFIG_IOMMU_API 0
#endif

#define IS_ENABLED(x) IS_ENABLED_##x

static inline int
order_base_2(u64 base)
{
	u64 temp = base >> 1;
	int log2;

	for (log2 = 0; temp; log2++, temp >>= 1) {
	}

	return (base & (base - 1)) ? log2 + 1: log2;
}

/******************************************************************************
 * errno
 *****************************************************************************/

#define ENOTSUPP 524


/******************************************************************************
 * endianness
 *****************************************************************************/

#include <endian.h>

#define le16_to_cpu(a) le16toh(a)
#define le32_to_cpu(a) le32toh(a)
#define cpu_to_le16(a) htole16(a)
#define cpu_to_le32(a) htole32(a)

/******************************************************************************
 * unaligned access
 *****************************************************************************/

static inline u16
get_unaligned_le16(void *ptr)
{
	return le16_to_cpu(*(u16 *)ptr);
}

static inline u32
get_unaligned_le32(void *ptr)
{
	return le32_to_cpu(*(u32 *)ptr);
}

static inline void
put_unaligned_le16(u16 val, void *ptr)
{
	*(u16 *)ptr = cpu_to_le16(val);
}

static inline void
put_unaligned_le32(u32 val, void *ptr)
{
	*(u32 *)ptr = cpu_to_le32(val);
}

/******************************************************************************
 * bitops
 *****************************************************************************/
#define __ffs64(a) (__builtin_ffsll(a) - 1)
#define __ffs(a) (__builtin_ffs(a) - 1)
#define fls(a) ((a) ? sizeof(a) * 8 - __builtin_clz(a) : 0)

static inline int
hweight8(u32 v) {
	int i = 0;
	while (v) {
		i += (v & 1);
		v >>= 1;
	}
	return i;
}

static inline int
hweight32(u32 v) {
	int i = 0;
	while (v) {
		i += (v & 1);
		v >>= 1;
	}
	return i;
}

#define BITS_PER_LONG (sizeof(unsigned long) * 8)
#define BITS_TO_LONGS(b) DIV_ROUND_UP((b), BITS_PER_LONG)
#define DECLARE_BITMAP(n,b) unsigned long n[BITS_TO_LONGS(b)]
#define BITMAP_POS(b) ((b) / BITS_PER_LONG)
#define BITMAP_BIT(b) ((b) % BITS_PER_LONG)

static inline int
test_bit(int bit, volatile unsigned long *ptr)
{
	return !!(ptr[BITMAP_POS(bit)] & (1UL << BITMAP_BIT(bit)));
}

static inline int
__test_and_clear_bit(int bit, volatile unsigned long *ptr)
{
	int ret = test_bit(bit, ptr);
	*ptr &= ~(1L << bit);
	return ret;
}

static inline int
test_and_clear_bit(int bit, volatile unsigned long *ptr)
{
	return !!(__sync_fetch_and_and(ptr, ~(1L << bit)) & (1L << bit));
}

static inline int
__test_and_set_bit(int bit, volatile unsigned long *ptr)
{
	int ret = test_bit(bit, ptr);
	*ptr |= (1L << bit);
	return ret;
}

static inline int
test_and_set_bit(int bit, volatile unsigned long *ptr)
{
	 return !!(__sync_fetch_and_or(ptr, (1L << bit)) & (1L << bit));
}

static inline void
set_bit(int bit, volatile unsigned long *ptr)
{
	test_and_set_bit(bit, ptr);
}

static inline void
__clear_bit(long bit, volatile unsigned long *addr)
{
	addr[BITMAP_POS(bit)] &= ~(1UL << BITMAP_BIT(bit));
}

static inline void
__set_bit(long bit, volatile unsigned long *addr)
{
	addr[BITMAP_POS(bit)] |= (1UL << BITMAP_BIT(bit));
}

static inline long
find_first_zero_bit(volatile unsigned long *addr, int bits)
{
	int bit;
	for (bit = 0; bit < bits; bit++) {
		if (!test_bit(bit, addr))
			break;
	}
	return bit;
}

static inline void
bitmap_fill(unsigned long *addr, unsigned int bits)
{
	int bit;
	for (bit = 0; bit < bits; bit++)
		__set_bit(bit, addr);
}

static inline void
bitmap_clear(unsigned long *addr, unsigned int pos, unsigned int bits)
{
	while (bits--)
		__clear_bit(pos++, addr);
}

/******************************************************************************
 * atomics
 *****************************************************************************/
typedef struct atomic {
	int value;
} atomic_t;

#define atomic_read(a) ((a)->value)
#define atomic_set(a,b) ((a)->value = (b))
#define atomic_inc(a) ((void) __sync_fetch_and_add (&(a)->value, 1))
#define atomic_dec(a) ((void) __sync_fetch_and_add (&(a)->value, -1))
#define atomic_add(a,b) ((void) __sync_add_and_fetch(&(a)->value, (b)))
#define atomic_add_return(b,a) (__sync_add_and_fetch(&(a)->value, (b)))
#define atomic_inc_return(a) atomic_add_return(1, (a))
#define atomic_dec_return(a) atomic_add_return(-1, (a))
#define atomic_dec_and_test(a) (atomic_dec_return(a) == 0)
#define atomic_or(a,b) (void) __sync_fetch_and_or(&(b)->value, (a));
#define atomic_xchg(a,b) \
	__atomic_exchange_n(&(a)->value, (b), __ATOMIC_SEQ_CST)

/******************************************************************************
 * ktime
 *****************************************************************************/
#include <time.h>

typedef struct timespec ktime_t;

static inline ktime_t
ktime_get(void)
{
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	return ts;
}

static inline s64
ktime_to_ns(ktime_t kt)
{
	return (s64)kt.tv_sec * 1000000000 + kt.tv_nsec;
}

static inline s64
ktime_to_us(ktime_t kt)
{
	return ktime_to_ns(kt) / 1000;
}

/******************************************************************************
 * krefs
 *****************************************************************************/
struct kref {
	atomic_t refcount;
};

#define kref_init(a) atomic_set(&(a)->refcount, 1)
#define kref_get(a) atomic_inc(&(a)->refcount)
#define kref_put(a,b) if (atomic_dec_and_test(&(a)->refcount)) b(a)

/******************************************************************************
 * string
 *****************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define kstrdup(a,b) strdup((a))
#define kstrndup(a,b,c) strndup((a), (b))

static inline int
kstrtol(const char *s, unsigned int base, long *res)
{
	*res = strtol(s, NULL, base);
	if (errno && *res == LONG_MAX)
		return errno;
	if (errno && *res == 0)
		return errno;
	return 0;
}

static inline int
kstrtoul(const char *s, unsigned int base, unsigned long *res)
{
	*res = strtoul(s, NULL, base);
	if (errno && *res == ULONG_MAX)
		return errno;
	if (errno && *res == 0)
		return errno;
	return 0;
}

/******************************************************************************
 * printk
 *****************************************************************************/
#include <stdarg.h>
#include <stdio.h>

#define KERN_CRIT ""
#define KERN_ERR ""
#define KERN_WARNING ""
#define KERN_INFO ""
#define KERN_DEBUG ""

#define printk printf
#define vprintk vprintf

/******************************************************************************
 * memory
 *****************************************************************************/
#define GFP_KERNEL 1
#define __GFP_ZERO 2
#define GFP_DMA32  4

typedef unsigned gfp_t;

#define vmalloc malloc
#define vfree free
#define kmalloc(a,b) malloc((a))
#define kzalloc(a,b) calloc(1, (a))
#define kcalloc(a,b,c) calloc((a), (b))
#define kfree free

#define vzalloc(a) calloc(1, (a))
#define vfree free

static inline void *
kmemdup(const void *src, size_t len, gfp_t gfp)
{
	void *dst = malloc(len);
	if (dst)
		memcpy(dst, src, len);
	return dst;
}

struct page {
};

static inline struct page *
alloc_page(gfp_t gfp)
{
	return NULL;
}

static inline void
__free_page(struct page *page)
{
}

static inline dma_addr_t
page_to_phys(struct page *page)
{
	return 0;
}

static inline struct page *
pfn_to_page(dma_addr_t pfn)
{
	return NULL;
}

static inline dma_addr_t
page_to_pfn(struct page *page)
{
	return 0;
}

static inline unsigned long
get_num_physpages(void)
{
	return 0;
}

/******************************************************************************
 * assertions
 *****************************************************************************/
#include <assert.h>
#include <execinfo.h>

static inline void
nvos_backtrace(void)
{
	void **buffer;
	char **sinfo;
	int ninfo, i;

	buffer = malloc(sizeof(*buffer) * 512);
	ninfo = backtrace(buffer, 512);
	sinfo = backtrace_symbols(buffer, ninfo);
	if (sinfo) {
		for (i = 0; i < ninfo; i++)
			printf("%s\n", sinfo[i]);
		free(sinfo);
	}
	free(buffer);
}

#define BUG() do {                                                             \
	printk("BUG at %s:%d/%s()!\n\n", __FILE__, __LINE__, __func__);        \
	nvos_backtrace();                                                      \
	assert(0);                                                             \
} while(0)

#define BUG_ON(c) do { if ((c)) BUG(); } while(0)

#define WARN() do {                                                            \
	printk("WARNING at %s:%d/%s()!\n\n", __FILE__, __LINE__, __func__);    \
	nvos_backtrace();                                                      \
} while(0)

#define WARN_ON(c) ({                                                          \
	int _ret = !!(c);                                                      \
	if (_ret)                                                              \
		WARN();                                                        \
	_ret;                                                                  \
})

#define WARN_ON_ONCE(c) ({                                                     \
	static int _once = 1;                                                  \
	int _ret = !!(c);                                                      \
	if (_ret && _once) {                                                   \
		WARN();                                                        \
		_once = 0;                                                     \
	}                                                                      \
	_ret;                                                                  \
})

/******************************************************************************
 * interrupts
 *****************************************************************************/
#define IRQF_SHARED 0

typedef enum irqreturn {
	IRQ_NONE,
	IRQ_HANDLED
} irqreturn_t;

typedef irqreturn_t (*irq_handler_t)(int, void *);

extern int  os_intr_init(unsigned int, irq_handler_t, unsigned long,
			 const char *, void *);
extern void os_intr_free(unsigned int, void *);

#define request_irq os_intr_init
#define free_irq os_intr_free

/******************************************************************************
 * spinlocks - using pthread mutex due to no static spinlock initialiser
 *****************************************************************************/
#include <pthread.h>

typedef struct spinlock_t {
	pthread_mutex_t lock;
} spinlock_t;

#define DEFINE_SPINLOCK(a) spinlock_t a = { .lock = PTHREAD_MUTEX_INITIALIZER }

#define spin_lock_init(a) do {                                                 \
	(a)->lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;                \
} while(0)
#define spin_lock(a) pthread_mutex_lock(&(a)->lock)
#define spin_unlock(a) pthread_mutex_unlock(&(a)->lock)
#define spin_lock_irqsave(a,b) do { (b) = 1; spin_lock((a)); } while (0)
#define spin_unlock_irqrestore(a,b) do { (void)(b); spin_unlock((a)); } while (0)
#define spin_is_locked(a) ({ \
	int _ret = pthread_mutex_trylock(&(a)->lock) != 0; \
	if (_ret == 0) \
		pthread_mutex_unlock(&(a)->lock); \
	_ret; \
})
#define assert_spin_locked(a) BUG_ON(!spin_is_locked(a))

/******************************************************************************
 * rwlocks
 *****************************************************************************/
typedef struct rwlock_t {
	pthread_rwlock_t lock;
} rwlock_t;

#define rwlock_init(a) pthread_rwlock_init(&(a)->lock, NULL)
#define read_lock(a) pthread_rwlock_rdlock(&(a)->lock)
#define read_unlock(a) pthread_rwlock_unlock(&(a)->lock)
#define write_lock_irq(a) pthread_rwlock_wrlock(&(a)->lock)
#define write_unlock_irq(a) pthread_rwlock_unlock(&(a)->lock)

/******************************************************************************
 * mutexes
 *****************************************************************************/
struct mutex {
	pthread_mutex_t mutex;
};

#define DEFINE_MUTEX(a) struct mutex a = { .mutex = PTHREAD_MUTEX_INITIALIZER }
#define mutex_init(a) pthread_mutex_init(&(a)->mutex, NULL)
#define mutex_lock(a) pthread_mutex_lock(&(a)->mutex)
#define mutex_unlock(a) pthread_mutex_unlock(&(a)->mutex)

/******************************************************************************
 * lockdep
 *****************************************************************************/
struct lock_class_key {
};

#define __mutex_init(a,b,c) do {                                               \
	struct lock_class_key *__key = (c); (void)__key;                       \
	pthread_mutex_init(&(a)->mutex, NULL);                                 \
} while(0)

/******************************************************************************
 * lists
 *****************************************************************************/
#include "list.h"

/******************************************************************************
 * rbtree
 *****************************************************************************/
struct rb_root {
	struct rb_node *rb_node;
};

#define RB_ROOT (struct rb_root) {}

struct rb_node {
	struct rb_node *parent;
	struct rb_node *rb_left;
	struct rb_node *rb_right;
};

#define RB_EMPTY_NODE(a) ((a)->parent == (a))
#define RB_CLEAR_NODE(a) ((a)->parent = (a))

void rb_link_node(struct rb_node *, struct rb_node *, struct rb_node **);
void rb_insert_color(struct rb_node *, struct rb_root *);
void rb_erase(struct rb_node *, struct rb_root *);

/******************************************************************************
 * io space
 *****************************************************************************/
#define __iomem

void __iomem *nvos_ioremap(u64 addr, u64 size);
void  nvos_iounmap(void __iomem *ptr);

#define ioremap(a,b) nvos_ioremap((a), (b))
#define iounmap(a) nvos_iounmap((a))

#define ioread8(a) *((volatile u8 *)(a))
#define ioread16(a) *((volatile u16 *)(a))
#define ioread16_native ioread16
#define ioread32(a) *((volatile u32 *)(a))
#define ioread32_native ioread32

#define iowrite8(b,a) *((volatile u8 *)(a)) = (b)
#define iowrite16(b,a) *((volatile u16 *)(a)) = (b)
#define iowrite16_native iowrite16
#define iowrite32(b,a) *((volatile u32 *)(a)) = (b)
#define iowrite32_native iowrite32

#define memset_io memset
#define memcpy_fromio memcpy
#define memcpy_toio memcpy
#define wmb()

static inline int
arch_phys_wc_add(u64 base, u64 size)
{
	return -1;
}

static inline void
arch_phys_wc_del(int index)
{
}

/******************************************************************************
 * io mapping
 *****************************************************************************/
struct io_mapping {
};

static inline struct io_mapping *
io_mapping_create_wc(u64 addr, u64 size)
{
	return NULL;
}

static inline void
io_mapping_free(struct io_mapping *io)
{
}

static inline void __iomem *
io_mapping_map_atomic_wc(struct io_mapping *io, u32 offset)
{
	return NULL;
}

static inline void
io_mapping_unmap_atomic(void __iomem *p)
{
}

/******************************************************************************
 * DMI
 *****************************************************************************/
struct dmi_strmatch {
};

struct dmi_system_id {
	const char *ident;
	struct dmi_strmatch matches[4];
};

#define DMI_MATCH(a, b) { }

static inline int
dmi_check_system(const struct dmi_system_id *list)
{
	return 0;
}

/******************************************************************************
 * Device
 *****************************************************************************/
struct device_driver {
};

struct device {
	struct device_driver *driver;
	char name[64];
};

#define dev_name(d) (d)->name
#define dev_crit(d,f,a...)   printf("nouveau %s: "f, dev_name(d), ##a)
#define dev_err(d,f,a...)    dev_crit((d), f, ##a)
#define dev_warn(d,f,a...)   dev_crit((d), f, ##a)
#define dev_notice(d,f,a...) dev_crit((d), f, ##a)
#define dev_info(d,f,a...)   dev_crit((d), f, ##a)

/******************************************************************************
 * PM runtime
 *****************************************************************************/
static inline void
pm_runtime_mark_last_busy(struct device *dev)
{
}

/******************************************************************************
 * ioport
 *****************************************************************************/
struct resource {
	resource_size_t start;
	resource_size_t end;
};

#define IORESOURCE_MEM 1

static inline resource_size_t
resource_size(const struct resource *res)
{
	return res->end - res->start + 1;
}

/******************************************************************************
 * OpenFirmware
 *****************************************************************************/

struct device_node {
};

static inline const void *
of_get_property(const struct device_node *dn, const char *name, int *lenp)
{
	return NULL;
}

/******************************************************************************
 * dma-mapping
 *****************************************************************************/

static inline void *
dma_alloc_coherent(struct device *dev, size_t sz, dma_addr_t *hdl, gfp_t gfp)
{
	return NULL;
}

static inline void
dma_free_coherent(struct device *dev, size_t sz, void *vaddr, dma_addr_t bus)
{
}

enum dma_attr {
	DMA_ATTR_WRITE_BARRIER,
	DMA_ATTR_WEAK_ORDERING,
	DMA_ATTR_WRITE_COMBINE,
	DMA_ATTR_NON_CONSISTENT,
	DMA_ATTR_NO_KERNEL_MAPPING,
	DMA_ATTR_SKIP_CPU_SYNC,
	DMA_ATTR_FORCE_CONTIGUOUS,
	DMA_ATTR_MAX,
};

struct dma_attrs {
};

static inline void init_dma_attrs(struct dma_attrs *attrs) {}
static inline void dma_set_attr(enum dma_attr attr, struct dma_attrs *attrs) {}

static inline void *
dma_alloc_attrs(struct device *dev, size_t sz, dma_addr_t *hdl, gfp_t gfp,
		struct dma_attrs *attrs)
{
	return NULL;
}

static inline void
dma_free_attrs(struct device *dev, size_t sz, void *vaddr, dma_addr_t bus,
	       struct dma_attrs *attrs)
{
}

/******************************************************************************
 * Bus
 *****************************************************************************/

struct bus_type {
};

extern struct bus_type platform_bus_type;

/******************************************************************************
 * IOMMU
 *****************************************************************************/
struct iommu_domain;

struct iommu_ops {
	unsigned long pgsize_bitmap;
};

struct iommu_domain {
	const struct iommu_ops *ops;
};

#define IOMMU_READ     (1 << 0)
#define IOMMU_WRITE    (1 << 1)
#define IOMMU_CACHE    (1 << 2) /* DMA cache coherency */
#define IOMMU_NOEXEC   (1 << 3)

static inline bool
iommu_present(struct bus_type *bus)
{
	return false;
}

static inline struct iommu_domain *
iommu_domain_alloc(struct bus_type *bus)
{
	return NULL;
}

static inline void
iommu_domain_free(struct iommu_domain *domain)
{
}

static inline int
iommu_attach_device(struct iommu_domain *domain, struct device *dev)
{
	return -ENOSYS;
}

static inline void
iommu_detach_device(struct iommu_domain *domain, struct device *dev)
{
}

static inline int
iommu_map(struct iommu_domain *domain, unsigned long iova, dma_addr_t paddr,
	  size_t size, int prot)
{
	return 0;
}

static inline size_t
iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
{
	return 0;
}

/******************************************************************************
 * PCI
 *****************************************************************************/
#include <pciaccess.h>

#define DMA_BIDIRECTIONAL 1

#define PCI_CAP_ID_AGP 0x02

struct pci_bus {
	u16 domain;
	u8  number;
};

struct pci_dev {
	struct pci_device *pdev;
	struct device dev;
	u16 vendor;
	u16 device;
	u16 subsystem_vendor;
	u16 subsystem_device;
	int irq;
	struct pci_bus _bus;
	struct pci_bus *bus;
	u8 devfn;
};

static inline void
pci_disable_device(struct pci_dev *pdev)
{
}

static inline int
pci_enable_device(struct pci_dev *pdev)
{
	return 0;
}

static inline void
pci_set_master(struct pci_dev *pdev)
{
}

static inline struct device_node *
pci_device_to_OF_node(const struct pci_dev *pdev)
{
	return NULL;
}

static inline int
pci_find_capability(struct pci_dev *pdev, int cap)
{
	return 0;
}

static inline bool
pci_is_pcie(struct pci_dev *pdev)
{
	return false;
}

static inline int
pci_write_config_byte(struct pci_dev *pdev, int addr, u8 data)
{
	return pci_device_cfg_write_u8(pdev->pdev, data, addr);
}

static inline u64
pci_resource_start(struct pci_dev *pdev, int bar)
{
	return pdev->pdev->regions[bar].base_addr;
}

static inline u64
pci_resource_len(struct pci_dev *pdev, int bar)
{
	return pdev->pdev->regions[bar].size;
}

static inline dma_addr_t
dma_map_page(struct device *pdev, struct page *page, int offset,
	     int length, unsigned flags)
{
	return 0;
}


static inline bool
dma_mapping_error(struct device *pdev, dma_addr_t addr)
{
	return true;
}

static inline void
dma_unmap_page(struct device *pdev, dma_addr_t addr, int size, unsigned flags)
{
}

static inline int
pci_enable_rom(struct pci_dev *pdev)
{
	return 0;
}

static inline void
pci_disable_rom(struct pci_dev *pdev)
{
}

static inline void __iomem *
pci_map_rom(struct pci_dev *pdev, size_t *size)
{
	void *buf;
	*size = pdev->pdev->rom_size;
	buf = malloc(*size);
	if (buf) {
		if (pci_device_read_rom(pdev->pdev, buf)) {
			free(buf);
			buf = NULL;
		}
	}
	return buf;
}

static inline void
pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
{
	free(rom);
}

static inline void __iomem *
pci_platform_rom(struct pci_dev *pdev, size_t *size)
{
	return NULL;
}

static inline void *
pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
		     dma_addr_t *dma_handle)
{
	return NULL;
}

static inline void
pci_free_consistent(struct pci_dev *hwdev, size_t size,
		    void *vaddr, dma_addr_t dma_handle)
{
}

static inline int
pci_enable_msi(struct pci_dev *pdev)
{
	return -ENOSYS;
}

static inline void
pci_disable_msi(struct pci_dev *pdev)
{
}

#define PCI_DEVFN(a,b) (((a) << 3) | (b))
#define PCI_SLOT(a) ((a) >> 3)
#define PCI_FUNC(a) ((a) & 0x07)
#define pci_domain_nr(a) (a)->domain
#define pci_get_bus_and_slot(a, b) NULL
#define pci_read_config_dword(a,b,c) *(c) = 0

#define PCI_ANY_ID (~0)

#define PCI_VENDOR_ID_SI 0x1039
#define PCI_VENDOR_ID_NVIDIA 0x10de
#define PCI_VENDOR_ID_VIA 0x1106

/******************************************************************************
 * AGP
 *****************************************************************************/
#define PCI_AGP_COMMAND_FW 0x10

#ifdef CONFIG_AGP
struct agp_bridge_data {
};

struct agp_kern_info {
	struct pci_dev *device;
	unsigned long mode;
	unsigned long aper_base;
	size_t aper_size;
	bool cant_use_aperture;
};

static inline struct agp_bridge_data *
agp_backend_acquire(struct pci_dev *pdev)
{
	return NULL;
}

static inline void
agp_backend_release(struct agp_bridge_data *bridge)
{
}

static inline int
agp_copy_info(struct agp_bridge_data *bridge, struct agp_kern_info *info)
{
	return -ENOSYS;
}

static inline void
agp_enable(struct agp_bridge_data *bridge, u32 mode)
{
}
#endif

/******************************************************************************
 * platform device
 *****************************************************************************/

struct platform_device {
	struct device dev;
	u64 id;
};

static inline struct resource *
platform_get_resource(struct platform_device *pdev, unsigned int type,
		      unsigned int num)
{
	return NULL;
}

static inline int
platform_get_irq_byname(struct platform_device *pdev, const char *name)
{
	return -1;
}

/******************************************************************************
 * sg table
 *****************************************************************************/
struct scatterlist {
};

struct sg_table {
	struct scatterlist *sgl;
	unsigned int nents;
};

#define for_each_sg(sglist, sg, nr, __i)	                               \
	for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
#define sg_next(a) (a)
#define sg_dma_address(a) 0ULL
#define sg_dma_len(a) 0ULL

/******************************************************************************
 * firmware
 *****************************************************************************/
#include <unistd.h>
#include <fcntl.h>

struct firmware {
	size_t size;
	void *data;
};

static inline int
request_firmware(const struct firmware **pfw, const char *name,
		 struct device *dev)
{
	struct firmware *fw = *(void **)pfw = malloc(sizeof(*fw));
	int fd = open(name, O_RDONLY);
	if (fd >= 0) {
		off_t len = lseek(fd, 0, SEEK_END);
		fw->data = malloc(len);
		fw->size = pread(fd, fw->data, len, 0);
		return 0;
	}
	free(fw);
	return -EINVAL;
}

static inline void
release_firmware(const struct firmware *fw)
{
	free(fw->data);
	free((void *)fw);
}

/******************************************************************************
 * workqueues
 *****************************************************************************/
struct work_struct {
	union {
		void (*func)(struct work_struct *);
		void (*exec)(void *);
	};
	struct nvos_work *nvos;
};

#define INIT_WORK(a,b) ((a)->func = (b), (a)->nvos = NULL)
#define schedule_work(a) BUG_ON(!nvos_work_init((a)->exec, (a), &(a)->nvos))
#define flush_work(a) nvos_work_fini(&(a)->nvos)

bool nvos_work_init(void (*)(void *), void *, struct nvos_work **);
void nvos_work_fini(struct nvos_work **);

/******************************************************************************
 * waitqueues
 *****************************************************************************/
typedef struct __wait_queue_head {
} wait_queue_head_t;

#define init_waitqueue_head(wq) (void)(wq)
#define wake_up(wq) (void)(wq)
#define wake_up_all(wq) (void)(wq)

#define wait_event(wq,cond) do {                                               \
	(void)(wq);                                                            \
	usleep(1);                                                             \
} while (!(cond))

#define wait_event_interruptible(wq,cond) ({                                   \
	wait_event((wq), (cond)); 0;                                           \
})

#define wait_event_timeout(wq,cond,jiffies) ({                                 \
	unsigned long _t = (jiffies) / 10000;                                  \
	(void)(wq);                                                            \
	do {                                                                   \
		if (cond)                                                      \
			break;                                                 \
		usleep(10);                                                    \
	} while (--_t > 0);                                                    \
	_t;                                                                    \
})

#define wait_event_interruptible_timeout(wq,cond,jiffies)                      \
	wait_event_timeout((wq), (cond), (jiffies))

/******************************************************************************
 * i2c
 *****************************************************************************/
struct i2c_adapter {
	struct {
		void *parent;
	} dev;
	const struct i2c_algorithm *algo;
	char name[48];
	void *algo_data;
	void *adapdata;
	int owner;
};

#define I2C_M_RD 1

struct i2c_msg {
	u16 addr;
	u16 flags;
	u16 len;
	u8 *buf;
};

#define I2C_FUNC_I2C 1
#define I2C_FUNC_SMBUS_EMUL 2

struct i2c_algorithm {
	int (*master_xfer)(struct i2c_adapter *, struct i2c_msg *, int);
	u32 (*functionality)(struct i2c_adapter *);
};

struct i2c_board_info {
	char type[64];
	unsigned short addr;
};

#define I2C_BOARD_INFO(a,b) .type = (a), .addr = (b)

struct i2c_client {
	struct device dev;
};

struct i2c_driver {
	struct device_driver driver;
	int (*detect)(struct i2c_client *, struct i2c_board_info *);
};

static inline struct i2c_driver *
to_i2c_driver(struct device_driver *driver)
{
	return (void *)driver;
}

static inline struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
	return NULL;
}

static inline void
i2c_unregister_device(struct i2c_client *client)
{
}

static inline int
i2c_add_adapter(struct i2c_adapter *a)
{
	return 0;
}

static inline int
i2c_del_adapter(struct i2c_adapter *a)
{
	return 0;
}

static inline void *
i2c_get_adapdata(const struct i2c_adapter *a)
{
	return a->adapdata;
}

static inline void
i2c_set_adapdata(struct i2c_adapter *a, void *d)
{
	a->adapdata = d;
}

static inline int
i2c_transfer(struct i2c_adapter *a, struct i2c_msg *m, int num)
{
	if (a->algo)
		return a->algo->master_xfer(a, m, num);
	return -ENODEV;
}

/******************************************************************************
 * i2c bit-bang
 *****************************************************************************/

struct i2c_algo_bit_data {
	int udelay;
	unsigned long timeout;
	void *data;
	void (*setsda) (void *data, int state);
	void (*setscl) (void *data, int state);
	int  (*getsda) (void *data);
	int  (*getscl) (void *data);
	int  (*pre_xfer)(struct i2c_adapter *);
	void (*post_xfer)(struct i2c_adapter *);
};

static inline int
i2c_bit_add_bus(struct i2c_adapter *a)
{
	return 0;
}

extern const struct i2c_algorithm i2c_bit_algo;

/******************************************************************************
 * delay
 *****************************************************************************/
#include <unistd.h>

#define udelay(a) usleep((a))
#define mdelay(a) usleep((a) * 1000)
#define msleep(a) usleep((a) * 1000)
#define usleep_range(a,b) usleep((a))

/******************************************************************************
 * reboot
 *****************************************************************************/
static inline int
orderly_poweroff(bool force)
{
	BUG_ON(1);
}

/******************************************************************************
 * power supply
 *****************************************************************************/
static inline int
power_supply_is_system_supplied(void)
{
	return -ENOSYS;
}

/******************************************************************************
 * notifier
 *****************************************************************************/
struct notifier_block {
};

/******************************************************************************
 * clk
 *****************************************************************************/
struct clk {
};

static inline struct clk *
clk_get(struct device *dev, const char *id)
{
	return NULL;
}

static inline unsigned long
clk_get_rate(struct clk *clk)
{
	return 0;
}

static inline int
clk_set_rate(struct clk *clk, unsigned long rate)
{
	return -ENOSYS;
}

static inline int
clk_prepare_enable(struct clk *clk)
{
	return -ENOSYS;
}

static inline void
clk_disable_unprepare(struct clk *clk)
{
}

/******************************************************************************
 * regulator
 *****************************************************************************/

struct regulator {
};

static inline int
regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
{
	return -ENOSYS;
}

static inline int
regulator_get_voltage(struct regulator *regulator)
{
	return -ENOSYS;
}

static inline int
regulator_enable(struct regulator *regulator)
{
	return -ENOSYS;
}

static inline int
regulator_disable(struct regulator *regulator)
{
	return -ENOSYS;
}

static inline struct regulator *
regulator_get(struct device *dev, const char *id)
{
	return NULL;
}

/******************************************************************************
 * reset
 *****************************************************************************/

struct reset_control {
};

static inline struct reset_control *
reset_control_get(struct device *dev, const char *id)
{
	return NULL;
}

static inline int
reset_control_assert(struct reset_control *reset)
{
	return -ENOSYS;
}

static inline int
reset_control_deassert(struct reset_control *reset)
{
	return -ENOSYS;
}

/******************************************************************************
 * devres
 *****************************************************************************/

#define devm_clk_get clk_get
#define devm_regulator_get regulator_get
#define devm_reset_control_get reset_control_get

/******************************************************************************
 * tegra
 *****************************************************************************/
struct tegra_sku_info {
	int gpu_speedo_value;
};

extern struct tegra_sku_info tegra_sku_info;

#define TEGRA_POWERGATE_3D 1

static inline int
tegra_powergate_remove_clamping(int id)
{
	return -ENOSYS;
}

/******************************************************************************
 * endianness (cont'd)
 *****************************************************************************/

#if __BYTE_ORDER == __LITTLE_ENDIAN
#undef __BIG_ENDIAN
#endif

#endif