aboutsummaryrefslogtreecommitdiffstats
path: root/mm/cma.c
blob: 2e5805b70dc309b8f02fcf5d327d755346f71f23 (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
/*
 * Contiguous Memory Allocator framework
 * Copyright (c) 2010 by Samsung Electronics.
 * Written by Michal Nazarewicz (m.nazarewicz@samsung.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; either version 2 of the
 * License or (at your optional) any later version of the license.
 */

/*
 * See Documentation/contiguous-memory.txt for details.
 */

#define pr_fmt(fmt) "cma: " fmt

#ifdef CONFIG_CMA_DEBUG
#  define DEBUG
#endif

#ifndef CONFIG_NO_BOOTMEM
#  include <linux/bootmem.h>   /* alloc_bootmem_pages_nopanic() */
#endif
#ifdef CONFIG_HAVE_MEMBLOCK
#  include <linux/memblock.h>  /* memblock*() */
#endif
#include <linux/device.h>      /* struct device, dev_name() */
#include <linux/errno.h>       /* Error numbers */
#include <linux/err.h>         /* IS_ERR, PTR_ERR, etc. */
#include <linux/mm.h>          /* PAGE_ALIGN() */
#include <linux/module.h>      /* EXPORT_SYMBOL_GPL() */
#include <linux/mutex.h>       /* mutex */
#include <linux/slab.h>        /* kmalloc() */
#include <linux/string.h>      /* str*() */

#include <linux/cma.h>
#include <linux/vmalloc.h>

/*
 * Protects cma_regions, cma_allocators, cma_map, cma_map_length,
 * cma_kobj, cma_sysfs_regions and cma_chunks_by_start.
 */
static DEFINE_MUTEX(cma_mutex);



/************************* Map attribute *************************/

static const char *cma_map;
static size_t cma_map_length;

/*
 * map-attr      ::= [ rules [ ';' ] ]
 * rules         ::= rule [ ';' rules ]
 * rule          ::= patterns '=' regions
 * patterns      ::= pattern [ ',' patterns ]
 * regions       ::= REG-NAME [ ',' regions ]
 * pattern       ::= dev-pattern [ '/' TYPE-NAME ] | '/' TYPE-NAME
 *
 * See Documentation/contiguous-memory.txt for details.
 */
static ssize_t cma_map_validate(const char *param)
{
	const char *ch = param;

	if (*ch == '\0' || *ch == '\n')
		return 0;

	for (;;) {
		const char *start = ch;

		while (*ch && *ch != '\n' && *ch != ';' && *ch != '=')
			++ch;

		if (*ch != '=' || start == ch) {
			pr_err("map: expecting \"<patterns>=<regions>\" near %s\n",
			       start);
			return -EINVAL;
		}

		while (*++ch != ';')
			if (*ch == '\0' || *ch == '\n')
				return ch - param;
		if (ch[1] == '\0' || ch[1] == '\n')
			return ch - param;
		++ch;
	}
}

static int __init cma_map_param(char *param)
{
	ssize_t len;

	pr_debug("param: map: %s\n", param);

	len = cma_map_validate(param);
	if (len < 0)
		return len;

	cma_map = param;
	cma_map_length = len;
	return 0;
}

#if defined CONFIG_CMA_CMDLINE

early_param("cma.map", cma_map_param);

#endif



/************************* Early regions *************************/

struct list_head cma_early_regions __initdata =
	LIST_HEAD_INIT(cma_early_regions);

#ifdef CONFIG_CMA_CMDLINE

/*
 * regions-attr ::= [ regions [ ';' ] ]
 * regions      ::= region [ ';' regions ]
 *
 * region       ::= [ '-' ] reg-name
 *                    '=' size
 *                  [ '@' start ]
 *                  [ '/' alignment ]
 *                  [ ':' alloc-name ]
 *
 * See Documentation/contiguous-memory.txt for details.
 *
 * Example:
 * cma=reg1=64M:bf;reg2=32M@0x100000:bf;reg3=64M/1M:bf
 *
 * If allocator is ommited the first available allocater will be used.
 */

#define NUMPARSE(cond_ch, type, cond) ({				\
		unsigned long long v = 0;				\
		if (*param == (cond_ch)) {				\
			const char *const msg = param + 1;		\
			v = memparse(msg, &param);			\
			if (!v || v > ~(type)0 || !(cond)) {		\
				pr_err("param: invalid value near %s\n", msg); \
				ret = -EINVAL;				\
				break;					\
			}						\
		}							\
		v;							\
	})

