blob: e0770ac55f73e516bc0ff3113b779b978538d2cb [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'Connor42a1d4c2010-06-06 11:10:24 -04009#include "memmap.h" // struct e820entry
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
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -040029// Information on a reserved area.
30struct allocinfo_s {
31 struct allocinfo_s *next, **pprev;
32 void *data, *dataend, *allocend;
Kevin O'Connor0bf92702009-08-01 11:45:37 -040033};
34
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -040035// Information on a tracked memory allocation.
36struct allocdetail_s {
37 struct allocinfo_s detailinfo;
38 struct allocinfo_s datainfo;
39 u32 handle;
40};
41
42// The various memory zones.
43struct zone_s {
44 struct allocinfo_s *info;
45};
46
47struct zone_s ZoneLow VAR32FLATVISIBLE;
48struct zone_s ZoneHigh VAR32FLATVISIBLE;
Kevin O'Connor52a300f2009-12-26 23:32:57 -050049struct zone_s ZoneFSeg VAR32FLATVISIBLE;
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -040050struct zone_s ZoneTmpLow VAR32FLATVISIBLE;
51struct zone_s ZoneTmpHigh VAR32FLATVISIBLE;
Kevin O'Connor0bf92702009-08-01 11:45:37 -040052
Kevin O'Connor52a300f2009-12-26 23:32:57 -050053struct zone_s *Zones[] VAR32FLATVISIBLE = {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -040054 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
Kevin O'Connor0bf92702009-08-01 11:45:37 -040055};
56
Kevin O'Connorf416fe92009-09-24 20:51:55 -040057
58/****************************************************************
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -040059 * low-level memory reservations
60 ****************************************************************/
61
62// Find and reserve space from a given zone
63static void *
64allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
65{
66 struct allocinfo_s *info;
67 for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next)) {
68 void *dataend = GET_PMMVAR(info->dataend);
69 void *allocend = GET_PMMVAR(info->allocend);
70 void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
71 if (newallocend >= dataend && newallocend <= allocend) {
72 // Found space - now reserve it.
73 struct allocinfo_s **pprev = GET_PMMVAR(info->pprev);
74 if (!fill)
75 fill = newallocend;
76 SET_PMMVAR(fill->next, info);
77 SET_PMMVAR(fill->pprev, pprev);
78 SET_PMMVAR(fill->data, newallocend);
79 SET_PMMVAR(fill->dataend, newallocend + size);
80 SET_PMMVAR(fill->allocend, allocend);
81
82 SET_PMMVAR(info->allocend, newallocend);
83 SET_PMMVAR(info->pprev, &fill->next);
84 SET_PMMVAR(*pprev, fill);
85 return newallocend;
86 }
87 }
88 return NULL;
89}
90
91// Release space allocated with allocSpace()
92static void
93freeSpace(struct allocinfo_s *info)
94{
95 struct allocinfo_s *next = GET_PMMVAR(info->next);
96 struct allocinfo_s **pprev = GET_PMMVAR(info->pprev);
97 SET_PMMVAR(*pprev, next);
98 if (next) {
99 if (GET_PMMVAR(next->allocend) == GET_PMMVAR(info->data))
100 SET_PMMVAR(next->allocend, GET_PMMVAR(info->allocend));
101 SET_PMMVAR(next->pprev, pprev);
102 }
103}
104
105// Add new memory to a zone
106static void
107addSpace(struct zone_s *zone, void *start, void *end)
108{
109 // Find position to add space
110 struct allocinfo_s **pprev = &zone->info, *info;
111 for (;;) {
112 info = GET_PMMVAR(*pprev);
113 if (!info || GET_PMMVAR(info->data) < start)
114 break;
115 pprev = &info->next;
116 }
117
118 // Add space using temporary allocation info.
119 struct allocdetail_s tempdetail;
120 tempdetail.datainfo.next = info;
121 tempdetail.datainfo.pprev = pprev;
122 tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
123 tempdetail.datainfo.allocend = end;
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400124 struct allocdetail_s *tempdetailp = MAKE_FLATPTR(GET_SEG(SS), &tempdetail);
125 SET_PMMVAR(*pprev, &tempdetailp->datainfo);
126 if (info)
127 SET_PMMVAR(info->pprev, &tempdetailp->datainfo.next);
128
129 // Allocate final allocation info.
130 struct allocdetail_s *detail = allocSpace(
131 &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
132 if (!detail) {
133 detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
134 , MALLOC_MIN_ALIGN, NULL);
135 if (!detail) {
136 SET_PMMVAR(*tempdetail.datainfo.pprev, tempdetail.datainfo.next);
137 if (tempdetail.datainfo.next)
138 SET_PMMVAR(tempdetail.datainfo.next->pprev
139 , tempdetail.datainfo.pprev);
140 warn_noalloc();
141 return;
142 }
143 }
144
145 // Replace temp alloc space with final alloc space
Kevin O'Connor8f59aa32010-06-06 16:11:45 -0400146 memcpy_fl(&detail->datainfo, &tempdetailp->datainfo
147 , sizeof(detail->datainfo));
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400148 SET_PMMVAR(detail->handle, PMM_DEFAULT_HANDLE);
149
150 SET_PMMVAR(*tempdetail.datainfo.pprev, &detail->datainfo);
151 if (tempdetail.datainfo.next)
152 SET_PMMVAR(tempdetail.datainfo.next->pprev, &detail->datainfo.next);
153}
154
155// Search all zones for an allocation obtained from allocSpace()
156static struct allocinfo_s *
157findAlloc(void *data)
158{
159 int i;
160 for (i=0; i<ARRAY_SIZE(Zones); i++) {
161 struct zone_s *zone = GET_PMMVAR(Zones[i]);
162 struct allocinfo_s *info;
163 for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next))
164 if (GET_PMMVAR(info->data) == data)
165 return info;
166 }
167 return NULL;
168}
169
170// Return the last sentinal node of a zone
171static struct allocinfo_s *
172findLast(struct zone_s *zone)
173{
174 struct allocinfo_s *info = GET_PMMVAR(zone->info);
175 if (!info)
176 return NULL;
177 for (;;) {
178 struct allocinfo_s *next = GET_PMMVAR(info->next);
179 if (!next)
180 return info;
181 info = next;
182 }
183}
184
185
186/****************************************************************
187 * Setup
188 ****************************************************************/
189
190void
191malloc_setup(void)
192{
193 ASSERT32FLAT();
194 dprintf(3, "malloc setup\n");
195
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400196 // Populate temp high ram
197 u32 highram = 0;
198 int i;
199 for (i=e820_count-1; i>=0; i--) {
200 struct e820entry *en = &e820_list[i];
201 u64 end = en->start + en->size;
202 if (end < 1024*1024)
203 break;
204 if (en->type != E820_RAM || end > 0xffffffff)
205 continue;
206 u32 s = en->start, e = end;
207 if (!highram) {
208 u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
209 if (newe <= e && newe >= s) {
210 highram = newe;
211 e = newe;
212 }
213 }
214 addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
215 }
216
217 // Populate other regions
218 addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
219 addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
220 addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
221 if (highram) {
222 addSpace(&ZoneHigh, (void*)highram
223 , (void*)highram + CONFIG_MAX_HIGHTABLE);
224 add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
225 }
226}
227
228void
229malloc_finalize(void)
230{
231 dprintf(3, "malloc finalize\n");
232
233 // Reserve more low-mem if needed.
234 u32 endlow = GET_BDA(mem_size_kb)*1024;
235 add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
236
237 // Give back unused high ram.
238 struct allocinfo_s *info = findLast(&ZoneHigh);
239 if (info) {
240 u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
241 add_e820((u32)info->dataend, giveback, E820_RAM);
242 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
243 }
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400244}
245
246
247/****************************************************************
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400248 * ebda movement
249 ****************************************************************/
250
251// Move ebda
252static int
253relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
254{
255 u32 lowram = GET_BDA(mem_size_kb) * 1024;
256 if (oldebda != lowram)
257 // EBDA isn't at end of ram - give up.
258 return -1;
259
Kevin O'Connor8f59aa32010-06-06 16:11:45 -0400260 // Do copy (this assumes memcpy copies forward - otherwise memmove
261 // is needed)
262 memcpy_fl((void*)newebda, (void*)oldebda, ebda_size * 1024);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400263
264 // Update indexes
265 dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
266 SET_BDA(mem_size_kb, newebda / 1024);
267 SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
268 return 0;
269}
270
271// Support expanding the ZoneLow dynamically.
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400272static void
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400273zonelow_expand(u32 size, u32 align)
274{
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400275 struct allocinfo_s *info = findLast(&ZoneLow);
276 if (!info)
277 return;
278 u32 oldpos = (u32)GET_PMMVAR(info->allocend);
279 u32 newpos = ALIGN_DOWN(oldpos - size, align);
280 u32 bottom = (u32)GET_PMMVAR(info->dataend);
281 if (newpos >= bottom && newpos <= oldpos)
282 // Space already present.
283 return;
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400284 u16 ebda_seg = get_ebda_seg();
285 u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400286 u8 ebda_size = GET_EBDA2(ebda_seg, size);
287 u32 ebda_end = ebda_pos + ebda_size * 1024;
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400288 if (ebda_end != bottom)
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400289 // Something else is after ebda - can't use any existing space.
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400290 newpos = ALIGN_DOWN(ebda_end - size, align);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400291 u32 newbottom = ALIGN_DOWN(newpos, 1024);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400292 u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
293 if (newebda < BUILD_EBDA_MINIMUM)
294 // Not enough space.
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400295 return;
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400296
297 // Move ebda
298 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
299 if (ret)
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400300 return;
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400301
302 // Update zone
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400303 if (ebda_end == bottom) {
304 SET_PMMVAR(info->data, (void*)newbottom);
305 SET_PMMVAR(info->dataend, (void*)newbottom);
306 } else
307 addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400308}
309
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400310// Check if can expand the given zone to fulfill an allocation
311static void *
312allocExpandSpace(struct zone_s *zone, u32 size, u32 align
313 , struct allocinfo_s *fill)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400314{
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400315 void *data = allocSpace(zone, size, align, fill);
316 if (data || zone != &ZoneLow)
317 return data;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400318
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400319 // Make sure to not move ebda while an optionrom is running.
320 if (unlikely(wait_preempt())) {
321 data = allocSpace(zone, size, align, fill);
322 if (data)
323 return data;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400324 }
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400325
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400326 zonelow_expand(size, align);
327 return allocSpace(zone, size, align, fill);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400328}
329
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400330
331/****************************************************************
Kevin O'Connor8f4409b2009-11-25 18:51:46 -0500332 * tracked memory allocations
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400333 ****************************************************************/
334
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400335// Allocate memory from the given zone and track it as a PMM allocation
Kevin O'Connorf9a774c2010-04-17 16:58:32 -0400336void * __malloc
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400337pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
338{
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400339 if (!size)
340 return NULL;
341
342 // Find and reserve space for bookkeeping.
343 struct allocdetail_s *detail = allocSpace(
344 &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
345 if (!detail) {
346 detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
347 , MALLOC_MIN_ALIGN, NULL);
348 if (!detail)
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400349 return NULL;
350 }
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400351
352 // Find and reserve space for main allocation
353 void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
354 if (!data) {
355 freeSpace(&detail->detailinfo);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400356 return NULL;
357 }
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400358
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400359 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
360 " ret=%p (detail=%p)\n"
361 , zone, handle, size, align
362 , data, detail);
363 SET_PMMVAR(detail->handle, handle);
364
365 return data;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400366}
367
368// Free a data block allocated with pmm_malloc
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400369int
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400370pmm_free(void *data)
371{
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400372 struct allocinfo_s *info = findAlloc(data);
373 if (!info || data == (void*)info || data == GET_PMMVAR(info->dataend))
374 return -1;
375 struct allocdetail_s *detail = container_of(
376 info, struct allocdetail_s, datainfo);
377 dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
378 freeSpace(info);
379 freeSpace(&detail->detailinfo);
380 return 0;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400381}
382
383// Find the amount of free space in a given zone.
384static u32
385pmm_getspace(struct zone_s *zone)
386{
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400387 // XXX - doesn't account for ZoneLow being able to grow.
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400388 // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
389 u32 maxspace = 0;
390 struct allocinfo_s *info;
391 for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next)) {
392 u32 space = GET_PMMVAR(info->allocend) - GET_PMMVAR(info->dataend);
393 if (space > maxspace)
394 maxspace = space;
395 }
396
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400397 if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400398 return maxspace;
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400399 // Account for space needed for PMM tracking.
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400400 u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
401 if (maxspace <= reserve)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400402 return 0;
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400403 return maxspace - reserve;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400404}
405
406// Find the data block allocated with pmm_malloc with a given handle.
407static void *
408pmm_find(u32 handle)
409{
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400410 int i;
411 for (i=0; i<ARRAY_SIZE(Zones); i++) {
412 struct zone_s *zone = GET_PMMVAR(Zones[i]);
413 struct allocinfo_s *info;
414 for (info = GET_PMMVAR(zone->info); info
415 ; info = GET_PMMVAR(info->next)) {
416 if (GET_PMMVAR(info->data) != (void*)info)
417 continue;
418 struct allocdetail_s *detail = container_of(
419 info, struct allocdetail_s, detailinfo);
420 if (GET_PMMVAR(detail->handle) == handle)
421 return GET_PMMVAR(detail->datainfo.data);
422 }
Kevin O'Connor8f4409b2009-11-25 18:51:46 -0500423 }
Kevin O'Connor42a1d4c2010-06-06 11:10:24 -0400424 return NULL;
Kevin O'Connor8f4409b2009-11-25 18:51:46 -0500425}
426
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400427
428/****************************************************************
429 * pmm interface
430 ****************************************************************/
Kevin O'Connore54ee382009-07-26 19:33:13 -0400431
432struct pmmheader {
433 u32 signature;
434 u8 version;
435 u8 length;
436 u8 checksum;
437 u16 entry_offset;
438 u16 entry_seg;
439 u8 reserved[5];
440} PACKED;
441
442extern struct pmmheader PMMHEADER;
443
444#define PMM_SIGNATURE 0x4d4d5024 // $PMM
445
446#if CONFIG_PMM
447struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
448 .version = 0x01,
449 .length = sizeof(PMMHEADER),
450 .entry_seg = SEG_BIOS,
451};
452#endif
453
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400454#define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
Kevin O'Connore54ee382009-07-26 19:33:13 -0400455
456// PMM - allocate
457static u32
458handle_pmm00(u16 *args)
459{
460 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
461 u16 flags = args[5];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400462 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
Kevin O'Connore54ee382009-07-26 19:33:13 -0400463 , length, handle, flags);
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400464 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
465 if (flags & 8) {
466 // Permanent memory request.
467 lowzone = &ZoneLow;
468 highzone = &ZoneHigh;
469 }
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400470 if (!length) {
471 // Memory size request
472 switch (flags & 3) {
473 default:
474 case 0:
475 return 0;
476 case 1:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400477 return pmm_getspace(lowzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400478 case 2:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400479 return pmm_getspace(highzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400480 case 3: {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400481 u32 spacelow = pmm_getspace(lowzone);
482 u32 spacehigh = pmm_getspace(highzone);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400483 if (spacelow > spacehigh)
484 return spacelow;
485 return spacehigh;
486 }
487 }
488 }
489 u32 size = length * 16;
490 if ((s32)size <= 0)
491 return 0;
492 u32 align = MALLOC_MIN_ALIGN;
493 if (flags & 4) {
494 align = 1<<__ffs(size);
495 if (align < MALLOC_MIN_ALIGN)
496 align = MALLOC_MIN_ALIGN;
497 }
498 switch (flags & 3) {
499 default:
500 case 0:
501 return 0;
502 case 1:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400503 return (u32)pmm_malloc(lowzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400504 case 2:
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400505 return (u32)pmm_malloc(highzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400506 case 3: {
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400507 void *data = pmm_malloc(lowzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400508 if (data)
509 return (u32)data;
Kevin O'Connor0e9fd612009-08-22 21:31:58 -0400510 return (u32)pmm_malloc(highzone, handle, size, align);
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400511 }
512 }
Kevin O'Connore54ee382009-07-26 19:33:13 -0400513}
514
515// PMM - find
516static u32
517handle_pmm01(u16 *args)
518{
519 u32 handle = *(u32*)&args[1];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400520 dprintf(3, "pmm01: handle=%x\n", handle);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400521 if (handle == PMM_DEFAULT_HANDLE)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400522 return 0;
523 return (u32)pmm_find(handle);
Kevin O'Connore54ee382009-07-26 19:33:13 -0400524}
525
526// PMM - deallocate
527static u32
528handle_pmm02(u16 *args)
529{
530 u32 buffer = *(u32*)&args[1];
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400531 dprintf(3, "pmm02: buffer=%x\n", buffer);
532 int ret = pmm_free((void*)buffer);
533 if (ret)
534 // Error
535 return 1;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400536 return 0;
537}
538
539static u32
540handle_pmmXX(u16 *args)
541{
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400542 return PMM_FUNCTION_NOT_SUPPORTED;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400543}
544
545u32 VISIBLE16
546handle_pmm(u16 *args)
547{
548 if (! CONFIG_PMM)
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400549 return PMM_FUNCTION_NOT_SUPPORTED;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400550
551 u16 arg1 = args[0];
552 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
553
554 switch (arg1) {
555 case 0x00: return handle_pmm00(args);
556 case 0x01: return handle_pmm01(args);
557 case 0x02: return handle_pmm02(args);
558 default: return handle_pmmXX(args);
559 }
560}
561
562// romlayout.S
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500563extern void entry_pmm(void);
Kevin O'Connore54ee382009-07-26 19:33:13 -0400564
565void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500566pmm_setup(void)
Kevin O'Connore54ee382009-07-26 19:33:13 -0400567{
568 if (! CONFIG_PMM)
569 return;
570
571 dprintf(3, "init PMM\n");
572
573 PMMHEADER.signature = PMM_SIGNATURE;
574 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
575 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
576}
577
578void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500579pmm_finalize(void)
Kevin O'Connore54ee382009-07-26 19:33:13 -0400580{
581 if (! CONFIG_PMM)
582 return;
583
584 dprintf(3, "finalize PMM\n");
585
586 PMMHEADER.signature = 0;
587 PMMHEADER.entry_offset = 0;
Kevin O'Connore54ee382009-07-26 19:33:13 -0400588}