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 #define HASH_SIZE 1024ULL
33 #define HASH_FN_SHIFT 13
34 #define HASH_FN_MASK (HASH_SIZE - 1)
43 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
45 struct dma_debug_entry {
46 struct list_head list;
55 #ifdef CONFIG_STACKTRACE
56 struct stack_trace stacktrace;
57 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
62 struct list_head list;
64 } ____cacheline_aligned_in_smp;
66 /* Hash list to save the allocated dma addresses */
67 static struct hash_bucket dma_entry_hash[HASH_SIZE];
68 /* List of pre-allocated dma_debug_entry's */
69 static LIST_HEAD(free_entries);
70 /* Lock for the list above */
71 static DEFINE_SPINLOCK(free_entries_lock);
73 /* Global disable flag - will be set in case of an error */
74 static bool global_disable __read_mostly;
76 /* Global error count */
77 static u32 error_count;
79 /* Global error show enable*/
80 static u32 show_all_errors __read_mostly;
81 /* Number of errors to show */
82 static u32 show_num_errors = 1;
84 static u32 num_free_entries;
85 static u32 min_free_entries;
87 /* number of preallocated entries requested by kernel cmdline */
88 static u32 req_entries;
90 /* debugfs dentry's for the stuff above */
91 static struct dentry *dma_debug_dent __read_mostly;
92 static struct dentry *global_disable_dent __read_mostly;
93 static struct dentry *error_count_dent __read_mostly;
94 static struct dentry *show_all_errors_dent __read_mostly;
95 static struct dentry *show_num_errors_dent __read_mostly;
96 static struct dentry *num_free_entries_dent __read_mostly;
97 static struct dentry *min_free_entries_dent __read_mostly;
99 static const char *type2name[4] = { "single", "page",
100 "scather-gather", "coherent" };
102 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
103 "DMA_FROM_DEVICE", "DMA_NONE" };
106 * The access to some variables in this macro is racy. We can't use atomic_t
107 * here because all these variables are exported to debugfs. Some of them even
108 * writeable. This is also the reason why a lock won't help much. But anyway,
109 * the races are no big deal. Here is why:
111 * error_count: the addition is racy, but the worst thing that can happen is
112 * that we don't count some errors
113 * show_num_errors: the subtraction is racy. Also no big deal because in
114 * worst case this will result in one warning more in the
115 * system log than the user configured. This variable is
116 * writeable via debugfs.
118 static inline void dump_entry_trace(struct dma_debug_entry *entry)
120 #ifdef CONFIG_STACKTRACE
122 printk(KERN_WARNING "Mapped at:\n");
123 print_stack_trace(&entry->stacktrace, 0);
128 #define err_printk(dev, entry, format, arg...) do { \
130 if (show_all_errors || show_num_errors > 0) { \
131 WARN(1, "%s %s: " format, \
132 dev_driver_string(dev), \
133 dev_name(dev) , ## arg); \
134 dump_entry_trace(entry); \
136 if (!show_all_errors && show_num_errors > 0) \
137 show_num_errors -= 1; \
141 * Hash related functions
143 * Every DMA-API request is saved into a struct dma_debug_entry. To
144 * have quick access to these structs they are stored into a hash.
146 static int hash_fn(struct dma_debug_entry *entry)
149 * Hash function is based on the dma address.
150 * We use bits 20-27 here as the index into the hash
152 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
156 * Request exclusive access to a hash bucket for a given dma_debug_entry.
158 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
159 unsigned long *flags)
161 int idx = hash_fn(entry);
162 unsigned long __flags;
164 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
166 return &dma_entry_hash[idx];
170 * Give up exclusive access to the hash bucket
172 static void put_hash_bucket(struct hash_bucket *bucket,
173 unsigned long *flags)
175 unsigned long __flags = *flags;
177 spin_unlock_irqrestore(&bucket->lock, __flags);
181 * Search a given entry in the hash bucket list
183 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
184 struct dma_debug_entry *ref)
186 struct dma_debug_entry *entry;
188 list_for_each_entry(entry, &bucket->list, list) {
189 if ((entry->dev_addr == ref->dev_addr) &&
190 (entry->dev == ref->dev))
198 * Add an entry to a hash bucket
200 static void hash_bucket_add(struct hash_bucket *bucket,
201 struct dma_debug_entry *entry)
203 list_add_tail(&entry->list, &bucket->list);
207 * Remove entry from a hash bucket list
209 static void hash_bucket_del(struct dma_debug_entry *entry)
211 list_del(&entry->list);
215 * Dump mapping entries for debugging purposes
217 void debug_dma_dump_mappings(struct device *dev)
221 for (idx = 0; idx < HASH_SIZE; idx++) {
222 struct hash_bucket *bucket = &dma_entry_hash[idx];
223 struct dma_debug_entry *entry;
226 spin_lock_irqsave(&bucket->lock, flags);
228 list_for_each_entry(entry, &bucket->list, list) {
229 if (!dev || dev == entry->dev) {
231 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
232 type2name[entry->type], idx,
233 (unsigned long long)entry->paddr,
234 entry->dev_addr, entry->size,
235 dir2name[entry->direction]);
239 spin_unlock_irqrestore(&bucket->lock, flags);
242 EXPORT_SYMBOL(debug_dma_dump_mappings);
245 * Wrapper function for adding an entry to the hash.
246 * This function takes care of locking itself.
248 static void add_dma_entry(struct dma_debug_entry *entry)
250 struct hash_bucket *bucket;
253 bucket = get_hash_bucket(entry, &flags);
254 hash_bucket_add(bucket, entry);
255 put_hash_bucket(bucket, &flags);
258 /* struct dma_entry allocator
260 * The next two functions implement the allocator for
261 * struct dma_debug_entries.
263 static struct dma_debug_entry *dma_entry_alloc(void)
265 struct dma_debug_entry *entry = NULL;
268 spin_lock_irqsave(&free_entries_lock, flags);
270 if (list_empty(&free_entries)) {
271 printk(KERN_ERR "DMA-API: debugging out of memory "
273 global_disable = true;
277 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
278 list_del(&entry->list);
279 memset(entry, 0, sizeof(*entry));
281 #ifdef CONFIG_STACKTRACE
282 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
283 entry->stacktrace.entries = entry->st_entries;
284 entry->stacktrace.skip = 2;
285 save_stack_trace(&entry->stacktrace);
287 num_free_entries -= 1;
288 if (num_free_entries < min_free_entries)
289 min_free_entries = num_free_entries;
292 spin_unlock_irqrestore(&free_entries_lock, flags);
297 static void dma_entry_free(struct dma_debug_entry *entry)
302 * add to beginning of the list - this way the entries are
303 * more likely cache hot when they are reallocated.
305 spin_lock_irqsave(&free_entries_lock, flags);
306 list_add(&entry->list, &free_entries);
307 num_free_entries += 1;
308 spin_unlock_irqrestore(&free_entries_lock, flags);
312 * DMA-API debugging init code
314 * The init code does two things:
315 * 1. Initialize core data structures
316 * 2. Preallocate a given number of dma_debug_entry structs
319 static int prealloc_memory(u32 num_entries)
321 struct dma_debug_entry *entry, *next_entry;
324 for (i = 0; i < num_entries; ++i) {
325 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
329 list_add_tail(&entry->list, &free_entries);
332 num_free_entries = num_entries;
333 min_free_entries = num_entries;
335 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
342 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
343 list_del(&entry->list);
350 static int dma_debug_fs_init(void)
352 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
353 if (!dma_debug_dent) {
354 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
358 global_disable_dent = debugfs_create_bool("disabled", 0444,
360 (u32 *)&global_disable);
361 if (!global_disable_dent)
364 error_count_dent = debugfs_create_u32("error_count", 0444,
365 dma_debug_dent, &error_count);
366 if (!error_count_dent)
369 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
372 if (!show_all_errors_dent)
375 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
378 if (!show_num_errors_dent)
381 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
384 if (!num_free_entries_dent)
387 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
390 if (!min_free_entries_dent)
396 debugfs_remove_recursive(dma_debug_dent);
403 * Let the architectures decide how many entries should be preallocated.
405 void dma_debug_init(u32 num_entries)
412 for (i = 0; i < HASH_SIZE; ++i) {
413 INIT_LIST_HEAD(&dma_entry_hash[i].list);
414 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
417 if (dma_debug_fs_init() != 0) {
418 printk(KERN_ERR "DMA-API: error creating debugfs entries "
420 global_disable = true;
426 num_entries = req_entries;
428 if (prealloc_memory(num_entries) != 0) {
429 printk(KERN_ERR "DMA-API: debugging out of memory error "
431 global_disable = true;
436 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
439 static __init int dma_debug_cmdline(char *str)
444 if (strncmp(str, "off", 3) == 0) {
445 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
447 global_disable = true;
453 static __init int dma_debug_entries_cmdline(char *str)
460 res = get_option(&str, &req_entries);
468 __setup("dma_debug=", dma_debug_cmdline);
469 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
471 static void check_unmap(struct dma_debug_entry *ref)
473 struct dma_debug_entry *entry;
474 struct hash_bucket *bucket;
477 if (dma_mapping_error(ref->dev, ref->dev_addr))
480 bucket = get_hash_bucket(ref, &flags);
481 entry = hash_bucket_find(bucket, ref);
484 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
485 "to free DMA memory it has not allocated "
486 "[device address=0x%016llx] [size=%llu bytes]\n",
487 ref->dev_addr, ref->size);
491 if (ref->size != entry->size) {
492 err_printk(ref->dev, entry, "DMA-API: device driver frees "
493 "DMA memory with different size "
494 "[device address=0x%016llx] [map size=%llu bytes] "
495 "[unmap size=%llu bytes]\n",
496 ref->dev_addr, entry->size, ref->size);
499 if (ref->type != entry->type) {
500 err_printk(ref->dev, entry, "DMA-API: device driver frees "
501 "DMA memory with wrong function "
502 "[device address=0x%016llx] [size=%llu bytes] "
503 "[mapped as %s] [unmapped as %s]\n",
504 ref->dev_addr, ref->size,
505 type2name[entry->type], type2name[ref->type]);
506 } else if ((entry->type == dma_debug_coherent) &&
507 (ref->paddr != entry->paddr)) {
508 err_printk(ref->dev, entry, "DMA-API: device driver frees "
509 "DMA memory with different CPU address "
510 "[device address=0x%016llx] [size=%llu bytes] "
511 "[cpu alloc address=%p] [cpu free address=%p]",
512 ref->dev_addr, ref->size,
513 (void *)entry->paddr, (void *)ref->paddr);
516 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
517 ref->sg_call_ents != entry->sg_call_ents) {
518 err_printk(ref->dev, entry, "DMA-API: device driver frees "
519 "DMA sg list with different entry count "
520 "[map count=%d] [unmap count=%d]\n",
521 entry->sg_call_ents, ref->sg_call_ents);
525 * This may be no bug in reality - but most implementations of the
526 * DMA API don't handle this properly, so check for it here
528 if (ref->direction != entry->direction) {
529 err_printk(ref->dev, entry, "DMA-API: device driver frees "
530 "DMA memory with different direction "
531 "[device address=0x%016llx] [size=%llu bytes] "
532 "[mapped with %s] [unmapped with %s]\n",
533 ref->dev_addr, ref->size,
534 dir2name[entry->direction],
535 dir2name[ref->direction]);
538 hash_bucket_del(entry);
539 dma_entry_free(entry);
542 put_hash_bucket(bucket, &flags);
545 static void check_for_stack(struct device *dev, void *addr)
547 if (object_is_on_stack(addr))
548 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
549 "stack [addr=%p]\n", addr);
552 static void check_sync(struct device *dev, dma_addr_t addr,
553 u64 size, u64 offset, int direction, bool to_cpu)
555 struct dma_debug_entry ref = {
559 .direction = direction,
561 struct dma_debug_entry *entry;
562 struct hash_bucket *bucket;
565 bucket = get_hash_bucket(&ref, &flags);
567 entry = hash_bucket_find(bucket, &ref);
570 err_printk(dev, NULL, "DMA-API: device driver tries "
571 "to sync DMA memory it has not allocated "
572 "[device address=0x%016llx] [size=%llu bytes]\n",
577 if ((offset + size) > entry->size) {
578 err_printk(dev, entry, "DMA-API: device driver syncs"
579 " DMA memory outside allocated range "
580 "[device address=0x%016llx] "
581 "[allocation size=%llu bytes] [sync offset=%llu] "
582 "[sync size=%llu]\n", entry->dev_addr, entry->size,
586 if (direction != entry->direction) {
587 err_printk(dev, entry, "DMA-API: device driver syncs "
588 "DMA memory with different direction "
589 "[device address=0x%016llx] [size=%llu bytes] "
590 "[mapped with %s] [synced with %s]\n",
592 dir2name[entry->direction],
593 dir2name[direction]);
596 if (entry->direction == DMA_BIDIRECTIONAL)
599 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
600 !(direction == DMA_TO_DEVICE))
601 err_printk(dev, entry, "DMA-API: device driver syncs "
602 "device read-only DMA memory for cpu "
603 "[device address=0x%016llx] [size=%llu bytes] "
604 "[mapped with %s] [synced with %s]\n",
606 dir2name[entry->direction],
607 dir2name[direction]);
609 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
610 !(direction == DMA_FROM_DEVICE))
611 err_printk(dev, entry, "DMA-API: device driver syncs "
612 "device write-only DMA memory to device "
613 "[device address=0x%016llx] [size=%llu bytes] "
614 "[mapped with %s] [synced with %s]\n",
616 dir2name[entry->direction],
617 dir2name[direction]);
620 put_hash_bucket(bucket, &flags);
624 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
625 size_t size, int direction, dma_addr_t dma_addr,
628 struct dma_debug_entry *entry;
630 if (unlikely(global_disable))
633 if (unlikely(dma_mapping_error(dev, dma_addr)))
636 entry = dma_entry_alloc();
641 entry->type = dma_debug_page;
642 entry->paddr = page_to_phys(page) + offset;
643 entry->dev_addr = dma_addr;
645 entry->direction = direction;
648 entry->type = dma_debug_single;
649 check_for_stack(dev, page_address(page) + offset);
652 add_dma_entry(entry);
654 EXPORT_SYMBOL(debug_dma_map_page);
656 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
657 size_t size, int direction, bool map_single)
659 struct dma_debug_entry ref = {
660 .type = dma_debug_page,
664 .direction = direction,
667 if (unlikely(global_disable))
671 ref.type = dma_debug_single;
675 EXPORT_SYMBOL(debug_dma_unmap_page);
677 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
678 int nents, int mapped_ents, int direction)
680 struct dma_debug_entry *entry;
681 struct scatterlist *s;
684 if (unlikely(global_disable))
687 for_each_sg(sg, s, mapped_ents, i) {
688 entry = dma_entry_alloc();
692 entry->type = dma_debug_sg;
694 entry->paddr = sg_phys(s);
695 entry->size = s->length;
696 entry->dev_addr = s->dma_address;
697 entry->direction = direction;
698 entry->sg_call_ents = nents;
699 entry->sg_mapped_ents = mapped_ents;
701 check_for_stack(dev, sg_virt(s));
703 add_dma_entry(entry);
706 EXPORT_SYMBOL(debug_dma_map_sg);
708 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
711 struct dma_debug_entry *entry;
712 struct scatterlist *s;
713 int mapped_ents = 0, i;
716 if (unlikely(global_disable))
719 for_each_sg(sglist, s, nelems, i) {
721 struct dma_debug_entry ref = {
722 .type = dma_debug_sg,
725 .dev_addr = s->dma_address,
731 if (mapped_ents && i >= mapped_ents)
734 if (mapped_ents == 0) {
735 struct hash_bucket *bucket;
736 ref.sg_call_ents = nelems;
737 bucket = get_hash_bucket(&ref, &flags);
738 entry = hash_bucket_find(bucket, &ref);
740 mapped_ents = entry->sg_mapped_ents;
741 put_hash_bucket(bucket, &flags);
747 EXPORT_SYMBOL(debug_dma_unmap_sg);
749 void debug_dma_alloc_coherent(struct device *dev, size_t size,
750 dma_addr_t dma_addr, void *virt)
752 struct dma_debug_entry *entry;
754 if (unlikely(global_disable))
757 if (unlikely(virt == NULL))
760 entry = dma_entry_alloc();
764 entry->type = dma_debug_coherent;
766 entry->paddr = virt_to_phys(virt);
768 entry->dev_addr = dma_addr;
769 entry->direction = DMA_BIDIRECTIONAL;
771 add_dma_entry(entry);
773 EXPORT_SYMBOL(debug_dma_alloc_coherent);
775 void debug_dma_free_coherent(struct device *dev, size_t size,
776 void *virt, dma_addr_t addr)
778 struct dma_debug_entry ref = {
779 .type = dma_debug_coherent,
781 .paddr = virt_to_phys(virt),
784 .direction = DMA_BIDIRECTIONAL,
787 if (unlikely(global_disable))
792 EXPORT_SYMBOL(debug_dma_free_coherent);
794 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
795 size_t size, int direction)
797 if (unlikely(global_disable))
800 check_sync(dev, dma_handle, size, 0, direction, true);
802 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
804 void debug_dma_sync_single_for_device(struct device *dev,
805 dma_addr_t dma_handle, size_t size,
808 if (unlikely(global_disable))
811 check_sync(dev, dma_handle, size, 0, direction, false);
813 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
815 void debug_dma_sync_single_range_for_cpu(struct device *dev,
816 dma_addr_t dma_handle,
817 unsigned long offset, size_t size,
820 if (unlikely(global_disable))
823 check_sync(dev, dma_handle, size, offset, direction, true);
825 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
827 void debug_dma_sync_single_range_for_device(struct device *dev,
828 dma_addr_t dma_handle,
829 unsigned long offset,
830 size_t size, int direction)
832 if (unlikely(global_disable))
835 check_sync(dev, dma_handle, size, offset, direction, false);
837 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
839 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
840 int nelems, int direction)
842 struct scatterlist *s;
845 if (unlikely(global_disable))
848 for_each_sg(sg, s, nelems, i) {
849 check_sync(dev, s->dma_address, s->dma_length, 0,
853 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
855 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
856 int nelems, int direction)
858 struct scatterlist *s;
861 if (unlikely(global_disable))
864 for_each_sg(sg, s, nelems, i) {
865 check_sync(dev, s->dma_address, s->dma_length, 0,
869 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);