Skip to content

Commit 19e6e5e

Browse files
vitkyrkaRussell King
authored andcommitted
ARM: 8547/1: dma-mapping: store buffer information
Keep a list of allocated DMA buffers so that we can store metadata in alloc() which we later need in free(). Signed-off-by: Rabin Vincent <rabin.vincent@axis.com> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent f57deb0 commit 19e6e5e

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

arch/arm/mm/dma-mapping.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@
4242
#include "dma.h"
4343
#include "mm.h"
4444

45+
struct arm_dma_buffer {
46+
struct list_head list;
47+
void *virt;
48+
};
49+
50+
static LIST_HEAD(arm_dma_bufs);
51+
static DEFINE_SPINLOCK(arm_dma_bufs_lock);
52+
53+
static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
54+
{
55+
struct arm_dma_buffer *buf, *found = NULL;
56+
unsigned long flags;
57+
58+
spin_lock_irqsave(&arm_dma_bufs_lock, flags);
59+
list_for_each_entry(buf, &arm_dma_bufs, list) {
60+
if (buf->virt == virt) {
61+
list_del(&buf->list);
62+
found = buf;
63+
break;
64+
}
65+
}
66+
spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
67+
return found;
68+
}
69+
4570
/*
4671
* The DMA API is built upon the notion of "buffer ownership". A buffer
4772
* is either exclusively owned by the CPU (and therefore may be accessed
@@ -620,6 +645,7 @@ static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
620645
struct page *page = NULL;
621646
void *addr;
622647
bool want_vaddr;
648+
struct arm_dma_buffer *buf;
623649

624650
#ifdef CONFIG_DMA_API_DEBUG
625651
u64 limit = (mask + 1) & ~mask;
@@ -633,6 +659,10 @@ static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
633659
if (!mask)
634660
return NULL;
635661

662+
buf = kzalloc(sizeof(*buf), gfp);
663+
if (!buf)
664+
return NULL;
665+
636666
if (mask < 0xffffffffULL)
637667
gfp |= GFP_DMA;
638668

@@ -662,8 +692,18 @@ static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
662692
addr = __alloc_remap_buffer(dev, size, gfp, prot, &page,
663693
caller, want_vaddr);
664694

665-
if (page)
695+
if (page) {
696+
unsigned long flags;
697+
666698
*handle = pfn_to_dma(dev, page_to_pfn(page));
699+
buf->virt = want_vaddr ? addr : page;
700+
701+
spin_lock_irqsave(&arm_dma_bufs_lock, flags);
702+
list_add(&buf->list, &arm_dma_bufs);
703+
spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
704+
} else {
705+
kfree(buf);
706+
}
667707

668708
return want_vaddr ? addr : page;
669709
}
@@ -742,6 +782,11 @@ static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
742782
{
743783
struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
744784
bool want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
785+
struct arm_dma_buffer *buf;
786+
787+
buf = arm_dma_buffer_find(cpu_addr);
788+
if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
789+
return;
745790

746791
size = PAGE_ALIGN(size);
747792

@@ -760,6 +805,8 @@ static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
760805
WARN_ON(irqs_disabled());
761806
__free_from_contiguous(dev, page, cpu_addr, size, want_vaddr);
762807
}
808+
809+
kfree(buf);
763810
}
764811

765812
void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,

0 commit comments

Comments
 (0)