Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
/*
 * rmm.c
 *
 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
 *
 * Copyright (C) 2005-2006 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.
 */

/*
 *  This memory manager provides general heap management and arbitrary
 *  alignment for any number of memory segments.
 *
 *  Notes:
 *
 *  Memory blocks are allocated from the end of the first free memory
 *  block large enough to satisfy the request.  Alignment requirements
 *  are satisfied by "sliding" the block forward until its base satisfies
 *  the alignment specification; if this is not possible then the next
 *  free block large enough to hold the request is tried.
 *
 *  Since alignment can cause the creation of a new free block - the
 *  unused memory formed between the start of the original free block
 *  and the start of the allocated block - the memory manager must free
 *  this memory to prevent a memory leak.
 *
 *  Overlay memory is managed by reserving through rmm_alloc, and freeing
 *  it through rmm_free. The memory manager prevents DSP code/data that is
 *  overlayed from being overwritten as long as the memory it runs at has
 *  been allocated, and not yet freed.
 */

#include <linux/types.h>
#include <linux/list.h>

/*  ----------------------------------- Host OS */
#include <dspbridge/host_os.h>

/*  ----------------------------------- DSP/BIOS Bridge */
#include <dspbridge/dbdefs.h>

/*  ----------------------------------- This */
#include <dspbridge/rmm.h>

/*
 *  ======== rmm_header ========
 *  This header is used to maintain a list of free memory blocks.
 */
struct rmm_header {
	struct rmm_header *next;	/* form a free memory link list */
	u32 size;		/* size of the free memory */
	u32 addr;		/* DSP address of memory block */
};

/*
 *  ======== rmm_ovly_sect ========
 *  Keeps track of memory occupied by overlay section.
 */
struct rmm_ovly_sect {
	struct list_head list_elem;
	u32 addr;		/* Start of memory section */
	u32 size;		/* Length (target MAUs) of section */
	s32 page;		/* Memory page */
};

/*
 *  ======== rmm_target_obj ========
 */
struct rmm_target_obj {
	struct rmm_segment *seg_tab;
	struct rmm_header **free_list;
	u32 num_segs;
	struct list_head ovly_list;	/* List of overlay memory in use */
};

static bool alloc_block(struct rmm_target_obj *target, u32 segid, u32 size,
			u32 align, u32 *dsp_address);
static bool free_block(struct rmm_target_obj *target, u32 segid, u32 addr,
		       u32 size);

/*
 *  ======== rmm_alloc ========
 */
int rmm_alloc(struct rmm_target_obj *target, u32 segid, u32 size,
		     u32 align, u32 *dsp_address, bool reserve)
{
	struct rmm_ovly_sect *sect, *prev_sect = NULL;
	struct rmm_ovly_sect *new_sect;
	u32 addr;
	int status = 0;

	if (!reserve) {
		if (!alloc_block(target, segid, size, align, dsp_address)) {
			status = -ENOMEM;
		} else {
			/* Increment the number of allocated blocks in this
			 * segment */
			target->seg_tab[segid].number++;
		}
		goto func_end;
	}
	/* An overlay section - See if block is already in use. If not,
	 * insert into the list in ascending address size. */
	addr = *dsp_address;
	/*  Find place to insert new list element. List is sorted from
	 *  smallest to largest address. */
	list_for_each_entry(sect, &target->ovly_list, list_elem) {
		if (addr <= sect->addr) {
			/* Check for overlap with sect */
			if ((addr + size > sect->addr) || (prev_sect &&
							   (prev_sect->addr +
							    prev_sect->size >
							    addr))) {
				status = -ENXIO;
			}
			break;
		}
		prev_sect = sect;
	}
	if (!status) {
		/* No overlap - allocate list element for new section. */
		new_sect = kzalloc(sizeof(struct rmm_ovly_sect), GFP_KERNEL);
		if (new_sect == NULL) {
			status = -ENOMEM;
		} else {
			new_sect->addr = addr;
			new_sect->size = size;
			new_sect->page = segid;
			if (list_is_last(&sect->list_elem, &target->ovly_list))
				/* Put new section at the end of the list */
				list_add_tail(&new_sect->list_elem,
						&target->ovly_list);
			else
				/* Put new section just before sect */
				list_add_tail(&new_sect->list_elem,
						&sect->list_elem);
		}
	}
func_end:
	return status;
}