static int __init cma_param_parse(char *param)
{
	static struct cma_region regions[16];

	size_t left = ARRAY_SIZE(regions);
	struct cma_region *reg = regions;
	int ret = 0;

	pr_debug("param: %s\n", param);

	for (; *param; ++reg) {
		dma_addr_t start, alignment;
		size_t size;

		if (unlikely(!--left)) {
			pr_err("param: too many early regions\n");
			return -ENOSPC;
		}

		/* Parse name */
		reg->name = param;
		param = strchr(param, '=');
		if (!param || param == reg->name) {
			pr_err("param: expected \"<name>=\" near %s\n",
			       reg->name);
			ret = -EINVAL;
			break;
		}
		*param = '\0';

		/* Parse numbers */
		size      = NUMPARSE('\0', size_t, true);
		start     = NUMPARSE('@', dma_addr_t, true);
		alignment = NUMPARSE('/', dma_addr_t, (v & (v - 1)) == 0);

		alignment = max(alignment, (dma_addr_t)PAGE_SIZE);
		start     = ALIGN(start, alignment);
		size      = PAGE_ALIGN(size);
		if (start + size < start) {
			pr_err("param: invalid start, size combination\n");
			ret = -EINVAL;
			break;
		}

		/* Parse allocator */
		if (*param == ':') {
			reg->alloc_name = ++param;
			while (*param && *param != ';')
				++param;
			if (param == reg->alloc_name)
				reg->alloc_name = NULL;
		}

		/* Go to next */
		if (*param == ';') {
			*param = '\0';
			++param;
		} else if (*param) {
			pr_err("param: expecting ';' or end of parameter near %s\n",
			       param);
			ret = -EINVAL;
			break;
		}

		/* Add */
		reg->size      = size;
		reg->start     = start;
		reg->alignment = alignment;
		reg->copy_name = 1;

		list_add_tail(&reg->list, &cma_early_regions);

		pr_debug("param: registering early region %s (%p@%p/%p)\n",
			 reg->name, (void *)reg->size, (void *)reg->start,
			 (void *)reg->alignment);
	}

	return ret;
}
early_param("cma", cma_param_parse);

#undef NUMPARSE

#endif


int __init __must_check cma_early_region_register(struct cma_region *reg)
{
	dma_addr_t start, alignment;
	size_t size;

	if (reg->alignment & (reg->alignment - 1))
		return -EINVAL;

	alignment = max(reg->alignment, (dma_addr_t)PAGE_SIZE);
	start     = ALIGN(reg->start, alignment);
	size      = PAGE_ALIGN(reg->size);

	if (start + size < start)
		return -EINVAL;

	reg->size      = size;
	reg->start     = start;
	reg->alignment = alignment;

	list_add_tail(&reg->list, &cma_early_regions);

	pr_debug("param: registering early region %s (%p@%p/%p)\n",
		 reg->name, (void *)reg->size, (void *)reg->start,
		 (void *)reg->alignment);

	return 0;
}



/************************* Regions & Allocators *************************/

static void __cma_sysfs_region_add(struct cma_region *reg);

static int __cma_region_attach_alloc(struct cma_region *reg);
static void __maybe_unused __cma_region_detach_alloc(struct cma_region *reg);


/* List of all regions.  Named regions are kept before unnamed. */
static LIST_HEAD(cma_regions);

#define cma_foreach_region(reg) \
	list_for_each_entry(reg, &cma_regions, list)

bool cma_is_registered_region(phys_addr_t start, size_t size)
{
	struct cma_region *reg;

	if (start + size <= start)
		return false;

	cma_foreach_region(reg) {
		if ((start >= reg->start) &&
			((start + size) <= (reg->start + reg->size)) &&
			(size <= reg->size) &&
			(start < (reg->start + reg->size)))

			return true;
	}
	return false;
}

int __must_check cma_region_register(struct cma_region *reg)
{
	const char *name, *alloc_name;
	struct cma_region *r;
	char *ch = NULL;
	int ret = 0;

	if (!reg->size || reg->start + reg->size < reg->start)
		return -EINVAL;

	reg->users = 0;
	reg->used = 0;
	reg->private_data = NULL;
	reg->registered = 0;
	reg->free_space = reg->size;

	/* Copy name and alloc_name */
	name = reg->name;
	alloc_name = reg->alloc_name;
	if (reg->copy_name && (reg->name || reg->alloc_name)) {
		size_t name_size, alloc_size;

		name_size  = reg->name       ? strlen(reg->name) + 1       : 0;
		alloc_size = reg->alloc_name ? strlen(reg->alloc_name) + 1 : 0;

		ch = kmalloc(name_size + alloc_size, GFP_KERNEL);
		if (!ch) {
			pr_err("%s: not enough memory to allocate name\n",
			       reg->name ?: "(private)");
			return -ENOMEM;
		}

		if (name_size) {
			memcpy(ch, reg->name, name_size);
			name = ch;
			ch += name_size;
		}

		if (alloc_size) {
			memcpy(ch, reg->alloc_name, alloc_size);
			alloc_name = ch;
		}
	}

	mutex_lock(&cma_mutex);

	/* Don't let regions overlap */
	cma_foreach_region(r)
		if (r->start + r->size > reg->start &&
		    r->start < reg->start + reg->size) {
			ret = -EADDRINUSE;
			goto done;
		}

	if (reg->alloc) {
		ret = __cma_region_attach_alloc(reg);
		if (unlikely(ret < 0))
			goto done;
	}

	reg->name = name;
	reg->alloc_name = alloc_name;
	reg->registered = 1;
	ch = NULL;

	/*
	 * Keep named at the beginning and unnamed (private) at the
	 * end.  This helps in traversal when named region is looked
	 * for.
	 */
	if (name)
		list_add(&reg->list, &cma_regions);
	else
		list_add_tail(&reg->list, &cma_regions);

	__cma_sysfs_region_add(reg);

done:
	mutex_unlock(&cma_mutex);

	pr_debug("%s: region %sregistered\n",
		 reg->name ?: "(private)", ret ? "not " : "");
	kfree(ch);

	return ret;
}
EXPORT_SYMBOL_GPL(cma_region_register);

