dma-debug: add variables and checks for driver filter
[linux-2.6/mini2440.git] / lib / dma-debug.c
blobc01f64780caf74cbab3eebe6955a2d0bb8a28837
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/scatterlist.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/stacktrace.h>
23 #include <linux/dma-debug.h>
24 #include <linux/spinlock.h>
25 #include <linux/debugfs.h>
26 #include <linux/device.h>
27 #include <linux/types.h>
28 #include <linux/sched.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
32 #include <asm/sections.h>
34 #define HASH_SIZE 1024ULL
35 #define HASH_FN_SHIFT 13
36 #define HASH_FN_MASK (HASH_SIZE - 1)
38 enum {
39 dma_debug_single,
40 dma_debug_page,
41 dma_debug_sg,
42 dma_debug_coherent,
45 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
47 struct dma_debug_entry {
48 struct list_head list;
49 struct device *dev;
50 int type;
51 phys_addr_t paddr;
52 u64 dev_addr;
53 u64 size;
54 int direction;
55 int sg_call_ents;
56 int sg_mapped_ents;
57 #ifdef CONFIG_STACKTRACE
58 struct stack_trace stacktrace;
59 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
60 #endif
63 struct hash_bucket {
64 struct list_head list;
65 spinlock_t lock;
66 } ____cacheline_aligned_in_smp;
68 /* Hash list to save the allocated dma addresses */
69 static struct hash_bucket dma_entry_hash[HASH_SIZE];
70 /* List of pre-allocated dma_debug_entry's */
71 static LIST_HEAD(free_entries);
72 /* Lock for the list above */
73 static DEFINE_SPINLOCK(free_entries_lock);
75 /* Global disable flag - will be set in case of an error */
76 static bool global_disable __read_mostly;
78 /* Global error count */
79 static u32 error_count;
81 /* Global error show enable*/
82 static u32 show_all_errors __read_mostly;
83 /* Number of errors to show */
84 static u32 show_num_errors = 1;
86 static u32 num_free_entries;
87 static u32 min_free_entries;
88 static u32 nr_total_entries;
90 /* number of preallocated entries requested by kernel cmdline */
91 static u32 req_entries;
93 /* debugfs dentry's for the stuff above */
94 static struct dentry *dma_debug_dent __read_mostly;
95 static struct dentry *global_disable_dent __read_mostly;
96 static struct dentry *error_count_dent __read_mostly;
97 static struct dentry *show_all_errors_dent __read_mostly;
98 static struct dentry *show_num_errors_dent __read_mostly;
99 static struct dentry *num_free_entries_dent __read_mostly;
100 static struct dentry *min_free_entries_dent __read_mostly;
102 /* per-driver filter related state */
104 #define NAME_MAX_LEN 64
106 static char current_driver_name[NAME_MAX_LEN] __read_mostly;
107 static struct device_driver *current_driver __read_mostly;
109 static DEFINE_RWLOCK(driver_name_lock);
111 static const char *type2name[4] = { "single", "page",
112 "scather-gather", "coherent" };
114 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
115 "DMA_FROM_DEVICE", "DMA_NONE" };
118 * The access to some variables in this macro is racy. We can't use atomic_t
119 * here because all these variables are exported to debugfs. Some of them even
120 * writeable. This is also the reason why a lock won't help much. But anyway,
121 * the races are no big deal. Here is why:
123 * error_count: the addition is racy, but the worst thing that can happen is
124 * that we don't count some errors
125 * show_num_errors: the subtraction is racy. Also no big deal because in
126 * worst case this will result in one warning more in the
127 * system log than the user configured. This variable is
128 * writeable via debugfs.
130 static inline void dump_entry_trace(struct dma_debug_entry *entry)
132 #ifdef CONFIG_STACKTRACE
133 if (entry) {
134 printk(KERN_WARNING "Mapped at:\n");
135 print_stack_trace(&entry->stacktrace, 0);
137 #endif
140 static bool driver_filter(struct device *dev)
142 /* driver filter off */
143 if (likely(!current_driver_name[0]))
144 return true;
146 /* driver filter on and initialized */
147 if (current_driver && dev->driver == current_driver)
148 return true;
150 /* driver filter on but not yet initialized */
151 if (!current_driver && current_driver_name[0]) {
152 struct device_driver *drv = get_driver(dev->driver);
153 unsigned long flags;
154 bool ret = false;
156 if (!drv)
157 return false;
159 /* lock to protect against change of current_driver_name */
160 read_lock_irqsave(&driver_name_lock, flags);
162 if (drv->name &&
163 strncmp(current_driver_name, drv->name, 63) == 0) {
164 current_driver = drv;
165 ret = true;
168 read_unlock_irqrestore(&driver_name_lock, flags);
169 put_driver(drv);
171 return ret;
174 return false;
177 #define err_printk(dev, entry, format, arg...) do { \
178 error_count += 1; \
179 if (driver_filter(dev) && \
180 (show_all_errors || show_num_errors > 0)) { \
181 WARN(1, "%s %s: " format, \
182 dev_driver_string(dev), \
183 dev_name(dev) , ## arg); \
184 dump_entry_trace(entry); \
186 if (!show_all_errors && show_num_errors > 0) \
187 show_num_errors -= 1; \
188 } while (0);
191 * Hash related functions
193 * Every DMA-API request is saved into a struct dma_debug_entry. To
194 * have quick access to these structs they are stored into a hash.
196 static int hash_fn(struct dma_debug_entry *entry)
199 * Hash function is based on the dma address.
200 * We use bits 20-27 here as the index into the hash
202 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
206 * Request exclusive access to a hash bucket for a given dma_debug_entry.
208 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
209 unsigned long *flags)
211 int idx = hash_fn(entry);
212 unsigned long __flags;
214 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
215 *flags = __flags;
216 return &dma_entry_hash[idx];
220 * Give up exclusive access to the hash bucket
222 static void put_hash_bucket(struct hash_bucket *bucket,
223 unsigned long *flags)
225 unsigned long __flags = *flags;
227 spin_unlock_irqrestore(&bucket->lock, __flags);
231 * Search a given entry in the hash bucket list
233 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
234 struct dma_debug_entry *ref)
236 struct dma_debug_entry *entry;
238 list_for_each_entry(entry, &bucket->list, list) {
239 if ((entry->dev_addr == ref->dev_addr) &&
240 (entry->dev == ref->dev))
241 return entry;
244 return NULL;
248 * Add an entry to a hash bucket
250 static void hash_bucket_add(struct hash_bucket *bucket,
251 struct dma_debug_entry *entry)
253 list_add_tail(&entry->list, &bucket->list);
257 * Remove entry from a hash bucket list
259 static void hash_bucket_del(struct dma_debug_entry *entry)
261 list_del(&entry->list);
265 * Dump mapping entries for debugging purposes
267 void debug_dma_dump_mappings(struct device *dev)
269 int idx;
271 for (idx = 0; idx < HASH_SIZE; idx++) {
272 struct hash_bucket *bucket = &dma_entry_hash[idx];
273 struct dma_debug_entry *entry;
274 unsigned long flags;
276 spin_lock_irqsave(&bucket->lock, flags);
278 list_for_each_entry(entry, &bucket->list, list) {
279 if (!dev || dev == entry->dev) {
280 dev_info(entry->dev,
281 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
282 type2name[entry->type], idx,
283 (unsigned long long)entry->paddr,
284 entry->dev_addr, entry->size,
285 dir2name[entry->direction]);
289 spin_unlock_irqrestore(&bucket->lock, flags);
292 EXPORT_SYMBOL(debug_dma_dump_mappings);
295 * Wrapper function for adding an entry to the hash.
296 * This function takes care of locking itself.
298 static void add_dma_entry(struct dma_debug_entry *entry)
300 struct hash_bucket *bucket;
301 unsigned long flags;
303 bucket = get_hash_bucket(entry, &flags);
304 hash_bucket_add(bucket, entry);
305 put_hash_bucket(bucket, &flags);
308 static struct dma_debug_entry *__dma_entry_alloc(void)
310 struct dma_debug_entry *entry;
312 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
313 list_del(&entry->list);
314 memset(entry, 0, sizeof(*entry));
316 num_free_entries -= 1;
317 if (num_free_entries < min_free_entries)
318 min_free_entries = num_free_entries;
320 return entry;
323 /* struct dma_entry allocator
325 * The next two functions implement the allocator for
326 * struct dma_debug_entries.
328 static struct dma_debug_entry *dma_entry_alloc(void)
330 struct dma_debug_entry *entry = NULL;
331 unsigned long flags;
333 spin_lock_irqsave(&free_entries_lock, flags);
335 if (list_empty(&free_entries)) {
336 printk(KERN_ERR "DMA-API: debugging out of memory "
337 "- disabling\n");
338 global_disable = true;
339 goto out;
342 entry = __dma_entry_alloc();
344 #ifdef CONFIG_STACKTRACE
345 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
346 entry->stacktrace.entries = entry->st_entries;
347 entry->stacktrace.skip = 2;
348 save_stack_trace(&entry->stacktrace);
349 #endif
351 out:
352 spin_unlock_irqrestore(&free_entries_lock, flags);
354 return entry;
357 static void dma_entry_free(struct dma_debug_entry *entry)
359 unsigned long flags;
362 * add to beginning of the list - this way the entries are
363 * more likely cache hot when they are reallocated.
365 spin_lock_irqsave(&free_entries_lock, flags);
366 list_add(&entry->list, &free_entries);
367 num_free_entries += 1;
368 spin_unlock_irqrestore(&free_entries_lock, flags);
371 int dma_debug_resize_entries(u32 num_entries)
373 int i, delta, ret = 0;
374 unsigned long flags;
375 struct dma_debug_entry *entry;
376 LIST_HEAD(tmp);
378 spin_lock_irqsave(&free_entries_lock, flags);
380 if (nr_total_entries < num_entries) {
381 delta = num_entries - nr_total_entries;
383 spin_unlock_irqrestore(&free_entries_lock, flags);
385 for (i = 0; i < delta; i++) {
386 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
387 if (!entry)
388 break;
390 list_add_tail(&entry->list, &tmp);
393 spin_lock_irqsave(&free_entries_lock, flags);
395 list_splice(&tmp, &free_entries);
396 nr_total_entries += i;
397 num_free_entries += i;
398 } else {
399 delta = nr_total_entries - num_entries;
401 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
402 entry = __dma_entry_alloc();
403 kfree(entry);
406 nr_total_entries -= i;
409 if (nr_total_entries != num_entries)
410 ret = 1;
412 spin_unlock_irqrestore(&free_entries_lock, flags);
414 return ret;
416 EXPORT_SYMBOL(dma_debug_resize_entries);
419 * DMA-API debugging init code
421 * The init code does two things:
422 * 1. Initialize core data structures
423 * 2. Preallocate a given number of dma_debug_entry structs
426 static int prealloc_memory(u32 num_entries)
428 struct dma_debug_entry *entry, *next_entry;
429 int i;
431 for (i = 0; i < num_entries; ++i) {
432 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
433 if (!entry)
434 goto out_err;
436 list_add_tail(&entry->list, &free_entries);
439 num_free_entries = num_entries;
440 min_free_entries = num_entries;
442 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
443 num_entries);
445 return 0;
447 out_err:
449 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
450 list_del(&entry->list);
451 kfree(entry);
454 return -ENOMEM;
457 static int dma_debug_fs_init(void)
459 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
460 if (!dma_debug_dent) {
461 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
462 return -ENOMEM;
465 global_disable_dent = debugfs_create_bool("disabled", 0444,
466 dma_debug_dent,
467 (u32 *)&global_disable);
468 if (!global_disable_dent)
469 goto out_err;
471 error_count_dent = debugfs_create_u32("error_count", 0444,
472 dma_debug_dent, &error_count);
473 if (!error_count_dent)
474 goto out_err;
476 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
477 dma_debug_dent,
478 &show_all_errors);
479 if (!show_all_errors_dent)
480 goto out_err;
482 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
483 dma_debug_dent,
484 &show_num_errors);
485 if (!show_num_errors_dent)
486 goto out_err;
488 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
489 dma_debug_dent,
490 &num_free_entries);
491 if (!num_free_entries_dent)
492 goto out_err;
494 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
495 dma_debug_dent,
496 &min_free_entries);
497 if (!min_free_entries_dent)
498 goto out_err;
500 return 0;
502 out_err:
503 debugfs_remove_recursive(dma_debug_dent);
505 return -ENOMEM;
508 void dma_debug_add_bus(struct bus_type *bus)
510 /* FIXME: register notifier */
514 * Let the architectures decide how many entries should be preallocated.
516 void dma_debug_init(u32 num_entries)
518 int i;
520 if (global_disable)
521 return;
523 for (i = 0; i < HASH_SIZE; ++i) {
524 INIT_LIST_HEAD(&dma_entry_hash[i].list);
525 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
528 if (dma_debug_fs_init() != 0) {
529 printk(KERN_ERR "DMA-API: error creating debugfs entries "
530 "- disabling\n");
531 global_disable = true;
533 return;
536 if (req_entries)
537 num_entries = req_entries;
539 if (prealloc_memory(num_entries) != 0) {
540 printk(KERN_ERR "DMA-API: debugging out of memory error "
541 "- disabled\n");
542 global_disable = true;
544 return;
547 nr_total_entries = num_free_entries;
549 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
552 static __init int dma_debug_cmdline(char *str)
554 if (!str)
555 return -EINVAL;
557 if (strncmp(str, "off", 3) == 0) {
558 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
559 "command line\n");
560 global_disable = true;
563 return 0;
566 static __init int dma_debug_entries_cmdline(char *str)
568 int res;
570 if (!str)
571 return -EINVAL;
573 res = get_option(&str, &req_entries);
575 if (!res)
576 req_entries = 0;
578 return 0;
581 __setup("dma_debug=", dma_debug_cmdline);
582 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
584 static void check_unmap(struct dma_debug_entry *ref)
586 struct dma_debug_entry *entry;
587 struct hash_bucket *bucket;
588 unsigned long flags;
590 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
591 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
592 "to free an invalid DMA memory address\n");
593 return;
596 bucket = get_hash_bucket(ref, &flags);
597 entry = hash_bucket_find(bucket, ref);
599 if (!entry) {
600 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
601 "to free DMA memory it has not allocated "
602 "[device address=0x%016llx] [size=%llu bytes]\n",
603 ref->dev_addr, ref->size);
604 goto out;
607 if (ref->size != entry->size) {
608 err_printk(ref->dev, entry, "DMA-API: device driver frees "
609 "DMA memory with different size "
610 "[device address=0x%016llx] [map size=%llu bytes] "
611 "[unmap size=%llu bytes]\n",
612 ref->dev_addr, entry->size, ref->size);
615 if (ref->type != entry->type) {
616 err_printk(ref->dev, entry, "DMA-API: device driver frees "
617 "DMA memory with wrong function "
618 "[device address=0x%016llx] [size=%llu bytes] "
619 "[mapped as %s] [unmapped as %s]\n",
620 ref->dev_addr, ref->size,
621 type2name[entry->type], type2name[ref->type]);
622 } else if ((entry->type == dma_debug_coherent) &&
623 (ref->paddr != entry->paddr)) {
624 err_printk(ref->dev, entry, "DMA-API: device driver frees "
625 "DMA memory with different CPU address "
626 "[device address=0x%016llx] [size=%llu bytes] "
627 "[cpu alloc address=%p] [cpu free address=%p]",
628 ref->dev_addr, ref->size,
629 (void *)entry->paddr, (void *)ref->paddr);
632 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
633 ref->sg_call_ents != entry->sg_call_ents) {
634 err_printk(ref->dev, entry, "DMA-API: device driver frees "
635 "DMA sg list with different entry count "
636 "[map count=%d] [unmap count=%d]\n",
637 entry->sg_call_ents, ref->sg_call_ents);
641 * This may be no bug in reality - but most implementations of the
642 * DMA API don't handle this properly, so check for it here
644 if (ref->direction != entry->direction) {
645 err_printk(ref->dev, entry, "DMA-API: device driver frees "
646 "DMA memory with different direction "
647 "[device address=0x%016llx] [size=%llu bytes] "
648 "[mapped with %s] [unmapped with %s]\n",
649 ref->dev_addr, ref->size,
650 dir2name[entry->direction],
651 dir2name[ref->direction]);
654 hash_bucket_del(entry);
655 dma_entry_free(entry);
657 out:
658 put_hash_bucket(bucket, &flags);
661 static void check_for_stack(struct device *dev, void *addr)
663 if (object_is_on_stack(addr))
664 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
665 "stack [addr=%p]\n", addr);
668 static inline bool overlap(void *addr, u64 size, void *start, void *end)
670 void *addr2 = (char *)addr + size;
672 return ((addr >= start && addr < end) ||
673 (addr2 >= start && addr2 < end) ||
674 ((addr < start) && (addr2 >= end)));
677 static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
679 if (overlap(addr, size, _text, _etext) ||
680 overlap(addr, size, __start_rodata, __end_rodata))
681 err_printk(dev, NULL, "DMA-API: device driver maps "
682 "memory from kernel text or rodata "
683 "[addr=%p] [size=%llu]\n", addr, size);
686 static void check_sync(struct device *dev, dma_addr_t addr,
687 u64 size, u64 offset, int direction, bool to_cpu)
689 struct dma_debug_entry ref = {
690 .dev = dev,
691 .dev_addr = addr,
692 .size = size,
693 .direction = direction,
695 struct dma_debug_entry *entry;
696 struct hash_bucket *bucket;
697 unsigned long flags;
699 bucket = get_hash_bucket(&ref, &flags);
701 entry = hash_bucket_find(bucket, &ref);
703 if (!entry) {
704 err_printk(dev, NULL, "DMA-API: device driver tries "
705 "to sync DMA memory it has not allocated "
706 "[device address=0x%016llx] [size=%llu bytes]\n",
707 (unsigned long long)addr, size);
708 goto out;
711 if ((offset + size) > entry->size) {
712 err_printk(dev, entry, "DMA-API: device driver syncs"
713 " DMA memory outside allocated range "
714 "[device address=0x%016llx] "
715 "[allocation size=%llu bytes] [sync offset=%llu] "
716 "[sync size=%llu]\n", entry->dev_addr, entry->size,
717 offset, size);
720 if (direction != entry->direction) {
721 err_printk(dev, entry, "DMA-API: device driver syncs "
722 "DMA memory with different direction "
723 "[device address=0x%016llx] [size=%llu bytes] "
724 "[mapped with %s] [synced with %s]\n",
725 (unsigned long long)addr, entry->size,
726 dir2name[entry->direction],
727 dir2name[direction]);
730 if (entry->direction == DMA_BIDIRECTIONAL)
731 goto out;
733 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
734 !(direction == DMA_TO_DEVICE))
735 err_printk(dev, entry, "DMA-API: device driver syncs "
736 "device read-only DMA memory for cpu "
737 "[device address=0x%016llx] [size=%llu bytes] "
738 "[mapped with %s] [synced with %s]\n",
739 (unsigned long long)addr, entry->size,
740 dir2name[entry->direction],
741 dir2name[direction]);
743 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
744 !(direction == DMA_FROM_DEVICE))
745 err_printk(dev, entry, "DMA-API: device driver syncs "
746 "device write-only DMA memory to device "
747 "[device address=0x%016llx] [size=%llu bytes] "
748 "[mapped with %s] [synced with %s]\n",
749 (unsigned long long)addr, entry->size,
750 dir2name[entry->direction],
751 dir2name[direction]);
753 out:
754 put_hash_bucket(bucket, &flags);
758 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
759 size_t size, int direction, dma_addr_t dma_addr,
760 bool map_single)
762 struct dma_debug_entry *entry;
764 if (unlikely(global_disable))
765 return;
767 if (unlikely(dma_mapping_error(dev, dma_addr)))
768 return;
770 entry = dma_entry_alloc();
771 if (!entry)
772 return;
774 entry->dev = dev;
775 entry->type = dma_debug_page;
776 entry->paddr = page_to_phys(page) + offset;
777 entry->dev_addr = dma_addr;
778 entry->size = size;
779 entry->direction = direction;
781 if (map_single)
782 entry->type = dma_debug_single;
784 if (!PageHighMem(page)) {
785 void *addr = ((char *)page_address(page)) + offset;
786 check_for_stack(dev, addr);
787 check_for_illegal_area(dev, addr, size);
790 add_dma_entry(entry);
792 EXPORT_SYMBOL(debug_dma_map_page);
794 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
795 size_t size, int direction, bool map_single)
797 struct dma_debug_entry ref = {
798 .type = dma_debug_page,
799 .dev = dev,
800 .dev_addr = addr,
801 .size = size,
802 .direction = direction,
805 if (unlikely(global_disable))
806 return;
808 if (map_single)
809 ref.type = dma_debug_single;
811 check_unmap(&ref);
813 EXPORT_SYMBOL(debug_dma_unmap_page);
815 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
816 int nents, int mapped_ents, int direction)
818 struct dma_debug_entry *entry;
819 struct scatterlist *s;
820 int i;
822 if (unlikely(global_disable))
823 return;
825 for_each_sg(sg, s, mapped_ents, i) {
826 entry = dma_entry_alloc();
827 if (!entry)
828 return;
830 entry->type = dma_debug_sg;
831 entry->dev = dev;
832 entry->paddr = sg_phys(s);
833 entry->size = s->length;
834 entry->dev_addr = s->dma_address;
835 entry->direction = direction;
836 entry->sg_call_ents = nents;
837 entry->sg_mapped_ents = mapped_ents;
839 if (!PageHighMem(sg_page(s))) {
840 check_for_stack(dev, sg_virt(s));
841 check_for_illegal_area(dev, sg_virt(s), s->length);
844 add_dma_entry(entry);
847 EXPORT_SYMBOL(debug_dma_map_sg);
849 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
850 int nelems, int dir)
852 struct dma_debug_entry *entry;
853 struct scatterlist *s;
854 int mapped_ents = 0, i;
855 unsigned long flags;
857 if (unlikely(global_disable))
858 return;
860 for_each_sg(sglist, s, nelems, i) {
862 struct dma_debug_entry ref = {
863 .type = dma_debug_sg,
864 .dev = dev,
865 .paddr = sg_phys(s),
866 .dev_addr = s->dma_address,
867 .size = s->length,
868 .direction = dir,
869 .sg_call_ents = 0,
872 if (mapped_ents && i >= mapped_ents)
873 break;
875 if (mapped_ents == 0) {
876 struct hash_bucket *bucket;
877 ref.sg_call_ents = nelems;
878 bucket = get_hash_bucket(&ref, &flags);
879 entry = hash_bucket_find(bucket, &ref);
880 if (entry)
881 mapped_ents = entry->sg_mapped_ents;
882 put_hash_bucket(bucket, &flags);
885 check_unmap(&ref);
888 EXPORT_SYMBOL(debug_dma_unmap_sg);
890 void debug_dma_alloc_coherent(struct device *dev, size_t size,
891 dma_addr_t dma_addr, void *virt)
893 struct dma_debug_entry *entry;
895 if (unlikely(global_disable))
896 return;
898 if (unlikely(virt == NULL))
899 return;
901 entry = dma_entry_alloc();
902 if (!entry)
903 return;
905 entry->type = dma_debug_coherent;
906 entry->dev = dev;
907 entry->paddr = virt_to_phys(virt);
908 entry->size = size;
909 entry->dev_addr = dma_addr;
910 entry->direction = DMA_BIDIRECTIONAL;
912 add_dma_entry(entry);
914 EXPORT_SYMBOL(debug_dma_alloc_coherent);
916 void debug_dma_free_coherent(struct device *dev, size_t size,
917 void *virt, dma_addr_t addr)
919 struct dma_debug_entry ref = {
920 .type = dma_debug_coherent,
921 .dev = dev,
922 .paddr = virt_to_phys(virt),
923 .dev_addr = addr,
924 .size = size,
925 .direction = DMA_BIDIRECTIONAL,
928 if (unlikely(global_disable))
929 return;
931 check_unmap(&ref);
933 EXPORT_SYMBOL(debug_dma_free_coherent);
935 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
936 size_t size, int direction)
938 if (unlikely(global_disable))
939 return;
941 check_sync(dev, dma_handle, size, 0, direction, true);
943 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
945 void debug_dma_sync_single_for_device(struct device *dev,
946 dma_addr_t dma_handle, size_t size,
947 int direction)
949 if (unlikely(global_disable))
950 return;
952 check_sync(dev, dma_handle, size, 0, direction, false);
954 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
956 void debug_dma_sync_single_range_for_cpu(struct device *dev,
957 dma_addr_t dma_handle,
958 unsigned long offset, size_t size,
959 int direction)
961 if (unlikely(global_disable))
962 return;
964 check_sync(dev, dma_handle, size, offset, direction, true);
966 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
968 void debug_dma_sync_single_range_for_device(struct device *dev,
969 dma_addr_t dma_handle,
970 unsigned long offset,
971 size_t size, int direction)
973 if (unlikely(global_disable))
974 return;
976 check_sync(dev, dma_handle, size, offset, direction, false);
978 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
980 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
981 int nelems, int direction)
983 struct scatterlist *s;
984 int i;
986 if (unlikely(global_disable))
987 return;
989 for_each_sg(sg, s, nelems, i) {
990 check_sync(dev, s->dma_address, s->dma_length, 0,
991 direction, true);
994 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
996 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
997 int nelems, int direction)
999 struct scatterlist *s;
1000 int i;
1002 if (unlikely(global_disable))
1003 return;
1005 for_each_sg(sg, s, nelems, i) {
1006 check_sync(dev, s->dma_address, s->dma_length, 0,
1007 direction, false);
1010 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);