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"
8#include "mos/mm/paging/paging.h"
9#include "mos/mm/physical/pmm.h"
10#include "mos/platform/platform.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{
17 phyframe_t *frames = pmm_allocate_frames(n_frames: n_pages, flags: PMM_ALLOC_NORMAL);
18 if (do_ref)
19 pmm_ref(frames, n_pages);
20 memset(s: (void *) phyframe_va(frames), c: 0, n: 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, do_ref: true);
27 const vmap_t *vmap = mm_map_user_pages(current_mm, MOS_ADDR_USER_MMAP, pfn, npages: n_pages, flags: VM_USER_RW | VM_CACHE_DISABLED, vaflags: VALLOC_DEFAULT, type: VMAP_TYPE_SHARED, content: VMAP_DMA);
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
40 spinlock_acquire(&current_mm->mm_lock);
41 vmap_t *vmap = vmap_obtain(current_mm, vaddr: (ptr_t) vaddr, NULL);
42 if (!vmap)
43 goto done;
44
45 ret = true;
46 vmap_destroy(vmap);
47
48done:
49 spinlock_release(&current_mm->mm_lock);
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, do_ref: false);
57 pr_dinfo2(dma, "sharing %zu bytes at " PFN_FMT, size, pfn);
58
59 memcpy(dest: (void *) pfn_va(pfn), src: buf, n: 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(dest: buffer, src: (void *) pfn_va(pfn), n: size);
68 pmm_free_frames(pfn_phyframe(pfn), ALIGN_UP_TO_PAGE(size) / MOS_PAGE_SIZE);
69 return true;
70}
71