static struct cma_region *__must_check
__cma_region_find(const char **namep)
{
	struct cma_region *reg;
	const char *ch, *name;
	size_t n;

	ch = *namep;
	while (*ch && *ch != ',' && *ch != ';')
		++ch;
	name = *namep;
	*namep = *ch == ',' ? ch + 1 : ch;
	n = ch - name;

	/*
	 * Named regions are kept in front of unnamed so if we
	 * encounter unnamed region we can stop.
	 */
	cma_foreach_region(reg)
		if (!reg->name)
			break;
		else if (!strncmp(name, reg->name, n) && !reg->name[n])
			return reg;

	return NULL;
}


/* List of all allocators. */
static LIST_HEAD(cma_allocators);

#define cma_foreach_allocator(alloc) \
	list_for_each_entry(alloc, &cma_allocators, list)

int cma_allocator_register(struct cma_allocator *alloc)
{
	struct cma_region *reg;
	int first;

	if (!alloc->alloc || !alloc->free)
		return -EINVAL;

	mutex_lock(&cma_mutex);

	first = list_empty(&cma_allocators);

	list_add_tail(&alloc->list, &cma_allocators);

	/*
	 * Attach this allocator to all allocator-less regions that
	 * request this particular allocator (reg->alloc_name equals
	 * alloc->name) or if region wants the first available
	 * allocator and we are the first.
	 */
	cma_foreach_region(reg) {
		if (reg->alloc)
			continue;
		if (reg->alloc_name
		  ? alloc->name && !strcmp(alloc->name, reg->alloc_name)
		  : (!reg->used && first))
			continue;

		reg->alloc = alloc;
		__cma_region_attach_alloc(reg);
	}

	mutex_unlock(&cma_mutex);

	pr_debug("%s: allocator registered\n", alloc->name ?: "(unnamed)");

	return 0;
}
EXPORT_SYMBOL_GPL(cma_allocator_register);

static struct cma_allocator *__must_check
__cma_allocator_find(const char *name)
{
	struct cma_allocator *alloc;

	if (!name)
		return list_empty(&cma_allocators)
			? NULL
			: list_entry(cma_allocators.next,
				     struct cma_allocator, list);

	cma_foreach_allocator(alloc)
		if (alloc->name && !strcmp(name, alloc->name))
			return alloc;

	return NULL;
}



/************************* Initialise CMA *************************/

int __init cma_set_defaults(struct cma_region *regions, const char *map)
{
	if (map) {
		int ret = cma_map_param((char *)map);
		if (unlikely(ret < 0))
			return ret;
	}

	if (!regions)
		return 0;

	for (; regions->size; ++regions) {
		int ret = cma_early_region_register(regions);
		if (unlikely(ret < 0))
			return ret;
	}

	return 0;
}


int __init cma_early_region_reserve(struct cma_region *reg)
{
	int tried = 0;

	if (!reg->size || (reg->alignment & (reg->alignment - 1)) ||
	    reg->reserved)
		return -EINVAL;

#ifndef CONFIG_NO_BOOTMEM

	tried = 1;

	{
		void *ptr = __alloc_bootmem_nopanic(reg->size, reg->alignment,
						    reg->start);
		if (ptr) {
			reg->start = virt_to_phys(ptr);
			reg->reserved = 1;
			return 0;
		}
	}

#endif

#ifdef CONFIG_HAVE_MEMBLOCK

	tried = 1;

	if (reg->start) {
		if (!memblock_is_region_reserved(reg->start, reg->size) &&
		    memblock_reserve(reg->start, reg->size) >= 0) {
			reg->reserved = 1;
			return 0;
		}
	} else {
		/*
		 * Use __memblock_alloc_base() since
		 * memblock_alloc_base() panic()s.
		 */
		u64 ret = __memblock_alloc_base(reg->size, reg->alignment, 0);
		if (ret &&
		    ret < ~(dma_addr_t)0 &&
		    ret + reg->size < ~(dma_addr_t)0 &&
		    ret + reg->size > ret) {
			reg->start = ret;
			reg->reserved = 1;
			return 0;
		}

		if (ret)
			memblock_free(ret, reg->size);
	}

#endif

	return tried ? -ENOMEM : -EOPNOTSUPP;
}

