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