forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.c
More file actions
1149 lines (942 loc) · 28 KB
/
alloc.c
File metadata and controls
1149 lines (942 loc) · 28 KB
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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
// Keyon Jie <yang.jie@linux.intel.com>
#include <rtos/panic.h>
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <sof/lib/cpu.h>
#include <sof/lib/dma.h>
#include <sof/lib/memory.h>
#include <sof/lib/mm_heap.h>
#include <sof/lib/uuid.h>
#include <sof/math/numbers.h>
#include <rtos/spinlock.h>
#include <rtos/string.h>
#include <ipc/topology.h>
#include <ipc/trace.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
LOG_MODULE_REGISTER(memory, CONFIG_SOF_LOG_LEVEL);
/* 425d6e68-145c-4455-b0b2-c7260b0600a5 */
DECLARE_SOF_UUID("memory", mem_uuid, 0x425d6e68, 0x145c, 0x4455,
0xb0, 0xb2, 0xc7, 0x26, 0x0b, 0x06, 0x00, 0xa5);
DECLARE_TR_CTX(mem_tr, SOF_UUID(mem_uuid), LOG_LEVEL_INFO);
/* debug to set memory value on every allocation */
#if CONFIG_DEBUG_BLOCK_FREE
#define DEBUG_BLOCK_FREE_VALUE_8BIT ((uint8_t)0xa5)
#define DEBUG_BLOCK_FREE_VALUE_32BIT ((uint32_t)0xa5a5a5a5)
#endif
/* We have 3 memory pools
*
* 1) System memory pool does not have a map and it's size is fixed at build
* time. Memory cannot be freed from this pool. Used by device drivers
* and any system core. Saved as part of PM context.
* 2) Runtime memory pool has variable size allocation map and memory is freed
* on calls to rfree(). Saved as part of PM context. Global size
* set at build time.
* 3) Buffer memory pool has fixed size allocation map and can be freed on
* module removal or calls to rfree(). Saved as part of PM context.
*/
#if CONFIG_DEBUG_BLOCK_FREE
/* Check whole memory region for debug pattern to find if memory was freed
* second time
*/
static void validate_memory(void *ptr, size_t size)
{
uint32_t *ptr_32 = ptr;
int i, not_matching = 0;
for (i = 0; i < size / 4; i++) {
if (ptr_32[i] != DEBUG_BLOCK_FREE_VALUE_32BIT)
not_matching = 1;
}
if (not_matching) {
tr_info(&mem_tr, "validate_memory() pointer: %p freed pattern not detected",
ptr);
} else {
tr_err(&mem_tr, "validate_memory() freeing pointer: %p double free detected",
ptr);
}
}
#endif
/* total size of block */
static inline uint32_t block_get_size(struct block_map *map)
{
uint32_t size = sizeof(*map) + map->count *
(map->block_size + sizeof(struct block_hdr));
return size;
}
/* total size of heap */
static inline uint32_t heap_get_size(struct mm_heap *heap)
{
uint32_t size = sizeof(struct mm_heap);
int i;
for (i = 0; i < heap->blocks; i++)
size += block_get_size(&heap->map[i]);
return size;
}
#if CONFIG_DEBUG_BLOCK_FREE
static void write_pattern(struct mm_heap *heap_map, int heap_depth,
uint8_t pattern)
{
struct mm_heap *heap;
struct block_map *current_map;
int i, j;
for (i = 0; i < heap_depth; i++) {
heap = &heap_map[i];
for (j = 0; j < heap->blocks; j++) {
current_map = &heap->map[j];
memset(
(void *)current_map->base, pattern,
current_map->count * current_map->block_size);
}
}
}
#endif
static void init_heap_map(struct mm_heap *heap, int count)
{
struct block_map *next_map;
struct block_map *current_map;
int i;
int j;
for (i = 0; i < count; i++) {
/* init the map[0] */
current_map = &heap[i].map[0];
current_map->base = heap[i].heap;
/* map[j]'s base is calculated based on map[j-1] */
for (j = 1; j < heap[i].blocks; j++) {
next_map = &heap[i].map[j];
next_map->base = current_map->base +
current_map->block_size *
current_map->count;
current_map = &heap[i].map[j];
}
}
}
/* allocate from system memory pool */
static void *rmalloc_sys(struct mm_heap *heap, uint32_t flags, int caps, size_t bytes)
{
void *ptr;
size_t alignment = 0;
if ((heap->caps & caps) != caps)
sof_panic(SOF_IPC_PANIC_MEM);
/* align address to dcache line size */
if (heap->info.used % PLATFORM_DCACHE_ALIGN)
alignment = PLATFORM_DCACHE_ALIGN -
(heap->info.used % PLATFORM_DCACHE_ALIGN);
/* always succeeds or panics */
if (alignment + bytes > heap->info.free) {
tr_err(&mem_tr, "rmalloc_sys(): core = %d, bytes = %d",
cpu_get_id(), bytes);
sof_panic(SOF_IPC_PANIC_MEM);
}
heap->info.used += alignment;
ptr = (void *)(heap->heap + heap->info.used);
heap->info.used += bytes;
heap->info.free -= alignment + bytes;
return ptr;
}
/* At this point the pointer we have should be unaligned
* (it was checked level higher) and be power of 2
*/
static void *align_ptr(struct mm_heap *heap, uint32_t alignment,
void *ptr, struct block_hdr *hdr)
{
/* Save unaligned ptr to block hdr */
hdr->unaligned_ptr = ptr;
/* If ptr is not already aligned we calculate alignment shift */
if (alignment <= 1)
return ptr;
return (void *)ALIGN_UP((uintptr_t)ptr, alignment);
}
/* allocate single block */
static void *alloc_block_index(struct mm_heap *heap, int level,
uint32_t alignment, int index)
{
struct block_map *map = &heap->map[level];
struct block_hdr *hdr;
void *ptr;
int i;
if (index < 0)
index = map->first_free;
map->free_count--;
hdr = &map->block[index];
ptr = (void *)(map->base + index * map->block_size);
ptr = align_ptr(heap, alignment, ptr, hdr);
hdr->size = 1;
hdr->used = 1;
heap->info.used += map->block_size;
heap->info.free -= map->block_size;
if (index == map->first_free)
/* find next free */
for (i = map->first_free; i < map->count; ++i) {
hdr = &map->block[i];
if (hdr->used == 0) {
map->first_free = i;
break;
}
}
return ptr;
}
static void *alloc_block(struct mm_heap *heap, int level,
uint32_t caps, uint32_t alignment)
{
return alloc_block_index(heap, level, alignment, -1);
}
/* allocates continuous blocks */
static void *alloc_cont_blocks(struct mm_heap *heap, int level,
uint32_t caps, size_t bytes, uint32_t alignment)
{
struct block_map *map = &heap->map[level];
struct block_hdr *hdr;
void *ptr = NULL, *unaligned_ptr;
unsigned int current;
unsigned int count = 0; /* keep compiler quiet */
unsigned int start = 0; /* keep compiler quiet */
uintptr_t blk_start = 0, aligned = 0; /* keep compiler quiet */
size_t found = 0, total_bytes = bytes;
/* check if we have enough consecutive blocks for requested
* allocation size.
*/
if ((map->count - map->first_free) * map->block_size < bytes)
return NULL;
/*
* Walk all blocks in the map, beginning with the first free one, until
* a sufficiently large sequence is found, in which the first block
* contains an address with the requested alignment.
*/
for (current = map->first_free, hdr = map->block + current;
current < map->count && found < total_bytes;
current++, hdr++) {
if (hdr->used) {
/* Restart the search */
found = 0;
count = 0;
total_bytes = bytes;
continue;
}
if (!found) {
/* A possible beginning of a sequence */
blk_start = map->base + current * map->block_size;
start = current;
/* Check if we can start a sequence here */
if (alignment) {
aligned = ALIGN_UP(blk_start, alignment);
if (blk_start & (alignment - 1) &&
aligned >= blk_start + map->block_size)
/*
* This block doesn't contain an address
* with required alignment, it is useless
* as the beginning of the sequence
*/
continue;
/*
* Found a potentially suitable beginning of a
* sequence, from here we'll check if we get
* enough blocks
*/
total_bytes += aligned - blk_start;
} else {
aligned = blk_start;
}
}
count++;
found += map->block_size;
}
if (found < total_bytes) {
tr_err(&mem_tr, "failed to allocate %u", total_bytes);
goto out;
}
ptr = (void *)aligned;
/* we found enough space, let's allocate it */
map->free_count -= count;
unaligned_ptr = (void *)blk_start;
hdr = &map->block[start];
hdr->size = count;
heap->info.used += count * map->block_size;
heap->info.free -= count * map->block_size;
/*
* if .first_free has to be updated, set it to first free block or past
* the end of the map
*/
if (map->first_free == start) {
for (current = map->first_free + count, hdr = &map->block[current];
current < map->count && hdr->used;
current++, hdr++)
;
map->first_free = current;
}
/* update each block */
for (current = start; current < start + count; current++) {
hdr = &map->block[current];
hdr->used = 1;
hdr->unaligned_ptr = unaligned_ptr;
}
out:
return ptr;
}
static inline struct mm_heap *find_in_heap_arr(struct mm_heap *heap_arr, int arr_len, void *ptr)
{
struct mm_heap *heap;
int i;
for (i = 0; i < arr_len; i++) {
heap = &heap_arr[i];
if ((uint32_t)ptr >= heap->heap &&
(uint32_t)ptr < heap->heap + heap->size)
return heap;
}
return NULL;
}
static struct mm_heap *get_heap_from_ptr(void *ptr)
{
struct mm *memmap = memmap_get();
struct mm_heap *heap;
/* find mm_heap that ptr belongs to */
heap = find_in_heap_arr(memmap->system_runtime + cpu_get_id(), 1, ptr);
if (heap)
goto out;
heap = find_in_heap_arr(memmap->runtime, PLATFORM_HEAP_RUNTIME, ptr);
if (heap)
goto out;
#if CONFIG_CORE_COUNT > 1
heap = find_in_heap_arr(memmap->runtime_shared, PLATFORM_HEAP_RUNTIME_SHARED, ptr);
if (heap)
goto out;
#endif
heap = find_in_heap_arr(memmap->buffer, PLATFORM_HEAP_BUFFER, ptr);
if (heap)
goto out;
return NULL;
out:
return heap;
}
static struct mm_heap *get_heap_from_caps(struct mm_heap *heap, int count,
uint32_t caps)
{
uint32_t mask;
int i;
/* find first heap that support type */
for (i = 0; i < count; i++) {
mask = heap[i].caps & caps;
if (mask == caps)
return &heap[i];
}
return NULL;
}
static void *get_ptr_from_heap(struct mm_heap *heap, uint32_t flags,
uint32_t caps, size_t bytes, uint32_t alignment)
{
struct block_map *map;
int i, temp_bytes = bytes;
void *ptr = NULL;
/* Only allow alignment as a power of 2 */
if ((alignment & (alignment - 1)) != 0)
sof_panic(SOF_IPC_PANIC_MEM);
for (i = 0; i < heap->blocks; i++) {
map = &heap->map[i];
/* size of requested buffer is adjusted for alignment purposes
* we check if first free block is already aligned if not
* we need to allocate bigger size for alignment
*/
if (alignment &&
((map->base + (map->block_size * map->first_free)) %
alignment))
temp_bytes += alignment;
/* is block big enough */
if (map->block_size < temp_bytes) {
temp_bytes = bytes;
continue;
}
/* does block have free space */
if (map->free_count == 0) {
temp_bytes = bytes;
continue;
}
/* free block space exists */
ptr = alloc_block(heap, i, caps, alignment);
break;
}
return ptr;
}
/* free block(s) */
static void free_block(void *ptr)
{
struct mm_heap *heap;
struct block_map *block_map = NULL;
struct block_hdr *hdr;
void *cached_ptr = uncache_to_cache(ptr);
void *uncached_ptr = cache_to_uncache(ptr);
void *free_ptr;
int i;
int block;
int used_blocks;
bool heap_is_full;
/* try cached_ptr first */
heap = get_heap_from_ptr(cached_ptr);
/* try uncached_ptr if needed */
if (!heap) {
heap = get_heap_from_ptr(uncached_ptr);
if (!heap) {
tr_err(&mem_tr, "free_block(): invalid heap, ptr = %p, cpu = %d",
ptr, cpu_get_id());
return;
}
free_ptr = uncached_ptr;
} else {
free_ptr = cached_ptr;
}
/* find block that ptr belongs to */
for (i = 0; i < heap->blocks; i++) {
block_map = &heap->map[i];
/* is ptr in this block */
if ((uint32_t)free_ptr < (block_map->base +
(block_map->block_size * block_map->count)))
break;
}
if (i == heap->blocks) {
/* not found */
tr_err(&mem_tr, "free_block(): invalid free_ptr = %p cpu = %d",
free_ptr, cpu_get_id());
return;
}
/* calculate block header */
block = ((uint32_t)free_ptr - block_map->base) / block_map->block_size;
hdr = &block_map->block[block];
/* bring back original unaligned pointer position
* and calculate correct hdr for free operation (it could
* be from different block since we got user pointer here
* or null if header was not set)
*/
if (hdr->unaligned_ptr != free_ptr && hdr->unaligned_ptr) {
free_ptr = hdr->unaligned_ptr;
block = ((uint32_t)free_ptr - block_map->base)
/ block_map->block_size;
hdr = &block_map->block[block];
}
/* report an error if ptr is not aligned to block */
if (block_map->base + block_map->block_size * block != (uint32_t)free_ptr)
sof_panic(SOF_IPC_PANIC_MEM);
/* There may still be live dirty cache lines in the region
* on the current core. Those must be invalidated, otherwise
* they will be evicted from the cache at some point in the
* future, on top of the memory region now being used for
* different purposes on another core.
*/
dcache_writeback_invalidate_region(ptr, block_map->block_size * hdr->size);
heap_is_full = !block_map->free_count;
/* free block header and continuous blocks */
used_blocks = block + hdr->size;
for (i = block; i < used_blocks; i++) {
hdr = &block_map->block[i];
hdr->size = 0;
hdr->used = 0;
hdr->unaligned_ptr = NULL;
block_map->free_count++;
heap->info.used -= block_map->block_size;
heap->info.free += block_map->block_size;
}
/* set first free block */
if (block < block_map->first_free || heap_is_full)
block_map->first_free = block;
#if CONFIG_DEBUG_BLOCK_FREE
/* memset the whole block in case of unaligned ptr */
validate_memory(
(void *)(block_map->base + block_map->block_size * block),
block_map->block_size * (i - block));
memset(
(void *)(block_map->base + block_map->block_size * block),
DEBUG_BLOCK_FREE_VALUE_8BIT, block_map->block_size *
(i - block));
#endif
}
#if CONFIG_TRACE
void heap_trace(struct mm_heap *heap, int size)
{
struct block_map *current_map;
int i;
int j;
for (i = 0; i < size; i++) {
tr_info(&mem_tr, " heap: 0x%x size %d blocks %d caps 0x%x",
heap->heap, heap->size, heap->blocks,
heap->caps);
tr_info(&mem_tr, " (In Bytes) used %d free %d", heap->info.used,
heap->info.free);
/* map[j]'s base is calculated based on map[j-1] */
for (j = 0; j < heap->blocks; j++) {
current_map = &heap->map[j];
tr_info(&mem_tr, " %d Bytes blocks ID:%d base 0x%x",
current_map->block_size, j, current_map->base);
tr_info(&mem_tr, " Number of Blocks: total %d used %d free %d",
current_map->count,
(current_map->count - current_map->free_count),
current_map->free_count);
}
heap++;
}
}
void heap_trace_all(int force)
{
struct mm *memmap = memmap_get();
/* has heap changed since last shown */
if (memmap->heap_trace_updated || force) {
tr_info(&mem_tr, "heap: system status");
heap_trace(memmap->system, PLATFORM_HEAP_SYSTEM);
tr_info(&mem_tr, "heap: system runtime status");
heap_trace(memmap->system_runtime, PLATFORM_HEAP_SYSTEM_RUNTIME);
tr_info(&mem_tr, "heap: buffer status");
heap_trace(memmap->buffer, PLATFORM_HEAP_BUFFER);
tr_info(&mem_tr, "heap: runtime status");
heap_trace(memmap->runtime, PLATFORM_HEAP_RUNTIME);
#if CONFIG_CORE_COUNT > 1
tr_info(&mem_tr, "heap: runtime shared status");
heap_trace(memmap->runtime_shared, PLATFORM_HEAP_RUNTIME_SHARED);
tr_info(&mem_tr, "heap: system shared status");
heap_trace(memmap->system_shared, PLATFORM_HEAP_SYSTEM_SHARED);
#endif
}
memmap->heap_trace_updated = 0;
}
#else
void heap_trace_all(int force) { }
void heap_trace(struct mm_heap *heap, int size) { }
#endif
#define _ALLOC_FAILURE(bytes, zone, caps, flags) \
tr_err(&mem_tr, \
"failed to alloc 0x%x bytes zone 0x%x caps 0x%x flags 0x%x", \
bytes, zone, caps, flags)
#if CONFIG_DEBUG_HEAP
#define DEBUG_TRACE_PTR(ptr, bytes, zone, caps, flags) do { \
if (trace_get()) { \
if (!(ptr)) \
_ALLOC_FAILURE(bytes, zone, caps, flags); \
heap_trace_all(0); \
} \
} while (0)
#else
#define DEBUG_TRACE_PTR(ptr, bytes, zone, caps, flags) do { \
if (trace_get()) { \
if (!(ptr)) { \
_ALLOC_FAILURE(bytes, zone, caps, flags); \
heap_trace_all(0); \
} \
} \
} while (0)
#endif
/* allocate single block for system runtime */
static void *rmalloc_sys_runtime(uint32_t flags, int caps, int core,
size_t bytes)
{
struct mm *memmap = memmap_get();
struct mm_heap *cpu_heap;
void *ptr;
/* use the heap dedicated for the selected core */
cpu_heap = memmap->system_runtime + core;
if ((cpu_heap->caps & caps) != caps)
sof_panic(SOF_IPC_PANIC_MEM);
ptr = get_ptr_from_heap(cpu_heap, flags, caps, bytes,
PLATFORM_DCACHE_ALIGN);
return ptr;
}
/* allocate single block for runtime */
static void *rmalloc_runtime(uint32_t flags, uint32_t caps, size_t bytes)
{
struct mm *memmap = memmap_get();
struct mm_heap *heap;
/* check runtime heap for capabilities */
heap = get_heap_from_caps(memmap->runtime, PLATFORM_HEAP_RUNTIME, caps);
if (!heap) {
/* next check buffer heap for capabilities */
heap = get_heap_from_caps(memmap->buffer, PLATFORM_HEAP_BUFFER,
caps);
if (!heap) {
tr_err(&mem_tr, "rmalloc_runtime(): caps = %x, bytes = %d",
caps, bytes);
return NULL;
}
}
return get_ptr_from_heap(heap, flags, caps, bytes,
PLATFORM_DCACHE_ALIGN);
}
#if CONFIG_CORE_COUNT > 1
/* allocate single block for shared */
static void *rmalloc_runtime_shared(uint32_t flags, uint32_t caps, size_t bytes)
{
struct mm *memmap = memmap_get();
struct mm_heap *heap;
/* check shared heap for capabilities */
heap = get_heap_from_caps(memmap->runtime_shared, PLATFORM_HEAP_RUNTIME_SHARED, caps);
if (!heap) {
tr_err(&mem_tr, "rmalloc_runtime_shared(): caps = %x, bytes = %d", caps, bytes);
return NULL;
}
return get_ptr_from_heap(heap, flags, caps, bytes, PLATFORM_DCACHE_ALIGN);
}
#endif
static void *_malloc_unlocked(enum mem_zone zone, uint32_t flags, uint32_t caps,
size_t bytes)
{
struct mm *memmap = memmap_get();
void *ptr = NULL;
switch (zone) {
case SOF_MEM_ZONE_SYS:
ptr = rmalloc_sys(memmap->system + cpu_get_id(), flags, caps, bytes);
break;
case SOF_MEM_ZONE_SYS_RUNTIME:
ptr = rmalloc_sys_runtime(flags, caps, cpu_get_id(), bytes);
break;
case SOF_MEM_ZONE_RUNTIME:
ptr = rmalloc_runtime(flags, caps, bytes);
break;
#if CONFIG_CORE_COUNT > 1
case SOF_MEM_ZONE_RUNTIME_SHARED:
ptr = rmalloc_runtime_shared(flags, caps, bytes);
break;
case SOF_MEM_ZONE_SYS_SHARED:
ptr = rmalloc_sys(memmap->system_shared, flags, caps, bytes);
break;
#else
case SOF_MEM_ZONE_RUNTIME_SHARED:
ptr = rmalloc_runtime(flags, caps, bytes);
break;
case SOF_MEM_ZONE_SYS_SHARED:
ptr = rmalloc_sys(memmap->system, flags, caps, bytes);
break;
#endif
default:
tr_err(&mem_tr, "rmalloc(): invalid zone");
sof_panic(SOF_IPC_PANIC_MEM); /* logic non recoverable problem */
break;
}
#if CONFIG_DEBUG_BLOCK_FREE
if (ptr)
bzero(ptr, bytes);
#endif
memmap->heap_trace_updated = 1;
return ptr;
}
void *rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes)
{
struct mm *memmap = memmap_get();
k_spinlock_key_t key;
void *ptr = NULL;
key = k_spin_lock(&memmap->lock);
ptr = _malloc_unlocked(zone, flags, caps, bytes);
k_spin_unlock(&memmap->lock, key);
DEBUG_TRACE_PTR(ptr, bytes, zone, caps, flags);
return ptr;
}
/* allocates and clears memory - not for direct use, clients use rzalloc() */
void *rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes)
{
void *ptr;
ptr = rmalloc(zone, flags, caps, bytes);
if (ptr)
bzero(ptr, bytes);
return ptr;
}
void *rzalloc_core_sys(int core, size_t bytes)
{
struct mm *memmap = memmap_get();
k_spinlock_key_t key;
void *ptr = NULL;
key = k_spin_lock(&memmap->lock);
ptr = rmalloc_sys(memmap->system + core, 0, 0, bytes);
if (ptr)
bzero(ptr, bytes);
k_spin_unlock(&memmap->lock, key);
return ptr;
}
/* allocates continuous buffers - not for direct use, clients use rballoc() */
static void *alloc_heap_buffer(struct mm_heap *heap, uint32_t flags,
uint32_t caps, size_t bytes, uint32_t alignment)
{
struct block_map *map;
#if CONFIG_DEBUG_BLOCK_FREE
unsigned int temp_bytes = bytes;
#endif
unsigned int j;
int i;
void *ptr = NULL;
/* Only allow alignment as a power of 2 */
if ((alignment & (alignment - 1)) != 0)
sof_panic(SOF_IPC_PANIC_MEM);
/*
* There are several cases when a memory allocation request can be
* satisfied with one buffer:
* 1. allocate 30 bytes 32-byte aligned from 32 byte buffers. Any free
* buffer is acceptable, the beginning of the buffer is used.
* 2. allocate 30 bytes 256-byte aligned from 0x180 byte buffers. 1
* buffer is also always enough, but in some buffers a part of the
* buffer has to be skipped.
* 3. allocate 200 bytes 256-byte aligned from 0x180 byte buffers. 1
* buffer is enough, but not every buffer is suitable.
*/
/* will request fit in single block */
for (i = 0, map = heap->map; i < heap->blocks; i++, map++) {
struct block_hdr *hdr;
uintptr_t free_start;
if (map->block_size < bytes || !map->free_count)
continue;
if (alignment <= 1) {
/* found: grab a block */
ptr = alloc_block(heap, i, caps, alignment);
break;
}
/*
* Usually block sizes are a power of 2 and all blocks are
* respectively aligned. But it's also possible to have
* non-power of 2 sized blocks, e.g. to optimize for typical
* ALSA allocations a map with 0x180 byte buffers can be used.
* For performance reasons we could first check the power-of-2
* case. This can be added as an optimization later.
*/
for (j = map->first_free, hdr = map->block + j,
free_start = map->base + map->block_size * j;
j < map->count;
j++, hdr++, free_start += map->block_size) {
uintptr_t aligned;
if (hdr->used)
continue;
aligned = ALIGN_UP(free_start, alignment);
if (aligned + bytes > free_start + map->block_size)
continue;
/* Found, alloc_block_index() cannot fail */
ptr = alloc_block_index(heap, i, alignment, j);
#if CONFIG_DEBUG_BLOCK_FREE
temp_bytes += aligned - free_start;
#endif
break;
}
if (ptr)
break;
}
/* request spans > 1 block */
if (!ptr) {
/* size of requested buffer is adjusted for alignment purposes
* since we span more blocks we have to assume worst case scenario
*/
bytes += alignment;
if (heap->size < bytes)
return NULL;
/*
* Find the best block size for request. We know, that we failed
* to find a single large enough block, so, skip those.
*/
for (i = heap->blocks - 1; i >= 0; i--) {
map = &heap->map[i];
/* allocate if block size is smaller than request */
if (map->block_size < bytes) {
ptr = alloc_cont_blocks(heap, i, caps,
bytes, alignment);
if (ptr)
break;
}
}
}
#if CONFIG_DEBUG_BLOCK_FREE
if (ptr)
bzero(ptr, temp_bytes);
#endif
return ptr;
}
static void *_balloc_unlocked(uint32_t flags, uint32_t caps, size_t bytes,
uint32_t alignment)
{
struct mm *memmap = memmap_get();
struct mm_heap *heap;
unsigned int i, n;
void *ptr = NULL;
for (i = 0, n = PLATFORM_HEAP_BUFFER, heap = memmap->buffer;
i < PLATFORM_HEAP_BUFFER;
i = heap - memmap->buffer + 1, n = PLATFORM_HEAP_BUFFER - i,
heap++) {
heap = get_heap_from_caps(heap, n, caps);
if (!heap)
break;
ptr = alloc_heap_buffer(heap, flags, caps, bytes, alignment);
if (ptr)
break;
/* Continue from the next heap */
}
/* return directly if allocation failed */
if (!ptr)
return ptr;
#ifdef CONFIG_DEBUG_FORCE_COHERENT_BUFFER
return cache_to_uncache(ptr);
#else
return (flags & SOF_MEM_FLAG_COHERENT) && (CONFIG_CORE_COUNT > 1) ?
cache_to_uncache(ptr) : uncache_to_cache(ptr);
#endif
}
/* allocates continuous buffers - not for direct use, clients use rballoc() */
void *rballoc_align(uint32_t flags, uint32_t caps, size_t bytes,
uint32_t alignment)
{
struct mm *memmap = memmap_get();
void *ptr = NULL;
k_spinlock_key_t key;
key = k_spin_lock(&memmap->lock);
ptr = _balloc_unlocked(flags, caps, bytes, alignment);
k_spin_unlock(&memmap->lock, key);
DEBUG_TRACE_PTR(ptr, bytes, SOF_MEM_ZONE_BUFFER, caps, flags);
return ptr;
}
static void _rfree_unlocked(void *ptr)
{
struct mm *memmap = memmap_get();
struct mm_heap *heap;
/* sanity check - NULL ptrs are fine */
if (!ptr)
return;
/* prepare pointer if it's platform requirement */
ptr = platform_rfree_prepare(ptr);
/* use the heap dedicated for the core or shared memory */
#if CONFIG_CORE_COUNT > 1
if (is_uncached(ptr))
heap = memmap->system_shared;
else
heap = memmap->system + cpu_get_id();
#else
heap = memmap->system;
#endif
/* panic if pointer is from system heap */
if (ptr >= (void *)heap->heap &&
(char *)ptr < (char *)heap->heap + heap->size) {
tr_err(&mem_tr, "rfree(): attempt to free system heap = %p, cpu = %d",
ptr, cpu_get_id());
sof_panic(SOF_IPC_PANIC_MEM);
}
/* free the block */
free_block(ptr);
memmap->heap_trace_updated = 1;
}
void rfree(void *ptr)
{
struct mm *memmap = memmap_get();
k_spinlock_key_t key;