void __init cma_early_regions_reserve(int (*reserve)(struct cma_region *reg))
{
	struct cma_region *reg;

	pr_debug("init: reserving early regions\n");

	if (!reserve)
		reserve = cma_early_region_reserve;

	list_for_each_entry(reg, &cma_early_regions, list) {
		if (reg->reserved) {
			/* nothing */
		} else if (reserve(reg) >= 0) {
			pr_debug("init: %s: reserved %p@%p\n",
				 reg->name ?: "(private)",
				 (void *)reg->size, (void *)reg->start);
			reg->reserved = 1;
		} else {
			pr_warn("init: %s: unable to reserve %p@%p/%p\n",
				reg->name ?: "(private)",
				(void *)reg->size, (void *)reg->start,
				(void *)reg->alignment);
		}
	}
}


static int __init cma_init(void)
{
	struct cma_region *reg, *n;

	pr_debug("init: initialising\n");

	if (cma_map) {
		char *val = kmemdup(cma_map, cma_map_length + 1, GFP_KERNEL);
		cma_map = val;
		if (!val)
			return -ENOMEM;
		val[cma_map_length] = '\0';
	}

	list_for_each_entry_safe(reg, n, &cma_early_regions, list) {
		INIT_LIST_HEAD(&reg->list);
		/*
		 * We don't care if there was an error.  It's a pity
		 * but there's not much we can do about it any way.
		 * If the error is on a region that was parsed from
		 * command line then it will stay and waste a bit of
		 * space; if it was registered using
		 * cma_early_region_register() it's caller's
		 * responsibility to do something about it.
		 */
		if (reg->reserved && cma_region_register(reg) < 0)
			/* ignore error */;
	}

	INIT_LIST_HEAD(&cma_early_regions);

	return 0;
}
/*
 * We want to be initialised earlier than module_init/__initcall so
 * that drivers that want to grab memory at boot time will get CMA
 * ready.  subsys_initcall() seems early enough and not too early at
 * the same time.
 */
subsys_initcall(cma_init);



/************************* SysFS *************************/

#if defined CONFIG_CMA_SYSFS

static struct kobject cma_sysfs_regions;
static int cma_sysfs_regions_ready;


#define CMA_ATTR_INLINE(_type, _name)					\
	(&((struct cma_ ## _type ## _attribute){			\
		.attr	= {						\
			.name	= __stringify(_name),			\
			.mode	= 0644,					\
		},							\
		.show	= cma_sysfs_ ## _type ## _ ## _name ## _show,	\
		.store	= cma_sysfs_ ## _type ## _ ## _name ## _store,	\
	}).attr)

#define CMA_ATTR_RO_INLINE(_type, _name)				\
	(&((struct cma_ ## _type ## _attribute){			\
		.attr	= {						\
			.name	= __stringify(_name),			\
			.mode	= 0444,					\
		},							\
		.show	= cma_sysfs_ ## _type ## _ ## _name ## _show,	\
	}).attr)


struct cma_root_attribute {
	struct attribute attr;
	ssize_t (*show)(char *buf);
	int (*store)(const char *buf);
};

static ssize_t cma_sysfs_root_map_show(char *page)
{
	ssize_t len;

	len = cma_map_length;
	if (!len) {
		*page = 0;
		len = 0;
	} else {
		if (len > (size_t)PAGE_SIZE - 1)
			len = (size_t)PAGE_SIZE - 1;
		memcpy(page, cma_map, len);
		page[len++] = '\n';
	}

	return len;
}

static int cma_sysfs_root_map_store(const char *page)
{
	ssize_t len = cma_map_validate(page);
	char *val = NULL;

	if (len < 0)
		return len;

	if (len) {
		val = kmemdup(page, len + 1, GFP_KERNEL);
		if (!val)
			return -ENOMEM;
		val[len] = '\0';
	}

	kfree(cma_map);
	cma_map = val;
	cma_map_length = len;

	return 0;
}

static ssize_t cma_sysfs_root_allocators_show(char *page)
{
	struct cma_allocator *alloc;
	size_t left = PAGE_SIZE;
	char *ch = page;

	cma_foreach_allocator(alloc) {
		ssize_t l = snprintf(ch, left, "%s ", alloc->name ?: "-");
		ch   += l;
		left -= l;
	}

	if (ch != page)
		ch[-1] = '\n';
	return ch - page;
}

