blob: 1ac35dbaf8e043f6d23421bb5117ce55b6da9182 [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
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800105static inline bool dma_debug_disabled(void)
106{
107 return global_disable;
108}
109
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100110/* 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
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100118static u32 num_free_entries;
119static u32 min_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900120static u32 nr_total_entries;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100121
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100122/* number of preallocated entries requested by kernel cmdline */
123static u32 req_entries;
124
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100125/* 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;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200133static struct dentry *filter_dent __read_mostly;
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100134
Joerg Roedel2e507d82009-05-22 18:24:20 +0200135/* 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);
Joerg Roedel30dfa902009-01-09 12:34:49 +0100143
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600144static 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
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100150static 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 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100169static inline void dump_entry_trace(struct dma_debug_entry *entry)
170{
171#ifdef CONFIG_STACKTRACE
172 if (entry) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200173 pr_warning("Mapped at:\n");
David Woodhouse6c132d12009-01-19 16:52:39 +0100174 print_stack_trace(&entry->stacktrace, 0);
175 }
176#endif
177}
178
Joerg Roedel2e507d82009-05-22 18:24:20 +0200179static bool driver_filter(struct device *dev)
180{
Joerg Roedel0bf84122009-06-08 15:53:46 +0200181 struct device_driver *drv;
182 unsigned long flags;
183 bool ret;
184
Joerg Roedel2e507d82009-05-22 18:24:20 +0200185 /* driver filter off */
186 if (likely(!current_driver_name[0]))
187 return true;
188
189 /* driver filter on and initialized */
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400190 if (current_driver && dev && dev->driver == current_driver)
Joerg Roedel2e507d82009-05-22 18:24:20 +0200191 return true;
192
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400193 /* driver filter on, but we can't filter on a NULL device... */
194 if (!dev)
195 return false;
196
Joerg Roedel0bf84122009-06-08 15:53:46 +0200197 if (current_driver || !current_driver_name[0])
198 return false;
199
Joerg Roedel2e507d82009-05-22 18:24:20 +0200200 /* driver filter on but not yet initialized */
Alan Sternf3ff9242012-01-24 13:35:24 -0500201 drv = dev->driver;
Joerg Roedel0bf84122009-06-08 15:53:46 +0200202 if (!drv)
203 return false;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200204
Joerg Roedel0bf84122009-06-08 15:53:46 +0200205 /* lock to protect against change of current_driver_name */
206 read_lock_irqsave(&driver_name_lock, flags);
Joerg Roedel2e507d82009-05-22 18:24:20 +0200207
Joerg Roedel0bf84122009-06-08 15:53:46 +0200208 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;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200213 }
214
Joerg Roedel0bf84122009-06-08 15:53:46 +0200215 read_unlock_irqrestore(&driver_name_lock, flags);
Joerg Roedel0bf84122009-06-08 15:53:46 +0200216
217 return ret;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200218}
219
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400220#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; \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100231 } while (0);
232
Joerg Roedel30dfa902009-01-09 12:34:49 +0100233/*
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
Neil Hormanc6a21d02011-08-08 15:13:54 -0400273static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
274{
Thomas Jarosch91ec37c2011-11-17 20:31:02 +0100275 return ((a->dev_addr == b->dev_addr) &&
Neil Hormanc6a21d02011-08-08 15:13:54 -0400276 (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
Joerg Roedel30dfa902009-01-09 12:34:49 +0100292/*
293 * Search a given entry in the hash bucket list
294 */
Neil Hormanc6a21d02011-08-08 15:13:54 -0400295static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
296 struct dma_debug_entry *ref,
297 match_fn match)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100298{
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200299 struct dma_debug_entry *entry, *ret = NULL;
Ming Leife73fbe2012-10-19 13:57:01 -0700300 int matches = 0, match_lvl, last_lvl = -1;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100301
302 list_for_each_entry(entry, &bucket->list, list) {
Neil Hormanc6a21d02011-08-08 15:13:54 -0400303 if (!match(ref, entry))
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200304 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
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200311 * positives being reported. Therefore we implement a
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200312 * 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;
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200318 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;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200322
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200323 if (match_lvl == 4) {
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200324 /* perfect-fit - return the result */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100325 return entry;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200326 } else if (match_lvl > last_lvl) {
327 /*
328 * We found an entry that fits better then the
Ming Leife73fbe2012-10-19 13:57:01 -0700329 * previous one or it is the 1st match.
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200330 */
331 last_lvl = match_lvl;
332 ret = entry;
333 }
Joerg Roedel30dfa902009-01-09 12:34:49 +0100334 }
335
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200336 /*
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;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100343}
344
Neil Hormanc6a21d02011-08-08 15:13:54 -0400345static 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
Joerg Roedel30dfa902009-01-09 12:34:49 +0100378/*
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
Dan Williams0abdd7a2014-01-21 15:48:12 -0800395static 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
Joerg Roedel30dfa902009-01-09 12:34:49 +0100400/*
David Woodhouseac26c182009-02-12 16:19:13 +0100401 * 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,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800417 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
David Woodhouseac26c182009-02-12 16:19:13 +0100418 type2name[entry->type], idx,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800419 phys_addr(entry), entry->pfn,
David Woodhouseac26c182009-02-12 16:19:13 +0100420 entry->dev_addr, entry->size,
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600421 dir2name[entry->direction],
422 maperr2str[entry->map_err_type]);
David Woodhouseac26c182009-02-12 16:19:13 +0100423 }
424 }
425
426 spin_unlock_irqrestore(&bucket->lock, flags);
427 }
428}
429EXPORT_SYMBOL(debug_dma_dump_mappings);
430
431/*
Dan Williams3b7a6412014-03-03 15:38:21 -0800432 * 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
Dan Williams0abdd7a2014-01-21 15:48:12 -0800436 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
Dan Williams3b7a6412014-03-03 15:38:21 -0800437 * the entry already exists at insertion time add a tag as a reference
Dan Williams0abdd7a2014-01-21 15:48:12 -0800438 * count for the overlapping mappings. For now, the overlap tracking
Dan Williams3b7a6412014-03-03 15:38:21 -0800439 * just ensures that 'unmaps' balance 'maps' before marking the
440 * cacheline idle, but we should also be flagging overlaps as an API
441 * violation.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800442 *
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
Dan Williams3b7a6412014-03-03 15:38:21 -0800445 * 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.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800450 *
451 * At any time debug_dma_assert_idle() can be called to trigger a
Dan Williams3b7a6412014-03-03 15:38:21 -0800452 * warning if any cachelines in the given page are in the active set.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800453 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800454static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800455static DEFINE_SPINLOCK(radix_lock);
Dan Williams3b7a6412014-03-03 15:38:21 -0800456#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)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800459
Dan Williams3b7a6412014-03-03 15:38:21 -0800460static 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)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800467{
468 int overlap = 0, i;
469
470 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
Dan Williams3b7a6412014-03-03 15:38:21 -0800471 if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
Dan Williams0abdd7a2014-01-21 15:48:12 -0800472 overlap |= 1 << i;
473 return overlap;
474}
475
Dan Williams3b7a6412014-03-03 15:38:21 -0800476static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800477{
478 int i;
479
Dan Williams3b7a6412014-03-03 15:38:21 -0800480 if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
Dan Williams59f2e7d2014-01-29 14:05:53 -0800481 return overlap;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800482
483 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
484 if (overlap & 1 << i)
Dan Williams3b7a6412014-03-03 15:38:21 -0800485 radix_tree_tag_set(&dma_active_cacheline, cln, i);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800486 else
Dan Williams3b7a6412014-03-03 15:38:21 -0800487 radix_tree_tag_clear(&dma_active_cacheline, cln, i);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800488
489 return overlap;
490}
491
Dan Williams3b7a6412014-03-03 15:38:21 -0800492static void active_cacheline_inc_overlap(phys_addr_t cln)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800493{
Dan Williams3b7a6412014-03-03 15:38:21 -0800494 int overlap = active_cacheline_read_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800495
Dan Williams3b7a6412014-03-03 15:38:21 -0800496 overlap = active_cacheline_set_overlap(cln, ++overlap);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800497
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
Dan Williams3b7a6412014-03-03 15:38:21 -0800501 * debug_dma_assert_idle() as the cacheline may be marked idle
Dan Williams0abdd7a2014-01-21 15:48:12 -0800502 * prematurely.
503 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800504 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
505 "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
506 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800507}
508
Dan Williams3b7a6412014-03-03 15:38:21 -0800509static int active_cacheline_dec_overlap(phys_addr_t cln)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800510{
Dan Williams3b7a6412014-03-03 15:38:21 -0800511 int overlap = active_cacheline_read_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800512
Dan Williams3b7a6412014-03-03 15:38:21 -0800513 return active_cacheline_set_overlap(cln, --overlap);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800514}
515
Dan Williams3b7a6412014-03-03 15:38:21 -0800516static int active_cacheline_insert(struct dma_debug_entry *entry)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800517{
Dan Williams3b7a6412014-03-03 15:38:21 -0800518 phys_addr_t cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800519 unsigned long flags;
520 int rc;
521
Dan Williams3b7a6412014-03-03 15:38:21 -0800522 /* 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
Dan Williams0abdd7a2014-01-21 15:48:12 -0800529 spin_lock_irqsave(&radix_lock, flags);
Dan Williams3b7a6412014-03-03 15:38:21 -0800530 rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800531 if (rc == -EEXIST)
Dan Williams3b7a6412014-03-03 15:38:21 -0800532 active_cacheline_inc_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800533 spin_unlock_irqrestore(&radix_lock, flags);
534
535 return rc;
536}
537
Dan Williams3b7a6412014-03-03 15:38:21 -0800538static void active_cacheline_remove(struct dma_debug_entry *entry)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800539{
Dan Williams3b7a6412014-03-03 15:38:21 -0800540 phys_addr_t cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800541 unsigned long flags;
542
Dan Williams3b7a6412014-03-03 15:38:21 -0800543 /* ...mirror the insert case */
544 if (entry->direction == DMA_TO_DEVICE)
545 return;
546
Dan Williams0abdd7a2014-01-21 15:48:12 -0800547 spin_lock_irqsave(&radix_lock, flags);
Dan Williams59f2e7d2014-01-29 14:05:53 -0800548 /* since we are counting overlaps the final put of the
Dan Williams3b7a6412014-03-03 15:38:21 -0800549 * cacheline will occur when the overlap count is 0.
550 * active_cacheline_dec_overlap() returns -1 in that case
Dan Williams59f2e7d2014-01-29 14:05:53 -0800551 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800552 if (active_cacheline_dec_overlap(cln) < 0)
553 radix_tree_delete(&dma_active_cacheline, cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800554 spin_unlock_irqrestore(&radix_lock, flags);
555}
556
557/**
558 * debug_dma_assert_idle() - assert that a page is not undergoing dma
Dan Williams3b7a6412014-03-03 15:38:21 -0800559 * @page: page to lookup in the dma_active_cacheline tree
Dan Williams0abdd7a2014-01-21 15:48:12 -0800560 *
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{
Dan Williams3b7a6412014-03-03 15:38:21 -0800567 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;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800571 unsigned long flags;
Dan Williams3b7a6412014-03-03 15:38:21 -0800572 phys_addr_t cln;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800573
574 if (!page)
575 return;
576
Dan Williams3b7a6412014-03-03 15:38:21 -0800577 cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800578 spin_lock_irqsave(&radix_lock, flags);
Dan Williams3b7a6412014-03-03 15:38:21 -0800579 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 }
Dan Williams0abdd7a2014-01-21 15:48:12 -0800590 spin_unlock_irqrestore(&radix_lock, flags);
591
592 if (!entry)
593 return;
594
Dan Williams3b7a6412014-03-03 15:38:21 -0800595 cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800596 err_printk(entry->dev, entry,
Dan Williams3b7a6412014-03-03 15:38:21 -0800597 "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
598 &cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800599}
600
601/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100602 * 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;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800609 int rc;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100610
611 bucket = get_hash_bucket(entry, &flags);
612 hash_bucket_add(bucket, entry);
613 put_hash_bucket(bucket, &flags);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800614
Dan Williams3b7a6412014-03-03 15:38:21 -0800615 rc = active_cacheline_insert(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800616 if (rc == -ENOMEM) {
Dan Williams3b7a6412014-03-03 15:38:21 -0800617 pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
Dan Williams0abdd7a2014-01-21 15:48:12 -0800618 global_disable = true;
619 }
620
621 /* TODO: report -EEXIST errors here as overlapping mappings are
622 * not supported by the DMA API
623 */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100624}
625
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900626static 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
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100641/* 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{
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200648 struct dma_debug_entry *entry;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100649 unsigned long flags;
650
651 spin_lock_irqsave(&free_entries_lock, flags);
652
653 if (list_empty(&free_entries)) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200654 pr_err("DMA-API: debugging out of memory - disabling\n");
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100655 global_disable = true;
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200656 spin_unlock_irqrestore(&free_entries_lock, flags);
657 return NULL;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100658 }
659
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900660 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100661
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200662 spin_unlock_irqrestore(&free_entries_lock, flags);
663
David Woodhouse6c132d12009-01-19 16:52:39 +0100664#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
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100670
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100671 return entry;
672}
673
674static void dma_entry_free(struct dma_debug_entry *entry)
675{
676 unsigned long flags;
677
Dan Williams3b7a6412014-03-03 15:38:21 -0800678 active_cacheline_remove(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800679
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100680 /*
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
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900690int 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
Joerg Roedel6bf07872009-01-09 12:54:42 +0100737/*
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
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200761 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100762
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
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200775static ssize_t filter_read(struct file *file, char __user *user_buf,
776 size_t count, loff_t *ppos)
777{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200778 char buf[NAME_MAX_LEN + 1];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200779 unsigned long flags;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200780 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{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200800 char buf[NAME_MAX_LEN];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200801 unsigned long flags;
802 size_t len;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200803 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 */
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200811 len = min(count, (size_t)(NAME_MAX_LEN - 1));
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200812 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
Joerg Roedel312325092009-06-08 15:07:08 +0200819 /*
820 * Now handle the string we got from userspace very carefully.
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200821 * 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 /*
Joerg Roedel312325092009-06-08 15:07:08 +0200829 * If the first character userspace gave us is not
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200830 * alphanumerical then assume the filter should be
831 * switched off.
832 */
833 if (current_driver_name[0])
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200834 pr_info("DMA-API: switching off dma-debug driver filter\n");
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200835 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 */
Dan Carpenter39a37ce2010-04-06 19:45:12 +0300844 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200845 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
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200852 pr_info("DMA-API: enable driver filter for driver [%s]\n",
853 current_driver_name);
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200854
855out_unlock:
856 write_unlock_irqrestore(&driver_name_lock, flags);
857
858 return count;
859}
860
Thiago Farinaaeb583d2010-01-18 18:57:33 -0500861static const struct file_operations filter_fops = {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200862 .read = filter_read,
863 .write = filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200864 .llseek = default_llseek,
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200865};
866
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100867static int dma_debug_fs_init(void)
868{
869 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
870 if (!dma_debug_dent) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200871 pr_err("DMA-API: can not create debugfs directory\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100872 return -ENOMEM;
873 }
874
875 global_disable_dent = debugfs_create_bool("disabled", 0444,
876 dma_debug_dent,
Dan Carpenter68ee6d22012-06-27 12:08:55 +0300877 &global_disable);
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100878 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
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200910 filter_dent = debugfs_create_file("driver_filter", 0644,
911 dma_debug_dent, NULL, &filter_fops);
912 if (!filter_dent)
913 goto out_err;
914
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100915 return 0;
916
917out_err:
918 debugfs_remove_recursive(dma_debug_dent);
919
920 return -ENOMEM;
921}
922
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400923static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200924{
925 struct dma_debug_entry *entry;
926 unsigned long flags;
927 int count = 0, i;
928
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200929 local_irq_save(flags);
930
Joerg Roedeled888ae2009-05-22 17:16:04 +0200931 for (i = 0; i < HASH_SIZE; ++i) {
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200932 spin_lock(&dma_entry_hash[i].lock);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200933 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400934 if (entry->dev == dev) {
Joerg Roedeled888ae2009-05-22 17:16:04 +0200935 count += 1;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400936 *out_entry = entry;
937 }
Joerg Roedeled888ae2009-05-22 17:16:04 +0200938 }
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200939 spin_unlock(&dma_entry_hash[i].lock);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200940 }
941
Joerg Roedelbe81c6e2009-06-08 15:46:19 +0200942 local_irq_restore(flags);
943
Joerg Roedeled888ae2009-05-22 17:16:04 +0200944 return count;
945}
946
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100947static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200948{
949 struct device *dev = data;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400950 struct dma_debug_entry *uninitialized_var(entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200951 int count;
952
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800953 if (dma_debug_disabled())
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100954 return 0;
Joerg Roedeled888ae2009-05-22 17:16:04 +0200955
956 switch (action) {
957 case BUS_NOTIFY_UNBOUND_DRIVER:
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400958 count = device_dma_allocations(dev, &entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200959 if (count == 0)
960 break;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400961 err_printk(dev, entry, "DMA-API: device driver has pending "
Joerg Roedeled888ae2009-05-22 17:16:04 +0200962 "DMA allocations while released from device "
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400963 "[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]);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200969 break;
970 default:
971 break;
972 }
973
974 return 0;
975}
976
Joerg Roedel41531c82009-03-16 17:32:14 +0100977void dma_debug_add_bus(struct bus_type *bus)
978{
Joerg Roedeled888ae2009-05-22 17:16:04 +0200979 struct notifier_block *nb;
980
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800981 if (dma_debug_disabled())
Shaun Ruffellf797d982009-12-17 18:00:36 -0600982 return;
983
Joerg Roedeled888ae2009-05-22 17:16:04 +0200984 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
985 if (nb == NULL) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200986 pr_err("dma_debug_add_bus: out of memory\n");
Joerg Roedeled888ae2009-05-22 17:16:04 +0200987 return;
988 }
989
990 nb->notifier_call = dma_debug_device_change;
991
992 bus_register_notifier(bus, nb);
Joerg Roedel41531c82009-03-16 17:32:14 +0100993}
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100994
Joerg Roedel6bf07872009-01-09 12:54:42 +0100995/*
996 * Let the architectures decide how many entries should be preallocated.
997 */
998void dma_debug_init(u32 num_entries)
999{
1000 int i;
1001
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001002 if (dma_debug_disabled())
Joerg Roedel6bf07872009-01-09 12:54:42 +01001003 return;
1004
1005 for (i = 0; i < HASH_SIZE; ++i) {
1006 INIT_LIST_HEAD(&dma_entry_hash[i].list);
Ingo Molnarb0a5b832009-06-16 16:11:14 +02001007 spin_lock_init(&dma_entry_hash[i].lock);
Joerg Roedel6bf07872009-01-09 12:54:42 +01001008 }
1009
Joerg Roedel788dcfa2009-01-09 13:13:27 +01001010 if (dma_debug_fs_init() != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001011 pr_err("DMA-API: error creating debugfs entries - disabling\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +01001012 global_disable = true;
1013
1014 return;
1015 }
1016
Joerg Roedel59d3daa2009-01-09 13:01:56 +01001017 if (req_entries)
1018 num_entries = req_entries;
1019
Joerg Roedel6bf07872009-01-09 12:54:42 +01001020 if (prealloc_memory(num_entries) != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001021 pr_err("DMA-API: debugging out of memory error - disabled\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +01001022 global_disable = true;
1023
1024 return;
1025 }
1026
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +09001027 nr_total_entries = num_free_entries;
1028
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001029 pr_info("DMA-API: debugging enabled by kernel config\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +01001030}
1031
Joerg Roedel59d3daa2009-01-09 13:01:56 +01001032static __init int dma_debug_cmdline(char *str)
1033{
1034 if (!str)
1035 return -EINVAL;
1036
1037 if (strncmp(str, "off", 3) == 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001038 pr_info("DMA-API: debugging disabled on kernel command line\n");
Joerg Roedel59d3daa2009-01-09 13:01:56 +01001039 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
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001063static 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
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001069 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001070 entry = bucket_find_exact(bucket, ref);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001071
1072 if (!entry) {
Alexander Duyck8d640a52013-03-22 15:04:48 -07001073 /* must drop lock before calling dma_mapping_error */
1074 put_hash_bucket(bucket, &flags);
1075
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001076 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1077 err_printk(ref->dev, NULL,
Alexander Duyck8d640a52013-03-22 15:04:48 -07001078 "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);
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001086 }
Alexander Duyck8d640a52013-03-22 15:04:48 -07001087 return;
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001088 }
1089
1090 if (ref->size != entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001091 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001092 "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) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001099 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001100 "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) &&
Dan Williams0abdd7a2014-01-21 15:48:12 -08001106 (phys_addr(ref) != phys_addr(entry))) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001107 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001108 "DMA memory with different CPU address "
1109 "[device address=0x%016llx] [size=%llu bytes] "
Joerg Roedel59a40e702009-10-29 16:25:50 +01001110 "[cpu alloc address=0x%016llx] "
1111 "[cpu free address=0x%016llx]",
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001112 ref->dev_addr, ref->size,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001113 phys_addr(entry),
1114 phys_addr(ref));
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001115 }
1116
1117 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1118 ref->sg_call_ents != entry->sg_call_ents) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001119 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001120 "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) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001130 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001131 "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
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001139 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
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001148 hash_bucket_del(entry);
1149 dma_entry_free(entry);
1150
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001151 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))
Horia Geantaf9134be2014-09-02 14:28:14 +03001157 err_printk(dev, NULL, "DMA-API: device driver maps memory from "
David Woodhouse6c132d12009-01-19 16:52:39 +01001158 "stack [addr=%p]\n", addr);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001159}
1160
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001161static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001162{
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001163 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;
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001167
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001168 return !(b1 <= a2 || a1 >= b2);
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001169}
1170
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001171static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001172{
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001173 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);
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001176}
1177
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001178static void check_sync(struct device *dev,
1179 struct dma_debug_entry *ref,
1180 bool to_cpu)
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001181{
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001182 struct dma_debug_entry *entry;
1183 struct hash_bucket *bucket;
1184 unsigned long flags;
1185
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001186 bucket = get_hash_bucket(ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001187
Neil Hormanc6a21d02011-08-08 15:13:54 -04001188 entry = bucket_find_contain(&bucket, ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001189
1190 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001191 err_printk(dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001192 "to sync DMA memory it has not allocated "
1193 "[device address=0x%016llx] [size=%llu bytes]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001194 (unsigned long long)ref->dev_addr, ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001195 goto out;
1196 }
1197
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001198 if (ref->size > entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001199 err_printk(dev, entry, "DMA-API: device driver syncs"
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001200 " DMA memory outside allocated range "
1201 "[device address=0x%016llx] "
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001202 "[allocation size=%llu bytes] "
1203 "[sync offset+size=%llu]\n",
1204 entry->dev_addr, entry->size,
1205 ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001206 }
1207
Krzysztof Halasa42d53b42010-01-08 14:42:36 -08001208 if (entry->direction == DMA_BIDIRECTIONAL)
1209 goto out;
1210
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001211 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001212 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001213 "DMA memory with different direction "
1214 "[device address=0x%016llx] [size=%llu bytes] "
1215 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001216 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001217 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001218 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001219 }
1220
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001221 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001222 !(ref->direction == DMA_TO_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +01001223 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001224 "device read-only DMA memory for cpu "
1225 "[device address=0x%016llx] [size=%llu bytes] "
1226 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001227 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001228 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001229 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001230
1231 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001232 !(ref->direction == DMA_FROM_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +01001233 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001234 "device write-only DMA memory to device "
1235 "[device address=0x%016llx] [size=%llu bytes] "
1236 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001237 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001238 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001239 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001240
1241out:
1242 put_hash_bucket(bucket, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001243}
1244
Joerg Roedelf62bc982009-01-09 14:14:49 +01001245void 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
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001251 if (unlikely(dma_debug_disabled()))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001252 return;
1253
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001254 if (dma_mapping_error(dev, dma_addr))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001255 return;
1256
1257 entry = dma_entry_alloc();
1258 if (!entry)
1259 return;
1260
1261 entry->dev = dev;
1262 entry->type = dma_debug_page;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001263 entry->pfn = page_to_pfn(page);
1264 entry->offset = offset,
Joerg Roedelf62bc982009-01-09 14:14:49 +01001265 entry->dev_addr = dma_addr;
1266 entry->size = size;
1267 entry->direction = direction;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001268 entry->map_err_type = MAP_ERR_NOT_CHECKED;
Joerg Roedelf62bc982009-01-09 14:14:49 +01001269
Joerg Roedel9537a482009-03-23 15:35:08 +01001270 if (map_single)
Joerg Roedelf62bc982009-01-09 14:14:49 +01001271 entry->type = dma_debug_single;
Joerg Roedel9537a482009-03-23 15:35:08 +01001272
1273 if (!PageHighMem(page)) {
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001274 void *addr = page_address(page) + offset;
1275
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001276 check_for_stack(dev, addr);
1277 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +01001278 }
1279
1280 add_dma_entry(entry);
1281}
1282EXPORT_SYMBOL(debug_dma_map_page);
1283
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001284void 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
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001291 if (unlikely(dma_debug_disabled()))
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001292 return;
1293
1294 ref.dev = dev;
1295 ref.dev_addr = dma_addr;
1296 bucket = get_hash_bucket(&ref, &flags);
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001297
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001298 list_for_each_entry(entry, &bucket->list, list) {
1299 if (!exact_match(&ref, entry))
1300 continue;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001301
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001302 /*
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 }
1317
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001318 put_hash_bucket(bucket, &flags);
1319}
1320EXPORT_SYMBOL(debug_dma_mapping_error);
1321
Joerg Roedelf62bc982009-01-09 14:14:49 +01001322void 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
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001333 if (unlikely(dma_debug_disabled()))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001334 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
Joerg Roedel972aa452009-01-09 14:19:54 +01001343void 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
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001350 if (unlikely(dma_debug_disabled()))
Joerg Roedel972aa452009-01-09 14:19:54 +01001351 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;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001360 entry->pfn = page_to_pfn(sg_page(s));
1361 entry->offset = s->offset,
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001362 entry->size = sg_dma_len(s);
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001363 entry->dev_addr = sg_dma_address(s);
Joerg Roedel972aa452009-01-09 14:19:54 +01001364 entry->direction = direction;
1365 entry->sg_call_ents = nents;
1366 entry->sg_mapped_ents = mapped_ents;
1367
Joerg Roedel9537a482009-03-23 15:35:08 +01001368 if (!PageHighMem(sg_page(s))) {
1369 check_for_stack(dev, sg_virt(s));
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001370 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
Joerg Roedel9537a482009-03-23 15:35:08 +01001371 }
Joerg Roedel972aa452009-01-09 14:19:54 +01001372
1373 add_dma_entry(entry);
1374 }
1375}
1376EXPORT_SYMBOL(debug_dma_map_sg);
1377
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001378static int get_nr_mapped_entries(struct device *dev,
1379 struct dma_debug_entry *ref)
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001380{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001381 struct dma_debug_entry *entry;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001382 struct hash_bucket *bucket;
1383 unsigned long flags;
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001384 int mapped_ents;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001385
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001386 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001387 entry = bucket_find_exact(bucket, ref);
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001388 mapped_ents = 0;
1389
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001390 if (entry)
1391 mapped_ents = entry->sg_mapped_ents;
1392 put_hash_bucket(bucket, &flags);
1393
1394 return mapped_ents;
1395}
1396
Joerg Roedel972aa452009-01-09 14:19:54 +01001397void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1398 int nelems, int dir)
1399{
Joerg Roedel972aa452009-01-09 14:19:54 +01001400 struct scatterlist *s;
1401 int mapped_ents = 0, i;
Joerg Roedel972aa452009-01-09 14:19:54 +01001402
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001403 if (unlikely(dma_debug_disabled()))
Joerg Roedel972aa452009-01-09 14:19:54 +01001404 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,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001411 .pfn = page_to_pfn(sg_page(s)),
1412 .offset = s->offset,
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001413 .dev_addr = sg_dma_address(s),
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001414 .size = sg_dma_len(s),
Joerg Roedel972aa452009-01-09 14:19:54 +01001415 .direction = dir,
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001416 .sg_call_ents = nelems,
Joerg Roedel972aa452009-01-09 14:19:54 +01001417 };
1418
1419 if (mapped_ents && i >= mapped_ents)
1420 break;
1421
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001422 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001423 mapped_ents = get_nr_mapped_entries(dev, &ref);
Joerg Roedel972aa452009-01-09 14:19:54 +01001424
1425 check_unmap(&ref);
1426 }
1427}
1428EXPORT_SYMBOL(debug_dma_unmap_sg);
1429
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001430void 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
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001435 if (unlikely(dma_debug_disabled()))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001436 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;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001447 entry->pfn = page_to_pfn(virt_to_page(virt));
1448 entry->offset = (size_t) virt & PAGE_MASK;
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001449 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,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001463 .pfn = page_to_pfn(virt_to_page(virt)),
1464 .offset = (size_t) virt & PAGE_MASK,
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001465 .dev_addr = addr,
1466 .size = size,
1467 .direction = DMA_BIDIRECTIONAL,
1468 };
1469
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001470 if (unlikely(dma_debug_disabled()))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001471 return;
1472
1473 check_unmap(&ref);
1474}
1475EXPORT_SYMBOL(debug_dma_free_coherent);
1476
Joerg Roedelb9d23172009-01-09 14:43:04 +01001477void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1478 size_t size, int direction)
1479{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001480 struct dma_debug_entry ref;
1481
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001482 if (unlikely(dma_debug_disabled()))
Joerg Roedelb9d23172009-01-09 14:43:04 +01001483 return;
1484
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001485 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);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001493}
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{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001500 struct dma_debug_entry ref;
1501
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001502 if (unlikely(dma_debug_disabled()))
Joerg Roedelb9d23172009-01-09 14:43:04 +01001503 return;
1504
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001505 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);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001513}
1514EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1515
Joerg Roedel948408b2009-01-09 14:55:38 +01001516void 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{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001521 struct dma_debug_entry ref;
1522
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001523 if (unlikely(dma_debug_disabled()))
Joerg Roedel948408b2009-01-09 14:55:38 +01001524 return;
1525
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001526 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);
Joerg Roedel948408b2009-01-09 14:55:38 +01001534}
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{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001542 struct dma_debug_entry ref;
1543
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001544 if (unlikely(dma_debug_disabled()))
Joerg Roedel948408b2009-01-09 14:55:38 +01001545 return;
1546
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001547 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);
Joerg Roedel948408b2009-01-09 14:55:38 +01001555}
1556EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1557
Joerg Roedela31fba52009-01-09 15:01:12 +01001558void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1559 int nelems, int direction)
1560{
1561 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001562 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001563
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001564 if (unlikely(dma_debug_disabled()))
Joerg Roedela31fba52009-01-09 15:01:12 +01001565 return;
1566
1567 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001568
1569 struct dma_debug_entry ref = {
1570 .type = dma_debug_sg,
1571 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001572 .pfn = page_to_pfn(sg_page(s)),
1573 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001574 .dev_addr = sg_dma_address(s),
1575 .size = sg_dma_len(s),
1576 .direction = direction,
1577 .sg_call_ents = nelems,
1578 };
1579
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001580 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001581 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001582
1583 if (i >= mapped_ents)
1584 break;
1585
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001586 check_sync(dev, &ref, true);
Joerg Roedela31fba52009-01-09 15:01:12 +01001587 }
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;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001595 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001596
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001597 if (unlikely(dma_debug_disabled()))
Joerg Roedela31fba52009-01-09 15:01:12 +01001598 return;
1599
1600 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001601
1602 struct dma_debug_entry ref = {
1603 .type = dma_debug_sg,
1604 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001605 .pfn = page_to_pfn(sg_page(s)),
1606 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001607 .dev_addr = sg_dma_address(s),
1608 .size = sg_dma_len(s),
1609 .direction = direction,
1610 .sg_call_ents = nelems,
1611 };
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001612 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001613 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001614
1615 if (i >= mapped_ents)
1616 break;
1617
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001618 check_sync(dev, &ref, false);
Joerg Roedela31fba52009-01-09 15:01:12 +01001619 }
1620}
1621EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1622
Joerg Roedel1745de52009-05-22 21:49:51 +02001623static 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])
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001634 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1635 current_driver_name);
Joerg Roedel1745de52009-05-22 21:49:51 +02001636
1637
1638 return 1;
1639}
1640__setup("dma_debug_driver=", dma_debug_driver_setup);