File: bigphysarea.c

package info (click to toggle)
kernel-source-2.2.19 2.2.19.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 92,116 kB
  • ctags: 276,892
  • sloc: ansic: 1,710,377; asm: 58,705; makefile: 10,198; sh: 2,398; perl: 907; tcl: 570; lisp: 218; cpp: 186; awk: 133; sed: 72
file content (337 lines) | stat: -rw-r--r-- 9,162 bytes parent folder | download | duplicates (6)
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
/* linux/mm/bigphysarea.c, M. Welsh (mdw@cs.cornell.edu)
 * Copyright (c) 1996 by Matt Welsh.
 * Extended by Roger Butenuth (butenuth@uni-paderborn.de), October 1997
 *
 * This is a set of routines which allow you to reserve a large (?) 
 * amount of physical memory at boot-time, which can be allocated/deallocated
 * by drivers. This memory is intended to be used for devices such as 
 * video framegrabbers which need a lot of physical RAM (above the amount
 * allocated by kmalloc). This is by no means efficient or recommended;
 * to be used only in extreme circumstances.
 *
 *   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 option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/config.h>
#include <linux/ptrace.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/malloc.h>
#include <linux/bigphysarea.h>

typedef struct range_struct {
  struct range_struct *next;
  caddr_t base;			/* base of allocated block */
  size_t  size;			/* size in bytes */
} range_t;

static int bigphysarea_pages = 0;
/*
 * 0: nothing initialized
 * 1: bigphysarea_pages initialized
 * 2: free list initialized
 */
static int init_level = 0;

/*
 * The free list and the used list are protected by one lock.
 */
static spinlock_t big_lock = SPIN_LOCK_UNLOCKED;
static range_t *free_list = NULL;
static range_t *used_list = NULL;

caddr_t bigphysarea = 0;

void bigphysarea_setup(char *str, int *ints)
{
	if (ints[0] == 1)
		bigphysarea_pages = ints[1];
	else
	        bigphysarea_pages = 0;
}

unsigned long bigphysarea_init(unsigned long mem_start, unsigned long mem_end) {
	if (bigphysarea_pages == 0)
		return mem_start;

	bigphysarea = (caddr_t)((mem_start + (PAGE_SIZE - 1)) &
				~(PAGE_SIZE - 1));
	
	init_level = 1;

	printk("bigphysarea: Allocated %d pages at 0x%08lx.\n",
	       bigphysarea_pages, (unsigned long)bigphysarea);

	return (unsigned long)(bigphysarea + (bigphysarea_pages * PAGE_SIZE));
}

/*
 * When we have pages but don't have a freelist, put all pages in
 * one free list entry. Return 0 on success, 1 on error.
 */
static int init2(int priority)
{
	int res;

	spin_lock(&big_lock);	/* >>>>>>>>>> */
	if (init_level == 1) {
		free_list = kmalloc(sizeof(range_t), priority);
		if (free_list != NULL) {
			free_list->next = NULL;
			free_list->base = bigphysarea;
			free_list->size = bigphysarea_pages * PAGE_SIZE;
			init_level = 2;
			res = 0;
		} else 
			res = 1;
	} else
		res = 1;

	spin_unlock(&big_lock);	/* <<<<<<<<<< */
	return res;
}


/*
 * Allocate `count' pages from the big physical area. Pages are aligned to
 * a multiple of `align'. `priority' has the same meaning in kmalloc, it
 * is needed for management information.
 * This function may not be called from an interrupt, this is the reason
 * we can use lock functions without disabling all interrupts.
 */
caddr_t bigphysarea_alloc_pages(int count, int align, int priority)
{
	range_t *range, **range_ptr, *new_range, *align_range;
	caddr_t aligned_base;
	caddr_t res = 0;

	if (init_level < 2)
		if (init2(priority))
			return 0;
	new_range   = NULL;
	align_range = NULL;

	if (align == 0)
		align = PAGE_SIZE;
	else
		align = align * PAGE_SIZE;

	spin_lock(&big_lock);	/* >>>>>>>>>> */
	/*
	 * Search a free block which is large enough, even with alignment.
	 */
	range_ptr = &free_list;
	while (*range_ptr != NULL) {
		range = *range_ptr;
		aligned_base =
		  (caddr_t)((((long)range->base + align - 1) / align) * align);
		if (aligned_base + count * PAGE_SIZE <= 
		    range->base + range->size)
			break;
	     range_ptr = &range->next;
	}
	if (*range_ptr == NULL) {
		res = 0;
		goto ret_label;
	}
	range = *range_ptr;
	/*
	 * When we have to align, the pages needed for alignment can
	 * be put back to the free pool.
	 * We check here if we need a second range data structure later
	 * and allocate it now, so that we don't have to check for a
	 * failed kmalloc later.
	 */
	if (aligned_base - range->base + count * PAGE_SIZE < range->size) {
		new_range = kmalloc(sizeof(range_t), priority);
		if (new_range == NULL) {
			res = 0;
			goto ret_label;
		}
	}
	if (aligned_base != range->base) {
		align_range = kmalloc(sizeof(range_t), priority);
		if (align_range == NULL) {
			if (new_range != NULL)
				kfree(new_range);
			res = 0;
			goto ret_label;
		}
		align_range->base = range->base;
		align_range->size = aligned_base - range->base;
		range->base = aligned_base;
		range->size -= align_range->size;
		align_range->next = range;
		*range_ptr = align_range;
		range_ptr = &align_range->next;
	}
	if (new_range != NULL) {
		/*
		 * Range is larger than needed, create a new list element for
		 * the used list and shrink the element in the free list.
		 */
		new_range->base        = range->base;
		new_range->size        = count * PAGE_SIZE;
		range->base = new_range->base + new_range->size;
		range->size = range->size - new_range->size;
	} else {
		/*
		 * Range fits perfectly, remove it from free list.
		 */
		*range_ptr = range->next;
		new_range = range;
	}
	/*
	 * Insert block into used list
	 */
	new_range->next = used_list;
	used_list = new_range;

	res = new_range->base;
 ret_label:
	spin_unlock(&big_lock);	/* <<<<<<<<<< */
	return res;

}