static ssize_t
cma_sysfs_root_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
	struct cma_root_attribute *rattr =
		container_of(attr, struct cma_root_attribute, attr);
	ssize_t ret;

	mutex_lock(&cma_mutex);
	ret = rattr->show(buf);
	mutex_unlock(&cma_mutex);

	return ret;
}

static ssize_t
cma_sysfs_root_store(struct kobject *kobj, struct attribute *attr,
		       const char *buf, size_t count)
{
	struct cma_root_attribute *rattr =
		container_of(attr, struct cma_root_attribute, attr);
	int ret;

	mutex_lock(&cma_mutex);
	ret = rattr->store(buf);
	mutex_unlock(&cma_mutex);

	return ret < 0 ? ret : count;
}

static struct kobj_type cma_sysfs_root_type = {
	.sysfs_ops	= &(const struct sysfs_ops){
		.show	= cma_sysfs_root_show,
		.store	= cma_sysfs_root_store,
	},
	.default_attrs	= (struct attribute * []) {
		CMA_ATTR_INLINE(root, map),
		CMA_ATTR_RO_INLINE(root, allocators),
		NULL
	},
};

static int __init cma_sysfs_init(void)
{
	static struct kobject root;
	static struct kobj_type fake_type;

	struct cma_region *reg;
	int ret;

	/* Root */
	ret = kobject_init_and_add(&root, &cma_sysfs_root_type,
				   mm_kobj, "contiguous");
	if (unlikely(ret < 0)) {
		pr_err("init: unable to add root kobject: %d\n", ret);
		return ret;
	}

	/* Regions */
	ret = kobject_init_and_add(&cma_sysfs_regions, &fake_type,
				   &root, "regions");
	if (unlikely(ret < 0)) {
		pr_err("init: unable to add regions kobject: %d\n", ret);
		return ret;
	}

	mutex_lock(&cma_mutex);
	cma_sysfs_regions_ready = 1;
	cma_foreach_region(reg)
		__cma_sysfs_region_add(reg);
	mutex_unlock(&cma_mutex);

	return 0;
}
device_initcall(cma_sysfs_init);



struct cma_region_attribute {
	struct attribute attr;
	ssize_t (*show)(struct cma_region *reg, char *buf);
	int (*store)(struct cma_region *reg, const char *buf);
};


static ssize_t cma_sysfs_region_name_show(struct cma_region *reg, char *page)
{
	return reg->name ? snprintf(page, PAGE_SIZE, "%s\n", reg->name) : 0;
}

static ssize_t cma_sysfs_region_start_show(struct cma_region *reg, char *page)
{
	return snprintf(page, PAGE_SIZE, "%p\n", (void *)reg->start);
}

static ssize_t cma_sysfs_region_size_show(struct cma_region *reg, char *page)
{
	return snprintf(page, PAGE_SIZE, "%zu\n", reg->size);
}

static ssize_t cma_sysfs_region_free_show(struct cma_region *reg, char *page)
{
	return snprintf(page, PAGE_SIZE, "%zu\n", reg->free_space);
}

static ssize_t cma_sysfs_region_users_show(struct cma_region *reg, char *page)
{
	return snprintf(page, PAGE_SIZE, "%u\n", reg->users);
}

static ssize_t cma_sysfs_region_alloc_show(struct cma_region *reg, char *page)
{
	if (reg->alloc)
		return snprintf(page, PAGE_SIZE, "%s\n",
				reg->alloc->name ?: "-");
	else if (reg->alloc_name)
		return snprintf(page, PAGE_SIZE, "[%s]\n", reg->alloc_name);
	else
		return 0;
}

static int
cma_sysfs_region_alloc_store(struct cma_region *reg, const char *page)
{
	char *s;

	if (reg->alloc && reg->users)
		return -EBUSY;

	if (!*page || *page == '\n') {
		s = NULL;
	} else {
		size_t len;

		for (s = (char *)page; *++s && *s != '\n'; )
			/* nop */;

		len = s - page;
		s = kmemdup(page, len + 1, GFP_KERNEL);
		if (!s)
			return -ENOMEM;
		s[len] = '\0';
	}

	if (reg->alloc)
		__cma_region_detach_alloc(reg);

	if (reg->free_alloc_name)
		kfree(reg->alloc_name);

	reg->alloc_name = s;
	reg->free_alloc_name = !!s;

	return 0;
}


static ssize_t
cma_sysfs_region_show(struct kobject *kobj, struct attribute *attr,
		      char *buf)
{
	struct cma_region *reg = container_of(kobj, struct cma_region, kobj);
	struct cma_region_attribute *rattr =
		container_of(attr, struct cma_region_attribute, attr);
	ssize_t ret;

	mutex_lock(&cma_mutex);
	ret = rattr->show(reg, buf);
	mutex_unlock(&cma_mutex);

	return ret;
}

static int
cma_sysfs_region_store(struct kobject *kobj, struct attribute *attr,
		       const char *buf, size_t count)
{
	struct cma_region *reg = container_of(kobj, struct cma_region, kobj);
	struct cma_region_attribute *rattr =
		container_of(attr, struct cma_region_attribute, attr);
	int ret;

	mutex_lock(&cma_mutex);
	ret = rattr->store(reg, buf);
	mutex_unlock(&cma_mutex);

	return ret < 0 ? ret : count;
}

static struct kobj_type cma_sysfs_region_type = {
	.sysfs_ops	= &(const struct sysfs_ops){
		.show	= cma_sysfs_region_show,
		.store	= cma_sysfs_region_store,
	},
	.default_attrs	= (struct attribute * []) {
		CMA_ATTR_RO_INLINE(region, name),
		CMA_ATTR_RO_INLINE(region, start),
		CMA_ATTR_RO_INLINE(region, size),
		CMA_ATTR_RO_INLINE(region, free),
		CMA_ATTR_RO_INLINE(region, users),
		CMA_ATTR_INLINE(region, alloc),
		NULL
	},
};

static void __cma_sysfs_region_add(struct cma_region *reg)
{
	int ret;

	if (!cma_sysfs_regions_ready)
		return;

	memset(&reg->kobj, 0, sizeof reg->kobj);

	ret = kobject_init_and_add(&reg->kobj, &cma_sysfs_region_type,
				   &cma_sysfs_regions,
				   "%p", (void *)reg->start);

	if (reg->name &&
	    sysfs_create_link(&cma_sysfs_regions, &reg->kobj, reg->name) < 0)
		/* Ignore any errors. */;
}

#else

static void __cma_sysfs_region_add(struct cma_region *reg)
{
	/* nop */
}

#endif


/************************* Chunks *************************/

/* All chunks sorted by start address. */
static struct rb_root cma_chunks_by_start;

static struct cma_chunk *__must_check __cma_chunk_find(dma_addr_t addr)
{
	struct cma_chunk *chunk;
	struct rb_node *n;

	for (n = cma_chunks_by_start.rb_node; n; ) {
		chunk = rb_entry(n, struct cma_chunk, by_start);
		if (addr < chunk->start)
			n = n->rb_left;
		else if (addr > chunk->start)
			n = n->rb_right;
		else
			return chunk;
	}
	WARN(1, KERN_WARNING "no chunk starting at %p\n", (void *)addr);
	return NULL;
}

static int __must_check __cma_chunk_insert(struct cma_chunk *chunk)
{
	struct rb_node **new, *parent = NULL;
	typeof(chunk->start) addr = chunk->start;

	for (new = &cma_chunks_by_start.rb_node; *new; ) {
		struct cma_chunk *c =
			container_of(*new, struct cma_chunk, by_start);

		parent = *new;
		if (addr < c->start) {
			new = &(*new)->rb_left;
		} else if (addr > c->start) {
			new = &(*new)->rb_right;
		} else {
			/*
			 * We should never be here.  If we are it
			 * means allocator gave us an invalid chunk
			 * (one that has already been allocated) so we
			 * refuse to accept it.  Our caller will
			 * recover by freeing the chunk.
			 */
			WARN_ON(1);
			return -EADDRINUSE;
		}
	}

	rb_link_node(&chunk->by_start, parent, new);
	rb_insert_color(&chunk->by_start, &cma_chunks_by_start);

	return 0;
}

static void __cma_chunk_free(struct cma_chunk *chunk)
{
	rb_erase(&chunk->by_start, &cma_chunks_by_start);

	chunk->reg->free_space += chunk->size;
	--chunk->reg->users;

	chunk->reg->alloc->free(chunk);
}


/************************* The Device API *************************/

static const char *__must_check
__cma_where_from(const struct device *dev, const char *type);


/* Allocate. */

static dma_addr_t __must_check
__cma_alloc_from_region(struct cma_region *reg,
			size_t size, dma_addr_t alignment)
{
	struct cma_chunk *chunk;

	pr_debug("allocate %p/%p from %s\n",
		 (void *)size, (void *)alignment,
		 reg ? reg->name ?: "(private)" : "(null)");

	if (!reg || reg->free_space < size)
		return -ENOMEM;

	if (!reg->alloc) {
		if (!reg->used)
			__cma_region_attach_alloc(reg);
		if (!reg->alloc)
			return -ENOMEM;
	}

	chunk = reg->alloc->alloc(reg, size, alignment);
	if (!chunk)
		return -ENOMEM;

	if (unlikely(__cma_chunk_insert(chunk) < 0)) {
		/* We should *never* be here. */
		chunk->reg->alloc->free(chunk);
		kfree(chunk);
		return -EADDRINUSE;
	}

	chunk->reg = reg;
	++reg->users;
	reg->free_space -= chunk->size;
	pr_debug("allocated at %p\n", (void *)chunk->start);
	return chunk->start;
}

