blob: c38083871f11dbaf3f7252674b937fe5e161380a [file] [log] [blame]
Joerg Roedelf2f45e52009-01-09 12:19:52 +01001/*
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
Joerg Roedel972aa452009-01-09 14:19:54 +010020#include <linux/scatterlist.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010021#include <linux/dma-mapping.h>
David Woodhouse6c132d12009-01-19 16:52:39 +010022#include <linux/stacktrace.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010023#include <linux/dma-debug.h>
Joerg Roedel30dfa902009-01-09 12:34:49 +010024#include <linux/spinlock.h>
Joerg Roedel788dcfa2009-01-09 13:13:27 +010025#include <linux/debugfs.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020026#include <linux/uaccess.h>
Paul Gortmaker23a7bfa2011-07-01 16:23:59 -040027#include <linux/export.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010028#include <linux/device.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010029#include <linux/types.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010030#include <linux/sched.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020031#include <linux/ctype.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010032#include <linux/list.h>
Joerg Roedel6bf07872009-01-09 12:54:42 +010033#include <linux/slab.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010034
Joerg Roedel2e34bde2009-03-16 16:51:55 +010035#include <asm/sections.h>
36
Joerg Roedel30dfa902009-01-09 12:34:49 +010037#define HASH_SIZE 1024ULL
38#define HASH_FN_SHIFT 13
39#define HASH_FN_MASK (HASH_SIZE - 1)
40
Joerg Roedelf2f45e52009-01-09 12:19:52 +010041enum {
42 dma_debug_single,
43 dma_debug_page,
44 dma_debug_sg,
45 dma_debug_coherent,
46};
47
Shuah Khan6c9c6d62012-10-08 11:08:06 -060048enum map_err_types {
49 MAP_ERR_CHECK_NOT_APPLICABLE,
50 MAP_ERR_NOT_CHECKED,
51 MAP_ERR_CHECKED,
52};
53
David Woodhouse6c132d12009-01-19 16:52:39 +010054#define DMA_DEBUG_STACKTRACE_ENTRIES 5
55
Dan Williams0abdd7a2014-01-21 15:48:12 -080056/**
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 */
Joerg Roedelf2f45e52009-01-09 12:19:52 +010070struct dma_debug_entry {
71 struct list_head list;
72 struct device *dev;
73 int type;
Dan Williams0abdd7a2014-01-21 15:48:12 -080074 unsigned long pfn;
75 size_t offset;
Joerg Roedelf2f45e52009-01-09 12:19:52 +010076 u64 dev_addr;
77 u64 size;
78 int direction;
79 int sg_call_ents;
80 int sg_mapped_ents;
Shuah Khan6c9c6d62012-10-08 11:08:06 -060081 enum map_err_types map_err_type;
David Woodhouse6c132d12009-01-19 16:52:39 +010082#ifdef CONFIG_STACKTRACE
83 struct stack_trace stacktrace;
84 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
85#endif
Joerg Roedelf2f45e52009-01-09 12:19:52 +010086};
87
Neil Hormanc6a21d02011-08-08 15:13:54 -040088typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
89
Joerg Roedel30dfa902009-01-09 12:34:49 +010090struct hash_bucket {
91 struct list_head list;
92 spinlock_t lock;
Joerg Roedel2d62ece2009-01-09 14:10:26 +010093} ____cacheline_aligned_in_smp;
Joerg Roedel30dfa902009-01-09 12:34:49 +010094
95/* Hash list to save the allocated dma addresses */
96static struct hash_bucket dma_entry_hash[HASH_SIZE];
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010097/* 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 */
Dan Carpenter68ee6d22012-06-27 12:08:55 +0300103static u32 global_disable __read_mostly;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100104
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100105/* Global error count */
106static u32 error_count;
107
108/* Global error show enable*/
109static u32 show_all_errors __read_mostly;
110/* Number of errors to show */
111static u32 show_num_errors = 1;
112
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100113static u32 num_free_entries;
114static u32 min_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900115static u32 nr_total_entries;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100116
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100117/* number of preallocated entries requested by kernel cmdline */
118static u32 req_entries;
119
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100120/* debugfs dentry's for the stuff above */
121static struct dentry *dma_debug_dent __read_mostly;
122static struct dentry *global_disable_dent __read_mostly;
123static struct dentry *error_count_dent __read_mostly;
124static struct dentry *show_all_errors_dent __read_mostly;
125static struct dentry *show_num_errors_dent __read_mostly;
126static struct dentry *num_free_entries_dent __read_mostly;
127static struct dentry *min_free_entries_dent __read_mostly;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200128static struct dentry *filter_dent __read_mostly;
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100129
Joerg Roedel2e507d82009-05-22 18:24:20 +0200130/* per-driver filter related state */
131
132#define NAME_MAX_LEN 64
133
134static char current_driver_name[NAME_MAX_LEN] __read_mostly;
135static struct device_driver *current_driver __read_mostly;
136
137static DEFINE_RWLOCK(driver_name_lock);
Joerg Roedel30dfa902009-01-09 12:34:49 +0100138
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600139static const char *const maperr2str[] = {
140 [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
141 [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
142 [MAP_ERR_CHECKED] = "dma map error checked",
143};
144
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100145static const char *type2name[4] = { "single", "page",
146 "scather-gather", "coherent" };
147
148static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
149 "DMA_FROM_DEVICE", "DMA_NONE" };
150
151/*
152 * The access to some variables in this macro is racy. We can't use atomic_t
153 * here because all these variables are exported to debugfs. Some of them even
154 * writeable. This is also the reason why a lock won't help much. But anyway,
155 * the races are no big deal. Here is why:
156 *
157 * error_count: the addition is racy, but the worst thing that can happen is
158 * that we don't count some errors
159 * show_num_errors: the subtraction is racy. Also no big deal because in
160 * worst case this will result in one warning more in the
161 * system log than the user configured. This variable is
162 * writeable via debugfs.
163 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100164static inline void dump_entry_trace(struct dma_debug_entry *entry)
165{
166#ifdef CONFIG_STACKTRACE
167 if (entry) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200168 pr_warning("Mapped at:\n");
David Woodhouse6c132d12009-01-19 16:52:39 +0100169 print_stack_trace(&entry->stacktrace, 0);
170 }
171#endif
172}
173
Joerg Roedel2e507d82009-05-22 18:24:20 +0200174static bool driver_filter(struct device *dev)
175{
Joerg Roedel0bf84122009-06-08 15:53:46 +0200176 struct device_driver *drv;
177 unsigned long flags;
178 bool ret;
179
Joerg Roedel2e507d82009-05-22 18:24:20 +0200180 /* driver filter off */
181 if (likely(!current_driver_name[0]))
182 return true;
183
184 /* driver filter on and initialized */
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400185 if (current_driver && dev && dev->driver == current_driver)
Joerg Roedel2e507d82009-05-22 18:24:20 +0200186 return true;
187
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400188 /* driver filter on, but we can't filter on a NULL device... */
189 if (!dev)
190 return false;
191
Joerg Roedel0bf84122009-06-08 15:53:46 +0200192 if (current_driver || !current_driver_name[0])
193 return false;
194
Joerg Roedel2e507d82009-05-22 18:24:20 +0200195 /* driver filter on but not yet initialized */
Alan Sternf3ff9242012-01-24 13:35:24 -0500196 drv = dev->driver;
Joerg Roedel0bf84122009-06-08 15:53:46 +0200197 if (!drv)
198 return false;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200199
Joerg Roedel0bf84122009-06-08 15:53:46 +0200200 /* lock to protect against change of current_driver_name */
201 read_lock_irqsave(&driver_name_lock, flags);
Joerg Roedel2e507d82009-05-22 18:24:20 +0200202
Joerg Roedel0bf84122009-06-08 15:53:46 +0200203 ret = false;
204 if (drv->name &&
205 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
206 current_driver = drv;
207 ret = true;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200208 }
209
Joerg Roedel0bf84122009-06-08 15:53:46 +0200210 read_unlock_irqrestore(&driver_name_lock, flags);
Joerg Roedel0bf84122009-06-08 15:53:46 +0200211
212 return ret;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200213}
214
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400215#define err_printk(dev, entry, format, arg...) do { \
216 error_count += 1; \
217 if (driver_filter(dev) && \
218 (show_all_errors || show_num_errors > 0)) { \
219 WARN(1, "%s %s: " format, \
220 dev ? dev_driver_string(dev) : "NULL", \
221 dev ? dev_name(dev) : "NULL", ## arg); \
222 dump_entry_trace(entry); \
223 } \
224 if (!show_all_errors && show_num_errors > 0) \
225 show_num_errors -= 1; \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100226 } while (0);
227
Joerg Roedel30dfa902009-01-09 12:34:49 +0100228/*
229 * Hash related functions
230 *
231 * Every DMA-API request is saved into a struct dma_debug_entry. To
232 * have quick access to these structs they are stored into a hash.
233 */
234static int hash_fn(struct dma_debug_entry *entry)
235{
236 /*
237 * Hash function is based on the dma address.
238 * We use bits 20-27 here as the index into the hash
239 */
240 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
241}
242
243/*
244 * Request exclusive access to a hash bucket for a given dma_debug_entry.
245 */
246static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
247 unsigned long *flags)
248{
249 int idx = hash_fn(entry);
250 unsigned long __flags;
251
252 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
253 *flags = __flags;
254 return &dma_entry_hash[idx];
255}
256
257/*
258 * Give up exclusive access to the hash bucket
259 */
260static void put_hash_bucket(struct hash_bucket *bucket,
261 unsigned long *flags)
262{
263 unsigned long __flags = *flags;
264
265 spin_unlock_irqrestore(&bucket->lock, __flags);
266}
267
Neil Hormanc6a21d02011-08-08 15:13:54 -0400268static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
269{
Thomas Jarosch91ec37c2011-11-17 20:31:02 +0100270 return ((a->dev_addr == b->dev_addr) &&
Neil Hormanc6a21d02011-08-08 15:13:54 -0400271 (a->dev == b->dev)) ? true : false;
272}
273
274static bool containing_match(struct dma_debug_entry *a,
275 struct dma_debug_entry *b)
276{
277 if (a->dev != b->dev)
278 return false;
279
280 if ((b->dev_addr <= a->dev_addr) &&
281 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
282 return true;
283
284 return false;
285}
286
Joerg Roedel30dfa902009-01-09 12:34:49 +0100287/*
288 * Search a given entry in the hash bucket list
289 */
Neil Hormanc6a21d02011-08-08 15:13:54 -0400290static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
291 struct dma_debug_entry *ref,
292 match_fn match)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100293{
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200294 struct dma_debug_entry *entry, *ret = NULL;
Ming Leife73fbe2012-10-19 13:57:01 -0700295 int matches = 0, match_lvl, last_lvl = -1;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100296
297 list_for_each_entry(entry, &bucket->list, list) {
Neil Hormanc6a21d02011-08-08 15:13:54 -0400298 if (!match(ref, entry))
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200299 continue;
300
301 /*
302 * Some drivers map the same physical address multiple
303 * times. Without a hardware IOMMU this results in the
304 * same device addresses being put into the dma-debug
305 * hash multiple times too. This can result in false
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200306 * positives being reported. Therefore we implement a
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200307 * best-fit algorithm here which returns the entry from
308 * the hash which fits best to the reference value
309 * instead of the first-fit.
310 */
311 matches += 1;
312 match_lvl = 0;
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200313 entry->size == ref->size ? ++match_lvl : 0;
314 entry->type == ref->type ? ++match_lvl : 0;
315 entry->direction == ref->direction ? ++match_lvl : 0;
316 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200317
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200318 if (match_lvl == 4) {
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200319 /* perfect-fit - return the result */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100320 return entry;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200321 } else if (match_lvl > last_lvl) {
322 /*
323 * We found an entry that fits better then the
Ming Leife73fbe2012-10-19 13:57:01 -0700324 * previous one or it is the 1st match.
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200325 */
326 last_lvl = match_lvl;
327 ret = entry;
328 }
Joerg Roedel30dfa902009-01-09 12:34:49 +0100329 }
330
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200331 /*
332 * If we have multiple matches but no perfect-fit, just return
333 * NULL.
334 */
335 ret = (matches == 1) ? ret : NULL;
336
337 return ret;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100338}
339
Neil Hormanc6a21d02011-08-08 15:13:54 -0400340static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
341 struct dma_debug_entry *ref)
342{
343 return __hash_bucket_find(bucket, ref, exact_match);
344}
345
346static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
347 struct dma_debug_entry *ref,
348 unsigned long *flags)
349{
350
351 unsigned int max_range = dma_get_max_seg_size(ref->dev);
352 struct dma_debug_entry *entry, index = *ref;
353 unsigned int range = 0;
354
355 while (range <= max_range) {
356 entry = __hash_bucket_find(*bucket, &index, containing_match);
357
358 if (entry)
359 return entry;
360
361 /*
362 * Nothing found, go back a hash bucket
363 */
364 put_hash_bucket(*bucket, flags);
365 range += (1 << HASH_FN_SHIFT);
366 index.dev_addr -= (1 << HASH_FN_SHIFT);
367 *bucket = get_hash_bucket(&index, flags);
368 }
369
370 return NULL;
371}
372
Joerg Roedel30dfa902009-01-09 12:34:49 +0100373/*
374 * Add an entry to a hash bucket
375 */
376static void hash_bucket_add(struct hash_bucket *bucket,
377 struct dma_debug_entry *entry)
378{
379 list_add_tail(&entry->list, &bucket->list);
380}
381
382/*
383 * Remove entry from a hash bucket list
384 */
385static void hash_bucket_del(struct dma_debug_entry *entry)
386{
387 list_del(&entry->list);
388}
389
Dan Williams0abdd7a2014-01-21 15:48:12 -0800390static unsigned long long phys_addr(struct dma_debug_entry *entry)
391{
392 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
393}
394
Joerg Roedel30dfa902009-01-09 12:34:49 +0100395/*
David Woodhouseac26c182009-02-12 16:19:13 +0100396 * Dump mapping entries for debugging purposes
397 */
398void debug_dma_dump_mappings(struct device *dev)
399{
400 int idx;
401
402 for (idx = 0; idx < HASH_SIZE; idx++) {
403 struct hash_bucket *bucket = &dma_entry_hash[idx];
404 struct dma_debug_entry *entry;
405 unsigned long flags;
406
407 spin_lock_irqsave(&bucket->lock, flags);
408
409 list_for_each_entry(entry, &bucket->list, list) {
410 if (!dev || dev == entry->dev) {
411 dev_info(entry->dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800412 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
David Woodhouseac26c182009-02-12 16:19:13 +0100413 type2name[entry->type], idx,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800414 phys_addr(entry), entry->pfn,
David Woodhouseac26c182009-02-12 16:19:13 +0100415 entry->dev_addr, entry->size,
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600416 dir2name[entry->direction],
417 maperr2str[entry->map_err_type]);
David Woodhouseac26c182009-02-12 16:19:13 +0100418 }
419 }
420
421 spin_unlock_irqrestore(&bucket->lock, flags);
422 }
423}
424EXPORT_SYMBOL(debug_dma_dump_mappings);
425
426/*
Dan Williams0abdd7a2014-01-21 15:48:12 -0800427 * For each page mapped (initial page in the case of
428 * dma_alloc_coherent/dma_map_{single|page}, or each page in a
429 * scatterlist) insert into this tree using the pfn as the key. At
430 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
431 * the pfn already exists at insertion time add a tag as a reference
432 * count for the overlapping mappings. For now, the overlap tracking
433 * just ensures that 'unmaps' balance 'maps' before marking the pfn
434 * idle, but we should also be flagging overlaps as an API violation.
435 *
436 * Memory usage is mostly constrained by the maximum number of available
437 * dma-debug entries in that we need a free dma_debug_entry before
438 * inserting into the tree. In the case of dma_map_{single|page} and
439 * dma_alloc_coherent there is only one dma_debug_entry and one pfn to
440 * track per event. dma_map_sg(), on the other hand,
441 * consumes a single dma_debug_entry, but inserts 'nents' entries into
442 * the tree.
443 *
444 * At any time debug_dma_assert_idle() can be called to trigger a
445 * warning if the given page is in the active set.
446 */
447static RADIX_TREE(dma_active_pfn, GFP_NOWAIT);
448static DEFINE_SPINLOCK(radix_lock);
449#define ACTIVE_PFN_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
450
451static int active_pfn_read_overlap(unsigned long pfn)
452{
453 int overlap = 0, i;
454
455 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
456 if (radix_tree_tag_get(&dma_active_pfn, pfn, i))
457 overlap |= 1 << i;
458 return overlap;
459}
460
461static int active_pfn_set_overlap(unsigned long pfn, int overlap)
462{
463 int i;
464
465 if (overlap > ACTIVE_PFN_MAX_OVERLAP || overlap < 0)
466 return 0;
467
468 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
469 if (overlap & 1 << i)
470 radix_tree_tag_set(&dma_active_pfn, pfn, i);
471 else
472 radix_tree_tag_clear(&dma_active_pfn, pfn, i);
473
474 return overlap;
475}
476
477static void active_pfn_inc_overlap(unsigned long pfn)
478{
479 int overlap = active_pfn_read_overlap(pfn);
480
481 overlap = active_pfn_set_overlap(pfn, ++overlap);
482
483 /* If we overflowed the overlap counter then we're potentially
484 * leaking dma-mappings. Otherwise, if maps and unmaps are
485 * balanced then this overflow may cause false negatives in
486 * debug_dma_assert_idle() as the pfn may be marked idle
487 * prematurely.
488 */
489 WARN_ONCE(overlap == 0,
490 "DMA-API: exceeded %d overlapping mappings of pfn %lx\n",
491 ACTIVE_PFN_MAX_OVERLAP, pfn);
492}
493
494static int active_pfn_dec_overlap(unsigned long pfn)
495{
496 int overlap = active_pfn_read_overlap(pfn);
497
498 return active_pfn_set_overlap(pfn, --overlap);
499}
500
501static int active_pfn_insert(struct dma_debug_entry *entry)
502{
503 unsigned long flags;
504 int rc;
505
506 spin_lock_irqsave(&radix_lock, flags);
507 rc = radix_tree_insert(&dma_active_pfn, entry->pfn, entry);
508 if (rc == -EEXIST)
509 active_pfn_inc_overlap(entry->pfn);
510 spin_unlock_irqrestore(&radix_lock, flags);
511
512 return rc;
513}
514
515static void active_pfn_remove(struct dma_debug_entry *entry)
516{
517 unsigned long flags;
518
519 spin_lock_irqsave(&radix_lock, flags);
520 if (active_pfn_dec_overlap(entry->pfn) == 0)
521 radix_tree_delete(&dma_active_pfn, entry->pfn);
522 spin_unlock_irqrestore(&radix_lock, flags);
523}
524
525/**
526 * debug_dma_assert_idle() - assert that a page is not undergoing dma
527 * @page: page to lookup in the dma_active_pfn tree
528 *
529 * Place a call to this routine in cases where the cpu touching the page
530 * before the dma completes (page is dma_unmapped) will lead to data
531 * corruption.
532 */
533void debug_dma_assert_idle(struct page *page)
534{
535 unsigned long flags;
536 struct dma_debug_entry *entry;
537
538 if (!page)
539 return;
540
541 spin_lock_irqsave(&radix_lock, flags);
542 entry = radix_tree_lookup(&dma_active_pfn, page_to_pfn(page));
543 spin_unlock_irqrestore(&radix_lock, flags);
544
545 if (!entry)
546 return;
547
548 err_printk(entry->dev, entry,
549 "DMA-API: cpu touching an active dma mapped page "
550 "[pfn=0x%lx]\n", entry->pfn);
551}
552
553/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100554 * Wrapper function for adding an entry to the hash.
555 * This function takes care of locking itself.
556 */
557static void add_dma_entry(struct dma_debug_entry *entry)
558{
559 struct hash_bucket *bucket;
560 unsigned long flags;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800561 int rc;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100562
563 bucket = get_hash_bucket(entry, &flags);
564 hash_bucket_add(bucket, entry);
565 put_hash_bucket(bucket, &flags);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800566
567 rc = active_pfn_insert(entry);
568 if (rc == -ENOMEM) {
569 pr_err("DMA-API: pfn tracking ENOMEM, dma-debug disabled\n");
570 global_disable = true;
571 }
572
573 /* TODO: report -EEXIST errors here as overlapping mappings are
574 * not supported by the DMA API
575 */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100576}
577
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900578static struct dma_debug_entry *__dma_entry_alloc(void)
579{
580 struct dma_debug_entry *entry;
581
582 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
583 list_del(&entry->list);
584 memset(entry, 0, sizeof(*entry));
585
586 num_free_entries -= 1;
587 if (num_free_entries < min_free_entries)
588 min_free_entries = num_free_entries;
589
590 return entry;
591}
592
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100593/* struct dma_entry allocator
594 *
595 * The next two functions implement the allocator for
596 * struct dma_debug_entries.
597 */
598static struct dma_debug_entry *dma_entry_alloc(void)
599{
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200600 struct dma_debug_entry *entry;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100601 unsigned long flags;
602
603 spin_lock_irqsave(&free_entries_lock, flags);
604
605 if (list_empty(&free_entries)) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200606 pr_err("DMA-API: debugging out of memory - disabling\n");
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100607 global_disable = true;
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200608 spin_unlock_irqrestore(&free_entries_lock, flags);
609 return NULL;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100610 }
611
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900612 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100613
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200614 spin_unlock_irqrestore(&free_entries_lock, flags);
615
David Woodhouse6c132d12009-01-19 16:52:39 +0100616#ifdef CONFIG_STACKTRACE
617 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
618 entry->stacktrace.entries = entry->st_entries;
619 entry->stacktrace.skip = 2;
620 save_stack_trace(&entry->stacktrace);
621#endif
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100622
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100623 return entry;
624}
625
626static void dma_entry_free(struct dma_debug_entry *entry)
627{
628 unsigned long flags;
629
Dan Williams0abdd7a2014-01-21 15:48:12 -0800630 active_pfn_remove(entry);
631
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100632 /*
633 * add to beginning of the list - this way the entries are
634 * more likely cache hot when they are reallocated.
635 */
636 spin_lock_irqsave(&free_entries_lock, flags);
637 list_add(&entry->list, &free_entries);
638 num_free_entries += 1;
639 spin_unlock_irqrestore(&free_entries_lock, flags);
640}
641
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900642int dma_debug_resize_entries(u32 num_entries)
643{
644 int i, delta, ret = 0;
645 unsigned long flags;
646 struct dma_debug_entry *entry;
647 LIST_HEAD(tmp);
648
649 spin_lock_irqsave(&free_entries_lock, flags);
650
651 if (nr_total_entries < num_entries) {
652 delta = num_entries - nr_total_entries;
653
654 spin_unlock_irqrestore(&free_entries_lock, flags);
655
656 for (i = 0; i < delta; i++) {
657 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
658 if (!entry)
659 break;
660
661 list_add_tail(&entry->list, &tmp);
662 }
663
664 spin_lock_irqsave(&free_entries_lock, flags);
665
666 list_splice(&tmp, &free_entries);
667 nr_total_entries += i;
668 num_free_entries += i;
669 } else {
670 delta = nr_total_entries - num_entries;
671
672 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
673 entry = __dma_entry_alloc();
674 kfree(entry);
675 }
676
677 nr_total_entries -= i;
678 }
679
680 if (nr_total_entries != num_entries)
681 ret = 1;
682
683 spin_unlock_irqrestore(&free_entries_lock, flags);
684
685 return ret;
686}
687EXPORT_SYMBOL(dma_debug_resize_entries);
688
Joerg Roedel6bf07872009-01-09 12:54:42 +0100689/*
690 * DMA-API debugging init code
691 *
692 * The init code does two things:
693 * 1. Initialize core data structures
694 * 2. Preallocate a given number of dma_debug_entry structs
695 */
696
697static int prealloc_memory(u32 num_entries)
698{
699 struct dma_debug_entry *entry, *next_entry;
700 int i;
701
702 for (i = 0; i < num_entries; ++i) {
703 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
704 if (!entry)
705 goto out_err;
706
707 list_add_tail(&entry->list, &free_entries);
708 }
709
710 num_free_entries = num_entries;
711 min_free_entries = num_entries;
712
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200713 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100714
715 return 0;
716
717out_err:
718
719 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
720 list_del(&entry->list);
721 kfree(entry);
722 }
723
724 return -ENOMEM;
725}
726
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200727static ssize_t filter_read(struct file *file, char __user *user_buf,
728 size_t count, loff_t *ppos)
729{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200730 char buf[NAME_MAX_LEN + 1];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200731 unsigned long flags;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200732 int len;
733
734 if (!current_driver_name[0])
735 return 0;
736
737 /*
738 * We can't copy to userspace directly because current_driver_name can
739 * only be read under the driver_name_lock with irqs disabled. So
740 * create a temporary copy first.
741 */
742 read_lock_irqsave(&driver_name_lock, flags);
743 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
744 read_unlock_irqrestore(&driver_name_lock, flags);
745
746 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
747}
748
749static ssize_t filter_write(struct file *file, const char __user *userbuf,
750 size_t count, loff_t *ppos)
751{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200752 char buf[NAME_MAX_LEN];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200753 unsigned long flags;
754 size_t len;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200755 int i;
756
757 /*
758 * We can't copy from userspace directly. Access to
759 * current_driver_name is protected with a write_lock with irqs
760 * disabled. Since copy_from_user can fault and may sleep we
761 * need to copy to temporary buffer first
762 */
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200763 len = min(count, (size_t)(NAME_MAX_LEN - 1));
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200764 if (copy_from_user(buf, userbuf, len))
765 return -EFAULT;
766
767 buf[len] = 0;
768
769 write_lock_irqsave(&driver_name_lock, flags);
770
Joerg Roedel312325092009-06-08 15:07:08 +0200771 /*
772 * Now handle the string we got from userspace very carefully.
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200773 * The rules are:
774 * - only use the first token we got
775 * - token delimiter is everything looking like a space
776 * character (' ', '\n', '\t' ...)
777 *
778 */
779 if (!isalnum(buf[0])) {
780 /*
Joerg Roedel312325092009-06-08 15:07:08 +0200781 * If the first character userspace gave us is not
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200782 * alphanumerical then assume the filter should be
783 * switched off.
784 */
785 if (current_driver_name[0])
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200786 pr_info("DMA-API: switching off dma-debug driver filter\n");
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200787 current_driver_name[0] = 0;
788 current_driver = NULL;
789 goto out_unlock;
790 }
791
792 /*
793 * Now parse out the first token and use it as the name for the
794 * driver to filter for.
795 */
Dan Carpenter39a37ce2010-04-06 19:45:12 +0300796 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200797 current_driver_name[i] = buf[i];
798 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
799 break;
800 }
801 current_driver_name[i] = 0;
802 current_driver = NULL;
803
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200804 pr_info("DMA-API: enable driver filter for driver [%s]\n",
805 current_driver_name);
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200806
807out_unlock:
808 write_unlock_irqrestore(&driver_name_lock, flags);
809
810 return count;
811}
812
Thiago Farinaaeb583d2010-01-18 18:57:33 -0500813static const struct file_operations filter_fops = {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200814 .read = filter_read,
815 .write = filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200816 .llseek = default_llseek,
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200817};
818
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100819static int dma_debug_fs_init(void)
820{
821 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
822 if (!dma_debug_dent) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200823 pr_err("DMA-API: can not create debugfs directory\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100824 return -ENOMEM;
825 }
826
827 global_disable_dent = debugfs_create_bool("disabled", 0444,
828 dma_debug_dent,
Dan Carpenter68ee6d22012-06-27 12:08:55 +0300829 &global_disable);
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100830 if (!global_disable_dent)
831 goto out_err;
832
833 error_count_dent = debugfs_create_u32("error_count", 0444,
834 dma_debug_dent, &error_count);
835 if (!error_count_dent)
836 goto out_err;
837
838 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
839 dma_debug_dent,
840 &show_all_errors);
841 if (!show_all_errors_dent)
842 goto out_err;
843
844 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
845 dma_debug_dent,
846 &show_num_errors);
847 if (!show_num_errors_dent)
848 goto out_err;
849
850 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
851 dma_debug_dent,
852 &num_free_entries);
853 if (!num_free_entries_dent)
854 goto out_err;
855
856 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
857 dma_debug_dent,
858 &min_free_entries);
859 if (!min_free_entries_dent)
860 goto out_err;
861
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200862 filter_dent = debugfs_create_file("driver_filter", 0644,
863 dma_debug_dent, NULL, &filter_fops);
864 if (!filter_dent)
865 goto out_err;
866
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100867 return 0;
868
869out_err:
870 debugfs_remove_recursive(dma_debug_dent);
871
872 return -ENOMEM;
873}
874
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400875static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200876{
877 struct dma_debug_entry *entry;
878 unsigned long flags;
879 int count = 0, i;
880
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200881 local_irq_save(flags);
882
Joerg Roedeled888ae2009-05-22 17:16:04 +0200883 for (i = 0; i < HASH_SIZE; ++i) {
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200884 spin_lock(&dma_entry_hash[i].lock);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200885 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400886 if (entry->dev == dev) {
Joerg Roedeled888ae2009-05-22 17:16:04 +0200887 count += 1;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400888 *out_entry = entry;
889 }
Joerg Roedeled888ae2009-05-22 17:16:04 +0200890 }
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200891 spin_unlock(&dma_entry_hash[i].lock);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200892 }
893
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200894 local_irq_restore(flags);
895
Joerg Roedeled888ae2009-05-22 17:16:04 +0200896 return count;
897}
898
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100899static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200900{
901 struct device *dev = data;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400902 struct dma_debug_entry *uninitialized_var(entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200903 int count;
904
Shaun Ruffellf797d982009-12-17 18:00:36 -0600905 if (global_disable)
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100906 return 0;
Joerg Roedeled888ae2009-05-22 17:16:04 +0200907
908 switch (action) {
909 case BUS_NOTIFY_UNBOUND_DRIVER:
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400910 count = device_dma_allocations(dev, &entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200911 if (count == 0)
912 break;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400913 err_printk(dev, entry, "DMA-API: device driver has pending "
Joerg Roedeled888ae2009-05-22 17:16:04 +0200914 "DMA allocations while released from device "
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400915 "[count=%d]\n"
916 "One of leaked entries details: "
917 "[device address=0x%016llx] [size=%llu bytes] "
918 "[mapped with %s] [mapped as %s]\n",
919 count, entry->dev_addr, entry->size,
920 dir2name[entry->direction], type2name[entry->type]);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200921 break;
922 default:
923 break;
924 }
925
926 return 0;
927}
928
Joerg Roedel41531c82009-03-16 17:32:14 +0100929void dma_debug_add_bus(struct bus_type *bus)
930{
Joerg Roedeled888ae2009-05-22 17:16:04 +0200931 struct notifier_block *nb;
932
Shaun Ruffellf797d982009-12-17 18:00:36 -0600933 if (global_disable)
934 return;
935
Joerg Roedeled888ae2009-05-22 17:16:04 +0200936 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
937 if (nb == NULL) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200938 pr_err("dma_debug_add_bus: out of memory\n");
Joerg Roedeled888ae2009-05-22 17:16:04 +0200939 return;
940 }
941
942 nb->notifier_call = dma_debug_device_change;
943
944 bus_register_notifier(bus, nb);
Joerg Roedel41531c82009-03-16 17:32:14 +0100945}
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100946
Joerg Roedel6bf07872009-01-09 12:54:42 +0100947/*
948 * Let the architectures decide how many entries should be preallocated.
949 */
950void dma_debug_init(u32 num_entries)
951{
952 int i;
953
954 if (global_disable)
955 return;
956
957 for (i = 0; i < HASH_SIZE; ++i) {
958 INIT_LIST_HEAD(&dma_entry_hash[i].list);
Ingo Molnarb0a5b832009-06-16 16:11:14 +0200959 spin_lock_init(&dma_entry_hash[i].lock);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100960 }
961
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100962 if (dma_debug_fs_init() != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200963 pr_err("DMA-API: error creating debugfs entries - disabling\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100964 global_disable = true;
965
966 return;
967 }
968
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100969 if (req_entries)
970 num_entries = req_entries;
971
Joerg Roedel6bf07872009-01-09 12:54:42 +0100972 if (prealloc_memory(num_entries) != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200973 pr_err("DMA-API: debugging out of memory error - disabled\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +0100974 global_disable = true;
975
976 return;
977 }
978
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900979 nr_total_entries = num_free_entries;
980
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200981 pr_info("DMA-API: debugging enabled by kernel config\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +0100982}
983
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100984static __init int dma_debug_cmdline(char *str)
985{
986 if (!str)
987 return -EINVAL;
988
989 if (strncmp(str, "off", 3) == 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200990 pr_info("DMA-API: debugging disabled on kernel command line\n");
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100991 global_disable = true;
992 }
993
994 return 0;
995}
996
997static __init int dma_debug_entries_cmdline(char *str)
998{
999 int res;
1000
1001 if (!str)
1002 return -EINVAL;
1003
1004 res = get_option(&str, &req_entries);
1005
1006 if (!res)
1007 req_entries = 0;
1008
1009 return 0;
1010}
1011
1012__setup("dma_debug=", dma_debug_cmdline);
1013__setup("dma_debug_entries=", dma_debug_entries_cmdline);
1014
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001015static void check_unmap(struct dma_debug_entry *ref)
1016{
1017 struct dma_debug_entry *entry;
1018 struct hash_bucket *bucket;
1019 unsigned long flags;
1020
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001021 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001022 entry = bucket_find_exact(bucket, ref);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001023
1024 if (!entry) {
Alexander Duyck8d640a52013-03-22 15:04:48 -07001025 /* must drop lock before calling dma_mapping_error */
1026 put_hash_bucket(bucket, &flags);
1027
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001028 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1029 err_printk(ref->dev, NULL,
Alexander Duyck8d640a52013-03-22 15:04:48 -07001030 "DMA-API: device driver tries to free an "
1031 "invalid DMA memory address\n");
1032 } else {
1033 err_printk(ref->dev, NULL,
1034 "DMA-API: device driver tries to free DMA "
1035 "memory it has not allocated [device "
1036 "address=0x%016llx] [size=%llu bytes]\n",
1037 ref->dev_addr, ref->size);
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001038 }
Alexander Duyck8d640a52013-03-22 15:04:48 -07001039 return;
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001040 }
1041
1042 if (ref->size != entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001043 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001044 "DMA memory with different size "
1045 "[device address=0x%016llx] [map size=%llu bytes] "
1046 "[unmap size=%llu bytes]\n",
1047 ref->dev_addr, entry->size, ref->size);
1048 }
1049
1050 if (ref->type != entry->type) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001051 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001052 "DMA memory with wrong function "
1053 "[device address=0x%016llx] [size=%llu bytes] "
1054 "[mapped as %s] [unmapped as %s]\n",
1055 ref->dev_addr, ref->size,
1056 type2name[entry->type], type2name[ref->type]);
1057 } else if ((entry->type == dma_debug_coherent) &&
Dan Williams0abdd7a2014-01-21 15:48:12 -08001058 (phys_addr(ref) != phys_addr(entry))) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001059 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001060 "DMA memory with different CPU address "
1061 "[device address=0x%016llx] [size=%llu bytes] "
Joerg Roedel59a40e702009-10-29 16:25:50 +01001062 "[cpu alloc address=0x%016llx] "
1063 "[cpu free address=0x%016llx]",
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001064 ref->dev_addr, ref->size,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001065 phys_addr(entry),
1066 phys_addr(ref));
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001067 }
1068
1069 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1070 ref->sg_call_ents != entry->sg_call_ents) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001071 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001072 "DMA sg list with different entry count "
1073 "[map count=%d] [unmap count=%d]\n",
1074 entry->sg_call_ents, ref->sg_call_ents);
1075 }
1076
1077 /*
1078 * This may be no bug in reality - but most implementations of the
1079 * DMA API don't handle this properly, so check for it here
1080 */
1081 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001082 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001083 "DMA memory with different direction "
1084 "[device address=0x%016llx] [size=%llu bytes] "
1085 "[mapped with %s] [unmapped with %s]\n",
1086 ref->dev_addr, ref->size,
1087 dir2name[entry->direction],
1088 dir2name[ref->direction]);
1089 }
1090
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001091 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1092 err_printk(ref->dev, entry,
1093 "DMA-API: device driver failed to check map error"
1094 "[device address=0x%016llx] [size=%llu bytes] "
1095 "[mapped as %s]",
1096 ref->dev_addr, ref->size,
1097 type2name[entry->type]);
1098 }
1099
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001100 hash_bucket_del(entry);
1101 dma_entry_free(entry);
1102
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001103 put_hash_bucket(bucket, &flags);
1104}
1105
1106static void check_for_stack(struct device *dev, void *addr)
1107{
1108 if (object_is_on_stack(addr))
David Woodhouse6c132d12009-01-19 16:52:39 +01001109 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
1110 "stack [addr=%p]\n", addr);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001111}
1112
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001113static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001114{
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001115 unsigned long a1 = (unsigned long)addr;
1116 unsigned long b1 = a1 + len;
1117 unsigned long a2 = (unsigned long)start;
1118 unsigned long b2 = (unsigned long)end;
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001119
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001120 return !(b1 <= a2 || a1 >= b2);
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001121}
1122
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001123static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001124{
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001125 if (overlap(addr, len, _text, _etext) ||
1126 overlap(addr, len, __start_rodata, __end_rodata))
1127 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001128}
1129
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001130static void check_sync(struct device *dev,
1131 struct dma_debug_entry *ref,
1132 bool to_cpu)
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001133{
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001134 struct dma_debug_entry *entry;
1135 struct hash_bucket *bucket;
1136 unsigned long flags;
1137
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001138 bucket = get_hash_bucket(ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001139
Neil Hormanc6a21d02011-08-08 15:13:54 -04001140 entry = bucket_find_contain(&bucket, ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001141
1142 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001143 err_printk(dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001144 "to sync DMA memory it has not allocated "
1145 "[device address=0x%016llx] [size=%llu bytes]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001146 (unsigned long long)ref->dev_addr, ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001147 goto out;
1148 }
1149
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001150 if (ref->size > entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001151 err_printk(dev, entry, "DMA-API: device driver syncs"
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001152 " DMA memory outside allocated range "
1153 "[device address=0x%016llx] "
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001154 "[allocation size=%llu bytes] "
1155 "[sync offset+size=%llu]\n",
1156 entry->dev_addr, entry->size,
1157 ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001158 }
1159
Krzysztof Halasa42d53b42010-01-08 14:42:36 -08001160 if (entry->direction == DMA_BIDIRECTIONAL)
1161 goto out;
1162
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001163 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001164 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001165 "DMA memory with different direction "
1166 "[device address=0x%016llx] [size=%llu bytes] "
1167 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001168 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001169 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001170 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001171 }
1172
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001173 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001174 !(ref->direction == DMA_TO_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +01001175 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001176 "device read-only DMA memory for cpu "
1177 "[device address=0x%016llx] [size=%llu bytes] "
1178 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001179 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001180 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001181 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001182
1183 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001184 !(ref->direction == DMA_FROM_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +01001185 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001186 "device write-only DMA memory to device "
1187 "[device address=0x%016llx] [size=%llu bytes] "
1188 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001189 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001190 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001191 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001192
1193out:
1194 put_hash_bucket(bucket, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001195}
1196
Joerg Roedelf62bc982009-01-09 14:14:49 +01001197void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1198 size_t size, int direction, dma_addr_t dma_addr,
1199 bool map_single)
1200{
1201 struct dma_debug_entry *entry;
1202
1203 if (unlikely(global_disable))
1204 return;
1205
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001206 if (dma_mapping_error(dev, dma_addr))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001207 return;
1208
1209 entry = dma_entry_alloc();
1210 if (!entry)
1211 return;
1212
1213 entry->dev = dev;
1214 entry->type = dma_debug_page;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001215 entry->pfn = page_to_pfn(page);
1216 entry->offset = offset,
Joerg Roedelf62bc982009-01-09 14:14:49 +01001217 entry->dev_addr = dma_addr;
1218 entry->size = size;
1219 entry->direction = direction;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001220 entry->map_err_type = MAP_ERR_NOT_CHECKED;
Joerg Roedelf62bc982009-01-09 14:14:49 +01001221
Joerg Roedel9537a482009-03-23 15:35:08 +01001222 if (map_single)
Joerg Roedelf62bc982009-01-09 14:14:49 +01001223 entry->type = dma_debug_single;
Joerg Roedel9537a482009-03-23 15:35:08 +01001224
1225 if (!PageHighMem(page)) {
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001226 void *addr = page_address(page) + offset;
1227
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001228 check_for_stack(dev, addr);
1229 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +01001230 }
1231
1232 add_dma_entry(entry);
1233}
1234EXPORT_SYMBOL(debug_dma_map_page);
1235
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001236void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1237{
1238 struct dma_debug_entry ref;
1239 struct dma_debug_entry *entry;
1240 struct hash_bucket *bucket;
1241 unsigned long flags;
1242
1243 if (unlikely(global_disable))
1244 return;
1245
1246 ref.dev = dev;
1247 ref.dev_addr = dma_addr;
1248 bucket = get_hash_bucket(&ref, &flags);
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001249
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001250 list_for_each_entry(entry, &bucket->list, list) {
1251 if (!exact_match(&ref, entry))
1252 continue;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001253
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001254 /*
1255 * The same physical address can be mapped multiple
1256 * times. Without a hardware IOMMU this results in the
1257 * same device addresses being put into the dma-debug
1258 * hash multiple times too. This can result in false
1259 * positives being reported. Therefore we implement a
1260 * best-fit algorithm here which updates the first entry
1261 * from the hash which fits the reference value and is
1262 * not currently listed as being checked.
1263 */
1264 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1265 entry->map_err_type = MAP_ERR_CHECKED;
1266 break;
1267 }
1268 }
1269
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001270 put_hash_bucket(bucket, &flags);
1271}
1272EXPORT_SYMBOL(debug_dma_mapping_error);
1273
Joerg Roedelf62bc982009-01-09 14:14:49 +01001274void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1275 size_t size, int direction, bool map_single)
1276{
1277 struct dma_debug_entry ref = {
1278 .type = dma_debug_page,
1279 .dev = dev,
1280 .dev_addr = addr,
1281 .size = size,
1282 .direction = direction,
1283 };
1284
1285 if (unlikely(global_disable))
1286 return;
1287
1288 if (map_single)
1289 ref.type = dma_debug_single;
1290
1291 check_unmap(&ref);
1292}
1293EXPORT_SYMBOL(debug_dma_unmap_page);
1294
Joerg Roedel972aa452009-01-09 14:19:54 +01001295void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1296 int nents, int mapped_ents, int direction)
1297{
1298 struct dma_debug_entry *entry;
1299 struct scatterlist *s;
1300 int i;
1301
1302 if (unlikely(global_disable))
1303 return;
1304
1305 for_each_sg(sg, s, mapped_ents, i) {
1306 entry = dma_entry_alloc();
1307 if (!entry)
1308 return;
1309
1310 entry->type = dma_debug_sg;
1311 entry->dev = dev;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001312 entry->pfn = page_to_pfn(sg_page(s));
1313 entry->offset = s->offset,
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001314 entry->size = sg_dma_len(s);
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001315 entry->dev_addr = sg_dma_address(s);
Joerg Roedel972aa452009-01-09 14:19:54 +01001316 entry->direction = direction;
1317 entry->sg_call_ents = nents;
1318 entry->sg_mapped_ents = mapped_ents;
1319
Joerg Roedel9537a482009-03-23 15:35:08 +01001320 if (!PageHighMem(sg_page(s))) {
1321 check_for_stack(dev, sg_virt(s));
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001322 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
Joerg Roedel9537a482009-03-23 15:35:08 +01001323 }
Joerg Roedel972aa452009-01-09 14:19:54 +01001324
1325 add_dma_entry(entry);
1326 }
1327}
1328EXPORT_SYMBOL(debug_dma_map_sg);
1329
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001330static int get_nr_mapped_entries(struct device *dev,
1331 struct dma_debug_entry *ref)
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001332{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001333 struct dma_debug_entry *entry;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001334 struct hash_bucket *bucket;
1335 unsigned long flags;
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001336 int mapped_ents;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001337
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001338 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001339 entry = bucket_find_exact(bucket, ref);
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001340 mapped_ents = 0;
1341
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001342 if (entry)
1343 mapped_ents = entry->sg_mapped_ents;
1344 put_hash_bucket(bucket, &flags);
1345
1346 return mapped_ents;
1347}
1348
Joerg Roedel972aa452009-01-09 14:19:54 +01001349void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1350 int nelems, int dir)
1351{
Joerg Roedel972aa452009-01-09 14:19:54 +01001352 struct scatterlist *s;
1353 int mapped_ents = 0, i;
Joerg Roedel972aa452009-01-09 14:19:54 +01001354
1355 if (unlikely(global_disable))
1356 return;
1357
1358 for_each_sg(sglist, s, nelems, i) {
1359
1360 struct dma_debug_entry ref = {
1361 .type = dma_debug_sg,
1362 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001363 .pfn = page_to_pfn(sg_page(s)),
1364 .offset = s->offset,
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001365 .dev_addr = sg_dma_address(s),
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001366 .size = sg_dma_len(s),
Joerg Roedel972aa452009-01-09 14:19:54 +01001367 .direction = dir,
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001368 .sg_call_ents = nelems,
Joerg Roedel972aa452009-01-09 14:19:54 +01001369 };
1370
1371 if (mapped_ents && i >= mapped_ents)
1372 break;
1373
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001374 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001375 mapped_ents = get_nr_mapped_entries(dev, &ref);
Joerg Roedel972aa452009-01-09 14:19:54 +01001376
1377 check_unmap(&ref);
1378 }
1379}
1380EXPORT_SYMBOL(debug_dma_unmap_sg);
1381
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001382void debug_dma_alloc_coherent(struct device *dev, size_t size,
1383 dma_addr_t dma_addr, void *virt)
1384{
1385 struct dma_debug_entry *entry;
1386
1387 if (unlikely(global_disable))
1388 return;
1389
1390 if (unlikely(virt == NULL))
1391 return;
1392
1393 entry = dma_entry_alloc();
1394 if (!entry)
1395 return;
1396
1397 entry->type = dma_debug_coherent;
1398 entry->dev = dev;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001399 entry->pfn = page_to_pfn(virt_to_page(virt));
1400 entry->offset = (size_t) virt & PAGE_MASK;
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001401 entry->size = size;
1402 entry->dev_addr = dma_addr;
1403 entry->direction = DMA_BIDIRECTIONAL;
1404
1405 add_dma_entry(entry);
1406}
1407EXPORT_SYMBOL(debug_dma_alloc_coherent);
1408
1409void debug_dma_free_coherent(struct device *dev, size_t size,
1410 void *virt, dma_addr_t addr)
1411{
1412 struct dma_debug_entry ref = {
1413 .type = dma_debug_coherent,
1414 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001415 .pfn = page_to_pfn(virt_to_page(virt)),
1416 .offset = (size_t) virt & PAGE_MASK,
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001417 .dev_addr = addr,
1418 .size = size,
1419 .direction = DMA_BIDIRECTIONAL,
1420 };
1421
1422 if (unlikely(global_disable))
1423 return;
1424
1425 check_unmap(&ref);
1426}
1427EXPORT_SYMBOL(debug_dma_free_coherent);
1428
Joerg Roedelb9d23172009-01-09 14:43:04 +01001429void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1430 size_t size, int direction)
1431{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001432 struct dma_debug_entry ref;
1433
Joerg Roedelb9d23172009-01-09 14:43:04 +01001434 if (unlikely(global_disable))
1435 return;
1436
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001437 ref.type = dma_debug_single;
1438 ref.dev = dev;
1439 ref.dev_addr = dma_handle;
1440 ref.size = size;
1441 ref.direction = direction;
1442 ref.sg_call_ents = 0;
1443
1444 check_sync(dev, &ref, true);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001445}
1446EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1447
1448void debug_dma_sync_single_for_device(struct device *dev,
1449 dma_addr_t dma_handle, size_t size,
1450 int direction)
1451{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001452 struct dma_debug_entry ref;
1453
Joerg Roedelb9d23172009-01-09 14:43:04 +01001454 if (unlikely(global_disable))
1455 return;
1456
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001457 ref.type = dma_debug_single;
1458 ref.dev = dev;
1459 ref.dev_addr = dma_handle;
1460 ref.size = size;
1461 ref.direction = direction;
1462 ref.sg_call_ents = 0;
1463
1464 check_sync(dev, &ref, false);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001465}
1466EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1467
Joerg Roedel948408b2009-01-09 14:55:38 +01001468void debug_dma_sync_single_range_for_cpu(struct device *dev,
1469 dma_addr_t dma_handle,
1470 unsigned long offset, size_t size,
1471 int direction)
1472{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001473 struct dma_debug_entry ref;
1474
Joerg Roedel948408b2009-01-09 14:55:38 +01001475 if (unlikely(global_disable))
1476 return;
1477
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001478 ref.type = dma_debug_single;
1479 ref.dev = dev;
1480 ref.dev_addr = dma_handle;
1481 ref.size = offset + size;
1482 ref.direction = direction;
1483 ref.sg_call_ents = 0;
1484
1485 check_sync(dev, &ref, true);
Joerg Roedel948408b2009-01-09 14:55:38 +01001486}
1487EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1488
1489void debug_dma_sync_single_range_for_device(struct device *dev,
1490 dma_addr_t dma_handle,
1491 unsigned long offset,
1492 size_t size, int direction)
1493{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001494 struct dma_debug_entry ref;
1495
Joerg Roedel948408b2009-01-09 14:55:38 +01001496 if (unlikely(global_disable))
1497 return;
1498
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001499 ref.type = dma_debug_single;
1500 ref.dev = dev;
1501 ref.dev_addr = dma_handle;
1502 ref.size = offset + size;
1503 ref.direction = direction;
1504 ref.sg_call_ents = 0;
1505
1506 check_sync(dev, &ref, false);
Joerg Roedel948408b2009-01-09 14:55:38 +01001507}
1508EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1509
Joerg Roedela31fba52009-01-09 15:01:12 +01001510void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1511 int nelems, int direction)
1512{
1513 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001514 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001515
1516 if (unlikely(global_disable))
1517 return;
1518
1519 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001520
1521 struct dma_debug_entry ref = {
1522 .type = dma_debug_sg,
1523 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001524 .pfn = page_to_pfn(sg_page(s)),
1525 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001526 .dev_addr = sg_dma_address(s),
1527 .size = sg_dma_len(s),
1528 .direction = direction,
1529 .sg_call_ents = nelems,
1530 };
1531
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001532 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001533 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001534
1535 if (i >= mapped_ents)
1536 break;
1537
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001538 check_sync(dev, &ref, true);
Joerg Roedela31fba52009-01-09 15:01:12 +01001539 }
1540}
1541EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1542
1543void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1544 int nelems, int direction)
1545{
1546 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001547 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001548
1549 if (unlikely(global_disable))
1550 return;
1551
1552 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001553
1554 struct dma_debug_entry ref = {
1555 .type = dma_debug_sg,
1556 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001557 .pfn = page_to_pfn(sg_page(s)),
1558 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001559 .dev_addr = sg_dma_address(s),
1560 .size = sg_dma_len(s),
1561 .direction = direction,
1562 .sg_call_ents = nelems,
1563 };
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001564 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001565 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001566
1567 if (i >= mapped_ents)
1568 break;
1569
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001570 check_sync(dev, &ref, false);
Joerg Roedela31fba52009-01-09 15:01:12 +01001571 }
1572}
1573EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1574
Joerg Roedel1745de52009-05-22 21:49:51 +02001575static int __init dma_debug_driver_setup(char *str)
1576{
1577 int i;
1578
1579 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1580 current_driver_name[i] = *str;
1581 if (*str == 0)
1582 break;
1583 }
1584
1585 if (current_driver_name[0])
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001586 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1587 current_driver_name);
Joerg Roedel1745de52009-05-22 21:49:51 +02001588
1589
1590 return 1;
1591}
1592__setup("dma_debug_driver=", dma_debug_driver_setup);