Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Author: Joerg Roedel <joerg.roedel@amd.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 20 | #include <linux/scatterlist.h> |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 21 | #include <linux/dma-mapping.h> |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 22 | #include <linux/stacktrace.h> |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 23 | #include <linux/dma-debug.h> |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Paul Gortmaker | 23a7bfa | 2011-07-01 16:23:59 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 28 | #include <linux/device.h> |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 29 | #include <linux/types.h> |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 30 | #include <linux/sched.h> |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 32 | #include <linux/list.h> |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 33 | #include <linux/slab.h> |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 34 | |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 35 | #include <asm/sections.h> |
| 36 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 37 | #define HASH_SIZE 1024ULL |
| 38 | #define HASH_FN_SHIFT 13 |
| 39 | #define HASH_FN_MASK (HASH_SIZE - 1) |
| 40 | |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 41 | enum { |
| 42 | dma_debug_single, |
| 43 | dma_debug_page, |
| 44 | dma_debug_sg, |
| 45 | dma_debug_coherent, |
| 46 | }; |
| 47 | |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 48 | enum map_err_types { |
| 49 | MAP_ERR_CHECK_NOT_APPLICABLE, |
| 50 | MAP_ERR_NOT_CHECKED, |
| 51 | MAP_ERR_CHECKED, |
| 52 | }; |
| 53 | |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 54 | #define DMA_DEBUG_STACKTRACE_ENTRIES 5 |
| 55 | |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 56 | /** |
| 57 | * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping |
| 58 | * @list: node on pre-allocated free_entries list |
| 59 | * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent |
| 60 | * @type: single, page, sg, coherent |
| 61 | * @pfn: page frame of the start address |
| 62 | * @offset: offset of mapping relative to pfn |
| 63 | * @size: length of the mapping |
| 64 | * @direction: enum dma_data_direction |
| 65 | * @sg_call_ents: 'nents' from dma_map_sg |
| 66 | * @sg_mapped_ents: 'mapped_ents' from dma_map_sg |
| 67 | * @map_err_type: track whether dma_mapping_error() was checked |
| 68 | * @stacktrace: support backtraces when a violation is detected |
| 69 | */ |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 70 | struct dma_debug_entry { |
| 71 | struct list_head list; |
| 72 | struct device *dev; |
| 73 | int type; |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 74 | unsigned long pfn; |
| 75 | size_t offset; |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 76 | u64 dev_addr; |
| 77 | u64 size; |
| 78 | int direction; |
| 79 | int sg_call_ents; |
| 80 | int sg_mapped_ents; |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 81 | enum map_err_types map_err_type; |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 82 | #ifdef CONFIG_STACKTRACE |
| 83 | struct stack_trace stacktrace; |
| 84 | unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES]; |
| 85 | #endif |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 88 | typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *); |
| 89 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 90 | struct hash_bucket { |
| 91 | struct list_head list; |
| 92 | spinlock_t lock; |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 93 | } ____cacheline_aligned_in_smp; |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 94 | |
| 95 | /* Hash list to save the allocated dma addresses */ |
| 96 | static struct hash_bucket dma_entry_hash[HASH_SIZE]; |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 97 | /* List of pre-allocated dma_debug_entry's */ |
| 98 | static LIST_HEAD(free_entries); |
| 99 | /* Lock for the list above */ |
| 100 | static DEFINE_SPINLOCK(free_entries_lock); |
| 101 | |
| 102 | /* Global disable flag - will be set in case of an error */ |
Dan Carpenter | 68ee6d2 | 2012-06-27 12:08:55 +0300 | [diff] [blame] | 103 | static u32 global_disable __read_mostly; |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 104 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 105 | /* Global error count */ |
| 106 | static u32 error_count; |
| 107 | |
| 108 | /* Global error show enable*/ |
| 109 | static u32 show_all_errors __read_mostly; |
| 110 | /* Number of errors to show */ |
| 111 | static u32 show_num_errors = 1; |
| 112 | |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 113 | static u32 num_free_entries; |
| 114 | static u32 min_free_entries; |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 115 | static u32 nr_total_entries; |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 116 | |
Joerg Roedel | 59d3daa | 2009-01-09 13:01:56 +0100 | [diff] [blame] | 117 | /* number of preallocated entries requested by kernel cmdline */ |
| 118 | static u32 req_entries; |
| 119 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 120 | /* debugfs dentry's for the stuff above */ |
| 121 | static struct dentry *dma_debug_dent __read_mostly; |
| 122 | static struct dentry *global_disable_dent __read_mostly; |
| 123 | static struct dentry *error_count_dent __read_mostly; |
| 124 | static struct dentry *show_all_errors_dent __read_mostly; |
| 125 | static struct dentry *show_num_errors_dent __read_mostly; |
| 126 | static struct dentry *num_free_entries_dent __read_mostly; |
| 127 | static struct dentry *min_free_entries_dent __read_mostly; |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 128 | static struct dentry *filter_dent __read_mostly; |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 129 | |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 130 | /* per-driver filter related state */ |
| 131 | |
| 132 | #define NAME_MAX_LEN 64 |
| 133 | |
| 134 | static char current_driver_name[NAME_MAX_LEN] __read_mostly; |
| 135 | static struct device_driver *current_driver __read_mostly; |
| 136 | |
| 137 | static DEFINE_RWLOCK(driver_name_lock); |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 138 | |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 139 | static const char *const maperr2str[] = { |
| 140 | [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable", |
| 141 | [MAP_ERR_NOT_CHECKED] = "dma map error not checked", |
| 142 | [MAP_ERR_CHECKED] = "dma map error checked", |
| 143 | }; |
| 144 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 145 | static const char *type2name[4] = { "single", "page", |
| 146 | "scather-gather", "coherent" }; |
| 147 | |
| 148 | static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE", |
| 149 | "DMA_FROM_DEVICE", "DMA_NONE" }; |
| 150 | |
| 151 | /* |
| 152 | * The access to some variables in this macro is racy. We can't use atomic_t |
| 153 | * here because all these variables are exported to debugfs. Some of them even |
| 154 | * writeable. This is also the reason why a lock won't help much. But anyway, |
| 155 | * the races are no big deal. Here is why: |
| 156 | * |
| 157 | * error_count: the addition is racy, but the worst thing that can happen is |
| 158 | * that we don't count some errors |
| 159 | * show_num_errors: the subtraction is racy. Also no big deal because in |
| 160 | * worst case this will result in one warning more in the |
| 161 | * system log than the user configured. This variable is |
| 162 | * writeable via debugfs. |
| 163 | */ |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 164 | static inline void dump_entry_trace(struct dma_debug_entry *entry) |
| 165 | { |
| 166 | #ifdef CONFIG_STACKTRACE |
| 167 | if (entry) { |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 168 | pr_warning("Mapped at:\n"); |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 169 | print_stack_trace(&entry->stacktrace, 0); |
| 170 | } |
| 171 | #endif |
| 172 | } |
| 173 | |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 174 | static bool driver_filter(struct device *dev) |
| 175 | { |
Joerg Roedel | 0bf8412 | 2009-06-08 15:53:46 +0200 | [diff] [blame] | 176 | struct device_driver *drv; |
| 177 | unsigned long flags; |
| 178 | bool ret; |
| 179 | |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 180 | /* driver filter off */ |
| 181 | if (likely(!current_driver_name[0])) |
| 182 | return true; |
| 183 | |
| 184 | /* driver filter on and initialized */ |
Kyle McMartin | ec9c96e | 2009-08-19 21:17:08 -0400 | [diff] [blame] | 185 | if (current_driver && dev && dev->driver == current_driver) |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 186 | return true; |
| 187 | |
Kyle McMartin | ec9c96e | 2009-08-19 21:17:08 -0400 | [diff] [blame] | 188 | /* driver filter on, but we can't filter on a NULL device... */ |
| 189 | if (!dev) |
| 190 | return false; |
| 191 | |
Joerg Roedel | 0bf8412 | 2009-06-08 15:53:46 +0200 | [diff] [blame] | 192 | if (current_driver || !current_driver_name[0]) |
| 193 | return false; |
| 194 | |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 195 | /* driver filter on but not yet initialized */ |
Alan Stern | f3ff924 | 2012-01-24 13:35:24 -0500 | [diff] [blame] | 196 | drv = dev->driver; |
Joerg Roedel | 0bf8412 | 2009-06-08 15:53:46 +0200 | [diff] [blame] | 197 | if (!drv) |
| 198 | return false; |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 199 | |
Joerg Roedel | 0bf8412 | 2009-06-08 15:53:46 +0200 | [diff] [blame] | 200 | /* lock to protect against change of current_driver_name */ |
| 201 | read_lock_irqsave(&driver_name_lock, flags); |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 202 | |
Joerg Roedel | 0bf8412 | 2009-06-08 15:53:46 +0200 | [diff] [blame] | 203 | ret = false; |
| 204 | if (drv->name && |
| 205 | strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) { |
| 206 | current_driver = drv; |
| 207 | ret = true; |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Joerg Roedel | 0bf8412 | 2009-06-08 15:53:46 +0200 | [diff] [blame] | 210 | read_unlock_irqrestore(&driver_name_lock, flags); |
Joerg Roedel | 0bf8412 | 2009-06-08 15:53:46 +0200 | [diff] [blame] | 211 | |
| 212 | return ret; |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Kyle McMartin | ec9c96e | 2009-08-19 21:17:08 -0400 | [diff] [blame] | 215 | #define err_printk(dev, entry, format, arg...) do { \ |
| 216 | error_count += 1; \ |
| 217 | if (driver_filter(dev) && \ |
| 218 | (show_all_errors || show_num_errors > 0)) { \ |
| 219 | WARN(1, "%s %s: " format, \ |
| 220 | dev ? dev_driver_string(dev) : "NULL", \ |
| 221 | dev ? dev_name(dev) : "NULL", ## arg); \ |
| 222 | dump_entry_trace(entry); \ |
| 223 | } \ |
| 224 | if (!show_all_errors && show_num_errors > 0) \ |
| 225 | show_num_errors -= 1; \ |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 226 | } while (0); |
| 227 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 228 | /* |
| 229 | * Hash related functions |
| 230 | * |
| 231 | * Every DMA-API request is saved into a struct dma_debug_entry. To |
| 232 | * have quick access to these structs they are stored into a hash. |
| 233 | */ |
| 234 | static int hash_fn(struct dma_debug_entry *entry) |
| 235 | { |
| 236 | /* |
| 237 | * Hash function is based on the dma address. |
| 238 | * We use bits 20-27 here as the index into the hash |
| 239 | */ |
| 240 | return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Request exclusive access to a hash bucket for a given dma_debug_entry. |
| 245 | */ |
| 246 | static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry, |
| 247 | unsigned long *flags) |
| 248 | { |
| 249 | int idx = hash_fn(entry); |
| 250 | unsigned long __flags; |
| 251 | |
| 252 | spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags); |
| 253 | *flags = __flags; |
| 254 | return &dma_entry_hash[idx]; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Give up exclusive access to the hash bucket |
| 259 | */ |
| 260 | static void put_hash_bucket(struct hash_bucket *bucket, |
| 261 | unsigned long *flags) |
| 262 | { |
| 263 | unsigned long __flags = *flags; |
| 264 | |
| 265 | spin_unlock_irqrestore(&bucket->lock, __flags); |
| 266 | } |
| 267 | |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 268 | static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b) |
| 269 | { |
Thomas Jarosch | 91ec37c | 2011-11-17 20:31:02 +0100 | [diff] [blame] | 270 | return ((a->dev_addr == b->dev_addr) && |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 271 | (a->dev == b->dev)) ? true : false; |
| 272 | } |
| 273 | |
| 274 | static bool containing_match(struct dma_debug_entry *a, |
| 275 | struct dma_debug_entry *b) |
| 276 | { |
| 277 | if (a->dev != b->dev) |
| 278 | return false; |
| 279 | |
| 280 | if ((b->dev_addr <= a->dev_addr) && |
| 281 | ((b->dev_addr + b->size) >= (a->dev_addr + a->size))) |
| 282 | return true; |
| 283 | |
| 284 | return false; |
| 285 | } |
| 286 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 287 | /* |
| 288 | * Search a given entry in the hash bucket list |
| 289 | */ |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 290 | static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket, |
| 291 | struct dma_debug_entry *ref, |
| 292 | match_fn match) |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 293 | { |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 294 | struct dma_debug_entry *entry, *ret = NULL; |
Ming Lei | fe73fbe | 2012-10-19 13:57:01 -0700 | [diff] [blame] | 295 | int matches = 0, match_lvl, last_lvl = -1; |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 296 | |
| 297 | list_for_each_entry(entry, &bucket->list, list) { |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 298 | if (!match(ref, entry)) |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 299 | continue; |
| 300 | |
| 301 | /* |
| 302 | * Some drivers map the same physical address multiple |
| 303 | * times. Without a hardware IOMMU this results in the |
| 304 | * same device addresses being put into the dma-debug |
| 305 | * hash multiple times too. This can result in false |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 306 | * positives being reported. Therefore we implement a |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 307 | * best-fit algorithm here which returns the entry from |
| 308 | * the hash which fits best to the reference value |
| 309 | * instead of the first-fit. |
| 310 | */ |
| 311 | matches += 1; |
| 312 | match_lvl = 0; |
Joerg Roedel | e5e8c5b | 2009-06-11 10:03:42 +0200 | [diff] [blame] | 313 | entry->size == ref->size ? ++match_lvl : 0; |
| 314 | entry->type == ref->type ? ++match_lvl : 0; |
| 315 | entry->direction == ref->direction ? ++match_lvl : 0; |
| 316 | entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0; |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 317 | |
Joerg Roedel | e5e8c5b | 2009-06-11 10:03:42 +0200 | [diff] [blame] | 318 | if (match_lvl == 4) { |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 319 | /* perfect-fit - return the result */ |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 320 | return entry; |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 321 | } else if (match_lvl > last_lvl) { |
| 322 | /* |
| 323 | * We found an entry that fits better then the |
Ming Lei | fe73fbe | 2012-10-19 13:57:01 -0700 | [diff] [blame] | 324 | * previous one or it is the 1st match. |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 325 | */ |
| 326 | last_lvl = match_lvl; |
| 327 | ret = entry; |
| 328 | } |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Joerg Roedel | 7caf6a4 | 2009-06-05 12:01:35 +0200 | [diff] [blame] | 331 | /* |
| 332 | * If we have multiple matches but no perfect-fit, just return |
| 333 | * NULL. |
| 334 | */ |
| 335 | ret = (matches == 1) ? ret : NULL; |
| 336 | |
| 337 | return ret; |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 340 | static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket, |
| 341 | struct dma_debug_entry *ref) |
| 342 | { |
| 343 | return __hash_bucket_find(bucket, ref, exact_match); |
| 344 | } |
| 345 | |
| 346 | static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket, |
| 347 | struct dma_debug_entry *ref, |
| 348 | unsigned long *flags) |
| 349 | { |
| 350 | |
| 351 | unsigned int max_range = dma_get_max_seg_size(ref->dev); |
| 352 | struct dma_debug_entry *entry, index = *ref; |
| 353 | unsigned int range = 0; |
| 354 | |
| 355 | while (range <= max_range) { |
| 356 | entry = __hash_bucket_find(*bucket, &index, containing_match); |
| 357 | |
| 358 | if (entry) |
| 359 | return entry; |
| 360 | |
| 361 | /* |
| 362 | * Nothing found, go back a hash bucket |
| 363 | */ |
| 364 | put_hash_bucket(*bucket, flags); |
| 365 | range += (1 << HASH_FN_SHIFT); |
| 366 | index.dev_addr -= (1 << HASH_FN_SHIFT); |
| 367 | *bucket = get_hash_bucket(&index, flags); |
| 368 | } |
| 369 | |
| 370 | return NULL; |
| 371 | } |
| 372 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 373 | /* |
| 374 | * Add an entry to a hash bucket |
| 375 | */ |
| 376 | static void hash_bucket_add(struct hash_bucket *bucket, |
| 377 | struct dma_debug_entry *entry) |
| 378 | { |
| 379 | list_add_tail(&entry->list, &bucket->list); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Remove entry from a hash bucket list |
| 384 | */ |
| 385 | static void hash_bucket_del(struct dma_debug_entry *entry) |
| 386 | { |
| 387 | list_del(&entry->list); |
| 388 | } |
| 389 | |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 390 | static unsigned long long phys_addr(struct dma_debug_entry *entry) |
| 391 | { |
| 392 | return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset; |
| 393 | } |
| 394 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 395 | /* |
David Woodhouse | ac26c18 | 2009-02-12 16:19:13 +0100 | [diff] [blame] | 396 | * Dump mapping entries for debugging purposes |
| 397 | */ |
| 398 | void debug_dma_dump_mappings(struct device *dev) |
| 399 | { |
| 400 | int idx; |
| 401 | |
| 402 | for (idx = 0; idx < HASH_SIZE; idx++) { |
| 403 | struct hash_bucket *bucket = &dma_entry_hash[idx]; |
| 404 | struct dma_debug_entry *entry; |
| 405 | unsigned long flags; |
| 406 | |
| 407 | spin_lock_irqsave(&bucket->lock, flags); |
| 408 | |
| 409 | list_for_each_entry(entry, &bucket->list, list) { |
| 410 | if (!dev || dev == entry->dev) { |
| 411 | dev_info(entry->dev, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 412 | "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n", |
David Woodhouse | ac26c18 | 2009-02-12 16:19:13 +0100 | [diff] [blame] | 413 | type2name[entry->type], idx, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 414 | phys_addr(entry), entry->pfn, |
David Woodhouse | ac26c18 | 2009-02-12 16:19:13 +0100 | [diff] [blame] | 415 | entry->dev_addr, entry->size, |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 416 | dir2name[entry->direction], |
| 417 | maperr2str[entry->map_err_type]); |
David Woodhouse | ac26c18 | 2009-02-12 16:19:13 +0100 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | spin_unlock_irqrestore(&bucket->lock, flags); |
| 422 | } |
| 423 | } |
| 424 | EXPORT_SYMBOL(debug_dma_dump_mappings); |
| 425 | |
| 426 | /* |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 427 | * For each page mapped (initial page in the case of |
| 428 | * dma_alloc_coherent/dma_map_{single|page}, or each page in a |
| 429 | * scatterlist) insert into this tree using the pfn as the key. At |
| 430 | * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If |
| 431 | * the pfn already exists at insertion time add a tag as a reference |
| 432 | * count for the overlapping mappings. For now, the overlap tracking |
| 433 | * just ensures that 'unmaps' balance 'maps' before marking the pfn |
| 434 | * idle, but we should also be flagging overlaps as an API violation. |
| 435 | * |
| 436 | * Memory usage is mostly constrained by the maximum number of available |
| 437 | * dma-debug entries in that we need a free dma_debug_entry before |
| 438 | * inserting into the tree. In the case of dma_map_{single|page} and |
| 439 | * dma_alloc_coherent there is only one dma_debug_entry and one pfn to |
| 440 | * track per event. dma_map_sg(), on the other hand, |
| 441 | * consumes a single dma_debug_entry, but inserts 'nents' entries into |
| 442 | * the tree. |
| 443 | * |
| 444 | * At any time debug_dma_assert_idle() can be called to trigger a |
| 445 | * warning if the given page is in the active set. |
| 446 | */ |
| 447 | static RADIX_TREE(dma_active_pfn, GFP_NOWAIT); |
| 448 | static DEFINE_SPINLOCK(radix_lock); |
| 449 | #define ACTIVE_PFN_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1) |
| 450 | |
| 451 | static int active_pfn_read_overlap(unsigned long pfn) |
| 452 | { |
| 453 | int overlap = 0, i; |
| 454 | |
| 455 | for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) |
| 456 | if (radix_tree_tag_get(&dma_active_pfn, pfn, i)) |
| 457 | overlap |= 1 << i; |
| 458 | return overlap; |
| 459 | } |
| 460 | |
| 461 | static int active_pfn_set_overlap(unsigned long pfn, int overlap) |
| 462 | { |
| 463 | int i; |
| 464 | |
| 465 | if (overlap > ACTIVE_PFN_MAX_OVERLAP || overlap < 0) |
Dan Williams | 59f2e7d | 2014-01-29 14:05:53 -0800 | [diff] [blame] | 466 | return overlap; |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 467 | |
| 468 | for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) |
| 469 | if (overlap & 1 << i) |
| 470 | radix_tree_tag_set(&dma_active_pfn, pfn, i); |
| 471 | else |
| 472 | radix_tree_tag_clear(&dma_active_pfn, pfn, i); |
| 473 | |
| 474 | return overlap; |
| 475 | } |
| 476 | |
| 477 | static void active_pfn_inc_overlap(unsigned long pfn) |
| 478 | { |
| 479 | int overlap = active_pfn_read_overlap(pfn); |
| 480 | |
| 481 | overlap = active_pfn_set_overlap(pfn, ++overlap); |
| 482 | |
| 483 | /* If we overflowed the overlap counter then we're potentially |
| 484 | * leaking dma-mappings. Otherwise, if maps and unmaps are |
| 485 | * balanced then this overflow may cause false negatives in |
| 486 | * debug_dma_assert_idle() as the pfn may be marked idle |
| 487 | * prematurely. |
| 488 | */ |
Dan Williams | 59f2e7d | 2014-01-29 14:05:53 -0800 | [diff] [blame] | 489 | WARN_ONCE(overlap > ACTIVE_PFN_MAX_OVERLAP, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 490 | "DMA-API: exceeded %d overlapping mappings of pfn %lx\n", |
| 491 | ACTIVE_PFN_MAX_OVERLAP, pfn); |
| 492 | } |
| 493 | |
| 494 | static int active_pfn_dec_overlap(unsigned long pfn) |
| 495 | { |
| 496 | int overlap = active_pfn_read_overlap(pfn); |
| 497 | |
| 498 | return active_pfn_set_overlap(pfn, --overlap); |
| 499 | } |
| 500 | |
| 501 | static int active_pfn_insert(struct dma_debug_entry *entry) |
| 502 | { |
| 503 | unsigned long flags; |
| 504 | int rc; |
| 505 | |
| 506 | spin_lock_irqsave(&radix_lock, flags); |
| 507 | rc = radix_tree_insert(&dma_active_pfn, entry->pfn, entry); |
| 508 | if (rc == -EEXIST) |
| 509 | active_pfn_inc_overlap(entry->pfn); |
| 510 | spin_unlock_irqrestore(&radix_lock, flags); |
| 511 | |
| 512 | return rc; |
| 513 | } |
| 514 | |
| 515 | static void active_pfn_remove(struct dma_debug_entry *entry) |
| 516 | { |
| 517 | unsigned long flags; |
| 518 | |
| 519 | spin_lock_irqsave(&radix_lock, flags); |
Dan Williams | 59f2e7d | 2014-01-29 14:05:53 -0800 | [diff] [blame] | 520 | /* since we are counting overlaps the final put of the |
| 521 | * entry->pfn will occur when the overlap count is 0. |
| 522 | * active_pfn_dec_overlap() returns -1 in that case |
| 523 | */ |
| 524 | if (active_pfn_dec_overlap(entry->pfn) < 0) |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 525 | radix_tree_delete(&dma_active_pfn, entry->pfn); |
| 526 | spin_unlock_irqrestore(&radix_lock, flags); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * debug_dma_assert_idle() - assert that a page is not undergoing dma |
| 531 | * @page: page to lookup in the dma_active_pfn tree |
| 532 | * |
| 533 | * Place a call to this routine in cases where the cpu touching the page |
| 534 | * before the dma completes (page is dma_unmapped) will lead to data |
| 535 | * corruption. |
| 536 | */ |
| 537 | void debug_dma_assert_idle(struct page *page) |
| 538 | { |
| 539 | unsigned long flags; |
| 540 | struct dma_debug_entry *entry; |
| 541 | |
| 542 | if (!page) |
| 543 | return; |
| 544 | |
| 545 | spin_lock_irqsave(&radix_lock, flags); |
| 546 | entry = radix_tree_lookup(&dma_active_pfn, page_to_pfn(page)); |
| 547 | spin_unlock_irqrestore(&radix_lock, flags); |
| 548 | |
| 549 | if (!entry) |
| 550 | return; |
| 551 | |
| 552 | err_printk(entry->dev, entry, |
| 553 | "DMA-API: cpu touching an active dma mapped page " |
| 554 | "[pfn=0x%lx]\n", entry->pfn); |
| 555 | } |
| 556 | |
| 557 | /* |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 558 | * Wrapper function for adding an entry to the hash. |
| 559 | * This function takes care of locking itself. |
| 560 | */ |
| 561 | static void add_dma_entry(struct dma_debug_entry *entry) |
| 562 | { |
| 563 | struct hash_bucket *bucket; |
| 564 | unsigned long flags; |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 565 | int rc; |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 566 | |
| 567 | bucket = get_hash_bucket(entry, &flags); |
| 568 | hash_bucket_add(bucket, entry); |
| 569 | put_hash_bucket(bucket, &flags); |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 570 | |
| 571 | rc = active_pfn_insert(entry); |
| 572 | if (rc == -ENOMEM) { |
| 573 | pr_err("DMA-API: pfn tracking ENOMEM, dma-debug disabled\n"); |
| 574 | global_disable = true; |
| 575 | } |
| 576 | |
| 577 | /* TODO: report -EEXIST errors here as overlapping mappings are |
| 578 | * not supported by the DMA API |
| 579 | */ |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 580 | } |
| 581 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 582 | static struct dma_debug_entry *__dma_entry_alloc(void) |
| 583 | { |
| 584 | struct dma_debug_entry *entry; |
| 585 | |
| 586 | entry = list_entry(free_entries.next, struct dma_debug_entry, list); |
| 587 | list_del(&entry->list); |
| 588 | memset(entry, 0, sizeof(*entry)); |
| 589 | |
| 590 | num_free_entries -= 1; |
| 591 | if (num_free_entries < min_free_entries) |
| 592 | min_free_entries = num_free_entries; |
| 593 | |
| 594 | return entry; |
| 595 | } |
| 596 | |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 597 | /* struct dma_entry allocator |
| 598 | * |
| 599 | * The next two functions implement the allocator for |
| 600 | * struct dma_debug_entries. |
| 601 | */ |
| 602 | static struct dma_debug_entry *dma_entry_alloc(void) |
| 603 | { |
Jakub Kicinski | 29cdd4e | 2012-04-04 03:19:10 +0200 | [diff] [blame] | 604 | struct dma_debug_entry *entry; |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 605 | unsigned long flags; |
| 606 | |
| 607 | spin_lock_irqsave(&free_entries_lock, flags); |
| 608 | |
| 609 | if (list_empty(&free_entries)) { |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 610 | pr_err("DMA-API: debugging out of memory - disabling\n"); |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 611 | global_disable = true; |
Jakub Kicinski | 29cdd4e | 2012-04-04 03:19:10 +0200 | [diff] [blame] | 612 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 613 | return NULL; |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 614 | } |
| 615 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 616 | entry = __dma_entry_alloc(); |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 617 | |
Jakub Kicinski | 29cdd4e | 2012-04-04 03:19:10 +0200 | [diff] [blame] | 618 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 619 | |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 620 | #ifdef CONFIG_STACKTRACE |
| 621 | entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES; |
| 622 | entry->stacktrace.entries = entry->st_entries; |
| 623 | entry->stacktrace.skip = 2; |
| 624 | save_stack_trace(&entry->stacktrace); |
| 625 | #endif |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 626 | |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 627 | return entry; |
| 628 | } |
| 629 | |
| 630 | static void dma_entry_free(struct dma_debug_entry *entry) |
| 631 | { |
| 632 | unsigned long flags; |
| 633 | |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 634 | active_pfn_remove(entry); |
| 635 | |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 636 | /* |
| 637 | * add to beginning of the list - this way the entries are |
| 638 | * more likely cache hot when they are reallocated. |
| 639 | */ |
| 640 | spin_lock_irqsave(&free_entries_lock, flags); |
| 641 | list_add(&entry->list, &free_entries); |
| 642 | num_free_entries += 1; |
| 643 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 644 | } |
| 645 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 646 | int dma_debug_resize_entries(u32 num_entries) |
| 647 | { |
| 648 | int i, delta, ret = 0; |
| 649 | unsigned long flags; |
| 650 | struct dma_debug_entry *entry; |
| 651 | LIST_HEAD(tmp); |
| 652 | |
| 653 | spin_lock_irqsave(&free_entries_lock, flags); |
| 654 | |
| 655 | if (nr_total_entries < num_entries) { |
| 656 | delta = num_entries - nr_total_entries; |
| 657 | |
| 658 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 659 | |
| 660 | for (i = 0; i < delta; i++) { |
| 661 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 662 | if (!entry) |
| 663 | break; |
| 664 | |
| 665 | list_add_tail(&entry->list, &tmp); |
| 666 | } |
| 667 | |
| 668 | spin_lock_irqsave(&free_entries_lock, flags); |
| 669 | |
| 670 | list_splice(&tmp, &free_entries); |
| 671 | nr_total_entries += i; |
| 672 | num_free_entries += i; |
| 673 | } else { |
| 674 | delta = nr_total_entries - num_entries; |
| 675 | |
| 676 | for (i = 0; i < delta && !list_empty(&free_entries); i++) { |
| 677 | entry = __dma_entry_alloc(); |
| 678 | kfree(entry); |
| 679 | } |
| 680 | |
| 681 | nr_total_entries -= i; |
| 682 | } |
| 683 | |
| 684 | if (nr_total_entries != num_entries) |
| 685 | ret = 1; |
| 686 | |
| 687 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | EXPORT_SYMBOL(dma_debug_resize_entries); |
| 692 | |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 693 | /* |
| 694 | * DMA-API debugging init code |
| 695 | * |
| 696 | * The init code does two things: |
| 697 | * 1. Initialize core data structures |
| 698 | * 2. Preallocate a given number of dma_debug_entry structs |
| 699 | */ |
| 700 | |
| 701 | static int prealloc_memory(u32 num_entries) |
| 702 | { |
| 703 | struct dma_debug_entry *entry, *next_entry; |
| 704 | int i; |
| 705 | |
| 706 | for (i = 0; i < num_entries; ++i) { |
| 707 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 708 | if (!entry) |
| 709 | goto out_err; |
| 710 | |
| 711 | list_add_tail(&entry->list, &free_entries); |
| 712 | } |
| 713 | |
| 714 | num_free_entries = num_entries; |
| 715 | min_free_entries = num_entries; |
| 716 | |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 717 | pr_info("DMA-API: preallocated %d debug entries\n", num_entries); |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 718 | |
| 719 | return 0; |
| 720 | |
| 721 | out_err: |
| 722 | |
| 723 | list_for_each_entry_safe(entry, next_entry, &free_entries, list) { |
| 724 | list_del(&entry->list); |
| 725 | kfree(entry); |
| 726 | } |
| 727 | |
| 728 | return -ENOMEM; |
| 729 | } |
| 730 | |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 731 | static ssize_t filter_read(struct file *file, char __user *user_buf, |
| 732 | size_t count, loff_t *ppos) |
| 733 | { |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 734 | char buf[NAME_MAX_LEN + 1]; |
Joerg Roedel | c17e2cf | 2009-06-08 15:19:29 +0200 | [diff] [blame] | 735 | unsigned long flags; |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 736 | int len; |
| 737 | |
| 738 | if (!current_driver_name[0]) |
| 739 | return 0; |
| 740 | |
| 741 | /* |
| 742 | * We can't copy to userspace directly because current_driver_name can |
| 743 | * only be read under the driver_name_lock with irqs disabled. So |
| 744 | * create a temporary copy first. |
| 745 | */ |
| 746 | read_lock_irqsave(&driver_name_lock, flags); |
| 747 | len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name); |
| 748 | read_unlock_irqrestore(&driver_name_lock, flags); |
| 749 | |
| 750 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 751 | } |
| 752 | |
| 753 | static ssize_t filter_write(struct file *file, const char __user *userbuf, |
| 754 | size_t count, loff_t *ppos) |
| 755 | { |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 756 | char buf[NAME_MAX_LEN]; |
Joerg Roedel | c17e2cf | 2009-06-08 15:19:29 +0200 | [diff] [blame] | 757 | unsigned long flags; |
| 758 | size_t len; |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 759 | int i; |
| 760 | |
| 761 | /* |
| 762 | * We can't copy from userspace directly. Access to |
| 763 | * current_driver_name is protected with a write_lock with irqs |
| 764 | * disabled. Since copy_from_user can fault and may sleep we |
| 765 | * need to copy to temporary buffer first |
| 766 | */ |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 767 | len = min(count, (size_t)(NAME_MAX_LEN - 1)); |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 768 | if (copy_from_user(buf, userbuf, len)) |
| 769 | return -EFAULT; |
| 770 | |
| 771 | buf[len] = 0; |
| 772 | |
| 773 | write_lock_irqsave(&driver_name_lock, flags); |
| 774 | |
Joerg Roedel | 31232509 | 2009-06-08 15:07:08 +0200 | [diff] [blame] | 775 | /* |
| 776 | * Now handle the string we got from userspace very carefully. |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 777 | * The rules are: |
| 778 | * - only use the first token we got |
| 779 | * - token delimiter is everything looking like a space |
| 780 | * character (' ', '\n', '\t' ...) |
| 781 | * |
| 782 | */ |
| 783 | if (!isalnum(buf[0])) { |
| 784 | /* |
Joerg Roedel | 31232509 | 2009-06-08 15:07:08 +0200 | [diff] [blame] | 785 | * If the first character userspace gave us is not |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 786 | * alphanumerical then assume the filter should be |
| 787 | * switched off. |
| 788 | */ |
| 789 | if (current_driver_name[0]) |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 790 | pr_info("DMA-API: switching off dma-debug driver filter\n"); |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 791 | current_driver_name[0] = 0; |
| 792 | current_driver = NULL; |
| 793 | goto out_unlock; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Now parse out the first token and use it as the name for the |
| 798 | * driver to filter for. |
| 799 | */ |
Dan Carpenter | 39a37ce | 2010-04-06 19:45:12 +0300 | [diff] [blame] | 800 | for (i = 0; i < NAME_MAX_LEN - 1; ++i) { |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 801 | current_driver_name[i] = buf[i]; |
| 802 | if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0) |
| 803 | break; |
| 804 | } |
| 805 | current_driver_name[i] = 0; |
| 806 | current_driver = NULL; |
| 807 | |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 808 | pr_info("DMA-API: enable driver filter for driver [%s]\n", |
| 809 | current_driver_name); |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 810 | |
| 811 | out_unlock: |
| 812 | write_unlock_irqrestore(&driver_name_lock, flags); |
| 813 | |
| 814 | return count; |
| 815 | } |
| 816 | |
Thiago Farina | aeb583d | 2010-01-18 18:57:33 -0500 | [diff] [blame] | 817 | static const struct file_operations filter_fops = { |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 818 | .read = filter_read, |
| 819 | .write = filter_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 820 | .llseek = default_llseek, |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 821 | }; |
| 822 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 823 | static int dma_debug_fs_init(void) |
| 824 | { |
| 825 | dma_debug_dent = debugfs_create_dir("dma-api", NULL); |
| 826 | if (!dma_debug_dent) { |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 827 | pr_err("DMA-API: can not create debugfs directory\n"); |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 828 | return -ENOMEM; |
| 829 | } |
| 830 | |
| 831 | global_disable_dent = debugfs_create_bool("disabled", 0444, |
| 832 | dma_debug_dent, |
Dan Carpenter | 68ee6d2 | 2012-06-27 12:08:55 +0300 | [diff] [blame] | 833 | &global_disable); |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 834 | if (!global_disable_dent) |
| 835 | goto out_err; |
| 836 | |
| 837 | error_count_dent = debugfs_create_u32("error_count", 0444, |
| 838 | dma_debug_dent, &error_count); |
| 839 | if (!error_count_dent) |
| 840 | goto out_err; |
| 841 | |
| 842 | show_all_errors_dent = debugfs_create_u32("all_errors", 0644, |
| 843 | dma_debug_dent, |
| 844 | &show_all_errors); |
| 845 | if (!show_all_errors_dent) |
| 846 | goto out_err; |
| 847 | |
| 848 | show_num_errors_dent = debugfs_create_u32("num_errors", 0644, |
| 849 | dma_debug_dent, |
| 850 | &show_num_errors); |
| 851 | if (!show_num_errors_dent) |
| 852 | goto out_err; |
| 853 | |
| 854 | num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444, |
| 855 | dma_debug_dent, |
| 856 | &num_free_entries); |
| 857 | if (!num_free_entries_dent) |
| 858 | goto out_err; |
| 859 | |
| 860 | min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444, |
| 861 | dma_debug_dent, |
| 862 | &min_free_entries); |
| 863 | if (!min_free_entries_dent) |
| 864 | goto out_err; |
| 865 | |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame] | 866 | filter_dent = debugfs_create_file("driver_filter", 0644, |
| 867 | dma_debug_dent, NULL, &filter_fops); |
| 868 | if (!filter_dent) |
| 869 | goto out_err; |
| 870 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 871 | return 0; |
| 872 | |
| 873 | out_err: |
| 874 | debugfs_remove_recursive(dma_debug_dent); |
| 875 | |
| 876 | return -ENOMEM; |
| 877 | } |
| 878 | |
Stanislaw Gruszka | ba4b87ad | 2011-03-31 08:08:09 -0400 | [diff] [blame] | 879 | static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry) |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 880 | { |
| 881 | struct dma_debug_entry *entry; |
| 882 | unsigned long flags; |
| 883 | int count = 0, i; |
| 884 | |
Joerg Roedel | be81c6e | 2009-06-08 15:46:19 +0200 | [diff] [blame] | 885 | local_irq_save(flags); |
| 886 | |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 887 | for (i = 0; i < HASH_SIZE; ++i) { |
Joerg Roedel | be81c6e | 2009-06-08 15:46:19 +0200 | [diff] [blame] | 888 | spin_lock(&dma_entry_hash[i].lock); |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 889 | list_for_each_entry(entry, &dma_entry_hash[i].list, list) { |
Stanislaw Gruszka | ba4b87ad | 2011-03-31 08:08:09 -0400 | [diff] [blame] | 890 | if (entry->dev == dev) { |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 891 | count += 1; |
Stanislaw Gruszka | ba4b87ad | 2011-03-31 08:08:09 -0400 | [diff] [blame] | 892 | *out_entry = entry; |
| 893 | } |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 894 | } |
Joerg Roedel | be81c6e | 2009-06-08 15:46:19 +0200 | [diff] [blame] | 895 | spin_unlock(&dma_entry_hash[i].lock); |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Joerg Roedel | be81c6e | 2009-06-08 15:46:19 +0200 | [diff] [blame] | 898 | local_irq_restore(flags); |
| 899 | |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 900 | return count; |
| 901 | } |
| 902 | |
Ingo Molnar | a8fe9ea | 2009-12-31 15:16:23 +0100 | [diff] [blame] | 903 | static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data) |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 904 | { |
| 905 | struct device *dev = data; |
Stanislaw Gruszka | ba4b87ad | 2011-03-31 08:08:09 -0400 | [diff] [blame] | 906 | struct dma_debug_entry *uninitialized_var(entry); |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 907 | int count; |
| 908 | |
Shaun Ruffell | f797d98 | 2009-12-17 18:00:36 -0600 | [diff] [blame] | 909 | if (global_disable) |
Ingo Molnar | a8fe9ea | 2009-12-31 15:16:23 +0100 | [diff] [blame] | 910 | return 0; |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 911 | |
| 912 | switch (action) { |
| 913 | case BUS_NOTIFY_UNBOUND_DRIVER: |
Stanislaw Gruszka | ba4b87ad | 2011-03-31 08:08:09 -0400 | [diff] [blame] | 914 | count = device_dma_allocations(dev, &entry); |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 915 | if (count == 0) |
| 916 | break; |
Stanislaw Gruszka | ba4b87ad | 2011-03-31 08:08:09 -0400 | [diff] [blame] | 917 | err_printk(dev, entry, "DMA-API: device driver has pending " |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 918 | "DMA allocations while released from device " |
Stanislaw Gruszka | ba4b87ad | 2011-03-31 08:08:09 -0400 | [diff] [blame] | 919 | "[count=%d]\n" |
| 920 | "One of leaked entries details: " |
| 921 | "[device address=0x%016llx] [size=%llu bytes] " |
| 922 | "[mapped with %s] [mapped as %s]\n", |
| 923 | count, entry->dev_addr, entry->size, |
| 924 | dir2name[entry->direction], type2name[entry->type]); |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 925 | break; |
| 926 | default: |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
Joerg Roedel | 41531c8 | 2009-03-16 17:32:14 +0100 | [diff] [blame] | 933 | void dma_debug_add_bus(struct bus_type *bus) |
| 934 | { |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 935 | struct notifier_block *nb; |
| 936 | |
Shaun Ruffell | f797d98 | 2009-12-17 18:00:36 -0600 | [diff] [blame] | 937 | if (global_disable) |
| 938 | return; |
| 939 | |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 940 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
| 941 | if (nb == NULL) { |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 942 | pr_err("dma_debug_add_bus: out of memory\n"); |
Joerg Roedel | ed888ae | 2009-05-22 17:16:04 +0200 | [diff] [blame] | 943 | return; |
| 944 | } |
| 945 | |
| 946 | nb->notifier_call = dma_debug_device_change; |
| 947 | |
| 948 | bus_register_notifier(bus, nb); |
Joerg Roedel | 41531c8 | 2009-03-16 17:32:14 +0100 | [diff] [blame] | 949 | } |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 950 | |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 951 | /* |
| 952 | * Let the architectures decide how many entries should be preallocated. |
| 953 | */ |
| 954 | void dma_debug_init(u32 num_entries) |
| 955 | { |
| 956 | int i; |
| 957 | |
| 958 | if (global_disable) |
| 959 | return; |
| 960 | |
| 961 | for (i = 0; i < HASH_SIZE; ++i) { |
| 962 | INIT_LIST_HEAD(&dma_entry_hash[i].list); |
Ingo Molnar | b0a5b83 | 2009-06-16 16:11:14 +0200 | [diff] [blame] | 963 | spin_lock_init(&dma_entry_hash[i].lock); |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 964 | } |
| 965 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 966 | if (dma_debug_fs_init() != 0) { |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 967 | pr_err("DMA-API: error creating debugfs entries - disabling\n"); |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 968 | global_disable = true; |
| 969 | |
| 970 | return; |
| 971 | } |
| 972 | |
Joerg Roedel | 59d3daa | 2009-01-09 13:01:56 +0100 | [diff] [blame] | 973 | if (req_entries) |
| 974 | num_entries = req_entries; |
| 975 | |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 976 | if (prealloc_memory(num_entries) != 0) { |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 977 | pr_err("DMA-API: debugging out of memory error - disabled\n"); |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 978 | global_disable = true; |
| 979 | |
| 980 | return; |
| 981 | } |
| 982 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 983 | nr_total_entries = num_free_entries; |
| 984 | |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 985 | pr_info("DMA-API: debugging enabled by kernel config\n"); |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 986 | } |
| 987 | |
Joerg Roedel | 59d3daa | 2009-01-09 13:01:56 +0100 | [diff] [blame] | 988 | static __init int dma_debug_cmdline(char *str) |
| 989 | { |
| 990 | if (!str) |
| 991 | return -EINVAL; |
| 992 | |
| 993 | if (strncmp(str, "off", 3) == 0) { |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 994 | pr_info("DMA-API: debugging disabled on kernel command line\n"); |
Joerg Roedel | 59d3daa | 2009-01-09 13:01:56 +0100 | [diff] [blame] | 995 | global_disable = true; |
| 996 | } |
| 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | static __init int dma_debug_entries_cmdline(char *str) |
| 1002 | { |
| 1003 | int res; |
| 1004 | |
| 1005 | if (!str) |
| 1006 | return -EINVAL; |
| 1007 | |
| 1008 | res = get_option(&str, &req_entries); |
| 1009 | |
| 1010 | if (!res) |
| 1011 | req_entries = 0; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | __setup("dma_debug=", dma_debug_cmdline); |
| 1017 | __setup("dma_debug_entries=", dma_debug_entries_cmdline); |
| 1018 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1019 | static void check_unmap(struct dma_debug_entry *ref) |
| 1020 | { |
| 1021 | struct dma_debug_entry *entry; |
| 1022 | struct hash_bucket *bucket; |
| 1023 | unsigned long flags; |
| 1024 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1025 | bucket = get_hash_bucket(ref, &flags); |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 1026 | entry = bucket_find_exact(bucket, ref); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1027 | |
| 1028 | if (!entry) { |
Alexander Duyck | 8d640a5 | 2013-03-22 15:04:48 -0700 | [diff] [blame] | 1029 | /* must drop lock before calling dma_mapping_error */ |
| 1030 | put_hash_bucket(bucket, &flags); |
| 1031 | |
Shuah Khan | bfe0fb0 | 2012-11-03 17:00:07 -0600 | [diff] [blame] | 1032 | if (dma_mapping_error(ref->dev, ref->dev_addr)) { |
| 1033 | err_printk(ref->dev, NULL, |
Alexander Duyck | 8d640a5 | 2013-03-22 15:04:48 -0700 | [diff] [blame] | 1034 | "DMA-API: device driver tries to free an " |
| 1035 | "invalid DMA memory address\n"); |
| 1036 | } else { |
| 1037 | err_printk(ref->dev, NULL, |
| 1038 | "DMA-API: device driver tries to free DMA " |
| 1039 | "memory it has not allocated [device " |
| 1040 | "address=0x%016llx] [size=%llu bytes]\n", |
| 1041 | ref->dev_addr, ref->size); |
Shuah Khan | bfe0fb0 | 2012-11-03 17:00:07 -0600 | [diff] [blame] | 1042 | } |
Alexander Duyck | 8d640a5 | 2013-03-22 15:04:48 -0700 | [diff] [blame] | 1043 | return; |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | if (ref->size != entry->size) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1047 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1048 | "DMA memory with different size " |
| 1049 | "[device address=0x%016llx] [map size=%llu bytes] " |
| 1050 | "[unmap size=%llu bytes]\n", |
| 1051 | ref->dev_addr, entry->size, ref->size); |
| 1052 | } |
| 1053 | |
| 1054 | if (ref->type != entry->type) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1055 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1056 | "DMA memory with wrong function " |
| 1057 | "[device address=0x%016llx] [size=%llu bytes] " |
| 1058 | "[mapped as %s] [unmapped as %s]\n", |
| 1059 | ref->dev_addr, ref->size, |
| 1060 | type2name[entry->type], type2name[ref->type]); |
| 1061 | } else if ((entry->type == dma_debug_coherent) && |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1062 | (phys_addr(ref) != phys_addr(entry))) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1063 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1064 | "DMA memory with different CPU address " |
| 1065 | "[device address=0x%016llx] [size=%llu bytes] " |
Joerg Roedel | 59a40e70 | 2009-10-29 16:25:50 +0100 | [diff] [blame] | 1066 | "[cpu alloc address=0x%016llx] " |
| 1067 | "[cpu free address=0x%016llx]", |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1068 | ref->dev_addr, ref->size, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1069 | phys_addr(entry), |
| 1070 | phys_addr(ref)); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (ref->sg_call_ents && ref->type == dma_debug_sg && |
| 1074 | ref->sg_call_ents != entry->sg_call_ents) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1075 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1076 | "DMA sg list with different entry count " |
| 1077 | "[map count=%d] [unmap count=%d]\n", |
| 1078 | entry->sg_call_ents, ref->sg_call_ents); |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * This may be no bug in reality - but most implementations of the |
| 1083 | * DMA API don't handle this properly, so check for it here |
| 1084 | */ |
| 1085 | if (ref->direction != entry->direction) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1086 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1087 | "DMA memory with different direction " |
| 1088 | "[device address=0x%016llx] [size=%llu bytes] " |
| 1089 | "[mapped with %s] [unmapped with %s]\n", |
| 1090 | ref->dev_addr, ref->size, |
| 1091 | dir2name[entry->direction], |
| 1092 | dir2name[ref->direction]); |
| 1093 | } |
| 1094 | |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 1095 | if (entry->map_err_type == MAP_ERR_NOT_CHECKED) { |
| 1096 | err_printk(ref->dev, entry, |
| 1097 | "DMA-API: device driver failed to check map error" |
| 1098 | "[device address=0x%016llx] [size=%llu bytes] " |
| 1099 | "[mapped as %s]", |
| 1100 | ref->dev_addr, ref->size, |
| 1101 | type2name[entry->type]); |
| 1102 | } |
| 1103 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1104 | hash_bucket_del(entry); |
| 1105 | dma_entry_free(entry); |
| 1106 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1107 | put_hash_bucket(bucket, &flags); |
| 1108 | } |
| 1109 | |
| 1110 | static void check_for_stack(struct device *dev, void *addr) |
| 1111 | { |
| 1112 | if (object_is_on_stack(addr)) |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1113 | err_printk(dev, NULL, "DMA-API: device driver maps memory from" |
| 1114 | "stack [addr=%p]\n", addr); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
Ingo Molnar | f39d1b9 | 2009-07-10 21:38:02 +0200 | [diff] [blame] | 1117 | static inline bool overlap(void *addr, unsigned long len, void *start, void *end) |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 1118 | { |
Ingo Molnar | f39d1b9 | 2009-07-10 21:38:02 +0200 | [diff] [blame] | 1119 | unsigned long a1 = (unsigned long)addr; |
| 1120 | unsigned long b1 = a1 + len; |
| 1121 | unsigned long a2 = (unsigned long)start; |
| 1122 | unsigned long b2 = (unsigned long)end; |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 1123 | |
Ingo Molnar | f39d1b9 | 2009-07-10 21:38:02 +0200 | [diff] [blame] | 1124 | return !(b1 <= a2 || a1 >= b2); |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Ingo Molnar | f39d1b9 | 2009-07-10 21:38:02 +0200 | [diff] [blame] | 1127 | static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len) |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 1128 | { |
Ingo Molnar | f39d1b9 | 2009-07-10 21:38:02 +0200 | [diff] [blame] | 1129 | if (overlap(addr, len, _text, _etext) || |
| 1130 | overlap(addr, len, __start_rodata, __end_rodata)) |
| 1131 | err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len); |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1134 | static void check_sync(struct device *dev, |
| 1135 | struct dma_debug_entry *ref, |
| 1136 | bool to_cpu) |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1137 | { |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1138 | struct dma_debug_entry *entry; |
| 1139 | struct hash_bucket *bucket; |
| 1140 | unsigned long flags; |
| 1141 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1142 | bucket = get_hash_bucket(ref, &flags); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1143 | |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 1144 | entry = bucket_find_contain(&bucket, ref, &flags); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1145 | |
| 1146 | if (!entry) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1147 | err_printk(dev, NULL, "DMA-API: device driver tries " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1148 | "to sync DMA memory it has not allocated " |
| 1149 | "[device address=0x%016llx] [size=%llu bytes]\n", |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1150 | (unsigned long long)ref->dev_addr, ref->size); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1151 | goto out; |
| 1152 | } |
| 1153 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1154 | if (ref->size > entry->size) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1155 | err_printk(dev, entry, "DMA-API: device driver syncs" |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1156 | " DMA memory outside allocated range " |
| 1157 | "[device address=0x%016llx] " |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1158 | "[allocation size=%llu bytes] " |
| 1159 | "[sync offset+size=%llu]\n", |
| 1160 | entry->dev_addr, entry->size, |
| 1161 | ref->size); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Krzysztof Halasa | 42d53b4 | 2010-01-08 14:42:36 -0800 | [diff] [blame] | 1164 | if (entry->direction == DMA_BIDIRECTIONAL) |
| 1165 | goto out; |
| 1166 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1167 | if (ref->direction != entry->direction) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1168 | err_printk(dev, entry, "DMA-API: device driver syncs " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1169 | "DMA memory with different direction " |
| 1170 | "[device address=0x%016llx] [size=%llu bytes] " |
| 1171 | "[mapped with %s] [synced with %s]\n", |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1172 | (unsigned long long)ref->dev_addr, entry->size, |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1173 | dir2name[entry->direction], |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1174 | dir2name[ref->direction]); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1177 | if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) && |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1178 | !(ref->direction == DMA_TO_DEVICE)) |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1179 | err_printk(dev, entry, "DMA-API: device driver syncs " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1180 | "device read-only DMA memory for cpu " |
| 1181 | "[device address=0x%016llx] [size=%llu bytes] " |
| 1182 | "[mapped with %s] [synced with %s]\n", |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1183 | (unsigned long long)ref->dev_addr, entry->size, |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1184 | dir2name[entry->direction], |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1185 | dir2name[ref->direction]); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1186 | |
| 1187 | if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) && |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1188 | !(ref->direction == DMA_FROM_DEVICE)) |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 1189 | err_printk(dev, entry, "DMA-API: device driver syncs " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1190 | "device write-only DMA memory to device " |
| 1191 | "[device address=0x%016llx] [size=%llu bytes] " |
| 1192 | "[mapped with %s] [synced with %s]\n", |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1193 | (unsigned long long)ref->dev_addr, entry->size, |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1194 | dir2name[entry->direction], |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1195 | dir2name[ref->direction]); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1196 | |
| 1197 | out: |
| 1198 | put_hash_bucket(bucket, &flags); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 1201 | void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, |
| 1202 | size_t size, int direction, dma_addr_t dma_addr, |
| 1203 | bool map_single) |
| 1204 | { |
| 1205 | struct dma_debug_entry *entry; |
| 1206 | |
| 1207 | if (unlikely(global_disable)) |
| 1208 | return; |
| 1209 | |
Shuah Khan | bfe0fb0 | 2012-11-03 17:00:07 -0600 | [diff] [blame] | 1210 | if (dma_mapping_error(dev, dma_addr)) |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 1211 | return; |
| 1212 | |
| 1213 | entry = dma_entry_alloc(); |
| 1214 | if (!entry) |
| 1215 | return; |
| 1216 | |
| 1217 | entry->dev = dev; |
| 1218 | entry->type = dma_debug_page; |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1219 | entry->pfn = page_to_pfn(page); |
| 1220 | entry->offset = offset, |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 1221 | entry->dev_addr = dma_addr; |
| 1222 | entry->size = size; |
| 1223 | entry->direction = direction; |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 1224 | entry->map_err_type = MAP_ERR_NOT_CHECKED; |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 1225 | |
Joerg Roedel | 9537a48 | 2009-03-23 15:35:08 +0100 | [diff] [blame] | 1226 | if (map_single) |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 1227 | entry->type = dma_debug_single; |
Joerg Roedel | 9537a48 | 2009-03-23 15:35:08 +0100 | [diff] [blame] | 1228 | |
| 1229 | if (!PageHighMem(page)) { |
Ingo Molnar | f39d1b9 | 2009-07-10 21:38:02 +0200 | [diff] [blame] | 1230 | void *addr = page_address(page) + offset; |
| 1231 | |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 1232 | check_for_stack(dev, addr); |
| 1233 | check_for_illegal_area(dev, addr, size); |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | add_dma_entry(entry); |
| 1237 | } |
| 1238 | EXPORT_SYMBOL(debug_dma_map_page); |
| 1239 | |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 1240 | void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 1241 | { |
| 1242 | struct dma_debug_entry ref; |
| 1243 | struct dma_debug_entry *entry; |
| 1244 | struct hash_bucket *bucket; |
| 1245 | unsigned long flags; |
| 1246 | |
| 1247 | if (unlikely(global_disable)) |
| 1248 | return; |
| 1249 | |
| 1250 | ref.dev = dev; |
| 1251 | ref.dev_addr = dma_addr; |
| 1252 | bucket = get_hash_bucket(&ref, &flags); |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 1253 | |
Alexander Duyck | 96e7d7a | 2013-03-22 15:04:49 -0700 | [diff] [blame] | 1254 | list_for_each_entry(entry, &bucket->list, list) { |
| 1255 | if (!exact_match(&ref, entry)) |
| 1256 | continue; |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 1257 | |
Alexander Duyck | 96e7d7a | 2013-03-22 15:04:49 -0700 | [diff] [blame] | 1258 | /* |
| 1259 | * The same physical address can be mapped multiple |
| 1260 | * times. Without a hardware IOMMU this results in the |
| 1261 | * same device addresses being put into the dma-debug |
| 1262 | * hash multiple times too. This can result in false |
| 1263 | * positives being reported. Therefore we implement a |
| 1264 | * best-fit algorithm here which updates the first entry |
| 1265 | * from the hash which fits the reference value and is |
| 1266 | * not currently listed as being checked. |
| 1267 | */ |
| 1268 | if (entry->map_err_type == MAP_ERR_NOT_CHECKED) { |
| 1269 | entry->map_err_type = MAP_ERR_CHECKED; |
| 1270 | break; |
| 1271 | } |
| 1272 | } |
| 1273 | |
Shuah Khan | 6c9c6d6 | 2012-10-08 11:08:06 -0600 | [diff] [blame] | 1274 | put_hash_bucket(bucket, &flags); |
| 1275 | } |
| 1276 | EXPORT_SYMBOL(debug_dma_mapping_error); |
| 1277 | |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 1278 | void debug_dma_unmap_page(struct device *dev, dma_addr_t addr, |
| 1279 | size_t size, int direction, bool map_single) |
| 1280 | { |
| 1281 | struct dma_debug_entry ref = { |
| 1282 | .type = dma_debug_page, |
| 1283 | .dev = dev, |
| 1284 | .dev_addr = addr, |
| 1285 | .size = size, |
| 1286 | .direction = direction, |
| 1287 | }; |
| 1288 | |
| 1289 | if (unlikely(global_disable)) |
| 1290 | return; |
| 1291 | |
| 1292 | if (map_single) |
| 1293 | ref.type = dma_debug_single; |
| 1294 | |
| 1295 | check_unmap(&ref); |
| 1296 | } |
| 1297 | EXPORT_SYMBOL(debug_dma_unmap_page); |
| 1298 | |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1299 | void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 1300 | int nents, int mapped_ents, int direction) |
| 1301 | { |
| 1302 | struct dma_debug_entry *entry; |
| 1303 | struct scatterlist *s; |
| 1304 | int i; |
| 1305 | |
| 1306 | if (unlikely(global_disable)) |
| 1307 | return; |
| 1308 | |
| 1309 | for_each_sg(sg, s, mapped_ents, i) { |
| 1310 | entry = dma_entry_alloc(); |
| 1311 | if (!entry) |
| 1312 | return; |
| 1313 | |
| 1314 | entry->type = dma_debug_sg; |
| 1315 | entry->dev = dev; |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1316 | entry->pfn = page_to_pfn(sg_page(s)); |
| 1317 | entry->offset = s->offset, |
FUJITA Tomonori | 884d059 | 2009-05-27 09:43:02 +0900 | [diff] [blame] | 1318 | entry->size = sg_dma_len(s); |
FUJITA Tomonori | 15aedea4 | 2009-05-27 09:43:01 +0900 | [diff] [blame] | 1319 | entry->dev_addr = sg_dma_address(s); |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1320 | entry->direction = direction; |
| 1321 | entry->sg_call_ents = nents; |
| 1322 | entry->sg_mapped_ents = mapped_ents; |
| 1323 | |
Joerg Roedel | 9537a48 | 2009-03-23 15:35:08 +0100 | [diff] [blame] | 1324 | if (!PageHighMem(sg_page(s))) { |
| 1325 | check_for_stack(dev, sg_virt(s)); |
FUJITA Tomonori | 884d059 | 2009-05-27 09:43:02 +0900 | [diff] [blame] | 1326 | check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s)); |
Joerg Roedel | 9537a48 | 2009-03-23 15:35:08 +0100 | [diff] [blame] | 1327 | } |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1328 | |
| 1329 | add_dma_entry(entry); |
| 1330 | } |
| 1331 | } |
| 1332 | EXPORT_SYMBOL(debug_dma_map_sg); |
| 1333 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1334 | static int get_nr_mapped_entries(struct device *dev, |
| 1335 | struct dma_debug_entry *ref) |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1336 | { |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1337 | struct dma_debug_entry *entry; |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1338 | struct hash_bucket *bucket; |
| 1339 | unsigned long flags; |
Joerg Roedel | c17e2cf | 2009-06-08 15:19:29 +0200 | [diff] [blame] | 1340 | int mapped_ents; |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1341 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1342 | bucket = get_hash_bucket(ref, &flags); |
Neil Horman | c6a21d0 | 2011-08-08 15:13:54 -0400 | [diff] [blame] | 1343 | entry = bucket_find_exact(bucket, ref); |
Joerg Roedel | c17e2cf | 2009-06-08 15:19:29 +0200 | [diff] [blame] | 1344 | mapped_ents = 0; |
| 1345 | |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1346 | if (entry) |
| 1347 | mapped_ents = entry->sg_mapped_ents; |
| 1348 | put_hash_bucket(bucket, &flags); |
| 1349 | |
| 1350 | return mapped_ents; |
| 1351 | } |
| 1352 | |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1353 | void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 1354 | int nelems, int dir) |
| 1355 | { |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1356 | struct scatterlist *s; |
| 1357 | int mapped_ents = 0, i; |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1358 | |
| 1359 | if (unlikely(global_disable)) |
| 1360 | return; |
| 1361 | |
| 1362 | for_each_sg(sglist, s, nelems, i) { |
| 1363 | |
| 1364 | struct dma_debug_entry ref = { |
| 1365 | .type = dma_debug_sg, |
| 1366 | .dev = dev, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1367 | .pfn = page_to_pfn(sg_page(s)), |
| 1368 | .offset = s->offset, |
FUJITA Tomonori | 15aedea4 | 2009-05-27 09:43:01 +0900 | [diff] [blame] | 1369 | .dev_addr = sg_dma_address(s), |
FUJITA Tomonori | 884d059 | 2009-05-27 09:43:02 +0900 | [diff] [blame] | 1370 | .size = sg_dma_len(s), |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1371 | .direction = dir, |
Joerg Roedel | e5e8c5b | 2009-06-11 10:03:42 +0200 | [diff] [blame] | 1372 | .sg_call_ents = nelems, |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1373 | }; |
| 1374 | |
| 1375 | if (mapped_ents && i >= mapped_ents) |
| 1376 | break; |
| 1377 | |
Joerg Roedel | e5e8c5b | 2009-06-11 10:03:42 +0200 | [diff] [blame] | 1378 | if (!i) |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1379 | mapped_ents = get_nr_mapped_entries(dev, &ref); |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 1380 | |
| 1381 | check_unmap(&ref); |
| 1382 | } |
| 1383 | } |
| 1384 | EXPORT_SYMBOL(debug_dma_unmap_sg); |
| 1385 | |
Joerg Roedel | 6bfd449 | 2009-01-09 14:38:50 +0100 | [diff] [blame] | 1386 | void debug_dma_alloc_coherent(struct device *dev, size_t size, |
| 1387 | dma_addr_t dma_addr, void *virt) |
| 1388 | { |
| 1389 | struct dma_debug_entry *entry; |
| 1390 | |
| 1391 | if (unlikely(global_disable)) |
| 1392 | return; |
| 1393 | |
| 1394 | if (unlikely(virt == NULL)) |
| 1395 | return; |
| 1396 | |
| 1397 | entry = dma_entry_alloc(); |
| 1398 | if (!entry) |
| 1399 | return; |
| 1400 | |
| 1401 | entry->type = dma_debug_coherent; |
| 1402 | entry->dev = dev; |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1403 | entry->pfn = page_to_pfn(virt_to_page(virt)); |
| 1404 | entry->offset = (size_t) virt & PAGE_MASK; |
Joerg Roedel | 6bfd449 | 2009-01-09 14:38:50 +0100 | [diff] [blame] | 1405 | entry->size = size; |
| 1406 | entry->dev_addr = dma_addr; |
| 1407 | entry->direction = DMA_BIDIRECTIONAL; |
| 1408 | |
| 1409 | add_dma_entry(entry); |
| 1410 | } |
| 1411 | EXPORT_SYMBOL(debug_dma_alloc_coherent); |
| 1412 | |
| 1413 | void debug_dma_free_coherent(struct device *dev, size_t size, |
| 1414 | void *virt, dma_addr_t addr) |
| 1415 | { |
| 1416 | struct dma_debug_entry ref = { |
| 1417 | .type = dma_debug_coherent, |
| 1418 | .dev = dev, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1419 | .pfn = page_to_pfn(virt_to_page(virt)), |
| 1420 | .offset = (size_t) virt & PAGE_MASK, |
Joerg Roedel | 6bfd449 | 2009-01-09 14:38:50 +0100 | [diff] [blame] | 1421 | .dev_addr = addr, |
| 1422 | .size = size, |
| 1423 | .direction = DMA_BIDIRECTIONAL, |
| 1424 | }; |
| 1425 | |
| 1426 | if (unlikely(global_disable)) |
| 1427 | return; |
| 1428 | |
| 1429 | check_unmap(&ref); |
| 1430 | } |
| 1431 | EXPORT_SYMBOL(debug_dma_free_coherent); |
| 1432 | |
Joerg Roedel | b9d2317 | 2009-01-09 14:43:04 +0100 | [diff] [blame] | 1433 | void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, |
| 1434 | size_t size, int direction) |
| 1435 | { |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1436 | struct dma_debug_entry ref; |
| 1437 | |
Joerg Roedel | b9d2317 | 2009-01-09 14:43:04 +0100 | [diff] [blame] | 1438 | if (unlikely(global_disable)) |
| 1439 | return; |
| 1440 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1441 | ref.type = dma_debug_single; |
| 1442 | ref.dev = dev; |
| 1443 | ref.dev_addr = dma_handle; |
| 1444 | ref.size = size; |
| 1445 | ref.direction = direction; |
| 1446 | ref.sg_call_ents = 0; |
| 1447 | |
| 1448 | check_sync(dev, &ref, true); |
Joerg Roedel | b9d2317 | 2009-01-09 14:43:04 +0100 | [diff] [blame] | 1449 | } |
| 1450 | EXPORT_SYMBOL(debug_dma_sync_single_for_cpu); |
| 1451 | |
| 1452 | void debug_dma_sync_single_for_device(struct device *dev, |
| 1453 | dma_addr_t dma_handle, size_t size, |
| 1454 | int direction) |
| 1455 | { |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1456 | struct dma_debug_entry ref; |
| 1457 | |
Joerg Roedel | b9d2317 | 2009-01-09 14:43:04 +0100 | [diff] [blame] | 1458 | if (unlikely(global_disable)) |
| 1459 | return; |
| 1460 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1461 | ref.type = dma_debug_single; |
| 1462 | ref.dev = dev; |
| 1463 | ref.dev_addr = dma_handle; |
| 1464 | ref.size = size; |
| 1465 | ref.direction = direction; |
| 1466 | ref.sg_call_ents = 0; |
| 1467 | |
| 1468 | check_sync(dev, &ref, false); |
Joerg Roedel | b9d2317 | 2009-01-09 14:43:04 +0100 | [diff] [blame] | 1469 | } |
| 1470 | EXPORT_SYMBOL(debug_dma_sync_single_for_device); |
| 1471 | |
Joerg Roedel | 948408b | 2009-01-09 14:55:38 +0100 | [diff] [blame] | 1472 | void debug_dma_sync_single_range_for_cpu(struct device *dev, |
| 1473 | dma_addr_t dma_handle, |
| 1474 | unsigned long offset, size_t size, |
| 1475 | int direction) |
| 1476 | { |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1477 | struct dma_debug_entry ref; |
| 1478 | |
Joerg Roedel | 948408b | 2009-01-09 14:55:38 +0100 | [diff] [blame] | 1479 | if (unlikely(global_disable)) |
| 1480 | return; |
| 1481 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1482 | ref.type = dma_debug_single; |
| 1483 | ref.dev = dev; |
| 1484 | ref.dev_addr = dma_handle; |
| 1485 | ref.size = offset + size; |
| 1486 | ref.direction = direction; |
| 1487 | ref.sg_call_ents = 0; |
| 1488 | |
| 1489 | check_sync(dev, &ref, true); |
Joerg Roedel | 948408b | 2009-01-09 14:55:38 +0100 | [diff] [blame] | 1490 | } |
| 1491 | EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu); |
| 1492 | |
| 1493 | void debug_dma_sync_single_range_for_device(struct device *dev, |
| 1494 | dma_addr_t dma_handle, |
| 1495 | unsigned long offset, |
| 1496 | size_t size, int direction) |
| 1497 | { |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1498 | struct dma_debug_entry ref; |
| 1499 | |
Joerg Roedel | 948408b | 2009-01-09 14:55:38 +0100 | [diff] [blame] | 1500 | if (unlikely(global_disable)) |
| 1501 | return; |
| 1502 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1503 | ref.type = dma_debug_single; |
| 1504 | ref.dev = dev; |
| 1505 | ref.dev_addr = dma_handle; |
| 1506 | ref.size = offset + size; |
| 1507 | ref.direction = direction; |
| 1508 | ref.sg_call_ents = 0; |
| 1509 | |
| 1510 | check_sync(dev, &ref, false); |
Joerg Roedel | 948408b | 2009-01-09 14:55:38 +0100 | [diff] [blame] | 1511 | } |
| 1512 | EXPORT_SYMBOL(debug_dma_sync_single_range_for_device); |
| 1513 | |
Joerg Roedel | a31fba5 | 2009-01-09 15:01:12 +0100 | [diff] [blame] | 1514 | void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
| 1515 | int nelems, int direction) |
| 1516 | { |
| 1517 | struct scatterlist *s; |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1518 | int mapped_ents = 0, i; |
Joerg Roedel | a31fba5 | 2009-01-09 15:01:12 +0100 | [diff] [blame] | 1519 | |
| 1520 | if (unlikely(global_disable)) |
| 1521 | return; |
| 1522 | |
| 1523 | for_each_sg(sg, s, nelems, i) { |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1524 | |
| 1525 | struct dma_debug_entry ref = { |
| 1526 | .type = dma_debug_sg, |
| 1527 | .dev = dev, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1528 | .pfn = page_to_pfn(sg_page(s)), |
| 1529 | .offset = s->offset, |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1530 | .dev_addr = sg_dma_address(s), |
| 1531 | .size = sg_dma_len(s), |
| 1532 | .direction = direction, |
| 1533 | .sg_call_ents = nelems, |
| 1534 | }; |
| 1535 | |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1536 | if (!i) |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1537 | mapped_ents = get_nr_mapped_entries(dev, &ref); |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1538 | |
| 1539 | if (i >= mapped_ents) |
| 1540 | break; |
| 1541 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1542 | check_sync(dev, &ref, true); |
Joerg Roedel | a31fba5 | 2009-01-09 15:01:12 +0100 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu); |
| 1546 | |
| 1547 | void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
| 1548 | int nelems, int direction) |
| 1549 | { |
| 1550 | struct scatterlist *s; |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1551 | int mapped_ents = 0, i; |
Joerg Roedel | a31fba5 | 2009-01-09 15:01:12 +0100 | [diff] [blame] | 1552 | |
| 1553 | if (unlikely(global_disable)) |
| 1554 | return; |
| 1555 | |
| 1556 | for_each_sg(sg, s, nelems, i) { |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1557 | |
| 1558 | struct dma_debug_entry ref = { |
| 1559 | .type = dma_debug_sg, |
| 1560 | .dev = dev, |
Dan Williams | 0abdd7a | 2014-01-21 15:48:12 -0800 | [diff] [blame] | 1561 | .pfn = page_to_pfn(sg_page(s)), |
| 1562 | .offset = s->offset, |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1563 | .dev_addr = sg_dma_address(s), |
| 1564 | .size = sg_dma_len(s), |
| 1565 | .direction = direction, |
| 1566 | .sg_call_ents = nelems, |
| 1567 | }; |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1568 | if (!i) |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1569 | mapped_ents = get_nr_mapped_entries(dev, &ref); |
FUJITA Tomonori | 88f3907 | 2009-05-27 09:43:03 +0900 | [diff] [blame] | 1570 | |
| 1571 | if (i >= mapped_ents) |
| 1572 | break; |
| 1573 | |
Joerg Roedel | aa010ef | 2009-06-12 15:25:06 +0200 | [diff] [blame] | 1574 | check_sync(dev, &ref, false); |
Joerg Roedel | a31fba5 | 2009-01-09 15:01:12 +0100 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | EXPORT_SYMBOL(debug_dma_sync_sg_for_device); |
| 1578 | |
Joerg Roedel | 1745de5 | 2009-05-22 21:49:51 +0200 | [diff] [blame] | 1579 | static int __init dma_debug_driver_setup(char *str) |
| 1580 | { |
| 1581 | int i; |
| 1582 | |
| 1583 | for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) { |
| 1584 | current_driver_name[i] = *str; |
| 1585 | if (*str == 0) |
| 1586 | break; |
| 1587 | } |
| 1588 | |
| 1589 | if (current_driver_name[0]) |
Joerg Roedel | e7ed70e | 2009-06-08 15:39:24 +0200 | [diff] [blame] | 1590 | pr_info("DMA-API: enable driver filter for driver [%s]\n", |
| 1591 | current_driver_name); |
Joerg Roedel | 1745de5 | 2009-05-22 21:49:51 +0200 | [diff] [blame] | 1592 | |
| 1593 | |
| 1594 | return 1; |
| 1595 | } |
| 1596 | __setup("dma_debug_driver=", dma_debug_driver_setup); |