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/uaccess.h>
27 #include <linux/device.h>
28 #include <linux/types.h>
29 #include <linux/sched.h>
30 #include <linux/ctype.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
34 #include <asm/sections.h>
36 #define HASH_SIZE 1024ULL
37 #define HASH_FN_SHIFT 13
38 #define HASH_FN_MASK (HASH_SIZE - 1)
47 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
49 struct dma_debug_entry
{
50 struct list_head list
;
59 #ifdef CONFIG_STACKTRACE
60 struct stack_trace stacktrace
;
61 unsigned long st_entries
[DMA_DEBUG_STACKTRACE_ENTRIES
];
66 struct list_head list
;
68 } ____cacheline_aligned_in_smp
;
70 /* Hash list to save the allocated dma addresses */
71 static struct hash_bucket dma_entry_hash
[HASH_SIZE
];
72 /* List of pre-allocated dma_debug_entry's */
73 static LIST_HEAD(free_entries
);
74 /* Lock for the list above */
75 static DEFINE_SPINLOCK(free_entries_lock
);
77 /* Global disable flag - will be set in case of an error */
78 static bool global_disable __read_mostly
;
80 /* Global error count */
81 static u32 error_count
;
83 /* Global error show enable*/
84 static u32 show_all_errors __read_mostly
;
85 /* Number of errors to show */
86 static u32 show_num_errors
= 1;
88 static u32 num_free_entries
;
89 static u32 min_free_entries
;
90 static u32 nr_total_entries
;
92 /* number of preallocated entries requested by kernel cmdline */
93 static u32 req_entries
;
95 /* debugfs dentry's for the stuff above */
96 static struct dentry
*dma_debug_dent __read_mostly
;
97 static struct dentry
*global_disable_dent __read_mostly
;
98 static struct dentry
*error_count_dent __read_mostly
;
99 static struct dentry
*show_all_errors_dent __read_mostly
;
100 static struct dentry
*show_num_errors_dent __read_mostly
;
101 static struct dentry
*num_free_entries_dent __read_mostly
;
102 static struct dentry
*min_free_entries_dent __read_mostly
;
103 static struct dentry
*filter_dent __read_mostly
;
105 /* per-driver filter related state */
107 #define NAME_MAX_LEN 64
109 static char current_driver_name
[NAME_MAX_LEN
] __read_mostly
;
110 static struct device_driver
*current_driver __read_mostly
;
112 static DEFINE_RWLOCK(driver_name_lock
);
114 static const char *type2name
[4] = { "single", "page",
115 "scather-gather", "coherent" };
117 static const char *dir2name
[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
118 "DMA_FROM_DEVICE", "DMA_NONE" };
120 /* little merge helper - remove it after the merge window */
121 #ifndef BUS_NOTIFY_UNBOUND_DRIVER
122 #define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
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:
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.
138 static inline void dump_entry_trace(struct dma_debug_entry
*entry
)
140 #ifdef CONFIG_STACKTRACE
142 pr_warning("Mapped at:\n");
143 print_stack_trace(&entry
->stacktrace
, 0);
148 static bool driver_filter(struct device
*dev
)
150 struct device_driver
*drv
;
154 /* driver filter off */
155 if (likely(!current_driver_name
[0]))
158 /* driver filter on and initialized */
159 if (current_driver
&& dev
->driver
== current_driver
)
162 if (current_driver
|| !current_driver_name
[0])
165 /* driver filter on but not yet initialized */
166 drv
= get_driver(dev
->driver
);
170 /* lock to protect against change of current_driver_name */
171 read_lock_irqsave(&driver_name_lock
, flags
);
175 strncmp(current_driver_name
, drv
->name
, NAME_MAX_LEN
- 1) == 0) {
176 current_driver
= drv
;
180 read_unlock_irqrestore(&driver_name_lock
, flags
);
186 #define err_printk(dev, entry, format, arg...) do { \
188 if (driver_filter(dev) && \
189 (show_all_errors || show_num_errors > 0)) { \
190 WARN(1, "%s %s: " format, \
191 dev_driver_string(dev), \
192 dev_name(dev) , ## arg); \
193 dump_entry_trace(entry); \
195 if (!show_all_errors && show_num_errors > 0) \
196 show_num_errors -= 1; \
200 * Hash related functions
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.
205 static int hash_fn(struct dma_debug_entry
*entry
)
208 * Hash function is based on the dma address.
209 * We use bits 20-27 here as the index into the hash
211 return (entry
->dev_addr
>> HASH_FN_SHIFT
) & HASH_FN_MASK
;
215 * Request exclusive access to a hash bucket for a given dma_debug_entry.
217 static struct hash_bucket
*get_hash_bucket(struct dma_debug_entry
*entry
,
218 unsigned long *flags
)
220 int idx
= hash_fn(entry
);
221 unsigned long __flags
;
223 spin_lock_irqsave(&dma_entry_hash
[idx
].lock
, __flags
);
225 return &dma_entry_hash
[idx
];
229 * Give up exclusive access to the hash bucket
231 static void put_hash_bucket(struct hash_bucket
*bucket
,
232 unsigned long *flags
)
234 unsigned long __flags
= *flags
;
236 spin_unlock_irqrestore(&bucket
->lock
, __flags
);
240 * Search a given entry in the hash bucket list
242 static struct dma_debug_entry
*hash_bucket_find(struct hash_bucket
*bucket
,
243 struct dma_debug_entry
*ref
)
245 struct dma_debug_entry
*entry
, *ret
= NULL
;
246 int matches
= 0, match_lvl
, last_lvl
= 0;
248 list_for_each_entry(entry
, &bucket
->list
, list
) {
249 if ((entry
->dev_addr
!= ref
->dev_addr
) ||
250 (entry
->dev
!= ref
->dev
))
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.
265 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;
270 if (match_lvl
== 4) {
271 /* perfect-fit - return the result */
273 } else if (match_lvl
> last_lvl
) {
275 * We found an entry that fits better then the
278 last_lvl
= match_lvl
;
284 * If we have multiple matches but no perfect-fit, just return
287 ret
= (matches
== 1) ? ret
: NULL
;
293 * Add an entry to a hash bucket
295 static void hash_bucket_add(struct hash_bucket
*bucket
,
296 struct dma_debug_entry
*entry
)
298 list_add_tail(&entry
->list
, &bucket
->list
);
302 * Remove entry from a hash bucket list
304 static void hash_bucket_del(struct dma_debug_entry
*entry
)
306 list_del(&entry
->list
);
310 * Dump mapping entries for debugging purposes
312 void debug_dma_dump_mappings(struct device
*dev
)
316 for (idx
= 0; idx
< HASH_SIZE
; idx
++) {
317 struct hash_bucket
*bucket
= &dma_entry_hash
[idx
];
318 struct dma_debug_entry
*entry
;
321 spin_lock_irqsave(&bucket
->lock
, flags
);
323 list_for_each_entry(entry
, &bucket
->list
, list
) {
324 if (!dev
|| dev
== 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
]);
334 spin_unlock_irqrestore(&bucket
->lock
, flags
);
337 EXPORT_SYMBOL(debug_dma_dump_mappings
);
340 * Wrapper function for adding an entry to the hash.
341 * This function takes care of locking itself.
343 static void add_dma_entry(struct dma_debug_entry
*entry
)
345 struct hash_bucket
*bucket
;
348 bucket
= get_hash_bucket(entry
, &flags
);
349 hash_bucket_add(bucket
, entry
);
350 put_hash_bucket(bucket
, &flags
);
353 static struct dma_debug_entry
*__dma_entry_alloc(void)
355 struct dma_debug_entry
*entry
;
357 entry
= list_entry(free_entries
.next
, struct dma_debug_entry
, list
);
358 list_del(&entry
->list
);
359 memset(entry
, 0, sizeof(*entry
));
361 num_free_entries
-= 1;
362 if (num_free_entries
< min_free_entries
)
363 min_free_entries
= num_free_entries
;
368 /* struct dma_entry allocator
370 * The next two functions implement the allocator for
371 * struct dma_debug_entries.
373 static struct dma_debug_entry
*dma_entry_alloc(void)
375 struct dma_debug_entry
*entry
= NULL
;
378 spin_lock_irqsave(&free_entries_lock
, flags
);
380 if (list_empty(&free_entries
)) {
381 pr_err("DMA-API: debugging out of memory - disabling\n");
382 global_disable
= true;
386 entry
= __dma_entry_alloc();
388 #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
);
396 spin_unlock_irqrestore(&free_entries_lock
, flags
);
401 static void dma_entry_free(struct dma_debug_entry
*entry
)
406 * add to beginning of the list - this way the entries are
407 * more likely cache hot when they are reallocated.
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
);
415 int dma_debug_resize_entries(u32 num_entries
)
417 int i
, delta
, ret
= 0;
419 struct dma_debug_entry
*entry
;
422 spin_lock_irqsave(&free_entries_lock
, flags
);
424 if (nr_total_entries
< num_entries
) {
425 delta
= num_entries
- nr_total_entries
;
427 spin_unlock_irqrestore(&free_entries_lock
, flags
);
429 for (i
= 0; i
< delta
; i
++) {
430 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
434 list_add_tail(&entry
->list
, &tmp
);
437 spin_lock_irqsave(&free_entries_lock
, flags
);
439 list_splice(&tmp
, &free_entries
);
440 nr_total_entries
+= i
;
441 num_free_entries
+= i
;
443 delta
= nr_total_entries
- num_entries
;
445 for (i
= 0; i
< delta
&& !list_empty(&free_entries
); i
++) {
446 entry
= __dma_entry_alloc();
450 nr_total_entries
-= i
;
453 if (nr_total_entries
!= num_entries
)
456 spin_unlock_irqrestore(&free_entries_lock
, flags
);
460 EXPORT_SYMBOL(dma_debug_resize_entries
);
463 * DMA-API debugging init code
465 * The init code does two things:
466 * 1. Initialize core data structures
467 * 2. Preallocate a given number of dma_debug_entry structs
470 static int prealloc_memory(u32 num_entries
)
472 struct dma_debug_entry
*entry
, *next_entry
;
475 for (i
= 0; i
< num_entries
; ++i
) {
476 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
480 list_add_tail(&entry
->list
, &free_entries
);
483 num_free_entries
= num_entries
;
484 min_free_entries
= num_entries
;
486 pr_info("DMA-API: preallocated %d debug entries\n", num_entries
);
492 list_for_each_entry_safe(entry
, next_entry
, &free_entries
, list
) {
493 list_del(&entry
->list
);
500 static ssize_t
filter_read(struct file
*file
, char __user
*user_buf
,
501 size_t count
, loff_t
*ppos
)
503 char buf
[NAME_MAX_LEN
+ 1];
507 if (!current_driver_name
[0])
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.
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
);
519 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
522 static ssize_t
filter_write(struct file
*file
, const char __user
*userbuf
,
523 size_t count
, loff_t
*ppos
)
525 char buf
[NAME_MAX_LEN
];
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
536 len
= min(count
, (size_t)(NAME_MAX_LEN
- 1));
537 if (copy_from_user(buf
, userbuf
, len
))
542 write_lock_irqsave(&driver_name_lock
, flags
);
545 * Now handle the string we got from userspace very carefully.
547 * - only use the first token we got
548 * - token delimiter is everything looking like a space
549 * character (' ', '\n', '\t' ...)
552 if (!isalnum(buf
[0])) {
554 * If the first character userspace gave us is not
555 * alphanumerical then assume the filter should be
558 if (current_driver_name
[0])
559 pr_info("DMA-API: switching off dma-debug driver filter\n");
560 current_driver_name
[0] = 0;
561 current_driver
= NULL
;
566 * Now parse out the first token and use it as the name for the
567 * driver to filter for.
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)
574 current_driver_name
[i
] = 0;
575 current_driver
= NULL
;
577 pr_info("DMA-API: enable driver filter for driver [%s]\n",
578 current_driver_name
);
581 write_unlock_irqrestore(&driver_name_lock
, flags
);
586 const struct file_operations filter_fops
= {
588 .write
= filter_write
,
591 static int dma_debug_fs_init(void)
593 dma_debug_dent
= debugfs_create_dir("dma-api", NULL
);
594 if (!dma_debug_dent
) {
595 pr_err("DMA-API: can not create debugfs directory\n");
599 global_disable_dent
= debugfs_create_bool("disabled", 0444,
601 (u32
*)&global_disable
);
602 if (!global_disable_dent
)
605 error_count_dent
= debugfs_create_u32("error_count", 0444,
606 dma_debug_dent
, &error_count
);
607 if (!error_count_dent
)
610 show_all_errors_dent
= debugfs_create_u32("all_errors", 0644,
613 if (!show_all_errors_dent
)
616 show_num_errors_dent
= debugfs_create_u32("num_errors", 0644,
619 if (!show_num_errors_dent
)
622 num_free_entries_dent
= debugfs_create_u32("num_free_entries", 0444,
625 if (!num_free_entries_dent
)
628 min_free_entries_dent
= debugfs_create_u32("min_free_entries", 0444,
631 if (!min_free_entries_dent
)
634 filter_dent
= debugfs_create_file("driver_filter", 0644,
635 dma_debug_dent
, NULL
, &filter_fops
);
642 debugfs_remove_recursive(dma_debug_dent
);
647 static int device_dma_allocations(struct device
*dev
)
649 struct dma_debug_entry
*entry
;
653 local_irq_save(flags
);
655 for (i
= 0; i
< HASH_SIZE
; ++i
) {
656 spin_lock(&dma_entry_hash
[i
].lock
);
657 list_for_each_entry(entry
, &dma_entry_hash
[i
].list
, list
) {
658 if (entry
->dev
== dev
)
661 spin_unlock(&dma_entry_hash
[i
].lock
);
664 local_irq_restore(flags
);
669 static int dma_debug_device_change(struct notifier_block
*nb
,
670 unsigned long action
, void *data
)
672 struct device
*dev
= data
;
677 case BUS_NOTIFY_UNBOUND_DRIVER
:
678 count
= device_dma_allocations(dev
);
681 err_printk(dev
, NULL
, "DMA-API: device driver has pending "
682 "DMA allocations while released from device "
683 "[count=%d]\n", count
);
692 void dma_debug_add_bus(struct bus_type
*bus
)
694 struct notifier_block
*nb
;
696 nb
= kzalloc(sizeof(struct notifier_block
), GFP_KERNEL
);
698 pr_err("dma_debug_add_bus: out of memory\n");
702 nb
->notifier_call
= dma_debug_device_change
;
704 bus_register_notifier(bus
, nb
);
708 * Let the architectures decide how many entries should be preallocated.
710 void dma_debug_init(u32 num_entries
)
717 for (i
= 0; i
< HASH_SIZE
; ++i
) {
718 INIT_LIST_HEAD(&dma_entry_hash
[i
].list
);
719 dma_entry_hash
[i
].lock
= SPIN_LOCK_UNLOCKED
;
722 if (dma_debug_fs_init() != 0) {
723 pr_err("DMA-API: error creating debugfs entries - disabling\n");
724 global_disable
= true;
730 num_entries
= req_entries
;
732 if (prealloc_memory(num_entries
) != 0) {
733 pr_err("DMA-API: debugging out of memory error - disabled\n");
734 global_disable
= true;
739 nr_total_entries
= num_free_entries
;
741 pr_info("DMA-API: debugging enabled by kernel config\n");
744 static __init
int dma_debug_cmdline(char *str
)
749 if (strncmp(str
, "off", 3) == 0) {
750 pr_info("DMA-API: debugging disabled on kernel command line\n");
751 global_disable
= true;
757 static __init
int dma_debug_entries_cmdline(char *str
)
764 res
= get_option(&str
, &req_entries
);
772 __setup("dma_debug=", dma_debug_cmdline
);
773 __setup("dma_debug_entries=", dma_debug_entries_cmdline
);
775 static void check_unmap(struct dma_debug_entry
*ref
)
777 struct dma_debug_entry
*entry
;
778 struct hash_bucket
*bucket
;
781 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");
787 bucket
= get_hash_bucket(ref
, &flags
);
788 entry
= hash_bucket_find(bucket
, ref
);
791 err_printk(ref
->dev
, NULL
, "DMA-API: device driver tries "
792 "to free DMA memory it has not allocated "
793 "[device address=0x%016llx] [size=%llu bytes]\n",
794 ref
->dev_addr
, ref
->size
);
798 if (ref
->size
!= entry
->size
) {
799 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
800 "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
);
806 if (ref
->type
!= entry
->type
) {
807 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
808 "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
)) {
815 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
816 "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
);
823 if (ref
->sg_call_ents
&& ref
->type
== dma_debug_sg
&&
824 ref
->sg_call_ents
!= entry
->sg_call_ents
) {
825 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
826 "DMA sg list with different entry count "
827 "[map count=%d] [unmap count=%d]\n",
828 entry
->sg_call_ents
, ref
->sg_call_ents
);
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
835 if (ref
->direction
!= entry
->direction
) {
836 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
837 "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
]);
845 hash_bucket_del(entry
);
846 dma_entry_free(entry
);
849 put_hash_bucket(bucket
, &flags
);
852 static void check_for_stack(struct device
*dev
, void *addr
)
854 if (object_is_on_stack(addr
))
855 err_printk(dev
, NULL
, "DMA-API: device driver maps memory from"
856 "stack [addr=%p]\n", addr
);
859 static inline bool overlap(void *addr
, u64 size
, void *start
, void *end
)
861 void *addr2
= (char *)addr
+ size
;
863 return ((addr
>= start
&& addr
< end
) ||
864 (addr2
>= start
&& addr2
< end
) ||
865 ((addr
< start
) && (addr2
>= end
)));
868 static void check_for_illegal_area(struct device
*dev
, void *addr
, u64 size
)
870 if (overlap(addr
, size
, _text
, _etext
) ||
871 overlap(addr
, size
, __start_rodata
, __end_rodata
))
872 err_printk(dev
, NULL
, "DMA-API: device driver maps "
873 "memory from kernel text or rodata "
874 "[addr=%p] [size=%llu]\n", addr
, size
);
877 static void check_sync(struct device
*dev
,
878 struct dma_debug_entry
*ref
,
881 struct dma_debug_entry
*entry
;
882 struct hash_bucket
*bucket
;
885 bucket
= get_hash_bucket(ref
, &flags
);
887 entry
= hash_bucket_find(bucket
, ref
);
890 err_printk(dev
, NULL
, "DMA-API: device driver tries "
891 "to sync DMA memory it has not allocated "
892 "[device address=0x%016llx] [size=%llu bytes]\n",
893 (unsigned long long)ref
->dev_addr
, ref
->size
);
897 if (ref
->size
> entry
->size
) {
898 err_printk(dev
, entry
, "DMA-API: device driver syncs"
899 " DMA memory outside allocated range "
900 "[device address=0x%016llx] "
901 "[allocation size=%llu bytes] "
902 "[sync offset+size=%llu]\n",
903 entry
->dev_addr
, entry
->size
,
907 if (ref
->direction
!= entry
->direction
) {
908 err_printk(dev
, entry
, "DMA-API: device driver syncs "
909 "DMA memory with different direction "
910 "[device address=0x%016llx] [size=%llu bytes] "
911 "[mapped with %s] [synced with %s]\n",
912 (unsigned long long)ref
->dev_addr
, entry
->size
,
913 dir2name
[entry
->direction
],
914 dir2name
[ref
->direction
]);
917 if (entry
->direction
== DMA_BIDIRECTIONAL
)
920 if (to_cpu
&& !(entry
->direction
== DMA_FROM_DEVICE
) &&
921 !(ref
->direction
== DMA_TO_DEVICE
))
922 err_printk(dev
, entry
, "DMA-API: device driver syncs "
923 "device read-only DMA memory for cpu "
924 "[device address=0x%016llx] [size=%llu bytes] "
925 "[mapped with %s] [synced with %s]\n",
926 (unsigned long long)ref
->dev_addr
, entry
->size
,
927 dir2name
[entry
->direction
],
928 dir2name
[ref
->direction
]);
930 if (!to_cpu
&& !(entry
->direction
== DMA_TO_DEVICE
) &&
931 !(ref
->direction
== DMA_FROM_DEVICE
))
932 err_printk(dev
, entry
, "DMA-API: device driver syncs "
933 "device write-only DMA memory to device "
934 "[device address=0x%016llx] [size=%llu bytes] "
935 "[mapped with %s] [synced with %s]\n",
936 (unsigned long long)ref
->dev_addr
, entry
->size
,
937 dir2name
[entry
->direction
],
938 dir2name
[ref
->direction
]);
941 put_hash_bucket(bucket
, &flags
);
945 void debug_dma_map_page(struct device
*dev
, struct page
*page
, size_t offset
,
946 size_t size
, int direction
, dma_addr_t dma_addr
,
949 struct dma_debug_entry
*entry
;
951 if (unlikely(global_disable
))
954 if (unlikely(dma_mapping_error(dev
, dma_addr
)))
957 entry
= dma_entry_alloc();
962 entry
->type
= dma_debug_page
;
963 entry
->paddr
= page_to_phys(page
) + offset
;
964 entry
->dev_addr
= dma_addr
;
966 entry
->direction
= direction
;
969 entry
->type
= dma_debug_single
;
971 if (!PageHighMem(page
)) {
972 void *addr
= ((char *)page_address(page
)) + offset
;
973 check_for_stack(dev
, addr
);
974 check_for_illegal_area(dev
, addr
, size
);
977 add_dma_entry(entry
);
979 EXPORT_SYMBOL(debug_dma_map_page
);
981 void debug_dma_unmap_page(struct device
*dev
, dma_addr_t addr
,
982 size_t size
, int direction
, bool map_single
)
984 struct dma_debug_entry ref
= {
985 .type
= dma_debug_page
,
989 .direction
= direction
,
992 if (unlikely(global_disable
))
996 ref
.type
= dma_debug_single
;
1000 EXPORT_SYMBOL(debug_dma_unmap_page
);
1002 void debug_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
1003 int nents
, int mapped_ents
, int direction
)
1005 struct dma_debug_entry
*entry
;
1006 struct scatterlist
*s
;
1009 if (unlikely(global_disable
))
1012 for_each_sg(sg
, s
, mapped_ents
, i
) {
1013 entry
= dma_entry_alloc();
1017 entry
->type
= dma_debug_sg
;
1019 entry
->paddr
= sg_phys(s
);
1020 entry
->size
= sg_dma_len(s
);
1021 entry
->dev_addr
= sg_dma_address(s
);
1022 entry
->direction
= direction
;
1023 entry
->sg_call_ents
= nents
;
1024 entry
->sg_mapped_ents
= mapped_ents
;
1026 if (!PageHighMem(sg_page(s
))) {
1027 check_for_stack(dev
, sg_virt(s
));
1028 check_for_illegal_area(dev
, sg_virt(s
), sg_dma_len(s
));
1031 add_dma_entry(entry
);
1034 EXPORT_SYMBOL(debug_dma_map_sg
);
1036 static int get_nr_mapped_entries(struct device
*dev
,
1037 struct dma_debug_entry
*ref
)
1039 struct dma_debug_entry
*entry
;
1040 struct hash_bucket
*bucket
;
1041 unsigned long flags
;
1044 bucket
= get_hash_bucket(ref
, &flags
);
1045 entry
= hash_bucket_find(bucket
, ref
);
1049 mapped_ents
= entry
->sg_mapped_ents
;
1050 put_hash_bucket(bucket
, &flags
);
1055 void debug_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
1056 int nelems
, int dir
)
1058 struct scatterlist
*s
;
1059 int mapped_ents
= 0, i
;
1061 if (unlikely(global_disable
))
1064 for_each_sg(sglist
, s
, nelems
, i
) {
1066 struct dma_debug_entry ref
= {
1067 .type
= dma_debug_sg
,
1069 .paddr
= sg_phys(s
),
1070 .dev_addr
= sg_dma_address(s
),
1071 .size
= sg_dma_len(s
),
1073 .sg_call_ents
= nelems
,
1076 if (mapped_ents
&& i
>= mapped_ents
)
1080 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1085 EXPORT_SYMBOL(debug_dma_unmap_sg
);
1087 void debug_dma_alloc_coherent(struct device
*dev
, size_t size
,
1088 dma_addr_t dma_addr
, void *virt
)
1090 struct dma_debug_entry
*entry
;
1092 if (unlikely(global_disable
))
1095 if (unlikely(virt
== NULL
))
1098 entry
= dma_entry_alloc();
1102 entry
->type
= dma_debug_coherent
;
1104 entry
->paddr
= virt_to_phys(virt
);
1106 entry
->dev_addr
= dma_addr
;
1107 entry
->direction
= DMA_BIDIRECTIONAL
;
1109 add_dma_entry(entry
);
1111 EXPORT_SYMBOL(debug_dma_alloc_coherent
);
1113 void debug_dma_free_coherent(struct device
*dev
, size_t size
,
1114 void *virt
, dma_addr_t addr
)
1116 struct dma_debug_entry ref
= {
1117 .type
= dma_debug_coherent
,
1119 .paddr
= virt_to_phys(virt
),
1122 .direction
= DMA_BIDIRECTIONAL
,
1125 if (unlikely(global_disable
))
1130 EXPORT_SYMBOL(debug_dma_free_coherent
);
1132 void debug_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
1133 size_t size
, int direction
)
1135 struct dma_debug_entry ref
;
1137 if (unlikely(global_disable
))
1140 ref
.type
= dma_debug_single
;
1142 ref
.dev_addr
= dma_handle
;
1144 ref
.direction
= direction
;
1145 ref
.sg_call_ents
= 0;
1147 check_sync(dev
, &ref
, true);
1149 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu
);
1151 void debug_dma_sync_single_for_device(struct device
*dev
,
1152 dma_addr_t dma_handle
, size_t size
,
1155 struct dma_debug_entry ref
;
1157 if (unlikely(global_disable
))
1160 ref
.type
= dma_debug_single
;
1162 ref
.dev_addr
= dma_handle
;
1164 ref
.direction
= direction
;
1165 ref
.sg_call_ents
= 0;
1167 check_sync(dev
, &ref
, false);
1169 EXPORT_SYMBOL(debug_dma_sync_single_for_device
);
1171 void debug_dma_sync_single_range_for_cpu(struct device
*dev
,
1172 dma_addr_t dma_handle
,
1173 unsigned long offset
, size_t size
,
1176 struct dma_debug_entry ref
;
1178 if (unlikely(global_disable
))
1181 ref
.type
= dma_debug_single
;
1183 ref
.dev_addr
= dma_handle
;
1184 ref
.size
= offset
+ size
;
1185 ref
.direction
= direction
;
1186 ref
.sg_call_ents
= 0;
1188 check_sync(dev
, &ref
, true);
1190 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu
);
1192 void 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
)
1197 struct dma_debug_entry ref
;
1199 if (unlikely(global_disable
))
1202 ref
.type
= dma_debug_single
;
1204 ref
.dev_addr
= dma_handle
;
1205 ref
.size
= offset
+ size
;
1206 ref
.direction
= direction
;
1207 ref
.sg_call_ents
= 0;
1209 check_sync(dev
, &ref
, false);
1211 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device
);
1213 void debug_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
1214 int nelems
, int direction
)
1216 struct scatterlist
*s
;
1217 int mapped_ents
= 0, i
;
1219 if (unlikely(global_disable
))
1222 for_each_sg(sg
, s
, nelems
, i
) {
1224 struct dma_debug_entry ref
= {
1225 .type
= dma_debug_sg
,
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
,
1235 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1237 if (i
>= mapped_ents
)
1240 check_sync(dev
, &ref
, true);
1243 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu
);
1245 void debug_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
1246 int nelems
, int direction
)
1248 struct scatterlist
*s
;
1249 int mapped_ents
= 0, i
;
1251 if (unlikely(global_disable
))
1254 for_each_sg(sg
, s
, nelems
, i
) {
1256 struct dma_debug_entry ref
= {
1257 .type
= dma_debug_sg
,
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
,
1266 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1268 if (i
>= mapped_ents
)
1271 check_sync(dev
, &ref
, false);
1274 EXPORT_SYMBOL(debug_dma_sync_sg_for_device
);
1276 static int __init
dma_debug_driver_setup(char *str
)
1280 for (i
= 0; i
< NAME_MAX_LEN
- 1; ++i
, ++str
) {
1281 current_driver_name
[i
] = *str
;
1286 if (current_driver_name
[0])
1287 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1288 current_driver_name
);
1293 __setup("dma_debug_driver=", dma_debug_driver_setup
);