ia64/xen-unstable

changeset 19078:8de0aae803e0

hvm bios: add PMM (a memory manager during POST)

The PMM (POST Memory Manager) offers malloc/free functionality
for PCI option ROMs during POST (Power On Self Test).

This patch adds a PMM functionality to the guest BIOS.

For example, the option ROM on LSI Logic SAS card uses PMM and
failed to initialize the device without PMM. Thus, the HVM can't
boot up directly from the passthroughed SCSI disk.
gPXE also uses PMM (I don't know what happens without PMM).

With this patch, we succeeded in SAS boot of HVM.

For further information about PMM:
http://www.phoenix.com/en/OEM-ODM/Customer+Services/White+Papers-Specs/PC+Industry+Specifications.htm
http://www.phoenix.com/NR/rdonlyres/873A00CF-33AC-4775-B77E-08E7B9754993/0/specspmm101.pdf

Signed-off-by: Kouya Shimura <kouya@jp.fujitsu.com>
Signed-off-by: Akio Takebe <takebe_akio@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jan 23 12:50:14 2009 +0000 (2009-01-23)
parents ced1c3069ada
children f3240cd3cd2b
files tools/firmware/rombios/32bit/Makefile tools/firmware/rombios/32bit/pmm.c tools/firmware/rombios/32bitprotos.h tools/firmware/rombios/rombios.c
line diff
     1.1 --- a/tools/firmware/rombios/32bit/Makefile	Fri Jan 23 11:51:43 2009 +0000
     1.2 +++ b/tools/firmware/rombios/32bit/Makefile	Fri Jan 23 12:50:14 2009 +0000
     1.3 @@ -18,7 +18,7 @@ all: subdirs-all
     1.4  clean: subdirs-clean
     1.5  	rm -rf *.o $(TARGET) $(DEPS)
     1.6  
     1.7 -$(TARGET): 32bitbios.o $(MODULES) util.o
     1.8 +$(TARGET): 32bitbios.o $(MODULES) util.o pmm.o
     1.9  	$(LD) $(LDFLAGS_DIRECT) -s -r $^ -o 32bitbios_all.o
    1.10  	@nm 32bitbios_all.o |                                \
    1.11  	  egrep '^ +U ' >/dev/null && {                      \
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tools/firmware/rombios/32bit/pmm.c	Fri Jan 23 12:50:14 2009 +0000
     2.3 @@ -0,0 +1,531 @@
     2.4 +/*
     2.5 + *  pmm.c - POST(Power On Self Test) Memory Manager
     2.6 + *  according to the specification described in
     2.7 + *  http://www.phoenix.com/NR/rdonlyres/873A00CF-33AC-4775-B77E-08E7B9754993/0/specspmm101.pdf
     2.8 + *
     2.9 + *  This library is free software; you can redistribute it and/or
    2.10 + *  modify it under the terms of the GNU Lesser General Public
    2.11 + *  License as published by the Free Software Foundation; either
    2.12 + *  version 2 of the License, or (at your option) any later version.
    2.13 + *
    2.14 + *  This library is distributed in the hope that it will be useful,
    2.15 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.16 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.17 + *  Lesser General Public License for more details.
    2.18 + *
    2.19 + *  You should have received a copy of the GNU Lesser General Public
    2.20 + *  License along with this library; if not, write to the Free Software
    2.21 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    2.22 + *
    2.23 + *  Copyright (C) 2009 FUJITSU LIMITED
    2.24 + *
    2.25 + *  Author: Kouya Shimura <kouya@jp.fujitsu.com>
    2.26 + */
    2.27 +
    2.28 +/*
    2.29 + * Algorithm:
    2.30 + *
    2.31 + * This is not a fast storage allocator but simple one.  There is no
    2.32 + * segregated management by block size and it does nothing special for
    2.33 + * avoiding the fragmentation.
    2.34 + *
    2.35 + * The allocation algorithm is a first-fit. All memory blocks are
    2.36 + * managed by linear single linked list in order of the address.
    2.37 + * (i.e. There is no backward pointer) It searches the first available
    2.38 + * equal or larger block from the head (lowest address) of memory
    2.39 + * heap. The larger block is splitted into two blocks unless one side
    2.40 + * becomes too small.
    2.41 + * 
    2.42 + * For de-allocation, the specified block is just marked as available
    2.43 + * and it does nothing else. Thus, the fragmentation will occur. The
    2.44 + * collection of continuous available blocks are done on the search
    2.45 + * phase of another block allocation.
    2.46 + *
    2.47 + * The following is an abstract of this algorithm. The actual code
    2.48 + * looks complicated on account of alignment and checking the handle.
    2.49 + *
    2.50 + *     static memblk_t *
    2.51 + *     alloc(heap_t *heap, uint32_t size)
    2.52 + *     {
    2.53 + *         static memblk_t *mb;
    2.54 + *         for_each_memblk(heap, mb) // search memory blocks
    2.55 + *             if (memblk_is_avail(mb))
    2.56 + *             {
    2.57 + *                 collect_avail_memblks(heap, mb);
    2.58 + *                 if (size <= memblk_bufsize(mb))
    2.59 + *                 {
    2.60 + *                     split_memblk(mb, size);
    2.61 + *                     set_inuse(mb);
    2.62 + *                     return mb;
    2.63 + *                 }
    2.64 + *             }
    2.65 + *         return NULL;
    2.66 + *     }
    2.67 + */
    2.68 +
    2.69 +#include <stdint.h>
    2.70 +#include <stddef.h>
    2.71 +#include <../hvmloader/config.h>
    2.72 +#include <../hvmloader/e820.h>
    2.73 +#include "util.h"
    2.74 +
    2.75 +#define DEBUG_PMM 0
    2.76 +
    2.77 +#define ASSERT(_expr, _action)                                  \
    2.78 +    if (!(_expr)) {                                             \
    2.79 +        printf("ASSERTION FAIL: %s %s:%d %s()\n",               \
    2.80 +               __STRING(_expr), __FILE__, __LINE__, __func__);  \
    2.81 +        _action;                                                \
    2.82 +    } else
    2.83 +
    2.84 +#if DEBUG_PMM
    2.85 +# define PMM_DEBUG(format, p...) printf("PMM " format, ##p)
    2.86 +#else
    2.87 +# define PMM_DEBUG(format, p...)
    2.88 +#endif
    2.89 +
    2.90 +struct pmmAllocArgs {
    2.91 +    uint16_t function;
    2.92 +    uint32_t length;
    2.93 +    uint32_t handle;
    2.94 +    uint16_t flags;
    2.95 +} __attribute__ ((packed));
    2.96 +
    2.97 +struct pmmFindArgs {
    2.98 +    uint16_t function;
    2.99 +    uint32_t handle;
   2.100 +} __attribute__ ((packed));
   2.101 +
   2.102 +struct pmmDeallocateArgs {
   2.103 +    uint16_t function;
   2.104 +    uint32_t buffer;
   2.105 +} __attribute__ ((packed));
   2.106 +
   2.107 +#define PMM_FUNCTION_ALLOCATE   0
   2.108 +#define PMM_FUNCTION_FIND       1         
   2.109 +#define PMM_FUNCTION_DEALLOC    2
   2.110 +
   2.111 +#define PARAGRAPH_LENGTH        16  // unit of length
   2.112 +
   2.113 +#define PMM_HANDLE_ANONYMOUS    0xffffffff
   2.114 +
   2.115 +#define PMM_FLAGS_MEMORY_TYPE_MASK      0x0003
   2.116 +#define PMM_FLAGS_MEMORY_INVALID        0
   2.117 +#define PMM_FLAGS_MEMORY_CONVENTIONAL   1  // 0 to 1MB
   2.118 +#define PMM_FLAGS_MEMORY_EXTENDED       2  // 1MB to 4GB
   2.119 +#define PMM_FLAGS_MEMORY_ANY            3  // whichever is available
   2.120 +#define PMM_FLAGS_ALIGINMENT            0x0004
   2.121 +
   2.122 +/* Error code */
   2.123 +#define PMM_ENOMEM      (0)     // Out of memory, duplicate handle
   2.124 +#define PMM_EINVAL      (-1)    // Invalid argument
   2.125 +
   2.126 +#define ALIGN_UP(addr, size)    (((addr)+((size)-1))&(~((size)-1)))
   2.127 +#define ALIGN_DOWN(addr, size)  ((addr)&(~((size)-1)))
   2.128 +
   2.129 +typedef struct memblk {
   2.130 +    uint32_t magic;      // inuse or available
   2.131 +    struct memblk *next; // points the very next of this memblk
   2.132 +    uint32_t handle;     // identifier of this block
   2.133 +    uint32_t __fill;     // for 16byte alignment, not used
   2.134 +    uint8_t buffer[0];
   2.135 +} memblk_t;
   2.136 +
   2.137 +typedef struct heap {
   2.138 +    memblk_t *head;     // start address of heap
   2.139 +    memblk_t *end;      // end address of heap
   2.140 +} heap_t;
   2.141 +
   2.142 +#define HEAP_NOT_INITIALIZED    (memblk_t *)-1
   2.143 +#define HEAP_ALIGNMENT          16
   2.144 +
   2.145 +/*
   2.146 + * PMM handles two memory heaps, the caller chooses either.
   2.147 + *
   2.148 + * - conventional memroy (below 1MB)
   2.149 + *    In HVM, the area is fixed. 0x00010000-0x0007FFFF
   2.150 + *    (from SCRATCH_PHYSICAL_ADDRESS to HYPERCALL_PHYSICAL_ADDRESS)
   2.151 + *
   2.152 + * - extended memory (start at 1MB, below 4GB)
   2.153 + *    In HVM, the area starts at memory address 0x00100000.
   2.154 + *    The end address is variable. We read low RAM address from e820 table.
   2.155 + *
   2.156 + * The following struct must be located in the data segment since bss
   2.157 + * in 32bitbios doesn't be relocated.
   2.158 + */
   2.159 +static struct {
   2.160 +    heap_t heap;     // conventional memory
   2.161 +    heap_t ext_heap; // extended memory
   2.162 +} pmm_data = { {HEAP_NOT_INITIALIZED, NULL}, {NULL, NULL} };
   2.163 +
   2.164 +/* These values are private use, not a spec in PMM */
   2.165 +#define MEMBLK_MAGIC_INUSE   0x2A4D4D50  // 'PMM*'
   2.166 +#define MEMBLK_MAGIC_AVAIL   0x5F4D4D50  // 'PMM_'
   2.167 +
   2.168 +#define memblk_is_inuse(_mb)  ((_mb)->magic == MEMBLK_MAGIC_INUSE)
   2.169 +#define memblk_is_avail(_mb)  ((_mb)->magic == MEMBLK_MAGIC_AVAIL)
   2.170 +
   2.171 +static void set_inuse(memblk_t *mb, uint32_t handle)
   2.172 +{
   2.173 +    mb->magic = MEMBLK_MAGIC_INUSE;
   2.174 +    mb->handle = handle;
   2.175 +}
   2.176 +
   2.177 +static void set_avail(memblk_t *mb)
   2.178 +{
   2.179 +    mb->magic = MEMBLK_MAGIC_AVAIL;
   2.180 +    mb->handle = PMM_HANDLE_ANONYMOUS;
   2.181 +}
   2.182 +
   2.183 +#define MEMBLK_HEADER_SIZE   ((int)(&((memblk_t *)0)->buffer))
   2.184 +#define MIN_MEMBLK_SIZE      (MEMBLK_HEADER_SIZE + PARAGRAPH_LENGTH)
   2.185 +
   2.186 +#define memblk_size(_mb)     ((void *)((_mb)->next) - (void *)(_mb))
   2.187 +#define memblk_buffer(_mb)   ((uint32_t)(&(_mb)->buffer))
   2.188 +#define memblk_bufsize(_mb)  (memblk_size(_mb) - MEMBLK_HEADER_SIZE)
   2.189 +
   2.190 +#define buffer_memblk(_buf)  (memblk_t *)((_buf) - MEMBLK_HEADER_SIZE)
   2.191 +
   2.192 +#define memblk_loop_mbondition(_h, _mb) \
   2.193 +    (((_mb) < (_h)->end) && (/* avoid infinite loop */ (_mb) < (_mb)->next))
   2.194 +
   2.195 +#define for_each_memblk(_h, _mb)                \
   2.196 +    for ((_mb) = (_h)->head;                    \
   2.197 +         memblk_loop_mbondition(_h, _mb);       \
   2.198 +         (_mb) = (_mb)->next)
   2.199 +
   2.200 +#define for_remain_memblk(_h, _mb)              \
   2.201 +    for (;                                      \
   2.202 +         memblk_loop_mbondition(_h, _mb);       \
   2.203 +         (_mb) = (_mb)->next)
   2.204 +
   2.205 +/*
   2.206 + *                                       <-size->
   2.207 + *    +==================+======+       +========+========+======+
   2.208 + *    |      avail       |      |       | avail  | avail  |      |
   2.209 + *    |      memblk      |memblk|...    | memblk | memblk |memblk|...
   2.210 + *    +==================+======+   =>  +========+========+======+
   2.211 + *    ^ |                ^ |    ^         |      ^ |      ^ |    ^
   2.212 + *    | |next            | |next|         |next  | |next  | |next|
   2.213 + *    | \________________/ \____/         \______/ \______/ \____/
   2.214 + *    |                                          ^
   2.215 + *    |                                          |
   2.216 + *    mb                                         +- sb(return value)
   2.217 + */
   2.218 +static memblk_t *
   2.219 +split_memblk(memblk_t *mb, uint32_t size)
   2.220 +{
   2.221 +    memblk_t *sb = (void *)memblk_buffer(mb) + size;
   2.222 +
   2.223 +    /* Only split if the remaining fragment is big enough. */
   2.224 +    if ( (memblk_bufsize(mb) - size) < MIN_MEMBLK_SIZE)
   2.225 +        return mb;
   2.226 +
   2.227 +    sb->next = mb->next;
   2.228 +    set_avail(sb);
   2.229 +
   2.230 +    mb->next = sb;
   2.231 +    return sb;
   2.232 +}
   2.233 +
   2.234 +/*
   2.235 + *    +======+======+======+======+       +=================+======+
   2.236 + *    |avail |avail |avail |inuse |       |      avail      |inuse |   
   2.237 + *    |memblk|memblk|memblk|memblk|...    |      memblk     |memblk|...
   2.238 + *    +======+======+======+======+   =>  +=================+======+
   2.239 + *    ^ |    ^ |    ^ |    ^ |    ^         |               ^ |    ^
   2.240 + *    | |next| |next| |next| |next|         |next           | |next|
   2.241 + *    | \____/ \____/ \____/ \____/         \_______________/ \____/
   2.242 + *    |
   2.243 + *    mb
   2.244 + */
   2.245 +static void
   2.246 +collect_avail_memblks(heap_t *heap, memblk_t *mb)
   2.247 +{
   2.248 +    memblk_t *nb = mb->next;
   2.249 +
   2.250 +    for_remain_memblk ( heap, nb )
   2.251 +        if ( memblk_is_inuse(nb) )
   2.252 +            break;
   2.253 +    mb->next = nb;
   2.254 +}
   2.255 +
   2.256 +static void
   2.257 +pmm_init_heap(heap_t *heap, uint32_t from_addr, uint32_t to_addr)
   2.258 +{
   2.259 +    memblk_t *mb = (memblk_t *)ALIGN_UP(from_addr, HEAP_ALIGNMENT);
   2.260 +
   2.261 +    mb->next = (memblk_t *)ALIGN_DOWN(to_addr, HEAP_ALIGNMENT);
   2.262 +    set_avail(mb);
   2.263 +
   2.264 +    heap->head = mb;
   2.265 +    heap->end = mb->next;
   2.266 +}
   2.267 +
   2.268 +static void
   2.269 +pmm_initalize(void)
   2.270 +{
   2.271 +    int i, e820_nr = *E820_NR;
   2.272 +    struct e820entry *e820 = E820;
   2.273 +
   2.274 +    /* Extended memory: RAM below 4GB, 0x100000-0xXXXXXXXX */
   2.275 +    for ( i = 0; i < e820_nr; i++ )
   2.276 +    {
   2.277 +        if ( (e820[i].type == E820_RAM) && (e820[i].addr >= 0x00100000) )
   2.278 +        {
   2.279 +            pmm_init_heap(&pmm_data.ext_heap, e820[i].addr, 
   2.280 +                          e820[i].addr + e820[i].size);
   2.281 +            break;
   2.282 +        }
   2.283 +    }
   2.284 +
   2.285 +    /* convectional memory: RAM below 1MB, 0x10000-0x7FFFF */
   2.286 +    pmm_init_heap(&pmm_data.heap, SCRATCH_PHYSICAL_ADDRESS,
   2.287 +                  HYPERCALL_PHYSICAL_ADDRESS);
   2.288 +}
   2.289 +
   2.290 +static uint32_t
   2.291 +pmm_max_avail_length(heap_t *heap)
   2.292 +{
   2.293 +    memblk_t *mb;
   2.294 +    uint32_t size, max = 0;
   2.295 +
   2.296 +    for_each_memblk ( heap, mb )
   2.297 +    {
   2.298 +        if ( !memblk_is_avail(mb) )
   2.299 +            continue;
   2.300 +        collect_avail_memblks(heap, mb);
   2.301 +        size = memblk_bufsize(mb);
   2.302 +        if ( size > max )
   2.303 +            max = size;
   2.304 +    }
   2.305 +
   2.306 +    return (max / PARAGRAPH_LENGTH);
   2.307 +}
   2.308 +
   2.309 +static memblk_t *
   2.310 +first_fit(heap_t *heap, uint32_t size, uint32_t handle, uint32_t flags)
   2.311 +{
   2.312 +    memblk_t *mb;
   2.313 +    int32_t align = 0;
   2.314 +
   2.315 +    if ( flags & PMM_FLAGS_ALIGINMENT )
   2.316 +        align = ((size ^ (size - 1)) >> 1) + 1;
   2.317 +
   2.318 +    for_each_memblk ( heap, mb )
   2.319 +    {
   2.320 +        if ( memblk_is_avail(mb) )
   2.321 +        {
   2.322 +            collect_avail_memblks(heap, mb);
   2.323 +
   2.324 +            if ( align )
   2.325 +            {
   2.326 +                uint32_t addr = memblk_buffer(mb);
   2.327 +                uint32_t offset = ALIGN_UP(addr, align) - addr;
   2.328 +
   2.329 +                if ( offset > 0 )
   2.330 +                {
   2.331 +                    ASSERT(offset >= MEMBLK_HEADER_SIZE, continue);
   2.332 +
   2.333 +                    if ( (offset + size) > memblk_bufsize(mb) )
   2.334 +                        continue;
   2.335 +
   2.336 +                    mb = split_memblk(mb, offset - MEMBLK_HEADER_SIZE);
   2.337 +                    return mb;
   2.338 +                }
   2.339 +            }
   2.340 +
   2.341 +            if ( size <= memblk_bufsize(mb) )
   2.342 +                return mb;
   2.343 +        }
   2.344 +        else
   2.345 +        {
   2.346 +            ASSERT(memblk_is_inuse(mb), return NULL);
   2.347 +
   2.348 +            /* Duplication check for handle. */
   2.349 +            if ( (handle != PMM_HANDLE_ANONYMOUS) && (mb->handle == handle) )
   2.350 +                return NULL;
   2.351 +        }
   2.352 +    }
   2.353 +
   2.354 +    return NULL;
   2.355 +}
   2.356 +
   2.357 +static memblk_t *
   2.358 +pmm_find_handle(heap_t *heap, uint32_t handle)
   2.359 +{
   2.360 +    memblk_t *mb;
   2.361 +
   2.362 +    if ( handle == PMM_HANDLE_ANONYMOUS )
   2.363 +        return NULL;
   2.364 +
   2.365 +    for_each_memblk ( heap, mb )
   2.366 +        if ( mb->handle == handle )
   2.367 +            return mb;
   2.368 +
   2.369 +    return NULL;
   2.370 +}
   2.371 +
   2.372 +/*
   2.373 + * allocate a memory block of the specified type and size, and returns
   2.374 + * the address of the memory block.
   2.375 + *
   2.376 + * A client-specified identifier to be associated with the allocated
   2.377 + * memory block. A handle of 0xFFFFFFFF indicates that no identifier
   2.378 + * should be associated with the block. Such a memory block is known
   2.379 + * as an "anonymous" memory block and cannot be found using the
   2.380 + * pmmFind function. If a specified handle for a requested memory
   2.381 + * block is already used in a currently allocated memory block, the
   2.382 + * error value of 0x00000000 is returned
   2.383 + *
   2.384 + * If length is 0x00000000, no memory is allocated and the value
   2.385 + * returned is the size of the largest memory block available for the
   2.386 + * memory type specified in the flags parameter. The alignment bit in
   2.387 + * the flags register is ignored when calculating the largest memory
   2.388 + * block available.
   2.389 + *
   2.390 + * If a specified handle for a requested memory block is already used
   2.391 + * in a currently allocated memory block, the error value of
   2.392 + * 0x00000000 is returned.
   2.393 + * 
   2.394 + * A return value of 0x00000000 indicates that an error occurred and
   2.395 + * no memory has been allocated. 
   2.396 + */
   2.397 +static uint32_t
   2.398 +pmmAllocate(uint32_t length, uint32_t handle, uint16_t flags)
   2.399 +{
   2.400 +    heap_t *heap;
   2.401 +    memblk_t *mb;
   2.402 +    uint32_t size;
   2.403 +
   2.404 +    switch ( flags & PMM_FLAGS_MEMORY_TYPE_MASK )
   2.405 +    {
   2.406 +    case PMM_FLAGS_MEMORY_CONVENTIONAL:
   2.407 +        heap = &pmm_data.heap;
   2.408 +        break;
   2.409 +
   2.410 +    case PMM_FLAGS_MEMORY_EXTENDED:
   2.411 +    case PMM_FLAGS_MEMORY_ANY: /* XXX: ignore conventional memory for now */
   2.412 +        heap = &pmm_data.ext_heap;
   2.413 +        break;
   2.414 +
   2.415 +    default:
   2.416 +        return PMM_EINVAL;
   2.417 +    }
   2.418 +
   2.419 +    /* return the largest memory block available */
   2.420 +    if ( length == 0 )
   2.421 +        return pmm_max_avail_length(heap);
   2.422 +
   2.423 +    size = length * PARAGRAPH_LENGTH;
   2.424 +    mb = first_fit(heap, size, handle, flags);
   2.425 +
   2.426 +    if ( mb == NULL )
   2.427 +        return PMM_ENOMEM;
   2.428 +
   2.429 +    /* duplication check for handle */
   2.430 +    if ( handle != PMM_HANDLE_ANONYMOUS )
   2.431 +    {
   2.432 +        memblk_t *nb = mb->next;
   2.433 +
   2.434 +        for_remain_memblk(heap, nb)
   2.435 +            if (nb->handle == handle)
   2.436 +                return PMM_ENOMEM;
   2.437 +    }
   2.438 +
   2.439 +    split_memblk(mb, size);
   2.440 +    set_inuse(mb, handle);
   2.441 +
   2.442 +    return memblk_buffer(mb);
   2.443 +}
   2.444 +
   2.445 +/*
   2.446 + * returns the address of the memory block associated with the
   2.447 + * specified handle.  
   2.448 + *
   2.449 + * A return value of 0x00000000 indicates that the handle does not
   2.450 + * correspond to a currently allocated memory block.
   2.451 + */
   2.452 +static uint32_t
   2.453 +pmmFind(uint32_t handle)
   2.454 +{
   2.455 +    memblk_t *mb;
   2.456 +
   2.457 +    if ( handle == PMM_HANDLE_ANONYMOUS )
   2.458 +        return 0;
   2.459 +
   2.460 +    mb = pmm_find_handle(&pmm_data.heap, handle);
   2.461 +    if ( mb == NULL )
   2.462 +        mb = pmm_find_handle(&pmm_data.ext_heap, handle);
   2.463 +
   2.464 +    return mb ? memblk_buffer(mb) : 0;
   2.465 +}
   2.466 +
   2.467 +/* 
   2.468 + * frees the specified memory block that was previously allocated by
   2.469 + * pmmAllocate.
   2.470 + *
   2.471 + * If the memory block was deallocated correctly, the return value is
   2.472 + * 0x00000000. If there was an error, the return value is non-zero.
   2.473 + */
   2.474 +static uint32_t
   2.475 +pmmDeallocate(uint32_t buffer)
   2.476 +{
   2.477 +    memblk_t *mb = buffer_memblk(buffer);
   2.478 +
   2.479 +    if ( !memblk_is_inuse(mb) )
   2.480 +        return PMM_EINVAL;
   2.481 +
   2.482 +    set_avail(mb);
   2.483 +    return 0;
   2.484 +}
   2.485 +
   2.486 +
   2.487 +union pmm_args {
   2.488 +    uint16_t function;
   2.489 +    struct pmmAllocArgs alloc;
   2.490 +    struct pmmFindArgs find;
   2.491 +    struct pmmDeallocateArgs dealloc;
   2.492 +} __attribute__ ((packed));
   2.493 +
   2.494 +/*
   2.495 + * entry function of all PMM services.
   2.496 + *
   2.497 + * Values returned to the caller are placed in the DX:AX register
   2.498 + * pair. The flags and all registers, other than DX and AX, are
   2.499 + * preserved across calls to PMM services.
   2.500 + */
   2.501 +uint32_t
   2.502 +pmm(void *argp)
   2.503 +{
   2.504 +    union pmm_args *ap = argp;
   2.505 +    uint32_t ret = PMM_EINVAL;
   2.506 +
   2.507 +    if ( pmm_data.heap.head == HEAP_NOT_INITIALIZED )
   2.508 +        pmm_initalize();
   2.509 +
   2.510 +    switch ( ap->function )
   2.511 +    {
   2.512 +    case PMM_FUNCTION_ALLOCATE:
   2.513 +        ret = pmmAllocate(ap->alloc.length, ap->alloc.handle, ap->alloc.flags);
   2.514 +        PMM_DEBUG("Alloc length=%x handle=%x flags=%x ret=%x\n", 
   2.515 +                  ap->alloc.length, ap->alloc.handle, ap->alloc.flags, ret);
   2.516 +        break;
   2.517 +
   2.518 +    case PMM_FUNCTION_FIND:
   2.519 +        ret = pmmFind(ap->find.handle);
   2.520 +        PMM_DEBUG("Find handle=%x ret=%x\n", ap->find.handle, ret);
   2.521 +        break;
   2.522 +
   2.523 +    case PMM_FUNCTION_DEALLOC:
   2.524 +        ret = pmmDeallocate(ap->dealloc.buffer);
   2.525 +        PMM_DEBUG("Dealloc buffer=%x ret=%x\n", ap->dealloc.buffer, ret);
   2.526 +        break;
   2.527 +
   2.528 +    default:
   2.529 +        PMM_DEBUG("Invalid function:%d\n", ap->function);
   2.530 +        break;
   2.531 +    }
   2.532 +
   2.533 +    return ret;
   2.534 +}
     3.1 --- a/tools/firmware/rombios/32bitprotos.h	Fri Jan 23 11:51:43 2009 +0000
     3.2 +++ b/tools/firmware/rombios/32bitprotos.h	Fri Jan 23 12:50:14 2009 +0000
     3.3 @@ -13,3 +13,4 @@ X(10, void,   tcpa_ipl, Bit32u bootcd, B
     3.4  X(11, void,   tcpa_measure_post, Bit32u from, Bit32u to)
     3.5  X(12, Bit32u, tcpa_initialize_tpm, Bit32u physpres)
     3.6  X(13, Bit32u, get_s3_waking_vector, void)
     3.7 +X(14, Bit32u, pmm, void *argp)
     4.1 --- a/tools/firmware/rombios/rombios.c	Fri Jan 23 11:51:43 2009 +0000
     4.2 +++ b/tools/firmware/rombios/rombios.c	Fri Jan 23 12:50:14 2009 +0000
     4.3 @@ -161,6 +161,8 @@
     4.4  
     4.5  #define BX_TCGBIOS       0   /* main switch for TCG BIOS ext. */
     4.6  
     4.7 +#define BX_PMM           1   /* POST Memory Manager */
     4.8 +
     4.9  #define BX_MAX_ATA_INTERFACES   4
    4.10  #define BX_MAX_ATA_DEVICES      (BX_MAX_ATA_INTERFACES*2)
    4.11  
    4.12 @@ -2054,7 +2056,10 @@ print_bios_banner()
    4.13    "rombios32 "
    4.14  #endif
    4.15  #if BX_TCGBIOS
    4.16 -  "TCG-enabled"
    4.17 +  "TCG-enabled "
    4.18 +#endif
    4.19 +#if BX_PMM
    4.20 +  "PMM "
    4.21  #endif
    4.22    "\n\n");
    4.23  }
    4.24 @@ -10356,6 +10361,32 @@ rombios32_gdt:
    4.25    dw 0xffff, 0, 0x9300, 0x0000 ; 16 bit data segment base=0x0 limit=0xffff
    4.26  #endif // BX_ROMBIOS32
    4.27  
    4.28 +#if BX_PMM
    4.29 +; according to POST Memory Manager Specification Version 1.01
    4.30 +.align 16
    4.31 +pmm_structure:
    4.32 +  db 0x24,0x50,0x4d,0x4d ;; "$PMM" signature
    4.33 +  db 0x01 ;; revision
    4.34 +  db 16 ;; length
    4.35 +  db (-((pmm_entry_point>>8)+pmm_entry_point+0x20f))&0xff;; checksum
    4.36 +  dw pmm_entry_point,0xf000 ;; far call entrypoint
    4.37 +  db 0,0,0,0,0 ;; reserved
    4.38 +
    4.39 +pmm_entry_point:
    4.40 +  pushad
    4.41 +  mov   eax, esp
    4.42 +  add   eax, #(8*4+2+2) ;; skip regs of pushad, ip, cs
    4.43 +  push  eax ;; pointer to PMM function args
    4.44 +  call _pmm
    4.45 +  mov   bx, sp
    4.46 +SEG SS
    4.47 +  mov   [bx+(4+7*4)], ax
    4.48 +SEG SS
    4.49 +  mov   [bx+(4+5*4)], dx
    4.50 +  pop   eax
    4.51 +  popad
    4.52 +  db 0xcb ;; lret
    4.53 +#endif // BX_PMM
    4.54  
    4.55  ; parallel port detection: base address in DX, index in BX, timeout in CL
    4.56  detect_parport: