#include <KernelExport.h>
#include <Errors.h>
#include <OS.h>
#include <string.h>
#include "util.h"
#define TRACE_UTIL
#ifdef TRACE_UTIL
#define TRACE dprintf
#else
#define TRACE(a...)
#endif
area_id
map_mem(void **virt, phys_addr_t phy, size_t size, uint32 protection,
const char *name)
{
uint32 offset;
phys_addr_t phyadr;
void *mapadr;
area_id area;
TRACE("mapping physical address %" B_PRIxPHYSADDR " with %ld bytes for %s\n",
phy, size, name);
offset = (uint32)phy & (B_PAGE_SIZE - 1);
phyadr = phy - offset;
size = ROUNDUP(size + offset, B_PAGE_SIZE);
area = map_physical_memory(name, phyadr, size,
B_ANY_KERNEL_BLOCK_ADDRESS, protection, &mapadr);
if (area < B_OK) {
TRACE("mapping '%s' failed, error 0x%" B_PRIx32 " (%s)\n", name, area,
strerror(area));
return area;
}
*virt = (char *)mapadr + offset;
TRACE("physical = %" B_PRIxPHYSADDR ", virtual = %p, offset = %" B_PRIu32
", phyadr = %" B_PRIxPHYSADDR ", mapadr = %p, size = %" B_PRIuSIZE
", area = 0x%08" B_PRIx32 "\n", phy, *virt, offset, phyadr, mapadr,
size, area);
return area;
}
area_id
alloc_mem(void **virt, phys_addr_t *phy, size_t size, uint32 protection,
const char *name)
{
physical_entry pe;
void * virtadr;
area_id areaid;
status_t rv;
TRACE("allocating %" B_PRIuSIZE " bytes for %s\n", size, name);
size = ROUNDUP(size, B_PAGE_SIZE);
areaid = create_area(name, &virtadr, B_ANY_KERNEL_ADDRESS, size,
B_32_BIT_CONTIGUOUS, protection);
if (areaid < B_OK) {
TRACE("couldn't allocate area %s\n", name);
return B_ERROR;
}
rv = get_memory_map(virtadr, size, &pe, 1);
if (rv < B_OK) {
delete_area(areaid);
TRACE("couldn't map %s\n", name);
return B_ERROR;
}
if (virt)
*virt = virtadr;
if (phy)
*phy = pe.address;
TRACE("area = %" B_PRId32 ", size = %" B_PRIuSIZE ", virt = %p, phy = %"
B_PRIxPHYSADDR "\n", areaid, size, virtadr, pe.address);
return areaid;
}