aboutsummaryrefslogtreecommitdiffstats
path: root/arch/openrisc
diff options
context:
space:
mode:
authorJonas Bonn <jonas@southpole.se>2011-06-04 14:56:48 -0400
committerJonas Bonn <jonas@southpole.se>2011-07-22 12:46:32 -0400
commita39af6f7b806f2a52962254ea8dc635b4c240810 (patch)
tree968f71a1812f7b4e658992d16451b36ab8b49c0d /arch/openrisc
parente5ad95ce9b8d7efc443d39a7bbc4e55b7a4593f1 (diff)
OpenRISC: DMA
Simple DMA implementation. Allows for allocation of coherent memory (simply uncached) for DMA operations. Signed-off-by: Jonas Bonn <jonas@southpole.se> Reviewed-by: Arnd Bergmann <arnd@arndb.de>
Diffstat (limited to 'arch/openrisc')
-rw-r--r--arch/openrisc/include/asm/dma-mapping.h134
-rw-r--r--arch/openrisc/kernel/dma.c191
2 files changed, 325 insertions, 0 deletions
diff --git a/arch/openrisc/include/asm/dma-mapping.h b/arch/openrisc/include/asm/dma-mapping.h
new file mode 100644
index 000000000000..052f877b52a5
--- /dev/null
+++ b/arch/openrisc/include/asm/dma-mapping.h
@@ -0,0 +1,134 @@
1/*
2 * OpenRISC Linux
3 *
4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
6 * declaration.
7 *
8 * OpenRISC implementation:
9 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#ifndef __ASM_OPENRISC_DMA_MAPPING_H
18#define __ASM_OPENRISC_DMA_MAPPING_H
19
20/*
21 * See Documentation/PCI/PCI-DMA-mapping.txt and
22 * Documentation/DMA-API.txt for documentation.
23 *
24 * This file is written with the intention of eventually moving over
25 * to largely using asm-generic/dma-mapping-common.h in its place.
26 */
27
28#include <linux/dma-debug.h>
29#include <asm-generic/dma-coherent.h>
30#include <linux/kmemcheck.h>
31
32#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
33
34int dma_mapping_error(struct device *dev, dma_addr_t dma_addr);
35
36#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
37#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
38
39void *or1k_dma_alloc_coherent(struct device *dev, size_t size,
40 dma_addr_t *dma_handle, gfp_t flag);
41void or1k_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
42 dma_addr_t dma_handle);
43dma_addr_t or1k_map_page(struct device *dev, struct page *page,
44 unsigned long offset, size_t size,
45 enum dma_data_direction dir,
46 struct dma_attrs *attrs);
47void or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
48 size_t size, enum dma_data_direction dir,
49 struct dma_attrs *attrs);
50void or1k_sync_single_for_cpu(struct device *dev,
51 dma_addr_t dma_handle, size_t size,
52 enum dma_data_direction dir);
53void or1k_sync_single_for_device(struct device *dev,
54 dma_addr_t dma_handle, size_t size,
55 enum dma_data_direction dir);
56
57static inline void *dma_alloc_coherent(struct device *dev, size_t size,
58 dma_addr_t *dma_handle, gfp_t flag)
59{
60 void *memory;
61
62 memory = or1k_dma_alloc_coherent(dev, size, dma_handle, flag);
63
64 debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
65 return memory;
66}
67
68static inline void dma_free_coherent(struct device *dev, size_t size,
69 void *cpu_addr, dma_addr_t dma_handle)
70{
71 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
72 or1k_dma_free_coherent(dev, size, cpu_addr, dma_handle);
73}
74
75static inline dma_addr_t dma_map_single(struct device *dev, void *ptr,
76 size_t size,
77 enum dma_data_direction dir)
78{
79 dma_addr_t addr;
80
81 kmemcheck_mark_initialized(ptr, size);
82 BUG_ON(!valid_dma_direction(dir));
83 addr = or1k_map_page(dev, virt_to_page(ptr),
84 (unsigned long)ptr & ~PAGE_MASK, size,
85 dir, NULL);
86 debug_dma_map_page(dev, virt_to_page(ptr),
87 (unsigned long)ptr & ~PAGE_MASK, size,
88 dir, addr, true);
89 return addr;
90}
91
92static inline void dma_unmap_single(struct device *dev, dma_addr_t addr,
93 size_t size,
94 enum dma_data_direction dir)
95{
96 BUG_ON(!valid_dma_direction(dir));
97 or1k_unmap_page(dev, addr, size, dir, NULL);
98 debug_dma_unmap_page(dev, addr, size, dir, true);
99}
100
101static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
102 size_t size,
103 enum dma_data_direction dir)
104{
105 BUG_ON(!valid_dma_direction(dir));
106 or1k_sync_single_for_cpu(dev, addr, size, dir);
107 debug_dma_sync_single_for_cpu(dev, addr, size, dir);
108}
109
110static inline void dma_sync_single_for_device(struct device *dev,
111 dma_addr_t addr, size_t size,
112 enum dma_data_direction dir)
113{
114 BUG_ON(!valid_dma_direction(dir));
115 or1k_sync_single_for_device(dev, addr, size, dir);
116 debug_dma_sync_single_for_device(dev, addr, size, dir);
117}
118
119static inline int dma_supported(struct device *dev, u64 dma_mask)
120{
121 /* Support 32 bit DMA mask exclusively */
122 return dma_mask == 0xffffffffULL;
123}
124
125static inline int dma_set_mask(struct device *dev, u64 dma_mask)
126{
127 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
128 return -EIO;
129
130 *dev->dma_mask = dma_mask;
131
132 return 0;
133}
134#endif /* __ASM_OPENRISC_DMA_MAPPING_H */
diff --git a/arch/openrisc/kernel/dma.c b/arch/openrisc/kernel/dma.c
new file mode 100644
index 000000000000..968d3ee477e3
--- /dev/null
+++ b/arch/openrisc/kernel/dma.c
@@ -0,0 +1,191 @@
1/*
2 * OpenRISC Linux
3 *
4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
6 * declaration.
7 *
8 * Modifications for the OpenRISC architecture:
9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * DMA mapping callbacks...
18 * As alloc_coherent is the only DMA callback being used currently, that's
19 * the only thing implemented properly. The rest need looking into...
20 */
21
22#include <linux/dma-mapping.h>
23#include <linux/dma-debug.h>
24
25#include <asm/cpuinfo.h>
26#include <asm/spr_defs.h>
27#include <asm/tlbflush.h>
28
29static int page_set_nocache(pte_t *pte, unsigned long addr,
30 unsigned long next, struct mm_walk *walk)
31{
32 unsigned long cl;
33
34 pte_val(*pte) |= _PAGE_CI;
35
36 /*
37 * Flush the page out of the TLB so that the new page flags get
38 * picked up next time there's an access
39 */
40 flush_tlb_page(NULL, addr);
41
42 /* Flush page out of dcache */
43 for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo.dcache_block_size)
44 mtspr(SPR_DCBFR, cl);
45
46 return 0;
47}
48
49static int page_clear_nocache(pte_t *pte, unsigned long addr,
50 unsigned long next, struct mm_walk *walk)
51{
52 pte_val(*pte) &= ~_PAGE_CI;
53
54 /*
55 * Flush the page out of the TLB so that the new page flags get
56 * picked up next time there's an access
57 */
58 flush_tlb_page(NULL, addr);
59
60 return 0;
61}
62
63/*
64 * Alloc "coherent" memory, which for OpenRISC means simply uncached.
65 *
66 * This function effectively just calls __get_free_pages, sets the
67 * cache-inhibit bit on those pages, and makes sure that the pages are
68 * flushed out of the cache before they are used.
69 *
70 */
71void *or1k_dma_alloc_coherent(struct device *dev, size_t size,
72 dma_addr_t *dma_handle, gfp_t gfp)
73{
74 unsigned long va;
75 void *page;
76 struct mm_walk walk = {
77 .pte_entry = page_set_nocache,
78 .mm = &init_mm
79 };
80
81 page = alloc_pages_exact(size, gfp);
82 if (!page)
83 return NULL;
84
85 /* This gives us the real physical address of the first page. */
86 *dma_handle = __pa(page);
87
88 va = (unsigned long)page;
89
90 /*
91 * We need to iterate through the pages, clearing the dcache for
92 * them and setting the cache-inhibit bit.
93 */
94 if (walk_page_range(va, va + size, &walk)) {
95 free_pages_exact(page, size);
96 return NULL;
97 }
98
99 return (void *)va;
100}
101
102void or1k_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
103 dma_addr_t dma_handle)
104{
105 unsigned long va = (unsigned long)vaddr;
106 struct mm_walk walk = {
107 .pte_entry = page_clear_nocache,
108 .mm = &init_mm
109 };
110
111 /* walk_page_range shouldn't be able to fail here */
112 WARN_ON(walk_page_range(va, va + size, &walk));
113
114 free_pages_exact(vaddr, size);
115}
116
117dma_addr_t or1k_map_page(struct device *dev, struct page *page,
118 unsigned long offset, size_t size,
119 enum dma_data_direction dir,
120 struct dma_attrs *attrs)
121{
122 unsigned long cl;
123 dma_addr_t addr = page_to_phys(page) + offset;
124
125 switch (dir) {
126 case DMA_TO_DEVICE:
127 /* Flush the dcache for the requested range */
128 for (cl = addr; cl < addr + size;
129 cl += cpuinfo.dcache_block_size)
130 mtspr(SPR_DCBFR, cl);
131 break;
132 case DMA_FROM_DEVICE:
133 /* Invalidate the dcache for the requested range */
134 for (cl = addr; cl < addr + size;
135 cl += cpuinfo.dcache_block_size)
136 mtspr(SPR_DCBIR, cl);
137 break;
138 default:
139 /*
140 * NOTE: If dir == DMA_BIDIRECTIONAL then there's no need to
141 * flush nor invalidate the cache here as the area will need
142 * to be manually synced anyway.
143 */
144 break;
145 }
146
147 return addr;
148}
149
150void or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
151 size_t size, enum dma_data_direction dir,
152 struct dma_attrs *attrs)
153{
154 /* Nothing special to do here... */
155}
156
157void or1k_sync_single_for_cpu(struct device *dev,
158 dma_addr_t dma_handle, size_t size,
159 enum dma_data_direction dir)
160{
161 unsigned long cl;
162 dma_addr_t addr = dma_handle;
163
164 /* Invalidate the dcache for the requested range */
165 for (cl = addr; cl < addr + size; cl += cpuinfo.dcache_block_size)
166 mtspr(SPR_DCBIR, cl);
167}
168
169void or1k_sync_single_for_device(struct device *dev,
170 dma_addr_t dma_handle, size_t size,
171 enum dma_data_direction dir)
172{
173 unsigned long cl;
174 dma_addr_t addr = dma_handle;
175
176 /* Flush the dcache for the requested range */
177 for (cl = addr; cl < addr + size; cl += cpuinfo.dcache_block_size)
178 mtspr(SPR_DCBFR, cl);
179}
180
181/* Number of entries preallocated for DMA-API debugging */
182#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
183
184static int __init dma_init(void)
185{
186 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
187
188 return 0;
189}
190
191fs_initcall(dma_init);