Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 2 | * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 4 | * Provide default implementations of the DMA mapping callbacks for |
Becky Bruce | 8dd0e95 | 2008-09-08 09:09:53 +0000 | [diff] [blame] | 5 | * directly mapped busses. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/device.h> |
| 9 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <asm/bug.h> |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 11 | #include <asm/abs_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 13 | /* |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 14 | * Generic direct DMA implementation |
Benjamin Herrenschmidt | 92b20c4 | 2006-11-11 17:25:14 +1100 | [diff] [blame] | 15 | * |
Michael Ellerman | 31d1b49 | 2008-01-21 16:42:48 +1100 | [diff] [blame] | 16 | * This implementation supports a per-device offset that can be applied if |
| 17 | * the address at which memory is visible to devices is not 0. Platform code |
| 18 | * can set archdata.dma_data to an unsigned long holding the offset. By |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 19 | * default the offset is PCI_DRAM_OFFSET. |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 20 | */ |
| 21 | |
Michael Ellerman | 35e4a6e | 2008-01-21 16:42:43 +1100 | [diff] [blame] | 22 | static unsigned long get_dma_direct_offset(struct device *dev) |
| 23 | { |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 24 | if (dev) |
| 25 | return (unsigned long)dev->archdata.dma_data; |
| 26 | |
| 27 | return PCI_DRAM_OFFSET; |
Michael Ellerman | 35e4a6e | 2008-01-21 16:42:43 +1100 | [diff] [blame] | 28 | } |
| 29 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 30 | void *dma_direct_alloc_coherent(struct device *dev, size_t size, |
| 31 | dma_addr_t *dma_handle, gfp_t flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Benjamin Herrenschmidt | 8aa2659 | 2008-10-09 17:06:24 +0000 | [diff] [blame^] | 33 | void *ret; |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_NOT_COHERENT_CACHE |
Benjamin Herrenschmidt | 8aa2659 | 2008-10-09 17:06:24 +0000 | [diff] [blame^] | 35 | ret = __dma_alloc_coherent(size, dma_handle, flag); |
| 36 | if (ret == NULL) |
| 37 | return NULL; |
| 38 | *dma_handle += get_dma_direct_offset(dev); |
| 39 | return ret; |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 40 | #else |
Benjamin Herrenschmidt | c80d913 | 2006-11-11 17:25:16 +1100 | [diff] [blame] | 41 | struct page *page; |
Becky Bruce | 8fae035 | 2008-09-08 09:09:54 +0000 | [diff] [blame] | 42 | int node = dev_to_node(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 44 | /* ignore region specifiers */ |
| 45 | flag &= ~(__GFP_HIGHMEM); |
| 46 | |
Benjamin Herrenschmidt | c80d913 | 2006-11-11 17:25:16 +1100 | [diff] [blame] | 47 | page = alloc_pages_node(node, flag, get_order(size)); |
| 48 | if (page == NULL) |
| 49 | return NULL; |
| 50 | ret = page_address(page); |
| 51 | memset(ret, 0, size); |
Michael Ellerman | 35e4a6e | 2008-01-21 16:42:43 +1100 | [diff] [blame] | 52 | *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev); |
Benjamin Herrenschmidt | c80d913 | 2006-11-11 17:25:16 +1100 | [diff] [blame] | 53 | |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 54 | return ret; |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 55 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 58 | void dma_direct_free_coherent(struct device *dev, size_t size, |
| 59 | void *vaddr, dma_addr_t dma_handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_NOT_COHERENT_CACHE |
| 62 | __dma_free_coherent(size, vaddr); |
| 63 | #else |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 64 | free_pages((unsigned long)vaddr, get_order(size)); |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 65 | #endif |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 66 | } |
| 67 | |
Jens Axboe | 78bdc31 | 2007-10-12 13:44:12 +0200 | [diff] [blame] | 68 | static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 69 | int nents, enum dma_data_direction direction, |
| 70 | struct dma_attrs *attrs) |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 71 | { |
Jens Axboe | 78bdc31 | 2007-10-12 13:44:12 +0200 | [diff] [blame] | 72 | struct scatterlist *sg; |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 73 | int i; |
| 74 | |
Jens Axboe | 78bdc31 | 2007-10-12 13:44:12 +0200 | [diff] [blame] | 75 | for_each_sg(sgl, sg, nents, i) { |
Michael Ellerman | 35e4a6e | 2008-01-21 16:42:43 +1100 | [diff] [blame] | 76 | sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev); |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 77 | sg->dma_length = sg->length; |
| 78 | } |
| 79 | |
| 80 | return nents; |
| 81 | } |
| 82 | |
| 83 | static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg, |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 84 | int nents, enum dma_data_direction direction, |
| 85 | struct dma_attrs *attrs) |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
| 89 | static int dma_direct_dma_supported(struct device *dev, u64 mask) |
| 90 | { |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 91 | #ifdef CONFIG_PPC64 |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 92 | /* Could be improved to check for memory though it better be |
| 93 | * done via some global so platforms can set the limit in case |
| 94 | * they have limited DMA windows |
| 95 | */ |
| 96 | return mask >= DMA_32BIT_MASK; |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 97 | #else |
| 98 | return 1; |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | static inline dma_addr_t dma_direct_map_page(struct device *dev, |
| 103 | struct page *page, |
| 104 | unsigned long offset, |
| 105 | size_t size, |
| 106 | enum dma_data_direction dir, |
| 107 | struct dma_attrs *attrs) |
| 108 | { |
| 109 | BUG_ON(dir == DMA_NONE); |
| 110 | __dma_sync_page(page, offset, size, dir); |
| 111 | return page_to_phys(page) + offset + get_dma_direct_offset(dev); |
| 112 | } |
| 113 | |
| 114 | static inline void dma_direct_unmap_page(struct device *dev, |
| 115 | dma_addr_t dma_address, |
| 116 | size_t size, |
| 117 | enum dma_data_direction direction, |
| 118 | struct dma_attrs *attrs) |
| 119 | { |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | struct dma_mapping_ops dma_direct_ops = { |
| 123 | .alloc_coherent = dma_direct_alloc_coherent, |
| 124 | .free_coherent = dma_direct_free_coherent, |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 125 | .map_sg = dma_direct_map_sg, |
| 126 | .unmap_sg = dma_direct_unmap_sg, |
| 127 | .dma_supported = dma_direct_dma_supported, |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 128 | .map_page = dma_direct_map_page, |
| 129 | .unmap_page = dma_direct_unmap_page, |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 130 | }; |
| 131 | EXPORT_SYMBOL(dma_direct_ops); |