summaryrefslogtreecommitdiff
path: root/src/cmem/module/cmemk.c
blob: 9aa801674da4d8ec7953ae064ed3473b243ed0a1 (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
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
/*
 *  Copyright (C) 2007-2014 Texas Instruments Incorporated - http://www.ti.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
/*
 * cmemk.c
 */
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/dma-contiguous.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/moduleparam.h>
#include <linux/list.h>
#include <linux/cdev.h>
#include <linux/proc_fs.h>
#include <linux/mm.h>
#include <linux/seq_file.h>
#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <asm/cacheflush.h>
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <asm/io.h>

#include <linux/version.h>

#include <ti/cmem.h>

#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>

/*
 * USE_MMAPSEM means acquire/release current->mm->mmap_sem around calls
 * to dma_[flush/clean/inv]_range.
 */
//#define USE_MMAPSEM

/*
 * CHECK_FOR_ALLOCATED_BUFFER means ensure that the passed addr/size block
 * is actually an allocated, CMEM-defined buffer.
 */
//#define CHECK_FOR_ALLOCATED_BUFFER

/* HEAP_ALIGN is used in place of sizeof(HeapMem_Header) */
#define HEAP_ALIGN PAGE_SIZE


#ifdef __DEBUG
#define __D(fmt, args...) printk(KERN_DEBUG "CMEMK Debug: " fmt, ## args)
#else
#define __D(fmt, args...)
#endif

#define __E(fmt, args...) printk(KERN_ERR "CMEMK Error: " fmt, ## args)

#define MAXTYPE(T) ((T) (((T)1 << ((sizeof(T) * 8) - 1) ^ ((T) -1))))

/*
 * Change here for supporting more than 4 blocks.  Also change all
 * NBLOCKS-based arrays to have NBLOCKS-worth of initialization values.
 */
#define NBLOCKS 4

#define BLOCK_IOREMAP    (1 << 0)
#define BLOCK_MEMREGION  (1 << 1)
#define BLOCK_REGION     (1 << 2)

#ifndef VM_RESERVED
#define VM_RESERVED 0x00080000
#endif

static struct vm_struct *ioremap_area;
static unsigned int nblocks = 0;
static unsigned int block_flags[NBLOCKS] = {0, 0, 0, 0};
static unsigned long long block_start[NBLOCKS] = {0, 0, 0, 0};
static unsigned long long block_end[NBLOCKS] = {0, 0, 0, 0};
static unsigned long long block_avail_size[NBLOCKS] = {0, 0, 0, 0};
static unsigned int total_num_buffers[NBLOCKS] = {0, 0, 0, 0};
static int pool_num_buffers[NBLOCKS][MAX_POOLS];
static unsigned long long pool_size[NBLOCKS][MAX_POOLS];

static int cmem_major;
static struct proc_dir_entry *cmem_proc_entry;
static atomic_t reference_count = ATOMIC_INIT(0);
static unsigned int version = CMEM_VERSION;

static struct class *cmem_class;

/* Register the module parameters. */
MODULE_PARM_DESC(phys_start, "\n\t\t Start Address for CMEM Pool Memory");
static char *phys_start = NULL;
MODULE_PARM_DESC(phys_end, "\n\t\t End Address for CMEM Pool Memory");
static char *phys_end = NULL;
module_param(phys_start, charp, S_IRUGO);
module_param(phys_end, charp, S_IRUGO);

static int npools[NBLOCKS + 1] = {0, 0, 0, 0, 0};

static char *pools[MAX_POOLS] = {
    NULL
};
MODULE_PARM_DESC(pools,
    "\n\t\t List of Pool Sizes and Number of Entries, comma separated,"
    "\n\t\t decimal sizes");
module_param_array(pools, charp, &npools[0], S_IRUGO);

/* begin block 1 */
MODULE_PARM_DESC(phys_start_1, "\n\t\t Start Address for Extended CMEM Pool Memory");
static char *phys_start_1 = NULL;
MODULE_PARM_DESC(phys_end_1, "\n\t\t End Address for Extended CMEM Pool Memory");
static char *phys_end_1 = NULL;
module_param(phys_start_1, charp, S_IRUGO);
module_param(phys_end_1, charp, S_IRUGO);

static char *pools_1[MAX_POOLS] = {
    NULL
};
MODULE_PARM_DESC(pools_1,
    "\n\t\t List of Pool Sizes and Number of Entries, comma separated,"
    "\n\t\t decimal sizes, for Extended CMEM Pool");
module_param_array(pools_1, charp, &npools[1], S_IRUGO);
/* end block 1 */

/* begin block 2 */
MODULE_PARM_DESC(phys_start_2, "\n\t\t Start Address for Extended CMEM Pool Memory");
static char *phys_start_2 = NULL;
MODULE_PARM_DESC(phys_end_2, "\n\t\t End Address for Extended CMEM Pool Memory");
static char *phys_end_2 = NULL;
module_param(phys_start_2, charp, S_IRUGO);
module_param(phys_end_2, charp, S_IRUGO);

static char *pools_2[MAX_POOLS] = {
    NULL
};
MODULE_PARM_DESC(pools_2,
    "\n\t\t List of Pool Sizes and Number of Entries, comma separated,"
    "\n\t\t decimal sizes, for Extended CMEM Pool");
module_param_array(pools_2, charp, &npools[2], S_IRUGO);
/* end block 2 */

/* cut-and-paste below as part of adding support for more than 4 blocks */
/* begin block 3 */
MODULE_PARM_DESC(phys_start_3, "\n\t\t Start Address for Extended CMEM Pool Memory");
static char *phys_start_3 = NULL;
MODULE_PARM_DESC(phys_end_3, "\n\t\t End Address for Extended CMEM Pool Memory");
static char *phys_end_3 = NULL;
module_param(phys_start_3, charp, S_IRUGO);
module_param(phys_end_3, charp, S_IRUGO);

static char *pools_3[MAX_POOLS] = {
    NULL
};
MODULE_PARM_DESC(pools_3,
    "\n\t\t List of Pool Sizes and Number of Entries, comma separated,"
    "\n\t\t decimal sizes, for Extended CMEM Pool");
module_param_array(pools_3, charp, &npools[3], S_IRUGO);
/* end block 3 */
/* cut-and-paste above as part of adding support for more than 4 blocks */

static int allowOverlap = -1;
MODULE_PARM_DESC(allowOverlap,
    "\n\t\t DEPRECATED - ignored if found"
    "\n\t\t Set to 1 if cmem range is allowed to overlap memory range"
    "\n\t\t allocated to kernel physical mem (via mem=xxx)");
module_param(allowOverlap, int, S_IRUGO);

static int useHeapIfPoolUnavailable = 0;
MODULE_PARM_DESC(useHeapIfPoolUnavailable,
    "\n\t\t Set to 1 if you want a pool-based allocation request to"
    "\n\t\t fall back to a heap-based allocation attempt");
module_param(useHeapIfPoolUnavailable, int, S_IRUGO);

static struct mutex cmem_mutex;

/* Describes a pool buffer */
typedef struct pool_buffer {
    struct list_head element;
    struct list_head users;
    dma_addr_t dma;             /* used only for CMA-based allocs */
    int id;
    phys_addr_t physp;
    int flags;			/* CMEM_CACHED or CMEM_NONCACHED */
    void *kvirtp;		/* used only for CMA-based allocs */
    unsigned long long size;	/* used only for heap-based allocs */
    struct device *dev;		/* used only for CMA-based allocs */
} pool_buffer;

typedef struct registered_user {
    struct list_head element;
    struct file *filp;
} registered_user;

#ifdef CMEM_KERNEL_STUB

#include <linux/cmemk_stub.h>

typedef struct pool_object pool_object;

#else

/* Describes a pool */
typedef struct pool_object {
    struct list_head freelist;
    struct list_head busylist;
    unsigned int numbufs;
    unsigned long long size;
    unsigned long long reqsize;
} pool_object;

static int cmem_cma_npools = 0;
static int cmem_cma_heapsize = 0;
static struct device *cmem_cma_dev;
static struct pool_object *cmem_cma_p_objs;
#if IS_ENABLED(CONFIG_ARCH_KEYSTONE) && IS_ENABLED(CONFIG_ARM_LPAE) \
	&& (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
static struct device *cmem_cma_dev_0;
#define KEYSTONE_DMA_PFN_OFFSET 0x780000UL
#endif
#endif

/*
 * For CMA allocations we treat p_objs[NBLOCKS] as a special "pool" array.
 */
static pool_object p_objs[NBLOCKS + 1][MAX_POOLS];


/* Forward declaration of system calls */
static long ioctl(struct file *filp, unsigned int cmd, unsigned long args);
static int mmap(struct file *filp, struct vm_area_struct *vma);
static int open(struct inode *inode, struct file *filp);
static int release(struct inode *inode, struct file *filp);

static struct file_operations cmem_fxns = {
    owner:   THIS_MODULE,
    unlocked_ioctl: ioctl,
    mmap:    mmap,
    open:    open,
    release: release
};


/*
 *  NOTE: The following implementation of a heap is taken from the
 *  DSP/BIOS 6.0 source tree (avala-f15/src/ti/sysbios/heaps).  Changes
 *  are necessary due to the fact that CMEM is not built in an XDC
 *  build environment, and therefore XDC types and helper APIs (e.g.,
 *  Assert) are not available.  However, these changes were kept to a
 *  minimum.
 *
 *  The changes include:
 *	- renaming XDC types to standard C types
 *	- replacing sizeof(HeapMem_Header) w/ HEAP_ALIGN throughout
 *
 *  As merged with CMEM, the heap becomes a distinguished "pool" and
 *  is sometimes treated specially, and at other times can be treated
 *  as a normal pool instance.
 */

/*
 * HeapMem compatibility stuff
 */
typedef struct HeapMem_Header {
    phys_addr_t next;
    size_t size;
} HeapMem_Header;

#define ALLOCRUN 0
#define DRYRUN 1

phys_addr_t HeapMem_alloc(int bi, size_t size, size_t align, int dryrun);
void HeapMem_free(int bi, phys_addr_t block, size_t size);

/*
 * Heap configuration stuff
 *
 * For CMA global heap allocations, we treat heap_pool[NBLOCKS] as
 * its own block.  For example, if you have 4 physically-specified
 * blocks then NBLOCKS = 4.  heap_pool[0]|[1]|[2]|[3] are the real blocks, and
 * heap_pool[4] represents the global CMA area.
 *
 * Only heap_pool[] gets extended with NBLOCKS + 1 dimension, since the
 * other heap_*[] arrays are used only with the real blocks.  You can't
 * use heap_pool[NBLOCKS] for HeapMem_alloc().
 */
static int heap_pool[NBLOCKS + 1] = {-1, -1, -1, -1, 0};

static unsigned long heap_size[NBLOCKS] = {0, 0, 0, 0};
static phys_addr_t heap_physp[NBLOCKS] = {0, 0, 0, 0};
static HeapMem_Header heap_head[NBLOCKS] = {
    {
	0,	/* next */
	0	/* size */
    },
    {
	0,	/* next */
	0	/* size */
    },
    {
	0,	/* next */
	0	/* size */
    },
/* cut-and-paste below as part of adding support for more than 4 blocks */
    {
	0,	/* next */
	0	/* size */
    },
/* cut-and-paste above as part of adding support for more than 4 blocks */
};

static int map_header(void **vaddrp, phys_addr_t physp, struct vm_struct **vm)
{
    unsigned long vaddr;

    *vm = __get_vm_area(PAGE_SIZE, VM_IOREMAP, VMALLOC_START, VMALLOC_END);
    if (!*vm) {
	__E("__get_vm_area() failed\n");

	return -ENOMEM;
    }

    vaddr = (unsigned long)(*vm)->addr;
    ioremap_page_range((unsigned long)vaddr, (unsigned long)vaddr + PAGE_SIZE,
                       physp, PAGE_KERNEL);
    *vaddrp = (*vm)->addr;

    __D("map_header: ioremap_page_range(%#llx, %#lx)=0x%p\n",
        (unsigned long long)physp, PAGE_SIZE, *vaddrp);

    return 0;
}

static void unmap_header(void *vaddr, struct vm_struct *vm)
{
    __D("unmap_header: unmap_kernel_page_rage(0x%p, %#lx)\n", vaddr, PAGE_SIZE);

    unmap_kernel_range_noflush((unsigned long)vaddr, PAGE_SIZE);
    free_vm_area(vm);
}

/*
 *  ======== HeapMem_alloc ========
 *  HeapMem is implemented such that all of the memory and blocks it works
 *  with have an alignment that is a multiple of HEAP_ALIGN and have a size
 *  which is a multiple of HEAP_ALIGN. Maintaining this requirement
 *  throughout the implementation ensures that there are never any odd
 *  alignments or odd block sizes to deal with.
 *
 *  Specifically:
 *  The buffer managed by HeapMem:
 *    1. Is aligned on a multiple of HEAP_ALIGN
 *    2. Has an adjusted size that is a multiple of HEAP_ALIGN
 *  All blocks on the freelist:
 *    1. Are aligned on a multiple of HEAP_ALIGN
 *    2. Have a size that is a multiple of HEAP_ALIGN
 *  All allocated blocks:
 *    1. Are aligned on a multiple of HEAP_ALIGN
 *    2. Have a size that is a multiple of HEAP_ALIGN
 *
 */
phys_addr_t HeapMem_alloc(int bi, size_t reqSize, size_t reqAlign, int dryrun)
{
    struct vm_struct *curHeader_vm_area;
    struct vm_struct *prevHeader_vm_area;
    struct vm_struct *newHeader_vm_area;
    HeapMem_Header *curHeader;
    HeapMem_Header *prevHeader;
    HeapMem_Header *newHeader;
    phys_addr_t curHeaderPhys;
    phys_addr_t prevHeaderPhys = 0;
    phys_addr_t newHeaderPhys = 0;  /* init to quiet compiler */
    phys_addr_t allocAddr;
    size_t curSize, adjSize;
    size_t remainSize;  /* free memory after allocated memory */
    size_t adjAlign, offset;

    adjSize = reqSize;

    /* Make size requested a multiple of HEAP_ALIGN */
    if ((offset = (adjSize & (HEAP_ALIGN - 1))) != 0) {
        adjSize = adjSize + (HEAP_ALIGN - offset);
    }

    /*
     *  Make sure the alignment is at least as large as HEAP_ALIGN.
     *  Note: adjAlign must be a power of 2 (by function constraint) and
     *  HEAP_ALIGN is also a power of 2,
     */
    adjAlign = reqAlign;
    if (adjAlign & (HEAP_ALIGN - 1)) {
        /* adjAlign is less than HEAP_ALIGN */
        adjAlign = HEAP_ALIGN;
    }

    /*
     * The block will be allocated from curHeader. Maintain a pointer to
     * prevHeader so prevHeader->next can be updated after the alloc.
     */
    curHeaderPhys = heap_head[bi].next;

    /* Loop over the free list. */
    while (curHeaderPhys != 0) {
	map_header((void **)&curHeader, curHeaderPhys, &curHeader_vm_area);
        curSize = curHeader->size;

        /*
         *  Determine the offset from the beginning to make sure
         *  the alignment request is honored.
         */
        offset = (unsigned long)curHeaderPhys & (adjAlign - 1);
        if (offset) {
            offset = adjAlign - offset;
        }

        /* big enough? */
        if (curSize >= (adjSize + offset)) {
            /* Set the pointer that will be returned. Alloc from front */
            allocAddr = curHeaderPhys + offset;

	    if (dryrun) {
		return allocAddr;
	    }

            /*
             *  Determine the remaining memory after the allocated block.
             *  Note: this cannot be negative because of above comparison.
             */
            remainSize = curSize - adjSize - offset;

	    if (remainSize) {
		newHeaderPhys = allocAddr + adjSize;
		map_header((void **)&newHeader, newHeaderPhys,
		           &newHeader_vm_area);

		newHeader->next = curHeader->next;
		newHeader->size = remainSize;

		unmap_header(newHeader, newHeader_vm_area);
	    }

            /*
             *  If there is memory at the beginning (due to alignment
             *  requirements), maintain it in the list.
             *
             *  offset and remainSize must be multiples of
             *  HEAP_ALIGN. Therefore the address of the newHeader
             *  below must be a multiple of the HEAP_ALIGN, thus
             *  maintaining the requirement.
             */
            if (offset) {
                /* Adjust the curHeader size accordingly */
                curHeader->size = offset;

                /*
                 *  If there is remaining memory, add into the free list.
                 *  Note: no need to coalesce and we have HeapMem locked so
                 *        it is safe.
                 */
                if (remainSize) {
                    curHeader->next = newHeaderPhys;
                }
            }
            else {
                /*
                 *  If there is any remaining, link it in,
                 *  else point to the next free block.
                 *  Note: no need to coalesce and we have HeapMem locked so
                 *        it is safe.
                 */
		if (prevHeaderPhys != 0) {
		    map_header((void **)&prevHeader, prevHeaderPhys,
		               &prevHeader_vm_area);
		}
		else {
		    prevHeader = &heap_head[bi];
		}

                if (remainSize) {
                    prevHeader->next = newHeaderPhys;
                }
                else {
                    prevHeader->next = curHeader->next;
                }

		if (prevHeader != &heap_head[bi]) {
		    unmap_header(prevHeader, prevHeader_vm_area);
		}
            }

	    unmap_header(curHeader, curHeader_vm_area);

            /* Success, return the allocated memory */
            return allocAddr;
        }
        else {
	    prevHeaderPhys = curHeaderPhys;
	    curHeaderPhys = curHeader->next;

	    unmap_header(curHeader, curHeader_vm_area);
        }
    }

    return 0;
}

/*
 *  ======== HeapMem_free ========
 */
void HeapMem_free(int bi, phys_addr_t block, size_t size)
{
    struct vm_struct *curHeader_vm_area;
    struct vm_struct *newHeader_vm_area;
    struct vm_struct *nextHeader_vm_area;
    HeapMem_Header *curHeader;
    HeapMem_Header *newHeader;
    HeapMem_Header *nextHeader;
    phys_addr_t curHeaderPhys = 0;
    phys_addr_t newHeaderPhys;
    phys_addr_t nextHeaderPhys;
    size_t offset;

    /* Restore size to actual allocated size */
    if ((offset = size & (HEAP_ALIGN - 1)) != 0) {
        size += HEAP_ALIGN - offset;
    }

    newHeaderPhys = block;
    nextHeaderPhys = heap_head[bi].next;

    /* Go down freelist and find right place for buf */
    while (nextHeaderPhys != 0 && nextHeaderPhys < newHeaderPhys) {
	map_header((void **)&nextHeader, nextHeaderPhys, &nextHeader_vm_area);

        curHeaderPhys = nextHeaderPhys;
        nextHeaderPhys = nextHeader->next;

	unmap_header(nextHeader, nextHeader_vm_area);
    }

    map_header((void **)&newHeader, newHeaderPhys, &newHeader_vm_area);

    if (curHeaderPhys != 0) {
	map_header((void **)&curHeader, curHeaderPhys, &curHeader_vm_area);
    }
    else {
	curHeader = &heap_head[bi];
    }

    newHeader->next = nextHeaderPhys;
    newHeader->size = size;
    curHeader->next = newHeaderPhys;

    /* Join contiguous free blocks */
    /* Join with upper block */
    if (nextHeaderPhys != 0 && (newHeaderPhys + size) == nextHeaderPhys) {
	map_header((void **)&nextHeader, nextHeaderPhys, &nextHeader_vm_area);

        newHeader->next = nextHeader->next;
        newHeader->size += nextHeader->size;

	unmap_header(nextHeader, nextHeader_vm_area);
    }

    /*
     * Join with lower block. Make sure to check to see if not the
     * first block.
     */
    if (curHeader != &heap_head[bi]) {
        if ((curHeaderPhys + curHeader->size) == newHeaderPhys) {
	    curHeader->next = newHeader->next;
	    curHeader->size += newHeader->size;
	}

	unmap_header(curHeader, curHeader_vm_area);
    }

    unmap_header(newHeader, newHeader_vm_area);
}

/* Traverses the page tables and translates a virtual address to a physical. */
static phys_addr_t get_phys(void *virtp)
{
    unsigned long virt = (unsigned long)virtp;
    phys_addr_t physp = ~(0LL);
    struct mm_struct *mm = current->mm;
    struct vm_area_struct *vma;

    /* For kernel direct-mapped memory, take the easy way */
    if (virt >= PAGE_OFFSET) {
        physp = virt_to_phys(virtp);
	__D("get_phys: virt_to_phys translated direct-mapped %#lx to %#llx\n",
	    virt, (unsigned long long)physp);
    }

    /* this will catch, kernel-allocated, mmaped-to-usermode addresses */
    else if ((vma = find_vma(mm, virt)) &&
             (vma->vm_flags & VM_IO) &&
             (vma->vm_pgoff)) {
        physp = ((unsigned long long)vma->vm_pgoff << PAGE_SHIFT) +
	        (virt - vma->vm_start);
	__D("get_phys: find_vma translated user %#lx to %#llx\n", virt,
	    (unsigned long long)physp);
    }

    /* otherwise, use get_user_pages() for general userland pages */
    else {
        int res, nr_pages = 1;
        struct page *pages;

        down_read(&current->mm->mmap_sem);
        res = get_user_pages(current, current->mm, virt, nr_pages, 1, 0,
                             &pages, NULL);
        up_read(&current->mm->mmap_sem);

        if (res == nr_pages) {
            physp = __pa(page_address(&pages[0]) + (virt & ~PAGE_MASK));
	    __D("get_phys: get_user_pages translated user %#lx to %#llx\n",
	        virt, (unsigned long long)physp);
        } else {
            __E("%s: Unable to find phys addr for %#lx\n",
                __FUNCTION__, virt);
            __E("%s: get_user_pages() failed: %d\n", __FUNCTION__, res);
        }
    }

    return physp;
}

/* Allocates space from the top "highmem" contiguous buffer for pool buffer. */
static phys_addr_t alloc_pool_buffer(int bi, unsigned long long size)
{
    phys_addr_t physp;

    __D("alloc_pool_buffer: Called for size 0x%llx\n", size);

    if (size <= block_avail_size[bi]) {
        __D("alloc_pool_buffer: Fits req %#llx < avail: %#llx\n",
            size, block_avail_size[bi]);
        block_avail_size[bi] -= size;
        physp = block_start[bi] + block_avail_size[bi];

        __D("alloc_pool_buffer: new available block size is %#llx\n",
            block_avail_size[bi]);

        __D("alloc_pool_buffer: returning allocated buffer at %#llx\n",
	    (unsigned long long)physp);

        return physp;
    }

    __E("Failed to find a big enough free block\n");

    return 0;
}


#ifdef __DEBUG
/* Only for debug */
static void dump_lists(int bi, int idx)
{
    struct list_head *freelistp = &p_objs[bi][idx].freelist;
    struct list_head *busylistp = &p_objs[bi][idx].busylist;
    struct list_head *e;
    struct pool_buffer *entry;

/* way too chatty, neuter for now */
return;

    if (mutex_lock_interruptible(&cmem_mutex)) {
        return;
    }

    __D("Busylist for pool %d:\n", idx);
    for (e = busylistp->next; e != busylistp; e = e->next) {

        entry = list_entry(e, struct pool_buffer, element);

        __D("Busy: Buffer with id %d and physical address %#llx\n",
            entry->id, (unsigned long long)entry->physp);
    }

    __D("Freelist for pool %d:\n", idx);
    for (e = freelistp->next; e != freelistp; e = e->next) {

        entry = list_entry(e, struct pool_buffer, element);

        __D("Free: Buffer with id %d and physical address %#llx\n",
            entry->id, (unsigned long long)entry->physp);
    }

    mutex_unlock(&cmem_mutex);
}
#endif

/*
 *  ======== find_busy_entry ========
 *  find_busy_entry looks for an allocated pool buffer containing
 *  physical addr physp -> (physp + *sizep).
 *
 *  Should be called with the cmem_mutex held.
 */
static struct pool_buffer *find_busy_entry(phys_addr_t physp, int *poolp, struct list_head **ep, int *bip, size_t *sizep)
{
    struct list_head *busylistp;
    struct list_head *e;
    struct pool_buffer *entry;
    int num_pools;
    int i;
    int bi;

    /* loop for NBLOCKS + 1 to handle special CMA global area "block" */
    for (bi = 0; bi < (NBLOCKS + 1); bi++) {
	num_pools = npools[bi];
	if (heap_pool[bi] != -1) {
	    num_pools++;
	}

	for (i = 0; i < num_pools; i++) {
	    busylistp = &p_objs[bi][i].busylist;

	    for (e = busylistp->next; e != busylistp; e = e->next) {
		entry = list_entry(e, struct pool_buffer, element);
		if ((!sizep && entry->physp == physp) ||
		     (sizep &&
		       (physp >= entry->physp &&
		         (physp + *sizep) <= (entry->physp + entry->size)
		       )
		     )
		   ) {
		    if (poolp) {
			*poolp = i;
		    }
		    if (ep) {
			*ep = e;
		    }
		    if (bip) {
			*bip = bi;
		    }

		    return entry;
		}
	    }
	}
    }

    return NULL;
}

static void cmem_seq_stop(struct seq_file *s, void *v);
static void *cmem_seq_start(struct seq_file *s, loff_t *pos);
static void *cmem_seq_next(struct seq_file *s, void *v, loff_t *pos);
static int cmem_seq_show(struct seq_file *s, void *v);

static struct seq_operations cmem_seq_ops = {
    .start = cmem_seq_start,
    .next = cmem_seq_next,
    .stop = cmem_seq_stop,
    .show = cmem_seq_show,
};

#define SHOW_BUSY_BANNER (1 << 0)
#define SHOW_PREV_FREE_BANNER (1 << 1)
#define SHOW_LAST_FREE_BANNER (1 << 2)
#define BUSY_ENTRY (1 << 3)
#define FREE_ENTRY (1 << 4)

void *find_buffer_n(struct seq_file *s, int n)
{
    struct list_head *listp = NULL;
    int busy_empty;
    int free_empty = 0;
    int found = 0;
    int count;
    int i;
    int bi;

    __D("find_buffer_n: n=%d\n", n);

    s->private = (void *)0;
    count = 0;

    for (bi = 0; bi < NBLOCKS; bi++) {
	for (i = 0; i < npools[bi]; i++) {
	    listp = &p_objs[bi][i].busylist;
	    listp = listp->next;
	    busy_empty = 1;
	    while (listp != &p_objs[bi][i].busylist) {
		busy_empty = 0;
		if (count == n) {
		    found = 1;
		    s->private = (void *)((int)s->private | BUSY_ENTRY);

		    break;
		}
		count++;
		listp = listp->next;
	    }
	    if (found) {
		break;
	    }

	    listp = &p_objs[bi][i].freelist;
	    listp = listp->next;
	    free_empty = 1;
	    while (listp != &p_objs[bi][i].freelist) {
		if (i == 0 ||
		    (p_objs[bi][i - 1].freelist.next !=
		    &p_objs[bi][i - 1].freelist)) {

		    free_empty = 0;
		}
		if (count == n) {
		    found = 1;
		    s->private = (void *)((int)s->private | FREE_ENTRY);

		    break;
		}
		count++;
		listp = listp->next;
	    }
	    if (found) {
		break;
	    }
	}
	if (found) {
	    break;
	}
    }

    if (!found) {
	listp = NULL;
    }
    else {
	if (busy_empty) {
	    s->private = (void *)((int)s->private | SHOW_BUSY_BANNER);
	}
	if (free_empty) {
	    s->private = (void *)((int)s->private | SHOW_PREV_FREE_BANNER);
	}
	if (count == (total_num_buffers[bi] - 1)) {
	    s->private = (void *)((int)s->private | SHOW_LAST_FREE_BANNER);
	}
    }

    return listp;
}

static void cmem_seq_stop(struct seq_file *s, void *v)
{
    __D("cmem_seq_stop: v=0x%p\n", v);

    mutex_unlock(&cmem_mutex);
}

static void *cmem_seq_start(struct seq_file *s, loff_t *pos)
{
    struct list_head *listp;
    int total_num;

    if (mutex_lock_interruptible(&cmem_mutex)) {
	return ERR_PTR(-ERESTARTSYS);
    }

    __D("cmem_seq_start: *pos=%d\n", (int)*pos);

    total_num = total_num_buffers[0] + total_num_buffers[1];
    if (*pos >= total_num) {
	__D("  %d >= %d\n", (int)*pos, total_num);

	return NULL;
    }

    listp = find_buffer_n(s, *pos);

    __D("  returning 0x%p\n", listp);

    return listp;
}

static void *cmem_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
    struct list_head *listp;
    int total_num;

    __D("cmem_seq_next: *pos=%d\n", (int)*pos);

    __D("  incrementing *pos\n");
    ++(*pos);

    total_num = total_num_buffers[0] + total_num_buffers[1];
    if (*pos >= total_num) {
	__D("  %d >= %d\n", (int)*pos, total_num);

	return NULL;
    }

    listp = find_buffer_n(s, *pos);

    __D("  returning 0x%p\n", listp);

    return listp;
}

void show_busy_banner(int bi, struct seq_file *s, int n)
{
    seq_printf(s, "\nBlock %d: Pool %d: %d bufs size 0x%llx"
                      " (0x%llx requested)\n\nPool %d busy bufs:\n",
                      bi, n, p_objs[bi][n].numbufs, p_objs[bi][n].size,
                      p_objs[bi][n].reqsize, n);
}

void show_free_banner(struct seq_file *s, int n)
{
    seq_printf(s, "\nPool %d free bufs:\n", n);
}

/*
 * Show one pool entry, w/ banners for first entries in a pool's busy or
 * free list.
 */
static int cmem_seq_show(struct seq_file *s, void *v)
{
    struct list_head *listp = v;
    struct list_head *e = v;
    struct pool_buffer *entry;
    char *attr;
    int i;
    int bi;

    __D("cmem_seq_show:\n");

    for (bi = 0; bi < NBLOCKS; bi++) {
	/* look for banners to show */
	for (i = 0; i < npools[bi]; i++) {
	    if (listp == p_objs[bi][i].busylist.next) {
		/* first buffer in busylist */
		if ((int)s->private & SHOW_PREV_FREE_BANNER) {
		    /*
		     * Previous pool's freelist empty, need to show banner.
		     */
		    show_free_banner(s, i - 1);
		}
		show_busy_banner(bi, s, i);

		break;
	    }
	    if (listp == p_objs[bi][i].freelist.next) {
		/* first buffer in freelist */
		if ((int)s->private & SHOW_PREV_FREE_BANNER) {
		    /*
		     * Previous pool's freelist & this pool's busylist empty,
		     * need to show banner.
		     */
		    show_free_banner(s, i - 1);
		}
		if ((int)s->private & SHOW_BUSY_BANNER) {
		    /*
		     * This pool's busylist empty, need to show banner.
		     */
		    show_busy_banner(bi, s, i);
		}
		show_free_banner(s, i);

		break;
	    }
	}
    }

    entry = list_entry(e, struct pool_buffer, element);

    if ((int)s->private & BUSY_ENTRY) {
	attr = entry->flags & CMEM_CACHED ? "(cached)" : "(noncached)";
	seq_printf(s, "id %d: phys addr %#llx %s\n", entry->id,
                   (unsigned long long)entry->physp, attr);
    }
    else {
	seq_printf(s, "id %d: phys addr %#llx\n", entry->id,
	                (unsigned long long)entry->physp);
    }

    if ((int)s->private & BUSY_ENTRY &&
        (int)s->private & SHOW_LAST_FREE_BANNER) {

	/* FIXME */
	show_free_banner(s, npools[0] - 1);
    }

    return 0;
}

static int cmem_proc_open(struct inode *inode, struct file *file);

static struct file_operations cmem_proc_ops = {
    .owner = THIS_MODULE,
    .open = cmem_proc_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = seq_release,
};

static int cmem_proc_open(struct inode *inode, struct file *file)
{
    return seq_open(file, &cmem_seq_ops);
}

/* Allocate a contiguous memory pool. */
static int alloc_pool(int bi, int idx, int num, unsigned long long reqsize, phys_addr_t *physpRet)
{
    struct pool_buffer *entry;
    struct list_head *freelistp = &p_objs[bi][idx].freelist;
    struct list_head *busylistp = &p_objs[bi][idx].busylist;
    unsigned long long size = PAGE_ALIGN(reqsize);
    phys_addr_t physp;
    int i;

    __D("Allocating %d buffers of size 0x%llx (requested 0x%llx)\n",
                num, size, reqsize);

    p_objs[bi][idx].reqsize = reqsize;
    p_objs[bi][idx].numbufs = num;
    p_objs[bi][idx].size = size;

    INIT_LIST_HEAD(freelistp);
    INIT_LIST_HEAD(busylistp);

    for (i = 0; i < num; i++) {
        entry = kmalloc(sizeof(struct pool_buffer), GFP_KERNEL);

        if (!entry) {
            __E("alloc_pool failed to malloc pool_buffer struct");
            return -ENOMEM;
        }

        physp = alloc_pool_buffer(bi, size);

        if (physp == 0) {
            __E("alloc_pool failed to get contiguous area of size %llu\n",
                size);

            /*
             * Need to free this entry now since it didn't get added to
             * a list that will be freed during module removal (cmem_exit())
             * Fixes SDSCM00027040.
             */
            kfree(entry);

            return -ENOMEM;
        }

        entry->id = i;
        entry->physp = physp;
        entry->size = size;
        INIT_LIST_HEAD(&entry->users);

        if (physpRet) {
            *physpRet++ = physp;
        }

        __D("Allocated buffer %d, physical %#llx and size %#llx\n",
            entry->id, (unsigned long long)entry->physp, size);

        list_add_tail(&entry->element, freelistp);
    }

#ifdef __DEBUG
    dump_lists(bi, idx);
#endif

    return 0;
}

struct block_struct {
    void *addr;
    size_t size;
};

static long ioctl(struct file *filp, unsigned int cmd, unsigned long args)
{
    unsigned int __user *argp = (unsigned int __user *) args;
    unsigned long long __user *llargp = (unsigned long long __user *) args;
    unsigned long virtArg;
    unsigned long long physArg;
    struct list_head *freelistp = NULL;
    struct list_head *busylistp = NULL;
    struct list_head *registeredlistp;
    struct list_head *e = NULL;
    struct list_head *u;
    struct list_head *unext;
    struct pool_buffer *entry;
    struct registered_user *user;
    phys_addr_t physp;
    void *virtp;
    void *virtp_end;
    dma_addr_t dma = 0;
    size_t reqsize, align;
    size_t size = 0;
    unsigned long long lsize, lreqsize;
    unsigned long long delta = MAXTYPE(unsigned long long);
    int pool = -1;
    int i;
    int bi;
    int id;
    int pool_alloc;
    struct block_struct block;
    union CMEM_AllocUnion allocDesc;
    struct device *dev = NULL;

    if (_IOC_TYPE(cmd) != _IOC_TYPE(CMEM_IOCMAGIC)) {
	__E("ioctl(): bad command type %#x (should be %#x)\n",
	    _IOC_TYPE(cmd), _IOC_TYPE(CMEM_IOCMAGIC));
    }

    switch (cmd & CMEM_IOCCMDMASK) {
        case CMEM_IOCALLOCHEAP:
	    if (copy_from_user(&allocDesc, argp, sizeof(allocDesc))) {
		return -EFAULT;
	    }

	    size = allocDesc.alloc_heap_inparams.size;
	    align = allocDesc.alloc_heap_inparams.align;
	    bi = allocDesc.alloc_heap_inparams.blockid;

	    if (bi == CMEM_CMABLOCKID) {
		bi = NBLOCKS;

		if (cmem_cma_heapsize == 0) {
		    __D("no explicit CMEM CMA heap, using global area\n");
#if IS_ENABLED(CONFIG_ARCH_KEYSTONE) && IS_ENABLED(CONFIG_ARM_LPAE) \
	&& (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
		    dev = cmem_cma_dev_0;
#else
		    dev = NULL;
#endif
		}
		else {
		    dev = &cmem_cma_dev[heap_pool[bi]];
		}
	    }

            __D("ALLOCHEAP%s ioctl received on heap pool for block %d\n",
	        cmd & CMEM_CACHED ? "CACHED" : "", bi);

	    if (bi > NBLOCKS || bi < 0) {
		__E("ioctl: invalid block id %d, must be < %d\n",
		    bi, NBLOCKS);
		return -EINVAL;
	    }

	    /* heap_pool[NBLOCKS] (the CMA heap) is always available */
	    if (bi < NBLOCKS && heap_pool[bi] == -1) {
		__E("ioctl: no heap available in block %d\n", bi);
		return -EINVAL;
	    }

	    pool = heap_pool[bi];

	    pool_alloc = 0;
alloc:
	    entry = kmalloc(sizeof(struct pool_buffer), GFP_KERNEL);
	    if (!entry) {
		__E("ioctl: failed to kmalloc pool_buffer struct for heap");

		return -ENOMEM;
	    }

	    if (mutex_lock_interruptible(&cmem_mutex)) {
		return -ERESTARTSYS;
	    }

	    size = PAGE_ALIGN(size);

	    if (bi == NBLOCKS) {
		virtp = dma_alloc_coherent(dev, size, &dma, GFP_KERNEL);

#if IS_ENABLED(CONFIG_ARCH_KEYSTONE) && IS_ENABLED(CONFIG_ARM_LPAE) \
	&& (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
		/* adjust from 32-bit alias to 36-bit phys */
		physp = dma
			+ ((unsigned long long)KEYSTONE_DMA_PFN_OFFSET
			   << PAGE_SHIFT);
#else
		physp = dma;
#endif
		entry->dev = dev;
		entry->kvirtp = virtp;
	    }
	    else {
		physp = HeapMem_alloc(bi, size, align, ALLOCRUN);

		/* set only for test just below here */
		virtp = (void *)(unsigned int)physp;
	    }

	    if (virtp == NULL) {
		__E("ioctl: failed to allocate heap buffer of size %#x\n",
		    size);

		mutex_unlock(&cmem_mutex);
		kfree(entry);

		return -ENOMEM;
	    }

	    entry->dma = dma;
	    entry->id = pool;
	    entry->physp = physp;
	    entry->size = size;
	    entry->flags = cmd & ~CMEM_IOCCMDMASK;
	    INIT_LIST_HEAD(&entry->users);

            busylistp = &p_objs[bi][pool].busylist;
	    list_add_tail(&entry->element, busylistp);

	    user = kmalloc(sizeof(struct registered_user), GFP_KERNEL);
	    user->filp = filp;
	    list_add(&user->element, &entry->users);

            mutex_unlock(&cmem_mutex);

	    if (pool_alloc) {
		allocDesc.alloc_pool_outparams.physp = physp;
		allocDesc.alloc_pool_outparams.size = size;

		if (copy_to_user(argp, &allocDesc, sizeof(allocDesc))) {
		    mutex_unlock(&cmem_mutex);
		    return -EFAULT;
		}
	    }
	    else {
		if (put_user(physp, llargp)) {
		    return -EFAULT;
		}
	    }

            __D("ALLOCHEAP%s: allocated %#x size buffer at %#llx (phys address)\n",
	        cmd & CMEM_CACHED ? "CACHED" : "", (size_t)entry->size,
	        (unsigned long long)entry->physp);

	    break;

        /*
         * argp contains a pointer to an alloc descriptor coming in, and the
         * physical address and size of the allocated buffer when returning.
         */
        case CMEM_IOCALLOC:
	    if (copy_from_user(&allocDesc, argp, sizeof(allocDesc))) {
                return -EFAULT;
            }

	    pool = allocDesc.alloc_pool_inparams.poolid;
	    bi = allocDesc.alloc_pool_inparams.blockid;

	    if (bi == CMEM_CMABLOCKID) {
		bi = NBLOCKS;
	    }

            __D("ALLOC%s ioctl received on pool %d for memory block %d\n",
	        cmd & CMEM_CACHED ? "CACHED" : "", pool, bi);

	    if (bi > NBLOCKS || bi < 0) {
		__E("ioctl: invalid block id %d, must be < %d\n",
		    bi, NBLOCKS);
		return -EINVAL;
	    }

            if (pool >= npools[bi] || pool < 0) {
                __E("ALLOC%s: invalid pool (%d) passed.\n",
		    cmd & CMEM_CACHED ? "CACHED" : "", pool);
                return -EINVAL;
            }

	    if (bi == NBLOCKS) {
		lsize = p_objs[bi][pool].size;
		dev = &cmem_cma_dev[pool];
		align = 0;
		pool_alloc = 1;

		goto alloc;
	    }

            busylistp = &p_objs[bi][pool].busylist;
	    freelistp = &p_objs[bi][pool].freelist;

	    if (mutex_lock_interruptible(&cmem_mutex)) {
		return -ERESTARTSYS;
	    }

	    e = freelistp->next;
	    if (e == freelistp) {
		__E("ALLOC%s: No free buffers available for pool %d\n",
		    cmd & CMEM_CACHED ? "CACHED" : "", pool);
		mutex_unlock(&cmem_mutex);
		return -ENOMEM;
	    }
	    entry = list_entry(e, struct pool_buffer, element);

	    allocDesc.alloc_pool_outparams.physp = entry->physp;
	    allocDesc.alloc_pool_outparams.size = p_objs[bi][pool].size;

	    if (copy_to_user(argp, &allocDesc, sizeof(allocDesc))) {
                mutex_unlock(&cmem_mutex);
                return -EFAULT;
            }

	    entry->flags = cmd & ~CMEM_IOCCMDMASK;

            list_del_init(e);
            list_add(e, busylistp);

	    user = kmalloc(sizeof(struct registered_user), GFP_KERNEL);
	    user->filp = filp;
	    list_add(&user->element, &entry->users);

            mutex_unlock(&cmem_mutex);

            __D("ALLOC%s: allocated a buffer at %#llx (phys address)\n",
	        cmd & CMEM_CACHED ? "CACHED" : "",
	        (unsigned long long)entry->physp);

#ifdef __DEBUG
            dump_lists(bi, pool);
#endif
            break;

        /*
         * argp contains either the user virtual address or the physical
	 * address of the buffer to free coming in, and contains the pool
	 * where it was freed from and the size of the block on return.
         */
        case CMEM_IOCFREE:
            __D("FREE%s%s ioctl received.\n",
	        cmd & CMEM_HEAP ? "HEAP" : "",
	        cmd & CMEM_PHYS ? "PHYS" : "");

	    if (!(cmd & CMEM_PHYS)) {
		if (get_user(virtArg, argp)) {
		    return -EFAULT;
		}

		physp = get_phys((void *)virtArg);

		if (physp == ~(0LL)) {
		    __E("FREE%s: Failed to convert virtual %#lx to physical\n",
			cmd & CMEM_HEAP ? "HEAP" : "", virtArg);
		    return -EFAULT;
		}

		virtp = (void *)virtArg;

		__D("FREE%s: translated 0x%p user virtual to %#llx physical\n",
		    cmd & CMEM_HEAP ? "HEAP" : "",
		    virtp, (unsigned long long)physp);
	    }
	    else {
		virtp = 0L;    /* silence the compiler warning */
		if (copy_from_user(&physArg, llargp,
		                   sizeof(unsigned long long))) {
		    return -EFAULT;
		}
		physp = physArg;
	    }

	    if (mutex_lock_interruptible(&cmem_mutex)) {
		return -ERESTARTSYS;
	    }

	    size = 0;

	    entry = find_busy_entry(physp, &pool, &e, &bi, NULL);
	    if (entry) {
		/* record values in case entry gets kfree()'d for CMEM_HEAP */
		id = entry->id;
		size = (size_t)entry->size;

		registeredlistp = &entry->users;
		u = registeredlistp->next;
		while (u != registeredlistp) {
		    unext = u->next;

		    user = list_entry(u, struct registered_user, element);
		    if (user->filp == filp) {
			__D("FREE%s%s: Removing file 0x%p from user list of buffer %#llx...\n",
			    cmd & CMEM_HEAP ? "HEAP" : "",
			    cmd & CMEM_PHYS ? "PHYS" : "",
			    filp, (unsigned long long)physp);

			list_del(u);
			kfree(user);

			break;
		    }

		    u = unext;
		}

		if (u == registeredlistp) {
		    __E("FREE%s%s: Not a registered user of physical buffer %#llx\n",
		        cmd & CMEM_HEAP ? "HEAP" : "",
		        cmd & CMEM_PHYS ? "PHYS" : "",
		        (unsigned long long)physp);
		    mutex_unlock(&cmem_mutex);

		    return -EFAULT;
		}

		if (registeredlistp->next == registeredlistp) {
		    /* no more registered users, free buffer */
		    if (bi == NBLOCKS || pool == heap_pool[bi]) {
			if (!(cmd & CMEM_PHYS) && bi != NBLOCKS) {
			    /*
			     * Need to invalidate possible cached entry for
			     * user's virt addr since the kernel is about to
			     * do a non-cached write to the entry in
			     * HeapMem_free()
			     */
			    virtp_end = virtp + size;
			    outer_inv_range(physp, physp + size);
			    dmac_map_area(virtp, size, DMA_FROM_DEVICE);

			    __D("FREEHEAP: invalidated user virtual "
			        "0x%p -> 0x%p\n", virtp, virtp_end);
			}

			if (bi == NBLOCKS) {
			    dma_free_coherent(entry->dev, (size_t)entry->size,
			                      entry->kvirtp, entry->dma);
			}
			else {
			    HeapMem_free(bi, entry->physp, (size_t)entry->size);
			}
			list_del(e);
			kfree(entry);
		    }
		    else {
			list_del_init(e);
			list_add(e, &p_objs[bi][pool].freelist);
		    }

		    __D("FREE%s%s: Successfully freed buffer %d from pool %d\n",
			cmd & CMEM_HEAP ? "HEAP" : "",
			cmd & CMEM_PHYS ? "PHYS" : "", id, pool);
		}
	    }

            mutex_unlock(&cmem_mutex);

            if (!entry) {
                __E("Failed to free memory at %#llx\n",
		    (unsigned long long)physp);
                return -EFAULT;
            }

#ifdef __DEBUG
            dump_lists(bi, pool);
#endif
	    if (cmd & CMEM_PHYS) {
		__D("FREE%sPHYS: returning\n", cmd & CMEM_HEAP ? "HEAP" : "");
	    }
	    else {
		    if (pool == heap_pool[bi]) {
			allocDesc.free_outparams.size = size;
		    }
		    else {
			allocDesc.free_outparams.size = p_objs[bi][pool].size;
		    }
		    allocDesc.free_outparams.poolid = pool;
		    if (copy_to_user(argp, &allocDesc, sizeof(allocDesc))) {
			return -EFAULT;
		    }

		    __D("FREE%s%s: returning size 0x%x, poolid %d\n",
			cmd & CMEM_HEAP ? "HEAP" : "",
			cmd & CMEM_PHYS ? "PHYS" : "",
			allocDesc.free_outparams.size,
			allocDesc.free_outparams.poolid);
	    }

            break;

        /*
         * argp contains the user virtual address of the buffer to translate
         * coming in, and the translated physical address on return.
         */
        case CMEM_IOCGETPHYS:
            __D("GETPHYS ioctl received.\n");
            if (get_user(virtArg, argp)) {
                return -EFAULT;
            }

            physp = get_phys((void *)virtArg);

            if (physp == ~(0LL)) {
                __E("GETPHYS: Failed to convert virtual %#lx to physical.\n",
                    virtArg);
                return -EFAULT;
            }

            if (put_user(physp, llargp)) {
                return -EFAULT;
            }

            __D("GETPHYS: returning %#llx\n", (unsigned long long)physp);
            break;

        /*
         * argp contains the pool to query for size coming in, and the size
         * of the pool on return.
         */
        case CMEM_IOCGETSIZE:
            __D("GETSIZE ioctl received\n");
	    if (copy_from_user(&allocDesc, argp, sizeof(allocDesc))) {
                return -EFAULT;
            }

	    pool = allocDesc.get_size_inparams.poolid;
	    bi = allocDesc.get_size_inparams.blockid;

	    if (bi == CMEM_CMABLOCKID) {
		bi = NBLOCKS;
	    }

	    if (bi > NBLOCKS || bi < 0) {
		__E("ioctl: invalid block id %d, must be < %d\n",
		    bi, NBLOCKS);
		return -EINVAL;
	    }

            if (pool >= npools[bi] || pool < 0) {
                __E("GETSIZE: invalid pool (%d) passed.\n", pool);
                return -EINVAL;
            }

            if (put_user(p_objs[bi][pool].size, argp)) {
                return -EFAULT;
            }
            __D("GETSIZE returning %#llx\n", p_objs[bi][pool].size);
            break;

        /*
         * argp contains the requested pool buffers size coming in, and the
         * pool id (index) on return.
         */
        case CMEM_IOCGETPOOL:
            __D("GETPOOL ioctl received.\n");
	    if (copy_from_user(&allocDesc, argp, sizeof(allocDesc))) {
                return -EFAULT;
            }

	    lreqsize = allocDesc.get_pool_inparams.size;
	    bi = allocDesc.get_pool_inparams.blockid;

	    if (bi == CMEM_CMABLOCKID) {
		bi = NBLOCKS;
	    }

	    if (bi > NBLOCKS || bi < 0) {
		__E("ioctl: invalid block id %d, must be < %d\n",
		    bi, NBLOCKS);
		return -EINVAL;
	    }

	    if (mutex_lock_interruptible(&cmem_mutex)) {
		return -ERESTARTSYS;
	    }

            __D("GETPOOL: Trying to find a pool to fit size %#llx\n", lreqsize);
            for (i = 0; i < npools[bi]; i++) {
                lsize = p_objs[bi][i].size;
                freelistp = &p_objs[bi][i].freelist;

                __D("GETPOOL: size (%#llx) > reqsize (%#llx)?\n",
		    lsize, lreqsize);
                if (lsize >= lreqsize) {
                    __D("GETPOOL: delta (%#llx) < olddelta (%#llx)?\n",
                        lsize - lreqsize, delta);
                    if ((lsize - lreqsize) < delta) {
                        if (bi < NBLOCKS) {
			    if (!list_empty(freelistp)) {
				delta = lsize - lreqsize;
				pool = i;
				__D("GETPOOL: Found a best fit delta %#llx in pool %d\n",
				    delta, pool);
			    }
                        }
			else {
			    delta = lsize - lreqsize;
			    pool = i;
			    __D("GETPOOL: Found a best fit delta %#llx in CMA block\n",
				    delta);
                        }
                    }
                }
            }

	    if (pool == -1 && heap_pool[bi] != -1) {
		if (useHeapIfPoolUnavailable) {
		    /* no pool buffer available, try heap */

		    reqsize = lreqsize;
		    physp = HeapMem_alloc(bi, reqsize, HEAP_ALIGN, DRYRUN);
		    if (physp != 0) {
			/*
			 * Indicate heap pool with magic negative value.
			 * -1 indicates no pool and no heap.
			 * -2 indicates no pool but heap available and allowed.
			 */
			pool = -2;

			__D("GETPOOL: no pool-based buffer available, "
			    "returning heap \"pool\" instead (due to config "
			    "override)\n");
		    }
		}
	    }

            mutex_unlock(&cmem_mutex);

	    if (pool == -1) {
		__E("Failed to find a pool which fits %#llx\n", lreqsize);

		return -ENOMEM;
            }

            if (put_user(pool, argp)) {
                return -EFAULT;
            }
            __D("GETPOOL: returning %d\n", pool);
            break;

        case CMEM_IOCCACHEWBINVALL:
	    flush_cache_all();
	    __D("CACHEWBINVALL: flush all cache\n");

	    break;

        case CMEM_IOCCACHE:
            __D("CACHE%s%s ioctl received.\n",
	        cmd & CMEM_WB ? "WB" : "", cmd & CMEM_INV ? "INV" : "");

	    if (copy_from_user(&block, argp, sizeof(block))) {
		return -EFAULT;
	    }
	    virtp = block.addr;
	    virtp_end = virtp + block.size;

#ifdef CHECK_FOR_ALLOCATED_BUFFER
            physp = get_phys(virtp);
            if (physp == ~(0LL)) {
                __E("CACHE%s%s: Failed to convert virtual 0x%p to physical\n",
		    cmd & CMEM_WB ? "WB" : "", cmd & CMEM_INV ? "INV" : "",
		    virtp);
                return -EFAULT;
            }

            __D("CACHE%s%s: translated 0x%p user virtual to %#lx physical\n",
	        cmd & CMEM_WB ? "WB" : "", cmd & CMEM_INV ? "INV" : "",
                virtp, physp);

	    if (mutex_lock_interruptible(&cmem_mutex)) {
		return -ERESTARTSYS;
	    }
	    entry = find_busy_entry(physp, &pool, &e, &bi, &block.size);
            mutex_unlock(&cmem_mutex);
	    if (!entry) {
                __E("CACHE%s%s: Failed to find allocated buffer at virtual 0x%p\n",
		    cmd & CMEM_WB ? "WB" : "", cmd & CMEM_INV ? "INV" : "",
		    virtp);
                return -ENXIO;
	    }
            if (!(entry->flags & CMEM_CACHED)) {
                __E("CACHE%s%s: virtual buffer 0x%p not cached\n",
		    cmd & CMEM_WB ? "WB" : "", cmd & CMEM_INV ? "INV" : "",
		    virtp);
                return -EINVAL;
	    }
#endif

#ifdef USE_MMAPSEM
	    __D("CACHE%s%s: acquiring mmap_sem ...\n",
	        cmd & CMEM_WB ? "WB" : "", cmd & CMEM_INV ? "INV" : "");
	    down_write(&current->mm->mmap_sem);
#endif

	    physp = get_phys(virtp);

	    switch (cmd & ~CMEM_IOCMAGIC) {
	      case CMEM_IOCCACHEWB:
		dmac_map_area(virtp, block.size, DMA_TO_DEVICE);
		outer_clean_range(physp, physp + block.size);

		__D("CACHEWB: cleaned user virtual 0x%p -> 0x%p\n",
		       virtp, virtp_end);

		break;

	      case CMEM_IOCCACHEINV:
		outer_inv_range(physp, physp + block.size);
		dmac_map_area(virtp, block.size, DMA_FROM_DEVICE);

		__D("CACHEINV: invalidated user virtual 0x%p -> 0x%p\n",
		       virtp, virtp_end);

		break;

	      case CMEM_IOCCACHEWBINV:
		dmac_map_area(virtp, block.size, DMA_BIDIRECTIONAL);
		outer_flush_range(physp, physp + block.size);

		__D("CACHEWBINV: flushed user virtual 0x%p -> 0x%p\n",
		       virtp, virtp_end);

		break;
	    }

#ifdef USE_MMAPSEM
	    __D("CACHE%s%s: releasing mmap_sem ...\n",
	        cmd & CMEM_WB ? "WB" : "", cmd & CMEM_INV ? "INV" : "");
	    up_write(&current->mm->mmap_sem);
#endif

	    break;

        case CMEM_IOCGETVERSION:
            __D("GETVERSION ioctl received, returning %#x.\n", version);

            if (put_user(version, argp)) {
                return -EFAULT;
            }

	    break;

        case CMEM_IOCGETBLOCK:
            __D("GETBLOCK ioctl received.\n");

	    if (copy_from_user(&allocDesc, argp, sizeof(allocDesc))) {
                return -EFAULT;
            }

	    bi = allocDesc.blockid;
	    if (bi >= nblocks || bi < 0) {
		__E("GETBLOCK: invalid block ID %d\n", bi);

		return -EINVAL;
	    }

	    allocDesc.get_block_outparams.physp = block_start[bi];
	    allocDesc.get_block_outparams.size = block_end[bi] -
	                                         block_start[bi];

            __D("GETBLOCK: returning phys base "
	        "%#llx, size %#llx.\n", allocDesc.get_block_outparams.physp,
                allocDesc.get_block_outparams.size);

	    if (copy_to_user(argp, &allocDesc, sizeof(allocDesc))) {
                return -EFAULT;
            }

	    break;

        case CMEM_IOCGETNUMBLOCKS:
            __D("GETNUMBLOCKS ioctl received, returning %d.\n", nblocks);

            if (put_user(nblocks, argp)) {
                return -EFAULT;
            }

	    break;

        case CMEM_IOCREGUSER:
            __D("REGUSER ioctl received.\n");

	    if (copy_from_user(&physArg, llargp, sizeof(unsigned long long))) {
		return -EFAULT;
	    }
	    physp = physArg;

	    if (mutex_lock_interruptible(&cmem_mutex)) {
		return -ERESTARTSYS;
	    }

	    entry = find_busy_entry(physp, &pool, &e, &bi, NULL);
	    if (entry) {
		/*
		 * Should we check if the "current" process is already on
		 * the list and return error if so?  Or should we just
		 * silently not put it on the list twice and return success?
		 * Or should we put it on the list a second time, which seems
		 * to be OK to do and will require being removed from the
		 * list twice?  So many questions...
		 *
		 * The code below, lacking the test, will put a process on
		 * the list multiple times (every time IOCREGUSER is called).
		 */
		user = kmalloc(sizeof(struct registered_user), GFP_KERNEL);
		user->filp = filp;
		list_add(&user->element, &entry->users);
	    }

            mutex_unlock(&cmem_mutex);

            if (!entry) {
                return -EFAULT;
            }

            if (put_user(entry->size, argp)) {
                return -EFAULT;
            }

	    break;

        default:
            __E("Unknown ioctl received.\n");
            return -EINVAL;
    }

    return 0;
}

static int mmap(struct file *filp, struct vm_area_struct *vma)
{
    phys_addr_t physp;
    struct pool_buffer *entry;
    unsigned long size = vma->vm_end - vma->vm_start;
    size_t s;

    __D("mmap: vma->vm_start     = %#lx\n", vma->vm_start);
    __D("mmap: vma->vm_end       = %#lx\n", vma->vm_end);
    __D("mmap: size              = %#lx\n", size);
    __D("mmap: vma->vm_pgoff     = %#lx\n", vma->vm_pgoff);

    physp = (unsigned long long)vma->vm_pgoff << PAGE_SHIFT;

    if (mutex_lock_interruptible(&cmem_mutex)) {
	return -ERESTARTSYS;
    }

    s = size;
    entry = find_busy_entry(physp, NULL, NULL, NULL, &s);
    mutex_unlock(&cmem_mutex);

    if (entry != NULL) {
	if (size > entry->size) {
	    __E("mmap: requested size %#llx too big (should be <= %#llx)\n",
	        (unsigned long long)size, (unsigned long long)entry->size);

	    return -EINVAL;
	}

	if (entry->flags & CMEM_CACHED) {
	    vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) |
                               (L_PTE_MT_WRITEALLOC | L_PTE_MT_BUFFERABLE));
	}
	else {
	    vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	}
	vma->vm_flags |= VM_RESERVED | VM_IO;

	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, size,
	                    vma->vm_page_prot)) {
	    __E("mmap: failed remap_pfn_range\n");

	    return -EAGAIN;
	}

	return 0;
    }
    else {
	__E("mmap: can't find allocated buffer with physp %#llx\n",
	    (unsigned long long)physp);

	return -EINVAL;
    }
}

