1
/******************************************************************************
2
* Copyright (c) 2013 IBM Corporation
4
* This program and the accompanying materials
5
* are made available under the terms of the BSD License
6
* which accompanies this distribution, and is available at
7
* http://www.opensource.org/licenses/bsd-license.php
10
* IBM Corporation - initial implementation
11
*****************************************************************************/
13
* All functions concerning interface to slof
20
#include <allocator.h>
25
#define dprintf(_x ...) do { printf ("%s: ", __func__); printf(_x); } while (0);
27
#define dprintf(_x ...)
30
#define DEFAULT_BLOCK_SIZE 4096
32
#define BM_WORD_SIZE (sizeof(unsigned long))
33
#define BM_WORD_BITS (BM_WORD_SIZE * 8)
38
unsigned long bm_size;
39
unsigned long block_size;
40
unsigned long free_blocks;
44
#define BIT(x) (1UL << x)
45
#define BM_WORD(bmw, n) (bmw[n/BM_WORD_BITS])
46
#define BM_WORD_MODULO(n) (n % BM_WORD_BITS)
47
#define BM_NUM_BITS(reqsize, bsize) ((reqsize / bsize) + (reqsize % bsize? 1 : 0))
49
static void bm_clear_bit(unsigned long *bmw, int n)
51
BM_WORD(bmw, n) &= ~BIT(BM_WORD_MODULO(n));
54
static void bm_set_bit(unsigned long *bmw, int n)
56
BM_WORD(bmw, n) |= BIT(BM_WORD_MODULO(n));
59
static bool bm_test_bit(unsigned long *bmw, int n)
62
//printf("BMW %x, bitpos %d, value %d\n", &BM_WORD(bmw, n), n, !!(BM_WORD(bmw, n) & BIT(BM_WORD_MODULO(n))));
64
return !!(BM_WORD(bmw, n) & BIT(BM_WORD_MODULO(n)));
67
/* Improvement: can use FFS routines to get faster results */
68
static int bm_find_bits(struct bitmap *bm, unsigned int n_bits)
70
unsigned int i, j, total_bits;
72
dprintf("Finding %d bits set\n", n_bits);
73
total_bits = BM_NUM_BITS(bm->size, bm->block_size);
74
for(i = 0; i < total_bits; i++) {
75
if (!bm_test_bit(bm->bmw, i))
77
/* we have hit the boundary now, give up */
78
if (i + n_bits > total_bits)
80
/* Lets find if we have consecutive bits set */
81
for(j = i; j < (i + n_bits); j++) {
82
if (!bm_test_bit(bm->bmw, (j)))
86
if (j == (i + n_bits)) {
94
void SLOF_bm_print(unsigned long handle)
102
bm = (struct bitmap *) handle;
103
printf("BITMAP: start %lx, size %ld, blocksize %ld\n\n",
104
bm->start, bm->size, bm->block_size);
105
printf("0 16 32 48 63\n");
106
for(i = 0; i < BM_NUM_BITS(bm->size, bm->block_size); i++) {
107
if (i > 0 && (i % 64 == 0))
109
else if (i > 0 && (i % 8 == 0))
111
printf("%d", bm_test_bit(bm->bmw, i));
116
unsigned long SLOF_bm_allocator_init(unsigned long start, unsigned long size,
117
unsigned long blocksize)
120
unsigned long alloc_size, bm_size, n_bits;
122
dprintf("enter start %x, size %d, block-size %d\n", start, size, blocksize);
127
blocksize = DEFAULT_BLOCK_SIZE;
129
n_bits = BM_NUM_BITS(size, blocksize);
130
bm_size = (n_bits / BM_WORD_BITS) + ((n_bits % BM_WORD_BITS)? 1 : 0);
131
alloc_size = sizeof(struct bitmap) + bm_size * BM_WORD_SIZE;
132
dprintf("Size %ld, blocksize %ld, bm_size %ld, alloc_size %ld\n",
133
size, blocksize, bm_size, alloc_size);
134
bm = (struct bitmap *) SLOF_alloc_mem(alloc_size);
139
bm->bm_size = bm_size;
140
bm->block_size = blocksize;
141
bm->free_blocks = n_bits;
142
memset(bm->bmw, 0xFF, bm_size*BM_WORD_SIZE);
143
return (unsigned long)bm;
146
unsigned long SLOF_bm_alloc(unsigned long handle, unsigned long size)
149
unsigned long n_bits;
157
bm = (struct bitmap *) handle;
159
n_bits = BM_NUM_BITS(size, bm->block_size);
160
if (n_bits > bm->free_blocks)
163
bitpos = bm_find_bits(bm, n_bits);
167
dprintf("BMW %d, bitpos %d\n", i, bitpos);
168
dprintf("size %d, block_size %d, n_bits %d\n", size, bm->block_size, n_bits);
169
for(i = bitpos; i < (bitpos + n_bits); i++) {
171
if (!bm_test_bit(bm->bmw, i))
172
dprintf("Warning: Bit already in use: %d\n", i);
174
bm_clear_bit(bm->bmw, i);
176
bm->free_blocks -= n_bits;
177
addr = bm->start + bitpos * bm->block_size;
178
dprintf("BMW %d, bitpos %d addr %lx free_blocks %d\n", i, bitpos, addr, bm->free_blocks);
182
void SLOF_bm_free(unsigned long handle, unsigned long ptr, unsigned long size)
185
unsigned long bitpos, n_bits;
192
bm = (struct bitmap *) handle;
193
addr = (unsigned long ) ptr;
194
n_bits = BM_NUM_BITS(size, bm->block_size);
195
if (addr < bm->start || (bm->start + bm->size) < (addr + size)) {
196
printf("Error: Bitmap start %lx, size %ld, requested address %lx, size %ld\n",
197
bm->start, bm->size, addr, size);
200
bitpos = (addr - bm->start) / bm->block_size;
201
bm->free_blocks += n_bits;
204
dprintf("addr %lx, bitpos %d\n", addr, bitpos);
205
dprintf("size %d, block_size %d, n_bits %d, free_blocks %d\n", size, bm->block_size, n_bits, bm->free_blocks);
206
if (addr % bm->block_size) {
207
dprintf("Warning: Address not aligned addr %lx\n", addr);
211
for(i = bitpos; i < (bitpos + n_bits); i++) {
213
if (bm_test_bit(bm->bmw, i))
214
dprintf("Warning: Bit already set: %d\n", i);
216
bm_set_bit(bm->bmw, i);