Skip to content

Commit

Permalink
Merge pull request #37 from dayeol/dev-cma
Browse files Browse the repository at this point in the history
Enable CMA in the kernel and the driver
  • Loading branch information
dayeol committed Feb 4, 2019
2 parents ec26e96 + 6bd99d3 commit 6de73dd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
76 changes: 76 additions & 0 deletions patches/linux-cma.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
diff --git arch/riscv/mm/dma.c arch/riscv/mm/dma.c
index 0689bb2..9fa9f11 100644
--- arch/riscv/mm/dma.c
+++ arch/riscv/mm/dma.c
@@ -20,6 +20,7 @@
#include <linux/gfp.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>
+#include <linux/dma-contiguous.h>
#include <linux/scatterlist.h>
#include <linux/swiotlb.h>

@@ -35,14 +36,41 @@ static void *dma_riscv_alloc(struct device *dev, size_t size,
gfp |= __GFP_DMA32;
}

- return swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
+ if (dev_get_cma_area(dev) && gfpflags_allow_blocking(gfp)) {
+ void* addr;
+ struct page *page;
+ page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
+ get_order(size), gfp);
+ if (!page)
+ {
+ pr_err("Unabled to allocated from contiguous memory\n");
+ return NULL;
+ }
+
+ *dma_handle = phys_to_dma(dev, page_to_phys(page));
+ addr = page_address(page);
+ memset(addr, 0, size);
+ return addr;
+ } else {
+ return swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
+ }
}

static void dma_riscv_free(struct device *dev, size_t size,
void *cpu_addr, dma_addr_t dma_addr,
unsigned long attrs)
{
- return swiotlb_free_coherent(dev, size, cpu_addr, dma_addr);
+ bool freed;
+ phys_addr_t paddr = dma_to_phys(dev, dma_addr);
+
+ freed = dma_release_from_contiguous(dev,
+ phys_to_page(paddr),
+ size >> PAGE_SHIFT);
+ if(!freed) {
+ swiotlb_free_coherent(dev, size, cpu_addr, dma_addr);
+ }
+
+ return;
}

static int dma_riscv_supported(struct device *dev, u64 mask)
diff --git arch/riscv/mm/init.c arch/riscv/mm/init.c
index c77df81..312c84d 100644
--- arch/riscv/mm/init.c
+++ arch/riscv/mm/init.c
@@ -18,6 +18,7 @@
#include <linux/memblock.h>
#include <linux/swap.h>
#include <linux/sizes.h>
+#include <linux/dma-contiguous.h>

#include <asm/tlbflush.h>
#include <asm/sections.h>
@@ -44,6 +45,7 @@ void __init paging_init(void)
setup_zero_page();
local_flush_tlb_all();
zone_sizes_init();
+ dma_contiguous_reserve(memblock_end_of_DRAM());
}

void __init mem_init(void)
9 changes: 8 additions & 1 deletion scripts/apply-patch.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#!/usr/bin/env bash
cd riscv-qemu
patch -p0 --forward hw/riscv/boot.c < ../patches/qemu-kernel-reloc.patch || true
patch --forward hw/riscv/boot.c < ../patches/qemu-kernel-reloc.patch || true
cd ..

# linux patch
cd riscv-linux
patch -p0 --forward < ../patches/linux-cma.patch || true
cd ..

0 comments on commit 6de73dd

Please sign in to comment.