]> bbs.cooldavid.org Git - net-next-2.6.git/blame - lib/dma-debug.c
dma-debug: warn of unmapping an invalid dma address
[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>
2d62ece1 26#include <linux/device.h>
f2f45e5f 27#include <linux/types.h>
2d62ece1 28#include <linux/sched.h>
f2f45e5f 29#include <linux/list.h>
6bf07871 30#include <linux/slab.h>
f2f45e5f 31
2e34bde1
JR
32#include <asm/sections.h>
33
30dfa90c
JR
34#define HASH_SIZE 1024ULL
35#define HASH_FN_SHIFT 13
36#define HASH_FN_MASK (HASH_SIZE - 1)
37
f2f45e5f
JR
38enum {
39 dma_debug_single,
40 dma_debug_page,
41 dma_debug_sg,
42 dma_debug_coherent,
43};
44
6c132d1b
DW
45#define DMA_DEBUG_STACKTRACE_ENTRIES 5
46
f2f45e5f
JR
47struct dma_debug_entry {
48 struct list_head list;
49 struct device *dev;
50 int type;
51 phys_addr_t paddr;
52 u64 dev_addr;
53 u64 size;
54 int direction;
55 int sg_call_ents;
56 int sg_mapped_ents;
6c132d1b
DW
57#ifdef CONFIG_STACKTRACE
58 struct stack_trace stacktrace;
59 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
60#endif
f2f45e5f
JR
61};
62
30dfa90c
JR
63struct hash_bucket {
64 struct list_head list;
65 spinlock_t lock;
2d62ece1 66} ____cacheline_aligned_in_smp;
30dfa90c
JR
67
68/* Hash list to save the allocated dma addresses */
69static struct hash_bucket dma_entry_hash[HASH_SIZE];
3b1e79ed
JR
70/* List of pre-allocated dma_debug_entry's */
71static LIST_HEAD(free_entries);
72/* Lock for the list above */
73static DEFINE_SPINLOCK(free_entries_lock);
74
75/* Global disable flag - will be set in case of an error */
76static bool global_disable __read_mostly;
77
788dcfa6
JR
78/* Global error count */
79static u32 error_count;
80
81/* Global error show enable*/
82static u32 show_all_errors __read_mostly;
83/* Number of errors to show */
84static u32 show_num_errors = 1;
85
3b1e79ed
JR
86static u32 num_free_entries;
87static u32 min_free_entries;
30dfa90c 88
59d3daaf
JR
89/* number of preallocated entries requested by kernel cmdline */
90static u32 req_entries;
91
788dcfa6
JR
92/* debugfs dentry's for the stuff above */
93static struct dentry *dma_debug_dent __read_mostly;
94static struct dentry *global_disable_dent __read_mostly;
95static struct dentry *error_count_dent __read_mostly;
96static struct dentry *show_all_errors_dent __read_mostly;
97static struct dentry *show_num_errors_dent __read_mostly;
98static struct dentry *num_free_entries_dent __read_mostly;
99static struct dentry *min_free_entries_dent __read_mostly;
100
2d62ece1
JR
101static const char *type2name[4] = { "single", "page",
102 "scather-gather", "coherent" };
103
104static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
105 "DMA_FROM_DEVICE", "DMA_NONE" };
106
107/*
108 * The access to some variables in this macro is racy. We can't use atomic_t
109 * here because all these variables are exported to debugfs. Some of them even
110 * writeable. This is also the reason why a lock won't help much. But anyway,
111 * the races are no big deal. Here is why:
112 *
113 * error_count: the addition is racy, but the worst thing that can happen is
114 * that we don't count some errors
115 * show_num_errors: the subtraction is racy. Also no big deal because in
116 * worst case this will result in one warning more in the
117 * system log than the user configured. This variable is
118 * writeable via debugfs.
119 */
6c132d1b
DW
120static inline void dump_entry_trace(struct dma_debug_entry *entry)
121{
122#ifdef CONFIG_STACKTRACE
123 if (entry) {
124 printk(KERN_WARNING "Mapped at:\n");
125 print_stack_trace(&entry->stacktrace, 0);
126 }
127#endif
128}
129
130#define err_printk(dev, entry, format, arg...) do { \
2d62ece1
JR
131 error_count += 1; \
132 if (show_all_errors || show_num_errors > 0) { \
133 WARN(1, "%s %s: " format, \
134 dev_driver_string(dev), \
135 dev_name(dev) , ## arg); \
6c132d1b 136 dump_entry_trace(entry); \
2d62ece1
JR
137 } \
138 if (!show_all_errors && show_num_errors > 0) \
139 show_num_errors -= 1; \
140 } while (0);
141
30dfa90c
JR
142/*
143 * Hash related functions
144 *
145 * Every DMA-API request is saved into a struct dma_debug_entry. To
146 * have quick access to these structs they are stored into a hash.
147 */
148static int hash_fn(struct dma_debug_entry *entry)
149{
150 /*
151 * Hash function is based on the dma address.
152 * We use bits 20-27 here as the index into the hash
153 */
154 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
155}
156
157/*
158 * Request exclusive access to a hash bucket for a given dma_debug_entry.
159 */
160static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
161 unsigned long *flags)
162{
163 int idx = hash_fn(entry);
164 unsigned long __flags;
165
166 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
167 *flags = __flags;
168 return &dma_entry_hash[idx];
169}
170
171/*
172 * Give up exclusive access to the hash bucket
173 */
174static void put_hash_bucket(struct hash_bucket *bucket,
175 unsigned long *flags)
176{
177 unsigned long __flags = *flags;
178
179 spin_unlock_irqrestore(&bucket->lock, __flags);
180}
181
182/*
183 * Search a given entry in the hash bucket list
184 */
185static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
186 struct dma_debug_entry *ref)
187{
188 struct dma_debug_entry *entry;
189
190 list_for_each_entry(entry, &bucket->list, list) {
191 if ((entry->dev_addr == ref->dev_addr) &&
192 (entry->dev == ref->dev))
193 return entry;
194 }
195
196 return NULL;
197}
198
199/*
200 * Add an entry to a hash bucket
201 */
202static void hash_bucket_add(struct hash_bucket *bucket,
203 struct dma_debug_entry *entry)
204{
205 list_add_tail(&entry->list, &bucket->list);
206}
207
208/*
209 * Remove entry from a hash bucket list
210 */
211static void hash_bucket_del(struct dma_debug_entry *entry)
212{
213 list_del(&entry->list);
214}
215
ac26c18b
DW
216/*
217 * Dump mapping entries for debugging purposes
218 */
219void debug_dma_dump_mappings(struct device *dev)
220{
221 int idx;
222
223 for (idx = 0; idx < HASH_SIZE; idx++) {
224 struct hash_bucket *bucket = &dma_entry_hash[idx];
225 struct dma_debug_entry *entry;
226 unsigned long flags;
227
228 spin_lock_irqsave(&bucket->lock, flags);
229
230 list_for_each_entry(entry, &bucket->list, list) {
231 if (!dev || dev == entry->dev) {
232 dev_info(entry->dev,
233 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
234 type2name[entry->type], idx,
235 (unsigned long long)entry->paddr,
236 entry->dev_addr, entry->size,
237 dir2name[entry->direction]);
238 }
239 }
240
241 spin_unlock_irqrestore(&bucket->lock, flags);
242 }
243}
244EXPORT_SYMBOL(debug_dma_dump_mappings);
245
30dfa90c
JR
246/*
247 * Wrapper function for adding an entry to the hash.
248 * This function takes care of locking itself.
249 */
250static void add_dma_entry(struct dma_debug_entry *entry)
251{
252 struct hash_bucket *bucket;
253 unsigned long flags;
254
255 bucket = get_hash_bucket(entry, &flags);
256 hash_bucket_add(bucket, entry);
257 put_hash_bucket(bucket, &flags);
258}
259
3b1e79ed
JR
260/* struct dma_entry allocator
261 *
262 * The next two functions implement the allocator for
263 * struct dma_debug_entries.
264 */
265static struct dma_debug_entry *dma_entry_alloc(void)
266{
267 struct dma_debug_entry *entry = NULL;
268 unsigned long flags;
269
270 spin_lock_irqsave(&free_entries_lock, flags);
271
272 if (list_empty(&free_entries)) {
273 printk(KERN_ERR "DMA-API: debugging out of memory "
274 "- disabling\n");
275 global_disable = true;
276 goto out;
277 }
278
279 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
280 list_del(&entry->list);
281 memset(entry, 0, sizeof(*entry));
282
6c132d1b
DW
283#ifdef CONFIG_STACKTRACE
284 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
285 entry->stacktrace.entries = entry->st_entries;
286 entry->stacktrace.skip = 2;
287 save_stack_trace(&entry->stacktrace);
288#endif
3b1e79ed
JR
289 num_free_entries -= 1;
290 if (num_free_entries < min_free_entries)
291 min_free_entries = num_free_entries;
292
293out:
294 spin_unlock_irqrestore(&free_entries_lock, flags);
295
296 return entry;
297}
298
299static void dma_entry_free(struct dma_debug_entry *entry)
300{
301 unsigned long flags;
302
303 /*
304 * add to beginning of the list - this way the entries are
305 * more likely cache hot when they are reallocated.
306 */
307 spin_lock_irqsave(&free_entries_lock, flags);
308 list_add(&entry->list, &free_entries);
309 num_free_entries += 1;
310 spin_unlock_irqrestore(&free_entries_lock, flags);
311}
312
6bf07871
JR
313/*
314 * DMA-API debugging init code
315 *
316 * The init code does two things:
317 * 1. Initialize core data structures
318 * 2. Preallocate a given number of dma_debug_entry structs
319 */
320
321static int prealloc_memory(u32 num_entries)
322{
323 struct dma_debug_entry *entry, *next_entry;
324 int i;
325
326 for (i = 0; i < num_entries; ++i) {
327 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
328 if (!entry)
329 goto out_err;
330
331 list_add_tail(&entry->list, &free_entries);
332 }
333
334 num_free_entries = num_entries;
335 min_free_entries = num_entries;
336
337 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
338 num_entries);
339
340 return 0;
341
342out_err:
343
344 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
345 list_del(&entry->list);
346 kfree(entry);
347 }
348
349 return -ENOMEM;
350}
351
788dcfa6
JR
352static int dma_debug_fs_init(void)
353{
354 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
355 if (!dma_debug_dent) {
356 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
357 return -ENOMEM;
358 }
359
360 global_disable_dent = debugfs_create_bool("disabled", 0444,
361 dma_debug_dent,
362 (u32 *)&global_disable);
363 if (!global_disable_dent)
364 goto out_err;
365
366 error_count_dent = debugfs_create_u32("error_count", 0444,
367 dma_debug_dent, &error_count);
368 if (!error_count_dent)
369 goto out_err;
370
371 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
372 dma_debug_dent,
373 &show_all_errors);
374 if (!show_all_errors_dent)
375 goto out_err;
376
377 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
378 dma_debug_dent,
379 &show_num_errors);
380 if (!show_num_errors_dent)
381 goto out_err;
382
383 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
384 dma_debug_dent,
385 &num_free_entries);
386 if (!num_free_entries_dent)
387 goto out_err;
388
389 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
390 dma_debug_dent,
391 &min_free_entries);
392 if (!min_free_entries_dent)
393 goto out_err;
394
395 return 0;
396
397out_err:
398 debugfs_remove_recursive(dma_debug_dent);
399
400 return -ENOMEM;
401}
402
41531c8f
JR
403static int device_dma_allocations(struct device *dev)
404{
405 struct dma_debug_entry *entry;
406 unsigned long flags;
407 int count = 0, i;
408
409 for (i = 0; i < HASH_SIZE; ++i) {
410 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
411 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
412 if (entry->dev == dev)
413 count += 1;
414 }
415 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
416 }
417
418 return count;
419}
420
421static int dma_debug_device_change(struct notifier_block *nb,
422 unsigned long action, void *data)
423{
424 struct device *dev = data;
425 int count;
426
427
428 switch (action) {
429 case BUS_NOTIFY_UNBIND_DRIVER:
430 count = device_dma_allocations(dev);
431 if (count == 0)
432 break;
433 err_printk(dev, NULL, "DMA-API: device driver has pending "
434 "DMA allocations while released from device "
435 "[count=%d]\n", count);
436 break;
437 default:
438 break;
439 }
440
441 return 0;
442}
443
444void dma_debug_add_bus(struct bus_type *bus)
445{
446 struct notifier_block *nb;
447
448 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
449 if (nb == NULL) {
450 printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
451 return;
452 }
453
454 nb->notifier_call = dma_debug_device_change;
455
456 bus_register_notifier(bus, nb);
457}
788dcfa6 458
6bf07871
JR
459/*
460 * Let the architectures decide how many entries should be preallocated.
461 */
462void dma_debug_init(u32 num_entries)
463{
464 int i;
465
466 if (global_disable)
467 return;
468
469 for (i = 0; i < HASH_SIZE; ++i) {
470 INIT_LIST_HEAD(&dma_entry_hash[i].list);
471 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
472 }
473
788dcfa6
JR
474 if (dma_debug_fs_init() != 0) {
475 printk(KERN_ERR "DMA-API: error creating debugfs entries "
476 "- disabling\n");
477 global_disable = true;
478
479 return;
480 }
481
59d3daaf
JR
482 if (req_entries)
483 num_entries = req_entries;
484
6bf07871
JR
485 if (prealloc_memory(num_entries) != 0) {
486 printk(KERN_ERR "DMA-API: debugging out of memory error "
487 "- disabled\n");
488 global_disable = true;
489
490 return;
491 }
492
493 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
494}
495
59d3daaf
JR
496static __init int dma_debug_cmdline(char *str)
497{
498 if (!str)
499 return -EINVAL;
500
501 if (strncmp(str, "off", 3) == 0) {
502 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
503 "command line\n");
504 global_disable = true;
505 }
506
507 return 0;
508}
509
510static __init int dma_debug_entries_cmdline(char *str)
511{
512 int res;
513
514 if (!str)
515 return -EINVAL;
516
517 res = get_option(&str, &req_entries);
518
519 if (!res)
520 req_entries = 0;
521
522 return 0;
523}
524
525__setup("dma_debug=", dma_debug_cmdline);
526__setup("dma_debug_entries=", dma_debug_entries_cmdline);
527
2d62ece1
JR
528static void check_unmap(struct dma_debug_entry *ref)
529{
530 struct dma_debug_entry *entry;
531 struct hash_bucket *bucket;
532 unsigned long flags;
533
35d40952
FT
534 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
535 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
536 "to free an invalid DMA memory address\n");
2d62ece1 537 return;
35d40952 538 }
2d62ece1
JR
539
540 bucket = get_hash_bucket(ref, &flags);
541 entry = hash_bucket_find(bucket, ref);
542
543 if (!entry) {
6c132d1b 544 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
2d62ece1
JR
545 "to free DMA memory it has not allocated "
546 "[device address=0x%016llx] [size=%llu bytes]\n",
547 ref->dev_addr, ref->size);
548 goto out;
549 }
550
551 if (ref->size != entry->size) {
6c132d1b 552 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
553 "DMA memory with different size "
554 "[device address=0x%016llx] [map size=%llu bytes] "
555 "[unmap size=%llu bytes]\n",
556 ref->dev_addr, entry->size, ref->size);
557 }
558
559 if (ref->type != entry->type) {
6c132d1b 560 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
561 "DMA memory with wrong function "
562 "[device address=0x%016llx] [size=%llu bytes] "
563 "[mapped as %s] [unmapped as %s]\n",
564 ref->dev_addr, ref->size,
565 type2name[entry->type], type2name[ref->type]);
566 } else if ((entry->type == dma_debug_coherent) &&
567 (ref->paddr != entry->paddr)) {
6c132d1b 568 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
569 "DMA memory with different CPU address "
570 "[device address=0x%016llx] [size=%llu bytes] "
571 "[cpu alloc address=%p] [cpu free address=%p]",
572 ref->dev_addr, ref->size,
573 (void *)entry->paddr, (void *)ref->paddr);
574 }
575
576 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
577 ref->sg_call_ents != entry->sg_call_ents) {
6c132d1b 578 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
579 "DMA sg list with different entry count "
580 "[map count=%d] [unmap count=%d]\n",
581 entry->sg_call_ents, ref->sg_call_ents);
582 }
583
584 /*
585 * This may be no bug in reality - but most implementations of the
586 * DMA API don't handle this properly, so check for it here
587 */
588 if (ref->direction != entry->direction) {
6c132d1b 589 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
590 "DMA memory with different direction "
591 "[device address=0x%016llx] [size=%llu bytes] "
592 "[mapped with %s] [unmapped with %s]\n",
593 ref->dev_addr, ref->size,
594 dir2name[entry->direction],
595 dir2name[ref->direction]);
596 }
597
598 hash_bucket_del(entry);
599 dma_entry_free(entry);
600
601out:
602 put_hash_bucket(bucket, &flags);
603}
604
605static void check_for_stack(struct device *dev, void *addr)
606{
607 if (object_is_on_stack(addr))
6c132d1b
DW
608 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
609 "stack [addr=%p]\n", addr);
2d62ece1
JR
610}
611
2e34bde1
JR
612static inline bool overlap(void *addr, u64 size, void *start, void *end)
613{
614 void *addr2 = (char *)addr + size;
615
616 return ((addr >= start && addr < end) ||
617 (addr2 >= start && addr2 < end) ||
618 ((addr < start) && (addr2 >= end)));
619}
620
621static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
622{
623 if (overlap(addr, size, _text, _etext) ||
624 overlap(addr, size, __start_rodata, __end_rodata))
625 err_printk(dev, NULL, "DMA-API: device driver maps "
626 "memory from kernel text or rodata "
627 "[addr=%p] [size=%llu]\n", addr, size);
628}
629
2d62ece1
JR
630static void check_sync(struct device *dev, dma_addr_t addr,
631 u64 size, u64 offset, int direction, bool to_cpu)
632{
633 struct dma_debug_entry ref = {
634 .dev = dev,
635 .dev_addr = addr,
636 .size = size,
637 .direction = direction,
638 };
639 struct dma_debug_entry *entry;
640 struct hash_bucket *bucket;
641 unsigned long flags;
642
643 bucket = get_hash_bucket(&ref, &flags);
644
645 entry = hash_bucket_find(bucket, &ref);
646
647 if (!entry) {
6c132d1b 648 err_printk(dev, NULL, "DMA-API: device driver tries "
2d62ece1
JR
649 "to sync DMA memory it has not allocated "
650 "[device address=0x%016llx] [size=%llu bytes]\n",
651 addr, size);
652 goto out;
653 }
654
655 if ((offset + size) > entry->size) {
6c132d1b 656 err_printk(dev, entry, "DMA-API: device driver syncs"
2d62ece1
JR
657 " DMA memory outside allocated range "
658 "[device address=0x%016llx] "
659 "[allocation size=%llu bytes] [sync offset=%llu] "
660 "[sync size=%llu]\n", entry->dev_addr, entry->size,
661 offset, size);
662 }
663
664 if (direction != entry->direction) {
6c132d1b 665 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
666 "DMA memory with different direction "
667 "[device address=0x%016llx] [size=%llu bytes] "
668 "[mapped with %s] [synced with %s]\n",
669 addr, entry->size,
670 dir2name[entry->direction],
671 dir2name[direction]);
672 }
673
674 if (entry->direction == DMA_BIDIRECTIONAL)
675 goto out;
676
677 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
678 !(direction == DMA_TO_DEVICE))
6c132d1b 679 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
680 "device read-only DMA memory for cpu "
681 "[device address=0x%016llx] [size=%llu bytes] "
682 "[mapped with %s] [synced with %s]\n",
683 addr, entry->size,
684 dir2name[entry->direction],
685 dir2name[direction]);
686
687 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
688 !(direction == DMA_FROM_DEVICE))
6c132d1b 689 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
690 "device write-only DMA memory to device "
691 "[device address=0x%016llx] [size=%llu bytes] "
692 "[mapped with %s] [synced with %s]\n",
693 addr, entry->size,
694 dir2name[entry->direction],
695 dir2name[direction]);
696
697out:
698 put_hash_bucket(bucket, &flags);
699
700}
701
f62bc980
JR
702void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
703 size_t size, int direction, dma_addr_t dma_addr,
704 bool map_single)
705{
706 struct dma_debug_entry *entry;
707
708 if (unlikely(global_disable))
709 return;
710
711 if (unlikely(dma_mapping_error(dev, dma_addr)))
712 return;
713
714 entry = dma_entry_alloc();
715 if (!entry)
716 return;
717
718 entry->dev = dev;
719 entry->type = dma_debug_page;
720 entry->paddr = page_to_phys(page) + offset;
721 entry->dev_addr = dma_addr;
722 entry->size = size;
723 entry->direction = direction;
724
725 if (map_single) {
2e34bde1
JR
726 void *addr = ((char *)page_address(page)) + offset;
727
f62bc980 728 entry->type = dma_debug_single;
2e34bde1
JR
729 check_for_stack(dev, addr);
730 check_for_illegal_area(dev, addr, size);
f62bc980
JR
731 }
732
733 add_dma_entry(entry);
734}
735EXPORT_SYMBOL(debug_dma_map_page);
736
737void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
738 size_t size, int direction, bool map_single)
739{
740 struct dma_debug_entry ref = {
741 .type = dma_debug_page,
742 .dev = dev,
743 .dev_addr = addr,
744 .size = size,
745 .direction = direction,
746 };
747
748 if (unlikely(global_disable))
749 return;
750
751 if (map_single)
752 ref.type = dma_debug_single;
753
754 check_unmap(&ref);
755}
756EXPORT_SYMBOL(debug_dma_unmap_page);
757
972aa45c
JR
758void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
759 int nents, int mapped_ents, int direction)
760{
761 struct dma_debug_entry *entry;
762 struct scatterlist *s;
763 int i;
764
765 if (unlikely(global_disable))
766 return;
767
768 for_each_sg(sg, s, mapped_ents, i) {
769 entry = dma_entry_alloc();
770 if (!entry)
771 return;
772
773 entry->type = dma_debug_sg;
774 entry->dev = dev;
775 entry->paddr = sg_phys(s);
776 entry->size = s->length;
777 entry->dev_addr = s->dma_address;
778 entry->direction = direction;
779 entry->sg_call_ents = nents;
780 entry->sg_mapped_ents = mapped_ents;
781
782 check_for_stack(dev, sg_virt(s));
2e34bde1 783 check_for_illegal_area(dev, sg_virt(s), s->length);
972aa45c
JR
784
785 add_dma_entry(entry);
786 }
787}
788EXPORT_SYMBOL(debug_dma_map_sg);
789
790void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
791 int nelems, int dir)
792{
793 struct dma_debug_entry *entry;
794 struct scatterlist *s;
795 int mapped_ents = 0, i;
796 unsigned long flags;
797
798 if (unlikely(global_disable))
799 return;
800
801 for_each_sg(sglist, s, nelems, i) {
802
803 struct dma_debug_entry ref = {
804 .type = dma_debug_sg,
805 .dev = dev,
806 .paddr = sg_phys(s),
807 .dev_addr = s->dma_address,
808 .size = s->length,
809 .direction = dir,
810 .sg_call_ents = 0,
811 };
812
813 if (mapped_ents && i >= mapped_ents)
814 break;
815
816 if (mapped_ents == 0) {
817 struct hash_bucket *bucket;
818 ref.sg_call_ents = nelems;
819 bucket = get_hash_bucket(&ref, &flags);
820 entry = hash_bucket_find(bucket, &ref);
821 if (entry)
822 mapped_ents = entry->sg_mapped_ents;
823 put_hash_bucket(bucket, &flags);
824 }
825
826 check_unmap(&ref);
827 }
828}
829EXPORT_SYMBOL(debug_dma_unmap_sg);
830
6bfd4498
JR
831void debug_dma_alloc_coherent(struct device *dev, size_t size,
832 dma_addr_t dma_addr, void *virt)
833{
834 struct dma_debug_entry *entry;
835
836 if (unlikely(global_disable))
837 return;
838
839 if (unlikely(virt == NULL))
840 return;
841
842 entry = dma_entry_alloc();
843 if (!entry)
844 return;
845
846 entry->type = dma_debug_coherent;
847 entry->dev = dev;
848 entry->paddr = virt_to_phys(virt);
849 entry->size = size;
850 entry->dev_addr = dma_addr;
851 entry->direction = DMA_BIDIRECTIONAL;
852
853 add_dma_entry(entry);
854}
855EXPORT_SYMBOL(debug_dma_alloc_coherent);
856
857void debug_dma_free_coherent(struct device *dev, size_t size,
858 void *virt, dma_addr_t addr)
859{
860 struct dma_debug_entry ref = {
861 .type = dma_debug_coherent,
862 .dev = dev,
863 .paddr = virt_to_phys(virt),
864 .dev_addr = addr,
865 .size = size,
866 .direction = DMA_BIDIRECTIONAL,
867 };
868
869 if (unlikely(global_disable))
870 return;
871
872 check_unmap(&ref);
873}
874EXPORT_SYMBOL(debug_dma_free_coherent);
875
b9d2317e
JR
876void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
877 size_t size, int direction)
878{
879 if (unlikely(global_disable))
880 return;
881
882 check_sync(dev, dma_handle, size, 0, direction, true);
883}
884EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
885
886void debug_dma_sync_single_for_device(struct device *dev,
887 dma_addr_t dma_handle, size_t size,
888 int direction)
889{
890 if (unlikely(global_disable))
891 return;
892
893 check_sync(dev, dma_handle, size, 0, direction, false);
894}
895EXPORT_SYMBOL(debug_dma_sync_single_for_device);
896
948408ba
JR
897void debug_dma_sync_single_range_for_cpu(struct device *dev,
898 dma_addr_t dma_handle,
899 unsigned long offset, size_t size,
900 int direction)
901{
902 if (unlikely(global_disable))
903 return;
904
905 check_sync(dev, dma_handle, size, offset, direction, true);
906}
907EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
908
909void debug_dma_sync_single_range_for_device(struct device *dev,
910 dma_addr_t dma_handle,
911 unsigned long offset,
912 size_t size, int direction)
913{
914 if (unlikely(global_disable))
915 return;
916
917 check_sync(dev, dma_handle, size, offset, direction, false);
918}
919EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
920
a31fba5d
JR
921void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
922 int nelems, int direction)
923{
924 struct scatterlist *s;
925 int i;
926
927 if (unlikely(global_disable))
928 return;
929
930 for_each_sg(sg, s, nelems, i) {
931 check_sync(dev, s->dma_address, s->dma_length, 0,
932 direction, true);
933 }
934}
935EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
936
937void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
938 int nelems, int direction)
939{
940 struct scatterlist *s;
941 int i;
942
943 if (unlikely(global_disable))
944 return;
945
946 for_each_sg(sg, s, nelems, i) {
947 check_sync(dev, s->dma_address, s->dma_length, 0,
948 direction, false);
949 }
950}
951EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
952