/*
 * Free pages allocated with `bigphysarea_alloc_pages'. `base' must be an
 * address returned by `bigphysarea_alloc_pages'.
 * This function my not be called from an interrupt, for this reason we 
 * can use a lock without interrupt disabling.
 */
void bigphysarea_free_pages(caddr_t base)
{
	range_t *prev, *next, *range, **range_ptr;
  
	spin_lock(&big_lock);	/* >>>>>>>>>> */
	/*
	 * Search the block in the used list.
	 */
	for (range_ptr = &used_list;
	     *range_ptr != NULL;
	     range_ptr = &(*range_ptr)->next)
		if ((*range_ptr)->base == base)
			break;
	if (*range_ptr == NULL) {
		printk("bigphysarea_free_pages(0x%08x), not allocated!\n",
		       (unsigned)base);
		goto ret_label;
	}
	range = *range_ptr;
	/*
	 * Remove range from the used list:
	 */
	*range_ptr = (*range_ptr)->next;
	/*
	 * The free-list is sorted by address, search insertion point
	 * and insert block in free list.
	 */
	for (range_ptr = &free_list, prev = NULL;
	     *range_ptr != NULL;
	     prev = *range_ptr, range_ptr = &(*range_ptr)->next)
		if ((*range_ptr)->base >= base)
			break;
	range->next  = *range_ptr;
	*range_ptr   = range;
	/*
	 * Concatenate free range with neighbors, if possible.
	 * Try for upper neighbor (next in list) first, then
	 * for lower neighbor (predecessor in list).
	 */
	if (range->next != NULL &&
	    range->base + range->size == range->next->base) {
		next = range->next;
		range->size += range->next->size;
		range->next = next->next;
		kfree(next);
	}
	if (prev != NULL &&
	    prev->base + prev->size == range->base) {
		prev->size += prev->next->size;
		prev->next = range->next;
		kfree(range);
	}
 ret_label:
	spin_unlock(&big_lock);	/* <<<<<<<<<< */
}

caddr_t bigphysarea_alloc(int size)
{
	int pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;

	return bigphysarea_alloc_pages(pages, 1, GFP_KERNEL);
}

void bigphysarea_free(caddr_t addr, int size)
{
	(void)size;
	bigphysarea_free_pages(addr);
}

int get_bigphysarea_info(char *buf)
{
	char    *p = buf;
	range_t *ptr;
	int     free_count, free_total, free_max;
	int     used_count, used_total, used_max;

	if (init_level == 1)
	  init2(GFP_KERNEL);

	spin_lock(&big_lock);	/* >>>>>>>>>> */
	free_count = 0;
	free_total = 0;
	free_max   = 0;
	for (ptr = free_list; ptr != NULL; ptr = ptr->next) {
		free_count++;
		free_total += ptr->size;
		if (ptr->size > free_max)
			free_max = ptr->size;
	}

	used_count = 0;
	used_total = 0;
	used_max   = 0;
	for (ptr = used_list; ptr != NULL; ptr = ptr->next) {
		used_count++;
		used_total += ptr->size;
		if (ptr->size > used_max)
			used_max = ptr->size;
	}
	spin_unlock(&big_lock);	/* <<<<<<<<<< */

	if (bigphysarea_pages == 0) {
		p += sprintf(p, "No big physical area allocated!\n");
		return  p - buf;
	}
	  
	p += sprintf(p, "Big physical area, size %ld kB\n",
		     bigphysarea_pages * PAGE_SIZE / 1024);
	p += sprintf(p, "                       free list:             used list:\n");
	p += sprintf(p, "number of blocks:      %8d               %8d\n",
		     free_count, used_count);
	p += sprintf(p, "size of largest block: %8d kB            %8d kB\n",
		     free_max / 1024, used_max / 1024);
	p += sprintf(p, "total:                 %8d kB            %8d kB\n",
		     free_total / 1024, used_total /1024);

	return  p - buf;
}