1 From c6d8f92cfd7f4f19eb3b16545b3b68c561978fe8 Mon Sep 17 00:00:00 2001
2 From: Kristoffer Glembo <kristoffer@gaisler.com>
3 Date: Mon, 7 Jun 2010 14:00:30 +0200
4 Subject: [PATCH] sparc32: Added LEON dma_ops.
6 Added leon3_dma_ops and mmu_inval_dma_area.
8 arch/sparc/kernel/ioport.c | 139 +++++++++++++++++++++++++++++++------------
9 1 files changed, 100 insertions(+), 39 deletions(-)
11 --- a/arch/sparc/kernel/ioport.c
12 +++ b/arch/sparc/kernel/ioport.c
14 #include <asm/io-unit.h>
17 -#ifdef CONFIG_SPARC_LEON
18 -#define mmu_inval_dma_area(p, l) leon_flush_dcache_all()
20 +#ifndef CONFIG_SPARC_LEON
21 #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
23 +static inline void mmu_inval_dma_area(unsigned long va, unsigned long len)
25 + if (!sparc_leon3_snooping_enabled()) {
26 + leon_flush_dcache_all();
31 static struct resource *_sparc_find_resource(struct resource *r,
32 @@ -254,7 +259,7 @@ static void *sbus_alloc_coherent(struct
33 dma_addr_t *dma_addrp, gfp_t gfp)
35 struct platform_device *op = to_platform_device(dev);
36 - unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
37 + unsigned long len_total = PAGE_ALIGN(len);
41 @@ -287,15 +292,19 @@ static void *sbus_alloc_coherent(struct
42 * XXX That's where sdev would be used. Currently we load
43 * all iommu tables with the same translations.
45 - if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
48 - res->name = op->dev.of_node->name;
49 +#ifdef CONFIG_SPARC_LEON
50 + sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
51 + *dma_addrp = virt_to_phys(va);
53 + if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0) {
54 + release_resource(res);
58 + res->name = op->node->name;
60 return (void *)(unsigned long)res->start;
63 - release_resource(res);
65 free_pages(va, order);
67 @@ -321,7 +330,7 @@ static void sbus_free_coherent(struct de
71 - n = (n + PAGE_SIZE-1) & PAGE_MASK;
73 if ((res->end-res->start)+1 != n) {
74 printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
75 (long)((res->end-res->start)+1), n);
76 @@ -333,7 +342,12 @@ static void sbus_free_coherent(struct de
78 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
79 pgv = virt_to_page(p);
80 - mmu_unmap_dma_area(dev, ba, n);
82 +#ifdef CONFIG_SPARC_LEON
83 + sparc_unmapiorange((unsigned long)p, n);
85 + mmu_unmap_dma_area(dev, ba, n);
88 __free_pages(pgv, get_order(n));
90 @@ -408,9 +422,6 @@ struct dma_map_ops sbus_dma_ops = {
91 .sync_sg_for_device = sbus_sync_sg_for_device,
94 -struct dma_map_ops *dma_ops = &sbus_dma_ops;
95 -EXPORT_SYMBOL(dma_ops);
97 static int __init sparc_register_ioport(void)
99 register_proc_sparc_ioport();
100 @@ -422,7 +433,7 @@ arch_initcall(sparc_register_ioport);
102 #endif /* CONFIG_SBUS */
105 +#if defined(CONFIG_PCI) || defined(CONFIG_SPARC_LEON)
107 /* Allocate and map kernel buffer using consistent mode DMA for a device.
108 * hwdev should be valid struct pci_dev pointer for PCI devices.
109 @@ -430,7 +441,7 @@ arch_initcall(sparc_register_ioport);
110 static void *pci32_alloc_coherent(struct device *dev, size_t len,
111 dma_addr_t *pba, gfp_t gfp)
113 - unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
114 + unsigned long len_total = PAGE_ALIGN(len);
116 struct resource *res;
118 @@ -463,10 +474,6 @@ static void *pci32_alloc_coherent(struct
121 mmu_inval_dma_area(va, len_total);
123 -/* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
124 - (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
126 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
128 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
129 @@ -498,7 +505,7 @@ static void pci32_free_coherent(struct d
133 - n = (n + PAGE_SIZE-1) & PAGE_MASK;
135 if ((res->end-res->start)+1 != n) {
136 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
137 (long)((res->end-res->start)+1), (long)n);
138 @@ -515,6 +522,14 @@ static void pci32_free_coherent(struct d
139 free_pages(pgp, get_order(n));
142 +static void pci32_unmap_page(struct device *dev, dma_addr_t ba, size_t size,
143 + enum dma_data_direction dir, struct dma_attrs *attrs)
145 + if (dir != PCI_DMA_TODEVICE) {
146 + mmu_inval_dma_area((unsigned long)phys_to_virt(ba), PAGE_ALIGN(size));
151 * Same as pci_map_single, but with pages.
153 @@ -551,8 +566,7 @@ static int pci32_map_sg(struct device *d
155 /* IIep is write-through, not flushing. */
156 for_each_sg(sgl, sg, nents, n) {
157 - BUG_ON(page_address(sg_page(sg)) == NULL);
158 - sg->dma_address = virt_to_phys(sg_virt(sg));
159 + sg->dma_address = sg_phys(sg);
160 sg->dma_length = sg->length;
163 @@ -571,10 +585,7 @@ static void pci32_unmap_sg(struct device
165 if (dir != PCI_DMA_TODEVICE) {
166 for_each_sg(sgl, sg, nents, n) {
167 - BUG_ON(page_address(sg_page(sg)) == NULL);
168 - mmu_inval_dma_area(
169 - (unsigned long) page_address(sg_page(sg)),
170 - (sg->length + PAGE_SIZE-1) & PAGE_MASK);
171 + mmu_inval_dma_area((unsigned long)sg_virt(sg), PAGE_ALIGN(sg->length));
175 @@ -594,7 +605,7 @@ static void pci32_sync_single_for_cpu(st
177 if (dir != PCI_DMA_TODEVICE) {
178 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
179 - (size + PAGE_SIZE-1) & PAGE_MASK);
184 @@ -621,10 +632,7 @@ static void pci32_sync_sg_for_cpu(struct
186 if (dir != PCI_DMA_TODEVICE) {
187 for_each_sg(sgl, sg, nents, n) {
188 - BUG_ON(page_address(sg_page(sg)) == NULL);
189 - mmu_inval_dma_area(
190 - (unsigned long) page_address(sg_page(sg)),
191 - (sg->length + PAGE_SIZE-1) & PAGE_MASK);
192 + mmu_inval_dma_area((unsigned long)sg_virt(sg), PAGE_ALIGN(sg->length));
196 @@ -637,18 +645,38 @@ static void pci32_sync_sg_for_device(str
198 if (dir != PCI_DMA_TODEVICE) {
199 for_each_sg(sgl, sg, nents, n) {
200 - BUG_ON(page_address(sg_page(sg)) == NULL);
201 - mmu_inval_dma_area(
202 - (unsigned long) page_address(sg_page(sg)),
203 - (sg->length + PAGE_SIZE-1) & PAGE_MASK);
204 + mmu_inval_dma_area((unsigned long)sg_virt(sg), PAGE_ALIGN(sg->length));
209 +/* LEON3 unmapping functions
211 + * We can only invalidate the whole cache so unmap_page and unmap_sg do the same thing
213 +static void leon3_unmap_page(struct device *dev, dma_addr_t ba, size_t size,
214 + enum dma_data_direction dir, struct dma_attrs *attrs)
216 + if (dir != PCI_DMA_TODEVICE) {
217 + mmu_inval_dma_area(0, 0);
221 +static void leon3_unmap_sg(struct device *dev, struct scatterlist *sgl,
222 + int nents, enum dma_data_direction dir,
223 + struct dma_attrs *attrs)
226 + if (dir != PCI_DMA_TODEVICE) {
227 + mmu_inval_dma_area(0, 0);
231 struct dma_map_ops pci32_dma_ops = {
232 .alloc_coherent = pci32_alloc_coherent,
233 .free_coherent = pci32_free_coherent,
234 .map_page = pci32_map_page,
235 + .unmap_page = pci32_unmap_page,
236 .map_sg = pci32_map_sg,
237 .unmap_sg = pci32_unmap_sg,
238 .sync_single_for_cpu = pci32_sync_single_for_cpu,
239 @@ -658,7 +686,30 @@ struct dma_map_ops pci32_dma_ops = {
241 EXPORT_SYMBOL(pci32_dma_ops);
243 -#endif /* CONFIG_PCI */
244 +struct dma_map_ops leon3_dma_ops = {
245 + .alloc_coherent = sbus_alloc_coherent,
246 + .free_coherent = sbus_free_coherent,
247 + .map_page = pci32_map_page,
248 + .unmap_page = leon3_unmap_page,
249 + .map_sg = pci32_map_sg,
250 + .unmap_sg = leon3_unmap_sg,
251 + .sync_single_for_cpu = pci32_sync_single_for_cpu,
252 + .sync_single_for_device = pci32_sync_single_for_device,
253 + .sync_sg_for_cpu = pci32_sync_sg_for_cpu,
254 + .sync_sg_for_device = pci32_sync_sg_for_device,
257 +#endif /* CONFIG_PCI || CONFIG_SPARC_LEON */
259 +#ifdef CONFIG_SPARC_LEON
260 +struct dma_map_ops *dma_ops = &leon3_dma_ops;
262 +struct dma_map_ops *dma_ops = &sbus_dma_ops;
266 +EXPORT_SYMBOL(dma_ops);
270 * Return whether the given PCI device DMA address mask can be
271 @@ -676,6 +727,16 @@ int dma_supported(struct device *dev, u6
273 EXPORT_SYMBOL(dma_supported);
275 +int dma_set_mask(struct device *dev, u64 dma_mask)
278 + if (dev->bus == &pci_bus_type)
279 + return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
281 + return -EOPNOTSUPP;
283 +EXPORT_SYMBOL(dma_set_mask);
285 #ifdef CONFIG_PROC_FS
287 static int sparc_io_proc_show(struct seq_file *m, void *v)
288 @@ -717,7 +778,7 @@ static const struct file_operations spar
289 static struct resource *_sparc_find_resource(struct resource *root,
292 - struct resource *tmp;
293 + struct resource *tmp;
295 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
296 if (tmp->start <= hit && tmp->end >= hit)