MOS Source Code
Loading...
Searching...
No Matches
dma.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#define pr_fmt(fmt) "dma: " fmt
4
5#include "mos/mm/dma.h"
6
7#include "mos/mm/mm.h"
11#include "mos/syslog/printk.h"
12
13#include <mos_string.h>
14
15static pfn_t dmabuf_do_allocate(size_t n_pages, bool do_ref)
16{
18 if (do_ref)
19 pmm_ref(frames, n_pages);
20 memset((void *) phyframe_va(frames), 0, n_pages * MOS_PAGE_SIZE);
21 return phyframe_pfn(frames);
22}
23
24pfn_t dmabuf_allocate(size_t n_pages, ptr_t *vaddr)
25{
26 const pfn_t pfn = dmabuf_do_allocate(n_pages, true);
28 *vaddr = vmap->vaddr;
29 pr_dinfo2(dma, "allocated %zu DMA pages at " PFN_FMT " and mapped them at " PTR_FMT, n_pages, pfn, *vaddr);
30 return pfn;
31}
32
33bool dmabuf_free(ptr_t vaddr, ptr_t paddr)
34{
35 MOS_UNUSED(paddr);
36 pr_dinfo2(dma, "freeing DMA pages at " PTR_FMT, vaddr);
37
38 bool ret = false;
39
41 vmap_t *vmap = vmap_obtain(current_mm, (ptr_t) vaddr, NULL);
42 if (!vmap)
43 goto done;
44
45 ret = true;
46 vmap_destroy(vmap);
47
48done:
50 return ret;
51}
52
53pfn_t dmabuf_share(void *buf, size_t size)
54{
55 const size_t n_pages = ALIGN_UP_TO_PAGE(size) / MOS_PAGE_SIZE;
56 const pfn_t pfn = dmabuf_do_allocate(n_pages, false);
57 pr_dinfo2(dma, "sharing %zu bytes at " PFN_FMT, size, pfn);
58
59 memcpy((void *) pfn_va(pfn), buf, size);
60 return pfn;
61}
62
63bool dmabuf_unshare(ptr_t phys, size_t size, void *buffer)
64{
65 const pfn_t pfn = phys / MOS_PAGE_SIZE;
66 pr_dinfo2(dma, "unsharing %zu bytes at " PFN_FMT, size, pfn);
67 memcpy(buffer, (void *) pfn_va(pfn), size);
69 return true;
70}
#define MOS_PAGE_SIZE
Definition autoconf.h:6
#define MOS_ADDR_USER_MMAP
Definition autoconf.h:2
pfn_t dmabuf_share(void *buf, size_t size)
Definition dma.c:53
bool dmabuf_free(ptr_t vaddr, ptr_t paddr)
Definition dma.c:33
static pfn_t dmabuf_do_allocate(size_t n_pages, bool do_ref)
Definition dma.c:15
bool dmabuf_unshare(ptr_t phys, size_t size, void *buffer)
Definition dma.c:63
pfn_t dmabuf_allocate(size_t n_pages, ptr_t *vaddr)
Allocate DMA pages.
Definition dma.c:24
#define phyframe_va(frame)
Definition mm.h:79
vmap_t * vmap_obtain(mm_context_t *mmctx, ptr_t vaddr, size_t *out_offset)
Get the vmap object for a virtual address.
Definition mm.c:192
#define pfn_va(pfn)
Definition mm.h:76
void vmap_destroy(vmap_t *vmap)
Destroy a vmap object, and unmmap the region.
Definition mm.c:171
@ VMAP_TYPE_SHARED
Definition mm.h:32
@ VMAP_DMA
Definition mm.h:26
vmap_t * mm_map_user_pages(mm_context_t *mmctx, ptr_t vaddr, pfn_t pfn, size_t npages, vm_flags flags, valloc_flags vaflags, vmap_type_t type, vmap_content_t content)
Definition paging.c:92
@ VALLOC_DEFAULT
Default allocation flags.
Definition paging.h:21
#define pfn_phyframe(pfn)
Definition pmm.h:81
#define pmm_ref(thing, npages)
Definition pmm.h:143
#define phyframe_pfn(frame)
Definition pmm.h:80
void pmm_free_frames(phyframe_t *start_frame, size_t n_pages)
Definition pmm.c:108
phyframe_t * pmm_allocate_frames(size_t n_frames, pmm_allocation_flags_t flags)
Allocate n_frames of contiguous physical memory.
Definition pmm.c:92
@ PMM_ALLOC_NORMAL
allocate normal pages
Definition pmm.h:75
#define ALIGN_UP_TO_PAGE(addr)
Definition mos_global.h:75
#define MOS_UNUSED(x)
Definition mos_global.h:64
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
@ VM_USER_RW
Definition platform.h:54
@ VM_CACHE_DISABLED
Definition platform.h:47
#define current_mm
Definition platform.h:32
#define pr_dinfo2(feat, fmt,...)
Definition printk.h:27
size_t size
Definition slab.c:30
#define spinlock_acquire(lock)
Definition spinlock.h:61
#define spinlock_release(lock)
Definition spinlock.h:62
Definition mm.h:58
ptr_t vaddr
Definition mm.h:62
static char buffer[2048]
Definition test_printf.c:7
unsigned long long pfn_t
Definition types.h:41
#define PFN_FMT
Definition types.h:42
#define PTR_FMT
Definition types.h:33
unsigned long ptr_t
Definition types.h:25