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