dma_addr_t __must_check
cma_alloc_from_region(struct cma_region *reg,
		      size_t size, dma_addr_t alignment)
{
	dma_addr_t addr;

	pr_debug("allocate %p/%p from %s\n",
		 (void *)size, (void *)alignment,
		 reg ? reg->name ?: "(private)" : "(null)");

	if (!size || alignment & (alignment - 1) || !reg)
		return -EINVAL;

	mutex_lock(&cma_mutex);

	addr = reg->registered ?
		__cma_alloc_from_region(reg, PAGE_ALIGN(size),
					max(alignment, (dma_addr_t)PAGE_SIZE)) :
		-EINVAL;

	mutex_unlock(&cma_mutex);

	return addr;
}
EXPORT_SYMBOL_GPL(cma_alloc_from_region);

dma_addr_t __must_check
__cma_alloc(const struct device *dev, const char *type,
	    dma_addr_t size, dma_addr_t alignment)
{
	struct cma_region *reg;
	const char *from;
	dma_addr_t addr;

	if (dev)
		pr_debug("allocate %p/%p for %s/%s\n",
			 (void *)size, (void *)alignment,
			 dev_name(dev), type ?: "");

	if (!size || (alignment & ~alignment))
		return -EINVAL;

	if (alignment < PAGE_SIZE)
		alignment = PAGE_SIZE;

	if (!IS_ALIGNED(size, alignment))
		size = ALIGN(size, alignment);

	mutex_lock(&cma_mutex);

	from = __cma_where_from(dev, type);
	if (unlikely(IS_ERR(from))) {
		addr = PTR_ERR(from);
		goto done;
	}

	pr_debug("allocate %p/%p from one of %s\n",
		 (void *)size, (void *)alignment, from);

	while (*from && *from != ';') {
		reg = __cma_region_find(&from);
		addr = __cma_alloc_from_region(reg, size, alignment);
		if (!IS_ERR_VALUE(addr))
			goto done;
	}

	pr_debug("not enough memory\n");
	addr = -ENOMEM;

done:
	mutex_unlock(&cma_mutex);

	return addr;
}
EXPORT_SYMBOL_GPL(__cma_alloc);


void *cma_get_virt(dma_addr_t phys, dma_addr_t size, int noncached)
{
	unsigned long num_pages, i;
	struct page **pages;
	void *virt;

	if (noncached) {
		num_pages = size >> PAGE_SHIFT;
		pages = kmalloc(num_pages * sizeof(struct page *), GFP_KERNEL);

		if (!pages)
			return ERR_PTR(-ENOMEM);

		for (i = 0; i < num_pages; i++)
			pages[i] = pfn_to_page((phys >> PAGE_SHIFT) + i);

		virt = vmap(pages, num_pages, VM_MAP,
			pgprot_writecombine(PAGE_KERNEL));

		if (!virt) {
			kfree(pages);
			return ERR_PTR(-ENOMEM);
		}

		kfree(pages);
	} else {
		virt = phys_to_virt((unsigned long)phys);
	}

	return virt;
}
EXPORT_SYMBOL_GPL(cma_get_virt);

/* Query information about regions. */
static void __cma_info_add(struct cma_info *infop, struct cma_region *reg)
{
	infop->total_size += reg->size;
	infop->free_size += reg->free_space;
	if (infop->lower_bound > reg->start)
		infop->lower_bound = reg->start;
	if (infop->upper_bound < reg->start + reg->size)
		infop->upper_bound = reg->start + reg->size;
	++infop->count;
}

int
__cma_info(struct cma_info *infop, const struct device *dev, const char *type)
{
	struct cma_info info = { ~(dma_addr_t)0, 0, 0, 0, 0 };
	struct cma_region *reg;
	const char *from;
	int ret;

	if (unlikely(!infop))
		return -EINVAL;

	mutex_lock(&cma_mutex);

	from = __cma_where_from(dev, type);
	if (IS_ERR(from)) {
		ret = PTR_ERR(from);
		info.lower_bound = 0;
		goto done;
	}

	while (*from && *from != ';') {
		reg = __cma_region_find(&from);
		if (reg)
			__cma_info_add(&info, reg);
	}

	ret = 0;
done:
	mutex_unlock(&cma_mutex);

	memcpy(infop, &info, sizeof info);
	return ret;
}
EXPORT_SYMBOL_GPL(__cma_info);


/* Freeing. */
int cma_free(dma_addr_t addr)
{
	struct cma_chunk *c;
	int ret;

	mutex_lock(&cma_mutex);

	c = __cma_chunk_find(addr);

	if (c) {
		__cma_chunk_free(c);
		ret = 0;
	} else {
		ret = -ENOENT;
	}

	mutex_unlock(&cma_mutex);

	if (c)
		pr_debug("free(%p): freed\n", (void *)addr);
	else
		pr_err("free(%p): not found\n", (void *)addr);
	return ret;
}
EXPORT_SYMBOL_GPL(cma_free);


