dma-debug: introduce dma_debug_disabled
[linux-2.6-block.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>
23a7bfae 27#include <linux/export.h>
2d62ece1 28#include <linux/device.h>
f2f45e5f 29#include <linux/types.h>
2d62ece1 30#include <linux/sched.h>
8a6fc708 31#include <linux/ctype.h>
f2f45e5f 32#include <linux/list.h>
6bf07871 33#include <linux/slab.h>
f2f45e5f 34
2e34bde1
JR
35#include <asm/sections.h>
36
30dfa90c
JR
37#define HASH_SIZE 1024ULL
38#define HASH_FN_SHIFT 13
39#define HASH_FN_MASK (HASH_SIZE - 1)
40
f2f45e5f
JR
41enum {
42 dma_debug_single,
43 dma_debug_page,
44 dma_debug_sg,
45 dma_debug_coherent,
46};
47
6c9c6d63
SK
48enum map_err_types {
49 MAP_ERR_CHECK_NOT_APPLICABLE,
50 MAP_ERR_NOT_CHECKED,
51 MAP_ERR_CHECKED,
52};
53
6c132d1b
DW
54#define DMA_DEBUG_STACKTRACE_ENTRIES 5
55
0abdd7a8
DW
56/**
57 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
58 * @list: node on pre-allocated free_entries list
59 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
60 * @type: single, page, sg, coherent
61 * @pfn: page frame of the start address
62 * @offset: offset of mapping relative to pfn
63 * @size: length of the mapping
64 * @direction: enum dma_data_direction
65 * @sg_call_ents: 'nents' from dma_map_sg
66 * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
67 * @map_err_type: track whether dma_mapping_error() was checked
68 * @stacktrace: support backtraces when a violation is detected
69 */
f2f45e5f
JR
70struct dma_debug_entry {
71 struct list_head list;
72 struct device *dev;
73 int type;
0abdd7a8
DW
74 unsigned long pfn;
75 size_t offset;
f2f45e5f
JR
76 u64 dev_addr;
77 u64 size;
78 int direction;
79 int sg_call_ents;
80 int sg_mapped_ents;
6c9c6d63 81 enum map_err_types map_err_type;
6c132d1b
DW
82#ifdef CONFIG_STACKTRACE
83 struct stack_trace stacktrace;
84 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
85#endif
f2f45e5f
JR
86};
87
c6a21d0b
NH
88typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
89
30dfa90c
JR
90struct hash_bucket {
91 struct list_head list;
92 spinlock_t lock;
2d62ece1 93} ____cacheline_aligned_in_smp;
30dfa90c
JR
94
95/* Hash list to save the allocated dma addresses */
96static struct hash_bucket dma_entry_hash[HASH_SIZE];
3b1e79ed
JR
97/* List of pre-allocated dma_debug_entry's */
98static LIST_HEAD(free_entries);
99/* Lock for the list above */
100static DEFINE_SPINLOCK(free_entries_lock);
101
102/* Global disable flag - will be set in case of an error */
68ee6d22 103static u32 global_disable __read_mostly;
3b1e79ed 104
01ce18b3
FF
105static inline bool dma_debug_disabled(void)
106{
107 return global_disable;
108}
109
788dcfa6
JR
110/* Global error count */
111static u32 error_count;
112
113/* Global error show enable*/
114static u32 show_all_errors __read_mostly;
115/* Number of errors to show */
116static u32 show_num_errors = 1;
117
3b1e79ed
JR
118static u32 num_free_entries;
119static u32 min_free_entries;
e6a1a89d 120static u32 nr_total_entries;
30dfa90c 121
59d3daaf
JR
122/* number of preallocated entries requested by kernel cmdline */
123static u32 req_entries;
124
788dcfa6
JR
125/* debugfs dentry's for the stuff above */
126static struct dentry *dma_debug_dent __read_mostly;
127static struct dentry *global_disable_dent __read_mostly;
128static struct dentry *error_count_dent __read_mostly;
129static struct dentry *show_all_errors_dent __read_mostly;
130static struct dentry *show_num_errors_dent __read_mostly;
131static struct dentry *num_free_entries_dent __read_mostly;
132static struct dentry *min_free_entries_dent __read_mostly;
8a6fc708 133static struct dentry *filter_dent __read_mostly;
788dcfa6 134
2e507d84
JR
135/* per-driver filter related state */
136
137#define NAME_MAX_LEN 64
138
139static char current_driver_name[NAME_MAX_LEN] __read_mostly;
140static struct device_driver *current_driver __read_mostly;
141
142static DEFINE_RWLOCK(driver_name_lock);
788dcfa6 143
6c9c6d63
SK
144static const char *const maperr2str[] = {
145 [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
146 [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
147 [MAP_ERR_CHECKED] = "dma map error checked",
148};
149
2d62ece1
JR
150static const char *type2name[4] = { "single", "page",
151 "scather-gather", "coherent" };
152
153static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
154 "DMA_FROM_DEVICE", "DMA_NONE" };
155
156/*
157 * The access to some variables in this macro is racy. We can't use atomic_t
158 * here because all these variables are exported to debugfs. Some of them even
159 * writeable. This is also the reason why a lock won't help much. But anyway,
160 * the races are no big deal. Here is why:
161 *
162 * error_count: the addition is racy, but the worst thing that can happen is
163 * that we don't count some errors
164 * show_num_errors: the subtraction is racy. Also no big deal because in
165 * worst case this will result in one warning more in the
166 * system log than the user configured. This variable is
167 * writeable via debugfs.
168 */
6c132d1b
DW
169static inline void dump_entry_trace(struct dma_debug_entry *entry)
170{
171#ifdef CONFIG_STACKTRACE
172 if (entry) {
e7ed70ee 173 pr_warning("Mapped at:\n");
6c132d1b
DW
174 print_stack_trace(&entry->stacktrace, 0);
175 }
176#endif
177}
178
2e507d84
JR
179static bool driver_filter(struct device *dev)
180{
0bf84128
JR
181 struct device_driver *drv;
182 unsigned long flags;
183 bool ret;
184
2e507d84
JR
185 /* driver filter off */
186 if (likely(!current_driver_name[0]))
187 return true;
188
189 /* driver filter on and initialized */
ec9c96ef 190 if (current_driver && dev && dev->driver == current_driver)
2e507d84
JR
191 return true;
192
ec9c96ef
KM
193 /* driver filter on, but we can't filter on a NULL device... */
194 if (!dev)
195 return false;
196
0bf84128
JR
197 if (current_driver || !current_driver_name[0])
198 return false;
2e507d84 199
0bf84128 200 /* driver filter on but not yet initialized */
f3ff9247 201 drv = dev->driver;
0bf84128
JR
202 if (!drv)
203 return false;
204
205 /* lock to protect against change of current_driver_name */
206 read_lock_irqsave(&driver_name_lock, flags);
207
208 ret = false;
209 if (drv->name &&
210 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
211 current_driver = drv;
212 ret = true;
2e507d84
JR
213 }
214
0bf84128 215 read_unlock_irqrestore(&driver_name_lock, flags);
0bf84128
JR
216
217 return ret;
2e507d84
JR
218}
219
ec9c96ef
KM
220#define err_printk(dev, entry, format, arg...) do { \
221 error_count += 1; \
222 if (driver_filter(dev) && \
223 (show_all_errors || show_num_errors > 0)) { \
224 WARN(1, "%s %s: " format, \
225 dev ? dev_driver_string(dev) : "NULL", \
226 dev ? dev_name(dev) : "NULL", ## arg); \
227 dump_entry_trace(entry); \
228 } \
229 if (!show_all_errors && show_num_errors > 0) \
230 show_num_errors -= 1; \
2d62ece1
JR
231 } while (0);
232
30dfa90c
JR
233/*
234 * Hash related functions
235 *
236 * Every DMA-API request is saved into a struct dma_debug_entry. To
237 * have quick access to these structs they are stored into a hash.
238 */
239static int hash_fn(struct dma_debug_entry *entry)
240{
241 /*
242 * Hash function is based on the dma address.
243 * We use bits 20-27 here as the index into the hash
244 */
245 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
246}
247
248/*
249 * Request exclusive access to a hash bucket for a given dma_debug_entry.
250 */
251static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
252 unsigned long *flags)
253{
254 int idx = hash_fn(entry);
255 unsigned long __flags;
256
257 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
258 *flags = __flags;
259 return &dma_entry_hash[idx];
260}
261
262/*
263 * Give up exclusive access to the hash bucket
264 */
265static void put_hash_bucket(struct hash_bucket *bucket,
266 unsigned long *flags)
267{
268 unsigned long __flags = *flags;
269
270 spin_unlock_irqrestore(&bucket->lock, __flags);
271}
272
c6a21d0b
NH
273static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
274{
91ec37cc 275 return ((a->dev_addr == b->dev_addr) &&
c6a21d0b
NH
276 (a->dev == b->dev)) ? true : false;
277}
278
279static bool containing_match(struct dma_debug_entry *a,
280 struct dma_debug_entry *b)
281{
282 if (a->dev != b->dev)
283 return false;
284
285 if ((b->dev_addr <= a->dev_addr) &&
286 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
287 return true;
288
289 return false;
290}
291
30dfa90c
JR
292/*
293 * Search a given entry in the hash bucket list
294 */
c6a21d0b
NH
295static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
296 struct dma_debug_entry *ref,
297 match_fn match)
30dfa90c 298{
7caf6a49 299 struct dma_debug_entry *entry, *ret = NULL;
fe73fbe1 300 int matches = 0, match_lvl, last_lvl = -1;
30dfa90c
JR
301
302 list_for_each_entry(entry, &bucket->list, list) {
c6a21d0b 303 if (!match(ref, entry))
7caf6a49
JR
304 continue;
305
306 /*
307 * Some drivers map the same physical address multiple
308 * times. Without a hardware IOMMU this results in the
309 * same device addresses being put into the dma-debug
310 * hash multiple times too. This can result in false
af901ca1 311 * positives being reported. Therefore we implement a
7caf6a49
JR
312 * best-fit algorithm here which returns the entry from
313 * the hash which fits best to the reference value
314 * instead of the first-fit.
315 */
316 matches += 1;
317 match_lvl = 0;
e5e8c5b9
JR
318 entry->size == ref->size ? ++match_lvl : 0;
319 entry->type == ref->type ? ++match_lvl : 0;
320 entry->direction == ref->direction ? ++match_lvl : 0;
321 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
7caf6a49 322
e5e8c5b9 323 if (match_lvl == 4) {
7caf6a49 324 /* perfect-fit - return the result */
30dfa90c 325 return entry;
7caf6a49
JR
326 } else if (match_lvl > last_lvl) {
327 /*
328 * We found an entry that fits better then the
fe73fbe1 329 * previous one or it is the 1st match.
7caf6a49
JR
330 */
331 last_lvl = match_lvl;
332 ret = entry;
333 }
30dfa90c
JR
334 }
335
7caf6a49
JR
336 /*
337 * If we have multiple matches but no perfect-fit, just return
338 * NULL.
339 */
340 ret = (matches == 1) ? ret : NULL;
341
342 return ret;
30dfa90c
JR
343}
344
c6a21d0b
NH
345static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
346 struct dma_debug_entry *ref)
347{
348 return __hash_bucket_find(bucket, ref, exact_match);
349}
350
351static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
352 struct dma_debug_entry *ref,
353 unsigned long *flags)
354{
355
356 unsigned int max_range = dma_get_max_seg_size(ref->dev);
357 struct dma_debug_entry *entry, index = *ref;
358 unsigned int range = 0;
359
360 while (range <= max_range) {
361 entry = __hash_bucket_find(*bucket, &index, containing_match);
362
363 if (entry)
364 return entry;
365
366 /*
367 * Nothing found, go back a hash bucket
368 */
369 put_hash_bucket(*bucket, flags);
370 range += (1 << HASH_FN_SHIFT);
371 index.dev_addr -= (1 << HASH_FN_SHIFT);
372 *bucket = get_hash_bucket(&index, flags);
373 }
374
375 return NULL;
376}
377
30dfa90c
JR
378/*
379 * Add an entry to a hash bucket
380 */
381static void hash_bucket_add(struct hash_bucket *bucket,
382 struct dma_debug_entry *entry)
383{
384 list_add_tail(&entry->list, &bucket->list);
385}
386
387/*
388 * Remove entry from a hash bucket list
389 */
390static void hash_bucket_del(struct dma_debug_entry *entry)
391{
392 list_del(&entry->list);
393}
394
0abdd7a8
DW
395static unsigned long long phys_addr(struct dma_debug_entry *entry)
396{
397 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
398}
399
ac26c18b
DW
400/*
401 * Dump mapping entries for debugging purposes
402 */
403void debug_dma_dump_mappings(struct device *dev)
404{
405 int idx;
406
407 for (idx = 0; idx < HASH_SIZE; idx++) {
408 struct hash_bucket *bucket = &dma_entry_hash[idx];
409 struct dma_debug_entry *entry;
410 unsigned long flags;
411
412 spin_lock_irqsave(&bucket->lock, flags);
413
414 list_for_each_entry(entry, &bucket->list, list) {
415 if (!dev || dev == entry->dev) {
416 dev_info(entry->dev,
0abdd7a8 417 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
ac26c18b 418 type2name[entry->type], idx,
0abdd7a8 419 phys_addr(entry), entry->pfn,
ac26c18b 420 entry->dev_addr, entry->size,
6c9c6d63
SK
421 dir2name[entry->direction],
422 maperr2str[entry->map_err_type]);
ac26c18b
DW
423 }
424 }
425
426 spin_unlock_irqrestore(&bucket->lock, flags);
427 }
428}
429EXPORT_SYMBOL(debug_dma_dump_mappings);
430
0abdd7a8 431/*
3b7a6418
DW
432 * For each mapping (initial cacheline in the case of
433 * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
434 * scatterlist, or the cacheline specified in dma_map_single) insert
435 * into this tree using the cacheline as the key. At
0abdd7a8 436 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
3b7a6418 437 * the entry already exists at insertion time add a tag as a reference
0abdd7a8 438 * count for the overlapping mappings. For now, the overlap tracking
3b7a6418
DW
439 * just ensures that 'unmaps' balance 'maps' before marking the
440 * cacheline idle, but we should also be flagging overlaps as an API
441 * violation.
0abdd7a8
DW
442 *
443 * Memory usage is mostly constrained by the maximum number of available
444 * dma-debug entries in that we need a free dma_debug_entry before
3b7a6418
DW
445 * inserting into the tree. In the case of dma_map_page and
446 * dma_alloc_coherent there is only one dma_debug_entry and one
447 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
448 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
449 * entries into the tree.
0abdd7a8
DW
450 *
451 * At any time debug_dma_assert_idle() can be called to trigger a
3b7a6418 452 * warning if any cachelines in the given page are in the active set.
0abdd7a8 453 */
3b7a6418 454static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
0abdd7a8 455static DEFINE_SPINLOCK(radix_lock);
3b7a6418
DW
456#define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
457#define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
458#define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
0abdd7a8 459
3b7a6418
DW
460static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
461{
462 return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
463 (entry->offset >> L1_CACHE_SHIFT);
464}
465
466static int active_cacheline_read_overlap(phys_addr_t cln)
0abdd7a8
DW
467{
468 int overlap = 0, i;
469
470 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
3b7a6418 471 if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
0abdd7a8
DW
472 overlap |= 1 << i;
473 return overlap;
474}
475
3b7a6418 476static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
0abdd7a8
DW
477{
478 int i;
479
3b7a6418 480 if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
59f2e7df 481 return overlap;
0abdd7a8
DW
482
483 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
484 if (overlap & 1 << i)
3b7a6418 485 radix_tree_tag_set(&dma_active_cacheline, cln, i);
0abdd7a8 486 else
3b7a6418 487 radix_tree_tag_clear(&dma_active_cacheline, cln, i);
0abdd7a8
DW
488
489 return overlap;
490}
491
3b7a6418 492static void active_cacheline_inc_overlap(phys_addr_t cln)
0abdd7a8 493{
3b7a6418 494 int overlap = active_cacheline_read_overlap(cln);
0abdd7a8 495
3b7a6418 496 overlap = active_cacheline_set_overlap(cln, ++overlap);
0abdd7a8
DW
497
498 /* If we overflowed the overlap counter then we're potentially
499 * leaking dma-mappings. Otherwise, if maps and unmaps are
500 * balanced then this overflow may cause false negatives in
3b7a6418 501 * debug_dma_assert_idle() as the cacheline may be marked idle
0abdd7a8
DW
502 * prematurely.
503 */
3b7a6418
DW
504 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
505 "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
506 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
0abdd7a8
DW
507}
508
3b7a6418 509static int active_cacheline_dec_overlap(phys_addr_t cln)
0abdd7a8 510{
3b7a6418 511 int overlap = active_cacheline_read_overlap(cln);
0abdd7a8 512
3b7a6418 513 return active_cacheline_set_overlap(cln, --overlap);
0abdd7a8
DW
514}
515
3b7a6418 516static int active_cacheline_insert(struct dma_debug_entry *entry)
0abdd7a8 517{
3b7a6418 518 phys_addr_t cln = to_cacheline_number(entry);
0abdd7a8
DW
519 unsigned long flags;
520 int rc;
521
3b7a6418
DW
522 /* If the device is not writing memory then we don't have any
523 * concerns about the cpu consuming stale data. This mitigates
524 * legitimate usages of overlapping mappings.
525 */
526 if (entry->direction == DMA_TO_DEVICE)
527 return 0;
528
0abdd7a8 529 spin_lock_irqsave(&radix_lock, flags);
3b7a6418 530 rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
0abdd7a8 531 if (rc == -EEXIST)
3b7a6418 532 active_cacheline_inc_overlap(cln);
0abdd7a8
DW
533 spin_unlock_irqrestore(&radix_lock, flags);
534
535 return rc;
536}
537
3b7a6418 538static void active_cacheline_remove(struct dma_debug_entry *entry)
0abdd7a8 539{
3b7a6418 540 phys_addr_t cln = to_cacheline_number(entry);
0abdd7a8
DW
541 unsigned long flags;
542
3b7a6418
DW
543 /* ...mirror the insert case */
544 if (entry->direction == DMA_TO_DEVICE)
545 return;
546
0abdd7a8 547 spin_lock_irqsave(&radix_lock, flags);
59f2e7df 548 /* since we are counting overlaps the final put of the
3b7a6418
DW
549 * cacheline will occur when the overlap count is 0.
550 * active_cacheline_dec_overlap() returns -1 in that case
59f2e7df 551 */
3b7a6418
DW
552 if (active_cacheline_dec_overlap(cln) < 0)
553 radix_tree_delete(&dma_active_cacheline, cln);
0abdd7a8
DW
554 spin_unlock_irqrestore(&radix_lock, flags);
555}
556
557/**
558 * debug_dma_assert_idle() - assert that a page is not undergoing dma
3b7a6418 559 * @page: page to lookup in the dma_active_cacheline tree
0abdd7a8
DW
560 *
561 * Place a call to this routine in cases where the cpu touching the page
562 * before the dma completes (page is dma_unmapped) will lead to data
563 * corruption.
564 */
565void debug_dma_assert_idle(struct page *page)
566{
3b7a6418
DW
567 static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
568 struct dma_debug_entry *entry = NULL;
569 void **results = (void **) &ents;
570 unsigned int nents, i;
0abdd7a8 571 unsigned long flags;
3b7a6418 572 phys_addr_t cln;
0abdd7a8
DW
573
574 if (!page)
575 return;
576
3b7a6418 577 cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
0abdd7a8 578 spin_lock_irqsave(&radix_lock, flags);
3b7a6418
DW
579 nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
580 CACHELINES_PER_PAGE);
581 for (i = 0; i < nents; i++) {
582 phys_addr_t ent_cln = to_cacheline_number(ents[i]);
583
584 if (ent_cln == cln) {
585 entry = ents[i];
586 break;
587 } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
588 break;
589 }
0abdd7a8
DW
590 spin_unlock_irqrestore(&radix_lock, flags);
591
592 if (!entry)
593 return;
594
3b7a6418 595 cln = to_cacheline_number(entry);
0abdd7a8 596 err_printk(entry->dev, entry,
3b7a6418
DW
597 "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
598 &cln);
0abdd7a8
DW
599}
600
30dfa90c
JR
601/*
602 * Wrapper function for adding an entry to the hash.
603 * This function takes care of locking itself.
604 */
605static void add_dma_entry(struct dma_debug_entry *entry)
606{
607 struct hash_bucket *bucket;
608 unsigned long flags;
0abdd7a8 609 int rc;
30dfa90c
JR
610
611 bucket = get_hash_bucket(entry, &flags);
612 hash_bucket_add(bucket, entry);
613 put_hash_bucket(bucket, &flags);
0abdd7a8 614
3b7a6418 615 rc = active_cacheline_insert(entry);
0abdd7a8 616 if (rc == -ENOMEM) {
3b7a6418 617 pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
0abdd7a8
DW
618 global_disable = true;
619 }
620
621 /* TODO: report -EEXIST errors here as overlapping mappings are
622 * not supported by the DMA API
623 */
30dfa90c
JR
624}
625
e6a1a89d
FT
626static struct dma_debug_entry *__dma_entry_alloc(void)
627{
628 struct dma_debug_entry *entry;
629
630 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
631 list_del(&entry->list);
632 memset(entry, 0, sizeof(*entry));
633
634 num_free_entries -= 1;
635 if (num_free_entries < min_free_entries)
636 min_free_entries = num_free_entries;
637
638 return entry;
639}
640
3b1e79ed
JR
641/* struct dma_entry allocator
642 *
643 * The next two functions implement the allocator for
644 * struct dma_debug_entries.
645 */
646static struct dma_debug_entry *dma_entry_alloc(void)
647{
29cdd4e4 648 struct dma_debug_entry *entry;
3b1e79ed
JR
649 unsigned long flags;
650
651 spin_lock_irqsave(&free_entries_lock, flags);
652
653 if (list_empty(&free_entries)) {
e7ed70ee 654 pr_err("DMA-API: debugging out of memory - disabling\n");
3b1e79ed 655 global_disable = true;
29cdd4e4
JK
656 spin_unlock_irqrestore(&free_entries_lock, flags);
657 return NULL;
3b1e79ed
JR
658 }
659
e6a1a89d 660 entry = __dma_entry_alloc();
3b1e79ed 661
29cdd4e4
JK
662 spin_unlock_irqrestore(&free_entries_lock, flags);
663
6c132d1b
DW
664#ifdef CONFIG_STACKTRACE
665 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
666 entry->stacktrace.entries = entry->st_entries;
667 entry->stacktrace.skip = 2;
668 save_stack_trace(&entry->stacktrace);
669#endif
3b1e79ed 670
3b1e79ed
JR
671 return entry;
672}
673
674static void dma_entry_free(struct dma_debug_entry *entry)
675{
676 unsigned long flags;
677
3b7a6418 678 active_cacheline_remove(entry);
0abdd7a8 679
3b1e79ed
JR
680 /*
681 * add to beginning of the list - this way the entries are
682 * more likely cache hot when they are reallocated.
683 */
684 spin_lock_irqsave(&free_entries_lock, flags);
685 list_add(&entry->list, &free_entries);
686 num_free_entries += 1;
687 spin_unlock_irqrestore(&free_entries_lock, flags);
688}
689
e6a1a89d
FT
690int dma_debug_resize_entries(u32 num_entries)
691{
692 int i, delta, ret = 0;
693 unsigned long flags;
694 struct dma_debug_entry *entry;
695 LIST_HEAD(tmp);
696
697 spin_lock_irqsave(&free_entries_lock, flags);
698
699 if (nr_total_entries < num_entries) {
700 delta = num_entries - nr_total_entries;
701
702 spin_unlock_irqrestore(&free_entries_lock, flags);
703
704 for (i = 0; i < delta; i++) {
705 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
706 if (!entry)
707 break;
708
709 list_add_tail(&entry->list, &tmp);
710 }
711
712 spin_lock_irqsave(&free_entries_lock, flags);
713
714 list_splice(&tmp, &free_entries);
715 nr_total_entries += i;
716 num_free_entries += i;
717 } else {
718 delta = nr_total_entries - num_entries;
719
720 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
721 entry = __dma_entry_alloc();
722 kfree(entry);
723 }
724
725 nr_total_entries -= i;
726 }
727
728 if (nr_total_entries != num_entries)
729 ret = 1;
730
731 spin_unlock_irqrestore(&free_entries_lock, flags);
732
733 return ret;
734}
735EXPORT_SYMBOL(dma_debug_resize_entries);
736
6bf07871
JR
737/*
738 * DMA-API debugging init code
739 *
740 * The init code does two things:
741 * 1. Initialize core data structures
742 * 2. Preallocate a given number of dma_debug_entry structs
743 */
744
745static int prealloc_memory(u32 num_entries)
746{
747 struct dma_debug_entry *entry, *next_entry;
748 int i;
749
750 for (i = 0; i < num_entries; ++i) {
751 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
752 if (!entry)
753 goto out_err;
754
755 list_add_tail(&entry->list, &free_entries);
756 }
757
758 num_free_entries = num_entries;
759 min_free_entries = num_entries;
760
e7ed70ee 761 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
6bf07871
JR
762
763 return 0;
764
765out_err:
766
767 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
768 list_del(&entry->list);
769 kfree(entry);
770 }
771
772 return -ENOMEM;
773}
774
8a6fc708
JR
775static ssize_t filter_read(struct file *file, char __user *user_buf,
776 size_t count, loff_t *ppos)
777{
8a6fc708 778 char buf[NAME_MAX_LEN + 1];
c17e2cf7 779 unsigned long flags;
8a6fc708
JR
780 int len;
781
782 if (!current_driver_name[0])
783 return 0;
784
785 /*
786 * We can't copy to userspace directly because current_driver_name can
787 * only be read under the driver_name_lock with irqs disabled. So
788 * create a temporary copy first.
789 */
790 read_lock_irqsave(&driver_name_lock, flags);
791 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
792 read_unlock_irqrestore(&driver_name_lock, flags);
793
794 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
795}
796
797static ssize_t filter_write(struct file *file, const char __user *userbuf,
798 size_t count, loff_t *ppos)
799{
8a6fc708 800 char buf[NAME_MAX_LEN];
c17e2cf7
JR
801 unsigned long flags;
802 size_t len;
8a6fc708
JR
803 int i;
804
805 /*
806 * We can't copy from userspace directly. Access to
807 * current_driver_name is protected with a write_lock with irqs
808 * disabled. Since copy_from_user can fault and may sleep we
809 * need to copy to temporary buffer first
810 */
e7ed70ee 811 len = min(count, (size_t)(NAME_MAX_LEN - 1));
8a6fc708
JR
812 if (copy_from_user(buf, userbuf, len))
813 return -EFAULT;
814
815 buf[len] = 0;
816
817 write_lock_irqsave(&driver_name_lock, flags);
818
31232509
JR
819 /*
820 * Now handle the string we got from userspace very carefully.
8a6fc708
JR
821 * The rules are:
822 * - only use the first token we got
823 * - token delimiter is everything looking like a space
824 * character (' ', '\n', '\t' ...)
825 *
826 */
827 if (!isalnum(buf[0])) {
828 /*
31232509 829 * If the first character userspace gave us is not
8a6fc708
JR
830 * alphanumerical then assume the filter should be
831 * switched off.
832 */
833 if (current_driver_name[0])
e7ed70ee 834 pr_info("DMA-API: switching off dma-debug driver filter\n");
8a6fc708
JR
835 current_driver_name[0] = 0;
836 current_driver = NULL;
837 goto out_unlock;
838 }
839
840 /*
841 * Now parse out the first token and use it as the name for the
842 * driver to filter for.
843 */
39a37ce1 844 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
8a6fc708
JR
845 current_driver_name[i] = buf[i];
846 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
847 break;
848 }
849 current_driver_name[i] = 0;
850 current_driver = NULL;
851
e7ed70ee
JR
852 pr_info("DMA-API: enable driver filter for driver [%s]\n",
853 current_driver_name);
8a6fc708
JR
854
855out_unlock:
856 write_unlock_irqrestore(&driver_name_lock, flags);
857
858 return count;
859}
860
aeb583d0 861static const struct file_operations filter_fops = {
8a6fc708
JR
862 .read = filter_read,
863 .write = filter_write,
6038f373 864 .llseek = default_llseek,
8a6fc708
JR
865};
866
788dcfa6
JR
867static int dma_debug_fs_init(void)
868{
869 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
870 if (!dma_debug_dent) {
e7ed70ee 871 pr_err("DMA-API: can not create debugfs directory\n");
788dcfa6
JR
872 return -ENOMEM;
873 }
874
875 global_disable_dent = debugfs_create_bool("disabled", 0444,
876 dma_debug_dent,
68ee6d22 877 &global_disable);
788dcfa6
JR
878 if (!global_disable_dent)
879 goto out_err;
880
881 error_count_dent = debugfs_create_u32("error_count", 0444,
882 dma_debug_dent, &error_count);
883 if (!error_count_dent)
884 goto out_err;
885
886 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
887 dma_debug_dent,
888 &show_all_errors);
889 if (!show_all_errors_dent)
890 goto out_err;
891
892 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
893 dma_debug_dent,
894 &show_num_errors);
895 if (!show_num_errors_dent)
896 goto out_err;
897
898 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
899 dma_debug_dent,
900 &num_free_entries);
901 if (!num_free_entries_dent)
902 goto out_err;
903
904 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
905 dma_debug_dent,
906 &min_free_entries);
907 if (!min_free_entries_dent)
908 goto out_err;
909
8a6fc708
JR
910 filter_dent = debugfs_create_file("driver_filter", 0644,
911 dma_debug_dent, NULL, &filter_fops);
912 if (!filter_dent)
913 goto out_err;
914
788dcfa6
JR
915 return 0;
916
917out_err:
918 debugfs_remove_recursive(dma_debug_dent);
919
920 return -ENOMEM;
921}
922
ba4b87ad 923static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
ed888aef
JR
924{
925 struct dma_debug_entry *entry;
926 unsigned long flags;
927 int count = 0, i;
928
be81c6ea
JR
929 local_irq_save(flags);
930
ed888aef 931 for (i = 0; i < HASH_SIZE; ++i) {
be81c6ea 932 spin_lock(&dma_entry_hash[i].lock);
ed888aef 933 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
ba4b87ad 934 if (entry->dev == dev) {
ed888aef 935 count += 1;
ba4b87ad
SG
936 *out_entry = entry;
937 }
ed888aef 938 }
be81c6ea 939 spin_unlock(&dma_entry_hash[i].lock);
ed888aef
JR
940 }
941
be81c6ea
JR
942 local_irq_restore(flags);
943
ed888aef
JR
944 return count;
945}
946
a8fe9ea2 947static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
ed888aef
JR
948{
949 struct device *dev = data;
ba4b87ad 950 struct dma_debug_entry *uninitialized_var(entry);
ed888aef
JR
951 int count;
952
01ce18b3 953 if (dma_debug_disabled())
a8fe9ea2 954 return 0;
ed888aef
JR
955
956 switch (action) {
957 case BUS_NOTIFY_UNBOUND_DRIVER:
ba4b87ad 958 count = device_dma_allocations(dev, &entry);
ed888aef
JR
959 if (count == 0)
960 break;
ba4b87ad 961 err_printk(dev, entry, "DMA-API: device driver has pending "
ed888aef 962 "DMA allocations while released from device "
ba4b87ad
SG
963 "[count=%d]\n"
964 "One of leaked entries details: "
965 "[device address=0x%016llx] [size=%llu bytes] "
966 "[mapped with %s] [mapped as %s]\n",
967 count, entry->dev_addr, entry->size,
968 dir2name[entry->direction], type2name[entry->type]);
ed888aef
JR
969 break;
970 default:
971 break;
972 }
973
974 return 0;
975}
976
41531c8f
JR
977void dma_debug_add_bus(struct bus_type *bus)
978{
ed888aef
JR
979 struct notifier_block *nb;
980
01ce18b3 981 if (dma_debug_disabled())
f797d988
SR
982 return;
983
ed888aef
JR
984 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
985 if (nb == NULL) {
e7ed70ee 986 pr_err("dma_debug_add_bus: out of memory\n");
ed888aef
JR
987 return;
988 }
989
990 nb->notifier_call = dma_debug_device_change;
991
992 bus_register_notifier(bus, nb);
41531c8f 993}
788dcfa6 994
6bf07871
JR
995/*
996 * Let the architectures decide how many entries should be preallocated.
997 */
998void dma_debug_init(u32 num_entries)
999{
1000 int i;
1001
01ce18b3 1002 if (dma_debug_disabled())
6bf07871
JR
1003 return;
1004
1005 for (i = 0; i < HASH_SIZE; ++i) {
1006 INIT_LIST_HEAD(&dma_entry_hash[i].list);
b0a5b83e 1007 spin_lock_init(&dma_entry_hash[i].lock);
6bf07871
JR
1008 }
1009
788dcfa6 1010 if (dma_debug_fs_init() != 0) {
e7ed70ee 1011 pr_err("DMA-API: error creating debugfs entries - disabling\n");
788dcfa6
JR
1012 global_disable = true;
1013
1014 return;
1015 }
1016
59d3daaf
JR
1017 if (req_entries)
1018 num_entries = req_entries;
1019
6bf07871 1020 if (prealloc_memory(num_entries) != 0) {
e7ed70ee 1021 pr_err("DMA-API: debugging out of memory error - disabled\n");
6bf07871
JR
1022 global_disable = true;
1023
1024 return;
1025 }
1026
e6a1a89d
FT
1027 nr_total_entries = num_free_entries;
1028
e7ed70ee 1029 pr_info("DMA-API: debugging enabled by kernel config\n");
6bf07871
JR
1030}
1031
59d3daaf
JR
1032static __init int dma_debug_cmdline(char *str)
1033{
1034 if (!str)
1035 return -EINVAL;
1036
1037 if (strncmp(str, "off", 3) == 0) {
e7ed70ee 1038 pr_info("DMA-API: debugging disabled on kernel command line\n");
59d3daaf
JR
1039 global_disable = true;
1040 }
1041
1042 return 0;
1043}
1044
1045static __init int dma_debug_entries_cmdline(char *str)
1046{
1047 int res;
1048
1049 if (!str)
1050 return -EINVAL;
1051
1052 res = get_option(&str, &req_entries);
1053
1054 if (!res)
1055 req_entries = 0;
1056
1057 return 0;
1058}
1059
1060__setup("dma_debug=", dma_debug_cmdline);
1061__setup("dma_debug_entries=", dma_debug_entries_cmdline);
1062
2d62ece1
JR
1063static void check_unmap(struct dma_debug_entry *ref)
1064{
1065 struct dma_debug_entry *entry;
1066 struct hash_bucket *bucket;
1067 unsigned long flags;
1068
2d62ece1 1069 bucket = get_hash_bucket(ref, &flags);
c6a21d0b 1070 entry = bucket_find_exact(bucket, ref);
2d62ece1
JR
1071
1072 if (!entry) {
8d640a51
AD
1073 /* must drop lock before calling dma_mapping_error */
1074 put_hash_bucket(bucket, &flags);
1075
bfe0fb0f
SK
1076 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1077 err_printk(ref->dev, NULL,
8d640a51
AD
1078 "DMA-API: device driver tries to free an "
1079 "invalid DMA memory address\n");
1080 } else {
1081 err_printk(ref->dev, NULL,
1082 "DMA-API: device driver tries to free DMA "
1083 "memory it has not allocated [device "
1084 "address=0x%016llx] [size=%llu bytes]\n",
1085 ref->dev_addr, ref->size);
bfe0fb0f 1086 }
8d640a51 1087 return;
2d62ece1
JR
1088 }
1089
1090 if (ref->size != entry->size) {
6c132d1b 1091 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
1092 "DMA memory with different size "
1093 "[device address=0x%016llx] [map size=%llu bytes] "
1094 "[unmap size=%llu bytes]\n",
1095 ref->dev_addr, entry->size, ref->size);
1096 }
1097
1098 if (ref->type != entry->type) {
6c132d1b 1099 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
1100 "DMA memory with wrong function "
1101 "[device address=0x%016llx] [size=%llu bytes] "
1102 "[mapped as %s] [unmapped as %s]\n",
1103 ref->dev_addr, ref->size,
1104 type2name[entry->type], type2name[ref->type]);
1105 } else if ((entry->type == dma_debug_coherent) &&
0abdd7a8 1106 (phys_addr(ref) != phys_addr(entry))) {
6c132d1b 1107 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
1108 "DMA memory with different CPU address "
1109 "[device address=0x%016llx] [size=%llu bytes] "
59a40e70
JR
1110 "[cpu alloc address=0x%016llx] "
1111 "[cpu free address=0x%016llx]",
2d62ece1 1112 ref->dev_addr, ref->size,
0abdd7a8
DW
1113 phys_addr(entry),
1114 phys_addr(ref));
2d62ece1
JR
1115 }
1116
1117 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1118 ref->sg_call_ents != entry->sg_call_ents) {
6c132d1b 1119 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
1120 "DMA sg list with different entry count "
1121 "[map count=%d] [unmap count=%d]\n",
1122 entry->sg_call_ents, ref->sg_call_ents);
1123 }
1124
1125 /*
1126 * This may be no bug in reality - but most implementations of the
1127 * DMA API don't handle this properly, so check for it here
1128 */
1129 if (ref->direction != entry->direction) {
6c132d1b 1130 err_printk(ref->dev, entry, "DMA-API: device driver frees "
2d62ece1
JR
1131 "DMA memory with different direction "
1132 "[device address=0x%016llx] [size=%llu bytes] "
1133 "[mapped with %s] [unmapped with %s]\n",
1134 ref->dev_addr, ref->size,
1135 dir2name[entry->direction],
1136 dir2name[ref->direction]);
1137 }
1138
6c9c6d63
SK
1139 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1140 err_printk(ref->dev, entry,
1141 "DMA-API: device driver failed to check map error"
1142 "[device address=0x%016llx] [size=%llu bytes] "
1143 "[mapped as %s]",
1144 ref->dev_addr, ref->size,
1145 type2name[entry->type]);
1146 }
1147
2d62ece1
JR
1148 hash_bucket_del(entry);
1149 dma_entry_free(entry);
1150
2d62ece1
JR
1151 put_hash_bucket(bucket, &flags);
1152}
1153
1154static void check_for_stack(struct device *dev, void *addr)
1155{
1156 if (object_is_on_stack(addr))
f9134be4 1157 err_printk(dev, NULL, "DMA-API: device driver maps memory from "
6c132d1b 1158 "stack [addr=%p]\n", addr);
2d62ece1
JR
1159}
1160
f39d1b97 1161static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
2e34bde1 1162{
f39d1b97
IM
1163 unsigned long a1 = (unsigned long)addr;
1164 unsigned long b1 = a1 + len;
1165 unsigned long a2 = (unsigned long)start;
1166 unsigned long b2 = (unsigned long)end;
2e34bde1 1167
f39d1b97 1168 return !(b1 <= a2 || a1 >= b2);
2e34bde1
JR
1169}
1170
f39d1b97 1171static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
2e34bde1 1172{
f39d1b97
IM
1173 if (overlap(addr, len, _text, _etext) ||
1174 overlap(addr, len, __start_rodata, __end_rodata))
1175 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
2e34bde1
JR
1176}
1177
aa010efb
JR
1178static void check_sync(struct device *dev,
1179 struct dma_debug_entry *ref,
1180 bool to_cpu)
2d62ece1 1181{
2d62ece1
JR
1182 struct dma_debug_entry *entry;
1183 struct hash_bucket *bucket;
1184 unsigned long flags;
1185
aa010efb 1186 bucket = get_hash_bucket(ref, &flags);
2d62ece1 1187
c6a21d0b 1188 entry = bucket_find_contain(&bucket, ref, &flags);
2d62ece1
JR
1189
1190 if (!entry) {
6c132d1b 1191 err_printk(dev, NULL, "DMA-API: device driver tries "
2d62ece1
JR
1192 "to sync DMA memory it has not allocated "
1193 "[device address=0x%016llx] [size=%llu bytes]\n",
aa010efb 1194 (unsigned long long)ref->dev_addr, ref->size);
2d62ece1
JR
1195 goto out;
1196 }
1197
aa010efb 1198 if (ref->size > entry->size) {
6c132d1b 1199 err_printk(dev, entry, "DMA-API: device driver syncs"
2d62ece1
JR
1200 " DMA memory outside allocated range "
1201 "[device address=0x%016llx] "
aa010efb
JR
1202 "[allocation size=%llu bytes] "
1203 "[sync offset+size=%llu]\n",
1204 entry->dev_addr, entry->size,
1205 ref->size);
2d62ece1
JR
1206 }
1207
42d53b4f
KH
1208 if (entry->direction == DMA_BIDIRECTIONAL)
1209 goto out;
1210
aa010efb 1211 if (ref->direction != entry->direction) {
6c132d1b 1212 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
1213 "DMA memory with different direction "
1214 "[device address=0x%016llx] [size=%llu bytes] "
1215 "[mapped with %s] [synced with %s]\n",
aa010efb 1216 (unsigned long long)ref->dev_addr, entry->size,
2d62ece1 1217 dir2name[entry->direction],
aa010efb 1218 dir2name[ref->direction]);
2d62ece1
JR
1219 }
1220
2d62ece1 1221 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
aa010efb 1222 !(ref->direction == DMA_TO_DEVICE))
6c132d1b 1223 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
1224 "device read-only DMA memory for cpu "
1225 "[device address=0x%016llx] [size=%llu bytes] "
1226 "[mapped with %s] [synced with %s]\n",
aa010efb 1227 (unsigned long long)ref->dev_addr, entry->size,
2d62ece1 1228 dir2name[entry->direction],
aa010efb 1229 dir2name[ref->direction]);
2d62ece1
JR
1230
1231 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
aa010efb 1232 !(ref->direction == DMA_FROM_DEVICE))
6c132d1b 1233 err_printk(dev, entry, "DMA-API: device driver syncs "
2d62ece1
JR
1234 "device write-only DMA memory to device "
1235 "[device address=0x%016llx] [size=%llu bytes] "
1236 "[mapped with %s] [synced with %s]\n",
aa010efb 1237 (unsigned long long)ref->dev_addr, entry->size,
2d62ece1 1238 dir2name[entry->direction],
aa010efb 1239 dir2name[ref->direction]);
2d62ece1
JR
1240
1241out:
1242 put_hash_bucket(bucket, &flags);
2d62ece1
JR
1243}
1244
f62bc980
JR
1245void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1246 size_t size, int direction, dma_addr_t dma_addr,
1247 bool map_single)
1248{
1249 struct dma_debug_entry *entry;
1250
01ce18b3 1251 if (unlikely(dma_debug_disabled()))
f62bc980
JR
1252 return;
1253
bfe0fb0f 1254 if (dma_mapping_error(dev, dma_addr))
f62bc980
JR
1255 return;
1256
1257 entry = dma_entry_alloc();
1258 if (!entry)
1259 return;
1260
1261 entry->dev = dev;
1262 entry->type = dma_debug_page;
0abdd7a8
DW
1263 entry->pfn = page_to_pfn(page);
1264 entry->offset = offset,
f62bc980
JR
1265 entry->dev_addr = dma_addr;
1266 entry->size = size;
1267 entry->direction = direction;
6c9c6d63 1268 entry->map_err_type = MAP_ERR_NOT_CHECKED;
f62bc980 1269
9537a48e 1270 if (map_single)
f62bc980 1271 entry->type = dma_debug_single;
9537a48e
JR
1272
1273 if (!PageHighMem(page)) {
f39d1b97
IM
1274 void *addr = page_address(page) + offset;
1275
2e34bde1
JR
1276 check_for_stack(dev, addr);
1277 check_for_illegal_area(dev, addr, size);
f62bc980
JR
1278 }
1279
1280 add_dma_entry(entry);
1281}
1282EXPORT_SYMBOL(debug_dma_map_page);
1283
6c9c6d63
SK
1284void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1285{
1286 struct dma_debug_entry ref;
1287 struct dma_debug_entry *entry;
1288 struct hash_bucket *bucket;
1289 unsigned long flags;
1290
01ce18b3 1291 if (unlikely(dma_debug_disabled()))
6c9c6d63
SK
1292 return;
1293
1294 ref.dev = dev;
1295 ref.dev_addr = dma_addr;
1296 bucket = get_hash_bucket(&ref, &flags);
6c9c6d63 1297
96e7d7a1
AD
1298 list_for_each_entry(entry, &bucket->list, list) {
1299 if (!exact_match(&ref, entry))
1300 continue;
1301
1302 /*
1303 * The same physical address can be mapped multiple
1304 * times. Without a hardware IOMMU this results in the
1305 * same device addresses being put into the dma-debug
1306 * hash multiple times too. This can result in false
1307 * positives being reported. Therefore we implement a
1308 * best-fit algorithm here which updates the first entry
1309 * from the hash which fits the reference value and is
1310 * not currently listed as being checked.
1311 */
1312 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1313 entry->map_err_type = MAP_ERR_CHECKED;
1314 break;
1315 }
1316 }
6c9c6d63 1317
6c9c6d63
SK
1318 put_hash_bucket(bucket, &flags);
1319}
1320EXPORT_SYMBOL(debug_dma_mapping_error);
1321
f62bc980
JR
1322void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1323 size_t size, int direction, bool map_single)
1324{
1325 struct dma_debug_entry ref = {
1326 .type = dma_debug_page,
1327 .dev = dev,
1328 .dev_addr = addr,
1329 .size = size,
1330 .direction = direction,
1331 };
1332
01ce18b3 1333 if (unlikely(dma_debug_disabled()))
f62bc980
JR
1334 return;
1335
1336 if (map_single)
1337 ref.type = dma_debug_single;
1338
1339 check_unmap(&ref);
1340}
1341EXPORT_SYMBOL(debug_dma_unmap_page);
1342
972aa45c
JR
1343void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1344 int nents, int mapped_ents, int direction)
1345{
1346 struct dma_debug_entry *entry;
1347 struct scatterlist *s;
1348 int i;
1349
01ce18b3 1350 if (unlikely(dma_debug_disabled()))
972aa45c
JR
1351 return;
1352
1353 for_each_sg(sg, s, mapped_ents, i) {
1354 entry = dma_entry_alloc();
1355 if (!entry)
1356 return;
1357
1358 entry->type = dma_debug_sg;
1359 entry->dev = dev;
0abdd7a8
DW
1360 entry->pfn = page_to_pfn(sg_page(s));
1361 entry->offset = s->offset,
884d0597 1362 entry->size = sg_dma_len(s);
15aedea4 1363 entry->dev_addr = sg_dma_address(s);
972aa45c
JR
1364 entry->direction = direction;
1365 entry->sg_call_ents = nents;
1366 entry->sg_mapped_ents = mapped_ents;
1367
9537a48e
JR
1368 if (!PageHighMem(sg_page(s))) {
1369 check_for_stack(dev, sg_virt(s));
884d0597 1370 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
9537a48e 1371 }
972aa45c
JR
1372
1373 add_dma_entry(entry);
1374 }
1375}
1376EXPORT_SYMBOL(debug_dma_map_sg);
1377
aa010efb
JR
1378static int get_nr_mapped_entries(struct device *dev,
1379 struct dma_debug_entry *ref)
88f3907f 1380{
aa010efb 1381 struct dma_debug_entry *entry;
88f3907f
FT
1382 struct hash_bucket *bucket;
1383 unsigned long flags;
c17e2cf7 1384 int mapped_ents;
88f3907f 1385
aa010efb 1386 bucket = get_hash_bucket(ref, &flags);
c6a21d0b 1387 entry = bucket_find_exact(bucket, ref);
c17e2cf7 1388 mapped_ents = 0;
88f3907f 1389
88f3907f
FT
1390 if (entry)
1391 mapped_ents = entry->sg_mapped_ents;
1392 put_hash_bucket(bucket, &flags);
1393
1394 return mapped_ents;
1395}
1396
972aa45c
JR
1397void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1398 int nelems, int dir)
1399{
972aa45c
JR
1400 struct scatterlist *s;
1401 int mapped_ents = 0, i;
972aa45c 1402
01ce18b3 1403 if (unlikely(dma_debug_disabled()))
972aa45c
JR
1404 return;
1405
1406 for_each_sg(sglist, s, nelems, i) {
1407
1408 struct dma_debug_entry ref = {
1409 .type = dma_debug_sg,
1410 .dev = dev,
0abdd7a8
DW
1411 .pfn = page_to_pfn(sg_page(s)),
1412 .offset = s->offset,
15aedea4 1413 .dev_addr = sg_dma_address(s),
884d0597 1414 .size = sg_dma_len(s),
972aa45c 1415 .direction = dir,
e5e8c5b9 1416 .sg_call_ents = nelems,
972aa45c
JR
1417 };
1418
1419 if (mapped_ents && i >= mapped_ents)
1420 break;
1421
e5e8c5b9 1422 if (!i)
aa010efb 1423 mapped_ents = get_nr_mapped_entries(dev, &ref);
972aa45c
JR
1424
1425 check_unmap(&ref);
1426 }
1427}
1428EXPORT_SYMBOL(debug_dma_unmap_sg);
1429
6bfd4498
JR
1430void debug_dma_alloc_coherent(struct device *dev, size_t size,
1431 dma_addr_t dma_addr, void *virt)
1432{
1433 struct dma_debug_entry *entry;
1434
01ce18b3 1435 if (unlikely(dma_debug_disabled()))
6bfd4498
JR
1436 return;
1437
1438 if (unlikely(virt == NULL))
1439 return;
1440
1441 entry = dma_entry_alloc();
1442 if (!entry)
1443 return;
1444
1445 entry->type = dma_debug_coherent;
1446 entry->dev = dev;
0abdd7a8
DW
1447 entry->pfn = page_to_pfn(virt_to_page(virt));
1448 entry->offset = (size_t) virt & PAGE_MASK;
6bfd4498
JR
1449 entry->size = size;
1450 entry->dev_addr = dma_addr;
1451 entry->direction = DMA_BIDIRECTIONAL;
1452
1453 add_dma_entry(entry);
1454}
1455EXPORT_SYMBOL(debug_dma_alloc_coherent);
1456
1457void debug_dma_free_coherent(struct device *dev, size_t size,
1458 void *virt, dma_addr_t addr)
1459{
1460 struct dma_debug_entry ref = {
1461 .type = dma_debug_coherent,
1462 .dev = dev,
0abdd7a8
DW
1463 .pfn = page_to_pfn(virt_to_page(virt)),
1464 .offset = (size_t) virt & PAGE_MASK,
6bfd4498
JR
1465 .dev_addr = addr,
1466 .size = size,
1467 .direction = DMA_BIDIRECTIONAL,
1468 };
1469
01ce18b3 1470 if (unlikely(dma_debug_disabled()))
6bfd4498
JR
1471 return;
1472
1473 check_unmap(&ref);
1474}
1475EXPORT_SYMBOL(debug_dma_free_coherent);
1476
b9d2317e
JR
1477void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1478 size_t size, int direction)
1479{
aa010efb
JR
1480 struct dma_debug_entry ref;
1481
01ce18b3 1482 if (unlikely(dma_debug_disabled()))
b9d2317e
JR
1483 return;
1484
aa010efb
JR
1485 ref.type = dma_debug_single;
1486 ref.dev = dev;
1487 ref.dev_addr = dma_handle;
1488 ref.size = size;
1489 ref.direction = direction;
1490 ref.sg_call_ents = 0;
1491
1492 check_sync(dev, &ref, true);
b9d2317e
JR
1493}
1494EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1495
1496void debug_dma_sync_single_for_device(struct device *dev,
1497 dma_addr_t dma_handle, size_t size,
1498 int direction)
1499{
aa010efb
JR
1500 struct dma_debug_entry ref;
1501
01ce18b3 1502 if (unlikely(dma_debug_disabled()))
b9d2317e
JR
1503 return;
1504
aa010efb
JR
1505 ref.type = dma_debug_single;
1506 ref.dev = dev;
1507 ref.dev_addr = dma_handle;
1508 ref.size = size;
1509 ref.direction = direction;
1510 ref.sg_call_ents = 0;
1511
1512 check_sync(dev, &ref, false);
b9d2317e
JR
1513}
1514EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1515
948408ba
JR
1516void debug_dma_sync_single_range_for_cpu(struct device *dev,
1517 dma_addr_t dma_handle,
1518 unsigned long offset, size_t size,
1519 int direction)
1520{
aa010efb
JR
1521 struct dma_debug_entry ref;
1522
01ce18b3 1523 if (unlikely(dma_debug_disabled()))
948408ba
JR
1524 return;
1525
aa010efb
JR
1526 ref.type = dma_debug_single;
1527 ref.dev = dev;
1528 ref.dev_addr = dma_handle;
1529 ref.size = offset + size;
1530 ref.direction = direction;
1531 ref.sg_call_ents = 0;
1532
1533 check_sync(dev, &ref, true);
948408ba
JR
1534}
1535EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1536
1537void debug_dma_sync_single_range_for_device(struct device *dev,
1538 dma_addr_t dma_handle,
1539 unsigned long offset,
1540 size_t size, int direction)
1541{
aa010efb
JR
1542 struct dma_debug_entry ref;
1543
01ce18b3 1544 if (unlikely(dma_debug_disabled()))
948408ba
JR
1545 return;
1546
aa010efb
JR
1547 ref.type = dma_debug_single;
1548 ref.dev = dev;
1549 ref.dev_addr = dma_handle;
1550 ref.size = offset + size;
1551 ref.direction = direction;
1552 ref.sg_call_ents = 0;
1553
1554 check_sync(dev, &ref, false);
948408ba
JR
1555}
1556EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1557
a31fba5d
JR
1558void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1559 int nelems, int direction)
1560{
1561 struct scatterlist *s;
88f3907f 1562 int mapped_ents = 0, i;
a31fba5d 1563
01ce18b3 1564 if (unlikely(dma_debug_disabled()))
a31fba5d
JR
1565 return;
1566
1567 for_each_sg(sg, s, nelems, i) {
aa010efb
JR
1568
1569 struct dma_debug_entry ref = {
1570 .type = dma_debug_sg,
1571 .dev = dev,
0abdd7a8
DW
1572 .pfn = page_to_pfn(sg_page(s)),
1573 .offset = s->offset,
aa010efb
JR
1574 .dev_addr = sg_dma_address(s),
1575 .size = sg_dma_len(s),
1576 .direction = direction,
1577 .sg_call_ents = nelems,
1578 };
1579
88f3907f 1580 if (!i)
aa010efb 1581 mapped_ents = get_nr_mapped_entries(dev, &ref);
88f3907f
FT
1582
1583 if (i >= mapped_ents)
1584 break;
1585
aa010efb 1586 check_sync(dev, &ref, true);
a31fba5d
JR
1587 }
1588}
1589EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1590
1591void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1592 int nelems, int direction)
1593{
1594 struct scatterlist *s;
88f3907f 1595 int mapped_ents = 0, i;
a31fba5d 1596
01ce18b3 1597 if (unlikely(dma_debug_disabled()))
a31fba5d
JR
1598 return;
1599
1600 for_each_sg(sg, s, nelems, i) {
aa010efb
JR
1601
1602 struct dma_debug_entry ref = {
1603 .type = dma_debug_sg,
1604 .dev = dev,
0abdd7a8
DW
1605 .pfn = page_to_pfn(sg_page(s)),
1606 .offset = s->offset,
aa010efb
JR
1607 .dev_addr = sg_dma_address(s),
1608 .size = sg_dma_len(s),
1609 .direction = direction,
1610 .sg_call_ents = nelems,
1611 };
88f3907f 1612 if (!i)
aa010efb 1613 mapped_ents = get_nr_mapped_entries(dev, &ref);
88f3907f
FT
1614
1615 if (i >= mapped_ents)
1616 break;
1617
aa010efb 1618 check_sync(dev, &ref, false);
a31fba5d
JR
1619 }
1620}
1621EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1622
1745de5e
JR
1623static int __init dma_debug_driver_setup(char *str)
1624{
1625 int i;
1626
1627 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1628 current_driver_name[i] = *str;
1629 if (*str == 0)
1630 break;
1631 }
1632
1633 if (current_driver_name[0])
e7ed70ee
JR
1634 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1635 current_driver_name);
1745de5e
JR
1636
1637
1638 return 1;
1639}
1640__setup("dma_debug_driver=", dma_debug_driver_setup);