Merge branch 'dma-debug/2.6.31' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / lib / dma-debug.c
1 /*
2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
3 *
4 * Author: Joerg Roedel <joerg.roedel@amd.com>
5 *
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.
9 *
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.
14 *
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
18 */
19
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>
33
34 #include <asm/sections.h>
35
36 #define HASH_SIZE 1024ULL
37 #define HASH_FN_SHIFT 13
38 #define HASH_FN_MASK (HASH_SIZE - 1)
39
40 enum {
41 dma_debug_single,
42 dma_debug_page,
43 dma_debug_sg,
44 dma_debug_coherent,
45 };
46
47 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
48
49 struct dma_debug_entry {
50 struct list_head list;
51 struct device *dev;
52 int type;
53 phys_addr_t paddr;
54 u64 dev_addr;
55 u64 size;
56 int direction;
57 int sg_call_ents;
58 int sg_mapped_ents;
59 #ifdef CONFIG_STACKTRACE
60 struct stack_trace stacktrace;
61 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
62 #endif
63 };
64
65 struct hash_bucket {
66 struct list_head list;
67 spinlock_t lock;
68 } ____cacheline_aligned_in_smp;
69
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);
76
77 /* Global disable flag - will be set in case of an error */
78 static bool global_disable __read_mostly;
79
80 /* Global error count */
81 static u32 error_count;
82
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;
87
88 static u32 num_free_entries;
89 static u32 min_free_entries;
90 static u32 nr_total_entries;
91
92 /* number of preallocated entries requested by kernel cmdline */
93 static u32 req_entries;
94
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;
104
105 /* per-driver filter related state */
106
107 #define NAME_MAX_LEN 64
108
109 static char current_driver_name[NAME_MAX_LEN] __read_mostly;
110 static struct device_driver *current_driver __read_mostly;
111
112 static DEFINE_RWLOCK(driver_name_lock);
113
114 static const char *type2name[4] = { "single", "page",
115 "scather-gather", "coherent" };
116
117 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
118 "DMA_FROM_DEVICE", "DMA_NONE" };
119
120 /* little merge helper - remove it after the merge window */
121 #ifndef BUS_NOTIFY_UNBOUND_DRIVER
122 #define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
123 #endif
124
125 /*
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:
130 *
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.
137 */
138 static inline void dump_entry_trace(struct dma_debug_entry *entry)
139 {
140 #ifdef CONFIG_STACKTRACE
141 if (entry) {
142 printk(KERN_WARNING "Mapped at:\n");
143 print_stack_trace(&entry->stacktrace, 0);
144 }
145 #endif
146 }
147
148 static bool driver_filter(struct device *dev)
149 {
150 /* driver filter off */
151 if (likely(!current_driver_name[0]))
152 return true;
153
154 /* driver filter on and initialized */
155 if (current_driver && dev->driver == current_driver)
156 return true;
157
158 /* driver filter on but not yet initialized */
159 if (!current_driver && current_driver_name[0]) {
160 struct device_driver *drv = get_driver(dev->driver);
161 unsigned long flags;
162 bool ret = false;
163
164 if (!drv)
165 return false;
166
167 /* lock to protect against change of current_driver_name */
168 read_lock_irqsave(&driver_name_lock, flags);
169
170 if (drv->name &&
171 strncmp(current_driver_name, drv->name,
172 NAME_MAX_LEN-1) == 0) {
173 current_driver = drv;
174 ret = true;
175 }
176
177 read_unlock_irqrestore(&driver_name_lock, flags);
178 put_driver(drv);
179
180 return ret;
181 }
182
183 return false;
184 }
185
186 #define err_printk(dev, entry, format, arg...) do { \
187 error_count += 1; \
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); \
194 } \
195 if (!show_all_errors && show_num_errors > 0) \
196 show_num_errors -= 1; \
197 } while (0);
198
199 /*
200 * Hash related functions
201 *
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.
204 */
205 static int hash_fn(struct dma_debug_entry *entry)
206 {
207 /*
208 * Hash function is based on the dma address.
209 * We use bits 20-27 here as the index into the hash
210 */
211 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
212 }
213
214 /*
215 * Request exclusive access to a hash bucket for a given dma_debug_entry.
216 */
217 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
218 unsigned long *flags)
219 {
220 int idx = hash_fn(entry);
221 unsigned long __flags;
222
223 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
224 *flags = __flags;
225 return &dma_entry_hash[idx];
226 }
227
228 /*
229 * Give up exclusive access to the hash bucket
230 */
231 static void put_hash_bucket(struct hash_bucket *bucket,
232 unsigned long *flags)
233 {
234 unsigned long __flags = *flags;
235
236 spin_unlock_irqrestore(&bucket->lock, __flags);
237 }
238
239 /*
240 * Search a given entry in the hash bucket list
241 */
242 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
243 struct dma_debug_entry *ref)
244 {
245 struct dma_debug_entry *entry, *ret = NULL;
246 int matches = 0, match_lvl, last_lvl = 0;
247
248 list_for_each_entry(entry, &bucket->list, list) {
249 if ((entry->dev_addr != ref->dev_addr) ||
250 (entry->dev != ref->dev))
251 continue;
252
253 /*
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.
262 */
263 matches += 1;
264 match_lvl = 0;
265 entry->size == ref->size ? ++match_lvl : match_lvl;
266 entry->type == ref->type ? ++match_lvl : match_lvl;
267 entry->direction == ref->direction ? ++match_lvl : match_lvl;
268
269 if (match_lvl == 3) {
270 /* perfect-fit - return the result */
271 return entry;
272 } else if (match_lvl > last_lvl) {
273 /*
274 * We found an entry that fits better then the
275 * previous one
276 */
277 last_lvl = match_lvl;
278 ret = entry;
279 }
280 }
281
282 /*
283 * If we have multiple matches but no perfect-fit, just return
284 * NULL.
285 */
286 ret = (matches == 1) ? ret : NULL;
287
288 return ret;
289 }
290
291 /*
292 * Add an entry to a hash bucket
293 */
294 static void hash_bucket_add(struct hash_bucket *bucket,
295 struct dma_debug_entry *entry)
296 {
297 list_add_tail(&entry->list, &bucket->list);
298 }
299
300 /*
301 * Remove entry from a hash bucket list
302 */
303 static void hash_bucket_del(struct dma_debug_entry *entry)
304 {
305 list_del(&entry->list);
306 }
307
308 /*
309 * Dump mapping entries for debugging purposes
310 */
311 void debug_dma_dump_mappings(struct device *dev)
312 {
313 int idx;
314
315 for (idx = 0; idx < HASH_SIZE; idx++) {
316 struct hash_bucket *bucket = &dma_entry_hash[idx];
317 struct dma_debug_entry *entry;
318 unsigned long flags;
319
320 spin_lock_irqsave(&bucket->lock, flags);
321
322 list_for_each_entry(entry, &bucket->list, list) {
323 if (!dev || dev == entry->dev) {
324 dev_info(entry->dev,
325 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
326 type2name[entry->type], idx,
327 (unsigned long long)entry->paddr,
328 entry->dev_addr, entry->size,
329 dir2name[entry->direction]);
330 }
331 }
332
333 spin_unlock_irqrestore(&bucket->lock, flags);
334 }
335 }
336 EXPORT_SYMBOL(debug_dma_dump_mappings);
337
338 /*
339 * Wrapper function for adding an entry to the hash.
340 * This function takes care of locking itself.
341 */
342 static void add_dma_entry(struct dma_debug_entry *entry)
343 {
344 struct hash_bucket *bucket;
345 unsigned long flags;
346
347 bucket = get_hash_bucket(entry, &flags);
348 hash_bucket_add(bucket, entry);
349 put_hash_bucket(bucket, &flags);
350 }
351
352 static struct dma_debug_entry *__dma_entry_alloc(void)
353 {
354 struct dma_debug_entry *entry;
355
356 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
357 list_del(&entry->list);
358 memset(entry, 0, sizeof(*entry));
359
360 num_free_entries -= 1;
361 if (num_free_entries < min_free_entries)
362 min_free_entries = num_free_entries;
363
364 return entry;
365 }
366
367 /* struct dma_entry allocator
368 *
369 * The next two functions implement the allocator for
370 * struct dma_debug_entries.
371 */
372 static struct dma_debug_entry *dma_entry_alloc(void)
373 {
374 struct dma_debug_entry *entry = NULL;
375 unsigned long flags;
376
377 spin_lock_irqsave(&free_entries_lock, flags);
378
379 if (list_empty(&free_entries)) {
380 printk(KERN_ERR "DMA-API: debugging out of memory "
381 "- disabling\n");
382 global_disable = true;
383 goto out;
384 }
385
386 entry = __dma_entry_alloc();
387
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);
393 #endif
394
395 out:
396 spin_unlock_irqrestore(&free_entries_lock, flags);
397
398 return entry;
399 }
400
401 static void dma_entry_free(struct dma_debug_entry *entry)
402 {
403 unsigned long flags;
404
405 /*
406 * add to beginning of the list - this way the entries are
407 * more likely cache hot when they are reallocated.
408 */
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);
413 }
414
415 int dma_debug_resize_entries(u32 num_entries)
416 {
417 int i, delta, ret = 0;
418 unsigned long flags;
419 struct dma_debug_entry *entry;
420 LIST_HEAD(tmp);
421
422 spin_lock_irqsave(&free_entries_lock, flags);
423
424 if (nr_total_entries < num_entries) {
425 delta = num_entries - nr_total_entries;
426
427 spin_unlock_irqrestore(&free_entries_lock, flags);
428
429 for (i = 0; i < delta; i++) {
430 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
431 if (!entry)
432 break;
433
434 list_add_tail(&entry->list, &tmp);
435 }
436
437 spin_lock_irqsave(&free_entries_lock, flags);
438
439 list_splice(&tmp, &free_entries);
440 nr_total_entries += i;
441 num_free_entries += i;
442 } else {
443 delta = nr_total_entries - num_entries;
444
445 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
446 entry = __dma_entry_alloc();
447 kfree(entry);
448 }
449
450 nr_total_entries -= i;
451 }
452
453 if (nr_total_entries != num_entries)
454 ret = 1;
455
456 spin_unlock_irqrestore(&free_entries_lock, flags);
457
458 return ret;
459 }
460 EXPORT_SYMBOL(dma_debug_resize_entries);
461
462 /*
463 * DMA-API debugging init code
464 *
465 * The init code does two things:
466 * 1. Initialize core data structures
467 * 2. Preallocate a given number of dma_debug_entry structs
468 */
469
470 static int prealloc_memory(u32 num_entries)
471 {
472 struct dma_debug_entry *entry, *next_entry;
473 int i;
474
475 for (i = 0; i < num_entries; ++i) {
476 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
477 if (!entry)
478 goto out_err;
479
480 list_add_tail(&entry->list, &free_entries);
481 }
482
483 num_free_entries = num_entries;
484 min_free_entries = num_entries;
485
486 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
487 num_entries);
488
489 return 0;
490
491 out_err:
492
493 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
494 list_del(&entry->list);
495 kfree(entry);
496 }
497
498 return -ENOMEM;
499 }
500
501 static ssize_t filter_read(struct file *file, char __user *user_buf,
502 size_t count, loff_t *ppos)
503 {
504 unsigned long flags;
505 char buf[NAME_MAX_LEN + 1];
506 int len;
507
508 if (!current_driver_name[0])
509 return 0;
510
511 /*
512 * We can't copy to userspace directly because current_driver_name can
513 * only be read under the driver_name_lock with irqs disabled. So
514 * create a temporary copy first.
515 */
516 read_lock_irqsave(&driver_name_lock, flags);
517 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
518 read_unlock_irqrestore(&driver_name_lock, flags);
519
520 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
521 }
522
523 static ssize_t filter_write(struct file *file, const char __user *userbuf,
524 size_t count, loff_t *ppos)
525 {
526 unsigned long flags;
527 char buf[NAME_MAX_LEN];
528 size_t len = NAME_MAX_LEN - 1;
529 int i;
530
531 /*
532 * We can't copy from userspace directly. Access to
533 * current_driver_name is protected with a write_lock with irqs
534 * disabled. Since copy_from_user can fault and may sleep we
535 * need to copy to temporary buffer first
536 */
537 len = min(count, len);
538 if (copy_from_user(buf, userbuf, len))
539 return -EFAULT;
540
541 buf[len] = 0;
542
543 write_lock_irqsave(&driver_name_lock, flags);
544
545 /* Now handle the string we got from userspace very carefully.
546 * The rules are:
547 * - only use the first token we got
548 * - token delimiter is everything looking like a space
549 * character (' ', '\n', '\t' ...)
550 *
551 */
552 if (!isalnum(buf[0])) {
553 /*
554 If the first character userspace gave us is not
555 * alphanumerical then assume the filter should be
556 * switched off.
557 */
558 if (current_driver_name[0])
559 printk(KERN_INFO "DMA-API: switching off dma-debug "
560 "driver filter\n");
561 current_driver_name[0] = 0;
562 current_driver = NULL;
563 goto out_unlock;
564 }
565
566 /*
567 * Now parse out the first token and use it as the name for the
568 * driver to filter for.
569 */
570 for (i = 0; i < NAME_MAX_LEN; ++i) {
571 current_driver_name[i] = buf[i];
572 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
573 break;
574 }
575 current_driver_name[i] = 0;
576 current_driver = NULL;
577
578 printk(KERN_INFO "DMA-API: enable driver filter for driver [%s]\n",
579 current_driver_name);
580
581 out_unlock:
582 write_unlock_irqrestore(&driver_name_lock, flags);
583
584 return count;
585 }
586
587 const struct file_operations filter_fops = {
588 .read = filter_read,
589 .write = filter_write,
590 };
591
592 static int dma_debug_fs_init(void)
593 {
594 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
595 if (!dma_debug_dent) {
596 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
597 return -ENOMEM;
598 }
599
600 global_disable_dent = debugfs_create_bool("disabled", 0444,
601 dma_debug_dent,
602 (u32 *)&global_disable);
603 if (!global_disable_dent)
604 goto out_err;
605
606 error_count_dent = debugfs_create_u32("error_count", 0444,
607 dma_debug_dent, &error_count);
608 if (!error_count_dent)
609 goto out_err;
610
611 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
612 dma_debug_dent,
613 &show_all_errors);
614 if (!show_all_errors_dent)
615 goto out_err;
616
617 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
618 dma_debug_dent,
619 &show_num_errors);
620 if (!show_num_errors_dent)
621 goto out_err;
622
623 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
624 dma_debug_dent,
625 &num_free_entries);
626 if (!num_free_entries_dent)
627 goto out_err;
628
629 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
630 dma_debug_dent,
631 &min_free_entries);
632 if (!min_free_entries_dent)
633 goto out_err;
634
635 filter_dent = debugfs_create_file("driver_filter", 0644,
636 dma_debug_dent, NULL, &filter_fops);
637 if (!filter_dent)
638 goto out_err;
639
640 return 0;
641
642 out_err:
643 debugfs_remove_recursive(dma_debug_dent);
644
645 return -ENOMEM;
646 }
647
648 static int device_dma_allocations(struct device *dev)
649 {
650 struct dma_debug_entry *entry;
651 unsigned long flags;
652 int count = 0, i;
653
654 for (i = 0; i < HASH_SIZE; ++i) {
655 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
656 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
657 if (entry->dev == dev)
658 count += 1;
659 }
660 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
661 }
662
663 return count;
664 }
665
666 static int dma_debug_device_change(struct notifier_block *nb,
667 unsigned long action, void *data)
668 {
669 struct device *dev = data;
670 int count;
671
672
673 switch (action) {
674 case BUS_NOTIFY_UNBOUND_DRIVER:
675 count = device_dma_allocations(dev);
676 if (count == 0)
677 break;
678 err_printk(dev, NULL, "DMA-API: device driver has pending "
679 "DMA allocations while released from device "
680 "[count=%d]\n", count);
681 break;
682 default:
683 break;
684 }
685
686 return 0;
687 }
688
689 void dma_debug_add_bus(struct bus_type *bus)
690 {
691 struct notifier_block *nb;
692
693 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
694 if (nb == NULL) {
695 printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
696 return;
697 }
698
699 nb->notifier_call = dma_debug_device_change;
700
701 bus_register_notifier(bus, nb);
702 }
703
704 /*
705 * Let the architectures decide how many entries should be preallocated.
706 */
707 void dma_debug_init(u32 num_entries)
708 {
709 int i;
710
711 if (global_disable)
712 return;
713
714 for (i = 0; i < HASH_SIZE; ++i) {
715 INIT_LIST_HEAD(&dma_entry_hash[i].list);
716 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
717 }
718
719 if (dma_debug_fs_init() != 0) {
720 printk(KERN_ERR "DMA-API: error creating debugfs entries "
721 "- disabling\n");
722 global_disable = true;
723
724 return;
725 }
726
727 if (req_entries)
728 num_entries = req_entries;
729
730 if (prealloc_memory(num_entries) != 0) {
731 printk(KERN_ERR "DMA-API: debugging out of memory error "
732 "- disabled\n");
733 global_disable = true;
734
735 return;
736 }
737
738 nr_total_entries = num_free_entries;
739
740 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
741 }
742
743 static __init int dma_debug_cmdline(char *str)
744 {
745 if (!str)
746 return -EINVAL;
747
748 if (strncmp(str, "off", 3) == 0) {
749 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
750 "command line\n");
751 global_disable = true;
752 }
753
754 return 0;
755 }
756
757 static __init int dma_debug_entries_cmdline(char *str)
758 {
759 int res;
760
761 if (!str)
762 return -EINVAL;
763
764 res = get_option(&str, &req_entries);
765
766 if (!res)
767 req_entries = 0;
768
769 return 0;
770 }
771
772 __setup("dma_debug=", dma_debug_cmdline);
773 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
774
775 static void check_unmap(struct dma_debug_entry *ref)
776 {
777 struct dma_debug_entry *entry;
778 struct hash_bucket *bucket;
779 unsigned long flags;
780
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");
784 return;
785 }
786
787 bucket = get_hash_bucket(ref, &flags);
788 entry = hash_bucket_find(bucket, ref);
789
790 if (!entry) {
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);
795 goto out;
796 }
797
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);
804 }
805
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);
821 }
822
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);
829 }
830
831 /*
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
834 */
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]);
843 }
844
845 hash_bucket_del(entry);
846 dma_entry_free(entry);
847
848 out:
849 put_hash_bucket(bucket, &flags);
850 }
851
852 static void check_for_stack(struct device *dev, void *addr)
853 {
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);
857 }
858
859 static inline bool overlap(void *addr, u64 size, void *start, void *end)
860 {
861 void *addr2 = (char *)addr + size;
862
863 return ((addr >= start && addr < end) ||
864 (addr2 >= start && addr2 < end) ||
865 ((addr < start) && (addr2 >= end)));
866 }
867
868 static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
869 {
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);
875 }
876
877 static void check_sync(struct device *dev, dma_addr_t addr,
878 u64 size, u64 offset, int direction, bool to_cpu)
879 {
880 struct dma_debug_entry ref = {
881 .dev = dev,
882 .dev_addr = addr,
883 .size = size,
884 .direction = direction,
885 };
886 struct dma_debug_entry *entry;
887 struct hash_bucket *bucket;
888 unsigned long flags;
889
890 bucket = get_hash_bucket(&ref, &flags);
891
892 entry = hash_bucket_find(bucket, &ref);
893
894 if (!entry) {
895 err_printk(dev, NULL, "DMA-API: device driver tries "
896 "to sync DMA memory it has not allocated "
897 "[device address=0x%016llx] [size=%llu bytes]\n",
898 (unsigned long long)addr, size);
899 goto out;
900 }
901
902 if ((offset + size) > entry->size) {
903 err_printk(dev, entry, "DMA-API: device driver syncs"
904 " DMA memory outside allocated range "
905 "[device address=0x%016llx] "
906 "[allocation size=%llu bytes] [sync offset=%llu] "
907 "[sync size=%llu]\n", entry->dev_addr, entry->size,
908 offset, size);
909 }
910
911 if (direction != entry->direction) {
912 err_printk(dev, entry, "DMA-API: device driver syncs "
913 "DMA memory with different direction "
914 "[device address=0x%016llx] [size=%llu bytes] "
915 "[mapped with %s] [synced with %s]\n",
916 (unsigned long long)addr, entry->size,
917 dir2name[entry->direction],
918 dir2name[direction]);
919 }
920
921 if (entry->direction == DMA_BIDIRECTIONAL)
922 goto out;
923
924 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
925 !(direction == DMA_TO_DEVICE))
926 err_printk(dev, entry, "DMA-API: device driver syncs "
927 "device read-only DMA memory for cpu "
928 "[device address=0x%016llx] [size=%llu bytes] "
929 "[mapped with %s] [synced with %s]\n",
930 (unsigned long long)addr, entry->size,
931 dir2name[entry->direction],
932 dir2name[direction]);
933
934 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
935 !(direction == DMA_FROM_DEVICE))
936 err_printk(dev, entry, "DMA-API: device driver syncs "
937 "device write-only DMA memory to device "
938 "[device address=0x%016llx] [size=%llu bytes] "
939 "[mapped with %s] [synced with %s]\n",
940 (unsigned long long)addr, entry->size,
941 dir2name[entry->direction],
942 dir2name[direction]);
943
944 out:
945 put_hash_bucket(bucket, &flags);
946
947 }
948
949 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
950 size_t size, int direction, dma_addr_t dma_addr,
951 bool map_single)
952 {
953 struct dma_debug_entry *entry;
954
955 if (unlikely(global_disable))
956 return;
957
958 if (unlikely(dma_mapping_error(dev, dma_addr)))
959 return;
960
961 entry = dma_entry_alloc();
962 if (!entry)
963 return;
964
965 entry->dev = dev;
966 entry->type = dma_debug_page;
967 entry->paddr = page_to_phys(page) + offset;
968 entry->dev_addr = dma_addr;
969 entry->size = size;
970 entry->direction = direction;
971
972 if (map_single)
973 entry->type = dma_debug_single;
974
975 if (!PageHighMem(page)) {
976 void *addr = ((char *)page_address(page)) + offset;
977 check_for_stack(dev, addr);
978 check_for_illegal_area(dev, addr, size);
979 }
980
981 add_dma_entry(entry);
982 }
983 EXPORT_SYMBOL(debug_dma_map_page);
984
985 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
986 size_t size, int direction, bool map_single)
987 {
988 struct dma_debug_entry ref = {
989 .type = dma_debug_page,
990 .dev = dev,
991 .dev_addr = addr,
992 .size = size,
993 .direction = direction,
994 };
995
996 if (unlikely(global_disable))
997 return;
998
999 if (map_single)
1000 ref.type = dma_debug_single;
1001
1002 check_unmap(&ref);
1003 }
1004 EXPORT_SYMBOL(debug_dma_unmap_page);
1005
1006 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1007 int nents, int mapped_ents, int direction)
1008 {
1009 struct dma_debug_entry *entry;
1010 struct scatterlist *s;
1011 int i;
1012
1013 if (unlikely(global_disable))
1014 return;
1015
1016 for_each_sg(sg, s, mapped_ents, i) {
1017 entry = dma_entry_alloc();
1018 if (!entry)
1019 return;
1020
1021 entry->type = dma_debug_sg;
1022 entry->dev = dev;
1023 entry->paddr = sg_phys(s);
1024 entry->size = sg_dma_len(s);
1025 entry->dev_addr = sg_dma_address(s);
1026 entry->direction = direction;
1027 entry->sg_call_ents = nents;
1028 entry->sg_mapped_ents = mapped_ents;
1029
1030 if (!PageHighMem(sg_page(s))) {
1031 check_for_stack(dev, sg_virt(s));
1032 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
1033 }
1034
1035 add_dma_entry(entry);
1036 }
1037 }
1038 EXPORT_SYMBOL(debug_dma_map_sg);
1039
1040 static int get_nr_mapped_entries(struct device *dev, struct scatterlist *s)
1041 {
1042 struct dma_debug_entry *entry;
1043 struct hash_bucket *bucket;
1044 unsigned long flags;
1045 int mapped_ents = 0;
1046 struct dma_debug_entry ref;
1047
1048 ref.dev = dev;
1049 ref.dev_addr = sg_dma_address(s);
1050 ref.size = sg_dma_len(s),
1051
1052 bucket = get_hash_bucket(&ref, &flags);
1053 entry = hash_bucket_find(bucket, &ref);
1054 if (entry)
1055 mapped_ents = entry->sg_mapped_ents;
1056 put_hash_bucket(bucket, &flags);
1057
1058 return mapped_ents;
1059 }
1060
1061 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1062 int nelems, int dir)
1063 {
1064 struct scatterlist *s;
1065 int mapped_ents = 0, i;
1066
1067 if (unlikely(global_disable))
1068 return;
1069
1070 for_each_sg(sglist, s, nelems, i) {
1071
1072 struct dma_debug_entry ref = {
1073 .type = dma_debug_sg,
1074 .dev = dev,
1075 .paddr = sg_phys(s),
1076 .dev_addr = sg_dma_address(s),
1077 .size = sg_dma_len(s),
1078 .direction = dir,
1079 .sg_call_ents = 0,
1080 };
1081
1082 if (mapped_ents && i >= mapped_ents)
1083 break;
1084
1085 if (!i) {
1086 ref.sg_call_ents = nelems;
1087 mapped_ents = get_nr_mapped_entries(dev, s);
1088 }
1089
1090 check_unmap(&ref);
1091 }
1092 }
1093 EXPORT_SYMBOL(debug_dma_unmap_sg);
1094
1095 void debug_dma_alloc_coherent(struct device *dev, size_t size,
1096 dma_addr_t dma_addr, void *virt)
1097 {
1098 struct dma_debug_entry *entry;
1099
1100 if (unlikely(global_disable))
1101 return;
1102
1103 if (unlikely(virt == NULL))
1104 return;
1105
1106 entry = dma_entry_alloc();
1107 if (!entry)
1108 return;
1109
1110 entry->type = dma_debug_coherent;
1111 entry->dev = dev;
1112 entry->paddr = virt_to_phys(virt);
1113 entry->size = size;
1114 entry->dev_addr = dma_addr;
1115 entry->direction = DMA_BIDIRECTIONAL;
1116
1117 add_dma_entry(entry);
1118 }
1119 EXPORT_SYMBOL(debug_dma_alloc_coherent);
1120
1121 void debug_dma_free_coherent(struct device *dev, size_t size,
1122 void *virt, dma_addr_t addr)
1123 {
1124 struct dma_debug_entry ref = {
1125 .type = dma_debug_coherent,
1126 .dev = dev,
1127 .paddr = virt_to_phys(virt),
1128 .dev_addr = addr,
1129 .size = size,
1130 .direction = DMA_BIDIRECTIONAL,
1131 };
1132
1133 if (unlikely(global_disable))
1134 return;
1135
1136 check_unmap(&ref);
1137 }
1138 EXPORT_SYMBOL(debug_dma_free_coherent);
1139
1140 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1141 size_t size, int direction)
1142 {
1143 if (unlikely(global_disable))
1144 return;
1145
1146 check_sync(dev, dma_handle, size, 0, direction, true);
1147 }
1148 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1149
1150 void debug_dma_sync_single_for_device(struct device *dev,
1151 dma_addr_t dma_handle, size_t size,
1152 int direction)
1153 {
1154 if (unlikely(global_disable))
1155 return;
1156
1157 check_sync(dev, dma_handle, size, 0, direction, false);
1158 }
1159 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1160
1161 void debug_dma_sync_single_range_for_cpu(struct device *dev,
1162 dma_addr_t dma_handle,
1163 unsigned long offset, size_t size,
1164 int direction)
1165 {
1166 if (unlikely(global_disable))
1167 return;
1168
1169 check_sync(dev, dma_handle, size, offset, direction, true);
1170 }
1171 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1172
1173 void debug_dma_sync_single_range_for_device(struct device *dev,
1174 dma_addr_t dma_handle,
1175 unsigned long offset,
1176 size_t size, int direction)
1177 {
1178 if (unlikely(global_disable))
1179 return;
1180
1181 check_sync(dev, dma_handle, size, offset, direction, false);
1182 }
1183 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1184
1185 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1186 int nelems, int direction)
1187 {
1188 struct scatterlist *s;
1189 int mapped_ents = 0, i;
1190
1191 if (unlikely(global_disable))
1192 return;
1193
1194 for_each_sg(sg, s, nelems, i) {
1195 if (!i)
1196 mapped_ents = get_nr_mapped_entries(dev, s);
1197
1198 if (i >= mapped_ents)
1199 break;
1200
1201 check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
1202 direction, true);
1203 }
1204 }
1205 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1206
1207 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1208 int nelems, int direction)
1209 {
1210 struct scatterlist *s;
1211 int mapped_ents = 0, i;
1212
1213 if (unlikely(global_disable))
1214 return;
1215
1216 for_each_sg(sg, s, nelems, i) {
1217 if (!i)
1218 mapped_ents = get_nr_mapped_entries(dev, s);
1219
1220 if (i >= mapped_ents)
1221 break;
1222
1223 check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
1224 direction, false);
1225 }
1226 }
1227 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1228
1229 static int __init dma_debug_driver_setup(char *str)
1230 {
1231 int i;
1232
1233 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1234 current_driver_name[i] = *str;
1235 if (*str == 0)
1236 break;
1237 }
1238
1239 if (current_driver_name[0])
1240 printk(KERN_INFO "DMA-API: enable driver filter for "
1241 "driver [%s]\n", current_driver_name);
1242
1243
1244 return 1;
1245 }
1246 __setup("dma_debug_driver=", dma_debug_driver_setup);