2 * Port on Texas Instruments TMS320C6x architecture
4 * Copyright (C) 2004, 2009, 2010, 2011 Texas Instruments Incorporated
5 * Author: Aurelien Jacquiot <aurelien.jacquiot@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * DMA uncached mapping support.
13 * Using code pulled from ARM
14 * Copyright (C) 2000-2004 Russell King
17 #include <linux/slab.h>
18 #include <linux/bitmap.h>
19 #include <linux/bitops.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/memblock.h>
28 * DMA coherent memory management, can be redefined using the memdma=
33 static phys_addr_t dma_base
;
37 static unsigned long *dma_bitmap
;
40 static DEFINE_SPINLOCK(dma_lock
);
43 * Return a DMA coherent and contiguous memory chunk from the DMA memory
45 static inline u32
__alloc_dma_pages(int order
)
50 spin_lock_irqsave(&dma_lock
, flags
);
51 pos
= bitmap_find_free_region(dma_bitmap
, dma_pages
, order
);
52 spin_unlock_irqrestore(&dma_lock
, flags
);
54 return dma_base
+ (pos
<< PAGE_SHIFT
);
57 static void __free_dma_pages(u32 addr
, int order
)
60 u32 pos
= (addr
- dma_base
) >> PAGE_SHIFT
;
62 if (addr
< dma_base
|| (pos
+ (1 << order
)) >= dma_pages
) {
63 printk(KERN_ERR
"%s: freeing outside range.\n", __func__
);
67 spin_lock_irqsave(&dma_lock
, flags
);
68 bitmap_release_region(dma_bitmap
, pos
, order
);
69 spin_unlock_irqrestore(&dma_lock
, flags
);
73 * Allocate DMA coherent memory space and return both the kernel
74 * virtual and DMA address for that space.
76 void *dma_alloc_coherent(struct device
*dev
, size_t size
,
77 dma_addr_t
*handle
, gfp_t gfp
)
82 if (!dma_size
|| !size
)
85 order
= get_count_order(((size
- 1) >> PAGE_SHIFT
) + 1);
87 paddr
= __alloc_dma_pages(order
);
95 return phys_to_virt(paddr
);
97 EXPORT_SYMBOL(dma_alloc_coherent
);
100 * Free DMA coherent memory as defined by the above mapping.
102 void dma_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
103 dma_addr_t dma_handle
)
107 if (!dma_size
|| !size
)
110 order
= get_count_order(((size
- 1) >> PAGE_SHIFT
) + 1);
112 __free_dma_pages(virt_to_phys(vaddr
), order
);
114 EXPORT_SYMBOL(dma_free_coherent
);
117 * Initialise the coherent DMA memory allocator using the given uncached region.
119 void __init
coherent_mem_init(phys_addr_t start
, u32 size
)
121 phys_addr_t bitmap_phys
;
127 "Coherent memory (DMA) region start=0x%x size=0x%x\n",
133 /* allocate bitmap */
134 dma_pages
= dma_size
>> PAGE_SHIFT
;
135 if (dma_size
& (PAGE_SIZE
- 1))
138 bitmap_phys
= memblock_alloc(BITS_TO_LONGS(dma_pages
) * sizeof(long),
141 dma_bitmap
= phys_to_virt(bitmap_phys
);
142 memset(dma_bitmap
, 0, dma_pages
* PAGE_SIZE
);