mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / lib / dma-debug.c
blob61e7240947f5496c60ae43ec0af38c80a554f2b8
1 /*
2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <joerg.roedel@amd.com>
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.
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.
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
20 #include <linux/sched/task_stack.h>
21 #include <linux/scatterlist.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/sched/task.h>
24 #include <linux/stacktrace.h>
25 #include <linux/dma-debug.h>
26 #include <linux/spinlock.h>
27 #include <linux/vmalloc.h>
28 #include <linux/debugfs.h>
29 #include <linux/uaccess.h>
30 #include <linux/export.h>
31 #include <linux/device.h>
32 #include <linux/types.h>
33 #include <linux/sched.h>
34 #include <linux/ctype.h>
35 #include <linux/list.h>
36 #include <linux/slab.h>
38 #include <asm/sections.h>
40 #define HASH_SIZE 1024ULL
41 #define HASH_FN_SHIFT 13
42 #define HASH_FN_MASK (HASH_SIZE - 1)
44 enum {
45 dma_debug_single,
46 dma_debug_page,
47 dma_debug_sg,
48 dma_debug_coherent,
49 dma_debug_resource,
52 enum map_err_types {
53 MAP_ERR_CHECK_NOT_APPLICABLE,
54 MAP_ERR_NOT_CHECKED,
55 MAP_ERR_CHECKED,
58 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
60 /**
61 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
62 * @list: node on pre-allocated free_entries list
63 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
64 * @type: single, page, sg, coherent
65 * @pfn: page frame of the start address
66 * @offset: offset of mapping relative to pfn
67 * @size: length of the mapping
68 * @direction: enum dma_data_direction
69 * @sg_call_ents: 'nents' from dma_map_sg
70 * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
71 * @map_err_type: track whether dma_mapping_error() was checked
72 * @stacktrace: support backtraces when a violation is detected
74 struct dma_debug_entry {
75 struct list_head list;
76 struct device *dev;
77 int type;
78 unsigned long pfn;
79 size_t offset;
80 u64 dev_addr;
81 u64 size;
82 int direction;
83 int sg_call_ents;
84 int sg_mapped_ents;
85 enum map_err_types map_err_type;
86 #ifdef CONFIG_STACKTRACE
87 struct stack_trace stacktrace;
88 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
89 #endif
92 typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
94 struct hash_bucket {
95 struct list_head list;
96 spinlock_t lock;
97 } ____cacheline_aligned_in_smp;
99 /* Hash list to save the allocated dma addresses */
100 static struct hash_bucket dma_entry_hash[HASH_SIZE];
101 /* List of pre-allocated dma_debug_entry's */
102 static LIST_HEAD(free_entries);
103 /* Lock for the list above */
104 static DEFINE_SPINLOCK(free_entries_lock);
106 /* Global disable flag - will be set in case of an error */
107 static bool global_disable __read_mostly;
109 /* Early initialization disable flag, set at the end of dma_debug_init */
110 static bool dma_debug_initialized __read_mostly;
112 static inline bool dma_debug_disabled(void)
114 return global_disable || !dma_debug_initialized;
117 /* Global error count */
118 static u32 error_count;
120 /* Global error show enable*/
121 static u32 show_all_errors __read_mostly;
122 /* Number of errors to show */
123 static u32 show_num_errors = 1;
125 static u32 num_free_entries;
126 static u32 min_free_entries;
127 static u32 nr_total_entries;
129 /* number of preallocated entries requested by kernel cmdline */
130 static u32 req_entries;
132 /* debugfs dentry's for the stuff above */
133 static struct dentry *dma_debug_dent __read_mostly;
134 static struct dentry *global_disable_dent __read_mostly;
135 static struct dentry *error_count_dent __read_mostly;
136 static struct dentry *show_all_errors_dent __read_mostly;
137 static struct dentry *show_num_errors_dent __read_mostly;
138 static struct dentry *num_free_entries_dent __read_mostly;
139 static struct dentry *min_free_entries_dent __read_mostly;
140 static struct dentry *filter_dent __read_mostly;
142 /* per-driver filter related state */
144 #define NAME_MAX_LEN 64
146 static char current_driver_name[NAME_MAX_LEN] __read_mostly;
147 static struct device_driver *current_driver __read_mostly;
149 static DEFINE_RWLOCK(driver_name_lock);
151 static const char *const maperr2str[] = {
152 [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
153 [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
154 [MAP_ERR_CHECKED] = "dma map error checked",
157 static const char *type2name[5] = { "single", "page",
158 "scather-gather", "coherent",
159 "resource" };
161 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
162 "DMA_FROM_DEVICE", "DMA_NONE" };
165 * The access to some variables in this macro is racy. We can't use atomic_t
166 * here because all these variables are exported to debugfs. Some of them even
167 * writeable. This is also the reason why a lock won't help much. But anyway,
168 * the races are no big deal. Here is why:
170 * error_count: the addition is racy, but the worst thing that can happen is
171 * that we don't count some errors
172 * show_num_errors: the subtraction is racy. Also no big deal because in
173 * worst case this will result in one warning more in the
174 * system log than the user configured. This variable is
175 * writeable via debugfs.
177 static inline void dump_entry_trace(struct dma_debug_entry *entry)
179 #ifdef CONFIG_STACKTRACE
180 if (entry) {
181 pr_warning("Mapped at:\n");
182 print_stack_trace(&entry->stacktrace, 0);
184 #endif
187 static bool driver_filter(struct device *dev)
189 struct device_driver *drv;
190 unsigned long flags;
191 bool ret;
193 /* driver filter off */
194 if (likely(!current_driver_name[0]))
195 return true;
197 /* driver filter on and initialized */
198 if (current_driver && dev && dev->driver == current_driver)
199 return true;
201 /* driver filter on, but we can't filter on a NULL device... */
202 if (!dev)
203 return false;
205 if (current_driver || !current_driver_name[0])
206 return false;
208 /* driver filter on but not yet initialized */
209 drv = dev->driver;
210 if (!drv)
211 return false;
213 /* lock to protect against change of current_driver_name */
214 read_lock_irqsave(&driver_name_lock, flags);
216 ret = false;
217 if (drv->name &&
218 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
219 current_driver = drv;
220 ret = true;
223 read_unlock_irqrestore(&driver_name_lock, flags);
225 return ret;
228 #define err_printk(dev, entry, format, arg...) do { \
229 error_count += 1; \
230 if (driver_filter(dev) && \
231 (show_all_errors || show_num_errors > 0)) { \
232 WARN(1, "%s %s: " format, \
233 dev ? dev_driver_string(dev) : "NULL", \
234 dev ? dev_name(dev) : "NULL", ## arg); \
235 dump_entry_trace(entry); \
237 if (!show_all_errors && show_num_errors > 0) \
238 show_num_errors -= 1; \
239 } while (0);
242 * Hash related functions
244 * Every DMA-API request is saved into a struct dma_debug_entry. To
245 * have quick access to these structs they are stored into a hash.
247 static int hash_fn(struct dma_debug_entry *entry)
250 * Hash function is based on the dma address.
251 * We use bits 20-27 here as the index into the hash
253 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
257 * Request exclusive access to a hash bucket for a given dma_debug_entry.
259 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
260 unsigned long *flags)
261 __acquires(&dma_entry_hash[idx].lock)
263 int idx = hash_fn(entry);
264 unsigned long __flags;
266 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
267 *flags = __flags;
268 return &dma_entry_hash[idx];
272 * Give up exclusive access to the hash bucket
274 static void put_hash_bucket(struct hash_bucket *bucket,
275 unsigned long *flags)
276 __releases(&bucket->lock)
278 unsigned long __flags = *flags;
280 spin_unlock_irqrestore(&bucket->lock, __flags);
283 static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
285 return ((a->dev_addr == b->dev_addr) &&
286 (a->dev == b->dev)) ? true : false;
289 static bool containing_match(struct dma_debug_entry *a,
290 struct dma_debug_entry *b)
292 if (a->dev != b->dev)
293 return false;
295 if ((b->dev_addr <= a->dev_addr) &&
296 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
297 return true;
299 return false;
303 * Search a given entry in the hash bucket list
305 static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
306 struct dma_debug_entry *ref,
307 match_fn match)
309 struct dma_debug_entry *entry, *ret = NULL;
310 int matches = 0, match_lvl, last_lvl = -1;
312 list_for_each_entry(entry, &bucket->list, list) {
313 if (!match(ref, entry))
314 continue;
317 * Some drivers map the same physical address multiple
318 * times. Without a hardware IOMMU this results in the
319 * same device addresses being put into the dma-debug
320 * hash multiple times too. This can result in false
321 * positives being reported. Therefore we implement a
322 * best-fit algorithm here which returns the entry from
323 * the hash which fits best to the reference value
324 * instead of the first-fit.
326 matches += 1;
327 match_lvl = 0;
328 entry->size == ref->size ? ++match_lvl : 0;
329 entry->type == ref->type ? ++match_lvl : 0;
330 entry->direction == ref->direction ? ++match_lvl : 0;
331 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
333 if (match_lvl == 4) {
334 /* perfect-fit - return the result */
335 return entry;
336 } else if (match_lvl > last_lvl) {
338 * We found an entry that fits better then the
339 * previous one or it is the 1st match.
341 last_lvl = match_lvl;
342 ret = entry;
347 * If we have multiple matches but no perfect-fit, just return
348 * NULL.
350 ret = (matches == 1) ? ret : NULL;
352 return ret;
355 static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
356 struct dma_debug_entry *ref)
358 return __hash_bucket_find(bucket, ref, exact_match);
361 static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
362 struct dma_debug_entry *ref,
363 unsigned long *flags)
366 unsigned int max_range = dma_get_max_seg_size(ref->dev);
367 struct dma_debug_entry *entry, index = *ref;
368 unsigned int range = 0;
370 while (range <= max_range) {
371 entry = __hash_bucket_find(*bucket, ref, containing_match);
373 if (entry)
374 return entry;
377 * Nothing found, go back a hash bucket
379 put_hash_bucket(*bucket, flags);
380 range += (1 << HASH_FN_SHIFT);
381 index.dev_addr -= (1 << HASH_FN_SHIFT);
382 *bucket = get_hash_bucket(&index, flags);
385 return NULL;
389 * Add an entry to a hash bucket
391 static void hash_bucket_add(struct hash_bucket *bucket,
392 struct dma_debug_entry *entry)
394 list_add_tail(&entry->list, &bucket->list);
398 * Remove entry from a hash bucket list
400 static void hash_bucket_del(struct dma_debug_entry *entry)
402 list_del(&entry->list);
405 static unsigned long long phys_addr(struct dma_debug_entry *entry)
407 if (entry->type == dma_debug_resource)
408 return __pfn_to_phys(entry->pfn) + entry->offset;
410 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
414 * Dump mapping entries for debugging purposes
416 void debug_dma_dump_mappings(struct device *dev)
418 int idx;
420 for (idx = 0; idx < HASH_SIZE; idx++) {
421 struct hash_bucket *bucket = &dma_entry_hash[idx];
422 struct dma_debug_entry *entry;
423 unsigned long flags;
425 spin_lock_irqsave(&bucket->lock, flags);
427 list_for_each_entry(entry, &bucket->list, list) {
428 if (!dev || dev == entry->dev) {
429 dev_info(entry->dev,
430 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
431 type2name[entry->type], idx,
432 phys_addr(entry), entry->pfn,
433 entry->dev_addr, entry->size,
434 dir2name[entry->direction],
435 maperr2str[entry->map_err_type]);
439 spin_unlock_irqrestore(&bucket->lock, flags);
440 cond_resched();
443 EXPORT_SYMBOL(debug_dma_dump_mappings);
446 * For each mapping (initial cacheline in the case of
447 * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
448 * scatterlist, or the cacheline specified in dma_map_single) insert
449 * into this tree using the cacheline as the key. At
450 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
451 * the entry already exists at insertion time add a tag as a reference
452 * count for the overlapping mappings. For now, the overlap tracking
453 * just ensures that 'unmaps' balance 'maps' before marking the
454 * cacheline idle, but we should also be flagging overlaps as an API
455 * violation.
457 * Memory usage is mostly constrained by the maximum number of available
458 * dma-debug entries in that we need a free dma_debug_entry before
459 * inserting into the tree. In the case of dma_map_page and
460 * dma_alloc_coherent there is only one dma_debug_entry and one
461 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
462 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
463 * entries into the tree.
465 * At any time debug_dma_assert_idle() can be called to trigger a
466 * warning if any cachelines in the given page are in the active set.
468 static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
469 static DEFINE_SPINLOCK(radix_lock);
470 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
471 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
472 #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
474 static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
476 return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
477 (entry->offset >> L1_CACHE_SHIFT);
480 static int active_cacheline_read_overlap(phys_addr_t cln)
482 int overlap = 0, i;
484 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
485 if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
486 overlap |= 1 << i;
487 return overlap;
490 static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
492 int i;
494 if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
495 return overlap;
497 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
498 if (overlap & 1 << i)
499 radix_tree_tag_set(&dma_active_cacheline, cln, i);
500 else
501 radix_tree_tag_clear(&dma_active_cacheline, cln, i);
503 return overlap;
506 static void active_cacheline_inc_overlap(phys_addr_t cln)
508 int overlap = active_cacheline_read_overlap(cln);
510 overlap = active_cacheline_set_overlap(cln, ++overlap);
512 /* If we overflowed the overlap counter then we're potentially
513 * leaking dma-mappings. Otherwise, if maps and unmaps are
514 * balanced then this overflow may cause false negatives in
515 * debug_dma_assert_idle() as the cacheline may be marked idle
516 * prematurely.
518 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
519 "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
520 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
523 static int active_cacheline_dec_overlap(phys_addr_t cln)
525 int overlap = active_cacheline_read_overlap(cln);
527 return active_cacheline_set_overlap(cln, --overlap);
530 static int active_cacheline_insert(struct dma_debug_entry *entry)
532 phys_addr_t cln = to_cacheline_number(entry);
533 unsigned long flags;
534 int rc;
536 /* If the device is not writing memory then we don't have any
537 * concerns about the cpu consuming stale data. This mitigates
538 * legitimate usages of overlapping mappings.
540 if (entry->direction == DMA_TO_DEVICE)
541 return 0;
543 spin_lock_irqsave(&radix_lock, flags);
544 rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
545 if (rc == -EEXIST)
546 active_cacheline_inc_overlap(cln);
547 spin_unlock_irqrestore(&radix_lock, flags);
549 return rc;
552 static void active_cacheline_remove(struct dma_debug_entry *entry)
554 phys_addr_t cln = to_cacheline_number(entry);
555 unsigned long flags;
557 /* ...mirror the insert case */
558 if (entry->direction == DMA_TO_DEVICE)
559 return;
561 spin_lock_irqsave(&radix_lock, flags);
562 /* since we are counting overlaps the final put of the
563 * cacheline will occur when the overlap count is 0.
564 * active_cacheline_dec_overlap() returns -1 in that case
566 if (active_cacheline_dec_overlap(cln) < 0)
567 radix_tree_delete(&dma_active_cacheline, cln);
568 spin_unlock_irqrestore(&radix_lock, flags);
572 * debug_dma_assert_idle() - assert that a page is not undergoing dma
573 * @page: page to lookup in the dma_active_cacheline tree
575 * Place a call to this routine in cases where the cpu touching the page
576 * before the dma completes (page is dma_unmapped) will lead to data
577 * corruption.
579 void debug_dma_assert_idle(struct page *page)
581 static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
582 struct dma_debug_entry *entry = NULL;
583 void **results = (void **) &ents;
584 unsigned int nents, i;
585 unsigned long flags;
586 phys_addr_t cln;
588 if (dma_debug_disabled())
589 return;
591 if (!page)
592 return;
594 cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
595 spin_lock_irqsave(&radix_lock, flags);
596 nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
597 CACHELINES_PER_PAGE);
598 for (i = 0; i < nents; i++) {
599 phys_addr_t ent_cln = to_cacheline_number(ents[i]);
601 if (ent_cln == cln) {
602 entry = ents[i];
603 break;
604 } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
605 break;
607 spin_unlock_irqrestore(&radix_lock, flags);
609 if (!entry)
610 return;
612 cln = to_cacheline_number(entry);
613 err_printk(entry->dev, entry,
614 "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
615 &cln);
619 * Wrapper function for adding an entry to the hash.
620 * This function takes care of locking itself.
622 static void add_dma_entry(struct dma_debug_entry *entry)
624 struct hash_bucket *bucket;
625 unsigned long flags;
626 int rc;
628 bucket = get_hash_bucket(entry, &flags);
629 hash_bucket_add(bucket, entry);
630 put_hash_bucket(bucket, &flags);
632 rc = active_cacheline_insert(entry);
633 if (rc == -ENOMEM) {
634 pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
635 global_disable = true;
638 /* TODO: report -EEXIST errors here as overlapping mappings are
639 * not supported by the DMA API
643 static struct dma_debug_entry *__dma_entry_alloc(void)
645 struct dma_debug_entry *entry;
647 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
648 list_del(&entry->list);
649 memset(entry, 0, sizeof(*entry));
651 num_free_entries -= 1;
652 if (num_free_entries < min_free_entries)
653 min_free_entries = num_free_entries;
655 return entry;
658 /* struct dma_entry allocator
660 * The next two functions implement the allocator for
661 * struct dma_debug_entries.
663 static struct dma_debug_entry *dma_entry_alloc(void)
665 struct dma_debug_entry *entry;
666 unsigned long flags;
668 spin_lock_irqsave(&free_entries_lock, flags);
670 if (list_empty(&free_entries)) {
671 global_disable = true;
672 spin_unlock_irqrestore(&free_entries_lock, flags);
673 pr_err("DMA-API: debugging out of memory - disabling\n");
674 return NULL;
677 entry = __dma_entry_alloc();
679 spin_unlock_irqrestore(&free_entries_lock, flags);
681 #ifdef CONFIG_STACKTRACE
682 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
683 entry->stacktrace.entries = entry->st_entries;
684 entry->stacktrace.skip = 2;
685 save_stack_trace(&entry->stacktrace);
686 #endif
688 return entry;
691 static void dma_entry_free(struct dma_debug_entry *entry)
693 unsigned long flags;
695 active_cacheline_remove(entry);
698 * add to beginning of the list - this way the entries are
699 * more likely cache hot when they are reallocated.
701 spin_lock_irqsave(&free_entries_lock, flags);
702 list_add(&entry->list, &free_entries);
703 num_free_entries += 1;
704 spin_unlock_irqrestore(&free_entries_lock, flags);
707 int dma_debug_resize_entries(u32 num_entries)
709 int i, delta, ret = 0;
710 unsigned long flags;
711 struct dma_debug_entry *entry;
712 LIST_HEAD(tmp);
714 spin_lock_irqsave(&free_entries_lock, flags);
716 if (nr_total_entries < num_entries) {
717 delta = num_entries - nr_total_entries;
719 spin_unlock_irqrestore(&free_entries_lock, flags);
721 for (i = 0; i < delta; i++) {
722 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
723 if (!entry)
724 break;
726 list_add_tail(&entry->list, &tmp);
729 spin_lock_irqsave(&free_entries_lock, flags);
731 list_splice(&tmp, &free_entries);
732 nr_total_entries += i;
733 num_free_entries += i;
734 } else {
735 delta = nr_total_entries - num_entries;
737 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
738 entry = __dma_entry_alloc();
739 kfree(entry);
742 nr_total_entries -= i;
745 if (nr_total_entries != num_entries)
746 ret = 1;
748 spin_unlock_irqrestore(&free_entries_lock, flags);
750 return ret;
752 EXPORT_SYMBOL(dma_debug_resize_entries);
755 * DMA-API debugging init code
757 * The init code does two things:
758 * 1. Initialize core data structures
759 * 2. Preallocate a given number of dma_debug_entry structs
762 static int prealloc_memory(u32 num_entries)
764 struct dma_debug_entry *entry, *next_entry;
765 int i;
767 for (i = 0; i < num_entries; ++i) {
768 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
769 if (!entry)
770 goto out_err;
772 list_add_tail(&entry->list, &free_entries);
775 num_free_entries = num_entries;
776 min_free_entries = num_entries;
778 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
780 return 0;
782 out_err:
784 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
785 list_del(&entry->list);
786 kfree(entry);
789 return -ENOMEM;
792 static ssize_t filter_read(struct file *file, char __user *user_buf,
793 size_t count, loff_t *ppos)
795 char buf[NAME_MAX_LEN + 1];
796 unsigned long flags;
797 int len;
799 if (!current_driver_name[0])
800 return 0;
803 * We can't copy to userspace directly because current_driver_name can
804 * only be read under the driver_name_lock with irqs disabled. So
805 * create a temporary copy first.
807 read_lock_irqsave(&driver_name_lock, flags);
808 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
809 read_unlock_irqrestore(&driver_name_lock, flags);
811 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
814 static ssize_t filter_write(struct file *file, const char __user *userbuf,
815 size_t count, loff_t *ppos)
817 char buf[NAME_MAX_LEN];
818 unsigned long flags;
819 size_t len;
820 int i;
823 * We can't copy from userspace directly. Access to
824 * current_driver_name is protected with a write_lock with irqs
825 * disabled. Since copy_from_user can fault and may sleep we
826 * need to copy to temporary buffer first
828 len = min(count, (size_t)(NAME_MAX_LEN - 1));
829 if (copy_from_user(buf, userbuf, len))
830 return -EFAULT;
832 buf[len] = 0;
834 write_lock_irqsave(&driver_name_lock, flags);
837 * Now handle the string we got from userspace very carefully.
838 * The rules are:
839 * - only use the first token we got
840 * - token delimiter is everything looking like a space
841 * character (' ', '\n', '\t' ...)
844 if (!isalnum(buf[0])) {
846 * If the first character userspace gave us is not
847 * alphanumerical then assume the filter should be
848 * switched off.
850 if (current_driver_name[0])
851 pr_info("DMA-API: switching off dma-debug driver filter\n");
852 current_driver_name[0] = 0;
853 current_driver = NULL;
854 goto out_unlock;
858 * Now parse out the first token and use it as the name for the
859 * driver to filter for.
861 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
862 current_driver_name[i] = buf[i];
863 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
864 break;
866 current_driver_name[i] = 0;
867 current_driver = NULL;
869 pr_info("DMA-API: enable driver filter for driver [%s]\n",
870 current_driver_name);
872 out_unlock:
873 write_unlock_irqrestore(&driver_name_lock, flags);
875 return count;
878 static const struct file_operations filter_fops = {
879 .read = filter_read,
880 .write = filter_write,
881 .llseek = default_llseek,
884 static int dma_debug_fs_init(void)
886 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
887 if (!dma_debug_dent) {
888 pr_err("DMA-API: can not create debugfs directory\n");
889 return -ENOMEM;
892 global_disable_dent = debugfs_create_bool("disabled", 0444,
893 dma_debug_dent,
894 &global_disable);
895 if (!global_disable_dent)
896 goto out_err;
898 error_count_dent = debugfs_create_u32("error_count", 0444,
899 dma_debug_dent, &error_count);
900 if (!error_count_dent)
901 goto out_err;
903 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
904 dma_debug_dent,
905 &show_all_errors);
906 if (!show_all_errors_dent)
907 goto out_err;
909 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
910 dma_debug_dent,
911 &show_num_errors);
912 if (!show_num_errors_dent)
913 goto out_err;
915 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
916 dma_debug_dent,
917 &num_free_entries);
918 if (!num_free_entries_dent)
919 goto out_err;
921 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
922 dma_debug_dent,
923 &min_free_entries);
924 if (!min_free_entries_dent)
925 goto out_err;
927 filter_dent = debugfs_create_file("driver_filter", 0644,
928 dma_debug_dent, NULL, &filter_fops);
929 if (!filter_dent)
930 goto out_err;
932 return 0;
934 out_err:
935 debugfs_remove_recursive(dma_debug_dent);
937 return -ENOMEM;
940 static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
942 struct dma_debug_entry *entry;
943 unsigned long flags;
944 int count = 0, i;
946 for (i = 0; i < HASH_SIZE; ++i) {
947 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
948 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
949 if (entry->dev == dev) {
950 count += 1;
951 *out_entry = entry;
954 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
957 return count;
960 static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
962 struct device *dev = data;
963 struct dma_debug_entry *uninitialized_var(entry);
964 int count;
966 if (dma_debug_disabled())
967 return 0;
969 switch (action) {
970 case BUS_NOTIFY_UNBOUND_DRIVER:
971 count = device_dma_allocations(dev, &entry);
972 if (count == 0)
973 break;
974 err_printk(dev, entry, "DMA-API: device driver has pending "
975 "DMA allocations while released from device "
976 "[count=%d]\n"
977 "One of leaked entries details: "
978 "[device address=0x%016llx] [size=%llu bytes] "
979 "[mapped with %s] [mapped as %s]\n",
980 count, entry->dev_addr, entry->size,
981 dir2name[entry->direction], type2name[entry->type]);
982 break;
983 default:
984 break;
987 return 0;
990 void dma_debug_add_bus(struct bus_type *bus)
992 struct notifier_block *nb;
994 if (dma_debug_disabled())
995 return;
997 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
998 if (nb == NULL) {
999 pr_err("dma_debug_add_bus: out of memory\n");
1000 return;
1003 nb->notifier_call = dma_debug_device_change;
1005 bus_register_notifier(bus, nb);
1009 * Let the architectures decide how many entries should be preallocated.
1011 void dma_debug_init(u32 num_entries)
1013 int i;
1015 /* Do not use dma_debug_initialized here, since we really want to be
1016 * called to set dma_debug_initialized
1018 if (global_disable)
1019 return;
1021 for (i = 0; i < HASH_SIZE; ++i) {
1022 INIT_LIST_HEAD(&dma_entry_hash[i].list);
1023 spin_lock_init(&dma_entry_hash[i].lock);
1026 if (dma_debug_fs_init() != 0) {
1027 pr_err("DMA-API: error creating debugfs entries - disabling\n");
1028 global_disable = true;
1030 return;
1033 if (req_entries)
1034 num_entries = req_entries;
1036 if (prealloc_memory(num_entries) != 0) {
1037 pr_err("DMA-API: debugging out of memory error - disabled\n");
1038 global_disable = true;
1040 return;
1043 nr_total_entries = num_free_entries;
1045 dma_debug_initialized = true;
1047 pr_info("DMA-API: debugging enabled by kernel config\n");
1050 static __init int dma_debug_cmdline(char *str)
1052 if (!str)
1053 return -EINVAL;
1055 if (strncmp(str, "off", 3) == 0) {
1056 pr_info("DMA-API: debugging disabled on kernel command line\n");
1057 global_disable = true;
1060 return 0;
1063 static __init int dma_debug_entries_cmdline(char *str)
1065 int res;
1067 if (!str)
1068 return -EINVAL;
1070 res = get_option(&str, &req_entries);
1072 if (!res)
1073 req_entries = 0;
1075 return 0;
1078 __setup("dma_debug=", dma_debug_cmdline);
1079 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
1081 static void check_unmap(struct dma_debug_entry *ref)
1083 struct dma_debug_entry *entry;
1084 struct hash_bucket *bucket;
1085 unsigned long flags;
1087 bucket = get_hash_bucket(ref, &flags);
1088 entry = bucket_find_exact(bucket, ref);
1090 if (!entry) {
1091 /* must drop lock before calling dma_mapping_error */
1092 put_hash_bucket(bucket, &flags);
1094 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1095 err_printk(ref->dev, NULL,
1096 "DMA-API: device driver tries to free an "
1097 "invalid DMA memory address\n");
1098 } else {
1099 err_printk(ref->dev, NULL,
1100 "DMA-API: device driver tries to free DMA "
1101 "memory it has not allocated [device "
1102 "address=0x%016llx] [size=%llu bytes]\n",
1103 ref->dev_addr, ref->size);
1105 return;
1108 if (ref->size != entry->size) {
1109 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1110 "DMA memory with different size "
1111 "[device address=0x%016llx] [map size=%llu bytes] "
1112 "[unmap size=%llu bytes]\n",
1113 ref->dev_addr, entry->size, ref->size);
1116 if (ref->type != entry->type) {
1117 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1118 "DMA memory with wrong function "
1119 "[device address=0x%016llx] [size=%llu bytes] "
1120 "[mapped as %s] [unmapped as %s]\n",
1121 ref->dev_addr, ref->size,
1122 type2name[entry->type], type2name[ref->type]);
1123 } else if ((entry->type == dma_debug_coherent) &&
1124 (phys_addr(ref) != phys_addr(entry))) {
1125 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1126 "DMA memory with different CPU address "
1127 "[device address=0x%016llx] [size=%llu bytes] "
1128 "[cpu alloc address=0x%016llx] "
1129 "[cpu free address=0x%016llx]",
1130 ref->dev_addr, ref->size,
1131 phys_addr(entry),
1132 phys_addr(ref));
1135 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1136 ref->sg_call_ents != entry->sg_call_ents) {
1137 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1138 "DMA sg list with different entry count "
1139 "[map count=%d] [unmap count=%d]\n",
1140 entry->sg_call_ents, ref->sg_call_ents);
1144 * This may be no bug in reality - but most implementations of the
1145 * DMA API don't handle this properly, so check for it here
1147 if (ref->direction != entry->direction) {
1148 err_printk(ref->dev, entry, "DMA-API: device driver frees "
1149 "DMA memory with different direction "
1150 "[device address=0x%016llx] [size=%llu bytes] "
1151 "[mapped with %s] [unmapped with %s]\n",
1152 ref->dev_addr, ref->size,
1153 dir2name[entry->direction],
1154 dir2name[ref->direction]);
1158 * Drivers should use dma_mapping_error() to check the returned
1159 * addresses of dma_map_single() and dma_map_page().
1160 * If not, print this warning message. See Documentation/DMA-API.txt.
1162 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1163 err_printk(ref->dev, entry,
1164 "DMA-API: device driver failed to check map error"
1165 "[device address=0x%016llx] [size=%llu bytes] "
1166 "[mapped as %s]",
1167 ref->dev_addr, ref->size,
1168 type2name[entry->type]);
1171 hash_bucket_del(entry);
1172 dma_entry_free(entry);
1174 put_hash_bucket(bucket, &flags);
1177 static void check_for_stack(struct device *dev,
1178 struct page *page, size_t offset)
1180 void *addr;
1181 struct vm_struct *stack_vm_area = task_stack_vm_area(current);
1183 if (!stack_vm_area) {
1184 /* Stack is direct-mapped. */
1185 if (PageHighMem(page))
1186 return;
1187 addr = page_address(page) + offset;
1188 if (object_is_on_stack(addr))
1189 err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [addr=%p]\n", addr);
1190 } else {
1191 /* Stack is vmalloced. */
1192 int i;
1194 for (i = 0; i < stack_vm_area->nr_pages; i++) {
1195 if (page != stack_vm_area->pages[i])
1196 continue;
1198 addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
1199 err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [probable addr=%p]\n", addr);
1200 break;
1205 static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
1207 unsigned long a1 = (unsigned long)addr;
1208 unsigned long b1 = a1 + len;
1209 unsigned long a2 = (unsigned long)start;
1210 unsigned long b2 = (unsigned long)end;
1212 return !(b1 <= a2 || a1 >= b2);
1215 static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
1217 if (overlap(addr, len, _stext, _etext) ||
1218 overlap(addr, len, __start_rodata, __end_rodata))
1219 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1222 static void check_sync(struct device *dev,
1223 struct dma_debug_entry *ref,
1224 bool to_cpu)
1226 struct dma_debug_entry *entry;
1227 struct hash_bucket *bucket;
1228 unsigned long flags;
1230 bucket = get_hash_bucket(ref, &flags);
1232 entry = bucket_find_contain(&bucket, ref, &flags);
1234 if (!entry) {
1235 err_printk(dev, NULL, "DMA-API: device driver tries "
1236 "to sync DMA memory it has not allocated "
1237 "[device address=0x%016llx] [size=%llu bytes]\n",
1238 (unsigned long long)ref->dev_addr, ref->size);
1239 goto out;
1242 if (ref->size > entry->size) {
1243 err_printk(dev, entry, "DMA-API: device driver syncs"
1244 " DMA memory outside allocated range "
1245 "[device address=0x%016llx] "
1246 "[allocation size=%llu bytes] "
1247 "[sync offset+size=%llu]\n",
1248 entry->dev_addr, entry->size,
1249 ref->size);
1252 if (entry->direction == DMA_BIDIRECTIONAL)
1253 goto out;
1255 if (ref->direction != entry->direction) {
1256 err_printk(dev, entry, "DMA-API: device driver syncs "
1257 "DMA memory with different direction "
1258 "[device address=0x%016llx] [size=%llu bytes] "
1259 "[mapped with %s] [synced with %s]\n",
1260 (unsigned long long)ref->dev_addr, entry->size,
1261 dir2name[entry->direction],
1262 dir2name[ref->direction]);
1265 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
1266 !(ref->direction == DMA_TO_DEVICE))
1267 err_printk(dev, entry, "DMA-API: device driver syncs "
1268 "device read-only DMA memory for cpu "
1269 "[device address=0x%016llx] [size=%llu bytes] "
1270 "[mapped with %s] [synced with %s]\n",
1271 (unsigned long long)ref->dev_addr, entry->size,
1272 dir2name[entry->direction],
1273 dir2name[ref->direction]);
1275 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
1276 !(ref->direction == DMA_FROM_DEVICE))
1277 err_printk(dev, entry, "DMA-API: device driver syncs "
1278 "device write-only DMA memory to device "
1279 "[device address=0x%016llx] [size=%llu bytes] "
1280 "[mapped with %s] [synced with %s]\n",
1281 (unsigned long long)ref->dev_addr, entry->size,
1282 dir2name[entry->direction],
1283 dir2name[ref->direction]);
1285 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1286 ref->sg_call_ents != entry->sg_call_ents) {
1287 err_printk(ref->dev, entry, "DMA-API: device driver syncs "
1288 "DMA sg list with different entry count "
1289 "[map count=%d] [sync count=%d]\n",
1290 entry->sg_call_ents, ref->sg_call_ents);
1293 out:
1294 put_hash_bucket(bucket, &flags);
1297 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1298 size_t size, int direction, dma_addr_t dma_addr,
1299 bool map_single)
1301 struct dma_debug_entry *entry;
1303 if (unlikely(dma_debug_disabled()))
1304 return;
1306 if (dma_mapping_error(dev, dma_addr))
1307 return;
1309 entry = dma_entry_alloc();
1310 if (!entry)
1311 return;
1313 entry->dev = dev;
1314 entry->type = dma_debug_page;
1315 entry->pfn = page_to_pfn(page);
1316 entry->offset = offset,
1317 entry->dev_addr = dma_addr;
1318 entry->size = size;
1319 entry->direction = direction;
1320 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1322 if (map_single)
1323 entry->type = dma_debug_single;
1325 check_for_stack(dev, page, offset);
1327 if (!PageHighMem(page)) {
1328 void *addr = page_address(page) + offset;
1330 check_for_illegal_area(dev, addr, size);
1333 add_dma_entry(entry);
1335 EXPORT_SYMBOL(debug_dma_map_page);
1337 void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1339 struct dma_debug_entry ref;
1340 struct dma_debug_entry *entry;
1341 struct hash_bucket *bucket;
1342 unsigned long flags;
1344 if (unlikely(dma_debug_disabled()))
1345 return;
1347 ref.dev = dev;
1348 ref.dev_addr = dma_addr;
1349 bucket = get_hash_bucket(&ref, &flags);
1351 list_for_each_entry(entry, &bucket->list, list) {
1352 if (!exact_match(&ref, entry))
1353 continue;
1356 * The same physical address can be mapped multiple
1357 * times. Without a hardware IOMMU this results in the
1358 * same device addresses being put into the dma-debug
1359 * hash multiple times too. This can result in false
1360 * positives being reported. Therefore we implement a
1361 * best-fit algorithm here which updates the first entry
1362 * from the hash which fits the reference value and is
1363 * not currently listed as being checked.
1365 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1366 entry->map_err_type = MAP_ERR_CHECKED;
1367 break;
1371 put_hash_bucket(bucket, &flags);
1373 EXPORT_SYMBOL(debug_dma_mapping_error);
1375 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1376 size_t size, int direction, bool map_single)
1378 struct dma_debug_entry ref = {
1379 .type = dma_debug_page,
1380 .dev = dev,
1381 .dev_addr = addr,
1382 .size = size,
1383 .direction = direction,
1386 if (unlikely(dma_debug_disabled()))
1387 return;
1389 if (map_single)
1390 ref.type = dma_debug_single;
1392 check_unmap(&ref);
1394 EXPORT_SYMBOL(debug_dma_unmap_page);
1396 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1397 int nents, int mapped_ents, int direction)
1399 struct dma_debug_entry *entry;
1400 struct scatterlist *s;
1401 int i;
1403 if (unlikely(dma_debug_disabled()))
1404 return;
1406 for_each_sg(sg, s, mapped_ents, i) {
1407 entry = dma_entry_alloc();
1408 if (!entry)
1409 return;
1411 entry->type = dma_debug_sg;
1412 entry->dev = dev;
1413 entry->pfn = page_to_pfn(sg_page(s));
1414 entry->offset = s->offset,
1415 entry->size = sg_dma_len(s);
1416 entry->dev_addr = sg_dma_address(s);
1417 entry->direction = direction;
1418 entry->sg_call_ents = nents;
1419 entry->sg_mapped_ents = mapped_ents;
1421 check_for_stack(dev, sg_page(s), s->offset);
1423 if (!PageHighMem(sg_page(s))) {
1424 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
1427 add_dma_entry(entry);
1430 EXPORT_SYMBOL(debug_dma_map_sg);
1432 static int get_nr_mapped_entries(struct device *dev,
1433 struct dma_debug_entry *ref)
1435 struct dma_debug_entry *entry;
1436 struct hash_bucket *bucket;
1437 unsigned long flags;
1438 int mapped_ents;
1440 bucket = get_hash_bucket(ref, &flags);
1441 entry = bucket_find_exact(bucket, ref);
1442 mapped_ents = 0;
1444 if (entry)
1445 mapped_ents = entry->sg_mapped_ents;
1446 put_hash_bucket(bucket, &flags);
1448 return mapped_ents;
1451 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1452 int nelems, int dir)
1454 struct scatterlist *s;
1455 int mapped_ents = 0, i;
1457 if (unlikely(dma_debug_disabled()))
1458 return;
1460 for_each_sg(sglist, s, nelems, i) {
1462 struct dma_debug_entry ref = {
1463 .type = dma_debug_sg,
1464 .dev = dev,
1465 .pfn = page_to_pfn(sg_page(s)),
1466 .offset = s->offset,
1467 .dev_addr = sg_dma_address(s),
1468 .size = sg_dma_len(s),
1469 .direction = dir,
1470 .sg_call_ents = nelems,
1473 if (mapped_ents && i >= mapped_ents)
1474 break;
1476 if (!i)
1477 mapped_ents = get_nr_mapped_entries(dev, &ref);
1479 check_unmap(&ref);
1482 EXPORT_SYMBOL(debug_dma_unmap_sg);
1484 void debug_dma_alloc_coherent(struct device *dev, size_t size,
1485 dma_addr_t dma_addr, void *virt)
1487 struct dma_debug_entry *entry;
1489 if (unlikely(dma_debug_disabled()))
1490 return;
1492 if (unlikely(virt == NULL))
1493 return;
1495 entry = dma_entry_alloc();
1496 if (!entry)
1497 return;
1499 entry->type = dma_debug_coherent;
1500 entry->dev = dev;
1501 entry->pfn = page_to_pfn(virt_to_page(virt));
1502 entry->offset = offset_in_page(virt);
1503 entry->size = size;
1504 entry->dev_addr = dma_addr;
1505 entry->direction = DMA_BIDIRECTIONAL;
1507 add_dma_entry(entry);
1509 EXPORT_SYMBOL(debug_dma_alloc_coherent);
1511 void debug_dma_free_coherent(struct device *dev, size_t size,
1512 void *virt, dma_addr_t addr)
1514 struct dma_debug_entry ref = {
1515 .type = dma_debug_coherent,
1516 .dev = dev,
1517 .pfn = page_to_pfn(virt_to_page(virt)),
1518 .offset = offset_in_page(virt),
1519 .dev_addr = addr,
1520 .size = size,
1521 .direction = DMA_BIDIRECTIONAL,
1524 if (unlikely(dma_debug_disabled()))
1525 return;
1527 check_unmap(&ref);
1529 EXPORT_SYMBOL(debug_dma_free_coherent);
1531 void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
1532 int direction, dma_addr_t dma_addr)
1534 struct dma_debug_entry *entry;
1536 if (unlikely(dma_debug_disabled()))
1537 return;
1539 entry = dma_entry_alloc();
1540 if (!entry)
1541 return;
1543 entry->type = dma_debug_resource;
1544 entry->dev = dev;
1545 entry->pfn = PHYS_PFN(addr);
1546 entry->offset = offset_in_page(addr);
1547 entry->size = size;
1548 entry->dev_addr = dma_addr;
1549 entry->direction = direction;
1550 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1552 add_dma_entry(entry);
1554 EXPORT_SYMBOL(debug_dma_map_resource);
1556 void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
1557 size_t size, int direction)
1559 struct dma_debug_entry ref = {
1560 .type = dma_debug_resource,
1561 .dev = dev,
1562 .dev_addr = dma_addr,
1563 .size = size,
1564 .direction = direction,
1567 if (unlikely(dma_debug_disabled()))
1568 return;
1570 check_unmap(&ref);
1572 EXPORT_SYMBOL(debug_dma_unmap_resource);
1574 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1575 size_t size, int direction)
1577 struct dma_debug_entry ref;
1579 if (unlikely(dma_debug_disabled()))
1580 return;
1582 ref.type = dma_debug_single;
1583 ref.dev = dev;
1584 ref.dev_addr = dma_handle;
1585 ref.size = size;
1586 ref.direction = direction;
1587 ref.sg_call_ents = 0;
1589 check_sync(dev, &ref, true);
1591 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1593 void debug_dma_sync_single_for_device(struct device *dev,
1594 dma_addr_t dma_handle, size_t size,
1595 int direction)
1597 struct dma_debug_entry ref;
1599 if (unlikely(dma_debug_disabled()))
1600 return;
1602 ref.type = dma_debug_single;
1603 ref.dev = dev;
1604 ref.dev_addr = dma_handle;
1605 ref.size = size;
1606 ref.direction = direction;
1607 ref.sg_call_ents = 0;
1609 check_sync(dev, &ref, false);
1611 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1613 void debug_dma_sync_single_range_for_cpu(struct device *dev,
1614 dma_addr_t dma_handle,
1615 unsigned long offset, size_t size,
1616 int direction)
1618 struct dma_debug_entry ref;
1620 if (unlikely(dma_debug_disabled()))
1621 return;
1623 ref.type = dma_debug_single;
1624 ref.dev = dev;
1625 ref.dev_addr = dma_handle;
1626 ref.size = offset + size;
1627 ref.direction = direction;
1628 ref.sg_call_ents = 0;
1630 check_sync(dev, &ref, true);
1632 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1634 void debug_dma_sync_single_range_for_device(struct device *dev,
1635 dma_addr_t dma_handle,
1636 unsigned long offset,
1637 size_t size, int direction)
1639 struct dma_debug_entry ref;
1641 if (unlikely(dma_debug_disabled()))
1642 return;
1644 ref.type = dma_debug_single;
1645 ref.dev = dev;
1646 ref.dev_addr = dma_handle;
1647 ref.size = offset + size;
1648 ref.direction = direction;
1649 ref.sg_call_ents = 0;
1651 check_sync(dev, &ref, false);
1653 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1655 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1656 int nelems, int direction)
1658 struct scatterlist *s;
1659 int mapped_ents = 0, i;
1661 if (unlikely(dma_debug_disabled()))
1662 return;
1664 for_each_sg(sg, s, nelems, i) {
1666 struct dma_debug_entry ref = {
1667 .type = dma_debug_sg,
1668 .dev = dev,
1669 .pfn = page_to_pfn(sg_page(s)),
1670 .offset = s->offset,
1671 .dev_addr = sg_dma_address(s),
1672 .size = sg_dma_len(s),
1673 .direction = direction,
1674 .sg_call_ents = nelems,
1677 if (!i)
1678 mapped_ents = get_nr_mapped_entries(dev, &ref);
1680 if (i >= mapped_ents)
1681 break;
1683 check_sync(dev, &ref, true);
1686 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1688 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1689 int nelems, int direction)
1691 struct scatterlist *s;
1692 int mapped_ents = 0, i;
1694 if (unlikely(dma_debug_disabled()))
1695 return;
1697 for_each_sg(sg, s, nelems, i) {
1699 struct dma_debug_entry ref = {
1700 .type = dma_debug_sg,
1701 .dev = dev,
1702 .pfn = page_to_pfn(sg_page(s)),
1703 .offset = s->offset,
1704 .dev_addr = sg_dma_address(s),
1705 .size = sg_dma_len(s),
1706 .direction = direction,
1707 .sg_call_ents = nelems,
1709 if (!i)
1710 mapped_ents = get_nr_mapped_entries(dev, &ref);
1712 if (i >= mapped_ents)
1713 break;
1715 check_sync(dev, &ref, false);
1718 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1720 static int __init dma_debug_driver_setup(char *str)
1722 int i;
1724 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1725 current_driver_name[i] = *str;
1726 if (*str == 0)
1727 break;
1730 if (current_driver_name[0])
1731 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1732 current_driver_name);
1735 return 1;
1737 __setup("dma_debug_driver=", dma_debug_driver_setup);