blob: 65b0d99b6d0aa8f7219741e731394d1514b1706e [file] [log] [blame]
Joerg Roedelf2f45e52009-01-09 12:19:52 +01001/*
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 Roedel972aa452009-01-09 14:19:54 +010020#include <linux/scatterlist.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010021#include <linux/dma-mapping.h>
David Woodhouse6c132d12009-01-19 16:52:39 +010022#include <linux/stacktrace.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010023#include <linux/dma-debug.h>
Joerg Roedel30dfa902009-01-09 12:34:49 +010024#include <linux/spinlock.h>
Joerg Roedel788dcfa2009-01-09 13:13:27 +010025#include <linux/debugfs.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020026#include <linux/uaccess.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010027#include <linux/device.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010028#include <linux/types.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010029#include <linux/sched.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020030#include <linux/ctype.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010031#include <linux/list.h>
Joerg Roedel6bf07872009-01-09 12:54:42 +010032#include <linux/slab.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010033
Joerg Roedel2e34bde2009-03-16 16:51:55 +010034#include <asm/sections.h>
35
Joerg Roedel30dfa902009-01-09 12:34:49 +010036#define HASH_SIZE 1024ULL
37#define HASH_FN_SHIFT 13
38#define HASH_FN_MASK (HASH_SIZE - 1)
39
Joerg Roedelf2f45e52009-01-09 12:19:52 +010040enum {
41 dma_debug_single,
42 dma_debug_page,
43 dma_debug_sg,
44 dma_debug_coherent,
45};
46
David Woodhouse6c132d12009-01-19 16:52:39 +010047#define DMA_DEBUG_STACKTRACE_ENTRIES 5
48
Joerg Roedelf2f45e52009-01-09 12:19:52 +010049struct 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 Woodhouse6c132d12009-01-19 16:52:39 +010059#ifdef CONFIG_STACKTRACE
60 struct stack_trace stacktrace;
61 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
62#endif
Joerg Roedelf2f45e52009-01-09 12:19:52 +010063};
64
Joerg Roedel30dfa902009-01-09 12:34:49 +010065struct hash_bucket {
66 struct list_head list;
67 spinlock_t lock;
Joerg Roedel2d62ece2009-01-09 14:10:26 +010068} ____cacheline_aligned_in_smp;
Joerg Roedel30dfa902009-01-09 12:34:49 +010069
70/* Hash list to save the allocated dma addresses */
71static struct hash_bucket dma_entry_hash[HASH_SIZE];
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010072/* List of pre-allocated dma_debug_entry's */
73static LIST_HEAD(free_entries);
74/* Lock for the list above */
75static DEFINE_SPINLOCK(free_entries_lock);
76
77/* Global disable flag - will be set in case of an error */
78static bool global_disable __read_mostly;
79
Joerg Roedel788dcfa2009-01-09 13:13:27 +010080/* Global error count */
81static u32 error_count;
82
83/* Global error show enable*/
84static u32 show_all_errors __read_mostly;
85/* Number of errors to show */
86static u32 show_num_errors = 1;
87
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010088static u32 num_free_entries;
89static u32 min_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +090090static u32 nr_total_entries;
Joerg Roedel30dfa902009-01-09 12:34:49 +010091
Joerg Roedel59d3daa2009-01-09 13:01:56 +010092/* number of preallocated entries requested by kernel cmdline */
93static u32 req_entries;
94
Joerg Roedel788dcfa2009-01-09 13:13:27 +010095/* debugfs dentry's for the stuff above */
96static struct dentry *dma_debug_dent __read_mostly;
97static struct dentry *global_disable_dent __read_mostly;
98static struct dentry *error_count_dent __read_mostly;
99static struct dentry *show_all_errors_dent __read_mostly;
100static struct dentry *show_num_errors_dent __read_mostly;
101static struct dentry *num_free_entries_dent __read_mostly;
102static struct dentry *min_free_entries_dent __read_mostly;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200103static struct dentry *filter_dent __read_mostly;
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100104
Joerg Roedel2e507d82009-05-22 18:24:20 +0200105/* per-driver filter related state */
106
107#define NAME_MAX_LEN 64
108
109static char current_driver_name[NAME_MAX_LEN] __read_mostly;
110static struct device_driver *current_driver __read_mostly;
111
112static DEFINE_RWLOCK(driver_name_lock);
Joerg Roedel30dfa902009-01-09 12:34:49 +0100113
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100114static const char *type2name[4] = { "single", "page",
115 "scather-gather", "coherent" };
116
117static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
118 "DMA_FROM_DEVICE", "DMA_NONE" };
119
Joerg Roedeled888ae2009-05-22 17:16:04 +0200120/* little merge helper - remove it after the merge window */
121#ifndef BUS_NOTIFY_UNBOUND_DRIVER
122#define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
123#endif
124
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100125/*
126 * The access to some variables in this macro is racy. We can't use atomic_t
127 * here because all these variables are exported to debugfs. Some of them even
128 * writeable. This is also the reason why a lock won't help much. But anyway,
129 * the races are no big deal. Here is why:
130 *
131 * error_count: the addition is racy, but the worst thing that can happen is
132 * that we don't count some errors
133 * show_num_errors: the subtraction is racy. Also no big deal because in
134 * worst case this will result in one warning more in the
135 * system log than the user configured. This variable is
136 * writeable via debugfs.
137 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100138static inline void dump_entry_trace(struct dma_debug_entry *entry)
139{
140#ifdef CONFIG_STACKTRACE
141 if (entry) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200142 pr_warning("Mapped at:\n");
David Woodhouse6c132d12009-01-19 16:52:39 +0100143 print_stack_trace(&entry->stacktrace, 0);
144 }
145#endif
146}
147
Joerg Roedel2e507d82009-05-22 18:24:20 +0200148static bool driver_filter(struct device *dev)
149{
Joerg Roedel0bf84122009-06-08 15:53:46 +0200150 struct device_driver *drv;
151 unsigned long flags;
152 bool ret;
153
Joerg Roedel2e507d82009-05-22 18:24:20 +0200154 /* driver filter off */
155 if (likely(!current_driver_name[0]))
156 return true;
157
158 /* driver filter on and initialized */
159 if (current_driver && dev->driver == current_driver)
160 return true;
161
Joerg Roedel0bf84122009-06-08 15:53:46 +0200162 if (current_driver || !current_driver_name[0])
163 return false;
164
Joerg Roedel2e507d82009-05-22 18:24:20 +0200165 /* driver filter on but not yet initialized */
Joerg Roedel0bf84122009-06-08 15:53:46 +0200166 drv = get_driver(dev->driver);
167 if (!drv)
168 return false;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200169
Joerg Roedel0bf84122009-06-08 15:53:46 +0200170 /* lock to protect against change of current_driver_name */
171 read_lock_irqsave(&driver_name_lock, flags);
Joerg Roedel2e507d82009-05-22 18:24:20 +0200172
Joerg Roedel0bf84122009-06-08 15:53:46 +0200173 ret = false;
174 if (drv->name &&
175 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
176 current_driver = drv;
177 ret = true;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200178 }
179
Joerg Roedel0bf84122009-06-08 15:53:46 +0200180 read_unlock_irqrestore(&driver_name_lock, flags);
181 put_driver(drv);
182
183 return ret;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200184}
185
David Woodhouse6c132d12009-01-19 16:52:39 +0100186#define err_printk(dev, entry, format, arg...) do { \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100187 error_count += 1; \
Joerg Roedel2e507d82009-05-22 18:24:20 +0200188 if (driver_filter(dev) && \
189 (show_all_errors || show_num_errors > 0)) { \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100190 WARN(1, "%s %s: " format, \
191 dev_driver_string(dev), \
192 dev_name(dev) , ## arg); \
David Woodhouse6c132d12009-01-19 16:52:39 +0100193 dump_entry_trace(entry); \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100194 } \
195 if (!show_all_errors && show_num_errors > 0) \
196 show_num_errors -= 1; \
197 } while (0);
198
Joerg Roedel30dfa902009-01-09 12:34:49 +0100199/*
200 * Hash related functions
201 *
202 * Every DMA-API request is saved into a struct dma_debug_entry. To
203 * have quick access to these structs they are stored into a hash.
204 */
205static int hash_fn(struct dma_debug_entry *entry)
206{
207 /*
208 * Hash function is based on the dma address.
209 * We use bits 20-27 here as the index into the hash
210 */
211 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
212}
213
214/*
215 * Request exclusive access to a hash bucket for a given dma_debug_entry.
216 */
217static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
218 unsigned long *flags)
219{
220 int idx = hash_fn(entry);
221 unsigned long __flags;
222
223 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
224 *flags = __flags;
225 return &dma_entry_hash[idx];
226}
227
228/*
229 * Give up exclusive access to the hash bucket
230 */
231static void put_hash_bucket(struct hash_bucket *bucket,
232 unsigned long *flags)
233{
234 unsigned long __flags = *flags;
235
236 spin_unlock_irqrestore(&bucket->lock, __flags);
237}
238
239/*
240 * Search a given entry in the hash bucket list
241 */
242static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
243 struct dma_debug_entry *ref)
244{
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200245 struct dma_debug_entry *entry, *ret = NULL;
246 int matches = 0, match_lvl, last_lvl = 0;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100247
248 list_for_each_entry(entry, &bucket->list, list) {
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200249 if ((entry->dev_addr != ref->dev_addr) ||
250 (entry->dev != ref->dev))
251 continue;
252
253 /*
254 * Some drivers map the same physical address multiple
255 * times. Without a hardware IOMMU this results in the
256 * same device addresses being put into the dma-debug
257 * hash multiple times too. This can result in false
258 * positives being reported. Therfore we implement a
259 * best-fit algorithm here which returns the entry from
260 * the hash which fits best to the reference value
261 * instead of the first-fit.
262 */
263 matches += 1;
264 match_lvl = 0;
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200265 entry->size == ref->size ? ++match_lvl : 0;
266 entry->type == ref->type ? ++match_lvl : 0;
267 entry->direction == ref->direction ? ++match_lvl : 0;
268 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200269
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200270 if (match_lvl == 4) {
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200271 /* perfect-fit - return the result */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100272 return entry;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200273 } else if (match_lvl > last_lvl) {
274 /*
275 * We found an entry that fits better then the
276 * previous one
277 */
278 last_lvl = match_lvl;
279 ret = entry;
280 }
Joerg Roedel30dfa902009-01-09 12:34:49 +0100281 }
282
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200283 /*
284 * If we have multiple matches but no perfect-fit, just return
285 * NULL.
286 */
287 ret = (matches == 1) ? ret : NULL;
288
289 return ret;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100290}
291
292/*
293 * Add an entry to a hash bucket
294 */
295static void hash_bucket_add(struct hash_bucket *bucket,
296 struct dma_debug_entry *entry)
297{
298 list_add_tail(&entry->list, &bucket->list);
299}
300
301/*
302 * Remove entry from a hash bucket list
303 */
304static void hash_bucket_del(struct dma_debug_entry *entry)
305{
306 list_del(&entry->list);
307}
308
309/*
David Woodhouseac26c182009-02-12 16:19:13 +0100310 * Dump mapping entries for debugging purposes
311 */
312void debug_dma_dump_mappings(struct device *dev)
313{
314 int idx;
315
316 for (idx = 0; idx < HASH_SIZE; idx++) {
317 struct hash_bucket *bucket = &dma_entry_hash[idx];
318 struct dma_debug_entry *entry;
319 unsigned long flags;
320
321 spin_lock_irqsave(&bucket->lock, flags);
322
323 list_for_each_entry(entry, &bucket->list, list) {
324 if (!dev || dev == entry->dev) {
325 dev_info(entry->dev,
326 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
327 type2name[entry->type], idx,
328 (unsigned long long)entry->paddr,
329 entry->dev_addr, entry->size,
330 dir2name[entry->direction]);
331 }
332 }
333
334 spin_unlock_irqrestore(&bucket->lock, flags);
335 }
336}
337EXPORT_SYMBOL(debug_dma_dump_mappings);
338
339/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100340 * Wrapper function for adding an entry to the hash.
341 * This function takes care of locking itself.
342 */
343static void add_dma_entry(struct dma_debug_entry *entry)
344{
345 struct hash_bucket *bucket;
346 unsigned long flags;
347
348 bucket = get_hash_bucket(entry, &flags);
349 hash_bucket_add(bucket, entry);
350 put_hash_bucket(bucket, &flags);
351}
352
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900353static struct dma_debug_entry *__dma_entry_alloc(void)
354{
355 struct dma_debug_entry *entry;
356
357 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
358 list_del(&entry->list);
359 memset(entry, 0, sizeof(*entry));
360
361 num_free_entries -= 1;
362 if (num_free_entries < min_free_entries)
363 min_free_entries = num_free_entries;
364
365 return entry;
366}
367
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100368/* struct dma_entry allocator
369 *
370 * The next two functions implement the allocator for
371 * struct dma_debug_entries.
372 */
373static struct dma_debug_entry *dma_entry_alloc(void)
374{
375 struct dma_debug_entry *entry = NULL;
376 unsigned long flags;
377
378 spin_lock_irqsave(&free_entries_lock, flags);
379
380 if (list_empty(&free_entries)) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200381 pr_err("DMA-API: debugging out of memory - disabling\n");
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100382 global_disable = true;
383 goto out;
384 }
385
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900386 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100387
David Woodhouse6c132d12009-01-19 16:52:39 +0100388#ifdef CONFIG_STACKTRACE
389 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
390 entry->stacktrace.entries = entry->st_entries;
391 entry->stacktrace.skip = 2;
392 save_stack_trace(&entry->stacktrace);
393#endif
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100394
395out:
396 spin_unlock_irqrestore(&free_entries_lock, flags);
397
398 return entry;
399}
400
401static void dma_entry_free(struct dma_debug_entry *entry)
402{
403 unsigned long flags;
404
405 /*
406 * add to beginning of the list - this way the entries are
407 * more likely cache hot when they are reallocated.
408 */
409 spin_lock_irqsave(&free_entries_lock, flags);
410 list_add(&entry->list, &free_entries);
411 num_free_entries += 1;
412 spin_unlock_irqrestore(&free_entries_lock, flags);
413}
414
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900415int dma_debug_resize_entries(u32 num_entries)
416{
417 int i, delta, ret = 0;
418 unsigned long flags;
419 struct dma_debug_entry *entry;
420 LIST_HEAD(tmp);
421
422 spin_lock_irqsave(&free_entries_lock, flags);
423
424 if (nr_total_entries < num_entries) {
425 delta = num_entries - nr_total_entries;
426
427 spin_unlock_irqrestore(&free_entries_lock, flags);
428
429 for (i = 0; i < delta; i++) {
430 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
431 if (!entry)
432 break;
433
434 list_add_tail(&entry->list, &tmp);
435 }
436
437 spin_lock_irqsave(&free_entries_lock, flags);
438
439 list_splice(&tmp, &free_entries);
440 nr_total_entries += i;
441 num_free_entries += i;
442 } else {
443 delta = nr_total_entries - num_entries;
444
445 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
446 entry = __dma_entry_alloc();
447 kfree(entry);
448 }
449
450 nr_total_entries -= i;
451 }
452
453 if (nr_total_entries != num_entries)
454 ret = 1;
455
456 spin_unlock_irqrestore(&free_entries_lock, flags);
457
458 return ret;
459}
460EXPORT_SYMBOL(dma_debug_resize_entries);
461
Joerg Roedel6bf07872009-01-09 12:54:42 +0100462/*
463 * DMA-API debugging init code
464 *
465 * The init code does two things:
466 * 1. Initialize core data structures
467 * 2. Preallocate a given number of dma_debug_entry structs
468 */
469
470static int prealloc_memory(u32 num_entries)
471{
472 struct dma_debug_entry *entry, *next_entry;
473 int i;
474
475 for (i = 0; i < num_entries; ++i) {
476 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
477 if (!entry)
478 goto out_err;
479
480 list_add_tail(&entry->list, &free_entries);
481 }
482
483 num_free_entries = num_entries;
484 min_free_entries = num_entries;
485
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200486 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100487
488 return 0;
489
490out_err:
491
492 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
493 list_del(&entry->list);
494 kfree(entry);
495 }
496
497 return -ENOMEM;
498}
499
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200500static ssize_t filter_read(struct file *file, char __user *user_buf,
501 size_t count, loff_t *ppos)
502{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200503 char buf[NAME_MAX_LEN + 1];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200504 unsigned long flags;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200505 int len;
506
507 if (!current_driver_name[0])
508 return 0;
509
510 /*
511 * We can't copy to userspace directly because current_driver_name can
512 * only be read under the driver_name_lock with irqs disabled. So
513 * create a temporary copy first.
514 */
515 read_lock_irqsave(&driver_name_lock, flags);
516 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
517 read_unlock_irqrestore(&driver_name_lock, flags);
518
519 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
520}
521
522static ssize_t filter_write(struct file *file, const char __user *userbuf,
523 size_t count, loff_t *ppos)
524{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200525 char buf[NAME_MAX_LEN];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200526 unsigned long flags;
527 size_t len;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200528 int i;
529
530 /*
531 * We can't copy from userspace directly. Access to
532 * current_driver_name is protected with a write_lock with irqs
533 * disabled. Since copy_from_user can fault and may sleep we
534 * need to copy to temporary buffer first
535 */
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200536 len = min(count, (size_t)(NAME_MAX_LEN - 1));
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200537 if (copy_from_user(buf, userbuf, len))
538 return -EFAULT;
539
540 buf[len] = 0;
541
542 write_lock_irqsave(&driver_name_lock, flags);
543
Joerg Roedel312325092009-06-08 15:07:08 +0200544 /*
545 * Now handle the string we got from userspace very carefully.
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200546 * The rules are:
547 * - only use the first token we got
548 * - token delimiter is everything looking like a space
549 * character (' ', '\n', '\t' ...)
550 *
551 */
552 if (!isalnum(buf[0])) {
553 /*
Joerg Roedel312325092009-06-08 15:07:08 +0200554 * If the first character userspace gave us is not
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200555 * alphanumerical then assume the filter should be
556 * switched off.
557 */
558 if (current_driver_name[0])
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200559 pr_info("DMA-API: switching off dma-debug driver filter\n");
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200560 current_driver_name[0] = 0;
561 current_driver = NULL;
562 goto out_unlock;
563 }
564
565 /*
566 * Now parse out the first token and use it as the name for the
567 * driver to filter for.
568 */
569 for (i = 0; i < NAME_MAX_LEN; ++i) {
570 current_driver_name[i] = buf[i];
571 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
572 break;
573 }
574 current_driver_name[i] = 0;
575 current_driver = NULL;
576
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200577 pr_info("DMA-API: enable driver filter for driver [%s]\n",
578 current_driver_name);
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200579
580out_unlock:
581 write_unlock_irqrestore(&driver_name_lock, flags);
582
583 return count;
584}
585
586const struct file_operations filter_fops = {
587 .read = filter_read,
588 .write = filter_write,
589};
590
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100591static int dma_debug_fs_init(void)
592{
593 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
594 if (!dma_debug_dent) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200595 pr_err("DMA-API: can not create debugfs directory\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100596 return -ENOMEM;
597 }
598
599 global_disable_dent = debugfs_create_bool("disabled", 0444,
600 dma_debug_dent,
601 (u32 *)&global_disable);
602 if (!global_disable_dent)
603 goto out_err;
604
605 error_count_dent = debugfs_create_u32("error_count", 0444,
606 dma_debug_dent, &error_count);
607 if (!error_count_dent)
608 goto out_err;
609
610 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
611 dma_debug_dent,
612 &show_all_errors);
613 if (!show_all_errors_dent)
614 goto out_err;
615
616 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
617 dma_debug_dent,
618 &show_num_errors);
619 if (!show_num_errors_dent)
620 goto out_err;
621
622 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
623 dma_debug_dent,
624 &num_free_entries);
625 if (!num_free_entries_dent)
626 goto out_err;
627
628 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
629 dma_debug_dent,
630 &min_free_entries);
631 if (!min_free_entries_dent)
632 goto out_err;
633
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200634 filter_dent = debugfs_create_file("driver_filter", 0644,
635 dma_debug_dent, NULL, &filter_fops);
636 if (!filter_dent)
637 goto out_err;
638
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100639 return 0;
640
641out_err:
642 debugfs_remove_recursive(dma_debug_dent);
643
644 return -ENOMEM;
645}
646
Joerg Roedeled888ae2009-05-22 17:16:04 +0200647static int device_dma_allocations(struct device *dev)
648{
649 struct dma_debug_entry *entry;
650 unsigned long flags;
651 int count = 0, i;
652
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200653 local_irq_save(flags);
654
Joerg Roedeled888ae2009-05-22 17:16:04 +0200655 for (i = 0; i < HASH_SIZE; ++i) {
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200656 spin_lock(&dma_entry_hash[i].lock);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200657 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
658 if (entry->dev == dev)
659 count += 1;
660 }
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200661 spin_unlock(&dma_entry_hash[i].lock);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200662 }
663
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200664 local_irq_restore(flags);
665
Joerg Roedeled888ae2009-05-22 17:16:04 +0200666 return count;
667}
668
669static int dma_debug_device_change(struct notifier_block *nb,
670 unsigned long action, void *data)
671{
672 struct device *dev = data;
673 int count;
674
675
676 switch (action) {
677 case BUS_NOTIFY_UNBOUND_DRIVER:
678 count = device_dma_allocations(dev);
679 if (count == 0)
680 break;
681 err_printk(dev, NULL, "DMA-API: device driver has pending "
682 "DMA allocations while released from device "
683 "[count=%d]\n", count);
684 break;
685 default:
686 break;
687 }
688
689 return 0;
690}
691
Joerg Roedel41531c82009-03-16 17:32:14 +0100692void dma_debug_add_bus(struct bus_type *bus)
693{
Joerg Roedeled888ae2009-05-22 17:16:04 +0200694 struct notifier_block *nb;
695
696 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
697 if (nb == NULL) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200698 pr_err("dma_debug_add_bus: out of memory\n");
Joerg Roedeled888ae2009-05-22 17:16:04 +0200699 return;
700 }
701
702 nb->notifier_call = dma_debug_device_change;
703
704 bus_register_notifier(bus, nb);
Joerg Roedel41531c82009-03-16 17:32:14 +0100705}
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100706
Joerg Roedel6bf07872009-01-09 12:54:42 +0100707/*
708 * Let the architectures decide how many entries should be preallocated.
709 */
710void dma_debug_init(u32 num_entries)
711{
712 int i;
713
714 if (global_disable)
715 return;
716
717 for (i = 0; i < HASH_SIZE; ++i) {
718 INIT_LIST_HEAD(&dma_entry_hash[i].list);
Ingo Molnarb0a5b832009-06-16 16:11:14 +0200719 spin_lock_init(&dma_entry_hash[i].lock);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100720 }
721
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100722 if (dma_debug_fs_init() != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200723 pr_err("DMA-API: error creating debugfs entries - disabling\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100724 global_disable = true;
725
726 return;
727 }
728
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100729 if (req_entries)
730 num_entries = req_entries;
731
Joerg Roedel6bf07872009-01-09 12:54:42 +0100732 if (prealloc_memory(num_entries) != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200733 pr_err("DMA-API: debugging out of memory error - disabled\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +0100734 global_disable = true;
735
736 return;
737 }
738
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900739 nr_total_entries = num_free_entries;
740
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200741 pr_info("DMA-API: debugging enabled by kernel config\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +0100742}
743
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100744static __init int dma_debug_cmdline(char *str)
745{
746 if (!str)
747 return -EINVAL;
748
749 if (strncmp(str, "off", 3) == 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200750 pr_info("DMA-API: debugging disabled on kernel command line\n");
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100751 global_disable = true;
752 }
753
754 return 0;
755}
756
757static __init int dma_debug_entries_cmdline(char *str)
758{
759 int res;
760
761 if (!str)
762 return -EINVAL;
763
764 res = get_option(&str, &req_entries);
765
766 if (!res)
767 req_entries = 0;
768
769 return 0;
770}
771
772__setup("dma_debug=", dma_debug_cmdline);
773__setup("dma_debug_entries=", dma_debug_entries_cmdline);
774
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100775static void check_unmap(struct dma_debug_entry *ref)
776{
777 struct dma_debug_entry *entry;
778 struct hash_bucket *bucket;
779 unsigned long flags;
780
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900781 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
782 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
783 "to free an invalid DMA memory address\n");
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100784 return;
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900785 }
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100786
787 bucket = get_hash_bucket(ref, &flags);
788 entry = hash_bucket_find(bucket, ref);
789
790 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100791 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100792 "to free DMA memory it has not allocated "
793 "[device address=0x%016llx] [size=%llu bytes]\n",
794 ref->dev_addr, ref->size);
795 goto out;
796 }
797
798 if (ref->size != entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100799 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100800 "DMA memory with different size "
801 "[device address=0x%016llx] [map size=%llu bytes] "
802 "[unmap size=%llu bytes]\n",
803 ref->dev_addr, entry->size, ref->size);
804 }
805
806 if (ref->type != entry->type) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100807 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100808 "DMA memory with wrong function "
809 "[device address=0x%016llx] [size=%llu bytes] "
810 "[mapped as %s] [unmapped as %s]\n",
811 ref->dev_addr, ref->size,
812 type2name[entry->type], type2name[ref->type]);
813 } else if ((entry->type == dma_debug_coherent) &&
814 (ref->paddr != entry->paddr)) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100815 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100816 "DMA memory with different CPU address "
817 "[device address=0x%016llx] [size=%llu bytes] "
818 "[cpu alloc address=%p] [cpu free address=%p]",
819 ref->dev_addr, ref->size,
820 (void *)entry->paddr, (void *)ref->paddr);
821 }
822
823 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
824 ref->sg_call_ents != entry->sg_call_ents) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100825 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100826 "DMA sg list with different entry count "
827 "[map count=%d] [unmap count=%d]\n",
828 entry->sg_call_ents, ref->sg_call_ents);
829 }
830
831 /*
832 * This may be no bug in reality - but most implementations of the
833 * DMA API don't handle this properly, so check for it here
834 */
835 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100836 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100837 "DMA memory with different direction "
838 "[device address=0x%016llx] [size=%llu bytes] "
839 "[mapped with %s] [unmapped with %s]\n",
840 ref->dev_addr, ref->size,
841 dir2name[entry->direction],
842 dir2name[ref->direction]);
843 }
844
845 hash_bucket_del(entry);
846 dma_entry_free(entry);
847
848out:
849 put_hash_bucket(bucket, &flags);
850}
851
852static void check_for_stack(struct device *dev, void *addr)
853{
854 if (object_is_on_stack(addr))
David Woodhouse6c132d12009-01-19 16:52:39 +0100855 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
856 "stack [addr=%p]\n", addr);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100857}
858
Ingo Molnarf39d1b92009-07-10 21:38:02 +0200859static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100860{
Ingo Molnarf39d1b92009-07-10 21:38:02 +0200861 unsigned long a1 = (unsigned long)addr;
862 unsigned long b1 = a1 + len;
863 unsigned long a2 = (unsigned long)start;
864 unsigned long b2 = (unsigned long)end;
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100865
Ingo Molnarf39d1b92009-07-10 21:38:02 +0200866 return !(b1 <= a2 || a1 >= b2);
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100867}
868
Ingo Molnarf39d1b92009-07-10 21:38:02 +0200869static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100870{
Ingo Molnarf39d1b92009-07-10 21:38:02 +0200871 if (overlap(addr, len, _text, _etext) ||
872 overlap(addr, len, __start_rodata, __end_rodata))
873 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100874}
875
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200876static void check_sync(struct device *dev,
877 struct dma_debug_entry *ref,
878 bool to_cpu)
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100879{
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100880 struct dma_debug_entry *entry;
881 struct hash_bucket *bucket;
882 unsigned long flags;
883
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200884 bucket = get_hash_bucket(ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100885
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200886 entry = hash_bucket_find(bucket, ref);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100887
888 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100889 err_printk(dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100890 "to sync DMA memory it has not allocated "
891 "[device address=0x%016llx] [size=%llu bytes]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200892 (unsigned long long)ref->dev_addr, ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100893 goto out;
894 }
895
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200896 if (ref->size > entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100897 err_printk(dev, entry, "DMA-API: device driver syncs"
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100898 " DMA memory outside allocated range "
899 "[device address=0x%016llx] "
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200900 "[allocation size=%llu bytes] "
901 "[sync offset+size=%llu]\n",
902 entry->dev_addr, entry->size,
903 ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100904 }
905
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200906 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100907 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100908 "DMA memory with different direction "
909 "[device address=0x%016llx] [size=%llu bytes] "
910 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200911 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100912 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200913 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100914 }
915
916 if (entry->direction == DMA_BIDIRECTIONAL)
917 goto out;
918
919 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200920 !(ref->direction == DMA_TO_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100921 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100922 "device read-only DMA memory for cpu "
923 "[device address=0x%016llx] [size=%llu bytes] "
924 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200925 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100926 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200927 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100928
929 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200930 !(ref->direction == DMA_FROM_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100931 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100932 "device write-only DMA memory to device "
933 "[device address=0x%016llx] [size=%llu bytes] "
934 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200935 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100936 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +0200937 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100938
939out:
940 put_hash_bucket(bucket, &flags);
941
942}
943
Joerg Roedelf62bc982009-01-09 14:14:49 +0100944void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
945 size_t size, int direction, dma_addr_t dma_addr,
946 bool map_single)
947{
948 struct dma_debug_entry *entry;
949
950 if (unlikely(global_disable))
951 return;
952
953 if (unlikely(dma_mapping_error(dev, dma_addr)))
954 return;
955
956 entry = dma_entry_alloc();
957 if (!entry)
958 return;
959
960 entry->dev = dev;
961 entry->type = dma_debug_page;
962 entry->paddr = page_to_phys(page) + offset;
963 entry->dev_addr = dma_addr;
964 entry->size = size;
965 entry->direction = direction;
966
Joerg Roedel9537a482009-03-23 15:35:08 +0100967 if (map_single)
Joerg Roedelf62bc982009-01-09 14:14:49 +0100968 entry->type = dma_debug_single;
Joerg Roedel9537a482009-03-23 15:35:08 +0100969
970 if (!PageHighMem(page)) {
Ingo Molnarf39d1b92009-07-10 21:38:02 +0200971 void *addr = page_address(page) + offset;
972
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100973 check_for_stack(dev, addr);
974 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +0100975 }
976
977 add_dma_entry(entry);
978}
979EXPORT_SYMBOL(debug_dma_map_page);
980
981void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
982 size_t size, int direction, bool map_single)
983{
984 struct dma_debug_entry ref = {
985 .type = dma_debug_page,
986 .dev = dev,
987 .dev_addr = addr,
988 .size = size,
989 .direction = direction,
990 };
991
992 if (unlikely(global_disable))
993 return;
994
995 if (map_single)
996 ref.type = dma_debug_single;
997
998 check_unmap(&ref);
999}
1000EXPORT_SYMBOL(debug_dma_unmap_page);
1001
Joerg Roedel972aa452009-01-09 14:19:54 +01001002void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1003 int nents, int mapped_ents, int direction)
1004{
1005 struct dma_debug_entry *entry;
1006 struct scatterlist *s;
1007 int i;
1008
1009 if (unlikely(global_disable))
1010 return;
1011
1012 for_each_sg(sg, s, mapped_ents, i) {
1013 entry = dma_entry_alloc();
1014 if (!entry)
1015 return;
1016
1017 entry->type = dma_debug_sg;
1018 entry->dev = dev;
1019 entry->paddr = sg_phys(s);
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001020 entry->size = sg_dma_len(s);
FUJITA Tomonori15aedea42009-05-27 09:43:01 +09001021 entry->dev_addr = sg_dma_address(s);
Joerg Roedel972aa452009-01-09 14:19:54 +01001022 entry->direction = direction;
1023 entry->sg_call_ents = nents;
1024 entry->sg_mapped_ents = mapped_ents;
1025
Joerg Roedel9537a482009-03-23 15:35:08 +01001026 if (!PageHighMem(sg_page(s))) {
1027 check_for_stack(dev, sg_virt(s));
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001028 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
Joerg Roedel9537a482009-03-23 15:35:08 +01001029 }
Joerg Roedel972aa452009-01-09 14:19:54 +01001030
1031 add_dma_entry(entry);
1032 }
1033}
1034EXPORT_SYMBOL(debug_dma_map_sg);
1035
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001036static int get_nr_mapped_entries(struct device *dev,
1037 struct dma_debug_entry *ref)
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001038{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001039 struct dma_debug_entry *entry;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001040 struct hash_bucket *bucket;
1041 unsigned long flags;
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001042 int mapped_ents;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001043
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001044 bucket = get_hash_bucket(ref, &flags);
1045 entry = hash_bucket_find(bucket, ref);
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001046 mapped_ents = 0;
1047
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001048 if (entry)
1049 mapped_ents = entry->sg_mapped_ents;
1050 put_hash_bucket(bucket, &flags);
1051
1052 return mapped_ents;
1053}
1054
Joerg Roedel972aa452009-01-09 14:19:54 +01001055void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1056 int nelems, int dir)
1057{
Joerg Roedel972aa452009-01-09 14:19:54 +01001058 struct scatterlist *s;
1059 int mapped_ents = 0, i;
Joerg Roedel972aa452009-01-09 14:19:54 +01001060
1061 if (unlikely(global_disable))
1062 return;
1063
1064 for_each_sg(sglist, s, nelems, i) {
1065
1066 struct dma_debug_entry ref = {
1067 .type = dma_debug_sg,
1068 .dev = dev,
1069 .paddr = sg_phys(s),
FUJITA Tomonori15aedea42009-05-27 09:43:01 +09001070 .dev_addr = sg_dma_address(s),
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001071 .size = sg_dma_len(s),
Joerg Roedel972aa452009-01-09 14:19:54 +01001072 .direction = dir,
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001073 .sg_call_ents = nelems,
Joerg Roedel972aa452009-01-09 14:19:54 +01001074 };
1075
1076 if (mapped_ents && i >= mapped_ents)
1077 break;
1078
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001079 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001080 mapped_ents = get_nr_mapped_entries(dev, &ref);
Joerg Roedel972aa452009-01-09 14:19:54 +01001081
1082 check_unmap(&ref);
1083 }
1084}
1085EXPORT_SYMBOL(debug_dma_unmap_sg);
1086
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001087void debug_dma_alloc_coherent(struct device *dev, size_t size,
1088 dma_addr_t dma_addr, void *virt)
1089{
1090 struct dma_debug_entry *entry;
1091
1092 if (unlikely(global_disable))
1093 return;
1094
1095 if (unlikely(virt == NULL))
1096 return;
1097
1098 entry = dma_entry_alloc();
1099 if (!entry)
1100 return;
1101
1102 entry->type = dma_debug_coherent;
1103 entry->dev = dev;
1104 entry->paddr = virt_to_phys(virt);
1105 entry->size = size;
1106 entry->dev_addr = dma_addr;
1107 entry->direction = DMA_BIDIRECTIONAL;
1108
1109 add_dma_entry(entry);
1110}
1111EXPORT_SYMBOL(debug_dma_alloc_coherent);
1112
1113void debug_dma_free_coherent(struct device *dev, size_t size,
1114 void *virt, dma_addr_t addr)
1115{
1116 struct dma_debug_entry ref = {
1117 .type = dma_debug_coherent,
1118 .dev = dev,
1119 .paddr = virt_to_phys(virt),
1120 .dev_addr = addr,
1121 .size = size,
1122 .direction = DMA_BIDIRECTIONAL,
1123 };
1124
1125 if (unlikely(global_disable))
1126 return;
1127
1128 check_unmap(&ref);
1129}
1130EXPORT_SYMBOL(debug_dma_free_coherent);
1131
Joerg Roedelb9d23172009-01-09 14:43:04 +01001132void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1133 size_t size, int direction)
1134{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001135 struct dma_debug_entry ref;
1136
Joerg Roedelb9d23172009-01-09 14:43:04 +01001137 if (unlikely(global_disable))
1138 return;
1139
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001140 ref.type = dma_debug_single;
1141 ref.dev = dev;
1142 ref.dev_addr = dma_handle;
1143 ref.size = size;
1144 ref.direction = direction;
1145 ref.sg_call_ents = 0;
1146
1147 check_sync(dev, &ref, true);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001148}
1149EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1150
1151void debug_dma_sync_single_for_device(struct device *dev,
1152 dma_addr_t dma_handle, size_t size,
1153 int direction)
1154{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001155 struct dma_debug_entry ref;
1156
Joerg Roedelb9d23172009-01-09 14:43:04 +01001157 if (unlikely(global_disable))
1158 return;
1159
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001160 ref.type = dma_debug_single;
1161 ref.dev = dev;
1162 ref.dev_addr = dma_handle;
1163 ref.size = size;
1164 ref.direction = direction;
1165 ref.sg_call_ents = 0;
1166
1167 check_sync(dev, &ref, false);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001168}
1169EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1170
Joerg Roedel948408b2009-01-09 14:55:38 +01001171void debug_dma_sync_single_range_for_cpu(struct device *dev,
1172 dma_addr_t dma_handle,
1173 unsigned long offset, size_t size,
1174 int direction)
1175{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001176 struct dma_debug_entry ref;
1177
Joerg Roedel948408b2009-01-09 14:55:38 +01001178 if (unlikely(global_disable))
1179 return;
1180
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001181 ref.type = dma_debug_single;
1182 ref.dev = dev;
1183 ref.dev_addr = dma_handle;
1184 ref.size = offset + size;
1185 ref.direction = direction;
1186 ref.sg_call_ents = 0;
1187
1188 check_sync(dev, &ref, true);
Joerg Roedel948408b2009-01-09 14:55:38 +01001189}
1190EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1191
1192void debug_dma_sync_single_range_for_device(struct device *dev,
1193 dma_addr_t dma_handle,
1194 unsigned long offset,
1195 size_t size, int direction)
1196{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001197 struct dma_debug_entry ref;
1198
Joerg Roedel948408b2009-01-09 14:55:38 +01001199 if (unlikely(global_disable))
1200 return;
1201
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001202 ref.type = dma_debug_single;
1203 ref.dev = dev;
1204 ref.dev_addr = dma_handle;
1205 ref.size = offset + size;
1206 ref.direction = direction;
1207 ref.sg_call_ents = 0;
1208
1209 check_sync(dev, &ref, false);
Joerg Roedel948408b2009-01-09 14:55:38 +01001210}
1211EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1212
Joerg Roedela31fba52009-01-09 15:01:12 +01001213void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1214 int nelems, int direction)
1215{
1216 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001217 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001218
1219 if (unlikely(global_disable))
1220 return;
1221
1222 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001223
1224 struct dma_debug_entry ref = {
1225 .type = dma_debug_sg,
1226 .dev = dev,
1227 .paddr = sg_phys(s),
1228 .dev_addr = sg_dma_address(s),
1229 .size = sg_dma_len(s),
1230 .direction = direction,
1231 .sg_call_ents = nelems,
1232 };
1233
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001234 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001235 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001236
1237 if (i >= mapped_ents)
1238 break;
1239
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001240 check_sync(dev, &ref, true);
Joerg Roedela31fba52009-01-09 15:01:12 +01001241 }
1242}
1243EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1244
1245void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1246 int nelems, int direction)
1247{
1248 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001249 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001250
1251 if (unlikely(global_disable))
1252 return;
1253
1254 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001255
1256 struct dma_debug_entry ref = {
1257 .type = dma_debug_sg,
1258 .dev = dev,
1259 .paddr = sg_phys(s),
1260 .dev_addr = sg_dma_address(s),
1261 .size = sg_dma_len(s),
1262 .direction = direction,
1263 .sg_call_ents = nelems,
1264 };
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001265 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001266 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001267
1268 if (i >= mapped_ents)
1269 break;
1270
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001271 check_sync(dev, &ref, false);
Joerg Roedela31fba52009-01-09 15:01:12 +01001272 }
1273}
1274EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1275
Joerg Roedel1745de52009-05-22 21:49:51 +02001276static int __init dma_debug_driver_setup(char *str)
1277{
1278 int i;
1279
1280 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1281 current_driver_name[i] = *str;
1282 if (*str == 0)
1283 break;
1284 }
1285
1286 if (current_driver_name[0])
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001287 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1288 current_driver_name);
Joerg Roedel1745de52009-05-22 21:49:51 +02001289
1290
1291 return 1;
1292}
1293__setup("dma_debug_driver=", dma_debug_driver_setup);