/*
 *  ======== rmm_create ========
 */
int rmm_create(struct rmm_target_obj **target_obj,
		      struct rmm_segment seg_tab[], u32 num_segs)
{
	struct rmm_header *hptr;
	struct rmm_segment *sptr, *tmp;
	struct rmm_target_obj *target;
	s32 i;
	int status = 0;

	/* Allocate DBL target object */
	target = kzalloc(sizeof(struct rmm_target_obj), GFP_KERNEL);

	if (target == NULL)
		status = -ENOMEM;

	if (status)
		goto func_cont;

	target->num_segs = num_segs;
	if (!(num_segs > 0))
		goto func_cont;

	/* Allocate the memory for freelist from host's memory */
	target->free_list = kzalloc(num_segs * sizeof(struct rmm_header *),
							GFP_KERNEL);
	if (target->free_list == NULL) {
		status = -ENOMEM;
	} else {
		/* Allocate headers for each element on the free list */
		for (i = 0; i < (s32) num_segs; i++) {
			target->free_list[i] =
				kzalloc(sizeof(struct rmm_header), GFP_KERNEL);
			if (target->free_list[i] == NULL) {
				status = -ENOMEM;
				break;
			}
		}
		/* Allocate memory for initial segment table */
		target->seg_tab = kzalloc(num_segs * sizeof(struct rmm_segment),
								GFP_KERNEL);
		if (target->seg_tab == NULL) {
			status = -ENOMEM;
		} else {
			/* Initialize segment table and free list */
			sptr = target->seg_tab;
			for (i = 0, tmp = seg_tab; num_segs > 0;
			     num_segs--, i++) {
				*sptr = *tmp;
				hptr = target->free_list[i];
				hptr->addr = tmp->base;
				hptr->size = tmp->length;
				hptr->next = NULL;
				tmp++;
				sptr++;
			}
		}
	}
func_cont:
	/* Initialize overlay memory list */
	if (!status)
		INIT_LIST_HEAD(&target->ovly_list);

	if (!status) {
		*target_obj = target;
	} else {
		*target_obj = NULL;
		if (target)
			rmm_delete(target);

	}

	return status;
}

/*
 *  ======== rmm_delete ========
 */
void rmm_delete(struct rmm_target_obj *target)
{
	struct rmm_ovly_sect *sect, *tmp;
	struct rmm_header *hptr;
	struct rmm_header *next;
	u32 i;

	kfree(target->seg_tab);

	list_for_each_entry_safe(sect, tmp, &target->ovly_list, list_elem) {
		list_del(&sect->list_elem);
		kfree(sect);
	}

	if (target->free_list != NULL) {
		/* Free elements on freelist */
		for (i = 0; i < target->num_segs; i++) {
			hptr = next = target->free_list[i];
			while (next) {
				hptr = next;
				next = hptr->next;
				kfree(hptr);
			}
		}
		kfree(target->free_list);
	}

	kfree(target);
}

/*
 *  ======== rmm_free ========
 */
bool rmm_free(struct rmm_target_obj *target, u32 segid, u32 dsp_addr, u32 size,
	      bool reserved)
{
	struct rmm_ovly_sect *sect, *tmp;
	bool ret = false;

	/*
	 *  Free or unreserve memory.
	 */
	if (!reserved) {
		ret = free_block(target, segid, dsp_addr, size);
		if (ret)
			target->seg_tab[segid].number--;

	} else {
		/* Unreserve memory */
		list_for_each_entry_safe(sect, tmp, &target->ovly_list,
				list_elem) {
			if (dsp_addr == sect->addr) {
				/* Remove from list */
				list_del(&sect->list_elem);
				kfree(sect);
				return true;
			}
		}
	}
	return ret;
}

/*
 *  ======== rmm_stat ========
 */
