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
&& dev
->driver
== current_driver
)
162 /* driver filter on, but we can't filter on a NULL device... */
166 if (current_driver
|| !current_driver_name
[0])
169 /* driver filter on but not yet initialized */
170 drv
= get_driver(dev
->driver
);
174 /* lock to protect against change of current_driver_name */
175 read_lock_irqsave(&driver_name_lock
, flags
);
179 strncmp(current_driver_name
, drv
->name
, NAME_MAX_LEN
- 1) == 0) {
180 current_driver
= drv
;
184 read_unlock_irqrestore(&driver_name_lock
, flags
);
190 #define err_printk(dev, entry, format, arg...) do { \
192 if (driver_filter(dev) && \
193 (show_all_errors || show_num_errors > 0)) { \
194 WARN(1, "%s %s: " format, \
195 dev ? dev_driver_string(dev) : "NULL", \
196 dev ? dev_name(dev) : "NULL", ## arg); \
197 dump_entry_trace(entry); \
199 if (!show_all_errors && show_num_errors > 0) \
200 show_num_errors -= 1; \
204 * Hash related functions
206 * Every DMA-API request is saved into a struct dma_debug_entry. To
207 * have quick access to these structs they are stored into a hash.
209 static int hash_fn(struct dma_debug_entry
*entry
)
212 * Hash function is based on the dma address.
213 * We use bits 20-27 here as the index into the hash
215 return (entry
->dev_addr
>> HASH_FN_SHIFT
) & HASH_FN_MASK
;
219 * Request exclusive access to a hash bucket for a given dma_debug_entry.
221 static struct hash_bucket
*get_hash_bucket(struct dma_debug_entry
*entry
,
222 unsigned long *flags
)
224 int idx
= hash_fn(entry
);
225 unsigned long __flags
;
227 spin_lock_irqsave(&dma_entry_hash
[idx
].lock
, __flags
);
229 return &dma_entry_hash
[idx
];
233 * Give up exclusive access to the hash bucket
235 static void put_hash_bucket(struct hash_bucket
*bucket
,
236 unsigned long *flags
)
238 unsigned long __flags
= *flags
;
240 spin_unlock_irqrestore(&bucket
->lock
, __flags
);
244 * Search a given entry in the hash bucket list
246 static struct dma_debug_entry
*hash_bucket_find(struct hash_bucket
*bucket
,
247 struct dma_debug_entry
*ref
)
249 struct dma_debug_entry
*entry
, *ret
= NULL
;
250 int matches
= 0, match_lvl
, last_lvl
= 0;
252 list_for_each_entry(entry
, &bucket
->list
, list
) {
253 if ((entry
->dev_addr
!= ref
->dev_addr
) ||
254 (entry
->dev
!= ref
->dev
))
258 * Some drivers map the same physical address multiple
259 * times. Without a hardware IOMMU this results in the
260 * same device addresses being put into the dma-debug
261 * hash multiple times too. This can result in false
262 * positives being reported. Therfore we implement a
263 * best-fit algorithm here which returns the entry from
264 * the hash which fits best to the reference value
265 * instead of the first-fit.
269 entry
->size
== ref
->size
? ++match_lvl
: 0;
270 entry
->type
== ref
->type
? ++match_lvl
: 0;
271 entry
->direction
== ref
->direction
? ++match_lvl
: 0;
272 entry
->sg_call_ents
== ref
->sg_call_ents
? ++match_lvl
: 0;
274 if (match_lvl
== 4) {
275 /* perfect-fit - return the result */
277 } else if (match_lvl
> last_lvl
) {
279 * We found an entry that fits better then the
282 last_lvl
= match_lvl
;
288 * If we have multiple matches but no perfect-fit, just return
291 ret
= (matches
== 1) ? ret
: NULL
;
297 * Add an entry to a hash bucket
299 static void hash_bucket_add(struct hash_bucket
*bucket
,
300 struct dma_debug_entry
*entry
)
302 list_add_tail(&entry
->list
, &bucket
->list
);
306 * Remove entry from a hash bucket list
308 static void hash_bucket_del(struct dma_debug_entry
*entry
)
310 list_del(&entry
->list
);
314 * Dump mapping entries for debugging purposes
316 void debug_dma_dump_mappings(struct device
*dev
)
320 for (idx
= 0; idx
< HASH_SIZE
; idx
++) {
321 struct hash_bucket
*bucket
= &dma_entry_hash
[idx
];
322 struct dma_debug_entry
*entry
;
325 spin_lock_irqsave(&bucket
->lock
, flags
);
327 list_for_each_entry(entry
, &bucket
->list
, list
) {
328 if (!dev
|| dev
== entry
->dev
) {
330 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
331 type2name
[entry
->type
], idx
,
332 (unsigned long long)entry
->paddr
,
333 entry
->dev_addr
, entry
->size
,
334 dir2name
[entry
->direction
]);
338 spin_unlock_irqrestore(&bucket
->lock
, flags
);
341 EXPORT_SYMBOL(debug_dma_dump_mappings
);
344 * Wrapper function for adding an entry to the hash.
345 * This function takes care of locking itself.
347 static void add_dma_entry(struct dma_debug_entry
*entry
)
349 struct hash_bucket
*bucket
;
352 bucket
= get_hash_bucket(entry
, &flags
);
353 hash_bucket_add(bucket
, entry
);
354 put_hash_bucket(bucket
, &flags
);
357 static struct dma_debug_entry
*__dma_entry_alloc(void)
359 struct dma_debug_entry
*entry
;
361 entry
= list_entry(free_entries
.next
, struct dma_debug_entry
, list
);
362 list_del(&entry
->list
);
363 memset(entry
, 0, sizeof(*entry
));
365 num_free_entries
-= 1;
366 if (num_free_entries
< min_free_entries
)
367 min_free_entries
= num_free_entries
;
372 /* struct dma_entry allocator
374 * The next two functions implement the allocator for
375 * struct dma_debug_entries.
377 static struct dma_debug_entry
*dma_entry_alloc(void)
379 struct dma_debug_entry
*entry
= NULL
;
382 spin_lock_irqsave(&free_entries_lock
, flags
);
384 if (list_empty(&free_entries
)) {
385 pr_err("DMA-API: debugging out of memory - disabling\n");
386 global_disable
= true;
390 entry
= __dma_entry_alloc();
392 #ifdef CONFIG_STACKTRACE
393 entry
->stacktrace
.max_entries
= DMA_DEBUG_STACKTRACE_ENTRIES
;
394 entry
->stacktrace
.entries
= entry
->st_entries
;
395 entry
->stacktrace
.skip
= 2;
396 save_stack_trace(&entry
->stacktrace
);
400 spin_unlock_irqrestore(&free_entries_lock
, flags
);
405 static void dma_entry_free(struct dma_debug_entry
*entry
)
410 * add to beginning of the list - this way the entries are
411 * more likely cache hot when they are reallocated.
413 spin_lock_irqsave(&free_entries_lock
, flags
);
414 list_add(&entry
->list
, &free_entries
);
415 num_free_entries
+= 1;
416 spin_unlock_irqrestore(&free_entries_lock
, flags
);
419 int dma_debug_resize_entries(u32 num_entries
)
421 int i
, delta
, ret
= 0;
423 struct dma_debug_entry
*entry
;
426 spin_lock_irqsave(&free_entries_lock
, flags
);
428 if (nr_total_entries
< num_entries
) {
429 delta
= num_entries
- nr_total_entries
;
431 spin_unlock_irqrestore(&free_entries_lock
, flags
);
433 for (i
= 0; i
< delta
; i
++) {
434 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
438 list_add_tail(&entry
->list
, &tmp
);
441 spin_lock_irqsave(&free_entries_lock
, flags
);
443 list_splice(&tmp
, &free_entries
);
444 nr_total_entries
+= i
;
445 num_free_entries
+= i
;
447 delta
= nr_total_entries
- num_entries
;
449 for (i
= 0; i
< delta
&& !list_empty(&free_entries
); i
++) {
450 entry
= __dma_entry_alloc();
454 nr_total_entries
-= i
;
457 if (nr_total_entries
!= num_entries
)
460 spin_unlock_irqrestore(&free_entries_lock
, flags
);
464 EXPORT_SYMBOL(dma_debug_resize_entries
);
467 * DMA-API debugging init code
469 * The init code does two things:
470 * 1. Initialize core data structures
471 * 2. Preallocate a given number of dma_debug_entry structs
474 static int prealloc_memory(u32 num_entries
)
476 struct dma_debug_entry
*entry
, *next_entry
;
479 for (i
= 0; i
< num_entries
; ++i
) {
480 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
484 list_add_tail(&entry
->list
, &free_entries
);
487 num_free_entries
= num_entries
;
488 min_free_entries
= num_entries
;
490 pr_info("DMA-API: preallocated %d debug entries\n", num_entries
);
496 list_for_each_entry_safe(entry
, next_entry
, &free_entries
, list
) {
497 list_del(&entry
->list
);
504 static ssize_t
filter_read(struct file
*file
, char __user
*user_buf
,
505 size_t count
, loff_t
*ppos
)
507 char buf
[NAME_MAX_LEN
+ 1];
511 if (!current_driver_name
[0])
515 * We can't copy to userspace directly because current_driver_name can
516 * only be read under the driver_name_lock with irqs disabled. So
517 * create a temporary copy first.
519 read_lock_irqsave(&driver_name_lock
, flags
);
520 len
= scnprintf(buf
, NAME_MAX_LEN
+ 1, "%s\n", current_driver_name
);
521 read_unlock_irqrestore(&driver_name_lock
, flags
);
523 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
526 static ssize_t
filter_write(struct file
*file
, const char __user
*userbuf
,
527 size_t count
, loff_t
*ppos
)
529 char buf
[NAME_MAX_LEN
];
535 * We can't copy from userspace directly. Access to
536 * current_driver_name is protected with a write_lock with irqs
537 * disabled. Since copy_from_user can fault and may sleep we
538 * need to copy to temporary buffer first
540 len
= min(count
, (size_t)(NAME_MAX_LEN
- 1));
541 if (copy_from_user(buf
, userbuf
, len
))
546 write_lock_irqsave(&driver_name_lock
, flags
);
549 * Now handle the string we got from userspace very carefully.
551 * - only use the first token we got
552 * - token delimiter is everything looking like a space
553 * character (' ', '\n', '\t' ...)
556 if (!isalnum(buf
[0])) {
558 * If the first character userspace gave us is not
559 * alphanumerical then assume the filter should be
562 if (current_driver_name
[0])
563 pr_info("DMA-API: switching off dma-debug driver filter\n");
564 current_driver_name
[0] = 0;
565 current_driver
= NULL
;
570 * Now parse out the first token and use it as the name for the
571 * driver to filter for.
573 for (i
= 0; i
< NAME_MAX_LEN
; ++i
) {
574 current_driver_name
[i
] = buf
[i
];
575 if (isspace(buf
[i
]) || buf
[i
] == ' ' || buf
[i
] == 0)
578 current_driver_name
[i
] = 0;
579 current_driver
= NULL
;
581 pr_info("DMA-API: enable driver filter for driver [%s]\n",
582 current_driver_name
);
585 write_unlock_irqrestore(&driver_name_lock
, flags
);
590 const struct file_operations filter_fops
= {
592 .write
= filter_write
,
595 static int dma_debug_fs_init(void)
597 dma_debug_dent
= debugfs_create_dir("dma-api", NULL
);
598 if (!dma_debug_dent
) {
599 pr_err("DMA-API: can not create debugfs directory\n");
603 global_disable_dent
= debugfs_create_bool("disabled", 0444,
605 (u32
*)&global_disable
);
606 if (!global_disable_dent
)
609 error_count_dent
= debugfs_create_u32("error_count", 0444,
610 dma_debug_dent
, &error_count
);
611 if (!error_count_dent
)
614 show_all_errors_dent
= debugfs_create_u32("all_errors", 0644,
617 if (!show_all_errors_dent
)
620 show_num_errors_dent
= debugfs_create_u32("num_errors", 0644,
623 if (!show_num_errors_dent
)
626 num_free_entries_dent
= debugfs_create_u32("num_free_entries", 0444,
629 if (!num_free_entries_dent
)
632 min_free_entries_dent
= debugfs_create_u32("min_free_entries", 0444,
635 if (!min_free_entries_dent
)
638 filter_dent
= debugfs_create_file("driver_filter", 0644,
639 dma_debug_dent
, NULL
, &filter_fops
);
646 debugfs_remove_recursive(dma_debug_dent
);
651 static int device_dma_allocations(struct device
*dev
)
653 struct dma_debug_entry
*entry
;
657 local_irq_save(flags
);
659 for (i
= 0; i
< HASH_SIZE
; ++i
) {
660 spin_lock(&dma_entry_hash
[i
].lock
);
661 list_for_each_entry(entry
, &dma_entry_hash
[i
].list
, list
) {
662 if (entry
->dev
== dev
)
665 spin_unlock(&dma_entry_hash
[i
].lock
);
668 local_irq_restore(flags
);
673 static int dma_debug_device_change(struct notifier_block
*nb
,
674 unsigned long action
, void *data
)
676 struct device
*dev
= data
;
681 case BUS_NOTIFY_UNBOUND_DRIVER
:
682 count
= device_dma_allocations(dev
);
685 err_printk(dev
, NULL
, "DMA-API: device driver has pending "
686 "DMA allocations while released from device "
687 "[count=%d]\n", count
);
696 void dma_debug_add_bus(struct bus_type
*bus
)
698 struct notifier_block
*nb
;
700 nb
= kzalloc(sizeof(struct notifier_block
), GFP_KERNEL
);
702 pr_err("dma_debug_add_bus: out of memory\n");
706 nb
->notifier_call
= dma_debug_device_change
;
708 bus_register_notifier(bus
, nb
);
712 * Let the architectures decide how many entries should be preallocated.
714 void dma_debug_init(u32 num_entries
)
721 for (i
= 0; i
< HASH_SIZE
; ++i
) {
722 INIT_LIST_HEAD(&dma_entry_hash
[i
].list
);
723 spin_lock_init(&dma_entry_hash
[i
].lock
);
726 if (dma_debug_fs_init() != 0) {
727 pr_err("DMA-API: error creating debugfs entries - disabling\n");
728 global_disable
= true;
734 num_entries
= req_entries
;
736 if (prealloc_memory(num_entries
) != 0) {
737 pr_err("DMA-API: debugging out of memory error - disabled\n");
738 global_disable
= true;
743 nr_total_entries
= num_free_entries
;
745 pr_info("DMA-API: debugging enabled by kernel config\n");
748 static __init
int dma_debug_cmdline(char *str
)
753 if (strncmp(str
, "off", 3) == 0) {
754 pr_info("DMA-API: debugging disabled on kernel command line\n");
755 global_disable
= true;
761 static __init
int dma_debug_entries_cmdline(char *str
)
768 res
= get_option(&str
, &req_entries
);
776 __setup("dma_debug=", dma_debug_cmdline
);
777 __setup("dma_debug_entries=", dma_debug_entries_cmdline
);
779 static void check_unmap(struct dma_debug_entry
*ref
)
781 struct dma_debug_entry
*entry
;
782 struct hash_bucket
*bucket
;
785 if (dma_mapping_error(ref
->dev
, ref
->dev_addr
)) {
786 err_printk(ref
->dev
, NULL
, "DMA-API: device driver tries "
787 "to free an invalid DMA memory address\n");
791 bucket
= get_hash_bucket(ref
, &flags
);
792 entry
= hash_bucket_find(bucket
, ref
);
795 err_printk(ref
->dev
, NULL
, "DMA-API: device driver tries "
796 "to free DMA memory it has not allocated "
797 "[device address=0x%016llx] [size=%llu bytes]\n",
798 ref
->dev_addr
, ref
->size
);
802 if (ref
->size
!= entry
->size
) {
803 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
804 "DMA memory with different size "
805 "[device address=0x%016llx] [map size=%llu bytes] "
806 "[unmap size=%llu bytes]\n",
807 ref
->dev_addr
, entry
->size
, ref
->size
);
810 if (ref
->type
!= entry
->type
) {
811 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
812 "DMA memory with wrong function "
813 "[device address=0x%016llx] [size=%llu bytes] "
814 "[mapped as %s] [unmapped as %s]\n",
815 ref
->dev_addr
, ref
->size
,
816 type2name
[entry
->type
], type2name
[ref
->type
]);
817 } else if ((entry
->type
== dma_debug_coherent
) &&
818 (ref
->paddr
!= entry
->paddr
)) {
819 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
820 "DMA memory with different CPU address "
821 "[device address=0x%016llx] [size=%llu bytes] "
822 "[cpu alloc address=%p] [cpu free address=%p]",
823 ref
->dev_addr
, ref
->size
,
824 (void *)entry
->paddr
, (void *)ref
->paddr
);
827 if (ref
->sg_call_ents
&& ref
->type
== dma_debug_sg
&&
828 ref
->sg_call_ents
!= entry
->sg_call_ents
) {
829 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
830 "DMA sg list with different entry count "
831 "[map count=%d] [unmap count=%d]\n",
832 entry
->sg_call_ents
, ref
->sg_call_ents
);
836 * This may be no bug in reality - but most implementations of the
837 * DMA API don't handle this properly, so check for it here
839 if (ref
->direction
!= entry
->direction
) {
840 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
841 "DMA memory with different direction "
842 "[device address=0x%016llx] [size=%llu bytes] "
843 "[mapped with %s] [unmapped with %s]\n",
844 ref
->dev_addr
, ref
->size
,
845 dir2name
[entry
->direction
],
846 dir2name
[ref
->direction
]);
849 hash_bucket_del(entry
);
850 dma_entry_free(entry
);
853 put_hash_bucket(bucket
, &flags
);
856 static void check_for_stack(struct device
*dev
, void *addr
)
858 if (object_is_on_stack(addr
))
859 err_printk(dev
, NULL
, "DMA-API: device driver maps memory from"
860 "stack [addr=%p]\n", addr
);
863 static inline bool overlap(void *addr
, unsigned long len
, void *start
, void *end
)
865 unsigned long a1
= (unsigned long)addr
;
866 unsigned long b1
= a1
+ len
;
867 unsigned long a2
= (unsigned long)start
;
868 unsigned long b2
= (unsigned long)end
;
870 return !(b1
<= a2
|| a1
>= b2
);
873 static void check_for_illegal_area(struct device
*dev
, void *addr
, unsigned long len
)
875 if (overlap(addr
, len
, _text
, _etext
) ||
876 overlap(addr
, len
, __start_rodata
, __end_rodata
))
877 err_printk(dev
, NULL
, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr
, len
);
880 static void check_sync(struct device
*dev
,
881 struct dma_debug_entry
*ref
,
884 struct dma_debug_entry
*entry
;
885 struct hash_bucket
*bucket
;
888 bucket
= get_hash_bucket(ref
, &flags
);
890 entry
= hash_bucket_find(bucket
, ref
);
893 err_printk(dev
, NULL
, "DMA-API: device driver tries "
894 "to sync DMA memory it has not allocated "
895 "[device address=0x%016llx] [size=%llu bytes]\n",
896 (unsigned long long)ref
->dev_addr
, ref
->size
);
900 if (ref
->size
> entry
->size
) {
901 err_printk(dev
, entry
, "DMA-API: device driver syncs"
902 " DMA memory outside allocated range "
903 "[device address=0x%016llx] "
904 "[allocation size=%llu bytes] "
905 "[sync offset+size=%llu]\n",
906 entry
->dev_addr
, entry
->size
,
910 if (entry
->direction
== DMA_BIDIRECTIONAL
)
913 if (ref
->direction
!= entry
->direction
) {
914 err_printk(dev
, entry
, "DMA-API: device driver syncs "
915 "DMA memory with different direction "
916 "[device address=0x%016llx] [size=%llu bytes] "
917 "[mapped with %s] [synced with %s]\n",
918 (unsigned long long)ref
->dev_addr
, entry
->size
,
919 dir2name
[entry
->direction
],
920 dir2name
[ref
->direction
]);
923 if (to_cpu
&& !(entry
->direction
== DMA_FROM_DEVICE
) &&
924 !(ref
->direction
== DMA_TO_DEVICE
))
925 err_printk(dev
, entry
, "DMA-API: device driver syncs "
926 "device read-only DMA memory for cpu "
927 "[device address=0x%016llx] [size=%llu bytes] "
928 "[mapped with %s] [synced with %s]\n",
929 (unsigned long long)ref
->dev_addr
, entry
->size
,
930 dir2name
[entry
->direction
],
931 dir2name
[ref
->direction
]);
933 if (!to_cpu
&& !(entry
->direction
== DMA_TO_DEVICE
) &&
934 !(ref
->direction
== DMA_FROM_DEVICE
))
935 err_printk(dev
, entry
, "DMA-API: device driver syncs "
936 "device write-only DMA memory to device "
937 "[device address=0x%016llx] [size=%llu bytes] "
938 "[mapped with %s] [synced with %s]\n",
939 (unsigned long long)ref
->dev_addr
, entry
->size
,
940 dir2name
[entry
->direction
],
941 dir2name
[ref
->direction
]);
944 put_hash_bucket(bucket
, &flags
);
947 void debug_dma_map_page(struct device
*dev
, struct page
*page
, size_t offset
,
948 size_t size
, int direction
, dma_addr_t dma_addr
,
951 struct dma_debug_entry
*entry
;
953 if (unlikely(global_disable
))
956 if (unlikely(dma_mapping_error(dev
, dma_addr
)))
959 entry
= dma_entry_alloc();
964 entry
->type
= dma_debug_page
;
965 entry
->paddr
= page_to_phys(page
) + offset
;
966 entry
->dev_addr
= dma_addr
;
968 entry
->direction
= direction
;
971 entry
->type
= dma_debug_single
;
973 if (!PageHighMem(page
)) {
974 void *addr
= page_address(page
) + offset
;
976 check_for_stack(dev
, addr
);
977 check_for_illegal_area(dev
, addr
, size
);
980 add_dma_entry(entry
);
982 EXPORT_SYMBOL(debug_dma_map_page
);
984 void debug_dma_unmap_page(struct device
*dev
, dma_addr_t addr
,
985 size_t size
, int direction
, bool map_single
)
987 struct dma_debug_entry ref
= {
988 .type
= dma_debug_page
,
992 .direction
= direction
,
995 if (unlikely(global_disable
))
999 ref
.type
= dma_debug_single
;
1003 EXPORT_SYMBOL(debug_dma_unmap_page
);
1005 void debug_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
1006 int nents
, int mapped_ents
, int direction
)
1008 struct dma_debug_entry
*entry
;
1009 struct scatterlist
*s
;
1012 if (unlikely(global_disable
))
1015 for_each_sg(sg
, s
, mapped_ents
, i
) {
1016 entry
= dma_entry_alloc();
1020 entry
->type
= dma_debug_sg
;
1022 entry
->paddr
= sg_phys(s
);
1023 entry
->size
= sg_dma_len(s
);
1024 entry
->dev_addr
= sg_dma_address(s
);
1025 entry
->direction
= direction
;
1026 entry
->sg_call_ents
= nents
;
1027 entry
->sg_mapped_ents
= mapped_ents
;
1029 if (!PageHighMem(sg_page(s
))) {
1030 check_for_stack(dev
, sg_virt(s
));
1031 check_for_illegal_area(dev
, sg_virt(s
), sg_dma_len(s
));
1034 add_dma_entry(entry
);
1037 EXPORT_SYMBOL(debug_dma_map_sg
);
1039 static int get_nr_mapped_entries(struct device
*dev
,
1040 struct dma_debug_entry
*ref
)
1042 struct dma_debug_entry
*entry
;
1043 struct hash_bucket
*bucket
;
1044 unsigned long flags
;
1047 bucket
= get_hash_bucket(ref
, &flags
);
1048 entry
= hash_bucket_find(bucket
, ref
);
1052 mapped_ents
= entry
->sg_mapped_ents
;
1053 put_hash_bucket(bucket
, &flags
);
1058 void debug_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
1059 int nelems
, int dir
)
1061 struct scatterlist
*s
;
1062 int mapped_ents
= 0, i
;
1064 if (unlikely(global_disable
))
1067 for_each_sg(sglist
, s
, nelems
, i
) {
1069 struct dma_debug_entry ref
= {
1070 .type
= dma_debug_sg
,
1072 .paddr
= sg_phys(s
),
1073 .dev_addr
= sg_dma_address(s
),
1074 .size
= sg_dma_len(s
),
1076 .sg_call_ents
= nelems
,
1079 if (mapped_ents
&& i
>= mapped_ents
)
1083 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1088 EXPORT_SYMBOL(debug_dma_unmap_sg
);
1090 void debug_dma_alloc_coherent(struct device
*dev
, size_t size
,
1091 dma_addr_t dma_addr
, void *virt
)
1093 struct dma_debug_entry
*entry
;
1095 if (unlikely(global_disable
))
1098 if (unlikely(virt
== NULL
))
1101 entry
= dma_entry_alloc();
1105 entry
->type
= dma_debug_coherent
;
1107 entry
->paddr
= virt_to_phys(virt
);
1109 entry
->dev_addr
= dma_addr
;
1110 entry
->direction
= DMA_BIDIRECTIONAL
;
1112 add_dma_entry(entry
);
1114 EXPORT_SYMBOL(debug_dma_alloc_coherent
);
1116 void debug_dma_free_coherent(struct device
*dev
, size_t size
,
1117 void *virt
, dma_addr_t addr
)
1119 struct dma_debug_entry ref
= {
1120 .type
= dma_debug_coherent
,
1122 .paddr
= virt_to_phys(virt
),
1125 .direction
= DMA_BIDIRECTIONAL
,
1128 if (unlikely(global_disable
))
1133 EXPORT_SYMBOL(debug_dma_free_coherent
);
1135 void debug_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
1136 size_t size
, int direction
)
1138 struct dma_debug_entry ref
;
1140 if (unlikely(global_disable
))
1143 ref
.type
= dma_debug_single
;
1145 ref
.dev_addr
= dma_handle
;
1147 ref
.direction
= direction
;
1148 ref
.sg_call_ents
= 0;
1150 check_sync(dev
, &ref
, true);
1152 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu
);
1154 void debug_dma_sync_single_for_device(struct device
*dev
,
1155 dma_addr_t dma_handle
, size_t size
,
1158 struct dma_debug_entry ref
;
1160 if (unlikely(global_disable
))
1163 ref
.type
= dma_debug_single
;
1165 ref
.dev_addr
= dma_handle
;
1167 ref
.direction
= direction
;
1168 ref
.sg_call_ents
= 0;
1170 check_sync(dev
, &ref
, false);
1172 EXPORT_SYMBOL(debug_dma_sync_single_for_device
);
1174 void debug_dma_sync_single_range_for_cpu(struct device
*dev
,
1175 dma_addr_t dma_handle
,
1176 unsigned long offset
, size_t size
,
1179 struct dma_debug_entry ref
;
1181 if (unlikely(global_disable
))
1184 ref
.type
= dma_debug_single
;
1186 ref
.dev_addr
= dma_handle
;
1187 ref
.size
= offset
+ size
;
1188 ref
.direction
= direction
;
1189 ref
.sg_call_ents
= 0;
1191 check_sync(dev
, &ref
, true);
1193 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu
);
1195 void debug_dma_sync_single_range_for_device(struct device
*dev
,
1196 dma_addr_t dma_handle
,
1197 unsigned long offset
,
1198 size_t size
, int direction
)
1200 struct dma_debug_entry ref
;
1202 if (unlikely(global_disable
))
1205 ref
.type
= dma_debug_single
;
1207 ref
.dev_addr
= dma_handle
;
1208 ref
.size
= offset
+ size
;
1209 ref
.direction
= direction
;
1210 ref
.sg_call_ents
= 0;
1212 check_sync(dev
, &ref
, false);
1214 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device
);
1216 void debug_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
1217 int nelems
, int direction
)
1219 struct scatterlist
*s
;
1220 int mapped_ents
= 0, i
;
1222 if (unlikely(global_disable
))
1225 for_each_sg(sg
, s
, nelems
, i
) {
1227 struct dma_debug_entry ref
= {
1228 .type
= dma_debug_sg
,
1230 .paddr
= sg_phys(s
),
1231 .dev_addr
= sg_dma_address(s
),
1232 .size
= sg_dma_len(s
),
1233 .direction
= direction
,
1234 .sg_call_ents
= nelems
,
1238 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1240 if (i
>= mapped_ents
)
1243 check_sync(dev
, &ref
, true);
1246 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu
);
1248 void debug_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
1249 int nelems
, int direction
)
1251 struct scatterlist
*s
;
1252 int mapped_ents
= 0, i
;
1254 if (unlikely(global_disable
))
1257 for_each_sg(sg
, s
, nelems
, i
) {
1259 struct dma_debug_entry ref
= {
1260 .type
= dma_debug_sg
,
1262 .paddr
= sg_phys(s
),
1263 .dev_addr
= sg_dma_address(s
),
1264 .size
= sg_dma_len(s
),
1265 .direction
= direction
,
1266 .sg_call_ents
= nelems
,
1269 mapped_ents
= get_nr_mapped_entries(dev
, &ref
);
1271 if (i
>= mapped_ents
)
1274 check_sync(dev
, &ref
, false);
1277 EXPORT_SYMBOL(debug_dma_sync_sg_for_device
);
1279 static int __init
dma_debug_driver_setup(char *str
)
1283 for (i
= 0; i
< NAME_MAX_LEN
- 1; ++i
, ++str
) {
1284 current_driver_name
[i
] = *str
;
1289 if (current_driver_name
[0])
1290 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1291 current_driver_name
);
1296 __setup("dma_debug_driver=", dma_debug_driver_setup
);