blob: dab8fb36b13c48cf09813720e2680bd5af48b077 [file] [log] [blame]
Kevin O'Connore54ee382009-07-26 19:33:13 -04001// Post memory manager (PMM) calls
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "util.h" // checksum
8#include "config.h" // BUILD_BIOS_ADDR
Kevin O'Connora3855ad2009-08-16 21:59:40 -04009#include "memmap.h" // find_high_area
Kevin O'Connor0bf92702009-08-01 11:45:37 -040010#include "farptr.h" // GET_FARVAR
Kevin O'Connorf416fe92009-09-24 20:51:55 -040011#include "biosvar.h" // GET_BDA
Kevin O'Connor0bf92702009-08-01 11:45:37 -040012
13
Kevin O'Connor52a300f2009-12-26 23:32:57 -050014#if MODESEGMENT
Kevin O'Connor0bf92702009-08-01 11:45:37 -040015// The 16bit pmm entry points runs in "big real" mode, and can
16// therefore read/write to the 32bit malloc variables.
Kevin O'Connoreda2c832009-12-27 00:14:26 -050017#define GET_PMMVAR(var) ({ \
18 SET_SEG(ES, 0); \
19 __GET_VAR("addr32 ", ES, (var)); })
20#define SET_PMMVAR(var, val) do { \
21 SET_SEG(ES, 0); \
22 __SET_VAR("addr32 ", ES, (var), (val)); \
23 } while (0)
Kevin O'Connor0bf92702009-08-01 11:45:37 -040024#else
25#define GET_PMMVAR(var) (var)
26#define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
27#endif
28
29// Zone definitions
30struct zone_s {
31 u32 top, bottom, cur;
32};
33
Kevin O'Connor52a300f2009-12-26 23:32:57 -050034struct zone_s ZoneLow VAR32FLATVISIBLE, ZoneHigh VAR32FLATVISIBLE;
35struct zone_s ZoneFSeg VAR32FLATVISIBLE;
36struct zone_s ZoneTmpLow VAR32FLATVISIBLE, ZoneTmpHigh VAR32FLATVISIBLE;
Kevin O'Connor0bf92702009-08-01 11:45:37 -040037
Kevin O'Connor52a300f2009-12-26 23:32:57 -050038struct zone_s *Zones[] VAR32FLATVISIBLE = {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -040039 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
Kevin O'Connor0bf92702009-08-01 11:45:37 -040040};
41
Kevin O'Connorf416fe92009-09-24 20:51:55 -040042
43/****************************************************************
44 * ebda movement
45 ****************************************************************/
46
47// Move ebda
48static int
49relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
50{
51 u32 lowram = GET_BDA(mem_size_kb) * 1024;
52 if (oldebda != lowram)
53 // EBDA isn't at end of ram - give up.
54 return -1;
55
56 // Do copy
Kevin O'Connor52a300f2009-12-26 23:32:57 -050057 if (MODESEGMENT)
Kevin O'Connorf416fe92009-09-24 20:51:55 -040058 memcpy_far(FLATPTR_TO_SEG(newebda)
59 , (void*)FLATPTR_TO_OFFSET(newebda)
60 , FLATPTR_TO_SEG(oldebda)
61 , (void*)FLATPTR_TO_OFFSET(oldebda)
62 , ebda_size * 1024);
63 else
64 memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
65
66 // Update indexes
67 dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
68 SET_BDA(mem_size_kb, newebda / 1024);
69 SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
70 return 0;
71}
72
73// Support expanding the ZoneLow dynamically.
Kevin O'Connord948e2b2009-10-12 12:54:56 -040074static void
Kevin O'Connorf416fe92009-09-24 20:51:55 -040075zonelow_expand(u32 size, u32 align)
76{
Kevin O'Connord948e2b2009-10-12 12:54:56 -040077 u32 oldpos = GET_PMMVAR(ZoneLow.cur);
78 u32 newpos = ALIGN_DOWN(oldpos - size, align);
79 u32 bottom = GET_PMMVAR(ZoneLow.bottom);
80 if (newpos >= bottom && newpos <= oldpos)
81 // Space already present.
82 return;
Kevin O'Connorf416fe92009-09-24 20:51:55 -040083 u16 ebda_seg = get_ebda_seg();
84 u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
Kevin O'Connorf416fe92009-09-24 20:51:55 -040085 u8 ebda_size = GET_EBDA2(ebda_seg, size);
86 u32 ebda_end = ebda_pos + ebda_size * 1024;
Kevin O'Connord948e2b2009-10-12 12:54:56 -040087 if (ebda_end != bottom) {
Kevin O'Connorf416fe92009-09-24 20:51:55 -040088 // Something else is after ebda - can't use any existing space.
Kevin O'Connord948e2b2009-10-12 12:54:56 -040089 oldpos = ebda_end;
90 newpos = ALIGN_DOWN(oldpos - size, align);
91 }
92 u32 newbottom = ALIGN_DOWN(newpos, 1024);
Kevin O'Connorf416fe92009-09-24 20:51:55 -040093 u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
94 if (newebda < BUILD_EBDA_MINIMUM)
95 // Not enough space.
Kevin O'Connord948e2b2009-10-12 12:54:56 -040096 return;
Kevin O'Connorf416fe92009-09-24 20:51:55 -040097
98 // Move ebda
99 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
100 if (ret)
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400101 return;
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400102
103 // Update zone
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400104 SET_PMMVAR(ZoneLow.cur, oldpos);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400105 SET_PMMVAR(ZoneLow.bottom, newbottom);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400106}
107
108
109/****************************************************************
110 * zone allocations
111 ****************************************************************/
112
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400113// Obtain memory from a given zone.
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400114static void *
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400115zone_malloc(struct zone_s *zone, u32 size, u32 align)
116{
Kevin O'Connor415d4292009-08-30 19:19:31 -0400117 u32 oldpos = GET_PMMVAR(zone->cur);
118 u32 newpos = ALIGN_DOWN(oldpos - size, align);
119 if (newpos < GET_PMMVAR(zone->bottom) || newpos > oldpos)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400120 // No space
121 return NULL;
122 SET_PMMVAR(zone->cur, newpos);
123 return (void*)newpos;
124}
125
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400126// Find the zone that contains the given data block.
127static struct zone_s *
128zone_find(void *data)
129{
130 int i;
131 for (i=0; i<ARRAY_SIZE(Zones); i++) {
132 struct zone_s *zone = GET_PMMVAR(Zones[i]);
133 if ((u32)data >= GET_PMMVAR(zone->cur)
134 && (u32)data < GET_PMMVAR(zone->top))
135 return zone;
136 }
137 return NULL;
138}
139
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400140// Return memory to a zone (if it was the last to be allocated).
141static int
142zone_free(void *data, u32 olddata)
143{
144 struct zone_s *zone = zone_find(data);
145 if (!zone || !data || GET_PMMVAR(zone->cur) != (u32)data)
146 return -1;
147 SET_PMMVAR(zone->cur, olddata);
148 return 0;
149}
150
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400151// Report the status of all the zones.
152static void
153dumpZones()
154{
155 int i;
156 for (i=0; i<ARRAY_SIZE(Zones); i++) {
157 struct zone_s *zone = Zones[i];
158 u32 used = zone->top - zone->cur;
159 u32 avail = zone->top - zone->bottom;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400160 u32 pct = avail ? ((100 * used) / avail) : 0;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400161 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400162 , i, zone->bottom, zone->top, used, pct);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400163 }
164}
165
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400166
167/****************************************************************
Kevin O'Connor8f4409b2009-11-25 18:51:46 -0500168 * tracked memory allocations
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400169 ****************************************************************/
170
171// Information on PMM tracked allocations
172struct pmmalloc_s {
173 void *data;
174 u32 olddata;
175 u32 handle;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400176 u32 oldallocdata;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400177 struct pmmalloc_s *next;
178};
179
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500180struct pmmalloc_s *PMMAllocs VAR32FLATVISIBLE;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400181
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400182// Allocate memory from the given zone and track it as a PMM allocation
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400183void *
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400184pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
185{
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400186 u32 oldallocdata = GET_PMMVAR(ZoneTmpHigh.cur);
187 struct pmmalloc_s *info = zone_malloc(&ZoneTmpHigh, sizeof(*info)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400188 , MALLOC_MIN_ALIGN);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400189 if (!info) {
190 oldallocdata = GET_PMMVAR(ZoneTmpLow.cur);
191 info = zone_malloc(&ZoneTmpLow, sizeof(*info), MALLOC_MIN_ALIGN);
192 if (!info)
193 return NULL;
194 }
195 if (zone == &ZoneLow)
196 zonelow_expand(size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400197 u32 olddata = GET_PMMVAR(zone->cur);
198 void *data = zone_malloc(zone, size, align);
199 if (! data) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400200 zone_free(info, oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400201 return NULL;
202 }
203 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
204 " ret=%p (info=%p)\n"
205 , zone, handle, size, align
206 , data, info);
207 SET_PMMVAR(info->data, data);
208 SET_PMMVAR(info->olddata, olddata);
209 SET_PMMVAR(info->handle, handle);
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400210 SET_PMMVAR(info->oldallocdata, oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400211 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
212 SET_PMMVAR(PMMAllocs, info);
213 return data;
214}
215
216// Free a raw data block (either from a zone or from pmm alloc list).
217static void
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400218pmm_free_data(void *data, u32 olddata)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400219{
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400220 int ret = zone_free(data, olddata);
221 if (!ret)
222 // Success - done.
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400223 return;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400224 struct pmmalloc_s *info;
225 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
226 if (GET_PMMVAR(info->olddata) == (u32)data) {
227 SET_PMMVAR(info->olddata, olddata);
228 return;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400229 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
230 SET_PMMVAR(info->oldallocdata, olddata);
231 return;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400232 }
233}
234
235// Free a data block allocated with pmm_malloc
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400236int
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400237pmm_free(void *data)
238{
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400239 struct pmmalloc_s **pinfo = &PMMAllocs;
240 for (;;) {
241 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
242 if (!info)
243 return -1;
244 if (GET_PMMVAR(info->data) == data) {
245 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400246 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400247 u32 olddata = GET_PMMVAR(info->olddata);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400248 pmm_free_data(data, olddata);
249 pmm_free_data(info, oldallocdata);
250 dprintf(8, "pmm_free data=%p olddata=%p oldallocdata=%p info=%p\n"
251 , data, (void*)olddata, (void*)oldallocdata, info);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400252 return 0;
253 }
254 pinfo = &info->next;
255 }
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400256}
257
258// Find the amount of free space in a given zone.
259static u32
260pmm_getspace(struct zone_s *zone)
261{
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400262 // XXX - doesn't account for ZoneLow being able to grow.
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400263 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400264 if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400265 return space;
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400266 // Account for space needed for PMM tracking.
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400267 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
268 if (space <= reserve)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400269 return 0;
Kevin O'Connor9c3e7472009-08-02 12:33:58 -0400270 return space - reserve;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400271}
272
273// Find the data block allocated with pmm_malloc with a given handle.
274static void *
275pmm_find(u32 handle)
276{
277 struct pmmalloc_s *info;
278 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
279 if (GET_PMMVAR(info->handle) == handle)
280 return GET_PMMVAR(info->data);
281 return NULL;
282}
283
Kevin O'Connor8f4409b2009-11-25 18:51:46 -0500284void
285malloc_setup()
286{
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500287 ASSERT32FLAT();
Kevin O'Connor8f4409b2009-11-25 18:51:46 -0500288 dprintf(3, "malloc setup\n");
289
290 PMMAllocs = NULL;
291
292 // Memory in 0xf0000 area.
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500293 extern u8 code32flat_start[];
294 if ((u32)code32flat_start > BUILD_BIOS_ADDR)
Kevin O'Connorc945b2f2009-11-25 18:53:52 -0500295 // Clear unused parts of f-segment
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500296 memset((void*)BUILD_BIOS_ADDR, 0
297 , (u32)code32flat_start - BUILD_BIOS_ADDR);
Kevin O'Connor8f4409b2009-11-25 18:51:46 -0500298 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
299 ZoneFSeg.bottom = (u32)BiosTableSpace;
300 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
301
302 // Memory under 1Meg.
303 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
304 ZoneTmpLow.top = ZoneTmpLow.cur = BUILD_EBDA_MINIMUM;
305
306 // Permanent memory under 1Meg.
307 ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = BUILD_LOWRAM_END;
308
309 // Find memory at the top of ram.
310 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
311 if (!e) {
312 // No memory above 1Meg
313 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
314 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
315 return;
316 }
317 u32 top = e->start + e->size, bottom = e->start;
318
319 // Memory at top of ram.
320 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
321 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
322 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
323
324 // Memory above 1Meg
325 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
326 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
327}
328
329void
330malloc_finalize()
331{
332 dprintf(3, "malloc finalize\n");
333
334 dumpZones();
335
336 // Reserve more low-mem if needed.
337 u32 endlow = GET_BDA(mem_size_kb)*1024;
338 add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
339
340 // Give back unused high ram.
341 u32 giveback = ALIGN_DOWN(ZoneHigh.cur - ZoneHigh.bottom, PAGE_SIZE);
342 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
343 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
344
345 // Clear low-memory allocations.
346 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
347}
348
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400349
350/****************************************************************
351 * pmm interface
352 ****************************************************************/
Kevin O'Connore54ee382009-07-26 19:33:13 -0400353
354struct pmmheader {
355 u32 signature;
356 u8 version;
357 u8 length;
358 u8 checksum;
359 u16 entry_offset;
360 u16 entry_seg;
361 u8 reserved[5];
362} PACKED;
363
364extern struct pmmheader PMMHEADER;
365
366#define PMM_SIGNATURE 0x4d4d5024 // $PMM
367
368#if CONFIG_PMM
369struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
370 .version = 0x01,
371 .length = sizeof(PMMHEADER),
372 .entry_seg = SEG_BIOS,
373};
374#endif
375
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400376#define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
Kevin O'Connore54ee382009-07-26 19:33:13 -0400377
378// PMM - allocate
379static u32
380handle_pmm00(u16 *args)
381{
382 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
383 u16 flags = args[5];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400384 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
Kevin O'Connore54ee382009-07-26 19:33:13 -0400385 , length, handle, flags);
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400386 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
387 if (flags & 8) {
388 // Permanent memory request.
389 lowzone = &ZoneLow;
390 highzone = &ZoneHigh;
391 }
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400392 if (!length) {
393 // Memory size request
394 switch (flags & 3) {
395 default:
396 case 0:
397 return 0;
398 case 1:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400399 return pmm_getspace(lowzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400400 case 2:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400401 return pmm_getspace(highzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400402 case 3: {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400403 u32 spacelow = pmm_getspace(lowzone);
404 u32 spacehigh = pmm_getspace(highzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400405 if (spacelow > spacehigh)
406 return spacelow;
407 return spacehigh;
408 }
409 }
410 }
411 u32 size = length * 16;
412 if ((s32)size <= 0)
413 return 0;
414 u32 align = MALLOC_MIN_ALIGN;
415 if (flags & 4) {
416 align = 1<<__ffs(size);
417 if (align < MALLOC_MIN_ALIGN)
418 align = MALLOC_MIN_ALIGN;
419 }
420 switch (flags & 3) {
421 default:
422 case 0:
423 return 0;
424 case 1:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400425 return (u32)pmm_malloc(lowzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400426 case 2:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400427 return (u32)pmm_malloc(highzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400428 case 3: {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400429 void *data = pmm_malloc(lowzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400430 if (data)
431 return (u32)data;
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400432 return (u32)pmm_malloc(highzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400433 }
434 }
Kevin O'Connore54ee382009-07-26 19:33:13 -0400435}
436
437// PMM - find
438static u32
439handle_pmm01(u16 *args)
440{
441 u32 handle = *(u32*)&args[1];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400442 dprintf(3, "pmm01: handle=%x\n", handle);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400443 if (handle == PMM_DEFAULT_HANDLE)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400444 return 0;
445 return (u32)pmm_find(handle);
Kevin O'Connore54ee382009-07-26 19:33:13 -0400446}
447
448// PMM - deallocate
449static u32
450handle_pmm02(u16 *args)
451{
452 u32 buffer = *(u32*)&args[1];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400453 dprintf(3, "pmm02: buffer=%x\n", buffer);
454 int ret = pmm_free((void*)buffer);
455 if (ret)
456 // Error
457 return 1;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400458 return 0;
459}
460
461static u32
462handle_pmmXX(u16 *args)
463{
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400464 return PMM_FUNCTION_NOT_SUPPORTED;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400465}
466
467u32 VISIBLE16
468handle_pmm(u16 *args)
469{
470 if (! CONFIG_PMM)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400471 return PMM_FUNCTION_NOT_SUPPORTED;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400472
473 u16 arg1 = args[0];
474 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
475
476 switch (arg1) {
477 case 0x00: return handle_pmm00(args);
478 case 0x01: return handle_pmm01(args);
479 case 0x02: return handle_pmm02(args);
480 default: return handle_pmmXX(args);
481 }
482}
483
484// romlayout.S
485extern void entry_pmm();
486
487void
488pmm_setup()
489{
490 if (! CONFIG_PMM)
491 return;
492
493 dprintf(3, "init PMM\n");
494
495 PMMHEADER.signature = PMM_SIGNATURE;
496 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
497 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
498}
499
500void
501pmm_finalize()
502{
503 if (! CONFIG_PMM)
504 return;
505
506 dprintf(3, "finalize PMM\n");
507
508 PMMHEADER.signature = 0;
509 PMMHEADER.entry_offset = 0;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400510}