Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/dma-mapping.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/pci.h> |
| 17 | #include <linux/highmem.h> |
Al Viro | 9e6a76b | 2007-10-27 19:23:30 +0100 | [diff] [blame] | 18 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | |
Al Viro | a5da7d3 | 2005-10-21 03:21:18 -0400 | [diff] [blame] | 21 | void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
| 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 Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 32 | EXPORT_SYMBOL(dma_alloc_coherent); |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle) |
| 35 | { |
| 36 | consistent_free(vaddr); |
| 37 | } |
| 38 | |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 39 | EXPORT_SYMBOL(dma_free_coherent); |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
| 42 | * Map a single buffer of the indicated size for DMA in streaming mode. |
| 43 | * The 32-bit bus address to use is returned. |
| 44 | * |
| 45 | * Once the device is given the dma address, the device owns this memory |
| 46 | * until either pci_unmap_single or pci_dma_sync_single is performed. |
| 47 | */ |
| 48 | dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size, |
| 49 | enum dma_data_direction direction) |
| 50 | { |
| 51 | if (direction == DMA_NONE) |
| 52 | BUG(); |
| 53 | |
| 54 | frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size); |
| 55 | |
| 56 | return virt_to_bus(ptr); |
| 57 | } |
| 58 | |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 59 | EXPORT_SYMBOL(dma_map_single); |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Map a set of buffers described by scatterlist in streaming |
| 63 | * mode for DMA. This is the scather-gather version of the |
David Howells | c9af956 | 2008-10-15 16:50:53 +0100 | [diff] [blame] | 64 | * above dma_map_single interface. Here the scatter gather list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * elements are each tagged with the appropriate dma address |
| 66 | * and length. They are obtained via sg_dma_{address,length}(SG). |
| 67 | * |
| 68 | * NOTE: An implementation may be able to use a smaller number of |
| 69 | * DMA address/length pairs than there are SG table elements. |
| 70 | * (for example via virtual mapping capabilities) |
| 71 | * The routine returns the number of addr/length pairs actually |
| 72 | * used, at most nents. |
| 73 | * |
David Howells | c9af956 | 2008-10-15 16:50:53 +0100 | [diff] [blame] | 74 | * Device ownership issues as mentioned above for dma_map_single are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * the same here. |
| 76 | */ |
| 77 | int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 78 | enum dma_data_direction direction) |
| 79 | { |
| 80 | unsigned long dampr2; |
| 81 | void *vaddr; |
| 82 | int i; |
| 83 | |
| 84 | if (direction == DMA_NONE) |
| 85 | BUG(); |
| 86 | |
| 87 | dampr2 = __get_DAMPR(2); |
| 88 | |
| 89 | for (i = 0; i < nents; i++) { |
Al Viro | 9e6a76b | 2007-10-27 19:23:30 +0100 | [diff] [blame] | 90 | vaddr = kmap_atomic(sg_page(&sg[i]), __KM_CACHE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | frv_dcache_writeback((unsigned long) vaddr, |
| 93 | (unsigned long) vaddr + PAGE_SIZE); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | kunmap_atomic(vaddr, __KM_CACHE); |
| 98 | if (dampr2) { |
| 99 | __set_DAMPR(2, dampr2); |
| 100 | __set_IAMPR(2, dampr2); |
| 101 | } |
| 102 | |
| 103 | return nents; |
| 104 | } |
| 105 | |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 106 | EXPORT_SYMBOL(dma_map_sg); |
| 107 | |
David Howells | c9af956 | 2008-10-15 16:50:53 +0100 | [diff] [blame] | 108 | /* |
| 109 | * Map a single page of the indicated size for DMA in streaming mode. |
| 110 | * The 32-bit bus address to use is returned. |
| 111 | * |
| 112 | * Device ownership issues as mentioned above for dma_map_single are |
| 113 | * the same here. |
| 114 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset, |
| 116 | size_t size, enum dma_data_direction direction) |
| 117 | { |
| 118 | BUG_ON(direction == DMA_NONE); |
| 119 | flush_dcache_page(page); |
| 120 | return (dma_addr_t) page_to_phys(page) + offset; |
| 121 | } |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 122 | |
| 123 | EXPORT_SYMBOL(dma_map_page); |