~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/blackfin/kernel/dma-mapping.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Dynamic DMA mapping support
  3  *
  4  * Copyright 2005-2009 Analog Devices Inc.
  5  *
  6  * Licensed under the GPL-2 or later
  7  */
  8 
  9 #include <linux/types.h>
 10 #include <linux/gfp.h>
 11 #include <linux/string.h>
 12 #include <linux/spinlock.h>
 13 #include <linux/dma-mapping.h>
 14 #include <linux/scatterlist.h>
 15 #include <linux/export.h>
 16 #include <linux/bitmap.h>
 17 
 18 static spinlock_t dma_page_lock;
 19 static unsigned long *dma_page;
 20 static unsigned int dma_pages;
 21 static unsigned long dma_base;
 22 static unsigned long dma_size;
 23 static unsigned int dma_initialized;
 24 
 25 static void dma_alloc_init(unsigned long start, unsigned long end)
 26 {
 27         spin_lock_init(&dma_page_lock);
 28         dma_initialized = 0;
 29 
 30         dma_page = (unsigned long *)__get_free_page(GFP_KERNEL);
 31         memset(dma_page, 0, PAGE_SIZE);
 32         dma_base = PAGE_ALIGN(start);
 33         dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start);
 34         dma_pages = dma_size >> PAGE_SHIFT;
 35         memset((void *)dma_base, 0, DMA_UNCACHED_REGION);
 36         dma_initialized = 1;
 37 
 38         printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__,
 39                dma_page, dma_pages, dma_base);
 40 }
 41 
 42 static inline unsigned int get_pages(size_t size)
 43 {
 44         return ((size - 1) >> PAGE_SHIFT) + 1;
 45 }
 46 
 47 static unsigned long __alloc_dma_pages(unsigned int pages)
 48 {
 49         unsigned long ret = 0, flags;
 50         unsigned long start;
 51 
 52         if (dma_initialized == 0)
 53                 dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend);
 54 
 55         spin_lock_irqsave(&dma_page_lock, flags);
 56 
 57         start = bitmap_find_next_zero_area(dma_page, dma_pages, 0, pages, 0);
 58         if (start < dma_pages) {
 59                 ret = dma_base + (start << PAGE_SHIFT);
 60                 bitmap_set(dma_page, start, pages);
 61         }
 62         spin_unlock_irqrestore(&dma_page_lock, flags);
 63         return ret;
 64 }
 65 
 66 static void __free_dma_pages(unsigned long addr, unsigned int pages)
 67 {
 68         unsigned long page = (addr - dma_base) >> PAGE_SHIFT;
 69         unsigned long flags;
 70 
 71         if ((page + pages) > dma_pages) {
 72                 printk(KERN_ERR "%s: freeing outside range.\n", __func__);
 73                 BUG();
 74         }
 75 
 76         spin_lock_irqsave(&dma_page_lock, flags);
 77         bitmap_clear(dma_page, page, pages);
 78         spin_unlock_irqrestore(&dma_page_lock, flags);
 79 }
 80 
 81 static void *bfin_dma_alloc(struct device *dev, size_t size,
 82                 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
 83 {
 84         void *ret;
 85 
 86         ret = (void *)__alloc_dma_pages(get_pages(size));
 87 
 88         if (ret) {
 89                 memset(ret, 0, size);
 90                 *dma_handle = virt_to_phys(ret);
 91         }
 92 
 93         return ret;
 94 }
 95 
 96 static void bfin_dma_free(struct device *dev, size_t size, void *vaddr,
 97                   dma_addr_t dma_handle, unsigned long attrs)
 98 {
 99         __free_dma_pages((unsigned long)vaddr, get_pages(size));
100 }
101 
102 /*
103  * Streaming DMA mappings
104  */
105 void __dma_sync(dma_addr_t addr, size_t size,
106                 enum dma_data_direction dir)
107 {
108         __dma_sync_inline(addr, size, dir);
109 }
110 EXPORT_SYMBOL(__dma_sync);
111 
112 static int bfin_dma_map_sg(struct device *dev, struct scatterlist *sg_list,
113                 int nents, enum dma_data_direction direction,
114                 unsigned long attrs)
115 {
116         struct scatterlist *sg;
117         int i;
118 
119         for_each_sg(sg_list, sg, nents, i) {
120                 sg->dma_address = (dma_addr_t) sg_virt(sg);
121                 __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
122         }
123 
124         return nents;
125 }
126 
127 static void bfin_dma_sync_sg_for_device(struct device *dev,
128                 struct scatterlist *sg_list, int nelems,
129                 enum dma_data_direction direction)
130 {
131         struct scatterlist *sg;
132         int i;
133 
134         for_each_sg(sg_list, sg, nelems, i) {
135                 sg->dma_address = (dma_addr_t) sg_virt(sg);
136                 __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
137         }
138 }
139 
140 static dma_addr_t bfin_dma_map_page(struct device *dev, struct page *page,
141                 unsigned long offset, size_t size, enum dma_data_direction dir,
142                 unsigned long attrs)
143 {
144         dma_addr_t handle = (dma_addr_t)(page_address(page) + offset);
145 
146         _dma_sync(handle, size, dir);
147         return handle;
148 }
149 
150 static inline void bfin_dma_sync_single_for_device(struct device *dev,
151                 dma_addr_t handle, size_t size, enum dma_data_direction dir)
152 {
153         _dma_sync(handle, size, dir);
154 }
155 
156 struct dma_map_ops bfin_dma_ops = {
157         .alloc                  = bfin_dma_alloc,
158         .free                   = bfin_dma_free,
159 
160         .map_page               = bfin_dma_map_page,
161         .map_sg                 = bfin_dma_map_sg,
162 
163         .sync_single_for_device = bfin_dma_sync_single_for_device,
164         .sync_sg_for_device     = bfin_dma_sync_sg_for_device,
165 };
166 EXPORT_SYMBOL(bfin_dma_ops);
167 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp