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
|
/* This file is part of the program psim.
Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _HW_MEMORY_C_
#define _HW_MEMORY_C_
#ifndef STATIC_INLINE_HW_MEMORY
#define STATIC_INLINE_HW_MEMORY STATIC_INLINE
#endif
#include "device_table.h"
/* DEVICE
memory - description of system memory
DESCRIPTION
This device describes the size and location of the banks of
physical memory within the simulation.
In addition, this device supports the "claim" and "release" methods
that can be used by OpenBoot client programs to manage the
allocation of physical memory.
PROPERTIES
reg = { <address> <size> } (required)
Each pair specify one bank of memory.
available = { <address> <size> } (automatic)
Each pair specifies a block of memory that is currently unallocated.
*/
typedef struct _memory_reg_spec {
unsigned32 base;
unsigned32 size;
} memory_reg_spec;
typedef struct _hw_memory_chunk hw_memory_chunk;
struct _hw_memory_chunk {
unsigned_word address;
unsigned_word size;
unsigned_word alloc_address;
unsigned_word alloc_size;
int available;
hw_memory_chunk *next;
};
typedef struct _hw_memory_device {
hw_memory_chunk *heap;
} hw_memory_device;
static void *
hw_memory_create(const char *name,
const device_unit *unit_address,
const char *args)
{
hw_memory_device *hw_memory = ZALLOC(hw_memory_device);
return hw_memory;
}
static void
hw_memory_set_available(device *me,
hw_memory_device *hw_memory)
{
hw_memory_chunk *chunk = NULL;
memory_reg_spec *available = NULL;
int nr_available = 0;
int curr = 0;
int sizeof_available = 0;
/* determine the nr of available chunks */
chunk = hw_memory->heap;
nr_available = 0;
while (chunk != NULL) {
if (chunk->available)
nr_available += 1;
chunk = chunk->next;
}
/* now create the available struct */
ASSERT(nr_available > 0);
sizeof_available = sizeof(memory_reg_spec) * nr_available;
available = zalloc(sizeof_available);
chunk = hw_memory->heap;
curr = 0;
while (chunk != NULL) {
if (chunk->available) {
available[curr].base = H2BE_4(chunk->address);
available[curr].size = H2BE_4(chunk->size);
curr += 1;
}
chunk = chunk->next;
}
/* update */
device_set_array_property(me, "available", available, sizeof_available);
zfree(available);
}
static void
hw_memory_init_address(device *me)
{
hw_memory_device *hw_memory = (hw_memory_device*)device_data(me);
const device_property *reg = device_find_array_property(me, "reg");
const memory_reg_spec *spec = reg->array;
int nr_entries = reg->sizeof_array / sizeof(*spec);
/* sanity check reg property */
if ((reg->sizeof_array % sizeof(*spec)) != 0)
error("devices/%s reg property of incorrect size\n", device_name(me));
/* free up any previous structures */
{
hw_memory_chunk *curr_chunk = hw_memory->heap;
hw_memory->heap = NULL;
while (curr_chunk != NULL) {
hw_memory_chunk *dead_chunk = curr_chunk;
curr_chunk = dead_chunk->next;
dead_chunk->next = NULL;
zfree(dead_chunk);
}
}
/* count/allocate memory entries */
{
hw_memory_chunk **curr_chunk = &hw_memory->heap;
while (nr_entries > 0) {
hw_memory_chunk *new_chunk = ZALLOC(hw_memory_chunk);
new_chunk->address = BE2H_4(spec->base);
new_chunk->size = BE2H_4(spec->size);
new_chunk->available = 1;
device_attach_address(device_parent(me),
device_name(me),
attach_raw_memory,
0 /*address space*/,
new_chunk->address,
new_chunk->size,
access_read_write_exec,
me);
spec++;
nr_entries--;
*curr_chunk = new_chunk;
curr_chunk = &new_chunk->next;
}
}
/* initialize the alloc property for this device */
hw_memory_set_available(me, hw_memory);
}
static void
hw_memory_instance_delete(device_instance *instance)
{
return;
}
static unsigned_word
hw_memory_instance_claim(device_instance *instance,
unsigned_word address,
unsigned_word size,
unsigned_word alignment)
{
hw_memory_device *hw_memory = device_instance_data(instance);
hw_memory_chunk *chunk = NULL;
DTRACE(memory, ("claim - address=0x%lx size=0x%lx alignment=%d\n",
(unsigned long)address,
(unsigned long)size,
(int)alignment));
/* find a chunk candidate, either according to address or alignment */
if (alignment == 0) {
chunk = hw_memory->heap;
while (chunk != NULL
&& (address+size) > (chunk->address+chunk->size))
chunk = chunk->next;
if (chunk == NULL || address < chunk->address || !chunk->available)
error("hw_memory_instance_claim: failed to allocate @0x%lx, size %ld\n",
(unsigned long)address, (unsigned long)size);
}
else {
chunk = hw_memory->heap;
while (chunk != NULL && chunk->size < size)
chunk = chunk->next;
if (chunk == NULL || FLOOR_PAGE(alignment-1) > 0)
error("hw_memory_instance_claim: failed to allocate %ld, align %ld\n",
(unsigned long)size, (unsigned long)size);
address = chunk->address;
}
/* break of a part before this memory if needed */
ASSERT(address >= chunk->address);
if (FLOOR_PAGE(address) > chunk->address) {
hw_memory_chunk *last_chunk = chunk;
/* insert a new earlier chunk */
chunk = ZALLOC(hw_memory_chunk);
chunk->next = last_chunk->next;
last_chunk->next = chunk;
/* adjust the address/size */
chunk->address = FLOOR_PAGE(address);
chunk->size = last_chunk->size - (chunk->address - last_chunk->address);
last_chunk->size = chunk->address - last_chunk->address;
}
ASSERT(FLOOR_PAGE(address) == chunk->address);
/* break of a bit after this chunk if needed */
if (ALIGN_PAGE(address+size) < chunk->address + chunk->size) {
hw_memory_chunk *next_chunk = ZALLOC(hw_memory_chunk);
/* insert it in to the list */
next_chunk->next = chunk->next;
chunk->next = next_chunk;
next_chunk->available = 1;
/* adjust the address/size */
next_chunk->address = ALIGN_PAGE(address+size);
next_chunk->size = chunk->address + chunk->size - next_chunk->address;
chunk->size = next_chunk->address - chunk->address;
}
ASSERT(ALIGN_PAGE(address+size) == chunk->address + chunk->size);
/* now allocate it */
chunk->alloc_address = address;
chunk->alloc_size = size;
chunk->available = 0;
hw_memory_set_available(device_instance_device(instance), hw_memory);
return address;
}
static void
hw_memory_instance_release(device_instance *instance,
unsigned_word address,
unsigned_word length)
{
hw_memory_device *hw_memory = device_instance_data(instance);
hw_memory_chunk *chunk = hw_memory->heap;
while (chunk != NULL) {
if (chunk->alloc_address == address
&& chunk->alloc_size == length
&& chunk->available == 0) {
/* free this chunk */
chunk->available = 1;
/* check for merge */
chunk = hw_memory->heap;
while (chunk != NULL) {
if (chunk->available
&& chunk->next != NULL && chunk->next->available) {
/* adjacent */
hw_memory_chunk *delete = chunk->next;
ASSERT(chunk->address + chunk->size == delete->address);
chunk->size += delete->size;
chunk->next = delete->next;
zfree(delete);
}
}
/* update the corresponding property */
hw_memory_set_available(device_instance_device(instance), hw_memory);
return;
}
chunk = chunk->next;
}
error("hw_memory_instance_release: Address 0x%lx, size %ld not found\n",
(unsigned long)address, (unsigned long)length);
/* FIXME - dump allocated */
/* FIXME - dump arguments */
}
static device_instance_callbacks const hw_memory_instance_callbacks = {
hw_memory_instance_delete,
NULL /*read*/, NULL /*write*/, NULL /*seek*/,
hw_memory_instance_claim, hw_memory_instance_release
};
static device_instance *
hw_memory_create_instance(device *me,
const char *path,
const char *args)
{
return device_create_instance_from(me, NULL,
device_data(me), /* nothing better */
path, args,
&hw_memory_instance_callbacks);
}
static device_callbacks const hw_memory_callbacks = {
{ hw_memory_init_address, },
{ NULL, }, /* address */
{ NULL, }, /* IO */
{ NULL, }, /* DMA */
{ NULL, }, /* interrupt */
{ NULL, }, /* unit */
hw_memory_create_instance,
};
const device_descriptor hw_memory_device_descriptor[] = {
{ "memory", hw_memory_create, &hw_memory_callbacks },
{ NULL },
};
#endif /* _HW_MEMORY_C_ */
|