Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 ARM Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #ifndef __ASM_DMA_MAPPING_H |
| 17 | #define __ASM_DMA_MAPPING_H |
| 18 | |
| 19 | #ifdef __KERNEL__ |
| 20 | |
Suthikulpanit, Suravee | b6197b9 | 2015-06-10 11:08:53 -0500 | [diff] [blame] | 21 | #include <linux/acpi.h> |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 22 | #include <linux/types.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | |
| 25 | #include <asm-generic/dma-coherent.h> |
| 26 | |
Stefano Stabellini | 58c8b26 | 2013-10-18 16:01:32 +0000 | [diff] [blame] | 27 | #include <xen/xen.h> |
| 28 | #include <asm/xen/hypervisor.h> |
| 29 | |
Stefano Stabellini | 25b719d | 2013-10-15 15:49:03 +0000 | [diff] [blame] | 30 | #define DMA_ERROR_CODE (~(dma_addr_t)0) |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 31 | extern struct dma_map_ops *dma_ops; |
Suthikulpanit, Suravee | b6197b9 | 2015-06-10 11:08:53 -0500 | [diff] [blame] | 32 | extern struct dma_map_ops dummy_dma_ops; |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 33 | |
Stefano Stabellini | 58c8b26 | 2013-10-18 16:01:32 +0000 | [diff] [blame] | 34 | static inline struct dma_map_ops *__generic_dma_ops(struct device *dev) |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 35 | { |
Suthikulpanit, Suravee | b6197b9 | 2015-06-10 11:08:53 -0500 | [diff] [blame] | 36 | if (unlikely(!dev)) |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 37 | return dma_ops; |
Suthikulpanit, Suravee | b6197b9 | 2015-06-10 11:08:53 -0500 | [diff] [blame] | 38 | else if (dev->archdata.dma_ops) |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 39 | return dev->archdata.dma_ops; |
Suthikulpanit, Suravee | b6197b9 | 2015-06-10 11:08:53 -0500 | [diff] [blame] | 40 | else if (acpi_disabled) |
| 41 | return dma_ops; |
| 42 | |
| 43 | /* |
| 44 | * When ACPI is enabled, if arch_set_dma_ops is not called, |
| 45 | * we will disable device DMA capability by setting it |
| 46 | * to dummy_dma_ops. |
| 47 | */ |
| 48 | return &dummy_dma_ops; |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Stefano Stabellini | 58c8b26 | 2013-10-18 16:01:32 +0000 | [diff] [blame] | 51 | static inline struct dma_map_ops *get_dma_ops(struct device *dev) |
| 52 | { |
| 53 | if (xen_initial_domain()) |
| 54 | return xen_dma_ops; |
| 55 | else |
| 56 | return __generic_dma_ops(dev); |
| 57 | } |
| 58 | |
Catalin Marinas | 31dde11 | 2014-12-18 17:13:49 +0000 | [diff] [blame] | 59 | static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, |
| 60 | struct iommu_ops *iommu, bool coherent) |
Catalin Marinas | 2189064 | 2014-09-22 11:48:31 +0100 | [diff] [blame] | 61 | { |
Suthikulpanit, Suravee | b6197b9 | 2015-06-10 11:08:53 -0500 | [diff] [blame] | 62 | if (!acpi_disabled && !dev->archdata.dma_ops) |
| 63 | dev->archdata.dma_ops = dma_ops; |
| 64 | |
Catalin Marinas | 31dde11 | 2014-12-18 17:13:49 +0000 | [diff] [blame] | 65 | dev->archdata.dma_coherent = coherent; |
Catalin Marinas | 2189064 | 2014-09-22 11:48:31 +0100 | [diff] [blame] | 66 | } |
Catalin Marinas | 31dde11 | 2014-12-18 17:13:49 +0000 | [diff] [blame] | 67 | #define arch_setup_dma_ops arch_setup_dma_ops |
Catalin Marinas | 2189064 | 2014-09-22 11:48:31 +0100 | [diff] [blame] | 68 | |
Stefano Stabellini | de7ee50 | 2014-11-20 10:41:35 +0000 | [diff] [blame] | 69 | /* do not use this function in a driver */ |
| 70 | static inline bool is_device_dma_coherent(struct device *dev) |
| 71 | { |
Catalin Marinas | 9d3bfbb | 2015-01-12 20:48:53 +0000 | [diff] [blame] | 72 | if (!dev) |
| 73 | return false; |
Stefano Stabellini | de7ee50 | 2014-11-20 10:41:35 +0000 | [diff] [blame] | 74 | return dev->archdata.dma_coherent; |
| 75 | } |
| 76 | |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 77 | #include <asm-generic/dma-mapping-common.h> |
| 78 | |
| 79 | static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) |
| 80 | { |
| 81 | return (dma_addr_t)paddr; |
| 82 | } |
| 83 | |
| 84 | static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr) |
| 85 | { |
| 86 | return (phys_addr_t)dev_addr; |
| 87 | } |
| 88 | |
| 89 | static inline int dma_mapping_error(struct device *dev, dma_addr_t dev_addr) |
| 90 | { |
| 91 | struct dma_map_ops *ops = get_dma_ops(dev); |
Shuah Khan | bb7c4de | 2012-11-23 14:31:09 -0700 | [diff] [blame] | 92 | debug_dma_mapping_error(dev, dev_addr); |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 93 | return ops->mapping_error(dev, dev_addr); |
| 94 | } |
| 95 | |
| 96 | static inline int dma_supported(struct device *dev, u64 mask) |
| 97 | { |
| 98 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 99 | return ops->dma_supported(dev, mask); |
| 100 | } |
| 101 | |
| 102 | static inline int dma_set_mask(struct device *dev, u64 mask) |
| 103 | { |
| 104 | if (!dev->dma_mask || !dma_supported(dev, mask)) |
| 105 | return -EIO; |
| 106 | *dev->dma_mask = mask; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) |
| 112 | { |
| 113 | if (!dev->dma_mask) |
Joe Perches | cc3979b | 2015-03-31 00:46:00 +0100 | [diff] [blame] | 114 | return false; |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 115 | |
| 116 | return addr + size - 1 <= *dev->dma_mask; |
| 117 | } |
| 118 | |
| 119 | static inline void dma_mark_clean(void *addr, size_t size) |
| 120 | { |
| 121 | } |
| 122 | |
Damian Hobson-Garcia | d25749a | 2013-04-30 04:02:13 +0100 | [diff] [blame] | 123 | #define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL) |
| 124 | #define dma_free_coherent(d, s, h, f) dma_free_attrs(d, s, h, f, NULL) |
| 125 | |
| 126 | static inline void *dma_alloc_attrs(struct device *dev, size_t size, |
| 127 | dma_addr_t *dma_handle, gfp_t flags, |
| 128 | struct dma_attrs *attrs) |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 129 | { |
| 130 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 131 | void *vaddr; |
| 132 | |
| 133 | if (dma_alloc_from_coherent(dev, size, dma_handle, &vaddr)) |
| 134 | return vaddr; |
| 135 | |
Damian Hobson-Garcia | d25749a | 2013-04-30 04:02:13 +0100 | [diff] [blame] | 136 | vaddr = ops->alloc(dev, size, dma_handle, flags, attrs); |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 137 | debug_dma_alloc_coherent(dev, size, *dma_handle, vaddr); |
| 138 | return vaddr; |
| 139 | } |
| 140 | |
Damian Hobson-Garcia | d25749a | 2013-04-30 04:02:13 +0100 | [diff] [blame] | 141 | static inline void dma_free_attrs(struct device *dev, size_t size, |
| 142 | void *vaddr, dma_addr_t dev_addr, |
| 143 | struct dma_attrs *attrs) |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 144 | { |
| 145 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 146 | |
| 147 | if (dma_release_from_coherent(dev, get_order(size), vaddr)) |
| 148 | return; |
| 149 | |
| 150 | debug_dma_free_coherent(dev, size, vaddr, dev_addr); |
Damian Hobson-Garcia | d25749a | 2013-04-30 04:02:13 +0100 | [diff] [blame] | 151 | ops->free(dev, size, vaddr, dev_addr, attrs); |
Catalin Marinas | 09b5541 | 2012-03-05 11:49:30 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * There is no dma_cache_sync() implementation, so just return NULL here. |
| 156 | */ |
| 157 | static inline void *dma_alloc_noncoherent(struct device *dev, size_t size, |
| 158 | dma_addr_t *handle, gfp_t flags) |
| 159 | { |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | static inline void dma_free_noncoherent(struct device *dev, size_t size, |
| 164 | void *cpu_addr, dma_addr_t handle) |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | #endif /* __KERNEL__ */ |
| 169 | #endif /* __ASM_DMA_MAPPING_H */ |