]> bbs.cooldavid.org Git - net-next-2.6.git/blame - lib/dma-debug.c
Linux 2.6.33-rc1
[net-next-2.6.git] / lib / dma-debug.c
CommitLineData
f2f45e5f
JR
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
972aa45c 20#include <linux/scatterlist.h>
2d62ece1 21#include <linux/dma-mapping.h>
6c132d1b 22#include <linux/stacktrace.h>
f2f45e5f 23#include <linux/dma-debug.h>
30dfa90c 24#include <linux/spinlock.h>
788dcfa6 25#include <linux/debugfs.h>
8a6fc708 26#include <linux/uaccess.h>
2d62ece1 27#include <linux/device.h>
f2f45e5f 28#include <linux/types.h>
2d62ece1 29#include <linux/sched.h>
8a6fc708 30#include <linux/ctype.h>
f2f45e5f 31#include <linux/list.h>
6bf07871 32#include <linux/slab.h>
f2f45e5f 33
2e34bde1
JR
34#include <asm/sections.h>
35
30dfa90c
JR
36#define HASH_SIZE 1024ULL
37#define HASH_FN_SHIFT 13
38#define HASH_FN_MASK (HASH_SIZE - 1)
39
f2f45e5f
JR
40enum {
41 dma_debug_single,
42 dma_debug_page,
43 dma_debug_sg,
44 dma_debug_coherent,
45};
46
6c132d1b
DW
47#define DMA_DEBUG_STACKTRACE_ENTRIES 5
48
f2f45e5f
JR
49struct 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;
6c132d1b
DW
59#ifdef CONFIG_STACKTRACE
60 struct stack_trace stacktrace;
61 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
62#endif
f2f45e5f
JR
63};
64
30dfa90c
JR
65struct hash_bucket {
66 struct list_head list;
67 spinlock_t lock;
2d62ece1 68} ____cacheline_aligned_in_smp;
30dfa90c
JR
69
70/* Hash list to save the allocated dma addresses */
71static struct hash_bucket dma_entry_hash[HASH_SIZE];
3b1e79ed
JR
72/* List of pre-allocated dma_debug_entry's */
73static LIST_HEAD(free_entries);
74/* Lock for the list above */
75static DEFINE_SPINLOCK(free_entries_lock);
76
77/* Global disable flag - will be set in case of an error */
78static bool global_disable __read_mostly;
79
788dcfa6
JR
80/* Global error count */
81static u32 error_count;
82
83/* Global error show enable*/
84static u32 show_all_errors __read_mostly;
85/* Number of errors to show */
86static u32 show_num_errors = 1;
87
3b1e79ed
JR
88static u32 num_free_entries;
89static u32 min_free_entries;
e6a1a89d 90static u32 nr_total_entries;
30dfa90c 91
59d3daaf
JR
92/* number of preallocated entries requested by kernel cmdline */
93static u32 req_entries;
94
788dcfa6
JR
95/* debugfs dentry's for the stuff above */
96static struct dentry *dma_debug_dent __read_mostly;
97static struct dentry *global_disable_dent __read_mostly;
98static struct dentry *error_count_dent __read_mostly;
99static struct dentry *show_all_errors_dent __read_mostly;
100static struct dentry *show_num_errors_dent __read_mostly;
101static struct dentry *num_free_entries_dent __read_mostly;
102static struct dentry *min_free_entries_dent __read_mostly;
8a6fc708 103static struct dentry *filter_dent __read_mostly;
788dcfa6 104
2e507d84
JR
105/* per-driver filter related state */
106
107#define NAME_MAX_LEN 64
108
109static char current_driver_name[NAME_MAX_LEN] __read_mostly;
110static struct device_driver *current_driver __read_mostly;
111
112static DEFINE_RWLOCK(driver_name_lock);
788dcfa6 113
2d62ece1
JR
114static const char *type2name[4] = { "single", "page",
115 "scather-gather", "coherent" };
116
117static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
118 "DMA_FROM_DEVICE", "DMA_NONE" };
119
ed888aef
JR
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
2d62ece1
JR
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 */
6c132d1b
DW
138static inline void dump_entry_trace(struct dma_debug_entry *entry)
139{
140#ifdef CONFIG_STACKTRACE
141 if (entry) {
e7ed70ee 142 pr_warning("Mapped at:\n");
6c132d1b
DW
143 print_stack_trace(&entry->stacktrace, 0);
144 }
145#endif
146}
147
2e507d84
JR
148static bool driver_filter(struct device *dev)
149{
0bf84128
JR
150 struct device_driver *drv;
151 unsigned long flags;
152 bool ret;
153
2e507d84
JR
154 /* driver filter off */
155 if (likely(!current_driver_name[0]))
156 return true;
157
158 /* driver filter on and initialized */
ec9c96ef 159 if (current_driver && dev && dev->driver == current_driver)
2e507d84
JR
160 return true;
161
ec9c96ef
KM
162 /* driver filter on, but we can't filter on a NULL device... */
163 if (!dev)
164 return false;
165
0bf84128
JR
166 if (current_driver || !current_driver_name[0])
167 return false;
2e507d84 168
0bf84128
JR
169 /* driver filter on but not yet initialized */
170 drv = get_driver(dev->driver);
171 if (!drv)
172 return false;
173
174 /* lock to protect against change of current_driver_name */
175 read_lock_irqsave(&driver_name_lock, flags);
176
177 ret = false;
178 if (drv->name &&
179 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
180 current_driver = drv;
181 ret = true;
2e507d84
JR
182 }
183
0bf84128
JR
184 read_unlock_irqrestore(&driver_name_lock, flags);
185 put_driver(drv);
186
187 return ret;
2e507d84
JR
188}
189
ec9c96ef
KM
190#define err_printk(dev, entry, format, arg...) do { \
191 error_count += 1; \
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); \
198 } \
199 if (!show_all_errors && show_num_errors > 0) \
200 show_num_errors -= 1; \
2d62ece1
JR
201 } while (0);
202
30dfa90c
JR
203/*
204 * Hash related functions
205 *
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.
208 */
209static int hash_fn(struct dma_debug_entry *entry)
210{
211 /*
212 * Hash function is based on the dma address.
213 * We use bits 20-27 here as the index into the hash
214 */
215 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
216}
217
218/*
219 * Request exclusive access to a hash bucket for a given dma_debug_entry.
220 */
221static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
222 unsigned long *flags)
223{
224 int idx = hash_fn(entry);
225 unsigned long __flags;
226
227 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
228 *flags = __flags;
229 return &dma_entry_hash[idx];
230}
231
232/*
233 * Give up exclusive access to the hash bucket
234 */
235static void put_hash_bucket(struct hash_bucket *bucket,
236 unsigned long *flags)
237{
238 unsigned long __flags = *flags;
239
240 spin_unlock_irqrestore(&bucket->lock, __flags);
241}
242
243/*
244 * Search a given entry in the hash bucket list
245 */
246static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
247 struct dma_debug_entry *ref)
248{
7caf6a49
JR
249 struct dma_debug_entry *entry, *ret = NULL;
250 int matches = 0, match_lvl, last_lvl = 0;
30dfa90c
JR
251
252 list_for_each_entry(entry, &bucket->list, list) {
7caf6a49
JR
253 if ((entry->dev_addr != ref->dev_addr) ||
254 (entry->dev != ref->dev))
255 continue;
256
257 /*
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
af901ca1 262 * positives being reported. Therefore we implement a
7caf6a49
JR
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.
266 */
267 matches += 1;
268 match_lvl = 0;
e5e8c5b9
JR
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;
7caf6a49 273
e5e8c5b9 274 if (match_lvl == 4) {
7caf6a49 275 /* perfect-fit - return the result */
30dfa90c 276 return entry;
7caf6a49
JR
277 } else if (match_lvl > last_lvl) {
278 /*
279 * We found an entry that fits better then the
280 * previous one
281 */
282 last_lvl = match_lvl;
283 ret = entry;
284 }
30dfa90c
JR
285 }
286
7caf6a49
JR
287 /*
288 * If we have multiple matches but no perfect-fit, just return
289 * NULL.
290 */
291 ret = (matches == 1) ? ret : NULL;
292
293 return ret;
30dfa90c
JR
294}
295
296/*
297 * Add an entry to a hash bucket
298 */
299static void hash_bucket_add(struct hash_bucket *bucket,
300 struct dma_debug_entry *entry)
301{
302 list_add_tail(&entry->list, &bucket->list);
303}
304
305/*
306 * Remove entry from a hash bucket list
307 */
308static void hash_bucket_del(struct dma_debug_entry *entry)
309{
310 list_del(&entry->list);
311}
312
ac26c18b
DW
313/*
314 * Dump mapping entries for debugging purposes
315 */
316void debug_dma_dump_mappings(struct device *dev)
317{
318 int idx;
319
320 for (idx = 0; idx < HASH_SIZE; idx++) {
321 struct hash_bucket *bucket = &dma_entry_hash[idx];
322 struct dma_debug_entry *entry;
323 unsigned long flags;
324
325 spin_lock_irqsave(&bucket->lock, flags);
326
327 list_for_each_entry(entry, &bucket->list, list) {
328 if (!dev || dev == entry->dev) {
329 dev_info(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]);
335 }
336 }
337
338 spin_unlock_irqrestore(&bucket->lock, flags);
339 }
340}
341EXPORT_SYMBOL(debug_dma_dump_mappings);
342
30dfa90c
JR
343/*
344 * Wrapper function for adding an entry to the hash.
345 * This function takes care of locking itself.
346 */
347static void add_dma_entry(struct dma_debug_entry *entry)
348{
349 struct hash_bucket *bucket;
350 unsigned long flags;
351
352 bucket = get_hash_bucket(entry, &flags);
353 hash_bucket_add(bucket, entry);
354 put_hash_bucket(bucket, &flags);
355}
356
e6a1a89d
FT
357static struct dma_debug_entry *__dma_entry_alloc(void)
358{
359 struct dma_debug_entry *entry;
360
361 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
362 list_del(&entry->list);
363 memset(entry, 0, sizeof(*entry));
364
365 num_free_entries -= 1;
366 if (num_free_entries < min_free_entries)
367 min_free_entries = num_free_entries;
368
369 return entry;
370}
371
3b1e79ed
JR
372/* struct dma_entry allocator
373 *
374 * The next two functions implement the allocator for
375 * struct dma_debug_entries.
376 */
377static struct dma_debug_entry *dma_entry_alloc(void)
378{
379 struct dma_debug_entry *entry = NULL;
380 unsigned long flags;
381
382 spin_lock_irqsave(&free_entries_lock, flags);
383
384 if (list_empty(&free_entries)) {
e7ed70ee 385 pr_err("DMA-API: debugging out of memory - disabling\n");
3b1e79ed
JR
386 global_disable = true;
387 goto out;
388 }
389
e6a1a89d 390 entry = __dma_entry_alloc();
3b1e79ed 391
6c132d1b
DW
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);
397#endif
3b1e79ed
JR
398
399out:
400 spin_unlock_irqrestore(&free_entries_lock, flags);
401
402 return entry;
403}
404
405static void dma_entry_free(struct dma_debug_entry *entry)
406{
407 unsigned long flags;
408
409 /*
410 * add to beginning of the list - this way the entries are
411 * more likely cache hot when they are reallocated.
412 */
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);
417}
418
e6a1a89d
FT
419int dma_debug_resize_entries(u32 num_entries)
420{
421 int i, delta, ret = 0;
422 unsigned long flags;
423 struct dma_debug_entry *entry;
424 LIST_HEAD(tmp);
425
426 spin_lock_irqsave(&free_entries_lock, flags);
427
428 if (nr_total_entries < num_entries) {
429 delta = num_entries - nr_total_entries;
430
431 spin_unlock_irqrestore(&free_entries_lock, flags);
432
433 for (i = 0; i < delta; i++) {
434 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
435 if (!entry)
436 break;
437
438 list_add_tail(&entry->list, &tmp);
439 }
440
441 spin_lock_irqsave(&free_entries_lock, flags);
442
443 list_splice(&tmp, &free_entries);
444 nr_total_entries += i;
445 num_free_entries += i;
446 } else {
447 delta = nr_total_entries - num_entries;
448
449 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
450 entry = __dma_entry_alloc();
451 kfree(entry);
452 }
453
454 nr_total_entries -= i;
455 }
456
457 if (nr_total_entries != num_entries)
458 ret = 1;
459
460 spin_unlock_irqrestore(&free_entries_lock, flags);
461
462 return ret;
463}
464EXPORT_SYMBOL(dma_debug_resize_entries);
465
6bf07871
JR
466/*
467 * DMA-API debugging init code
468 *
469 * The init code does two things:
470 * 1. Initialize core data structures
471 * 2. Preallocate a given number of dma_debug_entry structs
472 */
473
474static int prealloc_memory(u32 num_entries)
475{
476 struct dma_debug_entry *entry, *next_entry;
477 int i;
478
479 for (i = 0; i < num_entries; ++i) {
480 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
481 if (!entry)
482 goto out_err;
483
484 list_add_tail(&entry->list, &free_entries);
485 }
486
487 num_free_entries = num_entries;
488 min_free_entries = num_entries;
489
e7ed70ee 490 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
6bf07871
JR
491
492 return 0;
493
494out_err:
495
496 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
497 list_del(&entry->list);
498 kfree(entry);
499 }
500
501 return -ENOMEM;
502}
503
8a6fc708
JR
504static ssize_t filter_read(struct file *file, char __user *user_buf,
505 size_t count, loff_t *ppos)
506{
8a6fc708 507 char buf[NAME_MAX_LEN + 1];
c17e2cf7 508 unsigned long flags;
8a6fc708
JR
509 int len;
510
511 if (!current_driver_name[0])
512 return 0;
513
514 /*
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.
518 */
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);
522
523 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
524}
525
526static ssize_t filter_write(struct file *file, const char __user *userbuf,
527 size_t count, loff_t *ppos)
528{
8a6fc708 529 char buf[NAME_MAX_LEN];
c17e2cf7
JR
530 unsigned long flags;
531 size_t len;
8a6fc708
JR
532 int i;
533
534 /*
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
539 */
e7ed70ee 540 len = min(count, (size_t)(NAME_MAX_LEN - 1));
8a6fc708
JR
541 if (copy_from_user(buf, userbuf, len))
542 return -EFAULT;
543
544 buf[len] = 0;
545
546 write_lock_irqsave(&driver_name_lock, flags);
547
31232509
JR
548 /*
549 * Now handle the string we got from userspace very carefully.
8a6fc708
JR
550 * The rules are:
551 * - only use the first token we got
552 * - token delimiter is everything looking like a space
553 * character (' ', '\n', '\t' ...)
554 *
555 */
556 if (!isalnum(buf[0])) {
557 /*
31232509 558 * If the first character userspace gave us is not
8a6fc708
JR
559 * alphanumerical then assume the filter should be
560 * switched off.
561 */
562 if (current_driver_name[0])
e7ed70ee 563 pr_info("DMA-API: switching off dma-debug driver filter\n");
8a6fc708
JR
564 current_driver_name[0] = 0;
565 current_driver = NULL;
566 goto out_unlock;
567 }
568
569 /*
570 * Now parse out the first token and use it as the name for the
571 * driver to filter for.
572 */
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)
576 break;
577 }
578 current_driver_name[i] = 0;
579 current_driver = NULL;
580
e7ed70ee
JR
581 pr_info("DMA-API: enable driver filter for driver [%s]\n",
582 current_driver_name);
8a6fc708
JR
583
584out_unlock:
585 write_unlock_irqrestore(&driver_name_lock, flags);
586
587 return count;
588}
589
590const struct file_operations filter_fops = {
591 .read = filter_read,
592 .write = filter_write,
593};
594
788dcfa6
JR
595static int dma_debug_fs_init(void)
596{
597 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
598 if (!dma_debug_dent) {
e7ed70ee 599 pr_err("DMA-API: can not create debugfs directory\n");
788dcfa6
JR
600 return -ENOMEM;
601 }
602
603 global_disable_dent = debugfs_create_bool("disabled", 0444,
604 dma_debug_dent,
605 (u32 *)&global_disable);
606 if (!global_disable_dent)
607 goto out_err;
608
609 error_count_dent = debugfs_create_u32("error_count", 0444,
610 dma_debug_dent, &error_count);
611 if (!error_count_dent)
612 goto out_err;
613
614 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
615 dma_debug_dent,
616 &show_all_errors);
617 if (!show_all_errors_dent)
618 goto out_err;
619
620 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
621 dma_debug_dent,
622 &show_num_errors);
623 if (!show_num_errors_dent)
624 goto out_err;
625
626 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
627 dma_debug_dent,
628 &num_free_entries);
629 if (!num_free_entries_dent)
630 goto out_err;
631
632 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
633 dma_debug_dent,
634 &min_free_entries);
635 if (!min_free_entries_dent)
636 goto out_err;
637
8a6fc708
JR
638 filter_dent = debugfs_create_file("driver_filter", 0644,
639 dma_debug_dent, NULL, &filter_fops);
640 if (!filter_dent)
641 goto out_err;
642
788dcfa6
JR
643 return 0;
644
645out_err:
646 debugfs_remove_recursive(dma_debug_dent);
647
648 return -ENOMEM;
649}
650
ed888aef
JR
651static int device_dma_allocations(struct device *dev)
652{
653 struct dma_debug_entry *entry;
654 unsigned long flags;
655 int count = 0, i;
656
be81c6ea
JR
657 local_irq_save(flags);
658
ed888aef 659 for (i = 0; i < HASH_SIZE; ++i) {
be81c6ea 660 spin_lock(&dma_entry_hash[i].lock);
ed888aef
JR
661 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
662 if (entry->dev == dev)
663 count += 1;
664 }
be81c6ea 665 spin_unlock(&dma_entry_hash[i].lock);
ed888aef
JR
666 }
667
be81c6ea
JR
668 local_irq_restore(flags);
669
ed888aef
JR
670 return count;
671}
672
673static int dma_debug_device_change(struct notifier_block *nb,
674 unsigned long action, void *data)
675{
676 struct device *dev = data;
677 int count;
678
679
680 switch (action) {
681 case BUS_NOTIFY_UNBOUND_DRIVER:
682 count = device_dma_allocations(dev);
683 if (count == 0)
684 break;
685 err_printk(dev, NULL, "DMA-API: device driver has pending "
686 "DMA allocations while released from device "
687 "[count=%d]\n", count);
688 break;
689 default:
690 break;
691 }
692
693 return 0;
694}
695
41531c8f
JR
696void dma_debug_add_bus(struct bus_type *bus)
697{
ed888aef
JR
698 struct notifier_block *nb;
699
700 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
701 if (nb == NULL) {
e7ed70ee 702 pr_err("dma_debug_add_bus: out of memory\n");
ed888aef
JR
703 return;
704 }
705
706 nb->notifier_call = dma_debug_device_change;
707
708 bus_register_notifier(bus, nb);
41531c8f 709}
788dcfa6 710
6bf07871
JR
711/*
712 * Let the architectures decide how many entries should be preallocated.
713 */
714void dma_debug_init(u32 num_entries)
715{
716 int i;
717
718 if (global_disable)
719 return;
720
721 for (i = 0; i < HASH_SIZE; ++i) {
722 INIT_LIST_HEAD(&dma_entry_hash[i].list);
b0a5b83e 723 spin_lock_init(&dma_entry_hash[i].lock);
6bf07871
JR
724 }
725
788dcfa6 726 if (dma_debug_fs_init() != 0) {
e7ed70ee 727 pr_err("DMA-API: error creating debugfs entries - disabling\n");
788dcfa6
JR
728 global_disable = true;
729
730 return;
731 }
732
59d3daaf
JR
733 if (req_entries)
734 num_entries = req_entries;
735
6bf07871 736 if (prealloc_memory(num_entries) != 0) {
e7ed70ee 737 pr_err("DMA-API: debugging out of memory error - disabled\n");
6bf07871
JR
738 global_disable = true;
739
740 return;
741 }
742
e6a1a89d
FT
743 nr_total_entries = num_free_entries;
744
e7ed70ee 745 pr_info("DMA-API: debugging enabled by kernel config\n");
6bf07871
JR
746}
747
59d3daaf
JR
748static __init int dma_debug_cmdline(char *str)
749{
750 if (!str)
751 return -EINVAL;
752
753 if (strncmp(str, "off", 3) == 0) {
e7ed70ee 754 pr_info("DMA-API: debugging disabled on kernel command line\n");
59d3daaf
JR
755 global_disable = true;
756 }
757
758 return 0;
759}
760
761static __init int dma_debug_entries_cmdline(char *str)
762{
763 int res;
764
765 if (!str)
766 return -EINVAL;
767
768 res = get_option(&str, &req_entries);
769
770 if (!res)
771 req_entries = 0;
772
773 return 0;
774}
775
776__setup("dma_debug=", dma_debug_cmdline);
777__setup("dma_debug_entries=", dma_debug_entries_cmdline);
778
2d62ece1
JR
779static void check_unmap(struct dma_debug_entry *ref)
780{
781 struct dma_debug_entry *entry;
782 struct hash_bucket *bucket;
783 unsigned long flags;
784
35d40952
FT
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");
2d62ece1 788 return;
35d40952 789 }
2d62ece1
JR
790
791 bucket = get_hash_bucket(ref, &flags);
792 entry = hash_bucket_find(bucket, ref);
793
794 if (!entry) {
6c132d1b 795 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
2d62ece1
JR
796 "to free DMA memory it has not allocated "
797 "[device address=0x%016llx] [size=%llu bytes]\n",
798 ref->dev_addr, ref->size);
799 goto out;
800 }
801
802 if (ref->size != entry->size) {
6c132d1b 803 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
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);
808 }
809
810 if (ref->type != entry->type) {
6c132d1b 811 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
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)) {
6c132d1b 819 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
820 "DMA memory with different CPU address "
821 "[device address=0x%016llx] [size=%llu bytes] "
59a40e70
JR
822 "[cpu alloc address=0x%016llx] "
823 "[cpu free address=0x%016llx]",
2d62ece1 824 ref->dev_addr, ref->size,
59a40e70
JR
825 (unsigned long long)entry->paddr,
826 (unsigned long long)ref->paddr);
2d62ece1
JR
827 }
828
829 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
830 ref->sg_call_ents != entry->sg_call_ents) {
6c132d1b 831 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
832 "DMA sg list with different entry count "
833 "[map count=%d] [unmap count=%d]\n",
834 entry->sg_call_ents, ref->sg_call_ents);
835 }
836
837 /*
838 * This may be no bug in reality - but most implementations of the
839 * DMA API don't handle this properly, so check for it here
840 */
841 if (ref->direction != entry->direction) {
6c132d1b 842 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
843 "DMA memory with different direction "
844 "[device address=0x%016llx] [size=%llu bytes] "
845 "[mapped with %s] [unmapped with %s]\n",
846 ref->dev_addr, ref->size,
847 dir2name[entry->direction],
848 dir2name[ref->direction]);
849 }
850
851 hash_bucket_del(entry);
852 dma_entry_free(entry);
853
854out:
855 put_hash_bucket(bucket, &flags);
856}
857
858static void check_for_stack(struct device *dev, void *addr)
859{
860 if (object_is_on_stack(addr))
6c132d1b
DW
861 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
862 "stack [addr=%p]\n", addr);
2d62ece1
JR
863}
864
f39d1b97 865static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
2e34bde1 866{
f39d1b97
IM
867 unsigned long a1 = (unsigned long)addr;
868 unsigned long b1 = a1 + len;
869 unsigned long a2 = (unsigned long)start;
870 unsigned long b2 = (unsigned long)end;
2e34bde1 871
f39d1b97 872 return !(b1 <= a2 || a1 >= b2);
2e34bde1
JR
873}
874
f39d1b97 875static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
2e34bde1 876{
f39d1b97
IM
877 if (overlap(addr, len, _text, _etext) ||
878 overlap(addr, len, __start_rodata, __end_rodata))
879 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
2e34bde1
JR
880}
881
aa010efb
JR
882static void check_sync(struct device *dev,
883 struct dma_debug_entry *ref,
884 bool to_cpu)
2d62ece1 885{
2d62ece1
JR
886 struct dma_debug_entry *entry;
887 struct hash_bucket *bucket;
888 unsigned long flags;
889
aa010efb 890 bucket = get_hash_bucket(ref, &flags);
2d62ece1 891
aa010efb 892 entry = hash_bucket_find(bucket, ref);
2d62ece1
JR
893
894 if (!entry) {
6c132d1b 895 err_printk(dev, NULL, "DMA-API: device driver tries "
2d62ece1
JR
896 "to sync DMA memory it has not allocated "
897 "[device address=0x%016llx] [size=%llu bytes]\n",
aa010efb 898 (unsigned long long)ref->dev_addr, ref->size);
2d62ece1
JR
899 goto out;
900 }
901
aa010efb 902 if (ref->size > entry->size) {
6c132d1b 903 err_printk(dev, entry, "DMA-API: device driver syncs"
2d62ece1
JR
904 " DMA memory outside allocated range "
905 "[device address=0x%016llx] "
aa010efb
JR
906 "[allocation size=%llu bytes] "
907 "[sync offset+size=%llu]\n",
908 entry->dev_addr, entry->size,
909 ref->size);
2d62ece1
JR
910 }
911
aa010efb 912 if (ref->direction != entry->direction) {
6c132d1b 913 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
914 "DMA memory with different direction "
915 "[device address=0x%016llx] [size=%llu bytes] "
916 "[mapped with %s] [synced with %s]\n",
aa010efb 917 (unsigned long long)ref->dev_addr, entry->size,
2d62ece1 918 dir2name[entry->direction],
aa010efb 919 dir2name[ref->direction]);
2d62ece1
JR
920 }
921
922 if (entry->direction == DMA_BIDIRECTIONAL)
923 goto out;
924
925 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
aa010efb 926 !(ref->direction == DMA_TO_DEVICE))
6c132d1b 927 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
928 "device read-only DMA memory for cpu "
929 "[device address=0x%016llx] [size=%llu bytes] "
930 "[mapped with %s] [synced with %s]\n",
aa010efb 931 (unsigned long long)ref->dev_addr, entry->size,
2d62ece1 932 dir2name[entry->direction],
aa010efb 933 dir2name[ref->direction]);
2d62ece1
JR
934
935 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
aa010efb 936 !(ref->direction == DMA_FROM_DEVICE))
6c132d1b 937 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
938 "device write-only DMA memory to device "
939 "[device address=0x%016llx] [size=%llu bytes] "
940 "[mapped with %s] [synced with %s]\n",
aa010efb 941 (unsigned long long)ref->dev_addr, entry->size,
2d62ece1 942 dir2name[entry->direction],
aa010efb 943 dir2name[ref->direction]);
2d62ece1
JR
944
945out:
946 put_hash_bucket(bucket, &flags);
947
948}
949
f62bc980
JR
950void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
951 size_t size, int direction, dma_addr_t dma_addr,
952 bool map_single)
953{
954 struct dma_debug_entry *entry;
955
956 if (unlikely(global_disable))
957 return;
958
959 if (unlikely(dma_mapping_error(dev, dma_addr)))
960 return;
961
962 entry = dma_entry_alloc();
963 if (!entry)
964 return;
965
966 entry->dev = dev;
967 entry->type = dma_debug_page;
968 entry->paddr = page_to_phys(page) + offset;
969 entry->dev_addr = dma_addr;
970 entry->size = size;
971 entry->direction = direction;
972
9537a48e 973 if (map_single)
f62bc980 974 entry->type = dma_debug_single;
9537a48e
JR
975
976 if (!PageHighMem(page)) {
f39d1b97
IM
977 void *addr = page_address(page) + offset;
978
2e34bde1
JR
979 check_for_stack(dev, addr);
980 check_for_illegal_area(dev, addr, size);
f62bc980
JR
981 }
982
983 add_dma_entry(entry);
984}
985EXPORT_SYMBOL(debug_dma_map_page);
986
987void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
988 size_t size, int direction, bool map_single)
989{
990 struct dma_debug_entry ref = {
991 .type = dma_debug_page,
992 .dev = dev,
993 .dev_addr = addr,
994 .size = size,
995 .direction = direction,
996 };
997
998 if (unlikely(global_disable))
999 return;
1000
1001 if (map_single)
1002 ref.type = dma_debug_single;
1003
1004 check_unmap(&ref);
1005}
1006EXPORT_SYMBOL(debug_dma_unmap_page);
1007
972aa45c
JR
1008void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1009 int nents, int mapped_ents, int direction)
1010{
1011 struct dma_debug_entry *entry;
1012 struct scatterlist *s;
1013 int i;
1014
1015 if (unlikely(global_disable))
1016 return;
1017
1018 for_each_sg(sg, s, mapped_ents, i) {
1019 entry = dma_entry_alloc();
1020 if (!entry)
1021 return;
1022
1023 entry->type = dma_debug_sg;
1024 entry->dev = dev;
1025 entry->paddr = sg_phys(s);
884d0597 1026 entry->size = sg_dma_len(s);
15aedea4 1027 entry->dev_addr = sg_dma_address(s);
972aa45c
JR
1028 entry->direction = direction;
1029 entry->sg_call_ents = nents;
1030 entry->sg_mapped_ents = mapped_ents;
1031
9537a48e
JR
1032 if (!PageHighMem(sg_page(s))) {
1033 check_for_stack(dev, sg_virt(s));
884d0597 1034 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
9537a48e 1035 }
972aa45c
JR
1036
1037 add_dma_entry(entry);
1038 }
1039}
1040EXPORT_SYMBOL(debug_dma_map_sg);
1041
aa010efb
JR
1042static int get_nr_mapped_entries(struct device *dev,
1043 struct dma_debug_entry *ref)
88f3907f 1044{
aa010efb 1045 struct dma_debug_entry *entry;
88f3907f
FT
1046 struct hash_bucket *bucket;
1047 unsigned long flags;
c17e2cf7 1048 int mapped_ents;
88f3907f 1049
aa010efb
JR
1050 bucket = get_hash_bucket(ref, &flags);
1051 entry = hash_bucket_find(bucket, ref);
c17e2cf7 1052 mapped_ents = 0;
88f3907f 1053
88f3907f
FT
1054 if (entry)
1055 mapped_ents = entry->sg_mapped_ents;
1056 put_hash_bucket(bucket, &flags);
1057
1058 return mapped_ents;
1059}
1060
972aa45c
JR
1061void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1062 int nelems, int dir)
1063{
972aa45c
JR
1064 struct scatterlist *s;
1065 int mapped_ents = 0, i;
972aa45c
JR
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),
15aedea4 1076 .dev_addr = sg_dma_address(s),
884d0597 1077 .size = sg_dma_len(s),
972aa45c 1078 .direction = dir,
e5e8c5b9 1079 .sg_call_ents = nelems,
972aa45c
JR
1080 };
1081
1082 if (mapped_ents && i >= mapped_ents)
1083 break;
1084
e5e8c5b9 1085 if (!i)
aa010efb 1086 mapped_ents = get_nr_mapped_entries(dev, &ref);
972aa45c
JR
1087
1088 check_unmap(&ref);
1089 }
1090}
1091EXPORT_SYMBOL(debug_dma_unmap_sg);
1092
6bfd4498
JR
1093void debug_dma_alloc_coherent(struct device *dev, size_t size,
1094 dma_addr_t dma_addr, void *virt)
1095{
1096 struct dma_debug_entry *entry;
1097
1098 if (unlikely(global_disable))
1099 return;
1100
1101 if (unlikely(virt == NULL))
1102 return;
1103
1104 entry = dma_entry_alloc();
1105 if (!entry)
1106 return;
1107
1108 entry->type = dma_debug_coherent;
1109 entry->dev = dev;
1110 entry->paddr = virt_to_phys(virt);
1111 entry->size = size;
1112 entry->dev_addr = dma_addr;
1113 entry->direction = DMA_BIDIRECTIONAL;
1114
1115 add_dma_entry(entry);
1116}
1117EXPORT_SYMBOL(debug_dma_alloc_coherent);
1118
1119void debug_dma_free_coherent(struct device *dev, size_t size,
1120 void *virt, dma_addr_t addr)
1121{
1122 struct dma_debug_entry ref = {
1123 .type = dma_debug_coherent,
1124 .dev = dev,
1125 .paddr = virt_to_phys(virt),
1126 .dev_addr = addr,
1127 .size = size,
1128 .direction = DMA_BIDIRECTIONAL,
1129 };
1130
1131 if (unlikely(global_disable))
1132 return;
1133
1134 check_unmap(&ref);
1135}
1136EXPORT_SYMBOL(debug_dma_free_coherent);
1137
b9d2317e
JR
1138void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1139 size_t size, int direction)
1140{
aa010efb
JR
1141 struct dma_debug_entry ref;
1142
b9d2317e
JR
1143 if (unlikely(global_disable))
1144 return;
1145
aa010efb
JR
1146 ref.type = dma_debug_single;
1147 ref.dev = dev;
1148 ref.dev_addr = dma_handle;
1149 ref.size = size;
1150 ref.direction = direction;
1151 ref.sg_call_ents = 0;
1152
1153 check_sync(dev, &ref, true);
b9d2317e
JR
1154}
1155EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1156
1157void debug_dma_sync_single_for_device(struct device *dev,
1158 dma_addr_t dma_handle, size_t size,
1159 int direction)
1160{
aa010efb
JR
1161 struct dma_debug_entry ref;
1162
b9d2317e
JR
1163 if (unlikely(global_disable))
1164 return;
1165
aa010efb
JR
1166 ref.type = dma_debug_single;
1167 ref.dev = dev;
1168 ref.dev_addr = dma_handle;
1169 ref.size = size;
1170 ref.direction = direction;
1171 ref.sg_call_ents = 0;
1172
1173 check_sync(dev, &ref, false);
b9d2317e
JR
1174}
1175EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1176
948408ba
JR
1177void debug_dma_sync_single_range_for_cpu(struct device *dev,
1178 dma_addr_t dma_handle,
1179 unsigned long offset, size_t size,
1180 int direction)
1181{
aa010efb
JR
1182 struct dma_debug_entry ref;
1183
948408ba
JR
1184 if (unlikely(global_disable))
1185 return;
1186
aa010efb
JR
1187 ref.type = dma_debug_single;
1188 ref.dev = dev;
1189 ref.dev_addr = dma_handle;
1190 ref.size = offset + size;
1191 ref.direction = direction;
1192 ref.sg_call_ents = 0;
1193
1194 check_sync(dev, &ref, true);
948408ba
JR
1195}
1196EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1197
1198void debug_dma_sync_single_range_for_device(struct device *dev,
1199 dma_addr_t dma_handle,
1200 unsigned long offset,
1201 size_t size, int direction)
1202{
aa010efb
JR
1203 struct dma_debug_entry ref;
1204
948408ba
JR
1205 if (unlikely(global_disable))
1206 return;
1207
aa010efb
JR
1208 ref.type = dma_debug_single;
1209 ref.dev = dev;
1210 ref.dev_addr = dma_handle;
1211 ref.size = offset + size;
1212 ref.direction = direction;
1213 ref.sg_call_ents = 0;
1214
1215 check_sync(dev, &ref, false);
948408ba
JR
1216}
1217EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1218
a31fba5d
JR
1219void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1220 int nelems, int direction)
1221{
1222 struct scatterlist *s;
88f3907f 1223 int mapped_ents = 0, i;
a31fba5d
JR
1224
1225 if (unlikely(global_disable))
1226 return;
1227
1228 for_each_sg(sg, s, nelems, i) {
aa010efb
JR
1229
1230 struct dma_debug_entry ref = {
1231 .type = dma_debug_sg,
1232 .dev = dev,
1233 .paddr = sg_phys(s),
1234 .dev_addr = sg_dma_address(s),
1235 .size = sg_dma_len(s),
1236 .direction = direction,
1237 .sg_call_ents = nelems,
1238 };
1239
88f3907f 1240 if (!i)
aa010efb 1241 mapped_ents = get_nr_mapped_entries(dev, &ref);
88f3907f
FT
1242
1243 if (i >= mapped_ents)
1244 break;
1245
aa010efb 1246 check_sync(dev, &ref, true);
a31fba5d
JR
1247 }
1248}
1249EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1250
1251void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1252 int nelems, int direction)
1253{
1254 struct scatterlist *s;
88f3907f 1255 int mapped_ents = 0, i;
a31fba5d
JR
1256
1257 if (unlikely(global_disable))
1258 return;
1259
1260 for_each_sg(sg, s, nelems, i) {
aa010efb
JR
1261
1262 struct dma_debug_entry ref = {
1263 .type = dma_debug_sg,
1264 .dev = dev,
1265 .paddr = sg_phys(s),
1266 .dev_addr = sg_dma_address(s),
1267 .size = sg_dma_len(s),
1268 .direction = direction,
1269 .sg_call_ents = nelems,
1270 };
88f3907f 1271 if (!i)
aa010efb 1272 mapped_ents = get_nr_mapped_entries(dev, &ref);
88f3907f
FT
1273
1274 if (i >= mapped_ents)
1275 break;
1276
aa010efb 1277 check_sync(dev, &ref, false);
a31fba5d
JR
1278 }
1279}
1280EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1281
1745de5e
JR
1282static int __init dma_debug_driver_setup(char *str)
1283{
1284 int i;
1285
1286 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1287 current_driver_name[i] = *str;
1288 if (*str == 0)
1289 break;
1290 }
1291
1292 if (current_driver_name[0])
e7ed70ee
JR
1293 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1294 current_driver_name);
1745de5e
JR
1295
1296
1297 return 1;
1298}
1299__setup("dma_debug_driver=", dma_debug_driver_setup);