HelenOS sources
This source file includes following definitions.
- dma_policy_create
- cache_insert
- cache_evict
- cache_find
- dma_buffer_alloc_policy
- dma_buffer_alloc
- dma_buffer_free
- dma_buffer_phys
- dma_buffer_is_4gib
- dma_buffer_lock
- dma_buffer_unlock
- dma_buffer_acquire
- dma_buffer_release
#include <align.h>
#include <as.h>
#include <ddi.h>
#include <stddef.h>
#include "usb/dma_buffer.h"
dma_policy_t dma_policy_create(unsigned flags, size_t chunk_size)
{
assert((chunk_size & (chunk_size - 1)) == 0);
assert(chunk_size >= PAGE_SIZE || chunk_size == 0);
return ((chunk_size - 1) & DMA_POLICY_CHUNK_SIZE_MASK) |
(flags & DMA_POLICY_FLAGS_MASK);
}
static struct {
const void *last;
uintptr_t phys;
} phys_mapping_cache = { 0 };
static void cache_insert(const void *v, uintptr_t p)
{
phys_mapping_cache.last = v;
phys_mapping_cache.phys = p;
}
static void cache_evict(const void *v)
{
if (phys_mapping_cache.last == v)
phys_mapping_cache.last = NULL;
}
static bool cache_find(const void *v, uintptr_t *p)
{
*p = phys_mapping_cache.phys;
return phys_mapping_cache.last == v;
}
errno_t dma_buffer_alloc_policy(dma_buffer_t *db, size_t size, dma_policy_t policy)
{
assert(db);
const size_t real_size = ALIGN_UP(size, PAGE_SIZE);
const bool need_4gib = !!(policy & DMA_POLICY_4GiB);
const uintptr_t flags = need_4gib ? DMAMEM_4GiB : 0;
uintptr_t phys;
void *address = AS_AREA_ANY;
const int err = dmamem_map_anonymous(real_size,
flags, AS_AREA_READ | AS_AREA_WRITE, 0,
&phys, &address);
if (err)
return err;
volatile char *buf = address;
for (size_t i = 0; i < size; i += PAGE_SIZE)
buf[i] = 0xff;
db->virt = address;
db->policy = dma_policy_create(policy, 0);
cache_insert(db->virt, phys);
return EOK;
}
errno_t dma_buffer_alloc(dma_buffer_t *db, size_t size)
{
return dma_buffer_alloc_policy(db, size, DMA_POLICY_DEFAULT);
}
void dma_buffer_free(dma_buffer_t *db)
{
if (db->virt) {
dmamem_unmap_anonymous(db->virt);
db->virt = NULL;
db->policy = 0;
}
}
uintptr_t dma_buffer_phys(const dma_buffer_t *db, const void *virt)
{
const size_t chunk_mask = dma_policy_chunk_mask(db->policy);
const uintptr_t offset = (virt - db->virt) & chunk_mask;
const void *chunk_base = virt - offset;
uintptr_t phys;
if (!cache_find(chunk_base, &phys)) {
if (as_get_physical_mapping(chunk_base, &phys))
return 0;
cache_insert(chunk_base, phys);
}
return phys + offset;
}
static bool dma_buffer_is_4gib(dma_buffer_t *db, size_t size)
{
if (sizeof(uintptr_t) <= 32)
return true;
const size_t chunk_size = dma_policy_chunk_mask(db->policy) + 1;
const size_t chunks = chunk_size ? 1 : size / chunk_size;
for (size_t c = 0; c < chunks; c++) {
const void *addr = db->virt + (c * chunk_size);
const uintptr_t phys = dma_buffer_phys(db, addr);
if ((phys & DMAMEM_4GiB) != 0)
return false;
}
return true;
}
errno_t dma_buffer_lock(dma_buffer_t *db, void *virt, size_t size)
{
assert(virt);
uintptr_t phys;
const errno_t err = dmamem_map(virt, size, 0, 0, &phys);
if (err)
return err;
db->virt = virt;
db->policy = dma_policy_create(0, PAGE_SIZE);
cache_insert(virt, phys);
unsigned flags = -1U;
if (!dma_buffer_is_4gib(db, size))
flags &= ~DMA_POLICY_4GiB;
db->policy = dma_policy_create(flags, PAGE_SIZE);
return EOK;
}
void dma_buffer_unlock(dma_buffer_t *db, size_t size)
{
if (db->virt) {
dmamem_unmap(db->virt, size);
db->virt = NULL;
db->policy = 0;
}
}
void dma_buffer_acquire(dma_buffer_t *db)
{
cache_evict(db->virt);
}
void dma_buffer_release(dma_buffer_t *db)
{
cache_evict(db->virt);
}
HelenOS homepage, sources at GitHub