blob: 712a897174e41b330ba3c881848f73ab5e146e99 [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
Ingo Molnar68db0cf2017-02-08 18:51:37 +010020#include <linux/sched/task_stack.h>
Joerg Roedel972aa452009-01-09 14:19:54 +010021#include <linux/scatterlist.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010022#include <linux/dma-mapping.h>
Ingo Molnar29930022017-02-08 18:51:36 +010023#include <linux/sched/task.h>
David Woodhouse6c132d12009-01-19 16:52:39 +010024#include <linux/stacktrace.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010025#include <linux/dma-debug.h>
Joerg Roedel30dfa902009-01-09 12:34:49 +010026#include <linux/spinlock.h>
Andy Lutomirskib4a0f532016-08-11 02:35:22 -070027#include <linux/vmalloc.h>
Joerg Roedel788dcfa2009-01-09 13:13:27 +010028#include <linux/debugfs.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020029#include <linux/uaccess.h>
Paul Gortmaker23a7bfa2011-07-01 16:23:59 -040030#include <linux/export.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010031#include <linux/device.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010032#include <linux/types.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010033#include <linux/sched.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020034#include <linux/ctype.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010035#include <linux/list.h>
Joerg Roedel6bf07872009-01-09 12:54:42 +010036#include <linux/slab.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010037
Joerg Roedel2e34bde2009-03-16 16:51:55 +010038#include <asm/sections.h>
39
Joerg Roedel30dfa902009-01-09 12:34:49 +010040#define HASH_SIZE 1024ULL
41#define HASH_FN_SHIFT 13
42#define HASH_FN_MASK (HASH_SIZE - 1)
43
Christoph Hellwig15b28bb2018-04-16 17:22:28 +020044/* allow architectures to override this if absolutely required */
45#ifndef PREALLOC_DMA_DEBUG_ENTRIES
46#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
47#endif
48
Joerg Roedelf2f45e52009-01-09 12:19:52 +010049enum {
50 dma_debug_single,
51 dma_debug_page,
52 dma_debug_sg,
53 dma_debug_coherent,
Niklas Söderlund0e74b342016-08-10 13:22:15 +020054 dma_debug_resource,
Joerg Roedelf2f45e52009-01-09 12:19:52 +010055};
56
Shuah Khan6c9c6d62012-10-08 11:08:06 -060057enum map_err_types {
58 MAP_ERR_CHECK_NOT_APPLICABLE,
59 MAP_ERR_NOT_CHECKED,
60 MAP_ERR_CHECKED,
61};
62
David Woodhouse6c132d12009-01-19 16:52:39 +010063#define DMA_DEBUG_STACKTRACE_ENTRIES 5
64
Dan Williams0abdd7a2014-01-21 15:48:12 -080065/**
66 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
67 * @list: node on pre-allocated free_entries list
68 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
69 * @type: single, page, sg, coherent
70 * @pfn: page frame of the start address
71 * @offset: offset of mapping relative to pfn
72 * @size: length of the mapping
73 * @direction: enum dma_data_direction
74 * @sg_call_ents: 'nents' from dma_map_sg
75 * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
76 * @map_err_type: track whether dma_mapping_error() was checked
77 * @stacktrace: support backtraces when a violation is detected
78 */
Joerg Roedelf2f45e52009-01-09 12:19:52 +010079struct dma_debug_entry {
80 struct list_head list;
81 struct device *dev;
82 int type;
Dan Williams0abdd7a2014-01-21 15:48:12 -080083 unsigned long pfn;
84 size_t offset;
Joerg Roedelf2f45e52009-01-09 12:19:52 +010085 u64 dev_addr;
86 u64 size;
87 int direction;
88 int sg_call_ents;
89 int sg_mapped_ents;
Shuah Khan6c9c6d62012-10-08 11:08:06 -060090 enum map_err_types map_err_type;
David Woodhouse6c132d12009-01-19 16:52:39 +010091#ifdef CONFIG_STACKTRACE
92 struct stack_trace stacktrace;
93 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
94#endif
Joerg Roedelf2f45e52009-01-09 12:19:52 +010095};
96
Neil Hormanc6a21d02011-08-08 15:13:54 -040097typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
98
Joerg Roedel30dfa902009-01-09 12:34:49 +010099struct hash_bucket {
100 struct list_head list;
101 spinlock_t lock;
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100102} ____cacheline_aligned_in_smp;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100103
104/* Hash list to save the allocated dma addresses */
105static struct hash_bucket dma_entry_hash[HASH_SIZE];
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100106/* List of pre-allocated dma_debug_entry's */
107static LIST_HEAD(free_entries);
108/* Lock for the list above */
109static DEFINE_SPINLOCK(free_entries_lock);
110
111/* Global disable flag - will be set in case of an error */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700112static bool global_disable __read_mostly;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100113
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -0800114/* Early initialization disable flag, set at the end of dma_debug_init */
115static bool dma_debug_initialized __read_mostly;
116
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800117static inline bool dma_debug_disabled(void)
118{
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -0800119 return global_disable || !dma_debug_initialized;
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800120}
121
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100122/* Global error count */
123static u32 error_count;
124
125/* Global error show enable*/
126static u32 show_all_errors __read_mostly;
127/* Number of errors to show */
128static u32 show_num_errors = 1;
129
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100130static u32 num_free_entries;
131static u32 min_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900132static u32 nr_total_entries;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100133
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100134/* number of preallocated entries requested by kernel cmdline */
135static u32 req_entries;
136
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100137/* debugfs dentry's for the stuff above */
138static struct dentry *dma_debug_dent __read_mostly;
139static struct dentry *global_disable_dent __read_mostly;
140static struct dentry *error_count_dent __read_mostly;
141static struct dentry *show_all_errors_dent __read_mostly;
142static struct dentry *show_num_errors_dent __read_mostly;
143static struct dentry *num_free_entries_dent __read_mostly;
144static struct dentry *min_free_entries_dent __read_mostly;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200145static struct dentry *filter_dent __read_mostly;
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100146
Joerg Roedel2e507d82009-05-22 18:24:20 +0200147/* per-driver filter related state */
148
149#define NAME_MAX_LEN 64
150
151static char current_driver_name[NAME_MAX_LEN] __read_mostly;
152static struct device_driver *current_driver __read_mostly;
153
154static DEFINE_RWLOCK(driver_name_lock);
Joerg Roedel30dfa902009-01-09 12:34:49 +0100155
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600156static const char *const maperr2str[] = {
157 [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
158 [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
159 [MAP_ERR_CHECKED] = "dma map error checked",
160};
161
Niklas Söderlund0e74b342016-08-10 13:22:15 +0200162static const char *type2name[5] = { "single", "page",
163 "scather-gather", "coherent",
164 "resource" };
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100165
166static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
167 "DMA_FROM_DEVICE", "DMA_NONE" };
168
169/*
170 * The access to some variables in this macro is racy. We can't use atomic_t
171 * here because all these variables are exported to debugfs. Some of them even
172 * writeable. This is also the reason why a lock won't help much. But anyway,
173 * the races are no big deal. Here is why:
174 *
175 * error_count: the addition is racy, but the worst thing that can happen is
176 * that we don't count some errors
177 * show_num_errors: the subtraction is racy. Also no big deal because in
178 * worst case this will result in one warning more in the
179 * system log than the user configured. This variable is
180 * writeable via debugfs.
181 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100182static inline void dump_entry_trace(struct dma_debug_entry *entry)
183{
184#ifdef CONFIG_STACKTRACE
185 if (entry) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200186 pr_warning("Mapped at:\n");
David Woodhouse6c132d12009-01-19 16:52:39 +0100187 print_stack_trace(&entry->stacktrace, 0);
188 }
189#endif
190}
191
Joerg Roedel2e507d82009-05-22 18:24:20 +0200192static bool driver_filter(struct device *dev)
193{
Joerg Roedel0bf84122009-06-08 15:53:46 +0200194 struct device_driver *drv;
195 unsigned long flags;
196 bool ret;
197
Joerg Roedel2e507d82009-05-22 18:24:20 +0200198 /* driver filter off */
199 if (likely(!current_driver_name[0]))
200 return true;
201
202 /* driver filter on and initialized */
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400203 if (current_driver && dev && dev->driver == current_driver)
Joerg Roedel2e507d82009-05-22 18:24:20 +0200204 return true;
205
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400206 /* driver filter on, but we can't filter on a NULL device... */
207 if (!dev)
208 return false;
209
Joerg Roedel0bf84122009-06-08 15:53:46 +0200210 if (current_driver || !current_driver_name[0])
211 return false;
212
Joerg Roedel2e507d82009-05-22 18:24:20 +0200213 /* driver filter on but not yet initialized */
Alan Sternf3ff9242012-01-24 13:35:24 -0500214 drv = dev->driver;
Joerg Roedel0bf84122009-06-08 15:53:46 +0200215 if (!drv)
216 return false;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200217
Joerg Roedel0bf84122009-06-08 15:53:46 +0200218 /* lock to protect against change of current_driver_name */
219 read_lock_irqsave(&driver_name_lock, flags);
Joerg Roedel2e507d82009-05-22 18:24:20 +0200220
Joerg Roedel0bf84122009-06-08 15:53:46 +0200221 ret = false;
222 if (drv->name &&
223 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
224 current_driver = drv;
225 ret = true;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200226 }
227
Joerg Roedel0bf84122009-06-08 15:53:46 +0200228 read_unlock_irqrestore(&driver_name_lock, flags);
Joerg Roedel0bf84122009-06-08 15:53:46 +0200229
230 return ret;
Joerg Roedel2e507d82009-05-22 18:24:20 +0200231}
232
Kyle McMartinec9c96e2009-08-19 21:17:08 -0400233#define err_printk(dev, entry, format, arg...) do { \
234 error_count += 1; \
235 if (driver_filter(dev) && \
236 (show_all_errors || show_num_errors > 0)) { \
237 WARN(1, "%s %s: " format, \
238 dev ? dev_driver_string(dev) : "NULL", \
239 dev ? dev_name(dev) : "NULL", ## arg); \
240 dump_entry_trace(entry); \
241 } \
242 if (!show_all_errors && show_num_errors > 0) \
243 show_num_errors -= 1; \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100244 } while (0);
245
Joerg Roedel30dfa902009-01-09 12:34:49 +0100246/*
247 * Hash related functions
248 *
249 * Every DMA-API request is saved into a struct dma_debug_entry. To
250 * have quick access to these structs they are stored into a hash.
251 */
252static int hash_fn(struct dma_debug_entry *entry)
253{
254 /*
255 * Hash function is based on the dma address.
256 * We use bits 20-27 here as the index into the hash
257 */
258 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
259}
260
261/*
262 * Request exclusive access to a hash bucket for a given dma_debug_entry.
263 */
264static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
265 unsigned long *flags)
Stephen Boydd5dfc802016-07-26 15:21:08 -0700266 __acquires(&dma_entry_hash[idx].lock)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100267{
268 int idx = hash_fn(entry);
269 unsigned long __flags;
270
271 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
272 *flags = __flags;
273 return &dma_entry_hash[idx];
274}
275
276/*
277 * Give up exclusive access to the hash bucket
278 */
279static void put_hash_bucket(struct hash_bucket *bucket,
280 unsigned long *flags)
Stephen Boydd5dfc802016-07-26 15:21:08 -0700281 __releases(&bucket->lock)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100282{
283 unsigned long __flags = *flags;
284
285 spin_unlock_irqrestore(&bucket->lock, __flags);
286}
287
Neil Hormanc6a21d02011-08-08 15:13:54 -0400288static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
289{
Thomas Jarosch91ec37c2011-11-17 20:31:02 +0100290 return ((a->dev_addr == b->dev_addr) &&
Neil Hormanc6a21d02011-08-08 15:13:54 -0400291 (a->dev == b->dev)) ? true : false;
292}
293
294static bool containing_match(struct dma_debug_entry *a,
295 struct dma_debug_entry *b)
296{
297 if (a->dev != b->dev)
298 return false;
299
300 if ((b->dev_addr <= a->dev_addr) &&
301 ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
302 return true;
303
304 return false;
305}
306
Joerg Roedel30dfa902009-01-09 12:34:49 +0100307/*
308 * Search a given entry in the hash bucket list
309 */
Neil Hormanc6a21d02011-08-08 15:13:54 -0400310static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
311 struct dma_debug_entry *ref,
312 match_fn match)
Joerg Roedel30dfa902009-01-09 12:34:49 +0100313{
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200314 struct dma_debug_entry *entry, *ret = NULL;
Ming Leife73fbe2012-10-19 13:57:01 -0700315 int matches = 0, match_lvl, last_lvl = -1;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100316
317 list_for_each_entry(entry, &bucket->list, list) {
Neil Hormanc6a21d02011-08-08 15:13:54 -0400318 if (!match(ref, entry))
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200319 continue;
320
321 /*
322 * Some drivers map the same physical address multiple
323 * times. Without a hardware IOMMU this results in the
324 * same device addresses being put into the dma-debug
325 * hash multiple times too. This can result in false
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200326 * positives being reported. Therefore we implement a
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200327 * best-fit algorithm here which returns the entry from
328 * the hash which fits best to the reference value
329 * instead of the first-fit.
330 */
331 matches += 1;
332 match_lvl = 0;
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200333 entry->size == ref->size ? ++match_lvl : 0;
334 entry->type == ref->type ? ++match_lvl : 0;
335 entry->direction == ref->direction ? ++match_lvl : 0;
336 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200337
Joerg Roedele5e8c5b2009-06-11 10:03:42 +0200338 if (match_lvl == 4) {
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200339 /* perfect-fit - return the result */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100340 return entry;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200341 } else if (match_lvl > last_lvl) {
342 /*
343 * We found an entry that fits better then the
Ming Leife73fbe2012-10-19 13:57:01 -0700344 * previous one or it is the 1st match.
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200345 */
346 last_lvl = match_lvl;
347 ret = entry;
348 }
Joerg Roedel30dfa902009-01-09 12:34:49 +0100349 }
350
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200351 /*
352 * If we have multiple matches but no perfect-fit, just return
353 * NULL.
354 */
355 ret = (matches == 1) ? ret : NULL;
356
357 return ret;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100358}
359
Neil Hormanc6a21d02011-08-08 15:13:54 -0400360static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
361 struct dma_debug_entry *ref)
362{
363 return __hash_bucket_find(bucket, ref, exact_match);
364}
365
366static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
367 struct dma_debug_entry *ref,
368 unsigned long *flags)
369{
370
371 unsigned int max_range = dma_get_max_seg_size(ref->dev);
372 struct dma_debug_entry *entry, index = *ref;
373 unsigned int range = 0;
374
375 while (range <= max_range) {
Sebastian Otta7a2c022015-04-16 12:43:25 -0700376 entry = __hash_bucket_find(*bucket, ref, containing_match);
Neil Hormanc6a21d02011-08-08 15:13:54 -0400377
378 if (entry)
379 return entry;
380
381 /*
382 * Nothing found, go back a hash bucket
383 */
384 put_hash_bucket(*bucket, flags);
385 range += (1 << HASH_FN_SHIFT);
386 index.dev_addr -= (1 << HASH_FN_SHIFT);
387 *bucket = get_hash_bucket(&index, flags);
388 }
389
390 return NULL;
391}
392
Joerg Roedel30dfa902009-01-09 12:34:49 +0100393/*
394 * Add an entry to a hash bucket
395 */
396static void hash_bucket_add(struct hash_bucket *bucket,
397 struct dma_debug_entry *entry)
398{
399 list_add_tail(&entry->list, &bucket->list);
400}
401
402/*
403 * Remove entry from a hash bucket list
404 */
405static void hash_bucket_del(struct dma_debug_entry *entry)
406{
407 list_del(&entry->list);
408}
409
Dan Williams0abdd7a2014-01-21 15:48:12 -0800410static unsigned long long phys_addr(struct dma_debug_entry *entry)
411{
Niklas Söderlund0e74b342016-08-10 13:22:15 +0200412 if (entry->type == dma_debug_resource)
413 return __pfn_to_phys(entry->pfn) + entry->offset;
414
Dan Williams0abdd7a2014-01-21 15:48:12 -0800415 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
416}
417
Joerg Roedel30dfa902009-01-09 12:34:49 +0100418/*
David Woodhouseac26c182009-02-12 16:19:13 +0100419 * Dump mapping entries for debugging purposes
420 */
421void debug_dma_dump_mappings(struct device *dev)
422{
423 int idx;
424
425 for (idx = 0; idx < HASH_SIZE; idx++) {
426 struct hash_bucket *bucket = &dma_entry_hash[idx];
427 struct dma_debug_entry *entry;
428 unsigned long flags;
429
430 spin_lock_irqsave(&bucket->lock, flags);
431
432 list_for_each_entry(entry, &bucket->list, list) {
433 if (!dev || dev == entry->dev) {
434 dev_info(entry->dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800435 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
David Woodhouseac26c182009-02-12 16:19:13 +0100436 type2name[entry->type], idx,
Dan Williams0abdd7a2014-01-21 15:48:12 -0800437 phys_addr(entry), entry->pfn,
David Woodhouseac26c182009-02-12 16:19:13 +0100438 entry->dev_addr, entry->size,
Shuah Khan6c9c6d62012-10-08 11:08:06 -0600439 dir2name[entry->direction],
440 maperr2str[entry->map_err_type]);
David Woodhouseac26c182009-02-12 16:19:13 +0100441 }
442 }
443
444 spin_unlock_irqrestore(&bucket->lock, flags);
445 }
446}
447EXPORT_SYMBOL(debug_dma_dump_mappings);
448
449/*
Dan Williams3b7a6412014-03-03 15:38:21 -0800450 * For each mapping (initial cacheline in the case of
451 * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
452 * scatterlist, or the cacheline specified in dma_map_single) insert
453 * into this tree using the cacheline as the key. At
Dan Williams0abdd7a2014-01-21 15:48:12 -0800454 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
Dan Williams3b7a6412014-03-03 15:38:21 -0800455 * the entry already exists at insertion time add a tag as a reference
Dan Williams0abdd7a2014-01-21 15:48:12 -0800456 * count for the overlapping mappings. For now, the overlap tracking
Dan Williams3b7a6412014-03-03 15:38:21 -0800457 * just ensures that 'unmaps' balance 'maps' before marking the
458 * cacheline idle, but we should also be flagging overlaps as an API
459 * violation.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800460 *
461 * Memory usage is mostly constrained by the maximum number of available
462 * dma-debug entries in that we need a free dma_debug_entry before
Dan Williams3b7a6412014-03-03 15:38:21 -0800463 * inserting into the tree. In the case of dma_map_page and
464 * dma_alloc_coherent there is only one dma_debug_entry and one
465 * dma_active_cacheline entry to track per event. dma_map_sg(), on the
466 * other hand, consumes a single dma_debug_entry, but inserts 'nents'
467 * entries into the tree.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800468 *
469 * At any time debug_dma_assert_idle() can be called to trigger a
Dan Williams3b7a6412014-03-03 15:38:21 -0800470 * warning if any cachelines in the given page are in the active set.
Dan Williams0abdd7a2014-01-21 15:48:12 -0800471 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800472static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800473static DEFINE_SPINLOCK(radix_lock);
Dan Williams3b7a6412014-03-03 15:38:21 -0800474#define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
475#define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
476#define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800477
Dan Williams3b7a6412014-03-03 15:38:21 -0800478static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
479{
480 return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
481 (entry->offset >> L1_CACHE_SHIFT);
482}
483
484static int active_cacheline_read_overlap(phys_addr_t cln)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800485{
486 int overlap = 0, i;
487
488 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
Dan Williams3b7a6412014-03-03 15:38:21 -0800489 if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
Dan Williams0abdd7a2014-01-21 15:48:12 -0800490 overlap |= 1 << i;
491 return overlap;
492}
493
Dan Williams3b7a6412014-03-03 15:38:21 -0800494static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800495{
496 int i;
497
Dan Williams3b7a6412014-03-03 15:38:21 -0800498 if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
Dan Williams59f2e7d2014-01-29 14:05:53 -0800499 return overlap;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800500
501 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
502 if (overlap & 1 << i)
Dan Williams3b7a6412014-03-03 15:38:21 -0800503 radix_tree_tag_set(&dma_active_cacheline, cln, i);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800504 else
Dan Williams3b7a6412014-03-03 15:38:21 -0800505 radix_tree_tag_clear(&dma_active_cacheline, cln, i);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800506
507 return overlap;
508}
509
Dan Williams3b7a6412014-03-03 15:38:21 -0800510static void active_cacheline_inc_overlap(phys_addr_t cln)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800511{
Dan Williams3b7a6412014-03-03 15:38:21 -0800512 int overlap = active_cacheline_read_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800513
Dan Williams3b7a6412014-03-03 15:38:21 -0800514 overlap = active_cacheline_set_overlap(cln, ++overlap);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800515
516 /* If we overflowed the overlap counter then we're potentially
517 * leaking dma-mappings. Otherwise, if maps and unmaps are
518 * balanced then this overflow may cause false negatives in
Dan Williams3b7a6412014-03-03 15:38:21 -0800519 * debug_dma_assert_idle() as the cacheline may be marked idle
Dan Williams0abdd7a2014-01-21 15:48:12 -0800520 * prematurely.
521 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800522 WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
523 "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
524 ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800525}
526
Dan Williams3b7a6412014-03-03 15:38:21 -0800527static int active_cacheline_dec_overlap(phys_addr_t cln)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800528{
Dan Williams3b7a6412014-03-03 15:38:21 -0800529 int overlap = active_cacheline_read_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800530
Dan Williams3b7a6412014-03-03 15:38:21 -0800531 return active_cacheline_set_overlap(cln, --overlap);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800532}
533
Dan Williams3b7a6412014-03-03 15:38:21 -0800534static int active_cacheline_insert(struct dma_debug_entry *entry)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800535{
Dan Williams3b7a6412014-03-03 15:38:21 -0800536 phys_addr_t cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800537 unsigned long flags;
538 int rc;
539
Dan Williams3b7a6412014-03-03 15:38:21 -0800540 /* If the device is not writing memory then we don't have any
541 * concerns about the cpu consuming stale data. This mitigates
542 * legitimate usages of overlapping mappings.
543 */
544 if (entry->direction == DMA_TO_DEVICE)
545 return 0;
546
Dan Williams0abdd7a2014-01-21 15:48:12 -0800547 spin_lock_irqsave(&radix_lock, flags);
Dan Williams3b7a6412014-03-03 15:38:21 -0800548 rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800549 if (rc == -EEXIST)
Dan Williams3b7a6412014-03-03 15:38:21 -0800550 active_cacheline_inc_overlap(cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800551 spin_unlock_irqrestore(&radix_lock, flags);
552
553 return rc;
554}
555
Dan Williams3b7a6412014-03-03 15:38:21 -0800556static void active_cacheline_remove(struct dma_debug_entry *entry)
Dan Williams0abdd7a2014-01-21 15:48:12 -0800557{
Dan Williams3b7a6412014-03-03 15:38:21 -0800558 phys_addr_t cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800559 unsigned long flags;
560
Dan Williams3b7a6412014-03-03 15:38:21 -0800561 /* ...mirror the insert case */
562 if (entry->direction == DMA_TO_DEVICE)
563 return;
564
Dan Williams0abdd7a2014-01-21 15:48:12 -0800565 spin_lock_irqsave(&radix_lock, flags);
Dan Williams59f2e7d2014-01-29 14:05:53 -0800566 /* since we are counting overlaps the final put of the
Dan Williams3b7a6412014-03-03 15:38:21 -0800567 * cacheline will occur when the overlap count is 0.
568 * active_cacheline_dec_overlap() returns -1 in that case
Dan Williams59f2e7d2014-01-29 14:05:53 -0800569 */
Dan Williams3b7a6412014-03-03 15:38:21 -0800570 if (active_cacheline_dec_overlap(cln) < 0)
571 radix_tree_delete(&dma_active_cacheline, cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800572 spin_unlock_irqrestore(&radix_lock, flags);
573}
574
575/**
576 * debug_dma_assert_idle() - assert that a page is not undergoing dma
Dan Williams3b7a6412014-03-03 15:38:21 -0800577 * @page: page to lookup in the dma_active_cacheline tree
Dan Williams0abdd7a2014-01-21 15:48:12 -0800578 *
579 * Place a call to this routine in cases where the cpu touching the page
580 * before the dma completes (page is dma_unmapped) will lead to data
581 * corruption.
582 */
583void debug_dma_assert_idle(struct page *page)
584{
Dan Williams3b7a6412014-03-03 15:38:21 -0800585 static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
586 struct dma_debug_entry *entry = NULL;
587 void **results = (void **) &ents;
588 unsigned int nents, i;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800589 unsigned long flags;
Dan Williams3b7a6412014-03-03 15:38:21 -0800590 phys_addr_t cln;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800591
Haggai Eranc9d120b2015-07-17 16:24:06 -0700592 if (dma_debug_disabled())
593 return;
594
Dan Williams0abdd7a2014-01-21 15:48:12 -0800595 if (!page)
596 return;
597
Dan Williams3b7a6412014-03-03 15:38:21 -0800598 cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800599 spin_lock_irqsave(&radix_lock, flags);
Dan Williams3b7a6412014-03-03 15:38:21 -0800600 nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
601 CACHELINES_PER_PAGE);
602 for (i = 0; i < nents; i++) {
603 phys_addr_t ent_cln = to_cacheline_number(ents[i]);
604
605 if (ent_cln == cln) {
606 entry = ents[i];
607 break;
608 } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
609 break;
610 }
Dan Williams0abdd7a2014-01-21 15:48:12 -0800611 spin_unlock_irqrestore(&radix_lock, flags);
612
613 if (!entry)
614 return;
615
Dan Williams3b7a6412014-03-03 15:38:21 -0800616 cln = to_cacheline_number(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800617 err_printk(entry->dev, entry,
Dan Williams3b7a6412014-03-03 15:38:21 -0800618 "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
619 &cln);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800620}
621
622/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100623 * Wrapper function for adding an entry to the hash.
624 * This function takes care of locking itself.
625 */
626static void add_dma_entry(struct dma_debug_entry *entry)
627{
628 struct hash_bucket *bucket;
629 unsigned long flags;
Dan Williams0abdd7a2014-01-21 15:48:12 -0800630 int rc;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100631
632 bucket = get_hash_bucket(entry, &flags);
633 hash_bucket_add(bucket, entry);
634 put_hash_bucket(bucket, &flags);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800635
Dan Williams3b7a6412014-03-03 15:38:21 -0800636 rc = active_cacheline_insert(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800637 if (rc == -ENOMEM) {
Dan Williams3b7a6412014-03-03 15:38:21 -0800638 pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
Dan Williams0abdd7a2014-01-21 15:48:12 -0800639 global_disable = true;
640 }
641
642 /* TODO: report -EEXIST errors here as overlapping mappings are
643 * not supported by the DMA API
644 */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100645}
646
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900647static struct dma_debug_entry *__dma_entry_alloc(void)
648{
649 struct dma_debug_entry *entry;
650
651 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
652 list_del(&entry->list);
653 memset(entry, 0, sizeof(*entry));
654
655 num_free_entries -= 1;
656 if (num_free_entries < min_free_entries)
657 min_free_entries = num_free_entries;
658
659 return entry;
660}
661
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100662/* struct dma_entry allocator
663 *
664 * The next two functions implement the allocator for
665 * struct dma_debug_entries.
666 */
667static struct dma_debug_entry *dma_entry_alloc(void)
668{
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200669 struct dma_debug_entry *entry;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100670 unsigned long flags;
671
672 spin_lock_irqsave(&free_entries_lock, flags);
673
674 if (list_empty(&free_entries)) {
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100675 global_disable = true;
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200676 spin_unlock_irqrestore(&free_entries_lock, flags);
Ville Syrjälä3017cd62016-05-26 15:16:25 -0700677 pr_err("DMA-API: debugging out of memory - disabling\n");
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200678 return NULL;
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100679 }
680
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900681 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100682
Jakub Kicinski29cdd4e2012-04-04 03:19:10 +0200683 spin_unlock_irqrestore(&free_entries_lock, flags);
684
David Woodhouse6c132d12009-01-19 16:52:39 +0100685#ifdef CONFIG_STACKTRACE
686 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
687 entry->stacktrace.entries = entry->st_entries;
688 entry->stacktrace.skip = 2;
689 save_stack_trace(&entry->stacktrace);
690#endif
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100691
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100692 return entry;
693}
694
695static void dma_entry_free(struct dma_debug_entry *entry)
696{
697 unsigned long flags;
698
Dan Williams3b7a6412014-03-03 15:38:21 -0800699 active_cacheline_remove(entry);
Dan Williams0abdd7a2014-01-21 15:48:12 -0800700
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100701 /*
702 * add to beginning of the list - this way the entries are
703 * more likely cache hot when they are reallocated.
704 */
705 spin_lock_irqsave(&free_entries_lock, flags);
706 list_add(&entry->list, &free_entries);
707 num_free_entries += 1;
708 spin_unlock_irqrestore(&free_entries_lock, flags);
709}
710
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900711int dma_debug_resize_entries(u32 num_entries)
712{
713 int i, delta, ret = 0;
714 unsigned long flags;
715 struct dma_debug_entry *entry;
716 LIST_HEAD(tmp);
717
718 spin_lock_irqsave(&free_entries_lock, flags);
719
720 if (nr_total_entries < num_entries) {
721 delta = num_entries - nr_total_entries;
722
723 spin_unlock_irqrestore(&free_entries_lock, flags);
724
725 for (i = 0; i < delta; i++) {
726 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
727 if (!entry)
728 break;
729
730 list_add_tail(&entry->list, &tmp);
731 }
732
733 spin_lock_irqsave(&free_entries_lock, flags);
734
735 list_splice(&tmp, &free_entries);
736 nr_total_entries += i;
737 num_free_entries += i;
738 } else {
739 delta = nr_total_entries - num_entries;
740
741 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
742 entry = __dma_entry_alloc();
743 kfree(entry);
744 }
745
746 nr_total_entries -= i;
747 }
748
749 if (nr_total_entries != num_entries)
750 ret = 1;
751
752 spin_unlock_irqrestore(&free_entries_lock, flags);
753
754 return ret;
755}
756EXPORT_SYMBOL(dma_debug_resize_entries);
757
Joerg Roedel6bf07872009-01-09 12:54:42 +0100758/*
759 * DMA-API debugging init code
760 *
761 * The init code does two things:
762 * 1. Initialize core data structures
763 * 2. Preallocate a given number of dma_debug_entry structs
764 */
765
766static int prealloc_memory(u32 num_entries)
767{
768 struct dma_debug_entry *entry, *next_entry;
769 int i;
770
771 for (i = 0; i < num_entries; ++i) {
772 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
773 if (!entry)
774 goto out_err;
775
776 list_add_tail(&entry->list, &free_entries);
777 }
778
779 num_free_entries = num_entries;
780 min_free_entries = num_entries;
781
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200782 pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
Joerg Roedel6bf07872009-01-09 12:54:42 +0100783
784 return 0;
785
786out_err:
787
788 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
789 list_del(&entry->list);
790 kfree(entry);
791 }
792
793 return -ENOMEM;
794}
795
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200796static ssize_t filter_read(struct file *file, char __user *user_buf,
797 size_t count, loff_t *ppos)
798{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200799 char buf[NAME_MAX_LEN + 1];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200800 unsigned long flags;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200801 int len;
802
803 if (!current_driver_name[0])
804 return 0;
805
806 /*
807 * We can't copy to userspace directly because current_driver_name can
808 * only be read under the driver_name_lock with irqs disabled. So
809 * create a temporary copy first.
810 */
811 read_lock_irqsave(&driver_name_lock, flags);
812 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
813 read_unlock_irqrestore(&driver_name_lock, flags);
814
815 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
816}
817
818static ssize_t filter_write(struct file *file, const char __user *userbuf,
819 size_t count, loff_t *ppos)
820{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200821 char buf[NAME_MAX_LEN];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200822 unsigned long flags;
823 size_t len;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200824 int i;
825
826 /*
827 * We can't copy from userspace directly. Access to
828 * current_driver_name is protected with a write_lock with irqs
829 * disabled. Since copy_from_user can fault and may sleep we
830 * need to copy to temporary buffer first
831 */
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200832 len = min(count, (size_t)(NAME_MAX_LEN - 1));
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200833 if (copy_from_user(buf, userbuf, len))
834 return -EFAULT;
835
836 buf[len] = 0;
837
838 write_lock_irqsave(&driver_name_lock, flags);
839
Joerg Roedel312325092009-06-08 15:07:08 +0200840 /*
841 * Now handle the string we got from userspace very carefully.
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200842 * The rules are:
843 * - only use the first token we got
844 * - token delimiter is everything looking like a space
845 * character (' ', '\n', '\t' ...)
846 *
847 */
848 if (!isalnum(buf[0])) {
849 /*
Joerg Roedel312325092009-06-08 15:07:08 +0200850 * If the first character userspace gave us is not
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200851 * alphanumerical then assume the filter should be
852 * switched off.
853 */
854 if (current_driver_name[0])
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200855 pr_info("DMA-API: switching off dma-debug driver filter\n");
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200856 current_driver_name[0] = 0;
857 current_driver = NULL;
858 goto out_unlock;
859 }
860
861 /*
862 * Now parse out the first token and use it as the name for the
863 * driver to filter for.
864 */
Dan Carpenter39a37ce2010-04-06 19:45:12 +0300865 for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200866 current_driver_name[i] = buf[i];
867 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
868 break;
869 }
870 current_driver_name[i] = 0;
871 current_driver = NULL;
872
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200873 pr_info("DMA-API: enable driver filter for driver [%s]\n",
874 current_driver_name);
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200875
876out_unlock:
877 write_unlock_irqrestore(&driver_name_lock, flags);
878
879 return count;
880}
881
Thiago Farinaaeb583d2010-01-18 18:57:33 -0500882static const struct file_operations filter_fops = {
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200883 .read = filter_read,
884 .write = filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200885 .llseek = default_llseek,
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200886};
887
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100888static int dma_debug_fs_init(void)
889{
890 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
891 if (!dma_debug_dent) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +0200892 pr_err("DMA-API: can not create debugfs directory\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100893 return -ENOMEM;
894 }
895
896 global_disable_dent = debugfs_create_bool("disabled", 0444,
897 dma_debug_dent,
Dan Carpenter68ee6d22012-06-27 12:08:55 +0300898 &global_disable);
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100899 if (!global_disable_dent)
900 goto out_err;
901
902 error_count_dent = debugfs_create_u32("error_count", 0444,
903 dma_debug_dent, &error_count);
904 if (!error_count_dent)
905 goto out_err;
906
907 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
908 dma_debug_dent,
909 &show_all_errors);
910 if (!show_all_errors_dent)
911 goto out_err;
912
913 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
914 dma_debug_dent,
915 &show_num_errors);
916 if (!show_num_errors_dent)
917 goto out_err;
918
919 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
920 dma_debug_dent,
921 &num_free_entries);
922 if (!num_free_entries_dent)
923 goto out_err;
924
925 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
926 dma_debug_dent,
927 &min_free_entries);
928 if (!min_free_entries_dent)
929 goto out_err;
930
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200931 filter_dent = debugfs_create_file("driver_filter", 0644,
932 dma_debug_dent, NULL, &filter_fops);
933 if (!filter_dent)
934 goto out_err;
935
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100936 return 0;
937
938out_err:
939 debugfs_remove_recursive(dma_debug_dent);
940
941 return -ENOMEM;
942}
943
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400944static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200945{
946 struct dma_debug_entry *entry;
947 unsigned long flags;
948 int count = 0, i;
949
950 for (i = 0; i < HASH_SIZE; ++i) {
Pankaj Gupta6a5cd602017-05-03 14:51:28 -0700951 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200952 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400953 if (entry->dev == dev) {
Joerg Roedeled888ae2009-05-22 17:16:04 +0200954 count += 1;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400955 *out_entry = entry;
956 }
Joerg Roedeled888ae2009-05-22 17:16:04 +0200957 }
Pankaj Gupta6a5cd602017-05-03 14:51:28 -0700958 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200959 }
960
961 return count;
962}
963
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100964static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
Joerg Roedeled888ae2009-05-22 17:16:04 +0200965{
966 struct device *dev = data;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400967 struct dma_debug_entry *uninitialized_var(entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200968 int count;
969
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800970 if (dma_debug_disabled())
Ingo Molnara8fe9ea2009-12-31 15:16:23 +0100971 return 0;
Joerg Roedeled888ae2009-05-22 17:16:04 +0200972
973 switch (action) {
974 case BUS_NOTIFY_UNBOUND_DRIVER:
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400975 count = device_dma_allocations(dev, &entry);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200976 if (count == 0)
977 break;
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400978 err_printk(dev, entry, "DMA-API: device driver has pending "
Joerg Roedeled888ae2009-05-22 17:16:04 +0200979 "DMA allocations while released from device "
Stanislaw Gruszkaba4b87ad2011-03-31 08:08:09 -0400980 "[count=%d]\n"
981 "One of leaked entries details: "
982 "[device address=0x%016llx] [size=%llu bytes] "
983 "[mapped with %s] [mapped as %s]\n",
984 count, entry->dev_addr, entry->size,
985 dir2name[entry->direction], type2name[entry->type]);
Joerg Roedeled888ae2009-05-22 17:16:04 +0200986 break;
987 default:
988 break;
989 }
990
991 return 0;
992}
993
Joerg Roedel41531c82009-03-16 17:32:14 +0100994void dma_debug_add_bus(struct bus_type *bus)
995{
Joerg Roedeled888ae2009-05-22 17:16:04 +0200996 struct notifier_block *nb;
997
Florian Fainelli01ce18b2014-12-10 15:41:23 -0800998 if (dma_debug_disabled())
Shaun Ruffellf797d982009-12-17 18:00:36 -0600999 return;
1000
Joerg Roedeled888ae2009-05-22 17:16:04 +02001001 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1002 if (nb == NULL) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001003 pr_err("dma_debug_add_bus: out of memory\n");
Joerg Roedeled888ae2009-05-22 17:16:04 +02001004 return;
1005 }
1006
1007 nb->notifier_call = dma_debug_device_change;
1008
1009 bus_register_notifier(bus, nb);
Joerg Roedel41531c82009-03-16 17:32:14 +01001010}
Joerg Roedel788dcfa2009-01-09 13:13:27 +01001011
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001012static int dma_debug_init(void)
Joerg Roedel6bf07872009-01-09 12:54:42 +01001013{
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001014 u32 num_entries;
Joerg Roedel6bf07872009-01-09 12:54:42 +01001015 int i;
1016
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -08001017 /* Do not use dma_debug_initialized here, since we really want to be
1018 * called to set dma_debug_initialized
1019 */
1020 if (global_disable)
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001021 return 0;
Joerg Roedel6bf07872009-01-09 12:54:42 +01001022
1023 for (i = 0; i < HASH_SIZE; ++i) {
1024 INIT_LIST_HEAD(&dma_entry_hash[i].list);
Ingo Molnarb0a5b832009-06-16 16:11:14 +02001025 spin_lock_init(&dma_entry_hash[i].lock);
Joerg Roedel6bf07872009-01-09 12:54:42 +01001026 }
1027
Joerg Roedel788dcfa2009-01-09 13:13:27 +01001028 if (dma_debug_fs_init() != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001029 pr_err("DMA-API: error creating debugfs entries - disabling\n");
Joerg Roedel788dcfa2009-01-09 13:13:27 +01001030 global_disable = true;
1031
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001032 return 0;
Joerg Roedel788dcfa2009-01-09 13:13:27 +01001033 }
1034
Joerg Roedel59d3daa2009-01-09 13:01:56 +01001035 if (req_entries)
1036 num_entries = req_entries;
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001037 else
1038 num_entries = PREALLOC_DMA_DEBUG_ENTRIES;
Joerg Roedel59d3daa2009-01-09 13:01:56 +01001039
Joerg Roedel6bf07872009-01-09 12:54:42 +01001040 if (prealloc_memory(num_entries) != 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001041 pr_err("DMA-API: debugging out of memory error - disabled\n");
Joerg Roedel6bf07872009-01-09 12:54:42 +01001042 global_disable = true;
1043
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001044 return 0;
Joerg Roedel6bf07872009-01-09 12:54:42 +01001045 }
1046
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +09001047 nr_total_entries = num_free_entries;
1048
Florian Fainelli2ce8e7e2014-12-10 15:41:25 -08001049 dma_debug_initialized = true;
1050
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001051 pr_info("DMA-API: debugging enabled by kernel config\n");
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001052 return 0;
Joerg Roedel6bf07872009-01-09 12:54:42 +01001053}
Christoph Hellwig15b28bb2018-04-16 17:22:28 +02001054core_initcall(dma_debug_init);
Joerg Roedel6bf07872009-01-09 12:54:42 +01001055
Joerg Roedel59d3daa2009-01-09 13:01:56 +01001056static __init int dma_debug_cmdline(char *str)
1057{
1058 if (!str)
1059 return -EINVAL;
1060
1061 if (strncmp(str, "off", 3) == 0) {
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001062 pr_info("DMA-API: debugging disabled on kernel command line\n");
Joerg Roedel59d3daa2009-01-09 13:01:56 +01001063 global_disable = true;
1064 }
1065
1066 return 0;
1067}
1068
1069static __init int dma_debug_entries_cmdline(char *str)
1070{
1071 int res;
1072
1073 if (!str)
1074 return -EINVAL;
1075
1076 res = get_option(&str, &req_entries);
1077
1078 if (!res)
1079 req_entries = 0;
1080
1081 return 0;
1082}
1083
1084__setup("dma_debug=", dma_debug_cmdline);
1085__setup("dma_debug_entries=", dma_debug_entries_cmdline);
1086
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001087static void check_unmap(struct dma_debug_entry *ref)
1088{
1089 struct dma_debug_entry *entry;
1090 struct hash_bucket *bucket;
1091 unsigned long flags;
1092
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001093 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001094 entry = bucket_find_exact(bucket, ref);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001095
1096 if (!entry) {
Alexander Duyck8d640a52013-03-22 15:04:48 -07001097 /* must drop lock before calling dma_mapping_error */
1098 put_hash_bucket(bucket, &flags);
1099
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001100 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
1101 err_printk(ref->dev, NULL,
Alexander Duyck8d640a52013-03-22 15:04:48 -07001102 "DMA-API: device driver tries to free an "
1103 "invalid DMA memory address\n");
1104 } else {
1105 err_printk(ref->dev, NULL,
1106 "DMA-API: device driver tries to free DMA "
1107 "memory it has not allocated [device "
1108 "address=0x%016llx] [size=%llu bytes]\n",
1109 ref->dev_addr, ref->size);
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001110 }
Alexander Duyck8d640a52013-03-22 15:04:48 -07001111 return;
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001112 }
1113
1114 if (ref->size != entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001115 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001116 "DMA memory with different size "
1117 "[device address=0x%016llx] [map size=%llu bytes] "
1118 "[unmap size=%llu bytes]\n",
1119 ref->dev_addr, entry->size, ref->size);
1120 }
1121
1122 if (ref->type != entry->type) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001123 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001124 "DMA memory with wrong function "
1125 "[device address=0x%016llx] [size=%llu bytes] "
1126 "[mapped as %s] [unmapped as %s]\n",
1127 ref->dev_addr, ref->size,
1128 type2name[entry->type], type2name[ref->type]);
1129 } else if ((entry->type == dma_debug_coherent) &&
Dan Williams0abdd7a2014-01-21 15:48:12 -08001130 (phys_addr(ref) != phys_addr(entry))) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001131 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001132 "DMA memory with different CPU address "
1133 "[device address=0x%016llx] [size=%llu bytes] "
Joerg Roedel59a40e702009-10-29 16:25:50 +01001134 "[cpu alloc address=0x%016llx] "
1135 "[cpu free address=0x%016llx]",
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001136 ref->dev_addr, ref->size,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001137 phys_addr(entry),
1138 phys_addr(ref));
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001139 }
1140
1141 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1142 ref->sg_call_ents != entry->sg_call_ents) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001143 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001144 "DMA sg list with different entry count "
1145 "[map count=%d] [unmap count=%d]\n",
1146 entry->sg_call_ents, ref->sg_call_ents);
1147 }
1148
1149 /*
1150 * This may be no bug in reality - but most implementations of the
1151 * DMA API don't handle this properly, so check for it here
1152 */
1153 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001154 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001155 "DMA memory with different direction "
1156 "[device address=0x%016llx] [size=%llu bytes] "
1157 "[mapped with %s] [unmapped with %s]\n",
1158 ref->dev_addr, ref->size,
1159 dir2name[entry->direction],
1160 dir2name[ref->direction]);
1161 }
1162
Miles Chena5759b22017-02-22 15:40:09 -08001163 /*
1164 * Drivers should use dma_mapping_error() to check the returned
1165 * addresses of dma_map_single() and dma_map_page().
1166 * If not, print this warning message. See Documentation/DMA-API.txt.
1167 */
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001168 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1169 err_printk(ref->dev, entry,
1170 "DMA-API: device driver failed to check map error"
1171 "[device address=0x%016llx] [size=%llu bytes] "
1172 "[mapped as %s]",
1173 ref->dev_addr, ref->size,
1174 type2name[entry->type]);
1175 }
1176
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001177 hash_bucket_del(entry);
1178 dma_entry_free(entry);
1179
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001180 put_hash_bucket(bucket, &flags);
1181}
1182
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001183static void check_for_stack(struct device *dev,
1184 struct page *page, size_t offset)
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001185{
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001186 void *addr;
1187 struct vm_struct *stack_vm_area = task_stack_vm_area(current);
1188
1189 if (!stack_vm_area) {
1190 /* Stack is direct-mapped. */
1191 if (PageHighMem(page))
1192 return;
1193 addr = page_address(page) + offset;
1194 if (object_is_on_stack(addr))
1195 err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [addr=%p]\n", addr);
1196 } else {
1197 /* Stack is vmalloced. */
1198 int i;
1199
1200 for (i = 0; i < stack_vm_area->nr_pages; i++) {
1201 if (page != stack_vm_area->pages[i])
1202 continue;
1203
1204 addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
1205 err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [probable addr=%p]\n", addr);
1206 break;
1207 }
1208 }
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001209}
1210
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001211static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001212{
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001213 unsigned long a1 = (unsigned long)addr;
1214 unsigned long b1 = a1 + len;
1215 unsigned long a2 = (unsigned long)start;
1216 unsigned long b2 = (unsigned long)end;
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001217
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001218 return !(b1 <= a2 || a1 >= b2);
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001219}
1220
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001221static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001222{
Laura Abbottea535e42016-01-14 15:16:50 -08001223 if (overlap(addr, len, _stext, _etext) ||
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001224 overlap(addr, len, __start_rodata, __end_rodata))
1225 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 +01001226}
1227
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001228static void check_sync(struct device *dev,
1229 struct dma_debug_entry *ref,
1230 bool to_cpu)
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001231{
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001232 struct dma_debug_entry *entry;
1233 struct hash_bucket *bucket;
1234 unsigned long flags;
1235
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001236 bucket = get_hash_bucket(ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001237
Neil Hormanc6a21d02011-08-08 15:13:54 -04001238 entry = bucket_find_contain(&bucket, ref, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001239
1240 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001241 err_printk(dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001242 "to sync DMA memory it has not allocated "
1243 "[device address=0x%016llx] [size=%llu bytes]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001244 (unsigned long long)ref->dev_addr, ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001245 goto out;
1246 }
1247
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001248 if (ref->size > entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001249 err_printk(dev, entry, "DMA-API: device driver syncs"
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001250 " DMA memory outside allocated range "
1251 "[device address=0x%016llx] "
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001252 "[allocation size=%llu bytes] "
1253 "[sync offset+size=%llu]\n",
1254 entry->dev_addr, entry->size,
1255 ref->size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001256 }
1257
Krzysztof Halasa42d53b42010-01-08 14:42:36 -08001258 if (entry->direction == DMA_BIDIRECTIONAL)
1259 goto out;
1260
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001261 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +01001262 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001263 "DMA memory with different direction "
1264 "[device address=0x%016llx] [size=%llu bytes] "
1265 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001266 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001267 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001268 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001269 }
1270
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001271 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001272 !(ref->direction == DMA_TO_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +01001273 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001274 "device read-only DMA memory for cpu "
1275 "[device address=0x%016llx] [size=%llu bytes] "
1276 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001277 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001278 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001279 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001280
1281 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001282 !(ref->direction == DMA_FROM_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +01001283 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001284 "device write-only DMA memory to device "
1285 "[device address=0x%016llx] [size=%llu bytes] "
1286 "[mapped with %s] [synced with %s]\n",
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001287 (unsigned long long)ref->dev_addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001288 dir2name[entry->direction],
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001289 dir2name[ref->direction]);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001290
Robin Murphy7f830642015-11-06 16:32:55 -08001291 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1292 ref->sg_call_ents != entry->sg_call_ents) {
1293 err_printk(ref->dev, entry, "DMA-API: device driver syncs "
1294 "DMA sg list with different entry count "
1295 "[map count=%d] [sync count=%d]\n",
1296 entry->sg_call_ents, ref->sg_call_ents);
1297 }
1298
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001299out:
1300 put_hash_bucket(bucket, &flags);
Joerg Roedel2d62ece2009-01-09 14:10:26 +01001301}
1302
Joerg Roedelf62bc982009-01-09 14:14:49 +01001303void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
1304 size_t size, int direction, dma_addr_t dma_addr,
1305 bool map_single)
1306{
1307 struct dma_debug_entry *entry;
1308
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001309 if (unlikely(dma_debug_disabled()))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001310 return;
1311
Shuah Khanbfe0fb02012-11-03 17:00:07 -06001312 if (dma_mapping_error(dev, dma_addr))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001313 return;
1314
1315 entry = dma_entry_alloc();
1316 if (!entry)
1317 return;
1318
1319 entry->dev = dev;
1320 entry->type = dma_debug_page;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001321 entry->pfn = page_to_pfn(page);
1322 entry->offset = offset,
Joerg Roedelf62bc982009-01-09 14:14:49 +01001323 entry->dev_addr = dma_addr;
1324 entry->size = size;
1325 entry->direction = direction;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001326 entry->map_err_type = MAP_ERR_NOT_CHECKED;
Joerg Roedelf62bc982009-01-09 14:14:49 +01001327
Joerg Roedel9537a482009-03-23 15:35:08 +01001328 if (map_single)
Joerg Roedelf62bc982009-01-09 14:14:49 +01001329 entry->type = dma_debug_single;
Joerg Roedel9537a482009-03-23 15:35:08 +01001330
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001331 check_for_stack(dev, page, offset);
1332
Joerg Roedel9537a482009-03-23 15:35:08 +01001333 if (!PageHighMem(page)) {
Ingo Molnarf39d1b92009-07-10 21:38:02 +02001334 void *addr = page_address(page) + offset;
1335
Joerg Roedel2e34bde2009-03-16 16:51:55 +01001336 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +01001337 }
1338
1339 add_dma_entry(entry);
1340}
1341EXPORT_SYMBOL(debug_dma_map_page);
1342
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001343void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
1344{
1345 struct dma_debug_entry ref;
1346 struct dma_debug_entry *entry;
1347 struct hash_bucket *bucket;
1348 unsigned long flags;
1349
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001350 if (unlikely(dma_debug_disabled()))
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001351 return;
1352
1353 ref.dev = dev;
1354 ref.dev_addr = dma_addr;
1355 bucket = get_hash_bucket(&ref, &flags);
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001356
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001357 list_for_each_entry(entry, &bucket->list, list) {
1358 if (!exact_match(&ref, entry))
1359 continue;
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001360
Alexander Duyck96e7d7a2013-03-22 15:04:49 -07001361 /*
1362 * The same physical address can be mapped multiple
1363 * times. Without a hardware IOMMU this results in the
1364 * same device addresses being put into the dma-debug
1365 * hash multiple times too. This can result in false
1366 * positives being reported. Therefore we implement a
1367 * best-fit algorithm here which updates the first entry
1368 * from the hash which fits the reference value and is
1369 * not currently listed as being checked.
1370 */
1371 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1372 entry->map_err_type = MAP_ERR_CHECKED;
1373 break;
1374 }
1375 }
1376
Shuah Khan6c9c6d62012-10-08 11:08:06 -06001377 put_hash_bucket(bucket, &flags);
1378}
1379EXPORT_SYMBOL(debug_dma_mapping_error);
1380
Joerg Roedelf62bc982009-01-09 14:14:49 +01001381void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
1382 size_t size, int direction, bool map_single)
1383{
1384 struct dma_debug_entry ref = {
1385 .type = dma_debug_page,
1386 .dev = dev,
1387 .dev_addr = addr,
1388 .size = size,
1389 .direction = direction,
1390 };
1391
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001392 if (unlikely(dma_debug_disabled()))
Joerg Roedelf62bc982009-01-09 14:14:49 +01001393 return;
1394
1395 if (map_single)
1396 ref.type = dma_debug_single;
1397
1398 check_unmap(&ref);
1399}
1400EXPORT_SYMBOL(debug_dma_unmap_page);
1401
Joerg Roedel972aa452009-01-09 14:19:54 +01001402void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1403 int nents, int mapped_ents, int direction)
1404{
1405 struct dma_debug_entry *entry;
1406 struct scatterlist *s;
1407 int i;
1408
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001409 if (unlikely(dma_debug_disabled()))
Joerg Roedel972aa452009-01-09 14:19:54 +01001410 return;
1411
1412 for_each_sg(sg, s, mapped_ents, i) {
1413 entry = dma_entry_alloc();
1414 if (!entry)
1415 return;
1416
1417 entry->type = dma_debug_sg;
1418 entry->dev = dev;
Dan Williams0abdd7a2014-01-21 15:48:12 -08001419 entry->pfn = page_to_pfn(sg_page(s));
1420 entry->offset = s->offset,
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001421 entry->size = sg_dma_len(s);
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001422 entry->dev_addr = sg_dma_address(s);
Joerg Roedel972aa452009-01-09 14:19:54 +01001423 entry->direction = direction;
1424 entry->sg_call_ents = nents;
1425 entry->sg_mapped_ents = mapped_ents;
1426
Andy Lutomirskib4a0f532016-08-11 02:35:22 -07001427 check_for_stack(dev, sg_page(s), s->offset);
1428
Joerg Roedel9537a482009-03-23 15:35:08 +01001429 if (!PageHighMem(sg_page(s))) {
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001430 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
Joerg Roedel9537a482009-03-23 15:35:08 +01001431 }
Joerg Roedel972aa452009-01-09 14:19:54 +01001432
1433 add_dma_entry(entry);
1434 }
1435}
1436EXPORT_SYMBOL(debug_dma_map_sg);
1437
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001438static int get_nr_mapped_entries(struct device *dev,
1439 struct dma_debug_entry *ref)
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001440{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001441 struct dma_debug_entry *entry;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001442 struct hash_bucket *bucket;
1443 unsigned long flags;
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001444 int mapped_ents;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001445
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001446 bucket = get_hash_bucket(ref, &flags);
Neil Hormanc6a21d02011-08-08 15:13:54 -04001447 entry = bucket_find_exact(bucket, ref);
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001448 mapped_ents = 0;
1449
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001450 if (entry)
1451 mapped_ents = entry->sg_mapped_ents;
1452 put_hash_bucket(bucket, &flags);
1453
1454 return mapped_ents;
1455}
1456
Joerg Roedel972aa452009-01-09 14:19:54 +01001457void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1458 int nelems, int dir)
1459{
Joerg Roedel972aa452009-01-09 14:19:54 +01001460 struct scatterlist *s;
1461 int mapped_ents = 0, i;
Joerg Roedel972aa452009-01-09 14:19:54 +01001462
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001463 if (unlikely(dma_debug_disabled()))
Joerg Roedel972aa452009-01-09 14:19:54 +01001464 return;
1465
1466 for_each_sg(sglist, s, nelems, i) {
1467
1468 struct dma_debug_entry ref = {
1469 .type = dma_debug_sg,
1470 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001471 .pfn = page_to_pfn(sg_page(s)),
1472 .offset = s->offset,
FUJITA Tomonori15aedea2009-05-27 09:43:01 +09001473 .dev_addr = sg_dma_address(s),
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001474 .size = sg_dma_len(s),
Joerg Roedel972aa452009-01-09 14:19:54 +01001475 .direction = dir,
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001476 .sg_call_ents = nelems,
Joerg Roedel972aa452009-01-09 14:19:54 +01001477 };
1478
1479 if (mapped_ents && i >= mapped_ents)
1480 break;
1481
Joerg Roedele5e8c5b2009-06-11 10:03:42 +02001482 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001483 mapped_ents = get_nr_mapped_entries(dev, &ref);
Joerg Roedel972aa452009-01-09 14:19:54 +01001484
1485 check_unmap(&ref);
1486 }
1487}
1488EXPORT_SYMBOL(debug_dma_unmap_sg);
1489
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001490void debug_dma_alloc_coherent(struct device *dev, size_t size,
1491 dma_addr_t dma_addr, void *virt)
1492{
1493 struct dma_debug_entry *entry;
1494
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001495 if (unlikely(dma_debug_disabled()))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001496 return;
1497
1498 if (unlikely(virt == NULL))
1499 return;
1500
Miles Chenaf1da682018-02-22 19:22:20 +08001501 /* handle vmalloc and linear addresses */
1502 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001503 return;
1504
Miles Chenaf1da682018-02-22 19:22:20 +08001505 entry = dma_entry_alloc();
1506 if (!entry)
Miles Chen3aaabbf2017-11-17 15:26:19 -08001507 return;
1508
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001509 entry->type = dma_debug_coherent;
1510 entry->dev = dev;
Geliang Tange57d0552017-04-22 09:18:05 +08001511 entry->offset = offset_in_page(virt);
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001512 entry->size = size;
1513 entry->dev_addr = dma_addr;
1514 entry->direction = DMA_BIDIRECTIONAL;
1515
Miles Chen3aaabbf2017-11-17 15:26:19 -08001516 if (is_vmalloc_addr(virt))
1517 entry->pfn = vmalloc_to_pfn(virt);
1518 else
1519 entry->pfn = page_to_pfn(virt_to_page(virt));
1520
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001521 add_dma_entry(entry);
1522}
1523EXPORT_SYMBOL(debug_dma_alloc_coherent);
1524
1525void debug_dma_free_coherent(struct device *dev, size_t size,
1526 void *virt, dma_addr_t addr)
1527{
1528 struct dma_debug_entry ref = {
1529 .type = dma_debug_coherent,
1530 .dev = dev,
Geliang Tange57d0552017-04-22 09:18:05 +08001531 .offset = offset_in_page(virt),
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001532 .dev_addr = addr,
1533 .size = size,
1534 .direction = DMA_BIDIRECTIONAL,
1535 };
1536
Miles Chen3aaabbf2017-11-17 15:26:19 -08001537 /* handle vmalloc and linear addresses */
Miles Chenaf1da682018-02-22 19:22:20 +08001538 if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
Miles Chen3aaabbf2017-11-17 15:26:19 -08001539 return;
1540
1541 if (is_vmalloc_addr(virt))
1542 ref.pfn = vmalloc_to_pfn(virt);
1543 else
1544 ref.pfn = page_to_pfn(virt_to_page(virt));
1545
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001546 if (unlikely(dma_debug_disabled()))
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001547 return;
1548
1549 check_unmap(&ref);
1550}
1551EXPORT_SYMBOL(debug_dma_free_coherent);
1552
Niklas Söderlund0e74b342016-08-10 13:22:15 +02001553void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
1554 int direction, dma_addr_t dma_addr)
1555{
1556 struct dma_debug_entry *entry;
1557
1558 if (unlikely(dma_debug_disabled()))
1559 return;
1560
1561 entry = dma_entry_alloc();
1562 if (!entry)
1563 return;
1564
1565 entry->type = dma_debug_resource;
1566 entry->dev = dev;
Niklas Söderlund2e0cc302016-09-29 21:59:15 +02001567 entry->pfn = PHYS_PFN(addr);
Niklas Söderlund0e74b342016-08-10 13:22:15 +02001568 entry->offset = offset_in_page(addr);
1569 entry->size = size;
1570 entry->dev_addr = dma_addr;
1571 entry->direction = direction;
1572 entry->map_err_type = MAP_ERR_NOT_CHECKED;
1573
1574 add_dma_entry(entry);
1575}
1576EXPORT_SYMBOL(debug_dma_map_resource);
1577
1578void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
1579 size_t size, int direction)
1580{
1581 struct dma_debug_entry ref = {
1582 .type = dma_debug_resource,
1583 .dev = dev,
1584 .dev_addr = dma_addr,
1585 .size = size,
1586 .direction = direction,
1587 };
1588
1589 if (unlikely(dma_debug_disabled()))
1590 return;
1591
1592 check_unmap(&ref);
1593}
1594EXPORT_SYMBOL(debug_dma_unmap_resource);
1595
Joerg Roedelb9d23172009-01-09 14:43:04 +01001596void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1597 size_t size, int direction)
1598{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001599 struct dma_debug_entry ref;
1600
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001601 if (unlikely(dma_debug_disabled()))
Joerg Roedelb9d23172009-01-09 14:43:04 +01001602 return;
1603
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001604 ref.type = dma_debug_single;
1605 ref.dev = dev;
1606 ref.dev_addr = dma_handle;
1607 ref.size = size;
1608 ref.direction = direction;
1609 ref.sg_call_ents = 0;
1610
1611 check_sync(dev, &ref, true);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001612}
1613EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1614
1615void debug_dma_sync_single_for_device(struct device *dev,
1616 dma_addr_t dma_handle, size_t size,
1617 int direction)
1618{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001619 struct dma_debug_entry ref;
1620
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001621 if (unlikely(dma_debug_disabled()))
Joerg Roedelb9d23172009-01-09 14:43:04 +01001622 return;
1623
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001624 ref.type = dma_debug_single;
1625 ref.dev = dev;
1626 ref.dev_addr = dma_handle;
1627 ref.size = size;
1628 ref.direction = direction;
1629 ref.sg_call_ents = 0;
1630
1631 check_sync(dev, &ref, false);
Joerg Roedelb9d23172009-01-09 14:43:04 +01001632}
1633EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1634
Joerg Roedel948408b2009-01-09 14:55:38 +01001635void debug_dma_sync_single_range_for_cpu(struct device *dev,
1636 dma_addr_t dma_handle,
1637 unsigned long offset, size_t size,
1638 int direction)
1639{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001640 struct dma_debug_entry ref;
1641
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001642 if (unlikely(dma_debug_disabled()))
Joerg Roedel948408b2009-01-09 14:55:38 +01001643 return;
1644
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001645 ref.type = dma_debug_single;
1646 ref.dev = dev;
1647 ref.dev_addr = dma_handle;
1648 ref.size = offset + size;
1649 ref.direction = direction;
1650 ref.sg_call_ents = 0;
1651
1652 check_sync(dev, &ref, true);
Joerg Roedel948408b2009-01-09 14:55:38 +01001653}
1654EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1655
1656void debug_dma_sync_single_range_for_device(struct device *dev,
1657 dma_addr_t dma_handle,
1658 unsigned long offset,
1659 size_t size, int direction)
1660{
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001661 struct dma_debug_entry ref;
1662
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001663 if (unlikely(dma_debug_disabled()))
Joerg Roedel948408b2009-01-09 14:55:38 +01001664 return;
1665
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001666 ref.type = dma_debug_single;
1667 ref.dev = dev;
1668 ref.dev_addr = dma_handle;
1669 ref.size = offset + size;
1670 ref.direction = direction;
1671 ref.sg_call_ents = 0;
1672
1673 check_sync(dev, &ref, false);
Joerg Roedel948408b2009-01-09 14:55:38 +01001674}
1675EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1676
Joerg Roedela31fba52009-01-09 15:01:12 +01001677void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1678 int nelems, int direction)
1679{
1680 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001681 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001682
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001683 if (unlikely(dma_debug_disabled()))
Joerg Roedela31fba52009-01-09 15:01:12 +01001684 return;
1685
1686 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001687
1688 struct dma_debug_entry ref = {
1689 .type = dma_debug_sg,
1690 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001691 .pfn = page_to_pfn(sg_page(s)),
1692 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001693 .dev_addr = sg_dma_address(s),
1694 .size = sg_dma_len(s),
1695 .direction = direction,
1696 .sg_call_ents = nelems,
1697 };
1698
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001699 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001700 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001701
1702 if (i >= mapped_ents)
1703 break;
1704
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001705 check_sync(dev, &ref, true);
Joerg Roedela31fba52009-01-09 15:01:12 +01001706 }
1707}
1708EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1709
1710void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1711 int nelems, int direction)
1712{
1713 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001714 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001715
Florian Fainelli01ce18b2014-12-10 15:41:23 -08001716 if (unlikely(dma_debug_disabled()))
Joerg Roedela31fba52009-01-09 15:01:12 +01001717 return;
1718
1719 for_each_sg(sg, s, nelems, i) {
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001720
1721 struct dma_debug_entry ref = {
1722 .type = dma_debug_sg,
1723 .dev = dev,
Dan Williams0abdd7a2014-01-21 15:48:12 -08001724 .pfn = page_to_pfn(sg_page(s)),
1725 .offset = s->offset,
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001726 .dev_addr = sg_dma_address(s),
1727 .size = sg_dma_len(s),
1728 .direction = direction,
1729 .sg_call_ents = nelems,
1730 };
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001731 if (!i)
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001732 mapped_ents = get_nr_mapped_entries(dev, &ref);
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001733
1734 if (i >= mapped_ents)
1735 break;
1736
Joerg Roedelaa010ef2009-06-12 15:25:06 +02001737 check_sync(dev, &ref, false);
Joerg Roedela31fba52009-01-09 15:01:12 +01001738 }
1739}
1740EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1741
Joerg Roedel1745de52009-05-22 21:49:51 +02001742static int __init dma_debug_driver_setup(char *str)
1743{
1744 int i;
1745
1746 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1747 current_driver_name[i] = *str;
1748 if (*str == 0)
1749 break;
1750 }
1751
1752 if (current_driver_name[0])
Joerg Roedele7ed70e2009-06-08 15:39:24 +02001753 pr_info("DMA-API: enable driver filter for driver [%s]\n",
1754 current_driver_name);
Joerg Roedel1745de52009-05-22 21:49:51 +02001755
1756
1757 return 1;
1758}
1759__setup("dma_debug_driver=", dma_debug_driver_setup);