static int open(struct inode *inode, struct file *filp)
{
    __D("open: called.\n");

    atomic_inc(&reference_count);

    return 0;
}

static int release(struct inode *inode, struct file *filp)
{
    struct list_head *registeredlistp;
    struct list_head *freelistp;
    struct list_head *busylistp;
    struct list_head *e;
    struct list_head *u;
    struct list_head *next;
    struct list_head *unext;
    struct pool_buffer *entry;
    struct registered_user *user;
    int last_close = 0;
    int num_pools;
    int bi;
    int i;

    __D("close: called.\n");

    /* Force free all buffers owned by the 'current' process */

    if (atomic_dec_and_test(&reference_count)) {
        __D("close: all references closed, force freeing all busy buffers.\n");

	last_close = 1;
    }

    for (bi = 0; bi < (NBLOCKS + 1); bi++) {
	num_pools = npools[bi];
	if (heap_pool[bi] != -1) {
	    num_pools++;
	}

	/* Clean up any buffers on the busy list when cmem is closed */
	for (i = 0; i < num_pools; i++) {
	    __D("Forcing free on pool %d\n", i);

	    /* acquire the mutex in case this isn't the last close */
	    if (mutex_lock_interruptible(&cmem_mutex)) {
		return -ERESTARTSYS;
	    }

	    freelistp = &p_objs[bi][i].freelist;
	    busylistp = &p_objs[bi][i].busylist;

	    e = busylistp->next;
	    while (e != busylistp) {
		__D("busy entry(s) found\n");

		next = e->next;

		entry = list_entry(e, struct pool_buffer, element);
		registeredlistp = &entry->users;
		u = registeredlistp->next;
		while (u != registeredlistp) {
		    unext = u->next;

		    user = list_entry(u, struct registered_user, element);

		    if (last_close || user->filp == filp) {
			__D("Removing file 0x%p from user list of buffer %#llx...\n",
			    user->filp, (unsigned long long)entry->physp);

			list_del(u);
			kfree(user);
		    }

		    u = unext;
		}

		if (registeredlistp->next == registeredlistp) {
		    /* no more registered users, free buffer */

		    if ((heap_pool[bi] != -1) && (i == (num_pools - 1))) {
			/* HEAP */
			__D("Warning: Freeing 'busy' buffer from heap at "
			    "%#llx\n", (unsigned long long)entry->physp);

			if (bi == NBLOCKS) {
			    dma_free_coherent(NULL, entry->size,
			                      entry->kvirtp, entry->dma);
			}
			else {
			    HeapMem_free(bi, entry->physp, entry->size);
			}
			list_del(e);
			kfree(entry);
		    }
		    else {
			/* POOL */
			__D("Warning: Putting 'busy' buffer from pool %d at "
			    "%#llx on freelist\n",
			    i, (unsigned long long)entry->physp);

			list_del_init(e);
			list_add(e, freelistp);
		    }
		}

		e = next;
	    }

	    mutex_unlock(&cmem_mutex);
	}
    }

    __D("close: returning\n");

    return 0;
}

