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> |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 27 | #include <linux/device.h> |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 28 | #include <linux/types.h> |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 29 | #include <linux/sched.h> |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame^] | 30 | #include <linux/ctype.h> |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 31 | #include <linux/list.h> |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 32 | #include <linux/slab.h> |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 33 | |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 34 | #include <asm/sections.h> |
| 35 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 36 | #define HASH_SIZE 1024ULL |
| 37 | #define HASH_FN_SHIFT 13 |
| 38 | #define HASH_FN_MASK (HASH_SIZE - 1) |
| 39 | |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 40 | enum { |
| 41 | dma_debug_single, |
| 42 | dma_debug_page, |
| 43 | dma_debug_sg, |
| 44 | dma_debug_coherent, |
| 45 | }; |
| 46 | |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 47 | #define DMA_DEBUG_STACKTRACE_ENTRIES 5 |
| 48 | |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 49 | struct dma_debug_entry { |
| 50 | struct list_head list; |
| 51 | struct device *dev; |
| 52 | int type; |
| 53 | phys_addr_t paddr; |
| 54 | u64 dev_addr; |
| 55 | u64 size; |
| 56 | int direction; |
| 57 | int sg_call_ents; |
| 58 | int sg_mapped_ents; |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_STACKTRACE |
| 60 | struct stack_trace stacktrace; |
| 61 | unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES]; |
| 62 | #endif |
Joerg Roedel | f2f45e5 | 2009-01-09 12:19:52 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 65 | struct hash_bucket { |
| 66 | struct list_head list; |
| 67 | spinlock_t lock; |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 68 | } ____cacheline_aligned_in_smp; |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 69 | |
| 70 | /* Hash list to save the allocated dma addresses */ |
| 71 | static struct hash_bucket dma_entry_hash[HASH_SIZE]; |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 72 | /* List of pre-allocated dma_debug_entry's */ |
| 73 | static LIST_HEAD(free_entries); |
| 74 | /* Lock for the list above */ |
| 75 | static DEFINE_SPINLOCK(free_entries_lock); |
| 76 | |
| 77 | /* Global disable flag - will be set in case of an error */ |
| 78 | static bool global_disable __read_mostly; |
| 79 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 80 | /* Global error count */ |
| 81 | static u32 error_count; |
| 82 | |
| 83 | /* Global error show enable*/ |
| 84 | static u32 show_all_errors __read_mostly; |
| 85 | /* Number of errors to show */ |
| 86 | static u32 show_num_errors = 1; |
| 87 | |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 88 | static u32 num_free_entries; |
| 89 | static u32 min_free_entries; |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 90 | static u32 nr_total_entries; |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 91 | |
Joerg Roedel | 59d3daa | 2009-01-09 13:01:56 +0100 | [diff] [blame] | 92 | /* number of preallocated entries requested by kernel cmdline */ |
| 93 | static u32 req_entries; |
| 94 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 95 | /* debugfs dentry's for the stuff above */ |
| 96 | static struct dentry *dma_debug_dent __read_mostly; |
| 97 | static struct dentry *global_disable_dent __read_mostly; |
| 98 | static struct dentry *error_count_dent __read_mostly; |
| 99 | static struct dentry *show_all_errors_dent __read_mostly; |
| 100 | static struct dentry *show_num_errors_dent __read_mostly; |
| 101 | static struct dentry *num_free_entries_dent __read_mostly; |
| 102 | static struct dentry *min_free_entries_dent __read_mostly; |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame^] | 103 | static struct dentry *filter_dent __read_mostly; |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 104 | |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 105 | /* per-driver filter related state */ |
| 106 | |
| 107 | #define NAME_MAX_LEN 64 |
| 108 | |
| 109 | static char current_driver_name[NAME_MAX_LEN] __read_mostly; |
| 110 | static struct device_driver *current_driver __read_mostly; |
| 111 | |
| 112 | static DEFINE_RWLOCK(driver_name_lock); |
| 113 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 114 | static const char *type2name[4] = { "single", "page", |
| 115 | "scather-gather", "coherent" }; |
| 116 | |
| 117 | static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE", |
| 118 | "DMA_FROM_DEVICE", "DMA_NONE" }; |
| 119 | |
| 120 | /* |
| 121 | * The access to some variables in this macro is racy. We can't use atomic_t |
| 122 | * here because all these variables are exported to debugfs. Some of them even |
| 123 | * writeable. This is also the reason why a lock won't help much. But anyway, |
| 124 | * the races are no big deal. Here is why: |
| 125 | * |
| 126 | * error_count: the addition is racy, but the worst thing that can happen is |
| 127 | * that we don't count some errors |
| 128 | * show_num_errors: the subtraction is racy. Also no big deal because in |
| 129 | * worst case this will result in one warning more in the |
| 130 | * system log than the user configured. This variable is |
| 131 | * writeable via debugfs. |
| 132 | */ |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 133 | static inline void dump_entry_trace(struct dma_debug_entry *entry) |
| 134 | { |
| 135 | #ifdef CONFIG_STACKTRACE |
| 136 | if (entry) { |
| 137 | printk(KERN_WARNING "Mapped at:\n"); |
| 138 | print_stack_trace(&entry->stacktrace, 0); |
| 139 | } |
| 140 | #endif |
| 141 | } |
| 142 | |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 143 | static bool driver_filter(struct device *dev) |
| 144 | { |
| 145 | /* driver filter off */ |
| 146 | if (likely(!current_driver_name[0])) |
| 147 | return true; |
| 148 | |
| 149 | /* driver filter on and initialized */ |
| 150 | if (current_driver && dev->driver == current_driver) |
| 151 | return true; |
| 152 | |
| 153 | /* driver filter on but not yet initialized */ |
| 154 | if (!current_driver && current_driver_name[0]) { |
| 155 | struct device_driver *drv = get_driver(dev->driver); |
| 156 | unsigned long flags; |
| 157 | bool ret = false; |
| 158 | |
| 159 | if (!drv) |
| 160 | return false; |
| 161 | |
| 162 | /* lock to protect against change of current_driver_name */ |
| 163 | read_lock_irqsave(&driver_name_lock, flags); |
| 164 | |
| 165 | if (drv->name && |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame^] | 166 | strncmp(current_driver_name, drv->name, |
| 167 | NAME_MAX_LEN-1) == 0) { |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 168 | current_driver = drv; |
| 169 | ret = true; |
| 170 | } |
| 171 | |
| 172 | read_unlock_irqrestore(&driver_name_lock, flags); |
| 173 | put_driver(drv); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 181 | #define err_printk(dev, entry, format, arg...) do { \ |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 182 | error_count += 1; \ |
Joerg Roedel | 2e507d8 | 2009-05-22 18:24:20 +0200 | [diff] [blame] | 183 | if (driver_filter(dev) && \ |
| 184 | (show_all_errors || show_num_errors > 0)) { \ |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 185 | WARN(1, "%s %s: " format, \ |
| 186 | dev_driver_string(dev), \ |
| 187 | dev_name(dev) , ## arg); \ |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 188 | dump_entry_trace(entry); \ |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 189 | } \ |
| 190 | if (!show_all_errors && show_num_errors > 0) \ |
| 191 | show_num_errors -= 1; \ |
| 192 | } while (0); |
| 193 | |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Hash related functions |
| 196 | * |
| 197 | * Every DMA-API request is saved into a struct dma_debug_entry. To |
| 198 | * have quick access to these structs they are stored into a hash. |
| 199 | */ |
| 200 | static int hash_fn(struct dma_debug_entry *entry) |
| 201 | { |
| 202 | /* |
| 203 | * Hash function is based on the dma address. |
| 204 | * We use bits 20-27 here as the index into the hash |
| 205 | */ |
| 206 | return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Request exclusive access to a hash bucket for a given dma_debug_entry. |
| 211 | */ |
| 212 | static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry, |
| 213 | unsigned long *flags) |
| 214 | { |
| 215 | int idx = hash_fn(entry); |
| 216 | unsigned long __flags; |
| 217 | |
| 218 | spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags); |
| 219 | *flags = __flags; |
| 220 | return &dma_entry_hash[idx]; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Give up exclusive access to the hash bucket |
| 225 | */ |
| 226 | static void put_hash_bucket(struct hash_bucket *bucket, |
| 227 | unsigned long *flags) |
| 228 | { |
| 229 | unsigned long __flags = *flags; |
| 230 | |
| 231 | spin_unlock_irqrestore(&bucket->lock, __flags); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Search a given entry in the hash bucket list |
| 236 | */ |
| 237 | static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket, |
| 238 | struct dma_debug_entry *ref) |
| 239 | { |
| 240 | struct dma_debug_entry *entry; |
| 241 | |
| 242 | list_for_each_entry(entry, &bucket->list, list) { |
| 243 | if ((entry->dev_addr == ref->dev_addr) && |
| 244 | (entry->dev == ref->dev)) |
| 245 | return entry; |
| 246 | } |
| 247 | |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Add an entry to a hash bucket |
| 253 | */ |
| 254 | static void hash_bucket_add(struct hash_bucket *bucket, |
| 255 | struct dma_debug_entry *entry) |
| 256 | { |
| 257 | list_add_tail(&entry->list, &bucket->list); |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Remove entry from a hash bucket list |
| 262 | */ |
| 263 | static void hash_bucket_del(struct dma_debug_entry *entry) |
| 264 | { |
| 265 | list_del(&entry->list); |
| 266 | } |
| 267 | |
| 268 | /* |
David Woodhouse | ac26c18 | 2009-02-12 16:19:13 +0100 | [diff] [blame] | 269 | * Dump mapping entries for debugging purposes |
| 270 | */ |
| 271 | void debug_dma_dump_mappings(struct device *dev) |
| 272 | { |
| 273 | int idx; |
| 274 | |
| 275 | for (idx = 0; idx < HASH_SIZE; idx++) { |
| 276 | struct hash_bucket *bucket = &dma_entry_hash[idx]; |
| 277 | struct dma_debug_entry *entry; |
| 278 | unsigned long flags; |
| 279 | |
| 280 | spin_lock_irqsave(&bucket->lock, flags); |
| 281 | |
| 282 | list_for_each_entry(entry, &bucket->list, list) { |
| 283 | if (!dev || dev == entry->dev) { |
| 284 | dev_info(entry->dev, |
| 285 | "%s idx %d P=%Lx D=%Lx L=%Lx %s\n", |
| 286 | type2name[entry->type], idx, |
| 287 | (unsigned long long)entry->paddr, |
| 288 | entry->dev_addr, entry->size, |
| 289 | dir2name[entry->direction]); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | spin_unlock_irqrestore(&bucket->lock, flags); |
| 294 | } |
| 295 | } |
| 296 | EXPORT_SYMBOL(debug_dma_dump_mappings); |
| 297 | |
| 298 | /* |
Joerg Roedel | 30dfa90 | 2009-01-09 12:34:49 +0100 | [diff] [blame] | 299 | * Wrapper function for adding an entry to the hash. |
| 300 | * This function takes care of locking itself. |
| 301 | */ |
| 302 | static void add_dma_entry(struct dma_debug_entry *entry) |
| 303 | { |
| 304 | struct hash_bucket *bucket; |
| 305 | unsigned long flags; |
| 306 | |
| 307 | bucket = get_hash_bucket(entry, &flags); |
| 308 | hash_bucket_add(bucket, entry); |
| 309 | put_hash_bucket(bucket, &flags); |
| 310 | } |
| 311 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 312 | static struct dma_debug_entry *__dma_entry_alloc(void) |
| 313 | { |
| 314 | struct dma_debug_entry *entry; |
| 315 | |
| 316 | entry = list_entry(free_entries.next, struct dma_debug_entry, list); |
| 317 | list_del(&entry->list); |
| 318 | memset(entry, 0, sizeof(*entry)); |
| 319 | |
| 320 | num_free_entries -= 1; |
| 321 | if (num_free_entries < min_free_entries) |
| 322 | min_free_entries = num_free_entries; |
| 323 | |
| 324 | return entry; |
| 325 | } |
| 326 | |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 327 | /* struct dma_entry allocator |
| 328 | * |
| 329 | * The next two functions implement the allocator for |
| 330 | * struct dma_debug_entries. |
| 331 | */ |
| 332 | static struct dma_debug_entry *dma_entry_alloc(void) |
| 333 | { |
| 334 | struct dma_debug_entry *entry = NULL; |
| 335 | unsigned long flags; |
| 336 | |
| 337 | spin_lock_irqsave(&free_entries_lock, flags); |
| 338 | |
| 339 | if (list_empty(&free_entries)) { |
| 340 | printk(KERN_ERR "DMA-API: debugging out of memory " |
| 341 | "- disabling\n"); |
| 342 | global_disable = true; |
| 343 | goto out; |
| 344 | } |
| 345 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 346 | entry = __dma_entry_alloc(); |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 347 | |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 348 | #ifdef CONFIG_STACKTRACE |
| 349 | entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES; |
| 350 | entry->stacktrace.entries = entry->st_entries; |
| 351 | entry->stacktrace.skip = 2; |
| 352 | save_stack_trace(&entry->stacktrace); |
| 353 | #endif |
Joerg Roedel | 3b1e79e | 2009-01-09 12:42:46 +0100 | [diff] [blame] | 354 | |
| 355 | out: |
| 356 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 357 | |
| 358 | return entry; |
| 359 | } |
| 360 | |
| 361 | static void dma_entry_free(struct dma_debug_entry *entry) |
| 362 | { |
| 363 | unsigned long flags; |
| 364 | |
| 365 | /* |
| 366 | * add to beginning of the list - this way the entries are |
| 367 | * more likely cache hot when they are reallocated. |
| 368 | */ |
| 369 | spin_lock_irqsave(&free_entries_lock, flags); |
| 370 | list_add(&entry->list, &free_entries); |
| 371 | num_free_entries += 1; |
| 372 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 373 | } |
| 374 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 375 | int dma_debug_resize_entries(u32 num_entries) |
| 376 | { |
| 377 | int i, delta, ret = 0; |
| 378 | unsigned long flags; |
| 379 | struct dma_debug_entry *entry; |
| 380 | LIST_HEAD(tmp); |
| 381 | |
| 382 | spin_lock_irqsave(&free_entries_lock, flags); |
| 383 | |
| 384 | if (nr_total_entries < num_entries) { |
| 385 | delta = num_entries - nr_total_entries; |
| 386 | |
| 387 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 388 | |
| 389 | for (i = 0; i < delta; i++) { |
| 390 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 391 | if (!entry) |
| 392 | break; |
| 393 | |
| 394 | list_add_tail(&entry->list, &tmp); |
| 395 | } |
| 396 | |
| 397 | spin_lock_irqsave(&free_entries_lock, flags); |
| 398 | |
| 399 | list_splice(&tmp, &free_entries); |
| 400 | nr_total_entries += i; |
| 401 | num_free_entries += i; |
| 402 | } else { |
| 403 | delta = nr_total_entries - num_entries; |
| 404 | |
| 405 | for (i = 0; i < delta && !list_empty(&free_entries); i++) { |
| 406 | entry = __dma_entry_alloc(); |
| 407 | kfree(entry); |
| 408 | } |
| 409 | |
| 410 | nr_total_entries -= i; |
| 411 | } |
| 412 | |
| 413 | if (nr_total_entries != num_entries) |
| 414 | ret = 1; |
| 415 | |
| 416 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | EXPORT_SYMBOL(dma_debug_resize_entries); |
| 421 | |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 422 | /* |
| 423 | * DMA-API debugging init code |
| 424 | * |
| 425 | * The init code does two things: |
| 426 | * 1. Initialize core data structures |
| 427 | * 2. Preallocate a given number of dma_debug_entry structs |
| 428 | */ |
| 429 | |
| 430 | static int prealloc_memory(u32 num_entries) |
| 431 | { |
| 432 | struct dma_debug_entry *entry, *next_entry; |
| 433 | int i; |
| 434 | |
| 435 | for (i = 0; i < num_entries; ++i) { |
| 436 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 437 | if (!entry) |
| 438 | goto out_err; |
| 439 | |
| 440 | list_add_tail(&entry->list, &free_entries); |
| 441 | } |
| 442 | |
| 443 | num_free_entries = num_entries; |
| 444 | min_free_entries = num_entries; |
| 445 | |
| 446 | printk(KERN_INFO "DMA-API: preallocated %d debug entries\n", |
| 447 | num_entries); |
| 448 | |
| 449 | return 0; |
| 450 | |
| 451 | out_err: |
| 452 | |
| 453 | list_for_each_entry_safe(entry, next_entry, &free_entries, list) { |
| 454 | list_del(&entry->list); |
| 455 | kfree(entry); |
| 456 | } |
| 457 | |
| 458 | return -ENOMEM; |
| 459 | } |
| 460 | |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame^] | 461 | static ssize_t filter_read(struct file *file, char __user *user_buf, |
| 462 | size_t count, loff_t *ppos) |
| 463 | { |
| 464 | unsigned long flags; |
| 465 | char buf[NAME_MAX_LEN + 1]; |
| 466 | int len; |
| 467 | |
| 468 | if (!current_driver_name[0]) |
| 469 | return 0; |
| 470 | |
| 471 | /* |
| 472 | * We can't copy to userspace directly because current_driver_name can |
| 473 | * only be read under the driver_name_lock with irqs disabled. So |
| 474 | * create a temporary copy first. |
| 475 | */ |
| 476 | read_lock_irqsave(&driver_name_lock, flags); |
| 477 | len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name); |
| 478 | read_unlock_irqrestore(&driver_name_lock, flags); |
| 479 | |
| 480 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 481 | } |
| 482 | |
| 483 | static ssize_t filter_write(struct file *file, const char __user *userbuf, |
| 484 | size_t count, loff_t *ppos) |
| 485 | { |
| 486 | unsigned long flags; |
| 487 | char buf[NAME_MAX_LEN]; |
| 488 | size_t len = NAME_MAX_LEN - 1; |
| 489 | int i; |
| 490 | |
| 491 | /* |
| 492 | * We can't copy from userspace directly. Access to |
| 493 | * current_driver_name is protected with a write_lock with irqs |
| 494 | * disabled. Since copy_from_user can fault and may sleep we |
| 495 | * need to copy to temporary buffer first |
| 496 | */ |
| 497 | len = min(count, len); |
| 498 | if (copy_from_user(buf, userbuf, len)) |
| 499 | return -EFAULT; |
| 500 | |
| 501 | buf[len] = 0; |
| 502 | |
| 503 | write_lock_irqsave(&driver_name_lock, flags); |
| 504 | |
| 505 | /* Now handle the string we got from userspace very carefully. |
| 506 | * The rules are: |
| 507 | * - only use the first token we got |
| 508 | * - token delimiter is everything looking like a space |
| 509 | * character (' ', '\n', '\t' ...) |
| 510 | * |
| 511 | */ |
| 512 | if (!isalnum(buf[0])) { |
| 513 | /* |
| 514 | If the first character userspace gave us is not |
| 515 | * alphanumerical then assume the filter should be |
| 516 | * switched off. |
| 517 | */ |
| 518 | if (current_driver_name[0]) |
| 519 | printk(KERN_INFO "DMA-API: switching off dma-debug " |
| 520 | "driver filter\n"); |
| 521 | current_driver_name[0] = 0; |
| 522 | current_driver = NULL; |
| 523 | goto out_unlock; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Now parse out the first token and use it as the name for the |
| 528 | * driver to filter for. |
| 529 | */ |
| 530 | for (i = 0; i < NAME_MAX_LEN; ++i) { |
| 531 | current_driver_name[i] = buf[i]; |
| 532 | if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0) |
| 533 | break; |
| 534 | } |
| 535 | current_driver_name[i] = 0; |
| 536 | current_driver = NULL; |
| 537 | |
| 538 | printk(KERN_INFO "DMA-API: enable driver filter for driver [%s]\n", |
| 539 | current_driver_name); |
| 540 | |
| 541 | out_unlock: |
| 542 | write_unlock_irqrestore(&driver_name_lock, flags); |
| 543 | |
| 544 | return count; |
| 545 | } |
| 546 | |
| 547 | const struct file_operations filter_fops = { |
| 548 | .read = filter_read, |
| 549 | .write = filter_write, |
| 550 | }; |
| 551 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 552 | static int dma_debug_fs_init(void) |
| 553 | { |
| 554 | dma_debug_dent = debugfs_create_dir("dma-api", NULL); |
| 555 | if (!dma_debug_dent) { |
| 556 | printk(KERN_ERR "DMA-API: can not create debugfs directory\n"); |
| 557 | return -ENOMEM; |
| 558 | } |
| 559 | |
| 560 | global_disable_dent = debugfs_create_bool("disabled", 0444, |
| 561 | dma_debug_dent, |
| 562 | (u32 *)&global_disable); |
| 563 | if (!global_disable_dent) |
| 564 | goto out_err; |
| 565 | |
| 566 | error_count_dent = debugfs_create_u32("error_count", 0444, |
| 567 | dma_debug_dent, &error_count); |
| 568 | if (!error_count_dent) |
| 569 | goto out_err; |
| 570 | |
| 571 | show_all_errors_dent = debugfs_create_u32("all_errors", 0644, |
| 572 | dma_debug_dent, |
| 573 | &show_all_errors); |
| 574 | if (!show_all_errors_dent) |
| 575 | goto out_err; |
| 576 | |
| 577 | show_num_errors_dent = debugfs_create_u32("num_errors", 0644, |
| 578 | dma_debug_dent, |
| 579 | &show_num_errors); |
| 580 | if (!show_num_errors_dent) |
| 581 | goto out_err; |
| 582 | |
| 583 | num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444, |
| 584 | dma_debug_dent, |
| 585 | &num_free_entries); |
| 586 | if (!num_free_entries_dent) |
| 587 | goto out_err; |
| 588 | |
| 589 | min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444, |
| 590 | dma_debug_dent, |
| 591 | &min_free_entries); |
| 592 | if (!min_free_entries_dent) |
| 593 | goto out_err; |
| 594 | |
Joerg Roedel | 8a6fc70 | 2009-05-22 21:23:13 +0200 | [diff] [blame^] | 595 | filter_dent = debugfs_create_file("driver_filter", 0644, |
| 596 | dma_debug_dent, NULL, &filter_fops); |
| 597 | if (!filter_dent) |
| 598 | goto out_err; |
| 599 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 600 | return 0; |
| 601 | |
| 602 | out_err: |
| 603 | debugfs_remove_recursive(dma_debug_dent); |
| 604 | |
| 605 | return -ENOMEM; |
| 606 | } |
| 607 | |
Joerg Roedel | 41531c8 | 2009-03-16 17:32:14 +0100 | [diff] [blame] | 608 | void dma_debug_add_bus(struct bus_type *bus) |
| 609 | { |
Joerg Roedel | 314eeac | 2009-04-24 14:35:57 +0200 | [diff] [blame] | 610 | /* FIXME: register notifier */ |
Joerg Roedel | 41531c8 | 2009-03-16 17:32:14 +0100 | [diff] [blame] | 611 | } |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 612 | |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 613 | /* |
| 614 | * Let the architectures decide how many entries should be preallocated. |
| 615 | */ |
| 616 | void dma_debug_init(u32 num_entries) |
| 617 | { |
| 618 | int i; |
| 619 | |
| 620 | if (global_disable) |
| 621 | return; |
| 622 | |
| 623 | for (i = 0; i < HASH_SIZE; ++i) { |
| 624 | INIT_LIST_HEAD(&dma_entry_hash[i].list); |
| 625 | dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED; |
| 626 | } |
| 627 | |
Joerg Roedel | 788dcfa | 2009-01-09 13:13:27 +0100 | [diff] [blame] | 628 | if (dma_debug_fs_init() != 0) { |
| 629 | printk(KERN_ERR "DMA-API: error creating debugfs entries " |
| 630 | "- disabling\n"); |
| 631 | global_disable = true; |
| 632 | |
| 633 | return; |
| 634 | } |
| 635 | |
Joerg Roedel | 59d3daa | 2009-01-09 13:01:56 +0100 | [diff] [blame] | 636 | if (req_entries) |
| 637 | num_entries = req_entries; |
| 638 | |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 639 | if (prealloc_memory(num_entries) != 0) { |
| 640 | printk(KERN_ERR "DMA-API: debugging out of memory error " |
| 641 | "- disabled\n"); |
| 642 | global_disable = true; |
| 643 | |
| 644 | return; |
| 645 | } |
| 646 | |
FUJITA Tomonori | e6a1a89 | 2009-04-15 18:22:41 +0900 | [diff] [blame] | 647 | nr_total_entries = num_free_entries; |
| 648 | |
Joerg Roedel | 6bf0787 | 2009-01-09 12:54:42 +0100 | [diff] [blame] | 649 | printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n"); |
| 650 | } |
| 651 | |
Joerg Roedel | 59d3daa | 2009-01-09 13:01:56 +0100 | [diff] [blame] | 652 | static __init int dma_debug_cmdline(char *str) |
| 653 | { |
| 654 | if (!str) |
| 655 | return -EINVAL; |
| 656 | |
| 657 | if (strncmp(str, "off", 3) == 0) { |
| 658 | printk(KERN_INFO "DMA-API: debugging disabled on kernel " |
| 659 | "command line\n"); |
| 660 | global_disable = true; |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static __init int dma_debug_entries_cmdline(char *str) |
| 667 | { |
| 668 | int res; |
| 669 | |
| 670 | if (!str) |
| 671 | return -EINVAL; |
| 672 | |
| 673 | res = get_option(&str, &req_entries); |
| 674 | |
| 675 | if (!res) |
| 676 | req_entries = 0; |
| 677 | |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | __setup("dma_debug=", dma_debug_cmdline); |
| 682 | __setup("dma_debug_entries=", dma_debug_entries_cmdline); |
| 683 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 684 | static void check_unmap(struct dma_debug_entry *ref) |
| 685 | { |
| 686 | struct dma_debug_entry *entry; |
| 687 | struct hash_bucket *bucket; |
| 688 | unsigned long flags; |
| 689 | |
FUJITA Tomonori | 35d4095 | 2009-03-19 10:39:31 +0900 | [diff] [blame] | 690 | if (dma_mapping_error(ref->dev, ref->dev_addr)) { |
| 691 | err_printk(ref->dev, NULL, "DMA-API: device driver tries " |
| 692 | "to free an invalid DMA memory address\n"); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 693 | return; |
FUJITA Tomonori | 35d4095 | 2009-03-19 10:39:31 +0900 | [diff] [blame] | 694 | } |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 695 | |
| 696 | bucket = get_hash_bucket(ref, &flags); |
| 697 | entry = hash_bucket_find(bucket, ref); |
| 698 | |
| 699 | if (!entry) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 700 | err_printk(ref->dev, NULL, "DMA-API: device driver tries " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 701 | "to free DMA memory it has not allocated " |
| 702 | "[device address=0x%016llx] [size=%llu bytes]\n", |
| 703 | ref->dev_addr, ref->size); |
| 704 | goto out; |
| 705 | } |
| 706 | |
| 707 | if (ref->size != entry->size) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 708 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 709 | "DMA memory with different size " |
| 710 | "[device address=0x%016llx] [map size=%llu bytes] " |
| 711 | "[unmap size=%llu bytes]\n", |
| 712 | ref->dev_addr, entry->size, ref->size); |
| 713 | } |
| 714 | |
| 715 | if (ref->type != entry->type) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 716 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 717 | "DMA memory with wrong function " |
| 718 | "[device address=0x%016llx] [size=%llu bytes] " |
| 719 | "[mapped as %s] [unmapped as %s]\n", |
| 720 | ref->dev_addr, ref->size, |
| 721 | type2name[entry->type], type2name[ref->type]); |
| 722 | } else if ((entry->type == dma_debug_coherent) && |
| 723 | (ref->paddr != entry->paddr)) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 724 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 725 | "DMA memory with different CPU address " |
| 726 | "[device address=0x%016llx] [size=%llu bytes] " |
| 727 | "[cpu alloc address=%p] [cpu free address=%p]", |
| 728 | ref->dev_addr, ref->size, |
| 729 | (void *)entry->paddr, (void *)ref->paddr); |
| 730 | } |
| 731 | |
| 732 | if (ref->sg_call_ents && ref->type == dma_debug_sg && |
| 733 | ref->sg_call_ents != entry->sg_call_ents) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 734 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 735 | "DMA sg list with different entry count " |
| 736 | "[map count=%d] [unmap count=%d]\n", |
| 737 | entry->sg_call_ents, ref->sg_call_ents); |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * This may be no bug in reality - but most implementations of the |
| 742 | * DMA API don't handle this properly, so check for it here |
| 743 | */ |
| 744 | if (ref->direction != entry->direction) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 745 | err_printk(ref->dev, entry, "DMA-API: device driver frees " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 746 | "DMA memory with different direction " |
| 747 | "[device address=0x%016llx] [size=%llu bytes] " |
| 748 | "[mapped with %s] [unmapped with %s]\n", |
| 749 | ref->dev_addr, ref->size, |
| 750 | dir2name[entry->direction], |
| 751 | dir2name[ref->direction]); |
| 752 | } |
| 753 | |
| 754 | hash_bucket_del(entry); |
| 755 | dma_entry_free(entry); |
| 756 | |
| 757 | out: |
| 758 | put_hash_bucket(bucket, &flags); |
| 759 | } |
| 760 | |
| 761 | static void check_for_stack(struct device *dev, void *addr) |
| 762 | { |
| 763 | if (object_is_on_stack(addr)) |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 764 | err_printk(dev, NULL, "DMA-API: device driver maps memory from" |
| 765 | "stack [addr=%p]\n", addr); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 768 | static inline bool overlap(void *addr, u64 size, void *start, void *end) |
| 769 | { |
| 770 | void *addr2 = (char *)addr + size; |
| 771 | |
| 772 | return ((addr >= start && addr < end) || |
| 773 | (addr2 >= start && addr2 < end) || |
| 774 | ((addr < start) && (addr2 >= end))); |
| 775 | } |
| 776 | |
| 777 | static void check_for_illegal_area(struct device *dev, void *addr, u64 size) |
| 778 | { |
| 779 | if (overlap(addr, size, _text, _etext) || |
| 780 | overlap(addr, size, __start_rodata, __end_rodata)) |
| 781 | err_printk(dev, NULL, "DMA-API: device driver maps " |
| 782 | "memory from kernel text or rodata " |
| 783 | "[addr=%p] [size=%llu]\n", addr, size); |
| 784 | } |
| 785 | |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 786 | static void check_sync(struct device *dev, dma_addr_t addr, |
| 787 | u64 size, u64 offset, int direction, bool to_cpu) |
| 788 | { |
| 789 | struct dma_debug_entry ref = { |
| 790 | .dev = dev, |
| 791 | .dev_addr = addr, |
| 792 | .size = size, |
| 793 | .direction = direction, |
| 794 | }; |
| 795 | struct dma_debug_entry *entry; |
| 796 | struct hash_bucket *bucket; |
| 797 | unsigned long flags; |
| 798 | |
| 799 | bucket = get_hash_bucket(&ref, &flags); |
| 800 | |
| 801 | entry = hash_bucket_find(bucket, &ref); |
| 802 | |
| 803 | if (!entry) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 804 | err_printk(dev, NULL, "DMA-API: device driver tries " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 805 | "to sync DMA memory it has not allocated " |
| 806 | "[device address=0x%016llx] [size=%llu bytes]\n", |
Randy Dunlap | 93c36ed | 2009-03-30 14:08:44 -0700 | [diff] [blame] | 807 | (unsigned long long)addr, size); |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | if ((offset + size) > entry->size) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 812 | err_printk(dev, entry, "DMA-API: device driver syncs" |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 813 | " DMA memory outside allocated range " |
| 814 | "[device address=0x%016llx] " |
| 815 | "[allocation size=%llu bytes] [sync offset=%llu] " |
| 816 | "[sync size=%llu]\n", entry->dev_addr, entry->size, |
| 817 | offset, size); |
| 818 | } |
| 819 | |
| 820 | if (direction != entry->direction) { |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 821 | err_printk(dev, entry, "DMA-API: device driver syncs " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 822 | "DMA memory with different direction " |
| 823 | "[device address=0x%016llx] [size=%llu bytes] " |
| 824 | "[mapped with %s] [synced with %s]\n", |
Randy Dunlap | 93c36ed | 2009-03-30 14:08:44 -0700 | [diff] [blame] | 825 | (unsigned long long)addr, entry->size, |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 826 | dir2name[entry->direction], |
| 827 | dir2name[direction]); |
| 828 | } |
| 829 | |
| 830 | if (entry->direction == DMA_BIDIRECTIONAL) |
| 831 | goto out; |
| 832 | |
| 833 | if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) && |
| 834 | !(direction == DMA_TO_DEVICE)) |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 835 | err_printk(dev, entry, "DMA-API: device driver syncs " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 836 | "device read-only DMA memory for cpu " |
| 837 | "[device address=0x%016llx] [size=%llu bytes] " |
| 838 | "[mapped with %s] [synced with %s]\n", |
Randy Dunlap | 93c36ed | 2009-03-30 14:08:44 -0700 | [diff] [blame] | 839 | (unsigned long long)addr, entry->size, |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 840 | dir2name[entry->direction], |
| 841 | dir2name[direction]); |
| 842 | |
| 843 | if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) && |
| 844 | !(direction == DMA_FROM_DEVICE)) |
David Woodhouse | 6c132d1 | 2009-01-19 16:52:39 +0100 | [diff] [blame] | 845 | err_printk(dev, entry, "DMA-API: device driver syncs " |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 846 | "device write-only DMA memory to device " |
| 847 | "[device address=0x%016llx] [size=%llu bytes] " |
| 848 | "[mapped with %s] [synced with %s]\n", |
Randy Dunlap | 93c36ed | 2009-03-30 14:08:44 -0700 | [diff] [blame] | 849 | (unsigned long long)addr, entry->size, |
Joerg Roedel | 2d62ece | 2009-01-09 14:10:26 +0100 | [diff] [blame] | 850 | dir2name[entry->direction], |
| 851 | dir2name[direction]); |
| 852 | |
| 853 | out: |
| 854 | put_hash_bucket(bucket, &flags); |
| 855 | |
| 856 | } |
| 857 | |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 858 | void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, |
| 859 | size_t size, int direction, dma_addr_t dma_addr, |
| 860 | bool map_single) |
| 861 | { |
| 862 | struct dma_debug_entry *entry; |
| 863 | |
| 864 | if (unlikely(global_disable)) |
| 865 | return; |
| 866 | |
| 867 | if (unlikely(dma_mapping_error(dev, dma_addr))) |
| 868 | return; |
| 869 | |
| 870 | entry = dma_entry_alloc(); |
| 871 | if (!entry) |
| 872 | return; |
| 873 | |
| 874 | entry->dev = dev; |
| 875 | entry->type = dma_debug_page; |
| 876 | entry->paddr = page_to_phys(page) + offset; |
| 877 | entry->dev_addr = dma_addr; |
| 878 | entry->size = size; |
| 879 | entry->direction = direction; |
| 880 | |
Joerg Roedel | 9537a48 | 2009-03-23 15:35:08 +0100 | [diff] [blame] | 881 | if (map_single) |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 882 | entry->type = dma_debug_single; |
Joerg Roedel | 9537a48 | 2009-03-23 15:35:08 +0100 | [diff] [blame] | 883 | |
| 884 | if (!PageHighMem(page)) { |
| 885 | void *addr = ((char *)page_address(page)) + offset; |
Joerg Roedel | 2e34bde | 2009-03-16 16:51:55 +0100 | [diff] [blame] | 886 | check_for_stack(dev, addr); |
| 887 | check_for_illegal_area(dev, addr, size); |
Joerg Roedel | f62bc98 | 2009-01-09 14:14:49 +0100 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | add_dma_entry(entry); |
| 891 | } |
| 892 | EXPORT_SYMBOL(debug_dma_map_page); |
| 893 | |
| 894 | void debug_dma_unmap_page(struct device *dev, dma_addr_t addr, |
| 895 | size_t size, int direction, bool map_single) |
| 896 | { |
| 897 | struct dma_debug_entry ref = { |
| 898 | .type = dma_debug_page, |
| 899 | .dev = dev, |
| 900 | .dev_addr = addr, |
| 901 | .size = size, |
| 902 | .direction = direction, |
| 903 | }; |
| 904 | |
| 905 | if (unlikely(global_disable)) |
| 906 | return; |
| 907 | |
| 908 | if (map_single) |
| 909 | ref.type = dma_debug_single; |
| 910 | |
| 911 | check_unmap(&ref); |
| 912 | } |
| 913 | EXPORT_SYMBOL(debug_dma_unmap_page); |
| 914 | |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 915 | void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 916 | int nents, int mapped_ents, int direction) |
| 917 | { |
| 918 | struct dma_debug_entry *entry; |
| 919 | struct scatterlist *s; |
| 920 | int i; |
| 921 | |
| 922 | if (unlikely(global_disable)) |
| 923 | return; |
| 924 | |
| 925 | for_each_sg(sg, s, mapped_ents, i) { |
| 926 | entry = dma_entry_alloc(); |
| 927 | if (!entry) |
| 928 | return; |
| 929 | |
| 930 | entry->type = dma_debug_sg; |
| 931 | entry->dev = dev; |
| 932 | entry->paddr = sg_phys(s); |
| 933 | entry->size = s->length; |
| 934 | entry->dev_addr = s->dma_address; |
| 935 | entry->direction = direction; |
| 936 | entry->sg_call_ents = nents; |
| 937 | entry->sg_mapped_ents = mapped_ents; |
| 938 | |
Joerg Roedel | 9537a48 | 2009-03-23 15:35:08 +0100 | [diff] [blame] | 939 | if (!PageHighMem(sg_page(s))) { |
| 940 | check_for_stack(dev, sg_virt(s)); |
| 941 | check_for_illegal_area(dev, sg_virt(s), s->length); |
| 942 | } |
Joerg Roedel | 972aa45 | 2009-01-09 14:19:54 +0100 | [diff] [blame] | 943 | |
| 944 | add_dma_entry(entry); |
| 945 | } |
| 946 | } |
| 947 | EXPORT_SYMBOL(debug_dma_map_sg); |
| 948 | |
| 949 | void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 950 | int nelems, int dir) |
| 951 | { |
| 952 | struct dma_debug_entry *entry; |
| 953 | struct scatterlist *s; |
| 954 | int mapped_ents = 0, i; |
| 955 | unsigned long flags; |
| 956 | |
| 957 | if (unlikely(global_disable)) |
| 958 | return; |
| 959 | |
| 960 | for_each_sg(sglist, s, nelems, i) { |
| 961 | |
| 962 | struct dma_debug_entry ref = { |
| 963 | .type = dma_debug_sg, |
| 964 | .dev = dev, |
| 965 | .paddr = sg_phys(s), |
| 966 | .dev_addr = s->dma_address, |
| 967 | .size = s->length, |
| 968 | .direction = dir, |
| 969 | .sg_call_ents = 0, |
| 970 | }; |
| 971 | |
| 972 | if (mapped_ents && i >= mapped_ents) |
| 973 | break; |
| 974 | |
| 975 | if (mapped_ents == 0) { |
| 976 | struct hash_bucket *bucket; |
| 977 | ref.sg_call_ents = nelems; |
| 978 | bucket = get_hash_bucket(&ref, &flags); |
| 979 | entry = hash_bucket_find(bucket, &ref); |
| 980 | if (entry) |
| 981 | mapped_ents = entry->sg_mapped_ents; |
| 982 | put_hash_bucket(bucket, &flags); |
| 983 | } |
| 984 | |
| 985 | check_unmap(&ref); |
| 986 | } |
| 987 | } |
| 988 | EXPORT_SYMBOL(debug_dma_unmap_sg); |
| 989 | |
Joerg Roedel | 6bfd449 | 2009-01-09 14:38:50 +0100 | [diff] [blame] | 990 | void debug_dma_alloc_coherent(struct device *dev, size_t size, |
| 991 | dma_addr_t dma_addr, void *virt) |
| 992 | { |
| 993 | struct dma_debug_entry *entry; |
| 994 | |
| 995 | if (unlikely(global_disable)) |
| 996 | return; |
| 997 | |
| 998 | if (unlikely(virt == NULL)) |
| 999 | return; |
| 1000 | |
| 1001 | entry = dma_entry_alloc(); |
| 1002 | if (!entry) |
| 1003 | return; |
| 1004 | |
| 1005 | entry->type = dma_debug_coherent; |
| 1006 | entry->dev = dev; |
| 1007 | entry->paddr = virt_to_phys(virt); |
| 1008 | entry->size = size; |
| 1009 | entry->dev_addr = dma_addr; |
| 1010 | entry->direction = DMA_BIDIRECTIONAL; |
| 1011 | |
| 1012 | add_dma_entry(entry); |
| 1013 | } |
| 1014 | EXPORT_SYMBOL(debug_dma_alloc_coherent); |
| 1015 | |
| 1016 | void debug_dma_free_coherent(struct device *dev, size_t size, |
| 1017 | void *virt, dma_addr_t addr) |
| 1018 | { |
| 1019 | struct dma_debug_entry ref = { |
| 1020 | .type = dma_debug_coherent, |
| 1021 | .dev = dev, |
| 1022 | .paddr = virt_to_phys(virt), |
| 1023 | .dev_addr = addr, |
| 1024 | .size = size, |
| 1025 | .direction = DMA_BIDIRECTIONAL, |
| 1026 | }; |
| 1027 | |
| 1028 | if (unlikely(global_disable)) |
| 1029 | return; |
| 1030 | |
| 1031 | check_unmap(&ref); |
| 1032 | } |
| 1033 | EXPORT_SYMBOL(debug_dma_free_coherent); |
| 1034 | |
Joerg Roedel | b9d2317 | 2009-01-09 14:43:04 +0100 | [diff] [blame] | 1035 | void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, |
| 1036 | size_t size, int direction) |
| 1037 | { |
| 1038 | if (unlikely(global_disable)) |
| 1039 | return; |
| 1040 | |
| 1041 | check_sync(dev, dma_handle, size, 0, direction, true); |
| 1042 | } |
| 1043 | EXPORT_SYMBOL(debug_dma_sync_single_for_cpu); |
| 1044 | |
| 1045 | void debug_dma_sync_single_for_device(struct device *dev, |
| 1046 | dma_addr_t dma_handle, size_t size, |
| 1047 | int direction) |
| 1048 | { |
| 1049 | if (unlikely(global_disable)) |
| 1050 | return; |
| 1051 | |
| 1052 | check_sync(dev, dma_handle, size, 0, direction, false); |
| 1053 | } |
| 1054 | EXPORT_SYMBOL(debug_dma_sync_single_for_device); |
| 1055 | |
Joerg Roedel | 948408b | 2009-01-09 14:55:38 +0100 | [diff] [blame] | 1056 | void debug_dma_sync_single_range_for_cpu(struct device *dev, |
| 1057 | dma_addr_t dma_handle, |
| 1058 | unsigned long offset, size_t size, |
| 1059 | int direction) |
| 1060 | { |
| 1061 | if (unlikely(global_disable)) |
| 1062 | return; |
| 1063 | |
| 1064 | check_sync(dev, dma_handle, size, offset, direction, true); |
| 1065 | } |
| 1066 | EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu); |
| 1067 | |
| 1068 | void debug_dma_sync_single_range_for_device(struct device *dev, |
| 1069 | dma_addr_t dma_handle, |
| 1070 | unsigned long offset, |
| 1071 | size_t size, int direction) |
| 1072 | { |
| 1073 | if (unlikely(global_disable)) |
| 1074 | return; |
| 1075 | |
| 1076 | check_sync(dev, dma_handle, size, offset, direction, false); |
| 1077 | } |
| 1078 | EXPORT_SYMBOL(debug_dma_sync_single_range_for_device); |
| 1079 | |
Joerg Roedel | a31fba5 | 2009-01-09 15:01:12 +0100 | [diff] [blame] | 1080 | void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
| 1081 | int nelems, int direction) |
| 1082 | { |
| 1083 | struct scatterlist *s; |
| 1084 | int i; |
| 1085 | |
| 1086 | if (unlikely(global_disable)) |
| 1087 | return; |
| 1088 | |
| 1089 | for_each_sg(sg, s, nelems, i) { |
| 1090 | check_sync(dev, s->dma_address, s->dma_length, 0, |
| 1091 | direction, true); |
| 1092 | } |
| 1093 | } |
| 1094 | EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu); |
| 1095 | |
| 1096 | void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
| 1097 | int nelems, int direction) |
| 1098 | { |
| 1099 | struct scatterlist *s; |
| 1100 | int i; |
| 1101 | |
| 1102 | if (unlikely(global_disable)) |
| 1103 | return; |
| 1104 | |
| 1105 | for_each_sg(sg, s, nelems, i) { |
| 1106 | check_sync(dev, s->dma_address, s->dma_length, 0, |
| 1107 | direction, false); |
| 1108 | } |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(debug_dma_sync_sg_for_device); |
| 1111 | |