summaryrefslogtreecommitdiff
path: root/drivers/dsp/syslink/multicore_ipc/heapmemmp.c
blob: 2deae6c7c41085cceab5c182903e2ed8410255e0 (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
/*
 *  heapmemmp.c
 *
 *  Heap module manages variable size buffers that can be used
 *  in a multiprocessor system with shared memory.
 *
 *  Copyright (C) 2008-2009 Texas Instruments, Inc.
 *
 *  This package is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 *  WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE.
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/slab.h>

#include <atomic_linux.h>
#include <multiproc.h>
#include <nameserver.h>
#include <sharedregion.h>
#include <gatemp.h>
#include <heapmemmp.h>

/*
 *  Name of the reserved nameserver used for heapmemmp.
 */
#define HEAPMEMMP_NAMESERVER			"HeapMemMP"
#define HEAPMEMMP_MAX_NAME_LEN			32
#define HEAPMEMMP_MAX_RUNTIME_ENTRIES	32
#define HEAPMEMMP_CACHESIZE				128
/* brief Macro to make a correct module magic number with ref_count */
#define HEAPMEMMP_MAKE_MAGICSTAMP(x)	((HEAPMEMMP_MODULEID << 12) | (x))

#define ROUND_UP(a, b)	(((a) + ((b) - 1)) & (~((b) - 1)))

/*
 *  Structure defining processor related information for the
 *  heapmemmp module
 */
struct heapmemmp_proc_attrs {
	bool creator; /* Creator or opener */
	u16 proc_id; /* Processor identifier */
	u32 open_count; /* open count in a processor */
};

/*
 * heapmemmp header structure
 */
struct heapmemmp_header {
    u32 *next; /* SRPtr to next header */
    u32 size; /* Size of this segment */
};

/*
 *  Structure defining attribute parameters for the heapmemmp module
 */
struct heapmemmp_attrs {
	VOLATILE u32 status; /* Module status */
	VOLATILE u32 *buf_ptr; /* Memory managed by instance */
	VOLATILE struct heapmemmp_header head; /* header */
	VOLATILE u32 *gatemp_addr; /* gatemp shared address (shm safe) */
};

/*
 *  Structure for heapmemmp module state
 */
struct heapmemmp_module_object {
	atomic_t ref_count; /* Reference count */
	void *nameserver; /* Nameserver handle */
	struct list_head obj_list; /* List holding created objects */
	struct mutex *local_lock; /* lock for protecting obj_list */
	struct heapmemmp_config cfg; /* Current config values */
	struct heapmemmp_config default_cfg; /* Default config values */
	struct heapmemmp_params default_inst_params; /* Default instance
						creation parameters */
};

static struct heapmemmp_module_object heapmemmp_state = {
	.obj_list = LIST_HEAD_INIT(heapmemmp_state.obj_list),
	.default_cfg.max_name_len = HEAPMEMMP_MAX_NAME_LEN,
	.default_cfg.max_runtime_entries = HEAPMEMMP_MAX_RUNTIME_ENTRIES,
	.default_inst_params.gate = NULL,
	.default_inst_params.name = NULL,
	.default_inst_params.region_id = 0,
	.default_inst_params.shared_addr = NULL,
	.default_inst_params.shared_buf_size = 0,
};

/* Pointer to module state */
static struct heapmemmp_module_object *heapmemmp_module = &heapmemmp_state;

/*
 *  Structure for the handle for the heapmemmp
 */
struct heapmemmp_obj {
	struct list_head list_elem; /* Used for creating a linked list */
	struct heapmemmp_attrs *attrs; /* The shared attributes structure */
	void *gate; /* Lock used for critical region management */
	void *ns_key; /* nameserver key required for remove */
	bool cache_enabled; /* Whether to do cache calls */
	u16 region_id; /* shared region index */
	u32 alloc_size; /* Size of allocated shared memory */
	char *buf; /* Pointer to allocated memory */
	u32 min_align; /* Minimum alignment required */
	u32 buf_size; /* Buffer Size */
	struct heapmemmp_proc_attrs owner; /* owner processor info */
	void *top; /* Pointer to the top object */
	struct heapmemmp_params params; /* The creation parameter structure */
};

#define heapmemmp_object heap_object

/* =============================================================================
 * Forward declarations of internal functions
 * =============================================================================
 */
static int heapmemmp_post_init(struct heapmemmp_object *handle);

/* =============================================================================
 * APIs called directly by applications
 * =============================================================================
 */

/*
 *  This will get default configuration for the
 *  heapmemmp module
 */
int heapmemmp_get_config(struct heapmemmp_config *cfgparams)
{
	s32 retval = 0;

	BUG_ON(cfgparams == NULL);

	if (cfgparams == NULL) {
		retval = -EINVAL;
		goto error;
	}

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true)
		memcpy(cfgparams, &heapmemmp_module->default_cfg,
					sizeof(struct heapmemmp_config));
	else
		memcpy(cfgparams, &heapmemmp_module->cfg,
					sizeof(struct heapmemmp_config));
	return 0;
error:
	pr_err("heapmemmp_get_config failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(heapmemmp_get_config);

/*
 *  This will setup the heapmemmp module
 *
 *  This function sets up the heapmemmp module. This function
 *  must be called before any other instance-level APIs can be
 *  invoked.
 *  Module-level configuration needs to be provided to this
 *  function. If the user wishes to change some specific config
 *  parameters, then heapmemmp_getconfig can be called to get
 *  the configuration filled with the default values. After this,
 *  only the required configuration values can be changed. If the
 *  user does not wish to make any change in the default parameters,
 *  the application can simply call heapmemmp_setup with NULL
 *  parameters. The default parameters would get automatically used.
 */
int heapmemmp_setup(const struct heapmemmp_config *cfg)
{
	struct nameserver_params params;
	struct heapmemmp_config tmp_cfg;
	s32 retval  = 0;

	/* This sets the ref_count variable  not initialized, upper 16 bits is
	* written with module Id to ensure correctness of ref_count variable
	*/
	atomic_cmpmask_and_set(&heapmemmp_module->ref_count,
					HEAPMEMMP_MAKE_MAGICSTAMP(0),
					HEAPMEMMP_MAKE_MAGICSTAMP(0));
	if (atomic_inc_return(&heapmemmp_module->ref_count)
					!= HEAPMEMMP_MAKE_MAGICSTAMP(1)) {
		return 1;
	}

	if (cfg == NULL) {
		heapmemmp_get_config(&tmp_cfg);
		cfg = &tmp_cfg;
	}

	if (cfg->max_name_len == 0 ||
		cfg->max_name_len > HEAPMEMMP_MAX_NAME_LEN) {
		retval = -EINVAL;
		goto error;
	}

	/* Initialize the parameters */
	nameserver_params_init(&params);
	params.max_value_len = sizeof(u32);
	params.max_name_len = cfg->max_name_len;

	/* Create the nameserver for modules */
	heapmemmp_module->nameserver  =
		nameserver_create(HEAPMEMMP_NAMESERVER, &params);
	if (heapmemmp_module->nameserver == NULL) {
		retval = -EFAULT;
		goto error;
	}

	/* Construct the list object */
	INIT_LIST_HEAD(&heapmemmp_module->obj_list);
	/* Copy config info */
	memcpy(&heapmemmp_module->cfg, cfg, sizeof(struct heapmemmp_config));
	/* Create a lock for protecting list object */
	heapmemmp_module->local_lock = kmalloc(sizeof(struct mutex),
								GFP_KERNEL);
	mutex_init(heapmemmp_module->local_lock);
	if (heapmemmp_module->local_lock == NULL) {
		retval = -ENOMEM;
		heapmemmp_destroy();
		goto error;
	}

	return 0;

error:
	pr_err("heapmemmp_setup failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(heapmemmp_setup);

/*
 *  This will destroy the heapmemmp module
 */
int heapmemmp_destroy(void)
{
	s32 retval  = 0;
	struct mutex *lock = NULL;
	struct heapmemmp_obj *obj = NULL;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}

	if (atomic_dec_return(&heapmemmp_module->ref_count)
				== HEAPMEMMP_MAKE_MAGICSTAMP(0)) {
		/* Temporarily increment ref_count here. */
		atomic_set(&heapmemmp_module->ref_count,
					HEAPMEMMP_MAKE_MAGICSTAMP(1));

		/*  Check if any heapmemmp instances have not been
		 *  deleted/closed so far. if there any, delete or close them
		 */
		list_for_each_entry(obj, &heapmemmp_module->obj_list,
								list_elem) {
			if (obj->owner.proc_id == multiproc_get_id(NULL))
				retval = heapmemmp_delete(&obj->top);
			else
				retval = heapmemmp_close(obj->top);

			if (list_empty(&heapmemmp_module->obj_list))
				break;

			if (retval < 0)
				goto error;
		}

		/* Again reset ref_count. */
		atomic_set(&heapmemmp_module->ref_count,
					HEAPMEMMP_MAKE_MAGICSTAMP(0));

		if (likely(heapmemmp_module->nameserver != NULL)) {
			retval = nameserver_delete(&heapmemmp_module->
								nameserver);
			if (unlikely(retval != 0))
				goto error;
		}

		/* Delete the list lock */
		lock = heapmemmp_module->local_lock;
		retval = mutex_lock_interruptible(lock);
		if (retval)
			goto error;

		heapmemmp_module->local_lock = NULL;
		mutex_unlock(lock);
		kfree(lock);
		memset(&heapmemmp_module->cfg, 0,
				sizeof(struct heapmemmp_config));
	}

	return 0;

error:
	pr_err("heapmemmp_destroy failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(heapmemmp_destroy);

/*
 *  This will get the intialization prams for a heapmemmp
 *  module instance
 */
void heapmemmp_params_init(struct heapmemmp_params *params)
{
	s32 retval  = 0;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}

	BUG_ON(params == NULL);

	memcpy(params, &heapmemmp_module->default_inst_params,
				sizeof(struct heapmemmp_params));

	return;
error:
	pr_err("heapmemmp_params_init failed status: %x\n", retval);
}
EXPORT_SYMBOL(heapmemmp_params_init);

/*
 *  This will create a new instance of heapmemmp module
 *  This is an internal function as both heapmemmp_create
 *  and heapmemmp_open use the functionality
 *
 *  NOTE: The lock to protect the shared memory area
 *  used by heapmemmp is provided by the consumer of
 *  heapmemmp module
 */
static int _heapmemmp_create(void **handle_ptr,
				const struct heapmemmp_params *params,
				u32 create_flag)
{
	s32 retval  = 0;
	struct heapmemmp_obj *obj = NULL;
	struct heapmemmp_object *handle = NULL;
	void *gate_handle = NULL;
	void *local_addr = NULL;
	u32 *shared_shm_base;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}

	BUG_ON(handle_ptr == NULL);

	BUG_ON(params == NULL);

	/* No need for parameter checks, since this is an internal function. */

	/* Initialize return parameter. */
	*handle_ptr = NULL;

	handle = kmalloc(sizeof(struct heapmemmp_object), GFP_KERNEL);
	if (handle == NULL) {
		retval = -ENOMEM;
		goto error;
	}

	obj =  kmalloc(sizeof(struct heapmemmp_obj), GFP_KERNEL);
	if (obj == NULL) {
		retval = -ENOMEM;
		goto error;
	}

	handle->obj = (struct heapmemmp_obj *)obj;
	handle->alloc = &heapmemmp_alloc;
	handle->free  = &heapmemmp_free;
	handle->get_stats = &heapmemmp_get_stats;
	handle->is_blocking = &heapmemmp_isblocking;

	obj->ns_key = NULL;
	obj->alloc_size = 0;

	/* Put in local ilst */
	retval = mutex_lock_interruptible(heapmemmp_module->local_lock);
	if (retval < 0)
		goto error;

	INIT_LIST_HEAD(&obj->list_elem);
	list_add(&obj->list_elem, &heapmemmp_module->obj_list);
	mutex_unlock(heapmemmp_module->local_lock);

	if (create_flag == false) {
		obj->owner.creator = false;
		obj->owner.open_count = 0;
		obj->owner.proc_id = MULTIPROC_INVALIDID;
		obj->top = handle;

		obj->attrs = (struct heapmemmp_attrs *) params->shared_addr;

		/* No need to Cache_inv- already done in openByAddr() */
		obj->buf = (char *) sharedregion_get_ptr((u32 *)obj->
							attrs->buf_ptr);
		obj->buf_size	  = obj->attrs->head.size;
		obj->region_id	 = sharedregion_get_id(obj->buf);
		obj->cache_enabled = sharedregion_is_cache_enabled(obj->
								region_id);

		/* Set min_align */
		obj->min_align = sizeof(struct heapmemmp_header);
		if (sharedregion_get_cache_line_size(obj->region_id)
			> obj->min_align) {
			obj->min_align = sharedregion_get_cache_line_size(
							obj->region_id);
		}

		local_addr = sharedregion_get_ptr((u32 *)obj->attrs->
								gatemp_addr);
		retval = gatemp_open_by_addr(local_addr, &gate_handle);
		if (retval < 0) {
			retval = -EFAULT;
			goto error;
		}
		obj->gate = gate_handle;


	} else {
		obj->owner.creator = true;
		obj->owner.open_count = 1;
		obj->owner.proc_id = multiproc_self();
		obj->top = handle;

		/* Creating the gate */
		if (params->gate != NULL)
			obj->gate = params->gate;
		else {
			/* If no gate specified, get the default system gate */
			obj->gate = gatemp_get_default_remote();
		}

		if (obj->gate == NULL) {
			retval = -EFAULT;
			goto error;
		}

		obj->buf_size = params->shared_buf_size;

		if (params->shared_addr == NULL) {
			/* Creating using a shared region ID */
			/* It is allowed to have NULL name for an anonymous,
			 * not to be opened by name, heap.
			 */
			/* Will be allocated in post_init */
			obj->attrs = NULL;
			obj->region_id = params->region_id;
		} else {
			/* Creating using shared_addr */
			obj->region_id = sharedregion_get_id(params->
								shared_addr);

			/* Assert that the buffer is in a valid shared
			 * region
			 */
			if (obj->region_id == SHAREDREGION_INVALIDREGIONID) {
				retval = -EFAULT;
				goto error;
			} else if ((u32) params->shared_addr
				 % sharedregion_get_cache_line_size(obj->
				region_id) != 0) {
				retval = -EFAULT;
				goto error;
			}
			/* obj->buf will get alignment-adjusted in
			 * postInit
			 */
			obj->buf = (char *)((u32)params->shared_addr + \
						sizeof(struct heapmemmp_attrs));
			obj->attrs = (struct heapmemmp_attrs *)
							params->shared_addr;
		}

		obj->cache_enabled = sharedregion_is_cache_enabled(
							   obj->region_id);

		/* Set min_align */
		obj->min_align = sizeof(struct heapmemmp_header);
		if (sharedregion_get_cache_line_size(obj->region_id)
			> obj->min_align)
			obj->min_align = sharedregion_get_cache_line_size(
							obj->region_id);
		retval = heapmemmp_post_init(handle);
		if (retval < 0) {
			retval = -EFAULT;
			goto error;
		}

		/* Populate the params member */
		memcpy(&obj->params, params, sizeof(struct heapmemmp_params));
		if (params->name != NULL) {
			obj->params.name = kmalloc(strlen(params->name) + 1,
								GFP_KERNEL);
			if (obj->params.name == NULL) {
				retval  = -ENOMEM;
				goto error;
			}
			strncpy(obj->params.name, params->name,
						strlen(params->name) + 1);
		}

		/* We will store a shared pointer in the NameServer */
		shared_shm_base = sharedregion_get_srptr(obj->attrs,
							   obj->region_id);
		if (obj->params.name != NULL) {
			obj->ns_key =
			  nameserver_add_uint32(heapmemmp_module->nameserver,
						params->name,
						(u32) shared_shm_base);
			if (obj->ns_key == NULL) {
				retval = -EFAULT;
				goto error;
			}
		}
	}

	*handle_ptr = (void *)handle;
	return retval;

error:
	handle_ptr = (void *)handle;
	/* Do whatever cleanup is required*/
	if (create_flag == true)
		heapmemmp_delete(handle_ptr);
	else
		heapmemmp_close(handle_ptr);
	pr_err("_heapmemmp_create failed status: %x\n", retval);
	return retval;
}

/*
 *  This will create a new instance of heapmemmp module
 */
void *heapmemmp_create(const struct heapmemmp_params *params)
{
	s32 retval = 0;
	struct heapmemmp_object *handle = NULL;
	struct heapmemmp_params sparams;

	BUG_ON(params == NULL);

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}

	if (params == NULL) {
		retval = -EINVAL;
		goto error;
	}

	if (params->shared_buf_size == 0) {
		retval = -EINVAL;
		goto error;
	}

	memcpy(&sparams, (void *)params, sizeof(struct heapmemmp_params));
	retval = _heapmemmp_create((void **)&handle, params, true);
	if (retval < 0)
		goto error;

	return (void *)handle;

error:
	pr_err("heapmemmp_create failed status: %x\n", retval);
	return (void *)handle;
}
EXPORT_SYMBOL(heapmemmp_create);

/*
 *  This will delete an instance of heapmemmp module
 */
int heapmemmp_delete(void **handle_ptr)
{
	int status = 0;
	struct heapmemmp_object *handle = NULL;
	struct heapmemmp_obj *obj = NULL;
	struct heapmemmp_params *params = NULL;
	struct heapmemmp_object *region_heap = NULL;
	s32 retval = 0;
	int *key = NULL;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}

	if (WARN_ON(handle_ptr == NULL)) {
		retval = -EINVAL;
		goto error;
	}

	handle = (struct heapmemmp_object *)(*handle_ptr);
	if (WARN_ON(handle == NULL)) {
		retval = -EINVAL;
		goto error;
	}

	obj = (struct heapmemmp_obj *)handle->obj;
	if (obj != NULL) {
		if (obj->owner.proc_id != multiproc_self()) {
			status = -ENODEV;
			goto error;
		}

		/* Take the local lock */
		key = gatemp_enter(obj->gate);

		if (obj->owner.open_count > 1) {
			retval = -ENODEV;
			goto device_busy_error;
		}

		retval = mutex_lock_interruptible(heapmemmp_module->local_lock);
		if (retval < 0)
			goto lock_error;

		/* Remove frmo the local list */
		list_del(&obj->list_elem);

		mutex_unlock(heapmemmp_module->local_lock);

		params = (struct heapmemmp_params *) &obj->params;

		if (likely(params->name != NULL)) {
			if (likely(obj->ns_key != NULL)) {
				nameserver_remove_entry(heapmemmp_module->
					nameserver, obj->ns_key);
				obj->ns_key = NULL;
			}
			kfree(params->name);
		}

		/* Set status to 'not created' */
	    if (obj->attrs != NULL) {
#if 0
			obj->attrs->status = 0;
			if (obj->cache_enabled) {
				cache_wbinv(obj->attrs,
					sizeof(struct heapmemmp_attrs),
					CACHE_TYPE_ALL, true);
			}
#endif
		}

		/* Release the shared lock */
		gatemp_leave(obj->gate, key);

		/* If necessary, free shared memory  if memory is internally
		 * allocated
		 */
		region_heap = sharedregion_get_heap(obj->region_id);

		if ((region_heap != NULL) &&
			(obj->params.shared_addr == NULL) &&
			(obj->attrs != NULL)) {
			sl_heap_free(region_heap, obj->attrs, obj->alloc_size);
		}

		kfree(obj);
		kfree(region_heap);

		*handle_ptr = NULL;
	} else {	/* obj == NULL */
		kfree(handle);
		*handle_ptr = NULL;
	}

	return 0;

lock_error:
device_busy_error:
	gatemp_leave(obj->gate, key);

error:
	pr_err("heapmemmp_delete failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(heapmemmp_delete);

/*
 *  This will opens a created instance of heapmemmp
 *  module
 */
int heapmemmp_open(char *name, void **handle_ptr)
{
	s32 retval = 0;
	u32 *shared_shm_base = SHAREDREGION_INVALIDSRPTR;
	void *shared_addr = NULL;
	struct heapmemmp_obj *obj = NULL;
	bool done_flag = false;
	struct list_head *elem = NULL;

	BUG_ON(name  == NULL);
	BUG_ON(handle_ptr == NULL);

	if (unlikely(
		atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true)) {
		retval = -ENODEV;
		goto error;
	}

	if (name == NULL) {
		retval = -EINVAL;
		goto error;
	}

	if (handle_ptr == NULL) {
		retval = -EINVAL;
		goto error;
	}

	/* First check in the local list */
	list_for_each(elem, &heapmemmp_module->obj_list) {
		obj = (struct heapmemmp_obj *)elem;
		if (obj->params.name != NULL) {
			if (strcmp(obj->params.name, name)
				== 0) {
				retval = mutex_lock_interruptible(
					heapmemmp_module->local_lock);
				if (retval < 0)
					goto error;
				/* Check if we have created the heapmemmp */
				/* or not */
				if (obj->owner.proc_id == multiproc_self())
					obj->owner.open_count++;

				*handle_ptr = (void *)obj->top;
				mutex_unlock(heapmemmp_module->local_lock);
				done_flag = true;
				break;
			}
		}
	}

	if (likely(done_flag == false)) {
		/* Find in name server */
		retval = nameserver_get_uint32(heapmemmp_module->nameserver,
						name,
						&shared_shm_base,
						NULL);
		if (unlikely(retval < 0))
			goto error;

		/*
		 * Convert from shared region pointer to local address
		 */
		shared_addr = sharedregion_get_ptr(shared_shm_base);
		if (unlikely(shared_addr == NULL)) {
			retval = -EINVAL;
			goto error;
		}

		retval = heapmemmp_open_by_addr(shared_addr, handle_ptr);

		if (unlikely(retval < 0))
			goto error;
	}

	return 0;

error:
	pr_err("heapmemmp_open failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(heapmemmp_open);

/*
 *  This will closes previously opened/created instance
 *  of heapmemmp module
 */
int heapmemmp_close(void **handle_ptr)
{
	struct heapmemmp_object *handle = NULL;
	struct heapmemmp_obj *obj = NULL;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}

	if (WARN_ON(handle_ptr == NULL)) {
		retval  = -EINVAL;
		goto error;
	}

	if (WARN_ON(*handle_ptr == NULL)) {
		retval  = -EINVAL;
		goto error;
	}

	handle = (struct heapmemmp_object *)(*handle_ptr);
	obj = (struct heapmemmp_obj *)handle->obj;

	if (obj != NULL) {
		retval = mutex_lock_interruptible(heapmemmp_module->
								local_lock);
		if (retval)
			goto error;

		/* opening an instance created locally */
		if (obj->owner.proc_id == multiproc_self())
				obj->owner.open_count--;

		/* Check if HeapMemMP is opened on same processor and
		 * this is the last closure.
		 */
		if ((obj->owner.creator == false) &&
				(obj->owner.open_count == 0)) {
			list_del(&obj->list_elem);

			if (obj->gate != NULL) {
				/* Close the instance gate */
				gatemp_close(&obj->gate);
			}

			/* Now free the handle */
			kfree(obj);
			obj = NULL;
			kfree(handle);
			*handle_ptr = NULL;
		}

		mutex_unlock(heapmemmp_module->local_lock);
	} else {
		kfree(handle);
		*handle_ptr = NULL;
	}
	return 0;

error:
	pr_err("heapmemmp_close failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(heapmemmp_close);

/*
 *  This will allocs a block of memory
 */
void *heapmemmp_alloc(void *hphandle, u32 size, u32 align)
{
	char *alloc_addr = NULL;
	struct heapmemmp_object *handle = NULL;
	struct heapmemmp_obj *obj = NULL;
	int *key = NULL;
	struct heapmemmp_header *prev_header;
	struct heapmemmp_header *new_header;
	struct heapmemmp_header *cur_header;
	u32 cur_size;
	u32 adj_size;
	u32 remain_size;
	u32 adj_align;
	u32 offset;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}
	if (WARN_ON(hphandle == NULL)) {
		retval  = -EINVAL;
		goto error;
	}
	if (WARN_ON(size == 0)) {
		retval  = -EINVAL;
		goto error;
	}

	handle = (struct heapmemmp_object *)(hphandle);
	obj = (struct heapmemmp_obj *)handle->obj;
	if (WARN_ON(obj == NULL)) {
		retval = -EINVAL;
		goto error;
	}

	adj_size = size;

	/* Make size requested a multipel of min_align */
	offset = (adj_size & (obj->min_align - 1));
	if (offset != 0)
		adj_size += (obj->min_align - offset);

	/*
	 *  Make sure the alignment is at least as large as obj->min_align
	 *  Note: adjAlign must be a power of 2 (by function constraint) and
	 *  obj->min_align is also a power of 2,
	 */
	adj_align = align;
	if (adj_align == 0)
		adj_align =  obj->min_align;

	if (adj_align & (obj->min_align - 1))
		/* adj_align is less than obj->min_align */
		adj_align = obj->min_align;

	/* No need to Cache_inv Attrs- 'head' should be constant */
	prev_header = (struct heapmemmp_header *) &obj->attrs->head;

	key = gatemp_enter(obj->gate);
	/*
	 *  The block will be allocated from cur_header. Maintain a pointer to
	 *  prev_header so prev_header->next can be updated after the alloc.
	 */
#if 0
	if (unlikely(obj->cache_enabled))
		Cache_inv(prev_header,
			   sizeof(struct heapmemmp_header),
			   Cache_Type_ALL,
			   true); /* A1 */
#endif
	cur_header = (struct heapmemmp_header *)
				sharedregion_get_ptr(prev_header->next);
	/* A1 */

	/* Loop over the free list. */
	while (cur_header != NULL) {
#if 0
		/* Invalidate cur_header */
		if (unlikely(obj->cache_enabled))
			Cache_inv(cur_header,
				  sizeof(struct heapmemmp_header),
				  Cache_Type_ALL,
				  true); /* A2 */
#endif

		cur_size = cur_header->size;

		/*
		 *  Determine the offset from the beginning to make sure
		 *  the alignment request is honored.
		 */
		offset = (u32)cur_header & (adj_align - 1);
		if (offset)
			offset = adj_align - offset;

		/* Internal Assert that offset is a multiple of */
		/* obj->min_align */
		if (((offset & (obj->min_align - 1)) != 0)) {
			retval = -EINVAL;
			goto error;
		}
		/* big enough? */

		/* This is the "else" part of the next if block, but we are */
		/* moving it here to save indent space */
		if (cur_size < (adj_size + offset)) {
			prev_header = cur_header;
			cur_header = sharedregion_get_ptr(cur_header->next);
			/* We can quit this iteration of the while loop here */
			continue;
		}

		/* if (cur_size >= (adj_size + offset)) */

		/* Set the pointer that will be returned. */
		/* Alloc from front */
		alloc_addr = (char *) ((u32) cur_header + offset);
		/*
		 *  Determine the remaining memory after the
		 *  allocated block.
		 *  Note: this cannot be negative because of above
		 *  comparison.
		 */
		remain_size = cur_size - adj_size - offset;

		/* Internal Assert that remain_size is a multiple of
		 * obj->min_align
		 */
		if (((remain_size & (obj->min_align - 1)) != 0)) {
			alloc_addr = NULL;
			break;
		}
		/*
		 *  If there is memory at the beginning (due to alignment
		 *  requirements), maintain it in the list.
		 *
		 *  offset and remain_size must be multiples of sizeof(struct
		 *  heapmemmp_header). Therefore the address of the new_header
		 *  below must be a multiple of the sizeof(struct
		 *  heapmemmp_header), thus maintaining the requirement.
		 */
		if (offset) {
			/* Adjust the cur_header size accordingly */
			cur_header->size = offset; /* B2 */
			/* Cache wb at end of this if block */

			/*
			 *  If there is remaining memory, add into the free
			 *  list.
			 *  Note: no need to coalesce and we have heapmemmp
			 *  locked so it is safe.
			 */
			if (offset && remain_size) {
				new_header = (struct heapmemmp_header *)
						((u32) alloc_addr + adj_size);

				/* cur_header has been inv at top of 'while' */
				/* loop */
				new_header->next = cur_header->next;  /* B1 */
				new_header->size = remain_size;       /* B1 */
#if 0
				if (unlikely(obj->cache_enabled))
					/* Writing back cur_header will */
					/* cache-wait */
					Cache_wbInv(new_header,
						sizeof(struct
							heapmemmp_header),
						Cache_Type_ALL,
						false); /* B1 */
#endif

				cur_header->next = sharedregion_get_srptr
							(new_header,
							 obj->region_id);
				BUG_ON(cur_header->next
					== SHAREDREGION_INVALIDSRPTR);
			}
#if 0
			/* Write back (and invalidate) new_header and */
			/* cur_header */
			if (unlikely(obj->cache_enabled))
				/* B2 */
				Cache_wbInv(cur_header,
					 sizeof(struct heapmemmp_header),
					 Cache_Type_ALL,
					 true);
#endif
		} else if (remain_size) {
			/*
			 *  If there is any remaining, link it in,
			 *  else point to the next free block.
			 *  Note: no need to coalesce and we have heapmemmp
			 *  locked so it is safe.
			 */

			new_header = (struct heapmemmp_header *)
				((u32) alloc_addr + adj_size);

			new_header->next = cur_header->next; /* A2, B3  */
			new_header->size = remain_size;      /* B3      */

#if 0
			if (unlikely(obj->cache_enabled))
				/* Writing back prev_header will cache-wait */
				Cache_wbInv(new_header,
				 sizeof(struct heapmemmp_header),
				 Cache_Type_ALL,
				 false); /* B3 */
#endif

			/* B4 */
			prev_header->next = sharedregion_get_srptr(new_header,
							obj->region_id);
		} else
			/* cur_header has been inv at top of 'while' loop */
			prev_header->next = cur_header->next; /* A2, B4 */

#if 0
		if (unlikely(obj->cache_enabled))
			/* B4 */
			Cache_wbInv(prev_header,
					 sizeof(struct heapmemmp_header),
					 Cache_Type_ALL,
					 true);
#endif

		/* Success, return the allocated memory */
		break;

	}

	gatemp_leave(obj->gate, key);

	if (alloc_addr == NULL)
		pr_err("heapmemmp_alloc returned NULL\n");
	return alloc_addr;

error:
	pr_err("heapmemmp_alloc failed status: %x\n", retval);
	return NULL;
}
EXPORT_SYMBOL(heapmemmp_alloc);

/*
 *  This will free a block of memory
 */
int heapmemmp_free(void *hphandle, void *addr, u32 size)
{
	struct heapmemmp_object *handle = NULL;
	s32 retval = 0;
	struct heapmemmp_obj *obj = NULL;
	int *key = NULL;
	struct heapmemmp_header *next_header;
	struct heapmemmp_header *new_header;
	struct heapmemmp_header *cur_header;
	u32 offset;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto error;
	}
	if (WARN_ON(hphandle == NULL)) {
		retval  = -EINVAL;
		goto error;
	}
	if (WARN_ON(addr == NULL)) {
		retval  = -EINVAL;
		goto error;
	}

	handle = (struct heapmemmp_object *)(hphandle);
	obj = (struct heapmemmp_obj *)handle->obj;
	if (WARN_ON(obj == NULL)) {
		retval = -EINVAL;
		goto error;
	}

	/*
	 * obj->attrs never changes, doesn't need Gate protection
	 * and Cache invalidate
	 */
	cur_header = (struct heapmemmp_header *) &(obj->attrs->head);

	/* Restore size to actual allocated size */
	offset = size & (obj->min_align - 1);
	if (offset != 0)
		size += obj->min_align - offset;

	key = gatemp_enter(obj->gate);

	new_header = (struct heapmemmp_header *) addr;

#if 0
	if (unlikely(obj->cacheEnabled)) {
		/* A1 */
		Cache_inv(cur_header,
				   sizeof(struct heapmemmp_header),
				   Cache_Type_ALL,
				   true);
	}
#endif
	next_header = sharedregion_get_ptr(cur_header->next);

	if (unlikely(!(((u32)new_header >= (u32)obj->buf)
				&& ((u32)new_header + size
					<= (u32)obj->buf + obj->buf_size)))) {
		retval = -EFAULT;
		goto error;
	}
	/* Go down freelist and find right place for buf */
	while ((next_header != NULL) && (next_header < new_header)) {
#if 0
		if (unlikely(obj->cacheEnabled))
			Cache_inv(next_header,
				   sizeof(struct heapmemmp_header),
				   Cache_Type_ALL,
				   true); /* A2 */
#endif

		/* Make sure the addr is not in this free block */
		if (unlikely((u32)new_header < \
				((u32)next_header + next_header->size))) {
			/* A2 */
			retval = -EFAULT;
			goto error;
		}

		cur_header = next_header;
		/* A2 */
		next_header = sharedregion_get_ptr(next_header->next);
	}

	new_header->next = sharedregion_get_srptr(next_header,
							obj->region_id);
	new_header->size = size;

	/* B1, A1 */
	cur_header->next = sharedregion_get_srptr(new_header,
							obj->region_id);

	/* Join contiguous free blocks */
	if (next_header != NULL) {
		/*
		 *  Verify the free size is not overlapping. Not all cases
		 *  are detectable, but it is worth a shot. Note: only do
		 *  this assert if next_header is non-NULL.
		 */
		if (unlikely(((u32)new_header + size) > (u32)next_header)) {
			/* A2 */
			retval = -EFAULT;
			goto error;
		}
		/* Join with upper block */
		if (((u32)new_header + size) == (u32)next_header) {
#if 0
			if (unlikely(obj->cacheEnabled))
				Cache_inv(next_header,
					   sizeof(struct heapmemmp_header),
					   Cache_Type_ALL,
					   true);
#endif
			new_header->next = next_header->next; /* A2, B2 */
			new_header->size += next_header->size; /* A2, B2 */
			/* Don't Cache_wbInv, this will be done later */
		}
	}

	/*
	 * Join with lower block. Make sure to check to see if not the
	 * first block. No need to invalidate attrs since head
	 * shouldn't change.
	 */
	if ((cur_header != &obj->attrs->head)
		&& (((u32) cur_header + cur_header->size)
			== (u32) new_header)) {
		/*
		 * Don't Cache_inv new_header since new_header has
		 * data that hasn't been written back yet (B2)
		 */
		cur_header->next = new_header->next; /* B1, B2 */
		cur_header->size += new_header->size; /* B1, B2 */
	}
#if 0
		if (unlikely(obj->cacheEnabled)) {
			Cache_wbInv(cur_header,
					 sizeof(struct heapmemmp_header),
					 Cache_Type_ALL,
					 false); /* B1 */
			Cache_wbInv(new_header,
					 sizeof(struct heapmemmp_header),
					 Cache_Type_ALL,
					 true);  /* B2 */
		}
#endif

	gatemp_leave(obj->gate, key);
	return 0;

error:
	pr_err("heapmemmp_free failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(heapmemmp_free);

/*
 *  This will get memory statistics
 */
void heapmemmp_get_stats(void *hphandle, struct memory_stats *stats)
{
	struct heapmemmp_object *object = NULL;
	struct heapmemmp_obj *obj = NULL;
	struct heapmemmp_header *cur_header = NULL;
	int *key = NULL;
	s32 status = 0;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto error;
	}
	if (WARN_ON(hphandle == NULL)) {
		status  = -EINVAL;
		goto error;
	}
	if (WARN_ON(stats == NULL)) {
		status  = -EINVAL;
		goto error;
	}

	object = (struct heapmemmp_object *)(hphandle);
	obj = (struct heapmemmp_obj *)object->obj;
	if (WARN_ON(obj == NULL)) {
		status = -EINVAL;
		goto error;
	}

	stats->total_size = obj->buf_size;
	stats->total_free_size = 0; /* determined later */
	stats->largest_free_size = 0; /* determined later */

	key = gatemp_enter(obj->gate);
	cur_header = sharedregion_get_ptr(obj->attrs->head.next);

	while (cur_header != NULL) {
#if 0
		/* Invalidate cur_header */
		if (unlikely(obj->cacheEnabled)) {
			Cache_inv(cur_header,
				   sizeof(struct heapmemmp_header),
				   Cache_Type_ALL,
				   true);
		}
#endif
		stats->total_free_size += cur_header->size;
		if (stats->largest_free_size < cur_header->size)
			stats->largest_free_size = cur_header->size;

		/* This condition is required to avoid assertions during call
		 * to SharedRegion_getPtr  because at the end of the
		 * calculation cur_header->next  will become
		 * SHAREDREGION_INVALIDSRPTR.
		 */
		if (cur_header->next != SHAREDREGION_INVALIDSRPTR)
			cur_header = sharedregion_get_ptr(cur_header->next);
		else
			cur_header = NULL;
	}

	gatemp_leave(obj->gate, key);
error:
	if (status < 0)
		pr_err("heapmemmp_get_stats status: %x\n", status);
}
EXPORT_SYMBOL(heapmemmp_get_stats);

/*
 *  Indicate whether the heap may block during an alloc or free call
 */
bool heapmemmp_isblocking(void *handle)
{
	bool isblocking = false;
	s32 retval  = 0;

	if (WARN_ON(handle == NULL)) {
		retval  = -EINVAL;
		goto error;
	}

	/* TBD: Figure out how to determine whether the gate is blocking */
	isblocking = true;

	/* retval true  Heap blocks during alloc/free calls */
	/* retval false Heap does not block during alloc/free calls */
	return isblocking;

error:
	pr_err("heapmemmp_isblocking status: %x\n", retval);
	return isblocking;
}
EXPORT_SYMBOL(heapmemmp_isblocking);

/*
 *  This will get extended statistics
 */
void heapmemmp_get_extended_stats(void *hphandle,
				 struct heapmemmp_extended_stats *stats)
{
	int status = 0;
	struct heapmemmp_object *object = NULL;
	struct heapmemmp_obj *obj = NULL;

	if (atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
				HEAPMEMMP_MAKE_MAGICSTAMP(0),
				HEAPMEMMP_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto error;
	}
	if (WARN_ON(heapmemmp_module->nameserver == NULL)) {
		status  = -EINVAL;
		goto error;
	}
	if (WARN_ON(hphandle == NULL)) {
		status  = -EINVAL;
		goto error;
	}
	if (WARN_ON(stats == NULL)) {
		status = -EINVAL;
		goto error;
	}

	object = (struct heapmemmp_object *)hphandle;
	obj = (struct heapmemmp_obj *)object->obj;
	if (WARN_ON(obj == NULL)) {
		status = -EINVAL;
		goto error;
	}

	stats->buf   = obj->buf;
	stats->size  = obj->buf_size;

	return;

error:
	pr_err("heapmemmp_get_extended_stats status: %x\n", status);
}
EXPORT_SYMBOL(heapmemmp_get_extended_stats);

/*
 *  This will get amount of shared memory required for
 *  creation of each instance
 */
int heapmemmp_shared_mem_req(const struct heapmemmp_params *params)
{
	int mem_req = 0;
	s32 retval = 0;
	u32 region_id;
	u32 min_align;

	if (params == NULL) {
		retval = -EINVAL;
		goto error;
	}

	if (params->shared_addr == NULL)
		region_id = params->region_id;
	else
		region_id = sharedregion_get_id(params->shared_addr);

	if (region_id == SHAREDREGION_INVALIDREGIONID)  {
		retval = -EFAULT;
		goto error;
	}

	min_align = sizeof(struct heapmemmp_header);
	if (sharedregion_get_cache_line_size(region_id) > min_align)
		min_align = sharedregion_get_cache_line_size(region_id);

	/* Add size of heapmemmp Attrs */
	mem_req = ROUND_UP(sizeof(struct heapmemmp_attrs), min_align);

	/* Add the buffer size */
	mem_req += params->shared_buf_size;

	/* Make sure the size is a multiple of min_align (round down) */
	mem_req = (mem_req / min_align) * min_align;

	return mem_req;

error:
	pr_err("heapmemmp_shared_mem_req retval: %x\n", retval);
	return mem_req;
}
EXPORT_SYMBOL(heapmemmp_shared_mem_req);


/*
 *  Open existing heapmemmp based on address
 */
int
heapmemmp_open_by_addr(void *shared_addr, void **handle_ptr)
{
	s32 retval = 0;
	bool done_flag = false;
	struct heapmemmp_attrs *attrs = NULL;
	struct list_head *elem = NULL;
	u16 id = 0;
	struct heapmemmp_params params;
	struct heapmemmp_obj *obj = NULL;

	BUG_ON(handle_ptr == NULL);
	BUG_ON(shared_addr == NULL);

	if (unlikely(atomic_cmpmask_and_lt(&(heapmemmp_module->ref_count),
					HEAPMEMMP_MAKE_MAGICSTAMP(0),
					HEAPMEMMP_MAKE_MAGICSTAMP(1))
					 == true)) {
		retval = -ENODEV;
		goto error;
	}

	if (unlikely(handle_ptr == NULL)) {
		retval = -EINVAL;
		goto error;
	}

	/* First check in the local list */
	list_for_each(elem, (struct list_head *)&heapmemmp_module->obj_list) {
		obj = (struct heapmemmp_obj *)elem;
		if (obj->params.shared_addr == shared_addr) {
			retval = mutex_lock_interruptible(heapmemmp_module->
								local_lock);
			if (retval < 0)
				goto error;

			if (obj->owner.proc_id == multiproc_self())
				obj->owner.open_count++;

			mutex_unlock(heapmemmp_module->local_lock);
			*handle_ptr = obj->top;
			done_flag = true;
			break;
		}
	}

	/* If not already existing locally, create object locally for open. */
	if (unlikely(done_flag == false)) {
		heapmemmp_params_init(&params);
		params.shared_addr = shared_addr;
		attrs = (struct heapmemmp_attrs *) shared_addr;
		id = sharedregion_get_id(shared_addr);
#if 0
		if (unlikely(sharedregion_is_cache_enabled(id))) {
			Cache_inv(attrs,
				  sizeof(struct heapmemmp_attrs),
				  Cache_Type_ALL,
				  true);
		}
#endif
		if (unlikely(attrs->status != HEAPMEMMP_CREATED)) {
			*handle_ptr = NULL;
			retval = -ENOENT;
			goto error;
		}

		retval = _heapmemmp_create(handle_ptr, &params, false);
		if (unlikely(retval < 0))
			goto error;
	}
	return 0;

error:
	pr_err("heapmemmp_open_by_addr status: %x\n", retval);
	return retval;
}


/* =============================================================================
 * Internal functions
 * =============================================================================
 */
/*
 *  Slice and dice the buffer up into the correct size blocks and
 *  add to the freelist.
 */
static int heapmemmp_post_init(struct heapmemmp_object *handle)
{
	s32 retval = 0;
	struct heapmemmp_obj *obj = NULL;
	struct heapmemmp_object *region_heap = NULL;
	struct heapmemmp_params params;

	BUG_ON(handle == NULL);

	obj = (struct heapmemmp_obj *) handle->obj;
	if (obj->attrs == NULL) {
		heapmemmp_params_init(&params);
		params.region_id = obj->region_id;
		params.shared_buf_size = obj->buf_size;
		obj->alloc_size = heapmemmp_shared_mem_req(&params);
		region_heap = sharedregion_get_heap(obj->region_id);

		if (region_heap == NULL) {
			retval = -EFAULT;
			goto error;
		}

		obj->attrs = sl_heap_alloc(region_heap,
					obj->alloc_size,
					obj->min_align);

		if (obj->attrs == NULL) {
			retval = -ENOMEM;
			goto error;
		}
		obj->buf = (void *)((u32)obj->attrs +
				sizeof(struct heapmemmp_attrs));
	}

	/* Round obj->buf up by obj->min_align */
	obj->buf = (void *) ROUND_UP((u32)obj->buf, (obj->min_align));

	if (unlikely(obj->buf_size
			< sharedregion_get_cache_line_size(obj->region_id))) {
		retval = -EFAULT;
		goto error;
	}

	/* Make sure the size is a multiple of obj->min_align */
	obj->buf_size = (obj->buf_size / obj->min_align) * obj->min_align;

	obj->attrs->gatemp_addr = gatemp_get_shared_addr(obj->gate);
	obj->attrs->buf_ptr = sharedregion_get_srptr(obj->buf, obj->region_id);

	/* Store computed obj->buf_size in shared mem */
	obj->attrs->head.size = obj->buf_size;

	/* Place the initial header */
	heapmemmp_restore((struct heapmemmp_object *) handle);

	/* Last thing, set the status */
	obj->attrs->status = HEAPMEMMP_CREATED;
#if 0
	if (unlikely(obj->cacheEnabled))
		Cache_wbInv((Ptr) obj->attrs,
					 sizeof(heapmemmp_Attrs),
					 Cache_Type_ALL,
					 true);
#endif

	return 0;
error:
	pr_err("heapmemmp_post_init status: %x\n", retval);
	return retval;
}


/*
 *  Restore an instance to it's original created state.
 */
void
heapmemmp_restore(void *handle)
{
	struct heapmemmp_header *beg_header = NULL;
	struct heapmemmp_obj    *obj        = NULL;

	obj = ((struct heapmemmp_object *) handle)->obj;
	BUG_ON(obj == NULL);

	/*
	 *  Fill in the top of the memory block
	 *  next: pointer will be NULL (end of the list)
	 *  size: size of this block
	 *  NOTE: no need to Cache_inv because obj->attrs->bufPtr
	 *  should be const
	 */
	beg_header = (struct heapmemmp_header *) obj->buf;
	beg_header->next = (u32 *)SHAREDREGION_INVALIDSRPTR;
	beg_header->size = obj->buf_size;

	obj->attrs->head.next = (u32 *)obj->attrs->buf_ptr;
#if 0
	if (unlikely(obj->cacheEnabled)) {
		Cache_wbInv((Ptr)&(obj->attrs->head),
				   sizeof(struct heapmemmp_header),
				   Cache_Type_ALL,
				   false);
		Cache_wbInv(begHeader,
			    sizeof(struct heapmemmp_header),
			    Cache_Type_ALL,
			    true);
	}
#endif
}