static void banner(void)
{
    printk(KERN_INFO "CMEMK module: reference Linux version %d.%d.%d\n",
           (LINUX_VERSION_CODE & 0x00ff0000) >> 16,
           (LINUX_VERSION_CODE & 0x0000ff00) >> 8,
           (LINUX_VERSION_CODE & 0x000000ff) >> 0
          );
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)

/*
 * dt_config needs to set:
 *   block_start[bi]
 *   block_end[bi]
 *   npools[bi]
 *   pool_num_buffers[bi][p]
 *   pool_size[bi][p]
 * for blocks specified in DT
 */
int dt_config(void)
{
    struct device_node *np, *block, *mem;
    int ret;
    u32 tmp[MAX_POOLS * 3];
    unsigned long long addr;
    unsigned long long size;
    int n, p;
    int i;
    int num_pools;
    int addr_cells;
    int size_cells;
    u32 num_buffers;
    u32 pool_size_cells;
    int ints_per_pool;
    unsigned long long buffer_size;
    int block_num;

    np = of_find_compatible_node(NULL, NULL, "ti,cmem");
    if (!np) {
        __D("no cmem node found in device tree\n");
	return -ENODEV;
    }

    __D("found cmem node in device tree, getting child nodes\n");

    if (of_get_available_child_count(np) == 0) {
	__E("no child block node(s) found\n");
	return -EINVAL;
    }

    pool_size_cells = 1;
    if (of_get_property(np, "#pool-size-cells", NULL) != NULL) {
	of_property_read_u32(np, "#pool-size-cells", &pool_size_cells);
    }
    ints_per_pool = pool_size_cells + 1;

    block = NULL;

    while ((block = of_get_next_available_child(np, block)) != NULL) {
	__D("got child\n");

	if (of_property_read_u32(block, "reg", &block_num)) {
	    __E("cmem block has no reg property\n");
	    return -EINVAL;
	}
	if (block_num < 0 || block_num >= NBLOCKS) {
	    __E("cmem block 'address' (reg property) %d out of range\n"
	        "  must be 0 -> %d\n", block_num, NBLOCKS - 1);
	    return -EINVAL;
	}
	if (block_start[block_num] != 0) {
	    __E("cmem block %d already assigned\n", block_num);
	    return -EINVAL;
	}

	__D("  looking for memory-region phandle\n");

	mem = of_parse_phandle(block, "memory-region", 0);
	if (mem) {
	    __D("got memory-region\n");

	    addr_cells = of_n_addr_cells(mem);
	    size_cells = of_n_size_cells(mem);

	    ret = of_property_read_u32_array(mem, "reg", tmp,
	                                     addr_cells + size_cells);
	    if (ret) {
		return ret;
	    }

	    addr = 0;
	    for (i = 0; i < addr_cells; i++) {
		addr <<= 32;
		addr |= tmp[i];
	    }
	    size = 0;
	    for (i = 0; i < size_cells; i++) {
		size <<= 32;
		size |= tmp[addr_cells + i];
	    }

	    block_start[block_num] = addr;
	    block_end[block_num] = addr + size;

	    __D("got addr size: %#llx %#llx\n", addr, size);

	    num_pools = 0;
	    if (of_get_property(block, "cmem-buf-pools", &n) != NULL) {
		/*
		 * n is number of bytes, need multiple of (ints_per_pool * 4)
		 */
		if ((n % (ints_per_pool * 4)) != 0) {
			__E("bad cmem-buf-pools: must be multiple of %d ints\n",
			    ints_per_pool);
			return -EINVAL;
		}

                num_pools = n / (ints_per_pool * 4);
	    }

            if (num_pools > MAX_POOLS) {
		__E("bad cmem-buf-pools: too many pools\n"
		    "  must be <= %d\n", MAX_POOLS);
		return -EINVAL;
	    }

	    __D("num_pools=%d\n", num_pools);

	    npools[block_num] = num_pools;
	    if (num_pools) {
		ret = of_property_read_u32_array(block, "cmem-buf-pools", tmp,
		                                 ints_per_pool * num_pools);
		if (!ret) {
		    n = 0;
		    p = 0;
		    while (num_pools) {
			num_buffers = tmp[n];

			buffer_size = 0;
			for (i = 0; i < pool_size_cells; i++) {
				buffer_size <<= 32;
				buffer_size |= tmp[n + i + 1];
			}
			pool_num_buffers[block_num][p] = num_buffers;
			pool_size[block_num][p] = buffer_size;

			num_pools--;
			p++;
			n += ints_per_pool;

			__D("got a pool: %d x %#llx\n",
			    num_buffers, buffer_size);
		    }
		}
	    }
	}
	else {
	    __E("no memory-region phandle\n");
	    return -EINVAL;
	}
    }

    return 0;
}

