blob: 4d1f01dc46e5bb8e5aa1b2e53aa495d4d50ee391 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* pci-dma.c: Dynamic DMA mapping support for the FRV CPUs that have MMUs
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/dma-mapping.h>
14#include <linux/list.h>
15#include <linux/pci.h>
Paul Gortmaker9a78da12012-04-01 16:38:44 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/highmem.h>
Al Viro9e6a76b2007-10-27 19:23:30 +010018#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/io.h>
20
Al Viroa5da7d32005-10-21 03:21:18 -040021void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 void *ret;
24
25 ret = consistent_alloc(gfp, size, dma_handle);
26 if (ret)
27 memset(ret, 0, size);
28
29 return ret;
30}
31
David Howells40234402006-01-08 01:01:19 -080032EXPORT_SYMBOL(dma_alloc_coherent);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
35{
36 consistent_free(vaddr);
37}
38
David Howells40234402006-01-08 01:01:19 -080039EXPORT_SYMBOL(dma_free_coherent);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
42 enum dma_data_direction direction)
43{
Stoyan Gaydarovdb5c4442009-06-11 13:05:04 +010044 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);
47
48 return virt_to_bus(ptr);
49}
50
David Howells40234402006-01-08 01:01:19 -080051EXPORT_SYMBOL(dma_map_single);
52
Akinobu Mita0989e1f2015-06-25 15:00:46 -070053int dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 enum dma_data_direction direction)
55{
56 unsigned long dampr2;
57 void *vaddr;
58 int i;
Akinobu Mita0989e1f2015-06-25 15:00:46 -070059 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Stoyan Gaydarovdb5c4442009-06-11 13:05:04 +010061 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 dampr2 = __get_DAMPR(2);
64
Akinobu Mita0989e1f2015-06-25 15:00:46 -070065 for_each_sg(sglist, sg, nents, i) {
66 vaddr = kmap_atomic_primary(sg_page(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 frv_dcache_writeback((unsigned long) vaddr,
69 (unsigned long) vaddr + PAGE_SIZE);
70
71 }
72
Cong Wang144cf862012-06-27 13:02:49 +080073 kunmap_atomic_primary(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (dampr2) {
75 __set_DAMPR(2, dampr2);
76 __set_IAMPR(2, dampr2);
77 }
78
79 return nents;
80}
81
David Howells40234402006-01-08 01:01:19 -080082EXPORT_SYMBOL(dma_map_sg);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset,
85 size_t size, enum dma_data_direction direction)
86{
87 BUG_ON(direction == DMA_NONE);
88 flush_dcache_page(page);
89 return (dma_addr_t) page_to_phys(page) + offset;
90}
David Howells40234402006-01-08 01:01:19 -080091
92EXPORT_SYMBOL(dma_map_page);