bool rmm_stat(struct rmm_target_obj *target, enum dsp_memtype segid,
	      struct dsp_memstat *mem_stat_buf)
{
	struct rmm_header *head;
	bool ret = false;
	u32 max_free_size = 0;
	u32 total_free_size = 0;
	u32 free_blocks = 0;

	if ((u32) segid < target->num_segs) {
		head = target->free_list[segid];

		/* Collect data from free_list */
		while (head != NULL) {
			max_free_size = max(max_free_size, head->size);
			total_free_size += head->size;
			free_blocks++;
			head = head->next;
		}

		/* ul_size */
		mem_stat_buf->size = target->seg_tab[segid].length;

		/* num_free_blocks */
		mem_stat_buf->num_free_blocks = free_blocks;

		/* total_free_size */
		mem_stat_buf->total_free_size = total_free_size;

		/* len_max_free_block */
		mem_stat_buf->len_max_free_block = max_free_size;

		/* num_alloc_blocks */
		mem_stat_buf->num_alloc_blocks =
		    target->seg_tab[segid].number;

		ret = true;
	}

	return ret;
}

/*
 *  ======== balloc ========
 *  This allocation function allocates memory from the lowest addresses
 *  first.
 */
static bool alloc_block(struct rmm_target_obj *target, u32 segid, u32 size,
			u32 align, u32 *dsp_address)
{
	struct rmm_header *head;
	struct rmm_header *prevhead = NULL;
	struct rmm_header *next;
	u32 tmpalign;
	u32 alignbytes;
	u32 hsize;
	u32 allocsize;
	u32 addr;

	alignbytes = (align == 0) ? 1 : align;
	prevhead = NULL;
	head = target->free_list[segid];

	do {
		hsize = head->size;
		next = head->next;

		addr = head->addr;	/* alloc from the bottom */

		/* align allocation */
		(tmpalign = (u32) addr % alignbytes);
		if (tmpalign != 0)
			tmpalign = alignbytes - tmpalign;

		allocsize = size + tmpalign;

		if (hsize >= allocsize) {	/* big enough */
			if (hsize == allocsize && prevhead != NULL) {
				prevhead->next = next;
				kfree(head);
			} else {
				head->size = hsize - allocsize;
				head->addr += allocsize;
			}

			/* free up any hole created by alignment */
			if (tmpalign)
				free_block(target, segid, addr, tmpalign);

			*dsp_address = addr + tmpalign;
			return true;
		}

		prevhead = head;
		head = next;

	} while (head != NULL);

	return false;
}

/*
 *  ======== free_block ========
 *  TO DO: free_block() allocates memory, which could result in failure.
 *  Could allocate an rmm_header in rmm_alloc(), to be kept in a pool.
 *  free_block() could use an rmm_header from the pool, freeing as blocks
 *  are coalesced.
 */
static bool free_block(struct rmm_target_obj *target, u32 segid, u32 addr,
		       u32 size)
{
	struct rmm_header *head;
	struct rmm_header *thead;
	struct rmm_header *rhead;
	bool ret = true;

	/* Create a memory header to hold the newly free'd block. */
	rhead = kzalloc(sizeof(struct rmm_header), GFP_KERNEL);
	if (rhead == NULL) {
		ret = false;
	} else {
		/* search down the free list to find the right place for addr */
		head = target->free_list[segid];

		if (addr >= head->addr) {
			while (head->next != NULL && addr > head->next->addr)
				head = head->next;

			thead = head->next;

			head->next = rhead;
			rhead->next = thead;
			rhead->addr = addr;
			rhead->size = size;
		} else {
			*rhead = *head;
			head->next = rhead;
			head->addr = addr;
			head->size = size;
			thead = rhead->next;
		}

		/* join with upper block, if possible */
		if (thead != NULL && (rhead->addr + rhead->size) ==
		    thead->addr) {
			head->next = rhead->next;
			thead->size = size + thead->size;
			thead->addr = addr;
			kfree(rhead);
			rhead = thead;
		}

		/* join with the lower block, if possible */
		if ((head->addr + head->size) == rhead->addr) {
			head->next = rhead->next;
			head->size = head->size + rhead->size;
			kfree(rhead);
		}
	}

	return ret;
}