Skip to content

Commit f464938

Browse files
Zubair Lutfullah Kakakhelralfbaechle
authored andcommitted
MIPS: DMA: Add cma support
Adds cma support to the MIPS architecture. cma uses memblock. However, mips uses bootmem. bootmem is informed about any regions reserved by memblock dma api is modified to use cma reserved memory regions when available Tested using cma_test. cma_test is a simple driver that assigns blocks of memory from cma reserved sections. Signed-off-by: Zubair Lutfullah Kakakhel <Zubair.Kakakhel@imgtec.com> Acked-by: Michal Nazarewicz <mina86@mina86.com> Cc: catalin.marinas@arm.com Cc: will.deacon@arm.com Cc: tglx@linutronix.de Cc: mingo@redhat.com Cc: hpa@zytor.com Cc: arnd@arndb.de Cc: gregkh@linuxfoundation.org Cc: m.szyprowski@samsung.com Cc: x86@kernel.org Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Cc: linux-mips@linux-mips.org Cc: linux-arch@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/7360/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 8057b30 commit f464938

4 files changed

Lines changed: 36 additions & 12 deletions

File tree

‎arch/mips/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ config MIPS
2929
select GENERIC_ATOMIC64 if !64BIT
3030
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
3131
select HAVE_DMA_ATTRS
32+
select HAVE_DMA_CONTIGUOUS
3233
select HAVE_DMA_API_DEBUG
3334
select GENERIC_IRQ_PROBE
3435
select GENERIC_IRQ_SHOW

‎arch/mips/include/asm/Kbuild‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# MIPS headers
22
generic-y += cputime.h
33
generic-y += current.h
4+
generic-y += dma-contiguous.h
45
generic-y += emergency-restart.h
56
generic-y += hash.h
67
generic-y += local64.h

‎arch/mips/kernel/setup.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <linux/debugfs.h>
2525
#include <linux/kexec.h>
2626
#include <linux/sizes.h>
27+
#include <linux/device.h>
28+
#include <linux/dma-contiguous.h>
2729

2830
#include <asm/addrspace.h>
2931
#include <asm/bootinfo.h>
@@ -476,6 +478,7 @@ static void __init bootmem_init(void)
476478
* o bootmem_init()
477479
* o sparse_init()
478480
* o paging_init()
481+
* o dma_continguous_reserve()
479482
*
480483
* At this stage the bootmem allocator is ready to use.
481484
*
@@ -609,6 +612,7 @@ static void __init request_crashkernel(struct resource *res)
609612

610613
static void __init arch_mem_init(char **cmdline_p)
611614
{
615+
struct memblock_region *reg;
612616
extern void plat_mem_setup(void);
613617

614618
/* call board setup routine */
@@ -675,6 +679,11 @@ static void __init arch_mem_init(char **cmdline_p)
675679
sparse_init();
676680
plat_swiotlb_setup();
677681
paging_init();
682+
683+
dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
684+
/* Tell bootmem about cma reserved memblock section */
685+
for_each_memblock(reserved, reg)
686+
reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
678687
}
679688

680689
static void __init resource_init(void)

‎arch/mips/mm/dma-default.c‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/string.h>
1717
#include <linux/gfp.h>
1818
#include <linux/highmem.h>
19+
#include <linux/dma-contiguous.h>
1920

2021
#include <asm/cache.h>
2122
#include <asm/cpu-type.h>
@@ -128,23 +129,30 @@ static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
128129
dma_addr_t * dma_handle, gfp_t gfp, struct dma_attrs *attrs)
129130
{
130131
void *ret;
132+
struct page *page = NULL;
133+
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
131134

132135
if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
133136
return ret;
134137

135138
gfp = massage_gfp_flags(dev, gfp);
136139

137-
ret = (void *) __get_free_pages(gfp, get_order(size));
138-
139-
if (ret) {
140-
memset(ret, 0, size);
141-
*dma_handle = plat_map_dma_mem(dev, ret, size);
142-
143-
if (!plat_device_is_coherent(dev)) {
144-
dma_cache_wback_inv((unsigned long) ret, size);
145-
if (!hw_coherentio)
146-
ret = UNCAC_ADDR(ret);
147-
}
140+
if (IS_ENABLED(CONFIG_DMA_CMA) && !(gfp & GFP_ATOMIC))
141+
page = dma_alloc_from_contiguous(dev,
142+
count, get_order(size));
143+
if (!page)
144+
page = alloc_pages(gfp, get_order(size));
145+
146+
if (!page)
147+
return NULL;
148+
149+
ret = page_address(page);
150+
memset(ret, 0, size);
151+
*dma_handle = plat_map_dma_mem(dev, ret, size);
152+
if (!plat_device_is_coherent(dev)) {
153+
dma_cache_wback_inv((unsigned long) ret, size);
154+
if (!hw_coherentio)
155+
ret = UNCAC_ADDR(ret);
148156
}
149157

150158
return ret;
@@ -164,6 +172,8 @@ static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
164172
{
165173
unsigned long addr = (unsigned long) vaddr;
166174
int order = get_order(size);
175+
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
176+
struct page *page = NULL;
167177

168178
if (dma_release_from_coherent(dev, order, vaddr))
169179
return;
@@ -173,7 +183,10 @@ static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
173183
if (!plat_device_is_coherent(dev) && !hw_coherentio)
174184
addr = CAC_ADDR(addr);
175185

176-
free_pages(addr, get_order(size));
186+
page = virt_to_page((void *) addr);
187+
188+
if (!dma_release_from_contiguous(dev, page, count))
189+
__free_pages(page, get_order(size));
177190
}
178191

179192
static inline void __dma_sync_virtual(void *addr, size_t size,

0 commit comments

Comments
 (0)