David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 1 | /* dma.c: PCI and SBUS DMA accessors for 32-bit sparc. |
| 2 | * |
| 3 | * Copyright (C) 2008 David S. Miller <davem@davemloft.net> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/dma-mapping.h> |
| 9 | #include <linux/scatterlist.h> |
| 10 | #include <linux/mm.h> |
| 11 | |
| 12 | #ifdef CONFIG_PCI |
| 13 | #include <linux/pci.h> |
| 14 | #endif |
| 15 | |
| 16 | #include "dma.h" |
| 17 | |
| 18 | int dma_supported(struct device *dev, u64 mask) |
| 19 | { |
| 20 | #ifdef CONFIG_PCI |
| 21 | if (dev->bus == &pci_bus_type) |
| 22 | return pci_dma_supported(to_pci_dev(dev), mask); |
| 23 | #endif |
| 24 | return 0; |
| 25 | } |
| 26 | EXPORT_SYMBOL(dma_supported); |
| 27 | |
| 28 | int dma_set_mask(struct device *dev, u64 dma_mask) |
| 29 | { |
| 30 | #ifdef CONFIG_PCI |
| 31 | if (dev->bus == &pci_bus_type) |
| 32 | return pci_set_dma_mask(to_pci_dev(dev), dma_mask); |
| 33 | #endif |
| 34 | return -EOPNOTSUPP; |
| 35 | } |
| 36 | EXPORT_SYMBOL(dma_set_mask); |
| 37 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 38 | static void *dma32_alloc_coherent(struct device *dev, size_t size, |
| 39 | dma_addr_t *dma_handle, gfp_t flag) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 40 | { |
| 41 | #ifdef CONFIG_PCI |
| 42 | if (dev->bus == &pci_bus_type) |
| 43 | return pci_alloc_consistent(to_pci_dev(dev), size, dma_handle); |
| 44 | #endif |
| 45 | return sbus_alloc_consistent(dev, size, dma_handle); |
| 46 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 47 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 48 | static void dma32_free_coherent(struct device *dev, size_t size, |
| 49 | void *cpu_addr, dma_addr_t dma_handle) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 50 | { |
| 51 | #ifdef CONFIG_PCI |
| 52 | if (dev->bus == &pci_bus_type) { |
| 53 | pci_free_consistent(to_pci_dev(dev), size, |
| 54 | cpu_addr, dma_handle); |
| 55 | return; |
| 56 | } |
| 57 | #endif |
| 58 | sbus_free_consistent(dev, size, cpu_addr, dma_handle); |
| 59 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 60 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 61 | static dma_addr_t dma32_map_page(struct device *dev, struct page *page, |
| 62 | unsigned long offset, size_t size, |
| 63 | enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 64 | { |
| 65 | #ifdef CONFIG_PCI |
| 66 | if (dev->bus == &pci_bus_type) |
| 67 | return pci_map_page(to_pci_dev(dev), page, offset, |
| 68 | size, (int)direction); |
| 69 | #endif |
| 70 | return sbus_map_single(dev, page_address(page) + offset, |
| 71 | size, (int)direction); |
| 72 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 73 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 74 | static void dma32_unmap_page(struct device *dev, dma_addr_t dma_address, |
| 75 | size_t size, enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 76 | { |
| 77 | #ifdef CONFIG_PCI |
| 78 | if (dev->bus == &pci_bus_type) { |
| 79 | pci_unmap_page(to_pci_dev(dev), dma_address, |
| 80 | size, (int)direction); |
| 81 | return; |
| 82 | } |
| 83 | #endif |
| 84 | sbus_unmap_single(dev, dma_address, size, (int)direction); |
| 85 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 86 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 87 | static int dma32_map_sg(struct device *dev, struct scatterlist *sg, |
| 88 | int nents, enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 89 | { |
| 90 | #ifdef CONFIG_PCI |
| 91 | if (dev->bus == &pci_bus_type) |
| 92 | return pci_map_sg(to_pci_dev(dev), sg, nents, (int)direction); |
| 93 | #endif |
| 94 | return sbus_map_sg(dev, sg, nents, direction); |
| 95 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 96 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 97 | void dma32_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 98 | int nents, enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 99 | { |
| 100 | #ifdef CONFIG_PCI |
| 101 | if (dev->bus == &pci_bus_type) { |
| 102 | pci_unmap_sg(to_pci_dev(dev), sg, nents, (int)direction); |
| 103 | return; |
| 104 | } |
| 105 | #endif |
| 106 | sbus_unmap_sg(dev, sg, nents, (int)direction); |
| 107 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 108 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 109 | static void dma32_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, |
| 110 | size_t size, |
| 111 | enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 112 | { |
| 113 | #ifdef CONFIG_PCI |
| 114 | if (dev->bus == &pci_bus_type) { |
| 115 | pci_dma_sync_single_for_cpu(to_pci_dev(dev), dma_handle, |
| 116 | size, (int)direction); |
| 117 | return; |
| 118 | } |
| 119 | #endif |
| 120 | sbus_dma_sync_single_for_cpu(dev, dma_handle, size, (int) direction); |
| 121 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 122 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 123 | static void dma32_sync_single_for_device(struct device *dev, |
| 124 | dma_addr_t dma_handle, size_t size, |
| 125 | enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 126 | { |
| 127 | #ifdef CONFIG_PCI |
| 128 | if (dev->bus == &pci_bus_type) { |
| 129 | pci_dma_sync_single_for_device(to_pci_dev(dev), dma_handle, |
| 130 | size, (int)direction); |
| 131 | return; |
| 132 | } |
| 133 | #endif |
| 134 | sbus_dma_sync_single_for_device(dev, dma_handle, size, (int) direction); |
| 135 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 136 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 137 | static void dma32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
| 138 | int nelems, enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 139 | { |
| 140 | #ifdef CONFIG_PCI |
| 141 | if (dev->bus == &pci_bus_type) { |
| 142 | pci_dma_sync_sg_for_cpu(to_pci_dev(dev), sg, |
| 143 | nelems, (int)direction); |
| 144 | return; |
| 145 | } |
| 146 | #endif |
| 147 | BUG(); |
| 148 | } |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 149 | |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 150 | static void dma32_sync_sg_for_device(struct device *dev, |
| 151 | struct scatterlist *sg, int nelems, |
| 152 | enum dma_data_direction direction) |
David S. Miller | 944c67df | 2008-08-27 18:01:36 -0700 | [diff] [blame] | 153 | { |
| 154 | #ifdef CONFIG_PCI |
| 155 | if (dev->bus == &pci_bus_type) { |
| 156 | pci_dma_sync_sg_for_device(to_pci_dev(dev), sg, |
| 157 | nelems, (int)direction); |
| 158 | return; |
| 159 | } |
| 160 | #endif |
| 161 | BUG(); |
| 162 | } |
FUJITA Tomonori | d698641 | 2009-05-14 16:23:11 +0000 | [diff] [blame^] | 163 | |
| 164 | static const struct dma_ops dma32_dma_ops = { |
| 165 | .alloc_coherent = dma32_alloc_coherent, |
| 166 | .free_coherent = dma32_free_coherent, |
| 167 | .map_page = dma32_map_page, |
| 168 | .unmap_page = dma32_unmap_page, |
| 169 | .map_sg = dma32_map_sg, |
| 170 | .unmap_sg = dma32_unmap_sg, |
| 171 | .sync_single_for_cpu = dma32_sync_single_for_cpu, |
| 172 | .sync_single_for_device = dma32_sync_single_for_device, |
| 173 | .sync_sg_for_cpu = dma32_sync_sg_for_cpu, |
| 174 | .sync_sg_for_device = dma32_sync_sg_for_device, |
| 175 | }; |
| 176 | |
| 177 | const struct dma_ops *dma_ops = &dma32_dma_ops; |
| 178 | EXPORT_SYMBOL(dma_ops); |