#endif /* KERNEL_VERSION >= 3.14.0 */

/*
 * cl_config needs to set:
 *   block_start[bi]
 *   block_end[bi]
 *   npools[bi]
 *   pool_num_buffers[bi][p]
 *   pool_size[bi][p]
 * for blocks *not* specified in DT that *are* specified on the command line
 */
int cl_config(void)
{
    char *pstart[NBLOCKS];
    char *pend[NBLOCKS];
    char **pool_table[MAX_POOLS];
    int err = 0;
    int bi;
    int i;
    char *t;

    /* if allowOverlap != -1 then it was set on the command line (to 0 or 1) */
    if (allowOverlap != -1) {
	pr_warn("cmem_init: allowOverlap parameter has been deprecated, ignoring...\n");
    }

    if (npools[0] > MAX_POOLS) {
	__E("Too many pools specified (%d) for Block 0, only %d supported.\n", npools[0], MAX_POOLS);
	return -EINVAL;
    }

    if (npools[1] > MAX_POOLS) {
	__E("Too many pools specified (%d) for Block 1, only %d supported.\n", npools[1], MAX_POOLS);
	return -EINVAL;
    }

    if (npools[2] > MAX_POOLS) {
	__E("Too many pools specified (%d) for Block 2, only %d supported.\n", npools[2], MAX_POOLS);
	return -EINVAL;
    }

/* cut-and-paste below as part of adding support for more than 4 blocks */
    if (npools[3] > MAX_POOLS) {
	__E("Too many pools specified (%d) for Block 3, only %d supported.\n", npools[3], MAX_POOLS);
	return -EINVAL;
    }
/* cut-and-paste above as part of adding support for more than 4 blocks */

    pstart[0] = phys_start;
    pend[0] = phys_end;
    pool_table[0] = pools;

    pstart[1] = phys_start_1;
    pend[1] = phys_end_1;
    pool_table[1] = pools_1;

    pstart[2] = phys_start_2;
    pend[2] = phys_end_2;
    pool_table[2] = pools_2;

/* cut-and-paste below as part of adding support for more than 4 blocks */
    pstart[3] = phys_start_3;
    pend[3] = phys_end_3;
    pool_table[3] = pools_3;
/* cut-and-paste above as part of adding support for more than 4 blocks */

    for (bi = 0; bi < NBLOCKS; bi++) {
	if (!pstart[bi]) {
	    continue;
	}

	if (block_start[bi]) {
	    __D("block %d specified in DT, ignoring cmd line\n", bi);
	    continue;
	}

	/* Get the start and end of CMEM memory */
	block_start[bi] = PAGE_ALIGN(simple_strtoll(pstart[bi], NULL, 16));
	block_end[bi] = PAGE_ALIGN(simple_strtoll(pend[bi], NULL, 16));

	/* Parse the pools */
	for (i = 0; i < npools[bi]; i++) {
	    t = strsep(&pool_table[bi][i], "x");
	    if (!t) {
		err = -EINVAL;
		goto fail;
	    }
	    pool_num_buffers[bi][i] = simple_strtol(t, NULL, 10);

	    t = strsep(&pool_table[bi][i], "\0");
	    if (!t) {
		err = -EINVAL;
		goto fail;
	    }
	    pool_size[bi][i] = simple_strtoll(t, NULL, 10);
	}
    }

fail:
    return err;
}