/************************* Miscellaneous *************************/

static int __cma_region_attach_alloc(struct cma_region *reg)
{
	struct cma_allocator *alloc;
	int ret;

	/*
	 * If reg->alloc is set then caller wants us to use this
	 * allocator.  Otherwise we need to find one by name.
	 */
	if (reg->alloc) {
		alloc = reg->alloc;
	} else {
		alloc = __cma_allocator_find(reg->alloc_name);
		if (!alloc) {
			pr_warn("init: %s: %s: no such allocator\n",
				reg->name ?: "(private)",
				reg->alloc_name ?: "(default)");
			reg->used = 1;
			return -ENOENT;
		}
	}

	/* Try to initialise the allocator. */
	reg->private_data = NULL;
	ret = alloc->init ? alloc->init(reg) : 0;
	if (unlikely(ret < 0)) {
		pr_err("init: %s: %s: unable to initialise allocator\n",
		       reg->name ?: "(private)", alloc->name ?: "(unnamed)");
		reg->alloc = NULL;
		reg->used = 1;
	} else {
		reg->alloc = alloc;
		pr_debug("init: %s: %s: initialised allocator\n",
			 reg->name ?: "(private)", alloc->name ?: "(unnamed)");
	}
	return ret;
}

static void __cma_region_detach_alloc(struct cma_region *reg)
{
	if (!reg->alloc)
		return;

	if (reg->alloc->cleanup)
		reg->alloc->cleanup(reg);

	reg->alloc = NULL;
	reg->used = 1;
}


/*
 * s            ::= rules
 * rules        ::= rule [ ';' rules ]
 * rule         ::= patterns '=' regions
 * patterns     ::= pattern [ ',' patterns ]
 * regions      ::= REG-NAME [ ',' regions ]
 * pattern      ::= dev-pattern [ '/' TYPE-NAME ] | '/' TYPE-NAME
 */
static const char *__must_check
__cma_where_from(const struct device *dev, const char *type)
{
	/*
	 * This function matches the pattern from the map attribute
	 * agains given device name and type.  Type may be of course
	 * NULL or an emtpy string.
	 */

	const char *s, *name;
	int name_matched = 0;

	/*
	 * If dev is NULL we were called in alternative form where
	 * type is the from string.  All we have to do is return it.
	 */
	if (!dev)
		return type ?: ERR_PTR(-EINVAL);

	if (!cma_map)
		return ERR_PTR(-ENOENT);

	name = dev_name(dev);
	if (WARN_ON(!name || !*name))
		return ERR_PTR(-EINVAL);

	if (!type)
		type = "common";

	/*
	 * Now we go throught the cma_map attribute.
	 */
	for (s = cma_map; *s; ++s) {
		const char *c;

		/*
		 * If the pattern starts with a slash, the device part of the
		 * pattern matches if it matched previously.
		 */
		if (*s == '/') {
			if (!name_matched)
				goto look_for_next;
			goto match_type;
		}

		/*
		 * We are now trying to match the device name.  This also
		 * updates the name_matched variable.  If, while reading the
		 * spec, we ecnounter comma it means that the pattern does not
		 * match and we need to start over with another pattern (the
		 * one afther the comma).  If we encounter equal sign we need
		 * to start over with another rule.  If there is a character
		 * that does not match, we neet to look for a comma (to get
		 * another pattern) or semicolon (to get another rule) and try
		 * again if there is one somewhere.
		 */

		name_matched = 0;

		for (c = name; *s != '*' && *c; ++c, ++s)
			if (*s == '=')
				goto next_rule;
			else if (*s == ',')
				goto next_pattern;
			else if (*s != '?' && *c != *s)
				goto look_for_next;
		if (*s == '*')
			++s;

		name_matched = 1;

		/*
		 * Now we need to match the type part of the pattern.  If the
		 * pattern is missing it we match only if type points to an
		 * empty string.  Otherwise wy try to match it just like name.
		 */
		if (*s == '/') {
match_type:		/* s points to '/' */
			++s;

			for (c = type; *s && *c; ++c, ++s)
				if (*s == '=')
					goto next_rule;
				else if (*s == ',')
					goto next_pattern;
				else if (*c != *s)
					goto look_for_next;
		}

		/* Return the string behind the '=' sign of the rule. */
		if (*s == '=')
			return s + 1;
		else if (*s == ',')
			return strchr(s, '=') + 1;

		/* Pattern did not match */

look_for_next:
		do {
			++s;
		} while (*s != ',' && *s != '=');
		if (*s == ',')
			continue;

next_rule:	/* s points to '=' */
		s = strchr(s, ';');
		if (!s)
			break;

next_pattern:
		continue;
	}

	return ERR_PTR(-ENOENT);
}