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)
45 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
47 struct dma_debug_entry
{
48 struct list_head list
;
57 #ifdef CONFIG_STACKTRACE
58 struct stack_trace stacktrace
;
59 unsigned long st_entries
[DMA_DEBUG_STACKTRACE_ENTRIES
];
64 struct list_head list
;
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 static const char *type2name
[4] = { "single", "page",
103 "scather-gather", "coherent" };
105 static const char *dir2name
[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
106 "DMA_FROM_DEVICE", "DMA_NONE" };
108 /* little merge helper - remove it after the merge window */
109 #ifndef BUS_NOTIFY_UNBOUND_DRIVER
110 #define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
114 * The access to some variables in this macro is racy. We can't use atomic_t
115 * here because all these variables are exported to debugfs. Some of them even
116 * writeable. This is also the reason why a lock won't help much. But anyway,
117 * the races are no big deal. Here is why:
119 * error_count: the addition is racy, but the worst thing that can happen is
120 * that we don't count some errors
121 * show_num_errors: the subtraction is racy. Also no big deal because in
122 * worst case this will result in one warning more in the
123 * system log than the user configured. This variable is
124 * writeable via debugfs.
126 static inline void dump_entry_trace(struct dma_debug_entry
*entry
)
128 #ifdef CONFIG_STACKTRACE
130 printk(KERN_WARNING
"Mapped at:\n");
131 print_stack_trace(&entry
->stacktrace
, 0);
136 #define err_printk(dev, entry, format, arg...) do { \
138 if (show_all_errors || show_num_errors > 0) { \
139 WARN(1, "%s %s: " format, \
140 dev_driver_string(dev), \
141 dev_name(dev) , ## arg); \
142 dump_entry_trace(entry); \
144 if (!show_all_errors && show_num_errors > 0) \
145 show_num_errors -= 1; \
149 * Hash related functions
151 * Every DMA-API request is saved into a struct dma_debug_entry. To
152 * have quick access to these structs they are stored into a hash.
154 static int hash_fn(struct dma_debug_entry
*entry
)
157 * Hash function is based on the dma address.
158 * We use bits 20-27 here as the index into the hash
160 return (entry
->dev_addr
>> HASH_FN_SHIFT
) & HASH_FN_MASK
;
164 * Request exclusive access to a hash bucket for a given dma_debug_entry.
166 static struct hash_bucket
*get_hash_bucket(struct dma_debug_entry
*entry
,
167 unsigned long *flags
)
169 int idx
= hash_fn(entry
);
170 unsigned long __flags
;
172 spin_lock_irqsave(&dma_entry_hash
[idx
].lock
, __flags
);
174 return &dma_entry_hash
[idx
];
178 * Give up exclusive access to the hash bucket
180 static void put_hash_bucket(struct hash_bucket
*bucket
,
181 unsigned long *flags
)
183 unsigned long __flags
= *flags
;
185 spin_unlock_irqrestore(&bucket
->lock
, __flags
);
189 * Search a given entry in the hash bucket list
191 static struct dma_debug_entry
*hash_bucket_find(struct hash_bucket
*bucket
,
192 struct dma_debug_entry
*ref
)
194 struct dma_debug_entry
*entry
;
196 list_for_each_entry(entry
, &bucket
->list
, list
) {
197 if ((entry
->dev_addr
== ref
->dev_addr
) &&
198 (entry
->dev
== ref
->dev
))
206 * Add an entry to a hash bucket
208 static void hash_bucket_add(struct hash_bucket
*bucket
,
209 struct dma_debug_entry
*entry
)
211 list_add_tail(&entry
->list
, &bucket
->list
);
215 * Remove entry from a hash bucket list
217 static void hash_bucket_del(struct dma_debug_entry
*entry
)
219 list_del(&entry
->list
);
223 * Dump mapping entries for debugging purposes
225 void debug_dma_dump_mappings(struct device
*dev
)
229 for (idx
= 0; idx
< HASH_SIZE
; idx
++) {
230 struct hash_bucket
*bucket
= &dma_entry_hash
[idx
];
231 struct dma_debug_entry
*entry
;
234 spin_lock_irqsave(&bucket
->lock
, flags
);
236 list_for_each_entry(entry
, &bucket
->list
, list
) {
237 if (!dev
|| dev
== entry
->dev
) {
239 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
240 type2name
[entry
->type
], idx
,
241 (unsigned long long)entry
->paddr
,
242 entry
->dev_addr
, entry
->size
,
243 dir2name
[entry
->direction
]);
247 spin_unlock_irqrestore(&bucket
->lock
, flags
);
250 EXPORT_SYMBOL(debug_dma_dump_mappings
);
253 * Wrapper function for adding an entry to the hash.
254 * This function takes care of locking itself.
256 static void add_dma_entry(struct dma_debug_entry
*entry
)
258 struct hash_bucket
*bucket
;
261 bucket
= get_hash_bucket(entry
, &flags
);
262 hash_bucket_add(bucket
, entry
);
263 put_hash_bucket(bucket
, &flags
);
266 static struct dma_debug_entry
*__dma_entry_alloc(void)
268 struct dma_debug_entry
*entry
;
270 entry
= list_entry(free_entries
.next
, struct dma_debug_entry
, list
);
271 list_del(&entry
->list
);
272 memset(entry
, 0, sizeof(*entry
));
274 num_free_entries
-= 1;
275 if (num_free_entries
< min_free_entries
)
276 min_free_entries
= num_free_entries
;
281 /* struct dma_entry allocator
283 * The next two functions implement the allocator for
284 * struct dma_debug_entries.
286 static struct dma_debug_entry
*dma_entry_alloc(void)
288 struct dma_debug_entry
*entry
= NULL
;
291 spin_lock_irqsave(&free_entries_lock
, flags
);
293 if (list_empty(&free_entries
)) {
294 printk(KERN_ERR
"DMA-API: debugging out of memory "
296 global_disable
= true;
300 entry
= __dma_entry_alloc();
302 #ifdef CONFIG_STACKTRACE
303 entry
->stacktrace
.max_entries
= DMA_DEBUG_STACKTRACE_ENTRIES
;
304 entry
->stacktrace
.entries
= entry
->st_entries
;
305 entry
->stacktrace
.skip
= 2;
306 save_stack_trace(&entry
->stacktrace
);
310 spin_unlock_irqrestore(&free_entries_lock
, flags
);
315 static void dma_entry_free(struct dma_debug_entry
*entry
)
320 * add to beginning of the list - this way the entries are
321 * more likely cache hot when they are reallocated.
323 spin_lock_irqsave(&free_entries_lock
, flags
);
324 list_add(&entry
->list
, &free_entries
);
325 num_free_entries
+= 1;
326 spin_unlock_irqrestore(&free_entries_lock
, flags
);
329 int dma_debug_resize_entries(u32 num_entries
)
331 int i
, delta
, ret
= 0;
333 struct dma_debug_entry
*entry
;
336 spin_lock_irqsave(&free_entries_lock
, flags
);
338 if (nr_total_entries
< num_entries
) {
339 delta
= num_entries
- nr_total_entries
;
341 spin_unlock_irqrestore(&free_entries_lock
, flags
);
343 for (i
= 0; i
< delta
; i
++) {
344 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
348 list_add_tail(&entry
->list
, &tmp
);
351 spin_lock_irqsave(&free_entries_lock
, flags
);
353 list_splice(&tmp
, &free_entries
);
354 nr_total_entries
+= i
;
355 num_free_entries
+= i
;
357 delta
= nr_total_entries
- num_entries
;
359 for (i
= 0; i
< delta
&& !list_empty(&free_entries
); i
++) {
360 entry
= __dma_entry_alloc();
364 nr_total_entries
-= i
;
367 if (nr_total_entries
!= num_entries
)
370 spin_unlock_irqrestore(&free_entries_lock
, flags
);
374 EXPORT_SYMBOL(dma_debug_resize_entries
);
377 * DMA-API debugging init code
379 * The init code does two things:
380 * 1. Initialize core data structures
381 * 2. Preallocate a given number of dma_debug_entry structs
384 static int prealloc_memory(u32 num_entries
)
386 struct dma_debug_entry
*entry
, *next_entry
;
389 for (i
= 0; i
< num_entries
; ++i
) {
390 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
394 list_add_tail(&entry
->list
, &free_entries
);
397 num_free_entries
= num_entries
;
398 min_free_entries
= num_entries
;
400 printk(KERN_INFO
"DMA-API: preallocated %d debug entries\n",
407 list_for_each_entry_safe(entry
, next_entry
, &free_entries
, list
) {
408 list_del(&entry
->list
);
415 static int dma_debug_fs_init(void)
417 dma_debug_dent
= debugfs_create_dir("dma-api", NULL
);
418 if (!dma_debug_dent
) {
419 printk(KERN_ERR
"DMA-API: can not create debugfs directory\n");
423 global_disable_dent
= debugfs_create_bool("disabled", 0444,
425 (u32
*)&global_disable
);
426 if (!global_disable_dent
)
429 error_count_dent
= debugfs_create_u32("error_count", 0444,
430 dma_debug_dent
, &error_count
);
431 if (!error_count_dent
)
434 show_all_errors_dent
= debugfs_create_u32("all_errors", 0644,
437 if (!show_all_errors_dent
)
440 show_num_errors_dent
= debugfs_create_u32("num_errors", 0644,
443 if (!show_num_errors_dent
)
446 num_free_entries_dent
= debugfs_create_u32("num_free_entries", 0444,
449 if (!num_free_entries_dent
)
452 min_free_entries_dent
= debugfs_create_u32("min_free_entries", 0444,
455 if (!min_free_entries_dent
)
461 debugfs_remove_recursive(dma_debug_dent
);
466 static int device_dma_allocations(struct device
*dev
)
468 struct dma_debug_entry
*entry
;
472 for (i
= 0; i
< HASH_SIZE
; ++i
) {
473 spin_lock_irqsave(&dma_entry_hash
[i
].lock
, flags
);
474 list_for_each_entry(entry
, &dma_entry_hash
[i
].list
, list
) {
475 if (entry
->dev
== dev
)
478 spin_unlock_irqrestore(&dma_entry_hash
[i
].lock
, flags
);
484 static int dma_debug_device_change(struct notifier_block
*nb
,
485 unsigned long action
, void *data
)
487 struct device
*dev
= data
;
492 case BUS_NOTIFY_UNBOUND_DRIVER
:
493 count
= device_dma_allocations(dev
);
496 err_printk(dev
, NULL
, "DMA-API: device driver has pending "
497 "DMA allocations while released from device "
498 "[count=%d]\n", count
);
507 void dma_debug_add_bus(struct bus_type
*bus
)
509 struct notifier_block
*nb
;
511 nb
= kzalloc(sizeof(struct notifier_block
), GFP_KERNEL
);
513 printk(KERN_ERR
"dma_debug_add_bus: out of memory\n");
517 nb
->notifier_call
= dma_debug_device_change
;
519 bus_register_notifier(bus
, nb
);
523 * Let the architectures decide how many entries should be preallocated.
525 void dma_debug_init(u32 num_entries
)
532 for (i
= 0; i
< HASH_SIZE
; ++i
) {
533 INIT_LIST_HEAD(&dma_entry_hash
[i
].list
);
534 dma_entry_hash
[i
].lock
= SPIN_LOCK_UNLOCKED
;
537 if (dma_debug_fs_init() != 0) {
538 printk(KERN_ERR
"DMA-API: error creating debugfs entries "
540 global_disable
= true;
546 num_entries
= req_entries
;
548 if (prealloc_memory(num_entries
) != 0) {
549 printk(KERN_ERR
"DMA-API: debugging out of memory error "
551 global_disable
= true;
556 nr_total_entries
= num_free_entries
;
558 printk(KERN_INFO
"DMA-API: debugging enabled by kernel config\n");
561 static __init
int dma_debug_cmdline(char *str
)
566 if (strncmp(str
, "off", 3) == 0) {
567 printk(KERN_INFO
"DMA-API: debugging disabled on kernel "
569 global_disable
= true;
575 static __init
int dma_debug_entries_cmdline(char *str
)
582 res
= get_option(&str
, &req_entries
);
590 __setup("dma_debug=", dma_debug_cmdline
);
591 __setup("dma_debug_entries=", dma_debug_entries_cmdline
);
593 static void check_unmap(struct dma_debug_entry
*ref
)
595 struct dma_debug_entry
*entry
;
596 struct hash_bucket
*bucket
;
599 if (dma_mapping_error(ref
->dev
, ref
->dev_addr
)) {
600 err_printk(ref
->dev
, NULL
, "DMA-API: device driver tries "
601 "to free an invalid DMA memory address\n");
605 bucket
= get_hash_bucket(ref
, &flags
);
606 entry
= hash_bucket_find(bucket
, ref
);
609 err_printk(ref
->dev
, NULL
, "DMA-API: device driver tries "
610 "to free DMA memory it has not allocated "
611 "[device address=0x%016llx] [size=%llu bytes]\n",
612 ref
->dev_addr
, ref
->size
);
616 if (ref
->size
!= entry
->size
) {
617 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
618 "DMA memory with different size "
619 "[device address=0x%016llx] [map size=%llu bytes] "
620 "[unmap size=%llu bytes]\n",
621 ref
->dev_addr
, entry
->size
, ref
->size
);
624 if (ref
->type
!= entry
->type
) {
625 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
626 "DMA memory with wrong function "
627 "[device address=0x%016llx] [size=%llu bytes] "
628 "[mapped as %s] [unmapped as %s]\n",
629 ref
->dev_addr
, ref
->size
,
630 type2name
[entry
->type
], type2name
[ref
->type
]);
631 } else if ((entry
->type
== dma_debug_coherent
) &&
632 (ref
->paddr
!= entry
->paddr
)) {
633 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
634 "DMA memory with different CPU address "
635 "[device address=0x%016llx] [size=%llu bytes] "
636 "[cpu alloc address=%p] [cpu free address=%p]",
637 ref
->dev_addr
, ref
->size
,
638 (void *)entry
->paddr
, (void *)ref
->paddr
);
641 if (ref
->sg_call_ents
&& ref
->type
== dma_debug_sg
&&
642 ref
->sg_call_ents
!= entry
->sg_call_ents
) {
643 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
644 "DMA sg list with different entry count "
645 "[map count=%d] [unmap count=%d]\n",
646 entry
->sg_call_ents
, ref
->sg_call_ents
);
650 * This may be no bug in reality - but most implementations of the
651 * DMA API don't handle this properly, so check for it here
653 if (ref
->direction
!= entry
->direction
) {
654 err_printk(ref
->dev
, entry
, "DMA-API: device driver frees "
655 "DMA memory with different direction "
656 "[device address=0x%016llx] [size=%llu bytes] "
657 "[mapped with %s] [unmapped with %s]\n",
658 ref
->dev_addr
, ref
->size
,
659 dir2name
[entry
->direction
],
660 dir2name
[ref
->direction
]);
663 hash_bucket_del(entry
);
664 dma_entry_free(entry
);
667 put_hash_bucket(bucket
, &flags
);
670 static void check_for_stack(struct device
*dev
, void *addr
)
672 if (object_is_on_stack(addr
))
673 err_printk(dev
, NULL
, "DMA-API: device driver maps memory from"
674 "stack [addr=%p]\n", addr
);
677 static inline bool overlap(void *addr
, u64 size
, void *start
, void *end
)
679 void *addr2
= (char *)addr
+ size
;
681 return ((addr
>= start
&& addr
< end
) ||
682 (addr2
>= start
&& addr2
< end
) ||
683 ((addr
< start
) && (addr2
>= end
)));
686 static void check_for_illegal_area(struct device
*dev
, void *addr
, u64 size
)
688 if (overlap(addr
, size
, _text
, _etext
) ||
689 overlap(addr
, size
, __start_rodata
, __end_rodata
))
690 err_printk(dev
, NULL
, "DMA-API: device driver maps "
691 "memory from kernel text or rodata "
692 "[addr=%p] [size=%llu]\n", addr
, size
);
695 static void check_sync(struct device
*dev
, dma_addr_t addr
,
696 u64 size
, u64 offset
, int direction
, bool to_cpu
)
698 struct dma_debug_entry ref
= {
702 .direction
= direction
,
704 struct dma_debug_entry
*entry
;
705 struct hash_bucket
*bucket
;
708 bucket
= get_hash_bucket(&ref
, &flags
);
710 entry
= hash_bucket_find(bucket
, &ref
);
713 err_printk(dev
, NULL
, "DMA-API: device driver tries "
714 "to sync DMA memory it has not allocated "
715 "[device address=0x%016llx] [size=%llu bytes]\n",
716 (unsigned long long)addr
, size
);
720 if ((offset
+ size
) > entry
->size
) {
721 err_printk(dev
, entry
, "DMA-API: device driver syncs"
722 " DMA memory outside allocated range "
723 "[device address=0x%016llx] "
724 "[allocation size=%llu bytes] [sync offset=%llu] "
725 "[sync size=%llu]\n", entry
->dev_addr
, entry
->size
,
729 if (direction
!= entry
->direction
) {
730 err_printk(dev
, entry
, "DMA-API: device driver syncs "
731 "DMA memory with different direction "
732 "[device address=0x%016llx] [size=%llu bytes] "
733 "[mapped with %s] [synced with %s]\n",
734 (unsigned long long)addr
, entry
->size
,
735 dir2name
[entry
->direction
],
736 dir2name
[direction
]);
739 if (entry
->direction
== DMA_BIDIRECTIONAL
)
742 if (to_cpu
&& !(entry
->direction
== DMA_FROM_DEVICE
) &&
743 !(direction
== DMA_TO_DEVICE
))
744 err_printk(dev
, entry
, "DMA-API: device driver syncs "
745 "device read-only DMA memory for cpu "
746 "[device address=0x%016llx] [size=%llu bytes] "
747 "[mapped with %s] [synced with %s]\n",
748 (unsigned long long)addr
, entry
->size
,
749 dir2name
[entry
->direction
],
750 dir2name
[direction
]);
752 if (!to_cpu
&& !(entry
->direction
== DMA_TO_DEVICE
) &&
753 !(direction
== DMA_FROM_DEVICE
))
754 err_printk(dev
, entry
, "DMA-API: device driver syncs "
755 "device write-only DMA memory to device "
756 "[device address=0x%016llx] [size=%llu bytes] "
757 "[mapped with %s] [synced with %s]\n",
758 (unsigned long long)addr
, entry
->size
,
759 dir2name
[entry
->direction
],
760 dir2name
[direction
]);
763 put_hash_bucket(bucket
, &flags
);
767 void debug_dma_map_page(struct device
*dev
, struct page
*page
, size_t offset
,
768 size_t size
, int direction
, dma_addr_t dma_addr
,
771 struct dma_debug_entry
*entry
;
773 if (unlikely(global_disable
))
776 if (unlikely(dma_mapping_error(dev
, dma_addr
)))
779 entry
= dma_entry_alloc();
784 entry
->type
= dma_debug_page
;
785 entry
->paddr
= page_to_phys(page
) + offset
;
786 entry
->dev_addr
= dma_addr
;
788 entry
->direction
= direction
;
791 entry
->type
= dma_debug_single
;
793 if (!PageHighMem(page
)) {
794 void *addr
= ((char *)page_address(page
)) + offset
;
795 check_for_stack(dev
, addr
);
796 check_for_illegal_area(dev
, addr
, size
);
799 add_dma_entry(entry
);
801 EXPORT_SYMBOL(debug_dma_map_page
);
803 void debug_dma_unmap_page(struct device
*dev
, dma_addr_t addr
,
804 size_t size
, int direction
, bool map_single
)
806 struct dma_debug_entry ref
= {
807 .type
= dma_debug_page
,
811 .direction
= direction
,
814 if (unlikely(global_disable
))
818 ref
.type
= dma_debug_single
;
822 EXPORT_SYMBOL(debug_dma_unmap_page
);
824 void debug_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
825 int nents
, int mapped_ents
, int direction
)
827 struct dma_debug_entry
*entry
;
828 struct scatterlist
*s
;
831 if (unlikely(global_disable
))
834 for_each_sg(sg
, s
, mapped_ents
, i
) {
835 entry
= dma_entry_alloc();
839 entry
->type
= dma_debug_sg
;
841 entry
->paddr
= sg_phys(s
);
842 entry
->size
= sg_dma_len(s
);
843 entry
->dev_addr
= sg_dma_address(s
);
844 entry
->direction
= direction
;
845 entry
->sg_call_ents
= nents
;
846 entry
->sg_mapped_ents
= mapped_ents
;
848 if (!PageHighMem(sg_page(s
))) {
849 check_for_stack(dev
, sg_virt(s
));
850 check_for_illegal_area(dev
, sg_virt(s
), sg_dma_len(s
));
853 add_dma_entry(entry
);
856 EXPORT_SYMBOL(debug_dma_map_sg
);
858 void debug_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
861 struct dma_debug_entry
*entry
;
862 struct scatterlist
*s
;
863 int mapped_ents
= 0, i
;
866 if (unlikely(global_disable
))
869 for_each_sg(sglist
, s
, nelems
, i
) {
871 struct dma_debug_entry ref
= {
872 .type
= dma_debug_sg
,
875 .dev_addr
= sg_dma_address(s
),
876 .size
= sg_dma_len(s
),
881 if (mapped_ents
&& i
>= mapped_ents
)
884 if (mapped_ents
== 0) {
885 struct hash_bucket
*bucket
;
886 ref
.sg_call_ents
= nelems
;
887 bucket
= get_hash_bucket(&ref
, &flags
);
888 entry
= hash_bucket_find(bucket
, &ref
);
890 mapped_ents
= entry
->sg_mapped_ents
;
891 put_hash_bucket(bucket
, &flags
);
897 EXPORT_SYMBOL(debug_dma_unmap_sg
);
899 void debug_dma_alloc_coherent(struct device
*dev
, size_t size
,
900 dma_addr_t dma_addr
, void *virt
)
902 struct dma_debug_entry
*entry
;
904 if (unlikely(global_disable
))
907 if (unlikely(virt
== NULL
))
910 entry
= dma_entry_alloc();
914 entry
->type
= dma_debug_coherent
;
916 entry
->paddr
= virt_to_phys(virt
);
918 entry
->dev_addr
= dma_addr
;
919 entry
->direction
= DMA_BIDIRECTIONAL
;
921 add_dma_entry(entry
);
923 EXPORT_SYMBOL(debug_dma_alloc_coherent
);
925 void debug_dma_free_coherent(struct device
*dev
, size_t size
,
926 void *virt
, dma_addr_t addr
)
928 struct dma_debug_entry ref
= {
929 .type
= dma_debug_coherent
,
931 .paddr
= virt_to_phys(virt
),
934 .direction
= DMA_BIDIRECTIONAL
,
937 if (unlikely(global_disable
))
942 EXPORT_SYMBOL(debug_dma_free_coherent
);
944 void debug_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
945 size_t size
, int direction
)
947 if (unlikely(global_disable
))
950 check_sync(dev
, dma_handle
, size
, 0, direction
, true);
952 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu
);
954 void debug_dma_sync_single_for_device(struct device
*dev
,
955 dma_addr_t dma_handle
, size_t size
,
958 if (unlikely(global_disable
))
961 check_sync(dev
, dma_handle
, size
, 0, direction
, false);
963 EXPORT_SYMBOL(debug_dma_sync_single_for_device
);
965 void debug_dma_sync_single_range_for_cpu(struct device
*dev
,
966 dma_addr_t dma_handle
,
967 unsigned long offset
, size_t size
,
970 if (unlikely(global_disable
))
973 check_sync(dev
, dma_handle
, size
, offset
, direction
, true);
975 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu
);
977 void debug_dma_sync_single_range_for_device(struct device
*dev
,
978 dma_addr_t dma_handle
,
979 unsigned long offset
,
980 size_t size
, int direction
)
982 if (unlikely(global_disable
))
985 check_sync(dev
, dma_handle
, size
, offset
, direction
, false);
987 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device
);
989 void debug_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
990 int nelems
, int direction
)
992 struct scatterlist
*s
;
995 if (unlikely(global_disable
))
998 for_each_sg(sg
, s
, nelems
, i
) {
999 check_sync(dev
, sg_dma_address(s
), sg_dma_len(s
), 0,
1003 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu
);
1005 void debug_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
1006 int nelems
, int direction
)
1008 struct scatterlist
*s
;
1011 if (unlikely(global_disable
))
1014 for_each_sg(sg
, s
, nelems
, i
) {
1015 check_sync(dev
, sg_dma_address(s
), sg_dma_len(s
), 0,
1019 EXPORT_SYMBOL(debug_dma_sync_sg_for_device
);