int __init cmem_init(void)
{
    int bi;
    int i;
    int err;
    unsigned long long length;
    HeapMem_Header *header;
    char tmp_str[4];
    void *virtp;

    banner();

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
    if ((err = dt_config()) == -EINVAL) {
        __E("bad DT config\n");
	return err;
    }
    else {
	if (err == -ENODEV) {
	    __D("no DT config\n");
	}
    }
#endif /* KERNEL_VERSION >= 3.14.0 */

    if ((err = cl_config()) != 0) {
	__E("error %d processing command line\n", err);
	return err;
    }

    mutex_init(&cmem_mutex);

    cmem_major = register_chrdev(0, "cmem", &cmem_fxns);

    if (cmem_major < 0) {
        __E("Failed to allocate major number.\n");
        return -ENODEV;
    }

    __D("Allocated major number: %d\n", cmem_major);

    cmem_class = class_create(THIS_MODULE, "cmem");
    if (IS_ERR(cmem_class)) {
        __E("Error creating cmem device class.\n");
	err = -EIO;
	goto fail_after_reg;
    }

#if IS_ENABLED(CONFIG_ARCH_KEYSTONE) && IS_ENABLED(CONFIG_ARM_LPAE) \
	&& (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
    cmem_cma_dev_0 = device_create(cmem_class, NULL, MKDEV(cmem_major, 0),
				   NULL, "cmem");

    cmem_cma_dev_0->coherent_dma_mask = DMA_BIT_MASK(32);
    cmem_cma_dev_0->dma_pfn_offset = KEYSTONE_DMA_PFN_OFFSET;
#else
    device_create(cmem_class, NULL, MKDEV(cmem_major, 0), NULL, "cmem");
#endif
    for (bi = 0; bi < NBLOCKS; bi++) {
	if (!block_start[bi] || !block_end[bi]) {
            if (bi != 0) {
                continue;
            }

	    /* we know block 0 wasn't specified, ensure no pools for it */
	    if (pool_num_buffers[0][0]) {
		__E("pools specified: must specify both phys_start and phys_end, exiting...\n");
		err = -EINVAL;
		goto fail_after_create;
	    }
	    else {
		printk(KERN_INFO "no physical memory specified\n");

		break;
	    }
	}

	length = block_end[bi] - block_start[bi];

	if (block_start[bi] == 0) {
	    sprintf(tmp_str, "_%d", bi);
	    __E("Physical address of 0 not allowed (phys_start%s)\n",
	        bi == 0 ? "" : tmp_str);
	    __E("  (minimum physical address is %#lx)\n", PAGE_SIZE);
	    err = -EINVAL;
	    goto fail_after_create;
	}

	if (block_end[bi] < block_start[bi]) {
	    __E("phys_end (%#llx) < phys_start (%#llx)\n",
	        block_end[bi], block_start[bi]);
	    err = -EINVAL;
	    goto fail_after_create;
	}

	block_avail_size[bi] = length;

	__D("calling request_mem_region(%#llx, %#llx, \"CMEM\")\n",
	    block_start[bi], length);

	if (!request_mem_region(block_start[bi], length, "CMEM")) {
	    __E("Failed to request_mem_region(%#llx, %#llx)\n",
	        block_start[bi], length);
	    err = -EFAULT;
	    goto fail_after_create;
	}
	else {
	    block_flags[bi] |= BLOCK_MEMREGION;
	}

	/* Allocate the pools */
	for (i = 0; i < npools[bi]; i++) {
	    if (alloc_pool(bi, i, pool_num_buffers[bi][i], pool_size[bi][i],
                NULL) < 0) {
		__E("Failed to alloc pool of size 0x%llu and number of buffers %d\n", pool_size[bi][i], pool_num_buffers[bi][i]);
		err = -ENOMEM;
		goto fail_after_create;
	    }

	    total_num_buffers[bi] += pool_num_buffers[bi][i];
	}

	/* use whatever is left for the heap */
	heap_size[bi] = block_avail_size[bi] & PAGE_MASK;
	if (heap_size[bi] > 0) {
	    err = alloc_pool(bi, npools[bi], 1, heap_size[bi], &heap_physp[bi]);
	    if (err < 0) {
		__E("Failed to alloc heap of size %#lx\n", heap_size[bi]);
		goto fail_after_create;
	    }
	    printk(KERN_INFO "allocated heap buffer %#llx of size %#lx\n",
		   (unsigned long long)heap_physp[bi], heap_size[bi]);
	    heap_pool[bi] = npools[bi];
	    heap_head[bi].next = heap_physp[bi];
	    heap_head[bi].size = heap_size[bi];

	    map_header((void **)&virtp, heap_physp[bi], &ioremap_area);

	    header = (HeapMem_Header *)virtp;
	    header->next = 0;
	    header->size = heap_size[bi];

	    unmap_header(virtp, ioremap_area);

	    if (useHeapIfPoolUnavailable) {
		printk(KERN_INFO "heap fallback enabled - will try heap if "
		       "pool buffer is not available\n");
	    }
	}
	else {
	    __D("no remaining memory for heap, no heap created "
	           "for memory block %d\n", bi);
	    heap_head[bi].next = 0;
	}

	__D("cmem initialized %d pools between %#llx and %#llx\n",
	       npools[bi], block_start[bi], block_end[bi]);

	nblocks++;
    }


    if (cmem_cma_npools == 0) {
	/* no explicit pools, assuming global CMA area */
	__D("no CMEM CMA pools found\n");

	INIT_LIST_HEAD(&p_objs[NBLOCKS][0].busylist);
	p_objs[NBLOCKS][0].reqsize = 0;
	p_objs[NBLOCKS][0].size = 0;
	p_objs[NBLOCKS][0].numbufs = 1;

	heap_pool[NBLOCKS] = 0;
	npools[NBLOCKS] = 0;
    }
    else {
	__D("%d CMEM CMA pools\n", cmem_cma_npools);

	for (i = 0; i < cmem_cma_npools; i++) {
	    INIT_LIST_HEAD(&p_objs[NBLOCKS][i].busylist);
	    p_objs[NBLOCKS][i].reqsize = cmem_cma_p_objs[i].reqsize;
	    p_objs[NBLOCKS][i].size = cmem_cma_p_objs[i].size;
	    p_objs[NBLOCKS][i].numbufs = cmem_cma_p_objs[i].numbufs;

	    cmem_cma_dev[i].coherent_dma_mask = DMA_BIT_MASK(32);
#if IS_ENABLED(CONFIG_ARCH_KEYSTONE) && IS_ENABLED(CONFIG_ARM_LPAE) \
	&& (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
	    cmem_cma_dev[i].dma_pfn_offset = KEYSTONE_DMA_PFN_OFFSET;
#endif
	    __D("    pool %d: size=%#llx numbufs=%d\n", i,
	        p_objs[NBLOCKS][i].size, p_objs[NBLOCKS][i].numbufs);
	}

	if (cmem_cma_heapsize) {
	    /* already init'ed p_objs in loop above */
	    heap_pool[NBLOCKS] = cmem_cma_npools - 1;
	    npools[NBLOCKS] = cmem_cma_npools - 1;
	}
	else {
	    INIT_LIST_HEAD(&p_objs[NBLOCKS][cmem_cma_npools].busylist);
	    p_objs[NBLOCKS][cmem_cma_npools].reqsize = 0;
	    p_objs[NBLOCKS][cmem_cma_npools].size = 0;
	    p_objs[NBLOCKS][cmem_cma_npools].numbufs = 1;

	    heap_pool[NBLOCKS] = cmem_cma_npools;
	    npools[NBLOCKS] = cmem_cma_npools;
	}
    }

    /* Create the /proc entry */
    cmem_proc_entry = proc_create("cmem", 0, NULL, &cmem_proc_ops);

    printk(KERN_INFO "cmemk initialized\n");

    return 0;

fail_after_create:

    length = block_end[bi] - block_start[bi];

    for (bi = 0; bi < NBLOCKS; bi++) {
	if (block_flags[bi] & BLOCK_MEMREGION) {
	    __D("calling release_mem_region(%#llx, %#llx)...\n",
	        block_start[bi], length);

	    release_mem_region(block_start[bi], length);

	    block_flags[bi] &= ~BLOCK_MEMREGION;
	}
    }

    device_destroy(cmem_class, MKDEV(cmem_major, 0));
    class_destroy(cmem_class);

fail_after_reg:
    __D("Unregistering character device cmem\n");
    unregister_chrdev(cmem_major, "cmem");

    return err;
}

void __exit cmem_exit(void)
{
    struct list_head *registeredlistp;
    struct list_head *freelistp;
    struct list_head *busylistp;
    struct list_head *e;
    struct list_head *u;
    struct list_head *unext;
    struct pool_buffer *entry;
    struct registered_user *user;
    unsigned long long length;
    int num_pools;
    int bi;
    int i;

    __D("In cmem_exit()\n");

    /* Remove the /proc entry */
    remove_proc_entry("cmem", NULL);

    for (bi = 0; bi < NBLOCKS; bi++) {
	num_pools = npools[bi];

	if (heap_pool[bi] != -1) {
	    num_pools++;
	}

	/* Free the pool structures and empty the lists. */
	for (i = 0; i < num_pools; i++) {
	    __D("Freeing memory associated with pool %d\n", i);

	    freelistp = &p_objs[bi][i].freelist;
	    busylistp = &p_objs[bi][i].busylist;

	    e = busylistp->next;
	    while (e != busylistp) {
		entry = list_entry(e, struct pool_buffer, element);

		__D("Warning: Freeing busy entry %d at %#llx\n",
		    entry->id, (unsigned long long)entry->physp);

		registeredlistp = &entry->users;
		u = registeredlistp->next;
		while (u != registeredlistp) {
		    unext = u->next;

		    user = list_entry(u, struct registered_user, element);

		    __D("Removing file 0x%p from user list of buffer %#llx...\n",
			user->filp, (unsigned long long)entry->physp);

		    list_del(u);
		    kfree(user);

		    u = unext;
		}

		e = e->next;
		kfree(entry);
	    }

	    e = freelistp->next;
	    while (e != freelistp) {
		entry = list_entry(e, struct pool_buffer, element);

		__D("Freeing free entry %d at %#llx\n",
		    entry->id, (unsigned long long)entry->physp);

		registeredlistp = &entry->users;
		u = registeredlistp->next;
		while (u != registeredlistp) {
		    /* should never happen, but check to avoid mem leak */
		    unext = u->next;

		    user = list_entry(u, struct registered_user, element);

		    __D("Removing file 0x%p from user list of buffer %#llx...\n",
			user->filp, (unsigned long long)entry->physp);

		    list_del(u);
		    kfree(user);

		    u = unext;
		}

		e = e->next;
		kfree(entry);
	    }
	}

	length = block_end[bi] - block_start[bi];

	if (block_flags[bi] & BLOCK_MEMREGION) {
	    __D("calling release_mem_region(%#llx, %#llx)...\n",
	        block_start[bi], length);

	    release_mem_region(block_start[bi], length);

	    block_flags[bi] &= ~BLOCK_MEMREGION;
	}
    }

    device_destroy(cmem_class, MKDEV(cmem_major, 0));
    class_destroy(cmem_class);

    __D("Unregistering character device cmem\n");
    unregister_chrdev(cmem_major, "cmem");

    printk(KERN_INFO "cmemk unregistered\n");
}

MODULE_LICENSE("GPL");
module_init(cmem_init);
module_exit(cmem_exit);